node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

getstats

getStats.js

Single Page Demo or Multi User P2P Demo

npm downloads Build Status: Linux

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.

getStats

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

  1. result.bandwidth.availableSendBandwidth

result.audio

  1. result.audio.send.availableBandwidth
  2. result.audio.recv.availableBandwidth
  3. result.audio.inputLevel
  4. result.audio.packetsLost
  5. result.audio.rtt
  6. result.audio.packetsSent
  7. result.audio.bytesSent
  8. result.video.tracks.length /* array */

result.video

  1. result.video.send.availableBandwidth
  2. result.video.recv.availableBandwidth
  3. result.video.googFrameHeightInput
  4. result.video.googFrameWidthInput
  5. result.video.googCaptureQueueDelayMsPerS
  6. result.video.rtt
  7. result.video.packetsLost
  8. result.video.packetsSent
  9. result.video.googEncodeUsagePercent
  10. result.video.googCpuLimitedResolution
  11. result.video.googNacksReceived
  12. result.video.googFrameRateInput
  13. result.video.googPlisReceived
  14. result.video.googViewLimitedResolution
  15. result.video.googCaptureJitterMs
  16. result.video.googAvgEncodeMs
  17. result.video.googFrameHeightSent
  18. result.video.googFrameRateSent
  19. result.video.googBandwidthLimitedResolution
  20. result.video.googFrameWidthSent
  21. result.video.googFirsReceived
  22. result.video.bytesSent
  23. result.video.tracks.length /* array */

result.connectionType

  1. result.connectionType.local.candidateType
  2. result.connectionType.local.ipAddress /* external ip-address */
  3. result.connectionType.local.networkType
  4. result.connectionType.remote.candidateType
  5. result.connectionType.remote.ipAddress
  6. result.connectionType.transport
  7. result.connectionType.systemIpAddress /* 192.168.1.1 */

result.resolutions

  1. result.resolutions.send.width
  2. result.resolutions.send.height
  3. result.resolutions.recv.width
  4. 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.