Skip to content

Commit

Permalink
Update example to match webrtc spec's + senders.getStats.
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ivar committed Apr 3, 2017
1 parent f6ea1a4 commit fa7352e
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions webrtc-stats.html
Expand Up @@ -2267,45 +2267,47 @@ <h3>
determine if the cause of it is packet loss. The following example code might be used:
</p>
<pre class="example highlight">var baselineReport, currentReport;
var selector = pc.getRemoteStreams()[0].getAudioTracks()[0];
var sender = pc.getSenders()[0];

pc.getStats(selector, function (report) {
sender.getStats().then(function (report) {
baselineReport = report;
}, logError);

// ... wait a bit
setTimeout(function () {
pc.getStats(selector, function (report) {
currentReport = report;
processStats();
}, logError);
}, aBit);
})
.then(function() {
return new Promise(function(resolve) {
setTimeout(resolve, aBit); // ... wait a bit
});
})
.then(function() {
return sender.getStats();
})
.then(function (report) {
currentReport = report;
processStats();
})
.catch(function (error) {
console.log(error.toString());
});

function processStats() {
// compare the elements from the current report with the baseline
for (var i in currentReport) {
var now = currentReport[i];
if (now.type != "outbund-rtp")
for (let now of currentReport.values()) {
if (now.type != "outbound-rtp")
continue;

// get the corresponding stats from the baseline report
base = baselineReport[now.id];
let base = baselineReport.get(now.id);

if (base) {
remoteNow = currentReport[now.associateStatsId];
remoteBase = baselineReport[base.associateStatsId];
remoteNow = currentReport.get(now.associateStatsId);
remoteBase = baselineReport.get(base.associateStatsId);

var packetsSent = now.packetsSent - base.packetsSent;
var packetsReceived = remoteNow.packetsReceived - remoteBase.packetsReceived;

// if fractionLost is &gt; 0.3, we have probably found the culprit
// if fractionLost is > 0.3, we have probably found the culprit
var fractionLost = (packetsSent - packetsReceived) / packetsSent;
}
}
}

function logError(error) {
log(error.name + ": " + error.message);
});
}
</pre>
</section>
Expand Down

0 comments on commit fa7352e

Please sign in to comment.