2015-07-28 18:22:11 +00:00
|
|
|
/* global APP, require */
|
2015-09-11 02:42:15 +00:00
|
|
|
/* jshint -W101 */
|
2015-01-24 14:28:02 +00:00
|
|
|
var EventEmitter = require("events");
|
|
|
|
var eventEmitter = new EventEmitter();
|
2015-01-28 14:35:22 +00:00
|
|
|
var CQEvents = require("../../service/connectionquality/CQEvents");
|
|
|
|
var XMPPEvents = require("../../service/xmpp/XMPPEvents");
|
2015-09-15 20:34:16 +00:00
|
|
|
var StatisticsEvents = require("../../service/statistics/Events");
|
2015-01-24 14:28:02 +00:00
|
|
|
|
2015-01-08 12:11:53 +00:00
|
|
|
/**
|
|
|
|
* local stats
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
var stats = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remote stats
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
var remoteStats = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interval for sending statistics to other participants
|
|
|
|
* @type {null}
|
|
|
|
*/
|
|
|
|
var sendIntervalId = null;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start statistics sending.
|
|
|
|
*/
|
|
|
|
function startSendingStats() {
|
|
|
|
sendStats();
|
|
|
|
sendIntervalId = setInterval(sendStats, 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends statistics to other participants
|
|
|
|
*/
|
|
|
|
function sendStats() {
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.xmpp.addToPresence("connectionQuality", convertToMUCStats(stats));
|
2015-01-08 12:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts statistics to format for sending through XMPP
|
|
|
|
* @param stats the statistics
|
|
|
|
* @returns {{bitrate_donwload: *, bitrate_uplpoad: *, packetLoss_total: *, packetLoss_download: *, packetLoss_upload: *}}
|
|
|
|
*/
|
|
|
|
function convertToMUCStats(stats) {
|
|
|
|
return {
|
|
|
|
"bitrate_download": stats.bitrate.download,
|
|
|
|
"bitrate_upload": stats.bitrate.upload,
|
|
|
|
"packetLoss_total": stats.packetLoss.total,
|
|
|
|
"packetLoss_download": stats.packetLoss.download,
|
|
|
|
"packetLoss_upload": stats.packetLoss.upload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-28 18:22:11 +00:00
|
|
|
* Converts statistics to format used by VideoLayout
|
2015-01-08 12:11:53 +00:00
|
|
|
* @param stats
|
|
|
|
* @returns {{bitrate: {download: *, upload: *}, packetLoss: {total: *, download: *, upload: *}}}
|
|
|
|
*/
|
|
|
|
function parseMUCStats(stats) {
|
|
|
|
return {
|
|
|
|
bitrate: {
|
|
|
|
download: stats.bitrate_download,
|
|
|
|
upload: stats.bitrate_upload
|
|
|
|
},
|
|
|
|
packetLoss: {
|
|
|
|
total: stats.packetLoss_total,
|
|
|
|
download: stats.packetLoss_download,
|
|
|
|
upload: stats.packetLoss_upload
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var ConnectionQuality = {
|
2015-01-24 14:28:02 +00:00
|
|
|
init: function () {
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.xmpp.addListener(XMPPEvents.REMOTE_STATS, this.updateRemoteStats);
|
2015-09-15 20:34:16 +00:00
|
|
|
APP.statistics.addListener(StatisticsEvents.CONNECTION_STATS,
|
|
|
|
this.updateLocalStats);
|
|
|
|
APP.statistics.addListener(StatisticsEvents.STOP,
|
|
|
|
this.stopSendingStats);
|
2015-01-24 14:28:02 +00:00
|
|
|
},
|
|
|
|
|
2015-01-08 12:11:53 +00:00
|
|
|
/**
|
|
|
|
* Updates the local statistics
|
|
|
|
* @param data new statistics
|
|
|
|
*/
|
|
|
|
updateLocalStats: function (data) {
|
|
|
|
stats = data;
|
2015-01-24 14:28:02 +00:00
|
|
|
eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, 100 - stats.packetLoss.total, stats);
|
2015-09-11 03:26:29 +00:00
|
|
|
if (!sendIntervalId) {
|
2015-01-08 12:11:53 +00:00
|
|
|
startSendingStats();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates remote statistics
|
|
|
|
* @param jid the jid associated with the statistics
|
|
|
|
* @param data the statistics
|
|
|
|
*/
|
|
|
|
updateRemoteStats: function (jid, data) {
|
2015-09-11 03:26:29 +00:00
|
|
|
if (!data || !data.packetLoss_total) {
|
2015-01-24 14:28:02 +00:00
|
|
|
eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, jid, null, null);
|
2015-01-08 12:11:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
remoteStats[jid] = parseMUCStats(data);
|
|
|
|
|
2015-01-24 14:28:02 +00:00
|
|
|
eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED,
|
|
|
|
jid, 100 - data.packetLoss_total, remoteStats[jid]);
|
2015-01-08 12:11:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops statistics sending.
|
|
|
|
*/
|
|
|
|
stopSendingStats: function () {
|
|
|
|
clearInterval(sendIntervalId);
|
|
|
|
sendIntervalId = null;
|
|
|
|
//notify UI about stopping statistics gathering
|
2015-01-24 14:28:02 +00:00
|
|
|
eventEmitter.emit(CQEvents.STOP);
|
2015-01-08 12:11:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the local statistics.
|
|
|
|
*/
|
|
|
|
getStats: function () {
|
|
|
|
return stats;
|
2015-01-24 14:28:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
addListener: function (type, listener) {
|
|
|
|
eventEmitter.on(type, listener);
|
2015-01-08 12:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ConnectionQuality;
|