Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volume controlled opus streams do not get properly destroyed #5429

Closed
keybraker opened this issue Mar 22, 2021 · 1 comment · Fixed by #5402
Closed

Volume controlled opus streams do not get properly destroyed #5429

keybraker opened this issue Mar 22, 2021 · 1 comment · Fixed by #5402

Comments

@keybraker
Copy link

keybraker commented Mar 22, 2021

When creating a stream with opus that is volume controled volume: true it does NOT destroy the stream upon .end() or .destroy() function calls. In contrary, when source stream ends, it gets destroyed from source.

On playOpusStream in line 71 you can see that a pipe is used to enable for volume adjustments.

streams.opus = stream
        .pipe(decoder)
        .pipe(streams.volume)
        .pipe(new prism.opus.Encoder({ channels: 2, rate: 48000, frameSize: 960 }));

Piping the source stream to current dispatcher creates an intermediate which prevents _destroy() from destroying the source opus stream.

 _destroy(err, cb) {
    this._cleanup();
    super._destroy(err, cb);
  }

  _cleanup() {
    if (this.player.dispatcher === this) this.player.dispatcher = null;
    const { streams } = this;
    if (streams.broadcast) streams.broadcast.delete(this);
    if (streams.opus) streams.opus.destroy();
    if (streams.ffmpeg) streams.ffmpeg.destroy();
  }

Reproduce

With the following code, you execute the destroy function dispatcher.destroy(); and the stream will still run, in the background idle (can be seen using top in terminal)

function create_dispatcher(
	video_options: yts.VideoSearchResult, voice_connection: VoiceConnection
): StreamDispatcher {
	const stream = ytdl(video_options.url, {
		filter: 'audioonly',
		opusEncoded: true,
		encoderArgs: ['-af', 'bass=g=10, dynaudnorm=f=200']
	});

	const stream_options = <StreamOptions>{
		type: 'opus'
	}

	return voice_connection.play(stream, stream_options);
}

In order to destroy the source stream you must add volume: false option in StreamOptions

const stream_options = <StreamOptions>{
	type: 'opus',
        volume: false
}

Note that setting a custom bitrate will also trigger a pipe and thus not destroy upon destroy()

This probably is not the intended behaviour, and should probably be handled seperately.

Related errors:

4009986
nodejs/node#32968

Further details:

  • discord.js version: 12.5.1 (stable)
  • Node.js version: 14.16.0
  • Operating system: (Ubuntu 20.04.2 LTS focal)
  • Priority this issue should have: medium
    not essential but causes bots to crash and run artificially slow
@amishshah
Copy link
Member

Hi there,

We're working on a new implementation of Discord's Voice API that has better playback quality and is more reliable than what we currently support in Discord.js v12 - check it out at https://github.com/discordjs/voice!

The new library solves many of the issues that users are facing, and as part of this, we're dropping built-in support for voice in our next major release. We have a PR (#5402) that adds native support for our new voice library - once this PR is merged, this issue will be closed.

You can still use our new voice library before that PR lands - just take a look at our music bot example to see how to get started upgrading your voice code. By using the boilerplate music player in the example, you can make it even easier to upgrade your code.

Note that the PR above only reduces some of the boilerplate code you'd otherwise have to write - you do not have to wait for the PR to be merged to start using the new voice library.


If you have any questions about this, feel free to:

  • Make an issue if you have found a bug in the new voice library
  • Use GitHub Discussions or join our Discord server (we have a new channel, #djs-new-voice, specifically for this!) to ask general questions about the library, give feedback on the library, and get support with upgrading to it

@amishshah amishshah linked a pull request Jun 9, 2021 that will close this issue
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants