Skip to content

Commit 20044b6

Browse files
committed
Fix TotalLost type
1 parent ad1dcea commit 20044b6

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

AUTHORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ Mathis Engelbart <mathis.engelbart@gmail.com>
1919
Max Hawkins <maxhawkins@gmail.com>
2020
Sean DuBois <seaduboi@amazon.com>
2121
Sean DuBois <sean@siobud.com>
22+
seb-dev <sebapetit@protonmail.com>
2223
Simone Gotti <simone.gotti@gmail.com>
2324
Woodrow Douglass <wdouglass@carnegierobotics.com>

receiver_report_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,40 @@ func TestReceiverReportUnmarshal(t *testing.T) {
4949
ProfileExtensions: []byte{},
5050
},
5151
},
52+
{
53+
Name: "valid with negative totalLost",
54+
Data: []byte{
55+
// v=2, p=0, count=1, RR, len=7
56+
0x81, 0xc9, 0x0, 0x7,
57+
// ssrc=0x902f9e2e
58+
0x90, 0x2f, 0x9e, 0x2e,
59+
// ssrc=0xbc5e9a40
60+
0xbc, 0x5e, 0x9a, 0x40,
61+
// fracLost=0, totalLost=-1
62+
0x0, 0xff, 0xff, 0xff,
63+
// lastSeq=0x46e1
64+
0x0, 0x0, 0x46, 0xe1,
65+
// jitter=273
66+
0x0, 0x0, 0x1, 0x11,
67+
// lsr=0x9f36432
68+
0x9, 0xf3, 0x64, 0x32,
69+
// delay=150137
70+
0x0, 0x2, 0x4a, 0x79,
71+
},
72+
Want: ReceiverReport{
73+
SSRC: 0x902f9e2e,
74+
Reports: []ReceptionReport{{
75+
SSRC: 0xbc5e9a40,
76+
FractionLost: 0,
77+
TotalLost: -1,
78+
LastSequenceNumber: 0x46e1,
79+
Jitter: 273,
80+
LastSenderReport: 0x9f36432,
81+
Delay: 150137,
82+
}},
83+
ProfileExtensions: []byte{},
84+
},
85+
},
5286
{
5387
Name: "valid with extension data",
5488
Data: []byte{

reception_report.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ type ReceptionReport struct {
1414
FractionLost uint8
1515
// The total number of RTP data packets from source SSRC that have
1616
// been lost since the beginning of reception.
17-
TotalLost uint32
17+
// https://www.ietf.org/rfc/rfc3550.txt Section 6.4 and appendix A.3
18+
// The total number of packets lost is a signed number and can be
19+
// negative
20+
TotalLost int32
1821
// The low 16 bits contain the highest sequence number received in an
1922
// RTP data packet from source SSRC, and the most significant 16
2023
// bits extend that sequence number with the corresponding count of
@@ -71,13 +74,17 @@ func (r ReceptionReport) Marshal() ([]byte, error) {
7174
rawPacket[fractionLostOffset] = r.FractionLost
7275

7376
// pack TotalLost into 24 bits
74-
if r.TotalLost >= (1 << 25) {
77+
// we first convert signed integer to unsigned before using bit operators
78+
uTotalLost := uint32(r.TotalLost)
79+
if uTotalLost&0xff800000 != 0xff800000 && uTotalLost&0xff800000 != 0 {
7580
return nil, errInvalidTotalLost
7681
}
82+
// Convert int32 to int24
83+
uTotalLost = uTotalLost&0x80000000>>8 | uTotalLost&0x007fffff
7784
tlBytes := rawPacket[totalLostOffset:]
78-
tlBytes[0] = byte(r.TotalLost >> 16)
79-
tlBytes[1] = byte(r.TotalLost >> 8)
80-
tlBytes[2] = byte(r.TotalLost)
85+
tlBytes[0] = byte(uTotalLost >> 16)
86+
tlBytes[1] = byte(uTotalLost >> 8)
87+
tlBytes[2] = byte(uTotalLost)
8188

8289
binary.BigEndian.PutUint32(rawPacket[lastSeqOffset:], r.LastSequenceNumber)
8390
binary.BigEndian.PutUint32(rawPacket[jitterOffset:], r.Jitter)
@@ -115,7 +122,13 @@ func (r *ReceptionReport) Unmarshal(rawPacket []byte) error {
115122
r.FractionLost = rawPacket[fractionLostOffset]
116123

117124
tlBytes := rawPacket[totalLostOffset:]
118-
r.TotalLost = uint32(tlBytes[2]) | uint32(tlBytes[1])<<8 | uint32(tlBytes[0])<<16
125+
uTotalLost := uint32(tlBytes[2]) | uint32(tlBytes[1])<<8 | uint32(tlBytes[0])<<16
126+
// test sign
127+
ua := uTotalLost & 0x007fffff
128+
if uTotalLost&0x00800000 == 0x00800000 {
129+
ua |= 0xff800000
130+
}
131+
r.TotalLost = int32(ua)
119132

120133
r.LastSequenceNumber = binary.BigEndian.Uint32(rawPacket[lastSeqOffset:])
121134
r.Jitter = binary.BigEndian.Uint32(rawPacket[jitterOffset:])

0 commit comments

Comments
 (0)