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.

TalkMutedDetection.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import * as JitsiConferenceEvents from "../JitsiConferenceEvents";
  2. export default class TalkMutedDetection {
  3. /**
  4. * Creates TalkMutedDetection
  5. * @param conference the JitsiConference instance that created us.
  6. * @param callback the callback to call when detected that the local user is
  7. * talking while her microphone is muted.
  8. * @constructor
  9. */
  10. constructor(conference, callback) {
  11. /**
  12. * The callback to call when detected that the local user is talking
  13. * while her microphone is muted.
  14. *
  15. * @private
  16. */
  17. this._callback = callback;
  18. /**
  19. * The indicator which determines whether <tt>callback</tt> has been
  20. * invoked for the current local audio track of <tt>conference</tt> so
  21. * that it is invoked once only.
  22. *
  23. * @private
  24. */
  25. this._eventFired = false;
  26. // XXX I went back and forth on the subject of where to put the access
  27. // to statistics. On the one had, (1) statistics is likely intended to
  28. // be private to conference and (2) there is a desire to keep the
  29. // dependencies of modules to the minimum (i.e. not have
  30. // TalkMutedDetection depend on statistics). On the other hand, (1)
  31. // statistics is technically not private because
  32. // JitsiConferenceEventManager accesses it and (2) TalkMutedDetection
  33. // works exactly because it knows that there are no audio levels for
  34. // JitsiLocalTrack but there are audio levels for the local participant
  35. // through statistics.
  36. conference.statistics.addAudioLevelListener(
  37. this._audioLevel.bind(this));
  38. conference.on(
  39. JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  40. this._trackMuteChanged.bind(this));
  41. conference.on(
  42. JitsiConferenceEvents.TRACK_ADDED,
  43. this._trackAdded.bind(this));
  44. }
  45. /**
  46. * Receives audio level events for all send and receive streams.
  47. *
  48. * @param ssrc - The synchronization source identifier (SSRC) of the
  49. * endpoint/participant/stream being reported.
  50. * @param {number} audioLevel - The audio level of <tt>ssrc</tt>.
  51. * @param {boolean} isLocal - <tt>true</tt> if <tt>ssrc</tt> represents a
  52. * local/send stream or <tt>false</tt> for a remote/receive stream.
  53. */
  54. _audioLevel(ssrc, audioLevel, isLocal) {
  55. // We are interested in the local audio stream only and if event is not
  56. // sent yet.
  57. if (!isLocal || !this.audioTrack || this._eventFired) {
  58. return;
  59. }
  60. if (this.audioTrack.isMuted() && audioLevel > 0.6) {
  61. this._eventFired = true;
  62. this._callback();
  63. }
  64. }
  65. /**
  66. * Determines whether a specific {@link JitsiTrack} represents a local audio
  67. * track.
  68. *
  69. * @param {JitsiTrack} track - The <tt>JitsiTrack</tt> to be checked whether
  70. * it represents a local audio track.
  71. * @private
  72. * @return {boolean} - <tt>true</tt> if the specified <tt>track</tt>
  73. * represents a local audio track; otherwise, <tt>false</tt>.
  74. */
  75. _isLocalAudioTrack(track) {
  76. return track.isAudioTrack() && track.isLocal();
  77. }
  78. /**
  79. * Notifies this <tt>TalkMutedDetection</tt> that a {@link JitsiTrack} was
  80. * added to the associated {@link JitsiConference}. Looks for the local
  81. * audio track only.
  82. *
  83. * @param {JitsiTrack} track - The added <tt>JitsiTrack</tt>.
  84. * @private
  85. */
  86. _trackAdded(track) {
  87. if (this._isLocalAudioTrack(track)) {
  88. this.audioTrack = track;
  89. }
  90. }
  91. /**
  92. * Notifies this <tt>TalkMutedDetection</tt> that the mute state of a
  93. * {@link JitsiTrack} has changed. Looks for the local audio track only.
  94. *
  95. * @param {JitsiTrack} track - The <tt>JitsiTrack</tt> whose mute state has
  96. * changed.
  97. * @private
  98. */
  99. _trackMuteChanged(track) {
  100. if (this._isLocalAudioTrack(track) && track.isMuted()) {
  101. this._eventFired = false;
  102. }
  103. }
  104. }