From 85d6d263e1a3ee63a532d6ed184bc21a059f5ccd Mon Sep 17 00:00:00 2001 From: chmanie Date: Thu, 8 Jun 2023 12:17:11 +0200 Subject: [PATCH] Add more player API methods --- Cargo.lock | 1 + audio-player/Cargo.toml | 1 + audio-player/src/lib.rs | 41 ++++++++++++++++++++++++++++--- audio-player/src/player_engine.rs | 15 +++++------ 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d6855a5..31f1f2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,6 +179,7 @@ dependencies = [ "rodio", "stream-download", "symphonia", + "thiserror", "url", ] diff --git a/audio-player/Cargo.toml b/audio-player/Cargo.toml index 77877bf..c0ea07f 100644 --- a/audio-player/Cargo.toml +++ b/audio-player/Cargo.toml @@ -10,3 +10,4 @@ stream-download = { git = "https://github.com/aschey/stream-download-rs.git" } anyhow = "1.0.71" url = "2.4.0" flume = "0.10.14" +thiserror = "1.0.40" diff --git a/audio-player/src/lib.rs b/audio-player/src/lib.rs index 52e66d5..aca683d 100644 --- a/audio-player/src/lib.rs +++ b/audio-player/src/lib.rs @@ -15,6 +15,8 @@ use player_engine::{PlayerEngine, PlayerEngineCommand}; // * Emit buffering // * Emit errors +pub enum PlayerError {} + pub struct Player { pub messages: Receiver, tx_engine: Sender, @@ -32,8 +34,9 @@ impl Default for Player { let mut player = PlayerEngine::new(tx_decoder, tx_player); loop { match rx_engine.recv() { - Ok(PlayerEngineCommand::Play(source_str)) => { - player.play(&source_str); + Ok(PlayerEngineCommand::Play(source_str, tx)) => { + let res = player.play(&source_str); + tx.send(res); } Ok(PlayerEngineCommand::Pause) => { player.pause(); @@ -67,9 +70,34 @@ impl Default for Player { impl Player { // FIXME: this could check if the player started playing using a channel // Then it would be async (wait for Playing for example) - pub async fn play(&self, source_str: &str) -> Result<()> { + pub async fn play(&self, source_str: &str) -> Result { + let (tx, rx) = flume::bounded(1); self.tx_engine - .send(PlayerEngineCommand::Play(source_str.to_string())); + .send(PlayerEngineCommand::Play(source_str.to_string(), tx)); + if let Ok(res) = rx.recv_async().await { + return res; + } + // FIXME: add error type + Err(anyhow!("Player channel error")) + } + + pub async fn elpased(&self) -> Duration { + // FIXME: implement + Duration::default() + } + + pub async fn duration(&self) -> Duration { + // FIXME: implement + Duration::default() + } + + pub async fn volume(&self) -> f32 { + // FIXME: implement + 0.0 + } + + pub async fn set_volume(&self) -> Result<()> { + // FIXME: implement Ok(()) } @@ -92,4 +120,9 @@ impl Player { self.tx_engine.send(PlayerEngineCommand::Stop); Ok(()) } + + pub async fn restart(&self) -> Result<()> { + // FIXME: implement + Ok(()) + } } diff --git a/audio-player/src/player_engine.rs b/audio-player/src/player_engine.rs index 9abee90..325fbe0 100644 --- a/audio-player/src/player_engine.rs +++ b/audio-player/src/player_engine.rs @@ -17,7 +17,7 @@ use symphonia::core::io::{ }; pub enum PlayerEngineCommand { - Play(String), + Play(String, Sender>), Pause, Unpause, TogglePlay, @@ -56,26 +56,23 @@ impl PlayerEngine { } } - pub fn play(&mut self, source_str: &str) -> Result<()> { + pub fn play(&mut self, source_str: &str) -> Result { + let tx_player = self.tx_player.clone(); + let (stream, handle) = OutputStream::try_default()?; let mut sink = Sink::try_new(&handle)?; let (source, hint) = self.get_source(source_str)?; let mss = MediaSourceStream::new(source, MediaSourceStreamOptions::default()); - - let tx_player = self.tx_player.clone(); - let decoder = SymphoniaDecoder::new(mss, hint, self.tx_engine.clone())?; - let media_info = decoder.media_info(); + tx_player.send(PlayerMessage::Duration( media_info.duration.unwrap_or_default(), )); - // tx_player.send(PlayerEngineMessage::MediaInfo(media_info)); let decoder = decoder.periodic_access(Duration::from_millis(250), move |src| { tx_player.send(PlayerMessage::Elapsed(src.elapsed())); }); - sink.append(decoder); // We need to keep the stream around, otherwise it gets dropped outside of this scope @@ -85,7 +82,7 @@ impl PlayerEngine { self.tx_player.send(PlayerMessage::Playing); - Ok(()) + Ok(media_info) } pub fn pause(&mut self) {