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: Improve FrozenVideo and VideoDecoderCPU detectors #32

Merged
merged 19 commits into from
Jan 23, 2025
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
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"no-underscore-dangle": "off",
"max-len": ["error", { "code": 120 }],
"import/extensions": "off",
"import/no-cycle": "off"
"import/no-cycle": "off",
"no-continue": "off",
"import/prefer-default-export": "off"
}
}
32 changes: 7 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ 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 +67,13 @@ 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,34 +104,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,
}
```

### FramesEncodedSentIssueDetector
Detects issues with outbound network throughput.
```js
const exampleIssue = {
type: 'network',
reason: 'outbound-network-throughput',
statsSample: {
deltaFramesSent: 900,
deltaFramesEncoded: 1000,
missedFramesPct: 10,
},
ssrc: 1234,
}
```

Expand Down
23 changes: 11 additions & 12 deletions src/WebRTCIssueDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
IssueDetector,
IssuePayload,
Logger,
NetworkScores,
StatsReportItem,
WebRTCIssueDetectorConstructorParams,
WebRTCStatsParsed,
Expand All @@ -16,14 +17,13 @@ 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 @@ -60,14 +60,13 @@ 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(),
new MissingStreamDataDetector(),
];

Expand All @@ -90,11 +89,8 @@ class WebRTCIssueDetector {
}

this.statsReporter.on(PeriodicWebRTCStatsReporter.STATS_REPORT_READY_EVENT, (report: StatsReportItem) => {
this.detectIssues({
data: report.stats,
});

this.calculateNetworkScores(report.stats);
const networkScores = this.calculateNetworkScores(report.stats);
this.detectIssues({ data: report.stats }, networkScores);
});

this.statsReporter.on(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, (data: {
Expand Down Expand Up @@ -163,16 +159,19 @@ class WebRTCIssueDetector {
this.eventEmitter.emit(EventType.Issue, issues);
}

private detectIssues({ data }: DetectIssuesPayload): void {
const issues = this.detectors.reduce<IssuePayload[]>((acc, detector) => [...acc, ...detector.detect(data)], []);
private detectIssues({ data }: DetectIssuesPayload, networkScores: NetworkScores): void {
const issues = this.detectors
.reduce<IssuePayload[]>((acc, detector) => [...acc, ...detector.detect(data, networkScores)], []);

if (issues.length > 0) {
this.emitIssues(issues);
}
}

private calculateNetworkScores(data: WebRTCStatsParsed): void {
private calculateNetworkScores(data: WebRTCStatsParsed): NetworkScores {
const networkScores = this.networkScoresCalculator.calculate(data);
this.eventEmitter.emit(EventType.NetworkScoresUpdated, networkScores);
return networkScores;
}

private wrapRTCPeerConnection(): void {
Expand Down
32 changes: 23 additions & 9 deletions src/detectors/BaseIssueDetector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { IssueDetector, IssueDetectorResult, WebRTCStatsParsed } from '../types';
import {
IssueDetector,
IssueDetectorResult,
NetworkScores,
WebRTCStatsParsed,
WebRTCStatsParsedWithNetworkScores,
} from '../types';
import { scheduleTask } from '../utils/tasks';
import { CLEANUP_PREV_STATS_TTL_MS, MAX_PARSED_STATS_STORAGE_SIZE } from '../utils/constants';

Expand All @@ -13,7 +19,7 @@ export interface BaseIssueDetectorParams {
}

abstract class BaseIssueDetector implements IssueDetector {
readonly #parsedStatsStorage: Map<string, WebRTCStatsParsed[]> = new Map();
readonly #parsedStatsStorage: Map<string, WebRTCStatsParsedWithNetworkScores[]> = new Map();

readonly #statsCleanupDelayMs: number;

Expand All @@ -24,11 +30,19 @@ abstract class BaseIssueDetector implements IssueDetector {
this.#maxParsedStatsStorageSize = params.maxParsedStatsStorageSize ?? MAX_PARSED_STATS_STORAGE_SIZE;
}

abstract performDetection(data: WebRTCStatsParsed): IssueDetectorResult;
abstract performDetection(data: WebRTCStatsParsedWithNetworkScores): IssueDetectorResult;

detect(data: WebRTCStatsParsed): IssueDetectorResult {
const result = this.performDetection(data);
detect(data: WebRTCStatsParsed, networkScores?: NetworkScores): IssueDetectorResult {
const parsedStatsWithNetworkScores = {
...data,
networkScores: {
...networkScores,
statsSamples: networkScores?.statsSamples || {},
},
};
const result = this.performDetection(parsedStatsWithNetworkScores);

this.setLastProcessedStats(data.connection.id, parsedStatsWithNetworkScores);
this.performPrevStatsCleanup({
connectionId: data.connection.id,
});
Expand Down Expand Up @@ -56,7 +70,7 @@ abstract class BaseIssueDetector implements IssueDetector {
});
}

protected setLastProcessedStats(connectionId: string, parsedStats: WebRTCStatsParsed): void {
protected setLastProcessedStats(connectionId: string, parsedStats: WebRTCStatsParsedWithNetworkScores): void {
if (!connectionId || parsedStats.connection.id !== connectionId) {
return;
}
Expand All @@ -71,16 +85,16 @@ abstract class BaseIssueDetector implements IssueDetector {
this.#parsedStatsStorage.set(connectionId, connectionStats);
}

protected getLastProcessedStats(connectionId: string): WebRTCStatsParsed | undefined {
protected getLastProcessedStats(connectionId: string): WebRTCStatsParsedWithNetworkScores | undefined {
const connectionStats = this.#parsedStatsStorage.get(connectionId);
return connectionStats?.[connectionStats.length - 1];
}

protected getAllLastProcessedStats(connectionId: string): WebRTCStatsParsed[] {
protected getAllLastProcessedStats(connectionId: string): WebRTCStatsParsedWithNetworkScores[] {
return this.#parsedStatsStorage.get(connectionId) ?? [];
}

private deleteLastProcessedStats(connectionId: string): void {
protected deleteLastProcessedStats(connectionId: string): void {
this.#parsedStatsStorage.delete(connectionId);
}
}
Expand Down
80 changes: 0 additions & 80 deletions src/detectors/FramesDroppedIssueDetector.ts

This file was deleted.

83 changes: 0 additions & 83 deletions src/detectors/FramesEncodedSentIssueDetector.ts

This file was deleted.

Loading
Loading