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.

NoAudioSignalDetection.ts 6.0KB

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