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

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