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

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