diff --git a/src/client/voice/dispatcher/StreamDispatcher.js b/src/client/voice/dispatcher/StreamDispatcher.js index 57ff9cb88..927fb1d49 100644 --- a/src/client/voice/dispatcher/StreamDispatcher.js +++ b/src/client/voice/dispatcher/StreamDispatcher.js @@ -29,7 +29,7 @@ nonce.fill(0); * @extends {stream.Writable} */ class StreamDispatcher extends Writable { - constructor(player, streamOptions) { + constructor(player, streamOptions, streams) { super(streamOptions); /** * The Audio Player that controls this dispatcher @@ -37,6 +37,7 @@ class StreamDispatcher extends Writable { */ this.player = player; this.streamOptions = streamOptions; + this.streams = streams; /** * The time that the stream was paused at (null if not paused) @@ -67,9 +68,8 @@ class StreamDispatcher extends Writable { } _destroy(err, cb) { - if (this.player.dispatcher !== this) return; - this.player.dispatcher = null; - const streams = this.player.streams; + if (this.player.dispatcher === this) this.player.dispatcher = null; + const { streams } = this; if (streams.opus) streams.opus.unpipe(this); if (streams.ffmpeg) streams.ffmpeg.destroy(); super._destroy(err, cb); @@ -123,7 +123,12 @@ class StreamDispatcher extends Writable { * If set to 'auto', the voice channel's bitrate will be used * @returns {boolean} true if the bitrate has been successfully changed. */ - setBitrate(value) { return this.player.setBitrate(value); } + setBitrate(value) { + if (!value || !this.streams.opus || !this.streams.opus.setBitrate) return false; + const bitrate = value === 'auto' ? this.player.voiceConnection.channel.bitrate : value; + this.streams.opus.setBitrate(bitrate * 1000); + return true; + } _step(done) { if (this.pausedSince) { @@ -191,7 +196,7 @@ class StreamDispatcher extends Writable { this.emit('speaking', value); } - get volumeEditable() { return Boolean(this.player.streams.volume); } + get volumeEditable() { return Boolean(this.streams.volume); } /** * Whether or not the Opus bitrate of this stream is editable @@ -200,12 +205,12 @@ class StreamDispatcher extends Writable { // Volume get volume() { - return this.player.streams.volume ? this.player.streams.volume.volume : 1; + return this.streams.volume ? this.streams.volume.volume : 1; } setVolume(value) { - if (!this.player.streams.volume) return false; - this.player.streams.volume.setVolume(value); + if (!this.streams.volume) return false; + this.streams.volume.setVolume(value); return true; } diff --git a/src/client/voice/player/AudioPlayer.js b/src/client/voice/player/AudioPlayer.js index eeb578a64..a9b07a4d0 100644 --- a/src/client/voice/player/AudioPlayer.js +++ b/src/client/voice/player/AudioPlayer.js @@ -24,7 +24,6 @@ class AudioPlayer extends EventEmitter { */ this.voiceConnection = voiceConnection; - this.streams = {}; this.dispatcher = null; this.streamingData = { @@ -47,45 +46,33 @@ class AudioPlayer extends EventEmitter { } } - /** - * Set the bitrate of the current Opus encoder. - * @param {number} value New bitrate, in kbps - * If set to 'auto', the voice channel's bitrate will be used - * @returns {boolean} true if the bitrate has been successfully changed. - */ - setBitrate(value) { - if (!value || !this.streams.opus || !this.streams.opus.setBitrate) return false; - const bitrate = value === 'auto' ? this.voiceConnection.channel.bitrate : value; - this.streams.opus.setBitrate(bitrate * 1000); - return true; - } - playUnknownStream(stream, options = {}) { this.destroyDispatcher(); - const ffmpeg = this.streams.ffmpeg = new prism.FFmpeg({ args: FFMPEG_ARGUMENTS }); + const ffmpeg = new prism.FFmpeg({ args: FFMPEG_ARGUMENTS }); stream.pipe(ffmpeg); - return this.playPCMStream(ffmpeg, options); + return this.playPCMStream(ffmpeg, options, { ffmpeg }); } - playPCMStream(stream, options = {}) { + playPCMStream(stream, options = {}, streams = {}) { this.destroyDispatcher(); - const volume = this.streams.volume = new prism.VolumeTransformer16LE(null, { volume: 0.2 }); - const opus = this.streams.opus = new prism.opus.Encoder({ channels: 2, rate: 48000, frameSize: 960 }); + const volume = streams.volume = new prism.VolumeTransformer16LE(null, { volume: 0.2 }); + const opus = streams.opus = new prism.opus.Encoder({ channels: 2, rate: 48000, frameSize: 960 }); stream.pipe(volume).pipe(opus); - return this.playOpusStream(opus, options); + return this.playOpusStream(opus, options, streams); } - playOpusStream(stream, options = {}) { + playOpusStream(stream, options = {}, streams = {}) { this.destroyDispatcher(); - const dispatcher = this.dispatcher = this.createDispatcher(options); + streams.opus = stream; + const dispatcher = this.dispatcher = this.createDispatcher(options, streams); stream.pipe(dispatcher); return dispatcher; } - createDispatcher({ seek = 0, volume = 1, passes = 1 } = {}) { + createDispatcher({ seek = 0, volume = 1, passes = 1 } = {}, streams) { this.destroyDispatcher(); const options = { seek, volume, passes }; - const dispatcher = new StreamDispatcher(this, options); + const dispatcher = new StreamDispatcher(this, options, streams); dispatcher.on('speaking', value => this.voiceConnection.setSpeaking(value)); return dispatcher; }