Skip to content

Commit

Permalink
More explicit thread error handling in player
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLG1979 committed Jun 20, 2023
1 parent 105cdb6 commit c109386
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
45 changes: 39 additions & 6 deletions playback/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@ impl Player {
let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
let (event_sender, event_receiver) = mpsc::unbounded_channel();

let handle = thread::spawn(move || {
debug!("new Player[{}]", session.session_id());
let thread_name = format!("librespot-player:{}", session.session_id());

let builder = thread::Builder::new().name(thread_name.clone());

let handle = match builder.spawn(move || {
let sample_pipeline = SamplePipeline::new(&config, sink_builder(), volume_getter);

let internal = PlayerInternal {
Expand All @@ -281,8 +283,21 @@ impl Player {
// While PlayerInternal is written as a future, it still contains blocking code.
// It must be run by using block_on() in a dedicated thread.
futures_executor::block_on(internal);
debug!("PlayerInternal thread finished.");
});

match thread::current().name() {
Some(name) => debug!("<PlayerInternal> [{name}] thread finished"),
None => debug!("<PlayerInternal> thread finished"),
}
}) {
Ok(handle) => {
debug!("Created <PlayerInternal> [{thread_name}] thread");
handle
}
Err(e) => {
error!("Error creating <PlayerInternal> [{thread_name}] thread: {e}");
exit(1);
}
};

(
Player {
Expand Down Expand Up @@ -1744,12 +1759,30 @@ impl PlayerInternal {

let (result_tx, result_rx) = oneshot::channel();

std::thread::spawn(move || {
let thread_name = format!(
"librespot-loader:{}",
spotify_id.to_uri().unwrap_or_default()
);

let builder = thread::Builder::new().name(thread_name.clone());

match builder.spawn(move || {
let data = futures_executor::block_on(loader.load_track(spotify_id, position_ms));
if let Some(data) = data {
let _ = result_tx.send(data);

match thread::current().name() {
Some(name) => debug!("<PlayerInternal> [{name}] thread finished"),
None => debug!("<PlayerInternal> [librespot-loader] thread finished"),
}
}
});
}) {
Ok(_) => debug!("Created <PlayerInternal> [{thread_name}] thread"),
Err(e) => {
error!("Error creating <PlayerInternal> [{thread_name}] thread: {e}");
exit(1);
}
}

result_rx.map_err(|_| ())
}
Expand Down
4 changes: 2 additions & 2 deletions playback/src/resampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ impl StereoInterleavedResampler {
_ => {
debug!("Interpolation Quality: {interpolation_quality}");

let left_thread_name = "librespot-left-resampler".to_string();
let right_thread_name = "librespot-right-resampler".to_string();
let left_thread_name = "librespot-resampler:left".to_string();
let right_thread_name = "librespot-resampler:right".to_string();

match interpolation_quality {
InterpolationQuality::Low => {
Expand Down

0 comments on commit c109386

Please sign in to comment.