jiti-meet/modules/connectionquality/connectionquality.js

103 lines
2.7 KiB
JavaScript
Raw Normal View History

/* global APP, require */
/* 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-08 12:11:53 +00:00
/**
* local stats
* @type {{}}
*/
var stats = {};
/**
* remote stats
* @type {{}}
*/
var remoteStats = {};
/**
* Quality percent( 100% - good, 0% - bad.) for the local user.
*/
var localConnectionQuality = 100;
/**
* Quality percent( 100% - good, 0% - bad.) stored per id.
*/
var remoteConnectionQuality = {};
/**
* Calculates the quality percent based on passed new and old value.
* @param newVal the new value
* @param oldVal the old value
*/
function calculateQuality(newVal, oldVal) {
return (newVal <= oldVal) ? newVal : (9*oldVal + newVal) / 10;
}
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
* @param dontUpdateLocalConnectionQuality {boolean} if true -
* localConnectionQuality wont be recalculated.
2015-01-08 12:11:53 +00:00
*/
updateLocalStats: function (data, dontUpdateLocalConnectionQuality) {
2015-01-08 12:11:53 +00:00
stats = data;
if(!dontUpdateLocalConnectionQuality) {
var newVal = 100 - stats.packetLoss.total;
localConnectionQuality =
calculateQuality(newVal, localConnectionQuality);
}
eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, localConnectionQuality,
stats);
},
/**
* Updates only the localConnectionQuality value
* @param values {int} the new value. should be from 0 - 100.
*/
updateLocalConnectionQuality: function (value) {
localConnectionQuality = value;
eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, localConnectionQuality,
stats);
2015-01-08 12:11:53 +00:00
},
/**
* Updates remote statistics
* @param id the id associated with the statistics
2015-01-08 12:11:53 +00:00
* @param data the statistics
*/
updateRemoteStats: function (id, data) {
if (!data || !("packetLoss" in data) || !("total" in data.packetLoss)) {
eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null);
2015-01-08 12:11:53 +00:00
return;
}
// Use only the fields we need
data = {bitrate: data.bitrate, packetLoss: data.packetLoss};
remoteStats[id] = data;
2015-01-08 12:11:53 +00:00
var newVal = 100 - data.packetLoss.total;
var oldVal = remoteConnectionQuality[id];
remoteConnectionQuality[id] = calculateQuality(newVal, oldVal || 100);
eventEmitter.emit(
CQEvents.REMOTESTATS_UPDATED, id, remoteConnectionQuality[id],
remoteStats[id]);
2015-01-08 12:11:53 +00:00
},
/**
* Returns the local statistics.
*/
getStats: function () {
return stats;
},
addListener: function (type, listener) {
eventEmitter.on(type, listener);
}
2015-01-08 12:11:53 +00:00
};