Skip to content

Commit c6c857e

Browse files
committed
playback: fix error 404 when seeking before start of segment (#4276) (#4533)
Fixes #4276 Replaces #4533
1 parent 6c8bf4f commit c6c857e

File tree

4 files changed

+305
-33
lines changed

4 files changed

+305
-33
lines changed

internal/playback/muxer_fmp4.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4"
88
"github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4/seekablebuffer"
9+
"github.com/bluenviron/mediamtx/internal/recordstore"
910
)
1011

1112
const (
@@ -156,26 +157,20 @@ func (w *muxerFMP4) innerFlush(final bool) error {
156157
}
157158
}
158159

159-
if part.Tracks != nil {
160-
part.SequenceNumber = w.nextSequenceNumber
161-
w.nextSequenceNumber++
162-
160+
// no samples to write
161+
if part.Tracks == nil {
162+
// if no samples has been written before, return an error
163163
if w.init != nil {
164-
err := w.init.Marshal(&w.outBuf)
165-
if err != nil {
166-
return err
167-
}
168-
169-
_, err = w.w.Write(w.outBuf.Bytes())
170-
if err != nil {
171-
return err
172-
}
173-
174-
w.init = nil
175-
w.outBuf.Reset()
164+
return recordstore.ErrNoSegmentsFound
176165
}
166+
return nil
167+
}
177168

178-
err := part.Marshal(&w.outBuf)
169+
part.SequenceNumber = w.nextSequenceNumber
170+
w.nextSequenceNumber++
171+
172+
if w.init != nil {
173+
err := w.init.Marshal(&w.outBuf)
179174
if err != nil {
180175
return err
181176
}
@@ -185,9 +180,22 @@ func (w *muxerFMP4) innerFlush(final bool) error {
185180
return err
186181
}
187182

183+
w.init = nil
188184
w.outBuf.Reset()
189185
}
190186

187+
err := part.Marshal(&w.outBuf)
188+
if err != nil {
189+
return err
190+
}
191+
192+
_, err = w.w.Write(w.outBuf.Bytes())
193+
if err != nil {
194+
return err
195+
}
196+
197+
w.outBuf.Reset()
198+
191199
return nil
192200
}
193201

internal/playback/muxer_mp4.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4"
77
"github.com/bluenviron/mediacommon/v2/pkg/formats/pmp4"
8+
"github.com/bluenviron/mediamtx/internal/recordstore"
89
)
910

1011
type muxerMP4Track struct {
@@ -24,8 +25,9 @@ func findTrackMP4(tracks []*muxerMP4Track, id int) *muxerMP4Track {
2425
type muxerMP4 struct {
2526
w io.Writer
2627

27-
tracks []*muxerMP4Track
28-
curTrack *muxerMP4Track
28+
tracks []*muxerMP4Track
29+
curTrack *muxerMP4Track
30+
hasContent bool
2931
}
3032

3133
func (w *muxerMP4) writeInit(init *fmp4.Init) {
@@ -73,6 +75,8 @@ func (w *muxerMP4) writeSample(
7375
ptsOffset = 0
7476
}
7577

78+
w.hasContent = w.hasContent || dts >= 0
79+
7680
w.curTrack.Samples = append(w.curTrack.Samples, &pmp4.Sample{
7781
PTSOffset: ptsOffset,
7882
IsNonSyncSample: isNonSyncSample,
@@ -93,6 +97,10 @@ func (w *muxerMP4) writeFinalDTS(dts int64) {
9397
}
9498

9599
func (w *muxerMP4) flush() error {
100+
if !w.hasContent {
101+
return recordstore.ErrNoSegmentsFound
102+
}
103+
96104
h := pmp4.Presentation{
97105
Tracks: make([]*pmp4.Track, len(w.tracks)),
98106
}

0 commit comments

Comments
 (0)