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,10 +2,11 @@
|
||||
name = "twilight"
|
||||
version = "0.1.0"
|
||||
authors = ["Twilight and Serenity Contributors"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
futures = "0.3"
|
||||
reqwest = "0.11"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = "0.2"
|
||||
tokio = { features = ["macros", "rt-multi-thread", "sync"], version = "1" }
|
||||
@@ -18,3 +19,9 @@ twilight-standby = "0.12"
|
||||
default-features = false
|
||||
path = "../.."
|
||||
features = ["driver", "twilight-rustls", "zlib-stock"]
|
||||
|
||||
[dependencies.symphonia]
|
||||
version = "0.5"
|
||||
features = ["aac", "mp3", "isomp4", "alac"]
|
||||
git = "https://github.com/FelixMcFelix/Symphonia"
|
||||
branch = "songbird-fixes"
|
||||
|
||||
@@ -22,11 +22,11 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use songbird::{
|
||||
input::{Input, Restartable},
|
||||
input::{Compose, YoutubeDl},
|
||||
tracks::{PlayMode, TrackHandle},
|
||||
Songbird,
|
||||
};
|
||||
use std::{collections::HashMap, env, error::Error, future::Future, sync::Arc};
|
||||
use std::{collections::HashMap, env, error::Error, future::Future, num::NonZeroU64, sync::Arc};
|
||||
use tokio::sync::RwLock;
|
||||
use twilight_gateway::{Cluster, Event, Intents};
|
||||
use twilight_http::Client as HttpClient;
|
||||
@@ -68,7 +68,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
let http = HttpClient::new(token.clone());
|
||||
let user_id = http.current_user().exec().await?.model().await?.id;
|
||||
|
||||
let intents = Intents::GUILD_MESSAGES | Intents::GUILD_VOICE_STATES;
|
||||
let intents = Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT | Intents::GUILD_VOICE_STATES;
|
||||
let (cluster, events) = Cluster::new(token, intents).await?;
|
||||
cluster.up().await;
|
||||
|
||||
@@ -81,8 +81,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
trackdata: Default::default(),
|
||||
songbird,
|
||||
standby: Standby::new(),
|
||||
},
|
||||
))
|
||||
}),
|
||||
)
|
||||
};
|
||||
|
||||
while let Some((_, event)) = events.next().await {
|
||||
@@ -128,6 +128,8 @@ async fn join(msg: Message, state: State) -> Result<(), Box<dyn Error + Send + S
|
||||
let channel_id = msg.content.parse::<u64>()?;
|
||||
|
||||
let guild_id = msg.guild_id.ok_or("Can't join a non-guild channel.")?;
|
||||
let channel_id =
|
||||
NonZeroU64::new(channel_id).ok_or("Joined voice channel must have nonzero ID.")?;
|
||||
|
||||
let (_handle, success) = state.songbird.join(guild_id, channel_id).await;
|
||||
|
||||
@@ -190,21 +192,12 @@ async fn play(msg: Message, state: State) -> Result<(), Box<dyn Error + Send + S
|
||||
|
||||
let guild_id = msg.guild_id.unwrap();
|
||||
|
||||
if let Ok(song) = Restartable::ytdl(msg.content.clone(), false).await {
|
||||
let input = Input::from(song);
|
||||
|
||||
let mut src = YoutubeDl::new(reqwest::Client::new(), msg.content.clone());
|
||||
if let Ok(metadata) = src.aux_metadata().await {
|
||||
let content = format!(
|
||||
"Playing **{:?}** by **{:?}**",
|
||||
input
|
||||
.metadata
|
||||
.track
|
||||
.as_ref()
|
||||
.unwrap_or(&"<UNKNOWN>".to_string()),
|
||||
input
|
||||
.metadata
|
||||
.artist
|
||||
.as_ref()
|
||||
.unwrap_or(&"<UNKNOWN>".to_string()),
|
||||
metadata.track.as_ref().unwrap_or(&"<UNKNOWN>".to_string()),
|
||||
metadata.artist.as_ref().unwrap_or(&"<UNKNOWN>".to_string()),
|
||||
);
|
||||
|
||||
state
|
||||
@@ -216,7 +209,7 @@ async fn play(msg: Message, state: State) -> Result<(), Box<dyn Error + Send + S
|
||||
|
||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||
let mut call = call_lock.lock().await;
|
||||
let handle = call.play_source(input);
|
||||
let handle = call.play_input(src.into());
|
||||
|
||||
let mut store = state.trackdata.write().await;
|
||||
store.insert(guild_id, handle);
|
||||
@@ -251,11 +244,11 @@ async fn pause(msg: Message, state: State) -> Result<(), Box<dyn Error + Send +
|
||||
PlayMode::Play => {
|
||||
let _success = handle.pause();
|
||||
false
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
let _success = handle.play();
|
||||
true
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let action = if paused { "Unpaused" } else { "Paused" };
|
||||
@@ -301,12 +294,8 @@ async fn seek(msg: Message, state: State) -> Result<(), Box<dyn Error + Send + S
|
||||
let store = state.trackdata.read().await;
|
||||
|
||||
let content = if let Some(handle) = store.get(&guild_id) {
|
||||
if handle.is_seekable() {
|
||||
let _success = handle.seek_time(std::time::Duration::from_secs(position));
|
||||
format!("Seeked to {}s", position)
|
||||
} else {
|
||||
format!("Track is not compatible with seeking!")
|
||||
}
|
||||
let _success = handle.seek(std::time::Duration::from_secs(position));
|
||||
format!("Seeking to {}s", position)
|
||||
} else {
|
||||
format!("No track to seek over!")
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user