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>
90 lines
2.9 KiB
Rust
90 lines
2.9 KiB
Rust
//! Compatability and convenience methods for working with [serenity].
|
|
//! Requires the `"serenity-rustls"` or `"serenity-native"` features.
|
|
//!
|
|
//! [serenity]: https://crates.io/crates/serenity/0.9.0-rc.2
|
|
|
|
use crate::{Config, Songbird};
|
|
use serenity::{
|
|
client::{ClientBuilder, Context},
|
|
prelude::TypeMapKey,
|
|
};
|
|
use std::sync::Arc;
|
|
|
|
/// Zero-size type used to retrieve the registered [`Songbird`] instance
|
|
/// from serenity's inner [`TypeMap`].
|
|
///
|
|
/// [`Songbird`]: Songbird
|
|
/// [`TypeMap`]: serenity::prelude::TypeMap
|
|
pub struct SongbirdKey;
|
|
|
|
impl TypeMapKey for SongbirdKey {
|
|
type Value = Arc<Songbird>;
|
|
}
|
|
|
|
/// Installs a new songbird instance into the serenity client.
|
|
///
|
|
/// This should be called after any uses of `ClientBuilder::type_map`.
|
|
pub fn register(client_builder: ClientBuilder) -> ClientBuilder {
|
|
let voice = Songbird::serenity();
|
|
register_with(client_builder, voice)
|
|
}
|
|
|
|
/// Installs a given songbird instance into the serenity client.
|
|
///
|
|
/// This should be called after any uses of `ClientBuilder::type_map`.
|
|
pub fn register_with(client_builder: ClientBuilder, voice: Arc<Songbird>) -> ClientBuilder {
|
|
client_builder
|
|
.voice_manager_arc(voice.clone())
|
|
.type_map_insert::<SongbirdKey>(voice)
|
|
}
|
|
|
|
/// Installs a given songbird instance into the serenity client.
|
|
///
|
|
/// This should be called after any uses of `ClientBuilder::type_map`.
|
|
pub fn register_from_config(client_builder: ClientBuilder, config: Config) -> ClientBuilder {
|
|
let voice = Songbird::serenity_from_config(config);
|
|
register_with(client_builder, voice)
|
|
}
|
|
|
|
/// Retrieve the Songbird voice client from a serenity context's
|
|
/// shared key-value store.
|
|
pub async fn get(ctx: &Context) -> Option<Arc<Songbird>> {
|
|
let data = ctx.data.read().await;
|
|
|
|
data.get::<SongbirdKey>().cloned()
|
|
}
|
|
|
|
/// Helper trait to add installation/creation methods to serenity's
|
|
/// `ClientBuilder`.
|
|
///
|
|
/// These install the client to receive gateway voice events, and
|
|
/// store an easily accessible reference to Songbird's managers.
|
|
pub trait SerenityInit {
|
|
/// Registers a new Songbird voice system with serenity, storing it for easy
|
|
/// access via [`get`].
|
|
///
|
|
/// [`get`]: get
|
|
#[must_use]
|
|
fn register_songbird(self) -> Self;
|
|
/// Registers a given Songbird voice system with serenity, as above.
|
|
#[must_use]
|
|
fn register_songbird_with(self, voice: Arc<Songbird>) -> Self;
|
|
/// Registers a Songbird voice system serenity, based on the given configuration.
|
|
#[must_use]
|
|
fn register_songbird_from_config(self, config: Config) -> Self;
|
|
}
|
|
|
|
impl SerenityInit for ClientBuilder {
|
|
fn register_songbird(self) -> Self {
|
|
register(self)
|
|
}
|
|
|
|
fn register_songbird_with(self, voice: Arc<Songbird>) -> Self {
|
|
register_with(self, voice)
|
|
}
|
|
|
|
fn register_songbird_from_config(self, config: Config) -> Self {
|
|
register_from_config(self, config)
|
|
}
|
|
}
|