modified lib-jitsi-meet dev repo
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SpeakerStatsCollector.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  2. import SpeakerStats from './SpeakerStats';
  3. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  4. /**
  5. * A collection for tracking speaker stats. Attaches listeners
  6. * to the conference to automatically update on tracked events.
  7. */
  8. export default class SpeakerStatsCollector {
  9. /**
  10. * Initializes a new SpeakerStatsCollector instance.
  11. *
  12. * @constructor
  13. * @param {JitsiConference} conference - The conference to track.
  14. * @returns {void}
  15. */
  16. constructor(conference) {
  17. this.stats = {
  18. users: {
  19. // userId: SpeakerStats
  20. },
  21. dominantSpeakerId: null
  22. };
  23. const userId = conference.myUserId();
  24. this.stats.users[userId] = new SpeakerStats(userId, null, true);
  25. conference.addEventListener(
  26. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  27. this._onDominantSpeaker.bind(this));
  28. conference.addEventListener(
  29. JitsiConferenceEvents.USER_JOINED,
  30. this._onUserJoin.bind(this));
  31. conference.addEventListener(
  32. JitsiConferenceEvents.USER_LEFT,
  33. this._onUserLeave.bind(this));
  34. conference.addEventListener(
  35. JitsiConferenceEvents.DISPLAY_NAME_CHANGED,
  36. this._onDisplayNameChange.bind(this));
  37. if (conference.xmpp) {
  38. conference.xmpp.addListener(
  39. XMPPEvents.SPEAKER_STATS_RECEIVED,
  40. this._updateStats.bind(this));
  41. }
  42. }
  43. /**
  44. * Reacts to dominant speaker change events by changing its speaker stats
  45. * models to reflect the current dominant speaker.
  46. *
  47. * @param {string} dominantSpeakerId - The user id of the new
  48. * dominant speaker.
  49. * @returns {void}
  50. * @private
  51. */
  52. _onDominantSpeaker(dominantSpeakerId) {
  53. const oldDominantSpeaker
  54. = this.stats.users[this.stats.dominantSpeakerId];
  55. const newDominantSpeaker = this.stats.users[dominantSpeakerId];
  56. oldDominantSpeaker && oldDominantSpeaker.setDominantSpeaker(false);
  57. newDominantSpeaker && newDominantSpeaker.setDominantSpeaker(true);
  58. this.stats.dominantSpeakerId = dominantSpeakerId;
  59. }
  60. /**
  61. * Reacts to user join events by creating a new SpeakerStats model.
  62. *
  63. * @param {string} userId - The user id of the new user.
  64. * @param {JitsiParticipant} - The JitsiParticipant model for the new user.
  65. * @returns {void}
  66. * @private
  67. */
  68. _onUserJoin(userId, participant) {
  69. let savedUser = this.stats.users[userId];
  70. if (!savedUser) {
  71. savedUser = this.stats.users[userId]
  72. = new SpeakerStats(userId, participant.getDisplayName());
  73. }
  74. }
  75. /**
  76. * Reacts to user leave events by updating the associated user's
  77. * SpeakerStats model.
  78. *
  79. * @param {string} userId - The user id of the user that left.
  80. * @returns {void}
  81. * @private
  82. */
  83. _onUserLeave(userId) {
  84. const savedUser = this.stats.users[userId];
  85. if (savedUser) {
  86. savedUser.markAsHasLeft();
  87. }
  88. }
  89. /**
  90. * Reacts to user name change events by updating the last known name
  91. * tracked in the associated SpeakerStats model.
  92. *
  93. * @param {string} userId - The user id of the user that left.
  94. * @returns {void}
  95. * @private
  96. */
  97. _onDisplayNameChange(userId, newName) {
  98. const savedUser = this.stats.users[userId];
  99. if (savedUser) {
  100. savedUser.setDisplayName(newName);
  101. }
  102. }
  103. /**
  104. * Return a copy of the tracked SpeakerStats models.
  105. *
  106. * @returns {Object} The keys are the user ids and the values are the
  107. * associated user's SpeakerStats model.
  108. * @private
  109. */
  110. getStats() {
  111. return this.stats.users;
  112. }
  113. /**
  114. * Updates of the current stats is requested, passing the new values.
  115. *
  116. * @param {Object} newStats - The new values used to update current one.
  117. * @private
  118. */
  119. _updateStats(newStats) {
  120. for (const userId in newStats) { // eslint-disable-line guard-for-in
  121. let speakerStatsToUpdate;
  122. if (this.stats.users[userId]) {
  123. speakerStatsToUpdate = this.stats.users[userId];
  124. if (!speakerStatsToUpdate.getDisplayName()) {
  125. speakerStatsToUpdate
  126. .setDisplayName(newStats[userId].displayName);
  127. }
  128. } else {
  129. speakerStatsToUpdate = new SpeakerStats(
  130. userId, newStats[userId].displayName);
  131. this.stats.users[userId] = speakerStatsToUpdate;
  132. speakerStatsToUpdate.markAsHasLeft();
  133. }
  134. speakerStatsToUpdate.totalDominantSpeakerTime
  135. = newStats[userId].totalDominantSpeakerTime;
  136. }
  137. }
  138. }