connect: simplify handle_player_event

This commit is contained in:
Felix Prillwitz 2024-12-10 18:03:23 +01:00
parent 2f24606180
commit e0f11f12c9
No known key found for this signature in database
GPG key ID: DE334B43606D1455

View file

@ -664,122 +664,119 @@ 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 { }
PlayerEvent::EndOfTrack { .. } => self.handle_end_of_track().await,
PlayerEvent::Loading { .. } => { match event {
match self.play_status { PlayerEvent::EndOfTrack { .. } => {
SpircPlayStatus::LoadingPlay { position_ms } => { let next_track = self
self.connect_state .connect_state
.update_position(position_ms, self.now_ms()); .repeat_track()
trace!("==> kPlayStatusPlay"); .then(|| self.connect_state.current_track(|t| t.uri.clone()));
}
SpircPlayStatus::LoadingPause { position_ms } => { self.handle_next(next_track)?
self.connect_state }
.update_position(position_ms, self.now_ms()); PlayerEvent::Loading { .. } => match self.play_status {
trace!("==> kPlayStatusPause"); SpircPlayStatus::LoadingPlay { position_ms } => {
} self.connect_state
_ => { .update_position(position_ms, self.now_ms());
self.connect_state.update_position(0, self.now_ms()); trace!("==> kPlayStatusPlay");
trace!("==> kPlayStatusLoading"); }
} SpircPlayStatus::LoadingPause { position_ms } => {
} self.connect_state
self.notify().await .update_position(position_ms, self.now_ms());
} trace!("==> kPlayStatusPause");
PlayerEvent::Playing { position_ms, .. } }
| PlayerEvent::PositionCorrection { position_ms, .. } _ => {
| PlayerEvent::Seeked { position_ms, .. } => { self.connect_state.update_position(0, self.now_ms());
trace!("==> kPlayStatusPlay"); trace!("==> kPlayStatusLoading");
let new_nominal_start_time = self.now_ms() - position_ms as i64; }
match self.play_status { },
SpircPlayStatus::Playing { PlayerEvent::Playing { position_ms, .. }
ref mut nominal_start_time, | PlayerEvent::PositionCorrection { position_ms, .. }
.. | PlayerEvent::Seeked { position_ms, .. } => {
} => { trace!("==> kPlayStatusPlay");
if (*nominal_start_time - new_nominal_start_time).abs() > 100 { let new_nominal_start_time = self.now_ms() - position_ms as i64;
*nominal_start_time = new_nominal_start_time; match self.play_status {
self.connect_state SpircPlayStatus::Playing {
.update_position(position_ms, self.now_ms()); ref mut nominal_start_time,
self.notify().await
} else {
Ok(())
}
}
SpircPlayStatus::LoadingPlay { .. }
| SpircPlayStatus::LoadingPause { .. } => {
self.connect_state
.update_position(position_ms, self.now_ms());
self.play_status = SpircPlayStatus::Playing {
nominal_start_time: new_nominal_start_time,
preloading_of_next_track_triggered: false,
};
self.notify().await
}
_ => Ok(()),
}
}
PlayerEvent::Paused {
position_ms: new_position_ms,
.. ..
} => { } => {
trace!("==> kPlayStatusPause"); if (*nominal_start_time - new_nominal_start_time).abs() > 100 {
match self.play_status { *nominal_start_time = new_nominal_start_time;
SpircPlayStatus::Paused { .. } | SpircPlayStatus::Playing { .. } => { self.connect_state
self.connect_state .update_position(position_ms, self.now_ms());
.update_position(new_position_ms, self.now_ms()); } else {
self.play_status = SpircPlayStatus::Paused { return Ok(());
position_ms: new_position_ms,
preloading_of_next_track_triggered: false,
};
self.notify().await
}
SpircPlayStatus::LoadingPlay { .. }
| SpircPlayStatus::LoadingPause { .. } => {
self.connect_state
.update_position(new_position_ms, self.now_ms());
self.play_status = SpircPlayStatus::Paused {
position_ms: new_position_ms,
preloading_of_next_track_triggered: false,
};
self.notify().await
}
_ => Ok(()),
} }
} }
PlayerEvent::Stopped { .. } => { SpircPlayStatus::LoadingPlay { .. } | SpircPlayStatus::LoadingPause { .. } => {
trace!("==> kPlayStatusStop"); self.connect_state
match self.play_status { .update_position(position_ms, self.now_ms());
SpircPlayStatus::Stopped => Ok(()), self.play_status = SpircPlayStatus::Playing {
_ => { nominal_start_time: new_nominal_start_time,
self.play_status = SpircPlayStatus::Stopped; preloading_of_next_track_triggered: false,
self.notify().await };
}
}
} }
PlayerEvent::TimeToPreloadNextTrack { .. } => { _ => return Ok(()),
self.handle_preload_next_track();
Ok(())
}
PlayerEvent::Unavailable { track_id, .. } => {
self.handle_unavailable(track_id)?;
if self.connect_state.current_track(|t| &t.uri) == &track_id.to_uri()? {
self.handle_next(None)?;
}
self.notify().await
}
_ => Ok(()),
} }
} else {
Ok(())
} }
} else { PlayerEvent::Paused {
Ok(()) position_ms: new_position_ms,
..
} => {
trace!("==> kPlayStatusPause");
match self.play_status {
SpircPlayStatus::Paused { .. } | SpircPlayStatus::Playing { .. } => {
self.connect_state
.update_position(new_position_ms, self.now_ms());
self.play_status = SpircPlayStatus::Paused {
position_ms: new_position_ms,
preloading_of_next_track_triggered: false,
};
}
SpircPlayStatus::LoadingPlay { .. } | SpircPlayStatus::LoadingPause { .. } => {
self.connect_state
.update_position(new_position_ms, self.now_ms());
self.play_status = SpircPlayStatus::Paused {
position_ms: new_position_ms,
preloading_of_next_track_triggered: false,
};
}
_ => return Ok(()),
}
}
PlayerEvent::Stopped { .. } => {
trace!("==> kPlayStatusStop");
match self.play_status {
SpircPlayStatus::Stopped => return Ok(()),
_ => self.play_status = SpircPlayStatus::Stopped,
}
}
PlayerEvent::TimeToPreloadNextTrack { .. } => {
self.handle_preload_next_track();
return Ok(());
}
PlayerEvent::Unavailable { track_id, .. } => {
self.handle_unavailable(track_id)?;
if self.connect_state.current_track(|t| &t.uri) == &track_id.to_uri()? {
self.handle_next(None)?
}
}
_ => return Ok(()),
} }
self.notify().await
} }
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> {
@ -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,