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.

statsEmitter.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // @flow
  2. import _ from 'lodash';
  3. import JitsiMeetJS from '../base/lib-jitsi-meet';
  4. declare var APP: Object;
  5. /**
  6. * Contains all the callbacks to be notified when stats are updated.
  7. *
  8. * {
  9. * userId: Function[]
  10. * }
  11. */
  12. const subscribers = {};
  13. /**
  14. * A singleton that acts as a pub/sub service for connection stat updates.
  15. */
  16. const statsEmitter = {
  17. /**
  18. * Have {@code statsEmitter} subscribe to stat updates from a given
  19. * conference.
  20. *
  21. * @param {JitsiConference} conference - The conference for which
  22. * {@code statsEmitter} should subscribe for stat updates.
  23. * @returns {void}
  24. */
  25. startListeningForStats(conference: Object) {
  26. const { connectionQuality } = JitsiMeetJS.events;
  27. conference.on(connectionQuality.LOCAL_STATS_UPDATED,
  28. stats => this._onStatsUpdated(conference.myUserId(), stats));
  29. conference.on(connectionQuality.REMOTE_STATS_UPDATED,
  30. (id, stats) => this._emitStatsUpdate(id, stats));
  31. },
  32. /**
  33. * Add a subscriber to be notified when stats are updated for a specified
  34. * user id.
  35. *
  36. * @param {string} id - The user id whose stats updates are of interest.
  37. * @param {Function} callback - The function to invoke when stats for the
  38. * user have been updated.
  39. * @returns {void}
  40. */
  41. subscribeToClientStats(id: ?string, callback: Function) {
  42. if (!id) {
  43. return;
  44. }
  45. if (!subscribers[id]) {
  46. subscribers[id] = [];
  47. }
  48. subscribers[id].push(callback);
  49. },
  50. /**
  51. * Remove a subscriber that is listening for stats updates for a specified
  52. * user id.
  53. *
  54. * @param {string} id - The user id whose stats updates are no longer of
  55. * interest.
  56. * @param {Function} callback - The function that is currently subscribed to
  57. * stat updates for the specified user id.
  58. * @returns {void}
  59. */
  60. unsubscribeToClientStats(id: string, callback: Function) {
  61. if (!subscribers[id]) {
  62. return;
  63. }
  64. const filteredSubscribers = subscribers[id].filter(
  65. subscriber => subscriber !== callback);
  66. if (filteredSubscribers.length) {
  67. subscribers[id] = filteredSubscribers;
  68. } else {
  69. delete subscribers[id];
  70. }
  71. },
  72. /**
  73. * Emit a stat update to all those listening for a specific user's
  74. * connection stats.
  75. *
  76. * @param {string} id - The user id the stats are associated with.
  77. * @param {Object} stats - New connection stats for the user.
  78. * @returns {void}
  79. */
  80. _emitStatsUpdate(id: string, stats: Object = {}) {
  81. const callbacks = subscribers[id] || [];
  82. callbacks.forEach(callback => {
  83. callback(stats);
  84. });
  85. },
  86. /**
  87. * Emit a stat update to all those listening for local stat updates. Will
  88. * also update listeners of remote user stats of changes related to their
  89. * stats.
  90. *
  91. * @param {string} currentUserId - The user id for the local user.
  92. * @param {Object} stats - Connection stats for the local user as provided
  93. * by the library.
  94. * @returns {void}
  95. */
  96. _onStatsUpdated(currentUserId: string, stats: Object) {
  97. const allUserFramerates = stats.framerate || {};
  98. const allUserResolutions = stats.resolution || {};
  99. // FIXME resolution and framerate are hashes keyed off of user ids with
  100. // stat values. Receivers of stats expect resolution and framerate to
  101. // be primatives, not hashes, so overwrites the 'lib-jitsi-meet' stats
  102. // objects.
  103. const modifiedLocalStats = Object.assign({}, stats, {
  104. framerate: allUserFramerates[currentUserId],
  105. resolution: allUserResolutions[currentUserId]
  106. });
  107. this._emitStatsUpdate(currentUserId, modifiedLocalStats);
  108. // Get all the unique user ids from the framerate and resolution stats
  109. // and update remote user stats as needed.
  110. const framerateUserIds = Object.keys(allUserFramerates);
  111. const resolutionUserIds = Object.keys(allUserResolutions);
  112. _.union(framerateUserIds, resolutionUserIds)
  113. .filter(id => id !== currentUserId)
  114. .forEach(id => {
  115. const remoteUserStats = {};
  116. const framerate = allUserFramerates[id];
  117. if (framerate) {
  118. remoteUserStats.framerate = framerate;
  119. }
  120. const resolution = allUserResolutions[id];
  121. if (resolution) {
  122. remoteUserStats.resolution = resolution;
  123. }
  124. this._emitStatsUpdate(id, remoteUserStats);
  125. });
  126. }
  127. };
  128. export default statsEmitter;