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.

AudioMixerEffect.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import JitsiMeetJS from '../../base/lib-jitsi-meet';
  2. import { MEDIA_TYPE } from '../../base/media/constants';
  3. /**
  4. * Class Implementing the effect interface expected by a JitsiLocalTrack.
  5. * The AudioMixerEffect, as the name implies, mixes two JitsiLocalTracks containing a audio track. First track is
  6. * provided at the moment of creation, second is provided through the effect interface.
  7. */
  8. export class AudioMixerEffect {
  9. /**
  10. * JitsiLocalTrack that is going to be mixed into the track that uses this effect.
  11. */
  12. _mixAudio: any;
  13. /**
  14. * MediaStream resulted from mixing.
  15. */
  16. _mixedMediaStream: any;
  17. /**
  18. * MediaStreamTrack obtained from mixed stream.
  19. */
  20. _mixedMediaTrack: Object;
  21. /**
  22. * Original MediaStream from the JitsiLocalTrack that uses this effect.
  23. */
  24. _originalStream: Object;
  25. /**
  26. * MediaStreamTrack obtained from the original MediaStream.
  27. */
  28. _originalTrack: any;
  29. /**
  30. * Lib-jitsi-meet AudioMixer.
  31. */
  32. _audioMixer: any;
  33. /**
  34. * Creates AudioMixerEffect.
  35. *
  36. * @param {JitsiLocalTrack} mixAudio - JitsiLocalTrack which will be mixed with the original track.
  37. */
  38. constructor(mixAudio: any) {
  39. if (mixAudio.getType() !== MEDIA_TYPE.AUDIO) {
  40. throw new Error('AudioMixerEffect only supports audio JitsiLocalTracks; effect will not work!');
  41. }
  42. this._mixAudio = mixAudio;
  43. }
  44. /**
  45. * Checks if the JitsiLocalTrack supports this effect.
  46. *
  47. * @param {JitsiLocalTrack} sourceLocalTrack - Track to which the effect will be applied.
  48. * @returns {boolean} - Returns true if this effect can run on the specified track, false otherwise.
  49. */
  50. isEnabled(sourceLocalTrack: any) {
  51. // Both JitsiLocalTracks need to be audio i.e. contain an audio MediaStreamTrack
  52. return sourceLocalTrack.isAudioTrack() && this._mixAudio.isAudioTrack();
  53. }
  54. /**
  55. * Effect interface called by source JitsiLocalTrack, At this point a WebAudio ChannelMergerNode is created
  56. * and and the two associated MediaStreams are connected to it; the resulting mixed MediaStream is returned.
  57. *
  58. * @param {MediaStream} audioStream - Audio stream which will be mixed with _mixAudio.
  59. * @returns {MediaStream} - MediaStream containing both audio tracks mixed together.
  60. */
  61. startEffect(audioStream: MediaStream) {
  62. this._originalStream = audioStream;
  63. this._originalTrack = audioStream.getTracks()[0];
  64. this._audioMixer = JitsiMeetJS.createAudioMixer();
  65. this._audioMixer.addMediaStream(this._mixAudio.getOriginalStream());
  66. this._audioMixer.addMediaStream(this._originalStream);
  67. this._mixedMediaStream = this._audioMixer.start();
  68. this._mixedMediaTrack = this._mixedMediaStream.getTracks()[0];
  69. return this._mixedMediaStream;
  70. }
  71. /**
  72. * Reset the AudioMixer stopping it in the process.
  73. *
  74. * @returns {void}
  75. */
  76. stopEffect() {
  77. this._audioMixer.reset();
  78. }
  79. /**
  80. * Change the muted state of the effect.
  81. *
  82. * @param {boolean} muted - Should effect be muted or not.
  83. * @returns {void}
  84. */
  85. setMuted(muted: boolean) {
  86. this._originalTrack.enabled = !muted;
  87. }
  88. /**
  89. * Check whether or not this effect is muted.
  90. *
  91. * @returns {boolean}
  92. */
  93. isMuted() {
  94. return !this._originalTrack.enabled;
  95. }
  96. }