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

TalkMutedDetection.js 2.2KB

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