From a50642c669d1aad2e8e7c9e456d34db604c7d431 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Tue, 9 Apr 2024 09:36:38 +0200 Subject: [PATCH] improve record tests (#3216) --- internal/playback/segment_fmp4_test.go | 2 +- internal/protocols/rtmp/reader_test.go | 110 ++++++++------------- internal/record/agent_test.go | 131 +++++++++++++++++-------- internal/test/formats.go | 23 +++++ 4 files changed, 155 insertions(+), 111 deletions(-) diff --git a/internal/playback/segment_fmp4_test.go b/internal/playback/segment_fmp4_test.go index 7a1edfd5..e93d5f67 100644 --- a/internal/playback/segment_fmp4_test.go +++ b/internal/playback/segment_fmp4_test.go @@ -41,7 +41,7 @@ func writeBenchInit(f io.WriteSeeker) { } _, err = f.Write([]byte{ - 'm', 'o', 'o', 'f', 0x00, 0x00, 0x00, 0x10, + 0x00, 0x00, 0x00, 0x10, 'm', 'o', 'o', 'f', }) if err != nil { panic(err) diff --git a/internal/protocols/rtmp/reader_test.go b/internal/protocols/rtmp/reader_test.go index 8f0b28ee..0853ea3a 100644 --- a/internal/protocols/rtmp/reader_test.go +++ b/internal/protocols/rtmp/reader_test.go @@ -16,42 +16,12 @@ import ( "github.com/bluenviron/mediamtx/internal/protocols/rtmp/bytecounter" "github.com/bluenviron/mediamtx/internal/protocols/rtmp/h264conf" "github.com/bluenviron/mediamtx/internal/protocols/rtmp/message" + "github.com/bluenviron/mediamtx/internal/test" ) func TestReadTracks(t *testing.T) { - h264SPS := []byte{ - 0x67, 0x64, 0x00, 0x0c, 0xac, 0x3b, 0x50, 0xb0, - 0x4b, 0x42, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00, - 0x00, 0x03, 0x00, 0x3d, 0x08, - } - - h264PPS := []byte{ - 0x68, 0xee, 0x3c, 0x80, - } - - h265VPS := []byte{ - 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x40, - 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, - 0x03, 0x00, 0x00, 0x03, 0x00, 0x7b, 0xac, 0x09, - } - - h265SPS := []byte{ - 0x42, 0x01, 0x01, 0x01, 0x40, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, - 0x03, 0x00, 0x7b, 0xa0, 0x03, 0xc0, 0x80, 0x11, - 0x07, 0xcb, 0x96, 0xb4, 0xa4, 0x25, 0x92, 0xe3, - 0x01, 0x6a, 0x02, 0x02, 0x02, 0x08, 0x00, 0x00, - 0x03, 0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0xe3, - 0x00, 0x2e, 0xf2, 0x88, 0x00, 0x09, 0x89, 0x60, - 0x00, 0x04, 0xc4, 0xb4, 0x20, - } - - h265PPS := []byte{ - 0x44, 0x01, 0xc0, 0xf7, 0xc0, 0xcc, 0x90, - } - var spsp h265.SPS - err := spsp.Unmarshal(h265SPS) + err := spsp.Unmarshal(test.FormatH265.SPS) require.NoError(t, err) hvcc := &mp4.HvcC{ @@ -59,8 +29,8 @@ func TestReadTracks(t *testing.T) { GeneralProfileIdc: spsp.ProfileTierLevel.GeneralProfileIdc, GeneralProfileCompatibility: spsp.ProfileTierLevel.GeneralProfileCompatibilityFlag, GeneralConstraintIndicator: [6]uint8{ - h265SPS[7], h265SPS[8], h265SPS[9], - h265SPS[10], h265SPS[11], h265SPS[12], + test.FormatH265.SPS[7], test.FormatH265.SPS[8], test.FormatH265.SPS[9], + test.FormatH265.SPS[10], test.FormatH265.SPS[11], test.FormatH265.SPS[12], }, GeneralLevelIdc: spsp.ProfileTierLevel.GeneralLevelIdc, // MinSpatialSegmentationIdc @@ -79,24 +49,24 @@ func TestReadTracks(t *testing.T) { NaluType: byte(h265.NALUType_VPS_NUT), NumNalus: 1, Nalus: []mp4.HEVCNalu{{ - Length: uint16(len(h265VPS)), - NALUnit: h265VPS, + Length: uint16(len(test.FormatH265.VPS)), + NALUnit: test.FormatH265.VPS, }}, }, { NaluType: byte(h265.NALUType_SPS_NUT), NumNalus: 1, Nalus: []mp4.HEVCNalu{{ - Length: uint16(len(h265SPS)), - NALUnit: h265SPS, + Length: uint16(len(test.FormatH265.SPS)), + NALUnit: test.FormatH265.SPS, }}, }, { NaluType: byte(h265.NALUType_PPS_NUT), NumNalus: 1, Nalus: []mp4.HEVCNalu{{ - Length: uint16(len(h265PPS)), - NALUnit: h265PPS, + Length: uint16(len(test.FormatH265.PPS)), + NALUnit: test.FormatH265.PPS, }}, }, }, @@ -112,8 +82,8 @@ func TestReadTracks(t *testing.T) { "h264 + aac", &format.H264{ PayloadTyp: 96, - SPS: h264SPS, - PPS: h264PPS, + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, PacketizationMode: 1, }, &format.MPEG4Audio{ @@ -162,8 +132,8 @@ func TestReadTracks(t *testing.T) { Type: message.VideoTypeConfig, Payload: func() []byte { buf, _ := h264conf.Conf{ - SPS: h264SPS, - PPS: h264PPS, + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, }.Marshal() return buf }(), @@ -192,8 +162,8 @@ func TestReadTracks(t *testing.T) { "h264", &format.H264{ PayloadTyp: 96, - SPS: h264SPS, - PPS: h264PPS, + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, PacketizationMode: 1, }, nil, @@ -232,8 +202,8 @@ func TestReadTracks(t *testing.T) { Type: message.VideoTypeConfig, Payload: func() []byte { buf, _ := h264conf.Conf{ - SPS: h264SPS, - PPS: h264PPS, + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, }.Marshal() return buf }(), @@ -244,8 +214,8 @@ func TestReadTracks(t *testing.T) { "h264 + aac, issue mediamtx/386 (missing metadata)", &format.H264{ PayloadTyp: 96, - SPS: h264SPS, - PPS: h264PPS, + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, PacketizationMode: 1, }, &format.MPEG4Audio{ @@ -268,8 +238,8 @@ func TestReadTracks(t *testing.T) { Type: message.VideoTypeConfig, Payload: func() []byte { buf, _ := h264conf.Conf{ - SPS: h264SPS, - PPS: h264PPS, + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, }.Marshal() return buf }(), @@ -282,8 +252,8 @@ func TestReadTracks(t *testing.T) { Type: message.VideoTypeConfig, Payload: func() []byte { buf, _ := h264conf.Conf{ - SPS: h264SPS, - PPS: h264PPS, + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, }.Marshal() return buf }(), @@ -366,9 +336,9 @@ func TestReadTracks(t *testing.T) { "h265 + aac, obs studio pre 29.1 h265", &format.H265{ PayloadTyp: 96, - VPS: h265VPS, - SPS: h265SPS, - PPS: h265PPS, + VPS: test.FormatH265.VPS, + SPS: test.FormatH265.SPS, + PPS: test.FormatH265.PPS, }, &format.MPEG4Audio{ PayloadTyp: 96, @@ -416,9 +386,9 @@ func TestReadTracks(t *testing.T) { Type: message.VideoTypeAU, Payload: func() []byte { avcc, err := h264.AVCCMarshal([][]byte{ - h265VPS, - h265SPS, - h265PPS, + test.FormatH265.VPS, + test.FormatH265.SPS, + test.FormatH265.PPS, }) require.NoError(t, err) return avcc @@ -448,9 +418,9 @@ func TestReadTracks(t *testing.T) { "h265, issue mediamtx/2232 (xsplit broadcaster)", &format.H265{ PayloadTyp: 96, - VPS: h265VPS, - SPS: h265SPS, - PPS: h265PPS, + VPS: test.FormatH265.VPS, + SPS: test.FormatH265.SPS, + PPS: test.FormatH265.PPS, }, nil, []message.Message{ @@ -497,9 +467,9 @@ func TestReadTracks(t *testing.T) { "h265, obs 30.0", &format.H265{ PayloadTyp: 96, - VPS: h265VPS, - SPS: h265SPS, - PPS: h265PPS, + VPS: test.FormatH265.VPS, + SPS: test.FormatH265.SPS, + PPS: test.FormatH265.PPS, }, nil, []message.Message{ @@ -683,8 +653,8 @@ func TestReadTracks(t *testing.T) { "h264, issue mediamtx/2352", &format.H264{ PayloadTyp: 96, - SPS: h264SPS, - PPS: h264PPS, + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, PacketizationMode: 1, }, nil, @@ -735,8 +705,8 @@ func TestReadTracks(t *testing.T) { Type: message.VideoTypeConfig, Payload: func() []byte { buf, _ := h264conf.Conf{ - SPS: h264SPS, - PPS: h264PPS, + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, }.Marshal() return buf }(), diff --git a/internal/record/agent_test.go b/internal/record/agent_test.go index 9c394b6d..28857eeb 100644 --- a/internal/record/agent_test.go +++ b/internal/record/agent_test.go @@ -23,15 +23,15 @@ func TestAgent(t *testing.T) { desc := &description.Session{Medias: []*description.Media{ { Type: description.MediaTypeVideo, - Formats: []rtspformat.Format{&rtspformat.H265{ - PayloadTyp: 96, + Formats: []rtspformat.Format{&rtspformat.H264{ + PayloadTyp: 96, + PacketizationMode: 1, }}, }, { Type: description.MediaTypeVideo, - Formats: []rtspformat.Format{&rtspformat.H264{ - PayloadTyp: 96, - PacketizationMode: 1, + Formats: []rtspformat.Format{&rtspformat.H265{ + PayloadTyp: 96, }}, }, { @@ -59,53 +59,38 @@ func TestAgent(t *testing.T) { }, { Type: description.MediaTypeAudio, - Formats: []rtspformat.Format{&rtspformat.G711{ - PayloadTyp: 0, - MULaw: true, - SampleRate: 8000, - ChannelCount: 1, + Formats: []rtspformat.Format{&rtspformat.LPCM{ + PayloadTyp: 96, + BitDepth: 16, + SampleRate: 44100, + ChannelCount: 2, }}, }, }} writeToStream := func(stream *stream.Stream, ntp time.Time) { for i := 0; i < 3; i++ { - stream.WriteUnit(desc.Medias[0], desc.Medias[0].Formats[0], &unit.H265{ + stream.WriteUnit(desc.Medias[0], desc.Medias[0].Formats[0], &unit.H264{ Base: unit.Base{ PTS: (50 + time.Duration(i)) * time.Second, NTP: ntp.Add(time.Duration(i) * 60 * time.Second), }, AU: [][]byte{ - { // VPS - 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x02, 0x20, - 0x00, 0x00, 0x03, 0x00, 0xb0, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x03, 0x00, 0x7b, 0x18, 0xb0, 0x24, - }, - { // SPS - 0x42, 0x01, 0x01, 0x02, 0x20, 0x00, 0x00, 0x03, - 0x00, 0xb0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, - 0x00, 0x7b, 0xa0, 0x07, 0x82, 0x00, 0x88, 0x7d, - 0xb6, 0x71, 0x8b, 0x92, 0x44, 0x80, 0x53, 0x88, - 0x88, 0x92, 0xcf, 0x24, 0xa6, 0x92, 0x72, 0xc9, - 0x12, 0x49, 0x22, 0xdc, 0x91, 0xaa, 0x48, 0xfc, - 0xa2, 0x23, 0xff, 0x00, 0x01, 0x00, 0x01, 0x6a, - 0x02, 0x02, 0x02, 0x01, - }, - { // PPS - 0x44, 0x01, 0xc0, 0x25, 0x2f, 0x05, 0x32, 0x40, - }, - {byte(h265.NALUType_CRA_NUT) << 1, 0}, // IDR + test.FormatH264.SPS, + test.FormatH264.PPS, + {5}, // IDR }, }) - stream.WriteUnit(desc.Medias[1], desc.Medias[1].Formats[0], &unit.H264{ + stream.WriteUnit(desc.Medias[1], desc.Medias[1].Formats[0], &unit.H265{ Base: unit.Base{ PTS: (50 + time.Duration(i)) * time.Second, }, AU: [][]byte{ - test.FormatH264.SPS, - test.FormatH264.PPS, - {5}, // IDR + test.FormatH265.VPS, + test.FormatH265.SPS, + test.FormatH265.PPS, + {byte(h265.NALUType_CRA_NUT) << 1, 0}, // IDR }, }) @@ -123,7 +108,7 @@ func TestAgent(t *testing.T) { Samples: []byte{1, 2, 3, 4}, }) - stream.WriteUnit(desc.Medias[4], desc.Medias[4].Formats[0], &unit.G711{ + stream.WriteUnit(desc.Medias[4], desc.Medias[4].Formats[0], &unit.LPCM{ Base: unit.Base{ PTS: (50 + time.Duration(i)) * time.Second, }, @@ -181,7 +166,7 @@ func TestAgent(t *testing.T) { writeToStream(stream, time.Date(2008, 0o5, 20, 22, 15, 25, 0, time.UTC)) // simulate a write error - stream.WriteUnit(desc.Medias[1], desc.Medias[1].Formats[0], &unit.H264{ + stream.WriteUnit(desc.Medias[0], desc.Medias[0].Formats[0], &unit.H264{ Base: unit.Base{ PTS: 0, }, @@ -202,11 +187,77 @@ func TestAgent(t *testing.T) { ext = "ts" } - _, err = os.Stat(filepath.Join(dir, "mypath", "2008-05-20_22-15-25-000000."+ext)) - require.NoError(t, err) + if ca == "fmp4" { + func() { + f, err := os.Open(filepath.Join(dir, "mypath", "2008-05-20_22-15-25-000000."+ext)) + require.NoError(t, err) + defer f.Close() - _, err = os.Stat(filepath.Join(dir, "mypath", "2008-05-20_22-16-25-000000."+ext)) - require.NoError(t, err) + var init fmp4.Init + err = init.Unmarshal(f) + require.NoError(t, err) + + require.Equal(t, fmp4.Init{ + Tracks: []*fmp4.InitTrack{ + { + ID: 1, + TimeScale: 90000, + Codec: &fmp4.CodecH264{ + SPS: test.FormatH264.SPS, + PPS: test.FormatH264.PPS, + }, + }, + { + ID: 2, + TimeScale: 90000, + Codec: &fmp4.CodecH265{ + VPS: test.FormatH265.VPS, + SPS: test.FormatH265.SPS, + PPS: test.FormatH265.PPS, + }, + }, + { + ID: 3, + TimeScale: 44100, + Codec: &fmp4.CodecMPEG4Audio{ + Config: mpeg4audio.Config{ + Type: 2, + SampleRate: 44100, + ChannelCount: 2, + }, + }, + }, + { + ID: 4, + TimeScale: 8000, + Codec: &fmp4.CodecLPCM{ + BitDepth: 16, + SampleRate: 8000, + ChannelCount: 1, + }, + }, + { + ID: 5, + TimeScale: 44100, + Codec: &fmp4.CodecLPCM{ + BitDepth: 16, + SampleRate: 44100, + ChannelCount: 2, + }, + }, + }, + }, init) + }() + + _, err = os.Stat(filepath.Join(dir, "mypath", "2008-05-20_22-16-25-000000."+ext)) + require.NoError(t, err) + } else { + _, err := os.Stat(filepath.Join(dir, "mypath", "2008-05-20_22-15-25-000000."+ext)) + require.NoError(t, err) + + _, err = os.Stat(filepath.Join(dir, "mypath", "2008-05-20_22-16-25-000000."+ext)) + require.NoError(t, err) + } time.Sleep(50 * time.Millisecond) diff --git a/internal/test/formats.go b/internal/test/formats.go index e863137f..83ff40ed 100644 --- a/internal/test/formats.go +++ b/internal/test/formats.go @@ -17,6 +17,29 @@ var FormatH264 = &format.H264{ PacketizationMode: 1, } +// FormatH265 is a test H265 format. +var FormatH265 = &format.H265{ + PayloadTyp: 96, + VPS: []byte{ + 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x02, 0x20, + 0x00, 0x00, 0x03, 0x00, 0xb0, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x03, 0x00, 0x7b, 0x18, 0xb0, 0x24, + }, + SPS: []byte{ + 0x42, 0x01, 0x01, 0x02, 0x20, 0x00, 0x00, 0x03, + 0x00, 0xb0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, + 0x00, 0x7b, 0xa0, 0x07, 0x82, 0x00, 0x88, 0x7d, + 0xb6, 0x71, 0x8b, 0x92, 0x44, 0x80, 0x53, 0x88, + 0x88, 0x92, 0xcf, 0x24, 0xa6, 0x92, 0x72, 0xc9, + 0x12, 0x49, 0x22, 0xdc, 0x91, 0xaa, 0x48, 0xfc, + 0xa2, 0x23, 0xff, 0x00, 0x01, 0x00, 0x01, 0x6a, + 0x02, 0x02, 0x02, 0x01, + }, + PPS: []byte{ + 0x44, 0x01, 0xc0, 0x25, 0x2f, 0x05, 0x32, 0x40, + }, +} + // FormatMPEG4Audio is a test MPEG-4 audio format. var FormatMPEG4Audio = &format.MPEG4Audio{ PayloadTyp: 96,