Skip to content

Commit

Permalink
Fix audio provider not receiving configuration on initial output stre…
Browse files Browse the repository at this point in the history
…am creation
  • Loading branch information
manpat committed Oct 6, 2024
1 parent 164b725 commit 2a03e65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
32 changes: 20 additions & 12 deletions toybox-audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ impl System {

match handle.take().unwrap().join() {
Ok(Ok(new_stream)) => {
log::info!("Output stream active");

self.stream_state = StreamState::Active(new_stream);
self.shared.device_lost.store(false, Ordering::Relaxed);
}
Expand All @@ -88,10 +90,13 @@ impl System {
Err(panic_data) => {
log::error!("Panic during audio stream creation!");
self.stream_state = StreamState::InitFailure;
self.try_update_provider_config();

std::panic::resume_unwind(panic_data);
}
}

self.try_update_provider_config();
}

StreamState::InitFailure => {}
Expand All @@ -107,6 +112,8 @@ impl System {

if let Some(mut provider) = provider.into() {
let configuration = self.stream_state.as_active_stream().map(|active_stream| active_stream.configuration);

log::info!("Setting initial provider configuration: {configuration:?}");
provider.on_configuration_changed(configuration);

*shared_provider = Some(Box::new(provider));
Expand All @@ -117,6 +124,18 @@ impl System {

Ok(())
}

fn try_update_provider_config(&mut self) {
if let Ok(mut guard) = self.shared.provider.lock()
&& let Some(provider) = &mut *guard
{
let configuration = self.stream_state.as_active_stream()
.map(|active_stream| active_stream.configuration);

log::info!("Update provider configuration: {configuration:?}");
provider.on_configuration_changed(configuration);
}
}
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -145,18 +164,7 @@ struct SharedState {
fn start_stream_build(shared: Arc<SharedState>) -> JoinHandle<anyhow::Result<ActiveStream>> {
std::thread::spawn(move || {
let host = cpal::default_host();
let result = build_output_stream(&host, shared.clone());

let new_configuration = result.as_ref().ok().map(|stream| stream.configuration);

// If there's a provider, notify of the configuration change
if let Ok(mut guard) = shared.provider.lock()
&& let Some(provider) = &mut *guard
{
provider.on_configuration_changed(new_configuration);
}

result
build_output_stream(&host, shared.clone())
})
}

Expand Down
3 changes: 3 additions & 0 deletions toybox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ pub fn run_with_settings<F, A>(settings: host::Settings<'_>, start_app: F) -> an
wants_quit: false,
};

context.prepare_frame();
context.start_frame();

let app = tracing::info_span!("app start").in_scope(|| start_app(&mut context))?;

Ok(HostedApp {
Expand Down

0 comments on commit 2a03e65

Please sign in to comment.