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>
159 lines
5.3 KiB
Rust
159 lines
5.3 KiB
Rust
//! Driver and gateway error handling.
|
|
|
|
#[cfg(feature = "serenity")]
|
|
use futures::channel::mpsc::TrySendError;
|
|
#[cfg(feature = "serenity")]
|
|
use serenity::gateway::InterMessage;
|
|
#[cfg(feature = "gateway")]
|
|
use std::{error::Error, fmt};
|
|
#[cfg(feature = "twilight")]
|
|
use twilight_gateway::{cluster::ClusterCommandError, shard::CommandError};
|
|
|
|
#[cfg(feature = "gateway")]
|
|
#[derive(Debug)]
|
|
#[non_exhaustive]
|
|
/// Error returned when a manager or call handler is
|
|
/// unable to send messages over Discord's gateway.
|
|
pub enum JoinError {
|
|
/// Request to join was dropped, cancelled, or replaced.
|
|
Dropped,
|
|
/// No available gateway connection was provided to send
|
|
/// voice state update messages.
|
|
NoSender,
|
|
/// Tried to leave a [`Call`] which was not found.
|
|
///
|
|
/// [`Call`]: crate::Call
|
|
NoCall,
|
|
/// Connection details were not received from Discord in the
|
|
/// time given in [the `Call`'s configuration].
|
|
///
|
|
/// This can occur if a message is lost by the Discord client
|
|
/// between restarts, or if Discord's gateway believes that
|
|
/// this bot is still in the channel it attempts to join.
|
|
///
|
|
/// *Users should `leave` the server on the gateway before
|
|
/// re-attempting connection.*
|
|
///
|
|
/// [the `Call`'s configuration]: crate::Config
|
|
TimedOut,
|
|
#[cfg(feature = "driver")]
|
|
/// The driver failed to establish a voice connection.
|
|
///
|
|
/// *Users should `leave` the server on the gateway before
|
|
/// re-attempting connection.*
|
|
Driver(ConnectionError),
|
|
#[cfg(feature = "serenity")]
|
|
/// Serenity-specific WebSocket send error.
|
|
Serenity(TrySendError<InterMessage>),
|
|
#[cfg(feature = "twilight")]
|
|
/// Twilight-specific WebSocket send error returned when using a shard cluster.
|
|
TwilightCluster(ClusterCommandError),
|
|
#[cfg(feature = "twilight")]
|
|
/// Twilight-specific WebSocket send error when explicitly using a single shard.
|
|
TwilightShard(CommandError),
|
|
}
|
|
|
|
#[cfg(feature = "gateway")]
|
|
impl JoinError {
|
|
/// Indicates whether this failure may have left (or been
|
|
/// caused by) Discord's gateway state being in an
|
|
/// inconsistent state.
|
|
///
|
|
/// Failure to `leave` before rejoining may cause further
|
|
/// timeouts.
|
|
pub fn should_leave_server(&self) -> bool {
|
|
matches!(self, JoinError::TimedOut)
|
|
}
|
|
|
|
#[cfg(feature = "driver")]
|
|
/// Indicates whether this failure can be reattempted via
|
|
/// [`Driver::connect`] with retreived connection info.
|
|
///
|
|
/// Failure to `leave` before rejoining may cause further
|
|
/// timeouts.
|
|
///
|
|
/// [`Driver::connect`]: crate::driver::Driver
|
|
pub fn should_reconnect_driver(&self) -> bool {
|
|
matches!(self, JoinError::Driver(_))
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "gateway")]
|
|
impl fmt::Display for JoinError {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "failed to join voice channel: ")?;
|
|
match self {
|
|
JoinError::Dropped => write!(f, "request was cancelled/dropped"),
|
|
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"),
|
|
#[cfg(feature = "driver")]
|
|
JoinError::Driver(_) => write!(f, "establishing connection failed"),
|
|
#[cfg(feature = "serenity")]
|
|
JoinError::Serenity(e) => e.fmt(f),
|
|
#[cfg(feature = "twilight")]
|
|
JoinError::TwilightCluster(e) => e.fmt(f),
|
|
#[cfg(feature = "twilight")]
|
|
JoinError::TwilightShard(e) => e.fmt(f),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "gateway")]
|
|
impl Error for JoinError {
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
match self {
|
|
JoinError::Dropped => None,
|
|
JoinError::NoSender => None,
|
|
JoinError::NoCall => None,
|
|
JoinError::TimedOut => None,
|
|
#[cfg(feature = "driver")]
|
|
JoinError::Driver(e) => Some(e),
|
|
#[cfg(feature = "serenity")]
|
|
JoinError::Serenity(e) => e.source(),
|
|
#[cfg(feature = "twilight")]
|
|
JoinError::TwilightCluster(e) => e.source(),
|
|
#[cfg(feature = "twilight")]
|
|
JoinError::TwilightShard(e) => e.source(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[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"))]
|
|
impl From<CommandError> for JoinError {
|
|
fn from(e: CommandError) -> Self {
|
|
JoinError::TwilightShard(e)
|
|
}
|
|
}
|
|
|
|
#[cfg(all(feature = "twilight", feature = "gateway"))]
|
|
impl From<ClusterCommandError> for JoinError {
|
|
fn from(e: ClusterCommandError) -> Self {
|
|
JoinError::TwilightCluster(e)
|
|
}
|
|
}
|
|
|
|
#[cfg(all(feature = "driver", feature = "gateway"))]
|
|
impl From<ConnectionError> for JoinError {
|
|
fn from(e: ConnectionError) -> Self {
|
|
JoinError::Driver(e)
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "gateway")]
|
|
/// Convenience type for Discord gateway error handling.
|
|
pub type JoinResult<T> = Result<T, JoinError>;
|
|
|
|
#[cfg(feature = "driver")]
|
|
pub use crate::{
|
|
driver::connection::error::{Error as ConnectionError, Result as ConnectionResult},
|
|
tracks::{ControlError, PlayError, TrackResult},
|
|
};
|