webrtc-issue-detector
Version:
WebRTC diagnostic tool that detects issues with network or user devices
2 lines (1 loc) • 28.5 kB
JavaScript
var t,e,s,n;function r(){}function o(){o.init.call(this)}function i(t){return void 0===t._maxListeners?o.defaultMaxListeners:t._maxListeners}function a(t,e,s){if(e)t.call(s);else for(var n=t.length,r=m(t,n),o=0;o<n;++o)r[o].call(s)}function c(t,e,s,n){if(e)t.call(s,n);else for(var r=t.length,o=m(t,r),i=0;i<r;++i)o[i].call(s,n)}function d(t,e,s,n,r){if(e)t.call(s,n,r);else for(var o=t.length,i=m(t,o),a=0;a<o;++a)i[a].call(s,n,r)}function u(t,e,s,n,r,o){if(e)t.call(s,n,r,o);else for(var i=t.length,a=m(t,i),c=0;c<i;++c)a[c].call(s,n,r,o)}function h(t,e,s,n){if(e)t.apply(s,n);else for(var r=t.length,o=m(t,r),i=0;i<r;++i)o[i].apply(s,n)}function l(t,e,s,n){var o,a,c,d;if("function"!=typeof s)throw new TypeError('"listener" argument must be a function');if((a=t._events)?(a.newListener&&(t.emit("newListener",e,s.listener?s.listener:s),a=t._events),c=a[e]):(a=t._events=new r,t._eventsCount=0),c){if("function"==typeof c?c=a[e]=n?[s,c]:[c,s]:n?c.unshift(s):c.push(s),!c.warned&&(o=i(t))&&o>0&&c.length>o){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 r(){t.removeListener(e,r),n||(n=!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}r.prototype=Object.create(null),o.EventEmitter=o,o.usingDomains=!1,o.prototype.domain=void 0,o.prototype._events=void 0,o.prototype._maxListeners=void 0,o.defaultMaxListeners=10,o.init=function(){this.domain=null,o.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new r,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},o.prototype.setMaxListeners=function(t){if("number"!=typeof t||t<0||isNaN(t))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=t,this},o.prototype.getMaxListeners=function(){return i(this)},o.prototype.emit=function(t){var e,s,n,r,o,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(r=new Array(n-1),o=1;o<n;o++)r[o-1]=arguments[o];h(s,m,this,r)}return!0},o.prototype.addListener=function(t,e){return l(this,t,e,!1)},o.prototype.on=o.prototype.addListener,o.prototype.prependListener=function(t,e){return l(this,t,e,!0)},o.prototype.once=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.on(t,p(this,t,e)),this},o.prototype.prependOnceListener=function(t,e){if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');return this.prependListener(t,p(this,t,e)),this},o.prototype.removeListener=function(t,e){var s,n,o,i,a;if("function"!=typeof e)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(s=n[t]))return this;if(s===e||s.listener&&s.listener===e)0==--this._eventsCount?this._events=new r:(delete n[t],n.removeListener&&this.emit("removeListener",t,s.listener||e));else if("function"!=typeof s){for(o=-1,i=s.length;i-- >0;)if(s[i]===e||s[i].listener&&s[i].listener===e){a=s[i].listener,o=i;break}if(o<0)return this;if(1===s.length){if(s[0]=void 0,0==--this._eventsCount)return this._events=new r,this;delete n[t]}else!function(t,e){for(var s=e,n=s+1,r=t.length;n<r;s+=1,n+=1)t[s]=t[n];t.pop()}(s,o);n.removeListener&&this.emit("removeListener",t,a||e)}return this},o.prototype.off=function(t,e){return this.removeListener(t,e)},o.prototype.removeAllListeners=function(t){var e,s;if(!(s=this._events))return this;if(!s.removeListener)return 0===arguments.length?(this._events=new r,this._eventsCount=0):s[t]&&(0==--this._eventsCount?this._events=new r:delete s[t]),this;if(0===arguments.length){for(var n,o=Object.keys(s),i=0;i<o.length;++i)"removeListener"!==(n=o[i])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new r,this._eventsCount=0,this}if("function"==typeof(e=s[t]))this.removeListener(t,e);else if(e)do{this.removeListener(t,e[e.length-1])}while(e[0]);return this},o.prototype.listeners=function(t){var e,s=this._events;return s&&(e=s[t])?"function"==typeof e?[e.listener||e]:function(t){for(var e=new Array(t.length),s=0;s<e.length;++s)e[s]=t[s].listener||t[s];return e}(e):[]},o.listenerCount=function(t,e){return"function"==typeof t.listenerCount?t.listenerCount(e):f.call(t,e)},o.prototype.listenerCount=f,o.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class g extends o{}!function(t){t.Issue="issue",t.NetworkScoresUpdated="network-scores-updated",t.StatsParsingFinished="stats-parsing-finished"}(t||(t={})),function(t){t.Network="network",t.CPU="cpu",t.Server="server",t.Stream="stream"}(e||(e={})),function(t){t.OutboundNetworkQuality="outbound-network-quality",t.InboundNetworkQuality="inbound-network-quality",t.OutboundNetworkMediaLatency="outbound-network-media-latency",t.InboundNetworkMediaLatency="inbound-network-media-latency",t.NetworkMediaSyncFailure="network-media-sync-failure",t.OutboundNetworkThroughput="outbound-network-throughput",t.InboundNetworkThroughput="inbound-network-throughput",t.EncoderCPUThrottling="encoder-cpu-throttling",t.DecoderCPUThrottling="decoder-cpu-throttling",t.ServerIssue="server-issue",t.UnknownVideoDecoderIssue="unknown-video-decoder",t.LowInboundMOS="low-inbound-mean-opinion-score",t.LowOutboundMOS="low-outbound-mean-opinion-score",t.FrozenVideoTrack="frozen-video-track",t.MissingVideoStreamData="missing-video-stream-data",t.MissingAudioStreamData="missing-audio-stream-data"}(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 o{static STATS_REPORT_READY_EVENT="stats-report-ready";static STATS_REPORTS_PARSED="stats-reports-parsed";isStopped=!1;reportTimer;getStatsInterval;compositeStatsParser;constructor(t){super(),this.compositeStatsParser=t.compositeStatsParser,this.getStatsInterval=t.getStatsInterval??1e4}get isRunning(){return!!this.reportTimer&&!this.isStopped}startReporting(){if(this.reportTimer)return;const t=()=>setTimeout((()=>{this.isStopped?this.reportTimer=void 0:this.parseReports().finally((()=>{this.reportTimer=t()}))}),this.getStatsInterval);this.isStopped=!1,this.reportTimer=t()}stopReporting(){this.isStopped=!0,this.reportTimer&&(clearTimeout(this.reportTimer),this.reportTimer=void 0)}async parseReports(){const t=Date.now(),e=await this.compositeStatsParser.parse(),s=Date.now()-t;this.emit(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:r,callback:o}=e,i=Math.ceil(Math.random()*(r||0)),a=t.get(s);a&&clearTimeout(a);const c=setTimeout((()=>{o(),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:r,stats:o}=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:o,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:r}=t.connection,o=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=r-o,h=i.packetsLost-i.lastPacketsLost,l=u&&h?Math.round(100*h/(u+h)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:l}),stats:{avgJitter:d,rtt:a,packetsLoss:l}}}calculateInboundScore(t){const e=[...t.audio?.inbound,...t.video?.inbound];if(!e.length)return;const s=this.#t[t.connection.id];if(!s)return;const n=[...s.video?.inbound,...s.audio?.inbound],{packetsReceived:r}=t.connection,o=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=r-o,h=i.packetsLost-i.lastPacketsLost,l=u&&h?Math.round(100*h/(u+h)):0;return{mos:this.calculateMOS({avgJitter:d,rtt:a,packetsLoss:l}),stats:{avgJitter:d,rtt:a,packetsLoss:l}}}calculateMOS({avgJitter:t,rtt:e,packetsLoss:s}){const n=e+2*t+10;let r=n<160?93.2-n/40:93.2-n/120-10;return r-=2.5*s,1+.035*r+7e-6*r*(r-60)*(100-r)}}class y{#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 w extends y{#r;constructor(t={}){super(t),this.#r=t.availableOutgoingBitrateThreshold??1e5}performDetection(t){const n=[],{availableOutgoingBitrate:r}=t.connection;if(void 0===r)return n;const o=t.audio.outbound.reduce(((t,e)=>t+e.targetBitrate),0),i=t.video.outbound.reduce(((t,e)=>t+e.bitrate),0);if(!o&&!i)return n;const a={availableOutgoingBitrate:r,videoStreamsTotalBitrate:i,audioStreamsTotalTargetBitrate:o};return o>r||i>0&&r<this.#r?(n.push({statsSample:a,type:e.Network,reason:s.OutboundNetworkThroughput}),n):n}}class b extends y{#o;#i;#a;#c;constructor(t={}){super(),this.#o=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200,this.#a=t.highJitterBufferDelayThresholdMs??500,this.#c=t.highRttThresholdMs??250}performDetection(t){return this.processData(t)}processData(t){const n=[],r=[...t.audio?.inbound,...t.video?.inbound];if(!r.length)return n;const o=this.getLastProcessedStats(t.connection.id);if(!o)return n;const i=[...o.video?.inbound,...o.audio?.inbound],{packetsReceived:a}=t.connection,c=o.connection.packetsReceived,d=r.reduce(((t,e)=>{const s=i.find((t=>t.ssrc===e.ssrc)),n=s?.jitterBufferDelay||0,r=s?.jitterBufferEmittedCount||0,o=e.jitterBufferDelay-n,a=e.jitterBufferEmittedCount-r,c=o&&a?1e3*o/a:0;return{sumJitter:t.sumJitter+e.jitter,sumJitterBufferDelayMs:t.sumJitterBufferDelayMs+c,packetsLost:t.packetsLost+e.packetsLost,lastPacketsLost:t.lastPacketsLost+(s?.packetsLost||0)}}),{sumJitter:0,sumJitterBufferDelayMs:0,packetsLost:0,lastPacketsLost:0}),u=1e3*t.connection.currentRoundTripTime||0,{sumJitter:h,sumJitterBufferDelayMs:l}=d,p=h/r.length,f=l/r.length,m=a-c,g=d.packetsLost-d.lastPacketsLost,S=m&&g?Math.round(100*g/(m+g)):0,v=S>this.#o,k=p>=this.#i,y=u>=this.#c,w=f>this.#a,b=y&&!k&&!v,P=v&&k,T=k&&w,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}),T&&n.push({statsSample:L,type:e.Network,reason:s.NetworkMediaSyncFailure,iceCandidate:t.connection.local.id}),n}}class P extends y{#d;constructor(t={}){super(),this.#d=t.correctedSamplesThresholdPct??5}performDetection(t){return this.processData(t)}processData(t){const n=t.audio.inbound,r=[],o=this.getLastProcessedStats(t.connection.id)?.audio.inbound;return o?(n.forEach((t=>{const n=o.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&&r.push({statsSample:h,type:e.Network,reason:s.NetworkMediaSyncFailure,ssrc:t.ssrc})})),r):r}}class T extends y{#o;#i;constructor(t={}){super(),this.#o=t.highPacketLossThresholdPct??5,this.#i=t.highJitterThreshold??200}performDetection(t){return this.processData(t)}processData(t){const n=[],r=[...t.remote?.audio.inbound||[],...t.remote?.video.inbound||[]];if(!r.length)return n;const o=this.getLastProcessedStats(t.connection.id);if(!o)return n;const i=[...o.remote?.audio.inbound||[],...o.remote?.video.inbound||[]],{packetsSent:a}=t.connection,c=o.connection.packetsSent,d=r.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/r.length,p=a-c,f=d.packetsLost-d.lastPacketsLost,m=p&&f?Math.round(100*f/(p+f)):0,g=m>this.#o,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 y{performDetection(t){return this.processData(t)}processData(t){const n=t.video.outbound.filter((t=>"none"!==t.qualityLimitationReason)),r=[],o=this.getLastProcessedStats(t.connection.id)?.video.outbound;return o?(n.forEach((t=>{const n=o.find((e=>e.ssrc===t.ssrc));if(!n)return;const i={qualityLimitationReason:t.qualityLimitationReason};t.framesSent>n.framesSent||("cpu"===t.qualityLimitationReason&&r.push({statsSample:i,type:e.CPU,reason:s.EncoderCPUThrottling,ssrc:t.ssrc}),"bandwidth"===t.qualityLimitationReason&&r.push({statsSample:i,type:e.Network,reason:s.OutboundNetworkThroughput,ssrc:t.ssrc}))})),r):r}}class D extends y{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:r}=t.connection,o=this.getLastProcessedStats(r)?.video.inbound;return t.video.inbound.forEach((t=>{const{decoderImplementation:i,ssrc:a}=t,c=o?.find((t=>t.ssrc===a));if(c)if(i===this.UNKNOWN_DECODER){if(!this.hadLastDecoderWithIssue(r,a)){this.setLastDecoderWithIssue(r,a,this.UNKNOWN_DECODER);const o={mimeType:t.mimeType,decoderImplementation:i};n.push({ssrc:a,statsSample:o,type:e.Stream,reason:s.UnknownVideoDecoderIssue,trackIdentifier:t.track.trackIdentifier})}}else this.setLastDecoderWithIssue(r,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 C=t=>t.reduce(((t,e)=>t+e),0)/t.length,R=(t,e,s=30)=>{const n=[];for(let s=1;s<e.length-1;s+=1){const r=e[s]?.video?.inbound.find((e=>e.ssrc===t));if(!r)continue;const o=e[s-1]?.video?.inbound?.find((e=>e.ssrc===t));if(!r||!o)continue;const i=r.timestamp-o.timestamp,a=r.framesDecoded-o.framesDecoded;if(a>0){const t=i/a;n.push(t)}}if(n.length<=1)return!1;return(t=>{const e=((t,e)=>e.reduce(((e,s)=>e+(s-t)**2),0)/e.length)(C(t),t);return Math.sqrt(e)})(n)>s},M=(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 r=e[s-1].video.inbound.find((e=>e.ssrc===t)),o=n.frameWidth!==r?.frameWidth,i=n.frameHeight!==r?.frameHeight;if(o||i)return!0}return!1};class I extends y{#h;#l;#p;constructor(t={}){super(),this.#h=t.avgFreezeDurationThresholdMs??1e3,this.#l=t.frozenDurationThresholdPct??30,this.#p=t.minMosQuality??n.BAD}performDetection(t){const e=t.networkScores.inbound;return void 0!==e&&e<=this.#p?[]:this.processData(t)}processData(t){const n=[],r=this.getAllLastProcessedStats(t.connection.id);if(0===r.length)return[];const o=t.video.inbound.map((e=>{const s=r[r.length-1].video.inbound.find((t=>t.ssrc===e.ssrc));if(!s)return;if(M(e.ssrc,[r[r.length-1],t]))return;if(R(e.ssrc,r))return;const n=e.freezeCount-(s.freezeCount??0),o=1e3*(e.totalFreezesDuration-(s.totalFreezesDuration??0)),i=n>0?o/n:0,a=o/(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 o.length>0&&(n.push({type:e.Stream,reason:s.FrozenVideoTrack,statsSample:{ssrcs:o.map((t=>t.ssrc))}}),this.deleteLastProcessedStats(t.connection.id)),n}}class E extends y{#f;#m;#p;constructor(t={}){super(t),this.#f=t.volatilityThreshold??8,this.#m=t.affectedStreamsPercentThreshold??30,this.#p=t.minMosQuality??n.BAD}performDetection(t){return[...this.getAllLastProcessedStats(t.connection.id),t].find((t=>void 0!==t.networkScores.inbound&&t.networkScores.inbound<=this.#p))?[]:this.processData(t)}processData(t){const n=[],r=[...this.getAllLastProcessedStats(t.connection.id),t],o=t.video.inbound.map((t=>{if(r.length<5)return;if(M(t.ssrc,r))return;const e=[];for(let s=0;s<r.length-1;s+=1){const n=r[s].video.inbound.find((e=>e.ssrc===t.ssrc));void 0!==n?.framesPerSecond&&e.push(n.framesPerSecond)}if(e.length<5)return;if(R(t.ssrc,r))return;const s=(t=>{if(0===t.length)throw new Error("Cannot calculate volatility for empty array");const e=C(t);return t.reduce(((t,s)=>t+Math.abs(s-e)),0)/t.length*100/e})(e);return s>this.#f?{ssrc:t.ssrc,allFps:e,volatility:s}:void 0})).filter((t=>Boolean(t)));if(0===o.length)return n;const i=o.length/(t.video.inbound.length/100);return i>this.#m&&(n.push({type:e.CPU,reason:s.DecoderCPUThrottling,statsSample:{affectedStreamsPercent:i,throtthedStreams:o}}),this.deleteLastProcessedStats(t.connection.id)),n}}const N=t=>"closed"===t.iceConnectionState||"closed"===t.connectionState,_=(t,e,s)=>8*((t,e,s)=>{if(!e)return 0;const n=t[s],r=e[s];if(null==n||null==r)return 0;const o=Math.floor(t.timestamp)-Math.floor(e.timestamp);return 0===o?0:(Number(n)-Number(r))/o*1e3})(t,e,s);class A{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(!N(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(!N(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(),r=e.getReceivers().filter((t=>t.track?.enabled)),o=e.getSenders().filter((t=>t.track?.enabled)),i=await Promise.all(r.map((t=>t.getStats()))),a=await Promise.all(o.map((t=>t.getStats())));return{id:s,stats:this.mapReportsStats([...i,...a],t),timeTaken:Date.now()-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,r=this.prevStats.get(n);return r&&this.propagateStatsWithRateValues(s,r.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 r=this.getMediaType(t);if(!r)return;const o=t.ssrc;if(!o||!this.ignoreSSRCList.includes(o))if("outbound-rtp"!==n)if("inbound-rtp"!==n)"remote-outbound-rtp"!==n?"remote-inbound-rtp"===n&&(this.mapConnectionStatsIfNecessary(e,t,s),e.remote[r].inbound.push({...t})):e.remote[r].outbound.push({...t});else{const n=s.get(t.trackId)||s.get(t.mediaSourceId)||{};this.mapConnectionStatsIfNecessary(e,t,s);const o={...t,track:{...n}};e[r].inbound.push(o)}else{const n=s.get(t.trackId)||s.get(t.mediaSourceId)||{},o={...t,track:{...n}};e[r].outbound.push(o)}}getMediaType(t){const e=t.mediaType||t.kind;if(!["audio","video"].includes(e)){const{id:e}=t;if(!e)return;return String(e).includes("Video")?"video":String(e).includes("Audio")?"audio":void 0}return e}propagateStatsWithRateValues(t,e){t.audio.inbound.forEach((t=>{const s=e.audio.inbound.find((({id:e})=>e===t.id));t.bitrate=_(t,s,"bytesReceived"),t.packetRate=_(t,s,"packetsReceived")})),t.audio.outbound.forEach((t=>{const s=e.audio.outbound.find((({id:e})=>e===t.id));t.bitrate=_(t,s,"bytesSent"),t.packetRate=_(t,s,"packetsSent")})),t.video.inbound.forEach((t=>{const s=e.video.inbound.find((({id:e})=>e===t.id));t.bitrate=_(t,s,"bytesReceived"),t.packetRate=_(t,s,"packetsReceived")})),t.video.outbound.forEach((t=>{const s=e.video.outbound.find((({id:e})=>e===t.id));t.bitrate=_(t,s,"bytesSent"),t.packetRate=_(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 J extends y{#g=new Map;#S;#v;constructor(t={}){super(),this.#S=t.timeoutMs??15e3,this.#v=t.steps??3}performDetection(t){return this.processData(t)}processData(t){const n=[],r=[...this.getAllLastProcessedStats(t.connection.id),t];if(r.length<this.#v)return n;const o=r.slice(-this.#v),i=o.map((t=>t.video.inbound)),a=o.map((t=>t.audio.inbound));n.push(...this.detectMissingData(a,e.Stream,s.MissingAudioStreamData)),n.push(...this.detectMissingData(i,e.Stream,s.MissingVideoStreamData));return new Set(this.#g.keys()).forEach((t=>{const e=this.#g.get(t);e&&Date.now()-e>this.#S&&this.removeMarkedIssue(t)})),n}detectMissingData(t,e,s){const n=[],r=t.pop(),o=J.mapStatsByTrackId(t);return r.forEach((t=>{const r=t.track.trackIdentifier,i=o.get(r);if(!Array.isArray(i)||0===i.length)return;if(t.track.detached||t.track.ended)return;if(!J.isAllBytesReceivedDidntChange(t.bytesReceived,i))return void this.removeMarkedIssue(r);if(!this.markIssue(r))return;const a={bytesReceived:t.bytesReceived};n.push({type:e,reason:s,statsSample:a,trackIdentifier:r})})),n}static mapStatsByTrackId(t){const e=new Map;return t.forEach((t=>{t.forEach((t=>{const s=e.get(t.track.trackIdentifier)||[];s.push(t),e.set(t.track.trackIdentifier,s)}))})),e}static isAllBytesReceivedDidntChange(t,e){for(let s=0;s<e.length;s+=1){if(e[s].bytesReceived!==t)return!1}return!0}markIssue(t){const e=Date.now(),s=this.#g.get(t);return(!s||e-s>this.#S)&&(this.#g.set(t,e),!0)}removeMarkedIssue(t){this.#g.delete(t)}}class x{eventEmitter;#k=!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 T,new P,new w,new D,new I,new E,new J],this.networkScoresCalculator=e.networkScoresCalculator??new k,this.compositeStatsParser=e.compositeStatsParser??new A({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.#k?this.logger.warn("WebRTCIssueDetector is already started. Skip processing"):(this.logger.info("Start watching peer connections"),this.#k=!0,this.statsReporter.startReporting())}stopWatchingNewPeerConnections(){this.#k?(this.logger.info("Stop watching peer connections"),this.#k=!1,this.statsReporter.stopReporting()):this.logger.warn("WebRTCIssueDetector is already stopped. Skip processing")}handleNewPeerConnection(t,e){this.#k||!this.autoAddPeerConnections?(this.#k||!1!==this.autoAddPeerConnections||(this.logger.info("Starting stats reporting for new peer connection"),this.#k=!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{w as AvailableOutgoingBitrateIssueDetector,y as BaseIssueDetector,A as CompositeRTCStatsParser,t as EventType,I as FrozenVideoTrackDetector,b as InboundNetworkIssueDetector,s as IssueReason,e as IssueType,n as MosQuality,P as NetworkMediaSyncIssueDetector,k as NetworkScoresCalculator,T as OutboundNetworkIssueDetector,S as PeriodicWebRTCStatsReporter,L as QualityLimitationsIssueDetector,O as RTCStatsParser,D as UnknownVideoDecoderImplementationDetector,E as VideoDecoderIssueDetector,g as WebRTCIssueEmitter,x as default};