From 08cb37fefbce3a5314736607aebb9d581d4d0a38 Mon Sep 17 00:00:00 2001 From: arthur Date: Fri, 19 Oct 2018 19:13:53 +0800 Subject: [PATCH] fix: result.bytesReceived !== '0' --- dev/googCodecName.audio.js | 13 +++++-------- dev/googCodecName.video.js | 2 +- getStats.js | 17 +++++++---------- getStats.min.js | 4 ++-- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/dev/googCodecName.audio.js b/dev/googCodecName.audio.js index a3d3b37..2c6eb02 100644 --- a/dev/googCodecName.audio.js +++ b/dev/googCodecName.audio.js @@ -28,16 +28,13 @@ getStatsParser.checkAudioTracks = function(result) { } // 当参数合并后,根据 bytesReceived 来判断recv/send - if (result.bytesReceived) { - var bytes = 0; - if (!!result.bytesReceived) { - if (!getStatsResult.internal.audio['recv'].prevBytesReceived) { - getStatsResult.internal.audio['recv'].prevBytesReceived = result.bytesReceived; - } - - bytes = result.bytesReceived - getStatsResult.internal.audio['recv'].prevBytesReceived; + if (result.bytesReceived && result.bytesReceived !== '0') { + if (!getStatsResult.internal.audio['recv'].prevBytesReceived) { getStatsResult.internal.audio['recv'].prevBytesReceived = result.bytesReceived; } + + var bytes = result.bytesReceived - getStatsResult.internal.audio['recv'].prevBytesReceived; + getStatsResult.internal.audio['recv'].prevBytesReceived = result.bytesReceived; getStatsResult.audio['recv'].availableBandwidth = bytes; } diff --git a/dev/googCodecName.video.js b/dev/googCodecName.video.js index 918fa50..305df44 100644 --- a/dev/googCodecName.video.js +++ b/dev/googCodecName.video.js @@ -28,7 +28,7 @@ getStatsParser.checkVideoTracks = function(result) { } // 当参数合并后,根据 bytesReceived 来判断recv/send - if (!!result.bytesReceived) { + if (!!result.bytesReceived && result.bytesReceived !== '0') { var bytes = 0; if (!getStatsResult.internal.video['recv'].prevBytesReceived || getStatsResult.internal.video['recv'].prevBytesReceived > result.bytesReceived) { getStatsResult.internal.video['recv'].prevBytesReceived = result.bytesReceived; diff --git a/getStats.js b/getStats.js index 8a4579d..c408601 100755 --- a/getStats.js +++ b/getStats.js @@ -1,6 +1,6 @@ 'use strict'; -// Last time updated: 2018-10-19 9:41:01 AM UTC +// Last time updated: 2018-10-19 11:13:46 AM UTC // _______________ // getStats v1.0.10 @@ -332,16 +332,13 @@ window.getStats = function(mediaStreamTrack, callback, interval) { } // 当参数合并后,根据 bytesReceived 来判断recv/send - if (result.bytesReceived) { - var bytes = 0; - if (!!result.bytesReceived) { - if (!getStatsResult.internal.audio['recv'].prevBytesReceived) { - getStatsResult.internal.audio['recv'].prevBytesReceived = result.bytesReceived; - } - - bytes = result.bytesReceived - getStatsResult.internal.audio['recv'].prevBytesReceived; + if (result.bytesReceived && result.bytesReceived !== '0') { + if (!getStatsResult.internal.audio['recv'].prevBytesReceived) { getStatsResult.internal.audio['recv'].prevBytesReceived = result.bytesReceived; } + + var bytes = result.bytesReceived - getStatsResult.internal.audio['recv'].prevBytesReceived; + getStatsResult.internal.audio['recv'].prevBytesReceived = result.bytesReceived; getStatsResult.audio['recv'].availableBandwidth = bytes; } @@ -380,7 +377,7 @@ window.getStats = function(mediaStreamTrack, callback, interval) { } // 当参数合并后,根据 bytesReceived 来判断recv/send - if (!!result.bytesReceived) { + if (!!result.bytesReceived && result.bytesReceived !== '0') { var bytes = 0; if (!getStatsResult.internal.video['recv'].prevBytesReceived || getStatsResult.internal.video['recv'].prevBytesReceived > result.bytesReceived) { getStatsResult.internal.video['recv'].prevBytesReceived = result.bytesReceived; diff --git a/getStats.min.js b/getStats.min.js index e261f0d..eebca29 100644 --- a/getStats.min.js +++ b/getStats.min.js @@ -1,6 +1,6 @@ 'use strict'; -// Last time updated: 2018-10-19 9:41:01 AM UTC +// Last time updated: 2018-10-19 11:13:46 AM UTC // _______________ // getStats v1.0.10 @@ -12,4 +12,4 @@ // MIT License - www.WebRTC-Experiment.com/licence // -------------------------------------------------- -"use strict";window.getStats=function(mediaStreamTrack,callback,interval){function preHandler(result){var idMap=result.reduce(function(map,item){return"codec"!=item.type&&"track"!=item.type&&"transport"!=item.type?map:(map[item.id]=item,map)},{});return result.reduce(function(sum,item){return"outbound-rtp"!=item.type&&"inbound-rtp"!=item.type&&item.type.indexOf("candidate")<0?(sum.push(item),sum):(item.type.indexOf("candidate")>=0&&(item.ip&&(item.ipAddress=item.ip),item.protocol&&(item.googTransportType=item.protocol),item.state&&(item.googActiveConnection=("succeeded"==item.state).toString()),item.port&&(item.portNumber=item.port)),item=Object.assign({},idMap[item.transportId],idMap[item.codecId],idMap[item.trackId],item),item.mimeType&&(item.googCodecName=item.mimeType.split("/")[1]),sum.push(item),sum)},[])}function getStatsLooper(){getStatsWrapper(function(results){results.forEach(function(result){Object.keys(getStatsParser).forEach(function(key){"function"==typeof getStatsParser[key]&&getStatsParser[key](result)}),"local-candidate"!==result.type&&"remote-candidate"!==result.type&&"candidate-pair"!==result.type});try{peer.iceConnectionState.search(/failed/gi)!==-1&&(nomore=!0)}catch(e){nomore=!0}nomore===!0&&(getStatsResult.datachannel&&(getStatsResult.datachannel.state="close"),getStatsResult.ended=!0),getStatsResult.results=results,getStatsResult.audio&&getStatsResult.video&&(getStatsResult.bandwidth.speed=getStatsResult.audio.bytesSent-getStatsResult.bandwidth.helper.audioBytesSent+(getStatsResult.video.bytesSent-getStatsResult.bandwidth.helper.videoBytesSent),getStatsResult.bandwidth.helper.audioBytesSent=getStatsResult.audio.bytesSent,getStatsResult.bandwidth.helper.videoBytesSent=getStatsResult.video.bytesSent),callback(getStatsResult),nomore||void 0!=typeof interval&&interval&&setTimeout(getStatsLooper,interval||1e3)})}function getStatsWrapper(cb){peer&&"closed"!=peer.signalingState&&("undefined"!=typeof window.InstallTrigger?peer.getStats(mediaStreamTrack,function(res){var items=[];res.forEach(function(r){items.push(r)}),cb(items)},cb):peer.getStats(function(res){var items=[],result=res.result();console.dir(result),result.forEach(function(res){var item={},names=null;try{names=res.names(),names.forEach(function(name){item[name]=res.stat(name)})}catch(error){item=Object.assign(item,res)}item.id=res.id,item.type=res.type,item.timestamp=res.timestamp,items.push(item)}),console.log("items",items);var items=preHandler(items);cb(items)}))}var RTCPeerConnection=window.RTCPeerConnection||window.mozRTCPeerConnection||window.webkitRTCPeerConnection;"undefined"==typeof MediaStreamTrack&&(MediaStreamTrack={});var systemNetworkType=((navigator.connection||{}).type||"unknown").toString().toLowerCase(),getStatsResult={encryption:"sha-256",audio:{send:{tracks:[],codecs:[],availableBandwidth:0,streams:0},recv:{tracks:[],codecs:[],availableBandwidth:0,streams:0},bytesSent:0,bytesReceived:0},video:{send:{tracks:[],codecs:[],availableBandwidth:0,streams:0},recv:{tracks:[],codecs:[],availableBandwidth:0,streams:0},bytesSent:0,bytesReceived:0},bandwidth:{systemBandwidth:0,sentPerSecond:0,encodedPerSecond:0,helper:{audioBytesSent:0,videoBytesSent:0},speed:0},results:{},connectionType:{systemNetworkType:systemNetworkType,systemIpAddress:"192.168.1.2",local:{candidateType:[],transport:[],ipAddress:[],networkType:[]},remote:{candidateType:[],transport:[],ipAddress:[],networkType:[]}},resolutions:{send:{width:0,height:0},recv:{width:0,height:0}},internal:{audio:{send:{},recv:{}},video:{send:{},recv:{}},candidates:{}},nomore:function(){nomore=!0},bytesToSize:function(bytes){var k=1e3,sizes=["Bytes","KB","MB","GB","TB"];if(bytes<=0)return"0 Bytes";var i=parseInt(Math.floor(Math.log(bytes)/Math.log(k)),10);return sizes[i]?(bytes/Math.pow(k,i)).toPrecision(3)+" "+sizes[i]:"0 Bytes"}},getStatsParser={checkIfOfferer:function(result){"googLibjingleSession"===result.type&&(getStatsResult.isOfferer=result.googInitiator)}},peer=this;if("function"!=typeof arguments[0].getStats)throw"1st argument is not exit getStats function";peer=arguments[0],navigator.mozGetUserMedia&&(mediaStreamTrack=arguments[1],callback=arguments[2],interval=arguments[3]),arguments[0]instanceof RTCPeerConnection?!navigator.mozGetUserMedia||mediaStreamTrack instanceof MediaStreamTrack||console.warn("2nd argument is not instance of MediaStreamTrack."):!navigator.mozGetUserMedia||mediaStreamTrack instanceof MediaStreamTrack||console.warn("1st argument is not instance of MediaStreamTrack.");var nomore=!1;getStatsParser.datachannel=function(result){"datachannel"===result.type&&(getStatsResult.datachannel={state:result.state})},getStatsParser.googCertificate=function(result){"googCertificate"==result.type&&(getStatsResult.encryption=result.googFingerprintAlgorithm)};var AUDIO_codecs=["opus","isac","ilbc"];getStatsParser.checkAudioTracks=function(result){if(result.googCodecName&&"audio"===result.mediaType&&AUDIO_codecs.indexOf(result.googCodecName.toLowerCase())!==-1){var sendrecvType=result.id.split("_").pop();if("recv"!=sendrecvType&&"send"!=sendrecvType&&(sendrecvType=result.isRemote?"recv":"send"),getStatsResult.audio[sendrecvType].codecs.indexOf(result.googCodecName)===-1&&getStatsResult.audio[sendrecvType].codecs.push(result.googCodecName),result.bytesSent){var bytes=0;result.bytesSent&&(getStatsResult.internal.audio[sendrecvType].prevBytesSent||(getStatsResult.internal.audio[sendrecvType].prevBytesSent=result.bytesSent),bytes=result.bytesSent-getStatsResult.internal.audio[sendrecvType].prevBytesSent,getStatsResult.internal.audio[sendrecvType].prevBytesSent=result.bytesSent),getStatsResult.audio[sendrecvType].availableBandwidth=bytes}if(result.bytesReceived){var bytes=0;result.bytesReceived&&(getStatsResult.internal.audio.recv.prevBytesReceived||(getStatsResult.internal.audio.recv.prevBytesReceived=result.bytesReceived),bytes=result.bytesReceived-getStatsResult.internal.audio.recv.prevBytesReceived,getStatsResult.internal.audio.recv.prevBytesReceived=result.bytesReceived),getStatsResult.audio.recv.availableBandwidth=bytes}getStatsResult.audio[sendrecvType].tracks.indexOf(result.googTrackId)===-1&&getStatsResult.audio[sendrecvType].tracks.push(result.googTrackId)}};var VIDEO_codecs=["vp9","vp8","h264"];getStatsParser.checkVideoTracks=function(result){if(result.googCodecName&&"video"===result.mediaType&&VIDEO_codecs.indexOf(result.googCodecName.toLowerCase())!==-1){var sendrecvType=result.id.split("_").pop();if("recv"!=sendrecvType&&"send"!=sendrecvType&&(sendrecvType=result.isRemote?"recv":"send"),getStatsResult.video[sendrecvType].codecs.indexOf(result.googCodecName)===-1&&getStatsResult.video[sendrecvType].codecs.push(result.googCodecName),result.bytesSent){var bytes=0;(!getStatsResult.internal.video[sendrecvType].prevBytesSent||getStatsResult.internal.video[sendrecvType].prevBytesSent>result.bytesSent)&&(getStatsResult.internal.video[sendrecvType].prevBytesSent=result.bytesSent),bytes=result.bytesSent-getStatsResult.internal.video[sendrecvType].prevBytesSent,getStatsResult.internal.video[sendrecvType].prevBytesSent=result.bytesSent,getStatsResult.video[sendrecvType].availableBandwidth=bytes}if(result.bytesReceived){var bytes=0;(!getStatsResult.internal.video.recv.prevBytesReceived||getStatsResult.internal.video.recv.prevBytesReceived>result.bytesReceived)&&(getStatsResult.internal.video.recv.prevBytesReceived=result.bytesReceived),bytes=result.bytesReceived-getStatsResult.internal.video.recv.prevBytesReceived,getStatsResult.internal.video.recv.prevBytesReceived=result.bytesReceived,getStatsResult.video.recv.availableBandwidth=bytes}if(result.packetsLost){getStatsResult.internal.video.recv.prevLostPacket||(getStatsResult.internal.video.recv.prevLostPacket=result.packetsLost);var bytes=getStatsResult.internal.video.recv.prevBytesReceived,packets=result.packetsLost-getStatsResult.internal.video.recv.prevLostPacket;getStatsResult.video.recv.packetsLostRate=0!=bytes?Math.round(packets/bytes*100)/100+"%":"0.00%"}result.googFrameHeightReceived&&result.googFrameWidthReceived&&(getStatsResult.resolutions[sendrecvType].width=result.googFrameWidthReceived,getStatsResult.resolutions[sendrecvType].height=result.googFrameHeightReceived),result.googFrameHeightSent&&result.googFrameWidthSent&&(getStatsResult.resolutions[sendrecvType].width=result.googFrameWidthSent,getStatsResult.resolutions[sendrecvType].height=result.googFrameHeightSent),getStatsResult.video[sendrecvType].tracks.indexOf(result.googTrackId)===-1&&getStatsResult.video[sendrecvType].tracks.push(result.googTrackId)}},getStatsParser.bweforvideo=function(result){"VideoBwe"!==result.type&&"candidate-pair"!==result.type||(getStatsResult.bandwidth.availableSendBandwidth=result.googAvailableSendBandwidth||result.availableOutgoingBitrate||0,getStatsResult.bandwidth.googActualEncBitrate=result.googActualEncBitrate,getStatsResult.bandwidth.googAvailableSendBandwidth=result.googAvailableSendBandwidth||result.availableOutgoingBitrate||0,getStatsResult.bandwidth.googAvailableReceiveBandwidth=result.googAvailableReceiveBandwidth||result.availableIncomingBitrate||0,getStatsResult.bandwidth.googRetransmitBitrate=result.googRetransmitBitrate,getStatsResult.bandwidth.googTargetEncBitrate=result.googTargetEncBitrate,getStatsResult.bandwidth.googBucketDelay=result.googBucketDelay,getStatsResult.bandwidth.googTransmitBitrate=result.googTransmitBitrate||0)},getStatsParser.candidatePair=function(result){if("googCandidatePair"===result.type||"candidate-pair"===result.type){if("true"==result.googActiveConnection){if(result.bytesSent){getStatsResult.internal.preCandidateBytesSent||(getStatsResult.internal.preCandidateBytesSent=result.bytesSent);var bytes=result.bytesSent-getStatsResult.internal.preCandidateBytesSent;getStatsResult.internal.preCandidateBytesSent=result.bytesSent,getStatsResult.bandwidth.candidateTransmitBitrate=bytes}Object.keys(getStatsResult.internal.candidates).forEach(function(cid){var candidate=getStatsResult.internal.candidates[cid];candidate.ipAddress.indexOf(result.googLocalAddress)!==-1&&(getStatsResult.connectionType.local.candidateType=candidate.candidateType,getStatsResult.connectionType.local.ipAddress=candidate.ipAddress,getStatsResult.connectionType.local.networkType=candidate.networkType,getStatsResult.connectionType.local.transport=candidate.transport),candidate.ipAddress.indexOf(result.googRemoteAddress)!==-1&&(getStatsResult.connectionType.remote.candidateType=candidate.candidateType,getStatsResult.connectionType.remote.ipAddress=candidate.ipAddress,getStatsResult.connectionType.remote.networkType=candidate.networkType,getStatsResult.connectionType.remote.transport=candidate.transport)}),getStatsResult.connectionType.transport=result.googTransportType;var localCandidate=getStatsResult.internal.candidates[result.localCandidateId];localCandidate&&localCandidate.ipAddress&&(getStatsResult.connectionType.systemIpAddress=localCandidate.ipAddress);var remoteCandidate=getStatsResult.internal.candidates[result.remoteCandidateId];remoteCandidate&&remoteCandidate.ipAddress&&(getStatsResult.connectionType.systemIpAddress=remoteCandidate.ipAddress)}if(("transport"===result.type||"googComponent"===result.type)&&(result.selectedCandidatePairId||result.nominated===!0&&"succeeded"===result.state))var localCandidate=getStatsResult.internal.candidates[result.remoteCandidateId],remoteCandidate=getStatsResult.internal.candidates[result.remoteCandidateId]}};var LOCAL_candidateType={},LOCAL_transport={},LOCAL_ipAddress={},LOCAL_networkType={};getStatsParser.localcandidate=function(result){"localcandidate"!==result.type&&"local-candidate"!==result.type||result.id&&(LOCAL_candidateType[result.id]||(LOCAL_candidateType[result.id]=[]),LOCAL_transport[result.id]||(LOCAL_transport[result.id]=[]),LOCAL_ipAddress[result.id]||(LOCAL_ipAddress[result.id]=[]),LOCAL_networkType[result.id]||(LOCAL_networkType[result.id]=[]),result.candidateType&&LOCAL_candidateType[result.id].indexOf(result.candidateType)===-1&&LOCAL_candidateType[result.id].push(result.candidateType),result.transport&&LOCAL_transport[result.id].indexOf(result.transport)===-1&&LOCAL_transport[result.id].push(result.transport),result.ipAddress&&LOCAL_ipAddress[result.id].indexOf(result.ipAddress+":"+result.portNumber)===-1&&LOCAL_ipAddress[result.id].push(result.ipAddress+":"+result.portNumber),result.networkType&&LOCAL_networkType[result.id].indexOf(result.networkType)===-1&&LOCAL_networkType[result.id].push(result.networkType),getStatsResult.internal.candidates[result.id]={candidateType:LOCAL_candidateType[result.id],ipAddress:LOCAL_ipAddress[result.id],portNumber:result.portNumber,networkType:LOCAL_networkType[result.id],priority:result.priority,transport:LOCAL_transport[result.id],timestamp:result.timestamp,id:result.id,type:result.type},getStatsResult.connectionType.local.candidateType=LOCAL_candidateType[result.id],getStatsResult.connectionType.local.ipAddress=LOCAL_ipAddress[result.id],getStatsResult.connectionType.local.networkType=LOCAL_networkType[result.id],getStatsResult.connectionType.local.transport=LOCAL_transport[result.id])};var REMOTE_candidateType={},REMOTE_transport={},REMOTE_ipAddress={},REMOTE_networkType={};getStatsParser.remotecandidate=function(result){"remotecandidate"!==result.type&&"remote-candidate"!==result.type||result.id&&(REMOTE_candidateType[result.id]||(REMOTE_candidateType[result.id]=[]),REMOTE_transport[result.id]||(REMOTE_transport[result.id]=[]),REMOTE_ipAddress[result.id]||(REMOTE_ipAddress[result.id]=[]),REMOTE_networkType[result.id]||(REMOTE_networkType[result.id]=[]),result.candidateType&&REMOTE_candidateType[result.id].indexOf(result.candidateType)===-1&&REMOTE_candidateType[result.id].push(result.candidateType),result.transport&&REMOTE_transport[result.id].indexOf(result.transport)===-1&&REMOTE_transport[result.id].push(result.transport),result.ipAddress&&REMOTE_ipAddress[result.id].indexOf(result.ipAddress+":"+result.portNumber)===-1&&REMOTE_ipAddress[result.id].push(result.ipAddress+":"+result.portNumber),result.networkType&&REMOTE_networkType[result.id].indexOf(result.networkType)===-1&&REMOTE_networkType[result.id].push(result.networkType),getStatsResult.internal.candidates[result.id]={candidateType:REMOTE_candidateType[result.id],ipAddress:REMOTE_ipAddress[result.id],portNumber:result.portNumber,networkType:REMOTE_networkType[result.id],priority:result.priority,transport:REMOTE_transport[result.id],timestamp:result.timestamp,id:result.id,type:result.type},getStatsResult.connectionType.remote.candidateType=REMOTE_candidateType[result.id],getStatsResult.connectionType.remote.ipAddress=REMOTE_ipAddress[result.id],getStatsResult.connectionType.remote.networkType=REMOTE_networkType[result.id],getStatsResult.connectionType.remote.transport=REMOTE_transport[result.id])},getStatsParser.dataSentReceived=function(result){!result.googCodecName||"video"!==result.mediaType&&"audio"!==result.mediaType||(result.bytesSent&&(getStatsResult[result.mediaType].bytesSent=parseInt(result.bytesSent)),result.bytesReceived&&(getStatsResult[result.mediaType].bytesReceived=parseInt(result.bytesReceived)))};var SSRC={audio:{send:[],recv:[]},video:{send:[],recv:[]}};getStatsParser.ssrc=function(result){if(result.googCodecName&&("video"===result.mediaType||"audio"===result.mediaType)&&"ssrc"===result.type){var sendrecvType=result.id.split("_").pop();"recv"!=sendrecvType&&"send"!=sendrecvType&&(sendrecvType=result.isRemote?"recv":"send"),SSRC[result.mediaType][sendrecvType].indexOf(result.ssrc)===-1&&SSRC[result.mediaType][sendrecvType].push(result.ssrc),getStatsResult[result.mediaType][sendrecvType].streams=SSRC[result.mediaType][sendrecvType].length}},getStatsLooper()}; \ No newline at end of file +"use strict";window.getStats=function(mediaStreamTrack,callback,interval){function preHandler(result){var idMap=result.reduce(function(map,item){return"codec"!=item.type&&"track"!=item.type&&"transport"!=item.type?map:(map[item.id]=item,map)},{});return result.reduce(function(sum,item){return"outbound-rtp"!=item.type&&"inbound-rtp"!=item.type&&item.type.indexOf("candidate")<0?(sum.push(item),sum):(item.type.indexOf("candidate")>=0&&(item.ip&&(item.ipAddress=item.ip),item.protocol&&(item.googTransportType=item.protocol),item.state&&(item.googActiveConnection=("succeeded"==item.state).toString()),item.port&&(item.portNumber=item.port)),item=Object.assign({},idMap[item.transportId],idMap[item.codecId],idMap[item.trackId],item),item.mimeType&&(item.googCodecName=item.mimeType.split("/")[1]),sum.push(item),sum)},[])}function getStatsLooper(){getStatsWrapper(function(results){results.forEach(function(result){Object.keys(getStatsParser).forEach(function(key){"function"==typeof getStatsParser[key]&&getStatsParser[key](result)}),"local-candidate"!==result.type&&"remote-candidate"!==result.type&&"candidate-pair"!==result.type});try{peer.iceConnectionState.search(/failed/gi)!==-1&&(nomore=!0)}catch(e){nomore=!0}nomore===!0&&(getStatsResult.datachannel&&(getStatsResult.datachannel.state="close"),getStatsResult.ended=!0),getStatsResult.results=results,getStatsResult.audio&&getStatsResult.video&&(getStatsResult.bandwidth.speed=getStatsResult.audio.bytesSent-getStatsResult.bandwidth.helper.audioBytesSent+(getStatsResult.video.bytesSent-getStatsResult.bandwidth.helper.videoBytesSent),getStatsResult.bandwidth.helper.audioBytesSent=getStatsResult.audio.bytesSent,getStatsResult.bandwidth.helper.videoBytesSent=getStatsResult.video.bytesSent),callback(getStatsResult),nomore||void 0!=typeof interval&&interval&&setTimeout(getStatsLooper,interval||1e3)})}function getStatsWrapper(cb){peer&&"closed"!=peer.signalingState&&("undefined"!=typeof window.InstallTrigger?peer.getStats(mediaStreamTrack,function(res){var items=[];res.forEach(function(r){items.push(r)}),cb(items)},cb):peer.getStats(function(res){var items=[],result=res.result();console.dir(result),result.forEach(function(res){var item={},names=null;try{names=res.names(),names.forEach(function(name){item[name]=res.stat(name)})}catch(error){item=Object.assign(item,res)}item.id=res.id,item.type=res.type,item.timestamp=res.timestamp,items.push(item)}),console.log("items",items);var items=preHandler(items);cb(items)}))}var RTCPeerConnection=window.RTCPeerConnection||window.mozRTCPeerConnection||window.webkitRTCPeerConnection;"undefined"==typeof MediaStreamTrack&&(MediaStreamTrack={});var systemNetworkType=((navigator.connection||{}).type||"unknown").toString().toLowerCase(),getStatsResult={encryption:"sha-256",audio:{send:{tracks:[],codecs:[],availableBandwidth:0,streams:0},recv:{tracks:[],codecs:[],availableBandwidth:0,streams:0},bytesSent:0,bytesReceived:0},video:{send:{tracks:[],codecs:[],availableBandwidth:0,streams:0},recv:{tracks:[],codecs:[],availableBandwidth:0,streams:0},bytesSent:0,bytesReceived:0},bandwidth:{systemBandwidth:0,sentPerSecond:0,encodedPerSecond:0,helper:{audioBytesSent:0,videoBytesSent:0},speed:0},results:{},connectionType:{systemNetworkType:systemNetworkType,systemIpAddress:"192.168.1.2",local:{candidateType:[],transport:[],ipAddress:[],networkType:[]},remote:{candidateType:[],transport:[],ipAddress:[],networkType:[]}},resolutions:{send:{width:0,height:0},recv:{width:0,height:0}},internal:{audio:{send:{},recv:{}},video:{send:{},recv:{}},candidates:{}},nomore:function(){nomore=!0},bytesToSize:function(bytes){var k=1e3,sizes=["Bytes","KB","MB","GB","TB"];if(bytes<=0)return"0 Bytes";var i=parseInt(Math.floor(Math.log(bytes)/Math.log(k)),10);return sizes[i]?(bytes/Math.pow(k,i)).toPrecision(3)+" "+sizes[i]:"0 Bytes"}},getStatsParser={checkIfOfferer:function(result){"googLibjingleSession"===result.type&&(getStatsResult.isOfferer=result.googInitiator)}},peer=this;if("function"!=typeof arguments[0].getStats)throw"1st argument is not exit getStats function";peer=arguments[0],navigator.mozGetUserMedia&&(mediaStreamTrack=arguments[1],callback=arguments[2],interval=arguments[3]),arguments[0]instanceof RTCPeerConnection?!navigator.mozGetUserMedia||mediaStreamTrack instanceof MediaStreamTrack||console.warn("2nd argument is not instance of MediaStreamTrack."):!navigator.mozGetUserMedia||mediaStreamTrack instanceof MediaStreamTrack||console.warn("1st argument is not instance of MediaStreamTrack.");var nomore=!1;getStatsParser.datachannel=function(result){"datachannel"===result.type&&(getStatsResult.datachannel={state:result.state})},getStatsParser.googCertificate=function(result){"googCertificate"==result.type&&(getStatsResult.encryption=result.googFingerprintAlgorithm)};var AUDIO_codecs=["opus","isac","ilbc"];getStatsParser.checkAudioTracks=function(result){if(result.googCodecName&&"audio"===result.mediaType&&AUDIO_codecs.indexOf(result.googCodecName.toLowerCase())!==-1){var sendrecvType=result.id.split("_").pop();if("recv"!=sendrecvType&&"send"!=sendrecvType&&(sendrecvType=result.isRemote?"recv":"send"),getStatsResult.audio[sendrecvType].codecs.indexOf(result.googCodecName)===-1&&getStatsResult.audio[sendrecvType].codecs.push(result.googCodecName),result.bytesSent){var bytes=0;result.bytesSent&&(getStatsResult.internal.audio[sendrecvType].prevBytesSent||(getStatsResult.internal.audio[sendrecvType].prevBytesSent=result.bytesSent),bytes=result.bytesSent-getStatsResult.internal.audio[sendrecvType].prevBytesSent,getStatsResult.internal.audio[sendrecvType].prevBytesSent=result.bytesSent),getStatsResult.audio[sendrecvType].availableBandwidth=bytes}if(result.bytesReceived&&"0"!==result.bytesReceived){getStatsResult.internal.audio.recv.prevBytesReceived||(getStatsResult.internal.audio.recv.prevBytesReceived=result.bytesReceived);var bytes=result.bytesReceived-getStatsResult.internal.audio.recv.prevBytesReceived;getStatsResult.internal.audio.recv.prevBytesReceived=result.bytesReceived,getStatsResult.audio.recv.availableBandwidth=bytes}getStatsResult.audio[sendrecvType].tracks.indexOf(result.googTrackId)===-1&&getStatsResult.audio[sendrecvType].tracks.push(result.googTrackId)}};var VIDEO_codecs=["vp9","vp8","h264"];getStatsParser.checkVideoTracks=function(result){if(result.googCodecName&&"video"===result.mediaType&&VIDEO_codecs.indexOf(result.googCodecName.toLowerCase())!==-1){var sendrecvType=result.id.split("_").pop();if("recv"!=sendrecvType&&"send"!=sendrecvType&&(sendrecvType=result.isRemote?"recv":"send"),getStatsResult.video[sendrecvType].codecs.indexOf(result.googCodecName)===-1&&getStatsResult.video[sendrecvType].codecs.push(result.googCodecName),result.bytesSent){var bytes=0;(!getStatsResult.internal.video[sendrecvType].prevBytesSent||getStatsResult.internal.video[sendrecvType].prevBytesSent>result.bytesSent)&&(getStatsResult.internal.video[sendrecvType].prevBytesSent=result.bytesSent),bytes=result.bytesSent-getStatsResult.internal.video[sendrecvType].prevBytesSent,getStatsResult.internal.video[sendrecvType].prevBytesSent=result.bytesSent,getStatsResult.video[sendrecvType].availableBandwidth=bytes}if(result.bytesReceived&&"0"!==result.bytesReceived){var bytes=0;(!getStatsResult.internal.video.recv.prevBytesReceived||getStatsResult.internal.video.recv.prevBytesReceived>result.bytesReceived)&&(getStatsResult.internal.video.recv.prevBytesReceived=result.bytesReceived),bytes=result.bytesReceived-getStatsResult.internal.video.recv.prevBytesReceived,getStatsResult.internal.video.recv.prevBytesReceived=result.bytesReceived,getStatsResult.video.recv.availableBandwidth=bytes}if(result.packetsLost){getStatsResult.internal.video.recv.prevLostPacket||(getStatsResult.internal.video.recv.prevLostPacket=result.packetsLost);var bytes=getStatsResult.internal.video.recv.prevBytesReceived,packets=result.packetsLost-getStatsResult.internal.video.recv.prevLostPacket;getStatsResult.video.recv.packetsLostRate=0!=bytes?Math.round(packets/bytes*100)/100+"%":"0.00%"}result.googFrameHeightReceived&&result.googFrameWidthReceived&&(getStatsResult.resolutions[sendrecvType].width=result.googFrameWidthReceived,getStatsResult.resolutions[sendrecvType].height=result.googFrameHeightReceived),result.googFrameHeightSent&&result.googFrameWidthSent&&(getStatsResult.resolutions[sendrecvType].width=result.googFrameWidthSent,getStatsResult.resolutions[sendrecvType].height=result.googFrameHeightSent),getStatsResult.video[sendrecvType].tracks.indexOf(result.googTrackId)===-1&&getStatsResult.video[sendrecvType].tracks.push(result.googTrackId)}},getStatsParser.bweforvideo=function(result){"VideoBwe"!==result.type&&"candidate-pair"!==result.type||(getStatsResult.bandwidth.availableSendBandwidth=result.googAvailableSendBandwidth||result.availableOutgoingBitrate||0,getStatsResult.bandwidth.googActualEncBitrate=result.googActualEncBitrate,getStatsResult.bandwidth.googAvailableSendBandwidth=result.googAvailableSendBandwidth||result.availableOutgoingBitrate||0,getStatsResult.bandwidth.googAvailableReceiveBandwidth=result.googAvailableReceiveBandwidth||result.availableIncomingBitrate||0,getStatsResult.bandwidth.googRetransmitBitrate=result.googRetransmitBitrate,getStatsResult.bandwidth.googTargetEncBitrate=result.googTargetEncBitrate,getStatsResult.bandwidth.googBucketDelay=result.googBucketDelay,getStatsResult.bandwidth.googTransmitBitrate=result.googTransmitBitrate||0)},getStatsParser.candidatePair=function(result){if("googCandidatePair"===result.type||"candidate-pair"===result.type){if("true"==result.googActiveConnection){if(result.bytesSent){getStatsResult.internal.preCandidateBytesSent||(getStatsResult.internal.preCandidateBytesSent=result.bytesSent);var bytes=result.bytesSent-getStatsResult.internal.preCandidateBytesSent;getStatsResult.internal.preCandidateBytesSent=result.bytesSent,getStatsResult.bandwidth.candidateTransmitBitrate=bytes}Object.keys(getStatsResult.internal.candidates).forEach(function(cid){var candidate=getStatsResult.internal.candidates[cid];candidate.ipAddress.indexOf(result.googLocalAddress)!==-1&&(getStatsResult.connectionType.local.candidateType=candidate.candidateType,getStatsResult.connectionType.local.ipAddress=candidate.ipAddress,getStatsResult.connectionType.local.networkType=candidate.networkType,getStatsResult.connectionType.local.transport=candidate.transport),candidate.ipAddress.indexOf(result.googRemoteAddress)!==-1&&(getStatsResult.connectionType.remote.candidateType=candidate.candidateType,getStatsResult.connectionType.remote.ipAddress=candidate.ipAddress,getStatsResult.connectionType.remote.networkType=candidate.networkType,getStatsResult.connectionType.remote.transport=candidate.transport)}),getStatsResult.connectionType.transport=result.googTransportType;var localCandidate=getStatsResult.internal.candidates[result.localCandidateId];localCandidate&&localCandidate.ipAddress&&(getStatsResult.connectionType.systemIpAddress=localCandidate.ipAddress);var remoteCandidate=getStatsResult.internal.candidates[result.remoteCandidateId];remoteCandidate&&remoteCandidate.ipAddress&&(getStatsResult.connectionType.systemIpAddress=remoteCandidate.ipAddress)}if(("transport"===result.type||"googComponent"===result.type)&&(result.selectedCandidatePairId||result.nominated===!0&&"succeeded"===result.state))var localCandidate=getStatsResult.internal.candidates[result.remoteCandidateId],remoteCandidate=getStatsResult.internal.candidates[result.remoteCandidateId]}};var LOCAL_candidateType={},LOCAL_transport={},LOCAL_ipAddress={},LOCAL_networkType={};getStatsParser.localcandidate=function(result){"localcandidate"!==result.type&&"local-candidate"!==result.type||result.id&&(LOCAL_candidateType[result.id]||(LOCAL_candidateType[result.id]=[]),LOCAL_transport[result.id]||(LOCAL_transport[result.id]=[]),LOCAL_ipAddress[result.id]||(LOCAL_ipAddress[result.id]=[]),LOCAL_networkType[result.id]||(LOCAL_networkType[result.id]=[]),result.candidateType&&LOCAL_candidateType[result.id].indexOf(result.candidateType)===-1&&LOCAL_candidateType[result.id].push(result.candidateType),result.transport&&LOCAL_transport[result.id].indexOf(result.transport)===-1&&LOCAL_transport[result.id].push(result.transport),result.ipAddress&&LOCAL_ipAddress[result.id].indexOf(result.ipAddress+":"+result.portNumber)===-1&&LOCAL_ipAddress[result.id].push(result.ipAddress+":"+result.portNumber),result.networkType&&LOCAL_networkType[result.id].indexOf(result.networkType)===-1&&LOCAL_networkType[result.id].push(result.networkType),getStatsResult.internal.candidates[result.id]={candidateType:LOCAL_candidateType[result.id],ipAddress:LOCAL_ipAddress[result.id],portNumber:result.portNumber,networkType:LOCAL_networkType[result.id],priority:result.priority,transport:LOCAL_transport[result.id],timestamp:result.timestamp,id:result.id,type:result.type},getStatsResult.connectionType.local.candidateType=LOCAL_candidateType[result.id],getStatsResult.connectionType.local.ipAddress=LOCAL_ipAddress[result.id],getStatsResult.connectionType.local.networkType=LOCAL_networkType[result.id],getStatsResult.connectionType.local.transport=LOCAL_transport[result.id])};var REMOTE_candidateType={},REMOTE_transport={},REMOTE_ipAddress={},REMOTE_networkType={};getStatsParser.remotecandidate=function(result){"remotecandidate"!==result.type&&"remote-candidate"!==result.type||result.id&&(REMOTE_candidateType[result.id]||(REMOTE_candidateType[result.id]=[]),REMOTE_transport[result.id]||(REMOTE_transport[result.id]=[]),REMOTE_ipAddress[result.id]||(REMOTE_ipAddress[result.id]=[]),REMOTE_networkType[result.id]||(REMOTE_networkType[result.id]=[]),result.candidateType&&REMOTE_candidateType[result.id].indexOf(result.candidateType)===-1&&REMOTE_candidateType[result.id].push(result.candidateType),result.transport&&REMOTE_transport[result.id].indexOf(result.transport)===-1&&REMOTE_transport[result.id].push(result.transport),result.ipAddress&&REMOTE_ipAddress[result.id].indexOf(result.ipAddress+":"+result.portNumber)===-1&&REMOTE_ipAddress[result.id].push(result.ipAddress+":"+result.portNumber),result.networkType&&REMOTE_networkType[result.id].indexOf(result.networkType)===-1&&REMOTE_networkType[result.id].push(result.networkType),getStatsResult.internal.candidates[result.id]={candidateType:REMOTE_candidateType[result.id],ipAddress:REMOTE_ipAddress[result.id],portNumber:result.portNumber,networkType:REMOTE_networkType[result.id],priority:result.priority,transport:REMOTE_transport[result.id],timestamp:result.timestamp,id:result.id,type:result.type},getStatsResult.connectionType.remote.candidateType=REMOTE_candidateType[result.id],getStatsResult.connectionType.remote.ipAddress=REMOTE_ipAddress[result.id],getStatsResult.connectionType.remote.networkType=REMOTE_networkType[result.id],getStatsResult.connectionType.remote.transport=REMOTE_transport[result.id])},getStatsParser.dataSentReceived=function(result){!result.googCodecName||"video"!==result.mediaType&&"audio"!==result.mediaType||(result.bytesSent&&(getStatsResult[result.mediaType].bytesSent=parseInt(result.bytesSent)),result.bytesReceived&&(getStatsResult[result.mediaType].bytesReceived=parseInt(result.bytesReceived)))};var SSRC={audio:{send:[],recv:[]},video:{send:[],recv:[]}};getStatsParser.ssrc=function(result){if(result.googCodecName&&("video"===result.mediaType||"audio"===result.mediaType)&&"ssrc"===result.type){var sendrecvType=result.id.split("_").pop();"recv"!=sendrecvType&&"send"!=sendrecvType&&(sendrecvType=result.isRemote?"recv":"send"),SSRC[result.mediaType][sendrecvType].indexOf(result.ssrc)===-1&&SSRC[result.mediaType][sendrecvType].push(result.ssrc),getStatsResult[result.mediaType][sendrecvType].streams=SSRC[result.mediaType][sendrecvType].length}},getStatsLooper()}; \ No newline at end of file