Roll back the meta data processing.

This commit is contained in:
Konstantin Seiler 2020-03-20 17:31:18 +11:00
parent b562ec6015
commit 223b8d611e
2 changed files with 151 additions and 150 deletions

View file

@ -99,14 +99,16 @@ pub enum PlayerEvent {
// after a buffer-underrun // after a buffer-underrun
Playing { Playing {
play_request_id: u64, play_request_id: u64,
track_meta_data: TrackMetaData, track_id: SpotifyId,
position_ms: u32, position_ms: u32,
duration_ms: u32,
}, },
// The player entered a paused state. // The player entered a paused state.
Paused { Paused {
play_request_id: u64, play_request_id: u64,
track_meta_data: TrackMetaData, track_id: SpotifyId,
position_ms: u32, position_ms: u32,
duration_ms: u32,
}, },
// The player thinks it's a good idea to issue a preload command for the next track now. // The player thinks it's a good idea to issue a preload command for the next track now.
// This event is intended for use within spirc. // This event is intended for use within spirc.
@ -119,7 +121,7 @@ pub enum PlayerEvent {
// which will trigger another event (e.g. Changed or Stopped) // which will trigger another event (e.g. Changed or Stopped)
EndOfTrack { EndOfTrack {
play_request_id: u64, play_request_id: u64,
track_meta_data: TrackMetaData, track_id: SpotifyId,
}, },
// The mixer volume was set to a new level. // The mixer volume was set to a new level.
VolumeSet { VolumeSet {
@ -324,19 +326,12 @@ impl Drop for Player {
} }
} }
#[derive(Debug, Clone)]
pub struct TrackMetaData {
pub track_id: SpotifyId,
pub normalisation_factor: f32,
pub duration_ms: u32,
pub bytes_per_second: usize,
pub title: String,
}
struct PlayerLoadedTrackData { struct PlayerLoadedTrackData {
track_meta_data: TrackMetaData,
decoder: Decoder, decoder: Decoder,
normalisation_factor: f32,
stream_loader_controller: StreamLoaderController, stream_loader_controller: StreamLoaderController,
bytes_per_second: usize,
duration_ms: u32,
stream_position_pcm: u64, stream_position_pcm: u64,
} }
@ -347,6 +342,7 @@ enum PlayerPreload {
loader: Box<dyn Future<Item = PlayerLoadedTrackData, Error = ()>>, loader: Box<dyn Future<Item = PlayerLoadedTrackData, Error = ()>>,
}, },
Ready { Ready {
track_id: SpotifyId,
loaded_track: PlayerLoadedTrackData, loaded_track: PlayerLoadedTrackData,
}, },
} }
@ -362,23 +358,30 @@ enum PlayerState {
loader: Box<dyn Future<Item = PlayerLoadedTrackData, Error = ()>>, loader: Box<dyn Future<Item = PlayerLoadedTrackData, Error = ()>>,
}, },
Paused { Paused {
track_meta_data: TrackMetaData, track_id: SpotifyId,
play_request_id: u64, play_request_id: u64,
decoder: Decoder, decoder: Decoder,
normalisation_factor: f32,
stream_loader_controller: StreamLoaderController, stream_loader_controller: StreamLoaderController,
bytes_per_second: usize,
duration_ms: u32,
stream_position_pcm: u64, stream_position_pcm: u64,
suggested_to_preload_next_track: bool, suggested_to_preload_next_track: bool,
}, },
Playing { Playing {
track_meta_data: TrackMetaData, track_id: SpotifyId,
play_request_id: u64, play_request_id: u64,
decoder: Decoder, decoder: Decoder,
normalisation_factor: f32,
stream_loader_controller: StreamLoaderController, stream_loader_controller: StreamLoaderController,
bytes_per_second: usize,
duration_ms: u32,
stream_position_pcm: u64, stream_position_pcm: u64,
reported_nominal_start_time: Option<Instant>, reported_nominal_start_time: Option<Instant>,
suggested_to_preload_next_track: bool, suggested_to_preload_next_track: bool,
}, },
EndOfTrack { EndOfTrack {
track_id: SpotifyId,
play_request_id: u64, play_request_id: u64,
loaded_track: PlayerLoadedTrackData, loaded_track: PlayerLoadedTrackData,
}, },
@ -437,18 +440,24 @@ impl PlayerState {
use self::PlayerState::*; use self::PlayerState::*;
match mem::replace(self, Invalid) { match mem::replace(self, Invalid) {
Playing { Playing {
track_meta_data, track_id,
play_request_id, play_request_id,
decoder, decoder,
duration_ms,
bytes_per_second,
normalisation_factor,
stream_loader_controller, stream_loader_controller,
stream_position_pcm, stream_position_pcm,
.. ..
} => { } => {
*self = EndOfTrack { *self = EndOfTrack {
track_id,
play_request_id, play_request_id,
loaded_track: PlayerLoadedTrackData { loaded_track: PlayerLoadedTrackData {
track_meta_data,
decoder, decoder,
duration_ms,
bytes_per_second,
normalisation_factor,
stream_loader_controller, stream_loader_controller,
stream_position_pcm, stream_position_pcm,
}, },
@ -462,18 +471,24 @@ impl PlayerState {
use self::PlayerState::*; use self::PlayerState::*;
match ::std::mem::replace(self, Invalid) { match ::std::mem::replace(self, Invalid) {
Paused { Paused {
track_meta_data, track_id,
play_request_id, play_request_id,
decoder, decoder,
normalisation_factor,
stream_loader_controller, stream_loader_controller,
duration_ms,
bytes_per_second,
stream_position_pcm, stream_position_pcm,
suggested_to_preload_next_track, suggested_to_preload_next_track,
} => { } => {
*self = Playing { *self = Playing {
track_meta_data, track_id,
play_request_id, play_request_id,
decoder, decoder,
normalisation_factor,
stream_loader_controller, stream_loader_controller,
duration_ms,
bytes_per_second,
stream_position_pcm, stream_position_pcm,
reported_nominal_start_time: None, reported_nominal_start_time: None,
suggested_to_preload_next_track, suggested_to_preload_next_track,
@ -487,19 +502,25 @@ impl PlayerState {
use self::PlayerState::*; use self::PlayerState::*;
match ::std::mem::replace(self, Invalid) { match ::std::mem::replace(self, Invalid) {
Playing { Playing {
track_meta_data, track_id,
play_request_id, play_request_id,
decoder, decoder,
normalisation_factor,
stream_loader_controller, stream_loader_controller,
duration_ms,
bytes_per_second,
stream_position_pcm, stream_position_pcm,
reported_nominal_start_time: _, reported_nominal_start_time: _,
suggested_to_preload_next_track, suggested_to_preload_next_track,
} => { } => {
*self = Paused { *self = Paused {
track_meta_data, track_id,
play_request_id, play_request_id,
decoder, decoder,
normalisation_factor,
stream_loader_controller, stream_loader_controller,
duration_ms,
bytes_per_second,
stream_position_pcm, stream_position_pcm,
suggested_to_preload_next_track, suggested_to_preload_next_track,
}; };
@ -669,15 +690,11 @@ impl PlayerTrackLoader {
let stream_position_pcm = PlayerInternal::position_ms_to_pcm(position_ms); let stream_position_pcm = PlayerInternal::position_ms_to_pcm(position_ms);
info!("<{}> ({} ms) loaded", audio.name, audio.duration); info!("<{}> ({} ms) loaded", audio.name, audio.duration);
Some(PlayerLoadedTrackData { Some(PlayerLoadedTrackData {
track_meta_data: TrackMetaData {
track_id: spotify_id,
normalisation_factor,
bytes_per_second,
duration_ms,
title: audio.name.clone(),
},
decoder, decoder,
normalisation_factor,
stream_loader_controller, stream_loader_controller,
bytes_per_second,
duration_ms,
stream_position_pcm, stream_position_pcm,
}) })
} }
@ -712,14 +729,19 @@ impl Future for PlayerInternal {
// Handle loading of a new track to play // Handle loading of a new track to play
if let PlayerState::Loading { if let PlayerState::Loading {
ref mut loader, ref mut loader,
track_id,
start_playback, start_playback,
play_request_id, play_request_id,
..
} = self.state } = self.state
{ {
match loader.poll() { match loader.poll() {
Ok(Async::Ready(loaded_track)) => { Ok(Async::Ready(loaded_track)) => {
self.start_playback(play_request_id, loaded_track, start_playback); self.start_playback(
track_id,
play_request_id,
loaded_track,
start_playback,
);
if let PlayerState::Loading { .. } = self.state { if let PlayerState::Loading { .. } = self.state {
panic!("The state wasn't changed by start_playback()"); panic!("The state wasn't changed by start_playback()");
} }
@ -733,10 +755,17 @@ impl Future for PlayerInternal {
} }
// handle pending preload requests. // handle pending preload requests.
if let PlayerPreload::Loading { ref mut loader, .. } = self.preload { if let PlayerPreload::Loading {
ref mut loader,
track_id,
} = self.preload
{
match loader.poll() { match loader.poll() {
Ok(Async::Ready(loaded_track)) => { Ok(Async::Ready(loaded_track)) => {
self.preload = PlayerPreload::Ready { loaded_track }; self.preload = PlayerPreload::Ready {
track_id,
loaded_track,
};
} }
Ok(Async::NotReady) => (), Ok(Async::NotReady) => (),
Err(_) => { Err(_) => {
@ -749,16 +778,16 @@ impl Future for PlayerInternal {
self.ensure_sink_running(); self.ensure_sink_running();
if let PlayerState::Playing { if let PlayerState::Playing {
ref track_meta_data, track_id,
play_request_id, play_request_id,
ref mut decoder, ref mut decoder,
normalisation_factor,
ref mut stream_position_pcm, ref mut stream_position_pcm,
ref mut reported_nominal_start_time, ref mut reported_nominal_start_time,
duration_ms,
.. ..
} = self.state } = self.state
{ {
let track_meta_data = track_meta_data.clone();
let normalisation_factor = track_meta_data.normalisation_factor;
let packet = decoder.next_packet().expect("Vorbis error"); let packet = decoder.next_packet().expect("Vorbis error");
if let Some(ref packet) = packet { if let Some(ref packet) = packet {
@ -786,9 +815,10 @@ impl Future for PlayerInternal {
- Duration::from_millis(stream_position_millis as u64), - Duration::from_millis(stream_position_millis as u64),
); );
self.send_event(PlayerEvent::Playing { self.send_event(PlayerEvent::Playing {
track_meta_data, track_id,
play_request_id, play_request_id,
position_ms: stream_position_millis as u32, position_ms: stream_position_millis as u32,
duration_ms,
}); });
} }
} }
@ -800,16 +830,18 @@ impl Future for PlayerInternal {
} }
if let PlayerState::Playing { if let PlayerState::Playing {
ref track_meta_data, track_id,
play_request_id, play_request_id,
duration_ms,
stream_position_pcm, stream_position_pcm,
ref mut stream_loader_controller, ref mut stream_loader_controller,
ref mut suggested_to_preload_next_track, ref mut suggested_to_preload_next_track,
.. ..
} }
| PlayerState::Paused { | PlayerState::Paused {
ref track_meta_data, track_id,
play_request_id, play_request_id,
duration_ms,
stream_position_pcm, stream_position_pcm,
ref mut stream_loader_controller, ref mut stream_loader_controller,
ref mut suggested_to_preload_next_track, ref mut suggested_to_preload_next_track,
@ -817,15 +849,13 @@ impl Future for PlayerInternal {
} = self.state } = self.state
{ {
if (!*suggested_to_preload_next_track) if (!*suggested_to_preload_next_track)
&& ((track_meta_data.duration_ms as i64 && ((duration_ms as i64 - Self::position_pcm_to_ms(stream_position_pcm) as i64)
- Self::position_pcm_to_ms(stream_position_pcm) as i64)
< PRELOAD_NEXT_TRACK_BEFORE_END_DURATION_MS as i64) < PRELOAD_NEXT_TRACK_BEFORE_END_DURATION_MS as i64)
&& stream_loader_controller.range_to_end_available() && stream_loader_controller.range_to_end_available()
{ {
*suggested_to_preload_next_track = true; *suggested_to_preload_next_track = true;
let track_meta_data = track_meta_data.clone();
self.send_event(PlayerEvent::TimeToPreloadNextTrack { self.send_event(PlayerEvent::TimeToPreloadNextTrack {
track_id: track_meta_data.track_id, track_id,
play_request_id, play_request_id,
}); });
} }
@ -872,21 +902,17 @@ impl PlayerInternal {
fn handle_player_stop(&mut self) { fn handle_player_stop(&mut self) {
match self.state { match self.state {
PlayerState::Playing { PlayerState::Playing {
track_meta_data: TrackMetaData { track_id, .. }, track_id,
play_request_id, play_request_id,
.. ..
} }
| PlayerState::Paused { | PlayerState::Paused {
track_meta_data: TrackMetaData { track_id, .. }, track_id,
play_request_id, play_request_id,
.. ..
} }
| PlayerState::EndOfTrack { | PlayerState::EndOfTrack {
loaded_track: track_id,
PlayerLoadedTrackData {
track_meta_data: TrackMetaData { track_id, .. },
..
},
play_request_id, play_request_id,
.. ..
} }
@ -909,20 +935,21 @@ impl PlayerInternal {
fn handle_play(&mut self) { fn handle_play(&mut self) {
if let PlayerState::Paused { if let PlayerState::Paused {
ref track_meta_data, track_id,
play_request_id, play_request_id,
stream_position_pcm, stream_position_pcm,
duration_ms,
.. ..
} = self.state } = self.state
{ {
let track_meta_data = track_meta_data.clone();
self.state.paused_to_playing(); self.state.paused_to_playing();
let position_ms = Self::position_pcm_to_ms(stream_position_pcm); let position_ms = Self::position_pcm_to_ms(stream_position_pcm);
self.send_event(PlayerEvent::Playing { self.send_event(PlayerEvent::Playing {
track_meta_data, track_id,
play_request_id, play_request_id,
position_ms, position_ms,
duration_ms,
}); });
self.ensure_sink_running(); self.ensure_sink_running();
} else { } else {
@ -932,22 +959,22 @@ impl PlayerInternal {
fn handle_pause(&mut self) { fn handle_pause(&mut self) {
if let PlayerState::Playing { if let PlayerState::Playing {
ref track_meta_data, track_id,
play_request_id, play_request_id,
stream_position_pcm, stream_position_pcm,
duration_ms,
.. ..
} = self.state } = self.state
{ {
let track_meta_data = track_meta_data.clone();
self.state.playing_to_paused(); self.state.playing_to_paused();
self.ensure_sink_stopped(); self.ensure_sink_stopped();
let position_ms = Self::position_pcm_to_ms(stream_position_pcm); let position_ms = Self::position_pcm_to_ms(stream_position_pcm);
self.send_event(PlayerEvent::Paused { self.send_event(PlayerEvent::Paused {
track_meta_data, track_id,
play_request_id, play_request_id,
position_ms, position_ms,
duration_ms,
}); });
} else { } else {
warn!("Player::pause called from invalid state"); warn!("Player::pause called from invalid state");
@ -978,18 +1005,13 @@ impl PlayerInternal {
None => { None => {
self.state.playing_to_end_of_track(); self.state.playing_to_end_of_track();
if let PlayerState::EndOfTrack { if let PlayerState::EndOfTrack {
loaded_track: track_id,
PlayerLoadedTrackData {
ref track_meta_data,
..
},
play_request_id, play_request_id,
.. ..
} = self.state } = self.state
{ {
let track_meta_data = track_meta_data.clone();
self.send_event(PlayerEvent::EndOfTrack { self.send_event(PlayerEvent::EndOfTrack {
track_meta_data, track_id,
play_request_id, play_request_id,
}) })
} else { } else {
@ -1001,6 +1023,7 @@ impl PlayerInternal {
fn start_playback( fn start_playback(
&mut self, &mut self,
track_id: SpotifyId,
play_request_id: u64, play_request_id: u64,
loaded_track: PlayerLoadedTrackData, loaded_track: PlayerLoadedTrackData,
start_playback: bool, start_playback: bool,
@ -1011,16 +1034,20 @@ impl PlayerInternal {
self.ensure_sink_running(); self.ensure_sink_running();
self.send_event(PlayerEvent::Playing { self.send_event(PlayerEvent::Playing {
track_meta_data: loaded_track.track_meta_data.clone(), track_id,
play_request_id, play_request_id,
position_ms, position_ms,
duration_ms: loaded_track.duration_ms,
}); });
self.state = PlayerState::Playing { self.state = PlayerState::Playing {
track_meta_data: loaded_track.track_meta_data, track_id: track_id,
play_request_id: play_request_id, play_request_id: play_request_id,
decoder: loaded_track.decoder, decoder: loaded_track.decoder,
normalisation_factor: loaded_track.normalisation_factor,
stream_loader_controller: loaded_track.stream_loader_controller, stream_loader_controller: loaded_track.stream_loader_controller,
duration_ms: loaded_track.duration_ms,
bytes_per_second: loaded_track.bytes_per_second,
stream_position_pcm: loaded_track.stream_position_pcm, stream_position_pcm: loaded_track.stream_position_pcm,
reported_nominal_start_time: Some( reported_nominal_start_time: Some(
Instant::now() - Duration::from_millis(position_ms as u64), Instant::now() - Duration::from_millis(position_ms as u64),
@ -1030,21 +1057,23 @@ impl PlayerInternal {
} else { } else {
self.ensure_sink_stopped(); self.ensure_sink_stopped();
let track_meta_data = loaded_track.track_meta_data.clone();
self.state = PlayerState::Paused { self.state = PlayerState::Paused {
track_meta_data: loaded_track.track_meta_data, track_id: track_id,
play_request_id: play_request_id, play_request_id: play_request_id,
decoder: loaded_track.decoder, decoder: loaded_track.decoder,
normalisation_factor: loaded_track.normalisation_factor,
stream_loader_controller: loaded_track.stream_loader_controller, stream_loader_controller: loaded_track.stream_loader_controller,
duration_ms: loaded_track.duration_ms,
bytes_per_second: loaded_track.bytes_per_second,
stream_position_pcm: loaded_track.stream_position_pcm, stream_position_pcm: loaded_track.stream_position_pcm,
suggested_to_preload_next_track: false, suggested_to_preload_next_track: false,
}; };
self.send_event(PlayerEvent::Paused { self.send_event(PlayerEvent::Paused {
track_meta_data, track_id,
play_request_id, play_request_id,
position_ms, position_ms,
duration_ms: loaded_track.duration_ms,
}); });
} }
} }
@ -1062,31 +1091,15 @@ impl PlayerInternal {
// emit the correct player event // emit the correct player event
match self.state { match self.state {
PlayerState::Playing { PlayerState::Playing {
track_meta_data: track_id: old_track_id,
TrackMetaData {
track_id: old_track_id,
..
},
.. ..
} }
| PlayerState::Paused { | PlayerState::Paused {
track_meta_data: track_id: old_track_id,
TrackMetaData {
track_id: old_track_id,
..
},
.. ..
} }
| PlayerState::EndOfTrack { | PlayerState::EndOfTrack {
loaded_track: track_id: old_track_id,
PlayerLoadedTrackData {
track_meta_data:
TrackMetaData {
track_id: old_track_id,
..
},
..
},
.. ..
} }
| PlayerState::Loading { | PlayerState::Loading {
@ -1110,18 +1123,16 @@ impl PlayerInternal {
// Check if there's a matching loaded track in the EndOfTrack player state. // Check if there's a matching loaded track in the EndOfTrack player state.
// This is the case if we're repeating the same track again. // This is the case if we're repeating the same track again.
if let PlayerState::EndOfTrack { if let PlayerState::EndOfTrack {
ref mut loaded_track, track_id: previous_track_id,
.. ..
} = self.state } = self.state
{ {
if loaded_track.track_meta_data.track_id == track_id { if previous_track_id == track_id {
let mut loaded_track = match mem::replace(&mut self.state, PlayerState::Invalid) { let mut loaded_track = match mem::replace(&mut self.state, PlayerState::Invalid) {
PlayerState::EndOfTrack { loaded_track, .. } => loaded_track, PlayerState::EndOfTrack { loaded_track, .. } => loaded_track,
_ => unreachable!(), _ => unreachable!(),
}; };
//let loaded_track = mem::replace(&mut *loaded_track, None);
//if let Some(mut loaded_track) = loaded_track {
if Self::position_ms_to_pcm(position_ms) != loaded_track.stream_position_pcm { if Self::position_ms_to_pcm(position_ms) != loaded_track.stream_position_pcm {
loaded_track loaded_track
.stream_loader_controller .stream_loader_controller
@ -1134,32 +1145,31 @@ impl PlayerInternal {
loaded_track.stream_position_pcm = Self::position_ms_to_pcm(position_ms); loaded_track.stream_position_pcm = Self::position_ms_to_pcm(position_ms);
} }
self.preload = PlayerPreload::None; self.preload = PlayerPreload::None;
self.start_playback(play_request_id, loaded_track, play); self.start_playback(track_id, play_request_id, loaded_track, play);
if let PlayerState::Invalid = self.state { if let PlayerState::Invalid = self.state {
panic!("start_playback() hasn't set a valid player state."); panic!("start_playback() hasn't set a valid player state.");
} }
return; return;
//}
} }
} }
// Check if we are already playing the track. If so, just do a seek and update our info. // Check if we are already playing the track. If so, just do a seek and update our info.
if let PlayerState::Playing { if let PlayerState::Playing {
ref track_meta_data, track_id: current_track_id,
ref mut stream_position_pcm, ref mut stream_position_pcm,
ref mut decoder, ref mut decoder,
ref mut stream_loader_controller, ref mut stream_loader_controller,
.. ..
} }
| PlayerState::Paused { | PlayerState::Paused {
ref track_meta_data, track_id: current_track_id,
ref mut stream_position_pcm, ref mut stream_position_pcm,
ref mut decoder, ref mut decoder,
ref mut stream_loader_controller, ref mut stream_loader_controller,
.. ..
} = self.state } = self.state
{ {
if track_meta_data.track_id == track_id { if current_track_id == track_id {
// we can use the current decoder. Ensure it's at the correct position. // we can use the current decoder. Ensure it's at the correct position.
if Self::position_ms_to_pcm(position_ms) != *stream_position_pcm { if Self::position_ms_to_pcm(position_ms) != *stream_position_pcm {
stream_loader_controller.set_random_access_mode(); stream_loader_controller.set_random_access_mode();
@ -1173,29 +1183,35 @@ impl PlayerInternal {
let old_state = mem::replace(&mut self.state, PlayerState::Invalid); let old_state = mem::replace(&mut self.state, PlayerState::Invalid);
if let PlayerState::Playing { if let PlayerState::Playing {
track_meta_data,
stream_position_pcm, stream_position_pcm,
decoder, decoder,
stream_loader_controller, stream_loader_controller,
bytes_per_second,
duration_ms,
normalisation_factor,
.. ..
} }
| PlayerState::Paused { | PlayerState::Paused {
track_meta_data,
stream_position_pcm, stream_position_pcm,
decoder, decoder,
stream_loader_controller, stream_loader_controller,
bytes_per_second,
duration_ms,
normalisation_factor,
.. ..
} = old_state } = old_state
{ {
let loaded_track = PlayerLoadedTrackData { let loaded_track = PlayerLoadedTrackData {
track_meta_data,
decoder, decoder,
normalisation_factor,
stream_loader_controller, stream_loader_controller,
bytes_per_second,
duration_ms,
stream_position_pcm, stream_position_pcm,
}; };
self.preload = PlayerPreload::None; self.preload = PlayerPreload::None;
self.start_playback(play_request_id, loaded_track, play); self.start_playback(track_id, play_request_id, loaded_track, play);
if let PlayerState::Invalid = self.state { if let PlayerState::Invalid = self.state {
panic!("start_playback() hasn't set a valid player state."); panic!("start_playback() hasn't set a valid player state.");
@ -1210,17 +1226,17 @@ impl PlayerInternal {
// Check if the requested track has been preloaded already. If so use the preloaded data. // Check if the requested track has been preloaded already. If so use the preloaded data.
if let PlayerPreload::Ready { if let PlayerPreload::Ready {
loaded_track: track_id: loaded_track_id,
PlayerLoadedTrackData {
ref track_meta_data,
..
},
.. ..
} = self.preload } = self.preload
{ {
if track_id == track_meta_data.track_id { if track_id == loaded_track_id {
let preload = std::mem::replace(&mut self.preload, PlayerPreload::None); let preload = std::mem::replace(&mut self.preload, PlayerPreload::None);
if let PlayerPreload::Ready { mut loaded_track } = preload { if let PlayerPreload::Ready {
track_id,
mut loaded_track,
} = preload
{
if Self::position_ms_to_pcm(position_ms) != loaded_track.stream_position_pcm { if Self::position_ms_to_pcm(position_ms) != loaded_track.stream_position_pcm {
loaded_track loaded_track
.stream_loader_controller .stream_loader_controller
@ -1228,7 +1244,7 @@ impl PlayerInternal {
let _ = loaded_track.decoder.seek(position_ms as i64); // This may be blocking let _ = loaded_track.decoder.seek(position_ms as i64); // This may be blocking
loaded_track.stream_loader_controller.set_stream_mode(); loaded_track.stream_loader_controller.set_stream_mode();
} }
self.start_playback(play_request_id, loaded_track, play); self.start_playback(track_id, play_request_id, loaded_track, play);
return; return;
} else { } else {
unreachable!(); unreachable!();
@ -1293,15 +1309,7 @@ impl PlayerInternal {
.. ..
} }
| PlayerPreload::Ready { | PlayerPreload::Ready {
loaded_track: track_id: currently_loading,
PlayerLoadedTrackData {
track_meta_data:
TrackMetaData {
track_id: currently_loading,
..
},
..
},
.. ..
} = self.preload } = self.preload
{ {
@ -1315,23 +1323,19 @@ impl PlayerInternal {
} }
if let PlayerState::Playing { if let PlayerState::Playing {
ref track_meta_data, track_id: current_track_id,
.. ..
} }
| PlayerState::Paused { | PlayerState::Paused {
ref track_meta_data, track_id: current_track_id,
.. ..
} }
| PlayerState::EndOfTrack { | PlayerState::EndOfTrack {
loaded_track: track_id: current_track_id,
PlayerLoadedTrackData {
ref track_meta_data,
..
},
.. ..
} = self.state } = self.state
{ {
if track_meta_data.track_id == track_id { if current_track_id == track_id {
// we already have the requested track loaded. // we already have the requested track loaded.
preload_track = false; preload_track = false;
} }
@ -1378,32 +1382,34 @@ impl PlayerInternal {
self.preload_data_before_playback(); self.preload_data_before_playback();
if let PlayerState::Playing { if let PlayerState::Playing {
ref track_meta_data, track_id,
play_request_id, play_request_id,
ref mut reported_nominal_start_time, ref mut reported_nominal_start_time,
duration_ms,
.. ..
} = self.state } = self.state
{ {
*reported_nominal_start_time = *reported_nominal_start_time =
Some(Instant::now() - Duration::from_millis(position_ms as u64)); Some(Instant::now() - Duration::from_millis(position_ms as u64));
let track_meta_data = track_meta_data.clone();
self.send_event(PlayerEvent::Playing { self.send_event(PlayerEvent::Playing {
track_meta_data, track_id,
play_request_id, play_request_id,
position_ms, position_ms,
duration_ms,
}); });
} }
if let PlayerState::Paused { if let PlayerState::Paused {
ref track_meta_data, track_id,
play_request_id, play_request_id,
duration_ms,
.. ..
} = self.state } = self.state
{ {
let track_meta_data = track_meta_data.clone();
self.send_event(PlayerEvent::Paused { self.send_event(PlayerEvent::Paused {
track_meta_data, track_id,
play_request_id, play_request_id,
position_ms, position_ms,
duration_ms,
}); });
} }
} }
@ -1480,7 +1486,7 @@ impl PlayerInternal {
fn preload_data_before_playback(&mut self) { fn preload_data_before_playback(&mut self) {
if let PlayerState::Playing { if let PlayerState::Playing {
ref track_meta_data, bytes_per_second,
ref mut stream_loader_controller, ref mut stream_loader_controller,
.. ..
} = self.state } = self.state
@ -1489,9 +1495,8 @@ impl PlayerInternal {
let request_data_length = max( let request_data_length = max(
(READ_AHEAD_DURING_PLAYBACK_ROUNDTRIPS (READ_AHEAD_DURING_PLAYBACK_ROUNDTRIPS
* (0.001 * stream_loader_controller.ping_time_ms() as f64) * (0.001 * stream_loader_controller.ping_time_ms() as f64)
* track_meta_data.bytes_per_second as f64) as usize, * bytes_per_second as f64) as usize,
(READ_AHEAD_DURING_PLAYBACK_SECONDS * track_meta_data.bytes_per_second as f64) (READ_AHEAD_DURING_PLAYBACK_SECONDS * bytes_per_second as f64) as usize,
as usize,
); );
stream_loader_controller.fetch_next(request_data_length); stream_loader_controller.fetch_next(request_data_length);
@ -1499,9 +1504,8 @@ impl PlayerInternal {
let wait_for_data_length = max( let wait_for_data_length = max(
(READ_AHEAD_BEFORE_PLAYBACK_ROUNDTRIPS (READ_AHEAD_BEFORE_PLAYBACK_ROUNDTRIPS
* (0.001 * stream_loader_controller.ping_time_ms() as f64) * (0.001 * stream_loader_controller.ping_time_ms() as f64)
* track_meta_data.bytes_per_second as f64) as usize, * bytes_per_second as f64) as usize,
(READ_AHEAD_BEFORE_PLAYBACK_SECONDS * track_meta_data.bytes_per_second as f64) (READ_AHEAD_BEFORE_PLAYBACK_SECONDS * bytes_per_second as f64) as usize,
as usize,
); );
stream_loader_controller.fetch_next_blocking(wait_for_data_length); stream_loader_controller.fetch_next_blocking(wait_for_data_length);
} }

View file

@ -1,5 +1,4 @@
use librespot::playback::player::PlayerEvent; use librespot::playback::player::PlayerEvent;
use librespot_playback::player::TrackMetaData;
use log::info; use log::info;
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
@ -15,12 +14,6 @@ fn run_program(program: &str, env_vars: HashMap<&str, String>) -> io::Result<Chi
.spawn_async() .spawn_async()
} }
fn add_meta_data_to_env_vars(env_vars: &mut HashMap<&str, String>, track_meta_data: TrackMetaData) {
env_vars.insert("TRACK_ID", track_meta_data.track_id.to_base62());
env_vars.insert("DURATION_MS", track_meta_data.duration_ms.to_string());
env_vars.insert("TRACK_TITLE", track_meta_data.title);
}
pub fn run_program_on_events(event: PlayerEvent, onevent: &str) -> Option<io::Result<Child>> { pub fn run_program_on_events(event: PlayerEvent, onevent: &str) -> Option<io::Result<Child>> {
let mut env_vars = HashMap::new(); let mut env_vars = HashMap::new();
match event { match event {
@ -41,21 +34,25 @@ pub fn run_program_on_events(event: PlayerEvent, onevent: &str) -> Option<io::Re
env_vars.insert("TRACK_ID", track_id.to_base62()); env_vars.insert("TRACK_ID", track_id.to_base62());
} }
PlayerEvent::Playing { PlayerEvent::Playing {
track_meta_data, track_id,
duration_ms,
position_ms, position_ms,
.. ..
} => { } => {
env_vars.insert("PLAYER_EVENT", "playing".to_string()); env_vars.insert("PLAYER_EVENT", "playing".to_string());
add_meta_data_to_env_vars(&mut env_vars, track_meta_data); env_vars.insert("TRACK_ID", track_id.to_base62());
env_vars.insert("DURATION_MS", duration_ms.to_string());
env_vars.insert("POSITION_MS", position_ms.to_string()); env_vars.insert("POSITION_MS", position_ms.to_string());
} }
PlayerEvent::Paused { PlayerEvent::Paused {
track_meta_data, track_id,
duration_ms,
position_ms, position_ms,
.. ..
} => { } => {
env_vars.insert("PLAYER_EVENT", "paused".to_string()); env_vars.insert("PLAYER_EVENT", "paused".to_string());
add_meta_data_to_env_vars(&mut env_vars, track_meta_data); env_vars.insert("TRACK_ID", track_id.to_base62());
env_vars.insert("DURATION_MS", duration_ms.to_string());
env_vars.insert("POSITION_MS", position_ms.to_string()); env_vars.insert("POSITION_MS", position_ms.to_string());
} }
PlayerEvent::VolumeSet { volume } => { PlayerEvent::VolumeSet { volume } => {