Handle unplayable tracks during prefetch

This commit is contained in:
ashthespy 2020-05-10 14:31:43 +02:00
parent b63199743a
commit 902440925d
2 changed files with 64 additions and 24 deletions

View file

@ -621,24 +621,26 @@ impl SpircTask {
self.play_status = SpircPlayStatus::Stopped; self.play_status = SpircPlayStatus::Stopped;
} }
}, },
PlayerEvent::TimeToPreloadNextTrack { .. } => match self.play_status { PlayerEvent::TimeToPreloadNextTrack { preload_index, .. } => {
SpircPlayStatus::Paused { match self.play_status {
ref mut preloading_of_next_track_triggered, SpircPlayStatus::Paused {
.. ref mut preloading_of_next_track_triggered,
} ..
| SpircPlayStatus::Playing {
ref mut preloading_of_next_track_triggered,
..
} => {
*preloading_of_next_track_triggered = true;
if let Some(track_id) = self.preview_next_track() {
self.player.preload(track_id);
} }
| SpircPlayStatus::Playing {
ref mut preloading_of_next_track_triggered,
..
} => {
*preloading_of_next_track_triggered = true;
if let Some(track_id) = self.preview_next_track(preload_index) {
self.player.preload(track_id);
}
}
SpircPlayStatus::LoadingPause { .. }
| SpircPlayStatus::LoadingPlay { .. }
| SpircPlayStatus::Stopped => (),
} }
SpircPlayStatus::LoadingPause { .. } }
| SpircPlayStatus::LoadingPlay { .. }
| SpircPlayStatus::Stopped => (),
},
_ => (), _ => (),
} }
} }
@ -777,7 +779,8 @@ impl SpircTask {
} = self.play_status } = self.play_status
{ {
if preloading_of_next_track_triggered { if preloading_of_next_track_triggered {
if let Some(track_id) = self.preview_next_track() { // Get the next track_id in the playlist
if let Some(track_id) = self.preview_next_track(1) {
self.player.preload(track_id); self.player.preload(track_id);
} }
} }
@ -899,9 +902,12 @@ impl SpircTask {
} }
} }
fn preview_next_track(&mut self) -> Option<SpotifyId> { fn preview_next_track(&mut self, preview_index: u32) -> Option<SpotifyId> {
self.get_track_id_to_play_from_playlist(self.state.get_playing_track_index() + 1) trace!("Previewing {:}", preview_index);
.and_then(|(track_id, _)| Some(track_id)) self.get_track_id_to_play_from_playlist(
self.state.get_playing_track_index() + preview_index,
)
.and_then(|(track_id, _)| Some(track_id))
} }
fn handle_next(&mut self) { fn handle_next(&mut self) {

View file

@ -99,6 +99,7 @@ pub enum PlayerEvent {
TimeToPreloadNextTrack { TimeToPreloadNextTrack {
play_request_id: u64, play_request_id: u64,
track_id: SpotifyId, track_id: SpotifyId,
preload_index: u32,
}, },
EndOfTrack { EndOfTrack {
play_request_id: u64, play_request_id: u64,
@ -320,6 +321,10 @@ enum PlayerPreload {
Loading { Loading {
track_id: SpotifyId, track_id: SpotifyId,
loader: Box<dyn Future<Item = PlayerLoadedTrackData, Error = ()>>, loader: Box<dyn Future<Item = PlayerLoadedTrackData, Error = ()>>,
preload_index: u32,
},
Missing {
preload_index: u32,
}, },
Ready { Ready {
track_id: SpotifyId, track_id: SpotifyId,
@ -753,6 +758,7 @@ impl Future for PlayerInternal {
if let PlayerPreload::Loading { if let PlayerPreload::Loading {
ref mut loader, ref mut loader,
track_id, track_id,
preload_index,
} = self.preload } = self.preload
{ {
match loader.poll() { match loader.poll() {
@ -764,8 +770,26 @@ impl Future for PlayerInternal {
} }
Ok(Async::NotReady) => (), Ok(Async::NotReady) => (),
Err(_) => { Err(_) => {
warn!("Unable to preload {:?}", track_id); warn!("Unable to preload {:?}[{}]", track_id, preload_index,);
self.preload = PlayerPreload::None; // Preemptively fetch next track?
self.preload = PlayerPreload::Missing { preload_index };
if let PlayerState::Playing {
play_request_id, ..
}
| PlayerState::Paused {
play_request_id, ..
} = self.state
{
debug!(
"Requesting track_id for preload_index: {}",
preload_index + 1
);
self.send_event(PlayerEvent::TimeToPreloadNextTrack {
track_id,
play_request_id,
preload_index: preload_index + 1,
});
}
} }
} }
} }
@ -853,6 +877,7 @@ impl Future for PlayerInternal {
self.send_event(PlayerEvent::TimeToPreloadNextTrack { self.send_event(PlayerEvent::TimeToPreloadNextTrack {
track_id, track_id,
play_request_id, play_request_id,
preload_index: 1,
}); });
} }
} }
@ -1294,7 +1319,12 @@ impl PlayerInternal {
fn handle_command_preload(&mut self, track_id: SpotifyId) { fn handle_command_preload(&mut self, track_id: SpotifyId) {
debug!("Preloading track"); debug!("Preloading track");
let mut preload_track = true; let mut preload_track = true;
let preload_index = match self.preload {
PlayerPreload::Loading { preload_index, .. } => preload_index,
// The last preload was missing, so increment it
PlayerPreload::Missing { preload_index, .. } => preload_index + 1,
_ => 1,
};
// check whether the track is already loaded somewhere or being loaded. // check whether the track is already loaded somewhere or being loaded.
if let PlayerPreload::Loading { if let PlayerPreload::Loading {
track_id: currently_loading, track_id: currently_loading,
@ -1336,7 +1366,11 @@ impl PlayerInternal {
// schedule the preload if the current track if desired. // schedule the preload if the current track if desired.
if preload_track { if preload_track {
let loader = self.load_track(track_id, 0); let loader = self.load_track(track_id, 0);
self.preload = PlayerPreload::Loading { track_id, loader } self.preload = PlayerPreload::Loading {
track_id,
loader,
preload_index,
}
} }
} }