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.

SpeakerStats.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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._hasLeft = false;
  27. this._facialExpressions = {
  28. happy: 0,
  29. neutral: 0,
  30. surprised: 0,
  31. angry: 0,
  32. fearful: 0,
  33. disgusted: 0,
  34. sad: 0
  35. };
  36. }
  37. /**
  38. * Get the user id being tracked.
  39. *
  40. * @returns {string} The user id.
  41. */
  42. getUserId() {
  43. return this._userId;
  44. }
  45. /**
  46. * Get the name of the user being tracked.
  47. *
  48. * @returns {string} The user name.
  49. */
  50. getDisplayName() {
  51. return this.displayName;
  52. }
  53. /**
  54. * Updates the last known name of the user being tracked.
  55. *
  56. * @param {string} - The user name.
  57. * @returns {void}
  58. */
  59. setDisplayName(newName) {
  60. this.displayName = newName;
  61. }
  62. /**
  63. * Returns true if the stats are tracking the local user.
  64. *
  65. * @returns {boolean}
  66. */
  67. isLocalStats() {
  68. return this._isLocalStats;
  69. }
  70. /**
  71. * Returns true if the tracked user is currently a dominant speaker.
  72. *
  73. * @returns {boolean}
  74. */
  75. isDominantSpeaker() {
  76. return this._dominantSpeakerStart > 0;
  77. }
  78. /**
  79. * Returns true if the tracked user is currently a dominant speaker.
  80. *
  81. * @param {boolean} - If true, the user will being accumulating time
  82. * as dominant speaker. If false, the user will not accumulate time
  83. * and will record any time accumulated since starting as dominant speaker.
  84. * @returns {void}
  85. */
  86. setDominantSpeaker(isNowDominantSpeaker) {
  87. if (!this.isDominantSpeaker() && isNowDominantSpeaker) {
  88. this._dominantSpeakerStart = Date.now();
  89. } else if (this.isDominantSpeaker() && !isNowDominantSpeaker) {
  90. const now = Date.now();
  91. const timeElapsed = now - this._dominantSpeakerStart;
  92. this.totalDominantSpeakerTime += timeElapsed;
  93. this._dominantSpeakerStart = 0;
  94. }
  95. }
  96. /**
  97. * Get how long the tracked user has been dominant speaker.
  98. *
  99. * @returns {number} - The speaker time in milliseconds.
  100. */
  101. getTotalDominantSpeakerTime() {
  102. let total = this.totalDominantSpeakerTime;
  103. if (this.isDominantSpeaker()) {
  104. total += Date.now() - this._dominantSpeakerStart;
  105. }
  106. return total;
  107. }
  108. /**
  109. * Get whether or not the user is still in the meeting.
  110. *
  111. * @returns {boolean} True if the user is no longer in the meeting.
  112. */
  113. hasLeft() {
  114. return this._hasLeft;
  115. }
  116. /**
  117. * Set the user as having left the meeting.
  118. *
  119. * @returns {void}
  120. */
  121. markAsHasLeft() {
  122. this._hasLeft = true;
  123. this.setDominantSpeaker(false);
  124. }
  125. /**
  126. * Gets the facial expressions of the user.
  127. *
  128. * @returns {Object}
  129. */
  130. getFacialExpressions() {
  131. return this._facialExpressions;
  132. }
  133. /**
  134. * Sets the facial expressions of the user.
  135. *
  136. * @param {Object} facialExpressions - object with facial expressions.
  137. * @returns {void}
  138. */
  139. setFacialExpressions(facialExpressions) {
  140. this._facialExpressions = facialExpressions;
  141. }
  142. /**
  143. * Adds a new facial expression to speaker stats.
  144. *
  145. * @param {string} facialExpression
  146. * @param {number} duration
  147. */
  148. addFacialExpression(facialExpression, duration) {
  149. this._facialExpressions[facialExpression] += duration;
  150. }
  151. }
  152. module.exports = SpeakerStats;