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:
Kyle Simpson
2022-07-23 23:29:02 +01:00
parent 6c6ffa7ca8
commit 8cc7a22b0b
136 changed files with 9761 additions and 4891 deletions

View File

@@ -4,12 +4,12 @@
use futures::channel::mpsc::TrySendError;
#[cfg(feature = "serenity")]
use serenity::gateway::InterMessage;
#[cfg(feature = "gateway-core")]
#[cfg(feature = "gateway")]
use std::{error::Error, fmt};
#[cfg(feature = "twilight")]
use twilight_gateway::{cluster::ClusterCommandError, shard::CommandError};
#[cfg(feature = "gateway-core")]
#[cfg(feature = "gateway")]
#[derive(Debug)]
#[non_exhaustive]
/// Error returned when a manager or call handler is
@@ -36,11 +36,7 @@ pub enum JoinError {
///
/// [the `Call`'s configuration]: crate::Config
TimedOut,
/// The given guild ID was zero.
IllegalGuild,
/// The given channel ID was zero.
IllegalChannel,
#[cfg(feature = "driver-core")]
#[cfg(feature = "driver")]
/// The driver failed to establish a voice connection.
///
/// *Users should `leave` the server on the gateway before
@@ -57,7 +53,7 @@ pub enum JoinError {
TwilightShard(CommandError),
}
#[cfg(feature = "gateway-core")]
#[cfg(feature = "gateway")]
impl JoinError {
/// Indicates whether this failure may have left (or been
/// caused by) Discord's gateway state being in an
@@ -69,7 +65,7 @@ impl JoinError {
matches!(self, JoinError::TimedOut)
}
#[cfg(feature = "driver-core")]
#[cfg(feature = "driver")]
/// Indicates whether this failure can be reattempted via
/// [`Driver::connect`] with retreived connection info.
///
@@ -82,7 +78,7 @@ impl JoinError {
}
}
#[cfg(feature = "gateway-core")]
#[cfg(feature = "gateway")]
impl fmt::Display for JoinError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to join voice channel: ")?;
@@ -91,9 +87,7 @@ impl fmt::Display for JoinError {
JoinError::NoSender => write!(f, "no gateway destination"),
JoinError::NoCall => write!(f, "tried to leave a non-existent call"),
JoinError::TimedOut => write!(f, "gateway response from Discord timed out"),
JoinError::IllegalGuild => write!(f, "target guild ID was zero"),
JoinError::IllegalChannel => write!(f, "target channel ID was zero"),
#[cfg(feature = "driver-core")]
#[cfg(feature = "driver")]
JoinError::Driver(_) => write!(f, "establishing connection failed"),
#[cfg(feature = "serenity")]
JoinError::Serenity(e) => e.fmt(f),
@@ -105,7 +99,7 @@ impl fmt::Display for JoinError {
}
}
#[cfg(feature = "gateway-core")]
#[cfg(feature = "gateway")]
impl Error for JoinError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
@@ -113,9 +107,7 @@ impl Error for JoinError {
JoinError::NoSender => None,
JoinError::NoCall => None,
JoinError::TimedOut => None,
JoinError::IllegalGuild => None,
JoinError::IllegalChannel => None,
#[cfg(feature = "driver-core")]
#[cfg(feature = "driver")]
JoinError::Driver(e) => Some(e),
#[cfg(feature = "serenity")]
JoinError::Serenity(e) => e.source(),
@@ -127,40 +119,40 @@ impl Error for JoinError {
}
}
#[cfg(all(feature = "serenity", feature = "gateway-core"))]
#[cfg(all(feature = "serenity", feature = "gateway"))]
impl From<TrySendError<InterMessage>> for JoinError {
fn from(e: TrySendError<InterMessage>) -> Self {
JoinError::Serenity(e)
}
}
#[cfg(all(feature = "twilight", feature = "gateway-core"))]
#[cfg(all(feature = "twilight", feature = "gateway"))]
impl From<CommandError> for JoinError {
fn from(e: CommandError) -> Self {
JoinError::TwilightShard(e)
}
}
#[cfg(all(feature = "twilight", feature = "gateway-core"))]
#[cfg(all(feature = "twilight", feature = "gateway"))]
impl From<ClusterCommandError> for JoinError {
fn from(e: ClusterCommandError) -> Self {
JoinError::TwilightCluster(e)
}
}
#[cfg(all(feature = "driver-core", feature = "gateway-core"))]
#[cfg(all(feature = "driver", feature = "gateway"))]
impl From<ConnectionError> for JoinError {
fn from(e: ConnectionError) -> Self {
JoinError::Driver(e)
}
}
#[cfg(feature = "gateway-core")]
#[cfg(feature = "gateway")]
/// Convenience type for Discord gateway error handling.
pub type JoinResult<T> = Result<T, JoinError>;
#[cfg(feature = "driver-core")]
#[cfg(feature = "driver")]
pub use crate::{
driver::connection::error::{Error as ConnectionError, Result as ConnectionResult},
tracks::{TrackError, TrackResult},
tracks::{ControlError, PlayError, TrackResult},
};