Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

connectionquality.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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("stats", 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. tagName: "stats",
  42. attributes: {
  43. xmlns: 'http://jitsi.org/jitmeet/stats'
  44. },
  45. children: [
  46. {
  47. tagName: "stat",
  48. attributes: {name: "bitrate_download", value: stats.bitrate.download}
  49. },
  50. {
  51. tagName: "stat",
  52. attributes: {name: "bitrate_upload", value: stats.bitrate.upload}
  53. },
  54. {
  55. tagName: "stat",
  56. attributes: {name: "packetLoss_total", value: stats.packetLoss.total}
  57. },
  58. {
  59. tagName: "stat",
  60. attributes: {name: "packetLoss_download", value: stats.packetLoss.download}
  61. },
  62. {
  63. tagName: "stat",
  64. attributes: {name: "packetLoss_upload", value: stats.packetLoss.upload}
  65. }
  66. ]
  67. };
  68. }
  69. /**
  70. * Converts statistics to format used by VideoLayout
  71. * @param stats
  72. * @returns {{bitrate: {download: *, upload: *}, packetLoss: {total: *, download: *, upload: *}}}
  73. */
  74. function parseMUCStats(stats) {
  75. return {
  76. bitrate: {
  77. download: stats.bitrate_download,
  78. upload: stats.bitrate_upload
  79. },
  80. packetLoss: {
  81. total: stats.packetLoss_total,
  82. download: stats.packetLoss_download,
  83. upload: stats.packetLoss_upload
  84. }
  85. };
  86. }
  87. var ConnectionQuality = {
  88. init: function () {
  89. APP.xmpp.addListener(XMPPEvents.REMOTE_STATS, this.updateRemoteStats);
  90. APP.statistics.addConnectionStatsListener(this.updateLocalStats);
  91. APP.statistics.addRemoteStatsStopListener(this.stopSendingStats);
  92. },
  93. /**
  94. * Updates the local statistics
  95. * @param data new statistics
  96. */
  97. updateLocalStats: function (data) {
  98. stats = data;
  99. eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, 100 - stats.packetLoss.total, stats);
  100. if (sendIntervalId == null) {
  101. startSendingStats();
  102. }
  103. },
  104. /**
  105. * Updates remote statistics
  106. * @param jid the jid associated with the statistics
  107. * @param data the statistics
  108. */
  109. updateRemoteStats: function (jid, data) {
  110. if (data == null || data.packetLoss_total == null) {
  111. eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, jid, null, null);
  112. return;
  113. }
  114. remoteStats[jid] = parseMUCStats(data);
  115. eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED,
  116. jid, 100 - data.packetLoss_total, remoteStats[jid]);
  117. },
  118. /**
  119. * Stops statistics sending.
  120. */
  121. stopSendingStats: function () {
  122. clearInterval(sendIntervalId);
  123. sendIntervalId = null;
  124. //notify UI about stopping statistics gathering
  125. eventEmitter.emit(CQEvents.STOP);
  126. },
  127. /**
  128. * Returns the local statistics.
  129. */
  130. getStats: function () {
  131. return stats;
  132. },
  133. addListener: function (type, listener) {
  134. eventEmitter.on(type, listener);
  135. }
  136. };
  137. module.exports = ConnectionQuality;