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.

SpeakerStatsCollector.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  2. import SpeakerStats from './SpeakerStats';
  3. /**
  4. * A collection for tracking speaker stats. Attaches listeners
  5. * to the conference to automatically update on tracked events.
  6. */
  7. export default class SpeakerStatsCollector {
  8. /**
  9. * Initializes a new SpeakerStatsCollector instance.
  10. *
  11. * @constructor
  12. * @param {JitsiConference} conference - The conference to track.
  13. * @returns {void}
  14. */
  15. constructor(conference) {
  16. this.stats = {
  17. users: {
  18. // userId: SpeakerStats
  19. },
  20. dominantSpeakerId: null
  21. };
  22. const userId = conference.myUserId();
  23. this.stats.users[userId] = new SpeakerStats(userId, null, true);
  24. conference.addEventListener(
  25. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  26. this._onDominantSpeaker.bind(this));
  27. conference.addEventListener(
  28. JitsiConferenceEvents.USER_JOINED,
  29. this._onUserJoin.bind(this));
  30. conference.addEventListener(
  31. JitsiConferenceEvents.USER_LEFT,
  32. this._onUserLeave.bind(this));
  33. conference.addEventListener(
  34. JitsiConferenceEvents.DISPLAY_NAME_CHANGED,
  35. this._onDisplayNameChange.bind(this));
  36. }
  37. /**
  38. * Reacts to dominant speaker change events by changing its speaker stats
  39. * models to reflect the current dominant speaker.
  40. *
  41. * @param {string} dominantSpeakerId - The user id of the new
  42. * dominant speaker.
  43. * @returns {void}
  44. * @private
  45. */
  46. _onDominantSpeaker(dominantSpeakerId) {
  47. const oldDominantSpeaker
  48. = this.stats.users[this.stats.dominantSpeakerId];
  49. const newDominantSpaker = this.stats.users[dominantSpeakerId];
  50. oldDominantSpeaker && oldDominantSpeaker.setIsDominantSpeaker(false);
  51. newDominantSpaker && newDominantSpaker.setIsDominantSpeaker(true);
  52. this.stats.dominantSpeakerId = dominantSpeakerId;
  53. }
  54. /**
  55. * Reacts to user join events by creating a new SpeakerStats model.
  56. *
  57. * @param {string} userId - The user id of the new user.
  58. * @param {JitsiParticipant} - The JitsiParticipant model for the new user.
  59. * @returns {void}
  60. * @private
  61. */
  62. _onUserJoin(userId, participant) {
  63. let savedUser = this.stats.users[userId];
  64. if (!savedUser) {
  65. savedUser = this.stats.users[userId]
  66. = new SpeakerStats(userId, participant.getDisplayName());
  67. }
  68. }
  69. /**
  70. * Reacts to user leave events by updating the associated user's
  71. * SpeakerStats model.
  72. *
  73. * @param {string} userId - The user id of the user that left.
  74. * @returns {void}
  75. * @private
  76. */
  77. _onUserLeave(userId) {
  78. const savedUser = this.stats.users[userId];
  79. if (savedUser) {
  80. savedUser.markAsHasLeft();
  81. }
  82. }
  83. /**
  84. * Reacts to user name change events by updating the last known name
  85. * tracked in the associated SpeakerStats model.
  86. *
  87. * @param {string} userId - The user id of the user that left.
  88. * @returns {void}
  89. * @private
  90. */
  91. _onDisplayNameChange(userId, newName) {
  92. const savedUser = this.stats.users[userId];
  93. if (savedUser) {
  94. savedUser.setDisplayName(newName);
  95. }
  96. }
  97. /**
  98. * Return a copy of the tracked SpeakerStats models.
  99. *
  100. * @returns {Object} The keys are the user ids and the values are the
  101. * associated user's SpeakerStats model.
  102. * @private
  103. */
  104. getStats() {
  105. return this.stats.users;
  106. }
  107. }