@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 87 lines 3 KB
Raw
1 import type { VideoThumbnail } from "expo-video";
2
3 import type { AttachmentPreviewFile } from "./attachmentDownload";
4
5 const thumbnails = new Map<string, VideoThumbnail>();
6 const MAX_CACHED_THUMBNAILS = 32;
7 let pending: Promise<unknown> = Promise.resolve();
8
9 export function cachedVideoThumbnail(key: string): VideoThumbnail | null {
10 return thumbnails.get(key) ?? null;
11 }
12
13 async function extractFrame(uri: string) {
14 const { createVideoPlayer } = await import("expo-video");
15 const player = createVideoPlayer(null);
16 let disposed = false;
17 let timeout: ReturnType<typeof setTimeout> | undefined;
18 let attachedSubscription: { remove(): void } | undefined;
19 try {
20 // Never play or change audio settings: thumbnails must leave the shared audio session alone.
21 player.bufferOptions = { preferredForwardBufferDuration: 1 };
22 const timedOut = new Promise<null>((resolve) => {
23 timeout = setTimeout(() => resolve(null), 15_000);
24 });
25 // iOS resolves replaceAsync before the main thread attaches the item, and until then
26 // generateThumbnailsAsync finds no asset and returns no frames.
27 const attached = new Promise<void>((resolve) => {
28 attachedSubscription = player.addListener("sourceChange", ({ source }) => {
29 if (source) resolve();
30 });
31 });
32 const frame = (async () => {
33 await player.replaceAsync({ uri, contentType: "progressive" });
34 await attached;
35 if (disposed) return null;
36 const [thumbnail] = await player.generateThumbnailsAsync([0], {
37 maxWidth: 480,
38 maxHeight: 480,
39 });
40 return thumbnail ?? null;
41 })();
42 return await Promise.race([frame, timedOut]);
43 } finally {
44 disposed = true;
45 clearTimeout(timeout);
46 attachedSubscription?.remove();
47 player.release();
48 }
49 }
50
51 export function loadVideoThumbnail(
52 key: string,
53 resolveSource: (
54 signal: AbortSignal,
55 ) => Promise<Pick<AttachmentPreviewFile, "uri" | "dispose"> | null>,
56 signal: AbortSignal,
57 ): Promise<VideoThumbnail | null> {
58 if (signal.aborted) return Promise.resolve(null);
59 const cached = cachedVideoThumbnail(key);
60 if (cached) return Promise.resolve(cached);
61 const load = pending
62 .then(async () => {
63 if (signal.aborted) return null;
64 const cached = cachedVideoThumbnail(key);
65 if (cached) return cached;
66
67 const source = await resolveSource(signal);
68 if (!source) return null;
69 try {
70 if (signal.aborted) return null;
71 // A started extraction finishes even if its reader leaves: its requests are already in
72 // flight, and a re-signed URL for the same video replaces the reader, not the frame.
73 const thumbnail = await extractFrame(source.uri);
74 if (!thumbnail) return null;
75 thumbnails.set(key, thumbnail);
76 if (thumbnails.size > MAX_CACHED_THUMBNAILS) {
77 thumbnails.delete(thumbnails.keys().next().value!);
78 }
79 return thumbnail;
80 } finally {
81 source.dispose();
82 }
83 })
84 .catch(() => null);
85 pending = load;
86 return load;
87 }