Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

playback: a stalled audio device no longer freezes the playhead (#136)

carlitosteso30da0a3430adc
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index e7779a907..9e5c39f47 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -126,6 +126,9 @@ pub struct Playback {
anchor_tick: Tick,
/// Audio frames played at anchor (when the audio clock drives).
pub audio_clock: bool,
+ /// The audio clock's last reading and when (egui time, s) it last moved: a device that stops
+ /// consuming samples hands the clock back to the wall clock (see [`AUDIO_STALL_S`]).
+ audio_seen: (u64, f64),
/// Audio underruns for the current (or last) play (desktop: sound is mixed ahead).
#[cfg(not(target_arch = "wasm32"))]
pub audio_stats: std::sync::Arc<play_ahead::AudioStats>,
@@ -141,6 +144,12 @@ pub struct Playback {
pub stop_at: Option<Tick>,
}
+/// How long (s) the audio clock may stand still during playback before the wall clock takes over.
+/// An output stream can open and then never call back (ALSA with a busy or misconfigured device,
+/// #136); playback must not freeze on it. Devices that are slow to start (Bluetooth) stay well
+/// under this.
+pub const AUDIO_STALL_S: f64 = 1.0;
+
/// How long `ui.screenshot` waits for the window to present the frame.
const SCREENSHOT_TIMEOUT_S: f64 = 10.0;
/// How long `ui.screenshot` waits for the UI to settle (monitors showing their exact frames, the
@@ -667,6 +676,7 @@ impl FilmcraftApp {
self.playback.preroll = None;
self.playback.anchor_time = now;
self.playback.anchor_tick = self.session.playhead();
+ self.playback.audio_seen = (0, now);
self.start_audio();
// Audio Track Mixer: an automation pass runs while playing forward in real time
if (self.playback.speed - 1.0).abs() < 1e-9 && !self.session.mixrec.active() {
@@ -754,13 +764,27 @@ impl FilmcraftApp {
self.playback.anchor_time = now;
}
let rate = self.session.sequence_rate();
- let elapsed = if self.playback.audio_clock {
- match self.audio.as_ref().and_then(|a| a.played_frames().map(|f| (f, a.sample_rate()))) {
- Some((f, sr)) => f as f64 / sr as f64,
- None => now - self.playback.anchor_time,
+ let reading = if self.playback.audio_clock { self.audio.as_ref().and_then(|a| a.played_frames().map(|f| (f, a.sample_rate()))) } else { None };
+ if let Some((f, sr)) = reading {
+ if f != self.playback.audio_seen.0 {
+ self.playback.audio_seen = (f, now);
+ } else if now - self.playback.audio_seen.1 >= AUDIO_STALL_S {
+ // the device stopped consuming samples: continue from where the audio got to on
+ // the wall clock, without sound
+ let played = if sr > 0 { f as f64 / sr as f64 } else { 0.0 };
+ self.playback.anchor_tick += Tick::from_seconds_f64(played * self.playback.speed);
+ self.playback.anchor_time = now;
+ self.playback.audio_clock = false;
+ if let Some(a) = self.audio.as_mut() {
+ a.stop();
+ }
+ log::warn!("audio output stalled (no samples consumed for {AUDIO_STALL_S} s); playing without sound");
+ self.ui.status = "Audio output is not responding: playing without sound (check Settings ▸ Audio Hardware)".into();
}
- } else {
- now - self.playback.anchor_time
+ }
+ let elapsed = match reading {
+ Some((f, sr)) if self.playback.audio_clock && sr > 0 => f as f64 / sr as f64,
+ _ => now - self.playback.anchor_time,
};
let t = self.playback.anchor_tick + Tick::from_seconds_f64(elapsed * self.playback.speed);
let seq = self.session.active_sequence();
diff --git a/crates/ui-egui/tests/playback_clock_ui.rs b/crates/ui-egui/tests/playback_clock_ui.rs
new file mode 100644
index 000000000..81570d161
--- /dev/null
+++ b/crates/ui-egui/tests/playback_clock_ui.rs
@@ -0,0 +1,70 @@
+//! Playback clock: the audio device drives the playhead while it plays, and a device that opens
+//! but never consumes samples (seen on Linux with ALSA, #136) must not freeze playback.
+
+use std::sync::Arc;
+use std::sync::atomic::{AtomicU64, Ordering};
+
+use egui_kittest::Harness;
+use filmcraft_engine::Session;
+use filmcraft_time::Tick;
+use filmcraft_ui_egui::{AudioOut, FilmcraftApp};
+use serde_json::json;
+
+/// An output whose stream opens, then plays `per_poll` frames each time the clock is read
+/// (0 = a stalled device that never calls back).
+struct FakeOut {
+ played: Arc<AtomicU64>,
+ per_poll: u64,
+ started: bool,
+}
+
+impl AudioOut for FakeOut {
+ fn start(&mut self, _fill: Box<dyn FnMut(&mut [f32], usize) + Send>) -> Result<u32, String> {
+ self.played.store(0, Ordering::SeqCst);
+ self.started = true;
+ Ok(48_000)
+ }
+ fn stop(&mut self) {
+ self.started = false;
+ }
+ fn sample_rate(&self) -> u32 {
+ 48_000
+ }
+ fn played_frames(&self) -> Option<u64> {
+ self.started.then(|| self.played.fetch_add(self.per_poll, Ordering::SeqCst) + self.per_poll)
+ }
+}
+
+/// Play for `steps` × 0.25 s of simulated time with the given output; the playhead in seconds.
+fn play_with(per_poll: u64, steps: usize) -> (f64, bool) {
+ let mut s = Session::default();
+ 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 }));
+ 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);
+ for _ in 0..steps {
+ h.step();
+ }
+ let app = h.state_mut();
+ (app.session.playhead().seconds(), app.playback.audio_clock)
+}
+
+/// #136: the stream opened, the device never asked for samples, the audio clock stayed at 0 and
+/// the playhead never moved. After a short grace period playback falls back to the wall clock.
+#[test]
+fn a_stalled_audio_device_does_not_freeze_playback() {
+ let (t, audio_clock) = play_with(0, 16);
+ assert!(t > 1.5, "the playhead should move with the wall clock, it is at {t:.2} s");
+ assert!(!audio_clock, "the stalled device no longer drives the clock");
+}
+
+/// Control: a device that plays keeps driving the clock (0.1 s of audio per poll).
+#[test]
+fn a_playing_audio_device_drives_the_clock() {
+ let (t, audio_clock) = play_with(4_800, 8);
+ assert!(t > 0.2, "{t:.2} s");
+ assert!(audio_clock);
+}