Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webrtc-issue-detector

Package Overview
Dependencies
Maintainers
0
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webrtc-issue-detector - npm Package Compare versions

Comparing version 1.12.0-LD-6968-dead-video-track-detect.1 to 1.12.0-LD-6968-dead-video-track-detect.2

dist/detectors/FrozenVideoTrackDetector.d.ts

2

dist/bundle-cjs.js

@@ -1,1 +0,1 @@

"use strict";var e,t,s;function o(){}function r(){r.init.call(this)}function n(e){return void 0===e._maxListeners?r.defaultMaxListeners:e._maxListeners}function i(e,t,s){if(t)e.call(s);else for(var o=e.length,r=m(e,o),n=0;n<o;++n)r[n].call(s)}function a(e,t,s,o){if(t)e.call(s,o);else for(var r=e.length,n=m(e,r),i=0;i<r;++i)n[i].call(s,o)}function c(e,t,s,o,r){if(t)e.call(s,o,r);else for(var n=e.length,i=m(e,n),a=0;a<n;++a)i[a].call(s,o,r)}function d(e,t,s,o,r,n){if(t)e.call(s,o,r,n);else for(var i=e.length,a=m(e,i),c=0;c<i;++c)a[c].call(s,o,r,n)}function u(e,t,s,o){if(t)e.apply(s,o);else for(var r=e.length,n=m(e,r),i=0;i<r;++i)n[i].apply(s,o)}function p(e,t,s,r){var i,a,c,d;if("function"!=typeof s)throw new TypeError('"listener" argument must be a function');if((a=e._events)?(a.newListener&&(e.emit("newListener",t,s.listener?s.listener:s),a=e._events),c=a[t]):(a=e._events=new o,e._eventsCount=0),c){if("function"==typeof c?c=a[t]=r?[s,c]:[c,s]:r?c.unshift(s):c.push(s),!c.warned&&(i=n(e))&&i>0&&c.length>i){c.warned=!0;var u=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");u.name="MaxListenersExceededWarning",u.emitter=e,u.type=t,u.count=c.length,d=u,"function"==typeof console.warn?console.warn(d):console.log(d)}}else c=a[t]=s,++e._eventsCount;return e}function h(e,t,s){var o=!1;function r(){e.removeListener(t,r),o||(o=!0,s.apply(e,arguments))}return r.listener=s,r}function l(e){var t=this._events;if(t){var s=t[e];if("function"==typeof s)return 1;if(s)return s.length}return 0}function m(e,t){for(var s=new Array(t);t--;)s[t]=e[t];return s}Object.defineProperty(exports,"__esModule",{value:!0}),o.prototype=Object.create(null),r.EventEmitter=r,r.usingDomains=!1,r.prototype.domain=void 0,r.prototype._events=void 0,r.prototype._maxListeners=void 0,r.defaultMaxListeners=10,r.init=function(){this.domain=null,r.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new o,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},r.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},r.prototype.getMaxListeners=function(){return n(this)},r.prototype.emit=function(e){var t,s,o,r,n,p,h,l="error"===e;if(p=this._events)l=l&&null==p.error;else if(!l)return!1;if(h=this.domain,l){if(t=arguments[1],!h){if(t instanceof Error)throw t;var m=new Error('Uncaught, unspecified "error" event. ('+t+")");throw m.context=t,m}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=h,t.domainThrown=!1,h.emit("error",t),!1}if(!(s=p[e]))return!1;var f="function"==typeof s;switch(o=arguments.length){case 1:i(s,f,this);break;case 2:a(s,f,this,arguments[1]);break;case 3:c(s,f,this,arguments[1],arguments[2]);break;case 4:d(s,f,this,arguments[1],arguments[2],arguments[3]);break;default:for(r=new Array(o-1),n=1;n<o;n++)r[n-1]=arguments[n];u(s,f,this,r)}return!0},r.prototype.addListener=function(e,t){return p(this,e,t,!1)},r.prototype.on=r.prototype.addListener,r.prototype.prependListener=function(e,t){return p(this,e,t,!0)},r.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,h(this,e,t)),this},r.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,h(this,e,t)),this},r.prototype.removeListener=function(e,t){var s,r,n,i,a;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(r=this._events))return this;if(!(s=r[e]))return this;if(s===t||s.listener&&s.listener===t)0==--this._eventsCount?this._events=new o:(delete r[e],r.removeListener&&this.emit("removeListener",e,s.listener||t));else if("function"!=typeof s){for(n=-1,i=s.length;i-- >0;)if(s[i]===t||s[i].listener&&s[i].listener===t){a=s[i].listener,n=i;break}if(n<0)return this;if(1===s.length){if(s[0]=void 0,0==--this._eventsCount)return this._events=new o,this;delete r[e]}else!function(e,t){for(var s=t,o=s+1,r=e.length;o<r;s+=1,o+=1)e[s]=e[o];e.pop()}(s,n);r.removeListener&&this.emit("removeListener",e,a||t)}return this},r.prototype.off=function(e,t){return this.removeListener(e,t)},r.prototype.removeAllListeners=function(e){var t,s;if(!(s=this._events))return this;if(!s.removeListener)return 0===arguments.length?(this._events=new o,this._eventsCount=0):s[e]&&(0==--this._eventsCount?this._events=new o:delete s[e]),this;if(0===arguments.length){for(var r,n=Object.keys(s),i=0;i<n.length;++i)"removeListener"!==(r=n[i])&&this.removeAllListeners(r);return this.removeAllListeners("removeListener"),this._events=new o,this._eventsCount=0,this}if("function"==typeof(t=s[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},r.prototype.listeners=function(e){var t,s=this._events;return s&&(t=s[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),s=0;s<t.length;++s)t[s]=e[s].listener||e[s];return t}(t):[]},r.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):l.call(e,t)},r.prototype.listenerCount=l,r.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class f extends r{}exports.EventType=void 0,(e=exports.EventType||(exports.EventType={})).Issue="issue",e.NetworkScoresUpdated="network-scores-updated",e.StatsParsingFinished="stats-parsing-finished",exports.IssueType=void 0,(t=exports.IssueType||(exports.IssueType={})).Network="network",t.CPU="cpu",t.Server="server",t.Stream="stream",exports.IssueReason=void 0,(s=exports.IssueReason||(exports.IssueReason={})).OutboundNetworkQuality="outbound-network-quality",s.InboundNetworkQuality="inbound-network-quality",s.OutboundNetworkMediaLatency="outbound-network-media-latency",s.InboundNetworkMediaLatency="inbound-network-media-latency",s.NetworkMediaSyncFailure="network-media-sync-failure",s.OutboundNetworkThroughput="outbound-network-throughput",s.InboundNetworkThroughput="inbound-network-throughput",s.EncoderCPUThrottling="encoder-cpu-throttling",s.DecoderCPUThrottling="decoder-cpu-throttling",s.ServerIssue="server-issue",s.UnknownVideoDecoderIssue="unknown-video-decoder",s.LowInboundMOS="low-inbound-mean-opinion-score",s.LowOutboundMOS="low-outbound-mean-opinion-score",s.DeadVideoTrack="dead-video-track";class g extends r{static STATS_REPORT_READY_EVENT="stats-report-ready";static STATS_REPORTS_PARSED="stats-reports-parsed";isStopped=!1;reportTimer;getStatsInterval;compositeStatsParser;constructor(e){super(),this.compositeStatsParser=e.compositeStatsParser,this.getStatsInterval=e.getStatsInterval??1e4}get isRunning(){return!!this.reportTimer&&!this.isStopped}startReporting(){if(this.reportTimer)return;const e=()=>setTimeout((()=>{this.isStopped?this.reportTimer=void 0:this.parseReports().finally((()=>{this.reportTimer=e()}))}),this.getStatsInterval);this.isStopped=!1,this.reportTimer=e()}stopReporting(){this.isStopped=!0,this.reportTimer&&(clearTimeout(this.reportTimer),this.reportTimer=void 0)}async parseReports(){const e=Date.now(),t=await this.compositeStatsParser.parse(),s=Date.now()-e;this.emit(g.STATS_REPORTS_PARSED,{timeTaken:s}),t.forEach((e=>{this.emit(g.STATS_REPORT_READY_EVENT,e)}))}}const S=(()=>{const e=new Map;return t=>{const{taskId:s,delayMs:o,maxJitterMs:r,callback:n}=t,i=Math.ceil(Math.random()*(r||0)),a=e.get(s);a&&clearTimeout(a);const c=setTimeout((()=>{n(),e.delete(s)}),o+i);e.set(s,c)}})();class v{#e={};calculate(e){const{connection:{id:t}}=e,{mos:s,stats:o}=this.calculateOutboundScore(e)||{},{mos:r,stats:n}=this.calculateInboundScore(e)||{};return this.#e[t]=e,S({taskId:t,delayMs:35e3,callback:()=>delete this.#e[t]}),{outbound:s,inbound:r,connectionId:t,statsSamples:{inboundStatsSample:n,outboundStatsSample:o}}}calculateOutboundScore(e){const t=[...e.remote?.audio.inbound||[],...e.remote?.video.inbound||[]];if(!t.length)return;const s=this.#e[e.connection.id];if(!s)return;const o=[...s.remote?.audio.inbound||[],...s.remote?.video.inbound||[]],{packetsSent:r}=e.connection,n=s.connection.packetsSent,i=t.reduce(((e,t)=>{const s=o.find((e=>e.ssrc===t.ssrc));return{sumJitter:e.sumJitter+t.jitter,packetsLost:e.packetsLost+t.packetsLost,lastPacketsLost:e.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),a=1e3*e.connection.currentRoundTripTime||0,{sumJitter:c}=i,d=c/t.length,u=r-n,p=i.packetsLost-i.lastPacketsLost,h=u&&p?Math.round(100*p/(u+p)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:h}),stats:{avgJitter:d,rtt:a,packetsLoss:h}}}calculateInboundScore(e){const t=[...e.audio?.inbound,...e.video?.inbound];if(!t.length)return;const s=this.#e[e.connection.id];if(!s)return;const o=[...s.video?.inbound,...s.audio?.inbound],{packetsReceived:r}=e.connection,n=s.connection.packetsReceived,i=t.reduce(((e,t)=>{const s=o.find((e=>e.ssrc===t.ssrc));return{sumJitter:e.sumJitter+t.jitter,packetsLost:e.packetsLost+t.packetsLost,lastPacketsLost:e.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),a=1e3*e.connection.currentRoundTripTime||0,{sumJitter:c}=i,d=c/t.length,u=r-n,p=i.packetsLost-i.lastPacketsLost,h=u&&p?Math.round(100*p/(u+p)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:h}),stats:{avgJitter:d,rtt:a,packetsLoss:h}}}calculateMOS({avgJitter:e,rtt:t,packetsLoss:s}){const o=t+2*e+10;let r=o<160?93.2-o/40:93.2-o/120-10;return r-=2.5*s,1+.035*r+7e-6*r*(r-60)*(100-r)}}class k{#e;#t;constructor(e={}){this.#e=new Map,this.#t=e.statsCleanupTtlMs??35e3}detect(e){const t=this.performDetection(e);return this.performPrevStatsCleanup({connectionId:e.connection.id}),t}performPrevStatsCleanup(e){const{connectionId:t,cleanupCallback:s}=e;this.#e.has(t)&&S({taskId:t,delayMs:this.#t,callback:()=>{this.deleteLastProcessedStats(t),"function"==typeof s&&s()}})}setLastProcessedStats(e,t){this.#e.set(e,t)}getLastProcessedStats(e){return this.#e.get(e)}deleteLastProcessedStats(e){this.#e.delete(e)}}class y extends k{#s;constructor(e={}){super(e),this.#s=e.availableOutgoingBitrateThreshold??1e5}performDetection(e){const t=[],{availableOutgoingBitrate:s}=e.connection;if(void 0===s)return t;const o=e.audio.outbound.reduce(((e,t)=>e+t.targetBitrate),0),r=e.video.outbound.reduce(((e,t)=>e+t.bitrate),0);if(!o&&!r)return t;const n={availableOutgoingBitrate:s,videoStreamsTotalBitrate:r,audioStreamsTotalTargetBitrate:o};return o>s||r>0&&s<this.#s?(t.push({statsSample:n,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput}),t):t}}class w extends k{#o;constructor(e={}){super(e),this.#o=e.framesDroppedThreshold??.5}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=e.video.inbound.filter((e=>e.framesDropped>0)),s=[],o=this.getLastProcessedStats(e.connection.id)?.video.inbound;return o?(t.forEach((e=>{const t=o.find((t=>t.ssrc===e.ssrc));if(!t)return;if(e.framesDropped===t.framesDropped)return;const r=e.framesReceived-t.framesReceived,n=e.framesDecoded-t.framesDecoded,i=e.framesDropped-t.framesDropped,a=i/r;if(0===r||0===n)return;const c={deltaFramesDropped:i,deltaFramesReceived:r,deltaFramesDecoded:n,framesDroppedPct:Math.round(100*a)};a>=this.#o&&s.push({statsSample:c,type:exports.IssueType.CPU,reason:exports.IssueReason.DecoderCPUThrottling,ssrc:e.ssrc})})),s):s}}class T extends k{#r;constructor(e={}){super(e),this.#r=e.missedFramesThreshold??.15}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=e.video.outbound.filter((e=>e.framesEncoded>0)),s=[],o=this.getLastProcessedStats(e.connection.id)?.video.outbound;return o?(t.forEach((e=>{const t=o.find((t=>t.ssrc===e.ssrc));if(!t)return;if(e.framesEncoded===t.framesEncoded)return;const r=e.framesEncoded-t.framesEncoded,n=e.framesSent-t.framesSent,i=1-n/r;if(0===r)return;if(r===n)return;const a={deltaFramesSent:n,deltaFramesEncoded:r,missedFramesPct:Math.round(100*i)};i>=this.#r&&s.push({statsSample:a,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput,ssrc:e.ssrc})})),s):s}}class b extends k{#n;#i;#a;#c;constructor(e={}){super(),this.#n=e.highPacketLossThresholdPct??5,this.#i=e.highJitterThreshold??200,this.#a=e.highJitterBufferDelayThresholdMs??500,this.#c=e.highRttThresholdMs??250}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=[],s=[...e.audio?.inbound,...e.video?.inbound];if(!s.length)return t;const o=this.getLastProcessedStats(e.connection.id);if(!o)return t;const r=[...o.video?.inbound,...o.audio?.inbound],{packetsReceived:n}=e.connection,i=o.connection.packetsReceived,a=s.reduce(((e,t)=>{const s=r.find((e=>e.ssrc===t.ssrc)),o=s?.jitterBufferDelay||0,n=s?.jitterBufferEmittedCount||0,i=t.jitterBufferDelay-o,a=t.jitterBufferEmittedCount-n,c=i&&a?1e3*i/a:0;return{sumJitter:e.sumJitter+t.jitter,sumJitterBufferDelayMs:e.sumJitterBufferDelayMs+c,packetsLost:e.packetsLost+t.packetsLost,lastPacketsLost:e.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,sumJitterBufferDelayMs:0,packetsLost:0,lastPacketsLost:0}),c=1e3*e.connection.currentRoundTripTime||0,{sumJitter:d,sumJitterBufferDelayMs:u}=a,p=d/s.length,h=u/s.length,l=n-i,m=a.packetsLost-a.lastPacketsLost,f=l&&m?Math.round(100*m/(l+m)):0,g=f>this.#n,S=p>=this.#i,v=c>=this.#c,k=h>this.#a,y=v&&!S&&!g,w=g&&S,T=S&&k,b={rtt:c,packetLossPct:f,avgJitter:p,avgJitterBufferDelay:h};return(S||g)&&t.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.InboundNetworkQuality,iceCandidate:e.connection.local.id}),y&&t.push({statsSample:b,type:exports.IssueType.Server,reason:exports.IssueReason.ServerIssue,iceCandidate:e.connection.remote.id}),w&&t.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.InboundNetworkMediaLatency,iceCandidate:e.connection.local.id}),T&&t.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.NetworkMediaSyncFailure,iceCandidate:e.connection.local.id}),t}}class P extends k{#d;constructor(e={}){super(),this.#d=e.correctedSamplesThresholdPct??5}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=e.audio.inbound,s=[],o=this.getLastProcessedStats(e.connection.id)?.audio.inbound;return o?(t.forEach((e=>{const t=o.find((t=>t.ssrc===e.ssrc));if(!t)return;const r=e.track.insertedSamplesForDeceleration+e.track.removedSamplesForAcceleration,n=t.track.insertedSamplesForDeceleration+t.track.removedSamplesForAcceleration;if(r===n)return;const i=e.track.totalSamplesReceived-t.track.totalSamplesReceived,a=r-n,c=Math.round(100*a/i),d={correctedSamplesPct:c};c>this.#d&&s.push({statsSample:d,type:exports.IssueType.Network,reason:exports.IssueReason.NetworkMediaSyncFailure,ssrc:e.ssrc})})),s):s}}class L extends k{#n;#i;constructor(e={}){super(),this.#n=e.highPacketLossThresholdPct??5,this.#i=e.highJitterThreshold??200}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=[],s=[...e.remote?.audio.inbound||[],...e.remote?.video.inbound||[]];if(!s.length)return t;const o=this.getLastProcessedStats(e.connection.id);if(!o)return t;const r=[...o.remote?.audio.inbound||[],...o.remote?.video.inbound||[]],{packetsSent:n}=e.connection,i=o.connection.packetsSent,a=s.reduce(((e,t)=>{const s=r.find((e=>e.ssrc===t.ssrc));return{sumJitter:e.sumJitter+t.jitter,packetsLost:e.packetsLost+t.packetsLost,lastPacketsLost:e.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),c=1e3*e.connection.currentRoundTripTime||0,{sumJitter:d}=a,u=d/s.length,p=n-i,h=a.packetsLost-a.lastPacketsLost,l=p&&h?Math.round(100*h/(p+h)):0,m=l>this.#n,f=u>=this.#i,g=!m&&f||f||m,S={rtt:c,avgJitter:u,packetLossPct:l};return m&&f&&t.push({statsSample:S,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkMediaLatency,iceCandidate:e.connection.local.id}),g&&t.push({statsSample:S,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkQuality,iceCandidate:e.connection.local.id}),t}}class I extends k{performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=e.video.outbound.filter((e=>"none"!==e.qualityLimitationReason)),s=[],o=this.getLastProcessedStats(e.connection.id)?.video.outbound;return o?(t.forEach((e=>{const t=o.find((t=>t.ssrc===e.ssrc));if(!t)return;const r={qualityLimitationReason:e.qualityLimitationReason};e.framesSent>t.framesSent||("cpu"===e.qualityLimitationReason&&s.push({statsSample:r,type:exports.IssueType.CPU,reason:exports.IssueReason.EncoderCPUThrottling,ssrc:e.ssrc}),"bandwidth"===e.qualityLimitationReason&&s.push({statsSample:r,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput,ssrc:e.ssrc}))})),s):s}}class D extends k{UNKNOWN_DECODER="unknown";#u={};performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}performPrevStatsCleanup(e){const{connectionId:t,cleanupCallback:s}=e;super.performPrevStatsCleanup({...e,cleanupCallback:()=>{delete this.#u[t],"function"==typeof s&&s()}})}processData(e){const t=[],{id:s}=e.connection,o=this.getLastProcessedStats(s)?.video.inbound;return e.video.inbound.forEach((e=>{const{decoderImplementation:r,ssrc:n}=e,i=o?.find((e=>e.ssrc===n));if(i)if(r===this.UNKNOWN_DECODER){if(!this.hadLastDecoderWithIssue(s,n)){this.setLastDecoderWithIssue(s,n,this.UNKNOWN_DECODER);const o={mimeType:e.mimeType,decoderImplementation:r};t.push({ssrc:n,statsSample:o,type:exports.IssueType.Stream,reason:exports.IssueReason.UnknownVideoDecoderIssue,trackIdentifier:e.track.trackIdentifier})}}else this.setLastDecoderWithIssue(s,n,void 0)})),t}setLastDecoderWithIssue(e,t,s){const o=this.#u[e]??{};void 0===s?delete o[t]:o[t]=s,this.#u[e]=o}hadLastDecoderWithIssue(e,t){const s=this.#u[e];return(s&&s[t])===this.UNKNOWN_DECODER}}class R extends k{#p;#h;constructor(e={}){super(),this.#h=e.timeoutMs??1e4}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const{connection:{id:t}}=e,s=this.getLastProcessedStats(t),o=[];if(!s)return o;const{video:{inbound:r}}=e,{video:{inbound:n}}=s,i=e=>new Map(e.map((e=>[e.track.trackIdentifier,e]))),a=i(r),c=i(n);return Array.from(a.entries()).forEach((([e,t])=>{const s=c.get(e);if(s&&t.packetsReceived>s.packetsReceived)if(t.framesDecoded>s.framesDecoded)this.removeMarkIssue();else{if(this.markIssue()){const r={packetsReceived:t.packetsReceived,framesDecoded:t.framesDecoded,deltaFramesDecoded:t.framesDecoded-s.framesDecoded,deltaPacketsReceived:t.packetsReceived-s.packetsReceived};o.push({statsSample:r,type:exports.IssueType.Stream,reason:exports.IssueReason.DeadVideoTrack,iceCandidate:e})}}})),o}markIssue(){const e=Date.now();return this.#p?!(e-this.#p<this.#h):(this.#p=e,!1)}removeMarkIssue(){this.#p=void 0}}const C=e=>"closed"===e.iceConnectionState||"closed"===e.connectionState,x=(e,t,s)=>8*((e,t,s)=>{if(!t)return 0;const o=e[s],r=t[s];if(null==o||null==r)return 0;const n=Math.floor(e.timestamp)-Math.floor(t.timestamp);return 0===n?0:(Number(o)-Number(r))/n*1e3})(e,t,s);class E{connections=[];statsParser;constructor(e){this.statsParser=e.statsParser}listConnections(){return[...this.connections]}addPeerConnection(e){this.connections.push({id:e.id??String(Date.now()+Math.random().toString(32)),pc:e.pc})}removePeerConnection(e){const t=this.connections.findIndex((({pc:t})=>t===e.pc));t>=0&&this.removeConnectionsByIndexes([t])}async parse(){const e=[],t=this.connections.map((async(t,s)=>{if(!C(t.pc))return this.statsParser.parse(t);e.unshift(s)}));e.length&&this.removeConnectionsByIndexes(e);return(await Promise.all(t)).filter((e=>void 0!==e))}removeConnectionsByIndexes(e){e.forEach((e=>{this.connections.splice(e,1)}))}}class M{prevStats=new Map;allowedReportTypes=new Set(["candidate-pair","inbound-rtp","outbound-rtp","remote-outbound-rtp","remote-inbound-rtp","track","transport"]);ignoreSSRCList;logger;constructor(e){this.ignoreSSRCList=e.ignoreSSRCList??[],this.logger=e.logger}get previouslyParsedStatsConnectionsIds(){return[...this.prevStats.keys()]}async parse(e){if(!C(e.pc))return this.getConnectionStats(e);this.logger.debug("Skip stats parsing. Connection is closed.",{connection:e})}async getConnectionStats(e){const{pc:t,id:s}=e;try{const o=Date.now(),r=t.getReceivers().filter((e=>e.track?.enabled)),n=t.getSenders().filter((e=>e.track?.enabled)),i=await Promise.all(r.map((e=>e.getStats()))),a=await Promise.all(n.map((e=>e.getStats())));return{id:s,stats:this.mapReportsStats([...i,...a],e),timeTaken:Date.now()-o}}catch(e){return void this.logger.error("Failed to get stats for PC",{id:s,pc:t,error:e})}}mapReportsStats(e,t){const s={audio:{inbound:[],outbound:[]},video:{inbound:[],outbound:[]},connection:{},remote:{video:{inbound:[],outbound:[]},audio:{inbound:[],outbound:[]}}};e.forEach((e=>{e.forEach((t=>{this.allowedReportTypes.has(t.type)&&this.updateMappedStatsWithReportItemData(t,s,e)}))}));const{id:o}=t,r=this.prevStats.get(o);return r&&this.propagateStatsWithRateValues(s,r.stats),this.prevStats.set(o,{stats:s,ts:Date.now()}),S({taskId:o,delayMs:35e3,callback:()=>this.prevStats.delete(o)}),s}updateMappedStatsWithReportItemData(e,t,s){const o=e.type;if("candidate-pair"===o&&"succeeded"===e.state&&e.nominated)return void(t.connection=this.prepareConnectionStats(e,s));const r=this.getMediaType(e);if(!r)return;const n=e.ssrc;if(!n||!this.ignoreSSRCList.includes(n))if("outbound-rtp"!==o)if("inbound-rtp"!==o)"remote-outbound-rtp"!==o?"remote-inbound-rtp"===o&&(this.mapConnectionStatsIfNecessary(t,e,s),t.remote[r].inbound.push({...e})):t.remote[r].outbound.push({...e});else{const o=s.get(e.trackId)||s.get(e.mediaSourceId)||{};this.mapConnectionStatsIfNecessary(t,e,s);const n={...e,track:{...o}};t[r].inbound.push(n)}else{const o=s.get(e.trackId)||s.get(e.mediaSourceId)||{},n={...e,track:{...o}};t[r].outbound.push(n)}}getMediaType(e){const t=e.mediaType||e.kind;if(!["audio","video"].includes(t)){const{id:t}=e;if(!t)return;return String(t).includes("Video")?"video":String(t).includes("Audio")?"audio":void 0}return t}propagateStatsWithRateValues(e,t){e.audio.inbound.forEach((e=>{const s=t.audio.inbound.find((({id:t})=>t===e.id));e.bitrate=x(e,s,"bytesReceived"),e.packetRate=x(e,s,"packetsReceived")})),e.audio.outbound.forEach((e=>{const s=t.audio.outbound.find((({id:t})=>t===e.id));e.bitrate=x(e,s,"bytesSent"),e.packetRate=x(e,s,"packetsSent")})),e.video.inbound.forEach((e=>{const s=t.video.inbound.find((({id:t})=>t===e.id));e.bitrate=x(e,s,"bytesReceived"),e.packetRate=x(e,s,"packetsReceived")})),e.video.outbound.forEach((e=>{const s=t.video.outbound.find((({id:t})=>t===e.id));e.bitrate=x(e,s,"bytesSent"),e.packetRate=x(e,s,"packetsSent")}))}mapConnectionStatsIfNecessary(e,t,s){if(e.connection.id||!t.transportId)return;const o=s.get(t.transportId);if(o&&o.selectedCandidatePairId){const t=s.get(o.selectedCandidatePairId);e.connection=this.prepareConnectionStats(t,s)}}prepareConnectionStats(e,t){if(!e||!t)return{};const s={...e};if(s.remoteCandidateId){const e=t.get(s.remoteCandidateId);s.remote={...e}}if(s.localCandidateId){const e=t.get(s.localCandidateId);s.local={...e}}return s}}exports.AvailableOutgoingBitrateIssueDetector=y,exports.BaseIssueDetector=k,exports.CompositeRTCStatsParser=E,exports.DeadVideoTrackDetector=R,exports.FramesDroppedIssueDetector=w,exports.FramesEncodedSentIssueDetector=T,exports.InboundNetworkIssueDetector=b,exports.NetworkMediaSyncIssueDetector=P,exports.NetworkScoresCalculator=v,exports.OutboundNetworkIssueDetector=L,exports.PeriodicWebRTCStatsReporter=g,exports.QualityLimitationsIssueDetector=I,exports.RTCStatsParser=M,exports.UnknownVideoDecoderImplementationDetector=D,exports.WebRTCIssueEmitter=f,exports.default=class{eventEmitter;#l=!1;detectors=[];networkScoresCalculator;statsReporter;compositeStatsParser;logger;autoAddPeerConnections;constructor(e){this.logger=e.logger??{debug:()=>{},info:()=>{},warn:()=>{},error:()=>{}},this.eventEmitter=e.issueEmitter??new f,e.onIssues&&this.eventEmitter.on(exports.EventType.Issue,e.onIssues),e.onNetworkScoresUpdated&&this.eventEmitter.on(exports.EventType.NetworkScoresUpdated,e.onNetworkScoresUpdated),this.detectors=e.detectors??[new I,new w,new T,new b,new L,new P,new y,new D,new R],this.networkScoresCalculator=e.networkScoresCalculator??new v,this.compositeStatsParser=e.compositeStatsParser??new E({statsParser:new M({ignoreSSRCList:e.ignoreSSRCList,logger:this.logger})}),this.statsReporter=e.statsReporter??new g({compositeStatsParser:this.compositeStatsParser,getStatsInterval:e.getStatsInterval??5e3}),window.wid=this,this.autoAddPeerConnections=e.autoAddPeerConnections??!0,this.autoAddPeerConnections&&this.wrapRTCPeerConnection(),this.statsReporter.on(g.STATS_REPORT_READY_EVENT,(e=>{this.detectIssues({data:e.stats}),this.calculateNetworkScores(e.stats)})),this.statsReporter.on(g.STATS_REPORTS_PARSED,(e=>{const t={timeTaken:e.timeTaken,ts:Date.now()};this.eventEmitter.emit(exports.EventType.StatsParsingFinished,t)}))}watchNewPeerConnections(){if(!this.autoAddPeerConnections)throw new Error("Auto add peer connections was disabled in the constructor.");this.#l?this.logger.warn("WebRTCIssueDetector is already started. Skip processing"):(this.logger.info("Start watching peer connections"),this.#l=!0,this.statsReporter.startReporting())}stopWatchingNewPeerConnections(){this.#l?(this.logger.info("Stop watching peer connections"),this.#l=!1,this.statsReporter.stopReporting()):this.logger.warn("WebRTCIssueDetector is already stopped. Skip processing")}handleNewPeerConnection(e){this.#l||!this.autoAddPeerConnections?(this.#l||!1!==this.autoAddPeerConnections||(this.logger.info("Starting stats reporting for new peer connection"),this.#l=!0,this.statsReporter.startReporting()),this.logger.debug("Handling new peer connection",e),this.compositeStatsParser.addPeerConnection({pc:e})):this.logger.debug("Skip handling new peer connection. Detector is not running",e)}emitIssues(e){this.eventEmitter.emit(exports.EventType.Issue,e)}detectIssues({data:e}){const t=this.detectors.reduce(((t,s)=>[...t,...s.detect(e)]),[]);t.length>0&&this.emitIssues(t)}calculateNetworkScores(e){const t=this.networkScoresCalculator.calculate(e);this.eventEmitter.emit(exports.EventType.NetworkScoresUpdated,t)}wrapRTCPeerConnection(){if(!window.RTCPeerConnection)return void this.logger.warn("No RTCPeerConnection found in browser window. Skipping");const e=window.RTCPeerConnection,t=e=>this.handleNewPeerConnection(e);function s(s){const o=new e(s);return t(o),o}s.prototype=e.prototype,window.RTCPeerConnection=s}};
"use strict";var e,t,s;function o(){}function r(){r.init.call(this)}function n(e){return void 0===e._maxListeners?r.defaultMaxListeners:e._maxListeners}function i(e,t,s){if(t)e.call(s);else for(var o=e.length,r=m(e,o),n=0;n<o;++n)r[n].call(s)}function a(e,t,s,o){if(t)e.call(s,o);else for(var r=e.length,n=m(e,r),i=0;i<r;++i)n[i].call(s,o)}function c(e,t,s,o,r){if(t)e.call(s,o,r);else for(var n=e.length,i=m(e,n),a=0;a<n;++a)i[a].call(s,o,r)}function d(e,t,s,o,r,n){if(t)e.call(s,o,r,n);else for(var i=e.length,a=m(e,i),c=0;c<i;++c)a[c].call(s,o,r,n)}function u(e,t,s,o){if(t)e.apply(s,o);else for(var r=e.length,n=m(e,r),i=0;i<r;++i)n[i].apply(s,o)}function p(e,t,s,r){var i,a,c,d;if("function"!=typeof s)throw new TypeError('"listener" argument must be a function');if((a=e._events)?(a.newListener&&(e.emit("newListener",t,s.listener?s.listener:s),a=e._events),c=a[t]):(a=e._events=new o,e._eventsCount=0),c){if("function"==typeof c?c=a[t]=r?[s,c]:[c,s]:r?c.unshift(s):c.push(s),!c.warned&&(i=n(e))&&i>0&&c.length>i){c.warned=!0;var u=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");u.name="MaxListenersExceededWarning",u.emitter=e,u.type=t,u.count=c.length,d=u,"function"==typeof console.warn?console.warn(d):console.log(d)}}else c=a[t]=s,++e._eventsCount;return e}function h(e,t,s){var o=!1;function r(){e.removeListener(t,r),o||(o=!0,s.apply(e,arguments))}return r.listener=s,r}function l(e){var t=this._events;if(t){var s=t[e];if("function"==typeof s)return 1;if(s)return s.length}return 0}function m(e,t){for(var s=new Array(t);t--;)s[t]=e[t];return s}Object.defineProperty(exports,"__esModule",{value:!0}),o.prototype=Object.create(null),r.EventEmitter=r,r.usingDomains=!1,r.prototype.domain=void 0,r.prototype._events=void 0,r.prototype._maxListeners=void 0,r.defaultMaxListeners=10,r.init=function(){this.domain=null,r.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new o,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},r.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},r.prototype.getMaxListeners=function(){return n(this)},r.prototype.emit=function(e){var t,s,o,r,n,p,h,l="error"===e;if(p=this._events)l=l&&null==p.error;else if(!l)return!1;if(h=this.domain,l){if(t=arguments[1],!h){if(t instanceof Error)throw t;var m=new Error('Uncaught, unspecified "error" event. ('+t+")");throw m.context=t,m}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=h,t.domainThrown=!1,h.emit("error",t),!1}if(!(s=p[e]))return!1;var f="function"==typeof s;switch(o=arguments.length){case 1:i(s,f,this);break;case 2:a(s,f,this,arguments[1]);break;case 3:c(s,f,this,arguments[1],arguments[2]);break;case 4:d(s,f,this,arguments[1],arguments[2],arguments[3]);break;default:for(r=new Array(o-1),n=1;n<o;n++)r[n-1]=arguments[n];u(s,f,this,r)}return!0},r.prototype.addListener=function(e,t){return p(this,e,t,!1)},r.prototype.on=r.prototype.addListener,r.prototype.prependListener=function(e,t){return p(this,e,t,!0)},r.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,h(this,e,t)),this},r.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,h(this,e,t)),this},r.prototype.removeListener=function(e,t){var s,r,n,i,a;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(r=this._events))return this;if(!(s=r[e]))return this;if(s===t||s.listener&&s.listener===t)0==--this._eventsCount?this._events=new o:(delete r[e],r.removeListener&&this.emit("removeListener",e,s.listener||t));else if("function"!=typeof s){for(n=-1,i=s.length;i-- >0;)if(s[i]===t||s[i].listener&&s[i].listener===t){a=s[i].listener,n=i;break}if(n<0)return this;if(1===s.length){if(s[0]=void 0,0==--this._eventsCount)return this._events=new o,this;delete r[e]}else!function(e,t){for(var s=t,o=s+1,r=e.length;o<r;s+=1,o+=1)e[s]=e[o];e.pop()}(s,n);r.removeListener&&this.emit("removeListener",e,a||t)}return this},r.prototype.off=function(e,t){return this.removeListener(e,t)},r.prototype.removeAllListeners=function(e){var t,s;if(!(s=this._events))return this;if(!s.removeListener)return 0===arguments.length?(this._events=new o,this._eventsCount=0):s[e]&&(0==--this._eventsCount?this._events=new o:delete s[e]),this;if(0===arguments.length){for(var r,n=Object.keys(s),i=0;i<n.length;++i)"removeListener"!==(r=n[i])&&this.removeAllListeners(r);return this.removeAllListeners("removeListener"),this._events=new o,this._eventsCount=0,this}if("function"==typeof(t=s[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},r.prototype.listeners=function(e){var t,s=this._events;return s&&(t=s[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),s=0;s<t.length;++s)t[s]=e[s].listener||e[s];return t}(t):[]},r.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):l.call(e,t)},r.prototype.listenerCount=l,r.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class f extends r{}exports.EventType=void 0,(e=exports.EventType||(exports.EventType={})).Issue="issue",e.NetworkScoresUpdated="network-scores-updated",e.StatsParsingFinished="stats-parsing-finished",exports.IssueType=void 0,(t=exports.IssueType||(exports.IssueType={})).Network="network",t.CPU="cpu",t.Server="server",t.Stream="stream",exports.IssueReason=void 0,(s=exports.IssueReason||(exports.IssueReason={})).OutboundNetworkQuality="outbound-network-quality",s.InboundNetworkQuality="inbound-network-quality",s.OutboundNetworkMediaLatency="outbound-network-media-latency",s.InboundNetworkMediaLatency="inbound-network-media-latency",s.NetworkMediaSyncFailure="network-media-sync-failure",s.OutboundNetworkThroughput="outbound-network-throughput",s.InboundNetworkThroughput="inbound-network-throughput",s.EncoderCPUThrottling="encoder-cpu-throttling",s.DecoderCPUThrottling="decoder-cpu-throttling",s.ServerIssue="server-issue",s.UnknownVideoDecoderIssue="unknown-video-decoder",s.LowInboundMOS="low-inbound-mean-opinion-score",s.LowOutboundMOS="low-outbound-mean-opinion-score",s.FrozenVideoTrack="frozen-video-track";class g extends r{static STATS_REPORT_READY_EVENT="stats-report-ready";static STATS_REPORTS_PARSED="stats-reports-parsed";isStopped=!1;reportTimer;getStatsInterval;compositeStatsParser;constructor(e){super(),this.compositeStatsParser=e.compositeStatsParser,this.getStatsInterval=e.getStatsInterval??1e4}get isRunning(){return!!this.reportTimer&&!this.isStopped}startReporting(){if(this.reportTimer)return;const e=()=>setTimeout((()=>{this.isStopped?this.reportTimer=void 0:this.parseReports().finally((()=>{this.reportTimer=e()}))}),this.getStatsInterval);this.isStopped=!1,this.reportTimer=e()}stopReporting(){this.isStopped=!0,this.reportTimer&&(clearTimeout(this.reportTimer),this.reportTimer=void 0)}async parseReports(){const e=Date.now(),t=await this.compositeStatsParser.parse(),s=Date.now()-e;this.emit(g.STATS_REPORTS_PARSED,{timeTaken:s}),t.forEach((e=>{this.emit(g.STATS_REPORT_READY_EVENT,e)}))}}const S=(()=>{const e=new Map;return t=>{const{taskId:s,delayMs:o,maxJitterMs:r,callback:n}=t,i=Math.ceil(Math.random()*(r||0)),a=e.get(s);a&&clearTimeout(a);const c=setTimeout((()=>{n(),e.delete(s)}),o+i);e.set(s,c)}})();class v{#e={};calculate(e){const{connection:{id:t}}=e,{mos:s,stats:o}=this.calculateOutboundScore(e)||{},{mos:r,stats:n}=this.calculateInboundScore(e)||{};return this.#e[t]=e,S({taskId:t,delayMs:35e3,callback:()=>delete this.#e[t]}),{outbound:s,inbound:r,connectionId:t,statsSamples:{inboundStatsSample:n,outboundStatsSample:o}}}calculateOutboundScore(e){const t=[...e.remote?.audio.inbound||[],...e.remote?.video.inbound||[]];if(!t.length)return;const s=this.#e[e.connection.id];if(!s)return;const o=[...s.remote?.audio.inbound||[],...s.remote?.video.inbound||[]],{packetsSent:r}=e.connection,n=s.connection.packetsSent,i=t.reduce(((e,t)=>{const s=o.find((e=>e.ssrc===t.ssrc));return{sumJitter:e.sumJitter+t.jitter,packetsLost:e.packetsLost+t.packetsLost,lastPacketsLost:e.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),a=1e3*e.connection.currentRoundTripTime||0,{sumJitter:c}=i,d=c/t.length,u=r-n,p=i.packetsLost-i.lastPacketsLost,h=u&&p?Math.round(100*p/(u+p)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:h}),stats:{avgJitter:d,rtt:a,packetsLoss:h}}}calculateInboundScore(e){const t=[...e.audio?.inbound,...e.video?.inbound];if(!t.length)return;const s=this.#e[e.connection.id];if(!s)return;const o=[...s.video?.inbound,...s.audio?.inbound],{packetsReceived:r}=e.connection,n=s.connection.packetsReceived,i=t.reduce(((e,t)=>{const s=o.find((e=>e.ssrc===t.ssrc));return{sumJitter:e.sumJitter+t.jitter,packetsLost:e.packetsLost+t.packetsLost,lastPacketsLost:e.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),a=1e3*e.connection.currentRoundTripTime||0,{sumJitter:c}=i,d=c/t.length,u=r-n,p=i.packetsLost-i.lastPacketsLost,h=u&&p?Math.round(100*p/(u+p)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:h}),stats:{avgJitter:d,rtt:a,packetsLoss:h}}}calculateMOS({avgJitter:e,rtt:t,packetsLoss:s}){const o=t+2*e+10;let r=o<160?93.2-o/40:93.2-o/120-10;return r-=2.5*s,1+.035*r+7e-6*r*(r-60)*(100-r)}}class k{#e;#t;constructor(e={}){this.#e=new Map,this.#t=e.statsCleanupTtlMs??35e3}detect(e){const t=this.performDetection(e);return this.performPrevStatsCleanup({connectionId:e.connection.id}),t}performPrevStatsCleanup(e){const{connectionId:t,cleanupCallback:s}=e;this.#e.has(t)&&S({taskId:t,delayMs:this.#t,callback:()=>{this.deleteLastProcessedStats(t),"function"==typeof s&&s()}})}setLastProcessedStats(e,t){this.#e.set(e,t)}getLastProcessedStats(e){return this.#e.get(e)}deleteLastProcessedStats(e){this.#e.delete(e)}}class y extends k{#s;constructor(e={}){super(e),this.#s=e.availableOutgoingBitrateThreshold??1e5}performDetection(e){const t=[],{availableOutgoingBitrate:s}=e.connection;if(void 0===s)return t;const o=e.audio.outbound.reduce(((e,t)=>e+t.targetBitrate),0),r=e.video.outbound.reduce(((e,t)=>e+t.bitrate),0);if(!o&&!r)return t;const n={availableOutgoingBitrate:s,videoStreamsTotalBitrate:r,audioStreamsTotalTargetBitrate:o};return o>s||r>0&&s<this.#s?(t.push({statsSample:n,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput}),t):t}}class w extends k{#o;constructor(e={}){super(e),this.#o=e.framesDroppedThreshold??.5}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=e.video.inbound.filter((e=>e.framesDropped>0)),s=[],o=this.getLastProcessedStats(e.connection.id)?.video.inbound;return o?(t.forEach((e=>{const t=o.find((t=>t.ssrc===e.ssrc));if(!t)return;if(e.framesDropped===t.framesDropped)return;const r=e.framesReceived-t.framesReceived,n=e.framesDecoded-t.framesDecoded,i=e.framesDropped-t.framesDropped,a=i/r;if(0===r||0===n)return;const c={deltaFramesDropped:i,deltaFramesReceived:r,deltaFramesDecoded:n,framesDroppedPct:Math.round(100*a)};a>=this.#o&&s.push({statsSample:c,type:exports.IssueType.CPU,reason:exports.IssueReason.DecoderCPUThrottling,ssrc:e.ssrc})})),s):s}}class T extends k{#r;constructor(e={}){super(e),this.#r=e.missedFramesThreshold??.15}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=e.video.outbound.filter((e=>e.framesEncoded>0)),s=[],o=this.getLastProcessedStats(e.connection.id)?.video.outbound;return o?(t.forEach((e=>{const t=o.find((t=>t.ssrc===e.ssrc));if(!t)return;if(e.framesEncoded===t.framesEncoded)return;const r=e.framesEncoded-t.framesEncoded,n=e.framesSent-t.framesSent,i=1-n/r;if(0===r)return;if(r===n)return;const a={deltaFramesSent:n,deltaFramesEncoded:r,missedFramesPct:Math.round(100*i)};i>=this.#r&&s.push({statsSample:a,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput,ssrc:e.ssrc})})),s):s}}class b extends k{#n;#i;#a;#c;constructor(e={}){super(),this.#n=e.highPacketLossThresholdPct??5,this.#i=e.highJitterThreshold??200,this.#a=e.highJitterBufferDelayThresholdMs??500,this.#c=e.highRttThresholdMs??250}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=[],s=[...e.audio?.inbound,...e.video?.inbound];if(!s.length)return t;const o=this.getLastProcessedStats(e.connection.id);if(!o)return t;const r=[...o.video?.inbound,...o.audio?.inbound],{packetsReceived:n}=e.connection,i=o.connection.packetsReceived,a=s.reduce(((e,t)=>{const s=r.find((e=>e.ssrc===t.ssrc)),o=s?.jitterBufferDelay||0,n=s?.jitterBufferEmittedCount||0,i=t.jitterBufferDelay-o,a=t.jitterBufferEmittedCount-n,c=i&&a?1e3*i/a:0;return{sumJitter:e.sumJitter+t.jitter,sumJitterBufferDelayMs:e.sumJitterBufferDelayMs+c,packetsLost:e.packetsLost+t.packetsLost,lastPacketsLost:e.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,sumJitterBufferDelayMs:0,packetsLost:0,lastPacketsLost:0}),c=1e3*e.connection.currentRoundTripTime||0,{sumJitter:d,sumJitterBufferDelayMs:u}=a,p=d/s.length,h=u/s.length,l=n-i,m=a.packetsLost-a.lastPacketsLost,f=l&&m?Math.round(100*m/(l+m)):0,g=f>this.#n,S=p>=this.#i,v=c>=this.#c,k=h>this.#a,y=v&&!S&&!g,w=g&&S,T=S&&k,b={rtt:c,packetLossPct:f,avgJitter:p,avgJitterBufferDelay:h};return(S||g)&&t.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.InboundNetworkQuality,iceCandidate:e.connection.local.id}),y&&t.push({statsSample:b,type:exports.IssueType.Server,reason:exports.IssueReason.ServerIssue,iceCandidate:e.connection.remote.id}),w&&t.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.InboundNetworkMediaLatency,iceCandidate:e.connection.local.id}),T&&t.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.NetworkMediaSyncFailure,iceCandidate:e.connection.local.id}),t}}class P extends k{#d;constructor(e={}){super(),this.#d=e.correctedSamplesThresholdPct??5}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=e.audio.inbound,s=[],o=this.getLastProcessedStats(e.connection.id)?.audio.inbound;return o?(t.forEach((e=>{const t=o.find((t=>t.ssrc===e.ssrc));if(!t)return;const r=e.track.insertedSamplesForDeceleration+e.track.removedSamplesForAcceleration,n=t.track.insertedSamplesForDeceleration+t.track.removedSamplesForAcceleration;if(r===n)return;const i=e.track.totalSamplesReceived-t.track.totalSamplesReceived,a=r-n,c=Math.round(100*a/i),d={correctedSamplesPct:c};c>this.#d&&s.push({statsSample:d,type:exports.IssueType.Network,reason:exports.IssueReason.NetworkMediaSyncFailure,ssrc:e.ssrc})})),s):s}}class L extends k{#n;#i;constructor(e={}){super(),this.#n=e.highPacketLossThresholdPct??5,this.#i=e.highJitterThreshold??200}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=[],s=[...e.remote?.audio.inbound||[],...e.remote?.video.inbound||[]];if(!s.length)return t;const o=this.getLastProcessedStats(e.connection.id);if(!o)return t;const r=[...o.remote?.audio.inbound||[],...o.remote?.video.inbound||[]],{packetsSent:n}=e.connection,i=o.connection.packetsSent,a=s.reduce(((e,t)=>{const s=r.find((e=>e.ssrc===t.ssrc));return{sumJitter:e.sumJitter+t.jitter,packetsLost:e.packetsLost+t.packetsLost,lastPacketsLost:e.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),c=1e3*e.connection.currentRoundTripTime||0,{sumJitter:d}=a,u=d/s.length,p=n-i,h=a.packetsLost-a.lastPacketsLost,l=p&&h?Math.round(100*h/(p+h)):0,m=l>this.#n,f=u>=this.#i,g=!m&&f||f||m,S={rtt:c,avgJitter:u,packetLossPct:l};return m&&f&&t.push({statsSample:S,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkMediaLatency,iceCandidate:e.connection.local.id}),g&&t.push({statsSample:S,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkQuality,iceCandidate:e.connection.local.id}),t}}class D extends k{performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const t=e.video.outbound.filter((e=>"none"!==e.qualityLimitationReason)),s=[],o=this.getLastProcessedStats(e.connection.id)?.video.outbound;return o?(t.forEach((e=>{const t=o.find((t=>t.ssrc===e.ssrc));if(!t)return;const r={qualityLimitationReason:e.qualityLimitationReason};e.framesSent>t.framesSent||("cpu"===e.qualityLimitationReason&&s.push({statsSample:r,type:exports.IssueType.CPU,reason:exports.IssueReason.EncoderCPUThrottling,ssrc:e.ssrc}),"bandwidth"===e.qualityLimitationReason&&s.push({statsSample:r,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput,ssrc:e.ssrc}))})),s):s}}class I extends k{UNKNOWN_DECODER="unknown";#u={};performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}performPrevStatsCleanup(e){const{connectionId:t,cleanupCallback:s}=e;super.performPrevStatsCleanup({...e,cleanupCallback:()=>{delete this.#u[t],"function"==typeof s&&s()}})}processData(e){const t=[],{id:s}=e.connection,o=this.getLastProcessedStats(s)?.video.inbound;return e.video.inbound.forEach((e=>{const{decoderImplementation:r,ssrc:n}=e,i=o?.find((e=>e.ssrc===n));if(i)if(r===this.UNKNOWN_DECODER){if(!this.hadLastDecoderWithIssue(s,n)){this.setLastDecoderWithIssue(s,n,this.UNKNOWN_DECODER);const o={mimeType:e.mimeType,decoderImplementation:r};t.push({ssrc:n,statsSample:o,type:exports.IssueType.Stream,reason:exports.IssueReason.UnknownVideoDecoderIssue,trackIdentifier:e.track.trackIdentifier})}}else this.setLastDecoderWithIssue(s,n,void 0)})),t}setLastDecoderWithIssue(e,t,s){const o=this.#u[e]??{};void 0===s?delete o[t]:o[t]=s,this.#u[e]=o}hadLastDecoderWithIssue(e,t){const s=this.#u[e];return(s&&s[t])===this.UNKNOWN_DECODER}}class R extends k{#p=new Map;#h;#o;constructor(e={}){super(),this.#h=e.timeoutMs??1e4,this.#o=e.framesDroppedThreshold??.5}performDetection(e){const{connection:{id:t}}=e,s=this.processData(e);return this.setLastProcessedStats(t,e),s}processData(e){const{connection:{id:t}}=e,s=this.getLastProcessedStats(t),o=[];if(!s)return o;const{video:{inbound:r}}=e,{video:{inbound:n}}=s,i=e=>new Map(e.map((e=>[e.track.trackIdentifier,e]))),a=i(r),c=i(n),d=new Set(this.#p.keys());return Array.from(a.entries()).forEach((([e,t])=>{d.delete(e);const s=c.get(e);if(!s)return;const r=t.framesReceived-s.framesReceived,n=t.framesDropped-s.framesDropped,i=t.framesDecoded-s.framesDecoded;if(0===r)return;if(n/r>=this.#o)return;if(i>0)return void this.removeMarkIssue(e);if(!this.markIssue(e))return;const a={framesReceived:t.framesReceived,framesDropped:t.framesDropped,framesDecoded:t.framesDecoded,deltaFramesReceived:r,deltaFramesDropped:n,deltaFramesDecoded:i};o.push({statsSample:a,type:exports.IssueType.Stream,reason:exports.IssueReason.FrozenVideoTrack,trackIdentifier:e})})),d.forEach((e=>{this.removeMarkIssue(e)})),o}markIssue(e){const t=Date.now(),s=this.#p.get(e);return s?!(t-s<this.#h):(this.#p.set(e,t),!1)}removeMarkIssue(e){this.#p.delete(e)}}const C=e=>"closed"===e.iceConnectionState||"closed"===e.connectionState,x=(e,t,s)=>8*((e,t,s)=>{if(!t)return 0;const o=e[s],r=t[s];if(null==o||null==r)return 0;const n=Math.floor(e.timestamp)-Math.floor(t.timestamp);return 0===n?0:(Number(o)-Number(r))/n*1e3})(e,t,s);class E{connections=[];statsParser;constructor(e){this.statsParser=e.statsParser}listConnections(){return[...this.connections]}addPeerConnection(e){this.connections.push({id:e.id??String(Date.now()+Math.random().toString(32)),pc:e.pc})}removePeerConnection(e){const t=this.connections.findIndex((({pc:t})=>t===e.pc));t>=0&&this.removeConnectionsByIndexes([t])}async parse(){const e=[],t=this.connections.map((async(t,s)=>{if(!C(t.pc))return this.statsParser.parse(t);e.unshift(s)}));e.length&&this.removeConnectionsByIndexes(e);return(await Promise.all(t)).filter((e=>void 0!==e))}removeConnectionsByIndexes(e){e.forEach((e=>{this.connections.splice(e,1)}))}}class M{prevStats=new Map;allowedReportTypes=new Set(["candidate-pair","inbound-rtp","outbound-rtp","remote-outbound-rtp","remote-inbound-rtp","track","transport"]);ignoreSSRCList;logger;constructor(e){this.ignoreSSRCList=e.ignoreSSRCList??[],this.logger=e.logger}get previouslyParsedStatsConnectionsIds(){return[...this.prevStats.keys()]}async parse(e){if(!C(e.pc))return this.getConnectionStats(e);this.logger.debug("Skip stats parsing. Connection is closed.",{connection:e})}async getConnectionStats(e){const{pc:t,id:s}=e;try{const o=Date.now(),r=t.getReceivers().filter((e=>e.track?.enabled)),n=t.getSenders().filter((e=>e.track?.enabled)),i=await Promise.all(r.map((e=>e.getStats()))),a=await Promise.all(n.map((e=>e.getStats())));return{id:s,stats:this.mapReportsStats([...i,...a],e),timeTaken:Date.now()-o}}catch(e){return void this.logger.error("Failed to get stats for PC",{id:s,pc:t,error:e})}}mapReportsStats(e,t){const s={audio:{inbound:[],outbound:[]},video:{inbound:[],outbound:[]},connection:{},remote:{video:{inbound:[],outbound:[]},audio:{inbound:[],outbound:[]}}};e.forEach((e=>{e.forEach((t=>{this.allowedReportTypes.has(t.type)&&this.updateMappedStatsWithReportItemData(t,s,e)}))}));const{id:o}=t,r=this.prevStats.get(o);return r&&this.propagateStatsWithRateValues(s,r.stats),this.prevStats.set(o,{stats:s,ts:Date.now()}),S({taskId:o,delayMs:35e3,callback:()=>this.prevStats.delete(o)}),s}updateMappedStatsWithReportItemData(e,t,s){const o=e.type;if("candidate-pair"===o&&"succeeded"===e.state&&e.nominated)return void(t.connection=this.prepareConnectionStats(e,s));const r=this.getMediaType(e);if(!r)return;const n=e.ssrc;if(!n||!this.ignoreSSRCList.includes(n))if("outbound-rtp"!==o)if("inbound-rtp"!==o)"remote-outbound-rtp"!==o?"remote-inbound-rtp"===o&&(this.mapConnectionStatsIfNecessary(t,e,s),t.remote[r].inbound.push({...e})):t.remote[r].outbound.push({...e});else{const o=s.get(e.trackId)||s.get(e.mediaSourceId)||{};this.mapConnectionStatsIfNecessary(t,e,s);const n={...e,track:{...o}};t[r].inbound.push(n)}else{const o=s.get(e.trackId)||s.get(e.mediaSourceId)||{},n={...e,track:{...o}};t[r].outbound.push(n)}}getMediaType(e){const t=e.mediaType||e.kind;if(!["audio","video"].includes(t)){const{id:t}=e;if(!t)return;return String(t).includes("Video")?"video":String(t).includes("Audio")?"audio":void 0}return t}propagateStatsWithRateValues(e,t){e.audio.inbound.forEach((e=>{const s=t.audio.inbound.find((({id:t})=>t===e.id));e.bitrate=x(e,s,"bytesReceived"),e.packetRate=x(e,s,"packetsReceived")})),e.audio.outbound.forEach((e=>{const s=t.audio.outbound.find((({id:t})=>t===e.id));e.bitrate=x(e,s,"bytesSent"),e.packetRate=x(e,s,"packetsSent")})),e.video.inbound.forEach((e=>{const s=t.video.inbound.find((({id:t})=>t===e.id));e.bitrate=x(e,s,"bytesReceived"),e.packetRate=x(e,s,"packetsReceived")})),e.video.outbound.forEach((e=>{const s=t.video.outbound.find((({id:t})=>t===e.id));e.bitrate=x(e,s,"bytesSent"),e.packetRate=x(e,s,"packetsSent")}))}mapConnectionStatsIfNecessary(e,t,s){if(e.connection.id||!t.transportId)return;const o=s.get(t.transportId);if(o&&o.selectedCandidatePairId){const t=s.get(o.selectedCandidatePairId);e.connection=this.prepareConnectionStats(t,s)}}prepareConnectionStats(e,t){if(!e||!t)return{};const s={...e};if(s.remoteCandidateId){const e=t.get(s.remoteCandidateId);s.remote={...e}}if(s.localCandidateId){const e=t.get(s.localCandidateId);s.local={...e}}return s}}exports.AvailableOutgoingBitrateIssueDetector=y,exports.BaseIssueDetector=k,exports.CompositeRTCStatsParser=E,exports.FramesDroppedIssueDetector=w,exports.FramesEncodedSentIssueDetector=T,exports.FrozenVideoTrackDetector=R,exports.InboundNetworkIssueDetector=b,exports.NetworkMediaSyncIssueDetector=P,exports.NetworkScoresCalculator=v,exports.OutboundNetworkIssueDetector=L,exports.PeriodicWebRTCStatsReporter=g,exports.QualityLimitationsIssueDetector=D,exports.RTCStatsParser=M,exports.UnknownVideoDecoderImplementationDetector=I,exports.WebRTCIssueEmitter=f,exports.default=class{eventEmitter;#l=!1;detectors=[];networkScoresCalculator;statsReporter;compositeStatsParser;logger;autoAddPeerConnections;constructor(e){this.logger=e.logger??{debug:()=>{},info:()=>{},warn:()=>{},error:()=>{}},this.eventEmitter=e.issueEmitter??new f,e.onIssues&&this.eventEmitter.on(exports.EventType.Issue,e.onIssues),e.onNetworkScoresUpdated&&this.eventEmitter.on(exports.EventType.NetworkScoresUpdated,e.onNetworkScoresUpdated),this.detectors=e.detectors??[new D,new w,new T,new b,new L,new P,new y,new I,new R],this.networkScoresCalculator=e.networkScoresCalculator??new v,this.compositeStatsParser=e.compositeStatsParser??new E({statsParser:new M({ignoreSSRCList:e.ignoreSSRCList,logger:this.logger})}),this.statsReporter=e.statsReporter??new g({compositeStatsParser:this.compositeStatsParser,getStatsInterval:e.getStatsInterval??5e3}),window.wid=this,this.autoAddPeerConnections=e.autoAddPeerConnections??!0,this.autoAddPeerConnections&&this.wrapRTCPeerConnection(),this.statsReporter.on(g.STATS_REPORT_READY_EVENT,(e=>{this.detectIssues({data:e.stats}),this.calculateNetworkScores(e.stats)})),this.statsReporter.on(g.STATS_REPORTS_PARSED,(e=>{const t={timeTaken:e.timeTaken,ts:Date.now()};this.eventEmitter.emit(exports.EventType.StatsParsingFinished,t)}))}watchNewPeerConnections(){if(!this.autoAddPeerConnections)throw new Error("Auto add peer connections was disabled in the constructor.");this.#l?this.logger.warn("WebRTCIssueDetector is already started. Skip processing"):(this.logger.info("Start watching peer connections"),this.#l=!0,this.statsReporter.startReporting())}stopWatchingNewPeerConnections(){this.#l?(this.logger.info("Stop watching peer connections"),this.#l=!1,this.statsReporter.stopReporting()):this.logger.warn("WebRTCIssueDetector is already stopped. Skip processing")}handleNewPeerConnection(e){this.#l||!this.autoAddPeerConnections?(this.#l||!1!==this.autoAddPeerConnections||(this.logger.info("Starting stats reporting for new peer connection"),this.#l=!0,this.statsReporter.startReporting()),this.logger.debug("Handling new peer connection",e),this.compositeStatsParser.addPeerConnection({pc:e})):this.logger.debug("Skip handling new peer connection. Detector is not running",e)}emitIssues(e){this.eventEmitter.emit(exports.EventType.Issue,e)}detectIssues({data:e}){const t=this.detectors.reduce(((t,s)=>[...t,...s.detect(e)]),[]);t.length>0&&this.emitIssues(t)}calculateNetworkScores(e){const t=this.networkScoresCalculator.calculate(e);this.eventEmitter.emit(exports.EventType.NetworkScoresUpdated,t)}wrapRTCPeerConnection(){if(!window.RTCPeerConnection)return void this.logger.warn("No RTCPeerConnection found in browser window. Skipping");const e=window.RTCPeerConnection,t=e=>this.handleNewPeerConnection(e);function s(s){const o=new e(s);return t(o),o}s.prototype=e.prototype,window.RTCPeerConnection=s}};

@@ -1,1 +0,1 @@

var t,e,s;function n(){}function o(){o.init.call(this)}function r(t){return void 0===t._maxListeners?o.defaultMaxListeners:t._maxListeners}function i(t,e,s){if(e)t.call(s);else for(var n=t.length,o=m(t,n),r=0;r<n;++r)o[r].call(s)}function a(t,e,s,n){if(e)t.call(s,n);else for(var o=t.length,r=m(t,o),i=0;i<o;++i)r[i].call(s,n)}function c(t,e,s,n,o){if(e)t.call(s,n,o);else for(var r=t.length,i=m(t,r),a=0;a<r;++a)i[a].call(s,n,o)}function d(t,e,s,n,o,r){if(e)t.call(s,n,o,r);else for(var i=t.length,a=m(t,i),c=0;c<i;++c)a[c].call(s,n,o,r)}function u(t,e,s,n){if(e)t.apply(s,n);else for(var o=t.length,r=m(t,o),i=0;i<o;++i)r[i].apply(s,n)}function h(t,e,s,o){var i,a,c,d;if("function"!=typeof s)throw new TypeError('"listener" argument must be a function');if((a=t._events)?(a.newListener&&(t.emit("newListener",e,s.listener?s.listener:s),a=t._events),c=a[e]):(a=t._events=new n,t._eventsCount=0),c){if("function"==typeof c?c=a[e]=o?[s,c]:[c,s]:o?c.unshift(s):c.push(s),!c.warned&&(i=r(t))&&i>0&&c.length>i){c.warned=!0;var u=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+e+" listeners added. Use emitter.setMaxListeners() to increase limit");u.name="MaxListenersExceededWarning",u.emitter=t,u.type=e,u.count=c.length,d=u,"function"==typeof console.warn?console.warn(d):console.log(d)}}else c=a[e]=s,++t._eventsCount;return t}function l(t,e,s){var n=!1;function o(){t.removeListener(e,o),n||(n=!0,s.apply(t,arguments))}return o.listener=s,o}function p(t){var e=this._events;if(e){var s=e[t];if("function"==typeof s)return 1;if(s)return s.length}return 0}function m(t,e){for(var s=new Array(e);e--;)s[e]=t[e];return s}n.prototype=Object.create(null),o.EventEmitter=o,o.usingDomains=!1,o.prototype.domain=void 0,o.prototype._events=void 0,o.prototype._maxListeners=void 0,o.defaultMaxListeners=10,o.init=function(){this.domain=null,o.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new n,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},o.prototype.setMaxListeners=function(t){if("number"!=typeof t||t<0||isNaN(t))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=t,this},o.prototype.getMaxListeners=function(){return r(this)},o.prototype.emit=function(t){var e,s,n,o,r,h,l,p="error"===t;if(h=this._events)p=p&&null==h.error;else if(!p)return!1;if(l=this.domain,p){if(e=arguments[1],!l){if(e instanceof Error)throw e;var m=new Error('Uncaught, unspecified "error" event. ('+e+")");throw m.context=e,m}return e||(e=new Error('Uncaught, unspecified "error" event')),e.domainEmitter=this,e.domain=l,e.domainThrown=!1,l.emit("error",e),!1}if(!(s=h[t]))return!1;var f="function"==typeof s;switch(n=arguments.length){case 1:i(s,f,this);break;case 2:a(s,f,this,arguments[1]);break;case 3:c(s,f,this,arguments[1],arguments[2]);break;case 4:d(s,f,this,arguments[1],arguments[2],arguments[3]);break;default:for(o=new Array(n-1),r=1;r<n;r++)o[r-1]=arguments[r];u(s,f,this,o)}return!0},o.prototype.addListener=function(t,e){return h(this,t,e,!1)},o.prototype.on=o.prototype.addListener,o.prototype.prependListener=function(t,e){return h(this,t,e,!0)},o.prototype.once=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.on(t,l(this,t,e)),this},o.prototype.prependOnceListener=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.prependListener(t,l(this,t,e)),this},o.prototype.removeListener=function(t,e){var s,o,r,i,a;if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');if(!(o=this._events))return this;if(!(s=o[t]))return this;if(s===e||s.listener&&s.listener===e)0==--this._eventsCount?this._events=new n:(delete o[t],o.removeListener&&this.emit("removeListener",t,s.listener||e));else if("function"!=typeof s){for(r=-1,i=s.length;i-- >0;)if(s[i]===e||s[i].listener&&s[i].listener===e){a=s[i].listener,r=i;break}if(r<0)return this;if(1===s.length){if(s[0]=void 0,0==--this._eventsCount)return this._events=new n,this;delete o[t]}else!function(t,e){for(var s=e,n=s+1,o=t.length;n<o;s+=1,n+=1)t[s]=t[n];t.pop()}(s,r);o.removeListener&&this.emit("removeListener",t,a||e)}return this},o.prototype.off=function(t,e){return this.removeListener(t,e)},o.prototype.removeAllListeners=function(t){var e,s;if(!(s=this._events))return this;if(!s.removeListener)return 0===arguments.length?(this._events=new n,this._eventsCount=0):s[t]&&(0==--this._eventsCount?this._events=new n:delete s[t]),this;if(0===arguments.length){for(var o,r=Object.keys(s),i=0;i<r.length;++i)"removeListener"!==(o=r[i])&&this.removeAllListeners(o);return this.removeAllListeners("removeListener"),this._events=new n,this._eventsCount=0,this}if("function"==typeof(e=s[t]))this.removeListener(t,e);else if(e)do{this.removeListener(t,e[e.length-1])}while(e[0]);return this},o.prototype.listeners=function(t){var e,s=this._events;return s&&(e=s[t])?"function"==typeof e?[e.listener||e]:function(t){for(var e=new Array(t.length),s=0;s<e.length;++s)e[s]=t[s].listener||t[s];return e}(e):[]},o.listenerCount=function(t,e){return"function"==typeof t.listenerCount?t.listenerCount(e):p.call(t,e)},o.prototype.listenerCount=p,o.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class f extends o{}!function(t){t.Issue="issue",t.NetworkScoresUpdated="network-scores-updated",t.StatsParsingFinished="stats-parsing-finished"}(t||(t={})),function(t){t.Network="network",t.CPU="cpu",t.Server="server",t.Stream="stream"}(e||(e={})),function(t){t.OutboundNetworkQuality="outbound-network-quality",t.InboundNetworkQuality="inbound-network-quality",t.OutboundNetworkMediaLatency="outbound-network-media-latency",t.InboundNetworkMediaLatency="inbound-network-media-latency",t.NetworkMediaSyncFailure="network-media-sync-failure",t.OutboundNetworkThroughput="outbound-network-throughput",t.InboundNetworkThroughput="inbound-network-throughput",t.EncoderCPUThrottling="encoder-cpu-throttling",t.DecoderCPUThrottling="decoder-cpu-throttling",t.ServerIssue="server-issue",t.UnknownVideoDecoderIssue="unknown-video-decoder",t.LowInboundMOS="low-inbound-mean-opinion-score",t.LowOutboundMOS="low-outbound-mean-opinion-score",t.DeadVideoTrack="dead-video-track"}(s||(s={}));class g extends o{static STATS_REPORT_READY_EVENT="stats-report-ready";static STATS_REPORTS_PARSED="stats-reports-parsed";isStopped=!1;reportTimer;getStatsInterval;compositeStatsParser;constructor(t){super(),this.compositeStatsParser=t.compositeStatsParser,this.getStatsInterval=t.getStatsInterval??1e4}get isRunning(){return!!this.reportTimer&&!this.isStopped}startReporting(){if(this.reportTimer)return;const t=()=>setTimeout((()=>{this.isStopped?this.reportTimer=void 0:this.parseReports().finally((()=>{this.reportTimer=t()}))}),this.getStatsInterval);this.isStopped=!1,this.reportTimer=t()}stopReporting(){this.isStopped=!0,this.reportTimer&&(clearTimeout(this.reportTimer),this.reportTimer=void 0)}async parseReports(){const t=Date.now(),e=await this.compositeStatsParser.parse(),s=Date.now()-t;this.emit(g.STATS_REPORTS_PARSED,{timeTaken:s}),e.forEach((t=>{this.emit(g.STATS_REPORT_READY_EVENT,t)}))}}const S=(()=>{const t=new Map;return e=>{const{taskId:s,delayMs:n,maxJitterMs:o,callback:r}=e,i=Math.ceil(Math.random()*(o||0)),a=t.get(s);a&&clearTimeout(a);const c=setTimeout((()=>{r(),t.delete(s)}),n+i);t.set(s,c)}})();class v{#t={};calculate(t){const{connection:{id:e}}=t,{mos:s,stats:n}=this.calculateOutboundScore(t)||{},{mos:o,stats:r}=this.calculateInboundScore(t)||{};return this.#t[e]=t,S({taskId:e,delayMs:35e3,callback:()=>delete this.#t[e]}),{outbound:s,inbound:o,connectionId:e,statsSamples:{inboundStatsSample:r,outboundStatsSample:n}}}calculateOutboundScore(t){const e=[...t.remote?.audio.inbound||[],...t.remote?.video.inbound||[]];if(!e.length)return;const s=this.#t[t.connection.id];if(!s)return;const n=[...s.remote?.audio.inbound||[],...s.remote?.video.inbound||[]],{packetsSent:o}=t.connection,r=s.connection.packetsSent,i=e.reduce(((t,e)=>{const s=n.find((t=>t.ssrc===e.ssrc));return{sumJitter:t.sumJitter+e.jitter,packetsLost:t.packetsLost+e.packetsLost,lastPacketsLost:t.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),a=1e3*t.connection.currentRoundTripTime||0,{sumJitter:c}=i,d=c/e.length,u=o-r,h=i.packetsLost-i.lastPacketsLost,l=u&&h?Math.round(100*h/(u+h)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:l}),stats:{avgJitter:d,rtt:a,packetsLoss:l}}}calculateInboundScore(t){const e=[...t.audio?.inbound,...t.video?.inbound];if(!e.length)return;const s=this.#t[t.connection.id];if(!s)return;const n=[...s.video?.inbound,...s.audio?.inbound],{packetsReceived:o}=t.connection,r=s.connection.packetsReceived,i=e.reduce(((t,e)=>{const s=n.find((t=>t.ssrc===e.ssrc));return{sumJitter:t.sumJitter+e.jitter,packetsLost:t.packetsLost+e.packetsLost,lastPacketsLost:t.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),a=1e3*t.connection.currentRoundTripTime||0,{sumJitter:c}=i,d=c/e.length,u=o-r,h=i.packetsLost-i.lastPacketsLost,l=u&&h?Math.round(100*h/(u+h)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:l}),stats:{avgJitter:d,rtt:a,packetsLoss:l}}}calculateMOS({avgJitter:t,rtt:e,packetsLoss:s}){const n=e+2*t+10;let o=n<160?93.2-n/40:93.2-n/120-10;return o-=2.5*s,1+.035*o+7e-6*o*(o-60)*(100-o)}}class k{#t;#e;constructor(t={}){this.#t=new Map,this.#e=t.statsCleanupTtlMs??35e3}detect(t){const e=this.performDetection(t);return this.performPrevStatsCleanup({connectionId:t.connection.id}),e}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;this.#t.has(e)&&S({taskId:e,delayMs:this.#e,callback:()=>{this.deleteLastProcessedStats(e),"function"==typeof s&&s()}})}setLastProcessedStats(t,e){this.#t.set(t,e)}getLastProcessedStats(t){return this.#t.get(t)}deleteLastProcessedStats(t){this.#t.delete(t)}}class w extends k{#s;constructor(t={}){super(t),this.#s=t.availableOutgoingBitrateThreshold??1e5}performDetection(t){const n=[],{availableOutgoingBitrate:o}=t.connection;if(void 0===o)return n;const r=t.audio.outbound.reduce(((t,e)=>t+e.targetBitrate),0),i=t.video.outbound.reduce(((t,e)=>t+e.bitrate),0);if(!r&&!i)return n;const a={availableOutgoingBitrate:o,videoStreamsTotalBitrate:i,audioStreamsTotalTargetBitrate:r};return r>o||i>0&&o<this.#s?(n.push({statsSample:a,type:e.Network,reason:s.OutboundNetworkThroughput}),n):n}}class b extends k{#n;constructor(t={}){super(t),this.#n=t.framesDroppedThreshold??.5}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const n=t.video.inbound.filter((t=>t.framesDropped>0)),o=[],r=this.getLastProcessedStats(t.connection.id)?.video.inbound;return r?(n.forEach((t=>{const n=r.find((e=>e.ssrc===t.ssrc));if(!n)return;if(t.framesDropped===n.framesDropped)return;const i=t.framesReceived-n.framesReceived,a=t.framesDecoded-n.framesDecoded,c=t.framesDropped-n.framesDropped,d=c/i;if(0===i||0===a)return;const u={deltaFramesDropped:c,deltaFramesReceived:i,deltaFramesDecoded:a,framesDroppedPct:Math.round(100*d)};d>=this.#n&&o.push({statsSample:u,type:e.CPU,reason:s.DecoderCPUThrottling,ssrc:t.ssrc})})),o):o}}class P extends k{#o;constructor(t={}){super(t),this.#o=t.missedFramesThreshold??.15}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const n=t.video.outbound.filter((t=>t.framesEncoded>0)),o=[],r=this.getLastProcessedStats(t.connection.id)?.video.outbound;return r?(n.forEach((t=>{const n=r.find((e=>e.ssrc===t.ssrc));if(!n)return;if(t.framesEncoded===n.framesEncoded)return;const i=t.framesEncoded-n.framesEncoded,a=t.framesSent-n.framesSent,c=1-a/i;if(0===i)return;if(i===a)return;const d={deltaFramesSent:a,deltaFramesEncoded:i,missedFramesPct:Math.round(100*c)};c>=this.#o&&o.push({statsSample:d,type:e.Network,reason:s.OutboundNetworkThroughput,ssrc:t.ssrc})})),o):o}}class y extends k{#r;#i;#a;#c;constructor(t={}){super(),this.#r=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200,this.#a=t.highJitterBufferDelayThresholdMs??500,this.#c=t.highRttThresholdMs??250}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const n=[],o=[...t.audio?.inbound,...t.video?.inbound];if(!o.length)return n;const r=this.getLastProcessedStats(t.connection.id);if(!r)return n;const i=[...r.video?.inbound,...r.audio?.inbound],{packetsReceived:a}=t.connection,c=r.connection.packetsReceived,d=o.reduce(((t,e)=>{const s=i.find((t=>t.ssrc===e.ssrc)),n=s?.jitterBufferDelay||0,o=s?.jitterBufferEmittedCount||0,r=e.jitterBufferDelay-n,a=e.jitterBufferEmittedCount-o,c=r&&a?1e3*r/a:0;return{sumJitter:t.sumJitter+e.jitter,sumJitterBufferDelayMs:t.sumJitterBufferDelayMs+c,packetsLost:t.packetsLost+e.packetsLost,lastPacketsLost:t.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,sumJitterBufferDelayMs:0,packetsLost:0,lastPacketsLost:0}),u=1e3*t.connection.currentRoundTripTime||0,{sumJitter:h,sumJitterBufferDelayMs:l}=d,p=h/o.length,m=l/o.length,f=a-c,g=d.packetsLost-d.lastPacketsLost,S=f&&g?Math.round(100*g/(f+g)):0,v=S>this.#r,k=p>=this.#i,w=u>=this.#c,b=m>this.#a,P=w&&!k&&!v,y=v&&k,L=k&&b,T={rtt:u,packetLossPct:S,avgJitter:p,avgJitterBufferDelay:m};return(k||v)&&n.push({statsSample:T,type:e.Network,reason:s.InboundNetworkQuality,iceCandidate:t.connection.local.id}),P&&n.push({statsSample:T,type:e.Server,reason:s.ServerIssue,iceCandidate:t.connection.remote.id}),y&&n.push({statsSample:T,type:e.Network,reason:s.InboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),L&&n.push({statsSample:T,type:e.Network,reason:s.NetworkMediaSyncFailure,iceCandidate:t.connection.local.id}),n}}class L extends k{#d;constructor(t={}){super(),this.#d=t.correctedSamplesThresholdPct??5}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const n=t.audio.inbound,o=[],r=this.getLastProcessedStats(t.connection.id)?.audio.inbound;return r?(n.forEach((t=>{const n=r.find((e=>e.ssrc===t.ssrc));if(!n)return;const i=t.track.insertedSamplesForDeceleration+t.track.removedSamplesForAcceleration,a=n.track.insertedSamplesForDeceleration+n.track.removedSamplesForAcceleration;if(i===a)return;const c=t.track.totalSamplesReceived-n.track.totalSamplesReceived,d=i-a,u=Math.round(100*d/c),h={correctedSamplesPct:u};u>this.#d&&o.push({statsSample:h,type:e.Network,reason:s.NetworkMediaSyncFailure,ssrc:t.ssrc})})),o):o}}class T extends k{#r;#i;constructor(t={}){super(),this.#r=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const n=[],o=[...t.remote?.audio.inbound||[],...t.remote?.video.inbound||[]];if(!o.length)return n;const r=this.getLastProcessedStats(t.connection.id);if(!r)return n;const i=[...r.remote?.audio.inbound||[],...r.remote?.video.inbound||[]],{packetsSent:a}=t.connection,c=r.connection.packetsSent,d=o.reduce(((t,e)=>{const s=i.find((t=>t.ssrc===e.ssrc));return{sumJitter:t.sumJitter+e.jitter,packetsLost:t.packetsLost+e.packetsLost,lastPacketsLost:t.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),u=1e3*t.connection.currentRoundTripTime||0,{sumJitter:h}=d,l=h/o.length,p=a-c,m=d.packetsLost-d.lastPacketsLost,f=p&&m?Math.round(100*m/(p+m)):0,g=f>this.#r,S=l>=this.#i,v=!g&&S||S||g,k={rtt:u,avgJitter:l,packetLossPct:f};return g&&S&&n.push({statsSample:k,type:e.Network,reason:s.OutboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),v&&n.push({statsSample:k,type:e.Network,reason:s.OutboundNetworkQuality,iceCandidate:t.connection.local.id}),n}}class D extends k{performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const n=t.video.outbound.filter((t=>"none"!==t.qualityLimitationReason)),o=[],r=this.getLastProcessedStats(t.connection.id)?.video.outbound;return r?(n.forEach((t=>{const n=r.find((e=>e.ssrc===t.ssrc));if(!n)return;const i={qualityLimitationReason:t.qualityLimitationReason};t.framesSent>n.framesSent||("cpu"===t.qualityLimitationReason&&o.push({statsSample:i,type:e.CPU,reason:s.EncoderCPUThrottling,ssrc:t.ssrc}),"bandwidth"===t.qualityLimitationReason&&o.push({statsSample:i,type:e.Network,reason:s.OutboundNetworkThroughput,ssrc:t.ssrc}))})),o):o}}class C extends k{UNKNOWN_DECODER="unknown";#u={};performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;super.performPrevStatsCleanup({...t,cleanupCallback:()=>{delete this.#u[e],"function"==typeof s&&s()}})}processData(t){const n=[],{id:o}=t.connection,r=this.getLastProcessedStats(o)?.video.inbound;return t.video.inbound.forEach((t=>{const{decoderImplementation:i,ssrc:a}=t,c=r?.find((t=>t.ssrc===a));if(c)if(i===this.UNKNOWN_DECODER){if(!this.hadLastDecoderWithIssue(o,a)){this.setLastDecoderWithIssue(o,a,this.UNKNOWN_DECODER);const r={mimeType:t.mimeType,decoderImplementation:i};n.push({ssrc:a,statsSample:r,type:e.Stream,reason:s.UnknownVideoDecoderIssue,trackIdentifier:t.track.trackIdentifier})}}else this.setLastDecoderWithIssue(o,a,void 0)})),n}setLastDecoderWithIssue(t,e,s){const n=this.#u[t]??{};void 0===s?delete n[e]:n[e]=s,this.#u[t]=n}hadLastDecoderWithIssue(t,e){const s=this.#u[t];return(s&&s[e])===this.UNKNOWN_DECODER}}class R extends k{#h;#l;constructor(t={}){super(),this.#l=t.timeoutMs??1e4}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const{connection:{id:n}}=t,o=this.getLastProcessedStats(n),r=[];if(!o)return r;const{video:{inbound:i}}=t,{video:{inbound:a}}=o,c=t=>new Map(t.map((t=>[t.track.trackIdentifier,t]))),d=c(i),u=c(a);return Array.from(d.entries()).forEach((([t,n])=>{const o=u.get(t);if(o&&n.packetsReceived>o.packetsReceived)if(n.framesDecoded>o.framesDecoded)this.removeMarkIssue();else{if(this.markIssue()){const i={packetsReceived:n.packetsReceived,framesDecoded:n.framesDecoded,deltaFramesDecoded:n.framesDecoded-o.framesDecoded,deltaPacketsReceived:n.packetsReceived-o.packetsReceived};r.push({statsSample:i,type:e.Stream,reason:s.DeadVideoTrack,iceCandidate:t})}}})),r}markIssue(){const t=Date.now();return this.#h?!(t-this.#h<this.#l):(this.#h=t,!1)}removeMarkIssue(){this.#h=void 0}}const I=t=>"closed"===t.iceConnectionState||"closed"===t.connectionState,E=(t,e,s)=>8*((t,e,s)=>{if(!e)return 0;const n=t[s],o=e[s];if(null==n||null==o)return 0;const r=Math.floor(t.timestamp)-Math.floor(e.timestamp);return 0===r?0:(Number(n)-Number(o))/r*1e3})(t,e,s);class M{connections=[];statsParser;constructor(t){this.statsParser=t.statsParser}listConnections(){return[...this.connections]}addPeerConnection(t){this.connections.push({id:t.id??String(Date.now()+Math.random().toString(32)),pc:t.pc})}removePeerConnection(t){const e=this.connections.findIndex((({pc:e})=>e===t.pc));e>=0&&this.removeConnectionsByIndexes([e])}async parse(){const t=[],e=this.connections.map((async(e,s)=>{if(!I(e.pc))return this.statsParser.parse(e);t.unshift(s)}));t.length&&this.removeConnectionsByIndexes(t);return(await Promise.all(e)).filter((t=>void 0!==t))}removeConnectionsByIndexes(t){t.forEach((t=>{this.connections.splice(t,1)}))}}class N{prevStats=new Map;allowedReportTypes=new Set(["candidate-pair","inbound-rtp","outbound-rtp","remote-outbound-rtp","remote-inbound-rtp","track","transport"]);ignoreSSRCList;logger;constructor(t){this.ignoreSSRCList=t.ignoreSSRCList??[],this.logger=t.logger}get previouslyParsedStatsConnectionsIds(){return[...this.prevStats.keys()]}async parse(t){if(!I(t.pc))return this.getConnectionStats(t);this.logger.debug("Skip stats parsing. Connection is closed.",{connection:t})}async getConnectionStats(t){const{pc:e,id:s}=t;try{const n=Date.now(),o=e.getReceivers().filter((t=>t.track?.enabled)),r=e.getSenders().filter((t=>t.track?.enabled)),i=await Promise.all(o.map((t=>t.getStats()))),a=await Promise.all(r.map((t=>t.getStats())));return{id:s,stats:this.mapReportsStats([...i,...a],t),timeTaken:Date.now()-n}}catch(t){return void this.logger.error("Failed to get stats for PC",{id:s,pc:e,error:t})}}mapReportsStats(t,e){const s={audio:{inbound:[],outbound:[]},video:{inbound:[],outbound:[]},connection:{},remote:{video:{inbound:[],outbound:[]},audio:{inbound:[],outbound:[]}}};t.forEach((t=>{t.forEach((e=>{this.allowedReportTypes.has(e.type)&&this.updateMappedStatsWithReportItemData(e,s,t)}))}));const{id:n}=e,o=this.prevStats.get(n);return o&&this.propagateStatsWithRateValues(s,o.stats),this.prevStats.set(n,{stats:s,ts:Date.now()}),S({taskId:n,delayMs:35e3,callback:()=>this.prevStats.delete(n)}),s}updateMappedStatsWithReportItemData(t,e,s){const n=t.type;if("candidate-pair"===n&&"succeeded"===t.state&&t.nominated)return void(e.connection=this.prepareConnectionStats(t,s));const o=this.getMediaType(t);if(!o)return;const r=t.ssrc;if(!r||!this.ignoreSSRCList.includes(r))if("outbound-rtp"!==n)if("inbound-rtp"!==n)"remote-outbound-rtp"!==n?"remote-inbound-rtp"===n&&(this.mapConnectionStatsIfNecessary(e,t,s),e.remote[o].inbound.push({...t})):e.remote[o].outbound.push({...t});else{const n=s.get(t.trackId)||s.get(t.mediaSourceId)||{};this.mapConnectionStatsIfNecessary(e,t,s);const r={...t,track:{...n}};e[o].inbound.push(r)}else{const n=s.get(t.trackId)||s.get(t.mediaSourceId)||{},r={...t,track:{...n}};e[o].outbound.push(r)}}getMediaType(t){const e=t.mediaType||t.kind;if(!["audio","video"].includes(e)){const{id:e}=t;if(!e)return;return String(e).includes("Video")?"video":String(e).includes("Audio")?"audio":void 0}return e}propagateStatsWithRateValues(t,e){t.audio.inbound.forEach((t=>{const s=e.audio.inbound.find((({id:e})=>e===t.id));t.bitrate=E(t,s,"bytesReceived"),t.packetRate=E(t,s,"packetsReceived")})),t.audio.outbound.forEach((t=>{const s=e.audio.outbound.find((({id:e})=>e===t.id));t.bitrate=E(t,s,"bytesSent"),t.packetRate=E(t,s,"packetsSent")})),t.video.inbound.forEach((t=>{const s=e.video.inbound.find((({id:e})=>e===t.id));t.bitrate=E(t,s,"bytesReceived"),t.packetRate=E(t,s,"packetsReceived")})),t.video.outbound.forEach((t=>{const s=e.video.outbound.find((({id:e})=>e===t.id));t.bitrate=E(t,s,"bytesSent"),t.packetRate=E(t,s,"packetsSent")}))}mapConnectionStatsIfNecessary(t,e,s){if(t.connection.id||!e.transportId)return;const n=s.get(e.transportId);if(n&&n.selectedCandidatePairId){const e=s.get(n.selectedCandidatePairId);t.connection=this.prepareConnectionStats(e,s)}}prepareConnectionStats(t,e){if(!t||!e)return{};const s={...t};if(s.remoteCandidateId){const t=e.get(s.remoteCandidateId);s.remote={...t}}if(s.localCandidateId){const t=e.get(s.localCandidateId);s.local={...t}}return s}}class _{eventEmitter;#p=!1;detectors=[];networkScoresCalculator;statsReporter;compositeStatsParser;logger;autoAddPeerConnections;constructor(e){this.logger=e.logger??{debug:()=>{},info:()=>{},warn:()=>{},error:()=>{}},this.eventEmitter=e.issueEmitter??new f,e.onIssues&&this.eventEmitter.on(t.Issue,e.onIssues),e.onNetworkScoresUpdated&&this.eventEmitter.on(t.NetworkScoresUpdated,e.onNetworkScoresUpdated),this.detectors=e.detectors??[new D,new b,new P,new y,new T,new L,new w,new C,new R],this.networkScoresCalculator=e.networkScoresCalculator??new v,this.compositeStatsParser=e.compositeStatsParser??new M({statsParser:new N({ignoreSSRCList:e.ignoreSSRCList,logger:this.logger})}),this.statsReporter=e.statsReporter??new g({compositeStatsParser:this.compositeStatsParser,getStatsInterval:e.getStatsInterval??5e3}),window.wid=this,this.autoAddPeerConnections=e.autoAddPeerConnections??!0,this.autoAddPeerConnections&&this.wrapRTCPeerConnection(),this.statsReporter.on(g.STATS_REPORT_READY_EVENT,(t=>{this.detectIssues({data:t.stats}),this.calculateNetworkScores(t.stats)})),this.statsReporter.on(g.STATS_REPORTS_PARSED,(e=>{const s={timeTaken:e.timeTaken,ts:Date.now()};this.eventEmitter.emit(t.StatsParsingFinished,s)}))}watchNewPeerConnections(){if(!this.autoAddPeerConnections)throw new Error("Auto add peer connections was disabled in the constructor.");this.#p?this.logger.warn("WebRTCIssueDetector is already started. Skip processing"):(this.logger.info("Start watching peer connections"),this.#p=!0,this.statsReporter.startReporting())}stopWatchingNewPeerConnections(){this.#p?(this.logger.info("Stop watching peer connections"),this.#p=!1,this.statsReporter.stopReporting()):this.logger.warn("WebRTCIssueDetector is already stopped. Skip processing")}handleNewPeerConnection(t){this.#p||!this.autoAddPeerConnections?(this.#p||!1!==this.autoAddPeerConnections||(this.logger.info("Starting stats reporting for new peer connection"),this.#p=!0,this.statsReporter.startReporting()),this.logger.debug("Handling new peer connection",t),this.compositeStatsParser.addPeerConnection({pc:t})):this.logger.debug("Skip handling new peer connection. Detector is not running",t)}emitIssues(e){this.eventEmitter.emit(t.Issue,e)}detectIssues({data:t}){const e=this.detectors.reduce(((e,s)=>[...e,...s.detect(t)]),[]);e.length>0&&this.emitIssues(e)}calculateNetworkScores(e){const s=this.networkScoresCalculator.calculate(e);this.eventEmitter.emit(t.NetworkScoresUpdated,s)}wrapRTCPeerConnection(){if(!window.RTCPeerConnection)return void this.logger.warn("No RTCPeerConnection found in browser window. Skipping");const t=window.RTCPeerConnection,e=t=>this.handleNewPeerConnection(t);function s(s){const n=new t(s);return e(n),n}s.prototype=t.prototype,window.RTCPeerConnection=s}}export{w as AvailableOutgoingBitrateIssueDetector,k as BaseIssueDetector,M as CompositeRTCStatsParser,R as DeadVideoTrackDetector,t as EventType,b as FramesDroppedIssueDetector,P as FramesEncodedSentIssueDetector,y as InboundNetworkIssueDetector,s as IssueReason,e as IssueType,L as NetworkMediaSyncIssueDetector,v as NetworkScoresCalculator,T as OutboundNetworkIssueDetector,g as PeriodicWebRTCStatsReporter,D as QualityLimitationsIssueDetector,N as RTCStatsParser,C as UnknownVideoDecoderImplementationDetector,f as WebRTCIssueEmitter,_ as default};
var t,e,s;function r(){}function n(){n.init.call(this)}function o(t){return void 0===t._maxListeners?n.defaultMaxListeners:t._maxListeners}function i(t,e,s){if(e)t.call(s);else for(var r=t.length,n=m(t,r),o=0;o<r;++o)n[o].call(s)}function a(t,e,s,r){if(e)t.call(s,r);else for(var n=t.length,o=m(t,n),i=0;i<n;++i)o[i].call(s,r)}function c(t,e,s,r,n){if(e)t.call(s,r,n);else for(var o=t.length,i=m(t,o),a=0;a<o;++a)i[a].call(s,r,n)}function d(t,e,s,r,n,o){if(e)t.call(s,r,n,o);else for(var i=t.length,a=m(t,i),c=0;c<i;++c)a[c].call(s,r,n,o)}function u(t,e,s,r){if(e)t.apply(s,r);else for(var n=t.length,o=m(t,n),i=0;i<n;++i)o[i].apply(s,r)}function h(t,e,s,n){var i,a,c,d;if("function"!=typeof s)throw new TypeError('"listener" argument must be a function');if((a=t._events)?(a.newListener&&(t.emit("newListener",e,s.listener?s.listener:s),a=t._events),c=a[e]):(a=t._events=new r,t._eventsCount=0),c){if("function"==typeof c?c=a[e]=n?[s,c]:[c,s]:n?c.unshift(s):c.push(s),!c.warned&&(i=o(t))&&i>0&&c.length>i){c.warned=!0;var u=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+e+" listeners added. Use emitter.setMaxListeners() to increase limit");u.name="MaxListenersExceededWarning",u.emitter=t,u.type=e,u.count=c.length,d=u,"function"==typeof console.warn?console.warn(d):console.log(d)}}else c=a[e]=s,++t._eventsCount;return t}function l(t,e,s){var r=!1;function n(){t.removeListener(e,n),r||(r=!0,s.apply(t,arguments))}return n.listener=s,n}function p(t){var e=this._events;if(e){var s=e[t];if("function"==typeof s)return 1;if(s)return s.length}return 0}function m(t,e){for(var s=new Array(e);e--;)s[e]=t[e];return s}r.prototype=Object.create(null),n.EventEmitter=n,n.usingDomains=!1,n.prototype.domain=void 0,n.prototype._events=void 0,n.prototype._maxListeners=void 0,n.defaultMaxListeners=10,n.init=function(){this.domain=null,n.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new r,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},n.prototype.setMaxListeners=function(t){if("number"!=typeof t||t<0||isNaN(t))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=t,this},n.prototype.getMaxListeners=function(){return o(this)},n.prototype.emit=function(t){var e,s,r,n,o,h,l,p="error"===t;if(h=this._events)p=p&&null==h.error;else if(!p)return!1;if(l=this.domain,p){if(e=arguments[1],!l){if(e instanceof Error)throw e;var m=new Error('Uncaught, unspecified "error" event. ('+e+")");throw m.context=e,m}return e||(e=new Error('Uncaught, unspecified "error" event')),e.domainEmitter=this,e.domain=l,e.domainThrown=!1,l.emit("error",e),!1}if(!(s=h[t]))return!1;var f="function"==typeof s;switch(r=arguments.length){case 1:i(s,f,this);break;case 2:a(s,f,this,arguments[1]);break;case 3:c(s,f,this,arguments[1],arguments[2]);break;case 4:d(s,f,this,arguments[1],arguments[2],arguments[3]);break;default:for(n=new Array(r-1),o=1;o<r;o++)n[o-1]=arguments[o];u(s,f,this,n)}return!0},n.prototype.addListener=function(t,e){return h(this,t,e,!1)},n.prototype.on=n.prototype.addListener,n.prototype.prependListener=function(t,e){return h(this,t,e,!0)},n.prototype.once=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.on(t,l(this,t,e)),this},n.prototype.prependOnceListener=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.prependListener(t,l(this,t,e)),this},n.prototype.removeListener=function(t,e){var s,n,o,i,a;if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(s=n[t]))return this;if(s===e||s.listener&&s.listener===e)0==--this._eventsCount?this._events=new r:(delete n[t],n.removeListener&&this.emit("removeListener",t,s.listener||e));else if("function"!=typeof s){for(o=-1,i=s.length;i-- >0;)if(s[i]===e||s[i].listener&&s[i].listener===e){a=s[i].listener,o=i;break}if(o<0)return this;if(1===s.length){if(s[0]=void 0,0==--this._eventsCount)return this._events=new r,this;delete n[t]}else!function(t,e){for(var s=e,r=s+1,n=t.length;r<n;s+=1,r+=1)t[s]=t[r];t.pop()}(s,o);n.removeListener&&this.emit("removeListener",t,a||e)}return this},n.prototype.off=function(t,e){return this.removeListener(t,e)},n.prototype.removeAllListeners=function(t){var e,s;if(!(s=this._events))return this;if(!s.removeListener)return 0===arguments.length?(this._events=new r,this._eventsCount=0):s[t]&&(0==--this._eventsCount?this._events=new r:delete s[t]),this;if(0===arguments.length){for(var n,o=Object.keys(s),i=0;i<o.length;++i)"removeListener"!==(n=o[i])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new r,this._eventsCount=0,this}if("function"==typeof(e=s[t]))this.removeListener(t,e);else if(e)do{this.removeListener(t,e[e.length-1])}while(e[0]);return this},n.prototype.listeners=function(t){var e,s=this._events;return s&&(e=s[t])?"function"==typeof e?[e.listener||e]:function(t){for(var e=new Array(t.length),s=0;s<e.length;++s)e[s]=t[s].listener||t[s];return e}(e):[]},n.listenerCount=function(t,e){return"function"==typeof t.listenerCount?t.listenerCount(e):p.call(t,e)},n.prototype.listenerCount=p,n.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class f extends n{}!function(t){t.Issue="issue",t.NetworkScoresUpdated="network-scores-updated",t.StatsParsingFinished="stats-parsing-finished"}(t||(t={})),function(t){t.Network="network",t.CPU="cpu",t.Server="server",t.Stream="stream"}(e||(e={})),function(t){t.OutboundNetworkQuality="outbound-network-quality",t.InboundNetworkQuality="inbound-network-quality",t.OutboundNetworkMediaLatency="outbound-network-media-latency",t.InboundNetworkMediaLatency="inbound-network-media-latency",t.NetworkMediaSyncFailure="network-media-sync-failure",t.OutboundNetworkThroughput="outbound-network-throughput",t.InboundNetworkThroughput="inbound-network-throughput",t.EncoderCPUThrottling="encoder-cpu-throttling",t.DecoderCPUThrottling="decoder-cpu-throttling",t.ServerIssue="server-issue",t.UnknownVideoDecoderIssue="unknown-video-decoder",t.LowInboundMOS="low-inbound-mean-opinion-score",t.LowOutboundMOS="low-outbound-mean-opinion-score",t.FrozenVideoTrack="frozen-video-track"}(s||(s={}));class g extends n{static STATS_REPORT_READY_EVENT="stats-report-ready";static STATS_REPORTS_PARSED="stats-reports-parsed";isStopped=!1;reportTimer;getStatsInterval;compositeStatsParser;constructor(t){super(),this.compositeStatsParser=t.compositeStatsParser,this.getStatsInterval=t.getStatsInterval??1e4}get isRunning(){return!!this.reportTimer&&!this.isStopped}startReporting(){if(this.reportTimer)return;const t=()=>setTimeout((()=>{this.isStopped?this.reportTimer=void 0:this.parseReports().finally((()=>{this.reportTimer=t()}))}),this.getStatsInterval);this.isStopped=!1,this.reportTimer=t()}stopReporting(){this.isStopped=!0,this.reportTimer&&(clearTimeout(this.reportTimer),this.reportTimer=void 0)}async parseReports(){const t=Date.now(),e=await this.compositeStatsParser.parse(),s=Date.now()-t;this.emit(g.STATS_REPORTS_PARSED,{timeTaken:s}),e.forEach((t=>{this.emit(g.STATS_REPORT_READY_EVENT,t)}))}}const S=(()=>{const t=new Map;return e=>{const{taskId:s,delayMs:r,maxJitterMs:n,callback:o}=e,i=Math.ceil(Math.random()*(n||0)),a=t.get(s);a&&clearTimeout(a);const c=setTimeout((()=>{o(),t.delete(s)}),r+i);t.set(s,c)}})();class v{#t={};calculate(t){const{connection:{id:e}}=t,{mos:s,stats:r}=this.calculateOutboundScore(t)||{},{mos:n,stats:o}=this.calculateInboundScore(t)||{};return this.#t[e]=t,S({taskId:e,delayMs:35e3,callback:()=>delete this.#t[e]}),{outbound:s,inbound:n,connectionId:e,statsSamples:{inboundStatsSample:o,outboundStatsSample:r}}}calculateOutboundScore(t){const e=[...t.remote?.audio.inbound||[],...t.remote?.video.inbound||[]];if(!e.length)return;const s=this.#t[t.connection.id];if(!s)return;const r=[...s.remote?.audio.inbound||[],...s.remote?.video.inbound||[]],{packetsSent:n}=t.connection,o=s.connection.packetsSent,i=e.reduce(((t,e)=>{const s=r.find((t=>t.ssrc===e.ssrc));return{sumJitter:t.sumJitter+e.jitter,packetsLost:t.packetsLost+e.packetsLost,lastPacketsLost:t.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),a=1e3*t.connection.currentRoundTripTime||0,{sumJitter:c}=i,d=c/e.length,u=n-o,h=i.packetsLost-i.lastPacketsLost,l=u&&h?Math.round(100*h/(u+h)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:l}),stats:{avgJitter:d,rtt:a,packetsLoss:l}}}calculateInboundScore(t){const e=[...t.audio?.inbound,...t.video?.inbound];if(!e.length)return;const s=this.#t[t.connection.id];if(!s)return;const r=[...s.video?.inbound,...s.audio?.inbound],{packetsReceived:n}=t.connection,o=s.connection.packetsReceived,i=e.reduce(((t,e)=>{const s=r.find((t=>t.ssrc===e.ssrc));return{sumJitter:t.sumJitter+e.jitter,packetsLost:t.packetsLost+e.packetsLost,lastPacketsLost:t.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),a=1e3*t.connection.currentRoundTripTime||0,{sumJitter:c}=i,d=c/e.length,u=n-o,h=i.packetsLost-i.lastPacketsLost,l=u&&h?Math.round(100*h/(u+h)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:l}),stats:{avgJitter:d,rtt:a,packetsLoss:l}}}calculateMOS({avgJitter:t,rtt:e,packetsLoss:s}){const r=e+2*t+10;let n=r<160?93.2-r/40:93.2-r/120-10;return n-=2.5*s,1+.035*n+7e-6*n*(n-60)*(100-n)}}class k{#t;#e;constructor(t={}){this.#t=new Map,this.#e=t.statsCleanupTtlMs??35e3}detect(t){const e=this.performDetection(t);return this.performPrevStatsCleanup({connectionId:t.connection.id}),e}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;this.#t.has(e)&&S({taskId:e,delayMs:this.#e,callback:()=>{this.deleteLastProcessedStats(e),"function"==typeof s&&s()}})}setLastProcessedStats(t,e){this.#t.set(t,e)}getLastProcessedStats(t){return this.#t.get(t)}deleteLastProcessedStats(t){this.#t.delete(t)}}class w extends k{#s;constructor(t={}){super(t),this.#s=t.availableOutgoingBitrateThreshold??1e5}performDetection(t){const r=[],{availableOutgoingBitrate:n}=t.connection;if(void 0===n)return r;const o=t.audio.outbound.reduce(((t,e)=>t+e.targetBitrate),0),i=t.video.outbound.reduce(((t,e)=>t+e.bitrate),0);if(!o&&!i)return r;const a={availableOutgoingBitrate:n,videoStreamsTotalBitrate:i,audioStreamsTotalTargetBitrate:o};return o>n||i>0&&n<this.#s?(r.push({statsSample:a,type:e.Network,reason:s.OutboundNetworkThroughput}),r):r}}class b extends k{#r;constructor(t={}){super(t),this.#r=t.framesDroppedThreshold??.5}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const r=t.video.inbound.filter((t=>t.framesDropped>0)),n=[],o=this.getLastProcessedStats(t.connection.id)?.video.inbound;return o?(r.forEach((t=>{const r=o.find((e=>e.ssrc===t.ssrc));if(!r)return;if(t.framesDropped===r.framesDropped)return;const i=t.framesReceived-r.framesReceived,a=t.framesDecoded-r.framesDecoded,c=t.framesDropped-r.framesDropped,d=c/i;if(0===i||0===a)return;const u={deltaFramesDropped:c,deltaFramesReceived:i,deltaFramesDecoded:a,framesDroppedPct:Math.round(100*d)};d>=this.#r&&n.push({statsSample:u,type:e.CPU,reason:s.DecoderCPUThrottling,ssrc:t.ssrc})})),n):n}}class y extends k{#n;constructor(t={}){super(t),this.#n=t.missedFramesThreshold??.15}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const r=t.video.outbound.filter((t=>t.framesEncoded>0)),n=[],o=this.getLastProcessedStats(t.connection.id)?.video.outbound;return o?(r.forEach((t=>{const r=o.find((e=>e.ssrc===t.ssrc));if(!r)return;if(t.framesEncoded===r.framesEncoded)return;const i=t.framesEncoded-r.framesEncoded,a=t.framesSent-r.framesSent,c=1-a/i;if(0===i)return;if(i===a)return;const d={deltaFramesSent:a,deltaFramesEncoded:i,missedFramesPct:Math.round(100*c)};c>=this.#n&&n.push({statsSample:d,type:e.Network,reason:s.OutboundNetworkThroughput,ssrc:t.ssrc})})),n):n}}class P extends k{#o;#i;#a;#c;constructor(t={}){super(),this.#o=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200,this.#a=t.highJitterBufferDelayThresholdMs??500,this.#c=t.highRttThresholdMs??250}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const r=[],n=[...t.audio?.inbound,...t.video?.inbound];if(!n.length)return r;const o=this.getLastProcessedStats(t.connection.id);if(!o)return r;const i=[...o.video?.inbound,...o.audio?.inbound],{packetsReceived:a}=t.connection,c=o.connection.packetsReceived,d=n.reduce(((t,e)=>{const s=i.find((t=>t.ssrc===e.ssrc)),r=s?.jitterBufferDelay||0,n=s?.jitterBufferEmittedCount||0,o=e.jitterBufferDelay-r,a=e.jitterBufferEmittedCount-n,c=o&&a?1e3*o/a:0;return{sumJitter:t.sumJitter+e.jitter,sumJitterBufferDelayMs:t.sumJitterBufferDelayMs+c,packetsLost:t.packetsLost+e.packetsLost,lastPacketsLost:t.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,sumJitterBufferDelayMs:0,packetsLost:0,lastPacketsLost:0}),u=1e3*t.connection.currentRoundTripTime||0,{sumJitter:h,sumJitterBufferDelayMs:l}=d,p=h/n.length,m=l/n.length,f=a-c,g=d.packetsLost-d.lastPacketsLost,S=f&&g?Math.round(100*g/(f+g)):0,v=S>this.#o,k=p>=this.#i,w=u>=this.#c,b=m>this.#a,y=w&&!k&&!v,P=v&&k,L=k&&b,T={rtt:u,packetLossPct:S,avgJitter:p,avgJitterBufferDelay:m};return(k||v)&&r.push({statsSample:T,type:e.Network,reason:s.InboundNetworkQuality,iceCandidate:t.connection.local.id}),y&&r.push({statsSample:T,type:e.Server,reason:s.ServerIssue,iceCandidate:t.connection.remote.id}),P&&r.push({statsSample:T,type:e.Network,reason:s.InboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),L&&r.push({statsSample:T,type:e.Network,reason:s.NetworkMediaSyncFailure,iceCandidate:t.connection.local.id}),r}}class L extends k{#d;constructor(t={}){super(),this.#d=t.correctedSamplesThresholdPct??5}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const r=t.audio.inbound,n=[],o=this.getLastProcessedStats(t.connection.id)?.audio.inbound;return o?(r.forEach((t=>{const r=o.find((e=>e.ssrc===t.ssrc));if(!r)return;const i=t.track.insertedSamplesForDeceleration+t.track.removedSamplesForAcceleration,a=r.track.insertedSamplesForDeceleration+r.track.removedSamplesForAcceleration;if(i===a)return;const c=t.track.totalSamplesReceived-r.track.totalSamplesReceived,d=i-a,u=Math.round(100*d/c),h={correctedSamplesPct:u};u>this.#d&&n.push({statsSample:h,type:e.Network,reason:s.NetworkMediaSyncFailure,ssrc:t.ssrc})})),n):n}}class T extends k{#o;#i;constructor(t={}){super(),this.#o=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const r=[],n=[...t.remote?.audio.inbound||[],...t.remote?.video.inbound||[]];if(!n.length)return r;const o=this.getLastProcessedStats(t.connection.id);if(!o)return r;const i=[...o.remote?.audio.inbound||[],...o.remote?.video.inbound||[]],{packetsSent:a}=t.connection,c=o.connection.packetsSent,d=n.reduce(((t,e)=>{const s=i.find((t=>t.ssrc===e.ssrc));return{sumJitter:t.sumJitter+e.jitter,packetsLost:t.packetsLost+e.packetsLost,lastPacketsLost:t.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,packetsLost:0,lastPacketsLost:0}),u=1e3*t.connection.currentRoundTripTime||0,{sumJitter:h}=d,l=h/n.length,p=a-c,m=d.packetsLost-d.lastPacketsLost,f=p&&m?Math.round(100*m/(p+m)):0,g=f>this.#o,S=l>=this.#i,v=!g&&S||S||g,k={rtt:u,avgJitter:l,packetLossPct:f};return g&&S&&r.push({statsSample:k,type:e.Network,reason:s.OutboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),v&&r.push({statsSample:k,type:e.Network,reason:s.OutboundNetworkQuality,iceCandidate:t.connection.local.id}),r}}class D extends k{performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const r=t.video.outbound.filter((t=>"none"!==t.qualityLimitationReason)),n=[],o=this.getLastProcessedStats(t.connection.id)?.video.outbound;return o?(r.forEach((t=>{const r=o.find((e=>e.ssrc===t.ssrc));if(!r)return;const i={qualityLimitationReason:t.qualityLimitationReason};t.framesSent>r.framesSent||("cpu"===t.qualityLimitationReason&&n.push({statsSample:i,type:e.CPU,reason:s.EncoderCPUThrottling,ssrc:t.ssrc}),"bandwidth"===t.qualityLimitationReason&&n.push({statsSample:i,type:e.Network,reason:s.OutboundNetworkThroughput,ssrc:t.ssrc}))})),n):n}}class C extends k{UNKNOWN_DECODER="unknown";#u={};performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;super.performPrevStatsCleanup({...t,cleanupCallback:()=>{delete this.#u[e],"function"==typeof s&&s()}})}processData(t){const r=[],{id:n}=t.connection,o=this.getLastProcessedStats(n)?.video.inbound;return t.video.inbound.forEach((t=>{const{decoderImplementation:i,ssrc:a}=t,c=o?.find((t=>t.ssrc===a));if(c)if(i===this.UNKNOWN_DECODER){if(!this.hadLastDecoderWithIssue(n,a)){this.setLastDecoderWithIssue(n,a,this.UNKNOWN_DECODER);const o={mimeType:t.mimeType,decoderImplementation:i};r.push({ssrc:a,statsSample:o,type:e.Stream,reason:s.UnknownVideoDecoderIssue,trackIdentifier:t.track.trackIdentifier})}}else this.setLastDecoderWithIssue(n,a,void 0)})),r}setLastDecoderWithIssue(t,e,s){const r=this.#u[t]??{};void 0===s?delete r[e]:r[e]=s,this.#u[t]=r}hadLastDecoderWithIssue(t,e){const s=this.#u[t];return(s&&s[e])===this.UNKNOWN_DECODER}}class R extends k{#h=new Map;#l;#r;constructor(t={}){super(),this.#l=t.timeoutMs??1e4,this.#r=t.framesDroppedThreshold??.5}performDetection(t){const{connection:{id:e}}=t,s=this.processData(t);return this.setLastProcessedStats(e,t),s}processData(t){const{connection:{id:r}}=t,n=this.getLastProcessedStats(r),o=[];if(!n)return o;const{video:{inbound:i}}=t,{video:{inbound:a}}=n,c=t=>new Map(t.map((t=>[t.track.trackIdentifier,t]))),d=c(i),u=c(a),h=new Set(this.#h.keys());return Array.from(d.entries()).forEach((([t,r])=>{h.delete(t);const n=u.get(t);if(!n)return;const i=r.framesReceived-n.framesReceived,a=r.framesDropped-n.framesDropped,c=r.framesDecoded-n.framesDecoded;if(0===i)return;if(a/i>=this.#r)return;if(c>0)return void this.removeMarkIssue(t);if(!this.markIssue(t))return;const d={framesReceived:r.framesReceived,framesDropped:r.framesDropped,framesDecoded:r.framesDecoded,deltaFramesReceived:i,deltaFramesDropped:a,deltaFramesDecoded:c};o.push({statsSample:d,type:e.Stream,reason:s.FrozenVideoTrack,trackIdentifier:t})})),h.forEach((t=>{this.removeMarkIssue(t)})),o}markIssue(t){const e=Date.now(),s=this.#h.get(t);return s?!(e-s<this.#l):(this.#h.set(t,e),!1)}removeMarkIssue(t){this.#h.delete(t)}}const I=t=>"closed"===t.iceConnectionState||"closed"===t.connectionState,E=(t,e,s)=>8*((t,e,s)=>{if(!e)return 0;const r=t[s],n=e[s];if(null==r||null==n)return 0;const o=Math.floor(t.timestamp)-Math.floor(e.timestamp);return 0===o?0:(Number(r)-Number(n))/o*1e3})(t,e,s);class M{connections=[];statsParser;constructor(t){this.statsParser=t.statsParser}listConnections(){return[...this.connections]}addPeerConnection(t){this.connections.push({id:t.id??String(Date.now()+Math.random().toString(32)),pc:t.pc})}removePeerConnection(t){const e=this.connections.findIndex((({pc:e})=>e===t.pc));e>=0&&this.removeConnectionsByIndexes([e])}async parse(){const t=[],e=this.connections.map((async(e,s)=>{if(!I(e.pc))return this.statsParser.parse(e);t.unshift(s)}));t.length&&this.removeConnectionsByIndexes(t);return(await Promise.all(e)).filter((t=>void 0!==t))}removeConnectionsByIndexes(t){t.forEach((t=>{this.connections.splice(t,1)}))}}class N{prevStats=new Map;allowedReportTypes=new Set(["candidate-pair","inbound-rtp","outbound-rtp","remote-outbound-rtp","remote-inbound-rtp","track","transport"]);ignoreSSRCList;logger;constructor(t){this.ignoreSSRCList=t.ignoreSSRCList??[],this.logger=t.logger}get previouslyParsedStatsConnectionsIds(){return[...this.prevStats.keys()]}async parse(t){if(!I(t.pc))return this.getConnectionStats(t);this.logger.debug("Skip stats parsing. Connection is closed.",{connection:t})}async getConnectionStats(t){const{pc:e,id:s}=t;try{const r=Date.now(),n=e.getReceivers().filter((t=>t.track?.enabled)),o=e.getSenders().filter((t=>t.track?.enabled)),i=await Promise.all(n.map((t=>t.getStats()))),a=await Promise.all(o.map((t=>t.getStats())));return{id:s,stats:this.mapReportsStats([...i,...a],t),timeTaken:Date.now()-r}}catch(t){return void this.logger.error("Failed to get stats for PC",{id:s,pc:e,error:t})}}mapReportsStats(t,e){const s={audio:{inbound:[],outbound:[]},video:{inbound:[],outbound:[]},connection:{},remote:{video:{inbound:[],outbound:[]},audio:{inbound:[],outbound:[]}}};t.forEach((t=>{t.forEach((e=>{this.allowedReportTypes.has(e.type)&&this.updateMappedStatsWithReportItemData(e,s,t)}))}));const{id:r}=e,n=this.prevStats.get(r);return n&&this.propagateStatsWithRateValues(s,n.stats),this.prevStats.set(r,{stats:s,ts:Date.now()}),S({taskId:r,delayMs:35e3,callback:()=>this.prevStats.delete(r)}),s}updateMappedStatsWithReportItemData(t,e,s){const r=t.type;if("candidate-pair"===r&&"succeeded"===t.state&&t.nominated)return void(e.connection=this.prepareConnectionStats(t,s));const n=this.getMediaType(t);if(!n)return;const o=t.ssrc;if(!o||!this.ignoreSSRCList.includes(o))if("outbound-rtp"!==r)if("inbound-rtp"!==r)"remote-outbound-rtp"!==r?"remote-inbound-rtp"===r&&(this.mapConnectionStatsIfNecessary(e,t,s),e.remote[n].inbound.push({...t})):e.remote[n].outbound.push({...t});else{const r=s.get(t.trackId)||s.get(t.mediaSourceId)||{};this.mapConnectionStatsIfNecessary(e,t,s);const o={...t,track:{...r}};e[n].inbound.push(o)}else{const r=s.get(t.trackId)||s.get(t.mediaSourceId)||{},o={...t,track:{...r}};e[n].outbound.push(o)}}getMediaType(t){const e=t.mediaType||t.kind;if(!["audio","video"].includes(e)){const{id:e}=t;if(!e)return;return String(e).includes("Video")?"video":String(e).includes("Audio")?"audio":void 0}return e}propagateStatsWithRateValues(t,e){t.audio.inbound.forEach((t=>{const s=e.audio.inbound.find((({id:e})=>e===t.id));t.bitrate=E(t,s,"bytesReceived"),t.packetRate=E(t,s,"packetsReceived")})),t.audio.outbound.forEach((t=>{const s=e.audio.outbound.find((({id:e})=>e===t.id));t.bitrate=E(t,s,"bytesSent"),t.packetRate=E(t,s,"packetsSent")})),t.video.inbound.forEach((t=>{const s=e.video.inbound.find((({id:e})=>e===t.id));t.bitrate=E(t,s,"bytesReceived"),t.packetRate=E(t,s,"packetsReceived")})),t.video.outbound.forEach((t=>{const s=e.video.outbound.find((({id:e})=>e===t.id));t.bitrate=E(t,s,"bytesSent"),t.packetRate=E(t,s,"packetsSent")}))}mapConnectionStatsIfNecessary(t,e,s){if(t.connection.id||!e.transportId)return;const r=s.get(e.transportId);if(r&&r.selectedCandidatePairId){const e=s.get(r.selectedCandidatePairId);t.connection=this.prepareConnectionStats(e,s)}}prepareConnectionStats(t,e){if(!t||!e)return{};const s={...t};if(s.remoteCandidateId){const t=e.get(s.remoteCandidateId);s.remote={...t}}if(s.localCandidateId){const t=e.get(s.localCandidateId);s.local={...t}}return s}}class _{eventEmitter;#p=!1;detectors=[];networkScoresCalculator;statsReporter;compositeStatsParser;logger;autoAddPeerConnections;constructor(e){this.logger=e.logger??{debug:()=>{},info:()=>{},warn:()=>{},error:()=>{}},this.eventEmitter=e.issueEmitter??new f,e.onIssues&&this.eventEmitter.on(t.Issue,e.onIssues),e.onNetworkScoresUpdated&&this.eventEmitter.on(t.NetworkScoresUpdated,e.onNetworkScoresUpdated),this.detectors=e.detectors??[new D,new b,new y,new P,new T,new L,new w,new C,new R],this.networkScoresCalculator=e.networkScoresCalculator??new v,this.compositeStatsParser=e.compositeStatsParser??new M({statsParser:new N({ignoreSSRCList:e.ignoreSSRCList,logger:this.logger})}),this.statsReporter=e.statsReporter??new g({compositeStatsParser:this.compositeStatsParser,getStatsInterval:e.getStatsInterval??5e3}),window.wid=this,this.autoAddPeerConnections=e.autoAddPeerConnections??!0,this.autoAddPeerConnections&&this.wrapRTCPeerConnection(),this.statsReporter.on(g.STATS_REPORT_READY_EVENT,(t=>{this.detectIssues({data:t.stats}),this.calculateNetworkScores(t.stats)})),this.statsReporter.on(g.STATS_REPORTS_PARSED,(e=>{const s={timeTaken:e.timeTaken,ts:Date.now()};this.eventEmitter.emit(t.StatsParsingFinished,s)}))}watchNewPeerConnections(){if(!this.autoAddPeerConnections)throw new Error("Auto add peer connections was disabled in the constructor.");this.#p?this.logger.warn("WebRTCIssueDetector is already started. Skip processing"):(this.logger.info("Start watching peer connections"),this.#p=!0,this.statsReporter.startReporting())}stopWatchingNewPeerConnections(){this.#p?(this.logger.info("Stop watching peer connections"),this.#p=!1,this.statsReporter.stopReporting()):this.logger.warn("WebRTCIssueDetector is already stopped. Skip processing")}handleNewPeerConnection(t){this.#p||!this.autoAddPeerConnections?(this.#p||!1!==this.autoAddPeerConnections||(this.logger.info("Starting stats reporting for new peer connection"),this.#p=!0,this.statsReporter.startReporting()),this.logger.debug("Handling new peer connection",t),this.compositeStatsParser.addPeerConnection({pc:t})):this.logger.debug("Skip handling new peer connection. Detector is not running",t)}emitIssues(e){this.eventEmitter.emit(t.Issue,e)}detectIssues({data:t}){const e=this.detectors.reduce(((e,s)=>[...e,...s.detect(t)]),[]);e.length>0&&this.emitIssues(e)}calculateNetworkScores(e){const s=this.networkScoresCalculator.calculate(e);this.eventEmitter.emit(t.NetworkScoresUpdated,s)}wrapRTCPeerConnection(){if(!window.RTCPeerConnection)return void this.logger.warn("No RTCPeerConnection found in browser window. Skipping");const t=window.RTCPeerConnection,e=t=>this.handleNewPeerConnection(t);function s(s){const r=new t(s);return e(r),r}s.prototype=t.prototype,window.RTCPeerConnection=s}}export{w as AvailableOutgoingBitrateIssueDetector,k as BaseIssueDetector,M as CompositeRTCStatsParser,t as EventType,b as FramesDroppedIssueDetector,y as FramesEncodedSentIssueDetector,R as FrozenVideoTrackDetector,P as InboundNetworkIssueDetector,s as IssueReason,e as IssueType,L as NetworkMediaSyncIssueDetector,v as NetworkScoresCalculator,T as OutboundNetworkIssueDetector,g as PeriodicWebRTCStatsReporter,D as QualityLimitationsIssueDetector,N as RTCStatsParser,C as UnknownVideoDecoderImplementationDetector,f as WebRTCIssueEmitter,_ as default};

@@ -10,2 +10,2 @@ export { default as BaseIssueDetector } from './BaseIssueDetector';

export { default as UnknownVideoDecoderImplementationDetector } from './UnknownVideoDecoderImplementationDetector';
export { default as DeadVideoTrackDetector } from './DeadVideoTrackDetector';
export { default as FrozenVideoTrackDetector } from './FrozenVideoTrackDetector';

@@ -71,3 +71,3 @@ import PeriodicWebRTCStatsReporter from './parser/PeriodicWebRTCStatsReporter';

LowOutboundMOS = "low-outbound-mean-opinion-score",
DeadVideoTrack = "dead-video-track"
FrozenVideoTrack = "frozen-video-track"
}

@@ -74,0 +74,0 @@ export declare type IssuePayload = {

{
"name": "webrtc-issue-detector",
"version": "1.12.0-LD-6968-dead-video-track-detect.1",
"version": "1.12.0-LD-6968-dead-video-track-detect.2",
"description": "WebRTC diagnostic tool that detects issues with network or user devices",

@@ -5,0 +5,0 @@ "repository": "git@github.com:VLprojects/webrtc-issue-detector.git",

@@ -61,3 +61,3 @@ # webrtc-issue-detector

UnknownVideoDecoderImplementationDetector,
DeadVideoTrackDetector,
FrozenVideoTrackDetector,
} from 'webrtc-issue-detector';

@@ -79,3 +79,3 @@

new UnknownVideoDecoderImplementationDetector(),
new DeadVideoTrackDetector(),
new FrozenVideoTrackDetector(),
],

@@ -82,0 +82,0 @@ getStatsInterval: 10_000, // set custom stats parsing interval

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc