playback: fix panic when MP4 muxer flushes with no samples (#5867)

Co-authored-by: Arturo2511 <arturomozzon@gmail.com>
This commit is contained in:
Arturo Mozzon
2026-06-13 11:22:53 +02:00
committed by GitHub
co-authored by Arturo2511
parent e13e9660d1
commit 2d08cfec7b
2 changed files with 29 additions and 1 deletions
+1 -1
View File
@@ -90,7 +90,7 @@ func (w *muxerMP4) writeFinalDTS(dts int64) {
}
func (w *muxerMP4) flush() error {
if len(w.curTrack.Samples) == 0 || w.curTrack.lastDTS < 0 {
if w.curTrack == nil || len(w.curTrack.Samples) == 0 || w.curTrack.lastDTS < 0 {
return recordstore.ErrNoSegmentsFound
}
+28
View File
@@ -6,6 +6,7 @@ import (
"github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4"
mcodecs "github.com/bluenviron/mediacommon/v2/pkg/formats/mp4/codecs"
"github.com/bluenviron/mediamtx/internal/recordstore"
"github.com/bluenviron/mediamtx/internal/test"
"github.com/stretchr/testify/require"
)
@@ -57,3 +58,30 @@ func TestMuxerMP4EmptyTracks(t *testing.T) {
require.Greater(t, buf.Len(), 0)
}
// Test that flushing without any written sample returns an error instead of panicking
func TestMuxerMP4NoSamples(t *testing.T) {
var buf bytes.Buffer
mux := &muxerMP4{
w: &buf,
}
init := &fmp4.Init{
Tracks: []*fmp4.InitTrack{
{
ID: 1,
TimeScale: 90000,
Codec: &mcodecs.H264{
SPS: test.FormatH264.SPS,
PPS: test.FormatH264.PPS,
},
},
},
}
mux.writeInit(init)
err := mux.flush()
require.ErrorIs(t, err, recordstore.ErrNoSegmentsFound)
}