Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: speed up clean recording finalization and reveal the editor

Richie McIlroy1617a7451676
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index 241f9b2bd..bb8d2f73a 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -3489,28 +3489,18 @@ pub fn open_editor(project_path: PathBuf, cx: &mut App) {
}
let key = editor_key(&project_path);
- if let Some(handle) = cx
+ if cx
.global::<AppWindows>()
.editors
.iter()
- .find(|(path, _)| path == &key)
- .map(|(_, handle)| *handle)
+ .any(|(path, _)| path == &key)
{
tracing::info!(
path = %key.display(),
"editor already open for this project; focusing it"
);
- let native = handle
- .update(cx, |_, window, _| platform::native_window(window))
- .ok()
- .flatten();
- cx.spawn(async move |_| {
- if let Some(native) = &native {
- platform::show_native(native);
- }
- })
- .detach();
hide_main_and_park_camera_preview(cx);
+ reveal_editor_window(&key, cx);
return;
}
@@ -3589,6 +3579,7 @@ pub fn open_editor(project_path: PathBuf, cx: &mut App) {
.ok();
hide_main_and_park_camera_preview(cx);
+ reveal_editor_window(&key, cx);
load_editor_project(key, handle, cx);
}
@@ -4391,8 +4382,10 @@ fn reveal_editor_window(project_path: &Path, cx: &mut App) {
platform::show_native(native);
}
cx.update(|cx| {
+ cx.activate(true);
handle
.update(cx, |_, window, cx| {
+ window.activate_window();
platform::kick_display_link(window);
cx.notify();
window.refresh();
diff --git a/crates/recording/src/recovery.rs b/crates/recording/src/recovery.rs
index a84db12b3..0f9e672fa 100644
--- a/crates/recording/src/recovery.rs
+++ b/crates/recording/src/recovery.rs
@@ -63,7 +63,20 @@ enum RecoveryPurpose {
Finalize,
}
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum VideoValidation {
+ Full,
+ Bounded,
+}
+
impl RecoveryPurpose {
+ fn video_validation(self, status: Option<StudioRecordingStatus>) -> VideoValidation {
+ match (self, status) {
+ (Self::Finalize, Some(StudioRecordingStatus::NeedsRemux)) => VideoValidation::Bounded,
+ _ => VideoValidation::Full,
+ }
+ }
+
fn success_message(self) -> &'static str {
match self {
Self::Recover => "Successfully recovered recording",
@@ -897,6 +910,8 @@ impl RecoveryManager {
"Recording metadata changed".into(),
));
}
+ let video_validation =
+ purpose.video_validation(current.studio_meta().map(|studio| studio.status()));
Self::require_recoverable_tracks(recording)?;
let workspace = project.join(format!(".recovery-{}", uuid::Uuid::new_v4()));
create_private_recovery_dir(&workspace)?;
@@ -921,8 +936,8 @@ impl RecoveryManager {
.ok_or(RecoveryError::NoRecoverableSegments)?;
Self::require_recoverable_tracks(&staged_recording)?;
Self::require_local_fragments(&staged_recording)?;
- Self::validate_staged_inputs(&staged_recording)?;
- let recovered = Self::finalize_staged(&staged_recording, purpose)?;
+ Self::validate_staged_inputs(&staged_recording, video_validation)?;
+ let recovered = Self::finalize_staged(&staged_recording, purpose, video_validation)?;
let config = ProjectConfiguration::load(&staged)?;
config
.validate()
@@ -958,25 +973,35 @@ impl RecoveryManager {
})
}
- fn validate_staged_inputs(recording: &IncompleteRecording) -> Result<(), RecoveryError> {
+ fn validate_staged_inputs(
+ recording: &IncompleteRecording,
+ video_validation: VideoValidation,
+ ) -> Result<(), RecoveryError> {
for segment in &recording.recoverable_segments {
validate_recovery_video_inputs(
&segment.display_fragments,
segment.display_init_segment.as_deref(),
&recording.project_path,
+ video_validation,
)?;
let display_dir = recording.project_path.join(format!(
"content/segments/segment-{}/display",
segment.index
));
for (_, init, fragments) in Self::collect_respawn_groups(&display_dir, None) {
- validate_recovery_video_inputs(&fragments, Some(&init), &recording.project_path)?;
+ validate_recovery_video_inputs(
+ &fragments,
+ Some(&init),
+ &recording.project_path,
+ video_validation,
+ )?;
}
if let Some(fragments) = &segment.camera_fragments {
validate_recovery_video_inputs(
fragments,
segment.camera_init_segment.as_deref(),
&recording.project_path,
+ video_validation,
)?;
}
for path in segment
@@ -1086,6 +1111,7 @@ impl RecoveryManager {
fn finalize_staged(
recording: &IncompleteRecording,
purpose: RecoveryPurpose,
+ video_validation: VideoValidation,
) -> Result<RecoveredRecording, RecoveryError> {
Self::require_no_track_failure(&recording.project_path)?;
if recording.recoverable_segments.is_empty() {
@@ -1287,7 +1313,11 @@ impl RecoveryManager {
let dir = recording
.project_path
.join(format!("content/segments/segment-{}", segment.index));
- validate_recovered_track(&dir.join("display.mp4"), ffmpeg::media::Type::Video)?;
+ validate_recovery_track(
+ &dir.join("display.mp4"),
+ ffmpeg::media::Type::Video,
+ video_validation,
+ )?;
for (fragments, name, kind) in [
(
segment.camera_fragments.as_ref(),
@@ -1306,7 +1336,7 @@ impl RecoveryManager {
),
] {
if fragments.is_some() {
- validate_recovered_track(&dir.join(name), kind)?;
+ validate_recovery_track(&dir.join(name), kind, video_validation)?;
}
}
}
@@ -1424,6 +1454,7 @@ impl RecoveryManager {
&video.fragments,
video.init_segment.as_deref(),
&workspace,
+ VideoValidation::Full,
)?;
Self::finalize_instant_staged(&display, &audio, &final_output)?;
validate_recovered_track(&final_output, ffmpeg::media::Type::Video)?;
@@ -1478,6 +1509,7 @@ impl RecoveryManager {
audio_info.init_segment.as_deref(),
parent,
ffmpeg::media::Type::Audio,
+ VideoValidation::Full,
)?;
std::fs::create_dir_all(parent)?;
let stem = output
@@ -2126,8 +2158,15 @@ fn validate_recovery_video_inputs(
fragments: &[PathBuf],
init: Option<&Path>,
workspace: &Path,
+ video_validation: VideoValidation,
) -> Result<(), RecoveryError> {
- validate_recovery_track_inputs(fragments, init, workspace, ffmpeg::media::Type::Video)
+ validate_recovery_track_inputs(
+ fragments,
+ init,
+ workspace,
+ ffmpeg::media::Type::Video,
+ video_validation,
+ )
}
fn validate_recovery_track_inputs(
@@ -2135,6 +2174,7 @@ fn validate_recovery_track_inputs(
init: Option<&Path>,
workspace: &Path,
kind: ffmpeg::media::Type,
+ video_validation: VideoValidation,
) -> Result<(), RecoveryError> {
if fragments.is_empty() {
return Err(RecoveryError::NoRecoverableSegments);
@@ -2150,11 +2190,11 @@ fn validate_recovery_track_inputs(
}
output.sync_all()?;
drop(output);
- validate_recovered_track(&path, kind)?;
+ validate_recovery_track(&path, kind, video_validation)?;
std::fs::remove_file(path)?;
} else {
for path in fragments {
- validate_recovered_track(path, kind)?;
+ validate_recovery_track(path, kind, video_validation)?;
}
}
Ok(())
@@ -2806,6 +2846,14 @@ fn publish_recovery_with(
}
fn validate_recovered_track(path: &Path, kind: ffmpeg::media::Type) -> Result<(), RecoveryError> {
+ validate_recovery_track(path, kind, VideoValidation::Full)
+}
+
+fn validate_recovery_track(
+ path: &Path,
+ kind: ffmpeg::media::Type,
+ video_validation: VideoValidation,
+) -> Result<(), RecoveryError> {
let validate = || -> Result<(), String> {
let mut input = ffmpeg::format::input(path).map_err(|error| error.to_string())?;
let stream = input
@@ -2835,6 +2883,12 @@ fn validate_recovered_track(path: &Path, kind: ffmpeg::media::Type) -> Result<()
if packet.is_corrupt() {
return Err("Corrupt packet".into());
}
+ if decoded
+ && kind == ffmpeg::media::Type::Video
+ && video_validation == VideoValidation::Bounded
+ {
+ continue;
+ }
decoder
.send_packet(&packet)
.map_err(|error| error.to_string())?;
@@ -2887,11 +2941,54 @@ fn replace_file(src: &Path, dst: &Path) -> Result<(), RecoveryError> {
#[cfg(test)]
mod tests {
use super::{
- RecoveryManager, replace_file, start_time_or_display_fallback, valid_recovered_audio,
+ RecoveryManager, RecoveryPurpose, StudioRecordingStatus, VideoValidation, replace_file,
+ start_time_or_display_fallback, valid_recovered_audio, validate_recovery_track,
};
use std::fs;
use tempfile::tempdir;
+ #[test]
+ fn only_clean_finalization_uses_bounded_video_validation() {
+ for status in [
+ None,
+ Some(StudioRecordingStatus::InProgress),
+ Some(StudioRecordingStatus::NeedsRemux),
+ Some(StudioRecordingStatus::Complete),
+ Some(StudioRecordingStatus::Failed {
+ error: "capture failed".into(),
+ }),
+ ] {
+ assert_eq!(
+ RecoveryPurpose::Recover.video_validation(status.clone()),
+ VideoValidation::Full
+ );
+ let expected = if matches!(status, Some(StudioRecordingStatus::NeedsRemux)) {
+ VideoValidation::Bounded
+ } else {
+ VideoValidation::Full
+ };
+ assert_eq!(RecoveryPurpose::Finalize.video_validation(status), expected);
+ }
+ }
+
+ #[test]
+ fn bounded_video_validation_rejects_empty_and_unplayable_media() {
+ let directory = tempdir().unwrap();
+ let path = directory.path().join("display.mp4");
+ for bytes in [b"".as_slice(), b"invalid video".as_slice()] {
+ fs::write(&path, bytes).unwrap();
+ assert!(
+ validate_recovery_track(
+ &path,
+ ffmpeg::media::Type::Video,
+ VideoValidation::Bounded
+ )
+ .is_err()
+ );
+ assert_eq!(fs::read(&path).unwrap(), bytes);
+ }
+ }
+
fn complete_m4s_fragment() -> Vec<u8> {
let mut fragment = Vec::new();
for name in [b"moof", b"mdat"] {
diff --git a/crates/recording/tests/recovery.rs b/crates/recording/tests/recovery.rs
index eed5f5281..aae720884 100644
--- a/crates/recording/tests/recovery.rs
+++ b/crates/recording/tests/recovery.rs
@@ -1689,8 +1689,14 @@ fn complete_recovery_fixture() -> TestRecording {
#[test]
fn recovery_and_finalize_retain_every_known_track_on_success() {
test_utils::init_tracing();
- for finalize in [false, true] {
+ for (finalize, status) in [
+ (false, StudioRecordingStatus::InProgress),
+ (false, StudioRecordingStatus::NeedsRemux),
+ (true, StudioRecordingStatus::InProgress),
+ (true, StudioRecordingStatus::NeedsRemux),
+ ] {
let recording = complete_recovery_fixture();
+ assert!(set_fixture_status(recording.path(), status));
let incomplete = RecoveryManager::inspect_recording(recording.path()).unwrap();
let recovered = if finalize {
RecoveryManager::finalize(&incomplete)
@@ -1869,6 +1875,17 @@ fn recovery_rejects_later_corrupt_video_even_when_first_frame_decodes() {
let before = recovery_input_bytes(recording.path());
assert!(RecoveryManager::recover(&incomplete).is_err());
assert_eq!(recovery_input_bytes(recording.path()), before);
+ assert!(RecoveryManager::finalize(&incomplete).is_err());
+ assert_eq!(recovery_input_bytes(recording.path()), before);
+
+ assert!(set_fixture_status(
+ recording.path(),
+ StudioRecordingStatus::NeedsRemux
+ ));
+ let incomplete = RecoveryManager::inspect_recording(recording.path()).unwrap();
+ let before = recovery_input_bytes(recording.path());
+ assert!(RecoveryManager::recover(&incomplete).is_err());
+ assert_eq!(recovery_input_bytes(recording.path()), before);
}
fn write_recovery_dash_audio(directory: &Path) {