diff --git a/apps/desktop-gpui/src/session.rs b/apps/desktop-gpui/src/session.rs index b964bbead50..c30faff40e9 100644 --- a/apps/desktop-gpui/src/session.rs +++ b/apps/desktop-gpui/src/session.rs @@ -78,6 +78,24 @@ enum CountdownAdvance { Start, } +/// Waits for the pipeline to be primed, then plays the cue. A start that is +/// dropped before it primes plays nothing. +async fn play_cue_when_primed( + primed: Option>, + gate: cap_recording::RecordingStartGate, + play: F, +) where + F: FnOnce(cap_recording::RecordingStartGate) -> Fut, + Fut: Future, +{ + if let Some(mut primed) = primed + && primed.wait_for(|primed| *primed).await.is_err() + { + return; + } + play(gate).await; +} + fn countdown_advance( phase: Phase, current_generation: u64, @@ -440,6 +458,10 @@ pub struct RecordingSession { /// A capture pipeline that finished warming up while the countdown or the /// start cue was still running; activated the moment the cue ends. primed: Option, + /// Set once the pipeline is primed. The start cue waits for it, so the cue + /// never promises a recording that has not begun capturing (an Instant + /// start waits on the server's video before it builds the pipeline). + primed_ready: Option>, /// Teardown of a cancelled primed start still releasing its devices; the /// next start waits for it before locking the mic and camera again. discarding: Option>>, @@ -488,6 +510,7 @@ impl RecordingSession { start_gate: None, start_cue: None, primed: None, + primed_ready: None, discarding: None, pause_control: PauseControlState::default(), pause_control_error: None, @@ -665,6 +688,7 @@ impl RecordingSession { self.start_gate = Some(start_gate); self.start_cue = Some(crate::app_sounds::prime_recording_start_sound()); self.discard_primed(cx); + self.primed_ready = Some(tokio::sync::watch::channel(false).0); self.pause_control.invalidate(); self.pause_control.uncertain = false; self.pause_control_error = None; @@ -687,7 +711,8 @@ impl RecordingSession { cx.notify(); // The pipeline warms up during the final countdown second so capture is - // live when the cue ends; without a countdown it warms up under the cue. + // live when the cue ends; without a countdown it warms up before the cue. + // Either way the cue waits until the pipeline is primed. if countdown <= 1 { self.prime_engine(config.clone(), generation, cx); } @@ -742,27 +767,29 @@ impl RecordingSession { .detach(); } - /// Plays the start cue; the returned future completes once the recording's - /// start gate is armed, whether the cue played, failed or was aborted. + /// Plays the start cue once the pipeline is primed; the returned future + /// completes once the recording's start gate is armed, whether the cue + /// played, failed or was aborted. fn play_start_cue( &mut self, cx: &mut Context, ) -> std::pin::Pin>> { let gate = self.start_gate.clone().unwrap_or_default(); let cue = self.start_cue.take(); + let primed = self.primed_ready.as_ref().map(|ready| ready.subscribe()); let (abort, registration) = futures_util::future::AbortHandle::new_pair(); self.countdown_cue_abort = Some(abort); let task = gpui_tokio::Tokio::spawn( cx, futures_util::future::Abortable::new( - async move { + play_cue_when_primed(primed, gate, move |gate| async move { match cue { Some(cue) => crate::app_sounds::play_recording_start_sound(cue, gate).await, None => { gate.arm(); } } - }, + }), registration, ), ); @@ -842,6 +869,9 @@ impl RecordingSession { Ok(Ok(active)) => { if this.countdown_remaining.is_some() { this.primed = Some(active); + if let Some(ready) = &this.primed_ready { + ready.send_replace(true); + } } else { this.activate(active, cx); } @@ -1945,6 +1975,7 @@ impl RecordingSession { fn finish(&mut self, cx: &mut Context) { self.abort_start_cue(); self.discard_primed(cx); + self.primed_ready = None; self.start_gate = None; #[cfg(target_os = "linux")] if !self.instant_cleanup_safe() { @@ -1991,6 +2022,47 @@ fn recording_failure_is_current(phase: Phase, current: u64, completed: u64) -> b mod countdown_tests { use super::*; + /// The start cue promises that recording has begun, so it must not play + /// before the pipeline is built (an Instant start first waits for the + /// server to create the video). + #[tokio::test] + async fn start_cue_waits_for_the_primed_pipeline() { + let (ready, primed) = tokio::sync::watch::channel(false); + let played = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let gate = cap_recording::RecordingStartGate::new(); + let cue = tokio::spawn({ + let played = played.clone(); + let gate = gate.clone(); + play_cue_when_primed(Some(primed), gate, move |gate| async move { + played.store(true, std::sync::atomic::Ordering::SeqCst); + gate.arm(); + }) + }); + tokio::time::sleep(Duration::from_millis(200)).await; + assert!(!played.load(std::sync::atomic::Ordering::SeqCst)); + assert!(!gate.is_armed()); + ready.send_replace(true); + cue.await.unwrap(); + assert!(played.load(std::sync::atomic::Ordering::SeqCst)); + assert!(gate.is_armed()); + } + + #[tokio::test] + async fn start_cue_stays_silent_when_the_start_is_dropped_before_it_primes() { + let (ready, primed) = tokio::sync::watch::channel(false); + let gate = cap_recording::RecordingStartGate::new(); + let cue = tokio::spawn(play_cue_when_primed( + Some(primed), + gate.clone(), + |_| async { + panic!("the cue played for a start that never primed"); + }, + )); + drop(ready); + cue.await.unwrap(); + assert!(!gate.is_armed()); + } + #[cfg(target_os = "linux")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn countdown_stop_cancels_an_unpolled_instant_start() { diff --git a/apps/desktop-gpui/src/upload/real_app_tests.rs b/apps/desktop-gpui/src/upload/real_app_tests.rs index 8db6686779f..51851157af5 100644 --- a/apps/desktop-gpui/src/upload/real_app_tests.rs +++ b/apps/desktop-gpui/src/upload/real_app_tests.rs @@ -8,7 +8,7 @@ //! cargo test --bin cap-gpui real_app -- --ignored --test-threads=1 //! ``` -use super::verify_tests::{API_KEY, MockCap, OWNER}; +use super::verify_tests::{API_KEY, Fault, MockCap, OWNER, Route, rule}; use serde_json::{Value, json}; use std::{ path::{Path, PathBuf}, @@ -252,6 +252,23 @@ fn kill(app: App) { drop(app); } +fn without_ansi(line: &str) -> String { + let mut plain = String::with_capacity(line.len()); + let mut chars = line.chars(); + while let Some(c) = chars.next() { + if c == '\u{1b}' { + for c in chars.by_ref() { + if c.is_ascii_alphabetic() { + break; + } + } + } else { + plain.push(c); + } + } + plain +} + /// A normal Instant recording uploads, verifies, and leaves nothing for the /// cleanup journal. #[ignore] @@ -460,3 +477,68 @@ async fn real_app_cancelled_start_deletes_its_video() { } panic!("no stop landed while the start was in flight"); } + +/// The start cue tells the user they are recording. An Instant start first +/// waits for the server to create its video, which takes seconds when earlier +/// takes are still uploading. The cue must wait for capture too: it used to +/// play and arm the start gate long before the pipeline existed, so the +/// opening words of the take were never recorded. +#[ignore] +#[tokio::test(flavor = "multi_thread")] +async fn real_app_start_cue_waits_for_a_slow_server_video() { + let mock = MockCap::start(vec![rule( + Route::Api("video/create"), + Fault::Delay(Duration::from_secs(4)), + )]) + .await; + let sandbox = Sandbox::new(&mock.url, 0); + let app = sandbox.launch("slow-create", &[("CAP_GPUI_AUTO_RECORD", "instant:6")]); + wait_for_app( + &sandbox, + &app, + "a verified upload", + Duration::from_secs(180), + || { + sandbox + .projects() + .first() + .and_then(|project| state(project)) + .is_some_and(|state| state["phase"] == "Verified") + }, + ) + .await; + kill(app); + let log = sandbox.log("slow-create"); + let line = |needle: &str| { + log.lines() + .position(|line| line.contains(needle)) + .unwrap_or_else(|| panic!("no {needle:?} in the app log:\n{log}")) + }; + let building = line("starting recording"); + assert!( + line("Recording start cue playing") > building, + "the start cue played before capture was being built" + ); + // Capture that is already running when the cue ends holds its first frame + // behind the start gate. A cue that played first opened the gate before + // any capture existed, so nothing was ever held. + let first_frame = log + .lines() + .map(without_ansi) + .find(|line| { + line.contains("Start gate") + && (line.contains("video frame") || line.contains("frame on screen")) + }) + .unwrap_or_else(|| panic!("no first video frame in the app log:\n{log}")); + let held = first_frame + .split("held_frames=") + .nth(1) + .and_then(|rest| rest.split(|c: char| !c.is_ascii_digit()).next()) + .and_then(|count| count.parse::().ok()) + .unwrap_or_else(|| panic!("no held_frames in {first_frame:?}")); + assert!( + held >= 1, + "capture was not running when the start cue ended: {first_frame}" + ); + assert_eq!(mock.created().len(), 1); +}