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 2.7KB

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