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:
@@ -1,4 +1,4 @@
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
use crate::{driver::Driver, error::ConnectionResult};
|
||||
use crate::{
|
||||
error::{JoinError, JoinResult},
|
||||
@@ -12,7 +12,7 @@ use flume::Sender;
|
||||
use std::fmt::Debug;
|
||||
use tracing::instrument;
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -24,7 +24,7 @@ enum Return {
|
||||
// second indicates that the driver successfully connected.
|
||||
// The first is needed to cancel a timeout as the driver can/should
|
||||
// have separate connection timing/retry config.
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
Conn(Sender<()>, Sender<ConnectionResult<()>>),
|
||||
}
|
||||
|
||||
@@ -37,12 +37,12 @@ enum Return {
|
||||
/// [`Driver`]: struct@Driver
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Call {
|
||||
#[cfg(not(feature = "driver-core"))]
|
||||
#[cfg(not(feature = "driver"))]
|
||||
config: Config,
|
||||
|
||||
connection: Option<(ConnectionProgress, Return)>,
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
/// The internal controller of the voice connection monitor thread.
|
||||
driver: Driver,
|
||||
|
||||
@@ -73,12 +73,7 @@ impl Call {
|
||||
G: Into<GuildId> + Debug,
|
||||
U: Into<UserId> + Debug,
|
||||
{
|
||||
Self::new_raw_cfg(
|
||||
guild_id.into(),
|
||||
Some(ws),
|
||||
user_id.into(),
|
||||
Default::default(),
|
||||
)
|
||||
Self::new_raw_cfg(guild_id.into(), Some(ws), user_id.into(), Config::default())
|
||||
}
|
||||
|
||||
/// Creates a new Call, configuring the driver as specified.
|
||||
@@ -107,7 +102,7 @@ impl Call {
|
||||
G: Into<GuildId> + Debug,
|
||||
U: Into<UserId> + Debug,
|
||||
{
|
||||
Self::new_raw_cfg(guild_id.into(), None, user_id.into(), Default::default())
|
||||
Self::new_raw_cfg(guild_id.into(), None, user_id.into(), Config::default())
|
||||
}
|
||||
|
||||
/// Creates a new standalone Call from the given configuration file.
|
||||
@@ -123,10 +118,10 @@ impl Call {
|
||||
|
||||
fn new_raw_cfg(guild_id: GuildId, ws: Option<Shard>, user_id: UserId, config: Config) -> Self {
|
||||
Call {
|
||||
#[cfg(not(feature = "driver-core"))]
|
||||
#[cfg(not(feature = "driver"))]
|
||||
config,
|
||||
connection: None,
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
driver: Driver::new(config),
|
||||
guild_id,
|
||||
self_deaf: false,
|
||||
@@ -141,9 +136,9 @@ impl Call {
|
||||
match &self.connection {
|
||||
Some((ConnectionProgress::Complete(c), Return::Info(tx))) => {
|
||||
// It's okay if the receiver hung up.
|
||||
let _ = tx.send(c.clone());
|
||||
drop(tx.send(c.clone()));
|
||||
},
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
Some((ConnectionProgress::Complete(c), Return::Conn(first_tx, driver_tx))) => {
|
||||
// It's okay if the receiver hung up.
|
||||
let _ = first_tx.send(());
|
||||
@@ -195,7 +190,7 @@ impl Call {
|
||||
self.leave().await?;
|
||||
true
|
||||
} else if conn.0.channel_id() == channel_id {
|
||||
let _ = tx.send(completion_generator(self));
|
||||
drop(tx.send(completion_generator(self)));
|
||||
false
|
||||
} else {
|
||||
// not in progress, and/or a channel change.
|
||||
@@ -206,7 +201,7 @@ impl Call {
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
/// Connect or switch to the given voice channel by its Id.
|
||||
///
|
||||
/// This function acts as a future in two stages:
|
||||
@@ -227,7 +222,7 @@ impl Call {
|
||||
self._join(channel_id.into()).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
async fn _join(&mut self, channel_id: ChannelId) -> JoinResult<Join> {
|
||||
let (tx, rx) = flume::unbounded();
|
||||
let (gw_tx, gw_rx) = flume::unbounded();
|
||||
@@ -359,7 +354,7 @@ impl Call {
|
||||
fn leave_local(&mut self) {
|
||||
self.connection = None;
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
self.driver.leave();
|
||||
}
|
||||
|
||||
@@ -376,7 +371,7 @@ impl Call {
|
||||
pub async fn mute(&mut self, mute: bool) -> JoinResult<()> {
|
||||
self.self_mute = mute;
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
self.driver.mute(mute);
|
||||
|
||||
self.update().await
|
||||
@@ -419,7 +414,7 @@ impl Call {
|
||||
where
|
||||
C: Into<ChannelId> + Debug,
|
||||
{
|
||||
self._update_state(session_id, channel_id.map(|c| c.into()))
|
||||
self._update_state(session_id, channel_id.map(Into::into));
|
||||
}
|
||||
|
||||
fn _update_state(&mut self, session_id: String, channel_id: Option<ChannelId>) {
|
||||
@@ -460,7 +455,7 @@ impl Call {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "driver-core"))]
|
||||
#[cfg(not(feature = "driver"))]
|
||||
impl Call {
|
||||
/// Access this call handler's configuration.
|
||||
pub fn config(&self) -> &Config {
|
||||
@@ -478,7 +473,7 @@ impl Call {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
impl Deref for Call {
|
||||
type Target = Driver;
|
||||
|
||||
@@ -487,7 +482,7 @@ impl Deref for Call {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
impl DerefMut for Call {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.driver
|
||||
|
||||
Reference in New Issue
Block a user