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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * Converts statistics to format used by VideoLayout
  26. * @param stats
  27. * @returns {{bitrate: {download: *, upload: *}, packetLoss: {total: *, download: *, upload: *}}}
  28. */
  29. function parseMUCStats(stats) {
  30. if(!stats || !stats.children || !stats.children.length)
  31. return null;
  32. var children = stats.children;
  33. var extractedStats = {};
  34. children.forEach((child) => {
  35. if(child.tagName !== "stat" || !child.attributes)
  36. return;
  37. var attrKeys = Object.keys(child.attributes);
  38. if(!attrKeys || !attrKeys.length)
  39. return;
  40. attrKeys.forEach((attr) => {
  41. extractedStats[attr] = child.attributes[attr];
  42. });
  43. });
  44. return {
  45. bitrate: {
  46. download: extractedStats.bitrate_download,
  47. upload: extractedStats.bitrate_upload
  48. },
  49. packetLoss: {
  50. total: extractedStats.packetLoss_total,
  51. download: extractedStats.packetLoss_download,
  52. upload: extractedStats.packetLoss_upload
  53. }
  54. };
  55. }
  56. /**
  57. * Calculates the quality percent based on passed new and old value.
  58. * @param newVal the new value
  59. * @param oldVal the old value
  60. */
  61. function calculateQuality(newVal, oldVal) {
  62. return (newVal <= oldVal) ? newVal : (9*oldVal + newVal) / 10;
  63. }
  64. export default {
  65. /**
  66. * Updates the local statistics
  67. * @param data new statistics
  68. */
  69. updateLocalStats: function (data) {
  70. stats = data;
  71. var newVal = 100 - stats.packetLoss.total;
  72. localConnectionQuality =
  73. calculateQuality(newVal, localConnectionQuality);
  74. eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, localConnectionQuality,
  75. stats);
  76. },
  77. /**
  78. * Updates remote statistics
  79. * @param id the id associated with the statistics
  80. * @param data the statistics
  81. */
  82. updateRemoteStats: function (id, data) {
  83. data = parseMUCStats(data);
  84. if (!data || !data.packetLoss || !data.packetLoss.total) {
  85. eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null);
  86. return;
  87. }
  88. remoteStats[id] = data;
  89. var newVal = 100 - data.packetLoss.total;
  90. var oldVal = remoteConnectionQuality[id];
  91. remoteConnectionQuality[id] = calculateQuality(newVal, oldVal || 100);
  92. eventEmitter.emit(
  93. CQEvents.REMOTESTATS_UPDATED, id, remoteConnectionQuality[id],
  94. remoteStats[id]);
  95. },
  96. /**
  97. * Returns the local statistics.
  98. */
  99. getStats: function () {
  100. return stats;
  101. },
  102. addListener: function (type, listener) {
  103. eventEmitter.on(type, listener);
  104. },
  105. /**
  106. * Converts statistics to format for sending through XMPP
  107. * @param stats the statistics
  108. * @returns [{tagName: "stat", attributes: {{bitrate_donwload: *}},
  109. * {tagName: "stat", attributes: {{ bitrate_uplpoad: *}},
  110. * {tagName: "stat", attributes: {{ packetLoss_total: *}},
  111. * {tagName: "stat", attributes: {{ packetLoss_download: *}},
  112. * {tagName: "stat", attributes: {{ packetLoss_upload: *}}]
  113. */
  114. convertToMUCStats: function (stats) {
  115. return [
  116. {tagName: "stat", attributes: {"bitrate_download": stats.bitrate.download}},
  117. {tagName: "stat", attributes: {"bitrate_upload": stats.bitrate.upload}},
  118. {tagName: "stat", attributes: {"packetLoss_total": stats.packetLoss.total}},
  119. {tagName: "stat", attributes: {"packetLoss_download": stats.packetLoss.download}},
  120. {tagName: "stat", attributes: {"packetLoss_upload": stats.packetLoss.upload}}
  121. ];
  122. }
  123. };