diff --git a/src/client/voice/dispatcher/StreamDispatcher.js b/src/client/voice/dispatcher/StreamDispatcher.js index 4ad6998fe..068ff4104 100644 --- a/src/client/voice/dispatcher/StreamDispatcher.js +++ b/src/client/voice/dispatcher/StreamDispatcher.js @@ -38,7 +38,7 @@ class StreamDispatcher extends EventEmitter { * aren't recommended, as it means you are using 5x more bandwidth. You _can_ edit this at runtime. * @type {number} */ - this.passes = streamOptions.passes || 1; + this.passes = streamOptions.passes || 3; /** * Whether playing is paused diff --git a/src/client/voice/player/AudioPlayer.js b/src/client/voice/player/AudioPlayer.js index baef5acf4..73958c317 100644 --- a/src/client/voice/player/AudioPlayer.js +++ b/src/client/voice/player/AudioPlayer.js @@ -10,30 +10,51 @@ class AudioPlayer extends EventEmitter { this.voiceConnection = voiceConnection; this.audioToPCM = new (PCMConverters.fetch())(); this.opusEncoder = OpusEncoders.fetch(); - + this.currentConverter = null; + this.currentDispatcher = null; this.audioToPCM.on('error', e => this.emit('error', e)); - } - - playUnknownStream(stream) { - const conversionProcess = this.audioToPCM.createConvertStream(0); - stream.pipe(conversionProcess.process.stdin, { end: false }); - return this.playPCMStream(conversionProcess.process.stdout); - } - - playPCMStream(stream) { - stream.on('error', e => this.emit('error', e)); - const dispatcher = new StreamDispatcher(this, stream, { + this.streamingData = { channels: 2, count: 0, sequence: 0, timestamp: 0, pausedTime: 0, - }, { + }; + } + + playUnknownStream(stream) { + const conversionProcess = this.audioToPCM.createConvertStream(0); + stream.pipe(conversionProcess.process.stdin, { end: false }); + return this.playPCMStream(conversionProcess.process.stdout, conversionProcess); + } + + cleanup(checkStream) { + const filter = checkStream && this.currentDispatcher && this.currentDispatcher.stream === checkStream; + if (this.currentConverter && (checkStream ? filter : true)) { + if (this.currentConverter.process.stdin.destroy) { + this.currentConverter.process.stdin.destroy(); + } + if (this.currentConverter.process.kill) { + this.currentConverter.process.kill(); + } + this.currentConverter = null; + } + } + + playPCMStream(stream, converter) { + this.cleanup(); + this.currentConverter = converter; + if (this.currentDispatcher) { + this.streamingData = this.currentDispatcher.streamingData; + } + stream.on('error', e => this.emit('error', e)); + const dispatcher = new StreamDispatcher(this, stream, this.streamingData, { volume: 1, }); - dispatcher.on('error', e => console.log('error', e)); - dispatcher.on('end', e => console.log('end', e)); + dispatcher.on('error', e => this.emit('error', e)); + dispatcher.on('end', () => this.cleanup(dispatcher.stream)); dispatcher.on('speaking', value => this.voiceConnection.setSpeaking(value)); + this.currentDispatcher = dispatcher; } }