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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.setIsDominantSpeaker(false);
  24. this.totalDominantSpeakerTime = 0;
  25. this._dominantSpeakerStart = null;
  26. this._hasLeft = false;
  27. }
  28. /**
  29. * Get the user id being tracked.
  30. *
  31. * @returns {string} The user id.
  32. */
  33. getUserId() {
  34. return this._userId;
  35. }
  36. /**
  37. * Get the name of the user being tracked.
  38. *
  39. * @returns {string} The user name.
  40. */
  41. getDisplayName() {
  42. return this.displayName;
  43. }
  44. /**
  45. * Updates the last known name of the user being tracked.
  46. *
  47. * @param {string} - The user name.
  48. * @returns {void}
  49. */
  50. setDisplayName(newName) {
  51. this.displayName = newName;
  52. }
  53. /**
  54. * Returns true if the stats are tracking the local user.
  55. *
  56. * @returns {boolean}
  57. */
  58. isLocalStats() {
  59. return this._isLocalStats;
  60. }
  61. /**
  62. * Returns true if the tracked user is currently a dominant speaker.
  63. *
  64. * @returns {boolean}
  65. */
  66. isDominantSpeaker() {
  67. return this._isDominantSpeaker;
  68. }
  69. /**
  70. * Returns true if the tracked user is currently a dominant speaker.
  71. *
  72. * @param {boolean} - If true, the user will being accumulating time
  73. * as dominant speaker. If false, the user will not accumulate time
  74. * and will record any time accumulated since starting as dominant speaker.
  75. * @returns {void}
  76. */
  77. setIsDominantSpeaker(isNowDominantSpeaker) {
  78. if (!this._isDominantSpeaker && isNowDominantSpeaker) {
  79. this._dominantSpeakerStart = Date.now();
  80. } else if (this._isDominantSpeaker && !isNowDominantSpeaker) {
  81. const now = Date.now();
  82. const timeElapsed = now - (this._dominantSpeakerStart || 0);
  83. this.totalDominantSpeakerTime += timeElapsed;
  84. this._dominantSpeakerStart = null;
  85. }
  86. this._isDominantSpeaker = isNowDominantSpeaker;
  87. }
  88. /**
  89. * Get how long the tracked user has been dominant speaker.
  90. *
  91. * @returns {number} - The speaker time in milliseconds.
  92. */
  93. getTotalDominantSpeakerTime() {
  94. let total = this.totalDominantSpeakerTime;
  95. if (this._isDominantSpeaker) {
  96. total += Date.now() - this._dominantSpeakerStart;
  97. }
  98. return total;
  99. }
  100. /**
  101. * Get whether or not the user is still in the meeting.
  102. *
  103. * @returns {boolean} True if the user is no longer in the meeting.
  104. */
  105. hasLeft() {
  106. return this._hasLeft;
  107. }
  108. /**
  109. * Set the user as having left the meeting.
  110. *
  111. * @returns {void}
  112. */
  113. markAsHasLeft() {
  114. this._hasLeft = true;
  115. this.setIsDominantSpeaker(false);
  116. }
  117. }
  118. module.exports = SpeakerStats;