@@ -51,7 +51,6 @@ pub type PlayerResult = Result<(), Error>;
5151pub struct Player {
5252 commands : Option < mpsc:: UnboundedSender < PlayerCommand > > ,
5353 thread_handle : Option < thread:: JoinHandle < ( ) > > ,
54- play_request_id_generator : SeqGenerator < u64 > ,
5554}
5655
5756#[ derive( PartialEq , Eq , Debug , Clone , Copy ) ]
@@ -79,14 +78,14 @@ struct PlayerInternal {
7978 auto_normalise_as_album : bool ,
8079
8180 player_id : usize ,
81+ play_request_id_generator : SeqGenerator < u64 > ,
8282}
8383
8484pub static PLAYER_COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
8585
8686enum PlayerCommand {
8787 Load {
8888 track_id : SpotifyId ,
89- play_request_id : u64 ,
9089 play : bool ,
9190 position_ms : u32 ,
9291 } ,
@@ -97,6 +96,7 @@ enum PlayerCommand {
9796 Pause ,
9897 Stop ,
9998 Seek ( u32 ) ,
99+ SetSession ( Session ) ,
100100 AddEventSender ( mpsc:: UnboundedSender < PlayerEvent > ) ,
101101 SetSinkEventCallback ( Option < SinkEventCallback > ) ,
102102 EmitVolumeChangedEvent ( u16 ) ,
@@ -123,6 +123,10 @@ enum PlayerCommand {
123123
124124#[ derive( Debug , Clone ) ]
125125pub enum PlayerEvent {
126+ // Play request id changed
127+ PlayRequestIdChanged {
128+ play_request_id : u64 ,
129+ } ,
126130 // Fired when the player is stopped (e.g. by issuing a "stop" command to the player).
127131 Stopped {
128132 play_request_id : u64 ,
@@ -318,7 +322,7 @@ impl Player {
318322 session : Session ,
319323 volume_getter : Box < dyn VolumeGetter > ,
320324 sink_builder : F ,
321- ) -> Self
325+ ) -> Arc < Self >
322326 where
323327 F : FnOnce ( ) -> Box < dyn Sink > + Send + ' static ,
324328 {
@@ -349,6 +353,7 @@ impl Player {
349353 auto_normalise_as_album : false ,
350354
351355 player_id,
356+ play_request_id_generator : SeqGenerator :: new ( 0 ) ,
352357 } ;
353358
354359 // While PlayerInternal is written as a future, it still contains blocking code.
@@ -382,11 +387,17 @@ impl Player {
382387 }
383388 } ;
384389
385- Self {
390+ Arc :: new ( Self {
386391 commands : Some ( cmd_tx) ,
387392 thread_handle : Some ( handle) ,
388- play_request_id_generator : SeqGenerator :: new ( 0 ) ,
393+ } )
394+ }
395+
396+ pub fn is_invalid ( & self ) -> bool {
397+ if let Some ( handle) = self . thread_handle . as_ref ( ) {
398+ return handle. is_finished ( ) ;
389399 }
400+ true
390401 }
391402
392403 fn command ( & self , cmd : PlayerCommand ) {
@@ -397,16 +408,12 @@ impl Player {
397408 }
398409 }
399410
400- pub fn load ( & mut self , track_id : SpotifyId , start_playing : bool , position_ms : u32 ) -> u64 {
401- let play_request_id = self . play_request_id_generator . get ( ) ;
411+ pub fn load ( & self , track_id : SpotifyId , start_playing : bool , position_ms : u32 ) {
402412 self . command ( PlayerCommand :: Load {
403413 track_id,
404- play_request_id,
405414 play : start_playing,
406415 position_ms,
407416 } ) ;
408-
409- play_request_id
410417 }
411418
412419 pub fn preload ( & self , track_id : SpotifyId ) {
@@ -429,6 +436,10 @@ impl Player {
429436 self . command ( PlayerCommand :: Seek ( position_ms) ) ;
430437 }
431438
439+ pub fn set_session ( & self , session : Session ) {
440+ self . command ( PlayerCommand :: SetSession ( session) ) ;
441+ }
442+
432443 pub fn get_player_event_channel ( & self ) -> PlayerEventChannel {
433444 let ( event_sender, event_receiver) = mpsc:: unbounded_channel ( ) ;
434445 self . command ( PlayerCommand :: AddEventSender ( event_sender) ) ;
@@ -1264,10 +1275,6 @@ impl Future for PlayerInternal {
12641275 }
12651276 }
12661277
1267- if self . session . is_invalid ( ) {
1268- return Poll :: Ready ( ( ) ) ;
1269- }
1270-
12711278 if ( !self . state . is_playing ( ) ) && all_futures_completed_or_not_ready {
12721279 return Poll :: Pending ;
12731280 }
@@ -1515,10 +1522,15 @@ impl PlayerInternal {
15151522 fn handle_command_load (
15161523 & mut self ,
15171524 track_id : SpotifyId ,
1518- play_request_id : u64 ,
1525+ play_request_id_option : Option < u64 > ,
15191526 play : bool ,
15201527 position_ms : u32 ,
15211528 ) -> PlayerResult {
1529+ let play_request_id =
1530+ play_request_id_option. unwrap_or ( self . play_request_id_generator . get ( ) ) ;
1531+
1532+ self . send_event ( PlayerEvent :: PlayRequestIdChanged { play_request_id } ) ;
1533+
15221534 if !self . config . gapless {
15231535 self . ensure_sink_stopped ( play) ;
15241536 }
@@ -1771,7 +1783,7 @@ impl PlayerInternal {
17711783 {
17721784 return self . handle_command_load (
17731785 track_id,
1774- play_request_id,
1786+ Some ( play_request_id) ,
17751787 start_playback,
17761788 position_ms,
17771789 ) ;
@@ -1828,10 +1840,9 @@ impl PlayerInternal {
18281840 match cmd {
18291841 PlayerCommand :: Load {
18301842 track_id,
1831- play_request_id,
18321843 play,
18331844 position_ms,
1834- } => self . handle_command_load ( track_id, play_request_id , play, position_ms) ?,
1845+ } => self . handle_command_load ( track_id, None , play, position_ms) ?,
18351846
18361847 PlayerCommand :: Preload { track_id } => self . handle_command_preload ( track_id) ,
18371848
@@ -1843,6 +1854,8 @@ impl PlayerInternal {
18431854
18441855 PlayerCommand :: Stop => self . handle_player_stop ( ) ,
18451856
1857+ PlayerCommand :: SetSession ( session) => self . session = session,
1858+
18461859 PlayerCommand :: AddEventSender ( sender) => self . event_senders . push ( sender) ,
18471860
18481861 PlayerCommand :: SetSinkEventCallback ( callback) => self . sink_event_callback = callback,
@@ -2057,6 +2070,7 @@ impl fmt::Debug for PlayerCommand {
20572070 PlayerCommand :: Pause => f. debug_tuple ( "Pause" ) . finish ( ) ,
20582071 PlayerCommand :: Stop => f. debug_tuple ( "Stop" ) . finish ( ) ,
20592072 PlayerCommand :: Seek ( position) => f. debug_tuple ( "Seek" ) . field ( & position) . finish ( ) ,
2073+ PlayerCommand :: SetSession ( _) => f. debug_tuple ( "SetSession" ) . finish ( ) ,
20602074 PlayerCommand :: AddEventSender ( _) => f. debug_tuple ( "AddEventSender" ) . finish ( ) ,
20612075 PlayerCommand :: SetSinkEventCallback ( _) => {
20622076 f. debug_tuple ( "SetSinkEventCallback" ) . finish ( )
0 commit comments