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 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 local user is talking
  7. * while its microphone is muted.
  8. * @constructor
  9. */
  10. constructor(conference, callback) {
  11. this.callback = callback;
  12. conference.statistics.addAudioLevelListener(
  13. this.audioLevelListener.bind(this));
  14. conference.eventEmitter.on(
  15. JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  16. this.muteChanged.bind(this));
  17. conference.eventEmitter.on(
  18. JitsiConferenceEvents.TRACK_ADDED,
  19. this.onTrackAdded.bind(this));
  20. // we track firing the event, in order to avoid sending too many events
  21. this.eventFired = false;
  22. }
  23. /**
  24. * Adds local tracks. We are interested only in the audio one.
  25. * @param track
  26. */
  27. onTrackAdded(track) {
  28. if (!track.isAudioTrack())
  29. return;
  30. this.audioTrack = track;
  31. }
  32. /**
  33. * Receives audio level events for all send/receive streams.
  34. * @param ssrc the ssrc of the stream
  35. * @param level the current audio level
  36. * @param isLocal whether this is local or remote stream (sent or received)
  37. */
  38. audioLevelListener(ssrc, level, isLocal) {
  39. // we are interested only in local audio stream
  40. // and if event is not already sent
  41. if (!isLocal || !this.audioTrack || this.eventFired)
  42. return;
  43. if (this.audioTrack.isMuted() && level > 0.6) {
  44. this.eventFired = true;
  45. this.callback();
  46. }
  47. }
  48. /**
  49. * Mute changed for a track.
  50. * @param track the track which mute state has changed.
  51. */
  52. muteChanged(track) {
  53. if (track.isLocal() && track.isAudioTrack() && track.isMuted())
  54. this.eventFired = false;
  55. }
  56. }