From ffbdf516690bc8c5d4572aae990d645adcccd3b3 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Wed, 14 Dec 2022 17:50:03 +0100 Subject: [PATCH] fix handling of oversized H264 RTP packets Resized RTP packets were wrongly mixed with original packets. Original packets are now discarded correctly. --- internal/core/formatprocessor_h264.go | 5 ++++- internal/core/rtsp_source_test.go | 26 ++++++-------------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/internal/core/formatprocessor_h264.go b/internal/core/formatprocessor_h264.go index 1658f000..81104c8e 100644 --- a/internal/core/formatprocessor_h264.go +++ b/internal/core/formatprocessor_h264.go @@ -232,7 +232,10 @@ func (t *formatProcessorH264) process(dat data, hasNonRTSPReaders bool) error { t.decoder = t.format.CreateDecoder() } - nalus, pts, err := t.decoder.Decode(pkt) + tdata.rtpPackets = nil + + // DecodeUntilMarker() is necessary, otherwise Encode() generates partial groups + nalus, pts, err := t.decoder.DecodeUntilMarker(pkt) if err != nil { if err == rtph264.ErrNonStartingPacketAndNoPrevious || err == rtph264.ErrMorePacketsNeeded { return nil diff --git a/internal/core/rtsp_source_test.go b/internal/core/rtsp_source_test.go index 9bc697f8..f3a5dfa1 100644 --- a/internal/core/rtsp_source_test.go +++ b/internal/core/rtsp_source_test.go @@ -626,7 +626,7 @@ func TestRTSPSourceOversizedPackets(t *testing.T) { SSRC: 563423, Padding: true, }, - Payload: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 2000/4), + Payload: append([]byte{0x1c, 0b10000000}, bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 2000/4)...), }.Marshal() err = conn.WriteInterleavedFrame(&base.InterleavedFrame{ Channel: 0, @@ -644,7 +644,7 @@ func TestRTSPSourceOversizedPackets(t *testing.T) { SSRC: 563423, Padding: true, }, - Payload: []byte{0x01, 0x02, 0x03, 0x04}, + Payload: []byte{0x1c, 0b01000000, 0x01, 0x02, 0x03, 0x04}, }.Marshal() err = conn.WriteInterleavedFrame(&base.InterleavedFrame{ Channel: 0, @@ -719,8 +719,8 @@ func TestRTSPSourceOversizedPackets(t *testing.T) { CSRC: []uint32{}, }, Payload: append( - append([]byte{0x1c, 0x81, 0x02, 0x03, 0x04}, bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 363)...), - []byte{0x01, 0x02, 0x03}..., + append([]byte{0x1c, 0x80}, bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 364)...), + []byte{0x01, 0x02}..., ), }, pkt) @@ -736,24 +736,10 @@ func TestRTSPSourceOversizedPackets(t *testing.T) { CSRC: []uint32{}, }, Payload: append( - []byte{0x1c, 0x41, 0x04}, - bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 135)..., + []byte{0x1c, 0x40, 0x03, 0x04}, + bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 136)..., ), }, pkt) - - case 3: - require.Equal(t, &rtp.Packet{ - Header: rtp.Header{ - Version: 2, - Marker: true, - PayloadType: 96, - SequenceNumber: 126, - Timestamp: 45343, - SSRC: 563423, - CSRC: []uint32{}, - }, - Payload: []byte{0x01, 0x02, 0x03, 0x04}, - }, pkt) close(packetRecv) } i++