Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TalkMutedDetection.js 4.4KB

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