Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SpeakerStats.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. }
  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._dominantSpeakerStart > 0;
  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. setDominantSpeaker(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;
  83. this.totalDominantSpeakerTime += timeElapsed;
  84. this._dominantSpeakerStart = 0;
  85. }
  86. }
  87. /**
  88. * Get how long the tracked user has been dominant speaker.
  89. *
  90. * @returns {number} - The speaker time in milliseconds.
  91. */
  92. getTotalDominantSpeakerTime() {
  93. let total = this.totalDominantSpeakerTime;
  94. if (this.isDominantSpeaker()) {
  95. total += Date.now() - this._dominantSpeakerStart;
  96. }
  97. return total;
  98. }
  99. /**
  100. * Get whether or not the user is still in the meeting.
  101. *
  102. * @returns {boolean} True if the user is no longer in the meeting.
  103. */
  104. hasLeft() {
  105. return this._hasLeft;
  106. }
  107. /**
  108. * Set the user as having left the meeting.
  109. *
  110. * @returns {void}
  111. */
  112. markAsHasLeft() {
  113. this._hasLeft = true;
  114. this.setDominantSpeaker(false);
  115. }
  116. }
  117. module.exports = SpeakerStats;