@@ -4,17 +4,23 @@ import {
44 NetworkScores ,
55 INetworkScoresCalculator ,
66 WebRTCStatsParsed ,
7+ NetworkQualityStatsSample ,
78} from './types' ;
89import { scheduleTask } from './utils/tasks' ;
910import { CLEANUP_PREV_STATS_TTL_MS } from './utils/constants' ;
1011
12+ type MosCalculatorResult = {
13+ mos : NetworkScore ,
14+ stats : NetworkQualityStatsSample ,
15+ } ;
16+
1117class 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 : outboundStatsSample } = this . calculateOutboundScore ( data ) || { } ;
23+ const { mos : inbound , stats : inboundStatsSample } = 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+ statsSamples : {
36+ inboundStatsSample,
37+ outboundStatsSample,
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 (
0 commit comments