Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Decoder issue detector #31

Closed
wants to merge 10 commits into from
Closed
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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"no-underscore-dangle": "off",
"max-len": ["error", { "code": 120 }],
"import/extensions": "off",
"import/no-cycle": "off"
"import/no-cycle": "off",
"no-continue": "off"
}
}
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ By default, WebRTCIssueDetector can be created with minimum of mandatory constru
```typescript
import WebRTCIssueDetector, {
QualityLimitationsIssueDetector,
FramesDroppedIssueDetector,
FramesEncodedSentIssueDetector,
InboundNetworkIssueDetector,
OutboundNetworkIssueDetector,
NetworkMediaSyncIssueDetector,
AvailableOutgoingBitrateIssueDetector,
UnknownVideoDecoderImplementationDetector,
FrozenVideoTrackDetector,
VideoDecoderIssueDetector,
} from 'webrtc-issue-detector';

const widWithDefaultConstructorArgs = new WebRTCIssueDetector();
Expand All @@ -68,14 +68,14 @@ const widWithDefaultConstructorArgs = new WebRTCIssueDetector();
const widWithCustomConstructorArgs = new WebRTCIssueDetector({
detectors: [ // you are free to change the detectors list according to your needs
new QualityLimitationsIssueDetector(),
new FramesDroppedIssueDetector(),
new FramesEncodedSentIssueDetector(),
new InboundNetworkIssueDetector(),
new OutboundNetworkIssueDetector(),
new NetworkMediaSyncIssueDetector(),
new AvailableOutgoingBitrateIssueDetector(),
new UnknownVideoDecoderImplementationDetector(),
new FrozenVideoTrackDetector(),
new VideoDecoderIssueDetector(),
],
getStatsInterval: 10_000, // set custom stats parsing interval
onIssues: (payload: IssueDetectorResult) => {
Expand Down Expand Up @@ -106,19 +106,18 @@ const exampleIssue = {
}
```

### FramesDroppedIssueDetector
### VideoDecoderIssueDetector
Detects issues with decoder.
```js
const exampleIssue = {
type: 'cpu',
reason: 'decoder-cpu-throttling',
statsSample: {
deltaFramesDropped: 100,
deltaFramesReceived: 1000,
deltaFramesDecoded: 900,
framesDroppedPct: 10,
affectedStreamsPercent: 67,
throtthedStreams: [
{ ssrc: 123, allDecodeTimePerFrame: [1.2, 1.6, 1.9, 2.4, 2.9], volatility: 1.7 },
]
},
ssrc: 1234,
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webrtc-issue-detector",
"version": "1.13.0",
"version": "1.14.0-decoder-issue-detector.2",
"description": "WebRTC diagnostic tool that detects issues with network or user devices",
"repository": "[email protected]:VLprojects/webrtc-issue-detector.git",
"author": "Roman Kuzakov <[email protected]>",
Expand Down
4 changes: 2 additions & 2 deletions src/WebRTCIssueDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import PeriodicWebRTCStatsReporter from './parser/PeriodicWebRTCStatsReporter';
import DefaultNetworkScoresCalculator from './NetworkScoresCalculator';
import {
AvailableOutgoingBitrateIssueDetector,
FramesDroppedIssueDetector,
FramesEncodedSentIssueDetector,
InboundNetworkIssueDetector,
NetworkMediaSyncIssueDetector,
OutboundNetworkIssueDetector,
QualityLimitationsIssueDetector,
UnknownVideoDecoderImplementationDetector,
FrozenVideoTrackDetector,
VideoDecoderIssueDetector,
} from './detectors';
import { CompositeRTCStatsParser, RTCStatsParser } from './parser';
import createLogger from './utils/logger';
Expand Down Expand Up @@ -59,14 +59,14 @@ class WebRTCIssueDetector {

this.detectors = params.detectors ?? [
new QualityLimitationsIssueDetector(),
new FramesDroppedIssueDetector(),
new FramesEncodedSentIssueDetector(),
new InboundNetworkIssueDetector(),
new OutboundNetworkIssueDetector(),
new NetworkMediaSyncIssueDetector(),
new AvailableOutgoingBitrateIssueDetector(),
new UnknownVideoDecoderImplementationDetector(),
new FrozenVideoTrackDetector(),
new VideoDecoderIssueDetector(),
];

this.networkScoresCalculator = params.networkScoresCalculator ?? new DefaultNetworkScoresCalculator();
Expand Down
80 changes: 0 additions & 80 deletions src/detectors/FramesDroppedIssueDetector.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/detectors/FrozenVideoTrackDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class FrozenVideoTrackDetector extends BaseIssueDetector {
return;
}

// We skip it when ratio is too low because it should be handled by FramesDroppedIssueDetector
// We skip it when ratio is too low because it should be handled by VideoDecoderIssueDetector
if (ratioFramesDropped >= this.#framesDroppedThreshold) {
return;
}
Expand Down
122 changes: 122 additions & 0 deletions src/detectors/VideoDecoderIssueDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
IssueDetectorResult,
IssueReason,
IssueType,
WebRTCStatsParsed,
} from '../types';
import BaseIssueDetector, { BaseIssueDetectorParams } from './BaseIssueDetector';

interface VideoDecoderIssueDetectorParams extends BaseIssueDetectorParams {
volatilityThreshold?: number;
affectedStreamsPercentThreshold?: number;
}

class VideoDecoderIssueDetector extends BaseIssueDetector {
#volatilityThreshold: number;

#affectedStreamsPercentThreshold: number;

constructor(params: VideoDecoderIssueDetectorParams = {}) {
super(params);
this.#volatilityThreshold = params.volatilityThreshold ?? 1.5;
this.#affectedStreamsPercentThreshold = params.affectedStreamsPercentThreshold ?? 50;
}

performDetection(data: WebRTCStatsParsed): IssueDetectorResult {
const { connection: { id: connectionId } } = data;
const issues = this.processData(data);
this.setLastProcessedStats(connectionId, data);
return issues;
}

private processData(data: WebRTCStatsParsed): IssueDetectorResult {
const issues: IssueDetectorResult = [];

const allProcessedStats = [
...this.getAllLastProcessedStats(data.connection.id),
data,
];

const throtthedStreams = data.video.inbound
.map((incomeVideoStream): { ssrc: number, allDecodeTimePerFrame: number[], volatility: number } | undefined => {
const allDecodeTimePerFrame: number[] = [];

// We need at least 4 elements to have enough representation
if (allProcessedStats.length < 4) {
return undefined;
}

// exclude first element to calculate accurate delta
for (let i = 1; i < allProcessedStats.length; i += 1) {
let deltaFramesDecoded = 0;
let deltaTotalDecodeTime = 0;
let decodeTimePerFrame = 0;

const videoStreamStats = allProcessedStats[i].video.inbound.find(
(stream) => stream.id === incomeVideoStream.id,
);

if (!videoStreamStats) {
continue;
}

const prevVideoStreamStats = allProcessedStats[i - 1].video.inbound.find(
(stream) => stream.id === incomeVideoStream.id,
);

if (prevVideoStreamStats) {
deltaFramesDecoded = videoStreamStats.framesDecoded - prevVideoStreamStats.framesDecoded;
deltaTotalDecodeTime = videoStreamStats.totalDecodeTime - prevVideoStreamStats.totalDecodeTime;
}

if (deltaTotalDecodeTime > 0 && deltaFramesDecoded > 0) {
decodeTimePerFrame = (deltaTotalDecodeTime * 1000) / deltaFramesDecoded;
}

allDecodeTimePerFrame.push(decodeTimePerFrame);
}

// Calculate volatility
const mean = allDecodeTimePerFrame.reduce((acc, val) => acc + val, 0) / allDecodeTimePerFrame.length;
const squaredDiffs = allDecodeTimePerFrame.map((val) => (val - mean) ** 2);
const variance = squaredDiffs.reduce((acc, val) => acc + val, 0) / squaredDiffs.length;
const volatility = Math.sqrt(variance);

const isDecodeTimePerFrameIncrease = allDecodeTimePerFrame.every(
(decodeTimePerFrame, index) => index === 0 || decodeTimePerFrame > allDecodeTimePerFrame[index - 1],
);

console.log({
allDecodeTimePerFrame,
isDecodeTimePerFrameIncrease,
mean,
volatility,
});

if (volatility > this.#volatilityThreshold && isDecodeTimePerFrameIncrease) {
console.log('CPU THROTTLE SUSPECTED FOR STREAM', incomeVideoStream.ssrc);
return { ssrc: incomeVideoStream.ssrc, allDecodeTimePerFrame, volatility };
}

return undefined;
})
.filter((throttledVideoStream) => Boolean(throttledVideoStream));

const affectedStreamsPercent = throtthedStreams.length / (data.video.inbound.length / 100);
if (affectedStreamsPercent > this.#affectedStreamsPercentThreshold) {
console.log('CPU THROTTLE DETECTED');
issues.push({
type: IssueType.CPU,
reason: IssueReason.DecoderCPUThrottling,
statsSample: {
affectedStreamsPercent,
throtthedStreams,
},
});
}

return issues;
}
}

export default VideoDecoderIssueDetector;
2 changes: 1 addition & 1 deletion src/detectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export { default as BaseIssueDetector } from './BaseIssueDetector';
export { default as AvailableOutgoingBitrateIssueDetector } from './AvailableOutgoingBitrateIssueDetector';
export { default as FramesDroppedIssueDetector } from './FramesDroppedIssueDetector';
export { default as FramesEncodedSentIssueDetector } from './FramesEncodedSentIssueDetector';
export { default as InboundNetworkIssueDetector } from './InboundNetworkIssueDetector';
export { default as NetworkMediaSyncIssueDetector } from './NetworkMediaSyncIssueDetector';
export { default as OutboundNetworkIssueDetector } from './OutboundNetworkIssueDetector';
export { default as QualityLimitationsIssueDetector } from './QualityLimitationsIssueDetector';
export { default as UnknownVideoDecoderImplementationDetector } from './UnknownVideoDecoderImplementationDetector';
export { default as FrozenVideoTrackDetector } from './FrozenVideoTrackDetector';
export { default as VideoDecoderIssueDetector } from './VideoDecoderIssueDetector';
Loading