@dolbyio/webrtc-stats
Version:
Dolby OptiView WebRTC Statistics
3 lines • 17.9 kB
JavaScript
/*! For license information please see webrtc-stats.js.LICENSE.txt */
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.WebRTCStats=t():e.WebRTCStats=t()}(self,(()=>{return e={7:e=>{"use strict";var t,n="object"==typeof Reflect?Reflect:null,i=n&&"function"==typeof n.apply?n.apply:function(e,t,n){return Function.prototype.apply.call(e,t,n)};t=n&&"function"==typeof n.ownKeys?n.ownKeys:Object.getOwnPropertySymbols?function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:function(e){return Object.getOwnPropertyNames(e)};var r=Number.isNaN||function(e){return e!=e};function o(){o.init.call(this)}e.exports=o,e.exports.once=function(e,t){return new Promise((function(n,i){function r(n){e.removeListener(t,o),i(n)}function o(){"function"==typeof e.removeListener&&e.removeListener("error",r),n([].slice.call(arguments))}h(e,t,o,{once:!0}),"error"!==t&&function(e,t){"function"==typeof e.on&&h(e,"error",t,{once:!0})}(e,r)}))},o.EventEmitter=o,o.prototype._events=void 0,o.prototype._eventsCount=0,o.prototype._maxListeners=void 0;var s=10;function a(e){if("function"!=typeof e)throw new TypeError('The "listener" argument must be of type Function. Received type '+typeof e)}function l(e){return void 0===e._maxListeners?o.defaultMaxListeners:e._maxListeners}function c(e,t,n,i){var r,o,s,c;if(a(n),void 0===(o=e._events)?(o=e._events=Object.create(null),e._eventsCount=0):(void 0!==o.newListener&&(e.emit("newListener",t,n.listener?n.listener:n),o=e._events),s=o[t]),void 0===s)s=o[t]=n,++e._eventsCount;else if("function"==typeof s?s=o[t]=i?[n,s]:[s,n]:i?s.unshift(n):s.push(n),(r=l(e))>0&&s.length>r&&!s.warned){s.warned=!0;var u=new Error("Possible EventEmitter memory leak detected. "+s.length+" "+String(t)+" listeners added. Use emitter.setMaxListeners() to increase limit");u.name="MaxListenersExceededWarning",u.emitter=e,u.type=t,u.count=s.length,c=u,console&&console.warn&&console.warn(c)}return e}function u(){if(!this.fired)return this.target.removeListener(this.type,this.wrapFn),this.fired=!0,0===arguments.length?this.listener.call(this.target):this.listener.apply(this.target,arguments)}function f(e,t,n){var i={fired:!1,wrapFn:void 0,target:e,type:t,listener:n},r=u.bind(i);return r.listener=n,i.wrapFn=r,r}function d(e,t,n){var i=e._events;if(void 0===i)return[];var r=i[t];return void 0===r?[]:"function"==typeof r?n?[r.listener||r]:[r]:n?function(e){for(var t=new Array(e.length),n=0;n<t.length;++n)t[n]=e[n].listener||e[n];return t}(r):p(r,r.length)}function v(e){var t=this._events;if(void 0!==t){var n=t[e];if("function"==typeof n)return 1;if(void 0!==n)return n.length}return 0}function p(e,t){for(var n=new Array(t),i=0;i<t;++i)n[i]=e[i];return n}function h(e,t,n,i){if("function"==typeof e.on)i.once?e.once(t,n):e.on(t,n);else{if("function"!=typeof e.addEventListener)throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof e);e.addEventListener(t,(function r(o){i.once&&e.removeEventListener(t,r),n(o)}))}}Object.defineProperty(o,"defaultMaxListeners",{enumerable:!0,get:function(){return s},set:function(e){if("number"!=typeof e||e<0||r(e))throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received '+e+".");s=e}}),o.init=function(){void 0!==this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=Object.create(null),this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},o.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||r(e))throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received '+e+".");return this._maxListeners=e,this},o.prototype.getMaxListeners=function(){return l(this)},o.prototype.emit=function(e){for(var t=[],n=1;n<arguments.length;n++)t.push(arguments[n]);var r="error"===e,o=this._events;if(void 0!==o)r=r&&void 0===o.error;else if(!r)return!1;if(r){var s;if(t.length>0&&(s=t[0]),s instanceof Error)throw s;var a=new Error("Unhandled error."+(s?" ("+s.message+")":""));throw a.context=s,a}var l=o[e];if(void 0===l)return!1;if("function"==typeof l)i(l,this,t);else{var c=l.length,u=p(l,c);for(n=0;n<c;++n)i(u[n],this,t)}return!0},o.prototype.addListener=function(e,t){return c(this,e,t,!1)},o.prototype.on=o.prototype.addListener,o.prototype.prependListener=function(e,t){return c(this,e,t,!0)},o.prototype.once=function(e,t){return a(t),this.on(e,f(this,e,t)),this},o.prototype.prependOnceListener=function(e,t){return a(t),this.prependListener(e,f(this,e,t)),this},o.prototype.removeListener=function(e,t){var n,i,r,o,s;if(a(t),void 0===(i=this._events))return this;if(void 0===(n=i[e]))return this;if(n===t||n.listener===t)0==--this._eventsCount?this._events=Object.create(null):(delete i[e],i.removeListener&&this.emit("removeListener",e,n.listener||t));else if("function"!=typeof n){for(r=-1,o=n.length-1;o>=0;o--)if(n[o]===t||n[o].listener===t){s=n[o].listener,r=o;break}if(r<0)return this;0===r?n.shift():function(e,t){for(;t+1<e.length;t++)e[t]=e[t+1];e.pop()}(n,r),1===n.length&&(i[e]=n[0]),void 0!==i.removeListener&&this.emit("removeListener",e,s||t)}return this},o.prototype.off=o.prototype.removeListener,o.prototype.removeAllListeners=function(e){var t,n,i;if(void 0===(n=this._events))return this;if(void 0===n.removeListener)return 0===arguments.length?(this._events=Object.create(null),this._eventsCount=0):void 0!==n[e]&&(0==--this._eventsCount?this._events=Object.create(null):delete n[e]),this;if(0===arguments.length){var r,o=Object.keys(n);for(i=0;i<o.length;++i)"removeListener"!==(r=o[i])&&this.removeAllListeners(r);return this.removeAllListeners("removeListener"),this._events=Object.create(null),this._eventsCount=0,this}if("function"==typeof(t=n[e]))this.removeListener(e,t);else if(void 0!==t)for(i=t.length-1;i>=0;i--)this.removeListener(e,t[i]);return this},o.prototype.listeners=function(e){return d(this,e,!0)},o.prototype.rawListeners=function(e){return d(this,e,!1)},o.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):v.call(e,t)},o.prototype.listenerCount=v,o.prototype.eventNames=function(){return this._eventsCount>0?t(this._events):[]}},156:function(e,t,n){"use strict";var i=this&&this.__createBinding||(Object.create?function(e,t,n,i){void 0===i&&(i=n);var r=Object.getOwnPropertyDescriptor(t,n);r&&!("get"in r?!t.__esModule:r.writable||r.configurable)||(r={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,i,r)}:function(e,t,n,i){void 0===i&&(i=n),e[i]=t[n]}),r=this&&this.__exportStar||function(e,t){for(var n in e)"default"===n||Object.prototype.hasOwnProperty.call(t,n)||i(t,e,n)},o=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.Logger=void 0;var s=n(586);Object.defineProperty(t,"Logger",{enumerable:!0,get:function(){return o(s).default}}),r(n(284),t),r(n(280),t),r(n(738),t),r(n(767),t)},185:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.calculatePacketsLostRatio=t.calculateRate=t.getMediaKind=void 0,t.getMediaKind=e=>{let t=e.kind||e.mediaType;return["audio","video"].includes(t)||"inbound-rtp"!==e.type||(t=e.id.toLocaleLowerCase().includes("video")?"video":"audio"),t},t.calculateRate=(e,t,n,i)=>n&&i?(t-i)/((e-n)/1e3):0,t.calculatePacketsLostRatio=(e,t,n,i)=>{const r=e-(null!=n?n:0),o=r+(t-(null!=i?i:0));return 0===o?0:r/o}},280:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0})},284:function(e,t,n){"use strict";var i,r,o,s,a,l,c,u,f,d,v,p,h,m,y,g,b=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(r,o){function s(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}l((i=i.apply(e,t||[])).next())}))},w=this&&this.__classPrivateFieldSet||function(e,t,n,i,r){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?r.call(e,n):r?r.value=n:t.set(e,n),n},L=this&&this.__classPrivateFieldGet||function(e,t,n,i){if("a"===n&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?i:"a"===n?i.call(e):i?i.value:t.get(e)},R=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.WebRTCStats=void 0;const _=n(7),k=R(n(586)),O=n(738),E=n(185);class P extends _.EventEmitter{constructor(e){super(),i.add(this),r.set(this,void 0),o.set(this,void 0),s.set(this,void 0),a.set(this,null),l.set(this,void 0),c.set(this,null),this.start=()=>{L(this,l,"f").info("WebRTC statistics collection is starting..."),w(this,c,null,"f"),w(this,a,setInterval(L(this,u,"f"),L(this,o,"f")),"f")},this.stop=()=>{L(this,a,"f")&&(clearInterval(L(this,a,"f")),w(this,a,null,"f"),L(this,l,"f").info("WebRTC statistics collection has stopped."))},u.set(this,(()=>b(this,void 0,void 0,(function*(){let e,t;try{L(this,l,"f").trace("Requesting WebRTC statistics..."),e=yield L(this,r,"f").call(this),t=(new Date).toISOString()}catch(e){return L(this,l,"f").error("Problem collecting the WebRTC statistics.",e),void this.emit("error",`Problem collecting the WebRTC statistics - ${e}`)}const n=Array.from(e.values()),o={timestamp:t,input:{audio:[],video:[]},output:{audio:[],video:[]}};L(this,s,"f")&&(o.rawStats=e);for(let t=0;t<Object.keys(n).length;t++){const r=n[t];switch(r.type){case"outbound-rtp":const t=r,n=(0,E.getMediaKind)(t);"audio"===n?yield L(this,i,"m",v).call(this,e,t,o):"video"===n&&(yield L(this,i,"m",p).call(this,e,t,o));break;case"inbound-rtp":const s=r;let a=(0,E.getMediaKind)(s);"audio"===a?yield L(this,i,"m",m).call(this,e,s,o):"video"===a&&(yield L(this,i,"m",y).call(this,e,s,o));break;case"candidate-pair":const l=r;l.nominated&&L(this,i,"m",g).call(this,l,o)}}w(this,c,o,"f"),this.emit("stats",o)})))),w(this,l,k.default.get("WebRTCStats"),"f"),w(this,r,e.getStats,"f"),w(this,o,e.getStatsInterval||1e3,"f"),w(this,s,!!e.includeRawStats,"f")}}t.WebRTCStats=P,r=new WeakMap,o=new WeakMap,s=new WeakMap,a=new WeakMap,l=new WeakMap,c=new WeakMap,u=new WeakMap,i=new WeakSet,f=function(e,t){if(t){const n=e.get(t);if(n)return{mimeType:n.mimeType}}return{}},d=function(e,t,n){return b(this,void 0,void 0,(function*(){var r,o,s,a;const l=(0,E.calculateRate)(t.timestamp,t.bytesSent,null==n?void 0:n.timestamp,null==n?void 0:n.totalBytesSent),c=(0,E.calculateRate)(t.timestamp,t.packetsSent,null==n?void 0:n.timestamp,null==n?void 0:n.totalPacketsSent),u=L(this,i,"m",f).call(this,e,t.codecId);return Object.assign({id:t.id,timestamp:t.timestamp,mid:t.mid,totalBytesSent:t.bytesSent,bytesSentDelta:t.bytesSent-(null!==(r=null==n?void 0:n.totalBytesSent)&&void 0!==r?r:0),totalPacketsSent:t.packetsSent,packetsSentDelta:t.packetsSent-(null!==(o=null==n?void 0:n.totalPacketsSent)&&void 0!==o?o:0),bitrate:l,packetRate:c,targetBitrate:t.targetBitrate,retransmittedPacketsSent:t.retransmittedPacketsSent,retransmittedPacketsSentDelta:t.retransmittedPacketsSent-(null!==(s=null==n?void 0:n.retransmittedPacketsSent)&&void 0!==s?s:0),retransmittedBytesSent:t.retransmittedBytesSent,retransmittedBytesSentDelta:t.retransmittedBytesSent-(null!==(a=null==n?void 0:n.retransmittedBytesSent)&&void 0!==a?a:0)},u)}))},v=function(e,t,n){return b(this,void 0,void 0,(function*(){var r;const o=null===(r=L(this,c,"f"))||void 0===r?void 0:r.output.audio.find((e=>e.id===t.id));if(o&&t.timestamp-o.timestamp<=0)return;const s=yield L(this,i,"m",d).call(this,e,t,o),a=Object.assign({},s);n.output.audio.push(a)}))},p=function(e,t,n){return b(this,void 0,void 0,(function*(){var r;const o=null===(r=L(this,c,"f"))||void 0===r?void 0:r.output.video.find((e=>e.id===t.id));if(o&&t.timestamp-o.timestamp<=0)return;const s=yield L(this,i,"m",d).call(this,e,t,o);let a=O.QualityLimitationReason.none;t.qualityLimitationReason&&(a=t.qualityLimitationReason);const l=Object.assign(Object.assign({},s),{frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond,framesSent:t.framesSent,qualityLimitationReason:a,qualityLimitationDurations:t.qualityLimitationDurations});n.output.video.push(l)}))},h=function(e,t,n){return b(this,void 0,void 0,(function*(){var r,o;const s=(0,E.calculateRate)(t.timestamp,t.bytesReceived,null==n?void 0:n.timestamp,null==n?void 0:n.totalBytesReceived),a=(0,E.calculateRate)(t.timestamp,t.packetsReceived,null==n?void 0:n.timestamp,null==n?void 0:n.totalPacketsReceived),l=(0,E.calculatePacketsLostRatio)(t.packetsLost,t.packetsReceived,null==n?void 0:n.totalPacketsLost,null==n?void 0:n.totalPacketsReceived),c=(null!==(r=t.packetsLost)&&void 0!==r?r:0)-(null!==(o=null==n?void 0:n.totalPacketsLost)&&void 0!==o?o:0),u=L(this,i,"m",f).call(this,e,t.codecId);return Object.assign({id:t.id,timestamp:t.timestamp,mid:t.mid,trackIdentifier:t.trackIdentifier,jitter:t.jitter,jitterBufferDelay:t.jitterBufferDelay,jitterBufferEmittedCount:t.jitterBufferEmittedCount,totalBytesReceived:t.bytesReceived,totalPacketsReceived:t.packetsReceived,totalPacketsLost:t.packetsLost,bitrate:s,packetRate:a,packetLossRatio:l,packetLossDelta:c},u)}))},m=function(e,t,n){return b(this,void 0,void 0,(function*(){var r;const o=null===(r=L(this,c,"f"))||void 0===r?void 0:r.input.audio.find((e=>e.id===t.id));if(o&&t.timestamp-o.timestamp<=0)return;const s=yield L(this,i,"m",h).call(this,e,t,o);n.input.audio.push(s)}))},y=function(e,t,n){return b(this,void 0,void 0,(function*(){var r;const o=null===(r=L(this,c,"f"))||void 0===r?void 0:r.input.video.find((e=>e.id===t.id));if(o&&t.timestamp-o.timestamp<=0)return;const s=yield L(this,i,"m",h).call(this,e,t,o),a=Object.assign(Object.assign({},s),{keyFramesDecoded:t.keyFramesDecoded,frameHeight:t.frameHeight,frameWidth:t.frameWidth,framesDecoded:t.framesDecoded,framesDropped:t.framesDropped,framesPerSecond:t.framesPerSecond,framesReceived:t.framesReceived});n.input.video.push(a)}))},g=function(e,t){t.totalRoundTripTime=e.totalRoundTripTime,t.currentRoundTripTime=e.currentRoundTripTime,t.responsesReceived=e.responsesReceived,t.availableOutgoingBitrate=e.availableOutgoingBitrate,t.availableIncomingBitrate=e.availableIncomingBitrate}},586:function(e,t,n){var i,r;!function(){"use strict";var o,s={VERSION:"1.6.1"},a={},l=function(e,t){return function(){return t.apply(e,arguments)}},c=function(){var e,t,n=arguments,i=n[0];for(t=1;t<n.length;t++)for(e in n[t])!(e in i)&&n[t].hasOwnProperty(e)&&(i[e]=n[t][e]);return i},u=function(e,t){return{value:e,name:t}};s.TRACE=u(1,"TRACE"),s.DEBUG=u(2,"DEBUG"),s.INFO=u(3,"INFO"),s.TIME=u(4,"TIME"),s.WARN=u(5,"WARN"),s.ERROR=u(8,"ERROR"),s.OFF=u(99,"OFF");var f=function(e){this.context=e,this.setLevel(e.filterLevel),this.log=this.info};f.prototype={setLevel:function(e){e&&"value"in e&&(this.context.filterLevel=e)},getLevel:function(){return this.context.filterLevel},enabledFor:function(e){var t=this.context.filterLevel;return e.value>=t.value},trace:function(){this.invoke(s.TRACE,arguments)},debug:function(){this.invoke(s.DEBUG,arguments)},info:function(){this.invoke(s.INFO,arguments)},warn:function(){this.invoke(s.WARN,arguments)},error:function(){this.invoke(s.ERROR,arguments)},time:function(e){"string"==typeof e&&e.length>0&&this.invoke(s.TIME,[e,"start"])},timeEnd:function(e){"string"==typeof e&&e.length>0&&this.invoke(s.TIME,[e,"end"])},invoke:function(e,t){o&&this.enabledFor(e)&&o(t,c({level:e},this.context))}};var d,v=new f({filterLevel:s.OFF});(d=s).enabledFor=l(v,v.enabledFor),d.trace=l(v,v.trace),d.debug=l(v,v.debug),d.time=l(v,v.time),d.timeEnd=l(v,v.timeEnd),d.info=l(v,v.info),d.warn=l(v,v.warn),d.error=l(v,v.error),d.log=d.info,s.setHandler=function(e){o=e},s.setLevel=function(e){for(var t in v.setLevel(e),a)a.hasOwnProperty(t)&&a[t].setLevel(e)},s.getLevel=function(){return v.getLevel()},s.get=function(e){return a[e]||(a[e]=new f(c({name:e},v.context)))},s.createDefaultHandler=function(e){(e=e||{}).formatter=e.formatter||function(e,t){t.name&&e.unshift("["+t.name+"]")};var t={},n=function(e,t){Function.prototype.apply.call(e,console,t)};return"undefined"==typeof console?function(){}:function(i,r){i=Array.prototype.slice.call(i);var o,a=console.log;r.level===s.TIME?(o=(r.name?"["+r.name+"] ":"")+i[0],"start"===i[1]?console.time?console.time(o):t[o]=(new Date).getTime():console.timeEnd?console.timeEnd(o):n(a,[o+": "+((new Date).getTime()-t[o])+"ms"])):(r.level===s.WARN&&console.warn?a=console.warn:r.level===s.ERROR&&console.error?a=console.error:r.level===s.INFO&&console.info?a=console.info:r.level===s.DEBUG&&console.debug?a=console.debug:r.level===s.TRACE&&console.trace&&(a=console.trace),e.formatter(i,r),n(a,i))}},s.useDefaults=function(e){s.setLevel(e&&e.defaultLevel||s.DEBUG),s.setHandler(s.createDefaultHandler(e))},s.setDefaults=s.useDefaults,void 0===(r="function"==typeof(i=s)?i.call(t,n,t,e):i)||(e.exports=r)}()},738:(e,t)=>{"use strict";var n;Object.defineProperty(t,"__esModule",{value:!0}),t.QualityLimitationReason=void 0,function(e){e[e.none=0]="none",e[e.cpu=1]="cpu",e[e.bandwidth=2]="bandwidth",e[e.other=3]="other"}(n||(t.QualityLimitationReason=n={}))},767:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0})}},t={},function n(i){var r=t[i];if(void 0!==r)return r.exports;var o=t[i]={exports:{}};return e[i].call(o.exports,o,o.exports,n),o.exports}(156);var e,t}));
//# sourceMappingURL=webrtc-stats.js.map