-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstreams.ts
42 lines (34 loc) · 1.23 KB
/
streams.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { WebRTCStatsParsedWithNetworkScores } from '../types';
import { calculateStandardDeviation } from './calc';
export const isDtxLikeBehavior = (
ssrc: number,
allProcessedStats: WebRTCStatsParsedWithNetworkScores[],
stdDevThreshold = 30,
): boolean => {
const frameIntervals: number[] = [];
for (let i = 1; i < allProcessedStats.length - 1; i += 1) {
const videoStreamStats = allProcessedStats[i]?.video?.inbound.find(
(stream) => stream.ssrc === ssrc,
);
if (!videoStreamStats) {
continue;
}
const previousVideoStreamStats = allProcessedStats[i - 1]?.video?.inbound?.find(
(stream) => stream.ssrc === ssrc,
);
if (!videoStreamStats || !previousVideoStreamStats) {
continue;
}
const deltaTime = videoStreamStats.timestamp - previousVideoStreamStats.timestamp;
const deltaFrames = videoStreamStats.framesDecoded - previousVideoStreamStats.framesDecoded;
if (deltaFrames > 0) {
const frameInterval = deltaTime / deltaFrames; // Average time per frame
frameIntervals.push(frameInterval);
}
}
if (frameIntervals.length <= 1) {
return false;
}
const stdDev = calculateStandardDeviation(frameIntervals);
return stdDev > stdDevThreshold;
};