2015-01-20 15:56:00 +00:00
|
|
|
/* global ssrc2jid */
|
2014-11-28 15:23:57 +00:00
|
|
|
/* jshint -W117 */
|
2015-01-28 14:35:22 +00:00
|
|
|
var RTCBrowserType = require("../../service/RTC/RTCBrowserType");
|
|
|
|
|
|
|
|
|
2014-06-05 11:09:31 +00:00
|
|
|
/**
|
2014-10-29 10:49:57 +00:00
|
|
|
* Calculates packet lost percent using the number of lost packets and the
|
|
|
|
* number of all packet.
|
2014-10-16 15:11:26 +00:00
|
|
|
* @param lostPackets the number of lost packets
|
|
|
|
* @param totalPackets the number of all packets.
|
|
|
|
* @returns {number} packet loss percent
|
2014-06-05 11:09:31 +00:00
|
|
|
*/
|
2014-10-16 15:11:26 +00:00
|
|
|
function calculatePacketLoss(lostPackets, totalPackets) {
|
2014-10-29 10:49:57 +00:00
|
|
|
if(!totalPackets || totalPackets <= 0 || !lostPackets || lostPackets <= 0)
|
|
|
|
return 0;
|
2014-10-16 15:11:26 +00:00
|
|
|
return Math.round((lostPackets/totalPackets)*100);
|
2014-06-05 11:09:31 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 16:21:25 +00:00
|
|
|
function getStatValue(item, name) {
|
2015-01-28 14:35:22 +00:00
|
|
|
if(!keyMap[APP.RTC.getBrowserType()][name])
|
2014-12-17 16:21:25 +00:00
|
|
|
throw "The property isn't supported!";
|
2015-01-28 14:35:22 +00:00
|
|
|
var key = keyMap[APP.RTC.getBrowserType()][name];
|
|
|
|
return APP.RTC.getBrowserType() == RTCBrowserType.RTC_BROWSER_CHROME? item.stat(key) : item[key];
|
2014-12-17 16:21:25 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 11:09:31 +00:00
|
|
|
/**
|
|
|
|
* Peer statistics data holder.
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function PeerStats()
|
|
|
|
{
|
|
|
|
this.ssrc2Loss = {};
|
|
|
|
this.ssrc2AudioLevel = {};
|
2014-10-16 15:11:26 +00:00
|
|
|
this.ssrc2bitrate = {};
|
2014-10-21 09:19:17 +00:00
|
|
|
this.ssrc2resolution = {};
|
2014-06-05 11:09:31 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
/**
|
|
|
|
* The bandwidth
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
PeerStats.bandwidth = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The bit rate
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
PeerStats.bitrate = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The packet loss rate
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
PeerStats.packetLoss = null;
|
|
|
|
|
2014-06-05 11:09:31 +00:00
|
|
|
/**
|
|
|
|
* Sets packets loss rate for given <tt>ssrc</tt> that blong to the peer
|
|
|
|
* represented by this instance.
|
|
|
|
* @param ssrc audio or video RTP stream SSRC.
|
|
|
|
* @param lossRate new packet loss rate value to be set.
|
|
|
|
*/
|
|
|
|
PeerStats.prototype.setSsrcLoss = function (ssrc, lossRate)
|
|
|
|
{
|
|
|
|
this.ssrc2Loss[ssrc] = lossRate;
|
|
|
|
};
|
|
|
|
|
2014-10-21 09:19:17 +00:00
|
|
|
/**
|
|
|
|
* Sets resolution for given <tt>ssrc</tt> that belong to the peer
|
|
|
|
* represented by this instance.
|
|
|
|
* @param ssrc audio or video RTP stream SSRC.
|
|
|
|
* @param resolution new resolution value to be set.
|
|
|
|
*/
|
|
|
|
PeerStats.prototype.setSsrcResolution = function (ssrc, resolution)
|
|
|
|
{
|
2014-10-29 10:49:57 +00:00
|
|
|
if(resolution === null && this.ssrc2resolution[ssrc])
|
2014-10-21 09:19:17 +00:00
|
|
|
{
|
|
|
|
delete this.ssrc2resolution[ssrc];
|
|
|
|
}
|
2014-10-29 10:49:57 +00:00
|
|
|
else if(resolution !== null)
|
2014-10-21 09:19:17 +00:00
|
|
|
this.ssrc2resolution[ssrc] = resolution;
|
|
|
|
};
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
/**
|
|
|
|
* Sets the bit rate for given <tt>ssrc</tt> that blong to the peer
|
|
|
|
* represented by this instance.
|
|
|
|
* @param ssrc audio or video RTP stream SSRC.
|
|
|
|
* @param bitrate new bitrate value to be set.
|
|
|
|
*/
|
|
|
|
PeerStats.prototype.setSsrcBitrate = function (ssrc, bitrate)
|
|
|
|
{
|
2014-12-01 10:58:03 +00:00
|
|
|
if(this.ssrc2bitrate[ssrc])
|
|
|
|
{
|
|
|
|
this.ssrc2bitrate[ssrc].download += bitrate.download;
|
|
|
|
this.ssrc2bitrate[ssrc].upload += bitrate.upload;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.ssrc2bitrate[ssrc] = bitrate;
|
|
|
|
}
|
2014-10-16 15:11:26 +00:00
|
|
|
};
|
|
|
|
|
2014-06-05 11:09:31 +00:00
|
|
|
/**
|
|
|
|
* Sets new audio level(input or output) for given <tt>ssrc</tt> that identifies
|
|
|
|
* the stream which belongs to the peer represented by this instance.
|
|
|
|
* @param ssrc RTP stream SSRC for which current audio level value will be
|
|
|
|
* updated.
|
|
|
|
* @param audioLevel the new audio level value to be set. Value is truncated to
|
|
|
|
* fit the range from 0 to 1.
|
|
|
|
*/
|
|
|
|
PeerStats.prototype.setSsrcAudioLevel = function (ssrc, audioLevel)
|
|
|
|
{
|
|
|
|
// Range limit 0 - 1
|
2015-02-11 16:29:20 +00:00
|
|
|
this.ssrc2AudioLevel[ssrc] = formatAudioLevel(audioLevel);
|
2014-06-05 11:09:31 +00:00
|
|
|
};
|
|
|
|
|
2015-02-11 16:29:20 +00:00
|
|
|
function formatAudioLevel(audioLevel) {
|
|
|
|
return Math.min(Math.max(audioLevel, 0), 1);
|
|
|
|
}
|
|
|
|
|
2014-06-05 11:09:31 +00:00
|
|
|
/**
|
2014-10-16 15:11:26 +00:00
|
|
|
* Array with the transport information.
|
|
|
|
* @type {Array}
|
2014-06-05 11:09:31 +00:00
|
|
|
*/
|
2014-10-16 15:11:26 +00:00
|
|
|
PeerStats.transport = [];
|
2014-06-05 11:09:31 +00:00
|
|
|
|
2014-11-28 17:47:19 +00:00
|
|
|
|
2014-06-05 11:09:31 +00:00
|
|
|
/**
|
|
|
|
* <tt>StatsCollector</tt> registers for stats updates of given
|
|
|
|
* <tt>peerconnection</tt> in given <tt>interval</tt>. On each update particular
|
|
|
|
* stats are extracted and put in {@link PeerStats} objects. Once the processing
|
2014-10-29 10:49:57 +00:00
|
|
|
* is done <tt>audioLevelsUpdateCallback</tt> is called with <tt>this</tt>
|
|
|
|
* instance as an event source.
|
2014-06-05 11:09:31 +00:00
|
|
|
*
|
|
|
|
* @param peerconnection webRTC peer connection object.
|
|
|
|
* @param interval stats refresh interval given in ms.
|
2014-10-29 10:49:57 +00:00
|
|
|
* @param {function(StatsCollector)} audioLevelsUpdateCallback the callback
|
|
|
|
* called on stats update.
|
2014-06-05 11:09:31 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2014-12-17 16:21:25 +00:00
|
|
|
function StatsCollector(peerconnection, audioLevelsInterval, statsInterval, eventEmitter)
|
2014-06-05 11:09:31 +00:00
|
|
|
{
|
|
|
|
this.peerconnection = peerconnection;
|
2014-10-16 15:11:26 +00:00
|
|
|
this.baselineAudioLevelsReport = null;
|
|
|
|
this.currentAudioLevelsReport = null;
|
|
|
|
this.currentStatsReport = null;
|
|
|
|
this.baselineStatsReport = null;
|
|
|
|
this.audioLevelsIntervalId = null;
|
2014-12-17 16:21:25 +00:00
|
|
|
this.eventEmitter = eventEmitter;
|
2014-11-28 15:23:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gather PeerConnection stats once every this many milliseconds.
|
|
|
|
*/
|
2015-02-25 10:38:04 +00:00
|
|
|
this.GATHER_INTERVAL = 15000;
|
2014-11-28 15:23:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log stats via the focus once every this many milliseconds.
|
|
|
|
*/
|
|
|
|
this.LOG_INTERVAL = 60000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gather stats and store them in this.statsToBeLogged.
|
|
|
|
*/
|
|
|
|
this.gatherStatsIntervalId = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the stats already saved in this.statsToBeLogged to be logged via
|
|
|
|
* the focus.
|
|
|
|
*/
|
|
|
|
this.logStatsIntervalId = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the statistics which will be send to the focus to be logged.
|
|
|
|
*/
|
|
|
|
this.statsToBeLogged =
|
|
|
|
{
|
2014-12-17 16:21:25 +00:00
|
|
|
timestamps: [],
|
|
|
|
stats: {}
|
2014-11-28 15:23:57 +00:00
|
|
|
};
|
|
|
|
|
2014-06-05 11:09:31 +00:00
|
|
|
// Updates stats interval
|
2014-10-16 15:11:26 +00:00
|
|
|
this.audioLevelsIntervalMilis = audioLevelsInterval;
|
|
|
|
|
|
|
|
this.statsIntervalId = null;
|
|
|
|
this.statsIntervalMilis = statsInterval;
|
2014-06-05 11:09:31 +00:00
|
|
|
// Map of jids to PeerStats
|
|
|
|
this.jid2stats = {};
|
|
|
|
}
|
|
|
|
|
2014-12-17 16:21:25 +00:00
|
|
|
module.exports = StatsCollector;
|
|
|
|
|
2014-06-05 11:09:31 +00:00
|
|
|
/**
|
|
|
|
* Stops stats updates.
|
|
|
|
*/
|
2015-02-11 16:29:20 +00:00
|
|
|
StatsCollector.prototype.stop = function () {
|
|
|
|
if (this.audioLevelsIntervalId) {
|
2014-10-16 15:11:26 +00:00
|
|
|
clearInterval(this.audioLevelsIntervalId);
|
|
|
|
this.audioLevelsIntervalId = null;
|
2015-02-11 16:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.statsIntervalId)
|
|
|
|
{
|
2014-10-16 15:11:26 +00:00
|
|
|
clearInterval(this.statsIntervalId);
|
|
|
|
this.statsIntervalId = null;
|
2015-02-11 16:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(this.logStatsIntervalId)
|
|
|
|
{
|
2014-11-28 15:23:57 +00:00
|
|
|
clearInterval(this.logStatsIntervalId);
|
|
|
|
this.logStatsIntervalId = null;
|
2015-02-11 16:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(this.gatherStatsIntervalId)
|
|
|
|
{
|
2014-11-28 15:23:57 +00:00
|
|
|
clearInterval(this.gatherStatsIntervalId);
|
|
|
|
this.gatherStatsIntervalId = null;
|
2014-06-05 11:09:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback passed to <tt>getStats</tt> method.
|
|
|
|
* @param error an error that occurred on <tt>getStats</tt> call.
|
|
|
|
*/
|
|
|
|
StatsCollector.prototype.errorCallback = function (error)
|
|
|
|
{
|
|
|
|
console.error("Get stats error", error);
|
|
|
|
this.stop();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts stats updates.
|
|
|
|
*/
|
|
|
|
StatsCollector.prototype.start = function ()
|
|
|
|
{
|
|
|
|
var self = this;
|
2015-02-09 12:51:25 +00:00
|
|
|
if(!config.disableAudioLevels) {
|
2015-02-11 16:29:20 +00:00
|
|
|
console.debug("set audio levels interval");
|
2015-02-09 12:51:25 +00:00
|
|
|
this.audioLevelsIntervalId = setInterval(
|
|
|
|
function () {
|
|
|
|
// Interval updates
|
|
|
|
self.peerconnection.getStats(
|
|
|
|
function (report) {
|
|
|
|
var results = null;
|
|
|
|
if (!report || !report.result ||
|
|
|
|
typeof report.result != 'function') {
|
|
|
|
results = report;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
results = report.result();
|
|
|
|
}
|
|
|
|
//console.error("Got interval report", results);
|
|
|
|
self.currentAudioLevelsReport = results;
|
|
|
|
self.processAudioLevelReport();
|
|
|
|
self.baselineAudioLevelsReport =
|
|
|
|
self.currentAudioLevelsReport;
|
|
|
|
},
|
|
|
|
self.errorCallback
|
|
|
|
);
|
|
|
|
},
|
|
|
|
self.audioLevelsIntervalMilis
|
|
|
|
);
|
|
|
|
}
|
2014-11-28 17:47:19 +00:00
|
|
|
|
2015-02-09 12:51:25 +00:00
|
|
|
if(!config.disableStats) {
|
2015-02-11 16:29:20 +00:00
|
|
|
console.debug("set stats interval");
|
2015-02-09 12:51:25 +00:00
|
|
|
this.statsIntervalId = setInterval(
|
|
|
|
function () {
|
|
|
|
// Interval updates
|
|
|
|
self.peerconnection.getStats(
|
|
|
|
function (report) {
|
|
|
|
var results = null;
|
|
|
|
if (!report || !report.result ||
|
|
|
|
typeof report.result != 'function') {
|
|
|
|
//firefox
|
|
|
|
results = report;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//chrome
|
|
|
|
results = report.result();
|
|
|
|
}
|
|
|
|
//console.error("Got interval report", results);
|
|
|
|
self.currentStatsReport = results;
|
|
|
|
try {
|
|
|
|
self.processStatsReport();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.error("Unsupported key:" + e, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.baselineStatsReport = self.currentStatsReport;
|
|
|
|
},
|
|
|
|
self.errorCallback
|
|
|
|
);
|
|
|
|
},
|
|
|
|
self.statsIntervalMilis
|
|
|
|
);
|
|
|
|
}
|
2014-11-28 15:23:57 +00:00
|
|
|
|
|
|
|
if (config.logStats) {
|
|
|
|
this.gatherStatsIntervalId = setInterval(
|
|
|
|
function () {
|
|
|
|
self.peerconnection.getStats(
|
|
|
|
function (report) {
|
|
|
|
self.addStatsToBeLogged(report.result());
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
this.GATHER_INTERVAL
|
|
|
|
);
|
|
|
|
|
|
|
|
this.logStatsIntervalId = setInterval(
|
|
|
|
function() { self.logStats(); },
|
|
|
|
this.LOG_INTERVAL);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-25 10:38:04 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a certain record should be included in the logged statistics.
|
|
|
|
*/
|
|
|
|
function acceptStat(reportId, reportType, statName) {
|
|
|
|
if (reportType == "googCandidatePair" && statName == "googChannelId")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (reportType == "ssrc") {
|
|
|
|
if (statName == "googTrackId" ||
|
|
|
|
statName == "transportId" ||
|
|
|
|
statName == "ssrc")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a certain record should be included in the logged statistics.
|
|
|
|
*/
|
|
|
|
function acceptReport(id, type) {
|
|
|
|
if (id.substring(0, 15) == "googCertificate" ||
|
|
|
|
id.substring(0, 9) == "googTrack" ||
|
|
|
|
id.substring(0, 20) == "googLibjingleSession")
|
|
|
|
return false;
|
|
|
|
|
2015-02-26 09:03:29 +00:00
|
|
|
if (type == "googComponent")
|
2015-02-25 10:38:04 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-28 15:23:57 +00:00
|
|
|
/**
|
|
|
|
* Converts the stats to the format used for logging, and saves the data in
|
|
|
|
* this.statsToBeLogged.
|
|
|
|
* @param reports Reports as given by webkitRTCPerConnection.getStats.
|
|
|
|
*/
|
|
|
|
StatsCollector.prototype.addStatsToBeLogged = function (reports) {
|
|
|
|
var self = this;
|
|
|
|
var num_records = this.statsToBeLogged.timestamps.length;
|
|
|
|
this.statsToBeLogged.timestamps.push(new Date().getTime());
|
|
|
|
reports.map(function (report) {
|
2015-02-25 10:38:04 +00:00
|
|
|
if (!acceptReport(report.id, report.type))
|
|
|
|
return;
|
2014-11-28 15:23:57 +00:00
|
|
|
var stat = self.statsToBeLogged.stats[report.id];
|
|
|
|
if (!stat) {
|
|
|
|
stat = self.statsToBeLogged.stats[report.id] = {};
|
|
|
|
}
|
|
|
|
stat.type = report.type;
|
|
|
|
report.names().map(function (name) {
|
2015-02-25 10:38:04 +00:00
|
|
|
if (!acceptStat(report.id, report.type, name))
|
|
|
|
return;
|
2014-11-28 15:23:57 +00:00
|
|
|
var values = stat[name];
|
|
|
|
if (!values) {
|
|
|
|
values = stat[name] = [];
|
|
|
|
}
|
|
|
|
while (values.length < num_records) {
|
|
|
|
values.push(null);
|
|
|
|
}
|
|
|
|
values.push(report.stat(name));
|
|
|
|
});
|
|
|
|
});
|
2014-06-05 11:09:31 +00:00
|
|
|
};
|
|
|
|
|
2014-11-28 15:23:57 +00:00
|
|
|
StatsCollector.prototype.logStats = function () {
|
|
|
|
|
2015-01-28 14:35:22 +00:00
|
|
|
if(!APP.xmpp.sendLogs(this.statsToBeLogged))
|
2015-01-19 09:20:00 +00:00
|
|
|
return;
|
2014-11-28 15:23:57 +00:00
|
|
|
// Reset the stats
|
|
|
|
this.statsToBeLogged.stats = {};
|
|
|
|
this.statsToBeLogged.timestamps = [];
|
|
|
|
};
|
2014-12-19 13:59:08 +00:00
|
|
|
var keyMap = {};
|
|
|
|
keyMap[RTCBrowserType.RTC_BROWSER_FIREFOX] = {
|
|
|
|
"ssrc": "ssrc",
|
|
|
|
"packetsReceived": "packetsReceived",
|
|
|
|
"packetsLost": "packetsLost",
|
|
|
|
"packetsSent": "packetsSent",
|
|
|
|
"bytesReceived": "bytesReceived",
|
|
|
|
"bytesSent": "bytesSent"
|
|
|
|
};
|
|
|
|
keyMap[RTCBrowserType.RTC_BROWSER_CHROME] = {
|
|
|
|
"receiveBandwidth": "googAvailableReceiveBandwidth",
|
|
|
|
"sendBandwidth": "googAvailableSendBandwidth",
|
|
|
|
"remoteAddress": "googRemoteAddress",
|
|
|
|
"transportType": "googTransportType",
|
|
|
|
"localAddress": "googLocalAddress",
|
|
|
|
"activeConnection": "googActiveConnection",
|
|
|
|
"ssrc": "ssrc",
|
|
|
|
"packetsReceived": "packetsReceived",
|
|
|
|
"packetsSent": "packetsSent",
|
|
|
|
"packetsLost": "packetsLost",
|
|
|
|
"bytesReceived": "bytesReceived",
|
|
|
|
"bytesSent": "bytesSent",
|
|
|
|
"googFrameHeightReceived": "googFrameHeightReceived",
|
|
|
|
"googFrameWidthReceived": "googFrameWidthReceived",
|
|
|
|
"googFrameHeightSent": "googFrameHeightSent",
|
|
|
|
"googFrameWidthSent": "googFrameWidthSent",
|
|
|
|
"audioInputLevel": "audioInputLevel",
|
|
|
|
"audioOutputLevel": "audioOutputLevel"
|
2014-11-28 17:47:19 +00:00
|
|
|
};
|
2014-10-16 15:11:26 +00:00
|
|
|
|
2014-12-19 13:59:08 +00:00
|
|
|
|
2014-06-05 11:09:31 +00:00
|
|
|
/**
|
|
|
|
* Stats processing logic.
|
|
|
|
*/
|
2014-10-16 15:11:26 +00:00
|
|
|
StatsCollector.prototype.processStatsReport = function () {
|
|
|
|
if (!this.baselineStatsReport) {
|
2014-06-05 11:09:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
for (var idx in this.currentStatsReport) {
|
|
|
|
var now = this.currentStatsReport[idx];
|
2014-11-28 17:47:19 +00:00
|
|
|
try {
|
|
|
|
if (getStatValue(now, 'receiveBandwidth') ||
|
|
|
|
getStatValue(now, 'sendBandwidth')) {
|
|
|
|
PeerStats.bandwidth = {
|
|
|
|
"download": Math.round(
|
|
|
|
(getStatValue(now, 'receiveBandwidth')) / 1000),
|
|
|
|
"upload": Math.round(
|
|
|
|
(getStatValue(now, 'sendBandwidth')) / 1000)
|
|
|
|
};
|
|
|
|
}
|
2014-10-16 15:11:26 +00:00
|
|
|
}
|
2014-11-28 17:47:19 +00:00
|
|
|
catch(e){/*not supported*/}
|
2014-10-16 15:11:26 +00:00
|
|
|
|
|
|
|
if(now.type == 'googCandidatePair')
|
2014-06-05 11:09:31 +00:00
|
|
|
{
|
2014-11-28 17:47:19 +00:00
|
|
|
var ip, type, localIP, active;
|
|
|
|
try {
|
|
|
|
ip = getStatValue(now, 'remoteAddress');
|
|
|
|
type = getStatValue(now, "transportType");
|
|
|
|
localIP = getStatValue(now, "localAddress");
|
|
|
|
active = getStatValue(now, "activeConnection");
|
|
|
|
}
|
|
|
|
catch(e){/*not supported*/}
|
2014-10-17 14:16:59 +00:00
|
|
|
if(!ip || !type || !localIP || active != "true")
|
2014-10-16 15:11:26 +00:00
|
|
|
continue;
|
|
|
|
var addressSaved = false;
|
|
|
|
for(var i = 0; i < PeerStats.transport.length; i++)
|
|
|
|
{
|
2014-10-29 10:49:57 +00:00
|
|
|
if(PeerStats.transport[i].ip == ip &&
|
|
|
|
PeerStats.transport[i].type == type &&
|
2014-10-17 10:00:35 +00:00
|
|
|
PeerStats.transport[i].localip == localIP)
|
2014-10-16 15:11:26 +00:00
|
|
|
{
|
|
|
|
addressSaved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(addressSaved)
|
|
|
|
continue;
|
2014-10-17 10:00:35 +00:00
|
|
|
PeerStats.transport.push({localip: localIP, ip: ip, type: type});
|
2014-06-05 11:09:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-28 17:47:19 +00:00
|
|
|
if(now.type == "candidatepair")
|
|
|
|
{
|
|
|
|
if(now.state == "succeeded")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var local = this.currentStatsReport[now.localCandidateId];
|
|
|
|
var remote = this.currentStatsReport[now.remoteCandidateId];
|
|
|
|
PeerStats.transport.push({localip: local.ipAddress + ":" + local.portNumber,
|
|
|
|
ip: remote.ipAddress + ":" + remote.portNumber, type: local.transport});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now.type != 'ssrc' && now.type != "outboundrtp" &&
|
|
|
|
now.type != "inboundrtp") {
|
2014-10-16 15:11:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var before = this.baselineStatsReport[idx];
|
|
|
|
if (!before) {
|
2014-11-28 17:47:19 +00:00
|
|
|
console.warn(getStatValue(now, 'ssrc') + ' not enough data');
|
2014-06-05 11:09:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-28 17:47:19 +00:00
|
|
|
var ssrc = getStatValue(now, 'ssrc');
|
|
|
|
if(!ssrc)
|
|
|
|
continue;
|
2015-01-28 14:35:22 +00:00
|
|
|
var jid = APP.xmpp.getJidFromSSRC(ssrc);
|
2015-01-05 12:04:02 +00:00
|
|
|
if (!jid && (Date.now() - now.timestamp) < 3000) {
|
2014-06-05 11:09:31 +00:00
|
|
|
console.warn("No jid for ssrc: " + ssrc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var jidStats = this.jid2stats[jid];
|
2014-10-16 15:11:26 +00:00
|
|
|
if (!jidStats) {
|
2014-06-05 11:09:31 +00:00
|
|
|
jidStats = new PeerStats();
|
|
|
|
this.jid2stats[jid] = jidStats;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
var isDownloadStream = true;
|
2014-06-05 11:09:31 +00:00
|
|
|
var key = 'packetsReceived';
|
2014-11-28 17:47:19 +00:00
|
|
|
if (!getStatValue(now, key))
|
2014-06-05 11:09:31 +00:00
|
|
|
{
|
2014-10-16 15:11:26 +00:00
|
|
|
isDownloadStream = false;
|
2014-06-05 11:09:31 +00:00
|
|
|
key = 'packetsSent';
|
2014-11-28 17:47:19 +00:00
|
|
|
if (!getStatValue(now, key))
|
2014-06-05 11:09:31 +00:00
|
|
|
{
|
2014-11-28 17:47:19 +00:00
|
|
|
console.warn("No packetsReceived nor packetSent stat found");
|
|
|
|
continue;
|
2014-06-05 11:09:31 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-28 17:47:19 +00:00
|
|
|
var packetsNow = getStatValue(now, key);
|
2014-10-17 10:00:35 +00:00
|
|
|
if(!packetsNow || packetsNow < 0)
|
|
|
|
packetsNow = 0;
|
|
|
|
|
2014-11-28 17:47:19 +00:00
|
|
|
var packetsBefore = getStatValue(before, key);
|
2014-10-17 10:00:35 +00:00
|
|
|
if(!packetsBefore || packetsBefore < 0)
|
|
|
|
packetsBefore = 0;
|
2014-06-05 11:09:31 +00:00
|
|
|
var packetRate = packetsNow - packetsBefore;
|
2014-10-29 10:49:57 +00:00
|
|
|
if(!packetRate || packetRate < 0)
|
|
|
|
packetRate = 0;
|
2014-11-28 17:47:19 +00:00
|
|
|
var currentLoss = getStatValue(now, 'packetsLost');
|
2014-10-17 10:00:35 +00:00
|
|
|
if(!currentLoss || currentLoss < 0)
|
|
|
|
currentLoss = 0;
|
2014-11-28 17:47:19 +00:00
|
|
|
var previousLoss = getStatValue(before, 'packetsLost');
|
2014-10-17 10:00:35 +00:00
|
|
|
if(!previousLoss || previousLoss < 0)
|
|
|
|
previousLoss = 0;
|
2014-06-05 11:09:31 +00:00
|
|
|
var lossRate = currentLoss - previousLoss;
|
2014-10-29 10:49:57 +00:00
|
|
|
if(!lossRate || lossRate < 0)
|
2014-10-17 10:00:35 +00:00
|
|
|
lossRate = 0;
|
2014-06-05 11:09:31 +00:00
|
|
|
var packetsTotal = (packetRate + lossRate);
|
|
|
|
|
2014-10-29 10:49:57 +00:00
|
|
|
jidStats.setSsrcLoss(ssrc,
|
|
|
|
{"packetsTotal": packetsTotal,
|
|
|
|
"packetsLost": lossRate,
|
|
|
|
"isDownloadStream": isDownloadStream});
|
2014-10-16 15:11:26 +00:00
|
|
|
|
2014-12-01 10:58:03 +00:00
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
var bytesReceived = 0, bytesSent = 0;
|
2014-11-28 17:47:19 +00:00
|
|
|
if(getStatValue(now, "bytesReceived"))
|
2014-10-16 15:11:26 +00:00
|
|
|
{
|
2014-11-28 17:47:19 +00:00
|
|
|
bytesReceived = getStatValue(now, "bytesReceived") -
|
|
|
|
getStatValue(before, "bytesReceived");
|
2014-10-16 15:11:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 17:47:19 +00:00
|
|
|
if(getStatValue(now, "bytesSent"))
|
2014-10-16 15:11:26 +00:00
|
|
|
{
|
2014-12-01 10:58:03 +00:00
|
|
|
bytesSent = getStatValue(now, "bytesSent") -
|
|
|
|
getStatValue(before, "bytesSent");
|
2014-10-16 15:11:26 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 10:49:57 +00:00
|
|
|
var time = Math.round((now.timestamp - before.timestamp) / 1000);
|
|
|
|
if(bytesReceived <= 0 || time <= 0)
|
|
|
|
{
|
2014-10-16 15:11:26 +00:00
|
|
|
bytesReceived = 0;
|
2014-10-29 10:49:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytesReceived = Math.round(((bytesReceived * 8) / time) / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bytesSent <= 0 || time <= 0)
|
|
|
|
{
|
2014-10-16 15:11:26 +00:00
|
|
|
bytesSent = 0;
|
2014-10-29 10:49:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytesSent = Math.round(((bytesSent * 8) / time) / 1000);
|
|
|
|
}
|
2014-10-16 15:11:26 +00:00
|
|
|
|
|
|
|
jidStats.setSsrcBitrate(ssrc, {
|
2014-10-29 10:49:57 +00:00
|
|
|
"download": bytesReceived,
|
|
|
|
"upload": bytesSent});
|
2014-12-01 10:58:03 +00:00
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
var resolution = {height: null, width: null};
|
2014-11-28 17:47:19 +00:00
|
|
|
try {
|
|
|
|
if (getStatValue(now, "googFrameHeightReceived") &&
|
|
|
|
getStatValue(now, "googFrameWidthReceived")) {
|
|
|
|
resolution.height = getStatValue(now, "googFrameHeightReceived");
|
|
|
|
resolution.width = getStatValue(now, "googFrameWidthReceived");
|
|
|
|
}
|
|
|
|
else if (getStatValue(now, "googFrameHeightSent") &&
|
|
|
|
getStatValue(now, "googFrameWidthSent")) {
|
|
|
|
resolution.height = getStatValue(now, "googFrameHeightSent");
|
|
|
|
resolution.width = getStatValue(now, "googFrameWidthSent");
|
|
|
|
}
|
2014-10-16 15:11:26 +00:00
|
|
|
}
|
2014-11-28 17:47:19 +00:00
|
|
|
catch(e){/*not supported*/}
|
2014-10-16 15:11:26 +00:00
|
|
|
|
|
|
|
if(resolution.height && resolution.width)
|
|
|
|
{
|
2014-10-21 09:19:17 +00:00
|
|
|
jidStats.setSsrcResolution(ssrc, resolution);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
jidStats.setSsrcResolution(ssrc, null);
|
2014-10-16 15:11:26 +00:00
|
|
|
}
|
2014-06-05 11:09:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
// Jid stats
|
2014-10-16 15:11:26 +00:00
|
|
|
var totalPackets = {download: 0, upload: 0};
|
|
|
|
var lostPackets = {download: 0, upload: 0};
|
|
|
|
var bitrateDownload = 0;
|
|
|
|
var bitrateUpload = 0;
|
2014-10-29 10:49:57 +00:00
|
|
|
var resolutions = {};
|
2014-10-16 15:11:26 +00:00
|
|
|
Object.keys(this.jid2stats).forEach(
|
2014-06-05 11:09:31 +00:00
|
|
|
function (jid)
|
|
|
|
{
|
2014-10-16 15:11:26 +00:00
|
|
|
Object.keys(self.jid2stats[jid].ssrc2Loss).forEach(
|
|
|
|
function (ssrc)
|
2014-06-05 11:09:31 +00:00
|
|
|
{
|
2014-10-16 15:11:26 +00:00
|
|
|
var type = "upload";
|
|
|
|
if(self.jid2stats[jid].ssrc2Loss[ssrc].isDownloadStream)
|
|
|
|
type = "download";
|
2014-10-29 10:49:57 +00:00
|
|
|
totalPackets[type] +=
|
|
|
|
self.jid2stats[jid].ssrc2Loss[ssrc].packetsTotal;
|
|
|
|
lostPackets[type] +=
|
|
|
|
self.jid2stats[jid].ssrc2Loss[ssrc].packetsLost;
|
2014-10-16 15:11:26 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
Object.keys(self.jid2stats[jid].ssrc2bitrate).forEach(
|
|
|
|
function (ssrc) {
|
2014-10-29 10:49:57 +00:00
|
|
|
bitrateDownload +=
|
|
|
|
self.jid2stats[jid].ssrc2bitrate[ssrc].download;
|
|
|
|
bitrateUpload +=
|
|
|
|
self.jid2stats[jid].ssrc2bitrate[ssrc].upload;
|
2014-12-01 10:58:03 +00:00
|
|
|
|
|
|
|
delete self.jid2stats[jid].ssrc2bitrate[ssrc];
|
2014-06-05 11:09:31 +00:00
|
|
|
}
|
|
|
|
);
|
2014-10-29 10:49:57 +00:00
|
|
|
resolutions[jid] = self.jid2stats[jid].ssrc2resolution;
|
2014-06-05 11:09:31 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
PeerStats.bitrate = {"upload": bitrateUpload, "download": bitrateDownload};
|
|
|
|
|
|
|
|
PeerStats.packetLoss = {
|
|
|
|
total:
|
|
|
|
calculatePacketLoss(lostPackets.download + lostPackets.upload,
|
2014-12-17 16:21:25 +00:00
|
|
|
totalPackets.download + totalPackets.upload),
|
2014-10-16 15:11:26 +00:00
|
|
|
download:
|
|
|
|
calculatePacketLoss(lostPackets.download, totalPackets.download),
|
|
|
|
upload:
|
|
|
|
calculatePacketLoss(lostPackets.upload, totalPackets.upload)
|
|
|
|
};
|
2014-12-17 16:21:25 +00:00
|
|
|
this.eventEmitter.emit("statistics.connectionstats",
|
2014-10-16 15:11:26 +00:00
|
|
|
{
|
|
|
|
"bitrate": PeerStats.bitrate,
|
|
|
|
"packetLoss": PeerStats.packetLoss,
|
|
|
|
"bandwidth": PeerStats.bandwidth,
|
2014-10-29 10:49:57 +00:00
|
|
|
"resolution": resolutions,
|
2014-10-16 15:11:26 +00:00
|
|
|
"transport": PeerStats.transport
|
|
|
|
});
|
|
|
|
PeerStats.transport = [];
|
|
|
|
|
2014-10-29 10:49:57 +00:00
|
|
|
};
|
2014-10-16 15:11:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stats processing logic.
|
|
|
|
*/
|
|
|
|
StatsCollector.prototype.processAudioLevelReport = function ()
|
|
|
|
{
|
|
|
|
if (!this.baselineAudioLevelsReport)
|
2014-06-05 11:09:31 +00:00
|
|
|
{
|
2014-10-16 15:11:26 +00:00
|
|
|
return;
|
2014-06-05 11:09:31 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
for (var idx in this.currentAudioLevelsReport)
|
|
|
|
{
|
|
|
|
var now = this.currentAudioLevelsReport[idx];
|
|
|
|
|
|
|
|
if (now.type != 'ssrc')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var before = this.baselineAudioLevelsReport[idx];
|
|
|
|
if (!before)
|
|
|
|
{
|
2014-11-28 17:47:19 +00:00
|
|
|
console.warn(getStatValue(now, 'ssrc') + ' not enough data');
|
2014-10-16 15:11:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-28 17:47:19 +00:00
|
|
|
var ssrc = getStatValue(now, 'ssrc');
|
2015-01-28 14:35:22 +00:00
|
|
|
var jid = APP.xmpp.getJidFromSSRC(ssrc);
|
2015-01-05 12:04:02 +00:00
|
|
|
if (!jid && (Date.now() - now.timestamp) < 3000)
|
2014-10-16 15:11:26 +00:00
|
|
|
{
|
|
|
|
console.warn("No jid for ssrc: " + ssrc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var jidStats = this.jid2stats[jid];
|
|
|
|
if (!jidStats)
|
|
|
|
{
|
|
|
|
jidStats = new PeerStats();
|
|
|
|
this.jid2stats[jid] = jidStats;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Audio level
|
2014-11-28 17:47:19 +00:00
|
|
|
var audioLevel = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
audioLevel = getStatValue(now, 'audioInputLevel');
|
|
|
|
if (!audioLevel)
|
|
|
|
audioLevel = getStatValue(now, 'audioOutputLevel');
|
|
|
|
}
|
|
|
|
catch(e) {/*not supported*/
|
|
|
|
console.warn("Audio Levels are not available in the statistics.");
|
|
|
|
clearInterval(this.audioLevelsIntervalId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
if (audioLevel)
|
|
|
|
{
|
|
|
|
// TODO: can't find specs about what this value really is,
|
|
|
|
// but it seems to vary between 0 and around 32k.
|
2015-02-25 10:38:04 +00:00
|
|
|
audioLevel = audioLevel / 32767;
|
|
|
|
jidStats.setSsrcAudioLevel(ssrc, audioLevel);
|
|
|
|
if(jid != APP.xmpp.myJid())
|
2014-12-17 16:21:25 +00:00
|
|
|
this.eventEmitter.emit("statistics.audioLevel", jid, audioLevel);
|
2014-10-16 15:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-25 10:38:04 +00:00
|
|
|
};
|