mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
connect: simplify handle_player_event
This commit is contained in:
parent
2f24606180
commit
e0f11f12c9
1 changed files with 101 additions and 114 deletions
|
@ -664,16 +664,29 @@ impl SpircTask {
|
||||||
self.play_request_id = Some(play_request_id);
|
self.play_request_id = Some(play_request_id);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let is_current_track = matches! {
|
||||||
|
(event.get_play_request_id(), self.play_request_id),
|
||||||
|
(Some(event_id), Some(current_id)) if event_id == current_id
|
||||||
|
};
|
||||||
// we only process events if the play_request_id matches. If it doesn't, it is
|
// we only process events if the play_request_id matches. If it doesn't, it is
|
||||||
// an event that belongs to a previous track and only arrives now due to a race
|
// an event that belongs to a previous track and only arrives now due to a race
|
||||||
// condition. In this case we have updated the state already and don't want to
|
// condition. In this case we have updated the state already and don't want to
|
||||||
// mess with it.
|
// mess with it.
|
||||||
if let Some(play_request_id) = event.get_play_request_id() {
|
if !is_current_track {
|
||||||
if Some(play_request_id) == self.play_request_id {
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
PlayerEvent::EndOfTrack { .. } => self.handle_end_of_track().await,
|
PlayerEvent::EndOfTrack { .. } => {
|
||||||
PlayerEvent::Loading { .. } => {
|
let next_track = self
|
||||||
match self.play_status {
|
.connect_state
|
||||||
|
.repeat_track()
|
||||||
|
.then(|| self.connect_state.current_track(|t| t.uri.clone()));
|
||||||
|
|
||||||
|
self.handle_next(next_track)?
|
||||||
|
}
|
||||||
|
PlayerEvent::Loading { .. } => match self.play_status {
|
||||||
SpircPlayStatus::LoadingPlay { position_ms } => {
|
SpircPlayStatus::LoadingPlay { position_ms } => {
|
||||||
self.connect_state
|
self.connect_state
|
||||||
.update_position(position_ms, self.now_ms());
|
.update_position(position_ms, self.now_ms());
|
||||||
|
@ -688,9 +701,7 @@ impl SpircTask {
|
||||||
self.connect_state.update_position(0, self.now_ms());
|
self.connect_state.update_position(0, self.now_ms());
|
||||||
trace!("==> kPlayStatusLoading");
|
trace!("==> kPlayStatusLoading");
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
self.notify().await
|
|
||||||
}
|
|
||||||
PlayerEvent::Playing { position_ms, .. }
|
PlayerEvent::Playing { position_ms, .. }
|
||||||
| PlayerEvent::PositionCorrection { position_ms, .. }
|
| PlayerEvent::PositionCorrection { position_ms, .. }
|
||||||
| PlayerEvent::Seeked { position_ms, .. } => {
|
| PlayerEvent::Seeked { position_ms, .. } => {
|
||||||
|
@ -705,22 +716,19 @@ impl SpircTask {
|
||||||
*nominal_start_time = new_nominal_start_time;
|
*nominal_start_time = new_nominal_start_time;
|
||||||
self.connect_state
|
self.connect_state
|
||||||
.update_position(position_ms, self.now_ms());
|
.update_position(position_ms, self.now_ms());
|
||||||
self.notify().await
|
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SpircPlayStatus::LoadingPlay { .. }
|
SpircPlayStatus::LoadingPlay { .. } | SpircPlayStatus::LoadingPause { .. } => {
|
||||||
| SpircPlayStatus::LoadingPause { .. } => {
|
|
||||||
self.connect_state
|
self.connect_state
|
||||||
.update_position(position_ms, self.now_ms());
|
.update_position(position_ms, self.now_ms());
|
||||||
self.play_status = SpircPlayStatus::Playing {
|
self.play_status = SpircPlayStatus::Playing {
|
||||||
nominal_start_time: new_nominal_start_time,
|
nominal_start_time: new_nominal_start_time,
|
||||||
preloading_of_next_track_triggered: false,
|
preloading_of_next_track_triggered: false,
|
||||||
};
|
};
|
||||||
self.notify().await
|
|
||||||
}
|
}
|
||||||
_ => Ok(()),
|
_ => return Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PlayerEvent::Paused {
|
PlayerEvent::Paused {
|
||||||
|
@ -736,51 +744,40 @@ impl SpircTask {
|
||||||
position_ms: new_position_ms,
|
position_ms: new_position_ms,
|
||||||
preloading_of_next_track_triggered: false,
|
preloading_of_next_track_triggered: false,
|
||||||
};
|
};
|
||||||
self.notify().await
|
|
||||||
}
|
}
|
||||||
SpircPlayStatus::LoadingPlay { .. }
|
SpircPlayStatus::LoadingPlay { .. } | SpircPlayStatus::LoadingPause { .. } => {
|
||||||
| SpircPlayStatus::LoadingPause { .. } => {
|
|
||||||
self.connect_state
|
self.connect_state
|
||||||
.update_position(new_position_ms, self.now_ms());
|
.update_position(new_position_ms, self.now_ms());
|
||||||
self.play_status = SpircPlayStatus::Paused {
|
self.play_status = SpircPlayStatus::Paused {
|
||||||
position_ms: new_position_ms,
|
position_ms: new_position_ms,
|
||||||
preloading_of_next_track_triggered: false,
|
preloading_of_next_track_triggered: false,
|
||||||
};
|
};
|
||||||
self.notify().await
|
|
||||||
}
|
}
|
||||||
_ => Ok(()),
|
_ => return Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PlayerEvent::Stopped { .. } => {
|
PlayerEvent::Stopped { .. } => {
|
||||||
trace!("==> kPlayStatusStop");
|
trace!("==> kPlayStatusStop");
|
||||||
match self.play_status {
|
match self.play_status {
|
||||||
SpircPlayStatus::Stopped => Ok(()),
|
SpircPlayStatus::Stopped => return Ok(()),
|
||||||
_ => {
|
_ => self.play_status = SpircPlayStatus::Stopped,
|
||||||
self.play_status = SpircPlayStatus::Stopped;
|
|
||||||
self.notify().await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PlayerEvent::TimeToPreloadNextTrack { .. } => {
|
PlayerEvent::TimeToPreloadNextTrack { .. } => {
|
||||||
self.handle_preload_next_track();
|
self.handle_preload_next_track();
|
||||||
Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
PlayerEvent::Unavailable { track_id, .. } => {
|
PlayerEvent::Unavailable { track_id, .. } => {
|
||||||
self.handle_unavailable(track_id)?;
|
self.handle_unavailable(track_id)?;
|
||||||
if self.connect_state.current_track(|t| &t.uri) == &track_id.to_uri()? {
|
if self.connect_state.current_track(|t| &t.uri) == &track_id.to_uri()? {
|
||||||
self.handle_next(None)?;
|
self.handle_next(None)?
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
_ => return Ok(()),
|
||||||
|
}
|
||||||
|
|
||||||
self.notify().await
|
self.notify().await
|
||||||
}
|
}
|
||||||
_ => Ok(()),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_connection_id_update(&mut self, connection_id: String) -> Result<(), Error> {
|
async fn handle_connection_id_update(&mut self, connection_id: String) -> Result<(), Error> {
|
||||||
trace!("Received connection ID update: {:?}", connection_id);
|
trace!("Received connection ID update: {:?}", connection_id);
|
||||||
|
@ -1454,16 +1451,6 @@ impl SpircTask {
|
||||||
self.set_volume(volume);
|
self.set_volume(volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_end_of_track(&mut self) -> Result<(), Error> {
|
|
||||||
let next_track = self
|
|
||||||
.connect_state
|
|
||||||
.repeat_track()
|
|
||||||
.then(|| self.connect_state.current_track(|t| t.uri.clone()));
|
|
||||||
|
|
||||||
self.handle_next(next_track)?;
|
|
||||||
self.notify().await
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_playlist_modification(
|
fn handle_playlist_modification(
|
||||||
&mut self,
|
&mut self,
|
||||||
playlist_modification_info: PlaylistModificationInfo,
|
playlist_modification_info: PlaylistModificationInfo,
|
||||||
|
|
Loading…
Reference in a new issue