|
| 1 | +import { |
| 2 | + IssueDetectorResult, |
| 3 | + IssueReason, |
| 4 | + IssueType, |
| 5 | + ParsedInboundVideoStreamStats, |
| 6 | + WebRTCStatsParsed, |
| 7 | +} from '../types'; |
| 8 | +import BaseIssueDetector from './BaseIssueDetector'; |
| 9 | + |
| 10 | +const sumStats = ( |
| 11 | + accessor: (stat: ParsedInboundVideoStreamStats) => number, |
| 12 | + stats: ParsedInboundVideoStreamStats[], |
| 13 | +) => stats.reduce((sum, stat) => sum + accessor(stat), 0); |
| 14 | + |
| 15 | +const sumPacketsReceived = sumStats.bind(null, (stat) => stat.packetsReceived); |
| 16 | +const sumDecodedFrames = sumStats.bind(null, (stat) => stat.framesDecoded); |
| 17 | + |
| 18 | +const hasNewInboundTraffic = (data: WebRTCStatsParsed, prevData: WebRTCStatsParsed): boolean => { |
| 19 | + const { video: { inbound: newInbound } } = data; |
| 20 | + const { video: { inbound: prevInbound } } = prevData; |
| 21 | + |
| 22 | + return sumPacketsReceived(newInbound) > sumPacketsReceived(prevInbound); |
| 23 | +}; |
| 24 | + |
| 25 | +const hasNewDecodedFrames = (data: WebRTCStatsParsed, prevData: WebRTCStatsParsed): boolean => { |
| 26 | + const { video: { inbound: newInbound } } = data; |
| 27 | + const { video: { inbound: prevInbound } } = prevData; |
| 28 | + |
| 29 | + return sumDecodedFrames(newInbound) > sumDecodedFrames(prevInbound); |
| 30 | +}; |
| 31 | + |
| 32 | +class DeadVideoTrackDetector extends BaseIssueDetector { |
| 33 | + #lastMarkedAt: number | undefined; |
| 34 | + |
| 35 | + #timeoutMs: number; |
| 36 | + |
| 37 | + constructor(params: { timeoutMs?: number } = {}) { |
| 38 | + super(); |
| 39 | + this.#timeoutMs = params.timeoutMs ?? 10_000; |
| 40 | + } |
| 41 | + |
| 42 | + performDetection(data: WebRTCStatsParsed): IssueDetectorResult { |
| 43 | + const { connection: { id: connectionId } } = data; |
| 44 | + const issues = this.processData(data); |
| 45 | + this.setLastProcessedStats(connectionId, data); |
| 46 | + return issues; |
| 47 | + } |
| 48 | + |
| 49 | + private processData(data: WebRTCStatsParsed): IssueDetectorResult { |
| 50 | + const { connection: { id: connectionId } } = data; |
| 51 | + const previousStats = this.getLastProcessedStats(connectionId); |
| 52 | + const issues: IssueDetectorResult = []; |
| 53 | + |
| 54 | + if (!previousStats) { |
| 55 | + return issues; |
| 56 | + } |
| 57 | + |
| 58 | + if (hasNewInboundTraffic(data, previousStats)) { |
| 59 | + if (hasNewDecodedFrames(data, previousStats)) { |
| 60 | + this.removeMarkIssue(); |
| 61 | + } else { |
| 62 | + const hasIssue = this.markIssue(); |
| 63 | + |
| 64 | + if (hasIssue) { |
| 65 | + const statsSample = { |
| 66 | + packetsReceived: sumPacketsReceived(data.video.inbound), |
| 67 | + framesDecoded: sumDecodedFrames(data.video.inbound), |
| 68 | + deltaFramesDecoded: sumDecodedFrames(data.video.inbound) - sumDecodedFrames(previousStats.video.inbound), |
| 69 | + deltaPacketsReceived: |
| 70 | + sumPacketsReceived(data.video.inbound) - sumPacketsReceived(previousStats.video.inbound), |
| 71 | + }; |
| 72 | + |
| 73 | + issues.push({ |
| 74 | + statsSample, |
| 75 | + type: IssueType.Stream, |
| 76 | + reason: IssueReason.DeadVideoTrack, |
| 77 | + iceCandidate: data.connection.local.id, |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return issues; |
| 84 | + } |
| 85 | + |
| 86 | + private markIssue(): boolean { |
| 87 | + const now = Date.now(); |
| 88 | + |
| 89 | + if (!this.#lastMarkedAt) { |
| 90 | + this.#lastMarkedAt = now; |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + if (now - this.#lastMarkedAt < this.#timeoutMs) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + private removeMarkIssue(): void { |
| 102 | + this.#lastMarkedAt = undefined; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +export default DeadVideoTrackDetector; |
0 commit comments