-
Notifications
You must be signed in to change notification settings - Fork 3
/
stats.js
38 lines (32 loc) · 1.47 KB
/
stats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* jshint node: true */
const detectProvider = require('./lib/provider');
module.exports = function(qc, opts) {
opts = opts || {};
const provider = detectProvider(opts);
const getStats = (pc) => {
return new Promise((resolve, reject) => {
// A provider must be present for logging to be enabled
if (!provider) return reject('No provider detected');
provider.getStats(pc, null, function(err, reports) {
if (err) return reject(err);
return resolve(reports);
});
});
}
const getConnectionStats = (pc) => {
return getStats(pc).then((reports) => {
const candidatePair = reports.filter((r) => r.type === 'candidatePair' && r.data.writable && r.data.nominated)[0];
const outboundRtp = reports.filter((r) => r.type === 'outboundRtp');
// Available outgoing bitrate not defined (Firefox)
if (candidatePair && candidatePair.data && typeof candidatePair.data.availableOutgoingBitrate === 'undefined') {
// Add a compatibility stat in (not quite the same, as this is purely based off the data that is being sent, as opposed to
// the potential, so named differently but possibly should just be the availableOutgoingBitrate for compatibility)
candidatePair.data.rtcOutgoingBitrateMean = outboundRtp.reduce((result, report) => {
return result + (report.data.bitrateMean || 0);
}, 0);
}
return candidatePair;
});
}
return { provider, getStats, getConnectionStats };
};