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.

connection_quality.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. connection.emuc.addConnectionInfoToPresence(convertToMUCStats(stats));
  28. connection.emuc.sendPresence();
  29. }
  30. /**
  31. * Converts statistics to format for sending through XMPP
  32. * @param stats the statistics
  33. * @returns {{bitrate_donwload: *, bitrate_uplpoad: *, packetLoss_total: *, packetLoss_download: *, packetLoss_upload: *}}
  34. */
  35. function convertToMUCStats(stats) {
  36. return {
  37. "bitrate_download": stats.bitrate.download,
  38. "bitrate_upload": stats.bitrate.upload,
  39. "packetLoss_total": stats.packetLoss.total,
  40. "packetLoss_download": stats.packetLoss.download,
  41. "packetLoss_upload": stats.packetLoss.upload
  42. };
  43. }
  44. /**
  45. * Converts statitistics to format used by VideoLayout
  46. * @param stats
  47. * @returns {{bitrate: {download: *, upload: *}, packetLoss: {total: *, download: *, upload: *}}}
  48. */
  49. function parseMUCStats(stats) {
  50. return {
  51. bitrate: {
  52. download: stats.bitrate_download,
  53. upload: stats.bitrate_upload
  54. },
  55. packetLoss: {
  56. total: stats.packetLoss_total,
  57. download: stats.packetLoss_download,
  58. upload: stats.packetLoss_upload
  59. }
  60. };
  61. }
  62. var ConnectionQuality = {
  63. /**
  64. * Updates the local statistics
  65. * @param data new statistics
  66. */
  67. updateLocalStats: function (data) {
  68. stats = data;
  69. UI.updateLocalConnectionStats(100 - stats.packetLoss.total, stats);
  70. if (sendIntervalId == null) {
  71. startSendingStats();
  72. }
  73. },
  74. /**
  75. * Updates remote statistics
  76. * @param jid the jid associated with the statistics
  77. * @param data the statistics
  78. */
  79. updateRemoteStats: function (jid, data) {
  80. if (data == null || data.packetLoss_total == null) {
  81. UI.updateConnectionStats(jid, null, null);
  82. return;
  83. }
  84. remoteStats[jid] = parseMUCStats(data);
  85. UI.updateConnectionStats(jid, 100 - data.packetLoss_total, remoteStats[jid]);
  86. },
  87. /**
  88. * Stops statistics sending.
  89. */
  90. stopSendingStats: function () {
  91. clearInterval(sendIntervalId);
  92. sendIntervalId = null;
  93. //notify UI about stopping statistics gathering
  94. UI.onStatsStop();
  95. },
  96. /**
  97. * Returns the local statistics.
  98. */
  99. getStats: function () {
  100. return stats;
  101. }
  102. };
  103. module.exports = ConnectionQuality;