UNPKG

webrtc-stats

Version:

A wrapper for WebRTC getStats for Chrome, Firefox and Opera

1 lines 2.59 kB
require=function t(e,n,r){function i(a,s){if(!n[a]){if(!e[a]){var c="function"==typeof require&&require;if(!s&&c)return c(a,!0);if(o)return o(a,!0);var d=new Error("Cannot find module '"+a+"'");throw d.code="MODULE_NOT_FOUND",d}var p=n[a]={exports:{}};e[a][0].call(p.exports,function(t){var n=e[a][1][t];return i(n?n:t)},p,p.exports,t,e,n,r)}return n[a].exports}for(var o="function"==typeof require&&require,a=0;a<r.length;a++)i(r[a]);return i}({1:[function(t,e,n){function r(t,e){this.pc=t,this.timeInterval=e,this.listeners={},this.started=!1}r.prototype.on=function(t,e){this.listeners[t]?this.listeners[t].push(e):this.listeners[t]=[e]},r.prototype.emit=function(){var t=Array.prototype.slice.call(arguments),e=t.shift();this.listeners[e]&&this.listeners[e].forEach(function(e){e.apply(null,t)})},r.prototype.start=function(){var t=this;t.started||(t.started=!0,t.intervalEvent=setInterval(function(){return"completed"!==t.pc.iceConnectionState&&"connected"!==t.pc.iceConnectionState?clearInterval(t.intervalEvent):void t.pc.getStats(null).then(function(e){var n=0,r=0;if(Object.keys(e).forEach(function(i){e[i].ssrc&&e[i].isRemote!==!0&&(t.timestamp="number"==typeof e[i].timestamp?e[i].timestamp:e[i].timestamp.getTime(),e[i].bytesReceived?n+=Number(e[i].bytesReceived):e[i].bytesSent&&(r+=Number(e[i].bytesSent)))}),t.prevTotalbytesReceived&&t.prevTotalbytesSent){var i=t.timestamp-t.lastTimestamp,o=i/1e3,a=(r-t.prevTotalbytesSent)/o,s=(n-t.prevTotalbytesReceived)/o,c={upStream:Math.floor(a/1024),downStream:Math.floor(s/1024)};t.emit("bandwidth",c)}t.prevTotalbytesSent=r,t.prevTotalbytesReceived=n,t.lastTimestamp=t.timestamp})["catch"](function(t){throw new Error(t)})},t.timeInterval))},r.prototype.stop=function(){this.started=!1,clearInterval(this.intervalEvent)},e.exports=r},{}],WebRTCUtils:[function(t,e,n){function r(){}var i=t("./Bandwidth");r.prototype.getBandwidth=function(t,e){return e=!e||1e3>e?1e3:e,new i(t,e)},r.prototype.getConnectionDetails=function(t){return new Promise(function(e,n){return"completed"!==t.iceConnectionState&&"connected"!==t.iceConnectionState?n("not connected yet"):void t.getStats(null).then(function(t){var r={},i=t[Object.keys(t).filter(function(e){return t[e].googActiveConnection||t[e].selected})[0]];return i?(r.remoteIpAddress=t[i.remoteCandidateId].ipAddress,r.remoteCandidateType=t[i.remoteCandidateId].candidateType,r.localIpAddress=t[i.localCandidateId].ipAddress,r.localCandidateType=t[i.localCandidateId].candidateType,void e(r)):n("Could not find proper stats")})})},e.exports=new r},{"./Bandwidth":1}]},{},["WebRTCUtils"]);