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.7KB

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