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

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