You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

connectionquality.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* global APP, require */
  2. /* jshint -W101 */
  3. import EventEmitter from "events";
  4. import CQEvents from "../../service/connectionquality/CQEvents";
  5. const eventEmitter = new EventEmitter();
  6. /**
  7. * local stats
  8. * @type {{}}
  9. */
  10. var stats = {};
  11. /**
  12. * remote stats
  13. * @type {{}}
  14. */
  15. var remoteStats = {};
  16. /**
  17. * Quality percent( 100% - good, 0% - bad.) for the local user.
  18. */
  19. var localConnectionQuality = 100;
  20. /**
  21. * Quality percent( 100% - good, 0% - bad.) stored per id.
  22. */
  23. var remoteConnectionQuality = {};
  24. /**
  25. * Calculates the quality percent based on passed new and old value.
  26. * @param newVal the new value
  27. * @param oldVal the old value
  28. */
  29. function calculateQuality(newVal, oldVal) {
  30. return (newVal <= oldVal) ? newVal : (9*oldVal + newVal) / 10;
  31. }
  32. export default {
  33. /**
  34. * Updates the local statistics
  35. * @param data new statistics
  36. */
  37. updateLocalStats: function (data) {
  38. stats = data;
  39. var newVal = 100 - stats.packetLoss.total;
  40. localConnectionQuality =
  41. calculateQuality(newVal, localConnectionQuality);
  42. eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, localConnectionQuality,
  43. stats);
  44. },
  45. /**
  46. * Updates remote statistics
  47. * @param id the id associated with the statistics
  48. * @param data the statistics
  49. */
  50. updateRemoteStats: function (id, data) {
  51. if (!data || !("packetLoss" in data) || !("total" in data.packetLoss)) {
  52. eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null);
  53. return;
  54. }
  55. remoteStats[id] = data;
  56. var newVal = 100 - data.packetLoss.total;
  57. var oldVal = remoteConnectionQuality[id];
  58. remoteConnectionQuality[id] = calculateQuality(newVal, oldVal || 100);
  59. eventEmitter.emit(
  60. CQEvents.REMOTESTATS_UPDATED, id, remoteConnectionQuality[id],
  61. remoteStats[id]);
  62. },
  63. /**
  64. * Returns the local statistics.
  65. */
  66. getStats: function () {
  67. return stats;
  68. },
  69. addListener: function (type, listener) {
  70. eventEmitter.on(type, listener);
  71. }
  72. };