Skip to content

[pull] master from VLprojects:master #2

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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
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"
}
}
51 changes: 26 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +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 @@ -67,13 +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 @@ -104,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 Expand Up @@ -233,6 +217,23 @@ const exampleIssue = {
}
```


### MissingStreamDataDetector
Detects issues with missing data in active inbound streams
```ts
const exampleIssue = {
type: 'stream',
reason: 'missing-video-stream-data' | 'missing-audio-stream-data',
trackIdentifier: 'some-track-id',
statsSample: {
bytesReceivedDelta: 0, // always zero if issue detected
bytesReceived: 2392384,
trackDetached: false,
trackEnded: false,
},
}
```

## Roadmap

- [ ] Adaptive getStats() call interval based on last getStats() execution time
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.11.0",
"version": "1.16.3",
"description": "WebRTC diagnostic tool that detects issues with network or user devices",
"repository": "git@github.com:VLprojects/webrtc-issue-detector.git",
"author": "Roman Kuzakov <roman.kuzakov@gmail.com>",
Expand Down
40 changes: 25 additions & 15 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,16 +17,17 @@ 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';
import MissingStreamDataDetector from './detectors/MissingStreamDataDetector';

class WebRTCIssueDetector {
readonly eventEmitter: WebRTCIssueEmitter;
Expand Down Expand Up @@ -58,13 +60,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(),
new MissingStreamDataDetector(),
];

this.networkScoresCalculator = params.networkScoresCalculator ?? new DefaultNetworkScoresCalculator();
Expand All @@ -86,19 +89,23 @@ 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: { timeTaken: number }) => {
this.statsReporter.on(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, (data: {
timeTaken: number,
reportItems: StatsReportItem[],
}) => {
const payload = {
timeTaken: data.timeTaken,
ts: Date.now(),
};

if (params.onStats) {
params.onStats(data.reportItems);
}

this.eventEmitter.emit(EventType.StatsParsingFinished, payload);
});
}
Expand Down Expand Up @@ -131,7 +138,7 @@ class WebRTCIssueDetector {
this.statsReporter.stopReporting();
}

public handleNewPeerConnection(pc: RTCPeerConnection): void {
public handleNewPeerConnection(pc: RTCPeerConnection, id?: string): void {
if (!this.#running && this.autoAddPeerConnections) {
this.logger.debug('Skip handling new peer connection. Detector is not running', pc);
return;
Expand All @@ -145,23 +152,26 @@ class WebRTCIssueDetector {

this.logger.debug('Handling new peer connection', pc);

this.compositeStatsParser.addPeerConnection({ pc });
this.compositeStatsParser.addPeerConnection({ pc, id });
}

private emitIssues(issues: IssuePayload[]): void {
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
61 changes: 47 additions & 14 deletions src/detectors/BaseIssueDetector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
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 } from '../utils/constants';
import { CLEANUP_PREV_STATS_TTL_MS, MAX_PARSED_STATS_STORAGE_SIZE } from '../utils/constants';

export interface PrevStatsCleanupPayload {
connectionId: string;
Expand All @@ -9,23 +15,34 @@ export interface PrevStatsCleanupPayload {

export interface BaseIssueDetectorParams {
statsCleanupTtlMs?: number;
maxParsedStatsStorageSize?: number;
}

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

readonly #statsCleanupDelayMs: number;

readonly #maxParsedStatsStorageSize: number;

constructor(params: BaseIssueDetectorParams = {}) {
this.#lastProcessedStats = new Map();
this.#statsCleanupDelayMs = params.statsCleanupTtlMs ?? CLEANUP_PREV_STATS_TTL_MS;
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 All @@ -36,7 +53,7 @@ abstract class BaseIssueDetector implements IssueDetector {
protected performPrevStatsCleanup(payload: PrevStatsCleanupPayload): void {
const { connectionId, cleanupCallback } = payload;

if (!this.#lastProcessedStats.has(connectionId)) {
if (!this.#parsedStatsStorage.has(connectionId)) {
return;
}

Expand All @@ -53,16 +70,32 @@ abstract class BaseIssueDetector implements IssueDetector {
});
}

protected setLastProcessedStats(connectionId: string, parsedStats: WebRTCStatsParsed): void {
this.#lastProcessedStats.set(connectionId, parsedStats);
protected setLastProcessedStats(connectionId: string, parsedStats: WebRTCStatsParsedWithNetworkScores): void {
if (!connectionId || parsedStats.connection.id !== connectionId) {
return;
}

const connectionStats = this.#parsedStatsStorage.get(connectionId) ?? [];
connectionStats.push(parsedStats);

if (connectionStats.length > this.#maxParsedStatsStorageSize) {
connectionStats.shift();
}

this.#parsedStatsStorage.set(connectionId, connectionStats);
}

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

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

private deleteLastProcessedStats(connectionId: string): void {
this.#lastProcessedStats.delete(connectionId);
protected deleteLastProcessedStats(connectionId: string): void {
this.#parsedStatsStorage.delete(connectionId);
}
}

Expand Down
Loading
Loading