Skip to content

Commit 35954ba

Browse files
feat: added ability to specify id for pc
1 parent 2d1791e commit 35954ba

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/NetworkScoresCalculator.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
INetworkScoresCalculator,
66
WebRTCStatsParsed,
77
NetworkQualityStatsSample,
8+
NetworkScoresPayload,
89
} from './types';
910
import { scheduleTask } from './utils/tasks';
1011
import { CLEANUP_PREV_STATS_TTL_MS } from './utils/constants';
@@ -17,7 +18,7 @@ type MosCalculatorResult = {
1718
class NetworkScoresCalculator implements INetworkScoresCalculator {
1819
#lastProcessedStats: { [connectionId: string]: WebRTCStatsParsed } = {};
1920

20-
calculate(data: WebRTCStatsParsed): NetworkScores {
21+
calculate({ data, id }: NetworkScoresPayload): NetworkScores {
2122
const { connection: { id: connectionId } } = data;
2223
const { mos: outbound, stats: outboundStatsSample } = this.calculateOutboundScore(data) || {};
2324
const { mos: inbound, stats: inboundStatsSample } = this.calculateInboundScore(data) || {};
@@ -32,6 +33,7 @@ class NetworkScoresCalculator implements INetworkScoresCalculator {
3233
return {
3334
outbound,
3435
inbound,
36+
id,
3537
statsSamples: {
3638
inboundStatsSample,
3739
outboundStatsSample,

src/WebRTCIssueDetector.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
Logger,
1010
StatsReportItem,
1111
WebRTCIssueDetectorConstructorParams,
12-
WebRTCStatsParsed,
12+
NetworkScoresPayload,
1313
WIDWindow,
1414
} from './types';
1515
import PeriodicWebRTCStatsReporter from './parser/PeriodicWebRTCStatsReporter';
@@ -85,12 +85,10 @@ class WebRTCIssueDetector {
8585
this.wrapRTCPeerConnection();
8686
}
8787

88-
this.statsReporter.on(PeriodicWebRTCStatsReporter.STATS_REPORT_READY_EVENT, (report: StatsReportItem) => {
89-
this.detectIssues({
90-
data: report.stats,
91-
});
88+
this.statsReporter.on(PeriodicWebRTCStatsReporter.STATS_REPORT_READY_EVENT, ({ stats, id }: StatsReportItem) => {
89+
this.detectIssues({ data: stats });
9290

93-
this.calculateNetworkScores(report.stats);
91+
this.calculateNetworkScores({ data: stats, id });
9492
});
9593

9694
this.statsReporter.on(PeriodicWebRTCStatsReporter.STATS_REPORTS_PARSED, (data: { timeTaken: number }) => {
@@ -131,7 +129,7 @@ class WebRTCIssueDetector {
131129
this.statsReporter.stopReporting();
132130
}
133131

134-
public handleNewPeerConnection(pc: RTCPeerConnection): void {
132+
public handleNewPeerConnection(pc: RTCPeerConnection, id?: string): void {
135133
if (!this.#running && this.autoAddPeerConnections) {
136134
this.logger.debug('Skip handling new peer connection. Detector is not running', pc);
137135
return;
@@ -143,9 +141,9 @@ class WebRTCIssueDetector {
143141
this.statsReporter.startReporting();
144142
}
145143

146-
this.logger.debug('Handling new peer connection', pc);
144+
this.logger.debug(`Handling new peer connection with id ${id}`, pc);
147145

148-
this.compositeStatsParser.addPeerConnection({ pc });
146+
this.compositeStatsParser.addPeerConnection({ pc, id });
149147
}
150148

151149
private emitIssues(issues: IssuePayload[]): void {
@@ -159,8 +157,8 @@ class WebRTCIssueDetector {
159157
}
160158
}
161159

162-
private calculateNetworkScores(data: WebRTCStatsParsed): void {
163-
const networkScores = this.networkScoresCalculator.calculate(data);
160+
private calculateNetworkScores(payload: NetworkScoresPayload): void {
161+
const networkScores = this.networkScoresCalculator.calculate(payload);
164162
this.eventEmitter.emit(EventType.NetworkScoresUpdated, networkScores);
165163
}
166164

src/types.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface IssueDetector {
1515
}
1616

1717
export interface INetworkScoresCalculator {
18-
calculate(data: WebRTCStatsParsed): NetworkScores;
18+
calculate(data: NetworkScoresPayload): NetworkScores;
1919
}
2020

2121
export enum EventType {
@@ -105,9 +105,15 @@ export type NetworkQualityStatsSample = {
105105
packetsLoss: number;
106106
};
107107

108+
export type NetworkScoresPayload = {
109+
data: WebRTCStatsParsed,
110+
id?: string;
111+
};
112+
108113
export type NetworkScores = {
109114
outbound?: NetworkScore,
110115
inbound?: NetworkScore,
116+
id?: string;
111117
statsSamples: {
112118
outboundStatsSample?: NetworkQualityStatsSample,
113119
inboundStatsSample?: NetworkQualityStatsSample,

0 commit comments

Comments
 (0)