@@ -3,11 +3,13 @@ use crate::model::playable::Playable;
33use crate :: queue:: QueueEvent ;
44use crate :: spotify:: PlayerEvent ;
55use futures:: Future ;
6+ use librespot_connect:: { ConnectConfig , LoadRequest , LoadRequestOptions , Spirc } ;
67use librespot_core:: session:: Session ;
78use librespot_core:: spotify_id:: SpotifyId ;
89use librespot_core:: token:: Token ;
910use librespot_playback:: mixer:: Mixer ;
1011use librespot_playback:: player:: { Player , PlayerEvent as LibrespotPlayerEvent } ;
12+ use librespot_protocol:: credentials;
1113use log:: { debug, error, info, warn} ;
1214use std:: sync:: Arc ;
1315use std:: sync:: mpsc:: Sender ;
@@ -41,31 +43,37 @@ pub struct Worker {
4143 events : EventManager ,
4244 player_events : UnboundedReceiverStream < LibrespotPlayerEvent > ,
4345 commands : UnboundedReceiverStream < WorkerCommand > ,
44- session : Session ,
45- player : Arc < Player > ,
4646 token_task : Pin < Box < dyn Future < Output = ( ) > + Send > > ,
4747 player_status : PlayerStatus ,
48- mixer : Arc < dyn Mixer > ,
48+ session : Session ,
49+ spirc : Spirc ,
50+ spirc_task : Pin < Box < dyn Future < Output = ( ) > + Send > > ,
4951}
5052
5153impl Worker {
52- pub ( crate ) fn new (
54+ pub ( crate ) async fn new (
5355 events : EventManager ,
56+ credentials : librespot_core:: authentication:: Credentials ,
5457 player_events : mpsc:: UnboundedReceiver < LibrespotPlayerEvent > ,
5558 commands : mpsc:: UnboundedReceiver < WorkerCommand > ,
5659 session : Session ,
5760 player : Arc < Player > ,
5861 mixer : Arc < dyn Mixer > ,
5962 ) -> Self {
63+ let config = ConnectConfig {
64+ name : "ncspot" . to_string ( ) ,
65+ ..Default :: default ( )
66+ } ;
67+ let spirc = Spirc :: new ( config, session. clone ( ) , credentials, player, mixer) . await . expect ( "Spirc should be initialized" ) ;
6068 Self {
6169 events,
6270 player_events : UnboundedReceiverStream :: new ( player_events) ,
6371 commands : UnboundedReceiverStream :: new ( commands) ,
64- player,
65- session,
6672 token_task : Box :: pin ( futures:: future:: pending ( ) ) ,
6773 player_status : PlayerStatus :: Stopped ,
68- mixer,
74+ session,
75+ spirc : spirc. 0 ,
76+ spirc_task : Box :: pin ( spirc. 1 ) ,
6977 }
7078 }
7179
@@ -105,7 +113,14 @@ impl Worker {
105113 warn!( "track is not playable" ) ;
106114 self . events. send( Event :: Player ( PlayerEvent :: FinishedTrack ) ) ;
107115 } else {
108- self . player. load( id, start_playing, position_ms) ;
116+ let options = LoadRequestOptions {
117+ start_playing,
118+ seek_to: position_ms,
119+ context_options: None ,
120+ playing_track: None ,
121+ } ;
122+ let req = LoadRequest :: from_tracks( vec![ id. to_uri( ) . expect( "uri" ) ] , options) ;
123+ self . spirc. load( req) ;
109124 }
110125 }
111126 Err ( e) => {
@@ -115,32 +130,31 @@ impl Worker {
115130 }
116131 }
117132 Some ( WorkerCommand :: Play ) => {
118- self . player . play( ) ;
133+ self . spirc . play( ) ;
119134 }
120135 Some ( WorkerCommand :: Pause ) => {
121- self . player . pause( ) ;
136+ self . spirc . pause( ) ;
122137 }
123138 Some ( WorkerCommand :: Stop ) => {
124- self . player . stop ( ) ;
139+ todo! ( "stop spirc" ) ;
125140 }
126141 Some ( WorkerCommand :: Seek ( pos) ) => {
127- self . player . seek ( pos) ;
142+ self . spirc . set_position_ms ( pos) ;
128143 }
129144 Some ( WorkerCommand :: SetVolume ( volume) ) => {
130- self . mixer . set_volume( volume) ;
145+ self . spirc . set_volume( volume) ;
131146 }
132147 Some ( WorkerCommand :: RequestToken ( sender) ) => {
133148 self . token_task = Box :: pin( Self :: get_token( self . session. clone( ) , sender) ) ;
134149 }
135150 Some ( WorkerCommand :: Preload ( playable) ) => {
136151 if let Ok ( id) = SpotifyId :: from_uri( & playable. uri( ) ) {
137152 debug!( "Preloading {id:?}" ) ;
138- self . player. preload( id) ;
153+ // self.player.preload(id);
139154 }
140155 }
141156 Some ( WorkerCommand :: Shutdown ) => {
142- self . player. stop( ) ;
143- self . session. shutdown( ) ;
157+ self . spirc. shutdown( ) ;
144158 }
145159 None => info!( "empty stream" )
146160 } ,
@@ -197,6 +211,9 @@ impl Worker {
197211 break
198212 } ,
199213 } ,
214+ _ = self . spirc_task. as_mut( ) => {
215+ info!( "spirc task tick" ) ;
216+ } ,
200217 // Update animated parts of the UI (e.g. statusbar during playback).
201218 _ = ui_refresh. tick( ) => {
202219 if !matches!( self . player_status, PlayerStatus :: Stopped ) {
@@ -215,6 +232,6 @@ impl Worker {
215232impl Drop for Worker {
216233 fn drop ( & mut self ) {
217234 debug ! ( "Worker thread is shutting down, stopping player" ) ;
218- self . player . stop ( ) ;
235+ self . spirc . shutdown ( ) ;
219236 }
220237}
0 commit comments