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.

AudioMixerEffect.js 3.1KB

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