Skip to content

Commit de8ddff

Browse files
committed
feat: on stats parsed callback
1 parent 1bcc718 commit de8ddff

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/WebRTCIssueDetector.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,19 @@ class WebRTCIssueDetector {
9595
this.calculateNetworkScores(report.stats);
9696
});
9797

98-
this.statsReporter.on(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, (data: { timeTaken: number }) => {
98+
this.statsReporter.on(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, (data: {
99+
timeTaken: number,
100+
reportItems: StatsReportItem[],
101+
}) => {
99102
const payload = {
100103
timeTaken: data.timeTaken,
101104
ts: Date.now(),
102105
};
103106

107+
if (params.onStats) {
108+
params.onStats(data.reportItems);
109+
}
110+
104111
this.eventEmitter.emit(EventType.StatsParsingFinished, payload);
105112
});
106113
}
@@ -133,7 +140,7 @@ class WebRTCIssueDetector {
133140
this.statsReporter.stopReporting();
134141
}
135142

136-
public handleNewPeerConnection(pc: RTCPeerConnection): void {
143+
public handleNewPeerConnection({ pc, id }: { pc: RTCPeerConnection, id?: string }): void {
137144
if (!this.#running && this.autoAddPeerConnections) {
138145
this.logger.debug('Skip handling new peer connection. Detector is not running', pc);
139146
return;
@@ -147,7 +154,7 @@ class WebRTCIssueDetector {
147154

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

150-
this.compositeStatsParser.addPeerConnection({ pc });
157+
this.compositeStatsParser.addPeerConnection({ pc, id });
151158
}
152159

153160
private emitIssues(issues: IssuePayload[]): void {
@@ -173,7 +180,7 @@ class WebRTCIssueDetector {
173180
}
174181

175182
const OriginalRTCPeerConnection = window.RTCPeerConnection;
176-
const onConnectionCreated = (pc: RTCPeerConnection) => this.handleNewPeerConnection(pc);
183+
const onConnectionCreated = (pc: RTCPeerConnection) => this.handleNewPeerConnection({ pc });
177184

178185
function WIDRTCPeerConnection(rtcConfig?: RTCConfiguration) {
179186
const connection = new OriginalRTCPeerConnection(rtcConfig);

src/parser/PeriodicWebRTCStatsReporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class PeriodicWebRTCStatsReporter extends EventEmitter {
6464
const reportItems = await this.compositeStatsParser.parse();
6565
const timeTaken = Date.now() - startTime;
6666

67-
this.emit(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, { timeTaken });
67+
this.emit(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, { timeTaken, reportItems });
6868

6969
reportItems.forEach((item: StatsReportItem) => {
7070
this.emit(PeriodicWebRTCStatsReporter.STATS_REPORT_READY_EVENT, item);

src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { WebRTCIssueEmitter } from './WebRTCIssueEmitter';
44

55
export interface WIDWindow {
66
wid: {
7-
handleNewPeerConnection(pc: RTCPeerConnection): void;
7+
handleNewPeerConnection(params: { pc: RTCPeerConnection, id?: string }): void;
88
},
99
}
1010

@@ -55,6 +55,7 @@ export type WebRTCIssueDetectorConstructorParams = {
5555
logger?: Logger,
5656
onIssues?: (payload: IssueDetectorResult) => void,
5757
onNetworkScoresUpdated?: (payload: NetworkScores) => void,
58+
onStats?: (payload: StatsReportItem[]) => void,
5859
ignoreSSRCList?: number[],
5960
getStatsInterval?: number,
6061
autoAddPeerConnections?: boolean,
@@ -177,6 +178,7 @@ export type ParsedInboundAudioStreamStats = {
177178
},
178179
trackId: string,
179180
transportId: string,
181+
trackIdentifier: string,
180182
};
181183

182184
export type ParsedOutboundAudioStreamStats = {
@@ -268,6 +270,7 @@ export type ParsedInboundVideoStreamStats = {
268270
}
269271
trackId: string,
270272
transportId: string,
273+
trackIdentifier: string,
271274
};
272275

273276
export type ParsedOutboundVideoStreamStats = {

test/parser/PeriodicWebRTCStatsReporter.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('wid/lib/PeriodicWebRTCStatsReporter', () => {
102102
reporter.startReporting();
103103
await clock.tickAsync(getStatsInterval);
104104

105-
expect(emitSpy).to.be.calledWith(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, { timeTaken: 0 });
105+
expect(emitSpy).to.be.calledWith(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, { timeTaken: 0, reportItems: undefined });
106106
});
107107

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

0 commit comments

Comments
 (0)