Files
songbird/src/id.rs
Kyle Simpson 8cc7a22b0b 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>
2023-11-19 23:58:34 +00:00

124 lines
2.7 KiB
Rust

//! Newtypes around Discord IDs for library cross-compatibility.
#[cfg(feature = "driver")]
use crate::model::id::{GuildId as DriverGuild, UserId as DriverUser};
#[cfg(feature = "serenity")]
use serenity::model::id::{
ChannelId as SerenityChannel,
GuildId as SerenityGuild,
UserId as SerenityUser,
};
use std::{
fmt::{Display, Formatter, Result as FmtResult},
num::NonZeroU64,
};
#[cfg(feature = "twilight")]
use twilight_model::id::{
marker::{ChannelMarker, GuildMarker, UserMarker},
Id as TwilightId,
};
/// ID of a Discord voice/text channel.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ChannelId(pub NonZeroU64);
/// ID of a Discord guild (colloquially, "server").
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct GuildId(pub NonZeroU64);
/// ID of a Discord user.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct UserId(pub NonZeroU64);
impl Display for ChannelId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Display::fmt(&self.0, f)
}
}
impl From<NonZeroU64> for ChannelId {
fn from(id: NonZeroU64) -> Self {
Self(id)
}
}
#[cfg(feature = "serenity")]
impl From<SerenityChannel> for ChannelId {
fn from(id: SerenityChannel) -> Self {
Self(id.0)
}
}
#[cfg(feature = "twilight")]
impl From<TwilightId<ChannelMarker>> for ChannelId {
fn from(id: TwilightId<ChannelMarker>) -> Self {
Self(id.into_nonzero())
}
}
impl Display for GuildId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Display::fmt(&self.0, f)
}
}
impl From<NonZeroU64> for GuildId {
fn from(id: NonZeroU64) -> Self {
Self(id)
}
}
#[cfg(feature = "serenity")]
impl From<SerenityGuild> for GuildId {
fn from(id: SerenityGuild) -> Self {
Self(id.0)
}
}
#[cfg(feature = "driver")]
impl From<GuildId> for DriverGuild {
fn from(id: GuildId) -> Self {
Self(id.0.get())
}
}
#[cfg(feature = "twilight")]
impl From<TwilightId<GuildMarker>> for GuildId {
fn from(id: TwilightId<GuildMarker>) -> Self {
Self(id.into_nonzero())
}
}
impl Display for UserId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Display::fmt(&self.0, f)
}
}
impl From<NonZeroU64> for UserId {
fn from(id: NonZeroU64) -> Self {
Self(id)
}
}
#[cfg(feature = "serenity")]
impl From<SerenityUser> for UserId {
fn from(id: SerenityUser) -> Self {
Self(id.0)
}
}
#[cfg(feature = "driver")]
impl From<UserId> for DriverUser {
fn from(id: UserId) -> Self {
Self(id.0.get())
}
}
#[cfg(feature = "twilight")]
impl From<TwilightId<UserMarker>> for UserId {
fn from(id: TwilightId<UserMarker>) -> Self {
Self(id.into_nonzero())
}
}