-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathNetworkMediaSyncIssueDetector.ts
70 lines (57 loc) · 2.3 KB
/
NetworkMediaSyncIssueDetector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {
IssueDetectorResult,
IssueReason,
IssueType,
WebRTCStatsParsed,
} from '../types';
import BaseIssueDetector, { BaseIssueDetectorParams } from './BaseIssueDetector';
interface NetworkMediaSyncIssueDetectorParams extends BaseIssueDetectorParams {
correctedSamplesThresholdPct?: number
}
class NetworkMediaSyncIssueDetector extends BaseIssueDetector {
readonly #correctedSamplesThresholdPct: number;
constructor(params: NetworkMediaSyncIssueDetectorParams = {}) {
super();
this.#correctedSamplesThresholdPct = params.correctedSamplesThresholdPct ?? 5;
}
performDetection(data: WebRTCStatsParsed): IssueDetectorResult {
return this.processData(data);
}
private processData(data: WebRTCStatsParsed): IssueDetectorResult {
const inboundRTPAudioStreamsStats = data.audio.inbound;
const issues: IssueDetectorResult = [];
const previousInboundRTPAudioStreamsStats = this.getLastProcessedStats(data.connection.id)?.audio.inbound;
if (!previousInboundRTPAudioStreamsStats) {
return issues;
}
inboundRTPAudioStreamsStats.forEach((stats) => {
const previousStreamStats = previousInboundRTPAudioStreamsStats.find((item) => item.ssrc === stats.ssrc);
if (!previousStreamStats) {
return;
}
const nowCorrectedSamples = stats.track.insertedSamplesForDeceleration
+ stats.track.removedSamplesForAcceleration;
const lastCorrectedSamples = previousStreamStats.track.insertedSamplesForDeceleration
+ previousStreamStats.track.removedSamplesForAcceleration;
if (nowCorrectedSamples === lastCorrectedSamples) {
return;
}
const deltaSamplesReceived = stats.track.totalSamplesReceived - previousStreamStats.track.totalSamplesReceived;
const deltaCorrectedSamples = nowCorrectedSamples - lastCorrectedSamples;
const correctedSamplesPct = Math.round((deltaCorrectedSamples * 100) / deltaSamplesReceived);
const statsSample = {
correctedSamplesPct,
};
if (correctedSamplesPct > this.#correctedSamplesThresholdPct) {
issues.push({
statsSample,
type: IssueType.Network,
reason: IssueReason.NetworkMediaSyncFailure,
ssrc: stats.ssrc,
});
}
});
return issues;
}
}
export default NetworkMediaSyncIssueDetector;