2015-07-28 18:22:11 +00:00
|
|
|
/* global APP, require */
|
2015-09-11 02:42:15 +00:00
|
|
|
/* jshint -W101 */
|
2016-01-29 15:33:16 +00:00
|
|
|
import EventEmitter from "events";
|
|
|
|
|
|
|
|
import CQEvents from "../../service/connectionquality/CQEvents";
|
|
|
|
|
|
|
|
const eventEmitter = new EventEmitter();
|
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 = {};
|
|
|
|
|
|
|
|
/**
|
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) {
|
2016-01-04 23:16:58 +00:00
|
|
|
if(!stats || !stats.children || !stats.children.length)
|
|
|
|
return null;
|
|
|
|
var children = stats.children;
|
|
|
|
var extractedStats = {};
|
|
|
|
children.forEach((child) => {
|
|
|
|
if(child.tagName !== "stat" || !child.attributes)
|
|
|
|
return;
|
|
|
|
var attrKeys = Object.keys(child.attributes);
|
|
|
|
if(!attrKeys || !attrKeys.length)
|
|
|
|
return;
|
|
|
|
attrKeys.forEach((attr) => {
|
|
|
|
extractedStats[attr] = child.attributes[attr];
|
|
|
|
});
|
|
|
|
});
|
2015-01-08 12:11:53 +00:00
|
|
|
return {
|
|
|
|
bitrate: {
|
2016-01-04 23:16:58 +00:00
|
|
|
download: extractedStats.bitrate_download,
|
|
|
|
upload: extractedStats.bitrate_upload
|
2015-01-08 12:11:53 +00:00
|
|
|
},
|
|
|
|
packetLoss: {
|
2016-01-04 23:16:58 +00:00
|
|
|
total: extractedStats.packetLoss_total,
|
|
|
|
download: extractedStats.packetLoss_download,
|
|
|
|
upload: extractedStats.packetLoss_upload
|
2015-01-08 12:11:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:33:16 +00:00
|
|
|
export default {
|
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-01-08 12:11:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates remote statistics
|
2015-12-02 13:05:29 +00:00
|
|
|
* @param id the id associated with the statistics
|
2015-01-08 12:11:53 +00:00
|
|
|
* @param data the statistics
|
|
|
|
*/
|
2015-12-02 13:05:29 +00:00
|
|
|
updateRemoteStats: function (id, data) {
|
2016-01-04 23:16:58 +00:00
|
|
|
data = parseMUCStats(data);
|
|
|
|
if (!data || !data.packetLoss || !data.packetLoss.total) {
|
2015-12-02 13:05:29 +00:00
|
|
|
eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null);
|
2015-01-08 12:11:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-01-04 23:16:58 +00:00
|
|
|
remoteStats[id] = data;
|
2015-01-08 12:11:53 +00:00
|
|
|
|
2015-12-02 13:05:29 +00:00
|
|
|
eventEmitter.emit(
|
|
|
|
CQEvents.REMOTESTATS_UPDATED, id, 100 - data.packetLoss_total, remoteStats[id]
|
|
|
|
);
|
2015-01-08 12:11:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the local statistics.
|
|
|
|
*/
|
|
|
|
getStats: function () {
|
|
|
|
return stats;
|
2015-01-24 14:28:02 +00:00
|
|
|
},
|
2015-12-02 13:05:29 +00:00
|
|
|
|
2015-01-24 14:28:02 +00:00
|
|
|
addListener: function (type, listener) {
|
|
|
|
eventEmitter.on(type, listener);
|
2015-12-02 13:05:29 +00:00
|
|
|
},
|
2015-01-08 12:11:53 +00:00
|
|
|
|
2015-12-02 13:05:29 +00:00
|
|
|
/**
|
|
|
|
* Converts statistics to format for sending through XMPP
|
|
|
|
* @param stats the statistics
|
2016-01-04 23:16:58 +00:00
|
|
|
* @returns [{tagName: "stat", attributes: {{bitrate_donwload: *}},
|
|
|
|
* {tagName: "stat", attributes: {{ bitrate_uplpoad: *}},
|
|
|
|
* {tagName: "stat", attributes: {{ packetLoss_total: *}},
|
|
|
|
* {tagName: "stat", attributes: {{ packetLoss_download: *}},
|
|
|
|
* {tagName: "stat", attributes: {{ packetLoss_upload: *}}]
|
2015-12-02 13:05:29 +00:00
|
|
|
*/
|
|
|
|
convertToMUCStats: function (stats) {
|
2016-01-04 23:16:58 +00:00
|
|
|
return [
|
|
|
|
{tagName: "stat", attributes: {"bitrate_download": stats.bitrate.download}},
|
|
|
|
{tagName: "stat", attributes: {"bitrate_upload": stats.bitrate.upload}},
|
|
|
|
{tagName: "stat", attributes: {"packetLoss_total": stats.packetLoss.total}},
|
|
|
|
{tagName: "stat", attributes: {"packetLoss_download": stats.packetLoss.download}},
|
|
|
|
{tagName: "stat", attributes: {"packetLoss_upload": stats.packetLoss.upload}}
|
|
|
|
];
|
2015-12-02 13:05:29 +00:00
|
|
|
}
|
2015-01-08 12:11:53 +00:00
|
|
|
};
|