您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

NoAudioSignalDetection.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import EventEmitter from 'events';
  2. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  3. import * as JitsiTrackEvents from '../../JitsiTrackEvents';
  4. import * as DetectionEvents from './DetectionEvents';
  5. // We wait a certain time interval for constant silence input from the current device to account for
  6. // potential abnormalities and for a better use experience i.e. don't generate event the instant
  7. // an audio track is added to the tcr.
  8. // Potential improvement - add this as a configurable parameter.
  9. const SILENCE_PERIOD_MS = 4000;
  10. /**
  11. * Detect if there is no audio input on the current TraceAblePeerConnection selected track. The no audio
  12. * state must be constant for a configured amount of time in order for the event to be triggered.
  13. * @fires DetectionEvents.AUDIO_INPUT_STATE_CHANGE
  14. * @fires DetectionEvents.NO_AUDIO_INPUT
  15. */
  16. export default class NoAudioSignalDetection extends EventEmitter {
  17. /**
  18. * Creates new NoAudioSignalDetection.
  19. *
  20. * @param conference the JitsiConference instance that created us.
  21. * @constructor
  22. */
  23. constructor(conference) {
  24. super();
  25. this._conference = conference;
  26. this._timeoutTrigger = null;
  27. this._hasAudioInput = null;
  28. conference.on(JitsiConferenceEvents.TRACK_ADDED, this._trackAdded.bind(this));
  29. }
  30. /**
  31. * Clear the timeout state.
  32. */
  33. _clearTriggerTimeout() {
  34. clearTimeout(this._timeoutTrigger);
  35. this._timeoutTrigger = null;
  36. }
  37. /**
  38. * Generated event triggered by a change in the current conference audio input state.
  39. *
  40. * @param {*} audioLevel - The audio level of the ssrc.
  41. * @fires DetectionEvents.AUDIO_INPUT_STATE_CHANGE
  42. */
  43. _handleAudioInputStateChange(audioLevel) {
  44. // Current audio input state of the active local track in the conference, true for audio input false for no
  45. // audio input.
  46. const status = audioLevel !== 0;
  47. // If this is the first audio event picked up or the current status is different from the previous trigger
  48. // the event.
  49. if (this._hasAudioInput === null || this._hasAudioInput !== status) {
  50. this._hasAudioInput = status;
  51. this.emit(DetectionEvents.AUDIO_INPUT_STATE_CHANGE, this._hasAudioInput);
  52. }
  53. }
  54. /**
  55. * Generate event triggered by a prolonged period of no audio input.
  56. *
  57. * @param {number} audioLevel - The audio level of the ssrc.
  58. * @fires DetectionEvents.NO_AUDIO_INPUT
  59. */
  60. _handleNoAudioInputDetection(audioLevel) {
  61. if (this._eventFired) {
  62. return;
  63. }
  64. if (audioLevel === 0 && !this._timeoutTrigger) {
  65. this._timeoutTrigger = setTimeout(() => {
  66. this._eventFired = true;
  67. this.emit(DetectionEvents.NO_AUDIO_INPUT);
  68. }, SILENCE_PERIOD_MS);
  69. } else if (audioLevel !== 0 && this._timeoutTrigger) {
  70. this._clearTriggerTimeout();
  71. }
  72. }
  73. /**
  74. * Receives audio level events for all send and receive streams on the current TraceablePeerConnection.
  75. *
  76. * @param {TraceablePeerConnection} tpc - TraceablePeerConnection of the owning conference.
  77. * @param {number} ssrc - The synchronization source identifier (SSRC) of the endpoint/participant/stream
  78. * being reported.
  79. * @param {number} audioLevel - The audio level of the ssrc.
  80. * @param {boolean} isLocal - true for local/send streams or false for remote/receive streams.
  81. */
  82. _audioLevel(tpc, ssrc, audioLevel, isLocal) {
  83. // We are interested in the local audio streams
  84. if (!isLocal || !this._audioTrack) {
  85. return;
  86. }
  87. // Get currently active local tracks from the TraceablePeerConnection
  88. const localSSRCs = tpc.localSSRCs.get(this._audioTrack.rtcId);
  89. // Only target the current active track in the tpc. For some reason audio levels for previous
  90. // devices are also picked up from the PeerConnection so we filter them out.
  91. if (!localSSRCs || !localSSRCs.ssrcs.includes(ssrc)) {
  92. return;
  93. }
  94. // First handle audio input state change. In case the state changed to no input the no audio input event
  95. // can try to fire again.
  96. this._handleAudioInputStateChange(audioLevel);
  97. this._handleNoAudioInputDetection(audioLevel);
  98. }
  99. /**
  100. * Notifies NoAudioSignalDetection that a JitsiTrack was added to the associated JitsiConference.
  101. * Only take into account local audio tracks.
  102. *
  103. * @param {JitsiTrack} track - The added JitsiTrack.
  104. */
  105. _trackAdded(track) {
  106. if (track.isLocalAudioTrack()) {
  107. // Reset state for the new track.
  108. this._audioTrack = track;
  109. this._eventFired = false;
  110. this._clearTriggerTimeout();
  111. // Listen for the audio levels on the newly added audio track
  112. track.on(
  113. JitsiTrackEvents.NO_AUDIO_INPUT,
  114. audioLevel => {
  115. this._handleNoAudioInputDetection(audioLevel);
  116. }
  117. );
  118. track.on(
  119. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  120. audioLevel => {
  121. this._handleNoAudioInputDetection(audioLevel);
  122. this._handleAudioInputStateChange(audioLevel);
  123. }
  124. );
  125. }
  126. }
  127. }