Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: pause editor preview during video export

Richie McIlroy8a7f450b0b13
diff --git a/apps/desktop/src-tauri/src/editor_window.rs b/apps/desktop/src-tauri/src/editor_window.rs
index 4d5ff3833..87c68a916 100644
--- a/apps/desktop/src-tauri/src/editor_window.rs
+++ b/apps/desktop/src-tauri/src/editor_window.rs
@@ -144,6 +144,39 @@ impl<'de, R: Runtime> CommandArg<'de, R> for WindowEditorInstance {
}
}
+pub struct OptionalWindowEditorInstance(pub Option<Arc<EditorInstance>>);
+
+impl specta::function::FunctionArg for OptionalWindowEditorInstance {
+ fn to_datatype(_: &mut specta::TypeMap) -> Option<specta::DataType> {
+ None
+ }
+}
+
+impl Deref for OptionalWindowEditorInstance {
+ type Target = Option<Arc<EditorInstance>>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl<'de, R: Runtime> CommandArg<'de, R> for OptionalWindowEditorInstance {
+ fn from_command(
+ command: tauri::ipc::CommandItem<'de, R>,
+ ) -> Result<Self, tauri::ipc::InvokeError> {
+ let Ok(window) = Window::from_command(command) else {
+ return Ok(Self(None));
+ };
+
+ let Some(instances) = window.try_state::<EditorInstances>() else {
+ return Ok(Self(None));
+ };
+
+ let instance = futures::executor::block_on(instances.0.read());
+ Ok(Self(instance.get(window.label()).cloned()))
+ }
+}
+
impl EditorInstances {
pub async fn get_or_create(
window: &Window,
diff --git a/apps/desktop/src-tauri/src/export.rs b/apps/desktop/src-tauri/src/export.rs
index e461e0371..963f1c1f2 100644
--- a/apps/desktop/src-tauri/src/export.rs
+++ b/apps/desktop/src-tauri/src/export.rs
@@ -1,4 +1,4 @@
-use crate::editor_window::WindowEditorInstance;
+use crate::editor_window::{OptionalWindowEditorInstance, WindowEditorInstance};
use crate::{FramesRendered, get_video_metadata};
use cap_export::ExporterBase;
use cap_project::{RecordingMeta, XY};
@@ -11,10 +11,22 @@ use serde::{Deserialize, Serialize};
use specta::Type;
use std::{
path::{Path, PathBuf},
- sync::{Arc, atomic::Ordering},
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ },
};
use tracing::{info, instrument};
+struct ExportActiveGuard<'a>(&'a AtomicBool);
+
+impl Drop for ExportActiveGuard<'_> {
+ fn drop(&mut self) {
+ self.0.store(false, Ordering::Release);
+ tracing::info!("Resuming editor preview after export");
+ }
+}
+
#[derive(Deserialize, Clone, Copy, Debug, Type)]
#[serde(tag = "format")]
pub enum ExportSettings {
@@ -87,15 +99,25 @@ fn is_frame_decode_error(error: &str) -> bool {
#[tauri::command]
#[specta::specta]
-#[instrument(skip(progress))]
+#[instrument(skip(progress, editor))]
pub async fn export_video(
project_path: PathBuf,
progress: tauri::ipc::Channel<FramesRendered>,
settings: ExportSettings,
+ editor: OptionalWindowEditorInstance,
) -> Result<PathBuf, String> {
- let force_ffmpeg = match &settings {
- ExportSettings::Mp4(s) => s.force_ffmpeg_decoder,
- ExportSettings::Gif(_) => false,
+ let force_ffmpeg = true;
+ tracing::info!(
+ "Using FFmpeg decoder for export (ensures all frames can be decoded regardless of keyframe positions)"
+ );
+
+ let _guard = if let Some(ref ed) = *editor {
+ ed.export_active.store(true, Ordering::Release);
+ tracing::info!("Pausing editor preview during export");
+ tokio::time::sleep(std::time::Duration::from_millis(200)).await;
+ Some(ExportActiveGuard(&ed.export_active))
+ } else {
+ None
};
let result = do_export(&project_path, &settings, &progress, force_ffmpeg).await;
@@ -420,6 +442,10 @@ pub async fn generate_export_preview_fast(
use base64::{Engine, engine::general_purpose::STANDARD};
use std::time::Instant;
+ if editor.export_active.load(Ordering::Acquire) {
+ return Err("Export is in progress - preview generation skipped".to_string());
+ }
+
let project_config = editor.project_config.1.borrow().clone();
let Some((segment_time, segment)) = project_config.get_segment_time(frame_time) else {
diff --git a/crates/editor/src/editor_instance.rs b/crates/editor/src/editor_instance.rs
index f118c0786..04ea8238c 100644
--- a/crates/editor/src/editor_instance.rs
+++ b/crates/editor/src/editor_instance.rs
@@ -87,6 +87,7 @@ pub struct EditorInstance {
pub segment_medias: Arc<Vec<SegmentMedia>>,
meta: RecordingMeta,
pub export_preview_active: AtomicBool,
+ pub export_active: AtomicBool,
}
impl EditorInstance {
@@ -288,6 +289,7 @@ impl EditorInstance {
playback_active: playback_active_tx,
playback_active_rx,
export_preview_active: AtomicBool::new(false),
+ export_active: AtomicBool::new(false),
});
this.state.lock().await.preview_task =
@@ -393,9 +395,11 @@ impl EditorInstance {
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let mut prefetch_cancel_token: Option<CancellationToken> = None;
+ let mut has_rendered_first_frame = false;
loop {
preview_rx.changed().await.unwrap();
+ has_rendered_first_frame = false;
loop {
let Some((frame_number, fps, resolution_base)) =
@@ -412,6 +416,11 @@ impl EditorInstance {
break;
}
+ if self.export_active.load(Ordering::Acquire) {
+ tokio::time::sleep(std::time::Duration::from_millis(100)).await;
+ break;
+ }
+
let project = self.project_config.1.borrow().clone();
let Some((segment_time, segment)) =
@@ -437,7 +446,8 @@ impl EditorInstance {
let playback_is_active = *self.playback_active_rx.borrow();
let export_preview_is_active =
self.export_preview_active.load(Ordering::Acquire);
- if !playback_is_active && !export_preview_is_active {
+ let export_is_active = self.export_active.load(Ordering::Acquire);
+ if !playback_is_active && !export_preview_is_active && !export_is_active {
let prefetch_frames_count = 15u32;
let hide_camera = project.camera.hide;
let playback_rx = self.playback_active_rx.clone();
@@ -474,11 +484,7 @@ impl EditorInstance {
}
}
- let get_frames_future = segment_medias.decoders.get_frames(
- segment_time as f32,
- !project.camera.hide,
- clip_offsets,
- );
+ let use_initial_timeout = !has_rendered_first_frame;
tokio::select! {
biased;
@@ -487,12 +493,28 @@ impl EditorInstance {
continue;
}
- segment_frames_opt = get_frames_future => {
+ segment_frames_opt = async {
+ if use_initial_timeout {
+ segment_medias.decoders.get_frames_initial(
+ segment_time as f32,
+ !project.camera.hide,
+ clip_offsets,
+ ).await
+ } else {
+ segment_medias.decoders.get_frames(
+ segment_time as f32,
+ !project.camera.hide,
+ clip_offsets,
+ ).await
+ }
+ } => {
if preview_rx.has_changed().unwrap_or(false) {
continue;
}
if let Some(segment_frames) = segment_frames_opt {
+ has_rendered_first_frame = true;
+
let total_duration = project
.timeline
.as_ref()