webrtc-issue-detector
Advanced tools
Comparing version 1.14.0-debug-detectors.4 to 1.14.0-debug-detectors.5
@@ -1,1 +0,1 @@ | ||
"use strict";var t,e,s,o;function r(){}function n(){n.init.call(this)}function i(t){return void 0===t._maxListeners?n.defaultMaxListeners:t._maxListeners}function a(t,e,s){if(e)t.call(s);else for(var o=t.length,r=m(t,o),n=0;n<o;++n)r[n].call(s)}function c(t,e,s,o){if(e)t.call(s,o);else for(var r=t.length,n=m(t,r),i=0;i<r;++i)n[i].call(s,o)}function u(t,e,s,o,r){if(e)t.call(s,o,r);else for(var n=t.length,i=m(t,n),a=0;a<n;++a)i[a].call(s,o,r)}function d(t,e,s,o,r,n){if(e)t.call(s,o,r,n);else for(var i=t.length,a=m(t,i),c=0;c<i;++c)a[c].call(s,o,r,n)}function h(t,e,s,o){if(e)t.apply(s,o);else for(var r=t.length,n=m(t,r),i=0;i<r;++i)n[i].apply(s,o)}function l(t,e,s,o){var n,a,c,u;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]=o?[s,c]:[c,s]:o?c.unshift(s):c.push(s),!c.warned&&(n=i(t))&&n>0&&c.length>n){c.warned=!0;var d=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+e+" listeners added. Use emitter.setMaxListeners() to increase limit");d.name="MaxListenersExceededWarning",d.emitter=t,d.type=e,d.count=c.length,u=d,"function"==typeof console.warn?console.warn(u):console.log(u)}}else c=a[e]=s,++t._eventsCount;return t}function p(t,e,s){var o=!1;function r(){t.removeListener(e,r),o||(o=!0,s.apply(t,arguments))}return r.listener=s,r}function f(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}Object.defineProperty(exports,"__esModule",{value:!0}),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 i(this)},n.prototype.emit=function(t){var e,s,o,r,n,i,l,p="error"===t;if(i=this._events)p=p&&null==i.error;else if(!p)return!1;if(l=this.domain,p){if(e=arguments[1],!l){if(e instanceof Error)throw e;var f=new Error('Uncaught, unspecified "error" event. ('+e+")");throw f.context=e,f}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=i[t]))return!1;var m="function"==typeof s;switch(o=arguments.length){case 1:a(s,m,this);break;case 2:c(s,m,this,arguments[1]);break;case 3:u(s,m,this,arguments[1],arguments[2]);break;case 4:d(s,m,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];h(s,m,this,r)}return!0},n.prototype.addListener=function(t,e){return l(this,t,e,!1)},n.prototype.on=n.prototype.addListener,n.prototype.prependListener=function(t,e){return l(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,p(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,p(this,t,e)),this},n.prototype.removeListener=function(t,e){var s,o,n,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 r:(delete o[t],o.removeListener&&this.emit("removeListener",t,s.listener||e));else if("function"!=typeof s){for(n=-1,i=s.length;i-- >0;)if(s[i]===e||s[i].listener&&s[i].listener===e){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 r,this;delete o[t]}else!function(t,e){for(var s=e,o=s+1,r=t.length;o<r;s+=1,o+=1)t[s]=t[o];t.pop()}(s,n);o.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 o,n=Object.keys(s),i=0;i<n.length;++i)"removeListener"!==(o=n[i])&&this.removeAllListeners(o);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):f.call(t,e)},n.prototype.listenerCount=f,n.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class g extends n{}exports.EventType=void 0,(t=exports.EventType||(exports.EventType={})).Issue="issue",t.NetworkScoresUpdated="network-scores-updated",t.StatsParsingFinished="stats-parsing-finished",exports.IssueType=void 0,(e=exports.IssueType||(exports.IssueType={})).Network="network",e.CPU="cpu",e.Server="server",e.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",exports.MosQuality=void 0,(o=exports.MosQuality||(exports.MosQuality={}))[o.BAD=2.1]="BAD",o[o.POOR=2.6]="POOR",o[o.FAIR=3.1]="FAIR",o[o.GOOD=3.8]="GOOD",o[o.EXCELLENT=4.3]="EXCELLENT";class S 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(S.STATS_REPORTS_PARSED,{timeTaken:s,reportItems:e}),e.forEach((t=>{this.emit(S.STATS_REPORT_READY_EVENT,t)}))}}const v=(()=>{const t=new Map;return e=>{const{taskId:s,delayMs:o,maxJitterMs:r,callback:n}=e,i=Math.ceil(Math.random()*(r||0)),a=t.get(s);a&&clearTimeout(a);const c=setTimeout((()=>{n(),t.delete(s)}),o+i);t.set(s,c)}})();class k{#t={};calculate(t){const{connection:{id:e}}=t,{mos:s,stats:o}=this.calculateOutboundScore(t)||{},{mos:r,stats:n}=this.calculateInboundScore(t)||{};return this.#t[e]=t,v({taskId:e,delayMs:35e3,callback:()=>delete this.#t[e]}),{outbound:s,inbound:r,connectionId:e,statsSamples:{inboundStatsSample:n,outboundStatsSample:o}}}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 o=[...s.remote?.audio.inbound||[],...s.remote?.video.inbound||[]],{packetsSent:r}=t.connection,n=s.connection.packetsSent,i=e.reduce(((t,e)=>{const s=o.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,u=c/e.length,d=r-n,h=i.packetsLost-i.lastPacketsLost,l=d&&h?Math.round(100*h/(d+h)):0;return{mos:this.calculateMOS({avgJitter:u,rtt:a,packetsLoss:l}),stats:{avgJitter:u,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 o=[...s.video?.inbound,...s.audio?.inbound],{packetsReceived:r}=t.connection,n=s.connection.packetsReceived,i=e.reduce(((t,e)=>{const s=o.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,u=c/e.length,d=r-n,h=i.packetsLost-i.lastPacketsLost,l=d&&h?Math.round(100*h/(d+h)):0;return{mos:this.calculateMOS({avgJitter:u,rtt:a,packetsLoss:l}),stats:{avgJitter:u,rtt:a,packetsLoss:l}}}calculateMOS({avgJitter:t,rtt:e,packetsLoss:s}){const o=e+2*t+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 T{#e=new Map;#s;#o;constructor(t={}){this.#s=t.statsCleanupTtlMs??35e3,this.#o=t.maxParsedStatsStorageSize??5}detect(t,e){const s={...t,networkScores:{...e,statsSamples:e?.statsSamples||{}}},o=this.performDetection(s);return this.setLastProcessedStats(t.connection.id,s),this.performPrevStatsCleanup({connectionId:t.connection.id}),o}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;this.#e.has(e)&&v({taskId:e,delayMs:this.#s,callback:()=>{this.deleteLastProcessedStats(e),"function"==typeof s&&s()}})}setLastProcessedStats(t,e){if(!t||e.connection.id!==t)return;const s=this.#e.get(t)??[];s.push(e),s.length>this.#o&&s.shift(),this.#e.set(t,s)}getLastProcessedStats(t){const e=this.#e.get(t);return e?.[e.length-1]}getAllLastProcessedStats(t){return this.#e.get(t)??[]}deleteLastProcessedStats(t){this.#e.delete(t)}}class y extends T{#r;constructor(t={}){super(t),this.#r=t.availableOutgoingBitrateThreshold??1e5}performDetection(t){const e=[],{availableOutgoingBitrate:s}=t.connection;if(void 0===s)return e;const o=t.audio.outbound.reduce(((t,e)=>t+e.targetBitrate),0),r=t.video.outbound.reduce(((t,e)=>t+e.bitrate),0);if(!o&&!r)return e;const n={availableOutgoingBitrate:s,videoStreamsTotalBitrate:r,audioStreamsTotalTargetBitrate:o};return o>s||r>0&&s<this.#r?(e.push({statsSample:n,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput}),e):e}}class w extends T{#n;#i;#a;#c;constructor(t={}){super(),this.#n=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200,this.#a=t.highJitterBufferDelayThresholdMs??500,this.#c=t.highRttThresholdMs??250}performDetection(t){return this.processData(t)}processData(t){const e=[],s=[...t.audio?.inbound,...t.video?.inbound];if(!s.length)return e;const o=this.getLastProcessedStats(t.connection.id);if(!o)return e;const r=[...o.video?.inbound,...o.audio?.inbound],{packetsReceived:n}=t.connection,i=o.connection.packetsReceived,a=s.reduce(((t,e)=>{const s=r.find((t=>t.ssrc===e.ssrc)),o=s?.jitterBufferDelay||0,n=s?.jitterBufferEmittedCount||0,i=e.jitterBufferDelay-o,a=e.jitterBufferEmittedCount-n,c=i&&a?1e3*i/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}),c=1e3*t.connection.currentRoundTripTime||0,{sumJitter:u,sumJitterBufferDelayMs:d}=a,h=u/s.length,l=d/s.length,p=n-i,f=a.packetsLost-a.lastPacketsLost,m=p&&f?Math.round(100*f/(p+f)):0,g=m>this.#n,S=h>=this.#i,v=c>=this.#c,k=l>this.#a,T=v&&!S&&!g,y=g&&S,w=S&&k,b={rtt:c,packetLossPct:m,avgJitter:h,avgJitterBufferDelay:l};return(S||g)&&e.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.InboundNetworkQuality,iceCandidate:t.connection.local.id}),T&&e.push({statsSample:b,type:exports.IssueType.Server,reason:exports.IssueReason.ServerIssue,iceCandidate:t.connection.remote.id}),y&&e.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.InboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),w&&e.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.NetworkMediaSyncFailure,iceCandidate:t.connection.local.id}),e}}class b extends T{#u;constructor(t={}){super(),this.#u=t.correctedSamplesThresholdPct??5}performDetection(t){return this.processData(t)}processData(t){const e=t.audio.inbound,s=[],o=this.getLastProcessedStats(t.connection.id)?.audio.inbound;return o?(e.forEach((t=>{const e=o.find((e=>e.ssrc===t.ssrc));if(!e)return;const r=t.track.insertedSamplesForDeceleration+t.track.removedSamplesForAcceleration,n=e.track.insertedSamplesForDeceleration+e.track.removedSamplesForAcceleration;if(r===n)return;const i=t.track.totalSamplesReceived-e.track.totalSamplesReceived,a=r-n,c=Math.round(100*a/i),u={correctedSamplesPct:c};c>this.#u&&s.push({statsSample:u,type:exports.IssueType.Network,reason:exports.IssueReason.NetworkMediaSyncFailure,ssrc:t.ssrc})})),s):s}}class P extends T{#n;#i;constructor(t={}){super(),this.#n=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200}performDetection(t){return this.processData(t)}processData(t){const e=[],s=[...t.remote?.audio.inbound||[],...t.remote?.video.inbound||[]];if(!s.length)return e;const o=this.getLastProcessedStats(t.connection.id);if(!o)return e;const r=[...o.remote?.audio.inbound||[],...o.remote?.video.inbound||[]],{packetsSent:n}=t.connection,i=o.connection.packetsSent,a=s.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}),c=1e3*t.connection.currentRoundTripTime||0,{sumJitter:u}=a,d=u/s.length,h=n-i,l=a.packetsLost-a.lastPacketsLost,p=h&&l?Math.round(100*l/(h+l)):0,f=p>this.#n,m=d>=this.#i,g=!f&&m||m||f,S={rtt:c,avgJitter:d,packetLossPct:p};return f&&m&&e.push({statsSample:S,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),g&&e.push({statsSample:S,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkQuality,iceCandidate:t.connection.local.id}),e}}class L extends T{performDetection(t){return this.processData(t)}processData(t){const e=t.video.outbound.filter((t=>"none"!==t.qualityLimitationReason)),s=[],o=this.getLastProcessedStats(t.connection.id)?.video.outbound;return o?(e.forEach((t=>{const e=o.find((e=>e.ssrc===t.ssrc));if(!e)return;const r={qualityLimitationReason:t.qualityLimitationReason};t.framesSent>e.framesSent||("cpu"===t.qualityLimitationReason&&s.push({statsSample:r,type:exports.IssueType.CPU,reason:exports.IssueReason.EncoderCPUThrottling,ssrc:t.ssrc}),"bandwidth"===t.qualityLimitationReason&&s.push({statsSample:r,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput,ssrc:t.ssrc}))})),s):s}}class I extends T{UNKNOWN_DECODER="unknown";#d={};performDetection(t){return this.processData(t)}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;super.performPrevStatsCleanup({...t,cleanupCallback:()=>{delete this.#d[e],"function"==typeof s&&s()}})}processData(t){const e=[],{id:s}=t.connection,o=this.getLastProcessedStats(s)?.video.inbound;return t.video.inbound.forEach((t=>{const{decoderImplementation:r,ssrc:n}=t,i=o?.find((t=>t.ssrc===n));if(i)if(r===this.UNKNOWN_DECODER){if(!this.hadLastDecoderWithIssue(s,n)){this.setLastDecoderWithIssue(s,n,this.UNKNOWN_DECODER);const o={mimeType:t.mimeType,decoderImplementation:r};e.push({ssrc:n,statsSample:o,type:exports.IssueType.Stream,reason:exports.IssueReason.UnknownVideoDecoderIssue,trackIdentifier:t.track.trackIdentifier})}}else this.setLastDecoderWithIssue(s,n,void 0)})),e}setLastDecoderWithIssue(t,e,s){const o=this.#d[t]??{};void 0===s?delete o[e]:o[e]=s,this.#d[t]=o}hadLastDecoderWithIssue(t,e){const s=this.#d[t];return(s&&s[e])===this.UNKNOWN_DECODER}}const R=(t,e)=>{for(let s=1;s<e.length;s+=1){const o=e[s].video.inbound.find((e=>e.ssrc===t));if(!o)continue;const r=e[s-1].video.inbound.find((e=>e.ssrc===t)),n=o.frameWidth!==r?.frameWidth,i=o.frameHeight!==r?.frameHeight;if(n||i)return!0}return!1};class C extends T{#h;#l;constructor(t={}){super(),this.#h=t.avgFreezeDurationThresholdMs??1e3,this.#l=t.frozenDurationThresholdPct??30}performDetection(t){const e=t.networkScores.inbound;return void 0!==e&&e<=exports.MosQuality.BAD?[]:this.processData(t)}processData(t){const e=[],s=this.getAllLastProcessedStats(t.connection.id);if(0===s.length)return[];const o=t.video.inbound.map((e=>{const o=s[s.length-1].video.inbound.find((t=>t.ssrc===e.ssrc));if(!o)return;if(R(e.ssrc,[s[s.length-1],t]))return;const r=e.freezeCount-(o.freezeCount??0),n=1e3*(e.totalFreezesDuration-(o.totalFreezesDuration??0)),i=r>0?n/r:0,a=n/(e.timestamp-o.timestamp)*100;return a>this.#l||i>this.#h?{ssrc:e.ssrc,avgFreezeDurationMs:i,frozenDurationPct:a}:void 0})).filter((t=>void 0!==t));return o.length>0&&e.push({type:exports.IssueType.Stream,reason:exports.IssueReason.FrozenVideoTrack,statsSample:{ssrcs:o.map((t=>t.ssrc))}}),e}}class D extends T{#p;#f;constructor(t={}){super(t),this.#p=t.volatilityThreshold??8,this.#f=t.affectedStreamsPercentThreshold??50}performDetection(t){return[...this.getAllLastProcessedStats(t.connection.id),t].find((t=>void 0!==t.networkScores.inbound&&t.networkScores.inbound<=exports.MosQuality.BAD))?[]:this.processData(t)}processData(t){const e=[],s=[...this.getAllLastProcessedStats(t.connection.id),t],o=t.video.inbound.map((t=>{if(R(t.ssrc,s))return;if(s.length<5)return;const e=[];for(let o=1;o<s.length;o+=1){const r=s[o].video.inbound.find((e=>e.ssrc===t.ssrc));r&&e.push(r.framesPerSecond)}const o=e.reduce(((t,e)=>t+e),0)/e.length,r=100*(e.reduce(((t,e)=>t+Math.abs(e-o)),0)/e.length)/o;return console.log("THROTTLE",{volatility:r,allFps:e}),r>this.#p?(console.log("THROTTLE DETECTED on Single stream"),{ssrc:t.ssrc,allFps:e,volatility:r}):void 0})).filter((t=>Boolean(t))),r=o.length/(t.video.inbound.length/100);return console.log("THROTTLE AFFECTION",{affectedStreamsPercent:r}),r>this.#f&&(console.log("THROTTLE DETECTED !!!!"),e.push({type:exports.IssueType.CPU,reason:exports.IssueReason.DecoderCPUThrottling,statsSample:{affectedStreamsPercent:r,throtthedStreams:o}}),this.deleteLastProcessedStats(t.connection.id)),e}}const x=t=>"closed"===t.iceConnectionState||"closed"===t.connectionState,E=(t,e,s)=>8*((t,e,s)=>{if(!e)return 0;const o=t[s],r=e[s];if(null==o||null==r)return 0;const n=Math.floor(t.timestamp)-Math.floor(e.timestamp);return 0===n?0:(Number(o)-Number(r))/n*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(!x(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(!x(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 o=Date.now(),r=e.getReceivers().filter((t=>t.track?.enabled)),n=e.getSenders().filter((t=>t.track?.enabled)),i=await Promise.all(r.map((t=>t.getStats()))),a=await Promise.all(n.map((t=>t.getStats())));return{id:s,stats:this.mapReportsStats([...i,...a],t),timeTaken:Date.now()-o}}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:o}=e,r=this.prevStats.get(o);return r&&this.propagateStatsWithRateValues(s,r.stats),this.prevStats.set(o,{stats:s,ts:Date.now()}),v({taskId:o,delayMs:35e3,callback:()=>this.prevStats.delete(o)}),s}updateMappedStatsWithReportItemData(t,e,s){const o=t.type;if("candidate-pair"===o&&"succeeded"===t.state&&t.nominated)return void(e.connection=this.prepareConnectionStats(t,s));const r=this.getMediaType(t);if(!r)return;const n=t.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(e,t,s),e.remote[r].inbound.push({...t})):e.remote[r].outbound.push({...t});else{const o=s.get(t.trackId)||s.get(t.mediaSourceId)||{};this.mapConnectionStatsIfNecessary(e,t,s);const n={...t,track:{...o}};e[r].inbound.push(n)}else{const o=s.get(t.trackId)||s.get(t.mediaSourceId)||{},n={...t,track:{...o}};e[r].outbound.push(n)}}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 o=s.get(e.transportId);if(o&&o.selectedCandidatePairId){const e=s.get(o.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}}exports.AvailableOutgoingBitrateIssueDetector=y,exports.BaseIssueDetector=T,exports.CompositeRTCStatsParser=M,exports.FrozenVideoTrackDetector=C,exports.InboundNetworkIssueDetector=w,exports.NetworkMediaSyncIssueDetector=b,exports.NetworkScoresCalculator=k,exports.OutboundNetworkIssueDetector=P,exports.PeriodicWebRTCStatsReporter=S,exports.QualityLimitationsIssueDetector=L,exports.RTCStatsParser=N,exports.UnknownVideoDecoderImplementationDetector=I,exports.VideoDecoderIssueDetector=D,exports.WebRTCIssueEmitter=g,exports.default=class{eventEmitter;#m=!1;detectors=[];networkScoresCalculator;statsReporter;compositeStatsParser;logger;autoAddPeerConnections;constructor(t){this.logger=t.logger??{debug:()=>{},info:()=>{},warn:()=>{},error:()=>{}},this.eventEmitter=t.issueEmitter??new g,t.onIssues&&this.eventEmitter.on(exports.EventType.Issue,t.onIssues),t.onNetworkScoresUpdated&&this.eventEmitter.on(exports.EventType.NetworkScoresUpdated,t.onNetworkScoresUpdated),this.detectors=t.detectors??[new L,new w,new P,new b,new y,new I,new C,new D],this.networkScoresCalculator=t.networkScoresCalculator??new k,this.compositeStatsParser=t.compositeStatsParser??new M({statsParser:new N({ignoreSSRCList:t.ignoreSSRCList,logger:this.logger})}),this.statsReporter=t.statsReporter??new S({compositeStatsParser:this.compositeStatsParser,getStatsInterval:t.getStatsInterval??5e3}),window.wid=this,this.autoAddPeerConnections=t.autoAddPeerConnections??!0,this.autoAddPeerConnections&&this.wrapRTCPeerConnection(),this.statsReporter.on(S.STATS_REPORT_READY_EVENT,(t=>{const e=this.calculateNetworkScores(t.stats);this.detectIssues({data:t.stats},e)})),this.statsReporter.on(S.STATS_REPORTS_PARSED,(e=>{const s={timeTaken:e.timeTaken,ts:Date.now()};t.onStats&&t.onStats(e.reportItems),this.eventEmitter.emit(exports.EventType.StatsParsingFinished,s)}))}watchNewPeerConnections(){if(!this.autoAddPeerConnections)throw new Error("Auto add peer connections was disabled in the constructor.");this.#m?this.logger.warn("WebRTCIssueDetector is already started. Skip processing"):(this.logger.info("Start watching peer connections"),this.#m=!0,this.statsReporter.startReporting())}stopWatchingNewPeerConnections(){this.#m?(this.logger.info("Stop watching peer connections"),this.#m=!1,this.statsReporter.stopReporting()):this.logger.warn("WebRTCIssueDetector is already stopped. Skip processing")}handleNewPeerConnection(t,e){this.#m||!this.autoAddPeerConnections?(this.#m||!1!==this.autoAddPeerConnections||(this.logger.info("Starting stats reporting for new peer connection"),this.#m=!0,this.statsReporter.startReporting()),this.logger.debug("Handling new peer connection",t),this.compositeStatsParser.addPeerConnection({pc:t,id:e})):this.logger.debug("Skip handling new peer connection. Detector is not running",t)}emitIssues(t){this.eventEmitter.emit(exports.EventType.Issue,t)}detectIssues({data:t},e){const s=this.detectors.reduce(((s,o)=>[...s,...o.detect(t,e)]),[]);s.length>0&&this.emitIssues(s)}calculateNetworkScores(t){const e=this.networkScoresCalculator.calculate(t);return this.eventEmitter.emit(exports.EventType.NetworkScoresUpdated,e),e}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 o=new t(s);return e(o),o}s.prototype=t.prototype,window.RTCPeerConnection=s}}; | ||
"use strict";var t,e,s,o;function r(){}function n(){n.init.call(this)}function i(t){return void 0===t._maxListeners?n.defaultMaxListeners:t._maxListeners}function a(t,e,s){if(e)t.call(s);else for(var o=t.length,r=m(t,o),n=0;n<o;++n)r[n].call(s)}function c(t,e,s,o){if(e)t.call(s,o);else for(var r=t.length,n=m(t,r),i=0;i<r;++i)n[i].call(s,o)}function u(t,e,s,o,r){if(e)t.call(s,o,r);else for(var n=t.length,i=m(t,n),a=0;a<n;++a)i[a].call(s,o,r)}function d(t,e,s,o,r,n){if(e)t.call(s,o,r,n);else for(var i=t.length,a=m(t,i),c=0;c<i;++c)a[c].call(s,o,r,n)}function h(t,e,s,o){if(e)t.apply(s,o);else for(var r=t.length,n=m(t,r),i=0;i<r;++i)n[i].apply(s,o)}function l(t,e,s,o){var n,a,c,u;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]=o?[s,c]:[c,s]:o?c.unshift(s):c.push(s),!c.warned&&(n=i(t))&&n>0&&c.length>n){c.warned=!0;var d=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+e+" listeners added. Use emitter.setMaxListeners() to increase limit");d.name="MaxListenersExceededWarning",d.emitter=t,d.type=e,d.count=c.length,u=d,"function"==typeof console.warn?console.warn(u):console.log(u)}}else c=a[e]=s,++t._eventsCount;return t}function p(t,e,s){var o=!1;function r(){t.removeListener(e,r),o||(o=!0,s.apply(t,arguments))}return r.listener=s,r}function f(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}Object.defineProperty(exports,"__esModule",{value:!0}),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 i(this)},n.prototype.emit=function(t){var e,s,o,r,n,i,l,p="error"===t;if(i=this._events)p=p&&null==i.error;else if(!p)return!1;if(l=this.domain,p){if(e=arguments[1],!l){if(e instanceof Error)throw e;var f=new Error('Uncaught, unspecified "error" event. ('+e+")");throw f.context=e,f}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=i[t]))return!1;var m="function"==typeof s;switch(o=arguments.length){case 1:a(s,m,this);break;case 2:c(s,m,this,arguments[1]);break;case 3:u(s,m,this,arguments[1],arguments[2]);break;case 4:d(s,m,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];h(s,m,this,r)}return!0},n.prototype.addListener=function(t,e){return l(this,t,e,!1)},n.prototype.on=n.prototype.addListener,n.prototype.prependListener=function(t,e){return l(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,p(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,p(this,t,e)),this},n.prototype.removeListener=function(t,e){var s,o,n,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 r:(delete o[t],o.removeListener&&this.emit("removeListener",t,s.listener||e));else if("function"!=typeof s){for(n=-1,i=s.length;i-- >0;)if(s[i]===e||s[i].listener&&s[i].listener===e){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 r,this;delete o[t]}else!function(t,e){for(var s=e,o=s+1,r=t.length;o<r;s+=1,o+=1)t[s]=t[o];t.pop()}(s,n);o.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 o,n=Object.keys(s),i=0;i<n.length;++i)"removeListener"!==(o=n[i])&&this.removeAllListeners(o);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):f.call(t,e)},n.prototype.listenerCount=f,n.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class g extends n{}exports.EventType=void 0,(t=exports.EventType||(exports.EventType={})).Issue="issue",t.NetworkScoresUpdated="network-scores-updated",t.StatsParsingFinished="stats-parsing-finished",exports.IssueType=void 0,(e=exports.IssueType||(exports.IssueType={})).Network="network",e.CPU="cpu",e.Server="server",e.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",exports.MosQuality=void 0,(o=exports.MosQuality||(exports.MosQuality={}))[o.BAD=2.1]="BAD",o[o.POOR=2.6]="POOR",o[o.FAIR=3.1]="FAIR",o[o.GOOD=3.8]="GOOD",o[o.EXCELLENT=4.3]="EXCELLENT";class S 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(S.STATS_REPORTS_PARSED,{timeTaken:s,reportItems:e}),e.forEach((t=>{this.emit(S.STATS_REPORT_READY_EVENT,t)}))}}const v=(()=>{const t=new Map;return e=>{const{taskId:s,delayMs:o,maxJitterMs:r,callback:n}=e,i=Math.ceil(Math.random()*(r||0)),a=t.get(s);a&&clearTimeout(a);const c=setTimeout((()=>{n(),t.delete(s)}),o+i);t.set(s,c)}})();class k{#t={};calculate(t){const{connection:{id:e}}=t,{mos:s,stats:o}=this.calculateOutboundScore(t)||{},{mos:r,stats:n}=this.calculateInboundScore(t)||{};return this.#t[e]=t,v({taskId:e,delayMs:35e3,callback:()=>delete this.#t[e]}),{outbound:s,inbound:r,connectionId:e,statsSamples:{inboundStatsSample:n,outboundStatsSample:o}}}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 o=[...s.remote?.audio.inbound||[],...s.remote?.video.inbound||[]],{packetsSent:r}=t.connection,n=s.connection.packetsSent,i=e.reduce(((t,e)=>{const s=o.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,u=c/e.length,d=r-n,h=i.packetsLost-i.lastPacketsLost,l=d&&h?Math.round(100*h/(d+h)):0;return{mos:this.calculateMOS({avgJitter:u,rtt:a,packetsLoss:l}),stats:{avgJitter:u,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 o=[...s.video?.inbound,...s.audio?.inbound],{packetsReceived:r}=t.connection,n=s.connection.packetsReceived,i=e.reduce(((t,e)=>{const s=o.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,u=c/e.length,d=r-n,h=i.packetsLost-i.lastPacketsLost,l=d&&h?Math.round(100*h/(d+h)):0;return{mos:this.calculateMOS({avgJitter:u,rtt:a,packetsLoss:l}),stats:{avgJitter:u,rtt:a,packetsLoss:l}}}calculateMOS({avgJitter:t,rtt:e,packetsLoss:s}){const o=e+2*t+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 T{#e=new Map;#s;#o;constructor(t={}){this.#s=t.statsCleanupTtlMs??35e3,this.#o=t.maxParsedStatsStorageSize??5}detect(t,e){const s={...t,networkScores:{...e,statsSamples:e?.statsSamples||{}}},o=this.performDetection(s);return this.setLastProcessedStats(t.connection.id,s),this.performPrevStatsCleanup({connectionId:t.connection.id}),o}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;this.#e.has(e)&&v({taskId:e,delayMs:this.#s,callback:()=>{this.deleteLastProcessedStats(e),"function"==typeof s&&s()}})}setLastProcessedStats(t,e){if(!t||e.connection.id!==t)return;const s=this.#e.get(t)??[];s.push(e),s.length>this.#o&&s.shift(),this.#e.set(t,s)}getLastProcessedStats(t){const e=this.#e.get(t);return e?.[e.length-1]}getAllLastProcessedStats(t){return this.#e.get(t)??[]}deleteLastProcessedStats(t){this.#e.delete(t)}}class y extends T{#r;constructor(t={}){super(t),this.#r=t.availableOutgoingBitrateThreshold??1e5}performDetection(t){const e=[],{availableOutgoingBitrate:s}=t.connection;if(void 0===s)return e;const o=t.audio.outbound.reduce(((t,e)=>t+e.targetBitrate),0),r=t.video.outbound.reduce(((t,e)=>t+e.bitrate),0);if(!o&&!r)return e;const n={availableOutgoingBitrate:s,videoStreamsTotalBitrate:r,audioStreamsTotalTargetBitrate:o};return o>s||r>0&&s<this.#r?(e.push({statsSample:n,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput}),e):e}}class w extends T{#n;#i;#a;#c;constructor(t={}){super(),this.#n=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200,this.#a=t.highJitterBufferDelayThresholdMs??500,this.#c=t.highRttThresholdMs??250}performDetection(t){return this.processData(t)}processData(t){const e=[],s=[...t.audio?.inbound,...t.video?.inbound];if(!s.length)return e;const o=this.getLastProcessedStats(t.connection.id);if(!o)return e;const r=[...o.video?.inbound,...o.audio?.inbound],{packetsReceived:n}=t.connection,i=o.connection.packetsReceived,a=s.reduce(((t,e)=>{const s=r.find((t=>t.ssrc===e.ssrc)),o=s?.jitterBufferDelay||0,n=s?.jitterBufferEmittedCount||0,i=e.jitterBufferDelay-o,a=e.jitterBufferEmittedCount-n,c=i&&a?1e3*i/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}),c=1e3*t.connection.currentRoundTripTime||0,{sumJitter:u,sumJitterBufferDelayMs:d}=a,h=u/s.length,l=d/s.length,p=n-i,f=a.packetsLost-a.lastPacketsLost,m=p&&f?Math.round(100*f/(p+f)):0,g=m>this.#n,S=h>=this.#i,v=c>=this.#c,k=l>this.#a,T=v&&!S&&!g,y=g&&S,w=S&&k,b={rtt:c,packetLossPct:m,avgJitter:h,avgJitterBufferDelay:l};return(S||g)&&e.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.InboundNetworkQuality,iceCandidate:t.connection.local.id}),T&&e.push({statsSample:b,type:exports.IssueType.Server,reason:exports.IssueReason.ServerIssue,iceCandidate:t.connection.remote.id}),y&&e.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.InboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),w&&e.push({statsSample:b,type:exports.IssueType.Network,reason:exports.IssueReason.NetworkMediaSyncFailure,iceCandidate:t.connection.local.id}),e}}class b extends T{#u;constructor(t={}){super(),this.#u=t.correctedSamplesThresholdPct??5}performDetection(t){return this.processData(t)}processData(t){const e=t.audio.inbound,s=[],o=this.getLastProcessedStats(t.connection.id)?.audio.inbound;return o?(e.forEach((t=>{const e=o.find((e=>e.ssrc===t.ssrc));if(!e)return;const r=t.track.insertedSamplesForDeceleration+t.track.removedSamplesForAcceleration,n=e.track.insertedSamplesForDeceleration+e.track.removedSamplesForAcceleration;if(r===n)return;const i=t.track.totalSamplesReceived-e.track.totalSamplesReceived,a=r-n,c=Math.round(100*a/i),u={correctedSamplesPct:c};c>this.#u&&s.push({statsSample:u,type:exports.IssueType.Network,reason:exports.IssueReason.NetworkMediaSyncFailure,ssrc:t.ssrc})})),s):s}}class P extends T{#n;#i;constructor(t={}){super(),this.#n=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200}performDetection(t){return this.processData(t)}processData(t){const e=[],s=[...t.remote?.audio.inbound||[],...t.remote?.video.inbound||[]];if(!s.length)return e;const o=this.getLastProcessedStats(t.connection.id);if(!o)return e;const r=[...o.remote?.audio.inbound||[],...o.remote?.video.inbound||[]],{packetsSent:n}=t.connection,i=o.connection.packetsSent,a=s.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}),c=1e3*t.connection.currentRoundTripTime||0,{sumJitter:u}=a,d=u/s.length,h=n-i,l=a.packetsLost-a.lastPacketsLost,p=h&&l?Math.round(100*l/(h+l)):0,f=p>this.#n,m=d>=this.#i,g=!f&&m||m||f,S={rtt:c,avgJitter:d,packetLossPct:p};return f&&m&&e.push({statsSample:S,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),g&&e.push({statsSample:S,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkQuality,iceCandidate:t.connection.local.id}),e}}class L extends T{performDetection(t){return this.processData(t)}processData(t){const e=t.video.outbound.filter((t=>"none"!==t.qualityLimitationReason)),s=[],o=this.getLastProcessedStats(t.connection.id)?.video.outbound;return o?(e.forEach((t=>{const e=o.find((e=>e.ssrc===t.ssrc));if(!e)return;const r={qualityLimitationReason:t.qualityLimitationReason};t.framesSent>e.framesSent||("cpu"===t.qualityLimitationReason&&s.push({statsSample:r,type:exports.IssueType.CPU,reason:exports.IssueReason.EncoderCPUThrottling,ssrc:t.ssrc}),"bandwidth"===t.qualityLimitationReason&&s.push({statsSample:r,type:exports.IssueType.Network,reason:exports.IssueReason.OutboundNetworkThroughput,ssrc:t.ssrc}))})),s):s}}class I extends T{UNKNOWN_DECODER="unknown";#d={};performDetection(t){return this.processData(t)}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;super.performPrevStatsCleanup({...t,cleanupCallback:()=>{delete this.#d[e],"function"==typeof s&&s()}})}processData(t){const e=[],{id:s}=t.connection,o=this.getLastProcessedStats(s)?.video.inbound;return t.video.inbound.forEach((t=>{const{decoderImplementation:r,ssrc:n}=t,i=o?.find((t=>t.ssrc===n));if(i)if(r===this.UNKNOWN_DECODER){if(!this.hadLastDecoderWithIssue(s,n)){this.setLastDecoderWithIssue(s,n,this.UNKNOWN_DECODER);const o={mimeType:t.mimeType,decoderImplementation:r};e.push({ssrc:n,statsSample:o,type:exports.IssueType.Stream,reason:exports.IssueReason.UnknownVideoDecoderIssue,trackIdentifier:t.track.trackIdentifier})}}else this.setLastDecoderWithIssue(s,n,void 0)})),e}setLastDecoderWithIssue(t,e,s){const o=this.#d[t]??{};void 0===s?delete o[e]:o[e]=s,this.#d[t]=o}hadLastDecoderWithIssue(t,e){const s=this.#d[t];return(s&&s[e])===this.UNKNOWN_DECODER}}const R=(t,e)=>{for(let s=1;s<e.length;s+=1){const o=e[s].video.inbound.find((e=>e.ssrc===t));if(!o)continue;const r=e[s-1].video.inbound.find((e=>e.ssrc===t)),n=o.frameWidth!==r?.frameWidth,i=o.frameHeight!==r?.frameHeight;if(n||i)return!0}return!1};class C extends T{#h;#l;constructor(t={}){super(),this.#h=t.avgFreezeDurationThresholdMs??1e3,this.#l=t.frozenDurationThresholdPct??30}performDetection(t){const e=t.networkScores.inbound;return void 0!==e&&e<=exports.MosQuality.BAD?[]:this.processData(t)}processData(t){const e=[],s=this.getAllLastProcessedStats(t.connection.id);if(0===s.length)return[];const o=t.video.inbound.map((e=>{const o=s[s.length-1].video.inbound.find((t=>t.ssrc===e.ssrc));if(!o)return;if(R(e.ssrc,[s[s.length-1],t]))return;const r=e.freezeCount-(o.freezeCount??0),n=1e3*(e.totalFreezesDuration-(o.totalFreezesDuration??0)),i=r>0?n/r:0,a=n/(e.timestamp-o.timestamp)*100;return a>this.#l||i>this.#h?{ssrc:e.ssrc,avgFreezeDurationMs:i,frozenDurationPct:a}:void 0})).filter((t=>void 0!==t));return o.length>0&&e.push({type:exports.IssueType.Stream,reason:exports.IssueReason.FrozenVideoTrack,statsSample:{ssrcs:o.map((t=>t.ssrc))}}),e}}class D extends T{#p;#f;constructor(t={}){super(t),this.#p=t.volatilityThreshold??8,this.#f=t.affectedStreamsPercentThreshold??30}performDetection(t){return[...this.getAllLastProcessedStats(t.connection.id),t].find((t=>void 0!==t.networkScores.inbound&&t.networkScores.inbound<=exports.MosQuality.BAD))?[]:this.processData(t)}processData(t){const e=[],s=[...this.getAllLastProcessedStats(t.connection.id),t],o=t.video.inbound.map((t=>{if(R(t.ssrc,s))return;if(s.length<5)return;const e=[];for(let o=0;o<s.length-1;o+=1){const r=s[o].video.inbound.find((e=>e.ssrc===t.ssrc));void 0!==r?.framesPerSecond&&e.push(r.framesPerSecond)}const o=e.reduce(((t,e)=>t+e),0)/e.length,r=100*(e.reduce(((t,e)=>t+Math.abs(e-o)),0)/e.length)/o;return console.log("THROTTLE",{ssrc:t.ssrc,volatility:r,allFps:e}),r>this.#p?(console.log("THROTTLE DETECTED on Single stream",t.ssrc),{ssrc:t.ssrc,allFps:e,volatility:r}):void 0})).filter((t=>Boolean(t)));if(0===o.length)return e;const r=o.length/(t.video.inbound.length/100);return console.log("THROTTLE AFFECTION",{affectedStreamsPercent:r}),r>this.#f&&(console.log("THROTTLE DETECTED !!!!"),e.push({type:exports.IssueType.CPU,reason:exports.IssueReason.DecoderCPUThrottling,statsSample:{affectedStreamsPercent:r,throtthedStreams:o}}),this.deleteLastProcessedStats(t.connection.id)),e}}const x=t=>"closed"===t.iceConnectionState||"closed"===t.connectionState,E=(t,e,s)=>8*((t,e,s)=>{if(!e)return 0;const o=t[s],r=e[s];if(null==o||null==r)return 0;const n=Math.floor(t.timestamp)-Math.floor(e.timestamp);return 0===n?0:(Number(o)-Number(r))/n*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(!x(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(!x(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 o=Date.now(),r=e.getReceivers().filter((t=>t.track?.enabled)),n=e.getSenders().filter((t=>t.track?.enabled)),i=await Promise.all(r.map((t=>t.getStats()))),a=await Promise.all(n.map((t=>t.getStats())));return{id:s,stats:this.mapReportsStats([...i,...a],t),timeTaken:Date.now()-o}}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:o}=e,r=this.prevStats.get(o);return r&&this.propagateStatsWithRateValues(s,r.stats),this.prevStats.set(o,{stats:s,ts:Date.now()}),v({taskId:o,delayMs:35e3,callback:()=>this.prevStats.delete(o)}),s}updateMappedStatsWithReportItemData(t,e,s){const o=t.type;if("candidate-pair"===o&&"succeeded"===t.state&&t.nominated)return void(e.connection=this.prepareConnectionStats(t,s));const r=this.getMediaType(t);if(!r)return;const n=t.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(e,t,s),e.remote[r].inbound.push({...t})):e.remote[r].outbound.push({...t});else{const o=s.get(t.trackId)||s.get(t.mediaSourceId)||{};this.mapConnectionStatsIfNecessary(e,t,s);const n={...t,track:{...o}};e[r].inbound.push(n)}else{const o=s.get(t.trackId)||s.get(t.mediaSourceId)||{},n={...t,track:{...o}};e[r].outbound.push(n)}}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 o=s.get(e.transportId);if(o&&o.selectedCandidatePairId){const e=s.get(o.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}}exports.AvailableOutgoingBitrateIssueDetector=y,exports.BaseIssueDetector=T,exports.CompositeRTCStatsParser=M,exports.FrozenVideoTrackDetector=C,exports.InboundNetworkIssueDetector=w,exports.NetworkMediaSyncIssueDetector=b,exports.NetworkScoresCalculator=k,exports.OutboundNetworkIssueDetector=P,exports.PeriodicWebRTCStatsReporter=S,exports.QualityLimitationsIssueDetector=L,exports.RTCStatsParser=N,exports.UnknownVideoDecoderImplementationDetector=I,exports.VideoDecoderIssueDetector=D,exports.WebRTCIssueEmitter=g,exports.default=class{eventEmitter;#m=!1;detectors=[];networkScoresCalculator;statsReporter;compositeStatsParser;logger;autoAddPeerConnections;constructor(t){this.logger=t.logger??{debug:()=>{},info:()=>{},warn:()=>{},error:()=>{}},this.eventEmitter=t.issueEmitter??new g,t.onIssues&&this.eventEmitter.on(exports.EventType.Issue,t.onIssues),t.onNetworkScoresUpdated&&this.eventEmitter.on(exports.EventType.NetworkScoresUpdated,t.onNetworkScoresUpdated),this.detectors=t.detectors??[new L,new w,new P,new b,new y,new I,new C,new D],this.networkScoresCalculator=t.networkScoresCalculator??new k,this.compositeStatsParser=t.compositeStatsParser??new M({statsParser:new N({ignoreSSRCList:t.ignoreSSRCList,logger:this.logger})}),this.statsReporter=t.statsReporter??new S({compositeStatsParser:this.compositeStatsParser,getStatsInterval:t.getStatsInterval??5e3}),window.wid=this,this.autoAddPeerConnections=t.autoAddPeerConnections??!0,this.autoAddPeerConnections&&this.wrapRTCPeerConnection(),this.statsReporter.on(S.STATS_REPORT_READY_EVENT,(t=>{const e=this.calculateNetworkScores(t.stats);this.detectIssues({data:t.stats},e)})),this.statsReporter.on(S.STATS_REPORTS_PARSED,(e=>{const s={timeTaken:e.timeTaken,ts:Date.now()};t.onStats&&t.onStats(e.reportItems),this.eventEmitter.emit(exports.EventType.StatsParsingFinished,s)}))}watchNewPeerConnections(){if(!this.autoAddPeerConnections)throw new Error("Auto add peer connections was disabled in the constructor.");this.#m?this.logger.warn("WebRTCIssueDetector is already started. Skip processing"):(this.logger.info("Start watching peer connections"),this.#m=!0,this.statsReporter.startReporting())}stopWatchingNewPeerConnections(){this.#m?(this.logger.info("Stop watching peer connections"),this.#m=!1,this.statsReporter.stopReporting()):this.logger.warn("WebRTCIssueDetector is already stopped. Skip processing")}handleNewPeerConnection(t,e){this.#m||!this.autoAddPeerConnections?(this.#m||!1!==this.autoAddPeerConnections||(this.logger.info("Starting stats reporting for new peer connection"),this.#m=!0,this.statsReporter.startReporting()),this.logger.debug("Handling new peer connection",t),this.compositeStatsParser.addPeerConnection({pc:t,id:e})):this.logger.debug("Skip handling new peer connection. Detector is not running",t)}emitIssues(t){this.eventEmitter.emit(exports.EventType.Issue,t)}detectIssues({data:t},e){const s=this.detectors.reduce(((s,o)=>[...s,...o.detect(t,e)]),[]);s.length>0&&this.emitIssues(s)}calculateNetworkScores(t){const e=this.networkScoresCalculator.calculate(t);return this.eventEmitter.emit(exports.EventType.NetworkScoresUpdated,e),e}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 o=new t(s);return e(o),o}s.prototype=t.prototype,window.RTCPeerConnection=s}}; |
@@ -1,1 +0,1 @@ | ||
var t,e,s,n;function o(){}function r(){r.init.call(this)}function i(t){return void 0===t._maxListeners?r.defaultMaxListeners:t._maxListeners}function a(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 c(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 d(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 u(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 h(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 l(t,e,s,n){var r,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 o,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&&(r=i(t))&&r>0&&c.length>r){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 p(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 f(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}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(t){if("number"!=typeof t||t<0||isNaN(t))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=t,this},r.prototype.getMaxListeners=function(){return i(this)},r.prototype.emit=function(t){var e,s,n,o,r,i,l,p="error"===t;if(i=this._events)p=p&&null==i.error;else if(!p)return!1;if(l=this.domain,p){if(e=arguments[1],!l){if(e instanceof Error)throw e;var f=new Error('Uncaught, unspecified "error" event. ('+e+")");throw f.context=e,f}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=i[t]))return!1;var m="function"==typeof s;switch(n=arguments.length){case 1:a(s,m,this);break;case 2:c(s,m,this,arguments[1]);break;case 3:d(s,m,this,arguments[1],arguments[2]);break;case 4:u(s,m,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];h(s,m,this,o)}return!0},r.prototype.addListener=function(t,e){return l(this,t,e,!1)},r.prototype.on=r.prototype.addListener,r.prototype.prependListener=function(t,e){return l(this,t,e,!0)},r.prototype.once=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.on(t,p(this,t,e)),this},r.prototype.prependOnceListener=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.prependListener(t,p(this,t,e)),this},r.prototype.removeListener=function(t,e){var s,n,r,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 o:(delete n[t],n.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 o,this;delete n[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);n.removeListener&&this.emit("removeListener",t,a||e)}return this},r.prototype.off=function(t,e){return this.removeListener(t,e)},r.prototype.removeAllListeners=function(t){var e,s;if(!(s=this._events))return this;if(!s.removeListener)return 0===arguments.length?(this._events=new o,this._eventsCount=0):s[t]&&(0==--this._eventsCount?this._events=new o:delete s[t]),this;if(0===arguments.length){for(var n,r=Object.keys(s),i=0;i<r.length;++i)"removeListener"!==(n=r[i])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new o,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},r.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):[]},r.listenerCount=function(t,e){return"function"==typeof t.listenerCount?t.listenerCount(e):f.call(t,e)},r.prototype.listenerCount=f,r.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class g extends r{}!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={})),function(t){t[t.BAD=2.1]="BAD",t[t.POOR=2.6]="POOR",t[t.FAIR=3.1]="FAIR",t[t.GOOD=3.8]="GOOD",t[t.EXCELLENT=4.3]="EXCELLENT"}(n||(n={}));class S extends r{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(S.STATS_REPORTS_PARSED,{timeTaken:s,reportItems:e}),e.forEach((t=>{this.emit(S.STATS_REPORT_READY_EVENT,t)}))}}const v=(()=>{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 k{#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,v({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 w{#e=new Map;#s;#n;constructor(t={}){this.#s=t.statsCleanupTtlMs??35e3,this.#n=t.maxParsedStatsStorageSize??5}detect(t,e){const s={...t,networkScores:{...e,statsSamples:e?.statsSamples||{}}},n=this.performDetection(s);return this.setLastProcessedStats(t.connection.id,s),this.performPrevStatsCleanup({connectionId:t.connection.id}),n}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;this.#e.has(e)&&v({taskId:e,delayMs:this.#s,callback:()=>{this.deleteLastProcessedStats(e),"function"==typeof s&&s()}})}setLastProcessedStats(t,e){if(!t||e.connection.id!==t)return;const s=this.#e.get(t)??[];s.push(e),s.length>this.#n&&s.shift(),this.#e.set(t,s)}getLastProcessedStats(t){const e=this.#e.get(t);return e?.[e.length-1]}getAllLastProcessedStats(t){return this.#e.get(t)??[]}deleteLastProcessedStats(t){this.#e.delete(t)}}class T extends w{#o;constructor(t={}){super(t),this.#o=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.#o?(n.push({statsSample:a,type:e.Network,reason:s.OutboundNetworkThroughput}),n):n}}class b extends w{#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){return this.processData(t)}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,f=l/o.length,m=a-c,g=d.packetsLost-d.lastPacketsLost,S=m&&g?Math.round(100*g/(m+g)):0,v=S>this.#r,k=p>=this.#i,w=u>=this.#c,T=f>this.#a,b=w&&!k&&!v,P=v&&k,y=k&&T,L={rtt:u,packetLossPct:S,avgJitter:p,avgJitterBufferDelay:f};return(k||v)&&n.push({statsSample:L,type:e.Network,reason:s.InboundNetworkQuality,iceCandidate:t.connection.local.id}),b&&n.push({statsSample:L,type:e.Server,reason:s.ServerIssue,iceCandidate:t.connection.remote.id}),P&&n.push({statsSample:L,type:e.Network,reason:s.InboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),y&&n.push({statsSample:L,type:e.Network,reason:s.NetworkMediaSyncFailure,iceCandidate:t.connection.local.id}),n}}class P extends w{#d;constructor(t={}){super(),this.#d=t.correctedSamplesThresholdPct??5}performDetection(t){return this.processData(t)}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 y extends w{#r;#i;constructor(t={}){super(),this.#r=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200}performDetection(t){return this.processData(t)}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,f=d.packetsLost-d.lastPacketsLost,m=p&&f?Math.round(100*f/(p+f)):0,g=m>this.#r,S=l>=this.#i,v=!g&&S||S||g,k={rtt:u,avgJitter:l,packetLossPct:m};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 L extends w{performDetection(t){return this.processData(t)}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 w{UNKNOWN_DECODER="unknown";#u={};performDetection(t){return this.processData(t)}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}}const D=(t,e)=>{for(let s=1;s<e.length;s+=1){const n=e[s].video.inbound.find((e=>e.ssrc===t));if(!n)continue;const o=e[s-1].video.inbound.find((e=>e.ssrc===t)),r=n.frameWidth!==o?.frameWidth,i=n.frameHeight!==o?.frameHeight;if(r||i)return!0}return!1};class R extends w{#h;#l;constructor(t={}){super(),this.#h=t.avgFreezeDurationThresholdMs??1e3,this.#l=t.frozenDurationThresholdPct??30}performDetection(t){const e=t.networkScores.inbound;return void 0!==e&&e<=n.BAD?[]:this.processData(t)}processData(t){const n=[],o=this.getAllLastProcessedStats(t.connection.id);if(0===o.length)return[];const r=t.video.inbound.map((e=>{const s=o[o.length-1].video.inbound.find((t=>t.ssrc===e.ssrc));if(!s)return;if(D(e.ssrc,[o[o.length-1],t]))return;const n=e.freezeCount-(s.freezeCount??0),r=1e3*(e.totalFreezesDuration-(s.totalFreezesDuration??0)),i=n>0?r/n:0,a=r/(e.timestamp-s.timestamp)*100;return a>this.#l||i>this.#h?{ssrc:e.ssrc,avgFreezeDurationMs:i,frozenDurationPct:a}:void 0})).filter((t=>void 0!==t));return r.length>0&&n.push({type:e.Stream,reason:s.FrozenVideoTrack,statsSample:{ssrcs:r.map((t=>t.ssrc))}}),n}}class E extends w{#p;#f;constructor(t={}){super(t),this.#p=t.volatilityThreshold??8,this.#f=t.affectedStreamsPercentThreshold??50}performDetection(t){return[...this.getAllLastProcessedStats(t.connection.id),t].find((t=>void 0!==t.networkScores.inbound&&t.networkScores.inbound<=n.BAD))?[]:this.processData(t)}processData(t){const n=[],o=[...this.getAllLastProcessedStats(t.connection.id),t],r=t.video.inbound.map((t=>{if(D(t.ssrc,o))return;if(o.length<5)return;const e=[];for(let s=1;s<o.length;s+=1){const n=o[s].video.inbound.find((e=>e.ssrc===t.ssrc));n&&e.push(n.framesPerSecond)}const s=e.reduce(((t,e)=>t+e),0)/e.length,n=100*(e.reduce(((t,e)=>t+Math.abs(e-s)),0)/e.length)/s;return console.log("THROTTLE",{volatility:n,allFps:e}),n>this.#p?(console.log("THROTTLE DETECTED on Single stream"),{ssrc:t.ssrc,allFps:e,volatility:n}):void 0})).filter((t=>Boolean(t))),i=r.length/(t.video.inbound.length/100);return console.log("THROTTLE AFFECTION",{affectedStreamsPercent:i}),i>this.#f&&(console.log("THROTTLE DETECTED !!!!"),n.push({type:e.CPU,reason:s.DecoderCPUThrottling,statsSample:{affectedStreamsPercent:i,throtthedStreams:r}}),this.deleteLastProcessedStats(t.connection.id)),n}}const I=t=>"closed"===t.iceConnectionState||"closed"===t.connectionState,M=(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 N{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 O{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()}),v({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=M(t,s,"bytesReceived"),t.packetRate=M(t,s,"packetsReceived")})),t.audio.outbound.forEach((t=>{const s=e.audio.outbound.find((({id:e})=>e===t.id));t.bitrate=M(t,s,"bytesSent"),t.packetRate=M(t,s,"packetsSent")})),t.video.inbound.forEach((t=>{const s=e.video.inbound.find((({id:e})=>e===t.id));t.bitrate=M(t,s,"bytesReceived"),t.packetRate=M(t,s,"packetsReceived")})),t.video.outbound.forEach((t=>{const s=e.video.outbound.find((({id:e})=>e===t.id));t.bitrate=M(t,s,"bytesSent"),t.packetRate=M(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;#m=!1;detectors=[];networkScoresCalculator;statsReporter;compositeStatsParser;logger;autoAddPeerConnections;constructor(e){this.logger=e.logger??{debug:()=>{},info:()=>{},warn:()=>{},error:()=>{}},this.eventEmitter=e.issueEmitter??new g,e.onIssues&&this.eventEmitter.on(t.Issue,e.onIssues),e.onNetworkScoresUpdated&&this.eventEmitter.on(t.NetworkScoresUpdated,e.onNetworkScoresUpdated),this.detectors=e.detectors??[new L,new b,new y,new P,new T,new C,new R,new E],this.networkScoresCalculator=e.networkScoresCalculator??new k,this.compositeStatsParser=e.compositeStatsParser??new N({statsParser:new O({ignoreSSRCList:e.ignoreSSRCList,logger:this.logger})}),this.statsReporter=e.statsReporter??new S({compositeStatsParser:this.compositeStatsParser,getStatsInterval:e.getStatsInterval??5e3}),window.wid=this,this.autoAddPeerConnections=e.autoAddPeerConnections??!0,this.autoAddPeerConnections&&this.wrapRTCPeerConnection(),this.statsReporter.on(S.STATS_REPORT_READY_EVENT,(t=>{const e=this.calculateNetworkScores(t.stats);this.detectIssues({data:t.stats},e)})),this.statsReporter.on(S.STATS_REPORTS_PARSED,(s=>{const n={timeTaken:s.timeTaken,ts:Date.now()};e.onStats&&e.onStats(s.reportItems),this.eventEmitter.emit(t.StatsParsingFinished,n)}))}watchNewPeerConnections(){if(!this.autoAddPeerConnections)throw new Error("Auto add peer connections was disabled in the constructor.");this.#m?this.logger.warn("WebRTCIssueDetector is already started. Skip processing"):(this.logger.info("Start watching peer connections"),this.#m=!0,this.statsReporter.startReporting())}stopWatchingNewPeerConnections(){this.#m?(this.logger.info("Stop watching peer connections"),this.#m=!1,this.statsReporter.stopReporting()):this.logger.warn("WebRTCIssueDetector is already stopped. Skip processing")}handleNewPeerConnection(t,e){this.#m||!this.autoAddPeerConnections?(this.#m||!1!==this.autoAddPeerConnections||(this.logger.info("Starting stats reporting for new peer connection"),this.#m=!0,this.statsReporter.startReporting()),this.logger.debug("Handling new peer connection",t),this.compositeStatsParser.addPeerConnection({pc:t,id:e})):this.logger.debug("Skip handling new peer connection. Detector is not running",t)}emitIssues(e){this.eventEmitter.emit(t.Issue,e)}detectIssues({data:t},e){const s=this.detectors.reduce(((s,n)=>[...s,...n.detect(t,e)]),[]);s.length>0&&this.emitIssues(s)}calculateNetworkScores(e){const s=this.networkScoresCalculator.calculate(e);return this.eventEmitter.emit(t.NetworkScoresUpdated,s),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{T as AvailableOutgoingBitrateIssueDetector,w as BaseIssueDetector,N as CompositeRTCStatsParser,t as EventType,R as FrozenVideoTrackDetector,b as InboundNetworkIssueDetector,s as IssueReason,e as IssueType,n as MosQuality,P as NetworkMediaSyncIssueDetector,k as NetworkScoresCalculator,y as OutboundNetworkIssueDetector,S as PeriodicWebRTCStatsReporter,L as QualityLimitationsIssueDetector,O as RTCStatsParser,C as UnknownVideoDecoderImplementationDetector,E as VideoDecoderIssueDetector,g as WebRTCIssueEmitter,_ as default}; | ||
var t,e,s,n;function o(){}function r(){r.init.call(this)}function i(t){return void 0===t._maxListeners?r.defaultMaxListeners:t._maxListeners}function a(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 c(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 d(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 u(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 h(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 l(t,e,s,n){var r,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 o,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&&(r=i(t))&&r>0&&c.length>r){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 p(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 f(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}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(t){if("number"!=typeof t||t<0||isNaN(t))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=t,this},r.prototype.getMaxListeners=function(){return i(this)},r.prototype.emit=function(t){var e,s,n,o,r,i,l,p="error"===t;if(i=this._events)p=p&&null==i.error;else if(!p)return!1;if(l=this.domain,p){if(e=arguments[1],!l){if(e instanceof Error)throw e;var f=new Error('Uncaught, unspecified "error" event. ('+e+")");throw f.context=e,f}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=i[t]))return!1;var m="function"==typeof s;switch(n=arguments.length){case 1:a(s,m,this);break;case 2:c(s,m,this,arguments[1]);break;case 3:d(s,m,this,arguments[1],arguments[2]);break;case 4:u(s,m,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];h(s,m,this,o)}return!0},r.prototype.addListener=function(t,e){return l(this,t,e,!1)},r.prototype.on=r.prototype.addListener,r.prototype.prependListener=function(t,e){return l(this,t,e,!0)},r.prototype.once=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.on(t,p(this,t,e)),this},r.prototype.prependOnceListener=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.prependListener(t,p(this,t,e)),this},r.prototype.removeListener=function(t,e){var s,n,r,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 o:(delete n[t],n.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 o,this;delete n[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);n.removeListener&&this.emit("removeListener",t,a||e)}return this},r.prototype.off=function(t,e){return this.removeListener(t,e)},r.prototype.removeAllListeners=function(t){var e,s;if(!(s=this._events))return this;if(!s.removeListener)return 0===arguments.length?(this._events=new o,this._eventsCount=0):s[t]&&(0==--this._eventsCount?this._events=new o:delete s[t]),this;if(0===arguments.length){for(var n,r=Object.keys(s),i=0;i<r.length;++i)"removeListener"!==(n=r[i])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new o,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},r.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):[]},r.listenerCount=function(t,e){return"function"==typeof t.listenerCount?t.listenerCount(e):f.call(t,e)},r.prototype.listenerCount=f,r.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class g extends r{}!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={})),function(t){t[t.BAD=2.1]="BAD",t[t.POOR=2.6]="POOR",t[t.FAIR=3.1]="FAIR",t[t.GOOD=3.8]="GOOD",t[t.EXCELLENT=4.3]="EXCELLENT"}(n||(n={}));class S extends r{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(S.STATS_REPORTS_PARSED,{timeTaken:s,reportItems:e}),e.forEach((t=>{this.emit(S.STATS_REPORT_READY_EVENT,t)}))}}const v=(()=>{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 k{#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,v({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 w{#e=new Map;#s;#n;constructor(t={}){this.#s=t.statsCleanupTtlMs??35e3,this.#n=t.maxParsedStatsStorageSize??5}detect(t,e){const s={...t,networkScores:{...e,statsSamples:e?.statsSamples||{}}},n=this.performDetection(s);return this.setLastProcessedStats(t.connection.id,s),this.performPrevStatsCleanup({connectionId:t.connection.id}),n}performPrevStatsCleanup(t){const{connectionId:e,cleanupCallback:s}=t;this.#e.has(e)&&v({taskId:e,delayMs:this.#s,callback:()=>{this.deleteLastProcessedStats(e),"function"==typeof s&&s()}})}setLastProcessedStats(t,e){if(!t||e.connection.id!==t)return;const s=this.#e.get(t)??[];s.push(e),s.length>this.#n&&s.shift(),this.#e.set(t,s)}getLastProcessedStats(t){const e=this.#e.get(t);return e?.[e.length-1]}getAllLastProcessedStats(t){return this.#e.get(t)??[]}deleteLastProcessedStats(t){this.#e.delete(t)}}class T extends w{#o;constructor(t={}){super(t),this.#o=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.#o?(n.push({statsSample:a,type:e.Network,reason:s.OutboundNetworkThroughput}),n):n}}class b extends w{#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){return this.processData(t)}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,f=l/o.length,m=a-c,g=d.packetsLost-d.lastPacketsLost,S=m&&g?Math.round(100*g/(m+g)):0,v=S>this.#r,k=p>=this.#i,w=u>=this.#c,T=f>this.#a,b=w&&!k&&!v,P=v&&k,y=k&&T,L={rtt:u,packetLossPct:S,avgJitter:p,avgJitterBufferDelay:f};return(k||v)&&n.push({statsSample:L,type:e.Network,reason:s.InboundNetworkQuality,iceCandidate:t.connection.local.id}),b&&n.push({statsSample:L,type:e.Server,reason:s.ServerIssue,iceCandidate:t.connection.remote.id}),P&&n.push({statsSample:L,type:e.Network,reason:s.InboundNetworkMediaLatency,iceCandidate:t.connection.local.id}),y&&n.push({statsSample:L,type:e.Network,reason:s.NetworkMediaSyncFailure,iceCandidate:t.connection.local.id}),n}}class P extends w{#d;constructor(t={}){super(),this.#d=t.correctedSamplesThresholdPct??5}performDetection(t){return this.processData(t)}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 y extends w{#r;#i;constructor(t={}){super(),this.#r=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200}performDetection(t){return this.processData(t)}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,f=d.packetsLost-d.lastPacketsLost,m=p&&f?Math.round(100*f/(p+f)):0,g=m>this.#r,S=l>=this.#i,v=!g&&S||S||g,k={rtt:u,avgJitter:l,packetLossPct:m};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 L extends w{performDetection(t){return this.processData(t)}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 w{UNKNOWN_DECODER="unknown";#u={};performDetection(t){return this.processData(t)}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}}const D=(t,e)=>{for(let s=1;s<e.length;s+=1){const n=e[s].video.inbound.find((e=>e.ssrc===t));if(!n)continue;const o=e[s-1].video.inbound.find((e=>e.ssrc===t)),r=n.frameWidth!==o?.frameWidth,i=n.frameHeight!==o?.frameHeight;if(r||i)return!0}return!1};class R extends w{#h;#l;constructor(t={}){super(),this.#h=t.avgFreezeDurationThresholdMs??1e3,this.#l=t.frozenDurationThresholdPct??30}performDetection(t){const e=t.networkScores.inbound;return void 0!==e&&e<=n.BAD?[]:this.processData(t)}processData(t){const n=[],o=this.getAllLastProcessedStats(t.connection.id);if(0===o.length)return[];const r=t.video.inbound.map((e=>{const s=o[o.length-1].video.inbound.find((t=>t.ssrc===e.ssrc));if(!s)return;if(D(e.ssrc,[o[o.length-1],t]))return;const n=e.freezeCount-(s.freezeCount??0),r=1e3*(e.totalFreezesDuration-(s.totalFreezesDuration??0)),i=n>0?r/n:0,a=r/(e.timestamp-s.timestamp)*100;return a>this.#l||i>this.#h?{ssrc:e.ssrc,avgFreezeDurationMs:i,frozenDurationPct:a}:void 0})).filter((t=>void 0!==t));return r.length>0&&n.push({type:e.Stream,reason:s.FrozenVideoTrack,statsSample:{ssrcs:r.map((t=>t.ssrc))}}),n}}class E extends w{#p;#f;constructor(t={}){super(t),this.#p=t.volatilityThreshold??8,this.#f=t.affectedStreamsPercentThreshold??30}performDetection(t){return[...this.getAllLastProcessedStats(t.connection.id),t].find((t=>void 0!==t.networkScores.inbound&&t.networkScores.inbound<=n.BAD))?[]:this.processData(t)}processData(t){const n=[],o=[...this.getAllLastProcessedStats(t.connection.id),t],r=t.video.inbound.map((t=>{if(D(t.ssrc,o))return;if(o.length<5)return;const e=[];for(let s=0;s<o.length-1;s+=1){const n=o[s].video.inbound.find((e=>e.ssrc===t.ssrc));void 0!==n?.framesPerSecond&&e.push(n.framesPerSecond)}const s=e.reduce(((t,e)=>t+e),0)/e.length,n=100*(e.reduce(((t,e)=>t+Math.abs(e-s)),0)/e.length)/s;return console.log("THROTTLE",{ssrc:t.ssrc,volatility:n,allFps:e}),n>this.#p?(console.log("THROTTLE DETECTED on Single stream",t.ssrc),{ssrc:t.ssrc,allFps:e,volatility:n}):void 0})).filter((t=>Boolean(t)));if(0===r.length)return n;const i=r.length/(t.video.inbound.length/100);return console.log("THROTTLE AFFECTION",{affectedStreamsPercent:i}),i>this.#f&&(console.log("THROTTLE DETECTED !!!!"),n.push({type:e.CPU,reason:s.DecoderCPUThrottling,statsSample:{affectedStreamsPercent:i,throtthedStreams:r}}),this.deleteLastProcessedStats(t.connection.id)),n}}const I=t=>"closed"===t.iceConnectionState||"closed"===t.connectionState,M=(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 N{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 O{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()}),v({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=M(t,s,"bytesReceived"),t.packetRate=M(t,s,"packetsReceived")})),t.audio.outbound.forEach((t=>{const s=e.audio.outbound.find((({id:e})=>e===t.id));t.bitrate=M(t,s,"bytesSent"),t.packetRate=M(t,s,"packetsSent")})),t.video.inbound.forEach((t=>{const s=e.video.inbound.find((({id:e})=>e===t.id));t.bitrate=M(t,s,"bytesReceived"),t.packetRate=M(t,s,"packetsReceived")})),t.video.outbound.forEach((t=>{const s=e.video.outbound.find((({id:e})=>e===t.id));t.bitrate=M(t,s,"bytesSent"),t.packetRate=M(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;#m=!1;detectors=[];networkScoresCalculator;statsReporter;compositeStatsParser;logger;autoAddPeerConnections;constructor(e){this.logger=e.logger??{debug:()=>{},info:()=>{},warn:()=>{},error:()=>{}},this.eventEmitter=e.issueEmitter??new g,e.onIssues&&this.eventEmitter.on(t.Issue,e.onIssues),e.onNetworkScoresUpdated&&this.eventEmitter.on(t.NetworkScoresUpdated,e.onNetworkScoresUpdated),this.detectors=e.detectors??[new L,new b,new y,new P,new T,new C,new R,new E],this.networkScoresCalculator=e.networkScoresCalculator??new k,this.compositeStatsParser=e.compositeStatsParser??new N({statsParser:new O({ignoreSSRCList:e.ignoreSSRCList,logger:this.logger})}),this.statsReporter=e.statsReporter??new S({compositeStatsParser:this.compositeStatsParser,getStatsInterval:e.getStatsInterval??5e3}),window.wid=this,this.autoAddPeerConnections=e.autoAddPeerConnections??!0,this.autoAddPeerConnections&&this.wrapRTCPeerConnection(),this.statsReporter.on(S.STATS_REPORT_READY_EVENT,(t=>{const e=this.calculateNetworkScores(t.stats);this.detectIssues({data:t.stats},e)})),this.statsReporter.on(S.STATS_REPORTS_PARSED,(s=>{const n={timeTaken:s.timeTaken,ts:Date.now()};e.onStats&&e.onStats(s.reportItems),this.eventEmitter.emit(t.StatsParsingFinished,n)}))}watchNewPeerConnections(){if(!this.autoAddPeerConnections)throw new Error("Auto add peer connections was disabled in the constructor.");this.#m?this.logger.warn("WebRTCIssueDetector is already started. Skip processing"):(this.logger.info("Start watching peer connections"),this.#m=!0,this.statsReporter.startReporting())}stopWatchingNewPeerConnections(){this.#m?(this.logger.info("Stop watching peer connections"),this.#m=!1,this.statsReporter.stopReporting()):this.logger.warn("WebRTCIssueDetector is already stopped. Skip processing")}handleNewPeerConnection(t,e){this.#m||!this.autoAddPeerConnections?(this.#m||!1!==this.autoAddPeerConnections||(this.logger.info("Starting stats reporting for new peer connection"),this.#m=!0,this.statsReporter.startReporting()),this.logger.debug("Handling new peer connection",t),this.compositeStatsParser.addPeerConnection({pc:t,id:e})):this.logger.debug("Skip handling new peer connection. Detector is not running",t)}emitIssues(e){this.eventEmitter.emit(t.Issue,e)}detectIssues({data:t},e){const s=this.detectors.reduce(((s,n)=>[...s,...n.detect(t,e)]),[]);s.length>0&&this.emitIssues(s)}calculateNetworkScores(e){const s=this.networkScoresCalculator.calculate(e);return this.eventEmitter.emit(t.NetworkScoresUpdated,s),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{T as AvailableOutgoingBitrateIssueDetector,w as BaseIssueDetector,N as CompositeRTCStatsParser,t as EventType,R as FrozenVideoTrackDetector,b as InboundNetworkIssueDetector,s as IssueReason,e as IssueType,n as MosQuality,P as NetworkMediaSyncIssueDetector,k as NetworkScoresCalculator,y as OutboundNetworkIssueDetector,S as PeriodicWebRTCStatsReporter,L as QualityLimitationsIssueDetector,O as RTCStatsParser,C as UnknownVideoDecoderImplementationDetector,E as VideoDecoderIssueDetector,g as WebRTCIssueEmitter,_ as default}; |
{ | ||
"name": "webrtc-issue-detector", | ||
"version": "1.14.0-debug-detectors.4", | ||
"version": "1.14.0-debug-detectors.5", | ||
"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", |
88124
894