Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

connectionquality.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. var EventEmitter = require("events");
  17. var eventEmitter = new EventEmitter();
  18. var CQEvents = require("../../service/connectionquality/CQEvents");
  19. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  20. /**
  21. * local stats
  22. * @type {{}}
  23. */
  24. var stats = {};
  25. /**
  26. * remote stats
  27. * @type {{}}
  28. */
  29. var remoteStats = {};
  30. /**
  31. * Interval for sending statistics to other participants
  32. * @type {null}
  33. */
  34. var sendIntervalId = null;
  35. /**
  36. * Start statistics sending.
  37. */
  38. function startSendingStats() {
  39. sendStats();
  40. sendIntervalId = setInterval(sendStats, 10000);
  41. }
  42. /**
  43. * Sends statistics to other participants
  44. */
  45. function sendStats() {
  46. APP.xmpp.addToPresence("connectionQuality", convertToMUCStats(stats));
  47. }
  48. /**
  49. * Converts statistics to format for sending through XMPP
  50. * @param stats the statistics
  51. * @returns {{bitrate_donwload: *, bitrate_uplpoad: *, packetLoss_total: *, packetLoss_download: *, packetLoss_upload: *}}
  52. */
  53. function convertToMUCStats(stats) {
  54. return {
  55. "bitrate_download": stats.bitrate.download,
  56. "bitrate_upload": stats.bitrate.upload,
  57. "packetLoss_total": stats.packetLoss.total,
  58. "packetLoss_download": stats.packetLoss.download,
  59. "packetLoss_upload": stats.packetLoss.upload
  60. };
  61. }
  62. /**
  63. * Converts statitistics to format used by VideoLayout
  64. * @param stats
  65. * @returns {{bitrate: {download: *, upload: *}, packetLoss: {total: *, download: *, upload: *}}}
  66. */
  67. function parseMUCStats(stats) {
  68. return {
  69. bitrate: {
  70. download: stats.bitrate_download,
  71. upload: stats.bitrate_upload
  72. },
  73. packetLoss: {
  74. total: stats.packetLoss_total,
  75. download: stats.packetLoss_download,
  76. upload: stats.packetLoss_upload
  77. }
  78. };
  79. }
  80. var ConnectionQuality = {
  81. init: function () {
  82. APP.xmpp.addListener(XMPPEvents.REMOTE_STATS, this.updateRemoteStats);
  83. APP.statistics.addConnectionStatsListener(this.updateLocalStats);
  84. APP.statistics.addRemoteStatsStopListener(this.stopSendingStats);
  85. },
  86. /**
  87. * Updates the local statistics
  88. * @param data new statistics
  89. */
  90. updateLocalStats: function (data) {
  91. stats = data;
  92. eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, 100 - stats.packetLoss.total, stats);
  93. if (sendIntervalId == null) {
  94. startSendingStats();
  95. }
  96. },
  97. /**
  98. * Updates remote statistics
  99. * @param jid the jid associated with the statistics
  100. * @param data the statistics
  101. */
  102. updateRemoteStats: function (jid, data) {
  103. if (data == null || data.packetLoss_total == null) {
  104. eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, jid, null, null);
  105. return;
  106. }
  107. remoteStats[jid] = parseMUCStats(data);
  108. eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED,
  109. jid, 100 - data.packetLoss_total, remoteStats[jid]);
  110. },
  111. /**
  112. * Stops statistics sending.
  113. */
  114. stopSendingStats: function () {
  115. clearInterval(sendIntervalId);
  116. sendIntervalId = null;
  117. //notify UI about stopping statistics gathering
  118. eventEmitter.emit(CQEvents.STOP);
  119. },
  120. /**
  121. * Returns the local statistics.
  122. */
  123. getStats: function () {
  124. return stats;
  125. },
  126. addListener: function (type, listener) {
  127. eventEmitter.on(type, listener);
  128. }
  129. };
  130. module.exports = ConnectionQuality;