diff --git a/apps/desktop-gpui/README.md b/apps/desktop-gpui/README.md index e3c2c085311..8eb0599110f 100644 --- a/apps/desktop-gpui/README.md +++ b/apps/desktop-gpui/README.md @@ -1437,7 +1437,7 @@ Editor-specific deviations: | **The timeline's height is fixed** | The strip sits at the default 260px with the `MIN_TIMELINE_HEIGHT` floor expressed but its 16px drag handle inert, so the player/timeline split cannot be resized. Everything else in the strip — including editing — is real; see [Timeline editing](#timeline-editing). | | **No layout modes and no dialogs** | Export replaces the whole editor, transcript splits it and clips swaps the sidebar; none of the three is built, so `fullscreenMode`, the split ratio and the modal set are absent. | | **The editor does not park the other windows** | `ShowCapWindow::Editor` also hides the camera bubble and the target overlays and calls `release_camera_preview_if_idle`. Only the main-window half is reproduced, the same shape as the settings window's deviation. | -| **No prewarm** | `PendingEditorInstances::start_prewarm` runs *before* the Tauri window is built so decoders warm up in parallel with the webview. There is no webview to race here, so the instance is built once, after the window exists. | +| **No instance prewarm** | `PendingEditorInstances::start_prewarm` runs *before* the Tauri window is built so decoders warm up in parallel with the webview. There is no webview to race here, so the instance is built once, after the window exists. On macOS, a Studio recording that stops into the editor instead starts a throwaway decoder on the newest finished studio recording while the new one finalizes (`warm_video_decoder_for_editor`), because the first `AVAssetReader` a process starts is slow. | ## The timeline diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index e58c8a9a8f7..e796aa6e83a 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -692,6 +692,7 @@ fn open_preparing_studio_editor(session: &Entity, cx: &mut App .preparing_studio_editor() .is_some_and(|pending| matches!(pending.presentation, StudioEditorPresentation::Opened(_))) { + warm_video_decoder_for_editor(cx); close_controls(session, cx); close_target_overlays(cx); close_camera_window(cx); @@ -4957,6 +4958,54 @@ pub(crate) fn schedule_launch_render_warmup(cx: &App) { .detach(); } +/// Set once an editor starts building its instance, whose decoders bring up +/// the decode pipeline themselves, so the post-recording warmup stays out of +/// their way. +static EDITOR_DECODERS_STARTED: std::sync::atomic::AtomicBool = + std::sync::atomic::AtomicBool::new(false); + +#[cfg(target_os = "macos")] +fn editor_decoders_started() -> bool { + EDITOR_DECODERS_STARTED.load(std::sync::atomic::Ordering::Acquire) +} + +/// A Studio recording that stops into the editor makes the editor's decoders +/// the next ones to start, and the first `AVAssetReader` a process starts is +/// slow. Starting one on the newest finished studio recording while the new +/// one finalizes pays that before the editor needs its first frame. +#[cfg(target_os = "macos")] +fn warm_video_decoder_for_editor(cx: &App) { + if editor_decoders_started() { + return; + } + gpui_tokio::Tokio::spawn(cx, async { + let started = std::time::Instant::now(); + let tokio_handle = tokio::runtime::Handle::current(); + let warmed = tokio::task::spawn_blocking(move || { + let path = crate::library::newest_studio_display_path()?; + if editor_decoders_started() { + return None; + } + cap_rendering::prewarm_video_decoder(&path, tokio_handle) + .inspect_err(|error| tracing::warn!(%error, "editor decoder warmup failed")) + .ok() + }) + .await + .ok() + .flatten(); + if warmed.is_some() { + tracing::info!( + elapsed_ms = started.elapsed().as_secs_f64() * 1000.0, + "editor decoder warmup finished" + ); + } + }) + .detach(); +} + +#[cfg(not(target_os = "macos"))] +fn warm_video_decoder_for_editor(_cx: &App) {} + #[cfg(target_os = "macos")] async fn warm_renderer() -> Option { if gpu_work_requested() { @@ -5290,6 +5339,7 @@ fn load_editor_project_candidate( let frame_format = cap_editor::EditorFrameFormat::BgraSurface; #[cfg(not(target_os = "macos"))] let frame_format = cap_editor::EditorFrameFormat::Rgba; + EDITOR_DECODERS_STARTED.store(true, std::sync::atomic::Ordering::Release); if let Some(previous) = previous { return previous .recreate_preparing_candidate(state_cb, frame_cb, frame_format) diff --git a/apps/desktop-gpui/src/library.rs b/apps/desktop-gpui/src/library.rs index f45cef1cb7d..a832da62c82 100644 --- a/apps/desktop-gpui/src/library.rs +++ b/apps/desktop-gpui/src/library.rs @@ -392,6 +392,20 @@ pub fn list_recordings_limited(limit: usize) -> Vec { .collect() } +/// Not `recent_media()`: its `RECENT_MEDIA_LIMIT` cap can hide the newest +/// studio recording behind newer screenshots and Instant recordings. +#[cfg(target_os = "macos")] +pub(crate) fn newest_studio_display_path() -> Option { + newest_studio_display_path_in(&known_recordings_dirs()) +} + +#[cfg(any(target_os = "macos", test))] +fn newest_studio_display_path_in(dirs: &[PathBuf]) -> Option { + recording_items_in(dirs) + .filter(RecordingItem::opens_editor) + .find_map(|item| crate::recording::studio_display_path(&item.path)) +} + pub fn find_incomplete_recordings_in( dirs: &[PathBuf], active_recording: Option<&Path>, @@ -1759,6 +1773,41 @@ mod tests { std::fs::remove_dir_all(root).unwrap(); } + #[test] + fn newest_studio_display_skips_newer_instant_unfinished_and_trackless_recordings() { + let root = temp_dir("newest-studio-display"); + let finished = write_studio_bundle(&root, "finished-studio", 1); + let display = finished.join("content/segments/segment-0/display.mp4"); + std::fs::create_dir_all(display.parent().unwrap()).unwrap(); + std::fs::write(&display, b"").unwrap(); + std::thread::sleep(std::time::Duration::from_millis(6)); + write_studio_bundle(&root, "trackless-studio", 1); + std::thread::sleep(std::time::Duration::from_millis(6)); + let unfinished = write_bundle( + &root, + "unfinished-studio", + r#"{"pretty_name":"Unfinished","sharing":null,"segments":[{"display":{"path":"content/segments/segment-0/display.mp4","fps":30}}],"status":{"status":"InProgress"}}"#, + ); + let unfinished_display = unfinished.join("content/segments/segment-0/display.mp4"); + std::fs::create_dir_all(unfinished_display.parent().unwrap()).unwrap(); + std::fs::write(&unfinished_display, b"").unwrap(); + for index in 0..10 { + std::thread::sleep(std::time::Duration::from_millis(6)); + write_bundle( + &root, + &format!("instant-{index}"), + r#"{"pretty_name":"Instant","sharing":null,"fps":30,"sample_rate":48000}"#, + ); + } + + assert_eq!( + newest_studio_display_path_in(std::slice::from_ref(&root)), + Some(display) + ); + + std::fs::remove_dir_all(root).unwrap(); + } + #[test] fn bounded_screenshots_match_the_full_listing_and_parse_lazily() { fn eager_listing(dir: &Path) -> Vec { diff --git a/apps/desktop-gpui/src/recording.rs b/apps/desktop-gpui/src/recording.rs index a1ec646769b..a25c723693b 100644 --- a/apps/desktop-gpui/src/recording.rs +++ b/apps/desktop-gpui/src/recording.rs @@ -1449,7 +1449,7 @@ pub fn recover_instant_recording(project_path: &std::path::Path) -> anyhow::Resu /// `.../segment-0/display` *directory*, and afterwards at the muxed /// `display.mp4` beside it. The Tauri app has the same ordering, and uses its /// `updated_studio_meta` for the same reason. -fn studio_display_path(project_path: &std::path::Path) -> Option { +pub(crate) fn studio_display_path(project_path: &std::path::Path) -> Option { use cap_project::{RecordingMeta, StudioRecordingMeta}; let meta = RecordingMeta::load_for_project(project_path).ok()?; diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs index 15ddef1bfbb..8838ef36d7c 100644 --- a/crates/rendering/src/lib.rs +++ b/crates/rendering/src/lib.rs @@ -95,6 +95,22 @@ pub fn prewarm_fonts() { drop(layers::new_font_system()); } +/// The first `AVAssetReader` a process starts takes 80-90 ms in +/// `startReading` on an M4; every later one, on any file, takes a few ms. +#[cfg(target_os = "macos")] +pub fn prewarm_video_decoder( + path: &std::path::Path, + tokio_handle: tokio::runtime::Handle, +) -> Result<(), String> { + cap_video_decode::AVAssetReaderDecoder::new_with_keyframe_index( + path.to_path_buf(), + tokio_handle, + 0.0, + None, + ) + .map(drop) +} + /// Renders one small RGBA frame and one NV12 frame through the full frame /// pipeline with `layers`, then discards them. A fresh process waits on the /// driver finishing each pipeline the first time it draws (the first preview