-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathai_live_video.go
390 lines (353 loc) · 12 KB
/
ai_live_video.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"maps"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/livepeer/go-livepeer/clog"
"github.com/livepeer/go-livepeer/core"
"github.com/livepeer/go-livepeer/media"
"github.com/livepeer/go-livepeer/monitor"
"github.com/livepeer/go-livepeer/trickle"
"github.com/livepeer/lpms/ffmpeg"
)
func startTricklePublish(ctx context.Context, url *url.URL, params aiRequestParams, sess *AISession) {
ctx = clog.AddVal(ctx, "url", url.Redacted())
publisher, err := trickle.NewTricklePublisher(url.String())
if err != nil {
clog.Infof(ctx, "error publishing trickle. err=%s", err)
params.liveParams.stopPipeline(fmt.Errorf("Error publishing trickle %w", err))
return
}
// Start payments which probes a segment every "paymentProcessInterval" and sends a payment
ctx, cancel := context.WithCancel(ctx)
priceInfo := sess.OrchestratorInfo.PriceInfo
var paymentProcessor *LivePaymentProcessor
if priceInfo != nil && priceInfo.PricePerUnit != 0 {
paymentSender := livePaymentSender{}
sendPaymentFunc := func(inPixels int64) error {
return paymentSender.SendPayment(context.Background(), &SegmentInfoSender{
sess: sess.BroadcastSession,
inPixels: inPixels,
priceInfo: priceInfo,
mid: extractMid(url.Path),
})
}
paymentProcessor = NewLivePaymentProcessor(ctx, params.liveParams.paymentProcessInterval, sendPaymentFunc)
} else {
clog.Warningf(ctx, "No price info found from Orchestrator, Gateway will not send payments for the video processing")
}
slowOrchChecker := &SlowOrchChecker{}
params.liveParams.segmentReader.SwitchReader(func(reader media.CloneableReader) {
// check for end of stream
if _, eos := reader.(*media.EOSReader); eos {
if err := publisher.Close(); err != nil {
clog.Infof(ctx, "Error closing trickle publisher. err=%v", err)
}
cancel()
return
}
thisSeq, atMax := slowOrchChecker.BeginSegment()
if atMax {
clog.Infof(ctx, "Orchestrator is slow - terminating")
params.liveParams.stopPipeline(fmt.Errorf("slow orchestrator"))
cancel()
return
// TODO switch orchestrators
}
go func(seq int) {
defer slowOrchChecker.EndSegment()
var r io.Reader = reader
if paymentProcessor != nil {
r = paymentProcessor.process(reader)
}
clog.V(8).Infof(ctx, "trickle publish writing data seq=%d", seq)
segment, err := publisher.Next()
if err != nil {
clog.Infof(ctx, "error getting next publish handle; dropping segment err=%v", err)
params.liveParams.sendErrorEvent(fmt.Errorf("Missing next handle %v", err))
return
}
for {
currentSeq := slowOrchChecker.GetCount()
if seq != currentSeq {
clog.Infof(ctx, "Next segment has already started; skipping this one seq=%d currentSeq=%d", seq, currentSeq)
params.liveParams.sendErrorEvent(fmt.Errorf("Next segment has started"))
segment.Close()
return
}
n, err := segment.Write(r)
if err == nil {
// no error, all done, let's leave
return
}
if errors.Is(err, trickle.StreamNotFoundErr) {
clog.Infof(ctx, "Stream no longer exists on orchestrator; terminating")
params.liveParams.stopPipeline(fmt.Errorf("Stream does not exist"))
return
}
// Retry segment only if nothing has been sent yet
// and the next segment has not yet started
// otherwise drop
if n > 0 {
clog.Infof(ctx, "Error publishing segment; dropping remainder wrote=%d err=%v", n, err)
params.liveParams.sendErrorEvent(fmt.Errorf("Error publishing, wrote %d dropping %v", n, err))
segment.Close()
return
}
clog.Infof(ctx, "Error publishing segment before writing; retrying err=%v", err)
// Clone in case read head was incremented somewhere, which cloning ressets
r = reader.Clone()
time.Sleep(250 * time.Millisecond)
}
}(thisSeq)
})
clog.Infof(ctx, "trickle pub")
}
func startTrickleSubscribe(ctx context.Context, url *url.URL, params aiRequestParams) {
// subscribe to the outputs and send them into LPMS
subscriber := trickle.NewTrickleSubscriber(url.String())
r, w, err := os.Pipe()
if err != nil {
params.liveParams.stopPipeline(fmt.Errorf("error getting pipe for trickle-ffmpeg. url=%s %w", url, err))
return
}
ctx = clog.AddVal(ctx, "url", url.Redacted())
ctx = clog.AddVal(ctx, "outputRTMPURL", params.liveParams.outputRTMPURL)
// read segments from trickle subscription
go func() {
var err error
defer w.Close()
retries := 0
// we're trying to keep (retryPause x maxRetries) duration to fall within one output GOP length
const retryPause = 300 * time.Millisecond
const maxRetries = 5
for {
if !params.inputStreamExists() {
clog.Infof(ctx, "trickle subscribe stopping, input stream does not exist.")
break
}
var segment *http.Response
clog.V(8).Infof(ctx, "trickle subscribe read data begin")
segment, err = subscriber.Read()
if err != nil {
if errors.Is(err, trickle.EOS) || errors.Is(err, trickle.StreamNotFoundErr) {
params.liveParams.stopPipeline(fmt.Errorf("trickle subscribe end of stream: %w", err))
return
}
var sequenceNonexistent *trickle.SequenceNonexistent
if errors.As(err, &sequenceNonexistent) {
// stream exists but segment doesn't, so skip to leading edge
subscriber.SetSeq(sequenceNonexistent.Latest)
}
// TODO if not EOS then signal a new orchestrator is needed
err = fmt.Errorf("trickle subscribe error reading: %w", err)
clog.Infof(ctx, "%s", err)
if retries > maxRetries {
params.liveParams.stopPipeline(err)
return
}
retries++
params.liveParams.sendErrorEvent(err)
time.Sleep(retryPause)
continue
}
retries = 0
clog.V(8).Infof(ctx, "trickle subscribe read data end")
if err = copySegment(segment, w); err != nil {
params.liveParams.stopPipeline(fmt.Errorf("trickle subscribe error copying: %w", err))
return
}
}
}()
go func() {
defer r.Close()
for {
if !params.inputStreamExists() {
clog.Errorf(ctx, "Stopping output rtmp stream, input stream does not exist.")
break
}
_, err = ffmpeg.Transcode3(&ffmpeg.TranscodeOptionsIn{
Fname: fmt.Sprintf("pipe:%d", r.Fd()),
}, []ffmpeg.TranscodeOptions{{
Oname: params.liveParams.outputRTMPURL,
AudioEncoder: ffmpeg.ComponentOptions{Name: "copy"},
VideoEncoder: ffmpeg.ComponentOptions{Name: "copy"},
Muxer: ffmpeg.ComponentOptions{Name: "flv"},
}})
if err != nil {
clog.Infof(ctx, "Error sending RTMP out: %s", err)
}
time.Sleep(5 * time.Second)
}
}()
}
func copySegment(segment *http.Response, w io.Writer) error {
defer segment.Body.Close()
_, err := io.Copy(w, segment.Body)
return err
}
func startControlPublish(control *url.URL, params aiRequestParams) {
stream := params.liveParams.stream
controlPub, err := trickle.NewTricklePublisher(control.String())
if err != nil {
slog.Info("error starting control publisher", "stream", stream, "err", err)
return
}
params.node.LiveMu.Lock()
defer params.node.LiveMu.Unlock()
ticker := time.NewTicker(10 * time.Second)
done := make(chan bool, 1)
stop := func() {
ticker.Stop()
done <- true
}
params.node.LivePipelines[stream] = &core.LivePipeline{
ControlPub: controlPub,
StopControl: stop,
}
if monitor.Enabled {
monitor.AICurrentLiveSessions(len(params.node.LivePipelines))
}
// send a keepalive periodically to keep both ends of the connection alive
go func() {
for {
select {
case <-ticker.C:
const msg = `{"keep":"alive"}`
err := controlPub.Write(strings.NewReader(msg))
if err == trickle.StreamNotFoundErr {
// the channel doesn't exist anymore, so stop
stop()
continue // loop back to consume the `done` chan
}
// if there was another type of error, we'll just retry anyway
case <-done:
return
}
}
}()
}
func startEventsSubscribe(ctx context.Context, url *url.URL, params aiRequestParams, sess *AISession) {
subscriber := trickle.NewTrickleSubscriber(url.String())
stream := params.liveParams.stream
streamId := params.liveParams.streamID
clog.Infof(ctx, "Starting event subscription for URL: %s", url.String())
go func() {
defer StreamStatusStore.Clear(streamId)
for {
clog.V(8).Infof(ctx, "Reading from event subscription for URL: %s", url.String())
segment, err := subscriber.Read()
if err != nil {
clog.Infof(ctx, "Error reading events subscription: %s", err)
return
}
body, err := io.ReadAll(segment.Body)
segment.Body.Close()
if err != nil {
clog.Infof(ctx, "Error reading events subscription body: %s", err)
continue
}
var event map[string]interface{}
if err := json.Unmarshal(body, &event); err != nil {
clog.Infof(ctx, "Failed to parse JSON from events subscription: %s", err)
continue
}
event["stream_id"] = streamId
event["request_id"] = params.liveParams.requestID
event["pipeline_id"] = params.liveParams.pipelineID
if sess != nil {
event["orchestrator_info"] = map[string]interface{}{
"address": sess.Address(),
"url": sess.Transcoder(),
}
}
clog.V(8).Infof(ctx, "Received event for stream=%s event=%+v", stream, event)
eventType, ok := event["type"].(string)
if !ok {
eventType = "unknown"
clog.Warningf(ctx, "Received event without a type stream=%s event=%+v", stream, event)
}
queueEventType := "ai_stream_events"
if eventType == "status" {
queueEventType = "ai_stream_status"
// The large logs and params fields are only sent once and then cleared to save bandwidth. So coalesce the
// incoming status with the last non-null value that we received on such fields for the status API.
lastStreamStatus, _ := StreamStatusStore.Get(streamId)
// Check if inference_status exists in both current and last status
inferenceStatus, hasInference := event["inference_status"].(map[string]interface{})
lastInferenceStatus, hasLastInference := lastStreamStatus["inference_status"].(map[string]interface{})
if hasInference {
if logs, ok := inferenceStatus["last_restart_logs"]; !ok || logs == nil {
if hasLastInference {
inferenceStatus["last_restart_logs"] = lastInferenceStatus["last_restart_logs"]
}
}
if params, ok := inferenceStatus["last_params"]; !ok || params == nil {
if hasLastInference {
inferenceStatus["last_params"] = lastInferenceStatus["last_params"]
}
}
}
StreamStatusStore.Store(streamId, event)
}
monitor.SendQueueEventAsync(queueEventType, event)
}
}()
}
// Detect 'slow' orchs by keeping track of in-flight segments
// Count the difference between segments produced and segments completed
type SlowOrchChecker struct {
mu sync.Mutex
segmentCount int
completeCount int
}
// Number of in flight segments to allow.
// Should generally not be less than 1, because
// sometimes the beginning of the current segment
// may briefly overlap with the end of the previous segment
const maxInflightSegments = 3
// Returns the number of segments begun so far and
// whether the max number of inflight segments was hit.
// Number of segments is not incremented if inflight max is hit.
// If inflight max is hit, returns true, false otherwise.
func (s *SlowOrchChecker) BeginSegment() (int, bool) {
// Returns `false` if there are multiple segments in-flight
// this means the orchestrator is slow reading them
// If all-OK, returns `true`
s.mu.Lock()
defer s.mu.Unlock()
if s.segmentCount >= s.completeCount+maxInflightSegments {
// There is > 1 segment in flight ... orchestrator is slow reading
return s.segmentCount, true
}
s.segmentCount += 1
return s.segmentCount, false
}
func (s *SlowOrchChecker) EndSegment() {
s.mu.Lock()
defer s.mu.Unlock()
s.completeCount += 1
}
func (s *SlowOrchChecker) GetCount() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.segmentCount
}
func LiveErrorEventSender(ctx context.Context, event map[string]string) func(err error) {
return func(err error) {
ev := maps.Clone(event)
ev["capability"] = clog.GetVal(ctx, "capability")
ev["message"] = err.Error()
monitor.SendQueueEventAsync("ai_stream_events", ev)
}
}