Skip to content

Commit

Permalink
released: [email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
muaz-khan committed Aug 26, 2017
1 parent 336fa6d commit 4c8766e
Show file tree
Hide file tree
Showing 17 changed files with 558 additions and 302 deletions.
24 changes: 10 additions & 14 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
node_modules
bower_components
# ignore everything
*

.*.swp
._*
.DS_Store
.git
.hg
.npmrc
.lock-wscript
.svn
.wafpickle-*
config.gypi
CVS
npm-debug.log
# but not these files...
!getStats.js
!getStats.min.js
!index.html
!package.json
!bower.json
!server.js
!README.md
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ module.exports = function(grunt) {
'dev/googCodecName.audio.js',
'dev/googCodecName.video.js',
'dev/bweforvideo.js',
'dev/googCandidatePair.js',
'dev/localcandidate.js',
'dev/remotecandidate.js',
'dev/candidate-pair.js',
'dev/local-candidate.js',
'dev/remote-candidate.js',
'dev/dataSentReceived.js',
'dev/ssrc.js',
'dev/tail.js'
Expand Down
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# [getStats.js](https://github.com/muaz-khan/getStats) / [Demo](https://www.webrtc-experiment.com/getStats/)
# [getStats.js](https://github.com/muaz-khan/getStats)

# [Single Page Demo](https://www.webrtc-experiment.com/getStats/) or [Multi User P2P Demo](https://rtcmulticonnection.herokuapp.com/demos/getStats.html)

[![npm](https://img.shields.io/npm/v/getstats.svg)](https://npmjs.org/package/getstats) [![downloads](https://img.shields.io/npm/dm/getstats.svg)](https://npmjs.org/package/getstats) [![Build Status: Linux](https://travis-ci.org/muaz-khan/getStats.png?branch=master)](https://travis-ci.org/muaz-khan/getStats)

A tiny JavaScript library using [WebRTC getStats API](http://dev.w3.org/2011/webrtc/editor/webrtc.html#dom-peerconnection-getstats) to return peer connection stats i.e. bandwidth usage, packets lost, local/remote ip addresses and ports, type of connection etc.

It is <a href="https://www.webrtc-experiment.com/licence/">MIT Licenced</a>, which means that you can use it in any commercial/non-commercial product, free of cost.

![getStats](https://cdn.webrtc-experiment.com/images/getStats.png)

```
npm install getstats
Expand Down Expand Up @@ -33,14 +37,17 @@ To use it:

<!-- 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:

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

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

# `window.getStats`
Expand Down Expand Up @@ -92,8 +99,7 @@ rtcPeerConnection.getPeerStats(function(result) {
result.connectionType.remote.candidateType
result.connectionType.transport

result.audio.send.availableBandwidth
result.audio.recv.availableBandwidth
result.bandwidth.availableSendBandwidth // it will be your system bandwidth for STUN connections
result.audio.packetsSent
result.audio.packetsLost
result.audio.rtt
Expand Down Expand Up @@ -142,6 +148,10 @@ btnStopGetStats.onclick = function() {
};
```

# `result.bandwidth`

1. `result.bandwidth.availableSendBandwidth`

# `result.audio`

1. `result.audio.send.availableBandwidth`
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.4",
"version": "1.0.5",
"authors": [
{
"name": "Muaz Khan",
Expand Down
18 changes: 8 additions & 10 deletions dev/bweforvideo.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
getStatsParser.bweforvideo = function(result) {
if (result.type !== 'VideoBwe') return;

// id === 'bweforvideo'
getStatsResult.bandwidth.availableSendBandwidth = result.googAvailableSendBandwidth;

getStatsResult.video.bandwidth = {
googActualEncBitrate: result.googActualEncBitrate,
googAvailableSendBandwidth: result.googAvailableSendBandwidth,
googAvailableReceiveBandwidth: result.googAvailableReceiveBandwidth,
googRetransmitBitrate: result.googRetransmitBitrate,
googTargetEncBitrate: result.googTargetEncBitrate,
googBucketDelay: result.googBucketDelay,
googTransmitBitrate: result.googTransmitBitrate
};
getStatsResult.bandwidth.googActualEncBitrate = result.googActualEncBitrate;
getStatsResult.bandwidth.googAvailableSendBandwidth = result.googAvailableSendBandwidth;
getStatsResult.bandwidth.googAvailableReceiveBandwidth = result.googAvailableReceiveBandwidth;
getStatsResult.bandwidth.googRetransmitBitrate = result.googRetransmitBitrate;
getStatsResult.bandwidth.googTargetEncBitrate = result.googTargetEncBitrate;
getStatsResult.bandwidth.googBucketDelay = result.googBucketDelay;
getStatsResult.bandwidth.googTransmitBitrate = result.googTransmitBitrate;
};
54 changes: 54 additions & 0 deletions dev/candidate-pair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
getStatsParser.candidatePair = function(result) {
if (result.type !== 'googCandidatePair' && result.type !== 'candidate-pair') return;

// result.googActiveConnection means either STUN or TURN is used.

if (result.googActiveConnection == 'true') {
// id === 'Conn-audio-1-0'
// localCandidateId, remoteCandidateId

// bytesSent, bytesReceived

Object.keys(getStatsResult.internal.candidates).forEach(function(cid) {
var candidate = getStatsResult.internal.candidates[cid];
if (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;
}
if (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];
if (localCandidate) {
if (localCandidate.ipAddress) {
getStatsResult.connectionType.systemIpAddress = localCandidate.ipAddress;
}
}

var remoteCandidate = getStatsResult.internal.candidates[result.remoteCandidateId];
if (remoteCandidate) {
if (remoteCandidate.ipAddress) {
getStatsResult.connectionType.systemIpAddress = remoteCandidate.ipAddress;
}
}
}

if (result.type === 'candidate-pair') {
if (result.selected === true && result.nominated === true && result.state === 'succeeded') {
// remoteCandidateId, localCandidateId, componentId
var localCandidate = getStatsResult.internal.candidates[result.remoteCandidateId];
var remoteCandidate = getStatsResult.internal.candidates[result.remoteCandidateId];

// Firefox used above two pairs for connection
}
}
};
4 changes: 4 additions & 0 deletions dev/getStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function getStatsLooper() {
getStatsParser[key](result);
}
});

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

try {
Expand Down
5 changes: 5 additions & 0 deletions dev/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ var getStatsResult = {
bytesSent: 0,
bytesReceived: 0
},
bandwidth: {
systemBandwidth: 0,
sentPerSecond: 0,
encodedPerSecond: 0
},
results: {},
connectionType: {
systemNetworkType: systemNetworkType,
Expand Down
30 changes: 0 additions & 30 deletions dev/googCandidatePair.js

This file was deleted.

58 changes: 58 additions & 0 deletions dev/local-candidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var LOCAL_candidateType = {};
var LOCAL_transport = {};
var LOCAL_ipAddress = {};
var LOCAL_networkType = {};

getStatsParser.localcandidate = function(result) {
if (result.type !== 'localcandidate' && result.type !== 'local-candidate') return;
if (!result.id) return;

if (!LOCAL_candidateType[result.id]) {
LOCAL_candidateType[result.id] = [];
}

if (!LOCAL_transport[result.id]) {
LOCAL_transport[result.id] = [];
}

if (!LOCAL_ipAddress[result.id]) {
LOCAL_ipAddress[result.id] = [];
}

if (!LOCAL_networkType[result.id]) {
LOCAL_networkType[result.id] = [];
}

if (result.candidateType && LOCAL_candidateType[result.id].indexOf(result.candidateType) === -1) {
LOCAL_candidateType[result.id].push(result.candidateType);
}

if (result.transport && LOCAL_transport[result.id].indexOf(result.transport) === -1) {
LOCAL_transport[result.id].push(result.transport);
}

if (result.ipAddress && LOCAL_ipAddress[result.id].indexOf(result.ipAddress + ':' + result.portNumber) === -1) {
LOCAL_ipAddress[result.id].push(result.ipAddress + ':' + result.portNumber);
}

if (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];
};
41 changes: 0 additions & 41 deletions dev/localcandidate.js

This file was deleted.

58 changes: 58 additions & 0 deletions dev/remote-candidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var REMOTE_candidateType = {};
var REMOTE_transport = {};
var REMOTE_ipAddress = {};
var REMOTE_networkType = {};

getStatsParser.remotecandidate = function(result) {
if (result.type !== 'remotecandidate' && result.type !== 'remote-candidate') return;
if (!result.id) return;

if (!REMOTE_candidateType[result.id]) {
REMOTE_candidateType[result.id] = [];
}

if (!REMOTE_transport[result.id]) {
REMOTE_transport[result.id] = [];
}

if (!REMOTE_ipAddress[result.id]) {
REMOTE_ipAddress[result.id] = [];
}

if (!REMOTE_networkType[result.id]) {
REMOTE_networkType[result.id] = [];
}

if (result.candidateType && REMOTE_candidateType[result.id].indexOf(result.candidateType) === -1) {
REMOTE_candidateType[result.id].push(result.candidateType);
}

if (result.transport && REMOTE_transport[result.id].indexOf(result.transport) === -1) {
REMOTE_transport[result.id].push(result.transport);
}

if (result.ipAddress && REMOTE_ipAddress[result.id].indexOf(result.ipAddress + ':' + result.portNumber) === -1) {
REMOTE_ipAddress[result.id].push(result.ipAddress + ':' + result.portNumber);
}

if (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];
};
Loading

0 comments on commit 4c8766e

Please sign in to comment.