EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Merge main into fix/947-preview-opening-audio
tylerjenningsw0a2842be85c5
diff --cc crates/ui-egui/src/lib.rsindex b41011178,7f8e02683..6f610390d--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@@ -52,29 -65,129 +65,152 @@@ const MAX_SHOT_TOTAL_BYTES: usize = 25/// [`EffectcraftApp::editing`]).const EDIT_QUIET: f64 = 0.3;+/// Longest a preview with sound holds its first frame, once that frame is cached, waiting for the+/// frames ahead (seconds, #947). A frame ahead that never gets cached mustn't hold it for ever:+/// the sound starts from the first frame anyway, and the preview goes on as a stalled one does.+const STARTUP_WAIT: f64 = 2.0;++/// Longest a preview with sound holds its first frame in all (seconds, #947). The first frame+/// itself can take a while (a heavy comp, a busy machine), and [`STARTUP_WAIT`] counts only from+/// when it is cached; but one that never gets cached (larger than the RAM preview, which the size+/// estimate can't always tell) mustn't hold it for ever. The preview then goes on as a stalled+/// one does: its sound starts once the frames are cached.+const STARTUP_LIMIT: f64 = 10.0;++/// How many frames from the first a preview with sound waits for before it starts (#947): the+/// prefetch's `ahead`, but no more than its `window` requests from the current frame (offsets+/// `0..window`), and no more than fit in the RAM preview `budget` (90 %, as for the prefetch) at+/// `frame_bytes`, the size frames really render at; at least the current frame, which the viewer+/// always requests. Waiting for a frame that is never requested, or that can't stay cached with+/// the others, would hold the first frame for ever.+fn startup_frames(ahead: i64, window: i64, budget: usize, frame_bytes: usize) -> i64 {+ let fit = (budget as f64 * 0.9 / frame_bytes.max(1) as f64) as i64;+ ahead.min(window).min(fit).max(1)+}++ /// The application's monotonic clock, in seconds.+ ///+ /// Neither `ctx.input(|i| i.time)` nor `RawInput::time` is valid in a logic-only tick:+ /// `Context::run_logic` copies only the viewport information, so both are the previous full+ /// pass's values. Playback, auto-save, screenshot deadlines and the control channel measure time+ /// with this clock instead. Native and web use the same [`web_time::Instant`] source; tests+ /// inject [`AppClock::manual`] and advance it.+ #[derive(Clone, Debug)]+ pub struct AppClock {+ origin: web_time::Instant,+ manual: Option<f64>,+ }++ impl AppClock {+ /// The host's monotonic clock.+ pub fn real() -> Self {+ Self { origin: web_time::Instant::now(), manual: None }+ }++ /// A clock tests advance explicitly with [`AppClock::advance`].+ pub fn manual() -> Self {+ Self { origin: web_time::Instant::now(), manual: Some(0.0) }+ }++ /// Seconds since the clock started (monotonic on native and in the browser).+ pub fn now(&self) -> f64 {+ match self.manual {+ Some(t) => t,+ None => self.origin.elapsed().as_secs_f64(),+ }+ }++ /// Move a manual clock forward; a real clock ignores this.+ pub fn advance(&mut self, seconds: f64) {+ if let Some(t) = &mut self.manual {+ *t = (*t + seconds.max(0.0)).max(0.0);+ }+ }++ /// Whether the clock is test-driven.+ pub fn is_manual(&self) -> bool {+ self.manual.is_some()+ }+ }++ impl Default for AppClock {+ fn default() -> Self {+ Self::real()+ }+ }++ /// The root surface geometry of the last completed (non-discarded) UI pass.+ ///+ /// The window position is deliberately excluded: moving the window does not change the pixels+ /// a screenshot captures. Size and scale-factor changes advance [`Self::epoch`].+ #[derive(Clone, Copy, Debug, PartialEq)]+ pub struct DrawGeometry {+ pub rect: egui::Rect,+ pub ppp: f32,+ /// Bumped whenever `rect.size()` or `ppp` differs from the previous recorded pass.+ pub epoch: u64,+ }++ /// Whether a captured image's dimensions match `rect` at `ppp`.+ ///+ /// The surface may be smaller than requested when the adapter-limit fit (fork E2) applies, but a+ /// different aspect ratio means the pixels came from another layout than the one issued.+ fn shot_image_matches(image: &egui::ColorImage, rect: egui::Rect, ppp: f32) -> bool {+ let [w, h] = image.size;+ if w == 0 || h == 0 || !ppp.is_finite() || ppp <= 0.0 {+ return false;+ }+ let ew = (rect.width() as f64 * ppp as f64).max(1.0);+ let eh = (rect.height() as f64 * ppp as f64).max(1.0);+ let want = ew / eh;+ let got = w as f64 / h as f64;+ (got - want).abs() <= 0.02 * want.max(1.0)+ }++ /// What a `ui.screenshot` captures.+ ///+ /// A panel or element is an identity, not a rectangle: it is resolved again when the capture is+ /// issued, after the settle passes, so a panel that moved or resized while the UI settled is+ /// captured where it is now (never with the rectangle from request admission).+ #[derive(Clone, Debug, PartialEq)]+ pub enum ShotTarget {+ /// The whole root surface.+ Window,+ /// A fixed rectangle in logical points, relative to the root surface's top-left corner.+ Rect([f32; 4]),+ /// One dock panel's automation rectangle (`panel.<id>`).+ Panel(PanelKind),+ /// One automation element id's rectangle.+ Id(String),+ }++ /// One `ui.screenshot` request: its reply, its admission reservation and its completion slot.+ struct PendingShot {+ path: Option<String>,+ /// What to capture; resolved afresh at every issuance.+ target: ShotTarget,+ /// The crop resolved at issuance, **relative to the issued rect's origin** (the image's+ /// origin); `None` captures the whole surface.+ crop: Option<[f32; 4]>,+ reply: Sender<serde_json::Value>,+ /// App-clock instant after which the request fails instead of waiting.+ deadline: f64,+ /// App-clock instant before which the UI must have settled.+ settle_at: f64,+ /// Root geometry and geometry epoch the reservation was admitted against.+ admitted: DrawGeometry,+ /// Final UI passes completed when the request was admitted.+ admitted_pass: u64,+ /// The geometry epoch bound to the issued capture command, if it was issued.+ issued: Option<DrawGeometry>,+ /// The shared completion slot; dropping it cancels a late callback.+ slot: Option<craft_ui::capture::CaptureSlot>,+ /// Bytes reserved against [`EffectcraftApp::shot_bytes`].+ reserved_bytes: usize,+ /// Geometry retries used.+ attempts: u32,+ }+/// Modal dialogs.#[derive(Clone, Copy, Debug, PartialEq, Eq)]pub enum Dialog {@@@ -1196,11 -1430,53 +1470,56 @@@ impl EffectcraftApplet video = plan.is_none_or(|p| p.video);// Prefetch ahead.let cur = fr.frame_at(self.session.time());- // While paused (scrubbing, editing) the viewer's frame comes first: prefetch only a few- // frames, only once it is done, and not while edits keep coming (a layer dragged in the- // viewer): those frames would be stale at the next step and would hold the CPU and GPU- // the next viewer frame needs.- let ahead = if self.playback.playing {+ let ahead = self.prefetch_ahead(ctx, now);+ let queued = self.frames.inflight();+ let series = self.frame_series(cid, scale);+ // The preview's position orders prefetch and eviction (#595).+ let playhead = plan.filter(|p| p.video).map(|p| frames::Playhead { series, cur, start: p.start, end: p.end, step: p.step, looping: p.looping });+ self.frames.set_playhead(playhead);+ let pass = PlaybackPass {+ ctx,+ comp: &c,+ cid,+ scale,+ now,+ frame_rate: fr,+ series,+ plan,+ window: FrameWindow { first: wa, last: wb },+ cur,+ video,+ looping,+ ahead,+ queued,+ };+ if self.cache_preview(&pass) {+ return;+ }+ let step = plan.map_or(1, |p| p.step);+ let fit_ahead = self.frames_that_fit(&c, scale) - 1;+ self.prefetch_playback(&pass, fit_ahead);+ self.pump_meters(ctx, now);+ if !self.playback.playing {+ self.cache_when_idle(ctx, &c, &series, now, input_active, (wa, wb));+ return;+ }+ let fit = self.frames_that_fit(&c, scale);++ if self.hold_first_frame(&pass, fit) {++ return;++ }+ self.start_held_audio(&pass, fit);+ if self.advance_audio_clock(&pass, step) {+ return;+ }+ self.advance_frame_clock(&pass, step);+ }++ /// While paused (scrubbing, editing) the viewer's frame comes first: prefetch only a few+ /// frames, only once it is done, and not while edits keep coming (a layer dragged in the+ /// viewer): those frames would be stale at the next step and would hold the CPU and GPU+ /// the next viewer frame needs.+ fn prefetch_ahead(&mut self, ctx: &egui::Context, now: f64) -> i64 {+ if self.playback.playing {(self.frames_parallelism() * 2).max(4) as i64} else if self.frames.urgent_pending() {0@@@ -1297,56 -1586,17 +1629,63 @@@self.meter.update([0.0; 2], now);ctx.request_repaint();}- if !self.playback.playing {- self.cache_when_idle(ctx, &c, &series, now, (wa, wb));- return;+ }+- /// Held sound starts once the frames ahead are cached (see `Playback::audio_held`): the- /// prefetch window when the silent preview keeps real time, else the rest of the preview.++ /// Starting a preview with sound: hold the first frame until the frames ahead are cached, then++ /// start the sound from that same frame (see `Playback::starting`). Without an output device++ /// the picture plays on its own clock from it. How many frames ahead: only ones++ /// `prefetch_playback` requests and that fit in the RAM preview at the size they really render++ /// (an Extended Viewer frame is larger than the comp), so the wait always ends. Returns true++ /// while it holds the first frame.++ fn hold_first_frame(&mut self, pass: &PlaybackPass<'_>, fit: i64) -> bool {++ let PlaybackPass { ctx, comp, scale, now, plan, series, cur, ahead, frame_rate: fr, .. } = *pass;++ if !self.playback.starting {++ return false;+ }- let snap = |f: i64| wa + (f - wa).div_euclid(step) * step;- let fit = self.frames_that_fit(&c, scale);- // Starting with sound: hold the first frame until the frames ahead are cached, then start- // the sound from that same frame (see `Playback::starting`). Without an output device the- // picture plays on its own clock from it. How many frames ahead: only ones the prefetch- // above requests and that fit in the RAM preview at the size they really render (an- // Extended Viewer frame is larger than the comp), so the wait always ends.- if self.playback.starting- && let Some(p) = plan- {- self.playback.start_wall = now;- self.playback.start_frame = cur;- let need = self.frames.get(&FrameKey { frame: cur, ..series }).map(|img| {- let [w, h] = img.size();- startup_frames(ahead, window, self.frames.budget(), w.saturating_mul(h).saturating_mul(4))- });- if need.is_some() && self.playback.first_cached.is_none() {- self.playback.first_cached = Some(now);- }- // Don't wait for frames the RAM preview can't hold (not even one frame fits), for the- // frames ahead longer than STARTUP_WAIT once the first is in, nor longer than- // STARTUP_LIMIT in all (a frame that never gets cached).- let give_up = (self.frames.budget() as f64) < self.frame_bytes(&c, scale)- || self.playback.first_cached.is_some_and(|t| now - t >= STARTUP_WAIT)- || now - self.playback.starting_since >= STARTUP_LIMIT;- if !give_up && !need.is_some_and(|n| self.cached_ahead(&p, &series, cur, n)) {- self.playback.waiting = true;- ctx.request_repaint();- return;- }- self.playback.starting = false;- self.playback.waiting = false;- // Not on a frame that isn't cached: the stall below would stop the sound at once. The- // preview goes on as a stalled one does, and the sound starts once frames are cached.- if need.is_some() {- self.playback.audio_held = false;- self.start_audio(fr.tick_of(cur));- }++ let Some(p) = plan else { return false };++ self.playback.start_wall = now;++ self.playback.start_frame = cur;++ // `prefetch_playback` requests every uncached frame of offsets 0..window from here.++ let window = (ahead * 3).min(fit - 1);++ let need = self.frames.get(&FrameKey { frame: cur, ..series }).map(|img| {++ let [w, h] = img.size();++ startup_frames(ahead, window, self.frames.budget(), w.saturating_mul(h).saturating_mul(4))++ });++ if need.is_some() && self.playback.first_cached.is_none() {++ self.playback.first_cached = Some(now);++ }++ // Don't wait for frames the RAM preview can't hold (not even one frame fits), for the++ // frames ahead longer than STARTUP_WAIT once the first is in, nor longer than++ // STARTUP_LIMIT in all (a frame that never gets cached).++ let give_up = (self.frames.budget() as f64) < self.frame_bytes(comp, scale)++ || self.playback.first_cached.is_some_and(|t| now - t >= STARTUP_WAIT)++ || now - self.playback.starting_since >= STARTUP_LIMIT;++ if !give_up && !need.is_some_and(|n| self.cached_ahead(&p, &series, cur, n)) {++ self.playback.waiting = true;++ ctx.request_repaint();++ return true;+ }- // Held sound starts once the frames ahead are cached again after a stall (see- // `Playback::audio_held`): the prefetch window when the silent preview keeps real time,- // else the rest of the preview.- if self.playback.audio_held- && let Some(p) = plan- && ((self.playback.keeps_up(now) && self.cached_ahead(&p, &series, cur, ahead.min(fit))) || self.cached_ahead(&p, &series, cur, fit))- {++ self.playback.starting = false;++ self.playback.waiting = false;++ // Not on a frame that isn't cached: the stall below would stop the sound at once. The++ // preview goes on as a stalled one does, and the sound starts once frames are cached.++ if need.is_some() {++ self.playback.audio_held = false;++ self.start_audio(fr.tick_of(cur));++ }++ false++ }++++ /// Held sound starts once the frames ahead are cached again after a stall (see++ /// `Playback::audio_held`): the prefetch window when the silent preview keeps real time, else++ /// the rest of the preview.+ fn start_held_audio(&mut self, pass: &PlaybackPass<'_>, fit: i64) {+ let PlaybackPass { now, plan, series, cur, ahead, frame_rate: fr, .. } = *pass;+ if !self.playback.audio_held {+ return;+ }+ let Some(p) = plan else { return };+ if (self.playback.keeps_up(now) && self.cached_ahead(&p, &series, cur, ahead.min(fit))) || self.cached_ahead(&p, &series, cur, fit) {self.playback.audio_held = false;self.playback.start_wall = now;self.playback.start_frame = cur;@@@ -2229,27 -2886,520 +2979,545 @@@ mod appearance_tests}}+#[cfg(test)]+mod startup_tests {+ use super::startup_frames;++ /// The frames a preview with sound waits for are always requested and fit together (#947).+ #[test]+ fn startup_waits_only_for_frames_it_requests_and_can_keep() {+ // A 64×36 frame: 9216 bytes. A 46 080-byte budget fits 4 of them (90 %); the prefetch+ // window then requests offsets 0..3, so the start needs those 3, not 4 it never asks for.+ let frame = 64 * 36 * 4;+ let fit = 4;+ let window = (32i64 * 3).min(fit - 1);+ assert_eq!(startup_frames(32, window, 46_080, frame), 3);+ // A roomy budget: the prefetch's `ahead` frames.+ assert_eq!(startup_frames(8, 24, 100_000_000, frame), 8);+ // Extended Viewer: a 160×120 comp renders 320×248 frames. 952 320 bytes fit 11 comp-sized+ // frames but only 2 real ones (90 %): wait for 2, not 4.+ assert_eq!(startup_frames(4, 11, 952_320, 320 * 248 * 4), 2);+ // Nothing fits, or no window: still the current frame, never 0.+ assert_eq!(startup_frames(8, 0, 0, frame), 1);+ // A zero frame size counts as 1 byte: no division by zero.+ assert_eq!(startup_frames(8, 24, 10, 0), 8);+ }+}+++ #[cfg(test)]+ mod lifecycle_tests {+ //! The logic-only / UI-pass split: queue work in `tick`, input, layout and setup in `draw`.+ //!+ //! The driver mirrors eframe's order for a hidden window: `raw_input_hook` prepares the raw+ //! input, a logic-only iteration runs `tick`, and a forced pass runs `draw` with the same+ //! prepared input. Nothing may depend on `current_pass_index`, which is zero in both.++ use super::*;+ use effectcraft_engine::config::{ConfigStore, MemoryConfig};++ fn app_with_comp() -> EffectcraftApp {+ let mut session = Session::default();+ session.execute("comp.new", json!({"name": "Lifecycle", "width": 64, "height": 36, "duration": 1})).unwrap();+ session.execute("layer.newSolid", json!({"color": "#ff0000"})).unwrap();+ let mut app = EffectcraftApp::new(session);+ app.ui.start_screen = false;+ app.ui.show_menu_bar = false;+ app.use_manual_clock();+ app+ }++ fn raw(size: egui::Vec2) -> egui::RawInput {+ egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, size)), ..Default::default() }+ }++ fn tick_with(app: &mut EffectcraftApp, ctx: &egui::Context, frame: &eframe::Frame, raw: &egui::RawInput) -> egui::LogicOutput {+ ctx.run_logic(raw, |ctx| app.tick(ctx, frame))+ }++ fn hidden_tick(app: &mut EffectcraftApp, ctx: &egui::Context, frame: &eframe::Frame) {+ let _ = tick_with(app, ctx, frame, &raw(egui::vec2(800.0, 480.0)));+ }++ /// One host iteration that ends in a UI pass (the forced hidden pass, or a visible frame).+ fn pass(app: &mut EffectcraftApp, ctx: &egui::Context, frame: &eframe::Frame, raw: &mut egui::RawInput) -> egui::FullOutput {+ eframe::App::raw_input_hook(app, ctx, raw);+ let mut out = ctx.run_ui(raw.take(), |ui| app.draw(ui, frame));+ out.textures_delta.clear();+ out+ }++ fn ready(app: &mut EffectcraftApp, ctx: &egui::Context, frame: &eframe::Frame) -> DrawGeometry {+ // The first pass installs the fonts, the second draws with them.+ for _ in 0..3 {+ let mut raw = raw(egui::vec2(800.0, 480.0));+ pass(app, ctx, frame, &mut raw);+ }+ assert!(app.is_ready(), "fonts are ready after the warm-up passes");+ app.draw_geometry().expect("a completed pass records its geometry")+ }++ fn send(app: &mut EffectcraftApp, method: &str, params: serde_json::Value) -> std::sync::mpsc::Receiver<serde_json::Value> {+ let (request, reply) = ControlRequest::new(method, params);+ let (tx, rx) = std::sync::mpsc::channel();+ tx.send(request).unwrap();+ app.control_rx = Some(rx);+ reply+ }++ #[test]+ fn a_logic_only_tick_installs_no_fonts_and_records_no_geometry() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ hidden_tick(&mut app, &ctx, &frame);+ assert!(!app.is_ready(), "fonts need a real pass, not a logic-only tick");+ assert!(app.draw_geometry().is_none(), "a logic-only tick is not a frame");+ assert_eq!(app.ui_passes(), 0);+ }++ #[test]+ fn a_control_command_that_needs_no_ui_completes_without_a_pass() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ let reply = send(&mut app, "engine.execute", json!({"command": "comp.new", "params": {"name": "Queued", "width": 32, "height": 18, "duration": 1}}));+ hidden_tick(&mut app, &ctx, &frame);+ let value = reply.try_recv().expect("a command that needs no UI is answered by the tick");+ assert_eq!(value["ok"], true, "{value}");+ assert_eq!(app.ui_passes(), 0);+ }++ #[test]+ fn a_pass_that_cannot_draw_receives_no_synthetic_input() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ assert!(!app.is_ready(), "a fresh app installs its fonts in its first pass");+ let reply = send(&mut app, "ui.click", json!({"x": 40, "y": 40}));+ hidden_tick(&mut app, &ctx, &frame);+ assert!(reply.try_recv().is_err(), "a click is not answered before its UI pass");+ // The font-install pass cannot draw. It must not receive the click: `run_ui` applies what+ // the hook feeds, and a batch cannot be put back without applying its pointer state twice.+ let mut setup = raw(egui::vec2(800.0, 480.0));+ eframe::App::raw_input_hook(&mut app, &ctx, &mut setup);+ assert!(setup.events.is_empty(), "a pass that cannot draw gets no synthetic input");+ assert_eq!(app.synthetic.len(), 5, "the whole gesture still waits for a drawing pass");+ assert!(app.pending_synthetic_events().is_empty());+ let mut setup_pass = ctx.run_ui(setup.take(), |ui| app.draw(ui, &frame));+ setup_pass.textures_delta.clear();+ assert!(!app.is_ready(), "the pass that installs the fonts draws nothing");+ // Once fonts are ready the click reaches the UI exactly once, one event per pass.+ let mut passes = 0;+ while reply.try_recv().is_err() {+ passes += 1;+ assert!(passes < 40, "the click must be applied within a few passes");+ let mut raw = raw(egui::vec2(800.0, 480.0));+ pass(&mut app, &ctx, &frame, &mut raw);+ }+ assert!(reply.try_recv().is_err(), "the reply is sent once");+ assert!(app.pending_synthetic_events().is_empty());+ assert!(app.synthetic.is_empty(), "every event was applied once");+ }++ #[test]+ fn one_pending_synthetic_batch_is_never_duplicated_while_hidden() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ ready(&mut app, &ctx, &frame);+ let reply = send(&mut app, "ui.click", json!({"x": 40, "y": 40}));+ hidden_tick(&mut app, &ctx, &frame);+ assert!(reply.try_recv().is_err(), "a click is not answered before its UI pass");+ // Several hidden host iterations: eframe keeps the first prepared input; only its one+ // event may reach the raw input, and a later hook must not add another.+ let mut first = raw(egui::vec2(800.0, 480.0));+ eframe::App::raw_input_hook(&mut app, &ctx, &mut first);+ assert_eq!(first.events.len(), 1, "the first event of the gesture is fed");+ let mut second = raw(egui::vec2(800.0, 480.0));+ eframe::App::raw_input_hook(&mut app, &ctx, &mut second);+ assert!(second.events.is_empty(), "an unapplied batch blocks the next event");+ assert_eq!(app.pending_synthetic_events().len(), 1);+ // The pass that applies the batch acknowledges it once; it is never fed again.+ pass(&mut app, &ctx, &frame, &mut first);+ assert!(app.pending_synthetic_events().is_empty());+ // Drive real passes until the gesture is applied: exactly one reply, no lost events.+ let mut passes = 0;+ while reply.try_recv().is_err() {+ passes += 1;+ assert!(passes < 40, "the click must be applied within a few passes");+ let mut raw = raw(egui::vec2(800.0, 480.0));+ pass(&mut app, &ctx, &frame, &mut raw);+ }+ assert!(app.pending_synthetic_events().is_empty());+ assert!(app.synthetic.is_empty());+ assert!(reply.try_recv().is_err(), "the reply is sent once");+ }++ #[test]+ fn geometry_epoch_tracks_size_and_scale_but_not_window_position() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ let first = ready(&mut app, &ctx, &frame);+ let mut moved = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::pos2(120.0, 80.0), first.rect.size())), ..Default::default() };+ pass(&mut app, &ctx, &frame, &mut moved);+ assert_eq!(app.draw_geometry().unwrap().epoch, first.epoch, "moving the window does not change a capture");+ let mut resized = raw(egui::vec2(1024.0, 640.0));+ pass(&mut app, &ctx, &frame, &mut resized);+ let after_resize = app.draw_geometry().unwrap();+ assert!(after_resize.epoch > first.epoch, "a resize advances the epoch");+ let mut back = raw(egui::vec2(800.0, 480.0));+ pass(&mut app, &ctx, &frame, &mut back);+ assert!(app.draw_geometry().unwrap().epoch > after_resize.epoch, "changing back is another change, not the old epoch");+ }++ #[test]+ fn only_a_viewer_that_draws_records_a_prefetch_scale() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ ready(&mut app, &ctx, &frame);+ assert!(app.last_viewer_scale.is_some(), "the visible viewer recorded the scale it drew at");+ // The Home screen draws no viewer: the old comp's scale must not keep idle prefetch alive.+ app.ui.start_screen = true;+ let mut input = raw(egui::vec2(800.0, 480.0));+ pass(&mut app, &ctx, &frame, &mut input);+ assert!(app.last_viewer_scale.is_none(), "no viewer drew, so there is no scale to prefetch at");+ // Back to the comp: the next pass records it again.+ app.ui.start_screen = false;+ let mut input = raw(egui::vec2(800.0, 480.0));+ pass(&mut app, &ctx, &frame, &mut input);+ assert!(app.last_viewer_scale.is_some());+ }++ #[test]+ fn playback_advances_on_a_logic_only_tick() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ app.playback.playing = true;+ app.playback.plan = Some(effectcraft_engine::preview::PreviewPlan {+ start: 1,+ end: 1,+ first: 1,+ step: 1,+ fps: 24.0,+ looping: false,+ cache_first: false,+ video: false,+ audio: false,+ origin: Tick::ZERO,+ });+ app.playback.start_wall = 0.0;+ app.playback.start_frame = 1;+ app.clock_mut().advance(1.0);+ hidden_tick(&mut app, &ctx, &frame);+ assert!(!app.playback.playing, "a one-frame preview ends without any UI pass");+ assert_eq!(app.playback.audio_frame, Some(1), "the audio-only playhead reached the plan's end");+ assert_eq!(app.ui_passes(), 0);+ }++ #[test]+ fn workspace_layout_autosave_writes_on_a_hidden_tick() {+ let store = Arc::new(MemoryConfig::default());+ let mut app = EffectcraftApp::new(Session { config: Some(store.clone()), ..Default::default() });+ app.ui.start_screen = false;+ app.use_manual_clock();+ app.ui.focused = PanelKind::Timeline;+ app.clock_mut().advance(2.0);+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ hidden_tick(&mut app, &ctx, &frame);+ assert!(store.read(WORKSPACES_FILE).is_some(), "the layout is saved while the window is hidden");+ assert_eq!(app.ui_passes(), 0);+ }++ #[test]+ fn the_close_guard_answers_a_close_request_without_a_pass() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ assert!(app.session.is_dirty(), "creating a comp and a layer leaves unsaved changes");+ let mut raw = raw(egui::vec2(800.0, 480.0));+ raw.viewports.insert(egui::ViewportId::ROOT, egui::ViewportInfo { events: vec![egui::ViewportEvent::Close], ..Default::default() });+ let out = tick_with(&mut app, &ctx, &frame, &raw);+ assert!(app.dialog.is_some(), "a hidden close request opens the unsaved-changes prompt");+ let cancel = out.viewport_commands.values().flatten().any(|c| matches!(c, egui::ViewportCommand::CancelClose));+ assert!(cancel, "the window stays open while the prompt is asking");+ assert_eq!(app.ui_passes(), 0);+ }++ // ---------------------------------------------------------------- screenshots++ fn screenshot_request(+ app: &mut EffectcraftApp,+ ctx: &egui::Context,+ path: Option<&str>,+ target: ShotTarget,+ ) -> std::sync::mpsc::Receiver<serde_json::Value> {+ let (tx, rx) = std::sync::mpsc::channel();+ app.admit_screenshot(ctx, path.map(str::to_string), target, tx);+ rx+ }++ fn shot_callbacks(out: &egui::LogicOutput) -> Vec<egui::ScreenshotCallback> {+ out.viewport_commands+ .values()+ .flatten()+ .filter_map(|c| match c {+ egui::ViewportCommand::Screenshot(cb) => Some(cb.clone()),+ _ => None,+ })+ .collect()+ }++ /// Three settled passes and the settle delay, then the tick that issues the capture.+ fn issue_shot(app: &mut EffectcraftApp, ctx: &egui::Context, frame: &eframe::Frame) -> egui::ScreenshotCallback {+ issue_shot_at(app, ctx, frame, egui::vec2(800.0, 480.0))+ }++ fn issue_shot_at(app: &mut EffectcraftApp, ctx: &egui::Context, frame: &eframe::Frame, size: egui::Vec2) -> egui::ScreenshotCallback {+ for _ in 0..SCREENSHOT_SETTLE_PASSES {+ let mut raw = raw(size);+ pass(app, ctx, frame, &mut raw);+ }+ app.clock_mut().advance(SCREENSHOT_SETTLE_S + 0.01);+ let out = tick_with(app, ctx, frame, &raw(size));+ let cbs = shot_callbacks(&out);+ assert_eq!(cbs.len(), 1, "one settled screenshot is issued once");+ cbs.into_iter().next().unwrap()+ }++ fn image(width: usize, height: usize) -> Arc<egui::ColorImage> {+ Arc::new(egui::ColorImage::new([width, height], vec![egui::Color32::from_rgb(10, 20, 30); width.checked_mul(height).unwrap()]))+ }++ #[test]+ fn a_screenshot_is_issued_only_after_settled_passes_and_writes_from_the_tick() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ ready(&mut app, &ctx, &frame);+ let path = std::env::temp_dir().join(format!("effectcraft-shot-{}-ok.png", std::process::id()));+ let _ = std::fs::remove_file(&path);+ let reply = screenshot_request(&mut app, &ctx, path.to_str(), ShotTarget::Window);+ // Not settled: a tick issues nothing and asks for passes instead.+ let out = tick_with(&mut app, &ctx, &frame, &raw(egui::vec2(800.0, 480.0)));+ assert!(shot_callbacks(&out).is_empty(), "no capture before the UI settles");+ assert_eq!(out.viewport_commands.values().flatten().filter(|c| matches!(c, egui::ViewportCommand::RequestPaintWhileHidden)).count(), 1);+ let cb = issue_shot(&mut app, &ctx, &frame);+ assert!(reply.try_recv().is_err(), "the callback must not write or reply itself");+ cb.complete(image(800, 480));+ hidden_tick(&mut app, &ctx, &frame);+ let value = reply.try_recv().expect("the tick drains the completed slot and replies");+ assert_eq!(value["ok"], true, "{value}");+ assert!(path.exists(), "{value}");+ assert_eq!(value["result"]["width"], 800);+ assert_eq!(value["result"]["height"], 480);+ assert_eq!(app.shot_bytes, 0, "the reservation is released");+ let _ = std::fs::remove_file(&path);+ }++ #[test]+ fn a_resize_between_issue_and_completion_rejects_the_pixels_and_retries() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ ready(&mut app, &ctx, &frame);+ let path = std::env::temp_dir().join(format!("effectcraft-shot-{}-retry.png", std::process::id()));+ let _ = std::fs::remove_file(&path);+ let reply = screenshot_request(&mut app, &ctx, path.to_str(), ShotTarget::Window);+ let first = issue_shot(&mut app, &ctx, &frame);+ let size = egui::vec2(1024.0, 640.0);+ let mut resized = raw(size);+ pass(&mut app, &ctx, &frame, &mut resized);+ first.complete(image(800, 480));+ let _ = tick_with(&mut app, &ctx, &frame, &raw(size));+ assert!(reply.try_recv().is_err(), "pixels from the old surface epoch are discarded");+ assert!(!path.exists(), "a stale image is never written");+ // The retry is issued against the new geometry and the new image is accepted.+ let second = issue_shot_at(&mut app, &ctx, &frame, size);+ second.complete(image(1024, 640));+ let _ = tick_with(&mut app, &ctx, &frame, &raw(size));+ let value = reply.try_recv().expect("the retried capture replies");+ assert_eq!(value["ok"], true, "{value}");+ assert_eq!(value["result"]["width"], 1024);+ let _ = std::fs::remove_file(&path);+ }++ #[test]+ fn screenshot_admission_bounds_requests_and_a_timeout_releases_its_reservation() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ ready(&mut app, &ctx, &frame);+ let mut replies = Vec::new();+ for _ in 0..MAX_PENDING_SHOTS {+ replies.push(screenshot_request(&mut app, &ctx, None, ShotTarget::Rect([0.0, 0.0, 4.0, 4.0])));+ }+ let refused = screenshot_request(&mut app, &ctx, None, ShotTarget::Rect([0.0, 0.0, 4.0, 4.0]));+ let value = refused.try_recv().expect("the fifth request is refused immediately");+ assert_eq!(value["ok"], false, "{value}");+ // Every queued request times out on a hidden window that presents no frame; each one+ // releases its reservation exactly once.+ app.clock_mut().advance(SCREENSHOT_TIMEOUT_S + 1.0);+ hidden_tick(&mut app, &ctx, &frame);+ for reply in replies {+ let value = reply.try_recv().expect("a timed-out request gets an actionable reply");+ assert_eq!(value["ok"], false, "{value}");+ assert!(value["error"].as_str().is_some_and(|e| e.contains("no frame")), "{value}");+ }+ assert_eq!(app.shot_bytes, 0);+ assert!(app.pending_screenshots.is_empty());+ }++ #[test]+ fn a_late_callback_after_cancellation_writes_nothing_and_replies_once() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ ready(&mut app, &ctx, &frame);+ let path = std::env::temp_dir().join(format!("effectcraft-shot-{}-late.png", std::process::id()));+ let _ = std::fs::remove_file(&path);+ let reply = screenshot_request(&mut app, &ctx, path.to_str(), ShotTarget::Window);+ let late = issue_shot(&mut app, &ctx, &frame);+ app.clock_mut().advance(SCREENSHOT_TIMEOUT_S + 1.0);+ hidden_tick(&mut app, &ctx, &frame);+ let timeout = reply.try_recv().expect("the request times out");+ assert_eq!(timeout["ok"], false, "{timeout}");+ late.complete(image(800, 480));+ hidden_tick(&mut app, &ctx, &frame);+ assert!(reply.try_recv().is_err(), "a late completion sends no second reply");+ assert!(!path.exists(), "a late completion writes no file");+ assert_eq!(app.shot_bytes, 0);+ }++ /// Every repaint delay requested on `ctx` (what a host uses to wake the app).+ fn wake_requests(ctx: &egui::Context) -> Arc<std::sync::Mutex<Vec<std::time::Duration>>> {+ let wakes: Arc<std::sync::Mutex<Vec<std::time::Duration>>> = Arc::default();+ let record = Arc::clone(&wakes);+ ctx.set_repaint_observer(move |i| record.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(i.delay));+ wakes+ }++ #[test]+ fn an_issued_capture_schedules_a_wake_up_for_its_deadline() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ ready(&mut app, &ctx, &frame);+ let reply = screenshot_request(&mut app, &ctx, None, ShotTarget::Window);+ for _ in 0..SCREENSHOT_SETTLE_PASSES {+ let mut raw = raw(egui::vec2(800.0, 480.0));+ pass(&mut app, &ctx, &frame, &mut raw);+ }+ app.clock_mut().advance(SCREENSHOT_SETTLE_S + 0.01);+ // Record the repaint requests of the tick that issues the capture only.+ let wakes = wake_requests(&ctx);+ let out = tick_with(&mut app, &ctx, &frame, &raw(egui::vec2(800.0, 480.0)));+ assert_eq!(shot_callbacks(&out).len(), 1, "the settled capture is issued once");+ let delays = wakes.lock().unwrap_or_else(std::sync::PoisonError::into_inner).clone();+ // The deadline is 8 s after admission; the capture was issued ~0.31 s in, so the app must+ // ask for a wake-up close to that timeout, not only for the next paint.+ let deadline_wake = delays.iter().any(|d| d.as_secs_f64() > 7.0 && d.as_secs_f64() <= SCREENSHOT_TIMEOUT_S);+ assert!(deadline_wake, "an issued capture asks the host to wake for its timeout: {delays:?}");+ // The callback never arrives (a hidden window or a display asleep). The host wakes at the+ // requested delay; without this wake-up the timeout below could never be evaluated.+ app.clock_mut().advance(SCREENSHOT_TIMEOUT_S + 1.0);+ hidden_tick(&mut app, &ctx, &frame);+ let value = reply.try_recv().expect("the deadline wake-up produces the timeout reply");+ assert_eq!(value["ok"], false, "{value}");+ assert!(value["error"].as_str().is_some_and(|e| e.contains("no frame")), "{value}");+ assert_eq!(app.shot_bytes, 0);+ assert!(app.pending_screenshots.is_empty());+ }++ #[test]+ fn a_downscaled_capture_is_cropped_with_the_images_own_scale() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ let geom = ready(&mut app, &ctx, &frame);+ assert_eq!((geom.rect.width(), geom.rect.height(), geom.ppp), (800.0, 480.0, 1.0));+ // The adapter-limit fit (E2) returned half the requested pixels. Left red, right blue.+ let fitted =+ Arc::new(egui::ColorImage::new([400, 240], (0..400 * 240).map(|i| if i % 400 < 200 { egui::Color32::RED } else { egui::Color32::BLUE }).collect()));+ let dir = std::env::temp_dir();+ for (name, crop, want) in [("left", [0.0, 0.0, 400.0, 240.0], egui::Color32::RED), ("right", [400.0, 0.0, 400.0, 240.0], egui::Color32::BLUE)] {+ let path = dir.join(format!("effectcraft-shot-{}-scale-{name}.png", std::process::id()));+ let _ = std::fs::remove_file(&path);+ let reply = screenshot_request(&mut app, &ctx, path.to_str(), ShotTarget::Rect(crop));+ let cb = issue_shot(&mut app, &ctx, &frame);+ cb.complete(Arc::clone(&fitted));+ hidden_tick(&mut app, &ctx, &frame);+ let value = reply.try_recv().expect("the downscaled capture replies");+ assert_eq!(value["ok"], true, "{value}");+ assert_eq!(value["result"]["width"], 200, "{value}");+ assert_eq!(value["result"]["height"], 120, "{value}");+ let png = std::fs::read(&path).expect("the PNG is written");+ let decoded = image::load_from_memory(&png).expect("our PNG decodes").to_rgba8();+ assert_eq!((decoded.width(), decoded.height()), (200, 120), "{value}");+ let px = decoded.get_pixel(100, 60).0;+ assert!(px[0] == want.r() && px[1] == want.g() && px[2] == want.b(), "{name}: {px:?} is not from its half of the fitted image");+ let _ = std::fs::remove_file(&path);+ }+ }++ #[test]+ fn a_capture_before_any_pass_never_uses_the_window_frame_rect() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ // Only the outer (title bar included) rectangle is known: it is not the captured surface.+ let outer = egui::Rect::from_min_size(egui::pos2(0.0, -30.0), egui::vec2(800.0, 510.0));+ let mut raw = egui::RawInput::default();+ raw.viewports.insert(egui::ViewportId::ROOT, egui::ViewportInfo { outer_rect: Some(outer), ..Default::default() });+ let _ = tick_with(&mut app, &ctx, &frame, &raw);+ let (tx, reply) = std::sync::mpsc::channel();+ app.admit_screenshot(&ctx, None, ShotTarget::Window, tx);+ let value = reply.try_recv().expect("the request is answered now");+ assert_eq!(value["ok"], false, "{value}");+ assert!(value["error"].as_str().is_some_and(|e| e.contains("geometry is not known")), "{value}");+ assert!(app.pending_screenshots.is_empty(), "nothing was reserved against the wrong rectangle");+ assert_eq!(app.shot_bytes, 0);+ assert!(app.want_paint, "the pass that records the geometry is requested");+ }++ #[test]+ fn an_element_that_moves_while_the_ui_settles_is_captured_where_it_is() {+ let ctx = egui::Context::default();+ let frame = eframe::Frame::_new_kittest();+ let mut app = app_with_comp();+ let geom = ready(&mut app, &ctx, &frame);+ // Any registered panel: the crop follows this element's rectangle.+ let panel = app.auto.previous.iter().chain(app.auto.elements.iter()).find(|e| e.id.starts_with("panel.")).map(|e| e.id.clone());+ let panel = panel.expect("a dock panel is on screen after a completed pass");+ let reply = screenshot_request(&mut app, &ctx, None, ShotTarget::Id(panel.clone()));+ for _ in 0..SCREENSHOT_SETTLE_PASSES {+ let mut raw = raw(egui::vec2(800.0, 480.0));+ pass(&mut app, &ctx, &frame, &mut raw);+ }+ // The panel moves after its last draw: the crop must follow it, not the admission rect.+ let moved = [40.0, 30.0, 300.0, 200.0];+ for list in [&mut app.auto.previous, &mut app.auto.elements] {+ if let Some(e) = list.iter_mut().find(|e| e.id == panel) {+ e.rect = moved;+ }+ }+ app.clock_mut().advance(SCREENSHOT_SETTLE_S + 0.01);+ let out = tick_with(&mut app, &ctx, &frame, &raw(egui::vec2(800.0, 480.0)));+ assert_eq!(shot_callbacks(&out).len(), 1, "the settled capture is issued once");+ let crop = app.pending_screenshots.first().and_then(|s| s.crop).expect("the element crop is resolved at issuance");+ assert!((crop[0] - (moved[0] - geom.rect.min.x)).abs() < 0.01, "{crop:?}");+ assert!((crop[1] - (moved[1] - geom.rect.min.y)).abs() < 0.01, "{crop:?}");+ assert!((crop[2] - moved[2]).abs() < 0.01 && (crop[3] - moved[3]).abs() < 0.01, "{crop:?}");+ // Let it complete and reply, so the request does not outlive the test.+ if let Some(cb) = shot_callbacks(&out).into_iter().next() {+ cb.complete(image(800, 480));+ }+ hidden_tick(&mut app, &ctx, &frame);+ let value = reply.try_recv().expect("the capture replies");+ assert_eq!(value["ok"], true, "{value}");+ assert_eq!((value["result"]["width"].as_u64(), value["result"]["height"].as_u64()), (Some(300), Some(200)), "{value}");+ }+ }diff --cc crates/ui-egui/tests/it/ui_audio_scrub.rsindex 6b79e9838,c748afe8e..8596d2506--- a/crates/ui-egui/tests/it/ui_audio_scrub.rs+++ b/crates/ui-egui/tests/it/ui_audio_scrub.rs@@@ -113,450 -166,134 +166,488 @@@ fn cmd_dragging_the_current_time_scrubsassert!(h.state().scrub.is_none() && fake.0.lock().unwrap().1, "closed and stopped");}-/// A preview with audio waits for frames like a silent one (#103): while the frames ahead aren't-/// cached, every frame shows as it renders, silently; the sound starts once the rest of the-/// preview is cached, and a frame that isn't cached stops it instead of being skipped.+/// The comp's frame at the current time.+fn frame(h: &Harness<'_, EffectcraftApp>, cid: ItemId) -> i64 {+ let app = h.state();+ app.session.project.comp(cid).unwrap().frame_rate.frame_at(app.session.time())+}++/// The frame under the preview audio's first sample (its clock before the device pulls any).+fn audio_frame(h: &Harness<'_, EffectcraftApp>, cid: ItemId) -> Option<i64> {+ let app = h.state();+ let a = app.audio.as_ref()?;+ Some(effectcraft_ui_egui::audio::frame_of_sample(a.clock(), a.rate, app.session.project.comp(cid).unwrap().frame_rate))+}++/// Step until the preview's sound starts (or 60 s pass, for a loaded machine), checking the+/// playhead holds `first` meanwhile. Returns whether the sound started.+fn until_sound(h: &mut Harness<'_, EffectcraftApp>, cid: ItemId, first: i64) -> bool {+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);+ while std::time::Instant::now() < deadline {- h.step();++ step(h);+ if h.state().audio.is_some() {+ return true;+ }+ assert_eq!(frame(h, cid), first, "the playhead holds the first frame until the sound can start");+ assert!(h.state().playback.starting && h.state().playback.playing);+ std::thread::sleep(std::time::Duration::from_millis(1));+ }+ false+}+++/// One step with the app clock, which playback runs on, a step (1/30 s) further on: these tests++/// count time in steps, however long a step takes on a loaded machine.++fn step(h: &mut Harness<'_, EffectcraftApp>) {++ h.state_mut().clock_mut().advance(1.0 / 30.0);++ h.step();++}+++/// Starting a preview with sound before its frames are cached (#947): the playhead holds the+/// first frame while the frames ahead render, then sound and picture start together from it (the+/// sound used to start at a later frame, losing the opening). A frame that isn't cached later on+/// still stops the sound rather than being skipped (#103).#[test]-fn preview_with_audio_shows_every_frame_and_sounds_once_cached() {+fn preview_with_audio_holds_the_first_frame_and_sounds_from_it() {let fake = Fake::default();let (mut app, cid) = setup(true, &fake);- // Several steps' worth of frames to cache before the sound can start (see the prefetch).- let ahead = (app.frames.parallelism() * 2).max(4);- // Hold rendering in the test: the cache cannot fill ahead of what the test releases, so the- // silently shown frames and the moment the sound starts do not depend on the host's threads.++ // Release only the frames this test chooses, and drive the app clock: rendering cannot race++ // the clock on a slow or loaded test host.+ let gate = GatedFrames::default();+ let renderer: Arc<dyn RemoteFrames> = Arc::new(gate.clone());+ app.frames.set_remote(Some(renderer));- let ahead = (app.frames.parallelism() * 2).max(4);++ let ahead = (app.frames.parallelism() * 2).max(4) as i64;app.session.execute("comp.settings", json!({"comp": cid.0, "duration": (4 * ahead) as f64 / 30.0})).unwrap();- let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 31.0).build_eframe(|_| app);+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);- // Playback measures time on the app clock, never `ctx.input(|i| i.time)`; drive that clock- // explicitly at one frame per step.+ h.state_mut().use_manual_clock();h.run_steps(2);- let now = h.ctx.input(|i| i.time);+ gate.discard(&mut h);- let frame = |h: &Harness<'_, EffectcraftApp>| {- let app = h.state();- app.session.project.comp(cid).unwrap().frame_rate.frame_at(app.session.time())- };+ let now = h.state().now();h.state_mut().play(now);- assert!(h.state().playback.audio_held && h.state().audio.is_none(), "nothing is cached: no sound yet");- let mut last = frame(&h);- let mut shown = 0;- // Show frames while only those frames are cached: they play silently first.- for _ in 0..4 {- let target = last + 1;- gate.render(&mut h, cid, &[target]);- h.state_mut().clock_mut().advance(1.0 / 30.0);- h.step();- let f = frame(&h);- // Accumulating 1/30 in f64 can leave the same frame up for one more step; it must never- // jump over a frame.- assert!(f == last || f == last + 1, "{last} → {f}: a frame was skipped");- shown += usize::from(f != last);- last = f;- }- // A frame that has not rendered holds the clock at the frame on screen; once it renders it is- // shown, never jumped over. This exercises the production hold path, not a test-only shortcut.- let waiting = last;- h.state_mut().clock_mut().advance(1.0 / 30.0);- h.step();- assert_eq!(frame(&h), waiting, "an uncached frame holds the preview instead of being skipped");- gate.render(&mut h, cid, &[waiting + 1]);- // f64 clock accumulation need not put the next step exactly on the next frame boundary;- // keep stepping until the held frame appears, and never allow it to jump past that frame.- let mut showed_waiting = false;- for _ in 0..4 {- h.state_mut().clock_mut().advance(1.0 / 30.0);- h.step();- let f = frame(&h);- assert!(f == waiting || f == waiting + 1, "{waiting} → {f}: the held frame was skipped");- if f == waiting + 1 {- showed_waiting = true;- break;- }- }- assert!(showed_waiting, "the waiting frame is shown once it renders");- shown += 1;- last = waiting + 1;- assert!(shown > 0, "frames played silently first");- assert!(h.state().audio.is_none() && h.state().playback.audio_held, "the rest of the range is not cached: the sound waits");- // Cache everything ahead; only then may the sound start.- let end = h.state().playback.plan.expect("a preview plan").end;- let rest: Vec<i64> = ((last + 1)..=end).collect();- gate.render(&mut h, cid, &rest);- h.step();+ assert!(h.state().playback.starting && h.state().audio.is_none(), "nothing is cached: no sound yet");- assert!(until_sound(&mut h, cid, 0), "the sound started once the frames ahead were cached");- assert!(fake.0.lock().unwrap().0.is_some());++ // Half the frames ahead cached: the playhead holds the first frame, silently.++ let half: Vec<i64> = (0..ahead / 2).collect();++ gate.render(&mut h, cid, &half);++ step(&mut h);++ assert!(h.state().playback.starting && h.state().audio.is_none(), "{} of {ahead} frames ahead cached: no sound yet", half.len());++ assert_eq!(frame(&h, cid), 0, "the playhead holds the first frame");++ // All of them: the sound starts, from that frame.++ let all: Vec<i64> = (0..ahead).collect();++ gate.render(&mut h, cid, &all);++ step(&mut h);+ assert!(h.state().audio.is_some() && fake.0.lock().unwrap().0.is_some(), "the sound started once the frames ahead were cached");+ assert_eq!(audio_frame(&h, cid), Some(0), "from the first frame, not a later one");+ assert!(!h.state().playback.starting);- // A frame that isn't cached stops the sound rather than being skipped. (Frames render fast- // enough here to be cached again before the check: keep them out of the cache.)- h.state().frames.set_budget(0);+ // A frame that isn't cached stops the sound rather than being skipped.+ gate.discard(&mut h);- h.state().frames.set_budget(0);h.state().frames.clear();h.step();assert!(h.state().audio.is_none() && h.state().playback.audio_held && h.state().playback.playing);}-/// A preview whose frames render in real time sounds without waiting for the whole work area to-/// be cached (#208): once the silent preview has kept up for a moment with the frames ahead-/// cached, the sound starts, still without skipping a frame before it.+/// A long (30 s) preview sounds once the frames ahead of its first frame are cached, not the+/// whole work area (#208), and from that first frame (#947).#[test]-fn preview_with_audio_sounds_once_rendering_keeps_up() {+fn preview_with_audio_sounds_without_caching_the_work_area() {let fake = Fake::default();- let (app, cid) = setup_long(true, &fake, 30.0);+ let (mut app, cid) = setup_long(true, &fake, 30.0);- // Release only the frames this test chooses, and drive the app clock: rendering cannot race- // the clock, so a frame is never skipped by a slow or loaded test host.+ let gate = GatedFrames::default();+ let renderer: Arc<dyn RemoteFrames> = Arc::new(gate.clone());+ app.frames.set_remote(Some(renderer));let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);+ h.state_mut().use_manual_clock();h.run_steps(2);- let now = h.ctx.input(|i| i.time);+ gate.discard(&mut h);+ let ahead = (h.state().frames.parallelism() * 2).max(4) as i64;- let frame = |h: &Harness<'_, EffectcraftApp>| {- let app = h.state();- app.session.project.comp(cid).unwrap().frame_rate.frame_at(app.session.time())++ let now = h.state().now();+ h.state_mut().play(now);- assert!(until_sound(&mut h, cid, 0), "the sound started");++ let frames: Vec<i64> = (0..ahead).collect();++ gate.render(&mut h, cid, &frames);++ step(&mut h);++ assert!(h.state().audio.is_some(), "the sound started");+ assert_eq!(audio_frame(&h, cid), Some(0), "from the first frame");+ let cached = h.state().frames.cached_frames(&h.state().shown_series(cid)).len();+ assert!((1..900).contains(&cached), "it started with {cached} of the 900 frames cached, not the whole work area");+}++/// Pull `frames` stereo frames from the output, waiting for the feeder thread to mix them.+fn pull(feed: &AudioFeed, frames: usize) -> Vec<f32> {+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);+ while feed.queued_frames() < frames && std::time::Instant::now() < deadline {+ std::thread::sleep(std::time::Duration::from_millis(5));+ }+ let mut out = vec![0.0f32; frames * 2];+ feed.pull(&mut out);+ out+}++/// The opening of the preview is heard (#947): a sound that lasts only the first 100 ms after the+/// start, from the beginning of the comp or from the middle, reaches the output device.+#[test]+fn a_sound_in_the_first_100_ms_is_heard() {+ for start in [0.0, 1.0] {+ let fake = Fake::default();+ let (mut app, cid) = setup_long(false, &fake, 4.0);+ // A tone only from `start` to `start` + 0.1 s.+ let l = app.session.execute("layer.newSolid", json!({"color": "#808080"})).unwrap()["layer"].as_u64().unwrap();+ app.session.execute("effect.apply", json!({"layers": [l], "effect": "ec.audio.tone"})).unwrap();+ app.session.execute("layer.setSwitch", json!({"layers": [l], "switch": "audio", "value": true})).unwrap();+ app.session.execute("layer.timing", json!({"layers": [l], "in": start, "out": start + 0.1})).unwrap();+ app.session.execute("time.set", json!({"time": start})).unwrap();+ let first = (start * 30.0) as i64;+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);++ h.state_mut().use_manual_clock();+ h.run_steps(2);- let now = h.ctx.input(|i| i.time);++ let now = h.state().now();+ h.state_mut().play(now);+ assert!(until_sound(&mut h, cid, first), "{start}: the sound started");+ assert_eq!(audio_frame(&h, cid), Some(first), "{start}: from the first frame");+ let feed = fake.0.lock().unwrap().0.clone().expect("the output opened");+ // The first 100 ms at 48 kHz.+ let out = pull(&feed, 4800);+ assert!(out.iter().any(|v| v.abs() > 0.01), "{start}: the opening tone reached the output");+ }+}++/// A preview without video starts its sound at once, from the first frame (unchanged), and so+/// does one whose frames are already cached.+#[test]+fn audio_only_previews_start_at_once() {+ let fake = Fake::default();+ let (app, cid) = setup(true, &fake);+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_| app);++ h.state_mut().use_manual_clock();+ h.run_steps(2);- let now = h.ctx.input(|i| i.time);++ let now = h.state().now();+ h.state_mut().play_with(now, effectcraft_engine::preview::PreviewShortcut::AltNumpad0);+ assert!(h.state().audio.is_some() && !h.state().playback.starting, "Video off: the sound starts at once");+ assert_eq!(audio_frame(&h, cid), Some(0));+}++/// Without an output device the preview still plays, from the first frame, once the frames ahead+/// are cached; Stop while it prepares leaves nothing playing.+#[test]+fn no_device_plays_the_picture_and_stop_ends_the_wait() {+ let fake = Fake::default();+ let (mut app, cid) = setup_long(true, &fake, 10.0);+ app.hooks.audio_device = Some(Box::new(|_| None));+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);++ h.state_mut().use_manual_clock();+ h.run_steps(2);- let now = h.ctx.input(|i| i.time);++ let now = h.state().now();+ h.state_mut().play(now);+ assert!(h.state().playback.starting);+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);+ while h.state().playback.starting && std::time::Instant::now() < deadline {- h.step();++ step(&mut h);+ std::thread::sleep(std::time::Duration::from_millis(1));+ }+ assert!(!h.state().playback.starting && h.state().audio.is_none() && h.state().playback.playing, "no device: the picture plays alone");+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);+ while frame(&h, cid) == 0 && std::time::Instant::now() < deadline {- h.step();++ step(&mut h);+ std::thread::sleep(std::time::Duration::from_millis(5));+ }+ assert!(frame(&h, cid) > 0, "from the first frame on");+ // Stop while preparing.+ h.state_mut().stop();- let now = h.ctx.input(|i| i.time);++ let now = h.state().now();+ let device = fake.clone();+ h.state_mut().hooks.audio_device = Some(Box::new(move |_| Some(Box::new(device.clone()) as Box<dyn AudioDevice>)));+ h.state().frames.clear();+ h.state_mut().play(now);+ // One step into preparing: the frames ahead are requested and it waits for them.- h.step();++ step(&mut h);+ assert!(h.state().playback.starting && h.state().playback.waiting && h.state().audio.is_none(), "preparing");+ h.state_mut().stop();+ // The renders it queued finish after Stop: they must not start the sound.+ for _ in 0..30 {- h.step();++ step(&mut h);+ std::thread::sleep(std::time::Duration::from_millis(10));+ }+ assert!(!h.state().playback.playing && !h.state().playback.starting && h.state().audio.is_none(), "Stop while preparing: nothing plays");+ assert!(fake.0.lock().unwrap().0.is_none(), "the output never opened");+}++/// Start a preview and step until it leaves its startup (or 60 s pass). Returns whether it did.+fn until_started(h: &mut Harness<'_, EffectcraftApp>) -> bool {- let now = h.ctx.input(|i| i.time);++ let now = h.state().now();+ h.state_mut().play(now);+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);+ while h.state().playback.starting && std::time::Instant::now() < deadline {- h.step();++ step(h);+ std::thread::sleep(std::time::Duration::from_millis(1));+ }+ !h.state().playback.starting+}++/// RAM preview budgets just below, at and above the prefetch's frames ahead, and small ones, from+/// a cold cache, with and without an output device: the start never waits for a frame the+/// prefetch doesn't request (with a budget of 4 frames it asks for 3 ahead of the playhead).+#[test]+fn startup_finishes_at_any_ram_preview_budget() {+ let probe = Fake::default();+ let (app, _) = setup(true, &probe);+ let ahead = (app.frames.parallelism() * 2).max(4) as i64;+ // A 64×36 frame is 9216 bytes; `fit` frames fit in (fit + ½) frames' worth / 0.9.+ let mut fits = vec![2, 3, 4, ahead - 1, ahead, ahead + 1];+ fits.sort();+ fits.dedup();+ for fit in fits {+ for device in [true, false] {+ let fake = Fake::default();+ let (mut app, cid) = setup_long(true, &fake, 8.0);+ if !device {+ app.hooks.audio_device = Some(Box::new(|_| None));+ }+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);++ h.state_mut().use_manual_clock();+ h.run_steps(2);+ let budget = ((fit as f64 + 0.5) * 9216.0 / 0.9) as usize;+ h.state().frames.set_budget(budget);+ h.state().frames.clear();+ assert!(until_started(&mut h), "fit {fit} (ahead {ahead}), device {device}: the preview left its startup");+ assert_eq!(h.state().audio.is_some(), device, "fit {fit}, device {device}");+ if device {+ assert_eq!(audio_frame(&h, cid), Some(0), "fit {fit}: from the first frame");+ }+ }+ }+}++/// An audible 160×120 comp with a 3D layer and Draft 3D, Extended Viewer on: its viewer frames+/// render the pasteboard too, larger than the comp. Returns the harness once the first frame has+/// rendered, the comp, and that frame's size.+fn extended_viewer(fake: &Fake) -> (Harness<'static, EffectcraftApp>, ItemId, [usize; 2]) {+ let mut s = Session::default();+ s.execute("comp.new", json!({"name": "E", "width": 160, "height": 120, "frameRate": 30, "duration": 8})).unwrap();+ let cid = s.active_comp_id().unwrap();+ let l = s.execute("layer.newSolid", json!({"color": "#4060a0"})).unwrap()["layer"].as_u64().unwrap();+ s.execute("effect.apply", json!({"layers": [l], "effect": "ec.audio.tone"})).unwrap();+ s.execute("layer.setSwitch", json!({"layers": [l], "switch": "audio", "value": true})).unwrap();+ s.execute("layer.setSwitch", json!({"layers": [l], "switch": "threeD", "value": true})).unwrap();+ s.execute("comp.setSwitch", json!({"switch": "draft3d", "value": true})).unwrap();+ s.prefs.three_d.extended_viewer = true;+ let mut app = EffectcraftApp::new(s);+ let f = fake.clone();+ app.hooks.audio_device = Some(Box::new(move |_| Some(Box::new(f.clone()) as Box<dyn AudioDevice>)));+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);++ h.state_mut().use_manual_clock();+ // The viewer shows the pasteboard: frames render the extended region.+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);+ let size = loop {- h.step();++ step(&mut h);+ let series = h.state().shown_series(cid);+ if let Some(img) = h.state().frames.get(&series) {+ break img.size();+ }+ assert!(std::time::Instant::now() < deadline, "the first frame rendered");+ std::thread::sleep(std::time::Duration::from_millis(5));+ };+ assert!(h.state().ui.viewer.extended.is_some(), "Extended Viewer is on");+ (h, cid, size)+}++/// Extended Viewer frames render larger than the comp: a budget that would hold `ahead` comp-sized+/// frames holds fewer of them, and the start waits only for as many as really fit (#947).+#[test]+fn startup_finishes_when_extended_viewer_frames_are_larger() {+ let fake = Fake::default();+ let (mut h, cid, size) = extended_viewer(&fake);+ let ahead = (h.state().frames.parallelism() * 2).max(4);+ let (comp_bytes, real_bytes) = (160 * 120 * 4, size[0] * size[1] * 4);+ // Room for `ahead` comp-sized frames (and a half), which is fewer real ones.+ let budget = ((ahead as f64 + 0.5) * comp_bytes as f64 / 0.9) as usize;+ assert!((budget as f64 * 0.9 / real_bytes as f64) < ahead as f64, "real frames ({size:?}) fit fewer than {ahead}");+ h.state().frames.set_budget(budget);+ h.state().frames.clear();- let now = h.ctx.input(|i| i.time);++ let now = h.state().now();+ h.state_mut().play(now);- h.step();++ step(&mut h);+ // The first frame renders before the harness clock moves on: each step is 1/30 s however long+ // it takes, so on a busy machine the first frame could take longer than STARTUP_LIMIT of that+ // clock, and this test is about the wait for the frames ahead.+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);+ while !h.state().frames.is_cached(&h.state().shown_series(cid)) && std::time::Instant::now() < deadline {+ std::thread::sleep(std::time::Duration::from_millis(5));+ }+ while h.state().playback.starting && std::time::Instant::now() < deadline {- h.step();++ step(&mut h);+ std::thread::sleep(std::time::Duration::from_millis(1));+ }+ assert!(!h.state().playback.starting, "the preview left its startup");+ assert!(h.state().audio.is_some(), "with its sound");+ assert_eq!(audio_frame(&h, cid), Some(0), "from the first frame");+ let (started, stopped) = {+ let d = fake.0.lock().unwrap();+ (d.0.is_some(), d.1)};+ assert!(started && !stopped, "the device started and kept playing");+}++/// A first frame that takes longer than STARTUP_WAIT to be cached (a heavy comp, a busy machine;+/// here it can't be cached until the RAM preview grows): the start keeps holding it, then the+/// sound starts from it and keeps playing. It used to give up two seconds after Play, start the+/// sound on the uncached frame and stop it at once, so the opening played silently.+#[test]+fn a_slow_first_frame_keeps_its_opening_sound() {+ let fake = Fake::default();+ let (mut h, cid, size) = extended_viewer(&fake);+ let (comp_bytes, real_bytes) = (160 * 120 * 4, size[0] * size[1] * 4);+ assert!(real_bytes > comp_bytes * 2, "Extended Viewer frames ({size:?}) are larger than the comp");+ h.state().frames.set_budget((comp_bytes + real_bytes) / 2);+ h.state().frames.clear();- let start = h.ctx.input(|i| i.time);+ let start = h.state().now();h.state_mut().play(start);- while h.ctx.input(|i| i.time) - start < 3.0 {- h.step();- assert!(h.state().playback.audio_held && h.state().audio.is_none(), "nothing is cached: no sound yet");- let mut last = frame(&h);- let mut shown = 0;- // Two seconds of playback at one frame per clock step: keep the prefetch window cached, but- // not the rest of the 30 s comp, so the sound has to start by the real-time rule.- for step in 1..=60 {- let target = last + 1;- let window: Vec<i64> = (target..target + ahead).collect();- gate.render(&mut h, cid, &window);- // Set the clock to this step's exact time rather than adding 1/30 repeatedly: accumulated- // f64 error can put one step just below a frame boundary and the next just above it, so- // `floor(elapsed × fps)` would otherwise jump from frame N to N + 2 for a one-frame step.++ while h.state().now() - start < 3.0 {++ step(&mut h);+ assert!(h.state().playback.starting, "it still holds the first frame");+ assert_eq!(frame(&h, cid), 0);+ }+ assert!(fake.0.lock().unwrap().0.is_none(), "no sound yet");+ h.state().frames.set_budget(1 << 30);+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);+ while h.state().playback.starting && std::time::Instant::now() < deadline {- h.step();++ step(&mut h);+ std::thread::sleep(std::time::Duration::from_millis(1));+ }+ assert!(!h.state().playback.starting, "the preview left its startup");+ assert_eq!(audio_frame(&h, cid), Some(0), "with its sound from the first frame");+ let (started, stopped) = {+ let d = fake.0.lock().unwrap();+ (d.0.is_some(), d.1)+ };+ assert!(started && !stopped, "the device started and kept playing");+}++/// An output device whose start fails.+#[derive(Clone, Default)]+struct Broken;++impl AudioDevice for Broken {+ fn sample_rate(&self) -> u32 {+ 48000+ }+ fn start(&mut self, _feed: Arc<AudioFeed>) -> Result<(), String> {+ Err("device unavailable".into())+ }+ fn stop(&mut self) {}+}++/// A device that fails to start: the picture plays on its own from the first frame.+#[test]+fn a_device_that_fails_to_start_plays_the_picture() {+ let fake = Fake::default();+ let (mut app, cid) = setup_long(true, &fake, 8.0);+ app.hooks.audio_device = Some(Box::new(|_| Some(Box::new(Broken) as Box<dyn AudioDevice>)));+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);++ h.state_mut().use_manual_clock();+ h.run_steps(2);+ assert!(until_started(&mut h), "it left its startup");+ assert!(h.state().audio.is_none() && h.state().playback.playing);+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);+ while frame(&h, cid) == 0 && std::time::Instant::now() < deadline {- h.step();++ step(&mut h);+ std::thread::sleep(std::time::Duration::from_millis(5));+ }+ assert!(frame(&h, cid) > 0, "the picture plays");+}++/// An output that plays: a thread takes the feed's samples in real time, as a sound card would.+#[derive(Clone, Default)]+struct Playing(Arc<Mutex<bool>>);++impl AudioDevice for Playing {+ fn sample_rate(&self) -> u32 {+ 48000+ }+ fn start(&mut self, feed: Arc<AudioFeed>) -> Result<(), String> {+ let stop = self.0.clone();+ std::thread::spawn(move || {+ let mut buf = vec![0.0f32; 480 * 2];+ while !*stop.lock().unwrap() {+ feed.pull(&mut buf);+ std::thread::sleep(std::time::Duration::from_millis(10));+ }+ });+ Ok(())+ }+ fn stop(&mut self) {+ *self.0.lock().unwrap() = true;+ }+}++/// Once started, the sound's clock moves the picture on from the first frame, and a frame that+/// isn't cached still stops the sound rather than being skipped.+#[test]+fn a_started_preview_follows_its_sound() {+ let fake = Fake::default();+ let (mut app, cid) = setup_long(true, &fake, 8.0);+ let device = Playing::default();+ let d = device.clone();+ app.hooks.audio_device = Some(Box::new(move |_| Some(Box::new(d.clone()) as Box<dyn AudioDevice>)));+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);++ h.state_mut().use_manual_clock();+ h.run_steps(2);+ assert!(until_started(&mut h) && h.state().audio.is_some(), "the sound started");+ assert_eq!(audio_frame(&h, cid), Some(0), "from the first frame");+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);+ while frame(&h, cid) < 5 && std::time::Instant::now() < deadline && h.state().audio.is_some() {- h.step();++ step(&mut h);+ std::thread::sleep(std::time::Duration::from_millis(10));+ }+ assert!(frame(&h, cid) >= 5, "the picture follows the sound's clock (at frame {})", frame(&h, cid));+ h.state().frames.set_budget(0);+ h.state().frames.clear();- h.step();++ step(&mut h);+ assert!(h.state().audio.is_none() && h.state().playback.audio_held && h.state().playback.playing, "a stall stops the sound");+ *device.0.lock().unwrap() = true;+}++/// A RAM preview that can't hold even one frame (none, or half of one): the start doesn't wait+/// for frames that can never be cached. The sound isn't started on a frame that can't be shown+/// (a stall would stop it at once): the preview goes on as a stalled one does.+#[test]+fn startup_does_not_wait_when_the_ram_preview_holds_no_frame() {+ for budget in [0, 9216 / 2] {+ let fake = Fake::default();+ let (app, _) = setup_long(true, &fake, 8.0);+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);++ h.state_mut().use_manual_clock();+ h.run_steps(2);+ h.state().frames.set_budget(budget);+ h.state().frames.clear();- let now = h.ctx.input(|i| i.time);+ let now = h.state().now();- let tick_time = start + step as f64 / 30.0;- h.state_mut().clock_mut().advance(tick_time - now);- h.step();- if h.state().audio.is_some() {- break;- }- let f = frame(&h);- assert!(f == last || f == last + 1, "{last} → {f}: a frame was skipped");- shown += usize::from(f != last);- last = f;+ h.state_mut().play(now);- h.step();++ step(&mut h);+ assert!(!h.state().playback.starting, "budget {budget}: no wait");+ assert!(fake.0.lock().unwrap().0.is_none(), "budget {budget}: no sound started and dropped");+ assert!(h.state().playback.playing && h.state().playback.audio_held, "budget {budget}: it goes on as a stalled preview");+ }+}++/// A frame that never gets cached although the estimate says one fits (an Extended Viewer frame+/// larger than the whole budget): the start gives up after STARTUP_LIMIT instead of holding the+/// first frame for ever, without starting the sound on a frame it can't show.+#[test]+fn startup_gives_up_on_a_first_frame_that_never_caches() {+ let fake = Fake::default();+ let (mut h, cid, size) = extended_viewer(&fake);+ let (comp_bytes, real_bytes) = (160 * 120 * 4, size[0] * size[1] * 4);+ assert!(real_bytes > comp_bytes * 2, "Extended Viewer frames ({size:?}) are larger than the comp");+ // Room for a comp-sized frame but not a real one: the current frame can never be cached.+ let budget = (comp_bytes + real_bytes) / 2;+ h.state().frames.set_budget(budget);+ h.state().frames.clear();- let start = h.ctx.input(|i| i.time);++ let start = h.state().now();+ h.state_mut().play(start);+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);+ while h.state().playback.starting && std::time::Instant::now() < deadline {+ assert_eq!(frame(&h, cid), 0, "it holds the first frame while it waits");- h.step();++ step(&mut h);}- let waited = h.ctx.input(|i| i.time) - start;- assert!(h.state().audio.is_some() && fake.0.lock().unwrap().0.is_some(), "the sound started within 2 s, at frame {last} of 900");- assert!(shown >= 3, "the sound starts only after the silent preview kept up for a moment");++ let waited = h.state().now() - start;+ assert!(!h.state().playback.starting, "it gave up waiting");+ assert!((9.9..12.0).contains(&waited), "after about ten seconds ({waited:.2} s)");+ assert!(fake.0.lock().unwrap().0.is_none(), "no sound started and dropped");+ assert!(h.state().playback.playing && h.state().playback.audio_held, "it goes on as a stalled preview");}