FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Playback: fall back to a working output device when the ALSA default cannot be opened
Ahmed Hosny9bf6595ccd53
diff --git a/apps/filmcraft/src/audio.rs b/apps/filmcraft/src/audio.rsindex c8fe8b7f9..b8d164bb7 100644--- a/apps/filmcraft/src/audio.rs+++ b/apps/filmcraft/src/audio.rs@@ -17,6 +17,9 @@ pub struct CpalOut {hw: AudioHardwarePrefs,/// Sequence sample rate for "Attempt to force hardware to document sample rate".document_rate: Option<u32>,+ /// Status-bar note: the host's default device could not be opened and a fallback device is+ /// in use, or no output device was found (playing without sound).+ note: Option<String>,}/// The host named in the settings (empty or unknown = the default host).@@ -30,21 +33,47 @@ fn host(name: &str) -> cpal::Host {cpal::default_host()}-/// The output device named in the settings (empty or missing = the host's default).-fn output_device(h: &cpal::Host, name: &str) -> Option<cpal::Device> {+/// The output device the settings ask for, with a status-bar note when a fallback is in use.+struct DeviceChoice {+ device: cpal::Device,+ /// The host's default device could not be opened: this device is the fallback.+ fallback: Option<String>,+}++/// The output device named in the settings (empty or missing = the host's default). When the+/// default cannot be opened — a broken ALSA `default` (missing `99-pipewire-default.conf`,+/// #23/#106) — the first device that can, so the app is not silently mute.+fn output_device(h: &cpal::Host, name: &str) -> Option<DeviceChoice> {if !name.is_empty()&& let Ok(mut devs) = h.output_devices()&& let Some(d) = devs.find(|d| d.name().is_ok_and(|n| n == name)){- return Some(d);+ return Some(DeviceChoice { device: d, fallback: None });}- h.default_output_device()+ if let Some(d) = h.default_output_device()+ && d.default_output_config().is_ok()+ {+ return Some(DeviceChoice { device: d, fallback: None });+ }+ let mut devs = h.output_devices().ok()?;+ let d = devs.find(|d| d.default_output_config().is_ok())?;+ let found = d.name().unwrap_or_default();+ log::warn!("the default audio output device cannot be opened; falling back to '{found}'");+ Some(DeviceChoice { device: d, fallback: Some(format!("Audio hardware: the default output device cannot be opened; using '{found}'")) })}impl CpalOut {pub fn new() -> Self {let host = cpal::default_host();- let cfg = host.default_output_device().and_then(|dev| dev.default_output_config().ok());+ let choice = output_device(&host, "");+ let cfg = choice.as_ref().and_then(|c| c.device.default_output_config().ok());+ let note = match choice {+ Some(c) => c.fallback,+ None => {+ log::warn!("no audio output device found");+ Some("No audio output device found: playing without sound (check Settings ▸ Audio Hardware)".into())+ }+ };Self {stream: None,played: Arc::new(AtomicU64::new(0)),@@ -53,6 +82,7 @@ impl CpalOut {channels: cfg.as_ref().map_or(2, |c| c.channels()),hw: AudioHardwarePrefs::default(),document_rate: None,+ note,}}@@ -124,7 +154,8 @@ impl AudioOut for CpalOut {fn start(&mut self, mut fill: Box<dyn FnMut(&mut [f32], usize) + Send>) -> Result<u32, String> {self.stop();let host = host(&self.hw.device_class);- let dev = output_device(&host, &self.hw.default_output).ok_or("no output device")?;+ let DeviceChoice { device: dev, fallback } = output_device(&host, &self.hw.default_output).ok_or("no output device")?;+ self.note = fallback;let cfg = self.config(&dev)?;let channels = cfg.channels() as usize;if channels == 0 || cfg.sample_rate().0 == 0 {@@ -185,7 +216,7 @@ impl AudioOut for CpalOut {let h = host(&self.hw.device_class);let outputs = h.output_devices().map(|d| d.filter_map(|x| x.name().ok()).collect()).unwrap_or_default();let inputs = h.input_devices().map(|d| d.filter_map(|x| x.name().ok()).collect()).unwrap_or_default();- let output_channels = output_device(&h, &self.hw.default_output).and_then(|d| self.config(&d).ok()).map(|c| c.channels()).unwrap_or(0);+ let output_channels = output_device(&h, &self.hw.default_output).and_then(|c| self.config(&c.device).ok()).map(|cfg| cfg.channels()).unwrap_or(0);AudioDevices { hosts: cpal::available_hosts().iter().map(|h| h.name().to_string()).collect(), inputs, outputs, output_channels }}fn configure(&mut self, hw: &AudioHardwarePrefs, document_rate: Option<u32>) {@@ -193,13 +224,17 @@ impl AudioOut for CpalOut {self.document_rate = document_rate;// the rate playback mixes at must be known before `start`let host = host(&self.hw.device_class);- if let Some(dev) = output_device(&host, &self.hw.default_output)- && let Ok(cfg) = self.config(&dev)+ if let Some(choice) = output_device(&host, &self.hw.default_output)+ && let Ok(cfg) = self.config(&choice.device){self.rate = cfg.sample_rate().0;self.channels = cfg.channels();+ self.note = choice.fallback;}}+ fn note(&self) -> Option<String> {+ self.note.clone()+ }}#[cfg(test)]diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 461aa36ea..ed7ad14cb 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -62,6 +62,11 @@ pub trait AudioOut {/// effect at the next `start`; `document_rate` is the sequence sample rate for "Attempt to/// force hardware to document sample rate".fn configure(&mut self, _hw: &filmcraft_engine::settings::AudioHardwarePrefs, _document_rate: Option<u32>) {}+ /// A hardware note for the status bar — e.g. the default output device could not be opened+ /// and a fallback device is in use, or no output device was found — or None.+ fn note(&self) -> Option<String> {+ None+ }}/// What the platform audio layer can open (Settings ▸ Audio Hardware).@@ -494,6 +499,9 @@ impl FilmcraftApp {self.start_audio();}}+ if let Some(note) = self.audio.as_ref().and_then(|a| a.note()) {+ self.ui.status = note;+ }if prev.is_none() && !self.workspace_restored {// reopen the workspace in use when the app last closedself.workspace_restored = true;@@ -758,7 +766,12 @@ impl FilmcraftApp {})};match a.start(fill) {- Ok(_) => self.playback.audio_clock = true,+ Ok(_) => {+ self.playback.audio_clock = true;+ if let Some(note) = self.audio.as_ref().and_then(|audio| audio.note()) {+ self.ui.status = note;+ }+ }Err(e) => {log::warn!("audio output unavailable: {e}");self.playback.audio_clock = false;diff --git a/crates/ui-egui/tests/playback_clock_ui.rs b/crates/ui-egui/tests/playback_clock_ui.rsindex 81570d161..3fb7b3e67 100644--- a/crates/ui-egui/tests/playback_clock_ui.rs+++ b/crates/ui-egui/tests/playback_clock_ui.rs@@ -16,6 +16,7 @@ struct FakeOut {played: Arc<AtomicU64>,per_poll: u64,started: bool,+ note: Option<String>,}impl AudioOut for FakeOut {@@ -33,6 +34,9 @@ impl AudioOut for FakeOut {fn played_frames(&self) -> Option<u64> {self.started.then(|| self.played.fetch_add(self.per_poll, Ordering::SeqCst) + self.per_poll)}+ fn note(&self) -> Option<String> {+ self.note.clone()+ }}/// Play for `steps` × 0.25 s of simulated time with the given output; the playhead in seconds.@@ -41,7 +45,7 @@ fn play_with(per_poll: u64, steps: usize) -> (f64, bool) {s.execute("file.openDemoProject", json!({})).unwrap();s.set_playhead(Tick::ZERO);let mut app = FilmcraftApp::new(s);- app.audio = Some(Box::new(FakeOut { played: Arc::new(AtomicU64::new(0)), per_poll, started: false }));+ app.audio = Some(Box::new(FakeOut { played: Arc::new(AtomicU64::new(0)), per_poll, started: false, note: None }));let mut h = Harness::builder().with_size(egui::vec2(1280.0, 800.0)).with_step_dt(0.25).build_eframe(move |_cc| app);h.step();h.state_mut().play(1.0);@@ -68,3 +72,23 @@ fn a_playing_audio_device_drives_the_clock() {assert!(t > 0.2, "{t:.2} s");assert!(audio_clock);}++/// A fallback note from the audio device (broken ALSA `default`, #23/#106) reaches the status+/// bar on the first frame, and survives while playback runs.+#[test]+fn a_fallback_note_from_the_audio_device_reaches_the_status_bar() {+ let mut s = Session::default();+ s.execute("file.openDemoProject", json!({})).unwrap();+ s.set_playhead(Tick::ZERO);+ let note = "Audio hardware: the default output device cannot be opened; using 'sofhdadsp'".to_string();+ let mut app = FilmcraftApp::new(s);+ app.audio = Some(Box::new(FakeOut { played: Arc::new(AtomicU64::new(0)), per_poll: 4_800, started: false, note: Some(note.clone()) }));+ let mut h = Harness::builder().with_size(egui::vec2(1280.0, 800.0)).with_step_dt(0.25).build_eframe(move |_cc| app);+ h.step();+ assert_eq!(h.state_mut().ui.status, note, "the note surfaces on the first frame (apply_prefs)");+ h.state_mut().play(1.0);+ for _ in 0..16 {+ h.step();+ }+ assert_eq!(h.state_mut().ui.status, note, "the note survives while playback runs");+}