From 7f0199500ddc3d4a55386ee4bd52801701c32fdc Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 16 Jul 2023 23:27:16 +0200 Subject: [PATCH] Readd non-breaking version of #119 --- packet.go | 23 ++++++++++------------- packet_test.go | 45 --------------------------------------------- packetizer_test.go | 3 --- 3 files changed, 10 insertions(+), 61 deletions(-) diff --git a/packet.go b/packet.go index a47e5fc..500c60f 100644 --- a/packet.go +++ b/packet.go @@ -16,13 +16,11 @@ type Extension struct { } // Header represents an RTP packet header -// NOTE: PayloadOffset is populated by Marshal/Unmarshal and should not be modified type Header struct { Version uint8 Padding bool Extension bool Marker bool - PayloadOffset int PayloadType uint8 SequenceNumber uint16 Timestamp uint32 @@ -30,15 +28,19 @@ type Header struct { CSRC []uint32 ExtensionProfile uint16 Extensions []Extension + + // Deprecated: will be removed in a future version. + PayloadOffset int } // Packet represents an RTP Packet -// NOTE: Raw is populated by Marshal/Unmarshal and should not be modified type Packet struct { Header - Raw []byte Payload []byte PaddingSize byte + + // Deprecated: will be removed in a future version. + Raw []byte } const ( @@ -210,14 +212,12 @@ func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit } } - h.PayloadOffset = n - return n, nil } // Unmarshal parses the passed byte slice and stores the result in the Packet. func (p *Packet) Unmarshal(buf []byte) error { - _, err := p.Header.Unmarshal(buf) + n, err := p.Header.Unmarshal(buf) if err != nil { return err } @@ -227,12 +227,11 @@ func (p *Packet) Unmarshal(buf []byte) error { p.PaddingSize = buf[end-1] end -= int(p.PaddingSize) } - if end < p.PayloadOffset { + if end < n { return errTooSmall } - p.Payload = buf[p.PayloadOffset:end] - p.Raw = buf + p.Payload = buf[n:end] return nil } @@ -343,8 +342,6 @@ func (h Header) MarshalTo(buf []byte) (n int, err error) { } } - h.PayloadOffset = n - return n, nil } @@ -499,7 +496,7 @@ func (p *Packet) MarshalTo(buf []byte) (n int, err error) { } m := copy(buf[n:], p.Payload) - p.Raw = buf[:n+m] + if p.Header.Padding { buf[n+m+int(p.PaddingSize-1)] = p.PaddingSize } diff --git a/packet_test.go b/packet_test.go index be13efb..bc8f97a 100644 --- a/packet_test.go +++ b/packet_test.go @@ -35,7 +35,6 @@ func TestBasic(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 20, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -43,7 +42,6 @@ func TestBasic(t *testing.T) { CSRC: []uint32{}, }, Payload: rawPkt[20:], - Raw: rawPkt, PaddingSize: 0, } @@ -62,20 +60,12 @@ func TestBasic(t *testing.T) { t.Errorf("wrong computed marshal size") } - if p.PayloadOffset != 20 { - t.Errorf("wrong payload offset: %d != %d", p.PayloadOffset, 20) - } - raw, err := p.Marshal() if err != nil { t.Error(err) } else if !reflect.DeepEqual(raw, rawPkt) { t.Errorf("TestBasic marshal: got %#v, want %#v", raw, rawPkt) } - - if p.PayloadOffset != 20 { - t.Errorf("wrong payload offset: %d != %d", p.PayloadOffset, 20) - } }) } @@ -101,10 +91,8 @@ func TestBasic(t *testing.T) { Timestamp: 3653407706, SSRC: 476325762, CSRC: []uint32{}, - PayloadOffset: 20, }, Payload: rawPkt[20:21], - Raw: rawPkt, PaddingSize: 4, } if err := p.Unmarshal(rawPkt); err != nil { @@ -135,10 +123,8 @@ func TestBasic(t *testing.T) { Timestamp: 3653407706, SSRC: 476325762, CSRC: []uint32{}, - PayloadOffset: 20, }, Payload: []byte{}, - Raw: rawPkt, PaddingSize: 5, } if err := p.Unmarshal(rawPkt); err != nil { @@ -345,7 +331,6 @@ func TestRFC8285OneByteExtension(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 18, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -353,7 +338,6 @@ func TestRFC8285OneByteExtension(t *testing.T) { CSRC: []uint32{}, }, Payload: rawPkt[20:], - Raw: rawPkt, } dstData, _ := p.Marshal() @@ -409,7 +393,6 @@ func TestRFC8285OneByteTwoExtensionOfTwoBytes(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -417,7 +400,6 @@ func TestRFC8285OneByteTwoExtensionOfTwoBytes(t *testing.T) { CSRC: []uint32{}, }, Payload: rawPkt[20:], - Raw: rawPkt, } dstData, _ := p.Marshal() @@ -534,7 +516,6 @@ func TestRFC8285OneByteMultipleExtensions(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -542,7 +523,6 @@ func TestRFC8285OneByteMultipleExtensions(t *testing.T) { CSRC: []uint32{}, }, Payload: rawPkt[28:], - Raw: rawPkt, } dstData, _ := p.Marshal() @@ -578,7 +558,6 @@ func TestRFC8285TwoByteExtension(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 42, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -586,7 +565,6 @@ func TestRFC8285TwoByteExtension(t *testing.T) { CSRC: []uint32{}, }, Payload: rawPkt[44:], - Raw: rawPkt, } dstData, _ := p.Marshal() @@ -681,7 +659,6 @@ func TestRFC8285TwoByteMultipleExtensionsWithLargeExtension(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 40, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -689,7 +666,6 @@ func TestRFC8285TwoByteMultipleExtensionsWithLargeExtension(t *testing.T) { CSRC: []uint32{}, }, Payload: rawPkt[40:], - Raw: rawPkt, } dstData, _ := p.Marshal() @@ -708,7 +684,6 @@ func TestRFC8285GetExtensionReturnsNilWhenExtensionsDisabled(t *testing.T) { Marker: true, Extension: false, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -740,7 +715,6 @@ func TestRFC8285DelExtension(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -790,7 +764,6 @@ func TestRFC8285GetExtensionIDs(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -826,7 +799,6 @@ func TestRFC8285GetExtensionIDsReturnsErrorWhenExtensionsDisabled(t *testing.T) Marker: true, Extension: false, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -852,7 +824,6 @@ func TestRFC8285DelExtensionReturnsErrorWhenExtensionsDisabled(t *testing.T) { Marker: true, Extension: false, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -878,7 +849,6 @@ func TestRFC8285OneByteSetExtensionShouldEnableExensionsWhenAdding(t *testing.T) Marker: true, Extension: false, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -921,7 +891,6 @@ func TestRFC8285OneByteSetExtensionShouldSetCorrectExtensionProfileFor16ByteExte Marker: true, Extension: false, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -963,7 +932,6 @@ func TestRFC8285OneByteSetExtensionShouldUpdateExistingExension(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -1004,7 +972,6 @@ func TestRFC8285OneByteSetExtensionShouldErrorWhenInvalidIDProvided(t *testing.T }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -1062,7 +1029,6 @@ func TestRFC8285OneByteSetExtensionShouldErrorWhenPayloadTooLarge(t *testing.T) }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -1090,7 +1056,6 @@ func TestRFC8285TwoByteSetExtensionShouldEnableExensionsWhenAdding(t *testing.T) Marker: true, Extension: false, Version: 2, - PayloadOffset: 31, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -1142,7 +1107,6 @@ func TestRFC8285TwoByteSetExtensionShouldUpdateExistingExension(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -1186,7 +1150,6 @@ func TestRFC8285TwoByteSetExtensionShouldErrorWhenPayloadTooLarge(t *testing.T) }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -1244,7 +1207,6 @@ func TestRFC3550SetExtensionShouldErrorWhenNonZero(t *testing.T) { }}, }, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -1276,7 +1238,6 @@ func TestRFC3550SetExtensionShouldRaiseErrorWhenSettingNonzeroID(t *testing.T) { Extension: true, ExtensionProfile: 0x1111, Version: 2, - PayloadOffset: 26, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706, @@ -1364,9 +1325,6 @@ func TestRoundtrip(t *testing.T) { if err := p.Unmarshal(rawPkt); err != nil { t.Fatal(err) } - if !bytes.Equal(rawPkt, p.Raw) { - t.Errorf("p.Raw must be same as rawPkt.\n p.Raw: %+v,\nrawPkt: %+v", p.Raw, rawPkt) - } if !bytes.Equal(payload, p.Payload) { t.Errorf("p.Payload must be same as payload.\n payload: %+v,\np.Payload: %+v", payload, p.Payload, @@ -1380,9 +1338,6 @@ func TestRoundtrip(t *testing.T) { if !bytes.Equal(rawPkt, buf) { t.Errorf("buf must be same as rawPkt.\n buf: %+v,\nrawPkt: %+v", buf, rawPkt) } - if !bytes.Equal(rawPkt, p.Raw) { - t.Errorf("p.Raw must be same as rawPkt.\n p.Raw: %+v,\nrawPkt: %+v", p.Raw, rawPkt) - } if !bytes.Equal(payload, p.Payload) { t.Errorf("p.Payload must be same as payload.\n payload: %+v,\np.Payload: %+v", payload, p.Payload, diff --git a/packetizer_test.go b/packetizer_test.go index a4ac3b8..78f8220 100644 --- a/packetizer_test.go +++ b/packetizer_test.go @@ -51,7 +51,6 @@ func TestPacketizer_AbsSendTime(t *testing.T) { Padding: false, Extension: true, Marker: true, - PayloadOffset: 0, // not set by Packetize() at now PayloadType: 98, SequenceNumber: 1234, Timestamp: 45678, @@ -144,9 +143,7 @@ func TestPacketizer_Roundtrip(t *testing.T) { t.Errorf("Packet versions don't match, expected %v but got %v", expectedPkt.Payload, pkt.Payload) } - pkt.PayloadOffset = 0 pkt.PaddingSize = 0 - pkt.Raw = nil if !reflect.DeepEqual(expectedPkt, pkt) { t.Errorf("Packets don't match, expected %v but got %v", expectedPkt, pkt)