Skip to content

Commit ee0b2f4

Browse files
committed
fix RTCP total lost computation
1 parent 2c6ae2a commit ee0b2f4

File tree

2 files changed

+23
-38
lines changed

2 files changed

+23
-38
lines changed

src/packets/RTCP/ReceiverReportPacket.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
COMMON_HEADER_LENGTH,
66
} from './RtcpPacket';
77
import { Serializable, type SerializableDump } from '../Serializable';
8+
import {
9+
readSigned3BytesInDataView,
10+
writeSigned3BytesInDataView,
11+
} from '../../utils/byteOps';
812

913
// Common RTCP header length + 4 (SSRC of packet sender).
1014
const FIXED_HEADER_LENGTH = COMMON_HEADER_LENGTH + 4;
@@ -422,40 +426,21 @@ export class ReceptionReport extends Serializable {
422426
* Get total lost.
423427
*/
424428
getTotalLost(): number {
425-
let value = this.view.getUint32(4) & 0x0fff;
426-
427-
// Possitive value.
428-
if (((value >> 23) & 1) == 0) {
429-
return value;
430-
}
431-
432-
// Negative value.
433-
if (value != 0x0800000) {
434-
value &= ~(1 << 23);
435-
}
436-
437-
return -value;
429+
return readSigned3BytesInDataView({
430+
view: this.view,
431+
pos: 5,
432+
});
438433
}
439434

440435
/**
441436
* Set total lost.
442437
*/
443438
setTotalLost(totalLost: number): void {
444-
// Get the limit value for possitive and negative total lost.
445-
const clamp =
446-
totalLost >= 0
447-
? totalLost > 0x07fffff
448-
? 0x07fffff
449-
: totalLost
450-
: -totalLost > 0x0800000
451-
? 0x0800000
452-
: -totalLost;
453-
454-
const value = totalLost >= 0 ? clamp & 0x07fffff : clamp | 0x0800000;
455-
const fractionLost = this.view.getUint8(4);
456-
457-
this.view.setUint32(4, value);
458-
this.view.setUint8(4, fractionLost);
439+
writeSigned3BytesInDataView({
440+
view: this.view,
441+
pos: 5,
442+
value: totalLost,
443+
});
459444

460445
this.setSerializationNeeded(true);
461446
}

src/test/packets/RTCP/ReceiverReportPacket.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const receptionReportDump1: ReceptionReportDump = {
1313
byteLength: 24,
1414
ssrc: 26422708,
1515
fractionLost: 80,
16-
totalLost: 216,
16+
totalLost: -1,
1717
highestSeq: 342342,
1818
jitter: 0,
1919
lsr: 8234,
@@ -47,9 +47,9 @@ describe('parse RTCP Receiver Report packet', () => {
4747
0x2d,
4848
0xb4, // SSRC: 0x01932db4
4949
0x50,
50-
0x00,
51-
0x00,
52-
0xd8, // Fraction lost: 80, Total lost: 216
50+
0xff,
51+
0xff,
52+
0xff, // Fraction lost: 80, Total lost: -1
5353
0x00,
5454
0x05,
5555
0x39,
@@ -134,9 +134,9 @@ describe('parse RTCP Receiver Report packet', () => {
134134
0x2d,
135135
0xb4, // SSRC: 0x01932db4
136136
0x50,
137-
0x00,
138-
0x00,
139-
0xd8, // Fraction lost: 80, Total lost: 216
137+
0xff,
138+
0xff,
139+
0xff, // Fraction lost: 80, Total lost: -1
140140
0x00,
141141
0x05,
142142
0x39,
@@ -325,9 +325,9 @@ describe('parse RTCP Reception Report', () => {
325325
0x2d,
326326
0xb4, // SSRC: 0x01932db4
327327
0x50,
328-
0x00,
329-
0x00,
330-
0xd8, // Fraction lost: 80, Total lost: 216
328+
0xff,
329+
0xff,
330+
0xff, // Fraction lost: 80, Total lost: -1
331331
0x00,
332332
0x05,
333333
0x39,

0 commit comments

Comments
 (0)