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

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