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

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