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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * Converts statistics to format used by VideoLayout
  18. * @param stats
  19. * @returns {{bitrate: {download: *, upload: *}, packetLoss: {total: *, download: *, upload: *}}}
  20. */
  21. function parseMUCStats(stats) {
  22. if(!stats || !stats.children || !stats.children.length)
  23. return null;
  24. var children = stats.children;
  25. var extractedStats = {};
  26. children.forEach((child) => {
  27. if(child.tagName !== "stat" || !child.attributes)
  28. return;
  29. var attrKeys = Object.keys(child.attributes);
  30. if(!attrKeys || !attrKeys.length)
  31. return;
  32. attrKeys.forEach((attr) => {
  33. extractedStats[attr] = child.attributes[attr];
  34. });
  35. });
  36. return {
  37. bitrate: {
  38. download: extractedStats.bitrate_download,
  39. upload: extractedStats.bitrate_upload
  40. },
  41. packetLoss: {
  42. total: extractedStats.packetLoss_total,
  43. download: extractedStats.packetLoss_download,
  44. upload: extractedStats.packetLoss_upload
  45. }
  46. };
  47. }
  48. export default {
  49. /**
  50. * Updates the local statistics
  51. * @param data new statistics
  52. */
  53. updateLocalStats: function (data) {
  54. stats = data;
  55. eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, 100 - stats.packetLoss.total, stats);
  56. },
  57. /**
  58. * Updates remote statistics
  59. * @param id the id associated with the statistics
  60. * @param data the statistics
  61. */
  62. updateRemoteStats: function (id, data) {
  63. data = parseMUCStats(data);
  64. if (!data || !data.packetLoss || !data.packetLoss.total) {
  65. eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null);
  66. return;
  67. }
  68. remoteStats[id] = data;
  69. eventEmitter.emit(
  70. CQEvents.REMOTESTATS_UPDATED, id, 100 - data.packetLoss_total, remoteStats[id]
  71. );
  72. },
  73. /**
  74. * Returns the local statistics.
  75. */
  76. getStats: function () {
  77. return stats;
  78. },
  79. addListener: function (type, listener) {
  80. eventEmitter.on(type, listener);
  81. },
  82. /**
  83. * Converts statistics to format for sending through XMPP
  84. * @param stats the statistics
  85. * @returns [{tagName: "stat", attributes: {{bitrate_donwload: *}},
  86. * {tagName: "stat", attributes: {{ bitrate_uplpoad: *}},
  87. * {tagName: "stat", attributes: {{ packetLoss_total: *}},
  88. * {tagName: "stat", attributes: {{ packetLoss_download: *}},
  89. * {tagName: "stat", attributes: {{ packetLoss_upload: *}}]
  90. */
  91. convertToMUCStats: function (stats) {
  92. return [
  93. {tagName: "stat", attributes: {"bitrate_download": stats.bitrate.download}},
  94. {tagName: "stat", attributes: {"bitrate_upload": stats.bitrate.upload}},
  95. {tagName: "stat", attributes: {"packetLoss_total": stats.packetLoss.total}},
  96. {tagName: "stat", attributes: {"packetLoss_download": stats.packetLoss.download}},
  97. {tagName: "stat", attributes: {"packetLoss_upload": stats.packetLoss.upload}}
  98. ];
  99. }
  100. };