/
watch.go
389 lines (331 loc) · 10.3 KB
/
watch.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
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pipeline
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"github.com/go-gst/go-glib/glib"
"github.com/go-gst/go-gst/gst"
"github.com/livekit/egress/pkg/errors"
"github.com/livekit/egress/pkg/pipeline/builder"
"github.com/livekit/egress/pkg/pipeline/sink"
"github.com/livekit/egress/pkg/pipeline/source"
"github.com/livekit/egress/pkg/types"
"github.com/livekit/protocol/logger"
)
const (
// watch errors
msgClockProblem = "GStreamer error: clock problem."
msgStreamingNotNegotiated = "streaming stopped, reason not-negotiated (-4)"
msgMuxer = ":muxer"
elementGstRtmp2Sink = "GstRtmp2Sink"
elementGstAppSrc = "GstAppSrc"
elementSplitMuxSink = "GstSplitMuxSink"
// watch elements
msgFirstSampleMetadata = "FirstSampleMetadata"
msgFragmentOpened = "splitmuxsink-fragment-opened"
msgFragmentClosed = "splitmuxsink-fragment-closed"
msgGstMultiFileSink = "GstMultiFileSink"
fragmentLocation = "location"
fragmentRunningTime = "running-time"
gstMultiFileSinkFilename = "filename"
gstMultiFileSinkTimestamp = "timestamp"
// common gst errors
msgWrongThread = "Called from wrong thread"
// common gst warnings
msgKeyframe = "Could not request a keyframe. Files may not split at the exact location they should"
msgLatencyQuery = "Latency query failed"
msgTaps = "can't find exact taps"
msgInputDisappeared = "Can't copy metadata because input buffer disappeared"
// common gst fixmes
msgStreamStart = "stream-start event without group-id. Consider implementing group-id handling in the upstream elements"
msgCreatingStream = "Creating random stream-id, consider implementing a deterministic way of creating a stream-id"
msgAggregateSubclass = "Subclass should call gst_aggregator_selected_samples() from its aggregate implementation."
)
func (c *Controller) gstLog(level gst.DebugLevel, file, function string, line int, _ *glib.Object, message string) {
var lvl string
switch level {
case gst.LevelNone:
lvl = "none"
case gst.LevelError:
switch message {
case msgWrongThread:
// ignore
return
default:
lvl = "error"
}
case gst.LevelWarning:
switch message {
case msgKeyframe, msgLatencyQuery, msgTaps, msgInputDisappeared:
// ignore
return
default:
lvl = "warning"
}
case gst.LevelFixMe:
switch message {
case msgStreamStart, msgCreatingStream, msgAggregateSubclass:
// ignore
return
default:
lvl = "fixme"
}
case gst.LevelInfo:
lvl = "info"
case gst.LevelDebug:
lvl = "debug"
default:
lvl = "log"
}
var msg string
if function != "" {
msg = fmt.Sprintf("[gst %s] %s: %s", lvl, function, message)
} else {
msg = fmt.Sprintf("[gst %s] %s", lvl, message)
}
args := []interface{}{"caller", fmt.Sprintf("%s:%d", file, line)}
c.gstLogger.Debugw(msg, args...)
}
func (c *Controller) messageWatch(msg *gst.Message) bool {
var err error
switch msg.Type() {
case gst.MessageEOS:
logger.Infow("EOS received")
if c.eosTimer != nil {
c.eosTimer.Stop()
}
c.p.Stop()
return false
case gst.MessageWarning:
err = c.handleMessageWarning(msg.ParseWarning())
case gst.MessageError:
err = c.handleMessageError(msg.ParseError())
case gst.MessageStateChanged:
c.handleMessageStateChanged(msg)
case gst.MessageElement:
err = c.handleMessageElement(msg)
}
if err != nil {
if c.Debug.EnableProfiling {
c.uploadDebugFiles()
}
c.OnError(err)
return false
}
return true
}
func (c *Controller) handleMessageWarning(gErr *gst.GError) error {
element, _, message := parseDebugInfo(gErr)
if gErr.Message() == msgClockProblem {
err := errors.ErrGstPipelineError(gErr)
logger.Errorw(gErr.Error(), errors.New(message), "element", element)
return err
}
logger.Warnw(gErr.Message(), errors.New(message), "element", element)
return nil
}
// handleMessageError returns true if the error has been handled, false if the pipeline should quit
func (c *Controller) handleMessageError(gErr *gst.GError) error {
element, name, message := parseDebugInfo(gErr)
switch {
case element == elementGstRtmp2Sink:
name = strings.Split(name, "_")[1]
if !c.eos.IsBroken() {
// try reconnecting
ok, err := c.streamBin.MaybeResetStream(name, gErr)
if err != nil {
logger.Errorw("failed to reset stream", err)
} else if ok {
return nil
}
}
// remove sink
url, err := c.streamBin.GetStreamUrl(name)
if err != nil {
logger.Warnw("rtmp output not found", err, "url", url)
return err
}
return c.removeSink(context.Background(), url, gErr)
case element == elementGstAppSrc:
if message == msgStreamingNotNegotiated {
// send eos to app src
logger.Debugw("streaming stopped", "name", name)
c.src.(*source.SDKSource).StreamStopped(name)
return nil
}
case element == elementSplitMuxSink:
// We sometimes get GstSplitMuxSink errors if send EOS before the first media was sent to the mux
if message == msgMuxer {
if c.eos.IsBroken() {
logger.Debugw("GstSplitMuxSink failure after sending EOS")
return nil
}
}
}
// input failure or file write failure. Fatal
err := errors.ErrGstPipelineError(gErr)
logger.Errorw(gErr.Error(), errors.New(message), "element", name)
return err
}
// Debug info comes in the following format:
// file.c(line): method_name (): /GstPipeline:pipeline/GstBin:bin_name/GstElement:element_name:\nError message
var regExp = regexp.MustCompile("(?s)(.*?)GstPipeline:pipeline\\/GstBin:(.*?)\\/(.*?):([^:]*)(:\n)?(.*)")
func parseDebugInfo(gErr *gst.GError) (element, name, message string) {
match := regExp.FindStringSubmatch(gErr.DebugString())
element = match[3]
name = match[4]
message = match[6]
return
}
func (c *Controller) handleMessageStateChanged(msg *gst.Message) {
_, newState := msg.ParseStateChanged()
if newState != gst.StatePlaying {
return
}
s := msg.Source()
if s == pipelineName {
c.playing.Once(func() {
logger.Infow("pipeline playing")
c.updateStartTime(c.src.GetStartedAt())
})
} else if strings.HasPrefix(s, "app_") {
s = s[4:]
logger.Infow(fmt.Sprintf("%s playing", s))
c.src.(*source.SDKSource).Playing(s)
}
return
}
func (c *Controller) handleMessageElement(msg *gst.Message) error {
s := msg.GetStructure()
if s != nil {
switch s.Name() {
case msgFirstSampleMetadata:
startDate, err := getFirstSampleMetadataFromGstStructure(s)
if err != nil {
return err
}
logger.Debugw("received FirstSampleMetadata message", "startDate", startDate)
c.getSegmentSink().UpdateStartDate(startDate)
case msgFragmentOpened:
filepath, t, err := getSegmentParamsFromGstStructure(s)
if err != nil {
logger.Errorw("failed to retrieve segment parameters from event", err)
return err
}
if err = c.getSegmentSink().FragmentOpened(filepath, t); err != nil {
logger.Errorw("failed to register new segment with playlist writer", err, "location", filepath, "runningTime", t)
return err
}
case msgFragmentClosed:
filepath, t, err := getSegmentParamsFromGstStructure(s)
if err != nil {
logger.Errorw("failed to retrieve segment parameters from event", err, "location", filepath, "runningTime", t)
return err
}
// We need to dispatch to a queue to:
// 1. Avoid concurrent access to the SegmentsInfo structure
// 2. Ensure that playlists are uploaded in the same order they are enqueued to avoid an older playlist overwriting a newer one
if err = c.getSegmentSink().FragmentClosed(filepath, t); err != nil {
logger.Errorw("failed to end segment with playlist writer", err, "runningTime", t)
return err
}
case msgGstMultiFileSink:
location, ts, err := getImageInformationFromGstStructure(s)
if err != nil {
return err
}
logger.Debugw("received GstMultiFileSink message", "location", location, "timestamp", ts, "source", msg.Source())
imageSink := c.getImageSink(msg.Source())
if imageSink == nil {
return errors.ErrSinkNotFound
}
err = imageSink.NewImage(location, ts)
if err != nil {
return err
}
}
}
return nil
}
func getSegmentParamsFromGstStructure(s *gst.Structure) (filepath string, time uint64, err error) {
loc, err := s.GetValue(fragmentLocation)
if err != nil {
return "", 0, err
}
filepath, ok := loc.(string)
if !ok {
return "", 0, errors.ErrGstPipelineError(errors.New("invalid type for location"))
}
t, err := s.GetValue(fragmentRunningTime)
if err != nil {
return "", 0, err
}
ti, ok := t.(uint64)
if !ok {
return "", 0, errors.ErrGstPipelineError(errors.New("invalid type for time"))
}
return filepath, ti, nil
}
func getFirstSampleMetadataFromGstStructure(s *gst.Structure) (startDate time.Time, err error) {
firstSampleMetadata := builder.FirstSampleMetadata{}
err = s.UnmarshalInto(&firstSampleMetadata)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, firstSampleMetadata.StartDate), nil
}
func getImageInformationFromGstStructure(s *gst.Structure) (string, uint64, error) {
loc, err := s.GetValue(gstMultiFileSinkFilename)
if err != nil {
return "", 0, err
}
filepath, ok := loc.(string)
if !ok {
return "", 0, errors.ErrGstPipelineError(errors.New("invalid type for location"))
}
t, err := s.GetValue(gstMultiFileSinkTimestamp)
if err != nil {
return "", 0, err
}
ti, ok := t.(uint64)
if !ok {
return "", 0, errors.ErrGstPipelineError(errors.New("invalid type for time"))
}
return filepath, ti, nil
}
func (c *Controller) getSegmentSink() *sink.SegmentSink {
s := c.sinks[types.EgressTypeSegments]
if len(s) == 0 {
return nil
}
return s[0].(*sink.SegmentSink)
}
func (c *Controller) getImageSink(name string) *sink.ImageSink {
id := name[len("multifilesink_"):]
s := c.sinks[types.EgressTypeImages]
if len(s) == 0 {
return nil
}
// Use a map here?
for _, si := range s {
if si.(*sink.ImageSink).Id == id {
return si.(*sink.ImageSink)
}
}
return nil
}