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:
251
src/driver/test_config.rs
Normal file
251
src/driver/test_config.rs
Normal file
@@ -0,0 +1,251 @@
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use flume::{Receiver, Sender};
|
||||
|
||||
use crate::{
|
||||
tracks::{PlayMode, TrackHandle, TrackState},
|
||||
Event,
|
||||
EventContext,
|
||||
EventHandler,
|
||||
TrackEvent,
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TickStyle {
|
||||
Timed,
|
||||
UntimedWithExecLimit(Receiver<u64>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum OutputMessage {
|
||||
Passthrough(Vec<u8>),
|
||||
Mixed(Vec<f32>),
|
||||
Silent,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl OutputMessage {
|
||||
pub fn is_passthrough(&self) -> bool {
|
||||
matches!(self, Self::Passthrough(_))
|
||||
}
|
||||
|
||||
pub fn is_mixed(&self) -> bool {
|
||||
matches!(self, Self::Mixed(_))
|
||||
}
|
||||
|
||||
pub fn is_mixed_with_nonzero_signal(&self) -> bool {
|
||||
if let Self::Mixed(data) = self {
|
||||
data.iter().any(|v| *v != 0.0f32)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_explicit_silence(&self) -> bool {
|
||||
*self == Self::Silent
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum OutputMode {
|
||||
Raw(Sender<TickMessage<OutputMessage>>),
|
||||
Rtp(Sender<TickMessage<Vec<u8>>>),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TickMessage<T> {
|
||||
El(T),
|
||||
NoEl,
|
||||
}
|
||||
|
||||
impl<T> From<T> for TickMessage<T> {
|
||||
fn from(val: T) -> Self {
|
||||
TickMessage::El(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TickMessage<OutputMessage>> for OutputPacket {
|
||||
fn from(val: TickMessage<OutputMessage>) -> Self {
|
||||
match val {
|
||||
TickMessage::El(e) => OutputPacket::Raw(e),
|
||||
TickMessage::NoEl => OutputPacket::Empty,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TickMessage<Vec<u8>>> for OutputPacket {
|
||||
fn from(val: TickMessage<Vec<u8>>) -> Self {
|
||||
match val {
|
||||
TickMessage::El(e) => OutputPacket::Rtp(e),
|
||||
TickMessage::NoEl => OutputPacket::Empty,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum OutputPacket {
|
||||
Raw(OutputMessage),
|
||||
Rtp(Vec<u8>),
|
||||
Empty,
|
||||
}
|
||||
|
||||
impl OutputPacket {
|
||||
pub fn raw(&self) -> Option<&OutputMessage> {
|
||||
if let Self::Raw(o) = self {
|
||||
Some(o)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum OutputReceiver {
|
||||
Raw(Receiver<TickMessage<OutputMessage>>),
|
||||
Rtp(Receiver<TickMessage<Vec<u8>>>),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DriverTestHandle {
|
||||
pub rx: OutputReceiver,
|
||||
pub tx: Sender<u64>,
|
||||
}
|
||||
|
||||
impl DriverTestHandle {
|
||||
pub fn recv(&self) -> OutputPacket {
|
||||
match &self.rx {
|
||||
OutputReceiver::Raw(rx) => rx.recv().unwrap().into(),
|
||||
OutputReceiver::Rtp(rx) => rx.recv().unwrap().into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn recv_async(&self) -> OutputPacket {
|
||||
match &self.rx {
|
||||
OutputReceiver::Raw(rx) => rx.recv_async().await.unwrap().into(),
|
||||
OutputReceiver::Rtp(rx) => rx.recv_async().await.unwrap().into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
match &self.rx {
|
||||
OutputReceiver::Raw(rx) => rx.len(),
|
||||
OutputReceiver::Rtp(rx) => rx.len(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wait(&self, n_ticks: u64) {
|
||||
for _i in 0..n_ticks {
|
||||
drop(self.recv());
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn wait_async(&self, n_ticks: u64) {
|
||||
for _i in 0..n_ticks {
|
||||
drop(self.recv_async().await);
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn spawn_ticker(&self) {
|
||||
let remote = self.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
remote.skip(1).await;
|
||||
tokio::time::sleep(Duration::from_millis(1)).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn wait_noisy(&self, n_ticks: u64) {
|
||||
for _i in 0..n_ticks {
|
||||
match self.recv() {
|
||||
OutputPacket::Empty => eprintln!("pkt: Nothing"),
|
||||
OutputPacket::Rtp(p) => eprintln!("pkt: RTP[{}B]", p.len()),
|
||||
OutputPacket::Raw(OutputMessage::Silent) => eprintln!("pkt: Raw-Silent"),
|
||||
OutputPacket::Raw(OutputMessage::Passthrough(p)) =>
|
||||
eprintln!("pkt: Raw-Passthrough[{}B]", p.len()),
|
||||
OutputPacket::Raw(OutputMessage::Mixed(p)) =>
|
||||
eprintln!("pkt: Raw-Mixed[{}B]", p.len()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn skip(&self, n_ticks: u64) {
|
||||
self.tick(n_ticks);
|
||||
self.wait_async(n_ticks).await;
|
||||
}
|
||||
|
||||
pub fn tick(&self, n_ticks: u64) {
|
||||
if n_ticks == 0 {
|
||||
panic!("Number of ticks to advance driver/mixer must be >= 1.");
|
||||
}
|
||||
self.tx.send(n_ticks).unwrap();
|
||||
}
|
||||
|
||||
pub async fn ready_track(
|
||||
&self,
|
||||
handle: &TrackHandle,
|
||||
tick_wait: Option<Duration>,
|
||||
) -> TrackState {
|
||||
let (tx, rx) = flume::bounded(1);
|
||||
let (err_tx, err_rx) = flume::bounded(1);
|
||||
|
||||
struct SongPlayable {
|
||||
tx: Sender<TrackState>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl EventHandler for SongPlayable {
|
||||
async fn act(&self, ctx: &crate::EventContext<'_>) -> Option<Event> {
|
||||
if let EventContext::Track(&[(state, _)]) = ctx {
|
||||
drop(self.tx.send(state.clone()));
|
||||
}
|
||||
|
||||
Some(Event::Cancel)
|
||||
}
|
||||
}
|
||||
|
||||
struct SongErred {
|
||||
tx: Sender<PlayMode>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl EventHandler for SongErred {
|
||||
async fn act(&self, ctx: &crate::EventContext<'_>) -> Option<Event> {
|
||||
if let EventContext::Track(&[(state, _)]) = ctx {
|
||||
drop(self.tx.send(state.playing.clone()));
|
||||
}
|
||||
|
||||
Some(Event::Cancel)
|
||||
}
|
||||
}
|
||||
|
||||
handle
|
||||
.add_event(Event::Track(TrackEvent::Playable), SongPlayable { tx })
|
||||
.expect("Adding track evt should not fail before any ticks.");
|
||||
|
||||
handle
|
||||
.add_event(Event::Track(TrackEvent::Error), SongErred { tx: err_tx })
|
||||
.expect("Adding track evt should not fail before any ticks.");
|
||||
|
||||
loop {
|
||||
self.tick(1);
|
||||
tokio::time::sleep(tick_wait.unwrap_or_else(|| Duration::from_millis(20))).await;
|
||||
self.wait_async(1).await;
|
||||
|
||||
match err_rx.try_recv() {
|
||||
Ok(e) => panic!("Error reported on track: {:?}", e),
|
||||
Err(flume::TryRecvError::Empty | flume::TryRecvError::Disconnected) => {},
|
||||
}
|
||||
|
||||
match rx.try_recv() {
|
||||
Ok(val) => return val,
|
||||
Err(flume::TryRecvError::Disconnected) => panic!(),
|
||||
Err(flume::TryRecvError::Empty) => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user