Skip to content

Commit 2a03e65

Browse files
committed
Fix audio provider not receiving configuration on initial output stream creation
1 parent 164b725 commit 2a03e65

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

toybox-audio/src/lib.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ impl System {
7676

7777
match handle.take().unwrap().join() {
7878
Ok(Ok(new_stream)) => {
79+
log::info!("Output stream active");
80+
7981
self.stream_state = StreamState::Active(new_stream);
8082
self.shared.device_lost.store(false, Ordering::Relaxed);
8183
}
@@ -88,10 +90,13 @@ impl System {
8890
Err(panic_data) => {
8991
log::error!("Panic during audio stream creation!");
9092
self.stream_state = StreamState::InitFailure;
93+
self.try_update_provider_config();
9194

9295
std::panic::resume_unwind(panic_data);
9396
}
9497
}
98+
99+
self.try_update_provider_config();
95100
}
96101

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

108113
if let Some(mut provider) = provider.into() {
109114
let configuration = self.stream_state.as_active_stream().map(|active_stream| active_stream.configuration);
115+
116+
log::info!("Setting initial provider configuration: {configuration:?}");
110117
provider.on_configuration_changed(configuration);
111118

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

118125
Ok(())
119126
}
127+
128+
fn try_update_provider_config(&mut self) {
129+
if let Ok(mut guard) = self.shared.provider.lock()
130+
&& let Some(provider) = &mut *guard
131+
{
132+
let configuration = self.stream_state.as_active_stream()
133+
.map(|active_stream| active_stream.configuration);
134+
135+
log::info!("Update provider configuration: {configuration:?}");
136+
provider.on_configuration_changed(configuration);
137+
}
138+
}
120139
}
121140

122141
#[derive(Debug, Copy, Clone)]
@@ -145,18 +164,7 @@ struct SharedState {
145164
fn start_stream_build(shared: Arc<SharedState>) -> JoinHandle<anyhow::Result<ActiveStream>> {
146165
std::thread::spawn(move || {
147166
let host = cpal::default_host();
148-
let result = build_output_stream(&host, shared.clone());
149-
150-
let new_configuration = result.as_ref().ok().map(|stream| stream.configuration);
151-
152-
// If there's a provider, notify of the configuration change
153-
if let Ok(mut guard) = shared.provider.lock()
154-
&& let Some(provider) = &mut *guard
155-
{
156-
provider.on_configuration_changed(new_configuration);
157-
}
158-
159-
result
167+
build_output_stream(&host, shared.clone())
160168
})
161169
}
162170

toybox/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ pub fn run_with_settings<F, A>(settings: host::Settings<'_>, start_app: F) -> an
7575
wants_quit: false,
7676
};
7777

78+
context.prepare_frame();
79+
context.start_frame();
80+
7881
let app = tracing::info_span!("app start").in_scope(|| start_app(&mut context))?;
7982

8083
Ok(HostedApp {

0 commit comments

Comments
 (0)