1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-03 09:49:31 +02:00

Replaced PlayerEvent::PositionChanged with set_progress_callback() method

This commit is contained in:
fragsalat 2025-04-29 21:43:22 +02:00
parent 84c68497aa
commit f26e3de07b
2 changed files with 36 additions and 32 deletions

View file

@ -85,7 +85,13 @@ struct PlayerInternal {
player_id: usize, player_id: usize,
play_request_id_generator: SeqGenerator<u64>, play_request_id_generator: SeqGenerator<u64>,
last_progress_update: Instant, progress_callback: Option<ProgressCallback>,
}
pub struct ProgressCallback {
pub update_interval: Duration,
pub last_update: Instant,
pub callback: Box<dyn Fn(u32) + Send>,
} }
static PLAYER_COUNTER: AtomicUsize = AtomicUsize::new(0); static PLAYER_COUNTER: AtomicUsize = AtomicUsize::new(0);
@ -106,6 +112,7 @@ enum PlayerCommand {
SetSession(Session), SetSession(Session),
AddEventSender(mpsc::UnboundedSender<PlayerEvent>), AddEventSender(mpsc::UnboundedSender<PlayerEvent>),
SetSinkEventCallback(Option<SinkEventCallback>), SetSinkEventCallback(Option<SinkEventCallback>),
SetProgressCallback(ProgressCallback),
EmitVolumeChangedEvent(u16), EmitVolumeChangedEvent(u16),
SetAutoNormaliseAsAlbum(bool), SetAutoNormaliseAsAlbum(bool),
EmitSessionDisconnectedEvent { EmitSessionDisconnectedEvent {
@ -196,12 +203,6 @@ pub enum PlayerEvent {
track_id: SpotifyId, track_id: SpotifyId,
position_ms: u32, 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 { Seeked {
play_request_id: u64, play_request_id: u64,
track_id: SpotifyId, track_id: SpotifyId,
@ -488,7 +489,7 @@ impl Player {
player_id, player_id,
play_request_id_generator: SeqGenerator::new(0), 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. // While PlayerInternal is written as a future, it still contains blocking code.
@ -574,6 +575,18 @@ impl Player {
self.command(PlayerCommand::SetSinkEventCallback(callback)); self.command(PlayerCommand::SetSinkEventCallback(callback));
} }
pub fn set_progress_callback(
&self,
callback: Box<dyn Fn(u32) + Send>,
update_interval: Duration,
) {
self.command(PlayerCommand::SetProgressCallback(ProgressCallback {
update_interval,
last_update: Instant::now(),
callback,
}));
}
pub fn emit_volume_changed_event(&self, volume: u16) { pub fn emit_volume_changed_event(&self, volume: u16) {
self.command(PlayerCommand::EmitVolumeChangedEvent(volume)); self.command(PlayerCommand::EmitVolumeChangedEvent(volume));
} }
@ -1349,16 +1362,18 @@ impl Future for PlayerInternal {
}); });
} }
let last_progress_update_since_ms = now if let Some(progress_callback) =
.duration_since(self.last_progress_update) self.progress_callback.as_mut()
.as_millis(); {
if last_progress_update_since_ms > 250 { let last_progress_update_since_ms = now
self.last_progress_update = now; .duration_since(progress_callback.last_update);
self.send_event(PlayerEvent::PositionChanged { if last_progress_update_since_ms
play_request_id, > progress_callback.update_interval
track_id, {
position_ms: new_stream_position_ms, progress_callback.last_update = now;
}); let callback = &progress_callback.callback;
callback(new_stream_position_ms);
}
} }
} }
Err(e) => { Err(e) => {
@ -2125,6 +2140,8 @@ impl PlayerInternal {
PlayerCommand::SetSinkEventCallback(callback) => self.sink_event_callback = callback, PlayerCommand::SetSinkEventCallback(callback) => self.sink_event_callback = callback,
PlayerCommand::SetProgressCallback(callback) => self.progress_callback = Some(callback),
PlayerCommand::EmitVolumeChangedEvent(volume) => { PlayerCommand::EmitVolumeChangedEvent(volume) => {
self.send_event(PlayerEvent::VolumeChanged { volume }) self.send_event(PlayerEvent::VolumeChanged { volume })
} }
@ -2315,6 +2332,7 @@ impl fmt::Debug for PlayerCommand {
PlayerCommand::SetSinkEventCallback(_) => { PlayerCommand::SetSinkEventCallback(_) => {
f.debug_tuple("SetSinkEventCallback").finish() f.debug_tuple("SetSinkEventCallback").finish()
} }
PlayerCommand::SetProgressCallback(_) => f.debug_tuple("SetProgressCallback").finish(),
PlayerCommand::EmitVolumeChangedEvent(volume) => f PlayerCommand::EmitVolumeChangedEvent(volume) => f
.debug_tuple("EmitVolumeChangedEvent") .debug_tuple("EmitVolumeChangedEvent")
.field(&volume) .field(&volume)

View file

@ -194,20 +194,6 @@ impl EventHandler {
env_vars.insert("POSITION_MS", position_ms.to_string()); 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 { PlayerEvent::SessionConnected {
connection_id, connection_id,
user_name, user_name,