Skip to content

Commit 955179d

Browse files
committed
fix: review improvements
1 parent b4e65b5 commit 955179d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/helpers/calc.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
export const calculateMean = (values: number[]) => values.reduce((acc, val) => acc + val, 0) / values.length;
22

3+
export const calculateVariance = (mean: number, values: number[]) => values
4+
.reduce((sum, val) => sum + (val - mean) ** 2, 0) / values.length;
5+
6+
export const calculateStandardDeviation = (values: number[]) => {
7+
const mean = calculateMean(values);
8+
const variance = calculateVariance(mean, values);
9+
return Math.sqrt(variance);
10+
};
11+
312
export const calculateVolatility = (values: number[]) => {
413
if (values.length === 0) {
514
throw new Error('Cannot calculate volatility for empty array');

src/helpers/streams.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { WebRTCStatsParsedWithNetworkScores } from '../types';
2+
import { calculateStandardDeviation } from './calc';
23

34
export const isDtxLikeBehavior = (
45
ssrc: number,
56
allProcessedStats: WebRTCStatsParsedWithNetworkScores[],
67
stdDevThreshold = 30,
78
): boolean => {
89
const frameIntervals: number[] = [];
9-
for (let i = 0; i < allProcessedStats.length - 1; i += 1) {
10-
const videoStreamStats = allProcessedStats[i].video.inbound.find(
10+
for (let i = 1; i < allProcessedStats.length - 1; i += 1) {
11+
const videoStreamStats = allProcessedStats[i]?.video?.inbound.find(
1112
(stream) => stream.ssrc === ssrc,
1213
);
1314

15+
if (!videoStreamStats) {
16+
continue;
17+
}
18+
1419
const previousVideoStreamStats = allProcessedStats[i - 1]?.video?.inbound?.find(
1520
(stream) => stream.ssrc === ssrc,
1621
);
@@ -32,9 +37,6 @@ export const isDtxLikeBehavior = (
3237
return false;
3338
}
3439

35-
const mean = frameIntervals.reduce((a, b) => a + b, 0) / frameIntervals.length;
36-
const variance = frameIntervals
37-
.reduce((sum, val) => sum + (val - mean) ** 2, 0) / frameIntervals.length;
38-
const stdDev = Math.sqrt(variance);
40+
const stdDev = calculateStandardDeviation(frameIntervals);
3941
return stdDev > stdDevThreshold;
4042
};

0 commit comments

Comments
 (0)