Skip to content

feat: on stats parsed callback #28

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

Merged
merged 5 commits into from
Dec 3, 2024
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
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.12.0",
"version": "1.13.0-tt-212-on-parsed-stats.2",
"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
13 changes: 10 additions & 3 deletions src/WebRTCIssueDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,19 @@ class WebRTCIssueDetector {
this.calculateNetworkScores(report.stats);
});

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 @@ -133,7 +140,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 @@ -147,7 +154,7 @@ class WebRTCIssueDetector {

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

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

private emitIssues(issues: IssuePayload[]): void {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/PeriodicWebRTCStatsReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PeriodicWebRTCStatsReporter extends EventEmitter {
const reportItems = await this.compositeStatsParser.parse();
const timeTaken = Date.now() - startTime;

this.emit(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, { timeTaken });
this.emit(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, { timeTaken, reportItems });

reportItems.forEach((item: StatsReportItem) => {
this.emit(PeriodicWebRTCStatsReporter.STATS_REPORT_READY_EVENT, item);
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { WebRTCIssueEmitter } from './WebRTCIssueEmitter';

export interface WIDWindow {
wid: {
handleNewPeerConnection(pc: RTCPeerConnection): void;
handleNewPeerConnection(pc: RTCPeerConnection, id?: string): void;
},
}

Expand Down Expand Up @@ -55,6 +55,7 @@ export type WebRTCIssueDetectorConstructorParams = {
logger?: Logger,
onIssues?: (payload: IssueDetectorResult) => void,
onNetworkScoresUpdated?: (payload: NetworkScores) => void,
onStats?: (payload: StatsReportItem[]) => void,
ignoreSSRCList?: number[],
getStatsInterval?: number,
autoAddPeerConnections?: boolean,
Expand Down Expand Up @@ -177,6 +178,7 @@ export type ParsedInboundAudioStreamStats = {
},
trackId: string,
transportId: string,
trackIdentifier: string,
};

export type ParsedOutboundAudioStreamStats = {
Expand Down Expand Up @@ -268,6 +270,7 @@ export type ParsedInboundVideoStreamStats = {
}
trackId: string,
transportId: string,
trackIdentifier: string,
};

export type ParsedOutboundVideoStreamStats = {
Expand Down
2 changes: 1 addition & 1 deletion test/parser/PeriodicWebRTCStatsReporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('wid/lib/PeriodicWebRTCStatsReporter', () => {
reporter.startReporting();
await clock.tickAsync(getStatsInterval);

expect(emitSpy).to.be.calledWith(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, { timeTaken: 0 });
expect(emitSpy).to.be.calledWith(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, { timeTaken: 0, reportItems: undefined });
});

it('should emit stats report ready event for each stats report item', async () => {
Expand Down