|
| 1 | +/* eslint-disable class-methods-use-this */ |
| 2 | +import { |
| 3 | + NetworkScore, |
| 4 | + NetworkScores, |
| 5 | + NetworkScoresCalculator as INetworkScoresCalculator, |
| 6 | + WebRTCStatsParsed, |
| 7 | +} from './types'; |
| 8 | + |
| 9 | +class NetworkScoresCalculator implements INetworkScoresCalculator { |
| 10 | + #lastProcessedStats: { [connectionId: string]: WebRTCStatsParsed } = {}; |
| 11 | + |
| 12 | + calculate(data: WebRTCStatsParsed): NetworkScores { |
| 13 | + const outbound = this.calcucateOutboundScore(data); |
| 14 | + const inbound = this.calculateInboundScore(data); |
| 15 | + this.#lastProcessedStats[data.connection.id] = data; |
| 16 | + return { outbound, inbound }; |
| 17 | + } |
| 18 | + |
| 19 | + private calcucateOutboundScore(data: WebRTCStatsParsed): NetworkScore | undefined { |
| 20 | + const remoteInboundRTPStreamsStats = [ |
| 21 | + ...data.remote?.audio.inbound || [], |
| 22 | + ...data.remote?.video.inbound || [], |
| 23 | + ]; |
| 24 | + |
| 25 | + if (!remoteInboundRTPStreamsStats.length) { |
| 26 | + return undefined; |
| 27 | + } |
| 28 | + |
| 29 | + const previousStats = this.#lastProcessedStats[data.connection.id]; |
| 30 | + if (!previousStats) { |
| 31 | + return undefined; |
| 32 | + } |
| 33 | + |
| 34 | + const previousRemoteInboundRTPStreamsStats = [ |
| 35 | + ...previousStats.remote?.audio.inbound || [], |
| 36 | + ...previousStats.remote?.video.inbound || [], |
| 37 | + ]; |
| 38 | + |
| 39 | + const { packetsSent } = data.connection; |
| 40 | + const lastPacketsSent = previousStats.connection.packetsSent; |
| 41 | + |
| 42 | + const rtpNetworkStats = remoteInboundRTPStreamsStats.reduce((stats, currentStreamStats) => { |
| 43 | + const previousStreamStats = previousRemoteInboundRTPStreamsStats |
| 44 | + .find((stream) => stream.ssrc === currentStreamStats.ssrc); |
| 45 | + |
| 46 | + return { |
| 47 | + sumJitter: stats.sumJitter + currentStreamStats.jitter, |
| 48 | + packetsLost: stats.packetsLost + currentStreamStats.packetsLost, |
| 49 | + lastPacketsLost: stats.lastPacketsLost + (previousStreamStats?.packetsLost || 0), |
| 50 | + }; |
| 51 | + }, { |
| 52 | + sumJitter: 0, |
| 53 | + packetsLost: 0, |
| 54 | + lastPacketsLost: 0, |
| 55 | + }); |
| 56 | + |
| 57 | + const rtt = (1e3 * data.connection.currentRoundTripTime) || 0; |
| 58 | + const { sumJitter } = rtpNetworkStats; |
| 59 | + const avgJitter = sumJitter / remoteInboundRTPStreamsStats.length; |
| 60 | + |
| 61 | + const deltaPacketSent = packetsSent - lastPacketsSent; |
| 62 | + const deltaPacketLost = rtpNetworkStats.packetsLost - rtpNetworkStats.lastPacketsLost; |
| 63 | + |
| 64 | + const packetsLoss = deltaPacketSent && deltaPacketLost |
| 65 | + ? Math.round((deltaPacketLost * 100) / (deltaPacketSent + deltaPacketLost)) |
| 66 | + : 0; |
| 67 | + |
| 68 | + return this.calculateMOS({ avgJitter, rtt, packetsLoss }); |
| 69 | + } |
| 70 | + |
| 71 | + private calculateInboundScore(data: WebRTCStatsParsed): NetworkScore | undefined { |
| 72 | + const inboundRTPStreamsStats = [...data.audio?.inbound, ...data.video?.inbound]; |
| 73 | + if (!inboundRTPStreamsStats.length) { |
| 74 | + return undefined; |
| 75 | + } |
| 76 | + |
| 77 | + const previousStats = this.#lastProcessedStats[data.connection.id]; |
| 78 | + if (!previousStats) { |
| 79 | + return undefined; |
| 80 | + } |
| 81 | + |
| 82 | + const previousInboundStreamStats = [...previousStats.video?.inbound, ...previousStats.audio?.inbound]; |
| 83 | + const { packetsReceived } = data.connection; |
| 84 | + const lastPacketsReceived = previousStats.connection.packetsReceived; |
| 85 | + |
| 86 | + const rtpNetworkStats = inboundRTPStreamsStats.reduce((stats, currentStreamStats) => { |
| 87 | + const previousStreamStats = previousInboundStreamStats.find((stream) => stream.ssrc === currentStreamStats.ssrc); |
| 88 | + return { |
| 89 | + sumJitter: stats.sumJitter + currentStreamStats.jitter, |
| 90 | + packetsLost: stats.packetsLost + currentStreamStats.packetsLost, |
| 91 | + lastPacketsLost: stats.lastPacketsLost + (previousStreamStats?.packetsLost || 0), |
| 92 | + }; |
| 93 | + }, { |
| 94 | + sumJitter: 0, |
| 95 | + packetsLost: 0, |
| 96 | + lastPacketsLost: 0, |
| 97 | + }); |
| 98 | + |
| 99 | + const rtt = (1e3 * data.connection.currentRoundTripTime) || 0; |
| 100 | + const { sumJitter } = rtpNetworkStats; |
| 101 | + const avgJitter = sumJitter / inboundRTPStreamsStats.length; |
| 102 | + |
| 103 | + const deltaPacketReceived = packetsReceived - lastPacketsReceived; |
| 104 | + const deltaPacketLost = rtpNetworkStats.packetsLost - rtpNetworkStats.lastPacketsLost; |
| 105 | + |
| 106 | + const packetsLoss = deltaPacketReceived && deltaPacketLost |
| 107 | + ? Math.round((deltaPacketLost * 100) / (deltaPacketReceived + deltaPacketLost)) |
| 108 | + : 0; |
| 109 | + |
| 110 | + return this.calculateMOS({ avgJitter, rtt, packetsLoss }); |
| 111 | + } |
| 112 | + |
| 113 | + private calculateMOS( |
| 114 | + { avgJitter, rtt, packetsLoss }: |
| 115 | + { avgJitter: number, rtt: number, packetsLoss: number }, |
| 116 | + ): number { |
| 117 | + const effectiveLatency = rtt + (avgJitter * 2) + 10; |
| 118 | + let rFactor = effectiveLatency < 160 |
| 119 | + ? 93.2 - (effectiveLatency / 40) |
| 120 | + : 93.2 - (effectiveLatency / 120) - 10; |
| 121 | + rFactor -= (packetsLoss * 2.5); |
| 122 | + return 1 + (0.035) * rFactor + (0.000007) * rFactor * (rFactor - 60) * (100 - rFactor); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +export default NetworkScoresCalculator; |
0 commit comments