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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* global APP, require */
  2. /* jshint -W101 */
  3. var EventEmitter = require("events");
  4. var eventEmitter = new EventEmitter();
  5. var CQEvents = require("../../service/connectionquality/CQEvents");
  6. var StatisticsEvents = require("../../service/statistics/Events");
  7. /**
  8. * local stats
  9. * @type {{}}
  10. */
  11. var stats = {};
  12. /**
  13. * remote stats
  14. * @type {{}}
  15. */
  16. var remoteStats = {};
  17. /**
  18. * Converts statistics to format used by VideoLayout
  19. * @param stats
  20. * @returns {{bitrate: {download: *, upload: *}, packetLoss: {total: *, download: *, upload: *}}}
  21. */
  22. function parseMUCStats(stats) {
  23. if(!stats || !stats.children || !stats.children.length)
  24. return null;
  25. var children = stats.children;
  26. var extractedStats = {};
  27. children.forEach((child) => {
  28. if(child.tagName !== "stat" || !child.attributes)
  29. return;
  30. var attrKeys = Object.keys(child.attributes);
  31. if(!attrKeys || !attrKeys.length)
  32. return;
  33. attrKeys.forEach((attr) => {
  34. extractedStats[attr] = child.attributes[attr];
  35. });
  36. });
  37. return {
  38. bitrate: {
  39. download: extractedStats.bitrate_download,
  40. upload: extractedStats.bitrate_upload
  41. },
  42. packetLoss: {
  43. total: extractedStats.packetLoss_total,
  44. download: extractedStats.packetLoss_download,
  45. upload: extractedStats.packetLoss_upload
  46. }
  47. };
  48. }
  49. var ConnectionQuality = {
  50. init: function () {
  51. APP.statistics.addListener(
  52. StatisticsEvents.CONNECTION_STATS, this.updateLocalStats
  53. );
  54. APP.statistics.addListener(
  55. StatisticsEvents.STOP, this.stopSendingStats
  56. );
  57. },
  58. /**
  59. * Updates the local statistics
  60. * @param data new statistics
  61. */
  62. updateLocalStats: function (data) {
  63. stats = data;
  64. eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, 100 - stats.packetLoss.total, stats);
  65. },
  66. /**
  67. * Updates remote statistics
  68. * @param id the id associated with the statistics
  69. * @param data the statistics
  70. */
  71. updateRemoteStats: function (id, data) {
  72. data = parseMUCStats(data);
  73. if (!data || !data.packetLoss || !data.packetLoss.total) {
  74. eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null);
  75. return;
  76. }
  77. remoteStats[id] = data;
  78. eventEmitter.emit(
  79. CQEvents.REMOTESTATS_UPDATED, id, 100 - data.packetLoss_total, remoteStats[id]
  80. );
  81. },
  82. /**
  83. * Stops statistics sending.
  84. */
  85. stopSendingStats: function () {
  86. //notify UI about stopping statistics gathering
  87. eventEmitter.emit(CQEvents.STOP);
  88. },
  89. /**
  90. * Returns the local statistics.
  91. */
  92. getStats: function () {
  93. return stats;
  94. },
  95. addListener: function (type, listener) {
  96. eventEmitter.on(type, listener);
  97. },
  98. /**
  99. * Converts statistics to format for sending through XMPP
  100. * @param stats the statistics
  101. * @returns [{tagName: "stat", attributes: {{bitrate_donwload: *}},
  102. * {tagName: "stat", attributes: {{ bitrate_uplpoad: *}},
  103. * {tagName: "stat", attributes: {{ packetLoss_total: *}},
  104. * {tagName: "stat", attributes: {{ packetLoss_download: *}},
  105. * {tagName: "stat", attributes: {{ packetLoss_upload: *}}]
  106. */
  107. convertToMUCStats: function (stats) {
  108. return [
  109. {tagName: "stat", attributes: {"bitrate_download": stats.bitrate.download}},
  110. {tagName: "stat", attributes: {"bitrate_upload": stats.bitrate.upload}},
  111. {tagName: "stat", attributes: {"packetLoss_total": stats.packetLoss.total}},
  112. {tagName: "stat", attributes: {"packetLoss_download": stats.packetLoss.download}},
  113. {tagName: "stat", attributes: {"packetLoss_upload": stats.packetLoss.upload}}
  114. ];
  115. }
  116. };
  117. module.exports = ConnectionQuality;