Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SpeakerStats.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * A model for keeping track of each user's total
  3. * time as a dominant speaker. The model also
  4. * keeps track of the user's last known name
  5. * in case the user has left the meeting,
  6. * which is also tracked.
  7. */
  8. class SpeakerStats {
  9. /**
  10. * Initializes a new SpeakerStats instance.
  11. *
  12. * @constructor
  13. * @param {string} userId - The id of the user being tracked.
  14. * @param {string} displayName - The name of the user being tracked.
  15. * @param {boolean} isLocalStats - True if the stats model tracks
  16. * the local user.
  17. * @returns {void}
  18. */
  19. constructor(userId, displayName, isLocalStats) {
  20. this._userId = userId;
  21. this.setDisplayName(displayName);
  22. this._isLocalStats = isLocalStats || false;
  23. this.setDominantSpeaker(false);
  24. this.totalDominantSpeakerTime = 0;
  25. this._dominantSpeakerStart = 0;
  26. this._isDominantSpeaker = false;
  27. this._isSilent = false;
  28. this._hasLeft = false;
  29. this._faceLandmarks = [];
  30. }
  31. /**
  32. * Get the user id being tracked.
  33. *
  34. * @returns {string} The user id.
  35. */
  36. getUserId() {
  37. return this._userId;
  38. }
  39. /**
  40. * Get the name of the user being tracked.
  41. *
  42. * @returns {string} The user name.
  43. */
  44. getDisplayName() {
  45. return this.displayName;
  46. }
  47. /**
  48. * Updates the last known name of the user being tracked.
  49. *
  50. * @param {string} - The user name.
  51. * @returns {void}
  52. */
  53. setDisplayName(newName) {
  54. this.displayName = newName;
  55. }
  56. /**
  57. * Returns true if the stats are tracking the local user.
  58. *
  59. * @returns {boolean}
  60. */
  61. isLocalStats() {
  62. return this._isLocalStats;
  63. }
  64. /**
  65. * Returns true if the tracked user is currently a dominant speaker.
  66. *
  67. * @returns {boolean}
  68. */
  69. isDominantSpeaker() {
  70. return this._isDominantSpeaker;
  71. }
  72. /**
  73. * Returns true if the tracked user is currently a dominant speaker.
  74. *
  75. * @param {boolean} isNowDominantSpeaker - If true, the user will be accumulating time
  76. * as dominant speaker. If false, the user will not accumulate time
  77. * and will record any time accumulated since starting as dominant speaker.
  78. * @param {boolean} silence - Indecates whether the dominant speaker is silent or not.
  79. * @returns {void}
  80. */
  81. setDominantSpeaker(isNowDominantSpeaker, silence) {
  82. if (!this.isDominantSpeaker() && isNowDominantSpeaker && !silence) {
  83. this._dominantSpeakerStart = Date.now();
  84. } else if (this.isDominantSpeaker()) {
  85. if (!isNowDominantSpeaker) {
  86. if (!this._isSilent) {
  87. const now = Date.now();
  88. const timeElapsed = now - this._dominantSpeakerStart;
  89. this.totalDominantSpeakerTime += timeElapsed;
  90. this._dominantSpeakerStart = 0;
  91. }
  92. } else if (this._isSilent && !silence) {
  93. this._dominantSpeakerStart = Date.now();
  94. } else if (!this._isSilent && silence) {
  95. const now = Date.now();
  96. const timeElapsed = now - this._dominantSpeakerStart;
  97. this.totalDominantSpeakerTime += timeElapsed;
  98. this._dominantSpeakerStart = 0;
  99. }
  100. }
  101. this._isDominantSpeaker = isNowDominantSpeaker;
  102. this._isSilent = silence;
  103. }
  104. /**
  105. * Get how long the tracked user has been dominant speaker.
  106. *
  107. * @returns {number} - The speaker time in milliseconds.
  108. */
  109. getTotalDominantSpeakerTime() {
  110. let total = this.totalDominantSpeakerTime;
  111. if (this.isDominantSpeaker() && !this._isSilent) {
  112. total += Date.now() - this._dominantSpeakerStart;
  113. }
  114. return total;
  115. }
  116. /**
  117. * Get whether or not the user is still in the meeting.
  118. *
  119. * @returns {boolean} True if the user is no longer in the meeting.
  120. */
  121. hasLeft() {
  122. return this._hasLeft;
  123. }
  124. /**
  125. * Set the user as having left the meeting.
  126. *
  127. * @returns {void}
  128. */
  129. markAsHasLeft() {
  130. this._hasLeft = true;
  131. this.setDominantSpeaker(false);
  132. }
  133. /**
  134. * Gets the face landmarks of the user.
  135. *
  136. * @returns {Object}
  137. */
  138. getFaceLandmarks() {
  139. return this._faceLandmarks;
  140. }
  141. /**
  142. * Sets the face landmarks of the user.
  143. *
  144. * @param {Object} faceLandmarks - object with face expressions.
  145. * @returns {void}
  146. */
  147. setFaceLandmarks(faceLandmarks) {
  148. this._faceLandmarks = faceLandmarks;
  149. }
  150. /**
  151. * Adds new face landmarks to speaker stats.
  152. *
  153. * @param {string} faceExpression
  154. * @param {number} duration
  155. */
  156. addFaceLandmarks(faceLandmarks) {
  157. this._faceLandmarks.push(faceLandmarks);
  158. }
  159. }
  160. module.exports = SpeakerStats;