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.

P2PDominantSpeakerDetection.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import * as JitsiConferenceEvents from '../JitsiConferenceEvents';
  2. import RTCEvents from '../service/RTC/RTCEvents';
  3. /**
  4. * The value which we use to say, every sound over this threshold
  5. * is talking on the mic.
  6. * @type {number}
  7. */
  8. const SPEECH_DETECT_THRESHOLD = 0.6;
  9. /**
  10. * The <tt>P2PDominantSpeakerDetection</tt> is activated only when p2p is
  11. * currently used.
  12. * Listens for changes in the audio level changes of the local p2p audio track
  13. * or remote p2p one and fires dominant speaker events to be able to use
  14. * features depending on those events (speaker stats), to make them work without
  15. * the video bridge.
  16. */
  17. export default class P2PDominantSpeakerDetection {
  18. /**
  19. * Creates P2PDominantSpeakerDetection
  20. * @param conference the JitsiConference instance that created us.
  21. * @constructor
  22. */
  23. constructor(conference) {
  24. this.conference = conference;
  25. conference.addEventListener(
  26. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  27. this._audioLevel.bind(this));
  28. this.myUserID = this.conference.myUserId();
  29. }
  30. /**
  31. * Receives audio level events for all streams in the conference.
  32. *
  33. * @param {String} id - The participant id
  34. * @param {number} audioLevel - The audio level.
  35. */
  36. _audioLevel(id, audioLevel) {
  37. // we do not process if p2p is not active
  38. // or audio level is under certain threshold
  39. // or if the audio level is for local audio track which is muted
  40. if (!this.conference.isP2PActive()
  41. || audioLevel <= SPEECH_DETECT_THRESHOLD
  42. || (id === this.myUserID
  43. && this.conference.getLocalAudioTrack().isMuted())) {
  44. return;
  45. }
  46. this.conference.rtc.eventEmitter.emit(
  47. RTCEvents.DOMINANT_SPEAKER_CHANGED,
  48. id);
  49. }
  50. }