|
| 1 | +import { |
| 2 | + CommonParsedInboundStreamStats, |
| 3 | + IssueDetectorResult, |
| 4 | + IssuePayload, |
| 5 | + IssueReason, |
| 6 | + IssueType, |
| 7 | + WebRTCStatsParsed, |
| 8 | +} from '../types'; |
| 9 | +import BaseIssueDetector from './BaseIssueDetector'; |
| 10 | + |
| 11 | +interface MissingStreamDetectorParams { |
| 12 | + timeoutMs?: number; // delay to report the issue no more often then once per specified timeout |
| 13 | + steps?: number; // number of last stats to check |
| 14 | +} |
| 15 | + |
| 16 | +export default class MissingStreamDataDetector extends BaseIssueDetector { |
| 17 | + readonly #lastMarkedAt = new Map<string, number>(); |
| 18 | + |
| 19 | + readonly #timeoutMs: number; |
| 20 | + |
| 21 | + readonly #steps: number; |
| 22 | + |
| 23 | + constructor(params: MissingStreamDetectorParams = {}) { |
| 24 | + super(); |
| 25 | + this.#timeoutMs = params.timeoutMs ?? 15_000; |
| 26 | + this.#steps = params.steps ?? 3; |
| 27 | + } |
| 28 | + |
| 29 | + performDetection(data: WebRTCStatsParsed): IssueDetectorResult { |
| 30 | + const { connection: { id: connectionId } } = data; |
| 31 | + const issues = this.processData(data); |
| 32 | + this.setLastProcessedStats(connectionId, data); |
| 33 | + return issues; |
| 34 | + } |
| 35 | + |
| 36 | + private processData(data: WebRTCStatsParsed): IssueDetectorResult { |
| 37 | + const issues: IssueDetectorResult = []; |
| 38 | + |
| 39 | + const allLastProcessedStats = [...this.getAllLastProcessedStats(data.connection.id), data]; |
| 40 | + if (allLastProcessedStats.length < this.#steps) { |
| 41 | + return issues; |
| 42 | + } |
| 43 | + |
| 44 | + const lastNProcessedStats = allLastProcessedStats.slice(-this.#steps); |
| 45 | + |
| 46 | + const lastNVideoInbound = lastNProcessedStats.map((stats) => stats.video.inbound); |
| 47 | + const lastNAudioInbound = lastNProcessedStats.map((stats) => stats.audio.inbound); |
| 48 | + |
| 49 | + issues.push(...this.detectMissingData( |
| 50 | + lastNAudioInbound as unknown as CommonParsedInboundStreamStats[][], |
| 51 | + IssueType.Stream, |
| 52 | + IssueReason.MissingAudioStreamData, |
| 53 | + )); |
| 54 | + |
| 55 | + issues.push(...this.detectMissingData( |
| 56 | + lastNVideoInbound, |
| 57 | + IssueType.Stream, |
| 58 | + IssueReason.MissingVideoStreamData, |
| 59 | + )); |
| 60 | + |
| 61 | + const unvisitedTrackIds = new Set(this.#lastMarkedAt.keys()); |
| 62 | + |
| 63 | + unvisitedTrackIds.forEach((trackId) => { |
| 64 | + const lastMarkedAt = this.#lastMarkedAt.get(trackId); |
| 65 | + if (lastMarkedAt && Date.now() - lastMarkedAt > this.#timeoutMs) { |
| 66 | + this.removeMarkedIssue(trackId); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + return issues; |
| 71 | + } |
| 72 | + |
| 73 | + private detectMissingData( |
| 74 | + lastNInboundStats: CommonParsedInboundStreamStats[][], |
| 75 | + type: IssueType, |
| 76 | + reason: IssueReason, |
| 77 | + ): IssueDetectorResult { |
| 78 | + const issues: IssuePayload[] = []; |
| 79 | + |
| 80 | + const currentInboundStats = lastNInboundStats.pop()!; |
| 81 | + const prevInboundItemsByTrackId = MissingStreamDataDetector.mapStatsByTrackId(lastNInboundStats); |
| 82 | + |
| 83 | + currentInboundStats.forEach((inboundItem) => { |
| 84 | + const trackId = inboundItem.track.trackIdentifier; |
| 85 | + |
| 86 | + const prevInboundItems = prevInboundItemsByTrackId.get(trackId); |
| 87 | + |
| 88 | + if (!Array.isArray(prevInboundItems) || prevInboundItems.length === 0) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (inboundItem.track.detached || inboundItem.track.ended) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if (!MissingStreamDataDetector.isAllBytesReceivedDidntChange(inboundItem.bytesReceived, prevInboundItems)) { |
| 97 | + this.removeMarkedIssue(trackId); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const issueMarked = this.markIssue(trackId); |
| 102 | + |
| 103 | + if (!issueMarked) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const statsSample = { |
| 108 | + bytesReceived: inboundItem.bytesReceived, |
| 109 | + }; |
| 110 | + |
| 111 | + issues.push({ |
| 112 | + type, |
| 113 | + reason, |
| 114 | + statsSample, |
| 115 | + trackIdentifier: trackId, |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + return issues; |
| 120 | + } |
| 121 | + |
| 122 | + private static mapStatsByTrackId( |
| 123 | + items: CommonParsedInboundStreamStats[][], |
| 124 | + ): Map<string, CommonParsedInboundStreamStats[]> { |
| 125 | + const statsById = new Map<string, CommonParsedInboundStreamStats[]>(); |
| 126 | + items.forEach((inboundItems) => { |
| 127 | + inboundItems.forEach((inbountItem) => { |
| 128 | + const accumulatedItems = statsById.get(inbountItem.track.trackIdentifier) || []; |
| 129 | + accumulatedItems.push(inbountItem); |
| 130 | + statsById.set(inbountItem.track.trackIdentifier, accumulatedItems); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + return statsById; |
| 135 | + } |
| 136 | + |
| 137 | + private static isAllBytesReceivedDidntChange( |
| 138 | + bytesReceived: number, inboundItems: CommonParsedInboundStreamStats[], |
| 139 | + ): boolean { |
| 140 | + for (let i = 0; i < inboundItems.length; i += 1) { |
| 141 | + const inboundItem = inboundItems[i]; |
| 142 | + if (inboundItem.bytesReceived !== bytesReceived) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + private markIssue(trackId: string): boolean { |
| 151 | + const now = Date.now(); |
| 152 | + const lastMarkedAt = this.#lastMarkedAt.get(trackId); |
| 153 | + |
| 154 | + if (!lastMarkedAt || now - lastMarkedAt > this.#timeoutMs) { |
| 155 | + this.#lastMarkedAt.set(trackId, now); |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + private removeMarkedIssue(trackId: string): void { |
| 163 | + this.#lastMarkedAt.delete(trackId); |
| 164 | + } |
| 165 | +} |
0 commit comments