Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

connectionquality.js 4.1KB

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