Skip to content

Commit

Permalink
Fixed ReadMe.md; added result.video.packetsLost and result.video.latency
Browse files Browse the repository at this point in the history
  • Loading branch information
muaz-khan committed Dec 26, 2018
1 parent 70dc056 commit f219202
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 67 deletions.
44 changes: 7 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ Or link specific build:

* https://github.com/muaz-khan/getStats/releases

```html
<script src="https://github.com/muaz-khan/getStats/releases/download/1.0.6/getStats.js"></script>
```

# `window.getStats`

To invoke directly:
Expand All @@ -58,43 +54,13 @@ To invoke directly:
getStats(peer, callback, interval);
```

# RTCPeerConnection.prototype.getPeerStats

Or, to setup an instance method:

```javascript
// 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.

```javascript
// following will fail
RTCPeerConnection.prototype.getStats = window.getStats;

// it should be
RTCPeerConnection.prototype.intanceMethodNamae = window.getStats;
```

# Usage

```javascript
var rtcPeerConnection = new RTCPeerConnection(rtcConfig);

var repeatInterval = 2000; // 2000 ms == 2 seconds
rtcPeerConnection.getPeerStats(function(result) {
getStats(rtcPeerConnection, function(result) {
result.connectionType.remote.ipAddress
result.connectionType.remote.candidateType
result.connectionType.transport
Expand All @@ -118,10 +84,14 @@ rtcPeerConnection.getPeerStats(function(result) {
}, repeatInterval);
```

# Firefox?
# Safari?

```javascript
peer.getStats(peer.getLocalStreams()[0].getAudioTracks()[0], function(results) {
var audioTrack = stream.getTracks().filter(function(t) {
return t.kind === 'audio';
});

getStats(peer, audioTrack, function(results) {
// rest goes here
}, 5 * 1000);
```
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "getstats",
"description": "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.",
"version": "1.0.8",
"version": "1.0.9",
"authors": [
{
"name": "Muaz Khan",
Expand Down
10 changes: 3 additions & 7 deletions dev/getStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@ function getStatsLooper() {
if (!results || !results.forEach) return;

results.forEach(function(result) {
// console.error('result', result);
Object.keys(getStatsParser).forEach(function(key) {
if (typeof getStatsParser[key] === 'function') {
try {
getStatsParser[key](result);
} catch (e) {
console.error(e.message, e.stack);
console.error(e.message, e.stack, e);
}
}
});

if (result.type !== 'local-candidate' && result.type !== 'remote-candidate' && result.type !== 'candidate-pair') {
// console.error('result', result);
}
});

try {
// failed|closed
if (peer.iceConnectionState.search(/failed/gi) !== -1) {
if (peer.iceConnectionState.search(/failed|closed|disconnected/gi) !== -1) {
nomore = true;
}
} catch (e) {
Expand Down
8 changes: 6 additions & 2 deletions dev/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ var getStatsResult = {
bitrateMean: 0
},
bytesSent: 0,
bytesReceived: 0
bytesReceived: 0,
latency: 0,
packetsLost: 0
},
video: {
send: {
Expand All @@ -46,7 +48,9 @@ var getStatsResult = {
bitrateMean: 0
},
bytesSent: 0,
bytesReceived: 0
bytesReceived: 0,
latency: 0,
packetsLost: 0
},
bandwidth: {
systemBandwidth: 0,
Expand Down
38 changes: 36 additions & 2 deletions dev/googCodecName.audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ getStatsParser.checkAudioTracks = function(result) {

kilobytes = bytes / 1024;
getStatsResult.audio[sendrecvType].availableBandwidth = kilobytes.toFixed(1);
getStatsResult.video.bytesSent = kilobytes.toFixed(1);
getStatsResult.audio.bytesSent = kilobytes.toFixed(1);
}

if (!!result.bytesReceived) {
Expand All @@ -41,10 +41,44 @@ getStatsParser.checkAudioTracks = function(result) {
kilobytes = bytes / 1024;

// getStatsResult.audio[sendrecvType].availableBandwidth = kilobytes.toFixed(1);
getStatsResult.video.bytesReceived = kilobytes.toFixed(1);
getStatsResult.audio.bytesReceived = kilobytes.toFixed(1);
}

if (result.googTrackId && getStatsResult.audio[sendrecvType].tracks.indexOf(result.googTrackId) === -1) {
getStatsResult.audio[sendrecvType].tracks.push(result.googTrackId);
}

// calculate latency
if (!!result.googCurrentDelayMs) {
var kilobytes = 0;
if (!getStatsResult.internal.audio.prevGoogCurrentDelayMs) {
getStatsResult.internal.audio.prevGoogCurrentDelayMs = result.googCurrentDelayMs;
}

var bytes = result.googCurrentDelayMs - getStatsResult.internal.audio.prevGoogCurrentDelayMs;
getStatsResult.internal.audio.prevGoogCurrentDelayMs = result.googCurrentDelayMs;

getStatsResult.audio.latency = bytes.toFixed(1);

if (getStatsResult.audio.latency < 0) {
getStatsResult.audio.latency = 0;
}
}

// calculate packetsLost
if (!!result.packetsLost) {
var kilobytes = 0;
if (!getStatsResult.internal.audio.prevPacketsLost) {
getStatsResult.internal.audio.prevPacketsLost = result.packetsLost;
}

var bytes = result.packetsLost - getStatsResult.internal.audio.prevPacketsLost;
getStatsResult.internal.audio.prevPacketsLost = result.packetsLost;

getStatsResult.audio.packetsLost = bytes.toFixed(1);

if (getStatsResult.audio.packetsLost < 0) {
getStatsResult.audio.packetsLost = 0;
}
}
};
33 changes: 33 additions & 0 deletions dev/googCodecName.video.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,37 @@ getStatsParser.checkVideoTracks = function(result) {
kilobytes = bytes / 1024;
getStatsResult.video[sendrecvType].bitrateMean = bytes.toFixed(1);
}

// calculate latency
if (!!result.googCurrentDelayMs) {
var kilobytes = 0;
if (!getStatsResult.internal.video.prevGoogCurrentDelayMs) {
getStatsResult.internal.video.prevGoogCurrentDelayMs = result.googCurrentDelayMs;
}

var bytes = result.googCurrentDelayMs - getStatsResult.internal.video.prevGoogCurrentDelayMs;
getStatsResult.internal.video.prevGoogCurrentDelayMs = result.googCurrentDelayMs;

getStatsResult.video.latency = bytes.toFixed(1);

if (getStatsResult.video.latency < 0) {
getStatsResult.video.latency = 0;
}
}

// calculate packetsLost
var kilobytes = 0;
if (!getStatsResult.internal.video.prevPacketsLost) {
getStatsResult.internal.video.prevPacketsLost = result.packetsLost;
}

var bytes = result.packetsLost - getStatsResult.internal.video.prevPacketsLost;
getStatsResult.internal.video.prevPacketsLost = result.packetsLost;

getStatsResult.video.packetsLost = bytes.toFixed(1);

if (getStatsResult.video.packetsLost < 0) {
getStatsResult.video.packetsLost = 0;
}
}
};
94 changes: 81 additions & 13 deletions getStats.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

// Last time updated: 2018-12-19 9:53:31 AM UTC
// Last time updated: 2018-12-20 6:58:08 AM UTC

// _______________
// getStats v1.0.8
// getStats v1.0.9

// Open-Sourced: https://github.com/muaz-khan/getStats

Expand Down Expand Up @@ -42,7 +42,9 @@ window.getStats = function(mediaStreamTrack, callback, interval) {
bitrateMean: 0
},
bytesSent: 0,
bytesReceived: 0
bytesReceived: 0,
latency: 0,
packetsLost: 0
},
video: {
send: {
Expand All @@ -62,7 +64,9 @@ window.getStats = function(mediaStreamTrack, callback, interval) {
bitrateMean: 0
},
bytesSent: 0,
bytesReceived: 0
bytesReceived: 0,
latency: 0,
packetsLost: 0
},
bandwidth: {
systemBandwidth: 0,
Expand Down Expand Up @@ -148,24 +152,20 @@ window.getStats = function(mediaStreamTrack, callback, interval) {
if (!results || !results.forEach) return;

results.forEach(function(result) {
// console.error('result', result);
Object.keys(getStatsParser).forEach(function(key) {
if (typeof getStatsParser[key] === 'function') {
try {
getStatsParser[key](result);
} catch (e) {
console.error(e.message, e.stack);
console.error(e.message, e.stack, e);
}
}
});

if (result.type !== 'local-candidate' && result.type !== 'remote-candidate' && result.type !== 'candidate-pair') {
// console.error('result', result);
}
});

try {
// failed|closed
if (peer.iceConnectionState.search(/failed/gi) !== -1) {
if (peer.iceConnectionState.search(/failed|closed|disconnected/gi) !== -1) {
nomore = true;
}
} catch (e) {
Expand Down Expand Up @@ -279,7 +279,7 @@ window.getStats = function(mediaStreamTrack, callback, interval) {

kilobytes = bytes / 1024;
getStatsResult.audio[sendrecvType].availableBandwidth = kilobytes.toFixed(1);
getStatsResult.video.bytesSent = kilobytes.toFixed(1);
getStatsResult.audio.bytesSent = kilobytes.toFixed(1);
}

if (!!result.bytesReceived) {
Expand All @@ -294,12 +294,46 @@ window.getStats = function(mediaStreamTrack, callback, interval) {
kilobytes = bytes / 1024;

// getStatsResult.audio[sendrecvType].availableBandwidth = kilobytes.toFixed(1);
getStatsResult.video.bytesReceived = kilobytes.toFixed(1);
getStatsResult.audio.bytesReceived = kilobytes.toFixed(1);
}

if (result.googTrackId && getStatsResult.audio[sendrecvType].tracks.indexOf(result.googTrackId) === -1) {
getStatsResult.audio[sendrecvType].tracks.push(result.googTrackId);
}

// calculate latency
if (!!result.googCurrentDelayMs) {
var kilobytes = 0;
if (!getStatsResult.internal.audio.prevGoogCurrentDelayMs) {
getStatsResult.internal.audio.prevGoogCurrentDelayMs = result.googCurrentDelayMs;
}

var bytes = result.googCurrentDelayMs - getStatsResult.internal.audio.prevGoogCurrentDelayMs;
getStatsResult.internal.audio.prevGoogCurrentDelayMs = result.googCurrentDelayMs;

getStatsResult.audio.latency = bytes.toFixed(1);

if (getStatsResult.audio.latency < 0) {
getStatsResult.audio.latency = 0;
}
}

// calculate packetsLost
if (!!result.packetsLost) {
var kilobytes = 0;
if (!getStatsResult.internal.audio.prevPacketsLost) {
getStatsResult.internal.audio.prevPacketsLost = result.packetsLost;
}

var bytes = result.packetsLost - getStatsResult.internal.audio.prevPacketsLost;
getStatsResult.internal.audio.prevPacketsLost = result.packetsLost;

getStatsResult.audio.packetsLost = bytes.toFixed(1);

if (getStatsResult.audio.packetsLost < 0) {
getStatsResult.audio.packetsLost = 0;
}
}
};

getStatsParser.checkVideoTracks = function(result) {
Expand Down Expand Up @@ -389,6 +423,40 @@ window.getStats = function(mediaStreamTrack, callback, interval) {
kilobytes = bytes / 1024;
getStatsResult.video[sendrecvType].bitrateMean = bytes.toFixed(1);
}

// calculate latency
if (!!result.googCurrentDelayMs) {
var kilobytes = 0;
if (!getStatsResult.internal.video.prevGoogCurrentDelayMs) {
getStatsResult.internal.video.prevGoogCurrentDelayMs = result.googCurrentDelayMs;
}

var bytes = result.googCurrentDelayMs - getStatsResult.internal.video.prevGoogCurrentDelayMs;
getStatsResult.internal.video.prevGoogCurrentDelayMs = result.googCurrentDelayMs;

getStatsResult.video.latency = bytes.toFixed(1);

if (getStatsResult.video.latency < 0) {
getStatsResult.video.latency = 0;
}
}

// calculate packetsLost
if (!!result.packetsLost) {
var kilobytes = 0;
if (!getStatsResult.internal.video.prevPacketsLost) {
getStatsResult.internal.video.prevPacketsLost = result.packetsLost;
}

var bytes = result.packetsLost - getStatsResult.internal.video.prevPacketsLost;
getStatsResult.internal.video.prevPacketsLost = result.packetsLost;

getStatsResult.video.packetsLost = bytes.toFixed(1);

if (getStatsResult.video.packetsLost < 0) {
getStatsResult.video.packetsLost = 0;
}
}
};

getStatsParser.bweforvideo = function(result) {
Expand Down
Loading

0 comments on commit f219202

Please sign in to comment.