playback: fix crash (#5240) (#5250)

when requesting a recording with the mp4 format, if there are two
tracks and the second track has no sample, the server crashed. This
fixes the issue.
This commit is contained in:
Alessandro Ros 2025-12-06 22:45:50 +01:00 committed by GitHub
parent f99eb8e52d
commit 0139105583
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 66 additions and 4 deletions

View file

@ -94,12 +94,15 @@ func (w *muxerMP4) flush() error {
return recordstore.ErrNoSegmentsFound return recordstore.ErrNoSegmentsFound
} }
h := pmp4.Presentation{ var tracks []*pmp4.Track
Tracks: make([]*pmp4.Track, len(w.tracks)), for _, track := range w.tracks {
if len(track.Samples) != 0 {
tracks = append(tracks, &track.Track)
}
} }
for i, track := range w.tracks { h := pmp4.Presentation{
h.Tracks[i] = &track.Track Tracks: tracks,
} }
return h.Marshal(w.w) return h.Marshal(w.w)

View file

@ -0,0 +1,59 @@
package playback
import (
"bytes"
"testing"
"github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4"
"github.com/bluenviron/mediacommon/v2/pkg/formats/mp4"
"github.com/bluenviron/mediamtx/internal/test"
"github.com/stretchr/testify/require"
)
// Test that tracks with no samples are not included in the output
func TestMuxerMP4EmptyTracks(t *testing.T) {
var buf bytes.Buffer
mux := &muxerMP4{
w: &buf,
}
init := &fmp4.Init{
Tracks: []*fmp4.InitTrack{
{
ID: 1,
TimeScale: 90000,
Codec: &mp4.CodecH264{
SPS: test.FormatH264.SPS,
PPS: test.FormatH264.PPS,
},
},
{
ID: 2,
TimeScale: 48000,
Codec: &mp4.CodecOpus{},
},
},
}
mux.writeInit(init)
// Only write samples to the first track
mux.setTrack(1)
err := mux.writeSample(0, 0, false, 5, func() ([]byte, error) {
return []byte{0x01, 0x02, 0x03, 0x04, 0x05}, nil
})
require.NoError(t, err)
err = mux.writeSample(90000, 0, true, 5, func() ([]byte, error) {
return []byte{0x06, 0x07, 0x08, 0x09, 0x0a}, nil
})
require.NoError(t, err)
mux.writeFinalDTS(180000)
err = mux.flush()
require.NoError(t, err)
require.Greater(t, buf.Len(), 0)
}