Skip to content

Commit 504fa00

Browse files
committed
bump: gortsplib 2022-12-09
1 parent eab1b9f commit 504fa00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2053
-2125
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
*.swp
1111
*.backup
1212
/.vscode
13+
/new

addons/doods2/backend.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ func start(
9090
return fmt.Errorf("get detector: %w", err)
9191
}
9292

93-
infoCtx, infoCancel := context.WithTimeout(ctx, 30*time.Second)
94-
defer infoCancel()
95-
streamInfo, err := input.StreamInfo(infoCtx)
93+
streamInfo, err := input.StreamInfo(ctx)
9694
if err != nil {
9795
return fmt.Errorf("stream info: %w", err)
9896
}

addons/motion/backend.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ func run(
111111
config config,
112112
logf log.Func,
113113
) error {
114-
infoCtx, infoCancel := context.WithTimeout(ctx, 30*time.Second)
115-
defer infoCancel()
116-
streamInfo, err := i.StreamInfo(infoCtx)
114+
streamInfo, err := i.StreamInfo(ctx)
117115
if err != nil {
118116
return fmt.Errorf("stream info: %w", err)
119117
}

addons/watchdog/watchdog.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func onInputProcessStart(ctx context.Context, i *monitor.InputProcess, _ *[]stri
3333
})
3434
}
3535

36-
muxer := func() (muxer, error) {
37-
return i.HLSMuxer()
36+
muxer := func(ctx context.Context) (muxer, error) {
37+
return i.HLSMuxer(ctx)
3838
}
3939

4040
d := &watchdog{
@@ -51,7 +51,7 @@ type muxer interface {
5151
}
5252

5353
type watchdog struct {
54-
muxer func() (muxer, error)
54+
muxer func(context.Context) (muxer, error)
5555
interval time.Duration
5656
onFreeze func()
5757
logf func(log.Level, string, ...interface{})
@@ -74,9 +74,8 @@ func (d *watchdog) start(ctx context.Context) {
7474
return
7575
}
7676

77-
muxer, err := d.muxer()
77+
muxer, err := d.muxer(ctx)
7878
if err != nil {
79-
d.logf(log.LevelError, "could not get muxer")
8079
continue
8180
}
8281

addons/watchdog/watchdog_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type stubMuxer struct{}
1515

1616
func (m *stubMuxer) WaitForSegFinalized() {}
1717

18-
func newStubMuxer(err error) func() (muxer, error) {
19-
return func() (muxer, error) {
18+
func newStubMuxer(err error) func(context.Context) (muxer, error) {
19+
return func(context.Context) (muxer, error) {
2020
return &stubMuxer{}, err
2121
}
2222
}

pkg/monitor/monitor.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,7 @@ type SendEventFunc func(storage.Event) error
401401

402402
// SendEvent sends event to recorder.
403403
func (m *Monitor) SendEvent(event storage.Event) error {
404-
if m.ctx.Err() != nil {
405-
return context.Canceled
406-
}
407-
return m.recorder.sendEvent(event)
404+
return m.recorder.sendEvent(m.ctx, event)
408405
}
409406

410407
// Stop monitor.
@@ -483,32 +480,16 @@ func (i *InputProcess) RTSPprotocol() string {
483480
func (i *InputProcess) StreamInfo(ctx context.Context) (*hls.StreamInfo, error) {
484481
// It may take a few seconds for the stream to
485482
// become available after the monitor started.
486-
for {
487-
muxer, err := i.serverPath.HLSMuxer()
488-
if err != nil {
489-
return nil, fmt.Errorf("get muxer: %w", err)
490-
}
491-
// Returns nil if the stream isn't available yet.
492-
info, err := muxer.StreamInfo()
493-
if err != nil {
494-
return nil, err
495-
}
496-
if info == nil {
497-
select {
498-
case <-time.After(3 * time.Second):
499-
i.logf(log.LevelDebug, "could not get stream info")
500-
continue
501-
case <-ctx.Done():
502-
return nil, context.Canceled
503-
}
504-
}
505-
return info, nil
483+
muxer, err := i.serverPath.HLSMuxer(ctx)
484+
if err != nil {
485+
return nil, fmt.Errorf("get muxer: %w", err)
506486
}
487+
return muxer.StreamInfo(), nil
507488
}
508489

509490
// HLSMuxer returns the HLS muxer for this input.
510-
func (i *InputProcess) HLSMuxer() (video.IHLSMuxer, error) {
511-
return i.serverPath.HLSMuxer()
491+
func (i *InputProcess) HLSMuxer(ctx context.Context) (video.IHLSMuxer, error) {
492+
return i.serverPath.HLSMuxer(ctx)
512493
}
513494

514495
// ProcessName name of process "main" or "sub".

pkg/monitor/monitor_test.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func TestInputStreamInfo(t *testing.T) {
543543
t.Run("getMuxerErr", func(t *testing.T) {
544544
mockError := errors.New("mock")
545545
muxer := newMockMuxerFunc(&mockMuxer{
546-
streamInfoErr: mockError,
546+
getMuxerErr: mockError,
547547
})
548548
i := &InputProcess{
549549
serverPath: video.ServerPath{
@@ -554,30 +554,18 @@ func TestInputStreamInfo(t *testing.T) {
554554
require.ErrorIs(t, err, mockError)
555555
require.Nil(t, actual)
556556
})
557-
t.Run("canceled", func(t *testing.T) {
558-
muxer := newMockMuxerFunc(&mockMuxer{})
559-
i := &InputProcess{
560-
serverPath: video.ServerPath{
561-
HLSMuxer: muxer,
562-
},
563-
}
564-
565-
ctx, cancel := context.WithCancel(context.Background())
566-
cancel()
567-
568-
actual, err := i.StreamInfo(ctx)
569-
require.ErrorIs(t, err, context.Canceled)
570-
require.Nil(t, actual)
571-
})
572557
}
573558

574559
func TestSendEvent(t *testing.T) {
575560
t.Run("canceled", func(t *testing.T) {
576561
ctx, cancel := context.WithCancel(context.Background())
577562
cancel()
578-
m := &Monitor{ctx: ctx}
563+
m := &Monitor{ctx: ctx, recorder: &Recorder{}}
579564

580-
err := m.SendEvent(storage.Event{})
565+
err := m.SendEvent(storage.Event{
566+
Time: time.Unix(1, 0),
567+
RecDuration: 1,
568+
})
581569
require.ErrorIs(t, err, context.Canceled)
582570
})
583571
t.Run("missingTimeErr", func(t *testing.T) {

pkg/monitor/recorder.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ func (r *Recorder) runRecordingSession(ctx context.Context) {
153153
for {
154154
err := r.runSession(ctx, r)
155155
if err != nil {
156-
r.logf(log.LevelError, "recording crashed: %v", err)
156+
if !errors.Is(err, context.Canceled) {
157+
r.logf(log.LevelError, "recording crashed: %v", err)
158+
}
157159
select {
158160
case <-ctx.Done():
159161
// Session is canceled.
@@ -182,7 +184,7 @@ func runRecording(ctx context.Context, r *Recorder) error {
182184
return fmt.Errorf("parse timestamp offset %w", err)
183185
}
184186

185-
muxer, err := r.input.HLSMuxer()
187+
muxer, err := r.input.HLSMuxer(ctx)
186188
if err != nil {
187189
return fmt.Errorf("get muxer: %w", err)
188190
}
@@ -220,15 +222,11 @@ func runRecording(ctx context.Context, r *Recorder) error {
220222

221223
r.logf(log.LevelInfo, "starting recording: %v", basePath)
222224

223-
info, err := r.input.StreamInfo(ctx)
224-
if err != nil {
225-
return fmt.Errorf("stream info: %w", err)
226-
}
227-
228-
go r.generateThumbnail(filePath, firstSegment, *info)
225+
info := *muxer.StreamInfo()
226+
go r.generateThumbnail(filePath, firstSegment, info)
229227

230228
prevSeg, endTime, err := generateVideo(
231-
ctx, filePath, muxer.NextSegment, firstSegment, *info, videoLength)
229+
ctx, filePath, muxer.NextSegment, firstSegment, info, videoLength)
232230
if err != nil {
233231
return fmt.Errorf("write video: %w", err)
234232
}
@@ -399,10 +397,14 @@ func (r *Recorder) saveRecording(
399397
r.logf(log.LevelInfo, "recording saved: %v", filepath.Base(dataPath))
400398
}
401399

402-
func (r *Recorder) sendEvent(event storage.Event) error {
400+
func (r *Recorder) sendEvent(ctx context.Context, event storage.Event) error {
403401
if err := event.Validate(); err != nil {
404402
return fmt.Errorf("invalid event: %w", err)
405403
}
406-
r.eventChan <- event
407-
return nil
404+
select {
405+
case <-ctx.Done():
406+
return context.Canceled
407+
case r.eventChan <- event:
408+
return nil
409+
}
408410
}

pkg/monitor/recorder_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ func newTestRecorder(t *testing.T) *Recorder {
8686
}
8787

8888
type mockMuxer struct {
89-
streamInfo *hls.StreamInfo
90-
streamInfoErr error
91-
segCount int
89+
streamInfo *hls.StreamInfo
90+
getMuxerErr error
91+
segCount int
9292
}
9393

94-
func newMockMuxerFunc(muxer *mockMuxer) func() (video.IHLSMuxer, error) {
95-
return func() (video.IHLSMuxer, error) {
96-
return muxer, nil
94+
func newMockMuxerFunc(muxer *mockMuxer) func(context.Context) (video.IHLSMuxer, error) {
95+
return func(ctx context.Context) (video.IHLSMuxer, error) {
96+
return muxer, muxer.getMuxerErr
9797
}
9898
}
9999

100-
func (m *mockMuxer) StreamInfo() (*hls.StreamInfo, error) {
101-
return m.streamInfo, m.streamInfoErr
100+
func (m *mockMuxer) StreamInfo() *hls.StreamInfo {
101+
return m.streamInfo
102102
}
103103

104104
func (m *mockMuxer) NextSegment(prevID uint64) (*hls.Segment, error) {
@@ -132,7 +132,7 @@ func TestStartRecorder(t *testing.T) {
132132
r.runSession = mockRunRecording
133133
go r.start(ctx)
134134

135-
err := r.sendEvent(storage.Event{
135+
err := r.sendEvent(ctx, storage.Event{
136136
Time: time.Now().Add(time.Duration(-1) * time.Hour),
137137
RecDuration: 1,
138138
})

pkg/video/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ func (s *Server) Start(ctx context.Context) error {
7272
type CancelFunc func()
7373

7474
// HlsMuxerFunc .
75-
type HlsMuxerFunc func() (IHLSMuxer, error)
75+
type HlsMuxerFunc func(context.Context) (IHLSMuxer, error)
7676

7777
// IHLSMuxer HLS muxer interface.
7878
type IHLSMuxer interface {
79-
StreamInfo() (*hls.StreamInfo, error)
79+
StreamInfo() *hls.StreamInfo
8080
WaitForSegFinalized()
8181
NextSegment(prevID uint64) (*hls.Segment, error)
8282
}

0 commit comments

Comments
 (0)