Driver/Input: Migrate audio backend to Symphonia (#89)
This extensive PR rewrites the internal mixing logic of the driver to use symphonia for parsing and decoding audio data, and rubato to resample audio. Existing logic to decode DCA and Opus formats/data have been reworked as plugins for symphonia. The main benefit is that we no longer need to keep yt-dlp and ffmpeg processes alive, saving a lot of memory and CPU: all decoding can be done in Rust! In exchange, we now need to do a lot of the HTTP handling and resumption ourselves, but this is still a huge net positive. `Input`s have been completely reworked such that all default (non-cached) sources are lazy by default, and are no longer covered by a special-case `Restartable`. These now span a gamut from a `Compose` (lazy), to a live source, to a fully `Parsed` source. As mixing is still sync, this includes adapters for `AsyncRead`/`AsyncSeek`, and HTTP streams. `Track`s have been reworked so that they only contain initialisation state for each track. `TrackHandles` are only created once a `Track`/`Input` has been handed over to the driver, replacing `create_player` and related functions. `TrackHandle::action` now acts on a `View` of (im)mutable state, and can request seeks/readying via `Action`. Per-track event handling has also been improved -- we can now determine and propagate the reason behind individual track errors due to the new backend. Some `TrackHandle` commands (seek etc.) benefit from this, and now use internal callbacks to signal completion. Due to associated PRs on felixmcfelix/songbird from avid testers, this includes general clippy tweaks, API additions, and other repo-wide cleanup. Thanks go out to the below co-authors. Co-authored-by: Gnome! <45660393+GnomedDev@users.noreply.github.com> Co-authored-by: Alakh <36898190+alakhpc@users.noreply.github.com>
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
name = "voice_events_queue"
|
||||
version = "0.1.0"
|
||||
authors = ["my name <my@email.address>"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
reqwest = "0.11"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = "0.2"
|
||||
tracing-futures = "0.2"
|
||||
@@ -17,6 +18,12 @@ path = "../../../"
|
||||
version = "0.11"
|
||||
features = ["cache", "standard_framework", "voice", "rustls_backend"]
|
||||
|
||||
[dependencies.symphonia]
|
||||
version = "0.5"
|
||||
features = ["aac", "mp3", "isomp4", "alac"]
|
||||
git = "https://github.com/FelixMcFelix/Symphonia"
|
||||
branch = "songbird-fixes"
|
||||
|
||||
[dependencies.tokio]
|
||||
version = "1.0"
|
||||
features = ["macros", "rt-multi-thread"]
|
||||
features = ["macros", "rt-multi-thread", "signal"]
|
||||
|
||||
@@ -18,6 +18,8 @@ use std::{
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use reqwest::Client as HttpClient;
|
||||
|
||||
use serenity::{
|
||||
async_trait,
|
||||
client::{Client, Context, EventHandler},
|
||||
@@ -31,15 +33,12 @@ use serenity::{
|
||||
},
|
||||
http::Http,
|
||||
model::{channel::Message, gateway::Ready, prelude::ChannelId},
|
||||
prelude::{GatewayIntents, Mentionable},
|
||||
prelude::{GatewayIntents, Mentionable, TypeMapKey},
|
||||
Result as SerenityResult,
|
||||
};
|
||||
|
||||
use songbird::{
|
||||
input::{
|
||||
self,
|
||||
restartable::Restartable,
|
||||
},
|
||||
input::YoutubeDl,
|
||||
Event,
|
||||
EventContext,
|
||||
EventHandler as VoiceEventHandler,
|
||||
@@ -47,6 +46,12 @@ use songbird::{
|
||||
TrackEvent,
|
||||
};
|
||||
|
||||
struct HttpKey;
|
||||
|
||||
impl TypeMapKey for HttpKey {
|
||||
type Value = HttpClient;
|
||||
}
|
||||
|
||||
struct Handler;
|
||||
|
||||
#[async_trait]
|
||||
@@ -70,16 +75,16 @@ async fn main() {
|
||||
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
||||
|
||||
let framework = StandardFramework::new()
|
||||
.configure(|c| c.prefix("~"))
|
||||
.group(&GENERAL_GROUP);
|
||||
framework.configure(|c| c.prefix("~"));
|
||||
|
||||
let intents = GatewayIntents::non_privileged()
|
||||
| GatewayIntents::MESSAGE_CONTENT;
|
||||
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
|
||||
|
||||
let mut client = Client::builder(&token, intents)
|
||||
.event_handler(Handler)
|
||||
.framework(framework)
|
||||
.register_songbird()
|
||||
.type_map_insert::<HttpKey>(HttpClient::new())
|
||||
.await
|
||||
.expect("Err creating client");
|
||||
|
||||
@@ -87,12 +92,28 @@ async fn main() {
|
||||
.start()
|
||||
.await
|
||||
.map_err(|why| println!("Client ended: {:?}", why));
|
||||
|
||||
tokio::spawn(async move {
|
||||
let _ = client
|
||||
.start()
|
||||
.await
|
||||
.map_err(|why| println!("Client ended: {:?}", why));
|
||||
});
|
||||
|
||||
let _signal_err = tokio::signal::ctrl_c().await;
|
||||
println!("Received Ctrl-C, shutting down.");
|
||||
}
|
||||
|
||||
async fn get_http_client(ctx: &Context) -> HttpClient {
|
||||
let data = ctx.data.read().await;
|
||||
data.get::<HttpKey>()
|
||||
.cloned()
|
||||
.expect("Guaranteed to exist in the typemap.")
|
||||
}
|
||||
|
||||
#[command]
|
||||
async fn deafen(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let guild_id = msg.guild(&ctx.cache).unwrap().id;
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
@@ -130,13 +151,15 @@ async fn deafen(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
async fn join(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let (guild_id, channel_id) = {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let channel_id = guild
|
||||
.voice_states
|
||||
.get(&msg.author.id)
|
||||
.and_then(|voice_state| voice_state.channel_id);
|
||||
|
||||
let channel_id = guild
|
||||
.voice_states
|
||||
.get(&msg.author.id)
|
||||
.and_then(|voice_state| voice_state.channel_id);
|
||||
(guild.id, channel_id)
|
||||
};
|
||||
|
||||
let connect_to = match channel_id {
|
||||
Some(channel) => channel,
|
||||
@@ -245,8 +268,7 @@ impl VoiceEventHandler for ChannelDurationNotifier {
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
async fn leave(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let guild_id = msg.guild(&ctx.cache).unwrap().id;
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
@@ -274,8 +296,7 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
async fn mute(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let guild_id = msg.guild(&ctx.cache).unwrap().id;
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
@@ -343,8 +364,9 @@ async fn play_fade(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
let http_client = get_http_client(ctx).await;
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
@@ -354,20 +376,11 @@ async fn play_fade(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
|
||||
let source = match input::ytdl(&url).await {
|
||||
Ok(source) => source,
|
||||
Err(why) => {
|
||||
println!("Err starting source: {:?}", why);
|
||||
|
||||
check_msg(msg.channel_id.say(&ctx.http, "Error sourcing ffmpeg").await);
|
||||
|
||||
return Ok(());
|
||||
},
|
||||
};
|
||||
let src = YoutubeDl::new(http_client, url);
|
||||
|
||||
// This handler object will allow you to, as needed,
|
||||
// control the audio track via events and further commands.
|
||||
let song = handler.play_source(source);
|
||||
let song = handler.play_input(src.into());
|
||||
let send_http = ctx.http.clone();
|
||||
let chan_id = msg.channel_id;
|
||||
|
||||
@@ -474,8 +487,9 @@ async fn queue(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
let http_client = get_http_client(ctx).await;
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
@@ -487,18 +501,9 @@ async fn queue(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
|
||||
// Here, we use lazy restartable sources to make sure that we don't pay
|
||||
// for decoding, playback on tracks which aren't actually live yet.
|
||||
let source = match Restartable::ytdl(url, true).await {
|
||||
Ok(source) => source,
|
||||
Err(why) => {
|
||||
println!("Err starting source: {:?}", why);
|
||||
let src = YoutubeDl::new(http_client, url);
|
||||
|
||||
check_msg(msg.channel_id.say(&ctx.http, "Error sourcing ffmpeg").await);
|
||||
|
||||
return Ok(());
|
||||
},
|
||||
};
|
||||
|
||||
handler.enqueue_source(source.into());
|
||||
handler.enqueue_input(src.into()).await;
|
||||
|
||||
check_msg(
|
||||
msg.channel_id
|
||||
@@ -522,8 +527,7 @@ async fn queue(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
async fn skip(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
@@ -557,8 +561,7 @@ async fn skip(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
async fn stop(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
@@ -568,7 +571,7 @@ async fn stop(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let handler = handler_lock.lock().await;
|
||||
let queue = handler.queue();
|
||||
let _ = queue.stop();
|
||||
queue.stop();
|
||||
|
||||
check_msg(msg.channel_id.say(&ctx.http, "Queue cleared.").await);
|
||||
} else {
|
||||
@@ -585,8 +588,7 @@ async fn stop(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
@@ -618,8 +620,7 @@ async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
#[command]
|
||||
#[only_in(guilds)]
|
||||
async fn unmute(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
let guild = msg.guild(&ctx.cache).unwrap();
|
||||
let guild_id = guild.id;
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
|
||||
Reference in New Issue
Block a user