Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/detectors/FrozenVideoTrackDetector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDtxLikeBehavior } from '../helpers/streams';
import {
IssueDetectorResult,
IssueReason,
Expand Down Expand Up @@ -71,6 +72,12 @@ class FrozenVideoTrackDetector extends BaseIssueDetector {
return undefined;
}

const isDtx = isDtxLikeBehavior(videoStream.ssrc, allLastProcessedStats);
if (isDtx) {
// DTX-like behavior detected, ignoring freezes check
return undefined;
}

const deltaFreezeCount = videoStream.freezeCount - (prevStat.freezeCount ?? 0);
const deltaFreezesTimeMs = (videoStream.totalFreezesDuration - (prevStat.totalFreezesDuration ?? 0)) * 1000;
const avgFreezeDurationMs = deltaFreezeCount > 0 ? deltaFreezesTimeMs / deltaFreezeCount : 0;
Expand Down
7 changes: 7 additions & 0 deletions src/detectors/VideoDecoderIssueDetector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { calculateVolatility } from '../helpers/calc';
import { isDtxLikeBehavior } from '../helpers/streams';
import {
IssueDetectorResult,
IssueReason,
Expand Down Expand Up @@ -84,6 +85,12 @@ class VideoDecoderIssueDetector extends BaseIssueDetector {
return undefined;
}

const isDtx = isDtxLikeBehavior(incomeVideoStream.ssrc, allProcessedStats);
if (isDtx) {
// DTX-like behavior detected, ignoring FPS volatility check
return undefined;
}

const volatility = calculateVolatility(allFps);

if (volatility > this.#volatilityThreshold) {
Expand Down
9 changes: 9 additions & 0 deletions src/helpers/calc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
export const calculateMean = (values: number[]) => values.reduce((acc, val) => acc + val, 0) / values.length;

export const calculateVariance = (mean: number, values: number[]) => values
.reduce((sum, val) => sum + (val - mean) ** 2, 0) / values.length;

export const calculateStandardDeviation = (values: number[]) => {
const mean = calculateMean(values);
const variance = calculateVariance(mean, values);
return Math.sqrt(variance);
};

export const calculateVolatility = (values: number[]) => {
if (values.length === 0) {
throw new Error('Cannot calculate volatility for empty array');
Expand Down
42 changes: 42 additions & 0 deletions src/helpers/streams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
};