getStats.js
Single Page Demo or Multi User P2P Demo
A tiny JavaScript library using WebRTC getStats API to return peer connection stats i.e. bandwidth usage, packets lost, local/remote ip addresses and ports, type of connection etc.
It is MIT Licenced, which means that you can use it in any commercial/non-commercial product, free of cost.
npm install getstats
cd node_modules
cd getstats
node server.js
# and open:
# http://localhost:9999/
To use it:
<script src="./node_modules/getstats/getStats.js"></script>
Link the library
<script src="https://cdn.webrtc-experiment.com/getStats.js"></script>
<!-- or min.js -->
<script src="https://cdn.webrtc-experiment.com/getStats.min.js"></script>
<!-- or without CDN -->
<script src="https://www.webrtc-experiment.com/getStats.js"></script>
<!-- or rawgit -->
<script src="https://rawgit.com/muaz-khan/getStats/master/getStats.js"></script>
Or link specific build:
<script src="https://github.com/muaz-khan/getStats/releases/download/1.0.5/getStats.js"></script>
window.getStats
To invoke directly:
getStats(peer, callback, interval);
RTCPeerConnection.prototype.getPeerStats
Or, to setup an instance method:
// if your code is encapsulated under a method
(function() {
RTCPeerConnection.prototype.getPeerStats = window.getStats;
// or
RTCPeerConnection.prototype.__getStats = window.getStats;
// or
RTCPeerConnection.prototype.getConnectionStats = window.getStats;
// or
RTCPeerConnection.prototype['your-choice'] = window.getStats;
})();
NEVER set/override RTCPeerConnection.prototype.getStats
because it is a reserved method.
// following will fail
RTCPeerConnection.prototype.getStats = window.getStats;
// it should be
RTCPeerConnection.prototype.intanceMethodNamae = window.getStats;
Usage
var rtcPeerConnection = new RTCPeerConnection(rtcConfig);
var repeatInterval = 2000; // 2000 ms == 2 seconds
rtcPeerConnection.getPeerStats(function(result) {
result.connectionType.remote.ipAddress
result.connectionType.remote.candidateType
result.connectionType.transport
result.bandwidth.availableSendBandwidth // it will be your system bandwidth for STUN connections
result.audio.packetsSent
result.audio.packetsLost
result.audio.rtt
// to access native "results" array
result.results.forEach(function(r) {
console.log(r);
});
}, repeatInterval);
Firefox?
peer.getStats(peer.getLocalStreams()[0].getAudioTracks()[0], function(results) {
// rest goes here
}, 5 * 1000);
result.datachannel
// states => open or close
alert(result.datachannel.state === 'open');
result.isOfferer
Offerer is the person who invoked createOffer
method.
result.encryption
To detect which tech is used to encrypt your connections.
alert(result.encryption === 'sha-256');
result.nomore()
This function can be used to ask to stop invoking getStats API.
btnStopGetStats.onclick = function() {
getStatsResult.nomore();
};
result.bandwidth
result.bandwidth.availableSendBandwidth
result.audio
result.audio.send.availableBandwidth
result.audio.recv.availableBandwidth
result.audio.inputLevel
result.audio.packetsLost
result.audio.rtt
result.audio.packetsSent
result.audio.bytesSent
-
result.video.tracks.length
/* array */
result.video
result.video.send.availableBandwidth
result.video.recv.availableBandwidth
result.video.googFrameHeightInput
result.video.googFrameWidthInput
result.video.googCaptureQueueDelayMsPerS
result.video.rtt
result.video.packetsLost
result.video.packetsSent
result.video.googEncodeUsagePercent
result.video.googCpuLimitedResolution
result.video.googNacksReceived
result.video.googFrameRateInput
result.video.googPlisReceived
result.video.googViewLimitedResolution
result.video.googCaptureJitterMs
result.video.googAvgEncodeMs
result.video.googFrameHeightSent
result.video.googFrameRateSent
result.video.googBandwidthLimitedResolution
result.video.googFrameWidthSent
result.video.googFirsReceived
result.video.bytesSent
-
result.video.tracks.length
/* array */
result.connectionType
result.connectionType.local.candidateType
-
result.connectionType.local.ipAddress
/* external ip-address */ result.connectionType.local.networkType
result.connectionType.remote.candidateType
result.connectionType.remote.ipAddress
result.connectionType.transport
-
result.connectionType.systemIpAddress
/* 192.168.1.1 */
result.resolutions
result.resolutions.send.width
result.resolutions.send.height
result.resolutions.recv.width
result.resolutions.recv.height
result.results
It is an array that is returned by browser's native PeerConnection API.
console.log(result.results);
License
getStats.js is released under MIT licence . Copyright (c) Muaz Khan.