Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add retry logic to export preview and track preview activity

Richie McIlroya85a41a05d0c
diff --git a/apps/desktop/src/routes/editor/ExportPage.tsx b/apps/desktop/src/routes/editor/ExportPage.tsx
index c1d3f1cec..b905656a6 100644
--- a/apps/desktop/src/routes/editor/ExportPage.tsx
+++ b/apps/desktop/src/routes/editor/ExportPage.tsx
@@ -239,6 +239,7 @@ export function ExportPage() {
resWidth: number,
resHeight: number,
bpp: number,
+ retryCount = 0,
) => {
const cacheKey = getEstimateCacheKey(fps, resWidth, resHeight, bpp);
const cachedEstimate = estimateCache.get(cacheKey);
@@ -247,6 +248,8 @@ export function ExportPage() {
setRenderEstimate(cachedEstimate);
}
+ const maxRetries = 2;
+
try {
const result = await commands.generateExportPreviewFast(frameTime, {
fps,
@@ -275,6 +278,19 @@ export function ExportPage() {
setRenderEstimate(newEstimate);
} catch (e) {
console.error("Failed to generate preview:", e);
+ if (retryCount < maxRetries) {
+ await new Promise((resolve) =>
+ setTimeout(resolve, 200 * (retryCount + 1)),
+ );
+ return fetchPreview(
+ frameTime,
+ fps,
+ resWidth,
+ resHeight,
+ bpp,
+ retryCount + 1,
+ );
+ }
} finally {
setPreviewLoading(false);
}
diff --git a/crates/editor/src/editor_instance.rs b/crates/editor/src/editor_instance.rs
index fd0c33d9e..da5055931 100644
--- a/crates/editor/src/editor_instance.rs
+++ b/crates/editor/src/editor_instance.rs
@@ -10,7 +10,13 @@ use cap_rendering::{
ProjectRecordingsMeta, ProjectUniforms, RecordingSegmentDecoders, RenderVideoConstants,
RenderedFrame, SegmentVideoPaths, Video, get_duration,
};
-use std::{path::PathBuf, sync::Arc};
+use std::{
+ path::PathBuf,
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ },
+};
use tokio::sync::{Mutex, watch};
use tokio_util::sync::CancellationToken;
use tracing::warn;
@@ -32,6 +38,7 @@ pub struct EditorInstance {
// ws_shutdown_token: CancellationToken,
pub segment_medias: Arc<Vec<SegmentMedia>>,
meta: RecordingMeta,
+ pub export_preview_active: AtomicBool,
}
impl EditorInstance {
@@ -172,6 +179,7 @@ impl EditorInstance {
meta: recording_meta,
playback_active: playback_active_tx,
playback_active_rx,
+ export_preview_active: AtomicBool::new(false),
});
this.state.lock().await.preview_task =
@@ -326,7 +334,9 @@ impl EditorInstance {
prefetch_cancel_token = Some(new_cancel_token.clone());
let playback_is_active = *self.playback_active_rx.borrow();
- if !playback_is_active {
+ let export_preview_is_active =
+ self.export_preview_active.load(Ordering::Acquire);
+ if !playback_is_active && !export_preview_is_active {
let prefetch_frames_count = 5u32;
let hide_camera = project.camera.hide;
let playback_rx = self.playback_active_rx.clone();