@@ -60,7 +60,11 @@ pub struct Spotify {
6060}
6161
6262impl Spotify {
63- pub fn new ( events : EventManager , credentials : Credentials , cfg : Arc < config:: Config > ) -> Self {
63+ pub fn new (
64+ events : EventManager ,
65+ credentials : Credentials ,
66+ cfg : Arc < config:: Config > ,
67+ ) -> Result < Self , Box < dyn Error > > {
6468 let mut spotify = Self {
6569 events,
6670 credentials,
@@ -73,7 +77,7 @@ impl Spotify {
7377 } ;
7478
7579 let ( user_tx, user_rx) = oneshot:: channel ( ) ;
76- spotify. start_worker ( Some ( user_tx) ) ;
80+ spotify. start_worker ( Some ( user_tx) ) ? ;
7781 let user = ASYNC_RUNTIME . get ( ) . unwrap ( ) . block_on ( user_rx) . ok ( ) ;
7882 let volume = cfg. state ( ) . volume ;
7983 spotify. set_volume ( volume) ;
@@ -83,30 +87,35 @@ impl Spotify {
8387
8488 spotify. api . set_user ( user) ;
8589
86- spotify
90+ Ok ( spotify)
8791 }
8892
8993 /// Start the worker thread. If `user_tx` is given, it will receive the username of the logged
9094 /// in user.
91- pub fn start_worker ( & self , user_tx : Option < oneshot:: Sender < String > > ) {
95+ pub fn start_worker (
96+ & self ,
97+ user_tx : Option < oneshot:: Sender < String > > ,
98+ ) -> Result < ( ) , Box < dyn Error > > {
9299 let ( tx, rx) = mpsc:: unbounded_channel ( ) ;
93100 * self . channel . write ( ) . unwrap ( ) = Some ( tx) ;
94- {
95- let worker_channel = self . channel . clone ( ) ;
96- let cfg = self . cfg . clone ( ) ;
97- let events = self . events . clone ( ) ;
98- let volume = self . volume ( ) ;
99- let credentials = self . credentials . clone ( ) ;
100- ASYNC_RUNTIME . get ( ) . unwrap ( ) . spawn ( Self :: worker (
101- worker_channel,
102- events,
103- rx,
104- cfg,
105- credentials,
106- user_tx,
107- volume,
108- ) ) ;
109- }
101+ let worker_channel = self . channel . clone ( ) ;
102+ let cfg = self . cfg . clone ( ) ;
103+ let events = self . events . clone ( ) ;
104+ let volume = self . volume ( ) ;
105+ let credentials = self . credentials . clone ( ) ;
106+ let backend_name = cfg. values ( ) . backend . clone ( ) ;
107+ let backend = Self :: init_backend ( backend_name) ?;
108+ ASYNC_RUNTIME . get ( ) . unwrap ( ) . spawn ( Self :: worker (
109+ worker_channel,
110+ events,
111+ rx,
112+ cfg,
113+ credentials,
114+ user_tx,
115+ volume,
116+ backend,
117+ ) ) ;
118+ Ok ( ( ) )
110119 }
111120
112121 /// Generate the librespot [SessionConfig] used when creating a [Session].
@@ -161,14 +170,19 @@ impl Spotify {
161170 }
162171
163172 /// Create and initialize the requested audio backend.
164- fn init_backend ( desired_backend : Option < String > ) -> Option < SinkBuilder > {
173+ fn init_backend ( desired_backend : Option < String > ) -> Result < SinkBuilder , Box < dyn Error > > {
165174 let backend = if let Some ( name) = desired_backend {
166175 audio_backend:: BACKENDS
167176 . iter ( )
168177 . find ( |backend| name == backend. 0 )
178+ . ok_or ( format ! (
179+ r#"configured audio backend "{name}" can't be found"#
180+ ) ) ?
169181 } else {
170- audio_backend:: BACKENDS . first ( )
171- } ?;
182+ audio_backend:: BACKENDS
183+ . first ( )
184+ . ok_or ( "no available audio backends found" ) ?
185+ } ;
172186
173187 let backend_name = backend. 0 ;
174188
@@ -179,10 +193,11 @@ impl Spotify {
179193 env:: set_var ( "PULSE_PROP_media.role" , "music" ) ;
180194 }
181195
182- Some ( backend. 1 )
196+ Ok ( backend. 1 )
183197 }
184198
185199 /// Create and run the worker thread.
200+ #[ allow( clippy:: too_many_arguments) ]
186201 async fn worker (
187202 worker_channel : Arc < RwLock < Option < mpsc:: UnboundedSender < WorkerCommand > > > > ,
188203 events : EventManager ,
@@ -191,6 +206,7 @@ impl Spotify {
191206 credentials : Credentials ,
192207 user_tx : Option < oneshot:: Sender < String > > ,
193208 volume : u16 ,
209+ backend : SinkBuilder ,
194210 ) {
195211 let bitrate_str = cfg. values ( ) . bitrate . unwrap_or ( 320 ) . to_string ( ) ;
196212 let bitrate = Bitrate :: from_str ( & bitrate_str) ;
@@ -216,9 +232,6 @@ impl Spotify {
216232 let mixer = create_mixer ( MixerConfig :: default ( ) ) ;
217233 mixer. set_volume ( volume) ;
218234
219- let backend_name = cfg. values ( ) . backend . clone ( ) ;
220- let backend =
221- Self :: init_backend ( backend_name) . expect ( "Could not find an audio playback backend" ) ;
222235 let audio_format: librespot_playback:: config:: AudioFormat = Default :: default ( ) ;
223236 let ( player, player_events) = Player :: new (
224237 player_config,
0 commit comments