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

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