Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(export): retain MOV temporary output during encoding

Aditya Garud68c59483beb5
diff --git a/crates/export/src/mov.rs b/crates/export/src/mov.rs
index 8bb9c0959..c26d17dc3 100644
--- a/crates/export/src/mov.rs
+++ b/crates/export/src/mov.rs
@@ -5,7 +5,7 @@ use cap_rendering::{ProjectUniforms, RenderSegment, RenderedFrame};
use futures::FutureExt;
use serde::{Deserialize, Serialize};
use specta::Type;
-use std::{path::PathBuf, time::Duration};
+use std::{path::PathBuf, sync::Arc, time::Duration};
use crate::{ExportError, ExporterBase, mp4::temporary_export_output};
@@ -79,14 +79,16 @@ impl MovExportSettings {
if let Some(parent) = mov_output_path.parent() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
- let temporary_output = temporary_export_output(&mov_output_path)?;
+ let temporary_output = Arc::new(temporary_export_output(&mov_output_path)?);
let encoder_output_path = temporary_output.to_path_buf();
+ let encoder_temporary_output = temporary_output.clone();
let video_info =
VideoInfo::from_raw(RawVideoFormat::Rgba, output_size.0, output_size.1, fps);
let sample_timing = base.sample_timing.clone();
let encoder_thread = tokio::task::spawn_blocking(move || {
+ let _temporary_output_guard = encoder_temporary_output;
let mut mov_encoder = MOVFile::init(encoder_output_path, |output| {
ProResEncoder::builder(video_info).build(output)
})
@@ -165,7 +167,8 @@ impl MovExportSettings {
let (encoder_result, render_result) = tokio::join!(encoder_thread, render_video_task);
encoder_result?;
render_result?;
- temporary_output
+ Arc::try_unwrap(temporary_output)
+ .map_err(|_| "Export output is still in use".to_string())?
.persist_replace(&mov_output_path)
.map_err(|error| error.to_string())?;
diff --git a/crates/export/tests/mov_cancellation.rs b/crates/export/tests/mov_cancellation.rs
index 0112b7bf2..e6f8f47c4 100644
--- a/crates/export/tests/mov_cancellation.rs
+++ b/crates/export/tests/mov_cancellation.rs
@@ -91,6 +91,48 @@ async fn cancelled_mov_preserves_existing_destination() {
assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 2);
}
+#[tokio::test]
+#[ignore = "requires ffmpeg and a wgpu adapter"]
+async fn dropped_export_keeps_temporary_output_owned_by_encoder() {
+ let directory = tempfile::tempdir().unwrap();
+ let project = directory.path().join("project.cap");
+ create_project(&project);
+ let output = directory.path().join("export.mov");
+ fs::write(&output, b"existing export").unwrap();
+
+ let (started_tx, started_rx) = tokio::sync::oneshot::channel();
+ let (release_tx, release_rx) = std::sync::mpsc::channel();
+ let task = tokio::spawn(async move {
+ let mut started_tx = Some(started_tx);
+ export(&project, &output, move |_| {
+ if let Some(started_tx) = started_tx.take() {
+ let _ = started_tx.send(());
+ }
+ release_rx.recv().unwrap();
+ false
+ })
+ .await
+ });
+
+ started_rx.await.unwrap();
+ task.abort();
+ assert!(task.await.unwrap_err().is_cancelled());
+ let temporary_exists = fs::read_dir(directory.path()).unwrap().any(|entry| {
+ entry
+ .unwrap()
+ .file_name()
+ .to_string_lossy()
+ .starts_with(".cap-export-")
+ });
+ release_tx.send(()).unwrap();
+
+ assert!(temporary_exists);
+ assert_eq!(
+ fs::read(directory.path().join("export.mov")).unwrap(),
+ b"existing export"
+ );
+}
+
#[tokio::test]
#[ignore = "requires ffmpeg and a wgpu adapter"]
async fn finished_mov_replaces_existing_destination() {