Skip to content

Commit 380b01d

Browse files
Merge pull request #17 from DolbyIO/develop
Fix calculatePacketsLostRatio
2 parents 1fcf861 + 58a9a6b commit 380b01d

6 files changed

Lines changed: 26 additions & 16 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![npm](https://img.shields.io/npm/v/@dolbyio/webrtc-stats)](https://www.npmjs.com/package/@dolbyio/webrtc-stats)
55
[![License](https://img.shields.io/github/license/DolbyIO/web-webrtc-stats)](LICENSE)
66

7-
# Dolby.io WebRTC Statistics
7+
# Dolby OptiView WebRTC Statistics
88

99
This project is a library to use to parse WebRTC statistics.
1010

@@ -87,7 +87,7 @@ const tokenGenerator = () =>
8787
streamName: STREAM_NAME,
8888
});
8989

90-
const publisher = new Publish(STREAM_NAME, tokenGenerator);
90+
const publisher = new Publish(undefined, tokenGenerator);
9191

9292
// HERE: Publish a stream to Dolby OptiView
9393

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dolbyio/webrtc-stats",
3-
"version": "1.0.3",
4-
"description": "Dolby.io WebRTC Statistics",
3+
"version": "1.0.4",
4+
"description": "Dolby OptiView WebRTC Statistics",
55
"main": "dist/webrtc-stats.js",
66
"typings": "dist/index.d.ts",
77
"scripts": {
@@ -23,7 +23,8 @@
2323
"stats",
2424
"statistics",
2525
"dolbyio",
26-
"millicast"
26+
"millicast",
27+
"optiview"
2728
],
2829
"files": [
2930
"dist/*",

src/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ export const calculatePacketsLostRatio = (
2626
lastTotalPacketsLost?: number,
2727
lastTotalPacketsReceived?: number
2828
): number => {
29-
if (totalPacketsReceived == 0) {
29+
const currentLostPackets = totalPacketsLost - (lastTotalPacketsLost ?? 0);
30+
const currentReceivedPackets = totalPacketsReceived - (lastTotalPacketsReceived ?? 0);
31+
const currentPacketsExpected = currentLostPackets + currentReceivedPackets;
32+
33+
if (currentPacketsExpected === 0) {
3034
return 0;
3135
}
3236

33-
const currentLostPackages = totalPacketsLost - (lastTotalPacketsLost ?? 0);
34-
const currentReceivedPackages = totalPacketsReceived - (lastTotalPacketsReceived ?? 0);
35-
return currentLostPackages / currentReceivedPackages;
37+
return currentLostPackets / currentPacketsExpected;
3638
};

src/webRTCStats.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export class WebRTCStats extends EventEmitter implements WebRTCStats {
247247
async #getInputAudio(rtcStatsReport: RTCStatsReport, entry: RTCInboundRtpStreamStats, last: InputAudio): Promise<InputAudio> {
248248
const bitrate = calculateRate(entry.timestamp, entry.bytesReceived, last?.timestamp, last?.totalBytesReceived);
249249
const packetRate = calculateRate(entry.timestamp, entry.packetsReceived, last?.timestamp, last?.totalPacketsReceived);
250-
const packetLossRatio = calculatePacketsLostRatio(entry.packetsLost, entry.packetsReceived, last?.totalPacketsReceived, last?.totalPacketsLost);
250+
const packetLossRatio = calculatePacketsLostRatio(entry.packetsLost, entry.packetsReceived, last?.totalPacketsLost, last?.totalPacketsReceived);
251251
const packetLossDelta = (entry.packetsLost ?? 0) - (last?.totalPacketsLost ?? 0);
252252
const codec = this.#getCodec(rtcStatsReport, entry.codecId);
253253

tests/utils.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,16 @@ describe('utils test suite', () => {
5858
});
5959

6060
test('calculatePacketsLostRatio', () => {
61-
expect(calculatePacketsLostRatio(100, 0)).toEqual(0);
62-
expect(calculatePacketsLostRatio(100, 200)).toEqual(0.5);
63-
expect(calculatePacketsLostRatio(100, 200, 50)).toEqual(0.25);
64-
expect(calculatePacketsLostRatio(100, 200, 50, 100)).toEqual(0.5);
61+
expect(calculatePacketsLostRatio(100, 0)).toEqual(1);
62+
expect(calculatePacketsLostRatio(0, 1)).toEqual(0);
63+
64+
expect(calculatePacketsLostRatio(100, 300)).toEqual(0.25);
65+
expect(calculatePacketsLostRatio(150, 100, 50)).toEqual(0.5);
66+
expect(calculatePacketsLostRatio(150, 200, 50, 100)).toEqual(0.5);
67+
68+
// corner cases
69+
expect(calculatePacketsLostRatio(0, 0, 0, 0)).toEqual(0);
70+
expect(calculatePacketsLostRatio(100, 100, 100, 100)).toEqual(0);
71+
expect(calculatePacketsLostRatio(0, 0)).toEqual(0);
6572
});
6673
});

0 commit comments

Comments
 (0)