From f26e3de07b667fd764416a79ae682882063e0688 Mon Sep 17 00:00:00 2001 From: fragsalat Date: Tue, 29 Apr 2025 21:43:22 +0200 Subject: [PATCH] Replaced PlayerEvent::PositionChanged with set_progress_callback() method --- playback/src/player.rs | 54 ++++++++++++++++++++++++------------- src/player_event_handler.rs | 14 ---------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/playback/src/player.rs b/playback/src/player.rs index d277fe25..09984beb 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -85,7 +85,13 @@ struct PlayerInternal { player_id: usize, play_request_id_generator: SeqGenerator, - last_progress_update: Instant, + progress_callback: Option, +} + +pub struct ProgressCallback { + pub update_interval: Duration, + pub last_update: Instant, + pub callback: Box, } static PLAYER_COUNTER: AtomicUsize = AtomicUsize::new(0); @@ -106,6 +112,7 @@ enum PlayerCommand { SetSession(Session), AddEventSender(mpsc::UnboundedSender), SetSinkEventCallback(Option), + SetProgressCallback(ProgressCallback), EmitVolumeChangedEvent(u16), SetAutoNormaliseAsAlbum(bool), EmitSessionDisconnectedEvent { @@ -196,12 +203,6 @@ pub enum PlayerEvent { track_id: SpotifyId, position_ms: u32, }, - // Will be sent periodically while playing the track to inform about the current playback position - PositionChanged { - play_request_id: u64, - track_id: SpotifyId, - position_ms: u32, - }, Seeked { play_request_id: u64, track_id: SpotifyId, @@ -488,7 +489,7 @@ impl Player { player_id, play_request_id_generator: SeqGenerator::new(0), - last_progress_update: Instant::now(), + progress_callback: None, }; // While PlayerInternal is written as a future, it still contains blocking code. @@ -574,6 +575,18 @@ impl Player { self.command(PlayerCommand::SetSinkEventCallback(callback)); } + pub fn set_progress_callback( + &self, + callback: Box, + update_interval: Duration, + ) { + self.command(PlayerCommand::SetProgressCallback(ProgressCallback { + update_interval, + last_update: Instant::now(), + callback, + })); + } + pub fn emit_volume_changed_event(&self, volume: u16) { self.command(PlayerCommand::EmitVolumeChangedEvent(volume)); } @@ -1349,16 +1362,18 @@ impl Future for PlayerInternal { }); } - let last_progress_update_since_ms = now - .duration_since(self.last_progress_update) - .as_millis(); - if last_progress_update_since_ms > 250 { - self.last_progress_update = now; - self.send_event(PlayerEvent::PositionChanged { - play_request_id, - track_id, - position_ms: new_stream_position_ms, - }); + if let Some(progress_callback) = + self.progress_callback.as_mut() + { + let last_progress_update_since_ms = now + .duration_since(progress_callback.last_update); + if last_progress_update_since_ms + > progress_callback.update_interval + { + progress_callback.last_update = now; + let callback = &progress_callback.callback; + callback(new_stream_position_ms); + } } } Err(e) => { @@ -2125,6 +2140,8 @@ impl PlayerInternal { PlayerCommand::SetSinkEventCallback(callback) => self.sink_event_callback = callback, + PlayerCommand::SetProgressCallback(callback) => self.progress_callback = Some(callback), + PlayerCommand::EmitVolumeChangedEvent(volume) => { self.send_event(PlayerEvent::VolumeChanged { volume }) } @@ -2315,6 +2332,7 @@ impl fmt::Debug for PlayerCommand { PlayerCommand::SetSinkEventCallback(_) => { f.debug_tuple("SetSinkEventCallback").finish() } + PlayerCommand::SetProgressCallback(_) => f.debug_tuple("SetProgressCallback").finish(), PlayerCommand::EmitVolumeChangedEvent(volume) => f .debug_tuple("EmitVolumeChangedEvent") .field(&volume) diff --git a/src/player_event_handler.rs b/src/player_event_handler.rs index e1f2ad1b..21cfe01c 100644 --- a/src/player_event_handler.rs +++ b/src/player_event_handler.rs @@ -194,20 +194,6 @@ impl EventHandler { env_vars.insert("POSITION_MS", position_ms.to_string()); } }, - PlayerEvent::PositionChanged { - track_id, - position_ms, - .. - } => match track_id.to_base62() { - Err(e) => { - warn!("PlayerEvent::PositionChanged: Invalid track id: {}", e) - } - Ok(id) => { - env_vars.insert("PLAYER_EVENT", "position_changed".to_string()); - env_vars.insert("TRACK_ID", id); - env_vars.insert("POSITION_MS", position_ms.to_string()); - } - }, PlayerEvent::SessionConnected { connection_id, user_name,