Skip to content

Commit 8ead388

Browse files
committed
feat: Added debug stats to MOS output
1 parent 2daeb9d commit 8ead388

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/NetworkScoresCalculator.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ import {
44
NetworkScores,
55
INetworkScoresCalculator,
66
WebRTCStatsParsed,
7+
NetworkQualityStats,
78
} from './types';
89
import { scheduleTask } from './utils/tasks';
910
import { CLEANUP_PREV_STATS_TTL_MS } from './utils/constants';
1011

12+
type MosCalculatorResult = {
13+
mos: NetworkScore,
14+
stats: NetworkQualityStats,
15+
};
16+
1117
class NetworkScoresCalculator implements INetworkScoresCalculator {
1218
#lastProcessedStats: { [connectionId: string]: WebRTCStatsParsed } = {};
1319

1420
calculate(data: WebRTCStatsParsed): NetworkScores {
1521
const { connection: { id: connectionId } } = data;
16-
const outbound = this.calculateOutboundScore(data);
17-
const inbound = this.calculateInboundScore(data);
22+
const { mos: outbound, stats: outboundStats } = this.calculateOutboundScore(data) || {};
23+
const { mos: inbound, stats: inboundStats } = this.calculateInboundScore(data) || {};
1824
this.#lastProcessedStats[connectionId] = data;
1925

2026
scheduleTask({
@@ -23,10 +29,17 @@ class NetworkScoresCalculator implements INetworkScoresCalculator {
2329
callback: () => (delete this.#lastProcessedStats[connectionId]),
2430
});
2531

26-
return { outbound, inbound };
32+
return {
33+
outbound,
34+
inbound,
35+
debug: {
36+
inboundStats,
37+
outboundStats,
38+
},
39+
};
2740
}
2841

29-
private calculateOutboundScore(data: WebRTCStatsParsed): NetworkScore | undefined {
42+
private calculateOutboundScore(data: WebRTCStatsParsed): MosCalculatorResult | undefined {
3043
const remoteInboundRTPStreamsStats = [
3144
...data.remote?.audio.inbound || [],
3245
...data.remote?.video.inbound || [],
@@ -75,10 +88,14 @@ class NetworkScoresCalculator implements INetworkScoresCalculator {
7588
? Math.round((deltaPacketLost * 100) / (deltaPacketSent + deltaPacketLost))
7689
: 0;
7790

78-
return this.calculateMOS({ avgJitter, rtt, packetsLoss });
91+
const mos = this.calculateMOS({ avgJitter, rtt, packetsLoss });
92+
return {
93+
mos,
94+
stats: { avgJitter, rtt, packetsLoss },
95+
};
7996
}
8097

81-
private calculateInboundScore(data: WebRTCStatsParsed): NetworkScore | undefined {
98+
private calculateInboundScore(data: WebRTCStatsParsed): MosCalculatorResult | undefined {
8299
const inboundRTPStreamsStats = [...data.audio?.inbound, ...data.video?.inbound];
83100
if (!inboundRTPStreamsStats.length) {
84101
return undefined;
@@ -117,7 +134,11 @@ class NetworkScoresCalculator implements INetworkScoresCalculator {
117134
? Math.round((deltaPacketLost * 100) / (deltaPacketReceived + deltaPacketLost))
118135
: 0;
119136

120-
return this.calculateMOS({ avgJitter, rtt, packetsLoss });
137+
const mos = this.calculateMOS({ avgJitter, rtt, packetsLoss });
138+
return {
139+
mos,
140+
stats: { avgJitter, rtt, packetsLoss },
141+
};
121142
}
122143

123144
private calculateMOS(

src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,19 @@ export type DetectIssuesPayload = {
9999

100100
export type NetworkScore = number;
101101

102+
export type NetworkQualityStats = {
103+
avgJitter: number;
104+
rtt: number;
105+
packetsLoss: number;
106+
};
107+
102108
export type NetworkScores = {
103109
outbound?: NetworkScore,
104110
inbound?: NetworkScore,
111+
debug: {
112+
outboundStats?: NetworkQualityStats,
113+
inboundStats?: NetworkQualityStats,
114+
},
105115
};
106116

107117
export type StatsParsingFinishedPayload = {

0 commit comments

Comments
 (0)