Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop): bound thumbnail decoding without caching partial frames

Richie McIlroy16e3904ff440
diff --git a/apps/desktop-gpui/src/editor_clips.rs b/apps/desktop-gpui/src/editor_clips.rs
index 4d0fb144a..72f90015b 100644
--- a/apps/desktop-gpui/src/editor_clips.rs
+++ b/apps/desktop-gpui/src/editor_clips.rs
@@ -2079,7 +2079,7 @@ fn show_append_error(message: &str) {
/// 96px cards at 2x (`w-24` at `ClipsSidebar.tsx:881`).
const THUMB_MAX_WIDTH: u32 = 192;
-const SEEK_DECODE_PACKET_LIMIT: usize = 240;
+const SEEK_DECODE_PACKET_LIMIT: usize = 4096;
fn decode_clip_thumbnail(
project_path: &Path,
@@ -2105,6 +2105,20 @@ fn decode_clip_thumbnail(
}
fn decode_thumbnail_frame(input: &Path, time: f64) -> Result<Arc<RenderImage>, String> {
+ decode_thumbnail_frame_with_budget(
+ input,
+ time,
+ SEEK_DECODE_PACKET_LIMIT,
+ std::time::Duration::from_secs(2),
+ )
+}
+
+fn decode_thumbnail_frame_with_budget(
+ input: &Path,
+ time: f64,
+ packet_limit: usize,
+ timeout: std::time::Duration,
+) -> Result<Arc<RenderImage>, String> {
use ffmpeg::rescale::{Rescale, TIME_BASE};
let mut ictx =
@@ -2167,8 +2181,12 @@ fn decode_thumbnail_frame(input: &Path, time: f64) -> Result<Arc<RenderImage>, S
let mut reached_target = false;
let mut decoder_finished = false;
let mut packets_tried = 0usize;
+ let decode_started = std::time::Instant::now();
'outer: for (packet_stream, packet) in ictx.packets() {
+ if decode_started.elapsed() >= timeout {
+ return Err("Thumbnail decode time budget exhausted".to_string());
+ }
if packet_stream.index() != stream_index {
continue;
}
@@ -2176,13 +2194,16 @@ fn decode_thumbnail_frame(input: &Path, time: f64) -> Result<Arc<RenderImage>, S
packets_tried += 1;
if decoder.send_packet(&packet).is_err() {
- if packets_tried >= SEEK_DECODE_PACKET_LIMIT {
- break;
+ if packets_tried >= packet_limit {
+ return Err("Thumbnail decode packet budget exhausted".to_string());
}
continue;
}
loop {
+ if decode_started.elapsed() >= timeout {
+ return Err("Thumbnail decode time budget exhausted".to_string());
+ }
match decoder.receive_frame(&mut decoded) {
Ok(()) => {
std::mem::swap(&mut frame, &mut decoded);
@@ -2198,7 +2219,7 @@ fn decode_thumbnail_frame(input: &Path, time: f64) -> Result<Arc<RenderImage>, S
break 'outer;
}
Err(e) => {
- if packets_tried >= SEEK_DECODE_PACKET_LIMIT {
+ if packets_tried >= packet_limit {
return Err(format!("Failed to decode frame: {e}"));
}
break;
@@ -2206,8 +2227,8 @@ fn decode_thumbnail_frame(input: &Path, time: f64) -> Result<Arc<RenderImage>, S
}
}
- if packets_tried >= SEEK_DECODE_PACKET_LIMIT {
- break;
+ if packets_tried >= packet_limit {
+ return Err("Thumbnail decode packet budget exhausted".to_string());
}
}
@@ -2216,6 +2237,9 @@ fn decode_thumbnail_frame(input: &Path, time: f64) -> Result<Arc<RenderImage>, S
.send_eof()
.map_err(|e| format!("Failed to flush decoder: {e}"))?;
loop {
+ if decode_started.elapsed() >= timeout {
+ return Err("Thumbnail decode time budget exhausted".to_string());
+ }
match decoder.receive_frame(&mut decoded) {
Ok(()) => {
std::mem::swap(&mut frame, &mut decoded);
@@ -3387,6 +3411,41 @@ mod tests {
}
}
+ #[test]
+ fn long_gop_thumbnails_reach_requested_time_and_preserve_eof_fallback() {
+ let fixture = ImportFixture::new(include_bytes!(
+ "../../desktop/src-tauri/test-data/clip-thumbnail-long-gop.mp4"
+ ));
+ for time in [8.5, 10.0, 11.0] {
+ let thumbnail = decode_thumbnail_frame(&fixture.source, time).unwrap();
+ let data = thumbnail.as_bytes(0).unwrap();
+ let pixel = &data[..4];
+ assert!(
+ pixel[0] > 100 && pixel[1] < 20 && pixel[2] < 20,
+ "requested {time}s, got {pixel:?}"
+ );
+ }
+ }
+
+ #[test]
+ fn exhausted_decode_budget_does_not_return_partial_thumbnail() {
+ let fixture = ImportFixture::new(include_bytes!(
+ "../../desktop/src-tauri/test-data/clip-thumbnail-long-gop.mp4"
+ ));
+ for (packet_limit, timeout, expected_error) in [
+ (240, std::time::Duration::from_secs(2), "packet budget"),
+ (
+ SEEK_DECODE_PACKET_LIMIT,
+ std::time::Duration::ZERO,
+ "time budget",
+ ),
+ ] {
+ let result =
+ decode_thumbnail_frame_with_budget(&fixture.source, 8.5, packet_limit, timeout);
+ assert!(result.unwrap_err().contains(expected_error));
+ }
+ }
+
const MP4_WITHOUT_AUDIO: &[u8] =
include_bytes!("../../media-server/src/__tests__/fixtures/test-no-audio.mp4");
const MP4_WITH_AUDIO: &[u8] =
diff --git a/apps/desktop/src-tauri/src/clip_thumbnails.rs b/apps/desktop/src-tauri/src/clip_thumbnails.rs
index 929803550..65860a5ad 100644
--- a/apps/desktop/src-tauri/src/clip_thumbnails.rs
+++ b/apps/desktop/src-tauri/src/clip_thumbnails.rs
@@ -7,7 +7,8 @@ use crate::editor_window::WindowEditorInstance;
const THUMB_MAX_WIDTH: u32 = 240;
const THUMB_JPEG_QUALITY: u8 = 70;
-const SEEK_DECODE_PACKET_LIMIT: usize = 240;
+const SEEK_DECODE_PACKET_LIMIT: usize = 4096;
+const SEEK_DECODE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(2);
static THUMBNAIL_SEMAPHORE: LazyLock<Semaphore> = LazyLock::new(|| Semaphore::new(4));
@@ -71,6 +72,22 @@ pub async fn get_clip_thumbnail(
}
fn decode_clip_thumbnail(input: &Path, time: f64, output: &Path) -> Result<(), String> {
+ decode_clip_thumbnail_with_budget(
+ input,
+ time,
+ output,
+ SEEK_DECODE_PACKET_LIMIT,
+ SEEK_DECODE_TIMEOUT,
+ )
+}
+
+fn decode_clip_thumbnail_with_budget(
+ input: &Path,
+ time: f64,
+ output: &Path,
+ packet_limit: usize,
+ timeout: std::time::Duration,
+) -> Result<(), String> {
use ffmpeg::rescale::{Rescale, TIME_BASE};
let mut ictx =
@@ -131,8 +148,12 @@ fn decode_clip_thumbnail(input: &Path, time: f64, output: &Path) -> Result<(), S
let mut reached_target = false;
let mut decoder_finished = false;
let mut packets_tried = 0usize;
+ let decode_started = std::time::Instant::now();
'outer: for (packet_stream, packet) in ictx.packets() {
+ if decode_started.elapsed() >= timeout {
+ return Err("Thumbnail decode time budget exhausted".to_string());
+ }
if packet_stream.index() != stream_index {
continue;
}
@@ -140,13 +161,16 @@ fn decode_clip_thumbnail(input: &Path, time: f64, output: &Path) -> Result<(), S
packets_tried += 1;
if decoder.send_packet(&packet).is_err() {
- if packets_tried >= SEEK_DECODE_PACKET_LIMIT {
- break;
+ if packets_tried >= packet_limit {
+ return Err("Thumbnail decode packet budget exhausted".to_string());
}
continue;
}
loop {
+ if decode_started.elapsed() >= timeout {
+ return Err("Thumbnail decode time budget exhausted".to_string());
+ }
match decoder.receive_frame(&mut decoded) {
Ok(()) => {
std::mem::swap(&mut frame, &mut decoded);
@@ -162,7 +186,7 @@ fn decode_clip_thumbnail(input: &Path, time: f64, output: &Path) -> Result<(), S
break 'outer;
}
Err(e) => {
- if packets_tried >= SEEK_DECODE_PACKET_LIMIT {
+ if packets_tried >= packet_limit {
return Err(format!("Failed to decode frame: {e}"));
}
break;
@@ -170,8 +194,8 @@ fn decode_clip_thumbnail(input: &Path, time: f64, output: &Path) -> Result<(), S
}
}
- if packets_tried >= SEEK_DECODE_PACKET_LIMIT {
- break;
+ if packets_tried >= packet_limit {
+ return Err("Thumbnail decode packet budget exhausted".to_string());
}
}
@@ -180,6 +204,9 @@ fn decode_clip_thumbnail(input: &Path, time: f64, output: &Path) -> Result<(), S
.send_eof()
.map_err(|e| format!("Failed to flush decoder: {e}"))?;
loop {
+ if decode_started.elapsed() >= timeout {
+ return Err("Thumbnail decode time budget exhausted".to_string());
+ }
match decoder.receive_frame(&mut decoded) {
Ok(()) => {
std::mem::swap(&mut frame, &mut decoded);
@@ -322,14 +349,14 @@ mod decoding_tests {
};
fn fixture() -> (tempfile::TempDir, PathBuf) {
+ fixture_from_bytes(include_bytes!("../test-data/clip-thumbnail-gop.mp4"))
+ }
+
+ fn fixture_from_bytes(bytes: &[u8]) -> (tempfile::TempDir, PathBuf) {
ffmpeg::init().unwrap();
let root = tempfile::tempdir().unwrap();
let input = root.path().join("gop.mp4");
- std::fs::write(
- &input,
- include_bytes!("../test-data/clip-thumbnail-gop.mp4"),
- )
- .unwrap();
+ std::fs::write(&input, bytes).unwrap();
(root, input)
}
@@ -381,6 +408,41 @@ mod decoding_tests {
assert!(pixel[1] > 100 && pixel[0] < 20 && pixel[2] < 20);
}
+ #[test]
+ fn long_gop_thumbnails_reach_requested_time_and_preserve_eof_fallback() {
+ let (root, input) =
+ fixture_from_bytes(include_bytes!("../test-data/clip-thumbnail-long-gop.mp4"));
+ for time in [8.5, 10.0, 11.0] {
+ let output = root.path().join(format!("{time}.jpg"));
+ decode_clip_thumbnail(&input, time, &output).unwrap();
+ let pixel = image::open(&output).unwrap().to_rgb8().get_pixel(80, 45).0;
+ assert!(
+ pixel[2] > 100 && pixel[1] < 20 && pixel[0] < 20,
+ "requested {time}s, got {pixel:?}"
+ );
+ }
+ }
+
+ #[test]
+ fn exhausted_decode_budget_does_not_write_partial_thumbnail() {
+ let (root, input) =
+ fixture_from_bytes(include_bytes!("../test-data/clip-thumbnail-long-gop.mp4"));
+ let output = root.path().join("thumbnail.jpg");
+ for (packet_limit, timeout, expected_error) in [
+ (240, std::time::Duration::from_secs(2), "packet budget"),
+ (
+ SEEK_DECODE_PACKET_LIMIT,
+ std::time::Duration::ZERO,
+ "time budget",
+ ),
+ ] {
+ let result =
+ decode_clip_thumbnail_with_budget(&input, 8.5, &output, packet_limit, timeout);
+ assert!(result.unwrap_err().contains(expected_error));
+ assert!(!output.exists());
+ }
+ }
+
#[test]
fn frames_without_timestamps_preserve_first_frame_fallback() {
let mut frame = ffmpeg::frame::Video::empty();
diff --git a/apps/desktop/src-tauri/test-data/clip-thumbnail-long-gop.mp4 b/apps/desktop/src-tauri/test-data/clip-thumbnail-long-gop.mp4
new file mode 100644
index 000000000..d7078da84
Binary files /dev/null and b/apps/desktop/src-tauri/test-data/clip-thumbnail-long-gop.mp4 differ