1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-03 01:39:28 +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,
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);
@ -106,6 +112,7 @@ enum PlayerCommand {
SetSession(Session),
AddEventSender(mpsc::UnboundedSender<PlayerEvent>),
SetSinkEventCallback(Option<SinkEventCallback>),
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<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) {
self.command(PlayerCommand::EmitVolumeChangedEvent(volume));
}
@ -1349,16 +1362,18 @@ impl Future for PlayerInternal {
});
}
if let Some(progress_callback) =
self.progress_callback.as_mut()
{
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,
});
.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)

View file

@ -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,