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,3 +1,5 @@
|
||||
#[cfg(feature = "serenity")]
|
||||
use crate::shards::SerenitySharder;
|
||||
use crate::{
|
||||
error::{JoinError, JoinResult},
|
||||
id::{ChannelId, GuildId, UserId},
|
||||
@@ -11,6 +13,7 @@ use async_trait::async_trait;
|
||||
use dashmap::DashMap;
|
||||
#[cfg(feature = "serenity")]
|
||||
use futures::channel::mpsc::UnboundedSender as Sender;
|
||||
use once_cell::sync::OnceCell;
|
||||
use parking_lot::RwLock as PRwLock;
|
||||
#[cfg(feature = "serenity")]
|
||||
use serenity::{
|
||||
@@ -29,10 +32,9 @@ use twilight_gateway::Cluster;
|
||||
#[cfg(feature = "twilight")]
|
||||
use twilight_model::gateway::event::Event as TwilightEvent;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct ClientData {
|
||||
shard_count: u64,
|
||||
initialised: bool,
|
||||
user_id: UserId,
|
||||
}
|
||||
|
||||
@@ -44,7 +46,7 @@ struct ClientData {
|
||||
/// [`Call`]: Call
|
||||
#[derive(Debug)]
|
||||
pub struct Songbird {
|
||||
client_data: PRwLock<ClientData>,
|
||||
client_data: OnceCell<ClientData>,
|
||||
calls: DashMap<GuildId, Arc<Mutex<Call>>>,
|
||||
sharder: Sharder,
|
||||
config: PRwLock<Option<Config>>,
|
||||
@@ -57,8 +59,9 @@ impl Songbird {
|
||||
/// This must be [registered] after creation.
|
||||
///
|
||||
/// [registered]: crate::serenity::register_with
|
||||
#[must_use]
|
||||
pub fn serenity() -> Arc<Self> {
|
||||
Self::serenity_from_config(Default::default())
|
||||
Self::serenity_from_config(Config::default())
|
||||
}
|
||||
|
||||
#[cfg(feature = "serenity")]
|
||||
@@ -67,11 +70,12 @@ impl Songbird {
|
||||
/// This must be [registered] after creation.
|
||||
///
|
||||
/// [registered]: crate::serenity::register_with
|
||||
#[must_use]
|
||||
pub fn serenity_from_config(config: Config) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
client_data: Default::default(),
|
||||
calls: Default::default(),
|
||||
sharder: Sharder::Serenity(Default::default()),
|
||||
client_data: OnceCell::new(),
|
||||
calls: DashMap::new(),
|
||||
sharder: Sharder::Serenity(SerenitySharder::default()),
|
||||
config: Some(config).into(),
|
||||
})
|
||||
}
|
||||
@@ -88,7 +92,7 @@ impl Songbird {
|
||||
where
|
||||
U: Into<UserId>,
|
||||
{
|
||||
Self::twilight_from_config(cluster, user_id, Default::default())
|
||||
Self::twilight_from_config(cluster, user_id, Config::default())
|
||||
}
|
||||
|
||||
#[cfg(feature = "twilight")]
|
||||
@@ -104,12 +108,11 @@ impl Songbird {
|
||||
U: Into<UserId>,
|
||||
{
|
||||
Self {
|
||||
client_data: PRwLock::new(ClientData {
|
||||
client_data: OnceCell::with_value(ClientData {
|
||||
shard_count: cluster.config().shard_scheme().total(),
|
||||
initialised: true,
|
||||
user_id: user_id.into(),
|
||||
}),
|
||||
calls: Default::default(),
|
||||
calls: DashMap::new(),
|
||||
sharder: Sharder::TwilightCluster(cluster),
|
||||
config: Some(config).into(),
|
||||
}
|
||||
@@ -122,15 +125,12 @@ impl Songbird {
|
||||
///
|
||||
/// [`::twilight`]: #method.twilight
|
||||
pub fn initialise_client_data<U: Into<UserId>>(&self, shard_count: u64, user_id: U) {
|
||||
let mut client_data = self.client_data.write();
|
||||
|
||||
if client_data.initialised {
|
||||
return;
|
||||
}
|
||||
|
||||
client_data.shard_count = shard_count;
|
||||
client_data.user_id = user_id.into();
|
||||
client_data.initialised = true;
|
||||
self.client_data
|
||||
.set(ClientData {
|
||||
shard_count,
|
||||
user_id: user_id.into(),
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
|
||||
/// Retrieves a [`Call`] for the given guild, if one already exists.
|
||||
@@ -161,8 +161,12 @@ impl Songbird {
|
||||
self.calls
|
||||
.entry(guild_id)
|
||||
.or_insert_with(|| {
|
||||
let info = self.manager_info();
|
||||
let shard = shard_id(guild_id.0, info.shard_count);
|
||||
let info = self
|
||||
.client_data
|
||||
.get()
|
||||
.expect("Manager has not been initialised");
|
||||
|
||||
let shard = shard_id(guild_id.0.get(), info.shard_count);
|
||||
let shard_handle = self
|
||||
.sharder
|
||||
.get_shard(shard)
|
||||
@@ -192,13 +196,7 @@ impl Songbird {
|
||||
*config = Some(new_config);
|
||||
}
|
||||
|
||||
fn manager_info(&self) -> ClientData {
|
||||
let client_data = self.client_data.write();
|
||||
|
||||
*client_data
|
||||
}
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
/// Connects to a target by retrieving its relevant [`Call`] and
|
||||
/// connecting, or creating the handler if required.
|
||||
///
|
||||
@@ -230,7 +228,7 @@ impl Songbird {
|
||||
self._join(guild_id.into(), channel_id.into()).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "driver-core")]
|
||||
#[cfg(feature = "driver")]
|
||||
async fn _join(
|
||||
&self,
|
||||
guild_id: GuildId,
|
||||
@@ -371,7 +369,11 @@ impl Songbird {
|
||||
}
|
||||
},
|
||||
TwilightEvent::VoiceStateUpdate(v) => {
|
||||
if v.0.user_id.get() != self.client_data.read().user_id.0 {
|
||||
if self
|
||||
.client_data
|
||||
.get()
|
||||
.map_or(true, |data| v.0.user_id.into_nonzero() != data.user_id.0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -390,16 +392,16 @@ impl Songbird {
|
||||
#[cfg(feature = "serenity")]
|
||||
#[async_trait]
|
||||
impl VoiceGatewayManager for Songbird {
|
||||
async fn initialise(&self, shard_count: u64, user_id: SerenityUser) {
|
||||
async fn initialise(&self, shard_count: u32, user_id: SerenityUser) {
|
||||
debug!(
|
||||
"Initialising Songbird for Serenity: ID {:?}, {} Shards",
|
||||
user_id, shard_count
|
||||
);
|
||||
self.initialise_client_data(shard_count, user_id);
|
||||
self.initialise_client_data(shard_count as u64, user_id);
|
||||
debug!("Songbird ({:?}) Initialised!", user_id);
|
||||
}
|
||||
|
||||
async fn register_shard(&self, shard_id: u64, sender: Sender<InterMessage>) {
|
||||
async fn register_shard(&self, shard_id: u32, sender: Sender<InterMessage>) {
|
||||
debug!(
|
||||
"Registering Serenity shard handle {} with Songbird",
|
||||
shard_id
|
||||
@@ -408,7 +410,7 @@ impl VoiceGatewayManager for Songbird {
|
||||
debug!("Registered shard handle {}.", shard_id);
|
||||
}
|
||||
|
||||
async fn deregister_shard(&self, shard_id: u64) {
|
||||
async fn deregister_shard(&self, shard_id: u32) {
|
||||
debug!(
|
||||
"Deregistering Serenity shard handle {} with Songbird",
|
||||
shard_id
|
||||
@@ -427,7 +429,11 @@ impl VoiceGatewayManager for Songbird {
|
||||
}
|
||||
|
||||
async fn state_update(&self, guild_id: SerenityGuild, voice_state: &VoiceState) {
|
||||
if voice_state.user_id.0 != self.client_data.read().user_id.0 {
|
||||
if self
|
||||
.client_data
|
||||
.get()
|
||||
.map_or(true, |data| voice_state.user_id.0 != data.user_id.0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user