Skip to content

Commit

Permalink
Readd non-breaking version of #119
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jul 16, 2023
1 parent a2b2381 commit 7f01995
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 61 deletions.
23 changes: 10 additions & 13 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,31 @@ 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
SSRC uint32
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 (
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -343,8 +342,6 @@ func (h Header) MarshalTo(buf []byte) (n int, err error) {
}
}

h.PayloadOffset = n

return n, nil
}

Expand Down Expand Up @@ -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
}
Expand Down
45 changes: 0 additions & 45 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ func TestBasic(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 20,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[20:],
Raw: rawPkt,
PaddingSize: 0,
}

Expand All @@ -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)
}
})
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -345,15 +331,13 @@ func TestRFC8285OneByteExtension(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 18,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[20:],
Raw: rawPkt,
}

dstData, _ := p.Marshal()
Expand Down Expand Up @@ -409,15 +393,13 @@ func TestRFC8285OneByteTwoExtensionOfTwoBytes(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[20:],
Raw: rawPkt,
}

dstData, _ := p.Marshal()
Expand Down Expand Up @@ -534,15 +516,13 @@ func TestRFC8285OneByteMultipleExtensions(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[28:],
Raw: rawPkt,
}

dstData, _ := p.Marshal()
Expand Down Expand Up @@ -578,15 +558,13 @@ func TestRFC8285TwoByteExtension(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 42,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[44:],
Raw: rawPkt,
}

dstData, _ := p.Marshal()
Expand Down Expand Up @@ -681,15 +659,13 @@ func TestRFC8285TwoByteMultipleExtensionsWithLargeExtension(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 40,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[40:],
Raw: rawPkt,
}

dstData, _ := p.Marshal()
Expand All @@ -708,7 +684,6 @@ func TestRFC8285GetExtensionReturnsNilWhenExtensionsDisabled(t *testing.T) {
Marker: true,
Extension: false,
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -740,7 +715,6 @@ func TestRFC8285DelExtension(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -790,7 +764,6 @@ func TestRFC8285GetExtensionIDs(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -826,7 +799,6 @@ func TestRFC8285GetExtensionIDsReturnsErrorWhenExtensionsDisabled(t *testing.T)
Marker: true,
Extension: false,
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand All @@ -852,7 +824,6 @@ func TestRFC8285DelExtensionReturnsErrorWhenExtensionsDisabled(t *testing.T) {
Marker: true,
Extension: false,
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand All @@ -878,7 +849,6 @@ func TestRFC8285OneByteSetExtensionShouldEnableExensionsWhenAdding(t *testing.T)
Marker: true,
Extension: false,
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -921,7 +891,6 @@ func TestRFC8285OneByteSetExtensionShouldSetCorrectExtensionProfileFor16ByteExte
Marker: true,
Extension: false,
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -963,7 +932,6 @@ func TestRFC8285OneByteSetExtensionShouldUpdateExistingExension(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -1004,7 +972,6 @@ func TestRFC8285OneByteSetExtensionShouldErrorWhenInvalidIDProvided(t *testing.T
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -1062,7 +1029,6 @@ func TestRFC8285OneByteSetExtensionShouldErrorWhenPayloadTooLarge(t *testing.T)
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -1090,7 +1056,6 @@ func TestRFC8285TwoByteSetExtensionShouldEnableExensionsWhenAdding(t *testing.T)
Marker: true,
Extension: false,
Version: 2,
PayloadOffset: 31,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -1142,7 +1107,6 @@ func TestRFC8285TwoByteSetExtensionShouldUpdateExistingExension(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -1186,7 +1150,6 @@ func TestRFC8285TwoByteSetExtensionShouldErrorWhenPayloadTooLarge(t *testing.T)
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -1244,7 +1207,6 @@ func TestRFC3550SetExtensionShouldErrorWhenNonZero(t *testing.T) {
}},
},
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -1276,7 +1238,6 @@ func TestRFC3550SetExtensionShouldRaiseErrorWhenSettingNonzeroID(t *testing.T) {
Extension: true,
ExtensionProfile: 0x1111,
Version: 2,
PayloadOffset: 26,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions packetizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7f01995

Please sign in to comment.