Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Merge remote-tracking branch 'upstream/main' into macos-menus

Ilya7a7b8df4a7f4
diff --git a/.config/nextest.toml b/.config/nextest.toml
index a9f409ed4..466fdbcac 100644
--- a/.config/nextest.toml
+++ b/.config/nextest.toml
@@ -11,6 +11,17 @@ slow-timeout = { period = "60s" }
filter = 'binary_id(cap-rendering) and test(/^decoder::model::/)'
threads-required = 3
+# Wall-clock tests run first and alone. crash_safety hands frames to a
+# real-time VideoToolbox session on a 30 fps schedule, and realtime_loop
+# counts the frames playback presents against the clock. Sharing the runner
+# with the seek storms, chaos and export tests starves them. On macOS,
+# crash/equality lost 3 to 7 frames in 6 shared runs and passed in all 6
+# solo reruns; realtime30 presented 65% of its frames shared and passed solo.
+[[profile.default.overrides]]
+filter = '(binary_id(cap-verify::crash) and test(=crash_safety)) or (binary_id(cap-verify::playback) and test(=realtime_loop))'
+threads-required = "num-test-threads"
+priority = 100
+
[profile.ci]
# No retries: a flaky case is a bug. run.sh reruns failures once afterwards
# only to label them flaky in the job summary; the job still fails.
@@ -31,6 +42,12 @@ store-failure-output = true
filter = 'binary_id(cap-rendering) and test(/^decoder::model::/)'
threads-required = 3
+# Wall-clock tests run first and alone, as in the default profile.
+[[profile.ci.overrides]]
+filter = '(binary_id(cap-verify::crash) and test(=crash_safety)) or (binary_id(cap-verify::playback) and test(=realtime_loop))'
+threads-required = "num-test-threads"
+priority = 100
+
# The FULL-tier seek storms (3000 seeks per decoder) are the slowest tests:
# on the macos-15 runner one storm fixture takes 240 to 570 s per decoder,
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 57810c005..f18abceea 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -24,6 +24,7 @@ jobs:
rust: ${{ steps.filter.outputs.rust }}
gpui: ${{ steps.filter.outputs.rust == 'true' || steps.filter.outputs.gpui == 'true' || steps.desktop-resources.outputs.desktop-resources == 'true' }}
tauri-plugins: ${{ steps.filter.outputs.tauri-plugins }}
+ gpui-patches: ${{ steps.filter.outputs.gpui-patches }}
steps:
- name: Checkout
uses: actions/checkout@v4
@@ -42,7 +43,7 @@ jobs:
- 'crates/**'
- 'vendor/**'
- 'scripts/diagnostic-build.rs'
- - 'scripts/setup.js'
+ - 'scripts/setup*'
- 'Cargo.toml'
- 'Cargo.lock'
gpui:
@@ -65,6 +66,9 @@ jobs:
- 'scripts/sync-desktop-versions.mjs'
- 'scripts/verify-gpui-release-inputs.mjs'
- 'scripts/verify-windows-stack.mjs'
+ gpui-patches:
+ - 'apps/desktop-gpui/patches/**'
+ - 'scripts/prepare-gpui-dependency.sh'
tauri-plugins:
- 'Cargo.lock'
- 'bun.lock'
@@ -123,10 +127,12 @@ jobs:
__tests__/unit/linkified-text.test.ts
__tests__/unit/loom-import-ui.test.ts
__tests__/unit/media-player-store-refs.test.ts
+ __tests__/unit/playback-store.test.ts
__tests__/unit/seo-demo-videos.test.ts
__tests__/unit/signed-baa-card.test.ts
__tests__/unit/summary-editor.test.ts
__tests__/unit/sso-login-pages.test.ts
+ __tests__/unit/transcript-numbers.test.ts
- name: Typecheck
run: bun run typecheck
@@ -273,6 +279,9 @@ jobs:
prefix-key: v1-rust-tests
save-cache: ${{ github.ref == 'refs/heads/main' }}
+ - name: Test native dependency setup
+ run: node --test scripts/setup.test.mjs
+
- name: Run setup
run: node scripts/setup.js
@@ -280,8 +289,11 @@ jobs:
run: |
cargo test --locked -p cap-utils -p cap-cli-install --lib
cargo test --locked -p cap --bin cap record::tests
+ cargo test --locked -p cap --bin cap recordings::tests
+ cargo test --locked -p cap --test cli recordings_list
cargo test --locked -p cap-recording --lib -- --test-threads=1
cargo test --locked -p cap-recording --test x11_shm_capture -- --test-threads=1
+ cargo test --locked -p scap-targets --lib --test linux_targets
cargo test --locked -p cap-enc-ffmpeg
build-gpui:
@@ -370,6 +382,70 @@ jobs:
fi
cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --target ${{ matrix.settings.target }} -p cap-gpui-kernels
+ # Cap only builds gpui's crates as dependencies, so their own tests never run
+ # in the app jobs above. Run the ones covering Cap's patches against real
+ # devices: Metal on macOS, WARP on Windows, and Mesa's lavapipe (Vulkan) and
+ # llvmpipe (GL) on Linux. Windows renders with D3D11 and its runners expose
+ # no wgpu adapter, so wgpu's tests run on macOS and Linux only. Zed's lockfile predates the patches' dependency
+ # changes, so these builds cannot use --locked.
+ gpui-patch-tests:
+ name: GPUI patch tests (${{ matrix.settings.name }})
+ needs: changes
+ if: needs.changes.outputs.gpui-patches == 'true'
+ timeout-minutes: 90
+ strategy:
+ fail-fast: false
+ matrix:
+ settings:
+ - name: macOS
+ runner: blacksmith-6vcpu-macos-15
+ - name: Windows
+ runner: blacksmith-8vcpu-windows-2025
+ - name: Linux
+ runner: blacksmith-8vcpu-ubuntu-2404
+ runs-on: ${{ matrix.settings.runner }}
+ env:
+ CARGO_PROFILE_DEV_DEBUG: "0"
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Install Linux desktop dependencies
+ if: ${{ runner.os == 'Linux' }}
+ uses: ./.github/actions/install-desktop-deps
+
+ - name: Install software GPU drivers
+ if: ${{ runner.os == 'Linux' }}
+ run: sudo apt-get install -y --no-install-recommends mesa-vulkan-drivers libvulkan1 libegl1 libegl-mesa0 libgl1-mesa-dri
+
+ - name: Allow long paths in Zed's git dependencies
+ if: ${{ runner.os == 'Windows' }}
+ run: git config --global core.longpaths true
+
+ - name: Rust setup
+ uses: dtolnay/[email protected]
+
+ - name: Prepare GPUI dependency
+ shell: bash
+ run: ./scripts/prepare-gpui-dependency.sh
+
+ - name: Test GPUI patches
+ shell: bash
+ working-directory: ../zed-cap
+ env:
+ GPUI_REQUIRE_TEST_DEVICE: "1"
+ run: |
+ cargo +1.95.0 test -p gpui --lib stream
+ if [[ "$RUNNER_OS" == "Linux" ]]; then
+ WGPU_BACKEND=vulkan cargo +1.95.0 test -p gpui_wgpu --lib wgpu_atlas -- --nocapture
+ WGPU_BACKEND=gl cargo +1.95.0 test -p gpui_wgpu --lib wgpu_atlas -- --nocapture
+ elif [[ "$RUNNER_OS" == "macOS" ]]; then
+ cargo +1.95.0 test -p gpui_wgpu --lib wgpu_atlas -- --nocapture
+ cargo +1.95.0 test -p gpui_macos --lib -- metal_atlas metal_renderer
+ else
+ cargo +1.95.0 test -p gpui_windows --lib directx_atlas
+ fi
+
tauri-plugins:
name: Verify Tauri plugin versions
runs-on: blacksmith-8vcpu-ubuntu-2404
diff --git a/.github/workflows/engine-verify-os.yml b/.github/workflows/engine-verify-os.yml
index 95da06162..a6cd1398c 100644
--- a/.github/workflows/engine-verify-os.yml
+++ b/.github/workflows/engine-verify-os.yml
@@ -178,6 +178,13 @@ jobs:
name: verify-archive-${{ inputs.platform }}-${{ github.run_attempt }}
path: ${{ runner.temp }}
+ # A test process that crashes (SIGSEGV) leaves no Rust backtrace;
+ # macOS writes a crash report instead, collected below on failure.
+ - name: Mark crash report start
+ if: inputs.platform == 'macos'
+ shell: bash
+ run: touch "$RUNNER_TEMP/crash-reports-since"
+
- name: Prepare recorded fixtures without concurrent rendering
if: contains(format(',{0},', inputs.suites), ',export,')
shell: bash
@@ -217,8 +224,27 @@ jobs:
--junit "$CAP_VERIFY_REPORT_DIR/junit.xml" --flaky "$CAP_VERIFY_FLAKY_FILE"
"$CAP_VERIFY_REPORT_DIR" >> "$GITHUB_STEP_SUMMARY"
+ - name: Collect macOS crash reports
+ id: crash-reports
+ if: failure() && inputs.platform == 'macos'
+ continue-on-error: true
+ shell: bash
+ run: |
+ dest="$RUNNER_TEMP/verify-report/crash-reports"
+ for dir in "$HOME/Library/Logs/DiagnosticReports" /Library/Logs/DiagnosticReports; do
+ [[ -d "$dir" ]] || continue
+ { find "$dir" -type f \( -name '*.ips' -o -name '*.crash' \) -newer "$RUNNER_TEMP/crash-reports-since" -print0 2>/dev/null || true; } |
+ while IFS= read -r -d '' report; do
+ mkdir -p "$dest"
+ cp "$report" "$dest/" && echo "collected $report" || echo "could not copy $report"
+ done
+ done
+ if [[ -n "$(ls -A "$dest" 2>/dev/null)" ]]; then
+ echo "collected=true" >> "$GITHUB_OUTPUT"
+ fi
+
- name: Upload report
- if: always() && steps.test.outcome != 'skipped'
+ if: always() && (steps.test.outcome != 'skipped' || steps.crash-reports.outputs.collected == 'true')
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: verify-report-${{ inputs.platform }}-${{ inputs.tier }}-${{ matrix.shard }}
diff --git a/.github/workflows/opencode.yml b/.github/workflows/opencode.yml
deleted file mode 100644
index ec7b6a367..000000000
--- a/.github/workflows/opencode.yml
+++ /dev/null
@@ -1,29 +0,0 @@
-name: opencode
-
-on:
- issue_comment:
- types: [created]
-
-jobs:
- opencode:
- if: |
- contains(github.event.comment.body, ' /oc') ||
- startsWith(github.event.comment.body, '/oc') ||
- contains(github.event.comment.body, ' /opencode') ||
- startsWith(github.event.comment.body, '/opencode')
- runs-on: ubuntu-latest
- permissions:
- id-token: write
- contents: read
- pull-requests: read
- issues: read
- steps:
- - name: Checkout repository
- uses: actions/checkout@v4
-
- - name: Run opencode
- uses: sst/opencode/github@latest
- env:
- OPENCODE_API_KEY: ${{ secrets.OPENCODE_API_KEY }}
- with:
- model: opencode/claude-sonnet-4-5
\ No newline at end of file
diff --git a/.github/workflows/perf-budget.yml b/.github/workflows/perf-budget.yml
index 999f0b463..401f5a036 100644
--- a/.github/workflows/perf-budget.yml
+++ b/.github/workflows/perf-budget.yml
@@ -29,6 +29,8 @@ jobs:
- uses: actions/checkout@v4
with:
persist-credentials: false
+ - name: Test performance report isolation
+ run: python3 -B scripts/verify/test-perf-ab.py
- name: Scope
id: scope
env:
diff --git a/.github/workflows/recording-reliability.yml b/.github/workflows/recording-reliability.yml
index 74838a71b..d7be8f0c1 100644
--- a/.github/workflows/recording-reliability.yml
+++ b/.github/workflows/recording-reliability.yml
@@ -46,6 +46,7 @@ jobs:
__tests__/unit/recording-storage-lifecycle.test.ts \
__tests__/unit/desktop-recording-*.test.ts \
__tests__/unit/desktop-segments-*.test.ts \
+ __tests__/unit/segments-audio-download.test.ts \
__tests__/unit/finalize-desktop-recording.test.ts \
__tests__/unit/media-server-progress.test.ts \
__tests__/unit/media-processing-budget.test.ts \
@@ -56,4 +57,7 @@ jobs:
__tests__/unit/video-edit-published-output.test.ts \
__tests__/unit/upload-progress-playback.test.ts \
__tests__/unit/recording-prepare.test.ts \
- __tests__/unit/s3-bucket-connections.test.ts
+ __tests__/unit/s3-bucket-connections.test.ts \
+ __tests__/unit/media-processing-workflows.test.ts \
+ __tests__/unit/new-media-comment-thumbnail.test.ts \
+ __tests__/unit/comment-reply-parent.test.ts
diff --git a/.github/workflows/sync-tests.yml b/.github/workflows/sync-tests.yml
index 1228d40e5..0e7cc1049 100644
--- a/.github/workflows/sync-tests.yml
+++ b/.github/workflows/sync-tests.yml
@@ -146,7 +146,7 @@ jobs:
- name: Timestamp pipeline unit + property tests
shell: bash
run: |
- cargo test --locked -p cap-timestamp -p cap-enc-ffmpeg
+ cargo test --locked -p cap-timestamp -p cap-enc-ffmpeg -p ffmpeg-hw-device
cargo test --locked -p cap-camera-effects --lib
cargo test --locked -p cap-recording --lib -- --test-threads=1
# --nocapture so a WARP-adapter notch skip prints instead of
diff --git a/.github/workflows/verify-recording.yml b/.github/workflows/verify-recording.yml
index 671fe6b73..3420e677f 100644
--- a/.github/workflows/verify-recording.yml
+++ b/.github/workflows/verify-recording.yml
@@ -251,6 +251,15 @@ jobs:
done
SwitchAudioSource -s "BlackHole 2ch" -t output
SwitchAudioSource -s "BlackHole 2ch" -t input
+ # macOS 15 plays alert and interface sounds through the default
+ # output, BlackHole, so ScreenCaptureKit records them as system
+ # audio and Instant's mixed track and every export carry them over
+ # the microphone markers. Hosted runners and macOS 26 capture
+ # silence there; silence these too so every runner records the same.
+ osascript -e 'set volume alert volume 0' || true
+ defaults write -g com.apple.sound.uiaudio.enabled -int 0
+ defaults write -g com.apple.sound.beep.volume -float 0
+ defaults write -g com.apple.sound.beep.feedback -int 0
echo "outputs:"; SwitchAudioSource -a -t output
echo "default output: $(SwitchAudioSource -c -t output)"
diff --git a/.gitignore b/.gitignore
index e7b19bc4a..66b2c0900 100644
--- a/.gitignore
+++ b/.gitignore
@@ -38,6 +38,7 @@ outputs
.zed
.output
.vinxi
+*.config.timestamp_*.js
native-deps*
.tinyb
diff --git a/README.md b/README.md
index 425327695..389a250fb 100644
--- a/README.md
+++ b/README.md
@@ -110,6 +110,10 @@ S3_PUBLIC_URL=https://s3.yourdomain.com
See the [self-hosting guide](https://cap.so/docs/self-hosting) for email setup, AI providers, SSL, storage, production hardening, and troubleshooting.
+For single-owner instances behind an authenticating reverse proxy, see
+[trusted-proxy authentication](docs/trusted-proxy-auth.md) for the required shared
+secret, proxy hardening, and how to trigger sign-in without a built-in login button.
+
## Local Development
Cap is a Turborepo monorepo with Rust, TypeScript, Tauri, SolidStart, Next.js, Drizzle, MySQL, Tailwind CSS, and shared media crates.
diff --git a/apps/chrome-extension/src/background/service-worker.test.ts b/apps/chrome-extension/src/background/service-worker.test.ts
new file mode 100644
index 000000000..fc25cca41
--- /dev/null
+++ b/apps/chrome-extension/src/background/service-worker.test.ts
@@ -0,0 +1,177 @@
+import type { VideoId } from "@cap/recorder-core";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { RECORDING_STATE_KEY } from "../shared/storage-keys";
+import type {
+ OffscreenResponse,
+ RecordingStatus,
+ ServiceWorkerResponse,
+} from "../shared/types";
+
+type MessageListener = Parameters<
+ typeof chrome.runtime.onMessage.addListener
+>[0];
+
+let onMessage: MessageListener;
+let stored: Record<string, unknown>;
+let pollReplies: Array<(response: OffscreenResponse) => void>;
+let contextReplies: Array<(contexts: object[]) => void>;
+
+const liveStatus: RecordingStatus = {
+ phase: "recording",
+ startedAt: 1,
+ durationMs: 1000,
+ updatedAt: 1001,
+};
+
+beforeEach(async () => {
+ vi.resetModules();
+ stored = {};
+ pollReplies = [];
+ contextReplies = [];
+ const storage = {
+ get: (_keys: string[], callback: (items: object) => void) =>
+ callback({ ...stored }),
+ set: (items: object, callback: () => void) => {
+ Object.assign(stored, items);
+ callback();
+ },
+ setAccessLevel: vi.fn(),
+ };
+ vi.stubGlobal("chrome", {
+ storage: { local: storage, session: storage },
+ runtime: {
+ getURL: (path: string) => `chrome-extension://test/${path}`,
+ ContextType: { OFFSCREEN_DOCUMENT: "OFFSCREEN_DOCUMENT" },
+ getContexts: vi.fn((_filter, callback) => {
+ contextReplies.push(callback);
+ }),
+ sendMessage: vi.fn((_message, callback) => {
+ pollReplies.push(callback);
+ }),
+ onMessage: {
+ addListener: (listener: MessageListener) => {
+ onMessage = listener;
+ },
+ },
+ onInstalled: { addListener: vi.fn() },
+ },
+ action: {
+ setPopup: vi.fn((_options, callback) => callback()),
+ setBadgeText: vi.fn((_options, callback) => callback()),
+ setTitle: vi.fn((_options, callback) => callback()),
+ onClicked: { addListener: vi.fn() },
+ },
+ tabs: {
+ query: (_query: object, callback: (tabs: object[]) => void) =>
+ callback([]),
+ onActivated: { addListener: vi.fn() },
+ onUpdated: { addListener: vi.fn() },
+ onRemoved: { addListener: vi.fn() },
+ },
+ windows: { onFocusChanged: { addListener: vi.fn() } },
+ });
+ await import("./service-worker");
+});
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+const broadcast = (status: RecordingStatus) =>
+ onMessage(
+ {
+ target: "recording-status",
+ type: "recording-status-changed",
+ status,
+ },
+ {},
+ vi.fn(),
+ );
+
+const pollStatus = () =>
+ new Promise<ServiceWorkerResponse>((resolve) => {
+ onMessage(
+ { target: "service-worker", type: "get-recording-status" },
+ {},
+ resolve,
+ );
+ });
+
+const answerContexts = (present: boolean) => {
+ const reply = contextReplies.shift();
+ if (!reply) throw new Error("No pending context query");
+ reply(
+ present ? [{ documentUrl: "chrome-extension://test/offscreen.html" }] : [],
+ );
+};
+
+describe("recording status synchronization", () => {
+ it.each(["recording", "paused", "uploading"] as const)(
+ "restores %s from the offscreen document after a worker restart",
+ async (phase) => {
+ const currentStatus = { ...liveStatus, phase };
+ const response = pollStatus();
+ answerContexts(true);
+ await vi.waitFor(() => expect(contextReplies).toHaveLength(1));
+ answerContexts(true);
+ await vi.waitFor(() => expect(pollReplies).toHaveLength(1));
+ pollReplies[0]({ ok: true, status: currentStatus });
+
+ expect(await response).toMatchObject({ ok: true, status: currentStatus });
+ expect(stored[RECORDING_STATE_KEY]).toMatchObject({
+ status: currentStatus,
+ });
+ },
+ );
+
+ it.each([
+ {
+ phase: "completed",
+ videoId: "video" as VideoId,
+ shareUrl: "https://cap.so/s/video",
+ },
+ { phase: "error", message: "Upload failed", recoverable: true },
+ ] satisfies RecordingStatus[])(
+ "preserves a newer $phase broadcast over an older poll response",
+ async (nextStatus) => {
+ const response = pollStatus();
+ answerContexts(true);
+ await vi.waitFor(() => expect(contextReplies).toHaveLength(1));
+ answerContexts(true);
+ await vi.waitFor(() => expect(pollReplies).toHaveLength(1));
+
+ broadcast(nextStatus);
+ pollReplies[0]({ ok: true, status: liveStatus });
+
+ expect(await response).toMatchObject({ ok: true, status: nextStatus });
+ expect(stored[RECORDING_STATE_KEY]).toMatchObject({ status: nextStatus });
+ expect(chrome.action.setBadgeText).toHaveBeenLastCalledWith(
+ { text: "" },
+ expect.any(Function),
+ );
+ },
+ );
+
+ it("preserves a recording broadcast received while checking for the offscreen document", async () => {
+ const response = pollStatus();
+ broadcast(liveStatus);
+ answerContexts(false);
+
+ expect(await response).toMatchObject({ ok: true, status: liveStatus });
+ expect(stored[RECORDING_STATE_KEY]).toMatchObject({ status: liveStatus });
+ });
+
+ it("clears a phantom recording when its offscreen document is gone", async () => {
+ broadcast(liveStatus);
+ const response = pollStatus();
+ answerContexts(false);
+
+ expect(await response).toMatchObject({
+ ok: true,
+ status: { phase: "idle" },
+ });
+ expect(stored[RECORDING_STATE_KEY]).toMatchObject({
+ status: { phase: "idle" },
+ });
+ });
+});
diff --git a/apps/chrome-extension/src/background/service-worker.ts b/apps/chrome-extension/src/background/service-worker.ts
index 166072e30..d3d3e65f4 100644
--- a/apps/chrome-extension/src/background/service-worker.ts
+++ b/apps/chrome-extension/src/background/service-worker.ts
@@ -1267,7 +1267,9 @@ const forwardToOffscreen = (type: OffscreenRequest["type"]) =>
sendOffscreen({ target: "offscreen", type } as OffscreenRequest);
const syncRecordingStatus = async () => {
+ const initialStatus = recordingStatus;
const hasDocument = await hasOffscreenDocument();
+ if (recordingStatus !== initialStatus) return recordingStatus;
if (!hasDocument) {
if (
isActiveRecordingStatus(recordingStatus) ||
@@ -1292,6 +1294,7 @@ const syncRecordingStatus = async () => {
{ target: "offscreen", type: "get-recording-status" },
{ createIfMissing: false },
);
+ if (recordingStatus !== initialStatus) return recordingStatus;
if (response.ok && response.status) {
if (
recordingStatus.phase === "creating" &&
diff --git a/apps/chrome-extension/src/offscreen/recorder.test.ts b/apps/chrome-extension/src/offscreen/recorder.test.ts
new file mode 100644
index 000000000..97b706470
--- /dev/null
+++ b/apps/chrome-extension/src/offscreen/recorder.test.ts
@@ -0,0 +1,198 @@
+import type { RecordingSpool } from "@cap/recorder-core";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import type { OffscreenResponse, RecordingMode } from "../shared/types";
+
+type MessageListener = Parameters<
+ typeof chrome.runtime.onMessage.addListener
+>[0];
+
+class Track extends EventTarget {
+ enabled = true;
+ readyState = "live";
+ label = "Test capture";
+
+ constructor(readonly kind: "audio" | "video") {
+ super();
+ }
+
+ getSettings() {
+ return { width: 320, height: 180, frameRate: 30 };
+ }
+}
+
+class Stream {
+ constructor(private tracks: Track[] = []) {}
+
+ getTracks() {
+ return this.tracks;
+ }
+
+ getAudioTracks() {
+ return this.tracks.filter((track) => track.kind === "audio");
+ }
+
+ getVideoTracks() {
+ return this.tracks.filter((track) => track.kind === "video");
+ }
+
+ addTrack(track: Track) {
+ this.tracks.push(track);
+ }
+}
+
+let onMessage: MessageListener;
+let capturedStream: Stream;
+
+beforeEach(async () => {
+ vi.resetModules();
+ vi.useFakeTimers();
+ const stored: Record<string, unknown> = {};
+ const storage = {
+ get: (_keys: string[], callback: (items: object) => void) =>
+ callback({ ...stored }),
+ set: (items: object, callback: () => void) => {
+ Object.assign(stored, items);
+ callback();
+ },
+ };
+ vi.stubGlobal("window", globalThis);
+ vi.stubGlobal("chrome", {
+ storage: { local: storage, session: storage },
+ runtime: {
+ onMessage: {
+ addListener: (listener: MessageListener) => {
+ onMessage = listener;
+ },
+ },
+ sendMessage: (_message: unknown, callback: () => void) => callback(),
+ },
+ });
+ vi.stubGlobal("MediaStream", Stream);
+ vi.stubGlobal(
+ "MediaRecorder",
+ class {
+ static isTypeSupported() {
+ return true;
+ }
+
+ constructor(stream: Stream) {
+ capturedStream = stream;
+ }
+
+ start() {}
+ },
+ );
+ vi.stubGlobal(
+ "AudioContext",
+ class {
+ destination = {};
+
+ createMediaStreamDestination() {
+ return { stream: new Stream([new Track("audio")]) };
+ }
+
+ createMediaStreamSource() {
+ return { connect: vi.fn() };
+ }
+ },
+ );
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async (input: string | URL | Request) => {
+ const url = input.toString();
+ if (url.endsWith("/instant-recordings")) {
+ return Response.json({
+ id: "video-test",
+ shareUrl: "https://cap.test/s/video-test",
+ });
+ }
+ if (url.endsWith("/initiate")) {
+ return Response.json({ uploadId: "upload-test", provider: "s3" });
+ }
+ throw new Error(`Unexpected request: ${url}`);
+ }),
+ );
+ const { RecordingSpool } = await import("@cap/recorder-core");
+ vi.spyOn(RecordingSpool, "create").mockResolvedValue({
+ sessionId: "spool-test",
+ } as RecordingSpool);
+});
+
+afterEach(() => {
+ vi.clearAllTimers();
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+});
+
+const send = (message: Record<string, unknown>) =>
+ new Promise<OffscreenResponse>((resolve) => {
+ onMessage({ target: "offscreen", ...message }, {}, resolve);
+ });
+
+describe("recording microphone mute", () => {
+ it.each([
+ { mode: "tab", microphone: true, systemAudio: true },
+ { mode: "fullscreen", microphone: true, systemAudio: true },
+ { mode: "camera", microphone: true, systemAudio: false },
+ { mode: "tab", microphone: false, systemAudio: true },
+ ] satisfies Array<{
+ mode: RecordingMode;
+ microphone: boolean;
+ systemAudio: boolean;
+ }>)(
+ "mutes only microphone capture in $mode with microphone=$microphone",
+ async ({ mode, microphone, systemAudio }) => {
+ const mic = new Track("audio");
+ const system = new Track("audio");
+ const main = new Stream([new Track("video")]);
+ if (systemAudio) main.addTrack(system);
+ if (mode === "camera" && microphone) main.addTrack(mic);
+ vi.stubGlobal("navigator", {
+ userAgent: "Chrome/152.0.0.0",
+ mediaDevices: {
+ getUserMedia: async (constraints: MediaStreamConstraints) =>
+ constraints.video ? main : new Stream([mic]),
+ getDisplayMedia: async () => main,
+ },
+ });
+ const { defaultSettings } = await import("../shared/storage");
+ await import("./recorder");
+ expect(
+ await send({
+ type: "start-recording",
+ mode,
+ tabStreamId: "tab-test",
+ settings: {
+ ...defaultSettings,
+ apiBaseUrl: "https://cap.test",
+ microphone: { enabled: microphone, deviceId: null },
+ systemAudio: { enabled: systemAudio },
+ sounds: { enabled: false },
+ countdown: { enabled: false, seconds: 3 },
+ },
+ auth: { authApiKey: "test", userId: "user-test" },
+ bootstrap: {
+ organization: { id: "org-test" },
+ plan: { isPro: true, maxRecordingSeconds: null },
+ },
+ }),
+ ).toMatchObject({ ok: true, status: { phase: "recording" } });
+
+ expect(
+ await send({ type: "toggle-microphone-mute", muted: true }),
+ ).toEqual({ ok: true });
+ expect(mic.enabled).toBe(!microphone);
+ expect(system.enabled).toBe(true);
+ if (systemAudio) {
+ expect(capturedStream.getAudioTracks()[0]?.enabled).toBe(true);
+ }
+
+ expect(
+ await send({ type: "toggle-microphone-mute", muted: false }),
+ ).toEqual({ ok: true });
+ expect(mic.enabled).toBe(true);
+ expect(system.enabled).toBe(true);
+ },
+ );
+});
diff --git a/apps/chrome-extension/src/offscreen/recorder.ts b/apps/chrome-extension/src/offscreen/recorder.ts
index 2696dd36d..792186506 100644
--- a/apps/chrome-extension/src/offscreen/recorder.ts
+++ b/apps/chrome-extension/src/offscreen/recorder.ts
@@ -95,6 +95,7 @@ type ActiveRecording = {
recorder: MediaRecorder;
stopPromise: Promise<void>;
streams: MediaStream[];
+ microphoneTracks: MediaStreamTrack[];
recordingStream: MediaStream;
statusTimer: number | null;
spool: RecordingSpool;
@@ -999,6 +1000,10 @@ const startRecording = async (request: StartRecordingRequest) => {
recorder,
stopPromise: Promise.resolve(),
streams,
+ microphoneTracks:
+ request.mode === "camera"
+ ? mainStream.getAudioTracks()
+ : (microphoneStream?.getAudioTracks() ?? []),
recordingStream,
statusTimer: null,
spool,
@@ -1174,9 +1179,12 @@ const startRecording = async (request: StartRecordingRequest) => {
);
await ownedSpool.dispose().catch(() => undefined);
}
- // Reset the "creating" status so later status syncs do not report a
- // phantom in-progress recording.
- if (status.phase === "creating") {
+ // Reset the status this attempt set so later status syncs do not report
+ // a phantom in-progress recording. "recording" is already set before
+ // recorder.start(), so a throw there would otherwise leave a capture
+ // phase with no session behind it: stop becomes a no-op and the idle
+ // self-close never fires.
+ if (status.phase === "creating" || status.phase === "recording") {
status = isUserCancellationError(error)
? { phase: "idle" }
: {
@@ -1773,12 +1781,7 @@ const handleRequest = async (
if (message.type === "toggle-microphone-mute") {
const recording = activeRecording;
if (recording) {
- for (const stream of recording.streams) {
- for (const track of stream.getAudioTracks()) {
- track.enabled = !message.muted;
- }
- }
- for (const track of recording.recordingStream.getAudioTracks()) {
+ for (const track of recording.microphoneTracks) {
track.enabled = !message.muted;
}
}
diff --git a/apps/cli/README.md b/apps/cli/README.md
index e33a97c0e..817a528b3 100644
--- a/apps/cli/README.md
+++ b/apps/cli/README.md
@@ -82,7 +82,7 @@ step.
- `cap screenshot` — capture a still of a screen/window (`--json` → `{path,width,height}`).
- `cap targets` (`screens`/`windows`/`cameras`/`mics`) — enumerate capture inputs.
- `cap project inspect` / `validate` / `config get|set` — inspect and edit `.cap` projects.
-- `cap recordings list` — list `.cap` recordings in the desktop library.
+- `cap recordings list` — list `.cap` recordings across Desktop's current, default, and previously used storage folders. Use `--dir <path>` to list only that directory.
- `cap upload` — upload a `.cap` project or video file and get a shareable link.
- `cap update` — download and install the latest Cap Desktop bundle, then repair the `cap` shim.
- `cap doctor` / `version` / `guide` — diagnostics, version info, and the agent capability manifest.
diff --git a/apps/cli/src/project.rs b/apps/cli/src/project.rs
index 5b0f8cdfb..8fbf00526 100644
--- a/apps/cli/src/project.rs
+++ b/apps/cli/src/project.rs
@@ -24,6 +24,8 @@ pub struct ProjectInspection {
}
pub fn config_get(project_path: PathBuf) -> Result<(), String> {
+ RecordingMeta::load_for_project(&project_path)
+ .map_err(|e| format!("Failed to load recording meta: {e}"))?;
let config = match cap_project::ProjectConfiguration::load(&project_path) {
Ok(config) => config,
// Instant and un-edited studio recordings have no project-config.json; return the
@@ -42,6 +44,8 @@ pub fn config_set(
) -> Result<(), String> {
let config: cap_project::ProjectConfiguration = serde_json::from_str(settings_json)
.map_err(|e| format!("Invalid project config JSON: {e}"))?;
+ RecordingMeta::load_for_project(&project_path)
+ .map_err(|e| format!("Failed to load recording meta: {e}"))?;
// write() validates internally before its atomic temp-file-then-rename.
config
.write(&project_path)
diff --git a/apps/cli/src/recordings.rs b/apps/cli/src/recordings.rs
index c633bb4a0..dc517bb81 100644
--- a/apps/cli/src/recordings.rs
+++ b/apps/cli/src/recordings.rs
@@ -1,4 +1,5 @@
use std::{
+ collections::HashSet,
path::{Path, PathBuf},
time::SystemTime,
};
@@ -12,10 +13,41 @@ use crate::{OutputFormat, write_json};
// which the user can reach with an explicit `--dir`.
const DESKTOP_BUNDLE_IDENTIFIER: &str = "so.cap.desktop";
-fn default_library_dir() -> Result<PathBuf, String> {
- dirs::data_dir()
- .ok_or_else(|| "Could not determine the OS application-data directory".to_string())
- .map(|dir| dir.join(DESKTOP_BUNDLE_IDENTIFIER).join("recordings"))
+fn library_dirs(app_data: &Path) -> Vec<PathBuf> {
+ let store = std::fs::read(app_data.join("store"))
+ .ok()
+ .and_then(|bytes| serde_json::from_slice::<serde_json::Value>(&bytes).ok());
+ let settings = store
+ .as_ref()
+ .and_then(|store| store.get("general_settings"));
+ let mut dirs = Vec::new();
+
+ if let Some(path) = settings
+ .and_then(|settings| settings.get("recordingsPath"))
+ .and_then(serde_json::Value::as_str)
+ {
+ dirs.push(PathBuf::from(path));
+ }
+ dirs.push(app_data.join("recordings"));
+ if let Some(paths) = settings
+ .and_then(|settings| settings.get("previousRecordingsPaths"))
+ .and_then(serde_json::Value::as_array)
+ {
+ dirs.extend(
+ paths
+ .iter()
+ .filter_map(serde_json::Value::as_str)
+ .map(PathBuf::from),
+ );
+ }
+
+ let mut seen = HashSet::new();
+ dirs.retain(|path| {
+ path.is_absolute()
+ && path.is_dir()
+ && seen.insert(path.canonicalize().unwrap_or_else(|_| path.clone()))
+ });
+ dirs
}
#[derive(Serialize)]
@@ -37,28 +69,38 @@ fn recording_type(meta: &RecordingMeta) -> &'static str {
}
}
-fn collect_rows(dir: &Path) -> Result<Vec<RecordingRow>, String> {
- if !dir.exists() {
- return Ok(Vec::new());
- }
-
- let mut entries: Vec<(PathBuf, SystemTime, RecordingMeta)> = std::fs::read_dir(dir)
- .map_err(|e| format!("Failed to read recordings directory: {e}"))?
- .filter_map(|entry| {
+fn collect_rows(
+ dirs: &[PathBuf],
+ fail_on_unreadable: bool,
+ created_at: impl Fn(&Path) -> std::io::Result<SystemTime>,
+) -> Result<Vec<RecordingRow>, String> {
+ let mut entries: Vec<(PathBuf, SystemTime, RecordingMeta)> = Vec::new();
+ for dir in dirs {
+ if !dir.exists() {
+ continue;
+ }
+ let contents = match std::fs::read_dir(dir) {
+ Ok(contents) => contents,
+ Err(_) if !fail_on_unreadable => continue,
+ Err(error) => {
+ return Err(format!(
+ "Failed to read recordings directory {}: {error}",
+ dir.display()
+ ));
+ }
+ };
+ entries.extend(contents.filter_map(|entry| {
let path = entry.ok()?.path();
if !path.is_dir() || !path.file_name()?.to_str()?.ends_with(".cap") {
return None;
}
let meta = RecordingMeta::load_for_project(&path).ok()?;
- let created = path
- .metadata()
- .and_then(|m| m.created())
- .unwrap_or(SystemTime::UNIX_EPOCH);
+ let created = created_at(&path).unwrap_or(SystemTime::UNIX_EPOCH);
Some((path, created, meta))
- })
- .collect();
+ }));
+ }
- entries.sort_by(|a, b| b.1.cmp(&a.1));
+ entries.sort_by_key(|entry| std::cmp::Reverse(entry.1));
Ok(entries
.into_iter()
@@ -77,18 +119,26 @@ fn collect_rows(dir: &Path) -> Result<Vec<RecordingRow>, String> {
}
pub fn list(dir: Option<PathBuf>, format: OutputFormat) -> Result<(), String> {
- let dir = match dir {
- Some(dir) => dir,
- None => default_library_dir()?,
+ let dirs = match &dir {
+ Some(dir) => vec![dir.clone()],
+ None => {
+ let app_data = dirs::data_dir()
+ .ok_or_else(|| "Could not determine the OS application-data directory".to_string())?
+ .join(DESKTOP_BUNDLE_IDENTIFIER);
+ library_dirs(&app_data)
+ }
};
- let rows = collect_rows(&dir)?;
+ let rows = collect_rows(&dirs, dir.is_some(), |path| path.metadata()?.created())?;
match format {
OutputFormat::Json => write_json(&rows),
OutputFormat::Text => {
if rows.is_empty() {
- println!("No recordings found in {}", dir.display());
+ match dir {
+ Some(dir) => println!("No recordings found in {}", dir.display()),
+ None => println!("No recordings found in the desktop library"),
+ }
return Ok(());
}
for row in &rows {
@@ -106,3 +156,129 @@ pub fn list(dir: Option<PathBuf>, format: OutputFormat) -> Result<(), String> {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn write_recording(path: &Path, name: &str) {
+ std::fs::create_dir_all(path).unwrap();
+ std::fs::write(
+ path.join("recording-meta.json"),
+ serde_json::to_vec(&serde_json::json!({
+ "pretty_name": name, "fps": 30, "sample_rate": null
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+ }
+
+ #[test]
+ fn discovers_current_default_and_previous_storage_once() {
+ let root = tempfile::tempdir().unwrap();
+ let app_data = root.path().join("desktop");
+ let default = app_data.join("recordings");
+ let current = root.path().join("current");
+ let previous = root.path().join("previous");
+ for dir in [&default, &current, &previous] {
+ std::fs::create_dir_all(dir).unwrap();
+ }
+ std::fs::write(
+ app_data.join("store"),
+ serde_json::to_vec(&serde_json::json!({
+ "general_settings": {
+ "recordingsPath": current,
+ "previousRecordingsPaths": [previous, current, "relative", root.path().join("missing"), 42]
+ }
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+
+ assert_eq!(library_dirs(&app_data), vec![current, default, previous]);
+ }
+
+ #[test]
+ fn falls_back_to_default_storage_without_valid_settings() {
+ let root = tempfile::tempdir().unwrap();
+ let default = root.path().join("recordings");
+ std::fs::create_dir(&default).unwrap();
+ assert_eq!(library_dirs(root.path()), vec![default.clone()]);
+
+ for store in [
+ "invalid JSON",
+ r#"{"general_settings":{"recordingsPath":false,"previousRecordingsPaths":"invalid"}}"#,
+ ] {
+ std::fs::write(root.path().join("store"), store).unwrap();
+ assert_eq!(library_dirs(root.path()), vec![default.clone()]);
+ }
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn deduplicates_storage_reached_through_symlinks() {
+ let root = tempfile::tempdir().unwrap();
+ let default = root.path().join("recordings");
+ let alias = root.path().join("alias");
+ std::fs::create_dir(&default).unwrap();
+ std::os::unix::fs::symlink(&default, &alias).unwrap();
+ std::fs::write(
+ root.path().join("store"),
+ serde_json::to_vec(&serde_json::json!({
+ "general_settings": { "recordingsPath": alias }
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+
+ assert_eq!(library_dirs(root.path()), vec![alias]);
+ }
+
+ #[test]
+ fn merges_recordings_from_multiple_libraries_newest_first() {
+ let root = tempfile::tempdir().unwrap();
+ let recordings = [
+ (root.path().join("default/oldest.cap"), "Oldest", 10),
+ (root.path().join("custom/older.cap"), "Older", 20),
+ (root.path().join("default/newer.cap"), "Newer", 30),
+ (root.path().join("custom/newest.cap"), "Newest", 40),
+ ];
+ for (path, name, _) in &recordings {
+ write_recording(path, name);
+ }
+
+ let rows = collect_rows(
+ &[root.path().join("default"), root.path().join("custom")],
+ true,
+ |path| {
+ let (_, _, seconds) = recordings
+ .iter()
+ .find(|(recording_path, _, _)| recording_path == path)
+ .unwrap();
+ Ok(SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(*seconds))
+ },
+ )
+ .unwrap();
+ assert_eq!(
+ rows.iter().map(|row| row.name.as_str()).collect::<Vec<_>>(),
+ ["Newest", "Newer", "Older", "Oldest"]
+ );
+ }
+
+ #[test]
+ fn skips_unavailable_roots_only_when_discovering_the_library() {
+ let root = tempfile::tempdir().unwrap();
+ let available = root.path().join("available");
+ let unavailable = root.path().join("unavailable");
+ write_recording(&available.join("example.cap"), "Available");
+ std::fs::write(&unavailable, "no longer a directory").unwrap();
+
+ let rows = collect_rows(&[unavailable.clone(), available], false, |path| {
+ path.metadata()?.created()
+ })
+ .unwrap();
+ assert_eq!(rows.len(), 1);
+ assert_eq!(rows[0].name, "Available");
+ assert!(collect_rows(&[unavailable], true, |path| path.metadata()?.created()).is_err());
+ }
+}
diff --git a/apps/cli/src/selftest/playback.rs b/apps/cli/src/selftest/playback.rs
index 57e997d94..2e6929ea2 100644
--- a/apps/cli/src/selftest/playback.rs
+++ b/apps/cli/src/selftest/playback.rs
@@ -571,7 +571,7 @@ mod fixture {
use cap_recording::{
AudioFrame, ChannelAudioSource, ChannelAudioSourceConfig, ChannelVideoSource,
ChannelVideoSourceConfig, OutputPipeline,
- ffmpeg::{FFmpegVideoFrame, Mp4Muxer, OggMuxer},
+ ffmpeg::{FFmpegVideoFrame, Mp4Muxer, OggMuxer, OggMuxerConfig},
};
use cap_timestamp::{Timestamp, Timestamps};
use relative_path::RelativePathBuf;
@@ -783,7 +783,7 @@ mod fixture {
audio_info, mic_rx,
))
.with_timestamps(timestamps)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await
.map_err(|e| format!("mic pipeline: {e}"))?;
let sys_pipeline = OutputPipeline::builder(audio_path.clone())
@@ -794,7 +794,7 @@ mod fixture {
// System audio anchors at the recording epoch, exactly like the
// studio recorder configures it.
.with_audio_anchor(cap_recording::AudioAnchor::PipelineEpoch)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await
.map_err(|e| format!("system audio pipeline: {e}"))?;
@@ -860,12 +860,14 @@ mod fixture {
start_time: Some(mic_start),
device_id: None,
gap_summary: finished_mic.audio_gap_summary.map(to_project_gap_summary),
+ digital_silence_ms: finished_mic.digital_silence_ms,
}),
system_audio: Some(AudioMeta {
path: RelativePathBuf::from("content/segments/segment-0/system_audio.ogg"),
start_time: Some(sys_start),
device_id: None,
gap_summary: finished_sys.audio_gap_summary.map(to_project_gap_summary),
+ digital_silence_ms: None,
}),
cursor: None,
keyboard: None,
diff --git a/apps/cli/src/upload.rs b/apps/cli/src/upload.rs
index 0844b4832..e2a95d8b8 100644
--- a/apps/cli/src/upload.rs
+++ b/apps/cli/src/upload.rs
@@ -258,6 +258,27 @@ pub async fn upload_video_path(file_path: &Path, name: Option<String>) -> Result
}
}
+fn agent_upload_payload(
+ file_name: &str,
+ name: Option<&str>,
+ content_length: u64,
+ meta: &VideoMeta,
+) -> serde_json::Value {
+ let mut payload = json!({
+ "fileName": file_name,
+ "contentType": "video/mp4",
+ "contentLength": content_length,
+ "durationSeconds": meta.duration_in_secs,
+ "width": meta.width,
+ "height": meta.height,
+ "fps": meta.fps,
+ });
+ if let Some(name) = name {
+ payload["title"] = json!(name);
+ }
+ payload
+}
+
async fn upload_file_with_agent(
file_path: &Path,
name: Option<&str>,
@@ -276,16 +297,7 @@ async fn upload_file_with_agent(
.mutate_json(
Method::POST,
"/uploads",
- &json!({
- "fileName": file_name,
- "contentType": "video/mp4",
- "contentLength": content_length,
- "durationSeconds": meta.duration_in_secs,
- "width": meta.width,
- "height": meta.height,
- "fps": meta.fps,
- "title": name,
- }),
+ &agent_upload_payload(file_name, name, content_length, meta),
)
.await
.map_err(|error| error.to_string())?;
@@ -529,9 +541,26 @@ async fn upload_file(http: &Client, put_url: &str, path: &Path) -> Result<(), St
#[cfg(test)]
mod tests {
- use super::prefer_agent_upload_sources;
+ use super::{VideoMeta, agent_upload_payload, prefer_agent_upload_sources};
use crate::credentials::{AgentCredentialSource, CredentialSource};
+ #[test]
+ fn agent_upload_payload_only_includes_a_requested_title() {
+ let meta = VideoMeta {
+ duration_in_secs: 1.0,
+ width: 640,
+ height: 360,
+ fps: 30.0,
+ };
+ for name in [None, Some("Demo"), Some("")] {
+ let payload = agent_upload_payload("demo.mp4", name, 1024, &meta);
+ match name {
+ Some(name) => assert_eq!(payload["title"], name),
+ None => assert!(payload.get("title").is_none()),
+ }
+ }
+ }
+
#[test]
fn explicit_agent_environment_credential_has_highest_upload_priority() {
assert!(prefer_agent_upload_sources(
diff --git a/apps/cli/tests/cli.rs b/apps/cli/tests/cli.rs
index 712adbf74..d3372bb3e 100644
--- a/apps/cli/tests/cli.rs
+++ b/apps/cli/tests/cli.rs
@@ -609,6 +609,51 @@ fn recordings_list_empty_dir_is_empty_json() {
assert_eq!(json.as_array().map(|a| a.len()), Some(0));
}
+#[cfg(target_os = "linux")]
+#[test]
+fn recordings_list_follows_desktop_storage_unless_dir_is_explicit() {
+ let root = tempfile::tempdir().unwrap();
+ let app_data = root.path().join("so.cap.desktop");
+ let default = app_data.join("recordings");
+ let current = root.path().join("custom");
+ let previous = root.path().join("previous");
+ for dir in [&default, &current, &previous] {
+ write_single_segment_meta(&dir.join("example.cap"));
+ }
+ std::fs::write(
+ app_data.join("store"),
+ serde_json::to_vec(&serde_json::json!({
+ "general_settings": {
+ "recordingsPath": current,
+ "previousRecordingsPaths": [previous, current]
+ }
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+
+ let list = |dir: Option<&Path>| {
+ let mut command = cap();
+ command
+ .env("XDG_DATA_HOME", root.path())
+ .args(["recordings", "list", "--json"]);
+ if let Some(dir) = dir {
+ command.arg("--dir").arg(dir);
+ }
+ let output = command.output().unwrap();
+ assert!(output.status.success(), "stderr: {}", stderr(&output));
+ parse_json(&output)
+ };
+
+ assert_eq!(list(None).as_array().unwrap().len(), 3);
+ let explicit = list(Some(&current));
+ assert_eq!(explicit.as_array().unwrap().len(), 1);
+ assert_eq!(
+ explicit[0]["path"],
+ current.join("example.cap").to_str().unwrap()
+ );
+}
+
#[test]
fn project_config_set_then_get_roundtrips() {
let dir = tempfile::tempdir().unwrap();
@@ -660,6 +705,27 @@ fn project_config_get_without_file_returns_default() {
assert!(json.is_object(), "expected a default project config object");
}
+#[test]
+fn project_config_commands_reject_directories_without_recordings() {
+ let dir = tempfile::tempdir().unwrap();
+ let project = dir.path().join("not-a-recording.cap");
+ std::fs::create_dir(&project).unwrap();
+
+ for command in ["get", "set"] {
+ let mut args = vec!["project", "config", command, project.to_str().unwrap()];
+ if command == "set" {
+ args.extend(["--settings-json", "{}"]);
+ }
+ args.push("--json");
+
+ let output = run(&args);
+ assert!(!output.status.success(), "{command} unexpectedly succeeded");
+ assert!(parse_json(&output)["error"].is_string());
+ }
+
+ assert!(!project.join("project-config.json").exists());
+}
+
#[test]
fn export_missing_project_emits_json_error_event() {
let output = run(&["export", "/this/path/does/not/exist.cap", "--progress-json"]);
diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock
index 869eb69b1..435cda07f 100644
--- a/apps/desktop-gpui/Cargo.lock
+++ b/apps/desktop-gpui/Cargo.lock
@@ -2548,6 +2548,15 @@ dependencies = [
"unicode-segmentation",
]
+[[package]]
+name = "convert_case"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "affbf0190ed2caf063e3def54ff444b449371d55c58e513a95ab98eca50adb49"
+dependencies = [
+ "unicode-segmentation",
+]
+
[[package]]
name = "cookie"
version = "0.18.2"
@@ -4312,7 +4321,7 @@ dependencies = [
"num-traits",
"ordered-channel",
"pbr",
- "quick-error",
+ "quick-error 2.0.1",
"resize",
"rgb",
"wild",
@@ -4569,6 +4578,7 @@ dependencies = [
"anyhow",
"async-channel",
"async-task",
+ "backtrace",
"bindgen 0.71.1",
"bitflags 2.13.1",
"block",
@@ -4612,6 +4622,7 @@ dependencies = [
"pollster 0.4.0",
"postage",
"profiling",
+ "proptest",
"rand 0.9.5",
"raw-window-handle",
"refineable",
@@ -5465,7 +5476,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "525e9ff3e1a4be2fbea1fdf0e98686a6d98b4d8f937e1bf7402245af1909e8c3"
dependencies = [
"byteorder-lite",
- "quick-error",
+ "quick-error 2.0.1",
]
[[package]]
@@ -8370,6 +8381,36 @@ dependencies = [
"syn 2.0.119",
]
+[[package]]
+name = "proptest"
+version = "1.10.0"
+source = "git+https://github.com/proptest-rs/proptest?rev=3dca198a8fef1b32e3a66f1e1897c955b4dc5b5b#3dca198a8fef1b32e3a66f1e1897c955b4dc5b5b"
+dependencies = [
+ "bit-set 0.8.0",
+ "bit-vec 0.8.0",
+ "bitflags 2.13.1",
+ "num-traits",
+ "proptest-macro",
+ "rand 0.9.5",
+ "rand_chacha 0.9.0",
+ "rand_xorshift",
+ "regex-syntax",
+ "rusty-fork",
+ "tempfile",
+ "unarray",
+]
+
+[[package]]
+name = "proptest-macro"
+version = "0.5.0"
+source = "git+https://github.com/proptest-rs/proptest?rev=3dca198a8fef1b32e3a66f1e1897c955b4dc5b5b#3dca198a8fef1b32e3a66f1e1897c955b4dc5b5b"
+dependencies = [
+ "convert_case 0.11.0",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.119",
+]
+
[[package]]
name = "psl-types"
version = "2.0.11"
@@ -8434,6 +8475,12 @@ dependencies = [
"bytemuck",
]
+[[package]]
+name = "quick-error"
+version = "1.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
+
[[package]]
name = "quick-error"
version = "2.0.1"
@@ -8662,6 +8709,15 @@ dependencies = [
"rand_core 0.10.1",
]
+[[package]]
+name = "rand_xorshift"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "513962919efc330f829edb2535844d1b912b0fbe2ca165d613e4e8788bb05a5a"
+dependencies = [
+ "rand_core 0.9.5",
+]
+
[[package]]
name = "range-alloc"
version = "0.1.5"
@@ -8718,7 +8774,7 @@ dependencies = [
"avif-serialize",
"imgref",
"loop9",
- "quick-error",
+ "quick-error 2.0.1",
"rav1e",
"rayon",
"rgb",
@@ -9312,6 +9368,18 @@ version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f"
+[[package]]
+name = "rusty-fork"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cc6bf79ff24e648f6da1f8d1f011e9cac26491b619e6b9280f2b47f1774e6ee2"
+dependencies = [
+ "fnv",
+ "quick-error 1.2.3",
+ "tempfile",
+ "wait-timeout",
+]
+
[[package]]
name = "rustybuzz"
version = "0.14.1"
@@ -10949,7 +11017,7 @@ dependencies = [
"fax",
"flate2",
"half",
- "quick-error",
+ "quick-error 2.0.1",
"weezl",
"zune-jpeg 0.5.15",
]
@@ -11518,6 +11586,12 @@ dependencies = [
"libc",
]
+[[package]]
+name = "unarray"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94"
+
[[package]]
name = "unic-char-property"
version = "0.9.0"
@@ -11954,6 +12028,15 @@ dependencies = [
"libc",
]
+[[package]]
+name = "wait-timeout"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11"
+dependencies = [
+ "libc",
+]
+
[[package]]
name = "waker-fn"
version = "1.2.0"
diff --git a/apps/desktop-gpui/Cargo.toml b/apps/desktop-gpui/Cargo.toml
index a713117fb..e51320a0a 100644
--- a/apps/desktop-gpui/Cargo.toml
+++ b/apps/desktop-gpui/Cargo.toml
@@ -179,6 +179,7 @@ cc = "1.4"
tauri-winres = "0.3.5"
[dev-dependencies]
+gpui = { git = "https://github.com/wingleeio/zed", rev = "5d1f83d9f27a19bec1fb241dc33b42238af9cf8d", features = ["test-support"] }
# The editor smoke (`tests/editor_frame0.rs`) drives `EditorInstance`, whose
# decoders, renderer and preview tasks are all tokio-spawned -- a current-thread
# runtime would deadlock the moment the test thread blocks on a frame. Kept in
diff --git a/apps/desktop-gpui/README.md b/apps/desktop-gpui/README.md
index f29044473..f27c62630 100644
--- a/apps/desktop-gpui/README.md
+++ b/apps/desktop-gpui/README.md
@@ -1845,7 +1845,7 @@ its baseline values, every drag and delete having been undone.
| **Clips cannot be reordered, and their body drag does nothing** | The clip body's 4px drag is a **crossfade-duration** drag, not a move (`TL/ClipTrack.tsx:849-945`) — it grows the incoming transition and selects it. Transitions have no drawn affordance in this rev (E3 renders their effect on the boxes but not the handle or the marker), so the press only ever selects. Reordering exists in the shipping app but **not on the timeline**: it is the Clips layout mode's list (`ClipsSidebar.tsx:650`, behind the header's Clips button), which is its own unit. |
| **The 3D track's split is not reproduced** | `splitCamera3DSegment` rebuilds both halves' nine pose tracks around the pose the segment held at the cut (`ED/context.ts:640-676`), which needs the keyframe evaluator. Splitting a 3D segment is refused rather than done wrongly; every other track's split is real. |
| **Only the zoom track creates by click** | The mask, text and audio tracks also add a segment when their empty lane is clicked (`TL/MaskTrack.tsx:120-184` and siblings), each with its own gap-finding placement and default segment. Those are creation flows for objects the sidebar has to configure; the zoom track's is here because its ghost was already drawn. The sidebar now configures all four, so the remaining three are a timeline-side gap rather than a dependency — each needs its own gap-finding placement and default segment, and the mask track's also has to seed a rect on the canvas. |
-| **A no-op drag records no undo entry** | The source pushes a history entry on *every* `resume`, because the tracked memo re-runs when `pauseCount` changes — so a click that selected and never moved leaves a duplicate snapshot behind, and the first undo after one appears to do nothing. Here the resume only records when something actually changed during the bracket. |
+| **A no-op drag records no undo entry** | The source pushes a history entry on *every* `resume`, because the tracked memo re-runs when `pauseCount` changes — so a click that selected and never moved leaves a duplicate snapshot behind, and the first undo after one appears to do nothing. Here the resume only records when the project ends up different from where the bracket started, so a drag that comes back to its starting value records nothing either. |
| **No context menus** | Right-clicking a segment opens a real `Menu.popup()` with "Select all" and "Delete" (`TL/ZoomTrack.tsx:544-595`), and the zoom track's own dev-only "Generate zoom segments from clicks". None is reproduced. |
| **No double-click fill** | Double-clicking a zoom or scene handle expands the segment as far as it can go in that direction (`fillStart`/`fillEnd`, `TL/ZoomTrack.tsx:353-399`). gpui delivers `click_count`, but the behaviour is not wired. |
| **`normalizeClipTransitions` is a no-op** | `onHandleReleased` re-clamps every transition against its clips after a trim; here `effective_transition` already clamps on every read, so the stored value is left alone. The viewport half of `onHandleReleased` — pulling the transform back when a trim shortens the project out from under it — *is* reproduced. |
diff --git a/apps/desktop-gpui/assets/icons/clipboard-paste.svg b/apps/desktop-gpui/assets/icons/clipboard-paste.svg
new file mode 100644
index 000000000..312524c27
--- /dev/null
+++ b/apps/desktop-gpui/assets/icons/clipboard-paste.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-clipboard-paste"><path d="M11 14h10"/><path d="M16 4h2a2 2 0 0 1 2 2v1.344"/><path d="m17 18 4-4-4-4"/><path d="M8 4H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h8"/><rect x="8" y="2" width="8" height="4" rx="1"/></svg>
diff --git a/apps/desktop-gpui/kernels/Cargo.toml b/apps/desktop-gpui/kernels/Cargo.toml
index f57b830d1..fab553c50 100644
--- a/apps/desktop-gpui/kernels/Cargo.toml
+++ b/apps/desktop-gpui/kernels/Cargo.toml
@@ -27,6 +27,10 @@ harness = false
name = "thumbnail_fit"
harness = false
+[[bench]]
+name = "thumbnail_capture"
+harness = false
+
[[bench]]
name = "png_encode"
harness = false
@@ -42,3 +46,11 @@ harness = false
[[bench]]
name = "frame_unpad"
harness = false
+
+[[bench]]
+name = "screenshot_mask"
+harness = false
+
+[[bench]]
+name = "screenshot_blur"
+harness = false
diff --git a/apps/desktop-gpui/kernels/benches/screenshot_blur.rs b/apps/desktop-gpui/kernels/benches/screenshot_blur.rs
new file mode 100644
index 000000000..516ec9c68
--- /dev/null
+++ b/apps/desktop-gpui/kernels/benches/screenshot_blur.rs
@@ -0,0 +1,108 @@
+use cap_gpui_kernels::screenshot as candidate;
+use std::hint::black_box;
+use std::time::Instant;
+
+fn median(samples: &mut [u128]) -> u128 {
+ samples.sort_unstable();
+ samples[samples.len() / 2]
+}
+
+fn blur_reference(
+ rgba: &[u8],
+ frame_width: u32,
+ (x0, y0, width, height): (u32, u32, u32, u32),
+ level: f64,
+) -> Option<image::RgbaImage> {
+ let stride = frame_width as usize * 4;
+ let mut copied = Vec::with_capacity(width as usize * height as usize * 4);
+ for row in 0..height {
+ let start = (y0 + row) as usize * stride + x0 as usize * 4;
+ copied.extend_from_slice(&rgba[start..start + width as usize * 4]);
+ }
+ let source = image::RgbaImage::from_raw(width, height, copied)?;
+ let factor = ((level / 4.).round() as u32).max(2);
+ let small = image::imageops::resize(
+ &source,
+ (width / factor).max(1),
+ (height / factor).max(1),
+ image::imageops::FilterType::Triangle,
+ );
+ Some(image::imageops::resize(
+ &small,
+ width,
+ height,
+ image::imageops::FilterType::Triangle,
+ ))
+}
+
+fn time_call(rgba: &[u8], width: u32, height: u32, level: f64, reference: bool) -> u128 {
+ let start = Instant::now();
+ let image = if reference {
+ blur_reference(
+ black_box(rgba),
+ black_box(width),
+ black_box((0, 0, width, height)),
+ black_box(level),
+ )
+ } else {
+ candidate::mask_filter(
+ black_box(rgba),
+ black_box(width),
+ black_box(height),
+ black_box((0, 0, width, height)),
+ black_box(false),
+ black_box(level),
+ )
+ };
+ let elapsed = start.elapsed().as_nanos();
+ black_box(image.unwrap());
+ elapsed
+}
+
+fn main() {
+ // Warmup is discarded. Recorded samples alternate which implementation
+ // runs first. n is odd, and the reported time is the median.
+ println!("workload,level,n,before_ns,after_ns,speedup");
+ for (width, height, level, runs) in [
+ (400u32, 300u32, 16.0f64, 21usize),
+ (400, 300, 8.0, 21),
+ (400, 300, 40.0, 21),
+ (1920, 1080, 16.0, 21),
+ (1920, 1080, 8.0, 21),
+ (1920, 1080, 40.0, 21),
+ (3840, 2160, 16.0, 11),
+ (3840, 2160, 8.0, 11),
+ (3840, 2160, 40.0, 11),
+ ] {
+ let rgba: Vec<u8> = (0..width as usize * height as usize * 4)
+ .map(|i| (i.wrapping_mul(31) % 251) as u8)
+ .collect();
+ let before = blur_reference(&rgba, width, (0, 0, width, height), level)
+ .unwrap()
+ .into_raw();
+ let after =
+ candidate::mask_filter(&rgba, width, height, (0, 0, width, height), false, level)
+ .unwrap()
+ .into_raw();
+ assert_eq!(before, after, "{width}x{height} level {level}");
+ for _ in 0..3 {
+ black_box(time_call(&rgba, width, height, level, true));
+ black_box(time_call(&rgba, width, height, level, false));
+ }
+ let mut before_ns = Vec::with_capacity(runs);
+ let mut after_ns = Vec::with_capacity(runs);
+ for sample in 0..runs {
+ if sample % 2 == 0 {
+ before_ns.push(time_call(&rgba, width, height, level, true));
+ after_ns.push(time_call(&rgba, width, height, level, false));
+ } else {
+ after_ns.push(time_call(&rgba, width, height, level, false));
+ before_ns.push(time_call(&rgba, width, height, level, true));
+ }
+ }
+ let before_median = median(&mut before_ns);
+ let after_median = median(&mut after_ns);
+ let speedup = before_median as f64 / after_median as f64;
+ println!("{width}x{height},{level},{runs},{before_median},{after_median},{speedup:.3}");
+ }
+}
diff --git a/apps/desktop-gpui/kernels/benches/screenshot_mask.rs b/apps/desktop-gpui/kernels/benches/screenshot_mask.rs
new file mode 100644
index 000000000..59579244a
--- /dev/null
+++ b/apps/desktop-gpui/kernels/benches/screenshot_mask.rs
@@ -0,0 +1,95 @@
+use cap_gpui_kernels::screenshot as candidate;
+use std::{hint::black_box, time::Instant};
+
+type MaskFilter = fn(&[u8], u32, u32, (u32, u32, u32, u32), bool, f64) -> Option<image::RgbaImage>;
+
+fn main() {
+ println!("workload,sample,before_ns,after_ns");
+ for (name, frame, region, level) in [
+ (
+ "1080p-400x300-default",
+ (1920u32, 1080u32),
+ (200, 150, 400, 300),
+ 16.,
+ ),
+ ("1080p-400x300-fine", (1920, 1080), (200, 150, 400, 300), 4.),
+ (
+ "4k-1200x800-default",
+ (3840, 2160),
+ (600, 400, 1200, 800),
+ 16.,
+ ),
+ (
+ "4k-1200x800-coarse",
+ (3840, 2160),
+ (600, 400, 1200, 800),
+ 50.,
+ ),
+ ("4k-full-default", (3840, 2160), (0, 0, 3840, 2160), 16.),
+ ] {
+ let rgba: Vec<u8> = (0..frame.0 as usize * frame.1 as usize * 4)
+ .map(|i| (i.wrapping_mul(31) % 251) as u8)
+ .collect();
+ let run = |f: MaskFilter| {
+ let start = Instant::now();
+ let image = f(
+ black_box(&rgba),
+ black_box(frame.0),
+ black_box(frame.1),
+ black_box(region),
+ black_box(true),
+ black_box(level),
+ );
+ (start.elapsed().as_nanos(), black_box(image))
+ };
+ assert_eq!(
+ run(baseline::mask_filter).1.unwrap().into_raw(),
+ run(candidate::mask_filter).1.unwrap().into_raw(),
+ "{name}"
+ );
+ for sample in 0..25 {
+ let (old, new) = if sample % 2 == 0 {
+ let old = run(baseline::mask_filter).0;
+ (old, run(candidate::mask_filter).0)
+ } else {
+ let new = run(candidate::mask_filter).0;
+ (run(baseline::mask_filter).0, new)
+ };
+ if sample >= 5 {
+ println!("{name},{},{old},{new}", sample - 5);
+ }
+ }
+ }
+}
+
+mod baseline {
+ pub fn mask_filter(
+ rgba: &[u8],
+ frame_width: u32,
+ _frame_height: u32,
+ (x0, y0, width, height): (u32, u32, u32, u32),
+ _pixelate: bool,
+ level: f64,
+ ) -> Option<image::RgbaImage> {
+ let stride = frame_width as usize * 4;
+ let mut copied = Vec::with_capacity(width as usize * height as usize * 4);
+ for row in 0..height {
+ let start = (y0 + row) as usize * stride + x0 as usize * 4;
+ copied.extend_from_slice(&rgba[start..start + width as usize * 4]);
+ }
+ let source = image::RgbaImage::from_raw(width, height, copied)?;
+ let block = (level.round() as u32).max(2);
+ let small = image::imageops::resize(
+ &source,
+ (width / block).max(1),
+ (height / block).max(1),
+ image::imageops::FilterType::Nearest,
+ );
+ Some(image::imageops::resize(
+ &small,
+ width,
+ height,
+ image::imageops::FilterType::Nearest,
+ ))
+ }
+}
diff --git a/apps/desktop-gpui/kernels/benches/thumbnail_capture.rs b/apps/desktop-gpui/kernels/benches/thumbnail_capture.rs
new file mode 100644
index 000000000..a0b340a9d
--- /dev/null
+++ b/apps/desktop-gpui/kernels/benches/thumbnail_capture.rs
@@ -0,0 +1,64 @@
+use cap_gpui_kernels::thumbnail::{fit_centered, fit_centered_capture};
+use image::{DynamicImage, RgbImage, RgbaImage};
+use std::{hint::black_box, time::Instant};
+
+fn timed(image: &DynamicImage, fit: fn(DynamicImage) -> RgbaImage) -> u128 {
+ let input = image.clone();
+ let start = Instant::now();
+ black_box(fit(black_box(input)));
+ start.elapsed().as_nanos()
+}
+
+fn before(image: DynamicImage) -> RgbaImage {
+ fit_centered(&image.into_rgba8(), 320, 180)
+}
+
+fn after(image: DynamicImage) -> RgbaImage {
+ fit_centered_capture(image, 320, 180)
+}
+
+fn main() {
+ println!("workload,width,height,sample,before_ns,after_ns");
+ for (name, width, height, alpha) in [
+ ("display-1080p", 1920, 1080, false),
+ ("display-1440p", 2560, 1440, false),
+ ("display-ultrawide", 3440, 1440, false),
+ ("display-4k", 3840, 2160, false),
+ ("display-5k", 5120, 2880, false),
+ ("window-4k-rounded", 3840, 2160, true),
+ ] {
+ let image = if alpha {
+ DynamicImage::ImageRgba8(RgbaImage::from_fn(width, height, |x, y| {
+ image::Rgba([
+ (x * 17 + y) as u8,
+ (x + y * 11) as u8,
+ (x * 3 + y * 5) as u8,
+ if x < 12 && y < 12 { 0 } else { 255 },
+ ])
+ }))
+ } else {
+ DynamicImage::ImageRgb8(RgbImage::from_fn(width, height, |x, y| {
+ image::Rgb([
+ (x * 17 + y) as u8,
+ (x + y * 11) as u8,
+ (x * 3 + y * 5) as u8,
+ ])
+ }))
+ };
+ for sample in 0..25 {
+ let (before_ns, after_ns) = if sample % 2 == 0 {
+ let before_ns = timed(&image, before);
+ (before_ns, timed(&image, after))
+ } else {
+ let after_ns = timed(&image, after);
+ (timed(&image, before), after_ns)
+ };
+ if sample >= 5 {
+ println!(
+ "{name},{width},{height},{},{before_ns},{after_ns}",
+ sample - 5
+ );
+ }
+ }
+ }
+}
diff --git a/apps/desktop-gpui/kernels/src/screenshot.rs b/apps/desktop-gpui/kernels/src/screenshot.rs
index 0bc588e8e..ef353a662 100644
--- a/apps/desktop-gpui/kernels/src/screenshot.rs
+++ b/apps/desktop-gpui/kernels/src/screenshot.rs
@@ -300,15 +300,349 @@ pub fn mask_filter(
}
let (x0, y0, width, height) = region;
- let mut copied = Vec::with_capacity(width as usize * height as usize * 4);
+ if pixelate {
+ let block = (level.round() as u32).max(2);
+ return pixelate_region(rgba, stride, region, block);
+ }
+
+ blur_triangle(rgba, stride, x0, y0, width, height, level)
+}
+
+fn blur_triangle(
+ rgba: &[u8],
+ stride: usize,
+ x0: u32,
+ y0: u32,
+ width: u32,
+ height: u32,
+ level: f64,
+) -> Option<image::RgbaImage> {
+ if width == 0 || height == 0 {
+ return image::RgbaImage::from_raw(width, height, Vec::new());
+ }
+ let factor = ((level / 4.).round() as u32).max(2);
+ let small_w = (width / factor).max(1);
+ let small_h = (height / factor).max(1);
+ let small = resize_triangle(rgba, stride, (x0, y0, width, height), (small_w, small_h));
+ let blurred = resize_triangle(
+ &small,
+ small_w as usize * 4,
+ (0, 0, small_w, small_h),
+ (width, height),
+ );
+ image::RgbaImage::from_raw(width, height, blurred)
+}
+
+struct AxisSamples {
+ start: Vec<u32>,
+ count: Vec<u32>,
+ offset: Vec<usize>,
+ weights: Vec<f32>,
+ max_taps: u32,
+}
+
+struct Tap3 {
+ start: u32,
+ w0: f32,
+ w1: f32,
+ w2: f32,
+}
+
+fn triangle_kernel(x: f32) -> f32 {
+ let distance = x.abs();
+ if distance < 1.0 { 1.0 - distance } else { 0.0 }
+}
+
+fn triangle_axis(input_size: u32, output_size: u32) -> AxisSamples {
+ let ratio = input_size as f32 / output_size as f32;
+ let scale = if ratio < 1.0 { 1.0 } else { ratio };
+ let support = scale;
+ let outputs = output_size as usize;
+ let mut start = Vec::with_capacity(outputs);
+ let mut count = Vec::with_capacity(outputs);
+ let mut offset = Vec::with_capacity(outputs);
+ let mut weights = Vec::new();
+ let mut max_taps = 0u32;
+
+ for out in 0..output_size {
+ let input = (out as f32 + 0.5) * ratio;
+ let left = ((input - support).floor() as i64).clamp(0, i64::from(input_size) - 1) as u32;
+ let right = ((input + support).ceil() as i64)
+ .clamp(i64::from(left) + 1, i64::from(input_size)) as u32;
+ let center = input - 0.5;
+ let begin = weights.len();
+ let mut sum = 0.0f32;
+ for index in left..right {
+ let weight = triangle_kernel((index as f32 - center) / scale);
+ weights.push(weight);
+ sum += weight;
+ }
+ for weight in &mut weights[begin..] {
+ *weight /= sum;
+ }
+ let taps = right - left;
+ max_taps = max_taps.max(taps);
+ start.push(left);
+ count.push(taps);
+ offset.push(begin);
+ }
+
+ AxisSamples {
+ start,
+ count,
+ offset,
+ weights,
+ max_taps,
+ }
+}
+
+fn pack_tap3(axis: &AxisSamples) -> Vec<Tap3> {
+ axis.start
+ .iter()
+ .enumerate()
+ .map(|(out, &start)| {
+ let taps = axis.count[out] as usize;
+ let at = axis.offset[out];
+ let mut weight = [0.0f32; 3];
+ weight[..taps].copy_from_slice(&axis.weights[at..at + taps]);
+ Tap3 {
+ start,
+ w0: weight[0],
+ w1: weight[1],
+ w2: weight[2],
+ }
+ })
+ .collect()
+}
+
+#[inline(always)]
+fn quantize_channel(value: f32) -> u8 {
+ let clamped = value.clamp(0.0, 255.0);
+ // f32::round is a libm call. On [0, 255], truncating x + 0.5 matches
+ // half-away-from-zero except just below a midpoint, where the sum rounds
+ // up to the next integer. That exact integer sum is one past the tie.
+ let sum = clamped + 0.5;
+ // SAFETY: after the clamp, sum is finite and inside 0.5..=255.5, so it fits in i32.
+ let truncated = unsafe { sum.to_int_unchecked::<i32>() };
+ let boundary = truncated as f32;
+ let adjusted = if sum == boundary && clamped < boundary - 0.5 {
+ truncated - 1
+ } else {
+ truncated
+ };
+ adjusted as u8
+}
+
+fn copy_region(
+ src: &[u8],
+ stride: usize,
+ (x0, y0, width, height): (u32, u32, u32, u32),
+) -> Vec<u8> {
+ let row_bytes = width as usize * 4;
+ let mut copied = Vec::with_capacity(row_bytes * height as usize);
for row in 0..height {
let start = (y0 + row) as usize * stride + x0 as usize * 4;
- copied.extend_from_slice(&rgba[start..start + width as usize * 4]);
+ copied.extend_from_slice(&src[start..start + row_bytes]);
+ }
+ copied
+}
+
+// image::imageops::resize with FilterType::Triangle samples vertically into
+// f32, then horizontally, and rounds only when the horizontal pass stores.
+// Doing those passes in the other order, or rounding the intermediate, changes bytes.
+fn resize_triangle(
+ src: &[u8],
+ stride: usize,
+ (x0, y0, width, height): (u32, u32, u32, u32),
+ (new_width, new_height): (u32, u32),
+) -> Vec<u8> {
+ if width == 0 || height == 0 {
+ return vec![0u8; new_width as usize * new_height as usize * 4];
+ }
+ if new_width == 0 || new_height == 0 {
+ return Vec::new();
+ }
+ if new_width == width && new_height == height {
+ return copy_region(src, stride, (x0, y0, width, height));
}
- let source = image::RgbaImage::from_raw(width, height, copied)?;
- let processed = if pixelate {
- let block = (level.round() as u32).max(2);
+ let vertical_weights = triangle_axis(height, new_height);
+ let horizontal_weights = triangle_axis(width, new_width);
+ let row_bytes = width as usize * 4;
+ let mut vertical = vec![0f32; row_bytes * new_height as usize];
+ for out_y in 0..new_height as usize {
+ let dst = &mut vertical[out_y * row_bytes..(out_y + 1) * row_bytes];
+ let tap_count = vertical_weights.count[out_y] as usize;
+ let weight_at = vertical_weights.offset[out_y];
+ let src_y0 = vertical_weights.start[out_y];
+ let weights = &vertical_weights.weights[weight_at..weight_at + tap_count];
+ for (tap, &weight) in weights.iter().enumerate() {
+ let src_y = src_y0 + tap as u32;
+ let start = (y0 + src_y) as usize * stride + x0 as usize * 4;
+ accumulate_row(dst, &src[start..start + row_bytes], weight, tap == 0);
+ }
+ }
+
+ let out_row = new_width as usize * 4;
+ let mut out = vec![0u8; out_row * new_height as usize];
+ if horizontal_weights.max_taps <= 3 {
+ let packed = pack_tap3(&horizontal_weights);
+ for y in 0..new_height as usize {
+ sample_horizontal_tap3(
+ &vertical[y * row_bytes..(y + 1) * row_bytes],
+ &mut out[y * out_row..(y + 1) * out_row],
+ &packed,
+ width,
+ );
+ }
+ } else {
+ for y in 0..new_height as usize {
+ sample_horizontal_general(
+ &vertical[y * row_bytes..(y + 1) * row_bytes],
+ &mut out[y * out_row..(y + 1) * out_row],
+ &horizontal_weights,
+ );
+ }
+ }
+ out
+}
+
+// Separate from resize setup so the SAXPY vectorizes on its own.
+#[inline(never)]
+fn accumulate_row(dst: &mut [f32], src: &[u8], weight: f32, replace: bool) {
+ if replace {
+ for (slot, &pixel) in dst.iter_mut().zip(src) {
+ *slot = f32::from(pixel) * weight;
+ }
+ } else {
+ for (slot, &pixel) in dst.iter_mut().zip(src) {
+ *slot += f32::from(pixel) * weight;
+ }
+ }
+}
+
+#[inline(never)]
+fn sample_horizontal_tap3(src_row: &[f32], dst_row: &mut [u8], taps: &[Tap3], src_width: u32) {
+ let width = src_width as usize;
+ debug_assert!(width > 0);
+ debug_assert_eq!(src_row.len(), width * 4);
+ debug_assert_eq!(dst_row.len(), taps.len() * 4);
+ let src = src_row.as_ptr();
+ let dst = dst_row.as_mut_ptr();
+ for (out_x, tap) in taps.iter().enumerate() {
+ let i0 = tap.start as usize;
+ let i1 = if i0 + 1 < width { i0 + 1 } else { width - 1 };
+ let i2 = if i0 + 2 < width { i0 + 2 } else { width - 1 };
+ // SAFETY: i0, i1, and i2 are < width, and src_row holds width RGBA pixels.
+ // out_x is < taps.len(), and dst_row holds that many pixels.
+ unsafe {
+ let p0 = src.add(i0 * 4);
+ let p1 = src.add(i1 * 4);
+ let p2 = src.add(i2 * 4);
+ let c0 = (*p0) * tap.w0 + (*p1) * tap.w1 + (*p2) * tap.w2;
+ let c1 = (*p0.add(1)) * tap.w0 + (*p1.add(1)) * tap.w1 + (*p2.add(1)) * tap.w2;
+ let c2 = (*p0.add(2)) * tap.w0 + (*p1.add(2)) * tap.w1 + (*p2.add(2)) * tap.w2;
+ let c3 = (*p0.add(3)) * tap.w0 + (*p1.add(3)) * tap.w1 + (*p2.add(3)) * tap.w2;
+ let out = dst.add(out_x * 4);
+ *out = quantize_channel(c0);
+ *out.add(1) = quantize_channel(c1);
+ *out.add(2) = quantize_channel(c2);
+ *out.add(3) = quantize_channel(c3);
+ }
+ }
+}
+
+#[inline(never)]
+fn sample_horizontal_general(src_row: &[f32], dst_row: &mut [u8], axis: &AxisSamples) {
+ debug_assert_eq!(dst_row.len(), axis.start.len() * 4);
+ let src = src_row.as_ptr();
+ let dst = dst_row.as_mut_ptr();
+ let weights = axis.weights.as_ptr();
+ for out_x in 0..axis.start.len() {
+ let tap_count = axis.count[out_x] as usize;
+ let weight_at = axis.offset[out_x];
+ let src_x0 = axis.start[out_x] as usize;
+ let mut acc = [0.0f32; 4];
+ // SAFETY: triangle_axis keeps src_x0 + tap inside the row, weight_at + tap
+ // inside `weights`, and out_x inside dst_row.
+ unsafe {
+ for tap in 0..tap_count {
+ let weight = *weights.add(weight_at + tap);
+ let pixel = src.add((src_x0 + tap) * 4);
+ acc[0] += (*pixel) * weight;
+ acc[1] += (*pixel.add(1)) * weight;
+ acc[2] += (*pixel.add(2)) * weight;
+ acc[3] += (*pixel.add(3)) * weight;
+ }
+ let out = dst.add(out_x * 4);
+ *out = quantize_channel(acc[0]);
+ *out.add(1) = quantize_channel(acc[1]);
+ *out.add(2) = quantize_channel(acc[2]);
+ *out.add(3) = quantize_channel(acc[3]);
+ }
+ }
+}
+
+fn pixelate_region(
+ rgba: &[u8],
+ stride: usize,
+ (x0, y0, width, height): (u32, u32, u32, u32),
+ block: u32,
+) -> Option<image::RgbaImage> {
+ let columns: Vec<usize> = pixelated_samples(width, block)
+ .map(|x| (x0 as usize + x) * 4)
+ .collect();
+ let row_bytes = width as usize * 4;
+ let mut pixels = Vec::with_capacity(row_bytes * height as usize);
+ let mut previous = None;
+ for y in pixelated_samples(height, block) {
+ if previous == Some(y) {
+ pixels.extend_from_within(pixels.len() - row_bytes..);
+ continue;
+ }
+ let row = &rgba[(y0 as usize + y) * stride..];
+ for &offset in &columns {
+ pixels.extend_from_slice(&row[offset..offset + 4]);
+ }
+ previous = Some(y);
+ }
+ image::RgbaImage::from_raw(width, height, pixels)
+}
+
+fn pixelated_samples(size: u32, block: u32) -> impl Iterator<Item = usize> {
+ let small = (size / block).max(1);
+ (0..size).map(move |x| nearest_sample(nearest_sample(x, small, size), size, small) as usize)
+}
+
+fn nearest_sample(output: u32, input: u32, output_size: u32) -> u32 {
+ let ratio = input as f32 / output_size as f32;
+ let sample = ((output as f32 + 0.5) * ratio).floor() as i64;
+ sample.clamp(0, i64::from(input) - 1) as u32
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn reference_pixelate(
+ rgba: &[u8],
+ frame_width: u32,
+ (x0, y0, width, height): (u32, u32, u32, u32),
+ block: u32,
+ ) -> Vec<u8> {
+ let source = image::imageops::crop_imm(
+ &image::RgbaImage::from_raw(
+ frame_width,
+ (rgba.len() / 4) as u32 / frame_width,
+ rgba.to_vec(),
+ )
+ .unwrap(),
+ x0,
+ y0,
+ width,
+ height,
+ )
+ .to_image();
let small = image::imageops::resize(
&source,
(width / block).max(1),
@@ -316,7 +650,22 @@ pub fn mask_filter(
image::imageops::FilterType::Nearest,
);
image::imageops::resize(&small, width, height, image::imageops::FilterType::Nearest)
- } else {
+ .into_raw()
+ }
+
+ fn reference_blur(
+ rgba: &[u8],
+ frame_width: u32,
+ (x0, y0, width, height): (u32, u32, u32, u32),
+ level: f64,
+ ) -> Vec<u8> {
+ let stride = frame_width as usize * 4;
+ let mut copied = Vec::with_capacity(width as usize * height as usize * 4);
+ for row in 0..height {
+ let start = (y0 + row) as usize * stride + x0 as usize * 4;
+ copied.extend_from_slice(&rgba[start..start + width as usize * 4]);
+ }
+ let source = image::RgbaImage::from_raw(width, height, copied).unwrap();
let factor = ((level / 4.).round() as u32).max(2);
let small = image::imageops::resize(
&source,
@@ -325,14 +674,219 @@ pub fn mask_filter(
image::imageops::FilterType::Triangle,
);
image::imageops::resize(&small, width, height, image::imageops::FilterType::Triangle)
- };
+ .into_raw()
+ }
- Some(processed)
-}
+ fn assert_blur_matches(
+ rgba: &[u8],
+ frame_width: u32,
+ frame_height: u32,
+ region: (u32, u32, u32, u32),
+ level: f64,
+ ) {
+ let actual = mask_filter(rgba, frame_width, frame_height, region, false, level)
+ .unwrap()
+ .into_raw();
+ let expected = reference_blur(rgba, frame_width, region, level);
+ assert_eq!(actual.len(), expected.len());
+ let mut max = 0u8;
+ let mut sum = 0u64;
+ let mut mismatches = 0usize;
+ let mut first = None;
+ for (index, (&got, &want)) in actual.iter().zip(expected.iter()).enumerate() {
+ let delta = got.abs_diff(want);
+ if delta == 0 {
+ continue;
+ }
+ mismatches += 1;
+ sum += u64::from(delta);
+ if delta > max {
+ max = delta;
+ }
+ if first.is_none() {
+ first = Some((index, got, want));
+ }
+ }
+ assert_eq!(
+ max,
+ 0,
+ "{region:?} level {level}: max {max} mismatches {mismatches} mean {} first {first:?}",
+ if actual.is_empty() {
+ 0.0
+ } else {
+ sum as f64 / actual.len() as f64
+ }
+ );
+ }
-#[cfg(test)]
-mod tests {
- use super::*;
+ #[test]
+ fn blur_matches_triangle_resize_across_sizes_levels_and_odd_dimensions() {
+ let levels = [0.5, 1.0, 2.2, 4.0, 7.5, 8.0, 15.9, 16.0, 18.0, 40.0, 100.0];
+ for (width, height) in [
+ (1u32, 1u32),
+ (1, 2),
+ (2, 1),
+ (2, 3),
+ (3, 5),
+ (5, 3),
+ (7, 13),
+ (8, 8),
+ (15, 17),
+ (16, 9),
+ (31, 33),
+ (33, 31),
+ (63, 65),
+ (64, 48),
+ (100, 1),
+ (1, 100),
+ (127, 81),
+ ] {
+ let rgba: Vec<u8> = (0..width * height * 4)
+ .map(|i| (i.wrapping_mul(37).wrapping_add(19) % 251) as u8)
+ .collect();
+ for level in levels {
+ assert_blur_matches(&rgba, width, height, (0, 0, width, height), level);
+ }
+ }
+
+ let (frame_width, frame_height) = (80u32, 60u32);
+ let rgba: Vec<u8> = (0..frame_width * frame_height * 4)
+ .map(|i| (i.wrapping_mul(91) % 251) as u8)
+ .collect();
+ for region in [
+ (0, 0, 80, 60),
+ (3, 5, 17, 13),
+ (7, 2, 13, 29),
+ (4, 4, 0, 9),
+ (4, 4, 9, 0),
+ (0, 0, 1, 1),
+ (11, 9, 63, 41),
+ (20, 15, 15, 17),
+ ] {
+ for level in levels {
+ assert_blur_matches(&rgba, frame_width, frame_height, region, level);
+ }
+ }
+
+ let (width, height) = (400u32, 300u32);
+ let rgba: Vec<u8> = (0..width * height * 4)
+ .map(|i| (i.wrapping_mul(31) % 251) as u8)
+ .collect();
+ for level in [8.0, 16.0, 40.0] {
+ assert_blur_matches(&rgba, width, height, (0, 0, width, height), level);
+ }
+ }
+
+ #[test]
+ fn blur_matches_triangle_resize_for_full_byte_range() {
+ let levels = [0.5, 1.0, 2.2, 4.0, 7.5, 8.0, 15.9, 16.0, 18.0, 40.0, 100.0];
+ for (width, height) in [(17u32, 19u32), (31, 33), (64, 48)] {
+ let rgba = full_range_rgba(width, height);
+ assert!(rgba.contains(&0) && rgba.contains(&255));
+ for level in levels {
+ assert_blur_matches(&rgba, width, height, (0, 0, width, height), level);
+ }
+ }
+
+ let (frame_width, frame_height) = (80u32, 60u32);
+ let rgba = full_range_rgba(frame_width, frame_height);
+ let region = (3u32, 5u32, 17u32, 13u32);
+ let cropped = copy_region(&rgba, frame_width as usize * 4, region);
+ assert!(cropped.contains(&0) && cropped.contains(&255));
+ for level in levels {
+ assert_blur_matches(&rgba, frame_width, frame_height, region, level);
+ }
+
+ for fill in [0u8, 255u8] {
+ let (width, height) = (32u32, 24u32);
+ let rgba = vec![fill; width as usize * height as usize * 4];
+ for level in [8.0, 16.0, 40.0] {
+ assert_blur_matches(&rgba, width, height, (0, 0, width, height), level);
+ }
+ }
+ }
+
+ fn full_range_rgba(width: u32, height: u32) -> Vec<u8> {
+ (0..width as usize * height as usize)
+ .flat_map(|i| {
+ let channel = (i % 256) as u8;
+ [
+ channel,
+ 255 - channel,
+ channel.wrapping_mul(3),
+ if i % 2 == 0 { 0 } else { 255 },
+ ]
+ })
+ .collect()
+ }
+
+ #[test]
+ fn quantize_channel_matches_clamped_round() {
+ let expected = |value: f32| value.clamp(0.0, 255.0).round() as u8;
+ let mut values = Vec::new();
+ for n in 0..255 {
+ let half = n as f32 + 0.5;
+ values.extend([half.next_down(), half, half.next_up()]);
+ }
+ for n in 0..=255 {
+ let integer = n as f32;
+ values.extend([integer.next_down(), integer, integer.next_up()]);
+ }
+ for value in [
+ 0.5,
+ 1.5,
+ 127.5,
+ 254.5,
+ -0.0,
+ -0.1,
+ -0.5,
+ -1.0,
+ -1.5,
+ -127.5,
+ -255.0,
+ -1000.0,
+ f32::NEG_INFINITY,
+ 255.5,
+ 256.0,
+ 256.5,
+ 1000.0,
+ f32::INFINITY,
+ ] {
+ values.extend([value.next_down(), value, value.next_up()]);
+ }
+ for value in values {
+ assert_eq!(quantize_channel(value), expected(value), "{value:?}");
+ }
+ }
+
+ #[test]
+ fn pixelate_matches_two_nearest_resizes() {
+ let (frame_width, frame_height) = (61u32, 47u32);
+ let rgba: Vec<u8> = (0..frame_width * frame_height * 4)
+ .map(|i| (i.wrapping_mul(37) % 251) as u8)
+ .collect();
+ for region in [
+ (0, 0, 61, 47),
+ (3, 5, 1, 1),
+ (4, 4, 0, 9),
+ (4, 4, 9, 0),
+ (7, 2, 13, 29),
+ (60, 0, 1, 47),
+ (10, 11, 50, 36),
+ ] {
+ for level in [1., 2., 2.6, 3., 7., 16., 40., 100.] {
+ let block = (f64::round(level) as u32).max(2);
+ let actual =
+ mask_filter(&rgba, frame_width, frame_height, region, true, level).unwrap();
+ assert_eq!(actual.dimensions(), (region.2, region.3));
+ assert_eq!(
+ actual.into_raw(),
+ reference_pixelate(&rgba, frame_width, region, block),
+ "{region:?} at {level}"
+ );
+ }
+ }
+ }
fn reference_blit(
dst: &mut [u8],
diff --git a/apps/desktop-gpui/kernels/src/thumbnail.rs b/apps/desktop-gpui/kernels/src/thumbnail.rs
index 06b42c5cf..99c9c0a99 100644
--- a/apps/desktop-gpui/kernels/src/thumbnail.rs
+++ b/apps/desktop-gpui/kernels/src/thumbnail.rs
@@ -1,6 +1,9 @@
-use image::RgbaImage;
+use image::{DynamicImage, RgbaImage};
use std::borrow::Cow;
+const PRESCALE_HEADROOM: u32 = 4;
+const MAX_PRESCALE_FACTOR: u32 = u8::MAX as u32;
+
pub fn fit_centered(image: &RgbaImage, target_width: u32, target_height: u32) -> RgbaImage {
let width = image.width();
let height = image.height();
@@ -13,16 +16,7 @@ pub fn fit_centered(image: &RgbaImage, target_width: u32, target_height: u32) ->
return RgbaImage::from_pixel(target_width, target_height, image::Rgba([0, 0, 0, 0]));
}
- let scale = (target_width as f32 / width as f32)
- .min(target_height as f32 / height as f32)
- .max(f32::MIN_POSITIVE);
-
- let scaled_width = (width as f32 * scale)
- .round()
- .clamp(1.0, target_width as f32) as u32;
- let scaled_height = (height as f32 * scale)
- .round()
- .clamp(1.0, target_height as f32) as u32;
+ let (scaled_width, scaled_height) = fitted_size(width, height, target_width, target_height);
let resized = if (scaled_width, scaled_height) == image.dimensions() {
Cow::Borrowed(image)
@@ -34,6 +28,72 @@ pub fn fit_centered(image: &RgbaImage, target_width: u32, target_height: u32) ->
))
};
+ place_centered(&resized, target_width, target_height)
+}
+
+/// [`fit_centered`] for a full-resolution RGB or RGBA capture. A source more
+/// than `2 * PRESCALE_HEADROOM` times the fitted size is box-averaged by a
+/// whole factor first, leaving Lanczos3 at least `PRESCALE_HEADROOM` times the
+/// fitted size to filter; anything smaller takes [`fit_centered`] unchanged.
+pub fn fit_centered_capture(
+ image: DynamicImage,
+ target_width: u32,
+ target_height: u32,
+) -> RgbaImage {
+ let (width, height) = (image.width(), image.height());
+ let channels = match &image {
+ DynamicImage::ImageRgb8(_) => 3,
+ DynamicImage::ImageRgba8(_) => 4,
+ _ => 0,
+ };
+ let factor =
+ if channels == 0 || width == 0 || height == 0 || target_width == 0 || target_height == 0 {
+ 1
+ } else {
+ prescale_factor(width, height, target_width, target_height)
+ };
+ if factor < 2 {
+ return fit_centered(&image.into_rgba8(), target_width, target_height);
+ }
+
+ let (scaled_width, scaled_height) = fitted_size(width, height, target_width, target_height);
+ let reduced = match channels {
+ 3 => box_downscale::<3>(image.as_bytes(), width, height, factor),
+ _ => box_downscale::<4>(image.as_bytes(), width, height, factor),
+ };
+ let resized = resize_lanczos3_at(
+ &reduced,
+ scaled_width,
+ scaled_height,
+ width as f32 / (factor * scaled_width) as f32,
+ height as f32 / (factor * scaled_height) as f32,
+ );
+ place_centered(&resized, target_width, target_height)
+}
+
+fn prescale_factor(width: u32, height: u32, target_width: u32, target_height: u32) -> u32 {
+ let (scaled_width, scaled_height) = fitted_size(width, height, target_width, target_height);
+ (width / (scaled_width * PRESCALE_HEADROOM))
+ .min(height / (scaled_height * PRESCALE_HEADROOM))
+ .min(MAX_PRESCALE_FACTOR)
+}
+
+fn fitted_size(width: u32, height: u32, target_width: u32, target_height: u32) -> (u32, u32) {
+ let scale = (target_width as f32 / width as f32)
+ .min(target_height as f32 / height as f32)
+ .max(f32::MIN_POSITIVE);
+
+ let scaled_width = (width as f32 * scale)
+ .round()
+ .clamp(1.0, target_width as f32) as u32;
+ let scaled_height = (height as f32 * scale)
+ .round()
+ .clamp(1.0, target_height as f32) as u32;
+ (scaled_width, scaled_height)
+}
+
+fn place_centered(resized: &RgbaImage, target_width: u32, target_height: u32) -> RgbaImage {
+ let (scaled_width, scaled_height) = resized.dimensions();
let offset_x = (target_width - scaled_width) / 2;
let offset_y = (target_height - scaled_height) / 2;
@@ -52,48 +112,185 @@ pub fn fit_centered(image: &RgbaImage, target_width: u32, target_height: u32) ->
.copy_from_slice(&resized_raw[resized_start..resized_start + row_bytes]);
}
} else {
- image::imageops::overlay(&mut canvas, &*resized, offset_x as i64, offset_y as i64);
+ image::imageops::overlay(&mut canvas, resized, offset_x as i64, offset_y as i64);
}
canvas
}
+/// Average `factor` x `factor` blocks of packed `CHANNELS`-byte pixels into
+/// RGBA, rounding to nearest. The last row and column of blocks average only
+/// the pixels they cover, so no edge pixels are lost.
+fn box_downscale<const CHANNELS: usize>(
+ pixels: &[u8],
+ width: u32,
+ height: u32,
+ factor: u32,
+) -> RgbaImage {
+ let (width, height, factor) = (width as usize, height as usize, factor as usize);
+ let stride = width * CHANNELS;
+ let reduced_width = width.div_ceil(factor);
+ let reduced_height = height.div_ceil(factor);
+ let mut reduced = RgbaImage::new(reduced_width as u32, reduced_height as u32);
+ let mut columns = vec![0u16; stride];
+ for (output, band) in reduced
+ .chunks_exact_mut(reduced_width * 4)
+ .zip(pixels[..stride * height].chunks(stride * factor))
+ {
+ columns.fill(0);
+ accumulate_rows(&mut columns, band);
+ let band_rows = band.len() / stride;
+ let full = BlockDivisor::new((band_rows * factor) as u32);
+ let last = BlockDivisor::new((band_rows * (width - (reduced_width - 1) * factor)) as u32);
+ for (index, (pixel, block)) in output
+ .chunks_exact_mut(4)
+ .zip(columns.chunks(factor * CHANNELS))
+ .enumerate()
+ {
+ let divisor = if index + 1 == reduced_width {
+ &last
+ } else {
+ &full
+ };
+ let mut sums = [0u32; CHANNELS];
+ for source in block.chunks_exact(CHANNELS) {
+ for (sum, &value) in sums.iter_mut().zip(source) {
+ *sum += u32::from(value);
+ }
+ }
+ for (channel, sum) in pixel.iter_mut().zip(sums) {
+ *channel = divisor.rounded(sum);
+ }
+ if CHANNELS == 3 {
+ pixel[3] = u8::MAX;
+ }
+ }
+ }
+ reduced
+}
+
+/// `(sum + count / 2) / count` by a reciprocal multiply. Exact while
+/// `256 * count^2 < 2^40`, which `MAX_PRESCALE_FACTOR` keeps.
+struct BlockDivisor {
+ half: u32,
+ multiplier: u64,
+}
+
+impl BlockDivisor {
+ fn new(count: u32) -> Self {
+ Self {
+ half: count / 2,
+ multiplier: (1u64 << 40) / u64::from(count) + 1,
+ }
+ }
+
+ fn rounded(&self, sum: u32) -> u8 {
+ ((u64::from(sum + self.half) * self.multiplier) >> 40) as u8
+ }
+}
+
+fn accumulate_rows(columns: &mut [u16], band: &[u8]) {
+ #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
+ if is_x86_feature_detected!("avx2") {
+ unsafe { accumulate_rows_avx2(columns, band) };
+ return;
+ }
+ accumulate_rows_portable(columns, band);
+}
+
+#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
+#[target_feature(enable = "avx2")]
+fn accumulate_rows_avx2(columns: &mut [u16], band: &[u8]) {
+ accumulate_rows_portable(columns, band);
+}
+
+#[inline(always)]
+fn accumulate_rows_portable(columns: &mut [u16], band: &[u8]) {
+ for row in band.chunks_exact(columns.len()) {
+ for (sum, &value) in columns.iter_mut().zip(row) {
+ *sum += u16::from(value);
+ }
+ }
+}
+
// Same weights and per-pixel f32 accumulation order as
// `image::imageops::resize(.., FilterType::Lanczos3)`, so the output is
// byte-identical; only the loop order changes, so whole blocks of a row stay in
// registers and vectorize instead of one generic `get_pixel` per tap.
+// Kept out of line so inlining it doesn't slow `fit_centered`'s no-resize paths.
+#[inline(never)]
fn resize_lanczos3(image: &RgbaImage, width: u32, height: u32) -> RgbaImage {
+ resize_lanczos3_at(
+ image,
+ width,
+ height,
+ image.width() as f32 / width as f32,
+ image.height() as f32 / height as f32,
+ )
+}
+
+fn resize_lanczos3_at(
+ image: &RgbaImage,
+ width: u32,
+ height: u32,
+ ratio_x: f32,
+ ratio_y: f32,
+) -> RgbaImage {
let source_row = image.width() as usize * 4;
let mut vertical = vec![0f32; source_row * height as usize];
vertical_pass(
&mut vertical,
image.as_raw(),
- &lanczos3_taps(image.height(), height),
+ &lanczos3_taps_at(image.height(), height, ratio_y),
source_row,
);
- let columns = lanczos3_taps(image.width(), width);
+ let columns = lanczos3_taps_at(image.width(), width, ratio_x);
let mut resized = RgbaImage::new(width, height);
- for (output, row) in resized
- .chunks_exact_mut(width as usize * 4)
- .zip(vertical.chunks_exact(source_row))
+ let output_row = width as usize * 4;
+ let mut outputs = resized.chunks_exact_mut(output_row * HORIZONTAL_ROWS);
+ let mut inputs = vertical.chunks_exact(source_row * HORIZONTAL_ROWS);
+ for (output, input) in outputs.by_ref().zip(inputs.by_ref()) {
+ horizontal_pass::<HORIZONTAL_ROWS>(output, input, &columns);
+ }
+ for (output, input) in outputs
+ .into_remainder()
+ .chunks_exact_mut(output_row)
+ .zip(inputs.remainder().chunks_exact(source_row))
{
- for (pixel, taps) in output.chunks_exact_mut(4).zip(&columns) {
- let mut sum = [0f32; 4];
- for (index, weight) in taps.weights.iter().enumerate() {
- let start = (taps.start + index) * 4;
- for (channel, &value) in sum.iter_mut().zip(&row[start..start + 4]) {
+ horizontal_pass::<1>(output, input, &columns);
+ }
+ resized
+}
+
+// Each pixel's sum is one chain of dependent additions, so ROWS rows that
+// share the same taps run side by side to keep the adders busy.
+fn horizontal_pass<const ROWS: usize>(output: &mut [u8], input: &[f32], columns: &[Taps]) {
+ let output_row = output.len() / ROWS;
+ let source_row = input.len() / ROWS;
+ let rows: [&[[f32; 4]]; ROWS] =
+ std::array::from_fn(|row| input[row * source_row..][..source_row].as_chunks().0);
+ for (column, taps) in columns.iter().enumerate() {
+ let windows = rows.map(|row| &row[taps.start..taps.start + taps.weights.len()]);
+ let mut sums = [[0f32; 4]; ROWS];
+ for (index, &weight) in taps.weights.iter().enumerate() {
+ for (sum, window) in sums.iter_mut().zip(&windows) {
+ for (channel, &value) in sum.iter_mut().zip(&window[index]) {
*channel += value * weight;
}
}
+ }
+ for (row, sum) in sums.iter().enumerate() {
+ let pixel = &mut output[row * output_row + column * 4..][..4];
for (channel, value) in pixel.iter_mut().zip(sum) {
*channel = value.clamp(0.0, 255.0).round() as u8;
}
}
}
- resized
}
+const HORIZONTAL_ROWS: usize = 8;
+
fn vertical_pass(vertical: &mut [f32], pixels: &[u8], rows: &[Taps], source_row: usize) {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
if is_x86_feature_detected!("avx2") {
@@ -144,8 +341,12 @@ struct Taps {
weights: Vec<f32>,
}
+#[cfg(test)]
fn lanczos3_taps(source: u32, target: u32) -> Vec<Taps> {
- let ratio = source as f32 / target as f32;
+ lanczos3_taps_at(source, target, source as f32 / target as f32)
+}
+
+fn lanczos3_taps_at(source: u32, target: u32, ratio: f32) -> Vec<Taps> {
let scale = if ratio < 1.0 { 1.0 } else { ratio };
let support = 3.0 * scale;
(0..target)
@@ -345,6 +546,8 @@ mod tests {
(1920, 1080, 320, 180),
(1366, 768, 320, 180),
(777, 1333, 104, 180),
+ (1920, 1080, 319, 179),
+ (2560, 1440, 320, 13),
(321, 181, 320, 180),
(320, 400, 320, 180),
(100, 60, 320, 180),
@@ -365,6 +568,278 @@ mod tests {
}
}
+ fn rgb(image: &RgbaImage) -> DynamicImage {
+ DynamicImage::ImageRgb8(DynamicImage::ImageRgba8(image.clone()).into_rgb8())
+ }
+
+ fn opaque_noise(width: u32, height: u32) -> RgbaImage {
+ let mut image = noise(width, height);
+ for pixel in image.pixels_mut() {
+ pixel[3] = 255;
+ }
+ image
+ }
+
+ fn interface(width: u32, height: u32) -> RgbaImage {
+ let mut image = RgbaImage::from_pixel(width, height, image::Rgba([236, 236, 236, 255]));
+ for x in (0..width).step_by(97) {
+ for y in 0..height {
+ image.put_pixel(x, y, image::Rgba([40, 40, 40, 255]));
+ }
+ }
+ for top in (0..height.saturating_sub(12)).step_by(23) {
+ let left = (top * 7) % (width / 2);
+ for y in top..top + 12 {
+ for x in left..(left + width / 3).min(width) {
+ image.put_pixel(x, y, image::Rgba([30, 90, 200, 255]));
+ }
+ }
+ }
+ let mut state = 0x2545_f491_4f6c_dd1d_u64;
+ for _ in 0..4000 {
+ state = state
+ .wrapping_mul(6_364_136_223_846_793_005)
+ .wrapping_add(1_442_695_040_888_963_407);
+ let x = (state >> 33) as u32 % (width - 8);
+ let y = (state >> 13) as u32 % (height - 14);
+ for dy in 0..12 {
+ image.put_pixel(x, y + dy, image::Rgba([20, 20, 20, 255]));
+ }
+ for dx in 0..6 {
+ image.put_pixel(x + dx, y, image::Rgba([20, 20, 20, 255]));
+ }
+ }
+ image
+ }
+
+ fn checker(width: u32, height: u32, period: u32) -> RgbaImage {
+ RgbaImage::from_fn(width, height, |x, y| {
+ let value = if (x / period + y / period) % 2 == 0 {
+ 0
+ } else {
+ 255
+ };
+ image::Rgba([value, value, value, 255])
+ })
+ }
+
+ fn gradient(width: u32, height: u32) -> RgbaImage {
+ RgbaImage::from_fn(width, height, |x, y| {
+ image::Rgba([
+ (x * 255 / width) as u8,
+ (y * 255 / height) as u8,
+ ((x + y) * 255 / (width + height)) as u8,
+ 255,
+ ])
+ })
+ }
+
+ fn psnr(reference: &RgbaImage, actual: &RgbaImage) -> f64 {
+ let squared: f64 = reference
+ .as_raw()
+ .iter()
+ .zip(actual.as_raw())
+ .map(|(&a, &b)| (f64::from(a) - f64::from(b)).powi(2))
+ .sum();
+ let mse = squared / reference.as_raw().len() as f64;
+ if mse == 0.0 {
+ f64::INFINITY
+ } else {
+ 10.0 * (255.0 * 255.0 / mse).log10()
+ }
+ }
+
+ fn ssim(reference: &RgbaImage, actual: &RgbaImage) -> (f64, f64) {
+ let luma = |image: &RgbaImage| -> Vec<f64> {
+ image
+ .pixels()
+ .map(|p| (f64::from(p[0]) + f64::from(p[1]) + f64::from(p[2])) / 3.0)
+ .collect()
+ };
+ let (a, b) = (luma(reference), luma(actual));
+ let (width, height) = (reference.width() as usize, reference.height() as usize);
+ let (c1, c2) = ((0.01 * 255.0f64).powi(2), (0.03 * 255.0f64).powi(2));
+ let (mut total, mut count, mut worst) = (0.0, 0, f64::INFINITY);
+ for top in (0..=height - 8).step_by(4) {
+ for left in (0..=width - 8).step_by(4) {
+ let window = |values: &[f64]| -> Vec<f64> {
+ (top..top + 8)
+ .flat_map(|y| (left..left + 8).map(move |x| y * width + x))
+ .map(|index| values[index])
+ .collect()
+ };
+ let (wa, wb) = (window(&a), window(&b));
+ let mean = |w: &[f64]| w.iter().sum::<f64>() / 64.0;
+ let (ma, mb) = (mean(&wa), mean(&wb));
+ let (mut va, mut vb, mut cov) = (0.0, 0.0, 0.0);
+ for (x, y) in wa.iter().zip(&wb) {
+ va += (x - ma).powi(2) / 64.0;
+ vb += (y - mb).powi(2) / 64.0;
+ cov += (x - ma) * (y - mb) / 64.0;
+ }
+ let value = ((2.0 * ma * mb + c1) * (2.0 * cov + c2))
+ / ((ma * ma + mb * mb + c1) * (va + vb + c2));
+ total += value;
+ count += 1;
+ worst = worst.min(value);
+ }
+ }
+ (total / f64::from(count), worst)
+ }
+
+ #[test]
+ fn box_downscale_averages_blocks_and_keeps_partial_edges() {
+ let pixels: Vec<u8> = (0..5 * 3 * 3).map(|value| value as u8 * 5).collect();
+ let reduced = box_downscale::<3>(&pixels, 5, 3, 2);
+ assert_eq!(reduced.dimensions(), (3, 2));
+ let expected = |x: u32, y: u32| -> [u8; 4] {
+ let mut rgba = [0u8, 0, 0, 255];
+ for (channel, value) in rgba.iter_mut().take(3).enumerate() {
+ let mut sum = 0u32;
+ let mut count = 0u32;
+ for sy in y * 2..(y * 2 + 2).min(3) {
+ for sx in x * 2..(x * 2 + 2).min(5) {
+ sum += u32::from(pixels[((sy * 5 + sx) * 3) as usize + channel]);
+ count += 1;
+ }
+ }
+ *value = ((sum + count / 2) / count) as u8;
+ }
+ rgba
+ };
+ for (x, y, pixel) in reduced.enumerate_pixels() {
+ assert_eq!(pixel.0, expected(x, y), "{x},{y}");
+ }
+
+ let rgba = noise(7, 5);
+ let reduced = box_downscale::<4>(rgba.as_raw(), 7, 5, 3);
+ assert_eq!(reduced.dimensions(), (3, 2));
+ let corner = rgba.get_pixel(6, 3).0;
+ let below = rgba.get_pixel(6, 4).0;
+ for channel in 0..4 {
+ let sum = u32::from(corner[channel]) + u32::from(below[channel]);
+ assert_eq!(u32::from(reduced.get_pixel(2, 1)[channel]), (sum + 1) / 2);
+ }
+ }
+
+ #[test]
+ fn block_divisor_rounds_like_integer_division() {
+ let check = |count: u32, sum: u32| {
+ assert_eq!(
+ u32::from(BlockDivisor::new(count).rounded(sum)),
+ (sum + count / 2) / count,
+ "{sum} / {count}"
+ );
+ };
+ for count in 1..=64 {
+ for sum in 0..=255 * count {
+ check(count, sum);
+ }
+ }
+ let max = MAX_PRESCALE_FACTOR * MAX_PRESCALE_FACTOR;
+ for count in (65..=max).step_by(97).chain([max - 1, max]) {
+ for quotient in [0, 1, 127, 128, 254, 255] {
+ for offset in [0, 1, count / 2 - 1, count / 2, count / 2 + 1, count - 1] {
+ let sum = quotient * count + offset;
+ if sum <= 255 * count {
+ check(count, sum);
+ }
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn box_downscale_does_not_overflow_at_the_largest_factor() {
+ let side = MAX_PRESCALE_FACTOR;
+ for value in [255u8, 254, 1] {
+ let pixels = vec![value; (side * side * 3) as usize];
+ let reduced = box_downscale::<3>(&pixels, side, side, side);
+ assert_eq!(reduced.get_pixel(0, 0).0, [value, value, value, 255]);
+ }
+ assert_eq!(
+ prescale_factor(400_000, 225_000, 320, 180),
+ MAX_PRESCALE_FACTOR
+ );
+ }
+
+ #[test]
+ fn capture_fit_is_exact_below_the_prescale_threshold() {
+ for (width, height) in [
+ (1920, 1080),
+ (1366, 768),
+ (2001, 1201),
+ (2559, 1439),
+ (640, 360),
+ (320, 180),
+ (160, 90),
+ (1, 1),
+ (0, 0),
+ ] {
+ let source = noise(width, height);
+ let expected = fit_centered(&source, 320, 180);
+ assert!(
+ fit_centered_capture(DynamicImage::ImageRgba8(source.clone()), 320, 180)
+ == expected,
+ "rgba {width}x{height}"
+ );
+ let opaque = rgb(&source);
+ let expected = fit_centered(&opaque.to_rgba8(), 320, 180);
+ assert!(
+ fit_centered_capture(opaque, 320, 180) == expected,
+ "rgb {width}x{height}"
+ );
+ }
+ }
+
+ #[test]
+ fn capture_fit_stays_within_tight_bounds_of_the_exact_fit() {
+ for (width, height) in [
+ (3840, 2160),
+ (2560, 1440),
+ (2561, 1441),
+ (3440, 1440),
+ (5120, 2880),
+ (1440, 2560),
+ ] {
+ assert!(prescale_factor(width, height, 320, 180) >= 2);
+ let sources: [(&str, RgbaImage); 9] = [
+ ("noise", opaque_noise(width, height)),
+ ("noise-alpha", noise(width, height)),
+ ("interface", interface(width, height)),
+ ("gradient", gradient(width, height)),
+ ("checker-2", checker(width, height, 2)),
+ ("checker-3", checker(width, height, 3)),
+ ("checker-5", checker(width, height, 5)),
+ ("checker-7", checker(width, height, 7)),
+ ("checker-1", checker(width, height, 1)),
+ ];
+ for (name, source) in sources {
+ let expected = fit_centered(&source, 320, 180);
+ let input = if source.pixels().all(|pixel| pixel[3] == 255) {
+ rgb(&source)
+ } else {
+ DynamicImage::ImageRgba8(source)
+ };
+ let actual = fit_centered_capture(input, 320, 180);
+ assert_eq!(actual.dimensions(), expected.dimensions());
+ for (a, b) in actual.pixels().zip(expected.pixels()) {
+ assert_eq!(a[3] == 0, b[3] == 0, "{name} {width}x{height} padding");
+ }
+ let psnr = psnr(&expected, &actual);
+ let (ssim_mean, ssim_min) = ssim(&expected, &actual);
+ println!(
+ "{name} {width}x{height}: psnr {psnr:.2} dB, ssim mean {ssim_mean:.5}, min {ssim_min:.5}"
+ );
+ assert!(psnr >= 44.0, "{name} {width}x{height}: psnr {psnr:.2}");
+ assert!(
+ ssim_mean >= 0.99 && ssim_min >= 0.98,
+ "{name} {width}x{height}: ssim {ssim_mean:.5} min {ssim_min:.5}"
+ );
+ }
+ }
+ }
+
#[test]
fn portable_vertical_pass_matches_the_dispatched_one() {
for (width, height, target_height) in [(3840, 2160, 180), (1001, 777, 180), (7, 9, 16)] {
diff --git a/apps/desktop-gpui/kernels/src/waveform.rs b/apps/desktop-gpui/kernels/src/waveform.rs
index 76b8d0275..0c1728e1c 100644
--- a/apps/desktop-gpui/kernels/src/waveform.rs
+++ b/apps/desktop-gpui/kernels/src/waveform.rs
@@ -33,24 +33,79 @@ mod tests {
.collect()
}
+ fn assert_peak_bits(actual: &[f32], expected: &[f32], channels: u16, block_len: usize) {
+ assert_eq!(actual.len(), expected.len(), "{channels} {block_len}");
+ for (actual, expected) in actual.iter().zip(expected) {
+ assert_eq!(
+ actual.to_bits(),
+ expected.to_bits(),
+ "{channels} {block_len}"
+ );
+ }
+ }
+
#[test]
fn peaks_match_a_sequential_sum_across_uneven_blocks() {
- let samples = samples(48_000 * 3 + 1_234);
+ let uneven = samples(48_000 * 3 + 1_234);
+ for channels in [1, 2] {
+ let expected = sequential_peaks(&uneven, channels);
+ for block_len in [1, 7, 4_799, 9_600, 65_536, 32_768 * usize::from(channels)] {
+ let blocks: Vec<&[f32]> = uneven.chunks(block_len).collect();
+ assert_peak_bits(&peaks(&blocks, channels), &expected, channels, block_len);
+ }
+ assert_peak_bits(
+ &peaks(&[&uneven], channels),
+ &expected,
+ channels,
+ uneven.len(),
+ );
+ }
+
+ let contiguous = samples(48_000 * 120 + 17);
for channels in [1, 2] {
- for block_len in [1, 7, 4_799, 9_600, 65_536] {
- let blocks: Vec<&[f32]> = samples.chunks(block_len).collect();
- let actual = peaks(&blocks, channels);
- let expected = sequential_peaks(&samples, channels);
- assert_eq!(actual.len(), expected.len());
- for (actual, expected) in actual.iter().zip(&expected) {
- assert_eq!(
- actual.to_bits(),
- expected.to_bits(),
- "{channels} {block_len}"
- );
+ let expected = sequential_peaks(&contiguous, channels);
+ assert_peak_bits(
+ &peaks(&[&contiguous], channels),
+ &expected,
+ channels,
+ contiguous.len(),
+ );
+ let block_len = 32_768 * usize::from(channels);
+ let blocks: Vec<&[f32]> = contiguous.chunks(block_len).collect();
+ assert_peak_bits(&peaks(&blocks, channels), &expected, channels, block_len);
+ }
+ }
+
+ fn serial_peaks(blocks: &[&[f32]], channels: u16) -> Vec<f32> {
+ let block_samples = 48_000usize / 10 * usize::from(channels.max(1));
+ let mut peaks = Vec::new();
+ let mut sum = 0.0_f32;
+ let mut count = 0usize;
+ for slice in blocks {
+ let mut slice = *slice;
+ while !slice.is_empty() {
+ let (window, rest) = slice.split_at((block_samples - count).min(slice.len()));
+ sum = window.iter().fold(sum, |sum, sample| sum + sample.abs());
+ count += window.len();
+ slice = rest;
+ if count == block_samples {
+ peaks.push(sum / count as f32);
+ sum = 0.0;
+ count = 0;
}
}
}
+ if count > 0 {
+ peaks.push(sum / count as f32);
+ }
+ for peak in &mut peaks {
+ *peak = if *peak > 0.0 {
+ 20.0 * peak.log10()
+ } else {
+ -60.0
+ };
+ }
+ peaks
}
#[test]
@@ -61,17 +116,30 @@ mod tests {
for (track, channels) in [("mic", 1_u16), ("system audio", 2)] {
let samples = samples(48_000 * 3_600 * usize::from(channels));
let blocks: Vec<&[f32]> = samples.chunks(32_768 * usize::from(channels)).collect();
- let mut runs = Vec::new();
+ assert_peak_bits(
+ &peaks(&blocks, channels),
+ &serial_peaks(&blocks, channels),
+ channels,
+ 32_768 * usize::from(channels),
+ );
+ let mut serial_runs = Vec::new();
+ let mut lane_runs = Vec::new();
for _ in 0..5 {
+ let start = Instant::now();
+ black_box(serial_peaks(&blocks, channels));
+ serial_runs.push(start.elapsed().as_secs_f64() * 1000.0);
let start = Instant::now();
black_box(peaks(&blocks, channels));
- runs.push(start.elapsed().as_secs_f64() * 1000.0);
+ lane_runs.push(start.elapsed().as_secs_f64() * 1000.0);
}
- runs.sort_by(f64::total_cmp);
+ serial_runs.sort_by(f64::total_cmp);
+ lane_runs.sort_by(f64::total_cmp);
println!(
- "{{\"track\":\"{track}\",\"channels\":{channels},\"duration_seconds\":3600,\"median_ms\":{:.1},\"min_ms\":{:.1}}}",
- runs[runs.len() / 2],
- runs[0]
+ "{{\"track\":\"{track}\",\"channels\":{channels},\"duration_seconds\":3600,\"block_frames\":32768,\"serial_median_ms\":{:.1},\"serial_min_ms\":{:.1},\"lanes_median_ms\":{:.1},\"lanes_min_ms\":{:.1}}}",
+ serial_runs[serial_runs.len() / 2],
+ serial_runs[0],
+ lane_runs[lane_runs.len() / 2],
+ lane_runs[0]
);
}
}
diff --git a/apps/desktop-gpui/patches/zed-streaming-image.patch b/apps/desktop-gpui/patches/zed-streaming-image.patch
new file mode 100644
index 000000000..227458ff0
--- /dev/null
+++ b/apps/desktop-gpui/patches/zed-streaming-image.patch
@@ -0,0 +1,3136 @@
+diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs
+index 4b1679c..a143fab 100644
+--- a/crates/gpui/src/gpui.rs
++++ b/crates/gpui/src/gpui.rs
+@@ -47,6 +47,7 @@ pub mod profiler;
+ pub mod queue;
+ mod scene;
+ mod shared_uri;
++mod streaming_image;
+ mod style;
+ mod styled;
+ mod subscription;
+@@ -145,6 +146,7 @@ pub use refineable::*;
+ pub use scene::*;
+ pub use shared_uri::*;
+ use std::{any::Any, future::Future};
++pub use streaming_image::*;
+ pub use style::*;
+ pub use styled::*;
+ pub use subscription::*;
+diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs
+index 09adcdd..7dd2d38 100644
+--- a/crates/gpui/src/platform.rs
++++ b/crates/gpui/src/platform.rs
+@@ -40,7 +40,8 @@ use crate::{
+ FontRun, ForegroundExecutor, GlyphId, GpuSpecs, Hsla, ImageSource, Keymap, LineLayout, Pixels,
+ PlatformGestures, PlatformInput, Point, Priority, RenderGlyphParams, RenderImage,
+ RenderImageParams, RenderSvgParams, Scene, ShapedGlyph, ShapedRun, SharedString, Size,
+- SvgRenderer, SystemWindowTab, Task, Window, WindowControlArea, hash, point, px, size,
++ StreamFrame, StreamingImageId, SvgRenderer, SystemWindowTab, Task, Window, WindowControlArea,
++ hash, point, px, size,
+ };
+ #[cfg(any(target_os = "linux", target_os = "freebsd"))]
+ use anyhow::bail;
+@@ -1316,6 +1317,17 @@ pub trait PlatformAtlas {
+ build: &mut dyn FnMut() -> Result<Option<(Size<DevicePixels>, Cow<'a, [u8]>)>>,
+ ) -> Result<Option<AtlasTile>>;
+ fn remove(&self, key: &AtlasKey);
++ /// Returns the tile of the texture that holds `frame` for the streaming
++ /// image `id`, uploading the frame first when its generation differs from
++ /// the one the texture holds. Stream textures are never packed with, or
++ /// evicted alongside, atlas tiles.
++ fn get_or_update_stream(
++ &self,
++ id: StreamingImageId,
++ frame: &StreamFrame,
++ ) -> Result<Option<AtlasTile>>;
++ /// Releases the texture of the streaming image `id`, if there is one.
++ fn remove_stream(&self, id: StreamingImageId);
+ }
+
+ #[doc(hidden)]
+@@ -1368,6 +1380,24 @@ pub struct AtlasTile {
+ pub bounds: Bounds<DevicePixels>,
+ }
+
++impl AtlasTile {
++ /// The tile covering the whole texture of a streaming image.
++ pub fn for_stream(index: u32, size: Size<DevicePixels>) -> Self {
++ Self {
++ texture_id: AtlasTextureId {
++ index,
++ kind: AtlasTextureKind::Stream,
++ },
++ tile_id: TileId(0),
++ padding: 0,
++ bounds: Bounds {
++ origin: Point::default(),
++ size,
++ },
++ }
++ }
++}
++
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
+ #[repr(C)]
+ #[expect(missing_docs)]
+@@ -1393,6 +1423,7 @@ pub enum AtlasTextureKind {
+ Monochrome = 0,
+ Polychrome = 1,
+ Subpixel = 2,
++ Stream = 3,
+ }
+
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
+diff --git a/crates/gpui/src/platform/test/window.rs b/crates/gpui/src/platform/test/window.rs
+index 8894350..562ec92 100644
+--- a/crates/gpui/src/platform/test/window.rs
++++ b/crates/gpui/src/platform/test/window.rs
+@@ -2,8 +2,9 @@ use crate::{
+ AnyWindowHandle, AtlasKey, AtlasTextureId, AtlasTile, Bounds, DevicePixels,
+ DispatchEventResult, GpuSpecs, Pixels, PlatformAtlas, PlatformDisplay,
+ PlatformHeadlessRenderer, PlatformInput, PlatformInputHandler, PlatformWindow, Point,
+- PromptButton, RequestFrameOptions, Scene, Size, TestPlatform, TileId, WindowAppearance,
+- WindowBackgroundAppearance, WindowBounds, WindowControlArea, WindowParams,
++ PromptButton, RequestFrameOptions, Scene, Size, StreamFrame, StreamingImageId, TestPlatform,
++ TileId, WindowAppearance, WindowBackgroundAppearance, WindowBounds, WindowControlArea,
++ WindowParams,
+ };
+ use collections::HashMap;
+ use gpui_util::ResultExt as _;
+@@ -345,6 +346,9 @@ impl PlatformWindow for TestWindow {
+ pub(crate) struct TestAtlasState {
+ next_id: u32,
+ tiles: HashMap<AtlasKey, AtlasTile>,
++ streams: HashMap<StreamingImageId, (u64, AtlasTile)>,
++ stream_uploads: usize,
++ removed_streams: Vec<StreamingImageId>,
+ }
+
+ pub(crate) struct TestAtlas(Mutex<TestAtlasState>);
+@@ -354,8 +358,21 @@ impl TestAtlas {
+ TestAtlas(Mutex::new(TestAtlasState {
+ next_id: 0,
+ tiles: HashMap::default(),
++ streams: HashMap::default(),
++ stream_uploads: 0,
++ removed_streams: Vec::new(),
+ }))
+ }
++
++ #[cfg(test)]
++ pub(crate) fn stream_uploads(&self) -> usize {
++ self.0.lock().stream_uploads
++ }
++
++ #[cfg(test)]
++ pub(crate) fn removed_streams(&self) -> Vec<StreamingImageId> {
++ self.0.lock().removed_streams.clone()
++ }
+ }
+
+ impl PlatformAtlas for TestAtlas {
+@@ -405,4 +422,35 @@ impl PlatformAtlas for TestAtlas {
+ let mut state = self.0.lock();
+ state.tiles.remove(key);
+ }
++
++ fn get_or_update_stream(
++ &self,
++ id: StreamingImageId,
++ frame: &StreamFrame,
++ ) -> anyhow::Result<Option<AtlasTile>> {
++ let mut state = self.0.lock();
++ let state = &mut *state;
++ if let Some((generation, tile)) = state.streams.get_mut(&id)
++ && tile.bounds.size == frame.size()
++ {
++ let tile = *tile;
++ if *generation != frame.generation() {
++ *generation = frame.generation();
++ state.stream_uploads += 1;
++ }
++ return Ok(Some(tile));
++ }
++ state.next_id += 1;
++ let tile = AtlasTile::for_stream(state.next_id, frame.size());
++ state.streams.insert(id, (frame.generation(), tile));
++ state.stream_uploads += 1;
++ Ok(Some(tile))
++ }
++
++ fn remove_stream(&self, id: StreamingImageId) {
++ let mut state = self.0.lock();
++ if state.streams.remove(&id).is_some() {
++ state.removed_streams.push(id);
++ }
++ }
+ }
+diff --git a/crates/gpui/src/streaming_image.rs b/crates/gpui/src/streaming_image.rs
+new file mode 100644
+index 0000000..3cb3416
+--- /dev/null
++++ b/crates/gpui/src/streaming_image.rs
+@@ -0,0 +1,363 @@
++use crate::{DevicePixels, Size};
++use anyhow::{Context as _, Result};
++use parking_lot::Mutex;
++use std::{
++ fmt,
++ sync::{
++ Arc, Weak,
++ atomic::{AtomicU64, Ordering},
++ },
++};
++
++/// Identifies a [`StreamingImage`] for the lifetime of the process.
++#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
++pub struct StreamingImageId(pub u64);
++
++/// The byte order of a [`StreamingImage`]'s pixels.
++#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
++pub enum StreamingImageFormat {
++ /// Blue, green, red, alpha, the order [`RenderImage`](crate::RenderImage) uses.
++ Bgra8,
++ /// Red, green, blue, alpha.
++ Rgba8,
++}
++
++/// One frame of a [`StreamingImage`], as returned by [`StreamingImage::current`].
++#[derive(Clone)]
++pub struct StreamFrame {
++ generation: u64,
++ size: Size<DevicePixels>,
++ stride: usize,
++ format: StreamingImageFormat,
++ bytes: Arc<Vec<u8>>,
++}
++
++impl StreamFrame {
++ /// Increases every time the image receives a new frame.
++ pub fn generation(&self) -> u64 {
++ self.generation
++ }
++
++ /// The frame's size in pixels.
++ pub fn size(&self) -> Size<DevicePixels> {
++ self.size
++ }
++
++ /// The number of bytes from the start of one row to the start of the next.
++ pub fn stride(&self) -> usize {
++ self.stride
++ }
++
++ /// The byte order of the frame's pixels.
++ pub fn format(&self) -> StreamingImageFormat {
++ self.format
++ }
++
++ /// The frame's rows, [`Self::stride`] bytes apart. Holds at least
++ /// `stride * height` bytes.
++ pub fn bytes(&self) -> &[u8] {
++ &self.bytes
++ }
++}
++
++impl fmt::Debug for StreamFrame {
++ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
++ f.debug_struct("StreamFrame")
++ .field("generation", &self.generation)
++ .field("size", &self.size)
++ .field("stride", &self.stride)
++ .field("format", &self.format)
++ .field("len", &self.bytes.len())
++ .finish()
++ }
++}
++
++/// An image whose pixels are replaced frame after frame, such as a video or a
++/// camera preview.
++///
++/// Each window that paints the image keeps one texture for it and uploads into
++/// that texture only when the frame has changed, instead of packing every frame
++/// into the sprite atlas as a new image. The texture is released once every
++/// handle is dropped or the image is [cleared](Self::clear).
++///
++/// Handles are cheap to clone and frames can be set from any thread.
++#[derive(Clone)]
++pub struct StreamingImage(Arc<Shared>);
++
++struct Shared {
++ id: StreamingImageId,
++ format: StreamingImageFormat,
++ state: Mutex<State>,
++}
++
++#[derive(Default)]
++struct State {
++ generation: u64,
++ frame: Option<StreamFrame>,
++ spare: Option<Vec<u8>>,
++}
++
++impl StreamingImage {
++ /// Creates an image with no frame, whose frames are in the given byte order.
++ pub fn new(format: StreamingImageFormat) -> Self {
++ static NEXT_ID: AtomicU64 = AtomicU64::new(0);
++ Self(Arc::new(Shared {
++ id: StreamingImageId(NEXT_ID.fetch_add(1, Ordering::Relaxed)),
++ format,
++ state: Mutex::default(),
++ }))
++ }
++
++ /// The image's process-wide identifier.
++ pub fn id(&self) -> StreamingImageId {
++ self.0.id
++ }
++
++ /// The byte order of the image's frames.
++ pub fn format(&self) -> StreamingImageFormat {
++ self.0.format
++ }
++
++ /// Replaces the current frame with `bytes`, whose rows are `stride` bytes
++ /// apart.
++ ///
++ /// Returns the previous frame's buffer when nothing else holds it any more,
++ /// so the producer can write the next frame into it instead of allocating.
++ pub fn set_frame(
++ &self,
++ size: Size<DevicePixels>,
++ stride: usize,
++ bytes: Vec<u8>,
++ ) -> Result<Option<Vec<u8>>> {
++ validate_frame(size, stride, bytes.len())?;
++ let previous = self.replace_frame(size, stride, bytes);
++ Ok(previous.and_then(|frame| Arc::try_unwrap(frame.bytes).ok()))
++ }
++
++ /// Copies `bytes`, whose rows are `stride` bytes apart, into a new current
++ /// frame. The copy reuses the buffer of an earlier frame once nothing else
++ /// holds it, so a steady stream of same-sized frames does not allocate.
++ pub fn copy_frame(&self, size: Size<DevicePixels>, stride: usize, bytes: &[u8]) -> Result<()> {
++ validate_frame(size, stride, bytes.len())?;
++ let len = stride * size.height.0 as usize;
++ let mut buffer = self.0.state.lock().spare.take().unwrap_or_default();
++ buffer.clear();
++ buffer.extend_from_slice(&bytes[..len]);
++ let previous = self.replace_frame(size, stride, buffer);
++ if let Some(previous) = previous.and_then(|frame| Arc::try_unwrap(frame.bytes).ok()) {
++ let mut state = self.0.state.lock();
++ if state.frame.is_some() {
++ state.spare = Some(previous);
++ }
++ }
++ Ok(())
++ }
++
++ /// The current frame, if any.
++ pub fn current(&self) -> Option<StreamFrame> {
++ self.0.state.lock().frame.clone()
++ }
++
++ /// Drops the current frame. Windows stop painting the image and release
++ /// its texture on their next draw.
++ pub fn clear(&self) {
++ let mut state = self.0.state.lock();
++ state.frame = None;
++ state.spare = None;
++ }
++
++ pub(crate) fn downgrade(&self) -> WeakStreamingImage {
++ WeakStreamingImage(Arc::downgrade(&self.0))
++ }
++
++ fn replace_frame(
++ &self,
++ size: Size<DevicePixels>,
++ stride: usize,
++ bytes: Vec<u8>,
++ ) -> Option<StreamFrame> {
++ let mut state = self.0.state.lock();
++ state.generation += 1;
++ let frame = StreamFrame {
++ generation: state.generation,
++ size,
++ stride,
++ format: self.0.format,
++ bytes: Arc::new(bytes),
++ };
++ state.frame.replace(frame)
++ }
++}
++
++impl fmt::Debug for StreamingImage {
++ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
++ f.debug_struct("StreamingImage")
++ .field("id", &self.0.id)
++ .field("format", &self.0.format)
++ .finish_non_exhaustive()
++ }
++}
++
++pub(crate) struct WeakStreamingImage(Weak<Shared>);
++
++impl WeakStreamingImage {
++ pub(crate) fn has_frame(&self) -> bool {
++ self.0
++ .upgrade()
++ .is_some_and(|shared| shared.state.lock().frame.is_some())
++ }
++}
++
++fn validate_frame(size: Size<DevicePixels>, stride: usize, len: usize) -> Result<()> {
++ anyhow::ensure!(
++ size.width.0 > 0 && size.height.0 > 0,
++ "streaming image frames must not be empty, got {}x{}",
++ size.width.0,
++ size.height.0
++ );
++ let row_bytes = size.width.0 as usize * 4;
++ anyhow::ensure!(
++ stride >= row_bytes && stride.is_multiple_of(4),
++ "stride {stride} must be a multiple of 4 and at least {row_bytes} for a frame {} pixels wide",
++ size.width.0
++ );
++ let required = stride
++ .checked_mul(size.height.0 as usize)
++ .context("streaming image frame is too large")?;
++ anyhow::ensure!(
++ len >= required,
++ "a {}x{} frame with stride {stride} needs {required} bytes, got {len}",
++ size.width.0,
++ size.height.0
++ );
++ Ok(())
++}
++
++#[cfg(test)]
++mod tests {
++ use super::*;
++ use crate::size;
++
++ fn frame_size(width: i32, height: i32) -> Size<DevicePixels> {
++ size(DevicePixels(width), DevicePixels(height))
++ }
++
++ #[test]
++ fn handles_are_send_and_sync() {
++ fn assert_send_sync<T: Send + Sync>() {}
++ assert_send_sync::<StreamingImage>();
++ assert_send_sync::<StreamFrame>();
++ }
++
++ #[test]
++ fn ids_are_unique() {
++ let first = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let second = StreamingImage::new(StreamingImageFormat::Bgra8);
++ assert_ne!(first.id(), second.id());
++ assert_eq!(first.clone().id(), first.id());
++ }
++
++ #[test]
++ fn each_frame_advances_the_generation() {
++ let image = StreamingImage::new(StreamingImageFormat::Rgba8);
++ assert!(image.current().is_none());
++
++ image.set_frame(frame_size(2, 1), 8, vec![1; 8]).unwrap();
++ let first = image.current().unwrap();
++ assert_eq!(first.generation(), 1);
++ assert_eq!(first.size(), frame_size(2, 1));
++ assert_eq!(first.stride(), 8);
++ assert_eq!(first.format(), StreamingImageFormat::Rgba8);
++ assert_eq!(first.bytes(), &[1; 8]);
++
++ image.copy_frame(frame_size(1, 2), 4, &[2; 8]).unwrap();
++ let second = image.current().unwrap();
++ assert_eq!(second.generation(), 2);
++ assert_eq!(second.size(), frame_size(1, 2));
++ assert_eq!(second.bytes(), &[2; 8]);
++ assert_eq!(first.bytes(), &[1; 8]);
++ }
++
++ #[test]
++ fn set_frame_returns_the_previous_buffer_once_nothing_holds_it() {
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ assert_eq!(
++ image.set_frame(frame_size(1, 1), 4, vec![1; 4]).unwrap(),
++ None
++ );
++
++ let returned = image.set_frame(frame_size(1, 1), 4, vec![2; 4]).unwrap();
++ assert_eq!(returned, Some(vec![1; 4]));
++
++ let held = image.current().unwrap();
++ let returned = image.set_frame(frame_size(1, 1), 4, vec![3; 4]).unwrap();
++ assert_eq!(returned, None);
++ assert_eq!(held.bytes(), &[2; 4]);
++ }
++
++ #[test]
++ fn copy_frame_reuses_earlier_buffers() {
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let pixels = vec![7u8; 64 * 64 * 4];
++ image.copy_frame(frame_size(64, 64), 256, &pixels).unwrap();
++ let first = image.current().unwrap().bytes().as_ptr();
++ image.copy_frame(frame_size(64, 64), 256, &pixels).unwrap();
++ let second = image.current().unwrap().bytes().as_ptr();
++ image.copy_frame(frame_size(64, 64), 256, &pixels).unwrap();
++ let third = image.current().unwrap().bytes().as_ptr();
++
++ assert_ne!(first, second);
++ assert_eq!(first, third);
++ }
++
++ #[test]
++ fn copy_frame_copies_whole_padded_rows() {
++ let image = StreamingImage::new(StreamingImageFormat::Rgba8);
++ let bytes: Vec<u8> = (0..40).collect();
++ image.copy_frame(frame_size(1, 2), 16, &bytes).unwrap();
++ assert_eq!(image.current().unwrap().bytes(), &bytes[..32]);
++ }
++
++ #[test]
++ fn invalid_frames_are_rejected_without_changing_the_image() {
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ image.set_frame(frame_size(2, 2), 8, vec![0; 16]).unwrap();
++
++ assert!(image.set_frame(frame_size(0, 2), 8, vec![0; 16]).is_err());
++ assert!(image.set_frame(frame_size(2, 0), 8, vec![0; 16]).is_err());
++ assert!(image.set_frame(frame_size(2, 2), 4, vec![0; 16]).is_err());
++ assert!(image.set_frame(frame_size(2, 2), 10, vec![0; 20]).is_err());
++ assert!(image.set_frame(frame_size(2, 2), 8, vec![0; 15]).is_err());
++ assert!(image.copy_frame(frame_size(2, 2), 12, &[0; 20]).is_err());
++ assert!(
++ image
++ .copy_frame(frame_size(i32::MAX, i32::MAX), usize::MAX - 3, &[])
++ .is_err()
++ );
++
++ let current = image.current().unwrap();
++ assert_eq!(current.generation(), 1);
++ assert_eq!(current.size(), frame_size(2, 2));
++ }
++
++ #[test]
++ fn clear_drops_the_frame_and_later_frames_keep_counting() {
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let weak = image.downgrade();
++ assert!(!weak.has_frame());
++
++ image.set_frame(frame_size(1, 1), 4, vec![0; 4]).unwrap();
++ assert!(weak.has_frame());
++
++ image.clear();
++ assert!(image.current().is_none());
++ assert!(!weak.has_frame());
++
++ image.copy_frame(frame_size(1, 1), 4, &[0; 4]).unwrap();
++ assert_eq!(image.current().unwrap().generation(), 2);
++ assert!(weak.has_frame());
++
++ drop(image);
++ assert!(!weak.has_frame());
++ }
++}
+diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
+index de95b7d..75c3539 100644
+--- a/crates/gpui/src/window.rs
++++ b/crates/gpui/src/window.rs
+@@ -21,6 +21,7 @@ use crate::{
+ WindowOptions, WindowParams, WindowTextSystem, point, prelude::*, profiler, px, rems, size,
+ transparent_black,
+ };
++use crate::{AtlasTile, StreamingImage, StreamingImageId, WeakStreamingImage};
+
+ use anyhow::{Context as _, Result, anyhow};
+ use collections::{FxHashMap, FxHashSet};
+@@ -1102,6 +1103,7 @@ pub struct Window {
+ pub(crate) platform_window: Box<dyn PlatformWindow>,
+ display_id: Option<DisplayId>,
+ sprite_atlas: Arc<dyn PlatformAtlas>,
++ painted_streams: FxHashMap<StreamingImageId, WeakStreamingImage>,
+ text_system: Arc<WindowTextSystem>,
+ text_rendering_mode: Rc<Cell<TextRenderingMode>>,
+ rem_size: Pixels,
+@@ -1837,6 +1839,7 @@ impl Window {
+ platform_window,
+ display_id,
+ sprite_atlas,
++ painted_streams: FxHashMap::default(),
+ text_system,
+ text_rendering_mode: cx.text_rendering_mode.clone(),
+ rem_size: px(16.),
+@@ -2839,6 +2842,7 @@ impl Window {
+ let _arena_scope = ElementArenaScope::enter(&cx.element_arena);
+
+ self.invalidate_entities();
++ self.release_unpainted_streams();
+ cx.entities.clear_accessed();
+ debug_assert!(self.rendered_entity_stack.is_empty());
+ self.invalidator.set_dirty(false);
+@@ -2967,6 +2971,17 @@ impl Window {
+ mem::swap(&mut entities, entities_ref.deref_mut());
+ }
+
++ fn release_unpainted_streams(&mut self) {
++ let sprite_atlas = &self.sprite_atlas;
++ self.painted_streams.retain(|id, image| {
++ let keep = image.has_frame();
++ if !keep {
++ sprite_atlas.remove_stream(*id);
++ }
++ keep
++ });
++ }
++
+ fn invalidate_entities(&mut self) {
+ let mut views = self.invalidator.take_views();
+ for entity in views.drain() {
+@@ -4587,8 +4602,6 @@ impl Window {
+ ) -> Result<()> {
+ self.invalidator.debug_assert_paint();
+
+- let crop = (visible != fitted).then_some((visible, fitted));
+- let bounds = self.snap_bounds(visible);
+ let params = RenderImageParams {
+ image_id: data.id,
+ frame_index,
+@@ -4606,6 +4619,66 @@ impl Window {
+ )))
+ })?
+ .expect("Callback above only returns Some");
++ self.insert_polychrome_tile(visible, fitted, corner_radii, tile, grayscale);
++ Ok(())
++ }
++
++ /// Paint the current frame of a [`StreamingImage`] into the scene for the
++ /// next frame at the current z-index. Paints nothing while the image has
++ /// no frame.
++ ///
++ /// The window uploads a frame at most once, however many elements paint
++ /// it, and releases the image's texture once every handle to the image is
++ /// dropped or the image is cleared.
++ ///
++ /// This method should only be called as part of the paint phase of element drawing.
++ pub fn paint_streaming_image(
++ &mut self,
++ bounds: Bounds<Pixels>,
++ corner_radii: Corners<Pixels>,
++ image: &StreamingImage,
++ grayscale: bool,
++ ) -> Result<()> {
++ self.paint_streaming_image_fitted(bounds, bounds, corner_radii, image, grayscale)
++ }
++
++ /// [`Self::paint_streaming_image`] for object-fit layouts, with the same
++ /// contract as [`Self::paint_image_fitted`].
++ ///
++ /// This method should only be called as part of the paint phase of element drawing.
++ pub fn paint_streaming_image_fitted(
++ &mut self,
++ visible: Bounds<Pixels>,
++ fitted: Bounds<Pixels>,
++ corner_radii: Corners<Pixels>,
++ image: &StreamingImage,
++ grayscale: bool,
++ ) -> Result<()> {
++ self.invalidator.debug_assert_paint();
++
++ let Some(frame) = image.current() else {
++ return Ok(());
++ };
++ self.painted_streams
++ .entry(image.id())
++ .or_insert_with(|| image.downgrade());
++ let Some(tile) = self.sprite_atlas.get_or_update_stream(image.id(), &frame)? else {
++ return Ok(());
++ };
++ self.insert_polychrome_tile(visible, fitted, corner_radii, tile, grayscale);
++ Ok(())
++ }
++
++ fn insert_polychrome_tile(
++ &mut self,
++ visible: Bounds<Pixels>,
++ fitted: Bounds<Pixels>,
++ corner_radii: Corners<Pixels>,
++ tile: AtlasTile,
++ grayscale: bool,
++ ) {
++ let crop = (visible != fitted).then_some((visible, fitted));
++ let bounds = self.snap_bounds(visible);
+ // Crop the atlas tile to the visible fraction of the fitted box
+ // (proportional UV mapping; ≤1px rounding on arbitrary crops).
+ let tile = match crop {
+@@ -4646,7 +4719,6 @@ impl Window {
+ tile,
+ opacity,
+ });
+- Ok(())
+ }
+
+ /// Paint a surface into the scene for the next frame at the current z-index.
+@@ -6990,11 +7062,12 @@ mod frame_pacer_tests {
+ #[cfg(test)]
+ mod tests {
+ use crate::{
+- AppContext as _, Bounds, Context, FocusHandle, InteractiveElement as _, IntoElement,
+- ParentElement as _, Pixels, Render, Styled as _, TestAppContext, Window, canvas, div, px,
+- size,
++ AppContext as _, Bounds, Context, Corners, DevicePixels, FocusHandle,
++ InteractiveElement as _, IntoElement, ParentElement as _, Pixels, PlatformAtlas, Render,
++ StreamingImage, StreamingImageFormat, Styled as _, TestAppContext, Window, canvas, div,
++ platform::TestAtlas, px, size,
+ };
+- use std::{cell::Cell, rc::Rc};
++ use std::{cell::Cell, rc::Rc, sync::Arc};
+
+ struct RootView {
+ explicit_size: bool,
+@@ -7075,6 +7148,77 @@ mod tests {
+ }
+ }
+
++ struct StreamView {
++ images: Vec<StreamingImage>,
++ }
++
++ impl Render for StreamView {
++ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
++ let images = self.images.clone();
++ canvas(
++ |_, _, _| {},
++ move |bounds, _, window, _| {
++ for image in &images {
++ window
++ .paint_streaming_image(bounds, Corners::default(), image, false)
++ .unwrap();
++ }
++ },
++ )
++ .size_full()
++ }
++ }
++
++ #[test]
++ fn streams_upload_once_per_generation_and_are_released_when_unreachable() {
++ let mut cx = TestAppContext::single();
++ let frame_size = size(DevicePixels(2), DevicePixels(2));
++ let first = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let second = StreamingImage::new(StreamingImageFormat::Rgba8);
++ first.set_frame(frame_size, 8, vec![0; 16]).unwrap();
++ second.set_frame(frame_size, 8, vec![0; 16]).unwrap();
++ let window = cx.add_window({
++ let images = vec![first.clone(), first.clone(), second.clone()];
++ move |_, _| StreamView { images }
++ });
++ let atlas = Arc::new(TestAtlas::new());
++ cx.update_window(window.into(), |_, window, _| {
++ window.sprite_atlas = atlas.clone() as Arc<dyn PlatformAtlas>;
++ })
++ .unwrap();
++ let draw = |cx: &mut TestAppContext| {
++ cx.update_window(window.into(), |_, window, cx| {
++ window.draw(cx).clear();
++ window.painted_streams.len()
++ })
++ .unwrap()
++ };
++
++ assert_eq!(draw(&mut cx), 2);
++ assert_eq!(atlas.stream_uploads(), 2);
++ assert_eq!(draw(&mut cx), 2);
++ assert_eq!(atlas.stream_uploads(), 2);
++
++ first.set_frame(frame_size, 8, vec![1; 16]).unwrap();
++ assert_eq!(draw(&mut cx), 2);
++ assert_eq!(atlas.stream_uploads(), 3);
++ assert!(atlas.removed_streams().is_empty());
++
++ second.clear();
++ assert_eq!(draw(&mut cx), 1);
++ assert_eq!(atlas.removed_streams(), vec![second.id()]);
++
++ window
++ .update(&mut cx, |view, _, _| view.images.clear())
++ .unwrap();
++ let first_id = first.id();
++ drop(first);
++ assert_eq!(draw(&mut cx), 0);
++ assert_eq!(atlas.removed_streams(), vec![second.id(), first_id]);
++ assert_eq!(draw(&mut cx), 0);
++ assert_eq!(atlas.removed_streams().len(), 2);
++ }
++
+ /// When a focus listener moves focus again (e.g. a dock forwarding focus to its
+ /// active panel), the resulting focus events must be dispatched without waiting
+ /// for an unrelated redraw of the window.
+diff --git a/crates/gpui_linux/src/linux/headless/window.rs b/crates/gpui_linux/src/linux/headless/window.rs
+index e41b097..dac72f9 100644
+--- a/crates/gpui_linux/src/linux/headless/window.rs
++++ b/crates/gpui_linux/src/linux/headless/window.rs
+@@ -18,8 +18,8 @@ use gpui::{
+ AtlasKey, AtlasTextureId, AtlasTile, Bounds, Capslock, DevicePixels, DispatchEventResult,
+ DisplayId, GpuSpecs, Modifiers, Pixels, PlatformAtlas, PlatformDisplay, PlatformInput,
+ PlatformInputHandler, PlatformWindow, Point, PromptButton, PromptLevel, RequestFrameOptions,
+- Scene, Size, TileId, WindowAppearance, WindowBackgroundAppearance, WindowBounds,
+- WindowControlArea, WindowParams, px,
++ Scene, Size, StreamFrame, StreamingImageId, TileId, WindowAppearance,
++ WindowBackgroundAppearance, WindowBounds, WindowControlArea, WindowParams, px,
+ };
+
+ #[derive(Debug)]
+@@ -239,6 +239,7 @@ struct HeadlessAtlas(Mutex<HeadlessAtlasState>);
+ struct HeadlessAtlasState {
+ next_id: u32,
+ tiles: HashMap<AtlasKey, AtlasTile>,
++ streams: HashMap<StreamingImageId, AtlasTile>,
+ }
+
+ impl PlatformAtlas for HeadlessAtlas {
+@@ -284,4 +285,25 @@ impl PlatformAtlas for HeadlessAtlas {
+ fn remove(&self, key: &AtlasKey) {
+ self.0.lock().tiles.remove(key);
+ }
++
++ fn get_or_update_stream(
++ &self,
++ id: StreamingImageId,
++ frame: &StreamFrame,
++ ) -> anyhow::Result<Option<AtlasTile>> {
++ let mut state = self.0.lock();
++ if let Some(tile) = state.streams.get(&id)
++ && tile.bounds.size == frame.size()
++ {
++ return Ok(Some(*tile));
++ }
++ state.next_id += 1;
++ let tile = AtlasTile::for_stream(state.next_id, frame.size());
++ state.streams.insert(id, tile);
++ Ok(Some(tile))
++ }
++
++ fn remove_stream(&self, id: StreamingImageId) {
++ self.0.lock().streams.remove(&id);
++ }
+ }
+diff --git a/crates/gpui_macos/src/metal_atlas.rs b/crates/gpui_macos/src/metal_atlas.rs
+index d007abb..65f38d5 100644
+--- a/crates/gpui_macos/src/metal_atlas.rs
++++ b/crates/gpui_macos/src/metal_atlas.rs
+@@ -4,11 +4,21 @@ use derive_more::{Deref, DerefMut};
+ use etagere::BucketedAtlasAllocator;
+ use gpui::{
+ AtlasKey, AtlasTextureId, AtlasTextureKind, AtlasTextureList, AtlasTile, Bounds, DevicePixels,
+- PlatformAtlas, Point, Size,
++ PlatformAtlas, Point, Size, StreamFrame, StreamingImageFormat, StreamingImageId,
+ };
+ use metal::Device;
+ use parking_lot::Mutex;
+-use std::borrow::Cow;
++use std::{
++ borrow::Cow,
++ sync::{
++ Arc,
++ atomic::{AtomicU32, Ordering},
++ },
++};
++
++const MAX_STREAM_TEXTURE_SIZE: i32 = 16384;
++const SINGLE_SLOT_STREAM_BYTES: u64 = 32 * 1024 * 1024;
++const SPARE_SLOT_RELEASE_UPLOADS: u32 = 120;
+
+ pub(crate) struct MetalAtlas(Mutex<MetalAtlasState>);
+
+@@ -20,9 +30,44 @@ impl MetalAtlas {
+ monochrome_textures: Default::default(),
+ polychrome_textures: Default::default(),
+ tiles_by_key: Default::default(),
++ streams: Vec::new(),
++ next_stream_index: 0,
++ stream_textures_created: 0,
+ }))
+ }
+
++ /// The texture a stream sprite batch should sample, plus a lease that keeps
++ /// later uploads out of that texture until the GPU is done with it. `None`
++ /// once the stream has been removed.
++ pub(crate) fn lease_stream_texture(
++ &self,
++ id: AtlasTextureId,
++ ) -> Option<(metal::Texture, MetalStreamLease)> {
++ let lock = self.0.lock();
++ let stream = lock
++ .streams
++ .iter()
++ .find(|stream| stream.index == id.index)?;
++ let slot = stream.slots.get(stream.front)?;
++ Some((
++ slot.texture.0.clone(),
++ MetalStreamLease::new(&slot.in_flight),
++ ))
++ }
++
++ /// (live stream textures, their total bytes, stream textures created so
++ /// far) for COMET_GPU_STATS.
++ pub(crate) fn stream_stats(&self) -> (usize, u64, u64) {
++ let lock = self.0.lock();
++ let mut count = 0;
++ let mut bytes = 0;
++ for slot in lock.streams.iter().flat_map(|stream| &stream.slots) {
++ count += 1;
++ bytes += slot.texture.width() * slot.texture.height() * 4;
++ }
++ (count, bytes, lock.stream_textures_created)
++ }
++
+ pub(crate) fn metal_texture(&self, id: AtlasTextureId) -> metal::Texture {
+ self.0.lock().texture(id).metal_texture.clone()
+ }
+@@ -54,6 +99,9 @@ struct MetalAtlasState {
+ monochrome_textures: AtlasTextureList<MetalAtlasTexture>,
+ polychrome_textures: AtlasTextureList<MetalAtlasTexture>,
+ tiles_by_key: FxHashMap<AtlasKey, AtlasTile>,
++ streams: Vec<MetalAtlasStream>,
++ next_stream_index: u32,
++ stream_textures_created: u64,
+ }
+
+ impl PlatformAtlas for MetalAtlas {
+@@ -89,7 +137,7 @@ impl PlatformAtlas for MetalAtlas {
+ let textures = match id.kind {
+ AtlasTextureKind::Monochrome => &mut lock.monochrome_textures,
+ AtlasTextureKind::Polychrome => &mut lock.polychrome_textures,
+- AtlasTextureKind::Subpixel => unreachable!(),
++ AtlasTextureKind::Subpixel | AtlasTextureKind::Stream => unreachable!(),
+ };
+
+ let Some(texture_slot) = textures
+@@ -110,6 +158,18 @@ impl PlatformAtlas for MetalAtlas {
+ }
+ }
+ }
++
++ fn get_or_update_stream(
++ &self,
++ id: StreamingImageId,
++ frame: &StreamFrame,
++ ) -> Result<Option<AtlasTile>> {
++ self.0.lock().update_stream(id, frame).map(Some)
++ }
++
++ fn remove_stream(&self, id: StreamingImageId) {
++ self.0.lock().streams.retain(|stream| stream.id != id);
++ }
+ }
+
+ impl MetalAtlasState {
+@@ -122,7 +182,7 @@ impl MetalAtlasState {
+ let textures = match texture_kind {
+ AtlasTextureKind::Monochrome => &mut self.monochrome_textures,
+ AtlasTextureKind::Polychrome => &mut self.polychrome_textures,
+- AtlasTextureKind::Subpixel => unreachable!(),
++ AtlasTextureKind::Subpixel | AtlasTextureKind::Stream => unreachable!(),
+ };
+
+ if let Some(tile) = textures
+@@ -167,7 +227,7 @@ impl MetalAtlasState {
+ pixel_format = metal::MTLPixelFormat::BGRA8Unorm;
+ usage = metal::MTLTextureUsage::ShaderRead;
+ }
+- AtlasTextureKind::Subpixel => unreachable!(),
++ AtlasTextureKind::Subpixel | AtlasTextureKind::Stream => unreachable!(),
+ }
+ texture_descriptor.set_pixel_format(pixel_format);
+ texture_descriptor.set_usage(usage);
+@@ -183,7 +243,7 @@ impl MetalAtlasState {
+ let texture_list = match kind {
+ AtlasTextureKind::Monochrome => &mut self.monochrome_textures,
+ AtlasTextureKind::Polychrome => &mut self.polychrome_textures,
+- AtlasTextureKind::Subpixel => unreachable!(),
++ AtlasTextureKind::Subpixel | AtlasTextureKind::Stream => unreachable!(),
+ };
+
+ let index = texture_list.free_list.pop();
+@@ -214,10 +274,173 @@ impl MetalAtlasState {
+ let textures = match id.kind {
+ AtlasTextureKind::Monochrome => &self.monochrome_textures,
+ AtlasTextureKind::Polychrome => &self.polychrome_textures,
+- AtlasTextureKind::Subpixel => unreachable!(),
++ AtlasTextureKind::Subpixel | AtlasTextureKind::Stream => unreachable!(),
+ };
+ textures[id.index as usize].as_ref().unwrap()
+ }
++
++ fn update_stream(&mut self, id: StreamingImageId, frame: &StreamFrame) -> Result<AtlasTile> {
++ let size = frame.size();
++ anyhow::ensure!(
++ size.width.0 <= MAX_STREAM_TEXTURE_SIZE && size.height.0 <= MAX_STREAM_TEXTURE_SIZE,
++ "streaming image frame {}x{} exceeds the {MAX_STREAM_TEXTURE_SIZE} pixel texture limit",
++ size.width.0,
++ size.height.0
++ );
++ let pixel_format = match frame.format() {
++ StreamingImageFormat::Bgra8 => metal::MTLPixelFormat::BGRA8Unorm,
++ StreamingImageFormat::Rgba8 => metal::MTLPixelFormat::RGBA8Unorm,
++ };
++ let position = match self.streams.iter().position(|stream| stream.id == id) {
++ Some(position) => {
++ let stream = &mut self.streams[position];
++ if stream.size != size || stream.pixel_format != pixel_format {
++ *stream = MetalAtlasStream::new(id, stream.index, size, pixel_format);
++ }
++ position
++ }
++ None => {
++ let index = self.next_stream_index;
++ self.next_stream_index = self.next_stream_index.wrapping_add(1);
++ self.streams
++ .push(MetalAtlasStream::new(id, index, size, pixel_format));
++ self.streams.len() - 1
++ }
++ };
++ let stream = &mut self.streams[position];
++ if stream.slots.is_empty() || stream.generation != frame.generation() {
++ let created = stream.upload(&self.device, self.is_apple_gpu, frame);
++ self.stream_textures_created += created as u64;
++ }
++ Ok(AtlasTile::for_stream(stream.index, size))
++ }
++}
++
++/// A streaming image's textures. Frames go into a slot the GPU is not reading;
++/// a second slot exists only while uploads keep finding the current one busy.
++struct MetalAtlasStream {
++ id: StreamingImageId,
++ index: u32,
++ size: Size<DevicePixels>,
++ pixel_format: metal::MTLPixelFormat,
++ generation: u64,
++ slots: Vec<MetalStreamSlot>,
++ front: usize,
++ uncontended_uploads: u32,
++}
++
++struct MetalStreamSlot {
++ texture: AssertSend<metal::Texture>,
++ in_flight: Arc<AtomicU32>,
++}
++
++/// Marks a stream slot as read by a command buffer until dropped.
++pub(crate) struct MetalStreamLease(Arc<AtomicU32>);
++
++impl MetalStreamLease {
++ fn new(in_flight: &Arc<AtomicU32>) -> Self {
++ in_flight.fetch_add(1, Ordering::AcqRel);
++ Self(in_flight.clone())
++ }
++}
++
++impl Drop for MetalStreamLease {
++ fn drop(&mut self) {
++ self.0.fetch_sub(1, Ordering::AcqRel);
++ }
++}
++
++impl MetalAtlasStream {
++ fn new(
++ id: StreamingImageId,
++ index: u32,
++ size: Size<DevicePixels>,
++ pixel_format: metal::MTLPixelFormat,
++ ) -> Self {
++ Self {
++ id,
++ index,
++ size,
++ pixel_format,
++ generation: 0,
++ slots: Vec::new(),
++ front: 0,
++ uncontended_uploads: 0,
++ }
++ }
++
++ /// Writes `frame` into a slot no command buffer is reading and makes it
++ /// the front slot, without ever waiting on the GPU. Returns how many
++ /// textures it created.
++ fn upload(
++ &mut self,
++ device: &metal::DeviceRef,
++ is_apple_gpu: bool,
++ frame: &StreamFrame,
++ ) -> u32 {
++ let frame_bytes = self.size.width.0 as u64 * self.size.height.0 as u64 * 4;
++ let max_slots = if frame_bytes > SINGLE_SLOT_STREAM_BYTES {
++ 1
++ } else {
++ 2
++ };
++ let is_free = |slot: &MetalStreamSlot| slot.in_flight.load(Ordering::Acquire) == 0;
++ let mut created = 0;
++ let target = if self.slots.get(self.front).is_some_and(is_free) {
++ self.uncontended_uploads += 1;
++ self.front
++ } else {
++ self.uncontended_uploads = 0;
++ if let Some(index) = self.slots.iter().position(is_free) {
++ index
++ } else if self.slots.len() < max_slots {
++ created += 1;
++ self.slots.push(self.new_slot(device, is_apple_gpu));
++ self.slots.len() - 1
++ } else {
++ created += 1;
++ let index = (self.front + 1) % self.slots.len();
++ self.slots[index] = self.new_slot(device, is_apple_gpu);
++ index
++ }
++ };
++
++ let region =
++ metal::MTLRegion::new_2d(0, 0, self.size.width.0 as u64, self.size.height.0 as u64);
++ self.slots[target].texture.replace_region(
++ region,
++ 0,
++ frame.bytes().as_ptr() as *const _,
++ frame.stride() as u64,
++ );
++ self.front = target;
++ self.generation = frame.generation();
++
++ if self.uncontended_uploads >= SPARE_SLOT_RELEASE_UPLOADS && self.slots.len() > 1 {
++ self.slots.swap(0, self.front);
++ self.slots.truncate(1);
++ self.front = 0;
++ self.uncontended_uploads = 0;
++ }
++ created
++ }
++
++ fn new_slot(&self, device: &metal::DeviceRef, is_apple_gpu: bool) -> MetalStreamSlot {
++ let descriptor = metal::TextureDescriptor::new();
++ descriptor.set_width(self.size.width.0 as u64);
++ descriptor.set_height(self.size.height.0 as u64);
++ descriptor.set_pixel_format(self.pixel_format);
++ descriptor.set_usage(metal::MTLTextureUsage::ShaderRead);
++ descriptor.set_storage_mode(if is_apple_gpu {
++ metal::MTLStorageMode::Shared
++ } else {
++ metal::MTLStorageMode::Managed
++ });
++ MetalStreamSlot {
++ texture: AssertSend(device.new_texture(&descriptor)),
++ in_flight: Arc::default(),
++ }
++ }
+ }
+
+ struct MetalAtlasTexture {
+@@ -295,11 +518,20 @@ unsafe impl<T> Send for AssertSend<T> {}
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+- use gpui::PlatformAtlas;
++ use gpui::{PlatformAtlas, StreamingImage};
+ use std::borrow::Cow;
+
++ fn system_device() -> Option<metal::Device> {
++ let device = metal::Device::system_default();
++ assert!(
++ device.is_some() || std::env::var_os("GPUI_REQUIRE_TEST_DEVICE").is_none(),
++ "GPUI_REQUIRE_TEST_DEVICE is set but no Metal device is available"
++ );
++ device
++ }
++
+ fn create_atlas() -> Option<MetalAtlas> {
+- let device = metal::Device::system_default()?;
++ let device = system_device()?;
+ Some(MetalAtlas::new(device, true))
+ }
+
+@@ -396,4 +628,365 @@ mod tests {
+ let key = make_image_key(999, 0);
+ atlas.remove(&key);
+ }
++ fn stream_size(width: i32, height: i32) -> Size<DevicePixels> {
++ Size {
++ width: DevicePixels(width),
++ height: DevicePixels(height),
++ }
++ }
++
++ fn set_stream_frame(
++ image: &StreamingImage,
++ size: Size<DevicePixels>,
++ value: u8,
++ ) -> StreamFrame {
++ let stride = size.width.0 as usize * 4;
++ image
++ .set_frame(size, stride, vec![value; stride * size.height.0 as usize])
++ .unwrap();
++ image.current().unwrap()
++ }
++
++ fn update_stream(atlas: &MetalAtlas, image: &StreamingImage, frame: &StreamFrame) -> AtlasTile {
++ atlas
++ .get_or_update_stream(image.id(), frame)
++ .expect("stream upload should succeed")
++ .expect("stream upload returns a tile")
++ }
++
++ fn stream_slot_textures(atlas: &MetalAtlas, image: &StreamingImage) -> Vec<metal::Texture> {
++ let lock = atlas.0.lock();
++ let stream = lock
++ .streams
++ .iter()
++ .find(|stream| stream.id == image.id())
++ .expect("stream should exist");
++ stream
++ .slots
++ .iter()
++ .map(|slot| slot.texture.0.clone())
++ .collect()
++ }
++
++ fn texture_ptrs(textures: &[metal::Texture]) -> Vec<*mut metal::MTLTexture> {
++ use metal::foreign_types::ForeignType as _;
++ textures.iter().map(|texture| texture.as_ptr()).collect()
++ }
++
++ fn stream_textures_created(atlas: &MetalAtlas) -> u64 {
++ atlas.0.lock().stream_textures_created
++ }
++
++ fn read_texture(device: &metal::DeviceRef, texture: &metal::TextureRef) -> Vec<u8> {
++ let queue = device.new_command_queue();
++ let command_buffer = queue.new_command_buffer();
++ let buffer = read_back_into_new_buffer(device, command_buffer, texture);
++ command_buffer.commit();
++ command_buffer.wait_until_completed();
++ buffer_bytes(&buffer)
++ }
++
++ fn read_back_into_new_buffer(
++ device: &metal::DeviceRef,
++ command_buffer: &metal::CommandBufferRef,
++ texture: &metal::TextureRef,
++ ) -> metal::Buffer {
++ let row_bytes = texture.width() * 4;
++ let len = row_bytes * texture.height();
++ let buffer = device.new_buffer(len, metal::MTLResourceOptions::StorageModeShared);
++ let blit = command_buffer.new_blit_command_encoder();
++ blit.copy_from_texture_to_buffer(
++ texture,
++ 0,
++ 0,
++ metal::MTLOrigin { x: 0, y: 0, z: 0 },
++ metal::MTLSize {
++ width: texture.width(),
++ height: texture.height(),
++ depth: 1,
++ },
++ &buffer,
++ 0,
++ row_bytes,
++ len,
++ metal::MTLBlitOption::None,
++ );
++ blit.end_encoding();
++ buffer
++ }
++
++ fn buffer_bytes(buffer: &metal::BufferRef) -> Vec<u8> {
++ unsafe {
++ std::slice::from_raw_parts(buffer.contents() as *const u8, buffer.length() as usize)
++ .to_vec()
++ }
++ }
++
++ #[test]
++ fn stream_updates_of_the_same_size_reuse_one_texture() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(96, 54);
++
++ let first = update_stream(&atlas, &image, &set_stream_frame(&image, size, 1));
++ let textures = stream_slot_textures(&atlas, &image);
++ for value in 2..10 {
++ let tile = update_stream(&atlas, &image, &set_stream_frame(&image, size, value));
++ assert_eq!(tile, first);
++ }
++
++ assert_eq!(first.texture_id.kind, AtlasTextureKind::Stream);
++ assert_eq!(first.bounds.size, size);
++ assert_eq!(
++ texture_ptrs(&stream_slot_textures(&atlas, &image)),
++ texture_ptrs(&textures)
++ );
++ assert_eq!(textures.len(), 1);
++ assert_eq!(stream_textures_created(&atlas), 1);
++ let (texture, _lease) = atlas.lease_stream_texture(first.texture_id).unwrap();
++ assert!(
++ read_texture(&atlas.0.lock().device, &texture)
++ .iter()
++ .all(|&byte| byte == 9)
++ );
++ }
++
++ #[test]
++ fn stream_skips_upload_when_the_generation_is_unchanged() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(8, 8);
++ let frame = set_stream_frame(&image, size, 1);
++ let tile = update_stream(&atlas, &image, &frame);
++ let (texture, _lease) = atlas.lease_stream_texture(tile.texture_id).unwrap();
++ texture.replace_region(
++ metal::MTLRegion::new_2d(0, 0, 8, 8),
++ 0,
++ vec![5u8; 8 * 8 * 4].as_ptr() as *const _,
++ 32,
++ );
++
++ assert_eq!(update_stream(&atlas, &image, &frame), tile);
++ let device = atlas.0.lock().device.0.clone();
++ assert!(
++ read_texture(&device, &texture)
++ .iter()
++ .all(|&byte| byte == 5)
++ );
++ }
++
++ #[test]
++ fn stream_size_change_reallocates_once() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let small = update_stream(
++ &atlas,
++ &image,
++ &set_stream_frame(&image, stream_size(64, 36), 1),
++ );
++ let small_textures = stream_slot_textures(&atlas, &image);
++
++ let large_size = stream_size(128, 72);
++ let large = update_stream(&atlas, &image, &set_stream_frame(&image, large_size, 2));
++ let large_textures = stream_slot_textures(&atlas, &image);
++ update_stream(&atlas, &image, &set_stream_frame(&image, large_size, 3));
++
++ assert_eq!(large.texture_id, small.texture_id);
++ assert_eq!(large.bounds.size, large_size);
++ assert_ne!(texture_ptrs(&large_textures), texture_ptrs(&small_textures));
++ assert_eq!(
++ texture_ptrs(&stream_slot_textures(&atlas, &image)),
++ texture_ptrs(&large_textures)
++ );
++ assert_eq!(stream_textures_created(&atlas), 2);
++ }
++
++ #[test]
++ fn stream_format_change_reallocates() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let size = stream_size(16, 16);
++ let bgra = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let rgba = StreamingImage::new(StreamingImageFormat::Rgba8);
++ let bgra_tile = update_stream(&atlas, &bgra, &set_stream_frame(&bgra, size, 1));
++ let rgba_tile = update_stream(&atlas, &rgba, &set_stream_frame(&rgba, size, 1));
++
++ let (bgra_texture, _) = atlas.lease_stream_texture(bgra_tile.texture_id).unwrap();
++ let (rgba_texture, _) = atlas.lease_stream_texture(rgba_tile.texture_id).unwrap();
++ assert_ne!(bgra_tile.texture_id, rgba_tile.texture_id);
++ assert_eq!(
++ bgra_texture.pixel_format(),
++ metal::MTLPixelFormat::BGRA8Unorm
++ );
++ assert_eq!(
++ rgba_texture.pixel_format(),
++ metal::MTLPixelFormat::RGBA8Unorm
++ );
++ }
++
++ #[test]
++ fn images_and_streams_never_share_textures() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let small = stream_size(64, 64);
++ let image_key = make_image_key(1, 0);
++ let image_tile = insert_tile(&atlas, &image_key, small);
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let stream_tile = update_stream(&atlas, &image, &set_stream_frame(&image, small, 1));
++ let second_image_tile = insert_tile(&atlas, &make_image_key(2, 0), small);
++
++ assert_eq!(image_tile.texture_id.kind, AtlasTextureKind::Polychrome);
++ assert_eq!(second_image_tile.texture_id, image_tile.texture_id);
++ assert_eq!(stream_tile.texture_id.kind, AtlasTextureKind::Stream);
++ assert_eq!(
++ atlas
++ .0
++ .lock()
++ .polychrome_textures
++ .textures
++ .iter()
++ .flatten()
++ .count(),
++ 1
++ );
++
++ atlas.remove(&image_key);
++ assert!(atlas.lease_stream_texture(stream_tile.texture_id).is_some());
++ atlas.remove_stream(image.id());
++ assert!(atlas.lease_stream_texture(stream_tile.texture_id).is_none());
++ let _texture = atlas.metal_texture(second_image_tile.texture_id);
++ atlas.remove_stream(image.id());
++ }
++
++ #[test]
++ fn removed_streams_upload_again_with_a_fresh_texture_index() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let frame = set_stream_frame(&image, stream_size(8, 8), 1);
++ let first = update_stream(&atlas, &image, &frame);
++ atlas.remove_stream(image.id());
++ let second = update_stream(&atlas, &image, &frame);
++
++ assert_ne!(first.texture_id, second.texture_id);
++ assert!(atlas.lease_stream_texture(first.texture_id).is_none());
++ assert!(atlas.lease_stream_texture(second.texture_id).is_some());
++ assert_eq!(stream_textures_created(&atlas), 2);
++ }
++
++ #[test]
++ fn oversized_stream_frames_are_rejected() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(MAX_STREAM_TEXTURE_SIZE + 1, 1);
++ let stride = size.width.0 as usize * 4;
++ image.set_frame(size, stride, vec![0; stride]).unwrap();
++ assert!(
++ atlas
++ .get_or_update_stream(image.id(), &image.current().unwrap())
++ .is_err()
++ );
++ }
++
++ fn assert_upload_waits_for_no_reader(is_apple_gpu: bool) {
++ let Some(device) = system_device() else {
++ return;
++ };
++ let atlas = MetalAtlas::new(device.clone(), is_apple_gpu);
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(64, 64);
++ let tile = update_stream(&atlas, &image, &set_stream_frame(&image, size, 1));
++
++ let queue = device.new_command_queue();
++ let command_buffer = queue.new_command_buffer();
++ let (texture, lease) = atlas.lease_stream_texture(tile.texture_id).unwrap();
++ let first_read = read_back_into_new_buffer(&device, command_buffer, &texture);
++
++ assert_eq!(
++ update_stream(&atlas, &image, &set_stream_frame(&image, size, 2)),
++ tile
++ );
++ command_buffer.commit();
++ command_buffer.wait_until_completed();
++ drop(lease);
++
++ assert!(buffer_bytes(&first_read).iter().all(|&byte| byte == 1));
++ assert_eq!(stream_slot_textures(&atlas, &image).len(), 2);
++ assert_eq!(stream_textures_created(&atlas), 2);
++ let (texture, _lease) = atlas.lease_stream_texture(tile.texture_id).unwrap();
++ assert!(
++ read_texture(&device, &texture)
++ .iter()
++ .all(|&byte| byte == 2)
++ );
++
++ update_stream(&atlas, &image, &set_stream_frame(&image, size, 3));
++ assert_eq!(stream_textures_created(&atlas), 2);
++ }
++
++ #[test]
++ fn stream_upload_skips_a_slot_an_uncommitted_command_buffer_reads() {
++ assert_upload_waits_for_no_reader(true);
++ }
++
++ #[test]
++ fn stream_upload_skips_a_slot_an_uncommitted_command_buffer_reads_managed() {
++ assert_upload_waits_for_no_reader(false);
++ }
++
++ #[test]
++ fn stream_releases_its_spare_slot_once_uploads_stop_contending() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(32, 32);
++ let tile = update_stream(&atlas, &image, &set_stream_frame(&image, size, 0));
++ let lease = atlas.lease_stream_texture(tile.texture_id).unwrap();
++ update_stream(&atlas, &image, &set_stream_frame(&image, size, 1));
++ drop(lease);
++ assert_eq!(stream_slot_textures(&atlas, &image).len(), 2);
++
++ for value in 0..SPARE_SLOT_RELEASE_UPLOADS {
++ update_stream(&atlas, &image, &set_stream_frame(&image, size, value as u8));
++ }
++ assert_eq!(stream_slot_textures(&atlas, &image).len(), 1);
++ let (texture, _lease) = atlas.lease_stream_texture(tile.texture_id).unwrap();
++ let expected = (SPARE_SLOT_RELEASE_UPLOADS - 1) as u8;
++ let device = atlas.0.lock().device.0.clone();
++ assert!(
++ read_texture(&device, &texture)
++ .iter()
++ .all(|&byte| byte == expected)
++ );
++ }
++
++ #[test]
++ fn large_streams_keep_a_single_slot_and_never_block() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(3840, 2304);
++ let tile = update_stream(&atlas, &image, &set_stream_frame(&image, size, 1));
++ let (held, lease) = atlas.lease_stream_texture(tile.texture_id).unwrap();
++
++ update_stream(&atlas, &image, &set_stream_frame(&image, size, 2));
++ assert_eq!(stream_slot_textures(&atlas, &image).len(), 1);
++ assert_eq!(stream_textures_created(&atlas), 2);
++ let device = atlas.0.lock().device.0.clone();
++ assert!(read_texture(&device, &held).iter().all(|&byte| byte == 1));
++ drop(lease);
++ }
+ }
+diff --git a/crates/gpui_macos/src/metal_renderer.rs b/crates/gpui_macos/src/metal_renderer.rs
+index 1d35af1..4be15aa 100644
+--- a/crates/gpui_macos/src/metal_renderer.rs
++++ b/crates/gpui_macos/src/metal_renderer.rs
+@@ -1,4 +1,4 @@
+-use crate::metal_atlas::MetalAtlas;
++use crate::metal_atlas::{MetalAtlas, MetalStreamLease};
+ use anyhow::Result;
+ use block::ConcreteBlock;
+ use cocoa::{
+@@ -7,9 +7,9 @@ use cocoa::{
+ quartzcore::AutoresizingMask,
+ };
+ use gpui::{
+- AtlasTextureId, BackdropBlur, Background, Bounds, ContentMask, Corners, DevicePixels,
+- DrawOrder, MonochromeSprite, PaintSurface, Path, Point, PolychromeSprite, PrimitiveBatch, Quad,
+- ScaledPixels, Scene, Shadow, Size, Underline, point, size,
++ AtlasTextureId, AtlasTextureKind, BackdropBlur, Background, Bounds, ContentMask, Corners,
++ DevicePixels, DrawOrder, MonochromeSprite, PaintSurface, Path, Point, PolychromeSprite,
++ PrimitiveBatch, Quad, ScaledPixels, Scene, Shadow, Size, Underline, point, size,
+ };
+ #[cfg(any(test, feature = "test-support"))]
+ use image::RgbaImage;
+@@ -928,6 +928,7 @@ impl MetalRenderer {
+ let alpha = if self.opaque { 1. } else { 0. };
+ let mut instance_offset = 0;
+ let mut retained_surface_textures = CommandBufferResources::default();
++ let mut retained_stream_leases = CommandBufferResources::default();
+
+ // Big scratch textures are only worth holding while they're in use:
+ // after a run of frames that don't need them, drop them and let the
+@@ -1141,6 +1142,7 @@ impl MetalRenderer {
+ &mut instance_offset,
+ viewport_size,
+ command_encoder,
++ &mut retained_stream_leases,
+ ),
+ PrimitiveBatch::Surfaces(range) => self.draw_surfaces(
+ &scene.surfaces[range],
+@@ -1181,6 +1183,7 @@ impl MetalRenderer {
+ self.maybe_log_gpu_stats();
+
+ retained_surface_textures.retain_until_completion(command_buffer);
++ retained_stream_leases.retain_until_completion(command_buffer);
+
+ Ok(command_buffer.to_owned())
+ }
+@@ -1217,6 +1220,8 @@ impl MetalRenderer {
+ 1
+ };
+ let (atlas_textures, atlas_bytes) = self.sprite_atlas.texture_stats();
++ let (stream_textures, stream_bytes, stream_textures_created) =
++ self.sprite_atlas.stream_stats();
+ let (pool_buffers, pool_buffer_size) = {
+ let pool = self.instance_buffer_pool.lock();
+ (pool.buffers.len(), pool.buffer_size)
+@@ -1224,7 +1229,7 @@ impl MetalRenderer {
+ // stderr, not `log`: consumers (comet) run tracing without a log
+ // bridge, and this line only exists when explicitly opted into.
+ eprintln!(
+- "[gpu-stats] device: {:.1} MB; atlas: {} textures, {:.1} MB; instance pool: {} × {:.1} MB; backdrop scratch: {:.1} MB; path intermediate: {:.1} MB",
++ "[gpu-stats] device: {:.1} MB; atlas: {} textures, {:.1} MB; instance pool: {} × {:.1} MB; backdrop scratch: {:.1} MB; path intermediate: {:.1} MB; streams: {} textures, {:.1} MB, {} created",
+ self.device.current_allocated_size() as f64 / (1024.0 * 1024.0),
+ atlas_textures,
+ atlas_bytes as f64 / (1024.0 * 1024.0),
+@@ -1232,6 +1237,9 @@ impl MetalRenderer {
+ pool_buffer_size as f64 / (1024.0 * 1024.0),
+ backdrop_bytes as f64 / (1024.0 * 1024.0),
+ path_bytes as f64 / (1024.0 * 1024.0),
++ stream_textures,
++ stream_bytes as f64 / (1024.0 * 1024.0),
++ stream_textures_created,
+ );
+ }
+
+@@ -1832,13 +1840,22 @@ impl MetalRenderer {
+ instance_offset: &mut usize,
+ viewport_size: Size<DevicePixels>,
+ command_encoder: &metal::RenderCommandEncoderRef,
++ retained_stream_leases: &mut CommandBufferResources<MetalStreamLease>,
+ ) -> bool {
+ if sprites.is_empty() {
+ return true;
+ }
+ align_offset(instance_offset);
+
+- let texture = self.sprite_atlas.metal_texture(texture_id);
++ let texture = if texture_id.kind == AtlasTextureKind::Stream {
++ let Some((texture, lease)) = self.sprite_atlas.lease_stream_texture(texture_id) else {
++ return true;
++ };
++ retained_stream_leases.push(lease);
++ texture
++ } else {
++ self.sprite_atlas.metal_texture(texture_id)
++ };
+ let texture_size = size(
+ DevicePixels(texture.width() as i32),
+ DevicePixels(texture.height() as i32),
+@@ -2583,6 +2600,7 @@ mod backdrop_blur_tests {
+ border_color: transparent_black(),
+ corner_radii: Corners::default(),
+ border_widths: Edges::default(),
++ fade: Default::default(),
+ });
+ }
+
+@@ -2748,3 +2766,150 @@ mod backdrop_blur_tests {
+ );
+ }
+ }
++
++#[cfg(test)]
++mod streaming_image_tests {
++ use super::*;
++ use gpui::{
++ AtlasKey, AtlasTile, ImageId, PlatformAtlas, RenderImageParams, StreamingImage,
++ StreamingImageFormat,
++ };
++ use image::RgbaImage;
++ use std::borrow::Cow;
++
++ const VIEW: i32 = 96;
++ const WIDTH: i32 = 40;
++ const HEIGHT: i32 = 30;
++
++ fn new_renderer() -> Option<MetalRenderer> {
++ if metal::Device::system_default().is_none() {
++ assert!(
++ std::env::var_os("GPUI_REQUIRE_TEST_DEVICE").is_none(),
++ "GPUI_REQUIRE_TEST_DEVICE is set but no Metal device is available"
++ );
++ return None;
++ }
++ Some(
++ MetalRenderer::new_headless(Arc::new(Mutex::new(InstanceBufferPool::default())))
++ .expect("failed to initialize headless renderer"),
++ )
++ }
++
++ fn bgra_pattern() -> Vec<u8> {
++ let mut seed = 0x2545_f491_u32;
++ let mut bytes = Vec::with_capacity((WIDTH * HEIGHT * 4) as usize);
++ for _ in 0..WIDTH * HEIGHT {
++ for _ in 0..3 {
++ seed ^= seed << 13;
++ seed ^= seed >> 17;
++ seed ^= seed << 5;
++ bytes.push(seed as u8);
++ }
++ bytes.push(255);
++ }
++ bytes
++ }
++
++ fn frame_size() -> Size<DevicePixels> {
++ size(DevicePixels(WIDTH), DevicePixels(HEIGHT))
++ }
++
++ fn render(renderer: &mut MetalRenderer, tile: Option<AtlasTile>) -> RgbaImage {
++ let viewport = Bounds {
++ origin: point(ScaledPixels(0.), ScaledPixels(0.)),
++ size: size(ScaledPixels(VIEW as f32), ScaledPixels(VIEW as f32)),
++ };
++ let mut scene = Scene::default();
++ if let Some(tile) = tile {
++ scene.insert_primitive(PolychromeSprite {
++ order: 0,
++ pad: 0,
++ grayscale: false.into(),
++ opacity: 1.,
++ bounds: Bounds {
++ origin: point(ScaledPixels(10.), ScaledPixels(12.)),
++ size: size(ScaledPixels(WIDTH as f32), ScaledPixels(HEIGHT as f32)),
++ },
++ content_mask: ContentMask { bounds: viewport },
++ corner_radii: Corners::default(),
++ fade: Default::default(),
++ tile,
++ });
++ }
++ scene.finish();
++ renderer
++ .render_scene_to_image(&scene, size(DevicePixels(VIEW), DevicePixels(VIEW)))
++ .expect("render_scene_to_image failed")
++ }
++
++ #[test]
++ fn stream_sprites_render_exactly_like_atlas_images() {
++ let Some(mut renderer) = new_renderer() else {
++ return;
++ };
++ let atlas = renderer.sprite_atlas().clone();
++ let bgra = bgra_pattern();
++ let image_tile = atlas
++ .get_or_insert_with(
++ &AtlasKey::Image(RenderImageParams {
++ image_id: ImageId(usize::MAX),
++ frame_index: 0,
++ }),
++ &mut || Ok(Some((frame_size(), Cow::Borrowed(bgra.as_slice())))),
++ )
++ .unwrap()
++ .unwrap();
++
++ let bgra_stream = StreamingImage::new(StreamingImageFormat::Bgra8);
++ bgra_stream
++ .set_frame(frame_size(), WIDTH as usize * 4, bgra.clone())
++ .unwrap();
++ let bgra_tile = atlas
++ .get_or_update_stream(bgra_stream.id(), &bgra_stream.current().unwrap())
++ .unwrap()
++ .unwrap();
++
++ let stride = WIDTH as usize * 4 + 64;
++ let mut rgba = vec![0x5a; stride * HEIGHT as usize];
++ for (row, source) in rgba
++ .chunks_exact_mut(stride)
++ .zip(bgra.chunks_exact(WIDTH as usize * 4))
++ {
++ for (pixel, source) in row.chunks_exact_mut(4).zip(source.chunks_exact(4)) {
++ pixel.copy_from_slice(&[source[2], source[1], source[0], source[3]]);
++ }
++ }
++ let rgba_stream = StreamingImage::new(StreamingImageFormat::Rgba8);
++ rgba_stream.set_frame(frame_size(), stride, rgba).unwrap();
++ let rgba_tile = atlas
++ .get_or_update_stream(rgba_stream.id(), &rgba_stream.current().unwrap())
++ .unwrap()
++ .unwrap();
++
++ let expected = render(&mut renderer, Some(image_tile));
++ let pixel = expected.get_pixel(10, 12);
++ assert_eq!(pixel.0, [bgra[2], bgra[1], bgra[0], 255]);
++ assert_ne!(expected, render(&mut renderer, None));
++ assert!(render(&mut renderer, Some(bgra_tile)) == expected);
++ assert!(render(&mut renderer, Some(rgba_tile)) == expected);
++ }
++
++ #[test]
++ fn removed_stream_batches_are_skipped() {
++ let Some(mut renderer) = new_renderer() else {
++ return;
++ };
++ let atlas = renderer.sprite_atlas().clone();
++ let stream = StreamingImage::new(StreamingImageFormat::Bgra8);
++ stream
++ .set_frame(frame_size(), WIDTH as usize * 4, bgra_pattern())
++ .unwrap();
++ let tile = atlas
++ .get_or_update_stream(stream.id(), &stream.current().unwrap())
++ .unwrap()
++ .unwrap();
++ atlas.remove_stream(stream.id());
++
++ assert!(render(&mut renderer, Some(tile)) == render(&mut renderer, None));
++ }
++}
+diff --git a/crates/gpui_wgpu/src/wgpu_atlas.rs b/crates/gpui_wgpu/src/wgpu_atlas.rs
+index 3cf71ac..84c0043 100644
+--- a/crates/gpui_wgpu/src/wgpu_atlas.rs
++++ b/crates/gpui_wgpu/src/wgpu_atlas.rs
+@@ -3,7 +3,7 @@ use collections::FxHashMap;
+ use etagere::{BucketedAtlasAllocator, size2};
+ use gpui::{
+ AtlasKey, AtlasTextureId, AtlasTextureKind, AtlasTextureList, AtlasTile, Bounds, DevicePixels,
+- PlatformAtlas, Point, Size,
++ PlatformAtlas, Point, Size, StreamFrame, StreamingImageFormat, StreamingImageId,
+ };
+ use parking_lot::Mutex;
+ use std::{borrow::Cow, ops, sync::Arc};
+@@ -23,6 +23,14 @@ fn etagere_point_to_device(point: etagere::Point) -> Point<DevicePixels> {
+
+ pub struct WgpuAtlas(Mutex<WgpuAtlasState>);
+
++#[cfg(not(test))]
++const STREAM_TEXTURE_USAGES: wgpu::TextureUsages =
++ wgpu::TextureUsages::TEXTURE_BINDING.union(wgpu::TextureUsages::COPY_DST);
++#[cfg(test)]
++const STREAM_TEXTURE_USAGES: wgpu::TextureUsages = wgpu::TextureUsages::TEXTURE_BINDING
++ .union(wgpu::TextureUsages::COPY_DST)
++ .union(wgpu::TextureUsages::COPY_SRC);
++
+ struct PendingUpload {
+ id: AtlasTextureId,
+ bounds: Bounds<DevicePixels>,
+@@ -37,6 +45,25 @@ struct WgpuAtlasState {
+ storage: WgpuAtlasStorage,
+ tiles_by_key: FxHashMap<AtlasKey, AtlasTile>,
+ pending_uploads: Vec<PendingUpload>,
++ streams: Vec<WgpuAtlasStream>,
++ next_stream_index: u32,
++}
++
++/// A streaming image's texture. The newest painted frame is held, not copied,
++/// and written straight from its buffer in [`WgpuAtlas::before_frame`], where
++/// atlas uploads happen; wgpu orders that write after earlier submissions that
++/// sample the texture.
++struct WgpuAtlasStream {
++ id: StreamingImageId,
++ index: u32,
++ size: Size<DevicePixels>,
++ format: wgpu::TextureFormat,
++ swizzle: bool,
++ generation: u64,
++ pending: Option<StreamFrame>,
++ texture: wgpu::Texture,
++ view: wgpu::TextureView,
++ scratch: Vec<u8>,
+ }
+
+ pub struct WgpuTextureInfo {
+@@ -58,6 +85,8 @@ impl WgpuAtlas {
+ storage: WgpuAtlasStorage::default(),
+ tiles_by_key: Default::default(),
+ pending_uploads: Vec::new(),
++ streams: Vec::new(),
++ next_stream_index: 0,
+ }))
+ }
+
+@@ -82,6 +111,19 @@ impl WgpuAtlas {
+ }
+ }
+
++ /// The view of a streaming image's texture, or `None` once the stream has
++ /// been removed.
++ pub fn get_stream_texture_info(&self, id: AtlasTextureId) -> Option<WgpuTextureInfo> {
++ let lock = self.0.lock();
++ let stream = lock
++ .streams
++ .iter()
++ .find(|stream| stream.index == id.index)?;
++ Some(WgpuTextureInfo {
++ view: stream.view.clone(),
++ })
++ }
++
+ /// Clears all cached textures and tiles, forcing them to be recreated.
+ /// Use this for incremental recovery when the device is still valid.
+ pub fn clear(&self) {
+@@ -89,6 +131,7 @@ impl WgpuAtlas {
+ lock.storage = WgpuAtlasStorage::default();
+ lock.tiles_by_key.clear();
+ lock.pending_uploads.clear();
++ lock.streams.clear();
+ }
+
+ /// Handles device lost by clearing all textures and cached tiles.
+@@ -101,6 +144,7 @@ impl WgpuAtlas {
+ lock.storage = WgpuAtlasStorage::default();
+ lock.tiles_by_key.clear();
+ lock.pending_uploads.clear();
++ lock.streams.clear();
+ }
+ }
+
+@@ -153,9 +197,108 @@ impl PlatformAtlas for WgpuAtlas {
+ }
+ }
+ }
++
++ fn get_or_update_stream(
++ &self,
++ id: StreamingImageId,
++ frame: &StreamFrame,
++ ) -> Result<Option<AtlasTile>> {
++ self.0.lock().update_stream(id, frame).map(Some)
++ }
++
++ fn remove_stream(&self, id: StreamingImageId) {
++ self.0.lock().streams.retain(|stream| stream.id != id);
++ }
+ }
+
+ impl WgpuAtlasState {
++ fn update_stream(&mut self, id: StreamingImageId, frame: &StreamFrame) -> Result<AtlasTile> {
++ let size = frame.size();
++ anyhow::ensure!(
++ size.width.0 as u32 <= self.max_texture_size
++ && size.height.0 as u32 <= self.max_texture_size,
++ "streaming image frame {}x{} exceeds the {} pixel texture limit",
++ size.width.0,
++ size.height.0,
++ self.max_texture_size
++ );
++ u32::try_from(frame.stride()).context("streaming image stride is too large")?;
++ let (format, swizzle) = match frame.format() {
++ StreamingImageFormat::Rgba8 => (wgpu::TextureFormat::Rgba8Unorm, false),
++ StreamingImageFormat::Bgra8
++ if self.color_texture_format == wgpu::TextureFormat::Bgra8Unorm =>
++ {
++ (wgpu::TextureFormat::Bgra8Unorm, false)
++ }
++ StreamingImageFormat::Bgra8 => (wgpu::TextureFormat::Rgba8Unorm, true),
++ };
++ let position = self.streams.iter().position(|stream| stream.id == id);
++ let position = match position {
++ Some(position)
++ if self.streams[position].size == size
++ && self.streams[position].format == format
++ && self.streams[position].swizzle == swizzle =>
++ {
++ position
++ }
++ Some(position) => {
++ let index = self.streams[position].index;
++ self.streams[position] = self.new_stream(id, index, size, format, swizzle);
++ position
++ }
++ None => {
++ let index = self.next_stream_index;
++ self.next_stream_index = self.next_stream_index.wrapping_add(1);
++ let stream = self.new_stream(id, index, size, format, swizzle);
++ self.streams.push(stream);
++ self.streams.len() - 1
++ }
++ };
++ let stream = &mut self.streams[position];
++ if stream.generation != frame.generation() {
++ stream.generation = frame.generation();
++ stream.pending = Some(frame.clone());
++ }
++ Ok(AtlasTile::for_stream(stream.index, size))
++ }
++
++ fn new_stream(
++ &self,
++ id: StreamingImageId,
++ index: u32,
++ size: Size<DevicePixels>,
++ format: wgpu::TextureFormat,
++ swizzle: bool,
++ ) -> WgpuAtlasStream {
++ let texture = self.device.create_texture(&wgpu::TextureDescriptor {
++ label: Some("streaming_image"),
++ size: wgpu::Extent3d {
++ width: size.width.0 as u32,
++ height: size.height.0 as u32,
++ depth_or_array_layers: 1,
++ },
++ mip_level_count: 1,
++ sample_count: 1,
++ dimension: wgpu::TextureDimension::D2,
++ format,
++ usage: STREAM_TEXTURE_USAGES,
++ view_formats: &[],
++ });
++ let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
++ WgpuAtlasStream {
++ id,
++ index,
++ size,
++ format,
++ swizzle,
++ generation: 0,
++ pending: None,
++ texture,
++ view,
++ scratch: Vec::new(),
++ }
++ }
++
+ fn allocate(
+ &mut self,
+ size: Size<DevicePixels>,
+@@ -196,6 +339,7 @@ impl WgpuAtlasState {
+ let format = match kind {
+ AtlasTextureKind::Monochrome => wgpu::TextureFormat::R8Unorm,
+ AtlasTextureKind::Subpixel | AtlasTextureKind::Polychrome => self.color_texture_format,
++ AtlasTextureKind::Stream => unreachable!(),
+ };
+
+ let texture = self.device.create_texture(&wgpu::TextureDescriptor {
+@@ -289,6 +433,11 @@ impl WgpuAtlasState {
+ },
+ );
+ }
++ for stream in &mut self.streams {
++ if let Some(frame) = stream.pending.take() {
++ stream.write(&self.queue, &frame);
++ }
++ }
+ }
+ }
+
+@@ -306,6 +455,7 @@ impl ops::Index<AtlasTextureKind> for WgpuAtlasStorage {
+ AtlasTextureKind::Monochrome => &self.monochrome_textures,
+ AtlasTextureKind::Subpixel => &self.subpixel_textures,
+ AtlasTextureKind::Polychrome => &self.polychrome_textures,
++ AtlasTextureKind::Stream => unreachable!(),
+ }
+ }
+ }
+@@ -316,6 +466,7 @@ impl ops::IndexMut<AtlasTextureKind> for WgpuAtlasStorage {
+ AtlasTextureKind::Monochrome => &mut self.monochrome_textures,
+ AtlasTextureKind::Subpixel => &mut self.subpixel_textures,
+ AtlasTextureKind::Polychrome => &mut self.polychrome_textures,
++ AtlasTextureKind::Stream => unreachable!(),
+ }
+ }
+ }
+@@ -336,6 +487,7 @@ impl ops::Index<AtlasTextureId> for WgpuAtlasStorage {
+ AtlasTextureKind::Monochrome => &self.monochrome_textures,
+ AtlasTextureKind::Subpixel => &self.subpixel_textures,
+ AtlasTextureKind::Polychrome => &self.polychrome_textures,
++ AtlasTextureKind::Stream => unreachable!(),
+ };
+ textures[id.index as usize]
+ .as_ref()
+@@ -343,6 +495,46 @@ impl ops::Index<AtlasTextureId> for WgpuAtlasStorage {
+ }
+ }
+
++impl WgpuAtlasStream {
++ fn write(&mut self, queue: &wgpu::Queue, frame: &StreamFrame) {
++ let width = self.size.width.0 as u32;
++ let height = self.size.height.0 as u32;
++ let (data, bytes_per_row) = if self.swizzle {
++ let row_bytes = width as usize * 4;
++ self.scratch.clear();
++ self.scratch.reserve(row_bytes * height as usize);
++ for row in frame.bytes().chunks(frame.stride()).take(height as usize) {
++ for pixel in row[..row_bytes].chunks_exact(4) {
++ self.scratch
++ .extend_from_slice(&[pixel[2], pixel[1], pixel[0], pixel[3]]);
++ }
++ }
++ (self.scratch.as_slice(), row_bytes as u32)
++ } else {
++ (frame.bytes(), frame.stride() as u32)
++ };
++ queue.write_texture(
++ wgpu::TexelCopyTextureInfo {
++ texture: &self.texture,
++ mip_level: 0,
++ origin: wgpu::Origin3d::ZERO,
++ aspect: wgpu::TextureAspect::All,
++ },
++ data,
++ wgpu::TexelCopyBufferLayout {
++ offset: 0,
++ bytes_per_row: Some(bytes_per_row),
++ rows_per_image: None,
++ },
++ wgpu::Extent3d {
++ width,
++ height,
++ depth_or_array_layers: 1,
++ },
++ );
++ }
++}
++
+ struct WgpuAtlasTexture {
+ id: AtlasTextureId,
+ allocator: BucketedAtlasAllocator,
+@@ -402,13 +594,13 @@ fn swizzle_upload_data(bytes: &[u8], format: wgpu::TextureFormat) -> Vec<u8> {
+ mod tests {
+ use super::*;
+ use gpui::block_on;
+- use gpui::{ImageId, RenderImageParams};
++ use gpui::{ImageId, RenderImageParams, StreamingImage};
+ use std::sync::Arc;
+
+ fn test_device_and_queue() -> anyhow::Result<(Arc<wgpu::Device>, Arc<wgpu::Queue>)> {
+ block_on(async {
+ let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
+- backends: wgpu::Backends::all(),
++ backends: wgpu::Backends::from_env().unwrap_or(wgpu::Backends::all()),
+ flags: wgpu::InstanceFlags::default(),
+ backend_options: wgpu::BackendOptions::default(),
+ memory_budget_thresholds: wgpu::MemoryBudgetThresholds::default(),
+@@ -422,6 +614,11 @@ mod tests {
+ })
+ .await
+ .map_err(|error| anyhow::anyhow!("failed to request adapter: {error}"))?;
++ let info = adapter.get_info();
++ eprintln!(
++ "wgpu test adapter: {} ({:?}, {:?})",
++ info.name, info.backend, info.device_type
++ );
+ let (device, queue) = adapter
+ .request_device(&wgpu::DeviceDescriptor {
+ label: Some("wgpu_atlas_test_device"),
+@@ -507,6 +704,432 @@ mod tests {
+ Ok(())
+ }
+
++ struct StreamTest {
++ device: Arc<wgpu::Device>,
++ queue: Arc<wgpu::Queue>,
++ atlas: WgpuAtlas,
++ }
++
++ fn stream_test(color_texture_format: wgpu::TextureFormat) -> Option<StreamTest> {
++ let (device, queue) = match test_device_and_queue() {
++ Ok(device_and_queue) => device_and_queue,
++ Err(error) => {
++ assert!(
++ std::env::var_os("GPUI_REQUIRE_TEST_DEVICE").is_none(),
++ "GPUI_REQUIRE_TEST_DEVICE is set but no wgpu device is available: {error}"
++ );
++ eprintln!("skipping: {error}");
++ return None;
++ }
++ };
++ let atlas = WgpuAtlas::new(device.clone(), queue.clone(), color_texture_format);
++ Some(StreamTest {
++ device,
++ queue,
++ atlas,
++ })
++ }
++
++ fn stream_size(width: i32, height: i32) -> Size<DevicePixels> {
++ Size {
++ width: DevicePixels(width),
++ height: DevicePixels(height),
++ }
++ }
++
++ fn pixels(size: Size<DevicePixels>, seed: u8) -> Vec<u8> {
++ (0..size.width.0 * size.height.0 * 4)
++ .map(|index| (index as u8).wrapping_mul(31).wrapping_add(seed))
++ .collect()
++ }
++
++ fn set_frame(image: &StreamingImage, size: Size<DevicePixels>, bytes: Vec<u8>) -> StreamFrame {
++ image
++ .set_frame(size, size.width.0 as usize * 4, bytes)
++ .unwrap();
++ image.current().unwrap()
++ }
++
++ fn update(atlas: &WgpuAtlas, image: &StreamingImage, frame: &StreamFrame) -> AtlasTile {
++ let tile = atlas
++ .get_or_update_stream(image.id(), frame)
++ .expect("stream upload should succeed")
++ .expect("stream upload returns a tile");
++ atlas.before_frame();
++ tile
++ }
++
++ fn stream_texture(atlas: &WgpuAtlas, image: &StreamingImage) -> wgpu::Texture {
++ atlas
++ .0
++ .lock()
++ .streams
++ .iter()
++ .find(|stream| stream.id == image.id())
++ .expect("stream should exist")
++ .texture
++ .clone()
++ }
++
++ fn copy_to_buffer(
++ test: &StreamTest,
++ texture: &wgpu::Texture,
++ bounds: Bounds<DevicePixels>,
++ ) -> wgpu::Buffer {
++ let bytes_per_row =
++ (bounds.size.width.0 as u32 * 4).next_multiple_of(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT);
++ let buffer = test.device.create_buffer(&wgpu::BufferDescriptor {
++ label: Some("stream_readback"),
++ size: (bytes_per_row * bounds.size.height.0 as u32) as u64,
++ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
++ mapped_at_creation: false,
++ });
++ let mut encoder = test
++ .device
++ .create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
++ encoder.copy_texture_to_buffer(
++ wgpu::TexelCopyTextureInfo {
++ texture,
++ mip_level: 0,
++ origin: wgpu::Origin3d {
++ x: bounds.origin.x.0 as u32,
++ y: bounds.origin.y.0 as u32,
++ z: 0,
++ },
++ aspect: wgpu::TextureAspect::All,
++ },
++ wgpu::TexelCopyBufferInfo {
++ buffer: &buffer,
++ layout: wgpu::TexelCopyBufferLayout {
++ offset: 0,
++ bytes_per_row: Some(bytes_per_row),
++ rows_per_image: None,
++ },
++ },
++ wgpu::Extent3d {
++ width: bounds.size.width.0 as u32,
++ height: bounds.size.height.0 as u32,
++ depth_or_array_layers: 1,
++ },
++ );
++ test.queue.submit(Some(encoder.finish()));
++ buffer
++ }
++
++ fn read_buffer(test: &StreamTest, buffer: &wgpu::Buffer, size: Size<DevicePixels>) -> Vec<u8> {
++ buffer.map_async(wgpu::MapMode::Read, .., |result| result.unwrap());
++ test.device
++ .poll(wgpu::PollType::wait_indefinitely())
++ .unwrap();
++ let row_bytes = size.width.0 as usize * 4;
++ let bytes_per_row = buffer.size() as usize / size.height.0 as usize;
++ let mapped = buffer.get_mapped_range(..);
++ let bytes = mapped
++ .chunks(bytes_per_row)
++ .flat_map(|row| row[..row_bytes].to_vec())
++ .collect();
++ drop(mapped);
++ buffer.unmap();
++ bytes
++ }
++
++ fn read_stream(test: &StreamTest, image: &StreamingImage, size: Size<DevicePixels>) -> Vec<u8> {
++ let texture = stream_texture(&test.atlas, image);
++ let bounds = Bounds {
++ origin: Point::default(),
++ size,
++ };
++ let buffer = copy_to_buffer(test, &texture, bounds);
++ read_buffer(test, &buffer, size)
++ }
++
++ fn swizzled(bytes: &[u8]) -> Vec<u8> {
++ bytes
++ .chunks_exact(4)
++ .flat_map(|pixel| [pixel[2], pixel[1], pixel[0], pixel[3]])
++ .collect()
++ }
++
++ #[test]
++ fn stream_updates_of_the_same_size_reuse_one_texture() {
++ let Some(test) = stream_test(wgpu::TextureFormat::Bgra8Unorm) else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(64, 36);
++ let first = update(
++ &test.atlas,
++ &image,
++ &set_frame(&image, size, pixels(size, 1)),
++ );
++ let texture = stream_texture(&test.atlas, &image);
++ for seed in 2..10 {
++ let tile = update(
++ &test.atlas,
++ &image,
++ &set_frame(&image, size, pixels(size, seed)),
++ );
++ assert_eq!(tile, first);
++ }
++
++ assert_eq!(first.texture_id.kind, AtlasTextureKind::Stream);
++ assert_eq!(first.bounds.size, size);
++ assert!(stream_texture(&test.atlas, &image) == texture);
++ assert_eq!(read_stream(&test, &image, size), pixels(size, 9));
++ }
++
++ #[test]
++ fn stream_size_change_reallocates_once() {
++ let Some(test) = stream_test(wgpu::TextureFormat::Bgra8Unorm) else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let small = stream_size(64, 36);
++ let large = stream_size(128, 72);
++ let small_tile = update(
++ &test.atlas,
++ &image,
++ &set_frame(&image, small, pixels(small, 1)),
++ );
++ let small_texture = stream_texture(&test.atlas, &image);
++ let large_tile = update(
++ &test.atlas,
++ &image,
++ &set_frame(&image, large, pixels(large, 2)),
++ );
++ let large_texture = stream_texture(&test.atlas, &image);
++ update(
++ &test.atlas,
++ &image,
++ &set_frame(&image, large, pixels(large, 3)),
++ );
++
++ assert_eq!(large_tile.texture_id, small_tile.texture_id);
++ assert_eq!(large_tile.bounds.size, large);
++ assert!(large_texture != small_texture);
++ assert!(stream_texture(&test.atlas, &image) == large_texture);
++ assert_eq!(read_stream(&test, &image, large), pixels(large, 3));
++ }
++
++ #[test]
++ fn images_and_streams_never_share_textures() {
++ let Some(test) = stream_test(wgpu::TextureFormat::Bgra8Unorm) else {
++ return;
++ };
++ let size = stream_size(64, 64);
++ let image_key = AtlasKey::Image(RenderImageParams {
++ image_id: ImageId(1),
++ frame_index: 0,
++ });
++ let image_tile = test
++ .atlas
++ .get_or_insert_with(&image_key, &mut || {
++ Ok(Some((size, Cow::Owned(pixels(size, 1)))))
++ })
++ .unwrap()
++ .unwrap();
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let stream_tile = update(
++ &test.atlas,
++ &image,
++ &set_frame(&image, size, pixels(size, 2)),
++ );
++
++ assert_eq!(image_tile.texture_id.kind, AtlasTextureKind::Polychrome);
++ assert_eq!(stream_tile.texture_id.kind, AtlasTextureKind::Stream);
++ assert_eq!(
++ test.atlas
++ .0
++ .lock()
++ .storage
++ .polychrome_textures
++ .iter_mut()
++ .count(),
++ 1
++ );
++
++ test.atlas.remove(&image_key);
++ test.atlas.before_frame();
++ assert!(
++ test.atlas
++ .get_stream_texture_info(stream_tile.texture_id)
++ .is_some()
++ );
++ test.atlas.remove_stream(image.id());
++ assert!(
++ test.atlas
++ .get_stream_texture_info(stream_tile.texture_id)
++ .is_none()
++ );
++ test.atlas.remove_stream(image.id());
++ }
++
++ #[test]
++ fn stream_textures_hold_what_atlas_image_tiles_hold() {
++ for atlas_format in [
++ wgpu::TextureFormat::Bgra8Unorm,
++ wgpu::TextureFormat::Rgba8Unorm,
++ ] {
++ let Some(test) = stream_test(atlas_format) else {
++ return;
++ };
++ let size = stream_size(64, 32);
++ let bgra = pixels(size, 7);
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ update(&test.atlas, &image, &set_frame(&image, size, bgra.clone()));
++
++ assert_eq!(stream_texture(&test.atlas, &image).format(), atlas_format);
++ assert_eq!(
++ read_stream(&test, &image, size),
++ swizzle_upload_data(&bgra, atlas_format)
++ );
++ }
++ }
++
++ #[test]
++ fn stream_formats_and_strides_upload_the_same_pixels() {
++ for atlas_format in [
++ wgpu::TextureFormat::Bgra8Unorm,
++ wgpu::TextureFormat::Rgba8Unorm,
++ ] {
++ let Some(test) = stream_test(atlas_format) else {
++ return;
++ };
++ let size = stream_size(64, 16);
++ let bgra = pixels(size, 3);
++ let rgba = swizzled(&bgra);
++
++ let bgra_image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ update(
++ &test.atlas,
++ &bgra_image,
++ &set_frame(&bgra_image, size, bgra.clone()),
++ );
++ let stride = 64 * 4 + 32;
++ let mut padded = vec![0xee; stride * 16];
++ for (row, source) in padded
++ .chunks_exact_mut(stride)
++ .zip(rgba.chunks_exact(64 * 4))
++ {
++ row[..64 * 4].copy_from_slice(source);
++ }
++ let rgba_image = StreamingImage::new(StreamingImageFormat::Rgba8);
++ rgba_image.set_frame(size, stride, padded).unwrap();
++ update(&test.atlas, &rgba_image, &rgba_image.current().unwrap());
++
++ let bgra_texture = stream_texture(&test.atlas, &bgra_image);
++ let rgba_texture = stream_texture(&test.atlas, &rgba_image);
++ assert_eq!(rgba_texture.format(), wgpu::TextureFormat::Rgba8Unorm);
++ let as_rgba = |texture: &wgpu::Texture, bytes: Vec<u8>| match texture.format() {
++ wgpu::TextureFormat::Bgra8Unorm => swizzled(&bytes),
++ _ => bytes,
++ };
++ assert_eq!(
++ as_rgba(&bgra_texture, read_stream(&test, &bgra_image, size)),
++ rgba
++ );
++ assert_eq!(read_stream(&test, &rgba_image, size), rgba);
++ }
++ }
++
++ #[test]
++ fn stream_writes_are_ordered_with_submitted_reads() {
++ let Some(test) = stream_test(wgpu::TextureFormat::Bgra8Unorm) else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(64, 64);
++ let bounds = Bounds {
++ origin: Point::default(),
++ size,
++ };
++ update(
++ &test.atlas,
++ &image,
++ &set_frame(&image, size, pixels(size, 1)),
++ );
++ let texture = stream_texture(&test.atlas, &image);
++ let first = copy_to_buffer(&test, &texture, bounds);
++ update(
++ &test.atlas,
++ &image,
++ &set_frame(&image, size, pixels(size, 2)),
++ );
++ let second = copy_to_buffer(&test, &texture, bounds);
++
++ assert_eq!(read_buffer(&test, &first, size), pixels(size, 1));
++ assert_eq!(read_buffer(&test, &second, size), pixels(size, 2));
++ }
++
++ #[test]
++ fn stream_writes_wait_for_before_frame_and_keep_only_the_newest_frame() {
++ let Some(test) = stream_test(wgpu::TextureFormat::Bgra8Unorm) else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(64, 16);
++ let bounds = Bounds {
++ origin: Point::default(),
++ size,
++ };
++ update(
++ &test.atlas,
++ &image,
++ &set_frame(&image, size, pixels(size, 1)),
++ );
++ let texture = stream_texture(&test.atlas, &image);
++ for seed in 2..5 {
++ test.atlas
++ .get_or_update_stream(image.id(), &set_frame(&image, size, pixels(size, seed)))
++ .unwrap();
++ }
++ let before = copy_to_buffer(&test, &texture, bounds);
++ test.atlas.before_frame();
++ let after = copy_to_buffer(&test, &texture, bounds);
++
++ assert_eq!(read_buffer(&test, &before, size), pixels(size, 1));
++ assert_eq!(read_buffer(&test, &after, size), pixels(size, 4));
++ }
++
++ #[test]
++ fn cleared_atlas_uploads_streams_again() {
++ let Some(test) = stream_test(wgpu::TextureFormat::Bgra8Unorm) else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(64, 8);
++ let frame = set_frame(&image, size, pixels(size, 4));
++ let tile = update(&test.atlas, &image, &frame);
++ let texture = stream_texture(&test.atlas, &image);
++
++ test.atlas.clear();
++ assert!(
++ test.atlas
++ .get_stream_texture_info(tile.texture_id)
++ .is_none()
++ );
++ let tile = update(&test.atlas, &image, &frame);
++ assert!(
++ test.atlas
++ .get_stream_texture_info(tile.texture_id)
++ .is_some()
++ );
++ assert!(stream_texture(&test.atlas, &image) != texture);
++ assert_eq!(read_stream(&test, &image, size), pixels(size, 4));
++ }
++
++ #[test]
++ fn oversized_stream_frames_are_rejected() {
++ let Some(test) = stream_test(wgpu::TextureFormat::Bgra8Unorm) else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let width = test.atlas.0.lock().max_texture_size as i32 + 1;
++ let size = stream_size(width, 1);
++ let frame = set_frame(&image, size, vec![0; width as usize * 4]);
++ assert!(test.atlas.get_or_update_stream(image.id(), &frame).is_err());
++ }
++
+ #[test]
+ fn swizzle_upload_data_preserves_bgra_uploads() {
+ let input = vec![0x10, 0x20, 0x30, 0x40];
+diff --git a/crates/gpui_wgpu/src/wgpu_renderer.rs b/crates/gpui_wgpu/src/wgpu_renderer.rs
+index 729a5c4..2bfbc4e 100644
+--- a/crates/gpui_wgpu/src/wgpu_renderer.rs
++++ b/crates/gpui_wgpu/src/wgpu_renderer.rs
+@@ -2,9 +2,9 @@ use crate::surface_frame::SurfaceFrame;
+ use crate::{CompositorGpuHint, WgpuAtlas, WgpuContext};
+ use bytemuck::{Pod, Zeroable};
+ use gpui::{
+- AtlasTextureId, Background, Bounds, DevicePixels, GpuSpecs, MonochromeSprite, Path, Point,
+- PolychromeSprite, PrimitiveBatch, Quad, ScaledPixels, Scene, Shadow, Size, SubpixelSprite,
+- Underline, get_gamma_correction_ratios,
++ AtlasTextureId, AtlasTextureKind, Background, Bounds, DevicePixels, GpuSpecs, MonochromeSprite,
++ Path, Point, PolychromeSprite, PrimitiveBatch, Quad, ScaledPixels, Scene, Shadow, Size,
++ SubpixelSprite, Underline, get_gamma_correction_ratios,
+ };
+ use log::warn;
+ #[cfg(not(target_family = "wasm"))]
+@@ -1448,7 +1448,14 @@ impl WgpuRenderer {
+ instance_offset: &mut u64,
+ pass: &mut wgpu::RenderPass<'_>,
+ ) -> bool {
+- let tex_info = self.atlas.get_texture_info(texture_id);
++ let tex_info = if texture_id.kind == AtlasTextureKind::Stream {
++ let Some(tex_info) = self.atlas.get_stream_texture_info(texture_id) else {
++ return true;
++ };
++ tex_info
++ } else {
++ self.atlas.get_texture_info(texture_id)
++ };
+ let data = unsafe { Self::instance_bytes(sprites) };
+ self.draw_instances_with_texture(
+ data,
+diff --git a/crates/gpui_windows/src/directx_atlas.rs b/crates/gpui_windows/src/directx_atlas.rs
+index d49ec18..719fd3c 100644
+--- a/crates/gpui_windows/src/directx_atlas.rs
++++ b/crates/gpui_windows/src/directx_atlas.rs
+@@ -11,9 +11,11 @@ use windows::Win32::Graphics::{
+
+ use gpui::{
+ AtlasKey, AtlasTextureId, AtlasTextureKind, AtlasTextureList, AtlasTile, Bounds, DevicePixels,
+- PlatformAtlas, Point, Size,
++ PlatformAtlas, Point, Size, StreamFrame, StreamingImageFormat, StreamingImageId,
+ };
+
++const MAX_STREAM_TEXTURE_SIZE: i32 = 16384;
++
+ pub(crate) struct DirectXAtlas(Mutex<DirectXAtlasState>);
+
+ struct DirectXAtlasState {
+@@ -23,6 +25,20 @@ struct DirectXAtlasState {
+ polychrome_textures: AtlasTextureList<DirectXAtlasTexture>,
+ subpixel_textures: AtlasTextureList<DirectXAtlasTexture>,
+ tiles_by_key: FxHashMap<AtlasKey, AtlasTile>,
++ streams: Vec<DirectXAtlasStream>,
++ next_stream_index: u32,
++}
++
++/// A streaming image's texture, written with `UpdateSubresource`; the
++/// immediate context orders each write after earlier draws that sample it.
++struct DirectXAtlasStream {
++ id: StreamingImageId,
++ index: u32,
++ size: Size<DevicePixels>,
++ format: DXGI_FORMAT,
++ generation: u64,
++ texture: ID3D11Texture2D,
++ view: [Option<ID3D11ShaderResourceView>; 1],
+ }
+
+ struct DirectXAtlasTexture {
+@@ -43,9 +59,23 @@ impl DirectXAtlas {
+ polychrome_textures: Default::default(),
+ subpixel_textures: Default::default(),
+ tiles_by_key: Default::default(),
++ streams: Vec::new(),
++ next_stream_index: 0,
+ }))
+ }
+
++ pub(crate) fn get_stream_texture_view(
++ &self,
++ id: AtlasTextureId,
++ ) -> Option<[Option<ID3D11ShaderResourceView>; 1]> {
++ let lock = self.0.lock();
++ let stream = lock
++ .streams
++ .iter()
++ .find(|stream| stream.index == id.index)?;
++ Some(stream.view.clone())
++ }
++
+ pub(crate) fn get_texture_view(
+ &self,
+ id: AtlasTextureId,
+@@ -67,6 +97,7 @@ impl DirectXAtlas {
+ lock.polychrome_textures = AtlasTextureList::default();
+ lock.subpixel_textures = AtlasTextureList::default();
+ lock.tiles_by_key.clear();
++ lock.streams.clear();
+ }
+ }
+
+@@ -107,6 +138,7 @@ impl PlatformAtlas for DirectXAtlas {
+ AtlasTextureKind::Monochrome => &mut lock.monochrome_textures,
+ AtlasTextureKind::Polychrome => &mut lock.polychrome_textures,
+ AtlasTextureKind::Subpixel => &mut lock.subpixel_textures,
++ AtlasTextureKind::Stream => unreachable!(),
+ };
+ let cached_texture_index = if id.kind == AtlasTextureKind::Polychrome {
+ textures.textures.iter().position(|entry| {
+@@ -138,6 +170,18 @@ impl PlatformAtlas for DirectXAtlas {
+ }
+ }
+ }
++
++ fn get_or_update_stream(
++ &self,
++ id: StreamingImageId,
++ frame: &StreamFrame,
++ ) -> anyhow::Result<Option<AtlasTile>> {
++ self.0.lock().update_stream(id, frame)
++ }
++
++ fn remove_stream(&self, id: StreamingImageId) {
++ self.0.lock().streams.retain(|stream| stream.id != id);
++ }
+ }
+
+ impl DirectXAtlasState {
+@@ -151,6 +195,7 @@ impl DirectXAtlasState {
+ AtlasTextureKind::Monochrome => &mut self.monochrome_textures,
+ AtlasTextureKind::Polychrome => &mut self.polychrome_textures,
+ AtlasTextureKind::Subpixel => &mut self.subpixel_textures,
++ AtlasTextureKind::Stream => unreachable!(),
+ };
+
+ if let Some(tile) = textures
+@@ -201,6 +246,7 @@ impl DirectXAtlasState {
+ bind_flag = D3D11_BIND_SHADER_RESOURCE;
+ bytes_per_pixel = 4;
+ }
++ AtlasTextureKind::Stream => unreachable!(),
+ }
+ let texture_desc = D3D11_TEXTURE2D_DESC {
+ Width: size.width.0 as u32,
+@@ -231,6 +277,7 @@ impl DirectXAtlasState {
+ AtlasTextureKind::Monochrome => &mut self.monochrome_textures,
+ AtlasTextureKind::Polychrome => &mut self.polychrome_textures,
+ AtlasTextureKind::Subpixel => &mut self.subpixel_textures,
++ AtlasTextureKind::Stream => unreachable!(),
+ };
+ let index = texture_list.free_list.pop();
+ let view = unsafe {
+@@ -271,7 +318,113 @@ impl DirectXAtlasState {
+ AtlasTextureKind::Subpixel => {
+ &self.subpixel_textures[id.index as usize].as_ref().unwrap()
+ }
++ AtlasTextureKind::Stream => unreachable!(),
++ }
++ }
++
++ fn update_stream(
++ &mut self,
++ id: StreamingImageId,
++ frame: &StreamFrame,
++ ) -> anyhow::Result<Option<AtlasTile>> {
++ let size = frame.size();
++ anyhow::ensure!(
++ size.width.0 <= MAX_STREAM_TEXTURE_SIZE && size.height.0 <= MAX_STREAM_TEXTURE_SIZE,
++ "streaming image frame {}x{} exceeds the {MAX_STREAM_TEXTURE_SIZE} pixel texture limit",
++ size.width.0,
++ size.height.0
++ );
++ let stride = u32::try_from(frame.stride())
++ .map_err(|_| anyhow::anyhow!("streaming image stride is too large"))?;
++ let format = match frame.format() {
++ StreamingImageFormat::Bgra8 => DXGI_FORMAT_B8G8R8A8_UNORM,
++ StreamingImageFormat::Rgba8 => DXGI_FORMAT_R8G8B8A8_UNORM,
++ };
++ let position = self.streams.iter().position(|stream| stream.id == id);
++ let position = match position {
++ Some(position)
++ if self.streams[position].size == size
++ && self.streams[position].format == format =>
++ {
++ position
++ }
++ Some(position) => {
++ let index = self.streams[position].index;
++ let Some(stream) = self.new_stream(id, index, size, format) else {
++ self.streams.remove(position);
++ return Ok(None);
++ };
++ self.streams[position] = stream;
++ position
++ }
++ None => {
++ let index = self.next_stream_index;
++ let Some(stream) = self.new_stream(id, index, size, format) else {
++ return Ok(None);
++ };
++ self.next_stream_index = self.next_stream_index.wrapping_add(1);
++ self.streams.push(stream);
++ self.streams.len() - 1
++ }
++ };
++ let stream = &mut self.streams[position];
++ if stream.generation != frame.generation() {
++ unsafe {
++ self.device_context.UpdateSubresource(
++ &stream.texture,
++ 0,
++ None,
++ frame.bytes().as_ptr() as _,
++ stride,
++ 0,
++ );
++ }
++ stream.generation = frame.generation();
+ }
++ Ok(Some(AtlasTile::for_stream(stream.index, size)))
++ }
++
++ fn new_stream(
++ &self,
++ id: StreamingImageId,
++ index: u32,
++ size: Size<DevicePixels>,
++ format: DXGI_FORMAT,
++ ) -> Option<DirectXAtlasStream> {
++ let texture_desc = D3D11_TEXTURE2D_DESC {
++ Width: size.width.0 as u32,
++ Height: size.height.0 as u32,
++ MipLevels: 1,
++ ArraySize: 1,
++ Format: format,
++ SampleDesc: DXGI_SAMPLE_DESC {
++ Count: 1,
++ Quality: 0,
++ },
++ Usage: D3D11_USAGE_DEFAULT,
++ BindFlags: D3D11_BIND_SHADER_RESOURCE.0 as u32,
++ CPUAccessFlags: 0,
++ MiscFlags: 0,
++ };
++ let mut texture: Option<ID3D11Texture2D> = None;
++ let mut view = None;
++ unsafe {
++ self.device
++ .CreateTexture2D(&texture_desc, None, Some(&mut texture))
++ .ok()?;
++ self.device
++ .CreateShaderResourceView(texture.as_ref()?, None, Some(&mut view))
++ .ok()?;
++ }
++ Some(DirectXAtlasStream {
++ id,
++ index,
++ size,
++ format,
++ generation: 0,
++ texture: texture?,
++ view: [view],
++ })
+ }
+ }
+
+@@ -357,17 +510,31 @@ fn etagere_point_to_device(value: etagere::Point) -> Point<DevicePixels> {
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+- use gpui::{ImageId, RenderImageParams};
++ use gpui::{ImageId, RenderImageParams, StreamingImage};
+ use std::borrow::Cow;
+ use windows::Win32::{
+ Foundation::HMODULE,
+ Graphics::{
+ Direct3D::D3D_DRIVER_TYPE_WARP,
+- Direct3D11::{D3D11_CREATE_DEVICE_BGRA_SUPPORT, D3D11_SDK_VERSION, D3D11CreateDevice},
++ Direct3D11::{
++ D3D11_CPU_ACCESS_READ, D3D11_CREATE_DEVICE_BGRA_SUPPORT, D3D11_MAP_READ,
++ D3D11_MAPPED_SUBRESOURCE, D3D11_SDK_VERSION, D3D11_USAGE_STAGING,
++ D3D11CreateDevice,
++ },
+ },
+ };
+
+ fn create_atlas() -> Option<DirectXAtlas> {
++ let device = create_device();
++ assert!(
++ device.is_some() || std::env::var_os("GPUI_REQUIRE_TEST_DEVICE").is_none(),
++ "GPUI_REQUIRE_TEST_DEVICE is set but no WARP device is available"
++ );
++ let (device, device_context) = device?;
++ Some(DirectXAtlas::new(&device, &device_context))
++ }
++
++ fn create_device() -> Option<(ID3D11Device, ID3D11DeviceContext)> {
+ let mut device: Option<ID3D11Device> = None;
+ let mut device_context: Option<ID3D11DeviceContext> = None;
+ unsafe {
+@@ -384,7 +551,7 @@ mod tests {
+ )
+ }
+ .ok()?;
+- Some(DirectXAtlas::new(&device?, &device_context?))
++ Some((device?, device_context?))
+ }
+
+ fn make_image_key(image_id: usize) -> AtlasKey {
+@@ -433,6 +600,264 @@ mod tests {
+ assert_eq!(tile_b.texture_id, keeper_tile.texture_id);
+ }
+
++ fn stream_size(width: i32, height: i32) -> Size<DevicePixels> {
++ Size {
++ width: DevicePixels(width),
++ height: DevicePixels(height),
++ }
++ }
++
++ fn pixels(size: Size<DevicePixels>, seed: u8) -> Vec<u8> {
++ (0..size.width.0 * size.height.0 * 4)
++ .map(|index| (index as u8).wrapping_mul(31).wrapping_add(seed))
++ .collect()
++ }
++
++ fn set_frame(image: &StreamingImage, size: Size<DevicePixels>, bytes: Vec<u8>) -> StreamFrame {
++ image
++ .set_frame(size, size.width.0 as usize * 4, bytes)
++ .unwrap();
++ image.current().unwrap()
++ }
++
++ fn update(atlas: &DirectXAtlas, image: &StreamingImage, frame: &StreamFrame) -> AtlasTile {
++ atlas
++ .get_or_update_stream(image.id(), frame)
++ .expect("stream upload should succeed")
++ .expect("stream upload returns a tile")
++ }
++
++ fn stream_texture(atlas: &DirectXAtlas, image: &StreamingImage) -> ID3D11Texture2D {
++ atlas
++ .0
++ .lock()
++ .streams
++ .iter()
++ .find(|stream| stream.id == image.id())
++ .expect("stream should exist")
++ .texture
++ .clone()
++ }
++
++ fn copy_to_staging(atlas: &DirectXAtlas, texture: &ID3D11Texture2D) -> ID3D11Texture2D {
++ let lock = atlas.0.lock();
++ let mut desc = D3D11_TEXTURE2D_DESC::default();
++ unsafe { texture.GetDesc(&mut desc) };
++ desc.Usage = D3D11_USAGE_STAGING;
++ desc.BindFlags = 0;
++ desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ.0 as u32;
++ let mut staging: Option<ID3D11Texture2D> = None;
++ unsafe {
++ lock.device
++ .CreateTexture2D(&desc, None, Some(&mut staging))
++ .unwrap();
++ let staging = staging.unwrap();
++ lock.device_context.CopyResource(&staging, texture);
++ staging
++ }
++ }
++
++ fn read_staging(atlas: &DirectXAtlas, staging: &ID3D11Texture2D) -> Vec<u8> {
++ let lock = atlas.0.lock();
++ let mut desc = D3D11_TEXTURE2D_DESC::default();
++ unsafe { staging.GetDesc(&mut desc) };
++ let mut mapped = D3D11_MAPPED_SUBRESOURCE::default();
++ unsafe {
++ lock.device_context
++ .Map(staging, 0, D3D11_MAP_READ, 0, Some(&mut mapped))
++ .unwrap();
++ }
++ let row_bytes = desc.Width as usize * 4;
++ let mut bytes = Vec::with_capacity(row_bytes * desc.Height as usize);
++ for row in 0..desc.Height as usize {
++ let row = unsafe {
++ std::slice::from_raw_parts(
++ (mapped.pData as *const u8).add(row * mapped.RowPitch as usize),
++ row_bytes,
++ )
++ };
++ bytes.extend_from_slice(row);
++ }
++ unsafe { lock.device_context.Unmap(staging, 0) };
++ bytes
++ }
++
++ fn read_stream(atlas: &DirectXAtlas, image: &StreamingImage) -> Vec<u8> {
++ let staging = copy_to_staging(atlas, &stream_texture(atlas, image));
++ read_staging(atlas, &staging)
++ }
++
++ fn swizzled(bytes: &[u8]) -> Vec<u8> {
++ bytes
++ .chunks_exact(4)
++ .flat_map(|pixel| [pixel[2], pixel[1], pixel[0], pixel[3]])
++ .collect()
++ }
++
++ #[test]
++ fn stream_updates_of_the_same_size_reuse_one_texture() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(64, 36);
++ let first = update(&atlas, &image, &set_frame(&image, size, pixels(size, 1)));
++ let texture = stream_texture(&atlas, &image);
++ for seed in 2..10 {
++ let tile = update(&atlas, &image, &set_frame(&image, size, pixels(size, seed)));
++ assert_eq!(tile, first);
++ }
++
++ assert_eq!(first.texture_id.kind, AtlasTextureKind::Stream);
++ assert_eq!(first.bounds.size, size);
++ assert_eq!(stream_texture(&atlas, &image), texture);
++ assert_eq!(read_stream(&atlas, &image), pixels(size, 9));
++ }
++
++ #[test]
++ fn stream_size_change_reallocates_once() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let small = stream_size(64, 36);
++ let large = stream_size(128, 72);
++ let small_tile = update(&atlas, &image, &set_frame(&image, small, pixels(small, 1)));
++ let small_texture = stream_texture(&atlas, &image);
++ let large_tile = update(&atlas, &image, &set_frame(&image, large, pixels(large, 2)));
++ let large_texture = stream_texture(&atlas, &image);
++ update(&atlas, &image, &set_frame(&image, large, pixels(large, 3)));
++
++ assert_eq!(large_tile.texture_id, small_tile.texture_id);
++ assert_eq!(large_tile.bounds.size, large);
++ assert_ne!(large_texture, small_texture);
++ assert_eq!(stream_texture(&atlas, &image), large_texture);
++ assert_eq!(read_stream(&atlas, &image), pixels(large, 3));
++ }
++
++ #[test]
++ fn removing_images_leaves_streams_alone() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let size = stream_size(64, 64);
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let stream_tile = update(&atlas, &image, &set_frame(&image, size, pixels(size, 5)));
++ let stream = stream_texture(&atlas, &image);
++
++ let first_key = make_image_key(20);
++ let second_key = make_image_key(21);
++ let first = insert_tile(&atlas, &first_key, stream_size(1280, 720));
++ atlas.remove(&first_key);
++ let second = insert_tile(&atlas, &second_key, stream_size(1280, 720));
++ atlas.remove(&second_key);
++
++ assert_eq!(first.texture_id.kind, AtlasTextureKind::Polychrome);
++ assert_eq!(first.texture_id, second.texture_id);
++ assert_eq!(
++ atlas
++ .0
++ .lock()
++ .polychrome_textures
++ .textures
++ .iter()
++ .flatten()
++ .count(),
++ 1
++ );
++ assert!(
++ atlas
++ .get_stream_texture_view(stream_tile.texture_id)
++ .is_some()
++ );
++ assert_eq!(stream_texture(&atlas, &image), stream);
++ assert_eq!(read_stream(&atlas, &image), pixels(size, 5));
++
++ atlas.remove_stream(image.id());
++ assert!(
++ atlas
++ .get_stream_texture_view(stream_tile.texture_id)
++ .is_none()
++ );
++ atlas.remove_stream(image.id());
++ }
++
++ #[test]
++ fn device_loss_uploads_streams_again() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(64, 8);
++ let frame = set_frame(&image, size, pixels(size, 4));
++ let tile = update(&atlas, &image, &frame);
++
++ let (device, device_context) = create_device().unwrap();
++ atlas.handle_device_lost(&device, &device_context);
++ assert!(atlas.get_stream_texture_view(tile.texture_id).is_none());
++ let tile = update(&atlas, &image, &frame);
++ assert!(atlas.get_stream_texture_view(tile.texture_id).is_some());
++ assert_eq!(read_stream(&atlas, &image), pixels(size, 4));
++ }
++
++ #[test]
++ fn stream_writes_are_ordered_with_earlier_copies() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(64, 64);
++ update(&atlas, &image, &set_frame(&image, size, pixels(size, 1)));
++ let texture = stream_texture(&atlas, &image);
++ let first = copy_to_staging(&atlas, &texture);
++ update(&atlas, &image, &set_frame(&image, size, pixels(size, 2)));
++ let second = copy_to_staging(&atlas, &texture);
++
++ assert_eq!(read_staging(&atlas, &first), pixels(size, 1));
++ assert_eq!(read_staging(&atlas, &second), pixels(size, 2));
++ }
++
++ #[test]
++ fn stream_formats_and_strides_upload_the_same_pixels() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let size = stream_size(64, 16);
++ let bgra = pixels(size, 3);
++ let rgba = swizzled(&bgra);
++ let bgra_image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ update(
++ &atlas,
++ &bgra_image,
++ &set_frame(&bgra_image, size, bgra.clone()),
++ );
++ let stride = 64 * 4 + 32;
++ let mut padded = vec![0xee; stride * 16];
++ for (row, source) in padded
++ .chunks_exact_mut(stride)
++ .zip(rgba.chunks_exact(64 * 4))
++ {
++ row[..64 * 4].copy_from_slice(source);
++ }
++ let rgba_image = StreamingImage::new(StreamingImageFormat::Rgba8);
++ rgba_image.set_frame(size, stride, padded).unwrap();
++ update(&atlas, &rgba_image, &rgba_image.current().unwrap());
++
++ assert_eq!(read_stream(&atlas, &bgra_image), bgra);
++ assert_eq!(read_stream(&atlas, &rgba_image), rgba);
++ }
++
++ #[test]
++ fn oversized_stream_frames_are_rejected() {
++ let Some(atlas) = create_atlas() else {
++ return;
++ };
++ let image = StreamingImage::new(StreamingImageFormat::Bgra8);
++ let size = stream_size(MAX_STREAM_TEXTURE_SIZE + 1, 1);
++ let frame = set_frame(&image, size, vec![0; size.width.0 as usize * 4]);
++ assert!(atlas.get_or_update_stream(image.id(), &frame).is_err());
++ }
++
+ #[test]
+ fn test_reuses_one_unreferenced_polychrome_texture() {
+ let Some(atlas) = create_atlas() else {
+diff --git a/crates/gpui_windows/src/directx_renderer.rs b/crates/gpui_windows/src/directx_renderer.rs
+index 35b504e..e4b605b 100644
+--- a/crates/gpui_windows/src/directx_renderer.rs
++++ b/crates/gpui_windows/src/directx_renderer.rs
+@@ -712,7 +712,14 @@ impl DirectXRenderer {
+ }
+ let devices = self.devices.as_ref().context("devices missing")?;
+ let resources = self.resources.as_ref().context("resources missing")?;
+- let texture_view = self.atlas.get_texture_view(texture_id);
++ let texture_view = if texture_id.kind == AtlasTextureKind::Stream {
++ let Some(texture_view) = self.atlas.get_stream_texture_view(texture_id) else {
++ return Ok(());
++ };
++ texture_view
++ } else {
++ self.atlas.get_texture_view(texture_id)
++ };
+ self.pipelines.poly_sprites.draw_range_with_texture(
+ &devices.device,
+ &devices.device_context,
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index f9fb9a8e7..0a61ef5a4 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -69,8 +69,7 @@ pub(crate) fn display_work_area(
let bounds = scap_targets::Display::from_id(&id)
.as_ref()
.and_then(capture_display_bounds)?;
- let primary_height = cx.primary_display()?.bounds().size.height;
- Some(global_macos_work_area(available, bounds, primary_height))
+ Some(global_macos_work_area(available, bounds))
}
#[cfg(not(target_os = "macos"))]
Some(available)
@@ -147,13 +146,12 @@ fn capture_display_bounds(display: &scap_targets::Display) -> Option<Bounds<Pixe
fn global_macos_work_area(
mut available: Bounds<Pixels>,
display: Bounds<Pixels>,
- primary_height: Pixels,
) -> Bounds<Pixels> {
- // GPUI's macOS work area uses local x but includes the AppKit screen y.
- // Windows opened without a display ID need primary-display coordinates.
- let appkit_origin_y = primary_height - display.origin.y - display.size.height;
- available.origin.x += display.origin.x;
- available.origin.y += display.origin.y - appkit_origin_y;
+ // GPUI's macOS work area is relative to its own display's top-left corner
+ // (`visible_bounds` in gpui_macos). Windows opened without a display ID need
+ // primary-display coordinates, so a display above or below the primary one
+ // has to carry its global origin across.
+ available.origin += display.origin;
available
}
@@ -547,6 +545,18 @@ pub fn init(main: WindowHandle<MainWindow>, session: Entity<RecordingSession>, c
})
.detach();
+ let thumbnails = crate::recording::studio_thumbnail_events();
+ cx.spawn(async move |cx| {
+ while thumbnails.recv_async().await.is_ok() {
+ cx.update(|cx| {
+ crate::tray::refresh_previous(cx);
+ refresh_library_after_delete(cx);
+ crate::menus::recheck_pending_quit(cx);
+ });
+ }
+ })
+ .detach();
+
let mut last_phase = Phase::Idle;
cx.observe(&session, move |session, cx| {
let phase = session.read(cx).phase;
@@ -2280,6 +2290,27 @@ pub fn refresh_target_overlays(cx: &mut App) {
}
}
+/// [`refresh_target_overlays`] for a mic level change: only the overlays whose
+/// meter would visibly move repaint.
+pub fn refresh_microphone_overlays(cx: &mut App) {
+ let handles: Vec<_> = cx
+ .global::<AppWindows>()
+ .overlays
+ .iter()
+ .map(|(_, handle)| *handle)
+ .collect();
+ for handle in handles {
+ handle
+ .update(cx, |view, window, cx| {
+ if view.microphone_level_moved(window, cx) {
+ cx.notify();
+ window.refresh();
+ }
+ })
+ .ok();
+ }
+}
+
/// The overlays' native window numbers, for capture exclusion.
fn overlay_window_ids(cx: &mut App) -> Vec<scap_targets::WindowId> {
let handles: Vec<_> = cx
@@ -4875,13 +4906,86 @@ pub(crate) async fn shared_wgpu_device() -> Option<cap_rendering::SharedWgpuDevi
}
}
+/// Set once anything has asked for the shared device -- an editor, a
+/// screenshot editor or a recording -- so the launch warmup leaves the GPU to
+/// it instead of compiling pipelines alongside.
+static GPU_WORK_REQUESTED: std::sync::atomic::AtomicBool =
+ std::sync::atomic::AtomicBool::new(false);
+
+#[cfg(target_os = "macos")]
+fn gpu_work_requested() -> bool {
+ GPU_WORK_REQUESTED.load(std::sync::atomic::Ordering::Acquire)
+}
+
pub(crate) fn prewarm_shared_wgpu_device(cx: &App) {
+ GPU_WORK_REQUESTED.store(true, std::sync::atomic::Ordering::Release);
gpui_tokio::Tokio::spawn(cx, async {
let _ = shared_wgpu_device().await;
})
.detach();
}
+const LAUNCH_RENDER_WARMUP_DELAY: std::time::Duration = std::time::Duration::from_secs(3);
+
+/// The first editor open in a process paid for the system font scan, the
+/// shared device and the renderer's first pipeline compiles and draw before
+/// its first frame: 0.7-1.2 s, against ~0.2 s for every later open. This warms
+/// them a few seconds after launch, off the main thread. The layers are
+/// dropped again; only the driver's compiled pipelines, the device and the
+/// font database stay.
+pub(crate) fn schedule_launch_render_warmup(cx: &App) {
+ gpui_tokio::Tokio::spawn(cx, async {
+ tokio::time::sleep(LAUNCH_RENDER_WARMUP_DELAY).await;
+ let started = std::time::Instant::now();
+ if tokio::task::spawn_blocking(cap_rendering::prewarm_fonts)
+ .await
+ .is_err()
+ {
+ return;
+ }
+ let fonts_ms = started.elapsed().as_secs_f64() * 1000.0;
+ let renderer_ms = warm_renderer().await;
+ tracing::info!(fonts_ms, ?renderer_ms, "launch render warmup finished");
+ })
+ .detach();
+}
+
+#[cfg(target_os = "macos")]
+async fn warm_renderer() -> Option<f64> {
+ if gpu_work_requested() {
+ return None;
+ }
+ let shared = shared_wgpu_device().await?;
+ if gpu_work_requested() {
+ return None;
+ }
+ let started = std::time::Instant::now();
+ let (device, queue, software) = (
+ shared.device.clone(),
+ shared.queue.clone(),
+ shared.is_software_adapter,
+ );
+ let mut layers = tokio::task::spawn_blocking(move || {
+ (!gpu_work_requested())
+ .then(|| cap_rendering::RendererLayers::new_with_options(&device, &queue, software))
+ })
+ .await
+ .ok()??;
+ if gpu_work_requested() {
+ return None;
+ }
+ if let Err(error) = cap_rendering::prewarm_first_frame(shared, &mut layers).await {
+ tracing::warn!(%error, "launch render warmup frame failed");
+ }
+ drop(layers);
+ Some(started.elapsed().as_secs_f64() * 1000.0)
+}
+
+#[cfg(not(target_os = "macos"))]
+async fn warm_renderer() -> Option<f64> {
+ None
+}
+
fn editor_audio_output() -> Arc<cap_editor::AudioOutput> {
if std::env::var("CAP_GPUI_MUTE_AUDIO").is_ok_and(|value| value == "1") {
Arc::new(cap_editor::AudioOutput::new_headless(Box::new(|_, _| {})))
@@ -6358,23 +6462,6 @@ pub fn arm_target_mode(kind: crate::main_window::TargetType, cx: &mut App) {
.ok();
}
-/// Repaint the bar between its once-a-second ticks -- the mic meter updates at
-/// up to ~20Hz and the bar is never the active window.
-pub fn refresh_controls_window(cx: &mut App) {
- let level_db = crate::feeds::Feeds::global(cx).read(cx).mic_level_db;
- if let Some(handle) = cx.global::<AppWindows>().controls {
- handle
- .update(cx, |controls, window, cx| {
- if !controls.level_bar_moves(level_db, window.scale_factor()) {
- return;
- }
- cx.notify();
- window.refresh();
- })
- .ok();
- }
-}
-
fn camera_window_number(cx: &mut App) -> Option<isize> {
let handle = cx.global::<AppWindows>().camera?;
handle
@@ -6558,10 +6645,14 @@ pub fn screenshot_finished(captured: Option<PathBuf>, cx: &mut App) {
.ok();
let behaviour = crate::store::GeneralSettings::load().post_screenshot_behaviour;
- if behaviour.copies()
- && let Err(error) = platform::copy_image_to_clipboard(&png, cx)
- {
- tracing::error!("copying the screenshot to the clipboard failed: {error}");
+ if behaviour.copies() {
+ let copied = platform::copy_image_to_clipboard(png.clone(), cx);
+ cx.spawn(async move |_| {
+ if let Err(error) = copied.await {
+ tracing::error!("copying the screenshot to the clipboard failed: {error}");
+ }
+ })
+ .detach();
}
if behaviour.opens_editor() {
@@ -6888,11 +6979,12 @@ mod tests {
#[cfg(target_os = "macos")]
#[test]
fn macos_work_area_preserves_horizontal_and_vertical_display_origins() {
- for (display_origin, appkit_y) in [
- (point(px(-1440.), px(0.)), 180.),
- (point(px(1920.), px(0.)), 180.),
- (point(px(0.), px(-900.)), 1080.),
- (point(px(0.), px(1080.)), -900.),
+ for display_origin in [
+ point(px(-1440.), px(0.)),
+ point(px(1920.), px(0.)),
+ point(px(0.), px(-900.)),
+ point(px(0.), px(1080.)),
+ point(px(1691.), px(-1080.)),
] {
let display = Bounds {
origin: display_origin,
@@ -6900,11 +6992,10 @@ mod tests {
};
let available = global_macos_work_area(
Bounds {
- origin: point(px(0.), px(appkit_y + 25.)),
+ origin: point(px(0.), px(25.)),
size: size(px(1440.), px(825.)),
},
display,
- px(1080.),
);
assert_eq!(available.origin, display_origin + point(px(0.), px(25.)));
assert_eq!(available.size, size(px(1440.), px(825.)));
diff --git a/apps/desktop-gpui/src/assets.rs b/apps/desktop-gpui/src/assets.rs
index be8bf1b32..638020d00 100644
--- a/apps/desktop-gpui/src/assets.rs
+++ b/apps/desktop-gpui/src/assets.rs
@@ -182,6 +182,7 @@ const ICONS: &[(&str, &[u8])] = assets!("icons":
"import.svg",
"edit.svg",
"copy.svg",
+ "clipboard-paste.svg",
"record-fill.svg",
"warning-bold.svg",
"traffic-close.svg",
diff --git a/apps/desktop-gpui/src/auth.rs b/apps/desktop-gpui/src/auth.rs
index 0ddbed2aa..59a201895 100644
--- a/apps/desktop-gpui/src/auth.rs
+++ b/apps/desktop-gpui/src/auth.rs
@@ -299,7 +299,7 @@ impl UploadRequestContext {
current_server: &str,
current_auth: &serde_json::Map<String, Value>,
) -> Result<String, AuthApiError> {
- if current_server != self.server_url
+ if !has_same_origin(current_server, &self.server_url)
|| current_auth.get("user_id").and_then(Value::as_str) != Some(self.owner_id.as_str())
{
return Err(AuthApiError::InvalidAuthentication);
@@ -323,20 +323,74 @@ impl UploadRequestContext {
}
}
-pub fn api_client() -> Result<reqwest::Client, AuthApiError> {
+/// Idle API connections are dropped after 4 s, before the 5 s keep-alive
+/// timeout of a self-hosted Node server, so a request is never sent on a
+/// connection the server is closing.
+pub(crate) const API_POOL_IDLE_TIMEOUT: Duration = Duration::from_secs(4);
+/// The shared API client is rebuilt this often, so system proxy changes still
+/// apply. It is wall clock time, which counts a sleep where the pool's idle
+/// clock does not, so calls after a longer sleep start on new connections.
+const API_CLIENT_MAX_AGE: Duration = Duration::from_secs(30);
+
+fn api_client_builder() -> reqwest::ClientBuilder {
reqwest::Client::builder()
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(45))
+}
+
+/// Multipart uploads hold their own client and drop it when the upload ends,
+/// so a later upload never starts on a pooled connection from an earlier
+/// network (#2522).
+pub fn api_client() -> Result<reqwest::Client, AuthApiError> {
+ api_client_builder()
.build()
.map_err(|error| AuthApiError::Other(error.to_string()))
}
+static SHARED_API_CLIENT: Mutex<Option<(std::time::SystemTime, reqwest::Client)>> =
+ Mutex::new(None);
+
+/// Held by every test that resets the shared client or counts its
+/// connections, so a reset on one thread cannot land in another's count.
+#[cfg(test)]
+pub(crate) static SHARED_API_CLIENT_TEST_LOCK: tokio::sync::Mutex<()> =
+ tokio::sync::Mutex::const_new(());
+
+/// Drops the shared client, as quitting the app does, for tests that model a
+/// relaunch.
+#[cfg(test)]
+pub(crate) fn reset_shared_api_client() {
+ *SHARED_API_CLIENT
+ .lock()
+ .unwrap_or_else(|error| error.into_inner()) = None;
+}
+
+/// The client `authed_request` uses. Calls close together, like Instant
+/// presigns about once a second or the requests after a recording stops,
+/// reuse its open connections instead of each paying a TCP and TLS handshake.
+fn shared_api_client() -> Result<reqwest::Client, AuthApiError> {
+ let mut shared = SHARED_API_CLIENT
+ .lock()
+ .unwrap_or_else(|error| error.into_inner());
+ if let Some((built, client)) = shared.as_ref()
+ && built.elapsed().is_ok_and(|age| age < API_CLIENT_MAX_AGE)
+ {
+ return Ok(client.clone());
+ }
+ let client = api_client_builder()
+ .pool_idle_timeout(API_POOL_IDLE_TIMEOUT)
+ .build()
+ .map_err(|error| AuthApiError::Other(error.to_string()))?;
+ *shared = Some((std::time::SystemTime::now(), client.clone()));
+ Ok(client)
+}
+
pub async fn authed_request(
method: reqwest::Method,
path: &str,
body: Option<Value>,
) -> Result<reqwest::Response, AuthApiError> {
- authed_request_with(&api_client()?, method, path, body).await
+ authed_request_with(&shared_api_client()?, method, path, body).await
}
pub async fn authed_request_with(
@@ -867,6 +921,12 @@ mod upload_context_tests {
.unwrap(),
"refreshed-credential"
);
+ assert_eq!(
+ context
+ .credentials_from("https://original.invalid:443", &refreshed)
+ .unwrap(),
+ "refreshed-credential"
+ );
assert!(
context
.credentials_from("https://changed.invalid", &refreshed)
diff --git a/apps/desktop-gpui/src/camera_blur_portable.rs b/apps/desktop-gpui/src/camera_blur_portable.rs
index 0247ef09a..d8e2ca620 100644
--- a/apps/desktop-gpui/src/camera_blur_portable.rs
+++ b/apps/desktop-gpui/src/camera_blur_portable.rs
@@ -47,6 +47,7 @@ pub struct PortableCameraBlur {
device: wgpu::Device,
queue: wgpu::Queue,
processor: cap_camera_effects::BlurProcessor,
+ format: wgpu::TextureFormat,
source: Option<wgpu::Texture>,
readback: Option<wgpu::Buffer>,
dimensions: Option<(u32, u32)>,
@@ -54,13 +55,32 @@ pub struct PortableCameraBlur {
rgba: Vec<u8>,
}
+/// The source and output stay `Bgra8Unorm` where the adapter can sample,
+/// render and copy it, so neither side of the GPU trip swaps channels on the
+/// CPU. Sampling returns the same values for either channel order.
+fn bgra_supported(adapter: &wgpu::Adapter) -> bool {
+ let features = adapter.get_texture_format_features(wgpu::TextureFormat::Bgra8Unorm);
+ features.allowed_usages.contains(
+ wgpu::TextureUsages::TEXTURE_BINDING
+ | wgpu::TextureUsages::COPY_DST
+ | wgpu::TextureUsages::RENDER_ATTACHMENT
+ | wgpu::TextureUsages::COPY_SRC,
+ ) && features
+ .flags
+ .contains(wgpu::TextureFormatFeatureFlags::FILTERABLE)
+}
+
impl PortableCameraBlur {
pub fn new() -> anyhow::Result<Self> {
+ Self::with_bgra(true)
+ }
+
+ fn with_bgra(allow_bgra: bool) -> anyhow::Result<Self> {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.context("camera blur runtime")?;
- let (device, queue) = runtime.block_on(async {
+ let (format, device, queue) = runtime.block_on(async {
let instance = cap_rendering::create_wgpu_instance_sync();
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
@@ -70,7 +90,12 @@ impl PortableCameraBlur {
})
.await
.map_err(|error| anyhow!("camera blur adapter: {error}"))?;
- adapter
+ let format = if allow_bgra && bgra_supported(&adapter) {
+ wgpu::TextureFormat::Bgra8Unorm
+ } else {
+ wgpu::TextureFormat::Rgba8Unorm
+ };
+ let (device, queue) = adapter
.request_device(&wgpu::DeviceDescriptor {
label: Some("Camera Preview Blur"),
required_features: wgpu::Features::empty(),
@@ -80,17 +105,22 @@ impl PortableCameraBlur {
trace: wgpu::Trace::Off,
})
.await
- .map_err(|error| anyhow!("camera blur device: {error}"))
+ .map_err(|error| anyhow!("camera blur device: {error}"))?;
+ anyhow::Ok((format, device, queue))
})?;
- let mut processor =
- cap_camera_effects::BlurProcessor::new(&device, wgpu::TextureFormat::Rgba8Unorm)
- .context("camera blur processor")?;
+ device.push_error_scope(wgpu::ErrorFilter::Validation);
+ let processor = cap_camera_effects::BlurProcessor::new(&device, format);
+ if let Some(error) = runtime.block_on(device.pop_error_scope()) {
+ anyhow::bail!("camera blur GPU validation: {error}");
+ }
+ let mut processor = processor.context("camera blur processor")?;
processor.set_inference_interval(INFERENCE_INTERVAL);
Ok(Self {
device,
queue,
processor,
+ format,
source: None,
readback: None,
dimensions: None,
@@ -130,7 +160,12 @@ impl PortableCameraBlur {
}
self.ensure_resources(width, height)?;
- cap_gpui_kernels::frame::swap_red_blue_into(bgra, &mut self.rgba);
+ let pixels = if self.format == wgpu::TextureFormat::Bgra8Unorm {
+ bgra
+ } else {
+ cap_gpui_kernels::frame::swap_red_blue_into(bgra, &mut self.rgba);
+ self.rgba.as_slice()
+ };
let source = self.source.as_ref().context("camera blur source missing")?;
self.queue.write_texture(
@@ -140,7 +175,7 @@ impl PortableCameraBlur {
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
- &self.rgba,
+ pixels,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(row_bytes),
@@ -207,12 +242,21 @@ impl PortableCameraBlur {
let pixels = {
let mapped = readback.slice(..).get_mapped_range();
- cap_gpui_kernels::frame::unpad_rgba_to_bgra(
- &mapped,
- width as usize,
- self.padded_bytes_per_row as usize,
- height as usize,
- )
+ if self.format == wgpu::TextureFormat::Bgra8Unorm {
+ cap_gpui_kernels::frame::unpad_rows(
+ &mapped,
+ width as usize * 4,
+ self.padded_bytes_per_row as usize,
+ height as usize,
+ )
+ } else {
+ cap_gpui_kernels::frame::unpad_rgba_to_bgra(
+ &mapped,
+ width as usize,
+ self.padded_bytes_per_row as usize,
+ height as usize,
+ )
+ }
};
readback.unmap();
let pixels = pixels.context("camera blur readback rows")?;
@@ -256,7 +300,7 @@ impl PortableCameraBlur {
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
- format: wgpu::TextureFormat::Rgba8Unorm,
+ format: self.format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
}));
@@ -294,6 +338,49 @@ mod tests {
);
}
+ fn test_frame(width: u32, height: u32, seed: u32) -> gpui::RenderImage {
+ let image = image::RgbaImage::from_fn(width, height, |x, y| {
+ let inside = (x + seed * 7) % width < width / 3 && (y + seed * 3) % height < height / 2;
+ image::Rgba([
+ (x * 255 / width) as u8,
+ (y * 255 / height) as u8,
+ if inside { 230 } else { (seed * 40) as u8 },
+ 255,
+ ])
+ });
+ gpui::RenderImage::new(smallvec::smallvec![image::Frame::new(image)])
+ }
+
+ fn deterministic_blur(allow_bgra: bool) -> Option<PortableCameraBlur> {
+ #[cfg(not(target_os = "macos"))]
+ if std::env::var_os("CAP_GPUI_TEST_PORTABLE_BLUR").is_none() {
+ return None;
+ }
+ match PortableCameraBlur::with_bgra(allow_bgra) {
+ Ok(mut blur) => {
+ blur.processor.set_frame_synchronous(true);
+ Some(blur)
+ }
+ Err(error) => {
+ eprintln!("skipping: portable camera blur unavailable: {error:#}");
+ None
+ }
+ }
+ }
+
+ #[test]
+ fn unsupported_gpu_returns_an_error_without_panicking() {
+ if std::env::var_os("CAP_GPUI_TEST_UNSUPPORTED_BLUR_GPU").is_none() {
+ return;
+ }
+
+ cap_camera_effects::initialize_onnx_runtime().expect("ONNX Runtime available");
+ let error = PortableCameraBlur::new()
+ .err()
+ .expect("unsupported GPU rejected");
+ assert!(error.to_string().contains("camera blur GPU validation"));
+ }
+
#[test]
fn portable_blur_processes_bgra_frames_without_changing_dimensions() {
#[cfg(not(target_os = "macos"))]
@@ -355,6 +442,46 @@ mod tests {
assert_eq!(processor.readback.as_ref().unwrap().size(), 640 * 360 * 4);
}
+ #[test]
+ fn portable_blur_bgra_textures_match_the_rgba_swap_path() {
+ let Some(mut rgba) = deterministic_blur(false) else {
+ return;
+ };
+ let Some(mut bgra) = deterministic_blur(true) else {
+ return;
+ };
+ assert_eq!(rgba.format, wgpu::TextureFormat::Rgba8Unorm);
+ if bgra.format != wgpu::TextureFormat::Bgra8Unorm {
+ eprintln!("skipping: the adapter cannot sample, render and copy Bgra8Unorm");
+ return;
+ }
+
+ for (dimensions, mode) in [
+ ((640, 360), cap_camera_effects::BlurMode::Heavy),
+ ((480, 360), cap_camera_effects::BlurMode::Light),
+ ((64, 32), cap_camera_effects::BlurMode::Remove),
+ ] {
+ for seed in 0..4 {
+ let frame = test_frame(dimensions.0 as u32, dimensions.1 as u32, seed);
+ let (expected, expected_status) = rgba
+ .process_with_status(&frame, dimensions, mode)
+ .expect("rgba path blurred a frame");
+ let (actual, actual_status) = bgra
+ .process_with_status(&frame, dimensions, mode)
+ .expect("bgra path blurred a frame");
+ assert_eq!(
+ actual_status.output_dimensions,
+ expected_status.output_dimensions
+ );
+ assert!(
+ actual.as_bytes(0) == expected.as_bytes(0),
+ "bgra output differs at {dimensions:?} {mode:?} frame {seed}"
+ );
+ }
+ }
+ assert!(bgra.rgba.is_empty());
+ }
+
#[cfg(target_os = "macos")]
#[test]
fn portable_blur_processes_real_webcam_frames_when_requested() {
diff --git a/apps/desktop-gpui/src/camera_loading.rs b/apps/desktop-gpui/src/camera_loading.rs
index d10aba247..644a4af4c 100644
--- a/apps/desktop-gpui/src/camera_loading.rs
+++ b/apps/desktop-gpui/src/camera_loading.rs
@@ -1,10 +1,12 @@
mod script_outline;
-use std::time::Instant;
+use std::cell::Cell;
+use std::rc::Rc;
+use std::time::{Duration, Instant};
use gpui::{
- Bounds, BoxShadow, ContentMask, Corners, Hsla, IntoElement, Pixels, Styled, Window, canvas,
- fill, hsla, linear_color_stop, linear_gradient, point, px, size,
+ Bounds, BoxShadow, ContentMask, Corners, EntityId, Hsla, IntoElement, Pixels, Styled, Window,
+ canvas, fill, hsla, linear_color_stop, linear_gradient, point, px, size,
};
use script_outline::LOADING;
@@ -19,6 +21,8 @@ const SWEEP_PERIOD_SECONDS: f32 = 1.6;
const SWEEP_HALF_WIDTH: f32 = 0.26;
const SCRIPT_REST_ALPHA: f32 = 0.12;
const SCRIPT_PEAK_ALPHA: f32 = 0.42;
+const MOTION_FPS: f32 = 60.;
+const DRIFT_FPS: f32 = 30.;
struct Cloud {
lightness: f32,
@@ -82,6 +86,7 @@ pub(crate) fn camera_loading(
opacity: f32,
animate: bool,
show_script: bool,
+ redraw: LoadingRedraw,
) -> impl IntoElement {
canvas(
|_, _, _| {},
@@ -116,7 +121,9 @@ pub(crate) fn camera_loading(
}
}
if animate {
- window.request_animation_frame();
+ let due = Instant::now() + frame_gap(elapsed, opacity, show_script);
+ let generation = redraw.begin();
+ redraw_when_due(window, window.current_view(), due, redraw, generation);
}
},
)
@@ -124,6 +131,56 @@ pub(crate) fn camera_loading(
.inset_0()
}
+/// Fades and the script sweep redraw at 60 fps. Between sweeps only the
+/// clouds move, under a pixel per frame at 30 fps. The gap is a fifth short of
+/// the frame time so a display refresh landing just early still counts.
+fn frame_gap(elapsed: f32, opacity: f32, show_script: bool) -> Duration {
+ let fps = if opacity < 1. || (show_script && sweep_center(elapsed).is_some()) {
+ MOTION_FPS
+ } else {
+ DRIFT_FPS
+ };
+ Duration::from_secs_f32(0.8 / fps)
+}
+
+/// Each paint supersedes the redraw scheduled by the one before it, so extra
+/// paints (a resize, a chrome change) never leave several deadlines running.
+#[derive(Clone, Default)]
+pub(crate) struct LoadingRedraw(Rc<Cell<u64>>);
+
+impl LoadingRedraw {
+ fn begin(&self) -> u64 {
+ let generation = self.0.get().wrapping_add(1);
+ self.0.set(generation);
+ generation
+ }
+
+ fn is_current(&self, generation: u64) -> bool {
+ self.0.get() == generation
+ }
+}
+
+/// Checks on each display refresh and only re-renders the view once `due`
+/// has passed, so redraws never outpace the platform's own frames.
+fn redraw_when_due(
+ window: &Window,
+ view: EntityId,
+ due: Instant,
+ redraw: LoadingRedraw,
+ generation: u64,
+) {
+ window.on_next_frame(move |window, cx| {
+ if !redraw.is_current(generation) {
+ return;
+ }
+ if Instant::now() >= due {
+ cx.notify(view);
+ } else {
+ redraw_when_due(window, view, due, redraw, generation);
+ }
+ });
+}
+
fn sweep_center(elapsed: f32) -> Option<f32> {
if !elapsed.is_finite() {
return None;
@@ -317,3 +374,87 @@ fn paint_glass(
],
);
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn paints_in(start: f32, seconds: f32, refresh_hz: f32, opacity: f32, script: bool) -> usize {
+ let refresh = 1. / refresh_hz;
+ let mut due = 0.;
+ let mut paints = 0;
+ let mut frame = (start / refresh).ceil();
+ while frame * refresh < start + seconds {
+ let now = frame * refresh;
+ if now >= due {
+ paints += 1;
+ due = now + frame_gap(now, opacity, script).as_secs_f32();
+ }
+ frame += 1.;
+ }
+ paints
+ }
+
+ #[test]
+ fn loading_redraws_at_sixty_while_sweeping_and_thirty_between() {
+ let period_start = SWEEP_PERIOD_SECONDS - SWEEP_LEAD_SECONDS;
+ assert!(sweep_center(period_start + 0.01).is_some());
+ assert!(sweep_center(period_start + SWEEP_SECONDS + 0.01).is_none());
+ let resting_start = period_start + SWEEP_SECONDS;
+ let resting_seconds = SWEEP_PERIOD_SECONDS - SWEEP_SECONDS;
+
+ for refresh_hz in [60., 120.] {
+ let sweeping = paints_in(period_start, SWEEP_SECONDS, refresh_hz, 1., true);
+ assert!((59..=61).contains(&sweeping), "{refresh_hz} Hz: {sweeping}");
+ let resting = paints_in(resting_start, resting_seconds, refresh_hz, 1., true);
+ assert!((17..=19).contains(&resting), "{refresh_hz} Hz: {resting}");
+ let no_script = paints_in(period_start, SWEEP_PERIOD_SECONDS, refresh_hz, 1., false);
+ assert!(
+ (47..=49).contains(&no_script),
+ "{refresh_hz} Hz: {no_script}"
+ );
+ let fading = paints_in(0., FADE_IN_SECONDS, refresh_hz, 0.5, false);
+ assert!((17..=19).contains(&fading), "{refresh_hz} Hz: {fading}");
+ }
+ let slow_display = paints_in(period_start, SWEEP_SECONDS, 20., 1., true);
+ assert!((19..=21).contains(&slow_display), "{slow_display}");
+ }
+
+ #[test]
+ fn extra_paints_leave_one_pending_redraw() {
+ let refresh = 1. / 120.;
+ let start = SWEEP_PERIOD_SECONDS - SWEEP_LEAD_SECONDS + SWEEP_SECONDS;
+ let end = start + SWEEP_PERIOD_SECONDS - SWEEP_SECONDS;
+ let redraw = LoadingRedraw::default();
+ let mut pending: Vec<(u64, f32)> = Vec::new();
+ let paint = |now: f32, pending: &mut Vec<(u64, f32)>| {
+ pending.push((redraw.begin(), now + frame_gap(now, 1., true).as_secs_f32()));
+ };
+ paint(start, &mut pending);
+ paint(start + refresh, &mut pending);
+
+ let mut paints = 0;
+ let mut frame = (start / refresh).ceil() + 2.;
+ while frame * refresh < end {
+ let now = frame * refresh;
+ let mut due_paints = 0;
+ pending.retain(|&(generation, due)| {
+ if !redraw.is_current(generation) {
+ return false;
+ }
+ if now >= due {
+ due_paints += 1;
+ return false;
+ }
+ true
+ });
+ for _ in 0..due_paints {
+ paints += 1;
+ paint(now, &mut pending);
+ }
+ assert!(pending.len() <= 1, "{} pending at {now}", pending.len());
+ frame += 1.;
+ }
+ assert!((17..=19).contains(&paints), "{paints}");
+ }
+}
diff --git a/apps/desktop-gpui/src/camera_window.rs b/apps/desktop-gpui/src/camera_window.rs
index 030261571..d8e080e65 100644
--- a/apps/desktop-gpui/src/camera_window.rs
+++ b/apps/desktop-gpui/src/camera_window.rs
@@ -388,6 +388,10 @@ mod frame {
pub mirrored: bool,
}
+ // Handed from the conversion thread to the main thread once written; the
+ // buffer is only read after that, and CF refcounts are atomic.
+ unsafe impl Send for Converted {}
+
/// Converts camera frames (typically `420v`) into BGRA IOSurface-backed
/// pixel buffers that gpui paints directly via `paint_surface_fitted`.
///
@@ -410,8 +414,10 @@ mod frame {
/// neither touches the recorded camera track.
///
/// The fixed rings keep a buffer alive while gpui's scene still references
- /// the previous one; four slots at camera rates leaves ~100ms before a
- /// slot is overwritten.
+ /// the previous one. Conversion runs on the `camera-preview-convert`
+ /// thread, so besides the painted frame and the previous scene's, one
+ /// converted frame can wait for the main thread, one can sit with the blur
+ /// worker, and one is being written: six slots cover all of them.
pub struct FrameConverter {
session: arc::R<vt::PixelTransferSession>,
/// `None` when unmirrored, or when the rotation session could not be
@@ -426,10 +432,10 @@ mod frame {
generation: u64,
}
- // Only touched from the main thread; the CF objects have atomic refcounts.
+ // Owned by the conversion thread; the CF objects have atomic refcounts.
unsafe impl Send for FrameConverter {}
- const RING_SIZE: usize = 4;
+ const RING_SIZE: usize = 6;
fn bgra_buffers(
count: usize,
@@ -514,21 +520,26 @@ mod frame {
/// The frame as a BGRA IOSurface-backed pixel buffer, downscaled to
/// fit `max_dims` when given and horizontally flipped when
/// `mirrored`. Recreates the rings when the camera's frame size, the
- /// target size or the mirror toggle changes.
+ /// target size or the mirror toggle changes, or when `force_rebuild`
+ /// asks for a fresh rotation session (keeping the generation count
+ /// rising so no earlier ring can alias a new one).
pub fn convert(
this: &mut Option<Self>,
frame: &cap_recording::NativeCameraFrame,
max_dims: Option<(usize, usize)>,
mirrored: bool,
+ force_rebuild: bool,
) -> Option<Converted> {
let src = frame.sample_buf.image_buf()?;
let src_dims = (src.width(), src.height());
let dst_dims = max_dims.map_or(src_dims, |max| fit_within(src_dims, max));
- if this.as_ref().is_none_or(|converter| {
- converter.src_dims != src_dims
- || converter.dst_dims != dst_dims
- || converter.mirrored != mirrored
- }) {
+ if force_rebuild
+ || this.as_ref().is_none_or(|converter| {
+ converter.src_dims != src_dims
+ || converter.dst_dims != dst_dims
+ || converter.mirrored != mirrored
+ })
+ {
let generation = this.as_ref().map_or(0, |c| c.generation + 1);
*this = Self::new(src_dims, dst_dims, mirrored, generation);
}
@@ -1001,6 +1012,7 @@ struct CameraPreviewView {
reveal_started: Option<Instant>,
loading_started: Option<Instant>,
loading_fade_started: Option<Instant>,
+ loading_redraw: camera_loading::LoadingRedraw,
retained_invalidated: bool,
selection_revision: u64,
frame_revision: Option<u64>,
@@ -1107,6 +1119,7 @@ impl CameraPreviewView {
reveal_started: None,
loading_started: None,
loading_fade_started: None,
+ loading_redraw: camera_loading::LoadingRedraw::default(),
retained_invalidated: false,
selection_revision: 0,
frame_revision: None,
@@ -1327,7 +1340,12 @@ impl Render for CameraPreviewView {
if let Some((started, opacity, animate)) = loading {
container = container.child(camera_loading::camera_loading(
- started, radius, opacity, animate, !has_issue,
+ started,
+ radius,
+ opacity,
+ animate,
+ !has_issue,
+ self.loading_redraw.clone(),
));
}
@@ -1435,6 +1453,64 @@ impl Render for CameraToolbarView {
}
}
+#[cfg(target_os = "macos")]
+struct ConvertJob {
+ frame: cap_recording::NativeCameraFrame,
+ settings: ConvertSettings,
+}
+
+/// What a frame was converted for. A result whose settings no longer match
+/// the window's (another camera selected, blur or mirroring toggled while it
+/// converted) is dropped; the next frame arrives within a frame interval.
+#[cfg(target_os = "macos")]
+#[derive(Clone, Copy, PartialEq)]
+struct ConvertSettings {
+ epoch: u64,
+ blur_mode: Option<cap_camera_effects::BlurMode>,
+ mirrored: bool,
+ rebuilds: u64,
+}
+
+/// The `camera-preview-convert` thread. The VideoToolbox transfer (and the
+/// mirror pass) blocks until the hardware finishes, ~4-5 ms per frame, which
+/// stalled every main-thread frame while it ran on the main thread.
+#[cfg(target_os = "macos")]
+struct ConvertBridge {
+ tx: flume::Sender<ConvertJob>,
+ _pump: gpui::Task<()>,
+}
+
+#[cfg(target_os = "macos")]
+fn run_preview_conversion(
+ jobs: flume::Receiver<ConvertJob>,
+ converted: flume::Sender<(Option<frame::Converted>, ConvertSettings)>,
+) {
+ let mut converter = None;
+ let mut rebuilds = None;
+ while let Ok(mut job) = jobs.recv() {
+ // Latest wins: a frame that queued behind a slow conversion is stale.
+ while let Ok(newer) = jobs.try_recv() {
+ job = newer;
+ }
+ let settings = job.settings;
+ let force_rebuild = rebuilds.is_some_and(|rebuilds| rebuilds != settings.rebuilds);
+ rebuilds = Some(settings.rebuilds);
+ let max_dims = settings.blur_mode.map(|_| camera_blur::BLUR_MAX_DIMS);
+ let result = frame::FrameConverter::convert(
+ &mut converter,
+ &job.frame,
+ max_dims,
+ settings.mirrored,
+ force_rebuild,
+ );
+ // Blocks while the main thread still holds the previous result, so the
+ // ring never gets more than one frame ahead of what it painted.
+ if converted.send((result, settings)).is_err() {
+ return;
+ }
+ }
+}
+
/// Channels to and from the `camera-blur` worker thread, plus the foreground
/// pump that paints its outputs. Dropping this closes the job channel, which
/// ends the thread and releases the ONNX/GPU resources -- the
@@ -1460,7 +1536,16 @@ pub struct CameraWindow {
resizing: Option<ResizeDrag>,
hovered_handle: Option<ResizeCorner>,
#[cfg(target_os = "macos")]
- converter: Option<frame::FrameConverter>,
+ convert: Option<ConvertBridge>,
+ /// Bumped when the mirror toggle changes, so the conversion thread
+ /// rebuilds its rings and retries a rotation session that failed before.
+ #[cfg(target_os = "macos")]
+ convert_rebuilds: u64,
+ /// Inline conversion when the thread could not be spawned.
+ #[cfg(target_os = "macos")]
+ fallback_converter: Option<frame::FrameConverter>,
+ #[cfg(target_os = "macos")]
+ fallback_rebuilds: Option<u64>,
#[cfg(target_os = "macos")]
blur: Option<BlurBridge>,
#[cfg(target_os = "macos")]
@@ -1677,7 +1762,13 @@ impl CameraWindow {
resizing: None,
hovered_handle: None,
#[cfg(target_os = "macos")]
- converter: None,
+ convert: None,
+ #[cfg(target_os = "macos")]
+ convert_rebuilds: 0,
+ #[cfg(target_os = "macos")]
+ fallback_converter: None,
+ #[cfg(target_os = "macos")]
+ fallback_rebuilds: None,
#[cfg(target_os = "macos")]
blur: None,
#[cfg(target_os = "macos")]
@@ -1761,77 +1852,38 @@ impl CameraWindow {
let Some(epoch) = Feeds::global(cx).read(cx).camera_preview_epoch() else {
return;
};
- use core_foundation::base::TCFType as _;
- use core_video::pixel_buffer::{CVPixelBuffer, CVPixelBufferRef};
-
let failures_before = self.effect_failures;
if self.state.background_blur != BlurMode::Off && camera_blur::is_low_spec_preview() {
self.effect_failures.blur = true;
}
- let blur_mode = self.active_blur_mode();
- let max_dims = blur_mode.is_some().then_some(camera_blur::BLUR_MAX_DIMS);
- if let Some(converted) = frame::FrameConverter::convert(
- &mut self.converter,
- &frame,
- max_dims,
- self.state.mirrored,
- ) {
- self.effect_failures.mirror = self.state.mirrored && !converted.mirrored;
- let first_frame = self.frame_dims.is_none();
- let dims = converted.dims;
- let dims_changed = self.frame_dims != Some(dims);
- self.frame_dims = Some(dims);
-
- let mut paint_raw = blur_mode.is_none();
- if let Some(mode) = blur_mode {
- let job = camera_blur::BlurJob {
- buffer: camera_blur::SendPixelBuf(converted.buffer.clone()),
- width: dims.0 as u32,
- height: dims.1 as u32,
- ring_generation: converted.generation,
- mode,
- };
- match self.ensure_blur_bridge(epoch, window, cx).tx.try_send(job) {
- Ok(()) => {}
- // Worker busy: drop this frame and keep the last
- // painted one -- the bounded(1) latest-wins shape of
- // the Tauri preview's `camera_tx` (`camera.rs:424`).
- Err(flume::TrySendError::Full(_)) => {}
- Err(flume::TrySendError::Disconnected(_)) => {
- // The worker could not come up (no device, no
- // ONNX runtime, low-level failure). Same degrade
- // as `ensure_blur_processor` returning false:
- // raw frames, latched until the mode changes.
- tracing::warn!(
- "camera blur worker unavailable; preview continues unblurred"
- );
- self.blur = None;
- self.effect_failures.blur = true;
- paint_raw = true;
- }
- }
- }
-
- if paint_raw {
- let raw =
- converted.buffer.as_ref() as *const cidre::cv::PixelBuf as CVPixelBufferRef;
- let buffer = unsafe { CVPixelBuffer::wrap_under_get_rule(raw) };
- self.preview
- .update(cx, |preview, cx| preview.set_frame(buffer, dims, cx));
- }
- if dims_changed {
- self.apply_window_size(window, cx);
- cx.notify();
- }
- if first_frame && !window.is_window_active() {
- window.refresh();
- }
- } else if self.frame_dims.is_none() && self.frames_in_window == 0 {
- tracing::warn!("camera frame could not be converted for preview");
- }
if failures_before != self.effect_failures {
self.sync_effect_feedback(cx);
}
+ let job = ConvertJob {
+ frame,
+ settings: self.convert_settings(epoch),
+ };
+ match self.ensure_convert_bridge(window, cx).tx.try_send(job) {
+ // A full queue means the thread is mid-conversion; it picks up
+ // the next frame.
+ Ok(()) | Err(flume::TrySendError::Full(_)) => {}
+ Err(flume::TrySendError::Disconnected(job)) => {
+ let settings = job.settings;
+ let max_dims = settings.blur_mode.map(|_| camera_blur::BLUR_MAX_DIMS);
+ let force_rebuild = self
+ .fallback_rebuilds
+ .is_some_and(|rebuilds| rebuilds != settings.rebuilds);
+ self.fallback_rebuilds = Some(settings.rebuilds);
+ let converted = frame::FrameConverter::convert(
+ &mut self.fallback_converter,
+ &job.frame,
+ max_dims,
+ settings.mirrored,
+ force_rebuild,
+ );
+ self.converted_arrived(converted, settings, window, cx);
+ }
+ }
}
#[cfg(not(target_os = "macos"))]
{
@@ -1899,6 +1951,128 @@ impl CameraWindow {
}
}
+ #[cfg(target_os = "macos")]
+ fn convert_settings(&self, epoch: u64) -> ConvertSettings {
+ ConvertSettings {
+ epoch,
+ blur_mode: self.active_blur_mode(),
+ mirrored: self.state.mirrored,
+ rebuilds: self.convert_rebuilds,
+ }
+ }
+
+ #[cfg(target_os = "macos")]
+ fn converted_arrived(
+ &mut self,
+ converted: Option<frame::Converted>,
+ settings: ConvertSettings,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ let Some(epoch) = Feeds::global(cx).read(cx).camera_preview_epoch() else {
+ return;
+ };
+ if settings != self.convert_settings(epoch) {
+ return;
+ }
+ let blur_mode = settings.blur_mode;
+ use core_foundation::base::TCFType as _;
+ use core_video::pixel_buffer::{CVPixelBuffer, CVPixelBufferRef};
+
+ let failures_before = self.effect_failures;
+ if let Some(converted) = converted {
+ self.effect_failures.mirror = self.state.mirrored && !converted.mirrored;
+ let first_frame = self.frame_dims.is_none();
+ let dims = converted.dims;
+ let dims_changed = self.frame_dims != Some(dims);
+ self.frame_dims = Some(dims);
+
+ let mut paint_raw = blur_mode.is_none();
+ if let Some(mode) = blur_mode {
+ let job = camera_blur::BlurJob {
+ buffer: camera_blur::SendPixelBuf(converted.buffer.clone()),
+ width: dims.0 as u32,
+ height: dims.1 as u32,
+ ring_generation: converted.generation,
+ mode,
+ };
+ match self.ensure_blur_bridge(epoch, window, cx).tx.try_send(job) {
+ Ok(()) => {}
+ // Worker busy: drop this frame and keep the last
+ // painted one -- the bounded(1) latest-wins shape of
+ // the Tauri preview's `camera_tx` (`camera.rs:424`).
+ Err(flume::TrySendError::Full(_)) => {}
+ Err(flume::TrySendError::Disconnected(_)) => {
+ // The worker could not come up (no device, no
+ // ONNX runtime, low-level failure). Same degrade
+ // as `ensure_blur_processor` returning false:
+ // raw frames, latched until the mode changes.
+ tracing::warn!(
+ "camera blur worker unavailable; preview continues unblurred"
+ );
+ self.blur = None;
+ self.effect_failures.blur = true;
+ paint_raw = true;
+ }
+ }
+ }
+
+ if paint_raw {
+ let raw =
+ converted.buffer.as_ref() as *const cidre::cv::PixelBuf as CVPixelBufferRef;
+ let buffer = unsafe { CVPixelBuffer::wrap_under_get_rule(raw) };
+ self.preview
+ .update(cx, |preview, cx| preview.set_frame(buffer, dims, cx));
+ }
+ if dims_changed {
+ self.apply_window_size(window, cx);
+ cx.notify();
+ }
+ if first_frame && !window.is_window_active() {
+ window.refresh();
+ }
+ } else if self.frame_dims.is_none() && self.frames_in_window == 0 {
+ tracing::warn!("camera frame could not be converted for preview");
+ }
+ if failures_before != self.effect_failures {
+ self.sync_effect_feedback(cx);
+ }
+ }
+
+ #[cfg(target_os = "macos")]
+ fn ensure_convert_bridge(&mut self, window: &Window, cx: &mut Context<Self>) -> &ConvertBridge {
+ if self.convert.is_none() {
+ let (job_tx, job_rx) = flume::bounded::<ConvertJob>(1);
+ let (out_tx, out_rx) = flume::bounded(1);
+ if let Err(error) = std::thread::Builder::new()
+ .name("camera-preview-convert".into())
+ .stack_size(crate::MEDIA_THREAD_STACK_SIZE)
+ .spawn(move || run_preview_conversion(job_rx, out_tx))
+ {
+ // job_rx is gone; the caller's try_send sees Disconnected and
+ // converts inline.
+ tracing::warn!("camera preview conversion thread failed to spawn: {error}");
+ }
+ let pump = cx.spawn_in(window, async move |this, cx| {
+ while let Ok((converted, settings)) = out_rx.recv_async().await {
+ if this
+ .update_in(cx, |this: &mut CameraWindow, window, cx| {
+ this.converted_arrived(converted, settings, window, cx)
+ })
+ .is_err()
+ {
+ break;
+ }
+ }
+ });
+ self.convert = Some(ConvertBridge {
+ tx: job_tx,
+ _pump: pump,
+ });
+ }
+ self.convert.as_ref().expect("just ensured")
+ }
+
#[cfg(target_os = "macos")]
fn ensure_blur_bridge(
&mut self,
@@ -2027,7 +2201,7 @@ impl CameraWindow {
{
self.effect_failures.reset_changed(before, self.state);
if before.mirrored != self.state.mirrored {
- self.converter = None;
+ self.convert_rebuilds += 1;
}
self.sync_effect_feedback(cx);
if self.state.background_blur == BlurMode::Off {
diff --git a/apps/desktop-gpui/src/controls_window.rs b/apps/desktop-gpui/src/controls_window.rs
index 4636cf7e0..b32e98985 100644
--- a/apps/desktop-gpui/src/controls_window.rs
+++ b/apps/desktop-gpui/src/controls_window.rs
@@ -10,13 +10,13 @@
use std::time::Duration;
use gpui::{
- Context, Entity, FontWeight, InteractiveElement as _, IntoElement, MouseButton,
- ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, Styled, Window, div,
- prelude::FluentBuilder as _, px, svg,
+ AppContext as _, Context, Entity, FontWeight, InteractiveElement as _, IntoElement,
+ MouseButton, ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, Styled,
+ Window, div, prelude::FluentBuilder as _, px, svg,
};
use crate::{
- feeds::{self, Feeds},
+ feeds::Feeds,
recording::RecordingMode,
session::{Phase, RecordingSession},
theme::Theme,
@@ -33,9 +33,7 @@ pub struct ControlsWindow {
/// platform, so the tick both notifies and asks for a frame explicitly.
_tick: gpui::Task<()>,
ticking_live: bool,
- /// The mic level bar's fill in device pixels as last drawn, `None` while
- /// the bar is hidden.
- drawn_level_px: std::cell::Cell<Option<u32>>,
+ level_bar: Entity<ui::MicrophoneLevel>,
}
#[derive(Clone, Copy)]
@@ -71,6 +69,15 @@ impl ControlsWindow {
cx.notify();
})
.detach();
+ let mic_level = Feeds::global(cx).read(cx).mic_level().clone();
+ let level_bar = cx.new(|cx| {
+ ui::MicrophoneLevel::new(
+ ui::MicrophoneLevelKind::RecordingBar,
+ &mic_level,
+ window,
+ cx,
+ )
+ });
let mut this = Self {
session,
@@ -80,7 +87,7 @@ impl ControlsWindow {
confirmation_pending: false,
_tick: gpui::Task::ready(()),
ticking_live: false,
- drawn_level_px: std::cell::Cell::new(None),
+ level_bar,
};
this.ticking_live = this.timer_is_live(cx);
this._tick = Self::spawn_tick(window, cx);
@@ -121,13 +128,6 @@ impl ControlsWindow {
})
}
- /// Whether a new mic level would move the bar by at least a device pixel.
- pub fn level_bar_moves(&self, db: f64, scale: f32) -> bool {
- self.drawn_level_px
- .get()
- .is_some_and(|drawn| drawn != level_bar_px(db, scale))
- }
-
/// `formatTime`: `M:SS`, hours folded into minutes the same way.
fn format_elapsed(elapsed: Duration) -> String {
let total = elapsed.as_secs();
@@ -231,17 +231,21 @@ impl ControlsWindow {
/// gray-10 track). In instant mode with a live mic it doubles as the mute
/// button -- studio deliberately does not expose mute, since the mic is an
/// editable track and muted spans would bake zeros in.
- fn render_microphone(&self, scale: f32, cx: &mut Context<Self>) -> impl IntoElement {
+ fn render_microphone(&self, cx: &mut Context<Self>) -> impl IntoElement {
let theme = self.theme;
let session = self.session.read(cx);
let muted = session.mic_muted;
let can_mute = session.mode() == Some(RecordingMode::Instant)
&& session.has_microphone()
&& matches!(session.phase, Phase::Recording { .. });
- let level_db = Feeds::global(cx).read(cx).mic_level_db;
- let level = feeds::bar_level(level_db);
- self.drawn_level_px
- .set((self.has_microphone && !muted).then(|| level_bar_px(level_db, scale)));
+ self.level_bar.update(cx, |bar, cx| {
+ bar.configure(
+ self.has_microphone && !muted,
+ theme.blue_9.into(),
+ theme.gray_10.into(),
+ cx,
+ );
+ });
let icon = if !self.has_microphone || muted {
"icons/mic-off.svg"
@@ -277,26 +281,7 @@ impl ControlsWindow {
})
.child(svg().path(icon).size(px(20.)).text_color(icon_color))
.when(self.has_microphone && !muted, |this| {
- this.child(
- div()
- .absolute()
- .bottom(px(4.))
- .left(px(4.))
- .right(px(4.))
- .h(px(2.))
- .rounded_full()
- .overflow_hidden()
- .bg(theme.gray_10)
- .child(
- div()
- .absolute()
- .left_0()
- .top_0()
- .bottom_0()
- .w(gpui::relative(level as f32))
- .bg(theme.blue_9),
- ),
- )
+ this.child(self.level_bar.clone())
})
}
@@ -376,7 +361,7 @@ impl ControlsWindow {
.detach();
}
- fn render_bar(&mut self, scale: f32, cx: &mut Context<Self>) -> impl IntoElement {
+ fn render_bar(&mut self, cx: &mut Context<Self>) -> impl IntoElement {
let theme = self.theme;
let session = self.session.read(cx);
let paused = session.is_paused();
@@ -434,7 +419,7 @@ impl ControlsWindow {
.items_center()
.gap(px(2.))
.flex_shrink_0()
- .child(self.render_microphone(scale, cx))
+ .child(self.render_microphone(cx))
.child(
self.action_button(
"pause",
@@ -559,7 +544,7 @@ impl Render for ControlsWindow {
.child(recording_issue_text(issue)),
)
})
- .child(self.render_bar(window.scale_factor(), cx))
+ .child(self.render_bar(cx))
}
}
@@ -582,11 +567,6 @@ fn recording_issue_text(error: String) -> impl IntoElement + Styled {
})
}
-/// The bar's track is 20pt wide.
-fn level_bar_px(db: f64, scale: f32) -> u32 {
- (feeds::bar_level(db) * 20.0 * f64::from(scale)).round() as u32
-}
-
fn tick_delay(live: bool, elapsed: Duration) -> Duration {
if live {
until_next_second(elapsed)
@@ -629,15 +609,4 @@ mod repaint_tests {
Duration::from_secs(1)
);
}
-
- #[test]
- fn level_changes_under_a_device_pixel_do_not_move_the_bar() {
- assert_eq!(level_bar_px(-30.0, 2.0), level_bar_px(-30.5, 2.0));
- assert_ne!(level_bar_px(-30.0, 2.0), level_bar_px(-32.0, 2.0));
- assert_eq!(level_bar_px(-30.0, 1.0), level_bar_px(-31.0, 1.0));
- assert_ne!(level_bar_px(-30.0, 1.0), level_bar_px(-33.0, 1.0));
- assert_eq!(level_bar_px(-96.0, 2.0), 0);
- assert_eq!(level_bar_px(0.0, 1.0), 20);
- assert_eq!(level_bar_px(0.0, 2.0), 40);
- }
}
diff --git a/apps/desktop-gpui/src/devices.rs b/apps/desktop-gpui/src/devices.rs
index 6f6995139..8489d636f 100644
--- a/apps/desktop-gpui/src/devices.rs
+++ b/apps/desktop-gpui/src/devices.rs
@@ -124,6 +124,24 @@ impl WindowOption {
(None, None) => None,
}
}
+
+ /// Whether two entries draw the same card. `position` only feeds the
+ /// thumbnail signature, so a window that moved still draws the same card.
+ pub fn same_card(&self, other: &Self) -> bool {
+ let Self {
+ id,
+ label,
+ app,
+ size,
+ position: _,
+ refresh_rate,
+ } = self;
+ *id == other.id
+ && *label == other.label
+ && *app == other.app
+ && *size == other.size
+ && *refresh_rate == other.refresh_rate
+ }
}
#[derive(Debug, Clone, Default, PartialEq)]
@@ -135,6 +153,28 @@ pub struct DeviceSnapshot {
}
impl DeviceSnapshot {
+ /// Store a fresh display list. Returns whether any card changed.
+ pub fn install_displays(&mut self, displays: Vec<DisplayOption>) -> bool {
+ if self.displays == displays {
+ return false;
+ }
+ self.displays = displays;
+ true
+ }
+
+ /// Store a fresh window list. Returns whether any card changed. The list is
+ /// stored either way so the thumbnail signature sees moved windows.
+ pub fn install_windows(&mut self, windows: Vec<WindowOption>) -> bool {
+ let changed = self.windows.len() != windows.len()
+ || self
+ .windows
+ .iter()
+ .zip(&windows)
+ .any(|(current, next)| !current.same_card(next));
+ self.windows = windows;
+ changed
+ }
+
pub fn update_camera_formats(
&mut self,
queried: &CameraOption,
@@ -231,6 +271,122 @@ impl InputSnapshot {
}
}
+#[cfg(test)]
+mod target_list_tests {
+ use super::*;
+ use std::str::FromStr;
+
+ fn window(id: &str, label: &str) -> WindowOption {
+ WindowOption {
+ id: WindowId::from_str(id).unwrap(),
+ label: label.into(),
+ app: "Cap".into(),
+ size: Some((1920, 1080)),
+ position: Some((0., 0.)),
+ refresh_rate: Some(60.),
+ }
+ }
+
+ fn display(id: &str, label: &str) -> DisplayOption {
+ DisplayOption {
+ id: DisplayId::from_str(id).unwrap(),
+ label: label.into(),
+ refresh_rate: 60.,
+ }
+ }
+
+ #[test]
+ fn unchanged_display_list_does_not_repaint() {
+ let list = vec![display("1", "Built-in"), display("2", "Studio Display")];
+ let mut snapshot = DeviceSnapshot::default();
+ assert!(snapshot.install_displays(list.clone()));
+ assert!(!snapshot.install_displays(list.clone()));
+ assert_eq!(snapshot.displays, list);
+ }
+
+ #[test]
+ fn display_changes_repaint() {
+ let list = vec![display("1", "Built-in"), display("2", "Studio Display")];
+ let mut renamed = list.clone();
+ renamed[1].label = "Sidecar".into();
+ let mut slower = list.clone();
+ slower[0].refresh_rate = 30.;
+ let reordered = vec![list[1].clone(), list[0].clone()];
+ for next in [
+ renamed,
+ slower,
+ reordered,
+ vec![list[0].clone()],
+ Vec::new(),
+ ] {
+ let mut snapshot = DeviceSnapshot {
+ displays: list.clone(),
+ ..Default::default()
+ };
+ assert!(snapshot.install_displays(next.clone()));
+ assert_eq!(snapshot.displays, next);
+ }
+ }
+
+ #[test]
+ fn unchanged_window_list_does_not_repaint() {
+ let list = vec![window("1", "Main"), window("2", "Editor")];
+ let mut snapshot = DeviceSnapshot::default();
+ assert!(snapshot.install_windows(list.clone()));
+ assert!(!snapshot.install_windows(list.clone()));
+ assert_eq!(snapshot.windows, list);
+ }
+
+ #[test]
+ fn moved_window_is_stored_without_a_repaint() {
+ let list = vec![window("1", "Main"), window("2", "Editor")];
+ let mut moved = list.clone();
+ moved[1].position = Some((240., 96.));
+ let mut snapshot = DeviceSnapshot {
+ windows: list,
+ ..Default::default()
+ };
+ assert!(!snapshot.install_windows(moved.clone()));
+ assert_eq!(snapshot.windows, moved);
+ }
+
+ #[test]
+ fn window_card_changes_repaint() {
+ let list = vec![window("1", "Main"), window("2", "Editor")];
+ let mut renamed = list.clone();
+ renamed[0].label = "Untitled".into();
+ let mut other_app = list.clone();
+ other_app[1].app = "Finder".into();
+ let mut resized = list.clone();
+ resized[0].size = Some((1280, 720));
+ let mut sizeless = list.clone();
+ sizeless[0].size = None;
+ let mut slower = list.clone();
+ slower[1].refresh_rate = Some(30.);
+ let reordered = vec![list[1].clone(), list[0].clone()];
+ let mut opened = list.clone();
+ opened.push(window("3", "Settings"));
+ for next in [
+ renamed,
+ other_app,
+ resized,
+ sizeless,
+ slower,
+ reordered,
+ opened,
+ vec![list[0].clone()],
+ Vec::new(),
+ ] {
+ let mut snapshot = DeviceSnapshot {
+ windows: list.clone(),
+ ..Default::default()
+ };
+ assert!(snapshot.install_windows(next.clone()));
+ assert_eq!(snapshot.windows, next);
+ }
+ }
+}
+
#[cfg(test)]
mod input_enumeration_tests {
use super::*;
@@ -712,16 +868,6 @@ pub fn list_display_targets() -> Vec<(DisplayOption, Display)> {
.collect()
}
-/// Mirrors the picker path of `cap_recording::sources::screen_capture::list_windows`
-/// (i.e. `include_accessory_panels: false`), which collapses to: a non-empty
-/// title, not owned by the Window Server, and at window level 0.
-///
-/// The level check is what actually matters. A raw `Window::list()` on macOS is
-/// dominated by menu-bar extras and status items — Control Centre alone
-/// contributes a dozen `Item-0` windows at level 25 — so without it the picker
-/// is unusable. This is deliberately duplicated rather than imported:
-/// `cap-recording` drags in ffmpeg and the whole encode stack, which this app
-/// has no other reason to build.
pub fn list_windows() -> Vec<WindowOption> {
list_window_targets()
.into_iter()
@@ -754,17 +900,27 @@ pub fn list_window_targets() -> Vec<(WindowOption, Window)> {
// (`app_windows::begin_recording`), so offering one as a target
// records a hole. Matched by owner pid rather than title or
// bundle id -- the dev binary is unbundled, and most of our
- // windows carry no CG title at all. (The shared
- // `screen_capture::list_windows` has no such filter; the Tauri
- // main window only escapes it by being a level>0 panel.)
+ // windows carry no CG title at all.
#[cfg(target_os = "macos")]
if window.raw_handle().owner_pid() == Some(std::process::id() as i32) {
return None;
}
#[cfg(target_os = "macos")]
- if window.raw_handle().level() != Some(0) {
- return None;
+ {
+ let level = window.raw_handle().level();
+ let bundle_identifier = window.raw_handle().bundle_identifier();
+ let is_accessory_application = matches!(level, Some(level) if level > 0)
+ && window.raw_handle().is_accessory_application();
+ if !cap_recording::sources::screen_capture::is_listable_macos_window(
+ level,
+ &app,
+ bundle_identifier.as_deref(),
+ false,
+ is_accessory_application,
+ ) {
+ return None;
+ }
}
Some((
diff --git a/apps/desktop-gpui/src/editor_canvas.rs b/apps/desktop-gpui/src/editor_canvas.rs
index 14618f7be..e39582dff 100644
--- a/apps/desktop-gpui/src/editor_canvas.rs
+++ b/apps/desktop-gpui/src/editor_canvas.rs
@@ -305,12 +305,10 @@ fn overlay_drag_center(
} else {
snap_moving_rect(raw, targets, 7. / canvas.0.max(1.), 7. / canvas.1.max(1.))
};
- let x = (raw.x + dx).clamp(0., 1.);
- let y = (raw.y + dy).clamp(0., 1.);
(
XY {
- x: (x + raw.w / 2.).clamp(0., 1.),
- y: (y + raw.h / 2.).clamp(0., 1.),
+ x: (raw.x + dx + raw.w / 2.).clamp(0., 1.),
+ y: (raw.y + dy + raw.h / 2.).clamp(0., 1.),
},
guides,
)
@@ -2003,6 +2001,59 @@ mod tests {
assert!(guides.is_empty());
}
+ #[test]
+ fn overlay_drag_preserves_pointer_motion_near_top_and_left_edges() {
+ let rect = NormRect {
+ x: -0.1,
+ y: -0.05,
+ w: 0.4,
+ h: 0.3,
+ };
+ for canvas in [(800., 450.), (450., 800.), (500., 500.)] {
+ let (center, guides) = overlay_drag_center(
+ rect,
+ canvas,
+ (canvas.0 * 0.02, canvas.1 * 0.03),
+ &SnapTargets::default(),
+ true,
+ );
+ assert!((center.x - 0.12).abs() < 1e-9, "{center:?}");
+ assert!((center.y - 0.13).abs() < 1e-9, "{center:?}");
+ assert!(guides.is_empty());
+ }
+ }
+
+ #[test]
+ fn overlay_drag_clamps_the_center_at_every_frame_edge() {
+ let rect = NormRect {
+ x: 0.3,
+ y: 0.35,
+ w: 0.4,
+ h: 0.3,
+ };
+ for (delta, expected) in [((-900., -1600.), 0.), ((900., 1600.), 1.)] {
+ let (center, _) =
+ overlay_drag_center(rect, (450., 800.), delta, &SnapTargets::default(), true);
+ assert!((center.x - expected).abs() < 1e-9, "{center:?}");
+ assert!((center.y - expected).abs() < 1e-9, "{center:?}");
+ }
+ }
+
+ #[test]
+ fn overlay_drag_applies_snapping_while_overhanging_the_frame() {
+ let rect = NormRect {
+ x: -0.09,
+ y: -0.1,
+ w: 0.2,
+ h: 0.4,
+ };
+ let targets = build_snap_targets(&[], None);
+ let (center, guides) = overlay_drag_center(rect, (450., 800.), (0., 0.), &targets, false);
+ assert!(center.x.abs() < 1e-9, "{center:?}");
+ assert!((center.y - 0.1).abs() < 1e-9, "{center:?}");
+ assert!(guides.iter().any(|guide| guide.axis == GuideAxis::V));
+ }
+
#[test]
fn the_display_clamp_lets_half_the_card_hang_out() {
let rect = NormRect {
diff --git a/apps/desktop-gpui/src/editor_clips.rs b/apps/desktop-gpui/src/editor_clips.rs
index 51c4bfa52..81cc72e22 100644
--- a/apps/desktop-gpui/src/editor_clips.rs
+++ b/apps/desktop-gpui/src/editor_clips.rs
@@ -1932,6 +1932,7 @@ impl PreparedMp4Import {
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
Ok(Self {
target,
diff --git a/apps/desktop-gpui/src/editor_crop.rs b/apps/desktop-gpui/src/editor_crop.rs
index e9bfd7b82..ec1f860da 100644
--- a/apps/desktop-gpui/src/editor_crop.rs
+++ b/apps/desktop-gpui/src/editor_crop.rs
@@ -1081,6 +1081,15 @@ pub struct CropState {
}
impl CropState {
+ /// The display frame is a full-resolution decode and the window's atlas
+ /// keeps every painted image until it is told otherwise, so a closed crop
+ /// would otherwise stay resident until the editor window closes.
+ pub fn release_frame(&mut self, window: &mut Window) {
+ if let Some(frame) = self.frame.take() {
+ let _ = window.drop_image(frame);
+ }
+ }
+
/// `computeInitialBounds` (`:483-517`) at open, with the container the
/// viewport gives.
pub fn new(target: (u32, u32), box_size: (f32, f32), initial: CropBounds) -> Self {
@@ -1256,7 +1265,12 @@ impl CropState {
CropField::Width => real.width = value,
CropField::Height => real.height = value,
}
- let raw = bounds_to_raw(real, self.scale());
+ let mut raw = bounds_to_raw(real, self.scale());
+ if let CropField::Height = field
+ && let Some(ratio) = self.ratio_value()
+ {
+ raw.width = raw.height * ratio;
+ }
self.set_raw_constraining(raw, ORIGIN_TOP_LEFT);
}
@@ -1606,9 +1620,10 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- if self.crop.take().is_none() {
+ let Some(mut state) = self.crop.take() else {
return;
- }
+ };
+ state.release_frame(window);
tracing::info!(reason, "crop cancelled");
self.publish_project();
cx.notify();
@@ -1630,6 +1645,7 @@ impl EditorWindow {
cx.notify();
return;
}
+ state.release_frame(window);
let bounds = state.real();
let crop = Crop {
position: XY::new(bounds.x.max(0.) as u32, bounds.y.max(0.) as u32),
@@ -2154,12 +2170,16 @@ impl EditorWindow {
Some(task) => task.await.ok().flatten(),
None => None,
};
- this.update(cx, |this: &mut Self, cx| {
+ this.update_in(cx, |this: &mut Self, window, cx| {
let Some(state) = this.crop.as_mut() else {
return;
};
match image {
- Some(image) => state.frame = Some(image),
+ Some(image) => {
+ if let Some(previous) = state.frame.replace(image) {
+ let _ = window.drop_image(previous);
+ }
+ }
None => {
tracing::warn!("crop: display frame decode failed");
state.frame_failed = true;
@@ -3910,6 +3930,56 @@ mod tests {
assert_eq!(real.height, 1964.);
}
+ #[test]
+ fn locked_size_fields_resize_from_the_edited_dimension() {
+ for (ratio, width, height) in [((16, 9), 640., 360.), ((9, 16), 360., 640.)] {
+ let mut state = CropState::new(
+ (1920, 1080),
+ (482., 272.),
+ CropBounds::new(160., 80., 960., 540.),
+ );
+ state.set_aspect(Some(ratio));
+ state.stop_animation();
+ let before = state.real();
+
+ state.set_property(CropField::Height, height);
+ assert_eq!(
+ state.real(),
+ CropBounds::new(before.x, before.y, width, height)
+ );
+
+ state.set_property(CropField::Width, width / 2.);
+ assert_eq!(
+ state.real(),
+ CropBounds::new(before.x, before.y, width / 2., height / 2.)
+ );
+ }
+ }
+
+ #[test]
+ fn locked_height_edits_still_fit_inside_the_source() {
+ let mut state = CropState::new(
+ (1920, 1080),
+ (482., 272.),
+ CropBounds::new(160., 80., 960., 540.),
+ );
+ state.set_aspect(Some((16, 9)));
+ state.stop_animation();
+ state.set_property(CropField::Height, 2160.);
+ assert_eq!(state.real(), CropBounds::new(0., 0., 1920., 1080.));
+ }
+
+ #[test]
+ fn free_height_edits_preserve_width_and_position() {
+ let mut state = CropState::new(
+ (1920, 1080),
+ (482., 272.),
+ CropBounds::new(160., 80., 960., 540.),
+ );
+ state.set_property(CropField::Height, 360.);
+ assert_eq!(state.real(), CropBounds::new(160., 80., 960., 360.));
+ }
+
#[test]
fn fill_and_reset_walk_between_the_whole_frame_and_the_opening_crop() {
let initial = CropBounds::new(600., 400., 1200., 800.);
diff --git a/apps/desktop-gpui/src/editor_edits.rs b/apps/desktop-gpui/src/editor_edits.rs
index f2df27fdd..8206e376b 100644
--- a/apps/desktop-gpui/src/editor_edits.rs
+++ b/apps/desktop-gpui/src/editor_edits.rs
@@ -198,6 +198,13 @@ impl ProjectHistory {
pub fn resume(&mut self, config: &ProjectConfiguration) {
self.paused = self.paused.saturating_sub(1);
if self.paused == 0 && std::mem::take(&mut self.dirty) {
+ let current = &self.entries[self.entries.len() - self.count - 1];
+ if matches!(
+ (serde_json::to_value(current), serde_json::to_value(config)),
+ (Ok(before), Ok(after)) if before == after
+ ) {
+ return;
+ }
self.record(config);
}
}
@@ -437,7 +444,6 @@ pub fn trim_start_bounds(
}
/// `SegmentHandle position="end"`'s setup (`TL/MaskTrack.tsx:499-508`):
-/// `minValue = start + minDuration`, `maxValue = max(minValue, nextStart)`.
pub fn trim_end_bounds(
segments: &[Segment],
lane: u32,
@@ -446,11 +452,11 @@ pub fn trim_end_bounds(
total: f64,
) -> DragBounds {
let (_, next_start) = neighbours(segments, lane, index, total);
- let min = segments[index].start + min_duration;
- DragBounds {
- min,
- max: min.max(next_start),
- }
+ let max = next_start.max(segments[index].end);
+ let min = (segments[index].start + min_duration)
+ .min(segments[index].end)
+ .min(max);
+ DragBounds { min, max }
}
pub fn audio_trim_start_bounds(segment: &AudioTrackSegment, bounds: DragBounds) -> DragBounds {
@@ -514,7 +520,7 @@ pub trait TrackSegmentOps: Clone {
/// time, end: segment.end}`.
fn split_tail(&self, at: f64) -> Self {
let mut tail = self.clone();
- tail.set_start(self.start() + at);
+ tail.trim_start_to(self.start() + at);
tail
}
@@ -586,7 +592,39 @@ pub fn fit_camera3d_motion(segment: &mut Camera3DSegment) {
}
}
}
-impl_track_segment!(MaskSegment, lane: track);
+
+impl TrackSegmentOps for MaskSegment {
+ fn start(&self) -> f64 {
+ self.start
+ }
+ fn end(&self) -> f64 {
+ self.end
+ }
+ fn set_start(&mut self, value: f64) {
+ self.start = value;
+ }
+ fn set_end(&mut self, value: f64) {
+ self.end = value;
+ }
+ fn trim_start_to(&mut self, value: f64) {
+ let shift = self.start - value;
+ // Off-segment keys still anchor interpolation at the trimmed edges.
+ for time in self
+ .keyframes
+ .position
+ .iter_mut()
+ .chain(&mut self.keyframes.size)
+ .map(|key| &mut key.time)
+ .chain(self.keyframes.intensity.iter_mut().map(|key| &mut key.time))
+ {
+ *time += shift;
+ }
+ self.start = value;
+ }
+ fn lane(&self) -> u32 {
+ self.track
+ }
+}
impl_track_segment!(TextSegment, lane: track);
impl_track_segment!(cap_project::StyleSegment, lane: track);
impl_track_segment!(cap_project::ImageSegment, lane: track);
@@ -1262,7 +1300,6 @@ fn ripple_relative_keyframes<T>(
keyframes: &mut Vec<T>,
old_start: f64,
new_start: f64,
- new_end: f64,
cut_start: f64,
cut_end: f64,
shift: f64,
@@ -1279,7 +1316,7 @@ fn ripple_relative_keyframes<T>(
} else {
absolute
};
- *value = (mapped - new_start).clamp(0.0, new_end - new_start);
+ *value = mapped - new_start;
true
});
}
@@ -1291,6 +1328,14 @@ fn ripple_delete_mask_track(
shift: f64,
) {
segments.retain_mut(|segment| {
+ if segment.end <= cut_start {
+ return true;
+ }
+ if segment.start >= cut_end {
+ segment.start -= shift;
+ segment.end -= shift;
+ return true;
+ }
let old_start = segment.start;
let Some((new_start, new_end)) =
ripple_deleted_bounds(old_start, segment.end, cut_start, cut_end, shift)
@@ -1301,7 +1346,6 @@ fn ripple_delete_mask_track(
&mut segment.keyframes.position,
old_start,
new_start,
- new_end,
cut_start,
cut_end,
shift,
@@ -1311,7 +1355,6 @@ fn ripple_delete_mask_track(
&mut segment.keyframes.size,
old_start,
new_start,
- new_end,
cut_start,
cut_end,
shift,
@@ -1321,7 +1364,6 @@ fn ripple_delete_mask_track(
&mut segment.keyframes.intensity,
old_start,
new_start,
- new_end,
cut_start,
cut_end,
shift,
@@ -2960,6 +3002,34 @@ mod tests {
assert!(!clean.can_undo());
}
+ #[test]
+ fn a_drag_returning_to_its_start_keeps_undo_and_redo_unchanged() {
+ let initial = ProjectConfiguration::default();
+ let mut moved = initial.clone();
+ moved.background.padding += 1.0;
+ let mut future = initial.clone();
+ future.background.padding += 2.0;
+ let mut history = ProjectHistory::new(initial.clone());
+ history.pause();
+ history.record(&moved);
+ history.record(&initial);
+ history.resume(&initial);
+ assert_eq!(history.depth(), 1);
+ assert!(!history.can_undo());
+
+ history.record(&future);
+ history.undo();
+ history.pause();
+ history.record(&moved);
+ history.record(&initial);
+ history.resume(&initial);
+ assert!(history.can_redo());
+ assert_eq!(
+ history.redo().unwrap().background.padding,
+ future.background.padding
+ );
+ }
+
#[test]
fn the_stack_is_capped_at_a_hundred_entries() {
let mut history = ProjectHistory::new(with_zoom_count(0));
@@ -3153,6 +3223,58 @@ mod tests {
assert_eq!(bounds.clamp(4.), 0.);
}
+ #[test]
+ fn zoom_end_resize_cannot_cross_a_neighbour_when_zoomed_out() {
+ let mut project = zoom_fixture();
+ let timeline = project.timeline.as_mut().unwrap();
+ timeline.zoom_segments[1].start = 6.;
+ let model = TimelineModel::build(&project, false, false);
+ let zoom = segments(&model, TrackKind::Zoom);
+ let floor = min_segment_duration(TrackKind::Zoom, 0.2);
+ let bounds = trim_end_bounds(&zoom, 0, 0, floor, 30.);
+
+ assert_eq!(bounds, DragBounds { min: 5., max: 6. });
+ let timeline = project.timeline.as_mut().unwrap();
+ set_segment_end(timeline, TrackKind::Zoom, 0, bounds.clamp(4.));
+ assert_eq!(timeline.zoom_segments[0].end, 5.);
+ for requested_end in [5.5, 12.] {
+ set_segment_end(timeline, TrackKind::Zoom, 0, bounds.clamp(requested_end));
+ assert!(timeline.zoom_segments[0].end <= timeline.zoom_segments[1].start);
+ }
+ }
+
+ #[test]
+ fn zoom_end_resize_cannot_cross_the_timeline_end_when_zoomed_out() {
+ let mut project = zoom_fixture();
+ let model = TimelineModel::build(&project, false, false);
+ let zoom = segments(&model, TrackKind::Zoom);
+ let floor = min_segment_duration(TrackKind::Zoom, 0.5);
+ let bounds = trim_end_bounds(&zoom, 0, 1, floor, 30.);
+
+ assert_eq!(bounds, DragBounds { min: 24., max: 30. });
+ let timeline = project.timeline.as_mut().unwrap();
+ for requested_end in [23., 28., 40.] {
+ set_segment_end(timeline, TrackKind::Zoom, 1, bounds.clamp(requested_end));
+ assert!(timeline.zoom_segments[1].end <= timeline.duration());
+ }
+ }
+
+ #[test]
+ fn zoom_end_resize_preserves_a_range_left_beyond_a_shortened_timeline() {
+ let mut project = zoom_fixture();
+ project.timeline.as_mut().unwrap().segments[0].end = 10.;
+ let model = TimelineModel::build(&project, false, false);
+ let zoom = segments(&model, TrackKind::Zoom);
+ let floor = min_segment_duration(TrackKind::Zoom, 0.2);
+ let bounds = trim_end_bounds(&zoom, 0, 1, floor, 10.);
+
+ for requested_end in [8., 22., 30.] {
+ let end = bounds.clamp(requested_end);
+ assert!(end >= zoom[1].start);
+ assert!(end <= zoom[1].end);
+ }
+ }
+
// -- Clip trim ----------------------------------------------------------
fn two_clip_config() -> ProjectConfiguration {
@@ -3750,6 +3872,126 @@ mod tests {
assert_eq!(tail.fade_out, 0.6);
}
+ fn animated_mask() -> MaskSegment {
+ let mut mask = default_mask_segment(2.0, 10.0, 1);
+ mask.keyframes = serde_json::from_value(serde_json::json!({
+ "position": [
+ { "time": 0.0, "x": 0.2, "y": 0.3 },
+ { "time": 4.0, "x": 0.5, "y": 0.6 },
+ { "time": 8.0, "x": 0.8, "y": 0.9 }
+ ],
+ "size": [
+ { "time": 0.0, "x": 0.2, "y": 0.3 },
+ { "time": 4.0, "x": 0.4, "y": 0.5 },
+ { "time": 8.0, "x": 0.6, "y": 0.7 }
+ ],
+ "intensity": [
+ { "time": 0.0, "value": 0.2 },
+ { "time": 4.0, "value": 0.5 },
+ { "time": 8.0, "value": 0.8 }
+ ]
+ }))
+ .unwrap();
+ mask
+ }
+
+ fn mask_keyframe_times(mask: &MaskSegment) -> Vec<f64> {
+ mask.keyframes
+ .position
+ .iter()
+ .chain(&mask.keyframes.size)
+ .map(|key| key.time)
+ .chain(mask.keyframes.intensity.iter().map(|key| key.time))
+ .collect()
+ }
+
+ #[test]
+ fn mask_keyframes_keep_their_absolute_times_after_a_split() {
+ let mut project = zoom_fixture();
+ let timeline = project.timeline.as_mut().unwrap();
+ let original = animated_mask();
+ timeline.mask_segments.push(original.clone());
+
+ assert!(split_segment(timeline, TrackKind::Mask, 0, 3.0));
+
+ let head = &timeline.mask_segments[0];
+ let tail = &timeline.mask_segments[1];
+ assert_eq!((head.start, head.end), (2.0, 5.0));
+ assert_eq!((tail.start, tail.end), (5.0, 10.0));
+ assert_eq!(head.track, 1);
+ assert_eq!(tail.track, 1);
+ assert_eq!(
+ serde_json::to_value(&head.keyframes).unwrap(),
+ serde_json::to_value(&original.keyframes).unwrap()
+ );
+ assert_eq!(mask_keyframe_times(tail), [-3.0, 1.0, 5.0].repeat(3));
+ }
+
+ #[test]
+ fn mask_keyframes_keep_their_absolute_times_when_trimming_the_start() {
+ let mut project = zoom_fixture();
+ let timeline = project.timeline.as_mut().unwrap();
+ timeline.mask_segments.push(animated_mask());
+
+ for start in [5.0, 7.0, 1.0, 2.0] {
+ assert!(set_segment_start(timeline, TrackKind::Mask, 0, start));
+ let mask = &timeline.mask_segments[0];
+ let absolute_times = mask_keyframe_times(mask)
+ .into_iter()
+ .map(|time| mask.start + time)
+ .collect::<Vec<_>>();
+ assert_eq!(absolute_times, [2.0, 6.0, 10.0].repeat(3));
+ }
+ assert!(!set_segment_start(timeline, TrackKind::Mask, 0, 2.0));
+ assert!(move_segment(timeline, TrackKind::Mask, 0, 4.0, 12.0));
+ assert!(set_segment_end(timeline, TrackKind::Mask, 0, 11.0));
+ assert_eq!(
+ serde_json::to_value(&timeline.mask_segments[0].keyframes).unwrap(),
+ serde_json::to_value(&animated_mask().keyframes).unwrap()
+ );
+ }
+
+ #[test]
+ fn mask_keyframes_outside_the_segment_survive_an_unrelated_ripple_delete() {
+ for (cut_start, cut_end, expected_bounds) in
+ [(0.0, 3.0, (2.0, 6.0)), (12.0, 14.0, (5.0, 9.0))]
+ {
+ let mut mask = animated_mask();
+ mask.start = 5.0;
+ mask.end = 9.0;
+ mask.keyframes.position[0].time = -3.0;
+ mask.keyframes.size[0].time = -3.0;
+ mask.keyframes.intensity[0].time = -3.0;
+ let original = serde_json::to_value(&mask.keyframes).unwrap();
+ let mut project = zoom_fixture();
+ let timeline = project.timeline.as_mut().unwrap();
+ timeline.mask_segments.push(mask);
+
+ ripple_delete_output_tracks(timeline, cut_start, cut_end, cut_end - cut_start);
+
+ assert_eq!(
+ serde_json::to_value(&timeline.mask_segments[0].keyframes).unwrap(),
+ original
+ );
+ let mask = &timeline.mask_segments[0];
+ assert_eq!((mask.start, mask.end), expected_bounds);
+ }
+ }
+
+ #[test]
+ fn mask_keyframes_before_an_overlapping_ripple_delete_keep_their_times() {
+ let mut project = zoom_fixture();
+ let timeline = project.timeline.as_mut().unwrap();
+ timeline.mask_segments.push(animated_mask());
+ assert!(set_segment_start(timeline, TrackKind::Mask, 0, 5.0));
+
+ ripple_delete_output_tracks(timeline, 8.0, 9.0, 1.0);
+
+ let mask = &timeline.mask_segments[0];
+ assert_eq!((mask.start, mask.end), (5.0, 9.0));
+ assert_eq!(mask_keyframe_times(mask), [-3.0, 1.0, 4.0].repeat(3));
+ }
+
// -- Split snapping -----------------------------------------------------
#[test]
diff --git a/apps/desktop-gpui/src/editor_export.rs b/apps/desktop-gpui/src/editor_export.rs
index acab035ad..77498a410 100644
--- a/apps/desktop-gpui/src/editor_export.rs
+++ b/apps/desktop-gpui/src/editor_export.rs
@@ -373,6 +373,18 @@ impl ExportUi {
.is_some_and(|bpp| (bpp - self.compression.bits_per_pixel()).abs() > 0.001)
}
+ fn complete_share_export(&mut self, link: String, copy_link: impl FnOnce(String)) {
+ if self.close_requested || self.cancel.load(Ordering::Relaxed) {
+ self.phase = ExportPhase::Idle;
+ return;
+ }
+
+ self.phase = ExportPhase::Done;
+ self.share_link = Some(link.clone());
+ self.upload_progress = 1.0;
+ copy_link(link);
+ }
+
fn clipboard_retry_path(&self) -> Option<&Path> {
if self.phase == ExportPhase::Failed && self.destination == ExportDestination::Clipboard {
self.output_path.as_deref()
@@ -931,7 +943,11 @@ impl EditorWindow {
});
loop {
+ let mut latest: Option<(u32, u32)> = None;
while let Ok((rendered, total)) = progress_rx.try_recv() {
+ latest = Some((rendered, total));
+ }
+ if let Some((rendered, total)) = latest {
let _ = this.update(cx, |this, cx| {
if let Some(ui) = this.export.as_mut() {
ui.rendered = rendered;
@@ -1172,7 +1188,11 @@ impl EditorWindow {
});
loop {
+ let mut latest: Option<(u32, u32)> = None;
while let Ok((rendered, total)) = progress_rx.try_recv() {
+ latest = Some((rendered, total));
+ }
+ if let Some((rendered, total)) = latest {
let _ = this.update(cx, |this, cx| {
if let Some(ui) = this.export.as_mut() {
ui.rendered = rendered;
@@ -1217,7 +1237,11 @@ impl EditorWindow {
});
loop {
+ let mut latest: Option<f64> = None;
while let Ok(progress) = upload_rx.try_recv() {
+ latest = Some(progress);
+ }
+ if let Some(progress) = latest {
let _ = this.update(cx, |this, cx| {
if let Some(ui) = this.export.as_mut() {
ui.upload_progress = progress as f32;
@@ -1243,16 +1267,15 @@ impl EditorWindow {
this.sharing = meta.sharing;
}
if let Some(ui) = this.export.as_mut() {
- ui.phase = ExportPhase::Done;
- ui.share_link = Some(link.clone());
- ui.upload_progress = 1.0;
+ ui.complete_share_export(link, |link| {
+ cx.write_to_clipboard(gpui::ClipboardItem::new_string(
+ link,
+ ));
+ crate::app_sounds::play_notification();
+ });
}
cx.notify();
});
- let _ = this.update(cx, |_, cx| {
- cx.write_to_clipboard(gpui::ClipboardItem::new_string(link));
- crate::app_sounds::play_notification();
- });
}
Ok(Ok(crate::upload::UploadResult::NotAuthenticated)) => {
let _ = this.update(cx, |this, _| {
@@ -3084,6 +3107,44 @@ mod tests {
}
}
+ #[test]
+ fn completed_share_export_copies_the_link_and_shows_success() {
+ let mut ui = clipboard_export();
+ ui.destination = ExportDestination::Link;
+ ui.phase = ExportPhase::Uploading;
+ ui.upload_progress = 0.9;
+ let link = "https://cap.link/finished".to_string();
+ let mut copied = None;
+
+ ui.complete_share_export(link.clone(), |link| copied = Some(link));
+
+ assert_eq!(ui.phase, ExportPhase::Done);
+ assert_eq!(ui.share_link, Some(link.clone()));
+ assert_eq!(ui.upload_progress, 1.0);
+ assert_eq!(copied, Some(link));
+ }
+
+ #[test]
+ fn cancellation_or_close_ignores_a_completed_share_export() {
+ for closing in [false, true] {
+ let mut ui = clipboard_export();
+ ui.destination = ExportDestination::Link;
+ ui.phase = ExportPhase::Uploading;
+ ui.upload_progress = 0.9;
+ let completed_link = "https://cap.link/finished".to_string();
+ ui.close_requested = closing;
+ ui.cancel.store(!closing, Ordering::Relaxed);
+
+ ui.complete_share_export(completed_link, |_| {
+ panic!("cancelled share exports must not copy a link or notify")
+ });
+
+ assert_eq!(ui.phase, ExportPhase::Idle);
+ assert!(ui.share_link.is_none());
+ assert_eq!(ui.upload_progress, 0.9);
+ }
+ }
+
#[test]
fn incomplete_or_non_clipboard_exports_cannot_retry_copy() {
let mut ui = clipboard_export();
diff --git a/apps/desktop-gpui/src/editor_hover_bench.rs b/apps/desktop-gpui/src/editor_hover_bench.rs
index 4325410ad..8d5ad0630 100644
--- a/apps/desktop-gpui/src/editor_hover_bench.rs
+++ b/apps/desktop-gpui/src/editor_hover_bench.rs
@@ -6,6 +6,8 @@
//! does. Each move runs the timeline's own hover handler, then one synchronous
//! `Window::draw` (view renders + layout + prepaint + paint) is timed with it.
//! `CAP_GPUI_AUTO_SIDEBAR` picks the sidebar tab that stays open meanwhile.
+//! `CAP_GPUI_AUTO_HOVER_BENCH_SCRUB=1` holds the ruler down for the whole run,
+//! so every move is also a paused scrub step through the ruler's own handlers.
//!
//! Run:
//! CAP_GPUI_AUTO_EDITOR=<project.cap> CAP_GPUI_AUTO_HOVER_BENCH=1 \
@@ -14,7 +16,7 @@
use std::time::{Duration, Instant};
-use gpui::{App, WindowHandle};
+use gpui::{App, MouseButton, MouseDownEvent, MouseMoveEvent, WindowHandle, point, px};
use crate::editor_window::EditorWindow;
@@ -51,6 +53,24 @@ pub async fn run(handle: WindowHandle<EditorWindow>, cx: &mut gpui::AsyncApp) {
.and_then(|value| value.parse::<f32>().ok())
.filter(|secs| *secs > 0.)
.unwrap_or(DEFAULT_SWEEP_SECS);
+ let scrub = std::env::var("CAP_GPUI_AUTO_HOVER_BENCH_SCRUB").is_ok_and(|value| value == "1");
+ let pointer_x = |width: f32, phase: f32| 140. + (width - 180.).max(1.) * phase;
+ if scrub {
+ cx.update(|cx| {
+ handle
+ .update(cx, |view, window, cx| {
+ let x = pointer_x(f32::from(window.viewport_size().width), 0.);
+ let event = MouseDownEvent {
+ button: MouseButton::Left,
+ position: point(px(x), px(0.)),
+ click_count: 1,
+ ..Default::default()
+ };
+ view.timeline_mouse_down(&event, true, window, cx);
+ })
+ .ok()
+ });
+ }
let started = Instant::now();
let mut next_move = started;
let mut move_us = Vec::new();
@@ -85,9 +105,16 @@ pub async fn run(handle: WindowHandle<EditorWindow>, cx: &mut gpui::AsyncApp) {
let hovered = cx.update(|cx| {
handle
.update(cx, |view, window, cx| {
- let width = f32::from(window.viewport_size().width);
- let x = 140. + (width - 180.).max(1.) * phase;
+ let x = pointer_x(f32::from(window.viewport_size().width), phase);
view.timeline_hover(x, window, cx);
+ if scrub {
+ let event = MouseMoveEvent {
+ position: point(px(x), px(0.)),
+ pressed_button: Some(MouseButton::Left),
+ ..Default::default()
+ };
+ view.timeline_mouse_move(&event, window, cx);
+ }
})
.is_ok()
});
@@ -105,8 +132,16 @@ pub async fn run(handle: WindowHandle<EditorWindow>, cx: &mut gpui::AsyncApp) {
.zip(frames_at_start)
.map(|(end, start)| end.saturating_sub(start));
+ if scrub {
+ cx.update(|cx| {
+ handle
+ .update(cx, |view, _, cx| view.window_mouse_up(cx))
+ .ok()
+ });
+ }
println!(
- "hover-bench: sidebar tab {}, pointer at {mouse_hz}Hz crossing the timeline in {sweep_secs}s, {} moves handled and {frames:?} preview frames presented in {:.1}s",
+ "hover-bench: {}sidebar tab {}, pointer at {mouse_hz}Hz crossing the timeline in {sweep_secs}s, {} moves handled and {frames:?} preview frames presented in {:.1}s",
+ if scrub { "ruler scrub, " } else { "" },
std::env::var("CAP_GPUI_AUTO_SIDEBAR").unwrap_or_else(|_| "default".into()),
move_us.len(),
MEASURED.as_secs_f64(),
diff --git a/apps/desktop-gpui/src/editor_panels.rs b/apps/desktop-gpui/src/editor_panels.rs
index 4ad482e30..17ed08d00 100644
--- a/apps/desktop-gpui/src/editor_panels.rs
+++ b/apps/desktop-gpui/src/editor_panels.rs
@@ -668,14 +668,100 @@ pub fn pick_font_family(stack: &[&str], installed: &[String]) -> String {
.map_or_else(|| "sans-serif".to_string(), |family| (*family).to_string())
}
-/// `applyTextPreset` (`text-presets.ts:205-238`). Timing stays the user's, and
-/// so does the content unless it is still the placeholder a new segment is
-/// born with; the box is scaled with the font change about its **top** edge,
-/// exactly as the Size slider does.
-pub fn apply_text_preset(segment: &mut TextSegment, preset: &TextPreset, installed: &[String]) {
- let style = &preset.style;
+#[derive(Debug, Clone, PartialEq)]
+pub struct TextStyle {
+ pub font_family: String,
+ pub font_size: f32,
+ pub font_weight: f32,
+ pub italic: bool,
+ pub uppercase: bool,
+ pub align: TextAlign,
+ pub letter_spacing: f32,
+ pub line_height: f32,
+ pub opacity: f32,
+ pub shadow: f32,
+ pub glow: f32,
+ pub stroke_width: f32,
+ pub stroke_color: String,
+ pub background_style: TextBackgroundStyle,
+ pub background_color: Option<String>,
+ pub color: String,
+ pub gradient_color: Option<String>,
+ pub animation_in: TextAnimation,
+ pub animation_out: TextAnimation,
+ pub animation_in_duration: f64,
+ pub animation_out_duration: f64,
+ pub fade_duration: f64,
+}
+
+impl TextStyle {
+ pub fn of(segment: &TextSegment) -> Self {
+ Self {
+ font_family: segment.font_family.clone(),
+ font_size: segment.font_size,
+ font_weight: segment.font_weight,
+ italic: segment.italic,
+ uppercase: segment.uppercase,
+ align: segment.align,
+ letter_spacing: segment.letter_spacing,
+ line_height: segment.line_height,
+ opacity: segment.opacity,
+ shadow: segment.shadow,
+ glow: segment.glow,
+ stroke_width: segment.stroke_width,
+ stroke_color: segment.stroke_color.clone(),
+ background_style: segment.background_style,
+ background_color: segment.background_color.clone(),
+ color: segment.color.clone(),
+ gradient_color: segment.gradient_color.clone(),
+ animation_in: segment.animation_in,
+ animation_out: segment.animation_out,
+ animation_in_duration: segment.animation_in_duration,
+ animation_out_duration: segment.animation_out_duration,
+ fade_duration: segment.fade_duration,
+ }
+ }
+
+ pub fn apply_to(&self, segment: &mut TextSegment) -> bool {
+ let previous_style = Self::of(segment);
+ let previous_center = segment.center;
+ let previous_size = segment.size;
+ if segment.font_size != self.font_size {
+ scale_text_box(segment, self.font_size);
+ }
+ segment.font_family = self.font_family.clone();
+ segment.font_size = self.font_size;
+ segment.font_weight = self.font_weight;
+ segment.italic = self.italic;
+ segment.uppercase = self.uppercase;
+ segment.align = self.align;
+ segment.letter_spacing = self.letter_spacing;
+ segment.line_height = self.line_height;
+ segment.opacity = self.opacity;
+ segment.shadow = self.shadow;
+ segment.glow = self.glow;
+ segment.stroke_width = self.stroke_width;
+ segment.stroke_color = self.stroke_color.clone();
+ segment.background_style = self.background_style;
+ segment.background_color = self.background_color.clone();
+ segment.color = self.color.clone();
+ segment.gradient_color = self.gradient_color.clone();
+ segment.animation_in = self.animation_in;
+ segment.animation_out = self.animation_out;
+ segment.animation_in_duration = self.animation_in_duration;
+ segment.animation_out_duration = self.animation_out_duration;
+ segment.fade_duration = self.fade_duration;
+ previous_style != *self
+ || previous_center != segment.center
+ || previous_size != segment.size
+ }
+}
+
+/// Grows or shrinks the text box with a font-size change, top edge fixed,
+/// like the Size slider; the canvas overlay re-hugs the glyph bounds later.
+fn scale_text_box(segment: &mut TextSegment, font_size: f32) {
let box_scale = f64::from(
- style.font_size
+ font_size
/ if segment.font_size == 0. {
48.
} else {
@@ -686,6 +772,15 @@ pub fn apply_text_preset(segment: &mut TextSegment, preset: &TextPreset, install
segment.size.x = (segment.size.x * box_scale).min(1.);
segment.size.y *= box_scale;
segment.center.y = top_edge + segment.size.y / 2.;
+}
+
+/// `applyTextPreset` (`text-presets.ts:205-238`). Timing stays the user's, and
+/// so does the content unless it is still the placeholder a new segment is
+/// born with; the box is scaled with the font change about its **top** edge,
+/// exactly as the Size slider does.
+pub fn apply_text_preset(segment: &mut TextSegment, preset: &TextPreset, installed: &[String]) {
+ let style = &preset.style;
+ scale_text_box(segment, style.font_size);
segment.font_family = pick_font_family(style.font_stack, installed);
segment.font_size = style.font_size;
@@ -1591,6 +1686,30 @@ pub fn camera3d_pose_seek_time(segment: &Camera3DSegment, end: bool, fps: u32) -
camera3d_frame_before(segment.end, fps).max(start_frame)
}
+/// "The playhead is on this pose" is a frame's tolerance, which is as close
+/// as a seek can land.
+fn camera3d_pose_on(playhead: f64, time: f64, fps: u32) -> bool {
+ (playhead - time).abs() < 1. / f64::from(fps.max(1))
+}
+
+/// Every `(shot, end)` pose the playhead is on: what the 3D panel's pose cards
+/// mark, so a scrub can tell when the sidebar has to redraw.
+pub(crate) fn camera3d_poses_at(
+ segments: &[Camera3DSegment],
+ playhead: f64,
+ fps: u32,
+) -> Vec<(usize, bool)> {
+ let mut poses = Vec::new();
+ for (index, segment) in segments.iter().enumerate() {
+ for end in [false, true] {
+ if camera3d_pose_on(playhead, camera3d_pose_seek_time(segment, end, fps), fps) {
+ poses.push((index, end));
+ }
+ }
+ }
+ poses
+}
+
/// How many shots the Auto scene picker offers.
pub const AUTO_CAMERA3D_MAX_SHOTS: usize = 6;
@@ -2897,12 +3016,7 @@ impl EditorWindow {
};
let start = matches!(key, FieldKey::CaptionStart(_));
self.edit_caption_segment("caption-timing", index, window, cx, move |segment| {
- if start {
- segment.start = value;
- } else {
- segment.end = value;
- }
- true
+ commit_segment_timing(segment, start, &text, value)
});
}
FieldKey::KeyboardStart(index) | FieldKey::KeyboardEnd(index) => {
@@ -2914,12 +3028,7 @@ impl EditorWindow {
};
let start = matches!(key, FieldKey::KeyboardStart(_));
self.edit_keyboard_segment("keyboard-timing", index, window, cx, move |segment| {
- if start {
- segment.start = value;
- } else {
- segment.end = value;
- }
- true
+ commit_segment_timing(segment, start, &text, value)
});
}
FieldKey::SyncOffset(clip, kind) => {
@@ -2975,6 +3084,31 @@ impl EditorWindow {
}
}
+/// `text` is the field's committed text: it shows times to two decimals, so
+/// tabbing through an untouched field commits a rounded copy of the stored
+/// time, which must not count as an edit.
+fn commit_segment_timing(
+ segment: &mut impl crate::editor_edits::TrackSegmentOps,
+ start: bool,
+ text: &str,
+ value: f64,
+) -> bool {
+ let current = if start {
+ segment.start()
+ } else {
+ segment.end()
+ };
+ if text.trim() == format!("{current:.2}") || current == value {
+ return false;
+ }
+ if start {
+ segment.set_start(value);
+ } else {
+ segment.set_end(value);
+ }
+ true
+}
+
// ---------------------------------------------------------------------------
// Per-track edit helpers
// ---------------------------------------------------------------------------
@@ -6831,9 +6965,7 @@ impl EditorWindow {
let fps = crate::editor_window::EDITOR_PREVIEW_FPS;
let start_time = camera3d_pose_seek_time(segment, false, fps);
let end_time = camera3d_pose_seek_time(segment, true, fps);
- // "The playhead is on this pose" is a frame's tolerance, which is as
- // close as a seek can land.
- let on = |time: f64| (self.playhead_time() - time).abs() < 1. / f64::from(fps.max(1));
+ let on = |time: f64| camera3d_pose_on(self.playhead_time(), time, fps);
div()
.flex()
@@ -7936,6 +8068,171 @@ impl EditorWindow {
mod tests {
use super::*;
+ #[test]
+ fn unchanged_timing_fields_preserve_redo() {
+ use crate::editor_edits::ProjectHistory;
+
+ for base in [2., 2.3456] {
+ for caption in [true, false] {
+ for start in [true, false] {
+ for paused in [true, false] {
+ let mut project: cap_project::ProjectConfiguration =
+ serde_json::from_value(serde_json::json!({
+ "timeline": {
+ "segments": [],
+ "zoomSegments": [],
+ "captionSegments": [{ "id": "caption", "start": base, "end": base + 4., "text": "hello" }],
+ "keyboardSegments": [{ "id": "keyboard", "start": base, "end": base + 4., "displayText": "A", "keys": [] }]
+ }
+ }))
+ .unwrap();
+ let initial = serde_json::to_value(&project).unwrap();
+ let mut history = ProjectHistory::new(project.clone());
+ let commit = |project: &mut cap_project::ProjectConfiguration,
+ text: &str| {
+ let value = text.parse().unwrap();
+ let timeline = project.timeline.as_mut().unwrap();
+ if caption {
+ commit_segment_timing(
+ &mut timeline.caption_segments[0],
+ start,
+ text,
+ value,
+ )
+ } else {
+ commit_segment_timing(
+ &mut timeline.keyboard_segments[0],
+ start,
+ text,
+ value,
+ )
+ }
+ };
+ let before = if start { base } else { base + 4. };
+ assert!(commit(&mut project, &format!("{:.2}", before + 1.)));
+ history.record(&project);
+ let edited = serde_json::to_value(&project).unwrap();
+ project = history.undo().unwrap().clone();
+ assert_eq!(serde_json::to_value(&project).unwrap(), initial);
+
+ if paused {
+ history.pause();
+ }
+ if commit(&mut project, &format!("{before:.2}")) {
+ history.record(&project);
+ }
+ if paused {
+ history.resume(&project);
+ }
+ let context = format!(
+ "base={base}, caption={caption}, start={start}, paused={paused}"
+ );
+ assert_eq!(
+ serde_json::to_value(&project).unwrap(),
+ initial,
+ "{context}"
+ );
+ assert!(history.can_redo(), "{context}");
+ assert!(!history.can_undo(), "{context}");
+ assert_eq!(
+ serde_json::to_value(history.redo().unwrap()).unwrap(),
+ edited
+ );
+ assert_eq!(
+ serde_json::to_value(history.undo().unwrap()).unwrap(),
+ initial
+ );
+ }
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn pasting_a_text_style_keeps_content_timing_position_and_layout() {
+ let mut source = crate::editor_edits::default_text_segment(0., 2., 0);
+ source.content = "Hello".into();
+ source.font_family = "Georgia".into();
+ source.font_size = 96.;
+ source.font_weight = 400.;
+ source.italic = true;
+ source.uppercase = true;
+ source.letter_spacing = 3.;
+ source.color = "#ff0000".into();
+ source.background_color = Some("#00000080".into());
+ source.gradient_color = Some("#00ff00".into());
+ source.animation_in = TextAnimation::Pop;
+ source.animation_out = TextAnimation::Wipe;
+ source.animation_in_duration = 0.4;
+ source.animation_out_duration = 0.6;
+ source.center = XY::new(0.2, 0.2);
+ source.layout = TextLayout::Fullscreen;
+
+ let mut target = crate::editor_edits::default_text_segment(5., 8., 1);
+ target.content = "World".into();
+ target.center = XY::new(0.7, 0.8);
+ target.size = XY::new(0.2, 0.1);
+
+ assert!(TextStyle::of(&source).apply_to(&mut target));
+
+ assert_eq!(target.content, "World");
+ assert_eq!(target.start, 5.);
+ assert_eq!(target.end, 8.);
+ assert_eq!(target.track, 1);
+ assert_eq!(target.layout, TextLayout::Overlay);
+ assert_eq!(target.center.x, 0.7);
+ assert_eq!(target.font_family, "Georgia");
+ assert_eq!(target.font_size, 96.);
+ assert!(target.italic && target.uppercase);
+ assert_eq!(target.letter_spacing, 3.);
+ assert_eq!(target.color, "#ff0000");
+ assert_eq!(target.background_color.as_deref(), Some("#00000080"));
+ assert_eq!(target.gradient_color.as_deref(), Some("#00ff00"));
+ assert_eq!(target.animation_in, TextAnimation::Pop);
+ assert_eq!(target.animation_out, TextAnimation::Wipe);
+ assert_eq!(target.animation_in_duration, 0.4);
+ assert_eq!(target.animation_out_duration, 0.6);
+ assert_eq!(TextStyle::of(&target), TextStyle::of(&source));
+ }
+
+ #[test]
+ fn pasting_a_text_style_scales_the_box_from_its_top_edge() {
+ let mut source = crate::editor_edits::default_text_segment(0., 2., 0);
+ source.font_size = 96.;
+ let mut target = crate::editor_edits::default_text_segment(0., 2., 0);
+ target.font_size = 48.;
+ target.center = XY::new(0.5, 0.5);
+ target.size = XY::new(0.2, 0.1);
+
+ assert!(TextStyle::of(&source).apply_to(&mut target));
+
+ assert!((target.size.x - 0.4).abs() < 1e-9);
+ assert!((target.size.y - 0.2).abs() < 1e-9);
+ assert!((target.center.y - 0.55).abs() < 1e-9);
+ assert_eq!(target.center.x, 0.5);
+ }
+
+ #[test]
+ fn pasting_a_text_style_onto_its_own_source_changes_nothing() {
+ for (center, size) in [
+ (XY::new(0.3, 0.6), XY::new(0.25, 0.08)),
+ (XY::new(0.5, 0.9), XY::new(0.3, 0.4)),
+ ] {
+ let mut segment = crate::editor_edits::default_text_segment(0., 2., 0);
+ segment.color = "#ff0000".into();
+ segment.center = center;
+ segment.size = size;
+ let style = TextStyle::of(&segment);
+ let before = segment.clone();
+
+ assert!(!style.apply_to(&mut segment));
+
+ assert_eq!(TextStyle::of(&segment), style);
+ assert_eq!(segment.center, before.center);
+ assert_eq!(segment.size, before.size);
+ }
+ }
+
fn camera3d(start: f64, end: f64) -> Camera3DSegment {
Camera3DSegment {
start,
@@ -8613,6 +8910,22 @@ mod tests {
);
}
+ #[test]
+ fn a_paused_scrub_sees_the_poses_the_playhead_lands_on() {
+ // A paused scrub redraws the sidebar only when this list changes, and
+ // the pose cards mark exactly these poses.
+ let fps = 60;
+ let shots = [camera3d(1., 2.5), camera3d(4.02, 6.)];
+ assert_eq!(camera3d_poses_at(&shots, 1., fps), [(0, false)]);
+ assert_eq!(camera3d_poses_at(&shots, 149. / 60., fps), [(0, true)]);
+ assert_eq!(camera3d_poses_at(&shots, 242. / 60., fps), [(1, false)]);
+ assert_eq!(camera3d_poses_at(&shots, 359. / 60., fps), [(1, true)]);
+ for playhead in [0., 1.75, 3., 5., 7.] {
+ assert!(camera3d_poses_at(&shots, playhead, fps).is_empty());
+ }
+ assert!(camera3d_poses_at(&[], 1., fps).is_empty());
+ }
+
#[test]
fn a_sequence_stays_inside_the_shot_it_replaces() {
// The panel replaces one shot with the whole chain, so the chain has
diff --git a/apps/desktop-gpui/src/editor_preparing/audio.rs b/apps/desktop-gpui/src/editor_preparing/audio.rs
index bb5206e20..1b58380b9 100644
--- a/apps/desktop-gpui/src/editor_preparing/audio.rs
+++ b/apps/desktop-gpui/src/editor_preparing/audio.rs
@@ -338,6 +338,7 @@ mod tests {
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
};
for extension in ["m4a", "ogg"] {
let relative = PathBuf::from(format!("segment-0/audio-input.{extension}"));
diff --git a/apps/desktop-gpui/src/editor_sidebar.rs b/apps/desktop-gpui/src/editor_sidebar.rs
index 73afc538e..dcc1c0f2c 100644
--- a/apps/desktop-gpui/src/editor_sidebar.rs
+++ b/apps/desktop-gpui/src/editor_sidebar.rs
@@ -196,6 +196,7 @@ pub const BACKGROUND_IMAGE_EXTENSIONS: [&str; 6] = ["jpg", "jpeg", "png", "gif",
pub const CURRENT_DESKTOP_BACKGROUND_BASENAME: &str = "current-desktop-background";
/// `recording.rs:92-93`.
const DESKTOP_BACKGROUND_MAX_DIMENSION: u32 = 2560;
+#[cfg(target_os = "macos")]
const DESKTOP_BACKGROUND_JPEG_QUALITY: u8 = 82;
/// The sidebar's fallback border/shadow objects. The UI's numbers, **not**
@@ -596,6 +597,36 @@ fn noise_texture(width: u32, height: u32, base_frequency: f32) -> Arc<RenderImag
)]))
}
+#[derive(Default)]
+struct NoiseCache {
+ current: Option<(i32, Arc<RenderImage>)>,
+ retired: Vec<Arc<RenderImage>>,
+}
+
+impl NoiseCache {
+ fn image(&mut self, scale: f32) -> Arc<RenderImage> {
+ let key = scale.round() as i32;
+ if let Some((cached, image)) = &self.current
+ && *cached == key
+ {
+ return image.clone();
+ }
+ let image = noise_texture(
+ GRADIENT_PREVIEW_WIDTH as u32,
+ GRADIENT_PREVIEW_HEIGHT as u32,
+ noise_base_frequency(scale),
+ );
+ if let Some((_, previous)) = self.current.replace((key, image.clone())) {
+ self.retired.push(previous);
+ }
+ image
+ }
+
+ fn take_retired(&mut self) -> Vec<Arc<RenderImage>> {
+ std::mem::take(&mut self.retired)
+ }
+}
+
// ---------------------------------------------------------------------------
// State
// ---------------------------------------------------------------------------
@@ -787,7 +818,7 @@ pub struct SidebarState {
/// The gradient preview's grain, cached by the grain-scale step it was
/// generated for -- regenerating 42k pixels of noise on every frame of a
/// slider drag would be the one expensive thing in this panel.
- noise: std::cell::RefCell<Option<(i32, Arc<RenderImage>)>>,
+ noise: std::cell::RefCell<NoiseCache>,
/// The open `KSelect`'s menu, if any -- one slot for every select in the
/// sidebar, the settings window's `Menu.popup()` stand-in.
@@ -876,7 +907,7 @@ impl SidebarState {
cursor_previews: std::cell::RefCell::new(BTreeMap::new()),
cursor_scale: 2.,
panel_sections: std::cell::RefCell::new(HashMap::new()),
- noise: std::cell::RefCell::new(None),
+ noise: std::cell::RefCell::default(),
menu: None,
caption_model: "best",
caption_language: "auto",
@@ -905,19 +936,16 @@ impl SidebarState {
/// The grain texture for a grain-scale value, generated once per step.
fn noise_image(&self, scale: f32) -> Arc<RenderImage> {
- let key = scale.round() as i32;
- if let Some((cached, image)) = self.noise.borrow().as_ref()
- && *cached == key
- {
- return image.clone();
+ self.noise.borrow_mut().image(scale)
+ }
+
+ /// Evicts the grain textures earlier steps of a slider drag left in the
+ /// window's atlas. The panel is built without a `&mut Window`, so they
+ /// are queued there and released from `render` on the next frame.
+ pub(crate) fn release_retired_images(&self, window: &mut Window) {
+ for image in self.noise.borrow_mut().take_retired() {
+ let _ = window.drop_image(image);
}
- let image = noise_texture(
- GRADIENT_PREVIEW_WIDTH as u32,
- GRADIENT_PREVIEW_HEIGHT as u32,
- noise_base_frequency(scale),
- );
- *self.noise.borrow_mut() = Some((key, image.clone()));
- image
}
/// The stored track rect, without creating one -- the pointer maths reads
@@ -1895,22 +1923,7 @@ fn decode_scaled_rgba(path: &Path, max: u32) -> Option<image::RgbaImage> {
}
fn decode_jpeg_thumbnail(bytes: &[u8], max: u32) -> Option<(image::DynamicImage, u32, u32)> {
- let mut decoder = jpeg_decoder::Decoder::new(bytes);
- decoder.read_info().ok()?;
- let info = decoder.info()?;
- let requested = max.clamp(1, u16::MAX as u32) as u16;
- let (width, height) = decoder.scale(requested, requested).ok()?;
- let pixels = decoder.decode().ok()?;
- let decoded = match info.pixel_format {
- jpeg_decoder::PixelFormat::RGB24 => image::DynamicImage::ImageRgb8(
- image::RgbImage::from_raw(width as u32, height as u32, pixels)?,
- ),
- jpeg_decoder::PixelFormat::L8 => image::DynamicImage::ImageLuma8(
- image::GrayImage::from_raw(width as u32, height as u32, pixels)?,
- ),
- _ => return None,
- };
- Some((decoded, info.width as u32, info.height as u32))
+ crate::jpeg_thumbnail::decode_jpeg_thumbnail(bytes, |_, _| (max, max))
}
/// [`decode_scaled_rgba`] in gpui's BGRA order.
@@ -1953,30 +1966,33 @@ pub(crate) fn decode_wallpaper_thumbnail(id: &str) -> Option<Arc<RenderImage>> {
/// re-encoding to JPEG at quality 82. The `sips` path matters -- macOS ships
/// HEIC wallpapers the `image` crate cannot decode.
fn write_desktop_background_snapshot(source: &Path, output: &Path) -> Result<(), String> {
- let needs_downscale = image::image_dimensions(source).is_ok_and(|(width, height)| {
- width > DESKTOP_BACKGROUND_MAX_DIMENSION || height > DESKTOP_BACKGROUND_MAX_DIMENSION
- });
-
- let mut command = std::process::Command::new("sips");
- command
- .arg("-s")
- .arg("format")
- .arg("jpeg")
- .arg("-s")
- .arg("formatOptions")
- .arg(DESKTOP_BACKGROUND_JPEG_QUALITY.to_string());
- if needs_downscale {
- command
- .arg("-Z")
- .arg(DESKTOP_BACKGROUND_MAX_DIMENSION.to_string());
- }
- if let Ok(output_status) = command.arg(source).arg("--out").arg(output).output()
- && output_status.status.success()
+ #[cfg(target_os = "macos")]
{
- return Ok(());
+ let needs_downscale = image::image_dimensions(source).is_ok_and(|(width, height)| {
+ width > DESKTOP_BACKGROUND_MAX_DIMENSION || height > DESKTOP_BACKGROUND_MAX_DIMENSION
+ });
+
+ let mut command = std::process::Command::new("sips");
+ command
+ .arg("-s")
+ .arg("format")
+ .arg("jpeg")
+ .arg("-s")
+ .arg("formatOptions")
+ .arg(DESKTOP_BACKGROUND_JPEG_QUALITY.to_string());
+ if needs_downscale {
+ command
+ .arg("-Z")
+ .arg(DESKTOP_BACKGROUND_MAX_DIMENSION.to_string());
+ }
+ if let Ok(output_status) = command.arg(source).arg("--out").arg(output).output()
+ && output_status.status.success()
+ {
+ return Ok(());
+ }
}
- let decoded = cap_gpui_kernels::codec::open_image(source)
+ let decoded = cap_gpui_kernels::codec::decode_image_file(source)
.map_err(|err| format!("failed to decode desktop background: {err}"))?;
let decoded = if decoded.width() > DESKTOP_BACKGROUND_MAX_DIMENSION
|| decoded.height() > DESKTOP_BACKGROUND_MAX_DIMENSION
@@ -2137,7 +2153,9 @@ impl EditorWindow {
/// selected path.
fn ensure_preview(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let Some(path) = self.preview_path() else {
- self.sidebar.preview = None;
+ if let Some((_, previous)) = self.sidebar.preview.take() {
+ let _ = window.drop_image(previous);
+ }
return;
};
if self
@@ -2157,8 +2175,10 @@ impl EditorWindow {
.await
};
let Some(image) = decoded else { return };
- this.update(cx, |this, cx| {
- this.sidebar.preview = Some((target, image));
+ this.update_in(cx, |this, window, cx| {
+ if let Some((_, previous)) = this.sidebar.preview.replace((target, image)) {
+ let _ = window.drop_image(previous);
+ }
cx.notify();
})
.ok();
@@ -2426,6 +2446,13 @@ impl EditorWindow {
}
Err(error) => {
tracing::error!("couldn't import your desktop wallpaper: {error}");
+ cx.spawn(async move |_, _| {
+ crate::platform::alert_dialog(
+ "Couldn't import your desktop wallpaper",
+ &error,
+ );
+ })
+ .detach();
}
}
cx.notify();
@@ -4070,6 +4097,24 @@ impl EditorWindow {
mod tests {
use super::*;
+ #[test]
+ fn a_new_grain_step_retires_the_old_texture_for_atlas_eviction() {
+ let mut noise = NoiseCache::default();
+ let first = noise.image(20.);
+ assert!(Arc::ptr_eq(&noise.image(20.2), &first));
+ assert!(noise.take_retired().is_empty());
+
+ let second = noise.image(35.);
+ let third = noise.image(50.);
+ assert!(!Arc::ptr_eq(&second, &first));
+ let retired = noise.take_retired();
+ assert_eq!(retired.len(), 2);
+ assert!(Arc::ptr_eq(&retired[0], &first));
+ assert!(Arc::ptr_eq(&retired[1], &second));
+ assert!(noise.take_retired().is_empty());
+ assert!(Arc::ptr_eq(&noise.image(50.), &third));
+ }
+
#[test]
fn the_catalogue_matches_the_source() {
assert_eq!(WALLPAPER_NAMES.len(), 53);
diff --git a/apps/desktop-gpui/src/editor_tabs.rs b/apps/desktop-gpui/src/editor_tabs.rs
index a79364748..d59830d16 100644
--- a/apps/desktop-gpui/src/editor_tabs.rs
+++ b/apps/desktop-gpui/src/editor_tabs.rs
@@ -358,8 +358,7 @@ impl CameraSlider {
match self {
// `minValue={20} maxValue={80} step={0.1}` (`:3204-3206`).
Self::Size => (20., 80., 0.1),
- // `minValue={10} maxValue={60}` (`:3213-3215`).
- Self::ZoomSize => (10., 60., 0.1),
+ Self::ZoomSize => (10., 100., 0.1),
_ => (0., 100., 0.1),
}
}
@@ -369,8 +368,7 @@ impl CameraSlider {
let shadow = camera.advanced_shadow.as_ref();
match self {
Self::Size => camera.size,
- // `project.camera.zoomSize ?? 60`
- Self::ZoomSize => camera.zoom_size.unwrap_or(60.),
+ Self::ZoomSize => camera.scale_during_zoom * 100.,
Self::Rounding => camera.rounding,
Self::Shadow => camera.shadow,
Self::ShadowSize => shadow.map_or(UI_SHADOW_FALLBACK.size, |s| s.size),
@@ -1004,7 +1002,7 @@ impl EditorWindow {
let camera = &mut project.camera;
match slider {
CameraSlider::Size => camera.size = value,
- CameraSlider::ZoomSize => camera.zoom_size = Some(value),
+ CameraSlider::ZoomSize => camera.scale_during_zoom = value / 100.,
CameraSlider::Rounding => camera.rounding = value,
CameraSlider::Shadow => camera.shadow = value,
// `...(project.camera.advancedShadow ?? {50, 18, 50})`
@@ -3473,7 +3471,7 @@ mod tests {
#[test]
fn every_tab_slider_has_the_range_its_call_site_declares() {
assert_eq!(CameraSlider::Size.limits(), (20., 80., 0.1));
- assert_eq!(CameraSlider::ZoomSize.limits(), (10., 60., 0.1));
+ assert_eq!(CameraSlider::ZoomSize.limits(), (10., 100., 0.1));
assert_eq!(CameraSlider::ShadowBlur.limits(), (0., 100., 0.1));
assert_eq!(AudioSlider::MicVolume.limits(), (-30., 10., 0.1));
assert_eq!(CursorSlider::Size.limits(), (20., 300., 1.));
@@ -3487,6 +3485,16 @@ mod tests {
assert_eq!(CaptionSlider::FadeDuration.limits(), (0., 50., 1.));
}
+ #[test]
+ fn camera_zoom_size_slider_shows_the_rendered_scale() {
+ let mut project = ProjectConfiguration::default();
+ assert_eq!(CameraSlider::ZoomSize.read(&project), 70.);
+ project.camera.scale_during_zoom = 0.4;
+ assert_eq!(CameraSlider::ZoomSize.read(&project), 40.);
+ project.camera.scale_during_zoom = 1.;
+ assert_eq!(CameraSlider::ZoomSize.read(&project), 100.);
+ }
+
#[test]
fn the_percent_sliders_read_and_write_the_same_number() {
// `value={[getSetting("fadeDuration") * 100]}` / `v[0] / 100`.
diff --git a/apps/desktop-gpui/src/editor_timeline.rs b/apps/desktop-gpui/src/editor_timeline.rs
index 43702143c..16773fb1a 100644
--- a/apps/desktop-gpui/src/editor_timeline.rs
+++ b/apps/desktop-gpui/src/editor_timeline.rs
@@ -316,7 +316,8 @@ impl Transform {
/// `updateZoom(newZoom, origin)` (`ED/context.ts:1389-1403, 1456-1472`):
/// clamp the zoom into `[MAX_ZOOM_IN, zoomOutLimit]`, then move the
- /// position so `origin` stays at the same fraction across the viewport.
+ /// position so `origin` stays at the same fraction across the viewport,
+ /// or at its nearest edge when it is outside the viewport.
///
/// The clamp is `Math.max(Math.min(newZoom, zoomOutLimit()), MAX_ZOOM_IN)`
/// in that order, so on a project shorter than 3 s the *floor* wins and the
@@ -327,7 +328,7 @@ impl Transform {
.max(MAX_ZOOM_IN);
let visible_origin = origin - self.position;
- let origin_percentage = (visible_origin / self.zoom).min(1.);
+ let origin_percentage = (visible_origin / self.zoom).clamp(0., 1.);
let new_visible_origin = zoom * origin_percentage;
let new_position = origin - new_visible_origin;
@@ -583,6 +584,7 @@ pub fn waveform_path(geometry: &WaveformGeometry) -> Option<gpui::Path<Pixels>>
target_samples,
ref holds,
source_start: segment_start,
+ timescale,
origin,
size,
scale,
@@ -591,12 +593,12 @@ pub fn waveform_path(geometry: &WaveformGeometry) -> Option<gpui::Path<Pixels>>
if peaks.is_empty() || scale <= 0. {
return None;
}
- let duration = (range.1 - range.0).max(WAVEFORM_SAMPLE_STEP);
+ let duration = range.1 - range.0;
if !duration.is_finite() || duration <= 0. {
return None;
}
- let native_samples = (duration / WAVEFORM_SAMPLE_STEP).ceil() as usize + 1;
+ let native_samples = (duration * timescale / WAVEFORM_SAMPLE_STEP).ceil() as usize + 1;
let num_samples = target_samples
.clamp(50, MAX_WAVEFORM_SAMPLES)
.min(native_samples);
@@ -619,7 +621,7 @@ pub fn waveform_path(geometry: &WaveformGeometry) -> Option<gpui::Path<Pixels>>
break;
}
}
- Some(segment_start + output_time - held)
+ Some(segment_start + (output_time - held) * timescale)
};
let width = f32::from(size.width) as f64;
@@ -646,7 +648,7 @@ pub fn waveform_path(geometry: &WaveformGeometry) -> Option<gpui::Path<Pixels>>
let mut builder = gpui::PathBuilder::fill();
builder.move_to(map(0., 1.));
- let control_step = (WAVEFORM_CONTROL_STEP / duration).min(0.25);
+ let control_step = (WAVEFORM_CONTROL_STEP / timescale / duration).min(0.25);
for i in 0..=num_samples {
let time = range.0 + i as f64 * time_step;
@@ -692,6 +694,7 @@ pub struct WaveformGeometry {
pub target_samples: usize,
pub holds: Vec<(f64, f64)>,
pub source_start: f64,
+ pub timescale: f64,
pub origin: gpui::Point<Pixels>,
pub size: gpui::Size<Pixels>,
pub scale: f64,
@@ -705,6 +708,7 @@ impl PartialEq for WaveformGeometry {
&& self.target_samples == other.target_samples
&& self.holds == other.holds
&& self.source_start == other.source_start
+ && self.timescale == other.timescale
&& self.origin == other.origin
&& self.size == other.size
&& self.scale == other.scale
@@ -712,6 +716,96 @@ impl PartialEq for WaveformGeometry {
}
}
+/// The span of a segment (segment-local output seconds) its cached waveform
+/// path covers. The visible slice is grown to a window anchored on a grid of
+/// a quarter of the viewport, so a timeline that scrolls -- playback following
+/// the playhead while zoomed in -- reuses one path for a quarter-viewport of
+/// travel instead of rebuilding it every frame. A segment that is entirely
+/// visible covers exactly that, as before.
+pub fn waveform_window(visible: (f64, f64), zoom: f64, duration: f64) -> (f64, f64) {
+ if !zoom.is_finite() || zoom <= 0. || !duration.is_finite() || duration <= 0. {
+ return visible;
+ }
+ let grid = zoom / 4.;
+ let step = (visible.0 / grid).floor() - 1.;
+ let start = (step * grid).max(0.).min(visible.0);
+ let end = (start + zoom * 1.5).max(visible.1).min(duration);
+ (start, end)
+}
+
+/// Where a segment's waveform path sits inside its canvas. Everything is
+/// canvas-local, so a timeline that only scrolls keeps hitting the cache; the
+/// path is moved into place when painted.
+#[derive(Debug, Clone, Copy, PartialEq)]
+struct WaveformLayout {
+ range: (f64, f64),
+ origin: gpui::Point<Pixels>,
+ size: gpui::Size<Pixels>,
+ samples: usize,
+ clip_bounds: gpui::Bounds<Pixels>,
+}
+
+/// `view` is the timeline's (position, zoom), `segment` its (start, output
+/// duration) and `canvas` the (canvas height, lane width, lane height, wave
+/// height). `None` when the segment is off screen.
+fn waveform_layout(
+ view: (f64, f64),
+ segment: (f64, f64),
+ full_width: f64,
+ canvas: (Pixels, Pixels, Pixels, Pixels),
+) -> Option<WaveformLayout> {
+ let (view_start, zoom) = view;
+ let (timeline_start, output_duration) = segment;
+ let (canvas_height, lane_width, lane_height, wave_height) = canvas;
+ // The visible slice, in segment-local output seconds
+ // (`TL/ClipTrack.tsx:202-245`).
+ let view_end = view_start + zoom;
+ let visible_start = view_start.max(timeline_start) - timeline_start;
+ let visible_end = view_end.min(timeline_start + output_duration) - timeline_start;
+ if visible_end <= visible_start {
+ return None;
+ }
+ let px_per_sec = full_width / output_duration;
+ let range = waveform_window((visible_start, visible_end), zoom, output_duration);
+ let window_width = (range.1 - range.0) * px_per_sec;
+ Some(WaveformLayout {
+ range,
+ origin: gpui::point(px((range.0 * px_per_sec) as f32), px(0.)),
+ size: gpui::size(px(window_width as f32), wave_height),
+ samples: waveform_window_sample_count(window_width, zoom * px_per_sec),
+ clip_bounds: gpui::Bounds {
+ origin: gpui::point(px(0.), canvas_height - lane_height),
+ size: gpui::size(lane_width, lane_height),
+ },
+ })
+}
+
+/// `waveform_sample_count` for a window wider than the viewport: the sample
+/// cap grows with the window, so the slice on screen keeps the density it had
+/// when only the visible slice was built.
+fn waveform_window_sample_count(window_width: f64, viewport_width: f64) -> usize {
+ let scale = if viewport_width.is_finite() && viewport_width > 0. {
+ (window_width / viewport_width).max(1.)
+ } else {
+ 1.
+ };
+ let cap = (MAX_WAVEFORM_SAMPLES as f64 * scale).ceil() as usize;
+ ((window_width * WAVEFORM_SAMPLES_PER_PIXEL).ceil() as usize).min(cap)
+}
+
+/// `path` moved by `offset`: cached waveform paths are built relative to their
+/// segment and placed where the segment is drawn this frame.
+fn translated_path(
+ mut path: gpui::Path<Pixels>,
+ offset: gpui::Point<Pixels>,
+) -> gpui::Path<Pixels> {
+ for vertex in &mut path.vertices {
+ vertex.xy_position = vertex.xy_position + offset;
+ }
+ path.bounds.origin = path.bounds.origin + offset;
+ path
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WaveformTrack {
Mic,
@@ -1140,14 +1234,7 @@ pub enum SegmentDetail {
impl SegmentDetail {
fn shows_waveform(&self) -> bool {
- matches!(
- self,
- Self::Clip {
- timescale,
- muted: false,
- ..
- } if *timescale == 1.0
- )
+ matches!(self, Self::Clip { muted: false, .. })
}
}
@@ -3191,6 +3278,7 @@ fn render_waveform(
let SegmentDetail::Clip {
source_start: segment_start,
+ timescale,
volume,
..
} = segment.detail
@@ -3214,27 +3302,13 @@ fn render_waveform(
gpui::canvas(
|bounds, _window, _cx| bounds,
move |_, bounds, window, _cx| {
- // The visible slice, in segment-local output seconds
- // (`TL/ClipTrack.tsx:202-245`). Off screen entirely: nothing.
- let view_start = transform.position;
- let view_end = view_start + transform.zoom;
- let visible_start = view_start.max(timeline_start) - timeline_start;
- let visible_end = view_end.min(timeline_start + output_duration) - timeline_start;
- if visible_end <= visible_start {
+ let Some(layout) = waveform_layout(
+ (transform.position, transform.zoom),
+ (timeline_start, output_duration),
+ full_width,
+ (bounds.size.height, px(width), px(height), px(wave_height)),
+ ) else {
return;
- }
- let px_per_sec = full_width / output_duration;
- let origin = gpui::point(
- bounds.origin.x + px((visible_start * px_per_sec) as f32),
- bounds.origin.y,
- );
- let slice_width = ((visible_end - visible_start) * px_per_sec) as f32;
- let size = gpui::size(px(slice_width), px(wave_height));
- let samples = waveform_sample_count(slice_width as f64);
-
- let clip_bounds = gpui::Bounds {
- origin: gpui::point(bounds.origin.x, bounds.bottom() - px(height)),
- size: gpui::size(px(width), px(height)),
};
for (track, peaks, scale) in [
@@ -3243,16 +3317,18 @@ fn render_waveform(
] {
let geometry = WaveformGeometry {
peaks: peaks.clone(),
- range: (visible_start, visible_end),
- target_samples: samples,
+ range: layout.range,
+ target_samples: layout.samples,
holds: holds.clone(),
source_start: segment_start,
- origin,
- size,
+ timescale,
+ origin: layout.origin,
+ size: layout.size,
scale,
- clip_bounds,
+ clip_bounds: layout.clip_bounds,
};
if let Some(path) = paths.get_or_build((index, track), geometry) {
+ let path = translated_path(path, bounds.origin);
window.with_content_mask(Some(gpui::ContentMask { bounds }), |window| {
window.paint_path(path, wave_color)
});
@@ -3848,6 +3924,19 @@ mod tests {
);
}
+ #[test]
+ fn an_origin_before_the_viewport_pins_to_its_left_edge() {
+ let mut transform = Transform {
+ zoom: 10.,
+ position: 20.,
+ };
+
+ transform.update_zoom(20., 5., 100.);
+
+ assert_eq!(transform.zoom, 20.);
+ assert_eq!(transform.position, 5.);
+ }
+
#[test]
fn position_clamps_to_zero_and_to_the_content_end_plus_four() {
let total = 60.0;
@@ -4364,7 +4453,7 @@ mod tests {
}
#[test]
- fn clip_mute_indicators_follow_speed_audio_modes() {
+ fn clip_waveforms_and_mute_indicators_follow_speed_audio_modes() {
let config: ProjectConfiguration = serde_json::from_value(serde_json::json!({
"timeline": {
"segments": [
@@ -4383,7 +4472,7 @@ mod tests {
clip.detail,
SegmentDetail::Clip { muted, .. } if muted == expected_muted
));
- assert!(!clip.detail.shows_waveform());
+ assert_eq!(clip.detail.shows_waveform(), !expected_muted);
}
}
@@ -4598,6 +4687,7 @@ mod tests {
target_samples: 400,
holds: Vec::new(),
source_start: 20.,
+ timescale: 1.,
origin: gpui::point(px(0.), px(26.)),
size: gpui::size(px(200.), px(18.)),
scale,
@@ -4695,6 +4785,7 @@ mod tests {
target_samples: 400,
holds: Vec::new(),
source_start: 5.,
+ timescale: 1.,
origin: gpui::point(px(10.), px(26.)),
size: gpui::size(px(300.), px(18.)),
scale: 0.8,
@@ -4705,6 +4796,251 @@ mod tests {
}
}
+ #[test]
+ fn waveform_path_retimes_trimmed_source_around_holds() {
+ let mut peaks = vec![-60.; 200];
+ peaks[140..160].fill(-6.);
+ let peaks = Arc::new(peaks);
+ for timescale in [0.25, 0.5, 2.0, 8.0] {
+ for holds in [vec![], vec![(1. / timescale, 1. / timescale + 1.)]] {
+ let held = if holds.is_empty() { 0. } else { 1. };
+ let duration = 8. / timescale + held;
+ for range in [(0., duration), (duration / 4., duration * 0.9)] {
+ let path = waveform_path(&WaveformGeometry {
+ peaks: peaks.clone(),
+ range,
+ target_samples: 1200,
+ holds: holds.clone(),
+ source_start: 10.,
+ timescale,
+ origin: gpui::point(px(0.), px(26.)),
+ size: gpui::size(px(600.), px(18.)),
+ scale: 1.,
+ clip_bounds: gpui::Bounds {
+ origin: gpui::point(px(0.), px(0.)),
+ size: gpui::size(px(600.), px(44.)),
+ },
+ })
+ .unwrap();
+ let (first, last) = path
+ .vertices
+ .iter()
+ .filter(|vertex| f32::from(vertex.xy_position.y) < 35.)
+ .map(|vertex| {
+ range.0
+ + f64::from(f32::from(vertex.xy_position.x)) / 600.
+ * (range.1 - range.0)
+ })
+ .fold((f64::INFINITY, f64::NEG_INFINITY), |(first, last), time| {
+ (first.min(time), last.max(time))
+ });
+ let expected_start = 4. / timescale + held;
+ let expected_end = 6. / timescale + held;
+ assert!(
+ (first - expected_start).abs() < 0.2 / timescale,
+ "{timescale}x, {holds:?}, {range:?}: first {first}, expected {expected_start}"
+ );
+ assert!(
+ (last - expected_end).abs() < 0.2 / timescale,
+ "{timescale}x, {holds:?}, {range:?}: last {last}, expected {expected_end}"
+ );
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn short_retimed_waveform_does_not_read_past_the_trim_end() {
+ let mut peaks = vec![-60.; 200];
+ peaks[103..105].fill(0.);
+ let geometry = WaveformGeometry {
+ range: (0., 0.025),
+ source_start: 10.,
+ timescale: 8.,
+ ..waveform_geometry(Arc::new(peaks))
+ };
+ let path = waveform_path(&geometry).unwrap();
+ let silent = waveform_path(&WaveformGeometry {
+ peaks: Arc::new(vec![-60.; 200]),
+ ..geometry
+ })
+ .unwrap();
+ assert_eq!(waveform_vertices(&path), waveform_vertices(&silent));
+ }
+
+ #[test]
+ fn the_waveform_window_always_covers_the_visible_slice() {
+ let duration = 120.;
+ let zoom = 20.;
+ let mut start = 0.;
+ while start + zoom <= duration {
+ let visible = (start, start + zoom);
+ let window = waveform_window(visible, zoom, duration);
+ assert!(
+ window.0 <= visible.0 && window.1 >= visible.1,
+ "{window:?} {visible:?}"
+ );
+ assert!(window.1 - window.0 <= zoom * 1.5 + 1e-9);
+ start += 0.37;
+ }
+ }
+
+ #[test]
+ fn a_fully_visible_segment_covers_exactly_its_visible_slice() {
+ assert_eq!(waveform_window((0., 8.), 30., 8.), (0., 8.));
+ assert_eq!(waveform_window((0., 30.), 30., 30.), (0., 30.));
+ }
+
+ #[test]
+ fn scrolling_reuses_the_window_for_a_quarter_viewport() {
+ let zoom = 20.;
+ let duration = 600.;
+ let mut windows = std::collections::HashSet::new();
+ // 60 s of follow-mode playback at 60 fps.
+ for frame in 0..3600 {
+ let start = 100. + frame as f64 / 60.;
+ let window = waveform_window((start, start + zoom), zoom, duration);
+ windows.insert((window.0.to_bits(), window.1.to_bits()));
+ }
+ assert!(windows.len() <= 13, "{} distinct windows", windows.len());
+ }
+
+ #[test]
+ fn a_translated_relative_path_matches_one_built_in_place() {
+ let peaks = speech_like_peaks(400, 7.);
+ let offset = gpui::point(px(250.5), px(312.25));
+ let relative = waveform_geometry(peaks.clone());
+ let path = waveform_path(&relative).unwrap();
+ let moved = translated_path(path.clone(), offset);
+ for (before, after) in path.vertices.iter().zip(&moved.vertices) {
+ assert_eq!(after.xy_position, before.xy_position + offset);
+ }
+ assert_eq!(moved.bounds.origin, path.bounds.origin + offset);
+ assert_eq!(moved.bounds.size, path.bounds.size);
+
+ // Building at the absolute position gives the same shape; the
+ // tessellator's triangle order can differ with float rounding, so the
+ // comparison is on the vertex count and the bounds.
+ let absolute = WaveformGeometry {
+ origin: relative.origin + offset,
+ clip_bounds: gpui::Bounds {
+ origin: relative.clip_bounds.origin + offset,
+ size: relative.clip_bounds.size,
+ },
+ ..relative.clone()
+ };
+ let built = waveform_path(&absolute).unwrap();
+ assert_eq!(built.vertices.len(), moved.vertices.len());
+ let close = |a: Pixels, b: Pixels| (f32::from(a) - f32::from(b)).abs() < 0.01;
+ assert!(close(built.bounds.origin.x, moved.bounds.origin.x));
+ assert!(close(built.bounds.origin.y, moved.bounds.origin.y));
+ assert!(close(built.bounds.size.width, moved.bounds.size.width));
+ assert!(close(built.bounds.size.height, moved.bounds.size.height));
+ }
+
+ #[test]
+ fn follow_mode_paths_are_built_once_per_window() {
+ let paths = WaveformPaths::default();
+ let slot = (0, WaveformTrack::Mic);
+ let peaks = speech_like_peaks(2000, 9.);
+ let zoom = 20.;
+ let segment = (12., 190.);
+ let full_width = 190. * 60.;
+ let canvas = (px(44.), px(full_width as f32), px(44.), px(18.));
+ for frame in 0..600 {
+ let position = 40. + frame as f64 / 60.;
+ let layout = waveform_layout((position, zoom), segment, full_width, canvas).unwrap();
+ let geometry = WaveformGeometry {
+ range: layout.range,
+ target_samples: layout.samples,
+ origin: layout.origin,
+ size: layout.size,
+ clip_bounds: layout.clip_bounds,
+ ..waveform_geometry(peaks.clone())
+ };
+ paths.get_or_build(slot, geometry).unwrap();
+ }
+ let stats = paths.stats();
+ assert!(stats.builds <= 3, "{stats:?}");
+ assert_eq!(stats.builds + stats.hits, 600);
+ }
+
+ #[test]
+ fn retimed_waveforms_stay_in_place_across_scrolled_windows() {
+ // A 2x clip from source 10 s with a 1 s hold at output 2..3 s: the
+ // loud source 30..32 s lands at segment-local output 11..12 s.
+ let mut peaks = vec![-60.; 600];
+ peaks[300..320].fill(-6.);
+ let peaks = Arc::new(peaks);
+ let zoom = 6.;
+ let segment = (12., 21.);
+ let full_width = 21. * 60.;
+ let canvas = (px(44.), px(full_width as f32), px(44.), px(18.));
+ let offset = gpui::point(px(100.), px(50.));
+ for visible_start in [6.5, 8., 9.5] {
+ let layout = waveform_layout(
+ (segment.0 + visible_start, zoom),
+ segment,
+ full_width,
+ canvas,
+ )
+ .unwrap();
+ assert!(layout.range.0 > 0. && layout.range.0 < visible_start);
+ let path = waveform_path(&WaveformGeometry {
+ peaks: peaks.clone(),
+ range: layout.range,
+ target_samples: layout.samples,
+ holds: vec![(2., 3.)],
+ source_start: 10.,
+ timescale: 2.,
+ origin: layout.origin,
+ size: layout.size,
+ scale: 1.,
+ clip_bounds: layout.clip_bounds,
+ })
+ .unwrap();
+ let path = translated_path(path, offset);
+ let (first, last) = path
+ .vertices
+ .iter()
+ .filter(|vertex| f32::from(vertex.xy_position.y) < 59.)
+ .map(|vertex| f64::from(f32::from(vertex.xy_position.x) - 100.) / 60.)
+ .fold((f64::INFINITY, f64::NEG_INFINITY), |(first, last), time| {
+ (first.min(time), last.max(time))
+ });
+ assert!(
+ (first - 11.).abs() < 0.1,
+ "{visible_start}: {layout:?} first {first}"
+ );
+ assert!(
+ (last - 12.).abs() < 0.1,
+ "{visible_start}: {layout:?} last {last}"
+ );
+ }
+ }
+
+ #[test]
+ fn an_off_screen_segment_has_no_waveform_layout() {
+ let canvas = (px(44.), px(600.), px(44.), px(18.));
+ assert!(waveform_layout((0., 20.), (30., 10.), 600., canvas).is_none());
+ assert!(waveform_layout((50., 20.), (30., 10.), 600., canvas).is_none());
+ }
+
+ #[test]
+ fn the_wider_window_keeps_the_on_screen_sample_density_at_the_cap() {
+ for (window, viewport) in [(15_000., 10_000.), (4_500., 3_000.), (900., 600.)] {
+ let samples = waveform_window_sample_count(window, viewport);
+ let window_density = samples as f64 / window;
+ let visible_density = waveform_sample_count(viewport) as f64 / viewport;
+ assert!(
+ (window_density - visible_density).abs() < 1e-3,
+ "{window} {viewport}: {window_density} vs {visible_density}"
+ );
+ }
+ assert_eq!(waveform_window_sample_count(300., 600.), 600);
+ assert_eq!(waveform_window_sample_count(300., 0.), 600);
+ }
+
#[test]
fn cached_waveform_matches_an_uncached_build_after_every_input_change() {
let paths = WaveformPaths::default();
@@ -4739,6 +5075,13 @@ mod tests {
..base.clone()
},
),
+ (
+ "speed",
+ WaveformGeometry {
+ timescale: 2.,
+ ..base.clone()
+ },
+ ),
(
"source start",
WaveformGeometry {
@@ -4898,6 +5241,7 @@ mod tests {
target_samples: 200,
holds: Vec::new(),
source_start: 0.,
+ timescale: 1.,
origin: gpui::point(px(50. + x as f32), px(56.)),
size: gpui::size(px(span as f32), px(18.)),
scale: gain,
diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs
index 5acbe7e66..202d743c0 100644
--- a/apps/desktop-gpui/src/editor_window.rs
+++ b/apps/desktop-gpui/src/editor_window.rs
@@ -283,6 +283,17 @@ pub fn letterbox(container: (f32, f32), frame: (f32, f32)) -> (f32, f32) {
}
}
+fn frame_seconds(frame: u32) -> f64 {
+ frame as f64 / EDITOR_PREVIEW_FPS as f64
+}
+
+/// Where a seek leaves the playhead. Paused, the engine echoes the seek back
+/// as `playhead_changed(frame)`; storing the frame's own time makes that echo
+/// a no-op instead of a second whole-editor rebuild for every scrub step.
+fn seeked_playhead(time: f64, frame: u32, playing: bool) -> f64 {
+ if playing { time } else { frame_seconds(frame) }
+}
+
fn frame_layout_requires_editor_invalidation(
first_frame: bool,
playing: bool,
@@ -1545,6 +1556,8 @@ pub struct EditorWindow {
preview_quality: crate::store::EditorPreviewQuality,
pub(crate) tracks: TrackLanes,
toolbar_menu: Option<OpenToolbarMenu>,
+ text_menu: Option<OpenTextMenu>,
+ text_style_clipboard: Option<crate::editor_panels::TextStyle>,
frame_controls: frame::FrameControls,
add_track: Option<AddTrackMenu>,
add_track_opens: usize,
@@ -1652,6 +1665,7 @@ fn add_track_tile_progress(delta: f32, index: usize) -> f32 {
}
const CLIP_MENU_WIDTH: f32 = 300.;
+const TEXT_MENU_WIDTH: f32 = 250.;
/// Roughly the menu's laid-out height; only decides which side of its anchor
/// the card opens on.
const CLIP_MENU_HEIGHT_ESTIMATE: f32 = 440.;
@@ -1679,10 +1693,11 @@ fn clip_menu_placement(
index: usize,
origin: Point<Pixels>,
viewport: gpui::Size<Pixels>,
+ menu_width: f32,
) -> ClipSpeedMenu {
let (width, height) = (f32::from(viewport.width), f32::from(viewport.height));
let left = px((f32::from(origin.x) - 8.)
- .min(width - CLIP_MENU_WIDTH - 12.)
+ .min(width - menu_width - 12.)
.max(12.));
let y = f32::from(origin.y);
let room_above = y - 20.;
@@ -1866,6 +1881,12 @@ struct OpenToolbarMenu {
state: ui::MenuState,
}
+struct OpenTextMenu {
+ index: usize,
+ indices: Vec<usize>,
+ placement: ClipSpeedMenu,
+}
+
impl EditorWindow {
pub fn new(project_path: PathBuf, window: &mut Window, cx: &mut Context<Self>) -> Self {
crate::theme::bind_window(window, cx);
@@ -2113,6 +2134,8 @@ impl EditorWindow {
preview_quality: crate::store::GeneralSettings::load().editor_preview_quality,
tracks: TrackLanes::from_project(&ProjectConfiguration::default(), false),
toolbar_menu: None,
+ text_menu: None,
+ text_style_clipboard: None,
frame_controls: frame::FrameControls::default(),
add_track: None,
add_track_opens: 0,
@@ -2253,6 +2276,7 @@ impl EditorWindow {
/// carried across.
fn rebuild_timeline(&mut self) {
dismiss_indexed_sidebar_menu(&mut self.sidebar.menu);
+ self.text_menu = None;
let mic = std::mem::take(&mut self.timeline.mic_waveforms);
let system = std::mem::take(&mut self.timeline.system_waveforms);
let waveform_paths = self.timeline.waveform_paths.clone();
@@ -2542,6 +2566,7 @@ impl EditorWindow {
) {
self.sidebar.style_target = None;
self.sidebar.menu = None;
+ self.text_menu = None;
self.selection = None;
self.sidebar.image_asset_status = None;
self.canvas_selection = None;
@@ -2967,7 +2992,7 @@ impl EditorWindow {
/// is `setEditorState("playbackTime", payload.playhead_position / FPS)`
/// (`Editor.tsx:482-486`).
pub fn playhead_changed(&mut self, frame: u32, window: &mut Window, cx: &mut Context<Self>) {
- let next = frame as f64 / EDITOR_PREVIEW_FPS as f64;
+ let next = frame_seconds(frame);
if (next - self.playhead).abs() < f64::EPSILON {
return;
}
@@ -3188,11 +3213,53 @@ impl EditorWindow {
/// (`PlaybackHandle::seek`), so a scrub during playback holds the last
/// picture for one decode instead of paying a warmup per tick.
pub fn seek_to_time(&mut self, time: f64, cx: &mut Context<Self>) {
+ if self.apply_seek(time) {
+ cx.notify();
+ }
+ }
+
+ /// One pointer move of a ruler scrub. While paused, the playhead is read
+ /// by the timeline, the transport clock and what the window itself renders
+ /// (the canvas overlay, the clip popover), so this invalidates those the
+ /// way [`Self::invalidate_hover_chrome`] does instead of rebuilding the
+ /// header, toolbar and sidebar on every move. The one sidebar reader is
+ /// the 3D panel's "on the playhead" pose marker, which is only redrawn
+ /// when it changes. Playing and handoff seeks keep the full invalidation.
+ fn scrub_to_time(&mut self, time: f64, cx: &mut Context<Self>) {
+ let poses = self.camera3d_poses_at_playhead();
+ if !self.apply_seek(time) {
+ return;
+ }
+ if self.playing || self.ordinary_handoff_frame.is_some() {
+ cx.notify();
+ return;
+ }
+ self.invalidate_hover_chrome(cx);
+ if self.camera3d_poses_at_playhead() != poses {
+ self.sidebar_view.update(cx, |_, cx| cx.notify());
+ }
+ }
+
+ fn camera3d_poses_at_playhead(&self) -> Vec<(usize, bool)> {
+ crate::editor_panels::camera3d_poses_at(
+ self.project
+ .timeline
+ .as_ref()
+ .map(|timeline| timeline.camera3d_segments.as_slice())
+ .unwrap_or(&[]),
+ self.playhead_time(),
+ EDITOR_PREVIEW_FPS,
+ )
+ }
+
+ /// `seek_to_time` minus the invalidation. False when the seek went to the
+ /// preparing transport, which redraws through its own events.
+ fn apply_seek(&mut self, time: f64) -> bool {
// Moving the playhead by hand ends any shot audition.
self.play_until = None;
if self.preparing_transport_active() {
self.preparing_command(Some(time), None);
- return;
+ return false;
}
let Some(transport) = &self.transport else {
if let Some(target) = self
@@ -3202,7 +3269,7 @@ impl EditorWindow {
{
self.preparing_command(Some(target), None);
}
- return;
+ return false;
};
let time = time.clamp(0.0, self.total_duration());
// `Math.round(newTime * FPS)` -- "round to nearest frame to prevent
@@ -3230,10 +3297,11 @@ impl EditorWindow {
} else {
transport.seek(frame);
}
+ let time = seeked_playhead(time, frame, self.playing);
self.playhead = time;
self.view.playhead = time;
self.view.playing = self.playing;
- cx.notify();
+ true
}
/// The prev button (`Player.tsx:370-381`): stop, playhead to 0, and the
@@ -3448,6 +3516,12 @@ impl EditorWindow {
cx.stop_propagation();
return;
}
+ if self.text_menu.is_some() && event.keystroke.key.as_str() == "escape" {
+ self.text_menu = None;
+ cx.stop_propagation();
+ cx.notify();
+ return;
+ }
if self.add_track.is_some() && event.keystroke.key.as_str() == "escape" {
self.add_track = None;
cx.stop_propagation();
@@ -3860,7 +3934,7 @@ impl EditorWindow {
)
}
- fn timeline_mouse_down(
+ pub(crate) fn timeline_mouse_down(
&mut self,
event: &MouseDownEvent,
ruler: bool,
@@ -3883,7 +3957,7 @@ impl EditorWindow {
cx.notify();
}
- fn timeline_mouse_move(
+ pub(crate) fn timeline_mouse_move(
&mut self,
event: &MouseMoveEvent,
window: &mut Window,
@@ -3904,7 +3978,7 @@ impl EditorWindow {
// supersedes is simply overwritten, and the preview renderer cancels
// in-flight work when it sees a newer instruction
// (`editor_instance.rs:563-575`).
- self.seek_to_time(time, cx);
+ self.scrub_to_time(time, cx);
}
/// The **timeline's own** mouseup, the one the source registers on
@@ -3932,7 +4006,7 @@ impl EditorWindow {
/// dispose())`) and to end a ruler scrub. It runs after the timeline's own
/// handler -- gpui bubbles child to parent -- so a press released *outside*
/// the timeline is dropped rather than seeking.
- fn window_mouse_up(&mut self, cx: &mut Context<Self>) {
+ pub(crate) fn window_mouse_up(&mut self, cx: &mut Context<Self>) {
if self.scrub.take().is_some() {
cx.notify();
}
@@ -3983,6 +4057,7 @@ impl EditorWindow {
/// `setEditorState("timeline", "selection", ...)`.
pub(crate) fn set_selection(&mut self, selection: Option<Selection>, cx: &mut Context<Self>) {
if self.selection != selection {
+ self.text_menu = None;
self.dismiss_frame_controls(cx);
self.end_field_edit(cx);
self.close_color_picker(cx);
@@ -7807,7 +7882,8 @@ impl EditorWindow {
) {
let focus = self.focus_handle_for_menu();
window.focus(&focus, cx);
- let menu = clip_menu_placement(index, origin, window.viewport_size());
+ self.text_menu = None;
+ let menu = clip_menu_placement(index, origin, window.viewport_size(), CLIP_MENU_WIDTH);
tracing::info!(
index,
left = f32::from(menu.left),
@@ -7886,8 +7962,7 @@ impl EditorWindow {
/// `onContextMenu` on a clip's `SegmentRoot` (`TL/ClipTrack.tsx`): select
/// the clip unless it is already part of the selection, then open its
- /// menu at the pointer. Other tracks have no context menu, and split mode
- /// keeps every press for the cut.
+ /// menu at the pointer. Split mode keeps every press for the cut.
fn track_context_menu(
&mut self,
kind: TrackKind,
@@ -7896,7 +7971,10 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- if kind != TrackKind::Clip || self.split_mode || self.transport.is_none() {
+ if !matches!(kind, TrackKind::Clip | TrackKind::Text)
+ || self.split_mode
+ || self.transport.is_none()
+ {
return;
}
let viewport_width: f32 = window.viewport_size().width.into();
@@ -7913,13 +7991,247 @@ impl EditorWindow {
return;
};
cx.stop_propagation();
- let already_selected = self.selection.as_ref().is_some_and(|selection| {
- selection.track == TrackKind::Clip && selection.indices.contains(&index)
- });
+ let already_selected = self
+ .selection
+ .as_ref()
+ .is_some_and(|selection| selection.contains(kind, index));
if !already_selected {
- self.set_selection(Some(Selection::single(TrackKind::Clip, index)), cx);
+ self.set_selection(Some(Selection::single(kind, index)), cx);
+ }
+ match kind {
+ TrackKind::Clip => self.open_clip_menu_at(index, event.position, window, cx),
+ _ => self.open_text_menu_at(index, event.position, window, cx),
+ }
+ }
+
+ fn open_text_menu_at(
+ &mut self,
+ index: usize,
+ origin: Point<Pixels>,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ let focus = self.focus_handle_for_menu();
+ window.focus(&focus, cx);
+ self.clip_speed = None;
+ self.add_track = None;
+ let indices = self
+ .selection
+ .as_ref()
+ .filter(|selection| selection.contains(TrackKind::Text, index))
+ .map_or_else(|| vec![index], |selection| selection.indices.clone());
+ self.text_menu = Some(OpenTextMenu {
+ index,
+ indices,
+ placement: clip_menu_placement(index, origin, window.viewport_size(), TEXT_MENU_WIDTH),
+ });
+ cx.notify();
+ }
+
+ fn copy_text_style(&mut self, index: usize) {
+ let segment = self
+ .project
+ .timeline
+ .as_ref()
+ .and_then(|timeline| timeline.text_segments.get(index));
+ let Some(segment) = segment else {
+ return;
+ };
+ self.text_style_clipboard = Some(crate::editor_panels::TextStyle::of(segment));
+ tracing::info!(index, "text style copied");
+ }
+
+ fn paste_text_style(&mut self, indices: &[usize], window: &mut Window, cx: &mut Context<Self>) {
+ let Some(style) = self.text_style_clipboard.clone() else {
+ return;
+ };
+ let mut changed = false;
+ self.edit_project("text-paste-style", window, cx, |project| {
+ let Some(timeline) = project.timeline.as_mut() else {
+ return false;
+ };
+ for index in indices {
+ if let Some(segment) = timeline.text_segments.get_mut(*index)
+ && style.apply_to(segment)
+ {
+ changed = true;
+ }
+ }
+ changed
+ });
+ if changed {
+ self.note_edit("text-paste-style", Some(TrackKind::Text));
}
- self.open_clip_menu_at(index, event.position, window, cx);
+ }
+
+ fn render_text_menu(&self, cx: &mut Context<Self>) -> Option<gpui::AnyElement> {
+ let menu = self.text_menu.as_ref()?;
+ let theme = self.theme;
+ let timeline = self.project.timeline.as_ref()?;
+ let segment = timeline.text_segments.get(menu.index)?;
+ let index = menu.index;
+ let indices = menu.indices.clone();
+ let count = indices.len();
+ let can_paste = self.text_style_clipboard.is_some();
+ let title: SharedString = if count > 1 {
+ format!("{count} text segments").into()
+ } else {
+ let content = segment.content.trim();
+ if content.is_empty() {
+ "Text".into()
+ } else {
+ content.to_string().into()
+ }
+ };
+ let summary = crate::editor_clips::format_clip_duration(segment.end - segment.start);
+ let paste_label: &'static str = if count > 1 {
+ "Paste style to all selected"
+ } else {
+ "Paste style"
+ };
+ let delete_label: &'static str = if count > 1 {
+ "Delete text segments"
+ } else {
+ "Delete text segment"
+ };
+ let divider = || div().h(px(1.)).bg(Hsla::from(theme.editor.line));
+
+ let mut card = div()
+ .id("text-menu-popover")
+ .absolute()
+ .left(menu.placement.left)
+ .w(px(TEXT_MENU_WIDTH))
+ .flex()
+ .flex_col()
+ .gap(px(8.))
+ .rounded(px(12.))
+ .border_1()
+ .border_color(Hsla::from(theme.editor.line))
+ .bg(Hsla::from(theme.editor.card))
+ .p(px(6.))
+ .shadow(theme.editor.pop_shadow())
+ .on_mouse_down(
+ MouseButton::Left,
+ cx.listener(|_, _, _, cx| cx.stop_propagation()),
+ );
+ card = match menu.placement.anchor {
+ ClipMenuAnchor::Above { bottom } => card.bottom(bottom),
+ ClipMenuAnchor::Below { top } => card.top(top),
+ };
+
+ let paste_indices = indices.clone();
+ let card = card
+ .child(
+ div()
+ .flex()
+ .items_center()
+ .gap(px(8.))
+ .px(px(8.))
+ .pt(px(4.))
+ .child(
+ div()
+ .flex_1()
+ .min_w_0()
+ .overflow_hidden()
+ .whitespace_nowrap()
+ .text_ellipsis()
+ .text_size(px(12.))
+ .font_weight(FontWeight::MEDIUM)
+ .text_color(Hsla::from(theme.editor.text_1))
+ .child(title),
+ )
+ .child(
+ div()
+ .flex_none()
+ .text_size(px(10.5))
+ .text_color(Hsla::from(theme.editor.text_3))
+ .child(summary),
+ ),
+ )
+ .child(
+ div()
+ .flex()
+ .flex_col()
+ .child(
+ clip_menu_row(
+ &theme,
+ "text-menu-copy-style",
+ "icons/copy.svg",
+ "Copy style",
+ ClipMenuTrailing::None,
+ true,
+ false,
+ )
+ .on_click(cx.listener(
+ move |this, _, _window, cx| {
+ this.text_menu = None;
+ this.copy_text_style(index);
+ cx.notify();
+ },
+ )),
+ )
+ .child(
+ clip_menu_row(
+ &theme,
+ "text-menu-paste-style",
+ "icons/clipboard-paste.svg",
+ paste_label,
+ if can_paste {
+ ClipMenuTrailing::None
+ } else {
+ ClipMenuTrailing::Hint("Copy a style first")
+ },
+ can_paste,
+ false,
+ )
+ .when(can_paste, |this| {
+ this.on_click(cx.listener(move |this, _, window, cx| {
+ this.text_menu = None;
+ this.paste_text_style(&paste_indices, window, cx);
+ cx.notify();
+ }))
+ }),
+ ),
+ )
+ .child(divider())
+ .child(
+ clip_menu_row(
+ &theme,
+ "text-menu-delete",
+ "icons/trash.svg",
+ delete_label,
+ ClipMenuTrailing::Kbd("backspace"),
+ true,
+ true,
+ )
+ .on_click(cx.listener(move |this, _, window, cx| {
+ this.text_menu = None;
+ this.delete_selection(window, cx);
+ cx.notify();
+ })),
+ );
+
+ Some(
+ div()
+ .absolute()
+ .top_0()
+ .left_0()
+ .size_full()
+ .child(
+ div()
+ .id("text-menu-backdrop")
+ .absolute()
+ .top_0()
+ .left_0()
+ .size_full()
+ .on_click(cx.listener(|this, _, _, cx| {
+ this.text_menu = None;
+ cx.notify();
+ })),
+ )
+ .child(card)
+ .into_any_element(),
+ )
}
fn set_clip_timescale(
@@ -8229,6 +8541,7 @@ impl EditorWindow {
.bg(Hsla::from(theme.editor.card))
.p(px(6.))
.shadow(theme.editor.pop_shadow())
+ .occlude()
.on_mouse_down(
MouseButton::Left,
cx.listener(|_, _, _, cx| cx.stop_propagation()),
@@ -9160,6 +9473,8 @@ impl EditorWindow {
)
.child(
div()
+ .debug_selector(|| "preset-dialog".into())
+ .occlude()
.w(px(400.))
.flex()
.flex_col()
@@ -9864,11 +10179,17 @@ impl EditorWindow {
};
div()
+ .debug_selector(|| "editor-preview-canvas".into())
.relative()
.flex_1()
.min_h_0()
.overflow_hidden()
.bg(Hsla::from(theme.editor.card))
+ .capture_any_mouse_down(cx.listener(|this, event: &MouseDownEvent, window, cx| {
+ if event.button == MouseButton::Left {
+ this.focus_root(window, cx);
+ }
+ }))
.child(body)
// `CanvasElementsOverlay` + `SnapGuidesOverlay`
// (`Player.tsx:636-643`), both mounted inside the letterbox
@@ -11216,6 +11537,7 @@ impl Render for EditorWindow {
});
self.prepare_frame_fields(window, cx);
self.sync_crop_container(window);
+ self.sidebar.release_retired_images(window);
let theme = self.theme;
// The timeline's own bounds are what `secsPerPixel` divides by, and
// this window is resizable, so read them off the viewport rather than
@@ -11674,6 +11996,7 @@ impl Render for EditorWindow {
)
.children(self.with_style_controls(|this| this.render_sidebar_menu(cx)))
.children(self.render_toolbar_menu(cx))
+ .children(self.render_text_menu(cx))
.children(self.render_frame_controls(window, cx))
.children(self.render_add_track_popover(cx))
.children(self.render_clip_speed_popover(cx))
@@ -12538,6 +12861,64 @@ mod tests {
}
}
+ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.
+ #[cfg(target_os = "linux")]
+ #[gpui::test]
+ fn preview_click_returns_focus_to_the_editor(cx: &mut gpui::TestAppContext) {
+ let project = tempfile::tempdir().unwrap();
+ let project_path = project.path().to_path_buf();
+ let (editor, cx) = cx
+ .add_window_view(move |window, cx| EditorWindow::new(project_path.clone(), window, cx));
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_error("unreadable".into(), window, cx);
+ });
+ let (input_focus, editor_focus) = editor.read_with(cx, |editor, cx| {
+ (
+ editor.name_input.read(cx).focus_handle(),
+ editor.focus.clone(),
+ )
+ });
+
+ cx.update(|window, cx| window.focus(&input_focus, cx));
+ cx.update(|window, _| assert!(input_focus.is_focused(window)));
+
+ let preview = cx.debug_bounds("editor-preview-canvas").unwrap();
+ cx.simulate_mouse_move(preview.center(), None, gpui::Modifiers::default());
+ cx.simulate_click(preview.center(), gpui::Modifiers::default());
+
+ cx.update(|window, _| {
+ assert!(!input_focus.is_focused(window));
+ assert!(editor_focus.is_focused(window));
+ });
+ }
+
+ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.
+ #[cfg(target_os = "linux")]
+ #[gpui::test]
+ fn dialog_click_over_the_preview_keeps_its_focus(cx: &mut gpui::TestAppContext) {
+ let project = tempfile::tempdir().unwrap();
+ let project_path = project.path().to_path_buf();
+ let (editor, cx) = cx
+ .add_window_view(move |window, cx| EditorWindow::new(project_path.clone(), window, cx));
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_error("unreadable".into(), window, cx);
+ editor.open_preset_dialog(PresetDialog::Create { default: false }, window, cx);
+ });
+ let input_focus = editor.read_with(cx, |editor, cx| {
+ editor.preset_name_input.read(cx).focus_handle()
+ });
+ cx.run_until_parked();
+ cx.update(|window, _| assert!(input_focus.is_focused(window)));
+
+ let preview = cx.debug_bounds("editor-preview-canvas").unwrap();
+ let title = cx.debug_bounds("preset-dialog").unwrap().origin + point(px(8.), px(8.));
+ assert!(preview.contains(&title));
+ cx.simulate_mouse_move(title, None, gpui::Modifiers::default());
+ cx.simulate_click(title, gpui::Modifiers::default());
+
+ cx.update(|window, _| assert!(input_focus.is_focused(window)));
+ }
+
/// `default_editor_preview_resolution()` is asserted to be 1248x702 in the
/// Tauri app itself (`lib.rs:192-194`); the render size of a display
/// recording follows from it.
@@ -12677,6 +13058,19 @@ mod tests {
assert_eq!(playhead_extrapolation(true, true, -0.01), 0.0);
}
+ /// A paused seek stores exactly the time its engine echo carries, so the
+ /// echo's `playhead_changed` returns before rebuilding the editor again.
+ #[test]
+ fn paused_seek_lands_where_its_echo_does() {
+ for frame in 0..=EDITOR_PREVIEW_FPS * 3600 {
+ let time = (frame as f64 + 0.37) / EDITOR_PREVIEW_FPS as f64;
+ let seeked = seeked_playhead(time, frame, false);
+ assert_eq!(seeked, frame_seconds(frame));
+ assert!((frame_seconds(frame) - seeked).abs() < f64::EPSILON);
+ }
+ assert_eq!(seeked_playhead(1.234, 74, true), 1.234);
+ }
+
/// The perf gate's line. `frames` is what reached the window, `dropped`
/// what the pump refused, and the rate is measured on *painted* frames.
#[test]
diff --git a/apps/desktop-gpui/src/feeds.rs b/apps/desktop-gpui/src/feeds.rs
index 17f195422..4b02665fb 100644
--- a/apps/desktop-gpui/src/feeds.rs
+++ b/apps/desktop-gpui/src/feeds.rs
@@ -313,7 +313,7 @@ impl CameraProcessingFactory {
impl RecordingCameraWorker {
fn publish(
&self,
- image: &gpui::RenderImage,
+ image: &Arc<gpui::RenderImage>,
dims: (usize, usize),
timestamp: cap_timestamp::Timestamp,
mask: Option<cap_recording::instant_recording::LinuxCameraMaskReceipt>,
@@ -334,14 +334,16 @@ impl RecordingCameraWorker {
return;
}
}
- let Some(bytes) = image.as_bytes(0) else {
+ if image.as_bytes(0).is_none() {
self.publisher
.fail("Processed camera pixels unavailable".to_string());
return;
- };
+ }
self.publisher.publish(
cap_recording::instant_recording::LinuxProcessedCameraFrame {
- bgra: Arc::from(bytes),
+ bgra: cap_recording::instant_recording::LinuxCameraPixels::new(
+ RenderImagePixels(image.clone()),
+ ),
dimensions: (dims.0 as u32, dims.1 as u32),
stride: dims.0 * 4,
timestamp,
@@ -353,6 +355,16 @@ impl RecordingCameraWorker {
}
}
+#[cfg(target_os = "linux")]
+struct RenderImagePixels(Arc<gpui::RenderImage>);
+
+#[cfg(target_os = "linux")]
+impl AsRef<[u8]> for RenderImagePixels {
+ fn as_ref(&self) -> &[u8] {
+ self.0.as_bytes(0).unwrap_or_default()
+ }
+}
+
#[cfg(target_os = "linux")]
fn validate_recording_camera_frame(
timestamp: cap_timestamp::Timestamp,
@@ -688,8 +700,7 @@ pub struct Feeds {
pub camera: Option<SelectedCamera>,
/// Selected microphone name.
pub microphone: Option<String>,
- /// Rolling 200ms max of the mic level, in dB FS. `-96` when silent/absent.
- pub mic_level_db: f64,
+ mic_level: Entity<MicLevel>,
mic_level_shown: Arc<std::sync::atomic::AtomicU64>,
pub camera_error: Option<String>,
camera_preview_parked: bool,
@@ -729,12 +740,34 @@ pub struct SelectedCamera {
struct FeedsGlobal(Entity<Feeds>);
impl Global for FeedsGlobal {}
+/// The live mic level: rolling 200ms max in dB FS, `-96` when absent. Kept
+/// out of `Feeds` because windows read device state from `Feeds` in render,
+/// so each of its notifies redraws them, and the level changes up to 20
+/// times a second.
+pub struct MicLevel {
+ db: f64,
+}
+
+impl MicLevel {
+ pub fn db(&self) -> f64 {
+ self.db
+ }
+
+ fn set(&mut self, db: f64, cx: &mut Context<Self>) {
+ if self.db != db {
+ self.db = db;
+ cx.notify();
+ }
+ }
+}
+
impl Feeds {
pub fn camera_input_pending(&self) -> bool {
self.camera_input_pending
}
pub fn init(cx: &mut App) -> Entity<Self> {
+ let mic_level = cx.new(|_| MicLevel { db: -96.0 });
let feeds = cx.new(|_| Self {
camera_actor: None,
camera_preview_sender: None,
@@ -754,7 +787,7 @@ impl Feeds {
microphone_ready: None,
camera: None,
microphone: None,
- mic_level_db: -96.0,
+ mic_level,
mic_level_shown: Arc::new(std::sync::atomic::AtomicU64::new((-96.0f64).to_bits())),
camera_error: None,
camera_preview_parked: false,
@@ -1126,7 +1159,7 @@ impl Feeds {
self.microphone_error = None;
self.mic_input_pending = label.is_some();
self.mic_input_released = false;
- self.set_mic_level(-96.0);
+ self.set_mic_level(-96.0, cx);
let epoch = self.mic_epoch;
let actor = self.ensure_mic_actor(cx);
let gate = self.mic_input_gate.clone();
@@ -1239,13 +1272,17 @@ impl Feeds {
})
.detach();
}
- self.set_mic_level(-96.0);
+ self.set_mic_level(-96.0, cx);
cx.notify();
}
- fn set_mic_level(&mut self, db: f64) {
- self.mic_level_db = db;
+ pub fn mic_level(&self) -> &Entity<MicLevel> {
+ &self.mic_level
+ }
+
+ fn set_mic_level(&mut self, db: f64, cx: &mut Context<Self>) {
self.mic_level_shown.store(db.to_bits(), Ordering::Release);
+ self.mic_level.update(cx, |level, cx| level.set(db, cx));
}
fn ensure_camera_actor(&mut self, cx: &mut Context<Self>) -> ActorRef<CameraFeed> {
@@ -1370,20 +1407,11 @@ impl Feeds {
let _meter = meter;
while wakes.recv_async().await.is_ok() {
let level = f64::from_bits(latest.load(Ordering::Acquire));
- let changed = this.update(cx, |this: &mut Feeds, cx| {
- if this.mic_level_db == level {
- return false;
- }
- this.set_mic_level(level);
- cx.notify();
- true
- });
- match changed {
- Err(_) => return,
- Ok(true) => {
- cx.update(app_windows::refresh_controls_window);
- }
- Ok(false) => {}
+ if this
+ .update(cx, |this: &mut Feeds, cx| this.set_mic_level(level, cx))
+ .is_err()
+ {
+ return;
}
}
});
@@ -1458,6 +1486,10 @@ pub(crate) fn camera_preview_image(
Some((image, (width, height)))
}
+/// The quietest level any meter draws: `bar_level` bottoms out at -60 dB and
+/// `picker_level` at -40 dB, so anything below looks identical everywhere.
+const METER_FLOOR_DB: f64 = -60.0;
+
/// A 200ms max-hold window, coalesced to ~20 updates/s and quantized to
/// 0.5dB (under a pixel on both level mappings).
#[derive(Default)]
@@ -1501,6 +1533,7 @@ impl MeterSender {
let Some(level) = self.window.push(now, db) else {
return;
};
+ let level = level.max(METER_FLOOR_DB);
self.latest.store(level.to_bits(), Ordering::Release);
if level.to_bits() != self.shown.load(Ordering::Acquire) {
let _ = self.wake.try_send(());
@@ -1700,6 +1733,44 @@ mod tests {
assert!(wakes.try_recv().is_err());
}
+ #[test]
+ fn input_below_every_meter_floor_never_wakes_the_ui() {
+ use std::sync::atomic::AtomicU64;
+
+ let shown = Arc::new(AtomicU64::new((-96.0f64).to_bits()));
+ let latest = Arc::new(AtomicU64::new((-96.0f64).to_bits()));
+ let (wake, wakes) = flume::bounded(1);
+ let mut sender = MeterSender {
+ window: MeterWindow::default(),
+ shown: shown.clone(),
+ latest: latest.clone(),
+ wake,
+ };
+ let start = Instant::now();
+ let at = |callback: u64| start + Duration::from_micros(callback * 10_667);
+
+ sender.push(at(0), -72.0);
+ assert_eq!(wakes.try_recv(), Ok(()));
+ assert_eq!(
+ f64::from_bits(latest.load(Ordering::Acquire)),
+ METER_FLOOR_DB
+ );
+
+ shown.store(METER_FLOOR_DB.to_bits(), Ordering::Release);
+ for callback in 1..400 {
+ let db = -96.0 + (callback % 7) as f64 * 5.0;
+ sender.push(at(callback), db);
+ }
+ assert!(wakes.try_recv().is_err());
+
+ sender.push(at(400), -42.0);
+ sender.push(at(410), -42.0);
+ assert_eq!(wakes.try_recv(), Ok(()));
+ assert_eq!(f64::from_bits(latest.load(Ordering::Acquire)), -42.0);
+ assert_eq!(picker_level(METER_FLOOR_DB), picker_level(-96.0));
+ assert_eq!(bar_level(METER_FLOOR_DB), bar_level(-96.0));
+ }
+
#[test]
fn preview_rejects_frames_queued_before_input_readiness() {
let ready = cap_timestamp::Timestamps::now();
@@ -2135,6 +2206,22 @@ mod tests {
assert!(scaler.is_some());
}
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn recording_camera_pixels_share_the_preview_image() {
+ let mut frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::BGRA, 2, 1);
+ frame.data_mut(0)[..8].copy_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8]);
+ let (image, dims) = camera_preview_image(&frame, &mut None, false, None).unwrap();
+
+ let shared = RenderImagePixels(image.clone());
+ let pixels = cap_recording::instant_recording::LinuxCameraPixels::new(shared);
+
+ let preview = image.as_bytes(0).unwrap();
+ assert_eq!(pixels.as_ptr(), preview.as_ptr());
+ assert_eq!(&*pixels, preview);
+ assert_eq!(pixels.len(), dims.0 * dims.1 * 4);
+ }
+
#[test]
fn camera_preview_downscales_only_when_blur_requires_it() {
let frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::BGRA, 64, 32);
diff --git a/apps/desktop-gpui/src/hotkeys.rs b/apps/desktop-gpui/src/hotkeys.rs
index 46bb17797..175906491 100644
--- a/apps/desktop-gpui/src/hotkeys.rs
+++ b/apps/desktop-gpui/src/hotkeys.rs
@@ -216,6 +216,11 @@ pub fn pause(cx: &mut App) -> bool {
true
}
+pub fn is_paused(cx: &App) -> bool {
+ cx.try_global::<Hotkeys>()
+ .is_some_and(|hotkeys| hotkeys.paused)
+}
+
pub fn editing_blocked(cx: &App) -> bool {
#[cfg(target_os = "linux")]
if cx
diff --git a/apps/desktop-gpui/src/import.rs b/apps/desktop-gpui/src/import.rs
index 30f70292c..58b8a4438 100644
--- a/apps/desktop-gpui/src/import.rs
+++ b/apps/desktop-gpui/src/import.rs
@@ -134,6 +134,7 @@ fn imported_video_meta(
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
}),
cursor: None,
keyboard: None,
diff --git a/apps/desktop-gpui/src/jpeg_thumbnail.rs b/apps/desktop-gpui/src/jpeg_thumbnail.rs
new file mode 100644
index 000000000..85549b99a
--- /dev/null
+++ b/apps/desktop-gpui/src/jpeg_thumbnail.rs
@@ -0,0 +1,210 @@
+use image::DynamicImage;
+
+pub fn decode_jpeg_thumbnail(
+ bytes: &[u8],
+ request: impl FnOnce(u32, u32) -> (u32, u32),
+) -> Option<(DynamicImage, u32, u32)> {
+ let mut decoder = jpeg_decoder::Decoder::new(bytes);
+ decoder.read_info().ok()?;
+ let info = decoder.info()?;
+ match info.pixel_format {
+ jpeg_decoder::PixelFormat::RGB24 | jpeg_decoder::PixelFormat::L8 => {}
+ _ => return None,
+ }
+ let (requested_width, requested_height) =
+ request(u32::from(info.width), u32::from(info.height));
+ let requested_width = requested_width.clamp(1, u32::from(u16::MAX)) as u16;
+ let requested_height = requested_height.clamp(1, u32::from(u16::MAX)) as u16;
+ let (width, height) = decoder.scale(requested_width, requested_height).ok()?;
+ let pixels = decoder.decode().ok()?;
+ let decoded = match info.pixel_format {
+ jpeg_decoder::PixelFormat::RGB24 => DynamicImage::ImageRgb8(image::RgbImage::from_raw(
+ u32::from(width),
+ u32::from(height),
+ pixels,
+ )?),
+ jpeg_decoder::PixelFormat::L8 => DynamicImage::ImageLuma8(image::GrayImage::from_raw(
+ u32::from(width),
+ u32::from(height),
+ pixels,
+ )?),
+ _ => return None,
+ };
+ Some((decoded, u32::from(info.width), u32::from(info.height)))
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum ThumbnailResize {
+ Box,
+ Triangle,
+ Identity,
+}
+
+pub fn thumbnail_resize(
+ decoded_width: u32,
+ decoded_height: u32,
+ target_width: u32,
+ target_height: u32,
+) -> ThumbnailResize {
+ let decoded_width = decoded_width.max(1);
+ let decoded_height = decoded_height.max(1);
+ let target_width = target_width.max(1);
+ let target_height = target_height.max(1);
+ if decoded_width <= target_width && decoded_height <= target_height {
+ return ThumbnailResize::Identity;
+ }
+ let scale = (target_width as f32 / decoded_width as f32)
+ .min(target_height as f32 / decoded_height as f32);
+ if scale <= 0.5 {
+ ThumbnailResize::Box
+ } else {
+ ThumbnailResize::Triangle
+ }
+}
+
+pub fn cover_target(width: u32, height: u32, card_width: u32, card_height: u32) -> (f32, u32, u32) {
+ let width = width.max(1);
+ let height = height.max(1);
+ let scale = (card_width as f32 / width as f32)
+ .max(card_height as f32 / height as f32)
+ .min(1.0);
+ let target_width = ((width as f32 * scale).round() as u32).max(1);
+ let target_height = ((height as f32 * scale).round() as u32).max(1);
+ (scale, target_width, target_height)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const CARD_WIDTH: u32 = 392;
+ const CARD_HEIGHT: u32 = 224;
+
+ fn encode_rgb(width: u32, height: u32, pixel: [u8; 3]) -> Vec<u8> {
+ let image = image::RgbImage::from_pixel(width, height, image::Rgb(pixel));
+ let mut encoded = Vec::new();
+ image::codecs::jpeg::JpegEncoder::new_with_quality(&mut encoded, 75)
+ .encode_image(&image)
+ .unwrap();
+ encoded
+ }
+
+ #[test]
+ fn square_request_downsamples_before_the_full_frame() {
+ let encoded = encode_rgb(1024, 512, [40, 120, 200]);
+ let (decoded, width, height) = decode_jpeg_thumbnail(&encoded, |_, _| (128, 128)).unwrap();
+ assert_eq!((width, height), (1024, 512));
+ assert!(decoded.width() <= 256);
+ assert!(decoded.height() <= 128);
+ let pixel = decoded.to_rgb8().get_pixel(0, 0).0;
+ assert!(pixel[0].abs_diff(40) <= 3);
+ assert!(pixel[1].abs_diff(120) <= 3);
+ assert!(pixel[2].abs_diff(200) <= 3);
+ }
+
+ #[test]
+ fn library_cover_targets_match_the_card() {
+ assert_eq!(
+ cover_dims(800, 600),
+ (392, 294),
+ "800x600 covering the card"
+ );
+ assert_eq!(cover_dims(600, 600), (392, 392));
+ assert_eq!(cover_dims(1920, 1080), (398, 224));
+ assert_eq!(cover_dims(1512, 982), (392, 255));
+ }
+
+ #[test]
+ fn resize_uses_the_decoded_buffer_not_the_original_scale() {
+ let (_, target_width, target_height) = cover_target(1920, 1080, CARD_WIDTH, CARD_HEIGHT);
+ assert_eq!((target_width, target_height), (398, 224));
+ assert_eq!(
+ thumbnail_resize(1920, 1080, target_width, target_height),
+ ThumbnailResize::Box
+ );
+ assert_eq!(
+ thumbnail_resize(480, 270, target_width, target_height),
+ ThumbnailResize::Triangle
+ );
+ assert_eq!(
+ thumbnail_resize(target_width, target_height, target_width, target_height),
+ ThumbnailResize::Identity
+ );
+ }
+
+ #[test]
+ fn library_cover_idct_scales_1080p_to_a_quarter() {
+ let encoded = encode_rgb(1920, 1080, [40, 120, 200]);
+ let (decoded, width, height) = decode_jpeg_thumbnail(&encoded, |w, h| {
+ let (_, target_width, target_height) = cover_target(w, h, CARD_WIDTH, CARD_HEIGHT);
+ (target_width, target_height)
+ })
+ .unwrap();
+ assert_eq!((width, height), (1920, 1080));
+ assert_eq!((decoded.width(), decoded.height()), (480, 270));
+ let (_, target_width, target_height) = cover_target(width, height, CARD_WIDTH, CARD_HEIGHT);
+ assert_eq!(
+ thumbnail_resize(
+ decoded.width(),
+ decoded.height(),
+ target_width,
+ target_height
+ ),
+ ThumbnailResize::Triangle
+ );
+ }
+
+ #[test]
+ fn cmyk_frame_returns_none() {
+ let bytes = cmyk_jpeg();
+ let mut decoder = jpeg_decoder::Decoder::new(bytes.as_slice());
+ let pixels = decoder.decode().expect("cmyk fixture decodes");
+ assert_eq!(
+ decoder.info().map(|info| info.pixel_format),
+ Some(jpeg_decoder::PixelFormat::CMYK32)
+ );
+ assert_eq!(pixels.len(), 8 * 8 * 4);
+ assert!(decode_jpeg_thumbnail(&bytes, |w, h| (w, h)).is_none());
+ }
+
+ fn cmyk_jpeg() -> Vec<u8> {
+ fn marker(out: &mut Vec<u8>, tag: u8, payload: &[u8]) {
+ out.extend_from_slice(&[0xFF, tag]);
+ let len = u16::try_from(payload.len() + 2).unwrap();
+ out.extend_from_slice(&len.to_be_bytes());
+ out.extend_from_slice(payload);
+ }
+ fn huffman(class: u8) -> Vec<u8> {
+ let mut table = vec![class, 0x01];
+ table.extend(std::iter::repeat_n(0x00, 15));
+ table.push(0x00);
+ table
+ }
+ let mut quant = vec![0x00];
+ quant.extend(std::iter::repeat_n(0x01, 64));
+ let mut frame = vec![0x08, 0x00, 0x08, 0x00, 0x08, 0x04];
+ for id in 1..=4u8 {
+ frame.extend_from_slice(&[id, 0x11, 0x00]);
+ }
+ let mut scan = vec![0x04];
+ for id in 1..=4u8 {
+ scan.extend_from_slice(&[id, 0x00]);
+ }
+ scan.extend_from_slice(&[0x00, 0x3F, 0x00]);
+
+ let mut bytes = vec![0xFF, 0xD8];
+ marker(&mut bytes, 0xDB, &quant);
+ marker(&mut bytes, 0xC0, &frame);
+ marker(&mut bytes, 0xC4, &huffman(0x00));
+ marker(&mut bytes, 0xC4, &huffman(0x10));
+ marker(&mut bytes, 0xDA, &scan);
+ bytes.push(0x00);
+ bytes.extend_from_slice(&[0xFF, 0xD9]);
+ bytes
+ }
+
+ fn cover_dims(width: u32, height: u32) -> (u32, u32) {
+ let (_, target_width, target_height) = cover_target(width, height, CARD_WIDTH, CARD_HEIGHT);
+ (target_width, target_height)
+ }
+}
diff --git a/apps/desktop-gpui/src/library.rs b/apps/desktop-gpui/src/library.rs
index dc57098a7..f45cef1cb 100644
--- a/apps/desktop-gpui/src/library.rs
+++ b/apps/desktop-gpui/src/library.rs
@@ -72,9 +72,11 @@ pub struct RecentItem {
/// `media_sort_time_millis` (`lib.rs:3966-3972`): the filesystem `created()`
/// time of the path, falling back to `modified()`, then to the epoch.
fn media_sort_time_millis(path: &Path) -> f64 {
- let Ok(metadata) = path.metadata() else {
- return 0.0;
- };
+ path.metadata()
+ .map_or(0.0, |metadata| metadata_sort_time_millis(&metadata))
+}
+
+fn metadata_sort_time_millis(metadata: &std::fs::Metadata) -> f64 {
metadata
.created()
.or_else(|_| metadata.modified())
@@ -570,6 +572,9 @@ pub fn delete_recording_directory_in(dirs: &[PathBuf], path: &Path) -> Result<()
};
if let Some(canonical_path) = canonical_path {
+ let _thumbnails = crate::recording::hold_studio_thumbnail_writes().ok_or_else(|| {
+ "Timed out waiting for the recording's thumbnail to finish writing".to_string()
+ })?;
std::fs::remove_dir_all(canonical_path)
.map_err(|error| format!("Failed to delete recording: {error}"))?;
}
@@ -672,44 +677,61 @@ pub struct ScreenshotItem {
/// `list_screenshots` (`lib.rs:4055-4091`): `*.cap` directories only, and the
/// sort key is the PNG's timestamp rather than the directory's.
pub fn list_screenshots_in(dir: &Path) -> Vec<ScreenshotItem> {
- let Ok(entries) = std::fs::read_dir(dir) else {
- return Vec::new();
- };
+ screenshot_items_in(dir).collect()
+}
- let mut out = Vec::new();
- for entry in entries.flatten() {
- let bundle = entry.path();
- if !bundle.is_dir() || bundle.extension().and_then(|ext| ext.to_str()) != Some("cap") {
- continue;
+fn screenshot_items_in(dir: &Path) -> impl Iterator<Item = ScreenshotItem> {
+ let mut bundles = Vec::new();
+ if let Ok(entries) = std::fs::read_dir(dir) {
+ for entry in entries.flatten() {
+ let bundle = entry.path();
+ if bundle.extension().and_then(|ext| ext.to_str()) != Some("cap") || !bundle.is_dir() {
+ continue;
+ }
+ if let Some((png, sort_time_millis)) = screenshot_png(&bundle) {
+ bundles.push((bundle, png, sort_time_millis));
+ }
}
- let Ok(meta) = RecordingMeta::load_for_project(&bundle) else {
- continue;
- };
- let Some(png) = std::fs::read_dir(&bundle).ok().and_then(|entries| {
- entries
- .flatten()
- .map(|entry| entry.path())
- .find(|path| path.extension().and_then(|ext| ext.to_str()) == Some("png"))
- }) else {
- continue;
- };
+ }
+ bundles.sort_by(|(_, _, a), (_, _, b)| b.total_cmp(a));
+ bundles
+ .into_iter()
+ .filter_map(|(bundle, png, sort_time_millis)| {
+ let meta = RecordingMeta::load_for_project(&bundle).ok()?;
+ Some(ScreenshotItem {
+ path: png.clone(),
+ pretty_name: meta.pretty_name,
+ sort_time_millis,
+ thumbnail: Some(png),
+ bundle,
+ })
+ })
+}
- out.push(ScreenshotItem {
- path: png.clone(),
- pretty_name: meta.pretty_name,
- sort_time_millis: media_sort_time_millis(&png),
- thumbnail: Some(png),
- bundle,
- });
+fn screenshot_png(bundle: &Path) -> Option<(PathBuf, f64)> {
+ let original = bundle.join("original.png");
+ if let Ok(metadata) = original.metadata() {
+ return Some((original, metadata_sort_time_millis(&metadata)));
}
- out.sort_by(|a, b| b.sort_time_millis.total_cmp(&a.sort_time_millis));
- out
+ let png = std::fs::read_dir(bundle)
+ .ok()?
+ .flatten()
+ .map(|entry| entry.path())
+ .find(|path| path.extension().and_then(|ext| ext.to_str()) == Some("png"))?;
+ let sort_time_millis = media_sort_time_millis(&png);
+ Some((png, sort_time_millis))
}
pub fn list_screenshots() -> Vec<ScreenshotItem> {
list_screenshots_in(&screenshots_dir())
}
+pub fn list_screenshots_limited(limit: usize) -> Vec<ScreenshotItem> {
+ screenshot_items_in(&screenshots_dir())
+ .take(limit)
+ .collect()
+}
+
/// Delete the `.cap` bundle that owns `path` (the PNG or the directory).
pub fn delete_screenshot(path: &Path) -> Result<(), String> {
let bundle = if path.is_dir() {
@@ -750,16 +772,20 @@ pub fn delete_screenshot(path: &Path) -> Result<(), String> {
}
fn scan_screenshots(dir: &Path, out: &mut Vec<RecentItem>) {
- out.extend(list_screenshots_in(dir).into_iter().map(|item| RecentItem {
- kind: MediaKind::Screenshot,
- bundle: item.bundle,
- pretty_name: item.pretty_name,
- clip_count: 1,
- sort_time_millis: item.sort_time_millis,
- thumbnail_version: item.thumbnail.as_deref().and_then(source_mtime_nanos),
- thumbnail: item.thumbnail,
- sharing: None,
- }));
+ out.extend(
+ screenshot_items_in(dir)
+ .take(RECENT_MEDIA_LIMIT)
+ .map(|item| RecentItem {
+ kind: MediaKind::Screenshot,
+ bundle: item.bundle,
+ pretty_name: item.pretty_name,
+ clip_count: 1,
+ sort_time_millis: item.sort_time_millis,
+ thumbnail_version: item.thumbnail.as_deref().and_then(source_mtime_nanos),
+ thumbnail: item.thumbnail,
+ sharing: None,
+ }),
+ );
}
fn newest_first(items: &mut [RecentItem]) {
@@ -777,13 +803,8 @@ pub fn recent_media_in(recording_dirs: &[PathBuf], screenshots_dir: &Path) -> Ve
let mut recordings = Vec::new();
scan_recordings(recording_dirs, &mut recordings);
- let mut screenshots = Vec::new();
- scan_screenshots(screenshots_dir, &mut screenshots);
- newest_first(&mut screenshots);
- screenshots.truncate(RECENT_MEDIA_LIMIT);
-
let mut merged = recordings;
- merged.extend(screenshots);
+ scan_screenshots(screenshots_dir, &mut merged);
newest_first(&mut merged);
merged.truncate(RECENT_MEDIA_LIMIT);
merged
@@ -1060,6 +1081,11 @@ impl CacheSlot {
}
}
+#[cfg(test)]
+pub fn thumbnail_cache_file(source: &Path) -> Option<PathBuf> {
+ CacheSlot::for_source(source).map(|slot| slot.file())
+}
+
/// gpui's atlas takes BGRA; `image`'s RgbaImage is just the container (the
/// same swap gpui's own asset loader does after decoding).
pub fn rgba_to_render_image(mut rgba: image::RgbaImage) -> Arc<RenderImage> {
@@ -1094,32 +1120,50 @@ pub fn thumbnail_rgba(path: &Path) -> Option<image::RgbaImage> {
// Sniff rather than trust the extension: `list_screenshots` finds the
// preview by extension scan, and a bundle could hold a mislabelled file.
let format = image::guess_format(&bytes).ok()?;
- let decoded = image::load_from_memory_with_format(&bytes, format).ok()?;
-
- let (width, height) = (decoded.width().max(1), decoded.height().max(1));
- let scale = (THUMBNAIL_WIDTH as f32 / width as f32)
- .max(THUMBNAIL_HEIGHT as f32 / height as f32)
- .min(1.0);
- let target_width = ((width as f32 * scale).round() as u32).max(1);
- let target_height = ((height as f32 * scale).round() as u32).max(1);
+ let (decoded, width, height) = if format == image::ImageFormat::Jpeg {
+ crate::jpeg_thumbnail::decode_jpeg_thumbnail(&bytes, |width, height| {
+ let (_, target_width, target_height) = crate::jpeg_thumbnail::cover_target(
+ width,
+ height,
+ THUMBNAIL_WIDTH,
+ THUMBNAIL_HEIGHT,
+ );
+ (target_width, target_height)
+ })
+ .or_else(|| {
+ let decoded = image::load_from_memory_with_format(&bytes, format).ok()?;
+ let dimensions = (decoded.width(), decoded.height());
+ Some((decoded, dimensions.0, dimensions.1))
+ })?
+ } else {
+ let decoded = image::load_from_memory_with_format(&bytes, format).ok()?;
+ let dimensions = (decoded.width(), decoded.height());
+ (decoded, dimensions.0, dimensions.1)
+ };
+ let (width, height) = (width.max(1), height.max(1));
+ let (_, target_width, target_height) =
+ crate::jpeg_thumbnail::cover_target(width, height, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
// Box sampling for the big ratios (one pass over a multi-megapixel
// source, and at 8x down every output pixel averages a whole block);
// Triangle when the sizes are close, where box would alias and costs
// nothing to avoid.
- let rgba = if scale <= 0.5 {
- decoded
+ let rgba = match crate::jpeg_thumbnail::thumbnail_resize(
+ decoded.width(),
+ decoded.height(),
+ target_width,
+ target_height,
+ ) {
+ crate::jpeg_thumbnail::ThumbnailResize::Box => decoded
.thumbnail_exact(target_width, target_height)
- .into_rgba8()
- } else if scale < 1.0 {
- decoded
+ .into_rgba8(),
+ crate::jpeg_thumbnail::ThumbnailResize::Triangle => decoded
.resize_exact(
target_width,
target_height,
image::imageops::FilterType::Triangle,
)
- .into_rgba8()
- } else {
- decoded.into_rgba8()
+ .into_rgba8(),
+ crate::jpeg_thumbnail::ThumbnailResize::Identity => decoded.into_rgba8(),
};
if let Some(cache) = &cache {
@@ -1143,14 +1187,33 @@ pub fn thumbnail_rgba(path: &Path) -> Option<image::RgbaImage> {
/// rather than deferred.
///
/// Faithful in the parts that matter to the output: decode packets until the
-/// *first* video frame comes out, scale to RGB24 at the source's own size
-/// (`size: None` at both call sites, so a native-resolution JPEG, not a
-/// thumbnail), save as JPEG. Blocking; callers hand it to `spawn_blocking`.
+/// *first* video frame comes out and save it as a JPEG at the source's own
+/// size (`size: None` at every call site, so a native-resolution JPEG, not a
+/// thumbnail). Blocking; callers hand it to `spawn_blocking`.
pub fn create_screenshot(
input: &Path,
output: &Path,
size: Option<(u32, u32)>,
) -> Result<(), String> {
+ let frame = first_video_frame(input)?;
+ let size = size.unwrap_or((frame.width(), frame.height()));
+ let jpeg = match encode_mjpeg(&frame, size) {
+ Some(jpeg) => jpeg,
+ None => encode_rgb_jpeg(&frame, size)?,
+ };
+ if let Some(parent) = output.parent() {
+ std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
+ }
+ let staged = output.with_extension(format!("jpg.tmp-{}", crate::store::new_uuid_v4()));
+ std::fs::write(&staged, jpeg)
+ .and_then(|()| std::fs::rename(&staged, output))
+ .map_err(|e| {
+ let _ = std::fs::remove_file(&staged);
+ e.to_string()
+ })
+}
+
+fn first_video_frame(input: &Path) -> Result<ffmpeg::frame::Video, String> {
let mut ictx = ffmpeg::format::input(input).map_err(|e| e.to_string())?;
let input_stream = ictx
.streams()
@@ -1164,65 +1227,123 @@ pub fn create_screenshot(
.video()
.map_err(|e| e.to_string())?;
- let mut scaler = ffmpeg::software::scaling::context::Context::get(
- decoder.format(),
- decoder.width(),
- decoder.height(),
- ffmpeg::format::Pixel::RGB24,
- size.map_or(decoder.width(), |s| s.0),
- size.map_or(decoder.height(), |s| s.1),
- ffmpeg::software::scaling::flag::Flags::BILINEAR,
- )
- .map_err(|e| e.to_string())?;
-
let mut frame = ffmpeg::frame::Video::empty();
for (stream, packet) in ictx.packets() {
if stream.index() != video_stream_index {
continue;
}
decoder.send_packet(&packet).map_err(|e| e.to_string())?;
- if decoder.receive_frame(&mut frame).is_err() {
- continue;
+ if decoder.receive_frame(&mut frame).is_ok() {
+ return Ok(frame);
}
+ }
- let mut rgb_frame = ffmpeg::frame::Video::empty();
- scaler
- .run(&frame, &mut rgb_frame)
- .map_err(|e| e.to_string())?;
-
- // The scaler's rows are padded to its own stride; the image buffer is
- // tight, so the copy is row by row.
- let width = rgb_frame.width() as usize;
- let height = rgb_frame.height() as usize;
- let src_stride = rgb_frame.stride(0);
- let dst_stride = width * 3;
- let buffer = cap_gpui_kernels::frame::copy_rows(
- rgb_frame.data(0),
- dst_stride,
- src_stride,
- height,
- false,
- )
- .ok_or("Failed to create image from frame data")?;
+ Err("Failed to create screenshot".to_string())
+}
- let image = image::RgbImage::from_raw(width as u32, height as u32, buffer)
- .ok_or("Failed to create image from frame data")?;
- if let Some(parent) = output.parent() {
- std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
- }
- let staged = output.with_extension(format!("jpg.tmp-{}", crate::store::new_uuid_v4()));
- cap_gpui_kernels::codec::save_rgb_image(&image, &staged, image::ImageFormat::Jpeg)
- .map_err(|e| {
- let _ = std::fs::remove_file(&staged);
- e.to_string()
- })?;
- return std::fs::rename(&staged, output).map_err(|e| {
- let _ = std::fs::remove_file(&staged);
- e.to_string()
- });
+/// FFmpeg's MJPEG encoder takes the decoded YUV frame directly, so a 4K
+/// thumbnail skips the RGB round trip and `image`'s much slower JPEG encoder.
+/// 4:4:4 at qscale 4 keeps the unsubsampled layout of the quality-75 JPEG
+/// `image` wrote, with at least its quality.
+fn encode_mjpeg(frame: &ffmpeg::frame::Video, (width, height): (u32, u32)) -> Option<Vec<u8>> {
+ const QSCALE: i32 = 4;
+ let format = ffmpeg::format::Pixel::YUV444P;
+ let codec = ffmpeg::codec::encoder::find(ffmpeg::codec::Id::MJPEG)?;
+ let mut scaler = ffmpeg::software::scaling::context::Context::get(
+ frame.format(),
+ frame.width(),
+ frame.height(),
+ format,
+ width,
+ height,
+ ffmpeg::software::scaling::flag::Flags::BILINEAR,
+ )
+ .ok()?;
+ write_full_range(&mut scaler)?;
+ let mut yuv = ffmpeg::frame::Video::empty();
+ scaler.run(frame, &mut yuv).ok()?;
+
+ let mut encoder = ffmpeg::codec::context::Context::new_with_codec(codec)
+ .encoder()
+ .video()
+ .ok()?;
+ encoder.set_width(width);
+ encoder.set_height(height);
+ encoder.set_format(format);
+ encoder.set_color_range(ffmpeg::color::Range::JPEG);
+ encoder.set_time_base((1, 1));
+ encoder.set_flags(ffmpeg::codec::Flags::QSCALE);
+ encoder.set_global_quality(QSCALE * ffmpeg::ffi::FF_QP2LAMBDA);
+ encoder.set_qmin(QSCALE);
+ encoder.set_qmax(QSCALE);
+ let mut encoder = encoder.open().ok()?;
+
+ yuv.set_pts(Some(0));
+ encoder.send_frame(&yuv).ok()?;
+ encoder.send_eof().ok()?;
+ let mut packet = ffmpeg::Packet::empty();
+ encoder.receive_packet(&mut packet).ok()?;
+ packet.data().map(<[u8]>::to_vec)
+}
+
+/// JPEG stores full-range YUV, while swscale writes limited range unless told
+/// otherwise; the source keeps whatever range swscale detected for it.
+fn write_full_range(scaler: &mut ffmpeg::software::scaling::context::Context) -> Option<()> {
+ let mut inv_table = std::ptr::null_mut();
+ let mut table = std::ptr::null_mut();
+ let (mut src_range, mut dst_range) = (0, 0);
+ let (mut brightness, mut contrast, mut saturation) = (0, 0, 0);
+ unsafe {
+ let context = scaler.as_mut_ptr();
+ let read = ffmpeg::ffi::sws_getColorspaceDetails(
+ context,
+ &mut inv_table,
+ &mut src_range,
+ &mut table,
+ &mut dst_range,
+ &mut brightness,
+ &mut contrast,
+ &mut saturation,
+ );
+ (read >= 0
+ && ffmpeg::ffi::sws_setColorspaceDetails(
+ context, inv_table, src_range, table, 1, brightness, contrast, saturation,
+ ) >= 0)
+ .then_some(())
}
+}
- Err("Failed to create screenshot".to_string())
+/// The original path, for an FFmpeg build without the MJPEG encoder.
+fn encode_rgb_jpeg(
+ frame: &ffmpeg::frame::Video,
+ (width, height): (u32, u32),
+) -> Result<Vec<u8>, String> {
+ let mut rgb_frame = ffmpeg::frame::Video::empty();
+ ffmpeg::software::scaling::context::Context::get(
+ frame.format(),
+ frame.width(),
+ frame.height(),
+ ffmpeg::format::Pixel::RGB24,
+ width,
+ height,
+ ffmpeg::software::scaling::flag::Flags::BILINEAR,
+ )
+ .map_err(|e| e.to_string())?
+ .run(frame, &mut rgb_frame)
+ .map_err(|e| e.to_string())?;
+
+ // The scaler's rows are padded to its own stride; the image buffer is
+ // tight, so the copy is row by row.
+ let row_bytes = width as usize * 3;
+ let buffer = cap_gpui_kernels::frame::copy_rows(
+ rgb_frame.data(0),
+ row_bytes,
+ rgb_frame.stride(0),
+ height as usize,
+ false,
+ )
+ .ok_or("Failed to create image from frame data")?;
+ cap_gpui_kernels::codec::encode_jpeg_rgb(&buffer, width, height, 75).map_err(|e| e.to_string())
}
pub fn bundle_thumbnail_path(project_dir: &Path) -> PathBuf {
@@ -1293,6 +1414,163 @@ mod tests {
bundle
}
+ fn psnr(jpeg: &[u8], screen: &image::RgbImage) -> f64 {
+ let decoded = image::load_from_memory(jpeg).unwrap().into_rgb8();
+ assert_eq!(decoded.dimensions(), screen.dimensions());
+ let squared: f64 = decoded
+ .as_raw()
+ .iter()
+ .zip(screen.as_raw())
+ .map(|(&a, &b)| (f64::from(a) - f64::from(b)).powi(2))
+ .sum();
+ 10.0 * (255.0_f64.powi(2) * screen.as_raw().len() as f64 / squared).log10()
+ }
+
+ /// `screen` as a recording would hand it over: limited-range BT.709 4:2:0.
+ fn recorded_frame(screen: &image::RgbImage) -> ffmpeg::frame::Video {
+ let (width, height) = screen.dimensions();
+ let mut rgb = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::RGB24, width, height);
+ let stride = rgb.stride(0);
+ for (row, pixels) in rgb
+ .data_mut(0)
+ .chunks_mut(stride)
+ .zip(screen.as_raw().chunks(width as usize * 3))
+ {
+ row[..pixels.len()].copy_from_slice(pixels);
+ }
+ let mut scaler = ffmpeg::software::scaling::context::Context::get(
+ ffmpeg::format::Pixel::RGB24,
+ width,
+ height,
+ ffmpeg::format::Pixel::YUV420P,
+ width,
+ height,
+ ffmpeg::software::scaling::flag::Flags::BILINEAR,
+ )
+ .unwrap();
+ unsafe {
+ let bt709 = ffmpeg::ffi::sws_getCoefficients(ffmpeg::ffi::SWS_CS_ITU709);
+ let set = ffmpeg::ffi::sws_setColorspaceDetails(
+ scaler.as_mut_ptr(),
+ bt709,
+ 1,
+ bt709,
+ 0,
+ 0,
+ 1 << 16,
+ 1 << 16,
+ );
+ assert!(set >= 0);
+ }
+ let mut frame = ffmpeg::frame::Video::empty();
+ scaler.run(&rgb, &mut frame).unwrap();
+ frame.set_color_space(ffmpeg::color::Space::BT709);
+ frame.set_color_range(ffmpeg::color::Range::MPEG);
+ frame
+ }
+
+ #[test]
+ fn screenshot_is_written_with_the_mjpeg_encoder() {
+ ffmpeg::init().unwrap();
+ let root = temp_dir("mjpeg-screenshot");
+ let source = root.join("display.mp4");
+ std::fs::write(
+ &source,
+ include_bytes!("../../media-server/src/__tests__/fixtures/test-with-audio.mp4"),
+ )
+ .unwrap();
+ let output = bundle_thumbnail_path(&root);
+
+ create_screenshot(&source, &output, None).unwrap();
+
+ let frame = first_video_frame(&source).unwrap();
+ let written = std::fs::read(&output).unwrap();
+ let size = (frame.width(), frame.height());
+ assert_eq!(written, encode_mjpeg(&frame, size).unwrap());
+ let decoded = image::load_from_memory_with_format(&written, image::ImageFormat::Jpeg);
+ assert_eq!(decoded.unwrap().to_rgb8().dimensions(), size);
+ std::fs::remove_dir_all(root).unwrap();
+ }
+
+ #[test]
+ fn mjpeg_screenshot_is_at_least_as_close_to_the_screen() {
+ ffmpeg::init().unwrap();
+ let screen = image::RgbImage::from_fn(640, 360, |x, y| {
+ let text = (x / 2 + y / 3) % 7 == 0 && y % 24 < 14;
+ match (x / 160, text) {
+ (0, _) => image::Rgb([61, 106, 242]),
+ (_, true) => image::Rgb([212, 212, 212]),
+ (3, false) => image::Rgb([(x - 480) as u8, 53, (y / 2) as u8 + 60]),
+ _ => image::Rgb([30, 31, 34]),
+ }
+ });
+ let frame = recorded_frame(&screen);
+ let size = screen.dimensions();
+
+ let mjpeg = psnr(&encode_mjpeg(&frame, size).unwrap(), &screen);
+ let rgb = psnr(&encode_rgb_jpeg(&frame, size).unwrap(), &screen);
+
+ assert!(mjpeg >= rgb, "mjpeg {mjpeg:.2} dB, rgb {rgb:.2} dB");
+ }
+
+ /// Each source is a video; a `.png` beside it with the same name is the
+ /// screen it recorded, which both JPEGs are scored against.
+ #[test]
+ #[ignore = "needs CAP_SCREENSHOT_BENCH_SOURCES"]
+ fn benchmark_create_screenshot() {
+ ffmpeg::init().unwrap();
+ let sources = std::env::var("CAP_SCREENSHOT_BENCH_SOURCES").unwrap();
+ let median = |mut runs: Vec<f64>| {
+ runs.sort_by(f64::total_cmp);
+ runs[runs.len() / 2]
+ };
+ println!(
+ "source,width,height,decode_ms,rgb_ms,mjpeg_ms,rgb_bytes,mjpeg_bytes,rgb_psnr,mjpeg_psnr"
+ );
+ for source in sources.split(',').map(Path::new) {
+ let started = std::time::Instant::now();
+ let frame = first_video_frame(source).unwrap();
+ let decode = started.elapsed().as_secs_f64() * 1e3;
+ let size = (frame.width(), frame.height());
+ let rgb = || encode_rgb_jpeg(&frame, size).unwrap();
+ let mjpeg = || encode_mjpeg(&frame, size).unwrap();
+ let time = |encode: &dyn Fn() -> Vec<u8>| {
+ let started = std::time::Instant::now();
+ std::hint::black_box(encode());
+ started.elapsed().as_secs_f64() * 1e3
+ };
+ let (mut rgb_runs, mut mjpeg_runs) = (Vec::new(), Vec::new());
+ for run in 0..15 {
+ if run % 2 == 0 {
+ rgb_runs.push(time(&rgb));
+ mjpeg_runs.push(time(&mjpeg));
+ } else {
+ mjpeg_runs.push(time(&mjpeg));
+ rgb_runs.push(time(&rgb));
+ }
+ }
+ let (rgb, mjpeg) = (rgb(), mjpeg());
+ let screen = image::open(source.with_extension("png")).map(|png| png.into_rgb8());
+ let score = |jpeg: &[u8]| {
+ screen
+ .as_ref()
+ .map_or(String::new(), |screen| format!("{:.2}", psnr(jpeg, screen)))
+ };
+ println!(
+ "{},{},{},{decode:.1},{:.1},{:.1},{},{},{},{}",
+ source.display(),
+ size.0,
+ size.1,
+ median(rgb_runs),
+ median(mjpeg_runs),
+ rgb.len(),
+ mjpeg.len(),
+ score(&rgb),
+ score(&mjpeg),
+ );
+ }
+ }
+
fn temp_dir(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!(
"cap-gpui-library-{tag}-{}-{:?}",
@@ -1481,6 +1759,129 @@ mod tests {
std::fs::remove_dir_all(root).unwrap();
}
+ #[test]
+ fn bounded_screenshots_match_the_full_listing_and_parse_lazily() {
+ fn eager_listing(dir: &Path) -> Vec<ScreenshotItem> {
+ let mut out = Vec::new();
+ for entry in std::fs::read_dir(dir).unwrap().flatten() {
+ let bundle = entry.path();
+ if !bundle.is_dir()
+ || bundle.extension().and_then(|ext| ext.to_str()) != Some("cap")
+ {
+ continue;
+ }
+ let Ok(meta) = RecordingMeta::load_for_project(&bundle) else {
+ continue;
+ };
+ let Some(png) = std::fs::read_dir(&bundle)
+ .unwrap()
+ .flatten()
+ .find_map(|entry| {
+ let path = entry.path();
+ (path.extension().and_then(|ext| ext.to_str()) == Some("png"))
+ .then_some(path)
+ })
+ else {
+ continue;
+ };
+ out.push(ScreenshotItem {
+ path: png.clone(),
+ pretty_name: meta.pretty_name,
+ sort_time_millis: media_sort_time_millis(&png),
+ thumbnail: Some(png),
+ bundle,
+ });
+ }
+ out.sort_by(|a, b| b.sort_time_millis.total_cmp(&a.sort_time_millis));
+ out
+ }
+
+ fn write_shot(dir: &Path, name: &str) -> PathBuf {
+ write_bundle(
+ dir,
+ name,
+ &format!(r#"{{"pretty_name":"{name}","sharing":null,"fps":0}}"#),
+ )
+ }
+
+ let root = temp_dir("bounded-screenshots");
+ let legacy = write_shot(&root, "legacy");
+ std::fs::write(legacy.join("shot.png"), b"png").unwrap();
+ write_shot(&root, "no-png");
+ let not_a_bundle = root.join("not-a-bundle");
+ std::fs::create_dir_all(&not_a_bundle).unwrap();
+ std::fs::write(not_a_bundle.join("original.png"), b"png").unwrap();
+ std::fs::write(root.join("loose.cap"), b"").unwrap();
+ for i in 0..30 {
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let bundle = write_shot(&root, &format!("shot-{:02}", (i * 7) % 30));
+ std::fs::write(bundle.join("original.png"), b"png").unwrap();
+ }
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let tie_a = write_shot(&root, "tie-a");
+ std::fs::write(tie_a.join("original.png"), b"png").unwrap();
+ let tie_b = write_shot(&root, "tie-b");
+ std::fs::hard_link(tie_a.join("original.png"), tie_b.join("original.png")).unwrap();
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let corrupt = write_bundle(&root, "corrupt", "{ not json ");
+ std::fs::write(corrupt.join("original.png"), b"png").unwrap();
+
+ let full = list_screenshots_in(&root);
+ assert_eq!(full, eager_listing(&root));
+ assert_eq!(full.len(), 33);
+ assert!(full.iter().all(|item| item.bundle != corrupt));
+ let legacy_item = full.iter().find(|item| item.bundle == legacy).unwrap();
+ assert_eq!(legacy_item.path, legacy.join("shot.png"));
+
+ let read_dir_order: Vec<PathBuf> = std::fs::read_dir(&root)
+ .unwrap()
+ .flatten()
+ .map(|entry| entry.path())
+ .collect();
+ let read_dir_position = |bundle: &Path| {
+ read_dir_order
+ .iter()
+ .position(|path| path == bundle)
+ .unwrap()
+ };
+ let tie_time = |bundle: &Path| {
+ full.iter()
+ .find(|item| item.bundle == bundle)
+ .unwrap()
+ .sort_time_millis
+ };
+ assert_eq!(tie_time(&tie_a), tie_time(&tie_b));
+ assert!(
+ full.windows(2).all(|pair| {
+ pair[0].sort_time_millis > pair[1].sort_time_millis
+ || (pair[0].sort_time_millis == pair[1].sort_time_millis
+ && read_dir_position(&pair[0].bundle) < read_dir_position(&pair[1].bundle))
+ }),
+ "newest first, equal timestamps keep directory order"
+ );
+
+ for limit in [0, 1, 9, 20, 100] {
+ assert_eq!(
+ screenshot_items_in(&root).take(limit).collect::<Vec<_>>(),
+ full[..limit.min(full.len())],
+ "limit {limit}"
+ );
+ }
+
+ let mut lazy = screenshot_items_in(&root);
+ let head: Vec<_> = lazy.by_ref().take(RECENT_MEDIA_LIMIT).collect();
+ assert_eq!(head, full[..RECENT_MEDIA_LIMIT]);
+ for item in &full[RECENT_MEDIA_LIMIT..] {
+ std::fs::remove_file(item.bundle.join("recording-meta.json")).unwrap();
+ }
+ assert!(
+ lazy.next().is_none(),
+ "metas past the consumed prefix are read on demand"
+ );
+
+ std::fs::remove_dir_all(&root).unwrap();
+ }
+
#[test]
#[ignore = "manual filesystem benchmark; set CAP_GPUI_LIBRARY_BENCH_DIR and CAP_GPUI_LIBRARY_BENCH_MODE"]
fn recording_scan_benchmark() {
diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs
index 9e8860035..ecdc47011 100644
--- a/apps/desktop-gpui/src/main.rs
+++ b/apps/desktop-gpui/src/main.rs
@@ -43,6 +43,7 @@ mod feeds;
mod hotkeys;
mod import;
mod installer;
+mod jpeg_thumbnail;
mod library;
mod main_window;
mod media_access;
@@ -436,6 +437,7 @@ fn main() {
#[cfg(target_os = "linux")]
single_instance::init_linux_reopen(cx);
updates::schedule_startup_check(cx);
+ app_windows::schedule_launch_render_warmup(cx);
upload::queue::init(cx);
// The app menu (and with it ⌘W/⌘M/⌘Q) and the status-bar item. Both
diff --git a/apps/desktop-gpui/src/main_window.rs b/apps/desktop-gpui/src/main_window.rs
index 3953585cb..71f3e43b0 100644
--- a/apps/desktop-gpui/src/main_window.rs
+++ b/apps/desktop-gpui/src/main_window.rs
@@ -792,6 +792,7 @@ pub struct MainWindow {
camera_id: Option<recording::DeviceOrModelID>,
microphone: Option<MicrophoneOption>,
microphone_level: Entity<ui::MicrophoneLevel>,
+ device_microphone_level: Entity<ui::MicrophoneLevel>,
pending_device_restore: crate::store::RecordingInputSettings,
device_restore_suspended: bool,
device_format_target: Option<DeviceFormatTarget>,
@@ -851,6 +852,8 @@ pub struct MainWindow {
input_enumeration_gate: devices::InputEnumerationGate,
/// `scheduleTargetListPrewarm` (`new-main/index.tsx:1897-1965`).
prewarm_task: Option<gpui::Task<()>>,
+ keyboard_nav: bool,
+ _keyboard_nav: gpui::Subscription,
}
/// `createLicenseQuery` (`utils/queries.ts:257-273`): pro from the auth
@@ -942,11 +945,20 @@ impl MainWindow {
// Track the app-scoped feeds: the camera bubble's close button
// deselects the camera there, and this window's selection has to
- // follow. Repaints are gated to what is actually visible -- the mic
- // meter notifies at ~20Hz and would otherwise repaint the home view
- // for a level bar only the microphone picker shows.
+ // follow. Repaints are gated to what is actually visible.
let feeds = Feeds::global(cx);
- let microphone_level = cx.new(|cx| ui::MicrophoneLevel::new(&feeds, window, cx));
+ let mic_level = feeds.read(cx).mic_level().clone();
+ let microphone_level = cx.new(|cx| {
+ ui::MicrophoneLevel::new(
+ ui::MicrophoneLevelKind::CaptureButton,
+ &mic_level,
+ window,
+ cx,
+ )
+ });
+ let device_microphone_level = cx.new(|cx| {
+ ui::MicrophoneLevel::new(ui::MicrophoneLevelKind::DeviceRow, &mic_level, window, cx)
+ });
cx.observe(&feeds, |this: &mut Self, feeds, cx| {
let feeds = feeds.read(cx);
let camera_id = feeds.camera.as_ref().map(|camera| &camera.id);
@@ -1059,6 +1071,8 @@ impl MainWindow {
})
.detach();
+ let keyboard_nav = ui::keyboard_nav::install(window, cx, Self::keyboard_nav_blocked);
+
Self {
theme,
// `rawOptions.mode` -- the recording mode is a persisted setting,
@@ -1075,6 +1089,7 @@ impl MainWindow {
camera_id: None,
microphone: None,
microphone_level,
+ device_microphone_level,
pending_device_restore: crate::store::RecordingInputSettings::load(),
device_restore_suspended: false,
device_format_target: None,
@@ -1113,9 +1128,19 @@ impl MainWindow {
input_poll_generation: 0,
input_enumeration_gate: devices::InputEnumerationGate::default(),
prewarm_task: None,
+ keyboard_nav: false,
+ _keyboard_nav: keyboard_nav,
}
}
+ fn keyboard_nav_blocked(&self, cx: &gpui::App) -> bool {
+ self.session.read(cx).phase != Phase::Idle
+ || self.microphone_warning.is_some()
+ || self.sign_in_prompt.is_some()
+ || self.idle_error(cx).is_some()
+ || app_windows::clean_capture_pending(cx)
+ }
+
/// Kick off device enumeration.
///
/// Deliberately *not* called from `new`. `new` runs inside `open_window`'s
@@ -1265,11 +1290,14 @@ impl MainWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- for image in self.thumbnails.retain_displays(&displays) {
+ let dropped = self.thumbnails.retain_displays(&displays);
+ let changed = self.devices.install_displays(displays) || !dropped.is_empty();
+ for image in dropped {
let _ = window.drop_image(image);
}
- self.devices.displays = displays;
- cx.notify();
+ if changed {
+ cx.notify();
+ }
}
fn install_windows(
@@ -1278,11 +1306,14 @@ impl MainWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- for image in self.thumbnails.retain_windows(&windows) {
+ let dropped = self.thumbnails.retain_windows(&windows);
+ let changed = self.devices.install_windows(windows) || !dropped.is_empty();
+ for image in dropped {
let _ = window.drop_image(image);
}
- self.devices.windows = windows;
- cx.notify();
+ if changed {
+ cx.notify();
+ }
}
/// Start one capture sweep, or return `None` because one is already in
@@ -1375,8 +1406,9 @@ impl MainWindow {
self.thumbnails.set_window_inflight(true);
let (events_tx, events) = flume::unbounded();
+ let current = self.thumbnails.captured_windows();
let capture = gpui_tokio::Tokio::spawn(cx, async move {
- target_thumbnails::capture_windows(events_tx).await;
+ target_thumbnails::capture_windows(events_tx, current).await;
});
Some(cx.spawn_in(window, async move |this, cx| {
@@ -1598,11 +1630,12 @@ impl MainWindow {
id,
image,
app_icon,
+ entry,
} => {
let icon = app_icon.map(|bytes| {
std::sync::Arc::new(gpui::Image::from_bytes(gpui::ImageFormat::Png, bytes))
});
- if let Some(old) = self.thumbnails.insert_window(&id, image, icon) {
+ if let Some(old) = self.thumbnails.insert_window(&id, image, icon, entry) {
let _ = window.drop_image(old);
}
}
@@ -2704,12 +2737,35 @@ impl MainWindow {
impl Render for MainWindow {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.sync_appearance(window, cx);
+ if self.keyboard_nav_blocked(cx) {
+ ui::keyboard_nav::leave(window, cx);
+ }
+ self.keyboard_nav = ui::keyboard_nav::active(window, cx);
let theme = self.theme;
let meter_active = self.panel.is_none()
&& self.microphone.is_some()
&& self.session.read(cx).phase == Phase::Idle;
self.microphone_level.update(cx, |meter, cx| {
- meter.configure(meter_active, theme.blue_9.into(), cx);
+ meter.configure(
+ meter_active,
+ theme.blue_9.into(),
+ gpui::transparent_black(),
+ cx,
+ );
+ });
+ let device_meter_active = self.microphone.is_some()
+ && matches!(self.panel, Some(Panel::Device(DeviceMenu::Microphone)));
+ self.device_microphone_level.update(cx, |meter, cx| {
+ meter.configure(
+ device_meter_active,
+ theme.blue_500.into(),
+ if theme.is_dark() {
+ gpui::hsla(0., 0., 1., 0.1)
+ } else {
+ gpui::hsla(0., 0., 0., 0.07)
+ },
+ cx,
+ );
});
div()
@@ -2771,6 +2827,7 @@ impl Render for MainWindow {
cx.stop_propagation();
}
}))
+ .child(ui::keyboard_nav::pointer_watcher())
}
}
@@ -3465,6 +3522,7 @@ impl MainWindow {
/// 8px from the window edges (`mx-2`).
fn render_header_actions(&self, cx: &mut Context<Self>) -> impl IntoElement {
let theme = self.theme;
+ let keyboard_nav = self.keyboard_nav;
// `IconButton::header`: a 20px hit box with no fill, `text-gray-11`
// going to `text-gray-12` on hover.
@@ -3483,6 +3541,9 @@ impl MainWindow {
.group(id)
.when(cfg!(target_os = "windows"), |this| this.occlude())
.tab_index(0)
+ .when(keyboard_nav, ui::keyboard_nav::ring)
+ .role(gpui::Role::Button)
+ .aria_label(label)
.flex()
.items_center()
.justify_center()
@@ -3910,6 +3971,9 @@ impl MainWindow {
.child(
div()
.id("panel-back")
+ .when(self.keyboard_nav, ui::keyboard_nav::stop)
+ .role(gpui::Role::Button)
+ .aria_label("Back")
.flex()
.flex_row()
.items_center()
@@ -4140,8 +4204,8 @@ impl MainWindow {
(Some(items), None)
}
LibraryKind::Screenshots => {
- let mut items = library::list_screenshots();
- items.truncate(library::LIBRARY_PANEL_LIMIT);
+ let items =
+ library::list_screenshots_limited(library::LIBRARY_PANEL_LIMIT);
(None, Some(items))
}
}
@@ -4721,13 +4785,14 @@ impl MainWindow {
self.library_action(("lib-ss-copy", index), "icons/copy.svg", {
let png = png.clone();
move |_, _, cx| {
- if let Err(error) = crate::platform::copy_image_to_clipboard(&png, cx) {
- tracing::warn!("copying screenshot failed: {error}");
- cx.spawn(async move |_| {
+ let copied = crate::platform::copy_image_to_clipboard(png.clone(), cx);
+ cx.spawn(async move |_| {
+ if let Err(error) = copied.await {
+ tracing::warn!("copying screenshot failed: {error}");
crate::platform::alert_dialog("Could not copy screenshot", &error);
- })
- .detach();
- }
+ }
+ })
+ .detach();
}
}),
)
@@ -5158,9 +5223,7 @@ impl MainWindow {
.map(|description| format!("Available: {description}"))
},
selected,
- selected.then(|| {
- feeds::picker_level(Feeds::global(cx).read(cx).mic_level_db)
- }),
+ selected.then(|| self.device_microphone_level.clone()),
cx.listener(move |this, _, _window, cx| {
if !this.device_changes_allowed(cx) {
return;
@@ -5640,6 +5703,10 @@ impl MainWindow {
div()
.id(id)
+ .when(self.keyboard_nav, ui::keyboard_nav::stop)
+ .role(gpui::Role::Button)
+ .aria_label(label.clone())
+ .aria_selected(selected)
.flex()
.flex_col()
.w(px(self.target_card_width()))
@@ -5874,10 +5941,9 @@ impl MainWindow {
label: String,
detail: Option<String>,
selected: bool,
- // `MicrophoneListItem`'s live level wash: a white 25% overlay whose
- // right edge sits at `level * 100%` (1 = silence, the web app's
- // orientation). Only the selected microphone row gets one.
- audio_level: Option<f64>,
+ // `MicrophoneListItem`'s live level bar. Only the selected
+ // microphone row gets one.
+ audio_level: Option<Entity<ui::MicrophoneLevel>>,
on_click: impl Fn(&gpui::ClickEvent, &mut Window, &mut gpui::App) + 'static,
) -> gpui::Stateful<gpui::Div> {
let theme = self.theme;
@@ -5885,6 +5951,10 @@ impl MainWindow {
div()
.id(id)
+ .when(self.keyboard_nav, ui::keyboard_nav::stop)
+ .role(gpui::Role::Button)
+ .aria_label(label.clone())
+ .aria_selected(selected)
.relative()
.flex()
.flex_row()
@@ -5930,26 +6000,7 @@ impl MainWindow {
.child(detail)
}))
.when_some(audio_level.filter(|_| selected), |this, level| {
- this.child(
- div()
- .mt(px(3.))
- .h(px(3.))
- .w_full()
- .rounded_full()
- .overflow_hidden()
- .bg(if theme.is_dark() {
- gpui::hsla(0., 0., 1., 0.1)
- } else {
- gpui::hsla(0., 0., 0., 0.07)
- })
- .child(
- div()
- .h_full()
- .rounded_full()
- .bg(accent)
- .w(gpui::relative(1. - level.clamp(0., 1.) as f32)),
- ),
- )
+ this.child(level)
}),
)
.when(selected, |this| {
@@ -6159,6 +6210,10 @@ impl MainWindow {
div()
.id(SharedString::from(id))
.tab_index(0)
+ .when(self.keyboard_nav, ui::keyboard_nav::ring)
+ .role(gpui::Role::Button)
+ .aria_label(mode.panel_title())
+ .aria_selected(selected)
.relative()
.on_hover(cx.listener(move |this, hovered, _, cx| {
this.update_mode_hover(mode, false, *hovered, cx);
@@ -6227,6 +6282,9 @@ impl MainWindow {
div()
.id("mode-info")
.tab_index(0)
+ .when(self.keyboard_nav, ui::keyboard_nav::ring)
+ .role(gpui::Role::Button)
+ .aria_label("Recording mode info")
.tooltip(move |_, cx| ui::Tooltip::new(&theme, "Recording mode info").view(cx))
.tooltip_show_delay(ui::TOOLTIP_SHOW_DELAY)
.absolute()
@@ -6312,6 +6370,12 @@ impl MainWindow {
.child(
div()
.id(dropdown_id.clone())
+ .when(self.keyboard_nav, ui::keyboard_nav::stop)
+ .role(gpui::Role::Button)
+ .aria_label(match target {
+ TargetType::Display => "Choose a display",
+ _ => "Choose a window",
+ })
.flex()
.w(px(28.))
.rounded_r(px(9.))
@@ -6403,6 +6467,10 @@ impl MainWindow {
let base = div()
.id(SharedString::from(target.label()))
+ .when(self.keyboard_nav, ui::keyboard_nav::stop)
+ .role(gpui::Role::Button)
+ .aria_label(target.label())
+ .aria_selected(selected)
.flex()
.flex_1()
.py(px(8.))
@@ -6576,6 +6644,9 @@ impl MainWindow {
div()
.id(SharedString::from(id))
+ .when(self.keyboard_nav, ui::keyboard_nav::stop)
+ .role(gpui::Role::Button)
+ .aria_label(label.clone())
.relative()
.flex()
.flex_row()
diff --git a/apps/desktop-gpui/src/menus.rs b/apps/desktop-gpui/src/menus.rs
index 93f5f6c23..0974118e6 100644
--- a/apps/desktop-gpui/src/menus.rs
+++ b/apps/desktop-gpui/src/menus.rs
@@ -27,9 +27,11 @@ use gpui::{
use crate::{
app_windows::{self, AppWindows},
+ hotkeys,
main_window::MainWindow,
platform,
session::{Phase, RecordingSession},
+ settings_window::Page,
store,
ui::text_input,
};
@@ -146,6 +148,8 @@ actions!(
Zoom,
/// `PredefinedMenuItem::fullscreen` -- ⌃⌘F.
ToggleFullscreen,
+ /// Settings… -- ⌘, on macOS, Ctrl+, on Windows and Linux.
+ OpenSettings,
]
);
@@ -157,15 +161,7 @@ pub fn init(cx: &mut App) {
// commands. Deliberately *not* including cmd-z/x/c/v/a -- those stay the
// text field's own `TextInput`-scoped bindings (and, for the editor, its
// `on_key_down` project-undo). A global binding here would shadow both.
- cx.bind_keys([
- KeyBinding::new("cmd-q", Quit, None),
- KeyBinding::new("cmd-,", Preferences, None),
- KeyBinding::new("cmd-h", HideSelf, None),
- KeyBinding::new("alt-cmd-h", HideOthers, None),
- KeyBinding::new("cmd-w", CloseWindow, None),
- KeyBinding::new("cmd-m", Minimize, None),
- KeyBinding::new("ctrl-cmd-f", ToggleFullscreen, None),
- ]);
+ cx.bind_keys(bindings());
cx.on_action(|_: &CheckForUpdate, cx: &mut App| {
crate::updates::check_manually(cx);
@@ -213,6 +209,29 @@ pub fn init(cx: &mut App) {
cx.on_action(|_: &CloseWindow, cx: &mut App| close_key_window(cx));
cx.on_action(|_: &Minimize, cx: &mut App| with_key_native(cx, platform::minimize_native));
cx.on_action(|_: &Zoom, cx: &mut App| with_key_native(cx, platform::zoom_native));
+ cx.on_action(|_: &OpenSettings, cx: &mut App| {
+ // While a recording runs, onboarding is up or the Shortcuts page is
+ // recording a combo, the chord belongs to whatever has focus.
+ let idle = RecordingSession::global(cx).read(cx).phase == Phase::Idle;
+ let onboarding_open = cx
+ .try_global::<AppWindows>()
+ .is_some_and(|windows| windows.onboarding.is_some());
+ if !settings_shortcut_allowed(idle, onboarding_open, hotkeys::is_paused(cx)) {
+ cx.propagate();
+ return;
+ }
+ // Deferred: the keystroke arrives inside the key window's dispatch, and
+ // `open_settings` updates the settings window, which may be that one.
+ cx.defer(|cx: &mut App| {
+ let page = cx
+ .try_global::<AppWindows>()
+ .and_then(|windows| windows.settings)
+ .and_then(|settings| settings.read(cx).ok().map(|settings| settings.page))
+ .unwrap_or(Page::General);
+ app_windows::open_settings(page, cx);
+ cx.activate(true);
+ });
+ });
cx.on_action(|_: &ToggleFullscreen, cx: &mut App| {
#[cfg(target_os = "windows")]
if let Some(handle) = cx.active_window() {
@@ -252,9 +271,11 @@ fn install_menus(cx: &mut App) {
platform::patch_standard_application_menu();
}
-/// The menu bar hierarchy and app-specific destinations.
-fn build_menus(update_status: &crate::updates::UpdateStatus) -> Vec<Menu> {
- let update_row = UpdateRow::of(update_status);
+/// The menu bar model.
+///
+/// One-to-one with `build_macos_app_menu`, including the empty Help submenu
+/// (`Submenu::with_id_and_items(.., HELP_SUBMENU_ID, "Help", true, &[])`).
+pub fn build_menus() -> Vec<Menu> {
vec![
Menu {
name: APP_NAME.into(),
@@ -263,7 +284,9 @@ fn build_menus(update_status: &crate::updates::UpdateStatus) -> Vec<Menu> {
MenuItem::action(format!("About {APP_NAME}"), About),
MenuItem::action(update_row.title, CheckForUpdate).disabled(!update_row.enabled),
MenuItem::separator(),
- MenuItem::action("Preferences…", Preferences),
+ // MenuItem::action("Preferences…", Preferences),
+ MenuItem::separator(),
+ MenuItem::action("Settings…", OpenSettings),
MenuItem::separator(),
MenuItem::os_submenu("Services", SystemMenuType::Services),
MenuItem::separator(),
@@ -583,10 +606,11 @@ pub fn quit(cx: &mut App) {
}
let phase = session.read(cx).phase;
let ui_cleanup_pending = app_windows::clean_capture_owned(cx);
+ let finishing = ui_cleanup_pending || crate::recording::studio_thumbnails_pending();
let Some((generation, action)) = cx
.global_mut::<QuitCoordinator>()
.gate
- .request(quit_phase(phase, ui_cleanup_pending))
+ .request(quit_phase(phase, finishing))
else {
return;
};
@@ -612,9 +636,23 @@ pub fn quit(cx: &mut App) {
}
}
+pub(crate) fn recheck_pending_quit(cx: &mut App) {
+ let Some(generation) = cx
+ .try_global::<QuitCoordinator>()
+ .and_then(|coordinator| coordinator.gate.pending)
+ .map(|(generation, _)| generation)
+ else {
+ return;
+ };
+ advance_pending_quit(generation, false, cx);
+}
+
fn advance_pending_quit(generation: u64, timed_out: bool, cx: &mut App) {
let session = RecordingSession::global(cx);
- let phase = quit_phase(session.read(cx).phase, app_windows::clean_capture_owned(cx));
+ let phase = quit_phase(
+ session.read(cx).phase,
+ app_windows::clean_capture_owned(cx) || crate::recording::studio_thumbnails_pending(),
+ );
let failed = session.read(cx).error.is_some();
let action = cx
.global_mut::<QuitCoordinator>()
@@ -1035,7 +1073,60 @@ mod tests {
assert!(gate.pending.is_none());
}
- /// Separators are spelled `"-"` so the ordering is checked too.
+ #[test]
+ fn settings_shortcut_yields_to_recording_onboarding_and_the_recorder() {
+ assert!(settings_shortcut_allowed(true, false, false));
+ assert!(!settings_shortcut_allowed(false, false, false));
+ assert!(!settings_shortcut_allowed(true, true, false));
+ assert!(!settings_shortcut_allowed(true, false, true));
+ }
+
+ fn keystrokes(binding: &KeyBinding) -> Vec<gpui::Keystroke> {
+ binding
+ .keystrokes()
+ .iter()
+ .map(|keystroke| keystroke.inner().clone())
+ .collect()
+ }
+
+ #[test]
+ fn global_keymap_is_unchanged_apart_from_settings() {
+ let settings = if cfg!(target_os = "macos") {
+ "cmd-,"
+ } else {
+ "ctrl-,"
+ };
+ let expected: &[(&str, &dyn gpui::Action)] = &[
+ ("cmd-q", &Quit),
+ ("cmd-h", &HideSelf),
+ ("alt-cmd-h", &HideOthers),
+ ("cmd-w", &CloseWindow),
+ ("cmd-m", &Minimize),
+ ("ctrl-cmd-f", &ToggleFullscreen),
+ (settings, &OpenSettings),
+ ];
+ let bindings = bindings();
+ assert_eq!(bindings.len(), expected.len());
+ for (binding, (source, action)) in bindings.iter().zip(expected) {
+ assert_eq!(
+ keystrokes(binding),
+ vec![gpui::Keystroke::parse(source).unwrap()],
+ "{source}"
+ );
+ assert!(binding.action().partial_eq(*action), "{source}");
+ assert!(binding.predicate().is_none(), "{source}");
+ }
+ let mut seen = std::collections::HashSet::new();
+ for binding in &bindings {
+ assert!(
+ seen.insert(keystrokes(binding)),
+ "{binding:?} is bound twice"
+ );
+ }
+ }
+
+ /// The menu bar, transcribed from `build_macos_app_menu`. Separators are
+ /// spelled `"-"` so the ordering is checked too.
fn describe(menu: &Menu) -> (String, Vec<String>) {
let items = menu
.items
@@ -1071,7 +1162,7 @@ mod tests {
"About Cap".to_string(),
"Check for Update".into(),
"-".into(),
- "Preferences…".into(),
+ "Settings…".into(),
"-".into(),
"Services".into(),
"-".into(),
diff --git a/apps/desktop-gpui/src/mode_select_window.rs b/apps/desktop-gpui/src/mode_select_window.rs
index 660d27316..77dc4fc84 100644
--- a/apps/desktop-gpui/src/mode_select_window.rs
+++ b/apps/desktop-gpui/src/mode_select_window.rs
@@ -9,11 +9,11 @@
use gpui::{
Context, FocusHandle, FontWeight, Hsla, InteractiveElement, IntoElement, ParentElement, Pixels,
- Point, Render, SharedString, StatefulInteractiveElement, Styled, Window, div,
- prelude::FluentBuilder, px, svg,
+ Point, Render, Role, SharedString, StatefulInteractiveElement, Styled, Subscription, Window,
+ div, prelude::FluentBuilder, px, svg,
};
-use crate::{main_window::Mode, theme::Theme};
+use crate::{main_window::Mode, theme::Theme, ui::keyboard_nav};
/// `.inner_size(580.0, 340.0)` / `.min_inner_size(580.0, 340.0)` /
/// `.resizable(false)` on the `ShowCapWindow::ModeSelect` arm in `windows.rs`,
@@ -45,6 +45,8 @@ pub struct ModeSelectWindow {
/// cannot drift.
mode: Mode,
focus: FocusHandle,
+ keyboard_nav: bool,
+ _keyboard_nav: Subscription,
}
impl ModeSelectWindow {
@@ -67,6 +69,8 @@ impl ModeSelectWindow {
theme: Theme::for_window(window, cx, false),
mode,
focus: cx.focus_handle(),
+ keyboard_nav: false,
+ _keyboard_nav: keyboard_nav::install(window, cx, |_, _| false),
}
}
@@ -104,6 +108,11 @@ impl ModeSelectWindow {
div()
.id(SharedString::from(mode.panel_title()))
+ .when(self.keyboard_nav, keyboard_nav::stop)
+ .role(Role::Button)
+ .aria_label(mode.panel_title())
+ .aria_description(mode.card_description())
+ .aria_selected(selected)
.relative()
.flex()
.flex_col()
@@ -236,10 +245,12 @@ impl ModeSelectWindow {
impl Render for ModeSelectWindow {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.sync_appearance(window, cx);
+ self.keyboard_nav = keyboard_nav::active(window, cx);
let theme = self.theme;
let shell = div()
.track_focus(&self.focus)
+ .child(keyboard_nav::pointer_watcher())
// `flex flex-col relative justify-center items-center min-h-screen
// bg-gray-1`
.size_full()
diff --git a/apps/desktop-gpui/src/platform.rs b/apps/desktop-gpui/src/platform.rs
index 8e39012a4..8b71158ae 100644
--- a/apps/desktop-gpui/src/platform.rs
+++ b/apps/desktop-gpui/src/platform.rs
@@ -558,10 +558,54 @@ fn clipboard_file_path(path: &std::path::Path) -> Result<std::path::PathBuf, Str
Ok(path)
}
-pub fn copy_image_to_clipboard(path: &std::path::Path, cx: &gpui::App) -> Result<(), String> {
- let bytes = std::fs::read(path)
- .map_err(|error| format!("Could not read the clipboard image: {error}"))?;
- copy_image_bytes_to_clipboard(&bytes, cx)
+// Prepares finish in completion order, not click order, so only the newest
+// request may write: an older copy finishing late must not replace a newer one.
+struct ClipboardWrites {
+ latest: std::sync::atomic::AtomicU64,
+}
+
+impl ClipboardWrites {
+ const fn new() -> Self {
+ Self {
+ latest: std::sync::atomic::AtomicU64::new(0),
+ }
+ }
+
+ fn request(&self) -> u64 {
+ self.latest
+ .fetch_add(1, std::sync::atomic::Ordering::SeqCst)
+ + 1
+ }
+
+ fn is_current(&self, id: u64) -> bool {
+ self.latest.load(std::sync::atomic::Ordering::SeqCst) == id
+ }
+}
+
+static CLIPBOARD_WRITES: ClipboardWrites = ClipboardWrites::new();
+
+pub fn copy_image_to_clipboard(
+ path: std::path::PathBuf,
+ cx: &gpui::App,
+) -> gpui::Task<Result<(), String>> {
+ let id = CLIPBOARD_WRITES.request();
+ cx.spawn(async move |cx| {
+ let image = cx
+ .background_executor()
+ .spawn(async move {
+ let bytes = std::fs::read(&path)
+ .map_err(|error| format!("Could not read the clipboard image: {error}"))?;
+ prepare_clipboard_image(bytes)
+ })
+ .await?;
+ cx.update(|cx| {
+ if !CLIPBOARD_WRITES.is_current(id) {
+ tracing::debug!("skipping a superseded clipboard image write");
+ return Ok(());
+ }
+ write_clipboard_image(image, cx)
+ })
+ })
}
#[cfg(any(not(target_os = "macos"), test))]
@@ -2258,7 +2302,13 @@ mod mac {
})
}
- pub fn copy_image_bytes_to_clipboard(bytes: &[u8], _cx: &gpui::App) -> Result<(), String> {
+ pub struct ClipboardImage(Vec<u8>);
+
+ pub fn prepare_clipboard_image(bytes: Vec<u8>) -> Result<ClipboardImage, String> {
+ Ok(ClipboardImage(bytes))
+ }
+
+ pub fn write_clipboard_image(image: ClipboardImage, _cx: &gpui::App) -> Result<(), String> {
use objc2::rc::autoreleasepool;
use objc2::runtime::Bool;
use objc2::{class, msg_send};
@@ -2269,7 +2319,7 @@ mod mac {
if pasteboard.is_null() {
return Err("Clipboard unavailable".into());
}
- let data = NSData::with_bytes(bytes);
+ let data = NSData::with_bytes(&image.0);
let image: *mut AnyObject = msg_send![class!(NSImage), alloc];
let image: *mut AnyObject = msg_send![image, initWithData: &*data];
let image = Retained::from_raw(image).ok_or("Failed to load image")?;
@@ -2931,8 +2981,10 @@ mod stub {
.map_err(|error| format!("Could not copy the file to the clipboard: {error}"))
}
- pub fn copy_image_bytes_to_clipboard(bytes: &[u8], cx: &gpui::App) -> Result<(), String> {
- let format = match image::guess_format(bytes).map_err(|error| error.to_string())? {
+ pub struct ClipboardImage(gpui::ClipboardItem);
+
+ pub fn prepare_clipboard_image(bytes: Vec<u8>) -> Result<ClipboardImage, String> {
+ let format = match image::guess_format(&bytes).map_err(|error| error.to_string())? {
image::ImageFormat::Png => gpui::ImageFormat::Png,
image::ImageFormat::Jpeg => gpui::ImageFormat::Jpeg,
image::ImageFormat::WebP => gpui::ImageFormat::Webp,
@@ -2941,8 +2993,14 @@ mod stub {
image::ImageFormat::Tiff => gpui::ImageFormat::Tiff,
_ => return Err("Unsupported clipboard image format".into()),
};
- let image = gpui::Image::from_bytes(format, bytes.to_vec());
- cx.try_write_to_clipboard(gpui::ClipboardItem::new_image(&image))
+ let image = gpui::Image::from_bytes(format, bytes);
+ Ok(ClipboardImage(gpui::ClipboardItem {
+ entries: vec![gpui::ClipboardEntry::Image(image)],
+ }))
+ }
+
+ pub fn write_clipboard_image(image: ClipboardImage, cx: &gpui::App) -> Result<(), String> {
+ cx.try_write_to_clipboard(image.0)
.map_err(|error| format!("Could not copy the image to the clipboard: {error}"))
}
pub fn desktop_picture_path() -> Option<std::path::PathBuf> {
@@ -3044,6 +3102,52 @@ mod tests {
assert!(!requests.request());
}
+ #[test]
+ fn newest_clipboard_request_wins() {
+ let writes = super::ClipboardWrites::new();
+ let a = writes.request();
+ let b = writes.request();
+ assert!(!writes.is_current(a));
+ assert!(writes.is_current(b));
+ }
+
+ #[test]
+ fn completed_clipboard_write_does_not_invalidate_a_later_request() {
+ let writes = super::ClipboardWrites::new();
+ let a = writes.request();
+ assert!(writes.is_current(a));
+ let b = writes.request();
+ assert!(writes.is_current(b));
+ assert!(!writes.is_current(a));
+ }
+
+ #[test]
+ fn concurrent_clipboard_requests_get_distinct_ids_and_only_the_newest_is_current() {
+ let writes = std::sync::Arc::new(super::ClipboardWrites::new());
+ let threads: Vec<_> = (0..8)
+ .map(|_| {
+ let writes = writes.clone();
+ std::thread::spawn(move || writes.request())
+ })
+ .collect();
+ let mut ids: Vec<u64> = threads
+ .into_iter()
+ .map(|thread| thread.join().unwrap())
+ .collect();
+ ids.sort_unstable();
+ ids.dedup();
+ assert_eq!(ids, (1..=8).collect::<Vec<_>>());
+ let current: Vec<_> = ids.iter().filter(|id| writes.is_current(**id)).collect();
+ assert_eq!(current, [&8]);
+ }
+
+ #[test]
+ fn clipboard_request_ids_increase_from_one() {
+ let writes = super::ClipboardWrites::new();
+ let ids: Vec<_> = (0..4).map(|_| writes.request()).collect();
+ assert_eq!(ids, [1, 2, 3, 4]);
+ }
+
#[test]
fn confirmation_accepts_native_ok_and_matching_custom_label_only() {
let accept = "Install update";
diff --git a/apps/desktop-gpui/src/platform/windows.rs b/apps/desktop-gpui/src/platform/windows.rs
index e4a762749..8a25c01de 100644
--- a/apps/desktop-gpui/src/platform/windows.rs
+++ b/apps/desktop-gpui/src/platform/windows.rs
@@ -647,20 +647,48 @@ pub fn copy_file_to_clipboard(path: &Path, _cx: &gpui::App) -> Result<(), String
.map_err(|error| format!("Could not copy the file to the clipboard: {error}"))
}
-pub fn copy_image_bytes_to_clipboard(bytes: &[u8], _cx: &gpui::App) -> Result<(), String> {
+pub struct ClipboardImage(clipboard_rs::RustImageData);
+
+pub fn prepare_clipboard_image(bytes: Vec<u8>) -> Result<ClipboardImage, String> {
+ use clipboard_rs::RustImageData;
use clipboard_rs::common::RustImage;
- use clipboard_rs::{Clipboard, ClipboardContext, RustImageData};
- let image = RustImageData::from_bytes(bytes)
- .map_err(|error| format!("Could not load the clipboard image: {error}"))?;
+ RustImageData::from_bytes(&bytes)
+ .map(ClipboardImage)
+ .map_err(|error| format!("Could not load the clipboard image: {error}"))
+}
+
+pub fn write_clipboard_image(image: ClipboardImage, _cx: &gpui::App) -> Result<(), String> {
+ use clipboard_rs::{Clipboard, ClipboardContext};
+
ClipboardContext::new()
.map_err(|error| format!("Clipboard unavailable: {error}"))?
- .set_image(image)
+ .set_image(image.0)
.map_err(|error| format!("Could not copy the image to the clipboard: {error}"))
}
pub fn desktop_picture_path() -> Option<PathBuf> {
- None
+ use std::ffi::OsString;
+ use std::os::windows::ffi::OsStringExt;
+ use windows_sys::Win32::UI::WindowsAndMessaging::{
+ SPI_GETDESKWALLPAPER, SystemParametersInfoW,
+ };
+
+ let mut buffer = vec![0u16; 32_768];
+ let success = unsafe {
+ SystemParametersInfoW(
+ SPI_GETDESKWALLPAPER,
+ buffer.len() as u32,
+ buffer.as_mut_ptr().cast(),
+ 0,
+ )
+ };
+ if success == 0 {
+ tracing::warn!(error = %std::io::Error::last_os_error(), "failed to read desktop wallpaper path");
+ return None;
+ }
+ let length = buffer.iter().position(|character| *character == 0)?;
+ (length > 0).then(|| PathBuf::from(OsString::from_wide(&buffer[..length])))
}
pub fn set_activation_policy(_regular: bool) -> bool {
diff --git a/apps/desktop-gpui/src/presets.rs b/apps/desktop-gpui/src/presets.rs
index 1ca45d815..27c8f3b67 100644
--- a/apps/desktop-gpui/src/presets.rs
+++ b/apps/desktop-gpui/src/presets.rs
@@ -158,6 +158,8 @@ pub fn apply_preset(
) -> Option<ProjectConfiguration> {
let mut next: ProjectConfiguration = serde_json::from_value(config.clone()).ok()?;
next.timeline = current.timeline.clone();
+ next.text_size_version = current.text_size_version;
+ next.text_anim_version = current.text_anim_version;
next.overlay_order = current.overlay_order.clone();
next.clips = current.clips.clone();
Some(next)
@@ -264,6 +266,68 @@ mod tests {
assert!(applied.timeline.is_some(), "current timeline is kept");
}
+ #[test]
+ fn applying_a_legacy_tauri_preset_keeps_text_unchanged_after_reopening() {
+ let store: Value = serde_json::from_str(
+ r#"{
+ "presets": {
+ "default": 0,
+ "presets": [{
+ "name": "Legacy Tauri preset",
+ "config": {
+ "background": { "padding": 24.0 },
+ "camera": { "size": 45.0, "zoom_size": 65.0 },
+ "timeline": null,
+ "clips": []
+ }
+ }]
+ }
+ }"#,
+ )
+ .unwrap();
+ let presets = PresetsStore::from_section(store["presets"].as_object().unwrap().clone());
+ let current: ProjectConfiguration = serde_json::from_str(
+ r#"{
+ "textSizeVersion": 1,
+ "textAnimVersion": 1,
+ "timeline": {
+ "segments": [{ "recordingSegment": 0, "start": 0.0, "end": 5.0, "timescale": 1.0 }],
+ "zoomSegments": [],
+ "textSegments": [{
+ "start": 1.0,
+ "end": 4.0,
+ "content": "Keep this title",
+ "size": { "x": 0.6, "y": 0.4 },
+ "fontSize": 48.0,
+ "fadeDuration": 0.15,
+ "animationIn": "slideUp",
+ "animationOut": "slideDown",
+ "animationInDuration": 0.4,
+ "animationOutDuration": 0.6
+ }]
+ },
+ "clips": [{ "index": 0, "offsets": { "mic": -0.125 } }]
+ }"#,
+ )
+ .unwrap();
+ let applied = apply_preset(&presets.presets[0].config, &current).unwrap();
+ let directory = tempfile::tempdir().unwrap();
+ applied.write(directory.path()).unwrap();
+ let reopened = ProjectConfiguration::load(directory.path()).unwrap();
+
+ assert_eq!(reopened.background.padding, 24.0);
+ assert_eq!(reopened.camera.size, 45.0);
+ assert_eq!(reopened.camera.zoom_size, Some(65.0));
+ assert_eq!(
+ serde_json::to_value(&reopened.timeline).unwrap(),
+ serde_json::to_value(&current.timeline).unwrap()
+ );
+ assert_eq!(
+ serde_json::to_value(&reopened.clips).unwrap(),
+ serde_json::to_value(&current.clips).unwrap()
+ );
+ }
+
#[test]
fn malformed_entries_are_skipped_on_load() {
let mut section = Map::new();
diff --git a/apps/desktop-gpui/src/recording.rs b/apps/desktop-gpui/src/recording.rs
index b9bb2cf8b..a1ec64676 100644
--- a/apps/desktop-gpui/src/recording.rs
+++ b/apps/desktop-gpui/src/recording.rs
@@ -459,6 +459,16 @@ async fn finalize_studio(
{
tracing::warn!(%error, "Could not apply the Studio Sound default");
}
+ if crate::store::GeneralSettings::load().macbook_notch_overlay
+ && completed.meta.display_notch().is_some()
+ && matches!(
+ capture_target,
+ ScreenCaptureTarget::Display { .. } | ScreenCaptureTarget::Area { .. }
+ )
+ && let Err(error) = enable_notch_overlay(&project_path)
+ {
+ tracing::warn!(%error, "Could not apply the notch overlay preference");
+ }
Ok::<_, anyhow::Error>(completed)
})
.await
@@ -474,7 +484,12 @@ async fn finalize_studio(
}
let project_path = completed.project_path.clone();
+ let pending = PendingStudioThumbnail::new();
tokio::task::spawn_blocking(move || {
+ let _pending = pending;
+ let _writing = STUDIO_THUMBNAIL_WRITES
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
let thumbnail_started = std::time::Instant::now();
if let Some(display_path) = studio_display_path(&project_path) {
write_bundle_thumbnail(&project_path, &display_path);
@@ -483,9 +498,7 @@ async fn finalize_studio(
elapsed_ms = thumbnail_started.elapsed().as_secs_f64() * 1000.0,
"Studio stop thumbnail complete"
);
- })
- .await
- .context("studio post-finalize task")?;
+ });
Ok(completed.project_path)
}
@@ -1452,6 +1465,61 @@ fn studio_display_path(project_path: &std::path::Path) -> Option<PathBuf> {
path.is_file().then_some(path)
}
+static STUDIO_THUMBNAIL_WRITES: Mutex<()> = Mutex::new(());
+
+/// Deleting a recording waits for a Studio stop thumbnail being written, which
+/// checks its bundle still exists under the same lock, so the two can neither
+/// race nor leave a deleted bundle half recreated.
+pub(crate) fn hold_studio_thumbnail_writes() -> Option<std::sync::MutexGuard<'static, ()>> {
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
+ loop {
+ match STUDIO_THUMBNAIL_WRITES.try_lock() {
+ Ok(guard) => return Some(guard),
+ Err(std::sync::TryLockError::Poisoned(poisoned)) => return Some(poisoned.into_inner()),
+ Err(std::sync::TryLockError::WouldBlock) if std::time::Instant::now() < deadline => {
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ Err(std::sync::TryLockError::WouldBlock) => return None,
+ }
+ }
+}
+
+static PENDING_STUDIO_THUMBNAILS: std::sync::atomic::AtomicUsize =
+ std::sync::atomic::AtomicUsize::new(0);
+
+/// A Studio stop returns before its thumbnail is written: quit waits while one
+/// is pending, and the library views that scanned on the way to idle rescan
+/// once it lands.
+struct PendingStudioThumbnail;
+
+impl PendingStudioThumbnail {
+ fn new() -> Self {
+ PENDING_STUDIO_THUMBNAILS.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
+ Self
+ }
+}
+
+impl Drop for PendingStudioThumbnail {
+ fn drop(&mut self) {
+ PENDING_STUDIO_THUMBNAILS.fetch_sub(1, std::sync::atomic::Ordering::AcqRel);
+ let _ = studio_thumbnail_channel().0.send(());
+ }
+}
+
+fn studio_thumbnail_channel() -> &'static (flume::Sender<()>, flume::Receiver<()>) {
+ static CHANNEL: std::sync::OnceLock<(flume::Sender<()>, flume::Receiver<()>)> =
+ std::sync::OnceLock::new();
+ CHANNEL.get_or_init(flume::unbounded)
+}
+
+pub(crate) fn studio_thumbnail_events() -> flume::Receiver<()> {
+ studio_thumbnail_channel().1.clone()
+}
+
+pub(crate) fn studio_thumbnails_pending() -> bool {
+ PENDING_STUDIO_THUMBNAILS.load(std::sync::atomic::Ordering::Acquire) > 0
+}
+
/// Write `<bundle>/screenshots/display.jpg` -- the file both apps' Recents
/// cards draw. `cap-recording` does not produce it, so without this a project
/// recorded here would show the icon fallback forever, including in the
@@ -1562,6 +1630,34 @@ fn enable_studio_sound(project_path: &std::path::Path) -> std::io::Result<()> {
result
}
+fn enable_notch_overlay(project_path: &std::path::Path) -> std::io::Result<()> {
+ let path = project_path.join("project-config.json");
+ let mut config: serde_json::Value = serde_json::from_slice(&std::fs::read(&path)?)?;
+ let object = config.as_object_mut().ok_or_else(|| {
+ std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ "Project configuration is not an object",
+ )
+ })?;
+ let background = object
+ .entry("background")
+ .or_insert_with(|| serde_json::json!({}));
+ let background = background.as_object_mut().ok_or_else(|| {
+ std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ "Background configuration is not an object",
+ )
+ })?;
+ background.insert("notch".into(), serde_json::json!({ "enabled": true }));
+ let temp = path.with_extension(format!("notch-{}.tmp", crate::store::new_uuid_v4()));
+ let result = std::fs::write(&temp, serde_json::to_vec_pretty(&config)?)
+ .and_then(|()| std::fs::rename(&temp, path));
+ if result.is_err() {
+ let _ = std::fs::remove_file(temp);
+ }
+ result
+}
+
fn apply_animated_gradient_to_project_config(
project_dir: &std::path::Path,
capture_target: &ScreenCaptureTarget,
@@ -3533,6 +3629,26 @@ mod tests {
std::fs::remove_dir_all(dir).unwrap();
}
+ #[test]
+ fn notch_overlay_is_enabled_without_touching_other_settings() {
+ let dir = temp_project("notch-overlay");
+ let path = dir.join("project-config.json");
+ let mut original =
+ serde_json::to_value(cap_project::ProjectConfiguration::default()).unwrap();
+ original["background"]["future"] = 7.into();
+ original["futureProjectSetting"] = true.into();
+ std::fs::write(&path, serde_json::to_vec(&original).unwrap()).unwrap();
+
+ enable_notch_overlay(&dir).unwrap();
+
+ let written: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
+ assert_eq!(written["background"]["future"], 7);
+ assert_eq!(written["futureProjectSetting"], true);
+ let parsed: cap_project::ProjectConfiguration = serde_json::from_value(written).unwrap();
+ assert!(parsed.background.notch.is_some_and(|notch| notch.enabled));
+ std::fs::remove_dir_all(&dir).unwrap();
+ }
+
/// A config written by a build that predates `backgroundBlur` (or one that
/// never had a camera section at all) gets the key created rather than
/// being skipped.
@@ -3965,6 +4081,127 @@ mod studio_finalization_tests {
assert!(finalization.wait().await.is_err());
}
+ static THUMBNAIL_TESTS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
+
+ fn stopped_studio_project(tag: &str) -> studio_recording::CompletedRecording {
+ use cap_project::{RecordingMeta, RecordingMetaInner, StudioRecordingMeta};
+
+ let _ = ffmpeg::init();
+ let root = std::env::temp_dir().join(format!(
+ "cap-gpui-stop-thumbnail-{tag}-{}.cap",
+ crate::store::new_uuid_v4()
+ ));
+ std::fs::create_dir_all(root.join("content")).unwrap();
+ std::fs::write(
+ root.join("content/display.mp4"),
+ include_bytes!("../../media-server/src/__tests__/fixtures/test-with-audio.mp4"),
+ )
+ .unwrap();
+ let meta: StudioRecordingMeta = serde_json::from_value(serde_json::json!({
+ "display": { "path": "content/display.mp4", "fps": 30 }
+ }))
+ .unwrap();
+ RecordingMeta {
+ platform: None,
+ project_path: root.clone(),
+ pretty_name: "Stop".into(),
+ sharing: None,
+ inner: RecordingMetaInner::Studio(Box::new(meta.clone())),
+ upload: None,
+ }
+ .save_for_project()
+ .unwrap();
+ studio_recording::CompletedRecording {
+ project_path: root,
+ meta,
+ cursor_data: Default::default(),
+ clean_stopped: None,
+ }
+ }
+
+ async fn next_thumbnail_event(written: &flume::Receiver<()>) {
+ tokio::time::timeout(std::time::Duration::from_secs(30), written.recv_async())
+ .await
+ .unwrap()
+ .unwrap();
+ }
+
+ #[tokio::test(flavor = "multi_thread")]
+ async fn studio_stop_writes_its_thumbnail_after_returning() {
+ let _serial = THUMBNAIL_TESTS.lock().await;
+ let completed = stopped_studio_project("written");
+ let root = completed.project_path.clone();
+ let written = studio_thumbnail_events();
+ while written.try_recv().is_ok() {}
+
+ let writing = hold_studio_thumbnail_writes().unwrap();
+ let project = tokio::time::timeout(
+ std::time::Duration::from_secs(30),
+ finalize_studio(completed, ScreenCaptureTarget::CameraOnly, None, None),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(project, root);
+ assert!(studio_thumbnails_pending());
+ assert!(!crate::library::bundle_thumbnail_path(&root).exists());
+ drop(writing);
+
+ next_thumbnail_event(&written).await;
+ assert!(!studio_thumbnails_pending());
+ let thumbnail = image::open(crate::library::bundle_thumbnail_path(&root))
+ .unwrap()
+ .into_rgb8();
+ assert_eq!(thumbnail.dimensions(), (320, 240));
+ std::fs::remove_dir_all(root).unwrap();
+ }
+
+ #[tokio::test(flavor = "multi_thread")]
+ async fn thumbnail_written_after_a_delete_does_not_recreate_the_bundle() {
+ let _serial = THUMBNAIL_TESTS.lock().await;
+ let completed = stopped_studio_project("deleted");
+ let root = completed.project_path.clone();
+ let written = studio_thumbnail_events();
+ while written.try_recv().is_ok() {}
+
+ let deleting = hold_studio_thumbnail_writes().unwrap();
+ finalize_studio(completed, ScreenCaptureTarget::CameraOnly, None, None)
+ .await
+ .unwrap();
+ assert!(studio_thumbnails_pending());
+ std::fs::remove_dir_all(&root).unwrap();
+ drop(deleting);
+
+ next_thumbnail_event(&written).await;
+ assert!(!root.exists());
+ }
+
+ #[tokio::test(flavor = "multi_thread")]
+ async fn deleting_a_recording_waits_for_its_thumbnail_write() {
+ let _serial = THUMBNAIL_TESTS.lock().await;
+ let library = std::env::temp_dir().join(format!(
+ "cap-gpui-stop-thumbnail-library-{}",
+ crate::store::new_uuid_v4()
+ ));
+ let bundle = library.join("Recording.cap");
+ std::fs::create_dir_all(bundle.join("screenshots")).unwrap();
+
+ let writing = hold_studio_thumbnail_writes().unwrap();
+ let delete = std::thread::spawn({
+ let library = library.clone();
+ let bundle = bundle.clone();
+ move || crate::library::delete_recording_directory_in(&[library], &bundle)
+ });
+ std::thread::sleep(std::time::Duration::from_millis(200));
+ assert!(!delete.is_finished());
+ assert!(bundle.exists());
+ drop(writing);
+
+ assert_eq!(delete.join().unwrap(), Ok(()));
+ assert!(!bundle.exists());
+ std::fs::remove_dir_all(library).unwrap();
+ }
+
#[test]
fn readiness_identity_does_not_confuse_retries_or_later_recordings() {
let (_, first) = channel();
diff --git a/apps/desktop-gpui/src/screenshot_annotations.rs b/apps/desktop-gpui/src/screenshot_annotations.rs
index 2bd7b5393..11bd4e64a 100644
--- a/apps/desktop-gpui/src/screenshot_annotations.rs
+++ b/apps/desktop-gpui/src/screenshot_annotations.rs
@@ -2242,7 +2242,7 @@ impl ScreenshotEditorWindow {
// -- Mask overlays ---------------------------------------------------------
fn mask_requests(&self) -> Vec<MaskRequest> {
- if self.frame_rgba.is_none() {
+ if self.frame.is_none() {
return Vec::new();
}
let size = (
@@ -2312,14 +2312,16 @@ impl ScreenshotEditorWindow {
}
let previous = self.annotation_state.masks.clone();
- let Some(rgba) = self.frame_rgba.clone() else {
+ let Some(frame) = self.frame.clone() else {
return;
};
self.annotation_state.mask_pending = Some(requests.clone());
self.annotation_state.mask_task = Some(cx.spawn_in(window, async move |this, cx| {
let overlays = cx
.background_executor()
- .spawn(async move { build_mask_overlays(&rgba, &previous, &requests) })
+ .spawn(async move {
+ build_mask_overlays(frame.as_bytes(0).unwrap_or_default(), &previous, &requests)
+ })
.await;
this.update_in(cx, |this, window, cx| {
let overlays = finish_mask_build(&this.annotation_state.masks, overlays);
@@ -3952,14 +3954,16 @@ fn checker_quarters(size: f32) -> Vec<AnyElement> {
/// `renderMaskOverlays` + `blurRegion` (`Preview.tsx:537-687`): resample the
/// frame's own pixels inside each mask and hand back one image per region.
/// Runs on the background executor -- the regions are small, but a 50-block
-/// pixelate over a 4K screenshot is still not main-thread work.
+/// pixelate over a 4K screenshot is still not main-thread work. The filter
+/// treats every channel alike, so it runs on the atlas's BGRA as-is and the
+/// overlays come out already in atlas order.
fn build_mask_overlays(
- rgba: &[u8],
+ bgra: &[u8],
previous: &[MaskEntry<MaskOverlay>],
requests: &[MaskRequest],
) -> Vec<MaskEntry<MaskOverlay>> {
update_masks(previous, requests, |request| {
- let processed = request.filter(rgba)?;
+ let processed = request.filter(bgra)?;
Some(MaskOverlay {
rect: Rect {
x: request.region.0 as f64,
@@ -3967,7 +3971,9 @@ fn build_mask_overlays(
width: processed.width() as f64,
height: processed.height() as f64,
},
- image: crate::library::rgba_to_render_image(processed),
+ image: Arc::new(RenderImage::new(smallvec::smallvec![image::Frame::new(
+ processed
+ )])),
})
})
}
@@ -4523,6 +4529,68 @@ mod tests {
assert_eq!(mask_region(&away, rect, (200, 200)), None);
}
+ #[test]
+ fn mask_overlays_from_bgra_match_filtering_rgba_then_swapping() {
+ let (width, height) = (37u32, 23u32);
+ let stride = (width as usize * 4).next_multiple_of(256);
+ let padded: Vec<u8> = (0..stride * height as usize)
+ .map(|index| (index * 31 + index / 7) as u8)
+ .collect();
+ let rgba = cap_gpui_kernels::frame::unpad_rows(
+ &padded,
+ width as usize * 4,
+ stride,
+ height as usize,
+ )
+ .unwrap();
+ let bgra = cap_gpui_kernels::frame::unpad_rgba_to_bgra(
+ &padded,
+ width as usize,
+ stride,
+ height as usize,
+ )
+ .unwrap();
+ let frame = MaskFrame {
+ revision: 1,
+ size: (width, height),
+ content: [0., 0., f64::from(width), f64::from(height)].map(f64::to_bits),
+ padding: 0_f64.to_bits(),
+ crop: None,
+ };
+ let mut masks = Vec::new();
+ for (index, (mask_type, level, region)) in [
+ (MaskType::Blur, 4., (0., 0., 37., 23.)),
+ (MaskType::Blur, 9., (3., 1., 17., 11.)),
+ (MaskType::Blur, 16., (20., 5., 17., 18.)),
+ (MaskType::Blur, 33., (1., 2., 35., 21.)),
+ (MaskType::Pixelate, 2., (0., 0., 37., 23.)),
+ (MaskType::Pixelate, 7., (5., 3., 13., 9.)),
+ (MaskType::Pixelate, 20., (11., 7., 26., 16.)),
+ ]
+ .into_iter()
+ .enumerate()
+ {
+ let mut mask = shape(AnnotationType::Mask, region.0, region.1, region.2, region.3);
+ mask.id = index.to_string();
+ mask.mask_type = Some(mask_type);
+ mask.mask_level = Some(level);
+ masks.push(mask);
+ }
+ let requests: Vec<_> = masks
+ .iter()
+ .filter_map(|mask| mask_request(mask, frame))
+ .collect();
+ assert_eq!(requests.len(), masks.len());
+
+ let overlays = build_mask_overlays(&bgra, &[], &requests);
+ for (request, entry) in requests.iter().zip(&overlays) {
+ let expected = crate::library::rgba_to_render_image(request.filter(&rgba).unwrap());
+ let overlay = entry.value.as_ref().unwrap();
+ assert_eq!(overlay.image.size(0), expected.size(0));
+ assert_eq!(overlay.image.as_bytes(0), expected.as_bytes(0));
+ }
+ }
+
/// The resample itself: one overlay per mask, at the region's own size,
/// with the row slicing staying inside the frame buffer.
#[test]
diff --git a/apps/desktop-gpui/src/screenshot_crop.rs b/apps/desktop-gpui/src/screenshot_crop.rs
index 4e45a960a..a882b4716 100644
--- a/apps/desktop-gpui/src/screenshot_crop.rs
+++ b/apps/desktop-gpui/src/screenshot_crop.rs
@@ -86,6 +86,18 @@ pub struct ScreenshotCropDialog {
_field_subscriptions: Vec<gpui::Subscription>,
}
+impl ScreenshotCropDialog {
+ /// The original is decoded at full resolution (about 59 MB at 5K) and the
+ /// window's atlas keeps every painted image until it is told otherwise, so
+ /// each closed dialog would otherwise stay resident until the editor
+ /// window closes.
+ fn release_image(&mut self, window: &mut Window) {
+ if let Some(image) = self.image.take() {
+ let _ = window.drop_image(image);
+ }
+ }
+}
+
impl ScreenshotEditorWindow {
/// The header's crop button (`Header.tsx:84-93`): open the dialog seeded
/// with the crop in force, or the whole image. Gated on `image_size` the
@@ -163,10 +175,16 @@ impl ScreenshotEditorWindow {
/// Close without writing -- Escape or the backdrop. The project was never
/// touched, so there is nothing to roll back.
- pub(crate) fn cancel_crop_dialog(&mut self, reason: &'static str, cx: &mut Context<Self>) {
- if self.crop.take().is_none() {
+ pub(crate) fn cancel_crop_dialog(
+ &mut self,
+ reason: &'static str,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ let Some(mut dialog) = self.crop.take() else {
return;
- }
+ };
+ dialog.release_image(window);
tracing::info!(reason, "screenshot crop cancelled");
cx.notify();
}
@@ -174,10 +192,11 @@ impl ScreenshotEditorWindow {
/// The footer's Save (`Editor.tsx:463-482`): one `setProject` write of
/// `background.crop`, so one history entry, then close.
fn save_crop_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
- let Some(dialog) = self.crop.take() else {
+ let Some(mut dialog) = self.crop.take() else {
return;
};
let bounds = dialog.state.real();
+ dialog.release_image(window);
let crop = Crop {
position: XY::new(bounds.x.max(0.) as u32, bounds.y.max(0.) as u32),
size: XY::new(bounds.width.max(0.) as u32, bounds.height.max(0.) as u32),
@@ -470,7 +489,7 @@ impl ScreenshotEditorWindow {
return true;
}
if key == "escape" {
- self.cancel_crop_dialog("escape", cx);
+ self.cancel_crop_dialog("escape", window, cx);
return true;
}
let Some(nudge) = engine::is_nudge_key(key) else {
@@ -668,8 +687,8 @@ impl ScreenshotEditorWindow {
.absolute()
.inset_0()
.bg(gpui::hsla(0., 0., 0., 0.8))
- .on_click(cx.listener(|this, _, _window, cx| {
- this.cancel_crop_dialog("backdrop", cx)
+ .on_click(cx.listener(|this, _, window, cx| {
+ this.cancel_crop_dialog("backdrop", window, cx)
})),
)
.child(
diff --git a/apps/desktop-gpui/src/screenshot_editor.rs b/apps/desktop-gpui/src/screenshot_editor.rs
index 24707fd26..96489a861 100644
--- a/apps/desktop-gpui/src/screenshot_editor.rs
+++ b/apps/desktop-gpui/src/screenshot_editor.rs
@@ -912,12 +912,10 @@ pub struct ScreenshotEditorWindow {
/// renders.
exporting: bool,
export_status: ExportStatus,
- /// The latest GPU frame, already BGRA in gpui's atlas format.
- frame: Option<Arc<RenderImage>>,
- /// The same frame as tight RGBA. `Preview.tsx` reads the preview canvas
- /// back to blur/pixelate mask regions (`:537-687`); the mask overlay
- /// resamples this instead of the atlas image.
- pub(crate) frame_rgba: Option<Arc<Vec<u8>>>,
+ /// The latest GPU frame, already BGRA in gpui's atlas format. `Preview.tsx`
+ /// reads the preview canvas back to blur/pixelate mask regions
+ /// (`:537-687`); the mask overlay resamples these same pixels.
+ pub(crate) frame: Option<Arc<RenderImage>>,
pub(crate) mask_frame_revision: u64,
pub(crate) frame_size: (f32, f32),
/// `originalImageSize` -- the source PNG's dimensions, which the crop
@@ -1003,7 +1001,6 @@ impl ScreenshotEditorWindow {
exporting: false,
export_status: ExportStatus::Idle,
frame: None,
- frame_rgba: None,
mask_frame_revision: 0,
frame_size: (1., 1.),
image_size: None,
@@ -1107,7 +1104,7 @@ impl ScreenshotEditorWindow {
if self.bg_tab == BgTab::Wallpaper {
self.ensure_wallpapers(cx);
}
- self.ensure_background_image(cx);
+ self.ensure_background_image(window, cx);
if let Ok(dest) = std::env::var("CAP_GPUI_AUTO_SCREENSHOT_EXPORT")
&& !dest.trim().is_empty()
{
@@ -1126,7 +1123,6 @@ impl ScreenshotEditorWindow {
pub fn frame_arrived(
&mut self,
image: Arc<RenderImage>,
- rgba: Arc<Vec<u8>>,
size: (u32, u32),
window: &mut Window,
cx: &mut Context<Self>,
@@ -1134,7 +1130,6 @@ impl ScreenshotEditorWindow {
let next = (size.0.max(1) as f32, size.1.max(1) as f32);
self.rescale_annotations_for(next);
self.frame_size = next;
- self.frame_rgba = Some(rgba);
self.mask_frame_revision = self
.mask_frame_revision
.checked_add(1)
@@ -1271,7 +1266,7 @@ impl ScreenshotEditorWindow {
{
self.selected_annotation = None;
}
- self.ensure_background_image(cx);
+ self.ensure_background_image(window, cx);
self.publish();
self.schedule_save(window, cx);
self.refresh_mask_overlays(window, cx);
@@ -1505,9 +1500,11 @@ impl ScreenshotEditorWindow {
/// The Image tab's `h-48` preview. Decoded off-thread and cached by path,
/// so switching tabs does not re-read the file.
- fn ensure_background_image(&mut self, cx: &mut Context<Self>) {
+ fn ensure_background_image(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let BackgroundSource::Image { path: Some(path) } = &self.project.background.source else {
- self.bg_image = None;
+ if let Some((_, previous)) = self.bg_image.take() {
+ let _ = window.drop_image(previous);
+ }
return;
};
if self
@@ -1518,7 +1515,7 @@ impl ScreenshotEditorWindow {
return;
}
let path = path.clone();
- self.bg_image_task = Some(cx.spawn(async move |this, cx| {
+ self.bg_image_task = Some(cx.spawn_in(window, async move |this, cx| {
let decoded = cx
.background_executor()
.spawn({
@@ -1526,8 +1523,11 @@ impl ScreenshotEditorWindow {
async move { decode_preview_image(Path::new(&path)) }
})
.await;
- this.update(cx, |this, cx| {
- this.bg_image = decoded.map(|image| (path, image));
+ this.update_in(cx, |this, window, cx| {
+ let next = decoded.map(|image| (path, image));
+ if let Some((_, previous)) = std::mem::replace(&mut this.bg_image, next) {
+ let _ = window.drop_image(previous);
+ }
cx.notify();
})
.ok();
@@ -1550,7 +1550,7 @@ impl ScreenshotEditorWindow {
window,
cx,
);
- this.ensure_background_image(cx);
+ this.ensure_background_image(window, cx);
})
.ok();
})
@@ -1982,8 +1982,14 @@ impl ScreenshotEditorWindow {
match result {
Ok(bytes) => match destination {
ExportDestination::Clipboard => {
+ let image = cx
+ .background_executor()
+ .spawn(async move { crate::platform::prepare_clipboard_image(bytes) })
+ .await;
this.update_in(cx, |this, window, cx| {
- match crate::platform::copy_image_bytes_to_clipboard(&bytes, cx) {
+ match image
+ .and_then(|image| crate::platform::write_clipboard_image(image, cx))
+ {
Ok(()) => {
this.toast_success(
"Screenshot copied to clipboard!",
@@ -3284,7 +3290,7 @@ impl ScreenshotEditorWindow {
window,
cx,
);
- self.ensure_background_image(cx);
+ self.ensure_background_image(window, cx);
}
fn render_wallpaper_tab(&self, cx: &mut Context<Self>) -> AnyElement {
@@ -3484,7 +3490,7 @@ impl ScreenshotEditorWindow {
window,
cx,
);
- this.ensure_background_image(cx);
+ this.ensure_background_image(window, cx);
})),
)
.into_any_element()
@@ -4742,26 +4748,10 @@ fn render_input_key(project: &mut ProjectConfiguration) -> Option<Vec<u8>> {
Some(key)
}
-/// One rendered frame, in both the forms the window needs: tight RGBA (what a
-/// mask overlay resamples) and the BGRA copy gpui's atlas takes. The rows are
-/// copied once and the swap runs over a clone, so the padded GPU buffer is
-/// walked a single time.
-fn frame_buffers(frame: &RenderedFrame) -> Option<(Arc<Vec<u8>>, Arc<RenderImage>)> {
- let rgba = cap_gpui_kernels::frame::unpad_rows(
- &frame.data,
- frame.width as usize * 4,
- frame.padded_bytes_per_row as usize,
- frame.height as usize,
- )?;
- let mut bgra = rgba.clone();
- cap_gpui_kernels::frame::swap_red_blue(&mut bgra);
- let buffer = image::RgbaImage::from_raw(frame.width, frame.height, bgra)?;
- Some((
- Arc::new(rgba),
- Arc::new(RenderImage::new(smallvec::smallvec![image::Frame::new(
- buffer
- )])),
- ))
+/// Only the newest queued frame is ever shown, so older ones are dropped
+/// before the copy rather than converted and replaced.
+fn latest_frame<T>(frame: T, frames: &flume::Receiver<T>) -> T {
+ frames.drain().last().unwrap_or(frame)
}
/// Read the bundle, start the still renderer on tokio, pump frames back --
@@ -4832,13 +4822,14 @@ pub fn load_screenshot_project(
return;
}
- while let Ok((frame, revision)) = frame_rx.recv_async().await {
+ while let Ok(frame) = frame_rx.recv_async().await {
+ let (frame, revision) = latest_frame(frame, &frame_rx);
let size = (frame.width, frame.height);
- let buffers = cx
+ let image = cx
.background_executor()
- .spawn(async move { frame_buffers(&frame) })
+ .spawn(async move { crate::editor_window::frame_image(&frame) })
.await;
- let Some((rgba, image)) = buffers else {
+ let Some(image) = image else {
tracing::warn!("a rendered screenshot frame could not be converted for display");
continue;
};
@@ -4850,7 +4841,7 @@ pub fn load_screenshot_project(
);
if handle
.update(cx, |view, window, cx| {
- view.frame_arrived(image, rgba, size, window, cx)
+ view.frame_arrived(image, size, window, cx)
})
.is_err()
{
@@ -4882,6 +4873,56 @@ mod tests {
assert_eq!(CHECKER_TILE % (CHECKER_CELL * 2), 0);
}
+ #[test]
+ fn the_displayed_frame_matches_unpadding_then_swapping_a_copy() {
+ for (width, height) in [(1u32, 1u32), (3, 5), (7, 2), (33, 9), (1001, 3)] {
+ let row_bytes = width * 4;
+ for stride in [row_bytes, row_bytes + 4, row_bytes.next_multiple_of(256)] {
+ let frame = RenderedFrame {
+ data: Arc::new(
+ (0..stride as usize * height as usize)
+ .map(|index| (index * 7 + index / 251) as u8)
+ .collect(),
+ ),
+ width,
+ height,
+ padded_bytes_per_row: stride,
+ frame_number: 0,
+ target_time_ns: 0,
+ };
+ let rgba = cap_gpui_kernels::frame::unpad_rows(
+ &frame.data,
+ row_bytes as usize,
+ stride as usize,
+ height as usize,
+ )
+ .unwrap();
+ let mut bgra = rgba.clone();
+ cap_gpui_kernels::frame::swap_red_blue(&mut bgra);
+
+ let image = crate::editor_window::frame_image(&frame).unwrap();
+ assert_eq!(
+ image.size(0),
+ gpui::size(
+ gpui::DevicePixels::from(width),
+ gpui::DevicePixels::from(height)
+ )
+ );
+ assert_eq!(image.as_bytes(0).unwrap(), bgra.as_slice());
+ }
+ }
+ }
+
+ #[test]
+ fn only_the_newest_queued_frame_is_converted() {
+ let (frame_tx, frame_rx) = flume::bounded(2);
+ assert_eq!(latest_frame(1, &frame_rx), 1);
+ frame_tx.send(2).unwrap();
+ frame_tx.send(3).unwrap();
+ assert_eq!(latest_frame(1, &frame_rx), 3);
+ assert!(frame_rx.is_empty());
+ }
+
/// `PaddingPopover`'s guard (`context.tsx:30-42`).
#[test]
fn an_invisible_background_is_one_with_nothing_to_show() {
@@ -5177,7 +5218,7 @@ mod tests {
));
setup_rx.recv_async().await.unwrap().unwrap();
let (first, _) = frame_rx.recv_async().await.unwrap();
- assert!(frame_buffers(&first).is_some());
+ assert!(crate::editor_window::frame_image(&first).is_some());
let renders = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let render_ms = Arc::new(std::sync::Mutex::new(Vec::new()));
@@ -5189,10 +5230,12 @@ mod tests {
(renders.clone(), render_ms.clone(), sent_at.clone());
tokio::spawn(async move {
while let Ok((frame, revision)) = frame_rx.recv_async().await {
- let buffers = tokio::task::spawn_blocking(move || frame_buffers(&frame))
- .await
- .unwrap();
- assert!(buffers.is_some());
+ let image = tokio::task::spawn_blocking(move || {
+ crate::editor_window::frame_image(&frame)
+ })
+ .await
+ .unwrap();
+ assert!(image.is_some());
if let Some(sent) = sent_at.lock().unwrap().remove(&revision) {
render_ms
.lock()
diff --git a/apps/desktop-gpui/src/session.rs b/apps/desktop-gpui/src/session.rs
index ad48a204a..1763a0f74 100644
--- a/apps/desktop-gpui/src/session.rs
+++ b/apps/desktop-gpui/src/session.rs
@@ -1057,11 +1057,6 @@ impl RecordingSession {
}
pub fn stop(&mut self, cx: &mut Context<Self>) {
- if self.phase == Phase::Starting && self.countdown_remaining.is_some() {
- self.countdown_remaining = None;
- self.finish(cx);
- return;
- }
#[cfg(target_os = "linux")]
if matches!(self.phase, Phase::Starting | Phase::Stopping)
&& let Some(attempt) = self.instant_attempt.clone()
@@ -1072,6 +1067,10 @@ impl RecordingSession {
cx.notify();
return;
}
+ self.countdown_remaining = None;
+ self.abort_start_cue();
+ self.discard_primed(cx);
+ self.phase = Phase::Stopping;
self.stop_requested = true;
let generation = self.recording_generation;
let task =
@@ -1079,10 +1078,11 @@ impl RecordingSession {
cx.spawn(async move |this, cx| {
let result = task.await;
this.update(cx, |this, cx| {
- if this.recording_generation != generation || this.phase != Phase::Starting || this.active.is_some() { return; }
+ if this.recording_generation != generation || this.phase != Phase::Stopping || this.active.is_some() { return; }
if matches!(result, Ok(cap_recording::instant_recording::InstantQuiescence::Joined)) {
this.finish(cx);
} else {
+ this.phase = Phase::Starting;
this.stop_requested = false;
this.clean_control.uncertain = true;
this.error = Some("Capture cleanup is unconfirmed. Local files are preserved; use Ctrl+Shift+F9 to retry Stop.".into());
@@ -1093,6 +1093,11 @@ impl RecordingSession {
cx.notify();
return;
}
+ if self.phase == Phase::Starting && self.countdown_remaining.is_some() {
+ self.countdown_remaining = None;
+ self.finish(cx);
+ return;
+ }
#[cfg(target_os = "linux")]
if crate::app_windows::clean_capture_active(cx)
&& clean_capture_stop_must_wait(self.phase, self.clean_control.pending_kind())
@@ -1968,6 +1973,55 @@ fn recording_failure_is_current(phase: Phase, current: u64, completed: u64) -> b
mod countdown_tests {
use super::*;
+ #[cfg(target_os = "linux")]
+ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+ async fn countdown_stop_cancels_an_unpolled_instant_start() {
+ for countdown in [1, 0] {
+ let runtime = tokio::runtime::Handle::current();
+ gpui_platform::headless().run(move |cx| {
+ gpui_tokio::init_from_handle(cx, runtime);
+ let session = RecordingSession::init(cx);
+ let attempt = recording::InstantAttempt::new();
+ let gate = cap_recording::RecordingStartGate::new();
+ let (cue_abort, _) = futures_util::future::AbortHandle::new_pair();
+ session.update(cx, |session, cx| {
+ session.phase = Phase::Starting;
+ session.countdown_remaining = Some(countdown);
+ session.countdown_cue_abort = Some(cue_abort.clone());
+ session.start_gate = Some(gate.clone());
+ session.instant_attempt = Some(attempt.clone());
+ session.stop(cx);
+ assert_eq!(session.countdown_remaining, None);
+ });
+ assert!(cue_abort.is_aborted());
+ assert!(!gate.is_armed());
+ assert_eq!(
+ attempt.quiescence(),
+ cap_recording::instant_recording::InstantQuiescence::Joined
+ );
+ session.update(cx, |session, cx| session.stop(cx));
+ cx.spawn(async move |cx| {
+ for _ in 0..500 {
+ if session.read_with(cx, |session, _| session.phase) == Phase::Idle {
+ break;
+ }
+ cx.background_executor()
+ .timer(Duration::from_millis(10))
+ .await;
+ }
+ cx.update(|cx| {
+ cx.quit();
+ let session = session.read(cx);
+ assert_eq!(session.phase, Phase::Idle);
+ assert!(session.error.is_none());
+ assert!(session.start_gate.is_none());
+ });
+ })
+ .detach();
+ });
+ }
+ }
+
#[test]
fn countdown_ticks_until_the_final_step_before_start() {
assert_eq!(
diff --git a/apps/desktop-gpui/src/settings_pages.rs b/apps/desktop-gpui/src/settings_pages.rs
index 7ffc0113a..530a8df63 100644
--- a/apps/desktop-gpui/src/settings_pages.rs
+++ b/apps/desktop-gpui/src/settings_pages.rs
@@ -1329,6 +1329,14 @@ impl SettingsWindow {
cx.notify();
}
+ pub(crate) fn shortcut_listening(&self) -> bool {
+ self.pages.listening.is_some()
+ }
+
+ pub(crate) fn switch_back_running(&self) -> bool {
+ self.pages.switch_back.is_some()
+ }
+
/// Leave recording mode without touching the binding. Safe to call when
/// nothing is recording; every exit path (Escape, a click elsewhere,
/// another page, the window losing focus or closing) lands here.
@@ -2616,6 +2624,7 @@ impl SettingsWindow {
),
self.toggle(
"out-of-process-muxer",
+ "Out-of-process muxer",
self.settings.out_of_process_muxer,
cx,
|this, cx| {
@@ -2658,14 +2667,20 @@ impl SettingsWindow {
"Turn off to close this app and reopen the classic Tauri app in the dev harness. \
Recordings and settings are shared.",
),
- self.toggle("enable-gpui-app", checked, cx, |this, cx| {
- match this.pages.switch_back {
- Some(SwitchBack::Running(_)) => this.cancel_switch_back(cx),
- // Committed: the classic app is already being brought up.
- Some(SwitchBack::WaitingForClassic) => {}
- _ => this.start_switch_back(cx),
- }
- })
+ self.toggle(
+ "enable-gpui-app",
+ "Use native app",
+ checked,
+ cx,
+ |this, cx| {
+ match this.pages.switch_back {
+ Some(SwitchBack::Running(_)) => this.cancel_switch_back(cx),
+ // Committed: the classic app is already being brought up.
+ Some(SwitchBack::WaitingForClassic) => {}
+ _ => this.start_switch_back(cx),
+ }
+ },
+ )
.into_any_element(),
)
}
@@ -3391,6 +3406,7 @@ impl SettingsWindow {
),
self.toggle(
"diagnostic-mic",
+ "Test microphone",
self.pages.diagnostic_mic,
cx,
|this, cx| {
@@ -6988,15 +7004,19 @@ impl SettingsWindow {
(false, true) => "Disabled · Not saved",
}),
)
- .child(
- self.toggle(("rule-enabled", index), enabled, cx, move |this, cx| {
+ .child(self.toggle(
+ ("rule-enabled", index),
+ "Enabled",
+ enabled,
+ cx,
+ move |this, cx| {
if let Some(rule) = this.pages.automations.rules.get_mut(index) {
rule.enabled = !rule.enabled;
this.automations_persist();
cx.notify();
}
- }),
- ),
+ },
+ )),
)
.child(
div()
@@ -7685,6 +7705,7 @@ impl SettingsWindow {
SharedString::from(format!(
"action-copy-link-{rule}-{index}"
)),
+ "Copy link to clipboard",
copy_link,
cx,
move |this, cx| {
@@ -7717,6 +7738,7 @@ impl SettingsWindow {
SharedString::from(format!(
"action-open-browser-{rule}-{index}"
)),
+ "Open in browser",
open_in_browser,
cx,
move |this, cx| {
@@ -7771,6 +7793,7 @@ impl SettingsWindow {
.gap(px(8.))
.child(self.toggle(
SharedString::from(format!("action-shell-{rule}-{index}")),
+ "Run through shell",
use_shell,
cx,
move |this, cx| {
diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs
index 0fdb185e2..a13f277e8 100644
--- a/apps/desktop-gpui/src/settings_window.rs
+++ b/apps/desktop-gpui/src/settings_window.rs
@@ -12,7 +12,13 @@
//! file the shipping app uses, one key at a time, through
//! [`crate::store::set_store_setting`].
-use std::{cell::Cell, path::PathBuf, rc::Rc, sync::Arc, time::Duration};
+use std::{
+ cell::Cell,
+ path::{Path, PathBuf},
+ rc::Rc,
+ sync::Arc,
+ time::Duration,
+};
use gpui::{
AppContext as _, Bounds, Context, Entity, FocusHandle, FontWeight, Hsla, InteractiveElement,
@@ -370,6 +376,15 @@ struct RecordingRow {
thumbnail_stale: bool,
}
+impl RecordingRow {
+ fn pending_thumbnail(&self) -> Option<&Path> {
+ self.item
+ .thumbnail
+ .as_deref()
+ .filter(|_| self.thumbnail_stale)
+ }
+}
+
/// Everything the page owns. The Solid route keeps this in a `createQuery` plus
/// three signals; the lifetimes are the same, they just live on the window here.
struct Recordings {
@@ -384,9 +399,12 @@ struct Recordings {
search: String,
/// `visibleCount`, reset to `PAGE_SIZE` by a tab or search change.
visible_count: usize,
- /// The scan + thumbnail decode task. Dropping it cancels, so a refresh that
- /// arrives mid-scan replaces the old one rather than racing it.
+ /// The scan task. Dropping it cancels, so a refresh that arrives mid-scan
+ /// replaces the old one rather than racing it.
scan: Option<gpui::Task<()>>,
+ /// The thumbnail decode for the rows on screen, replaced whenever more of
+ /// them come into view.
+ decode: Option<gpui::Task<()>>,
/// The 2s poll, armed only while something in the list is still being
/// written (`refetchInterval`).
tick: Option<gpui::Task<()>>,
@@ -401,6 +419,7 @@ impl Recordings {
search: String::new(),
visible_count: RECORDINGS_PAGE_SIZE,
scan: None,
+ decode: None,
tick: None,
}
}
@@ -410,14 +429,26 @@ impl Recordings {
self.search.trim()
}
- /// `filteredRecordings()`: the tab, then the case-insensitive substring.
- fn filtered(&self) -> Vec<&RecordingRow> {
+ /// `visibleRecordings()` over `filteredRecordings()`: the tab, then the
+ /// case-insensitive substring.
+ fn visible_rows(&self) -> VisibleRows {
let query = self.trimmed_search().to_lowercase();
- self.items
- .iter()
- .flatten()
- .filter(|row| matches_recording_filters(&row.item, self.tab, &query))
- .collect()
+ visible_rows(
+ self.items
+ .iter()
+ .flatten()
+ .map(|row| matches_recording_filters(&row.item, self.tab, &query)),
+ !query.is_empty(),
+ self.visible_count,
+ )
+ }
+
+ fn pending_thumbnails(&self) -> Vec<(usize, PathBuf)> {
+ pending_thumbnail_decodes(
+ self.items.as_deref().unwrap_or_default(),
+ &self.visible_rows().indices,
+ RecordingRow::pending_thumbnail,
+ )
}
}
@@ -436,12 +467,22 @@ struct ScreenshotRow {
thumbnail: Option<Arc<RenderImage>>,
}
+impl ScreenshotRow {
+ fn pending_thumbnail(&self) -> Option<&Path> {
+ self.item
+ .thumbnail
+ .as_deref()
+ .filter(|_| self.thumbnail.is_none())
+ }
+}
+
struct Screenshots {
items: Option<Vec<ScreenshotRow>>,
search_input: Entity<ui::TextInputState>,
search: String,
visible_count: usize,
scan: Option<gpui::Task<()>>,
+ decode: Option<gpui::Task<()>>,
}
impl Screenshots {
@@ -452,6 +493,7 @@ impl Screenshots {
search: String::new(),
visible_count: RECORDINGS_PAGE_SIZE,
scan: None,
+ decode: None,
}
}
@@ -459,16 +501,64 @@ impl Screenshots {
self.search.trim()
}
- fn filtered(&self) -> Vec<&ScreenshotRow> {
+ fn visible_rows(&self) -> VisibleRows {
let query = self.trimmed_search().to_lowercase();
- self.items
- .iter()
- .flatten()
- .filter(|row| query.is_empty() || row.item.pretty_name.to_lowercase().contains(&query))
- .collect()
+ visible_rows(
+ self.items.iter().flatten().map(|row| {
+ query.is_empty() || row.item.pretty_name.to_lowercase().contains(&query)
+ }),
+ !query.is_empty(),
+ self.visible_count,
+ )
+ }
+
+ fn pending_thumbnails(&self) -> Vec<(usize, PathBuf)> {
+ pending_thumbnail_decodes(
+ self.items.as_deref().unwrap_or_default(),
+ &self.visible_rows().indices,
+ ScreenshotRow::pending_thumbnail,
+ )
}
}
+/// The rows a list paints, as indices into the unfiltered scan, so the list and
+/// its thumbnail decode agree on what is on screen.
+struct VisibleRows {
+ indices: Vec<usize>,
+ /// `filteredRecordings().length`, which the empty state and "Load more"
+ /// read.
+ matching: usize,
+}
+
+fn visible_rows(
+ matches: impl IntoIterator<Item = bool>,
+ has_search: bool,
+ visible_count: usize,
+) -> VisibleRows {
+ let mut indices: Vec<usize> = matches
+ .into_iter()
+ .enumerate()
+ .filter_map(|(index, matched)| matched.then_some(index))
+ .collect();
+ let matching = indices.len();
+ indices.truncate(visible_recordings_len(matching, has_search, visible_count));
+ VisibleRows { indices, matching }
+}
+
+/// The `(row, thumbnail)` decode jobs for the visible rows only. A library can
+/// hold thousands of items while the page paints twenty; rows further down are
+/// queued when "Load more", a tab, or a search brings them on screen.
+fn pending_thumbnail_decodes<R>(
+ rows: &[R],
+ visible: &[usize],
+ pending: impl Fn(&R) -> Option<&Path>,
+) -> Vec<(usize, PathBuf)> {
+ visible
+ .iter()
+ .filter_map(|&index| pending(rows.get(index)?).map(|path| (index, path.to_path_buf())))
+ .collect()
+}
+
/// `visibleRecordings()`: an active search shows every match, unpaginated.
fn visible_recordings_len(total: usize, has_search: bool, visible_count: usize) -> usize {
if has_search {
@@ -488,6 +578,13 @@ fn load_more_count(visible_count: usize, total: usize) -> usize {
(visible_count + RECORDINGS_PAGE_SIZE).min(total)
}
+fn search_may_have_changed(event: &ui::TextInputEvent) -> bool {
+ matches!(
+ event,
+ ui::TextInputEvent::Changed | ui::TextInputEvent::Cancelled
+ )
+}
+
/// `emptyMessage()`.
fn recordings_empty_message(tab: RecordingsTab, trimmed_search: &str) -> String {
let tab_label = match tab {
@@ -663,6 +760,8 @@ pub struct SettingsWindow {
_sign_in_subscriptions: [gpui::Subscription; 2],
user_profile: Option<UserProfile>,
profile_image: Option<Arc<RenderImage>>,
+ keyboard_nav: bool,
+ _keyboard_nav: gpui::Subscription,
}
impl SettingsWindow {
@@ -719,12 +818,26 @@ impl SettingsWindow {
cx.subscribe(&server_url_input, |this, input, event, cx| {
this.on_field_event(Field::ServerUrl, input, event, cx)
}),
- cx.subscribe(&recordings_search, |this, input, event, cx| {
- this.on_field_event(Field::RecordingsSearch, input, event, cx)
- }),
- cx.subscribe(&screenshots_search, |this, input, event, cx| {
- this.on_field_event(Field::ScreenshotsSearch, input, event, cx)
- }),
+ cx.subscribe_in(
+ &recordings_search,
+ window,
+ |this, input, event, window, cx| {
+ this.on_field_event(Field::RecordingsSearch, input.clone(), event, cx);
+ if search_may_have_changed(event) {
+ this.decode_visible_recordings(window, cx);
+ }
+ },
+ ),
+ cx.subscribe_in(
+ &screenshots_search,
+ window,
+ |this, input, event, window, cx| {
+ this.on_field_event(Field::ScreenshotsSearch, input.clone(), event, cx);
+ if search_may_have_changed(event) {
+ this.decode_visible_screenshots(window, cx);
+ }
+ },
+ ),
];
let sign_in = SignIn::global(cx);
@@ -749,6 +862,9 @@ impl SettingsWindow {
let pages = crate::settings_pages::PagesState::new(window, cx);
let page_scroll = ScrollHandle::new();
+ let keyboard_nav = ui::keyboard_nav::install(window, cx, |this: &Self, _| {
+ this.menu.is_some() || this.shortcut_listening() || this.switch_back_running()
+ });
Self {
theme,
@@ -784,6 +900,8 @@ impl SettingsWindow {
_sign_in_subscriptions: sign_in_subscriptions,
user_profile,
profile_image: None,
+ keyboard_nav: false,
+ _keyboard_nav: keyboard_nav,
}
}
@@ -870,22 +988,27 @@ impl SettingsWindow {
}
if self.page == Page::Recordings {
self.screenshots.scan = None;
+ self.screenshots.decode = None;
self.refresh_recordings(window, cx);
return;
}
if self.page == Page::Screenshots {
self.recordings.scan = None;
+ self.recordings.decode = None;
self.recordings.tick = None;
self.refresh_screenshots(window, cx);
return;
}
self.recordings.scan = None;
+ self.recordings.decode = None;
self.recordings.tick = None;
self.screenshots.scan = None;
+ self.screenshots.decode = None;
}
/// The `recordings` query: scan the library on the background executor,
- /// then decode the thumbnails there through [`library::spawn_decode_pool`].
+ /// then decode the visible rows' thumbnails there through
+ /// [`library::spawn_decode_pool`].
///
/// Same two-stage shape as the main window's Recents (`refresh_recents`):
/// the list lands first so the rows can paint with their grey placeholder,
@@ -905,17 +1028,39 @@ impl SettingsWindow {
.await;
tracing::info!(count = items.len(), "scanned the recordings library");
- let Ok(thumbnails) = this.update_in(cx, |this, window, cx| {
- this.set_recordings(items, window, cx)
- }) else {
- return;
- };
+ this.update_in(cx, |this, window, cx| {
+ this.set_recordings(items, window, cx);
+ let pending = this.recordings.pending_thumbnails();
+ this.decode_recording_thumbnails(pending, window, cx);
+ })
+ .ok();
+ }));
+ }
- let (_decodes, results) = library::spawn_decode_pool(
- cx.background_executor(),
- thumbnails,
- |(index, path)| library::decode_thumbnail(&path).map(|image| (index, path, image)),
- );
+ /// After "Load more", a tab, or a search: decode whatever just came on
+ /// screen. With nothing new the running batch, and the poll it arms when it
+ /// finishes, are left alone.
+ fn decode_visible_recordings(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ let pending = self.recordings.pending_thumbnails();
+ if !pending.is_empty() {
+ self.decode_recording_thumbnails(pending, window, cx);
+ }
+ }
+
+ /// Replaces the running batch rather than adding a second pool: its
+ /// unfinished rows are still pending, so `pending` re-queues the ones that
+ /// are still on screen.
+ fn decode_recording_thumbnails(
+ &mut self,
+ pending: Vec<(usize, PathBuf)>,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ self.recordings.decode = Some(cx.spawn_in(window, async move |this, cx| {
+ let (_decodes, results) =
+ library::spawn_decode_pool(cx.background_executor(), pending, |(index, path)| {
+ library::decode_thumbnail(&path).map(|image| (index, path, image))
+ });
while let Ok(first) = results.recv_async().await {
let mut batch = vec![first];
batch.extend(results.try_iter());
@@ -956,8 +1101,8 @@ impl SettingsWindow {
}));
}
- /// Install a scan result, and hand back the thumbnails that still have to
- /// be decoded.
+ /// Install a scan result. A row whose thumbnail still has to be decoded is
+ /// left `thumbnail_stale` until it is on screen.
///
/// `reconcile: "path"` on the query is doing real work here: the 2s poll
/// re-runs the whole scan, and a row that is still in the library must keep
@@ -971,7 +1116,7 @@ impl SettingsWindow {
items: Vec<RecordingItem>,
window: &mut Window,
cx: &mut Context<Self>,
- ) -> Vec<(usize, PathBuf)> {
+ ) {
let mut cached: std::collections::HashMap<_, _> = self
.recordings
.items
@@ -981,11 +1126,9 @@ impl SettingsWindow {
.map(|row| (row.item.path.clone(), row))
.collect();
- let mut pending = Vec::new();
let rows: Vec<RecordingRow> = items
.into_iter()
- .enumerate()
- .map(|(index, item)| {
+ .map(|item| {
let previous = cached.remove(&item.path);
let changed = previous.as_ref().is_none_or(|row| {
row.item.thumbnail != item.thumbnail
@@ -994,9 +1137,6 @@ impl SettingsWindow {
|| row.thumbnail.is_none()
});
let thumbnail = previous.and_then(|row| row.thumbnail);
- if changed && let Some(path) = item.thumbnail.clone() {
- pending.push((index, path));
- }
RecordingRow {
item,
thumbnail,
@@ -1017,7 +1157,6 @@ impl SettingsWindow {
// recording is being written into the library, and an inactive gpui
// window does not repaint from a background-driven model update.
window.refresh();
- pending
}
/// `refetchInterval: data.some(hasActiveRecording) ? 2000 : false` -- armed
@@ -1093,17 +1232,33 @@ impl SettingsWindow {
.await;
tracing::info!(count = items.len(), "scanned the screenshots library");
- let Ok(thumbnails) = this.update_in(cx, |this, window, cx| {
- this.set_screenshots(items, window, cx)
- }) else {
- return;
- };
+ this.update_in(cx, |this, window, cx| {
+ this.set_screenshots(items, window, cx);
+ let pending = this.screenshots.pending_thumbnails();
+ this.decode_screenshot_thumbnails(pending, window, cx);
+ })
+ .ok();
+ }));
+ }
- let (_decodes, results) = library::spawn_decode_pool(
- cx.background_executor(),
- thumbnails,
- |(index, path)| library::decode_thumbnail(&path).map(|image| (index, path, image)),
- );
+ fn decode_visible_screenshots(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ let pending = self.screenshots.pending_thumbnails();
+ if !pending.is_empty() {
+ self.decode_screenshot_thumbnails(pending, window, cx);
+ }
+ }
+
+ fn decode_screenshot_thumbnails(
+ &mut self,
+ pending: Vec<(usize, PathBuf)>,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ self.screenshots.decode = Some(cx.spawn_in(window, async move |this, cx| {
+ let (_decodes, results) =
+ library::spawn_decode_pool(cx.background_executor(), pending, |(index, path)| {
+ library::decode_thumbnail(&path).map(|image| (index, path, image))
+ });
while let Ok(first) = results.recv_async().await {
let mut batch = vec![first];
batch.extend(results.try_iter());
@@ -1141,7 +1296,7 @@ impl SettingsWindow {
items: Vec<ScreenshotItem>,
window: &mut Window,
cx: &mut Context<Self>,
- ) -> Vec<(usize, PathBuf)> {
+ ) {
let mut cached: std::collections::HashMap<PathBuf, Arc<RenderImage>> = self
.screenshots
.items
@@ -1151,17 +1306,10 @@ impl SettingsWindow {
.filter_map(|row| row.thumbnail.map(|image| (row.item.path, image)))
.collect();
- let mut pending = Vec::new();
let rows: Vec<ScreenshotRow> = items
.into_iter()
- .enumerate()
- .map(|(index, item)| {
+ .map(|item| {
let thumbnail = cached.remove(&item.path);
- if thumbnail.is_none()
- && let Some(path) = item.thumbnail.clone()
- {
- pending.push((index, path));
- }
ScreenshotRow { item, thumbnail }
})
.collect();
@@ -1173,7 +1321,6 @@ impl SettingsWindow {
self.screenshots.items = Some(rows);
cx.notify();
window.refresh();
- pending
}
fn delete_screenshot(&mut self, path: PathBuf, window: &mut Window, cx: &mut Context<Self>) {
@@ -1553,6 +1700,10 @@ fn covers_default_exclusion(entry: &WindowExclusion, default: &WindowExclusion)
impl Render for SettingsWindow {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.sync_appearance(window, cx);
+ if self.switch_back_running() {
+ ui::keyboard_nav::leave(window, cx);
+ }
+ self.keyboard_nav = ui::keyboard_nav::active(window, cx);
let theme = self.theme;
if self.page == Page::Quality
&& let Some(mode) = self.quality_scroll_request.take()
@@ -1567,6 +1718,7 @@ impl Render for SettingsWindow {
let shell = div()
.track_focus(&self.focus)
+ .child(ui::keyboard_nav::pointer_watcher())
// `(window-chrome).tsx` binds Cmd-W to `getCurrentWindow().close()`
// for every chrome window. Escape is not bound there and is not
// bound here.
@@ -1774,6 +1926,7 @@ impl SettingsWindow {
div()
.id("settings-nav")
+ .role(gpui::Role::TabList)
.flex()
.flex_col()
// `.cap-settings-nav { padding: 0 var(--sidebar-padding-x)
@@ -1788,6 +1941,10 @@ impl SettingsWindow {
let selected = page == self.page;
div()
.id(SharedString::from(page.slug()))
+ .when(self.keyboard_nav, ui::keyboard_nav::stop)
+ .role(gpui::Role::Tab)
+ .aria_label(page.label())
+ .aria_selected(selected)
.flex()
.flex_row()
.items_center()
@@ -2257,14 +2414,17 @@ impl SettingsWindow {
fn render_screenshots_list(&self, cx: &mut Context<Self>) -> impl IntoElement {
let theme = self.theme;
- let rows = self.screenshots.filtered();
- let total = rows.len();
+ let rows = self.screenshots.items.as_deref().unwrap_or_default();
+ let VisibleRows {
+ indices,
+ matching: total,
+ } = self.screenshots.visible_rows();
let has_search = !self.screenshots.trimmed_search().is_empty();
- let visible = visible_recordings_len(total, has_search, self.screenshots.visible_count);
+ let visible = indices.len();
let more = has_more_recordings(total, has_search, self.screenshots.visible_count);
let mut items = Vec::with_capacity(visible);
- for (index, row) in rows.iter().take(visible).enumerate() {
+ for (index, row) in indices.into_iter().map(|row| &rows[row]).enumerate() {
items.push(
self.render_screenshot_row(index, row, index + 1 == visible, cx)
.into_any_element(),
@@ -2303,10 +2463,11 @@ impl SettingsWindow {
"Load more",
false,
cx,
- |this, _window, cx| {
- let total = this.screenshots.filtered().len();
+ |this, window, cx| {
+ let total = this.screenshots.visible_rows().matching;
this.screenshots.visible_count =
load_more_count(this.screenshots.visible_count, total);
+ this.decode_visible_screenshots(window, cx);
cx.notify();
},
)),
@@ -2376,13 +2537,14 @@ impl SettingsWindow {
{
let png = png.clone();
move |_this, _window, cx| {
- if let Err(error) = crate::platform::copy_image_to_clipboard(&png, cx) {
- tracing::warn!("copying screenshot failed: {error}");
- cx.spawn(async move |_, _| {
+ let copied = crate::platform::copy_image_to_clipboard(png.clone(), cx);
+ cx.spawn(async move |_, _| {
+ if let Err(error) = copied.await {
+ tracing::warn!("copying screenshot failed: {error}");
crate::platform::alert_dialog("Could not copy screenshot", &error);
- })
- .detach();
- }
+ }
+ })
+ .detach();
}
},
))
@@ -2504,11 +2666,12 @@ impl SettingsWindow {
}))
// `text-xs text-gray-12`
.child(div().text_size(px(12.)).child(tab.label()))
- .on_click(cx.listener(move |this, _, _window, cx| {
+ .on_click(cx.listener(move |this, _, window, cx| {
this.recordings.tab = tab;
// `createEffect(() => { activeTab(); ...
// setVisibleCount(PAGE_SIZE) })`
this.recordings.visible_count = RECORDINGS_PAGE_SIZE;
+ this.decode_visible_recordings(window, cx);
cx.notify();
}))
}));
@@ -2546,16 +2709,19 @@ impl SettingsWindow {
/// border-gray-3`, with the "Load more" footer under it.
fn render_recordings_list(&self, cx: &mut Context<Self>) -> impl IntoElement {
let theme = self.theme;
- let rows = self.recordings.filtered();
- let total = rows.len();
+ let rows = self.recordings.items.as_deref().unwrap_or_default();
+ let VisibleRows {
+ indices,
+ matching: total,
+ } = self.recordings.visible_rows();
let has_search = !self.recordings.trimmed_search().is_empty();
- let visible = visible_recordings_len(total, has_search, self.recordings.visible_count);
+ let visible = indices.len();
let more = has_more_recordings(total, has_search, self.recordings.visible_count);
// Built up front rather than in a `.children(map(..))`: the closure
// would have to hold `cx` across every row, which it cannot.
let mut items = Vec::with_capacity(visible);
- for (index, row) in rows.iter().take(visible).enumerate() {
+ for (index, row) in indices.into_iter().map(|row| &rows[row]).enumerate() {
// `not-last:border-b` counts the *rendered* rows, so the border
// stops at the last visible one rather than the last matching one.
items.push(
@@ -2597,10 +2763,11 @@ impl SettingsWindow {
"Load more",
false,
cx,
- |this, _window, cx| {
- let total = this.recordings.filtered().len();
+ |this, window, cx| {
+ let total = this.recordings.visible_rows().matching;
this.recordings.visible_count =
load_more_count(this.recordings.visible_count, total);
+ this.decode_visible_recordings(window, cx);
cx.notify();
},
)),
@@ -3052,6 +3219,7 @@ impl SettingsWindow {
// The row is the *inverse* of the stored key.
self.toggle(
"hide-dock-icon",
+ "Always show dock icon",
!self.settings.hide_dock_icon,
cx,
|this, cx| {
@@ -3075,6 +3243,7 @@ impl SettingsWindow {
),
self.toggle(
"enable-notifications",
+ "System notifications",
self.settings.enable_notifications,
cx,
|this, cx| {
@@ -3134,6 +3303,7 @@ impl SettingsWindow {
Some("Open the link in your browser when an upload finishes."),
self.toggle(
"auto-open-links",
+ "Open share links automatically",
!self.settings.disable_auto_open_links,
cx,
|this, cx| {
@@ -3416,6 +3586,7 @@ impl SettingsWindow {
),
self.toggle(
"confirm-without-mic",
+ "Confirm before recording without a microphone",
settings.confirm_without_microphone,
cx,
|this, cx| {
@@ -3442,6 +3613,7 @@ impl SettingsWindow {
),
self.toggle(
"studio-sound-default",
+ "Studio Sound on new recordings",
settings.studio_sound_by_default,
cx,
|this, cx| {
@@ -3486,6 +3658,7 @@ impl SettingsWindow {
Some("Cap removes the local file once it has uploaded successfully."),
self.toggle(
"delete-after-upload",
+ "Delete Instant recordings after upload",
settings.delete_instant_recordings_after_upload,
cx,
|this, cx| {
@@ -3505,6 +3678,7 @@ impl SettingsWindow {
),
self.toggle(
"crash-recovery",
+ "Crash-recoverable recording",
settings.crash_recovery_recording,
cx,
|this, cx| {
@@ -3524,6 +3698,7 @@ impl SettingsWindow {
),
self.toggle(
"custom-cursor",
+ "Custom cursor capture (Studio)",
settings.custom_cursor_capture,
cx,
|this, cx| {
@@ -3541,11 +3716,18 @@ impl SettingsWindow {
"Automatically add zoom segments around mouse clicks in Studio \
recordings.",
),
- self.toggle("auto-zoom", settings.auto_zoom_on_clicks, cx, |this, cx| {
- this.settings.auto_zoom_on_clicks = !this.settings.auto_zoom_on_clicks;
- let value = this.settings.auto_zoom_on_clicks;
- this.write_bool("autoZoomOnClicks", value, cx);
- })
+ self.toggle(
+ "auto-zoom",
+ "Auto zoom on clicks",
+ settings.auto_zoom_on_clicks,
+ cx,
+ |this, cx| {
+ this.settings.auto_zoom_on_clicks =
+ !this.settings.auto_zoom_on_clicks;
+ let value = this.settings.auto_zoom_on_clicks;
+ this.write_bool("autoZoomOnClicks", value, cx);
+ },
+ )
.into_any_element(),
),
self.setting_row(
@@ -3574,6 +3756,7 @@ impl SettingsWindow {
Some("Record key presses so you can add keyboard overlays in the editor."),
self.toggle(
"capture-keyboard",
+ "Capture keyboard presses",
settings.capture_keyboard_events,
cx,
|this, cx| {
@@ -3595,6 +3778,7 @@ impl SettingsWindow {
),
self.toggle(
"notch-overlay",
+ "Draw the MacBook notch on screen recordings",
settings.macbook_notch_overlay,
cx,
|this, cx| {
@@ -4279,6 +4463,7 @@ impl SettingsWindow {
),
self.toggle(
"telemetry",
+ "Share anonymous telemetry",
self.settings.enable_telemetry,
cx,
|this, cx| {
@@ -4347,11 +4532,13 @@ impl SettingsWindow {
pub(crate) fn toggle(
&self,
id: impl Into<gpui::ElementId>,
+ label: impl Into<SharedString>,
checked: bool,
cx: &mut Context<Self>,
on_change: impl Fn(&mut Self, &mut Context<Self>) + 'static,
) -> ui::Toggle {
ui::Toggle::settings(&self.theme, id, checked)
+ .label(label)
.on_click(cx.listener(move |this, _, _window, cx| on_change(this, cx)))
}
@@ -4622,13 +4809,11 @@ const ZOOM_MAX: f32 = 4.5;
/// origin before storing it, and throws (leaving the setting alone) when it
/// does not parse.
fn origin_of(value: &str) -> Option<String> {
- let value = value.trim();
- let (scheme, rest) = value.split_once("://")?;
- if !matches!(scheme, "http" | "https") || rest.is_empty() {
+ let url = reqwest::Url::parse(value.trim()).ok()?;
+ if !matches!(url.scheme(), "http" | "https") {
return None;
}
- let authority = rest.split(['/', '?', '#']).next()?;
- (!authority.is_empty()).then(|| format!("{scheme}://{authority}"))
+ Some(url.origin().ascii_serialization())
}
/// The preview under the project-name template.
@@ -4685,6 +4870,37 @@ mod tests {
assert_eq!(origin_of(""), None);
}
+ #[test]
+ fn invalid_server_authorities_are_rejected_before_saving() {
+ for value in [
+ "https://cap.example.com:invalid",
+ "https://cap.example.com:65536",
+ "https://cap example.com",
+ "http://[::1",
+ "https://user@",
+ ] {
+ assert_eq!(origin_of(value), None, "{value}");
+ }
+ }
+
+ #[test]
+ fn server_origins_are_normalised_for_api_requests() {
+ for (value, expected) in [
+ ("https://cap.example.com/", "https://cap.example.com"),
+ (
+ "https://user:[email protected]/dashboard?x=1#top",
+ "https://cap.example.com",
+ ),
+ (" HTTPS://CAP.EXAMPLE.COM:443/ ", "https://cap.example.com"),
+ ("http://localhost:3000/path", "http://localhost:3000"),
+ ("http://[::1]:3000/path", "http://[::1]:3000"),
+ ] {
+ let origin = origin_of(value).expect(value);
+ assert_eq!(origin, expected);
+ assert!(reqwest::Url::parse(&format!("{origin}/api/desktop/feedback")).is_ok());
+ }
+ }
+
#[test]
fn account_name_matches_the_tauri_profile_fallbacks() {
let mut profile = UserProfile {
@@ -4854,6 +5070,100 @@ mod tests {
assert!(!has_more_recordings(53, true, 20));
}
+ #[test]
+ fn visible_rows_follow_the_filter_and_the_page() {
+ let all = visible_rows([true; 53], false, 20);
+ assert_eq!(all.indices, (0..20).collect::<Vec<_>>());
+ assert_eq!(all.matching, 53);
+
+ let more = visible_rows([true; 53], false, load_more_count(20, 53));
+ assert_eq!(more.indices, (0..40).collect::<Vec<_>>());
+ let last = visible_rows([true; 53], false, load_more_count(40, 53));
+ assert_eq!(last.indices, (0..53).collect::<Vec<_>>());
+
+ // A tab or a search matches a subset, and the indices still address the
+ // unfiltered rows.
+ let every_third: Vec<bool> = (0..90).map(|index| index % 3 == 0).collect();
+ let paged = visible_rows(every_third.iter().copied(), false, 20);
+ assert_eq!(paged.indices, (0..60).step_by(3).collect::<Vec<_>>());
+ assert_eq!(paged.matching, 30);
+ let searched = visible_rows(every_third.iter().copied(), true, 20);
+ assert_eq!(searched.indices, (0..90).step_by(3).collect::<Vec<_>>());
+ assert_eq!(searched.matching, 30);
+
+ let none = visible_rows([false; 5], true, 20);
+ assert!(none.indices.is_empty());
+ assert_eq!(none.matching, 0);
+ }
+
+ fn recording_row(index: usize, decoded: bool) -> RecordingRow {
+ let mut item = recording(&format!("Recording {index}"), RecordingMode::Studio);
+ item.thumbnail = Some(item.path.join("screenshots/display.jpg"));
+ RecordingRow {
+ item,
+ thumbnail: None,
+ thumbnail_stale: !decoded,
+ }
+ }
+
+ #[test]
+ fn only_visible_rows_are_queued_for_a_thumbnail_decode() {
+ let rows: Vec<RecordingRow> = (0..500).map(|index| recording_row(index, false)).collect();
+ let every_row: Vec<usize> = (0..rows.len()).collect();
+ let before = pending_thumbnail_decodes(&rows, &every_row, RecordingRow::pending_thumbnail);
+ let visible = visible_rows(rows.iter().map(|_| true), false, RECORDINGS_PAGE_SIZE);
+ let after =
+ pending_thumbnail_decodes(&rows, &visible.indices, RecordingRow::pending_thumbnail);
+ println!(
+ "decode jobs for a 500-row scan: every row {}, visible rows {}",
+ before.len(),
+ after.len()
+ );
+ assert_eq!(before.len(), 500);
+ assert_eq!(
+ after,
+ (0..20)
+ .map(|index| (index, rows[index].item.thumbnail.clone().unwrap()))
+ .collect::<Vec<_>>()
+ );
+
+ let mut rows = rows;
+ for row in &mut rows[..20] {
+ row.thumbnail_stale = false;
+ }
+ let more = visible_rows(
+ rows.iter().map(|_| true),
+ false,
+ load_more_count(RECORDINGS_PAGE_SIZE, 500),
+ );
+ let next: Vec<usize> =
+ pending_thumbnail_decodes(&rows, &more.indices, RecordingRow::pending_thumbnail)
+ .into_iter()
+ .map(|(index, _)| index)
+ .collect();
+ assert_eq!(next, (20..40).collect::<Vec<_>>());
+
+ // A search reaches past the first page in its own order.
+ let query = "recording 49";
+ let searched = visible_rows(
+ rows.iter()
+ .map(|row| matches_recording_filters(&row.item, RecordingsTab::All, query)),
+ true,
+ RECORDINGS_PAGE_SIZE,
+ );
+ let found: Vec<usize> =
+ pending_thumbnail_decodes(&rows, &searched.indices, RecordingRow::pending_thumbnail)
+ .into_iter()
+ .map(|(index, _)| index)
+ .collect();
+ assert_eq!(found, [49].into_iter().chain(490..500).collect::<Vec<_>>());
+
+ rows[30].item.thumbnail = None;
+ assert!(
+ pending_thumbnail_decodes(&rows, &[30], RecordingRow::pending_thumbnail).is_empty()
+ );
+ }
+
/// `emptyMessage()`.
#[test]
fn the_empty_message_names_the_tab_and_the_search() {
diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs
index c6c3406e0..8c0e2e7e2 100644
--- a/apps/desktop-gpui/src/store.rs
+++ b/apps/desktop-gpui/src/store.rs
@@ -14,7 +14,12 @@
//! read-modify-write on the raw JSON that touches exactly one key: see
//! [`set_store_setting`].
-use std::path::{Path, PathBuf};
+use std::{
+ collections::HashMap,
+ path::{Path, PathBuf},
+ sync::{Arc, LazyLock, Mutex, MutexGuard, PoisonError},
+ time::{Duration, SystemTime},
+};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
@@ -338,6 +343,11 @@ thread_local! {
};
}
+#[cfg(test)]
+thread_local! {
+ static TEST_STORE_FILE_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
+}
+
#[cfg(test)]
pub(crate) fn set_test_tauri_store_path(path: Option<PathBuf>) {
TEST_TAURI_STORE_PATH.with(|current| *current.borrow_mut() = path);
@@ -370,6 +380,8 @@ static STORE_UPDATE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
/// *not*: it is returned as `None` so no write path can silently replace
/// someone's settings with a fresh object.
fn read_store(path: &std::path::Path) -> Option<Map<String, Value>> {
+ #[cfg(test)]
+ TEST_STORE_FILE_READS.with(|reads| reads.set(reads.get() + 1));
let bytes = match std::fs::read(path) {
Ok(bytes) => bytes,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(Map::new()),
@@ -391,24 +403,113 @@ fn read_store(path: &std::path::Path) -> Option<Map<String, Value>> {
}
}
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+struct StoreFileStamp {
+ len: u64,
+ modified: Option<SystemTime>,
+ #[cfg(unix)]
+ inode: (u64, u64),
+ #[cfg(unix)]
+ changed: (i64, i64),
+}
+
+/// Filesystem mtimes are only as fine as the clock that stamps them (a
+/// scheduler tick on Linux, two seconds on FAT), so a rewrite that lands in
+/// the same tick as the read we cached can leave every field of the stamp
+/// unchanged. A cached parse is only trusted once the file's mtime is older
+/// than the read by more than that, the same rule git uses for racily clean
+/// index entries.
+const STORE_MTIME_SETTLE: Duration = Duration::from_secs(3);
+
+impl StoreFileStamp {
+ fn of(metadata: &std::fs::Metadata) -> Self {
+ #[cfg(unix)]
+ use std::os::unix::fs::MetadataExt;
+
+ Self {
+ len: metadata.len(),
+ modified: metadata.modified().ok(),
+ #[cfg(unix)]
+ inode: (metadata.dev(), metadata.ino()),
+ #[cfg(unix)]
+ changed: (metadata.ctime(), metadata.ctime_nsec()),
+ }
+ }
+
+ fn settled_before(&self, read_at: SystemTime) -> bool {
+ self.modified
+ .and_then(|modified| modified.checked_add(STORE_MTIME_SETTLE))
+ .is_some_and(|settled| settled < read_at)
+ }
+}
+
+struct CachedStore {
+ stamp: StoreFileStamp,
+ read_at: SystemTime,
+ store: Arc<Map<String, Value>>,
+}
+
+static STORE_CACHE: LazyLock<Mutex<HashMap<PathBuf, CachedStore>>> =
+ LazyLock::new(Default::default);
+
+fn store_cache() -> MutexGuard<'static, HashMap<PathBuf, CachedStore>> {
+ STORE_CACHE.lock().unwrap_or_else(PoisonError::into_inner)
+}
+
+/// [`read_store`] for readers, which run inside render: the export page asks
+/// for the auth section on every progress tick. The file is shared with the
+/// Tauri app, which can rewrite it at any time, so every call still stats it
+/// and re-reads when anything about the file changed; only the read and parse
+/// are skipped. Writers keep reading the disk directly.
+fn cached_store(path: &Path) -> Option<Arc<Map<String, Value>>> {
+ let read_at = SystemTime::now();
+ let stamp = match std::fs::metadata(path) {
+ Ok(metadata) => StoreFileStamp::of(&metadata),
+ Err(_) => {
+ store_cache().remove(path);
+ return read_store(path).map(Arc::new);
+ }
+ };
+
+ if let Some(cached) = store_cache().get(path)
+ && cached.stamp == stamp
+ && cached.stamp.settled_before(cached.read_at)
+ {
+ return Some(cached.store.clone());
+ }
+
+ let Some(store) = read_store(path).map(Arc::new) else {
+ store_cache().remove(path);
+ return None;
+ };
+ store_cache().insert(
+ path.to_path_buf(),
+ CachedStore {
+ stamp,
+ read_at,
+ store: store.clone(),
+ },
+ );
+ Some(store)
+}
+
/// One section of the store (`general_settings`, `recording_start_safety`,
/// ...) as raw JSON. Empty when absent or the wrong shape.
pub fn store_section(section: &str) -> Map<String, Value> {
- read_store(&tauri_store_path())
- .and_then(|mut store| store.remove(section))
- .and_then(|value| match value {
- Value::Object(map) => Some(map),
+ cached_store(&tauri_store_path())
+ .and_then(|store| match store.get(section) {
+ Some(Value::Object(map)) => Some(map.clone()),
_ => None,
})
.unwrap_or_default()
}
pub fn notification_sounds_enabled() -> bool {
- read_store(&tauri_store_path())
- .and_then(|mut store| store.remove(GENERAL_SETTINGS))
- .and_then(|settings| {
- settings
- .as_object()
+ cached_store(&tauri_store_path())
+ .and_then(|store| {
+ store
+ .get(GENERAL_SETTINGS)
+ .and_then(Value::as_object)
.map(|settings| bool_at(settings, "enableNotifications", true))
})
.unwrap_or(false)
@@ -486,7 +587,12 @@ fn update_store(mutate: impl FnOnce(&mut Map<String, Value>) -> bool) -> bool {
let Some(mut store) = read_store(&path) else {
return false;
};
- mutate(&mut store) && write_store(&path, &Value::Object(store))
+ if !mutate(&mut store) {
+ return false;
+ }
+ let written = write_store(&path, &Value::Object(store));
+ store_cache().remove(&path);
+ written
}
/// Pretty JSON, two-space indent, no trailing newline -- byte-shape identical
@@ -1206,7 +1312,7 @@ impl Default for GeneralSettings {
impl GeneralSettings {
pub fn load() -> Self {
- let store = read_store(&tauri_store_path()).unwrap_or_default();
+ let store = cached_store(&tauri_store_path()).unwrap_or_default();
let empty = Map::new();
let section = |name: &str| store.get(name).and_then(Value::as_object).unwrap_or(&empty);
Self::from_sections(
@@ -1843,7 +1949,7 @@ pub enum ExportDestination {
/// overwrite-on-next-save semantics as the Tauri app.
pub fn automations() -> AutomationsStore {
let Some(value) =
- read_store(&tauri_store_path()).and_then(|mut store| store.remove(AUTOMATIONS))
+ cached_store(&tauri_store_path()).and_then(|store| store.get(AUTOMATIONS).cloned())
else {
return AutomationsStore::default();
};
@@ -1964,6 +2070,236 @@ mod tests {
}
}
+ fn backdate(path: &Path, modified: SystemTime) {
+ std::fs::File::options()
+ .write(true)
+ .open(path)
+ .unwrap()
+ .set_modified(modified)
+ .unwrap();
+ }
+
+ fn seconds_ago(seconds: u64) -> SystemTime {
+ SystemTime::now()
+ .checked_sub(Duration::from_secs(seconds))
+ .unwrap()
+ }
+
+ fn store_file_reads() -> usize {
+ TEST_STORE_FILE_READS.with(std::cell::Cell::get)
+ }
+
+ #[test]
+ fn render_reads_parse_the_store_once_until_the_file_changes() {
+ let store = TempStore::new(
+ "cache-render-reads",
+ Some(
+ r#"{"auth":{"secret":{"token":"aaaa"},"organizations":[{"id":"1","name":"One"},{"id":"2","name":"Two"}]}}"#,
+ ),
+ );
+ backdate(&store.path, seconds_ago(60));
+ let before = store_file_reads();
+ for _ in 0..100 {
+ assert!(auth_snapshot().signed_in());
+ assert_eq!(auth_snapshot().organizations.len(), 2);
+ assert_eq!(auth_snapshot().organizations.len(), 2);
+ }
+ assert_eq!(store_file_reads() - before, 1);
+
+ std::fs::write(
+ &store.path,
+ r#"{"auth":{"secret":{"token":"bbbb"},"organizations":[{"id":"1","name":"One"},{"id":"2","name":"Two"}]}}"#,
+ )
+ .unwrap();
+ backdate(&store.path, seconds_ago(30));
+ assert_eq!(auth_snapshot().token.as_deref(), Some("bbbb"));
+ assert_eq!(auth_snapshot().token.as_deref(), Some("bbbb"));
+ assert_eq!(store_file_reads() - before, 2);
+ }
+
+ #[test]
+ fn a_same_length_rewrite_within_the_cached_mtime_tick_is_still_read() {
+ let store = TempStore::new(
+ "cache-racy-rewrite",
+ Some(r#"{"general_settings":{"hasCompletedStartup":true}}"#),
+ );
+ assert!(has_completed_startup());
+ let modified = std::fs::metadata(&store.path).unwrap().modified().unwrap();
+ std::fs::write(
+ &store.path,
+ r#"{"general_settings":{"hasCompletedStartup":null}}"#,
+ )
+ .unwrap();
+ backdate(&store.path, modified);
+ assert!(!has_completed_startup());
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn an_in_place_rewrite_that_restores_the_old_mtime_is_still_read() {
+ let store = TempStore::new(
+ "cache-restored-mtime",
+ Some(r#"{"general_settings":{"hasCompletedStartup":true}}"#),
+ );
+ let modified = seconds_ago(60);
+ backdate(&store.path, modified);
+ assert!(has_completed_startup());
+ std::thread::sleep(Duration::from_millis(50));
+ std::fs::write(
+ &store.path,
+ r#"{"general_settings":{"hasCompletedStartup":null}}"#,
+ )
+ .unwrap();
+ backdate(&store.path, modified);
+ assert!(!has_completed_startup());
+ }
+
+ #[test]
+ fn a_store_that_stops_parsing_or_disappears_is_not_served_from_the_cache() {
+ let store = TempStore::new(
+ "cache-broken",
+ Some(r#"{"general_settings":{"hasCompletedStartup":true}}"#),
+ );
+ backdate(&store.path, seconds_ago(60));
+ assert!(has_completed_startup());
+ std::fs::write(&store.path, "{ not json").unwrap();
+ backdate(&store.path, seconds_ago(30));
+ assert!(!has_completed_startup());
+ assert!(!set_has_completed_onboarding(true));
+ std::fs::remove_file(&store.path).unwrap();
+ assert!(!has_completed_startup());
+ assert!(store_section(GENERAL_SETTINGS).is_empty());
+ }
+
+ #[test]
+ fn every_store_writer_is_visible_to_the_next_cached_read() {
+ let store = TempStore::new("cache-writers", Some("{}"));
+ let settle = || {
+ backdate(&store.path, seconds_ago(60));
+ store_section(GENERAL_SETTINGS);
+ };
+
+ settle();
+ assert!(set_store_setting(
+ GENERAL_SETTINGS,
+ "hasCompletedStartup",
+ Value::Bool(true)
+ ));
+ assert!(has_completed_startup());
+ assert_eq!(GeneralSettings::load(), load_general_settings_separately());
+
+ settle();
+ assert!(set_has_completed_onboarding(true));
+ assert!(has_completed_onboarding());
+
+ settle();
+ assert!(set_store_value(
+ "presets",
+ serde_json::json!({"presets": [{"name": "Mine", "config": {}}], "default": 0})
+ ));
+ assert_eq!(preset_names(), vec!["Mine".to_string()]);
+
+ settle();
+ let mut presets = crate::presets::PresetsStore::load();
+ presets.create("Second".into(), serde_json::json!({}), true);
+ assert!(presets.save());
+ assert_eq!(crate::presets::PresetsStore::load(), presets);
+
+ settle();
+ assert!(update_store_section(RECORDING_SETTINGS, |section| {
+ section.insert("mode".into(), Value::String("studio".into()));
+ true
+ }));
+ assert_eq!(recording_mode_slug().as_deref(), Some("studio"));
+
+ settle();
+ assert!(set_auth(Some(
+ serde_json::json!({"secret": {"token": "abc"}, "plan": {"upgraded": true}})
+ )));
+ assert!(auth_snapshot().signed_in());
+ assert!(auth_snapshot().is_upgraded());
+
+ settle();
+ assert!(set_auth(None));
+ assert!(!auth_snapshot().signed_in());
+
+ settle();
+ let mut hotkeys = Map::new();
+ hotkeys.insert("startStudioRecording".into(), serde_json::json!({}));
+ assert!(set_hotkeys_raw(&hotkeys));
+ assert_eq!(hotkeys_raw(), hotkeys);
+
+ settle();
+ let rules = AutomationsStore {
+ version: 7,
+ ..AutomationsStore::default()
+ };
+ assert!(set_automations(&rules));
+ assert_eq!(automations().version, 7);
+
+ settle();
+ assert!(set_store_setting(
+ GENERAL_SETTINGS,
+ "enableNotifications",
+ Value::Bool(false)
+ ));
+ assert!(!notification_sounds_enabled());
+ }
+
+ #[test]
+ #[ignore]
+ fn benchmark_cached_store_section_reads() {
+ use std::{hint::black_box, time::Instant};
+
+ let config = crate::presets::preset_config(&cap_project::ProjectConfiguration::default());
+ let contents = serde_json::to_string_pretty(&serde_json::json!({
+ "general_settings": {"theme": "dark", "maxFps": 60, "hasCompletedStartup": true},
+ "recording_start_safety": {"confirmBeforeRecordingWithoutMicrophone": false},
+ "audio_enhancement": {"enabledByDefault": true},
+ "auth": {
+ "secret": {"api_key": "k".repeat(64)},
+ "plan": {"upgraded": true, "manual": false},
+ "organizations": [{"id": "1", "name": "One"}, {"id": "2", "name": "Two"}]
+ },
+ "presets": {"presets": [{"name": "Preset", "config": config}], "default": null},
+ "teleprompter": {"script": "A recording script. ".repeat(60)}
+ }))
+ .unwrap();
+ let store = TempStore::new("cache-benchmark", Some(&contents));
+ backdate(&store.path, seconds_ago(60));
+ let uncached = || {
+ read_store(&tauri_store_path())
+ .and_then(|mut store| store.remove("auth"))
+ .and_then(|value| match value {
+ Value::Object(map) => Some(map),
+ _ => None,
+ })
+ .unwrap_or_default()
+ };
+ assert_eq!(uncached(), store_section("auth"));
+ let measure = |read: &dyn Fn() -> Map<String, Value>| {
+ let start = Instant::now();
+ for _ in 0..1000 {
+ black_box(read());
+ }
+ start.elapsed().as_secs_f64() * 1_000_000.0 / 1000.0
+ };
+ let mut before = Vec::new();
+ let mut after = Vec::new();
+ for _ in 0..9 {
+ before.push(measure(&uncached));
+ after.push(measure(&|| store_section("auth")));
+ }
+ before.sort_by(f64::total_cmp);
+ after.sort_by(f64::total_cmp);
+ eprintln!(
+ "store_bytes={} uncached_median_us={:.3} cached_median_us={:.3} uncached_runs_us={before:?} cached_runs_us={after:?}",
+ contents.len(),
+ before[4],
+ after[4]
+ );
+ }
+
#[test]
#[ignore]
fn benchmark_general_settings_load() {
diff --git a/apps/desktop-gpui/src/target_overlay.rs b/apps/desktop-gpui/src/target_overlay.rs
index 7c0bbd63f..8314519dd 100644
--- a/apps/desktop-gpui/src/target_overlay.rs
+++ b/apps/desktop-gpui/src/target_overlay.rs
@@ -19,14 +19,15 @@
//! Metrics are transcribed from the TSX with the Tailwind class quoted next to
//! them, the same convention `main_window.rs` uses.
+use std::cell::Cell;
use std::collections::HashMap;
use std::sync::Arc;
-use std::time::Duration;
+use std::time::{Duration, Instant};
use cap_recording::sources::screen_capture::ScreenCaptureTarget;
use gpui::{
- Animation, AnimationExt as _, App, AppContext as _, Context, Entity, FontWeight, Global, Hsla,
- ImageFormat, InteractiveElement as _, IntoElement, MouseButton, MouseMoveEvent, MouseUpEvent,
+ App, AppContext as _, Context, Entity, FontWeight, Global, Hsla, ImageFormat,
+ InteractiveElement as _, IntoElement, MouseButton, MouseMoveEvent, MouseUpEvent,
ParentElement as _, Pixels, Point, Render, SharedString, StatefulInteractiveElement as _,
Styled, Window, div, img, linear_color_stop, linear_gradient, prelude::FluentBuilder as _, px,
rgb, svg,
@@ -80,6 +81,13 @@ fn overlay_display_is_active(
cursor_display.map_or(only_overlay || window_hovered, |cursor| cursor == display)
}
+/// Whether a new mic level should repaint an overlay: only when it drew the
+/// meter and the fill moves by a device pixel. The cluster's width bounds the
+/// meter's.
+fn overlay_meter_moved(drawn: Option<f32>, next: f32, scale: f32) -> bool {
+ drawn.is_some_and(|drawn| crate::ui::moves_a_device_pixel(drawn, next, CLUSTER_WIDTH * scale))
+}
+
fn overlay_viewport_size(viewport: (f32, f32), previous: (f32, f32)) -> (f32, f32) {
if viewport.0.is_finite() && viewport.1.is_finite() && viewport.0 > 0. && viewport.1 > 0. {
viewport
@@ -141,6 +149,16 @@ const COUNTDOWN_OPTIONS: &[(u32, &str)] = &[
const SELECTION_HINT_CYCLE: Duration = Duration::from_millis(2800);
+/// The hint redraws on a 30 fps timer rather than every display refresh: it
+/// is a small illustration, but each frame repaints a full-screen overlay.
+const SELECTION_HINT_FRAME: Duration = Duration::from_nanos(1_000_000_000 / 30);
+
+/// Where the hint is in its cycle, the way gpui's repeating `Animation`
+/// computes it: linear time since it first drew, wrapped to `0..1`.
+fn selection_hint_progress(elapsed: Duration) -> f32 {
+ (elapsed.as_secs_f32() / SELECTION_HINT_CYCLE.as_secs_f32()) % 1.
+}
+
fn material_ease(progress: f32) -> f32 {
let progress = progress.clamp(0., 1.);
if progress <= 0. {
@@ -368,28 +386,32 @@ impl TargetSelect {
})
.await;
- let Ok((icon_wanted, active, changed)) = this.update(cx, |this: &mut Self, cx| {
- let (display, window) = probe;
- let microphone_fill =
- crate::ui::MicrophoneLevel::fill(crate::feeds::Feeds::global(cx).read(cx));
- let changed = this.cursor_display != display
- || this.hovered_window != window
- || this.microphone_fill != microphone_fill;
- this.microphone_fill = microphone_fill;
- this.cursor_display = display;
- this.hovered_window = window;
- if changed {
- cx.notify();
- }
+ let Ok((icon_wanted, active, changed, microphone_changed)) =
+ this.update(cx, |this: &mut Self, cx| {
+ let (display, window) = probe;
+ let microphone_fill = crate::ui::MicrophoneLevel::capture_fill(cx);
+ // The mic level stays out of `changed`: only the overlays
+ // drawing the meter repaint for it, below.
+ let changed =
+ this.cursor_display != display || this.hovered_window != window;
+ let microphone_changed = this.microphone_fill != microphone_fill;
+ this.microphone_fill = microphone_fill;
+ this.cursor_display = display;
+ this.hovered_window = window;
+ if changed {
+ cx.notify();
+ }
- (
- this.active_window()
- .map(|window| window.id.clone())
- .filter(|id| !this.icons.contains_key(&id.to_string())),
- this.active_window().cloned(),
- changed,
- )
- }) else {
+ (
+ this.active_window()
+ .map(|window| window.id.clone())
+ .filter(|id| !this.icons.contains_key(&id.to_string())),
+ this.active_window().cloned(),
+ changed,
+ microphone_changed,
+ )
+ })
+ else {
return;
};
let fetched_icon = icon_wanted.is_some();
@@ -409,6 +431,8 @@ impl TargetSelect {
// that changed nothing they draw has nothing to repaint.
if changed || fetched_icon {
cx.update(app_windows::refresh_target_overlays);
+ } else if microphone_changed {
+ cx.update(app_windows::refresh_microphone_overlays);
}
cx.background_executor().timer(POLL_INTERVAL).await;
}
@@ -613,6 +637,11 @@ pub struct OverlayWindow {
/// Cached off the store so the start pill does not re-read the settings
/// file every frame; refreshed whenever the shared sign-in reports.
signed_in: bool,
+ /// The mic fill the last render drew, `None` when it drew no meter.
+ drawn_microphone_fill: Cell<Option<f32>>,
+ /// When the area hint first drew since it was last hidden.
+ selection_hint_started: Option<Instant>,
+ selection_hint_redraw: Option<gpui::Task<()>>,
_main_subscription: Option<gpui::Subscription>,
}
@@ -741,6 +770,9 @@ impl OverlayWindow {
empty_camera_state: crate::store::load().camera_window.unwrap_or_default(),
device_menu: None,
signed_in: crate::store::auth_snapshot().signed_in(),
+ drawn_microphone_fill: Cell::new(None),
+ selection_hint_started: None,
+ selection_hint_redraw: None,
_main_subscription: main_subscription,
}
}
@@ -917,6 +949,14 @@ impl OverlayWindow {
});
}
+ pub fn microphone_level_moved(&self, window: &Window, cx: &App) -> bool {
+ overlay_meter_moved(
+ self.drawn_microphone_fill.get(),
+ self.select.read(cx).microphone_fill,
+ window.scale_factor(),
+ )
+ }
+
/// True when the cursor is on this overlay's display -- `data-over` in the
/// display variant, `isActiveDisplay` in the area variant.
fn is_active_display(&self, window: &Window, cx: &App) -> bool {
@@ -946,6 +986,7 @@ impl Render for OverlayWindow {
self.sync_area_camera(cx);
}
let mode = self.select.read(cx).mode;
+ self.drawn_microphone_fill.set(None);
let root = div()
.id("overlay-root")
@@ -1212,14 +1253,13 @@ impl OverlayWindow {
.overflow_hidden()
.when(disabled, |row| row.opacity(0.7))
.when(!camera && enabled && !disabled, |row| {
+ let fill = self.select.read(cx).microphone_fill;
+ self.drawn_microphone_fill.set(Some(fill));
row.child(
- crate::ui::MicrophoneLevel::snapshot(
- self.select.read(cx).microphone_fill,
- theme.blue_9.into(),
- )
- .absolute()
- .top_0()
- .left_0(),
+ crate::ui::MicrophoneLevel::snapshot(fill, theme.blue_9.into())
+ .absolute()
+ .top_0()
+ .left_0(),
)
})
.child(
@@ -1611,10 +1651,13 @@ impl OverlayWindow {
/// region with eight handles, a size readout on top and the start cluster
/// placed against the crop.
fn render_area_variant(
- &self,
- window: &Window,
+ &mut self,
+ window: &mut Window,
cx: &mut Context<Self>,
) -> gpui::Stateful<gpui::Div> {
+ // Taken so the hint restarts whenever a render leaves it out, as a
+ // gpui animation's element state would.
+ let hint_started = self.selection_hint_started.take();
let interacting = self.drag.is_some();
// `shouldShowOverlay = isInteracting() || isActiveDisplay()`.
let visible = self.recording_area || interacting || self.is_active_display(window, cx);
@@ -1704,7 +1747,7 @@ impl OverlayWindow {
return root;
}
if crop.is_none() && !interacting {
- root = root.child(self.render_selection_hint());
+ root = root.child(self.render_selection_hint(hint_started, window, cx));
}
root = root.child(self.render_area_toolbar(crop, valid));
if crop.is_some() {
@@ -1838,13 +1881,36 @@ impl OverlayWindow {
.collect()
}
- fn render_selection_hint(&self) -> impl IntoElement {
+ fn render_selection_hint(
+ &mut self,
+ started: Option<Instant>,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) -> gpui::Div {
let cursor_path = if cfg!(target_os = "windows") {
"onboarding/cursor-windows.svg"
} else {
"onboarding/cursor-macos.svg"
};
- let animation = || Animation::new(SELECTION_HINT_CYCLE).repeat();
+ let started = started.unwrap_or_else(Instant::now);
+ self.selection_hint_started = Some(started);
+ // Reduced motion holds the first frame, as gpui's animations do.
+ let progress = if cx.reduce_motion() {
+ 0.
+ } else {
+ if self.selection_hint_redraw.is_none() {
+ self.selection_hint_redraw = Some(cx.spawn_in(window, async move |this, cx| {
+ cx.background_executor().timer(SELECTION_HINT_FRAME).await;
+ this.update_in(cx, |this, window, cx| {
+ this.selection_hint_redraw = None;
+ repaint(window, cx);
+ })
+ .ok();
+ }));
+ }
+ selection_hint_progress(started.elapsed())
+ };
+ let drag = selection_drag_progress(progress);
let selection = div()
.absolute()
.left(px(41.))
@@ -1856,55 +1922,37 @@ impl OverlayWindow {
linear_color_stop(gpui::hsla(0.55, 1., 0.72, 0.4), 0.),
linear_color_stop(gpui::hsla(0.62, 1., 0.59, 0.2), 1.),
))
- .with_animation("area-selection-hint-box", animation(), |this, progress| {
- let progress = selection_drag_progress(progress);
- this.w(px(10. + 58. * progress)).h(px(8. + 44. * progress))
- });
+ .w(px(10. + 58. * drag))
+ .h(px(8. + 44. * drag));
let cursor = img(cursor_path)
.absolute()
- .left(px(41.))
- .top(px(26.))
+ .left(px(41. + 68. * drag))
+ .top(px(26. + 52. * drag))
.w(px(18.))
- .h(px(26.))
- .with_animation(
- "area-selection-hint-cursor",
- animation(),
- |this, progress| {
- let progress = selection_drag_progress(progress);
- this.left(px(41. + 68. * progress))
- .top(px(26. + 52. * progress))
- },
- );
+ .h(px(26.));
+ let (scale, opacity) = if (0.1..0.12).contains(&progress) {
+ (
+ (progress - 0.1) / 0.02 * 0.2,
+ (progress - 0.1) / 0.02 * 0.95,
+ )
+ } else if (0.12..0.18).contains(&progress) {
+ (
+ 0.2 + (progress - 0.12) / 0.06 * 0.65,
+ 0.95 * (1. - (progress - 0.12) / 0.06),
+ )
+ } else {
+ (0., 0.)
+ };
+ let size = 20. * scale;
let click = div()
.absolute()
.rounded_full()
.border_2()
.border_color(gpui::hsla(0., 0., 1., 0.85))
- .with_animation(
- "area-selection-hint-click",
- animation(),
- |this, progress| {
- let (scale, opacity) = if (0.1..0.12).contains(&progress) {
- (
- (progress - 0.1) / 0.02 * 0.2,
- (progress - 0.1) / 0.02 * 0.95,
- )
- } else if (0.12..0.18).contains(&progress) {
- (
- 0.2 + (progress - 0.12) / 0.06 * 0.65,
- 0.95 * (1. - (progress - 0.12) / 0.06),
- )
- } else {
- (0., 0.)
- };
- let drag = selection_drag_progress(progress);
- let size = 20. * scale;
- this.left(px(41. + 68. * drag + 9. - size / 2.))
- .top(px(26. + 52. * drag + 14.3 - size / 2.))
- .size(px(size))
- .opacity(opacity)
- },
- );
+ .left(px(41. + 68. * drag + 9. - size / 2.))
+ .top(px(26. + 52. * drag + 14.3 - size / 2.))
+ .size(px(size))
+ .opacity(opacity);
div()
.absolute()
@@ -2784,6 +2832,42 @@ mod tests {
assert!(guides.is_empty());
}
+ #[test]
+ fn only_an_overlay_drawing_the_meter_repaints_for_the_level() {
+ assert!(!overlay_meter_moved(None, 0.8, 2.));
+ assert!(overlay_meter_moved(Some(0.), 0.8, 2.));
+ assert!(overlay_meter_moved(
+ Some(0.5),
+ 0.5 + 1. / (CLUSTER_WIDTH * 2.),
+ 2.
+ ));
+ assert!(!overlay_meter_moved(
+ Some(0.5),
+ 0.5 + 0.4 / (CLUSTER_WIDTH * 2.),
+ 2.
+ ));
+ assert!(!overlay_meter_moved(Some(0.), 0., 1.));
+ }
+
+ #[test]
+ fn selection_hint_progress_repeats_like_a_gpui_animation() {
+ let at = |ms| selection_hint_progress(Duration::from_millis(ms));
+ assert_eq!(at(0), 0.);
+ assert!((at(1400) - 0.5).abs() < 1e-4);
+ assert!((at(2800 + 700) - 0.25).abs() < 1e-4);
+ assert!((at(5 * 2800 + 1400) - 0.5).abs() < 1e-3);
+ assert!(at(2799) < 1.);
+ }
+
+ #[test]
+ fn selection_hint_paces_to_thirty_frames_a_second() {
+ let frames = SELECTION_HINT_CYCLE.as_secs_f32() / SELECTION_HINT_FRAME.as_secs_f32();
+ assert!((83.9..84.1).contains(&frames), "{frames}");
+ // The click ring (`0.1..0.18` of the cycle) still gets several frames.
+ let ring = 0.08 * SELECTION_HINT_CYCLE.as_secs_f32() / SELECTION_HINT_FRAME.as_secs_f32();
+ assert!(ring >= 6., "{ring}");
+ }
+
#[test]
fn selection_hint_curve_reaches_both_drag_extents() {
assert_eq!(selection_drag_progress(0.), 0.);
diff --git a/apps/desktop-gpui/src/target_thumbnails.rs b/apps/desktop-gpui/src/target_thumbnails.rs
index c18c57d33..b3019eab0 100644
--- a/apps/desktop-gpui/src/target_thumbnails.rs
+++ b/apps/desktop-gpui/src/target_thumbnails.rs
@@ -30,6 +30,9 @@
//!
//! Deliberate deviations:
//!
+//! * Windows and Linux capture at full resolution, so a source more than
+//! eight times the thumbnail is box-averaged before the Lanczos3 pass
+//! (`cap_gpui_kernels::thumbnail::fit_centered_capture`).
//! * No base64 and no PNG round-trip. The Tauri command has to hand bytes to a
//! webview; here the RGBA buffer goes straight to `RenderImage`, which skips
//! a PNG encode and a base64 encode per target per refresh.
@@ -160,9 +163,27 @@ pub enum WindowEvent {
image: Option<Arc<RenderImage>>,
/// Raw PNG bytes from `Window::app_icon()` (`thumbnails/mod.rs:113-122`).
app_icon: Option<Vec<u8>>,
+ entry: CapturedWindow,
},
}
+/// What a window thumbnail was captured from: the row's
+/// [`window_entry_signature`] and the capture time.
+#[derive(Clone, Debug)]
+pub struct CapturedWindow {
+ signature: String,
+ at: std::time::Instant,
+}
+
+impl CapturedWindow {
+ pub fn new(window: &WindowOption, at: std::time::Instant) -> Self {
+ Self {
+ signature: window_entry_signature(window),
+ at,
+ }
+ }
+}
+
/// Per-view thumbnail state.
///
/// Deliberately *not* an app-global. gpui's sprite atlas is per-window
@@ -180,6 +201,9 @@ pub enum WindowEvent {
pub struct ThumbnailCache {
displays: HashMap<String, Arc<RenderImage>>,
windows: HashMap<String, TargetThumb>,
+ /// What each window thumbnail in `windows` was captured from, so a sweep
+ /// only re-captures the windows that changed or have gone stale.
+ window_entries: HashMap<String, CapturedWindow>,
/// Signature of the list the cached display thumbnails were captured from.
display_signature: Option<String>,
window_signature: Option<String>,
@@ -221,13 +245,18 @@ impl ThumbnailCache {
id: &WindowId,
image: Option<Arc<RenderImage>>,
app_icon: Option<Arc<gpui::Image>>,
+ entry: CapturedWindow,
) -> Option<Arc<RenderImage>> {
- let slot = self.windows.entry(id.to_string()).or_default();
+ let key = id.to_string();
+ let slot = self.windows.entry(key.clone()).or_default();
// A refresh that failed to capture must not blank a thumbnail that is
// already on screen: only a real capture replaces one. Same for the
// icon, which the Tauri card keeps for the life of the row.
let replaced = match image {
- Some(image) => slot.image.replace(image),
+ Some(image) => {
+ self.window_entries.insert(key, entry);
+ slot.image.replace(image)
+ }
None => None,
};
if app_icon.is_some() {
@@ -268,9 +297,14 @@ impl ThumbnailCache {
false
}
});
+ self.window_entries.retain(|id, _| live.contains(id));
dropped
}
+ pub fn captured_windows(&self) -> HashMap<String, CapturedWindow> {
+ self.window_entries.clone()
+ }
+
pub fn display_inflight(&self) -> bool {
self.display_inflight
}
@@ -443,25 +477,44 @@ pub fn render_thumbnail_slot(
/// only ever compared against another string this function produced.
pub fn window_signature(list: &[WindowOption]) -> String {
list.iter()
- .map(|window| {
- let (x, y) = window.position.unwrap_or((0., 0.));
- let (width, height) = window.size.unwrap_or((0, 0));
- format!(
- "{}:{}:{}:{}:{}:{}:{}:{}",
- window.id,
- window.app,
- window.label,
- x,
- y,
- width,
- height,
- window.refresh_rate.unwrap_or(0.),
- )
- })
+ .map(window_entry_signature)
.collect::<Vec<_>>()
.join("|")
}
+pub fn window_entry_signature(window: &WindowOption) -> String {
+ let (x, y) = window.position.unwrap_or((0., 0.));
+ let (width, height) = window.size.unwrap_or((0, 0));
+ format!(
+ "{}:{}:{}:{}:{}:{}:{}:{}",
+ window.id,
+ window.app,
+ window.label,
+ x,
+ y,
+ width,
+ height,
+ window.refresh_rate.unwrap_or(0.),
+ )
+}
+
+/// Whether a sweep has to capture `window` again: it is new, its last capture
+/// missed, it moved, resized, or changed since, or its thumbnail is at least
+/// [`THUMBNAIL_STALE_TIME`] old. A sweep only runs when the list signature
+/// changed, so the age rule keeps the refresh that any list change (a focus
+/// switch reorders it) used to give every window, without re-capturing a
+/// window captured moments ago on every step of a drag.
+pub fn window_needs_capture(
+ captured: &HashMap<String, CapturedWindow>,
+ window: &WindowOption,
+ now: std::time::Instant,
+) -> bool {
+ captured.get(&window.id.to_string()).is_none_or(|entry| {
+ entry.signature != window_entry_signature(window)
+ || now.saturating_duration_since(entry.at) >= THUMBNAIL_STALE_TIME
+ })
+}
+
/// `createDisplaySignature` (`new-main/index.tsx:400-408`): id, name, refresh
/// rate. A display has no position to move to, so those three are the whole
/// identity as far as its thumbnail is concerned.
@@ -501,6 +554,7 @@ pub fn fitted_capture_size(source: Option<(f64, f64)>) -> (usize, usize) {
}
/// `normalize_thumbnail_dimensions` (`thumbnails/mod.rs:44-87`), verbatim.
+#[cfg(any(target_os = "macos", test))]
pub fn normalize_thumbnail_dimensions(image: &RgbaImage) -> RgbaImage {
cap_gpui_kernels::thumbnail::fit_centered(image, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT)
}
@@ -546,9 +600,13 @@ pub async fn capture_displays(events: flume::Sender<DisplayEvent>) {
/// The window twin. `collect_windows_with_thumbnails` (`thumbnails/mod.rs:107-137`)
/// also reads `Window::app_icon()` per row, which is what the card's centred
-/// icon-on-scrim overlay paints.
-pub async fn capture_windows(events: flume::Sender<WindowEvent>) {
- run_capture(move || capture_windows_inner(events)).await
+/// icon-on-scrim overlay paints. Windows whose thumbnail in `current` is still
+/// up to date are listed but not captured again.
+pub async fn capture_windows(
+ events: flume::Sender<WindowEvent>,
+ current: HashMap<String, CapturedWindow>,
+) {
+ run_capture(move || capture_windows_inner(events, current)).await
}
async fn capture_displays_inner(events: flume::Sender<DisplayEvent>) {
@@ -607,7 +665,10 @@ async fn capture_displays_inner(events: flume::Sender<DisplayEvent>) {
tracing::debug!(captured, total, "display thumbnail sweep finished");
}
-async fn capture_windows_inner(events: flume::Sender<WindowEvent>) {
+async fn capture_windows_inner(
+ events: flume::Sender<WindowEvent>,
+ current: HashMap<String, CapturedWindow>,
+) {
let targets = crate::devices::list_window_targets();
let total = targets.len();
let mut captured = 0usize;
@@ -619,6 +680,15 @@ async fn capture_windows_inner(events: flume::Sender<WindowEvent>) {
{
return;
}
+ let now = std::time::Instant::now();
+ let targets: Vec<_> = targets
+ .into_iter()
+ .filter(|(option, _)| window_needs_capture(&current, option, now))
+ .collect();
+ if targets.is_empty() {
+ tracing::debug!(total, "window thumbnails all current");
+ return;
+ }
let Some(Some(content)) = within_capture_timeout(
"shareable content",
@@ -656,6 +726,7 @@ async fn capture_windows_inner(events: flume::Sender<WindowEvent>) {
}
if events
.send(WindowEvent::Captured {
+ entry: CapturedWindow::new(&option, std::time::Instant::now()),
id: option.id,
image,
app_icon,
@@ -991,8 +1062,6 @@ mod platform {
use cap_recording::sources::screen_capture::ScreenCaptureTarget;
use image::RgbaImage;
- use super::normalize_thumbnail_dimensions;
-
#[derive(Clone)]
pub struct ShareableContent;
@@ -1027,7 +1096,7 @@ mod platform {
}
let image = match capture_screenshot(target).await {
- Ok(image) => image.into_rgba8(),
+ Ok(image) => image,
Err(error) => {
tracing::warn!(%error, "target thumbnail capture failed");
return None;
@@ -1038,7 +1107,11 @@ mod platform {
return None;
}
- Some(normalize_thumbnail_dimensions(&image))
+ Some(cap_gpui_kernels::thumbnail::fit_centered_capture(
+ image,
+ super::THUMBNAIL_WIDTH,
+ super::THUMBNAIL_HEIGHT,
+ ))
}
}
@@ -1149,6 +1222,7 @@ mod tests {
use super::*;
use scap_targets::{DisplayId, WindowId};
use std::str::FromStr;
+ use std::time::Instant;
fn solid(width: u32, height: u32, colour: [u8; 4]) -> RgbaImage {
RgbaImage::from_pixel(width, height, image::Rgba(colour))
@@ -1436,13 +1510,27 @@ mod tests {
assert!(
cache
- .insert_window(&id, Some(image.clone()), None)
+ .insert_window(
+ &id,
+ Some(image.clone()),
+ None,
+ CapturedWindow::new(&window("7", "Cap", "Main"), Instant::now())
+ )
.is_none()
);
assert!(cache.window(&id).image.is_some());
// A refresh with no capture and no icon: the slot survives untouched.
- assert!(cache.insert_window(&id, None, None).is_none());
+ assert!(
+ cache
+ .insert_window(
+ &id,
+ None,
+ None,
+ CapturedWindow::new(&window("7", "Cap", "Main"), Instant::now())
+ )
+ .is_none()
+ );
assert!(cache.window(&id).image.is_some());
// An icon arriving on its own does not disturb the thumbnail either.
@@ -1450,7 +1538,16 @@ mod tests {
gpui::ImageFormat::Png,
vec![0u8; 4],
));
- assert!(cache.insert_window(&id, None, Some(icon)).is_none());
+ assert!(
+ cache
+ .insert_window(
+ &id,
+ None,
+ Some(icon),
+ CapturedWindow::new(&window("7", "Cap", "Main"), Instant::now())
+ )
+ .is_none()
+ );
let thumb = cache.window(&id);
assert!(thumb.image.is_some());
assert!(thumb.app_icon.is_some());
@@ -1503,6 +1600,117 @@ mod tests {
assert!(cache.windows_stale(&moved));
}
+ fn insert_captured(cache: &mut ThumbnailCache, window: &WindowOption, at: Instant) {
+ let image = crate::library::rgba_to_render_image(solid(2, 2, [1, 2, 3, 255]));
+ let _ = cache.insert_window(
+ &window.id,
+ Some(image),
+ None,
+ CapturedWindow::new(window, at),
+ );
+ }
+
+ #[test]
+ fn a_sweep_recaptures_only_the_windows_that_changed() {
+ let now = Instant::now();
+ let mut cache = ThumbnailCache::default();
+ let list = [
+ window("1", "Cap", "Main"),
+ window("2", "Finder", "Downloads"),
+ window("3", "Safari", "Docs"),
+ ];
+ assert!(list.iter().all(|window| window_needs_capture(
+ &cache.captured_windows(),
+ window,
+ now
+ )));
+
+ for window in &list[..2] {
+ insert_captured(&mut cache, window, now);
+ }
+ let _ = cache.insert_window(&list[2].id, None, None, CapturedWindow::new(&list[2], now));
+ let current = cache.captured_windows();
+ assert!(!window_needs_capture(&current, &list[0], now));
+ assert!(!window_needs_capture(&current, &list[1], now));
+ assert!(window_needs_capture(&current, &list[2], now));
+
+ let mut moved = list[1].clone();
+ moved.position = Some((40., 12.));
+ let mut resized = list[0].clone();
+ resized.size = Some((1280, 720));
+ let mut renamed = list[0].clone();
+ renamed.label = "Settings".into();
+ for changed in [&moved, &resized, &renamed] {
+ assert!(window_needs_capture(&current, changed, now));
+ }
+
+ cache.set_window_signature(window_signature(&list[..2]));
+ let reordered = [list[1].clone(), list[0].clone()];
+ assert!(cache.windows_stale(&reordered));
+ assert!(
+ reordered
+ .iter()
+ .all(|window| !window_needs_capture(&current, window, now))
+ );
+
+ let dropped = cache.retain_windows(&list[1..]);
+ assert_eq!(dropped.len(), 1);
+ assert!(window_needs_capture(
+ &cache.captured_windows(),
+ &list[0],
+ now
+ ));
+ assert!(!window_needs_capture(
+ &cache.captured_windows(),
+ &list[1],
+ now
+ ));
+ }
+
+ #[test]
+ fn a_sweep_recaptures_unchanged_windows_once_their_thumbnail_is_stale() {
+ let captured_at = Instant::now();
+ let mut cache = ThumbnailCache::default();
+ let list = [
+ window("1", "Cap", "Main"),
+ window("2", "Finder", "Downloads"),
+ ];
+ insert_captured(&mut cache, &list[0], captured_at);
+ insert_captured(&mut cache, &list[1], captured_at + Duration::from_secs(4));
+ let current = cache.captured_windows();
+
+ let fresh = captured_at + THUMBNAIL_STALE_TIME.saturating_sub(Duration::from_millis(1));
+ assert!(!window_needs_capture(&current, &list[0], fresh));
+ assert!(!window_needs_capture(&current, &list[1], fresh));
+
+ let stale = captured_at + THUMBNAIL_STALE_TIME;
+ assert!(window_needs_capture(&current, &list[0], stale));
+ assert!(!window_needs_capture(&current, &list[1], stale));
+
+ let later = captured_at + Duration::from_secs(4) + THUMBNAIL_STALE_TIME;
+ assert!(window_needs_capture(&current, &list[1], later));
+
+ insert_captured(&mut cache, &list[0], stale);
+ assert!(!window_needs_capture(
+ &cache.captured_windows(),
+ &list[0],
+ stale
+ ));
+ }
+
+ #[test]
+ fn window_signature_is_its_entries_joined() {
+ let list = [window("1", "Cap", "Main"), window("2", "Cap", "Editor")];
+ assert_eq!(
+ window_signature(&list),
+ format!(
+ "{}|{}",
+ window_entry_signature(&list[0]),
+ window_entry_signature(&list[1])
+ )
+ );
+ }
+
#[test]
fn prewarm_is_a_one_shot_latch() {
let mut cache = ThumbnailCache::default();
diff --git a/apps/desktop-gpui/src/transcription.rs b/apps/desktop-gpui/src/transcription.rs
index bc7ad223b..4b66e2235 100644
--- a/apps/desktop-gpui/src/transcription.rs
+++ b/apps/desktop-gpui/src/transcription.rs
@@ -83,6 +83,32 @@ pub fn is_parakeet_model(model: &str) -> bool {
matches!(model, "best" | "best-max")
}
+/// The 25 languages parakeet-tdt-0.6b-v3 transcribes. It takes no language
+/// hint and its vocabulary has no other scripts, so any other language comes
+/// out as phonetic Latin text.
+const PARAKEET_LANGUAGES: [&str; 25] = [
+ "bg", "cs", "da", "de", "el", "en", "es", "et", "fi", "fr", "hr", "hu", "it", "lt", "lv", "mt",
+ "nl", "pl", "pt", "ro", "ru", "sk", "sl", "sv", "uk",
+];
+
+const PARAKEET_UNSUPPORTED_LANGUAGE: &str = "Parakeet cannot transcribe language";
+
+/// The model that transcribes `language`: a Parakeet model hands a language
+/// it cannot write to a downloaded Whisper model, larger first.
+fn transcription_model<'a>(
+ model: &'a str,
+ language: &str,
+ downloaded: impl Fn(&str) -> bool,
+) -> Result<&'a str, String> {
+ if !is_parakeet_model(model) || language == "auto" || PARAKEET_LANGUAGES.contains(&language) {
+ return Ok(model);
+ }
+ ["medium", "small"]
+ .into_iter()
+ .find(|whisper| downloaded(whisper))
+ .ok_or_else(|| format!("{PARAKEET_UNSUPPORTED_LANGUAGE} {language}"))
+}
+
/// Tauri's `appLocalDataDir()` for the `so.cap.desktop` identifier: on macOS
/// `~/Library/Application Support`, on Windows `%LOCALAPPDATA%` (deliberately
/// *not* the roaming dir [`store::app_data_dir`] uses), on Linux
@@ -158,6 +184,12 @@ struct Hub {
}
impl Hub {
+ fn downloading(&self, model: &str) -> bool {
+ self.download.as_ref().is_some_and(|download| {
+ download.model == model && download.state == DownloadState::Downloading
+ })
+ }
+
fn work_in_flight(&self) -> bool {
self.download
.as_ref()
@@ -1738,6 +1770,15 @@ pub fn transcribe_blocking(
model: &str,
language: &str,
) -> Result<Vec<CaptionSegment>, String> {
+ let requested = model;
+ // A download writes straight to the model's final path, so a model still
+ // downloading looks present but cannot load yet.
+ let model = transcription_model(model, language, |model| {
+ model_files_present(model) && !hub().downloading(model)
+ })?;
+ if model != requested {
+ tracing::info!("{requested} cannot transcribe {language}; using the {model} Whisper model");
+ }
let model_file = model_path(model);
if !project_path.exists() {
return Err(format!(
@@ -2108,7 +2149,6 @@ pub fn sync_caption_words_with_text(
.iter()
.enumerate()
.map(|(index, token)| {
- let source = existing.get(index);
let word_start = start + step * index as f64;
let word_end = if index == tokens.len() - 1 {
end
@@ -2117,8 +2157,8 @@ pub fn sync_caption_words_with_text(
};
CaptionWord {
text: (*token).to_string(),
- start: source.map_or(word_start as f32, |word| word.start),
- end: source.map_or(word_end as f32, |word| word.end),
+ start: word_start as f32,
+ end: word_end as f32,
}
})
.collect()
@@ -2251,6 +2291,9 @@ pub fn caption_generation_error_message(message: &str) -> String {
if message.contains("Failed to load Whisper model") {
return "Failed to load the caption model. Try downloading it again".to_string();
}
+ if message.contains(PARAKEET_UNSUPPORTED_LANGUAGE) {
+ return "The Recommended and High Accuracy models only transcribe English and 24 other European languages. Download the Small or Medium model to caption this language".to_string();
+ }
if message.contains("Parakeet transcription is not available on Intel macOS") {
return "Parakeet models are not available on Intel Macs. Use a Whisper model instead"
.to_string();
@@ -2265,6 +2308,38 @@ mod verify_tests;
mod tests {
use super::*;
+ #[test]
+ fn parakeet_keeps_the_languages_it_can_write() {
+ for language in ["auto", "en", "de", "ru", "uk", "pl"] {
+ assert_eq!(transcription_model("best", language, |_| true), Ok("best"));
+ assert_eq!(
+ transcription_model("best-max", language, |_| true),
+ Ok("best-max")
+ );
+ }
+ }
+
+ #[test]
+ fn other_languages_move_to_a_downloaded_whisper_model() {
+ for language in ["zh", "ja", "ko", "ar", "hi", "tr", "he", "fa"] {
+ assert_eq!(
+ transcription_model("best", language, |_| true),
+ Ok("medium")
+ );
+ assert_eq!(
+ transcription_model("best-max", language, |model| model == "small"),
+ Ok("small")
+ );
+ assert_eq!(
+ transcription_model("best", language, |model| model != "medium"),
+ Ok("small")
+ );
+ let error = transcription_model("best", language, |_| false).unwrap_err();
+ assert!(caption_generation_error_message(&error).contains("Small or Medium"));
+ }
+ assert_eq!(transcription_model("small", "zh", |_| false), Ok("small"));
+ }
+
#[cfg(not(all(target_os = "macos", target_arch = "x86_64")))]
#[test]
fn parakeet_chunks_cut_in_the_quiet_gap_and_cover_every_sample() {
@@ -2355,6 +2430,100 @@ mod tests {
}
}
+ #[test]
+ fn caption_text_edits_redistribute_timings_when_word_count_changes() {
+ let existing = caption(
+ "caption",
+ 1.0,
+ 4.0,
+ &[("hello", 1.0, 1.5), ("world", 1.5, 4.0)],
+ );
+
+ for text in ["hello", "hello new world"] {
+ let words = sync_caption_words_with_text(text, &existing.words, 1.0, 4.0);
+ assert_eq!(caption_text_from_words(&words), text);
+ assert_eq!(words.first().unwrap().start, 1.0);
+ assert_eq!(words.last().unwrap().end, 4.0);
+ assert!(words.iter().all(|word| word.start < word.end));
+ assert!(words.windows(2).all(|pair| pair[0].end <= pair[1].start));
+ }
+ }
+
+ #[test]
+ fn caption_spelling_edits_preserve_timings_and_empty_text_clears_words() {
+ let existing = caption(
+ "caption",
+ 1.0,
+ 4.0,
+ &[("hello", 1.0, 1.5), ("world", 2.0, 4.0)],
+ );
+ let words = sync_caption_words_with_text("Hello world!", &existing.words, 1.0, 4.0);
+ assert_eq!(caption_text_from_words(&words), "Hello world!");
+ for (word, original) in words.iter().zip(&existing.words) {
+ assert_eq!((word.start, word.end), (original.start, original.end));
+ }
+ assert!(sync_caption_words_with_text(" \n\t", &existing.words, 1.0, 4.0).is_empty());
+ }
+
+ #[test]
+ fn caption_text_edits_survive_source_write_and_reload() {
+ for text in ["hello new world", "hello"] {
+ let mut project = ProjectConfiguration {
+ timeline: Some(timeline(serde_json::json!({
+ "segments": [
+ { "recordingSegment": 1, "timescale": 2.0, "start": 1.0, "end": 6.0 }
+ ],
+ "zoomSegments": [],
+ "textSegments": [
+ { "start": 0.5, "end": 1.5, "layout": "fullscreen" }
+ ]
+ }))),
+ captions: Some(CaptionsData {
+ segments: vec![caption(
+ "caption",
+ 11.0,
+ 13.0,
+ &[("hello", 11.0, 11.5), ("world", 11.5, 13.0)],
+ )],
+ source_timed: true,
+ ..Default::default()
+ }),
+ ..Default::default()
+ };
+ let durations = [10.0, 10.0];
+ cap_project::synchronize_captions(&mut project, &durations);
+ project.timeline.as_mut().unwrap().caption_segments[0].text = text.into();
+
+ write_caption_edit_to_source(&mut project, 0, &durations);
+ cap_project::synchronize_captions(&mut project, &durations);
+ let dir = tempfile::tempdir().unwrap();
+ project.write(dir.path()).unwrap();
+ let mut loaded = ProjectConfiguration::load(dir.path()).unwrap();
+ cap_project::synchronize_captions(&mut loaded, &durations);
+
+ let source = &loaded.captions.as_ref().unwrap().segments[0];
+ assert_eq!(source.text, text);
+ assert_eq!(source.words.first().unwrap().start, 11.0);
+ assert_eq!(source.words.last().unwrap().end, 13.0);
+ assert!(
+ source
+ .words
+ .windows(2)
+ .all(|pair| pair[0].end <= pair[1].start)
+ );
+ let track = &loaded.timeline.as_ref().unwrap().caption_segments[0];
+ assert_eq!(track.text, text);
+ assert_eq!((track.start, track.end), (0.0, 2.0));
+ assert_eq!(track.words.len(), text.split_whitespace().count());
+ assert!(
+ track
+ .words
+ .windows(2)
+ .all(|pair| pair[0].end <= pair[1].start)
+ );
+ }
+ }
+
#[cfg(not(all(target_os = "macos", target_arch = "x86_64")))]
#[test]
fn idle_release_drops_cached_value_after_delay() {
@@ -2486,6 +2655,22 @@ mod tests {
assert_eq!(model_path("medium"), base.join("medium.bin"));
}
+ #[test]
+ fn only_the_model_being_downloaded_counts_as_downloading() {
+ let mut state = Hub::default();
+ assert!(!state.downloading("medium"));
+ state.download = Some(ModelDownload {
+ model: "medium".to_string(),
+ state: DownloadState::Downloading,
+ progress: 40.0,
+ message: "Downloading".to_string(),
+ });
+ assert!(state.downloading("medium"));
+ assert!(!state.downloading("small"));
+ state.download.as_mut().unwrap().state = DownloadState::Completed;
+ assert!(!state.downloading("medium"));
+ }
+
#[test]
fn downloads_generation_and_deletion_block_application_handoffs() {
let mut state = Hub::default();
diff --git a/apps/desktop-gpui/src/tray.rs b/apps/desktop-gpui/src/tray.rs
index 39c3acd04..0ad5bd029 100644
--- a/apps/desktop-gpui/src/tray.rs
+++ b/apps/desktop-gpui/src/tray.rs
@@ -287,17 +287,20 @@ pub fn cover_crop_geometry(orig_w: u32, orig_h: u32, size: u32) -> (u32, u32, u3
/// `load_thumbnail_data`, ending in a PNG rather than raw RGBA -- muda hands
/// its icon to AppKit as a PNG too (`PlatformIcon::to_nsimage`), so this is the
-/// same conversion one step earlier. A JPEG source starts from the Recents card
+/// same conversion one step earlier. An opaque source (a recording's JPEG, a
+/// display or area screenshot's RGB PNG) starts from the Recents card
/// thumbnail cache instead of the native-resolution original. That cache is
-/// itself JPEG and drops alpha, so anything else (a transparent screenshot PNG)
-/// still decodes directly.
+/// itself JPEG and drops alpha, so a source that can carry alpha (a window
+/// screenshot's rounded-corner RGBA PNG) still decodes directly.
fn load_thumbnail_png(path: &Path) -> Option<Vec<u8>> {
- use image::{GenericImageView, ImageEncoder, ImageFormat, RgbaImage, imageops::FilterType};
-
- let img = if ImageFormat::from_path(path).ok() == Some(ImageFormat::Jpeg) {
- image::DynamicImage::ImageRgba8(library::thumbnail_rgba(path)?)
- } else {
- image::load_from_memory(&std::fs::read(path).ok()?).ok()?
+ use image::{GenericImageView, ImageEncoder, RgbaImage, imageops::FilterType};
+
+ let cached = (!source_may_have_alpha(path))
+ .then(|| library::thumbnail_rgba(path))
+ .flatten();
+ let img = match cached {
+ Some(rgba) => image::DynamicImage::ImageRgba8(rgba),
+ None => image::load_from_memory(&std::fs::read(path).ok()?).ok()?,
};
let (orig_w, orig_h) = img.dimensions();
if orig_w == 0 || orig_h == 0 {
@@ -325,6 +328,24 @@ fn load_thumbnail_png(path: &Path) -> Option<Vec<u8>> {
Some(png)
}
+/// Reads only the signature and, for a PNG, the header chunks: the color type
+/// says whether the pixels can carry alpha without decoding any of them.
+fn source_may_have_alpha(path: &Path) -> bool {
+ use image::{ImageDecoder, ImageFormat, ImageReader};
+
+ let Ok(reader) = ImageReader::open(path).and_then(ImageReader::with_guessed_format) else {
+ return true;
+ };
+ match reader.format() {
+ Some(ImageFormat::Jpeg) => false,
+ Some(ImageFormat::Png) => reader
+ .into_decoder()
+ .ok()
+ .is_none_or(|decoder| decoder.color_type().has_alpha()),
+ _ => true,
+ }
+}
+
/// `load_all_previous_items` + the background thumbnail pass, in one call.
///
/// Reuses the library scan the main window's Recents runs on, so the two lists
@@ -904,6 +925,7 @@ mod mac {
actions: Vec<TrayItem>,
mode: Mode,
previous: Vec<PreviousItem>,
+ previous_generation: u64,
recording: bool,
}
@@ -967,41 +989,52 @@ mod mac {
}
fn create_status_item() -> Option<Tray> {
- let mtm = MainThreadMarker::new()?;
- let status_item =
- NSStatusBar::systemStatusBar().statusItemWithLength(NS_VARIABLE_STATUS_ITEM_LENGTH);
- let target: Retained<TrayTarget> = unsafe { msg_send![TrayTarget::alloc(mtm), init] };
-
- let mode = Mode::from_store();
- let tray = Tray {
- status_item,
- target,
- menu: None,
- actions: Vec::new(),
- mode,
- previous: Vec::new(),
- recording: false,
- };
- set_button_image(&tray, mode_icon(mode));
- let button = tray.status_item.button(mtm);
- let frame = button.as_ref().map_or_else(
- || {
+ unsafe {
+ let status_bar: *mut AnyObject = msg_send![class!(NSStatusBar), systemStatusBar];
+ if status_bar.is_null() {
+ return None;
+ }
+ let raw: *mut AnyObject =
+ msg_send![status_bar, statusItemWithLength: NS_VARIABLE_STATUS_ITEM_LENGTH];
+ // `statusItemWithLength:` hands back an autoreleased object owned
+ // by the status bar; retain it for as long as we hold it, and never
+ // release it -- removing a status item is what makes it disappear
+ // from the menu bar, and this one lives for the process.
+ let status_item = Id::retain(raw)?;
+ let target: Id<TrayTarget> = msg_send_id![TrayTarget::alloc(), init];
+
+ let mode = Mode::from_store();
+ let tray = Tray {
+ status_item,
+ target,
+ menu: None,
+ actions: Vec::new(),
+ mode,
+ previous: Vec::new(),
+ previous_generation: 0,
+ recording: false,
+ };
+ set_button_image(&tray, mode_icon(mode));
+ let button: *mut AnyObject = msg_send![&*tray.status_item, button];
+ let visible: bool = msg_send![&*tray.status_item, isVisible];
+ let frame: objc2_foundation::NSRect = if button.is_null() {
objc2_foundation::NSRect::new(
- objc2_foundation::NSPoint::new(0.0, 0.0),
- objc2_foundation::NSSize::new(0.0, 0.0),
+ objc2_foundation::NSPoint::new(0., 0.),
+ objc2_foundation::NSSize::new(0., 0.),
)
- },
- |button| button.frame(),
- );
- tracing::info!(
- has_button = button.is_some(),
- visible = tray.status_item.isVisible(),
- width = frame.size.width,
- height = frame.size.height,
- ?mode,
- "status item created"
- );
- Some(tray)
+ } else {
+ msg_send![button, frame]
+ };
+ tracing::info!(
+ has_button = !button.is_null(),
+ visible,
+ width = frame.size.width,
+ height = frame.size.height,
+ ?mode,
+ "status item created"
+ );
+ Some(tray)
+ }
}
/// `set_icon_for_ns_status_item_button`: an 18pt-tall template image on the
@@ -1301,13 +1334,17 @@ mod mac {
if !cx.has_global::<Tray>() {
return;
}
+ let tray = cx.global_mut::<Tray>();
+ tray.previous_generation = tray.previous_generation.wrapping_add(1);
+ let generation = tray.previous_generation;
cx.spawn(async move |cx| {
let previous = cx
.background_executor()
.spawn(async { scan_previous(true) })
.await;
cx.update(|cx| {
- if !cx.has_global::<Tray>() {
+ if !cx.has_global::<Tray>() || cx.global::<Tray>().previous_generation != generation
+ {
return;
}
cx.global_mut::<Tray>().previous = previous;
@@ -1520,6 +1557,8 @@ mod tests {
let icon = image::load_from_memory(&load_thumbnail_png(&png).unwrap())
.unwrap()
.into_rgba8();
+ assert_eq!(icon.dimensions(), (THUMBNAIL_SIZE, THUMBNAIL_SIZE));
+ assert!(icon.get_pixel(0, 0)[3] < 8, "{scan} scan lost the corner");
assert!(
icon.get_pixel(2, 16)[3] < 8,
"{scan} scan lost the transparent half"
@@ -1529,6 +1568,50 @@ mod tests {
"{scan} scan lost the opaque half"
);
}
+ let cache = library::thumbnail_cache_file(&png).unwrap();
+ assert!(
+ !cache.exists(),
+ "a transparent PNG never goes through the alpha-dropping cache"
+ );
+ std::fs::remove_dir_all(&root).ok();
+ }
+
+ #[test]
+ fn previous_thumbnails_reuse_the_cache_for_an_opaque_png() {
+ let root = temp_root("opaque-png");
+ std::fs::create_dir_all(&root).unwrap();
+ let png = root.join("screenshot.png");
+ image::RgbImage::from_pixel(1280, 720, image::Rgb([20, 120, 220]))
+ .save_with_format(&png, image::ImageFormat::Png)
+ .unwrap();
+ let cache = library::thumbnail_cache_file(&png).unwrap();
+ std::fs::remove_file(&cache).ok();
+
+ let cold = image::load_from_memory(&load_thumbnail_png(&png).unwrap())
+ .unwrap()
+ .into_rgba8();
+ assert_eq!(cold.dimensions(), (THUMBNAIL_SIZE, THUMBNAIL_SIZE));
+ assert_eq!(cold.get_pixel(0, 0)[3], 255);
+ assert!(cache.exists(), "the first scan fills the shared cache");
+
+ let marker = image::Rgb([200, 30, 90]);
+ image::RgbImage::from_pixel(398, 224, marker)
+ .save_with_format(&cache, image::ImageFormat::Jpeg)
+ .unwrap();
+ let warm = image::load_from_memory(&load_thumbnail_png(&png).unwrap())
+ .unwrap()
+ .into_rgba8();
+ assert_eq!(warm.dimensions(), (THUMBNAIL_SIZE, THUMBNAIL_SIZE));
+ assert!(
+ warm.pixels().all(|pixel| pixel.0[..3]
+ .iter()
+ .zip(marker.0)
+ .all(|(a, b)| a.abs_diff(b) <= 3)
+ && pixel.0[3] == 255),
+ "the warm icon is built from the cached thumbnail, not the original"
+ );
+
+ std::fs::remove_file(&cache).ok();
std::fs::remove_dir_all(&root).ok();
}
diff --git a/apps/desktop-gpui/src/ui/button.rs b/apps/desktop-gpui/src/ui/button.rs
index 048cadefa..fc5741245 100644
--- a/apps/desktop-gpui/src/ui/button.rs
+++ b/apps/desktop-gpui/src/ui/button.rs
@@ -8,7 +8,7 @@ use gpui::{
prelude::FluentBuilder, px, svg,
};
-use super::menu::OpenHandler;
+use super::{keyboard_nav, menu::OpenHandler};
use crate::theme::Theme;
/// The click handler every component takes. `cx.listener(..)` produces exactly
@@ -408,7 +408,7 @@ fn radix_paint(theme: &Theme, variant: ButtonVariant) -> ButtonPaint {
}
impl RenderOnce for Button {
- fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
+ fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let Button {
id,
label,
@@ -433,6 +433,8 @@ impl RenderOnce for Button {
div()
.id(id)
.tab_index(0)
+ .tab_stop(!disabled)
+ .when(keyboard_nav::active(window, cx), keyboard_nav::ring)
.flex()
.flex_row()
.items_center()
@@ -635,7 +637,7 @@ impl IconButton {
}
impl RenderOnce for IconButton {
- fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
+ fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let IconButton {
id,
icon,
@@ -659,6 +661,8 @@ impl RenderOnce for IconButton {
.id(id)
.when(occlude, |this| this.occlude())
.tab_index(0)
+ .tab_stop(!disabled)
+ .when(keyboard_nav::active(window, cx), keyboard_nav::ring)
.flex()
.items_center()
.justify_center()
diff --git a/apps/desktop-gpui/src/ui/keyboard_nav.rs b/apps/desktop-gpui/src/ui/keyboard_nav.rs
new file mode 100644
index 000000000..48ec55292
--- /dev/null
+++ b/apps/desktop-gpui/src/ui/keyboard_nav.rs
@@ -0,0 +1,202 @@
+//! Tab / Shift-Tab focus navigation for the windows that opt in.
+//!
+//! Nothing in a window has focus until the user presses Tab, and the main
+//! window has no root focus handle, so a key binding would never match there:
+//! with no focused node only the root receives keys. An owner-scoped
+//! keystroke interceptor runs before bindings and element listeners instead.
+//!
+//! Tab stops that are not already focusable are only added while a window is
+//! in keyboard mode, which starts on Tab and ends on the next mouse press. A
+//! focusable element takes focus on click and fires its click handler on
+//! Enter/Space key-up, so adding them unconditionally would change what mouse
+//! users get.
+
+use std::collections::HashSet;
+
+use gpui::{
+ App, BoxShadow, Context, DispatchPhase, Global, InteractiveElement, IntoElement, Keystroke,
+ MouseDownEvent, Styled, Subscription, Window, WindowId, canvas, point, px, rgb,
+};
+
+use crate::{theme::Theme, ui::text_input::text_input_is_composing};
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum Direction {
+ Next,
+ Previous,
+}
+
+pub fn direction(keystroke: &Keystroke) -> Option<Direction> {
+ let modifiers = keystroke.modifiers;
+ if keystroke.key != "tab"
+ || modifiers.control
+ || modifiers.alt
+ || modifiers.platform
+ || modifiers.function
+ {
+ return None;
+ }
+ Some(if modifiers.shift {
+ Direction::Previous
+ } else {
+ Direction::Next
+ })
+}
+
+fn takes_tab(blocked: bool, prompt_active: bool, composing: bool) -> bool {
+ !(blocked || prompt_active || composing)
+}
+
+#[derive(Default)]
+struct KeyboardWindows(HashSet<WindowId>);
+
+impl Global for KeyboardWindows {}
+
+pub fn active(window: &Window, cx: &App) -> bool {
+ cx.try_global::<KeyboardWindows>()
+ .is_some_and(|windows| windows.0.contains(&window.window_handle().window_id()))
+}
+
+pub fn leave(window: &mut Window, cx: &mut App) {
+ let id = window.window_handle().window_id();
+ if cx.has_global::<KeyboardWindows>() && cx.global_mut::<KeyboardWindows>().0.remove(&id) {
+ window.refresh();
+ }
+}
+
+/// Leaves keyboard mode on any mouse press in the window. A window-level
+/// listener, because `.occlude()` hitboxes (menus, modals, the Windows header)
+/// hide the root from element mouse listeners.
+pub fn pointer_watcher() -> impl IntoElement {
+ canvas(
+ |_, _, _| (),
+ |_, (), window, _| {
+ window.on_mouse_event(|_: &MouseDownEvent, phase, window, cx| {
+ if phase == DispatchPhase::Capture {
+ leave(window, cx);
+ }
+ });
+ },
+ )
+ .absolute()
+ .inset_0()
+}
+
+/// Route Tab in `window` to focus navigation. `blocked` lets the owner keep
+/// Tab for itself, e.g. while a shortcut recorder or a modal is up.
+pub fn install<V: 'static>(
+ window: &Window,
+ cx: &mut Context<V>,
+ blocked: impl Fn(&V, &App) -> bool + 'static,
+) -> Subscription {
+ let owner = window.window_handle().window_id();
+ let view = cx.weak_entity();
+ cx.intercept_keystrokes(move |event, window, cx| {
+ if window.window_handle().window_id() != owner {
+ return;
+ }
+ let Some(direction) = direction(&event.keystroke) else {
+ return;
+ };
+ let Some(view) = view.upgrade() else {
+ return;
+ };
+ if !takes_tab(
+ blocked(view.read(cx), cx),
+ window.has_active_prompt(),
+ text_input_is_composing(window, cx),
+ ) {
+ return;
+ }
+ navigate(direction, window, cx);
+ window.prevent_default();
+ cx.stop_propagation();
+ })
+}
+
+fn navigate(direction: Direction, window: &mut Window, cx: &mut App) {
+ let entered = cx
+ .default_global::<KeyboardWindows>()
+ .0
+ .insert(window.window_handle().window_id());
+ if !entered {
+ move_focus(direction, window, cx);
+ return;
+ }
+ // The stops gated on keyboard mode only exist from the next draw on, and
+ // `focus_next` walks the last rendered frame. Next-frame callbacks run
+ // before that frame draws, so the move waits one frame more.
+ window.refresh();
+ window.on_next_frame(move |window, _| {
+ window.on_next_frame(move |window, cx| {
+ if active(window, cx) {
+ move_focus(direction, window, cx);
+ }
+ });
+ });
+}
+
+fn move_focus(direction: Direction, window: &mut Window, cx: &mut App) {
+ match direction {
+ Direction::Next => window.focus_next(cx),
+ Direction::Previous => window.focus_prev(cx),
+ }
+}
+
+pub fn ring<E: InteractiveElement>(element: E) -> E {
+ element.focus_visible(|style| {
+ style.shadow(vec![BoxShadow {
+ color: rgb(Theme::SETTINGS_ACCENT).into(),
+ offset: point(px(0.), px(0.)),
+ blur_radius: px(0.),
+ spread_radius: px(2.),
+ inset: true,
+ }])
+ })
+}
+
+pub fn stop<E: InteractiveElement>(element: E) -> E {
+ ring(element.tab_index(0))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn parsed(source: &str) -> Option<Direction> {
+ direction(&Keystroke::parse(source).unwrap())
+ }
+
+ #[test]
+ fn only_bare_tab_and_shift_tab_navigate() {
+ assert_eq!(parsed("tab"), Some(Direction::Next));
+ assert_eq!(parsed("shift-tab"), Some(Direction::Previous));
+ for source in [
+ "ctrl-tab",
+ "ctrl-shift-tab",
+ "alt-tab",
+ "cmd-tab",
+ "fn-tab",
+ "secondary-tab",
+ "enter",
+ "space",
+ "escape",
+ "t",
+ ] {
+ assert_eq!(parsed(source), None, "{source}");
+ }
+ }
+
+ #[test]
+ fn tab_is_left_alone_while_anything_else_owns_it() {
+ assert!(takes_tab(false, false, false));
+ for (blocked, prompt_active, composing) in [
+ (true, false, false),
+ (false, true, false),
+ (false, false, true),
+ (true, true, true),
+ ] {
+ assert!(!takes_tab(blocked, prompt_active, composing));
+ }
+ }
+}
diff --git a/apps/desktop-gpui/src/ui/microphone_level.rs b/apps/desktop-gpui/src/ui/microphone_level.rs
index 8bc64a3eb..2983941b2 100644
--- a/apps/desktop-gpui/src/ui/microphone_level.rs
+++ b/apps/desktop-gpui/src/ui/microphone_level.rs
@@ -1,57 +1,132 @@
use gpui::{
- Context, Entity, Hsla, IntoElement, ParentElement as _, Render, Styled as _, Window, div,
+ App, Context, Entity, Hsla, IntoElement, ParentElement as _, Render, Styled as _, Window, div,
prelude::FluentBuilder as _, px,
};
-use crate::feeds::{self, Feeds};
+use crate::feeds::{self, Feeds, MicLevel};
+const RECORDING_BAR_TRACK_WIDTH: f32 = 20.;
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum MicrophoneLevelKind {
+ CaptureButton,
+ DeviceRow,
+ RecordingBar,
+}
+
+impl MicrophoneLevelKind {
+ pub(crate) fn fill(self, db: f64) -> f32 {
+ if !db.is_finite() {
+ return 0.;
+ }
+ match self {
+ Self::CaptureButton | Self::DeviceRow => (1. - feeds::picker_level(db)) as f32,
+ Self::RecordingBar => feeds::bar_level(db) as f32,
+ }
+ }
+
+ /// An upper bound on the fill's drawn width, in device pixels. The main
+ /// window meters are never wider than the window itself.
+ fn span(self, viewport_width: f32, scale: f32) -> f32 {
+ match self {
+ Self::CaptureButton | Self::DeviceRow => viewport_width * scale,
+ Self::RecordingBar => RECORDING_BAR_TRACK_WIDTH * scale,
+ }
+ }
+
+ fn shown(self, window: &Window) -> bool {
+ match self {
+ Self::RecordingBar => true,
+ // Linux has no visibility query (`window_is_visible` is a stub),
+ // and gating on focus would freeze the meter in a visible window.
+ Self::CaptureButton | Self::DeviceRow => {
+ !cfg!(any(target_os = "macos", target_os = "windows"))
+ || crate::platform::window_is_visible(window)
+ }
+ }
+ }
+}
+
+pub(crate) fn moves_a_device_pixel(drawn: f32, next: f32, span: f32) -> bool {
+ (drawn * span).round() != (next * span).round()
+}
+
+/// A live mic level meter. It is its own view so a level change repaints
+/// only when the meter would visibly move, without the window that hosts it
+/// ever reading the level in render.
pub(crate) struct MicrophoneLevel {
+ kind: MicrophoneLevelKind,
active: bool,
+ level_db: f64,
fill: f32,
color: Hsla,
+ track: Hsla,
}
impl MicrophoneLevel {
- pub(crate) fn new(feeds: &Entity<Feeds>, window: &mut Window, cx: &mut Context<Self>) -> Self {
- cx.observe_in(feeds, window, |this, feeds, window, cx| {
- let visible = if cfg!(any(target_os = "macos", target_os = "windows")) {
- crate::platform::window_is_visible(window)
- } else {
- window.is_window_active()
- };
- if this.active && visible {
- let fill = Self::fill(feeds.read(cx));
- if this.fill != fill {
- this.fill = fill;
- cx.notify();
+ pub(crate) fn new(
+ kind: MicrophoneLevelKind,
+ level: &Entity<MicLevel>,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) -> Self {
+ cx.observe_in(level, window, |this, level, window, cx| {
+ this.level_db = level.read(cx).db();
+ if !this.active || !this.kind.shown(window) {
+ return;
+ }
+ let fill = this.kind.fill(this.level_db);
+ let span = this.kind.span(
+ f32::from(window.viewport_size().width),
+ window.scale_factor(),
+ );
+ if moves_a_device_pixel(this.fill, fill, span) {
+ this.fill = fill;
+ cx.notify();
+ if this.kind == MicrophoneLevelKind::RecordingBar {
+ window.refresh();
}
}
})
.detach();
Self {
+ kind,
active: false,
+ level_db: level.read(cx).db(),
fill: 0.,
color: gpui::transparent_black(),
+ track: gpui::transparent_black(),
}
}
- pub(crate) fn fill(feeds: &Feeds) -> f32 {
- if feeds.microphone.is_some() && feeds.mic_level_db.is_finite() {
- (1. - feeds::picker_level(feeds.mic_level_db)) as f32
+ /// The capture-button fill for the current level, for surfaces that
+ /// snapshot it outside render.
+ pub(crate) fn capture_fill(cx: &App) -> f32 {
+ let feeds = Feeds::global(cx).read(cx);
+ if feeds.microphone.is_some() {
+ MicrophoneLevelKind::CaptureButton.fill(feeds.mic_level().read(cx).db())
} else {
0.
}
}
- pub(crate) fn configure(&mut self, active: bool, color: Hsla, cx: &mut Context<Self>) {
+ pub(crate) fn configure(
+ &mut self,
+ active: bool,
+ color: Hsla,
+ track: Hsla,
+ cx: &mut Context<Self>,
+ ) {
let fill = if active {
- Self::fill(Feeds::global(cx).read(cx))
+ self.kind.fill(self.level_db)
} else {
0.
};
- if self.active != active || self.color != color || self.fill != fill {
+ if self.active != active || self.color != color || self.track != track || self.fill != fill
+ {
self.active = active;
self.color = color;
+ self.track = track;
self.fill = fill;
cx.notify();
}
@@ -74,6 +149,130 @@ impl MicrophoneLevel {
impl Render for MicrophoneLevel {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
- Self::snapshot(self.fill, self.color)
+ match self.kind {
+ MicrophoneLevelKind::CaptureButton => Self::snapshot(self.fill, self.color),
+ MicrophoneLevelKind::DeviceRow => div()
+ .mt(px(3.))
+ .h(px(3.))
+ .w_full()
+ .rounded_full()
+ .overflow_hidden()
+ .bg(self.track)
+ .child(
+ div()
+ .h_full()
+ .rounded_full()
+ .bg(self.color)
+ .w(gpui::relative(self.fill)),
+ ),
+ MicrophoneLevelKind::RecordingBar => div()
+ .absolute()
+ .bottom(px(4.))
+ .left(px(4.))
+ .right(px(4.))
+ .h(px(2.))
+ .rounded_full()
+ .overflow_hidden()
+ .bg(self.track)
+ .child(
+ div()
+ .absolute()
+ .left_0()
+ .top_0()
+ .bottom_0()
+ .w(gpui::relative(self.fill))
+ .bg(self.color),
+ ),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn px_moved(kind: MicrophoneLevelKind, from_db: f64, to_db: f64, span: f32) -> bool {
+ moves_a_device_pixel(kind.fill(from_db), kind.fill(to_db), span)
+ }
+
+ #[test]
+ fn a_level_change_of_a_device_pixel_or_more_repaints() {
+ let span = MicrophoneLevelKind::CaptureButton.span(320., 2.);
+ assert!(px_moved(
+ MicrophoneLevelKind::CaptureButton,
+ -30.,
+ -20.,
+ span
+ ));
+ assert!(px_moved(
+ MicrophoneLevelKind::CaptureButton,
+ -96.,
+ -39.,
+ span
+ ));
+ assert!(px_moved(MicrophoneLevelKind::DeviceRow, -12., -10., span));
+ assert!(moves_a_device_pixel(0.5, 0.5 + 1. / span, span));
+ }
+
+ #[test]
+ fn a_sub_pixel_level_change_does_not_repaint() {
+ let span = MicrophoneLevelKind::CaptureButton.span(320., 2.);
+ assert!(!moves_a_device_pixel(0.5, 0.5 + 0.4 / span, span));
+ assert!(!moves_a_device_pixel(0.25, 0.25, span));
+ assert!(!px_moved(
+ MicrophoneLevelKind::CaptureButton,
+ -20.,
+ -20.25,
+ 40.
+ ));
+ }
+
+ #[test]
+ fn levels_below_the_visible_range_never_repaint() {
+ let span = MicrophoneLevelKind::CaptureButton.span(4096., 3.);
+ for db in [-96., -72., -60., -55.5, -48., -40.5, -40.] {
+ assert!(!px_moved(
+ MicrophoneLevelKind::CaptureButton,
+ -96.,
+ db,
+ span
+ ));
+ assert!(!px_moved(MicrophoneLevelKind::DeviceRow, -96., db, span));
+ }
+ let span = MicrophoneLevelKind::RecordingBar.span(0., 2.);
+ for db in [-96., -80., -60.5, -60.] {
+ assert!(!px_moved(MicrophoneLevelKind::RecordingBar, -96., db, span));
+ }
+ assert!(px_moved(
+ MicrophoneLevelKind::RecordingBar,
+ -96.,
+ -58.,
+ span
+ ));
+ }
+
+ #[test]
+ fn the_recording_bar_repaints_per_device_pixel_of_its_track() {
+ let kind = MicrophoneLevelKind::RecordingBar;
+ assert!(!px_moved(kind, -30., -30.5, kind.span(0., 2.)));
+ assert!(px_moved(kind, -30., -32., kind.span(0., 2.)));
+ assert!(!px_moved(kind, -30., -31., kind.span(0., 1.)));
+ assert!(px_moved(kind, -30., -33., kind.span(0., 1.)));
+ assert_eq!(kind.fill(-96.) * kind.span(0., 2.), 0.);
+ assert_eq!(kind.fill(0.) * kind.span(0., 1.), 20.);
+ assert_eq!(kind.fill(0.) * kind.span(0., 2.), 40.);
+ }
+
+ #[test]
+ fn fills_match_the_existing_level_mappings() {
+ assert_eq!(MicrophoneLevelKind::CaptureButton.fill(-96.), 0.);
+ assert_eq!(MicrophoneLevelKind::CaptureButton.fill(0.), 1.);
+ assert_eq!(MicrophoneLevelKind::DeviceRow.fill(-40.), 0.);
+ assert_eq!(MicrophoneLevelKind::RecordingBar.fill(-60.), 0.);
+ assert_eq!(MicrophoneLevelKind::RecordingBar.fill(-30.), 0.5);
+ assert_eq!(
+ MicrophoneLevelKind::CaptureButton.fill(f64::NEG_INFINITY),
+ 0.
+ );
}
}
diff --git a/apps/desktop-gpui/src/ui/mod.rs b/apps/desktop-gpui/src/ui/mod.rs
index e69609d4b..4d3b32d94 100644
--- a/apps/desktop-gpui/src/ui/mod.rs
+++ b/apps/desktop-gpui/src/ui/mod.rs
@@ -46,6 +46,7 @@ mod color_picker;
mod editor_button;
mod field;
mod kbd;
+pub mod keyboard_nav;
mod menu;
mod microphone_level;
mod number_field;
@@ -89,7 +90,7 @@ pub use field::{Field, Subfield};
pub use kbd::{KbdChip, KbdSize, kbd_symbol};
#[allow(unused_imports)]
pub use menu::{Menu, MenuItem, MenuKey, MenuState};
-pub(crate) use microphone_level::MicrophoneLevel;
+pub(crate) use microphone_level::{MicrophoneLevel, MicrophoneLevelKind, moves_a_device_pixel};
#[allow(unused_imports)]
pub use number_field::{
NumberChange, NumberField, NumberFieldState, NumberLimits, format_number, parse_number,
diff --git a/apps/desktop-gpui/src/ui/select.rs b/apps/desktop-gpui/src/ui/select.rs
index 172d2d398..3b0498493 100644
--- a/apps/desktop-gpui/src/ui/select.rs
+++ b/apps/desktop-gpui/src/ui/select.rs
@@ -12,7 +12,7 @@ use gpui::{
div, prelude::FluentBuilder, px, svg,
};
-use super::menu::OpenHandler;
+use super::{keyboard_nav, menu::OpenHandler};
use crate::theme::Theme;
#[derive(IntoElement)]
@@ -126,7 +126,7 @@ impl Select {
}
impl RenderOnce for Select {
- fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
+ fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let Select {
id,
label,
@@ -148,8 +148,13 @@ impl RenderOnce for Select {
on_open,
} = self;
+ let interactive = !disabled && (on_click.is_some() || on_open.is_some());
div()
.id(id)
+ .when(
+ interactive && keyboard_nav::active(window, cx),
+ keyboard_nav::stop,
+ )
.flex()
.flex_row()
.items_center()
diff --git a/apps/desktop-gpui/src/ui/text_input.rs b/apps/desktop-gpui/src/ui/text_input.rs
index 88bc8eb30..cbf4628ab 100644
--- a/apps/desktop-gpui/src/ui/text_input.rs
+++ b/apps/desktop-gpui/src/ui/text_input.rs
@@ -75,6 +75,7 @@ use gpui::{
use smallvec::SmallVec;
use unicode_segmentation::UnicodeSegmentation;
+use super::keyboard_nav;
use crate::theme::Theme;
// -- Actions and key bindings -------------------------------------------------
@@ -139,8 +140,12 @@ pub const KEY_CONTEXT: &str = "TextInput";
/// matched *before* the IME composes the character, so binding `a` would mean
/// the letter `a` could never be typed.
pub fn bind_keys(cx: &mut App) {
+ cx.bind_keys(bindings());
+}
+
+pub(crate) fn bindings() -> Vec<KeyBinding> {
let ctx = Some(KEY_CONTEXT);
- cx.bind_keys([
+ let mut bindings = vec![
KeyBinding::new("backspace", Backspace, ctx),
KeyBinding::new("delete", DeleteForward, ctx),
KeyBinding::new("alt-backspace", DeleteWordLeft, ctx),
@@ -181,7 +186,36 @@ pub fn bind_keys(cx: &mut App) {
KeyBinding::new("enter", Confirm, ctx),
KeyBinding::new("escape", Cancel, ctx),
KeyBinding::new("ctrl-cmd-space", ShowCharacterPalette, ctx),
- ]);
+ ];
+ if cfg!(not(target_os = "macos")) {
+ bindings.extend(ctrl_bindings(ctx));
+ }
+ bindings
+}
+
+/// The fork parses `cmd` as the Windows/Super key off macOS, so the `cmd-*`
+/// chords above never fire there. These are the Windows and Linux spellings of
+/// the same commands.
+fn ctrl_bindings(ctx: Option<&str>) -> [KeyBinding; 17] {
+ [
+ KeyBinding::new("ctrl-a", SelectAll, ctx),
+ KeyBinding::new("ctrl-c", Copy, ctx),
+ KeyBinding::new("ctrl-x", Cut, ctx),
+ KeyBinding::new("ctrl-v", Paste, ctx),
+ KeyBinding::new("ctrl-z", Undo, ctx),
+ KeyBinding::new("ctrl-shift-z", Redo, ctx),
+ KeyBinding::new("ctrl-y", Redo, ctx),
+ KeyBinding::new("ctrl-left", MoveWordLeft, ctx),
+ KeyBinding::new("ctrl-right", MoveWordRight, ctx),
+ KeyBinding::new("ctrl-shift-left", SelectWordLeft, ctx),
+ KeyBinding::new("ctrl-shift-right", SelectWordRight, ctx),
+ KeyBinding::new("ctrl-backspace", DeleteWordLeft, ctx),
+ KeyBinding::new("ctrl-delete", DeleteWordRight, ctx),
+ KeyBinding::new("ctrl-home", MoveToStart, ctx),
+ KeyBinding::new("ctrl-end", MoveToEnd, ctx),
+ KeyBinding::new("ctrl-shift-home", SelectToStart, ctx),
+ KeyBinding::new("ctrl-shift-end", SelectToEnd, ctx),
+ ]
}
// -- The focus gate for bare printable keys -----------------------------------
@@ -213,6 +247,21 @@ pub fn text_input_has_focus(window: &Window, cx: &App) -> bool {
})
}
+/// Is a focused field in this window composing IME text? Tab belongs to the
+/// input method then.
+pub fn text_input_is_composing(window: &Window, cx: &App) -> bool {
+ let Some(live) = cx.try_global::<LiveInputs>() else {
+ return false;
+ };
+ let inputs = live.0.borrow();
+ inputs.iter().any(|weak| {
+ weak.upgrade().is_some_and(|entity| {
+ let input = entity.read(cx);
+ input.marked_range.is_some() && input.focus.is_focused(window)
+ })
+ })
+}
+
fn register_live_input(weak: WeakEntity<TextInputState>, cx: &mut App) {
if !cx.has_global::<LiveInputs>() {
cx.set_global(LiveInputs::default());
@@ -1828,7 +1877,11 @@ impl RenderOnce for TextInput {
let state = self.state.clone();
let (focus, multi_line, disabled) = {
let read = state.read(cx);
- (read.focus.clone(), read.multi_line, read.disabled)
+ (
+ read.focus.clone().tab_stop(!read.disabled),
+ read.multi_line,
+ read.disabled,
+ )
};
let focused = focus.is_focused(window);
// `leading-normal` on a `text-xs` input is 1.35 in this font stack;
@@ -1863,6 +1916,7 @@ impl RenderOnce for TextInput {
.id(self.id.clone())
.key_context(KEY_CONTEXT)
.track_focus(&focus)
+ .when(keyboard_nav::active(window, cx), keyboard_nav::ring)
.relative()
.flex()
.flex_row()
@@ -2380,6 +2434,164 @@ impl Element for TextBody {
mod tests {
use super::*;
+ // -- Key bindings ---------------------------------------------------------
+
+ fn single_keystroke(binding: &KeyBinding) -> &gpui::Keystroke {
+ assert_eq!(binding.keystrokes().len(), 1, "{binding:?}");
+ binding.keystrokes()[0].inner()
+ }
+
+ fn bound<'a>(bindings: &'a [KeyBinding], source: &str) -> Vec<&'a KeyBinding> {
+ let keystroke = gpui::Keystroke::parse(source).unwrap();
+ bindings
+ .iter()
+ .filter(|binding| single_keystroke(binding) == &keystroke)
+ .collect()
+ }
+
+ fn assert_bound(bindings: &[KeyBinding], source: &str, action: &dyn gpui::Action) {
+ let matches = bound(bindings, source);
+ assert_eq!(matches.len(), 1, "{source} should be bound exactly once");
+ assert!(
+ matches[0].action().partial_eq(action),
+ "{source} is bound to {}, expected {}",
+ matches[0].action().name(),
+ action.name()
+ );
+ }
+
+ const MAC_CHORDS: usize = 40;
+
+ #[test]
+ fn every_binding_is_a_single_keystroke_in_the_text_input_context() {
+ for binding in bindings() {
+ single_keystroke(&binding);
+ assert_eq!(
+ binding.predicate().map(|predicate| predicate.to_string()),
+ Some(KEY_CONTEXT.to_string()),
+ "{binding:?}"
+ );
+ }
+ }
+
+ #[test]
+ fn no_keystroke_is_bound_twice() {
+ let bindings = bindings();
+ let mut seen = std::collections::HashSet::new();
+ for binding in &bindings {
+ assert!(
+ seen.insert(single_keystroke(binding).unparse()),
+ "{binding:?} is bound twice"
+ );
+ }
+ }
+
+ #[test]
+ fn no_bare_printable_key_is_bound() {
+ for binding in bindings() {
+ let keystroke = single_keystroke(&binding);
+ let modified = keystroke.modifiers.control
+ || keystroke.modifiers.alt
+ || keystroke.modifiers.platform
+ || keystroke.modifiers.function;
+ assert!(
+ modified || keystroke.key.chars().count() > 1,
+ "{binding:?} would swallow a typed character"
+ );
+ }
+ }
+
+ #[test]
+ fn mac_chords_are_unchanged() {
+ let golden: &[(&str, &dyn gpui::Action)] = &[
+ ("backspace", &Backspace),
+ ("delete", &DeleteForward),
+ ("alt-backspace", &DeleteWordLeft),
+ ("alt-delete", &DeleteWordRight),
+ ("cmd-backspace", &DeleteToLineStart),
+ ("cmd-delete", &DeleteToLineEnd),
+ ("left", &MoveLeft),
+ ("right", &MoveRight),
+ ("up", &MoveUp),
+ ("down", &MoveDown),
+ ("alt-left", &MoveWordLeft),
+ ("alt-right", &MoveWordRight),
+ ("cmd-left", &MoveToLineStart),
+ ("cmd-right", &MoveToLineEnd),
+ ("home", &MoveToLineStart),
+ ("end", &MoveToLineEnd),
+ ("cmd-up", &MoveToStart),
+ ("cmd-down", &MoveToEnd),
+ ("shift-left", &SelectLeft),
+ ("shift-right", &SelectRight),
+ ("shift-up", &SelectUp),
+ ("shift-down", &SelectDown),
+ ("alt-shift-left", &SelectWordLeft),
+ ("alt-shift-right", &SelectWordRight),
+ ("cmd-shift-left", &SelectToLineStart),
+ ("cmd-shift-right", &SelectToLineEnd),
+ ("shift-home", &SelectToLineStart),
+ ("shift-end", &SelectToLineEnd),
+ ("cmd-shift-up", &SelectToStart),
+ ("cmd-shift-down", &SelectToEnd),
+ ("cmd-a", &SelectAll),
+ ("cmd-c", &Copy),
+ ("cmd-x", &Cut),
+ ("cmd-v", &Paste),
+ ("cmd-z", &Undo),
+ ("cmd-shift-z", &Redo),
+ ("cmd-y", &Redo),
+ ("enter", &Confirm),
+ ("escape", &Cancel),
+ ("ctrl-cmd-space", &ShowCharacterPalette),
+ ];
+ assert_eq!(golden.len(), MAC_CHORDS);
+ let bindings = bindings();
+ for (source, action) in golden {
+ assert_bound(&bindings, source, *action);
+ }
+ if cfg!(target_os = "macos") {
+ assert_eq!(bindings.len(), MAC_CHORDS);
+ }
+ }
+
+ #[test]
+ fn ctrl_chords_edit_text_only_off_macos() {
+ let chords: &[(&str, &dyn gpui::Action)] = &[
+ ("ctrl-a", &SelectAll),
+ ("ctrl-c", &Copy),
+ ("ctrl-x", &Cut),
+ ("ctrl-v", &Paste),
+ ("ctrl-z", &Undo),
+ ("ctrl-shift-z", &Redo),
+ ("ctrl-y", &Redo),
+ ("ctrl-left", &MoveWordLeft),
+ ("ctrl-right", &MoveWordRight),
+ ("ctrl-shift-left", &SelectWordLeft),
+ ("ctrl-shift-right", &SelectWordRight),
+ ("ctrl-backspace", &DeleteWordLeft),
+ ("ctrl-delete", &DeleteWordRight),
+ ("ctrl-home", &MoveToStart),
+ ("ctrl-end", &MoveToEnd),
+ ("ctrl-shift-home", &SelectToStart),
+ ("ctrl-shift-end", &SelectToEnd),
+ ];
+ let bindings = bindings();
+ for (source, action) in chords {
+ if cfg!(target_os = "macos") {
+ assert!(
+ bound(&bindings, source).is_empty(),
+ "{source} is bound on macOS"
+ );
+ } else {
+ assert_bound(&bindings, source, *action);
+ }
+ }
+ if !cfg!(target_os = "macos") {
+ assert_eq!(bindings.len(), MAC_CHORDS + chords.len());
+ }
+ }
+
// -- Word navigation ------------------------------------------------------
/// `alt-left` skips the run of separators first, then the word, which is
diff --git a/apps/desktop-gpui/src/ui/toggle.rs b/apps/desktop-gpui/src/ui/toggle.rs
index ff8e690b5..4a6bf7fdf 100644
--- a/apps/desktop-gpui/src/ui/toggle.rs
+++ b/apps/desktop-gpui/src/ui/toggle.rs
@@ -6,9 +6,11 @@
use gpui::{
App, ClickEvent, ElementId, Hsla, InteractiveElement, IntoElement, ParentElement, Pixels,
- RenderOnce, StatefulInteractiveElement, Styled, Window, div, prelude::FluentBuilder, px,
+ RenderOnce, SharedString, StatefulInteractiveElement, Styled, Window, div,
+ prelude::FluentBuilder, px,
};
+use super::keyboard_nav;
use crate::theme::Theme;
/// `cva` sizes. `lg` is `w-14 h-7 p-0.75` with a `size-6` thumb; no window
@@ -54,6 +56,7 @@ pub struct Toggle {
/// `bg-white` -- the thumb is white on every surface.
thumb: Hsla,
disabled: bool,
+ label: Option<SharedString>,
on_click: Option<crate::ui::button::ClickHandler>,
}
@@ -68,6 +71,7 @@ impl Toggle {
on_fill: Hsla::from(theme.editor.accent),
thumb: gpui::white(),
disabled: false,
+ label: None,
on_click: None,
}
}
@@ -115,6 +119,11 @@ impl Toggle {
self
}
+ pub fn label(mut self, label: impl Into<SharedString>) -> Self {
+ self.label = Some(label.into());
+ self
+ }
+
pub fn on_click(
mut self,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
@@ -125,7 +134,7 @@ impl Toggle {
}
impl RenderOnce for Toggle {
- fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
+ fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let Toggle {
id,
checked,
@@ -134,12 +143,24 @@ impl RenderOnce for Toggle {
on_fill,
thumb,
disabled,
+ label,
on_click,
} = self;
let (width, height, padding) = size.track();
div()
.id(id)
+ .when(
+ keyboard_nav::active(window, cx) && !disabled,
+ keyboard_nav::stop,
+ )
+ .role(gpui::Role::Switch)
+ .aria_toggled(if checked {
+ gpui::Toggled::True
+ } else {
+ gpui::Toggled::False
+ })
+ .when_some(label, |this, label| this.aria_label(label))
.w(width)
.h(height)
.p(padding)
diff --git a/apps/desktop-gpui/src/upload/multipart_tests.rs b/apps/desktop-gpui/src/upload/multipart_tests.rs
index df5be2ea8..954320f77 100644
--- a/apps/desktop-gpui/src/upload/multipart_tests.rs
+++ b/apps/desktop-gpui/src/upload/multipart_tests.rs
@@ -756,3 +756,111 @@ async fn multipart_mock_network_benchmark() {
);
}
}
+
+/// Signs this thread's store in against `server`, so `auth::authed_request`
+/// builds its URL and credentials exactly as the app does.
+struct SignedIn(PathBuf);
+
+impl SignedIn {
+ fn new(server: std::net::SocketAddr) -> Self {
+ static NEXT: AtomicUsize = AtomicUsize::new(0);
+ let dir = std::env::temp_dir().join(format!(
+ "cap-gpui-api-client-{}-{}",
+ std::process::id(),
+ NEXT.fetch_add(1, Ordering::AcqRel),
+ ));
+ std::fs::create_dir_all(&dir).unwrap();
+ let path = dir.join("store");
+ std::fs::write(
+ &path,
+ serde_json::to_vec(&json!({
+ "general_settings": { "serverUrl": format!("http://{server}") },
+ "auth": { "secret": { "api_key": "api-key" }, "user_id": "owner" },
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+ crate::store::set_test_tauri_store_path(Some(path));
+ Self(dir)
+ }
+}
+
+impl Drop for SignedIn {
+ fn drop(&mut self) {
+ crate::store::set_test_tauri_store_path(None);
+ let _ = std::fs::remove_dir_all(&self.0);
+ }
+}
+
+async fn authed_presign(part: u32) -> Duration {
+ let started = Instant::now();
+ let response = auth::authed_request(
+ reqwest::Method::POST,
+ &format!("/presign/{part}"),
+ Some(json!({ "partNumber": part })),
+ )
+ .await
+ .unwrap();
+ assert!(response.status().is_success());
+ let body = response.json::<Value>().await.unwrap();
+ assert!(
+ body["presignedUrl"]
+ .as_str()
+ .unwrap()
+ .ends_with(&format!("/part/{part}"))
+ );
+ started.elapsed()
+}
+
+#[tokio::test]
+async fn sequential_api_requests_reuse_one_connection_until_it_idles_out() {
+ let server = mock_network::start(fast_local_profile());
+ let _signed_in = SignedIn::new(server.addr);
+ let _shared_client = auth::SHARED_API_CLIENT_TEST_LOCK.lock().await;
+ for part in 1..=20 {
+ authed_presign(part).await;
+ }
+ // One more if the shared client reaches its maximum age mid-loop.
+ let reused = server.connections.load(Ordering::Acquire);
+ assert!(reused <= 2, "{reused} connections for 20 requests");
+
+ tokio::time::sleep(auth::API_POOL_IDLE_TIMEOUT + Duration::from_millis(500)).await;
+ authed_presign(21).await;
+ assert_eq!(server.connections.load(Ordering::Acquire), reused + 1);
+}
+
+/// `cargo test --bin cap-gpui api_request_mock_network_benchmark -- --ignored --nocapture`
+/// 100 sequential presigns through `auth::authed_request`, the way Instant
+/// recording sends them, with no added latency and over a 50 ms RTT link
+/// where a new connection costs a 2 RTT TCP and TLS handshake.
+#[tokio::test]
+#[ignore = "benchmark: 200 presigns through a throttled loopback server"]
+async fn api_request_mock_network_benchmark() {
+ const CALLS: u32 = 100;
+ println!("| RTT | connections | p50 | p95 | total |");
+ println!("|---|---|---|---|---|");
+ for (rtt_ms, handshake_rtts) in [(0u64, 0u32), (50, 2)] {
+ let server = mock_network::start(mock_network::Profile {
+ rtt: Duration::from_millis(rtt_ms),
+ link_bytes_per_sec: 1e9,
+ max_window: 1 << 20,
+ handshake_rtts,
+ presign_server_time: Duration::ZERO,
+ });
+ let _signed_in = SignedIn::new(server.addr);
+ let started = Instant::now();
+ let mut latencies = Vec::new();
+ for part in 1..=CALLS {
+ latencies.push(authed_presign(part).await);
+ }
+ let total = started.elapsed();
+ latencies.sort();
+ let percentile = |p: usize| latencies[(latencies.len() * p / 100).min(latencies.len() - 1)];
+ println!(
+ "| {rtt_ms} ms | {} | {:.2?} | {:.2?} | {total:.2?} |",
+ server.connections.load(Ordering::Acquire),
+ percentile(50),
+ percentile(95),
+ );
+ }
+}
diff --git a/apps/desktop-gpui/src/upload/queue.rs b/apps/desktop-gpui/src/upload/queue.rs
index 6cf371979..c32fe8c91 100644
--- a/apps/desktop-gpui/src/upload/queue.rs
+++ b/apps/desktop-gpui/src/upload/queue.rs
@@ -3,7 +3,9 @@ use cap_project::{InstantRecordingMeta, RecordingMetaInner};
use cap_recording::upload_resume::{UploadLock, UploadLockError, collect_segment_events};
use cap_recording::upload_verification::{UploadVerification, VerifiedUploadReceipt};
use futures_util::FutureExt as _;
+use std::ffi::OsString;
use std::io::Write;
+use std::time::SystemTime;
static STATE_CHANGED: tokio::sync::Notify = tokio::sync::Notify::const_new();
@@ -377,7 +379,7 @@ struct LiveBackend;
impl UploadBackend for LiveBackend {
fn check_account(&self, state: &UploadState) -> Result<(), String> {
- if state.server_url != auth::server_url()
+ if !auth::has_same_origin(&state.server_url, &auth::server_url())
|| state.owner_id != owner_id()
|| store_auth_missing()
{
@@ -536,6 +538,7 @@ struct Manager<B> {
wake: tokio::sync::Notify,
confirmation_timeout: Duration,
pending_rechecks: Mutex<HashMap<String, u32>>,
+ skipped_meta: Mutex<SkippedMeta>,
}
impl<B: UploadBackend> Manager<B> {
@@ -547,6 +550,7 @@ impl<B: UploadBackend> Manager<B> {
wake: tokio::sync::Notify::new(),
confirmation_timeout,
pending_rechecks: Mutex::new(HashMap::new()),
+ skipped_meta: Mutex::new(SkippedMeta::default()),
}
}
@@ -564,6 +568,12 @@ impl<B: UploadBackend> Manager<B> {
}
}
+ fn skipped_meta(&self) -> std::sync::MutexGuard<'_, SkippedMeta> {
+ self.skipped_meta
+ .lock()
+ .unwrap_or_else(|error| error.into_inner())
+ }
+
fn forget_pending_rechecks(&self, video_id: &str) {
self.pending_rechecks
.lock()
@@ -901,9 +911,93 @@ fn legacy_state(meta: &RecordingMeta) -> Option<UploadState> {
})
}
+fn upload_candidate(meta: &RecordingMeta) -> bool {
+ matches!(
+ meta.inner,
+ RecordingMetaInner::Instant(InstantRecordingMeta::Complete { .. })
+ ) && !matches!(meta.upload, None | Some(UploadMeta::Complete))
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+struct MetaStamp {
+ modified: SystemTime,
+ len: u64,
+ #[cfg(unix)]
+ inode: u64,
+ #[cfg(unix)]
+ changed: (i64, i64),
+}
+
+impl MetaStamp {
+ fn read(project: &Path) -> Option<Self> {
+ #[cfg(unix)]
+ use std::os::unix::fs::MetadataExt;
+ let metadata = std::fs::metadata(project.join("recording-meta.json")).ok()?;
+ Some(Self {
+ modified: metadata.modified().ok()?,
+ len: metadata.len(),
+ #[cfg(unix)]
+ inode: metadata.ino(),
+ #[cfg(unix)]
+ changed: (metadata.ctime(), metadata.ctime_nsec()),
+ })
+ }
+}
+
+// Only "not a candidate" verdicts from a successfully loaded meta are cached:
+// that verdict depends solely on recording-meta.json, while candidates still
+// go through the upload lock and instant-upload.json (where auth changes
+// apply) on every pass. A failed load may be transient (sharing violation,
+// antivirus lock) and leaves the stamp unchanged, so it is retried every pass.
+// Every writer replaces the file by rename, so a save also changes the inode
+// on unix.
+#[derive(Default)]
+struct SkippedMeta {
+ stamps: HashMap<OsString, (MetaStamp, u64)>,
+ pass: u64,
+}
+
+impl SkippedMeta {
+ fn begin_pass(&mut self) {
+ self.pass = self.pass.wrapping_add(1);
+ }
+
+ fn is_candidate(
+ &mut self,
+ project: &Path,
+ load: impl FnOnce(&Path) -> Option<RecordingMeta>,
+ ) -> bool {
+ let stamp = MetaStamp::read(project);
+ if let Some(stamp) = stamp
+ && let Some((cached, seen)) = self.stamps.get_mut(project.as_os_str())
+ && *cached == stamp
+ {
+ *seen = self.pass;
+ return false;
+ }
+ let candidate = load(project).map(|meta| upload_candidate(&meta));
+ match (stamp, candidate) {
+ (Some(stamp), Some(false)) => {
+ self.stamps
+ .insert(project.as_os_str().to_os_string(), (stamp, self.pass));
+ }
+ _ => {
+ self.stamps.remove(project.as_os_str());
+ }
+ }
+ candidate.unwrap_or(false)
+ }
+
+ fn finish_pass(&mut self) {
+ let pass = self.pass;
+ self.stamps.retain(|_, (_, seen)| *seen == pass);
+ }
+}
+
impl Manager<LiveBackend> {
async fn reconcile(self: &Arc<Self>, auth_changed: bool) {
self.reap().await;
+ self.skipped_meta().begin_pass();
for directory in crate::library::known_recordings_dirs() {
let Ok(entries) = std::fs::read_dir(directory) else {
continue;
@@ -913,16 +1007,9 @@ impl Manager<LiveBackend> {
return;
}
let project = entry.path();
- let Ok(meta) = RecordingMeta::load_for_project(&project) else {
- continue;
- };
- if !matches!(
- meta.inner,
- RecordingMetaInner::Instant(InstantRecordingMeta::Complete { .. })
- ) {
- continue;
- }
- if matches!(meta.upload, None | Some(UploadMeta::Complete)) {
+ if !self.skipped_meta().is_candidate(&project, |project| {
+ RecordingMeta::load_for_project(project).ok()
+ }) {
continue;
}
let ownership = match UploadLock::acquire(&project) {
@@ -951,6 +1038,7 @@ impl Manager<LiveBackend> {
}
}
}
+ self.skipped_meta().finish_pass();
}
async fn resume(
@@ -1107,7 +1195,9 @@ pub(crate) async fn delete_recording(project: PathBuf) -> Result<(), String> {
state.phase = UploadPhase::Cancelled;
write_state(&canonical, &state)?;
}
- crate::library::delete_recording_directory(&project)
+ tokio::task::spawn_blocking(move || crate::library::delete_recording_directory(&project))
+ .await
+ .map_err(|error| error.to_string())?
}
#[cfg(test)]
@@ -2285,4 +2375,167 @@ mod tests {
assert_eq!(state.phase, UploadPhase::Retrying);
fixture.assert_retained();
}
+
+ fn save_library_meta(project: &Path, upload: Option<UploadMeta>) {
+ std::fs::create_dir_all(project).unwrap();
+ RecordingMeta {
+ platform: None,
+ project_path: project.to_path_buf(),
+ pretty_name: "Library recording".into(),
+ sharing: None,
+ inner: RecordingMetaInner::Instant(InstantRecordingMeta::Complete {
+ fps: 30,
+ sample_rate: None,
+ }),
+ upload,
+ }
+ .save_for_project()
+ .unwrap();
+ }
+
+ fn scan_library(
+ skipped: &mut SkippedMeta,
+ library: &Path,
+ parses: &AtomicUsize,
+ ) -> Vec<PathBuf> {
+ skipped.begin_pass();
+ let mut candidates = std::fs::read_dir(library)
+ .unwrap()
+ .flatten()
+ .map(|entry| entry.path())
+ .filter(|project| {
+ skipped.is_candidate(project, |project| {
+ parses.fetch_add(1, Ordering::Relaxed);
+ RecordingMeta::load_for_project(project).ok()
+ })
+ })
+ .collect::<Vec<_>>();
+ skipped.finish_pass();
+ candidates.sort();
+ candidates
+ }
+
+ fn library_with_skipped_projects(skipped_projects: usize) -> Fixture {
+ let fixture = Fixture::new();
+ for index in 0..skipped_projects {
+ let upload = (index % 2 == 0).then_some(UploadMeta::Complete);
+ save_library_meta(&fixture.0.join(format!("skip-{index}.cap")), upload);
+ }
+ std::fs::write(fixture.0.join("notes.txt"), b"not a recording").unwrap();
+ fixture
+ }
+
+ #[test]
+ fn unchanged_skipped_recordings_are_not_parsed_again() {
+ let fixture = library_with_skipped_projects(6);
+ let mut skipped = SkippedMeta::default();
+ let parses = AtomicUsize::new(0);
+
+ assert_eq!(
+ scan_library(&mut skipped, &fixture.0, &parses),
+ vec![fixture.project()]
+ );
+ assert_eq!(parses.swap(0, Ordering::Relaxed), 8);
+ assert_eq!(skipped.stamps.len(), 6);
+
+ assert_eq!(
+ scan_library(&mut skipped, &fixture.0, &parses),
+ vec![fixture.project()]
+ );
+ assert_eq!(parses.load(Ordering::Relaxed), 2);
+ assert_eq!(
+ read_state(&fixture.project()).unwrap().unwrap().phase,
+ UploadPhase::Pending
+ );
+ }
+
+ #[test]
+ fn rewritten_recording_meta_is_reclassified_on_the_next_pass() {
+ let fixture = library_with_skipped_projects(2);
+ let mut skipped = SkippedMeta::default();
+ let parses = AtomicUsize::new(0);
+ let flipped = fixture.0.join("skip-1.cap");
+ scan_library(&mut skipped, &fixture.0, &parses);
+
+ save_library_meta(
+ &flipped,
+ Some(UploadMeta::SinglePartUpload {
+ video_id: "flipped-video".into(),
+ file_path: flipped.join("content/output.mp4"),
+ screenshot_path: flipped.join("screenshots/display.jpg"),
+ recording_dir: flipped.clone(),
+ }),
+ );
+ assert_eq!(
+ scan_library(&mut skipped, &fixture.0, &parses),
+ vec![fixture.project(), flipped.clone()]
+ );
+ assert!(!skipped.stamps.contains_key(flipped.as_os_str()));
+
+ save_library_meta(&flipped, Some(UploadMeta::Complete));
+ assert_eq!(
+ scan_library(&mut skipped, &fixture.0, &parses),
+ vec![fixture.project()]
+ );
+ parses.store(0, Ordering::Relaxed);
+ scan_library(&mut skipped, &fixture.0, &parses);
+ assert_eq!(parses.load(Ordering::Relaxed), 2);
+ }
+
+ #[test]
+ fn recordings_whose_meta_failed_to_load_are_retried() {
+ let fixture = Fixture::new();
+ let project = fixture.project();
+ let mut skipped = SkippedMeta::default();
+
+ skipped.begin_pass();
+ assert!(!skipped.is_candidate(&project, |_| None));
+ skipped.finish_pass();
+ assert!(!skipped.stamps.contains_key(project.as_os_str()));
+
+ skipped.begin_pass();
+ assert!(skipped.is_candidate(&project, |project| {
+ RecordingMeta::load_for_project(project).ok()
+ }));
+ skipped.finish_pass();
+ assert!(skipped.stamps.is_empty());
+ }
+
+ #[test]
+ fn unparsable_recording_meta_is_parsed_again_every_pass() {
+ let fixture = library_with_skipped_projects(2);
+ let mut skipped = SkippedMeta::default();
+ let parses = AtomicUsize::new(0);
+ let corrupt = fixture.0.join("skip-0.cap");
+ std::fs::write(corrupt.join("recording-meta.json"), b"{ not json").unwrap();
+
+ scan_library(&mut skipped, &fixture.0, &parses);
+ assert!(!skipped.stamps.contains_key(corrupt.as_os_str()));
+ assert_eq!(skipped.stamps.len(), 1);
+
+ parses.store(0, Ordering::Relaxed);
+ scan_library(&mut skipped, &fixture.0, &parses);
+ assert_eq!(parses.load(Ordering::Relaxed), 3);
+ assert!(!skipped.stamps.contains_key(corrupt.as_os_str()));
+ }
+
+ #[test]
+ fn deleted_recordings_leave_the_skip_cache() {
+ let fixture = library_with_skipped_projects(4);
+ let mut skipped = SkippedMeta::default();
+ let parses = AtomicUsize::new(0);
+ let deleted = fixture.0.join("skip-2.cap");
+ scan_library(&mut skipped, &fixture.0, &parses);
+ assert!(skipped.stamps.contains_key(deleted.as_os_str()));
+
+ std::fs::remove_dir_all(&deleted).unwrap();
+ scan_library(&mut skipped, &fixture.0, &parses);
+ assert_eq!(skipped.stamps.len(), 3);
+ assert!(!skipped.stamps.contains_key(deleted.as_os_str()));
+
+ std::fs::remove_dir_all(&fixture.0).unwrap();
+ std::fs::create_dir_all(&fixture.0).unwrap();
+ scan_library(&mut skipped, &fixture.0, &parses);
+ assert!(skipped.stamps.is_empty());
+ }
}
diff --git a/apps/desktop-gpui/src/upload/queue/verify_resume_tests.rs b/apps/desktop-gpui/src/upload/queue/verify_resume_tests.rs
index 5b2e8de53..6b0739523 100644
--- a/apps/desktop-gpui/src/upload/queue/verify_resume_tests.rs
+++ b/apps/desktop-gpui/src/upload/queue/verify_resume_tests.rs
@@ -126,6 +126,7 @@ fn assert_verified(
#[test]
fn saved_instant_upload_resumes_after_quit_mid_transfer() {
let case = "queue_resume_after_quit_mid_transfer";
+ let _shared_client = crate::auth::SHARED_API_CLIENT_TEST_LOCK.blocking_lock();
let scratch = Scratch::new(case);
virtual_time(async {
let mock = MockCap::start(vec![rule(
@@ -147,6 +148,7 @@ fn saved_instant_upload_resumes_after_quit_mid_transfer() {
mock.drain().await;
mock.new_session();
+ crate::auth::reset_shared_api_client();
let relaunched = Arc::new(Manager::new(LiveBackend, CONFIRMATION_TIMEOUT));
launch(&relaunched, &project).await;
settle(&relaunched).await;
@@ -178,6 +180,7 @@ fn saved_instant_upload_resumes_after_quit_mid_transfer() {
#[test]
fn saved_instant_upload_quit_during_verification_confirms_without_reupload() {
let case = "queue_resume_after_quit_during_verification";
+ let _shared_client = crate::auth::SHARED_API_CLIENT_TEST_LOCK.blocking_lock();
let scratch = Scratch::new(case);
virtual_time(async {
let mock = MockCap::start(vec![rule(Route::Api("recording-complete"), Fault::Hang)]).await;
@@ -196,6 +199,7 @@ fn saved_instant_upload_quit_during_verification_confirms_without_reupload() {
mock.drain().await;
mock.new_session();
+ crate::auth::reset_shared_api_client();
let relaunched = Arc::new(Manager::new(LiveBackend, CONFIRMATION_TIMEOUT));
launch(&relaunched, &project).await;
settle(&relaunched).await;
diff --git a/apps/desktop/src-tauri/src/editor_preparing/audio.rs b/apps/desktop/src-tauri/src/editor_preparing/audio.rs
index 0a58e3d73..a7e106ada 100644
--- a/apps/desktop/src-tauri/src/editor_preparing/audio.rs
+++ b/apps/desktop/src-tauri/src/editor_preparing/audio.rs
@@ -199,6 +199,7 @@ mod tests {
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
};
for extension in ["m4a", "ogg"] {
let relative = PathBuf::from(format!("segment-0/audio-input.{extension}"));
diff --git a/apps/desktop/src-tauri/src/import.rs b/apps/desktop/src-tauri/src/import.rs
index cebe7d9c0..3b517fcd5 100644
--- a/apps/desktop/src-tauri/src/import.rs
+++ b/apps/desktop/src-tauri/src/import.rs
@@ -1517,6 +1517,7 @@ pub async fn start_video_import(app: AppHandle, source_path: PathBuf) -> Result<
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
})
} else {
None
@@ -1702,6 +1703,7 @@ async fn append_mp4_to_editor_project(
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
})
} else {
None
diff --git a/apps/desktop/src-tauri/src/linux_instant_camera.rs b/apps/desktop/src-tauri/src/linux_instant_camera.rs
index 56d0aaa7a..ff9fa79d1 100644
--- a/apps/desktop/src-tauri/src/linux_instant_camera.rs
+++ b/apps/desktop/src-tauri/src/linux_instant_camera.rs
@@ -462,7 +462,7 @@ mod producer {
FFmpegVideoFrame,
feeds::camera::{self, CameraFeed, CameraFeedLock},
instant_recording::{
- LINUX_CAMERA_MAX_MASK_AGE, LinuxCameraBlur, LinuxCameraMaskReceipt,
+ LINUX_CAMERA_MAX_MASK_AGE, LinuxCameraBlur, LinuxCameraMaskReceipt, LinuxCameraPixels,
LinuxCameraProcessing, LinuxCameraPublisher, LinuxProcessedCameraFrame,
LinuxProcessedCameraSource,
},
@@ -765,7 +765,7 @@ mod producer {
}
}
Ok(Some(LinuxProcessedCameraFrame {
- bgra: Arc::from(bgra),
+ bgra: LinuxCameraPixels::new(bgra),
dimensions: receipt.dimensions,
stride: row,
timestamp: receipt.timestamp,
diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rs
index 757c65f87..55c0bd2f8 100644
--- a/apps/desktop/src-tauri/src/recording.rs
+++ b/apps/desktop/src-tauri/src/recording.rs
@@ -3422,6 +3422,10 @@ async fn start_recording_prepared(
cap_recording::PipelineHealthEvent::CaptureTargetLost { target } => {
Some(format!("Capture target lost: {target}"))
}
+ cap_recording::PipelineHealthEvent::MicrophoneDigitalSilence => Some(
+ "Microphone is recording silence. Another app, like a call, may be using it."
+ .to_string(),
+ ),
cap_recording::PipelineHealthEvent::SourceRestarted => None,
};
diff --git a/apps/desktop/src-tauri/src/recording_telemetry.rs b/apps/desktop/src-tauri/src/recording_telemetry.rs
index b13bcccf8..5d3238f87 100644
--- a/apps/desktop/src-tauri/src/recording_telemetry.rs
+++ b/apps/desktop/src-tauri/src/recording_telemetry.rs
@@ -139,7 +139,8 @@ impl RecordingHealthAccumulator {
| PipelineHealthEvent::EncoderRebuilt { .. }
| PipelineHealthEvent::SourceAudioReset { .. }
| PipelineHealthEvent::RecoveryFragmentCorrupt { .. }
- | PipelineHealthEvent::CaptureTargetLost { .. } => None,
+ | PipelineHealthEvent::CaptureTargetLost { .. }
+ | PipelineHealthEvent::MicrophoneDigitalSilence => None,
}
}
diff --git a/apps/desktop/src/routes/editor/context.ts b/apps/desktop/src/routes/editor/context.ts
index c8b3c4781..645385066 100644
--- a/apps/desktop/src/routes/editor/context.ts
+++ b/apps/desktop/src/routes/editor/context.ts
@@ -1823,7 +1823,10 @@ export const [EditorContextProvider, useBaseEditorContext] =
const visibleOrigin = origin - state.position;
- const originPercentage = Math.min(1, visibleOrigin / state.zoom);
+ const originPercentage = Math.min(
+ 1,
+ Math.max(0, visibleOrigin / state.zoom),
+ );
const newVisibleOrigin = zoom * originPercentage;
const newPosition = origin - newVisibleOrigin;
diff --git a/apps/desktop/src/utils/tauri.ts b/apps/desktop/src/utils/tauri.ts
index 767b14c17..65e5b101a 100644
--- a/apps/desktop/src/utils/tauri.ts
+++ b/apps/desktop/src/utils/tauri.ts
@@ -710,7 +710,12 @@ overlap_dropped_frames: number;
startup_overlap_drops: number }
export type AudioInputLevelChange = number
export type AudioLibraryTrack = { id: string; name: string; category: string }
-export type AudioMeta = { path: string; start_time?: number | null; device_id?: string | null; gap_summary?: AudioGapSummary | null }
+export type AudioMeta = { path: string; start_time?: number | null; device_id?: string | null; gap_summary?: AudioGapSummary | null;
+/**
+ * Milliseconds the unmuted microphone delivered sustained bit-exact silence,
+ * e.g. while a VoIP call held the input on macOS.
+ */
+digital_silence_ms?: number | null }
/**
* A timeline-positioned audio clip (background music or imported audio).
*
diff --git a/apps/discord-bot/src/index.ts b/apps/discord-bot/src/index.ts
index 3b2d0846a..1580c6183 100644
--- a/apps/discord-bot/src/index.ts
+++ b/apps/discord-bot/src/index.ts
@@ -6,6 +6,11 @@ import * as jose from "jose";
import { App } from "octokit";
import nacl from "tweetnacl";
import * as v from "valibot";
+import {
+ canManageGuild,
+ isGuildInvocation,
+ matchesRepositoryClaims,
+} from "./release-checks";
const DISCORD_APP_ID = "1334742236096757861";
const PUBLIC_KEY =
@@ -18,12 +23,16 @@ const GITHUB_APP_INSTALLATION = 63538726;
const WORKFLOW_FILE = "publish.yml";
const CHANNEL_MESSAGE_WITH_SOURCE = 4;
+const EPHEMERAL = 1 << 6;
type InteractionBody = {
id: string;
token: string;
channel_id: string;
+ guild_id?: string;
+ authorizing_integration_owners?: Partial<Record<string, string>>;
member: {
+ permissions?: string;
user: {
id: string;
username: string;
@@ -59,11 +68,13 @@ type Message = {
const app = new Hono<{ Bindings: Env }>();
app.post("/interactions", async (c) => {
- const body = await c.req.json();
+ const rawBody = await c.req.text();
- const verification = await verifyRequest(c.req.raw, body);
+ const verification = verifyRequest(c.req.raw, rawBody);
if (!verification) return new Response("Invalid signature", { status: 401 });
+ const body = JSON.parse(rawBody);
+
// Ping
if (body.type === 1) return Response.json({ type: 1 });
@@ -107,7 +118,7 @@ const ghActionsOidc = createMiddleware<{
if (!payload.sub) return new Response("Missing sub claim", { status: 401 });
- if (!payload.sub?.includes(`${GITHUB_ORG}/${GITHUB_REPO}`)) {
+ if (!matchesRepositoryClaims(payload, GITHUB_ORG, GITHUB_REPO)) {
return new Response("Invalid repository", { status: 401 });
}
@@ -235,6 +246,25 @@ async function handleCommand(interaction: InteractionBody, env: Env) {
switch (data.name) {
case "release": {
+ if (!isGuildInvocation(interaction))
+ return {
+ type: CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content: "/release can only be used in a server.",
+ flags: EPHEMERAL,
+ },
+ };
+
+ if (!canManageGuild(interaction.member.permissions))
+ return {
+ type: CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content:
+ "/release requires the Manage Server or Administrator permission.",
+ flags: EPHEMERAL,
+ },
+ };
+
await env.release_discord_interactions.put(
interaction.id,
JSON.stringify(interaction),
@@ -266,17 +296,16 @@ async function handleCommand(interaction: InteractionBody, env: Env) {
}
}
-async function verifyRequest(request: Request, jsonBody: unknown) {
+function verifyRequest(request: Request, rawBody: string) {
const signature = request.headers.get("X-Signature-Ed25519");
- if (!signature) throw new Error("Signature header not found");
-
const timestamp = request.headers.get("X-Signature-Timestamp");
- if (!timestamp) throw new Error("Timestamp header not found");
+ if (!signature || !/^[0-9a-f]{128}$/i.test(signature) || !timestamp)
+ return false;
return nacl.sign.detached.verify(
- new Buffer(timestamp + JSON.stringify(jsonBody)),
- new Buffer(signature, "hex"),
- new Buffer(PUBLIC_KEY, "hex"),
+ Buffer.from(timestamp + rawBody),
+ Buffer.from(signature, "hex"),
+ Buffer.from(PUBLIC_KEY, "hex"),
);
}
diff --git a/apps/discord-bot/src/release-checks.test.ts b/apps/discord-bot/src/release-checks.test.ts
new file mode 100644
index 000000000..c9d68d844
--- /dev/null
+++ b/apps/discord-bot/src/release-checks.test.ts
@@ -0,0 +1,142 @@
+import { describe, expect, it } from "vitest";
+import {
+ canManageGuild,
+ isGuildInvocation,
+ matchesRepositoryClaims,
+} from "./release-checks";
+
+const GUILD_ID = "1100000000000000000";
+const USER_ID = "1200000000000000000";
+
+describe("isGuildInvocation", () => {
+ it("accepts a guild install invocation", () => {
+ expect(
+ isGuildInvocation({
+ guild_id: GUILD_ID,
+ member: { permissions: "8" },
+ authorizing_integration_owners: { "0": GUILD_ID },
+ }),
+ ).toBe(true);
+ });
+
+ it("accepts a guild invocation without integration owners", () => {
+ expect(
+ isGuildInvocation({ guild_id: GUILD_ID, member: { permissions: "8" } }),
+ ).toBe(true);
+ });
+
+ it("accepts a guild invocation authorized by both install types", () => {
+ expect(
+ isGuildInvocation({
+ guild_id: GUILD_ID,
+ member: { permissions: "8" },
+ authorizing_integration_owners: { "0": GUILD_ID, "1": USER_ID },
+ }),
+ ).toBe(true);
+ });
+
+ it("rejects a DM invocation", () => {
+ expect(
+ isGuildInvocation({
+ authorizing_integration_owners: { "1": USER_ID },
+ }),
+ ).toBe(false);
+ });
+
+ it("rejects a bot DM invocation", () => {
+ expect(
+ isGuildInvocation({ authorizing_integration_owners: { "0": "0" } }),
+ ).toBe(false);
+ });
+
+ it("rejects a user install invocation inside a guild", () => {
+ expect(
+ isGuildInvocation({
+ guild_id: GUILD_ID,
+ member: { permissions: "8" },
+ authorizing_integration_owners: { "1": USER_ID },
+ }),
+ ).toBe(false);
+ });
+
+ it("rejects a guild invocation without member data", () => {
+ expect(isGuildInvocation({ guild_id: GUILD_ID })).toBe(false);
+ });
+});
+
+describe("canManageGuild", () => {
+ it("accepts ADMINISTRATOR", () => {
+ expect(canManageGuild("8")).toBe(true);
+ });
+
+ it("accepts MANAGE_GUILD", () => {
+ expect(canManageGuild("32")).toBe(true);
+ });
+
+ it("reads bits above 2^53 without losing precision", () => {
+ expect(canManageGuild(((1n << 53n) | (1n << 5n)).toString())).toBe(true);
+ expect(canManageGuild(((1n << 53n) | 1n).toString())).toBe(false);
+ });
+
+ it("rejects members without either permission", () => {
+ expect(canManageGuild("0")).toBe(false);
+ expect(canManageGuild(((1n << 31n) | 0b111n).toString())).toBe(false);
+ });
+
+ it("rejects missing or malformed values", () => {
+ expect(canManageGuild(undefined)).toBe(false);
+ expect(canManageGuild("")).toBe(false);
+ expect(canManageGuild("-8")).toBe(false);
+ expect(canManageGuild("0x8")).toBe(false);
+ expect(canManageGuild(8)).toBe(false);
+ });
+});
+
+describe("matchesRepositoryClaims", () => {
+ it("accepts the exact repository and owner", () => {
+ expect(
+ matchesRepositoryClaims(
+ { repository: "CapSoftware/Cap", repository_owner: "CapSoftware" },
+ "CapSoftware",
+ "Cap",
+ ),
+ ).toBe(true);
+ });
+
+ it("rejects repositories that only contain the name", () => {
+ for (const repository of [
+ "CapSoftware/Cap-fork",
+ "someone/CapSoftware/Cap",
+ "CapSoftware/Caps",
+ ]) {
+ expect(
+ matchesRepositoryClaims(
+ { repository, repository_owner: "CapSoftware" },
+ "CapSoftware",
+ "Cap",
+ ),
+ ).toBe(false);
+ }
+ });
+
+ it("rejects a different owner", () => {
+ expect(
+ matchesRepositoryClaims(
+ { repository: "CapSoftware/Cap", repository_owner: "someone" },
+ "CapSoftware",
+ "Cap",
+ ),
+ ).toBe(false);
+ });
+
+ it("rejects missing claims", () => {
+ expect(matchesRepositoryClaims({}, "CapSoftware", "Cap")).toBe(false);
+ expect(
+ matchesRepositoryClaims(
+ { repository: "CapSoftware/Cap" },
+ "CapSoftware",
+ "Cap",
+ ),
+ ).toBe(false);
+ });
+});
diff --git a/apps/discord-bot/src/release-checks.ts b/apps/discord-bot/src/release-checks.ts
new file mode 100644
index 000000000..e8a04b89b
--- /dev/null
+++ b/apps/discord-bot/src/release-checks.ts
@@ -0,0 +1,38 @@
+const ADMINISTRATOR = 1n << 3n;
+const MANAGE_GUILD = 1n << 5n;
+const GUILD_INSTALL = "0";
+
+export type ReleaseInvocation = {
+ guild_id?: string;
+ member?: { permissions?: string };
+ authorizing_integration_owners?: Partial<Record<string, string>>;
+};
+
+export function isGuildInvocation(interaction: ReleaseInvocation) {
+ if (typeof interaction.guild_id !== "string" || !interaction.guild_id)
+ return false;
+ if (!interaction.member) return false;
+
+ const owners = interaction.authorizing_integration_owners;
+ if (owners && owners[GUILD_INSTALL] !== interaction.guild_id) return false;
+
+ return true;
+}
+
+export function canManageGuild(permissions: unknown) {
+ if (typeof permissions !== "string" || !/^\d+$/.test(permissions))
+ return false;
+
+ return (BigInt(permissions) & (ADMINISTRATOR | MANAGE_GUILD)) !== 0n;
+}
+
+export function matchesRepositoryClaims(
+ payload: Record<string, unknown>,
+ owner: string,
+ repo: string,
+) {
+ return (
+ payload.repository === `${owner}/${repo}` &&
+ payload.repository_owner === owner
+ );
+}
diff --git a/apps/discord-bot/test/interactions.spec.ts b/apps/discord-bot/test/interactions.spec.ts
new file mode 100644
index 000000000..165c89cb5
--- /dev/null
+++ b/apps/discord-bot/test/interactions.spec.ts
@@ -0,0 +1,98 @@
+import { env } from "cloudflare:test";
+import { Buffer } from "buffer";
+import nacl from "tweetnacl";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import worker from "../src/index";
+
+const keyPair = nacl.sign.keyPair.fromSeed(new Uint8Array(32).fill(1));
+const verify = nacl.sign.detached.verify;
+
+function signedRequest(body: string) {
+ const timestamp = "1770000000";
+ const signature = nacl.sign.detached(
+ Buffer.from(timestamp + body),
+ keyPair.secretKey,
+ );
+
+ return new Request("https://example.com/interactions", {
+ method: "POST",
+ body,
+ headers: {
+ "Content-Type": "application/json",
+ "X-Signature-Ed25519": Buffer.from(signature).toString("hex"),
+ "X-Signature-Timestamp": timestamp,
+ },
+ });
+}
+
+describe("Discord interaction signatures", () => {
+ beforeEach(() => {
+ vi.spyOn(nacl.sign.detached, "verify").mockImplementation(
+ (message, signature) => verify(message, signature, keyPair.publicKey),
+ );
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it.each([
+ '{"type":1}',
+ '{ "type": 1 }\n',
+ '{"type":1,"user":{"username":"caf\\u00e9"}}',
+ ])("acknowledges a ping signed over the exact body %s", async (body) => {
+ const response = await worker.fetch(signedRequest(body), env);
+
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ type: 1 });
+ });
+
+ it("rejects a changed body even when its parsed JSON is unchanged", async () => {
+ const request = new Request(signedRequest('{"type":1}'), {
+ body: '{ "type": 1 }',
+ });
+ const response = await worker.fetch(request, env);
+
+ expect(response.status).toBe(401);
+ });
+
+ it("rejects a changed timestamp", async () => {
+ const request = signedRequest('{"type":1}');
+ request.headers.set("X-Signature-Timestamp", "1770000001");
+ const response = await worker.fetch(request, env);
+
+ expect(response.status).toBe(401);
+ });
+
+ it.each(["X-Signature-Ed25519", "X-Signature-Timestamp"])(
+ "rejects a missing %s header",
+ async (header) => {
+ const request = signedRequest('{"type":1}');
+ request.headers.delete(header);
+ const response = await worker.fetch(request, env);
+
+ expect(response.status).toBe(401);
+ },
+ );
+
+ it.each(["00", "zz".repeat(64), "00".repeat(64)])(
+ "rejects an invalid signature %s",
+ async (signature) => {
+ const request = signedRequest('{"type":1}');
+ request.headers.set("X-Signature-Ed25519", signature);
+ const response = await worker.fetch(request, env);
+
+ expect(response.status).toBe(401);
+ },
+ );
+
+ it("checks the signature before parsing untrusted JSON", async () => {
+ const request = new Request("https://example.com/interactions", {
+ method: "POST",
+ body: "not JSON",
+ });
+ const response = await worker.fetch(request, env);
+
+ expect(response.status).toBe(401);
+ });
+});
diff --git a/apps/media-server/Dockerfile b/apps/media-server/Dockerfile
index c09234479..64b89c176 100644
--- a/apps/media-server/Dockerfile
+++ b/apps/media-server/Dockerfile
@@ -15,13 +15,17 @@ COPY apps/media-server/capcodec.md ./capcodec.md
RUN bun test src/__tests__/lib/audio-quality-policy.test.ts src/__tests__/lib/audio-quality.integration.test.ts \
&& bun test src/__tests__/lib/audio-quality-formats.integration.test.ts src/__tests__/lib/audio-quality-benchmark.test.ts \
&& bun test src/__tests__/lib/audio-levels.integration.test.ts \
- && bun test src/__tests__/lib/drive-resumable-upload.test.ts src/__tests__/lib/storage-upload.test.ts src/__tests__/lib/container-memory.test.ts \
+ && bun test src/__tests__/lib/drive-resumable-upload.test.ts src/__tests__/lib/storage-upload.test.ts src/__tests__/lib/container-memory.test.ts src/__tests__/lib/parallel-download.test.ts \
+ && bun test src/__tests__/lib/loom-import-limit.test.ts \
&& bun test src/__tests__/lib/media-size.test.ts src/__tests__/lib/media-probe.integration.test.ts \
&& bun test src/__tests__/lib/media-audio.integration.test.ts \
&& bun test src/__tests__/lib/recording-verification.integration.test.ts src/__tests__/lib/job-manager.test.ts \
- && bun test src/__tests__/lib/media-transfer.test.ts src/__tests__/lib/capcodec-encoder.test.ts \
+ && bun test src/__tests__/lib/media-transfer.test.ts src/__tests__/lib/capcodec-encoder.test.ts src/__tests__/lib/nvenc-encoder.test.ts \
&& bun test src/__tests__/routes/recording-verification.test.ts \
&& bun test src/__tests__/routes/video.test.ts \
+ && bun test src/__tests__/lib/lifecycle.test.ts src/__tests__/lib/temp-files.test.ts \
+ && bun test src/__tests__/routes/lifecycle.test.ts \
+ && bun test src/__tests__/routes/peer-jobs.test.ts \
&& bun test src/__tests__/lib/media-routes-real-world.integration.test.ts \
&& bun test src/__tests__/lib/media-video.integration.test.ts
@@ -37,6 +41,6 @@ ENV PORT=3456
EXPOSE 3456
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
- CMD ["bun", "-e", "fetch(`http://127.0.0.1:${process.env.PORT || 3456}/health`).then((response) => process.exit(response.ok ? 0 : 1)).catch(() => process.exit(1))"]
+ CMD ["bun", "-e", "fetch(`http://127.0.0.1:${process.env.PORT || 3456}/health`).then(async (response) => process.exit(response.ok || (await response.json().catch(() => ({}))).status === 'draining' ? 0 : 1)).catch(() => process.exit(1))"]
CMD ["bun", "run", "src/index.ts"]
diff --git a/apps/media-server/src/__tests__/lib/job-manager.test.ts b/apps/media-server/src/__tests__/lib/job-manager.test.ts
index ff2b43e69..5bada4e69 100644
--- a/apps/media-server/src/__tests__/lib/job-manager.test.ts
+++ b/apps/media-server/src/__tests__/lib/job-manager.test.ts
@@ -6,7 +6,9 @@ import {
cleanupExpiredJobs,
createJob,
deleteJob,
+ extendJobLifetime,
getJob,
+ getJobTotals,
type JobProgress,
type RecordingWorkerAcknowledgement,
sendWebhook,
@@ -522,3 +524,83 @@ describe("job cleanup", () => {
expect(currentJob?.error).toContain("maximum lifetime of 60 minutes");
});
});
+
+describe("job lifetime", () => {
+ test("extends the lifetime cap for long work without shortening it", () => {
+ const job = createTrackedJob("job-extended-lifetime");
+ const now = Date.now();
+ job.phase = "processing";
+ job.createdAt = now - 10 * 60 * 1000;
+
+ expect(extendJobLifetime(job.jobId, 60_000)).toBe(
+ job.createdAt + 60 * 60 * 1000,
+ );
+ const deadline = extendJobLifetime(job.jobId, 2 * 60 * 60 * 1000);
+ expect(deadline).toBeGreaterThanOrEqual(now + 2 * 60 * 60 * 1000);
+ expect(extendJobLifetime(job.jobId, 60_000)).toBe(deadline);
+
+ job.createdAt = now - 90 * 60 * 1000;
+ job.updatedAt = Date.now();
+ expect(cleanupExpiredJobs()).toBe(0);
+ expect(getJob(job.jobId)?.phase).toBe("processing");
+ });
+
+ test("keeps a hard upper bound on extended lifetimes", () => {
+ const job = createTrackedJob("job-capped-lifetime");
+ job.phase = "uploading";
+ job.createdAt = Date.now() - 10 * 60 * 1000;
+
+ expect(extendJobLifetime(job.jobId, 24 * 60 * 60 * 1000)).toBe(
+ job.createdAt + 4 * 60 * 60 * 1000,
+ );
+ expect(extendJobLifetime(job.jobId, Number.NaN)).toBeUndefined();
+ expect(extendJobLifetime(job.jobId, -1)).toBeUndefined();
+ });
+
+ test("expires a job past its extended deadline", () => {
+ const job = createTrackedJob("job-past-extended-lifetime");
+ const abortController = new AbortController();
+ updateJob(job.jobId, { phase: "processing", abortController });
+ job.createdAt = Date.now() - 10 * 60 * 1000;
+ extendJobLifetime(job.jobId, 2 * 60 * 60 * 1000);
+ job.createdAt -= 3 * 60 * 60 * 1000;
+ job.lifetimeDeadlineAt = Date.now() - 1;
+ job.updatedAt = Date.now();
+
+ expect(cleanupExpiredJobs()).toBe(1);
+ expect(job.phase).toBe("error");
+ expect(job.error).toContain("maximum lifetime");
+ expect(abortController.signal.aborted).toBe(true);
+ });
+
+ test("does not extend terminal jobs", () => {
+ const job = createTrackedJob("job-terminal-lifetime");
+ updateJob(job.jobId, { phase: "complete" });
+ expect(extendJobLifetime(job.jobId, 2 * 60 * 60 * 1000)).toBeUndefined();
+ expect(job.lifetimeDeadlineAt).toBeUndefined();
+ });
+});
+
+describe("job totals", () => {
+ test("counts created, terminal, and expired jobs", () => {
+ const before = getJobTotals();
+ const completed = createTrackedJob("job-total-complete");
+ const errored = createTrackedJob("job-total-error");
+ const cancelled = createTrackedJob("job-total-cancelled");
+ const stale = createTrackedJob("job-total-stale");
+ updateJob(completed.jobId, { phase: "complete" });
+ updateJob(completed.jobId, { phase: "error" });
+ updateJob(errored.jobId, { phase: "error" });
+ updateJob(cancelled.jobId, { phase: "cancelled" });
+ updateJob(stale.jobId, { phase: "processing" });
+ stale.updatedAt = Date.now() - 16 * 60 * 1000;
+ cleanupExpiredJobs();
+
+ const after = getJobTotals();
+ expect(after.created - before.created).toBe(4);
+ expect(after.completed - before.completed).toBe(1);
+ expect(after.errored - before.errored).toBe(2);
+ expect(after.cancelled - before.cancelled).toBe(1);
+ expect(after.expired - before.expired).toBe(1);
+ });
+});
diff --git a/apps/media-server/src/__tests__/lib/lifecycle.test.ts b/apps/media-server/src/__tests__/lib/lifecycle.test.ts
new file mode 100644
index 000000000..bf1aa4f7e
--- /dev/null
+++ b/apps/media-server/src/__tests__/lib/lifecycle.test.ts
@@ -0,0 +1,207 @@
+import { afterEach, describe, expect, test } from "bun:test";
+import {
+ beginRequest,
+ createShutdownHandler,
+ getDrainBlockers,
+ getIdleTimeoutSeconds,
+ getShutdownGraceMs,
+ isDraining,
+ isIdle,
+ restoreIdleTimeoutOnFirstChunk,
+ setDraining,
+ setRequestIdleTimeout,
+ trackBackgroundTask,
+ waitForDrain,
+} from "../../lib/lifecycle";
+
+const originalGrace = process.env.MEDIA_SERVER_SHUTDOWN_GRACE_MS;
+const originalIdle = process.env.MEDIA_SERVER_IDLE_TIMEOUT_SECONDS;
+
+function deferred() {
+ let resolve = () => {};
+ const promise = new Promise<void>((done) => {
+ resolve = done;
+ });
+ return { promise, resolve };
+}
+
+afterEach(() => {
+ setDraining(false);
+ if (originalGrace === undefined)
+ delete process.env.MEDIA_SERVER_SHUTDOWN_GRACE_MS;
+ else process.env.MEDIA_SERVER_SHUTDOWN_GRACE_MS = originalGrace;
+ if (originalIdle === undefined)
+ delete process.env.MEDIA_SERVER_IDLE_TIMEOUT_SECONDS;
+ else process.env.MEDIA_SERVER_IDLE_TIMEOUT_SECONDS = originalIdle;
+});
+
+describe("shutdown configuration", () => {
+ test("defaults to the immediate shutdown path", () => {
+ delete process.env.MEDIA_SERVER_SHUTDOWN_GRACE_MS;
+ expect(getShutdownGraceMs()).toBe(0);
+ process.env.MEDIA_SERVER_SHUTDOWN_GRACE_MS = "not-a-number";
+ expect(getShutdownGraceMs()).toBe(0);
+ process.env.MEDIA_SERVER_SHUTDOWN_GRACE_MS = "120000";
+ expect(getShutdownGraceMs()).toBe(120_000);
+ });
+
+ test("bounds the idle timeout to Bun's maximum", () => {
+ delete process.env.MEDIA_SERVER_IDLE_TIMEOUT_SECONDS;
+ expect(getIdleTimeoutSeconds()).toBe(255);
+ process.env.MEDIA_SERVER_IDLE_TIMEOUT_SECONDS = "90";
+ expect(getIdleTimeoutSeconds()).toBe(90);
+ process.env.MEDIA_SERVER_IDLE_TIMEOUT_SECONDS = "10000";
+ expect(getIdleTimeoutSeconds()).toBe(255);
+ });
+
+ test("ignores servers without per-request timeouts", () => {
+ const calls: Array<[Request, number]> = [];
+ const request = new Request("http://localhost/video/convert");
+ setRequestIdleTimeout(undefined, request, 0);
+ setRequestIdleTimeout(
+ {
+ timeout: (req: Request, seconds: number) => calls.push([req, seconds]),
+ },
+ request,
+ 0,
+ );
+ expect(calls).toEqual([[request, 0]]);
+ });
+});
+
+describe("drain", () => {
+ test("waits for background work and in-flight requests", async () => {
+ const work = deferred();
+ const task = trackBackgroundTask(work.promise);
+ const endRequest = beginRequest();
+ expect(isIdle()).toBe(false);
+ expect(getDrainBlockers().backgroundTasks).toBeGreaterThanOrEqual(1);
+
+ const drained = waitForDrain(5_000);
+ await Bun.sleep(50);
+ work.resolve();
+ await task;
+ await Bun.sleep(50);
+ endRequest();
+
+ expect(await drained).toBe(true);
+ expect(isIdle()).toBe(true);
+ });
+
+ test("gives up when the grace period elapses", async () => {
+ const work = deferred();
+ const task = trackBackgroundTask(work.promise);
+ const startedAt = Date.now();
+
+ expect(await waitForDrain(300)).toBe(false);
+ expect(Date.now() - startedAt).toBeGreaterThanOrEqual(250);
+
+ work.resolve();
+ await task;
+ });
+
+ test("stops tracking rejected background work", async () => {
+ const failure = trackBackgroundTask(Promise.reject(new Error("failed")));
+ await failure.catch(() => {});
+ await Bun.sleep(0);
+ expect(await waitForDrain(1_000)).toBe(true);
+ });
+});
+
+describe("shutdown handler", () => {
+ test("keeps today's immediate shutdown when no grace period is set", async () => {
+ const work = deferred();
+ const task = trackBackgroundTask(work.promise);
+ let finalized = 0;
+ const shutdown = createShutdownHandler(
+ async () => {
+ finalized++;
+ },
+ () => 0,
+ );
+
+ await shutdown("SIGTERM");
+ await shutdown("SIGTERM");
+
+ expect(finalized).toBe(1);
+ expect(isDraining()).toBe(false);
+ work.resolve();
+ await task;
+ });
+
+ test("drains before finalizing when a grace period is set", async () => {
+ const work = deferred();
+ const task = trackBackgroundTask(work.promise);
+ let finalizedWhileBusy: boolean | undefined;
+ const shutdown = createShutdownHandler(
+ async () => {
+ finalizedWhileBusy = !isIdle();
+ },
+ () => 10_000,
+ );
+
+ const shuttingDown = shutdown("SIGTERM");
+ await Bun.sleep(50);
+ expect(isDraining()).toBe(true);
+ expect(finalizedWhileBusy).toBeUndefined();
+
+ work.resolve();
+ await task;
+ await shuttingDown;
+ expect(finalizedWhileBusy).toBe(false);
+ });
+
+ test("forces the immediate path on a second signal", async () => {
+ const work = deferred();
+ const task = trackBackgroundTask(work.promise);
+ let finalized = 0;
+ const shutdown = createShutdownHandler(
+ async () => {
+ finalized++;
+ },
+ () => 60_000,
+ );
+
+ const shuttingDown = shutdown("SIGTERM");
+ await Bun.sleep(50);
+ expect(finalized).toBe(0);
+ await shutdown("SIGINT");
+ await shuttingDown;
+ expect(finalized).toBe(1);
+
+ work.resolve();
+ await task;
+ });
+});
+
+describe("streamed response idle timeout", () => {
+ test("comes back once, on the first chunk, and cancel still reaches the source", async () => {
+ let restores = 0;
+ let cancelled = false;
+ let push: ReadableStreamDefaultController<Uint8Array> | undefined;
+ const source = new ReadableStream<Uint8Array>({
+ start(controller) {
+ push = controller;
+ },
+ cancel() {
+ cancelled = true;
+ },
+ });
+ const reader = restoreIdleTimeoutOnFirstChunk(source, () => {
+ restores++;
+ }).getReader();
+
+ const first = reader.read();
+ await Bun.sleep(10);
+ expect(restores).toBe(0);
+
+ push?.enqueue(new Uint8Array([1]));
+ expect((await first).value).toEqual(new Uint8Array([1]));
+ push?.enqueue(new Uint8Array([2]));
+ await reader.read();
+ expect(restores).toBe(1);
+
+ await reader.cancel();
+ expect(cancelled).toBe(true);
+ });
+});
diff --git a/apps/media-server/src/__tests__/lib/loom-import-limit.test.ts b/apps/media-server/src/__tests__/lib/loom-import-limit.test.ts
new file mode 100644
index 000000000..cb5aa9a29
--- /dev/null
+++ b/apps/media-server/src/__tests__/lib/loom-import-limit.test.ts
@@ -0,0 +1,193 @@
+import { afterEach, describe, expect, spyOn, test } from "bun:test";
+import { execFileSync } from "node:child_process";
+import { mkdtemp, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import {
+ downloadVideoToTemp,
+ LOOM_IMPORT_MAX_BYTES,
+} from "../../lib/media-video";
+
+const originalFetch = globalThis.fetch;
+const originalDownloadConcurrency =
+ process.env.MEDIA_SERVER_DOWNLOAD_CONCURRENCY;
+
+function stubFetch(response: () => Promise<Response>) {
+ globalThis.fetch = Object.assign(response, {
+ preconnect: originalFetch.preconnect,
+ });
+}
+
+describe("Loom import file size", () => {
+ afterEach(() => {
+ globalThis.fetch = originalFetch;
+ if (originalDownloadConcurrency === undefined) {
+ delete process.env.MEDIA_SERVER_DOWNLOAD_CONCURRENCY;
+ } else {
+ process.env.MEDIA_SERVER_DOWNLOAD_CONCURRENCY =
+ originalDownloadConcurrency;
+ }
+ });
+
+ test("permits exactly the configured boundary", async () => {
+ expect(LOOM_IMPORT_MAX_BYTES).toBe(10 * 1024 ** 3);
+ stubFetch(async () => new Response(new Uint8Array(16)));
+ const result = await downloadVideoToTemp(
+ "https://source.test/video.mp4",
+ ".mp4",
+ undefined,
+ 16,
+ );
+ try {
+ expect(Bun.file(result.path).size).toBe(16);
+ } finally {
+ await result.cleanup();
+ }
+ });
+
+ test("rejects an oversized declared body before reading it", async () => {
+ let cancelled = false;
+ stubFetch(
+ async () =>
+ new Response(
+ new ReadableStream({
+ cancel() {
+ cancelled = true;
+ },
+ }),
+ { headers: { "Content-Length": "17" } },
+ ),
+ );
+ await expect(
+ downloadVideoToTemp(
+ "https://source.test/video.mp4",
+ ".mp4",
+ undefined,
+ 16,
+ ),
+ ).rejects.toThrow("10 GB");
+ expect(cancelled).toBe(true);
+ });
+
+ test.each([
+ ["bounded Loom downloads", 16, 30 * 60 * 1000],
+ ["ordinary downloads", undefined, 10 * 60 * 1000],
+ ])("keeps the expected timeout for %s", async (_, maxBytes, timeoutMs) => {
+ const timeoutSpy = spyOn(AbortSignal, "timeout");
+ stubFetch(async () => new Response(new Uint8Array(16)));
+ try {
+ const result = await downloadVideoToTemp(
+ "https://source.test/video.mp4",
+ ".mp4",
+ undefined,
+ maxBytes,
+ );
+ try {
+ expect(timeoutSpy).toHaveBeenCalledWith(timeoutMs);
+ } finally {
+ await result.cleanup();
+ }
+ } finally {
+ timeoutSpy.mockRestore();
+ }
+ });
+
+ test("enforces the import limit when parallel downloads are enabled", async () => {
+ process.env.MEDIA_SERVER_DOWNLOAD_CONCURRENCY = "4";
+ stubFetch(async () => new Response(new Uint8Array(17)));
+ await expect(
+ downloadVideoToTemp(
+ "https://source.test/video.mp4",
+ ".mp4",
+ undefined,
+ 16,
+ ),
+ ).rejects.toThrow("10 GB");
+ });
+
+ test("cancels a chunked transfer when it exceeds the limit", async () => {
+ let cancelled = false;
+ stubFetch(
+ async () =>
+ new Response(
+ new ReadableStream({
+ start(controller) {
+ controller.enqueue(new Uint8Array(8));
+ controller.enqueue(new Uint8Array(9));
+ },
+ cancel() {
+ cancelled = true;
+ },
+ }),
+ ),
+ );
+ await expect(
+ downloadVideoToTemp(
+ "https://source.test/video.mp4",
+ ".mp4",
+ undefined,
+ 16,
+ ),
+ ).rejects.toThrow("10 GB");
+ expect(cancelled).toBe(true);
+ });
+
+ test("rejects an oversized real HLS recording without returning truncated media", async () => {
+ const directory = await mkdtemp(join(tmpdir(), "cap-loom-limit-"));
+ let server: ReturnType<typeof Bun.serve> | undefined;
+ try {
+ execFileSync("ffmpeg", [
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ "-y",
+ "-f",
+ "lavfi",
+ "-i",
+ "testsrc2=size=64x64:rate=10",
+ "-t",
+ "2",
+ "-c:v",
+ "libx264",
+ "-g",
+ "10",
+ "-f",
+ "hls",
+ "-hls_time",
+ "1",
+ "-hls_segment_filename",
+ join(directory, "segment%d.ts"),
+ join(directory, "index.m3u8"),
+ ]);
+ globalThis.fetch = originalFetch;
+ server = Bun.serve({
+ hostname: "127.0.0.1",
+ port: 0,
+ fetch(request) {
+ const path = new URL(request.url).pathname;
+ return /^\/(?:index\.m3u8|segment\d+\.ts)$/.test(path)
+ ? new Response(Bun.file(join(directory, path.slice(1))))
+ : new Response(null, { status: 404 });
+ },
+ });
+ const source = `${server.url}index.m3u8`;
+ const complete = await downloadVideoToTemp(
+ source,
+ ".m3u8",
+ undefined,
+ 1024 ** 2,
+ );
+ try {
+ expect(Bun.file(complete.path).size).toBeGreaterThan(1);
+ } finally {
+ await complete.cleanup();
+ }
+ await expect(
+ downloadVideoToTemp(source, ".m3u8", undefined, 1),
+ ).rejects.toThrow("10 GB");
+ } finally {
+ server?.stop(true);
+ await rm(directory, { recursive: true, force: true });
+ }
+ });
+});
diff --git a/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts b/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
index 826d49234..44cd86983 100644
--- a/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, test } from "bun:test";
+import { describe, expect, spyOn, test } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -8,6 +8,7 @@ import {
extractAudioStream,
getActiveProcessCount,
} from "../../lib/media-audio";
+import * as subprocess from "../../lib/subprocess";
const FIXTURES_DIR = join(import.meta.dir, "..", "fixtures");
const TEST_VIDEO_WITH_AUDIO = `file://${join(FIXTURES_DIR, "test-with-audio.mp4")}`;
@@ -223,6 +224,69 @@ describe("mediaAudio integration tests", () => {
});
describe("extractAudioStream", () => {
+ test("preserves queued audio when the reader pauses before EOF", async () => {
+ const expected = await extractAudio(TEST_VIDEO_WITH_AUDIO);
+ const { stream, cleanup } = extractAudioStream(TEST_VIDEO_WITH_AUDIO);
+ const reader = stream.getReader();
+ const chunks: Uint8Array[] = [];
+ try {
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ chunks.push(value);
+ await Bun.sleep(50);
+ }
+ expect(Buffer.concat(chunks).equals(expected)).toBe(true);
+ } finally {
+ reader.releaseLock();
+ cleanup();
+ }
+ });
+
+ test("drains all encoded bytes when the consumer waits for FFmpeg to exit", async () => {
+ const expected = await extractAudio(TEST_VIDEO_WITH_AUDIO);
+ const registration = spyOn(subprocess, "registerSubprocess");
+ const { stream, cleanup } = extractAudioStream(TEST_VIDEO_WITH_AUDIO);
+ try {
+ const proc = registration.mock.calls[0][0];
+ expect(await proc.exited).toBe(0);
+ const actual = await readStream(stream);
+ expect(actual.byteLength).toBe(expected.byteLength);
+ expect(Buffer.from(actual).equals(expected)).toBe(true);
+ } finally {
+ cleanup();
+ registration.mockRestore();
+ }
+ });
+
+ test("errors the stream when FFmpeg cannot extract audio", async () => {
+ const beforeCount = getActiveProcessCount();
+ const { stream, cleanup } = extractAudioStream(TEST_VIDEO_NO_AUDIO);
+ try {
+ await expect(readStream(stream)).rejects.toThrow("FFmpeg exited");
+ await waitForAudioOperations(beforeCount);
+ } finally {
+ cleanup();
+ }
+ });
+
+ test("errors the stream and terminates FFmpeg at the deadline", async () => {
+ const beforeCount = getActiveProcessCount();
+ const registration = spyOn(subprocess, "registerSubprocess");
+ const { stream, cleanup } = extractAudioStream(TEST_VIDEO_WITH_AUDIO, {
+ timeoutMs: 1,
+ });
+ try {
+ await expect(readStream(stream)).rejects.toThrow("timed out");
+ const proc = registration.mock.calls[0][0];
+ expect(await proc.exited).not.toBe(0);
+ await waitForAudioOperations(beforeCount);
+ } finally {
+ cleanup();
+ registration.mockRestore();
+ }
+ });
+
test("streams real mp3 data and cleans up its operation", async () => {
const beforeCount = getActiveProcessCount();
const { stream } = extractAudioStream(TEST_VIDEO_WITH_AUDIO);
diff --git a/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts b/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
index 1c7a4cc54..f59eb5b58 100644
--- a/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
@@ -687,6 +687,58 @@ describe("media routes real-world integration tests", () => {
}
}, 30_000);
+ test("uploads mux previews only after the recording output is verified", async () => {
+ const body = {
+ ...fencedMuxRequest("mux-previews"),
+ thumbnailPresignedUrl: uploadUrl("mux-previews.jpg"),
+ previewGifPresignedUrl: uploadUrl("mux-previews.gif"),
+ };
+ const response = await app.fetch(
+ mediaPostRequest("/video/mux-segments", body),
+ );
+ expect(response.status).toBe(200);
+ const { jobId } = (await response.json()) as { jobId: string };
+ try {
+ const job = await waitForTerminalJob(jobId);
+ expect(job.phase).toBe("complete");
+ expect(uploadRequests[0]).toBe("/uploads/mux-previews.mp4");
+ expect([...uploadRequests].sort()).toEqual([
+ "/uploads/mux-previews.gif",
+ "/uploads/mux-previews.jpg",
+ "/uploads/mux-previews.mp4",
+ ]);
+ expect(
+ uploadedBytes("/uploads/mux-previews.jpg").byteLength,
+ ).toBeGreaterThan(0);
+ expect(
+ uploadedBytes("/uploads/mux-previews.gif").byteLength,
+ ).toBeGreaterThan(0);
+ } finally {
+ deleteJob(jobId);
+ }
+ }, 30_000);
+
+ test("skips mux previews when the recording output upload fails", async () => {
+ uploadFailures.set("/uploads/mux-previews-failed.mp4", 403);
+ const body = {
+ ...fencedMuxRequest("mux-previews-failed"),
+ thumbnailPresignedUrl: uploadUrl("mux-previews-failed.jpg"),
+ previewGifPresignedUrl: uploadUrl("mux-previews-failed.gif"),
+ };
+ const response = await app.fetch(
+ mediaPostRequest("/video/mux-segments", body),
+ );
+ expect(response.status).toBe(200);
+ const { jobId } = (await response.json()) as { jobId: string };
+ try {
+ const job = await waitForTerminalJob(jobId);
+ expect(job.phase).toBe("error");
+ expect(uploadRequests).toEqual(["/uploads/mux-previews-failed.mp4"]);
+ } finally {
+ deleteJob(jobId);
+ }
+ }, 30_000);
+
test("runs optional audio correction on the local verified file after publication and retains success if it fails", async () => {
let observed: () => void = () => {};
const observations: Record<string, unknown>[] = [];
@@ -930,6 +982,28 @@ describe("media routes real-world integration tests", () => {
}
}, 30_000);
+ test("skips mux previews when the uploaded recording fails its remote check", async () => {
+ corruptRecordingReadback = true;
+ const body = {
+ ...fencedMuxRequest("mux-previews-corrupt"),
+ thumbnailPresignedUrl: uploadUrl("mux-previews-corrupt.jpg"),
+ previewGifPresignedUrl: uploadUrl("mux-previews-corrupt.gif"),
+ };
+ const response = await app.fetch(
+ mediaPostRequest("/video/mux-segments", body),
+ );
+ expect(response.status).toBe(200);
+ const { jobId } = (await response.json()) as { jobId: string };
+ try {
+ const job = await waitForTerminalJob(jobId);
+ expect(job.phase).toBe("error");
+ expect(job.errorCode).toBe("output-invalid");
+ expect(uploadRequests).toEqual(["/uploads/mux-previews-corrupt.mp4"]);
+ } finally {
+ deleteJob(jobId);
+ }
+ }, 30_000);
+
test("does not overwrite an existing immutable candidate", async () => {
const original = new Uint8Array([1, 2, 3]);
uploadedArtifacts.set("/uploads/fenced-existing.mp4", original);
@@ -1163,6 +1237,81 @@ describe("media routes real-world integration tests", () => {
30_000,
);
+ test("recovers a saved output after preview failure without overwriting it", async () => {
+ const thumbnail = spyOn(mediaVideo, "generateThumbnail").mockRejectedValue(
+ new Error("Thumbnail decode interrupted"),
+ );
+ const jobs: string[] = [];
+ try {
+ const body = {
+ videoId: "saved-loom-output",
+ userId: "real-process-user",
+ videoUrl: fixtureUrl(),
+ outputPresignedUrl: uploadUrl("saved-output.mp4"),
+ thumbnailPresignedUrl: uploadUrl("saved-output.jpg"),
+ inputExtension: ".mp4",
+ maxImportBytes: 10 * 1024 ** 3,
+ };
+ const first = await app.fetch(mediaPostRequest("/video/process", body));
+ const firstJobId = ((await first.json()) as { jobId: string }).jobId;
+ jobs.push(firstJobId);
+ expect((await waitForTerminalJob(firstJobId)).phase).toBe("error");
+ const original = Uint8Array.from(
+ uploadedBytes("/uploads/saved-output.mp4"),
+ );
+ const second = await app.fetch(
+ mediaPostRequest("/video/process", {
+ ...body,
+ existingOutput: {
+ url: uploadUrl("saved-output.mp4"),
+ objectIdentity: objectIdentity(original),
+ fileSize: original.byteLength,
+ },
+ }),
+ );
+ expect(second.status).toBe(200);
+ const secondJobId = ((await second.json()) as { jobId: string }).jobId;
+ jobs.push(secondJobId);
+ expect((await waitForTerminalJob(secondJobId)).phase).toBe("complete");
+ expect(uploadRequests.filter((path) => path.endsWith(".mp4"))).toEqual([
+ "/uploads/saved-output.mp4",
+ ]);
+ expect(uploadedBytes("/uploads/saved-output.mp4")).toEqual(original);
+ } finally {
+ thumbnail.mockRestore();
+ for (const jobId of jobs) deleteJob(jobId);
+ }
+ }, 30_000);
+
+ test("holds a saved output whose storage identity changed without overwriting it", async () => {
+ const original = new Uint8Array(
+ await Bun.file(TEST_VIDEO_WITH_AUDIO).arrayBuffer(),
+ );
+ uploadedArtifacts.set("/uploads/changed-output.mp4", original);
+ const response = await app.fetch(
+ mediaPostRequest("/video/process", {
+ videoId: "changed-output",
+ userId: "real-process-user",
+ videoUrl: fixtureUrl(),
+ outputPresignedUrl: uploadUrl("changed-output.mp4"),
+ maxImportBytes: 10 * 1024 ** 3,
+ existingOutput: {
+ url: uploadUrl("changed-output.mp4"),
+ objectIdentity: '"previous-identity"',
+ fileSize: original.byteLength,
+ },
+ }),
+ );
+ const { jobId } = (await response.json()) as { jobId: string };
+ try {
+ expect((await waitForTerminalJob(jobId)).phase).toBe("error");
+ expect(uploadRequests).toHaveLength(0);
+ expect(uploadedBytes("/uploads/changed-output.mp4")).toEqual(original);
+ } finally {
+ deleteJob(jobId);
+ }
+ }, 30_000);
+
test("still fails processing when the video output cannot be uploaded", async () => {
uploadFailures.set("/uploads/failed-output.mp4", 403);
const response = await app.fetch(
diff --git a/apps/media-server/src/__tests__/lib/nvenc-encoder.test.ts b/apps/media-server/src/__tests__/lib/nvenc-encoder.test.ts
new file mode 100644
index 000000000..5892b6181
--- /dev/null
+++ b/apps/media-server/src/__tests__/lib/nvenc-encoder.test.ts
@@ -0,0 +1,790 @@
+import { describe, expect, test } from "bun:test";
+import { join } from "node:path";
+import { FfmpegExitError, ffmpegErrorLines } from "../../lib/ffmpeg-errors";
+import type { VideoMetadata } from "../../lib/job-manager";
+import { decodeThreadsFromEnv } from "../../lib/media-common";
+import {
+ buildTranscodeEditArgs,
+ renderEditedVideo,
+} from "../../lib/media-edit";
+import { probeVideo } from "../../lib/media-probe";
+import {
+ buildProcessVideoArgs,
+ type ProcessVideoArgsInput,
+ pickMobileSafeH264Level,
+ processVideo,
+} from "../../lib/media-video";
+import {
+ buildNvencEncodeArgs,
+ canDecodeOnGpu,
+ encodeAttemptsBudgetMs,
+ fitWithinEven,
+ getVideoEncoderStatus,
+ type NvencOptions,
+ nvencCq,
+ nvencFallbackReason,
+ nvencOptionsFromEnv,
+ recordNvencAttempt,
+ selectedVideoEncoder,
+} from "../../lib/nvenc-encoder";
+
+const enabled = { CAP_MEDIA_VIDEO_ENCODER: "nvenc" };
+const defaults: NvencOptions = {
+ preset: "p1",
+ cqOffset: 8,
+ lookahead: 0,
+ spatialAq: true,
+ temporalAq: false,
+ gpuDecodeCodecs: ["hevc", "av1"],
+};
+const opts: ProcessVideoArgsInput["opts"] = {
+ maxWidth: 1920,
+ maxHeight: 1080,
+ preset: "medium",
+ crf: 23,
+ audioBitrate: "128k",
+};
+const nvencVideoArgs = [
+ "-c:v",
+ "h264_nvenc",
+ "-preset",
+ "p1",
+ "-tune",
+ "hq",
+ "-rc",
+ "vbr",
+ "-cq",
+ "31",
+ "-b:v",
+ "0",
+ "-spatial-aq",
+ "1",
+ "-profile:v",
+ "high",
+ "-g",
+ "250",
+];
+const gpuScale = "scale_cuda=format=nv12";
+const cpuScale =
+ "scale='min(1920,iw)':'min(1080,ih)':force_original_aspect_ratio=decrease,scale=trunc(iw/2)*2:trunc(ih/2)*2";
+
+function metadata(
+ width: number,
+ height: number,
+ videoCodec: string,
+): VideoMetadata {
+ return {
+ duration: 10,
+ width,
+ height,
+ fps: 30,
+ videoCodec,
+ audioCodec: "aac",
+ audioChannels: 2,
+ sampleRate: 48_000,
+ bitrate: 0,
+ fileSize: 0,
+ };
+}
+
+function processArgs(
+ source: VideoMetadata,
+ overrides: Partial<ProcessVideoArgsInput> = {},
+): ProcessVideoArgsInput {
+ return {
+ inputPath: "in.mp4",
+ outputPath: "out.mp4",
+ videoCodec: source.videoCodec,
+ hasAudio: true,
+ opts,
+ targetH264Level: pickMobileSafeH264Level(source, opts).ffmpegValue,
+ normalizeH264Level: false,
+ videoTranscode: true,
+ audioTranscode: false,
+ extraInputArgs: [],
+ extraOutputArgs: [],
+ decodeThreads: "2",
+ sourceWidth: source.width,
+ sourceHeight: source.height,
+ ...overrides,
+ };
+}
+
+const outputTail = [
+ "-c:a",
+ "copy",
+ "-movflags",
+ "+faststart",
+ "-progress",
+ "pipe:2",
+ "-y",
+ "out.mp4",
+];
+
+describe("nvenc encoder selection", () => {
+ test("is off unless CAP_MEDIA_VIDEO_ENCODER is nvenc", () => {
+ expect(selectedVideoEncoder({})).toBe("libx264");
+ expect(selectedVideoEncoder({ CAP_MEDIA_VIDEO_ENCODER: "x264" })).toBe(
+ "libx264",
+ );
+ expect(selectedVideoEncoder({ CAP_MEDIA_VIDEO_ENCODER: "capcodec" })).toBe(
+ "capcodec",
+ );
+ expect(selectedVideoEncoder(enabled)).toBe("nvenc");
+ expect(nvencOptionsFromEnv({})).toBeNull();
+ expect(
+ nvencOptionsFromEnv({ CAP_MEDIA_VIDEO_ENCODER: "capcodec" }),
+ ).toBeNull();
+ expect(getVideoEncoderStatus({})).toEqual({
+ active: "libx264",
+ nvenc: null,
+ });
+ expect(getVideoEncoderStatus(enabled, false)).toEqual({
+ active: "nvenc",
+ nvenc: null,
+ });
+ });
+
+ test("uses p1, cq = crf + 8, no lookahead and spatial AQ by default", () => {
+ expect(nvencOptionsFromEnv(enabled)).toEqual(defaults);
+ });
+
+ test("reads tuning overrides from the environment", () => {
+ expect(
+ nvencOptionsFromEnv({
+ ...enabled,
+ CAP_NVENC_PRESET: " P7 ",
+ CAP_NVENC_CQ_OFFSET: "-3",
+ CAP_NVENC_LOOKAHEAD: "0",
+ CAP_NVENC_SPATIAL_AQ: "off",
+ CAP_NVENC_TEMPORAL_AQ: "1",
+ }),
+ ).toEqual({
+ preset: "p7",
+ cqOffset: -3,
+ lookahead: 0,
+ spatialAq: false,
+ temporalAq: true,
+ gpuDecodeCodecs: ["hevc", "av1"],
+ });
+ expect(
+ nvencOptionsFromEnv({
+ ...enabled,
+ CAP_NVENC_GPU_DECODE_CODECS: "h264, HEVC,vp8,nope",
+ })?.gpuDecodeCodecs,
+ ).toEqual(["h264", "hevc"]);
+ });
+
+ test("ignores invalid tuning overrides", () => {
+ expect(
+ nvencOptionsFromEnv({
+ ...enabled,
+ CAP_NVENC_PRESET: "slow",
+ CAP_NVENC_CQ_OFFSET: "2.5",
+ CAP_NVENC_LOOKAHEAD: "64",
+ CAP_NVENC_SPATIAL_AQ: "maybe",
+ CAP_NVENC_TEMPORAL_AQ: "",
+ }),
+ ).toEqual(defaults);
+ });
+
+ test("maps crf to a bounded cq", () => {
+ expect(nvencCq(23, 2)).toBe("25");
+ expect(nvencCq(18, 2)).toBe("20");
+ expect(nvencCq(20.5, 0)).toBe("20.5");
+ expect(nvencCq(50, 5)).toBe("51");
+ expect(nvencCq(0, -4)).toBe("1");
+ expect(nvencCq(Number.NaN, 2)).toBe("25");
+ });
+
+ test("builds constant-quality VBR encoder args", () => {
+ expect(buildNvencEncodeArgs(defaults, 23)).toEqual(nvencVideoArgs);
+ expect(
+ buildNvencEncodeArgs(
+ { ...defaults, lookahead: 0, spatialAq: false, temporalAq: true },
+ 18,
+ ),
+ ).toEqual([
+ "-c:v",
+ "h264_nvenc",
+ "-preset",
+ "p1",
+ "-tune",
+ "hq",
+ "-rc",
+ "vbr",
+ "-cq",
+ "26",
+ "-b:v",
+ "0",
+ "-temporal-aq",
+ "1",
+ "-profile:v",
+ "high",
+ "-g",
+ "250",
+ ]);
+ });
+
+ test("fits the GPU resize to the CPU path's box and even dimensions", () => {
+ expect(fitWithinEven(3840, 2160, 1920, 1080)).toEqual({
+ width: 1920,
+ height: 1080,
+ });
+ expect(fitWithinEven(3024, 1964, 1920, 1080)).toEqual({
+ width: 1662,
+ height: 1080,
+ });
+ expect(fitWithinEven(1079, 1919, 1920, 1080)).toEqual({
+ width: 606,
+ height: 1080,
+ });
+ expect(fitWithinEven(1281, 721, 1920, 1080)).toEqual({
+ width: 1280,
+ height: 720,
+ });
+ });
+
+ test("decodes on the GPU only for configured, unrotated codecs", () => {
+ for (const codec of ["hevc", "av1"]) {
+ expect(canDecodeOnGpu(codec, 0)).toBe(true);
+ }
+ expect(canDecodeOnGpu("h264", 0)).toBe(false);
+ expect(canDecodeOnGpu("h264", 0, ["h264"])).toBe(true);
+ expect(canDecodeOnGpu("vp8", 0)).toBe(false);
+ expect(canDecodeOnGpu("prores", 0)).toBe(false);
+ expect(canDecodeOnGpu("hevc", 90)).toBe(false);
+ expect(canDecodeOnGpu("h264", null)).toBe(false);
+ });
+});
+
+describe("nvenc fallback", () => {
+ test("reserves job time for the libx264 retry only when nvenc is on", () => {
+ expect(encodeAttemptsBudgetMs(60_000, {})).toBe(60_000);
+ expect(encodeAttemptsBudgetMs(60_000, enabled)).toBe(120_000);
+ });
+
+ test("falls back to libx264 on any failure that was not a cancellation", () => {
+ expect(
+ nvencFallbackReason(
+ new Error("FFmpeg exited with code 1. Last stderr: No device"),
+ false,
+ ),
+ ).toBe("FFmpeg exited with code 1. Last stderr: No device");
+ expect(nvencFallbackReason("boom", false)).toBe("boom");
+ expect(
+ nvencFallbackReason(
+ new Error(
+ "https://bucket.s3.amazonaws.com/u/v/raw.mp4?X-Amz-Signature=abc: Server returned 403",
+ ),
+ false,
+ ),
+ ).toBe("<url> Server returned 403");
+ expect(nvencFallbackReason(new Error(""), false)).toBe("unknown error");
+ expect(
+ nvencFallbackReason(new Error("x".repeat(2000)), false),
+ ).toHaveLength(500);
+ expect(nvencFallbackReason(new Error("killed"), true)).toBeNull();
+ });
+
+ test("leads with the encoder error printed before the progress tail", () => {
+ const progress = [
+ "frame=0",
+ "fps=0.00",
+ "stream_0_0_q=0.0",
+ "bitrate=N/A",
+ "total_size=0",
+ "out_time_us=N/A",
+ "out_time_ms=N/A",
+ "out_time=N/A",
+ "dup_frames=0",
+ "drop_frames=0",
+ "speed=N/A",
+ "progress=end",
+ ];
+ const stderr = [
+ "Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'https://bucket.s3.us-east-1.amazonaws.com/user-123/video-456/raw.mp4?X-Amz-Credential=AKIAEXAMPLE%2F20261023&X-Amz-Signature=deadbeef':",
+ "[h264_nvenc @ 0x5581c2a0] InitializeEncoder failed: invalid param (8): Invalid Level.",
+ "[vost#0:0/h264_nvenc @ 0x5581c3b0] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height.",
+ "Error opening output file /tmp/cap-media-server/instance-1/user-123-out.mp4.",
+ ...progress,
+ "Conversion failed!",
+ ];
+ const reason = nvencFallbackReason(
+ new FfmpegExitError(
+ `FFmpeg exited with code 1. Last stderr: ${stderr.slice(-10).join(" | ")}`,
+ ffmpegErrorLines(stderr.join("\n")),
+ ),
+ false,
+ );
+ expect(reason).toStartWith(
+ "[h264_nvenc @ 0x5581c2a0] InitializeEncoder failed: invalid param (8): Invalid Level.",
+ );
+ expect(reason).toContain("Error while opening encoder");
+ expect(reason).toContain("Error opening output file <path>");
+ expect(reason).toContain("Conversion failed!");
+ expect(reason).not.toContain("amazonaws");
+ expect(reason).not.toContain("X-Amz");
+ expect(reason).not.toContain("user-123");
+ expect(reason?.length).toBeLessThanOrEqual(500);
+ expect(
+ ffmpegErrorLines(
+ "Reconfiguring filter graph because video parameters changed to yuv420p(tv), 1280x720\r\nprogress=continue\nerror_rate=0\n",
+ ),
+ ).toEqual([
+ "Reconfiguring filter graph because video parameters changed to yuv420p(tv), 1280x720",
+ ]);
+ expect(
+ ffmpegErrorLines(
+ [
+ "[https @ 0x1] HTTP error 403 Forbidden",
+ `[in#0 @ 0x2] Error opening input: Server returned 403 Forbidden (access denied)`,
+ ].join("\n"),
+ ),
+ ).toHaveLength(2);
+ expect(
+ nvencFallbackReason(
+ new FfmpegExitError(
+ "FFmpeg exited with code 1. Last stderr: Conversion failed!",
+ [
+ "Error opening input file https://bucket.example/raw.mp4?X-Amz-Signature=secret",
+ ],
+ ),
+ false,
+ ),
+ ).toBe(
+ "Error opening input file <url> || FFmpeg exited with code 1. Last stderr: Conversion failed!",
+ );
+ });
+
+ test("reports the last attempt through the encoder status", () => {
+ const before = getVideoEncoderStatus(enabled).nvenc;
+ recordNvencAttempt("process", "No NVENC capable devices found");
+ const fellBack = getVideoEncoderStatus(enabled).nvenc;
+ expect(fellBack?.attempts).toBe((before?.attempts ?? 0) + 1);
+ expect(fellBack?.fallbacks).toBe((before?.fallbacks ?? 0) + 1);
+ expect(fellBack?.lastAttempt).toMatchObject({
+ job: "process",
+ fellBack: true,
+ reason: "No NVENC capable devices found",
+ });
+ recordNvencAttempt("edit");
+ const succeeded = getVideoEncoderStatus(enabled).nvenc;
+ expect(succeeded?.fallbacks).toBe(fellBack?.fallbacks);
+ expect(succeeded?.lastAttempt).toMatchObject({
+ job: "edit",
+ fellBack: false,
+ reason: null,
+ });
+ });
+});
+
+describe("process video args", () => {
+ test("keeps the default libx264 args unchanged", () => {
+ const source = metadata(3024, 1964, "h264");
+ expect(buildProcessVideoArgs(processArgs(source))).toEqual([
+ "ffmpeg",
+ "-threads",
+ "2",
+ "-i",
+ "in.mp4",
+ "-c:v",
+ "libx264",
+ "-preset",
+ "medium",
+ "-crf",
+ "23",
+ "-vf",
+ cpuScale,
+ "-pix_fmt",
+ "yuv420p",
+ "-level:v",
+ "4.2",
+ ...outputTail,
+ ]);
+ });
+
+ test("runs a 3024x1964 H.264 source fully on the GPU", () => {
+ const source = metadata(3024, 1964, "h264");
+ expect(
+ buildProcessVideoArgs(
+ processArgs(source, {
+ nvenc: { options: defaults, gpuDecode: true },
+ }),
+ ),
+ ).toEqual([
+ "ffmpeg",
+ "-threads",
+ "2",
+ "-hwaccel",
+ "cuda",
+ "-hwaccel_output_format",
+ "cuda",
+ "-c:v",
+ "h264_cuvid",
+ "-resize",
+ "1662x1080",
+ "-i",
+ "in.mp4",
+ ...nvencVideoArgs,
+ "-vf",
+ gpuScale,
+ "-bsf:v",
+ "h264_metadata=level=4.2",
+ ...outputTail,
+ ]);
+ });
+
+ test("runs a 4K HEVC source fully on the GPU", () => {
+ const source = metadata(3840, 2160, "hevc");
+ expect(
+ buildProcessVideoArgs(
+ processArgs(source, {
+ inputPath: "in.mov",
+ audioTranscode: true,
+ nvenc: { options: defaults, gpuDecode: true },
+ }),
+ ),
+ ).toEqual([
+ "ffmpeg",
+ "-threads",
+ "2",
+ "-hwaccel",
+ "cuda",
+ "-hwaccel_output_format",
+ "cuda",
+ "-c:v",
+ "hevc_cuvid",
+ "-resize",
+ "1920x1080",
+ "-i",
+ "in.mov",
+ ...nvencVideoArgs,
+ "-vf",
+ gpuScale,
+ "-bsf:v",
+ "h264_metadata=level=4.2",
+ "-c:a",
+ "aac",
+ "-b:a",
+ "128k",
+ "-movflags",
+ "+faststart",
+ "-progress",
+ "pipe:2",
+ "-y",
+ "out.mp4",
+ ]);
+ });
+
+ test("keeps -bf 0 for a 1080p VP9 webm", () => {
+ const source = metadata(1920, 1080, "vp9");
+ expect(
+ buildProcessVideoArgs(
+ processArgs(source, {
+ inputPath: "in.webm",
+ audioTranscode: true,
+ nvenc: { options: defaults, gpuDecode: true },
+ }),
+ ),
+ ).toEqual([
+ "ffmpeg",
+ "-threads",
+ "2",
+ "-hwaccel",
+ "cuda",
+ "-hwaccel_output_format",
+ "cuda",
+ "-c:v",
+ "vp9_cuvid",
+ "-i",
+ "in.webm",
+ ...nvencVideoArgs,
+ "-vf",
+ gpuScale,
+ "-bsf:v",
+ "h264_metadata=level=4.2",
+ "-bf",
+ "0",
+ "-c:a",
+ "aac",
+ "-b:a",
+ "128k",
+ "-movflags",
+ "+faststart",
+ "-progress",
+ "pipe:2",
+ "-y",
+ "out.mp4",
+ ]);
+ });
+
+ test("scales on the CPU before NVENC when the GPU cannot decode", () => {
+ const source = metadata(1280, 720, "vp8");
+ expect(
+ buildProcessVideoArgs(
+ processArgs(source, {
+ inputPath: "in.webm",
+ decodeThreads: "4",
+ extraInputArgs: ["-fflags", "+genpts"],
+ extraOutputArgs: ["-max_muxing_queue_size", "4096"],
+ nvenc: { options: defaults, gpuDecode: false },
+ }),
+ ),
+ ).toEqual([
+ "ffmpeg",
+ "-threads",
+ "4",
+ "-fflags",
+ "+genpts",
+ "-reinit_filter",
+ "0",
+ "-i",
+ "in.webm",
+ ...nvencVideoArgs,
+ "-vf",
+ cpuScale,
+ "-pix_fmt",
+ "yuv420p",
+ "-bsf:v",
+ "h264_metadata=level=4.2",
+ "-bf",
+ "0",
+ "-c:a",
+ "copy",
+ "-movflags",
+ "+faststart",
+ "-max_muxing_queue_size",
+ "4096",
+ "-progress",
+ "pipe:2",
+ "-y",
+ "out.mp4",
+ ]);
+ });
+
+ test("never touches level normalization or stream copies", () => {
+ const source = metadata(1920, 1080, "h264");
+ const nvenc = { options: defaults, gpuDecode: false };
+ expect(
+ buildProcessVideoArgs(
+ processArgs(source, { normalizeH264Level: true, nvenc }),
+ ),
+ ).toEqual([
+ "ffmpeg",
+ "-threads",
+ "2",
+ "-i",
+ "in.mp4",
+ "-c:v",
+ "copy",
+ "-bsf:v",
+ "h264_metadata=level=4.2",
+ ...outputTail,
+ ]);
+ expect(
+ buildProcessVideoArgs(
+ processArgs(source, { videoTranscode: false, hasAudio: false }),
+ ),
+ ).toEqual([
+ "ffmpeg",
+ "-threads",
+ "2",
+ "-i",
+ "in.mp4",
+ "-c:v",
+ "copy",
+ "-an",
+ ...outputTail.slice(2),
+ ]);
+ });
+});
+
+describe("edit re-encode args", () => {
+ const range = [{ start: 1, end: 3 }];
+
+ test("swaps libx264 for NVENC and keeps the rest of the edit graph", () => {
+ const libx264 = buildTranscodeEditArgs("in.mp4", range, "out.mp4", true);
+ const nvenc = buildTranscodeEditArgs(
+ "in.mp4",
+ range,
+ "out.mp4",
+ true,
+ 30,
+ defaults,
+ );
+ expect(libx264.slice(libx264.indexOf("[v]") + 1)).toEqual([
+ "-c:v",
+ "libx264",
+ "-preset",
+ "fast",
+ "-crf",
+ "18",
+ "-pix_fmt",
+ "yuv420p",
+ "-enc_time_base:v",
+ "1/30",
+ "-map",
+ "[a]",
+ "-c:a",
+ "aac",
+ "-b:a",
+ "160k",
+ "-movflags",
+ "+faststart",
+ "out.mp4",
+ ]);
+ expect(nvenc).toEqual([
+ "ffmpeg",
+ "-hide_banner",
+ "-y",
+ "-ss",
+ "1.000",
+ "-t",
+ "2.000",
+ "-i",
+ "in.mp4",
+ "-filter_complex",
+ "[0:v:0]fps=30,setpts=PTS-STARTPTS[v0];[0:a:0]asetpts=PTS-STARTPTS[a0];[v0][a0]concat=n=1:v=1:a=1[v][a]",
+ "-map",
+ "[v]",
+ "-c:v",
+ "h264_nvenc",
+ "-preset",
+ "p1",
+ "-tune",
+ "hq",
+ "-rc",
+ "vbr",
+ "-cq",
+ "26",
+ "-b:v",
+ "0",
+ "-spatial-aq",
+ "1",
+ "-profile:v",
+ "high",
+ "-g",
+ "250",
+ "-pix_fmt",
+ "yuv420p",
+ "-enc_time_base:v",
+ "1/30",
+ "-map",
+ "[a]",
+ "-c:a",
+ "aac",
+ "-b:a",
+ "160k",
+ "-movflags",
+ "+faststart",
+ "out.mp4",
+ ]);
+ });
+});
+
+describe("decode thread overrides", () => {
+ test("keep today's defaults unless set to a valid count", () => {
+ const name = "MEDIA_SERVER_VERIFY_DECODE_THREADS";
+ expect(decodeThreadsFromEnv(name, 1, {})).toBe("1");
+ expect(decodeThreadsFromEnv(name, 1, { [name]: " " })).toBe("1");
+ expect(decodeThreadsFromEnv(name, 1, { [name]: "4" })).toBe("4");
+ expect(decodeThreadsFromEnv(name, 2, { [name]: "0" })).toBe("0");
+ expect(decodeThreadsFromEnv(name, 2, { [name]: "-1" })).toBe("2");
+ expect(decodeThreadsFromEnv(name, 2, { [name]: "1.5" })).toBe("2");
+ expect(decodeThreadsFromEnv(name, 2, { [name]: "65" })).toBe("2");
+ expect(decodeThreadsFromEnv(name, 2, { [name]: "auto" })).toBe("2");
+ });
+});
+
+describe("nvenc jobs on hosts without an NVENC device", () => {
+ const fixture = join(
+ import.meta.dir,
+ "..",
+ "fixtures",
+ "test-with-audio.mp4",
+ );
+
+ async function withNvencEnv<T>(run: () => Promise<T>): Promise<T> {
+ const previous = process.env.CAP_MEDIA_VIDEO_ENCODER;
+ process.env.CAP_MEDIA_VIDEO_ENCODER = "nvenc";
+ try {
+ return await run();
+ } finally {
+ if (previous === undefined) delete process.env.CAP_MEDIA_VIDEO_ENCODER;
+ else process.env.CAP_MEDIA_VIDEO_ENCODER = previous;
+ }
+ }
+
+ function expectRecordedAttempt(job: "process" | "edit", attempts: number) {
+ const status = getVideoEncoderStatus(enabled).nvenc;
+ expect(status?.attempts).toBe(attempts + 1);
+ expect(status?.lastAttempt?.job).toBe(job);
+ if (status?.lastAttempt?.fellBack) {
+ expect(status.lastAttempt.reason).toBeString();
+ }
+ }
+
+ test("does not start the encoder once the job is cancelled", async () => {
+ const metadata = await probeVideo(`file://${fixture}`);
+ const attempts = getVideoEncoderStatus(enabled).nvenc?.attempts ?? 0;
+ const controller = new AbortController();
+ controller.abort();
+ await expect(
+ withNvencEnv(() =>
+ processVideo(
+ fixture,
+ metadata,
+ { maxWidth: 160, maxHeight: 120 },
+ undefined,
+ controller.signal,
+ ),
+ ),
+ ).rejects.toThrow("Video processing was cancelled");
+ expect(getVideoEncoderStatus(enabled).nvenc?.attempts ?? 0).toBe(attempts);
+ });
+
+ test("still produces the transcode", async () => {
+ const metadata = await probeVideo(`file://${fixture}`);
+ const attempts = getVideoEncoderStatus(enabled).nvenc?.attempts ?? 0;
+ const output = await withNvencEnv(() =>
+ processVideo(fixture, metadata, { maxWidth: 160, maxHeight: 120 }),
+ );
+ try {
+ const result = await probeVideo(`file://${output.path}`);
+ expect(result.videoCodec).toBe("h264");
+ expect(result.width).toBe(160);
+ expect(result.height).toBe(120);
+ expect(result.audioCodec).toBe("aac");
+ expectRecordedAttempt("process", attempts);
+ const lastAttempt = getVideoEncoderStatus(enabled).nvenc?.lastAttempt;
+ if (lastAttempt?.fellBack) {
+ const [cause] = lastAttempt.reason?.split(" || ") ?? [];
+ expect(cause).not.toStartWith("FFmpeg exited");
+ expect(cause).not.toMatch(/out_time=|total_size=|progress=/);
+ }
+ } finally {
+ await output.cleanup();
+ }
+ });
+
+ test("still renders the edit", async () => {
+ const metadata = await probeVideo(`file://${fixture}`);
+ const attempts = getVideoEncoderStatus(enabled).nvenc?.attempts ?? 0;
+ const output = await withNvencEnv(() =>
+ renderEditedVideo({
+ inputPath: fixture,
+ keepRanges: [{ start: 0, end: 0.5 }],
+ metadata,
+ }),
+ );
+ try {
+ const result = await probeVideo(`file://${output.path}`);
+ expect(result.videoCodec).toBe("h264");
+ expect(result.audioCodec).toBe("aac");
+ expectRecordedAttempt("edit", attempts);
+ } finally {
+ await output.cleanup();
+ }
+ });
+});
diff --git a/apps/media-server/src/__tests__/lib/parallel-download.test.ts b/apps/media-server/src/__tests__/lib/parallel-download.test.ts
new file mode 100644
index 000000000..ce8290f87
--- /dev/null
+++ b/apps/media-server/src/__tests__/lib/parallel-download.test.ts
@@ -0,0 +1,193 @@
+import { afterAll, beforeAll, describe, expect, test } from "bun:test";
+import { randomBytes } from "node:crypto";
+import { mkdtemp, readFile, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import {
+ downloadUrlToPathParallel,
+ parallelDownloadOptionsFromEnv,
+ planRanges,
+} from "../../lib/parallel-download";
+
+const MB = 1024 * 1024;
+const payload = randomBytes(5 * MB + 12345);
+let dir: string;
+let server: ReturnType<typeof Bun.serve>;
+let rangeRequests = 0;
+
+beforeAll(async () => {
+ dir = await mkdtemp(join(tmpdir(), "parallel-download-"));
+ server = Bun.serve({
+ port: 0,
+ fetch(req) {
+ const url = new URL(req.url);
+ const range = req.headers.get("range")?.match(/^bytes=(\d+)-(\d+)$/);
+ if (url.pathname === "/plain" || !range) {
+ return new Response(payload, { headers: { ETag: '"v1"' } });
+ }
+ rangeRequests++;
+ if (url.pathname === "/no-total" || url.pathname === "/capped") {
+ const end =
+ url.pathname === "/capped" ? 64 * 1024 - 1 : Number(range[2]);
+ return new Response(payload.subarray(0, end + 1), {
+ status: 206,
+ headers: {
+ "Content-Range":
+ url.pathname === "/capped"
+ ? `bytes 0-${end}/${payload.length}`
+ : `bytes 0-${end}/*`,
+ },
+ });
+ }
+ if (url.pathname === "/huge") {
+ return new Response(payload.subarray(0, MB), {
+ status: 206,
+ headers: {
+ "Content-Range": `bytes 0-${MB - 1}/${Number.MAX_SAFE_INTEGER}`,
+ },
+ });
+ }
+ if (url.pathname === "/changed" && Number(range[1]) > 0) {
+ return new Response("precondition", { status: 412 });
+ }
+ const start = Number(range[1]);
+ const end = Math.min(Number(range[2]), payload.length - 1);
+ const body =
+ url.pathname === "/short" && start > 0
+ ? payload.subarray(start, end)
+ : payload.subarray(start, end + 1);
+ return new Response(body, {
+ status: 206,
+ headers: {
+ ETag: '"v1"',
+ "Content-Range": `bytes ${start}-${end}/${payload.length}`,
+ },
+ });
+ },
+ });
+});
+
+afterAll(async () => {
+ server.stop(true);
+ await rm(dir, { recursive: true, force: true });
+});
+
+describe("parallel download", () => {
+ test("env defaults keep the single-stream path", () => {
+ expect(parallelDownloadOptionsFromEnv({})).toEqual({
+ concurrency: 1,
+ partBytes: 16 * MB,
+ });
+ expect(
+ parallelDownloadOptionsFromEnv({
+ MEDIA_SERVER_DOWNLOAD_CONCURRENCY: "8",
+ MEDIA_SERVER_DOWNLOAD_PART_MB: "32",
+ }),
+ ).toEqual({ concurrency: 8, partBytes: 32 * MB });
+ expect(
+ parallelDownloadOptionsFromEnv({
+ MEDIA_SERVER_DOWNLOAD_CONCURRENCY: "500",
+ }).concurrency,
+ ).toBe(32);
+ });
+
+ test("plans contiguous ranges after the first part", () => {
+ const ranges = planRanges(10, 4, 3);
+ expect(ranges.count).toBe(2);
+ expect([ranges.at(0), ranges.at(1)]).toEqual([
+ { start: 4, end: 6 },
+ { start: 7, end: 9 },
+ ]);
+ expect(planRanges(4, 4, 3).count).toBe(0);
+ expect(planRanges(Number.MAX_SAFE_INTEGER, MB, MB).count).toBeGreaterThan(
+ 1e9,
+ );
+ });
+
+ test("reassembles ranged parts byte for byte", async () => {
+ const path = join(dir, "ranged.bin");
+ const result = await downloadUrlToPathParallel(
+ `http://localhost:${server.port}/file`,
+ path,
+ { concurrency: 4, partBytes: MB },
+ );
+ expect(result.ranged).toBe(true);
+ expect(result.parts).toBe(6);
+ expect(Buffer.compare(await readFile(path), payload)).toBe(0);
+ });
+
+ test("falls back to one stream when ranges are unsupported", async () => {
+ const path = join(dir, "plain.bin");
+ const result = await downloadUrlToPathParallel(
+ `http://localhost:${server.port}/plain`,
+ path,
+ { concurrency: 4, partBytes: MB },
+ );
+ expect(result).toEqual({ bytes: payload.length, parts: 1, ranged: false });
+ expect(Buffer.compare(await readFile(path), payload)).toBe(0);
+ });
+
+ test("fails when the object changes mid-download", async () => {
+ await expect(
+ downloadUrlToPathParallel(
+ `http://localhost:${server.port}/changed`,
+ join(dir, "changed.bin"),
+ { concurrency: 4, partBytes: MB },
+ ),
+ ).rejects.toThrow("Ranged download part failed: 412");
+ });
+
+ test.each(["no-total", "capped"])(
+ "restarts as one stream when the first range can't be used (%s)",
+ async (name) => {
+ const path = join(dir, `${name}.bin`);
+ const result = await downloadUrlToPathParallel(
+ `http://localhost:${server.port}/${name}`,
+ path,
+ { concurrency: 4, partBytes: MB },
+ );
+ expect(result).toEqual({
+ bytes: payload.length,
+ parts: 1,
+ ranged: false,
+ });
+ expect(Buffer.compare(await readFile(path), payload)).toBe(0);
+ },
+ );
+
+ test("rejects an absurd Content-Range total before planning parts", async () => {
+ const before = rangeRequests;
+ await expect(
+ downloadUrlToPathParallel(
+ `http://localhost:${server.port}/huge`,
+ join(dir, "huge.bin"),
+ { concurrency: 4, partBytes: MB },
+ ),
+ ).rejects.toThrow("invalid size");
+ expect(rangeRequests).toBe(before + 1);
+ });
+
+ test("fails when a part comes back short", async () => {
+ await expect(
+ downloadUrlToPathParallel(
+ `http://localhost:${server.port}/short`,
+ join(dir, "short.bin"),
+ { concurrency: 4, partBytes: MB },
+ ),
+ ).rejects.toThrow("expected");
+ });
+
+ test("stops on abort", async () => {
+ const controller = new AbortController();
+ controller.abort();
+ const before = rangeRequests;
+ await expect(
+ downloadUrlToPathParallel(
+ `http://localhost:${server.port}/file`,
+ join(dir, "aborted.bin"),
+ { concurrency: 4, partBytes: MB, signal: controller.signal },
+ ),
+ ).rejects.toThrow();
+ expect(rangeRequests).toBe(before);
+ });
+});
diff --git a/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts b/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts
index 0d11a2fb7..8ce747bb6 100644
--- a/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts
@@ -21,6 +21,7 @@ import {
import {
RecordingTimingError,
readRecordingVideoTiming,
+ timingMetadataBudget,
} from "../../lib/recording-timing";
import {
inspectRecordingSources,
@@ -960,6 +961,26 @@ describe("complete recording decode", () => {
);
});
+ test("skips decoded content hashes only when nothing compares them", async () => {
+ const options = { expectedDuration: 5, requireAudio: true };
+ const hashed = await verifyRecording(silent, options);
+ const unhashed = await verifyRecording(silent, {
+ ...options,
+ hashDecodedContent: false,
+ });
+ expect(unhashed.integrity).toBeUndefined();
+ expect(unhashed.video).toEqual(hashed.video);
+ expect(unhashed.audio).toEqual(hashed.audio);
+ const evidence = await inspectRecordingSources(silent, silent);
+ const preserved = await verifyRecording(silent, {
+ requireAudio: false,
+ hashDecodedContent: false,
+ sourceEvidence: evidence,
+ });
+ expect(preserved.sourcePreserved).toBe(true);
+ expect(preserved.integrity).toEqual(evidence.integrity);
+ });
+
test("accepts legacy video without audio only when audio was not requested", async () => {
const input = join(FIXTURES, "test-no-audio.mp4");
const evidence = await verifyRecording(input, {
@@ -1944,6 +1965,25 @@ function expectExited(pid: number) {
expect(() => process.kill(pid, 0)).toThrow();
}
+describe("recording timing metadata budget", () => {
+ const mb = 1024 * 1024;
+ test("keeps the 64 MB budget for normal recordings", () => {
+ expect(timingMetadataBudget(0)).toBe(64 * mb);
+ expect(timingMetadataBudget(300 * mb)).toBe(64 * mb);
+ expect(timingMetadataBudget(4096 * mb)).toBe(64 * mb);
+ });
+
+ test("scales with very long recordings up to 1 GB", () => {
+ expect(timingMetadataBudget(20 * 1024 * mb)).toBe(320 * mb);
+ expect(timingMetadataBudget(200 * 1024 * mb)).toBe(1024 * mb);
+ });
+
+ test("ignores an invalid size", () => {
+ expect(timingMetadataBudget(Number.NaN)).toBe(64 * mb);
+ expect(timingMetadataBudget(-1)).toBe(64 * mb);
+ });
+});
+
describe("recording timing metadata lifetime", () => {
test.each(["cancel", "timeout"])(
"joins a stalled raw terminal-duration read after %s",
@@ -2424,6 +2464,585 @@ function objectResponse(
return new Response(bytes, { headers });
}
+describe("chunked packet-proven verification", () => {
+ async function segmentedRecording(
+ name: string,
+ gop: number,
+ frames = 120,
+ segmentSeconds = 0.5,
+ ) {
+ const fragments = await mkdtemp(join(directory, `${name}-`));
+ await run([
+ "ffmpeg",
+ "-v",
+ "error",
+ "-f",
+ "lavfi",
+ "-i",
+ "testsrc2=size=320x180:rate=30",
+ "-frames:v",
+ String(frames),
+ "-c:v",
+ "libx264",
+ "-preset",
+ "ultrafast",
+ "-g",
+ String(gop),
+ "-keyint_min",
+ String(gop),
+ "-sc_threshold",
+ "0",
+ "-pix_fmt",
+ "yuv420p",
+ "-f",
+ "dash",
+ "-seg_duration",
+ String(segmentSeconds),
+ "-use_template",
+ "1",
+ "-use_timeline",
+ "1",
+ "-init_seg_name",
+ "init.mp4",
+ "-media_seg_name",
+ "fragment-$Number%05d$.m4s",
+ join(fragments, "manifest.mpd"),
+ ]);
+ const init = join(fragments, "init.mp4");
+ const segments = (await readdir(fragments))
+ .filter((file) => /^fragment-\d+\.m4s$/.test(file))
+ .sort()
+ .map((file) => join(fragments, file));
+ const combined = join(directory, `${name}-combined.mp4`);
+ await writeFile(
+ combined,
+ Buffer.concat(
+ await Promise.all([init, ...segments].map((path) => readFile(path))),
+ ),
+ );
+ const output = join(directory, `${name}-output.mp4`);
+ await muxMediaTracksToMp4(combined, silent, output);
+ return { init, segments, combined, output };
+ }
+
+ const previous = process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS;
+ afterAll(() => {
+ if (previous === undefined)
+ delete process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS;
+ else process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS = previous;
+ });
+
+ async function both(
+ recording: Awaited<ReturnType<typeof segmentedRecording>>,
+ ) {
+ delete process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS;
+ const single = await verifyRemuxedRecording(
+ recording.combined,
+ silent,
+ recording.output,
+ { requireAudio: false },
+ );
+ process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS = "3";
+ const warn = spyOn(console, "warn");
+ try {
+ const chunked = await verifyRemuxedRecording(
+ recording.combined,
+ silent,
+ recording.output,
+ { requireAudio: false },
+ { init: recording.init, segments: recording.segments },
+ );
+ const fallbacks = warn.mock.calls.filter((call) =>
+ String(call[0]).includes("recording_chunked_decode_fallback"),
+ );
+ return { single, chunked, fallbacks };
+ } finally {
+ warn.mockRestore();
+ delete process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS;
+ }
+ }
+
+ test("returns the same evidence as one full decode", async () => {
+ const recording = await segmentedRecording("chunk-parity", 15);
+ expect(recording.segments.length).toBeGreaterThanOrEqual(6);
+ const { single, chunked, fallbacks } = await both(recording);
+ expect(fallbacks).toHaveLength(0);
+ expect(chunked).toEqual(single);
+ expect(chunked.video.frameCount).toBe(120);
+ expect(chunked.audio).not.toBeNull();
+ });
+
+ async function misalignedRecording(name: string) {
+ const fragmented = join(directory, `${name}-fragmented.mp4`);
+ await run([
+ "ffmpeg",
+ "-v",
+ "error",
+ "-f",
+ "lavfi",
+ "-i",
+ "testsrc2=size=320x180:rate=30",
+ "-frames:v",
+ "120",
+ "-c:v",
+ "libx264",
+ "-preset",
+ "ultrafast",
+ "-g",
+ "45",
+ "-keyint_min",
+ "45",
+ "-sc_threshold",
+ "0",
+ "-pix_fmt",
+ "yuv420p",
+ "-movflags",
+ "empty_moov+default_base_moof+separate_moof",
+ "-frag_duration",
+ "400000",
+ fragmented,
+ ]);
+ const bytes = await readFile(fragmented);
+ const boxes: { type: string; start: number; end: number }[] = [];
+ for (let offset = 0; offset < bytes.length; ) {
+ const size = bytes.readUInt32BE(offset);
+ boxes.push({
+ type: bytes.toString("ascii", offset + 4, offset + 8),
+ start: offset,
+ end: offset + size,
+ });
+ offset += size;
+ }
+ const firstFragment = boxes.findIndex((box) => box.type === "moof");
+ const init = join(directory, `${name}-init.mp4`);
+ await writeFile(init, bytes.subarray(0, boxes[firstFragment].start));
+ const segments: string[] = [];
+ for (let index = firstFragment; index < boxes.length; index += 2) {
+ const segment = join(
+ directory,
+ `${name}-fragment-${segments.length}.m4s`,
+ );
+ await writeFile(
+ segment,
+ bytes.subarray(
+ boxes[index].start,
+ boxes[index + 1]?.end ?? bytes.length,
+ ),
+ );
+ segments.push(segment);
+ }
+ const output = join(directory, `${name}-output.mp4`);
+ await muxMediaTracksToMp4(fragmented, silent, output);
+ return { init, segments, combined: fragmented, output };
+ }
+
+ test("falls back to one full decode when fragments do not start on keyframes", async () => {
+ const recording = await misalignedRecording("chunk-misaligned");
+ expect(recording.segments.length).toBeGreaterThanOrEqual(6);
+ const { single, chunked, fallbacks } = await both(recording);
+ expect(fallbacks).toHaveLength(1);
+ expect(chunked).toEqual(single);
+ expect(chunked.video.frameCount).toBe(120);
+ });
+
+ async function resizedRecording(name: string) {
+ const halves: string[] = [];
+ for (const size of ["320x180", "352x180"]) {
+ const half = join(directory, `${name}-${size}.h264`);
+ await run([
+ "ffmpeg",
+ "-v",
+ "error",
+ "-f",
+ "lavfi",
+ "-i",
+ `testsrc2=size=${size}:rate=30`,
+ "-frames:v",
+ "90",
+ "-c:v",
+ "libx264",
+ "-preset",
+ "ultrafast",
+ "-g",
+ "15",
+ "-keyint_min",
+ "15",
+ "-sc_threshold",
+ "0",
+ "-pix_fmt",
+ "yuv420p",
+ "-bsf:v",
+ "h264_mp4toannexb",
+ "-f",
+ "h264",
+ half,
+ ]);
+ halves.push(half);
+ }
+ const stream = join(directory, `${name}.h264`);
+ await writeFile(
+ stream,
+ Buffer.concat(await Promise.all(halves.map((path) => readFile(path)))),
+ );
+ const fragments = await mkdtemp(join(directory, `${name}-`));
+ await run([
+ "ffmpeg",
+ "-v",
+ "error",
+ "-framerate",
+ "30",
+ "-f",
+ "h264",
+ "-i",
+ stream,
+ "-c",
+ "copy",
+ "-f",
+ "dash",
+ "-seg_duration",
+ "0.5",
+ "-use_template",
+ "1",
+ "-use_timeline",
+ "1",
+ "-init_seg_name",
+ "init.mp4",
+ "-media_seg_name",
+ "fragment-$Number%05d$.m4s",
+ join(fragments, "manifest.mpd"),
+ ]);
+ const init = join(fragments, "init.mp4");
+ const segments = (await readdir(fragments))
+ .filter((file) => /^fragment-\d+\.m4s$/.test(file))
+ .sort()
+ .map((file) => join(fragments, file));
+ expect(segments.length).toBeGreaterThanOrEqual(6);
+ const combined = join(directory, `${name}-combined.mp4`);
+ await writeFile(
+ combined,
+ Buffer.concat(
+ await Promise.all([init, ...segments].map((path) => readFile(path))),
+ ),
+ );
+ const output = join(directory, `${name}-output.mp4`);
+ await muxMediaTracksToMp4(combined, silent, output);
+ return { init, segments, combined, output };
+ }
+
+ test("falls back when a later chunk starts at a different resolution", async () => {
+ const recording = await resizedRecording("chunk-resize");
+ const { single, chunked, fallbacks } = await both(recording);
+ expect(fallbacks).toHaveLength(1);
+ expect(String(fallbacks[0][0])).toContain(
+ "dimensions 0: 320x180 -> 352x180",
+ );
+ expect(chunked).toEqual(single);
+ expect(chunked.video.frameCount).toBe(180);
+ });
+
+ test("stops the other chunk decoders once headers differ", async () => {
+ const recording = await resizedRecording("chunk-resize-early");
+ const ffmpeg = Bun.which("ffmpeg");
+ if (!ffmpeg) throw new Error("ffmpeg is required");
+ // Every chunk after the first keeps running for 30 s after its output is complete,
+ // so verification only finishes quickly if those decoders are stopped early.
+ const bin = await mkdtemp(join(directory, "slow-ffmpeg-"));
+ await writeFile(
+ join(bin, "ffmpeg"),
+ [
+ "#!/bin/sh",
+ 'case "$*" in',
+ `*verify-chunk-0.mp4*) exec "${ffmpeg}" "$@" ;;`,
+ `*verify-chunk-*) "${ffmpeg}" "$@"; status=$?; exec 1>&- 2>&-; sleep 30; exit $status ;;`,
+ `*) exec "${ffmpeg}" "$@" ;;`,
+ "esac",
+ "",
+ ].join("\n"),
+ { mode: 0o755 },
+ );
+ const path = process.env.PATH;
+ process.env.PATH = `${bin}:${path}`;
+ process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS = "3";
+ const warn = spyOn(console, "warn");
+ try {
+ const started = performance.now();
+ const result = await verifyRemuxedRecording(
+ recording.combined,
+ silent,
+ recording.output,
+ { requireAudio: false },
+ { init: recording.init, segments: recording.segments },
+ );
+ expect(performance.now() - started).toBeLessThan(15_000);
+ expect(result.video.frameCount).toBe(180);
+ expect(
+ warn.mock.calls.some((call) =>
+ String(call[0]).includes("chunk stream parameters differ"),
+ ),
+ ).toBe(true);
+ } finally {
+ warn.mockRestore();
+ process.env.PATH = path;
+ delete process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS;
+ }
+ }, 60_000);
+
+ test("copies many fragments into one run without piling up stream listeners", async () => {
+ const recording = await segmentedRecording("chunk-many", 6, 240, 0.2);
+ expect(recording.segments.length).toBeGreaterThanOrEqual(30);
+ const warnings: string[] = [];
+ const onWarning = (warning: Error) => warnings.push(warning.name);
+ const events = process as unknown as NodeJS.EventEmitter;
+ events.on("warning", onWarning);
+ try {
+ const { single, chunked, fallbacks } = await both(recording);
+ expect(fallbacks).toHaveLength(0);
+ expect(chunked).toEqual(single);
+ } finally {
+ events.off("warning", onWarning);
+ }
+ expect(warnings).not.toContain("MaxListenersExceededWarning");
+ });
+
+ test("still rejects a corrupt recording through the full decode", async () => {
+ const recording = await segmentedRecording("chunk-corrupt", 15);
+ const target = recording.segments[3];
+ const bytes = await readFile(target);
+ bytes.fill(
+ 0,
+ Math.floor(bytes.length / 2),
+ Math.floor(bytes.length / 2) + 2048,
+ );
+ await writeFile(target, bytes);
+ const combined = Buffer.concat(
+ await Promise.all(
+ [recording.init, ...recording.segments].map((path) => readFile(path)),
+ ),
+ );
+ await writeFile(recording.combined, combined);
+ await muxMediaTracksToMp4(recording.combined, silent, recording.output);
+ process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS = "3";
+ try {
+ await expect(
+ verifyRemuxedRecording(
+ recording.combined,
+ silent,
+ recording.output,
+ { requireAudio: false },
+ { init: recording.init, segments: recording.segments },
+ ),
+ ).rejects.toThrow();
+ } finally {
+ delete process.env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS;
+ }
+ });
+});
+
+describe("ranged remote byte verification", () => {
+ const payload = new Uint8Array(3 * 1024 * 1024 + 12_345).map(
+ (_, index) => (index * 31 + 7) % 251,
+ );
+ const sha256 = createHash("sha256").update(payload).digest("hex");
+ const identity = '"ranged-object"';
+
+ function rangedServer(
+ respond?: (start: number, end: number, attempt: number) => Response | null,
+ ) {
+ const ranges: string[] = [];
+ const attempts = new Map<string, number>();
+ const server = Bun.serve({
+ hostname: "127.0.0.1",
+ port: 0,
+ fetch(request) {
+ if (request.headers.get("if-match") !== identity)
+ return new Response("Object changed", { status: 412 });
+ const range = request.headers.get("range");
+ const match = range?.match(/^bytes=(\d+)-(\d+)$/);
+ if (!match)
+ return new Response(payload, { headers: { ETag: identity } });
+ const start = Number(match[1]);
+ const end = Number(match[2]);
+ if (range !== "bytes=0-0") ranges.push(range ?? "");
+ const attempt = (attempts.get(range ?? "") ?? 0) + 1;
+ attempts.set(range ?? "", attempt);
+ const custom =
+ range === "bytes=0-0" ? null : respond?.(start, end, attempt);
+ if (custom) return custom;
+ return new Response(payload.slice(start, end + 1), {
+ status: 206,
+ headers: {
+ ETag: identity,
+ "Content-Range": `bytes ${start}-${end}/${payload.length}`,
+ },
+ });
+ },
+ });
+ return { server, ranges };
+ }
+
+ async function verify(port: number | undefined) {
+ return verifyRemoteRecordingBytes(
+ `http://127.0.0.1:${port}/recording.mp4`,
+ {
+ expectedObjectIdentity: identity,
+ expectedSha256: sha256,
+ expectedFileSize: payload.length,
+ },
+ );
+ }
+
+ const env = { ...process.env };
+ beforeAll(() => {
+ process.env.MEDIA_SERVER_VERIFY_DOWNLOAD_CONCURRENCY = "3";
+ process.env.MEDIA_SERVER_VERIFY_DOWNLOAD_PART_MB = "1";
+ });
+ afterAll(() => {
+ process.env.MEDIA_SERVER_VERIFY_DOWNLOAD_CONCURRENCY =
+ env.MEDIA_SERVER_VERIFY_DOWNLOAD_CONCURRENCY;
+ process.env.MEDIA_SERVER_VERIFY_DOWNLOAD_PART_MB =
+ env.MEDIA_SERVER_VERIFY_DOWNLOAD_PART_MB;
+ if (env.MEDIA_SERVER_VERIFY_DOWNLOAD_CONCURRENCY === undefined)
+ delete process.env.MEDIA_SERVER_VERIFY_DOWNLOAD_CONCURRENCY;
+ if (env.MEDIA_SERVER_VERIFY_DOWNLOAD_PART_MB === undefined)
+ delete process.env.MEDIA_SERVER_VERIFY_DOWNLOAD_PART_MB;
+ });
+
+ test("hashes every pinned range in order", async () => {
+ const { server, ranges } = rangedServer();
+ try {
+ const result = await verify(server.port);
+ expect(result).toEqual({
+ objectIdentity: identity,
+ fileSize: payload.length,
+ remoteSha256: sha256,
+ });
+ const mb = 1024 * 1024;
+ expect([...ranges].sort()).toEqual(
+ [0, 1, 2, 3]
+ .map(
+ (index) =>
+ `bytes=${index * mb}-${Math.min(payload.length, (index + 1) * mb) - 1}`,
+ )
+ .sort(),
+ );
+ } finally {
+ await server.stop(true);
+ }
+ });
+
+ test("rejects a range served from a different object", async () => {
+ const { server } = rangedServer((start, end) =>
+ start === 2 * 1024 * 1024
+ ? new Response(payload.slice(start, end + 1), {
+ status: 206,
+ headers: {
+ ETag: '"replaced-object"',
+ "Content-Range": `bytes ${start}-${end}/${payload.length}`,
+ },
+ })
+ : null,
+ );
+ try {
+ await expect(verify(server.port)).rejects.toThrow(
+ "Recording object changed during byte verification",
+ );
+ } finally {
+ await server.stop(true);
+ }
+ });
+
+ test("rejects a range that does not match its requested bytes", async () => {
+ const { server } = rangedServer((start, end) =>
+ start === 1024 * 1024
+ ? new Response(payload.slice(start, end + 1), {
+ status: 206,
+ headers: {
+ ETag: identity,
+ "Content-Range": `bytes ${start}-${end - 1}/${payload.length}`,
+ },
+ })
+ : null,
+ );
+ try {
+ await expect(verify(server.port)).rejects.toThrow(
+ "Recording byte resume range does not match the verified object",
+ );
+ } finally {
+ await server.stop(true);
+ }
+ });
+
+ test("retries a range after a transient storage error", async () => {
+ const { server, ranges } = rangedServer((start, _end, attempt) =>
+ start === 0 && attempt === 1
+ ? new Response("busy", { status: 503 })
+ : null,
+ );
+ try {
+ expect((await verify(server.port)).remoteSha256).toBe(sha256);
+ expect(
+ ranges.filter((range) => range.startsWith("bytes=0-")),
+ ).toHaveLength(2);
+ } finally {
+ await server.stop(true);
+ }
+ });
+
+ test("rejects an oversized range body without buffering it whole", async () => {
+ const { server } = rangedServer((start, end) =>
+ start === 1024 * 1024
+ ? new Response(payload, {
+ status: 206,
+ headers: {
+ ETag: identity,
+ "Content-Range": `bytes ${start}-${end}/${payload.length}`,
+ },
+ })
+ : null,
+ );
+ try {
+ await expect(verify(server.port)).rejects.toThrow(
+ "Recording object size changed during byte verification",
+ );
+ } finally {
+ await server.stop(true);
+ }
+ });
+
+ test("retries a range whose body ends early", async () => {
+ const { server, ranges } = rangedServer((start, end, attempt) =>
+ start === 2 * 1024 * 1024 && attempt === 1
+ ? new Response(payload.slice(start, start + 1000), {
+ status: 206,
+ headers: {
+ ETag: identity,
+ "Content-Range": `bytes ${start}-${end}/${payload.length}`,
+ },
+ })
+ : null,
+ );
+ try {
+ expect((await verify(server.port)).remoteSha256).toBe(sha256);
+ expect(
+ ranges.filter((range) => range.startsWith(`bytes=${2 * 1024 * 1024}-`)),
+ ).toHaveLength(2);
+ } finally {
+ await server.stop(true);
+ }
+ });
+
+ test("falls back to one sequential read when storage ignores ranges", async () => {
+ const { server } = rangedServer(
+ () => new Response(payload, { headers: { ETag: identity } }),
+ );
+ try {
+ expect((await verify(server.port)).remoteSha256).toBe(sha256);
+ } finally {
+ await server.stop(true);
+ }
+ });
+});
+
describe("remote recording object identity", () => {
test("resumes interrupted byte verification without rereading the verified prefix", async () => {
const identity = '"resume-object"';
diff --git a/apps/media-server/src/__tests__/lib/storage-upload.test.ts b/apps/media-server/src/__tests__/lib/storage-upload.test.ts
index f260ea8ae..4df363215 100644
--- a/apps/media-server/src/__tests__/lib/storage-upload.test.ts
+++ b/apps/media-server/src/__tests__/lib/storage-upload.test.ts
@@ -1,9 +1,12 @@
import { afterEach, describe, expect, test } from "bun:test";
import { createHash } from "node:crypto";
-import { mkdtemp, rm, writeFile } from "node:fs/promises";
+import { mkdtemp, open, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { uploadFileToStorage } from "../../lib/media-video";
+import {
+ multipartUploadConcurrencyFromEnv,
+ uploadFileToStorage,
+} from "../../lib/media-video";
import { verifyRemoteRecordingBytes } from "../../lib/recording-verification";
const originalFetch = globalThis.fetch;
@@ -29,6 +32,71 @@ describe("uploadFileToStorage", () => {
globalThis.fetch = originalFetch;
});
+ test("uploads a 10 GB sparse file as 160 bounded parts with its Loom attempt", async () => {
+ const uploadFile = await createTempUploadFile(1);
+ const handle = await open(uploadFile.path, "r+");
+ try {
+ await handle.truncate(10 * 1024 ** 3);
+ } finally {
+ await handle.close();
+ }
+ let uploadedParts = 0;
+ let uploadedBytes = 0;
+ let completed = false;
+ globalThis.fetch = (async (input, init) => {
+ const url = String(input);
+ if (url.endsWith("/sign-part")) {
+ const request = JSON.parse(String(init?.body));
+ expect(request.loomImportAttemptId).toBe("loom-run:0");
+ expect(request.contentLength).toBe(64 * 1024 ** 2);
+ return Response.json({
+ url: `https://storage.test/part/${request.partNumber}`,
+ });
+ }
+ if (init?.method === "PUT") {
+ const body = init.body;
+ if (!(body instanceof Blob))
+ throw new Error("Expected a bounded file slice");
+ uploadedParts++;
+ uploadedBytes += body.size;
+ expect(body.size).toBe(64 * 1024 ** 2);
+ return new Response(null, {
+ headers: { ETag: `"part-${uploadedParts}"` },
+ });
+ }
+ if (url.endsWith("/complete")) {
+ const request = JSON.parse(String(init?.body));
+ expect(request.loomImportAttemptId).toBe("loom-run:0");
+ expect(request.parts).toHaveLength(160);
+ completed = true;
+ return Response.json({ objectIdentity: '"multipart-result"' });
+ }
+ throw new Error("Unexpected multipart request");
+ }) as typeof fetch;
+ try {
+ await uploadFileToStorage(
+ uploadFile.path,
+ {
+ type: "multipart",
+ videoId: "video",
+ key: "owner/video/raw-upload.mp4",
+ uploadId: "upload",
+ partSize: 64 * 1024 ** 2,
+ signPartUrl: "https://cap.test/sign-part",
+ completeUrl: "https://cap.test/complete",
+ abortUrl: "https://cap.test/abort",
+ loomImportAttemptId: "loom-run:0",
+ },
+ "video/mp4",
+ );
+ expect(uploadedParts).toBe(160);
+ expect(uploadedBytes).toBe(10 * 1024 ** 3);
+ expect(completed).toBe(true);
+ } finally {
+ await uploadFile.cleanup();
+ }
+ });
+
test.each([false, true])(
"checks full remote bytes after Drive metadata version drift (corrupt=%s)",
async (corrupt) => {
@@ -314,6 +382,74 @@ describe("uploadFileToStorage", () => {
});
});
+ test("uploads multipart parts concurrently and completes them in order", async () => {
+ const uploadFile = await createTempUploadFile(partSize * 4 + 1);
+ let inFlight = 0;
+ let maxInFlight = 0;
+ let completed: unknown;
+ globalThis.fetch = (async (input, init) => {
+ const url = String(input);
+ if (init?.method === "POST") {
+ const json = JSON.parse(String(init.body));
+ if (url.endsWith("/sign"))
+ return Response.json({
+ url: `https://storage.example.com/part-${json.partNumber}`,
+ });
+ completed = json;
+ return Response.json({ objectIdentity: '"completed"' });
+ }
+ const partNumber = Number(url.split("part-")[1]);
+ inFlight++;
+ maxInFlight = Math.max(maxInFlight, inFlight);
+ await Bun.sleep((6 - partNumber) * 5);
+ inFlight--;
+ return new Response(null, {
+ status: 200,
+ headers: { etag: `"etag-${partNumber}"` },
+ });
+ }) as typeof fetch;
+ process.env.MEDIA_SERVER_UPLOAD_CONCURRENCY = "3";
+ try {
+ await uploadFileToStorage(
+ uploadFile.path,
+ {
+ type: "multipart",
+ videoId: "video-id",
+ key: "user-id/video-id/result.mp4",
+ uploadId: "upload-id",
+ partSize,
+ signPartUrl: "https://cap.example.com/sign",
+ completeUrl: "https://cap.example.com/complete",
+ abortUrl: "https://cap.example.com/abort",
+ },
+ "video/mp4",
+ );
+ } finally {
+ delete process.env.MEDIA_SERVER_UPLOAD_CONCURRENCY;
+ await uploadFile.cleanup();
+ }
+ expect(maxInFlight).toBe(3);
+ expect(
+ (completed as { parts: Array<{ partNumber: number }> }).parts.map(
+ (part) => part.partNumber,
+ ),
+ ).toEqual([1, 2, 3, 4, 5]);
+ });
+
+ test("reads a bounded multipart upload concurrency", () => {
+ expect(multipartUploadConcurrencyFromEnv({})).toBe(1);
+ expect(
+ multipartUploadConcurrencyFromEnv({
+ MEDIA_SERVER_UPLOAD_CONCURRENCY: "8",
+ }),
+ ).toBe(8);
+ expect(
+ multipartUploadConcurrencyFromEnv({
+ MEDIA_SERVER_UPLOAD_CONCURRENCY: "99",
+ }),
+ ).toBe(16);
+ });
+
test("aborts multipart uploads when a part fails", async () => {
const uploadFile = await createTempUploadFile(partSize + 1);
const requests: Array<{ url: string; method: string }> = [];
diff --git a/apps/media-server/src/__tests__/lib/temp-files.test.ts b/apps/media-server/src/__tests__/lib/temp-files.test.ts
new file mode 100644
index 000000000..04c6b136c
--- /dev/null
+++ b/apps/media-server/src/__tests__/lib/temp-files.test.ts
@@ -0,0 +1,180 @@
+import { afterEach, describe, expect, test } from "bun:test";
+import { randomUUID } from "node:crypto";
+import { existsSync } from "node:fs";
+import { mkdir, rm, utimes, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { spawn } from "bun";
+import {
+ cleanupMediaTransferCache,
+ sweepMediaTransferCache,
+} from "../../lib/media-transfer";
+import {
+ cleanupStaleTempFiles,
+ createTempFile,
+ ensureTempDir,
+ getInstanceDir,
+ getTempDir,
+ getTempDirUsage,
+ getTempRoot,
+ removeOwnTempDir,
+ retainTempPath,
+ sweepOrphanedTempDirs,
+} from "../../lib/temp-files";
+
+const created: string[] = [];
+
+afterEach(async () => {
+ for (const path of created.splice(0)) {
+ await rm(path, { recursive: true, force: true });
+ }
+});
+
+async function makeTree(path: string, ageMs: number, lease = false) {
+ created.push(path);
+ await mkdir(join(path, "nested", "deeper"), { recursive: true });
+ await writeFile(join(path, "nested", "deeper", "segment.m4s"), "segment");
+ const past = new Date(Date.now() - ageMs);
+ if (lease) {
+ await writeFile(join(path, ".lease"), "");
+ await utimes(join(path, ".lease"), past, past);
+ }
+ await utimes(path, past, past);
+}
+
+async function setAge(path: string, ageMs: number) {
+ const past = new Date(Date.now() - ageMs);
+ await utimes(path, past, past);
+}
+
+describe("startup temp sweep", () => {
+ test("removes orphaned instance dirs and old legacy entries only", async () => {
+ await ensureTempDir();
+ const root = getTempRoot();
+ const staleInstance = join(root, `instance-${randomUUID()}`);
+ const liveInstance = join(root, `instance-${randomUUID()}`);
+ const oldLegacyDir = join(root, `mux-job_${randomUUID()}`);
+ const youngLegacyFile = join(root, `${randomUUID()}.mp4`);
+ await makeTree(staleInstance, 10 * 60 * 1000, true);
+ await makeTree(liveInstance, 10 * 60 * 1000);
+ await writeFile(join(liveInstance, ".lease"), "");
+ await makeTree(oldLegacyDir, 2 * 60 * 60 * 1000);
+ created.push(youngLegacyFile);
+ await writeFile(youngLegacyFile, "in use by an older server version");
+
+ const removed = await sweepOrphanedTempDirs();
+
+ expect(removed).toBeGreaterThanOrEqual(2);
+ expect(existsSync(staleInstance)).toBe(false);
+ expect(existsSync(oldLegacyDir)).toBe(false);
+ expect(existsSync(liveInstance)).toBe(true);
+ expect(existsSync(youngLegacyFile)).toBe(true);
+ expect(existsSync(getTempDir())).toBe(true);
+ expect(existsSync(join(getTempDir(), ".lease"))).toBe(true);
+ });
+
+ test("never treats the server's own instance dir as orphaned", async () => {
+ await ensureTempDir();
+ await setAge(join(getTempDir(), ".lease"), 24 * 60 * 60 * 1000);
+ await setAge(getTempDir(), 24 * 60 * 60 * 1000);
+
+ await sweepOrphanedTempDirs();
+
+ expect(existsSync(getTempDir())).toBe(true);
+ });
+});
+
+describe("stale temp cleanup", () => {
+ test("removes stale directories recursively and keeps in-use entries", async () => {
+ await ensureTempDir();
+ const staleDir = join(getTempDir(), `stream-${randomUUID()}`);
+ await makeTree(staleDir, 2 * 60 * 60 * 1000);
+ const retainedDir = join(getTempDir(), `mux-job_${randomUUID()}`);
+ await makeTree(retainedDir, 2 * 60 * 60 * 1000);
+ const releaseRetainedDir = retainTempPath(retainedDir);
+ const inUse = await createTempFile(".mp4");
+ created.push(inUse.path);
+ await writeFile(inUse.path, "input still read by ffmpeg");
+ await setAge(inUse.path, 2 * 60 * 60 * 1000);
+ const fresh = await createTempFile(".mp4");
+ created.push(fresh.path);
+ await writeFile(fresh.path, "fresh");
+
+ try {
+ expect(await cleanupStaleTempFiles()).toBeGreaterThanOrEqual(1);
+ expect(existsSync(staleDir)).toBe(false);
+ expect(existsSync(retainedDir)).toBe(true);
+ expect(existsSync(inUse.path)).toBe(true);
+ expect(existsSync(fresh.path)).toBe(true);
+
+ releaseRetainedDir();
+ await cleanupStaleTempFiles();
+ expect(existsSync(retainedDir)).toBe(false);
+ } finally {
+ releaseRetainedDir();
+ await inUse.cleanup();
+ await fresh.cleanup();
+ }
+ });
+
+ test("expires entries that were retained far beyond any job lifetime", async () => {
+ await ensureTempDir();
+ const leaked = await createTempFile(".mp4");
+ created.push(leaked.path);
+ await writeFile(leaked.path, "leaked handle");
+ await setAge(leaked.path, 7 * 60 * 60 * 1000);
+
+ await cleanupStaleTempFiles();
+
+ expect(existsSync(leaked.path)).toBe(false);
+ });
+
+ test("reports temp usage including nested files", async () => {
+ await ensureTempDir();
+ const dir = join(getTempDir(), `usage-${randomUUID()}`);
+ await makeTree(dir, 0);
+
+ const usage = await getTempDirUsage();
+
+ expect(usage.entries).toBeGreaterThanOrEqual(4);
+ expect(usage.bytes).toBeGreaterThanOrEqual("segment".length);
+ expect(usage.truncated).toBe(false);
+ });
+
+ test("removes the server's own dir on shutdown and recreates it on demand", async () => {
+ const handle = await createTempFile(".mp4");
+ await writeFile(handle.path, "shutdown");
+
+ await removeOwnTempDir();
+ expect(existsSync(getTempDir())).toBe(false);
+
+ const next = await createTempFile(".mp4");
+ expect(existsSync(join(getTempDir(), ".lease"))).toBe(true);
+ await next.cleanup();
+ });
+});
+
+describe("media transfer cache sweep", () => {
+ test("removes orphaned instance and dead-pid cache dirs but keeps a live pid's cache however old", async () => {
+ const base = join(tmpdir(), "cap-media-download-cache");
+ const staleInstance = join(base, `instance-${randomUUID()}`);
+ const liveInstance = join(base, `instance-${randomUUID()}`);
+ const exited = spawn({ cmd: ["true"] });
+ await exited.exited;
+ const deadPidDir = join(base, String(exited.pid));
+ const oldLivePidDir = join(base, String(process.ppid));
+ await makeTree(staleInstance, 10 * 60 * 1000, true);
+ await makeTree(liveInstance, 0, true);
+ await makeTree(deadPidDir, 0);
+ await makeTree(oldLivePidDir, 24 * 60 * 60 * 1000);
+
+ await sweepMediaTransferCache();
+
+ expect(existsSync(staleInstance)).toBe(false);
+ expect(existsSync(deadPidDir)).toBe(false);
+ expect(existsSync(liveInstance)).toBe(true);
+ expect(existsSync(oldLivePidDir)).toBe(true);
+ expect(existsSync(getInstanceDir(base))).toBe(false);
+ await cleanupMediaTransferCache();
+ });
+});
diff --git a/apps/media-server/src/__tests__/routes/lifecycle.test.ts b/apps/media-server/src/__tests__/routes/lifecycle.test.ts
new file mode 100644
index 000000000..dfd39edce
--- /dev/null
+++ b/apps/media-server/src/__tests__/routes/lifecycle.test.ts
@@ -0,0 +1,600 @@
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ mock,
+ spyOn,
+ test,
+} from "bun:test";
+import { existsSync } from "node:fs";
+import { writeFile } from "node:fs/promises";
+import app from "../../app";
+import * as jobManager from "../../lib/job-manager";
+import {
+ cleanupExpiredJobs,
+ createJob,
+ deleteJob,
+ getActiveVideoProcessCount,
+ getJob,
+ type VideoMetadata,
+} from "../../lib/job-manager";
+import {
+ DRAINING_DETAILS,
+ setDraining,
+ waitForDrain,
+} from "../../lib/lifecycle";
+import * as mediaEdit from "../../lib/media-edit";
+import * as mediaProbe from "../../lib/media-probe";
+import * as mediaVideo from "../../lib/media-video";
+import { createTempFile, type TempFileHandle } from "../../lib/temp-files";
+import { getActiveDirectVideoProcessCount } from "../../lib/video-capacity";
+
+const MEDIA_SERVER_SECRET = "test-secret";
+const AUTH_HEADERS = {
+ "Content-Type": "application/json",
+ "x-media-server-secret": MEDIA_SERVER_SECRET,
+};
+const originalPublicStatus = process.env.MEDIA_SERVER_PUBLIC_STATUS;
+
+process.env.MEDIA_SERVER_WEBHOOK_SECRET = MEDIA_SERVER_SECRET;
+
+const metadata: VideoMetadata = {
+ duration: 3 * 60 * 60,
+ width: 1920,
+ height: 1080,
+ fps: 30,
+ videoCodec: "h264",
+ audioCodec: "aac",
+ audioChannels: 2,
+ sampleRate: 48_000,
+ bitrate: 5_000_000,
+ fileSize: 1024 ** 3,
+};
+
+const createdJobs: string[] = [];
+
+function post(path: string, body: unknown): Request {
+ return new Request(`http://localhost${path}`, {
+ method: "POST",
+ headers: AUTH_HEADERS,
+ body: JSON.stringify(body),
+ });
+}
+
+function deferred<T = void>() {
+ let resolve: (value: T) => void = () => {};
+ const promise = new Promise<T>((done) => {
+ resolve = done;
+ });
+ return { promise, resolve };
+}
+
+async function tempFileWith(extension: string, contents: string) {
+ const handle = await createTempFile(extension);
+ await writeFile(handle.path, contents);
+ return handle;
+}
+
+async function waitFor(condition: () => boolean, timeoutMs = 2_000) {
+ const deadline = Date.now() + timeoutMs;
+ while (!condition()) {
+ if (Date.now() > deadline) throw new Error("Timed out waiting");
+ await Bun.sleep(10);
+ }
+}
+
+beforeEach(() => {
+ spyOn(jobManager, "canAcceptNewVideoProcess").mockReturnValue(true);
+});
+
+afterEach(async () => {
+ mock.restore();
+ setDraining(false);
+ if (originalPublicStatus === undefined)
+ delete process.env.MEDIA_SERVER_PUBLIC_STATUS;
+ else process.env.MEDIA_SERVER_PUBLIC_STATUS = originalPublicStatus;
+ await waitForDrain(2_000);
+ for (const jobId of createdJobs.splice(0)) deleteJob(jobId);
+});
+
+describe("draining", () => {
+ const url = "https://example.com/video.mp4";
+ const admissions: Array<[string, unknown]> = [
+ [
+ "/video/process",
+ { videoId: "v", userId: "u", videoUrl: url, outputPresignedUrl: url },
+ ],
+ [
+ "/video/import",
+ {
+ videoId: "v",
+ userId: "u",
+ videoUrl: url,
+ outputPresignedUrl: url,
+ sourcePresignedUrl: url,
+ },
+ ],
+ [
+ "/video/edit",
+ {
+ videoId: "v",
+ userId: "u",
+ sourceUrl: url,
+ outputPresignedUrl: url,
+ keepRanges: [{ start: 0, end: 1 }],
+ },
+ ],
+ [
+ "/video/mux-segments",
+ {
+ videoId: "v",
+ userId: "u",
+ outputPresignedUrl: url,
+ videoInitUrl: url,
+ videoSegmentUrls: ["https://example.com/segment.m4s"],
+ },
+ ],
+ [
+ "/video/verify-recording",
+ {
+ videoId: "v",
+ userId: "u",
+ videoUrl: url,
+ fileSize: 1,
+ duration: 1,
+ requiredAudio: false,
+ objectIdentity: '"etag"',
+ webhookUrl: "https://example.com/webhook",
+ },
+ ],
+ ["/video/convert", { videoUrl: url }],
+ ["/video/probe", { videoUrl: url }],
+ ["/video/thumbnail", { videoUrl: url }],
+ ["/audio/check", { videoUrl: url }],
+ ["/audio/extract", { videoUrl: url }],
+ ["/audio/convert", { audioUrl: url }],
+ ];
+
+ test.each(admissions)("rejects new work on %s", async (path, body) => {
+ setDraining(true);
+ const response = await app.fetch(post(path, body));
+
+ expect(response.status).toBe(503);
+ expect(response.headers.get("Retry-After")).toBe("15");
+ const data = await response.json();
+ expect(data.code).toBe("SERVER_BUSY");
+ expect(data.details).toBe(DRAINING_DETAILS);
+ });
+
+ test("fails health checks but keeps status endpoints working", async () => {
+ const job = createJob("job-draining-status", "video-id", "user-id");
+ createdJobs.push(job.jobId);
+ setDraining(true);
+
+ const health = await app.fetch(new Request("http://localhost/health"));
+ expect(health.status).toBe(503);
+ expect((await health.json()).status).toBe("draining");
+
+ const status = await app.fetch(
+ new Request(`http://localhost/video/process/${job.jobId}/status`),
+ );
+ expect(status.status).toBe(200);
+ expect((await status.json()).jobId).toBe(job.jobId);
+
+ const serverStatus = await app.fetch(
+ new Request("http://localhost/video/status"),
+ );
+ expect(serverStatus.status).toBe(200);
+ });
+});
+
+describe("status access", () => {
+ test("requires the secret when public status is disabled", async () => {
+ process.env.MEDIA_SERVER_PUBLIC_STATUS = "false";
+ for (const path of [
+ "/video/status",
+ "/audio/status",
+ "/video/process/job-missing/status",
+ ]) {
+ const anonymous = await app.fetch(new Request(`http://localhost${path}`));
+ expect(anonymous.status).toBe(401);
+ const authenticated = await app.fetch(
+ new Request(`http://localhost${path}`, { headers: AUTH_HEADERS }),
+ );
+ expect(authenticated.status).not.toBe(401);
+ }
+ });
+
+ test("reports runtime counters only to authenticated callers", async () => {
+ const job = createJob("job_runtime_report", "video-runtime", "user-id");
+ createdJobs.push(job.jobId);
+ const anonymous = await app.fetch(new Request("http://localhost/health"));
+ expect((await anonymous.json()).runtime).toBeUndefined();
+
+ for (const path of ["/health", "/video/status"]) {
+ const response = await app.fetch(
+ new Request(`http://localhost${path}`, { headers: AUTH_HEADERS }),
+ );
+ const { runtime } = await response.json();
+ expect(runtime.activeJobs).toBeGreaterThanOrEqual(1);
+ expect(runtime.jobTotals.created).toBeGreaterThanOrEqual(1);
+ expect(typeof runtime.activeSubprocesses).toBe("number");
+ expect(typeof runtime.tempDir.bytes).toBe("number");
+ expect(typeof runtime.tempDir.entries).toBe("number");
+ expect(typeof runtime.mediaTransferCache.bytes).toBe("number");
+ expect(typeof runtime.slots.video.max).toBe("number");
+ expect(typeof runtime.slots.directVideo.active).toBe("number");
+ expect(typeof runtime.slots.audio.max).toBe("number");
+ expect(typeof runtime.slots.probe.max).toBe("number");
+ expect(JSON.stringify(runtime)).not.toContain(job.jobId);
+ expect(JSON.stringify(runtime)).not.toContain(job.videoId);
+ }
+ });
+});
+
+describe("process jobs", () => {
+ test("heartbeats a slow output upload and derives the lifetime from the input", async () => {
+ const intervals: Array<{
+ callback: () => void;
+ delay?: number;
+ handle: ReturnType<typeof setInterval>;
+ }> = [];
+ const cleared = new Set<unknown>();
+ const originalSetInterval = globalThis.setInterval;
+ const originalClearInterval = globalThis.clearInterval;
+ spyOn(globalThis, "setInterval").mockImplementation(((
+ callback: () => void,
+ delay?: number,
+ ) => {
+ const handle = originalSetInterval(callback, delay);
+ intervals.push({ callback, delay, handle });
+ return handle;
+ }) as unknown as typeof setInterval);
+ spyOn(globalThis, "clearInterval").mockImplementation(((
+ handle: ReturnType<typeof setInterval>,
+ ) => {
+ cleared.add(handle);
+ originalClearInterval(handle);
+ }) as unknown as typeof clearInterval);
+ const files: TempFileHandle[] = [];
+ spyOn(mediaVideo, "downloadVideoToTemp").mockImplementation(async () => {
+ const input = await tempFileWith(".mp4", "input");
+ files.push(input);
+ return input;
+ });
+ spyOn(mediaProbe, "probeVideoFile").mockResolvedValue(metadata);
+ spyOn(mediaVideo, "processVideo").mockImplementation(async () => {
+ const output = await tempFileWith(".mp4", "output");
+ files.push(output);
+ return output;
+ });
+ const uploadStarted = deferred();
+ const upload = deferred<mediaVideo.StorageUploadReceipt>();
+ spyOn(mediaVideo, "uploadFileToS3").mockImplementation(async () => {
+ uploadStarted.resolve();
+ return await upload.promise;
+ });
+
+ const response = await app.fetch(
+ post("/video/process", {
+ videoId: "video-slow-upload",
+ userId: "user-id",
+ videoUrl: "https://example.com/input.mp4",
+ outputPresignedUrl: "https://example.com/output.mp4",
+ }),
+ );
+ const { jobId } = await response.json();
+ createdJobs.push(jobId);
+ await uploadStarted.promise;
+
+ const job = getJob(jobId);
+ expect(job?.phase).toBe("uploading");
+ if (!job) throw new Error("Job missing");
+ expect(job.lifetimeDeadlineAt).toBeGreaterThan(
+ job.createdAt + 2 * 60 * 60 * 1000,
+ );
+ expect(job.lifetimeDeadlineAt).toBeLessThanOrEqual(
+ job.createdAt + 4 * 60 * 60 * 1000,
+ );
+
+ job.updatedAt = Date.now() - 16 * 60 * 1000;
+ const heartbeats = intervals.filter(
+ ({ delay, handle }) => delay === 60_000 && !cleared.has(handle),
+ );
+ expect(heartbeats).toHaveLength(1);
+ heartbeats[0]?.callback();
+ expect(cleanupExpiredJobs()).toBe(0);
+ expect(job.phase).toBe("uploading");
+
+ upload.resolve({});
+ await waitFor(() => job.phase === "complete");
+ await waitForDrain(2_000);
+ expect(files.every((handle) => !existsSync(handle.path))).toBe(true);
+ });
+
+ function previewJob(videoId: string) {
+ return post("/video/process", {
+ videoId,
+ userId: "user-id",
+ videoUrl: "https://example.com/input.mp4",
+ outputPresignedUrl: "https://example.com/output.mp4",
+ thumbnailPresignedUrl: "https://example.com/screenshot.jpg",
+ previewGifPresignedUrl: "https://example.com/preview.gif",
+ });
+ }
+
+ function mockPreviewJob() {
+ const events: string[] = [];
+ const signals: AbortSignal[] = [];
+ const gifs: TempFileHandle[] = [];
+ spyOn(mediaVideo, "downloadVideoToTemp").mockImplementation(() =>
+ tempFileWith(".mp4", "input"),
+ );
+ spyOn(mediaProbe, "probeVideoFile").mockResolvedValue({
+ ...metadata,
+ duration: 10,
+ });
+ spyOn(mediaVideo, "processVideo").mockImplementation(() =>
+ tempFileWith(".mp4", "output"),
+ );
+ spyOn(mediaVideo, "generateThumbnail").mockImplementation(
+ async (_path, _duration, _options, signal) => {
+ events.push("thumbnail rendered");
+ if (signal) signals.push(signal);
+ return new Uint8Array([1]);
+ },
+ );
+ spyOn(mediaVideo, "generatePreviewGif").mockImplementation(
+ async (_path, _duration, _options, signal) => {
+ events.push("gif rendered");
+ if (signal) signals.push(signal);
+ const gif = await tempFileWith(".gif", "gif");
+ gifs.push(gif);
+ return gif;
+ },
+ );
+ spyOn(mediaVideo, "uploadToS3").mockImplementation(async () => {
+ events.push("thumbnail uploaded");
+ });
+ const output = deferred<mediaVideo.StorageUploadReceipt>();
+ const outputStarted = deferred();
+ const outputFailure = deferred<Error | null>();
+ spyOn(mediaVideo, "uploadFileToS3").mockImplementation(
+ async (_path, _url, contentType) => {
+ if (contentType === "image/gif") {
+ events.push("gif uploaded");
+ return {};
+ }
+ events.push("output upload started");
+ outputStarted.resolve();
+ const failure = await Promise.race([
+ output.promise.then(() => null),
+ outputFailure.promise,
+ ]);
+ if (failure) throw failure;
+ events.push("output uploaded");
+ return {};
+ },
+ );
+ return { events, signals, gifs, output, outputStarted, outputFailure };
+ }
+
+ test("renders previews while the output uploads and uploads them after it", async () => {
+ const preview = mockPreviewJob();
+ const response = await app.fetch(previewJob("video-preview-overlap"));
+ const { jobId } = await response.json();
+ createdJobs.push(jobId);
+ await preview.outputStarted.promise;
+ await waitFor(() => preview.events.includes("gif rendered"));
+ expect(preview.events).toContain("thumbnail rendered");
+ expect(preview.events).not.toContain("thumbnail uploaded");
+ expect(preview.events).not.toContain("gif uploaded");
+
+ preview.output.resolve({});
+ await waitFor(() => getJob(jobId)?.phase === "complete");
+ const uploaded = preview.events.indexOf("output uploaded");
+ expect(preview.events.indexOf("thumbnail uploaded")).toBeGreaterThan(
+ uploaded,
+ );
+ expect(preview.events.indexOf("gif uploaded")).toBeGreaterThan(uploaded);
+ await waitForDrain(2_000);
+ expect(preview.gifs.every((gif) => !existsSync(gif.path))).toBe(true);
+ });
+
+ test("reports an upload failure only after the thumbnail render has stopped", async () => {
+ const preview = mockPreviewJob();
+ let thumbnailStopped = false;
+ spyOn(mediaVideo, "generateThumbnail").mockImplementation(
+ (_path, _duration, _options, signal) =>
+ new Promise((_resolve, reject) => {
+ signal?.addEventListener("abort", () => {
+ setTimeout(() => {
+ thumbnailStopped = true;
+ reject(new Error("Thumbnail render stopped"));
+ }, 100);
+ });
+ }),
+ );
+ const response = await app.fetch(previewJob("video-preview-thumb-stop"));
+ const { jobId } = await response.json();
+ createdJobs.push(jobId);
+ await preview.outputStarted.promise;
+
+ preview.outputFailure.resolve(new Error("Storage upload failed: 403"));
+ await waitFor(() => getJob(jobId)?.phase === "error");
+ expect(thumbnailStopped).toBe(true);
+ expect(await waitForDrain(2_000)).toBe(true);
+ });
+
+ test("uploads no previews and stops rendering when the output upload fails", async () => {
+ const preview = mockPreviewJob();
+ const response = await app.fetch(previewJob("video-preview-upload-fails"));
+ const { jobId } = await response.json();
+ createdJobs.push(jobId);
+ await preview.outputStarted.promise;
+ await waitFor(() => preview.events.includes("gif rendered"));
+
+ preview.outputFailure.resolve(new Error("Storage upload failed: 403"));
+ expect(await waitForDrain(2_000)).toBe(true);
+ expect(getJob(jobId)?.phase).toBe("error");
+ expect(preview.events).not.toContain("thumbnail uploaded");
+ expect(preview.events).not.toContain("gif uploaded");
+ expect(preview.signals.length).toBe(2);
+ expect(preview.signals.every((signal) => signal.aborted)).toBe(true);
+ expect(preview.gifs.every((gif) => !existsSync(gif.path))).toBe(true);
+ });
+
+ test("releases the job slot and temp files when processing fails", async () => {
+ const before = getActiveVideoProcessCount();
+ let input: TempFileHandle | undefined;
+ spyOn(mediaVideo, "downloadVideoToTemp").mockImplementation(async () => {
+ input = await tempFileWith(".mp4", "input");
+ return input;
+ });
+ spyOn(mediaProbe, "probeVideoFile").mockResolvedValue({
+ ...metadata,
+ duration: 10,
+ });
+ spyOn(mediaVideo, "processVideo").mockRejectedValue(
+ new Error("FFmpeg exited with code 1"),
+ );
+
+ const response = await app.fetch(
+ post("/video/process", {
+ videoId: "video-failed-process",
+ userId: "user-id",
+ videoUrl: "https://example.com/input.mp4",
+ outputPresignedUrl: "https://example.com/output.mp4",
+ }),
+ );
+ const { jobId } = await response.json();
+ createdJobs.push(jobId);
+
+ expect(await waitForDrain(2_000)).toBe(true);
+ expect(getJob(jobId)?.phase).toBe("error");
+ expect(getActiveVideoProcessCount()).toBe(before);
+ expect(input && existsSync(input.path)).toBe(false);
+ });
+});
+
+describe("edit jobs", () => {
+ test("cancellation aborts the upload and skips preview work", async () => {
+ const files: TempFileHandle[] = [];
+ spyOn(mediaVideo, "downloadVideoToTemp").mockImplementation(async () => {
+ const input = await tempFileWith(".mp4", "input");
+ files.push(input);
+ return input;
+ });
+ spyOn(mediaProbe, "probeVideoFile").mockResolvedValue({
+ ...metadata,
+ duration: 10,
+ });
+ spyOn(mediaEdit, "renderEditedVideo").mockImplementation(async () => {
+ const output = await tempFileWith(".mp4", "edited");
+ files.push(output);
+ return output;
+ });
+ const thumbnail = spyOn(mediaVideo, "generateThumbnail");
+ const uploadStarted = deferred();
+ let uploadSignal: AbortSignal | undefined;
+ spyOn(mediaVideo, "uploadFileToS3").mockImplementation(
+ (_path, _url, _contentType, signal) => {
+ uploadSignal = signal;
+ uploadStarted.resolve();
+ return new Promise((_resolve, reject) => {
+ signal?.addEventListener("abort", () => reject(signal.reason), {
+ once: true,
+ });
+ });
+ },
+ );
+
+ const response = await app.fetch(
+ post("/video/edit", {
+ videoId: "video-cancelled-edit",
+ userId: "user-id",
+ sourceUrl: "https://example.com/source.mp4",
+ outputPresignedUrl: "https://example.com/output.mp4",
+ thumbnailPresignedUrl: "https://example.com/thumbnail.jpg",
+ keepRanges: [{ start: 0, end: 5 }],
+ }),
+ );
+ const { jobId } = await response.json();
+ createdJobs.push(jobId);
+ await uploadStarted.promise;
+
+ const cancel = await app.fetch(post(`/video/process/${jobId}/cancel`, {}));
+ expect(cancel.status).toBe(200);
+ expect(uploadSignal?.aborted).toBe(true);
+
+ expect(await waitForDrain(2_000)).toBe(true);
+ expect(getJob(jobId)?.phase).toBe("cancelled");
+ expect(thumbnail).not.toHaveBeenCalled();
+ expect(files).toHaveLength(2);
+ expect(files.every((handle) => !existsSync(handle.path))).toBe(true);
+ });
+});
+
+describe("direct conversions", () => {
+ test("release the slot when the download fails", async () => {
+ spyOn(mediaVideo, "downloadVideoToTemp").mockRejectedValue(
+ new Error("Failed to download video: 404"),
+ );
+
+ const response = await app.fetch(
+ post("/video/convert", { videoUrl: "https://example.com/missing.mp4" }),
+ );
+
+ expect(response.status).toBe(500);
+ expect(getActiveDirectVideoProcessCount()).toBe(0);
+ });
+
+ test("release the slot and temp files when the client cancels the body", async () => {
+ const files: TempFileHandle[] = [];
+ spyOn(mediaVideo, "downloadVideoToTemp").mockImplementation(async () => {
+ const input = await tempFileWith(".webm", "input");
+ files.push(input);
+ return input;
+ });
+ spyOn(mediaProbe, "probeVideoFile").mockResolvedValue({
+ ...metadata,
+ duration: 10,
+ });
+ spyOn(mediaVideo, "processVideo").mockImplementation(async () => {
+ const output = await tempFileWith(".mp4", "x".repeat(1024 * 1024));
+ files.push(output);
+ return output;
+ });
+
+ const response = await app.fetch(
+ post("/video/convert", { videoUrl: "https://example.com/input.webm" }),
+ );
+ expect(response.status).toBe(200);
+ expect(getActiveDirectVideoProcessCount()).toBe(1);
+
+ await response.body?.cancel();
+ await waitFor(() => getActiveDirectVideoProcessCount() === 0);
+ await waitFor(() => files.every((handle) => !existsSync(handle.path)));
+ });
+
+ test("release the slot after the body is fully read", async () => {
+ spyOn(mediaVideo, "downloadVideoToTemp").mockImplementation(() =>
+ tempFileWith(".webm", "input"),
+ );
+ spyOn(mediaProbe, "probeVideoFile").mockResolvedValue({
+ ...metadata,
+ duration: 10,
+ });
+ spyOn(mediaVideo, "processVideo").mockImplementation(() =>
+ tempFileWith(".mp4", "converted"),
+ );
+
+ const response = await app.fetch(
+ post("/video/convert", { videoUrl: "https://example.com/input.webm" }),
+ );
+
+ expect(await response.text()).toBe("converted");
+ await waitFor(() => getActiveDirectVideoProcessCount() === 0);
+ });
+});
diff --git a/apps/media-server/src/__tests__/routes/peer-jobs.test.ts b/apps/media-server/src/__tests__/routes/peer-jobs.test.ts
new file mode 100644
index 000000000..c9ef0e01c
--- /dev/null
+++ b/apps/media-server/src/__tests__/routes/peer-jobs.test.ts
@@ -0,0 +1,151 @@
+import {
+ afterAll,
+ afterEach,
+ beforeAll,
+ describe,
+ expect,
+ test,
+} from "bun:test";
+import app from "../../app";
+import { mediaServerPeers } from "../../lib/peer-jobs";
+
+const SECRET = "peer-test-secret";
+process.env.MEDIA_SERVER_WEBHOOK_SECRET = SECRET;
+
+const peerRequests: {
+ method: string;
+ peerLookup: string | null;
+ secret: string | null;
+}[] = [];
+let respond: (request: Request) => Response | Promise<Response> = () =>
+ new Response("not found", { status: 404 });
+let peer: ReturnType<typeof Bun.serve>;
+let missingPeer: ReturnType<typeof Bun.serve>;
+
+beforeAll(() => {
+ peer = Bun.serve({
+ hostname: "127.0.0.1",
+ port: 0,
+ fetch(request) {
+ peerRequests.push({
+ method: request.method,
+ peerLookup: request.headers.get("x-cap-peer-lookup"),
+ secret: request.headers.get("x-media-server-secret"),
+ });
+ return respond(request);
+ },
+ });
+ missingPeer = Bun.serve({
+ hostname: "127.0.0.1",
+ port: 0,
+ fetch: () => new Response("not found", { status: 404 }),
+ });
+ process.env.MEDIA_SERVER_PEERS = `http://127.0.0.1:${missingPeer.port}, http://127.0.0.1:${peer.port}/`;
+});
+
+afterEach(() => {
+ peerRequests.length = 0;
+ respond = () => new Response("not found", { status: 404 });
+});
+
+afterAll(async () => {
+ delete process.env.MEDIA_SERVER_PEERS;
+ await peer.stop(true);
+ await missingPeer.stop(true);
+});
+
+function statusRequest(jobId: string, headers: Record<string, string> = {}) {
+ return new Request(`http://localhost/video/process/${jobId}/status`, {
+ headers: { "x-media-server-secret": SECRET, ...headers },
+ });
+}
+
+describe("peer job lookup", () => {
+ test("parses configured peers and drops invalid entries", () => {
+ expect(
+ mediaServerPeers({
+ MEDIA_SERVER_PEERS:
+ "http://10.0.0.1:3456/, nonsense, ftp://x, https://b",
+ }),
+ ).toEqual(["http://10.0.0.1:3456", "https://b"]);
+ expect(mediaServerPeers({})).toEqual([]);
+ });
+
+ test("answers a status request with the peer that owns the job", async () => {
+ respond = () =>
+ Response.json({ jobId: "job-elsewhere", videoId: "v1", phase: "error" });
+ const response = await app.fetch(statusRequest("job-elsewhere"));
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({
+ jobId: "job-elsewhere",
+ videoId: "v1",
+ phase: "error",
+ });
+ expect(peerRequests).toHaveLength(1);
+ expect(peerRequests[0].peerLookup).toBe("1");
+ expect(peerRequests[0].secret).toBe(SECRET);
+ });
+
+ test("still answers not found when no peer has the job", async () => {
+ const response = await app.fetch(statusRequest("job-nowhere"));
+ expect(response.status).toBe(404);
+ expect((await response.json()).code).toBe("NOT_FOUND");
+ });
+
+ test("does not forward a lookup that already came from a peer", async () => {
+ respond = () => Response.json({ jobId: "job-loop", phase: "processing" });
+ const response = await app.fetch(
+ statusRequest("job-loop", { "x-cap-peer-lookup": "1" }),
+ );
+ expect(response.status).toBe(404);
+ expect(peerRequests).toHaveLength(0);
+ });
+
+ test("does not forward unauthenticated status reads", async () => {
+ respond = () => Response.json({ jobId: "job-public", phase: "processing" });
+ const response = await app.fetch(
+ new Request("http://localhost/video/process/job-public/status"),
+ );
+ expect(response.status).toBe(404);
+ expect(peerRequests).toHaveLength(0);
+ });
+
+ test("ignores a peer that rejects the shared secret", async () => {
+ respond = () => new Response("Unauthorized", { status: 401 });
+ const response = await app.fetch(statusRequest("job-rejected"));
+ expect(response.status).toBe(404);
+ });
+
+ test("waits for an owner that delivers its webhook before confirming a cancel", async () => {
+ respond = async (request) => {
+ if (request.method !== "POST")
+ return new Response("not found", { status: 404 });
+ await Bun.sleep(2_500);
+ return Response.json({ success: true, jobId: "job-slow-cancel" });
+ };
+ const response = await app.fetch(
+ new Request("http://localhost/video/process/job-slow-cancel/cancel", {
+ method: "POST",
+ headers: { "x-media-server-secret": SECRET },
+ }),
+ );
+ expect(response.status).toBe(200);
+ expect((await response.json()).success).toBe(true);
+ }, 10_000);
+
+ test("cancels a job on the peer that owns it", async () => {
+ respond = (request) =>
+ request.method === "POST"
+ ? Response.json({ success: true, jobId: "job-cancel" })
+ : new Response("not found", { status: 404 });
+ const response = await app.fetch(
+ new Request("http://localhost/video/process/job-cancel/cancel", {
+ method: "POST",
+ headers: { "x-media-server-secret": SECRET },
+ }),
+ );
+ expect(response.status).toBe(200);
+ expect((await response.json()).success).toBe(true);
+ expect(peerRequests.map((request) => request.method)).toEqual(["POST"]);
+ });
+});
diff --git a/apps/media-server/src/__tests__/routes/recording-verification.test.ts b/apps/media-server/src/__tests__/routes/recording-verification.test.ts
index 7d3beb673..7355cc15c 100644
--- a/apps/media-server/src/__tests__/routes/recording-verification.test.ts
+++ b/apps/media-server/src/__tests__/routes/recording-verification.test.ts
@@ -108,6 +108,17 @@ async function terminalWebhook(jobId: string): Promise<JobProgress> {
return payload;
}
+function captureFailureLogs() {
+ const info = spyOn(console, "info");
+ const events = () =>
+ info.mock.calls.flatMap(([line]) => {
+ if (typeof line !== "string" || !line.startsWith("{")) return [];
+ const event = JSON.parse(line) as Record<string, unknown>;
+ return event.event === "recording_verification_failed" ? [event] : [];
+ });
+ return { events, restore: () => info.mockRestore() };
+}
+
function waitForDecoderCancellation() {
let joined = false;
verify.mockImplementation(async (_input, options) => {
@@ -508,4 +519,94 @@ describe("asynchronous recording verification", () => {
expect(failed.metadata).toBeUndefined();
},
);
+
+ test("logs why permanent verification failed without leaking the presigned URL", async () => {
+ const logs = captureFailureLogs();
+ try {
+ verify.mockRejectedValue(
+ new Error(
+ "Recording full decode failed: https://bucket.s3.amazonaws.com/user-1/video-1/raw.mp4?X-Amz-Credential=AKIAEXAMPLE&X-Amz-Signature=deadbeef: Invalid data found when processing input",
+ ),
+ );
+ const jobId = await startJob();
+ const failed = await terminalWebhook(jobId);
+ expect(failed.errorCode).toBe("source-invalid");
+ const [event, ...rest] = logs.events();
+ expect(rest).toEqual([]);
+ expect(event).toMatchObject({
+ jobId,
+ videoId: body.videoId,
+ phase: "error",
+ errorCode: "source-invalid",
+ retryable: false,
+ stage: "decode",
+ reason:
+ "Recording full decode failed: <url> Invalid data found when processing input",
+ });
+ const line = JSON.stringify(event);
+ expect(line).not.toContain("amazonaws");
+ expect(line).not.toContain("X-Amz");
+ expect(line).not.toContain("deadbeef");
+ expect(line).not.toContain(body.videoUrl);
+ } finally {
+ logs.restore();
+ }
+ });
+
+ test("logs which uploaded metadata check failed", async () => {
+ const logs = captureFailureLogs();
+ try {
+ probe.mockResolvedValue({ ...metadata, fileSize: 64, duration: 2 });
+ const jobId = await startJob();
+ const failed = await terminalWebhook(jobId);
+ expect(failed.errorCode).toBe("source-invalid");
+ expect(logs.events()).toEqual([
+ expect.objectContaining({
+ jobId,
+ errorCode: "source-invalid",
+ retryable: false,
+ stage: "metadata",
+ reason: "Uploaded recording does not match the completed local file",
+ mismatch: {
+ expectedFileSize: 128,
+ actualFileSize: 64,
+ expectedDuration: 5,
+ actualDuration: 2,
+ requiredAudio: false,
+ audioCodec: "aac",
+ },
+ }),
+ ]);
+ expect(verify).not.toHaveBeenCalled();
+ } finally {
+ logs.restore();
+ }
+ });
+
+ test("logs the original probe error behind a retryable failure", async () => {
+ const logs = captureFailureLogs();
+ try {
+ probe.mockRejectedValue(
+ new Error(
+ "Media input is not accessible (503) for /tmp/cap-media-server/instance-1/user-1.mp4?X-Amz-Signature=deadbeef",
+ ),
+ );
+ const jobId = await startJob();
+ const failed = await terminalWebhook(jobId);
+ expect(failed.error).toBe(
+ "Recording verification temporarily unavailable (503)",
+ );
+ expect(logs.events()).toEqual([
+ expect.objectContaining({
+ jobId,
+ errorCode: "processing-unavailable",
+ retryable: true,
+ stage: "probe",
+ reason: "Media input is not accessible (503) for <path>",
+ }),
+ ]);
+ } finally {
+ logs.restore();
+ }
+ });
});
diff --git a/apps/media-server/src/__tests__/routes/video.test.ts b/apps/media-server/src/__tests__/routes/video.test.ts
index d2643b3bf..51c8f19e4 100644
--- a/apps/media-server/src/__tests__/routes/video.test.ts
+++ b/apps/media-server/src/__tests__/routes/video.test.ts
@@ -51,6 +51,8 @@ describe("GET /video/status", () => {
expect(data).toHaveProperty("canAcceptNewProbeProcess");
expect(data).toHaveProperty("jobCount");
expect(data).toHaveProperty("jobs");
+ expect(data.loomMultipartUploads).toBe(true);
+ expect(data.loomImportMaxBytes).toBe(10 * 1024 ** 3);
expect(Array.isArray(data.jobs)).toBe(true);
});
});
@@ -404,6 +406,44 @@ describe("POST /video/process", () => {
mock.restore();
});
+ test("accepts a Loom multipart original without a single-PUT original URL", async () => {
+ spyOn(jobManager, "canAcceptNewVideoProcess").mockReturnValue(false);
+ const response = await app.fetch(
+ videoPostRequest("/video/import", {
+ videoId: "test-id",
+ userId: "user-id",
+ videoUrl: "https://example.com/video.mp4",
+ outputPresignedUrl: "https://s3.example.com/output",
+ maxImportBytes: 10 * 1024 ** 3,
+ sourceUpload: {
+ type: "multipart",
+ videoId: "test-id",
+ loomImportAttemptId: "run:0",
+ key: "user-id/test-id/raw-upload.mp4",
+ uploadId: "upload-id",
+ partSize: 64 * 1024 ** 2,
+ signPartUrl: "https://cap.example.com/sign",
+ completeUrl: "https://cap.example.com/complete",
+ abortUrl: "https://cap.example.com/abort",
+ },
+ }),
+ );
+ expect(response.status).toBe(503);
+ expect((await response.json()).code).toBe("SERVER_BUSY");
+ });
+
+ test("rejects an import without an original upload target", async () => {
+ const response = await app.fetch(
+ videoPostRequest("/video/import", {
+ videoId: "test-id",
+ userId: "user-id",
+ videoUrl: "https://example.com/video.mp4",
+ outputPresignedUrl: "https://s3.example.com/output",
+ }),
+ );
+ expect(response.status).toBe(400);
+ });
+
test("returns 400 for missing required fields", async () => {
const response = await app.fetch(videoPostRequest("/video/process", {}));
diff --git a/apps/media-server/src/app.ts b/apps/media-server/src/app.ts
index e50979955..162a7652d 100644
--- a/apps/media-server/src/app.ts
+++ b/apps/media-server/src/app.ts
@@ -1,5 +1,6 @@
import { Hono } from "hono";
import { logger } from "hono/logger";
+import { beginRequest } from "./lib/lifecycle";
import audio from "./routes/audio";
import health from "./routes/health";
import video from "./routes/video";
@@ -7,6 +8,15 @@ import video from "./routes/video";
const app = new Hono();
app.use("*", logger());
+app.use("*", async (c, next) => {
+ if (c.req.method === "GET" || c.req.method === "HEAD") return await next();
+ const end = beginRequest();
+ try {
+ await next();
+ } finally {
+ end();
+ }
+});
app.route("/health", health);
app.route("/audio", audio);
diff --git a/apps/media-server/src/index.ts b/apps/media-server/src/index.ts
index 7a5b2151d..05dbdac4f 100644
--- a/apps/media-server/src/index.ts
+++ b/apps/media-server/src/index.ts
@@ -1,38 +1,52 @@
import app from "./app";
import { abortAllJobs } from "./lib/job-manager";
+import { createShutdownHandler, getIdleTimeoutSeconds } from "./lib/lifecycle";
import { cancelAllMediaOperations } from "./lib/media-operations";
-import { cleanupMediaTransferCache } from "./lib/media-transfer";
+import {
+ cleanupMediaTransferCache,
+ sweepMediaTransferCache,
+} from "./lib/media-transfer";
+import { removeOwnTempDir, startTempDirMaintenance } from "./lib/temp-files";
const port = Number(process.env.PORT) || 3456;
console.log(`[media-server] Starting on port ${port}`);
-let shuttingDown = false;
+void Promise.allSettled([
+ startTempDirMaintenance(),
+ sweepMediaTransferCache(),
+]).then(([temp, cache]) => {
+ const removed =
+ (temp.status === "fulfilled" ? temp.value : 0) +
+ (cache.status === "fulfilled" ? cache.value : 0);
+ if (removed > 0) {
+ console.log(`[media-server] Removed ${removed} orphaned temp entries`);
+ }
+});
-const shutdown = async () => {
- if (shuttingDown) return;
- shuttingDown = true;
- console.log("[media-server] Shutting down...");
+const shutdown = createShutdownHandler(async () => {
const abortedJobs = await abortAllJobs();
if (abortedJobs > 0) {
console.log(`[media-server] Aborted ${abortedJobs} active jobs`);
}
await cancelAllMediaOperations();
await cleanupMediaTransferCache();
+ await removeOwnTempDir();
process.exit(0);
-};
+});
process.on("SIGINT", () => {
- void shutdown();
+ void shutdown("SIGINT");
});
process.on("SIGTERM", () => {
- void shutdown();
+ void shutdown("SIGTERM");
});
process.on("SIGHUP", () => {
- void shutdown();
+ void shutdown("SIGHUP");
});
export default {
port,
+ idleTimeout: getIdleTimeoutSeconds(),
fetch: app.fetch,
};
diff --git a/apps/media-server/src/lib/audio-quality.ts b/apps/media-server/src/lib/audio-quality.ts
index d51fe5b76..455824b25 100644
--- a/apps/media-server/src/lib/audio-quality.ts
+++ b/apps/media-server/src/lib/audio-quality.ts
@@ -1,7 +1,6 @@
import { createHash } from "node:crypto";
import { createReadStream } from "node:fs";
import { lstat, mkdtemp, rm } from "node:fs/promises";
-import { tmpdir } from "node:os";
import { isAbsolute, join } from "node:path";
import { spawn } from "bun";
import { z } from "zod";
@@ -13,6 +12,7 @@ import {
} from "./audio-quality-policy";
import { proveRecordingPackets } from "./recording-packet-proof";
import { registerSubprocess, unregisterSubprocess } from "./subprocess";
+import { ensureTempDir, getTempDir } from "./temp-files";
const streamSchema = z.object({
codec_type: z.string(),
@@ -307,7 +307,8 @@ export async function createAudioQualityCandidate(
Math.abs(videoDuration - input.duration) > 0.1
)
return { status: "unchanged", reason: "source-duration-mismatch" };
- directory = await mkdtemp(join(tmpdir(), "cap-audio-quality-"));
+ await ensureTempDir();
+ directory = await mkdtemp(join(getTempDir(), "audio-quality-"));
let path = join(directory, "candidate.mp4");
const renderArgs = [
"ffmpeg",
diff --git a/apps/media-server/src/lib/auth.ts b/apps/media-server/src/lib/auth.ts
index a8e322d28..9641d67c4 100644
--- a/apps/media-server/src/lib/auth.ts
+++ b/apps/media-server/src/lib/auth.ts
@@ -12,3 +12,17 @@ export function validateMediaServerSecret(c: MediaServerContext): boolean {
}
return c.req.header("x-media-server-secret") === secret;
}
+
+export function hasMediaServerSecret(c: MediaServerContext): boolean {
+ return (
+ c.req.header("x-media-server-secret") !== undefined &&
+ validateMediaServerSecret(c)
+ );
+}
+
+export function canReadStatus(c: MediaServerContext): boolean {
+ const publicStatus = process.env.MEDIA_SERVER_PUBLIC_STATUS?.toLowerCase();
+ return publicStatus === "false" || publicStatus === "0"
+ ? validateMediaServerSecret(c)
+ : true;
+}
diff --git a/apps/media-server/src/lib/ffmpeg-errors.ts b/apps/media-server/src/lib/ffmpeg-errors.ts
new file mode 100644
index 000000000..b9b8998d4
--- /dev/null
+++ b/apps/media-server/src/lib/ffmpeg-errors.ts
@@ -0,0 +1,30 @@
+const MAX_FFMPEG_ERROR_LINES = 3;
+const ERROR_LINE_PATTERN =
+ /error|invalid|failed|unsupported|cannot|unknown|unrecognized|not found|no such|no capable|reconfiguring/i;
+const PROGRESS_LINE_PATTERN = /^\s*[\w.]+=\S*\s*$/;
+
+export class FfmpegExitError extends Error {
+ constructor(
+ message: string,
+ readonly errorLines: string[],
+ ) {
+ super(message);
+ }
+}
+
+function isFfmpegErrorLine(line: string): boolean {
+ return ERROR_LINE_PATTERN.test(line) && !PROGRESS_LINE_PATTERN.test(line);
+}
+
+export function collectFfmpegErrorLine(errorLines: string[], line: string) {
+ if (errorLines.length < MAX_FFMPEG_ERROR_LINES && isFfmpegErrorLine(line))
+ errorLines.push(line.trim());
+}
+
+export function ffmpegErrorLines(stderr: string): string[] {
+ const errorLines: string[] = [];
+ for (const line of stderr.split(/\r\n|\r|\n/)) {
+ collectFfmpegErrorLine(errorLines, line);
+ }
+ return errorLines;
+}
diff --git a/apps/media-server/src/lib/job-manager.ts b/apps/media-server/src/lib/job-manager.ts
index 74caa0dea..5c05238f2 100644
--- a/apps/media-server/src/lib/job-manager.ts
+++ b/apps/media-server/src/lib/job-manager.ts
@@ -124,6 +124,7 @@ export interface Job {
webhookLastAttemptAt?: number;
recordingVerificationDeadlineAt?: number;
recordingProcessingDeadlineAt?: number;
+ lifetimeDeadlineAt?: number;
recordingVerification?: RecordingVerificationProof;
manifestSha256?: string;
jobId: string;
@@ -147,9 +148,17 @@ export interface Job {
}
const jobs = new Map<string, Job>();
+const jobTotals = {
+ created: 0,
+ completed: 0,
+ errored: 0,
+ cancelled: 0,
+ expired: 0,
+};
const JOB_TTL_MS = 60 * 60 * 1000;
const STALE_JOB_MS = 15 * 60 * 1000;
const MAX_JOB_LIFETIME_MS = 60 * 60 * 1000;
+const MAX_EXTENDED_JOB_LIFETIME_MS = 4 * 60 * 60 * 1000;
const MAX_RECORDING_PROCESSING_BUDGET_MS = 3 * 60 * 60 * 1000;
const WEBHOOK_MAX_ATTEMPTS = 3;
const WEBHOOK_RETRY_BASE_MS = 500;
@@ -366,9 +375,14 @@ export function createJob(
webhookSecret,
};
jobs.set(jobId, job);
+ jobTotals.created++;
return job;
}
+export function getJobTotals(): typeof jobTotals {
+ return { ...jobTotals };
+}
+
export function getJob(jobId: string): Job | undefined {
return jobs.get(jobId);
}
@@ -410,12 +424,37 @@ export function updateJob(
job.terminalAt ??= job.updatedAt;
if (job.recordingWorkerLeaseTimer)
clearTimeout(job.recordingWorkerLeaseTimer);
+ if (job.phase === "complete") jobTotals.completed++;
+ else if (job.phase === "error") jobTotals.errored++;
+ else jobTotals.cancelled++;
}
if (!isActivePhase(job.phase) && job.webhookInFlight)
job.webhookPending = true;
return job;
}
+export function extendJobLifetime(
+ jobId: string,
+ remainingBudgetMs: number,
+): number | undefined {
+ const job = jobs.get(jobId);
+ if (
+ !job ||
+ !isActivePhase(job.phase) ||
+ !Number.isFinite(remainingBudgetMs) ||
+ remainingBudgetMs <= 0
+ )
+ return undefined;
+ job.lifetimeDeadlineAt = Math.min(
+ job.createdAt + MAX_EXTENDED_JOB_LIFETIME_MS,
+ Math.max(
+ job.lifetimeDeadlineAt ?? job.createdAt + MAX_JOB_LIFETIME_MS,
+ Date.now() + remainingBudgetMs,
+ ),
+ );
+ return job.lifetimeDeadlineAt;
+}
+
export function touchJob(jobId: string): Job | undefined {
const job = jobs.get(jobId);
if (!job) return undefined;
@@ -556,11 +595,14 @@ export function cleanupExpiredJobs(): number {
`[job-manager] Cleaning up expired job ${jobId} (phase=${job.phase}, age=${Math.round(age / 60000)}m)`,
);
job.abortController?.abort();
- updateJob(jobId, {
- phase: "error",
- error: `Job expired: no progress update for ${Math.round(staleness / 60000)} minutes`,
- message: "Processing failed (expired)",
- });
+ if (
+ updateJob(jobId, {
+ phase: "error",
+ error: `Job expired: no progress update for ${Math.round(staleness / 60000)} minutes`,
+ message: "Processing failed (expired)",
+ })
+ )
+ jobTotals.expired++;
void sendWebhook(job);
}
cleaned++;
@@ -572,34 +614,40 @@ export function cleanupExpiredJobs(): number {
`[job-manager] Marking stale job ${jobId} as error (phase=${job.phase}, no update for ${Math.round(staleness / 60000)}m)`,
);
job.abortController?.abort();
- updateJob(jobId, {
- phase: "error",
- error: `Job stale: no progress update for ${Math.round(staleness / 60000)} minutes`,
- message: "Processing failed (stale)",
- });
+ if (
+ updateJob(jobId, {
+ phase: "error",
+ error: `Job stale: no progress update for ${Math.round(staleness / 60000)} minutes`,
+ message: "Processing failed (stale)",
+ })
+ )
+ jobTotals.expired++;
void sendWebhook(job);
cleaned++;
continue;
}
+ const lifetimeDeadline =
+ job.lifetimeDeadlineAt ?? job.createdAt + MAX_JOB_LIFETIME_MS;
const deadline =
job.recordingVerificationDeadlineAt ??
job.recordingProcessingDeadlineAt ??
- job.createdAt + MAX_JOB_LIFETIME_MS;
+ lifetimeDeadline;
if (isActivePhase(job.phase) && now > deadline) {
console.warn(
`[job-manager] Marking long-running job ${jobId} as error (phase=${job.phase}, age=${Math.round(age / 60000)}m)`,
);
job.abortController?.abort();
- updateJob(jobId, {
+ const expired = updateJob(jobId, {
phase: "error",
error:
job.recordingVerificationDeadlineAt === undefined &&
job.recordingProcessingDeadlineAt === undefined
- ? `Job exceeded maximum lifetime of ${Math.round(MAX_JOB_LIFETIME_MS / 60000)} minutes`
+ ? `Job exceeded maximum lifetime of ${Math.round((lifetimeDeadline - job.createdAt) / 60000)} minutes`
: "Recording verification timed out",
message: "Processing failed (timeout)",
});
+ if (expired) jobTotals.expired++;
void sendWebhook(job);
cleaned++;
continue;
diff --git a/apps/media-server/src/lib/lifecycle.ts b/apps/media-server/src/lib/lifecycle.ts
new file mode 100644
index 000000000..fd60e537a
--- /dev/null
+++ b/apps/media-server/src/lib/lifecycle.ts
@@ -0,0 +1,192 @@
+import { getActiveVideoProcessCount } from "./job-manager";
+import {
+ getActiveAudioOperationCount,
+ getActiveProbeOperationCount,
+} from "./media-operations";
+import { getActiveSubprocessCount } from "./subprocess";
+
+const DRAIN_POLL_INTERVAL_MS = 250;
+const DRAIN_LOG_INTERVAL_MS = 30_000;
+export const DRAIN_RETRY_AFTER_SECONDS = 15;
+export const DRAINING_DETAILS =
+ "Server is draining for shutdown, please retry later";
+
+const MAX_IDLE_TIMEOUT_SECONDS = 255;
+
+let draining = false;
+let inFlightRequests = 0;
+const backgroundTasks = new Set<Promise<unknown>>();
+
+// Bun closes a connection after idleTimeout seconds without I/O, including
+// while a handler is still working, and its 10 s default is shorter than the
+// AWS ALB keep-alive idle timeout (60 s), which turns reused sockets into 502s.
+export function getIdleTimeoutSeconds(): number {
+ const value = Number.parseInt(
+ process.env.MEDIA_SERVER_IDLE_TIMEOUT_SECONDS ?? "",
+ 10,
+ );
+ return Number.isFinite(value) && value >= 0
+ ? Math.min(MAX_IDLE_TIMEOUT_SECONDS, value)
+ : MAX_IDLE_TIMEOUT_SECONDS;
+}
+
+// A streamed response can stay silent while ffmpeg opens its input, so the
+// idle timeout only comes back once the first bytes are on their way.
+export function restoreIdleTimeoutOnFirstChunk(
+ stream: ReadableStream<Uint8Array>,
+ restore: () => void,
+): ReadableStream<Uint8Array> {
+ let restored = false;
+ return stream.pipeThrough(
+ new TransformStream<Uint8Array, Uint8Array>({
+ transform(chunk, controller) {
+ if (!restored) {
+ restored = true;
+ restore();
+ }
+ controller.enqueue(chunk);
+ },
+ }),
+ );
+}
+
+type IdleTimeoutServer = {
+ timeout?: (request: Request, seconds: number) => void;
+};
+
+export function setRequestIdleTimeout(
+ server: unknown,
+ request: Request,
+ seconds: number,
+): void {
+ const timeout = (server as IdleTimeoutServer | undefined)?.timeout;
+ if (typeof timeout !== "function") return;
+ try {
+ timeout.call(server, request, seconds);
+ } catch {}
+}
+
+export function getShutdownGraceMs(): number {
+ const value = Number.parseInt(
+ process.env.MEDIA_SERVER_SHUTDOWN_GRACE_MS ?? "0",
+ 10,
+ );
+ return Number.isFinite(value) && value > 0 ? value : 0;
+}
+
+export function isDraining(): boolean {
+ return draining;
+}
+
+export function setDraining(value: boolean): void {
+ draining = value;
+}
+
+export function trackBackgroundTask<T>(task: Promise<T>): Promise<T> {
+ backgroundTasks.add(task);
+ const settle = () => {
+ backgroundTasks.delete(task);
+ };
+ task.then(settle, settle);
+ return task;
+}
+
+export function beginRequest(): () => void {
+ inFlightRequests++;
+ let ended = false;
+ return () => {
+ if (ended) return;
+ ended = true;
+ inFlightRequests--;
+ };
+}
+
+export function getDrainBlockers() {
+ return {
+ activeVideoProcesses: getActiveVideoProcessCount(),
+ backgroundTasks: backgroundTasks.size,
+ inFlightRequests,
+ audioOperations: getActiveAudioOperationCount(),
+ probeOperations: getActiveProbeOperationCount(),
+ subprocesses: getActiveSubprocessCount(),
+ };
+}
+
+export function isIdle(): boolean {
+ return Object.values(getDrainBlockers()).every((count) => count === 0);
+}
+
+function wait(ms: number, signal?: AbortSignal): Promise<void> {
+ return new Promise((resolve) => {
+ const done = () => {
+ clearTimeout(timer);
+ signal?.removeEventListener("abort", done);
+ resolve();
+ };
+ const timer = setTimeout(done, ms);
+ signal?.addEventListener("abort", done, { once: true });
+ });
+}
+
+export async function waitForDrain(
+ graceMs: number,
+ signal?: AbortSignal,
+): Promise<boolean> {
+ const startedAt = Date.now();
+ const deadline = startedAt + graceMs;
+ let lastLogAt = startedAt;
+ while (!isIdle()) {
+ const now = Date.now();
+ if (signal?.aborted || now >= deadline) return false;
+ if (now - lastLogAt >= DRAIN_LOG_INTERVAL_MS) {
+ lastLogAt = now;
+ console.log("[media-server] Draining", {
+ remainingMs: deadline - now,
+ ...getDrainBlockers(),
+ });
+ }
+ await wait(Math.min(DRAIN_POLL_INTERVAL_MS, deadline - now), signal);
+ }
+ return true;
+}
+
+export function createShutdownHandler(
+ finalize: () => Promise<void>,
+ graceMs = getShutdownGraceMs,
+): (signal: string) => Promise<void> {
+ let shuttingDown = false;
+ let forceDrain: AbortController | undefined;
+
+ return async (signal) => {
+ if (forceDrain) {
+ console.log(
+ `[media-server] Received ${signal} while draining, shutting down now`,
+ );
+ forceDrain.abort();
+ return;
+ }
+ if (shuttingDown) return;
+ shuttingDown = true;
+
+ const drainMs = graceMs();
+ if (drainMs > 0) {
+ forceDrain = new AbortController();
+ setDraining(true);
+ console.log(
+ `[media-server] Received ${signal}, draining for up to ${drainMs}ms`,
+ getDrainBlockers(),
+ );
+ const drained = await waitForDrain(drainMs, forceDrain.signal);
+ console.log(
+ drained
+ ? "[media-server] Drain complete"
+ : "[media-server] Drain ended with work still active",
+ getDrainBlockers(),
+ );
+ } else {
+ console.log("[media-server] Shutting down...");
+ }
+
+ await finalize();
+ };
+}
diff --git a/apps/media-server/src/lib/log-redaction.ts b/apps/media-server/src/lib/log-redaction.ts
new file mode 100644
index 000000000..bf30f4bca
--- /dev/null
+++ b/apps/media-server/src/lib/log-redaction.ts
@@ -0,0 +1,30 @@
+const MAX_LOG_REASON_LENGTH = 300;
+
+const REDACTIONS: [RegExp, string][] = [
+ [/[a-z][a-z0-9+.-]*:\/\/\S+/gi, "<url>"],
+ [/\b[\w.-]+\.amazonaws\.com\S*/gi, "<url>"],
+ [/X-Amz-[A-Za-z-]+=[^\s&]*/gi, "<redacted>"],
+ [/\?[^\s?=]+=\S*/g, "?<redacted>"],
+ [/\bBearer\s+\S+/gi, "Bearer <redacted>"],
+ [
+ /\b(token|secret|signature|password|credential|api[_-]?key|access[_-]?key)(\s*[=:]\s*)\S+/gi,
+ "$1$2<redacted>",
+ ],
+ [/(?<![\w.~-])\/(?:[^\s/:'"]+\/)+[^\s/:'",)]*/g, "<path>"],
+];
+
+export function redactLogText(text: string): string {
+ let redacted = text;
+ for (const [pattern, replacement] of REDACTIONS) {
+ redacted = redacted.replace(pattern, replacement);
+ }
+ return redacted.replace(/\s+/g, " ").trim();
+}
+
+export function logReason(
+ error: unknown,
+ maxLength = MAX_LOG_REASON_LENGTH,
+): string {
+ const message = error instanceof Error ? error.message : String(error);
+ return redactLogText(message).slice(0, maxLength) || "unknown error";
+}
diff --git a/apps/media-server/src/lib/media-audio.ts b/apps/media-server/src/lib/media-audio.ts
index 5e438acbd..eeb2096c0 100644
--- a/apps/media-server/src/lib/media-audio.ts
+++ b/apps/media-server/src/lib/media-audio.ts
@@ -399,10 +399,12 @@ export function extractAudioStream(
let timeoutId: ReturnType<typeof setTimeout> | undefined;
let cleaned = false;
let reader: ReadableStreamDefaultReader<Uint8Array> | null = null;
+ let streamController: ReadableStreamDefaultController<Uint8Array> | undefined;
- const cleanup = () => {
+ const finish = (error?: Error) => {
if (cleaned) return;
cleaned = true;
+ if (error) streamController?.error(error);
if (timeoutId) clearTimeout(timeoutId);
if (reader) {
try {
@@ -414,22 +416,31 @@ export function extractAudioStream(
unregisterMediaOperation(operation);
void terminateProcess(proc);
};
+ const cleanup = () => finish(new Error("Audio extraction cancelled"));
timeoutId = setTimeout(() => {
- cleanup();
+ finish(new Error(`Operation timed out after ${opts.timeoutMs}ms`));
}, opts.timeoutMs);
void drainStream(proc.stderr as ReadableStream<Uint8Array>);
- proc.exited.then(() => {
- cleanup();
+ proc.exited.then((exitCode) => {
+ if (cleaned) return;
+ if (exitCode !== 0) {
+ finish(new Error(`FFmpeg exited with code ${exitCode}`));
+ return;
+ }
+ if (timeoutId) clearTimeout(timeoutId);
+ unregisterMediaOperation(operation);
+ void terminateProcess(proc);
});
const originalStream = proc.stdout as ReadableStream<Uint8Array>;
const stream = new ReadableStream<Uint8Array>(
{
- start() {
+ start(controller) {
+ streamController = controller;
reader = originalStream.getReader();
},
async pull(controller) {
@@ -440,15 +451,18 @@ export function extractAudioStream(
try {
const { done, value } = await reader.read();
+ if (cleaned) return;
if (done) {
+ await proc.exited;
+ if (cleaned) return;
controller.close();
- cleanup();
+ finish();
} else {
controller.enqueue(value);
}
} catch (err) {
controller.error(err);
- cleanup();
+ finish();
}
},
cancel() {
diff --git a/apps/media-server/src/lib/media-common.ts b/apps/media-server/src/lib/media-common.ts
index ad3c69224..875a8faba 100644
--- a/apps/media-server/src/lib/media-common.ts
+++ b/apps/media-server/src/lib/media-common.ts
@@ -32,6 +32,20 @@ export const DOWNLOAD_TIMEOUT_MS = 10 * 60 * 1000;
export const UPLOAD_TIMEOUT_MS = 10 * 60 * 1000;
const mediaFetch: typeof fetch = globalThis.fetch.bind(globalThis);
+export function decodeThreadsFromEnv(
+ name: string,
+ fallback: number,
+ env: Record<string, string | undefined> = process.env,
+): string {
+ const raw = env[name]?.trim();
+ if (!raw) return String(fallback);
+ const value = Number(raw);
+ if (!Number.isInteger(value) || value < 0 || value > 64) {
+ return String(fallback);
+ }
+ return String(value);
+}
+
export async function withTimeout<T>(
promise: Promise<T>,
timeoutMs: number,
diff --git a/apps/media-server/src/lib/media-edit.ts b/apps/media-server/src/lib/media-edit.ts
index aa8b69c53..88e65bf9c 100644
--- a/apps/media-server/src/lib/media-edit.ts
+++ b/apps/media-server/src/lib/media-edit.ts
@@ -1,6 +1,15 @@
import { writeFile } from "node:fs/promises";
import { file, spawn } from "bun";
+import { FfmpegExitError, ffmpegErrorLines } from "./ffmpeg-errors";
import type { VideoMetadata } from "./job-manager";
+import {
+ buildNvencEncodeArgs,
+ logNvencFallback,
+ type NvencOptions,
+ nvencFallbackReason,
+ nvencOptionsFromEnv,
+ recordNvencAttempt,
+} from "./nvenc-encoder";
import { registerSubprocess, terminateProcess } from "./subprocess";
import { createTempFile, type TempFileHandle } from "./temp-files";
@@ -24,6 +33,7 @@ const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000;
const MAX_TIMEOUT_MS = 60 * 60 * 1000;
const DEFAULT_OUTPUT_FPS = 30;
const MAX_TRANSCODE_RANGES_PER_BATCH = 4;
+const EDIT_CRF = 18;
function roundTime(value: number) {
return Math.round(value * 1000) / 1000;
@@ -37,7 +47,7 @@ function getTotalRangeDuration(ranges: EditRange[]) {
return ranges.reduce((total, range) => total + getRangeDuration(range), 0);
}
-function getTimeoutMs(ranges: EditRange[]) {
+export function getTimeoutMs(ranges: EditRange[]) {
const durationMs = getTotalRangeDuration(ranges) * 20_000;
return Math.min(MAX_TIMEOUT_MS, Math.max(DEFAULT_TIMEOUT_MS, durationMs));
}
@@ -154,6 +164,7 @@ export function buildTranscodeEditArgs(
outputPath: string,
hasAudio: boolean,
fps = DEFAULT_OUTPUT_FPS,
+ nvenc: NvencOptions | null = null,
) {
if (ranges.length === 0 || ranges.length > MAX_TRANSCODE_RANGES_PER_BATCH) {
throw new Error(
@@ -187,12 +198,9 @@ export function buildTranscodeEditArgs(
[...filters, concat].join(";"),
"-map",
"[v]",
- "-c:v",
- "libx264",
- "-preset",
- "fast",
- "-crf",
- "18",
+ ...(nvenc
+ ? buildNvencEncodeArgs(nvenc, EDIT_CRF)
+ : ["-c:v", "libx264", "-preset", "fast", "-crf", String(EDIT_CRF)]),
"-pix_fmt",
"yuv420p",
"-enc_time_base:v",
@@ -319,8 +327,9 @@ async function runFfmpegCommand(
]);
if (exitCode !== 0) {
- throw new Error(
+ throw new FfmpegExitError(
`FFmpeg exited with code ${exitCode}. Last stderr: ${stderrText.slice(-2000)}`,
+ ffmpegErrorLines(stderrText),
);
}
})(),
@@ -398,6 +407,7 @@ async function renderTranscodedEdit(
hasAudio: boolean,
fps: number | undefined,
timeoutMs: number,
+ nvenc: NvencOptions | null,
onProgress?: ProgressCallback,
abortSignal?: AbortSignal,
) {
@@ -411,7 +421,14 @@ async function renderTranscodedEdit(
const batchFile = await createTempFile(".mp4");
batchFiles.push(batchFile);
await runFfmpegCommand(
- buildTranscodeEditArgs(inputPath, batch, batchFile.path, hasAudio, fps),
+ buildTranscodeEditArgs(
+ inputPath,
+ batch,
+ batchFile.path,
+ hasAudio,
+ fps,
+ nvenc,
+ ),
getRemainingTimeoutMs(startedAt, timeoutMs),
abortSignal,
);
@@ -456,14 +473,36 @@ export async function renderEditedVideo({
}
const timeoutMs = getTimeoutMs(normalizedRanges);
+ const render = (nvenc: NvencOptions | null) =>
+ renderTranscodedEdit(
+ inputPath,
+ normalizedRanges,
+ Boolean(metadata.audioCodec),
+ metadata.fps,
+ timeoutMs,
+ nvenc,
+ onProgress,
+ abortSignal,
+ );
- return await renderTranscodedEdit(
- inputPath,
- normalizedRanges,
- Boolean(metadata.audioCodec),
- metadata.fps,
- timeoutMs,
- onProgress,
- abortSignal,
- );
+ const nvenc = nvencOptionsFromEnv();
+ if (nvenc) {
+ try {
+ const outputFile = await render(nvenc);
+ recordNvencAttempt("edit");
+ return outputFile;
+ } catch (error) {
+ const reason = nvencFallbackReason(error, abortSignal?.aborted ?? false);
+ if (reason === null) throw error;
+ recordNvencAttempt("edit", reason);
+ logNvencFallback({
+ job: "edit",
+ sourceCodec: metadata.videoCodec,
+ gpuDecode: false,
+ reason,
+ });
+ }
+ }
+
+ return await render(null);
}
diff --git a/apps/media-server/src/lib/media-operations.ts b/apps/media-server/src/lib/media-operations.ts
index 1c9901dd8..c1af41209 100644
--- a/apps/media-server/src/lib/media-operations.ts
+++ b/apps/media-server/src/lib/media-operations.ts
@@ -30,6 +30,14 @@ export function getActiveAudioOperationCount(): number {
return count;
}
+export function getMaxConcurrentAudioOperations(): number {
+ return MAX_CONCURRENT_AUDIO_OPERATIONS;
+}
+
+export function getMaxConcurrentProbeOperations(): number {
+ return MAX_CONCURRENT_PROBE_OPERATIONS;
+}
+
export function canAcceptNewAudioOperation(): boolean {
return (
!hasCriticalMemoryPressure() &&
diff --git a/apps/media-server/src/lib/media-transfer.ts b/apps/media-server/src/lib/media-transfer.ts
index 627207793..a74cbc409 100644
--- a/apps/media-server/src/lib/media-transfer.ts
+++ b/apps/media-server/src/lib/media-transfer.ts
@@ -1,9 +1,15 @@
import { AsyncLocalStorage } from "node:async_hooks";
import { createHash } from "node:crypto";
-import { mkdir, open, readdir, rm, stat } from "node:fs/promises";
+import { open, rm, stat } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { z } from "zod";
+import {
+ ensureInstanceDir,
+ getInstanceDir,
+ releaseInstanceDir,
+ sweepOrphanedInstanceDirs,
+} from "./temp-files";
const targetSchema = z.object({
version: z.literal(1),
@@ -36,46 +42,59 @@ const transfers = new AsyncLocalStorage<TransferContext>();
const cache = new Map<string, CachedFile>();
const pendingTransfers = new Map<string, Promise<void>>();
const cacheBase = join(tmpdir(), "cap-media-download-cache");
-const cacheRoot = join(cacheBase, String(process.pid));
+const cacheRoot = getInstanceDir(cacheBase);
const CACHE_BYTES = 2 * 1024 ** 3;
const CACHE_TTL_MS = 30 * 60_000;
let cacheBytes = 0;
let lastPruneAt = 0;
-async function initializeCache() {
- await mkdir(cacheRoot, { recursive: true, mode: 0o700 });
- for (const entry of await readdir(cacheBase, { withFileTypes: true })) {
- if (
- !entry.isDirectory() ||
- !/^\d+$/.test(entry.name) ||
- entry.name === String(process.pid)
- )
- continue;
- try {
- process.kill(Number(entry.name), 0);
- } catch (error) {
- if (
- typeof error === "object" &&
- error !== null &&
- "code" in error &&
- error.code === "ESRCH"
- )
- await rm(join(cacheBase, entry.name), { recursive: true, force: true });
- }
+// Pre-instance-dir caches are named after their owner's pid. Only remove one
+// once that process is confirmed gone; an old server sharing this volume during
+// a rolling deploy may still be reading from its cache.
+function isLegacyCacheOrphan(name: string) {
+ if (!/^\d+$/.test(name)) return false;
+ if (name === String(process.pid)) return false;
+ try {
+ process.kill(Number(name), 0);
+ return false;
+ } catch (error) {
+ return (
+ typeof error === "object" &&
+ error !== null &&
+ "code" in error &&
+ error.code === "ESRCH"
+ );
}
}
+
+export function sweepMediaTransferCache(): Promise<number> {
+ return sweepOrphanedInstanceDirs(cacheBase, isLegacyCacheOrphan);
+}
+
+async function initializeCache() {
+ await ensureInstanceDir(cacheBase, 0o700);
+ await sweepMediaTransferCache();
+}
let cacheReady: Promise<void> | undefined;
const pruneTimer = setInterval(() => {
void pruneCache().catch(() => {});
}, 60_000);
pruneTimer.unref?.();
+const sweepTimer = setInterval(() => {
+ void sweepMediaTransferCache().catch(() => {});
+}, 5 * 60_000);
+sweepTimer.unref?.();
export async function cleanupMediaTransferCache() {
- await rm(cacheRoot, { recursive: true, force: true }).catch(() => {});
+ await releaseInstanceDir(cacheBase);
cache.clear();
cacheBytes = 0;
cacheReady = undefined;
}
+export function getMediaTransferCacheUsage() {
+ return { bytes: cacheBytes, entries: cache.size };
+}
+
export function isInternalMediaUrl(input: string): boolean {
try {
const url = new URL(input);
diff --git a/apps/media-server/src/lib/media-video.ts b/apps/media-server/src/lib/media-video.ts
index 481589793..da16eceb2 100644
--- a/apps/media-server/src/lib/media-video.ts
+++ b/apps/media-server/src/lib/media-video.ts
@@ -10,9 +10,11 @@ import {
remainingTimeoutMs,
} from "./capcodec-encoder";
import { uploadDriveResumable } from "./drive-resumable-upload";
+import { collectFfmpegErrorLine, FfmpegExitError } from "./ffmpeg-errors";
import type { VideoMetadata } from "./job-manager";
import {
DOWNLOAD_TIMEOUT_MS,
+ decodeThreadsFromEnv,
normalizeLocalPath,
PROCESS_TIMEOUT_MS,
type ProgressCallback,
@@ -21,6 +23,23 @@ import {
} from "./media-common";
import { probeVideoFile } from "./media-probe";
import { fetchMedia, materializeMedia } from "./media-transfer";
+import {
+ buildGpuDecodeInputArgs,
+ buildNvencEncodeArgs,
+ canDecodeOnGpu,
+ fitWithinEven,
+ GPU_DECODE_FORMAT_FILTER,
+ logNvencFallback,
+ type NvencOptions,
+ nvencFallbackReason,
+ nvencOptionsFromEnv,
+ recordNvencAttempt,
+ selectedVideoEncoder,
+} from "./nvenc-encoder";
+import {
+ downloadUrlToPathParallel,
+ parallelDownloadOptionsFromEnv,
+} from "./parallel-download";
import { readRecordingAudioTail } from "./recording-packet-proof";
import {
RecordingTimingError,
@@ -41,7 +60,7 @@ const MAX_PROCESS_TIMEOUT_MS = 2 * 60 * 60 * 1000;
// flat 10-minute budget is enough for typical short recordings but not for a
// long (e.g. 60+ minute) manifest with 1000+ segments.
const STREAMING_DOWNLOAD_TIMEOUT_PER_SECOND_MS = 5_000;
-const MAX_STREAMING_DOWNLOAD_TIMEOUT_MS = 30 * 60 * 1000;
+const MAX_DOWNLOAD_TIMEOUT_MS = 30 * 60 * 1000;
const THUMBNAIL_TIMEOUT_MS = 60_000;
const PREVIEW_GIF_TIMEOUT_MS = 30_000;
const PROBE_H264_LEVEL_TIMEOUT_MS = 10_000;
@@ -58,6 +77,7 @@ const MULTIPART_MIN_PART_SIZE_BYTES = 5 * 1024 * 1024;
const MULTIPART_MAX_PARTS = 10_000;
const MULTIPART_ABORT_TIMEOUT_MS = 30_000;
const STORAGE_ERROR_BODY_LIMIT_BYTES = 2_048;
+export const LOOM_IMPORT_MAX_BYTES = 10 * 1024 ** 3;
export type StorageUploadTarget =
| {
@@ -77,6 +97,7 @@ export type StorageUploadTarget =
completeUrl: string;
abortUrl: string;
webhookSecret?: string;
+ loomImportAttemptId?: string;
};
export interface StorageUploadReceipt {
@@ -1175,7 +1196,7 @@ function getStreamingDownloadTimeoutMs(durationSeconds: number | null): number {
if (!durationSeconds) return DOWNLOAD_TIMEOUT_MS;
return Math.min(
- MAX_STREAMING_DOWNLOAD_TIMEOUT_MS,
+ MAX_DOWNLOAD_TIMEOUT_MS,
Math.max(
DOWNLOAD_TIMEOUT_MS,
Math.ceil(durationSeconds * STREAMING_DOWNLOAD_TIMEOUT_PER_SECOND_MS),
@@ -1186,10 +1207,24 @@ function getStreamingDownloadTimeoutMs(durationSeconds: number | null): number {
async function downloadStreamingVideoToTemp(
videoUrl: string,
abortSignal?: AbortSignal,
+ maxBytes?: number,
): Promise<TempFileHandle> {
await ensureTempDir();
const manifestDir = await mkdtemp(join(getTempDir(), "stream-"));
const tempFile = await createTempFile(".mkv");
+ const sizeController = new AbortController();
+ const signal = AbortSignal.any([
+ sizeController.signal,
+ ...(abortSignal ? [abortSignal] : []),
+ ]);
+ const sizeCheck = maxBytes
+ ? setInterval(() => {
+ if (file(tempFile.path).size > maxBytes) {
+ sizeController.abort(new Error("Loom imports are limited to 10 GB"));
+ }
+ }, 250)
+ : undefined;
+ sizeCheck?.unref();
const cleanup = async () => {
await tempFile.cleanup();
await rm(manifestDir, { force: true, recursive: true }).catch(() => {});
@@ -1200,7 +1235,7 @@ async function downloadStreamingVideoToTemp(
const inputPath = await materializeStreamingInput(
videoUrl,
manifestDir,
- abortSignal,
+ signal,
);
const durationSeconds =
await estimateMaterializedStreamingDurationSeconds(manifestDir);
@@ -1213,8 +1248,11 @@ async function downloadStreamingVideoToTemp(
ffmpegHlsCapabilities,
),
downloadTimeoutMs,
- abortSignal,
+ signal,
);
+ if (maxBytes && file(tempFile.path).size > maxBytes) {
+ throw new Error("Loom imports are limited to 10 GB");
+ }
if (file(tempFile.path).size === 0) {
throw new Error("Streaming download produced empty file");
@@ -1226,7 +1264,10 @@ async function downloadStreamingVideoToTemp(
};
} catch (err) {
await cleanup();
+ if (sizeController.signal.aborted) throw sizeController.signal.reason;
throw err;
+ } finally {
+ if (sizeCheck) clearInterval(sizeCheck);
}
}
@@ -1234,24 +1275,44 @@ export async function downloadVideoToTemp(
videoUrl: string,
inputExtension?: string,
abortSignal?: AbortSignal,
+ maxBytes?: number,
): Promise<TempFileHandle> {
if (isStreamingUrl(videoUrl)) {
- return await downloadStreamingVideoToTemp(videoUrl, abortSignal);
+ return await downloadStreamingVideoToTemp(videoUrl, abortSignal, maxBytes);
}
const local = await materializeMedia(videoUrl, abortSignal);
- if (local) return { path: local.path, cleanup: async () => {} };
+ if (local) {
+ if (maxBytes && file(local.path).size > maxBytes) {
+ throw new Error("Loom imports are limited to 10 GB");
+ }
+ return { path: local.path, cleanup: async () => {} };
+ }
const tempFile = await createTempFile(
normalizeVideoInputExtension(inputExtension),
);
try {
- const timeoutSignal = AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS);
+ const timeoutSignal = AbortSignal.timeout(
+ maxBytes === undefined ? DOWNLOAD_TIMEOUT_MS : MAX_DOWNLOAD_TIMEOUT_MS,
+ );
const combinedSignal = abortSignal
? AbortSignal.any([abortSignal, timeoutSignal])
: timeoutSignal;
+ const parallel = parallelDownloadOptionsFromEnv();
+ if (parallel.concurrency > 1 && maxBytes === undefined) {
+ await downloadUrlToPathParallel(videoUrl, tempFile.path, {
+ ...parallel,
+ signal: combinedSignal,
+ });
+ if (file(tempFile.path).size === 0) {
+ throw new Error("Downloaded video is empty");
+ }
+ return tempFile;
+ }
+
const response = await fetchMedia(videoUrl, {
signal: combinedSignal,
});
@@ -1266,18 +1327,31 @@ export async function downloadVideoToTemp(
if (!response.body) {
throw new Error("No response body");
}
+ if (maxBytes && Number(response.headers.get("content-length")) > maxBytes) {
+ await response.body.cancel();
+ throw new Error("Loom imports are limited to 10 GB");
+ }
const reader = response.body.getReader();
const writer = file(tempFile.path).writer();
+ let downloadedBytes = 0;
+ let writerEnded = false;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
+ downloadedBytes += value.byteLength;
+ if (maxBytes && downloadedBytes > maxBytes) {
+ throw new Error("Loom imports are limited to 10 GB");
+ }
writer.write(value);
}
+ writerEnded = true;
await writer.end();
} finally {
+ await reader.cancel().catch(() => {});
reader.releaseLock();
+ if (!writerEnded) await Promise.resolve(writer.end()).catch(() => {});
}
if (file(tempFile.path).size === 0) {
@@ -1311,7 +1385,7 @@ function needsAudioTranscode(metadata: VideoMetadata): boolean {
return Boolean(metadata.audioCodec && metadata.audioCodec !== "aac");
}
-function getProcessTimeoutMs(
+export function getProcessTimeoutMs(
durationSeconds: number,
baseTimeoutMs: number,
): number {
@@ -1425,10 +1499,11 @@ export function pickMobileSafeH264Level(
return { value: 52, ffmpegValue: "5.2" };
}
-async function probeH264Level(
+async function probeVideoStreamEntries(
inputPath: string,
+ showEntries: string,
abortSignal?: AbortSignal,
-): Promise<number | null> {
+): Promise<string | null> {
const proc = registerSubprocess(
spawn({
cmd: [
@@ -1438,7 +1513,7 @@ async function probeH264Level(
"-select_streams",
"v:0",
"-show_entries",
- "stream=level",
+ showEntries,
"-of",
"default=noprint_wrappers=1:nokey=1",
inputPath,
@@ -1472,10 +1547,7 @@ async function probeH264Level(
);
const exitCode = await proc.exited;
if (exitCode !== 0) return null;
-
- const rawLevel = stdout.trim().split(/\s+/)[0] ?? "";
- const level = Number.parseInt(rawLevel, 10);
- return Number.isFinite(level) && level > 0 ? level : null;
+ return stdout.trim();
} catch {
return null;
} finally {
@@ -1486,6 +1558,37 @@ async function probeH264Level(
}
}
+async function probeH264Level(
+ inputPath: string,
+ abortSignal?: AbortSignal,
+): Promise<number | null> {
+ const stdout = await probeVideoStreamEntries(
+ inputPath,
+ "stream=level",
+ abortSignal,
+ );
+ if (stdout === null) return null;
+ const rawLevel = stdout.split(/\s+/)[0] ?? "";
+ const level = Number.parseInt(rawLevel, 10);
+ return Number.isFinite(level) && level > 0 ? level : null;
+}
+
+async function probeVideoRotation(
+ inputPath: string,
+ abortSignal?: AbortSignal,
+): Promise<number | null> {
+ const stdout = await probeVideoStreamEntries(
+ inputPath,
+ "stream_side_data=rotation",
+ abortSignal,
+ );
+ if (stdout === null) return null;
+ const rawRotation = stdout.split(/\s+/)[0] ?? "";
+ if (rawRotation === "") return 0;
+ const rotation = Number.parseFloat(rawRotation);
+ return Number.isFinite(rotation) ? rotation : null;
+}
+
export async function processVideo(
inputPath: string,
metadata: VideoMetadata,
@@ -1550,38 +1653,176 @@ export async function processVideo(
throw err;
}
}
+ const argsInput: ProcessVideoArgsInput = {
+ inputPath,
+ outputPath: outputTempFile.path,
+ videoCodec: metadata.videoCodec,
+ hasAudio: Boolean(metadata.audioCodec),
+ opts,
+ targetH264Level: targetH264Level.ffmpegValue,
+ normalizeH264Level,
+ videoTranscode,
+ audioTranscode,
+ extraInputArgs,
+ extraOutputArgs,
+ sourceWidth: metadata.width,
+ sourceHeight: metadata.height,
+ // NVENC leaves the CPU free, so decode (and scale) can use every core.
+ decodeThreads: decodeThreadsFromEnv(
+ "MEDIA_SERVER_PROCESS_DECODE_THREADS",
+ selectedVideoEncoder() === "nvenc" ? 0 : 2,
+ ),
+ };
+ const totalDurationUs = metadata.duration * 1_000_000;
+ const nvenc =
+ videoTranscode && !normalizeH264Level ? nvencOptionsFromEnv() : null;
+
+ try {
+ if (nvenc) {
+ const gpuDecode = canDecodeOnGpu(
+ metadata.videoCodec,
+ await probeVideoRotation(inputPath, abortSignal),
+ nvenc.gpuDecodeCodecs,
+ );
+ try {
+ await runTranscodeCommand(
+ buildProcessVideoArgs({
+ ...argsInput,
+ nvenc: { options: nvenc, gpuDecode },
+ }),
+ outputTempFile.path,
+ totalDurationUs,
+ processTimeoutMs,
+ onProgress,
+ abortSignal,
+ );
+ recordNvencAttempt("process");
+ return outputTempFile;
+ } catch (error) {
+ const reason = nvencFallbackReason(
+ error,
+ abortSignal?.aborted ?? false,
+ );
+ if (reason === null) throw error;
+ recordNvencAttempt("process", reason);
+ logNvencFallback({
+ job: "process",
+ sourceCodec: metadata.videoCodec,
+ gpuDecode,
+ reason,
+ });
+ }
+ }
+
+ await runTranscodeCommand(
+ buildProcessVideoArgs(argsInput),
+ outputTempFile.path,
+ totalDurationUs,
+ processTimeoutMs,
+ onProgress,
+ abortSignal,
+ );
+ return outputTempFile;
+ } catch (err) {
+ await outputTempFile.cleanup();
+ throw err;
+ }
+}
+
+export interface ProcessVideoArgsInput {
+ inputPath: string;
+ outputPath: string;
+ videoCodec: string;
+ hasAudio: boolean;
+ opts: Pick<
+ Required<VideoProcessingOptions>,
+ "maxWidth" | "maxHeight" | "preset" | "crf" | "audioBitrate"
+ >;
+ targetH264Level: string;
+ normalizeH264Level: boolean;
+ videoTranscode: boolean;
+ audioTranscode: boolean;
+ extraInputArgs: string[];
+ extraOutputArgs: string[];
+ decodeThreads: string;
+ sourceWidth?: number;
+ sourceHeight?: number;
+ nvenc?: { options: NvencOptions; gpuDecode: boolean } | null;
+}
+
+export function buildProcessVideoArgs(input: ProcessVideoArgsInput): string[] {
+ const { opts, nvenc } = input;
+ const gpuDecode = Boolean(
+ nvenc?.gpuDecode && input.sourceWidth && input.sourceHeight,
+ );
+ const source = {
+ width: input.sourceWidth ?? 0,
+ height: input.sourceHeight ?? 0,
+ };
+ const cpuScaleFilter = `scale='min(${opts.maxWidth},iw)':'min(${opts.maxHeight},ih)':force_original_aspect_ratio=decrease,scale=trunc(iw/2)*2:trunc(ih/2)*2`;
const ffmpegArgs: string[] = [
"ffmpeg",
"-threads",
- "2",
- ...extraInputArgs,
+ input.decodeThreads,
+ ...(gpuDecode
+ ? buildGpuDecodeInputArgs(
+ input.videoCodec,
+ source,
+ fitWithinEven(
+ source.width,
+ source.height,
+ opts.maxWidth,
+ opts.maxHeight,
+ ),
+ )
+ : []),
+ ...input.extraInputArgs,
+ // Browser captures change size when the tab resizes. Rebuilding the filter graph for that crashes h264_nvenc
+ // (SIGSEGV on FFmpeg 7.1, even with a fixed output size), so keep the first graph; scale absorbs the new size
+ // exactly as the libx264 fallback's auto-inserted scaler does.
+ ...(nvenc && !gpuDecode && input.videoTranscode && !input.normalizeH264Level
+ ? ["-reinit_filter", "0"]
+ : []),
"-i",
- inputPath,
+ input.inputPath,
];
- if (normalizeH264Level) {
+ if (input.normalizeH264Level) {
ffmpegArgs.push(
"-c:v",
"copy",
"-bsf:v",
- `h264_metadata=level=${targetH264Level.ffmpegValue}`,
+ `h264_metadata=level=${input.targetH264Level}`,
);
- } else if (videoTranscode) {
- ffmpegArgs.push(
- "-c:v",
- "libx264",
- "-preset",
- opts.preset,
- "-crf",
- opts.crf.toString(),
- "-vf",
- `scale='min(${opts.maxWidth},iw)':'min(${opts.maxHeight},ih)':force_original_aspect_ratio=decrease,scale=trunc(iw/2)*2:trunc(ih/2)*2`,
- "-pix_fmt",
- "yuv420p",
- "-level:v",
- targetH264Level.ffmpegValue,
- );
- if (metadata.videoCodec === "vp8" || metadata.videoCodec === "vp9") {
+ } else if (input.videoTranscode) {
+ if (nvenc) {
+ ffmpegArgs.push(
+ ...buildNvencEncodeArgs(nvenc.options, opts.crf),
+ ...(gpuDecode
+ ? ["-vf", GPU_DECODE_FORMAT_FILTER]
+ : ["-vf", cpuScaleFilter, "-pix_fmt", "yuv420p"]),
+ );
+ } else {
+ ffmpegArgs.push(
+ "-c:v",
+ "libx264",
+ "-preset",
+ opts.preset,
+ "-crf",
+ opts.crf.toString(),
+ "-vf",
+ cpuScaleFilter,
+ "-pix_fmt",
+ "yuv420p",
+ );
+ }
+ if (nvenc) {
+ // h264_nvenc refuses to open when the nominal frame rate exceeds the level (browser WebM often reports 1000 fps); libx264 only writes it into the SPS, so tag it the same way.
+ ffmpegArgs.push("-bsf:v", `h264_metadata=level=${input.targetH264Level}`);
+ } else {
+ ffmpegArgs.push("-level:v", input.targetH264Level);
+ }
+ if (input.videoCodec === "vp8" || input.videoCodec === "vp9") {
// Reordered B-frames can make MP4 duration exclude the tail of sparse browser captures.
ffmpegArgs.push("-bf", "0");
}
@@ -1589,8 +1830,8 @@ export async function processVideo(
ffmpegArgs.push("-c:v", "copy");
}
- if (metadata.audioCodec) {
- if (audioTranscode) {
+ if (input.hasAudio) {
+ if (input.audioTranscode) {
ffmpegArgs.push("-c:a", "aac", "-b:a", opts.audioBitrate);
} else {
ffmpegArgs.push("-c:a", "copy");
@@ -1602,13 +1843,24 @@ export async function processVideo(
ffmpegArgs.push(
"-movflags",
"+faststart",
- ...extraOutputArgs,
+ ...input.extraOutputArgs,
"-progress",
"pipe:2",
"-y",
- outputTempFile.path,
+ input.outputPath,
);
+ return ffmpegArgs;
+}
+async function runTranscodeCommand(
+ ffmpegArgs: string[],
+ outputPath: string,
+ totalDurationUs: number,
+ processTimeoutMs: number,
+ onProgress?: ProgressCallback,
+ abortSignal?: AbortSignal,
+): Promise<void> {
+ if (abortSignal?.aborted) throw new Error("Video processing was cancelled");
const proc = registerSubprocess(
spawn({
cmd: ffmpegArgs,
@@ -1616,7 +1868,6 @@ export async function processVideo(
stderr: "pipe",
}),
);
- const totalDurationUs = metadata.duration * 1_000_000;
let abortCleanup: (() => void) | undefined;
if (abortSignal) {
abortCleanup = () => {
@@ -1627,6 +1878,7 @@ export async function processVideo(
const stderrLines: string[] = [];
const maxStderrLines = 50;
+ const errorLines: string[] = [];
try {
await withTimeout(
@@ -1653,6 +1905,7 @@ export async function processVideo(
if (stderrLines.length > maxStderrLines) {
stderrLines.shift();
}
+ collectFfmpegErrorLine(errorLines, line);
const progress = parseProgressFromStderr(line, totalDurationUs);
if (progress !== null && onProgress) {
onProgress(progress, `Encoding: ${Math.round(progress)}%`);
@@ -1665,12 +1918,13 @@ export async function processVideo(
const exitCode = await proc.exited;
if (exitCode !== 0) {
- throw new Error(
+ throw new FfmpegExitError(
`FFmpeg exited with code ${exitCode}. Last stderr: ${stderrLines.slice(-10).join(" | ")}`,
+ errorLines,
);
}
- const outputSize = await file(outputTempFile.path).size;
+ const outputSize = await file(outputPath).size;
if (outputSize === 0) {
throw new Error("FFmpeg produced empty output file");
}
@@ -1678,11 +1932,6 @@ export async function processVideo(
processTimeoutMs,
() => terminateProcess(proc),
);
-
- return outputTempFile;
- } catch (err) {
- await outputTempFile.cleanup();
- throw err;
} finally {
if (abortCleanup) {
abortSignal?.removeEventListener("abort", abortCleanup);
@@ -2389,6 +2638,7 @@ async function getMultipartPartUrl(
videoId: target.videoId,
generation: target.generation,
attemptId: target.attemptId,
+ loomImportAttemptId: target.loomImportAttemptId,
key: target.key,
uploadId: target.uploadId,
partNumber,
@@ -2490,6 +2740,7 @@ async function completeMultipartUpload(
videoId: target.videoId,
generation: target.generation,
attemptId: target.attemptId,
+ loomImportAttemptId: target.loomImportAttemptId,
key: target.key,
uploadId: target.uploadId,
parts,
@@ -2524,6 +2775,7 @@ async function abortMultipartUpload(
videoId: target.videoId,
generation: target.generation,
attemptId: target.attemptId,
+ loomImportAttemptId: target.loomImportAttemptId,
key: target.key,
uploadId: target.uploadId,
},
@@ -2537,6 +2789,13 @@ async function abortMultipartUpload(
await response.body?.cancel().catch(() => {});
}
+export function multipartUploadConcurrencyFromEnv(
+ env: Record<string, string | undefined> = process.env,
+): number {
+ const value = Number.parseInt(env.MEDIA_SERVER_UPLOAD_CONCURRENCY ?? "", 10);
+ return Number.isFinite(value) && value > 0 ? Math.min(value, 16) : 1;
+}
+
async function uploadFileMultipart(
filePath: string,
target: Extract<StorageUploadTarget, { type: "multipart" }>,
@@ -2564,25 +2823,45 @@ async function uploadFileMultipart(
);
}
- for (let partNumber = 1; partNumber <= partCount; partNumber++) {
- const start = (partNumber - 1) * partSize;
- const end = Math.min(start + partSize, contentLength);
- const partLength = end - start;
- const url = await getMultipartPartUrl(
- target,
- partNumber,
- partLength,
- abortSignal,
- );
- const etag = await uploadMultipartPart(
- url,
- fileHandle.slice(start, end),
- partLength,
- partNumber,
- abortSignal,
- );
- parts.push({ partNumber, etag, size: partLength });
+ const failed = new AbortController();
+ const signal = abortSignal
+ ? AbortSignal.any([abortSignal, failed.signal])
+ : failed.signal;
+ let nextPart = 1;
+ const worker = async () => {
+ while (nextPart <= partCount) {
+ const partNumber = nextPart++;
+ const start = (partNumber - 1) * partSize;
+ const end = Math.min(start + partSize, contentLength);
+ const partLength = end - start;
+ const url = await getMultipartPartUrl(
+ target,
+ partNumber,
+ partLength,
+ signal,
+ );
+ const etag = await uploadMultipartPart(
+ url,
+ fileHandle.slice(start, end),
+ partLength,
+ partNumber,
+ signal,
+ );
+ parts.push({ partNumber, etag, size: partLength });
+ }
+ };
+ const workers = Array.from(
+ { length: Math.min(multipartUploadConcurrencyFromEnv(), partCount) },
+ () => worker(),
+ );
+ try {
+ await Promise.all(workers);
+ } catch (error) {
+ failed.abort();
+ await Promise.allSettled(workers);
+ throw error;
}
+ parts.sort((a, b) => a.partNumber - b.partNumber);
return await completeMultipartUpload(target, parts, abortSignal);
} catch (error) {
diff --git a/apps/media-server/src/lib/nvenc-encoder.ts b/apps/media-server/src/lib/nvenc-encoder.ts
new file mode 100644
index 000000000..162d56b65
--- /dev/null
+++ b/apps/media-server/src/lib/nvenc-encoder.ts
@@ -0,0 +1,259 @@
+import { FfmpegExitError } from "./ffmpeg-errors";
+import { redactLogText } from "./log-redaction";
+
+const DEFAULT_PRESET = "p1";
+const DEFAULT_CQ_OFFSET = 8;
+const DEFAULT_LOOKAHEAD = 0;
+const MAX_LOOKAHEAD = 32;
+const GOP_LENGTH = "250";
+const NVENC_PRESETS = new Set(["p1", "p2", "p3", "p4", "p5", "p6", "p7"]);
+const NVDEC_CODECS = new Set(["h264", "hevc", "vp9", "av1"]);
+// H.264 and VP9 decode faster on the host CPU than on L4 NVDEC; HEVC and AV1 do not.
+const DEFAULT_GPU_DECODE_CODECS = ["hevc", "av1"];
+const MAX_FALLBACK_REASON_LENGTH = 500;
+const MAX_FFMPEG_CAUSE_LENGTH = 300;
+
+export const NVENC_HWACCEL_INPUT_ARGS = [
+ "-hwaccel",
+ "cuda",
+ "-hwaccel_output_format",
+ "cuda",
+];
+
+export type VideoEncoderName = "libx264" | "capcodec" | "nvenc";
+export type NvencJobKind = "process" | "edit";
+
+export interface NvencOptions {
+ preset: string;
+ cqOffset: number;
+ lookahead: number;
+ spatialAq: boolean;
+ temporalAq: boolean;
+ gpuDecodeCodecs: string[];
+}
+
+interface NvencAttempt {
+ job: NvencJobKind;
+ at: string;
+ fellBack: boolean;
+ reason: string | null;
+}
+
+const nvencStatus: {
+ attempts: number;
+ fallbacks: number;
+ lastAttempt: NvencAttempt | null;
+} = { attempts: 0, fallbacks: 0, lastAttempt: null };
+
+type Env = Record<string, string | undefined>;
+
+export function selectedVideoEncoder(env: Env = process.env): VideoEncoderName {
+ const value = env.CAP_MEDIA_VIDEO_ENCODER;
+ if (value === "capcodec" || value === "nvenc") return value;
+ return "libx264";
+}
+
+function parseBoundedInt(
+ raw: string | undefined,
+ min: number,
+ max: number,
+): number | undefined {
+ if (raw === undefined || raw.trim() === "") return undefined;
+ const value = Number(raw);
+ if (!Number.isInteger(value) || value < min || value > max) return undefined;
+ return value;
+}
+
+function parseToggle(raw: string | undefined): boolean | undefined {
+ const normalized = raw?.trim().toLowerCase();
+ if (normalized === "1" || normalized === "true" || normalized === "on") {
+ return true;
+ }
+ if (normalized === "0" || normalized === "false" || normalized === "off") {
+ return false;
+ }
+ return undefined;
+}
+
+export function nvencOptionsFromEnv(
+ env: Env = process.env,
+): NvencOptions | null {
+ if (selectedVideoEncoder(env) !== "nvenc") return null;
+ const preset = env.CAP_NVENC_PRESET?.trim().toLowerCase() ?? "";
+ return {
+ preset: NVENC_PRESETS.has(preset) ? preset : DEFAULT_PRESET,
+ cqOffset:
+ parseBoundedInt(env.CAP_NVENC_CQ_OFFSET, -51, 51) ?? DEFAULT_CQ_OFFSET,
+ lookahead:
+ parseBoundedInt(env.CAP_NVENC_LOOKAHEAD, 0, MAX_LOOKAHEAD) ??
+ DEFAULT_LOOKAHEAD,
+ spatialAq: parseToggle(env.CAP_NVENC_SPATIAL_AQ) ?? true,
+ temporalAq: parseToggle(env.CAP_NVENC_TEMPORAL_AQ) ?? false,
+ gpuDecodeCodecs: parseGpuDecodeCodecs(env.CAP_NVENC_GPU_DECODE_CODECS),
+ };
+}
+
+function parseGpuDecodeCodecs(raw: string | undefined): string[] {
+ if (raw === undefined || raw.trim() === "") return DEFAULT_GPU_DECODE_CODECS;
+ return raw
+ .split(",")
+ .map((codec) => codec.trim().toLowerCase())
+ .filter((codec) => NVDEC_CODECS.has(codec));
+}
+
+export function nvencCq(crf: number, cqOffset: number): string {
+ const base = Number.isFinite(crf) ? crf : 23;
+ const cq = Math.min(51, Math.max(1, base + cqOffset));
+ return String(Math.round(cq * 1000) / 1000);
+}
+
+export function buildNvencEncodeArgs(
+ options: NvencOptions,
+ crf: number,
+): string[] {
+ return [
+ "-c:v",
+ "h264_nvenc",
+ "-preset",
+ options.preset,
+ "-tune",
+ "hq",
+ "-rc",
+ "vbr",
+ "-cq",
+ nvencCq(crf, options.cqOffset),
+ "-b:v",
+ "0",
+ ...(options.spatialAq ? ["-spatial-aq", "1"] : []),
+ ...(options.temporalAq ? ["-temporal-aq", "1"] : []),
+ ...(options.lookahead > 0
+ ? ["-rc-lookahead", String(options.lookahead)]
+ : []),
+ "-profile:v",
+ "high",
+ // h264_nvenc otherwise inherits the preset's GOP; pin libx264's default keyint.
+ "-g",
+ GOP_LENGTH,
+ ];
+}
+
+// Mirrors the CPU filter chain: fit within the box keeping aspect (rounded like libavfilter), then floor to even.
+export function fitWithinEven(
+ width: number,
+ height: number,
+ maxWidth: number,
+ maxHeight: number,
+): { width: number; height: number } {
+ let w = Math.min(maxWidth, width);
+ let h = Math.min(maxHeight, height);
+ const tmpW = Math.round((h * width) / height);
+ const tmpH = Math.round((w * height) / width);
+ w = Math.min(tmpW, w);
+ h = Math.min(tmpH, h);
+ return { width: Math.trunc(w / 2) * 2, height: Math.trunc(h / 2) * 2 };
+}
+
+// cuvid resizes inside NVDEC for free; scale_cuda then only converts 10-bit surfaces and passes 8-bit through.
+export function buildGpuDecodeInputArgs(
+ videoCodec: string,
+ source: { width: number; height: number },
+ target: { width: number; height: number },
+): string[] {
+ return [
+ ...NVENC_HWACCEL_INPUT_ARGS,
+ "-c:v",
+ `${videoCodec}_cuvid`,
+ ...(target.width !== source.width || target.height !== source.height
+ ? ["-resize", `${target.width}x${target.height}`]
+ : []),
+ ];
+}
+
+export const GPU_DECODE_FORMAT_FILTER = "scale_cuda=format=nv12";
+
+// FFmpeg skips autorotation for hardware frames, so rotated sources decode on the CPU to keep the libx264 geometry.
+export function canDecodeOnGpu(
+ videoCodec: string,
+ rotation: number | null,
+ gpuDecodeCodecs: string[] = DEFAULT_GPU_DECODE_CODECS,
+): boolean {
+ return gpuDecodeCodecs.includes(videoCodec) && rotation === 0;
+}
+
+// A failed NVENC encode is retried on libx264 with a fresh timeout, so the job
+// deadline has to leave room for both attempts.
+export function encodeAttemptsBudgetMs(
+ encodeTimeoutMs: number,
+ env: Env = process.env,
+): number {
+ return selectedVideoEncoder(env) === "nvenc"
+ ? encodeTimeoutMs * 2
+ : encodeTimeoutMs;
+}
+
+export function nvencFallbackReason(
+ error: unknown,
+ aborted: boolean,
+): string | null {
+ if (aborted) return null;
+ const message = redactLogText(
+ error instanceof Error ? error.message : String(error),
+ );
+ // The message tail is mostly -progress output; the encoder's own error is printed earlier.
+ const cause =
+ error instanceof FfmpegExitError && error.errorLines.length > 0
+ ? `${redactLogText(error.errorLines.join(" | ")).slice(0, MAX_FFMPEG_CAUSE_LENGTH)} || `
+ : "";
+ return (
+ (
+ cause + message.slice(-(MAX_FALLBACK_REASON_LENGTH - cause.length))
+ ).trim() || "unknown error"
+ );
+}
+
+export function recordNvencAttempt(
+ job: NvencJobKind,
+ fallbackReason: string | null = null,
+): void {
+ nvencStatus.attempts += 1;
+ if (fallbackReason !== null) nvencStatus.fallbacks += 1;
+ nvencStatus.lastAttempt = {
+ job,
+ at: new Date().toISOString(),
+ fellBack: fallbackReason !== null,
+ reason: fallbackReason,
+ };
+}
+
+export function logNvencFallback(details: {
+ job: NvencJobKind;
+ sourceCodec: string;
+ gpuDecode: boolean;
+ reason: string;
+}): void {
+ console.warn(
+ JSON.stringify({
+ event: "nvenc_fallback_to_libx264",
+ timestamp: new Date().toISOString(),
+ ...details,
+ }),
+ );
+}
+
+export function getVideoEncoderStatus(
+ env: Env = process.env,
+ includeDetails = true,
+) {
+ const active = selectedVideoEncoder(env);
+ return {
+ active,
+ nvenc:
+ active === "nvenc" && includeDetails
+ ? {
+ attempts: nvencStatus.attempts,
+ fallbacks: nvencStatus.fallbacks,
+ lastAttempt: nvencStatus.lastAttempt,
+ }
+ : null,
+ };
+}
diff --git a/apps/media-server/src/lib/parallel-download.ts b/apps/media-server/src/lib/parallel-download.ts
new file mode 100644
index 000000000..f582f3537
--- /dev/null
+++ b/apps/media-server/src/lib/parallel-download.ts
@@ -0,0 +1,195 @@
+import { open } from "node:fs/promises";
+
+const MIN_PART_BYTES = 1024 * 1024;
+// Far above any recording we store (the largest so far is ~47 GB). Anything
+// bigger is a bad Content-Range header, not a real object.
+export const MAX_RANGED_TOTAL_BYTES = 1024 ** 4;
+
+export interface ParallelDownloadOptions {
+ concurrency: number;
+ partBytes: number;
+ signal?: AbortSignal;
+ fetchImpl?: typeof fetch;
+}
+
+export interface ParallelDownloadResult {
+ bytes: number;
+ parts: number;
+ ranged: boolean;
+}
+
+export function parallelDownloadOptionsFromEnv(
+ env: Record<string, string | undefined> = process.env,
+): Pick<ParallelDownloadOptions, "concurrency" | "partBytes"> {
+ const concurrency = Number.parseInt(
+ env.MEDIA_SERVER_DOWNLOAD_CONCURRENCY ?? "",
+ 10,
+ );
+ const partMb = Number.parseInt(env.MEDIA_SERVER_DOWNLOAD_PART_MB ?? "", 10);
+ return {
+ concurrency:
+ Number.isFinite(concurrency) && concurrency > 0
+ ? Math.min(concurrency, 32)
+ : 1,
+ partBytes: Math.max(
+ MIN_PART_BYTES,
+ (Number.isFinite(partMb) && partMb > 0 ? partMb : 16) * 1024 * 1024,
+ ),
+ };
+}
+
+export function planRanges(
+ totalBytes: number,
+ firstPartBytes: number,
+ partBytes: number,
+): { count: number; at: (index: number) => { start: number; end: number } } {
+ return {
+ count: Math.max(0, Math.ceil((totalBytes - firstPartBytes) / partBytes)),
+ at: (index) => {
+ const start = firstPartBytes + index * partBytes;
+ return { start, end: Math.min(totalBytes, start + partBytes) - 1 };
+ },
+ };
+}
+
+function parseContentRange(
+ value: string | null,
+): { start: number; end: number; total: number } | null {
+ const match = value?.match(/^bytes\s+(\d+)-(\d+)\/(\d+)$/i);
+ if (!match) return null;
+ const [start, end, total] = match.slice(1).map(Number) as [
+ number,
+ number,
+ number,
+ ];
+ return Number.isSafeInteger(total) && start <= end && end < total
+ ? { start, end, total }
+ : null;
+}
+
+async function readInto(
+ response: Response,
+ write: (chunk: Uint8Array, offset: number) => Promise<void>,
+ start: number,
+ expected: number | null,
+): Promise<number> {
+ if (!response.body) throw new Error("No response body");
+ const reader = response.body.getReader();
+ let offset = start;
+ try {
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ await write(value, offset);
+ offset += value.byteLength;
+ }
+ } finally {
+ reader.releaseLock();
+ }
+ const read = offset - start;
+ if (expected !== null && read !== expected) {
+ throw new Error(
+ `Ranged download returned ${read} bytes, expected ${expected}`,
+ );
+ }
+ return read;
+}
+
+export async function downloadUrlToPathParallel(
+ url: string,
+ path: string,
+ options: ParallelDownloadOptions,
+): Promise<ParallelDownloadResult> {
+ const fetchImpl = options.fetchImpl ?? fetch;
+ const partBytes = Math.max(MIN_PART_BYTES, options.partBytes);
+ const handle = await open(path, "w");
+ const write = async (chunk: Uint8Array, offset: number) => {
+ let written = 0;
+ while (written < chunk.byteLength) {
+ const { bytesWritten } = await handle.write(
+ chunk,
+ written,
+ chunk.byteLength - written,
+ offset + written,
+ );
+ written += bytesWritten;
+ }
+ };
+ const controller = new AbortController();
+ const signal = options.signal
+ ? AbortSignal.any([options.signal, controller.signal])
+ : controller.signal;
+ const fetchOk = async (headers?: Record<string, string>) => {
+ const response = await fetchImpl(url, { headers, signal });
+ if (!response.ok) {
+ await response.body?.cancel().catch(() => {});
+ throw new Error(
+ `Failed to download video: ${response.status} ${response.statusText}`,
+ );
+ }
+ return response;
+ };
+ try {
+ const first = await fetchOk({ Range: `bytes=0-${partBytes - 1}` });
+ if (first.status !== 206) {
+ const bytes = await readInto(first, write, 0, null);
+ return { bytes, parts: 1, ranged: false };
+ }
+ const range = parseContentRange(first.headers.get("content-range"));
+ if (
+ range === null ||
+ range.start !== 0 ||
+ range.end + 1 < Math.min(partBytes, range.total)
+ ) {
+ // A partial reply we can't place or that the server capped below our
+ // part size: start over as one stream rather than keep a fragment.
+ await first.body?.cancel().catch(() => {});
+ const bytes = await readInto(await fetchOk(), write, 0, null);
+ return { bytes, parts: 1, ranged: false };
+ }
+ const { total } = range;
+ if (total > MAX_RANGED_TOTAL_BYTES) {
+ await first.body?.cancel().catch(() => {});
+ throw new Error(`Ranged download reported an invalid size: ${total}`);
+ }
+ const firstBytes = range.end + 1;
+ const etag = first.headers.get("etag");
+ const ranges = planRanges(total, firstBytes, partBytes);
+ let next = 0;
+ const worker = async () => {
+ while (next < ranges.count) {
+ signal.throwIfAborted();
+ const part = ranges.at(next++);
+ const response = await fetchImpl(url, {
+ headers: {
+ Range: `bytes=${part.start}-${part.end}`,
+ ...(etag ? { "If-Match": etag } : {}),
+ },
+ signal,
+ });
+ if (response.status !== 206) {
+ await response.body?.cancel().catch(() => {});
+ throw new Error(
+ `Ranged download part failed: ${response.status} ${response.statusText}`,
+ );
+ }
+ await readInto(response, write, part.start, part.end - part.start + 1);
+ }
+ };
+ const workers = Array.from(
+ { length: Math.max(1, Math.min(options.concurrency - 1, ranges.count)) },
+ () => worker(),
+ );
+ const firstRead = readInto(first, write, 0, firstBytes);
+ try {
+ await Promise.all([firstRead, ...workers]);
+ } catch (error) {
+ controller.abort();
+ await Promise.allSettled([firstRead, ...workers]);
+ throw error;
+ }
+ return { bytes: total, parts: ranges.count + 1, ranged: true };
+ } finally {
+ await handle.close();
+ }
+}
diff --git a/apps/media-server/src/lib/peer-jobs.ts b/apps/media-server/src/lib/peer-jobs.ts
new file mode 100644
index 000000000..99318e2a1
--- /dev/null
+++ b/apps/media-server/src/lib/peer-jobs.ts
@@ -0,0 +1,75 @@
+const PEER_LOOKUP_HEADER = "x-cap-peer-lookup";
+const PEER_STATUS_TIMEOUT_MS = 2_000;
+// The owner replies to a cancel only after delivering its webhook (up to three 5 s attempts).
+const PEER_CANCEL_TIMEOUT_MS = 30_000;
+
+// Jobs live in the memory of the instance that runs them. Behind a load
+// balancer a status or cancel request can land on another instance, so it asks
+// the configured peers once before answering that the job does not exist.
+export function mediaServerPeers(
+ env: Record<string, string | undefined> = process.env,
+): string[] {
+ return (env.MEDIA_SERVER_PEERS ?? "")
+ .split(",")
+ .map((value) => value.trim().replace(/\/+$/, ""))
+ .filter((value) => {
+ try {
+ const url = new URL(value);
+ return url.protocol === "http:" || url.protocol === "https:";
+ } catch {
+ return false;
+ }
+ });
+}
+
+export function isPeerLookup(request: Request): boolean {
+ return request.headers.get(PEER_LOOKUP_HEADER) === "1";
+}
+
+export async function findJobOnPeer(
+ path: string,
+ method: "GET" | "POST",
+): Promise<Response | null> {
+ const secret = process.env.MEDIA_SERVER_WEBHOOK_SECRET;
+ const peers = mediaServerPeers();
+ if (!secret || peers.length === 0) return null;
+ const controller = new AbortController();
+ const timeout = setTimeout(
+ () => controller.abort(),
+ method === "POST" ? PEER_CANCEL_TIMEOUT_MS : PEER_STATUS_TIMEOUT_MS,
+ );
+ try {
+ return await Promise.any(
+ peers.map(async (peer) => {
+ const response = await fetch(`${peer}${path}`, {
+ method,
+ headers: {
+ "x-media-server-secret": secret,
+ [PEER_LOOKUP_HEADER]: "1",
+ Accept: "application/json",
+ },
+ signal: controller.signal,
+ redirect: "error",
+ });
+ // Only a peer that owns the job answers 200, or 400 for a finished job's cancel.
+ if (response.status !== 200 && response.status !== 400) {
+ await response.body?.cancel().catch(() => {});
+ throw new Error("Job is not on this peer");
+ }
+ const body = await response.text();
+ return new Response(body, {
+ status: response.status,
+ headers: {
+ "Content-Type":
+ response.headers.get("content-type") ?? "application/json",
+ },
+ });
+ }),
+ );
+ } catch {
+ return null;
+ } finally {
+ clearTimeout(timeout);
+ controller.abort();
+ }
+}
diff --git a/apps/media-server/src/lib/recording-packet-proof.ts b/apps/media-server/src/lib/recording-packet-proof.ts
index 59cf38060..67cf1ace4 100644
--- a/apps/media-server/src/lib/recording-packet-proof.ts
+++ b/apps/media-server/src/lib/recording-packet-proof.ts
@@ -381,41 +381,60 @@ export async function proveRecordingPackets(
const before = await Promise.all(
paths.map((path) => lstat(path, { bigint: true })),
);
- const sourceTiming = await readRecordingVideoTiming(videoPath, {
- abortSignal: signal,
- timeoutMs: 45 * 60_000,
- });
- if (sourceTiming.terminalPacketCount !== 1)
- throw new Error("Tied terminal samples require decoded source evidence");
- const sourceVideo = await readTrack(videoPath, "video", signal);
- const outputVideo = await readTrack(outputPath, "video", signal);
- if (JSON.stringify(sourceVideo) !== JSON.stringify(outputVideo))
- throw new RecordingPacketMismatchError(
- "Recording encoded video does not preserve the source",
- );
- if (audioPath) {
- const { tail: sourceTail, ...sourceAudio } = await readTrack(
- audioPath,
- "audio",
- signal,
- );
- const { tail: outputTail, ...outputAudio } = await readTrack(
- outputPath,
- "audio",
- signal,
- );
- if (JSON.stringify(sourceAudio) !== JSON.stringify(outputAudio))
+ // Every read below is independent and the files are pinned by `before`, so
+ // run them together and still check their results in the original order.
+ const reads = new AbortController();
+ const readSignal = AbortSignal.any([signal, reads.signal]);
+ const settled = <T>(promise: Promise<T>) => {
+ promise.catch(() => {});
+ return promise;
+ };
+ const timingOptions = { abortSignal: readSignal, timeoutMs: 45 * 60_000 };
+ const sourceTimingRead = settled(
+ readRecordingVideoTiming(videoPath, timingOptions),
+ );
+ const sourceVideoRead = settled(readTrack(videoPath, "video", readSignal));
+ const outputVideoRead = settled(readTrack(outputPath, "video", readSignal));
+ const sourceAudioRead = audioPath
+ ? settled(readTrack(audioPath, "audio", readSignal))
+ : null;
+ const outputAudioRead = audioPath
+ ? settled(readTrack(outputPath, "audio", readSignal))
+ : null;
+ const outputTimingRead = settled(
+ readRecordingVideoTiming(outputPath, timingOptions),
+ );
+ let sourceTiming: Awaited<typeof sourceTimingRead>;
+ let outputTiming: Awaited<typeof outputTimingRead>;
+ let sourceVideo: Awaited<typeof sourceVideoRead>;
+ let outputVideo: Awaited<typeof outputVideoRead>;
+ try {
+ sourceTiming = await sourceTimingRead;
+ if (sourceTiming.terminalPacketCount !== 1)
+ throw new Error("Tied terminal samples require decoded source evidence");
+ sourceVideo = await sourceVideoRead;
+ outputVideo = await outputVideoRead;
+ if (JSON.stringify(sourceVideo) !== JSON.stringify(outputVideo))
throw new RecordingPacketMismatchError(
- "Recording encoded audio does not preserve the source",
+ "Recording encoded video does not preserve the source",
);
- // MP4 edit-list rounding can change the reported AAC tail without changing decoded samples.
- if (sourceTail?.duration !== outputTail?.duration)
- throw new Error("Recording audio tail requires decoded source evidence");
+ if (sourceAudioRead && outputAudioRead) {
+ const { tail: sourceTail, ...sourceAudio } = await sourceAudioRead;
+ const { tail: outputTail, ...outputAudio } = await outputAudioRead;
+ if (JSON.stringify(sourceAudio) !== JSON.stringify(outputAudio))
+ throw new RecordingPacketMismatchError(
+ "Recording encoded audio does not preserve the source",
+ );
+ // MP4 edit-list rounding can change the reported AAC tail without changing decoded samples.
+ if (sourceTail?.duration !== outputTail?.duration)
+ throw new Error(
+ "Recording audio tail requires decoded source evidence",
+ );
+ }
+ outputTiming = await outputTimingRead;
+ } finally {
+ reads.abort();
}
- const outputTiming = await readRecordingVideoTiming(outputPath, {
- abortSignal: signal,
- timeoutMs: 45 * 60_000,
- });
const normalizeTiming = (timing: typeof sourceTiming) => ({
packets: timing.packetTimelineSha256,
terminalPackets: timing.terminalPacketCount,
diff --git a/apps/media-server/src/lib/recording-timing.ts b/apps/media-server/src/lib/recording-timing.ts
index c6b740121..cec679497 100644
--- a/apps/media-server/src/lib/recording-timing.ts
+++ b/apps/media-server/src/lib/recording-timing.ts
@@ -13,6 +13,11 @@ import {
} from "mediabunny";
const MAX_METADATA_BYTES = 64 * 1024 * 1024;
+const MAX_LONG_RECORDING_METADATA_BYTES = 1024 * 1024 * 1024;
+// A long budget covers many reads; one allocation for a single header box stays bounded.
+const MAX_SINGLE_METADATA_READ_BYTES = 256 * 1024 * 1024;
+// Sample tables grow with a recording's length; reads stream through a fixed cache.
+const METADATA_BYTES_PER_FILE_BYTE = 1 / 64;
const MAX_RANGE_BYTES = 1024 * 1024;
const MAX_CACHE_BYTES = 8 * 1024 * 1024;
const MAX_TERMINAL_PACKETS = 1024;
@@ -304,6 +309,22 @@ async function readTerminalDurations(
return durations;
}
+export function timingMetadataBudget(fileSize: number): number {
+ if (!Number.isSafeInteger(fileSize) || fileSize <= 0)
+ return MAX_METADATA_BYTES;
+ return Math.min(
+ MAX_LONG_RECORDING_METADATA_BYTES,
+ Math.max(
+ MAX_METADATA_BYTES,
+ Math.ceil(fileSize * METADATA_BYTES_PER_FILE_BYTE),
+ ),
+ );
+}
+
+function megabytes(bytes: number): string {
+ return (bytes / (1024 * 1024)).toFixed(1);
+}
+
export async function readRecordingVideoTiming(
path: string,
options: RecordingTimingOptions,
@@ -359,13 +380,10 @@ export async function readRecordingVideoTiming(
const reads = new Set<Promise<Uint8Array>>();
const reserve = (bytes: number) => {
metadataBytes += bytes;
- if (
- !Number.isSafeInteger(bytes) ||
- bytes < 0 ||
- metadataBytes > MAX_METADATA_BYTES
- ) {
+ const budget = timingMetadataBudget(fileSize);
+ if (!Number.isSafeInteger(bytes) || bytes < 0 || metadataBytes > budget) {
throw new RecordingTimingError(
- "Recording timing metadata exceeds its read budget",
+ `Recording timing metadata exceeds its read budget (${megabytes(metadataBytes)} of ${megabytes(budget)} MB for a ${megabytes(fileSize)} MB file)`,
false,
);
}
@@ -508,6 +526,12 @@ export async function readRecordingVideoTiming(
false,
);
}
+ if (end - start > MAX_SINGLE_METADATA_READ_BYTES) {
+ throw new RecordingTimingError(
+ `Recording timing metadata exceeds its read budget (one ${megabytes(end - start)} MB read)`,
+ false,
+ );
+ }
reserve(end - start);
const result = new Uint8Array(end - start);
for (let position = start; position < end; ) {
diff --git a/apps/media-server/src/lib/recording-verification.ts b/apps/media-server/src/lib/recording-verification.ts
index 9a599fbf0..51e1c69e0 100644
--- a/apps/media-server/src/lib/recording-verification.ts
+++ b/apps/media-server/src/lib/recording-verification.ts
@@ -1,11 +1,12 @@
import { spawn } from "node:child_process";
import { createHash, type Hash } from "node:crypto";
import { createReadStream } from "node:fs";
-import { lstat } from "node:fs/promises";
-import { isAbsolute } from "node:path";
+import { lstat, open, rm } from "node:fs/promises";
+import { availableParallelism } from "node:os";
+import { dirname, isAbsolute, join } from "node:path";
import { Readable } from "node:stream";
import { setTimeout as sleep } from "node:timers/promises";
-import { PROCESS_TIMEOUT_MS } from "./media-common";
+import { decodeThreadsFromEnv, PROCESS_TIMEOUT_MS } from "./media-common";
import {
fetchMedia,
MediaTransferBudgetError,
@@ -53,6 +54,9 @@ export interface RecordingVerificationOptions {
hasAudio?: boolean;
sourceEvidence?: RecordingSourceEvidence;
allowObservedDuration?: boolean;
+ // Hashing every decoded frame costs as much as the decode; skip it when the
+ // caller has no source evidence to compare against.
+ hashDecodedContent?: boolean;
abortSignal?: AbortSignal;
timeoutMs?: number;
onProgress?: (progress: { frames: number; totalFrames?: number }) => void;
@@ -665,6 +669,526 @@ export async function inspectRecordingSources(
return { ...result, integrity: result.integrity };
}
+export interface RecordingVideoChunkSource {
+ init: string;
+ segments: string[];
+}
+
+export function verifyDecodeChunks(
+ env: Record<string, string | undefined> = process.env,
+): number {
+ const value = Number(env.MEDIA_SERVER_VERIFY_DECODE_CHUNKS?.trim());
+ return Number.isInteger(value) && value > 1 ? Math.min(value, 16) : 1;
+}
+
+class ChunkedDecodeUnavailable extends Error {}
+
+const DECODED_STREAM_METADATA =
+ /^#(tb|media_type|sample_rate|dimensions|sar|channel_layout_name) (\d+):/;
+
+const metadataOf = (lines: string[]) =>
+ lines.filter((line) => DECODED_STREAM_METADATA.test(line)).join("\n");
+
+function describeHeaderChange(first: string, other: string): string {
+ const fields = (header: string) =>
+ new Map(
+ header
+ .split("\n")
+ .filter(Boolean)
+ .map((line) => {
+ const [name, ...value] = line.slice(1).split(": ");
+ return [name, value.join(": ")] as const;
+ }),
+ );
+ const before = fields(first);
+ const after = fields(other);
+ const changes = [...new Set([...before.keys(), ...after.keys()])]
+ .filter((name) => before.get(name) !== after.get(name))
+ .map(
+ (name) =>
+ `${name}: ${before.get(name) ?? "none"} -> ${after.get(name) ?? "none"}`,
+ );
+ return changes.join("; ").slice(0, 200) || "unknown";
+}
+
+async function collectDecoderLines(
+ stream: AsyncIterable<Uint8Array>,
+ lines: string[],
+ onLine: () => void,
+): Promise<void> {
+ const decoder = new TextDecoder();
+ let pending = "";
+ const take = (line: string) => {
+ const trimmed = line.trim();
+ if (!trimmed) return;
+ lines.push(trimmed);
+ if (!trimmed.startsWith("#")) onLine();
+ };
+ for await (const chunk of stream) {
+ pending += decoder.decode(chunk, { stream: true });
+ let newline = pending.indexOf("\n");
+ while (newline !== -1) {
+ if (newline > MAX_OUTPUT_LINE_LENGTH) {
+ throw new Error("Recording decoder output exceeded its limit");
+ }
+ take(pending.slice(0, newline));
+ pending = pending.slice(newline + 1);
+ newline = pending.indexOf("\n");
+ }
+ if (pending.length > MAX_OUTPUT_LINE_LENGTH) {
+ throw new Error("Recording decoder output exceeded its limit");
+ }
+ }
+ take(pending + decoder.decode());
+}
+
+async function concatFiles(
+ paths: string[],
+ target: string,
+ signal?: AbortSignal,
+): Promise<void> {
+ const output = await open(target, "w");
+ try {
+ for (const path of paths) {
+ signal?.throwIfAborted();
+ for await (const chunk of createReadStream(path, { signal })) {
+ for (let offset = 0; offset < chunk.byteLength; ) {
+ const { bytesWritten } = await output.write(
+ chunk,
+ offset,
+ chunk.byteLength - offset,
+ );
+ if (bytesWritten <= 0)
+ throw new Error(
+ "Recording verification chunk could not be written",
+ );
+ offset += bytesWritten;
+ }
+ }
+ }
+ } finally {
+ await output.close();
+ }
+}
+
+const KEYFRAME_SEARCH_SEGMENTS = 8;
+
+async function countOutputLines(
+ command: string,
+ args: string[],
+ pattern: RegExp,
+ signal?: AbortSignal,
+): Promise<number | null> {
+ const child = spawn(command, args, {
+ stdio: ["ignore", "pipe", "ignore"],
+ signal,
+ timeout: 60_000,
+ });
+ let count = 0;
+ let pending = "";
+ child.stdout.setEncoding("utf8");
+ child.stdout.on("data", (text: string) => {
+ pending += text;
+ const lines = pending.split("\n");
+ pending = lines.pop() ?? "";
+ for (const line of lines) if (pattern.test(line)) count++;
+ });
+ const code = await new Promise<number | null>((resolve) => {
+ child.once("error", () => resolve(null));
+ child.once("close", resolve);
+ });
+ if (pattern.test(pending)) count++;
+ return code === 0 ? count : null;
+}
+
+// A fragment can open with a sync sample the decoder still cannot start from
+// cleanly, so prove it: decoding it alone must yield a frame for every packet.
+async function decodesCleanlyFromStart(
+ init: string,
+ segment: string,
+ signal?: AbortSignal,
+): Promise<boolean> {
+ const input = `concat:${init}|${segment}`;
+ const [packets, frames] = await Promise.all([
+ countOutputLines(
+ "ffprobe",
+ [
+ "-v",
+ "error",
+ "-protocol_whitelist",
+ "file,concat",
+ "-select_streams",
+ "v:0",
+ "-show_entries",
+ "packet=flags",
+ "-of",
+ "csv=p=0",
+ input,
+ ],
+ /^\S/,
+ signal,
+ ),
+ countOutputLines(
+ "ffmpeg",
+ [
+ "-hide_banner",
+ "-nostdin",
+ "-copyts",
+ "-v",
+ "error",
+ "-xerror",
+ "-err_detect",
+ "explode+crccheck",
+ "-threads",
+ "1",
+ "-protocol_whitelist",
+ "file,concat",
+ "-f",
+ "mov",
+ "-i",
+ input,
+ "-map",
+ "0:v:0",
+ "-fps_mode",
+ "passthrough",
+ "-enc_time_base:v",
+ "-1",
+ "-c:v",
+ "wrapped_avframe",
+ "-bsf:v",
+ "setts=pts=N:dts=N:duration=1",
+ "-f",
+ "framecrc",
+ "pipe:1",
+ ],
+ /^0,/,
+ signal,
+ ),
+ ]);
+ return packets !== null && packets > 0 && frames === packets;
+}
+
+// Splits only where a fragment decodes cleanly on its own, searching a few
+// fragments past each even split point and merging runs when none does.
+async function keyframeAlignedGroups(
+ source: RecordingVideoChunkSource,
+ chunkCount: number,
+ signal?: AbortSignal,
+): Promise<string[][]> {
+ const total = source.segments.length;
+ const per = Math.ceil(total / chunkCount);
+ const targets = Array.from(
+ { length: chunkCount - 1 },
+ (_, index) => (index + 1) * per,
+ ).filter((target) => target < total);
+ const found = await Promise.all(
+ targets.map(async (target) => {
+ for (
+ let index = target;
+ index < Math.min(total, target + KEYFRAME_SEARCH_SEGMENTS);
+ index++
+ ) {
+ if (
+ await decodesCleanlyFromStart(
+ source.init,
+ source.segments[index],
+ signal,
+ )
+ )
+ return index;
+ }
+ return undefined;
+ }),
+ );
+ const boundaries = [0];
+ for (const index of found) {
+ if (index !== undefined && index > boundaries[boundaries.length - 1])
+ boundaries.push(index);
+ }
+ return boundaries.map((start, position) =>
+ source.segments.slice(start, boundaries[position + 1] ?? total),
+ );
+}
+
+// Decodes the packet-proven video as independent runs of whole fragments in
+// parallel, then replays their frame lines through the same parser and evidence
+// checks as one decode. Any decoder complaint falls back to the single decode,
+// which stays the judge of whether a recording is corrupt.
+async function decodePacketProvenChunks(
+ outputPath: string,
+ source: RecordingVideoChunkSource,
+ chunkCount: number,
+ options: RecordingVerificationOptions,
+ packetProof: Awaited<ReturnType<typeof proveRecordingPackets>>,
+): Promise<RecordingVerificationResult> {
+ const groups = await keyframeAlignedGroups(
+ source,
+ chunkCount,
+ options.abortSignal,
+ );
+ if (groups.length < 2)
+ throw new ChunkedDecodeUnavailable("no cleanly decodable fragment runs");
+ const directory = dirname(outputPath);
+ const chunkPaths = groups.map((_, index) =>
+ join(directory, `verify-chunk-${index}.mp4`),
+ );
+ const configuredThreads = Number(
+ decodeThreadsFromEnv("MEDIA_SERVER_VERIFY_DECODE_THREADS", 0),
+ );
+ const threads = String(
+ Math.max(
+ 1,
+ Math.floor(
+ (configuredThreads > 0 ? configuredThreads : availableParallelism()) /
+ groups.length,
+ ),
+ ),
+ );
+ const base = [
+ "-hide_banner",
+ "-nostdin",
+ "-copyts",
+ "-v",
+ "error",
+ "-xerror",
+ "-err_detect",
+ "explode+crccheck",
+ "-filter_threads",
+ "1",
+ "-filter_complex_threads",
+ "1",
+ ];
+ const decoders: Awaited<ReturnType<typeof spawnRecordingDecoder>>[] = [];
+ const outputs: string[][] = [];
+ const readers: Promise<unknown>[] = [];
+ let failure: Error | undefined;
+ let decodedFrames = 0;
+ let lastAdvance = performance.now();
+ let lastReport = 0;
+ const stopAll = () => {
+ for (const decoder of decoders) {
+ if (decoder.exitCode === null) {
+ try {
+ decoder.kill("SIGKILL");
+ } catch {
+ failure ??= new Error("Recording decoder termination failed");
+ }
+ }
+ }
+ };
+ const cancel = () => {
+ failure ??= new Error("Recording verification was cancelled");
+ stopAll();
+ };
+ options.abortSignal?.addEventListener("abort", cancel, { once: true });
+ const timeout = setTimeout(() => {
+ failure ??= new RecordingVerificationError(
+ "Recording verification timed out",
+ true,
+ );
+ stopAll();
+ }, options.timeoutMs ?? MAX_LOCAL_VERIFICATION_MS);
+ const stall = setInterval(() => {
+ if (performance.now() - lastAdvance > DECODER_STALL_MS) {
+ failure ??= new RecordingVerificationError(
+ "Recording decoder stopped making progress",
+ true,
+ );
+ stopAll();
+ }
+ }, 1000);
+ const onFrame = () => {
+ decodedFrames++;
+ lastAdvance = performance.now();
+ if (performance.now() - lastReport >= 1000) {
+ lastReport = performance.now();
+ options.onProgress?.({
+ frames: decodedFrames,
+ totalFrames: packetProof.videoPackets,
+ });
+ }
+ };
+ try {
+ await Promise.all(
+ groups.map((group, index) =>
+ concatFiles(
+ [source.init, ...group],
+ chunkPaths[index],
+ options.abortSignal,
+ ),
+ ),
+ );
+ const jobs = [
+ ...chunkPaths.map((path) => [
+ ...base,
+ "-threads",
+ threads,
+ "-protocol_whitelist",
+ "file",
+ "-f",
+ "mov",
+ "-i",
+ path,
+ "-map",
+ "0:v:0",
+ "-fps_mode",
+ "passthrough",
+ "-enc_time_base:v",
+ "-1",
+ "-c:v",
+ "wrapped_avframe",
+ "-bsf:v",
+ "setts=pts=N:dts=N:duration=1",
+ "-max_interleave_delta",
+ "1",
+ "-f",
+ "framecrc",
+ "pipe:1",
+ ]),
+ ...(packetProof.hasAudio
+ ? [
+ [
+ ...base,
+ "-threads",
+ "1",
+ "-protocol_whitelist",
+ "file",
+ "-f",
+ "mov",
+ "-i",
+ outputPath,
+ "-map",
+ "0:a:0",
+ "-c:a",
+ "pcm_f64le",
+ "-threads",
+ "1",
+ "-max_interleave_delta",
+ "1",
+ "-f",
+ "framecrc",
+ "pipe:1",
+ ],
+ ]
+ : []),
+ ];
+ const errors: Promise<{ error: string }>[] = [];
+ // A chunk's header describes its first decoded frame, so a mismatch is known
+ // as soon as each chunk emits one frame; stop then instead of decoding everything first.
+ const headers = new Map<number, string>();
+ const checkHeader = (index: number, lines: string[]) => {
+ if (headers.has(index)) return;
+ headers.set(index, metadataOf(lines));
+ const first = headers.get(0);
+ if (first === undefined) return;
+ for (const [other, header] of headers) {
+ if (other === 0 || header === first) continue;
+ failure ??= new ChunkedDecodeUnavailable(
+ `chunk stream parameters differ (${describeHeaderChange(first, header)})`,
+ );
+ stopAll();
+ return;
+ }
+ };
+ for (const [index, args] of jobs.entries()) {
+ options.abortSignal?.throwIfAborted();
+ const decoder = registerSubprocess(await spawnRecordingDecoder(args, 0));
+ decoders.push(decoder);
+ const lines: string[] = [];
+ outputs.push(lines);
+ const frames = collectDecoderLines(
+ decoder.stdout,
+ lines,
+ index < chunkPaths.length
+ ? () => {
+ checkHeader(index, lines);
+ onFrame();
+ }
+ : onFrame,
+ );
+ const stderr = readDecoderErrors(decoder.stderr, outputPath);
+ frames.catch(stopAll);
+ stderr.catch(stopAll);
+ readers.push(frames, stderr);
+ errors.push(stderr);
+ }
+ const exits = await Promise.all(decoders.map((decoder) => decoder.exited));
+ await Promise.all(readers);
+ if (failure) throw failure;
+ const stderr = await Promise.all(errors);
+ for (const [index, decoder] of decoders.entries()) {
+ if (exits[index] !== 0 || stderr[index].error || decoder.signalCode)
+ throw new ChunkedDecodeUnavailable(
+ stderr[index].error || `decoder exited with ${exits[index]}`,
+ );
+ }
+ const streams = new Map<number, DecodedStream>();
+ const videoOutputs = outputs.slice(0, chunkPaths.length);
+ const videoMetadata = metadataOf(videoOutputs[0]);
+ let offset = 0;
+ for (const [index, lines] of videoOutputs.entries()) {
+ const metadata = metadataOf(lines);
+ if (index > 0 && metadata !== videoMetadata)
+ throw new ChunkedDecodeUnavailable(
+ `chunk stream parameters differ (${describeHeaderChange(videoMetadata, metadata)})`,
+ );
+ let frames = 0;
+ for (const line of lines) {
+ if (line.startsWith("#")) {
+ if (index === 0) decodeLine(line, streams, true);
+ continue;
+ }
+ const columns = line.split(",").map((value) => value.trim());
+ if (columns[0] !== "0")
+ throw new ChunkedDecodeUnavailable("unexpected chunk stream");
+ const dts = Number(columns[1]);
+ const pts = Number(columns[2]);
+ if (dts !== frames || pts !== frames)
+ throw new ChunkedDecodeUnavailable("chunk frames are not contiguous");
+ columns[1] = String(dts + offset);
+ columns[2] = String(pts + offset);
+ decodeLine(columns.join(", "), streams, true);
+ frames++;
+ }
+ offset += frames;
+ }
+ // A decoder silently skips frames until a keyframe, so a short count is a split problem, not proof.
+ if (offset !== packetProof.videoPackets)
+ throw new ChunkedDecodeUnavailable("chunk frame count differs");
+ if (packetProof.hasAudio) {
+ for (const line of outputs[chunkPaths.length]) {
+ if (line.startsWith("#")) {
+ const metadata = line.match(DECODED_STREAM_METADATA);
+ if (metadata?.[2] === "0")
+ decodeLine(line.replace(/^(#\w+) 0:/, "$1 1:"), streams, true);
+ continue;
+ }
+ if (!line.startsWith("0,"))
+ throw new ChunkedDecodeUnavailable("unexpected audio stream");
+ decodeLine(`1${line.slice(1)}`, streams, true);
+ }
+ }
+ return validateEvidence(
+ streams,
+ options,
+ true,
+ packetProof.outputTiming,
+ false,
+ );
+ } finally {
+ clearTimeout(timeout);
+ clearInterval(stall);
+ options.abortSignal?.removeEventListener("abort", cancel);
+ stopAll();
+ await Promise.allSettled([
+ ...decoders.map((decoder) => decoder.exited),
+ ...readers,
+ ]);
+ for (const decoder of decoders) unregisterSubprocess(decoder);
+ await Promise.all(chunkPaths.map((path) => rm(path, { force: true })));
+ }
+}
+
export async function verifyRemuxedRecording(
videoPath: string,
audioPath: string | null,
@@ -673,6 +1197,7 @@ export async function verifyRemuxedRecording(
RecordingVerificationOptions,
"requireAudio" | "abortSignal" | "timeoutMs" | "onProgress"
>,
+ videoChunks?: RecordingVideoChunkSource,
): Promise<RecordingVerificationResult & { sourcePreserved: true }> {
const signal = AbortSignal.any([
...(options.abortSignal ? [options.abortSignal] : []),
@@ -705,9 +1230,45 @@ export async function verifyRemuxedRecording(
throw new Error("Recording source preservation was not verified");
return { ...result, sourcePreserved: true };
}
- const result = await decodeRecording(
+ const decodeOptions = {
+ ...options,
+ timeoutMs: options.timeoutMs ?? MAX_LOCAL_VERIFICATION_MS,
+ };
+ const chunkCount = verifyDecodeChunks();
+ let result: RecordingVerificationResult | undefined;
+ if (
+ videoChunks &&
+ chunkCount > 1 &&
+ videoChunks.segments.length >= chunkCount * 2
+ ) {
+ try {
+ result = await decodePacketProvenChunks(
+ outputPath,
+ videoChunks,
+ chunkCount,
+ decodeOptions,
+ proof,
+ );
+ } catch (error) {
+ signal.throwIfAborted();
+ if (
+ error instanceof RecordingVerificationError &&
+ error.message === "Recording verification timed out"
+ )
+ throw error;
+ console.warn(
+ JSON.stringify({
+ event: "recording_chunked_decode_fallback",
+ reason: (error instanceof Error ? error.message : String(error))
+ .replace(/https?:\/\/\S+/g, "<url>")
+ .slice(0, 300),
+ }),
+ );
+ }
+ }
+ result ??= await decodeRecording(
outputPath,
- { ...options, timeoutMs: options.timeoutMs ?? MAX_LOCAL_VERIFICATION_MS },
+ decodeOptions,
undefined,
undefined,
proof,
@@ -846,8 +1407,11 @@ async function decodeRecording(
? Boolean(options.sourceEvidence.audio)
: options.hasAudio
: sourceAudioInput !== null;
+ const hashContent =
+ !packetProof &&
+ (options.hashDecodedContent !== false || Boolean(options.sourceEvidence));
const streamDecodedContent =
- !packetProof && process.platform !== "win32" && knownAudio !== undefined;
+ hashContent && process.platform !== "win32" && knownAudio !== undefined;
if (streamDecodedContent && !Bun.semver.satisfies(Bun.version, ">=1.4.0")) {
throw new Error(
"Recording content verification requires Bun 1.4.0 or newer; upgrade the media-server runtime to avoid double-closing decoded-content pipes",
@@ -890,8 +1454,9 @@ async function decodeRecording(
"1",
"-filter_complex_threads",
"1",
+ // FFmpeg's H.264/HEVC decoders are bit-exact across thread counts, so decoded hashes do not depend on this.
"-threads",
- "1",
+ decodeThreadsFromEnv("MEDIA_SERVER_VERIFY_DECODE_THREADS", 1),
"-protocol_whitelist",
remote ? "http,https,tcp,tls" : "file",
"-f",
@@ -958,7 +1523,7 @@ async function decodeRecording(
]
: []),
]
- : packetProof
+ : !hashContent
? []
: [
...outputOptions,
@@ -1089,7 +1654,7 @@ async function decodeRecording(
}
stream.contentSha256 = digest.sha256;
}
- } else if (!packetProof) {
+ } else if (hashContent) {
for (const digest of diagnostics.digests) decodeLine(digest, streams);
}
result = validateEvidence(
@@ -1097,7 +1662,7 @@ async function decodeRecording(
options,
sourceAudioInput !== undefined || packetProof !== undefined,
videoTiming,
- !packetProof,
+ hashContent,
);
} catch (error) {
throw failure ?? error;
@@ -1181,12 +1746,168 @@ async function readRemoteIdentity(
}
}
+const DEFAULT_RANGED_VERIFY_PART_MB = 16;
+
+export function verifyDownloadConcurrency(
+ env: Record<string, string | undefined> = process.env,
+): number {
+ const value = Number(env.MEDIA_SERVER_VERIFY_DOWNLOAD_CONCURRENCY?.trim());
+ return Number.isInteger(value) && value > 1 ? Math.min(value, 16) : 1;
+}
+
+export function verifyDownloadPartBytes(
+ env: Record<string, string | undefined> = process.env,
+): number {
+ const value = Number(env.MEDIA_SERVER_VERIFY_DOWNLOAD_PART_MB?.trim());
+ const megabytes =
+ Number.isInteger(value) && value >= 1 && value <= 64
+ ? value
+ : DEFAULT_RANGED_VERIFY_PART_MB;
+ return megabytes * 1024 * 1024;
+}
+
+class RangedVerificationUnsupported extends Error {}
+
+// Same proof as the sequential read (every byte, hashed in order, pinned to one
+// ETag), fetched as concurrent ranges so large uploads re-read at network speed.
+async function hashRemoteRecordingRanged(
+ input: string,
+ objectIdentity: string,
+ fileSize: number,
+ abortSignal: AbortSignal,
+ concurrency: number,
+ partBytes: number,
+): Promise<string> {
+ const reads = new AbortController();
+ const signal = AbortSignal.any([abortSignal, reads.signal]);
+ const partCount = Math.ceil(fileSize / partBytes);
+ const readPart = async (index: number): Promise<Uint8Array> => {
+ const start = index * partBytes;
+ const end = Math.min(fileSize, start + partBytes) - 1;
+ for (let attempt = 0; ; attempt++) {
+ signal.throwIfAborted();
+ let response: Response | undefined;
+ try {
+ try {
+ response = await fetchMedia(input, {
+ headers: {
+ "If-Match": objectIdentity,
+ "X-Cap-Recording-Verification": "1",
+ Range: `bytes=${start}-${end}`,
+ },
+ signal,
+ });
+ } catch (error) {
+ if (error instanceof MediaTransferBudgetError) throw error;
+ throw new RecordingVerificationError(
+ "Recording bytes could not be read",
+ true,
+ );
+ }
+ if (response.status === 200) throw new RangedVerificationUnsupported();
+ if (response.status !== 206) {
+ throw new RecordingVerificationError(
+ `Recording byte verification failed: ${response.status}`,
+ response.status === 408 ||
+ response.status === 429 ||
+ response.status >= 500,
+ );
+ }
+ if (response.headers.get("etag") !== objectIdentity || !response.body)
+ throw new Error("Recording object changed during byte verification");
+ if (
+ response.headers.get("content-range") !==
+ `bytes ${start}-${end}/${fileSize}`
+ )
+ throw new Error(
+ "Recording byte resume range does not match the verified object",
+ );
+ const expected = end - start + 1;
+ const bytes = new Uint8Array(expected);
+ let received = 0;
+ try {
+ for await (const chunk of response.body) {
+ if (received + chunk.byteLength > expected)
+ throw new RecordingVerificationError(
+ "Recording object size changed during byte verification",
+ false,
+ );
+ bytes.set(chunk, received);
+ received += chunk.byteLength;
+ }
+ } catch (error) {
+ if (error instanceof RecordingVerificationError) throw error;
+ throw new RecordingVerificationError(
+ "Recording bytes could not be read completely",
+ true,
+ );
+ }
+ if (received !== expected)
+ throw new RecordingVerificationError(
+ "Recording bytes could not be read completely",
+ true,
+ );
+ return bytes;
+ } catch (error) {
+ signal.throwIfAborted();
+ if (
+ !(error instanceof RecordingVerificationError) ||
+ !error.retryable ||
+ attempt >= 3
+ )
+ throw error;
+ } finally {
+ if (!response?.body?.locked)
+ await response?.body?.cancel().catch(() => {});
+ }
+ await sleep(250 * 2 ** attempt, undefined, { signal });
+ }
+ };
+ const hash = createHash("sha256");
+ const inFlight = new Map<number, Promise<Uint8Array>>();
+ let requested = 0;
+ try {
+ for (let index = 0; index < partCount; index++) {
+ while (requested < partCount && requested < index + concurrency) {
+ const part = readPart(requested);
+ part.catch(() => {});
+ inFlight.set(requested, part);
+ requested++;
+ }
+ const bytes = await inFlight.get(index);
+ inFlight.delete(index);
+ if (!bytes) throw new Error("Recording byte range was not requested");
+ hash.update(bytes);
+ }
+ return hash.digest("hex");
+ } finally {
+ reads.abort();
+ await Promise.allSettled(inFlight.values());
+ }
+}
+
async function hashRemoteRecording(
input: string,
objectIdentity: string,
fileSize: number,
abortSignal: AbortSignal,
): Promise<string> {
+ const concurrency = verifyDownloadConcurrency();
+ const partBytes = verifyDownloadPartBytes();
+ if (concurrency > 1 && fileSize > partBytes) {
+ try {
+ return await hashRemoteRecordingRanged(
+ input,
+ objectIdentity,
+ fileSize,
+ abortSignal,
+ concurrency,
+ partBytes,
+ );
+ } catch (error) {
+ if (!(error instanceof RangedVerificationUnsupported)) throw error;
+ }
+ }
const hash = createHash("sha256");
let bytesRead = 0;
for (let attempt = 0; ; attempt++) {
diff --git a/apps/media-server/src/lib/runtime-report.ts b/apps/media-server/src/lib/runtime-report.ts
new file mode 100644
index 000000000..bfe0bace9
--- /dev/null
+++ b/apps/media-server/src/lib/runtime-report.ts
@@ -0,0 +1,60 @@
+import {
+ getActiveVideoProcessCount,
+ getAllJobs,
+ getJobTotals,
+ getMaxConcurrentVideoProcesses,
+ getSystemResources,
+} from "./job-manager";
+import { getDrainBlockers, getShutdownGraceMs, isDraining } from "./lifecycle";
+import {
+ getMaxConcurrentAudioOperations,
+ getMaxConcurrentProbeOperations,
+} from "./media-operations";
+import { getMediaTransferCacheUsage } from "./media-transfer";
+import { getTempDirUsage } from "./temp-files";
+import {
+ getActiveDirectVideoProcessCount,
+ getMaxConcurrentDirectVideoProcesses,
+} from "./video-capacity";
+
+export async function getRuntimeReport() {
+ const blockers = getDrainBlockers();
+ const jobs = getAllJobs();
+ return {
+ draining: isDraining(),
+ shutdownGraceMs: getShutdownGraceMs(),
+ processUptimeSeconds: Math.round(process.uptime()),
+ activeJobs: jobs.filter(
+ (job) =>
+ job.phase !== "complete" &&
+ job.phase !== "error" &&
+ job.phase !== "cancelled",
+ ).length,
+ jobsInMemory: jobs.length,
+ jobTotals: getJobTotals(),
+ activeSubprocesses: blockers.subprocesses,
+ backgroundTasks: blockers.backgroundTasks,
+ inFlightRequests: blockers.inFlightRequests,
+ slots: {
+ video: {
+ active: getActiveVideoProcessCount(),
+ max: getMaxConcurrentVideoProcesses(),
+ effectiveMax: getSystemResources().effectiveMax,
+ },
+ directVideo: {
+ active: getActiveDirectVideoProcessCount(),
+ max: getMaxConcurrentDirectVideoProcesses(),
+ },
+ audio: {
+ active: blockers.audioOperations,
+ max: getMaxConcurrentAudioOperations(),
+ },
+ probe: {
+ active: blockers.probeOperations,
+ max: getMaxConcurrentProbeOperations(),
+ },
+ },
+ tempDir: await getTempDirUsage(),
+ mediaTransferCache: getMediaTransferCacheUsage(),
+ };
+}
diff --git a/apps/media-server/src/lib/subprocess.ts b/apps/media-server/src/lib/subprocess.ts
index 31ac5ee4e..ffdf7c3f6 100644
--- a/apps/media-server/src/lib/subprocess.ts
+++ b/apps/media-server/src/lib/subprocess.ts
@@ -19,6 +19,10 @@ export function unregisterSubprocess(proc: ManagedSubprocess): void {
activeSubprocesses.delete(proc.pid);
}
+export function getActiveSubprocessCount(): number {
+ return activeSubprocesses.size;
+}
+
async function waitForProcessExit(
proc: Pick<Subprocess, "exited" | "exitCode">,
timeoutMs: number,
diff --git a/apps/media-server/src/lib/temp-files.ts b/apps/media-server/src/lib/temp-files.ts
index 5141ad6c4..0c8ec1fdf 100644
--- a/apps/media-server/src/lib/temp-files.ts
+++ b/apps/media-server/src/lib/temp-files.ts
@@ -1,18 +1,139 @@
import { randomUUID } from "node:crypto";
-import { mkdir, readdir, rm, stat, unlink } from "node:fs/promises";
+import type { Dirent, Stats } from "node:fs";
+import {
+ lstat,
+ mkdir,
+ readdir,
+ rm,
+ unlink,
+ utimes,
+ writeFile,
+} from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
-const TEMP_DIR = join(tmpdir(), "cap-media-server");
+const TEMP_ROOT = join(tmpdir(), "cap-media-server");
+const INSTANCE_ID = randomUUID();
+const INSTANCE_DIR_PREFIX = "instance-";
+const INSTANCE_LEASE_FILE = ".lease";
+const INSTANCE_LEASE_REFRESH_MS = 30_000;
+const INSTANCE_LEASE_STALE_MS = 5 * 60 * 1000;
const STALE_FILE_AGE_MS = 60 * 60 * 1000;
+const MAX_ACTIVE_TEMP_AGE_MS = 6 * 60 * 60 * 1000;
+const TEMP_MAINTENANCE_INTERVAL_MS = 5 * 60 * 1000;
+const TEMP_USAGE_CACHE_MS = 5_000;
+const TEMP_USAGE_MAX_ENTRIES = 20_000;
+
+const leasedDirs = new Set<string>();
+const activeTempPaths = new Set<string>();
+let leaseTimer: ReturnType<typeof setInterval> | undefined;
+let maintenanceTimer: ReturnType<typeof setInterval> | undefined;
export interface TempFileHandle {
path: string;
cleanup: () => Promise<void>;
}
+export interface TempDirUsage {
+ bytes: number;
+ entries: number;
+ truncated: boolean;
+}
+
+export function getInstanceDir(base: string): string {
+ return join(base, `${INSTANCE_DIR_PREFIX}${INSTANCE_ID}`);
+}
+
+const TEMP_DIR = getInstanceDir(TEMP_ROOT);
+
+async function refreshLeases(): Promise<void> {
+ const now = new Date();
+ await Promise.allSettled(
+ Array.from(leasedDirs, async (dir) => {
+ const lease = join(dir, INSTANCE_LEASE_FILE);
+ try {
+ await utimes(lease, now, now);
+ } catch {
+ await mkdir(dir, { recursive: true });
+ await writeFile(lease, "");
+ }
+ }),
+ );
+}
+
+export async function ensureInstanceDir(
+ base: string,
+ mode?: number,
+): Promise<string> {
+ const dir = getInstanceDir(base);
+ await mkdir(dir, { recursive: true, mode });
+ if (!leasedDirs.has(dir)) {
+ await writeFile(join(dir, INSTANCE_LEASE_FILE), "");
+ leasedDirs.add(dir);
+ if (!leaseTimer) {
+ leaseTimer = setInterval(() => {
+ void refreshLeases();
+ }, INSTANCE_LEASE_REFRESH_MS);
+ leaseTimer.unref?.();
+ }
+ }
+ return dir;
+}
+
+export async function releaseInstanceDir(base: string): Promise<void> {
+ const dir = getInstanceDir(base);
+ leasedDirs.delete(dir);
+ await rm(dir, { recursive: true, force: true }).catch(() => {});
+}
+
+// Instance directories carry a lease so a host that shares one temp volume
+// between an old (draining) and a new container never sweeps live work.
+export async function sweepOrphanedInstanceDirs(
+ base: string,
+ isLegacyOrphan: (name: string, stats: Stats) => boolean,
+): Promise<number> {
+ let entries: Dirent[];
+ try {
+ entries = await readdir(base, { withFileTypes: true });
+ } catch {
+ return 0;
+ }
+ const ownDir = getInstanceDir(base);
+ const now = Date.now();
+ let removed = 0;
+
+ for (const entry of entries) {
+ const path = join(base, entry.name);
+ if (path === ownDir) continue;
+ try {
+ const orphaned =
+ entry.isDirectory() && entry.name.startsWith(INSTANCE_DIR_PREFIX)
+ ? now -
+ (
+ await lstat(join(path, INSTANCE_LEASE_FILE)).catch(() =>
+ lstat(path),
+ )
+ ).mtimeMs >
+ INSTANCE_LEASE_STALE_MS
+ : isLegacyOrphan(entry.name, await lstat(path));
+ if (!orphaned) continue;
+ await rm(path, { recursive: true, force: true });
+ removed++;
+ } catch {}
+ }
+
+ return removed;
+}
+
export async function ensureTempDir(): Promise<void> {
- await mkdir(TEMP_DIR, { recursive: true });
+ await ensureInstanceDir(TEMP_ROOT);
+}
+
+export function retainTempPath(path: string): () => void {
+ activeTempPaths.add(path);
+ return () => {
+ activeTempPaths.delete(path);
+ };
}
export async function createTempFile(
@@ -21,6 +142,7 @@ export async function createTempFile(
await ensureTempDir();
const filename = `${randomUUID()}${extension.startsWith(".") ? extension : `.${extension}`}`;
const path = join(TEMP_DIR, filename);
+ const release = retainTempPath(path);
return {
path,
@@ -28,32 +150,116 @@ export async function createTempFile(
try {
await unlink(path);
} catch {}
+ release();
},
};
}
+export function sweepOrphanedTempDirs(): Promise<number> {
+ return sweepOrphanedInstanceDirs(
+ TEMP_ROOT,
+ (_name, stats) => Date.now() - stats.mtimeMs > STALE_FILE_AGE_MS,
+ );
+}
+
export async function cleanupStaleTempFiles(): Promise<number> {
+ let cleaned = await sweepOrphanedTempDirs();
+ let entries: string[];
try {
- await ensureTempDir();
- const files = await readdir(TEMP_DIR);
- const now = Date.now();
- let cleaned = 0;
+ entries = await readdir(TEMP_DIR);
+ } catch {
+ return cleaned;
+ }
+ const now = Date.now();
- for (const file of files) {
- const filePath = join(TEMP_DIR, file);
- try {
- const fileStat = await stat(filePath);
- if (now - fileStat.mtimeMs > STALE_FILE_AGE_MS) {
- await rm(filePath, { force: true });
- cleaned++;
- }
- } catch {}
- }
+ for (const entry of entries) {
+ if (entry === INSTANCE_LEASE_FILE) continue;
+ const path = join(TEMP_DIR, entry);
+ try {
+ const age = now - (await lstat(path)).mtimeMs;
+ if (
+ age <= STALE_FILE_AGE_MS ||
+ (activeTempPaths.has(path) && age <= MAX_ACTIVE_TEMP_AGE_MS)
+ )
+ continue;
+ await rm(path, { recursive: true, force: true });
+ activeTempPaths.delete(path);
+ cleaned++;
+ } catch {}
+ }
- return cleaned;
+ return cleaned;
+}
+
+export async function startTempDirMaintenance(): Promise<number> {
+ if (!maintenanceTimer) {
+ maintenanceTimer = setInterval(() => {
+ void cleanupStaleTempFiles()
+ .then((cleaned) => {
+ if (cleaned > 0)
+ console.log(`[temp-files] Removed ${cleaned} stale temp entries`);
+ })
+ .catch(() => {});
+ }, TEMP_MAINTENANCE_INTERVAL_MS);
+ maintenanceTimer.unref?.();
+ }
+ return await sweepOrphanedTempDirs();
+}
+
+export async function removeOwnTempDir(): Promise<void> {
+ activeTempPaths.clear();
+ await releaseInstanceDir(TEMP_ROOT);
+}
+
+async function measureDir(
+ dir: string,
+ usage: TempDirUsage,
+): Promise<TempDirUsage> {
+ let entries: Dirent[];
+ try {
+ entries = await readdir(dir, { withFileTypes: true });
} catch {
- return 0;
+ return usage;
+ }
+ for (const entry of entries) {
+ if (usage.entries >= TEMP_USAGE_MAX_ENTRIES) {
+ usage.truncated = true;
+ return usage;
+ }
+ usage.entries++;
+ const path = join(dir, entry.name);
+ if (entry.isDirectory()) {
+ await measureDir(path, usage);
+ } else {
+ usage.bytes += await lstat(path).then(
+ (stats) => stats.size,
+ () => 0,
+ );
+ }
}
+ return usage;
+}
+
+let tempUsageCache:
+ | { measuredAt: number; usage: Promise<TempDirUsage> }
+ | undefined;
+
+export function getTempDirUsage(): Promise<TempDirUsage> {
+ const now = Date.now();
+ if (
+ !tempUsageCache ||
+ now - tempUsageCache.measuredAt > TEMP_USAGE_CACHE_MS
+ ) {
+ tempUsageCache = {
+ measuredAt: now,
+ usage: measureDir(TEMP_ROOT, { bytes: 0, entries: 0, truncated: false }),
+ };
+ }
+ return tempUsageCache.usage;
+}
+
+export function getTempRoot(): string {
+ return TEMP_ROOT;
}
export function getTempDir(): string {
diff --git a/apps/media-server/src/lib/video-input-validation.ts b/apps/media-server/src/lib/video-input-validation.ts
index 41e265323..2adb2dbbe 100644
--- a/apps/media-server/src/lib/video-input-validation.ts
+++ b/apps/media-server/src/lib/video-input-validation.ts
@@ -1,5 +1,9 @@
import { spawn } from "bun";
-import { PROCESS_TIMEOUT_MS, withTimeout } from "./media-common";
+import {
+ decodeThreadsFromEnv,
+ PROCESS_TIMEOUT_MS,
+ withTimeout,
+} from "./media-common";
import { registerSubprocess, terminateProcess } from "./subprocess";
const MAX_DIAGNOSTIC_LENGTH = 8_192;
@@ -22,7 +26,7 @@ export async function validateVideoInput(
"-err_detect",
"explode+crccheck",
"-threads",
- "2",
+ decodeThreadsFromEnv("MEDIA_SERVER_VALIDATE_DECODE_THREADS", 2),
"-i",
inputPath,
"-map",
diff --git a/apps/media-server/src/routes/audio.ts b/apps/media-server/src/routes/audio.ts
index d2f8cc71e..bd4c3a12c 100644
--- a/apps/media-server/src/routes/audio.ts
+++ b/apps/media-server/src/routes/audio.ts
@@ -1,6 +1,14 @@
-import { Hono } from "hono";
+import { type Context, Hono } from "hono";
import { z } from "zod";
-import { validateMediaServerSecret } from "../lib/auth";
+import { canReadStatus, validateMediaServerSecret } from "../lib/auth";
+import {
+ DRAIN_RETRY_AFTER_SECONDS,
+ DRAINING_DETAILS,
+ getIdleTimeoutSeconds,
+ isDraining,
+ restoreIdleTimeoutOnFirstChunk,
+ setRequestIdleTimeout,
+} from "../lib/lifecycle";
import {
canAcceptNewAudioOperation as canAcceptNewProcess,
checkHasAudioTrack,
@@ -35,7 +43,22 @@ function isTimeoutError(err: unknown): boolean {
return err instanceof Error && err.message.includes("timed out");
}
+function drainingResponse(c: Context) {
+ c.header("Retry-After", DRAIN_RETRY_AFTER_SECONDS.toString());
+ return c.json(
+ {
+ error: "Server is busy",
+ code: "SERVER_BUSY",
+ details: DRAINING_DETAILS,
+ },
+ 503,
+ );
+}
+
audio.get("/status", (c) => {
+ if (!canReadStatus(c)) {
+ return c.json({ error: "Unauthorized" }, 401);
+ }
return c.json({
activeProcesses: getActiveProcessCount(),
canAcceptNewProcess: canAcceptNewProcess(),
@@ -61,6 +84,8 @@ audio.post("/check", async (c) => {
);
}
+ if (isDraining()) return drainingResponse(c);
+
try {
const hasAudio = await checkHasAudioTrack(result.data.videoUrl);
return c.json({ hasAudio });
@@ -120,6 +145,11 @@ audio.post("/extract", async (c) => {
}
const { videoUrl, stream: useStreaming } = result.data;
+ if (isDraining()) return drainingResponse(c);
+ setRequestIdleTimeout(c.env, c.req.raw, 0);
+ const restoreIdleTimeout = () =>
+ setRequestIdleTimeout(c.env, c.req.raw, getIdleTimeoutSeconds());
+ let streaming = false;
try {
const hasAudio = await checkHasAudioTrack(videoUrl);
@@ -137,12 +167,16 @@ audio.post("/extract", async (c) => {
cleanup();
});
- return new Response(stream, {
- headers: {
- "Content-Type": "audio/mpeg",
- "Transfer-Encoding": "chunked",
+ streaming = true;
+ return new Response(
+ restoreIdleTimeoutOnFirstChunk(stream, restoreIdleTimeout),
+ {
+ headers: {
+ "Content-Type": "audio/mpeg",
+ "Transfer-Encoding": "chunked",
+ },
},
- });
+ );
}
const audioData = await extractAudio(videoUrl);
@@ -186,6 +220,8 @@ audio.post("/extract", async (c) => {
},
500,
);
+ } finally {
+ if (!streaming) restoreIdleTimeout();
}
});
@@ -209,6 +245,11 @@ audio.post("/convert", async (c) => {
}
const { audioUrl, bitrate } = result.data;
+ if (isDraining()) return drainingResponse(c);
+ setRequestIdleTimeout(c.env, c.req.raw, 0);
+ const restoreIdleTimeout = () =>
+ setRequestIdleTimeout(c.env, c.req.raw, getIdleTimeoutSeconds());
+ let streaming = false;
try {
const { stream, cleanup } = extractAudioStream(audioUrl, {
@@ -220,12 +261,16 @@ audio.post("/convert", async (c) => {
cleanup();
});
- return new Response(stream, {
- headers: {
- "Content-Type": "audio/mpeg",
- "Transfer-Encoding": "chunked",
+ streaming = true;
+ return new Response(
+ restoreIdleTimeoutOnFirstChunk(stream, restoreIdleTimeout),
+ {
+ headers: {
+ "Content-Type": "audio/mpeg",
+ "Transfer-Encoding": "chunked",
+ },
},
- });
+ );
} catch (err) {
console.error("[audio/convert] Error:", err);
@@ -259,6 +304,8 @@ audio.post("/convert", async (c) => {
},
500,
);
+ } finally {
+ if (!streaming) restoreIdleTimeout();
}
});
diff --git a/apps/media-server/src/routes/health.ts b/apps/media-server/src/routes/health.ts
index 07b2731ab..c3fef26c6 100644
--- a/apps/media-server/src/routes/health.ts
+++ b/apps/media-server/src/routes/health.ts
@@ -1,11 +1,15 @@
import os from "node:os";
import { Hono } from "hono";
+import { hasMediaServerSecret } from "../lib/auth";
import { getSystemResources } from "../lib/job-manager";
+import { isDraining } from "../lib/lifecycle";
import { getMediaEngineStatus } from "../lib/media-engine";
+import { getVideoEncoderStatus } from "../lib/nvenc-encoder";
+import { getRuntimeReport } from "../lib/runtime-report";
const health = new Hono();
-health.get("/", (c) => {
+health.get("/", async (c) => {
const mediaEngine = getMediaEngineStatus();
const [, loadAvg5m, loadAvg15m] = os.loadavg();
const totalMemoryBytes = os.totalmem();
@@ -16,26 +20,36 @@ health.get("/", (c) => {
(1 - freeMemoryBytes / totalMemoryBytes) * 100,
);
const resources = getSystemResources();
+ const draining = isDraining();
+ const authenticated = hasMediaServerSecret(c);
- return c.json({
- status:
- mediaEngine.available && resources.effectiveMax > 0 ? "ok" : "degraded",
- mediaEngine,
- ["ff" + "mpeg"]: {
- available: mediaEngine.available,
- version: mediaEngine.version,
+ return c.json(
+ {
+ status: draining
+ ? "draining"
+ : mediaEngine.available && resources.effectiveMax > 0
+ ? "ok"
+ : "degraded",
+ mediaEngine,
+ ["ff" + "mpeg"]: {
+ available: mediaEngine.available,
+ version: mediaEngine.version,
+ },
+ videoEncoder: getVideoEncoderStatus(process.env, authenticated),
+ system: {
+ ...resources,
+ loadAvg5m,
+ loadAvg15m,
+ totalMemoryMB,
+ freeMemoryMB,
+ memoryUsagePercent,
+ uptimeSeconds: Math.round(os.uptime()),
+ processUptimeSeconds: Math.round(process.uptime()),
+ },
+ ...(authenticated && { runtime: await getRuntimeReport() }),
},
- system: {
- ...resources,
- loadAvg5m,
- loadAvg15m,
- totalMemoryMB,
- freeMemoryMB,
- memoryUsagePercent,
- uptimeSeconds: Math.round(os.uptime()),
- processUptimeSeconds: Math.round(process.uptime()),
- },
- });
+ draining ? 503 : 200,
+ );
});
export default health;
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index cd2b44e68..1bedd8777 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -4,7 +4,11 @@ import { file } from "bun";
import { Hono } from "hono";
import { z } from "zod";
import { enhanceLocalRecording } from "../lib/audio-levels";
-import { validateMediaServerSecret } from "../lib/auth";
+import {
+ canReadStatus,
+ hasMediaServerSecret,
+ validateMediaServerSecret,
+} from "../lib/auth";
import type {
Job,
RecordingErrorCode,
@@ -17,6 +21,7 @@ import {
claimRecordingWorker,
createJob,
deleteJob,
+ extendJobLifetime,
forceCleanupActiveJobs,
generateJobId,
getActiveVideoProcessCount,
@@ -30,8 +35,19 @@ import {
touchJob,
updateJob,
} from "../lib/job-manager";
-import { PROCESS_TIMEOUT_MS } from "../lib/media-common";
-import { renderEditedVideo } from "../lib/media-edit";
+import {
+ DRAINING_DETAILS,
+ getIdleTimeoutSeconds,
+ isDraining,
+ setRequestIdleTimeout,
+ trackBackgroundTask,
+} from "../lib/lifecycle";
+import { logReason } from "../lib/log-redaction";
+import { PROCESS_TIMEOUT_MS, UPLOAD_TIMEOUT_MS } from "../lib/media-common";
+import {
+ getTimeoutMs as getEditRenderTimeoutMs,
+ renderEditedVideo,
+} from "../lib/media-edit";
import {
canAcceptNewProbeOperation as canAcceptNewProbeProcess,
getActiveProbeOperationCount as getActiveProbeProcessCount,
@@ -47,6 +63,7 @@ import {
} from "../lib/media-transfer";
import type {
ResilientInputFlags,
+ StorageUploadReceipt,
StorageUploadTarget,
} from "../lib/media-video";
import {
@@ -54,6 +71,8 @@ import {
downloadVideoToTemp,
generatePreviewGif,
generateThumbnail,
+ getProcessTimeoutMs,
+ LOOM_IMPORT_MAX_BYTES,
muxMediaTracksToMp4,
processVideo,
repairContainer,
@@ -61,14 +80,19 @@ import {
uploadFileToStorage,
uploadToS3,
} from "../lib/media-video";
+import { encodeAttemptsBudgetMs } from "../lib/nvenc-encoder";
+import { findJobOnPeer, isPeerLookup } from "../lib/peer-jobs";
import { RecordingTimingError } from "../lib/recording-timing";
import {
hashRecordingFile,
isRetryableRecordingVerificationError,
+ verifyDecodeChunks,
+ verifyRecording,
verifyRemoteRecording,
verifyRemoteRecordingBytes,
verifyRemuxedRecording,
} from "../lib/recording-verification";
+import { getRuntimeReport } from "../lib/runtime-report";
import type { TempFileHandle } from "../lib/temp-files";
import { cleanupStaleTempFiles } from "../lib/temp-files";
import {
@@ -91,8 +115,11 @@ const PROBE_ERROR_CODE = ["FF", "PROBE_ERROR"].join("");
const VIDEO_BUSY_RETRY_AFTER_SECONDS = 15;
const VIDEO_MEMORY_PRESSURE_ERROR =
"SERVER_BUSY: container memory pressure is too high";
+const MUX_FAILURE_LOG_LENGTH = 1_000;
const SEGMENT_DOWNLOAD_MAX_ATTEMPTS = 3;
const SEGMENT_DOWNLOAD_RETRY_BASE_MS = 250;
+const JOB_TRANSFER_ALLOWANCE_MS = 2 * UPLOAD_TIMEOUT_MS;
+const JOB_TRANSFER_BYTES_PER_SECOND = 2 * 1024 ** 2;
const probeSchema = z.object({
videoUrl: z.string().url(),
@@ -111,6 +138,31 @@ const convertSchema = z.object({
inputExtension: z.string().optional(),
});
+const storageUploadSchema = z.discriminatedUnion("type", [
+ z.object({
+ type: z.literal("put"),
+ url: z.string().url(),
+ ifNoneMatch: z.literal("*").optional(),
+ }),
+ z.object({
+ type: z.literal("multipart"),
+ videoId: z.string(),
+ generation: z.string().min(1).max(200).optional(),
+ attemptId: z.string().min(1).max(200).optional(),
+ key: z.string().min(1),
+ uploadId: z.string().min(1),
+ partSize: z
+ .number()
+ .int()
+ .min(5 * 1024 * 1024),
+ signPartUrl: z.string().url(),
+ completeUrl: z.string().url(),
+ abortUrl: z.string().url(),
+ webhookSecret: z.string().optional(),
+ loomImportAttemptId: z.string().min(1).max(200).optional(),
+ }),
+]);
+
const processSchema = z.object({
audioLevels: z.boolean().optional(),
videoId: z.string(),
@@ -118,6 +170,16 @@ const processSchema = z.object({
videoUrl: z.string().url(),
outputPresignedUrl: z.string().url(),
sourcePresignedUrl: z.string().url().optional(),
+ sourceUpload: storageUploadSchema.optional(),
+ outputUpload: storageUploadSchema.optional(),
+ existingOutput: z
+ .object({
+ url: z.string().url(),
+ objectIdentity: z.string().regex(/^"[^"\r\n]+"$/),
+ fileSize: z.number().int().positive().max(LOOM_IMPORT_MAX_BYTES),
+ })
+ .optional(),
+ maxImportBytes: z.literal(LOOM_IMPORT_MAX_BYTES).optional(),
thumbnailPresignedUrl: z.string().url().optional(),
previewGifPresignedUrl: z.string().url().optional(),
webhookUrl: z.string().url().optional(),
@@ -220,6 +282,12 @@ function getMuxBusyResponseBody(
};
}
+const DRAINING_RESPONSE_BODY = {
+ error: "Server is busy",
+ code: "SERVER_BUSY",
+ details: DRAINING_DETAILS,
+};
+
function summarizeVideoInput(videoUrl: string, inputExtension?: string) {
const extension = inputExtension?.toLowerCase() ?? null;
try {
@@ -286,25 +354,26 @@ async function createVideoDownloadResponse(
onCleanup?.();
};
+ const reader = outputFile.stream().getReader();
const stream = new ReadableStream<Uint8Array>({
- async start(controller) {
- const reader = outputFile.stream().getReader();
-
+ async pull(controller) {
try {
- while (true) {
- const { done, value } = await reader.read();
- if (done) break;
- if (value) controller.enqueue(value);
+ const { done, value } = await reader.read();
+ if (done) {
+ controller.close();
+ reader.releaseLock();
+ await cleanup();
+ } else if (value) {
+ controller.enqueue(value);
}
- controller.close();
} catch (error) {
controller.error(error);
- } finally {
reader.releaseLock();
await cleanup();
}
},
async cancel() {
+ await reader.cancel().catch(() => {});
await cleanup();
},
});
@@ -345,6 +414,23 @@ async function withJobHeartbeat<T>(
}
}
+function extendJobLifetimeForWork(
+ jobId: string,
+ workTimeoutMs: number,
+ transferBytes: number,
+): void {
+ extendJobLifetime(
+ jobId,
+ encodeAttemptsBudgetMs(workTimeoutMs) +
+ JOB_TRANSFER_ALLOWANCE_MS +
+ POST_VERIFICATION_ASSET_BUDGET_MS +
+ Math.ceil(
+ (Math.max(0, transferBytes || 0) / JOB_TRANSFER_BYTES_PER_SECOND) *
+ 1000,
+ ),
+ );
+}
+
async function withMuxMemoryGuard<T>(
abortController: AbortController,
operation: () => Promise<T>,
@@ -373,13 +459,18 @@ async function withMuxMemoryGuard<T>(
}
}
-video.get("/status", (c) => {
+video.get("/status", async (c) => {
+ if (!canReadStatus(c)) {
+ return c.json({ error: "Unauthorized" }, 401);
+ }
const jobs = getAllJobs();
const resources = getSystemResources();
const now = Date.now();
return c.json({
instanceId: getInstanceId(),
pid: process.pid,
+ loomImportMaxBytes: LOOM_IMPORT_MAX_BYTES,
+ loomMultipartUploads: true,
activeVideoProcesses: getActiveVideoProcessCount(),
maxConcurrentVideoProcesses: getMaxConcurrentVideoProcesses(),
effectiveMaxVideoProcesses: resources.effectiveMax,
@@ -401,6 +492,7 @@ video.get("/status", (c) => {
stalenessMinutes: Math.round((now - j.updatedAt) / 60000),
error: j.error,
})),
+ ...(hasMediaServerSecret(c) && { runtime: await getRuntimeReport() }),
});
});
@@ -423,6 +515,11 @@ video.post("/probe", async (c) => {
);
}
+ if (isDraining()) {
+ c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
+ return c.json(DRAINING_RESPONSE_BODY, 503);
+ }
+
try {
const metadata = await probeVideo(result.data.videoUrl);
return c.json({ metadata });
@@ -481,6 +578,11 @@ video.post("/thumbnail", async (c) => {
);
}
+ if (isDraining()) {
+ c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
+ return c.json(DRAINING_RESPONSE_BODY, 503);
+ }
+
try {
const metadata = await probeVideo(result.data.videoUrl);
@@ -562,6 +664,17 @@ video.post("/convert", async (c) => {
const requestId = crypto.randomUUID();
const startedAt = Date.now();
+ if (isDraining()) {
+ c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
+ return c.json(
+ {
+ ...getBusyResponseBody(getVideoCapacitySnapshot()),
+ details: DRAINING_DETAILS,
+ },
+ 503,
+ );
+ }
+
try {
slot = tryAcquireDirectVideoProcessSlot(canAcceptNewVideoProcess);
const capacity = getVideoCapacitySnapshot();
@@ -583,6 +696,7 @@ video.post("/convert", async (c) => {
),
...capacity,
});
+ setRequestIdleTimeout(c.env, c.req.raw, 0);
inputTempFile = await downloadVideoToTemp(
result.data.videoUrl,
@@ -618,12 +732,14 @@ video.post("/convert", async (c) => {
...getVideoCapacitySnapshot(),
});
+ setRequestIdleTimeout(c.env, c.req.raw, getIdleTimeoutSeconds());
return await createVideoDownloadResponse(
outputTempFile,
[inputTempFile, outputTempFile],
slot.release,
);
} catch (err) {
+ setRequestIdleTimeout(c.env, c.req.raw, getIdleTimeoutSeconds());
slot?.release();
await cleanupTempFiles([outputTempFile, inputTempFile]);
logVideoEvent("video_convert_failed", {
@@ -676,7 +792,10 @@ video.on("POST", ["/process", "/import"], async (c) => {
const body = await c.req.json();
const result = (
c.req.path.endsWith("/import")
- ? processSchema.extend({ sourcePresignedUrl: z.string().url() })
+ ? processSchema.refine(
+ (input) => input.sourcePresignedUrl || input.sourceUpload,
+ { message: "An original video upload target is required" },
+ )
: processSchema
).safeParse(body);
@@ -691,7 +810,29 @@ video.on("POST", ["/process", "/import"], async (c) => {
);
}
+ if (
+ result.data.existingOutput &&
+ (result.data.sourceUpload ||
+ result.data.sourcePresignedUrl ||
+ result.data.outputUpload ||
+ result.data.maxImportBytes !== LOOM_IMPORT_MAX_BYTES)
+ ) {
+ return c.json(
+ {
+ error: "Existing output cannot be combined with upload targets",
+ code: "INVALID_REQUEST",
+ },
+ 400,
+ );
+ }
const capacity = getVideoCapacitySnapshot();
+ if (isDraining()) {
+ c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
+ return c.json(
+ { ...getBusyResponseBody(capacity), details: DRAINING_DETAILS },
+ 503,
+ );
+ }
const bulkCapacityReached =
result.data.priority === "bulk" &&
capacity.effectiveMaxVideoProcesses > 1 &&
@@ -715,34 +856,36 @@ video.on("POST", ["/process", "/import"], async (c) => {
const jobId = generateJobId();
const job = createJob(jobId, videoId, userId, webhookUrl, webhookSecret);
- withMediaTransfers(32 * 1024 ** 3, () =>
- processVideoAsync(
- job.jobId,
- videoUrl,
- outputPresignedUrl,
- thumbnailPresignedUrl,
- previewGifPresignedUrl,
- result.data,
- ),
- ).catch((err) => {
- console.error(
- `[video/process] Async processing error for job ${jobId}:`,
- err,
- );
- const currentJob = getJob(jobId);
- if (
- currentJob &&
- currentJob.phase !== "error" &&
- currentJob.phase !== "complete" &&
- currentJob.phase !== "cancelled"
- ) {
- updateJob(jobId, {
- phase: "error",
- error: err instanceof Error ? err.message : String(err),
- message: "Processing failed (unhandled)",
- });
- }
- });
+ void trackBackgroundTask(
+ withMediaTransfers(32 * 1024 ** 3, () =>
+ processVideoAsync(
+ job.jobId,
+ videoUrl,
+ outputPresignedUrl,
+ thumbnailPresignedUrl,
+ previewGifPresignedUrl,
+ result.data,
+ ),
+ ).catch((err) => {
+ console.error(
+ `[video/process] Async processing error for job ${jobId}:`,
+ err,
+ );
+ const currentJob = getJob(jobId);
+ if (
+ currentJob &&
+ currentJob.phase !== "error" &&
+ currentJob.phase !== "complete" &&
+ currentJob.phase !== "cancelled"
+ ) {
+ updateJob(jobId, {
+ phase: "error",
+ error: err instanceof Error ? err.message : String(err),
+ message: "Processing failed (unhandled)",
+ });
+ }
+ }),
+ );
return c.json({
jobId,
@@ -775,6 +918,17 @@ video.post("/edit", async (c) => {
);
}
+ if (isDraining()) {
+ c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
+ return c.json(
+ {
+ ...getBusyResponseBody(getVideoCapacitySnapshot()),
+ details: DRAINING_DETAILS,
+ },
+ 503,
+ );
+ }
+
if (!canAcceptNewVideoProcess()) {
c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
return c.json(getBusyResponseBody(getVideoCapacitySnapshot()), 503);
@@ -794,31 +948,33 @@ video.post("/edit", async (c) => {
const jobId = generateJobId();
const job = createJob(jobId, videoId, userId, webhookUrl, webhookSecret);
- withMediaTransfers(32 * 1024 ** 3, () =>
- editVideoAsync(
- job.jobId,
- sourceUrl,
- outputPresignedUrl,
- thumbnailPresignedUrl,
- previewGifPresignedUrl,
- result.data,
- ),
- ).catch((err) => {
- console.error(`[video/edit] Async edit error for job ${jobId}:`, err);
- const currentJob = getJob(jobId);
- if (
- currentJob &&
- currentJob.phase !== "error" &&
- currentJob.phase !== "complete" &&
- currentJob.phase !== "cancelled"
- ) {
- updateJob(jobId, {
- phase: "error",
- error: err instanceof Error ? err.message : String(err),
- message: "Edit failed (unhandled)",
- });
- }
- });
+ void trackBackgroundTask(
+ withMediaTransfers(32 * 1024 ** 3, () =>
+ editVideoAsync(
+ job.jobId,
+ sourceUrl,
+ outputPresignedUrl,
+ thumbnailPresignedUrl,
+ previewGifPresignedUrl,
+ result.data,
+ ),
+ ).catch((err) => {
+ console.error(`[video/edit] Async edit error for job ${jobId}:`, err);
+ const currentJob = getJob(jobId);
+ if (
+ currentJob &&
+ currentJob.phase !== "error" &&
+ currentJob.phase !== "complete" &&
+ currentJob.phase !== "cancelled"
+ ) {
+ updateJob(jobId, {
+ phase: "error",
+ error: err instanceof Error ? err.message : String(err),
+ message: "Edit failed (unhandled)",
+ });
+ }
+ }),
+ );
return c.json({
jobId,
@@ -963,6 +1119,11 @@ async function processWithResilientRetry(
progress: 10,
message: "Retrying with error recovery...",
});
+ extendJobLifetimeForWork(
+ jobId,
+ getProcessTimeoutMs(metadata.duration, PROCESS_TIMEOUT_MS),
+ metadata.fileSize,
+ );
try {
const outputFile = await processVideo(
@@ -988,6 +1149,11 @@ async function processWithResilientRetry(
progress: 10,
message: "Attempting full repair...",
});
+ extendJobLifetimeForWork(
+ jobId,
+ getProcessTimeoutMs(metadata.duration, PROCESS_TIMEOUT_MS),
+ metadata.fileSize,
+ );
let lastResortRepairFile: TempFileHandle | null = null;
try {
@@ -1032,15 +1198,30 @@ async function generateAndUploadPreviewGif(
): Promise<void> {
if (!previewGifPresignedUrl) return;
- let previewGifFile: TempFileHandle | null = null;
-
+ const render = generatePreviewGif(inputPath, duration, {}, abortSignal);
try {
- previewGifFile = await generatePreviewGif(
- inputPath,
- duration,
- {},
+ await uploadRenderedPreviewGif(
+ render,
+ previewGifPresignedUrl,
abortSignal,
+ logPrefix,
+ );
+ } finally {
+ await render.then(
+ (gif) => gif.cleanup(),
+ () => {},
);
+ }
+}
+
+async function uploadRenderedPreviewGif(
+ render: Promise<TempFileHandle>,
+ previewGifPresignedUrl: string,
+ abortSignal: AbortSignal | undefined,
+ logPrefix: string,
+): Promise<void> {
+ try {
+ const previewGifFile = await render;
await uploadFileToS3(
previewGifFile.path,
previewGifPresignedUrl,
@@ -1054,8 +1235,6 @@ async function generateAndUploadPreviewGif(
: new Error("Preview GIF generation aborted");
}
console.warn(`[${logPrefix}] Preview GIF generation failed:`, previewErr);
- } finally {
- await previewGifFile?.cleanup();
}
}
@@ -1075,21 +1254,25 @@ function isDurationClose(actual: number, expected: number) {
);
}
-async function waitForVerificationRetry(attempt: number) {
- await new Promise((resolve) =>
- setTimeout(resolve, UPLOAD_VERIFICATION_RETRY_MS * (attempt + 1)),
- );
+async function waitForVerificationRetry(
+ attempt: number,
+ abortSignal?: AbortSignal,
+) {
+ await Bun.sleep(UPLOAD_VERIFICATION_RETRY_MS * (attempt + 1));
+ abortSignal?.throwIfAborted();
}
async function verifyUploadedVideo(
outputVerificationUrl: string | undefined,
expectedMetadata: VideoMetadata,
+ abortSignal?: AbortSignal,
) {
if (!outputVerificationUrl) return;
let lastError: Error | undefined;
for (let attempt = 0; attempt < UPLOAD_VERIFICATION_ATTEMPTS; attempt++) {
+ abortSignal?.throwIfAborted();
try {
const actualMetadata = await probeVideo(outputVerificationUrl);
if (isDurationClose(actualMetadata.duration, expectedMetadata.duration)) {
@@ -1104,7 +1287,7 @@ async function verifyUploadedVideo(
}
if (attempt < UPLOAD_VERIFICATION_ATTEMPTS - 1) {
- await waitForVerificationRetry(attempt);
+ await waitForVerificationRetry(attempt, abortSignal);
}
}
@@ -1124,7 +1307,10 @@ async function editVideoAsync(
}
const abortController = new AbortController();
+ const signal = abortController.signal;
updateJob(jobId, { abortController });
+ let inputTempFile: TempFileHandle | null = null;
+ let outputTempFile: TempFileHandle | null = null;
try {
updateJob(jobId, {
@@ -1137,10 +1323,11 @@ async function editVideoAsync(
await sendWebhook(downloadingJob);
}
- const inputTempFile = await withJobHeartbeat(jobId, () =>
- downloadVideoToTemp(sourceUrl, ".mp4", abortController.signal),
+ const input = await withJobHeartbeat(jobId, () =>
+ downloadVideoToTemp(sourceUrl, ".mp4", signal),
);
- updateJob(jobId, { inputTempFile });
+ inputTempFile = input;
+ updateJob(jobId, { inputTempFile: input });
updateJob(jobId, {
phase: "probing",
@@ -1152,7 +1339,15 @@ async function editVideoAsync(
await sendWebhook(probingJob);
}
- const sourceMetadata = await probeVideoFile(inputTempFile.path);
+ const sourceMetadata = await withJobHeartbeat(jobId, () =>
+ probeVideoFile(input.path),
+ );
+ signal.throwIfAborted();
+ extendJobLifetimeForWork(
+ jobId,
+ getEditRenderTimeoutMs(options.keepRanges),
+ sourceMetadata.fileSize,
+ );
updateJob(jobId, {
phase: "processing",
@@ -1164,12 +1359,12 @@ async function editVideoAsync(
await sendWebhook(processingJob);
}
- const outputTempFile = await withJobHeartbeat(jobId, () =>
+ const output = await withJobHeartbeat(jobId, () =>
renderEditedVideo({
- inputPath: inputTempFile.path,
+ inputPath: input.path,
keepRanges: options.keepRanges,
metadata: sourceMetadata,
- abortSignal: abortController.signal,
+ abortSignal: signal,
onProgress: (progress, message) => {
updateJob(jobId, {
progress: Math.min(80, 10 + progress * 0.9),
@@ -1187,9 +1382,13 @@ async function editVideoAsync(
},
}),
);
- updateJob(jobId, { outputTempFile });
+ outputTempFile = output;
+ updateJob(jobId, { outputTempFile: output });
- const outputMetadata = await probeVideoFile(outputTempFile.path);
+ const outputMetadata = await withJobHeartbeat(jobId, () =>
+ probeVideoFile(output.path),
+ );
+ signal.throwIfAborted();
updateJob(jobId, { metadata: outputMetadata });
updateJob(jobId, {
@@ -1202,8 +1401,19 @@ async function editVideoAsync(
await sendWebhook(uploadingJob);
}
- await uploadFileToS3(outputTempFile.path, outputPresignedUrl, "video/mp4");
- await verifyUploadedVideo(options.outputVerificationUrl, outputMetadata);
+ await withJobHeartbeat(jobId, async () => {
+ await uploadFileToS3(
+ output.path,
+ outputPresignedUrl,
+ "video/mp4",
+ signal,
+ );
+ await verifyUploadedVideo(
+ options.outputVerificationUrl,
+ outputMetadata,
+ signal,
+ );
+ });
if (thumbnailPresignedUrl || previewGifPresignedUrl) {
updateJob(jobId, {
@@ -1217,21 +1427,31 @@ async function editVideoAsync(
}
}
- if (thumbnailPresignedUrl) {
- const thumbnailData = await generateThumbnail(
- outputTempFile.path,
+ await withJobHeartbeat(jobId, async () => {
+ if (thumbnailPresignedUrl) {
+ const thumbnailData = await generateThumbnail(
+ output.path,
+ outputMetadata.duration,
+ {},
+ signal,
+ );
+ await uploadToS3(
+ thumbnailData,
+ thumbnailPresignedUrl,
+ "image/jpeg",
+ signal,
+ );
+ }
+
+ await generateAndUploadPreviewGif(
+ output.path,
outputMetadata.duration,
+ previewGifPresignedUrl,
+ signal,
+ "video/edit",
);
- await uploadToS3(thumbnailData, thumbnailPresignedUrl, "image/jpeg");
- }
-
- await generateAndUploadPreviewGif(
- outputTempFile.path,
- outputMetadata.duration,
- previewGifPresignedUrl,
- abortController.signal,
- "video/edit",
- );
+ });
+ signal.throwIfAborted();
updateJob(jobId, {
phase: "complete",
@@ -1243,12 +1463,11 @@ async function editVideoAsync(
await sendWebhook(completedJob);
}
- await inputTempFile.cleanup();
- await outputTempFile.cleanup();
-
setTimeout(() => deleteJob(jobId), 5 * 60 * 1000);
} catch (err) {
- console.error(`[video/edit] Error editing job ${jobId}:`, err);
+ if (!signal.aborted) {
+ console.error(`[video/edit] Error editing job ${jobId}:`, err);
+ }
const updatedJob = updateJob(jobId, {
phase: "error",
@@ -1261,16 +1480,10 @@ async function editVideoAsync(
await sendWebhook(updatedJob);
}
} finally {
- const currentJob = getJob(jobId);
- if (currentJob) {
- await Promise.allSettled([
- currentJob.inputTempFile?.cleanup(),
- currentJob.outputTempFile?.cleanup(),
- ]);
- }
-
setTimeout(() => deleteJob(jobId), 5 * 60 * 1000);
}
+ } finally {
+ await cleanupTempFiles([inputTempFile, outputTempFile]);
}
}
@@ -1288,10 +1501,28 @@ async function processVideoAsync(
}
const abortController = new AbortController();
+ const signal = abortController.signal;
updateJob(jobId, { abortController });
+ let inputTempFile: TempFileHandle | null = null;
+ let outputTempFile: TempFileHandle | null = null;
let repairedTempFile: TempFileHandle | null = null;
let lastResortRepairFile: TempFileHandle | null = null;
+ let sourceUploaded = false;
+ let outputUploaded = false;
+ const abortPendingUploads = async () => {
+ const targets = [
+ !sourceUploaded ? options.sourceUpload : undefined,
+ !outputUploaded ? options.outputUpload : undefined,
+ ].filter((target): target is StorageUploadTarget => target !== undefined);
+ await Promise.all(
+ targets.map((target) =>
+ abortStorageUploadTarget(target).catch((error) => {
+ console.warn("Loom multipart cleanup failed", error);
+ }),
+ ),
+ );
+ };
try {
updateJob(jobId, {
@@ -1301,34 +1532,60 @@ async function processVideoAsync(
});
await sendWebhook(job);
- const inputTempFile = await withJobHeartbeat(jobId, () =>
+ const input = await withJobHeartbeat(jobId, () =>
downloadVideoToTemp(
- videoUrl,
+ options.existingOutput?.url ?? videoUrl,
options.inputExtension,
- abortController.signal,
+ signal,
+ options.maxImportBytes,
),
);
- updateJob(jobId, { inputTempFile });
+ inputTempFile = input;
+ updateJob(jobId, { inputTempFile: input });
+ if (options.existingOutput) {
+ const existing = options.existingOutput;
+ await withJobHeartbeat(jobId, async () => {
+ await verifyRecording(input.path, {
+ requireAudio: false,
+ allowObservedDuration: true,
+ abortSignal: signal,
+ });
+ await verifyRemoteRecordingBytes(existing.url, {
+ expectedObjectIdentity: existing.objectIdentity,
+ expectedFileSize: existing.fileSize,
+ expectedSha256: await hashRecordingFile(input.path, signal),
+ abortSignal: signal,
+ });
+ });
+ }
const sourcePresignedUrl = options.sourcePresignedUrl;
- if (sourcePresignedUrl) {
+ if (sourcePresignedUrl || options.sourceUpload) {
updateJob(jobId, { message: "Saving original video..." });
await sendWebhook(job);
await withJobHeartbeat(jobId, () =>
- uploadFileToS3(
- inputTempFile.path,
- sourcePresignedUrl,
- "video/mp4",
- abortController.signal,
- ),
+ options.sourceUpload
+ ? uploadFileToStorage(
+ input.path,
+ options.sourceUpload,
+ "video/mp4",
+ signal,
+ )
+ : uploadFileToS3(
+ input.path,
+ sourcePresignedUrl as string,
+ "video/mp4",
+ signal,
+ ),
);
+ sourceUploaded = true;
}
const isWebm = isWebmInput(options.inputExtension);
if (isWebm) {
updateJob(jobId, { message: "Checking the original recording..." });
await withJobHeartbeat(jobId, () =>
- validateVideoInput(inputTempFile.path, abortController.signal),
+ validateVideoInput(input.path, signal),
);
}
@@ -1339,17 +1596,24 @@ async function processVideoAsync(
});
await sendWebhook(job);
- const { metadata, repairedFile } = await probeWithRepairFallback(
- inputTempFile.path,
- isWebm,
- abortController.signal,
+ const { metadata, repairedFile } = await withJobHeartbeat(jobId, () =>
+ options.existingOutput
+ ? probeVideoFile(input.path).then((metadata) => ({
+ metadata,
+ repairedFile: null,
+ }))
+ : probeWithRepairFallback(input.path, isWebm, signal),
);
repairedTempFile = repairedFile;
+ signal.throwIfAborted();
updateJob(jobId, { metadata });
+ extendJobLifetimeForWork(
+ jobId,
+ getProcessTimeoutMs(metadata.duration, PROCESS_TIMEOUT_MS),
+ metadata.fileSize,
+ );
- const processingInputPath = repairedFile
- ? repairedFile.path
- : inputTempFile.path;
+ const processingInputPath = repairedFile ? repairedFile.path : input.path;
updateJob(jobId, {
phase: "processing",
@@ -1360,18 +1624,27 @@ async function processVideoAsync(
});
await sendWebhook(job);
- const { outputFile: outputTempFile, lastResortRepairFile: lrrf } =
- await processWithResilientRetry(
- processingInputPath,
- inputTempFile.path,
- metadata,
- options,
- isWebm,
- jobId,
- abortController.signal,
- );
- lastResortRepairFile = lrrf;
- updateJob(jobId, { outputTempFile });
+ const processed = options.existingOutput
+ ? { outputFile: input, lastResortRepairFile: null }
+ : await processWithResilientRetry(
+ processingInputPath,
+ input.path,
+ metadata,
+ options,
+ isWebm,
+ jobId,
+ signal,
+ );
+ outputTempFile = processed.outputFile;
+ lastResortRepairFile = processed.lastResortRepairFile;
+ const outputPath = processed.outputFile.path;
+ updateJob(jobId, { outputTempFile: processed.outputFile });
+ if (
+ options.maxImportBytes &&
+ file(outputPath).size > options.maxImportBytes
+ ) {
+ throw new Error("Loom imports are limited to 10 GB");
+ }
updateJob(jobId, {
phase: "uploading",
@@ -1380,54 +1653,93 @@ async function processVideoAsync(
});
await sendWebhook(job);
- const uploadReceipt = await uploadFileToS3(
- outputTempFile.path,
- outputPresignedUrl,
- "video/mp4",
- abortController.signal,
- );
-
- if (thumbnailPresignedUrl || previewGifPresignedUrl) {
- updateJob(jobId, {
- phase: "generating_thumbnail",
- progress: 90,
- message: "Generating preview assets...",
- });
- await sendWebhook(job);
- }
-
- if (thumbnailPresignedUrl) {
- // The uploaded MP4 has not been independently verified, so decode failures must still fail the job.
- const thumbnailData = await generateThumbnail(
- outputTempFile.path,
- metadata.duration,
- {},
- abortController.signal,
+ // Previews only read the finished output, so render them while it uploads; they are still
+ // uploaded after the output, so a failed upload never leaves previews behind.
+ let uploadReceipt: StorageUploadReceipt;
+ const previewAbort = new AbortController();
+ const previewSignal = AbortSignal.any([signal, previewAbort.signal]);
+ const thumbnailRender = thumbnailPresignedUrl
+ ? generateThumbnail(outputPath, metadata.duration, {}, previewSignal)
+ : undefined;
+ const previewGifRender = previewGifPresignedUrl
+ ? generatePreviewGif(outputPath, metadata.duration, {}, previewSignal)
+ : undefined;
+ thumbnailRender?.catch(() => {});
+ previewGifRender?.catch(() => {});
+ try {
+ uploadReceipt = await withJobHeartbeat(jobId, async () =>
+ options.existingOutput
+ ? { objectIdentity: options.existingOutput.objectIdentity }
+ : options.outputUpload
+ ? uploadFileToStorage(
+ outputPath,
+ options.outputUpload,
+ "video/mp4",
+ signal,
+ )
+ : uploadFileToS3(
+ outputPath,
+ outputPresignedUrl,
+ "video/mp4",
+ signal,
+ ),
);
- try {
- await uploadToS3(
- thumbnailData,
- thumbnailPresignedUrl,
- "image/jpeg",
- abortController.signal,
- );
- } catch (error) {
- abortController.signal.throwIfAborted();
- console.warn(
- `[video/process] Thumbnail upload failed for ${jobId}:`,
- error,
- );
+ outputUploaded = true;
+
+ if (thumbnailPresignedUrl || previewGifPresignedUrl) {
+ updateJob(jobId, {
+ phase: "generating_thumbnail",
+ progress: 90,
+ message: "Generating preview assets...",
+ });
+ await sendWebhook(job);
}
- }
- await generateAndUploadPreviewGif(
- outputTempFile.path,
- metadata.duration,
- previewGifPresignedUrl,
- abortController.signal,
- "video/process",
- );
- abortController.signal.throwIfAborted();
+ await withJobHeartbeat(jobId, async () => {
+ if (thumbnailRender && thumbnailPresignedUrl) {
+ // Only the fully verified reuse path can finish without a decoded preview.
+ const thumbnailData = await thumbnailRender.catch((error) => {
+ signal.throwIfAborted();
+ if (!options.existingOutput) throw error;
+ console.warn(
+ "Verified Loom output preview could not be generated",
+ error,
+ );
+ return undefined;
+ });
+ try {
+ if (thumbnailData)
+ await uploadToS3(
+ thumbnailData,
+ thumbnailPresignedUrl,
+ "image/jpeg",
+ signal,
+ );
+ } catch (error) {
+ signal.throwIfAborted();
+ console.warn(
+ `[video/process] Thumbnail upload failed for ${jobId}:`,
+ error,
+ );
+ }
+ }
+
+ if (previewGifRender && previewGifPresignedUrl)
+ await uploadRenderedPreviewGif(
+ previewGifRender,
+ previewGifPresignedUrl,
+ signal,
+ "video/process",
+ );
+ });
+ } finally {
+ previewAbort.abort();
+ await Promise.allSettled([
+ thumbnailRender,
+ previewGifRender?.then((gif) => gif.cleanup()),
+ ]);
+ }
+ signal.throwIfAborted();
updateJob(jobId, {
phase: "complete",
@@ -1441,7 +1753,7 @@ async function processVideoAsync(
if (options.audioLevels)
await enhanceLocalRecording({
- path: outputTempFile.path,
+ path: outputPath,
videoId: options.videoId,
userId: options.userId,
jobId,
@@ -1452,14 +1764,10 @@ async function processVideoAsync(
webhookSecret: options.webhookSecret,
});
- await inputTempFile.cleanup();
- await outputTempFile.cleanup();
- await repairedTempFile?.cleanup();
- await lastResortRepairFile?.cleanup();
-
setTimeout(() => deleteJob(jobId), 5 * 60 * 1000);
} catch (err) {
- if (!abortController.signal.aborted) {
+ await abortPendingUploads();
+ if (!signal.aborted) {
console.error(`[video/process] Error processing job ${jobId}:`, err);
}
const failedJob = getJob(jobId);
@@ -1469,7 +1777,7 @@ async function processVideoAsync(
failedJob.phase !== "cancelled" &&
failedJob.phase !== "error"
) {
- const cancelled = abortController.signal.aborted;
+ const cancelled = signal.aborted;
const updatedJob = updateJob(jobId, {
phase: cancelled ? "cancelled" : "error",
error: cancelled
@@ -1483,22 +1791,36 @@ async function processVideoAsync(
await sendWebhook(updatedJob);
}
}
-
- const currentJob = getJob(jobId);
- if (currentJob) {
- await currentJob.inputTempFile?.cleanup();
- await currentJob.outputTempFile?.cleanup();
- }
- await repairedTempFile?.cleanup();
- await lastResortRepairFile?.cleanup();
+ } finally {
+ await cleanupTempFiles([
+ inputTempFile,
+ outputTempFile,
+ repairedTempFile,
+ lastResortRepairFile,
+ ]);
}
}
video.get("/process/:jobId/status", async (c) => {
+ if (!canReadStatus(c)) {
+ return c.json({ error: "Unauthorized" }, 401);
+ }
const jobId = c.req.param("jobId");
const job = getJob(jobId);
+ const accept = c.req.header("Accept");
if (!job) {
+ if (
+ hasMediaServerSecret(c) &&
+ !isPeerLookup(c.req.raw) &&
+ !accept?.includes("text/event-stream")
+ ) {
+ const owner = await findJobOnPeer(
+ `/video/process/${encodeURIComponent(jobId)}/status`,
+ "GET",
+ );
+ if (owner) return owner;
+ }
return c.json(
{
error: "Job not found",
@@ -1510,8 +1832,6 @@ video.get("/process/:jobId/status", async (c) => {
);
}
- const accept = c.req.header("Accept");
-
if (accept?.includes("text/event-stream")) {
const stream = new ReadableStream({
start(controller) {
@@ -1581,6 +1901,13 @@ video.post("/process/:jobId/cancel", async (c) => {
const job = getJob(jobId);
if (!job) {
+ if (!isPeerLookup(c.req.raw)) {
+ const owner = await findJobOnPeer(
+ `/video/process/${encodeURIComponent(jobId)}/cancel`,
+ "POST",
+ );
+ if (owner) return owner;
+ }
return c.json(
{
error: "Job not found",
@@ -1648,30 +1975,6 @@ video.post("/force-cleanup", (c) => {
});
});
-const muxSegmentsOutputUploadSchema = z.discriminatedUnion("type", [
- z.object({
- type: z.literal("put"),
- url: z.string().url(),
- ifNoneMatch: z.literal("*").optional(),
- }),
- z.object({
- type: z.literal("multipart"),
- videoId: z.string(),
- generation: z.string().min(1).max(200).optional(),
- attemptId: z.string().min(1).max(200).optional(),
- key: z.string().min(1),
- uploadId: z.string().min(1),
- partSize: z
- .number()
- .int()
- .min(5 * 1024 * 1024),
- signPartUrl: z.string().url(),
- completeUrl: z.string().url(),
- abortUrl: z.string().url(),
- webhookSecret: z.string().optional(),
- }),
-]);
-
const strongObjectIdentitySchema = z
.string()
.max(1_024)
@@ -1845,6 +2148,16 @@ video.post("/verify-recording", async (c) => {
);
return c.json(recordingWorkerResponse(existing, ownerJobId));
}
+ if (isDraining()) {
+ c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
+ return c.json(
+ {
+ ...getMuxBusyResponseBody(getVideoCapacitySnapshot()),
+ details: DRAINING_DETAILS,
+ },
+ 503,
+ );
+ }
if (!canAcceptNewVideoProcess(body.processingPriority)) {
c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
return c.json(getMuxBusyResponseBody(getVideoCapacitySnapshot()), 503);
@@ -1877,19 +2190,34 @@ video.post("/verify-recording", async (c) => {
deleteJob(jobId);
return c.json(recordingWorkerResponse(job, ownerJobId));
}
- void withMediaTransfers(body.downloadBudgetBytes ?? body.fileSize * 3, () =>
- verifyUploadedRecordingAsync(jobId, body),
- ).catch((error: unknown) => {
- const current = getJob(jobId);
- if (!current || ["complete", "error", "cancelled"].includes(current.phase))
- return;
- updateJob(jobId, {
- phase: "error",
- error: "Recording transfer could not complete",
- errorCode: classifySourceError(error),
- });
- sendCurrentJobWebhook(jobId);
- });
+ void trackBackgroundTask(
+ withMediaTransfers(body.downloadBudgetBytes ?? body.fileSize * 3, () =>
+ verifyUploadedRecordingAsync(jobId, body),
+ ).catch((error: unknown) => {
+ const current = getJob(jobId);
+ if (
+ !current ||
+ ["complete", "error", "cancelled"].includes(current.phase)
+ )
+ return;
+ const errorCode = classifySourceError(error);
+ logVideoEvent("recording_verification_failed", {
+ jobId,
+ videoId: body.videoId,
+ phase: "error",
+ errorCode,
+ retryable: errorCode === "processing-unavailable",
+ stage: "transfer",
+ reason: logReason(error),
+ });
+ updateJob(jobId, {
+ phase: "error",
+ error: "Recording transfer could not complete",
+ errorCode,
+ });
+ sendCurrentJobWebhook(jobId);
+ }),
+ );
return c.json(recordingWorkerResponse(job, jobId));
});
@@ -1900,21 +2228,35 @@ async function verifyUploadedRecordingAsync(
const abortController = new AbortController();
if (!updateJob(jobId, { abortController, phase: "processing", progress: 0 }))
return;
+ let stage = "probe";
+ let cause: unknown;
+ let mismatch: Record<string, unknown> | undefined;
try {
const metadata = await probeVideo(body.videoUrl).catch((error: unknown) => {
if (error instanceof MediaTransferBudgetError) throw error;
+ cause = error;
throw new Error(RECORDING_VERIFICATION_RETRY_ERROR);
});
+ stage = "metadata";
if (
metadata.fileSize !== body.fileSize ||
(body.duration !== undefined &&
!isDurationClose(metadata.duration, body.duration)) ||
(body.requiredAudio && !metadata.audioCodec)
) {
+ mismatch = {
+ expectedFileSize: body.fileSize,
+ actualFileSize: metadata.fileSize,
+ expectedDuration: body.duration,
+ actualDuration: metadata.duration,
+ requiredAudio: body.requiredAudio,
+ audioCodec: metadata.audioCodec,
+ };
throw new Error(
"Uploaded recording does not match the completed local file",
);
}
+ stage = "decode";
if (
!beginRecordingVerification(
jobId,
@@ -1933,10 +2275,12 @@ async function verifyUploadedRecordingAsync(
body.sourceObjectIdentity ?? body.objectIdentity,
expectedFileSize: body.fileSize,
hashContent: Boolean(body.generation),
+ hashDecodedContent: false,
abortSignal: abortController.signal,
}),
),
);
+ stage = "identity";
if (verified.fileSize !== body.fileSize)
throw new Error("Verified recording size does not match the local file");
if (body.generation && !verified.remoteSha256)
@@ -1974,15 +2318,27 @@ async function verifyUploadedRecordingAsync(
(error instanceof Error &&
(error.message === VIDEO_MEMORY_PRESSURE_ERROR ||
error.message === RECORDING_VERIFICATION_RETRY_ERROR));
+ const phase =
+ !retryable && abortController.signal.aborted ? "cancelled" : "error";
+ const errorCode = retryable
+ ? "processing-unavailable"
+ : classifySourceError(error);
+ logVideoEvent("recording_verification_failed", {
+ jobId,
+ videoId: body.videoId,
+ phase,
+ errorCode,
+ retryable: errorCode === "processing-unavailable",
+ stage,
+ reason: logReason(cause ?? error),
+ ...(mismatch && { mismatch }),
+ });
updateJob(jobId, {
- phase:
- !retryable && abortController.signal.aborted ? "cancelled" : "error",
+ phase,
error: retryable
? RECORDING_VERIFICATION_RETRY_ERROR
: "Uploaded recording content could not be verified; retain the local recording",
- errorCode: retryable
- ? "processing-unavailable"
- : classifySourceError(error),
+ errorCode,
});
}
const job = getJob(jobId);
@@ -2013,7 +2369,7 @@ const muxSegmentsSchema = z
videoId: z.string(),
userId: z.string(),
outputPresignedUrl: z.string().url().optional(),
- outputUpload: muxSegmentsOutputUploadSchema.optional(),
+ outputUpload: storageUploadSchema.optional(),
thumbnailPresignedUrl: z.string().url().optional(),
previewGifPresignedUrl: z.string().url().optional(),
webhookUrl: z.string().url().optional(),
@@ -2174,6 +2530,17 @@ video.post("/mux-segments", async (c) => {
});
}
+ if (isDraining()) {
+ c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
+ return c.json(
+ {
+ ...getMuxBusyResponseBody(getVideoCapacitySnapshot()),
+ details: DRAINING_DETAILS,
+ },
+ 503,
+ );
+ }
+
if (!canAcceptNewVideoProcess(body.data.processingPriority)) {
c.header("Retry-After", VIDEO_BUSY_RETRY_AFTER_SECONDS.toString());
return c.json(getMuxBusyResponseBody(getVideoCapacitySnapshot()), 503);
@@ -2215,36 +2582,38 @@ video.post("/mux-segments", async (c) => {
});
}
- withMediaTransfers(body.data.downloadBudgetBytes ?? 32 * 1024 ** 3, () =>
- muxSegmentsAsync(
- jobId,
- videoId,
- outputUpload,
- thumbnailPresignedUrl,
- previewGifPresignedUrl,
- videoInitUrl,
- videoSegUrls,
- audioInitUrl ?? null,
- audioSegUrls ?? null,
- body.data.outputVerificationUrl,
- body.data,
- ),
- ).catch((err) => {
- console.error(`[mux-segments] Async mux error for job ${jobId}:`, err);
- const currentJob = getJob(jobId);
- if (
- currentJob &&
- currentJob.phase !== "error" &&
- currentJob.phase !== "complete" &&
- currentJob.phase !== "cancelled"
- ) {
- updateJob(jobId, {
- phase: "error",
- error: err instanceof Error ? err.message : String(err),
- });
- sendCurrentJobWebhook(jobId);
- }
- });
+ void trackBackgroundTask(
+ withMediaTransfers(body.data.downloadBudgetBytes ?? 32 * 1024 ** 3, () =>
+ muxSegmentsAsync(
+ jobId,
+ videoId,
+ outputUpload,
+ thumbnailPresignedUrl,
+ previewGifPresignedUrl,
+ videoInitUrl,
+ videoSegUrls,
+ audioInitUrl ?? null,
+ audioSegUrls ?? null,
+ body.data.outputVerificationUrl,
+ body.data,
+ ),
+ ).catch((err) => {
+ console.error(`[mux-segments] Async mux error for job ${jobId}:`, err);
+ const currentJob = getJob(jobId);
+ if (
+ currentJob &&
+ currentJob.phase !== "error" &&
+ currentJob.phase !== "complete" &&
+ currentJob.phase !== "cancelled"
+ ) {
+ updateJob(jobId, {
+ phase: "error",
+ error: err instanceof Error ? err.message : String(err),
+ });
+ sendCurrentJobWebhook(jobId);
+ }
+ }),
+ );
return c.json({
...recordingWorkerResponse(job, jobId),
@@ -2546,6 +2915,22 @@ async function downloadSegmentsBatchTracked(
return outputPaths;
}
+function muxFailureErrorCode(
+ error: unknown,
+ stageErrorCode: RecordingErrorCode,
+): RecordingErrorCode {
+ if (error instanceof MediaTransferBudgetError)
+ return "processing-budget-exhausted";
+ if (error instanceof MediaDownloadError) return error.errorCode;
+ if (
+ isRetryableRecordingVerificationError(error) ||
+ isBusyError(error) ||
+ isTimeoutError(error)
+ )
+ return "processing-unavailable";
+ return stageErrorCode;
+}
+
function sendCurrentJobWebhook(jobId: string): void {
const job = getJob(jobId);
if (!job) {
@@ -2568,15 +2953,14 @@ async function muxSegmentsAsync(
outputVerificationUrl?: string,
context: MuxContext = {},
): Promise<void> {
- const { ensureTempDir } = await import("../lib/temp-files");
+ const { ensureTempDir, getTempDir, retainTempPath } = await import(
+ "../lib/temp-files"
+ );
const { lstat, mkdir, rm } = await import("node:fs/promises");
const { join } = await import("node:path");
- const workDir = join(
- (await import("node:os")).tmpdir(),
- "cap-media-server",
- `mux-${jobId}`,
- );
+ const workDir = join(getTempDir(), `mux-${jobId}`);
+ const releaseWorkDir = retainTempPath(workDir);
const abortController = new AbortController();
updateJob(jobId, { abortController });
let outputUploadStarted = false;
@@ -2586,6 +2970,8 @@ async function muxSegmentsAsync(
? new Map(context.sourceObjects.map((source) => [source.url, source]))
: undefined;
const startedAt = Date.now();
+ let previewAssets: Promise<PreviewAssets> | undefined;
+ const previewAbort = new AbortController();
try {
if (!beginRecordingProcessing(jobId, SEGMENTED_RECORDING_TIMEOUT_MS))
@@ -2666,12 +3052,19 @@ async function muxSegmentsAsync(
const combinedVideoPath = join(workDir, "combined_video.mp4");
const videoInitPath = join(videoDir, "init.mp4");
- await streamConcatFiles(
- [videoInitPath, ...videoSegmentFiles],
- combinedVideoPath,
- abortController.signal,
+ await withJobHeartbeat(jobId, () =>
+ streamConcatFiles(
+ [videoInitPath, ...videoSegmentFiles],
+ combinedVideoPath,
+ abortController.signal,
+ ),
);
- await rm(videoDir, { recursive: true, force: true });
+ // Parallel verification decodes runs of these fragments; otherwise free them now.
+ const videoChunks =
+ verifyDecodeChunks() > 1
+ ? { init: videoInitPath, segments: videoSegmentFiles }
+ : undefined;
+ if (!videoChunks) await rm(videoDir, { recursive: true, force: true });
let combinedAudioPath: string | null = null;
@@ -2679,10 +3072,13 @@ async function muxSegmentsAsync(
combinedAudioPath = join(workDir, "combined_audio.mp4");
const audioInitPath = join(audioDir, "init.mp4");
- await streamConcatFiles(
- [audioInitPath, ...audioSegmentFiles],
- combinedAudioPath,
- abortController.signal,
+ const audioOutputPath = combinedAudioPath;
+ await withJobHeartbeat(jobId, () =>
+ streamConcatFiles(
+ [audioInitPath, ...audioSegmentFiles],
+ audioOutputPath,
+ abortController.signal,
+ ),
);
await rm(audioDir, { recursive: true, force: true });
}
@@ -2744,9 +3140,11 @@ async function muxSegmentsAsync(
});
},
},
+ videoChunks,
),
),
);
+ if (videoChunks) await rm(videoDir, { recursive: true, force: true });
logVideoEvent("video_mux_verification_complete", {
jobId,
videoId,
@@ -2788,8 +3186,21 @@ async function muxSegmentsAsync(
durationMs: Date.now() - startedAt,
resources: getSystemResources(),
});
+ const outputReadyAt = Date.now();
+ // The result is final here, so render previews while the upload and its
+ // remote check run; they are still only uploaded once the recording passes.
+ previewAssets = renderPreviewAssets(
+ resultPath,
+ localVerified.video.duration,
+ Boolean(thumbnailPresignedUrl),
+ Boolean(previewGifPresignedUrl),
+ AbortSignal.any([abortController.signal, previewAbort.signal]),
+ videoId,
+ );
- let metadata = await probeVideoFile(resultPath);
+ let metadata = await withJobHeartbeat(jobId, () =>
+ probeVideoFile(resultPath),
+ );
if (
!Number.isFinite(metadata.duration) ||
metadata.duration <= 0 ||
@@ -2903,37 +3314,43 @@ async function muxSegmentsAsync(
sendCurrentJobWebhook(jobId);
}
- if (thumbnailPresignedUrl) {
- try {
- const duration = metadata?.duration ?? 0;
- const thumbnailData = await generateThumbnail(
- resultPath,
- duration,
- {},
- abortController.signal,
- );
- await uploadToS3(
- thumbnailData,
- thumbnailPresignedUrl,
- "image/jpeg",
- abortController.signal,
- );
- } catch (thumbErr) {
- abortController.signal.throwIfAborted();
- console.warn(
- `[mux-segments] Thumbnail generation failed for ${videoId}:`,
- thumbErr,
- );
+ const uploadedAt = Date.now();
+ await withJobHeartbeat(jobId, async () => {
+ const assets = await previewAssets;
+ abortController.signal.throwIfAborted();
+ if (thumbnailPresignedUrl && assets?.thumbnail) {
+ try {
+ await uploadToS3(
+ assets.thumbnail,
+ thumbnailPresignedUrl,
+ "image/jpeg",
+ abortController.signal,
+ );
+ } catch (thumbErr) {
+ abortController.signal.throwIfAborted();
+ console.warn(
+ `[mux-segments] Thumbnail generation failed for ${videoId}:`,
+ thumbErr,
+ );
+ }
}
- }
-
- await generateAndUploadPreviewGif(
- resultPath,
- metadata?.duration ?? 0,
- previewGifPresignedUrl,
- abortController.signal,
- "mux-segments",
- );
+ if (previewGifPresignedUrl && assets?.previewGif) {
+ try {
+ await uploadFileToS3(
+ assets.previewGif.path,
+ previewGifPresignedUrl,
+ "image/gif",
+ abortController.signal,
+ );
+ } catch (previewErr) {
+ abortController.signal.throwIfAborted();
+ console.warn(
+ "[mux-segments] Preview GIF generation failed:",
+ previewErr,
+ );
+ }
+ }
+ });
abortController.signal.throwIfAborted();
updateJob(jobId, {
@@ -2947,6 +3364,8 @@ async function muxSegmentsAsync(
jobId,
videoId,
durationMs: Date.now() - startedAt,
+ uploadMs: uploadedAt - outputReadyAt,
+ previewMs: Date.now() - uploadedAt,
metadata,
resources: getSystemResources(),
});
@@ -2977,7 +3396,8 @@ async function muxSegmentsAsync(
jobId,
videoId,
durationMs: Date.now() - startedAt,
- error: error instanceof Error ? error.message : String(error),
+ errorCode: muxFailureErrorCode(error, errorCode),
+ error: logReason(error, MUX_FAILURE_LOG_LENGTH),
resources: getSystemResources(),
});
if (!outputUploadStarted) {
@@ -2994,16 +3414,7 @@ async function muxSegmentsAsync(
abortController.signal.aborted && !isBusyError(error)
? "cancelled"
: "error",
- errorCode:
- error instanceof MediaTransferBudgetError
- ? "processing-budget-exhausted"
- : error instanceof MediaDownloadError
- ? error.errorCode
- : isRetryableRecordingVerificationError(error) ||
- isBusyError(error) ||
- isTimeoutError(error)
- ? "processing-unavailable"
- : errorCode,
+ errorCode: muxFailureErrorCode(error, errorCode),
error: isRetryableRecordingVerificationError(error)
? "Recording verification temporarily unavailable (503)"
: error instanceof Error
@@ -3013,8 +3424,48 @@ async function muxSegmentsAsync(
sendCurrentJobWebhook(jobId);
} finally {
if (processingTimeout) clearTimeout(processingTimeout);
+ // Previews still rendering here belong to a job that failed before using them.
+ previewAbort.abort();
+ await (await previewAssets)?.previewGif?.cleanup();
await rm(workDir, { recursive: true, force: true }).catch(() => {});
+ releaseWorkDir();
}
}
+interface PreviewAssets {
+ thumbnail: Uint8Array | null;
+ previewGif: TempFileHandle | null;
+}
+
+// Never rejects: a failed preview only costs the asset, and the caller checks for abort.
+async function renderPreviewAssets(
+ resultPath: string,
+ duration: number,
+ thumbnail: boolean,
+ previewGif: boolean,
+ signal: AbortSignal,
+ videoId: string,
+): Promise<PreviewAssets> {
+ const [thumbnailResult, previewGifResult] = await Promise.allSettled([
+ thumbnail ? generateThumbnail(resultPath, duration, {}, signal) : null,
+ previewGif ? generatePreviewGif(resultPath, duration, {}, signal) : null,
+ ]);
+ if (thumbnailResult.status === "rejected" && !signal.aborted)
+ console.warn(
+ `[mux-segments] Thumbnail generation failed for ${videoId}:`,
+ thumbnailResult.reason,
+ );
+ if (previewGifResult.status === "rejected" && !signal.aborted)
+ console.warn(
+ "[mux-segments] Preview GIF generation failed:",
+ previewGifResult.reason,
+ );
+ return {
+ thumbnail:
+ thumbnailResult.status === "fulfilled" ? thumbnailResult.value : null,
+ previewGif:
+ previewGifResult.status === "fulfilled" ? previewGifResult.value : null,
+ };
+}
+
export default video;
diff --git a/apps/mobile/app/(tabs)/upload.tsx b/apps/mobile/app/(tabs)/upload.tsx
index 0f0a76369..4e509d9a0 100644
--- a/apps/mobile/app/(tabs)/upload.tsx
+++ b/apps/mobile/app/(tabs)/upload.tsx
@@ -166,6 +166,9 @@ export default function UploadScreen() {
const mountedRef = useRef(true);
const activeIdRef = useRef<string | null>(null);
const sourceBusyRef = useRef(false);
+ const processingIdsRef = useRef(new Set<string>());
+ const processingClientRef = useRef<typeof auth.client | null>(null);
+ processingClientRef.current = auth.status === "signedIn" ? auth.client : null;
const uploadSourceError =
sourceError?.source === "files" || sourceError?.source === "photos"
? sourceError.message
@@ -340,14 +343,27 @@ export default function UploadScreen() {
};
const waitForProcessing = async (queueItemId: string, capId: string) => {
- if (auth.status !== "signedIn") return;
+ if (!mountedRef.current || processingClientRef.current !== auth.client) {
+ return;
+ }
+
+ processingIdsRef.current.add(queueItemId);
+ const isActive = () =>
+ mountedRef.current &&
+ processingClientRef.current === auth.client &&
+ processingIdsRef.current.has(queueItemId);
- for (const delayMs of processingPollDelaysMs) {
- await new Promise((resolve) => setTimeout(resolve, delayMs));
- if (!mountedRef.current) return;
+ try {
+ for (let attempt = 0; isActive(); attempt += 1) {
+ const delayMs =
+ processingPollDelaysMs[
+ Math.min(attempt, processingPollDelaysMs.length - 1)
+ ];
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
+ if (!isActive()) return;
- try {
const detail = await auth.client.getCap(capId);
+ if (!isActive()) return;
const action = uploadQueueActionFromCapUpload(
queueItemId,
detail.cap.upload,
@@ -361,9 +377,11 @@ export default function UploadScreen() {
return;
}
}
- } catch {
- return;
}
+ } catch {
+ return;
+ } finally {
+ processingIdsRef.current.delete(queueItemId);
}
};
@@ -508,6 +526,7 @@ export default function UploadScreen() {
const removeQueueItem = (item: UploadQueueItem) => {
if (activeIdRef.current !== null) return;
+ processingIdsRef.current.delete(item.id);
dispatch({ type: "remove", id: item.id });
};
diff --git a/apps/mobile/src/auth/AuthContext.tsx b/apps/mobile/src/auth/AuthContext.tsx
index 7c4902a5b..e80613331 100644
--- a/apps/mobile/src/auth/AuthContext.tsx
+++ b/apps/mobile/src/auth/AuthContext.tsx
@@ -9,6 +9,7 @@ import {
useContext,
useEffect,
useMemo,
+ useRef,
useState,
} from "react";
import {
@@ -70,6 +71,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
null,
);
const [loading, setLoading] = useState(true);
+ const sessionRevision = useRef(0);
const client = useMemo(
() =>
@@ -89,8 +91,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
);
const refresh = useCallback(async () => {
+ const revision = sessionRevision.current;
const response = await client.bootstrap();
- setBootstrap(response);
+ if (revision === sessionRevision.current) setBootstrap(response);
}, [client]);
useEffect(() => {
@@ -126,7 +129,10 @@ export function AuthProvider({ children }: { children: ReactNode }) {
return;
}
+ const revision = sessionRevision.current;
refresh().catch(() => {
+ if (revision !== sessionRevision.current) return;
+ sessionRevision.current += 1;
setApiKey(null);
setUserId(null);
setBootstrap(null);
@@ -137,6 +143,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const storeSession = useCallback(
async (session: { apiKey: string; userId: string | null }) => {
+ sessionRevision.current += 1;
await SecureStore.setItemAsync(
sessionKey,
session.apiKey,
@@ -219,6 +226,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
);
const signOut = useCallback(async () => {
+ sessionRevision.current += 1;
if (apiKey) {
await client.revokeSession().catch(() => {});
}
@@ -226,6 +234,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
SecureStore.deleteItemAsync(sessionKey),
SecureStore.deleteItemAsync(userIdKey),
]);
+ sessionRevision.current += 1;
setApiKey(null);
setUserId(null);
setBootstrap(null);
@@ -233,8 +242,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const setActiveOrganization = useCallback(
async (organizationId: string) => {
+ const revision = sessionRevision.current;
const nextBootstrap = await client.setActiveOrganization(organizationId);
- setBootstrap(nextBootstrap);
+ if (revision === sessionRevision.current) setBootstrap(nextBootstrap);
},
[client],
);
diff --git a/apps/mobile/src/auth/AuthProvider.test.tsx b/apps/mobile/src/auth/AuthProvider.test.tsx
index 05f98ea89..469e20d3c 100644
--- a/apps/mobile/src/auth/AuthProvider.test.tsx
+++ b/apps/mobile/src/auth/AuthProvider.test.tsx
@@ -31,6 +31,8 @@ const apiMock = vi.hoisted(() => ({
workosAuthAvailable: true,
}),
),
+ revokeSession: vi.fn(() => Promise.resolve({ success: true })),
+ setActiveOrganization: vi.fn(),
createSessionRequestUrl: vi.fn(
() => "https://cap.so/api/mobile/session/request",
),
@@ -70,8 +72,8 @@ vi.mock("@/api/mobile", () => ({
createMobileApiClient: vi.fn(() => ({
bootstrap: apiMock.bootstrap,
getAuthConfig: apiMock.getAuthConfig,
- revokeSession: vi.fn(() => Promise.resolve({ success: true })),
- setActiveOrganization: vi.fn(),
+ revokeSession: apiMock.revokeSession,
+ setActiveOrganization: apiMock.setActiveOrganization,
})),
createSessionRequestUrl: apiMock.createSessionRequestUrl,
}));
@@ -87,6 +89,16 @@ const flushMicrotasks = async () => {
await Promise.resolve();
};
+const createDeferred = <T,>() => {
+ let resolve!: (value: T) => void;
+ let reject!: (reason: Error) => void;
+ const promise = new Promise<T>((nextResolve, nextReject) => {
+ resolve = nextResolve;
+ reject = nextReject;
+ });
+ return { promise, reject, resolve };
+};
+
const states: Array<{
apiKey: string | null;
status: string;
@@ -128,6 +140,9 @@ describe("AuthProvider", () => {
googleAuthAvailable: true,
workosAuthAvailable: true,
});
+ apiMock.revokeSession.mockReset();
+ apiMock.revokeSession.mockResolvedValue({ success: true });
+ apiMock.setActiveOrganization.mockReset();
apiMock.createSessionRequestUrl.mockClear();
webBrowserMock.openAuthSessionAsync.mockReset();
});
@@ -251,4 +266,175 @@ describe("AuthProvider", () => {
"cap.mobile.userId",
);
});
+
+ it("does not restore account data when a refresh finishes after sign-out", async () => {
+ secureStoreMock.getItemAsync.mockImplementation((key: string) =>
+ Promise.resolve(key === "cap.mobile.apiKey" ? "key_123" : "user_123"),
+ );
+ const staleBootstrap = createDeferred<{
+ activeOrganizationId: string;
+ user: { email: string; name: string };
+ }>();
+ apiMock.bootstrap.mockResolvedValueOnce({
+ activeOrganizationId: "org_123",
+ user: { email: "[email protected]", name: "Old" },
+ });
+ apiMock.bootstrap.mockReturnValueOnce(staleBootstrap.promise);
+
+ await act(async () => {
+ TestRenderer.create(
+ React.createElement(AuthProvider, null, React.createElement(Probe)),
+ );
+ await flushMicrotasks();
+ });
+ expect(latestAuth?.bootstrap?.user.email).toBe("[email protected]");
+
+ let refreshPromise: Promise<void> | undefined;
+ await act(async () => {
+ refreshPromise = latestAuth?.refresh();
+ await Promise.resolve();
+ });
+ await act(async () => {
+ await latestAuth?.signOut();
+ });
+ expect(latestAuth?.bootstrap).toBeNull();
+
+ await act(async () => {
+ staleBootstrap.resolve({
+ activeOrganizationId: "org_123",
+ user: { email: "[email protected]", name: "Old" },
+ });
+ await refreshPromise;
+ });
+ expect(latestAuth?.status).toBe("signedOut");
+ expect(latestAuth?.bootstrap).toBeNull();
+ });
+
+ it("discards a refresh started while sign-out is revoking the session", async () => {
+ secureStoreMock.getItemAsync.mockImplementation((key: string) =>
+ Promise.resolve(key === "cap.mobile.apiKey" ? "key_123" : "user_123"),
+ );
+ const revocation = createDeferred<{ success: boolean }>();
+ const staleBootstrap = createDeferred<{
+ activeOrganizationId: string;
+ user: { email: string; name: string };
+ }>();
+ apiMock.revokeSession.mockReturnValueOnce(revocation.promise);
+ apiMock.bootstrap.mockResolvedValueOnce({
+ activeOrganizationId: "org_123",
+ user: { email: "[email protected]", name: "Old" },
+ });
+ apiMock.bootstrap.mockReturnValueOnce(staleBootstrap.promise);
+
+ await act(async () => {
+ TestRenderer.create(
+ React.createElement(AuthProvider, null, React.createElement(Probe)),
+ );
+ await flushMicrotasks();
+ });
+ expect(latestAuth?.bootstrap?.user.email).toBe("[email protected]");
+
+ let signOutPromise: Promise<void> | undefined;
+ let refreshPromise: Promise<void> | undefined;
+ await act(async () => {
+ signOutPromise = latestAuth?.signOut();
+ refreshPromise = latestAuth?.refresh();
+ });
+ expect(apiMock.bootstrap).toHaveBeenCalledTimes(2);
+ await act(async () => {
+ revocation.resolve({ success: true });
+ await signOutPromise;
+ });
+ expect(latestAuth?.status).toBe("signedOut");
+
+ await act(async () => {
+ staleBootstrap.resolve({
+ activeOrganizationId: "org_123",
+ user: { email: "[email protected]", name: "Old" },
+ });
+ await refreshPromise;
+ });
+ expect(latestAuth?.bootstrap).toBeNull();
+ });
+
+ it("keeps a new session when an old automatic bootstrap fails", async () => {
+ secureStoreMock.getItemAsync.mockImplementation((key: string) =>
+ Promise.resolve(key === "cap.mobile.apiKey" ? "old_key" : "old_user"),
+ );
+ const oldBootstrap = createDeferred<{
+ activeOrganizationId: string;
+ user: { email: string; name: string };
+ }>();
+ apiMock.bootstrap.mockReturnValueOnce(oldBootstrap.promise);
+ apiMock.bootstrap.mockResolvedValueOnce({
+ activeOrganizationId: "new_org",
+ user: { email: "[email protected]", name: "New" },
+ });
+ webBrowserMock.openAuthSessionAsync.mockResolvedValueOnce({
+ type: "success",
+ url: "cap://auth?api_key=new_key&user_id=new_user",
+ });
+
+ await act(async () => {
+ TestRenderer.create(
+ React.createElement(AuthProvider, null, React.createElement(Probe)),
+ );
+ await flushMicrotasks();
+ });
+ await act(async () => {
+ await latestAuth?.signInWithApple();
+ await flushMicrotasks();
+ });
+ expect(latestAuth?.bootstrap?.user.email).toBe("[email protected]");
+
+ await act(async () => {
+ oldBootstrap.reject(new Error("Old session expired"));
+ await flushMicrotasks();
+ });
+ expect(latestAuth?.status).toBe("signedIn");
+ expect(latestAuth?.apiKey).toBe("new_key");
+ expect(latestAuth?.bootstrap?.user.email).toBe("[email protected]");
+ expect(secureStoreMock.deleteItemAsync).not.toHaveBeenCalled();
+ });
+
+ it("discards an organization switch from an old session", async () => {
+ secureStoreMock.getItemAsync.mockImplementation((key: string) =>
+ Promise.resolve(key === "cap.mobile.apiKey" ? "old_key" : "old_user"),
+ );
+ const oldSwitch = createDeferred<{
+ activeOrganizationId: string;
+ user: { email: string; name: string };
+ }>();
+ apiMock.setActiveOrganization.mockReturnValueOnce(oldSwitch.promise);
+ webBrowserMock.openAuthSessionAsync.mockResolvedValueOnce({
+ type: "success",
+ url: "cap://auth?api_key=new_key&user_id=new_user",
+ });
+
+ await act(async () => {
+ TestRenderer.create(
+ React.createElement(AuthProvider, null, React.createElement(Probe)),
+ );
+ await flushMicrotasks();
+ });
+ let switchPromise: Promise<void> | undefined;
+ await act(async () => {
+ switchPromise = latestAuth?.setActiveOrganization("old_org");
+ await Promise.resolve();
+ });
+ await act(async () => {
+ await latestAuth?.signInWithApple();
+ await flushMicrotasks();
+ });
+ expect(latestAuth?.bootstrap?.user.email).toBe("[email protected]");
+
+ await act(async () => {
+ oldSwitch.resolve({
+ activeOrganizationId: "old_org",
+ user: { email: "[email protected]", name: "Old" },
+ });
+ await switchPromise;
+ });
+ expect(latestAuth?.bootstrap?.user.email).toBe("[email protected]");
+ });
});
diff --git a/apps/mobile/src/auth/SignInPanel.test.tsx b/apps/mobile/src/auth/SignInPanel.test.tsx
index 54ddee082..365cb15f0 100644
--- a/apps/mobile/src/auth/SignInPanel.test.tsx
+++ b/apps/mobile/src/auth/SignInPanel.test.tsx
@@ -934,6 +934,72 @@ describe("SignInPanel", () => {
).toBe(true);
});
+ it("shows a retry-later message when code requests are throttled", async () => {
+ const { MobileApiError } = await import("@/api/mobile");
+ authFns.requestEmailCode.mockRejectedValueOnce(
+ new MobileApiError("Too Many Requests", 429, null),
+ );
+ const renderer = await renderPanel();
+ const [emailInput] = renderer.root.findAllByProps({
+ placeholder: "[email protected]",
+ });
+ const [emailButton] = renderer.root.findAllByProps({
+ accessibilityLabel: "Login with email",
+ });
+ if (!emailInput || !emailButton) {
+ throw new Error("Email sign in controls were not rendered");
+ }
+
+ await act(async () => {
+ emailInput.props.onChangeText("[email protected]");
+ });
+ await act(async () => {
+ await emailButton.props.onPress();
+ });
+
+ expect(getTextNodes(renderer.toJSON())).toContain(
+ "Too many codes requested. Try again in a few minutes.",
+ );
+ });
+
+ it("shows a retry-later message when code verification is throttled", async () => {
+ const { MobileApiError } = await import("@/api/mobile");
+ authFns.verifyEmailCode.mockRejectedValueOnce(
+ new MobileApiError("Too Many Requests", 429, null),
+ );
+ const renderer = await renderPanel();
+ const [emailInput] = renderer.root.findAllByProps({
+ placeholder: "[email protected]",
+ });
+ const [emailButton] = renderer.root.findAllByProps({
+ accessibilityLabel: "Login with email",
+ });
+ if (!emailInput || !emailButton) {
+ throw new Error("Email sign in controls were not rendered");
+ }
+
+ await act(async () => {
+ emailInput.props.onChangeText("[email protected]");
+ });
+ await act(async () => {
+ await emailButton.props.onPress();
+ });
+
+ const [codeInput] = renderer.root.findAllByProps({
+ textContentType: "oneTimeCode",
+ });
+ if (!codeInput) throw new Error("One-time code input was not rendered");
+
+ await act(async () => {
+ codeInput.props.onChangeText("123456");
+ await Promise.resolve();
+ });
+
+ expect(getTextNodes(renderer.toJSON())).toContain(
+ "Too many attempts. Try again in a few minutes.",
+ );
+ });
+
it("switches to a web-like verification code step after requesting email", async () => {
const renderer = await renderPanel();
const [emailInput] = renderer.root.findAllByProps({
diff --git a/apps/mobile/src/auth/SignInPanel.tsx b/apps/mobile/src/auth/SignInPanel.tsx
index 1a57ad267..3ec3beb48 100644
--- a/apps/mobile/src/auth/SignInPanel.tsx
+++ b/apps/mobile/src/auth/SignInPanel.tsx
@@ -33,6 +33,9 @@ const getEmailRequestErrorMessage = (error: unknown) => {
if (error.status === 403) {
return "This email cannot be used to sign in to Cap.";
}
+ if (error.status === 429) {
+ return "Too many codes requested. Try again in a few minutes.";
+ }
}
return error instanceof Error
? error.message
@@ -43,6 +46,9 @@ const getCodeVerificationErrorMessage = (error: unknown) => {
if (error instanceof MobileApiError) {
if (error.status === 400) return "Enter a valid email and 6-digit code.";
if (error.status === 403) return "That code is invalid or expired.";
+ if (error.status === 429) {
+ return "Too many attempts. Try again in a few minutes.";
+ }
}
return error instanceof Error ? error.message : "Unable to verify that code.";
};
diff --git a/apps/mobile/src/screens/dashboard-upload-visibility.test.tsx b/apps/mobile/src/screens/dashboard-upload-visibility.test.tsx
index 2971152e2..0654133cd 100644
--- a/apps/mobile/src/screens/dashboard-upload-visibility.test.tsx
+++ b/apps/mobile/src/screens/dashboard-upload-visibility.test.tsx
@@ -4,12 +4,13 @@ import TestRenderer, {
type ReactTestRenderer,
type ReactTestRendererJSON,
} from "react-test-renderer";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import type { MobileCapSummary } from "@/api/mobile";
import CapsScreen from "../../app/(tabs)";
import UploadScreen from "../../app/(tabs)/upload";
type AuthStub = {
- status: "signedIn";
+ status: "signedIn" | "signedOut";
apiKey: string;
bootstrap: {
activeOrganizationId: string;
@@ -42,7 +43,7 @@ type AuthStub = {
}>;
getCap: (id: string) => Promise<{
cap: {
- upload: null;
+ upload: MobileCapSummary["upload"];
};
}>;
getCapStatuses: (ids: readonly string[]) => Promise<{
@@ -177,9 +178,9 @@ const uploadQueueState = vi.hoisted(
}),
);
-const uploadQueueActionsState = vi.hoisted((): { value: unknown[] } => ({
- value: [],
-}));
+const uploadQueueActionsState = vi.hoisted(
+ (): { value: unknown[]; reduce: boolean } => ({ value: [], reduce: false }),
+);
const recordingUploadState = vi.hoisted(() => ({
libraryRevision: 0,
@@ -592,9 +593,14 @@ vi.mock("@/uploads/uploadQueue", async (importOriginal) => {
...actual,
emptyUploadQueue: uploadQueueState.value,
uploadProgressPercent: (progress: number) => Math.round(progress * 100),
- uploadQueueReducer: (state: { items: unknown[] }, action: unknown) => {
+ uploadQueueReducer: (
+ state: Parameters<typeof actual.uploadQueueReducer>[0],
+ action: Parameters<typeof actual.uploadQueueReducer>[1],
+ ) => {
uploadQueueActionsState.value.push(action);
- return state;
+ return uploadQueueActionsState.reduce
+ ? actual.uploadQueueReducer(state, action)
+ : state;
},
uploadQueueStatusText: (item: {
processingMessage?: string | null;
@@ -1754,6 +1760,221 @@ describe("upload and dashboard visibility", () => {
}
});
+ describe("processing status polling", () => {
+ const processingDetail = {
+ cap: {
+ upload: {
+ phase: "processing" as const,
+ processingError: null,
+ processingMessage: "Converting video",
+ processingProgress: 42,
+ total: 100,
+ uploaded: 100,
+ },
+ },
+ };
+ const cancellations = [
+ "removal",
+ "unmount",
+ "sign-out",
+ "new session",
+ ] as const;
+ let auth: AuthStub;
+ let renderer: ReactTestRenderer | undefined;
+
+ beforeEach(async () => {
+ vi.useFakeTimers();
+ uploadQueueActionsState.reduce = true;
+ auth = createAuth();
+ auth.refresh = vi.fn(() => Promise.resolve());
+ auth.client.getCap = vi.fn(() => Promise.resolve(processingDetail));
+ authState.value = auth;
+ const DocumentPicker = await import("expo-document-picker");
+ vi.mocked(DocumentPicker.getDocumentAsync).mockResolvedValueOnce({
+ assets: [
+ {
+ name: "slow.mp4",
+ uri: "file:///tmp/slow.mp4",
+ size: 100,
+ lastModified: 0,
+ },
+ ],
+ canceled: false,
+ });
+ const { runMobileUpload } = await import("@/uploads/runMobileUpload");
+ vi.mocked(runMobileUpload).mockImplementationOnce(
+ async ({ onCreated }) => {
+ onCreated?.("video_123", "raw.mp4");
+ return { id: "video_123" } as Awaited<
+ ReturnType<typeof runMobileUpload>
+ >;
+ },
+ );
+ });
+
+ afterEach(async () => {
+ await act(async () => renderer?.unmount());
+ renderer = undefined;
+ uploadQueueActionsState.reduce = false;
+ vi.clearAllTimers();
+ vi.useRealTimers();
+ });
+
+ const startImport = async () => {
+ renderer = await renderComponent(React.createElement(UploadScreen));
+ const [browseButton] = renderer.root.findAllByProps({
+ accessibilityLabel: "Browse Files",
+ });
+ if (!browseButton)
+ throw new Error("Browse Files button was not rendered");
+ await act(async () => {
+ void browseButton.props.onPress();
+ });
+ return renderer;
+ };
+
+ const cancel = async (reason: (typeof cancellations)[number]) => {
+ if (!renderer) throw new Error("Upload screen was not rendered");
+ if (reason === "removal") {
+ const { ActionSheetIOS } = await import("react-native");
+ const sheet = vi.mocked(ActionSheetIOS.showActionSheetWithOptions);
+ sheet.mockClear();
+ const [menu] = renderer.root.findAllByProps({
+ accessibilityLabel: "More actions for slow.mp4",
+ });
+ if (!menu) throw new Error("Upload queue menu was not rendered");
+ await act(async () => {
+ menu.props.onPress({ stopPropagation: vi.fn() });
+ });
+ const call = sheet.mock.calls.at(-1);
+ if (!call) throw new Error("Upload queue sheet did not open");
+ await act(async () => {
+ call[1](call[0].options.indexOf("Remove from Queue"));
+ });
+ expect(
+ renderer.root.findAllByProps({
+ accessibilityLabel: "Upload slow.mp4",
+ }),
+ ).toHaveLength(0);
+ return;
+ }
+ if (reason === "unmount") {
+ await act(async () => renderer?.unmount());
+ renderer = undefined;
+ return;
+ }
+ authState.value =
+ reason === "sign-out"
+ ? { ...auth, status: "signedOut" }
+ : { ...createAuth(), apiKey: "new-api-key" };
+ await act(async () =>
+ renderer?.update(React.createElement(UploadScreen)),
+ );
+ };
+
+ it.each(["complete", "error"] as const)(
+ "observes a late %s after the initial four checks",
+ async (phase) => {
+ const startedAt = Date.now();
+ auth.client.getCap = vi.fn(async () => {
+ if (Date.now() - startedAt < 30_000) return processingDetail;
+ return {
+ cap: {
+ upload: {
+ ...processingDetail.cap.upload,
+ phase,
+ processingError: phase === "error" ? "Conversion failed" : null,
+ },
+ },
+ };
+ });
+ const screen = await startImport();
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(17_500);
+ });
+ expect(auth.client.getCap).toHaveBeenCalledTimes(4);
+ expect(getTextNodes(screen.toJSON()).join(" ")).toContain(
+ "Converting video",
+ );
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(16_000);
+ });
+ expect(auth.client.getCap).toHaveBeenCalledTimes(6);
+ expect(getTextNodes(screen.toJSON()).join(" ")).toContain(
+ phase === "complete" ? "Ready to view" : "Conversion failed",
+ );
+ expect(auth.refresh).toHaveBeenCalledTimes(
+ phase === "complete" ? 2 : 1,
+ );
+ if (phase === "error") {
+ expect(
+ screen.root.findAllByProps({
+ accessibilityLabel: "Retry upload slow.mp4",
+ }).length,
+ ).toBeGreaterThan(0);
+ }
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(32_000);
+ });
+ expect(auth.client.getCap).toHaveBeenCalledTimes(6);
+ },
+ );
+
+ it.each(cancellations)(
+ "stops requests after %s while waiting",
+ async (reason) => {
+ await startImport();
+ await cancel(reason);
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(40_000);
+ });
+ expect(auth.client.getCap).not.toHaveBeenCalled();
+ expect(auth.refresh).toHaveBeenCalledTimes(1);
+ },
+ );
+
+ it.each(cancellations)(
+ "ignores an in-flight reply after %s",
+ async (reason) => {
+ const detail =
+ createDeferred<Awaited<ReturnType<AuthStub["client"]["getCap"]>>>();
+ auth.client.getCap = vi.fn(() => detail.promise);
+ await startImport();
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(1500);
+ });
+ expect(auth.client.getCap).toHaveBeenCalledTimes(1);
+ await cancel(reason);
+ await act(async () => {
+ detail.resolve({ cap: { upload: null } });
+ });
+ expect(uploadQueueActionsState.value).not.toContainEqual(
+ expect.objectContaining({ type: "complete" }),
+ );
+ expect(auth.refresh).toHaveBeenCalledTimes(1);
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(40_000);
+ });
+ expect(auth.client.getCap).toHaveBeenCalledTimes(1);
+ },
+ );
+
+ it("does not start an old upload's poll after a session change during refresh", async () => {
+ const refresh = createDeferred<void>();
+ auth.refresh = vi.fn(() => refresh.promise);
+ await startImport();
+ expect(auth.refresh).toHaveBeenCalledTimes(1);
+ await cancel("new session");
+ await act(async () => {
+ refresh.resolve();
+ });
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(40_000);
+ });
+ expect(auth.client.getCap).not.toHaveBeenCalled();
+ });
+ });
+
it("opens the native iOS upload queue sheet", async () => {
uploadQueueState.value.items = [
{
diff --git a/apps/render-farm/src/coordinator.test.ts b/apps/render-farm/src/coordinator.test.ts
index 661af17e1..1a79b5dc2 100644
--- a/apps/render-farm/src/coordinator.test.ts
+++ b/apps/render-farm/src/coordinator.test.ts
@@ -8,6 +8,7 @@ import {
import { readFileSync } from "node:fs";
import { ANNEX_B_PARAMETER_SETS } from "./boxes.test-util";
import type { Job, TaskState } from "./coordinator";
+import type { Engine } from "./engine";
import * as fmp4 from "./fmp4";
import * as hls from "./hls";
import * as mp4 from "./mp4";
@@ -31,6 +32,11 @@ function harness(env: Record<string, string> = {}) {
let copyGate: (source: string) => Promise<void> | undefined = () => undefined;
const completedParts: number[][] = [];
const timers: (() => void)[] = [];
+ const timerDelays: number[] = [];
+ const clearedTimers: unknown[] = [];
+ const engineCalls: ReturnType<typeof Promise.withResolvers<unknown>>[] = [];
+ const engineKills: (NodeJS.Signals | undefined)[] = [];
+ const engineLabels: string[] = [];
const watchdogs: (() => void)[] = [];
let putGate: Promise<void> | undefined;
let headGate: Promise<void> | undefined;
@@ -166,8 +172,38 @@ function harness(env: Record<string, string> = {}) {
},
s3ConfigFromEnv: () => ({}),
mediaS3ConfigFromEnv: () => ({}),
+ ProjectCache: class {
+ constructor(
+ _s3: unknown,
+ readonly root: string,
+ ) {}
+ async materialize() {
+ return { bytes: 0, requests: 0, ms: 0 };
+ }
+ close() {}
+ },
ProbeEngine: class {},
- Engine: class {},
+ Engine: class {
+ alive = true;
+ private pending?: ReturnType<typeof Promise.withResolvers<unknown>>;
+ constructor(
+ _binary: string,
+ _env: Record<string, string>,
+ readonly label: string,
+ ) {
+ engineLabels.push(label);
+ }
+ request() {
+ this.pending = Promise.withResolvers<unknown>();
+ engineCalls.push(this.pending);
+ return this.pending.promise;
+ }
+ kill(signal?: NodeJS.Signals) {
+ engineKills.push(signal);
+ this.alive = false;
+ this.pending?.reject(new Error("audio engine exited"));
+ }
+ },
process: {
env: {
RF_TOKEN: "test",
@@ -185,11 +221,12 @@ function harness(env: Record<string, string> = {}) {
setInterval: (callback: () => void) => {
watchdogs.push(callback);
},
- setTimeout: (callback: () => void) => {
+ setTimeout: (callback: () => void, delay: number) => {
timers.push(callback);
+ timerDelays.push(delay);
return { unref() {} };
},
- clearTimeout: () => {},
+ clearTimeout: (timer: unknown) => clearedTimers.push(timer),
fetch: async (url: string, init: RequestInit) => {
callbacks.push({ url, init });
return new Response("ok");
@@ -202,8 +239,13 @@ function harness(env: Record<string, string> = {}) {
const compiled = new Bun.Transpiler({ loader: "ts" }).transformSync(source);
const coordinator = new Function(
...Object.keys(deps),
- `${compiled}\nreturn {jobs, queue, straggler, dispatchedTask, onVideoDone, onAudioDone, publishPlaylist, journalJob, resumeJobs, newHlsState, finish, requeue, sourceIndex, assemble, stitchAhead, sweepStashes, setPlanner: (fn) => { planJob = fn; }};`,
+ `${compiled}\nreturn {withAudioEngine, pumpLocalAudio, jobs, queue, straggler, dispatchedTask, onVideoDone, onAudioDone, publishPlaylist, journalJob, resumeJobs, newHlsState, finish, requeue, sourceIndex, assemble, stitchAhead, sweepStashes, setPlanner: (fn) => { planJob = fn; }};`,
)(...Object.values(deps)) as {
+ pumpLocalAudio: () => void;
+ withAudioEngine: <T>(
+ fn: (engine: Engine) => Promise<T>,
+ timeoutMs: number,
+ ) => Promise<T>;
setPlanner: (fn: (job: Job) => Promise<void>) => void;
sourceIndex: (prefix: string, sourceRoot?: string) => Promise<unknown>;
assemble: (job: Job) => Promise<void>;
@@ -250,6 +292,11 @@ function harness(env: Record<string, string> = {}) {
},
callbacks,
timers,
+ timerDelays,
+ clearedTimers,
+ engineCalls,
+ engineKills,
+ engineLabels,
watchdogs,
fetch: (request: Request) => fetchHandler(request),
gate: (gate?: Promise<void>) => {
@@ -1368,3 +1415,85 @@ describe("jobs for the product", () => {
expect(summary.progress).toBe(0.75);
});
});
+
+describe("local audio deadlines", () => {
+ test("local sections include preroll in their deadlines and fail after three hung attempts", async () => {
+ const h = harness({ RF_LOCAL_AUDIO_SLOTS: "1" });
+ const j = job();
+ const state: TaskState = {
+ task: {
+ kind: "audio",
+ taskId: "job:a0",
+ jobId: j.id,
+ section: 0,
+ fps: 30,
+ range: [0, 300 * 48_000],
+ preroll: 30 * 48_000,
+ files: [],
+ },
+ state: "queued",
+ attempts: 0,
+ };
+ j.tasks.set(state.task.taskId, state);
+ h.jobs.set(j.id, j);
+ h.queue.push(state);
+ h.pumpLocalAudio();
+ for (let attempt = 1; attempt <= 3; attempt++) {
+ await Bun.sleep(0);
+ expect(h.engineCalls).toHaveLength(attempt);
+ expect(h.timerDelays.at(-1)).toBe(110_000);
+ h.timers.at(-1)?.();
+ await Bun.sleep(0);
+ if (attempt < 3) {
+ expect(j.status).toBe("rendering");
+ expect(h.timerDelays.at(-1)).toBe(1000 * attempt);
+ h.timers.at(-1)?.();
+ }
+ }
+ expect(j.status).toBe("error");
+ expect(j.error).toBe("audio engine exited");
+ expect(state.attempts).toBe(3);
+ expect(h.engineKills).toEqual(["SIGKILL", "SIGKILL", "SIGKILL"]);
+ expect(j.audioSections.size).toBe(0);
+ expect(h.queue).toHaveLength(0);
+ });
+
+ test("a hung engine is killed and replaced before queued work runs", async () => {
+ const h = harness({ RF_LOCAL_AUDIO_SLOTS: "1" });
+ const first = h.withAudioEngine(
+ (engine) => engine.request("audio", {}),
+ 60_000,
+ );
+ first.catch(() => {});
+ const second = h.withAudioEngine(
+ (engine) => engine.request("audio", {}),
+ 90_000,
+ );
+ expect(h.engineCalls).toHaveLength(1);
+ expect(h.timerDelays).toEqual([60_000]);
+ h.timers[0]?.();
+ await expect(first).rejects.toThrow("audio engine exited");
+ expect(h.engineKills).toEqual(["SIGKILL"]);
+ expect(h.engineLabels).toEqual(["audio0", "audio0"]);
+ expect(h.engineCalls).toHaveLength(2);
+ expect(h.timerDelays).toEqual([60_000, 90_000]);
+ h.engineCalls[1]?.resolve("recovered");
+ expect(await second).toBe("recovered");
+ expect(h.clearedTimers).toHaveLength(2);
+ });
+
+ test("successful requests clear their deadlines and keep the warm engine", async () => {
+ const h = harness({ RF_LOCAL_AUDIO_SLOTS: "1" });
+ for (let index = 0; index < 2; index++) {
+ const pending = h.withAudioEngine(
+ (engine) => engine.request("audio", {}),
+ 60_000,
+ );
+ h.engineCalls[index]?.resolve(index);
+ expect(await pending).toBe(index);
+ }
+ expect(h.engineLabels).toEqual(["audio0"]);
+ expect(h.engineKills).toEqual([]);
+ expect(h.clearedTimers).toHaveLength(2);
+ });
+});
diff --git a/apps/render-farm/src/coordinator.ts b/apps/render-farm/src/coordinator.ts
index 2a0b544f7..2d717bd54 100644
--- a/apps/render-farm/src/coordinator.ts
+++ b/apps/render-farm/src/coordinator.ts
@@ -119,15 +119,23 @@ const audioEngines = Array.from(
);
const idleAudioEngines = [...audioEngines];
const localAudioQueue: (() => void)[] = [];
-async function withAudioEngine<T>(fn: (engine: Engine) => Promise<T>) {
+async function withAudioEngine<T>(
+ fn: (engine: Engine) => Promise<T>,
+ timeoutMs: number,
+) {
while (idleAudioEngines.length === 0) {
await new Promise<void>((resolve) => localAudioQueue.push(resolve));
}
let engine = idleAudioEngines.pop() as Engine;
if (!engine.alive) engine = new Engine(ENGINE_BIN, {}, engine.label);
+ const watchdog = setTimeout(() => {
+ console.error(`${engine.label}: audio section timed out, killing engine`);
+ engine.kill("SIGKILL");
+ }, timeoutMs);
try {
return await fn(engine);
} finally {
+ clearTimeout(watchdog);
idleAudioEngines.push(engine);
localAudioQueue.shift()?.();
}
@@ -171,19 +179,23 @@ async function runAudioLocally(job: Job, state: TaskState) {
const fetch = await cache.materialize(task.files);
const out = join(cache.root, `a${task.section}.aac`);
const engineStarted = performance.now();
- const result = await withAudioEngine((engine) =>
- engine.request<{
- first_packet: number;
- sizes: number[];
- extradata: string;
- timings: Record<string, number>;
- }>("audio", {
- project: cache.root,
- fps: task.fps,
- section: task.range,
- preroll: task.preroll,
- out,
- }),
+ const seconds =
+ (task.range[1] - task.range[0] + task.preroll) / SAMPLE_RATE;
+ const result = await withAudioEngine(
+ (engine) =>
+ engine.request<{
+ first_packet: number;
+ sizes: number[];
+ extradata: string;
+ timings: Record<string, number>;
+ }>("audio", {
+ project: cache.root,
+ fps: task.fps,
+ section: task.range,
+ preroll: task.preroll,
+ out,
+ }),
+ Math.max(60_000, (seconds * 1000) / 3),
);
const data = new Uint8Array(await Bun.file(out).arrayBuffer());
rmSync(out, { force: true });
diff --git a/apps/render-farm/src/mp4.test.ts b/apps/render-farm/src/mp4.test.ts
index 7bcc26915..0a9fbe500 100644
--- a/apps/render-farm/src/mp4.test.ts
+++ b/apps/render-farm/src/mp4.test.ts
@@ -98,24 +98,28 @@ describe("byteRangeFor", () => {
describe("buildHeader", () => {
test("lays out ftyp, moov, padding and a 64-bit mdat header", () => {
+ const videoSizes = Uint32Array.of(0x01020304, 0x80000000, 300);
+ const audioSizes = Uint32Array.of(10, 20);
+ const videoBytes = videoSizes.reduce((sum, size) => sum + size, 0);
+ const payloadSize = videoBytes + 30;
const header = buildHeader({
width: 1920,
height: 1080,
fps: 30,
video: {
- sizes: Uint32Array.from([100, 200, 300]),
+ sizes: videoSizes,
runs: [{ first: 0, count: 3, offset: 0 }],
keyframes: Uint32Array.from([0]),
avcC: avcC(ANNEX_B_PARAMETER_SETS),
},
audio: {
- sizes: Uint32Array.from([10, 10]),
- runs: [{ first: 0, count: 2, offset: 600 }],
+ sizes: audioSizes,
+ runs: [{ first: 0, count: 2, offset: videoBytes }],
asc: Uint8Array.of(0x11, 0x90),
totalSamples: 1024,
priming: 1024,
},
- payloadSize: 620,
+ payloadSize,
minimumSize: 64 * 1024,
});
expect(header.byteLength).toBe(64 * 1024);
@@ -128,17 +132,29 @@ describe("buildHeader", () => {
]);
const mdat = boxes[3];
expect(u32(header, mdat?.start ?? 0)).toBe(1);
- expect(u64(header, (mdat?.start ?? 0) + 8)).toBe(16 + 620);
+ expect(u64(header, (mdat?.start ?? 0) + 8)).toBe(16 + payloadSize);
const moov = boxes[1];
if (!moov) throw new Error("no moov");
const traks = parseBoxes(moov.body).filter((box) => box.type === "trak");
expect(traks).toHaveLength(2);
- const stbl = child(
- child(child(traks[0] ?? moov, "mdia") ?? moov, "minf") ?? moov,
- "stbl",
- );
- const co64 = stbl && child(stbl, "co64");
- expect(co64 && u64(co64.body, 8)).toBe(header.byteLength);
+ for (const [index, sizes] of [videoSizes, audioSizes].entries()) {
+ const stbl = child(
+ child(child(traks[index] ?? moov, "mdia") ?? moov, "minf") ?? moov,
+ "stbl",
+ );
+ const stsz = stbl && child(stbl, "stsz");
+ if (!stsz) throw new Error("no sample sizes");
+ expect(stsz.body.byteLength).toBe(12 + sizes.length * 4);
+ expect(u32(stsz.body, 4)).toBe(0);
+ expect(u32(stsz.body, 8)).toBe(sizes.length);
+ expect(sizes.map((_, sample) => u32(stsz.body, 12 + sample * 4))).toEqual(
+ sizes,
+ );
+ const co64 = stbl && child(stbl, "co64");
+ expect(co64 && u64(co64.body, 8)).toBe(
+ header.byteLength + (index === 0 ? 0 : videoBytes),
+ );
+ }
});
});
diff --git a/apps/render-farm/src/mp4.ts b/apps/render-farm/src/mp4.ts
index 8c4ba67d8..10b92b398 100644
--- a/apps/render-farm/src/mp4.ts
+++ b/apps/render-farm/src/mp4.ts
@@ -485,18 +485,13 @@ function sampleTable(
}),
),
);
- parts.push(
- fullBox(
- "stsz",
- 0,
- 0,
- build((writer) => {
- writer.u32(0);
- writer.u32(table.sizes.length);
- for (const size of table.sizes) writer.u32(size);
- }),
- ),
- );
+ const sampleSizes = new Uint8Array(8 + table.sizes.length * 4);
+ const sampleSizesView = new DataView(sampleSizes.buffer);
+ sampleSizesView.setUint32(4, table.sizes.length);
+ table.sizes.forEach((size, index) => {
+ sampleSizesView.setUint32(8 + index * 4, size);
+ });
+ parts.push(fullBox("stsz", 0, 0, sampleSizes));
parts.push(
fullBox(
"co64",
diff --git a/apps/render-farm/src/transcode.integration.test.ts b/apps/render-farm/src/transcode.integration.test.ts
new file mode 100644
index 000000000..4fb8da23c
--- /dev/null
+++ b/apps/render-farm/src/transcode.integration.test.ts
@@ -0,0 +1,66 @@
+import { expect, test } from "bun:test";
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { transcodeArgs } from "./transcode";
+
+test.skipIf(!Bun.which("ffmpeg") || !Bun.which("ffprobe")).each([
+ [641, 360],
+ [640, 361],
+ [641, 361],
+])("CPU transcodes accept %i × %i recordings", (width, height) => {
+ const directory = mkdtempSync(join(tmpdir(), "cap-transcode-"));
+ const input = join(directory, "input.webm");
+ const output = join(directory, "output.mp4");
+ try {
+ const generated = Bun.spawnSync([
+ "ffmpeg",
+ "-v",
+ "error",
+ "-f",
+ "lavfi",
+ "-i",
+ `testsrc=size=${width}x${height}:rate=10`,
+ "-t",
+ "1",
+ "-pix_fmt",
+ "yuv420p",
+ "-c:v",
+ "libvpx",
+ "-deadline",
+ "realtime",
+ input,
+ ]);
+ expect(generated.exitCode, generated.stderr.toString()).toBe(0);
+ const encoded = Bun.spawnSync([
+ "ffmpeg",
+ ...transcodeArgs(input, output, 1, "libx264"),
+ ]);
+ expect(encoded.exitCode, encoded.stderr.toString()).toBe(0);
+ const decoded = Bun.spawnSync([
+ "ffprobe",
+ "-v",
+ "error",
+ "-count_frames",
+ "-show_entries",
+ "stream=width,height,nb_read_frames,duration",
+ "-of",
+ "json",
+ output,
+ ]);
+ expect(decoded.exitCode, decoded.stderr.toString()).toBe(0);
+ expect(decoded.stderr.toString()).toBe("");
+ expect(JSON.parse(decoded.stdout.toString())).toMatchObject({
+ streams: [
+ {
+ width: Math.ceil(width / 2) * 2,
+ height: Math.ceil(height / 2) * 2,
+ nb_read_frames: "10",
+ duration: "1.000000",
+ },
+ ],
+ });
+ } finally {
+ rmSync(directory, { recursive: true, force: true });
+ }
+});
diff --git a/apps/render-farm/src/transcode.ts b/apps/render-farm/src/transcode.ts
index 0e7571ab2..5c93a3524 100644
--- a/apps/render-farm/src/transcode.ts
+++ b/apps/render-farm/src/transcode.ts
@@ -28,6 +28,7 @@ export function transcodeArgs(
"-an",
"-fps_mode",
"passthrough",
+ ...(nvenc ? [] : ["-vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2"]),
"-pix_fmt",
"yuv420p",
"-c:v",
diff --git a/apps/render-farm/src/worker.test.ts b/apps/render-farm/src/worker.test.ts
index a9e9e99c0..39d144ece 100644
--- a/apps/render-farm/src/worker.test.ts
+++ b/apps/render-farm/src/worker.test.ts
@@ -2,7 +2,12 @@ import { describe, expect, test } from "bun:test";
import { randomUUID } from "node:crypto";
import { readFileSync } from "node:fs";
import { join } from "node:path";
-import { MIN_PART, type TranscodeTask, type WorkItem } from "./protocol";
+import {
+ MIN_PART,
+ type TranscodeTask,
+ type VideoTask,
+ type WorkItem,
+} from "./protocol";
import * as stitch from "./stitch";
import * as transcode from "./transcode";
@@ -26,6 +31,8 @@ function harness(
randomUUID,
join,
...transcode,
+ processCpuSeconds: async () => 0,
+ sampleThreads: async () => ({}),
availableParallelism: () => 1,
hostname: () => "worker-test",
mkdirSync: () => {},
@@ -34,6 +41,13 @@ function harness(
s3ConfigFromEnv: () => ({}),
...stitch,
MIN_PART,
+ ProjectCache: class {
+ root = "/work/job";
+ async materialize() {
+ return { bytes: 0, ms: 0, requests: 0 };
+ }
+ close() {}
+ },
S3: class {
async presignFresh() {
await options.presignGate;
@@ -97,8 +111,18 @@ function harness(
const compiled = new Bun.Transpiler({ loader: "ts" }).transformSync(source);
const worker = new Function(
...Object.keys(deps),
- `${compiled}\nreturn {runTranscode, cancel, busy, progress, transcoders, engineEnv, uploadLayout};`,
+ `${compiled}\nreturn {runVideo, runTranscode, cancel, busy, progress, transcoders, engineEnv, uploadLayout};`,
)(...Object.values(deps)) as {
+ runVideo: (
+ task: VideoTask,
+ engine: {
+ pid: number;
+ onEvent: ((event: Record<string, unknown>) => void) | null;
+ onProgress: ((frames: number) => void) | null;
+ request: () => Promise<never>;
+ },
+ queuedMs: number,
+ ) => Promise<unknown>;
runTranscode: (task: TranscodeTask, slot: number) => Promise<number>;
cancel: (taskIds: string[]) => void;
busy: Map<number, WorkItem>;
@@ -227,3 +251,41 @@ describe("chunk upload", () => {
});
});
});
+
+test("a failed video render removes its temporary output", async () => {
+ const worker = harness();
+ const task: VideoTask = {
+ kind: "video",
+ taskId: "job:v0",
+ jobId: "job",
+ chunk: 0,
+ fps: 30,
+ resolution: [1920, 1080],
+ bpp: 0.1,
+ frames: [0, 30],
+ threads: 1,
+ files: [],
+ upload: {
+ key: "out/job.mp4",
+ uploadId: "upload",
+ firstPart: 2,
+ partLimit: 3,
+ partTarget: MIN_PART,
+ stashKey: "stash/job/0",
+ },
+ audio: null,
+ hls: null,
+ };
+ const engine = {
+ pid: 1,
+ onEvent: null,
+ onProgress: null,
+ request: () => Promise.reject(new Error("render failed")),
+ };
+ await expect(worker.runVideo(task, engine, 0)).rejects.toThrow(
+ "render failed",
+ );
+ expect(worker.removed).toHaveLength(1);
+ expect(worker.removed[0]).toStartWith("/work/job/v0-");
+ expect(worker.removed[0]).toEndWith(".h264");
+});
diff --git a/apps/render-farm/src/worker.ts b/apps/render-farm/src/worker.ts
index 4de9dfdbf..e6e129f21 100644
--- a/apps/render-farm/src/worker.ts
+++ b/apps/render-farm/src/worker.ts
@@ -505,110 +505,116 @@ async function runVideo(
cache.root,
`v${task.chunk}-${randomUUID().slice(0, 6)}.h264`,
);
- const engineStarted = performance.now();
- const slot = engines.indexOf(engine);
- const entry = {
- taskId: task.taskId,
- kind: "video" as const,
- frames: 0,
- total: task.frames[1] - task.frames[0],
- startedAt: Date.now(),
- lastProgressAt: Date.now(),
- };
- progress.set(slot, entry);
- const stream = task.hls
- ? new SegmentStream({ ...task, hls: task.hls }, out, engineStarted)
- : null;
- engine.onEvent = stream ? (event) => stream.onEvent(event) : null;
- // Ask for the next task once this one is ~PREFETCH_LEAD_MS from done, so
- // its sources download while this one finishes instead of after.
- engine.onProgress = (frames) => {
- entry.frames = frames;
- entry.lastProgressAt = Date.now();
- const elapsed = performance.now() - engineStarted;
- const remainingMs =
- ((entry.total - frames) * elapsed) / Math.max(1, frames);
- if (frames > 0 && remainingMs <= PREFETCH_LEAD_MS) nearEnd();
- };
- const request = engine.request<{
- width: number;
- height: number;
- sizes: number[];
- keyframes: number[];
- extradata: string;
- bytes: number;
- timings: Record<string, number>;
- }>("video", {
- project: cache.root,
- fps: task.fps,
- resolution: task.resolution,
- bpp: task.bpp,
- frames: task.frames,
- threads: task.threads,
- out,
- });
- const threadsPromise: Promise<Record<string, number>> =
- process.env.RF_PROFILE_THREADS === "1"
- ? sampleThreads(engine.pid, request)
- : Promise.resolve({});
- const result = await request.finally(() => {
- engine.onProgress = null;
- engine.onEvent = null;
- progress.delete(slot);
- });
- nearEnd();
- const segmentsDone = stream?.finish(result.sizes, result.extradata);
- segmentsDone?.catch(() => {});
- const engineMs = performance.now() - engineStarted;
- const threads = await threadsPromise;
- const waitStarted = performance.now();
- const audio = await audioPromise;
- const audioWaitMs = performance.now() - waitStarted;
- const uploadStarted = performance.now();
- const plan = layout(task, out, result.sizes, audio);
- const [{ parts, stash }, segments] = await Promise.all([
- uploadLayout(task, plan.segments, plan.bytes),
- segmentsDone ?? Promise.resolve(0),
- ]);
- const uploadMs = performance.now() - uploadStarted;
- rmSync(out, { force: true });
- const timings: TaskTimings = {
- queuedMs,
- fetch: fetchStats,
- engine: {
- ...result.timings,
- ...Object.fromEntries(
- Object.entries(threads).map(([name, value]) => [
- `thread:${name}`,
- value,
- ]),
- ),
- ...(stream
- ? { segments, firstSegmentMs: Math.round(stream.firstUploadedMs ?? -1) }
- : {}),
- },
- engineMs,
- audioWaitMs,
- uploadMs,
- totalMs: performance.now() - started,
- cpuSeconds: (await processCpuSeconds(engine.pid)) - cpuBefore,
- };
- return {
- taskId: task.taskId,
- worker: WORKER_ID,
- sizes: result.sizes,
- keyframes: result.keyframes,
- extradata: result.extradata,
- width: result.width,
- height: result.height,
- videoRuns: plan.videoRuns,
- audioRuns: plan.audioRuns,
- firstPart: task.upload.firstPart,
- stash,
- parts,
- bytes: plan.bytes,
- timings,
- };
+ try {
+ const engineStarted = performance.now();
+ const slot = engines.indexOf(engine);
+ const entry = {
+ taskId: task.taskId,
+ kind: "video" as const,
+ frames: 0,
+ total: task.frames[1] - task.frames[0],
+ startedAt: Date.now(),
+ lastProgressAt: Date.now(),
+ };
+ progress.set(slot, entry);
+ const stream = task.hls
+ ? new SegmentStream({ ...task, hls: task.hls }, out, engineStarted)
+ : null;
+ engine.onEvent = stream ? (event) => stream.onEvent(event) : null;
+ // Ask for the next task once this one is ~PREFETCH_LEAD_MS from done, so
+ // its sources download while this one finishes instead of after.
+ engine.onProgress = (frames) => {
+ entry.frames = frames;
+ entry.lastProgressAt = Date.now();
+ const elapsed = performance.now() - engineStarted;
+ const remainingMs =
+ ((entry.total - frames) * elapsed) / Math.max(1, frames);
+ if (frames > 0 && remainingMs <= PREFETCH_LEAD_MS) nearEnd();
+ };
+ const request = engine.request<{
+ width: number;
+ height: number;
+ sizes: number[];
+ keyframes: number[];
+ extradata: string;
+ bytes: number;
+ timings: Record<string, number>;
+ }>("video", {
+ project: cache.root,
+ fps: task.fps,
+ resolution: task.resolution,
+ bpp: task.bpp,
+ frames: task.frames,
+ threads: task.threads,
+ out,
+ });
+ const threadsPromise: Promise<Record<string, number>> =
+ process.env.RF_PROFILE_THREADS === "1"
+ ? sampleThreads(engine.pid, request)
+ : Promise.resolve({});
+ const result = await request.finally(() => {
+ engine.onProgress = null;
+ engine.onEvent = null;
+ progress.delete(slot);
+ });
+ nearEnd();
+ const segmentsDone = stream?.finish(result.sizes, result.extradata);
+ segmentsDone?.catch(() => {});
+ const engineMs = performance.now() - engineStarted;
+ const threads = await threadsPromise;
+ const waitStarted = performance.now();
+ const audio = await audioPromise;
+ const audioWaitMs = performance.now() - waitStarted;
+ const uploadStarted = performance.now();
+ const plan = layout(task, out, result.sizes, audio);
+ const [{ parts, stash }, segments] = await Promise.all([
+ uploadLayout(task, plan.segments, plan.bytes),
+ segmentsDone ?? Promise.resolve(0),
+ ]);
+ const uploadMs = performance.now() - uploadStarted;
+ const timings: TaskTimings = {
+ queuedMs,
+ fetch: fetchStats,
+ engine: {
+ ...result.timings,
+ ...Object.fromEntries(
+ Object.entries(threads).map(([name, value]) => [
+ `thread:${name}`,
+ value,
+ ]),
+ ),
+ ...(stream
+ ? {
+ segments,
+ firstSegmentMs: Math.round(stream.firstUploadedMs ?? -1),
+ }
+ : {}),
+ },
+ engineMs,
+ audioWaitMs,
+ uploadMs,
+ totalMs: performance.now() - started,
+ cpuSeconds: (await processCpuSeconds(engine.pid)) - cpuBefore,
+ };
+ return {
+ taskId: task.taskId,
+ worker: WORKER_ID,
+ sizes: result.sizes,
+ keyframes: result.keyframes,
+ extradata: result.extradata,
+ width: result.width,
+ height: result.height,
+ videoRuns: plan.videoRuns,
+ audioRuns: plan.audioRuns,
+ firstPart: task.upload.firstPart,
+ stash,
+ parts,
+ bytes: plan.bytes,
+ timings,
+ };
+ } finally {
+ rmSync(out, { force: true });
+ }
}
async function runAudio(task: AudioTask, engine: Engine, queuedMs: number) {
diff --git a/apps/web/Dockerfile b/apps/web/Dockerfile
index 5592afb83..62a0507cb 100644
--- a/apps/web/Dockerfile
+++ b/apps/web/Dockerfile
@@ -2,7 +2,7 @@
FROM oven/bun:1.4.0-alpine AS bun
-FROM node:24-alpine AS base
+FROM node:24.22.1-alpine AS base
FROM base AS builder
WORKDIR /app
diff --git a/apps/web/__tests__/integration/desktop-recording-production-replay.test.ts b/apps/web/__tests__/integration/desktop-recording-production-replay.test.ts
index 4f810487c..0e223ed7c 100644
--- a/apps/web/__tests__/integration/desktop-recording-production-replay.test.ts
+++ b/apps/web/__tests__/integration/desktop-recording-production-replay.test.ts
@@ -165,7 +165,7 @@ function replay(sample: Sample) {
getInternalSignedObjectUrl: (name: string) =>
checked(() => `https://replay.invalid/${name}`),
};
- mocks.storage.mockReturnValue(Effect.succeed([bucket]));
+ mocks.storage.mockReturnValue(Effect.succeed([bucket, Option.none()]));
const manifest = (complete: boolean) => {
const parsed = readCompletedRecordingManifest(
JSON.stringify(sample.manifest),
diff --git a/apps/web/__tests__/integration/loom-concierge-database.test.ts b/apps/web/__tests__/integration/loom-concierge-database.test.ts
index f296f046c..2604688e7 100644
--- a/apps/web/__tests__/integration/loom-concierge-database.test.ts
+++ b/apps/web/__tests__/integration/loom-concierge-database.test.ts
@@ -256,6 +256,11 @@ describe.skipIf(!databaseUrl)("Loom concierge migration database", () => {
(...args: Parameters<typeof rawActions.updateLoomMigrationStatus>) =>
rawActions.updateLoomMigrationStatus(...args),
),
+ reconcileLoomMigrationInventory: unwrapped(
+ (
+ ...args: Parameters<typeof rawActions.reconcileLoomMigrationInventory>
+ ) => rawActions.reconcileLoomMigrationInventory(...args),
+ ),
};
const loom = {
importFromLoomCsvForConcierge: unwrapped(
@@ -729,6 +734,120 @@ describe.skipIf(!databaseUrl)("Loom concierge migration database", () => {
expect(reopened.id).not.toBe(requestId);
});
+ it.each(["recording", "source mapping"])(
+ "does not complete or notify when a migrated %s disappears",
+ async (missing) => {
+ const { ownerId, organizationId } = await makeOrganization();
+ const requestId = await requestMigration(organizationId, ownerId);
+ const videoId = Video.VideoId.make(id());
+ await db().insert(videos).values({
+ id: videoId,
+ name: "Finished import",
+ ownerId,
+ orgId: organizationId,
+ });
+ await db().insert(importedVideos).values({
+ id: videoId,
+ orgId: organizationId,
+ source: "loom",
+ sourceId: loomId(),
+ });
+ await db().insert(loomMigrationImports).values({ videoId, requestId });
+ if (missing === "recording") {
+ await db().delete(videos).where(eq(videos.id, videoId));
+ } else {
+ await db().delete(importedVideos).where(eq(importedVideos.id, videoId));
+ }
+ signInAs(operatorId);
+ fixture.sendEmail.mockClear();
+ await expect(
+ actions.updateLoomMigrationStatus({
+ requestId,
+ nextStatus: "completed",
+ message: "All done",
+ verified: true,
+ expectedVideoCount: 1,
+ importedVideoCount: 1,
+ }),
+ ).rejects.toThrow("missing or changed");
+ const request = await getRequest(requestId);
+ expect(request.status).not.toBe("completed");
+ expect(request.activeOrganizationId).toBe(organizationId);
+ expect(request.completedAt).toBeNull();
+ expect(fixture.sendEmail).not.toHaveBeenCalled();
+ },
+ );
+
+ it("reconciles only deleted inventory entries in the selected request", async () => {
+ const { ownerId, organizationId } = await makeOrganization();
+ const requestId = await requestMigration(organizationId, ownerId);
+ const other = await makeOrganization();
+ const otherRequestId = await requestMigration(
+ other.organizationId,
+ other.ownerId,
+ );
+ const retainedVideoId = Video.VideoId.make(id());
+ const deletedVideoId = Video.VideoId.make(id());
+ const otherDeletedVideoId = Video.VideoId.make(id());
+ await db().insert(videos).values({
+ id: retainedVideoId,
+ name: "Retained",
+ ownerId,
+ orgId: organizationId,
+ });
+ await db().insert(importedVideos).values({
+ id: retainedVideoId,
+ orgId: organizationId,
+ source: "loom",
+ sourceId: loomId(),
+ });
+ await db()
+ .insert(loomMigrationImports)
+ .values([
+ { videoId: retainedVideoId, requestId },
+ { videoId: deletedVideoId, requestId },
+ { videoId: otherDeletedVideoId, requestId: otherRequestId },
+ ]);
+ signInAs(ownerId);
+ await expect(
+ actions.reconcileLoomMigrationInventory({ requestId, verified: true }),
+ ).rejects.toThrow();
+ signInAs(operatorId);
+ await expect(
+ actions.reconcileLoomMigrationInventory({ requestId, verified: false }),
+ ).rejects.toThrow("source list first");
+ fixture.sendEmail.mockClear();
+ await expect(
+ actions.reconcileLoomMigrationInventory({ requestId, verified: true }),
+ ).resolves.toEqual({ removedCount: 1, hasMore: false });
+ expect(
+ await db()
+ .select({ videoId: loomMigrationImports.videoId })
+ .from(loomMigrationImports)
+ .where(eq(loomMigrationImports.requestId, requestId)),
+ ).toEqual([{ videoId: retainedVideoId }]);
+ expect(
+ await db()
+ .select({ videoId: loomMigrationImports.videoId })
+ .from(loomMigrationImports)
+ .where(eq(loomMigrationImports.requestId, otherRequestId)),
+ ).toEqual([{ videoId: otherDeletedVideoId }]);
+ expect(
+ await db()
+ .select({ id: videos.id })
+ .from(videos)
+ .where(eq(videos.id, retainedVideoId)),
+ ).toHaveLength(1);
+ expect(
+ await db()
+ .select({ id: importedVideos.id })
+ .from(importedVideos)
+ .where(eq(importedVideos.id, retainedVideoId)),
+ ).toHaveLength(1);
+ expect((await getRequest(requestId)).status).not.toBe("completed");
+ expect(fixture.sendEmail).not.toHaveBeenCalled();
+ });
+
it("uploads restricted MP4 files to storage and starts processing", async () => {
const { ownerId, organizationId } = await makeOrganization({
privateByDefault: true,
diff --git a/apps/web/__tests__/integration/sso-database.test.ts b/apps/web/__tests__/integration/sso-database.test.ts
index 5d4c6ce48..8492cadf6 100644
--- a/apps/web/__tests__/integration/sso-database.test.ts
+++ b/apps/web/__tests__/integration/sso-database.test.ts
@@ -6,6 +6,7 @@ import {
organizationSso,
organizations,
users,
+ verificationTokens,
} from "@cap/database/schema";
import { Organisation, User } from "@cap/web-domain";
import { and, eq } from "drizzle-orm";
@@ -344,5 +345,62 @@ describe.runIf(Boolean(databaseUrl))(
.where(eq(organizationMembers.userId, userId)),
).toHaveLength(0);
});
+
+ it("burns an email OTP exactly once under concurrent guesses", async () => {
+ const identifier = `${id()}@example.com`;
+ const token = "482913";
+ const { useVerificationToken } = DrizzleAdapter(database());
+ if (!useVerificationToken) {
+ throw new Error("Missing useVerificationToken adapter.");
+ }
+ await database()
+ .insert(verificationTokens)
+ .values({ identifier, token, expires: new Date(Date.now() + 600_000) });
+
+ const attempts = await Promise.all(
+ Array.from({ length: 8 }, () =>
+ useVerificationToken({ identifier, token }),
+ ),
+ );
+
+ expect(attempts.filter((result) => result !== null)).toHaveLength(1);
+ expect(
+ await database()
+ .select()
+ .from(verificationTokens)
+ .where(eq(verificationTokens.identifier, identifier)),
+ ).toHaveLength(0);
+ });
+
+ it("never authenticates a wrong guess when it races the correct one", async () => {
+ const identifier = `${id()}@example.com`;
+ const token = "531942";
+ const { useVerificationToken } = DrizzleAdapter(database());
+ if (!useVerificationToken) {
+ throw new Error("Missing useVerificationToken adapter.");
+ }
+ await database()
+ .insert(verificationTokens)
+ .values({ identifier, token, expires: new Date(Date.now() + 600_000) });
+
+ const guesses = ["000000", "111111", token, "222222", "333333"];
+ const attempts = await Promise.all(
+ guesses.map((guess) =>
+ useVerificationToken({ identifier, token: guess }),
+ ),
+ );
+
+ const successes = attempts.filter((result) => result !== null);
+ expect(successes.length).toBeLessThanOrEqual(1);
+ for (const success of successes) {
+ expect(success).toMatchObject({ identifier, token });
+ }
+ expect(
+ await database()
+ .select()
+ .from(verificationTokens)
+ .where(eq(verificationTokens.identifier, identifier)),
+ ).toHaveLength(0);
+ });
},
);
diff --git a/apps/web/__tests__/unit/add-folder-videos.test.ts b/apps/web/__tests__/unit/add-folder-videos.test.ts
new file mode 100644
index 000000000..04cd3a0fe
--- /dev/null
+++ b/apps/web/__tests__/unit/add-folder-videos.test.ts
@@ -0,0 +1,139 @@
+import { Folder, Organisation, Space, Video } from "@cap/web-domain";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { addVideosToFolder } from "@/actions/folders/add-videos";
+
+const mocks = vi.hoisted(() => ({ query: vi.fn(), user: vi.fn() }));
+
+vi.mock("@cap/database", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ return { db: () => drizzle(mocks.query) };
+});
+vi.mock("@cap/database/auth/session", () => ({ getCurrentUser: mocks.user }));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+
+const folderId = Folder.FolderId.make("folder-1");
+const orgId = Organisation.OrganisationId.make("org-1");
+const existingId = Video.VideoId.make("existing-video");
+const newId = Video.VideoId.make("new-video");
+
+beforeEach(() => {
+ mocks.user.mockResolvedValue({ id: "owner", activeOrganizationId: orgId });
+});
+
+describe.each([
+ {
+ name: "space",
+ spaceId: Space.SpaceId.make("space-1"),
+ table: "space_videos",
+ },
+ { name: "organization", spaceId: orgId, table: "shared_videos" },
+])("adding videos to a $name folder", ({ spaceId, table }) => {
+ function setup(ownedIds: Video.VideoId[], sharedIds: Video.VideoId[]) {
+ mocks.query.mockImplementation(async (query: string) => {
+ if (query.startsWith("select")) {
+ if (query.includes("from `folders`")) {
+ return { rows: [[folderId, spaceId]] };
+ }
+ if (query.includes("from `videos`")) {
+ return { rows: ownedIds.map((id) => [id]) };
+ }
+ return { rows: sharedIds.map((id) => [id]) };
+ }
+ return { rows: [{ insertId: 1, affectedRows: 1 }] };
+ });
+ }
+
+ it("moves an existing share without inserting another sharing row", async () => {
+ setup([existingId], [existingId]);
+
+ await expect(
+ addVideosToFolder(folderId, [existingId], spaceId),
+ ).resolves.toMatchObject({ success: true, addedCount: 1 });
+
+ const writes = mocks.query.mock.calls.filter(([query]) =>
+ String(query).startsWith("insert"),
+ );
+ expect(writes).toHaveLength(0);
+ const update = mocks.query.mock.calls.find(([query]) =>
+ String(query).startsWith(`update \`${table}\``),
+ );
+ expect(update?.[1]).toEqual([folderId, spaceId, existingId]);
+ });
+
+ it("inserts only missing shares when moving a mixed selection", async () => {
+ setup([existingId, newId], [existingId]);
+
+ await expect(
+ addVideosToFolder(folderId, [existingId, newId], spaceId),
+ ).resolves.toMatchObject({ success: true, addedCount: 2 });
+
+ const insert = mocks.query.mock.calls.find(([query]) =>
+ String(query).startsWith(`insert into \`${table}\``),
+ );
+ expect(insert?.[1]).toContain(newId);
+ expect(insert?.[1]).not.toContain(existingId);
+ const existingQuery = mocks.query.mock.calls.find(([query]) =>
+ String(query).includes(`from \`${table}\``),
+ );
+ expect(existingQuery?.[1]).toEqual([spaceId, existingId, newId]);
+ expect(existingQuery?.[0]).not.toContain("folderId");
+ const update = mocks.query.mock.calls.find(([query]) =>
+ String(query).startsWith(`update \`${table}\``),
+ );
+ expect(update?.[1]).toEqual([folderId, spaceId, existingId, newId]);
+ });
+
+ it("creates the share when the video is not in this collection", async () => {
+ setup([newId], []);
+
+ await expect(
+ addVideosToFolder(folderId, [newId], spaceId),
+ ).resolves.toMatchObject({ success: true, addedCount: 1 });
+
+ const insert = mocks.query.mock.calls.find(([query]) =>
+ String(query).startsWith(`insert into \`${table}\``),
+ );
+ expect(insert?.[1]).toEqual([
+ expect.any(String),
+ ...(table === "shared_videos"
+ ? [newId, folderId, spaceId, "owner"]
+ : [spaceId, folderId, newId, "owner"]),
+ ]);
+ });
+
+ it("does not share requested videos the caller does not own", async () => {
+ setup([newId], []);
+
+ await expect(
+ addVideosToFolder(folderId, [existingId, newId], spaceId),
+ ).resolves.toMatchObject({ success: true, addedCount: 1 });
+
+ const writes = mocks.query.mock.calls.filter(
+ ([query]) => !String(query).startsWith("select"),
+ );
+ expect(writes).toHaveLength(2);
+ for (const [, params] of writes) {
+ expect(params).toContain(newId);
+ expect(params).not.toContain(existingId);
+ }
+ });
+
+ it("does not write when the sharing lookup fails", async () => {
+ setup([existingId], [existingId]);
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ mocks.query
+ .mockResolvedValueOnce({ rows: [[folderId, spaceId]] })
+ .mockResolvedValueOnce({ rows: [[existingId]] })
+ .mockRejectedValueOnce(new Error("Database unavailable"));
+
+ await expect(
+ addVideosToFolder(folderId, [existingId], spaceId),
+ ).resolves.toMatchObject({ success: false });
+
+ expect(
+ mocks.query.mock.calls.every(([query]) =>
+ String(query).startsWith("select"),
+ ),
+ ).toBe(true);
+ });
+});
diff --git a/apps/web/__tests__/unit/agent-s3-endpoint-policy.test.ts b/apps/web/__tests__/unit/agent-s3-endpoint-policy.test.ts
new file mode 100644
index 000000000..7dc6ebfdd
--- /dev/null
+++ b/apps/web/__tests__/unit/agent-s3-endpoint-policy.test.ts
@@ -0,0 +1,202 @@
+import { Agent as HttpAgent } from "node:http";
+import { Agent as HttpsAgent } from "node:https";
+import type { S3ClientConfig } from "@aws-sdk/client-s3";
+import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
+
+const state = vi.hoisted(() => ({
+ isCap: "true" as string | undefined,
+ nodeEnv: "production",
+ s3Configs: [] as Array<Record<string, unknown>>,
+ send: vi.fn(async () => ({})),
+}));
+
+vi.mock("server-only", () => ({}));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+vi.mock("@/lib/rate-limit", () => ({
+ isRateLimited: vi.fn(async () => false),
+ RATE_LIMIT_IDS: {
+ AGENT_TOKEN_EXCHANGE: "rl_agent_token_exchange",
+ AGENT_AUTHORIZATION: "rl_agent_authorization",
+ AGENT_UNLOCK: "rl_agent_unlock",
+ AGENT_LOOM_IMPORT: "rl_loom_import_per_user",
+ },
+}));
+
+vi.mock("@cap/env", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("@cap/env")>();
+ return {
+ ...actual,
+ buildEnv: new Proxy(actual.buildEnv, {
+ get: (target, property) =>
+ property === "NEXT_PUBLIC_IS_CAP"
+ ? state.isCap
+ : Reflect.get(target, property),
+ }),
+ get NODE_ENV() {
+ return state.nodeEnv;
+ },
+ };
+});
+
+vi.mock("@aws-sdk/client-s3", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("@aws-sdk/client-s3")>();
+ class S3Client extends actual.S3Client {
+ constructor(config: S3ClientConfig) {
+ super(config);
+ const credentials = config.credentials as
+ | { accessKeyId?: string }
+ | undefined;
+ if (credentials?.accessKeyId === "AKIASYNTHETIC") {
+ state.s3Configs.push(config as Record<string, unknown>);
+ this.send = state.send as unknown as S3Client["send"];
+ }
+ }
+ }
+ return { ...actual, S3Client };
+});
+
+vi.mock("@/lib/agent-write", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("@/lib/agent-write")>();
+ return {
+ ...actual,
+ runAgentExternalMutation: (input: {
+ execute: (providerIdempotencyKey: string) => unknown;
+ }) => input.execute("synthetic-provider-key"),
+ };
+});
+
+const principalRow = {
+ tokenId: "agk_synthetic",
+ scopes: ["caps:read", "integrations:read", "integrations:write"],
+ expiresAt: new Date(Date.now() + 60 * 60 * 1000),
+ revokedAt: null,
+ lastUsedAt: new Date(),
+ id: "usr_synthetic",
+ email: "[email protected]",
+ activeOrganizationId: "org_synthetic",
+ role: "owner",
+ hasProSeat: true,
+ stripeSubscriptionStatus: "active",
+ thirdPartyStripeSubscriptionId: null,
+};
+
+const query = (): unknown =>
+ new Proxy(() => undefined, {
+ get: (_, property) =>
+ property === "then"
+ ? (resolve: (value: unknown) => void) => resolve([principalRow])
+ : query,
+ apply: query,
+ });
+
+vi.mock("@cap/database", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("@cap/database")>();
+ return { ...actual, db: query };
+});
+
+const token = `cap_cli_${"A".repeat(43)}`;
+
+describe("agent API S3 endpoint policy", () => {
+ let POST: typeof import("@/app/api/v1/[...route]/route").POST;
+ let PUT: typeof import("@/app/api/v1/[...route]/route").PUT;
+ let blockedS3EndpointMessage: string;
+
+ beforeAll(async () => {
+ Object.assign(process.env, {
+ NEXT_PUBLIC_WEB_URL: "http://localhost",
+ WEB_URL: "http://localhost",
+ DATABASE_URL: "mysql://unused:[email protected]:3306/unused",
+ NEXTAUTH_SECRET: "synthetic-test-secret-that-is-long-enough",
+ NEXTAUTH_URL: "http://localhost",
+ CAP_AWS_BUCKET: "synthetic",
+ CAP_AWS_REGION: "us-east-1",
+ CAP_AWS_ACCESS_KEY: "synthetic",
+ CAP_AWS_SECRET_KEY: "synthetic",
+ NODE_ENV: "test",
+ });
+ const route = await import("@/app/api/v1/[...route]/route");
+ POST = route.POST;
+ PUT = route.PUT;
+ ({ blockedS3EndpointMessage } = await import("@/lib/s3-endpoint-policy"));
+ });
+
+ beforeEach(() => {
+ state.isCap = "true";
+ state.nodeEnv = "production";
+ state.s3Configs.length = 0;
+ state.send.mockClear();
+ });
+
+ const s3Request = (method: "POST" | "PUT", endpoint: string) =>
+ new Request(
+ `http://localhost/api/v1/organizations/org_synthetic/storage/s3${
+ method === "POST" ? "/test" : ""
+ }`,
+ {
+ method,
+ headers: {
+ Authorization: `Bearer ${token}`,
+ "Content-Type": "application/json",
+ "Idempotency-Key": "synthetic-idempotency-key",
+ "X-Cap-Confirmation": "user",
+ },
+ body: JSON.stringify({
+ provider: "aws",
+ accessKeyId: "AKIASYNTHETIC",
+ secretAccessKey: "synthetic-secret",
+ endpoint,
+ bucketName: "synthetic-bucket",
+ region: "us-east-1",
+ }),
+ },
+ );
+
+ it.each([
+ ["PUT", "http://169.254.169.254"],
+ ["PUT", "http://localhost:9000"],
+ ["POST", "http://127.0.0.1:9000"],
+ ["POST", " http://[::1]:9000 "],
+ ] as const)(
+ "rejects %s with private endpoint %s on Cap Cloud",
+ async (method, endpoint) => {
+ const handler = method === "POST" ? POST : PUT;
+ const response = await handler(s3Request(method, endpoint));
+ const body = await response.json();
+
+ expect(response.status).toBe(400);
+ expect(body).toMatchObject({
+ code: "INVALID_REQUEST",
+ message: blockedS3EndpointMessage,
+ });
+ expect(state.s3Configs).toHaveLength(0);
+ expect(state.send).not.toHaveBeenCalled();
+ },
+ );
+
+ it("tests public endpoints through public-only agents on Cap Cloud", async () => {
+ const response = await POST(s3Request("POST", "https://8.8.8.8"));
+
+ expect(response.status).toBe(200);
+ expect(state.s3Configs).toHaveLength(1);
+ const requestHandler = state.s3Configs[0]?.requestHandler as {
+ httpAgent?: unknown;
+ httpsAgent?: unknown;
+ };
+ expect(requestHandler.httpAgent).toBeInstanceOf(HttpAgent);
+ expect(requestHandler.httpsAgent).toBeInstanceOf(HttpsAgent);
+ expect(state.send).toHaveBeenCalledTimes(1);
+ });
+
+ it("allows local endpoints on self-hosted deployments", async () => {
+ state.isCap = undefined;
+ const response = await POST(s3Request("POST", "http://localhost:9000"));
+
+ expect(response.status).toBe(200);
+ expect(state.s3Configs).toHaveLength(1);
+ expect(state.s3Configs[0]).toMatchObject({
+ endpoint: "http://localhost:9000",
+ });
+ expect(state.s3Configs[0]?.requestHandler).toBeUndefined();
+ expect(state.send).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/apps/web/__tests__/unit/agent-upload-entitlement.test.ts b/apps/web/__tests__/unit/agent-upload-entitlement.test.ts
index bc0adfdb5..70afdaf6a 100644
--- a/apps/web/__tests__/unit/agent-upload-entitlement.test.ts
+++ b/apps/web/__tests__/unit/agent-upload-entitlement.test.ts
@@ -136,6 +136,32 @@ describe("agent upload entitlement", () => {
expect(accept).toBeLessThan(processing);
});
+ it("checks the accepted upload key against the video before processing", () => {
+ const source = readFileSync(
+ join(process.cwd(), "app/api/v1/[...route]/route.ts"),
+ "utf8",
+ );
+ const completionSource = source.slice(
+ source.indexOf('.handle("completeUpload"'),
+ source.indexOf('.handle("importLoomCap"'),
+ );
+ const acceptedKeyCheck = completionSource.search(
+ /isVideoStorageKey\(\s*\{ id: path\.id, ownerId: principal\.id \},\s*acceptedRawFileKey,/,
+ );
+ const stateKeyCheck = completionSource.search(
+ /isVideoStorageKey\(\s*\{ id: state\.id, ownerId: state\.ownerId \},\s*state\.rawFileKey,/,
+ );
+
+ expect(acceptedKeyCheck).toBeGreaterThan(-1);
+ expect(stateKeyCheck).toBeGreaterThan(-1);
+ expect(acceptedKeyCheck).toBeLessThan(
+ completionSource.indexOf("runAgentMutation"),
+ );
+ expect(stateKeyCheck).toBeLessThan(
+ completionSource.indexOf("startVideoProcessingWorkflow"),
+ );
+ });
+
it("durably rejects only marked agent uploads and cleans storage first", () => {
const source = readFileSync(
join(process.cwd(), "app/api/v1/[...route]/route.ts"),
diff --git a/apps/web/__tests__/unit/analytics-chart-dates.test.ts b/apps/web/__tests__/unit/analytics-chart-dates.test.ts
new file mode 100644
index 000000000..a7eefb2b0
--- /dev/null
+++ b/apps/web/__tests__/unit/analytics-chart-dates.test.ts
@@ -0,0 +1,81 @@
+import { createElement, type ReactNode } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import ChartArea from "@/app/(org)/dashboard/analytics/components/ChartArea";
+
+vi.mock("recharts", () => ({
+ AreaChart: ({ data }: { data: Array<{ label: string; views: number }> }) =>
+ createElement(
+ "div",
+ null,
+ data.map((point) =>
+ createElement(
+ "span",
+ { key: point.label },
+ `${point.label}: ${point.views}`,
+ ),
+ ),
+ ),
+ Area: () => null,
+ CartesianGrid: () => null,
+ XAxis: () => null,
+ YAxis: () => null,
+}));
+
+vi.mock("@/components/ui/chart", () => ({
+ ChartContainer: ({ children }: { children: ReactNode }) => children,
+ ChartTooltip: () => null,
+ ChartTooltipContent: () => null,
+}));
+
+const renderChart = (buckets: string[]) =>
+ renderToStaticMarkup(
+ createElement(ChartArea, {
+ selectedMetrics: ["views"],
+ data: buckets.map((bucket, index) => ({
+ bucket,
+ views: index + 1,
+ caps: 0,
+ comments: 0,
+ reactions: 0,
+ })),
+ }),
+ );
+
+describe.each(["America/Los_Angeles", "Asia/Kolkata", "UTC"])(
+ "analytics chart dates in %s",
+ (timeZone) => {
+ beforeEach(() => vi.stubEnv("TZ", timeZone));
+ afterEach(() => vi.unstubAllEnvs());
+ it.each([
+ ["2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z"],
+ ["2026-03-08T00:00:00Z", "2026-03-09T00:00:00Z"],
+ ["2026-11-01T00:00:00Z", "2026-11-02T00:00:00Z"],
+ ])("labels UTC daily buckets on their actual date: %s", (...buckets) => {
+ const chart = renderChart(buckets);
+ for (const [index, bucket] of buckets.entries()) {
+ const label = new Date(bucket).toLocaleDateString([], {
+ month: "short",
+ day: "numeric",
+ timeZone: "UTC",
+ });
+ expect(chart).toContain(`${label}: ${index + 1}`);
+ }
+ });
+
+ it("keeps hourly labels in the viewer's local time", () => {
+ const buckets = ["2026-01-01T00:00:00Z", "2026-01-01T01:00:00Z"];
+ const chart = renderChart(buckets);
+ for (const [index, bucket] of buckets.entries()) {
+ const label = new Date(bucket).toLocaleTimeString([], {
+ hour: "numeric",
+ });
+ expect(chart).toContain(`${label}: ${index + 1}`);
+ }
+ });
+
+ it("shows the empty state when there are no buckets", () => {
+ expect(renderChart([])).toContain("No analytics data yet.");
+ });
+ },
+);
diff --git a/apps/web/__tests__/unit/assemblyai-client.test.ts b/apps/web/__tests__/unit/assemblyai-client.test.ts
new file mode 100644
index 000000000..42a926e1f
--- /dev/null
+++ b/apps/web/__tests__/unit/assemblyai-client.test.ts
@@ -0,0 +1,46 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { realAssemblyAIResponse } from "../fixtures/assemblyai-responses";
+
+const serverEnvMock = vi.hoisted(() =>
+ vi.fn<() => Record<string, string | undefined>>(() => ({})),
+);
+
+vi.mock("@cap/env", () => ({
+ serverEnv: serverEnvMock,
+}));
+
+import { createAssemblyAIClient } from "@/lib/assemblyai";
+
+const fetchMock = vi.fn(
+ async (_input: string, _init?: RequestInit) =>
+ new Response(JSON.stringify(realAssemblyAIResponse)),
+);
+
+describe("createAssemblyAIClient", () => {
+ beforeEach(() => {
+ vi.stubGlobal("fetch", fetchMock);
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ fetchMock.mockClear();
+ });
+
+ it.each([
+ [undefined, "https://api.assemblyai.com"],
+ ["", "https://api.assemblyai.com"],
+ ["https://api.eu.assemblyai.com", "https://api.eu.assemblyai.com"],
+ ["https://api.eu.assemblyai.com/", "https://api.eu.assemblyai.com"],
+ ])("ASSEMBLY_BASE_URL=%s sends requests to %s", async (baseUrl, host) => {
+ serverEnvMock.mockReturnValue({
+ ASSEMBLY_API_KEY: "key",
+ ASSEMBLY_BASE_URL: baseUrl,
+ });
+
+ await createAssemblyAIClient().transcripts.get(realAssemblyAIResponse.id);
+
+ expect(fetchMock.mock.calls[0]?.[0]).toBe(
+ `${host}/v2/transcript/${realAssemblyAIResponse.id}`,
+ );
+ });
+});
diff --git a/apps/web/__tests__/unit/async-video-code-reviews-page.test.ts b/apps/web/__tests__/unit/async-video-code-reviews-page.test.ts
index 088cf28c4..364f2a46c 100644
--- a/apps/web/__tests__/unit/async-video-code-reviews-page.test.ts
+++ b/apps/web/__tests__/unit/async-video-code-reviews-page.test.ts
@@ -55,6 +55,13 @@ describe("AsyncVideoCodeReviewsPage component content", () => {
expect(componentSource).toContain("comparisonTable");
});
+ it("describes the license split in the comparison table", () => {
+ expect(componentSource).toContain(
+ '{ text: "Yes — AGPLv3 and MIT crates", status: "positive" }',
+ );
+ expect(componentSource).not.toContain('{ text: "Yes — MIT"');
+ });
+
it("includes recording modes section", () => {
expect(componentSource).toContain("recordingModes");
});
diff --git a/apps/web/__tests__/unit/auth-options.test.ts b/apps/web/__tests__/unit/auth-options.test.ts
index 8f919507e..3b2ccf11c 100644
--- a/apps/web/__tests__/unit/auth-options.test.ts
+++ b/apps/web/__tests__/unit/auth-options.test.ts
@@ -44,9 +44,15 @@ const env = vi.hoisted(() => ({
GOOGLE_CLIENT_ID: "google-client",
GOOGLE_CLIENT_SECRET: "google-secret",
NEXTAUTH_SECRET: "next-auth-secret",
+ OIDC_ISSUER: undefined as string | undefined,
+ OIDC_CLIENT_ID: undefined as string | undefined,
+ OIDC_CLIENT_SECRET: undefined as string | undefined,
RESEND_API_KEY: undefined,
WORKOS_API_KEY: "workos-secret",
WORKOS_CLIENT_ID: "workos-client",
+ TRUSTED_PROXY_AUTH_HEADER: undefined as string | undefined,
+ TRUSTED_PROXY_AUTH_EMAIL: undefined as string | undefined,
+ TRUSTED_PROXY_AUTH_SECRET: undefined as string | undefined,
}));
vi.mock("@cap/env", () => ({
@@ -105,14 +111,29 @@ function callbacksFor(options: NextAuthOptions) {
return { signIn, jwt };
}
+// next-auth keeps a Credentials provider's custom id in `options` until it
+// merges providers at runtime, so read it from there when present.
+function providerIds(options: NextAuthOptions) {
+ return options.providers.map(
+ (provider) =>
+ (provider.options as { id?: string } | undefined)?.id ?? provider.id,
+ );
+}
+
describe("authOptions", () => {
beforeEach(() => {
env.APPLE_CLIENT_ID = "so.cap.auth";
env.APPLE_CLIENT_SECRET = "apple-secret";
env.WORKOS_CLIENT_ID = "workos-client";
env.WORKOS_API_KEY = "workos-secret";
+ env.OIDC_ISSUER = undefined;
+ env.OIDC_CLIENT_ID = undefined;
+ env.OIDC_CLIENT_SECRET = undefined;
env.CAP_ALLOWED_SIGNUP_DOMAINS = undefined;
env.CAP_BLOCKED_SIGNUP_DOMAINS = undefined;
+ env.TRUSTED_PROXY_AUTH_HEADER = undefined;
+ env.TRUSTED_PROXY_AUTH_EMAIL = undefined;
+ env.TRUSTED_PROXY_AUTH_SECRET = undefined;
mocks.validate.mockReset().mockResolvedValue(IDENTITY);
mocks.provision.mockReset().mockResolvedValue(undefined);
mocks.adapter.mockClear();
@@ -167,6 +188,229 @@ describe("authOptions", () => {
},
);
+ it("enables trusted-proxy sign-in when header, email and secret are configured", () => {
+ env.TRUSTED_PROXY_AUTH_HEADER = "x-openhost-is-owner";
+ env.TRUSTED_PROXY_AUTH_EMAIL = PROFILE.email;
+ env.TRUSTED_PROXY_AUTH_SECRET = "proxy-secret";
+
+ expect(providerIds(authOptions())).toContain("trusted-proxy");
+ });
+
+ it("does not expose trusted-proxy sign-in by default", () => {
+ expect(providerIds(authOptions())).not.toContain("trusted-proxy");
+ });
+
+ it.each([
+ "TRUSTED_PROXY_AUTH_HEADER",
+ "TRUSTED_PROXY_AUTH_EMAIL",
+ "TRUSTED_PROXY_AUTH_SECRET",
+ ] as const)(
+ "does not expose a partially configured trusted-proxy provider when %s is missing",
+ (key) => {
+ env.TRUSTED_PROXY_AUTH_HEADER = "x-openhost-is-owner";
+ env.TRUSTED_PROXY_AUTH_EMAIL = PROFILE.email;
+ env.TRUSTED_PROXY_AUTH_SECRET = "proxy-secret";
+ env[key] = undefined;
+
+ expect(providerIds(authOptions())).not.toContain("trusted-proxy");
+ },
+ );
+
+ it.each([undefined, "", " "])(
+ "does not register trusted-proxy with only header/email and an unusable secret (%s)",
+ (secret) => {
+ env.TRUSTED_PROXY_AUTH_HEADER = "x-openhost-is-owner";
+ env.TRUSTED_PROXY_AUTH_EMAIL = PROFILE.email;
+ env.TRUSTED_PROXY_AUTH_SECRET = secret;
+
+ expect(providerIds(authOptions())).not.toContain("trusted-proxy");
+ },
+ );
+
+ it.each([undefined, "proxy-secret"])(
+ "preserves independent OIDC sign-in and its verified-email gate with proxy secret %s",
+ async (secret) => {
+ env.OIDC_ISSUER = "https://idp.example";
+ env.OIDC_CLIENT_ID = "oidc-client";
+ env.OIDC_CLIENT_SECRET = "oidc-secret";
+ env.TRUSTED_PROXY_AUTH_HEADER = "x-openhost-is-owner";
+ env.TRUSTED_PROXY_AUTH_EMAIL = PROFILE.email;
+ env.TRUSTED_PROXY_AUTH_SECRET = secret;
+ const options = authOptions();
+ expect(providerIds(options)).toContain("oidc");
+ expect(providerIds(options).includes("trusted-proxy")).toBe(!!secret);
+ const { signIn } = callbacksFor(options);
+ const account: Account = {
+ provider: "oidc",
+ providerAccountId: "oidc-user",
+ type: "oauth",
+ };
+ for (const email_verified of [undefined, false, true]) {
+ const profile = { email: PROFILE.email, email_verified };
+ await expect(
+ signIn({
+ user: AUTHENTICATED_USER,
+ account,
+ profile,
+ }),
+ ).resolves.toBe(
+ email_verified === true ? true : "/login?error=OidcEmailUnverified",
+ );
+ }
+ },
+ );
+
+ describe("trusted-proxy authorize", () => {
+ type Authorize = (
+ credentials: Record<string, string> | undefined,
+ req: { headers?: Record<string, string> },
+ ) => Promise<unknown>;
+
+ function trustedProxyAuthorize() {
+ const options = authOptions();
+ const provider = options.providers[
+ providerIds(options).indexOf("trusted-proxy")
+ ] as { options?: { authorize?: Authorize } } | undefined;
+ const authorize = provider?.options?.authorize;
+ if (!authorize) throw new Error("trusted-proxy provider is missing");
+ return authorize;
+ }
+
+ beforeEach(() => {
+ env.TRUSTED_PROXY_AUTH_HEADER = "X-OpenHost-Is-Owner";
+ env.TRUSTED_PROXY_AUTH_EMAIL = "[email protected]";
+ env.TRUSTED_PROXY_AUTH_SECRET = "proxy-secret";
+ });
+
+ const validHeaders = {
+ "x-openhost-is-owner": "true",
+ "x-trusted-proxy-secret": "proxy-secret",
+ };
+
+ it("signs in the configured user when the proxy sets both headers", async () => {
+ await expect(
+ trustedProxyAuthorize()({}, { headers: validHeaders }),
+ ).resolves.toMatchObject({
+ id: AUTHENTICATED_USER.id,
+ email: AUTHENTICATED_USER.email,
+ });
+ });
+
+ it.each([
+ ["header is absent", { "x-trusted-proxy-secret": "proxy-secret" }],
+ [
+ "header is not exactly 'true'",
+ { ...validHeaders, "x-openhost-is-owner": "1" },
+ ],
+ ])("fails closed when the %s", async (_label, headers) => {
+ await expect(
+ trustedProxyAuthorize()({}, { headers }),
+ ).resolves.toBeNull();
+ expect(mocks.select).not.toHaveBeenCalled();
+ });
+
+ it("fails closed when the configured user does not exist", async () => {
+ env.TRUSTED_PROXY_AUTH_EMAIL = "[email protected]";
+
+ await expect(
+ trustedProxyAuthorize()({}, { headers: validHeaders }),
+ ).resolves.toBeNull();
+ expect(mocks.select).toHaveBeenCalledOnce();
+ });
+
+ it.each([undefined, "", " "])(
+ "fails closed if the configured secret becomes unusable after registration (%s)",
+ async (secret) => {
+ const authorize = trustedProxyAuthorize();
+ env.TRUSTED_PROXY_AUTH_SECRET = secret;
+ await expect(
+ authorize({}, { headers: validHeaders }),
+ ).resolves.toBeNull();
+ expect(mocks.select).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each([undefined, "", "wrong", "x".repeat(1024), ["proxy-secret"]])(
+ "rejects missing, empty, incorrect or malformed request secrets (%s)",
+ async (secret) => {
+ await expect(
+ trustedProxyAuthorize()(
+ {},
+ {
+ headers: {
+ ...validHeaders,
+ "x-trusted-proxy-secret": secret,
+ } as Record<string, string>,
+ },
+ ),
+ ).resolves.toBeNull();
+ expect(mocks.select).not.toHaveBeenCalled();
+ },
+ );
+
+ it("rejects requests without headers", async () => {
+ await expect(trustedProxyAuthorize()({}, {})).resolves.toBeNull();
+ expect(mocks.select).not.toHaveBeenCalled();
+ });
+
+ it("always requires the shared secret", async () => {
+ const authorize = trustedProxyAuthorize();
+
+ await expect(
+ authorize({}, { headers: { "x-openhost-is-owner": "true" } }),
+ ).resolves.toBeNull();
+ await expect(
+ authorize(
+ {},
+ {
+ headers: {
+ "x-openhost-is-owner": "true",
+ "x-trusted-proxy-secret": "wrong",
+ },
+ },
+ ),
+ ).resolves.toBeNull();
+ await expect(
+ authorize(
+ {},
+ {
+ headers: {
+ "x-openhost-is-owner": "true",
+ "x-trusted-proxy-secret": "proxy-secret",
+ },
+ },
+ ),
+ ).resolves.toMatchObject({ id: AUTHENTICATED_USER.id });
+ });
+ });
+
+ // authOptions() is rebuilt on every session lookup, so a per-instance
+ // warning would log on every authenticated request.
+ it("warns about trusted-proxy sign-in once per process", async () => {
+ vi.resetModules();
+ const { authOptions: freshAuthOptions } = await import(
+ "@cap/database/auth/auth-options"
+ );
+ env.TRUSTED_PROXY_AUTH_HEADER = "x-openhost-is-owner";
+ env.TRUSTED_PROXY_AUTH_EMAIL = PROFILE.email;
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
+
+ try {
+ void freshAuthOptions().providers;
+ expect(warn).not.toHaveBeenCalled();
+ env.TRUSTED_PROXY_AUTH_SECRET = "proxy-secret";
+ for (let i = 0; i < 3; i++) void freshAuthOptions().providers;
+
+ expect(
+ warn.mock.calls.filter(([message]) =>
+ String(message).includes("trusted-proxy sign-in is ENABLED"),
+ ),
+ ).toHaveLength(1);
+ } finally {
+ warn.mockRestore();
+ }
+ });
+
// Without an explicit maxAge next-auth falls back to 24 hours, which is far
// too long for a 6-digit code and contradicts what the OTP email tells users.
it("expires email verification codes after the advertised 10 minutes", () => {
diff --git a/apps/web/__tests__/unit/cloudfront-signed-url.test.ts b/apps/web/__tests__/unit/cloudfront-signed-url.test.ts
new file mode 100644
index 000000000..809bb2095
--- /dev/null
+++ b/apps/web/__tests__/unit/cloudfront-signed-url.test.ts
@@ -0,0 +1,85 @@
+import { generateKeyPairSync } from "node:crypto";
+import { describe, expect, it } from "vitest";
+
+import {
+ createCloudFrontSignedUrl,
+ getCloudFrontObjectUrl,
+} from "../../../../packages/web-backend/src/S3Buckets/cloudfront-signed-url";
+
+const { privateKey } = generateKeyPairSync("rsa", {
+ modulusLength: 2048,
+ privateKeyEncoding: { type: "pkcs1", format: "pem" },
+ publicKeyEncoding: { type: "spki", format: "pem" },
+});
+
+const bucketUrl = "https://cdn.example.com";
+
+const sign = (key: string) =>
+ createCloudFrontSignedUrl({
+ bucketUrl,
+ key,
+ keyPairId: "K2JCJMDEHXQW5F",
+ privateKey,
+ expiresAt: 1_900_000_000,
+ });
+
+const decodePolicy = (signedUrl: string) => {
+ const encoded = new URL(signedUrl).searchParams.get("Policy") ?? "";
+ const base64 = encoded
+ .replace(/-/g, "+")
+ .replace(/_/g, "=")
+ .replace(/~/g, "/");
+ return JSON.parse(Buffer.from(base64, "base64").toString("utf8")) as {
+ Statement: { Resource: string }[];
+ };
+};
+
+const resourceOf = (signedUrl: string) =>
+ decodePolicy(signedUrl).Statement[0]?.Resource;
+
+const baseOf = (signedUrl: string) =>
+ signedUrl.slice(0, signedUrl.indexOf("?"));
+
+describe("createCloudFrontSignedUrl", () => {
+ it("signs ordinary keys for the exact object URL", () => {
+ const signedUrl = sign("user/video/result.mp4");
+
+ expect(resourceOf(signedUrl)).toBe(
+ "https://cdn.example.com/user/video/result.mp4",
+ );
+ expect(baseOf(signedUrl)).toBe(resourceOf(signedUrl));
+ });
+
+ it("does not leave CloudFront wildcards in the policy resource", () => {
+ for (const key of [
+ "user/video/*",
+ "user/video/?",
+ "user/video/*.mp4",
+ "user/video/a?b/*",
+ ]) {
+ const signedUrl = sign(key);
+ const resource = resourceOf(signedUrl) ?? "";
+
+ expect(resource).not.toMatch(/[*?]/);
+ expect(baseOf(signedUrl)).toBe(resource);
+ }
+ });
+
+ it("percent-encodes wildcard and reserved characters per path segment", () => {
+ expect(getCloudFrontObjectUrl(bucketUrl, "user/video/a*b?c d#e%f")).toBe(
+ "https://cdn.example.com/user/video/a%2Ab%3Fc%20d%23e%25f",
+ );
+ expect(getCloudFrontObjectUrl(bucketUrl, "user/video/(1)!'.mp4")).toBe(
+ "https://cdn.example.com/user/video/%281%29%21%27.mp4",
+ );
+ });
+
+ it("keeps the returned URL aligned with the signed resource for encoded keys", () => {
+ const signedUrl = sign("user/video/my clip (1).mp4");
+
+ expect(resourceOf(signedUrl)).toBe(
+ "https://cdn.example.com/user/video/my%20clip%20%281%29.mp4",
+ );
+ expect(baseOf(signedUrl)).toBe(resourceOf(signedUrl));
+ });
+});
diff --git a/apps/web/__tests__/unit/comment-recorder-lifecycle.test.ts b/apps/web/__tests__/unit/comment-recorder-lifecycle.test.ts
new file mode 100644
index 000000000..9ecdf58cb
--- /dev/null
+++ b/apps/web/__tests__/unit/comment-recorder-lifecycle.test.ts
@@ -0,0 +1,186 @@
+// @vitest-environment jsdom
+
+import { act, createElement } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ display: vi.fn(),
+ mic: vi.fn(),
+ mixer: vi.fn(),
+}));
+
+vi.mock("@cap/recorder-core/capture-streams", () => ({
+ acquireCameraStream: vi.fn(),
+ acquireCameraWithMicStream: vi.fn(),
+ acquireDisplayStream: mocks.display,
+ acquireMicStream: mocks.mic,
+ createAudioMixer: mocks.mixer,
+ getCaptureErrorMessage: (error: Error) => error.message,
+}));
+
+import { useCommentRecorder } from "@/app/s/[videoId]/_components/timeline/recording/useCommentRecorder";
+
+class Track extends EventTarget {
+ kind: "audio" | "video";
+ readyState: MediaStreamTrackState = "live";
+ stop = vi.fn(() => {
+ this.readyState = "ended";
+ });
+
+ constructor(kind: "audio" | "video") {
+ super();
+ this.kind = kind;
+ }
+
+ end() {
+ this.readyState = "ended";
+ this.dispatchEvent(new Event("ended"));
+ }
+
+ getSettings() {
+ return {};
+ }
+}
+
+class Stream {
+ constructor(private readonly tracks: Track[]) {}
+
+ getTracks() {
+ return this.tracks;
+ }
+
+ getVideoTracks() {
+ return this.tracks.filter((track) => track.kind === "video");
+ }
+
+ getAudioTracks() {
+ return this.tracks.filter((track) => track.kind === "audio");
+ }
+}
+
+class Recorder {
+ static isTypeSupported = () => true;
+ static startError: Error | null = null;
+
+ state: RecordingState = "inactive";
+ ondataavailable: ((event: BlobEvent) => void) | null = null;
+ onstop: ((event: Event) => void) | null = null;
+ onerror: ((event: Event) => void) | null = null;
+ mimeType = "video/webm";
+
+ constructor(private readonly stream: Stream) {}
+
+ start() {
+ if (Recorder.startError) throw Recorder.startError;
+ if (this.stream.getVideoTracks()[0]?.readyState === "ended") {
+ throw new DOMException("The video track ended", "InvalidStateError");
+ }
+ this.state = "recording";
+ }
+
+ stop() {
+ this.state = "inactive";
+ }
+}
+
+const deferred = <T>() => {
+ let resolve!: (value: T) => void;
+ const promise = new Promise<T>((done) => {
+ resolve = done;
+ });
+ return { promise, resolve };
+};
+
+describe("comment recorder lifecycle", () => {
+ let root: Root;
+ let container: HTMLDivElement;
+ let recorder: ReturnType<typeof useCommentRecorder>;
+
+ beforeEach(async () => {
+ vi.clearAllMocks();
+ Recorder.startError = null;
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ vi.stubGlobal("MediaStream", Stream);
+ vi.stubGlobal("MediaRecorder", Recorder);
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+ function Harness() {
+ recorder = useCommentRecorder({
+ onFinish: vi.fn(),
+ selectedCameraId: null,
+ selectedMicId: "mic",
+ });
+ return null;
+ }
+ await act(async () => root.render(createElement(Harness)));
+ });
+
+ afterEach(async () => {
+ await act(async () => root.unmount());
+ container.remove();
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+ });
+
+ it("releases capture when screen sharing ends during microphone setup", async () => {
+ const videoTrack = new Track("video");
+ const micTrack = new Track("audio");
+ const mixedTrack = new Track("audio");
+ const displayStream = new Stream([videoTrack]);
+ const micStream = new Stream([micTrack]);
+ const closeMixer = vi.fn(async () => {});
+ const pendingMic = deferred<Stream>();
+ const start = vi.spyOn(Recorder.prototype, "start");
+ mocks.display.mockResolvedValue(displayStream);
+ mocks.mic.mockReturnValue(pendingMic.promise);
+ mocks.mixer.mockResolvedValue({
+ context: {},
+ stream: new Stream([mixedTrack]),
+ setMicStream: vi.fn(),
+ close: closeMixer,
+ });
+
+ let starting!: Promise<void>;
+ await act(async () => {
+ starting = recorder.startScreen();
+ await Promise.resolve();
+ });
+ expect(recorder.phase).toBe("requesting");
+
+ videoTrack.end();
+ await act(async () => {
+ pendingMic.resolve(micStream);
+ await starting;
+ });
+
+ expect(recorder.phase).toBe("idle");
+ expect(recorder.error).toBe(
+ "The recording source ended before recording could start.",
+ );
+ expect(start).not.toHaveBeenCalled();
+ expect(videoTrack.stop).toHaveBeenCalled();
+ expect(micTrack.stop).toHaveBeenCalled();
+ expect(mixedTrack.stop).toHaveBeenCalled();
+ expect(closeMixer).toHaveBeenCalled();
+ });
+
+ it("releases capture when MediaRecorder rejects a live source", async () => {
+ const videoTrack = new Track("video");
+ mocks.display.mockResolvedValue(new Stream([videoTrack]));
+ mocks.mic.mockRejectedValue(new Error("No microphone"));
+ Recorder.startError = new DOMException(
+ "The recorder could not start",
+ "InvalidStateError",
+ );
+
+ await act(async () => recorder.startScreen());
+
+ expect(recorder.phase).toBe("idle");
+ expect(recorder.error).toBe(
+ "Recording couldn't start. Please choose the source again.",
+ );
+ expect(videoTrack.stop).toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/comment-reply-parent.test.ts b/apps/web/__tests__/unit/comment-reply-parent.test.ts
new file mode 100644
index 000000000..793464700
--- /dev/null
+++ b/apps/web/__tests__/unit/comment-reply-parent.test.ts
@@ -0,0 +1,180 @@
+import * as Db from "@cap/database/schema";
+import type { SQL } from "drizzle-orm";
+import { MySqlDialect, type MySqlTable } from "drizzle-orm/mysql-core";
+import { Effect, Option } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+type Row = Record<string, unknown>;
+
+const videoId = "accessible-video";
+const commentId = "new-comment";
+const mediaKey = `owner/${videoId}/comments/${commentId}/media.webm`;
+
+const mocks = vi.hoisted(() => ({
+ notify: vi.fn(async () => undefined),
+ state: { parents: [] as Row[], inserted: [] as Row[] },
+}));
+
+const dialect = new MySqlDialect();
+
+function matchesParent(condition: SQL, row: Row) {
+ const { sql, params } = dialect.sqlToQuery(condition);
+ let index = 0;
+ return sql
+ .replaceAll(/[()]/g, "")
+ .split(" and ")
+ .every((clause) => {
+ const match = /^`comments`\.`([^`]+)` = \?$/.exec(clause);
+ if (!match?.[1]) throw new Error(`Unsupported predicate: ${clause}`);
+ return row[match[1]] === params[index++];
+ });
+}
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({
+ from: (table: MySqlTable) => ({
+ where: (condition: SQL) => ({
+ limit: async () => {
+ if (table === Db.videos) return [{ id: videoId }];
+ if (table !== Db.comments) throw new Error("Unexpected table");
+ return mocks.state.parents.filter((row) =>
+ matchesParent(condition, row),
+ );
+ },
+ }),
+ }),
+ }),
+ insert: (table: MySqlTable) => ({
+ values: async (row: Row) => {
+ if (table !== Db.comments) throw new Error("Unexpected insert");
+ mocks.state.inserted.push(row);
+ },
+ }),
+ }),
+}));
+vi.mock("@cap/database/helpers", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@cap/database/helpers")>()),
+ nanoId: () => commentId,
+}));
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: async () => ({ id: "viewer", name: "Viewer" }),
+}));
+vi.mock("@cap/web-domain", () => ({
+ Comment: { CommentId: { make: (id: string) => id } },
+ Policy: { withPublicPolicy: () => (effect: unknown) => effect },
+}));
+vi.mock("@cap/web-backend", () => ({
+ provideOptionalAuth: (effect: unknown) => effect,
+ verifyCommentMediaUploadToken: () => ({
+ userId: "viewer",
+ videoId,
+ commentId,
+ kind: "audio",
+ key: mediaKey,
+ thumbnailKey: null,
+ contentType: "audio/webm",
+ }),
+ VideosRepo: Effect.succeed({
+ getById: () =>
+ Effect.succeed(Option.some([{ id: videoId, ownerId: "owner" }])),
+ }),
+ VideosPolicy: Effect.succeed({ canView: () => Effect.void }),
+ Storage: {
+ getAccessForVideo: () =>
+ Effect.succeed([
+ { headObject: () => Effect.succeed({ ContentLength: 2048 }) },
+ ]),
+ },
+}));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+vi.mock("@/lib/comment-media", () => ({ sanitizeWaveform: () => undefined }));
+vi.mock("@/lib/media-comments", () => ({
+ canUseMediaComments: async () => true,
+}));
+vi.mock("@/lib/Notification", () => ({ createNotification: mocks.notify }));
+vi.mock("@/lib/server", () => ({ runPromiseExit: Effect.runPromiseExit }));
+
+import { newComment } from "@/actions/videos/new-comment";
+import { newMediaComment } from "@/actions/videos/new-media-comment";
+
+function postText(parentCommentId = "parent") {
+ return newComment({
+ content: "A reply",
+ videoId: videoId as never,
+ type: "text",
+ authorImage: null,
+ parentCommentId: parentCommentId as never,
+ timestamp: null,
+ });
+}
+
+function postMedia(parentCommentId = "parent") {
+ return newMediaComment({
+ videoId: videoId as never,
+ commentId: commentId as never,
+ uploadToken: "token",
+ kind: "audio",
+ durationSeconds: 3,
+ parentCommentId: parentCommentId as never,
+ timestamp: null,
+ authorImage: null,
+ });
+}
+
+const rejectedParents = [
+ {
+ name: "belongs to another video",
+ parents: [{ id: "parent", videoId: "private-video", type: "text" }],
+ },
+ { name: "does not exist", parents: [] },
+];
+
+beforeEach(() => {
+ mocks.state.parents = [{ id: "parent", videoId, type: "text" }];
+ mocks.state.inserted = [];
+ mocks.notify.mockClear();
+});
+
+describe.each([
+ { action: "newComment", post: postText },
+ { action: "newMediaComment", post: postMedia },
+])("$action reply parent authorization", ({ post }) => {
+ it.each(rejectedParents)(
+ "rejects a parent that $name",
+ async ({ parents }) => {
+ mocks.state.parents = parents;
+ await expect(post()).rejects.toThrow("Comment not found");
+ expect(mocks.state.inserted).toEqual([]);
+ expect(mocks.notify).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each(["text", "emoji", "video", "audio"])(
+ "preserves replies to a %s comment on the same video",
+ async (type) => {
+ mocks.state.parents = [{ id: "parent", videoId, type }];
+ await expect(post()).resolves.toMatchObject({
+ parentCommentId: "parent",
+ videoId,
+ });
+ expect(mocks.state.inserted).toHaveLength(1);
+ expect(mocks.notify).toHaveBeenCalledWith(
+ expect.objectContaining({
+ type: "reply",
+ parentCommentId: "parent",
+ videoId,
+ }),
+ );
+ },
+ );
+
+ it("preserves top-level comments", async () => {
+ mocks.state.parents = [];
+ await expect(post("")).resolves.toMatchObject({ parentCommentId: "" });
+ expect(mocks.state.inserted).toHaveLength(1);
+ expect(mocks.notify).toHaveBeenCalledWith(
+ expect.objectContaining({ type: "comment" }),
+ );
+ });
+});
diff --git a/apps/web/__tests__/unit/cookie-write-site.test.ts b/apps/web/__tests__/unit/cookie-write-site.test.ts
new file mode 100644
index 000000000..564ff3db0
--- /dev/null
+++ b/apps/web/__tests__/unit/cookie-write-site.test.ts
@@ -0,0 +1,473 @@
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
+import {
+ Database,
+ getCurrentUser,
+ HttpAuthMiddlewareLive,
+ RpcAuthMiddlewareLive,
+} from "@cap/web-backend";
+import {
+ CurrentUser,
+ DatabaseError,
+ HttpAuthMiddleware,
+ RpcAuthMiddleware,
+} from "@cap/web-domain";
+import {
+ HttpApi,
+ HttpApiBuilder,
+ HttpApiEndpoint,
+ HttpApiGroup,
+ HttpServer,
+ HttpServerRequest,
+} from "@effect/platform";
+import { Rpc, RpcGroup, RpcSerialization, RpcServer } from "@effect/rpc";
+import { Effect, Layer, Option, Schema } from "effect";
+import { Hono } from "hono";
+import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => {
+ const cookieUser = {
+ id: "cookie-user",
+ email: "[email protected]",
+ activeOrganizationId: "org",
+ image: null,
+ };
+ const keyUser = {
+ id: "key-user",
+ email: "[email protected]",
+ activeOrganizationId: "org",
+ image: null,
+ };
+ const client = {
+ select: () => ({
+ from: () => ({
+ where: async () => [cookieUser],
+ leftJoin: () => ({ where: async () => [{ users: keyUser }] }),
+ }),
+ }),
+ update: () => ({ set: () => ({ where: async () => undefined }) }),
+ };
+ return {
+ cookieUser,
+ keyUser,
+ client,
+ getServerSession: vi.fn(async () => ({ user: { id: cookieUser.id } })),
+ getSessionUser: vi.fn(async () => cookieUser),
+ };
+});
+
+vi.mock("server-only", () => ({}));
+
+vi.mock("@cap/env", () => ({
+ serverEnv: () => ({ WEB_URL: "https://cap.so" }),
+ buildEnv: {
+ NEXT_PUBLIC_WEB_URL: "https://cap.so",
+ NEXT_PUBLIC_IS_CAP: "true",
+ },
+}));
+
+vi.mock("@cap/database", () => ({ db: () => mocks.client }));
+
+vi.mock("@cap/database/auth/auth-options", () => ({
+ authOptions: () => ({}),
+ getServerSession: mocks.getServerSession,
+}));
+
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: mocks.getSessionUser,
+}));
+
+import { POST as updateUserName } from "@/app/api/settings/user/name/route";
+import { withAuth, withOptionalAuth } from "@/app/api/utils";
+
+const API_KEY = "0123456789abcdef0123456789abcdef0123";
+
+const request = (
+ method: string,
+ headers: Record<string, string> = {},
+ url = "https://cap.so/api/test",
+) =>
+ new Request(url, {
+ method,
+ headers: { host: new URL(url).host, ...headers },
+ });
+
+const crossSite = {
+ "sec-fetch-site": "cross-site",
+ origin: "https://other.example",
+};
+const sameOrigin = {
+ "sec-fetch-site": "same-origin",
+ origin: "https://cap.so",
+};
+const sameSite = {
+ "sec-fetch-site": "same-site",
+ origin: "https://www.cap.so",
+};
+
+beforeEach(() => {
+ mocks.getServerSession.mockClear();
+ mocks.getSessionUser.mockClear();
+});
+
+describe("isCrossSiteCookieWrite", () => {
+ it.each(["POST", "PUT", "PATCH", "DELETE"])(
+ "flags a %s with Sec-Fetch-Site cross-site",
+ (method) => {
+ expect(isCrossSiteCookieWrite(request(method, crossSite))).toBe(true);
+ },
+ );
+
+ it("flags a write whose Origin is another site when fetch metadata is missing", () => {
+ expect(
+ isCrossSiteCookieWrite(
+ request("POST", { origin: "https://other.example" }),
+ ),
+ ).toBe(true);
+ expect(isCrossSiteCookieWrite(request("POST", { origin: "null" }))).toBe(
+ true,
+ );
+ });
+
+ it.each([
+ ["same-origin", sameOrigin],
+ ["same-site", sameSite],
+ ["user-initiated", { "sec-fetch-site": "none" }],
+ ["app origin without fetch metadata", { origin: "https://cap.so" }],
+ ["no browser headers", {}],
+ ])("allows a %s write", (_label, headers) => {
+ expect(isCrossSiteCookieWrite(request("POST", headers))).toBe(false);
+ });
+
+ it("treats the request host as an own origin for custom domains and previews", () => {
+ expect(
+ isCrossSiteCookieWrite(
+ request(
+ "POST",
+ { origin: "https://videos.customer.example" },
+ "https://videos.customer.example/api/analytics/track",
+ ),
+ ),
+ ).toBe(false);
+ expect(
+ isCrossSiteCookieWrite(
+ request(
+ "POST",
+ {
+ origin: "https://cap-preview.vercel.app",
+ host: "internal:3000",
+ "x-forwarded-host": "cap-preview.vercel.app",
+ },
+ "http://internal:3000/api/test",
+ ),
+ ),
+ ).toBe(false);
+ expect(
+ isCrossSiteCookieWrite({
+ method: "POST",
+ url: "http://localhost:3000/api/test",
+ headers: new Headers({ origin: "http://localhost:3000" }),
+ }),
+ ).toBe(false);
+ });
+
+ it("still flags a cross-site write that carries an Authorization header", () => {
+ expect(
+ isCrossSiteCookieWrite(
+ request("POST", { ...crossSite, authorization: `Bearer ${API_KEY}` }),
+ ),
+ ).toBe(true);
+ expect(
+ isCrossSiteCookieWrite(
+ request("POST", { ...crossSite, authorization: "Bearer bogus" }),
+ ),
+ ).toBe(true);
+ });
+
+ it.each(["GET", "HEAD", "OPTIONS"])("leaves %s alone", (method) => {
+ expect(isCrossSiteCookieWrite(request(method, crossSite))).toBe(false);
+ });
+});
+
+describe("Hono cookie fallback", () => {
+ const app = new Hono()
+ .post("/required", withAuth, (c) => c.json({ user: c.get("user").id }))
+ .get("/required", withAuth, (c) => c.json({ user: c.get("user").id }))
+ .post("/optional", withOptionalAuth, (c) =>
+ c.json({ user: c.get("user")?.id ?? null }),
+ );
+
+ const send = (
+ path: string,
+ method: string,
+ headers: Record<string, string>,
+ ) => app.request(request(method, headers, `https://cap.so${path}`));
+
+ it("does not authenticate a cross-site cookie write", async () => {
+ const response = await send("/required", "POST", crossSite);
+ expect(response.status).toBe(401);
+ expect(mocks.getSessionUser).not.toHaveBeenCalled();
+ });
+
+ it("does not authenticate a cross-site cookie write carrying a bogus bearer", async () => {
+ const response = await send("/required", "POST", {
+ ...crossSite,
+ authorization: "Bearer bogus",
+ });
+ expect(response.status).toBe(401);
+ expect(mocks.getSessionUser).not.toHaveBeenCalled();
+ });
+
+ it("keeps optional endpoints working anonymously for cross-site writes", async () => {
+ const response = await send("/optional", "POST", crossSite);
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ user: null });
+ });
+
+ it("keeps optional endpoints anonymous for a cross-site write with a bogus bearer", async () => {
+ const response = await send("/optional", "POST", {
+ ...crossSite,
+ authorization: "Bearer bogus",
+ });
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ user: null });
+ });
+
+ it.each([
+ ["same-origin", sameOrigin],
+ ["same-site", sameSite],
+ ])("authenticates a %s cookie write", async (_label, headers) => {
+ const response = await send("/required", "POST", headers);
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ user: "cookie-user" });
+ });
+
+ it("authenticates a bearer write from any site", async () => {
+ const response = await send("/required", "POST", {
+ ...crossSite,
+ authorization: `Bearer ${API_KEY}`,
+ });
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ user: "key-user" });
+ });
+
+ it("authenticates a cross-site GET with the cookie", async () => {
+ const response = await send("/required", "GET", crossSite);
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ user: "cookie-user" });
+ });
+});
+
+describe("App Router cookie writes", () => {
+ const rename = (headers: Record<string, string>) =>
+ updateUserName(
+ new Request("https://cap.so/api/settings/user/name", {
+ method: "POST",
+ headers: { host: "cap.so", ...headers },
+ body: JSON.stringify({ firstName: "A", lastName: "B" }),
+ }) as never,
+ );
+
+ it("does not authenticate a cross-site cookie write", async () => {
+ const response = await rename(crossSite);
+ expect(response.status).toBe(401);
+ expect(mocks.getSessionUser).not.toHaveBeenCalled();
+ });
+
+ it("authenticates a same-origin cookie write", async () => {
+ const response = await rename(sameOrigin);
+ expect(response.status).toBe(200);
+ expect(mocks.getSessionUser).toHaveBeenCalledTimes(1);
+ });
+});
+
+const database = Layer.succeed(
+ Database,
+ Database.make({
+ use: (callback) =>
+ Effect.tryPromise({
+ try: () => callback(mocks.client as never),
+ catch: (cause) => new DatabaseError({ cause }),
+ }),
+ }),
+);
+
+describe("Effect getCurrentUser", () => {
+ const run = (web?: Request) =>
+ Effect.runPromise(
+ getCurrentUser.pipe(
+ Effect.map(Option.map((user) => user.id)),
+ Effect.provide(database),
+ (effect) =>
+ web
+ ? Effect.provideService(
+ effect,
+ HttpServerRequest.HttpServerRequest,
+ HttpServerRequest.fromWeb(web),
+ )
+ : effect,
+ ),
+ );
+
+ it.each([
+ ["plain cross-site write", request("POST", crossSite)],
+ [
+ "cross-site write carrying a bogus bearer",
+ request("POST", { ...crossSite, authorization: "Bearer other" }),
+ ],
+ ])("returns no user for a %s", async (_label, web) => {
+ expect(await run(web)).toEqual(Option.none());
+ expect(mocks.getServerSession).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ ["same-origin write", request("POST", sameOrigin)],
+ ["same-site write", request("POST", sameSite)],
+ ["cross-site GET", request("GET", crossSite)],
+ ["call outside a request", undefined],
+ ])("returns the cookie user for a %s", async (_label, web) => {
+ expect(await run(web)).toEqual(Option.some("cookie-user"));
+ });
+});
+
+const WhoAmI = HttpApiEndpoint.post("whoAmI", "/api/who-am-i")
+ .addSuccess(Schema.Struct({ user: Schema.String }))
+ .middleware(HttpAuthMiddleware);
+const WhoAmIGet = HttpApiEndpoint.get("whoAmIGet", "/api/who-am-i")
+ .addSuccess(Schema.Struct({ user: Schema.String }))
+ .middleware(HttpAuthMiddleware);
+class WhoAmIApi extends HttpApi.make("whoAmI").add(
+ HttpApiGroup.make("whoAmI").add(WhoAmI).add(WhoAmIGet),
+) {}
+
+const httpApi = HttpApiBuilder.api(WhoAmIApi).pipe(
+ Layer.provide(
+ HttpApiBuilder.group(WhoAmIApi, "whoAmI", (handlers) =>
+ handlers
+ .handle("whoAmI", () =>
+ Effect.map(CurrentUser, (user) => ({ user: user.id })),
+ )
+ .handle("whoAmIGet", () =>
+ Effect.map(CurrentUser, (user) => ({ user: user.id })),
+ ),
+ ),
+ ),
+ Layer.provide(HttpAuthMiddlewareLive),
+ Layer.provide(database),
+ Layer.merge(HttpServer.layerContext),
+ HttpApiBuilder.toWebHandler,
+);
+
+const RpcWhoAmI = Rpc.make("WhoAmI", { success: Schema.String }).middleware(
+ RpcAuthMiddleware,
+);
+const rpcGroup = RpcGroup.make(RpcWhoAmI);
+const rpcApi = RpcServer.toWebHandler(rpcGroup, {
+ layer: Layer.mergeAll(
+ rpcGroup.toLayer({
+ WhoAmI: () => Effect.map(CurrentUser, (user) => user.id),
+ }),
+ RpcAuthMiddlewareLive,
+ RpcSerialization.layerJson,
+ HttpServer.layerContext,
+ ).pipe(Layer.provide(database)),
+});
+
+afterAll(async () => {
+ await httpApi.dispose();
+ await rpcApi.dispose();
+});
+
+describe("HttpApi auth middleware", () => {
+ const send = (method: string, headers: Record<string, string>) =>
+ httpApi.handler(request(method, headers, "https://cap.so/api/who-am-i"));
+
+ it("does not authenticate a cross-site cookie write", async () => {
+ const response = await send("POST", crossSite);
+ expect(response.status).toBe(401);
+ });
+
+ it("does not authenticate a cross-site cookie write carrying a bogus bearer", async () => {
+ const response = await send("POST", {
+ ...crossSite,
+ authorization: "Bearer bogus",
+ });
+ expect(response.status).toBe(401);
+ });
+
+ it.each([
+ ["same-origin", sameOrigin],
+ ["same-site", sameSite],
+ ])("authenticates a %s cookie write", async (_label, headers) => {
+ const response = await send("POST", headers);
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ user: "cookie-user" });
+ });
+
+ it("authenticates a bearer write from any site", async () => {
+ const response = await send("POST", {
+ ...crossSite,
+ authorization: `Bearer ${API_KEY}`,
+ });
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ user: "key-user" });
+ });
+
+ it("authenticates a cross-site GET with the cookie", async () => {
+ const response = await send("GET", crossSite);
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ user: "cookie-user" });
+ });
+});
+
+describe("RPC auth middleware", () => {
+ const call = async (
+ headers: Record<string, string>,
+ rpcHeaders: [string, string][] = [],
+ ) => {
+ const response = await rpcApi.handler(
+ new Request("https://cap.so/api/erpc", {
+ method: "POST",
+ headers: {
+ host: "cap.so",
+ "content-type": "application/json",
+ ...headers,
+ },
+ body: JSON.stringify({
+ _tag: "Request",
+ id: "1",
+ tag: "WhoAmI",
+ payload: {},
+ headers: rpcHeaders,
+ }),
+ }),
+ );
+ const [message] = (await response.json()) as [
+ { exit: { _tag: string; value?: unknown; cause?: unknown } },
+ ];
+ return message.exit;
+ };
+
+ it("does not authenticate a cross-site cookie call", async () => {
+ const exit = await call(crossSite);
+ expect(exit._tag).toBe("Failure");
+ expect(JSON.stringify(exit.cause)).toContain("UnauthenticatedError");
+ });
+
+ it("reads fetch metadata from the HTTP request, not the RPC payload headers", async () => {
+ const exit = await call(crossSite, [
+ ["sec-fetch-site", "same-origin"],
+ ["origin", "https://cap.so"],
+ ]);
+ expect(exit._tag).toBe("Failure");
+ });
+
+ it.each([
+ ["same-origin", sameOrigin],
+ ["same-site", sameSite],
+ ])("authenticates a %s cookie call", async (_label, headers) => {
+ expect(await call(headers)).toEqual({
+ _tag: "Success",
+ value: "cookie-user",
+ });
+ });
+});
diff --git a/apps/web/__tests__/unit/dashboard-video-count.test.ts b/apps/web/__tests__/unit/dashboard-video-count.test.ts
new file mode 100644
index 000000000..991854bc8
--- /dev/null
+++ b/apps/web/__tests__/unit/dashboard-video-count.test.ts
@@ -0,0 +1,112 @@
+import type { Database, ImageUploads, Spaces } from "@cap/web-backend";
+import { Effect, Layer } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import CapsPage from "@/app/(org)/dashboard/caps/page";
+import SharedCapsPage from "@/app/(org)/dashboard/spaces/[spaceId]/page";
+
+const mocks = vi.hoisted(() => ({ query: vi.fn() }));
+
+vi.mock("@cap/database", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ const client = drizzle(mocks.query);
+ return { db: () => client };
+});
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: async () => ({ id: "user-1", activeOrganizationId: "org-1" }),
+}));
+vi.mock("@cap/env", () => ({ serverEnv: () => ({}) }));
+vi.mock("@cap/web-backend", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ const { Effect, Layer } = await import("effect");
+ const client = drizzle(mocks.query);
+ class Database extends Effect.Service<Database>()("Database", {
+ succeed: {
+ use: <T>(callback: (db: typeof client) => Promise<T>) =>
+ Effect.promise(() => callback(client)),
+ },
+ }) {}
+ class ImageUploads extends Effect.Service<ImageUploads>()("ImageUploads", {
+ succeed: { resolveImageUrl: (value: string) => Effect.succeed(value) },
+ }) {}
+ class Spaces extends Effect.Service<Spaces>()("Spaces", {
+ succeed: {
+ getSpaceOrOrg: () =>
+ Effect.succeed({
+ variant: "organization",
+ organization: { id: "org-1" },
+ }),
+ },
+ }) {}
+ return {
+ Database,
+ ImageUploads,
+ Spaces,
+ makeCurrentUserLayer: () => Layer.empty,
+ };
+});
+vi.mock("@/lib/server", async () => {
+ const { Database, ImageUploads, Spaces } = await import("@cap/web-backend");
+ return {
+ runPromise: <A, E>(
+ effect: Effect.Effect<A, E, Database | ImageUploads | Spaces>,
+ ) =>
+ Effect.runPromise(
+ Effect.provide(
+ effect,
+ Layer.mergeAll(
+ Database.Default,
+ ImageUploads.Default,
+ Spaces.Default,
+ ),
+ ),
+ ),
+ };
+});
+vi.mock("@/app/(org)/dashboard/caps/Caps", () => ({ Caps: () => null }));
+vi.mock("@/app/(org)/dashboard/spaces/[spaceId]/SharedCaps", () => ({
+ SharedCaps: () => null,
+}));
+
+beforeEach(() => {
+ mocks.query.mockResolvedValue({ rows: [] });
+});
+
+describe("dashboard pagination counts", () => {
+ it.each([
+ {
+ name: "personal Caps",
+ folderTable: "videos",
+ load: () =>
+ CapsPage({
+ params: Promise.resolve({}),
+ searchParams: Promise.resolve({}),
+ }),
+ },
+ {
+ name: "organization Caps",
+ folderTable: "shared_videos",
+ load: () =>
+ SharedCapsPage({
+ params: Promise.resolve({ spaceId: "org-1" }),
+ searchParams: Promise.resolve({}),
+ }),
+ },
+ ])(
+ "counts the same root videos it lists for $name",
+ async ({ load, folderTable }) => {
+ await load();
+ const queries = mocks.query.mock.calls.map(([query]) => String(query));
+ const countQuery = queries.find((query) =>
+ query.startsWith("select count(*)"),
+ );
+ const listQuery = queries.find((query) => query.includes(" group by "));
+ const folderFilter = `\`${folderTable}\`.\`folderId\` is null`;
+
+ expect(listQuery).toContain(folderFilter);
+ expect(countQuery).toContain(folderFilter);
+ if (folderTable === "shared_videos") {
+ expect(countQuery).not.toContain("`videos`.`folderId`");
+ }
+ },
+ );
+});
diff --git a/apps/web/__tests__/unit/desktop-recording-source.test.ts b/apps/web/__tests__/unit/desktop-recording-source.test.ts
index 023f65f50..2172d261b 100644
--- a/apps/web/__tests__/unit/desktop-recording-source.test.ts
+++ b/apps/web/__tests__/unit/desktop-recording-source.test.ts
@@ -7,11 +7,33 @@ import { z } from "zod";
import type { DesktopRecordingSourceCheckpoint } from "@/lib/desktop-recording-source-checkpoint";
import type { RecordingVerification } from "@/lib/desktop-recording-verification";
-const mocks = vi.hoisted(() => ({ storage: vi.fn() }));
+const mocks = vi.hoisted(() => ({
+ storage: vi.fn(),
+ pageSize: undefined as number | undefined,
+ treePageSize: undefined as number | undefined,
+}));
vi.mock("@cap/web-backend/src/Storage/index", () => ({
Storage: { getAccessForVideo: mocks.storage },
}));
+vi.mock("@/lib/desktop-recording-source-checkpoint", async (importOriginal) => {
+ const actual =
+ await importOriginal<
+ typeof import("@/lib/desktop-recording-source-checkpoint")
+ >();
+ return {
+ ...actual,
+ get SOURCE_COMMIT_PAGE_SIZE() {
+ return mocks.pageSize ?? actual.SOURCE_COMMIT_PAGE_SIZE;
+ },
+ get SOURCE_COMMIT_AWS_PAGE_SIZE() {
+ return mocks.pageSize ?? actual.SOURCE_COMMIT_AWS_PAGE_SIZE;
+ },
+ get SOURCE_COMMIT_TREE_PAGE_SIZE() {
+ return mocks.treePageSize ?? actual.SOURCE_COMMIT_TREE_PAGE_SIZE;
+ },
+ };
+});
vi.mock("@/lib/workflow-runtime", async () => {
const { Effect } = await import("effect");
return { runWorkflowPromise: Effect.runPromise };
@@ -99,7 +121,7 @@ function recording(source: VideoRow["source"] = { type: "desktopSegments" }) {
return { id: "owned-video", ownerId: "owned-user", source } as VideoRow;
}
-function storageFixture(provider = "s3") {
+function storageFixture(provider = "s3", customBucket = false) {
const objects = new Map<string, StoredObject>();
const uploads = new Map<
string,
@@ -311,7 +333,12 @@ function storageFixture(provider = "s3") {
),
},
};
- mocks.storage.mockReturnValue(Effect.succeed([bucket]));
+ mocks.storage.mockReturnValue(
+ Effect.succeed([
+ bucket,
+ customBucket ? Option.some({ id: "custom-bucket" }) : Option.none(),
+ ]),
+ );
return { bucket, objects, uploads, seed, object };
}
@@ -422,6 +449,8 @@ function seedSegments(
beforeEach(() => {
vi.clearAllMocks();
+ mocks.pageSize = undefined;
+ mocks.treePageSize = undefined;
});
describe("recording source preparation during upload", () => {
@@ -1369,13 +1398,16 @@ describe("large desktop recording snapshots", () => {
).toBe(8 * 1024 ** 3);
});
- it.each(["s3", "googleDrive"])(
+ it.each([
+ ["s3", 128],
+ ["googleDrive", 16],
+ ] as const)(
"resumes a completed %s copy when the database checkpoint write was lost",
- async (provider) => {
+ async (provider, page) => {
const storage = storageFixture(provider);
seedSegments(
storage,
- Array.from({ length: 33 }, (_, index) => index + 1),
+ Array.from({ length: 129 }, (_, index) => index + 1),
false,
);
const video = recording();
@@ -1389,19 +1421,19 @@ describe("large desktop recording snapshots", () => {
checkpoint,
);
if (!("checkpoint" in copied)) throw new Error("Unexpected completion");
- expect(copied.checkpoint.cursor).toBe(16);
- expect(storage.bucket.copyObject).toHaveBeenCalledTimes(16);
+ expect(copied.checkpoint.cursor).toBe(page);
+ expect(storage.bucket.copyObject).toHaveBeenCalledTimes(page);
const replay = await advanceDesktopRecordingSourceCommit(
video,
checkpoint,
);
if (!("checkpoint" in replay)) throw new Error("Unexpected completion");
- expect(storage.bucket.copyObject).toHaveBeenCalledTimes(16);
+ expect(storage.bucket.copyObject).toHaveBeenCalledTimes(page);
expect(replay.checkpoint.snapshotId).toBe(checkpoint.snapshotId);
const source = await finishSnapshot(video, replay.checkpoint);
const urls = await buildDesktopRecordingSourceUrls(video, source);
- expect(urls.sourceObjects).toHaveLength(34);
- expect(storage.bucket.copyObject).toHaveBeenCalledTimes(34);
+ expect(urls.sourceObjects).toHaveLength(130);
+ expect(storage.bucket.copyObject).toHaveBeenCalledTimes(130);
expect(JSON.stringify(replay.checkpoint).length).toBeLessThan(5_000);
},
);
@@ -1627,6 +1659,313 @@ describe("large desktop recording snapshots", () => {
});
});
+describe("source commit page size", () => {
+ const segmentCount = 130;
+
+ function pagedFixture(provider = "s3", customBucket = false) {
+ const storage = storageFixture(provider, customBucket);
+ seedSegments(
+ storage,
+ Array.from({ length: segmentCount }, (_, index) => index + 1),
+ );
+ return storage;
+ }
+
+ async function advanceWithPageSize(
+ pageSize: number,
+ checkpoint: DesktopRecordingSourceCheckpoint,
+ done: (checkpoint: DesktopRecordingSourceCheckpoint) => boolean = () =>
+ false,
+ ) {
+ mocks.pageSize = pageSize;
+ let checkpoints = 0;
+ while (!done(checkpoint)) {
+ const result = await advanceDesktopRecordingSourceCommit(
+ recording(),
+ checkpoint,
+ );
+ checkpoints++;
+ if ("source" in result)
+ return { source: result.source, checkpoint, checkpoints };
+ checkpoint = result.checkpoint;
+ }
+ return { source: undefined, checkpoint, checkpoints };
+ }
+
+ function leafSizes(
+ storage: ReturnType<typeof storageFixture>,
+ inventoryKey: string,
+ ) {
+ const page = z.discriminatedUnion("type", [
+ z.object({ type: z.literal("leaf"), entries: z.array(z.unknown()) }),
+ z.object({
+ type: z.literal("branch"),
+ children: z.array(z.object({ key: z.string() })),
+ }),
+ ]);
+ const sizes = (key: string): number[] => {
+ const node = page.parse(JSON.parse(storage.object(key).body ?? ""));
+ return node.type === "leaf"
+ ? [node.entries.length]
+ : node.children.flatMap((child) => sizes(child.key));
+ };
+ return z
+ .object({ roots: z.array(z.object({ key: z.string() })) })
+ .parse(JSON.parse(storage.object(inventoryKey).body ?? ""))
+ .roots.flatMap((root) => sizes(root.key));
+ }
+
+ async function snapshotOutput(
+ storage: ReturnType<typeof storageFixture>,
+ source: Awaited<ReturnType<typeof commitDesktopRecordingSource>>,
+ ) {
+ const objects = readInventory(storage, source.inventoryKey).objects.map(
+ (object) => {
+ const copy = storage.object(object.key);
+ expect(copy.identity).toBe(object.objectIdentity);
+ const receiptKey = object.key.replace(
+ /\/(video|audio)\/\d+\.(mp4|m4s)$/,
+ "/receipt.json",
+ );
+ expect(JSON.parse(storage.object(receiptKey).body ?? "")).toEqual(
+ object,
+ );
+ return {
+ key: object.key.replace(/\/copies\/(\d+)\/[0-9a-f-]{36}\//, "/$1/"),
+ originalKey: object.originalKey,
+ originalIdentity: object.originalIdentity,
+ size: object.size,
+ track: object.track,
+ index: object.index,
+ body: copy.body,
+ };
+ },
+ );
+ const urls = await buildDesktopRecordingSourceUrls(recording(), source);
+ return {
+ source: {
+ kind: source.kind,
+ manifestSha256: source.manifestSha256,
+ requiredAudio: source.requiredAudio,
+ },
+ objects,
+ urls: {
+ sourceObjects: urls.sourceObjects.map(({ size }) => size),
+ videoSegments: urls.videoSegmentUrls.length,
+ audioSegments: urls.audioSegmentUrls.length,
+ videoInit: urls.videoInitUrl !== undefined,
+ audioInit: urls.audioInitUrl !== undefined,
+ },
+ copies: storage.bucket.copyObject.mock.calls.length,
+ };
+ }
+
+ async function referenceOutput(pageSize: number) {
+ const storage = pagedFixture();
+ const result = await advanceWithPageSize(pageSize, snapshotCheckpoint());
+ if (!result.source) throw new Error("Snapshot did not complete");
+ return {
+ output: await snapshotOutput(storage, result.source),
+ checkpoints: result.checkpoints,
+ leaves: leafSizes(storage, result.source.inventoryKey),
+ };
+ }
+
+ it("uses 128-entry pages for Cap's bucket and keeps 16 elsewhere, both within the leaf schema", async () => {
+ const actual = await vi.importActual<
+ typeof import("@/lib/desktop-recording-source-checkpoint")
+ >("@/lib/desktop-recording-source-checkpoint");
+ expect(actual.SOURCE_COMMIT_AWS_PAGE_SIZE).toBe(128);
+ expect(actual.SOURCE_COMMIT_PAGE_SIZE).toBe(16);
+ expect(actual.SOURCE_COMMIT_AWS_PAGE_SIZE).toBeLessThanOrEqual(
+ actual.SOURCE_COMMIT_TREE_PAGE_SIZE,
+ );
+ expect(actual.SOURCE_COMMIT_TREE_PAGE_SIZE).toBeLessThanOrEqual(
+ actual.SOURCE_COMMIT_MAX_TREE_PAGE_ENTRIES,
+ );
+ });
+
+ it("resumes a commit saved with larger tree pages using today's 128-entry writer, as after a rollback", async () => {
+ const reference = await referenceOutput(128);
+ const storage = pagedFixture();
+ mocks.treePageSize = 256;
+ const partial = await advanceWithPageSize(
+ 256,
+ snapshotCheckpoint(),
+ (checkpoint) => checkpoint.phase === "verify",
+ );
+ expect(partial.source).toBeUndefined();
+ expect(partial.checkpoint.phase).toBe("verify");
+ mocks.treePageSize = undefined;
+ const resumed = await advanceWithPageSize(128, partial.checkpoint);
+ if (!resumed.source) throw new Error("Resumed commit did not complete");
+ expect(await snapshotOutput(storage, resumed.source)).toEqual(
+ reference.output,
+ );
+ });
+
+ it.each([
+ [256, true],
+ [257, false],
+ ] as const)(
+ "reads a commit with %i-entry tree pages only within the read limit",
+ async (size, readable) => {
+ const storage = storageFixture("s3");
+ seedSegments(
+ storage,
+ Array.from({ length: 300 }, (_, index) => index + 1),
+ );
+ mocks.treePageSize = size;
+ const commit = advanceWithPageSize(size, snapshotCheckpoint());
+ if (readable) {
+ const result = await commit;
+ if (!result.source) throw new Error("Commit did not complete");
+ expect(
+ Math.max(...leafSizes(storage, result.source.inventoryKey)),
+ ).toBe(size);
+ } else {
+ await expect(commit).rejects.toMatchObject({ code: "source-invalid" });
+ }
+ },
+ );
+
+ it.each([
+ ["a custom S3-compatible bucket", "s3", true],
+ ["Google Drive", "googleDrive", false],
+ ] as const)(
+ "keeps 16-entry checkpoints for %s",
+ async (_, provider, customBucket) => {
+ pagedFixture(provider, customBucket);
+ mocks.pageSize = undefined;
+ let checkpoint = snapshotCheckpoint();
+ let checkpoints = 0;
+ while (true) {
+ const result = await advanceDesktopRecordingSourceCommit(
+ recording(),
+ checkpoint,
+ );
+ checkpoints++;
+ if ("source" in result) break;
+ checkpoint = result.checkpoint;
+ }
+ expect(checkpoints).toBe(2 + 3 * Math.ceil((segmentCount + 3) / 16));
+ },
+ );
+
+ it("commits more than 64 objects with the same inventory and receipts as 16-entry pages", async () => {
+ const legacy = await referenceOutput(16);
+ vi.clearAllMocks();
+ const current = await referenceOutput(64);
+ const objectCount = segmentCount + 3;
+ expect(current.output.objects).toHaveLength(objectCount);
+ expect(current.output).toEqual(legacy.output);
+ expect(current.output.copies).toBe(objectCount);
+ expect(legacy.checkpoints).toBe(2 + 3 * Math.ceil(objectCount / 16));
+ expect(current.checkpoints).toBe(2 + 3 * Math.ceil(objectCount / 64));
+ expect(legacy.leaves.every((size) => size <= 16)).toBe(true);
+ expect(current.leaves).toEqual([64, 64, objectCount - 128]);
+ });
+
+ for (const [from, to] of [
+ [16, 64],
+ [64, 16],
+ ] as const) {
+ it.each(["enumerate", "copy", "verify"] as const)(
+ `resumes a %s checkpoint saved with ${from}-entry pages after switching to ${to}`,
+ async (phase) => {
+ const reference = await referenceOutput(64);
+ vi.clearAllMocks();
+ const storage = pagedFixture();
+ const saved = await advanceWithPageSize(
+ from,
+ snapshotCheckpoint(),
+ (checkpoint) =>
+ checkpoint.phase === phase && checkpoint.cursor === 64,
+ );
+ expect(saved.source).toBeUndefined();
+ const next = await advanceDesktopRecordingSourceCommit(
+ recording(),
+ saved.checkpoint,
+ );
+ if (!("checkpoint" in next)) throw new Error("Unexpected completion");
+ expect(next.checkpoint).toMatchObject({
+ phase,
+ cursor: 64 + from,
+ plan: saved.checkpoint.plan,
+ snapshotId: saved.checkpoint.snapshotId,
+ });
+ const resumed = await advanceWithPageSize(to, next.checkpoint);
+ if (!resumed.source) throw new Error("Snapshot did not complete");
+ expect(await snapshotOutput(storage, resumed.source)).toEqual(
+ reference.output,
+ );
+ const leaves = leafSizes(storage, resumed.source.inventoryKey);
+ expect(leaves.reduce((total, size) => total + size, 0)).toBe(
+ segmentCount + 3,
+ );
+ if (phase !== "verify") expect(new Set(leaves).size).toBeGreaterThan(1);
+ },
+ );
+ }
+
+ it.each([
+ ["Cap's AWS bucket", "s3", false, 128],
+ ["a custom S3-compatible bucket", "s3", true, 8],
+ ["Google Drive", "googleDrive", false, 8],
+ ] as const)(
+ "bounds parallel storage requests for %s",
+ async (_, provider, customBucket, expected) => {
+ const storage = pagedFixture(provider, customBucket);
+ const active = { head: 0, copy: 0 };
+ const peak = { head: 0, copy: 0 };
+ const track = <A, E>(
+ name: "head" | "copy",
+ effect: Effect.Effect<A, E>,
+ ) =>
+ Effect.suspend(() => {
+ active[name]++;
+ peak[name] = Math.max(peak[name], active[name]);
+ return Effect.sleep("1 millis").pipe(
+ Effect.zipRight(effect),
+ Effect.ensuring(
+ Effect.sync(() => {
+ active[name]--;
+ }),
+ ),
+ );
+ });
+ const head = storage.bucket.headObject.getMockImplementation();
+ const copy = storage.bucket.copyObject.getMockImplementation();
+ if (!head || !copy) throw new Error("Missing storage fixture");
+ const enumerate = await advanceToPhase(
+ recording(),
+ snapshotCheckpoint(),
+ "enumerate",
+ );
+ storage.bucket.headObject.mockImplementation((key) =>
+ track("head", head(key)),
+ );
+ storage.bucket.copyObject.mockImplementation((source, target, options) =>
+ track("copy", copy(source, target, options)),
+ );
+ const planned = await advanceDesktopRecordingSourceCommit(
+ recording(),
+ enumerate,
+ );
+ if (!("checkpoint" in planned)) throw new Error("Unexpected completion");
+ expect(peak.head).toBe(expected);
+ const source = await finishSnapshot(recording(), planned.checkpoint);
+ expect(peak.copy).toBe(expected);
+ // Each copy/verify worker checks the copy and its original together, so the
+ // bound is on objects in flight; requests peak at two per object.
+ expect(peak.head).toBeLessThanOrEqual(expected * 2);
+ expect(readInventory(storage, source.inventoryKey).objects).toHaveLength(
+ segmentCount + 3,
+ );
+ },
+ );
+});
+
describe("committed source access", () => {
it("classifies malformed committed inventory JSON as a permanent source error", async () => {
const storage = storageFixture();
diff --git a/apps/web/__tests__/unit/developer-documentation-videos-page.test.ts b/apps/web/__tests__/unit/developer-documentation-videos-page.test.ts
index 0f2815f66..616de9caf 100644
--- a/apps/web/__tests__/unit/developer-documentation-videos-page.test.ts
+++ b/apps/web/__tests__/unit/developer-documentation-videos-page.test.ts
@@ -59,6 +59,13 @@ describe("DeveloperDocumentationVideosPage component content", () => {
expect(componentSource).toContain("comparisonTable");
});
+ it("describes the license split in the comparison table", () => {
+ expect(componentSource).toContain(
+ '{ text: "Yes — AGPLv3 and MIT crates", status: "positive" }',
+ );
+ expect(componentSource).not.toContain('{ text: "Yes — MIT"');
+ });
+
it("includes recording modes section", () => {
expect(componentSource).toContain("recordingModes");
});
diff --git a/apps/web/__tests__/unit/finalize-desktop-recording.test.ts b/apps/web/__tests__/unit/finalize-desktop-recording.test.ts
index b62ab316f..dcebcc4ae 100644
--- a/apps/web/__tests__/unit/finalize-desktop-recording.test.ts
+++ b/apps/web/__tests__/unit/finalize-desktop-recording.test.ts
@@ -1,5 +1,5 @@
import type { User, Video } from "@cap/web-domain";
-import { Effect } from "effect";
+import { Effect, Option } from "effect";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type {
DesktopRecordingAttempt,
@@ -33,6 +33,8 @@ const mocks = vi.hoisted(() => ({
put: vi.fn(),
databaseRows: vi.fn(),
reserveBudget: vi.fn(),
+ storage: vi.fn(),
+ multipartCreate: vi.fn(),
}));
vi.mock("@/lib/media-processing-budget", () => ({
reserveMediaProcessingBudget: mocks.reserveBudget,
@@ -97,20 +99,13 @@ vi.mock("@cap/web-backend/src/Storage/index", async () => {
const { Effect } = await import("effect");
return {
Storage: {
- getAccessForVideo: () =>
- Effect.succeed([
- {
- provider: "test",
- getInternalSignedObjectUrl: (key: string) =>
- Effect.succeed(`https://storage.test/${key}`),
- getInternalPresignedPutUrl: mocks.put,
- },
- ]),
+ getAccessForVideo: () => Effect.sync(mocks.storage),
},
};
});
import {
+ commitAndStartDesktopRecordingAttempt,
commitDesktopRecordingAttempt,
finalizeDesktopRecordingWorkflow,
pollDesktopRecordingAttempt,
@@ -150,6 +145,19 @@ const fixture: DesktopRecordingAttempt = {
};
let current: DesktopRecordingJob | null;
+function storageAccess(provider: string, customBucket = false) {
+ return [
+ {
+ provider,
+ getInternalSignedObjectUrl: (key: string) =>
+ Effect.succeed(`https://storage.test/${key}`),
+ getInternalPresignedPutUrl: mocks.put,
+ multipart: { create: mocks.multipartCreate },
+ },
+ customBucket ? Option.some({ id: "bucket" }) : Option.none(),
+ ];
+}
+
function withCurrent(update: Partial<DesktopRecordingJob>) {
if (!current) throw new Error("Fixture job disappeared");
current = { ...current, ...update };
@@ -157,6 +165,10 @@ function withCurrent(update: Partial<DesktopRecordingJob>) {
beforeEach(() => {
mocks.reserveBudget.mockResolvedValue(100_000_000);
+ mocks.storage.mockImplementation(() => storageAccess("test"));
+ mocks.multipartCreate.mockImplementation(() =>
+ Effect.succeed({ UploadId: "upload" }),
+ );
mocks.databaseRows.mockResolvedValue([
{ id: "video", ownerId: "user", source: { type: "desktopSegments" } },
]);
@@ -317,6 +329,30 @@ describe("durable post-publication transcription enqueue", () => {
expect(mocks.claim).not.toHaveBeenCalled();
});
+ it("finishes after repeated transcription queue failures", async () => {
+ withCurrent({ state: "verified" });
+ mocks.transcribe.mockResolvedValue({
+ success: false,
+ message: "Queue unavailable",
+ });
+ mocks.sleep.mockImplementation(async () => {
+ if (mocks.transcribe.mock.calls.length >= 12) {
+ throw new Error("Transcription retry limit exceeded");
+ }
+ });
+
+ await expect(
+ finalizeDesktopRecordingWorkflow({
+ videoId,
+ userId,
+ generation: fixture.generation,
+ }),
+ ).resolves.toEqual({ success: true });
+ expect(mocks.transcribe).toHaveBeenCalledTimes(12);
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ expect(mocks.claim).not.toHaveBeenCalled();
+ });
+
it.each(["unconfigured", "deleted"])(
"skips %s transcription without retrying forever",
async (reason) => {
@@ -555,6 +591,144 @@ describe("bounded source commitment steps", () => {
expect(mocks.persist).not.toHaveBeenCalled();
});
+ it("logs the timing of every commit page without storage keys", async () => {
+ let clock = 0;
+ const now = vi.spyOn(performance, "now").mockImplementation(() => clock);
+ mocks.commitSource.mockImplementation(
+ async (_video, checkpoint, _verification, onProgress) => {
+ clock += 5;
+ await onProgress();
+ clock += 100;
+ if (checkpoint.revision === 1) return { source };
+ return {
+ checkpoint: {
+ ...checkpoint,
+ phase: "copy",
+ revision: checkpoint.revision + 1,
+ },
+ };
+ },
+ );
+ mocks.heartbeat.mockImplementation(async () => {
+ clock += 7;
+ return true;
+ });
+ mocks.saveCheckpoint.mockImplementation(async (_attempt, checkpoint) => {
+ clock += 20;
+ withCurrent({ output: checkpoint });
+ return true;
+ });
+ mocks.persist.mockImplementation(async (_attempt, savedSource) => {
+ clock += 30;
+ withCurrent({ source: savedSource, state: "processing" });
+ return true;
+ });
+ const info = vi.spyOn(console, "info").mockImplementation(() => {});
+ try {
+ expect(await commitDesktopRecordingAttempt(fixture)).toBe("ready");
+ const events = info.mock.calls
+ .map(([line]) => JSON.parse(String(line)))
+ .filter((event) => event.event === "desktop_source_commit_advance");
+ expect(events).toEqual([
+ {
+ event: "desktop_source_commit_advance",
+ videoId: "video",
+ attemptId: "attempt",
+ phase: "plan",
+ next: "copy",
+ outcome: "progress",
+ loadMs: 0,
+ heartbeatMs: 7,
+ advanceMs: 112,
+ persistMs: 20,
+ },
+ {
+ event: "desktop_source_commit_advance",
+ videoId: "video",
+ attemptId: "attempt",
+ phase: "copy",
+ next: "committed",
+ outcome: "ready",
+ loadMs: 0,
+ heartbeatMs: 7,
+ advanceMs: 112,
+ persistMs: 30,
+ },
+ ]);
+ expect(JSON.stringify(events)).not.toContain(source.inventoryKey);
+ } finally {
+ info.mockRestore();
+ now.mockRestore();
+ }
+ });
+
+ it("dispatches in the same step when the commit finishes inside its batch budget", async () => {
+ withCurrent({ source: null });
+ mocks.commitSource.mockResolvedValue({ source });
+ await expect(
+ commitAndStartDesktopRecordingAttempt(fixture),
+ ).resolves.toEqual({
+ committed: "ready",
+ dispatched: true,
+ started: "remote-job",
+ });
+ expect(mocks.persist).toHaveBeenCalledOnce();
+ expect(mocks.fetch).toHaveBeenCalledOnce();
+ });
+
+ it("returns the accepted job without committing or dispatching again when the combined step is retried", async () => {
+ withCurrent({ source: null });
+ mocks.commitSource.mockResolvedValue({ source });
+ mocks.attach.mockImplementation(
+ async ({ remoteJobId }: { remoteJobId: string }) => {
+ withCurrent({ remoteJobId });
+ return true;
+ },
+ );
+ await expect(
+ commitAndStartDesktopRecordingAttempt(fixture),
+ ).resolves.toEqual({
+ committed: "ready",
+ dispatched: true,
+ started: "remote-job",
+ });
+ await expect(
+ commitAndStartDesktopRecordingAttempt(fixture),
+ ).resolves.toEqual({
+ committed: "ready",
+ dispatched: true,
+ started: "remote-job",
+ });
+ expect(mocks.commitSource).toHaveBeenCalledOnce();
+ expect(mocks.persist).toHaveBeenCalledOnce();
+ expect(mocks.fetch).toHaveBeenCalledOnce();
+ expect(mocks.sourceUrls).toHaveBeenCalledOnce();
+ });
+
+ it("leaves dispatch to its own step after a slow commit", async () => {
+ withCurrent({ source: null });
+ mocks.commitSource.mockImplementation(async () => {
+ vi.setSystemTime(Date.now() + 15_000);
+ return { source };
+ });
+ await expect(
+ commitAndStartDesktopRecordingAttempt(fixture),
+ ).resolves.toEqual({ committed: "ready" });
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ });
+
+ it("does not dispatch while the source commit is still in progress", async () => {
+ withCurrent({ source: null });
+ mocks.commitSource.mockImplementation(async (_video, checkpoint) => {
+ vi.setSystemTime(Date.now() + 15_000);
+ return advanceCheckpoint(_video, checkpoint);
+ });
+ await expect(
+ commitAndStartDesktopRecordingAttempt(fixture),
+ ).resolves.toEqual({ committed: "progress" });
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ });
+
it("includes checkpoint persistence latency in the batch budget", async () => {
mocks.commitSource.mockImplementation(advanceCheckpoint);
mocks.saveCheckpoint.mockImplementation(async (_attempt, checkpoint) => {
@@ -867,6 +1041,57 @@ describe("source commitment and media request compatibility", () => {
);
});
+ function withSourceBytes(size: number) {
+ mocks.sourceUrls.mockResolvedValue({
+ videoInitUrl: "https://source.test/init.mp4",
+ videoSegmentUrls: ["https://source.test/segment.m4s"],
+ sourceObjects: [
+ {
+ url: "https://source.test/segment.m4s",
+ objectIdentity: '"segment"',
+ size,
+ },
+ ],
+ });
+ }
+
+ it("uploads a small Cap storage output with one PUT instead of multipart", async () => {
+ mocks.storage.mockImplementation(() => storageAccess("s3"));
+ withSourceBytes(64 * 1024 * 1024);
+ await startDesktopRecordingJob(fixture);
+ const body = JSON.parse(mocks.fetch.mock.calls[0]?.[1]?.body ?? "{}");
+ expect(body.outputUpload).toEqual({
+ type: "put",
+ url: "https://storage.test/user/video/.recording/outputs/generation/attempt.mp4",
+ ifNoneMatch: "*",
+ });
+ expect(mocks.multipartCreate).not.toHaveBeenCalled();
+ });
+
+ it("keeps multipart output uploads for recordings larger than one part", async () => {
+ mocks.storage.mockImplementation(() => storageAccess("s3"));
+ withSourceBytes(64 * 1024 * 1024 + 1);
+ await startDesktopRecordingJob(fixture);
+ const body = JSON.parse(mocks.fetch.mock.calls[0]?.[1]?.body ?? "{}");
+ expect(body.outputUpload).toMatchObject({
+ type: "multipart",
+ uploadId: "upload",
+ partSize: 64 * 1024 * 1024,
+ signPartUrl:
+ "https://cap.test/api/webhooks/media-server/multipart/sign-part",
+ });
+ expect(mocks.multipartCreate).toHaveBeenCalledOnce();
+ });
+
+ it("keeps multipart output uploads for small recordings in a custom bucket", async () => {
+ mocks.storage.mockImplementation(() => storageAccess("s3", true));
+ withSourceBytes(1024);
+ await startDesktopRecordingJob(fixture);
+ const body = JSON.parse(mocks.fetch.mock.calls[0]?.[1]?.body ?? "{}");
+ expect(body.outputUpload).toMatchObject({ type: "multipart" });
+ expect(mocks.multipartCreate).toHaveBeenCalledOnce();
+ });
+
it("reuses a recorded remote job after a step replay instead of submitting another mux", async () => {
withCurrent({ remoteJobId: "existing-worker" });
expect(await startDesktopRecordingJob(fixture)).toBe("existing-worker");
@@ -1091,6 +1316,40 @@ describe("workflow retry lifetime", () => {
);
});
+ it.each([
+ ["uses the generation it was started with", "generation", 0],
+ ["looks the job up when started without a generation", undefined, 1],
+ ])("%s", async (_name, generation, lookups) => {
+ withCurrent({
+ state: "queued",
+ leaseExpiresAt: null,
+ attemptId: null,
+ attemptCount: 0,
+ });
+ mocks.env.mockReturnValue({ WEB_URL: "https://self-hosted.test" });
+ const result = await finalizeDesktopRecordingWorkflow({
+ videoId,
+ userId,
+ generation,
+ });
+ expect(result).toEqual({
+ success: false,
+ reason: "media-server-unconfigured",
+ });
+ expect(mocks.ensure).toHaveBeenCalledTimes(lookups);
+ expect(mocks.defer).toHaveBeenCalledOnce();
+ });
+
+ it("reports a missing media server from the dispatch step without dispatching", async () => {
+ mocks.env.mockReturnValue({ WEB_URL: "https://self-hosted.test" });
+ await expect(startDesktopRecordingJob(fixture)).resolves.toEqual({
+ status: "media-server-unconfigured",
+ });
+ expect(mocks.defer).toHaveBeenCalledOnce();
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ expect(mocks.sourceUrls).not.toHaveBeenCalled();
+ });
+
it("preserves the self-hosted playback fallback without issuing a verified cleanup receipt", async () => {
withCurrent({
state: "queued",
diff --git a/apps/web/__tests__/unit/folder-access.test.ts b/apps/web/__tests__/unit/folder-access.test.ts
new file mode 100644
index 000000000..eff4d9f69
--- /dev/null
+++ b/apps/web/__tests__/unit/folder-access.test.ts
@@ -0,0 +1,100 @@
+import { Organisation, Space, User } from "@cap/web-domain";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ organizationAccess: vi.fn(),
+ spaceAccess: vi.fn(),
+}));
+
+vi.mock("@/actions/organization/authorization", () => ({
+ getOrganizationAccess: mocks.organizationAccess,
+}));
+vi.mock("@/actions/organization/space-authorization", () => ({
+ getSpaceAccess: mocks.spaceAccess,
+}));
+
+import { canViewFolder } from "@/actions/folders/folder-access";
+
+const userId = User.UserId.make("user-1");
+const otherUserId = User.UserId.make("user-2");
+const organizationId = Organisation.OrganisationId.make("org-1");
+const spaceId = Space.SpaceId.make("space-1");
+
+describe("canViewFolder", () => {
+ beforeEach(() => {
+ mocks.organizationAccess.mockReset();
+ mocks.spaceAccess.mockReset();
+ });
+
+ it("limits personal folders to their creator", async () => {
+ const folder = { organizationId, spaceId: null, createdById: userId };
+
+ await expect(canViewFolder(userId, folder)).resolves.toBe(true);
+ await expect(canViewFolder(otherUserId, folder)).resolves.toBe(false);
+ expect(mocks.organizationAccess).not.toHaveBeenCalled();
+ expect(mocks.spaceAccess).not.toHaveBeenCalled();
+ });
+
+ it("allows organization members into organization folders", async () => {
+ const folder = {
+ organizationId,
+ spaceId: organizationId,
+ createdById: otherUserId,
+ };
+
+ mocks.organizationAccess.mockResolvedValueOnce({ role: "member" });
+ await expect(canViewFolder(userId, folder)).resolves.toBe(true);
+
+ mocks.organizationAccess.mockResolvedValueOnce(null);
+ await expect(canViewFolder(userId, folder)).resolves.toBe(false);
+ expect(mocks.organizationAccess).toHaveBeenCalledWith(
+ userId,
+ organizationId,
+ );
+ expect(mocks.spaceAccess).not.toHaveBeenCalled();
+ });
+
+ it("requires an organization or space role for space folders", async () => {
+ const folder = { organizationId, spaceId, createdById: otherUserId };
+
+ mocks.spaceAccess.mockResolvedValueOnce({
+ organizationId,
+ organizationRole: null,
+ spaceRole: "member",
+ });
+ await expect(canViewFolder(userId, folder)).resolves.toBe(true);
+
+ mocks.spaceAccess.mockResolvedValueOnce({
+ organizationId,
+ organizationRole: "admin",
+ spaceRole: null,
+ });
+ await expect(canViewFolder(userId, folder)).resolves.toBe(true);
+
+ mocks.spaceAccess.mockResolvedValueOnce({
+ organizationId,
+ organizationRole: null,
+ spaceRole: null,
+ });
+ await expect(canViewFolder(userId, folder)).resolves.toBe(false);
+
+ mocks.spaceAccess.mockResolvedValueOnce(null);
+ await expect(canViewFolder(userId, folder)).resolves.toBe(false);
+ });
+
+ it("rejects space folders whose space belongs to another organization", async () => {
+ mocks.spaceAccess.mockResolvedValueOnce({
+ organizationId: Organisation.OrganisationId.make("org-2"),
+ organizationRole: "owner",
+ spaceRole: "admin",
+ });
+
+ await expect(
+ canViewFolder(userId, {
+ organizationId,
+ spaceId,
+ createdById: otherUserId,
+ }),
+ ).resolves.toBe(false);
+ });
+});
diff --git a/apps/web/__tests__/unit/hipaa-compliant-screen-recording-page.test.ts b/apps/web/__tests__/unit/hipaa-compliant-screen-recording-page.test.ts
index a75617280..5d22c1460 100644
--- a/apps/web/__tests__/unit/hipaa-compliant-screen-recording-page.test.ts
+++ b/apps/web/__tests__/unit/hipaa-compliant-screen-recording-page.test.ts
@@ -59,6 +59,13 @@ describe("HipaaCompliantScreenRecordingPage component content", () => {
expect(componentSource).toContain("comparisonTable");
});
+ it("describes the license split in the comparison table", () => {
+ expect(componentSource).toContain(
+ '{ text: "Yes — AGPLv3 and MIT crates", status: "positive" }',
+ );
+ expect(componentSource).not.toContain('{ text: "Yes — MIT"');
+ });
+
it("includes recording modes section", () => {
expect(componentSource).toContain("recordingModes");
});
@@ -107,7 +114,8 @@ describe("HipaaCompliantScreenRecordingPage FAQ schema", () => {
{
question:
"Is Cap open source and auditable for HIPAA vendor assessments?",
- answer: "Yes. Cap is MIT-licensed and fully open source on GitHub.",
+ answer:
+ "Yes. Most Cap code is under AGPLv3, with specified Rust crates under MIT. The source is available on GitHub.",
},
{
question: "Can I disable AI transcription in Cap for HIPAA compliance?",
diff --git a/apps/web/__tests__/unit/homepage-seo.test.ts b/apps/web/__tests__/unit/homepage-seo.test.ts
index aeaadf3cc..82e6735a8 100644
--- a/apps/web/__tests__/unit/homepage-seo.test.ts
+++ b/apps/web/__tests__/unit/homepage-seo.test.ts
@@ -3,7 +3,15 @@ import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
import robots from "@/app/robots";
-import { homePageMetadata } from "@/components/pages/HomeTwo/metadata";
+import { DISCORD_URL } from "@/components/pages/about/content";
+import {
+ DiscordLinkPreview,
+ discordLinkPreview,
+} from "@/components/pages/HomeTwo/DiscordLinkPreview";
+import {
+ homePageMetadata,
+ homepageOgImageUrl,
+} from "@/components/pages/HomeTwo/metadata";
import { HomeTwoSchema } from "@/components/pages/HomeTwo/Schema";
import { homepageSchema, homepageSeo } from "@/components/pages/HomeTwo/seo";
import { PRICING } from "@/data/pricing";
@@ -81,4 +89,31 @@ describe("homepage SEO", () => {
expect(JSON.parse(json ?? "")).toEqual(homepageSchema);
expect(homePageMetadata.description).toBe(homepageSeo.description);
});
+
+ it("renders a Discord component link preview with a community button", () => {
+ const html = renderToStaticMarkup(createElement(DiscordLinkPreview));
+ const json = html.match(
+ /<script id="discord:component-embed" type="application\/json">(.*?)<\/script>/,
+ )?.[1];
+
+ expect(JSON.parse(json ?? "")).toEqual(discordLinkPreview);
+ expect(JSON.stringify(discordLinkPreview)).toContain("Join our Discord");
+ expect(JSON.stringify(discordLinkPreview)).toContain(DISCORD_URL);
+ expect(JSON.stringify(discordLinkPreview)).toContain(homepageOgImageUrl);
+ expect(JSON.stringify(discordLinkPreview)).not.toContain(
+ "apple-touch-icon",
+ );
+ expect(
+ discordLinkPreview.component.components[0].components[0].content,
+ ).toBe(`# [Cap](${homepageSeo.url})\n${homepageSeo.description}`);
+ expect(discordLinkPreview.component.components[0]).toMatchObject({
+ type: 9,
+ accessory: {
+ type: 2,
+ style: 5,
+ label: "Join our Discord",
+ url: DISCORD_URL,
+ },
+ });
+ });
});
diff --git a/apps/web/__tests__/unit/import-media-progress.test.ts b/apps/web/__tests__/unit/import-media-progress.test.ts
new file mode 100644
index 000000000..ed0318d1d
--- /dev/null
+++ b/apps/web/__tests__/unit/import-media-progress.test.ts
@@ -0,0 +1,138 @@
+import { Organisation, Video } from "@cap/web-domain";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ create: vi.fn(),
+ trigger: vi.fn(),
+ progress: vi.fn(),
+}));
+
+vi.mock("@/actions/video/create-for-processing", () => ({
+ createVideoForServerProcessing: mocks.create,
+}));
+vi.mock("@/actions/video/trigger-processing", () => ({
+ triggerVideoProcessing: mocks.trigger,
+}));
+vi.mock("@/actions/video/upload", () => ({
+ createVideoAndGetUploadUrl: vi.fn(),
+}));
+vi.mock("@/app/(org)/dashboard/caps/components/sendProgressUpdate", () => ({
+ sendProgressUpdate: mocks.progress,
+}));
+vi.mock("@remotion/media-parser", () => ({
+ parseMedia: async () => ({ durationInSeconds: 60, dimensions: null }),
+}));
+vi.mock("sonner", () => ({
+ toast: { success: vi.fn(), error: vi.fn() },
+}));
+
+import { importMediaFile } from "@/app/(org)/dashboard/import/import-media";
+
+class MockXMLHttpRequest {
+ static current: MockXMLHttpRequest | undefined;
+ upload = {
+ onprogress: null as
+ | ((event: {
+ lengthComputable: boolean;
+ loaded: number;
+ total: number;
+ }) => void)
+ | null,
+ };
+ onload: (() => void) | null = null;
+ onerror: (() => void) | null = null;
+ status = 0;
+
+ constructor() {
+ MockXMLHttpRequest.current = this;
+ }
+
+ open() {}
+ setRequestHeader() {}
+ send() {}
+
+ progress(loaded: number, total: number) {
+ this.upload.onprogress?.({ lengthComputable: true, loaded, total });
+ }
+}
+
+const videoId = Video.VideoId.make("video-123");
+const total = 8 * 1024 ** 3;
+
+describe("dashboard video import progress", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ vi.stubGlobal("XMLHttpRequest", MockXMLHttpRequest);
+ MockXMLHttpRequest.current = undefined;
+ mocks.create.mockResolvedValue({
+ id: videoId,
+ rawFileKey: "user-123/video-123/raw-upload.mp4",
+ bucketId: null,
+ uploadTarget: {
+ type: "put",
+ url: "https://storage.example/upload",
+ headers: {},
+ },
+ });
+ mocks.progress.mockResolvedValue(undefined);
+ mocks.trigger.mockResolvedValue({ success: true });
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.useRealTimers();
+ });
+
+ it.each(["success", "failure"] as const)(
+ "reports steady uploads periodically and cleans up after %s",
+ async (outcome) => {
+ const file = new File(["video"], "clip.mp4", { type: "video/mp4" });
+ Object.defineProperty(file, "size", { value: total });
+ const importing = importMediaFile({
+ file,
+ orgId: Organisation.OrganisationId.make("org-123"),
+ setUploadStatus: vi.fn(),
+ });
+ const request = await vi.waitFor(() => {
+ if (!MockXMLHttpRequest.current) throw new Error("Upload not started");
+ return MockXMLHttpRequest.current;
+ });
+
+ request.progress(total / 16, total);
+ for (let tick = 1; tick <= 8; tick++) {
+ await vi.advanceTimersByTimeAsync(500);
+ request.progress(((tick + 1) * total) / 16, total);
+ }
+
+ expect(mocks.progress.mock.calls).toEqual([
+ [videoId, total / 4, total],
+ [videoId, total / 2, total],
+ ]);
+ expect(mocks.trigger).not.toHaveBeenCalled();
+
+ if (outcome === "success") {
+ request.progress(total, total);
+ await vi.advanceTimersByTimeAsync(2000);
+ expect(mocks.progress).toHaveBeenCalledTimes(2);
+ expect(mocks.trigger).not.toHaveBeenCalled();
+ request.status = 200;
+ request.onload?.();
+ } else {
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ request.onerror?.();
+ }
+
+ expect(await importing).toBe(outcome === "success");
+ expect(mocks.trigger).toHaveBeenCalledTimes(
+ outcome === "success" ? 1 : 0,
+ );
+ if (outcome === "success") {
+ expect(mocks.progress).toHaveBeenLastCalledWith(videoId, total, total);
+ }
+ const updates = mocks.progress.mock.calls.length;
+ await vi.advanceTimersByTimeAsync(5000);
+ expect(mocks.progress).toHaveBeenCalledTimes(updates);
+ expect(vi.getTimerCount()).toBe(0);
+ },
+ );
+});
diff --git a/apps/web/__tests__/unit/loom-csv.test.ts b/apps/web/__tests__/unit/loom-csv.test.ts
index 90d8db8f1..0c50abfc8 100644
--- a/apps/web/__tests__/unit/loom-csv.test.ts
+++ b/apps/web/__tests__/unit/loom-csv.test.ts
@@ -1,5 +1,12 @@
import { describe, expect, it } from "vitest";
-import { parseConciergeLoomCsv } from "@/lib/loom-csv";
+import {
+ formatLoomCsvCell,
+ isLoomCsvResultsFile,
+ LOOM_CSV_RESULTS_HEADERS,
+ parseConciergeLoomCsv,
+ parseLoomCsvRecords,
+ readLoomCsvCell,
+} from "@/lib/loom-csv";
describe("concierge Loom CSV", () => {
it("keeps quoted workspace names and source row numbers", () => {
@@ -25,3 +32,74 @@ describe("concierge Loom CSV", () => {
);
});
});
+
+describe("Loom CSV results cells", () => {
+ it("keeps plain values unchanged", () => {
+ expect(formatLoomCsvCell("[email protected]")).toBe("[email protected]");
+ expect(
+ formatLoomCsvCell("https://www.loom.com/share/0123456789abcdef"),
+ ).toBe("https://www.loom.com/share/0123456789abcdef");
+ });
+
+ it("quotes delimiters and doubles embedded quotes", () => {
+ expect(formatLoomCsvCell("Sales, Europe")).toBe('"Sales, Europe"');
+ expect(formatLoomCsvCell('The "A" team')).toBe('"The ""A"" team"');
+ });
+
+ it.each([
+ ["=HYPERLINK(1)", "'=HYPERLINK(1)"],
+ ["+1", "'+1"],
+ ["-1", "'-1"],
+ ["@SUM(A1)", "'@SUM(A1)"],
+ ["\tTab", "'\tTab"],
+ ])("neutralizes the formula prefix in %j", (value, expected) => {
+ expect(formatLoomCsvCell(value)).toBe(expected);
+ });
+
+ it("neutralizes formulas that also need quoting", () => {
+ expect(formatLoomCsvCell("=1,2")).toBe(`"'=1,2"`);
+ expect(formatLoomCsvCell("\r=1")).toBe(`"'\r=1"`);
+ });
+});
+
+describe("Loom CSV results round trip", () => {
+ it("restores every value when a results CSV is imported again", () => {
+ const values = [
+ "@Sales",
+ "-Ops, EU",
+ "=1",
+ "'@Sales",
+ "''=1",
+ "'Sales",
+ "O'Brien",
+ "Sales",
+ ];
+ const [cells] = parseLoomCsvRecords(
+ `${values.map(formatLoomCsvCell).join(",")}\n`,
+ );
+ expect(cells?.map(readLoomCsvCell)).toEqual(values);
+ });
+
+ it("leaves apostrophes alone when no formula character follows", () => {
+ expect(readLoomCsvCell("'Sales")).toBe("'Sales");
+ expect(readLoomCsvCell("O'Brien")).toBe("O'Brien");
+ });
+
+ it("recognizes the results CSV layout regardless of column order or case", () => {
+ expect(isLoomCsvResultsFile([...LOOM_CSV_RESULTS_HEADERS])).toBe(true);
+ expect(
+ isLoomCsvResultsFile([
+ "Status",
+ "Error",
+ "Row",
+ "User_Email",
+ "Space_Name",
+ "Loom_Video_URL",
+ ]),
+ ).toBe(true);
+ expect(
+ isLoomCsvResultsFile(["loom_video_url", "user_email", "space_name"]),
+ ).toBe(false);
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-download-url.test.ts b/apps/web/__tests__/unit/loom-download-url.test.ts
new file mode 100644
index 000000000..a67860259
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-download-url.test.ts
@@ -0,0 +1,229 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import {
+ getLoomDownloadUrl,
+ getReusableLoomDownloadUrl,
+ LoomDownloadTemporaryError,
+} from "@/lib/loom-download-url";
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+describe("getLoomDownloadUrl", () => {
+ it("honors rate limits without making more endpoint requests", async () => {
+ const fetch = vi.fn().mockResolvedValue(
+ new Response(null, {
+ status: 429,
+ headers: { "Retry-After": "120" },
+ }),
+ );
+ vi.stubGlobal("fetch", fetch);
+ await expect(getLoomDownloadUrl("video")).rejects.toMatchObject({
+ retryAfterMs: 120_000,
+ });
+ expect(fetch).toHaveBeenCalledOnce();
+ });
+
+ it("treats network and server failures as temporary", async () => {
+ const fetch = vi.fn().mockRejectedValueOnce(new Error("Timeout"));
+ vi.stubGlobal("fetch", fetch);
+ await expect(getLoomDownloadUrl("video")).rejects.toBeInstanceOf(
+ LoomDownloadTemporaryError,
+ );
+ fetch.mockResolvedValueOnce(new Response(null, { status: 503 }));
+ await expect(getLoomDownloadUrl("video")).rejects.toMatchObject({
+ retryAfterMs: 60_000,
+ });
+ });
+
+ it("does not retry a missing or inaccessible video", async () => {
+ const fetch = vi
+ .fn()
+ .mockResolvedValue(new Response(null, { status: 204 }));
+ vi.stubGlobal("fetch", fetch);
+ await expect(getLoomDownloadUrl("video")).resolves.toBeNull();
+ expect(fetch).toHaveBeenCalledTimes(5);
+ });
+
+ it("uses an available stream when a later endpoint is rate limited", async () => {
+ const fetch = vi
+ .fn()
+ .mockResolvedValueOnce(
+ Response.json({
+ url: "https://cdn.loom.com/video.m3u8?Signature=token",
+ }),
+ )
+ .mockResolvedValueOnce(new Response(null, { status: 429 }));
+ vi.stubGlobal("fetch", fetch);
+ await expect(getLoomDownloadUrl("video")).resolves.toBe(
+ "https://cdn.loom.com/video.m3u8?Signature=token",
+ );
+ expect(fetch).toHaveBeenCalledTimes(2);
+ });
+
+ it("prefers a direct file over an earlier stream", async () => {
+ const fetch = vi
+ .fn()
+ .mockResolvedValueOnce(
+ Response.json({ url: "https://cdn.loom.com/video.m3u8" }),
+ )
+ .mockResolvedValueOnce(
+ Response.json({ url: "https://cdn.loom.com/original.mp4" }),
+ );
+ vi.stubGlobal("fetch", fetch);
+ await expect(getLoomDownloadUrl("video")).resolves.toBe(
+ "https://cdn.loom.com/original.mp4",
+ );
+ });
+
+ it.each(["m3u8", "mpd"])(
+ "uses segment credentials for a legacy %s stream",
+ async (extension) => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(
+ Response.json({
+ url: `https://cdn.loom.com/video.${extension}?Policy=manifest-policy&Signature=manifest-signature&Key-Pair-Id=manifest-key`,
+ part_credentials: {
+ Policy: "segment-policy",
+ Signature: "segment-signature",
+ "Key-Pair-Id": "segment-key",
+ },
+ }),
+ ),
+ );
+ const url = new URL((await getLoomDownloadUrl("video")) ?? "");
+ expect(url.searchParams.get("Policy")).toBe("segment-policy");
+ expect(url.searchParams.get("Signature")).toBe("segment-signature");
+ expect(url.searchParams.get("Key-Pair-Id")).toBe("segment-key");
+ },
+ );
+
+ it("treats incomplete segment credentials as a temporary source failure", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(
+ Response.json({
+ url: "https://cdn.loom.com/video.m3u8?Policy=manifest-policy",
+ part_credentials: { Policy: "segment-policy" },
+ }),
+ ),
+ );
+ await expect(getLoomDownloadUrl("video")).rejects.toBeInstanceOf(
+ LoomDownloadTemporaryError,
+ );
+ });
+
+ it("uses the public player source for older trimmed videos", async () => {
+ const fetch = vi
+ .fn()
+ .mockResolvedValueOnce(new Response(null, { status: 204 }))
+ .mockResolvedValueOnce(new Response(null, { status: 204 }))
+ .mockResolvedValueOnce(new Response(null, { status: 204 }))
+ .mockResolvedValueOnce(new Response(null, { status: 204 }))
+ .mockResolvedValueOnce(
+ Response.json({
+ data: {
+ getVideo: {
+ id: "video",
+ downloadable: true,
+ download_enabled: true,
+ nullableRawCdnUrl: { url: "https://cdn.loom.com/video-trim.mp4" },
+ },
+ },
+ }),
+ );
+ vi.stubGlobal("fetch", fetch);
+ await expect(getLoomDownloadUrl("video")).resolves.toBe(
+ "https://cdn.loom.com/video-trim.mp4",
+ );
+ });
+
+ it.each([null, {}])(
+ "retries a public downloadable video while its source is unavailable: %j",
+ async (source) => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async (input) =>
+ String(input) === "https://www.loom.com/graphql"
+ ? Response.json({
+ data: {
+ getVideo: {
+ id: "video",
+ downloadable: true,
+ download_enabled: true,
+ nullableRawCdnUrl: source,
+ },
+ },
+ })
+ : new Response(null, { status: 204 }),
+ ),
+ );
+ await expect(getLoomDownloadUrl("video")).rejects.toBeInstanceOf(
+ LoomDownloadTemporaryError,
+ );
+ },
+ );
+
+ it.each([
+ { id: "video", downloadable: false, download_enabled: false },
+ { id: "different-video", downloadable: true, download_enabled: true },
+ ])(
+ "rejects a disabled or mismatched public player source: %j",
+ async (video) => {
+ const fetch = vi.fn().mockImplementation(async (url: string) =>
+ url.endsWith("/graphql")
+ ? Response.json({
+ data: {
+ getVideo: {
+ ...video,
+ nullableRawCdnUrl: {
+ url: "https://cdn.loom.com/video.mp4",
+ },
+ },
+ },
+ })
+ : new Response(null, { status: 204 }),
+ );
+ vi.stubGlobal("fetch", fetch);
+ await expect(getLoomDownloadUrl("video")).resolves.toBeNull();
+ },
+ );
+});
+
+describe("getReusableLoomDownloadUrl", () => {
+ it.each(["cdn.loom.com", "luna.loom.com"])(
+ "reuses a source with enough signed URL lifetime on %s",
+ (hostname) => {
+ const expires = Math.floor(Date.now() / 1000) + 7200;
+ const url = `https://${hostname}/video.m3u8?Expires=${expires}`;
+ expect(getReusableLoomDownloadUrl(url)).toBe(url);
+ const policy = Buffer.from(
+ JSON.stringify({
+ Statement: [
+ { Condition: { DateLessThan: { "AWS:EpochTime": expires } } },
+ ],
+ }),
+ )
+ .toString("base64")
+ .replace(/\+/g, "-")
+ .replace(/=/g, "_")
+ .replace(/\//g, "~");
+ const policyUrl = `https://${hostname}/video.m3u8?Policy=${policy}`;
+ expect(getReusableLoomDownloadUrl(policyUrl)).toBe(policyUrl);
+ },
+ );
+
+ it.each([
+ undefined,
+ "https://cdn.loom.com/video.m3u8?Expires=1",
+ "https://luna.loom.com/video.m3u8?Expires=1",
+ "https://luna.loom.com.other.example/video.mp4",
+ "https://cdn.loom.com/video.m3u8?Policy=invalid",
+ "https://other.example/video.mp4",
+ "http://cdn.loom.com/video.mp4",
+ "https://cdn.loom.com/video.m3u8?Signature=unknown",
+ ])("refreshes stale or unrecognized sources: %s", (url) => {
+ expect(getReusableLoomDownloadUrl(url)).toBeNull();
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-file-cleanup.test.ts b/apps/web/__tests__/unit/loom-file-cleanup.test.ts
new file mode 100644
index 000000000..0170122e2
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-file-cleanup.test.ts
@@ -0,0 +1,114 @@
+import { Effect } from "effect";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ where: vi.fn(),
+ access: vi.fn(),
+ head: vi.fn(),
+ remove: vi.fn(),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({ from: () => ({ where: mocks.where }) }),
+ }),
+}));
+vi.mock("@cap/web-backend/src/Storage/index", () => ({
+ Storage: { getAccessForVideo: mocks.access },
+}));
+vi.mock("@/lib/video-storage", () => ({
+ decodeStorageVideo: (video: unknown) => video,
+}));
+vi.mock("@/lib/workflow-runtime", () => ({
+ runWorkflowPromise: Effect.runPromise,
+}));
+
+describe("completed Loom file cleanup", () => {
+ const input = {
+ videoId: "existing-video",
+ userId: "owner-id",
+ rawFileKey: "owner-id/existing-video/raw-upload.mp4",
+ };
+
+ beforeEach(() => {
+ vi.resetAllMocks();
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+ mocks.where.mockResolvedValue([
+ { id: input.videoId, ownerId: input.userId },
+ ]);
+ mocks.access.mockImplementation(() =>
+ Effect.succeed([{ headObject: mocks.head, deleteObject: mocks.remove }]),
+ );
+ mocks.head.mockImplementation(() => Effect.succeed({ ContentLength: 100 }));
+ mocks.remove.mockImplementation(() => Effect.void);
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it("removes only the owned source after finding a completed output", async () => {
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await cleanupLoomFileSource(input);
+ expect(mocks.head).toHaveBeenCalledExactlyOnceWith(
+ "owner-id/existing-video/result.mp4",
+ );
+ expect(mocks.remove).toHaveBeenCalledExactlyOnceWith(input.rawFileKey);
+ const [headOrder = Infinity] = mocks.head.mock.invocationCallOrder;
+ const [removeOrder = 0] = mocks.remove.mock.invocationCallOrder;
+ expect(headOrder).toBeLessThan(removeOrder);
+ });
+
+ it.each([
+ "owner-id/existing-video/result.mp4",
+ "another-owner/existing-video/raw-upload.mp4",
+ "owner-id/another-video/raw-upload.mp4",
+ ])("never removes an output or unrelated source: %s", async (rawFileKey) => {
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await cleanupLoomFileSource({ ...input, rawFileKey });
+ expect(mocks.access).not.toHaveBeenCalled();
+ expect(mocks.remove).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ { records: [] },
+ { records: [{ id: input.videoId, ownerId: "another-owner" }] },
+ ])(
+ "keeps the source if its recording is gone or ownership changed",
+ async ({ records }) => {
+ mocks.where.mockResolvedValue(records);
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await cleanupLoomFileSource(input);
+ expect(mocks.access).not.toHaveBeenCalled();
+ expect(mocks.remove).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each([0, -1, undefined])(
+ "keeps the source when the output has no positive size: %s",
+ async (ContentLength) => {
+ mocks.head.mockImplementation(() => Effect.succeed({ ContentLength }));
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await cleanupLoomFileSource(input);
+ expect(mocks.remove).not.toHaveBeenCalled();
+ },
+ );
+
+ it("keeps the source if storage cannot confirm the output", async () => {
+ mocks.head.mockImplementation(() =>
+ Effect.fail(new Error("Storage unavailable")),
+ );
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await expect(cleanupLoomFileSource(input)).resolves.toBeUndefined();
+ expect(mocks.remove).not.toHaveBeenCalled();
+ });
+
+ it("does not fail a completed import when source cleanup fails", async () => {
+ mocks.remove.mockImplementation(() =>
+ Effect.fail(new Error("Cleanup unavailable")),
+ );
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await expect(cleanupLoomFileSource(input)).resolves.toBeUndefined();
+ expect(mocks.remove).toHaveBeenCalledOnce();
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-file-processing.test.ts b/apps/web/__tests__/unit/loom-file-processing.test.ts
new file mode 100644
index 000000000..2bcf65ecc
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-file-processing.test.ts
@@ -0,0 +1,130 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ transition: vi.fn(),
+ start: vi.fn(),
+ setError: vi.fn(),
+}));
+
+vi.mock("@/lib/video-processing", () => ({
+ transitionVideoToProcessing: mocks.transition,
+ setVideoProcessingError: mocks.setError,
+}));
+
+vi.mock("@/lib/loom-import-start", () => ({
+ startLoomImportWorkflow: mocks.start,
+ LoomImportStartError: class extends Error {
+ constructor(
+ readonly canRetry: boolean,
+ cause: unknown,
+ ) {
+ super("Loom startup failed", { cause });
+ }
+ },
+}));
+
+describe("large Loom file processing", () => {
+ const input = {
+ videoId: "existing-video" as never,
+ userId: "owner-id",
+ loomVideoId: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ rawFileKey: "owner-id/existing-video/raw-upload.mp4",
+ bucketId: null,
+ processingMessage: "Processing Loom video...",
+ startFailureMessage: "Loom file processing could not start.",
+ };
+
+ beforeEach(() => {
+ vi.resetAllMocks();
+ mocks.transition.mockResolvedValue("started");
+ mocks.start.mockResolvedValue(undefined);
+ mocks.setError.mockResolvedValue(undefined);
+ });
+
+ it("claims the existing upload and reuses its raw source through the multipart Loom workflow", async () => {
+ const { startLoomFileProcessingWorkflow } = await import(
+ "@/lib/loom-file-processing"
+ );
+ await expect(startLoomFileProcessingWorkflow(input)).resolves.toBe(
+ "started",
+ );
+ expect(mocks.transition).toHaveBeenCalledExactlyOnceWith({
+ videoId: input.videoId,
+ rawFileKey: input.rawFileKey,
+ processingMessage: input.processingMessage,
+ });
+ expect(mocks.start).toHaveBeenCalledExactlyOnceWith({
+ videoId: input.videoId,
+ userId: input.userId,
+ rawFileKey: input.rawFileKey,
+ bucketId: null,
+ loomVideoId: input.loomVideoId,
+ reuseExistingRawUpload: true,
+ cleanupRawUpload: true,
+ });
+ expect(mocks.setError).not.toHaveBeenCalled();
+ });
+
+ it.each(["already-processing", "already-complete"])(
+ "does not dispatch another run when the upload is %s",
+ async (status) => {
+ mocks.transition.mockResolvedValue(status);
+ const { startLoomFileProcessingWorkflow } = await import(
+ "@/lib/loom-file-processing"
+ );
+ await expect(startLoomFileProcessingWorkflow(input)).resolves.toBe(
+ status,
+ );
+ expect(mocks.start).not.toHaveBeenCalled();
+ },
+ );
+
+ it("rejects an invalid source before claiming the upload", async () => {
+ const { startLoomFileProcessingWorkflow } = await import(
+ "@/lib/loom-file-processing"
+ );
+ await expect(
+ startLoomFileProcessingWorkflow({ ...input, loomVideoId: "invalid" }),
+ ).rejects.toThrow("no valid Loom source mapping");
+ expect(mocks.transition).not.toHaveBeenCalled();
+ expect(mocks.start).not.toHaveBeenCalled();
+ });
+
+ it("does not dispatch when claiming the upload fails", async () => {
+ mocks.transition.mockRejectedValue(new Error("Claim changed"));
+ const { startLoomFileProcessingWorkflow } = await import(
+ "@/lib/loom-file-processing"
+ );
+ await expect(startLoomFileProcessingWorkflow(input)).rejects.toThrow(
+ "Claim changed",
+ );
+ expect(mocks.start).not.toHaveBeenCalled();
+ expect(mocks.setError).not.toHaveBeenCalled();
+ });
+
+ it("restores a retryable error only after a confirmed queue rejection", async () => {
+ const { LoomImportStartError } = await import("@/lib/loom-import-start");
+ const error = new LoomImportStartError(true, new Error("Rejected"));
+ mocks.start.mockRejectedValue(error);
+ const { startLoomFileProcessingWorkflow } = await import(
+ "@/lib/loom-file-processing"
+ );
+ await expect(startLoomFileProcessingWorkflow(input)).rejects.toBe(error);
+ expect(mocks.setError).toHaveBeenCalledExactlyOnceWith(
+ input.videoId,
+ input.startFailureMessage,
+ error,
+ );
+ });
+
+ it("keeps an uncertain dispatch claimed so it cannot be started twice", async () => {
+ const { LoomImportStartError } = await import("@/lib/loom-import-start");
+ const error = new LoomImportStartError(false, new Error("Response lost"));
+ mocks.start.mockRejectedValue(error);
+ const { startLoomFileProcessingWorkflow } = await import(
+ "@/lib/loom-file-processing"
+ );
+ await expect(startLoomFileProcessingWorkflow(input)).rejects.toBe(error);
+ expect(mocks.setError).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-import-start.test.ts b/apps/web/__tests__/unit/loom-import-start.test.ts
new file mode 100644
index 000000000..bf4127d8e
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-import-start.test.ts
@@ -0,0 +1,284 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ where: vi.fn(),
+ set: vi.fn(),
+ queue: vi.fn(),
+ start: vi.fn(),
+ getRun: vi.fn(),
+ getWorld: vi.fn(),
+ mediaJobFailed: vi.fn(),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({ update: () => ({ set: mocks.set }) }),
+}));
+vi.mock("@cap/database/schema", () => ({
+ videos: { id: "videoId", ownerId: "ownerId", metadata: "metadata" },
+ videoUploads: {
+ videoId: "uploadVideoId",
+ phase: "phase",
+ updatedAt: "updatedAt",
+ },
+}));
+vi.mock("@cap/web-domain", () => ({
+ Video: { VideoId: { make: (id: string) => id } },
+ User: { UserId: { make: (id: string) => id } },
+}));
+vi.mock("drizzle-orm", () => ({
+ eq: (column: string, value: unknown) => ({ column, value }),
+ and: (...conditions: unknown[]) => ({ conditions }),
+ sql: (strings: TemplateStringsArray, ...values: unknown[]) => ({
+ strings,
+ values,
+ }),
+}));
+vi.mock("workflow/api", () => ({ start: mocks.start, getRun: mocks.getRun }));
+vi.mock("workflow/runtime", () => ({ getWorld: mocks.getWorld }));
+vi.mock("@/lib/loom-media-dispatch", () => ({
+ isLoomMediaJobFailed: mocks.mediaJobFailed,
+}));
+vi.mock("@/workflows/import-loom-video", () => ({
+ importLoomVideoWorkflow: vi.fn(),
+}));
+
+import {
+ getLoomImportRunState,
+ LoomImportStartError,
+ restoreLoomImportStartError,
+ startLoomImportWorkflow,
+} from "@/lib/loom-import-start";
+
+const payload = {
+ videoId: "video-1",
+ userId: "owner-1",
+ rawFileKey: "owner-1/video-1/raw-upload.mp4",
+ bucketId: null,
+ loomVideoId: "loom-1",
+};
+
+describe("Loom workflow startup receipts", () => {
+ beforeEach(() => {
+ vi.resetAllMocks();
+ mocks.set.mockReturnValue({ where: mocks.where });
+ mocks.where.mockResolvedValue([{ affectedRows: 1 }]);
+ mocks.queue.mockResolvedValue({ messageId: "message-1" });
+ mocks.getWorld.mockReturnValue({ queue: mocks.queue });
+ mocks.mediaJobFailed.mockResolvedValue(false);
+ mocks.start.mockImplementation(
+ async (
+ _workflow: unknown,
+ _args: unknown,
+ options: {
+ world: {
+ queue: (
+ name: string,
+ message: { runId: string },
+ ) => Promise<unknown>;
+ };
+ },
+ ) => {
+ await options.world.queue("workflow-loom", { runId: "run-1" });
+ },
+ );
+ });
+
+ it.each([false, true])(
+ "requires a matching failed worker before releasing an accepted dispatch: %s",
+ async (failed) => {
+ mocks.getRun.mockReturnValue({ status: Promise.resolve("failed") });
+ mocks.mediaJobFailed.mockResolvedValue(failed);
+ expect(
+ await getLoomImportRunState({
+ runId: "run-1",
+ dispatch: "accepted",
+ mediaDispatch: {
+ attemptId: "run-1:0",
+ videoId: "video-1",
+ jobId: "worker-1",
+ state: "accepted",
+ },
+ }),
+ ).toBe(failed ? "inactive" : "unconfirmed");
+ expect(mocks.mediaJobFailed).toHaveBeenCalledWith("worker-1", "video-1");
+ },
+ );
+
+ it("records the run before dispatch and stores queue acceptance", async () => {
+ await startLoomImportWorkflow(payload);
+ expect(
+ mocks.set.mock.calls.map(([value]) =>
+ JSON.parse(value.metadata.values.at(-1)),
+ ),
+ ).toEqual([
+ { runId: "run-1", dispatch: "pending" },
+ { runId: "run-1", dispatch: "accepted" },
+ ]);
+ expect(mocks.where.mock.invocationCallOrder[0]).toBeLessThan(
+ mocks.queue.mock.invocationCallOrder[0] ?? 0,
+ );
+ });
+
+ it("does not overwrite a newer import startup receipt", async () => {
+ mocks.where
+ .mockResolvedValueOnce([{ affectedRows: 1 }])
+ .mockResolvedValueOnce([{ affectedRows: 0 }]);
+ await expect(startLoomImportWorkflow(payload)).resolves.toBeUndefined();
+ const condition = mocks.where.mock.calls.at(-1)?.[0].conditions.at(-1);
+ expect(condition.values).toEqual(["metadata", "run-1"]);
+ expect(condition.strings.join("")).toContain("$.loomImportRun.runId");
+ });
+
+ it.each([
+ "BadRequestError",
+ "UnauthorizedError",
+ "ForbiddenError",
+ "TooManyRequestsError",
+ ])("allows retry after an explicit queue rejection: %s", async (name) => {
+ mocks.queue.mockRejectedValueOnce(
+ Object.assign(new Error("Queue rejected"), { name }),
+ );
+ await expect(startLoomImportWorkflow(payload)).rejects.toMatchObject({
+ canRetry: true,
+ });
+ expect(
+ JSON.parse(mocks.set.mock.calls.at(-1)?.[0].metadata.values.at(-1)),
+ ).toEqual({ runId: "run-1", dispatch: "rejected" });
+ });
+
+ it("preserves an uncertain queue acknowledgement without allowing another dispatch", async () => {
+ mocks.queue.mockRejectedValueOnce(new TypeError("Connection reset"));
+ await expect(startLoomImportWorkflow(payload)).rejects.toMatchObject({
+ canRetry: false,
+ });
+ expect(
+ JSON.parse(mocks.set.mock.calls.at(-1)?.[0].metadata.values.at(-1)),
+ ).toEqual({ runId: "run-1", dispatch: "uncertain" });
+ });
+
+ it("does not treat a run creation error as a rejected queue message", async () => {
+ mocks.start.mockImplementationOnce(
+ async (
+ _workflow: unknown,
+ _args: unknown,
+ options: {
+ world: {
+ queue: (
+ name: string,
+ message: { runId: string },
+ ) => Promise<unknown>;
+ };
+ },
+ ) => {
+ await options.world.queue("workflow-loom", { runId: "run-1" });
+ throw Object.assign(new Error("Run creation rejected"), {
+ name: "BadRequestError",
+ });
+ },
+ );
+ await expect(startLoomImportWorkflow(payload)).resolves.toBeUndefined();
+ expect(
+ JSON.parse(mocks.set.mock.calls.at(-1)?.[0].metadata.values.at(-1))
+ .dispatch,
+ ).toBe("accepted");
+ });
+
+ it("allows retry when startup fails before dispatch", async () => {
+ mocks.start.mockRejectedValueOnce(new Error("Workflow not registered"));
+ await expect(startLoomImportWorkflow(payload)).rejects.toBeInstanceOf(
+ LoomImportStartError,
+ );
+ await expect(startLoomImportWorkflow(payload)).resolves.toBeUndefined();
+ expect(mocks.queue).toHaveBeenCalledTimes(1);
+ });
+
+ it("does not send a job when its run receipt cannot be saved", async () => {
+ mocks.where.mockResolvedValueOnce([{ affectedRows: 0 }]);
+ await expect(startLoomImportWorkflow(payload)).rejects.toMatchObject({
+ canRetry: true,
+ });
+ expect(mocks.queue).not.toHaveBeenCalled();
+ });
+
+ it("keeps the claim when a rejected dispatch receipt cannot be saved", async () => {
+ mocks.queue.mockRejectedValueOnce(
+ Object.assign(new Error("Queue rejected"), { name: "ForbiddenError" }),
+ );
+ mocks.where
+ .mockResolvedValueOnce([{ affectedRows: 1 }])
+ .mockRejectedValueOnce(new Error("Database unavailable"));
+ await expect(startLoomImportWorkflow(payload)).rejects.toMatchObject({
+ canRetry: false,
+ });
+ });
+
+ it.each(["pending", "running", "completed", "failed", "cancelled"] as const)(
+ "uses authoritative run status for %s",
+ async (status) => {
+ mocks.getRun.mockReturnValue({ status: Promise.resolve(status) });
+ expect(
+ await getLoomImportRunState({ runId: "run-1", dispatch: "uncertain" }),
+ ).toBe(
+ status === "pending"
+ ? "pending"
+ : status === "running"
+ ? "active"
+ : "inactive",
+ );
+ },
+ );
+
+ it("holds startup while the run cannot be queried", async () => {
+ mocks.getRun.mockReturnValue({
+ status: Promise.reject(new Error("Not found yet")),
+ });
+ expect(
+ await getLoomImportRunState({ runId: "run-1", dispatch: "uncertain" }),
+ ).toBe("unconfirmed");
+ });
+
+ it.each(["completed", "failed", "cancelled"])(
+ "holds an uncertain media dispatch after the workflow is %s",
+ async (status) => {
+ mocks.getRun.mockReturnValue({ status: Promise.resolve(status) });
+ expect(
+ await getLoomImportRunState({
+ runId: "run-1",
+ dispatch: "accepted",
+ mediaDispatch: { attemptId: "run-1:0", state: "uncertain" },
+ }),
+ ).toBe("unconfirmed");
+ },
+ );
+
+ it("allows rejected and legacy imports without an active run check", async () => {
+ expect(
+ await getLoomImportRunState({ runId: "run-1", dispatch: "rejected" }),
+ ).toBe("inactive");
+ expect(await getLoomImportRunState()).toBe("inactive");
+ expect(mocks.getRun).not.toHaveBeenCalled();
+ });
+
+ it("only restores the same upload claim after dispatch rejection", async () => {
+ const claimedAt = new Date("2026-09-01T00:00:00Z");
+ await restoreLoomImportStartError(
+ "video-1",
+ "processing",
+ claimedAt,
+ "Startup rejected",
+ );
+ expect(mocks.set).toHaveBeenCalledWith(
+ expect.objectContaining({
+ phase: "error",
+ processingError: "Startup rejected",
+ }),
+ );
+ expect(mocks.where).toHaveBeenCalledWith({
+ conditions: [
+ { column: "uploadVideoId", value: "video-1" },
+ { column: "phase", value: "processing" },
+ { column: "updatedAt", value: claimedAt },
+ ],
+ });
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-import-upload.test.ts b/apps/web/__tests__/unit/loom-import-upload.test.ts
new file mode 100644
index 000000000..405393777
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-import-upload.test.ts
@@ -0,0 +1,190 @@
+import { Effect } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ set: vi.fn(),
+ where: vi.fn(),
+ create: vi.fn(),
+ abort: vi.fn(),
+ head: vi.fn(),
+ get: vi.fn(),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({ update: () => ({ set: mocks.set }) }),
+}));
+vi.mock("@cap/database/schema", () => ({
+ videos: {
+ id: "id",
+ ownerId: "ownerId",
+ orgId: "orgId",
+ metadata: "metadata",
+ },
+}));
+vi.mock("@/lib/workflow-runtime", () => ({
+ runWorkflowPromise: (effect: Effect.Effect<unknown>) =>
+ Effect.runPromise(effect),
+}));
+
+import {
+ LOOM_IMPORT_MAX_BYTES,
+ LOOM_IMPORT_PART_BYTES,
+ prepareLoomMultipartUploads,
+} from "@/lib/loom-import-upload";
+
+type Input = Parameters<typeof prepareLoomMultipartUploads>[0];
+
+const video = {
+ id: "video",
+ ownerId: "owner",
+ orgId: "organization",
+ metadata: { loomImportRun: { runId: "run", dispatch: "accepted" } },
+} as Input["video"];
+
+function input(): Input {
+ return {
+ video,
+ bucket: {
+ provider: "s3",
+ headObject: mocks.head,
+ getInternalSignedObjectUrl: mocks.get,
+ multipart: { create: mocks.create, abort: mocks.abort },
+ } as unknown as Input["bucket"],
+ attemptId: "run:0",
+ sourceKey: "owner/video/raw-upload.mp4",
+ webhookBaseUrl: "https://cap.example.com",
+ webhookSecret: "fixture-secret",
+ };
+}
+
+describe("Loom multipart upload preparation", () => {
+ beforeEach(() => {
+ vi.resetAllMocks();
+ mocks.set.mockReturnValue({ where: mocks.where });
+ mocks.where.mockResolvedValue([{ affectedRows: 1 }]);
+ mocks.create.mockImplementation((key: string) =>
+ Effect.succeed({ UploadId: `upload:${key}` }),
+ );
+ mocks.abort.mockReturnValue(Effect.void);
+ mocks.head.mockReturnValue(Effect.fail({ name: "NotFound" }));
+ mocks.get.mockReturnValue(
+ Effect.succeed("https://storage.example.com/result.mp4"),
+ );
+ });
+
+ it("prepares original and output parts for files up to 10 GB", async () => {
+ const result = await prepareLoomMultipartUploads(input());
+ expect(LOOM_IMPORT_MAX_BYTES).toBe(10 * 1024 ** 3);
+ expect(Math.ceil(LOOM_IMPORT_MAX_BYTES / LOOM_IMPORT_PART_BYTES)).toBe(160);
+ expect(mocks.create.mock.calls.map(([key]) => key)).toEqual([
+ "owner/video/raw-upload.mp4",
+ "owner/video/result.mp4",
+ ]);
+ expect(result.sourceUpload).toMatchObject({
+ type: "multipart",
+ key: "owner/video/raw-upload.mp4",
+ loomImportAttemptId: "run:0",
+ partSize: 64 * 1024 ** 2,
+ });
+ expect(result.outputUpload).toMatchObject({
+ type: "multipart",
+ key: "owner/video/result.mp4",
+ loomImportAttemptId: "run:0",
+ });
+ expect(mocks.where).toHaveBeenCalledOnce();
+ });
+
+ it("reuses the same multipart reservations after worker backpressure", async () => {
+ const args = input();
+ args.video = {
+ ...video,
+ metadata: {
+ ...video.metadata,
+ loomImportUpload: {
+ attemptId: "run:0",
+ ownerId: "owner",
+ organizationId: "organization",
+ source: { key: "owner/video/raw-upload.mp4", uploadId: "source-id" },
+ output: { key: "owner/video/result.mp4", uploadId: "output-id" },
+ },
+ },
+ };
+ const result = await prepareLoomMultipartUploads(args);
+ expect(result.sourceUpload?.uploadId).toBe("source-id");
+ expect(result.outputUpload?.uploadId).toBe("output-id");
+ expect(mocks.create).not.toHaveBeenCalled();
+ expect(mocks.where).not.toHaveBeenCalled();
+ });
+
+ it("reuses an identity-bound saved output without reserving or overwriting objects", async () => {
+ const args = input();
+ args.video = {
+ ...video,
+ metadata: {
+ ...video.metadata,
+ loomImportUpload: {
+ attemptId: "run:0",
+ ownerId: "owner",
+ organizationId: "organization",
+ output: {
+ key: "owner/video/result.mp4",
+ uploadId: "completed-upload",
+ },
+ },
+ },
+ };
+ mocks.head.mockReturnValue(
+ Effect.succeed({ ContentLength: 4096, ETag: '"saved-output"' }),
+ );
+ const result = await prepareLoomMultipartUploads({
+ ...args,
+ attemptId: "run:1",
+ });
+ expect(result).toEqual({
+ existingOutput: {
+ url: "https://storage.example.com/result.mp4",
+ objectIdentity: '"saved-output"',
+ fileSize: 4096,
+ },
+ });
+ expect(mocks.create).not.toHaveBeenCalled();
+ expect(mocks.abort).not.toHaveBeenCalled();
+ expect(mocks.where).not.toHaveBeenCalled();
+ });
+
+ it("aborts only newly reserved uploads when recording ownership changes", async () => {
+ mocks.where.mockResolvedValue([{ affectedRows: 0 }]);
+ await expect(prepareLoomMultipartUploads(input())).rejects.toThrow(
+ "ownership changed",
+ );
+ expect(mocks.abort.mock.calls).toEqual([
+ ["owner/video/raw-upload.mp4", "upload:owner/video/raw-upload.mp4"],
+ ["owner/video/result.mp4", "upload:owner/video/result.mp4"],
+ ]);
+ });
+
+ it("cleans up the original reservation if the output cannot be reserved", async () => {
+ mocks.create
+ .mockReturnValueOnce(Effect.succeed({ UploadId: "source-id" }))
+ .mockReturnValueOnce(Effect.fail(new Error("Storage unavailable")));
+ await expect(prepareLoomMultipartUploads(input())).rejects.toThrow();
+ expect(mocks.abort).toHaveBeenCalledExactlyOnceWith(
+ "owner/video/raw-upload.mp4",
+ "source-id",
+ );
+ expect(mocks.where).not.toHaveBeenCalled();
+ });
+
+ it("refuses a stale workflow and another recording's source key", async () => {
+ await expect(
+ prepareLoomMultipartUploads({ ...input(), attemptId: "another-run:0" }),
+ ).rejects.toThrow("workflow ownership changed");
+ await expect(
+ prepareLoomMultipartUploads({
+ ...input(),
+ sourceKey: "other/video/raw.mp4",
+ }),
+ ).rejects.toThrow("does not match");
+ expect(mocks.create).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-import.test.ts b/apps/web/__tests__/unit/loom-import.test.ts
index a8068f6e3..e26258bd6 100644
--- a/apps/web/__tests__/unit/loom-import.test.ts
+++ b/apps/web/__tests__/unit/loom-import.test.ts
@@ -1,19 +1,33 @@
+import type { Organisation } from "@cap/web-domain";
import { Effect, Option } from "effect";
import { beforeEach, describe, expect, it, vi } from "vitest";
const whereMock = vi.fn();
const valuesMock = vi.fn();
const startMock = vi.fn();
+const getRunMock = vi.hoisted(() => vi.fn());
const revalidatePathMock = vi.fn();
const storageGetWritableAccessForUserMock = vi.hoisted(() => vi.fn());
+const storageGetAccessForVideoMock = vi.hoisted(() => vi.fn());
+const startVideoProcessingWorkflowMock = vi.hoisted(() => vi.fn());
+const startLoomFileProcessingWorkflowMock = vi.hoisted(() => vi.fn());
+const uploadedFileHeadMock = vi.hoisted(() => vi.fn());
const checkRateLimitMock = vi.hoisted(() => vi.fn());
const headersMock = vi.hoisted(() => vi.fn());
const getOrganizationAccessMock = vi.hoisted(() => vi.fn());
const requireSpaceManagerMock = vi.hoisted(() => vi.fn());
const requireOrganizationSettingsManagerMock = vi.hoisted(() => vi.fn());
+const getLoomImportRunStateMock = vi.hoisted(() => vi.fn());
+const restoreLoomImportStartErrorMock = vi.hoisted(() => vi.fn());
const defaultSharing = vi.hoisted(() => ({
getNewVideoPublic: vi.fn(),
}));
+const conciergeMocks = vi.hoisted(() => ({
+ requireMigrationOperator: vi.fn(),
+ reserveConciergeImport: vi.fn(),
+ renewConciergeImport: vi.fn(),
+ releaseConciergeImport: vi.fn(),
+}));
const mockDb = {
select: vi.fn(() => mockDb),
@@ -24,7 +38,7 @@ const mockDb = {
innerJoin: vi.fn(() => mockDb),
leftJoin: vi.fn(() => mockDb),
where: whereMock,
- set: vi.fn(() => mockDb),
+ set: vi.fn((_value: { updatedAt?: Date }) => mockDb),
values: valuesMock,
transaction: vi.fn((callback) => callback(mockDb)),
};
@@ -60,6 +74,19 @@ vi.mock("@cap/database/schema", () => ({
source: "source",
sourceId: "sourceId",
},
+ loomMigrationImports: {
+ videoId: "loomMigrationImportVideoId",
+ requestId: "loomMigrationImportRequestId",
+ },
+ loomMigrationRequests: {
+ id: "loomMigrationRequestId",
+ organizationId: "loomMigrationRequestOrganizationId",
+ activeOrganizationId: "loomMigrationRequestActiveOrganizationId",
+ status: "loomMigrationRequestStatus",
+ activeImportLeaseToken: "loomMigrationRequestLeaseToken",
+ activeImportLeaseUntil: "loomMigrationRequestLeaseUntil",
+ queuedVideoCount: "loomMigrationRequestQueuedVideoCount",
+ },
organizationMembers: {
id: "memberId",
userId: "memberUserId",
@@ -115,6 +142,9 @@ vi.mock("@cap/database/schema", () => ({
},
videoUploads: {
videoId: "videoId",
+ phase: "phase",
+ rawFileKey: "rawFileKey",
+ updatedAt: "updatedAt",
},
}));
@@ -139,9 +169,23 @@ vi.mock("@cap/utils", () => ({
vi.mock("@cap/web-backend", () => ({
Storage: {
getWritableAccessForUser: storageGetWritableAccessForUserMock,
+ getAccessForVideo: storageGetAccessForVideoMock,
},
}));
+vi.mock("@/lib/video-processing", () => ({
+ startVideoProcessingWorkflow: startVideoProcessingWorkflowMock,
+}));
+
+vi.mock("@/lib/loom-file-processing", () => ({
+ LOOM_FILE_SINGLE_UPLOAD_MAX_BYTES: 5 * 1024 ** 3,
+ startLoomFileProcessingWorkflow: startLoomFileProcessingWorkflowMock,
+}));
+
+vi.mock("@/lib/video-storage", () => ({
+ decodeStorageVideo: (video: unknown) => video,
+}));
+
vi.mock("@cap/web-domain", () => ({
Space: {
SpaceId: {
@@ -168,6 +212,11 @@ vi.mock("drizzle-orm", () => ({
and: vi.fn((...args: unknown[]) => args),
eq: vi.fn((field: unknown, value: unknown) => ({ field, value })),
isNull: vi.fn((field: unknown) => ({ field })),
+ isNotNull: vi.fn((field: unknown) => ({ field })),
+ sql: vi.fn((strings: TemplateStringsArray, ...values: unknown[]) => ({
+ strings,
+ values,
+ })),
}));
vi.mock("next/cache", () => ({
@@ -199,12 +248,45 @@ vi.mock("@/actions/organization/space-authorization", () => ({
vi.mock("workflow/api", () => ({
start: startMock,
+ getRun: getRunMock,
}));
vi.mock("@/workflows/import-loom-video", () => ({
importLoomVideoWorkflow: Symbol("importLoomVideoWorkflow"),
}));
+vi.mock("@/lib/loom-concierge", () => ({
+ ...conciergeMocks,
+ toLoomMigrationResult: async (run: () => Promise<unknown>) => {
+ const { LoomMigrationError } = await import("@/lib/loom-migration-state");
+ try {
+ return { ok: true, value: await run() };
+ } catch (error) {
+ if (error instanceof LoomMigrationError) {
+ return { ok: false, error: error.message };
+ }
+ throw error;
+ }
+ },
+}));
+
+vi.mock("@/lib/org-pro", () => ({
+ isOrganizationOwnerPro: vi.fn(async () => true),
+}));
+
+vi.mock("@/lib/loom-import-start", () => ({
+ getLoomImportRunState: getLoomImportRunStateMock,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE: "Confirmation required",
+ restoreLoomImportStartError: restoreLoomImportStartErrorMock,
+ startLoomImportWorkflow: (payload: unknown) =>
+ startMock(Symbol("workflow"), [payload]),
+ LoomImportStartError: class extends Error {
+ constructor(readonly canRetry: boolean) {
+ super("Loom import startup failed");
+ }
+ },
+}));
+
import { getCurrentUser } from "@cap/database/auth/session";
const mockGetCurrentUser = getCurrentUser as ReturnType<typeof vi.fn>;
@@ -236,6 +318,7 @@ describe("importFromLoom", () => {
valuesMock.mockResolvedValue(undefined);
whereMock.mockResolvedValue([]);
startMock.mockResolvedValue(undefined);
+ getLoomImportRunStateMock.mockResolvedValue("inactive");
checkRateLimitMock.mockResolvedValue({ rateLimited: false });
getOrganizationAccessMock.mockResolvedValue({
id: "org-1",
@@ -261,6 +344,151 @@ describe("importFromLoom", () => {
vi.stubGlobal("fetch", vi.fn());
});
+ it("restarts a failed import without creating another video", async () => {
+ whereMock
+ .mockResolvedValueOnce([
+ {
+ videoId: "existing-video",
+ ownerId: "user-123",
+ bucketId: "bucket-1",
+ phase: "error",
+ rawFileKey: null,
+ uploadUpdatedAt: new Date("2026-09-01T00:00:00Z"),
+ },
+ ])
+ .mockResolvedValueOnce([{ affectedRows: 1 }]);
+ const { importFromLoom } = await import("@/actions/loom");
+ await expect(
+ importFromLoom({
+ loomUrl: "https://www.loom.com/share/loom-abc1234567",
+ orgId: "org-1" as Organisation.OrganisationId,
+ }),
+ ).resolves.toEqual({ success: true, videoId: "existing-video" });
+ expect(mockDb.insert).not.toHaveBeenCalled();
+ expect(startMock).toHaveBeenCalledWith(expect.anything(), [
+ {
+ videoId: "existing-video",
+ userId: "user-123",
+ bucketId: "bucket-1",
+ loomVideoId: "loom-abc1234567",
+ rawFileKey: "user-123/existing-video/raw-upload.mp4",
+ reuseExistingRawUpload: true,
+ },
+ ]);
+ });
+
+ it.each([true, false])(
+ "restores a rejected startup without reopening an uncertain dispatch: %s",
+ async (canRetry) => {
+ whereMock
+ .mockResolvedValueOnce([
+ {
+ videoId: "existing-video",
+ ownerId: "user-123",
+ phase: "error",
+ bucketId: null,
+ rawFileKey: null,
+ uploadUpdatedAt: new Date(),
+ },
+ ])
+ .mockResolvedValueOnce([{ affectedRows: 1 }]);
+ const { LoomImportStartError } = await import("@/lib/loom-import-start");
+ startMock.mockRejectedValueOnce(
+ new LoomImportStartError(canRetry, new Error()),
+ );
+ const { importFromLoom } = await import("@/actions/loom");
+ expect(
+ await importFromLoom({
+ loomUrl: "https://www.loom.com/share/loom-abc1234567",
+ orgId: "org-1" as never,
+ }),
+ ).toMatchObject({ success: false, error: "Loom import startup failed" });
+ if (canRetry) {
+ expect(restoreLoomImportStartErrorMock).toHaveBeenCalledWith(
+ "existing-video",
+ "processing",
+ mockDb.set.mock.calls[0]?.[0].updatedAt,
+ "Loom import startup failed",
+ );
+ } else expect(restoreLoomImportStartErrorMock).not.toHaveBeenCalled();
+ },
+ );
+
+ it("holds a failed row while its recorded workflow is still active", async () => {
+ const receipt = { runId: "run-active", dispatch: "accepted" };
+ whereMock.mockResolvedValueOnce([
+ {
+ videoId: "existing-video",
+ ownerId: "user-123",
+ phase: "error",
+ uploadUpdatedAt: new Date(),
+ metadata: { loomImportRun: receipt },
+ },
+ ]);
+ getLoomImportRunStateMock.mockResolvedValueOnce("active");
+ const { importFromLoom } = await import("@/actions/loom");
+ expect(
+ await importFromLoom({
+ loomUrl: "https://www.loom.com/share/loom-abc1234567",
+ orgId: "org-1" as never,
+ }),
+ ).toMatchObject({ success: false });
+ expect(getLoomImportRunStateMock).toHaveBeenCalledWith(receipt);
+ expect(mockDb.update).not.toHaveBeenCalled();
+ expect(startMock).not.toHaveBeenCalled();
+ });
+
+ it("reports an unconfirmed dispatch without claiming it again", async () => {
+ whereMock.mockResolvedValueOnce([
+ {
+ videoId: "existing-video",
+ ownerId: "user-123",
+ phase: "error",
+ uploadUpdatedAt: new Date(),
+ metadata: {
+ loomImportRun: { runId: "missing-run", dispatch: "uncertain" },
+ },
+ },
+ ]);
+ getLoomImportRunStateMock.mockResolvedValueOnce("unconfirmed");
+ const { importFromLoom } = await import("@/actions/loom");
+ expect(
+ await importFromLoom({
+ loomUrl: "https://www.loom.com/share/loom-abc1234567",
+ orgId: "org-1" as never,
+ }),
+ ).toMatchObject({ success: false, error: "Confirmation required" });
+ expect(mockDb.update).not.toHaveBeenCalled();
+ expect(startMock).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ { ownerId: "user-123", phase: "processing", affectedRows: 1 },
+ { ownerId: "other-user", phase: "error", affectedRows: 1 },
+ { ownerId: "user-123", phase: "error", affectedRows: 0 },
+ ])("does not restart an unclaimed or foreign import: %j", async (row) => {
+ whereMock
+ .mockResolvedValueOnce([
+ {
+ videoId: "existing-video",
+ bucketId: "bucket-1",
+ rawFileKey: null,
+ uploadUpdatedAt: new Date("2026-09-01T00:00:00Z"),
+ ...row,
+ },
+ ])
+ .mockResolvedValueOnce([{ affectedRows: row.affectedRows }]);
+ const { importFromLoom } = await import("@/actions/loom");
+ await expect(
+ importFromLoom({
+ loomUrl: "https://www.loom.com/share/loom-abc1234567",
+ orgId: "org-1" as Organisation.OrganisationId,
+ }),
+ ).resolves.toMatchObject({ success: false });
+ expect(startMock).not.toHaveBeenCalled();
+ expect(mockDb.insert).not.toHaveBeenCalled();
+ });
+
it("returns direct MP4 URLs for public Loom downloads", async () => {
const fetchMock = vi.mocked(fetch);
fetchMock.mockImplementation(async (input) => {
@@ -394,6 +622,49 @@ describe("importFromLoom", () => {
expect(valuesMock).not.toHaveBeenCalled();
});
+ it.each([
+ ["long titles", "a".repeat(383), "a".repeat(255)],
+ ["Unicode titles", "🎥".repeat(260), "🎥".repeat(255)],
+ ["short titles", "Imported 🎥 video", "Imported 🎥 video"],
+ ])(
+ "fits %s into the destination without changing the import",
+ async (_, title, expected) => {
+ valuesMock.mockImplementation(async (value: { name?: string }) => {
+ if (value.name && Array.from(value.name).length > 255) {
+ throw new Error("Data too long for column name");
+ }
+ });
+ vi.mocked(fetch).mockImplementation(async (input) => {
+ const url = input.toString();
+ if (url.includes("/transcoded-url")) {
+ return Response.json({ url: "https://cdn.loom.com/video.mp4" });
+ }
+ if (url === "https://www.loom.com/graphql") {
+ return Response.json({ data: { getVideo: { name: title } } });
+ }
+ if (url.includes("/v1/oembed")) {
+ return Response.json({ duration: 42, width: 1920, height: 1080 });
+ }
+ throw new Error(`Unexpected fetch: ${url}`);
+ });
+ const { importFromLoom } = await import("@/actions/loom");
+ expect(
+ await importFromLoom({
+ loomUrl: "https://www.loom.com/share/loom-abc1234567",
+ orgId: "org-1" as Organisation.OrganisationId,
+ }),
+ ).toEqual({ success: true, videoId: "video-123" });
+ expect(valuesMock).toHaveBeenCalledWith(
+ expect.objectContaining({
+ name: expected,
+ ownerId: "user-123",
+ orgId: "org-1",
+ }),
+ );
+ expect(startMock).toHaveBeenCalledTimes(1);
+ },
+ );
+
it("does not call the Vercel firewall from the Loom import server action", async () => {
checkRateLimitMock.mockResolvedValueOnce({ rateLimited: true });
@@ -498,7 +769,7 @@ describe("importFromLoom", () => {
3,
expect.objectContaining({
id: "video-123",
- orgId: "org-1",
+ orgId: "org-1" as Organisation.OrganisationId,
source: "loom",
sourceId: "loom-abc1234567",
}),
@@ -936,6 +1207,59 @@ describe("importFromLoom", () => {
expect(startMock).toHaveBeenCalledTimes(1);
});
+ it("marks CSV rows that hit a temporary Loom failure as retryable", async () => {
+ whereMock.mockImplementation((conditions: unknown) => {
+ if (JSON.stringify(conditions).includes("sourceId")) {
+ return Promise.resolve([]);
+ }
+
+ return withLimit([{ userId: "member-123", email: "[email protected]" }]);
+ });
+
+ const fetchMock = vi.mocked(fetch);
+ fetchMock.mockImplementation(async (input) => {
+ const url = typeof input === "string" ? input : input.toString();
+
+ if (url.includes("/transcoded-url")) {
+ return {
+ ok: false,
+ status: 429,
+ headers: new Headers({ "retry-after": "30" }),
+ body: null,
+ } as Response;
+ }
+
+ throw new Error(`Unexpected fetch: ${url}`);
+ });
+
+ const { importFromLoomCsv } = await import("@/actions/loom");
+
+ const result = await importFromLoomCsv({
+ orgId: "org-1" as never,
+ rows: [
+ {
+ rowNumber: 2,
+ loomUrl: "https://www.loom.com/share/loom-ratelimited1",
+ userEmail: "[email protected]",
+ },
+ ],
+ });
+
+ expect(result.results).toStrictEqual([
+ {
+ rowNumber: 2,
+ userEmail: "[email protected]",
+ spaceName: undefined,
+ success: false,
+ videoId: undefined,
+ error:
+ "Loom is temporarily unavailable. Please retry the import later.",
+ errorCode: "loom_temporarily_unavailable",
+ },
+ ]);
+ expect(startMock).not.toHaveBeenCalled();
+ });
+
it("limits CSV imports to 500 rows", async () => {
const fetchMock = vi.mocked(fetch);
const { importFromLoomCsv } = await import("@/actions/loom");
@@ -1157,3 +1481,490 @@ describe("importFromLoom", () => {
expect(revalidatePathMock).toHaveBeenCalledWith("/dashboard");
});
});
+
+describe("importFromLoomCsvForConcierge", () => {
+ const requestId = "request12345678";
+ const leaseToken = "lease-1";
+ const failedImport = {
+ videoId: "existing-video",
+ ownerId: "user-123",
+ bucketId: "bucket-1",
+ phase: "error",
+ rawFileKey: null,
+ uploadUpdatedAt: new Date("2026-09-01T00:00:00Z"),
+ };
+ const forUpdateMock = vi.fn();
+
+ function withReservation(reservation: unknown) {
+ forUpdateMock.mockResolvedValueOnce([reservation]);
+ return { limit: vi.fn(() => ({ for: forUpdateMock })) };
+ }
+
+ function queueConciergeRowLookups() {
+ whereMock
+ .mockReturnValueOnce(
+ withLimit([
+ {
+ organizationId: "org-1",
+ status: "in_progress",
+ ownerId: "owner-1",
+ },
+ ]),
+ )
+ .mockReturnValueOnce(
+ withLimit([{ userId: "user-123", email: "[email protected]" }]),
+ )
+ .mockResolvedValueOnce([failedImport]);
+ }
+
+ async function importConciergeRow() {
+ const { importFromLoomCsvForConcierge } = await import("@/actions/loom");
+ return importFromLoomCsvForConcierge({
+ requestId,
+ rows: [
+ {
+ rowNumber: 2,
+ loomUrl: "https://www.loom.com/share/loom-abc1234567",
+ userEmail: "[email protected]",
+ },
+ ],
+ });
+ }
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ whereMock.mockReset();
+ valuesMock.mockReset();
+ forUpdateMock.mockReset();
+ mockDb.select.mockReturnValue(mockDb);
+ mockDb.insert.mockReturnValue(mockDb);
+ mockDb.update.mockReturnValue(mockDb);
+ mockDb.from.mockReturnValue(mockDb);
+ mockDb.innerJoin.mockReturnValue(mockDb);
+ mockDb.leftJoin.mockReturnValue(mockDb);
+ mockDb.set.mockReturnValue(mockDb);
+ mockDb.transaction.mockImplementation((callback) => callback(mockDb));
+ valuesMock.mockResolvedValue(undefined);
+ whereMock.mockResolvedValue([]);
+ startMock.mockResolvedValue(undefined);
+ getLoomImportRunStateMock.mockResolvedValue("inactive");
+ conciergeMocks.requireMigrationOperator.mockResolvedValue({
+ id: "operator-1",
+ });
+ conciergeMocks.reserveConciergeImport.mockResolvedValue(leaseToken);
+ conciergeMocks.renewConciergeImport.mockResolvedValue(undefined);
+ conciergeMocks.releaseConciergeImport.mockResolvedValue(undefined);
+ });
+
+ it("records a restarted failed import against the migration request under the lease", async () => {
+ queueConciergeRowLookups();
+ whereMock
+ .mockReturnValueOnce(
+ withReservation({
+ activeOrganizationId: "org-1",
+ leaseToken,
+ leaseUntil: new Date(Date.now() + 60_000),
+ }),
+ )
+ .mockReturnValueOnce(withLimit([]))
+ .mockResolvedValueOnce([{ affectedRows: 1 }]);
+
+ const result = await importConciergeRow();
+
+ expect(result).toMatchObject({
+ ok: true,
+ value: {
+ success: true,
+ importedCount: 1,
+ results: [{ success: true, videoId: "existing-video" }],
+ },
+ });
+ expect(forUpdateMock).toHaveBeenCalledWith("update");
+ expect(mockDb.set).toHaveBeenCalledWith(
+ expect.objectContaining({ phase: "processing" }),
+ );
+ expect(valuesMock).toHaveBeenCalledWith({
+ videoId: "existing-video",
+ requestId,
+ });
+ expect(mockDb.set).toHaveBeenCalledWith(
+ expect.objectContaining({ queuedVideoCount: expect.anything() }),
+ );
+ expect(startMock).toHaveBeenCalledWith(expect.anything(), [
+ expect.objectContaining({
+ videoId: "existing-video",
+ reuseExistingRawUpload: true,
+ }),
+ ]);
+ expect(conciergeMocks.releaseConciergeImport).toHaveBeenCalledWith(
+ requestId,
+ leaseToken,
+ );
+ });
+
+ it("does not count a failed import twice when it already belongs to the request", async () => {
+ queueConciergeRowLookups();
+ whereMock
+ .mockReturnValueOnce(
+ withReservation({
+ activeOrganizationId: "org-1",
+ leaseToken,
+ leaseUntil: new Date(Date.now() + 60_000),
+ }),
+ )
+ .mockReturnValueOnce(withLimit([{ requestId }]))
+ .mockResolvedValueOnce([{ affectedRows: 1 }]);
+
+ const result = await importConciergeRow();
+
+ expect(result).toMatchObject({ ok: true, value: { importedCount: 1 } });
+ expect(valuesMock).not.toHaveBeenCalled();
+ expect(mockDb.set).not.toHaveBeenCalledWith(
+ expect.objectContaining({ queuedVideoCount: expect.anything() }),
+ );
+ expect(startMock).toHaveBeenCalledTimes(1);
+ });
+
+ it("does not restart a failed import once the concierge lease has expired", async () => {
+ queueConciergeRowLookups();
+ whereMock.mockReturnValueOnce(
+ withReservation({
+ activeOrganizationId: "org-1",
+ leaseToken: "newer-lease",
+ leaseUntil: new Date(Date.now() + 60_000),
+ }),
+ );
+
+ const result = await importConciergeRow();
+
+ expect(result).toMatchObject({
+ ok: true,
+ value: { success: false, importedCount: 0, failedCount: 1 },
+ });
+ expect(mockDb.update).not.toHaveBeenCalled();
+ expect(valuesMock).not.toHaveBeenCalled();
+ expect(startMock).not.toHaveBeenCalled();
+ });
+
+ it("refuses to restart a failed import that belongs to another migration request", async () => {
+ queueConciergeRowLookups();
+ whereMock
+ .mockReturnValueOnce(
+ withReservation({
+ activeOrganizationId: "org-1",
+ leaseToken,
+ leaseUntil: new Date(Date.now() + 60_000),
+ }),
+ )
+ .mockReturnValueOnce(withLimit([{ requestId: "otherrequest123" }]));
+
+ const result = await importConciergeRow();
+
+ expect(result).toMatchObject({
+ ok: true,
+ value: {
+ success: false,
+ results: [
+ {
+ success: false,
+ error: "This Loom import belongs to another migration request.",
+ },
+ ],
+ },
+ });
+ expect(mockDb.update).not.toHaveBeenCalled();
+ expect(valuesMock).not.toHaveBeenCalled();
+ expect(startMock).not.toHaveBeenCalled();
+ });
+});
+
+describe("resuming an uploaded concierge Loom file", () => {
+ const input = {
+ requestId: "req000000000001",
+ loomUrl: "https://www.loom.com/share/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ userEmail: "[email protected]",
+ confirmedPreviousProcessingStopped: true,
+ };
+ const recording = {
+ videoId: "existing-video",
+ ownerId: "owner-id",
+ ownerEmail: "[email protected]",
+ organizationId: "org-1",
+ metadata: { loomImportRun: { runId: "old-run", dispatch: "accepted" } },
+ };
+ const uploaded = {
+ loomVideoId: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ video: {
+ id: "existing-video",
+ ownerId: "owner-id",
+ orgId: "org-1",
+ bucket: null,
+ },
+ rawFileKey: "owner-id/existing-video/raw-upload.mp4",
+ phase: "error",
+ processingMessage: null,
+ };
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ whereMock.mockReset();
+ mockDb.select.mockReturnValue(mockDb);
+ mockDb.from.mockReturnValue(mockDb);
+ mockDb.innerJoin.mockReturnValue(mockDb);
+ mockDb.leftJoin.mockReturnValue(mockDb);
+ conciergeMocks.requireMigrationOperator.mockResolvedValue({
+ id: "operator",
+ });
+ conciergeMocks.reserveConciergeImport.mockResolvedValue("lease");
+ conciergeMocks.releaseConciergeImport.mockResolvedValue(undefined);
+ storageGetAccessForVideoMock.mockReturnValue(
+ Effect.succeed([{ provider: "s3", headObject: uploadedFileHeadMock }]),
+ );
+ uploadedFileHeadMock.mockReturnValue(
+ Effect.succeed({ ContentLength: 6 * 1024 ** 3 }),
+ );
+ startVideoProcessingWorkflowMock.mockResolvedValue("started");
+ startLoomFileProcessingWorkflowMock.mockResolvedValue("started");
+ getRunMock.mockReturnValue({ status: Promise.resolve("failed") });
+ });
+
+ it("requires explicit confirmation before inspecting a recording", async () => {
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(
+ resumeConciergeLoomFileUpload({
+ ...input,
+ confirmedPreviousProcessingStopped: false,
+ }),
+ ).resolves.toMatchObject({ ok: false });
+ expect(mockDb.select).not.toHaveBeenCalled();
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ });
+
+ it.each(["running", "pending"])(
+ "refuses a previous workflow that is still %s",
+ async (status) => {
+ whereMock.mockReturnValueOnce(withLimit([recording]));
+ getRunMock.mockReturnValueOnce({ status: Promise.resolve(status) });
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject(
+ {
+ ok: false,
+ error: expect.stringContaining("active or could not be confirmed"),
+ },
+ );
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ },
+ );
+
+ it("refuses an unconfirmed previous workflow", async () => {
+ whereMock.mockReturnValueOnce(withLimit([recording]));
+ getRunMock.mockImplementationOnce(() => ({
+ status: Promise.reject(new Error("Unavailable")),
+ }));
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({
+ ok: false,
+ error: expect.stringContaining("active or could not be confirmed"),
+ });
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ });
+
+ it("can resume after a confirmed queue rejection without querying its unqueued run", async () => {
+ whereMock
+ .mockReturnValueOnce(
+ withLimit([
+ {
+ ...recording,
+ metadata: {
+ loomImportRun: {
+ runId: "rejected-run",
+ dispatch: "rejected",
+ },
+ },
+ },
+ ]),
+ )
+ .mockReturnValueOnce(withLimit([uploaded]));
+ getRunMock.mockImplementation(() => {
+ throw new Error("Unqueued run is unavailable");
+ });
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toEqual({
+ ok: true,
+ value: { status: "started" },
+ });
+ expect(getRunMock).not.toHaveBeenCalled();
+ expect(startLoomFileProcessingWorkflowMock).toHaveBeenCalledOnce();
+ });
+
+ it("processes a large uploaded file in its existing recording", async () => {
+ whereMock
+ .mockReturnValueOnce(withLimit([recording]))
+ .mockReturnValueOnce(withLimit([uploaded]));
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toEqual({
+ ok: true,
+ value: { status: "started" },
+ });
+ expect(startLoomFileProcessingWorkflowMock).toHaveBeenCalledExactlyOnceWith(
+ {
+ videoId: "existing-video",
+ userId: "owner-id",
+ rawFileKey: uploaded.rawFileKey,
+ bucketId: null,
+ processingMessage: "Processing Loom video...",
+ startFailureMessage: "Loom file processing could not start.",
+ loomVideoId: uploaded.loomVideoId,
+ },
+ );
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ expect(mockDb.insert).not.toHaveBeenCalled();
+ expect(mockDb.update).not.toHaveBeenCalled();
+ expect(storageGetWritableAccessForUserMock).not.toHaveBeenCalled();
+ });
+
+ it.each([1024, 5 * 1024 ** 3])(
+ "keeps the existing file workflow for a %i-byte upload",
+ async (bytes) => {
+ whereMock
+ .mockReturnValueOnce(withLimit([recording]))
+ .mockReturnValueOnce(withLimit([uploaded]));
+ uploadedFileHeadMock.mockReturnValue(
+ Effect.succeed({ ContentLength: bytes }),
+ );
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject(
+ {
+ ok: true,
+ },
+ );
+ expect(startVideoProcessingWorkflowMock).toHaveBeenCalledOnce();
+ expect(startLoomFileProcessingWorkflowMock).not.toHaveBeenCalled();
+ },
+ );
+
+ it("keeps non-S3 storage on its existing processing path", async () => {
+ whereMock
+ .mockReturnValueOnce(withLimit([recording]))
+ .mockReturnValueOnce(withLimit([uploaded]));
+ storageGetAccessForVideoMock.mockReturnValue(
+ Effect.succeed([
+ { provider: "google-drive", headObject: uploadedFileHeadMock },
+ ]),
+ );
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({
+ ok: true,
+ });
+ expect(startVideoProcessingWorkflowMock).toHaveBeenCalledOnce();
+ expect(startLoomFileProcessingWorkflowMock).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ { records: [] },
+ { records: [{ ...recording, ownerEmail: "[email protected]" }] },
+ ])(
+ "refuses missing or differently owned recordings without creating one",
+ async ({ records }) => {
+ whereMock.mockReturnValueOnce(withLimit(records));
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject(
+ {
+ ok: false,
+ error: expect.stringContaining("matches this request and owner"),
+ },
+ );
+ expect(mockDb.insert).not.toHaveBeenCalled();
+ expect(mockDb.update).not.toHaveBeenCalled();
+ expect(storageGetAccessForVideoMock).not.toHaveBeenCalled();
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each(["processing", "complete"])(
+ "does not start another job for a recording in phase %s",
+ async (phase) => {
+ whereMock
+ .mockReturnValueOnce(withLimit([recording]))
+ .mockReturnValueOnce(withLimit([{ ...uploaded, phase }]));
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject(
+ {
+ ok: false,
+ error: expect.stringContaining("no longer available"),
+ },
+ );
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ },
+ );
+
+ it("rejects an empty uploaded file", async () => {
+ whereMock
+ .mockReturnValueOnce(withLimit([recording]))
+ .mockReturnValueOnce(withLimit([uploaded]));
+ uploadedFileHeadMock.mockReturnValue(Effect.succeed({ ContentLength: 0 }));
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({
+ ok: false,
+ error: "The uploaded file is empty.",
+ });
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ });
+
+ it("rejects an oversized upload before starting any processing", async () => {
+ whereMock
+ .mockReturnValueOnce(withLimit([recording]))
+ .mockReturnValueOnce(withLimit([uploaded]));
+ uploadedFileHeadMock.mockReturnValue(
+ Effect.succeed({ ContentLength: 11 * 1024 ** 3 }),
+ );
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({
+ ok: false,
+ error: expect.stringContaining("exceeds 10 GB"),
+ });
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ });
+
+ it("stops if ownership changes during resume preparation", async () => {
+ whereMock
+ .mockReturnValueOnce(withLimit([recording]))
+ .mockReturnValueOnce(
+ withLimit([
+ { ...uploaded, video: { ...uploaded.video, ownerId: "new-owner" } },
+ ]),
+ );
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({
+ ok: false,
+ error: expect.stringContaining("no longer available"),
+ });
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ });
+
+ it("does not start when the stored upload cannot be found", async () => {
+ whereMock
+ .mockReturnValueOnce(withLimit([recording]))
+ .mockReturnValueOnce(withLimit([uploaded]));
+ uploadedFileHeadMock.mockReturnValue(Effect.fail(new Error("Not found")));
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({
+ ok: false,
+ error: expect.stringContaining("not found in storage"),
+ });
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ });
+
+ it("requires an operator before reading any recording", async () => {
+ conciergeMocks.requireMigrationOperator.mockRejectedValueOnce(
+ new Error("Unauthorized"),
+ );
+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");
+ await expect(resumeConciergeLoomFileUpload(input)).rejects.toThrow(
+ "Unauthorized",
+ );
+ expect(mockDb.select).not.toHaveBeenCalled();
+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-media-dispatch.test.ts b/apps/web/__tests__/unit/loom-media-dispatch.test.ts
new file mode 100644
index 000000000..f0fbaf347
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-media-dispatch.test.ts
@@ -0,0 +1,51 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("@cap/env", () => ({
+ serverEnv: () => ({
+ MEDIA_SERVER_URL: "https://worker.example.com",
+ MEDIA_SERVER_WEBHOOK_SECRET: "fixture-secret",
+ }),
+}));
+
+import { isLoomMediaJobFailed } from "@/lib/loom-media-dispatch";
+
+describe("Loom worker failure reconciliation", () => {
+ afterEach(() => vi.unstubAllGlobals());
+
+ it.each([
+ [{ jobId: "job", videoId: "video", phase: "error" }, true],
+ [{ jobId: "other", videoId: "video", phase: "error" }, false],
+ [{ jobId: "job", videoId: "other", phase: "error" }, false],
+ [{ jobId: "job", videoId: "video", phase: "processing" }, false],
+ [{ jobId: "job", videoId: "video", phase: "complete" }, false],
+ [{ jobId: "job", videoId: "video", phase: "cancelled" }, false],
+ [{}, false],
+ ])(
+ "requires the exact job, recording and failed phase: %j",
+ async (body, failed) => {
+ const fetch = vi.fn().mockResolvedValue(Response.json(body));
+ vi.stubGlobal("fetch", fetch);
+ expect(await isLoomMediaJobFailed("job", "video")).toBe(failed);
+ expect(fetch).toHaveBeenCalledWith(
+ "https://worker.example.com/video/process/job/status",
+ expect.objectContaining({ redirect: "error" }),
+ );
+ },
+ );
+
+ it.each([404, 500])(
+ "does not treat HTTP %s as a failed job",
+ async (status) => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(new Response(null, { status })),
+ );
+ expect(await isLoomMediaJobFailed("job", "video")).toBe(false);
+ },
+ );
+
+ it("holds an unavailable worker instead of authorizing another dispatch", async () => {
+ vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new Error("Timed out")));
+ expect(await isLoomMediaJobFailed("job", "video")).toBe(false);
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-migration-inventory.test.ts b/apps/web/__tests__/unit/loom-migration-inventory.test.ts
new file mode 100644
index 000000000..0208fdd6c
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-migration-inventory.test.ts
@@ -0,0 +1,161 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ operator: vi.fn(),
+ transaction: vi.fn(),
+ rows: [] as unknown[][],
+ select: vi.fn(),
+ delete: vi.fn(),
+ deleteWhere: vi.fn(),
+ update: vi.fn(),
+ set: vi.fn(),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({ transaction: mocks.transaction }),
+}));
+vi.mock("@cap/database/schema", () => ({
+ loomMigrationRequests: { id: "request.id" },
+ loomMigrationImports: {
+ requestId: "inventory.requestId",
+ videoId: "inventory.videoId",
+ },
+ videos: { id: "video.id" },
+}));
+vi.mock("drizzle-orm", () => ({
+ and: (...conditions: unknown[]) => ({ and: conditions }),
+ eq: (left: unknown, right: unknown) => ({ eq: [left, right] }),
+ inArray: (column: unknown, values: unknown[]) => ({
+ inArray: [column, values],
+ }),
+ isNull: (column: unknown) => ({ isNull: column }),
+ notExists: (query: unknown) => ({ notExists: query }),
+}));
+vi.mock("@/lib/loom-concierge", () => ({
+ requireMigrationOperator: mocks.operator,
+}));
+
+import { reconcileMissingLoomMigrationImports } from "@/lib/loom-migration-inventory";
+
+const input = { requestId: "request12345678", verified: true };
+const request = {
+ status: "in_progress",
+ organizationId: "destination",
+ activeOrganizationId: "destination",
+ activeImportCount: 0,
+};
+
+describe("operator migration inventory reconciliation", () => {
+ beforeEach(() => {
+ vi.resetAllMocks();
+ mocks.rows = [[request], []];
+ mocks.operator.mockResolvedValue({ id: "operator" });
+ mocks.select.mockImplementation(() => {
+ const builder = {
+ from: vi.fn().mockReturnThis(),
+ leftJoin: vi.fn().mockReturnThis(),
+ where: vi.fn().mockReturnThis(),
+ limit: vi.fn().mockReturnThis(),
+ for: vi.fn(async () => mocks.rows.shift() ?? []),
+ };
+ return builder;
+ });
+ mocks.delete.mockReturnValue({ where: mocks.deleteWhere });
+ mocks.deleteWhere.mockResolvedValue([{ affectedRows: 1 }]);
+ mocks.set.mockReturnValue({ where: vi.fn().mockResolvedValue(undefined) });
+ mocks.update.mockReturnValue({ set: mocks.set });
+ mocks.transaction.mockImplementation(async (fn: (tx: unknown) => unknown) =>
+ fn({ select: mocks.select, delete: mocks.delete, update: mocks.update }),
+ );
+ });
+
+ it("requires operator access before reading migration state", async () => {
+ mocks.operator.mockRejectedValue(new Error("Forbidden"));
+ await expect(reconcileMissingLoomMigrationImports(input)).rejects.toThrow(
+ "Forbidden",
+ );
+ expect(mocks.transaction).not.toHaveBeenCalled();
+ });
+
+ it("requires an explicit source-list reconciliation confirmation", async () => {
+ await expect(
+ reconcileMissingLoomMigrationImports({ ...input, verified: false }),
+ ).rejects.toThrow("source list first");
+ expect(mocks.transaction).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ undefined,
+ { ...request, status: "completed" },
+ { ...request, activeOrganizationId: null },
+ { ...request, activeOrganizationId: "other" },
+ { ...request, activeImportCount: 1 },
+ {
+ ...request,
+ activeImportCount: 1,
+ activeImportLeaseUntil: new Date(Date.now() + 60_000),
+ },
+ ])("holds inactive requests and concurrent imports: %j", async (row) => {
+ mocks.rows = [row ? [row] : []];
+ await expect(reconcileMissingLoomMigrationImports(input)).rejects.toThrow(
+ "inactive or imports",
+ );
+ expect(mocks.delete).not.toHaveBeenCalled();
+ });
+
+ it("does not mutate requests without missing recordings", async () => {
+ await expect(reconcileMissingLoomMigrationImports(input)).resolves.toEqual({
+ removedCount: 0,
+ hasMore: false,
+ });
+ expect(mocks.delete).not.toHaveBeenCalled();
+ expect(mocks.update).not.toHaveBeenCalled();
+ });
+
+ it("permits inventory-only reconciliation after an import reservation expires", async () => {
+ mocks.rows = [
+ [
+ {
+ ...request,
+ activeImportCount: 1,
+ activeImportLeaseUntil: new Date(0),
+ },
+ ],
+ [],
+ ];
+ await expect(reconcileMissingLoomMigrationImports(input)).resolves.toEqual({
+ removedCount: 0,
+ hasMore: false,
+ });
+ expect(mocks.delete).not.toHaveBeenCalled();
+ });
+
+ it("bounds cleanup, rechecks absent videos and never changes completion or counts", async () => {
+ mocks.rows = [
+ [request],
+ Array.from({ length: 101 }, (_, i) => ({ videoId: `deleted-${i}` })),
+ ];
+ mocks.deleteWhere.mockResolvedValue([{ affectedRows: 100 }]);
+ await expect(reconcileMissingLoomMigrationImports(input)).resolves.toEqual({
+ removedCount: 100,
+ hasMore: true,
+ });
+ expect(mocks.delete).toHaveBeenCalledWith({
+ requestId: "inventory.requestId",
+ videoId: "inventory.videoId",
+ });
+ const conditions = mocks.deleteWhere.mock.calls[0]?.[0].and;
+ expect(conditions[0]).toEqual({
+ eq: ["inventory.requestId", input.requestId],
+ });
+ expect(conditions[1].inArray[1]).toHaveLength(100);
+ expect(conditions[2].notExists.where).toHaveBeenCalledWith({
+ eq: ["video.id", "inventory.videoId"],
+ });
+ expect(mocks.set).toHaveBeenCalledWith({
+ lastOperatorUserId: "operator",
+ lastOperatorAt: expect.any(Date),
+ updatedAt: expect.any(Date),
+ });
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-migration-state.test.ts b/apps/web/__tests__/unit/loom-migration-state.test.ts
index 87b1f90a5..74a30112c 100644
--- a/apps/web/__tests__/unit/loom-migration-state.test.ts
+++ b/apps/web/__tests__/unit/loom-migration-state.test.ts
@@ -1,5 +1,8 @@
import { describe, expect, it } from "vitest";
-import { validateOperatorMigrationUpdate } from "@/lib/loom-migration-state";
+import {
+ validateLoomMigrationInventory,
+ validateOperatorMigrationUpdate,
+} from "@/lib/loom-migration-state";
const base = {
currentStatus: "in_progress" as const,
@@ -37,3 +40,51 @@ describe("Loom migration completion", () => {
expect(validateOperatorMigrationUpdate(base)).toBe("Migration finished");
});
});
+
+describe("Loom migration destination inventory", () => {
+ const imported = {
+ videoId: "video",
+ videoOrganizationId: "destination",
+ mappingId: "video",
+ mappingOrganizationId: "destination",
+ source: "loom",
+ phase: null,
+ };
+
+ it("accepts completed recordings with and without retained upload rows", () => {
+ expect(() =>
+ validateLoomMigrationInventory({
+ organizationId: "destination",
+ imports: [imported, { ...imported, phase: "complete" }],
+ }),
+ ).not.toThrow();
+ });
+
+ it.each([
+ { videoId: null, videoOrganizationId: null },
+ { videoOrganizationId: "another-organization" },
+ { mappingId: null, mappingOrganizationId: null, source: null },
+ { mappingId: "another-recording" },
+ { mappingOrganizationId: "another-organization" },
+ { source: "another-source" },
+ ])("blocks a missing or changed destination: %j", (changes) => {
+ expect(() =>
+ validateLoomMigrationInventory({
+ organizationId: "destination",
+ imports: [imported, { ...imported, ...changes }],
+ }),
+ ).toThrow("missing or changed");
+ });
+
+ it.each(["uploading", "processing", "generating_thumbnail", "error"])(
+ "blocks an import that is %s",
+ (phase) => {
+ expect(() =>
+ validateLoomMigrationInventory({
+ organizationId: "destination",
+ imports: [imported, { ...imported, phase }],
+ }),
+ ).toThrow("still processing");
+ },
+ );
+});
diff --git a/apps/web/__tests__/unit/loom-retry-processing.test.ts b/apps/web/__tests__/unit/loom-retry-processing.test.ts
new file mode 100644
index 000000000..eb18ca41a
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-retry-processing.test.ts
@@ -0,0 +1,236 @@
+import type { Video } from "@cap/web-domain";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ where: vi.fn(),
+ set: vi.fn(),
+ start: vi.fn(),
+ currentUser: vi.fn(),
+ startProcessing: vi.fn(),
+ setError: vi.fn(),
+ isRunning: vi.fn(),
+ restoreStartError: vi.fn(),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({ from: () => ({ where: mocks.where }) }),
+ update: () => ({ set: mocks.set }),
+ }),
+}));
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: mocks.currentUser,
+}));
+vi.mock("@cap/database/schema", () => ({
+ videos: { id: "videoId" },
+ videoUploads: {
+ videoId: "uploadVideoId",
+ phase: "phase",
+ updatedAt: "updatedAt",
+ },
+ importedVideos: {
+ id: "importVideoId",
+ orgId: "importOrgId",
+ source: "source",
+ sourceId: "sourceId",
+ },
+}));
+vi.mock("drizzle-orm", () => ({
+ eq: (column: string, value: unknown) => ({ column, value }),
+ and: (...conditions: unknown[]) => ({ conditions }),
+}));
+vi.mock("workflow/api", () => ({ start: mocks.start }));
+vi.mock("@/workflows/import-loom-video", () => ({
+ importLoomVideoWorkflow: vi.fn(),
+}));
+vi.mock("@/lib/loom-import-start", () => ({
+ getLoomImportRunState: mocks.isRunning,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE: "Confirmation required",
+ restoreLoomImportStartError: mocks.restoreStartError,
+ startLoomImportWorkflow: (payload: unknown) =>
+ mocks.start(Symbol("workflow"), [payload]),
+ LoomImportStartError: class extends Error {
+ constructor(readonly canRetry: boolean) {
+ super("Loom import startup failed");
+ }
+ },
+}));
+vi.mock("@/lib/video-processing", () => ({
+ startVideoProcessingWorkflow: mocks.startProcessing,
+ setVideoProcessingError: mocks.setError,
+}));
+
+import { retryVideoProcessing } from "@/actions/video/retry-processing";
+
+const videoId = "video-1" as Video.VideoId;
+const video = { id: videoId, ownerId: "owner-1", orgId: "org-1", bucket: null };
+const updatedAt = new Date();
+const upload = {
+ phase: "error",
+ rawFileKey: null,
+ updatedAt,
+ processingProgress: 0,
+};
+
+describe("Loom processing retries", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.currentUser.mockResolvedValue({ id: "owner-1" });
+ mocks.set.mockReturnValue({ where: mocks.where });
+ mocks.start.mockResolvedValue({});
+ mocks.isRunning.mockResolvedValue("inactive");
+ });
+
+ it.each([true, false])(
+ "only restores the processing claim after a definite startup rejection: %s",
+ async (canRetry) => {
+ mocks.where
+ .mockResolvedValueOnce([video])
+ .mockResolvedValueOnce([upload])
+ .mockResolvedValueOnce([{ source: "loom", sourceId: "loom-1" }])
+ .mockResolvedValueOnce([{ affectedRows: 1 }]);
+ const { LoomImportStartError } = await import("@/lib/loom-import-start");
+ mocks.start.mockRejectedValueOnce(
+ new LoomImportStartError(canRetry, new Error()),
+ );
+ await expect(retryVideoProcessing({ videoId })).rejects.toThrow(
+ "Loom import startup failed",
+ );
+ if (canRetry)
+ expect(mocks.restoreStartError).toHaveBeenCalledWith(
+ videoId,
+ "processing",
+ mocks.set.mock.calls[0]?.[0].updatedAt,
+ "Loom import startup failed",
+ );
+ else expect(mocks.restoreStartError).not.toHaveBeenCalled();
+ expect(mocks.setError).not.toHaveBeenCalled();
+ },
+ );
+
+ it("holds a stale processing row while its workflow is still active", async () => {
+ mocks.where
+ .mockResolvedValueOnce([video])
+ .mockResolvedValueOnce([
+ { ...upload, phase: "processing", updatedAt: new Date(0) },
+ ])
+ .mockResolvedValueOnce([{ source: "loom", sourceId: "loom-1" }]);
+ mocks.isRunning.mockResolvedValueOnce("active");
+ await expect(retryVideoProcessing({ videoId })).resolves.toEqual({
+ success: true,
+ status: "already-processing",
+ });
+ expect(mocks.set).not.toHaveBeenCalled();
+ expect(mocks.start).not.toHaveBeenCalled();
+ });
+
+ it("reports missing provider confirmation without another retry", async () => {
+ mocks.where
+ .mockResolvedValueOnce([video])
+ .mockResolvedValueOnce([upload])
+ .mockResolvedValueOnce([{ source: "loom", sourceId: "loom-1" }]);
+ mocks.isRunning.mockResolvedValueOnce("unconfirmed");
+ await expect(retryVideoProcessing({ videoId })).rejects.toThrow(
+ "Confirmation required",
+ );
+ expect(mocks.set).not.toHaveBeenCalled();
+ expect(mocks.start).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ { rawFileKey: null, updatedAt: new Date() },
+ { rawFileKey: null, updatedAt: new Date(0) },
+ { rawFileKey: "owner-1/video-1/raw-upload.mp4", updatedAt: new Date() },
+ { rawFileKey: "owner-1/video-1/raw-upload.mp4", updatedAt: new Date(0) },
+ ])(
+ "does not replace an uploading import without a startup receipt: %j",
+ async (uploading) => {
+ mocks.where
+ .mockResolvedValueOnce([video])
+ .mockResolvedValueOnce([
+ { ...upload, ...uploading, phase: "uploading" },
+ ])
+ .mockResolvedValueOnce([{ source: "loom", sourceId: "loom-1" }])
+ .mockResolvedValueOnce([{ affectedRows: 1 }]);
+ await expect(retryVideoProcessing({ videoId })).resolves.toEqual({
+ success: true,
+ status: "already-processing",
+ });
+ expect(mocks.set).not.toHaveBeenCalled();
+ expect(mocks.start).not.toHaveBeenCalled();
+ expect(mocks.startProcessing).not.toHaveBeenCalled();
+ },
+ );
+
+ it("recovers a source lookup failure that has no raw file key", async () => {
+ mocks.where
+ .mockResolvedValueOnce([video])
+ .mockResolvedValueOnce([upload])
+ .mockResolvedValueOnce([{ source: "loom", sourceId: "loom-1" }])
+ .mockResolvedValueOnce([{ affectedRows: 1 }]);
+ await expect(retryVideoProcessing({ videoId })).resolves.toEqual({
+ success: true,
+ status: "started",
+ });
+ expect(mocks.where.mock.calls[2]?.[0]).toEqual({
+ conditions: [
+ { column: "importVideoId", value: videoId },
+ { column: "importOrgId", value: "org-1" },
+ ],
+ });
+ expect(mocks.start).toHaveBeenCalledWith(expect.anything(), [
+ expect.objectContaining({
+ videoId,
+ rawFileKey: "owner-1/video-1/raw-upload.mp4",
+ loomVideoId: "loom-1",
+ reuseExistingRawUpload: true,
+ }),
+ ]);
+ expect(mocks.startProcessing).not.toHaveBeenCalled();
+ });
+
+ it("does not dispatch after losing the processing claim", async () => {
+ mocks.where
+ .mockResolvedValueOnce([video])
+ .mockResolvedValueOnce([upload])
+ .mockResolvedValueOnce([{ source: "loom", sourceId: "loom-1" }])
+ .mockResolvedValueOnce([{ affectedRows: 0 }]);
+ await expect(retryVideoProcessing({ videoId })).resolves.toEqual({
+ success: true,
+ status: "already-processing",
+ });
+ expect(mocks.start).not.toHaveBeenCalled();
+ });
+
+ it("leaves a recently active import running", async () => {
+ mocks.where
+ .mockResolvedValueOnce([video])
+ .mockResolvedValueOnce([{ ...upload, phase: "processing" }])
+ .mockResolvedValueOnce([{ source: "loom", sourceId: "loom-1" }]);
+ await expect(retryVideoProcessing({ videoId })).resolves.toEqual({
+ success: true,
+ status: "already-processing",
+ });
+ expect(mocks.set).not.toHaveBeenCalled();
+ expect(mocks.start).not.toHaveBeenCalled();
+ });
+
+ it("requires a raw file for a regular upload", async () => {
+ mocks.where
+ .mockResolvedValueOnce([video])
+ .mockResolvedValueOnce([upload])
+ .mockResolvedValueOnce([]);
+ await expect(retryVideoProcessing({ videoId })).rejects.toThrow(
+ "No raw file key found for retry",
+ );
+ expect(mocks.start).not.toHaveBeenCalled();
+ });
+
+ it("rejects a different owner's video", async () => {
+ mocks.where.mockResolvedValueOnce([{ ...video, ownerId: "other-owner" }]);
+ await expect(retryVideoProcessing({ videoId })).rejects.toThrow(
+ "Unauthorized",
+ );
+ expect(mocks.start).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/media-comment-playback-cleanup.test.ts b/apps/web/__tests__/unit/media-comment-playback-cleanup.test.ts
new file mode 100644
index 000000000..1f89690d6
--- /dev/null
+++ b/apps/web/__tests__/unit/media-comment-playback-cleanup.test.ts
@@ -0,0 +1,249 @@
+// @vitest-environment jsdom
+
+import {
+ act,
+ type ComponentProps,
+ createElement,
+ Fragment,
+ forwardRef,
+ type PropsWithChildren,
+} from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { CommentMiniPlayer } from "@/app/s/[videoId]/_components/media-comment/CommentMiniPlayer";
+import type { MediaComment } from "@/app/s/[videoId]/_components/media-comment/media-comment-types";
+import {
+ stopAll,
+ useMainVideoInterlock,
+} from "@/app/s/[videoId]/_components/media-comment/media-playback-registry";
+import { VideoCommentCard } from "@/app/s/[videoId]/_components/media-comment/VideoCommentCard";
+import {
+ createPlaybackStore,
+ type PlaybackStore,
+} from "@/app/s/[videoId]/_components/playback/playback-store";
+
+const mocks = vi.hoisted(() => ({
+ store: null as PlaybackStore | null,
+ miniPlayerReady: true,
+}));
+
+vi.mock("@/app/s/[videoId]/_components/playback/PlaybackContext", () => ({
+ useOptionalPlayback: () => mocks.store,
+}));
+
+vi.mock("motion/react", () => ({
+ motion: {
+ button: ({
+ whileHover: _whileHover,
+ whileTap: _whileTap,
+ ...props
+ }: ComponentProps<"button"> & {
+ whileHover?: unknown;
+ whileTap?: unknown;
+ }) => createElement("button", { ...props, type: "button" }),
+ },
+}));
+vi.mock("next/dynamic", () => ({
+ default: () => (props: ComponentProps<typeof CommentMiniPlayer>) =>
+ mocks.miniPlayerReady ? createElement(CommentMiniPlayer, props) : null,
+}));
+vi.mock("@cap/ui", () => ({
+ Dialog: ({ children }: PropsWithChildren) => children,
+ DialogContent: ({ children }: PropsWithChildren) => children,
+ DialogTitle: ({ children }: PropsWithChildren) => children,
+}));
+vi.mock("@/app/s/[videoId]/_components/video/media-player", () => {
+ const Container = ({ children }: PropsWithChildren) => children;
+ const Empty = () => null;
+ return {
+ MediaPlayer: Container,
+ MediaPlayerControls: Container,
+ MediaPlayerVideo: forwardRef<HTMLVideoElement, ComponentProps<"video">>(
+ (props, ref) => createElement("video", { ...props, ref }),
+ ),
+ MediaPlayerControlsOverlay: Empty,
+ MediaPlayerFullscreen: Empty,
+ MediaPlayerPlay: Empty,
+ MediaPlayerPlaybackSpeed: Empty,
+ MediaPlayerSeek: Empty,
+ MediaPlayerTime: Empty,
+ MediaPlayerVolume: Empty,
+ };
+});
+
+const comment: MediaComment = {
+ id: "fixture-comment" as MediaComment["id"],
+ videoId: "fixture-video" as MediaComment["videoId"],
+ type: "video",
+ content: "",
+ mediaKey: "fixture.mp4",
+ mediaDuration: 10,
+ mediaMeta: null,
+};
+
+function Interlock() {
+ useMainVideoInterlock();
+ return null;
+}
+
+let root: Root;
+let container: HTMLDivElement;
+let main: HTMLVideoElement;
+let mainPlaying: boolean;
+
+beforeEach(() => {
+ mocks.miniPlayerReady = true;
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ vi.spyOn(HTMLMediaElement.prototype, "pause").mockImplementation(() => {});
+ vi.spyOn(window, "requestAnimationFrame").mockReturnValue(1);
+ vi.spyOn(window, "cancelAnimationFrame").mockImplementation(() => {});
+ container = document.createElement("div");
+ main = document.createElement("video");
+ mainPlaying = false;
+ Object.defineProperty(main, "paused", { get: () => !mainPlaying });
+ vi.spyOn(main, "pause").mockImplementation(() => {
+ mainPlaying = false;
+ main.dispatchEvent(new Event("pause"));
+ });
+ vi.spyOn(main, "play").mockImplementation(async () => {
+ mainPlaying = true;
+ main.dispatchEvent(new Event("play"));
+ });
+ document.body.append(container, main);
+ root = createRoot(container);
+});
+
+afterEach(async () => {
+ await act(async () => root.unmount());
+ stopAll();
+ mocks.store?.detach();
+ mocks.store = null;
+ container.remove();
+ main.remove();
+ vi.unstubAllGlobals();
+});
+
+describe("video comment playback cleanup with fixture media state", () => {
+ async function mount(kind: "inline" | "mini", initiallyPlaying: boolean) {
+ mainPlaying = initiallyPlaying;
+ mocks.store = createPlaybackStore();
+ mocks.store.attach(main);
+ const render = (visible: boolean, interlockMounted = true) =>
+ act(async () => {
+ root.render(
+ createElement(
+ Fragment,
+ null,
+ interlockMounted ? createElement(Interlock) : null,
+ visible
+ ? kind === "inline"
+ ? createElement(VideoCommentCard, { comment })
+ : createElement(CommentMiniPlayer, {
+ comment,
+ src: "fixture.mp4",
+ onClose: () => undefined,
+ })
+ : null,
+ ),
+ );
+ });
+ await render(false);
+ await render(true);
+ if (kind === "inline") {
+ await act(async () => container.querySelector("button")?.click());
+ }
+ expect(container.querySelector("video")).not.toBeNull();
+ expect(mainPlaying).toBe(false);
+ return render;
+ }
+
+ it.each([
+ ["inline", true],
+ ["inline", false],
+ ["mini", true],
+ ["mini", false],
+ ] as const)(
+ "removing %s restores the main video's prior playing state of %s",
+ async (kind, initiallyPlaying) => {
+ const render = await mount(kind, initiallyPlaying);
+ await render(false);
+ expect(mainPlaying).toBe(initiallyPlaying);
+ expect(main.play).toHaveBeenCalledTimes(initiallyPlaying ? 1 : 0);
+ },
+ );
+
+ it("keeps the main video paused through inline-to-dialog handoff", async () => {
+ const render = await mount("inline", true);
+ await act(async () => {
+ container
+ .querySelector<HTMLButtonElement>('[aria-label="Open in mini player"]')
+ ?.click();
+ });
+ expect(container.textContent).toContain("Video comment");
+ expect(main.play).not.toHaveBeenCalled();
+ await render(false);
+ expect(main.play).toHaveBeenCalledTimes(1);
+ expect(mainPlaying).toBe(true);
+ });
+
+ it("returns playback when removed before the mini player loads", async () => {
+ const render = await mount("inline", true);
+ mocks.miniPlayerReady = false;
+ await act(async () => {
+ container
+ .querySelector<HTMLButtonElement>('[aria-label="Open in mini player"]')
+ ?.click();
+ });
+ expect(container.querySelector("video")).toBeNull();
+ expect(main.play).not.toHaveBeenCalled();
+ await render(false);
+ expect(main.play).toHaveBeenCalledTimes(1);
+ expect(mainPlaying).toBe(true);
+ });
+
+ it.each(["close", "ended"] as const)(
+ "can play another inline comment after %s ends the interaction",
+ async (ending) => {
+ const render = await mount("inline", true);
+ await act(async () => {
+ if (ending === "close") {
+ container
+ .querySelector<HTMLButtonElement>(
+ '[aria-label="Close video comment"]',
+ )
+ ?.click();
+ } else {
+ container.querySelector("video")?.dispatchEvent(new Event("ended"));
+ }
+ });
+ expect(mainPlaying).toBe(true);
+ expect(main.play).toHaveBeenCalledTimes(1);
+ await act(async () => container.querySelector("button")?.click());
+ expect(mainPlaying).toBe(false);
+ await render(false);
+ expect(main.play).toHaveBeenCalledTimes(2);
+ expect(mainPlaying).toBe(true);
+ },
+ );
+
+ it.each(["inline", "mini"] as const)(
+ "returns playback before the last interlock unmounts with %s",
+ async (kind) => {
+ const render = await mount(kind, true);
+ await render(false, false);
+ expect(main.play).toHaveBeenCalledTimes(1);
+ expect(mainPlaying).toBe(true);
+ },
+ );
+
+ it.each(["inline", "mini"] as const)(
+ "does not resume twice after the main video takes over from %s",
+ async (kind) => {
+ const render = await mount(kind, true);
+ await act(async () => main.play());
+ await render(false);
+ expect(main.play).toHaveBeenCalledTimes(1);
+ expect(mainPlaying).toBe(true);
+ },
+ );
+});
diff --git a/apps/web/__tests__/unit/media-processing-workflows.test.ts b/apps/web/__tests__/unit/media-processing-workflows.test.ts
index 57deff5af..5e8fb252b 100644
--- a/apps/web/__tests__/unit/media-processing-workflows.test.ts
+++ b/apps/web/__tests__/unit/media-processing-workflows.test.ts
@@ -11,6 +11,9 @@ const mocks = vi.hoisted(() => ({
head: vi.fn(),
remove: vi.fn(),
fetch: vi.fn(),
+ statusFetch: vi.fn(),
+ dispatch: vi.fn(),
+ mediaJobFailed: vi.fn(),
sleep: vi.fn(),
}));
@@ -62,6 +65,13 @@ vi.mock("@/lib/ai-generation-entitlement", () => ({
vi.mock("workflow", () => ({
sleep: mocks.sleep,
FatalError: class FatalError extends Error {},
+ getWorkflowMetadata: () => ({ workflowRunId: "run-import" }),
+ RetryableError: class RetryableError extends Error {},
+}));
+
+vi.mock("@/lib/loom-media-dispatch", () => ({
+ recordLoomMediaDispatch: mocks.dispatch,
+ isLoomMediaJobFailed: mocks.mediaJobFailed,
}));
import { importLoomVideoWorkflow } from "@/workflows/import-loom-video";
@@ -76,6 +86,7 @@ const payload = {
};
const video = {
id: "video",
+ ownerId: "owner",
source: { type: "webMP4" },
createdAt: new Date(),
updatedAt: new Date(),
@@ -83,6 +94,17 @@ const video = {
const metadata = { duration: 30, width: 1920, height: 1080, fps: 30 };
const pending = { phase: "processing", processingProgress: 25 };
+function storageError(cause: unknown) {
+ return { _tag: "StorageError", cause: { _tag: "S3Error", cause } };
+}
+
+function s3Error(name: string, httpStatusCode: number) {
+ return Object.assign(new Error(name), {
+ name,
+ $metadata: { httpStatusCode },
+ });
+}
+
describe("media processing workflows", () => {
beforeEach(() => {
mocks.rows = [];
@@ -110,8 +132,20 @@ describe("media processing workflows", () => {
Effect.succeed({ ContentLength: 2048 }),
);
mocks.remove.mockImplementation(() => Effect.void);
+ mocks.dispatch.mockReset().mockResolvedValue(undefined);
+ mocks.mediaJobFailed.mockReset().mockResolvedValue(true);
mocks.sleep.mockResolvedValue(undefined);
- vi.stubGlobal("fetch", mocks.fetch);
+ mocks.statusFetch.mockReset().mockImplementation(async () =>
+ Response.json({
+ loomMultipartUploads: true,
+ loomImportMaxBytes: 10 * 1024 ** 3,
+ }),
+ );
+ vi.stubGlobal("fetch", (url: string, init?: RequestInit) =>
+ url.endsWith("/video/status")
+ ? mocks.statusFetch(url, init)
+ : mocks.fetch(url, init),
+ );
mocks.fetch.mockReset().mockImplementation(async (url: string) => {
if (url.startsWith("https://www.loom.com/")) {
return Response.json({ url: "https://cdn.loom.com/original.mp4" });
@@ -213,6 +247,125 @@ describe("media processing workflows", () => {
);
});
+ it("uses a fresh preflight source without requesting it again", async () => {
+ const expires = Math.floor(Date.now() / 1000) + 7200;
+ const loomDownloadUrl = `https://cdn.loom.com/original.mp4?Expires=${expires}`;
+ mocks.rows = [[video], [pending], [], [metadata]];
+ await importLoomVideoWorkflow({ ...payload, loomDownloadUrl });
+ expect(mocks.fetch).toHaveBeenCalledOnce();
+ expect(JSON.parse(mocks.fetch.mock.calls[0]?.[1].body).videoUrl).toBe(
+ loomDownloadUrl,
+ );
+ });
+
+ it("refreshes an expired preflight source before dispatch", async () => {
+ mocks.rows = [[video], [pending], [], [metadata]];
+ await importLoomVideoWorkflow({
+ ...payload,
+ loomDownloadUrl: "https://cdn.loom.com/original.mp4?Expires=1",
+ });
+ expect(mocks.fetch).toHaveBeenCalledTimes(2);
+ expect(JSON.parse(mocks.fetch.mock.calls[1]?.[1].body).videoUrl).toBe(
+ "https://cdn.loom.com/original.mp4",
+ );
+ });
+
+ it.each([false, true])(
+ "refreshes a source waiting for worker capacity only when it expires: %s",
+ async (expiresDuringWait) => {
+ const now = Date.now();
+ const clock = vi.spyOn(Date, "now").mockReturnValue(now);
+ const expires = Math.floor(now / 1000) + 7200;
+ const loomDownloadUrl = `https://cdn.loom.com/original.mp4?Expires=${expires}`;
+ let workerAttempts = 0;
+ mocks.rows = [[video], [video], [], [metadata]];
+ mocks.fetch.mockImplementation(async (url: string) => {
+ if (url.startsWith("https://www.loom.com/")) {
+ return Response.json({ url: "https://cdn.loom.com/refreshed.mp4" });
+ }
+ workerAttempts++;
+ return workerAttempts <= 2
+ ? Response.json(
+ { code: "SERVER_BUSY", error: "Server is busy" },
+ { status: 503 },
+ )
+ : Response.json({ jobId: "job-1" });
+ });
+ mocks.sleep.mockImplementationOnce(async () => {
+ if (expiresDuringWait) clock.mockReturnValue(now + 2 * 60 * 60 * 1000);
+ });
+ try {
+ await importLoomVideoWorkflow({ ...payload, loomDownloadUrl });
+ expect(workerAttempts).toBe(3);
+ expect(
+ mocks.fetch.mock.calls.filter(([url]) =>
+ String(url).startsWith("https://www.loom.com/"),
+ ),
+ ).toHaveLength(expiresDuringWait ? 1 : 0);
+ expect(
+ JSON.parse(mocks.fetch.mock.calls.at(-1)?.[1].body),
+ ).toMatchObject({
+ videoUrl: expiresDuringWait
+ ? "https://cdn.loom.com/refreshed.mp4"
+ : loomDownloadUrl,
+ priority: "bulk",
+ });
+ } finally {
+ clock.mockRestore();
+ }
+ },
+ );
+
+ it("does not re-import from Loom when the preserved upload check fails transiently", async () => {
+ mocks.head.mockReturnValue(
+ Effect.fail(storageError(s3Error("ServiceUnavailable", 503))),
+ );
+ mocks.rows = [[video]];
+ await expect(
+ importLoomVideoWorkflow({ ...payload, reuseExistingRawUpload: true }),
+ ).rejects.toThrow();
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ expect(mocks.put).not.toHaveBeenCalled();
+ });
+
+ it("does not re-import from Loom when the preserved upload check hits a network error", async () => {
+ mocks.head.mockReturnValue(
+ Effect.fail(storageError(new Error("socket hang up"))),
+ );
+ mocks.rows = [[video]];
+ await expect(
+ importLoomVideoWorkflow({ ...payload, reuseExistingRawUpload: true }),
+ ).rejects.toThrow();
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ expect(mocks.put).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ ["storage", storageError(s3Error("NotFound", 404))],
+ [
+ "Google Drive",
+ {
+ _tag: "StorageError",
+ cause: Object.assign(new Error("Storage object not found"), {
+ name: "GoogleDriveRequestError",
+ status: 404,
+ }),
+ },
+ ],
+ ])(
+ "re-imports from Loom when %s reports the preserved upload missing",
+ async (_, error) => {
+ mocks.head.mockReturnValue(Effect.fail(error));
+ mocks.rows = [[video], [video], [pending], [], [metadata]];
+ await expect(
+ importLoomVideoWorkflow({ ...payload, reuseExistingRawUpload: true }),
+ ).resolves.toMatchObject({ success: true });
+ expect(mocks.fetch.mock.calls.at(-1)?.[0]).toBe(
+ "https://worker.example.com/video/import",
+ );
+ },
+ );
+
it("retries a failed Loom worker from its preserved original", async () => {
mocks.rows = [
[video],
@@ -234,6 +387,80 @@ describe("media processing workflows", () => {
);
});
+ it("does not retry a failed workflow while its accepted worker is unconfirmed", async () => {
+ mocks.mediaJobFailed.mockResolvedValue(false);
+ mocks.rows = [
+ [video],
+ [{ ...pending, processingError: "Processing status interrupted" }],
+ ];
+ await expect(importLoomVideoWorkflow(payload)).rejects.toThrow(
+ "must be reconciled before retrying",
+ );
+ expect(mocks.mediaJobFailed).toHaveBeenCalledWith("job-1", "video");
+ expect(
+ mocks.fetch.mock.calls.filter(([url]) =>
+ url.startsWith("https://worker.example.com/"),
+ ),
+ ).toHaveLength(1);
+ });
+
+ it("blocks an old worker before creating upload targets or dispatching", async () => {
+ mocks.statusFetch.mockResolvedValue(Response.json({}));
+ await expect(importLoomVideoWorkflow(payload)).rejects.toThrow(
+ "must be updated for 10 GB",
+ );
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ expect(mocks.put).not.toHaveBeenCalled();
+ expect(mocks.dispatch).not.toHaveBeenCalled();
+ });
+
+ it("leaves an uncertain dispatch receipt after a worker request times out", async () => {
+ mocks.rows = [[video]];
+ mocks.fetch.mockImplementation(async (url: string) => {
+ if (url.startsWith("https://www.loom.com/"))
+ return Response.json({ url: "https://cdn.loom.com/original.mp4" });
+ throw new Error("Response timed out after dispatch");
+ });
+ await expect(importLoomVideoWorkflow(payload)).rejects.toThrow("timed out");
+ expect(mocks.dispatch).toHaveBeenCalledTimes(1);
+ expect(mocks.dispatch).toHaveBeenCalledWith(
+ expect.objectContaining({
+ runId: "run-import",
+ attemptId: "run-import:0",
+ state: "uncertain",
+ }),
+ );
+ expect(
+ mocks.fetch.mock.calls.filter(([url]) =>
+ String(url).startsWith("https://worker.example.com/"),
+ ),
+ ).toHaveLength(1);
+ });
+
+ it.each([
+ [503, { error: "Upstream response unavailable" }],
+ [200, {}],
+ ])(
+ "holds an ambiguous HTTP %s worker response without redispatching",
+ async (status, body) => {
+ mocks.rows = [[video]];
+ mocks.fetch.mockImplementation(async (url: string) =>
+ url.startsWith("https://www.loom.com/")
+ ? Response.json({ url: "https://cdn.loom.com/original.mp4" })
+ : Response.json(body, { status }),
+ );
+ await expect(importLoomVideoWorkflow(payload)).rejects.toThrow();
+ expect(mocks.dispatch).toHaveBeenCalledExactlyOnceWith(
+ expect.objectContaining({ state: "uncertain" }),
+ );
+ expect(
+ mocks.fetch.mock.calls.filter(([url]) =>
+ url.startsWith("https://worker.example.com/"),
+ ),
+ ).toHaveLength(1);
+ },
+ );
+
it("supports Loom streaming fallbacks without reading a nonexistent raw object", async () => {
mocks.fetch.mockImplementation(async (url: string) =>
Response.json(
diff --git a/apps/web/__tests__/unit/media-server-multipart.test.ts b/apps/web/__tests__/unit/media-server-multipart.test.ts
index 886171c3a..add20925a 100644
--- a/apps/web/__tests__/unit/media-server-multipart.test.ts
+++ b/apps/web/__tests__/unit/media-server-multipart.test.ts
@@ -364,4 +364,115 @@ describe("media-server multipart fencing", () => {
expect((await request("unexpected")).status).toBe(404);
expect(mocks.storage).not.toHaveBeenCalled();
});
+
+ function loomUpload(source = true) {
+ const sourceKey = `${ownerId}/${videoId}/raw-upload.mp4`;
+ const resultKey = `${ownerId}/${videoId}/result.mp4`;
+ const row = {
+ id: videoId,
+ ownerId,
+ orgId: "organization",
+ metadata: {
+ loomImportUpload: {
+ attemptId: "loom-run:0",
+ ownerId,
+ organizationId: "organization",
+ source: { key: sourceKey, uploadId: "source-upload" },
+ output: { key: resultKey, uploadId: "result-upload" },
+ },
+ },
+ };
+ mocks.rows = [row];
+ return {
+ row,
+ body: {
+ videoId,
+ key: source ? sourceKey : resultKey,
+ uploadId: source ? "source-upload" : "result-upload",
+ loomImportAttemptId: "loom-run:0",
+ partNumber: 1,
+ contentLength: 64 * 1024 ** 2,
+ parts: [{ partNumber: 1, etag: '"part-1"', size: 64 * 1024 ** 2 }],
+ },
+ };
+ }
+
+ it.each([true, false])(
+ "signs only the registered Loom upload and preserves existing objects (source=%s)",
+ async (source) => {
+ const { body } = loomUpload(source);
+ expect((await request("sign-part", body)).status).toBe(200);
+ expect(mocks.signPart).toHaveBeenCalledWith(body.key, body.uploadId, 1, {
+ ContentLength: body.contentLength,
+ });
+ expect((await request("complete", body)).status).toBe(200);
+ expect(mocks.complete).toHaveBeenCalledWith(
+ body.key,
+ body.uploadId,
+ expect.objectContaining({ IfNoneMatch: "*" }),
+ );
+ },
+ );
+
+ it.each([
+ { loomImportAttemptId: "old-run:0" },
+ { loomImportAttemptId: undefined },
+ { uploadId: "unregistered-upload" },
+ { key: "other-owner/other-video/raw-upload.mp4" },
+ ])("blocks a stale or substituted Loom upload %#", async (change) => {
+ const { body } = loomUpload(false);
+ expect((await request("sign-part", { ...body, ...change })).status).toBe(
+ 409,
+ );
+ expect((await request("complete", { ...body, ...change })).status).toBe(
+ 409,
+ );
+ expect(mocks.signPart).not.toHaveBeenCalled();
+ expect(mocks.complete).not.toHaveBeenCalled();
+ });
+
+ it("blocks Loom upload finalization after a destination move", async () => {
+ const { body, row } = loomUpload();
+ row.orgId = "another-organization";
+ expect((await request("complete", body)).status).toBe(409);
+ expect(mocks.complete).not.toHaveBeenCalled();
+ });
+
+ it("blocks callbacks after a replacement workflow takes ownership", async () => {
+ const { body, row } = loomUpload();
+ mocks.rows = [
+ {
+ ...row,
+ metadata: {
+ ...row.metadata,
+ loomImportRun: { runId: "replacement-run", dispatch: "accepted" },
+ },
+ },
+ ];
+ expect((await request("sign-part", body)).status).toBe(409);
+ expect((await request("complete", body)).status).toBe(409);
+ expect((await request("abort", body)).status).toBe(409);
+ expect(mocks.signPart).not.toHaveBeenCalled();
+ expect(mocks.complete).not.toHaveBeenCalled();
+ expect(mocks.abort).not.toHaveBeenCalled();
+ });
+
+ it("accepts exactly 10 GB and rejects a further multipart part", async () => {
+ const { body } = loomUpload();
+ const parts = Array.from({ length: 160 }, (_, index) => ({
+ partNumber: index + 1,
+ etag: `"part-${index + 1}"`,
+ size: 64 * 1024 ** 2,
+ }));
+ expect((await request("complete", { ...body, parts })).status).toBe(200);
+ mocks.complete.mockClear();
+ expect(
+ (await request("sign-part", { ...body, partNumber: 161 })).status,
+ ).toBe(400);
+ expect(
+ (await request("complete", { ...body, parts: [...parts, parts[0]] }))
+ .status,
+ ).toBe(400);
+ expect(mocks.complete).not.toHaveBeenCalled();
+ });
});
diff --git a/apps/web/__tests__/unit/media-server-progress.test.ts b/apps/web/__tests__/unit/media-server-progress.test.ts
index f4e817178..ff6a9a157 100644
--- a/apps/web/__tests__/unit/media-server-progress.test.ts
+++ b/apps/web/__tests__/unit/media-server-progress.test.ts
@@ -256,15 +256,19 @@ function databaseFixture(initial: DesktopRecordingJob | null = fixture().job) {
function request(
body: unknown = fixture().payload,
secret: string | null = "media-secret",
+ search = "",
) {
const headers = new Headers({ "Content-Type": "application/json" });
if (secret !== null) headers.set("x-media-server-secret", secret);
return POST(
- new NextRequest("https://cap.so/api/webhooks/media-server/progress", {
- method: "POST",
- headers,
- body: JSON.stringify(body),
- }),
+ new NextRequest(
+ `https://cap.so/api/webhooks/media-server/progress${search}`,
+ {
+ method: "POST",
+ headers,
+ body: JSON.stringify(body),
+ },
+ ),
);
}
@@ -748,4 +752,61 @@ describe("media-server recording progress webhook", () => {
expect(database.mutations).toEqual([]);
expect(mocks.transcribe).not.toHaveBeenCalled();
});
+
+ it.each([
+ ["a retryable error", "error", "Worker failed", "?retryable=true"],
+ [
+ "a media server shutdown",
+ "cancelled",
+ "Server shutting down",
+ "?retryable=true",
+ ],
+ ])(
+ "keeps a workflow upload processing for retry after %s",
+ async (_, phase, message, search) => {
+ const database = databaseFixture(null);
+ const response = await request(
+ { jobId: "web-job", videoId, phase, progress: 37, message },
+ "media-secret",
+ search,
+ );
+ expect(response.status).toBe(200);
+ expect(database.mutations).toEqual([
+ {
+ operation: "update",
+ table: mocks.tables.uploads,
+ values: expect.objectContaining({
+ phase: "processing",
+ processingProgress: 37,
+ processingError: message,
+ processingMessage: "Retrying video processing...",
+ }),
+ },
+ ]);
+ },
+ );
+
+ it.each([
+ ["a deliberate cancel", "Cancelled by user", "?retryable=true"],
+ ["a shutdown cancel without retry support", "Server shutting down", ""],
+ ])("marks a workflow upload failed after %s", async (_, message, search) => {
+ const database = databaseFixture(null);
+ const response = await request(
+ { jobId: "web-job", videoId, phase: "cancelled", progress: 37, message },
+ "media-secret",
+ search,
+ );
+ expect(response.status).toBe(200);
+ expect(database.mutations).toEqual([
+ {
+ operation: "update",
+ table: mocks.tables.uploads,
+ values: expect.objectContaining({
+ phase: "error",
+ processingError: message,
+ processingMessage: message,
+ }),
+ },
+ ]);
+ });
});
diff --git a/apps/web/__tests__/unit/mobile-cap-password-handler.test.ts b/apps/web/__tests__/unit/mobile-cap-password-handler.test.ts
new file mode 100644
index 000000000..e0b569dc8
--- /dev/null
+++ b/apps/web/__tests__/unit/mobile-cap-password-handler.test.ts
@@ -0,0 +1,190 @@
+import type { DbClient } from "@cap/web-backend/src/Database";
+import type { HttpApi } from "@effect/platform";
+import type { Context, Layer as EffectLayer } from "effect";
+import {
+ afterAll,
+ beforeAll,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ vi,
+} from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ ownerId: "owner",
+ revalidatePath: vi.fn(),
+ update: vi.fn(),
+ dispose: async () => {},
+}));
+
+vi.mock("server-only", () => ({}));
+vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath }));
+
+vi.mock("@/lib/server", async () => {
+ const { Database, Videos } = await import("@cap/web-backend");
+ const { HttpAuthMiddleware, Organisation, User } = await import(
+ "@cap/web-domain"
+ );
+ const { HttpApiBuilder, HttpServer } = await import("@effect/platform");
+ const { Effect, Layer, Option } = await import("effect");
+ const Db = await import("@cap/database/schema");
+ const { sql } = await import("drizzle-orm");
+ const summary = {
+ id: "video",
+ ownerId: "owner",
+ name: "Test video",
+ createdAt: new Date("2026-01-01"),
+ updatedAt: new Date("2026-01-01"),
+ ownerName: "Owner",
+ duration: 10,
+ folderId: null,
+ public: true,
+ hasPassword: false,
+ commentCount: 0,
+ reactionCount: 0,
+ uploadVideoId: null,
+ uploadUploaded: null,
+ uploadTotal: null,
+ uploadPhase: null,
+ processingProgress: null,
+ processingMessage: null,
+ processingError: null,
+ metadata: null,
+ transcriptionStatus: null,
+ };
+ const client = {
+ select: (fields: Record<string, unknown>) => {
+ let table: unknown;
+ const rows = () => {
+ if (table === Db.organizations) return [{ id: "org" }];
+ if (table === Db.users) return [{ preferences: null }];
+ if (table === Db.videos && "ownerPreferences" in fields) {
+ return [
+ {
+ ownerId: mocks.ownerId,
+ ownerPreferences: null,
+ hasPassword: false,
+ hasInheritedPassword: false,
+ },
+ ];
+ }
+ if (table === Db.videos)
+ return [{ ...summary, ownerId: mocks.ownerId }];
+ return [];
+ };
+ const query = {
+ from: (value: unknown) => {
+ table = value;
+ return query;
+ },
+ leftJoin: () => query,
+ innerJoin: () => query,
+ where: () => query,
+ limit: async () => rows(),
+ groupBy: async () => rows(),
+ getSQL: () => sql`SELECT 1`,
+ };
+ return query;
+ },
+ update: () => ({
+ set: (value: unknown) => ({
+ where: async () => {
+ mocks.update(value);
+ },
+ }),
+ }),
+ };
+ const database = Layer.succeed(
+ Database,
+ Database.make({
+ use: (callback) =>
+ Effect.promise(() => callback(client as unknown as DbClient)),
+ }),
+ );
+ const videos = Layer.succeed(Videos, {
+ getAnalytics: () => Effect.succeed({ count: 0 }),
+ } as unknown as Context.Tag.Service<typeof Videos>);
+ const auth = Layer.succeed(
+ HttpAuthMiddleware,
+ Effect.succeed({
+ id: User.UserId.make("owner"),
+ email: "[email protected]",
+ activeOrganizationId: Organisation.OrganisationId.make("org"),
+ iconUrlOrKey: Option.none(),
+ }),
+ );
+ return {
+ apiToHandler: (
+ api: EffectLayer.Layer<
+ HttpApi.Api,
+ never,
+ | Context.Tag.Identifier<typeof Database>
+ | Context.Tag.Identifier<typeof Videos>
+ | Context.Tag.Identifier<typeof HttpAuthMiddleware>
+ >,
+ ) => {
+ const handler = api.pipe(
+ Layer.provideMerge(auth),
+ Layer.provideMerge(database),
+ Layer.provideMerge(videos),
+ Layer.merge(HttpServer.layerContext),
+ HttpApiBuilder.toWebHandler,
+ );
+ mocks.dispose = handler.dispose;
+ return handler.handler;
+ },
+ };
+});
+
+describe("mobile Cap password update", () => {
+ let PATCH: typeof import("@/app/api/mobile/[...route]/route").PATCH;
+
+ beforeAll(async () => {
+ Object.assign(process.env, {
+ NEXT_PUBLIC_WEB_URL: "http://localhost",
+ WEB_URL: "http://localhost",
+ DATABASE_URL: "mysql://unused:[email protected]:3306/unused",
+ NEXTAUTH_SECRET: "synthetic-test-secret-that-is-long-enough",
+ NEXTAUTH_URL: "http://localhost",
+ CAP_AWS_BUCKET: "synthetic",
+ CAP_AWS_REGION: "us-east-1",
+ CAP_AWS_ACCESS_KEY: "synthetic",
+ CAP_AWS_SECRET_KEY: "synthetic",
+ NODE_ENV: "test",
+ });
+ ({ PATCH } = await import("@/app/api/mobile/[...route]/route"));
+ });
+
+ beforeEach(() => {
+ mocks.ownerId = "owner";
+ });
+
+ afterAll(() => mocks.dispose());
+
+ const request = () =>
+ PATCH(
+ new Request("http://localhost/api/mobile/caps/video/password", {
+ method: "PATCH",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ password: null }),
+ }),
+ );
+
+ it("revalidates the share page after removing its password", async () => {
+ expect((await request()).status).toBe(200);
+ expect(mocks.update).toHaveBeenCalledWith({ password: null });
+ expect(mocks.revalidatePath.mock.calls.map(([path]) => path)).toEqual([
+ "/dashboard/caps",
+ "/dashboard/shared-caps",
+ "/s/video",
+ ]);
+ });
+
+ it("does not revalidate when the caller does not own the Cap", async () => {
+ mocks.ownerId = "someone-else";
+ expect((await request()).status).toBe(404);
+ expect(mocks.update).not.toHaveBeenCalled();
+ expect(mocks.revalidatePath).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/mobile-checkout.test.ts b/apps/web/__tests__/unit/mobile-checkout.test.ts
index 5eecd2e22..ab52feaa5 100644
--- a/apps/web/__tests__/unit/mobile-checkout.test.ts
+++ b/apps/web/__tests__/unit/mobile-checkout.test.ts
@@ -22,6 +22,7 @@ vi.mock("@cap/utils", () => ({
sessions: { create: checkoutMocks.create },
},
}),
+ isValidStripePlanPriceId: (id: string) => id === "price_pro",
}));
vi.mock("@/lib/server-analytics", () => ({
@@ -59,6 +60,15 @@ describe("checkout redirects", () => {
});
});
+ it("rejects an invalid guest checkout quantity before creating a session", async () => {
+ const response = await startGuestCheckout(
+ makeGuestCheckoutRequest({ priceId: "price_pro", quantity: -5 }),
+ );
+
+ expect(response.status).toBe(400);
+ expect(checkoutMocks.create).not.toHaveBeenCalled();
+ });
+
it("keeps existing guest checkout requests on the web flow", async () => {
const response = await startGuestCheckout(
makeGuestCheckoutRequest({ priceId: "price_pro", quantity: 1 }),
@@ -79,6 +89,19 @@ describe("checkout redirects", () => {
});
});
+ it("rejects non-allowlisted arbitrary priceId with 400", async () => {
+ const response = await startGuestCheckout(
+ makeGuestCheckoutRequest({
+ priceId: "price_arbitrary_attacker_id",
+ quantity: 1,
+ }),
+ );
+
+ expect(response.status).toBe(400);
+ expect(await response.json()).toEqual({ error: "Invalid priceId" });
+ expect(checkoutMocks.create).not.toHaveBeenCalled();
+ });
+
it("sends mobile checkout results through the HTTPS completion route", () => {
expect(getCheckoutRedirectUrls("mobile", "https://cap.so/")).toEqual({
successUrl: "https://cap.so/mobile/checkout/complete?checkout=success",
diff --git a/apps/web/__tests__/unit/mobile-comment-parent.test.ts b/apps/web/__tests__/unit/mobile-comment-parent.test.ts
new file mode 100644
index 000000000..1e74518cb
--- /dev/null
+++ b/apps/web/__tests__/unit/mobile-comment-parent.test.ts
@@ -0,0 +1,222 @@
+import * as Db from "@cap/database/schema";
+import {
+ CurrentUser,
+ DatabaseError,
+ HttpAuthMiddleware,
+ Organisation,
+ User,
+ Video,
+} from "@cap/web-domain";
+import { type HttpApi, HttpApiBuilder, HttpServer } from "@effect/platform";
+import type { SQL } from "drizzle-orm";
+import { MySqlDialect, type MySqlTable } from "drizzle-orm/mysql-core";
+import { Effect, Layer, Option } from "effect";
+import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
+import {
+ Database,
+ type DbClient,
+} from "../../../../packages/web-backend/src/Database";
+
+const mocks = vi.hoisted(() => ({ notify: vi.fn(async () => undefined) }));
+vi.mock("server-only", () => ({}));
+vi.mock("@cap/database/auth/auth-options", () => ({ authOptions: () => ({}) }));
+vi.mock("@cap/database/emails/config", () => ({ sendEmail: vi.fn() }));
+vi.mock("@/actions/loom", () => ({ downloadLoomVideo: vi.fn() }));
+vi.mock("@/app/(org)/dashboard/analytics/data", () => ({
+ getOrgAnalyticsData: vi.fn(),
+}));
+vi.mock("@/lib/account-deletion-request", () => ({}));
+vi.mock("@/lib/desktop-segments-finalization", () => ({}));
+vi.mock("@/lib/Notification", () => ({ createNotification: mocks.notify }));
+vi.mock("@/lib/sync-video-storage-names", () => ({}));
+vi.mock("@/lib/video-processing", () => ({}));
+vi.mock("@/workflows/import-loom-video", () => ({}));
+vi.mock("workflow/api", () => ({ start: vi.fn() }));
+vi.mock("@cap/web-backend", async () => {
+ const { Context } = await import("effect");
+ const { Database } = await import(
+ "../../../../packages/web-backend/src/Database"
+ );
+ return {
+ Database,
+ Videos: Context.GenericTag("mobile-comment-test/Videos"),
+ ImageUploads: Context.GenericTag("mobile-comment-test/ImageUploads"),
+ };
+});
+
+const user = {
+ id: User.UserId.make("viewer"),
+ email: "[email protected]",
+ activeOrganizationId: Organisation.OrganisationId.make("organization"),
+ iconUrlOrKey: Option.none(),
+};
+const videoId = Video.VideoId.make("accessible-video");
+type Row = Record<string, unknown>;
+let parents: Row[];
+let inserted: Row[];
+const dialect = new MySqlDialect();
+
+function matchesParent(condition: SQL, row: Row) {
+ const { sql, params } = dialect.sqlToQuery(condition);
+ let index = 0;
+ return sql
+ .replaceAll(/[()]/g, "")
+ .split(" and ")
+ .every((clause) => {
+ const match = /^`comments`\.`([^`]+)` = \?$/.exec(clause);
+ if (!match?.[1]) throw new Error(`Unsupported predicate: ${clause}`);
+ return row[match[1]] === params[index++];
+ });
+}
+
+const database = {
+ select: (columns?: Record<string, unknown>) => ({
+ from: (table: MySqlTable) => {
+ let condition: SQL;
+ const result = () => {
+ if (table === Db.users) return [{ preferences: {} }];
+ if (table === Db.organizations)
+ return [{ id: user.activeOrganizationId }];
+ if (table === Db.videos) return [{ ownerId: user.id }];
+ if (table !== Db.comments) throw new Error("Unexpected table");
+ if (columns && Object.keys(columns).length === 1) {
+ return parents.filter((row) => matchesParent(condition, row));
+ }
+ return inserted.map((row) => ({
+ ...row,
+ authorName: "Viewer",
+ authorImage: null,
+ authorPreferences: {},
+ }));
+ };
+ const query = {
+ leftJoin: () => query,
+ innerJoin: () => query,
+ where: (value: SQL) => {
+ condition = value;
+ return query;
+ },
+ limit: async () => result(),
+ orderBy: async () => result(),
+ };
+ return query;
+ },
+ }),
+ insert: (table: MySqlTable) => ({
+ values: async (row: Row) => {
+ if (table !== Db.comments) throw new Error("Unexpected insert");
+ inserted.push(row);
+ },
+ }),
+};
+
+let dispose: () => Promise<void>;
+vi.mock("@/lib/server", async () => {
+ const { Videos, ImageUploads } = await import("@cap/web-backend");
+ return {
+ apiToHandler: (
+ api: Layer.Layer<HttpApi.Api, never, Database | HttpAuthMiddleware>,
+ ) => {
+ const web = HttpApiBuilder.toWebHandler(
+ api.pipe(
+ Layer.provide(
+ Layer.succeed(
+ Database,
+ Database.make({
+ use: (callback) =>
+ Effect.tryPromise({
+ try: () => callback(database as unknown as DbClient),
+ catch: (cause) => new DatabaseError({ cause }),
+ }),
+ }),
+ ),
+ ),
+ Layer.provide(Layer.mock(Videos, { _tag: "Videos" })),
+ Layer.provide(Layer.mock(ImageUploads, { _tag: "ImageUploads" })),
+ Layer.provide(
+ Layer.succeed(
+ HttpAuthMiddleware,
+ Effect.succeed(CurrentUser.of(user)),
+ ),
+ ),
+ Layer.merge(HttpServer.layerContext),
+ ),
+ );
+ dispose = web.dispose;
+ return web.handler;
+ },
+ };
+});
+
+const { POST } = await import("@/app/api/mobile/[...route]/route");
+
+function postComment(parentCommentId: string | null = "parent") {
+ return POST(
+ new Request(`http://localhost/api/mobile/caps/${videoId}/comments`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ content: " A reply ",
+ timestamp: null,
+ parentCommentId,
+ }),
+ }),
+ );
+}
+
+beforeEach(() => {
+ parents = [{ id: "parent", videoId, type: "text" }];
+ inserted = [];
+ mocks.notify.mockClear();
+});
+
+afterAll(async () => {
+ await dispose();
+});
+
+describe("mobile reply parent authorization", () => {
+ it.each([
+ {
+ name: "belongs to another video",
+ candidates: [{ id: "parent", videoId: "private-video", type: "text" }],
+ },
+ {
+ name: "is not a text comment",
+ candidates: [{ id: "parent", videoId, type: "emoji" }],
+ },
+ { name: "does not exist", candidates: [] },
+ ])("rejects a parent that $name", async ({ candidates }) => {
+ parents = candidates;
+ const response = await postComment();
+ expect(response.status).toBe(404);
+ expect(inserted).toEqual([]);
+ expect(mocks.notify).not.toHaveBeenCalled();
+ });
+
+ it("preserves replies to text comments on the accessible video", async () => {
+ const response = await postComment();
+ expect(response.status).toBe(200);
+ expect(await response.json()).toMatchObject({
+ content: "A reply",
+ parentCommentId: "parent",
+ videoId,
+ });
+ expect(inserted).toHaveLength(1);
+ expect(mocks.notify).toHaveBeenCalledWith(
+ expect.objectContaining({
+ type: "reply",
+ parentCommentId: "parent",
+ videoId,
+ }),
+ );
+ });
+
+ it("preserves top-level comments", async () => {
+ const response = await postComment(null);
+ expect(response.status).toBe(200);
+ expect(inserted).toHaveLength(1);
+ expect(mocks.notify).toHaveBeenCalledWith(
+ expect.objectContaining({ type: "comment" }),
+ );
+ });
+});
diff --git a/apps/web/__tests__/unit/mobile-email-session.test.ts b/apps/web/__tests__/unit/mobile-email-session.test.ts
new file mode 100644
index 000000000..8d103a4fb
--- /dev/null
+++ b/apps/web/__tests__/unit/mobile-email-session.test.ts
@@ -0,0 +1,335 @@
+import type { DbClient } from "@cap/web-backend/src/Database";
+import type { HttpApi } from "@effect/platform";
+import type { SQL } from "drizzle-orm";
+import { MySqlDialect } from "drizzle-orm/mysql-core";
+import type { Context, Layer as EffectLayer } from "effect";
+import {
+ afterAll,
+ beforeAll,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ vi,
+} from "vitest";
+
+type TokenRow = { identifier: string; token: string; expires: Date };
+
+const mocks = vi.hoisted(() => ({
+ isRateLimited: vi.fn<(ruleId: string, opts?: unknown) => Promise<boolean>>(),
+ sendEmail: vi.fn<(input: { react: { code: string } }) => Promise<void>>(),
+ tokenRow: null as TokenRow | null,
+ selectBarrier: null as null | { size: number; waiting: (() => void)[] },
+ apiKeys: [] as string[],
+ dispose: async () => {},
+}));
+
+vi.mock("server-only", () => ({}));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+vi.mock("@/lib/rate-limit", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@/lib/rate-limit")>()),
+ isRateLimited: mocks.isRateLimited,
+}));
+vi.mock("@cap/database/emails/config", () => ({ sendEmail: mocks.sendEmail }));
+vi.mock("@cap/database/emails/otp-email", () => ({
+ OTPEmail: (props: { code: string; email: string }) => props,
+}));
+vi.mock("@/lib/account-deletion-request", () => ({
+ createAccountDeletionRequest: vi.fn(),
+ createMobileContentReport: vi.fn(),
+ hasPendingAccountDeletion: vi.fn(async () => false),
+}));
+vi.mock("@cap/database/auth/auth-options", () => ({
+ authOptions: () => ({
+ adapter: {
+ createUser: vi.fn(async ({ email }: { email: string }) => ({
+ id: "user-1",
+ email,
+ })),
+ getUserByEmail: vi.fn(async (email: string) => ({
+ id: "user-1",
+ email,
+ })),
+ updateUser: vi.fn(async ({ id }: { id: string }) => ({ id })),
+ },
+ }),
+}));
+
+vi.mock("@/lib/server", async () => {
+ const { Database, Videos } = await import("@cap/web-backend");
+ const { HttpAuthMiddleware } = await import("@cap/web-domain");
+ const { HttpApiBuilder, HttpServer } = await import("@effect/platform");
+ const { Effect, Layer } = await import("effect");
+ const Db = await import("@cap/database/schema");
+
+ const matches = (condition: SQL, row: TokenRow) => {
+ const query = new MySqlDialect().sqlToQuery(condition);
+ const columns = [
+ ...query.sql.matchAll(
+ /`verification_tokens`\.`(identifier|token)`\s*=\s*\?/g,
+ ),
+ ].map((match) => match[1] as "identifier" | "token");
+ if (columns.length === 0) {
+ throw new Error(`Unexpected predicate: ${query.sql}`);
+ }
+ return columns.every((column, i) => row[column] === query.params[i]);
+ };
+
+ const waitAtBarrier = async () => {
+ const barrier = mocks.selectBarrier;
+ if (!barrier) return;
+ await new Promise<void>((resolve) => {
+ barrier.waiting.push(resolve);
+ if (barrier.waiting.length >= barrier.size) {
+ for (const release of barrier.waiting) release();
+ }
+ });
+ };
+
+ const client = {
+ select: () => ({
+ from: (table: unknown) => ({
+ where: (condition: SQL) => ({
+ limit: async () => {
+ if (table !== Db.verificationTokens) return [];
+ const row = mocks.tokenRow;
+ const result = row && matches(condition, row) ? [{ ...row }] : [];
+ await waitAtBarrier();
+ return result;
+ },
+ }),
+ }),
+ }),
+ insert: (table: unknown) => ({
+ values: async (value: TokenRow & { id?: string }) => {
+ if (table === Db.verificationTokens) {
+ mocks.tokenRow = {
+ identifier: value.identifier,
+ token: value.token,
+ expires: value.expires,
+ };
+ }
+ if (table === Db.authApiKeys && value.id) mocks.apiKeys.push(value.id);
+ },
+ }),
+ update: (table: unknown) => ({
+ set: (value: Partial<TokenRow>) => ({
+ where: async (condition: SQL) => {
+ const row = mocks.tokenRow;
+ if (
+ table === Db.verificationTokens &&
+ row &&
+ matches(condition, row)
+ ) {
+ mocks.tokenRow = { ...row, ...value };
+ }
+ },
+ }),
+ }),
+ delete: (table: unknown) => ({
+ where: async (condition: SQL) => {
+ await Promise.resolve();
+ if (table !== Db.verificationTokens) return [{ affectedRows: 0 }];
+ const row = mocks.tokenRow;
+ if (row && matches(condition, row)) {
+ mocks.tokenRow = null;
+ return [{ affectedRows: 1 }];
+ }
+ return [{ affectedRows: 0 }];
+ },
+ }),
+ };
+ const clientWithTransaction = {
+ ...client,
+ transaction: async (callback: (tx: typeof client) => Promise<unknown>) =>
+ callback(client),
+ };
+
+ const database = Layer.succeed(
+ Database,
+ Database.make({
+ use: (callback) =>
+ Effect.promise(() =>
+ callback(clientWithTransaction as unknown as DbClient),
+ ),
+ }),
+ );
+ const videos = Layer.succeed(
+ Videos,
+ {} as unknown as Context.Tag.Service<typeof Videos>,
+ );
+ const auth = Layer.succeed(HttpAuthMiddleware, Effect.die("unused"));
+
+ return {
+ apiToHandler: (
+ api: EffectLayer.Layer<
+ HttpApi.Api,
+ never,
+ | Context.Tag.Identifier<typeof Database>
+ | Context.Tag.Identifier<typeof Videos>
+ | Context.Tag.Identifier<typeof HttpAuthMiddleware>
+ >,
+ ) => {
+ const handler = api.pipe(
+ Layer.provideMerge(auth),
+ Layer.provideMerge(database),
+ Layer.provideMerge(videos),
+ Layer.merge(HttpServer.layerContext),
+ HttpApiBuilder.toWebHandler,
+ );
+ mocks.dispose = handler.dispose;
+ return handler.handler;
+ },
+ };
+});
+
+describe("mobile email sessions", () => {
+ let POST: typeof import("@/app/api/mobile/[...route]/route").POST;
+
+ beforeAll(async () => {
+ Object.assign(process.env, {
+ NEXT_PUBLIC_WEB_URL: "http://localhost",
+ WEB_URL: "http://localhost",
+ DATABASE_URL: "mysql://unused:[email protected]:3306/unused",
+ NEXTAUTH_SECRET: "synthetic-test-secret-that-is-long-enough",
+ NEXTAUTH_URL: "http://localhost",
+ CAP_AWS_BUCKET: "synthetic",
+ CAP_AWS_REGION: "us-east-1",
+ CAP_AWS_ACCESS_KEY: "synthetic",
+ CAP_AWS_SECRET_KEY: "synthetic",
+ RESEND_API_KEY: "synthetic",
+ NODE_ENV: "test",
+ });
+ ({ POST } = await import("@/app/api/mobile/[...route]/route"));
+ });
+
+ beforeEach(() => {
+ mocks.isRateLimited.mockReset().mockResolvedValue(false);
+ mocks.sendEmail.mockReset().mockResolvedValue(undefined);
+ mocks.tokenRow = null;
+ mocks.selectBarrier = null;
+ mocks.apiKeys = [];
+ });
+
+ afterAll(() => mocks.dispose());
+
+ const post = (path: string, body: unknown) =>
+ POST(
+ new Request(`http://localhost/api/mobile/session/email/${path}`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify(body),
+ }),
+ );
+
+ const requestCode = async (email = "[email protected]") => {
+ const response = await post("request", { email });
+ expect(response.status).toBe(200);
+ const code = mocks.sendEmail.mock.calls.at(-1)?.[0].react.code;
+ if (!code) throw new Error("No code was sent");
+ return code;
+ };
+
+ const wrongCode = (code: string) => (code === "123456" ? "654321" : "123456");
+
+ it("sends a code and signs in with it", async () => {
+ const code = await requestCode(" [email protected] ");
+
+ const response = await post("verify", {
+ email: "[email protected]",
+ code,
+ });
+
+ expect(response.status).toBe(200);
+ expect(await response.json()).toMatchObject({
+ type: "api_key",
+ userId: "user-1",
+ });
+ expect(mocks.tokenRow).toBeNull();
+ expect(mocks.isRateLimited.mock.calls.map(([ruleId]) => ruleId)).toEqual([
+ "rl_auth_otp_send",
+ "rl_auth_otp_verify",
+ ]);
+ });
+
+ it("uses one bucket per normalized email", async () => {
+ await requestCode(" [email protected] ");
+ await requestCode("[email protected]");
+
+ const [first, second] = mocks.isRateLimited.mock.calls.map(
+ ([, opts]) => (opts as { key: string }).key,
+ );
+ expect(first).toMatch(/^email-code-send:[0-9a-f]{64}$/);
+ expect(second).toBe(first);
+ });
+
+ it("returns 429 without sending when code requests are limited", async () => {
+ mocks.isRateLimited.mockResolvedValue(true);
+
+ const response = await post("request", { email: "[email protected]" });
+
+ expect(response.status).toBe(429);
+ expect(mocks.sendEmail).not.toHaveBeenCalled();
+ expect(mocks.tokenRow).toBeNull();
+ });
+
+ it("returns 429 and keeps the code when verification is limited", async () => {
+ const code = await requestCode();
+ mocks.isRateLimited.mockResolvedValue(true);
+
+ const response = await post("verify", {
+ email: "[email protected]",
+ code,
+ });
+
+ expect(response.status).toBe(429);
+ expect(mocks.tokenRow).not.toBeNull();
+ expect(mocks.apiKeys).toEqual([]);
+ });
+
+ it("invalidates the code after a wrong attempt", async () => {
+ const code = await requestCode();
+
+ const wrong = await post("verify", {
+ email: "[email protected]",
+ code: wrongCode(code),
+ });
+ const retry = await post("verify", {
+ email: "[email protected]",
+ code,
+ });
+
+ expect(wrong.status).toBe(403);
+ expect(retry.status).toBe(403);
+ expect(mocks.apiKeys).toEqual([]);
+ });
+
+ it("rejects an expired code", async () => {
+ const code = await requestCode();
+ if (!mocks.tokenRow) throw new Error("No stored code");
+ mocks.tokenRow.expires = new Date(Date.now() - 1000);
+
+ const response = await post("verify", {
+ email: "[email protected]",
+ code,
+ });
+
+ expect(response.status).toBe(403);
+ expect(mocks.tokenRow).toBeNull();
+ });
+
+ it("lets only one of two concurrent verifications use a code", async () => {
+ const code = await requestCode();
+ mocks.selectBarrier = { size: 2, waiting: [] };
+
+ const responses = await Promise.all([
+ post("verify", { email: "[email protected]", code }),
+ post("verify", { email: "[email protected]", code }),
+ ]);
+
+ expect(responses.map((response) => response.status).sort()).toEqual([
+ 200, 403,
+ ]);
+ expect(mocks.apiKeys).toHaveLength(1);
+ });
+});
diff --git a/apps/web/__tests__/unit/new-media-comment-thumbnail.test.ts b/apps/web/__tests__/unit/new-media-comment-thumbnail.test.ts
new file mode 100644
index 000000000..2f44c42ce
--- /dev/null
+++ b/apps/web/__tests__/unit/new-media-comment-thumbnail.test.ts
@@ -0,0 +1,152 @@
+import { Effect, Option } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ head: vi.fn(),
+ insert: vi.fn(),
+}));
+
+const video = { id: "video-1", ownerId: "owner-1" };
+const mediaKey = "owner-1/video-1/comments/comment-1/media.webm";
+const thumbnailKey = "owner-1/video-1/comments/comment-1/thumbnail.jpg";
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ insert: () => ({ values: mocks.insert }),
+ }),
+}));
+vi.mock("@cap/database/schema", () => ({ comments: {} }));
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: async () => ({ id: "user-1", name: "User" }),
+}));
+vi.mock("@cap/web-domain", () => ({
+ Policy: { withPublicPolicy: () => (effect: unknown) => effect },
+}));
+vi.mock("@cap/web-backend", () => ({
+ provideOptionalAuth: (effect: unknown) => effect,
+ verifyCommentMediaUploadToken: () => ({
+ userId: "user-1",
+ videoId: "video-1",
+ commentId: "comment-1",
+ kind: "video",
+ key: mediaKey,
+ thumbnailKey,
+ contentType: "video/webm",
+ }),
+ VideosRepo: Effect.succeed({
+ getById: () => Effect.succeed(Option.some([video])),
+ }),
+ VideosPolicy: Effect.succeed({ canView: () => Effect.void }),
+ Storage: {
+ getAccessForVideo: () =>
+ Effect.succeed([
+ {
+ headObject: (key: string) =>
+ key === thumbnailKey
+ ? mocks.head()
+ : Effect.succeed({ ContentLength: 4096 }),
+ },
+ ]),
+ },
+}));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+vi.mock("@/lib/comment-media", () => ({ sanitizeWaveform: () => undefined }));
+vi.mock("@/lib/media-comments", () => ({
+ canUseMediaComments: async () => true,
+}));
+vi.mock("@/lib/Notification", () => ({ createNotification: vi.fn() }));
+vi.mock("@/lib/server", () => ({ runPromiseExit: Effect.runPromiseExit }));
+
+import { newMediaComment } from "@/actions/videos/new-media-comment";
+
+function storageError(cause: unknown) {
+ return { _tag: "StorageError", cause: { _tag: "S3Error", cause } };
+}
+
+function s3Error(name: string, httpStatusCode: number) {
+ return Object.assign(new Error(name), {
+ name,
+ $metadata: { httpStatusCode },
+ });
+}
+
+async function postComment() {
+ return newMediaComment({
+ videoId: "video-1" as never,
+ commentId: "comment-1" as never,
+ uploadToken: "token",
+ kind: "video",
+ durationSeconds: 5,
+ parentCommentId: "" as never,
+ timestamp: null,
+ hasThumbnail: true,
+ authorImage: null,
+ });
+}
+
+function savedThumbnailKey() {
+ return mocks.insert.mock.calls[0]?.[0]?.mediaMeta?.thumbnailKey;
+}
+
+describe("newMediaComment thumbnail check", () => {
+ beforeEach(() => {
+ mocks.head.mockReset();
+ mocks.insert.mockReset().mockResolvedValue(undefined);
+ });
+
+ it("keeps the thumbnail when its HEAD request fails transiently", async () => {
+ mocks.head.mockReturnValue(
+ Effect.fail(storageError(s3Error("ServiceUnavailable", 503))),
+ );
+
+ await postComment();
+
+ expect(savedThumbnailKey()).toBe(thumbnailKey);
+ });
+
+ it("keeps the thumbnail when its HEAD request hits a network error", async () => {
+ mocks.head.mockReturnValue(
+ Effect.fail(storageError(new Error("socket hang up"))),
+ );
+
+ await postComment();
+
+ expect(savedThumbnailKey()).toBe(thumbnailKey);
+ });
+
+ it("drops a thumbnail that storage reports missing", async () => {
+ mocks.head.mockReturnValue(
+ Effect.fail(storageError(s3Error("NotFound", 404))),
+ );
+
+ await postComment();
+
+ expect(mocks.insert).toHaveBeenCalledOnce();
+ expect(savedThumbnailKey()).toBeUndefined();
+ });
+
+ it("drops a thumbnail that Google Drive reports missing", async () => {
+ mocks.head.mockReturnValue(
+ Effect.fail({
+ _tag: "StorageError",
+ cause: Object.assign(new Error("Storage object not found"), {
+ name: "GoogleDriveRequestError",
+ status: 404,
+ }),
+ }),
+ );
+
+ await postComment();
+
+ expect(mocks.insert).toHaveBeenCalledOnce();
+ expect(savedThumbnailKey()).toBeUndefined();
+ });
+
+ it("keeps a thumbnail that exists", async () => {
+ mocks.head.mockReturnValue(Effect.succeed({ ContentLength: 128 }));
+
+ await postComment();
+
+ expect(savedThumbnailKey()).toBe(thumbnailKey);
+ });
+});
diff --git a/apps/web/__tests__/unit/nextauth-email-route.test.ts b/apps/web/__tests__/unit/nextauth-email-route.test.ts
new file mode 100644
index 000000000..5e747a1a2
--- /dev/null
+++ b/apps/web/__tests__/unit/nextauth-email-route.test.ts
@@ -0,0 +1,180 @@
+import { NextRequest } from "next/server";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { GET, POST } from "@/app/api/auth/[...nextauth]/route";
+
+type RouteContext = { params: Promise<{ nextauth: string[] }> };
+
+const mocks = vi.hoisted(() => ({
+ isRateLimited: vi.fn<(ruleId: string, opts?: unknown) => Promise<boolean>>(),
+ nextAuth: vi.fn<(request: NextRequest) => Promise<Response>>(),
+}));
+
+const env = vi.hoisted(() => ({
+ WEB_URL: "https://cap.example",
+ NEXTAUTH_SECRET: "test-email-route-secret-with-sufficient-entropy",
+}));
+
+vi.mock("@cap/env", () => ({ serverEnv: () => env }));
+vi.mock("@cap/database/auth/auth-options", () => ({
+ authOptions: () => ({ providers: [] }),
+ decodeSessionToken: vi.fn(),
+}));
+vi.mock("@cap/database/auth/sso", () => ({ getWorkOS: vi.fn() }));
+vi.mock("next-auth", () => ({ default: mocks.nextAuth }));
+vi.mock("next-auth/jwt", () => ({ getToken: vi.fn(async () => null) }));
+vi.mock("@/lib/rate-limit", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@/lib/rate-limit")>()),
+ isRateLimited: mocks.isRateLimited,
+}));
+
+function context(...nextauth: string[]): RouteContext {
+ return { params: Promise.resolve({ nextauth }) };
+}
+
+function emailSignIn(email: string, extra: Record<string, string> = {}) {
+ return new NextRequest(`${env.WEB_URL}/api/auth/signin/email`, {
+ method: "POST",
+ headers: { "content-type": "application/x-www-form-urlencoded" },
+ body: new URLSearchParams({
+ email,
+ csrfToken: "fixture-csrf-token",
+ callbackUrl: "/dashboard",
+ json: "true",
+ ...extra,
+ }),
+ });
+}
+
+function emailCallback(query: Record<string, string>) {
+ const url = new URL("/api/auth/callback/email", env.WEB_URL);
+ url.search = new URLSearchParams(query).toString();
+ return new NextRequest(url);
+}
+
+beforeEach(() => {
+ mocks.isRateLimited.mockReset().mockResolvedValue(false);
+ mocks.nextAuth.mockReset().mockImplementation(async (request) => {
+ const body = request.method === "POST" ? await request.text() : "";
+ return Response.json({ handledBy: "next-auth", body });
+ });
+});
+
+describe("email sign-in requests", () => {
+ it("passes an allowed request to NextAuth with its body intact", async () => {
+ const response = await POST(
+ emailSignIn("[email protected]"),
+ context("signin", "email"),
+ );
+
+ expect(mocks.nextAuth).toHaveBeenCalledTimes(1);
+ const body = (await response.json()) as { body: string };
+ expect(new URLSearchParams(body.body).get("email")).toBe(
+ );
+ expect(mocks.isRateLimited).toHaveBeenCalledTimes(1);
+ const [ruleId, opts] = mocks.isRateLimited.mock.calls[0] ?? [];
+ expect(ruleId).toBe("rl_auth_otp_send");
+ expect((opts as { key: string }).key).toMatch(
+ /^email-code-send:[0-9a-f]{64}$/,
+ );
+ });
+
+ it("uses one bucket per normalized email", async () => {
+ await POST(emailSignIn(" [email protected] "), context("signin", "email"));
+ await POST(emailSignIn("[email protected]"), context("signin", "email"));
+
+ const [first, second] = mocks.isRateLimited.mock.calls.map(
+ ([, opts]) => (opts as { key: string }).key,
+ );
+ expect(second).toBe(first);
+ });
+
+ it("returns the NextAuth email error without sending when limited", async () => {
+ mocks.isRateLimited.mockResolvedValue(true);
+
+ const response = await POST(
+ emailSignIn("[email protected]"),
+ context("signin", "email"),
+ );
+
+ expect(response.status).toBe(429);
+ expect(mocks.nextAuth).not.toHaveBeenCalled();
+ const { url } = (await response.json()) as { url: string };
+ expect(new URL(url).searchParams.get("error")).toBe("EmailSignin");
+ });
+
+ it("redirects non-JSON requests to the error page when limited", async () => {
+ mocks.isRateLimited.mockResolvedValue(true);
+
+ const response = await POST(
+ emailSignIn("[email protected]", { json: "false" }),
+ context("signin", "email"),
+ );
+
+ expect(response.status).toBe(302);
+ expect(mocks.nextAuth).not.toHaveBeenCalled();
+ const location = new URL(response.headers.get("location") ?? "");
+ expect(location.pathname).toBe("/api/auth/error");
+ expect(location.searchParams.get("error")).toBe("EmailSignin");
+ });
+});
+
+describe("email code verification", () => {
+ it("passes an allowed verification to NextAuth", async () => {
+ const response = await GET(
+ emailCallback({ email: "[email protected]", token: "123456" }),
+ context("callback", "email"),
+ );
+
+ expect(mocks.nextAuth).toHaveBeenCalledTimes(1);
+ expect(await response.json()).toMatchObject({ handledBy: "next-auth" });
+ expect(mocks.isRateLimited.mock.calls[0]?.[0]).toBe("rl_auth_otp_verify");
+ });
+
+ it("redirects to the verification error without checking the code when limited", async () => {
+ mocks.isRateLimited.mockResolvedValue(true);
+
+ const response = await GET(
+ emailCallback({ email: "[email protected]", token: "123456" }),
+ context("callback", "email"),
+ );
+
+ expect(response.status).toBe(302);
+ expect(mocks.nextAuth).not.toHaveBeenCalled();
+ const location = new URL(response.headers.get("location") ?? "");
+ expect(location.pathname).toBe("/api/auth/error");
+ expect(location.searchParams.get("error")).toBe("Verification");
+ });
+});
+
+describe("other auth routes", () => {
+ it.each([
+ ["GET", ["session"]],
+ ["GET", ["csrf"]],
+ ["GET", ["providers"]],
+ ["POST", ["signin", "google"]],
+ ["GET", ["callback", "google"]],
+ ["GET", ["signin", "email"]],
+ ["POST", ["signout"]],
+ ] as const)("leaves %s %j untouched", async (method, segments) => {
+ mocks.isRateLimited.mockResolvedValue(true);
+ const request = new NextRequest(
+ `${env.WEB_URL}/api/auth/${segments.join("/")}?email=person%40example.com&token=123456`,
+ {
+ method,
+ ...(method === "POST"
+ ? {
+ headers: { "content-type": "application/x-www-form-urlencoded" },
+ body: new URLSearchParams({ email: "[email protected]" }),
+ }
+ : {}),
+ },
+ );
+
+ const handler = method === "POST" ? POST : GET;
+ await handler(request, context(...segments));
+
+ expect(mocks.isRateLimited).not.toHaveBeenCalled();
+ expect(mocks.nextAuth).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/apps/web/__tests__/unit/notification-read-state.test.ts b/apps/web/__tests__/unit/notification-read-state.test.ts
new file mode 100644
index 000000000..3edbf65f1
--- /dev/null
+++ b/apps/web/__tests__/unit/notification-read-state.test.ts
@@ -0,0 +1,85 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ query: vi.fn(),
+ getCurrentUser: vi.fn(),
+ revalidatePath: vi.fn(),
+}));
+
+vi.mock("@cap/database", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ const client = drizzle(mocks.query);
+ return { db: () => client };
+});
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: mocks.getCurrentUser,
+}));
+vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath }));
+
+import { markAsRead } from "@/actions/notifications/mark-as-read";
+
+beforeEach(() => {
+ mocks.getCurrentUser.mockResolvedValue({ id: "user-1" });
+ mocks.query.mockResolvedValue({ rows: [{ affectedRows: 1, insertId: 0 }] });
+});
+
+describe("notification read state", () => {
+ it.each(["own-notification", "another-users-notification"])(
+ "scopes an individual update to the current recipient for %s",
+ async (notificationId) => {
+ await markAsRead(notificationId);
+
+ expect(mocks.query).toHaveBeenCalledExactlyOnceWith(
+ expect.stringContaining(
+ "where (`notifications`.`recipientId` = ? and `notifications`.`id` = ?)",
+ ),
+ [expect.any(String), "user-1", notificationId],
+ "execute",
+ );
+ expect(mocks.revalidatePath).toHaveBeenCalledWith("/dashboard");
+ },
+ );
+
+ it("marks all notifications for the current recipient when no ID is supplied", async () => {
+ await markAsRead();
+
+ expect(mocks.query).toHaveBeenCalledExactlyOnceWith(
+ "update `notifications` set `readAt` = ? where `notifications`.`recipientId` = ?",
+ [expect.any(String), "user-1"],
+ "execute",
+ );
+ expect(mocks.revalidatePath).toHaveBeenCalledWith("/dashboard");
+ });
+
+ it("succeeds without exposing whether a notification matched", async () => {
+ mocks.query.mockResolvedValueOnce({
+ rows: [{ affectedRows: 0, insertId: 0 }],
+ });
+
+ await expect(markAsRead("missing-notification")).resolves.toBeUndefined();
+
+ expect(mocks.revalidatePath).toHaveBeenCalledWith("/dashboard");
+ });
+
+ it("does not write when the requester is not authenticated", async () => {
+ mocks.getCurrentUser.mockResolvedValueOnce(null);
+
+ await expect(markAsRead("notification-1")).rejects.toThrow(
+ "User not found",
+ );
+
+ expect(mocks.query).not.toHaveBeenCalled();
+ expect(mocks.revalidatePath).not.toHaveBeenCalled();
+ });
+
+ it("reports a failed write without revalidating the dashboard", async () => {
+ mocks.query.mockRejectedValueOnce(new Error("Database unavailable"));
+ vi.spyOn(console, "log").mockImplementation(() => {});
+
+ await expect(markAsRead("notification-1")).rejects.toThrow(
+ "Error marking notification(s) as read",
+ );
+
+ expect(mocks.revalidatePath).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/oauth-sign-in-errors.test.ts b/apps/web/__tests__/unit/oauth-sign-in-errors.test.ts
new file mode 100644
index 000000000..67f92ade1
--- /dev/null
+++ b/apps/web/__tests__/unit/oauth-sign-in-errors.test.ts
@@ -0,0 +1,161 @@
+// @vitest-environment jsdom
+
+import {
+ act,
+ type ButtonHTMLAttributes,
+ createElement,
+ type ElementType,
+ type ReactNode,
+} from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ signIn: vi.fn(),
+ error: vi.fn(),
+ captureException: vi.fn(),
+ searchParams: new URLSearchParams(),
+}));
+vi.mock("next-auth/react", () => ({ signIn: mocks.signIn }));
+vi.mock("@sentry/nextjs", () => ({ captureException: mocks.captureException }));
+vi.mock("sonner", () => ({ toast: { error: mocks.error } }));
+vi.mock("next/navigation", () => ({
+ useSearchParams: () => mocks.searchParams,
+ useRouter: () => ({ push: vi.fn() }),
+}));
+vi.mock("@cap/ui", () => ({
+ Button: ({
+ spinner: _spinner,
+ variant: _variant,
+ ...props
+ }: ButtonHTMLAttributes<HTMLButtonElement> & {
+ spinner?: boolean;
+ variant?: string;
+ }) => createElement("button", { type: "button", ...props }),
+ Input: "input",
+ LogoBadge: "div",
+}));
+vi.mock("framer-motion", () => ({
+ AnimatePresence: ({ children }: { children: ReactNode }) => children,
+ motion: Object.assign((component: ElementType) => component, {
+ button: "button",
+ div: "div",
+ form: "form",
+ h1: "h1",
+ p: "p",
+ span: "span",
+ }),
+}));
+vi.mock("next/image", () => ({ default: "img" }));
+vi.mock("next/link", () => ({ default: "a" }));
+vi.mock("@fortawesome/react-fontawesome", () => ({
+ FontAwesomeIcon: () => null,
+}));
+vi.mock("@/actions/organization/get-organization-sso-data", () => ({
+ getOrganizationSSOData: vi.fn(),
+}));
+vi.mock("@/app/utils/analytics", () => ({ trackEvent: vi.fn() }));
+vi.mock("@/utils/public-env", () => ({
+ usePublicEnv: () => ({
+ googleAuthAvailable: true,
+ workosAuthAvailable: false,
+ }),
+}));
+vi.mock("@/app/(org)/auth-email", () => ({
+ getEmailCodeCooldownSeconds: () => 0,
+ requestEmailCode: vi.fn(),
+}));
+
+const { LoginForm } = await import("@/app/(org)/login/form");
+const { SignupForm } = await import("@/app/(org)/signup/form");
+let container: HTMLDivElement;
+let root: Root;
+const actEnvironment = globalThis as typeof globalThis & {
+ IS_REACT_ACT_ENVIRONMENT?: boolean;
+};
+
+beforeEach(() => {
+ actEnvironment.IS_REACT_ACT_ENVIRONMENT = true;
+ vi.stubGlobal("localStorage", {
+ getItem: () => null,
+ setItem: vi.fn(),
+ removeItem: vi.fn(),
+ });
+ mocks.searchParams = new URLSearchParams();
+ mocks.signIn.mockReset().mockResolvedValue(undefined);
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+});
+
+afterEach(async () => {
+ await act(async () => root.unmount());
+ container.remove();
+ vi.unstubAllGlobals();
+ delete actEnvironment.IS_REACT_ACT_ENVIRONMENT;
+});
+
+async function clickGoogle() {
+ const button = [...container.querySelectorAll("button")].find((element) =>
+ element.textContent?.includes("Google"),
+ );
+ expect(button).toBeDefined();
+ await act(async () => button?.click());
+}
+
+describe.each([
+ ["login", LoginForm],
+ ["signup", SignupForm],
+] as const)("%s OAuth failures", (surface, Form) => {
+ it("shows a retry message after a failed request and allows another attempt", async () => {
+ mocks.signIn.mockRejectedValueOnce(new TypeError("Load failed"));
+ await act(async () => root.render(createElement(Form)));
+ await clickGoogle();
+ expect(mocks.error).toHaveBeenCalledWith(
+ "Could not start sign-in. Check your connection and try again.",
+ );
+ expect(mocks.captureException).toHaveBeenCalledWith(expect.any(TypeError), {
+ tags: { auth_provider: "google", auth_surface: surface },
+ });
+ await clickGoogle();
+ expect(mocks.signIn).toHaveBeenCalledTimes(2);
+ });
+
+ it("preserves the callback and does not show an error after success", async () => {
+ mocks.searchParams.set("next", "/dashboard/caps");
+ await act(async () => root.render(createElement(Form)));
+ await clickGoogle();
+ expect(mocks.signIn).toHaveBeenCalledWith("google", {
+ callbackUrl: "/dashboard/caps",
+ });
+ expect(mocks.error).not.toHaveBeenCalled();
+ });
+});
+
+it("handles failed automatic mobile google sign-in without an unhandled rejection", async () => {
+ mocks.searchParams.set("mobileProvider", "google");
+ mocks.signIn.mockRejectedValueOnce(new TypeError("Load failed"));
+ await act(async () => root.render(createElement(LoginForm)));
+ expect(mocks.signIn).toHaveBeenCalledWith("google", {});
+ expect(mocks.error).toHaveBeenCalledWith(
+ "Could not start sign-in. Check your connection and try again.",
+ );
+});
+
+it("offers a retry after failed automatic mobile apple sign-in", async () => {
+ mocks.searchParams.set("mobileProvider", "apple");
+ mocks.signIn.mockRejectedValueOnce(new TypeError("Load failed"));
+ await act(async () => root.render(createElement(LoginForm)));
+ expect(mocks.signIn).toHaveBeenCalledWith("apple", {});
+ expect(mocks.error).toHaveBeenLastCalledWith(
+ "Could not start sign-in. Check your connection and try again.",
+ { action: { label: "Try again", onClick: expect.any(Function) } },
+ );
+
+ const options = mocks.error.mock.lastCall?.[1] as
+ | { action: { onClick: () => void } }
+ | undefined;
+ await act(async () => options?.action.onClick());
+ expect(mocks.signIn).toHaveBeenCalledTimes(2);
+ expect(mocks.signIn).toHaveBeenLastCalledWith("apple", {});
+});
diff --git a/apps/web/__tests__/unit/obs-alternative-page.test.ts b/apps/web/__tests__/unit/obs-alternative-page.test.ts
index a646e5b1c..2df55636e 100644
--- a/apps/web/__tests__/unit/obs-alternative-page.test.ts
+++ b/apps/web/__tests__/unit/obs-alternative-page.test.ts
@@ -65,9 +65,10 @@ describe("ObsAlternativePage component content", () => {
expect(componentSource).toContain("migrationGuide");
});
- it("mentions open source and MIT license", () => {
+ it("mentions AGPLv3 and specified MIT crates", () => {
expect(componentSource.toLowerCase()).toContain("open source");
- expect(componentSource.toLowerCase()).toContain("mit");
+ expect(componentSource).toContain("AGPLv3");
+ expect(componentSource).toContain("MIT");
});
it("mentions instant sharing capability", () => {
@@ -109,7 +110,8 @@ describe("ObsAlternativePage FAQ schema", () => {
},
{
question: "Is Cap open source like OBS?",
- answer: "Yes. Cap is fully open source and MIT-licensed on GitHub.",
+ answer:
+ "Yes. Most Cap code is under AGPLv3, with specified Rust crates under MIT. The source is available on GitHub.",
},
{
question: "Can Cap do live streaming like OBS?",
diff --git a/apps/web/__tests__/unit/open-source-screen-recorder-page.test.ts b/apps/web/__tests__/unit/open-source-screen-recorder-page.test.ts
index c66095536..e54cdc739 100644
--- a/apps/web/__tests__/unit/open-source-screen-recorder-page.test.ts
+++ b/apps/web/__tests__/unit/open-source-screen-recorder-page.test.ts
@@ -67,9 +67,10 @@ describe("OpenSourceScreenRecorderPage component content", () => {
expect(componentSource).toContain("OBS");
});
- it("mentions open source and MIT license", () => {
+ it("mentions AGPLv3 and specified MIT crates", () => {
expect(componentSource.toLowerCase()).toContain("open source");
- expect(componentSource.toLowerCase()).toContain("mit");
+ expect(componentSource).toContain("AGPLv3");
+ expect(componentSource).toContain("MIT");
});
it("mentions self-hosting capability", () => {
@@ -94,7 +95,7 @@ describe("OpenSourceScreenRecorderPage FAQ schema", () => {
{
question: "Is Cap really open source?",
answer:
- "Yes. Cap is fully open source and MIT-licensed. The complete codebase is publicly available on GitHub.",
+ "Yes. Most Cap code is under AGPLv3, with specified Rust crates under MIT. The source is available on GitHub.",
},
{
question: "Can I self-host Cap's screen recordings?",
@@ -130,7 +131,7 @@ describe("OpenSourceScreenRecorderPage FAQ schema", () => {
{
question: "What license does Cap use?",
answer:
- "Cap is released under the MIT License. You are free to use, copy, modify, merge, publish, distribute, sublicense, and sell copies.",
+ "Most Cap code is under AGPLv3. The cap-camera* and scap-* Rust crate families are under MIT.",
},
];
diff --git a/apps/web/__tests__/unit/organization-storage-s3-endpoint.test.ts b/apps/web/__tests__/unit/organization-storage-s3-endpoint.test.ts
new file mode 100644
index 000000000..1ba8dd701
--- /dev/null
+++ b/apps/web/__tests__/unit/organization-storage-s3-endpoint.test.ts
@@ -0,0 +1,184 @@
+import { Agent as HttpAgent } from "node:http";
+import { Agent as HttpsAgent } from "node:https";
+import type { Organisation } from "@cap/web-domain";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const state = vi.hoisted(() => ({
+ buildEnv: { NEXT_PUBLIC_IS_CAP: "true" as string | undefined },
+ NODE_ENV: "production",
+ s3Configs: [] as Array<Record<string, unknown>>,
+ send: vi.fn(async () => ({})),
+ insert: vi.fn(),
+}));
+
+vi.mock("@cap/env", () => ({
+ get buildEnv() {
+ return state.buildEnv;
+ },
+ get NODE_ENV() {
+ return state.NODE_ENV;
+ },
+ serverEnv: () => ({ NEXTAUTH_SECRET: "synthetic-secret" }),
+}));
+
+vi.mock("@aws-sdk/client-s3", () => ({
+ HeadBucketCommand: class {
+ constructor(readonly input: unknown) {}
+ },
+ S3Client: class {
+ constructor(config: Record<string, unknown>) {
+ state.s3Configs.push(config);
+ }
+ send = state.send;
+ },
+}));
+
+const query = (result: unknown): unknown =>
+ new Proxy(() => undefined, {
+ get: (_, property) =>
+ property === "then"
+ ? (resolve: (value: unknown) => void) => resolve(result)
+ : () => query(result),
+ });
+
+type FakeDatabase = {
+ select: () => unknown;
+ update: () => unknown;
+ insert: (...args: unknown[]) => unknown;
+ transaction: (
+ callback: (tx: FakeDatabase) => Promise<unknown>,
+ ) => Promise<unknown>;
+};
+
+const database: FakeDatabase = {
+ select: () => query([{ id: "org_synthetic", name: "Org", ownerId: "usr" }]),
+ update: () => query(undefined),
+ insert: (...args: unknown[]) => {
+ state.insert(...args);
+ return query(undefined);
+ },
+ transaction: async (callback) => callback(database),
+};
+
+vi.mock("@cap/database", () => ({ db: () => database }));
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: async () => ({ id: "usr_synthetic" }),
+}));
+vi.mock("@cap/database/crypto", () => ({
+ encrypt: async (value: string) => `enc:${value}`,
+ decrypt: async (value: string) => value.replace(/^enc:/, ""),
+}));
+vi.mock("@cap/database/helpers", () => ({ nanoId: () => "synthetic_id" }));
+vi.mock("@cap/database/schema", () => ({
+ organizations: {},
+ s3Buckets: {},
+ storageIntegrations: {},
+ storageObjects: {},
+ videos: {},
+}));
+vi.mock("@cap/utils", () => ({ userIsPro: () => true }));
+vi.mock("@cap/web-backend", () => ({}));
+vi.mock("@cap/web-domain", () => ({
+ S3Bucket: { S3BucketId: { make: (id: string) => id } },
+ Storage: { StorageIntegrationId: { make: (id: string) => id } },
+}));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+vi.mock("@/lib/server", () => ({ runPromise: vi.fn() }));
+vi.mock("@/actions/organization/authorization", () => ({
+ requireOrganizationSettingsManager: async () => undefined,
+}));
+
+import {
+ saveOrganizationS3Config,
+ testOrganizationS3Config,
+} from "@/actions/organization/storage";
+import { blockedS3EndpointMessage } from "@/lib/s3-endpoint-policy";
+
+const input = (endpoint: string) => ({
+ organizationId: "org_synthetic" as Organisation.OrganisationId,
+ provider: "aws",
+ accessKeyId: "AKIASYNTHETIC",
+ secretAccessKey: "synthetic-secret",
+ endpoint,
+ bucketName: "synthetic-bucket",
+ region: "us-east-1",
+});
+
+describe("organization storage S3 endpoint policy", () => {
+ beforeEach(() => {
+ state.buildEnv.NEXT_PUBLIC_IS_CAP = "true";
+ state.NODE_ENV = "production";
+ state.s3Configs.length = 0;
+ state.send.mockClear();
+ state.insert.mockClear();
+ });
+
+ it.each([
+ "http://169.254.169.254",
+ "http://127.0.0.1:9000",
+ "http://localhost:9000",
+ "http://[::1]:9000",
+ ])("refuses to save %s on Cap Cloud", async (endpoint) => {
+ await expect(saveOrganizationS3Config(input(endpoint))).rejects.toThrow(
+ blockedS3EndpointMessage,
+ );
+ expect(state.insert).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ "http://169.254.169.254",
+ "http://10.0.0.5",
+ "http://minio.localhost",
+ ])("refuses to test %s on Cap Cloud", async (endpoint) => {
+ await expect(testOrganizationS3Config(input(endpoint))).rejects.toThrow(
+ blockedS3EndpointMessage,
+ );
+ expect(state.s3Configs).toHaveLength(0);
+ expect(state.send).not.toHaveBeenCalled();
+ });
+
+ it("saves public and default endpoints on Cap Cloud", async () => {
+ await expect(
+ saveOrganizationS3Config(input("https://8.8.8.8")),
+ ).resolves.toEqual({ success: true });
+ await expect(saveOrganizationS3Config(input(""))).resolves.toEqual({
+ success: true,
+ });
+ expect(state.insert).toHaveBeenCalledTimes(2);
+ });
+
+ it("tests public endpoints through public-only agents on Cap Cloud", async () => {
+ await expect(
+ testOrganizationS3Config(input("https://8.8.8.8")),
+ ).resolves.toEqual({ success: true });
+
+ expect(state.s3Configs).toHaveLength(1);
+ const requestHandler = state.s3Configs[0]?.requestHandler as {
+ httpAgent?: unknown;
+ httpsAgent?: unknown;
+ };
+ expect(requestHandler.httpAgent).toBeInstanceOf(HttpAgent);
+ expect(requestHandler.httpsAgent).toBeInstanceOf(HttpsAgent);
+ expect(state.send).toHaveBeenCalledTimes(1);
+ });
+
+ it.each([
+ ["self-hosted", undefined, "production"],
+ ["development", "true", "development"],
+ ])("allows local endpoints on %s deployments", async (_, isCap, nodeEnv) => {
+ state.buildEnv.NEXT_PUBLIC_IS_CAP = isCap;
+ state.NODE_ENV = nodeEnv;
+
+ await expect(
+ saveOrganizationS3Config(input("http://localhost:9000")),
+ ).resolves.toEqual({ success: true });
+ await expect(
+ testOrganizationS3Config(input("http://192.168.1.10:9000")),
+ ).resolves.toEqual({ success: true });
+
+ expect(state.insert).toHaveBeenCalledTimes(1);
+ expect(state.s3Configs).toHaveLength(1);
+ expect(state.s3Configs[0]?.endpoint).toBe("http://192.168.1.10:9000");
+ expect(state.s3Configs[0]?.requestHandler).toBeUndefined();
+ });
+});
diff --git a/apps/web/__tests__/unit/playback-store.test.ts b/apps/web/__tests__/unit/playback-store.test.ts
new file mode 100644
index 000000000..6b8abec03
--- /dev/null
+++ b/apps/web/__tests__/unit/playback-store.test.ts
@@ -0,0 +1,119 @@
+import { describe, expect, it, vi } from "vitest";
+import { createPlaybackStore } from "@/app/s/[videoId]/_components/playback/playback-store";
+
+describe("playback store", () => {
+ it.each(["loadedmetadata", "durationchange"])(
+ "publishes the loaded duration on %s while paused at zero",
+ (event) => {
+ const video = Object.assign(new EventTarget(), {
+ currentTime: 0,
+ duration: Number.NaN,
+ paused: true,
+ ended: false,
+ });
+ const store = createPlaybackStore();
+ let observedDuration = 0;
+ store.subscribe(() => {
+ observedDuration = store.getDuration();
+ });
+ store.attach(video as unknown as HTMLVideoElement);
+
+ video.duration = 120;
+ video.dispatchEvent(new Event(event));
+
+ expect(observedDuration).toBe(120);
+ expect(store.getCurrentTime()).toBe(0);
+ store.detach();
+ },
+ );
+
+ it("publishes duration changes without repeating unchanged samples", () => {
+ const video = Object.assign(new EventTarget(), {
+ currentTime: 5,
+ duration: 30,
+ paused: true,
+ ended: false,
+ });
+ const store = createPlaybackStore(30);
+ store.attach(video as unknown as HTMLVideoElement);
+ const subscriber = vi.fn(() => store.getDuration());
+ store.subscribe(subscriber);
+ subscriber.mockClear();
+
+ video.duration = 45;
+ video.dispatchEvent(new Event("durationchange"));
+ video.dispatchEvent(new Event("loadedmetadata"));
+ video.dispatchEvent(new Event("timeupdate"));
+
+ expect(subscriber).toHaveBeenCalledExactlyOnceWith(5);
+ expect(subscriber).toHaveReturnedWith(45);
+ store.detach();
+ });
+
+ it("keeps time updates and the last finite duration until detached", () => {
+ const video = Object.assign(new EventTarget(), {
+ currentTime: 0,
+ duration: 30,
+ paused: true,
+ ended: false,
+ });
+ const store = createPlaybackStore();
+ store.attach(video as unknown as HTMLVideoElement);
+ const subscriber = vi.fn(() => store.getDuration());
+ store.subscribe(subscriber);
+ subscriber.mockClear();
+
+ video.duration = Number.POSITIVE_INFINITY;
+ video.dispatchEvent(new Event("durationchange"));
+ expect(subscriber).not.toHaveBeenCalled();
+ expect(store.getDuration()).toBe(30);
+
+ video.currentTime = 6;
+ video.dispatchEvent(new Event("timeupdate"));
+ expect(subscriber).toHaveBeenCalledExactlyOnceWith(6);
+ expect(subscriber).toHaveReturnedWith(30);
+
+ store.detach();
+ video.currentTime = 7;
+ video.duration = 45;
+ video.dispatchEvent(new Event("durationchange"));
+ video.dispatchEvent(new Event("timeupdate"));
+ expect(subscriber).toHaveBeenCalledTimes(1);
+ });
+
+ it("notifies subscribers when late fallbackDuration changes the store duration", () => {
+ const store = createPlaybackStore(0);
+ let observedDuration = store.getDuration();
+ store.subscribe(() => {
+ observedDuration = store.getDuration();
+ });
+
+ expect(observedDuration).toBe(0);
+
+ store.setFallbackDuration(60);
+
+ expect(observedDuration).toBe(60);
+ expect(store.getDuration()).toBe(60);
+ });
+
+ it("does not notify subscribers when fallbackDuration does not change total duration", () => {
+ const video = Object.assign(new EventTarget(), {
+ currentTime: 0,
+ duration: 120,
+ paused: true,
+ ended: false,
+ });
+ const store = createPlaybackStore(60);
+ store.attach(video as unknown as HTMLVideoElement);
+
+ const subscriber = vi.fn(() => store.getDuration());
+ store.subscribe(subscriber);
+ subscriber.mockClear();
+
+ store.setFallbackDuration(90);
+
+ expect(subscriber).not.toHaveBeenCalled();
+ expect(store.getDuration()).toBe(120);
+ store.detach();
+ });
+});
diff --git a/apps/web/__tests__/unit/proxy-frame-headers.test.ts b/apps/web/__tests__/unit/proxy-frame-headers.test.ts
new file mode 100644
index 000000000..30ac2a61c
--- /dev/null
+++ b/apps/web/__tests__/unit/proxy-frame-headers.test.ts
@@ -0,0 +1,105 @@
+import { NextRequest } from "next/server";
+import { describe, expect, it, vi } from "vitest";
+import { proxy } from "../../proxy";
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({ from: () => ({ where: async () => [] }) }),
+ }),
+}));
+
+vi.mock("@cap/database/schema", () => ({ organizations: {} }));
+
+vi.mock("@cap/env", () => ({
+ buildEnv: { NEXT_PUBLIC_IS_CAP: "true" },
+ serverEnv: () => ({
+ WEB_URL: "https://cap.so",
+ VERCEL_URL_HOST: undefined,
+ VERCEL_BRANCH_URL_HOST: undefined,
+ VERCEL_PROJECT_PRODUCTION_URL_HOST: undefined,
+ }),
+}));
+
+const send = (url: string, headers: Record<string, string> = {}) =>
+ proxy(new NextRequest(url, { headers }));
+
+const frameAncestors = (response: Response) =>
+ [...response.headers.values()].filter((value) =>
+ value.startsWith("frame-ancestors"),
+ );
+
+const expectFrameHeaders = (response: Response) => {
+ expect(response.headers.get("x-frame-options")).toBe("SAMEORIGIN");
+ expect(frameAncestors(response)).toEqual(["frame-ancestors https://cap.so"]);
+};
+
+const expectNoFrameHeaders = (response: Response) => {
+ expect(response.headers.get("x-frame-options")).toBeNull();
+ expect(frameAncestors(response)).toEqual([]);
+};
+
+describe("proxy frame headers", () => {
+ it.each([
+ "/login",
+ "/dashboard",
+ "/dashboard/caps",
+ "/dashboard/settings/account",
+ "/dashboard/settings/organization/billing",
+ "/onboarding",
+ "/onboarding/organization-setup",
+ "/invite/invite-id",
+ "/cli/authorize",
+ "/mcp/authorize",
+ "/admin",
+ "/admin/replace-video",
+ "/admin/reprocess-video",
+ "/signup",
+ "/verify-otp",
+ ])("sets frame headers on %s", async (path) => {
+ const response = await send(`https://cap.so${path}`);
+ expect(response.status).toBe(200);
+ expectFrameHeaders(response);
+ });
+
+ it.each([
+ "/",
+ "/s/video-id",
+ "/embed/video-id",
+ "/c/collection-id",
+ "/pricing",
+ "/blog/some-post",
+ "/cli/complete",
+ "/dashboards",
+ "/invited",
+ "/administrator",
+ "/signups",
+ "/verify-otp-help",
+ ])("leaves %s without frame headers", async (path) => {
+ const response = await send(`https://cap.so${path}`);
+ expectNoFrameHeaders(response);
+ });
+
+ it("leaves the share iframe redirect without frame headers", async () => {
+ const response = await send("https://cap.so/s/video-id", {
+ "sec-fetch-dest": "iframe",
+ });
+ expect(response.headers.get("location")).toBe(
+ "https://cap.so/embed/video-id",
+ );
+ expectNoFrameHeaders(response);
+ });
+
+ it("keeps redirecting app pages on custom domains to the main origin", async () => {
+ const response = await send("https://videos.customer.example/dashboard");
+ expect(response.headers.get("location")).toBe("https://cap.so/dashboard");
+ expectFrameHeaders(response);
+ });
+
+ it("keeps serving share pages on verified custom domains without frame headers", async () => {
+ const response = await send("https://videos.customer.example/s/video-id", {
+ cookie: "verified_domain=videos.customer.example",
+ });
+ expect(response.status).toBe(200);
+ expectNoFrameHeaders(response);
+ });
+});
diff --git a/apps/web/__tests__/unit/public-license-claims.test.ts b/apps/web/__tests__/unit/public-license-claims.test.ts
new file mode 100644
index 000000000..e3c2289e7
--- /dev/null
+++ b/apps/web/__tests__/unit/public-license-claims.test.ts
@@ -0,0 +1,50 @@
+import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
+import { join, relative } from "node:path";
+import { describe, expect, it } from "vitest";
+
+const root = process.cwd();
+
+const scannedRoots = [
+ "app/(site)",
+ "components/pages",
+ "content",
+ "lib/seo-metadata.ts",
+ "public/llms.txt",
+ "public/llms-full.txt",
+];
+
+const scannedExtensions = [".ts", ".tsx", ".md", ".mdx", ".json", ".txt"];
+
+const staleLicenseClaim = /MIT[- ]licen[sc]|"Yes(?:,| —) MIT"/i;
+
+function collectFiles(path: string): string[] {
+ const absolute = join(root, path);
+ if (!existsSync(absolute)) return [];
+ if (statSync(absolute).isFile()) return [absolute];
+ return readdirSync(absolute, { recursive: true, encoding: "utf-8" })
+ .map((entry) => join(absolute, entry))
+ .filter(
+ (file) =>
+ scannedExtensions.some((extension) => file.endsWith(extension)) &&
+ statSync(file).isFile(),
+ );
+}
+
+describe("public license claims", () => {
+ it("does not describe Cap as MIT-licensed", () => {
+ const files = scannedRoots.flatMap(collectFiles);
+ expect(files.length).toBeGreaterThan(0);
+
+ const offenders = files.flatMap((file) =>
+ readFileSync(file, "utf-8")
+ .split("\n")
+ .flatMap((line, index) =>
+ staleLicenseClaim.test(line)
+ ? [`${relative(root, file)}:${index + 1}`]
+ : [],
+ ),
+ );
+
+ expect(offenders).toEqual([]);
+ });
+});
diff --git a/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts b/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
index 45896b27b..ee7e21f39 100644
--- a/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
+++ b/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
@@ -134,13 +134,14 @@ type DatabaseMutation = {
values?: Record<string, unknown>;
};
-function databaseFixture(video = recording()) {
+function databaseFixture(video = recording(), password?: string | null) {
const encoded = Schema.encodeSync(Video.Video)(video);
const original = {
...encoded,
bucket: encoded.bucketId,
createdAt: video.createdAt,
updatedAt: video.updatedAt,
+ password: password ?? null,
};
const rows = new Map<string, Record<string, unknown>>([[videoId, original]]);
const jobs = new Map<string, Record<string, unknown>>([
@@ -902,6 +903,12 @@ describe("recording storage lifecycle", () => {
expect(database.rows.get("duplicate-video")?.metadata).not.toHaveProperty(
"desktopRecordingUpload",
);
+ expect(database.rows.get("duplicate-video")).not.toHaveProperty(
+ "createdAt",
+ );
+ expect(database.rows.get("duplicate-video")).not.toHaveProperty(
+ "updatedAt",
+ );
expect(database.rows.get("owned-video")?.metadata).toHaveProperty(
"desktopRecordingUpload",
);
@@ -943,6 +950,32 @@ describe("recording storage lifecycle", () => {
expect(storage.objects.get(`${prefix}result.mp4`)).toBe("original");
});
+ it("preserves password and strips customCreatedAt without copying original createdAt or updatedAt", async () => {
+ const videoWithCustomDate = Video.Video.make({
+ ...recording(),
+ metadata: Option.some({
+ sourceName: "Dated recording",
+ customCreatedAt: "2020-01-01T00:00:00.000Z",
+ }),
+ });
+ const database = databaseFixture(videoWithCustomDate, "secret-pass-123");
+ await storageFixture([
+ [outputKey, "verified-video"],
+ [thumbnailKey, "verified-thumbnail"],
+ [previewKey, "verified-preview"],
+ ]);
+ const result = await runVideoOperation("duplicate");
+ expect(Exit.isSuccess(result)).toBe(true);
+ const duplicated = database.rows.get("duplicate-video");
+ expect(duplicated).toBeDefined();
+ expect(duplicated).not.toHaveProperty("createdAt");
+ expect(duplicated).not.toHaveProperty("updatedAt");
+ expect(duplicated?.metadata).not.toHaveProperty("customCreatedAt");
+ expect(duplicated).toMatchObject({
+ password: "secret-pass-123",
+ });
+ });
+
it("does not duplicate source inventories, raw fragments, or comment attachments across pages", async () => {
const database = databaseFixture();
const retained = Array.from(
diff --git a/apps/web/__tests__/unit/remove-organization-domain.test.ts b/apps/web/__tests__/unit/remove-organization-domain.test.ts
new file mode 100644
index 000000000..d0d04ebe6
--- /dev/null
+++ b/apps/web/__tests__/unit/remove-organization-domain.test.ts
@@ -0,0 +1,80 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ updates: [] as unknown[],
+}));
+
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: async () => ({ id: "owner" }),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({
+ from: () => ({
+ where: async () => [{ id: "org", customDomain: "video.example.com" }],
+ }),
+ }),
+ update: () => ({
+ set: (values: unknown) => ({
+ where: async () => {
+ mocks.updates.push(values);
+ },
+ }),
+ }),
+ }),
+}));
+
+vi.mock("@/actions/organization/authorization", () => ({
+ requireOrganizationSettingsManager: async () => ({}),
+}));
+
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+
+import type { Organisation } from "@cap/web-domain";
+import { removeOrganizationDomain } from "@/actions/organization/remove-domain";
+
+const organizationId = "org" as Organisation.OrganisationId;
+
+describe("removeOrganizationDomain", () => {
+ beforeEach(() => {
+ mocks.updates = [];
+ vi.unstubAllGlobals();
+ });
+
+ it("keeps the domain when the provider rejects removal", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(new Response(null, { status: 403 })),
+ );
+
+ await expect(removeOrganizationDomain(organizationId)).rejects.toThrow();
+ expect(mocks.updates).toEqual([]);
+ });
+
+ it("clears the domain after the provider removes it", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(new Response(null, { status: 204 })),
+ );
+
+ await expect(removeOrganizationDomain(organizationId)).resolves.toEqual({
+ success: true,
+ });
+ expect(mocks.updates).toEqual([
+ { customDomain: null, domainVerified: null },
+ ]);
+ });
+
+ it("clears a domain that is already absent at the provider", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(new Response(null, { status: 404 })),
+ );
+
+ await expect(removeOrganizationDomain(organizationId)).resolves.toEqual({
+ success: true,
+ });
+ expect(mocks.updates).toHaveLength(1);
+ });
+});
diff --git a/apps/web/__tests__/unit/s3-bucket-connections.test.ts b/apps/web/__tests__/unit/s3-bucket-connections.test.ts
index 07df81095..df73c43bc 100644
--- a/apps/web/__tests__/unit/s3-bucket-connections.test.ts
+++ b/apps/web/__tests__/unit/s3-bucket-connections.test.ts
@@ -40,7 +40,10 @@ vi.mock("@cap/web-backend/src/S3Buckets/S3BucketsRepo.ts", async () => {
});
import { S3Buckets } from "@cap/web-backend/src/S3Buckets";
-import { s3ConnectionPool } from "@cap/web-backend/src/S3Buckets/S3ConnectionPool";
+import {
+ defaultBucketS3ConnectionPool,
+ s3ConnectionPool,
+} from "@cap/web-backend/src/S3Buckets/S3ConnectionPool";
async function storageFixture() {
let connections = 0;
@@ -115,7 +118,7 @@ describe("S3 connection reuse", () => {
}),
);
expect(fixture.authorizations).toHaveLength(192);
- expect(fixture.connections()).toBeLessThanOrEqual(50);
+ expect(fixture.connections()).toBeLessThanOrEqual(128);
} finally {
await fixture.close();
}
@@ -193,6 +196,40 @@ describe("S3 connection reuse", () => {
}
});
+ it("keeps default bucket connections warm across 32-wide snapshot pages", async () => {
+ const fixture = await storageFixture();
+ try {
+ const [access] = await fixture.runtime.runPromise(
+ fixture.service.getBucketAccess(Option.none()),
+ );
+ for (let checkpoint = 0; checkpoint < 10; checkpoint++) {
+ await Promise.all(
+ Array.from({ length: 32 }, (_, index) =>
+ fixture.runtime.runPromise(
+ access.headObject(`recording/${checkpoint}/${index}`),
+ ),
+ ),
+ );
+ }
+ expect(fixture.authorizations).toHaveLength(320);
+ expect(fixture.connections()).toBeLessThanOrEqual(40);
+ } finally {
+ await fixture.close();
+ }
+ });
+
+ it("keeps the custom bucket idle connection bound", async () => {
+ const pools = await Effect.runPromise(
+ Effect.scoped(
+ Effect.all([s3ConnectionPool, defaultBucketS3ConnectionPool]),
+ ),
+ );
+ expect(pools.map((pool) => pool.httpsAgent.maxFreeSockets)).toEqual([
+ 8, 32,
+ ]);
+ expect(pools.map((pool) => pool.httpsAgent.maxSockets)).toEqual([50, 128]);
+ });
+
it("reuses custom bucket connections while using current credentials", async () => {
const fixture = await storageFixture();
try {
diff --git a/apps/web/__tests__/unit/s3-endpoint-policy.test.ts b/apps/web/__tests__/unit/s3-endpoint-policy.test.ts
new file mode 100644
index 000000000..92b461162
--- /dev/null
+++ b/apps/web/__tests__/unit/s3-endpoint-policy.test.ts
@@ -0,0 +1,212 @@
+import { Agent, createServer, get } from "node:http";
+import { Agent as HttpsAgent } from "node:https";
+import type { AddressInfo } from "node:net";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const env = vi.hoisted(() => ({
+ buildEnv: { NEXT_PUBLIC_IS_CAP: "true" as string | undefined },
+ NODE_ENV: "production",
+}));
+
+vi.mock("@cap/env", () => env);
+
+import {
+ isBlockedS3Endpoint,
+ isDisallowedS3Endpoint,
+ isPrivateAddress,
+ publicOnlyLookup,
+ publicOnlyS3Agents,
+ shouldBlockPrivateS3Endpoints,
+} from "@/lib/s3-endpoint-policy";
+
+const resolveTo = (addresses: string[]) => async () => addresses;
+
+describe("isPrivateAddress", () => {
+ it.each([
+ "127.0.0.1",
+ "10.1.2.3",
+ "172.16.0.1",
+ "192.168.1.10",
+ "169.254.169.254",
+ "100.64.0.1",
+ "0.0.0.0",
+ "::",
+ "::1",
+ "fc00::1",
+ "fd12::1",
+ "fe80::1",
+ "fe90::1",
+ "febf::1",
+ "ff02::1",
+ "::127.0.0.1",
+ "::7f00:1",
+ "::8.8.8.8",
+ "::ffff:127.0.0.1",
+ "::ffff:7f00:1",
+ "::ffff:a9fe:a9fe",
+ "::ffff:10.0.0.1",
+ "0:0:0:0:0:ffff:7f00:1",
+ "64:ff9b::a9fe:a9fe",
+ "not-an-ip",
+ ])("blocks %s", (address) => {
+ expect(isPrivateAddress(address)).toBe(true);
+ });
+
+ it.each([
+ "8.8.8.8",
+ "52.216.1.1",
+ "::ffff:8.8.8.8",
+ "2600:1f18::1",
+ "2001:4860:4860::8888",
+ ])("allows %s", (address) => {
+ expect(isPrivateAddress(address)).toBe(false);
+ });
+});
+
+describe("isBlockedS3Endpoint", () => {
+ it.each([
+ "http://127.0.0.1:9000",
+ "http://[::1]:9000",
+ "http://[::ffff:127.0.0.1]",
+ "http://[::ffff:a9fe:a9fe]",
+ "http://[::127.0.0.1]",
+ "http://0x7f000001",
+ "http://localhost:9000",
+ "http://minio.localhost",
+ "http://metadata.google.internal",
+ "http://user:[email protected]",
+ "ftp://s3.example.com",
+ "not a url",
+ ])("blocks %s without resolving", async (endpoint) => {
+ const resolve = vi.fn(resolveTo(["52.216.1.1"]));
+ await expect(isBlockedS3Endpoint(endpoint, resolve)).resolves.toBe(true);
+ expect(resolve).not.toHaveBeenCalled();
+ });
+
+ it("blocks hostnames that resolve to any private address", async () => {
+ await expect(
+ isBlockedS3Endpoint(
+ "https://s3.example.com",
+ resolveTo(["52.216.1.1", "10.0.0.5"]),
+ ),
+ ).resolves.toBe(true);
+ });
+
+ it("blocks hostnames that do not resolve", async () => {
+ await expect(
+ isBlockedS3Endpoint("https://s3.example.com", async () => {
+ throw new Error("ENOTFOUND");
+ }),
+ ).resolves.toBe(true);
+ });
+
+ it("allows public endpoints", async () => {
+ await expect(
+ isBlockedS3Endpoint(
+ "https://s3.us-east-1.amazonaws.com",
+ resolveTo(["52.216.1.1", "2600:1f18::1"]),
+ ),
+ ).resolves.toBe(false);
+ await expect(
+ isBlockedS3Endpoint("https://8.8.8.8", resolveTo([])),
+ ).resolves.toBe(false);
+ });
+});
+
+describe("publicOnlyLookup", () => {
+ it("refuses connections whose hostname resolves to a private address", async () => {
+ const server = createServer((_, response) => response.end("ok"));
+ await new Promise<void>((resolve) =>
+ server.listen(0, "127.0.0.1", resolve),
+ );
+ const { port } = server.address() as AddressInfo;
+
+ try {
+ const error = await new Promise<NodeJS.ErrnoException | null>(
+ (resolve) => {
+ get(
+ `http://localhost:${port}/`,
+ { agent: new Agent({ lookup: publicOnlyLookup }), family: 4 },
+ (response) => {
+ response.resume();
+ resolve(null);
+ },
+ ).on("error", resolve);
+ },
+ );
+
+ expect(error?.message).toContain("Refused to connect to private address");
+ } finally {
+ server.close();
+ }
+ });
+});
+
+describe("shouldBlockPrivateS3Endpoints", () => {
+ beforeEach(() => {
+ env.buildEnv.NEXT_PUBLIC_IS_CAP = "true";
+ env.NODE_ENV = "production";
+ });
+
+ it("only enforces on Cap Cloud production", () => {
+ expect(shouldBlockPrivateS3Endpoints()).toBe(true);
+
+ env.NODE_ENV = "development";
+ expect(shouldBlockPrivateS3Endpoints()).toBe(false);
+
+ env.NODE_ENV = "production";
+ env.buildEnv.NEXT_PUBLIC_IS_CAP = undefined;
+ expect(shouldBlockPrivateS3Endpoints()).toBe(false);
+ });
+});
+
+describe("isDisallowedS3Endpoint", () => {
+ beforeEach(() => {
+ env.buildEnv.NEXT_PUBLIC_IS_CAP = "true";
+ env.NODE_ENV = "production";
+ });
+
+ it("blocks private endpoints on Cap Cloud production", async () => {
+ await expect(isDisallowedS3Endpoint("http://127.0.0.1:9000")).resolves.toBe(
+ true,
+ );
+ await expect(isDisallowedS3Endpoint("https://8.8.8.8")).resolves.toBe(
+ false,
+ );
+ });
+
+ it("leaves an empty endpoint to the AWS default", async () => {
+ await expect(isDisallowedS3Endpoint("")).resolves.toBe(false);
+ });
+
+ it("allows private endpoints outside Cap Cloud production", async () => {
+ env.buildEnv.NEXT_PUBLIC_IS_CAP = undefined;
+ await expect(isDisallowedS3Endpoint("http://localhost:9000")).resolves.toBe(
+ false,
+ );
+
+ env.buildEnv.NEXT_PUBLIC_IS_CAP = "true";
+ env.NODE_ENV = "development";
+ await expect(isDisallowedS3Endpoint("http://10.0.0.5:9000")).resolves.toBe(
+ false,
+ );
+ });
+});
+
+describe("publicOnlyS3Agents", () => {
+ beforeEach(() => {
+ env.buildEnv.NEXT_PUBLIC_IS_CAP = "true";
+ env.NODE_ENV = "production";
+ });
+
+ it("returns public-only agents on Cap Cloud production", () => {
+ const agents = publicOnlyS3Agents();
+ expect(agents?.httpAgent).toBeInstanceOf(Agent);
+ expect(agents?.httpsAgent).toBeInstanceOf(HttpsAgent);
+ });
+
+ it("keeps the SDK default handler elsewhere", () => {
+ env.buildEnv.NEXT_PUBLIC_IS_CAP = undefined;
+ expect(publicOnlyS3Agents()).toBeUndefined();
+ });
+});
diff --git a/apps/web/__tests__/unit/save-video-edits-original-source.test.ts b/apps/web/__tests__/unit/save-video-edits-original-source.test.ts
new file mode 100644
index 000000000..4218049e8
--- /dev/null
+++ b/apps/web/__tests__/unit/save-video-edits-original-source.test.ts
@@ -0,0 +1,212 @@
+import { Effect } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const tables = vi.hoisted(() => ({
+ videos: { id: "videos.id" },
+ videoUploads: { videoId: "videoUploads.videoId" },
+ videoEdits: { videoId: "videoEdits.videoId" },
+}));
+
+const state = vi.hoisted(() => ({
+ rows: new Map<unknown, unknown[]>(),
+}));
+
+const headObjectMock = vi.fn();
+const copyObjectMock = vi.fn();
+const clearPendingEditMock = vi.fn();
+const startMock = vi.fn();
+
+function selectFromRows() {
+ return {
+ from: (table: unknown) => ({
+ where: () => {
+ const rows = state.rows.get(table) ?? [];
+ return Object.assign(Promise.resolve(rows), {
+ for: () => Promise.resolve(rows),
+ });
+ },
+ }),
+ };
+}
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: selectFromRows,
+ transaction: async (run: (tx: Record<string, unknown>) => Promise<void>) =>
+ run({
+ select: selectFromRows,
+ insert: () => ({ values: async () => undefined }),
+ update: () => ({
+ set: () => ({ where: async () => undefined }),
+ }),
+ delete: () => ({ where: async () => undefined }),
+ }),
+ }),
+}));
+
+vi.mock("@cap/database/schema", () => tables);
+
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: async () => ({ id: "user-1", isPro: true }),
+}));
+
+vi.mock("@cap/env", () => ({ serverEnv: () => ({}) }));
+
+vi.mock("@cap/utils", () => ({
+ userIsPro: (user?: { isPro?: boolean } | null) => Boolean(user?.isPro),
+}));
+
+vi.mock("@cap/web-backend", () => ({
+ Storage: {
+ getAccessForVideo: () =>
+ Effect.succeed([
+ {
+ bucketName: "cap-bucket",
+ headObject: headObjectMock,
+ copyObject: copyObjectMock,
+ },
+ ]),
+ },
+}));
+
+vi.mock("drizzle-orm", () => ({ eq: () => undefined }));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+vi.mock("workflow/api", () => ({ start: startMock }));
+vi.mock("server-only", () => ({}));
+vi.mock("@/lib/server", () => ({ runPromise: Effect.runPromise }));
+vi.mock("@/lib/video-storage", () => ({ decodeStorageVideo: vi.fn() }));
+vi.mock("@/lib/legacy-video-edit-recovery", () => ({
+ assertLegacyEditsQuiescent: vi.fn(),
+}));
+vi.mock("@/lib/video-edit-operation", () => ({
+ clearPendingEdit: clearPendingEditMock,
+}));
+vi.mock("@/utils/flags", () => ({ isAiGenerationEnabled: async () => false }));
+vi.mock("@/workflows/edit-video", () => ({ editVideoWorkflow: {} }));
+
+const sourceKey = "user-1/video-1/source/original.mp4";
+
+function storageError(cause: unknown) {
+ return { _tag: "StorageError", cause: { _tag: "S3Error", cause } };
+}
+
+function s3Error(name: string, httpStatusCode: number) {
+ return Object.assign(new Error(name), {
+ name,
+ $metadata: { httpStatusCode },
+ });
+}
+
+function setUpVideo({ edited }: { edited: boolean }) {
+ const video = {
+ id: "video-1",
+ ownerId: "user-1",
+ duration: edited ? 120 : 600,
+ source: { type: "webMP4" },
+ bucket: null,
+ storageIntegrationId: null,
+ isScreenshot: false,
+ metadata: null,
+ };
+ state.rows.set(tables.videos, [video]);
+ state.rows.set(tables.videoUploads, []);
+ state.rows.set(
+ tables.videoEdits,
+ edited
+ ? [
+ {
+ videoId: "video-1",
+ sourceKey,
+ editSpec: {
+ version: 1,
+ sourceDuration: 600,
+ keepRanges: [{ start: 0, end: 120 }],
+ },
+ },
+ ]
+ : [],
+ );
+}
+
+async function saveTrim(end: number) {
+ const { saveVideoEdits } = await import("@/actions/videos/save-edits");
+ return saveVideoEdits("video-1" as never, {
+ version: 1,
+ sourceDuration: 600,
+ keepRanges: [{ start: 0, end }],
+ });
+}
+
+describe("saveVideoEdits original source copy", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ state.rows.clear();
+ copyObjectMock.mockReturnValue(Effect.succeed({}));
+ });
+
+ it("keeps the preserved original when its HEAD request fails transiently", async () => {
+ setUpVideo({ edited: true });
+ headObjectMock.mockReturnValue(
+ Effect.fail(storageError(s3Error("SlowDown", 503))),
+ );
+
+ await expect(saveTrim(60)).rejects.toBeDefined();
+
+ expect(copyObjectMock).not.toHaveBeenCalled();
+ expect(startMock).not.toHaveBeenCalled();
+ expect(clearPendingEditMock).toHaveBeenCalledOnce();
+ });
+
+ it("keeps the preserved original when the HEAD request hits a network error", async () => {
+ setUpVideo({ edited: true });
+ headObjectMock.mockReturnValue(
+ Effect.fail(storageError(new Error("socket hang up"))),
+ );
+
+ await expect(saveTrim(60)).rejects.toBeDefined();
+
+ expect(copyObjectMock).not.toHaveBeenCalled();
+ });
+
+ it("preserves the current result as the original on the first edit", async () => {
+ setUpVideo({ edited: false });
+ headObjectMock.mockReturnValue(
+ Effect.fail(storageError(s3Error("NotFound", 404))),
+ );
+
+ await expect(saveTrim(300)).resolves.toEqual({ success: true });
+
+ expect(copyObjectMock).toHaveBeenCalledWith(
+ "cap-bucket/user-1/video-1/result.mp4",
+ sourceKey,
+ );
+ expect(startMock).toHaveBeenCalledOnce();
+ });
+
+ it("treats a Google Drive 404 as a missing original", async () => {
+ setUpVideo({ edited: false });
+ headObjectMock.mockReturnValue(
+ Effect.fail(
+ storageError(
+ Object.assign(new Error("Storage object not found"), {
+ name: "GoogleDriveRequestError",
+ status: 404,
+ }),
+ ),
+ ),
+ );
+
+ await expect(saveTrim(300)).resolves.toEqual({ success: true });
+
+ expect(copyObjectMock).toHaveBeenCalledOnce();
+ });
+
+ it("does not copy when the preserved original exists", async () => {
+ setUpVideo({ edited: true });
+ headObjectMock.mockReturnValue(Effect.succeed({ ContentLength: 1 }));
+
+ await expect(saveTrim(60)).resolves.toEqual({ success: true });
+
+ expect(copyObjectMock).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/segment-playlist-route.test.ts b/apps/web/__tests__/unit/segment-playlist-route.test.ts
index 3de904048..d079178e3 100644
--- a/apps/web/__tests__/unit/segment-playlist-route.test.ts
+++ b/apps/web/__tests__/unit/segment-playlist-route.test.ts
@@ -7,6 +7,11 @@ const mocks = vi.hoisted(() => ({
audioInitExists: false,
storageUnavailable: false,
denied: false,
+ defaultBucket: false,
+ source: { type: "desktopSegments" } as {
+ type: "desktopSegments" | "desktopMP4";
+ outputKey?: string;
+ },
sign: vi.fn(),
read: vi.fn(),
dispose: async () => {},
@@ -23,6 +28,7 @@ vi.mock("@cap/web-backend", async (importOriginal) => {
const { Context, Effect, Option } = await import("effect");
const { Policy, Storage: StorageDomain } = await import("@cap/web-domain");
const bucket = {
+ provider: "s3",
getObject: () =>
Effect.sync(() => {
mocks.read();
@@ -48,7 +54,11 @@ vi.mock("@cap/web-backend", async (importOriginal) => {
effect: import("effect").Effect.Effect<A, E, R>,
) => effect,
Storage: Object.assign(Context.GenericTag("PlaylistTestStorage"), {
- getAccessForVideo: () => Effect.succeed([bucket, false] as const),
+ getAccessForVideo: () =>
+ Effect.succeed([
+ bucket,
+ mocks.defaultBucket ? Option.none() : Option.some({}),
+ ] as const),
}),
Videos: Object.assign(Context.GenericTag("PlaylistTestVideos"), {
testService: {
@@ -60,7 +70,7 @@ vi.mock("@cap/web-backend", async (importOriginal) => {
{
id: "recording",
ownerId: "owner",
- source: { type: "desktopSegments" },
+ source: mocks.source,
},
]),
),
@@ -121,6 +131,8 @@ describe("Instant playlist readiness API", () => {
mocks.audioInitExists = false;
mocks.storageUnavailable = false;
mocks.denied = false;
+ mocks.defaultBucket = false;
+ mocks.source = { type: "desktopSegments" };
});
afterAll(() => mocks.dispose());
@@ -170,4 +182,20 @@ describe("Instant playlist readiness API", () => {
expect((await request()).status).toBe(401);
expect(mocks.read).not.toHaveBeenCalled();
});
+
+ it.each(["custom", "default"] as const)(
+ "redirects a published MP4 in a %s bucket to its retained storage key",
+ async (bucket) => {
+ mocks.defaultBucket = bucket === "default";
+ mocks.source = {
+ type: "desktopMP4",
+ outputKey: "owner/recording/.recording/outputs/generation/result.mp4",
+ };
+ const response = await request("mp4", "");
+ expect(response.status).toBe(302);
+ expect(mocks.sign).toHaveBeenCalledExactlyOnceWith(
+ mocks.source.outputKey,
+ );
+ },
+ );
});
diff --git a/apps/web/__tests__/unit/segments-audio-download.test.ts b/apps/web/__tests__/unit/segments-audio-download.test.ts
new file mode 100644
index 000000000..2cdb62d98
--- /dev/null
+++ b/apps/web/__tests__/unit/segments-audio-download.test.ts
@@ -0,0 +1,45 @@
+import { mkdtemp, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { downloadConcatenatedSegments } from "@/lib/segments-audio-download";
+
+describe("segment audio output errors", () => {
+ let directory: string;
+
+ beforeEach(async () => {
+ directory = await mkdtemp(join(tmpdir(), "cap-segments-output-"));
+ });
+
+ afterEach(async () => {
+ await rm(directory, { recursive: true, force: true });
+ });
+
+ const delayedFetch: typeof fetch = async () => {
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ return new Response("audio segment");
+ };
+
+ it("rejects an output open failure while a segment is downloading", async () => {
+ await expect(
+ downloadConcatenatedSegments(
+ ["https://fixture.invalid/audio.m4s"],
+ join(directory, "missing", "audio.mp4"),
+ { fetchImpl: delayedFetch },
+ ),
+ ).rejects.toMatchObject({ code: "ENOENT" });
+ });
+
+ it.skipIf(process.platform !== "linux")(
+ "rejects a full output device without emitting an uncaught stream error",
+ async () => {
+ await expect(
+ downloadConcatenatedSegments(
+ ["https://fixture.invalid/audio.m4s"],
+ "/dev/full",
+ { fetchImpl: delayedFetch },
+ ),
+ ).rejects.toMatchObject({ code: "ENOSPC" });
+ },
+ );
+});
diff --git a/apps/web/__tests__/unit/self-hosted-screen-recording-page.test.ts b/apps/web/__tests__/unit/self-hosted-screen-recording-page.test.ts
index 455eb33fd..bece788b5 100644
--- a/apps/web/__tests__/unit/self-hosted-screen-recording-page.test.ts
+++ b/apps/web/__tests__/unit/self-hosted-screen-recording-page.test.ts
@@ -53,6 +53,13 @@ describe("SelfHostedScreenRecordingPage component content", () => {
expect(componentSource).toContain("comparisonTable");
});
+ it("describes the license split in the comparison table", () => {
+ expect(componentSource).toContain(
+ '{ text: "Yes — AGPLv3 and MIT crates", status: "positive" }',
+ );
+ expect(componentSource).not.toContain('{ text: "Yes — MIT"');
+ });
+
it("includes recording modes section", () => {
expect(componentSource).toContain("recordingModes");
});
@@ -73,9 +80,10 @@ describe("SelfHostedScreenRecordingPage component content", () => {
expect(componentSource).toContain("migrationGuide");
});
- it("mentions open source and MIT license", () => {
+ it("mentions AGPLv3 and specified MIT crates", () => {
expect(componentSource.toLowerCase()).toContain("open source");
- expect(componentSource.toLowerCase()).toContain("mit");
+ expect(componentSource).toContain("AGPLv3");
+ expect(componentSource).toContain("MIT");
});
it("has badge set", () => {
@@ -117,7 +125,7 @@ describe("SelfHostedScreenRecordingPage FAQ schema", () => {
{
question: "Can I self-host the entire Cap platform, not just storage?",
answer:
- "Yes. Cap is fully open source under the MIT license. You can deploy the complete Cap platform on your own infrastructure.",
+ "Yes. Most Cap code is under AGPLv3, with specified Rust crates under MIT. You can deploy the platform on your own infrastructure.",
},
{
question: "Is self-hosted Cap suitable for HIPAA compliance?",
diff --git a/apps/web/__tests__/unit/sentry-browser-filter.test.ts b/apps/web/__tests__/unit/sentry-browser-filter.test.ts
new file mode 100644
index 000000000..144f1313e
--- /dev/null
+++ b/apps/web/__tests__/unit/sentry-browser-filter.test.ts
@@ -0,0 +1,54 @@
+import { type Client, type Event, eventFiltersIntegration } from "@sentry/core";
+import { afterAll, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({ init: vi.fn() }));
+vi.mock("@sentry/nextjs", () => ({
+ init: mocks.init,
+ captureRouterTransitionStart: vi.fn(),
+}));
+
+vi.stubEnv("NEXT_PUBLIC_SENTRY_DSN", "https://[email protected]/1");
+await import("../../instrumentation-client");
+const options = mocks.init.mock.calls[0]?.[0];
+afterAll(() => vi.unstubAllEnvs());
+
+function filter(filename: string, message = "Failed to connect to MetaMask") {
+ const event: Event = {
+ exception: {
+ values: [
+ {
+ type: "Error",
+ value: message,
+ stacktrace: { frames: [{ filename, lineno: 7 }] },
+ },
+ ],
+ },
+ };
+ return eventFiltersIntegration().processEvent?.(event, {}, {
+ getOptions: () => options,
+ } as Client);
+}
+
+describe("Sentry browser extension filtering", () => {
+ it.each(["chrome", "moz", "safari", "safari-web"])(
+ "drops errors originating in %s extensions",
+ (scheme) => {
+ expect(filter(`${scheme}-extension://extension/scripts/inpage.js`)).toBe(
+ null,
+ );
+ },
+ );
+
+ it.each(["Load failed", "La", "Failed to connect to MetaMask"])(
+ "retains application errors named %s",
+ (message) => {
+ expect(filter("https://cap.so/_next/static/chunk.js", message)).not.toBe(
+ null,
+ );
+ },
+ );
+
+ it("retains unknown sources instead of guessing from the message", () => {
+ expect(filter("app:///assets/js/content.js")).not.toBe(null);
+ });
+});
diff --git a/apps/web/__tests__/unit/share-cap-authorization.test.ts b/apps/web/__tests__/unit/share-cap-authorization.test.ts
new file mode 100644
index 000000000..e37fa9ac9
--- /dev/null
+++ b/apps/web/__tests__/unit/share-cap-authorization.test.ts
@@ -0,0 +1,235 @@
+import {
+ sharedVideos,
+ spaces,
+ spaceVideos,
+ videos,
+} from "@cap/database/schema";
+import { Space, Video } from "@cap/web-domain";
+import { getTableColumns } from "drizzle-orm";
+import type { MySqlTable } from "drizzle-orm/mysql-core";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({ query: vi.fn(), user: vi.fn() }));
+
+vi.mock("@cap/database", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ const client = drizzle(mocks.query);
+ return { db: () => client };
+});
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: mocks.user,
+}));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+
+import { shareCap } from "@/actions/caps/share";
+
+const capId = Video.VideoId.make("video-1");
+const spaceId = Space.SpaceId.make("space-1");
+let privacy: "Private" | "Public";
+let creatorId: string;
+let organizationRole: string | null;
+let spaceRole: string | null;
+let videoOwnerId: string;
+let accessExists: boolean;
+let accessError: boolean;
+let alreadyShared: boolean;
+let organizationShareExists: boolean;
+
+function row(table: MySqlTable, values: Record<string, unknown>) {
+ return Object.values(getTableColumns(table)).map(
+ (column) => values[column.name] ?? null,
+ );
+}
+
+beforeEach(() => {
+ privacy = "Private";
+ creatorId = "space-owner";
+ organizationRole = "member";
+ spaceRole = null;
+ videoOwnerId = "caller";
+ accessExists = true;
+ accessError = false;
+ alreadyShared = false;
+ organizationShareExists = false;
+ mocks.user.mockResolvedValue({ id: "caller" });
+ mocks.query.mockImplementation(async (query: string) => {
+ if (query.startsWith("delete from `shared_videos`")) {
+ organizationShareExists = false;
+ }
+ if (/^(insert|update|delete) /.test(query)) {
+ return { rows: [{ insertId: 0, affectedRows: 1 }] };
+ }
+ if (query.includes("where (false")) return { rows: [] };
+ if (query.includes("from `videos`")) {
+ return { rows: [row(videos, { id: capId, ownerId: videoOwnerId })] };
+ }
+ if (query.includes("from `space_videos`") && alreadyShared) {
+ return {
+ rows: [row(spaceVideos, { id: "share-1", videoId: capId, spaceId })],
+ };
+ }
+ if (query.includes("from `shared_videos`") && organizationShareExists) {
+ return {
+ rows: [
+ row(sharedVideos, {
+ id: "organization-share-1",
+ videoId: capId,
+ organizationId: "org-1",
+ }),
+ ],
+ };
+ }
+ if (query.includes("from `organization_members`")) {
+ return { rows: [["org-1"]] };
+ }
+ if (query.includes("from `spaces`")) {
+ if (query.includes("inner join `organizations`")) {
+ if (accessError) throw new Error("Database unavailable");
+ if (!accessExists) return { rows: [] };
+ return {
+ rows: [
+ [
+ spaceId,
+ "org-1",
+ creatorId,
+ "org-owner",
+ organizationRole,
+ spaceRole,
+ ],
+ ],
+ };
+ }
+ return {
+ rows: [
+ row(spaces, {
+ id: spaceId,
+ organizationId: "org-1",
+ createdById: creatorId,
+ privacy,
+ }),
+ ],
+ };
+ }
+ return { rows: [] };
+ });
+});
+
+const writes = () =>
+ mocks.query.mock.calls.filter(([query]) =>
+ /^(insert|update|delete) /.test(query),
+ );
+
+describe("sharing Caps to private spaces", () => {
+ it("rejects an organization member outside the space before changing any shares or visibility", async () => {
+ organizationShareExists = true;
+ await expect(
+ shareCap({ capId, spaceIds: [spaceId], public: true }),
+ ).resolves.toMatchObject({ success: false });
+ expect(organizationShareExists).toBe(true);
+ expect(writes()).toEqual([]);
+ const accessQuery = mocks.query.mock.calls.find(([query]) =>
+ query.includes("inner join `organizations`"),
+ );
+ expect(accessQuery?.[0]).toContain("`organization_members`.`userId` = ?");
+ expect(accessQuery?.[0]).toContain("`space_members`.`userId` = ?");
+ expect(accessQuery?.[0]).toContain("`spaces`.`id` = ?");
+ expect(accessQuery?.[0]).toContain("`organizations`.`tombstoneAt` is null");
+ expect(accessQuery?.[1]).toEqual(["caller", "caller", spaceId, 1]);
+ });
+
+ it("rejects a missing space access row without writing", async () => {
+ accessExists = false;
+ await expect(shareCap({ capId, spaceIds: [spaceId] })).resolves.toEqual({
+ success: false,
+ error: "Unauthorized",
+ });
+ expect(writes()).toEqual([]);
+ });
+
+ it("does not write when the space access query fails", async () => {
+ accessError = true;
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ await expect(
+ shareCap({ capId, spaceIds: [spaceId] }),
+ ).resolves.toMatchObject({
+ success: false,
+ });
+ expect(writes()).toEqual([]);
+ });
+
+ it.each(["member", "admin"])("allows a space %s", async (role) => {
+ spaceRole = role;
+ await expect(shareCap({ capId, spaceIds: [spaceId] })).resolves.toEqual({
+ success: true,
+ });
+ expect(writes()).toHaveLength(1);
+ expect(writes()[0]?.[0]).toMatch(/^insert into `space_videos`/);
+ expect(writes()[0]?.[1]).toEqual(
+ expect.arrayContaining([capId, spaceId, "caller"]),
+ );
+ });
+
+ it("allows the space creator without a space membership row", async () => {
+ creatorId = "caller";
+ await expect(shareCap({ capId, spaceIds: [spaceId] })).resolves.toEqual({
+ success: true,
+ });
+ expect(writes()).toHaveLength(1);
+ });
+
+ it("allows an organization admin without a space membership row", async () => {
+ organizationRole = "admin";
+ await expect(shareCap({ capId, spaceIds: [spaceId] })).resolves.toEqual({
+ success: true,
+ });
+ expect(writes()).toHaveLength(1);
+ });
+
+ it("preserves sharing to an organization-visible space", async () => {
+ privacy = "Public";
+ await expect(shareCap({ capId, spaceIds: [spaceId] })).resolves.toEqual({
+ success: true,
+ });
+ expect(writes()).toHaveLength(1);
+ });
+
+ it("keeps an existing private space share when access was since removed", async () => {
+ alreadyShared = true;
+ await expect(shareCap({ capId, spaceIds: [spaceId] })).resolves.toEqual({
+ success: true,
+ });
+ expect(writes()).toEqual([]);
+ expect(
+ mocks.query.mock.calls.some(([query]) =>
+ query.includes("inner join `organizations`"),
+ ),
+ ).toBe(false);
+ });
+
+ it("rejects unauthenticated callers without querying the database", async () => {
+ mocks.user.mockResolvedValue(null);
+ await expect(shareCap({ capId, spaceIds: [spaceId] })).resolves.toEqual({
+ success: false,
+ error: "Unauthorized",
+ });
+ expect(mocks.query).not.toHaveBeenCalled();
+ });
+
+ it("still rejects another user's video", async () => {
+ videoOwnerId = "another-user";
+ spaceRole = "admin";
+ await expect(shareCap({ capId, spaceIds: [spaceId] })).resolves.toEqual({
+ success: false,
+ error: "Unauthorized",
+ });
+ expect(writes()).toEqual([]);
+ expect(mocks.query).toHaveBeenCalledTimes(1);
+ });
+
+ it("allows clearing sharing without a destination space", async () => {
+ await expect(shareCap({ capId, spaceIds: [] })).resolves.toEqual({
+ success: true,
+ });
+ expect(writes()).toEqual([]);
+ });
+});
diff --git a/apps/web/__tests__/unit/share-playback.test.ts b/apps/web/__tests__/unit/share-playback.test.ts
index 1b6c9e87d..900b503f8 100644
--- a/apps/web/__tests__/unit/share-playback.test.ts
+++ b/apps/web/__tests__/unit/share-playback.test.ts
@@ -79,6 +79,31 @@ describe("share page playback URL", () => {
source: publishedVideo.source,
storageIntegrationId: Option.some("drive-integration"),
});
+ expect(mocks.sign).toHaveBeenCalledExactlyOnceWith(
+ publishedVideo.source.outputKey,
+ );
+ });
+
+ it("signs the audio leveled output when it is the published source", async () => {
+ mocks.sign.mockReturnValue(
+ Effect.succeed("https://media.example.com/audio.mp4"),
+ );
+ mocks.access.mockReturnValue(
+ Effect.succeed([{ getSignedObjectUrl: mocks.sign }, Option.none()]),
+ );
+ const outputKey = "owner/video/.recording/outputs/generation/result.mp4";
+ const audioLevelOutputKey =
+ "owner/video/.recording/outputs/audio-quality-v3/result.mp4";
+ await getSharePlaybackUrl({
+ ...video,
+ source: {
+ type: "desktopMP4",
+ outputKey,
+ audioLevelSourceKey: outputKey,
+ audioLevelOutputKey,
+ },
+ });
+ expect(mocks.sign).toHaveBeenCalledExactlyOnceWith(audioLevelOutputKey);
});
it("allows the player to use its existing route when storage resolution fails", async () => {
diff --git a/apps/web/__tests__/unit/share-stage-identity.test.ts b/apps/web/__tests__/unit/share-stage-identity.test.ts
index 9bb74ad1e..d0eaa4e13 100644
--- a/apps/web/__tests__/unit/share-stage-identity.test.ts
+++ b/apps/web/__tests__/unit/share-stage-identity.test.ts
@@ -65,7 +65,7 @@ vi.mock("next/image", async () => {
});
vi.mock("next/navigation", () => ({
- useSearchParams: () => new URLSearchParams(),
+ useSearchParams: () => new URLSearchParams(window.location.search),
useRouter: () => ({ refresh: () => undefined }),
}));
@@ -163,12 +163,13 @@ const findButton = (container: HTMLElement, label: string) =>
const stageOf = (video: Element | null) =>
video?.parentElement?.parentElement?.parentElement ?? null;
-describe("Share view toggle", () => {
+describe("Share", () => {
beforeAll(() => {
actEnvironment.IS_REACT_ACT_ENVIRONMENT = true;
});
afterEach(() => {
+ vi.useRealTimers();
document.body.replaceChildren();
window.localStorage.clear();
window.history.replaceState(window.history.state, "", "/s/video-id");
@@ -178,6 +179,41 @@ describe("Share view toggle", () => {
delete actEnvironment.IS_REACT_ACT_ENVIRONMENT;
});
+ it.each(["loadedmetadata", "canplay"])(
+ "seeks a timestamp link when %s arrives after five seconds",
+ async (readyEvent) => {
+ vi.useFakeTimers();
+ window.history.replaceState(null, "", "/s/video-id?t=42");
+ const container = document.createElement("div");
+ document.body.append(container);
+ const root = createRoot(container);
+
+ try {
+ await act(async () => {
+ root.render(createElement(Share, createProps()));
+ });
+
+ const video = container.querySelector("video");
+ if (!video) throw new Error("Share video did not mount");
+ expect(video.currentTime).toBe(0);
+
+ await act(async () => {
+ vi.advanceTimersByTime(5000);
+ Object.defineProperty(video, "duration", { value: 60 });
+ video.dispatchEvent(new Event(readyEvent));
+ });
+
+ expect(video.currentTime).toBe(42);
+ video.currentTime = 50;
+ video.dispatchEvent(new Event("loadedmetadata"));
+ video.dispatchEvent(new Event("canplay"));
+ expect(video.currentTime).toBe(50);
+ } finally {
+ await act(async () => root.unmount());
+ }
+ },
+ );
+
it("keeps the sidebar open after mounting with an old collapsed preference", async () => {
window.localStorage.setItem("cap_share_rail_collapsed", "true");
const container = document.createElement("div");
diff --git a/apps/web/__tests__/unit/share-video-metadata.test.ts b/apps/web/__tests__/unit/share-video-metadata.test.ts
index 376c57cd6..2af7bb6af 100644
--- a/apps/web/__tests__/unit/share-video-metadata.test.ts
+++ b/apps/web/__tests__/unit/share-video-metadata.test.ts
@@ -48,6 +48,8 @@ describe("share video metadata", () => {
name: "Product demo",
sourceType: "desktopMP4",
webUrl: "https://cap.so",
+ width: null,
+ height: null,
advertiseIframelyPlayer: true,
});
@@ -84,7 +86,7 @@ describe("share video metadata", () => {
rel: "iframely player",
url: "https://cap.so/embed/video123",
type: "text/html",
- media: "(aspect-ratio: 16/9)",
+ media: "(aspect-ratio: 1280/720)",
},
],
});
@@ -99,11 +101,66 @@ describe("share video metadata", () => {
name: "Product demo",
sourceType: "desktopMP4",
webUrl: "https://cap.so",
+ width: null,
+ height: null,
});
expect(metadata.icons).toBeUndefined();
});
+ it.each([
+ [1080, 1920],
+ [1920, 1080],
+ [1440, 1080],
+ ])(
+ "preserves %ix%i recording dimensions in link previews",
+ (width, height) => {
+ const metadata = buildShareVideoMetadata({
+ videoId: "video123",
+ name: "Product demo",
+ sourceType: "desktopMP4",
+ webUrl: "https://cap.so",
+ width,
+ height,
+ advertiseIframelyPlayer: true,
+ });
+
+ expect(metadata.openGraph).toMatchObject({
+ videos: [{ width, height }],
+ });
+ expect(metadata.twitter).toMatchObject({
+ players: { width, height },
+ });
+ expect(metadata.icons).toMatchObject({
+ other: [{ media: `(aspect-ratio: ${width}/${height})` }],
+ });
+ },
+ );
+
+ it.each([
+ [null, null],
+ [1080, null],
+ [null, 1920],
+ [0, 1920],
+ [1080, 0],
+ ])("uses default dimensions when %s x %s is incomplete", (width, height) => {
+ const metadata = buildShareVideoMetadata({
+ videoId: "video123",
+ name: "Product demo",
+ sourceType: "desktopMP4",
+ webUrl: "https://cap.so",
+ width,
+ height,
+ });
+
+ expect(metadata.openGraph).toMatchObject({
+ videos: [{ width: 1280, height: 720 }],
+ });
+ expect(metadata.twitter).toMatchObject({
+ players: { width: 1280, height: 720 },
+ });
+ });
+
it.each([
["MediaConvert", "master"],
["local", "master"],
diff --git a/apps/web/__tests__/unit/storage-object-response-headers.test.ts b/apps/web/__tests__/unit/storage-object-response-headers.test.ts
new file mode 100644
index 000000000..315b17069
--- /dev/null
+++ b/apps/web/__tests__/unit/storage-object-response-headers.test.ts
@@ -0,0 +1,206 @@
+import { Effect } from "effect";
+import { NextRequest } from "next/server";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ head: vi.fn(),
+ read: vi.fn(),
+ signedUrl: null as string | null,
+ video: {
+ id: "video",
+ ownerId: "owner",
+ source: { type: "desktopMP4" },
+ },
+}));
+
+vi.mock("@cap/web-backend", async () => {
+ const { Effect, Option } = await import("effect");
+ return {
+ provideOptionalAuth: (effect: unknown) => effect,
+ Storage: {
+ getAccessForVideo: () =>
+ Effect.succeed([
+ mocks.signedUrl
+ ? { getSignedObjectUrl: () => Effect.succeed(mocks.signedUrl) }
+ : { headObject: mocks.head, getObjectResponse: mocks.read },
+ ]),
+ },
+ Videos: Effect.succeed({
+ getByIdForViewing: () => Effect.succeed(Option.some([mocks.video])),
+ }),
+ VideosRepo: Effect.succeed({
+ getById: () => Effect.succeed(Option.some([mocks.video])),
+ }),
+ verifyStorageObjectToken: () => null,
+ };
+});
+vi.mock("@cap/env", () => ({
+ serverEnv: () => ({ MEDIA_SERVER_WEBHOOK_SECRET: "test-media-secret" }),
+}));
+vi.mock("@/lib/server", async () => {
+ const { Effect } = await import("effect");
+ return { runPromise: Effect.runPromise };
+});
+vi.mock("@/utils/helpers", () => ({ CACHE_CONTROL_HEADERS: {} }));
+
+import { GET, HEAD } from "@/app/api/storage/object/route";
+
+const objectUrl = (key: string) =>
+ `https://cap.test/api/storage/object?videoId=video&key=${encodeURIComponent(key)}`;
+
+const upstream = (contentType: string | null, init: ResponseInit = {}) => {
+ const headers = new Headers(init.headers);
+ const response = new Response("data", {
+ status: init.status ?? 200,
+ headers,
+ });
+ if (contentType) {
+ response.headers.set("Content-Type", contentType);
+ } else {
+ response.headers.delete("Content-Type");
+ }
+ return response;
+};
+
+const expectContentHeaders = (response: Response) => {
+ expect(response.headers.get("X-Content-Type-Options")).toBe("nosniff");
+ expect(response.headers.get("Content-Security-Policy")).toBe(
+ "sandbox; default-src 'none'",
+ );
+};
+
+describe("storage object content headers", () => {
+ beforeEach(() => {
+ mocks.head.mockReset();
+ mocks.read.mockReset();
+ mocks.signedUrl = null;
+ });
+
+ it.each([
+ ["owner/video/result.mp4", "video/mp4"],
+ ["owner/video/raw-upload.webm", "audio/webm"],
+ ["owner/video/raw-upload.mov", "video/quicktime"],
+ ["owner/video/segments/video/segment_001.m4s", "video/mp4"],
+ ["owner/video/combined-source/segment_1.ts", "video/mp2t"],
+ ["owner/video/stream.m3u8", "application/x-mpegURL"],
+ ["owner/video/screenshot/screen-capture.jpg", "image/jpeg"],
+ ["owner/video/screenshot/screen-capture.png", "image/png"],
+ ["owner/video/animated-preview.gif", "image/gif"],
+ ["owner/video/transcription.vtt", "text/vtt; charset=utf-8"],
+ ["owner/video/comments/comment/media.m4a", "audio/mp4"],
+ ["owner/video/comments/comment/media.mp3", "audio/mpeg"],
+ ])("serves %s inline as %s", async (key, contentType) => {
+ mocks.read.mockReturnValue(Effect.succeed(upstream(contentType)));
+ const response = await GET(new NextRequest(objectUrl(key)));
+ expect(response.status).toBe(200);
+ expect(response.headers.get("Content-Type")).toBe(contentType);
+ expect(response.headers.get("Content-Disposition")).toBeNull();
+ expectContentHeaders(response);
+ expect(await response.text()).toBe("data");
+ });
+
+ it.each([
+ "text/html",
+ "text/html; charset=utf-8",
+ "image/svg+xml",
+ "application/xhtml+xml",
+ "application/pdf",
+ "text/plain",
+ "application/javascript",
+ ])("serves %s as an attachment", async (contentType) => {
+ mocks.read.mockReturnValue(Effect.succeed(upstream(contentType)));
+ const response = await GET(
+ new NextRequest(objectUrl("owner/video/upload.bin")),
+ );
+ expect(response.status).toBe(200);
+ expect(response.headers.get("Content-Type")).toBe(
+ "application/octet-stream",
+ );
+ expect(response.headers.get("Content-Disposition")).toBe("attachment");
+ expectContentHeaders(response);
+ });
+
+ it("serves an upstream without a Content-Type as an attachment", async () => {
+ const source = upstream(null);
+ expect(source.headers.get("Content-Type")).toBeNull();
+ mocks.read.mockReturnValue(Effect.succeed(source));
+ const response = await GET(
+ new NextRequest(objectUrl("owner/video/upload.bin")),
+ );
+ expect(response.status).toBe(200);
+ expect(response.headers.get("Content-Type")).toBe(
+ "application/octet-stream",
+ );
+ expect(response.headers.get("Content-Disposition")).toBe("attachment");
+ expectContentHeaders(response);
+ });
+
+ it("keeps byte-range responses intact", async () => {
+ mocks.read.mockReturnValue(
+ Effect.succeed(
+ upstream("video/mp4", {
+ status: 206,
+ headers: {
+ "Content-Range": "bytes 0-3/100",
+ "Content-Length": "4",
+ },
+ }),
+ ),
+ );
+ const response = await GET(
+ new NextRequest(objectUrl("owner/video/result.mp4"), {
+ headers: { Range: "bytes=0-3" },
+ }),
+ );
+ expect(mocks.read).toHaveBeenCalledWith(
+ "owner/video/result.mp4",
+ "bytes=0-3",
+ { signal: expect.any(AbortSignal) },
+ );
+ expect(response.status).toBe(206);
+ expect(response.headers.get("Content-Range")).toBe("bytes 0-3/100");
+ expect(response.headers.get("Content-Length")).toBe("4");
+ expect(response.headers.get("Accept-Ranges")).toBe("bytes");
+ expect(response.headers.get("Content-Type")).toBe("video/mp4");
+ expectContentHeaders(response);
+ });
+
+ it("applies the same content headers to HEAD responses", async () => {
+ mocks.head.mockReturnValueOnce(
+ Effect.succeed({ ContentLength: 100, ContentType: "video/mp4" }),
+ );
+ const inline = await HEAD(
+ new NextRequest(objectUrl("owner/video/result.mp4"), {
+ method: "HEAD",
+ }),
+ );
+ expect(inline.headers.get("Content-Type")).toBe("video/mp4");
+ expect(inline.headers.get("Content-Disposition")).toBeNull();
+ expectContentHeaders(inline);
+
+ mocks.head.mockReturnValueOnce(
+ Effect.succeed({ ContentLength: 100, ContentType: "text/html" }),
+ );
+ const attachment = await HEAD(
+ new NextRequest(objectUrl("owner/video/page.html"), {
+ method: "HEAD",
+ }),
+ );
+ expect(attachment.headers.get("Content-Type")).toBe(
+ "application/octet-stream",
+ );
+ expect(attachment.headers.get("Content-Disposition")).toBe("attachment");
+ expectContentHeaders(attachment);
+ expect(mocks.read).not.toHaveBeenCalled();
+ });
+
+ it("keeps redirecting to signed URLs without proxying bytes", async () => {
+ mocks.signedUrl = "https://bucket.example/owner/video/result.mp4?sig=1";
+ const response = await GET(
+ new NextRequest(objectUrl("owner/video/result.mp4")),
+ );
+ expect(response.status).toBe(302);
+ expect(response.headers.get("Location")).toBe(mocks.signedUrl);
+ expect(mocks.read).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/subscribe-checkout.test.ts b/apps/web/__tests__/unit/subscribe-checkout.test.ts
new file mode 100644
index 000000000..afef86e61
--- /dev/null
+++ b/apps/web/__tests__/unit/subscribe-checkout.test.ts
@@ -0,0 +1,194 @@
+import { isValidStripePlanPriceId, STRIPE_PLAN_IDS } from "@cap/utils";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { POST as subscribe } from "@/app/api/settings/billing/subscribe/route";
+
+const checkoutMocks = vi.hoisted(() => ({
+ create: vi.fn(),
+ track: vi.fn(() => Promise.resolve()),
+ getCurrentUser: vi.fn(),
+ dbUpdate: vi.fn(),
+}));
+
+vi.mock("@cap/env", () => ({
+ buildEnv: { NEXT_PUBLIC_IS_CAP: "true" },
+ serverEnv: () => ({ WEB_URL: "https://cap.so" }),
+}));
+
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: checkoutMocks.getCurrentUser,
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ update: () => ({
+ set: () => ({
+ where: checkoutMocks.dbUpdate,
+ }),
+ }),
+ }),
+}));
+
+vi.mock("@cap/database/schema", () => ({
+ users: { id: "id" },
+}));
+
+vi.mock("@cap/utils", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("@cap/utils")>();
+ return {
+ ...actual,
+ stripe: () => ({
+ customers: {
+ list: vi.fn().mockResolvedValue({ data: [] }),
+ create: vi.fn().mockResolvedValue({ id: "cus_new" }),
+ update: vi.fn().mockResolvedValue({ id: "cus_new" }),
+ },
+ checkout: {
+ sessions: { create: checkoutMocks.create },
+ },
+ }),
+ userIsPro: vi.fn().mockReturnValue(false),
+ };
+});
+
+vi.mock("@/lib/server-analytics", () => ({
+ trackServerEvent: checkoutMocks.track,
+}));
+
+const makeSubscribeRequest = (body: Record<string, unknown>) =>
+ new Request("https://cap.so/api/settings/billing/subscribe", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify(body),
+ }) as unknown as import("next/server").NextRequest;
+
+describe("Stripe plan allowlist", () => {
+ it("rejects IDs that are not a Pro plan price in either environment", () => {
+ for (const environment of ["development", "production"] as const) {
+ expect(
+ isValidStripePlanPriceId("price_arbitrary_attacker_id", environment),
+ ).toBe(false);
+ expect(isValidStripePlanPriceId("price_free_tier", environment)).toBe(
+ false,
+ );
+ expect(isValidStripePlanPriceId("", environment)).toBe(false);
+ }
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.development.monthly,
+ "development",
+ ),
+ ).toBe(true);
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.production.monthly,
+ "production",
+ ),
+ ).toBe(true);
+ });
+
+ it("validates price IDs against specific deployment environments", () => {
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.development.yearly,
+ "development",
+ ),
+ ).toBe(true);
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.production.yearly,
+ "development",
+ ),
+ ).toBe(false);
+
+ expect(
+ isValidStripePlanPriceId(STRIPE_PLAN_IDS.production.yearly, "production"),
+ ).toBe(true);
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.development.yearly,
+ "production",
+ ),
+ ).toBe(false);
+ });
+});
+
+describe("POST /api/settings/billing/subscribe", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ checkoutMocks.getCurrentUser.mockResolvedValue({
+ id: "user_123",
+ email: "[email protected]",
+ stripeCustomerId: "cus_123",
+ });
+ checkoutMocks.create.mockResolvedValue({
+ id: "cs_test",
+ url: "https://pay.cap.so/session",
+ });
+ });
+
+ it("rejects arbitrary price IDs with 400", async () => {
+ const response = await subscribe(
+ makeSubscribeRequest({
+ priceId: "price_arbitrary_malicious",
+ quantity: 1,
+ }),
+ );
+
+ expect(response.status).toBe(400);
+ expect(await response.json()).toEqual({
+ error: true,
+ message: "Invalid priceId",
+ });
+ expect(checkoutMocks.create).not.toHaveBeenCalled();
+ });
+
+ it("accepts valid Pro plan price ID and creates checkout session", async () => {
+ const response = await subscribe(
+ makeSubscribeRequest({
+ priceId: STRIPE_PLAN_IDS.development.monthly,
+ quantity: 2,
+ }),
+ );
+
+ expect(response.status).toBe(200);
+ expect(checkoutMocks.create).toHaveBeenCalledWith(
+ expect.objectContaining({
+ customer: "cus_123",
+ line_items: [
+ { price: STRIPE_PLAN_IDS.development.monthly, quantity: 2 },
+ ],
+ mode: "subscription",
+ }),
+ );
+ });
+
+ it.each([0, -5, 1.5, "3", 10001])(
+ "rejects quantity %s with 400 before creating a session",
+ async (quantity) => {
+ const response = await subscribe(
+ makeSubscribeRequest({
+ priceId: STRIPE_PLAN_IDS.development.monthly,
+ quantity,
+ }),
+ );
+
+ expect(response.status).toBe(400);
+ expect(checkoutMocks.create).not.toHaveBeenCalled();
+ },
+ );
+
+ it("defaults a missing quantity to one seat", async () => {
+ const response = await subscribe(
+ makeSubscribeRequest({ priceId: STRIPE_PLAN_IDS.development.monthly }),
+ );
+
+ expect(response.status).toBe(200);
+ expect(checkoutMocks.create).toHaveBeenCalledWith(
+ expect.objectContaining({
+ line_items: [
+ { price: STRIPE_PLAN_IDS.development.monthly, quantity: 1 },
+ ],
+ }),
+ );
+ });
+});
diff --git a/apps/web/__tests__/unit/toolbar-reactions.test.ts b/apps/web/__tests__/unit/toolbar-reactions.test.ts
new file mode 100644
index 000000000..c76ca2ba6
--- /dev/null
+++ b/apps/web/__tests__/unit/toolbar-reactions.test.ts
@@ -0,0 +1,83 @@
+import { describe, expect, it, vi } from "vitest";
+import {
+ handleEmojiReaction,
+ REACTIONS,
+} from "@/app/s/[videoId]/_components/toolbar-reactions";
+
+describe("toolbar reactions", () => {
+ it("exposes the standard reaction set in display order", () => {
+ expect(REACTIONS).toEqual([
+ { emoji: "😂", label: "joy" },
+ { emoji: "😍", label: "love" },
+ { emoji: "😮", label: "wow" },
+ { emoji: "🙌", label: "yay" },
+ { emoji: "👍", label: "up" },
+ { emoji: "👎", label: "down" },
+ ]);
+ });
+
+ it("prompts sign-in overlay when unauthenticated viewer reacts", () => {
+ const onRequireAuth = vi.fn();
+ const onReact = vi.fn();
+
+ handleEmojiReaction({
+ emoji: "😂",
+ canReact: true,
+ user: null,
+ onRequireAuth,
+ onReact,
+ });
+
+ expect(onRequireAuth).toHaveBeenCalledOnce();
+ expect(onReact).not.toHaveBeenCalled();
+ });
+
+ it("prompts sign-in overlay when user is undefined", () => {
+ const onRequireAuth = vi.fn();
+ const onReact = vi.fn();
+
+ handleEmojiReaction({
+ emoji: "👍",
+ canReact: true,
+ user: undefined,
+ onRequireAuth,
+ onReact,
+ });
+
+ expect(onRequireAuth).toHaveBeenCalledOnce();
+ expect(onReact).not.toHaveBeenCalled();
+ });
+
+ it("suppresses interaction when reactions are disabled", () => {
+ const onRequireAuth = vi.fn();
+ const onReact = vi.fn();
+
+ handleEmojiReaction({
+ emoji: "😍",
+ canReact: false,
+ user: null,
+ onRequireAuth,
+ onReact,
+ });
+
+ expect(onRequireAuth).not.toHaveBeenCalled();
+ expect(onReact).not.toHaveBeenCalled();
+ });
+
+ it("submits reaction when viewer is signed in", () => {
+ const onRequireAuth = vi.fn();
+ const onReact = vi.fn();
+
+ handleEmojiReaction({
+ emoji: "🙌",
+ canReact: true,
+ user: { id: "user-123" },
+ onRequireAuth,
+ onReact,
+ });
+
+ expect(onRequireAuth).not.toHaveBeenCalled();
+ expect(onReact).toHaveBeenCalledOnce();
+ expect(onReact).toHaveBeenCalledWith("🙌", { id: "user-123" });
+ });
+});
diff --git a/apps/web/__tests__/unit/transcript-numbers.test.ts b/apps/web/__tests__/unit/transcript-numbers.test.ts
new file mode 100644
index 000000000..ec8e953f4
--- /dev/null
+++ b/apps/web/__tests__/unit/transcript-numbers.test.ts
@@ -0,0 +1,63 @@
+import { describe, expect, it } from "vitest";
+import { parseTranscriptVTT } from "@/app/s/[videoId]/_components/utils/parse-transcript-vtt";
+import {
+ formatTranscriptAsVTT,
+ parseVTT,
+} from "@/app/s/[videoId]/_components/utils/transcript-utils";
+import { formatToWebVTT } from "@/lib/transcribe-utils";
+import { updateVttEntryText } from "@/lib/transcript-vtt";
+
+describe.each([
+ { name: "embed", parse: parseVTT },
+ { name: "transcript", parse: parseTranscriptVTT },
+])("$name numeric captions", ({ parse }) => {
+ it("preserves numbers separated into their own caption by a pause", () => {
+ const vtt = formatToWebVTT({
+ words: [
+ { text: "The", start: 0, end: 100 },
+ { text: "year", start: 150, end: 300 },
+ { text: "is", start: 350, end: 500 },
+ { text: "2026", start: 1500, end: 1900 },
+ { text: "Next.", start: 3000, end: 3500 },
+ ],
+ });
+
+ expect(parse(vtt).map(({ id, text }) => ({ id, text }))).toEqual([
+ { id: 1, text: "The year is" },
+ { id: 2, text: "2026" },
+ { id: 3, text: "Next." },
+ ]);
+ expect(formatTranscriptAsVTT(parse(vtt))).toContain("\n2026\n");
+ });
+
+ it.each(["0", "007"])("retains a saved correction to %s", (text) => {
+ const original = formatToWebVTT({
+ words: [{ text: "Zero.", start: 0, end: 1000 }],
+ });
+ const saved = updateVttEntryText(original, 1, text);
+
+ expect(saved.updated).toBe(true);
+ expect(parse(saved.content)).toMatchObject([
+ { id: 1, text, timestamp: "00:00", startTime: 0 },
+ ]);
+ });
+
+ it("keeps the next caption's ID when an earlier cue has no text", () => {
+ const vtt =
+ "WEBVTT\n\n1\n00:00:00.000 --> 00:00:01.000\n\n2\n00:00:01.000 --> 00:00:02.000\nHello\n";
+
+ expect(parse(vtt)).toMatchObject([
+ { id: 2, timestamp: "00:01", startTime: 1, text: "Hello" },
+ ]);
+ });
+});
+
+it("preserves fractional seek and end times when reloading a saved numeric caption", () => {
+ const original = "WEBVTT\n\n1\n00:00:01.500 --> 00:00:02.750\nBond\n";
+ const saved = updateVttEntryText(original, 1, "007");
+
+ expect(saved.updated).toBe(true);
+ expect(parseTranscriptVTT(saved.content)).toEqual([
+ { id: 1, text: "007", timestamp: "00:01", startTime: 1.5, endTime: 2.75 },
+ ]);
+});
diff --git a/apps/web/__tests__/unit/transcript-sidebar-search.test.ts b/apps/web/__tests__/unit/transcript-sidebar-search.test.ts
new file mode 100644
index 000000000..5443f5b8a
--- /dev/null
+++ b/apps/web/__tests__/unit/transcript-sidebar-search.test.ts
@@ -0,0 +1,198 @@
+// @vitest-environment jsdom
+
+import type { Video } from "@cap/web-domain";
+import { act, createElement, type RefObject } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ getEditTranscript: vi.fn(),
+ scrollToIndex: vi.fn(),
+}));
+
+vi.mock("@/actions/videos/get-edit-transcript", () => ({
+ getEditTranscript: mocks.getEditTranscript,
+ requestEditTranscript: vi.fn(),
+}));
+
+vi.mock("@/app/s/[videoId]/edit/use-active-transcript-word-index", () => ({
+ useActiveTranscriptWordIndex: () => -1,
+}));
+
+vi.mock("@virtual-grid/react", () => ({
+ useVirtualizer: ({ count }: { count: number }) => ({
+ getTotalSize: () => count * 100,
+ getVirtualItems: () =>
+ Array.from({ length: count }, (_, index) => ({
+ index,
+ key: index,
+ start: index * 100,
+ })),
+ measureElement: vi.fn(),
+ scrollToIndex: mocks.scrollToIndex,
+ }),
+}));
+
+import { TranscriptSidebar } from "@/app/s/[videoId]/edit/TranscriptSidebar";
+
+const videoId = "video-id" as Video.VideoId;
+const transcript = {
+ version: 3 as const,
+ speechModelUsed: "fixture",
+ durationMs: 5_000,
+ languageCode: "en",
+ words: [
+ {
+ id: "word-0",
+ text: "Match",
+ startMs: 1_000,
+ endMs: 1_400,
+ confidence: 1,
+ speaker: null,
+ channel: null,
+ },
+ {
+ id: "word-1",
+ text: "between",
+ startMs: 2_000,
+ endMs: 2_400,
+ confidence: 1,
+ speaker: null,
+ channel: null,
+ },
+ {
+ id: "word-2",
+ text: "matching",
+ startMs: 3_000,
+ endMs: 3_400,
+ confidence: 1,
+ speaker: null,
+ channel: null,
+ },
+ ],
+};
+
+let container: HTMLDivElement;
+let root: Root;
+let video: HTMLVideoElement;
+
+async function waitFor(assertion: () => void) {
+ let error: unknown;
+ for (let attempt = 0; attempt < 50; attempt++) {
+ try {
+ assertion();
+ return;
+ } catch (cause) {
+ error = cause;
+ }
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ });
+ }
+ throw error;
+}
+
+async function enterSearch(query: string, expectedCounter = "1/2") {
+ const input = container.querySelector<HTMLInputElement>(
+ 'input[placeholder="Search transcript"]',
+ );
+ if (!input) throw new Error("Search input missing");
+
+ await act(async () => {
+ const setter = Object.getOwnPropertyDescriptor(
+ HTMLInputElement.prototype,
+ "value",
+ )?.set;
+ setter?.call(input, query);
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+ await waitFor(() => expect(container.textContent).toContain(expectedCounter));
+ return input;
+}
+
+async function pressEnter(input: HTMLInputElement) {
+ await act(async () => {
+ input.dispatchEvent(
+ new KeyboardEvent("keydown", { bubbles: true, key: "Enter" }),
+ );
+ });
+}
+
+beforeEach(async () => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
+ callback(0);
+ return 1;
+ });
+ HTMLElement.prototype.scrollIntoView = vi.fn();
+ mocks.getEditTranscript.mockResolvedValue({ status: "ready", transcript });
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+ video = document.createElement("video");
+ const videoRef: RefObject<HTMLVideoElement | null> = { current: video };
+
+ await act(async () => {
+ root.render(
+ createElement(TranscriptSidebar, {
+ videoId,
+ videoRef,
+ keepRanges: [{ start: 0, end: 5 }],
+ onDeleteRanges: vi.fn(),
+ }),
+ );
+ });
+ await waitFor(() =>
+ expect(
+ container.querySelector('input[placeholder="Search transcript"]'),
+ ).not.toBeNull(),
+ );
+});
+
+afterEach(async () => {
+ await act(async () => root.unmount());
+ container.remove();
+ vi.unstubAllGlobals();
+ vi.clearAllMocks();
+});
+
+describe("transcript search navigation", () => {
+ it("seeks to the first match before advancing through later matches", async () => {
+ const input = await enterSearch("match");
+
+ await pressEnter(input);
+ expect(video.currentTime).toBe(1);
+ expect(container.textContent).toContain("1/2");
+
+ await pressEnter(input);
+ expect(video.currentTime).toBe(3);
+ expect(container.textContent).toContain("2/2");
+
+ await pressEnter(input);
+ expect(video.currentTime).toBe(1);
+ expect(container.textContent).toContain("1/2");
+ });
+
+ it("seeks to the last match when Previous is pressed first", async () => {
+ await enterSearch("match");
+ const previous = container.querySelector<HTMLButtonElement>(
+ 'button[aria-label="Previous search result"]',
+ );
+ if (!previous) throw new Error("Previous search button missing");
+
+ await act(async () => previous.click());
+ expect(video.currentTime).toBe(3);
+ expect(container.textContent).toContain("2/2");
+ });
+
+ it("seeks to the new first match after changing the query", async () => {
+ const input = await enterSearch("between", "1/1");
+ await pressEnter(input);
+ expect(video.currentTime).toBe(2);
+
+ await enterSearch("match");
+ await pressEnter(input);
+ expect(video.currentTime).toBe(1);
+ expect(container.textContent).toContain("1/2");
+ });
+});
diff --git a/apps/web/__tests__/unit/translation-vtt.test.ts b/apps/web/__tests__/unit/translation-vtt.test.ts
new file mode 100644
index 000000000..dc366e92a
--- /dev/null
+++ b/apps/web/__tests__/unit/translation-vtt.test.ts
@@ -0,0 +1,90 @@
+import { describe, expect, it } from "vitest";
+import {
+ isCompleteTranslation,
+ joinTranslationChunks,
+ readCachedTranslation,
+ splitTranslationChunks,
+ withSourceFingerprint,
+} from "@/actions/videos/translation-vtt";
+
+const source = `WEBVTT
+
+1
+00:00:00.000 --> 00:00:02.000
+Hello there
+
+2
+00:00:02.000 --> 00:00:04.000
+How are you?
+
+3
+00:00:04.000 --> 00:00:06.000
+See you soon`;
+
+describe("translation VTT completeness", () => {
+ it("rejects a translated file that stops before the original", () => {
+ const truncated = source.replace(/\n\n3\n[\s\S]*$/, "");
+ expect(isCompleteTranslation(source, truncated)).toBe(false);
+ });
+
+ it("rejects changed cue timing and empty cue text", () => {
+ expect(
+ isCompleteTranslation(
+ source,
+ source.replace("00:00:04.000", "00:00:04.100"),
+ ),
+ ).toBe(false);
+ expect(
+ isCompleteTranslation(source, source.replace("How are you?", "")),
+ ).toBe(false);
+ expect(
+ isCompleteTranslation(source, source.replace("\n2\n", "\n9\n")),
+ ).toBe(false);
+ });
+
+ it("joins long translations without dropping the final cue", () => {
+ const timestamp = (seconds: number) =>
+ `00:${String(Math.floor(seconds / 60)).padStart(2, "0")}:${String(seconds % 60).padStart(2, "0")}.000`;
+ const cues = Array.from({ length: 180 }, (_, index) => {
+ return `${index + 1}\n${timestamp(index)} --> ${timestamp(index + 1)}\nCaption ${index + 1} with enough text to require multiple chunks`;
+ });
+ const longSource = `WEBVTT\n\n${cues.join("\n\n")}`;
+ const chunks = splitTranslationChunks(longSource);
+ expect(chunks?.length).toBeGreaterThan(1);
+ if (!chunks) throw new Error("Expected chunks");
+
+ const translations = chunks.map((chunk) =>
+ chunk.replaceAll("Caption", "Translated caption"),
+ );
+ const result = joinTranslationChunks(longSource, chunks, translations);
+ expect(result).not.toBeNull();
+ expect(isCompleteTranslation(longSource, result ?? "")).toBe(true);
+ expect(result).toContain("Translated caption 180");
+ });
+
+ it("rejects an incomplete chunk without returning a partial file", () => {
+ const chunks = splitTranslationChunks(source);
+ if (!chunks) throw new Error("Expected chunks");
+ expect(joinTranslationChunks(source, chunks, ["WEBVTT"])).toBeNull();
+ });
+
+ it("translates a header-only transcript without any chunks", () => {
+ const empty = "WEBVTT\n\n";
+ const chunks = splitTranslationChunks(empty);
+ expect(chunks).toEqual([]);
+ expect(joinTranslationChunks(empty, [], [])).toBe("WEBVTT");
+ expect(isCompleteTranslation(empty, "WEBVTT")).toBe(true);
+ });
+
+ it("reuses a cached translation only for the transcript it was made from", () => {
+ const translated = source.replaceAll("Hello there", "Hola");
+ const cached = withSourceFingerprint(source, translated);
+ if (!cached) throw new Error("Expected cache body");
+
+ expect(readCachedTranslation(source, cached)).toBe(translated);
+ expect(
+ readCachedTranslation(source.replace("See you soon", "Bye"), cached),
+ ).toBeNull();
+ expect(readCachedTranslation(source, translated)).toBeNull();
+ });
+});
diff --git a/apps/web/__tests__/unit/verification-token.test.ts b/apps/web/__tests__/unit/verification-token.test.ts
new file mode 100644
index 000000000..ea7c96357
--- /dev/null
+++ b/apps/web/__tests__/unit/verification-token.test.ts
@@ -0,0 +1,146 @@
+import type { SQL } from "drizzle-orm";
+import { MySqlDialect } from "drizzle-orm/mysql-core";
+import type { MySql2Database } from "drizzle-orm/mysql2";
+import { describe, expect, it } from "vitest";
+import { DrizzleAdapter } from "../../../../packages/database/auth/drizzle-adapter";
+
+type TokenRow = { identifier: string; token: string; expires: Date };
+
+function matchesPredicate(condition: SQL, row: TokenRow) {
+ const query = new MySqlDialect().sqlToQuery(condition);
+ const columns = [
+ ...query.sql.matchAll(
+ /`verification_tokens`\.`(identifier|token)`\s*=\s*\?/g,
+ ),
+ ].map((match) => match[1] as "identifier" | "token");
+ if (columns.length === 0) {
+ throw new Error(`Unexpected predicate: ${query.sql}`);
+ }
+ return columns.every((column, i) => row[column] === query.params[i]);
+}
+
+function fakeDatabase(
+ initialRow: TokenRow,
+ options?: { afterSelect?: () => void },
+) {
+ let row: TokenRow | undefined = initialRow;
+ const db = {
+ select: () => ({
+ from: () => ({
+ where: (condition: SQL) => ({
+ limit: async () => {
+ const result = row && matchesPredicate(condition, row) ? [row] : [];
+ options?.afterSelect?.();
+ return result;
+ },
+ }),
+ }),
+ }),
+ delete: () => ({
+ where: async (condition: SQL) => {
+ if (row && matchesPredicate(condition, row)) {
+ row = undefined;
+ return [{ affectedRows: 1 }];
+ }
+ return [{ affectedRows: 0 }];
+ },
+ }),
+ };
+ return {
+ db: db as unknown as MySql2Database,
+ getRow: () => row,
+ setRow: (next: TokenRow) => {
+ row = next;
+ },
+ };
+}
+
+describe("useVerificationToken", () => {
+ const identifier = "[email protected]";
+ const validRow: TokenRow = {
+ identifier,
+ token: "111111",
+ expires: new Date(Date.now() + 60_000),
+ };
+
+ it("burns the code on a wrong guess instead of leaving it guessable", async () => {
+ const { db, getRow } = fakeDatabase({ ...validRow });
+ const adapter = DrizzleAdapter(db);
+
+ const wrongGuess = await adapter.useVerificationToken?.({
+ identifier,
+ token: "000000",
+ });
+
+ expect(wrongGuess).toBeNull();
+ expect(getRow()).toBeUndefined();
+
+ const correctGuessAfterward = await adapter.useVerificationToken?.({
+ identifier,
+ token: "111111",
+ });
+ expect(correctGuessAfterward).toBeNull();
+ });
+
+ it("returns the row and deletes it on a correct guess", async () => {
+ const { db, getRow } = fakeDatabase({ ...validRow });
+ const adapter = DrizzleAdapter(db);
+
+ const result = await adapter.useVerificationToken?.({
+ identifier,
+ token: "111111",
+ });
+
+ expect(result).toMatchObject({ identifier, token: "111111" });
+ expect(getRow()).toBeUndefined();
+ });
+
+ it("returns null when no code was ever requested for the identifier", async () => {
+ const { db } = fakeDatabase({ ...validRow });
+ const adapter = DrizzleAdapter(db);
+
+ const result = await adapter.useVerificationToken?.({
+ identifier: "[email protected]",
+ token: "111111",
+ });
+
+ expect(result).toBeNull();
+ });
+
+ it("lets only one of two concurrent correct guesses succeed", async () => {
+ const { db, getRow } = fakeDatabase({ ...validRow });
+ const adapter = DrizzleAdapter(db);
+
+ const [first, second] = await Promise.all([
+ adapter.useVerificationToken?.({ identifier, token: "111111" }),
+ adapter.useVerificationToken?.({ identifier, token: "111111" }),
+ ]);
+
+ const successes = [first, second].filter((result) => result !== null);
+ expect(successes).toHaveLength(1);
+ expect(getRow()).toBeUndefined();
+ });
+
+ it("does not delete a resent code that replaced the row a guess read", async () => {
+ const resentRow: TokenRow = {
+ identifier,
+ token: "222222",
+ expires: new Date(Date.now() + 60_000),
+ };
+ const { db, getRow, setRow } = fakeDatabase(
+ { ...validRow },
+ {
+ afterSelect: () => setRow({ ...resentRow }),
+ },
+ );
+ const adapter = DrizzleAdapter(db);
+
+ const wrongGuessAgainstStaleCode = await adapter.useVerificationToken?.({
+ identifier,
+ token: "000000",
+ });
+
+ expect(wrongGuessAgainstStaleCode).toBeNull();
+ expect(getRow()).toEqual(resentRow);
+ });
+});
diff --git a/apps/web/__tests__/unit/video-analytics-bulk.test.ts b/apps/web/__tests__/unit/video-analytics-bulk.test.ts
new file mode 100644
index 000000000..4d68e8d13
--- /dev/null
+++ b/apps/web/__tests__/unit/video-analytics-bulk.test.ts
@@ -0,0 +1,156 @@
+import { CurrentUser, Organisation, User, Video } from "@cap/web-domain";
+import { Effect, Exit, Option } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ rows: [] as Record<string, unknown>[],
+ select: vi.fn(),
+ query: vi.fn(),
+ readError: undefined as Error | undefined,
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: (columns: unknown) => {
+ mocks.select(columns);
+ return {
+ from: () => ({
+ where: async () => {
+ if (mocks.readError) throw mocks.readError;
+ return mocks.rows;
+ },
+ }),
+ };
+ },
+ }),
+}));
+vi.mock("@cap/utils", () => ({ dub: {} }));
+vi.mock("@cap/web-backend/src/Storage/index", async () => {
+ const { Effect } = await import("effect");
+ class Storage extends Effect.Service<Storage>()("Storage", {
+ sync: () => ({}),
+ }) {}
+ return { Storage };
+});
+vi.mock("@cap/web-backend/src/Tinybird/index", async () => {
+ const { Effect } = await import("effect");
+ class Tinybird extends Effect.Service<Tinybird>()("Tinybird", {
+ sync: () => ({
+ querySql: (sql: string) => {
+ mocks.query(sql);
+ return Effect.succeed({
+ data: [{ pathname: "/s/owned", views: 12 }],
+ });
+ },
+ }),
+ }) {}
+ return { Tinybird };
+});
+
+const { Videos } = await import("@cap/web-backend/src/Videos/index");
+const { VideosPolicy } = await import(
+ "@cap/web-backend/src/Videos/VideosPolicy"
+);
+const { VideosRepo } = await import("@cap/web-backend/src/Videos/VideosRepo");
+const { Database } = await import("@cap/web-backend/src/Database");
+const { Storage } = await import("@cap/web-backend/src/Storage/index");
+const { Tinybird } = await import("@cap/web-backend/src/Tinybird/index");
+const { Policy } = await import("@cap/web-domain");
+const check = vi.fn((video: { id: string }) =>
+ video.id === "denied"
+ ? Effect.fail(new Policy.PolicyDeniedError())
+ : video.id === "protected"
+ ? Effect.fail(
+ new Video.VerifyVideoPasswordError({
+ id: Video.VideoId.make(video.id),
+ cause: "not-provided",
+ }),
+ )
+ : Effect.void,
+);
+const user: CurrentUser["Type"] = {
+ id: User.UserId.make("owner"),
+ email: "[email protected]",
+ activeOrganizationId: Organisation.OrganisationId.make("org"),
+ iconUrlOrKey: Option.none(),
+};
+
+function run(ids: string[]) {
+ return Effect.runPromise(
+ Effect.flatMap(Videos, (videos) =>
+ videos.getAnalyticsBulk(ids.map((id) => Video.VideoId.make(id))),
+ ).pipe(
+ Effect.provide(Videos.DefaultWithoutDependencies),
+ Effect.provideService(
+ VideosPolicy,
+ VideosPolicy.make({
+ canViewLoaded: check,
+ canView: vi.fn(),
+ isOwner: vi.fn(),
+ isOwnerLoaded: vi.fn(),
+ getViewableById: vi.fn(),
+ getOwnedById: vi.fn(),
+ }),
+ ),
+ Effect.provide([
+ Database.Default,
+ VideosRepo.Default,
+ Storage.Default,
+ Tinybird.Default,
+ ]),
+ Effect.provideService(CurrentUser, user),
+ ),
+ );
+}
+
+beforeEach(() => {
+ mocks.readError = undefined;
+ mocks.rows = ["owned", "denied", "protected"].map((id) => ({
+ id,
+ ownerId: "owner",
+ orgId: "org",
+ public: false,
+ password: id === "protected" ? "password-hash" : null,
+ }));
+});
+
+describe("bulk video analytics", () => {
+ it("batches lookups and preserves order, duplicates and missing results", async () => {
+ const results = await run(["missing", "owned", "owned"]);
+ expect(results.map((result) => Exit.getOrElse(result, () => null))).toEqual(
+ [{ count: 0 }, { count: 12 }, { count: 12 }],
+ );
+ expect(mocks.select).toHaveBeenCalledTimes(1);
+ expect(mocks.query).toHaveBeenCalledTimes(1);
+ });
+
+ it("preserves per-video access and password failures", async () => {
+ const results = await run(["denied", "owned", "protected"]);
+ expect(results.map((result) => Exit.isFailure(result))).toEqual([
+ true,
+ false,
+ true,
+ ]);
+ expect(results.map((result) => Exit.getOrElse(result, () => null))).toEqual(
+ [null, { count: 12 }, null],
+ );
+ expect(mocks.query.mock.calls[0]?.[0]).not.toContain("/s/denied");
+ expect(mocks.query.mock.calls[0]?.[0]).not.toContain("/s/protected");
+ expect(check).toHaveBeenCalledWith(
+ expect.objectContaining({ id: "protected" }),
+ Option.some("password-hash"),
+ );
+ });
+
+ it("does not read the database for an empty batch", async () => {
+ expect(await run([])).toEqual([]);
+ expect(mocks.select).not.toHaveBeenCalled();
+ expect(mocks.query).not.toHaveBeenCalled();
+ });
+
+ it("reports database failures instead of returning zero views", async () => {
+ mocks.readError = new Error("Database unavailable");
+ await expect(run(["owned"])).rejects.toThrow();
+ expect(mocks.query).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/video-create-atomicity.test.ts b/apps/web/__tests__/unit/video-create-atomicity.test.ts
index 6f2c15fdf..69f0aa87f 100644
--- a/apps/web/__tests__/unit/video-create-atomicity.test.ts
+++ b/apps/web/__tests__/unit/video-create-atomicity.test.ts
@@ -79,6 +79,11 @@ vi.mock("@cap/database/schema", () => ({
videos: { name: "video", id: "id" },
videoUploads: { name: "upload", videoId: "videoId" },
}));
+// The real lookup reads `organizations`, which this transaction mock does not
+// model; visibility is not what these tests are about.
+vi.mock("@cap/database/video-sharing-default", () => ({
+ getNewVideoPublic: async () => true,
+}));
vi.mock("@cap/env", () => ({
serverEnv: () => ({ CAP_VIDEOS_DEFAULT_PUBLIC: true }),
}));
diff --git a/apps/web/__tests__/unit/video-download-key.test.ts b/apps/web/__tests__/unit/video-download-key.test.ts
new file mode 100644
index 000000000..4776c6321
--- /dev/null
+++ b/apps/web/__tests__/unit/video-download-key.test.ts
@@ -0,0 +1,88 @@
+import { Video } from "@cap/web-domain";
+import { Effect } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ rows: [] as unknown[][],
+ sign: vi.fn(),
+ access: vi.fn(),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({
+ from: () => ({ where: () => Promise.resolve(mocks.rows.shift() ?? []) }),
+ }),
+ }),
+}));
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: () => Promise.resolve({ id: "owner" }),
+}));
+vi.mock("@cap/web-backend", () => ({
+ Storage: { getAccessForVideo: mocks.access },
+}));
+vi.mock("@/lib/video-download-permissions", () => ({
+ canUserDownloadVideo: () => Promise.resolve(true),
+}));
+vi.mock("@/lib/server", () => ({ runPromise: Effect.runPromise }));
+
+import { getVideoDownloadInfo } from "@/actions/videos/download";
+
+describe("current video download", () => {
+ beforeEach(() => {
+ mocks.rows = [];
+ mocks.sign.mockReset();
+ mocks.access.mockReset();
+ });
+
+ it.each(["current", "original"] as const)(
+ "signs the %s MP4 key",
+ async (variant) => {
+ const outputKey = "owner/video/.recording/outputs/generation/result.mp4";
+ const originalKey = "owner/video/.recording/sources/original.mp4";
+ mocks.rows = [
+ [
+ {
+ id: "video",
+ ownerId: "owner",
+ orgId: "organization",
+ name: "Recording",
+ public: true,
+ source: { type: "desktopMP4", outputKey },
+ metadata: null,
+ bucket: "custom-bucket",
+ storageIntegrationId: null,
+ folderId: null,
+ transcriptionStatus: null,
+ width: null,
+ height: null,
+ duration: null,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ },
+ ],
+ variant === "current" ? [] : [{ sourceKey: originalKey }],
+ ];
+ mocks.sign.mockReturnValue(
+ Effect.succeed("https://media.example.com/video.mp4"),
+ );
+ mocks.access.mockReturnValue(
+ Effect.succeed([
+ {
+ headObject: () => Effect.succeed({}),
+ getSignedObjectUrl: mocks.sign,
+ },
+ ]),
+ );
+
+ expect(
+ await getVideoDownloadInfo(Video.VideoId.make("video"), variant),
+ ).toMatchObject({
+ success: true,
+ });
+ expect(mocks.sign).toHaveBeenCalledExactlyOnceWith(
+ variant === "current" ? outputKey : originalKey,
+ );
+ },
+ );
+});
diff --git a/apps/web/__tests__/unit/video-edit-actions.test.ts b/apps/web/__tests__/unit/video-edit-actions.test.ts
index 4294a303d..c1ba0f9aa 100644
--- a/apps/web/__tests__/unit/video-edit-actions.test.ts
+++ b/apps/web/__tests__/unit/video-edit-actions.test.ts
@@ -166,7 +166,9 @@ function legacyUpload() {
describe("video edit claims and recovery", () => {
it("claims the recording before copying its original source", async () => {
- mocks.head.mockReturnValue(Effect.fail(new Error("Missing")));
+ mocks.head.mockReturnValue(
+ Effect.fail(Object.assign(new Error("Missing"), { name: "NotFound" })),
+ );
mocks.copy.mockImplementation(() => {
expect(mocks.upload?.phase).toBe("processing");
return Effect.void;
diff --git a/apps/web/__tests__/unit/video-edits.test.ts b/apps/web/__tests__/unit/video-edits.test.ts
index ff3fb5cbf..cda1e4522 100644
--- a/apps/web/__tests__/unit/video-edits.test.ts
+++ b/apps/web/__tests__/unit/video-edits.test.ts
@@ -224,6 +224,79 @@ describe("timeline editing", () => {
expect(mapTimelineDisplayTimeToSourceTime(deleted, 3)).toBe(3);
});
+ it("preserves display offsets and split indices across trimmed deletions", () => {
+ const state = {
+ ...createTimelineState(20),
+ trimStart: 2,
+ trimEnd: 18,
+ splitPoints: [17, 14, 12, 10, 9, 8, 7, 6, 4, 3],
+ deletedRanges: [
+ { start: 17, end: 19 },
+ { start: 10, end: 12 },
+ { start: 6, end: 8 },
+ { start: 1, end: 3 },
+ ],
+ selectedSegmentId: "8:9",
+ };
+
+ expect(
+ getTimelineDisplaySegments(state).map(
+ ({ start, end, displayStart, displayEnd, selected }) => ({
+ start,
+ end,
+ displayStart,
+ displayEnd,
+ selected,
+ }),
+ ),
+ ).toEqual([
+ { start: 3, end: 4, displayStart: 1, displayEnd: 2, selected: false },
+ { start: 4, end: 6, displayStart: 2, displayEnd: 4, selected: false },
+ { start: 8, end: 9, displayStart: 4, displayEnd: 5, selected: true },
+ { start: 9, end: 10, displayStart: 5, displayEnd: 6, selected: false },
+ { start: 12, end: 14, displayStart: 6, displayEnd: 8, selected: false },
+ { start: 14, end: 17, displayStart: 8, displayEnd: 11, selected: false },
+ ]);
+ expect(
+ getTimelineDisplaySplitPoints(state).map(
+ ({ time, sourceTimes, splitIndices }) => ({
+ time,
+ sourceTimes,
+ splitIndices,
+ }),
+ ),
+ ).toEqual([
+ { time: 2, sourceTimes: [4], splitIndices: [1] },
+ { time: 4, sourceTimes: [6, 8], splitIndices: [2, 3] },
+ { time: 5, sourceTimes: [9], splitIndices: [4] },
+ { time: 6, sourceTimes: [10, 12], splitIndices: [5, 6] },
+ { time: 8, sourceTimes: [14], splitIndices: [7] },
+ ]);
+ });
+
+ it("retains nearby split indices when a subminimum segment is omitted", () => {
+ const state = {
+ ...createTimelineState(10),
+ splitPoints: [1, 1.001, 2, 3, 7, 8],
+ deletedRanges: [{ start: 3, end: 7 }],
+ };
+
+ expect(
+ getTimelineDisplaySplitPoints(state).map(
+ ({ time, sourceTimes, splitIndices }) => ({
+ time,
+ sourceTimes,
+ splitIndices,
+ }),
+ ),
+ ).toEqual([
+ { time: 1, sourceTimes: [1], splitIndices: [0, 1] },
+ { time: 2, sourceTimes: [2], splitIndices: [2] },
+ { time: 3, sourceTimes: [3, 7], splitIndices: [3, 4] },
+ { time: 4, sourceTimes: [8], splitIndices: [5] },
+ ]);
+ });
+
it("removes a collapsed display split by restoring the deleted segment", () => {
const splitState = splitTimelineAt(
splitTimelineAt(createTimelineState(10), 3),
diff --git a/apps/web/__tests__/unit/video-pipeline-recovery.test.ts b/apps/web/__tests__/unit/video-pipeline-recovery.test.ts
index dd1de83df..561debc48 100644
--- a/apps/web/__tests__/unit/video-pipeline-recovery.test.ts
+++ b/apps/web/__tests__/unit/video-pipeline-recovery.test.ts
@@ -2,6 +2,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const mockDb = vi.fn();
const mockStart = vi.fn();
+const mockStartLoomImport = vi.fn();
+const mockLoomImportRunState = vi.fn();
const mockTranscribeVideo = vi.fn();
const mockStartAiGeneration = vi.fn();
@@ -69,6 +71,17 @@ vi.mock("workflow/api", () => ({
start: mockStart,
}));
+vi.mock("@/lib/loom-import-start", () => ({
+ startLoomImportWorkflow: mockStartLoomImport,
+ getLoomImportRunState: mockLoomImportRunState,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE: "Confirming Loom import",
+ LoomImportStartError: class extends Error {
+ constructor(readonly canRetry: boolean) {
+ super("Loom startup failed");
+ }
+ },
+}));
+
vi.mock("@/lib/transcribe", () => ({
transcribeVideo: mockTranscribeVideo,
}));
@@ -127,6 +140,8 @@ const staleAt = new Date("2026-07-20T14:00:00.000Z");
beforeEach(() => {
vi.clearAllMocks();
mockStart.mockResolvedValue({ runId: "run-1" });
+ mockStartLoomImport.mockReset().mockResolvedValue(undefined);
+ mockLoomImportRunState.mockReset().mockResolvedValue("inactive");
mockTranscribeVideo.mockResolvedValue({
success: true,
message: "Transcription workflow started",
@@ -217,7 +232,11 @@ describe("recoverStalledVideoPipeline", () => {
expect(result.media.statuses).toEqual({ "already-claimed": 1 });
});
- it.each(["Downloading video...", "Queued for Loom import processing..."])(
+ it.each([
+ "Downloading video...",
+ "Queued for Loom import processing...",
+ "Confirming Loom import",
+ ])(
"restarts orphaned Loom jobs in the %s state through the bulk import workflow",
async (processingMessage) => {
const mediaCandidate = {
@@ -226,6 +245,7 @@ describe("recoverStalledVideoPipeline", () => {
bucketId: null,
rawFileKey: "user-1/video-loom/raw.mp4",
loomVideoId: "loom-video-1",
+ metadata: { loomImportRun: { runId: "old-run", dispatch: "accepted" } },
processingMessage,
updatedAt: staleAt,
};
@@ -248,17 +268,100 @@ describe("recoverStalledVideoPipeline", () => {
"videoUploads.processingMessage",
expect.arrayContaining([processingMessage]),
);
- expect(mockStart).toHaveBeenCalledWith(expect.any(Function), [
+ expect(mockStartLoomImport).toHaveBeenCalledWith(
expect.objectContaining({
videoId: "video-loom",
loomVideoId: "loom-video-1",
reuseExistingRawUpload: true,
}),
- ]);
+ );
+ expect(mockStart).not.toHaveBeenCalled();
expect(result.media.statuses).toEqual({ started: 1 });
},
);
+ it.each(["active", "pending", "unconfirmed", "legacy"])(
+ "preserves a %s Loom attempt without starting a replacement",
+ async (state) => {
+ mockLoomImportRunState.mockResolvedValue(
+ state === "legacy" ? "inactive" : state,
+ );
+ const mediaCandidate = {
+ videoId: "video-loom",
+ userId: "user-1",
+ bucketId: null,
+ rawFileKey: "user-1/video-loom/raw.mp4",
+ loomVideoId: "loom-video-1",
+ metadata:
+ state === "legacy"
+ ? null
+ : { loomImportRun: { runId: "old-run", dispatch: "accepted" } },
+ processingMessage: "Downloading video...",
+ updatedAt: staleAt,
+ };
+ const update = makeUpdateChain(1);
+ let dbCall = 0;
+ mockDb.mockImplementation(() => {
+ dbCall++;
+ if (dbCall === 1) return makeSelectChain([mediaCandidate]);
+ if (dbCall <= 3) return makeSelectChain([]);
+ return update;
+ });
+ const { recoverStalledVideoPipeline } = await import(
+ "@/lib/video-pipeline-recovery"
+ );
+ const result = await recoverStalledVideoPipeline({ now, concurrency: 1 });
+ expect(mockStartLoomImport).not.toHaveBeenCalled();
+ expect(mockStart).not.toHaveBeenCalled();
+ expect(result.media.statuses).toEqual({
+ [state === "active" ? "already-claimed" : "reconciliation-required"]: 1,
+ });
+ if (state === "active") expect(update.set).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each([true, false])(
+ "keeps the claimed Loom attempt safe after retryable=%s startup failure",
+ async (canRetry) => {
+ const { LoomImportStartError } = await import("@/lib/loom-import-start");
+ mockStartLoomImport.mockRejectedValue(
+ new LoomImportStartError(canRetry, new Error("startup")),
+ );
+ const mediaCandidate = {
+ videoId: "video-loom",
+ userId: "user-1",
+ bucketId: null,
+ rawFileKey: "user-1/video-loom/raw.mp4",
+ loomVideoId: "loom-video-1",
+ metadata: { loomImportRun: { runId: "old-run", dispatch: "accepted" } },
+ processingMessage: "Downloading video...",
+ updatedAt: staleAt,
+ };
+ const update = makeUpdateChain(1);
+ let dbCall = 0;
+ mockDb.mockImplementation(() => {
+ dbCall++;
+ if (dbCall === 1) return makeSelectChain([mediaCandidate]);
+ if (dbCall <= 3) return makeSelectChain([]);
+ return update;
+ });
+ const { recoverStalledVideoPipeline } = await import(
+ "@/lib/video-pipeline-recovery"
+ );
+ const result = await recoverStalledVideoPipeline({ now, concurrency: 1 });
+ expect(result.media.statuses).toEqual({
+ [canRetry ? "retry-scheduled" : "reconciliation-required"]: 1,
+ });
+ expect(update.set).toHaveBeenLastCalledWith(
+ expect.objectContaining({
+ processingMessage: canRetry
+ ? "Processing recovery will retry automatically"
+ : "Confirming Loom import",
+ }),
+ );
+ },
+ );
+
it("starts eligible AI generation when legacy metadata is null", async () => {
const aiCandidate = {
videoId: "video-ai",
diff --git a/apps/web/__tests__/unit/video-processing-recovery.test.ts b/apps/web/__tests__/unit/video-processing-recovery.test.ts
index e55f634d7..b2d59f6fa 100644
--- a/apps/web/__tests__/unit/video-processing-recovery.test.ts
+++ b/apps/web/__tests__/unit/video-processing-recovery.test.ts
@@ -1,8 +1,11 @@
+import type { VideoMetadata } from "@cap/database/types";
import { beforeEach, describe, expect, it, vi } from "vitest";
const mockDb = vi.fn();
const mockStart = vi.fn();
const mockSetVideoProcessingError = vi.fn();
+const mockGetLoomImportRunState = vi.fn();
+const mockStartLoomImportWorkflow = vi.fn();
vi.mock("@cap/database", () => ({
db: mockDb,
@@ -21,6 +24,7 @@ vi.mock("@cap/database/schema", () => ({
orgId: "videos.orgId",
bucket: "videos.bucket",
source: "videos.source",
+ metadata: "videos.metadata",
},
videoUploads: {
videoId: "videoUploads.videoId",
@@ -42,7 +46,10 @@ vi.mock("drizzle-orm", () => ({
lte: vi.fn((left: unknown, right: unknown) => ({ left, right })),
notLike: vi.fn((left: unknown, right: unknown) => ({ left, right })),
or: vi.fn((...conditions: unknown[]) => conditions),
- sql: vi.fn(),
+ sql: vi.fn((strings: TemplateStringsArray, ...values: unknown[]) => ({
+ strings,
+ values,
+ })),
}));
vi.mock("workflow/api", () => ({
@@ -53,6 +60,20 @@ vi.mock("@/lib/video-processing", () => ({
setVideoProcessingError: mockSetVideoProcessingError,
}));
+vi.mock("@/lib/loom-import-start", () => ({
+ getLoomImportRunState: mockGetLoomImportRunState,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE: "Confirmation required",
+ startLoomImportWorkflow: mockStartLoomImportWorkflow,
+ LoomImportStartError: class extends Error {
+ constructor(
+ readonly canRetry: boolean,
+ cause: unknown,
+ ) {
+ super("Loom import startup failed", { cause });
+ }
+ },
+}));
+
vi.mock("@/workflows/process-video", () => ({
processVideoWorkflow: vi.fn(),
}));
@@ -66,6 +87,8 @@ type Candidate = {
userId: string;
bucketId: string | null;
rawFileKey: string | null;
+ uploadUpdatedAt: Date;
+ metadata?: VideoMetadata;
};
function makeSelectChain(candidates: unknown[]) {
@@ -105,17 +128,170 @@ const candidate: Candidate = {
userId: "user-1",
bucketId: null,
rawFileKey: "user-1/video-1/raw.mp4",
+ uploadUpdatedAt: new Date("2026-10-21T16:00:00Z"),
};
beforeEach(() => {
- vi.clearAllMocks();
+ vi.resetAllMocks();
mockStart.mockResolvedValue({ runId: "run-1" });
mockSetVideoProcessingError.mockResolvedValue(undefined);
+ mockGetLoomImportRunState.mockResolvedValue("inactive");
+ mockStartLoomImportWorkflow.mockResolvedValue(undefined);
});
describe("recoverFailedVideoProcessing", () => {
+ it("keeps one recovery budget across indexed startup phases", async () => {
+ const createCandidates = (
+ phase: "uploading" | "processing",
+ offset: number,
+ ) =>
+ Array.from({ length: 8 }, (_, index) => ({
+ ...candidate,
+ phase,
+ videoId: `video-${index * 2 + offset}`,
+ loomVideoId: `loom-${index * 2 + offset}`,
+ uploadUpdatedAt: new Date(index * 2000 + offset * 1000),
+ metadata: {
+ loomImportRun: {
+ runId: `run-${index * 2 + offset}`,
+ dispatch: "accepted" as const,
+ },
+ },
+ }));
+ mockDb
+ .mockReturnValueOnce(makeSelectChain(createCandidates("uploading", 0)))
+ .mockReturnValueOnce(makeSelectChain(createCandidates("processing", 1)))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValue(makeUpdateChain(1));
+ mockGetLoomImportRunState.mockResolvedValue("active");
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+
+ const result = await recoverFailedVideoProcessing();
+
+ expect(result.checked).toBe(8);
+ expect(result.results.map((row) => row.videoId)).toEqual(
+ Array.from({ length: 8 }, (_, index) => `video-${index}`),
+ );
+ expect(mockGetLoomImportRunState).toHaveBeenCalledTimes(8);
+ expect(mockStartLoomImportWorkflow).not.toHaveBeenCalled();
+ });
+
+ it("rotates eight unresolved holds so a newer import is checked on the next scan", async () => {
+ const holds = Array.from({ length: 8 }, (_, i) => ({
+ ...candidate,
+ videoId: `hold-${i}`,
+ phase: "processing",
+ loomVideoId: `loom-${i}`,
+ uploadUpdatedAt: new Date(1000 + i),
+ metadata: {
+ loomImportRun: { runId: `hold-${i}`, dispatch: "uncertain" as const },
+ },
+ }));
+ const pending = {
+ ...candidate,
+ videoId: "newer",
+ phase: "uploading",
+ rawFileKey: null,
+ loomVideoId: "newer-loom",
+ uploadUpdatedAt: new Date(9000),
+ metadata: {
+ loomImportRun: { runId: "newer", dispatch: "rejected" as const },
+ },
+ };
+ let selection = 0;
+ mockDb.mockImplementation(() => ({
+ select: (...args: unknown[]) => {
+ const slot = selection++ % 4;
+ const rows =
+ slot === 0
+ ? [pending]
+ : slot === 1
+ ? [...holds].sort(
+ (a, b) =>
+ a.uploadUpdatedAt.getTime() - b.uploadUpdatedAt.getTime(),
+ )
+ : [];
+ return makeSelectChain(rows).select(...args);
+ },
+ update: () => ({
+ set: (values: { updatedAt?: Date }) => ({
+ where: async (
+ conditions: Array<{ left?: string; right?: unknown }>,
+ ) => {
+ const id = conditions.find(
+ (c) => c?.left === "videoUploads.videoId",
+ )?.right;
+ const row = [...holds, pending].find((row) => row.videoId === id);
+ if (row && values.updatedAt) row.uploadUpdatedAt = values.updatedAt;
+ return [{ affectedRows: 1 }];
+ },
+ }),
+ }),
+ }));
+ mockGetLoomImportRunState.mockImplementation(async (receipt) =>
+ receipt.runId === "newer" ? "inactive" : "unconfirmed",
+ );
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+ const first = await recoverFailedVideoProcessing();
+ expect(first.results.map((row) => row.videoId)).not.toContain("newer");
+ const second = await recoverFailedVideoProcessing();
+ expect(second.results).toContainEqual({
+ videoId: "newer",
+ kind: "loom",
+ status: "started",
+ });
+ expect(mockStartLoomImportWorkflow).toHaveBeenCalledTimes(1);
+ });
+
+ it("reserves a failed-import slot while uncertain startups remain protected", async () => {
+ const pending = Array.from({ length: 8 }, (_, index) => ({
+ ...candidate,
+ videoId: `pending-${index}`,
+ loomVideoId: `pending-loom-${index}`,
+ phase: "uploading",
+ metadata: {
+ loomImportRun: { runId: `run-${index}`, dispatch: "uncertain" },
+ },
+ }));
+ const failedQuery = makeSelectChain([
+ { ...candidate, videoId: "failed-video", orgId: "organization-1" },
+ ]);
+ mockDb
+ .mockReturnValueOnce(makeSelectChain(pending))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(failedQuery)
+ .mockReturnValueOnce(makeSelectChain([{ loomVideoId: "failed-loom" }]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValue(makeUpdateChain(1));
+ mockGetLoomImportRunState.mockImplementation(async (receipt) =>
+ receipt ? "unconfirmed" : "inactive",
+ );
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+
+ const result = await recoverFailedVideoProcessing();
+
+ expect(failedQuery.limit).toHaveBeenCalledWith(1);
+ expect(result.checked).toBe(8);
+ expect(result.statuses).toEqual({
+ "reconciliation-required": 7,
+ started: 1,
+ });
+ expect(mockStartLoomImportWorkflow).toHaveBeenCalledTimes(1);
+ expect(mockStartLoomImportWorkflow).toHaveBeenCalledWith(
+ expect.objectContaining({ videoId: "failed-video" }),
+ );
+ });
+
it("atomically claims and restarts an affected upload", async () => {
mockDb
+ .mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([candidate]))
@@ -132,6 +308,7 @@ describe("recoverFailedVideoProcessing", () => {
it("does not start a duplicate workflow when another run owns the claim", async () => {
mockDb
+ .mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([candidate]))
@@ -148,6 +325,7 @@ describe("recoverFailedVideoProcessing", () => {
it("keeps a transient start failure eligible for the next recovery run", async () => {
mockDb
+ .mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([candidate]))
@@ -171,6 +349,7 @@ describe("recoverFailedVideoProcessing", () => {
it("restarts a Loom import that exhausted its capacity retries", async () => {
mockDb
+ .mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(
makeSelectChain([
@@ -189,20 +368,21 @@ describe("recoverFailedVideoProcessing", () => {
const result = await recoverFailedVideoProcessing();
- expect(mockStart).toHaveBeenCalledWith(expect.any(Function), [
+ expect(mockStartLoomImportWorkflow).toHaveBeenCalledWith(
expect.objectContaining({
videoId: "video-1",
loomVideoId: "loom-video-1",
rawFileKey: "user-1/video-1/raw.mp4",
reuseExistingRawUpload: true,
}),
- ]);
+ );
expect(result.statuses).toEqual({ started: 1 });
});
it("keeps a failed Loom workflow start eligible for Loom-specific recovery", async () => {
const restore = makeUpdateChain(1);
mockDb
+ .mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(
makeSelectChain([
@@ -216,7 +396,10 @@ describe("recoverFailedVideoProcessing", () => {
.mockReturnValueOnce(makeSelectChain([]))
.mockReturnValueOnce(makeUpdateChain(1))
.mockReturnValueOnce(restore);
- mockStart.mockRejectedValueOnce(new Error("temporary start failure"));
+ const { LoomImportStartError } = await import("@/lib/loom-import-start");
+ mockStartLoomImportWorkflow.mockRejectedValueOnce(
+ new LoomImportStartError(true, new Error("temporary start failure")),
+ );
const { recoverFailedVideoProcessing } = await import(
"@/lib/video-processing-recovery"
);
@@ -231,4 +414,324 @@ describe("recoverFailedVideoProcessing", () => {
);
expect(result.statuses).toEqual({ "retry-scheduled": 1 });
});
+
+ it.each(["pending", "accepted", "uncertain"] as const)(
+ "preserves a slow source lookup with a protected %s receipt",
+ async (dispatch) => {
+ const receipt = { runId: "slow-source-run", dispatch };
+ mockDb
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(
+ makeSelectChain([
+ {
+ ...candidate,
+ rawFileKey: null,
+ loomVideoId: "loom-video-1",
+ uploadUpdatedAt: new Date(Date.now() - 16 * 60 * 1000),
+ metadata: { loomImportRun: receipt },
+ },
+ ]),
+ )
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeUpdateChain(1));
+ mockGetLoomImportRunState.mockResolvedValueOnce("active");
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+
+ const result = await recoverFailedVideoProcessing();
+
+ expect(mockStart).not.toHaveBeenCalled();
+ expect(mockStartLoomImportWorkflow).not.toHaveBeenCalled();
+ expect(result.statuses).toEqual({ "already-claimed": 1 });
+ expect(mockDb).toHaveBeenCalledTimes(5);
+ expect(mockGetLoomImportRunState).toHaveBeenCalledWith(receipt);
+ },
+ );
+
+ it("rechecks the selected upload timestamp before a terminal run is recovered", async () => {
+ const claim = makeUpdateChain(0);
+ const receipt = { runId: "terminal-run", dispatch: "accepted" as const };
+ mockDb
+ .mockReturnValueOnce(
+ makeSelectChain([
+ {
+ ...candidate,
+ rawFileKey: null,
+ loomVideoId: "loom-video-1",
+ metadata: { loomImportRun: receipt },
+ },
+ ]),
+ )
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(claim);
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+
+ const result = await recoverFailedVideoProcessing();
+
+ expect(claim.where).toHaveBeenCalledWith(
+ expect.arrayContaining([
+ { left: "videoUploads.updatedAt", right: candidate.uploadUpdatedAt },
+ ]),
+ );
+ expect(mockStartLoomImportWorkflow).not.toHaveBeenCalled();
+ expect(result.statuses).toEqual({ "already-claimed": 1 });
+ });
+
+ it.each([
+ { phase: "uploading", state: "pending" },
+ { phase: "uploading", state: "unconfirmed" },
+ { phase: "processing", state: "pending" },
+ { phase: "processing", state: "unconfirmed" },
+ ])(
+ "exposes startup reconciliation without redispatching: %j",
+ async (row) => {
+ const receipt = {
+ runId: "unresolved-run",
+ dispatch: "uncertain" as const,
+ };
+ const selected = makeSelectChain([
+ {
+ ...candidate,
+ phase: row.phase,
+ loomVideoId: "loom-video-1",
+ metadata: { loomImportRun: receipt },
+ },
+ ]);
+ const update = makeUpdateChain(1);
+ mockDb
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(selected)
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(update);
+ mockGetLoomImportRunState.mockResolvedValueOnce(row.state);
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+
+ const result = await recoverFailedVideoProcessing();
+
+ expect(result.statuses).toEqual({ "reconciliation-required": 1 });
+ expect(update.set).toHaveBeenCalledWith({
+ processingMessage: "Confirmation required",
+ updatedAt: expect.any(Date),
+ });
+ expect(update.where).toHaveBeenCalledWith(
+ expect.arrayContaining([
+ { left: "videoUploads.updatedAt", right: candidate.uploadUpdatedAt },
+ expect.objectContaining({
+ values: expect.arrayContaining(["unresolved-run"]),
+ }),
+ ]),
+ );
+ expect(selected.select).toHaveBeenCalledWith(
+ expect.objectContaining({ phase: "videoUploads.phase" }),
+ );
+ expect(mockStartLoomImportWorkflow).not.toHaveBeenCalled();
+ expect(mockStart).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each([false, true])(
+ "reclaims a confirmed inactive processing attempt without losing its source, rejection=%s",
+ async (reject) => {
+ const { LoomImportStartError } = await import("@/lib/loom-import-start");
+ const receipt = { runId: "failed-run", dispatch: "accepted" as const };
+ const claim = makeUpdateChain(1);
+ const restore = makeUpdateChain(1);
+ mockDb
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(
+ makeSelectChain([
+ {
+ ...candidate,
+ phase: "processing",
+ loomVideoId: "loom-1",
+ metadata: { loomImportRun: receipt },
+ },
+ ]),
+ )
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(claim)
+ .mockReturnValueOnce(restore);
+ if (reject)
+ mockStartLoomImportWorkflow.mockRejectedValueOnce(
+ new LoomImportStartError(true, new Error("busy")),
+ );
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+ const result = await recoverFailedVideoProcessing();
+ expect(result.statuses).toEqual({
+ [reject ? "retry-scheduled" : "started"]: 1,
+ });
+ expect(mockStartLoomImportWorkflow).toHaveBeenCalledWith(
+ expect.objectContaining({
+ rawFileKey: candidate.rawFileKey,
+ reuseExistingRawUpload: true,
+ }),
+ );
+ expect(claim.where).toHaveBeenCalledWith(
+ expect.arrayContaining([
+ { left: "videoUploads.rawFileKey", right: candidate.rawFileKey },
+ { left: "videoUploads.updatedAt", right: candidate.uploadUpdatedAt },
+ expect.objectContaining({
+ values: expect.arrayContaining(["failed-run"]),
+ }),
+ ]),
+ );
+ if (reject)
+ expect(restore.set).toHaveBeenCalledWith(
+ expect.objectContaining({
+ phase: "error",
+ rawFileKey: candidate.rawFileKey,
+ }),
+ );
+ },
+ );
+
+ it("does not change reconciliation state after the selected claim changed", async () => {
+ const update = makeUpdateChain(0);
+ mockDb
+ .mockReturnValueOnce(
+ makeSelectChain([
+ {
+ ...candidate,
+ phase: "processing",
+ loomVideoId: "loom-video-1",
+ metadata: {
+ loomImportRun: { runId: "old-run", dispatch: "pending" },
+ },
+ },
+ ]),
+ )
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(update);
+ mockGetLoomImportRunState.mockResolvedValueOnce("unconfirmed");
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+
+ expect((await recoverFailedVideoProcessing()).statuses).toEqual({
+ "already-claimed": 1,
+ });
+ expect(mockStartLoomImportWorkflow).not.toHaveBeenCalled();
+ });
+
+ it("recovers an uploading confirmation hold after its run becomes terminal", async () => {
+ const update = makeUpdateChain(1);
+ mockDb
+ .mockReturnValueOnce(
+ makeSelectChain([
+ {
+ ...candidate,
+ phase: "uploading",
+ rawFileKey: null,
+ loomVideoId: "loom-video-1",
+ metadata: {
+ loomImportRun: { runId: "terminal-run", dispatch: "uncertain" },
+ },
+ },
+ ]),
+ )
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(update);
+ mockGetLoomImportRunState.mockResolvedValueOnce("inactive");
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+
+ expect((await recoverFailedVideoProcessing()).statuses).toEqual({
+ started: 1,
+ });
+ expect(update.where).toHaveBeenCalledWith(
+ expect.arrayContaining([
+ expect.arrayContaining([
+ {
+ left: "videoUploads.processingMessage",
+ right: "Confirmation required",
+ },
+ ]),
+ expect.objectContaining({
+ values: expect.arrayContaining(["terminal-run"]),
+ }),
+ ]),
+ );
+ expect(mockStartLoomImportWorkflow).toHaveBeenCalledTimes(1);
+ });
+
+ it("retains the recovery claim when dispatch acknowledgement is uncertain", async () => {
+ mockDb
+ .mockReturnValueOnce(
+ makeSelectChain([
+ { ...candidate, rawFileKey: null, loomVideoId: "loom-video-1" },
+ ]),
+ )
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeUpdateChain(1));
+ const { LoomImportStartError } = await import("@/lib/loom-import-start");
+ mockStartLoomImportWorkflow.mockRejectedValueOnce(
+ new LoomImportStartError(false, new Error("acknowledgement lost")),
+ );
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+
+ const result = await recoverFailedVideoProcessing();
+
+ expect(mockDb).toHaveBeenCalledTimes(5);
+ expect(mockStart).not.toHaveBeenCalled();
+ expect(result.statuses).toEqual({ "already-claimed": 1 });
+ });
+
+ it("restores only the claimed pending upload after a confirmed queue rejection", async () => {
+ const claim = makeUpdateChain(1);
+ const restore = makeUpdateChain(1);
+ mockDb
+ .mockReturnValueOnce(
+ makeSelectChain([
+ { ...candidate, rawFileKey: null, loomVideoId: "loom-video-1" },
+ ]),
+ )
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(makeSelectChain([]))
+ .mockReturnValueOnce(claim)
+ .mockReturnValueOnce(restore);
+ const { LoomImportStartError } = await import("@/lib/loom-import-start");
+ mockStartLoomImportWorkflow.mockRejectedValueOnce(
+ new LoomImportStartError(true, new Error("queue rejected")),
+ );
+ const { recoverFailedVideoProcessing } = await import(
+ "@/lib/video-processing-recovery"
+ );
+
+ const result = await recoverFailedVideoProcessing();
+
+ expect(restore.set).toHaveBeenCalledWith(
+ expect.objectContaining({ phase: "uploading", rawFileKey: null }),
+ );
+ expect(restore.where).toHaveBeenCalledWith(
+ expect.arrayContaining([
+ {
+ left: "videoUploads.updatedAt",
+ right: claim.set.mock.calls[0]?.[0].updatedAt,
+ },
+ ]),
+ );
+ expect(result.statuses).toEqual({ "retry-scheduled": 1 });
+ });
});
diff --git a/apps/web/__tests__/unit/video-processing.test.ts b/apps/web/__tests__/unit/video-processing.test.ts
index e9ae285a4..904cea30e 100644
--- a/apps/web/__tests__/unit/video-processing.test.ts
+++ b/apps/web/__tests__/unit/video-processing.test.ts
@@ -1,3 +1,4 @@
+import { MySqlDialect } from "drizzle-orm/mysql-core";
import { beforeEach, describe, expect, it, vi } from "vitest";
const updateWhereMock = vi.fn();
@@ -36,12 +37,16 @@ describe("video processing starts", () => {
vi.clearAllMocks();
});
- it("does not start a duplicate workflow when processing is already running", async () => {
+ it.each([
+ ["processing", "already-processing"],
+ ["generating_thumbnail", "already-processing"],
+ ["complete", "already-complete"],
+ ])("does not start a duplicate workflow during %s", async (phase, status) => {
updateWhereMock.mockResolvedValueOnce({ affectedRows: 0 });
selectWhereMock.mockResolvedValueOnce([
{
videoId: "video-123",
- phase: "processing",
+ phase,
rawFileKey: "user-123/video-123/raw-upload.webm",
},
]);
@@ -59,9 +64,13 @@ describe("video processing starts", () => {
processingMessage: "Starting video processing...",
startFailureMessage: "Video processing could not start.",
}),
- ).resolves.toBe("already-processing");
+ ).resolves.toBe(status);
expect(startMock).not.toHaveBeenCalled();
+ const condition = updateWhereMock.mock.calls[0]?.[0];
+ const query = new MySqlDialect().sqlToQuery(condition);
+ expect(query.params).toContain(phase);
+ expect(query.sql).toContain("`video_uploads`.`phase` <> ?");
});
it("starts the workflow after claiming the upload row", async () => {
@@ -131,6 +140,10 @@ describe("video processing starts", () => {
).resolves.toBe("started");
expect(startMock).toHaveBeenCalledTimes(1);
+ const condition = updateWhereMock.mock.calls[0]?.[0];
+ expect(new MySqlDialect().sqlToQuery(condition).params).toEqual([
+ "video-123",
+ ]);
});
it("marks the upload as errored when workflow start fails", async () => {
diff --git a/apps/web/__tests__/unit/video-recording-software-page.test.ts b/apps/web/__tests__/unit/video-recording-software-page.test.ts
index 4741fca8b..b74497c90 100644
--- a/apps/web/__tests__/unit/video-recording-software-page.test.ts
+++ b/apps/web/__tests__/unit/video-recording-software-page.test.ts
@@ -129,7 +129,7 @@ describe("VideoRecordingSoftwarePage FAQ schema", () => {
{
question: "Is Cap open-source video recording software?",
answer:
- "Yes. Cap is fully open-source and MIT-licensed. The complete codebase is publicly available on GitHub.",
+ "Yes. Most Cap code is under AGPLv3, with specified Rust crates under MIT. The source is available on GitHub.",
},
];
diff --git a/apps/web/__tests__/unit/video-status.test.ts b/apps/web/__tests__/unit/video-status.test.ts
new file mode 100644
index 000000000..ee8a08589
--- /dev/null
+++ b/apps/web/__tests__/unit/video-status.test.ts
@@ -0,0 +1,115 @@
+import { Video } from "@cap/web-domain";
+import { QueryClient } from "@tanstack/react-query";
+import { Effect } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ rows: [] as Record<string, unknown>[],
+ allowed: true,
+ read: vi.fn(),
+ transcribe: vi.fn(),
+ generate: vi.fn(),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({
+ from: () => ({
+ where: async () => {
+ mocks.read();
+ return mocks.rows;
+ },
+ }),
+ }),
+ }),
+}));
+vi.mock("@cap/env", () => ({ serverEnv: () => ({}) }));
+vi.mock("@cap/web-backend", async () => {
+ const { Effect } = await import("effect");
+ const { Policy } = await import("@cap/web-domain");
+ class VideosPolicy extends Effect.Service<VideosPolicy>()("VideosPolicy", {
+ sync: () => ({
+ canView: () =>
+ mocks.allowed
+ ? Effect.void
+ : Effect.fail(new Policy.PolicyDeniedError()),
+ }),
+ }) {}
+ return {
+ VideosPolicy,
+ provideOptionalAuth: Effect.provide(VideosPolicy.Default),
+ };
+});
+vi.mock("@/lib/server", () => ({ runPromiseExit: Effect.runPromiseExit }));
+vi.mock("@/lib/ai/provider", () => ({ isAiConfigured: () => false }));
+vi.mock("@/lib/desktop-segments-finalization", () => ({
+ isRetryableDesktopSegmentsFinalizationError: () => false,
+ queueDesktopSegmentsFinalization: vi.fn(),
+}));
+vi.mock("@/lib/generate-ai", () => ({ startAiGeneration: mocks.generate }));
+vi.mock("@/lib/transcribe", () => ({ transcribeVideo: mocks.transcribe }));
+vi.mock("@/utils/flags", () => ({ isAiGenerationEnabled: () => false }));
+
+const { getVideoStatus } = await import("@/actions/videos/get-status");
+const { videoStatusQueryOptions, VideoStatusNotFoundError } = await import(
+ "@/lib/video-status-query"
+);
+const videoId = Video.VideoId.make("missing-video");
+
+beforeEach(() => {
+ mocks.rows = [];
+ mocks.allowed = true;
+});
+
+describe("video status", () => {
+ it("returns an expected missing result without triggering processing", async () => {
+ await expect(getVideoStatus(videoId)).resolves.toEqual({
+ success: false,
+ reason: "not_found",
+ });
+ expect(mocks.transcribe).not.toHaveBeenCalled();
+ expect(mocks.generate).not.toHaveBeenCalled();
+ });
+
+ it("keeps access denial separate from a missing video", async () => {
+ mocks.allowed = false;
+ await expect(getVideoStatus(videoId)).resolves.toEqual({ success: false });
+ expect(mocks.read).not.toHaveBeenCalled();
+ });
+
+ it("preserves metadata for an available video", async () => {
+ mocks.rows = [{ name: "Recording", transcriptionStatus: "COMPLETE" }];
+ await expect(getVideoStatus(videoId)).resolves.toMatchObject({
+ name: "Recording",
+ transcriptionStatus: "COMPLETE",
+ });
+ });
+
+ it("does not retry missing videos even when initial data is cached", async () => {
+ const client = new QueryClient();
+ const options = videoStatusQueryOptions(videoId);
+ client.setQueryData(options.queryKey, { name: "Old recording" });
+ await expect(
+ client.fetchQuery({ ...options, retryDelay: 0 }),
+ ).rejects.toBeInstanceOf(VideoStatusNotFoundError);
+ expect(mocks.read).toHaveBeenCalledTimes(1);
+ client.clear();
+ });
+
+ it("still retries other failures", async () => {
+ const client = new QueryClient();
+ let attempts = 0;
+ const result = await client.fetchQuery({
+ ...videoStatusQueryOptions(videoId),
+ retryDelay: 0,
+ queryFn: async () => {
+ attempts++;
+ if (attempts < 3) throw new TypeError("Failed to fetch");
+ return "recovered";
+ },
+ });
+ expect(result).toBe("recovered");
+ expect(attempts).toBe(3);
+ client.clear();
+ });
+});
diff --git a/apps/web/__tests__/unit/video-storage-key.test.ts b/apps/web/__tests__/unit/video-storage-key.test.ts
new file mode 100644
index 000000000..04d809ce4
--- /dev/null
+++ b/apps/web/__tests__/unit/video-storage-key.test.ts
@@ -0,0 +1,306 @@
+import { Video } from "@cap/web-domain";
+import { NextRequest } from "next/server";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { isVideoStorageKey } from "@/lib/video-storage-key";
+
+const mocks = vi.hoisted(() => ({
+ db: vi.fn(),
+ getCurrentUser: vi.fn(),
+ getAccessForVideo: vi.fn(),
+ headObject: vi.fn(),
+ startVideoProcessingWorkflow: vi.fn(),
+ selectWhere: vi.fn(),
+ updateSet: vi.fn(),
+ lockedFor: vi.fn(),
+ transaction: vi.fn(),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: mocks.db,
+}));
+
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: mocks.getCurrentUser,
+}));
+
+vi.mock("@cap/database/schema", () => ({
+ videos: { id: "videos.id" },
+}));
+
+vi.mock("@cap/web-backend", () => ({
+ Storage: {
+ getAccessForVideo: mocks.getAccessForVideo,
+ },
+}));
+
+vi.mock("drizzle-orm", () => ({
+ eq: (field: unknown, value: unknown) => ({ field, value }),
+}));
+
+vi.mock("@/lib/server", () => ({
+ runPromise: vi.fn(),
+}));
+
+vi.mock("@/lib/video-storage", () => ({
+ decodeStorageVideo: (video: unknown) => video,
+}));
+
+vi.mock("@/lib/video-processing", () => ({
+ startVideoProcessingWorkflow: mocks.startVideoProcessingWorkflow,
+}));
+
+const ownerId = "user_owner1";
+const videoId = Video.VideoId.make("video_abc123");
+const video = { id: videoId, ownerId };
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.getCurrentUser.mockResolvedValue({ id: ownerId });
+ mocks.headObject.mockReturnValue({
+ pipe: () => Promise.resolve({ ContentLength: 1024 }),
+ });
+ mocks.getAccessForVideo.mockReturnValue({
+ pipe: () => Promise.resolve([{ headObject: mocks.headObject }]),
+ });
+ mocks.startVideoProcessingWorkflow.mockResolvedValue("started");
+ mocks.updateSet.mockReturnValue({ where: vi.fn().mockResolvedValue([]) });
+ mocks.lockedFor.mockResolvedValue([]);
+ mocks.transaction.mockImplementation((run: (tx: unknown) => unknown) =>
+ run({
+ select: () => ({
+ from: () => ({
+ where: () => ({
+ for: mocks.lockedFor,
+ }),
+ }),
+ }),
+ update: () => ({
+ set: mocks.updateSet,
+ }),
+ }),
+ );
+ mocks.db.mockReturnValue({
+ select: () => ({
+ from: () => ({
+ where: mocks.selectWhere,
+ }),
+ }),
+ update: () => ({
+ set: mocks.updateSet,
+ }),
+ transaction: mocks.transaction,
+ });
+});
+
+describe("isVideoStorageKey", () => {
+ it("accepts the upload paths the server issues for a video", () => {
+ for (const subpath of [
+ "raw-upload.mp4",
+ "raw-upload.webm",
+ "raw-upload.mov",
+ "agent-upload.mp4",
+ "verified-upload-AbC_123-xyz.mp4",
+ "result.mp4",
+ ".recording/sources/source-1.mp4",
+ ]) {
+ expect(isVideoStorageKey(video, `${ownerId}/${videoId}/${subpath}`)).toBe(
+ true,
+ );
+ }
+ });
+
+ it("rejects keys outside the video's upload path", () => {
+ for (const key of [
+ `other_user/${videoId}/raw-upload.mp4`,
+ `${ownerId}/other_video/raw-upload.mp4`,
+ `${ownerId}/${videoId}-extra/raw-upload.mp4`,
+ `${ownerId}/${videoId}/`,
+ `${ownerId}/${videoId}`,
+ "raw-upload.mp4",
+ "",
+ ]) {
+ expect(isVideoStorageKey(video, key)).toBe(false);
+ }
+ });
+
+ it("rejects relative segments, leading slashes and backslashes", () => {
+ for (const key of [
+ `${ownerId}/${videoId}/../../other_user/other_video/raw-upload.mp4`,
+ `${ownerId}/${videoId}/./raw-upload.mp4`,
+ `${ownerId}/${videoId}//raw-upload.mp4`,
+ `/${ownerId}/${videoId}/raw-upload.mp4`,
+ `${ownerId}/${videoId}/..\\raw-upload.mp4`,
+ `${ownerId}/${videoId}/sub\\raw-upload.mp4`,
+ ]) {
+ expect(isVideoStorageKey(video, key)).toBe(false);
+ }
+ expect(
+ isVideoStorageKey({ id: videoId, ownerId: "" }, `/${videoId}/raw.mp4`),
+ ).toBe(false);
+ });
+});
+
+describe("triggerVideoProcessing", () => {
+ const loadAction = async () =>
+ (await import("@/actions/video/trigger-processing")).triggerVideoProcessing;
+
+ it("starts processing for the video's own upload in the default bucket", async () => {
+ mocks.selectWhere.mockResolvedValue([{ ...video, bucket: null }]);
+ const triggerVideoProcessing = await loadAction();
+
+ await expect(
+ triggerVideoProcessing({
+ videoId,
+ rawFileKey: `${ownerId}/${videoId}/raw-upload.mp4`,
+ bucketId: null,
+ }),
+ ).resolves.toEqual({ success: true });
+ expect(mocks.headObject).toHaveBeenCalledWith(
+ `${ownerId}/${videoId}/raw-upload.mp4`,
+ );
+ expect(mocks.startVideoProcessingWorkflow).toHaveBeenCalledWith(
+ expect.objectContaining({
+ rawFileKey: `${ownerId}/${videoId}/raw-upload.mp4`,
+ bucketId: null,
+ }),
+ );
+ });
+
+ it("starts processing for the video's own upload in a custom bucket", async () => {
+ mocks.selectWhere.mockResolvedValue([{ ...video, bucket: "bucket_1" }]);
+ const triggerVideoProcessing = await loadAction();
+
+ await expect(
+ triggerVideoProcessing({
+ videoId,
+ rawFileKey: `${ownerId}/${videoId}/raw-upload.mp4`,
+ bucketId: "bucket_1",
+ }),
+ ).resolves.toEqual({ success: true });
+ expect(mocks.startVideoProcessingWorkflow).toHaveBeenCalledOnce();
+ });
+
+ it("rejects keys outside the video's upload path", async () => {
+ mocks.selectWhere.mockResolvedValue([{ ...video, bucket: null }]);
+ const triggerVideoProcessing = await loadAction();
+
+ for (const rawFileKey of [
+ "other_user/other_video/raw-upload.mp4",
+ `${ownerId}/other_video/raw-upload.mp4`,
+ `${ownerId}/${videoId}/../../other_user/other_video/raw-upload.mp4`,
+ `/${ownerId}/${videoId}/raw-upload.mp4`,
+ ]) {
+ await expect(
+ triggerVideoProcessing({ videoId, rawFileKey, bucketId: null }),
+ ).rejects.toThrow("Upload file does not belong to this video");
+ }
+ expect(mocks.headObject).not.toHaveBeenCalled();
+ expect(mocks.startVideoProcessingWorkflow).not.toHaveBeenCalled();
+ });
+
+ it("rejects a bucket that does not match the video", async () => {
+ const triggerVideoProcessing = await loadAction();
+ const rawFileKey = `${ownerId}/${videoId}/raw-upload.mp4`;
+
+ mocks.selectWhere.mockResolvedValue([{ ...video, bucket: null }]);
+ await expect(
+ triggerVideoProcessing({ videoId, rawFileKey, bucketId: "bucket_1" }),
+ ).rejects.toThrow("Upload bucket does not match this video");
+
+ mocks.selectWhere.mockResolvedValue([{ ...video, bucket: "bucket_1" }]);
+ await expect(
+ triggerVideoProcessing({ videoId, rawFileKey, bucketId: null }),
+ ).rejects.toThrow("Upload bucket does not match this video");
+ await expect(
+ triggerVideoProcessing({ videoId, rawFileKey, bucketId: "bucket_2" }),
+ ).rejects.toThrow("Upload bucket does not match this video");
+
+ expect(mocks.headObject).not.toHaveBeenCalled();
+ expect(mocks.startVideoProcessingWorkflow).not.toHaveBeenCalled();
+ });
+});
+
+describe("video metadata route", () => {
+ const put = async (metadata: unknown) => {
+ const { PUT } = await import("@/app/api/video/metadata/route");
+ return PUT(
+ new NextRequest("http://localhost:3000/api/video/metadata", {
+ method: "PUT",
+ body: JSON.stringify({ videoId, metadata }),
+ }),
+ );
+ };
+
+ it("keeps the stored agent upload state when updating metadata", async () => {
+ const agentUpload = {
+ state: "accepted",
+ rawFileKey: `${ownerId}/${videoId}/verified-upload-abc.mp4`,
+ };
+ mocks.selectWhere.mockResolvedValue([
+ { ...video, metadata: { sourceName: "a.mp4", agentUpload } },
+ ]);
+ mocks.lockedFor.mockResolvedValue([
+ { metadata: { sourceName: "a.mp4", agentUpload } },
+ ]);
+
+ const response = await put({
+ sourceName: "b.mp4",
+ agentUpload: { state: "accepted", rawFileKey: "other/key.mp4" },
+ });
+
+ expect(response.status).toBe(200);
+ expect(mocks.updateSet).toHaveBeenCalledWith({
+ metadata: { sourceName: "b.mp4", agentUpload },
+ });
+ });
+
+ it("keeps an agent upload accepted after the initial read when metadata is written", async () => {
+ const accepted = {
+ state: "accepted",
+ rawFileKey: `${ownerId}/${videoId}/verified-upload-abc.mp4`,
+ };
+ mocks.selectWhere.mockResolvedValue([
+ {
+ ...video,
+ metadata: { sourceName: "a.mp4", agentUpload: { state: "pending" } },
+ },
+ ]);
+ mocks.lockedFor.mockResolvedValue([
+ { metadata: { sourceName: "a.mp4", agentUpload: accepted } },
+ ]);
+
+ const response = await put({ sourceName: "b.mp4" });
+
+ expect(response.status).toBe(200);
+ expect(mocks.transaction).toHaveBeenCalledOnce();
+ expect(mocks.lockedFor).toHaveBeenCalledWith("update");
+ expect(mocks.updateSet).toHaveBeenCalledWith({
+ metadata: { sourceName: "b.mp4", agentUpload: accepted },
+ });
+ });
+
+ it("does not add an agent upload state from the request", async () => {
+ mocks.selectWhere.mockResolvedValue([{ ...video, metadata: null }]);
+ mocks.lockedFor.mockResolvedValue([{ metadata: null }]);
+
+ const response = await put({
+ customCreatedAt: "2026-01-01T00:00:00.000Z",
+ agentUpload: { state: "accepted", rawFileKey: "other/key.mp4" },
+ });
+
+ expect(response.status).toBe(200);
+ expect(mocks.updateSet).toHaveBeenCalledWith({
+ metadata: { customCreatedAt: "2026-01-01T00:00:00.000Z" },
+ });
+ });
+
+ it("rejects metadata that is not an object", async () => {
+ mocks.selectWhere.mockResolvedValue([{ ...video, metadata: null }]);
+
+ const response = await put(["not", "an", "object"]);
+
+ expect(response.status).toBe(400);
+ expect(mocks.updateSet).not.toHaveBeenCalled();
+ expect(mocks.transaction).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/web-editor-playback.test.ts b/apps/web/__tests__/unit/web-editor-playback.test.ts
new file mode 100644
index 000000000..f2699ede0
--- /dev/null
+++ b/apps/web/__tests__/unit/web-editor-playback.test.ts
@@ -0,0 +1,192 @@
+// @vitest-environment jsdom
+
+import { act, createElement, type Ref } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { EditVideoClient } from "@/app/s/[videoId]/edit/EditVideoClient";
+import * as timelineDrafts from "@/lib/video-edit-drafts";
+import { createTimelineState, deleteTimelineRanges } from "@/lib/video-edits";
+
+vi.mock("next/navigation", () => ({
+ useRouter: () => ({ push: vi.fn(), refresh: vi.fn() }),
+}));
+vi.mock("@/actions/videos/save-edits", () => ({
+ saveVideoEdits: vi.fn(),
+ restoreVideoToOriginal: vi.fn(),
+}));
+vi.mock("@cap/ui", () => ({
+ Button: () => null,
+ Dialog: () => null,
+ DialogContent: () => null,
+ DialogDescription: () => null,
+ DialogFooter: () => null,
+ DialogHeader: () => null,
+ DialogTitle: () => null,
+}));
+vi.mock("@/app/s/[videoId]/_components/CapVideoPlayer", () => ({
+ CapVideoPlayer: ({ videoRef }: { videoRef: Ref<HTMLVideoElement> }) =>
+ createElement("video", { ref: videoRef }),
+}));
+vi.mock("@/app/s/[videoId]/_components/VideoDownloadMenu", () => ({
+ VideoDownloadMenu: () => null,
+}));
+vi.mock("@/app/s/[videoId]/edit/TranscriptSidebar", () => ({
+ TranscriptSidebar: () => null,
+}));
+
+describe("web editor preview playback", () => {
+ let root: Root;
+ let container: HTMLDivElement;
+ let video: HTMLVideoElement;
+ let playing: boolean;
+ let storage: timelineDrafts.TimelineDraftStorage;
+ let frames: Map<number, FrameRequestCallback>;
+ let nextFrameId: number;
+
+ beforeEach(() => {
+ vi.useFakeTimers();
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ vi.stubGlobal(
+ "ResizeObserver",
+ class {
+ observe() {}
+ disconnect() {}
+ },
+ );
+ frames = new Map();
+ nextFrameId = 0;
+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
+ const id = ++nextFrameId;
+ frames.set(id, callback);
+ return id;
+ });
+ vi.stubGlobal("cancelAnimationFrame", (id: number) => frames.delete(id));
+ vi.spyOn(HTMLMediaElement.prototype, "pause").mockImplementation(() => {
+ playing = false;
+ });
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+ playing = false;
+ const values = new Map<string, string>();
+ storage = {
+ getItem: (key) => values.get(key) ?? null,
+ setItem: (key, value) => {
+ values.set(key, value);
+ },
+ removeItem: (key) => {
+ values.delete(key);
+ },
+ };
+ vi.spyOn(timelineDrafts, "getTimelineDraftStorage").mockReturnValue(
+ storage,
+ );
+ });
+
+ afterEach(async () => {
+ await act(async () => root.unmount());
+ container.remove();
+ vi.useRealTimers();
+ vi.unstubAllGlobals();
+ });
+
+ async function mount(cutEnd: number, muted = false) {
+ const videoId = "video-1";
+ timelineDrafts.writeTimelineDraft(
+ storage,
+ timelineDrafts.getTimelineDraftKey(videoId),
+ 10,
+ deleteTimelineRanges(createTimelineState(10), [
+ { start: 2, end: cutEnd },
+ ]),
+ );
+ await act(async () => {
+ root.render(
+ createElement(EditVideoClient, {
+ video: {
+ id: videoId as Parameters<typeof EditVideoClient>[0]["video"]["id"],
+ name: "Recording",
+ ownerId: "owner-1",
+ duration: 10,
+ width: 1920,
+ height: 1080,
+ transcriptionStatus: null,
+ },
+ hasExistingEdits: false,
+ }),
+ );
+ });
+ const element = container.querySelector("video");
+ if (!element) throw new Error("Expected editor preview");
+ video = element;
+ video.muted = muted;
+ Object.defineProperty(video, "paused", { get: () => !playing });
+ playing = true;
+ await act(async () => {
+ video.dispatchEvent(new Event("play"));
+ });
+ }
+
+ async function advanceTo(time: number, trigger: "frame" | "timeupdate") {
+ video.currentTime = time;
+ await act(async () => {
+ if (trigger === "timeupdate") {
+ video.dispatchEvent(new Event("timeupdate"));
+ return;
+ }
+ for (const [id, callback] of [...frames]) {
+ if (frames.delete(id)) callback(time * 1000);
+ }
+ });
+ }
+
+ it.each(["frame", "timeupdate"] as const)(
+ "restores audio after a short cut using %s updates",
+ async (trigger) => {
+ await mount(2.06);
+ await advanceTo(1.98, trigger);
+ expect(video.muted).toBe(true);
+
+ await advanceTo(2.04, trigger);
+ expect(video.muted).toBe(true);
+ await advanceTo(2.07, trigger);
+ expect(video.muted).toBe(false);
+ await advanceTo(3, trigger);
+ expect(video.currentTime).toBe(3);
+ expect(video.muted).toBe(false);
+ expect(playing).toBe(true);
+ },
+ );
+
+ it.each([false, true])(
+ "restores the prior mute setting %s after skipping a long cut",
+ async (muted) => {
+ await mount(5, muted);
+ await advanceTo(1.98, "frame");
+ await advanceTo(2.04, "frame");
+ expect(video.currentTime).toBe(5);
+ await act(async () => {
+ video.dispatchEvent(new Event("seeked"));
+ });
+ expect(video.muted).toBe(muted);
+ },
+ );
+
+ it("keeps audio muted while the cut seek is still pending", async () => {
+ await mount(2.06);
+ await advanceTo(1.98, "frame");
+ await advanceTo(2.04, "frame");
+ Object.defineProperty(video, "seeking", { value: true });
+ await advanceTo(2.06, "frame");
+ expect(video.muted).toBe(true);
+ });
+
+ it("restores audio when a delayed frame lands beyond the cut", async () => {
+ await mount(2.06);
+ await advanceTo(1.98, "frame");
+ expect(video.muted).toBe(true);
+ await advanceTo(2.08, "frame");
+ expect(video.currentTime).toBe(2.08);
+ expect(video.muted).toBe(false);
+ });
+});
diff --git a/apps/web/actions/caps/share.ts b/apps/web/actions/caps/share.ts
index c6ae3969f..e6e7a7b0e 100644
--- a/apps/web/actions/caps/share.ts
+++ b/apps/web/actions/caps/share.ts
@@ -14,6 +14,7 @@ import {
import type { Organisation, Space, Video } from "@cap/web-domain";
import { and, eq, inArray } from "drizzle-orm";
import { revalidatePath } from "next/cache";
+import { getSpaceAccess } from "@/actions/organization/space-authorization";
interface ShareCapParams {
capId: Video.VideoId;
@@ -69,6 +70,21 @@ export async function shareCap({
),
);
+ const currentSpaceVideos = await db()
+ .select()
+ .from(spaceVideos)
+ .where(eq(spaceVideos.videoId, capId));
+ const existingSpaceIds = new Set(currentSpaceVideos.map((s) => s.spaceId));
+
+ for (const space of spacesData) {
+ if (space.privacy === "Public" || existingSpaceIds.has(space.id))
+ continue;
+ const access = await getSpaceAccess(user.id, space.id);
+ if (!access || (!access.canManage && !access.spaceRole)) {
+ return { success: false, error: "Unauthorized" };
+ }
+ }
+
const organizationIds = directOrgIds;
const currentSharedOrganizations = await db()
@@ -107,11 +123,6 @@ export async function shareCap({
const spacesIds = spacesData.map((space) => space.id);
- const currentSpaceVideos = await db()
- .select()
- .from(spaceVideos)
- .where(eq(spaceVideos.videoId, capId));
-
const spaceIdsToRemove = currentSpaceVideos
.filter((s) => !spacesIds.includes(s.spaceId))
.map((s) => s.spaceId);
@@ -126,7 +137,6 @@ export async function shareCap({
);
}
- const existingSpaceIds = new Set(currentSpaceVideos.map((s) => s.spaceId));
const newSpaceEntries = spacesIds
.filter((id) => !existingSpaceIds.has(id))
.map((spaceId) => ({
diff --git a/apps/web/actions/folders/add-videos.ts b/apps/web/actions/folders/add-videos.ts
index 817da6191..9c730bda0 100644
--- a/apps/web/actions/folders/add-videos.ts
+++ b/apps/web/actions/folders/add-videos.ts
@@ -12,6 +12,8 @@ import {
import type { Folder, Space, Video } from "@cap/web-domain";
import { and, eq, inArray } from "drizzle-orm";
import { revalidatePath } from "next/cache";
+import { getOrganizationAccess } from "@/actions/organization/authorization";
+import { getSpaceAccess } from "@/actions/organization/space-authorization";
export async function addVideosToFolder(
folderId: Folder.FolderId,
@@ -30,14 +32,38 @@ export async function addVideosToFolder(
}
const [folder] = await db()
- .select({ id: folders.id, spaceId: folders.spaceId })
+ .select({
+ id: folders.id,
+ organizationId: folders.organizationId,
+ spaceId: folders.spaceId,
+ })
.from(folders)
- .where(eq(folders.id, folderId));
+ .where(and(eq(folders.id, folderId), eq(folders.spaceId, spaceId)));
if (!folder) {
throw new Error("Folder not found");
}
+ if (spaceId === user.activeOrganizationId) {
+ const access = await getOrganizationAccess(
+ user.id,
+ user.activeOrganizationId,
+ );
+ if (!access || folder.organizationId !== user.activeOrganizationId) {
+ throw new Error("Folder not found");
+ }
+ } else {
+ const access = await getSpaceAccess(user.id, spaceId);
+ if (
+ !access ||
+ access.organizationId !== folder.organizationId ||
+ !access.organizationRole ||
+ (!access.canManage && !access.spaceRole)
+ ) {
+ throw new Error("Folder not found");
+ }
+ }
+
const userVideos = await db()
.select({ id: videos.id })
.from(videos)
@@ -50,13 +76,29 @@ export async function addVideosToFolder(
}
const isAllSpacesEntry = spaceId === user.activeOrganizationId;
+ const sharesWithOrganization = isAllSpacesEntry && !!folder.spaceId;
+ const sharingTable = sharesWithOrganization ? sharedVideos : spaceVideos;
+ const existingShares = await db()
+ .select({ videoId: sharingTable.videoId })
+ .from(sharingTable)
+ .where(
+ and(
+ sharesWithOrganization
+ ? eq(sharedVideos.organizationId, user.activeOrganizationId)
+ : eq(spaceVideos.spaceId, spaceId),
+ inArray(sharingTable.videoId, validVideoIds),
+ ),
+ );
+ const existingVideoIds = new Set(
+ existingShares.map((share) => share.videoId),
+ );
+ const newVideoIds = validVideoIds.filter((id) => !existingVideoIds.has(id));
- //if we're adding videos to a folder from Caps page, then insert the videos into the folder
- if (isAllSpacesEntry && folder.spaceId) {
+ if (newVideoIds.length > 0 && sharesWithOrganization) {
await db()
.insert(sharedVideos)
.values(
- validVideoIds.map((videoId) => {
+ newVideoIds.map((videoId) => {
const id = nanoId();
return {
id,
@@ -67,11 +109,11 @@ export async function addVideosToFolder(
};
}),
);
- } else {
+ } else if (newVideoIds.length > 0) {
await db()
.insert(spaceVideos)
.values(
- validVideoIds.map((videoId) => {
+ newVideoIds.map((videoId) => {
const id = nanoId();
return {
id,
diff --git a/apps/web/actions/folders/folder-access.ts b/apps/web/actions/folders/folder-access.ts
new file mode 100644
index 000000000..ce33352fc
--- /dev/null
+++ b/apps/web/actions/folders/folder-access.ts
@@ -0,0 +1,27 @@
+import type { Organisation, Space, User } from "@cap/web-domain";
+import { getOrganizationAccess } from "@/actions/organization/authorization";
+import { getSpaceAccess } from "@/actions/organization/space-authorization";
+
+export async function canViewFolder(
+ userId: User.UserId,
+ folder: {
+ organizationId: Organisation.OrganisationId;
+ spaceId: Space.SpaceIdOrOrganisationId | null;
+ createdById: User.UserId;
+ },
+) {
+ if (folder.spaceId === null) return folder.createdById === userId;
+
+ if (folder.spaceId === folder.organizationId) {
+ return (
+ (await getOrganizationAccess(userId, folder.organizationId)) !== null
+ );
+ }
+
+ const access = await getSpaceAccess(userId, folder.spaceId);
+ return Boolean(
+ access &&
+ access.organizationId === folder.organizationId &&
+ (access.organizationRole || access.spaceRole),
+ );
+}
diff --git a/apps/web/actions/folders/get-folder-videos.ts b/apps/web/actions/folders/get-folder-videos.ts
index 704f6aeb2..edcce6fb9 100644
--- a/apps/web/actions/folders/get-folder-videos.ts
+++ b/apps/web/actions/folders/get-folder-videos.ts
@@ -2,9 +2,10 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
-import { sharedVideos, spaceVideos } from "@cap/database/schema";
+import { folders, sharedVideos, spaceVideos } from "@cap/database/schema";
import type { Folder, Space, Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
+import { canViewFolder } from "@/actions/folders/folder-access";
export async function getFolderVideoIds(
folderId: Folder.FolderId,
@@ -21,6 +22,19 @@ export async function getFolderVideoIds(
throw new Error("Folder ID is required");
}
+ const [folder] = await db()
+ .select({
+ organizationId: folders.organizationId,
+ spaceId: folders.spaceId,
+ createdById: folders.createdById,
+ })
+ .from(folders)
+ .where(eq(folders.id, folderId));
+
+ if (!folder || !(await canViewFolder(user.id, folder))) {
+ throw new Error("Folder not found");
+ }
+
const isAllSpacesEntry = user.activeOrganizationId === spaceId;
const rows = isAllSpacesEntry
diff --git a/apps/web/actions/loom-concierge.tsx b/apps/web/actions/loom-concierge.tsx
index 5b9452dd1..6daa9d426 100644
--- a/apps/web/actions/loom-concierge.tsx
+++ b/apps/web/actions/loom-concierge.tsx
@@ -8,15 +8,17 @@ import {
} from "@cap/database/emails/loom-migration";
import { nanoId } from "@cap/database/helpers";
import {
+ importedVideos,
loomMigrationImports,
loomMigrationRequests,
organizations,
users,
+ videos,
videoUploads,
} from "@cap/database/schema";
import { serverEnv } from "@cap/env";
import type { Organisation } from "@cap/web-domain";
-import { and, eq, inArray, isNotNull } from "drizzle-orm";
+import { and, eq, isNotNull } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import {
getCustomerMigrationRequests,
@@ -26,11 +28,13 @@ import {
requireProCustomerMigrationAccess,
toLoomMigrationResult,
} from "@/lib/loom-concierge";
+import { reconcileMissingLoomMigrationImports } from "@/lib/loom-migration-inventory";
import {
LOOM_MIGRATION_STATUS_LABELS,
LoomMigrationError,
type LoomMigrationStatus,
normalizeMigrationText,
+ validateLoomMigrationInventory,
validateOperatorMigrationUpdate,
} from "@/lib/loom-migration-state";
import { isOrganizationOwnerPro } from "@/lib/org-pro";
@@ -246,6 +250,17 @@ export async function updateLoomMigrationStatus(
return toLoomMigrationResult(() => saveMigrationStatus(input));
}
+export async function reconcileLoomMigrationInventory(input: {
+ requestId: string;
+ verified: boolean;
+}) {
+ return toLoomMigrationResult(async () => {
+ const result = await reconcileMissingLoomMigrationImports(input);
+ refreshMigrationPages();
+ return result;
+ });
+}
+
async function saveMigrationStatus({
requestId,
nextStatus,
@@ -304,31 +319,31 @@ async function saveMigrationStatus({
);
}
if (nextStatus === "completed") {
- const [unfinishedImport] = await tx
- .select({ videoId: videoUploads.videoId })
+ const imports = await tx
+ .select({
+ videoId: videos.id,
+ videoOrganizationId: videos.orgId,
+ mappingId: importedVideos.id,
+ mappingOrganizationId: importedVideos.orgId,
+ source: importedVideos.source,
+ phase: videoUploads.phase,
+ })
.from(loomMigrationImports)
- .innerJoin(
+ .leftJoin(videos, eq(videos.id, loomMigrationImports.videoId))
+ .leftJoin(
+ importedVideos,
+ eq(importedVideos.id, loomMigrationImports.videoId),
+ )
+ .leftJoin(
videoUploads,
eq(videoUploads.videoId, loomMigrationImports.videoId),
)
- .where(
- and(
- eq(loomMigrationImports.requestId, requestId),
- inArray(videoUploads.phase, [
- "uploading",
- "processing",
- "generating_thumbnail",
- "error",
- ]),
- ),
- )
- .limit(1)
+ .where(eq(loomMigrationImports.requestId, requestId))
.for("share");
- if (unfinishedImport) {
- throw new LoomMigrationError(
- "Loom imports are still processing or have errors. Resolve them before completion.",
- );
- }
+ validateLoomMigrationInventory({
+ organizationId: request.organizationId,
+ imports,
+ });
}
await tx
.update(loomMigrationRequests)
diff --git a/apps/web/actions/loom.ts b/apps/web/actions/loom.ts
index 96dca601f..2fd4d4890 100644
--- a/apps/web/actions/loom.ts
+++ b/apps/web/actions/loom.ts
@@ -1,6 +1,5 @@
"use server";
-import { randomUUID } from "node:crypto";
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoId } from "@cap/database/helpers";
@@ -34,7 +33,7 @@ import {
import { and, asc, eq, isNotNull, isNull, sql } from "drizzle-orm";
import { Effect, Option, Schedule } from "effect";
import { revalidatePath } from "next/cache";
-import { start } from "workflow/api";
+import { getRun } from "workflow/api";
import {
getOrganizationAccess,
requireOrganizationAccess,
@@ -48,7 +47,23 @@ import {
reserveConciergeImport,
toLoomMigrationResult,
} from "@/lib/loom-concierge";
+import {
+ getLoomDownloadUrl,
+ LoomDownloadTemporaryError,
+} from "@/lib/loom-download-url";
+import {
+ LOOM_FILE_SINGLE_UPLOAD_MAX_BYTES,
+ startLoomFileProcessingWorkflow,
+} from "@/lib/loom-file-processing";
import type { LoomImportDestination } from "@/lib/loom-import-destination";
+import { LOOM_IMPORT_MAX_BYTES } from "@/lib/loom-import-limits";
+import {
+ getLoomImportRunState,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE,
+ LoomImportStartError,
+ restoreLoomImportStartError,
+ startLoomImportWorkflow,
+} from "@/lib/loom-import-start";
import {
LoomMigrationError,
type LoomMigrationResult,
@@ -59,11 +74,6 @@ import { canManageOrganizationSettings } from "@/lib/permissions/roles";
import { runPromise } from "@/lib/server";
import { startVideoProcessingWorkflow } from "@/lib/video-processing";
import { decodeStorageVideo } from "@/lib/video-storage";
-import { importLoomVideoWorkflow } from "@/workflows/import-loom-video";
-
-interface LoomUrlResponse {
- url?: string;
-}
type LoomDownloadMode = "direct-download" | "browser-conversion";
@@ -80,10 +90,13 @@ interface LoomDownloadResult {
error?: string;
}
+export type LoomImportErrorCode = "loom_temporarily_unavailable";
+
export interface LoomImportResult {
success: boolean;
videoId?: Video.VideoId;
error?: string;
+ errorCode?: LoomImportErrorCode;
}
export interface LoomCsvImportRow {
@@ -100,6 +113,7 @@ export interface LoomCsvImportRowResult {
success: boolean;
videoId?: Video.VideoId;
error?: string;
+ errorCode?: LoomImportErrorCode;
}
export interface LoomCsvImportResult {
@@ -112,6 +126,7 @@ export interface LoomCsvImportResult {
const MAX_LOOM_CSV_ROWS = 500;
const MAX_LOOM_SPACE_NAME_LENGTH = 255;
+const MAX_LOOM_VIDEO_NAME_LENGTH = 255;
const LOOM_CSV_LIMIT_ERROR = `CSV imports are limited to ${MAX_LOOM_CSV_ROWS} rows at a time. Contact support to raise this limit.`;
const LOOM_CSV_PERMISSION_ERROR =
"Only organization admins and owners can import Loom videos from a CSV.";
@@ -140,47 +155,6 @@ function extractLoomVideoId(url: string): string | null {
}
}
-async function fetchLoomEndpoint(
- videoId: string,
- endpoint: "transcoded-url" | "raw-url",
- includeBody = true,
-): Promise<string | null> {
- try {
- const options: RequestInit = { method: "POST" };
- if (includeBody) {
- options.headers = {
- "Content-Type": "application/json",
- Accept: "application/json",
- };
- options.body = JSON.stringify({
- anonID: randomUUID(),
- deviceID: null,
- force_original: false,
- password: null,
- });
- }
-
- const response = await fetch(
- `https://www.loom.com/api/campaigns/sessions/${encodeURIComponent(videoId)}/${endpoint}`,
- options,
- );
-
- if (!response.ok || response.status === 204) {
- return null;
- }
-
- const text = await response.text();
- if (!text.trim()) {
- return null;
- }
-
- const data: LoomUrlResponse = JSON.parse(text);
- return data.url ?? null;
- } catch {
- return null;
- }
-}
-
async function fetchVideoName(videoId: string): Promise<string | null> {
try {
const response = await fetch("https://www.loom.com/graphql", {
@@ -212,41 +186,11 @@ async function fetchVideoName(videoId: string): Promise<string | null> {
}
}
-function isStreamingUrl(url: string): boolean {
- const path = (url.split("?")[0] ?? "").toLowerCase();
- return path.endsWith(".m3u8") || path.endsWith(".mpd");
-}
-
function isDirectMp4Url(url: string): boolean {
const path = (url.split("?")[0] ?? "").toLowerCase();
return path.endsWith(".mp4");
}
-async function getLoomDownloadUrl(loomVideoId: string): Promise<string | null> {
- const requestVariants: Array<{
- endpoint: "transcoded-url" | "raw-url";
- includeBody: boolean;
- }> = [
- { endpoint: "transcoded-url", includeBody: true },
- { endpoint: "raw-url", includeBody: true },
- { endpoint: "transcoded-url", includeBody: false },
- { endpoint: "raw-url", includeBody: false },
- ];
-
- let fallbackStreamingUrl: string | null = null;
-
- for (const { endpoint, includeBody } of requestVariants) {
- const url = await fetchLoomEndpoint(loomVideoId, endpoint, includeBody);
- if (!url) continue;
-
- if (!isStreamingUrl(url)) return url;
-
- if (!fallbackStreamingUrl) fallbackStreamingUrl = url;
- }
-
- return fallbackStreamingUrl;
-}
-
async function fetchLoomOEmbed(
loomVideoId: string,
): Promise<{ duration?: number; width?: number; height?: number } | null> {
@@ -312,15 +256,64 @@ export async function downloadLoomVideo(
height: oembedMeta?.height,
requiresProxy: false,
};
- } catch {
+ } catch (error) {
return {
success: false,
error:
- "An unexpected error occurred. Please try again or check your internet connection.",
+ error instanceof LoomDownloadTemporaryError
+ ? error.message
+ : "An unexpected error occurred. Please try again or check your internet connection.",
};
}
}
+type LoomImportTransaction = Parameters<
+ Parameters<ReturnType<typeof db>["transaction"]>[0]
+>[0];
+
+async function assertConciergeLease(
+ tx: LoomImportTransaction,
+ requestId: string,
+ leaseToken: string | undefined,
+) {
+ const [reservation] = await tx
+ .select({
+ activeOrganizationId: loomMigrationRequests.activeOrganizationId,
+ leaseToken: loomMigrationRequests.activeImportLeaseToken,
+ leaseUntil: loomMigrationRequests.activeImportLeaseUntil,
+ })
+ .from(loomMigrationRequests)
+ .where(eq(loomMigrationRequests.id, requestId))
+ .limit(1)
+ .for("update");
+ if (
+ !leaseToken ||
+ !reservation?.activeOrganizationId ||
+ reservation.leaseToken !== leaseToken ||
+ !reservation.leaseUntil ||
+ reservation.leaseUntil <= new Date()
+ ) {
+ throw new LoomMigrationError(
+ "The concierge import expired. Retry this video.",
+ );
+ }
+}
+
+async function recordConciergeImport(
+ tx: LoomImportTransaction,
+ videoId: Video.VideoId,
+ requestId: string,
+) {
+ await tx.insert(loomMigrationImports).values({ videoId, requestId });
+ await tx
+ .update(loomMigrationRequests)
+ .set({
+ queuedVideoCount: sql`${loomMigrationRequests.queuedVideoCount} + 1`,
+ updatedAt: new Date(),
+ })
+ .where(eq(loomMigrationRequests.id, requestId));
+}
+
async function importLoomVideoForOwner({
loomUrl,
orgId,
@@ -348,6 +341,12 @@ async function importLoomVideoForOwner({
const existing = await db()
.select({
videoId: videos.id,
+ ownerId: videos.ownerId,
+ bucketId: videos.bucket,
+ metadata: videos.metadata,
+ phase: videoUploads.phase,
+ rawFileKey: videoUploads.rawFileKey,
+ uploadUpdatedAt: videoUploads.updatedAt,
})
.from(importedVideos)
.leftJoin(
@@ -357,6 +356,7 @@ async function importLoomVideoForOwner({
eq(videos.orgId, importedVideos.orgId),
),
)
+ .leftJoin(videoUploads, eq(videoUploads.videoId, videos.id))
.where(
and(
eq(importedVideos.orgId, orgId),
@@ -365,7 +365,116 @@ async function importLoomVideoForOwner({
),
);
- if (existing.some((row) => row.videoId !== null)) {
+ const existingVideo = existing.find((row) => row.videoId !== null);
+ if (existingVideo?.videoId) {
+ if (
+ existingVideo.phase === "error" &&
+ existingVideo.ownerId === ownerId &&
+ existingVideo.uploadUpdatedAt
+ ) {
+ const runState = await getLoomImportRunState(
+ existingVideo.metadata?.loomImportRun,
+ );
+ if (runState === "unconfirmed") {
+ return { success: false, error: LOOM_IMPORT_CONFIRMATION_MESSAGE };
+ }
+ if (runState !== "inactive") {
+ return {
+ success: false,
+ error: "This Loom import is already restarting.",
+ };
+ }
+ const rawFileKey =
+ existingVideo.rawFileKey ??
+ `${ownerId}/${existingVideo.videoId}/raw-upload.mp4`;
+ const failedVideoId = existingVideo.videoId;
+ const failedUpdatedAt = existingVideo.uploadUpdatedAt;
+ const claimedAt = new Date(Math.floor(Date.now() / 1000) * 1000);
+ const claimOutcome = await db().transaction(async (tx) => {
+ let mappedRequestId: string | undefined;
+ if (migrationRequestId) {
+ await assertConciergeLease(
+ tx,
+ migrationRequestId,
+ migrationLeaseToken,
+ );
+ const [mapped] = await tx
+ .select({ requestId: loomMigrationImports.requestId })
+ .from(loomMigrationImports)
+ .where(eq(loomMigrationImports.videoId, failedVideoId))
+ .limit(1);
+ mappedRequestId = mapped?.requestId;
+ if (mappedRequestId && mappedRequestId !== migrationRequestId) {
+ return "foreign" as const;
+ }
+ }
+ const claim = await tx
+ .update(videoUploads)
+ .set({
+ phase: "processing",
+ processingProgress: 0,
+ processingMessage: "Restarting Loom import...",
+ processingError: null,
+ rawFileKey,
+ updatedAt: claimedAt,
+ })
+ .where(
+ and(
+ eq(videoUploads.videoId, failedVideoId),
+ eq(videoUploads.phase, "error"),
+ eq(videoUploads.updatedAt, failedUpdatedAt),
+ ),
+ );
+ const affectedRows = Array.isArray(claim)
+ ? (claim[0] as { affectedRows?: number } | undefined)?.affectedRows
+ : (claim as { affectedRows?: number }).affectedRows;
+ if (affectedRows !== 1) return "busy" as const;
+ if (migrationRequestId && !mappedRequestId) {
+ await recordConciergeImport(tx, failedVideoId, migrationRequestId);
+ }
+ return "claimed" as const;
+ });
+ if (claimOutcome === "foreign") {
+ return {
+ success: false,
+ error: "This Loom import belongs to another migration request.",
+ };
+ }
+ if (claimOutcome === "busy") {
+ return {
+ success: false,
+ error: "This Loom import is already restarting.",
+ };
+ }
+ try {
+ await startLoomImportWorkflow({
+ videoId: existingVideo.videoId,
+ userId: ownerId,
+ rawFileKey,
+ bucketId: existingVideo.bucketId,
+ loomVideoId,
+ reuseExistingRawUpload: true,
+ });
+ } catch (error) {
+ if (error instanceof LoomImportStartError && error.canRetry) {
+ await restoreLoomImportStartError(
+ existingVideo.videoId,
+ "processing",
+ claimedAt,
+ error.message,
+ );
+ }
+ return {
+ success: false,
+ error:
+ error instanceof Error
+ ? error.message
+ : "Loom import could not restart.",
+ };
+ }
+ revalidatePath("/dashboard/caps");
+ return { success: true, videoId: existingVideo.videoId };
+ }
return {
success: false,
error: "This Loom video has already been imported.",
@@ -384,7 +493,19 @@ async function importLoomVideoForOwner({
);
}
- const downloadUrl = await getLoomDownloadUrl(loomVideoId);
+ let downloadUrl: string | null;
+ try {
+ downloadUrl = await getLoomDownloadUrl(loomVideoId);
+ } catch (error) {
+ if (error instanceof LoomDownloadTemporaryError) {
+ return {
+ success: false,
+ error: error.message,
+ errorCode: "loom_temporarily_unavailable",
+ };
+ }
+ throw error;
+ }
if (!downloadUrl) {
return {
success: false,
@@ -420,33 +541,17 @@ async function importLoomVideoForOwner({
const writable = writableResult.value;
const videoId = Video.VideoId.make(nanoId());
- const name =
+ const name = Array.from(
videoName ||
- `Loom Import - ${new Date().toLocaleDateString("en-US", { day: "numeric", month: "long", year: "numeric" })}`;
+ `Loom Import - ${new Date().toLocaleDateString("en-US", { day: "numeric", month: "long", year: "numeric" })}`,
+ )
+ .slice(0, MAX_LOOM_VIDEO_NAME_LENGTH)
+ .join("");
+ const claimedAt = new Date(Math.floor(Date.now() / 1000) * 1000);
await db().transaction(async (tx) => {
if (migrationRequestId) {
- const [reservation] = await tx
- .select({
- activeOrganizationId: loomMigrationRequests.activeOrganizationId,
- leaseToken: loomMigrationRequests.activeImportLeaseToken,
- leaseUntil: loomMigrationRequests.activeImportLeaseUntil,
- })
- .from(loomMigrationRequests)
- .where(eq(loomMigrationRequests.id, migrationRequestId))
- .limit(1)
- .for("update");
- if (
- !migrationLeaseToken ||
- !reservation?.activeOrganizationId ||
- reservation.leaseToken !== migrationLeaseToken ||
- !reservation.leaseUntil ||
- reservation.leaseUntil <= new Date()
- ) {
- throw new LoomMigrationError(
- "The concierge import expired. Retry this video.",
- );
- }
+ await assertConciergeLease(tx, migrationRequestId, migrationLeaseToken);
}
await tx.insert(videos).values({
id: videoId,
@@ -468,6 +573,7 @@ async function importLoomVideoForOwner({
phase: "uploading",
processingProgress: 0,
processingMessage: "Importing from Loom...",
+ updatedAt: claimedAt,
});
await tx.insert(importedVideos).values({
@@ -477,17 +583,7 @@ async function importLoomVideoForOwner({
sourceId: loomVideoId,
});
if (migrationRequestId) {
- await tx.insert(loomMigrationImports).values({
- videoId,
- requestId: migrationRequestId,
- });
- await tx
- .update(loomMigrationRequests)
- .set({
- queuedVideoCount: sql`${loomMigrationRequests.queuedVideoCount} + 1`,
- updatedAt: new Date(),
- })
- .where(eq(loomMigrationRequests.id, migrationRequestId));
+ await recordConciergeImport(tx, videoId, migrationRequestId);
}
if (destination.spaceId === orgId) {
@@ -521,15 +617,31 @@ async function importLoomVideoForOwner({
.catch(() => {});
}
- await start(importLoomVideoWorkflow, [
- {
+ try {
+ await startLoomImportWorkflow({
videoId,
userId: ownerId,
rawFileKey,
bucketId: Option.getOrNull(writable.bucketId),
loomVideoId,
- },
- ]);
+ loomDownloadUrl: downloadUrl,
+ });
+ } catch (error) {
+ if (error instanceof LoomImportStartError && error.canRetry) {
+ await restoreLoomImportStartError(
+ videoId,
+ "uploading",
+ claimedAt,
+ error.message,
+ );
+ }
+ return {
+ success: false,
+ videoId,
+ error:
+ error instanceof Error ? error.message : "Loom import could not start.",
+ };
+ }
revalidatePath("/dashboard/caps");
if (destination.folderId) revalidatePath("/dashboard/folder/[id]", "page");
@@ -1005,6 +1117,7 @@ async function processLoomCsvRows({
success: result.success,
videoId: result.videoId,
error: result.error ?? spaceError,
+ ...(result.errorCode ? { errorCode: result.errorCode } : {}),
});
} catch {
results.push({
@@ -1121,6 +1234,108 @@ export async function createConciergeLoomFileUpload(
return toLoomMigrationResult(() => prepareConciergeLoomFileUpload(input));
}
+export async function resumeConciergeLoomFileUpload(input: {
+ requestId: string;
+ loomUrl: string;
+ userEmail: string;
+ confirmedPreviousProcessingStopped: boolean;
+}) {
+ return toLoomMigrationResult(async () => {
+ await requireMigrationOperator();
+ if (input.confirmedPreviousProcessingStopped !== true) {
+ throw new LoomMigrationError(
+ "Confirm the previous processing has stopped before resuming this upload.",
+ );
+ }
+ if (
+ typeof input.requestId !== "string" ||
+ !/^[A-Za-z0-9_-]{15}$/.test(input.requestId)
+ ) {
+ throw new LoomMigrationError("Invalid migration request.");
+ }
+ if (
+ typeof input.loomUrl !== "string" ||
+ typeof input.userEmail !== "string"
+ ) {
+ throw new LoomMigrationError("Enter a valid Loom URL and owner email.");
+ }
+ const loomVideoId = extractLoomVideoId(input.loomUrl.trim());
+ const ownerEmail = normalizeImportEmail(input.userEmail);
+ if (!loomVideoId || !isValidImportEmail(ownerEmail)) {
+ throw new LoomMigrationError("Enter a valid Loom URL and owner email.");
+ }
+ const [record] = await db()
+ .select({
+ videoId: videos.id,
+ ownerId: videos.ownerId,
+ ownerEmail: users.email,
+ organizationId: videos.orgId,
+ metadata: videos.metadata,
+ })
+ .from(loomMigrationRequests)
+ .innerJoin(
+ loomMigrationImports,
+ eq(loomMigrationImports.requestId, loomMigrationRequests.id),
+ )
+ .innerJoin(
+ importedVideos,
+ eq(importedVideos.id, loomMigrationImports.videoId),
+ )
+ .innerJoin(
+ videos,
+ and(
+ eq(videos.id, importedVideos.id),
+ eq(videos.orgId, importedVideos.orgId),
+ eq(videos.orgId, loomMigrationRequests.organizationId),
+ ),
+ )
+ .innerJoin(users, eq(users.id, videos.ownerId))
+ .innerJoin(organizations, eq(organizations.id, videos.orgId))
+ .where(
+ and(
+ eq(loomMigrationRequests.id, input.requestId),
+ isNotNull(loomMigrationRequests.activeOrganizationId),
+ isNull(organizations.tombstoneAt),
+ eq(importedVideos.source, "loom"),
+ eq(importedVideos.sourceId, loomVideoId),
+ ),
+ )
+ .limit(1);
+ if (!record || normalizeImportEmail(record.ownerEmail) !== ownerEmail) {
+ throw new LoomMigrationError(
+ "No existing Loom recording matches this request and owner.",
+ );
+ }
+ if (!(await isOrganizationOwnerPro(record.organizationId))) {
+ throw new LoomMigrationError("The destination workspace needs Cap Pro.");
+ }
+ const previousRun = record.metadata?.loomImportRun;
+ if (previousRun && previousRun.dispatch !== "rejected") {
+ let status: string | null;
+ try {
+ status = await getRun(previousRun.runId).status;
+ } catch {
+ status = null;
+ }
+ if (
+ status !== "completed" &&
+ status !== "failed" &&
+ status !== "cancelled"
+ ) {
+ throw new LoomMigrationError(
+ "The previous Loom workflow is active or could not be confirmed.",
+ );
+ }
+ }
+ return startConciergeLoomFileProcessing({
+ requestId: input.requestId,
+ videoId: record.videoId,
+ expectedOwnerId: record.ownerId,
+ maxBytes: LOOM_IMPORT_MAX_BYTES,
+ });
+ });
+}
+
async function prepareConciergeLoomFileUpload({
requestId,
loomUrl,
@@ -1424,9 +1639,13 @@ export async function finishConciergeLoomFileUpload(input: {
async function startConciergeLoomFileProcessing({
requestId,
videoId,
+ expectedOwnerId,
+ maxBytes,
}: {
requestId: string;
videoId: Video.VideoId;
+ expectedOwnerId?: User.UserId;
+ maxBytes?: number;
}) {
const operator = await requireMigrationOperator();
if (typeof requestId !== "string" || !/^[A-Za-z0-9_-]{15}$/.test(requestId)) {
@@ -1437,6 +1656,7 @@ async function startConciergeLoomFileProcessing({
const [record] = await db()
.select({
video: videos,
+ loomVideoId: importedVideos.sourceId,
rawFileKey: videoUploads.rawFileKey,
phase: videoUploads.phase,
processingMessage: videoUploads.processingMessage,
@@ -1464,6 +1684,7 @@ async function startConciergeLoomFileProcessing({
.where(
and(
eq(videos.id, videoId),
+ expectedOwnerId ? eq(videos.ownerId, expectedOwnerId) : undefined,
eq(loomMigrationRequests.id, requestId),
eq(importedVideos.source, "loom"),
isNotNull(loomMigrationRequests.activeOrganizationId),
@@ -1472,6 +1693,7 @@ async function startConciergeLoomFileProcessing({
.limit(1);
if (
!record ||
+ (expectedOwnerId && record.video.ownerId !== expectedOwnerId) ||
!record.rawFileKey ||
(record.phase !== "error" &&
(record.phase !== "uploading" ||
@@ -1498,14 +1720,25 @@ async function startConciergeLoomFileProcessing({
});
if ((head.ContentLength ?? 0) <= 0)
throw new LoomMigrationError("The uploaded file is empty.");
- const status = await startVideoProcessingWorkflow({
+ if (maxBytes !== undefined && (head.ContentLength ?? 0) > maxBytes) {
+ throw new LoomMigrationError("The uploaded Loom file exceeds 10 GB.");
+ }
+ const processingInput = {
videoId,
userId: record.video.ownerId,
rawFileKey: record.rawFileKey,
bucketId: record.video.bucket,
processingMessage: "Processing Loom video...",
startFailureMessage: "Loom file processing could not start.",
- });
+ };
+ const status =
+ bucket.provider === "s3" &&
+ (head.ContentLength ?? 0) > LOOM_FILE_SINGLE_UPLOAD_MAX_BYTES
+ ? await startLoomFileProcessingWorkflow({
+ ...processingInput,
+ loomVideoId: record.loomVideoId,
+ })
+ : await startVideoProcessingWorkflow(processingInput);
revalidatePath("/dashboard/import/loom");
revalidatePath("/dashboard/admin/loom-migrations");
return { status };
diff --git a/apps/web/actions/notifications/mark-as-read.ts b/apps/web/actions/notifications/mark-as-read.ts
index 0c820c607..b3904d548 100644
--- a/apps/web/actions/notifications/mark-as-read.ts
+++ b/apps/web/actions/notifications/mark-as-read.ts
@@ -3,7 +3,7 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { notifications } from "@cap/database/schema";
-import { eq } from "drizzle-orm";
+import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
export const markAsRead = async (notificationId?: string) => {
@@ -14,17 +14,15 @@ export const markAsRead = async (notificationId?: string) => {
try {
const now = new Date();
- if (notificationId) {
- await db()
- .update(notifications)
- .set({ readAt: now })
- .where(eq(notifications.id, notificationId));
- } else {
- await db()
- .update(notifications)
- .set({ readAt: now })
- .where(eq(notifications.recipientId, currentUser.id));
- }
+ await db()
+ .update(notifications)
+ .set({ readAt: now })
+ .where(
+ and(
+ eq(notifications.recipientId, currentUser.id),
+ notificationId ? eq(notifications.id, notificationId) : undefined,
+ ),
+ );
} catch (error) {
console.log(error);
throw new Error("Error marking notification(s) as read");
diff --git a/apps/web/actions/organization/domain-utils.ts b/apps/web/actions/organization/domain-utils.ts
index 4a8fcb11e..ea3a5ed53 100644
--- a/apps/web/actions/organization/domain-utils.ts
+++ b/apps/web/actions/organization/domain-utils.ts
@@ -1,3 +1,20 @@
+import { parse } from "tldts";
+
+const VERCEL_CNAME_TARGET = "cname.vercel-dns.com";
+const VERCEL_A_RECORD = "76.76.21.21";
+
+export const isSubdomain = (raw: string): boolean => {
+ const input =
+ raw
+ .trim()
+ .replace(/^https?:\/\//i, "")
+ .split("/")[0] ?? "";
+ if (!input) return false;
+ const host = (input.replace(/\.$/, "").split(":")[0] || "").toLowerCase();
+ const { subdomain } = parse(host);
+ return Boolean(subdomain);
+};
+
export const getConfigResponse = async (domain: string) => {
const response = await fetch(
`https://api.vercel.com/v6/domains/${domain.toLowerCase()}/config?teamId=${
@@ -162,8 +179,25 @@ export const checkDomainStatus = async (domain: string) => {
verified = verificationJson?.verified;
}
- // Get the current and required A records
const currentAValues = configJson.aValues || [];
+ const subdomain = isSubdomain(domain);
+
+ // Vercel's recommendations are the source of truth, but the config
+ // endpoint can omit them (e.g. for domains not using Vercel DNS). Fall
+ // back to the well-known Vercel targets so the setup UI always has a
+ // record to display instead of rendering empty.
+ let recommendedCNAME: Array<{ rank: number; value: string }> =
+ configJson.recommendedCNAME || [];
+ let recommendedIPv4: Array<{ rank: number; value: string[] | string }> =
+ configJson.recommendedIPv4 || [];
+
+ if (subdomain && recommendedCNAME.length === 0) {
+ recommendedCNAME = [{ rank: 0, value: VERCEL_CNAME_TARGET }];
+ }
+ if (!subdomain && recommendedIPv4.length === 0) {
+ recommendedIPv4 = [{ rank: 0, value: [VERCEL_A_RECORD] }];
+ }
+
const requiredAValue = requiredConfigJson.aValues?.[0];
return {
@@ -173,10 +207,13 @@ export const checkDomainStatus = async (domain: string) => {
verification: domainJson?.verification || [],
currentAValues,
requiredAValue,
+ recommendedCNAME,
+ recommendedIPv4,
},
status: domainJson,
};
- } catch (_error) {
+ } catch (error) {
+ console.error("checkDomainStatus failed", { domain, error });
return {
verified: false,
error: "Failed to check domain status",
diff --git a/apps/web/actions/organization/remove-domain.ts b/apps/web/actions/organization/remove-domain.ts
index 5d1c051bc..a38f560e3 100644
--- a/apps/web/actions/organization/remove-domain.ts
+++ b/apps/web/actions/organization/remove-domain.ts
@@ -28,7 +28,7 @@ export async function removeOrganizationDomain(
try {
if (organization.customDomain) {
- await fetch(
+ const response = await fetch(
`https://api.vercel.com/v9/projects/${
process.env.VERCEL_PROJECT_ID
}/domains/${organization.customDomain.toLowerCase()}?teamId=${
@@ -41,6 +41,9 @@ export async function removeOrganizationDomain(
},
},
);
+ if (!response.ok && response.status !== 404) {
+ throw new Error("Failed to remove domain from hosting provider");
+ }
}
await db()
diff --git a/apps/web/actions/organization/storage.ts b/apps/web/actions/organization/storage.ts
index a39448033..3ae866662 100644
--- a/apps/web/actions/organization/storage.ts
+++ b/apps/web/actions/organization/storage.ts
@@ -25,6 +25,11 @@ import {
import { type Organisation, S3Bucket, Storage } from "@cap/web-domain";
import { and, desc, eq, isNull } from "drizzle-orm";
import { revalidatePath } from "next/cache";
+import {
+ blockedS3EndpointMessage,
+ isDisallowedS3Endpoint,
+ publicOnlyS3Agents,
+} from "@/lib/s3-endpoint-policy";
import { runPromise } from "@/lib/server";
import { requireOrganizationSettingsManager } from "./authorization";
@@ -384,6 +389,9 @@ export async function saveOrganizationS3Config(input: S3ConfigInput) {
const { user } = await requireOrganizationStorageManagerPro(
input.organizationId,
);
+ if (await isDisallowedS3Endpoint(input.endpoint)) {
+ throw new Error(blockedS3EndpointMessage);
+ }
const credentials = await getS3InputCredentials(input);
const encryptedConfig = {
provider: input.provider,
@@ -426,6 +434,9 @@ export async function removeOrganizationS3Config(
export async function testOrganizationS3Config(input: S3ConfigInput) {
await requireOrganizationStorageManagerPro(input.organizationId);
+ if (await isDisallowedS3Endpoint(input.endpoint)) {
+ throw new Error(blockedS3EndpointMessage);
+ }
const credentials = await getS3InputCredentials(input);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
@@ -436,6 +447,7 @@ export async function testOrganizationS3Config(input: S3ConfigInput) {
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
},
+ requestHandler: publicOnlyS3Agents(),
});
try {
diff --git a/apps/web/actions/organization/update-domain.ts b/apps/web/actions/organization/update-domain.ts
index 7ca542c9c..5cf1cf21d 100644
--- a/apps/web/actions/organization/update-domain.ts
+++ b/apps/web/actions/organization/update-domain.ts
@@ -24,6 +24,8 @@ export async function updateDomain(
throw new Error("User is not subscribed");
}
+ const normalizedDomain = domain.trim().toLowerCase();
+
const [organization] = await db()
.select()
.from(organizations)
@@ -37,7 +39,7 @@ export async function updateDomain(
const existingDomain = await db()
.select()
.from(organizations)
- .where(eq(organizations.customDomain, domain))
+ .where(eq(organizations.customDomain, normalizedDomain))
.limit(1);
if (existingDomain.length > 0 && existingDomain[0]?.id !== organizationId) {
@@ -45,7 +47,7 @@ export async function updateDomain(
}
try {
- const addDomainResponse = await addDomain(domain);
+ const addDomainResponse = await addDomain(normalizedDomain);
if (addDomainResponse.error) {
throw new Error(addDomainResponse.error.message);
@@ -54,12 +56,12 @@ export async function updateDomain(
await db()
.update(organizations)
.set({
- customDomain: domain,
+ customDomain: normalizedDomain,
domainVerified: null,
})
.where(eq(organizations.id, organizationId));
- const status = await checkDomainStatus(domain);
+ const status = await checkDomainStatus(normalizedDomain);
if (status.verified) {
await db()
diff --git a/apps/web/actions/organizations/get-organization-videos.ts b/apps/web/actions/organizations/get-organization-videos.ts
index 2e584388f..7fde37a1e 100644
--- a/apps/web/actions/organizations/get-organization-videos.ts
+++ b/apps/web/actions/organizations/get-organization-videos.ts
@@ -5,6 +5,7 @@ import { getCurrentUser } from "@cap/database/auth/session";
import { sharedVideos } from "@cap/database/schema";
import type { Organisation } from "@cap/web-domain";
import { and, eq, isNull } from "drizzle-orm";
+import { getOrganizationAccess } from "@/actions/organization/authorization";
export async function getOrganizationVideoIds(
organizationId: Organisation.OrganisationId,
@@ -20,6 +21,11 @@ export async function getOrganizationVideoIds(
throw new Error("Organization ID is required");
}
+ const access = await getOrganizationAccess(user.id, organizationId);
+ if (!access) {
+ throw new Error("Organization not found");
+ }
+
const videoIds = await db()
.select({
videoId: sharedVideos.videoId,
diff --git a/apps/web/actions/spaces/get-space-videos.ts b/apps/web/actions/spaces/get-space-videos.ts
index ddbcc42b8..a53dc3ac0 100644
--- a/apps/web/actions/spaces/get-space-videos.ts
+++ b/apps/web/actions/spaces/get-space-videos.ts
@@ -5,6 +5,8 @@ import { getCurrentUser } from "@cap/database/auth/session";
import { sharedVideos, spaceVideos } from "@cap/database/schema";
import type { Space } from "@cap/web-domain";
import { and, eq, isNull } from "drizzle-orm";
+import { getOrganizationAccess } from "@/actions/organization/authorization";
+import { getSpaceAccess } from "@/actions/organization/space-authorization";
export async function getSpaceVideoIds(spaceId: Space.SpaceIdOrOrganisationId) {
try {
@@ -20,6 +22,21 @@ export async function getSpaceVideoIds(spaceId: Space.SpaceIdOrOrganisationId) {
const isAllSpacesEntry = user.activeOrganizationId === spaceId;
+ if (isAllSpacesEntry) {
+ const access = await getOrganizationAccess(
+ user.id,
+ user.activeOrganizationId,
+ );
+ if (!access) {
+ throw new Error("Space not found");
+ }
+ } else {
+ const access = await getSpaceAccess(user.id, spaceId);
+ if (!access || (!access.organizationRole && !access.spaceRole)) {
+ throw new Error("Space not found");
+ }
+ }
+
const videoIds = isAllSpacesEntry
? await db()
.select({
diff --git a/apps/web/actions/video/retry-processing.ts b/apps/web/actions/video/retry-processing.ts
index fa53dabec..c2557ee2a 100644
--- a/apps/web/actions/video/retry-processing.ts
+++ b/apps/web/actions/video/retry-processing.ts
@@ -4,14 +4,18 @@ import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { importedVideos, videos, videoUploads } from "@cap/database/schema";
import type { Video } from "@cap/web-domain";
-import { eq } from "drizzle-orm";
-import { start } from "workflow/api";
+import { and, eq } from "drizzle-orm";
+import {
+ getLoomImportRunState,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE,
+ LoomImportStartError,
+ restoreLoomImportStartError,
+ startLoomImportWorkflow,
+} from "@/lib/loom-import-start";
import {
- setVideoProcessingError,
startVideoProcessingWorkflow,
type VideoProcessingStartStatus,
} from "@/lib/video-processing";
-import { importLoomVideoWorkflow } from "@/workflows/import-loom-video";
const SECOND = 1000;
const MINUTE = 60 * SECOND;
@@ -63,7 +67,6 @@ export async function retryVideoProcessing({
.where(eq(videoUploads.videoId, videoId));
if (!upload) throw new Error("No upload record found");
- if (!upload.rawFileKey) throw new Error("No raw file key found for retry");
const [importedVideo] = await db()
.select({
@@ -71,52 +74,85 @@ export async function retryVideoProcessing({
sourceId: importedVideos.sourceId,
})
.from(importedVideos)
- .where(eq(importedVideos.id, videoId));
+ .where(
+ and(
+ eq(importedVideos.id, videoId),
+ eq(importedVideos.orgId, video.orgId),
+ ),
+ );
if (importedVideo?.source === "loom") {
- if (upload.phase === "processing" && !shouldForceRetryProcessing(upload)) {
+ const runState = await getLoomImportRunState(video.metadata?.loomImportRun);
+ if (runState === "unconfirmed") {
+ throw new Error(LOOM_IMPORT_CONFIRMATION_MESSAGE);
+ }
+ if (
+ runState !== "inactive" ||
+ upload.phase === "uploading" ||
+ ((upload.phase === "processing" ||
+ upload.phase === "generating_thumbnail") &&
+ !shouldForceRetryProcessing(upload))
+ ) {
return { success: true, status: "already-processing" };
}
- await db()
+ const rawFileKey =
+ upload.rawFileKey ?? `${video.ownerId}/${videoId}/raw-upload.mp4`;
+ const claimedAt = new Date(Math.floor(Date.now() / 1000) * 1000);
+ const claim = await db()
.update(videoUploads)
.set({
phase: "processing",
processingProgress: 0,
processingMessage: "Retrying Loom import...",
processingError: null,
- rawFileKey: upload.rawFileKey,
- updatedAt: new Date(),
+ rawFileKey,
+ updatedAt: claimedAt,
})
- .where(eq(videoUploads.videoId, videoId));
+ .where(
+ and(
+ eq(videoUploads.videoId, videoId),
+ eq(videoUploads.phase, upload.phase),
+ eq(videoUploads.updatedAt, upload.updatedAt),
+ ),
+ );
+ const affectedRows = Array.isArray(claim)
+ ? (claim[0] as { affectedRows?: number } | undefined)?.affectedRows
+ : (claim as { affectedRows?: number }).affectedRows;
+ if (affectedRows !== 1) {
+ return { success: true, status: "already-processing" };
+ }
try {
- await start(importLoomVideoWorkflow, [
- {
- videoId,
- userId: user.id,
- rawFileKey: upload.rawFileKey,
- bucketId: video.bucket ?? null,
- loomDownloadUrl: "",
- loomVideoId: importedVideo.sourceId,
- },
- ]);
+ await startLoomImportWorkflow({
+ videoId,
+ userId: user.id,
+ rawFileKey,
+ bucketId: video.bucket ?? null,
+ loomDownloadUrl: "",
+ loomVideoId: importedVideo.sourceId,
+ reuseExistingRawUpload: true,
+ });
} catch (error) {
const normalizedError =
error instanceof Error
? error
: new Error("Loom import could not restart");
- await setVideoProcessingError(
- videoId,
- "Loom import could not restart.",
- normalizedError,
- );
+ if (error instanceof LoomImportStartError && error.canRetry) {
+ await restoreLoomImportStartError(
+ videoId,
+ "processing",
+ claimedAt,
+ error.message,
+ );
+ }
throw normalizedError;
}
return { success: true, status: "started" };
}
+ if (!upload.rawFileKey) throw new Error("No raw file key found for retry");
const status = await startVideoProcessingWorkflow({
videoId,
userId: user.id,
diff --git a/apps/web/actions/video/trigger-processing.ts b/apps/web/actions/video/trigger-processing.ts
index ed6ed5df9..ca48badff 100644
--- a/apps/web/actions/video/trigger-processing.ts
+++ b/apps/web/actions/video/trigger-processing.ts
@@ -10,6 +10,7 @@ import { Effect, Schedule } from "effect";
import { runPromise } from "@/lib/server";
import { startVideoProcessingWorkflow } from "@/lib/video-processing";
import { decodeStorageVideo } from "@/lib/video-storage";
+import { isVideoStorageKey } from "@/lib/video-storage-key";
async function verifyRawFileUploaded(
video: typeof videos.$inferSelect,
@@ -49,6 +50,12 @@ export async function triggerVideoProcessing({
if (!video) throw new Error("Video not found");
if (video.ownerId !== user.id) throw new Error("Unauthorized");
+ if (!isVideoStorageKey(video, rawFileKey)) {
+ throw new Error("Upload file does not belong to this video");
+ }
+ if ((video.bucket ?? null) !== (bucketId ?? null)) {
+ throw new Error("Upload bucket does not match this video");
+ }
await verifyRawFileUploaded(video, rawFileKey);
diff --git a/apps/web/actions/videos/download.ts b/apps/web/actions/videos/download.ts
index 5ced7ae8c..6352ea7a9 100644
--- a/apps/web/actions/videos/download.ts
+++ b/apps/web/actions/videos/download.ts
@@ -4,7 +4,7 @@ import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { videoEdits, videos, videoUploads } from "@cap/database/schema";
import { Storage } from "@cap/web-backend";
-import type { Video } from "@cap/web-domain";
+import { Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { Effect } from "effect";
import { runPromise } from "@/lib/server";
@@ -141,10 +141,15 @@ export async function getVideoDownloadInfo(
}
try {
+ const decodedVideo = decodeStorageVideo(video);
+ if (variant === "current") {
+ const source = Video.Video.getSource(decodedVideo);
+ if (source instanceof Video.Mp4Source) {
+ downloadKey = source.getFileKey();
+ }
+ }
const downloadUrl = await Effect.gen(function* () {
- const [bucket] = yield* Storage.getAccessForVideo(
- decodeStorageVideo(video),
- );
+ const [bucket] = yield* Storage.getAccessForVideo(decodedVideo);
const exists = yield* bucket.headObject(downloadKey).pipe(
Effect.as(true),
Effect.catchAll(() => Effect.succeed(false)),
diff --git a/apps/web/actions/videos/get-status.ts b/apps/web/actions/videos/get-status.ts
index 282ad7488..ba7ac039b 100644
--- a/apps/web/actions/videos/get-status.ts
+++ b/apps/web/actions/videos/get-status.ts
@@ -44,7 +44,7 @@ export interface VideoStatusResult {
export async function getVideoStatus(
videoId: Video.VideoId,
-): Promise<VideoStatusResult | { success: false }> {
+): Promise<VideoStatusResult | { success: false; reason?: "not_found" }> {
if (!videoId) throw new Error("Video ID not provided");
const exit = await Effect.gen(function* () {
@@ -58,7 +58,7 @@ export async function getVideoStatus(
if (Exit.isFailure(exit)) return { success: false };
const video = exit.value[0];
- if (!video) throw new Error("Video not found");
+ if (!video) return { success: false, reason: "not_found" };
const metadata: VideoMetadata = (video.metadata as VideoMetadata) || {};
diff --git a/apps/web/actions/videos/new-comment.ts b/apps/web/actions/videos/new-comment.ts
index 8c2cf7ab5..616ac95a8 100644
--- a/apps/web/actions/videos/new-comment.ts
+++ b/apps/web/actions/videos/new-comment.ts
@@ -10,6 +10,7 @@ import { Comment, Policy, type Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { Effect, Exit } from "effect";
import { revalidatePath } from "next/cache";
+import { isReplyParentOnVideo } from "@/lib/comment-reply-parent";
import { createNotification } from "@/lib/Notification";
import * as EffectRuntime from "@/lib/server";
@@ -68,6 +69,12 @@ export async function newComment(data: {
throw new Error("Video not found");
}
+ if (
+ parentCommentId &&
+ !(await isReplyParentOnVideo(parentCommentId, videoId))
+ )
+ throw new Error("Comment not found");
+
const id = Comment.CommentId.make(nanoId());
const newComment = {
diff --git a/apps/web/actions/videos/new-media-comment.ts b/apps/web/actions/videos/new-media-comment.ts
index 70b562927..77e460bee 100644
--- a/apps/web/actions/videos/new-media-comment.ts
+++ b/apps/web/actions/videos/new-media-comment.ts
@@ -15,9 +15,11 @@ import { type Comment, Policy } from "@cap/web-domain";
import { Effect, Exit, Option } from "effect";
import { revalidatePath } from "next/cache";
import { sanitizeWaveform } from "@/lib/comment-media";
+import { isReplyParentOnVideo } from "@/lib/comment-reply-parent";
import { canUseMediaComments } from "@/lib/media-comments";
import { createNotification } from "@/lib/Notification";
import * as EffectRuntime from "@/lib/server";
+import { isStorageObjectNotFound } from "@/lib/storage-not-found";
export async function newMediaComment(data: {
videoId: Video.VideoId;
@@ -68,6 +70,12 @@ export async function newMediaComment(data: {
throw new Error("Video not found");
const [video] = accessExit.value.value;
+ if (
+ data.parentCommentId &&
+ !(await isReplyParentOnVideo(data.parentCommentId, data.videoId))
+ )
+ throw new Error("Comment not found");
+
const expectedPrefix = `${video.ownerId}/${video.id}/comments/${data.commentId}/`;
if (!claims.key.startsWith(expectedPrefix))
throw new Error("Invalid media key");
@@ -87,7 +95,9 @@ export async function newMediaComment(data: {
// A missing thumbnail degrades the card, it doesn't block the comment.
const thumbnailExists = yield* bucket.headObject(thumbnailKey).pipe(
Effect.map(() => true),
- Effect.catchAll(() => Effect.succeed(false)),
+ Effect.catchAll((error) =>
+ Effect.succeed(!isStorageObjectNotFound(error)),
+ ),
);
if (!thumbnailExists) thumbnailKey = null;
}
diff --git a/apps/web/actions/videos/save-edits.ts b/apps/web/actions/videos/save-edits.ts
index 2ba3c7cc3..b58f687a8 100644
--- a/apps/web/actions/videos/save-edits.ts
+++ b/apps/web/actions/videos/save-edits.ts
@@ -15,6 +15,7 @@ import { revalidatePath } from "next/cache";
import { start } from "workflow/api";
import { assertLegacyEditsQuiescent } from "@/lib/legacy-video-edit-recovery";
import { runPromise } from "@/lib/server";
+import { isStorageObjectNotFound } from "@/lib/storage-not-found";
import {
clearPendingEdit,
type EditOperation,
@@ -53,7 +54,7 @@ async function objectExists(
) {
return await bucket.headObject(key).pipe(
Effect.as(true),
- Effect.catchAll(() => Effect.succeed(false)),
+ Effect.catchIf(isStorageObjectNotFound, () => Effect.succeed(false)),
runPromise,
);
}
diff --git a/apps/web/actions/videos/translate-transcript.ts b/apps/web/actions/videos/translate-transcript.ts
index f3f78d388..4c03c8feb 100644
--- a/apps/web/actions/videos/translate-transcript.ts
+++ b/apps/web/actions/videos/translate-transcript.ts
@@ -17,6 +17,13 @@ import {
type LanguageCode,
SUPPORTED_LANGUAGES,
} from "./translation-languages";
+import {
+ isCompleteTranslation,
+ joinTranslationChunks,
+ readCachedTranslation,
+ splitTranslationChunks,
+ withSourceFingerprint,
+} from "./translation-vtt";
interface TranslateResult {
success: boolean;
@@ -75,6 +82,19 @@ export async function translateTranscript(
const translatedKey = `${video.ownerId}/${videoId}/transcription.${targetLanguage}.vtt`;
+ const originalVtt = await Effect.gen(function* () {
+ const [bucket] = yield* Storage.getAccessForVideo(
+ decodeStorageVideo(video),
+ );
+ return yield* bucket.getObject(
+ `${video.ownerId}/${videoId}/transcription.vtt`,
+ );
+ }).pipe(runPromise);
+
+ if (Option.isNone(originalVtt)) {
+ return { success: false, message: "Original transcript not found" };
+ }
+
try {
const existingTranslation = await Effect.gen(function* () {
const [bucket] = yield* Storage.getAccessForVideo(
@@ -83,10 +103,13 @@ export async function translateTranscript(
return yield* bucket.getObject(translatedKey);
}).pipe(runPromise);
- if (Option.isSome(existingTranslation)) {
+ const cachedTranslation = Option.isSome(existingTranslation)
+ ? readCachedTranslation(originalVtt.value, existingTranslation.value)
+ : null;
+ if (cachedTranslation) {
return {
success: true,
- translatedVtt: existingTranslation.value,
+ translatedVtt: cachedTranslation,
message: "Retrieved cached translation",
};
}
@@ -94,19 +117,6 @@ export async function translateTranscript(
console.debug("[translateTranscript] No cached translation found:", e);
}
- const originalVtt = await Effect.gen(function* () {
- const [bucket] = yield* Storage.getAccessForVideo(
- decodeStorageVideo(video),
- );
- return yield* bucket.getObject(
- `${video.ownerId}/${videoId}/transcription.vtt`,
- );
- }).pipe(runPromise);
-
- if (Option.isNone(originalVtt)) {
- return { success: false, message: "Original transcript not found" };
- }
-
const translatedVtt = await translateVttContent(
originalVtt.value,
targetLanguage,
@@ -116,17 +126,23 @@ export async function translateTranscript(
return { success: false, message: "Translation failed" };
}
- try {
- await Effect.gen(function* () {
- const [bucket] = yield* Storage.getAccessForVideo(
- decodeStorageVideo(video),
+ const cacheBody = withSourceFingerprint(originalVtt.value, translatedVtt);
+ if (cacheBody) {
+ try {
+ await Effect.gen(function* () {
+ const [bucket] = yield* Storage.getAccessForVideo(
+ decodeStorageVideo(video),
+ );
+ yield* bucket.putObject(translatedKey, cacheBody, {
+ contentType: "text/vtt",
+ });
+ }).pipe(runPromise);
+ } catch (error) {
+ console.error(
+ "[translateTranscript] Failed to cache translation:",
+ error,
);
- yield* bucket.putObject(translatedKey, translatedVtt, {
- contentType: "text/vtt",
- });
- }).pipe(runPromise);
- } catch (error) {
- console.error("[translateTranscript] Failed to cache translation:", error);
+ }
}
return {
@@ -141,8 +157,12 @@ async function translateVttContent(
targetLanguage: LanguageCode,
): Promise<string | null> {
const targetLanguageName = SUPPORTED_LANGUAGES[targetLanguage];
+ const chunks = splitTranslationChunks(vttContent);
+ if (!chunks) return null;
- const prompt = `Translate the following WebVTT subtitle file to ${targetLanguageName}.
+ const translations: string[] = [];
+ for (const chunk of chunks) {
+ const prompt = `Translate the following WebVTT subtitle file to ${targetLanguageName}.
IMPORTANT RULES:
1. Keep the "WEBVTT" header exactly as is
@@ -155,27 +175,35 @@ IMPORTANT RULES:
VTT content to translate:
-${vttContent}`;
-
- try {
- return await runWithAiProviders("generation", async (selection) => {
- const response = await generateText({
- model: selection.model(),
- prompt,
- maxOutputTokens: 8000,
- ...(selection.supportsTemperature ? { temperature: 0.3 } : {}),
- });
-
- // Validate inside the provider loop so a fulfilled response that
- // dropped the WEBVTT header falls through to the next provider.
- if (!response.text.includes("WEBVTT")) {
- throw new Error("translation response did not contain WEBVTT");
- }
-
- return response.text.trim();
- });
- } catch (error) {
- console.error("[translateVttContent] Translation error:", error);
- return null;
+${chunk}`;
+
+ try {
+ const translatedChunk = await runWithAiProviders(
+ "generation",
+ async (selection) => {
+ const response = await generateText({
+ model: selection.model(),
+ prompt,
+ maxOutputTokens: 8000,
+ ...(selection.supportsTemperature ? { temperature: 0.3 } : {}),
+ });
+
+ if (
+ response.finishReason === "length" ||
+ !isCompleteTranslation(chunk, response.text)
+ ) {
+ throw new Error("translation response has missing or changed cues");
+ }
+
+ return response.text.trim();
+ },
+ );
+ translations.push(translatedChunk);
+ } catch (error) {
+ console.error("[translateVttContent] Translation error:", error);
+ return null;
+ }
}
+
+ return joinTranslationChunks(vttContent, chunks, translations);
}
diff --git a/apps/web/actions/videos/translation-vtt.ts b/apps/web/actions/videos/translation-vtt.ts
new file mode 100644
index 000000000..3fa7cbaf5
--- /dev/null
+++ b/apps/web/actions/videos/translation-vtt.ts
@@ -0,0 +1,145 @@
+import { createHash } from "node:crypto";
+
+const MAX_CHUNK_LENGTH = 3_000;
+const SOURCE_FINGERPRINT_PREFIX = "NOTE cap-source-sha256 ";
+
+function splitVtt(content: string): string[] | null {
+ const blocks = content
+ .replace(/\r\n?/g, "\n")
+ .trim()
+ .split(/\n{2,}/);
+ if (!blocks[0]?.startsWith("WEBVTT")) {
+ return null;
+ }
+ return blocks;
+}
+
+function timestampLines(block: string): string[] {
+ return block.split("\n").filter((line) => line.includes("-->"));
+}
+
+function hasCueText(block: string): boolean {
+ const lines = block.split("\n");
+ const timestampIndex = lines.findIndex((line) => line.includes("-->"));
+ return (
+ timestampIndex >= 0 &&
+ lines.slice(timestampIndex + 1).some((line) => line.trim().length > 0)
+ );
+}
+
+function cueIdentifier(block: string): string {
+ const lines = block.split("\n");
+ const timestampIndex = lines.findIndex((line) => line.includes("-->"));
+ return lines.slice(0, timestampIndex).join("\n");
+}
+
+export function isCompleteTranslation(
+ source: string,
+ translation: string,
+): boolean {
+ const sourceBlocks = splitVtt(source);
+ const translatedBlocks = splitVtt(translation);
+ if (
+ !sourceBlocks ||
+ !translatedBlocks ||
+ sourceBlocks.length !== translatedBlocks.length
+ ) {
+ return false;
+ }
+
+ for (let index = 1; index < sourceBlocks.length; index++) {
+ const original = sourceBlocks[index];
+ const translated = translatedBlocks[index];
+ if (!original || !translated) return false;
+
+ const originalTimestamps = timestampLines(original);
+ const translatedTimestamps = timestampLines(translated);
+ if (
+ originalTimestamps.length !== translatedTimestamps.length ||
+ originalTimestamps.some(
+ (line, timestampIndex) => line !== translatedTimestamps[timestampIndex],
+ ) ||
+ (originalTimestamps.length > 0 &&
+ cueIdentifier(original) !== cueIdentifier(translated)) ||
+ (originalTimestamps.length === 0 && original !== translated) ||
+ (originalTimestamps.length > 0 && !hasCueText(translated))
+ ) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+export function splitTranslationChunks(source: string): string[] | null {
+ const blocks = splitVtt(source);
+ if (!blocks) return null;
+
+ const chunks: string[] = [];
+ let current: string[] = [];
+ let length = 0;
+
+ for (const block of blocks.slice(1)) {
+ if (current.length > 0 && length + block.length > MAX_CHUNK_LENGTH) {
+ chunks.push(`WEBVTT\n\n${current.join("\n\n")}`);
+ current = [];
+ length = 0;
+ }
+ current.push(block);
+ length += block.length + 2;
+ }
+
+ if (current.length > 0) {
+ chunks.push(`WEBVTT\n\n${current.join("\n\n")}`);
+ }
+
+ return chunks;
+}
+
+export function joinTranslationChunks(
+ source: string,
+ chunks: string[],
+ translations: string[],
+): string | null {
+ const sourceBlocks = splitVtt(source);
+ if (!sourceBlocks || chunks.length !== translations.length) return null;
+
+ const translatedBlocks: string[] = [];
+ for (let index = 0; index < chunks.length; index++) {
+ const chunk = chunks[index];
+ const translation = translations[index];
+ if (!chunk || !translation || !isCompleteTranslation(chunk, translation)) {
+ return null;
+ }
+ const blocks = splitVtt(translation);
+ if (!blocks) return null;
+ translatedBlocks.push(...blocks.slice(1));
+ }
+
+ const result = [sourceBlocks[0], ...translatedBlocks].join("\n\n");
+ return isCompleteTranslation(source, result) ? result : null;
+}
+
+function sourceFingerprint(source: string): string {
+ return `${SOURCE_FINGERPRINT_PREFIX}${createHash("sha256").update(source).digest("hex")}`;
+}
+
+export function withSourceFingerprint(
+ source: string,
+ translation: string,
+): string | null {
+ const blocks = splitVtt(translation);
+ if (!blocks) return null;
+ const [header, ...cues] = blocks;
+ return [header, sourceFingerprint(source), ...cues].join("\n\n");
+}
+
+export function readCachedTranslation(
+ source: string,
+ cached: string,
+): string | null {
+ const blocks = splitVtt(cached);
+ if (!blocks || blocks[1] !== sourceFingerprint(source)) return null;
+ const translation = [blocks[0], ...blocks.slice(2)].join("\n\n");
+ return isCompleteTranslation(source, translation) ? translation : null;
+}
diff --git a/apps/web/app/(org)/dashboard/admin/loom-migrations/LoomMigrationQueue.tsx b/apps/web/app/(org)/dashboard/admin/loom-migrations/LoomMigrationQueue.tsx
index f9d860b04..6fb711fff 100644
--- a/apps/web/app/(org)/dashboard/admin/loom-migrations/LoomMigrationQueue.tsx
+++ b/apps/web/app/(org)/dashboard/admin/loom-migrations/LoomMigrationQueue.tsx
@@ -11,9 +11,11 @@ import {
importFromLoomCsvForConcierge,
type LoomCsvImportRow,
type LoomCsvImportRowResult,
+ resumeConciergeLoomFileUpload,
} from "@/actions/loom";
import {
getLoomMigrationOperatorQueue,
+ reconcileLoomMigrationInventory,
updateLoomMigrationStatus,
} from "@/actions/loom-concierge";
import type { OperatorLoomMigrationView } from "@/lib/loom-concierge";
@@ -45,6 +47,8 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
request.importedVideoCount.toString(),
);
const [verified, setVerified] = useState(false);
+ const [deletedRecordingsVerified, setDeletedRecordingsVerified] =
+ useState(false);
const [csvRows, setCsvRows] = useState<LoomCsvImportRow[]>([]);
const [processedRows, setProcessedRows] = useState(0);
const [startedRows, setStartedRows] = useState(0);
@@ -55,6 +59,8 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
const [fileTitle, setFileTitle] = useState("");
const [file, setFile] = useState<File | null>(null);
const [uploadPercent, setUploadPercent] = useState(0);
+ const [previousProcessingStopped, setPreviousProcessingStopped] =
+ useState(false);
useEffect(() => {
setNextStatus(request.status);
@@ -62,6 +68,7 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
setExpectedVideoCount(request.expectedVideoCount?.toString() ?? "");
setImportedVideoCount(request.importedVideoCount.toString());
setVerified(false);
+ setDeletedRecordingsVerified(false);
}, [
request.status,
request.capMessage,
@@ -92,6 +99,24 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
},
onError: (error) => toast.error(error.message),
});
+ const reconcileMutation = useMutation({
+ mutationFn: async () =>
+ unwrapLoomMigrationResult(
+ await reconcileLoomMigrationInventory({
+ requestId: request.id,
+ verified: deletedRecordingsVerified,
+ }),
+ ),
+ onSuccess: async ({ removedCount, hasMore }) => {
+ setVerified(false);
+ setDeletedRecordingsVerified(false);
+ await refresh();
+ toast.success(
+ `Removed ${removedCount} deleted recording entries. ${hasMore ? "More entries remain; reconcile again." : "Check source counts before completing the migration."}`,
+ );
+ },
+ onError: (error) => toast.error(error.message),
+ });
const importMutation = useMutation({
mutationFn: async () => {
if (csvRows.length === 0) throw new Error("Choose a Loom CSV first.");
@@ -183,6 +208,31 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
);
},
});
+ const resumeFileMutation = useMutation({
+ mutationFn: async () =>
+ unwrapLoomMigrationResult(
+ await resumeConciergeLoomFileUpload({
+ requestId: request.id,
+ loomUrl: fileLoomUrl,
+ userEmail: fileOwnerEmail,
+ confirmedPreviousProcessingStopped: previousProcessingStopped,
+ }),
+ ),
+ onSuccess: async (result) => {
+ await refresh();
+ setPreviousProcessingStopped(false);
+ toast.success(
+ result.status === "started"
+ ? "Uploaded Loom file processing has started."
+ : "Uploaded Loom file processing was already underway.",
+ );
+ },
+ onError: async (error) => {
+ await refresh();
+ toast.error(error.message);
+ },
+ });
+ const filePending = fileMutation.isPending || resumeFileMutation.isPending;
return (
<div className="rounded-xl border border-gray-4 bg-gray-1 p-5">
@@ -237,7 +287,7 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
<input
accept=".csv,text/csv"
className="mt-2 block w-full text-sm text-gray-11"
- disabled={importMutation.isPending}
+ disabled={importMutation.isPending || filePending}
id={`loom-csv-${request.id}`}
onChange={async (event) => {
const file = event.target.files?.[0];
@@ -266,7 +316,9 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
)}
<Button
className="mt-3"
- disabled={importMutation.isPending || csvRows.length === 0}
+ disabled={
+ importMutation.isPending || filePending || csvRows.length === 0
+ }
onClick={() => importMutation.mutate()}
size="sm"
variant="dark"
@@ -321,7 +373,10 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
<input
className="mt-1 w-full rounded-lg border border-gray-6 bg-gray-1 px-3 py-2 text-gray-12"
id={`loom-file-url-${request.id}`}
- onChange={(event) => setFileLoomUrl(event.target.value)}
+ onChange={(event) => {
+ setFileLoomUrl(event.target.value);
+ setPreviousProcessingStopped(false);
+ }}
placeholder="https://www.loom.com/share/..."
type="url"
value={fileLoomUrl}
@@ -335,7 +390,10 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
<input
className="mt-1 w-full rounded-lg border border-gray-6 bg-gray-1 px-3 py-2 text-gray-12"
id={`loom-file-owner-${request.id}`}
- onChange={(event) => setFileOwnerEmail(event.target.value)}
+ onChange={(event) => {
+ setFileOwnerEmail(event.target.value);
+ setPreviousProcessingStopped(false);
+ }}
type="email"
value={fileOwnerEmail}
/>
@@ -379,7 +437,7 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
/>
</label>
<Button
- disabled={fileMutation.isPending || importMutation.isPending || !file}
+ disabled={filePending || importMutation.isPending || !file}
size="sm"
type="submit"
variant="dark"
@@ -388,6 +446,39 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
? `Uploading ${uploadPercent}%`
: "Upload and process"}
</Button>
+ <label className="flex items-start gap-2 text-xs text-gray-10">
+ <input
+ checked={previousProcessingStopped}
+ disabled={filePending}
+ onChange={(event) =>
+ setPreviousProcessingStopped(event.target.checked)
+ }
+ type="checkbox"
+ />
+ I verified the complete MP4 is uploaded and the previous processing
+ has stopped.
+ </label>
+ <Button
+ disabled={
+ filePending ||
+ importMutation.isPending ||
+ !previousProcessingStopped ||
+ !fileLoomUrl.trim() ||
+ !fileOwnerEmail.trim()
+ }
+ onClick={() => resumeFileMutation.mutate()}
+ size="sm"
+ type="button"
+ variant="gray"
+ >
+ {resumeFileMutation.isPending
+ ? "Starting processing..."
+ : "Process uploaded file"}
+ </Button>
+ <p className="text-xs text-gray-10">
+ Process an existing recording after its MP4 has finished uploading to
+ Cap storage. Review its Loom URL and owner first.
+ </p>
</form>
<form
@@ -488,8 +579,9 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
<Button
disabled={
statusMutation.isPending ||
+ reconcileMutation.isPending ||
importMutation.isPending ||
- fileMutation.isPending ||
+ filePending ||
(nextStatus === "completed" && request.activeImportCount > 0)
}
size="sm"
@@ -499,6 +591,41 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {
Save status
</Button>
</form>
+ <div className="mt-4 space-y-3 border-t border-gray-6 pt-4">
+ <p className="text-sm text-gray-11">
+ Remove inventory entries for recordings that no longer exist. Existing
+ recordings and source mappings are kept. This does not complete the
+ migration.
+ </p>
+ <label className="flex items-start gap-2 text-sm text-gray-11">
+ <input
+ checked={deletedRecordingsVerified}
+ className="mt-1"
+ type="checkbox"
+ onChange={(event) =>
+ setDeletedRecordingsVerified(event.target.checked)
+ }
+ />
+ I reconciled deleted recordings against the agreed source list and
+ re-imported any missing sources.
+ </label>
+ <Button
+ type="button"
+ size="sm"
+ variant="gray"
+ disabled={
+ !deletedRecordingsVerified ||
+ reconcileMutation.isPending ||
+ statusMutation.isPending ||
+ importMutation.isPending ||
+ filePending ||
+ request.activeImportCount > 0
+ }
+ onClick={() => reconcileMutation.mutate()}
+ >
+ Reconcile deleted recordings
+ </Button>
+ </div>
</div>
);
}
diff --git a/apps/web/app/(org)/dashboard/analytics/components/ChartArea.tsx b/apps/web/app/(org)/dashboard/analytics/components/ChartArea.tsx
index 56c46efb5..c2f9f95a6 100644
--- a/apps/web/app/(org)/dashboard/analytics/components/ChartArea.tsx
+++ b/apps/web/app/(org)/dashboard/analytics/components/ChartArea.tsx
@@ -247,5 +247,9 @@ const formatBucketLabel = (bucket: string, hourly: boolean) => {
hour: "numeric",
minute: undefined,
});
- return date.toLocaleDateString([], { month: "short", day: "numeric" });
+ return date.toLocaleDateString([], {
+ month: "short",
+ day: "numeric",
+ timeZone: "UTC",
+ });
};
diff --git a/apps/web/app/(org)/dashboard/caps/page.tsx b/apps/web/app/(org)/dashboard/caps/page.tsx
index 6200ac48b..1b636ecf6 100644
--- a/apps/web/app/(org)/dashboard/caps/page.tsx
+++ b/apps/web/app/(org)/dashboard/caps/page.tsx
@@ -146,6 +146,7 @@ export default async function CapsPage(props: PageProps<"/dashboard/caps">) {
and(
eq(videos.ownerId, userId),
eq(organizations.id, user.activeOrganizationId),
+ isNull(videos.folderId),
isNull(organizations.tombstoneAt),
),
);
diff --git a/apps/web/app/(org)/dashboard/folder/[id]/page.tsx b/apps/web/app/(org)/dashboard/folder/[id]/page.tsx
index 703aed3df..c0701a7ee 100644
--- a/apps/web/app/(org)/dashboard/folder/[id]/page.tsx
+++ b/apps/web/app/(org)/dashboard/folder/[id]/page.tsx
@@ -4,6 +4,7 @@ import { makeCurrentUserLayer } from "@cap/web-backend";
import { Folder } from "@cap/web-domain";
import { Effect } from "effect";
import { notFound } from "next/navigation";
+import { canViewFolder } from "@/actions/folders/folder-access";
import {
getChildFolders,
getFolderBreadcrumb,
@@ -31,6 +32,14 @@ const FolderPage = async (props: PageProps<"/dashboard/folder/[id]">) => {
const user = await getCurrentUser();
if (!user || !user.activeOrganizationId) return notFound();
+ const folder = await getFolderById(folderId).pipe(
+ Effect.provide(makeCurrentUserLayer(user)),
+ Effect.catchAllCause(() => Effect.succeed(null)),
+ runPromise,
+ );
+
+ if (!folder || !(await canViewFolder(user.id, folder))) return notFound();
+
return Effect.gen(function* () {
const [childFolders, breadcrumb, videosData, share] = yield* Effect.all(
[
@@ -40,7 +49,6 @@ const FolderPage = async (props: PageProps<"/dashboard/folder/[id]">) => {
variant: "user",
}),
Effect.gen(function* () {
- const folder = yield* getFolderById(folderId);
// Mirrors FoldersPolicy.canEdit for personal folders: only the
// creator may manage sharing; the Pro gate uses the folder's own
// organization, which is what the server enforces on writes.
diff --git a/apps/web/app/(org)/dashboard/import/import-media.ts b/apps/web/app/(org)/dashboard/import/import-media.ts
index 3a2a4ab7d..22a1d5384 100644
--- a/apps/web/app/(org)/dashboard/import/import-media.ts
+++ b/apps/web/app/(org)/dashboard/import/import-media.ts
@@ -169,24 +169,21 @@ async function uploadVideoForServerProcessing(
uploaded: 0,
total: 0,
pendingTask: undefined as ReturnType<typeof setTimeout> | undefined,
- lastUpdateTime: Date.now(),
};
const scheduleProgressUpdate = (uploaded: number, total: number) => {
uploadState.uploaded = uploaded;
uploadState.total = total;
- uploadState.lastUpdateTime = Date.now();
if (uploadState.pendingTask) {
- clearTimeout(uploadState.pendingTask);
- uploadState.pendingTask = undefined;
+ return;
}
const shouldSendImmediately = uploaded >= total;
if (!shouldSendImmediately) {
uploadState.pendingTask = setTimeout(() => {
- if (uploadState.videoId) {
+ if (uploadState.uploaded < uploadState.total) {
sendProgressUpdate(
uploadState.videoId,
uploadState.uploaded,
diff --git a/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx b/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
index 085577b42..fcf63b506 100644
--- a/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
+++ b/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
@@ -56,7 +56,13 @@ import {
} from "@/actions/loom";
import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
import { UpgradeModal } from "@/components/UpgradeModal";
-import { parseLoomCsvRecords } from "@/lib/loom-csv";
+import {
+ formatLoomCsvCell,
+ isLoomCsvResultsFile,
+ LOOM_CSV_RESULTS_HEADERS,
+ parseLoomCsvRecords,
+ readLoomCsvCell,
+} from "@/lib/loom-csv";
import {
type LoomImportDestination,
loomImportDestinationHref,
@@ -100,6 +106,8 @@ const MAX_SPACE_NAME_LENGTH = 255;
const MAX_LOOM_CSV_IMPORT_ROWS = 500;
const LOOM_CSV_BATCH_SIZE = 10;
const LOOM_CSV_BATCH_DELAY_MS = 1500;
+const LOOM_CSV_RETRY_DELAY_MS = 30000;
+const LOOM_CSV_NOT_ATTEMPTED_MESSAGE = "Not attempted.";
const LOOM_CSV_LIMIT_MESSAGE =
"CSV imports are limited to 500 videos at a time. Contact support to raise this limit.";
const LOOM_CSV_PERMISSION_MESSAGE =
@@ -135,12 +143,88 @@ function buildCsvImportResult(
};
}
+function buildRowFailure(
+ row: MappedRow,
+ error: string,
+): LoomCsvImportRowResult {
+ return {
+ rowNumber: row.rowNumber,
+ userEmail: row.userEmail,
+ spaceName: row.spaceName || undefined,
+ success: false,
+ error,
+ };
+}
+
+function withNotAttemptedRows(
+ rows: MappedRow[],
+ results: LoomCsvImportRowResult[],
+): LoomCsvImportRowResult[] {
+ const attempted = new Set(results.map((row) => row.rowNumber));
+ const missing = rows.filter((row) => !attempted.has(row.rowNumber));
+ if (missing.length === 0) return results;
+
+ return [
+ ...results,
+ ...missing.map((row) =>
+ buildRowFailure(row, LOOM_CSV_NOT_ATTEMPTED_MESSAGE),
+ ),
+ ].sort((a, b) => a.rowNumber - b.rowNumber);
+}
+
+function retryableBatchRows(
+ batch: MappedRow[],
+ results: LoomCsvImportRowResult[],
+) {
+ const retryable = new Set(
+ results
+ .filter(
+ (row) =>
+ !row.success && row.errorCode === "loom_temporarily_unavailable",
+ )
+ .map((row) => row.rowNumber),
+ );
+ return batch.filter((row) => retryable.has(row.rowNumber));
+}
+
+function importErrorMessage(error: unknown) {
+ return error instanceof Error && error.message
+ ? error.message
+ : "The import request failed for this batch.";
+}
+
+function buildResultsCsv(
+ results: LoomCsvImportRowResult[],
+ sourceRows: MappedRow[],
+) {
+ const rowsByNumber = new Map(sourceRows.map((row) => [row.rowNumber, row]));
+ const lines = [
+ LOOM_CSV_RESULTS_HEADERS,
+ ...results.map((row) => [
+ String(row.rowNumber),
+ rowsByNumber.get(row.rowNumber)?.loomUrl ?? "",
+ row.userEmail,
+ row.spaceName ?? "",
+ row.success
+ ? "started"
+ : row.error === LOOM_CSV_NOT_ATTEMPTED_MESSAGE
+ ? "not_attempted"
+ : "failed",
+ row.error ?? "",
+ ]),
+ ];
+ return `${lines.map((line) => line.map(formatLoomCsvCell).join(",")).join("\n")}\n`;
+}
+
function parseCsv(text: string, fileName: string): CsvData {
const records = parseLoomCsvRecords(text);
const headers = records[0]?.map((header) => header.trim()) ?? [];
- const rows = records
+ const dataRows = records
.slice(1)
.filter((row) => row.some((cell) => cell.trim().length > 0));
+ const rows = isLoomCsvResultsFile(headers)
+ ? dataRows.map((row) => row.map(readLoomCsvCell))
+ : dataRows;
if (headers.length === 0) {
throw new Error("No CSV headers found.");
@@ -279,6 +363,7 @@ export const ImportLoomPage = ({
const [isCsvImporting, setIsCsvImporting] = useState(false);
const [csvImportProgress, setCsvImportProgress] = useState(0);
const [result, setResult] = useState<LoomCsvImportResult | null>(null);
+ const importRowsRef = useRef<MappedRow[]>([]);
const selectedColumnValues = [
mapping.loomUrl,
@@ -479,27 +564,70 @@ export const ImportLoomPage = ({
if (!activeOrganization || !canImport) return;
+ const orgId = activeOrganization.organization.id;
+ const importRows = readyRows;
+ importRowsRef.current = importRows;
+ let combinedResults: LoomCsvImportRowResult[] = [];
+
setIsCsvImporting(true);
setResult(null);
setCsvImportProgress(0);
try {
- const batches = chunkRows(readyRows, LOOM_CSV_BATCH_SIZE);
- let combinedResults: LoomCsvImportRowResult[] = [];
+ const batches = chunkRows(importRows, LOOM_CSV_BATCH_SIZE);
let blockedError: string | undefined;
for (const [batchIndex, batch] of batches.entries()) {
- const importResult = await importFromLoomCsv({
- orgId: activeOrganization.organization.id,
- rows: batch,
- });
-
- if (importResult.results.length === 0 && importResult.error) {
- blockedError = importResult.error;
- break;
+ let batchResults: LoomCsvImportRowResult[];
+ let retryRows: MappedRow[] = [];
+
+ try {
+ const importResult = await importFromLoomCsv({
+ orgId,
+ rows: batch,
+ });
+
+ if (importResult.results.length === 0 && importResult.error) {
+ blockedError = importResult.error;
+ const message = importResult.error;
+ combinedResults = [
+ ...combinedResults,
+ ...batch.map((row) => buildRowFailure(row, message)),
+ ];
+ setCsvImportProgress(combinedResults.length);
+ break;
+ }
+
+ batchResults = importResult.results;
+ retryRows = retryableBatchRows(batch, batchResults);
+ } catch (error) {
+ const message = importErrorMessage(error);
+ batchResults = batch.map((row) => buildRowFailure(row, message));
+ }
+
+ if (retryRows.length > 0) {
+ await delay(LOOM_CSV_RETRY_DELAY_MS);
+
+ try {
+ const retryResult = await importFromLoomCsv({
+ orgId,
+ rows: retryRows,
+ });
+
+ if (retryResult.results.length > 0) {
+ const retriedByRowNumber = new Map(
+ retryResult.results.map((row) => [row.rowNumber, row]),
+ );
+ batchResults = batchResults.map(
+ (row) => retriedByRowNumber.get(row.rowNumber) ?? row,
+ );
+ }
+ } catch {
+ // Keep the first attempt's failures for this batch.
+ }
}
- combinedResults = [...combinedResults, ...importResult.results];
+ combinedResults = [...combinedResults, ...batchResults];
setCsvImportProgress(combinedResults.length);
setResult(buildCsvImportResult(combinedResults));
@@ -508,7 +636,10 @@ export const ImportLoomPage = ({
}
}
- const finalResult = buildCsvImportResult(combinedResults, blockedError);
+ const finalResult = buildCsvImportResult(
+ withNotAttemptedRows(importRows, combinedResults),
+ blockedError,
+ );
setResult(finalResult);
if (finalResult.importedCount > 0) {
@@ -526,12 +657,30 @@ export const ImportLoomPage = ({
setConfirmOpen(false);
} catch {
+ setResult(
+ buildCsvImportResult(withNotAttemptedRows(importRows, combinedResults)),
+ );
toast.error("An unexpected error occurred. Please try again.");
} finally {
setIsCsvImporting(false);
}
};
+ const handleResultsDownload = () => {
+ if (!result) return;
+
+ const blob = new Blob(
+ [buildResultsCsv(result.results, importRowsRef.current)],
+ { type: "text/csv;charset=utf-8" },
+ );
+ const url = URL.createObjectURL(blob);
+ const link = document.createElement("a");
+ link.href = url;
+ link.download = "cap-loom-import-results.csv";
+ link.click();
+ URL.revokeObjectURL(url);
+ };
+
return (
<div className="flex flex-col w-full h-full">
<div className="mb-8">
@@ -1006,7 +1155,7 @@ export const ImportLoomPage = ({
{pluralize(result.failedCount, "failed", "failed")}
</p>
</div>
- <div className="flex gap-2 items-center">
+ <div className="flex flex-wrap gap-2 items-center">
<StatusPill
ready
label={`${result.importedCount} started`}
@@ -1017,6 +1166,17 @@ export const ImportLoomPage = ({
label={`${result.failedCount} failed`}
/>
)}
+ <Button
+ type="button"
+ variant="white"
+ size="sm"
+ onClick={handleResultsDownload}
+ disabled={isCsvImporting}
+ className="flex-shrink-0"
+ >
+ <FontAwesomeIcon className="size-3.5" icon={faDownload} />
+ Download Results
+ </Button>
</div>
</div>
<div className="overflow-hidden">
diff --git a/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainDialog/CustomDomainDialog.tsx b/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainDialog/CustomDomainDialog.tsx
index cc29a590e..477eb93b8 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainDialog/CustomDomainDialog.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainDialog/CustomDomainDialog.tsx
@@ -267,6 +267,22 @@ const CustomDomainDialog = ({
onClose();
}, [handleReset, onClose]);
+ const handleCloseRef = useRef(handleClose);
+ handleCloseRef.current = handleClose;
+
+ useEffect(() => {
+ if (isVerified) {
+ dispatch({ type: "GO_TO_STEP", payload: 2 });
+ }
+ }, [isVerified]);
+
+ useEffect(() => {
+ if (stepState.currentIndex !== 2) return;
+ // Read handleClose via a ref: the parent passes an inline onClose, so depending on it would restart the countdown on every parent render.
+ const timer = setTimeout(() => handleCloseRef.current(), 8000);
+ return () => clearTimeout(timer);
+ }, [stepState.currentIndex]);
+
const checkVerification = useCallback(
(showToasts = true) => {
if (
@@ -343,17 +359,6 @@ const CustomDomainDialog = ({
});
};
- useEffect(() => {
- //if current step is success, close dialog in 8 seconds
- if (stepState.currentIndex === 2) {
- setTimeout(() => {
- handleClose();
- }, 8000);
- } else if (isVerified) {
- handleNext();
- }
- }, [isVerified, stepState.currentIndex, handleClose, handleNext]);
-
return (
<>
{stepState.currentIndex === 2 && (
diff --git a/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainDialog/VerifyStep.tsx b/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainDialog/VerifyStep.tsx
index 5c8eb5d35..3b11cc847 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainDialog/VerifyStep.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainDialog/VerifyStep.tsx
@@ -99,6 +99,14 @@ const VerifyStep = ({
hasRecommendedCNAME && !cnameConfigured && isSubdomain(domain);
const showTXTRecord = hasTXTVerification && !isVerified;
+ const showFallbackRecords =
+ !showTXTRecord &&
+ !showARecord &&
+ !showCNAMERecord &&
+ !aRecordConfigured &&
+ !cnameConfigured;
+ const fallbackIsSubdomain = isSubdomain(domain);
+
const handleCopy = async (text: string, fieldId: string) => {
try {
await navigator.clipboard.writeText(text);
@@ -146,6 +154,13 @@ const VerifyStep = ({
<div className="flex justify-center items-center w-full h-20">
<LoadingSpinner size={36} />
</div>
+ ) : !isVerified && !domainConfig ? (
+ <div className="px-4 py-3 text-center rounded-lg border border-gray-4 bg-gray-2">
+ <p className="text-sm text-gray-11">
+ We couldn't load the DNS configuration for this domain. Use Check
+ Status to try again.
+ </p>
+ </div>
) : (
!isVerified &&
domainConfig && (
@@ -432,6 +447,72 @@ const VerifyStep = ({
</div>
</div>
)}
+
+ {showFallbackRecords && (
+ <div className="overflow-hidden rounded-lg border border-gray-4">
+ <div className="px-4 py-3 border-b bg-gray-2 border-gray-4">
+ <p className="font-medium text-md text-gray-12">
+ {fallbackIsSubdomain
+ ? "CNAME Record Configuration"
+ : "A Record Configuration"}
+ </p>
+ <p className="mt-1 text-sm text-gray-10">
+ Add this record to your domain:
+ </p>
+ </div>
+ <div className="px-4 py-3">
+ <dl className="grid gap-4">
+ <div className="grid grid-cols-[100px,1fr] items-center">
+ <dt className="text-sm font-medium text-gray-12">Type</dt>
+ <dd className="text-sm text-gray-10">
+ {fallbackIsSubdomain ? "CNAME" : "A"}
+ </dd>
+ </div>
+ <div className="grid grid-cols-[100px,1fr] items-center">
+ <dt className="text-sm font-medium text-gray-12">Name</dt>
+ <dd className="text-sm text-gray-10">
+ <code className="px-2 py-1 text-xs rounded bg-gray-4">
+ {fallbackIsSubdomain ? domain.split(".")[0] : "@"}
+ </code>
+ </dd>
+ </div>
+ <div className="grid grid-cols-[100px,1fr] items-center">
+ <dt className="text-sm font-medium text-gray-12">
+ Value
+ </dt>
+ <dd className="text-sm text-gray-10">
+ <div className="flex items-center justify-between gap-1.5 bg-gray-3 px-2 py-1 rounded-lg flex-1 min-w-0 border border-gray-4">
+ <code className="text-xs text-gray-10">
+ {fallbackIsSubdomain
+ ? "cname.vercel-dns.com"
+ : "76.76.21.21"}
+ </code>
+ <button
+ type="button"
+ onClick={() =>
+ handleCopy(
+ fallbackIsSubdomain
+ ? "cname.vercel-dns.com"
+ : "76.76.21.21",
+ "fallback-record",
+ )
+ }
+ className="p-1 rounded-md transition-colors hover:bg-gray-1 shrink-0"
+ title="Copy to clipboard"
+ >
+ {copiedField === "fallback-record" ? (
+ <Check className="size-3.5 text-green-500" />
+ ) : (
+ <Copy className="size-3.5 text-gray-10" />
+ )}
+ </button>
+ </div>
+ </dd>
+ </div>
+ </dl>
+ </div>
+ </div>
+ )}
</div>
)
)}
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
index f267ce433..9d8b91a17 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
@@ -506,7 +506,7 @@ export default async function SharedCapsPage(props: {
.where(
and(
eq(sharedVideos.organizationId, orgId),
- isNull(videos.folderId),
+ isNull(sharedVideos.folderId),
),
),
]);
diff --git a/apps/web/app/(org)/login/form.tsx b/apps/web/app/(org)/login/form.tsx
index 63521755f..e7f9af333 100644
--- a/apps/web/app/(org)/login/form.tsx
+++ b/apps/web/app/(org)/login/form.tsx
@@ -7,6 +7,7 @@ import {
faExclamationCircle,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
+import { captureException } from "@sentry/nextjs";
import { AnimatePresence, motion } from "framer-motion";
import Cookies from "js-cookie";
import { LucideArrowUpRight } from "lucide-react";
@@ -162,19 +163,37 @@ export function LoginForm() {
});
signIn("google", {
...(nextPath ? { callbackUrl: nextPath } : {}),
+ }).catch((error) => {
+ captureException(error, {
+ tags: { auth_provider: "google", auth_surface: "login" },
+ });
+ toast.error(
+ "Could not start sign-in. Check your connection and try again.",
+ );
});
}, [getNextPath]);
const handleAppleSignIn = useCallback(() => {
- const nextPath = getNextPath();
- trackEvent("auth_started", {
- method: "apple",
- is_signup: false,
- auth_surface: "login",
- });
- signIn("apple", {
- ...(nextPath ? { callbackUrl: nextPath } : {}),
- });
+ const startAppleSignIn = () => {
+ const nextPath = getNextPath();
+ trackEvent("auth_started", {
+ method: "apple",
+ is_signup: false,
+ auth_surface: "login",
+ });
+ signIn("apple", {
+ ...(nextPath ? { callbackUrl: nextPath } : {}),
+ }).catch((error) => {
+ captureException(error, {
+ tags: { auth_provider: "apple", auth_surface: "login" },
+ });
+ toast.error(
+ "Could not start sign-in. Check your connection and try again.",
+ { action: { label: "Try again", onClick: startAppleSignIn } },
+ );
+ });
+ };
+ startAppleSignIn();
}, [getNextPath]);
const handleOidcSignIn = useCallback(() => {
diff --git a/apps/web/app/(org)/signup/form.tsx b/apps/web/app/(org)/signup/form.tsx
index ab81a3579..5380dec7c 100644
--- a/apps/web/app/(org)/signup/form.tsx
+++ b/apps/web/app/(org)/signup/form.tsx
@@ -7,6 +7,7 @@ import {
faExclamationCircle,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
+import { captureException } from "@sentry/nextjs";
import { AnimatePresence, motion } from "framer-motion";
import Cookies from "js-cookie";
import { LucideArrowUpRight } from "lucide-react";
@@ -169,6 +170,13 @@ export function SignupForm() {
});
signIn("google", {
...(nextPath ? { callbackUrl: nextPath } : {}),
+ }).catch((error) => {
+ captureException(error, {
+ tags: { auth_provider: "google", auth_surface: "signup" },
+ });
+ toast.error(
+ "Could not start sign-in. Check your connection and try again.",
+ );
});
};
diff --git a/apps/web/app/(site)/(seo)/open-source-screen-recorder/page.tsx b/apps/web/app/(site)/(seo)/open-source-screen-recorder/page.tsx
index 55fe1517d..80fbc4917 100644
--- a/apps/web/app/(site)/(seo)/open-source-screen-recorder/page.tsx
+++ b/apps/web/app/(site)/(seo)/open-source-screen-recorder/page.tsx
@@ -14,14 +14,14 @@ const ogImage = ogImageUrl({
export const metadata: Metadata = {
title: "Open Source Screen Recorder — Free, Private, Self-Hostable | Cap",
description:
- "Cap is the leading open-source screen recorder for Mac and Windows. Audit the code, self-host your recordings, and own your data. MIT-licensed, 4K quality, no watermarks.",
+ "Cap is the leading open-source screen recorder for Mac and Windows. Audit the code, self-host your recordings, and own your data. AGPLv3 and MIT crates, 4K quality, no watermarks.",
alternates: {
canonical: "https://cap.so/open-source-screen-recorder",
},
openGraph: {
title: "Open Source Screen Recorder — Free, Private, Self-Hostable | Cap",
description:
- "Cap is the leading open-source screen recorder for Mac and Windows. MIT-licensed, 4K quality, instant sharing, self-hostable storage. No watermarks, no vendor lock-in.",
+ "Cap is the leading open-source screen recorder for Mac and Windows. AGPLv3 and MIT crates, 4K quality, instant sharing, self-hostable storage. No watermarks, no vendor lock-in.",
url: "https://cap.so/open-source-screen-recorder",
siteName: "Cap",
images: [
@@ -39,7 +39,7 @@ export const metadata: Metadata = {
card: "summary_large_image",
title: "Open Source Screen Recorder — Free, Private, Self-Hostable | Cap",
description:
- "Cap is the leading open-source screen recorder for Mac and Windows. MIT-licensed, 4K quality, instant sharing, self-hostable storage. No watermarks, no vendor lock-in.",
+ "Cap is the leading open-source screen recorder for Mac and Windows. AGPLv3 and MIT crates, 4K quality, instant sharing, self-hostable storage. No watermarks, no vendor lock-in.",
images: [ogImage],
},
};
diff --git a/apps/web/app/(site)/(seo)/self-hosted-screen-recording/page.tsx b/apps/web/app/(site)/(seo)/self-hosted-screen-recording/page.tsx
index d10554e03..9d1232c46 100644
--- a/apps/web/app/(site)/(seo)/self-hosted-screen-recording/page.tsx
+++ b/apps/web/app/(site)/(seo)/self-hosted-screen-recording/page.tsx
@@ -15,7 +15,7 @@ export const metadata: Metadata = {
title:
"Self-Hosted Screen Recording — Own Your Data, No Vendor Lock-In | Cap",
description:
- "Cap lets you self-host screen recordings on your own S3-compatible storage. Connect AWS S3, Cloudflare R2, or MinIO — recordings go directly to your infrastructure. Open source, MIT-licensed, free to use.",
+ "Cap lets you self-host screen recordings on your own S3-compatible storage. Connect AWS S3, Cloudflare R2, or MinIO — recordings go directly to your infrastructure. Open source with most code under AGPLv3, free to use.",
alternates: {
canonical: "https://cap.so/self-hosted-screen-recording",
},
diff --git a/apps/web/app/(site)/blog/[slug]/page.tsx b/apps/web/app/(site)/blog/[slug]/page.tsx
index 1aab643ea..6d9f02811 100644
--- a/apps/web/app/(site)/blog/[slug]/page.tsx
+++ b/apps/web/app/(site)/blog/[slug]/page.tsx
@@ -4,11 +4,13 @@ import type { Metadata } from "next";
import Image from "next/image";
import { notFound } from "next/navigation";
import { MDXRemote } from "next-mdx-remote/rsc";
+import remarkGfm from "remark-gfm";
import { AuthorByline } from "@/components/blog/AuthorByline";
import { BlogTemplate } from "@/components/blog/BlogTemplate";
import { getPostComponents } from "@/components/blog/post-components";
import { ReadyToGetStarted } from "@/components/ReadyToGetStarted";
import { ogImageUrl } from "@/lib/og/url";
+import { parseAuthors } from "@/utils/authors";
import { getBlogPosts } from "@/utils/blog";
import {
getInteractiveBlogContent,
@@ -37,11 +39,13 @@ export async function generateMetadata(
publishedAt: publishedTime,
description,
image,
+ video,
} = post.metadata as {
title: string;
publishedAt: string;
description: string;
image?: string;
+ video?: string;
};
// Posts with real cover art keep it; everything else gets the branded
// dynamic OG image.
@@ -64,6 +68,18 @@ export async function generateMetadata(
url: ogImage,
},
],
+ // Discord, iMessage and Telegram play a silent looping MP4
+ // from og:video; X and LinkedIn ignore it and use the image.
+ ...(video && {
+ videos: [
+ {
+ url: `${buildEnv.NEXT_PUBLIC_WEB_URL}${video}`,
+ type: "video/mp4",
+ width: 1200,
+ height: 630,
+ },
+ ],
+ }),
},
twitter: {
card: "summary_large_image",
@@ -88,11 +104,18 @@ export default async function PostPage(props: PostProps) {
}
const readingTime = calculateReadingTime(post.content);
+ // `layout: feature` in the frontmatter swaps the cover image for a larger
+ // title, the description as a subtitle and the authors up top.
+ const feature = (post.metadata as { layout?: string }).layout === "feature";
+ const publishedAt = parseISO(
+ (post.metadata as { publishedAt?: string }).publishedAt ||
+ new Date().toISOString(),
+ );
return (
<>
<article className="px-5 py-24 mx-auto md:py-40 prose">
- {post.metadata.image && (
+ {post.metadata.image && !feature && (
<div className="relative mb-6 h-[200px] sm:h-[280px] md:h-[345px] w-full rounded-lg overflow-hidden">
<Image
className="object-contain m-0 w-full sm:object-cover"
@@ -106,28 +129,62 @@ export default async function PostPage(props: PostProps) {
)}
<div className="wrapper">
- <header>
- <h1 className="mb-2 font-semibold">{post.metadata.title}</h1>
- <p className="space-x-1 text-xs text-gray-12">
- <span>
- {format(
- parseISO(
- (post.metadata as any).publishedAt ||
- new Date().toISOString(),
- ),
- "MMMM dd, yyyy",
- )}
- </span>
- <span>—</span>
- <span>{readingTime} min read</span>
- </p>
- </header>
- <hr className="my-6" />
+ {feature ? (
+ <header className="not-prose">
+ <h1 className="m-0 text-[40px] font-semibold leading-[1.06] tracking-[-0.03em] text-gray-12 sm:text-[56px]">
+ {post.metadata.title}
+ </h1>
+ <div className="mt-4 max-w-[34rem] text-[19px] leading-[1.5] text-gray-10 sm:text-[21px]">
+ {post.metadata.description}
+ </div>
+ <div className="mt-8 flex flex-wrap items-center justify-between gap-4">
+ <div className="flex items-center gap-2.5">
+ {parseAuthors(
+ "author" in post.metadata ? post.metadata.author : "",
+ ).map((author) => (
+ <span
+ key={author.handle}
+ className="flex items-center gap-2.5"
+ >
+ <Image
+ src={author.image}
+ alt=""
+ width={28}
+ height={28}
+ className="m-0 size-7 rounded-full object-cover"
+ />
+ <span className="text-[14px] font-medium text-gray-12">
+ {author.name}
+ </span>
+ </span>
+ ))}
+ </div>
+ <span className="text-[14px] text-gray-10">
+ {format(publishedAt, "MMM d, yyyy")} · {readingTime} min read
+ </span>
+ </div>
+ </header>
+ ) : (
+ <header>
+ <h1 className="mb-2 font-semibold">{post.metadata.title}</h1>
+ <p className="space-x-1 text-xs text-gray-12">
+ <span>{format(publishedAt, "MMMM dd, yyyy")}</span>
+ <span>—</span>
+ <span>{readingTime} min read</span>
+ </p>
+ </header>
+ )}
+ <hr className={feature ? "my-10" : "my-6"} />
<MDXRemote
source={post.content}
components={getPostComponents(post.slug)}
+ options={
+ feature
+ ? { mdxOptions: { remarkPlugins: [remarkGfm] } }
+ : undefined
+ }
/>
- {"author" in post.metadata && post.metadata.author && (
+ {!feature && "author" in post.metadata && post.metadata.author && (
<AuthorByline authors={post.metadata.author} />
)}
<Share
diff --git a/apps/web/app/api/analytics/track/route.ts b/apps/web/app/api/analytics/track/route.ts
index 9386d1d24..01746e860 100644
--- a/apps/web/app/api/analytics/track/route.ts
+++ b/apps/web/app/api/analytics/track/route.ts
@@ -1,4 +1,5 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { videos, videoUploads } from "@cap/database/schema";
import { provideOptionalAuth, Tinybird } from "@cap/web-backend";
import { CurrentUser, Video } from "@cap/web-domain";
@@ -254,7 +255,11 @@ export async function POST(request: NextRequest) {
),
);
}
- }).pipe(provideOptionalAuth),
+ }).pipe((trackView) =>
+ isCrossSiteCookieWrite(request)
+ ? trackView
+ : provideOptionalAuth(trackView),
+ ),
);
return Response.json({ success: true });
diff --git a/apps/web/app/api/auth/[...nextauth]/route.ts b/apps/web/app/api/auth/[...nextauth]/route.ts
index 2d5c0d65a..6a3449ac9 100644
--- a/apps/web/app/api/auth/[...nextauth]/route.ts
+++ b/apps/web/app/api/auth/[...nextauth]/route.ts
@@ -13,6 +13,11 @@ import { type NextRequest, NextResponse } from "next/server";
import NextAuth from "next-auth";
import { getToken } from "next-auth/jwt";
import { getSafeNextPath } from "@/app/(org)/safe-next";
+import {
+ isEmailCodeSendLimited,
+ isEmailCodeVerifyLimited,
+ normalizeSignInEmail,
+} from "@/lib/email-code-rate-limit";
import { isRateLimited } from "@/lib/rate-limit";
export const dynamic = "force-dynamic";
@@ -56,6 +61,7 @@ async function readSignInBody(request: NextRequest) {
json: payload.json === "true",
callbackUrl:
typeof payload.callbackUrl === "string" ? payload.callbackUrl : null,
+ email: typeof payload.email === "string" ? payload.email : null,
};
}
if (contentType?.includes("application/x-www-form-urlencoded")) {
@@ -63,6 +69,7 @@ async function readSignInBody(request: NextRequest) {
return {
json: body.get("json") === "true",
callbackUrl: body.get("callbackUrl"),
+ email: body.get("email"),
};
}
} catch {
@@ -71,11 +78,50 @@ async function readSignInBody(request: NextRequest) {
return null;
}
+function authErrorUrl(error: string) {
+ const url = new URL("/api/auth/error", serverEnv().WEB_URL);
+ url.searchParams.set("error", error);
+ return url.toString();
+}
+
+async function emailCodeLimitResponse(
+ request: NextRequest,
+ nextauth: string[],
+) {
+ if (nextauth[1] !== "email") return null;
+ if (nextauth[0] === "signin" && request.method === "POST") {
+ const body = await readSignInBody(request);
+ const email = normalizeSignInEmail(body?.email);
+ if (!email || !(await isEmailCodeSendLimited(email, request.headers))) {
+ return null;
+ }
+ const url = authErrorUrl("EmailSignin");
+ return body?.json
+ ? NextResponse.json({ url }, { status: 429 })
+ : NextResponse.redirect(url, 302);
+ }
+ if (nextauth[0] === "callback") {
+ const query = request.nextUrl.searchParams;
+ const email = normalizeSignInEmail(query.get("email"));
+ if (
+ !email ||
+ !query.get("token") ||
+ !(await isEmailCodeVerifyLimited(email, request.headers))
+ ) {
+ return null;
+ }
+ return NextResponse.redirect(authErrorUrl("Verification"), 302);
+ }
+ return null;
+}
+
async function handler(
request: NextRequest,
context: { params: Promise<{ nextauth: string[] }> },
) {
const { nextauth } = await context.params;
+ const limitedResponse = await emailCodeLimitResponse(request, nextauth);
+ if (limitedResponse) return limitedResponse;
if (nextauth[1] !== "workos") {
return NextAuth(request, context, authOptions());
}
diff --git a/apps/web/app/api/desktop/[...route]/root.ts b/apps/web/app/api/desktop/[...route]/root.ts
index 3727d4467..8eb06bd3d 100644
--- a/apps/web/app/api/desktop/[...route]/root.ts
+++ b/apps/web/app/api/desktop/[...route]/root.ts
@@ -12,6 +12,7 @@ import {
import { serverEnv } from "@cap/env";
import {
isProSubscription,
+ isValidStripePlanPriceId,
STRIPE_AVAILABLE,
stripe,
userIsPro,
@@ -546,7 +547,20 @@ app.post(
zValidator(
"json",
z.object({
- priceId: z.string(),
+ priceId: z
+ .string()
+ .refine(
+ (id) =>
+ isValidStripePlanPriceId(
+ id,
+ serverEnv().VERCEL_ENV === "production"
+ ? "production"
+ : "development",
+ ),
+ {
+ message: "Invalid priceId",
+ },
+ ),
platform: z.literal("mobile").optional(),
}),
),
diff --git a/apps/web/app/api/desktop/[...route]/s3Config.ts b/apps/web/app/api/desktop/[...route]/s3Config.ts
index 13b592712..fba622ce6 100644
--- a/apps/web/app/api/desktop/[...route]/s3Config.ts
+++ b/apps/web/app/api/desktop/[...route]/s3Config.ts
@@ -9,6 +9,11 @@ import { and, desc, eq, isNull } from "drizzle-orm";
import { Hono } from "hono";
import { z } from "zod";
import { withAuth } from "@/app/api/utils";
+import {
+ blockedS3EndpointMessage,
+ isDisallowedS3Endpoint,
+ publicOnlyS3Agents,
+} from "@/lib/s3-endpoint-policy";
import {
getAccessibleOrganization,
getManagedOrganizationStorage,
@@ -57,6 +62,11 @@ const getS3ErrorMetadata = (error: unknown) => {
return error.$metadata as { httpStatusCode?: number } | undefined;
};
+const blockedEndpointResponse = {
+ error: blockedS3EndpointMessage,
+ details: "The provided endpoint is not allowed.",
+};
+
app.post(
"/",
zValidator(
@@ -74,6 +84,10 @@ app.post(
const user = c.get("user");
const data = c.req.valid("json");
+ if (await isDisallowedS3Endpoint(data.endpoint)) {
+ return c.json(blockedEndpointResponse, { status: 400 });
+ }
+
try {
const encryptedConfig = {
id: S3Bucket.S3BucketId.make(nanoId()),
@@ -227,6 +241,13 @@ app.post(
const TIMEOUT_MS = 5000; // 5 second timeout
const data = c.req.valid("json");
+ if (await isDisallowedS3Endpoint(data.endpoint)) {
+ return c.json(
+ { ...blockedEndpointResponse, metadata: undefined },
+ { status: 400 },
+ );
+ }
+
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => {
@@ -240,7 +261,10 @@ app.post(
accessKeyId: data.accessKeyId,
secretAccessKey: data.secretAccessKey,
},
- requestHandler: { abortSignal: controller.signal },
+ requestHandler: {
+ abortSignal: controller.signal,
+ ...publicOnlyS3Agents(),
+ },
});
try {
diff --git a/apps/web/app/api/dev-reset-transcript/route.ts b/apps/web/app/api/dev-reset-transcript/route.ts
index c8a1d4ff9..ce7963d94 100644
--- a/apps/web/app/api/dev-reset-transcript/route.ts
+++ b/apps/web/app/api/dev-reset-transcript/route.ts
@@ -1,4 +1,5 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import { videoEdits, videos, videoUploads } from "@cap/database/schema";
import { Storage } from "@cap/web-backend";
@@ -15,7 +16,7 @@ export async function POST(request: NextRequest) {
if (request.headers.get("origin") !== request.nextUrl.origin) {
return Response.json({ error: "same origin required" }, { status: 403 });
}
- const user = await getCurrentUser();
+ const user = isCrossSiteCookieWrite(request) ? null : await getCurrentUser();
if (!user) {
return Response.json({ error: "unauthorized" }, { status: 401 });
}
diff --git a/apps/web/app/api/invite/accept/route.ts b/apps/web/app/api/invite/accept/route.ts
index 02576e7c2..36dc1eca8 100644
--- a/apps/web/app/api/invite/accept/route.ts
+++ b/apps/web/app/api/invite/accept/route.ts
@@ -1,4 +1,5 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoId } from "@cap/database/helpers";
import { enqueueLoopsSync } from "@cap/database/loops/queue";
@@ -17,7 +18,7 @@ import {
} from "@/utils/organization";
export async function POST(request: NextRequest) {
- const user = await getCurrentUser();
+ const user = isCrossSiteCookieWrite(request) ? null : await getCurrentUser();
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
diff --git a/apps/web/app/api/invite/decline/route.ts b/apps/web/app/api/invite/decline/route.ts
index 4bf150e95..7aad9a133 100644
--- a/apps/web/app/api/invite/decline/route.ts
+++ b/apps/web/app/api/invite/decline/route.ts
@@ -1,4 +1,5 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import {
organizationInvites,
@@ -12,7 +13,7 @@ import { and, eq, inArray } from "drizzle-orm";
import { type NextRequest, NextResponse } from "next/server";
export async function POST(request: NextRequest) {
- const user = await getCurrentUser();
+ const user = isCrossSiteCookieWrite(request) ? null : await getCurrentUser();
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
diff --git a/apps/web/app/api/mobile/[...route]/route.ts b/apps/web/app/api/mobile/[...route]/route.ts
index dd56af277..3ebe46202 100644
--- a/apps/web/app/api/mobile/[...route]/route.ts
+++ b/apps/web/app/api/mobile/[...route]/route.ts
@@ -58,6 +58,10 @@ import {
hasPendingAccountDeletion,
} from "@/lib/account-deletion-request";
import { queueDesktopSegmentsFinalization } from "@/lib/desktop-segments-finalization";
+import {
+ isEmailCodeSendLimited,
+ isEmailCodeVerifyLimited,
+} from "@/lib/email-code-rate-limit";
import {
resolveMobileRequestOrigin,
resolveMobileWebResourceUrl,
@@ -472,11 +476,16 @@ const createMobileApiKey = Effect.fn("Mobile.createMobileApiKey")(function* (
const requestEmailSession = Effect.fn("Mobile.requestEmailSession")(function* (
rawEmail: string,
+ headers: Headers,
) {
const email = normalizeEmail(rawEmail);
yield* ensureEmailSignInAllowed(email);
yield* ensureAccountDeletionNotPending({ email });
+ if (yield* Effect.promise(() => isEmailCodeSendLimited(email, headers))) {
+ return yield* Effect.fail(new Mobile.MobileTooManyRequests());
+ }
+
const code = crypto.randomInt(100000, 1000000).toString();
const token = hashEmailCode(code);
const expires = new Date(Date.now() + emailCodeTtlMs);
@@ -513,10 +522,13 @@ const requestEmailSession = Effect.fn("Mobile.requestEmailSession")(function* (
return { success: true as const };
});
-const verifyEmailSession = Effect.fn("Mobile.verifyEmailSession")(function* ({
- email: rawEmail,
- code: rawCode,
-}: (typeof Mobile.MobileEmailSessionVerifyInput)["Type"]) {
+const verifyEmailSession = Effect.fn("Mobile.verifyEmailSession")(function* (
+ {
+ email: rawEmail,
+ code: rawCode,
+ }: (typeof Mobile.MobileEmailSessionVerifyInput)["Type"],
+ headers: Headers,
+) {
const email = normalizeEmail(rawEmail);
const code = rawCode.trim();
@@ -527,6 +539,10 @@ const verifyEmailSession = Effect.fn("Mobile.verifyEmailSession")(function* ({
yield* ensureEmailSignInAllowed(email);
yield* ensureAccountDeletionNotPending({ email });
+ if (yield* Effect.promise(() => isEmailCodeVerifyLimited(email, headers))) {
+ return yield* Effect.fail(new Mobile.MobileTooManyRequests());
+ }
+
const database = yield* Database;
const token = hashEmailCode(code);
const identifier = getMobileEmailVerificationIdentifier(email);
@@ -539,32 +555,21 @@ const verifyEmailSession = Effect.fn("Mobile.verifyEmailSession")(function* ({
if (!verificationToken) return "missing" as const;
- if (verificationToken.expires.valueOf() < Date.now()) {
- await db
- .delete(Db.verificationTokens)
- .where(eq(Db.verificationTokens.identifier, identifier));
- return "expired" as const;
- }
-
- if (verificationToken.token !== token) {
- await db
- .delete(Db.verificationTokens)
- .where(eq(Db.verificationTokens.identifier, identifier));
- return "invalid" as const;
- }
-
- const result = await db
+ const claim = await db
.delete(Db.verificationTokens)
.where(
and(
- eq(Db.verificationTokens.identifier, identifier),
- eq(Db.verificationTokens.token, token),
+ eq(Db.verificationTokens.identifier, verificationToken.identifier),
+ eq(Db.verificationTokens.token, verificationToken.token),
),
);
- return getAffectedRows(result) === 1
- ? ("verified" as const)
- : ("used" as const);
+ if (getAffectedRows(claim) !== 1) return "used" as const;
+ if (verificationToken.expires.valueOf() < Date.now()) {
+ return "expired" as const;
+ }
+ if (verificationToken.token !== token) return "invalid" as const;
+ return "verified" as const;
});
if (verificationStatus !== "verified") {
@@ -2111,6 +2116,22 @@ const createMobileComment = Effect.fn("Mobile.createComment")(function* ({
const id = Comment.CommentId.make(nanoId());
const now = new Date();
const database = yield* Database;
+ if (parentCommentId) {
+ const [parent] = yield* database.use((db) =>
+ db
+ .select({ id: Db.comments.id })
+ .from(Db.comments)
+ .where(
+ and(
+ eq(Db.comments.id, parentCommentId),
+ eq(Db.comments.videoId, videoId),
+ eq(Db.comments.type, "text"),
+ ),
+ )
+ .limit(1),
+ );
+ if (!parent) return yield* Effect.fail(new HttpApiError.NotFound());
+ }
yield* database.use((db) =>
db.insert(Db.comments).values({
id,
@@ -2813,11 +2834,15 @@ const ApiLive = HttpApiBuilder.api(Mobile.MobileApiContract).pipe(
}),
),
)
- .handle("requestEmailSession", ({ payload }) =>
- withMappedErrors(requestEmailSession(payload.email)),
+ .handle("requestEmailSession", ({ payload, request }) =>
+ withMappedErrors(
+ requestEmailSession(payload.email, toHeaders(request.headers)),
+ ),
)
- .handle("verifyEmailSession", ({ payload }) =>
- withMappedErrors(verifyEmailSession(payload)),
+ .handle("verifyEmailSession", ({ payload, request }) =>
+ withMappedErrors(
+ verifyEmailSession(payload, toHeaders(request.headers)),
+ ),
)
.handle("revokeSession", ({ headers }) =>
withMappedErrors(
@@ -2989,6 +3014,11 @@ const ApiLive = HttpApiBuilder.api(Mobile.MobileApiContract).pipe(
),
),
);
+ yield* Effect.sync(() => {
+ revalidatePath("/dashboard/caps");
+ revalidatePath("/dashboard/shared-caps");
+ revalidatePath(`/s/${path.id}`);
+ });
const { cap } = yield* getCapById(path.id, publicOrigin);
return cap;
}),
diff --git a/apps/web/app/api/playlist/route.ts b/apps/web/app/api/playlist/route.ts
index 692038d5c..4a8c3c7f3 100644
--- a/apps/web/app/api/playlist/route.ts
+++ b/apps/web/app/api/playlist/route.ts
@@ -170,8 +170,11 @@ const getPlaylistResponse = (
) =>
Effect.gen(function* () {
const [bucket, customBucket] = yield* Storage.getAccessForVideo(video);
- const isMp4Source =
- video.source.type === "desktopMP4" || video.source.type === "webMP4";
+ const source = Video.Video.getSource(video);
+ const isMp4Source = source instanceof Video.Mp4Source;
+ const mp4Key = isMp4Source
+ ? source.getFileKey()
+ : `${video.ownerId}/${video.id}/result.mp4`;
if (urlParams.videoType === "raw-preview") {
const rawFileKey = yield* resolveRawPreviewKey(video);
@@ -364,8 +367,7 @@ const getPlaylistResponse = (
if (bucket.provider === "s3" && Option.isNone(customBucket)) {
let redirect = `${video.ownerId}/${video.id}/combined-source/stream.m3u8`;
- if (isMp4Source || urlParams.videoType === "mp4")
- redirect = `${video.ownerId}/${video.id}/result.mp4`;
+ if (isMp4Source || urlParams.videoType === "mp4") redirect = mp4Key;
else if (video.source.type === "MediaConvert")
redirect = `${video.ownerId}/${video.id}/output/video_recording_000.m3u8`;
@@ -443,11 +445,9 @@ const getPlaylistResponse = (
},
});
} else if (isMp4Source) {
- yield* Effect.log(
- `Returning path ${`${video.ownerId}/${video.id}/result.mp4`}`,
- );
+ yield* Effect.log(`Returning path ${mp4Key}`);
return yield* bucket
- .getSignedObjectUrl(`${video.ownerId}/${video.id}/result.mp4`)
+ .getSignedObjectUrl(mp4Key)
.pipe(Effect.map(HttpServerResponse.redirect));
}
diff --git a/apps/web/app/api/settings/billing/guest-checkout/route.ts b/apps/web/app/api/settings/billing/guest-checkout/route.ts
index 6726ae711..5f0813e34 100644
--- a/apps/web/app/api/settings/billing/guest-checkout/route.ts
+++ b/apps/web/app/api/settings/billing/guest-checkout/route.ts
@@ -1,6 +1,7 @@
import { serverEnv } from "@cap/env";
-import { stripe } from "@cap/utils";
+import { isValidStripePlanPriceId, stripe } from "@cap/utils";
import type { NextRequest } from "next/server";
+import { parseCheckoutQuantity } from "@/lib/checkout-quantity";
import { getCheckoutRedirectUrls } from "@/lib/mobile-checkout";
import { trackServerEvent } from "@/lib/server-analytics";
@@ -11,9 +12,22 @@ export async function POST(request: NextRequest) {
console.log("Received guest checkout request:", { priceId, quantity });
- if (!priceId) {
- console.error("Missing required priceId");
- return Response.json({ error: "priceId is required" }, { status: 400 });
+ const environment =
+ serverEnv().VERCEL_ENV === "production" ? "production" : "development";
+
+ if (
+ !priceId ||
+ typeof priceId !== "string" ||
+ !isValidStripePlanPriceId(priceId, environment)
+ ) {
+ console.error("Invalid or missing priceId");
+ return Response.json({ error: "Invalid priceId" }, { status: 400 });
+ }
+
+ const safeQuantity = parseCheckoutQuantity(quantity);
+ if (safeQuantity === null) {
+ console.error("Invalid quantity");
+ return Response.json({ error: "Invalid quantity" }, { status: 400 });
}
try {
@@ -23,7 +37,7 @@ export async function POST(request: NextRequest) {
serverEnv().WEB_URL,
);
const checkoutSession = await stripe().checkout.sessions.create({
- line_items: [{ price: priceId, quantity: quantity || 1 }],
+ line_items: [{ price: priceId, quantity: safeQuantity }],
mode: "subscription",
success_url: redirects.successUrl,
cancel_url: redirects.cancelUrl,
@@ -42,7 +56,7 @@ export async function POST(request: NextRequest) {
"guest_checkout_started",
{
price_id: priceId,
- quantity: quantity || 1,
+ quantity: safeQuantity,
platform: checkoutPlatform,
session_id: checkoutSession.id,
},
diff --git a/apps/web/app/api/settings/billing/manage/route.ts b/apps/web/app/api/settings/billing/manage/route.ts
index 8d38fd0ab..f57a48d31 100644
--- a/apps/web/app/api/settings/billing/manage/route.ts
+++ b/apps/web/app/api/settings/billing/manage/route.ts
@@ -1,4 +1,5 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import { users } from "@cap/database/schema";
import { serverEnv } from "@cap/env";
@@ -7,8 +8,8 @@ import { eq } from "drizzle-orm";
import { type NextRequest, NextResponse } from "next/server";
import type Stripe from "stripe";
-export async function POST(_request: NextRequest) {
- const user = await getCurrentUser();
+export async function POST(request: NextRequest) {
+ const user = isCrossSiteCookieWrite(request) ? null : await getCurrentUser();
let customerId = user?.stripeCustomerId;
if (!user) {
diff --git a/apps/web/app/api/settings/billing/subscribe/route.ts b/apps/web/app/api/settings/billing/subscribe/route.ts
index 397662e77..d5c2f2dcf 100644
--- a/apps/web/app/api/settings/billing/subscribe/route.ts
+++ b/apps/web/app/api/settings/billing/subscribe/route.ts
@@ -1,21 +1,42 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import { users } from "@cap/database/schema";
import { serverEnv } from "@cap/env";
-import { stripe, userIsPro } from "@cap/utils";
+import { isValidStripePlanPriceId, stripe, userIsPro } from "@cap/utils";
import { eq } from "drizzle-orm";
import type { NextRequest } from "next/server";
import type Stripe from "stripe";
+import { parseCheckoutQuantity } from "@/lib/checkout-quantity";
import { trackServerEvent } from "@/lib/server-analytics";
export async function POST(request: NextRequest) {
- const user = await getCurrentUser();
+ const user = isCrossSiteCookieWrite(request) ? null : await getCurrentUser();
let customerId = user?.stripeCustomerId;
const { priceId, quantity, isOnBoarding } = await request.json();
- if (!priceId) {
- console.error("Price ID not found");
- return Response.json({ error: true }, { status: 400 });
+ const environment =
+ serverEnv().VERCEL_ENV === "production" ? "production" : "development";
+
+ if (
+ !priceId ||
+ typeof priceId !== "string" ||
+ !isValidStripePlanPriceId(priceId, environment)
+ ) {
+ console.error("Invalid or missing priceId");
+ return Response.json(
+ { error: true, message: "Invalid priceId" },
+ { status: 400 },
+ );
+ }
+
+ const safeQuantity = parseCheckoutQuantity(quantity);
+ if (safeQuantity === null) {
+ console.error("Invalid quantity");
+ return Response.json(
+ { error: true, message: "Invalid quantity" },
+ { status: 400 },
+ );
}
if (!user) {
@@ -65,7 +86,7 @@ export async function POST(request: NextRequest) {
const checkoutSession = await stripe().checkout.sessions.create({
customer: customerId as string,
- line_items: [{ price: priceId, quantity: quantity }],
+ line_items: [{ price: priceId, quantity: safeQuantity }],
mode: "subscription",
success_url: isOnBoarding
? `${serverEnv().WEB_URL}/dashboard/settings/organization?upgrade=true&session_id={CHECKOUT_SESSION_ID}`
@@ -84,7 +105,7 @@ export async function POST(request: NextRequest) {
if (checkoutSession.url) {
trackServerEvent(user.id, "checkout_started", {
price_id: priceId,
- quantity: quantity,
+ quantity: safeQuantity,
platform: "web",
});
diff --git a/apps/web/app/api/settings/user/name/route.ts b/apps/web/app/api/settings/user/name/route.ts
index 1d803d849..a5a349db9 100644
--- a/apps/web/app/api/settings/user/name/route.ts
+++ b/apps/web/app/api/settings/user/name/route.ts
@@ -1,11 +1,12 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import { users } from "@cap/database/schema";
import { eq } from "drizzle-orm";
import type { NextRequest } from "next/server";
export async function POST(request: NextRequest) {
- const user = await getCurrentUser();
+ const user = isCrossSiteCookieWrite(request) ? null : await getCurrentUser();
const { firstName, lastName } = await request.json();
if (!user) {
diff --git a/apps/web/app/api/storage/object/route.ts b/apps/web/app/api/storage/object/route.ts
index b529129d8..c224e911c 100644
--- a/apps/web/app/api/storage/object/route.ts
+++ b/apps/web/app/api/storage/object/route.ts
@@ -28,6 +28,47 @@ const copyHeader = (
if (value) target.set(targetName, value);
};
+const INLINE_CONTENT_TYPES = new Set([
+ "video/mp4",
+ "video/webm",
+ "video/quicktime",
+ "video/x-matroska",
+ "video/x-msvideo",
+ "video/x-m4v",
+ "video/mp2t",
+ "video/mpeg",
+ "video/ogg",
+ "video/3gpp",
+ "audio/mp4",
+ "audio/mpeg",
+ "audio/aac",
+ "audio/webm",
+ "audio/ogg",
+ "image/jpeg",
+ "image/png",
+ "image/gif",
+ "image/webp",
+ "text/vtt",
+ "application/vnd.apple.mpegurl",
+ "application/x-mpegurl",
+ "application/json",
+]);
+
+const setObjectContentHeaders = (
+ headers: Headers,
+ contentType: string | null | undefined,
+) => {
+ headers.set("X-Content-Type-Options", "nosniff");
+ headers.set("Content-Security-Policy", "sandbox; default-src 'none'");
+ const essence = contentType?.split(";")[0]?.trim().toLowerCase();
+ if (contentType && essence && INLINE_CONTENT_TYPES.has(essence)) {
+ headers.set("Content-Type", contentType);
+ return;
+ }
+ headers.set("Content-Type", "application/octet-stream");
+ headers.set("Content-Disposition", "attachment");
+};
+
const asRecord = (value: unknown): Record<string, unknown> | null =>
typeof value === "object" && value !== null
? (value as Record<string, unknown>)
@@ -230,7 +271,7 @@ export async function GET(request: NextRequest) {
headers.set("Accept-Ranges", "bytes");
if (head.ContentLength !== undefined)
headers.set("Content-Length", String(head.ContentLength));
- if (head.ContentType) headers.set("Content-Type", head.ContentType);
+ setObjectContentHeaders(headers, head.ContentType);
return new Response(null, { status: 200, headers });
}
const upstream = identity
@@ -243,7 +284,7 @@ export async function GET(request: NextRequest) {
});
const headers = new Headers(CACHE_CONTROL_HEADERS);
if (identity) headers.set("ETag", identity);
- copyHeader(upstream.headers, headers, "content-type", "Content-Type");
+ setObjectContentHeaders(headers, upstream.headers.get("content-type"));
copyHeader(upstream.headers, headers, "content-length", "Content-Length");
copyHeader(upstream.headers, headers, "content-range", "Content-Range");
copyHeader(upstream.headers, headers, "accept-ranges", "Accept-Ranges");
diff --git a/apps/web/app/api/utils.ts b/apps/web/app/api/utils.ts
index 15897cccd..a7aa9710e 100644
--- a/apps/web/app/api/utils.ts
+++ b/apps/web/app/api/utils.ts
@@ -1,5 +1,6 @@
import { db } from "@cap/database";
import { isBlockedAccountEmail } from "@cap/database/auth/domain-utils";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import {
authApiKeys,
@@ -93,7 +94,7 @@ async function getAuth(c: Context) {
.where(eq(authApiKeys.id, authHeader));
user = res[0]?.users;
if (user && isBlockedAccountEmail(user.email)) user = undefined;
- } else {
+ } else if (!isCrossSiteCookieWrite(c.req.raw)) {
user = await getCurrentUser();
}
diff --git a/apps/web/app/api/v1/[...route]/route.ts b/apps/web/app/api/v1/[...route]/route.ts
index 5e889e3c4..70cbf08f5 100644
--- a/apps/web/app/api/v1/[...route]/route.ts
+++ b/apps/web/app/api/v1/[...route]/route.ts
@@ -126,8 +126,14 @@ import {
} from "@/lib/permissions/roles";
import { normalizePlaybackSpeed } from "@/lib/playback-speed";
import { isRateLimited, RATE_LIMIT_IDS } from "@/lib/rate-limit";
+import {
+ blockedS3EndpointMessage,
+ isDisallowedS3Endpoint,
+ publicOnlyS3Agents,
+} from "@/lib/s3-endpoint-policy";
import { transcribeVideo } from "@/lib/transcribe";
import { startVideoProcessingWorkflow } from "@/lib/video-processing";
+import { isVideoStorageKey } from "@/lib/video-storage-key";
import { isAiGenerationEnabled } from "@/utils/flags";
import {
calculateProSeats,
@@ -1335,6 +1341,9 @@ const resolveAgentS3Config = Effect.fn("Agent.resolveS3Config")(function* (
} catch {
return yield* badRequest(requestId, "S3 endpoint is invalid");
}
+ if (yield* Effect.promise(() => isDisallowedS3Endpoint(endpoint))) {
+ return yield* badRequest(requestId, blockedS3EndpointMessage);
+ }
}
let accessKeyId = payload.accessKeyId.trim();
let secretAccessKey = payload.secretAccessKey.trim();
@@ -1411,6 +1420,7 @@ const testAgentS3Config = Effect.fn("Agent.testS3Config")(function* (
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
},
+ requestHandler: publicOnlyS3Agents(),
});
await client.send(
new HeadBucketCommand({ Bucket: config.bucketName }),
@@ -7038,6 +7048,17 @@ const AgentManagementHandlersLive = HttpApiBuilder.group(
"The upload completion state is unavailable",
);
}
+ if (
+ !isVideoStorageKey(
+ { id: path.id, ownerId: principal.id },
+ acceptedRawFileKey,
+ )
+ ) {
+ return yield* badRequest(
+ requestId,
+ "Upload completion is invalid",
+ );
+ }
if (payload.rawFileKey !== acceptedRawFileKey) {
yield* deleteStoredKeyIfPresent(payload.rawFileKey);
}
@@ -7094,6 +7115,17 @@ const AgentManagementHandlersLive = HttpApiBuilder.group(
};
},
});
+ if (
+ !isVideoStorageKey(
+ { id: state.id, ownerId: state.ownerId },
+ state.rawFileKey,
+ )
+ ) {
+ return yield* badRequest(
+ requestId,
+ "Upload completion is invalid",
+ );
+ }
const processing = yield* Effect.tryPromise(() =>
startVideoProcessingWorkflow({
videoId: state.id,
diff --git a/apps/web/app/api/video/comment/delete/route.ts b/apps/web/app/api/video/comment/delete/route.ts
index 46f671827..66e362f3f 100644
--- a/apps/web/app/api/video/comment/delete/route.ts
+++ b/apps/web/app/api/video/comment/delete/route.ts
@@ -1,4 +1,5 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import { comments } from "@cap/database/schema";
import { Comment } from "@cap/web-domain";
@@ -8,7 +9,7 @@ import type { NextRequest } from "next/server";
import { getHeaders } from "@/utils/helpers";
export async function DELETE(request: NextRequest) {
- const user = await getCurrentUser();
+ const user = isCrossSiteCookieWrite(request) ? null : await getCurrentUser();
const { searchParams } = request.nextUrl;
const commentId = Option.fromNullable(searchParams.get("commentId")).pipe(
Option.map(Comment.CommentId.make),
diff --git a/apps/web/app/api/video/metadata/route.ts b/apps/web/app/api/video/metadata/route.ts
index 38c5ae9be..4b0021c13 100644
--- a/apps/web/app/api/video/metadata/route.ts
+++ b/apps/web/app/api/video/metadata/route.ts
@@ -1,11 +1,12 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import { videos } from "@cap/database/schema";
import { eq } from "drizzle-orm";
import type { NextRequest } from "next/server";
export async function PUT(request: NextRequest) {
- const user = await getCurrentUser();
+ const user = isCrossSiteCookieWrite(request) ? null : await getCurrentUser();
const { videoId, metadata } = await request.json();
const userId = user?.id as string;
@@ -30,12 +31,30 @@ export async function PUT(request: NextRequest) {
return Response.json({ error: true }, { status: 401 });
}
- await db()
- .update(videos)
- .set({
- metadata: metadata,
- })
- .where(eq(videos.id, videoId));
+ if (typeof metadata !== "object" || Array.isArray(metadata)) {
+ return Response.json({ error: true }, { status: 400 });
+ }
+
+ const { agentUpload: _agentUpload, ...clientMetadata } = metadata;
+
+ await db().transaction(async (tx) => {
+ const [locked] = await tx
+ .select({ metadata: videos.metadata })
+ .from(videos)
+ .where(eq(videos.id, videoId))
+ .for("update");
+
+ const storedAgentUpload = locked?.metadata?.agentUpload;
+
+ await tx
+ .update(videos)
+ .set({
+ metadata: storedAgentUpload
+ ? { ...clientMetadata, agentUpload: storedAgentUpload }
+ : clientMetadata,
+ })
+ .where(eq(videos.id, videoId));
+ });
return Response.json(true, { status: 200 });
}
diff --git a/apps/web/app/api/videos/[videoId]/retry-ai/route.ts b/apps/web/app/api/videos/[videoId]/retry-ai/route.ts
index dad6851b7..eb26330ae 100644
--- a/apps/web/app/api/videos/[videoId]/retry-ai/route.ts
+++ b/apps/web/app/api/videos/[videoId]/retry-ai/route.ts
@@ -1,4 +1,5 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import { users, videos } from "@cap/database/schema";
import type { VideoMetadata } from "@cap/database/types";
@@ -12,11 +13,13 @@ const LEGACY_AI_SUMMARY_FALLBACK =
"The AI was unable to generate a proper summary for this content.";
export async function POST(
- _request: Request,
+ request: Request,
props: RouteContext<"/api/videos/[videoId]/retry-ai">,
) {
try {
- const user = await getCurrentUser();
+ const user = isCrossSiteCookieWrite(request)
+ ? null
+ : await getCurrentUser();
if (!user) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
diff --git a/apps/web/app/api/videos/[videoId]/retry-transcription/route.ts b/apps/web/app/api/videos/[videoId]/retry-transcription/route.ts
index f679c7200..00b03f36c 100644
--- a/apps/web/app/api/videos/[videoId]/retry-transcription/route.ts
+++ b/apps/web/app/api/videos/[videoId]/retry-transcription/route.ts
@@ -1,4 +1,5 @@
import { db } from "@cap/database";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import { getCurrentUser } from "@cap/database/auth/session";
import { videos } from "@cap/database/schema";
import type { Video } from "@cap/web-domain";
@@ -6,11 +7,13 @@ import { eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
export async function POST(
- _request: Request,
+ request: Request,
props: RouteContext<"/api/videos/[videoId]/retry-transcription">,
) {
try {
- const user = await getCurrentUser();
+ const user = isCrossSiteCookieWrite(request)
+ ? null
+ : await getCurrentUser();
if (!user) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
diff --git a/apps/web/app/api/webhooks/media-server/multipart/[action]/route.ts b/apps/web/app/api/webhooks/media-server/multipart/[action]/route.ts
index 1a956e072..cf5de3f0d 100644
--- a/apps/web/app/api/webhooks/media-server/multipart/[action]/route.ts
+++ b/apps/web/app/api/webhooks/media-server/multipart/[action]/route.ts
@@ -9,6 +9,10 @@ import { type NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { getProcessingState } from "@/lib/desktop-recording-jobs";
import { getDesktopRecordingOutputKey } from "@/lib/desktop-recording-source";
+import {
+ LOOM_IMPORT_MAX_BYTES,
+ LOOM_IMPORT_PART_BYTES,
+} from "@/lib/loom-import-upload";
import { runPromise } from "@/lib/server";
import { decodeStorageVideo } from "@/lib/video-storage";
@@ -16,6 +20,7 @@ const baseSchema = z.object({
videoId: z.string().min(1),
key: z.string().min(1),
uploadId: z.string().min(1),
+ loomImportAttemptId: z.string().min(1).max(200).optional(),
generation: z
.string()
.regex(/^[a-zA-Z0-9_-]{1,64}$/)
@@ -80,6 +85,36 @@ async function getMultipartContext(
const fenced =
payload.generation !== undefined || payload.attemptId !== undefined;
+ const loom =
+ payload.loomImportAttemptId !== undefined ||
+ video.metadata?.loomImportUpload !== undefined;
+ if (loom) {
+ const receipt = video.metadata?.loomImportUpload;
+ const upload = [receipt?.source, receipt?.output].find(
+ (value) =>
+ value?.key === payload.key && value.uploadId === payload.uploadId,
+ );
+ if (
+ fenced ||
+ !receipt ||
+ receipt.attemptId !== payload.loomImportAttemptId ||
+ (video.metadata?.loomImportRun &&
+ !receipt.attemptId.startsWith(
+ `${video.metadata.loomImportRun.runId}:`,
+ )) ||
+ receipt.ownerId !== video.ownerId ||
+ receipt.organizationId !== video.orgId ||
+ !upload ||
+ !payload.key.startsWith(`${video.ownerId}/${video.id}/`)
+ ) {
+ return {
+ response: NextResponse.json(
+ { error: "Loom import upload is no longer current" },
+ { status: 409 },
+ ),
+ };
+ }
+ }
const expectedKey =
fenced && payload.generation && payload.attemptId
? getDesktopRecordingOutputKey(
@@ -90,7 +125,7 @@ async function getMultipartContext(
)
: `${video.ownerId}/${video.id}/result.mp4`;
if (
- payload.key !== expectedKey ||
+ (!loom && payload.key !== expectedKey) ||
(fenced && (!payload.generation || !payload.attemptId))
) {
return {
@@ -134,7 +169,7 @@ async function getMultipartContext(
};
}
- return { bucket, fenced };
+ return { bucket, fenced, loom };
}
async function readJson(request: NextRequest) {
@@ -168,14 +203,32 @@ export async function POST(
const multipartContext = await getMultipartContext(payload.data);
if ("response" in multipartContext) return multipartContext.response;
+ if (
+ multipartContext.loom &&
+ (payload.data.partNumber >
+ Math.ceil(LOOM_IMPORT_MAX_BYTES / LOOM_IMPORT_PART_BYTES) ||
+ payload.data.contentLength > LOOM_IMPORT_PART_BYTES)
+ ) {
+ return NextResponse.json(
+ { error: "Loom imports are limited to 10 GB" },
+ { status: 400 },
+ );
+ }
- const url = await multipartContext.bucket.multipart
- .getPresignedUploadPartUrl(
- payload.data.key,
- payload.data.uploadId,
- payload.data.partNumber,
- )
- .pipe(runPromise);
+ const partArguments = [
+ payload.data.key,
+ payload.data.uploadId,
+ payload.data.partNumber,
+ ] as const;
+ const url = await (multipartContext.loom
+ ? multipartContext.bucket.multipart.getPresignedUploadPartUrl(
+ ...partArguments,
+ { ContentLength: payload.data.contentLength },
+ )
+ : multipartContext.bucket.multipart.getPresignedUploadPartUrl(
+ ...partArguments,
+ )
+ ).pipe(runPromise);
return NextResponse.json({ url });
}
@@ -191,6 +244,19 @@ export async function POST(
const multipartContext = await getMultipartContext(payload.data);
if ("response" in multipartContext) return multipartContext.response;
+ if (
+ multipartContext.loom &&
+ (payload.data.parts.length >
+ Math.ceil(LOOM_IMPORT_MAX_BYTES / LOOM_IMPORT_PART_BYTES) ||
+ payload.data.parts.reduce((size, part) => size + part.size, 0) >
+ LOOM_IMPORT_MAX_BYTES ||
+ payload.data.parts.some((part) => part.size > LOOM_IMPORT_PART_BYTES))
+ ) {
+ return NextResponse.json(
+ { error: "Loom imports are limited to 10 GB" },
+ { status: 400 },
+ );
+ }
const parts = [...payload.data.parts]
.sort((a, b) => a.partNumber - b.partNumber)
@@ -208,7 +274,9 @@ export async function POST(
const result = await multipartContext.bucket.multipart
.complete(payload.data.key, payload.data.uploadId, {
MultipartUpload: { Parts: parts },
- ...(multipartContext.fenced ? { IfNoneMatch: "*" } : {}),
+ ...(multipartContext.fenced || multipartContext.loom
+ ? { IfNoneMatch: "*" }
+ : {}),
})
.pipe(runPromise);
diff --git a/apps/web/app/api/webhooks/media-server/progress/route.ts b/apps/web/app/api/webhooks/media-server/progress/route.ts
index 9e4a7bf82..35233e295 100644
--- a/apps/web/app/api/webhooks/media-server/progress/route.ts
+++ b/apps/web/app/api/webhooks/media-server/progress/route.ts
@@ -58,6 +58,8 @@ interface ProgressWebhookPayload {
};
}
+const MEDIA_SERVER_SHUTDOWN_MESSAGE = "Server shutting down";
+
function getValidDuration(duration: number) {
return Number.isFinite(duration) && duration > 0 ? duration : undefined;
}
@@ -146,7 +148,9 @@ export async function POST(request: NextRequest) {
}
const isRetryableWorkflowError =
request.nextUrl.searchParams.get("retryable") === "true" &&
- payload.phase === "error";
+ (payload.phase === "error" ||
+ (payload.phase === "cancelled" &&
+ payload.message === MEDIA_SERVER_SHUTDOWN_MESSAGE));
console.log(
"[media-server-webhook] Received progress update for video %s: %s (%d%%)",
diff --git a/apps/web/app/embed/[videoId]/page.tsx b/apps/web/app/embed/[videoId]/page.tsx
index bb19036f3..0ba5dd33c 100644
--- a/apps/web/app/embed/[videoId]/page.tsx
+++ b/apps/web/app/embed/[videoId]/page.tsx
@@ -53,6 +53,8 @@ export async function generateMetadata(
videoId,
name: video.name,
sourceType: video.source.type,
+ width: Option.getOrNull(video.width),
+ height: Option.getOrNull(video.height),
webUrl: buildEnv.NEXT_PUBLIC_WEB_URL,
}),
robots: "index, follow",
diff --git a/apps/web/app/globals.css b/apps/web/app/globals.css
index 446382dd7..ba4568b68 100644
--- a/apps/web/app/globals.css
+++ b/apps/web/app/globals.css
@@ -836,6 +836,29 @@ footer a {
line-height: inherit;
}
+/* Links inside a post's charts take the size of the text around them. */
+.prose .chart-figure a {
+ font-size: inherit !important;
+ line-height: inherit !important;
+ font-weight: inherit !important;
+}
+
+/* react-tweet embeds in a post: put back X's own sizes over the forced prose ones. */
+.prose .x-embed p {
+ font-size: var(--tweet-body-font-size) !important;
+ font-weight: var(--tweet-body-font-weight) !important;
+ line-height: var(--tweet-body-line-height) !important;
+ letter-spacing: normal !important;
+ color: inherit !important;
+}
+
+.prose .x-embed a {
+ font-size: inherit !important;
+ font-weight: inherit !important;
+ line-height: inherit !important;
+ letter-spacing: normal !important;
+}
+
.new-card-style {
background: #ffffff;
border: 1px solid #e7eaf0;
diff --git a/apps/web/app/s/[videoId]/Share.tsx b/apps/web/app/s/[videoId]/Share.tsx
index c747064d8..73e15f178 100644
--- a/apps/web/app/s/[videoId]/Share.tsx
+++ b/apps/web/app/s/[videoId]/Share.tsx
@@ -8,7 +8,7 @@ import clsx from "clsx";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import dynamic from "next/dynamic";
import Image from "next/image";
-import { useSearchParams } from "next/navigation";
+import { useRouter, useSearchParams } from "next/navigation";
import {
Suspense,
startTransition,
@@ -21,13 +21,13 @@ import {
useRef,
useState,
} from "react";
-import {
- getVideoStatus,
- type VideoStatusResult,
-} from "@/actions/videos/get-status";
import type { OrganizationSettings } from "@/app/(org)/dashboard/dashboard-data";
import { SignedImageUrl } from "@/components/SignedImageUrl";
import type { ShareDashboardDestination } from "@/lib/share-dashboard-destination";
+import {
+ VideoStatusNotFoundError,
+ videoStatusQueryOptions,
+} from "@/lib/video-status-query";
import { CaptionProvider } from "./_components/CaptionContext";
import { DashboardBackLink } from "./_components/DashboardBackLink";
import { PlaybackProvider } from "./_components/playback/PlaybackContext";
@@ -238,13 +238,7 @@ const useVideoStatus = (
enabled: boolean = true,
) => {
return useQuery({
- queryKey: ["videoStatus", videoId],
- queryFn: async (): Promise<VideoStatusResult> => {
- const res = await getVideoStatus(videoId);
- if ("success" in res && res.success === false)
- throw new Error("Failed to fetch video status");
- return res as VideoStatusResult;
- },
+ ...videoStatusQueryOptions(videoId),
initialData: initialData
? {
transcriptionStatus:
@@ -260,6 +254,7 @@ const useVideoStatus = (
: undefined,
enabled,
refetchInterval: (query) => {
+ if (query.state.error instanceof VideoStatusNotFoundError) return false;
const data = query.state.data;
if (!data) return 2000;
@@ -365,7 +360,7 @@ export const Share = ({
},
);
- const { data: videoStatus } = useVideoStatus(
+ const { data: videoStatus, error: videoStatusError } = useVideoStatus(
data.id,
{
aiGeneration: aiGenerationAvailable,
@@ -378,6 +373,11 @@ export const Share = ({
},
!isScreenshot,
);
+ const router = useRouter();
+ const videoWasDeleted = videoStatusError instanceof VideoStatusNotFoundError;
+ useEffect(() => {
+ if (videoWasDeleted) router.refresh();
+ }, [videoWasDeleted, router]);
const transcriptionStatus =
videoStatus?.transcriptionStatus || data.transcriptionStatus;
@@ -482,7 +482,10 @@ export const Share = ({
);
}, [data.source.type, searchParams]);
+ const cancelPendingSeek = useRef<(() => void) | null>(null);
+
const handleSeek = useCallback((time: number) => {
+ cancelPendingSeek.current?.();
const v =
playerRef.current ??
(document.querySelector("video") as HTMLVideoElement | null);
@@ -504,19 +507,21 @@ export const Share = ({
seekOnce(time);
return;
}
- let timeoutId: ReturnType<typeof setTimeout> | null = null;
- const handleReady = () => {
- seekOnce(time);
+ // A timeline seek before metadata moves currentTime; don't undo it.
+ const requestedFrom = v.currentTime;
+ const cancel = () => {
v.removeEventListener("canplay", handleReady);
v.removeEventListener("loadedmetadata", handleReady);
- if (timeoutId) clearTimeout(timeoutId);
+ if (cancelPendingSeek.current === cancel)
+ cancelPendingSeek.current = null;
+ };
+ const handleReady = () => {
+ cancel();
+ if (v.currentTime === requestedFrom) seekOnce(time);
};
v.addEventListener("canplay", handleReady, { once: true });
v.addEventListener("loadedmetadata", handleReady, { once: true });
- timeoutId = setTimeout(() => {
- v.removeEventListener("canplay", handleReady);
- v.removeEventListener("loadedmetadata", handleReady);
- }, 3000);
+ cancelPendingSeek.current = cancel;
}, []);
useEffect(() => {
diff --git a/apps/web/app/s/[videoId]/_components/Toolbar.tsx b/apps/web/app/s/[videoId]/_components/Toolbar.tsx
index 84a9ddb5c..64420f05d 100644
--- a/apps/web/app/s/[videoId]/_components/Toolbar.tsx
+++ b/apps/web/app/s/[videoId]/_components/Toolbar.tsx
@@ -13,6 +13,7 @@ import type { VideoData } from "../types";
import { AuthOverlay } from "./AuthOverlayLazy";
import { RecordActionButtons } from "./media-comment/record-actions";
import type { RecordIntentKind } from "./timeline/TimelineComposer";
+import { handleEmojiReaction, REACTIONS } from "./toolbar-reactions";
// Capture and upload code stays out of the page bundle until someone records.
const RecorderSurface = dynamic(
@@ -84,50 +85,59 @@ export const Toolbar = ({
setRecordIntent({ kind, t });
};
- const handleEmojiClick = async (emoji: string) => {
- if (!canReact || !user) return;
- const videoElement = document.querySelector("video") as HTMLVideoElement;
- const currentTime = data.isScreenshot
- ? null
- : videoElement?.currentTime || 0;
- const optimisticComment: CommentType = {
- id: `temp-${Date.now()}` as Comment.CommentId,
- authorId: user.id,
- authorName: user.name,
- authorImage: user.imageUrl,
- content: emoji,
- createdAt: new Date(),
- videoId: data.id,
- parentCommentId: "" as Comment.CommentId,
- type: "emoji",
- timestamp: currentTime,
- updatedAt: new Date(),
- mediaKey: null,
- mediaDuration: null,
- mediaMeta: null,
- sending: true,
- };
+ const handleEmojiClick = (emoji: string) => {
+ handleEmojiReaction({
+ emoji,
+ canReact,
+ user,
+ onRequireAuth: () => setShowAuthOverlay(true),
+ onReact: async (chosenEmoji, authedUser) => {
+ const videoElement = document.querySelector(
+ "video",
+ ) as HTMLVideoElement;
+ const currentTime = data.isScreenshot
+ ? null
+ : videoElement?.currentTime || 0;
+ const optimisticComment: CommentType = {
+ id: `temp-${Date.now()}` as Comment.CommentId,
+ authorId: authedUser.id,
+ authorName: authedUser.name,
+ authorImage: authedUser.imageUrl,
+ content: chosenEmoji,
+ createdAt: new Date(),
+ videoId: data.id,
+ parentCommentId: "" as Comment.CommentId,
+ type: "emoji",
+ timestamp: currentTime,
+ updatedAt: new Date(),
+ mediaKey: null,
+ mediaDuration: null,
+ mediaMeta: null,
+ sending: true,
+ };
- onOptimisticComment?.(optimisticComment);
+ onOptimisticComment?.(optimisticComment);
- try {
- const newCommentData = await newComment({
- content: emoji,
- videoId: data.id,
- authorImage: user.imageUrl,
- parentCommentId: "" as Comment.CommentId,
- type: "emoji",
- timestamp: currentTime,
- });
- startTransition(() => {
- onCommentSuccess?.(newCommentData);
- });
- } catch (error) {
- console.error("Error posting comment:", error);
- } finally {
- setCommentBoxOpen(false);
- setComment("");
- }
+ try {
+ const newCommentData = await newComment({
+ content: chosenEmoji,
+ videoId: data.id,
+ authorImage: authedUser.imageUrl,
+ parentCommentId: "" as Comment.CommentId,
+ type: "emoji",
+ timestamp: currentTime,
+ });
+ startTransition(() => {
+ onCommentSuccess?.(newCommentData);
+ });
+ } catch (error) {
+ console.error("Error posting comment:", error);
+ } finally {
+ setCommentBoxOpen(false);
+ setComment("");
+ }
+ },
+ });
};
const handleCommentSubmit = async () => {
@@ -377,30 +387,3 @@ export const Toolbar = ({
</>
);
};
-
-const REACTIONS = [
- {
- emoji: "😂",
- label: "joy",
- },
- {
- emoji: "😍",
- label: "love",
- },
- {
- emoji: "😮",
- label: "wow",
- },
- {
- emoji: "🙌",
- label: "yay",
- },
- {
- emoji: "👍",
- label: "up",
- },
- {
- emoji: "👎",
- label: "down",
- },
-];
diff --git a/apps/web/app/s/[videoId]/_components/media-comment/CommentMiniPlayer.tsx b/apps/web/app/s/[videoId]/_components/media-comment/CommentMiniPlayer.tsx
index 6c3b55432..e4dfb810d 100644
--- a/apps/web/app/s/[videoId]/_components/media-comment/CommentMiniPlayer.tsx
+++ b/apps/web/app/s/[videoId]/_components/media-comment/CommentMiniPlayer.tsx
@@ -1,7 +1,7 @@
"use client";
import { Dialog, DialogContent, DialogTitle } from "@cap/ui";
-import { useCallback, useEffect, useRef } from "react";
+import { useCallback, useLayoutEffect, useRef } from "react";
import {
MediaPlayer,
MediaPlayerControls,
@@ -70,7 +70,7 @@ export function CommentMiniPlayer({
// The host card can unmount the whole dialog (list re-order, tab switch);
// the interaction is over either way, so settle up like a close would.
- useEffect(
+ useLayoutEffect(
() => () => {
const el = videoRef.current;
if (!el) return;
diff --git a/apps/web/app/s/[videoId]/_components/media-comment/VideoCommentCard.tsx b/apps/web/app/s/[videoId]/_components/media-comment/VideoCommentCard.tsx
index f78901b75..e3e9325dc 100644
--- a/apps/web/app/s/[videoId]/_components/media-comment/VideoCommentCard.tsx
+++ b/apps/web/app/s/[videoId]/_components/media-comment/VideoCommentCard.tsx
@@ -2,7 +2,7 @@
import { motion } from "motion/react";
import dynamic from "next/dynamic";
-import { useCallback, useEffect, useRef, useState } from "react";
+import { useCallback, useLayoutEffect, useRef, useState } from "react";
import { commentMediaUrl } from "@/lib/comment-media";
import { formatMediaDuration } from "./format-media-duration";
import type { MediaComment } from "./media-comment-types";
@@ -98,6 +98,7 @@ export const VideoCommentCard = ({
// losing the slot doesn't linger as a paused frame).
const collapse = useCallback(() => {
videoRef.current?.pause();
+ videoRef.current = null;
stopPainting();
setPlaying(false);
}, [stopPainting]);
@@ -113,7 +114,9 @@ export const VideoCommentCard = ({
// The mini player's own claim takes the slot over when its video mounts,
// and its close is what hands the main video back.
const expand = useCallback(() => {
+ const el = videoRef.current;
collapse();
+ videoRef.current = el;
setExpanded(true);
}, [collapse]);
@@ -121,8 +124,9 @@ export const VideoCommentCard = ({
// callback rather than an effect racing autoPlay.
const attachVideo = useCallback(
(el: HTMLVideoElement | null) => {
- videoRef.current = el;
+ // Keep the claim reachable while the lazy mini player loads.
if (!el) return;
+ videoRef.current = el;
claimPlayback(el, collapse);
suspendMainVideo();
el.addEventListener("ended", () => {
@@ -134,7 +138,7 @@ export const VideoCommentCard = ({
[collapse, paintLoop],
);
- useEffect(
+ useLayoutEffect(
() => () => {
stopPainting();
const el = videoRef.current;
diff --git a/apps/web/app/s/[videoId]/_components/playback/playback-store.ts b/apps/web/app/s/[videoId]/_components/playback/playback-store.ts
index c6e385462..d15ab69e7 100644
--- a/apps/web/app/s/[videoId]/_components/playback/playback-store.ts
+++ b/apps/web/app/s/[videoId]/_components/playback/playback-store.ts
@@ -46,11 +46,12 @@ export function createPlaybackStore(fallbackDuration: number | null = 0) {
/** Pull `currentTime`/`duration` off the element and notify time subscribers. */
const sample = (force = false) => {
+ const previousDuration = duration;
if (element && isFiniteDuration(element.duration)) {
duration = element.duration;
}
const next = element?.currentTime ?? time;
- if (next === time && !force) return;
+ if (next === time && duration === previousDuration && !force) return;
time = next;
emitTime();
};
@@ -174,8 +175,12 @@ export function createPlaybackStore(fallbackDuration: number | null = 0) {
const getPlaying = () => playing;
const setFallbackDuration = (next: number | null) => {
+ const previousDuration = getDuration();
fallback = normalizeDuration(next);
if (!isFiniteDuration(duration)) duration = fallback;
+ if (getDuration() !== previousDuration) {
+ emitTime();
+ }
};
const seek = (target: number) => {
diff --git a/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx b/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx
index 9851431a1..de9a8b253 100644
--- a/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx
+++ b/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx
@@ -25,6 +25,8 @@ import { formatTranscriptAsParagraphs } from "@/lib/transcript-text";
import { normalizeTranscriptCueText } from "@/lib/transcript-vtt";
import type { VideoData } from "../../types";
import { type CaptionLanguage, useCaptionContext } from "../CaptionContext";
+import type { TranscriptEntry } from "../utils/parse-transcript-vtt";
+import { parseTranscriptVTT } from "../utils/parse-transcript-vtt";
interface TranscriptProps {
data: VideoData;
@@ -32,116 +34,6 @@ interface TranscriptProps {
user?: { id: string } | null;
}
-interface TranscriptEntry {
- id: number;
- timestamp: string;
- text: string;
- startTime: number;
- endTime: number;
-}
-
-const parseVTT = (vttContent: string): TranscriptEntry[] => {
- const lines = vttContent.split("\n");
- const entries: TranscriptEntry[] = [];
- let currentEntry: Partial<TranscriptEntry & { startTime: number }> = {};
- let currentId = 0;
-
- const timeToSeconds = (timeStr: string): number | null => {
- const parts = timeStr.split(":");
- if (parts.length !== 3) return null;
-
- const [hoursStr, minutesStr, secondsStr] = parts;
- if (!hoursStr || !minutesStr || !secondsStr) return null;
-
- const hours = parseInt(hoursStr, 10);
- const minutes = parseInt(minutesStr, 10);
- const seconds = parseInt(secondsStr, 10);
-
- if (Number.isNaN(hours) || Number.isNaN(minutes) || Number.isNaN(seconds))
- return null;
-
- return hours * 3600 + minutes * 60 + seconds;
- };
-
- const parseTimestamp = (
- timestamp: string,
- ): { mm_ss: string; totalSeconds: number } | null => {
- const parts = timestamp.split(":");
- if (parts.length !== 3) return null;
-
- const [hoursStr, minutesStr, secondsWithMs] = parts;
- if (!hoursStr || !minutesStr || !secondsWithMs) return null;
-
- const secondsPart = secondsWithMs.split(".")[0];
- if (!secondsPart) return null;
-
- const totalSeconds = timeToSeconds(
- `${hoursStr}:${minutesStr}:${secondsPart}`,
- );
- if (totalSeconds === null) return null;
-
- const fractionPart = secondsWithMs.split(".")[1];
- const fraction = fractionPart ? Number(`0.${fractionPart}`) : 0;
-
- return {
- mm_ss: `${minutesStr}:${secondsPart}`,
- totalSeconds: totalSeconds + (Number.isFinite(fraction) ? fraction : 0),
- };
- };
-
- for (let i = 0; i < lines.length; i++) {
- const line = lines[i];
- if (!line?.trim()) continue;
-
- const trimmedLine = line.trim();
-
- if (trimmedLine === "WEBVTT") continue;
-
- if (/^\d+$/.test(trimmedLine)) {
- currentId = parseInt(trimmedLine, 10);
- continue;
- }
-
- if (trimmedLine.includes("-->")) {
- const [startTimeStr, endTimeStr] = trimmedLine.split(" --> ");
- if (!startTimeStr || !endTimeStr) continue;
-
- const startTimestamp = parseTimestamp(startTimeStr);
- const endTimestamp = parseTimestamp(endTimeStr);
- if (startTimestamp) {
- currentEntry = {
- id: currentId,
- timestamp: startTimestamp.mm_ss,
- startTime: startTimestamp.totalSeconds,
- endTime: endTimestamp?.totalSeconds ?? startTimestamp.totalSeconds,
- };
- }
- continue;
- }
-
- if (currentEntry.timestamp && !currentEntry.text) {
- const textContent =
- trimmedLine.startsWith('"') && trimmedLine.endsWith('"')
- ? trimmedLine.slice(1, -1)
- : trimmedLine;
-
- currentEntry.text = textContent;
- if (
- currentEntry.id !== undefined &&
- currentEntry.timestamp &&
- currentEntry.text &&
- currentEntry.startTime !== undefined
- ) {
- entries.push(currentEntry as TranscriptEntry);
- }
- currentEntry = {};
- }
- }
-
- const sortedEntries = entries.sort((a, b) => a.startTime - b.startTime);
- return sortedEntries;
-};
-
/** Module-level so the last live transcript survives component remounts and
* router refreshes during the live→canonical handoff. */
const lastLiveTranscript = new Map<
@@ -268,7 +160,8 @@ export const Transcript: React.FC<TranscriptProps> = ({ data, onSeek }) => {
const isLiveTranscriptActive =
liveTranscript?.kind === "ready" && liveTranscript.state === "active";
const liveTranscriptData = useMemo(
- () => (liveTranscriptContent ? parseVTT(liveTranscriptContent) : []),
+ () =>
+ liveTranscriptContent ? parseTranscriptVTT(liveTranscriptContent) : [],
[liveTranscriptContent],
);
@@ -313,10 +206,10 @@ export const Transcript: React.FC<TranscriptProps> = ({ data, onSeek }) => {
useEffect(() => {
const vttContent = captionContext.currentVttContent;
if (vttContent) {
- const parsed = parseVTT(vttContent);
+ const parsed = parseTranscriptVTT(vttContent);
setTranscriptData(parsed);
} else if (transcriptContent && selectedLanguage === "original") {
- const parsed = parseVTT(transcriptContent);
+ const parsed = parseTranscriptVTT(transcriptContent);
setTranscriptData(parsed);
}
}, [captionContext.currentVttContent, transcriptContent, selectedLanguage]);
diff --git a/apps/web/app/s/[videoId]/_components/timeline/recording/useCommentRecorder.ts b/apps/web/app/s/[videoId]/_components/timeline/recording/useCommentRecorder.ts
index defaac2df..a22e2fb94 100644
--- a/apps/web/app/s/[videoId]/_components/timeline/recording/useCommentRecorder.ts
+++ b/apps/web/app/s/[videoId]/_components/timeline/recording/useCommentRecorder.ts
@@ -236,6 +236,13 @@ export function useCommentRecorder(options: {
const beginRecording = useCallback(
(stream: MediaStream) => {
+ const videoTrack = stream.getVideoTracks()[0];
+ if (!videoTrack || videoTrack.readyState === "ended") {
+ failRecording(
+ "The recording source ended before recording could start.",
+ );
+ return;
+ }
const hasAudio = stream.getAudioTracks().length > 0;
const pipeline = selectCommentVideoPipeline(hasAudio);
if (!pipeline) {
@@ -265,7 +272,7 @@ export function useCommentRecorder(options: {
// The browser's own "Stop sharing" bar ends the video track; treat it
// as a normal stop.
- stream.getVideoTracks()[0]?.addEventListener(
+ videoTrack.addEventListener(
"ended",
() => {
void stopRef.current?.();
@@ -273,7 +280,14 @@ export function useCommentRecorder(options: {
{ once: true },
);
- recorder.start(200);
+ try {
+ recorder.start(200);
+ } catch {
+ failRecording(
+ "Recording couldn't start. Please choose the source again.",
+ );
+ return;
+ }
startTimer();
setLiveMicSwitchable(mixerRef.current !== null);
updatePhase("recording");
diff --git a/apps/web/app/s/[videoId]/_components/toolbar-reactions.ts b/apps/web/app/s/[videoId]/_components/toolbar-reactions.ts
new file mode 100644
index 000000000..99a1ff379
--- /dev/null
+++ b/apps/web/app/s/[videoId]/_components/toolbar-reactions.ts
@@ -0,0 +1,54 @@
+export interface ReactionDefinition {
+ emoji: string;
+ label: string;
+}
+
+export const REACTIONS: readonly ReactionDefinition[] = [
+ {
+ emoji: "😂",
+ label: "joy",
+ },
+ {
+ emoji: "😍",
+ label: "love",
+ },
+ {
+ emoji: "😮",
+ label: "wow",
+ },
+ {
+ emoji: "🙌",
+ label: "yay",
+ },
+ {
+ emoji: "👍",
+ label: "up",
+ },
+ {
+ emoji: "👎",
+ label: "down",
+ },
+];
+
+export interface HandleEmojiReactionParams<TUser> {
+ emoji: string;
+ canReact: boolean;
+ user: TUser | null | undefined;
+ onRequireAuth: () => void;
+ onReact: (emoji: string, user: TUser) => void | Promise<void>;
+}
+
+export function handleEmojiReaction<TUser>({
+ emoji,
+ canReact,
+ user,
+ onRequireAuth,
+ onReact,
+}: HandleEmojiReactionParams<TUser>): void {
+ if (!canReact) return;
+ if (!user) {
+ onRequireAuth();
+ return;
+ }
+ void onReact(emoji, user);
+}
diff --git a/apps/web/app/s/[videoId]/_components/utils/parse-transcript-vtt.ts b/apps/web/app/s/[videoId]/_components/utils/parse-transcript-vtt.ts
new file mode 100644
index 000000000..d62fccfb4
--- /dev/null
+++ b/apps/web/app/s/[videoId]/_components/utils/parse-transcript-vtt.ts
@@ -0,0 +1,112 @@
+export interface TranscriptEntry {
+ id: number;
+ timestamp: string;
+ text: string;
+ startTime: number;
+ endTime: number;
+}
+
+export const parseTranscriptVTT = (vttContent: string): TranscriptEntry[] => {
+ const lines = vttContent.split("\n");
+ const entries: TranscriptEntry[] = [];
+ let currentEntry: Partial<TranscriptEntry & { startTime: number }> = {};
+ let currentId = 0;
+
+ const timeToSeconds = (timeStr: string): number | null => {
+ const parts = timeStr.split(":");
+ if (parts.length !== 3) return null;
+
+ const [hoursStr, minutesStr, secondsStr] = parts;
+ if (!hoursStr || !minutesStr || !secondsStr) return null;
+
+ const hours = parseInt(hoursStr, 10);
+ const minutes = parseInt(minutesStr, 10);
+ const seconds = parseInt(secondsStr, 10);
+
+ if (Number.isNaN(hours) || Number.isNaN(minutes) || Number.isNaN(seconds))
+ return null;
+
+ return hours * 3600 + minutes * 60 + seconds;
+ };
+
+ const parseTimestamp = (
+ timestamp: string,
+ ): { mm_ss: string; totalSeconds: number } | null => {
+ const parts = timestamp.split(":");
+ if (parts.length !== 3) return null;
+
+ const [hoursStr, minutesStr, secondsWithMs] = parts;
+ if (!hoursStr || !minutesStr || !secondsWithMs) return null;
+
+ const secondsPart = secondsWithMs.split(".")[0];
+ if (!secondsPart) return null;
+
+ const totalSeconds = timeToSeconds(
+ `${hoursStr}:${minutesStr}:${secondsPart}`,
+ );
+ if (totalSeconds === null) return null;
+
+ const fractionPart = secondsWithMs.split(".")[1];
+ const fraction = fractionPart ? Number(`0.${fractionPart}`) : 0;
+
+ return {
+ mm_ss: `${minutesStr}:${secondsPart}`,
+ totalSeconds: totalSeconds + (Number.isFinite(fraction) ? fraction : 0),
+ };
+ };
+
+ for (let i = 0; i < lines.length; i++) {
+ const line = lines[i];
+ if (!line?.trim()) {
+ currentEntry = {};
+ continue;
+ }
+
+ const trimmedLine = line.trim();
+
+ if (trimmedLine === "WEBVTT") continue;
+
+ if (!currentEntry.timestamp && /^\d+$/.test(trimmedLine)) {
+ currentId = parseInt(trimmedLine, 10);
+ continue;
+ }
+
+ if (trimmedLine.includes("-->")) {
+ const [startTimeStr, endTimeStr] = trimmedLine.split(" --> ");
+ if (!startTimeStr || !endTimeStr) continue;
+
+ const startTimestamp = parseTimestamp(startTimeStr);
+ const endTimestamp = parseTimestamp(endTimeStr);
+ if (startTimestamp) {
+ currentEntry = {
+ id: currentId,
+ timestamp: startTimestamp.mm_ss,
+ startTime: startTimestamp.totalSeconds,
+ endTime: endTimestamp?.totalSeconds ?? startTimestamp.totalSeconds,
+ };
+ }
+ continue;
+ }
+
+ if (currentEntry.timestamp && !currentEntry.text) {
+ const textContent =
+ trimmedLine.startsWith('"') && trimmedLine.endsWith('"')
+ ? trimmedLine.slice(1, -1)
+ : trimmedLine;
+
+ currentEntry.text = textContent;
+ if (
+ currentEntry.id !== undefined &&
+ currentEntry.timestamp &&
+ currentEntry.text &&
+ currentEntry.startTime !== undefined
+ ) {
+ entries.push(currentEntry as TranscriptEntry);
+ }
+ currentEntry = {};
+ }
+ }
+
+ const sortedEntries = entries.sort((a, b) => a.startTime - b.startTime);
+ return sortedEntries;
+};
diff --git a/apps/web/app/s/[videoId]/_components/utils/transcript-utils.ts b/apps/web/app/s/[videoId]/_components/utils/transcript-utils.ts
index 14a986e17..97a16a64b 100644
--- a/apps/web/app/s/[videoId]/_components/utils/transcript-utils.ts
+++ b/apps/web/app/s/[videoId]/_components/utils/transcript-utils.ts
@@ -124,13 +124,16 @@ export const parseVTT = (vttContent: string): TranscriptEntry[] => {
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
- if (!line?.trim()) continue;
+ if (!line?.trim()) {
+ currentEntry = {};
+ continue;
+ }
const trimmedLine = line.trim();
if (trimmedLine === "WEBVTT") continue;
- if (/^\d+$/.test(trimmedLine)) {
+ if (!currentEntry.timestamp && /^\d+$/.test(trimmedLine)) {
currentId = parseInt(trimmedLine, 10);
continue;
}
diff --git a/apps/web/app/s/[videoId]/edit/EditVideoClient.tsx b/apps/web/app/s/[videoId]/edit/EditVideoClient.tsx
index e10a88652..8fd02c2be 100644
--- a/apps/web/app/s/[videoId]/edit/EditVideoClient.tsx
+++ b/apps/web/app/s/[videoId]/edit/EditVideoClient.tsx
@@ -1305,6 +1305,8 @@ export function EditVideoClient({
playableRange.end - currentTime <= PREVIEW_CUT_MUTE_LEAD_SECONDS
) {
muteForCut();
+ } else if (playableRange && !videoElement.seeking) {
+ restoreCutAudio();
}
const nextTime =
playableRangeIndex >= 0
diff --git a/apps/web/app/s/[videoId]/edit/TranscriptSidebar.tsx b/apps/web/app/s/[videoId]/edit/TranscriptSidebar.tsx
index dd72e7b39..0c54fcf62 100644
--- a/apps/web/app/s/[videoId]/edit/TranscriptSidebar.tsx
+++ b/apps/web/app/s/[videoId]/edit/TranscriptSidebar.tsx
@@ -240,7 +240,7 @@ export function TranscriptSidebar({
endIndex: number;
} | null>(null);
const [searchQuery, setSearchQuery] = useState("");
- const [searchMatchIndex, setSearchMatchIndex] = useState(0);
+ const [searchMatchIndex, setSearchMatchIndex] = useState(-1);
const [previewFillers, setPreviewFillers] = useState(false);
const deferredSearchQuery = useDeferredValue(searchQuery);
const dragAnchorRef = useRef<number | null>(null);
@@ -465,9 +465,14 @@ export function TranscriptSidebar({
const moveSearchMatch = useCallback(
(direction: -1 | 1) => {
if (searchMatches.length === 0) return;
- const next =
- (searchMatchIndex + direction + searchMatches.length) %
- searchMatches.length;
+ let next: number;
+ if (searchMatchIndex === -1) {
+ next = direction === 1 ? 0 : searchMatches.length - 1;
+ } else {
+ next =
+ (searchMatchIndex + direction + searchMatches.length) %
+ searchMatches.length;
+ }
setSearchMatchIndex(next);
const wordIndex = searchMatches[next];
if (wordIndex === undefined) return;
@@ -666,7 +671,7 @@ export function TranscriptSidebar({
value={searchQuery}
onChange={(event) => {
setSearchQuery(event.target.value);
- setSearchMatchIndex(0);
+ setSearchMatchIndex(-1);
}}
onKeyDown={(event) => {
if (event.key === "Enter") {
@@ -678,7 +683,7 @@ export function TranscriptSidebar({
event.preventDefault();
event.stopPropagation();
setSearchQuery("");
- setSearchMatchIndex(0);
+ setSearchMatchIndex(-1);
}
}}
placeholder="Search transcript"
@@ -687,8 +692,10 @@ export function TranscriptSidebar({
{searchQuery && (
<>
<span className="shrink-0 text-[10px] font-medium tabular-nums text-gray-9">
- {searchMatches.length === 0 ? 0 : searchMatchIndex + 1}/
- {searchMatches.length}
+ {searchMatches.length === 0
+ ? 0
+ : Math.max(searchMatchIndex + 1, 1)}
+ /{searchMatches.length}
</span>
<button
type="button"
diff --git a/apps/web/app/s/[videoId]/page.tsx b/apps/web/app/s/[videoId]/page.tsx
index d9e6ab944..d54403b87 100644
--- a/apps/web/app/s/[videoId]/page.tsx
+++ b/apps/web/app/s/[videoId]/page.tsx
@@ -289,6 +289,8 @@ export async function generateMetadata(
videoId,
name: video.name,
sourceType: video.source.type,
+ width: Option.getOrNull(video.width),
+ height: Option.getOrNull(video.height),
webUrl,
canonicalWebUrl: buildEnv.NEXT_PUBLIC_WEB_URL,
advertiseIframelyPlayer: shouldAdvertiseIframelyPlayer,
diff --git a/apps/web/components/blog/native/BeforeAfter.tsx b/apps/web/components/blog/native/BeforeAfter.tsx
new file mode 100644
index 000000000..21f3d35ab
--- /dev/null
+++ b/apps/web/components/blog/native/BeforeAfter.tsx
@@ -0,0 +1,426 @@
+"use client";
+
+import clsx from "clsx";
+import { useDetectPlatform } from "hooks/useDetectPlatform";
+import { useReducedMotion } from "motion/react";
+import Image from "next/image";
+import {
+ type KeyboardEvent,
+ type PointerEvent,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
+
+/**
+ * The cursor is drawn the way Cap's editor renders a recorded cursor
+ * (crates/rendering: cursor_interpolation.rs, layers/cursor.rs and
+ * shaders/cursor.wgsl), with the editor's defaults.
+ */
+type Spring = { tension: number; mass: number; friction: number };
+/** "Mellow", the default cursor smoothing. */
+const MELLOW: Spring = { tension: 470, mass: 3, friction: 70 };
+/** Used while the primary button is held. */
+const DRAG: Spring = { tension: 1000, mass: 1, friction: 40 };
+
+/** 60px per 1080px of recording, which on the Mac these windows were
+ * captured on comes to this fraction of the window's width. */
+const CURSOR_HEIGHT_PER_WINDOW_WIDTH = 0.165;
+const CLICK_DURATION = 0.13;
+const CLICK_SHRINK = 0.8;
+const TILT_LOOKBACK = 0.4;
+const ROTATION_AMOUNT = 0.15;
+const RESOLUTION_BASE = 1920;
+const MAX_TILT_DEG = 20;
+const MOTION_BLUR = 1;
+const BLUR_TAPS = 21;
+/** 480px of a 1920px frame. */
+const MAX_SMEAR = 0.25;
+
+/** Where the arrow's tip sits inside each cursor's viewBox. */
+const CURSOR = {
+ macos: {
+ src: "/blog/gpui/cursor-macos.svg",
+ width: 17,
+ height: 24,
+ tipX: 3.6,
+ tipY: 3.2,
+ },
+ windows: {
+ src: "/blog/gpui/cursor-windows.svg",
+ width: 18,
+ height: 25,
+ tipX: 3.3,
+ tipY: 3.75,
+ },
+} as const;
+
+const SIDES = [
+ {
+ key: "before",
+ version: "0.6.0",
+ stack: "Tauri",
+ window: "/blog/gpui/main-window-060-glass.png",
+ height: 790,
+ },
+ {
+ key: "after",
+ version: "0.6.1",
+ stack: "GPUI",
+ window: "/blog/gpui/main-window-061-glass.png",
+ height: 872,
+ },
+] as const;
+
+const WINDOW_WIDTH = 660;
+
+/** Closed-form step of a damped spring, as in spring_mass_damper.rs. Both
+ * profiles above are underdamped, so that's the only case needed. */
+function springStep(
+ spring: Spring,
+ displacement: number,
+ velocity: number,
+ t: number,
+) {
+ const omega0 = Math.sqrt(spring.tension / spring.mass);
+ const zeta = spring.friction / (2 * Math.sqrt(spring.tension * spring.mass));
+ const omegaD = omega0 * Math.sqrt(1 - zeta * zeta);
+ const decay = Math.exp(-zeta * omega0 * t);
+ const cos = Math.cos(omegaD * t);
+ const sin = Math.sin(omegaD * t);
+ const a = displacement;
+ const b = (velocity + a * zeta * omega0) / omegaD;
+ return [
+ decay * (a * cos + b * sin),
+ decay *
+ ((b * omegaD - a * zeta * omega0) * cos -
+ (a * omegaD + b * zeta * omega0) * sin),
+ ] as const;
+}
+
+const smoothstep = (t: number) => t * t * (3 - 2 * t);
+
+export function BeforeAfter() {
+ const stageRef = useRef<HTMLDivElement>(null);
+ const afterRef = useRef<HTMLDivElement>(null);
+ const dividerRef = useRef<HTMLDivElement>(null);
+ const windowRef = useRef<HTMLImageElement>(null);
+ const canvasRef = useRef<HTMLCanvasElement>(null);
+ const spriteRef = useRef<HTMLCanvasElement | null>(null);
+ const kickRef = useRef<() => void>(() => {});
+ const sim = useRef({
+ target: { x: 0.5, y: 0.5 },
+ pos: { x: 0.5, y: 0.5 },
+ vel: { x: 0, y: 0 },
+ pressed: false,
+ click: 0,
+ history: [] as { t: number; x: number }[],
+ });
+
+ const reduceMotion = useReducedMotion() ?? false;
+ const reduceRef = useRef(reduceMotion);
+ reduceRef.current = reduceMotion;
+ const { platform } = useDetectPlatform();
+ const cursor = CURSOR[platform === "windows" ? "windows" : "macos"];
+ const cursorRef = useRef(cursor);
+ cursorRef.current = cursor;
+
+ const [hovering, setHovering] = useState(false);
+
+ useEffect(() => {
+ let cancelled = false;
+ const image = new window.Image();
+ image.src = cursor.src;
+ image
+ .decode()
+ .then(() => {
+ if (cancelled) return;
+ const sprite = document.createElement("canvas");
+ sprite.height = 256;
+ sprite.width = Math.round((256 * cursor.width) / cursor.height);
+ sprite
+ .getContext("2d")
+ ?.drawImage(image, 0, 0, sprite.width, sprite.height);
+ spriteRef.current = sprite;
+ kickRef.current();
+ })
+ .catch(() => {});
+ return () => {
+ cancelled = true;
+ };
+ }, [cursor]);
+
+ useEffect(() => {
+ const stage = stageRef.current;
+ const after = afterRef.current;
+ const divider = dividerRef.current;
+ const canvas = canvasRef.current;
+ const ctx = canvas?.getContext("2d");
+ if (!stage || !after || !divider || !canvas || !ctx) return;
+
+ const s = sim.current;
+ let width = 0;
+ let height = 0;
+ let windowWidth = 0;
+ let dpr = 1;
+ let tilt = 0;
+ let raf = 0;
+ let last = 0;
+
+ const draw = () => {
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ const sprite = spriteRef.current;
+ if (!sprite || reduceRef.current) return;
+ const { width: vw, height: vh, tipX, tipY } = cursorRef.current;
+
+ const scale = 1 - (1 - CLICK_SHRINK) * smoothstep(s.click);
+ const h = windowWidth * CURSOR_HEIGHT_PER_WINDOW_WIDTH * scale;
+ const w = (h * vw) / vh;
+ const x = s.pos.x * width - (tipX / vw) * w;
+ const y = s.pos.y * height - (tipY / vh) * h;
+
+ // The smear is one frame's travel at 60fps, trailing the motion.
+ let mx = (s.vel.x * width * MOTION_BLUR) / 60;
+ let my = (s.vel.y * height * MOTION_BLUR) / 60;
+ const length = Math.hypot(mx, my);
+ if (length > MAX_SMEAR * width) {
+ mx *= (MAX_SMEAR * width) / length;
+ my *= (MAX_SMEAR * width) / length;
+ }
+ const taps = length < 0.5 ? 1 : BLUR_TAPS;
+
+ // Adding up equal-weight copies gives the shader's box filter.
+ ctx.globalCompositeOperation = taps > 1 ? "lighter" : "source-over";
+ ctx.globalAlpha = 1 / taps;
+ ctx.imageSmoothingQuality = "high";
+ for (let i = 0; i < taps; i++) {
+ const f = taps > 1 ? i / (taps - 1) : 0;
+ ctx.setTransform(
+ dpr,
+ 0,
+ 0,
+ dpr,
+ (x - mx * f) * dpr,
+ (y - my * f) * dpr,
+ );
+ // The shader tilts around the sprite's top-left corner.
+ ctx.rotate((-tilt * Math.PI) / 180);
+ ctx.drawImage(sprite, 0, 0, w, h);
+ }
+ };
+
+ const frame = (now: number) => {
+ raf = 0;
+ const t = now / 1000;
+ const dt = Math.min(0.1, Math.max(0, t - last));
+ last = t;
+
+ let settled = true;
+ for (const axis of ["x", "y"] as const) {
+ if (reduceRef.current) {
+ s.pos[axis] = s.target[axis];
+ s.vel[axis] = 0;
+ continue;
+ }
+ const [d, v] = springStep(
+ s.pressed ? DRAG : MELLOW,
+ s.pos[axis] - s.target[axis],
+ s.vel[axis],
+ dt,
+ );
+ s.pos[axis] = s.target[axis] + d;
+ s.vel[axis] = v;
+ if (Math.abs(d) > 1e-5 || Math.abs(v) > 1e-4) settled = false;
+ }
+
+ s.click = Math.min(
+ 1,
+ Math.max(0, s.click + ((s.pressed ? 1 : -1) * dt) / CLICK_DURATION),
+ );
+ if (s.click !== (s.pressed ? 1 : 0)) settled = false;
+
+ // Tilt follows how far the cursor moved sideways in the last 0.4s.
+ const history = s.history;
+ history.push({ t, x: s.pos.x });
+ while (history.length > 2 && (history[1]?.t ?? t) <= t - TILT_LOOKBACK)
+ history.shift();
+ const [a = { t, x: s.pos.x }, b] = history;
+ const past =
+ b && b.t > a.t && a.t < t - TILT_LOOKBACK
+ ? a.x + ((b.x - a.x) * (t - TILT_LOOKBACK - a.t)) / (b.t - a.t)
+ : a.x;
+ tilt = Math.min(
+ MAX_TILT_DEG,
+ Math.max(
+ -MAX_TILT_DEG,
+ (s.pos.x - past) * RESOLUTION_BASE * 0.03 * ROTATION_AMOUNT,
+ ),
+ );
+ if (Math.abs(tilt) > 0.01) settled = false;
+
+ const split = `${s.pos.x * 100}%`;
+ after.style.clipPath = `inset(0 0 0 ${split})`;
+ divider.style.left = split;
+ stage.setAttribute("aria-valuenow", String(Math.round(s.target.x * 100)));
+ draw();
+
+ if (!settled) raf = requestAnimationFrame(frame);
+ };
+
+ kickRef.current = () => {
+ if (raf) return;
+ last = performance.now() / 1000;
+ s.history.push({ t: last, x: s.pos.x });
+ raf = requestAnimationFrame(frame);
+ };
+
+ const measure = () => {
+ const rect = stage.getBoundingClientRect();
+ width = rect.width;
+ height = rect.height;
+ dpr = window.devicePixelRatio || 1;
+ windowWidth = windowRef.current?.offsetWidth || width * 0.31;
+ canvas.width = Math.round(width * dpr);
+ canvas.height = Math.round(height * dpr);
+ draw();
+ };
+ const observer = new ResizeObserver(measure);
+ observer.observe(stage);
+ measure();
+
+ return () => {
+ observer.disconnect();
+ cancelAnimationFrame(raf);
+ kickRef.current = () => {};
+ };
+ }, []);
+
+ const point = (event: PointerEvent<HTMLDivElement>) => {
+ const rect = stageRef.current?.getBoundingClientRect();
+ if (!rect) return null;
+ return {
+ x: Math.min(1, Math.max(0, (event.clientX - rect.left) / rect.width)),
+ y: Math.min(1, Math.max(0, (event.clientY - rect.top) / rect.height)),
+ };
+ };
+
+ const move = (event: PointerEvent<HTMLDivElement>) => {
+ const next = point(event);
+ if (!next) return;
+ sim.current.target = next;
+ kickRef.current();
+ };
+
+ const release = () => {
+ sim.current.pressed = false;
+ kickRef.current();
+ };
+
+ const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
+ const step = event.shiftKey ? 0.2 : 0.05;
+ const target = sim.current.target;
+ if (event.key === "ArrowLeft") target.x = Math.max(0, target.x - step);
+ else if (event.key === "ArrowRight")
+ target.x = Math.min(1, target.x + step);
+ else return;
+ event.preventDefault();
+ kickRef.current();
+ };
+
+ const showCursor = hovering && !reduceMotion;
+
+ return (
+ <figure className="not-prose relative left-1/2 my-14 w-[min(94vw,68rem)] -translate-x-1/2 sm:my-20">
+ <div
+ ref={stageRef}
+ role="slider"
+ tabIndex={0}
+ aria-label="Compare Cap 0.6.0 and Cap 0.6.1"
+ aria-valuemin={0}
+ aria-valuemax={100}
+ aria-valuenow={50}
+ aria-valuetext="Left shows 0.6.0 on Tauri, right shows 0.6.1 on GPUI"
+ onKeyDown={onKeyDown}
+ onPointerEnter={(event) => {
+ if (event.pointerType !== "mouse") return;
+ // Arrive from the split line at the pointer's height.
+ const next = point(event);
+ if (next) {
+ sim.current.pos.y = next.y;
+ sim.current.vel.y = 0;
+ }
+ setHovering(true);
+ move(event);
+ }}
+ onPointerMove={(event) => {
+ if (event.pointerType === "mouse" || event.buttons > 0) move(event);
+ }}
+ onPointerDown={(event) => {
+ sim.current.pressed = true;
+ move(event);
+ }}
+ onPointerUp={release}
+ onPointerCancel={release}
+ onPointerLeave={(event) => {
+ release();
+ if (event.pointerType === "mouse") setHovering(false);
+ }}
+ className={clsx(
+ "relative aspect-[4/3] w-full touch-pan-y select-none overflow-hidden rounded-[20px] bg-[#E6E8EC] sm:rounded-[24px] outline-none ring-blue-9/60 ring-offset-4 focus-visible:ring-2 sm:aspect-[16/9]",
+ showCursor && "cursor-none",
+ )}
+ >
+ {SIDES.map((side) => (
+ <div
+ key={side.key}
+ ref={side.key === "after" ? afterRef : undefined}
+ className="absolute inset-0"
+ style={
+ side.key === "after"
+ ? { clipPath: "inset(0 0 0 50%)" }
+ : undefined
+ }
+ >
+ {/* The windows were captured over this painting, placed the same
+ way, so the glass is tinted by what's really behind it. */}
+ <Image
+ src="/backgrounds/miami.webp"
+ alt=""
+ fill
+ sizes="(min-width: 1200px) 68rem, 94vw"
+ draggable={false}
+ className="object-cover"
+ />
+ <Image
+ ref={side.key === "before" ? windowRef : undefined}
+ src={side.window}
+ alt={`Cap's main window in ${side.version}, built with ${side.stack}`}
+ width={WINDOW_WIDTH}
+ height={side.height}
+ sizes="(min-width: 1200px) 26rem, 40vw"
+ draggable={false}
+ className="absolute left-1/2 top-[9%] h-auto w-[40%] -translate-x-1/2 drop-shadow-[0_24px_36px_rgba(20,28,45,0.28)] sm:top-[9%] sm:w-[31%]"
+ />
+ </div>
+ ))}
+
+ <div
+ ref={dividerRef}
+ aria-hidden="true"
+ className="pointer-events-none absolute inset-y-0 left-1/2 w-px -translate-x-1/2 bg-white shadow-[0_0_0_0.5px_rgba(0,0,0,0.12)]"
+ />
+
+ <canvas
+ ref={canvasRef}
+ className="pointer-events-none absolute inset-0 size-full transition-opacity duration-150"
+ style={{ opacity: showCursor ? 1 : 0 }}
+ />
+ </div>
+ <figcaption className="mx-auto mt-4 max-w-[40rem] px-2 text-center text-[14px] leading-relaxed text-gray-10">
+ Cap's main window in 0.6.0 on the left and 0.6.1 on the right. Move
+ across it to compare.
+ </figcaption>
+ </figure>
+ );
+}
diff --git a/apps/web/components/blog/native/CodeCompare.tsx b/apps/web/components/blog/native/CodeCompare.tsx
new file mode 100644
index 000000000..fe14e9a42
--- /dev/null
+++ b/apps/web/components/blog/native/CodeCompare.tsx
@@ -0,0 +1,86 @@
+import clsx from "clsx";
+import type { ReactNode } from "react";
+import { htMono } from "../../pages/HomeTwo/fonts";
+import { FadeIn } from "./shared";
+
+const mono = htMono.className;
+
+const SOLID = `<KSwitch.Control
+ class="w-9 h-5 p-0.5 rounded-full bg-ed-ctl-active
+ data-checked:bg-ed-accent"
+>
+ <KSwitch.Thumb
+ class="size-4 rounded-full bg-white
+ data-checked:translate-x-[calc(100%)]"
+ />
+</KSwitch.Control>`;
+
+const GPUI = `div()
+ .w(px(34.)).h(px(20.)).p(px(2.))
+ .flex().rounded_full()
+ .when(checked, |this| this.justify_end())
+ .bg(if checked { on_fill } else { off_fill })
+ .child(
+ div().size(px(16.)).rounded_full().bg(white()),
+ )`;
+
+const TOKEN =
+ /(?<string>"[^"]*")|(?<tag><\/?[A-Z][\w.]*|\/?>)|(?<keyword>\b(?:if|else)\b)|(?<number>\b\d+(?:\.\d*)?)|(?<call>\b[a-z_]\w*(?=\())|(?<attr>\bclass(?==))/g;
+
+const COLORS: Record<string, string> = {
+ string: "#0B7A3E",
+ tag: "#8250DF",
+ keyword: "#CF222E",
+ number: "#0550AE",
+ call: "#6639BA",
+ attr: "#0550AE",
+};
+
+function highlight(code: string) {
+ const parts: ReactNode[] = [];
+ let cursor = 0;
+ for (const match of code.matchAll(TOKEN)) {
+ const index = match.index ?? 0;
+ if (index > cursor) parts.push(code.slice(cursor, index));
+ const kind = Object.entries(match.groups ?? {}).find(
+ ([, value]) => value !== undefined,
+ )?.[0];
+ parts.push(
+ <span key={index} style={{ color: kind ? COLORS[kind] : undefined }}>
+ {match[0]}
+ </span>,
+ );
+ cursor = index + match[0].length;
+ }
+ parts.push(code.slice(cursor));
+ return parts;
+}
+
+const FILES = [
+ { name: "Toggle.tsx", detail: "0.6.0, SolidJS and Tailwind", code: SOLID },
+ { name: "toggle.rs", detail: "0.6.1, GPUI", code: GPUI },
+];
+
+/** The same toggle switch in the old app and the new one, side by side. */
+export function CodeCompare() {
+ return (
+ <FadeIn className="not-prose relative left-1/2 my-12 grid w-[min(calc(100vw-2.5rem),56rem)] -translate-x-1/2 gap-6 md:grid-cols-2 md:gap-4">
+ {FILES.map((file) => (
+ <div key={file.name} className="flex min-w-0 flex-col">
+ <div className="mb-2.5 flex items-baseline gap-2 px-1 text-[13px]">
+ <span className={clsx(mono, "text-gray-12")}>{file.name}</span>
+ <span className="text-gray-10">{file.detail}</span>
+ </div>
+ <pre
+ className={clsx(
+ mono,
+ "m-0 flex-1 overflow-x-auto rounded-[14px] bg-gray-2 px-5 py-4 text-[12.5px] leading-[1.75] text-gray-12 shadow-[inset_0_0_0_1px_rgba(0,0,0,0.05)]",
+ )}
+ >
+ <code>{highlight(file.code)}</code>
+ </pre>
+ </div>
+ ))}
+ </FadeIn>
+ );
+}
diff --git a/apps/web/components/blog/native/ExportRace.tsx b/apps/web/components/blog/native/ExportRace.tsx
new file mode 100644
index 000000000..58833b88c
--- /dev/null
+++ b/apps/web/components/blog/native/ExportRace.tsx
@@ -0,0 +1,116 @@
+"use client";
+
+import { type ReactNode, useState } from "react";
+import { BarRow, Chart, Headline, Tabs } from "./shared";
+
+type Scenario = "windows" | "prores1080" | "prores4k";
+
+/** Seconds to export, before and after 0.6.1, as measured in each PR. */
+const SCENARIOS: Record<
+ Scenario,
+ {
+ before: number;
+ after: number;
+ subtitle: string;
+ caption: ReactNode;
+ }
+> = {
+ windows: {
+ before: 99.5,
+ after: 20.5,
+ subtitle: "An 83 second recording to MP4 at 1080p60, on Windows",
+ caption: (
+ <>
+ Windows 11 on a laptop with an i7-12800H and an RTX 3080 Ti. The speedup
+ is mostly Hona's work in <PR number={2439} />, <PR number={2448} /> and{" "}
+ <PR number={2450} />.
+ </>
+ ),
+ },
+ prores1080: {
+ before: 1800 / 13.2,
+ after: 1800 / 54.3,
+ subtitle: "A minute of ProRes at 1080p30, on an Apple M5",
+ caption: (
+ <>
+ 13.2 frames a second before, 54.3 after, with a bit-for-bit identical
+ file. Measured by Aqua-123 in <PR number={2528} />.
+ </>
+ ),
+ },
+ prores4k: {
+ before: 1800 / 3.2,
+ after: 1800 / 11.8,
+ subtitle: "A minute of ProRes at 4K30, on an Apple M5",
+ caption: (
+ <>
+ 3.2 frames a second before, 11.8 after, with a bit-for-bit identical
+ file. Measured by Aqua-123 in <PR number={2528} />.
+ </>
+ ),
+ },
+};
+
+const OPTIONS = [
+ { value: "windows", label: "Windows MP4" },
+ { value: "prores1080", label: "ProRes 1080p" },
+ { value: "prores4k", label: "ProRes 4K" },
+] as const;
+
+function PR({ number }: { number: number }) {
+ return (
+ <a
+ href={`https://github.com/CapSoftware/Cap/pull/${number}`}
+ className="text-gray-11 underline decoration-gray-6 underline-offset-2 hover:text-gray-12"
+ >
+ #{number}
+ </a>
+ );
+}
+
+function formatSeconds(total: number) {
+ if (total < 100) return `${total.toFixed(1).replace(/\.0$/, "")} s`;
+ const rounded = Math.round(total);
+ return `${Math.floor(rounded / 60)} min ${rounded % 60} s`;
+}
+
+export function ExportRace() {
+ const [scenario, setScenario] = useState<Scenario>("windows");
+ const run = SCENARIOS[scenario];
+
+ return (
+ <Chart
+ title="Export time"
+ subtitle={run.subtitle}
+ controls={
+ <Tabs
+ label="Export"
+ options={OPTIONS}
+ value={scenario}
+ onChange={setScenario}
+ />
+ }
+ caption={run.caption}
+ >
+ <div className="grid items-end gap-x-14 gap-y-6 pt-8 md:grid-cols-[13rem_minmax(0,1fr)]">
+ <Headline
+ value={`${(run.before / run.after).toFixed(1)}×`}
+ label="faster than 0.6.0"
+ />
+ <div>
+ <BarRow
+ name="Cap 0.6.0"
+ value={formatSeconds(run.before)}
+ share={1}
+ />
+ <BarRow
+ name="Cap 0.6.1"
+ value={formatSeconds(run.after)}
+ share={run.after / run.before}
+ highlight
+ />
+ </div>
+ </div>
+ </Chart>
+ );
+}
diff --git a/apps/web/components/blog/native/ResourceChart.tsx b/apps/web/components/blog/native/ResourceChart.tsx
new file mode 100644
index 000000000..2cb4edd8d
--- /dev/null
+++ b/apps/web/components/blog/native/ResourceChart.tsx
@@ -0,0 +1,129 @@
+"use client";
+
+import { useState } from "react";
+import { BarRow, Chart, Headline, Tabs } from "./shared";
+
+type Workload = "playback" | "recording";
+
+type Sample = { cpu: number; memory: number };
+
+type AppKey = "tauri" | "electron" | "native";
+
+type App = { key: AppKey; name: string; detail: string };
+
+const APPS: App[] = [
+ { key: "tauri", name: "Tauri", detail: "0.6.0" },
+ { key: "electron", name: "Electron", detail: "experiment" },
+ { key: "native", name: "GPUI", detail: "first build" },
+];
+
+/** PR #2133: M4 Max, release builds, one warm-up and four trials each. */
+const DATA: Record<Workload, Record<AppKey, Sample>> = {
+ playback: {
+ tauri: { cpu: 76.6, memory: 3724 },
+ electron: { cpu: 53.2, memory: 1035 },
+ native: { cpu: 17.5, memory: 440 },
+ },
+ recording: {
+ tauri: { cpu: 37.6, memory: 1983 },
+ electron: { cpu: 31.7, memory: 805 },
+ native: { cpu: 20.3, memory: 256 },
+ },
+};
+
+const OPTIONS = [
+ { value: "playback", label: "Playing back" },
+ { value: "recording", label: "Recording" },
+] as const;
+
+function formatMemory(mib: number) {
+ return mib >= 1000
+ ? `${(mib / 1024).toFixed(1)} GB`
+ : `${Math.round(mib)} MB`;
+}
+
+export function ResourceChart() {
+ const [workload, setWorkload] = useState<Workload>("playback");
+ const data = DATA[workload];
+ const cpuMax = Math.max(...APPS.map((app) => data[app.key].cpu));
+ const memoryMax = Math.max(...APPS.map((app) => data[app.key].memory));
+
+ return (
+ <Chart
+ title="Three builds of Cap, one recording"
+ subtitle={
+ workload === "playback"
+ ? "While playing back in the editor"
+ : "While recording"
+ }
+ controls={
+ <Tabs
+ label="Workload"
+ options={OPTIONS}
+ value={workload}
+ onChange={setWorkload}
+ />
+ }
+ caption="Measured in August on an M4 Max, with release builds, one warm-up and four runs of each."
+ >
+ <div className="grid gap-x-14 gap-y-10 pt-8 md:grid-cols-2">
+ {(["cpu", "memory"] as const).map((metric) => {
+ const max = metric === "cpu" ? cpuMax : memoryMax;
+ const saved = 1 - data.native[metric] / data.tauri[metric];
+ return (
+ <div key={metric}>
+ <Headline
+ value={`${Math.round(saved * 100)}%`}
+ label={
+ metric === "cpu"
+ ? "less CPU than Tauri"
+ : "less memory than Tauri"
+ }
+ />
+ <div className="mt-6">
+ {APPS.map((app) => {
+ const value = data[app.key][metric];
+ return (
+ <BarRow
+ key={app.key}
+ name={app.name}
+ detail={app.detail}
+ value={
+ metric === "cpu" ? `${value}%` : formatMemory(value)
+ }
+ share={value / max}
+ highlight={app.key === "native"}
+ />
+ );
+ })}
+ </div>
+ </div>
+ );
+ })}
+ </div>
+
+ <table className="sr-only">
+ <caption>
+ CPU and memory while{" "}
+ {workload === "playback" ? "playing back in the editor" : "recording"}
+ </caption>
+ <thead>
+ <tr>
+ <th>App</th>
+ <th>CPU</th>
+ <th>Memory</th>
+ </tr>
+ </thead>
+ <tbody>
+ {APPS.map((app) => (
+ <tr key={app.key}>
+ <td>{app.name}</td>
+ <td>{data[app.key].cpu}%</td>
+ <td>{data[app.key].memory} MiB</td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </Chart>
+ );
+}
diff --git a/apps/web/components/blog/native/Screenshot.tsx b/apps/web/components/blog/native/Screenshot.tsx
new file mode 100644
index 000000000..c11a50c5c
--- /dev/null
+++ b/apps/web/components/blog/native/Screenshot.tsx
@@ -0,0 +1,45 @@
+import clsx from "clsx";
+import Image from "next/image";
+import { type RibbonKey, Stage } from "./shared";
+
+/**
+ * A real capture of a Cap window, taken with its shadow off so the page can
+ * draw one that suits the panel. MDX passes every prop as a string.
+ */
+export function Screenshot({
+ src,
+ alt,
+ width,
+ height,
+ caption,
+ size = "wide",
+ ribbons = "0",
+}: {
+ src: string;
+ alt: string;
+ width: string;
+ height: string;
+ caption?: string;
+ size?: "wide" | "narrow";
+ ribbons?: string;
+}) {
+ return (
+ <Stage
+ ribbons={Number(ribbons) as RibbonKey}
+ caption={caption}
+ className="flex justify-center"
+ >
+ <Image
+ src={src}
+ alt={alt}
+ width={Number(width)}
+ height={Number(height)}
+ sizes={size === "wide" ? "(min-width: 1088px) 900px, 90vw" : "480px"}
+ className={clsx(
+ "h-auto w-full drop-shadow-[0_24px_36px_rgba(20,28,45,0.24)]",
+ size === "wide" ? "max-w-[56rem]" : "max-w-[30rem]",
+ )}
+ />
+ </Stage>
+ );
+}
diff --git a/apps/web/components/blog/native/Tweet.tsx b/apps/web/components/blog/native/Tweet.tsx
new file mode 100644
index 000000000..ee187c387
--- /dev/null
+++ b/apps/web/components/blog/native/Tweet.tsx
@@ -0,0 +1,14 @@
+import { Tweet as XTweet } from "react-tweet";
+
+/**
+ * A real X embed, rendered on the server by react-tweet from X's public
+ * syndication data, so it looks like X and needs no third-party script.
+ * `x-embed` undoes the forced prose sizes in globals.css.
+ */
+export function Tweet({ id }: { id: string }) {
+ return (
+ <div className="not-prose x-embed my-10 flex justify-center [&_.react-tweet-theme]:my-0">
+ <XTweet id={id} />
+ </div>
+ );
+}
diff --git a/apps/web/components/blog/native/shared.tsx b/apps/web/components/blog/native/shared.tsx
new file mode 100644
index 000000000..42c948803
--- /dev/null
+++ b/apps/web/components/blog/native/shared.tsx
@@ -0,0 +1,291 @@
+"use client";
+
+import clsx from "clsx";
+import { motion, useInView, useReducedMotion } from "motion/react";
+import { type ReactNode, useId, useRef } from "react";
+import { htMono } from "../../pages/HomeTwo/fonts";
+
+/**
+ * Four arrangements of the soft white light ribbons every figure sits on, so
+ * neighbouring figures don't look stamped from the same mould.
+ */
+const RIBBONS = [
+ {
+ band: "M-80 520 C 220 330 420 640 700 420 S 1120 120 1300 220",
+ edge: "M-80 506 C 220 316 420 626 700 406 S 1120 106 1300 206",
+ shade: "M-60 120 C 200 230 380 40 640 140 S 1060 360 1300 300",
+ },
+ {
+ band: "M-80 180 C 260 60 460 380 760 260 S 1140 420 1300 340",
+ edge: "M-80 168 C 260 48 460 368 760 248 S 1140 408 1300 328",
+ shade: "M-60 560 C 240 460 520 640 820 520 S 1180 420 1300 470",
+ },
+ {
+ band: "M-80 420 C 180 520 400 200 640 300 S 1060 560 1300 420",
+ edge: "M-80 407 C 180 507 400 187 640 287 S 1060 547 1300 407",
+ shade: "M-60 80 C 300 180 520 0 800 120 S 1160 240 1300 160",
+ },
+ {
+ band: "M-80 300 C 300 120 520 520 860 340 S 1160 160 1300 200",
+ edge: "M-80 287 C 300 107 520 507 860 327 S 1160 147 1300 187",
+ shade: "M-60 560 C 260 600 460 420 760 500 S 1120 640 1300 580",
+ },
+] as const;
+
+export type RibbonKey = 0 | 1 | 2 | 3;
+
+/** A soft grey panel with glossy white ribbons of light across it. */
+export function Ribbons({ variant = 0 }: { variant?: RibbonKey }) {
+ const id = useId().replace(/:/g, "");
+ const ribbon = RIBBONS[variant];
+ return (
+ <svg
+ aria-hidden="true"
+ className="absolute inset-0 size-full"
+ viewBox="0 0 1200 640"
+ preserveAspectRatio="xMidYMid slice"
+ >
+ <defs>
+ <linearGradient id={`${id}-base`} x1="0" y1="0" x2="0" y2="1">
+ <stop offset="0" stopColor="#EEF0F3" />
+ <stop offset="1" stopColor="#E1E4E9" />
+ </linearGradient>
+ <linearGradient id={`${id}-light`} x1="0" y1="0" x2="1" y2="0">
+ <stop offset="0" stopColor="#FFFFFF" stopOpacity="0" />
+ <stop offset="0.45" stopColor="#FFFFFF" stopOpacity="0.95" />
+ <stop offset="1" stopColor="#FFFFFF" stopOpacity="0" />
+ </linearGradient>
+ <filter id={`${id}-soft`} x="-20%" y="-100%" width="140%" height="300%">
+ <feGaussianBlur stdDeviation="26" />
+ </filter>
+ </defs>
+ <rect width="1200" height="640" fill={`url(#${id}-base)`} />
+ <path
+ d={ribbon.shade}
+ stroke="#C7CBD3"
+ strokeWidth="90"
+ fill="none"
+ opacity="0.55"
+ filter={`url(#${id}-soft)`}
+ />
+ <path
+ d={ribbon.band}
+ stroke={`url(#${id}-light)`}
+ strokeWidth="110"
+ fill="none"
+ filter={`url(#${id}-soft)`}
+ />
+ <path
+ d={ribbon.edge}
+ stroke={`url(#${id}-light)`}
+ strokeWidth="1.6"
+ fill="none"
+ />
+ </svg>
+ );
+}
+
+/**
+ * Every figure in the post is the same thing: a soft grey panel with light
+ * ribbons, one width, one line of caption, and a short fade the first time
+ * it comes into view. Nothing inside moves unless you touch it.
+ *
+ * It breaks out of the prose column with `94vw` rather than `100vw` so it
+ * never adds a horizontal scrollbar, and avoids `p`, `a` and `li` inside,
+ * because globals.css sizes those with `!important` inside `.prose`.
+ */
+export function Stage({
+ ribbons = 0,
+ caption,
+ children,
+ className,
+}: {
+ ribbons?: RibbonKey;
+ caption?: ReactNode;
+ children: ReactNode;
+ className?: string;
+}) {
+ return (
+ <FadeIn className="not-prose relative left-1/2 my-14 w-[min(94vw,68rem)] -translate-x-1/2 sm:my-20">
+ <div className="relative overflow-hidden rounded-[20px] bg-[#E6E8EC] shadow-[inset_0_0_0_1px_rgba(0,0,0,0.04)] sm:rounded-[24px]">
+ <Ribbons variant={ribbons} />
+ <div
+ className={clsx(
+ "relative px-3 py-8 sm:px-10 sm:py-14 lg:px-16",
+ className,
+ )}
+ >
+ {children}
+ </div>
+ </div>
+ {caption && (
+ <figcaption className="mx-auto mt-4 max-w-[40rem] px-2 text-center text-[14px] leading-relaxed text-gray-10">
+ {caption}
+ </figcaption>
+ )}
+ </FadeIn>
+ );
+}
+
+/** A figure that fades in once, the first time it's on screen. */
+export function FadeIn({
+ className,
+ children,
+}: {
+ className?: string;
+ children: ReactNode;
+}) {
+ const ref = useRef<HTMLElement>(null);
+ const seen = useInView(ref, { once: true, amount: 0.15 });
+ const reduceMotion = useReducedMotion() ?? false;
+ return (
+ <motion.figure
+ ref={ref}
+ className={className}
+ initial={false}
+ animate={{ opacity: seen || reduceMotion ? 1 : 0 }}
+ transition={{ duration: 0.6, ease: "easeOut" }}
+ >
+ {children}
+ </motion.figure>
+ );
+}
+
+/**
+ * The charts sit straight on the page, like a table in a book: a hairline,
+ * a few rows and the numbers. A bit wider than the text column so a bar has
+ * room to mean something.
+ */
+export function Chart({
+ title,
+ subtitle,
+ controls,
+ caption,
+ children,
+}: {
+ title: ReactNode;
+ subtitle?: ReactNode;
+ controls?: ReactNode;
+ caption?: ReactNode;
+ children: ReactNode;
+}) {
+ return (
+ <FadeIn className="chart-figure not-prose relative left-1/2 my-14 w-[min(calc(100vw-2.5rem),48rem)] -translate-x-1/2 sm:my-16">
+ <div className="flex flex-wrap items-end justify-between gap-x-6 gap-y-3 border-b border-gray-4 pb-4">
+ <div className="min-w-0">
+ <div className="text-[17px] font-medium leading-snug text-gray-12">
+ {title}
+ </div>
+ {subtitle && (
+ <div className="mt-0.5 text-[14px] leading-snug text-gray-10">
+ {subtitle}
+ </div>
+ )}
+ </div>
+ {controls}
+ </div>
+ {children}
+ {caption && (
+ <figcaption className="mt-5 border-t border-gray-4 pt-4 text-[13px] leading-relaxed text-gray-10">
+ {caption}
+ </figcaption>
+ )}
+ </FadeIn>
+ );
+}
+
+/** Plain text tabs. The active one gets a soft grey fill, nothing slides. */
+export function Tabs<T extends string>({
+ label,
+ options,
+ value,
+ onChange,
+}: {
+ label: string;
+ options: readonly { value: T; label: string }[];
+ value: T;
+ onChange: (value: T) => void;
+}) {
+ return (
+ <fieldset className="-mr-2 m-0 flex min-w-0 gap-0.5 border-0 p-0">
+ <legend className="sr-only">{label}</legend>
+ {options.map((option) => {
+ const active = option.value === value;
+ return (
+ <button
+ key={option.value}
+ type="button"
+ aria-pressed={active}
+ onClick={() => onChange(option.value)}
+ className={clsx(
+ "h-8 rounded-full px-3 text-[14px] transition-colors duration-150",
+ active
+ ? "bg-gray-3 font-medium text-gray-12"
+ : "text-gray-10 hover:text-gray-12",
+ )}
+ >
+ {option.label}
+ </button>
+ );
+ })}
+ </fieldset>
+ );
+}
+
+/**
+ * One row of a chart: a name, a thin bar and its value. Only the new app is
+ * blue, so the eye lands on it first. Widths ease when a tab changes.
+ */
+export function BarRow({
+ name,
+ detail,
+ value,
+ share,
+ highlight,
+}: {
+ name: string;
+ detail?: string;
+ value: string;
+ share: number;
+ highlight?: boolean;
+}) {
+ return (
+ <div className="grid grid-cols-[minmax(0,1fr)_auto] items-baseline gap-x-4 py-3">
+ <div className="min-w-0 truncate text-[15px] text-gray-12">
+ {name}
+ {detail && <span className="ml-2 text-gray-10">{detail}</span>}
+ </div>
+ <div
+ className={clsx(
+ "text-[15px] tabular-nums",
+ highlight ? "font-medium text-gray-12" : "text-gray-11",
+ )}
+ >
+ {value}
+ </div>
+ <div className="col-span-2 mt-2 h-1.5 rounded-full bg-gray-3">
+ <div
+ className={clsx(
+ "h-full rounded-full transition-[width] duration-500 ease-out motion-reduce:transition-none",
+ highlight ? "bg-[#3B6FE8]" : "bg-gray-7",
+ )}
+ style={{ width: `${Math.max(1.5, share * 100)}%` }}
+ />
+ </div>
+ </div>
+ );
+}
+
+/** A big number with a short line under it, for the result of a chart. */
+export function Headline({ value, label }: { value: string; label: string }) {
+ return (
+ <div>
+ <div className="text-[44px] font-semibold leading-none tracking-[-0.035em] tabular-nums text-gray-12 sm:text-[52px]">
+ {value}
+ </div>
+ <div className="mt-2 text-[14px] text-gray-10">{label}</div>
+ </div>
+ );
+}
+
+export const mono = htMono.className;
diff --git a/apps/web/components/blog/post-components.tsx b/apps/web/components/blog/post-components.tsx
index cf6e1af54..ae3a33066 100644
--- a/apps/web/components/blog/post-components.tsx
+++ b/apps/web/components/blog/post-components.tsx
@@ -1,5 +1,11 @@
import type { MDXComponents } from "mdx/types";
import { CapEmbed } from "./CapEmbed";
+import { BeforeAfter } from "./native/BeforeAfter";
+import { CodeCompare } from "./native/CodeCompare";
+import { ExportRace } from "./native/ExportRace";
+import { ResourceChart } from "./native/ResourceChart";
+import { Screenshot } from "./native/Screenshot";
+import { Tweet } from "./native/Tweet";
import { ToggleDemo } from "./timeline/ToggleDemo";
/**
@@ -18,6 +24,15 @@ const POST_COMPONENTS: Record<string, PostComponents> = {
"cap-v06": {
CapEmbed,
},
+ "rebuilding-cap-in-gpui": {
+ BeforeAfter,
+ CapEmbed,
+ CodeCompare,
+ ExportRace,
+ ResourceChart,
+ Screenshot,
+ Tweet,
+ },
};
export function getPostComponents(slug: string): PostComponents | undefined {
diff --git a/apps/web/components/pages/HomePage/Header.tsx b/apps/web/components/pages/HomePage/Header.tsx
index f6010638d..066803b82 100644
--- a/apps/web/components/pages/HomePage/Header.tsx
+++ b/apps/web/components/pages/HomePage/Header.tsx
@@ -458,7 +458,7 @@ const Header = ({ serverHomepageCopyVariant = "" }: HeaderProps) => {
<div className="mt-14">
<p className="mb-4 text-sm italic text-gray-10 text-center md:text-left">
- Trusted by <strong>50,000+</strong> teams, builders and creators
+ Trusted by <strong>80,000+</strong> teams, builders and creators
</p>
<LogoMarquee />
</div>
diff --git a/apps/web/components/pages/HomeTwo/DiscordLinkPreview.tsx b/apps/web/components/pages/HomeTwo/DiscordLinkPreview.tsx
new file mode 100644
index 000000000..2bb2b342b
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/DiscordLinkPreview.tsx
@@ -0,0 +1,41 @@
+import { DISCORD_URL } from "@/components/pages/about/content";
+import { homepageOgImageUrl } from "@/components/pages/HomeTwo/metadata";
+import { homepageSeo } from "@/components/pages/HomeTwo/seo";
+
+export const discordLinkPreview = {
+ component: {
+ type: 17,
+ accent_color: 9280752,
+ components: [
+ {
+ type: 9,
+ components: [
+ {
+ type: 10,
+ content: `# [Cap](${homepageSeo.url})\n${homepageSeo.description}`,
+ },
+ ],
+ accessory: {
+ type: 2,
+ style: 5,
+ label: "Join our Discord",
+ url: DISCORD_URL,
+ },
+ },
+ {
+ type: 12,
+ items: [{ media: { url: homepageOgImageUrl } }],
+ },
+ ],
+ },
+} as const;
+
+const discordComponentEmbedId = "discord:component-embed";
+
+export function DiscordLinkPreview() {
+ return (
+ <script id={discordComponentEmbedId} type="application/json">
+ {JSON.stringify(discordLinkPreview)}
+ </script>
+ );
+}
diff --git a/apps/web/components/pages/HomeTwo/Hero.tsx b/apps/web/components/pages/HomeTwo/Hero.tsx
index 29517df3d..2b45c665a 100644
--- a/apps/web/components/pages/HomeTwo/Hero.tsx
+++ b/apps/web/components/pages/HomeTwo/Hero.tsx
@@ -122,37 +122,37 @@ export const Hero = () => {
/>
<div className="relative mx-auto flex max-w-[1020px] flex-col items-center text-center">
<Link
- href="/blog/cap-v06"
+ href="/blog/rebuilding-cap-in-gpui"
onClick={() =>
trackEvent("release_notice_clicked", {
source_page: "home_hero",
- release: "v0.6",
+ release: "v0.6.1",
})
}
- className="group mb-7 inline-flex max-w-full items-center gap-2.5 rounded-full bg-[#111111] py-1.5 pl-1.5 pr-3.5 text-[13px] leading-none text-white shadow-[0_1px_2px_rgba(17,17,17,0.12),0_10px_24px_-14px_rgba(17,17,17,0.6)] transition-colors duration-200 hover:bg-[#2A2A2A] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111] focus-visible:ring-offset-2 focus-visible:ring-offset-[#EDF1F6] md:mb-9"
+ className="ht-rise group mb-7 inline-flex [--d:0.2s] max-w-full items-center gap-2.5 rounded-full bg-[#111111] py-1.5 pl-1.5 pr-3.5 text-[13px] leading-none text-white shadow-[0_1px_2px_rgba(17,17,17,0.12),0_10px_24px_-14px_rgba(17,17,17,0.6)] transition-colors duration-200 hover:bg-[#2A2A2A] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111] focus-visible:ring-offset-2 focus-visible:ring-offset-[#EDF1F6] md:mb-9"
>
<span
className={`${MONO} rounded-full bg-[#8DBCF0] px-2 py-[5px] text-[10.5px] uppercase leading-none tracking-[0.06em] text-[#111111]`}
>
New
</span>
- <span className="truncate">Cap v0.6 is here</span>
+ <span className="truncate">Cap's migration to GPUI</span>
<span className="hidden text-white/60 transition-colors duration-200 group-hover:text-white sm:inline">
- See what's new
+ Read the deep dive
</span>
<ArrowRight className="size-3.5 text-white/70 transition-transform duration-200 group-hover:translate-x-0.5 group-hover:text-white" />
</Link>
<HeroHeadline />
<p
- className={`${BODY_TEXT} mt-8 max-w-[660px] text-balance text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[19px] md:mt-10`}
+ className={`ht-rise [--d:0.75s] ${BODY_TEXT} mt-8 max-w-[660px] text-balance text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[19px] md:mt-10`}
>
- One open source app for every way you capture your screen. Start free
- and move to Cap Pro when you need more, whether you work alone, with a
- small team, or across an entire organization.
+ Record your screen, then share it as a link the moment you stop or
+ polish it in the built-in editor. Trusted by over 80,000 teams and
+ creators, and free to start.
</p>
- <div className="mt-9 flex flex-col items-stretch gap-3 sm:flex-row sm:items-center">
+ <div className="ht-rise mt-9 flex flex-col items-stretch gap-3 [--d:0.9s] sm:flex-row sm:items-center">
<Link
href={downloadHref}
onClick={() =>
@@ -207,7 +207,7 @@ export const Hero = () => {
cta_location: "under_primary",
})
}
- className="group mt-5 inline-flex items-center gap-2 rounded-[6px] text-[14px] text-[rgba(17,17,17,0.55)] transition-colors duration-200 hover:text-[#111111] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111] focus-visible:ring-offset-4 focus-visible:ring-offset-[#F8FAFC]"
+ className="ht-rise group mt-5 inline-flex items-center gap-2 rounded-[6px] [--d:1.05s] text-[14px] text-[rgba(17,17,17,0.55)] transition-colors duration-200 hover:text-[#111111] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111] focus-visible:ring-offset-4 focus-visible:ring-offset-[#F8FAFC]"
>
<LoomMark
size={13}
@@ -222,7 +222,7 @@ export const Hero = () => {
<ArrowRight className="size-3.5 transition-transform duration-200 group-hover:translate-x-0.5" />
</Link>
- <div className="mt-3 flex items-center gap-2 text-[14px] text-[rgba(17,17,17,0.5)]">
+ <div className="ht-rise mt-3 flex items-center gap-2 text-[14px] text-[rgba(17,17,17,0.5)] [--d:1.15s]">
<span>Also available on</span>
<span className="flex items-center gap-1">
{isWindows || isLinux ? (
diff --git a/apps/web/components/pages/HomeTwo/HeroHeadline.tsx b/apps/web/components/pages/HomeTwo/HeroHeadline.tsx
index 74b54489c..f76fb8b17 100644
--- a/apps/web/components/pages/HomeTwo/HeroHeadline.tsx
+++ b/apps/web/components/pages/HomeTwo/HeroHeadline.tsx
@@ -14,15 +14,15 @@ type SlideKey = ModeKey | "all";
type Slide = {
key: SlideKey;
-
- words: [string, string, string];
+ /** The end of "The open source screen recorder for …". */
+ tail: string;
};
const SLIDES: Slide[] = [
- { key: "all", words: ["Record.", "Edit.", "Share."] },
- { key: "instant", words: ["Record.", "Stop.", "Instant Share."] },
- { key: "studio", words: ["Record.", "Edit.", "Export."] },
- { key: "screenshot", words: ["Screenshot.", "Style.", "Share."] },
+ { key: "all", tail: "everyone." },
+ { key: "instant", tail: "quick shares." },
+ { key: "studio", tail: "polished videos." },
+ { key: "screenshot", tail: "screenshots, too." },
];
const NUDGE = "Click me \u{1F440}";
@@ -52,8 +52,6 @@ const PILL_ICON = {
screenshot: ScreenshotIcon,
} as const;
-const SLOT_DELAY = [0, 120, 240];
-
const STRIKE_MS = 260;
const HOLD_MS = 110;
@@ -253,7 +251,7 @@ export const HeroHeadline = () => {
{CSS}
</style>
- <div className="relative pb-10 md:pb-14">
+ <div className="ht-rise relative pb-10 [--d:0.35s] md:pb-14">
<div className="mx-auto flex w-fit flex-nowrap items-center justify-center gap-0.5 rounded-full border border-[#DDE4EB] bg-white/70 p-1 shadow-[0_1px_2px_rgba(17,17,17,0.04)]">
{PILLS.map((pill) => {
const active = index === pill.slide;
@@ -303,25 +301,27 @@ export const HeroHeadline = () => {
</span>
</div>
- <h1 className={`${H_HERO} text-[clamp(36px,5.6vw,74px)]`}>
+ <h1
+ className={`ht-rise ht-rise-soft [--d:0.5s] ${H_HERO} max-w-[15ch] text-balance text-[clamp(34px,4.4vw,60px)] sm:max-w-none`}
+ >
<span className="sr-only">
- Record. Edit. Share. Cap has three recording modes: Instant, Studio,
- and Screenshot.
+ The open source screen recorder for everyone. Cap has three recording
+ modes: Instant, Studio, and Screenshot.
</span>
- <span
- aria-hidden="true"
- className="ht-headline flex flex-col items-center sm:flex-row sm:justify-center sm:gap-[0.26em]"
- >
- {slide.words.map((word, i) => (
+ <span aria-hidden="true" className="ht-headline block">
+ <span className="block">The open source screen recorder</span>
+ {/* The line that changes with the mode, in the subheading's
+ serif so the short line reads as deliberate. */}
+ <span className="ht-tail block">
+ for{" "}
<Slot
- key={`slot-${i === 0 ? "capture" : i === 1 ? "beat" : "payoff"}`}
- text={word}
- color={i === 2 ? accent : INK}
+ text={slide.tail}
+ color={accent}
strike={accent}
- delay={SLOT_DELAY[i] ?? 0}
+ delay={0}
animate={!reduced}
/>
- ))}
+ </span>
</span>
</h1>
</div>
@@ -329,9 +329,36 @@ export const HeroHeadline = () => {
};
const CSS = `
+/* The hero's entrance, staggered by --d on each piece. */
+.ht-rise { animation: ht-rise 1.1s cubic-bezier(0.16, 1, 0.3, 1) var(--d, 0s) both; }
+.ht-rise-soft { animation-name: ht-rise-soft; animation-duration: 1.3s; }
+@keyframes ht-rise {
+ from { opacity: 0; transform: translate3d(0, 14px, 0); }
+ to { opacity: 1; transform: none; }
+}
+@keyframes ht-rise-soft {
+ from { opacity: 0; transform: translate3d(0, 18px, 0); filter: blur(10px); }
+ to { opacity: 1; transform: none; filter: blur(0); }
+}
+
/* The site's base sheet pins spans to line-height 1.5rem, which would
collapse the slot line boxes. Restore the H_HERO leading. */
.ht-headline, .ht-headline span { line-height: 0.98; }
+.ht-tail {
+ margin-top: 0.06em;
+ font-family: var(--font-ht-serif), Georgia, serif;
+ font-style: italic;
+ font-weight: 400;
+ font-size: 1.06em;
+ letter-spacing: -0.025em;
+ white-space: nowrap;
+}
+/* Never let "for" and the ending split across lines: on narrow phones the
+ line shrinks until the longest ending ("for screenshots, too.", about
+ 8.6em wide) fits inside the hero's padding. */
+@media (max-width: 639px) {
+ .ht-tail { font-size: min(1.06em, calc((100vw - 64px) / 8.8)); }
+}
.ht-slot {
position: relative;
@@ -471,6 +498,7 @@ const CSS = `
@media (min-width: 1024px) { .ht-nudge { right: 94%; } }
@media (prefers-reduced-motion: reduce) {
+ .ht-rise { animation: none; }
.ht-slot { transition: none; }
.ht-nudge, .ht-nudge-chip { transition: none; }
.ht-strike, .ht-out, .ht-in, .ht-caret { animation: none; }
diff --git a/apps/web/components/pages/HomeTwo/HeroSky.tsx b/apps/web/components/pages/HomeTwo/HeroSky.tsx
index f9a7ebd45..be1c23bc0 100644
--- a/apps/web/components/pages/HomeTwo/HeroSky.tsx
+++ b/apps/web/components/pages/HomeTwo/HeroSky.tsx
@@ -1,59 +1,66 @@
"use client";
import { classNames } from "@cap/utils/helpers";
-import Image from "next/image";
import { useRef } from "react";
-import { BAND, GRAIN } from "./theme";
+import { BAND } from "./theme";
import { useInView, usePageVisible } from "./visibility";
+/**
+ * The desktop app's welcome screen sky
+ * (apps/desktop-gpui/src/onboarding_window.rs, `welcome_sky`) over the
+ * landing page's clouds: light rays behind and in front of them, a sun in
+ * the top right, moving film grain, and an entrance that lifts a pale wash
+ * off it all.
+ */
+
const ART = {
- cumulusA: { src: "/backgrounds/clouds/cumulus-a.webp", w: 896, h: 387 },
- cumulusB: { src: "/backgrounds/clouds/cumulus-b.webp", w: 760, h: 360 },
- tuft: { src: "/backgrounds/clouds/tuft-a.webp", w: 600, h: 300 },
- bank: { src: "/backgrounds/clouds/bank-a.webp", w: 864, h: 241 },
- wisp: { src: "/backgrounds/clouds/wisp-a.webp", w: 860, h: 203 },
+ cumulusA: { src: "/backgrounds/clouds/cumulus-a.webp", aspect: 387 / 896 },
+ cumulusB: { src: "/backgrounds/clouds/cumulus-b.webp", aspect: 360 / 760 },
+ tuft: { src: "/backgrounds/clouds/tuft-a.webp", aspect: 300 / 600 },
+ bank: { src: "/backgrounds/clouds/bank-a.webp", aspect: 241 / 864 },
+ wisp: { src: "/backgrounds/clouds/wisp-a.webp", aspect: 203 / 860 },
} as const;
type Art = keyof typeof ART;
-
-type CloudSpec = {
+/** `drift` is seconds per crossing and `offset` how far into it a cloud
+ * starts, so they never line up. Far clouds sit behind the mist. */
+type Cloud = {
art: Art;
top: string;
width: number;
opacity: number;
drift: number;
offset: number;
- bob: number;
- near?: boolean;
+ far?: boolean;
};
-const CLOUDS: CloudSpec[] = [
+const CLOUDS: Cloud[] = [
{
art: "wisp",
top: "4%",
width: 300,
- opacity: 0.62,
+ opacity: 0.55,
drift: 280,
offset: -100,
- bob: 19,
+ far: true,
},
{
art: "tuft",
top: "2%",
width: 210,
- opacity: 0.62,
+ opacity: 0.55,
drift: 250,
offset: -210,
- bob: 23,
+ far: true,
},
{
art: "bank",
top: "11%",
width: 340,
- opacity: 0.6,
+ opacity: 0.5,
drift: 300,
offset: -40,
- bob: 20,
+ far: true,
},
{
art: "cumulusB",
@@ -62,7 +69,6 @@ const CLOUDS: CloudSpec[] = [
opacity: 0.86,
drift: 210,
offset: -150,
- bob: 21,
},
{
art: "tuft",
@@ -71,7 +77,6 @@ const CLOUDS: CloudSpec[] = [
opacity: 0.82,
drift: 220,
offset: -30,
- bob: 25,
},
{
art: "cumulusA",
@@ -80,8 +85,6 @@ const CLOUDS: CloudSpec[] = [
opacity: 0.97,
drift: 175,
offset: -115,
- bob: 27,
- near: true,
},
{
art: "bank",
@@ -90,8 +93,6 @@ const CLOUDS: CloudSpec[] = [
opacity: 0.94,
drift: 185,
offset: -60,
- bob: 29,
- near: true,
},
{
art: "wisp",
@@ -100,46 +101,39 @@ const CLOUDS: CloudSpec[] = [
opacity: 0.82,
drift: 230,
offset: -190,
- bob: 24,
},
];
-const Cloud = ({ spec }: { spec: CloudSpec }) => {
- const art = ART[spec.art];
- const height = Math.round((art.h / art.w) * spec.width);
- return (
- <div
- className="ht-cloud"
- style={{
- top: spec.top,
- width: spec.width,
- height,
- opacity: spec.opacity,
- animationDuration: `${spec.drift}s`,
- animationDelay: `${spec.offset}s`,
- }}
- >
- <Image
- src={art.src}
- alt=""
- width={spec.width}
- height={height}
- unoptimized
- draggable={false}
- priority={spec.near}
- loading="eager"
- fetchPriority={spec.near ? "high" : "low"}
- className="ht-cloud-bob block select-none"
- style={{ animationDuration: `${spec.bob}s` }}
- />
- </div>
- );
-};
+const CloudSprite = ({ cloud }: { cloud: Cloud }) => (
+ // The track is the sky plus the cloud's own width, so each one crosses
+ // from fully off the left edge to fully off the right.
+ <div
+ className="ht-cloud"
+ style={{
+ top: cloud.top,
+ left: -cloud.width,
+ width: `calc(100% + ${cloud.width}px)`,
+ opacity: cloud.opacity,
+ animationDuration: `${cloud.drift}s`,
+ animationDelay: `${cloud.offset}s`,
+ }}
+ >
+ {/* biome-ignore lint/performance/noImgElement: decorative art on a CSS track */}
+ <img
+ src={ART[cloud.art].src}
+ alt=""
+ width={cloud.width}
+ height={Math.round(cloud.width * ART[cloud.art].aspect)}
+ draggable={false}
+ decoding="async"
+ className="block select-none"
+ />
+ </div>
+);
const SKY = [
- "radial-gradient(36% 32% at 70% 2%, rgba(255,224,192,0.8) 0%, rgba(255,224,192,0) 70%)",
"radial-gradient(80% 42% at 50% 74%, rgba(255,240,224,0.5) 0%, rgba(255,240,224,0) 72%)",
- `linear-gradient(180deg, #C8DDF6 0%, #D9E7F8 34%, #E6EDF6 66%, ${BAND} 100%)`,
+ `linear-gradient(180deg, #A3BFEA 0%, #C8DBF4 34%, #E2EAF5 64%, ${BAND} 92%)`,
].join(",");
export const HeroSky = ({ className }: { className?: string }) => {
@@ -152,62 +146,176 @@ export const HeroSky = ({ className }: { className?: string }) => {
ref={rootRef}
aria-hidden="true"
className={classNames(
- "pointer-events-none absolute inset-0 overflow-hidden",
+ "ht-sky pointer-events-none absolute inset-0 overflow-hidden",
className ?? "rounded-t-[24px]",
drifting ? "" : "ht-sky-idle",
)}
- style={{
- backgroundColor: BAND,
- backgroundImage: SKY,
- }}
+ style={{ backgroundColor: BAND, backgroundImage: SKY }}
>
<style href="ht-hero-sky" precedence="default">
{CSS}
</style>
- <div className="ht-cloud-track">
- {CLOUDS.map((spec) => (
- <Cloud key={`${spec.art}-${spec.top}`} spec={spec} />
+ <div className="ht-light">
+ <div className="ht-rays ht-rays-far" />
+ </div>
+ <div className="ht-clouds">
+ {CLOUDS.filter((cloud) => cloud.far).map((cloud) => (
+ <CloudSprite key={`${cloud.art}-${cloud.top}`} cloud={cloud} />
))}
+ <div className="ht-mist" />
+ {CLOUDS.filter((cloud) => !cloud.far).map((cloud) => (
+ <CloudSprite key={`${cloud.art}-${cloud.top}`} cloud={cloud} />
+ ))}
+ </div>
+ <div className="ht-vignette" />
+ <div className="ht-light">
+ <div className="ht-sun" />
+ <div className="ht-rays ht-rays-near" />
</div>
- <div
- className="absolute inset-0"
- style={{ backgroundImage: GRAIN, backgroundSize: "200px 200px" }}
- />
+ <div className="ht-ground" />
+ <div className="ht-grain" />
+ <div className="ht-wash" />
</div>
);
};
const CSS = `
-.ht-cloud-track {
+.ht-cloud {
+ position: absolute;
+ will-change: transform;
+ animation: ht-cloud-drift linear infinite;
+}
+@keyframes ht-cloud-drift {
+ from { transform: translate3d(0, 0, 0); }
+ to { transform: translate3d(100%, 0, 0); }
+}
+
+/* Clouds and light arrive after the wash starts lifting. */
+.ht-clouds, .ht-light {
position: absolute;
inset: 0;
- -webkit-mask-image: linear-gradient(180deg, #000 0%, #000 62%, rgba(0,0,0,0) 96%);
- mask-image: linear-gradient(180deg, #000 0%, #000 62%, rgba(0,0,0,0) 96%);
+ animation: ht-fade-in 2.2s cubic-bezier(0.4, 0, 0.2, 1) 0.25s both;
}
-.ht-cloud {
+.ht-light { animation-duration: 2.8s; animation-delay: 0.45s; }
+.ht-clouds {
+ -webkit-mask-image: linear-gradient(180deg, #000 0%, #000 60%, transparent 94%);
+ mask-image: linear-gradient(180deg, #000 0%, #000 60%, transparent 94%);
+}
+@keyframes ht-fade-in { from { opacity: 0; } to { opacity: 1; } }
+
+.ht-mist {
position: absolute;
- left: 0;
- will-change: transform;
- animation-name: ht-cloud-drift;
- animation-timing-function: linear;
- animation-iteration-count: infinite;
+ left: -10%;
+ top: 0;
+ width: 120%;
+ height: 150%;
+ background:
+ radial-gradient(66.7% 15% at 41.7% 35%, rgba(255,255,255,0.28) 0%, rgba(244,248,253,0.12) 50%, rgba(244,248,253,0) 100%),
+ radial-gradient(66.7% 15% at 62.5% 56%, rgba(255,255,255,0.22) 0%, rgba(244,248,253,0.1) 50%, rgba(244,248,253,0) 100%);
+ animation: ht-mist 44s ease-in-out infinite alternate;
}
-.ht-cloud-bob {
- animation-name: ht-cloud-bob;
- animation-timing-function: ease-in-out;
- animation-iteration-count: infinite;
- animation-direction: alternate;
+@keyframes ht-mist {
+ from { transform: translate3d(-20px, 0, 0); }
+ to { transform: translate3d(20px, 0, 0); }
}
-@keyframes ht-cloud-drift {
- from { transform: translate3d(-100%, 0, 0); }
- to { transform: translate3d(100vw, 0, 0); }
+
+/* Shafts fan down and to the left from just above the top right corner,
+ baked from the welcome screen's blurred SVGs (a live SVG filter on a
+ stretched background can drop its blur when Chrome re-rasterises it).
+ Each layer sways and breathes on its own period so they never line up. */
+.ht-rays {
+ position: absolute;
+ top: 0;
+ bottom: 0;
+ left: -40px;
+ right: -40px;
+ background-size: 100% 100%;
+ background-repeat: no-repeat;
+}
+.ht-rays-far {
+ background-image: url(/backgrounds/hero/rays-far.webp);
+ opacity: 0.75;
+ animation: ht-sway-far 16s ease-in-out infinite alternate,
+ ht-breathe 10s ease-in-out infinite alternate;
+}
+.ht-rays-near {
+ background-image: url(/backgrounds/hero/rays-near.webp);
+ opacity: 0.7;
+ animation: ht-sway-near 21s ease-in-out -4s infinite alternate,
+ ht-breathe 13s ease-in-out -6s infinite alternate;
+}
+@keyframes ht-sway-far {
+ from { transform: translate3d(-14px, 0, 0); }
+ to { transform: translate3d(14px, 0, 0); }
+}
+@keyframes ht-sway-near {
+ from { transform: translate3d(18px, 0, 0); }
+ to { transform: translate3d(-18px, 0, 0); }
+}
+@keyframes ht-breathe {
+ from { opacity: 0.62; }
+ to { opacity: 1; }
}
-@keyframes ht-cloud-bob {
- from { transform: translate3d(0, -5px, 0); }
- to { transform: translate3d(0, 5px, 0); }
+
+.ht-sun {
+ position: absolute;
+ left: 84%;
+ top: -6%;
+ width: 75%;
+ aspect-ratio: 1;
+ transform: translate(-50%, -50%);
+ background: radial-gradient(closest-side, rgba(255,248,236,0.95) 0%, rgba(255,227,196,0.5) 35%, rgba(255,227,196,0) 100%);
+ animation: ht-sun 9s ease-in-out infinite alternate;
+}
+@keyframes ht-sun { from { opacity: 0.8; } to { opacity: 1; } }
+
+/* Darkens the top corners only; the bottom has to meet the band cleanly. */
+.ht-vignette {
+ position: absolute;
+ inset: 0;
+ background: radial-gradient(75% 75% at 50% 50%, rgba(52,80,122,0) 50%, rgba(52,80,122,0.14) 100%);
+ -webkit-mask-image: linear-gradient(180deg, #000 0%, #000 35%, transparent 75%);
+ mask-image: linear-gradient(180deg, #000 0%, #000 35%, transparent 75%);
}
-.ht-sky-idle .ht-cloud, .ht-sky-idle .ht-cloud-bob { animation-play-state: paused; }
+.ht-ground {
+ position: absolute;
+ inset: auto 0 0 0;
+ height: 28%;
+ background: linear-gradient(180deg, ${BAND}00 0%, ${BAND} 100%);
+}
+
+/* Film grain, jumping to a new offset 12 times a second. */
+.ht-grain {
+ position: absolute;
+ inset: -200px;
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='260' height='260'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3CfeComponentTransfer%3E%3CfeFuncA type='linear' slope='0.12'/%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Crect width='260' height='260' filter='url(%23n)'/%3E%3C/svg%3E");
+ background-size: 260px 260px;
+ animation: ht-grain 0.84s steps(1, end) infinite;
+}
+@keyframes ht-grain {
+ 0% { transform: translate3d(0, 0, 0); }
+ 10% { transform: translate3d(-97px, 61px, 0); }
+ 20% { transform: translate3d(43px, -128px, 0); }
+ 30% { transform: translate3d(-151px, -22px, 0); }
+ 40% { transform: translate3d(118px, 87px, 0); }
+ 50% { transform: translate3d(-36px, 143px, 0); }
+ 60% { transform: translate3d(71px, -59px, 0); }
+ 70% { transform: translate3d(-122px, -114px, 0); }
+ 80% { transform: translate3d(160px, 19px, 0); }
+ 90% { transform: translate3d(-64px, 102px, 0); }
+}
+
+.ht-wash {
+ position: absolute;
+ inset: 0;
+ background: linear-gradient(180deg, #F6F8FB 0%, #F6F8FB 60%, ${BAND} 100%);
+ animation: ht-wash 1.9s cubic-bezier(0.4, 0, 0.2, 1) 0.05s both;
+}
+@keyframes ht-wash { from { opacity: 1; } to { opacity: 0; } }
+
+.ht-sky-idle *, .ht-sky-idle *::before { animation-play-state: paused; }
@media (prefers-reduced-motion: reduce) {
- .ht-cloud, .ht-cloud-bob { animation-play-state: paused; }
+ .ht-sky * { animation: none !important; }
+ .ht-wash { opacity: 0; }
}
`;
diff --git a/apps/web/components/pages/HomeTwo/fonts.ts b/apps/web/components/pages/HomeTwo/fonts.ts
index 7ca3b75d7..da3b85942 100644
--- a/apps/web/components/pages/HomeTwo/fonts.ts
+++ b/apps/web/components/pages/HomeTwo/fonts.ts
@@ -15,7 +15,7 @@ export const htSans = Instrument_Sans({
export const htSerif = Source_Serif_4({
subsets: ["latin"],
weight: ["300", "400"],
- style: ["normal"],
+ style: ["normal", "italic"],
display: "swap",
variable: "--font-ht-serif",
});
diff --git a/apps/web/components/pages/HomeTwo/index.tsx b/apps/web/components/pages/HomeTwo/index.tsx
index 6dcbf4b07..aa5f4a8a4 100644
--- a/apps/web/components/pages/HomeTwo/index.tsx
+++ b/apps/web/components/pages/HomeTwo/index.tsx
@@ -1,5 +1,6 @@
import { Agents } from "@/components/pages/HomeTwo/Agents";
import { DeepDives } from "@/components/pages/HomeTwo/DeepDives";
+import { DiscordLinkPreview } from "@/components/pages/HomeTwo/DiscordLinkPreview";
import { Faq } from "@/components/pages/HomeTwo/Faq";
import { Features } from "@/components/pages/HomeTwo/Features";
import { FinalCta } from "@/components/pages/HomeTwo/FinalCta";
@@ -28,7 +29,8 @@ export function HomeTwoPage() {
style={grainBg(SHELL)}
>
<HomeTwoSchema />
- <div className="px-2.5 pb-2.5 pt-[68px] sm:px-4 sm:pb-4 lg:pt-[76px]">
+ <DiscordLinkPreview />
+ <div className="px-2.5 pb-2.5 pt-[68px] sm:px-5 sm:pb-4 lg:px-8 lg:pt-[76px]">
<div
className={`${CARD_RADIUS} shadow-[0_0_0_1px_rgba(17,17,17,0.045)]`}
style={grainBg(CREAM)}
diff --git a/apps/web/components/pages/HomeTwo/metadata.ts b/apps/web/components/pages/HomeTwo/metadata.ts
index 7386e8633..0854ec7e9 100644
--- a/apps/web/components/pages/HomeTwo/metadata.ts
+++ b/apps/web/components/pages/HomeTwo/metadata.ts
@@ -1,15 +1,22 @@
import type { Metadata } from "next";
-import { buildMarketingMetadata } from "@/lib/og/url";
+import { buildMarketingMetadata, ogImageUrl } from "@/lib/og/url";
import { homepageSeo } from "./seo";
+export const homepageOgTitle = "Record. Edit. Share.";
+export const homepageOgDescription =
+ "The open source screen recorder for Mac, Windows, and Linux.";
+export const homepageOgImageUrl = new URL(
+ ogImageUrl({ title: homepageOgTitle, description: homepageOgDescription }),
+ homepageSeo.url,
+).toString();
+
export const homePageMetadata: Metadata = {
...buildMarketingMetadata({
title: homepageSeo.title,
description: homepageSeo.description,
path: homepageSeo.url,
- ogTitle: "Record. Edit. Share.",
- ogDescription:
- "The open source screen recorder for Mac, Windows, and Linux.",
+ ogTitle: homepageOgTitle,
+ ogDescription: homepageOgDescription,
}),
robots: {
index: true,
diff --git a/apps/web/components/pages/UpdatesPage.tsx b/apps/web/components/pages/UpdatesPage.tsx
index ce923abca..5f4f65ccd 100644
--- a/apps/web/components/pages/UpdatesPage.tsx
+++ b/apps/web/components/pages/UpdatesPage.tsx
@@ -8,7 +8,7 @@ import {
} from "@/utils/blog-registry";
import { generateGradientFromSlug } from "@/utils/gradients";
-const FEATURED_SLUGS = ["cap-v06"];
+const FEATURED_SLUGS = ["rebuilding-cap-in-gpui"];
export const UpdatesPage = () => {
const allUpdates = getBlogPosts() as BlogPost[];
@@ -56,12 +56,42 @@ export const UpdatesPage = () => {
className="overflow-hidden w-full rounded-xl border transition-shadow bg-gray-1 border-gray-5 hover:shadow-md"
>
<Link href={`/blog/${post.slug}`}>
- {post.metadata.image ? (
+ {"video" in post.metadata && post.metadata.video ? (
+ <div className="w-full border-b bg-black">
+ {/* The animated cover from og:video. Its first frame is
+ saved next to it as the poster. */}
+ <video
+ src={post.metadata.video}
+ poster={post.metadata.video.replace(
+ /\.mp4$/,
+ "-poster.jpg",
+ )}
+ autoPlay
+ muted
+ loop
+ playsInline
+ preload="auto"
+ aria-label={post.metadata.title}
+ className="block aspect-[40/21] w-full object-cover motion-reduce:hidden"
+ />
+ {post.metadata.image && (
+ <Image
+ src={post.metadata.image}
+ width={2000}
+ height={1000}
+ sizes="(min-width: 1100px) 1024px, 100vw"
+ alt={post.metadata.title}
+ className={`${featuredImageClassName} hidden motion-reduce:block`}
+ />
+ )}
+ </div>
+ ) : post.metadata.image ? (
<div className="w-full border-b">
<Image
src={post.metadata.image}
- width={900}
- height={400}
+ width={2000}
+ height={1000}
+ sizes="(min-width: 1100px) 1024px, 100vw"
objectFit="cover"
alt={post.metadata.title}
className={featuredImageClassName}
diff --git a/apps/web/components/pages/pricing/PricingPage.tsx b/apps/web/components/pages/pricing/PricingPage.tsx
index 3fc8b9b47..1dc3116cc 100644
--- a/apps/web/components/pages/pricing/PricingPage.tsx
+++ b/apps/web/components/pages/pricing/PricingPage.tsx
@@ -69,10 +69,10 @@ const Hero = () => (
<span
className={`${MONO} hidden rounded-full bg-[#8DBCF0] px-2 py-[5px] text-[10.5px] uppercase leading-none tracking-[0.06em] text-[#111111] sm:inline-block`}
>
- 50,000+
+ 80,000+
</span>
<span className="truncate pl-2 sm:hidden">
- Trusted by 50,000+ users
+ Trusted by 80,000+ users
</span>
<span className="hidden truncate sm:inline">
Trusted by teams and creators
diff --git a/apps/web/components/pages/seo/AsyncVideoCodeReviewsPage.tsx b/apps/web/components/pages/seo/AsyncVideoCodeReviewsPage.tsx
index b79030038..e1e8d371c 100644
--- a/apps/web/components/pages/seo/AsyncVideoCodeReviewsPage.tsx
+++ b/apps/web/components/pages/seo/AsyncVideoCodeReviewsPage.tsx
@@ -136,7 +136,7 @@ export const asyncVideoCodeReviewsContent: SeoPageContent = {
],
[
"Open source",
- { text: "Yes — MIT", status: "positive" },
+ { text: "Yes — AGPLv3 and MIT crates", status: "positive" },
{ text: "No", status: "negative" },
{ text: "Open source (GitHub)", status: "positive" },
{ text: "No", status: "negative" },
@@ -159,7 +159,7 @@ export const asyncVideoCodeReviewsContent: SeoPageContent = {
{
title: "Cap vs Loom for Code Reviews",
description:
- "Loom is a capable async video tool but limits recording length and quality on its free plan, and stores all video on Loom's servers with no self-hosting option. Cap records at 4K with no recording-length limits in Studio Mode and supports <a href='/self-hosted-screen-recording'>self-hosted S3 storage</a> for teams with data residency requirements. Cap is also MIT-licensed and fully open source.",
+ "Loom is a capable async video tool but limits recording length and quality on its free plan, and stores all video on Loom's servers with no self-hosting option. Cap records at 4K with no recording-length limits in Studio Mode and supports <a href='/self-hosted-screen-recording'>self-hosted S3 storage</a> for teams with data residency requirements. Cap is also open source, with most code under AGPLv3 and specified Rust crates under MIT.",
},
{
title: "Cap vs Written PR Comments",
@@ -271,7 +271,7 @@ export const asyncVideoCodeReviewsContent: SeoPageContent = {
{
question: "What is the best tool for async video code reviews?",
answer:
- "Cap is the best tool for async video code reviews for engineering teams that want 4K quality, instant shareable links, timestamped commenting, and the option to self-host recordings. It's free, open-source under the MIT license, and available for Mac and Windows. Unlike general async video tools, Cap's instant-mode and studio-mode workflows are optimized for the review cycle engineers actually use.",
+ "Cap is the best tool for async video code reviews for engineering teams that want 4K quality, instant shareable links, timestamped commenting, and the option to self-host recordings. It's free and open source, with most code under AGPLv3, and available for Mac and Windows. Unlike general async video tools, Cap's instant-mode and studio-mode workflows are optimized for the review cycle engineers actually use.",
},
],
diff --git a/apps/web/components/pages/seo/DeveloperDocumentationVideosPage.tsx b/apps/web/components/pages/seo/DeveloperDocumentationVideosPage.tsx
index acaf4921d..f75801c20 100644
--- a/apps/web/components/pages/seo/DeveloperDocumentationVideosPage.tsx
+++ b/apps/web/components/pages/seo/DeveloperDocumentationVideosPage.tsx
@@ -130,7 +130,7 @@ export const developerDocumentationVideosContent: SeoPageContent = {
],
[
"Open source",
- { text: "Yes — MIT", status: "positive" },
+ { text: "Yes — AGPLv3 and MIT crates", status: "positive" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
@@ -160,7 +160,7 @@ export const developerDocumentationVideosContent: SeoPageContent = {
{
title: "Cap vs Loom for Developer Documentation",
description:
- "Loom is a capable async video tool but limits recording quality and length on its free plan, and has no self-hosting option for teams with data residency requirements. Cap records at 4K with no recording-length limits in Studio Mode and supports <a href='/self-hosted-screen-recording'>self-hosted S3 storage</a>. Cap is also fully open source under the MIT license — developers can audit the code, contribute features, and trust the tool they're using to document their own products.",
+ "Loom is a capable async video tool but limits recording quality and length on its free plan, and has no self-hosting option for teams with data residency requirements. Cap records at 4K with no recording-length limits in Studio Mode and supports <a href='/self-hosted-screen-recording'>self-hosted S3 storage</a>. Cap is also open source, with most code under AGPLv3 and specified Rust crates under MIT — developers can audit the code, contribute features, and trust the tool they're using to document their own products.",
},
{
title: "Cap vs Written Documentation",
@@ -265,7 +265,7 @@ export const developerDocumentationVideosContent: SeoPageContent = {
{
question: "What is the best screen recorder for developer documentation?",
answer:
- "Cap is the best screen recorder for developer documentation because it combines 4K recording quality, instant shareable links, AI-generated transcripts, and self-hosted storage in a single free, open-source tool. Unlike general-purpose video tools, Cap is optimized for the async sharing workflows developers actually use — paste a link in a README, embed in a docs site, or share in a PR comment. It's free, MIT-licensed, and available for Mac and Windows.",
+ "Cap is the best screen recorder for developer documentation because it combines 4K recording quality, instant shareable links, AI-generated transcripts, and self-hosted storage in a single free, open-source tool. Unlike general-purpose video tools, Cap is optimized for the async sharing workflows developers actually use — paste a link in a README, embed in a docs site, or share in a PR comment. It's free and open source, with most code under AGPLv3, and available for Mac and Windows.",
},
{
question: "Does Cap work for recording API demos and SDK walkthroughs?",
diff --git a/apps/web/components/pages/seo/GoogleDriveScreenRecorderPage.tsx b/apps/web/components/pages/seo/GoogleDriveScreenRecorderPage.tsx
index 76eacc6a0..d0bd57676 100644
--- a/apps/web/components/pages/seo/GoogleDriveScreenRecorderPage.tsx
+++ b/apps/web/components/pages/seo/GoogleDriveScreenRecorderPage.tsx
@@ -134,7 +134,7 @@ const comparisonTable: {
],
[
"Open source",
- { text: "Yes, MIT licensed", status: "positive" },
+ { text: "Yes, AGPLv3 and MIT crates", status: "positive" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
],
diff --git a/apps/web/components/pages/seo/HipaaCompliantScreenRecordingPage.tsx b/apps/web/components/pages/seo/HipaaCompliantScreenRecordingPage.tsx
index b794d7488..a3a6de507 100644
--- a/apps/web/components/pages/seo/HipaaCompliantScreenRecordingPage.tsx
+++ b/apps/web/components/pages/seo/HipaaCompliantScreenRecordingPage.tsx
@@ -27,7 +27,7 @@ export const hipaaCompliantScreenRecordingContent: SeoPageContent = {
{
title: "Open-Source and Auditable",
description:
- "Cap is MIT-licensed and fully open source on GitHub. Your security and compliance teams can audit every line of code. No black-box behavior, no undisclosed data collection — full transparency for your compliance reviews and vendor assessments.",
+ "Cap is open source on GitHub, with most code under AGPLv3 and specified Rust crates under MIT. Your security and compliance teams can audit every line of code. No black-box behavior, no undisclosed data collection — full transparency for your compliance reviews and vendor assessments.",
},
{
title: "Access-Controlled Sharing",
@@ -107,7 +107,7 @@ export const hipaaCompliantScreenRecordingContent: SeoPageContent = {
],
[
"Open-source code",
- { text: "Yes — MIT", status: "positive" },
+ { text: "Yes — AGPLv3 and MIT crates", status: "positive" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
@@ -257,7 +257,7 @@ export const hipaaCompliantScreenRecordingContent: SeoPageContent = {
question:
"Is Cap open source and auditable for HIPAA vendor assessments?",
answer:
- "Yes. Cap is MIT-licensed and fully open source on GitHub. Your security and compliance teams can audit the complete source code to verify data handling behavior, network calls, and storage logic. This transparency simplifies HIPAA vendor security reviews compared to closed-source recording tools.",
+ "Yes. Cap is open source on GitHub, with most code under AGPLv3 and specified Rust crates under MIT. Your security and compliance teams can audit the complete source code to verify data handling behavior, network calls, and storage logic. This transparency simplifies HIPAA vendor security reviews compared to closed-source recording tools.",
},
{
question: "Can I disable AI transcription in Cap for HIPAA compliance?",
diff --git a/apps/web/components/pages/seo/ObsAlternativePage.tsx b/apps/web/components/pages/seo/ObsAlternativePage.tsx
index 545a35a65..67cb6fa2b 100644
--- a/apps/web/components/pages/seo/ObsAlternativePage.tsx
+++ b/apps/web/components/pages/seo/ObsAlternativePage.tsx
@@ -53,7 +53,7 @@ export const obsAlternativeContent: SeoPageContent = {
{
title: "Open Source Like OBS",
description:
- "Cap is MIT-licensed and fully open source on GitHub, just like OBS. You can audit the code, contribute features, and self-host the entire platform. You get OBS's commitment to transparency with the usability of a modern sharing tool.",
+ "Cap is open source on GitHub, with most code under AGPLv3 and specified Rust crates under MIT. You can audit the code, contribute features, and self-host the entire platform. You get OBS's commitment to transparency with the usability of a modern sharing tool.",
},
],
@@ -117,7 +117,7 @@ export const obsAlternativeContent: SeoPageContent = {
],
[
"Open source",
- { text: "Yes — MIT licensed", status: "positive" },
+ { text: "Yes — AGPLv3 and MIT crates", status: "positive" },
{ text: "Yes — GPL licensed", status: "positive" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
@@ -250,7 +250,7 @@ export const obsAlternativeContent: SeoPageContent = {
{
question: "Is Cap open source like OBS?",
answer:
- "Yes. Cap is fully open source and MIT-licensed on GitHub. You can inspect the complete codebase, contribute features, and self-host the entire platform. Both Cap and OBS are open source — but Cap uses the permissive MIT License while OBS uses the GPL.",
+ "Yes. Cap's source is available on GitHub. Most Cap code uses AGPLv3, with specified Rust crates under MIT; OBS uses the GPL. You can inspect the code, contribute features, and self-host the platform.",
},
{
question: "Can Cap do live streaming like OBS?",
diff --git a/apps/web/components/pages/seo/OpenSourceScreenRecorderPage.tsx b/apps/web/components/pages/seo/OpenSourceScreenRecorderPage.tsx
index 737c7190a..4d10007e9 100644
--- a/apps/web/components/pages/seo/OpenSourceScreenRecorderPage.tsx
+++ b/apps/web/components/pages/seo/OpenSourceScreenRecorderPage.tsx
@@ -16,7 +16,7 @@ export const openSourceScreenRecorderContent: SeoPageContent = {
{
title: "Fully Open Source on GitHub",
description:
- "Cap's entire codebase is publicly available and MIT-licensed on GitHub. Inspect every line of code, understand exactly what happens to your recordings, and contribute features or fixes back to the community.",
+ "Cap's source is publicly available on GitHub. Most code is licensed under AGPLv3, with specified Rust crate families under MIT. Inspect every line of code, understand exactly what happens to your recordings, and contribute features or fixes back to the community.",
},
{
title: "Self-Host Your Recordings",
@@ -87,7 +87,7 @@ export const openSourceScreenRecorderContent: SeoPageContent = {
rows: [
[
"Open source",
- { text: "Yes — MIT licensed", status: "positive" },
+ { text: "Yes — AGPLv3 and MIT crates", status: "positive" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
@@ -212,7 +212,7 @@ export const openSourceScreenRecorderContent: SeoPageContent = {
{
question: "Is Cap really open source?",
answer:
- "Yes. Cap is fully open source and MIT-licensed. The complete codebase — desktop app, web app, Rust media processing pipeline, and all backend services — is publicly available on GitHub. You can inspect every line, fork the project, and contribute back to the community.",
+ "Yes. Cap is open source, with most code under AGPLv3 and specified Rust crates under MIT. The complete codebase — desktop app, web app, Rust media processing pipeline, and all backend services — is publicly available on GitHub. You can inspect every line, fork the project, and contribute back to the community.",
},
{
question: "Can I self-host Cap's screen recordings?",
@@ -247,7 +247,7 @@ export const openSourceScreenRecorderContent: SeoPageContent = {
{
question: "What license does Cap use?",
answer:
- "Cap is released under the MIT License, one of the most permissive open-source licenses available. You are free to use, copy, modify, merge, publish, distribute, sublicense, and sell copies of the software. Commercial use is permitted. Attribution is required.",
+ "Most Cap code is licensed under AGPLv3. The cap-camera* and scap-* Rust crate families are licensed under MIT. See the repository LICENSE for the terms that apply to each part of the project.",
},
],
diff --git a/apps/web/components/pages/seo/SelfHostedScreenRecordingPage.tsx b/apps/web/components/pages/seo/SelfHostedScreenRecordingPage.tsx
index 37cf1602f..e9b00a64e 100644
--- a/apps/web/components/pages/seo/SelfHostedScreenRecordingPage.tsx
+++ b/apps/web/components/pages/seo/SelfHostedScreenRecordingPage.tsx
@@ -33,7 +33,7 @@ export const selfHostedScreenRecordingContent: SeoPageContent = {
{
title: "Open Source and Auditable",
description:
- "Cap is MIT-licensed and fully open source on GitHub. Your security team can audit every line of code that handles recording, uploading, and sharing. No black-box behavior, no undisclosed data flows — complete transparency for enterprise security reviews.",
+ "Cap is open source on GitHub, with most code under AGPLv3 and specified Rust crates under MIT. Your security team can audit every line of code that handles recording, uploading, and sharing. No black-box behavior, no undisclosed data flows — complete transparency for enterprise security reviews.",
},
{
title: "Instant Shareable Links from Your Own Storage",
@@ -103,7 +103,7 @@ export const selfHostedScreenRecordingContent: SeoPageContent = {
],
[
"Open source",
- { text: "Yes — MIT", status: "positive" },
+ { text: "Yes — AGPLv3 and MIT crates", status: "positive" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
{ text: "No", status: "negative" },
@@ -206,7 +206,7 @@ export const selfHostedScreenRecordingContent: SeoPageContent = {
{
title: "Self-Hosting Enthusiasts and Privacy Advocates",
description:
- "Individuals and teams who prefer to own their tools deploy the full Cap platform — web app, API, and storage — on their own VPS or cloud account. MIT-licensed code means no restrictions on how you run it or modify it for your needs.",
+ "Individuals and teams who prefer to own their tools deploy the full Cap platform — web app, API, and storage — on their own VPS or cloud account. The repository LICENSE explains the terms for modifying and deploying the code.",
},
],
@@ -250,7 +250,7 @@ export const selfHostedScreenRecordingContent: SeoPageContent = {
{
question: "Can I self-host the entire Cap platform, not just storage?",
answer:
- "Yes. Cap is fully open source under the MIT license. You can deploy the complete Cap platform — including the Next.js web application, API, and all services — on your own infrastructure. See Cap's self-hosting documentation for deployment instructions and configuration options.",
+ "Yes. Cap is open source, with most code under AGPLv3 and specified Rust crates under MIT. You can deploy the complete Cap platform — including the Next.js web application, API, and all services — on your own infrastructure. See Cap's self-hosting documentation for deployment instructions and configuration options.",
},
{
question: "Is self-hosted Cap suitable for HIPAA compliance?",
@@ -266,7 +266,7 @@ export const selfHostedScreenRecordingContent: SeoPageContent = {
{
question: "What is the best self-hosted screen recorder?",
answer:
- "Cap is the best self-hosted screen recorder for teams that need both data control and a modern async video experience. It combines S3-compatible custom storage, instant shareable links, webcam overlay, AI captions, and thread commenting — all in an MIT-licensed open-source package. Unlike local-only recorders, Cap gives you the collaboration layer. Unlike closed-source SaaS tools, Cap gives you full data ownership.",
+ "Cap is the best self-hosted screen recorder for teams that need both data control and a modern async video experience. It combines S3-compatible custom storage, instant shareable links, webcam overlay, AI captions, and thread commenting in an open-source package with most code under AGPLv3. Unlike local-only recorders, Cap gives you the collaboration layer. Unlike closed-source SaaS tools, Cap gives you full data ownership.",
},
],
diff --git a/apps/web/components/pages/seo/VideoRecordingSoftwarePage.tsx b/apps/web/components/pages/seo/VideoRecordingSoftwarePage.tsx
index 657506171..8726b9372 100644
--- a/apps/web/components/pages/seo/VideoRecordingSoftwarePage.tsx
+++ b/apps/web/components/pages/seo/VideoRecordingSoftwarePage.tsx
@@ -36,7 +36,7 @@ export const videoRecordingSoftwareContent: SeoPageContent = {
{
title: "Open Source and Auditable",
description:
- "Cap is fully open-source on GitHub and MIT-licensed. Inspect every line of code, verify how your recordings are handled, and contribute features back to the community. No black boxes, no vendor lock-in. <a href='/open-source-screen-recorder'>Learn more about Cap as open-source software</a>.",
+ "Cap is open source on GitHub, with most code under AGPLv3 and specified Rust crates under MIT. Inspect every line of code, verify how your recordings are handled, and contribute features back to the community. No black boxes, no vendor lock-in. <a href='/open-source-screen-recorder'>Learn more about Cap as open-source software</a>.",
},
{
title: "Separate Screen and Webcam Tracks",
@@ -249,7 +249,7 @@ export const videoRecordingSoftwareContent: SeoPageContent = {
{
question: "Is Cap open-source video recording software?",
answer:
- "Yes. Cap is fully open-source and MIT-licensed. The complete codebase — desktop app, web app, and media processing pipeline — is publicly available on GitHub. You can audit every line of code, fork the project, and contribute back to the community. <a href='/open-source-screen-recorder'>Learn more about Cap as open-source software</a>.",
+ "Yes. Cap is open source, with most code under AGPLv3 and specified Rust crates under MIT. The complete codebase — desktop app, web app, and media processing pipeline — is publicly available on GitHub. You can audit every line of code, fork the project, and contribute back to the community. <a href='/open-source-screen-recorder'>Learn more about Cap as open-source software</a>.",
},
],
diff --git a/apps/web/content/blog/rebuilding-cap-in-gpui.mdx b/apps/web/content/blog/rebuilding-cap-in-gpui.mdx
new file mode 100644
index 000000000..6b3ec0690
--- /dev/null
+++ b/apps/web/content/blog/rebuilding-cap-in-gpui.mdx
@@ -0,0 +1,209 @@
+---
+title: "A technical deep dive into Cap's migration to GPUI"
+description: Why we moved Cap's desktop app off Tauri and web views, how we rebuilt it in GPUI, and what changed once there was no browser inside it.
+publishedAt: "2026-10-21"
+category: Technical
+image: /blog/rebuilding-cap-in-gpui.jpg
+video: /blog/rebuilding-cap-in-gpui.mp4
+author: Richie McIlroy
+tags: Engineering, Rust, GPUI, Performance
+layout: feature
+---
+
+Cap 0.6.1 is out. It's the biggest change we've made to the desktop app since we started, and it's the first version that doesn't have a web browser inside it.
+
+<CapEmbed id="68d8avca11vv12x" title="Cap v0.6.1 release video" />
+
+Until now, Cap's desktop app was built with [Tauri](https://tauri.app). Tauri is a lot like Electron, in that you build your app's interface with HTML, CSS and JavaScript. The difference is that Electron ships a full copy of Chromium inside every app, while Tauri uses the browser engine that already comes with your operating system, which is called a web view. So every window in Cap was really a small web page, running on Safari's engine on a Mac, Microsoft Edge's on Windows and WebKitGTK on Linux.
+
+That kept Cap small and fast for a long time. It also meant our interface was running in three different browsers that we didn't control, and a lot of our bugs came from there. Cap's panels overflowed on Windows if you'd changed your text size, windows showed up blank on some Linux graphics drivers, and area selection on WebKit cancelled itself as soon as you started dragging.
+
+In 0.6.1 every window is drawn on your GPU by Rust code instead, using [GPUI](https://www.gpui.rs), the UI framework the Zed team built for their code editor. It should look and work like the Cap you're used to.
+
+<BeforeAfter />
+
+We had the first GPUI build working overnight in August. We could have shipped it sooner, but we wanted to roll it out safely, so it spent a few weeks as an experimental option in 0.6.0 first. The engine mostly came across as it was, with a few changes to get the most out of the new app.
+
+## Drawn like a game
+
+If I had to explain it in one line, Cap's interface used to work like a web page, and now it works more like a video game.
+
+A web page is a document. Each Cap window was a tree of HTML elements, and the browser had to style, lay out, paint and composite all of it, using whatever browser engine your OS shipped with.
+
+Games work differently. They describe the whole screen and draw it on the GPU each time, and GPUI does the same. When a window changes, GPUI lays it out again and turns it into one flat list of shapes. Cap's main window only uses five kinds of shape.
+
+- **Shadows.** Blurred rounded rectangles under the window and its cards.
+- **Quads.** Every box, with its fill, border and rounded corners.
+- **Glyphs.** The text. Each letter is cut out of a texture that stays on the GPU.
+- **Icons.** SVGs, rasterized once and reused on every frame.
+- **Images.** Full-colour pictures, like the Cap logo.
+
+That list is sent to the GPU in batches and drawn by a handful of small shaders, using Metal on macOS, Direct3D 11 on Windows and Vulkan on Linux. There's no DOM, CSS engine or JavaScript involved. One thing Cap doesn't copy from games is redrawing 60 times a second for no reason. If a window has nothing new to show, it stops asking for frames (more on that further down).
+
+Zed wrote about this in more depth in [Leveraging Rust and the GPU to render user interfaces at 120 FPS](https://zed.dev/blog/videogame).
+
+## Where we started
+
+Cap has always been made of two parts.
+
+The first is the engine. It captures your screen, camera, mic and system audio, writes everything to disk without dropping frames, renders your edits on the GPU and encodes your exports. It's been written in Rust from day one, and it lives in a set of crates (Rust's name for libraries) that don't know anything about windows or buttons.
+
+The second is the interface. Up to 0.6.0 that was a [SolidJS](https://www.solidjs.com) app running inside Tauri. Each Cap window (the main window, the camera bubble, the recording bar, the editor and so on) had its own web view, which is basically a browser tab without the browser around it. The JavaScript running in those web views couldn't touch the recording engine directly, so every click turned into a message to the Rust side, which did the work and sent the result back.
+
+You also don't ship the web view yourself. On a Mac it's WKWebView, which updates along with Safari and macOS. On Windows it's WebView2, which is built on Microsoft Edge and updates itself in the background. On Linux it's WebKitGTK, in whatever version your distro packages. So the browser running Cap's interface was different on every platform, and it could change without us shipping anything.
+
+We love Tauri. It let a small team ship a fast recorder on three platforms, and I don't think Cap would be where it is today without it. For a lot of apps it's still the right choice. Cap just ended up needing more control over its interface than a web view can give.
+
+## Why the rewrite
+
+### Three operating systems, three browsers
+
+Using the system web view means you're really using three different browsers. The same CSS and JavaScript went through three different layout engines and three different text renderers, and each of them had its own quirks. These are some of the ones we ran into this year.
+
+- **Blank windows on Linux.** On some graphics drivers, WebKitGTK's DMA-BUF renderer leaves web view windows completely blank. We had to ship with it turned off.
+- **Text size on Windows.** WebView2 picks up its zoom level from the Windows "Text size" setting, so anyone who had turned that up saw Cap's panels overflow. We ended up pinning the zoom through WebView2's own APIs.
+- **Moving between monitors on Windows.** If you dragged a window onto a monitor with different scaling, WebView2 didn't always pick up the new scale, so we had to listen for the change and pass it on ourselves.
+- **Area selection on WebKit.** When you clicked and dragged to select an area, WebKit could fire a blur event before it had finished capturing the pointer. Our code saw that as the drag ending, so the selection cancelled itself straight away, for both recordings and screenshots.
+- **Lost clicks.** The overlay you pick a display or window from was its own web view. In some cases it closed itself before the click on the record button had made it across to the Rust side, so the click just disappeared. There was no recording and no error, and because the main window had already hidden itself, it looked like Cap had quit.
+- **Crashes in the macOS picker.** Opening and closing the display picker a few times could crash Cap on a Mac. The picker was a web view inside a floating panel, and the library that turned its window into a panel was releasing that window one time too many while WebKit was still attached to it. Fixing it meant forking the panel library.
+
+All of these came from the browser layer, which we couldn't control or update, and we usually heard about them from someone whose setup we'd never seen. Getting rid of that whole category of problem is the main reason for this release. The speed improvements are great and I'll get to them, but more than anything we wanted Cap to behave the same on every computer.
+
+### A detour through Electron
+
+In August I tried moving Cap from Tauri to Electron, mostly out of curiosity. Electron ships its own copy of Chromium, so at least we'd have the same browser on every platform. After a night of optimising, the Electron build was faster than the Tauri app we had in production. That wasn't a completely fair comparison, since I'd made optimisations specifically for the Electron build that the Tauri app never got. It still got me wondering how far we could go with a fully native interface.
+
+<Tweet id="2089061554570293447" />
+
+<Tweet id="2089444596111597621" />
+
+That first GPUI build was a prototype. It was fast, but a lot of the app was missing. We shipped it in 0.6.0 as an opt-in preview under **Settings > Experimental**, then spent the next five weeks filling in the gaps until it could replace the Tauri app completely. That's what 0.6.1 is.
+
+### Getting the community involved
+
+We wanted the community to be part of this, and to show what open source can really do. So I asked for help on X, and said anyone who sent us a decent performance PR would get sponsored on GitHub. A lot of people took us up on it. Since mid-August, 65 people have had pull requests merged into Cap, and 111 of those PRs were performance work from 42 different people. Cap now sponsors 20 developers on GitHub. Two of the export improvements later in this post came from them. [Hona](https://github.com/Hona) fixed Windows exports and [Aqua-123](https://github.com/Aqua-123) made ProRes exports faster.
+
+Having that many people involved also meant Cap was tested on far more kinds of machines than we have on our desks, and that's how a lot of the edge cases turned up.
+
+### Testing all of it
+
+More PRs meant more to test, and CI quickly became the slowest part of the process. GitHub Actions only lets us run five macOS jobs at a time, so PRs sat in a queue waiting for a Mac. [Blacksmith](https://www.blacksmith.sh) generously offered to sponsor Cap's CI, and moving our test suites over to them got rid of the queue. Their jobs start straight away and there's no limit on how many run at once.
+
+We've had help with reviews too. [Greptile](https://www.greptile.com) sponsors the AI code review that checks our PRs for bugs, and [Superagent](https://superagent.sh) keeps watch over security.
+
+## Picking a stack
+
+Moving off the web view meant we had to draw the interface ourselves. Realistically we could use a native toolkit on each platform (AppKit, WinUI and GTK), Electron, or a cross-platform Rust UI framework. Going with native toolkits would have meant building Cap's interface three times. Electron fixes the different-browsers problem by bundling one, but the app gets bigger, and our own tests already had a GPU-drawn app well ahead of it.
+
+We went with GPUI for four reasons.
+
+**It's Rust.** So is Cap's engine. In Tauri, the interface and the engine talked to each other over a bridge, and every command was converted to JSON on one side and parsed on the other. GPUI doesn't need that. The editor calls the same engine crates as the CLI, with the same types, in the same process.
+
+**Everything is drawn on the GPU.** It's the game approach from earlier. There's no browser engine sitting between Cap and the screen, and we can read and change every part of it.
+
+**There's one implementation for every platform.** Layout, text and drawing all run the same code on Mac, Windows and Linux. The only thing that changes is a thin layer at the bottom, which talks to Metal on macOS, Direct3D 11 on Windows and Vulkan on Linux. If there's a layout bug on one platform, it shows up on all of them, so we actually see it and can fix it.
+
+**It still reads like Tailwind.** I didn't expect this to matter as much as it did. Below is the same toggle switch in the old app and in the new one.
+
+<CodeCompare />
+
+Width, height, padding, flex, rounded corners and backgrounds are all written almost the same way. That's a big part of how we managed to port the whole app without redesigning it.
+
+A few people have pointed out that GPUI isn't "native" in the same way AppKit or WinUI is, and they have a point. Cap doesn't use the system's own buttons and text fields. The windows themselves are real platform windows, and everything inside them is drawn by Rust talking directly to the GPU. There's no WebKit, Chromium or JavaScript anywhere in the app.
+
+## How it's built
+
+We rewrote Cap's interface and kept the engine. The old app and the new one share fourteen engine crates, covering screen and camera capture, the recorder, the renderer (backgrounds, zooms, cursors and captions), the editor's playback engine and the exporter. The Cap CLI is built on the same crates and has been exporting videos with no web view since 2024, so we were confident they'd hold up without Tauri. The Tauri app actually exported videos by launching a renamed copy of the CLI as a separate process. The new app just calls the exporter directly.
+
+Your files carry over too. 0.6.1 reads the same projects, settings and recordings as 0.6.0 and updates in place, so you don't need to migrate anything.
+
+### Getting video frames to the screen
+
+The biggest single change is in the editor. When you hit play, Cap's renderer puts together your recording, background, cursor, camera, zooms and captions on the GPU, 60 times a second. That bit is the same in both apps. What's changed is how each finished frame gets onto your screen.
+
+A web page has no access to the GPU memory Rust is using, so in the Tauri app every frame went through six steps.
+
+1. Rendered on the GPU
+2. Copied into regular memory
+3. Sent over a local WebSocket
+4. Passed to a JavaScript worker
+5. Uploaded to the GPU again
+6. Composited by the browser, and then displayed
+
+At the default preview size that adds up to around 216 MB of raw pixels every second, plus about 5,000 lines of Rust and TypeScript that existed purely to make the trip fast enough.
+
+On a Mac, the new app copies each frame into an IOSurface, which is a chunk of graphics memory that macOS lets two renderers share, and GPUI draws directly from that. The frame stays on the GPU the whole way.
+
+<Screenshot src="/blog/gpui/editor-061.webp" width="2200" height="1381" alt="The Cap 0.6.1 editor, with a recording of the Cap website on a gradient background, the background settings on the right and the timeline below" caption="The 0.6.1 editor. The preview, the timeline and every control around them are drawn by GPUI." ribbons="2" />
+
+## Making it feel like the same app
+
+We wanted 0.6.1 to feel like the Cap you already know. So we went through every window (the recorder, target picker, camera bubble, recording bar, settings, editor, screenshot editor, onboarding and teleprompter) and checked it against the old version, one interaction at a time. The engine was the easy part. Most of the hard work was rebuilding things a browser handles for you without you ever thinking about them.
+
+<Screenshot src="/blog/gpui/settings-061.webp" width="1200" height="1189" alt="The General page of Cap 0.6.1's settings, with the appearance picker, toggles and a countdown menu" caption="Settings in 0.6.1. There's no web view behind any of it, down to the toggles and the menu." size="narrow" ribbons="1" />
+
+These are a few of them.
+
+- **Text fields.** In a browser you get typing, selection, copy and paste, undo, accents and input methods for languages like Japanese for free. In GPUI you build the text field yourself on top of the operating system's text input APIs. Ours is about 2,700 lines of Rust.
+- **One-line CSS.** Things like backdrop blur, masks and fancy gradients are one line of CSS. In GPUI some of them became shaders, some needed a patch to the framework, and for some we changed the design. The Liquid Glass background on macOS 26 is the actual system material placed behind the GPUI window, which ended up being easier than trying to fake it.
+- **Windows that aren't normal windows.** In the Tauri app the recording bar was a web view that could only be fully clickable or fully click-through. To make it work we checked where its buttons were 20 times a second and switched the whole window between the two modes. In the new app the bar is a native panel and that loop doesn't exist anymore.
+- **A brand new macOS.** On macOS 26, windows started reporting that they were visible using an undocumented flag, and GPUI waits for a window to be visible before it draws anything. So every window drew a single frame and then froze. We now patch that check when the app launches. Having the framework's source code is what made that fix possible.
+- **Idling properly.** GPUI wakes up on every screen refresh to check if anything needs redrawing, which is 120 times a second on a ProMotion display. We patched it so that if a window has had nothing to draw for 100 ms, it stops checking. An idle Cap window on macOS went from 121 wake-ups a second to 1.
+
+## Performance
+
+### The numbers
+
+These measurements are from August. They compare the Tauri app we were shipping, the Electron experiment and the first GPUI build, on the same Mac with the same recording. You can switch between playback and recording.
+
+<ResourceChart />
+
+Most of the CPU saving comes from the new frame path described above. Most of the memory saving comes from dropping the browser engine. Every Tauri window had its own web view, with its own JavaScript runtime, layout engine and caches, even when it was sitting there doing nothing.
+
+### Exports
+
+Exports never ran inside the web view, so moving to GPUI doesn't speed them up on its own. A lot of export work landed in this release anyway, and on some machines it makes a big difference.
+
+On one of our Windows test laptops, an 83 second recording used to take 99.5 seconds to export at 1080p60. The renderer was waiting on Windows' default 15.6 ms timer between frames, which left the GPU and the hardware encoder idle most of the time. Fixing that, moving audio onto its own thread and removing a few remaining sleeps got it down to about 20 seconds.
+
+On Mac, MP4 exports were already running as fast as Apple's hardware encoder can go, so we worked on everything around it. Exports now use about 11% less CPU, audio is encoded with Apple's AudioToolbox on a separate thread, and ProRes exports use every CPU core instead of just one.
+
+<ExportRace />
+
+Mac exports are smaller now too. Because of a missing setting, the hardware encoder was writing a full keyframe every 12 frames. We changed it to one every two seconds, and a one minute recording exported at 4K Maximum went from 93 MB to 30 MB with no visible drop in quality.
+
+### Download size
+
+Dropping the web view also means there's less to download. Every 0.6.1 download is smaller than before, and on macOS and Linux they're about half the size.
+
+| Download | 0.6.0 | 0.6.1 | Smaller by |
+| --- | ---: | ---: | ---: |
+| macOS, Apple silicon (.dmg) | 189.6 MB | 95.8 MB | 49% |
+| macOS, Intel (.dmg) | 200.9 MB | 106.1 MB | 47% |
+| Windows (.exe) | 203.7 MB | 133.4 MB | 35% |
+| Linux (.deb) | 224.4 MB | 94.9 MB | 58% |
+| Linux (.AppImage) | 264.7 MB | 129.0 MB | 51% |
+| Linux (.rpm) | 202.8 MB | 99.4 MB | 51% |
+
+Not all of that is down to GPUI. 0.6.0 included both apps (the Tauri app and the native preview) plus a second copy of the CLI, and we also switched every installer to stronger compression. The Linux AppImage used to bundle WebKitGTK and all of its dependencies, and our release build now fails if any of them get added back.
+
+## Trade-offs
+
+Going native wasn't free, and some of it we're still paying for.
+
+- **We have to build things the browser used to give us.** Text fields, menus, scroll views and focus handling are all our own code now. The new app is about 182,000 lines of Rust, which is almost exactly the size of the 181,000 lines of TypeScript and Rust it replaced.
+- **Keyboard navigation isn't everywhere yet.** In a browser, tabbing between controls just works. In GPUI we have to set it up window by window, and we haven't finished.
+- **No hot reload.** The old app used Vite, so a style tweak showed up in the running app the moment you saved. Rebuilding the GPUI app only takes a few seconds, but you still relaunch it and click back to whatever you were working on.
+- **A smaller ecosystem.** GPUI is young and it's built for Zed first. There are far fewer libraries to pull in than on the web, and we carry four small patches on top of it.
+
+## Where we go from here
+
+Everything we build from now on will be on top of the native app. Next on the list is full keyboard navigation in every window, Cap in more languages, and more performance work, now that we can change every step from capture to screen.
+
+You can download Cap 0.6.1 at [Cap.so/download](https://cap.so/download), and if you already have Cap installed it'll update in place. The [changelog](/changelog/desktop) lists the 180+ other fixes and improvements that shipped alongside it.
+
+Thanks to everyone who turned on the preview in 0.6.0 and told us what was broken. And a huge thank you to the 41 people who contributed to this release.
+
+@yashranaway, @Hona, @AatmanAJ, @AksharP5, @tadhgdowdall, @jatmn, @celeroncoder, @YoavLevavi, @Theophilus2003, @viralcodex, @unhingedpanda, @notkainoa, @nikolas-sapa, @himanshu748, @dheeraj-juvvadi, @cesarfrancots, @x1xhlol, @wleeaf, @vagxrth, @shubhook, @qflen, @patrickballard, @mihir0209, @melcheikh, @maddiedreese, @hridaya423, @harshav167, @dennisimoo, @colinvkim, @cartesian-plane, @apoorvdarshan, @ajanraj, @Zingzy, @The-Best-Codes, @Supernova-45, @SoulSniper-V2, @OptimistOtaku, @JeffreyLin1, @Ehsan-hash, @EfeDurmaz16 and @Aqua-123.
+
+Richie
diff --git a/apps/web/content/docs/self-hosting.mdx b/apps/web/content/docs/self-hosting.mdx
index 45219a81b..32e16f0f9 100644
--- a/apps/web/content/docs/self-hosting.mdx
+++ b/apps/web/content/docs/self-hosting.mdx
@@ -160,6 +160,12 @@ AI_CHAT_MODEL=your-model # Support chat
AI_STREAM_MODEL=your-model # Streaming docs Ask AI
```
+To keep transcription in the EU, set:
+
+```bash
+ASSEMBLY_BASE_URL=https://api.eu.assemblyai.com
+```
+
For the AssemblyAI EU gateway, set
`AI_BASE_URL=https://llm-gateway.eu.assemblyai.com/v1`. The EU gateway only
serves Claude and Gemini models, so you must also override `AI_MODEL` and
diff --git a/apps/web/data/homepage-copy.ts b/apps/web/data/homepage-copy.ts
index 8b199734e..4dcfd7a16 100644
--- a/apps/web/data/homepage-copy.ts
+++ b/apps/web/data/homepage-copy.ts
@@ -301,7 +301,7 @@ export const homepageCopy: HomePageCopy = {
title: "Simple, Honest Pricing",
subtitle:
"Start free, upgrade when you need more. Early adopter pricing locked in forever.",
- lovedBy: "Trusted by 50,000+ users",
+ lovedBy: "Trusted by 80,000+ users",
commercial: {
title: "Desktop License",
description:
diff --git a/apps/web/instrumentation-client.ts b/apps/web/instrumentation-client.ts
index 408230ef5..401283631 100644
--- a/apps/web/instrumentation-client.ts
+++ b/apps/web/instrumentation-client.ts
@@ -10,6 +10,7 @@ if (dsn) {
replaysSessionSampleRate: 0,
replaysOnErrorSampleRate: 0,
maxBreadcrumbs: 30,
+ denyUrls: [/^(?:chrome|moz|safari(?:-web)?)-extension:\/\//i],
integrations: (integrations) =>
integrations.filter(
({ name }) => name !== "BrowserTracing" && name !== "BrowserSession",
diff --git a/apps/web/lib/assemblyai.ts b/apps/web/lib/assemblyai.ts
index 0c743dc0f..21d1fbc66 100644
--- a/apps/web/lib/assemblyai.ts
+++ b/apps/web/lib/assemblyai.ts
@@ -1,8 +1,18 @@
+import { serverEnv } from "@cap/env";
import {
AI_GENERATION_LANGUAGE_AUTO,
type AiGenerationLanguage,
type AiGenerationLanguageCode,
} from "@cap/web-domain";
+import { AssemblyAI } from "assemblyai";
+
+export function createAssemblyAIClient() {
+ const env = serverEnv();
+ return new AssemblyAI({
+ apiKey: env.ASSEMBLY_API_KEY as string,
+ baseUrl: env.ASSEMBLY_BASE_URL,
+ });
+}
export const ASSEMBLYAI_SPEECH_MODELS = [
"universal-3-5-pro",
diff --git a/apps/web/lib/checkout-quantity.ts b/apps/web/lib/checkout-quantity.ts
new file mode 100644
index 000000000..131b84a1f
--- /dev/null
+++ b/apps/web/lib/checkout-quantity.ts
@@ -0,0 +1,11 @@
+export const MAX_CHECKOUT_QUANTITY = 10000;
+
+export const parseCheckoutQuantity = (quantity: unknown): number | null => {
+ if (quantity === undefined || quantity === null) return 1;
+ return typeof quantity === "number" &&
+ Number.isInteger(quantity) &&
+ quantity >= 1 &&
+ quantity <= MAX_CHECKOUT_QUANTITY
+ ? quantity
+ : null;
+};
diff --git a/apps/web/lib/comment-reply-parent.ts b/apps/web/lib/comment-reply-parent.ts
new file mode 100644
index 000000000..7e9c3e851
--- /dev/null
+++ b/apps/web/lib/comment-reply-parent.ts
@@ -0,0 +1,16 @@
+import { db } from "@cap/database";
+import { comments } from "@cap/database/schema";
+import type { Comment, Video } from "@cap/web-domain";
+import { and, eq } from "drizzle-orm";
+
+export async function isReplyParentOnVideo(
+ parentCommentId: Comment.CommentId,
+ videoId: Video.VideoId,
+) {
+ const [parent] = await db()
+ .select({ id: comments.id })
+ .from(comments)
+ .where(and(eq(comments.id, parentCommentId), eq(comments.videoId, videoId)))
+ .limit(1);
+ return parent !== undefined;
+}
diff --git a/apps/web/lib/desktop-recording-source-checkpoint.ts b/apps/web/lib/desktop-recording-source-checkpoint.ts
index 328a6d253..ec741b9b2 100644
--- a/apps/web/lib/desktop-recording-source-checkpoint.ts
+++ b/apps/web/lib/desktop-recording-source-checkpoint.ts
@@ -1,8 +1,13 @@
import { z } from "zod";
export const SOURCE_COMMIT_PAGE_SIZE = 16;
+// Cap's own AWS bucket answers fast enough to finish 8x larger pages well inside a step's deadline.
+export const SOURCE_COMMIT_AWS_PAGE_SIZE = 128;
export const SOURCE_COMMIT_PART_BATCH_SIZE = 4;
export const SOURCE_COMMIT_PART_PAGE_SIZE = 64;
+// Readers accept larger tree pages than writers produce, so a deployment can be rolled back while commits written by a newer one are in flight.
+export const SOURCE_COMMIT_MAX_TREE_PAGE_ENTRIES = 256;
+export const SOURCE_COMMIT_TREE_PAGE_SIZE = 128;
export const SOURCE_COMMIT_MAX_OBJECTS = 100_002;
const sha256 = z.string().regex(/^[a-f0-9]{64}$/);
diff --git a/apps/web/lib/desktop-recording-source.ts b/apps/web/lib/desktop-recording-source.ts
index 514421bb0..3492c7247 100644
--- a/apps/web/lib/desktop-recording-source.ts
+++ b/apps/web/lib/desktop-recording-source.ts
@@ -9,10 +9,13 @@ import { z } from "zod";
import {
type DesktopRecordingSourceCheckpoint,
desktopRecordingSourceCheckpointSchema,
+ SOURCE_COMMIT_AWS_PAGE_SIZE,
SOURCE_COMMIT_MAX_OBJECTS,
+ SOURCE_COMMIT_MAX_TREE_PAGE_ENTRIES,
SOURCE_COMMIT_PAGE_SIZE,
SOURCE_COMMIT_PART_BATCH_SIZE,
SOURCE_COMMIT_PART_PAGE_SIZE,
+ SOURCE_COMMIT_TREE_PAGE_SIZE,
type SourceCommitPageReference,
type SourceCommitReference,
sourceCommitPageReferenceSchema,
@@ -60,9 +63,10 @@ export type DesktopRecordingSource = z.infer<
>;
type SourceObject = z.infer<typeof sourceObjectSchema>;
type DbVideo = typeof videos.$inferSelect;
-type RecordingStorage = Effect.Effect.Success<
+type RecordingStorageAccess = Effect.Effect.Success<
ReturnType<typeof Storage.getAccessForVideo>
->[0];
+>;
+type RecordingStorage = RecordingStorageAccess[0];
const sourceInventorySchema = z.object({
version: z.literal(1),
@@ -199,6 +203,8 @@ type SourceContext = {
bucket: RecordingStorage;
prefix: string;
run: SourceRun;
+ concurrency: number;
+ pageSize: number;
};
type TreeKind = "plan" | "objects" | "parts";
@@ -229,7 +235,10 @@ const treePageSchema = z.discriminatedUnion("type", [
kind: z.enum(["plan", "objects", "parts"]),
scope: z.string(),
start: z.number().int().nonnegative(),
- entries: z.array(z.unknown()).min(1).max(SOURCE_COMMIT_PART_PAGE_SIZE),
+ entries: z
+ .array(z.unknown())
+ .min(1)
+ .max(SOURCE_COMMIT_MAX_TREE_PAGE_ENTRIES),
}),
z.object({
version: z.literal(1),
@@ -256,6 +265,20 @@ const committedInventorySchema = z.union([
]);
const SOURCE_COMMIT_STEP_TIMEOUT_MS = 180_000;
const SOURCE_COMMIT_IO_TIMEOUT_MS = 60_000;
+const SOURCE_COMMIT_CONCURRENCY = 8;
+const SOURCE_COMMIT_AWS_CONCURRENCY = 128;
+
+function sourceLimits([bucket, customBucket]: RecordingStorageAccess) {
+ return bucket.provider === "s3" && Option.isNone(customBucket)
+ ? {
+ concurrency: SOURCE_COMMIT_AWS_CONCURRENCY,
+ pageSize: SOURCE_COMMIT_AWS_PAGE_SIZE,
+ }
+ : {
+ concurrency: SOURCE_COMMIT_CONCURRENCY,
+ pageSize: SOURCE_COMMIT_PAGE_SIZE,
+ };
+}
function invalidSource(message: string): never {
throw new DesktopRecordingSourceError("source-invalid", message);
@@ -272,12 +295,13 @@ function parseSourceJson(content: string): unknown {
async function mapBounded<T, R>(
items: T[],
operation: (item: T) => Promise<R>,
+ concurrency = SOURCE_COMMIT_CONCURRENCY,
) {
const output = new Array<R>(items.length);
const entries = items.entries();
let failure: unknown;
await Promise.all(
- Array.from({ length: Math.min(8, items.length) }, async () => {
+ Array.from({ length: Math.min(concurrency, items.length) }, async () => {
while (failure === undefined) {
const next = entries.next();
if (next.done) return;
@@ -367,7 +391,7 @@ async function appendTree(
scope: string,
entries: unknown[],
) {
- if (entries.length === 0 || entries.length > SOURCE_COMMIT_PART_PAGE_SIZE)
+ if (entries.length === 0 || entries.length > SOURCE_COMMIT_TREE_PAGE_SIZE)
invalidSource("Invalid source page size");
const start = treeCount(roots);
const leaf = await writeReference(context, {
@@ -424,51 +448,57 @@ async function readTree<T>(
let pending = roots.filter(overlaps);
const leaves: { start: number; entries: T[] }[] = [];
while (pending.length > 0) {
- const pages = await mapBounded(pending, async (reference) => {
- const parsed = treePageSchema.safeParse(
- await readReference(context, reference),
- );
- if (!parsed.success) invalidSource("Recording source page is invalid");
- const page = parsed.data;
- if (page.kind !== kind || page.scope !== scope)
- invalidSource("Recording source page belongs to a different snapshot");
- if (page.type === "leaf") {
+ const pages = await mapBounded(
+ pending,
+ async (reference) => {
+ const parsed = treePageSchema.safeParse(
+ await readReference(context, reference),
+ );
+ if (!parsed.success) invalidSource("Recording source page is invalid");
+ const page = parsed.data;
+ if (page.kind !== kind || page.scope !== scope)
+ invalidSource(
+ "Recording source page belongs to a different snapshot",
+ );
+ if (page.type === "leaf") {
+ if (
+ reference.level !== 0 ||
+ page.start !== reference.start ||
+ page.entries.length !== reference.count
+ )
+ invalidSource(
+ "Recording source leaf does not match its committed range",
+ );
+ return {
+ children: [],
+ leaf: {
+ start: page.start,
+ entries: page.entries.map((entry) => {
+ const parsed = schema.safeParse(entry);
+ if (!parsed.success)
+ invalidSource(
+ "Recording source page contains an invalid receipt",
+ );
+ return parsed.data;
+ }),
+ },
+ };
+ }
+ const [left, right] = page.children;
if (
- reference.level !== 0 ||
- page.start !== reference.start ||
- page.entries.length !== reference.count
+ left.start !== reference.start ||
+ left.start + left.count !== right.start ||
+ left.count + right.count !== reference.count ||
+ left.level + 1 !== reference.level ||
+ right.level !== left.level
)
invalidSource(
- "Recording source leaf does not match its committed range",
+ "Recording source branch does not match its committed range",
);
- return {
- children: [],
- leaf: {
- start: page.start,
- entries: page.entries.map((entry) => {
- const parsed = schema.safeParse(entry);
- if (!parsed.success)
- invalidSource(
- "Recording source page contains an invalid receipt",
- );
- return parsed.data;
- }),
- },
- };
- }
- const [left, right] = page.children;
- if (
- left.start !== reference.start ||
- left.start + left.count !== right.start ||
- left.count + right.count !== reference.count ||
- left.level + 1 !== reference.level ||
- right.level !== left.level
- )
- invalidSource(
- "Recording source branch does not match its committed range",
- );
- return { children: page.children.filter(overlaps), leaf: null };
- });
+ return { children: page.children.filter(overlaps), leaf: null };
+ },
+ context.concurrency,
+ );
pending = [];
for (const page of pages) {
pending.push(...page.children);
@@ -922,6 +952,8 @@ export async function prepareDesktopRecordingSegments(
bucket,
run,
prefix: sourcePrefix(video).slice(0, -1),
+ concurrency: SOURCE_COMMIT_CONCURRENCY,
+ pageSize: SOURCE_COMMIT_PAGE_SIZE,
};
const unique = [
...new Map(
@@ -1153,7 +1185,7 @@ async function advanceSourceSnapshot(
invalidSource("Recording source plan cursor is incomplete");
const end = Math.min(
plan.objectCount,
- checkpoint.cursor + SOURCE_COMMIT_PAGE_SIZE,
+ checkpoint.cursor + context.pageSize,
);
const originals = await mapBounded(
Array.from(
@@ -1166,6 +1198,7 @@ async function advanceSourceSnapshot(
originalAt(context.video, plan, position),
verification,
),
+ context.concurrency,
);
const planRoots = await appendTree(
context,
@@ -1198,7 +1231,7 @@ async function advanceSourceSnapshot(
scope,
originalObjectSchema,
checkpoint.cursor,
- Math.min(plan.objectCount, checkpoint.cursor + SOURCE_COMMIT_PAGE_SIZE),
+ Math.min(plan.objectCount, checkpoint.cursor + context.pageSize),
);
const large =
context.bucket.provider === "s3"
@@ -1235,6 +1268,7 @@ async function advanceSourceSnapshot(
position,
plan.sourcePreparation === true,
),
+ context.concurrency,
);
}
const receiptRoots = await appendTree(
@@ -1261,7 +1295,7 @@ async function advanceSourceSnapshot(
if (checkpoint.phase === "verify") {
const end = Math.min(
plan.objectCount,
- checkpoint.cursor + SOURCE_COMMIT_PAGE_SIZE,
+ checkpoint.cursor + context.pageSize,
);
const [originals, objects] = await Promise.all([
readTree(
@@ -1299,6 +1333,7 @@ async function advanceSourceSnapshot(
);
await checkCopy(context, original, object.key, object.objectIdentity);
},
+ context.concurrency,
);
return {
checkpoint: {
@@ -1419,7 +1454,7 @@ export async function advanceDesktopRecordingSourceCommit(
);
};
try {
- const [bucket] = await run(
+ const access = await run(
Storage.getAccessForVideo(decodeStorageVideo(video), {
resolvePublishedOutput: false,
}),
@@ -1427,9 +1462,10 @@ export async function advanceDesktopRecordingSourceCommit(
const result = await advanceSourceSnapshot(
{
video,
- bucket,
+ bucket: access[0],
run,
prefix: `${sourcePrefix(video)}${parsed.generation}/${parsed.snapshotId}`,
+ ...sourceLimits(access),
},
parsed,
verification,
@@ -1480,9 +1516,10 @@ export async function buildDesktopRecordingSourceUrls(
source: DesktopRecordingSource,
) {
assertSourceKey(video, source.inventoryKey);
- const [bucket] = await runWorkflowPromise(
+ const access = await runWorkflowPromise(
Storage.getAccessForVideo(decodeStorageVideo(video)),
);
+ const [bucket] = access;
const content = await readRequiredObject(bucket, source.inventoryKey);
if (hash(content) !== source.inventorySha256) {
throw new DesktopRecordingSourceError(
@@ -1526,6 +1563,7 @@ export async function buildDesktopRecordingSourceUrls(
bucket,
run,
prefix: sourcePrefix(video).slice(0, -1),
+ ...sourceLimits(access),
},
inventory.roots,
"objects",
diff --git a/apps/web/lib/email-code-rate-limit.ts b/apps/web/lib/email-code-rate-limit.ts
new file mode 100644
index 000000000..95cb0e7e3
--- /dev/null
+++ b/apps/web/lib/email-code-rate-limit.ts
@@ -0,0 +1,32 @@
+import crypto from "node:crypto";
+import { isRateLimited, RATE_LIMIT_IDS } from "@/lib/rate-limit";
+
+export function normalizeSignInEmail(value: unknown) {
+ if (typeof value !== "string") return null;
+ const trimmed = value.trim();
+ if (trimmed.split("@").length !== 2 || trimmed.includes('"')) return null;
+ const [local, domain] = trimmed.toLowerCase().split("@");
+ const host = domain?.split(",")[0];
+ if (!local || !host || !host.includes(".")) return null;
+ return `${local}@${host}`;
+}
+
+// Verification rows are looked up under MySQL's accent-insensitive default
+// collation, so accented spellings of an address share one bucket.
+const emailCodeRateLimitKey = (scope: string, email: string) =>
+ `${scope}:${crypto
+ .createHash("sha256")
+ .update(email.normalize("NFKD").replace(/\p{M}/gu, ""))
+ .digest("hex")}`;
+
+export const isEmailCodeSendLimited = (email: string, headers?: Headers) =>
+ isRateLimited(RATE_LIMIT_IDS.AUTH_OTP_SEND, {
+ key: emailCodeRateLimitKey("email-code-send", email),
+ headers,
+ });
+
+export const isEmailCodeVerifyLimited = (email: string, headers?: Headers) =>
+ isRateLimited(RATE_LIMIT_IDS.AUTH_OTP_VERIFY, {
+ key: emailCodeRateLimitKey("email-code-verify", email),
+ headers,
+ });
diff --git a/apps/web/lib/loom-csv.ts b/apps/web/lib/loom-csv.ts
index 3aaea1f19..99cbb2441 100644
--- a/apps/web/lib/loom-csv.ts
+++ b/apps/web/lib/loom-csv.ts
@@ -50,6 +50,35 @@ export function parseLoomCsvRecords(text: string) {
return records;
}
+export const LOOM_CSV_RESULTS_HEADERS = [
+ "row",
+ "loom_video_url",
+ "user_email",
+ "space_name",
+ "status",
+ "error",
+];
+
+const GUARDED_FORMULA_PREFIX = /^'*[=+\-@\t\r]/;
+const FORMULA_GUARD = /^'(?='*[=+\-@\t\r])/;
+
+export function formatLoomCsvCell(value: string) {
+ const literal = GUARDED_FORMULA_PREFIX.test(value) ? `'${value}` : value;
+ if (/[",\n\r]/.test(literal)) {
+ return `"${literal.replace(/"/g, '""')}"`;
+ }
+ return literal;
+}
+
+export function isLoomCsvResultsFile(headers: string[]) {
+ const normalized = new Set(headers.map((header) => header.toLowerCase()));
+ return LOOM_CSV_RESULTS_HEADERS.every((header) => normalized.has(header));
+}
+
+export function readLoomCsvCell(value: string) {
+ return value.replace(FORMULA_GUARD, "");
+}
+
export function parseConciergeLoomCsv(text: string): LoomCsvImportRow[] {
const records = parseLoomCsvRecords(text);
const headers = records[0]?.map((value) => value.toLowerCase()) ?? [];
diff --git a/apps/web/lib/loom-download-url.ts b/apps/web/lib/loom-download-url.ts
new file mode 100644
index 000000000..1efde716b
--- /dev/null
+++ b/apps/web/lib/loom-download-url.ts
@@ -0,0 +1,281 @@
+import { randomUUID } from "node:crypto";
+
+export class LoomDownloadTemporaryError extends Error {
+ constructor(public readonly retryAfterMs: number) {
+ super("Loom is temporarily unavailable. Please retry the import later.");
+ }
+}
+
+export function getReusableLoomDownloadUrl(value: string | undefined) {
+ if (!value) return null;
+ try {
+ const url = new URL(value);
+ if (
+ url.protocol !== "https:" ||
+ !["cdn.loom.com", "luna.loom.com"].includes(url.hostname) ||
+ url.username ||
+ url.password
+ ) {
+ return null;
+ }
+ if (!url.search) return url.href;
+ let expiresAt = Number(url.searchParams.get("Expires"));
+ const encodedPolicy = url.searchParams.get("Policy");
+ if (encodedPolicy) {
+ const policy: unknown = JSON.parse(
+ Buffer.from(
+ encodedPolicy
+ .replace(/-/g, "+")
+ .replace(/_/g, "=")
+ .replace(/~/g, "/"),
+ "base64",
+ ).toString("utf8"),
+ );
+ if (
+ typeof policy !== "object" ||
+ policy === null ||
+ !("Statement" in policy) ||
+ !Array.isArray(policy.Statement)
+ ) {
+ return null;
+ }
+ const statement = policy.Statement[0] as
+ | { Condition?: { DateLessThan?: { "AWS:EpochTime"?: unknown } } }
+ | undefined;
+ expiresAt = Number(statement?.Condition?.DateLessThan?.["AWS:EpochTime"]);
+ }
+ return Number.isFinite(expiresAt) &&
+ expiresAt * 1000 > Date.now() + 60 * 60 * 1000
+ ? url.href
+ : null;
+ } catch {
+ return null;
+ }
+}
+
+function getRetryAfterMs(value: string | null): number {
+ if (!value) return 60_000;
+ const seconds = Number(value);
+ const delay = Number.isFinite(seconds)
+ ? seconds * 1000
+ : Date.parse(value) - Date.now();
+ return Number.isFinite(delay) ? Math.max(30_000, delay) : 60_000;
+}
+
+async function fetchEndpoint(
+ videoId: string,
+ endpoint: string,
+ includeBody: boolean,
+): Promise<string | null> {
+ let response: Response;
+ try {
+ response = await fetch(
+ `https://www.loom.com/api/campaigns/sessions/${encodeURIComponent(videoId)}/${endpoint}`,
+ {
+ method: "POST",
+ signal: AbortSignal.timeout(15_000),
+ ...(includeBody
+ ? {
+ headers: {
+ "Content-Type": "application/json",
+ Accept: "application/json",
+ },
+ body: JSON.stringify({
+ anonID: randomUUID(),
+ deviceID: null,
+ force_original: false,
+ password: null,
+ }),
+ }
+ : {}),
+ },
+ );
+ } catch {
+ throw new LoomDownloadTemporaryError(60_000);
+ }
+ if (response.status === 429 || response.status >= 500) {
+ await response.body?.cancel();
+ throw new LoomDownloadTemporaryError(
+ getRetryAfterMs(response.headers.get("retry-after")),
+ );
+ }
+ if (!response.ok || response.status === 204) return null;
+ try {
+ const text = await response.text();
+ if (!text.trim()) return null;
+ const data: unknown = JSON.parse(text);
+ return parseLoomDownloadResponse(data);
+ } catch {
+ throw new LoomDownloadTemporaryError(60_000);
+ }
+}
+
+function parseLoomDownloadResponse(data: unknown): string | null {
+ if (
+ typeof data !== "object" ||
+ data === null ||
+ !("url" in data) ||
+ typeof data.url !== "string"
+ ) {
+ return null;
+ }
+ const url = new URL(data.url);
+ if (url.protocol !== "https:" || url.username || url.password) return null;
+ const pathname = url.pathname.toLowerCase();
+ if (
+ (pathname.endsWith(".m3u8") || pathname.endsWith(".mpd")) &&
+ "part_credentials" in data &&
+ data.part_credentials !== null
+ ) {
+ const credentials = data.part_credentials;
+ if (
+ typeof credentials !== "object" ||
+ !("Signature" in credentials) ||
+ typeof credentials.Signature !== "string" ||
+ !credentials.Signature ||
+ !("Key-Pair-Id" in credentials) ||
+ typeof credentials["Key-Pair-Id"] !== "string" ||
+ !credentials["Key-Pair-Id"]
+ ) {
+ throw new LoomDownloadTemporaryError(60_000);
+ }
+ if (
+ "Policy" in credentials &&
+ typeof credentials.Policy === "string" &&
+ credentials.Policy
+ ) {
+ url.searchParams.set("Policy", credentials.Policy);
+ url.searchParams.delete("Expires");
+ } else if (
+ "Expires" in credentials &&
+ Number.isFinite(Number(credentials.Expires)) &&
+ Number(credentials.Expires) > 0
+ ) {
+ url.searchParams.set("Expires", String(credentials.Expires));
+ url.searchParams.delete("Policy");
+ } else {
+ throw new LoomDownloadTemporaryError(60_000);
+ }
+ url.searchParams.set("Signature", credentials.Signature);
+ url.searchParams.set("Key-Pair-Id", credentials["Key-Pair-Id"]);
+ }
+ return url.href;
+}
+
+async function fetchPublicLoomPlaybackUrl(
+ videoId: string,
+): Promise<string | null> {
+ let response: Response;
+ try {
+ response = await fetch("https://www.loom.com/graphql", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Accept: "application/json",
+ },
+ body: JSON.stringify({
+ operationName: "GetVideoSource",
+ variables: {
+ videoId,
+ password: null,
+ acceptableMimes: ["DASH", "M3U8", "MP4", "WEBM"],
+ },
+ query:
+ "query GetVideoSource($videoId: ID!, $password: String, $acceptableMimes: [CloudfrontVideoAcceptableMime]) { getVideo(id: $videoId, password: $password) { ... on RegularUserVideo { id downloadable download_enabled nullableRawCdnUrl(acceptableMimes: $acceptableMimes, password: $password) { url credentials { Policy Signature KeyPairId } } } __typename } }",
+ }),
+ signal: AbortSignal.timeout(15_000),
+ });
+ } catch {
+ throw new LoomDownloadTemporaryError(60_000);
+ }
+ if (response.status === 429 || response.status >= 500) {
+ await response.body?.cancel();
+ throw new LoomDownloadTemporaryError(
+ getRetryAfterMs(response.headers.get("retry-after")),
+ );
+ }
+ if (!response.ok || response.status === 204) return null;
+ try {
+ const body: unknown = await response.json();
+ if (typeof body !== "object" || body === null) return null;
+ if (
+ "errors" in body &&
+ Array.isArray(body.errors) &&
+ body.errors.length > 0
+ )
+ throw new LoomDownloadTemporaryError(60_000);
+ if (
+ !("data" in body) ||
+ typeof body.data !== "object" ||
+ body.data === null ||
+ !("getVideo" in body.data)
+ )
+ return null;
+ const video = body.data.getVideo;
+ if (
+ typeof video !== "object" ||
+ video === null ||
+ !("id" in video) ||
+ video.id !== videoId ||
+ !("downloadable" in video) ||
+ video.downloadable !== true ||
+ !("download_enabled" in video) ||
+ video.download_enabled !== true ||
+ !("nullableRawCdnUrl" in video)
+ )
+ return null;
+ const source = video.nullableRawCdnUrl;
+ if (typeof source !== "object" || source === null)
+ throw new LoomDownloadTemporaryError(60_000);
+ const credentials = "credentials" in source ? source.credentials : null;
+ const partCredentials =
+ typeof credentials === "object" &&
+ credentials !== null &&
+ "Policy" in credentials &&
+ "Signature" in credentials &&
+ "KeyPairId" in credentials
+ ? {
+ Policy: credentials.Policy,
+ Signature: credentials.Signature,
+ "Key-Pair-Id": credentials.KeyPairId,
+ }
+ : null;
+ const url = parseLoomDownloadResponse({
+ ...source,
+ part_credentials: partCredentials,
+ });
+ if (!url) throw new LoomDownloadTemporaryError(60_000);
+ return url;
+ } catch {
+ throw new LoomDownloadTemporaryError(60_000);
+ }
+}
+
+export async function getLoomDownloadUrl(
+ videoId: string,
+): Promise<string | null> {
+ let streamingUrl: string | null = null;
+ for (const { endpoint, includeBody } of [
+ { endpoint: "transcoded-url", includeBody: true },
+ { endpoint: "raw-url", includeBody: true },
+ { endpoint: "transcoded-url", includeBody: false },
+ { endpoint: "raw-url", includeBody: false },
+ ]) {
+ let url: string | null;
+ try {
+ url = await fetchEndpoint(videoId, endpoint, includeBody);
+ } catch (error) {
+ if (streamingUrl && error instanceof LoomDownloadTemporaryError) {
+ return streamingUrl;
+ }
+ throw error;
+ }
+ if (!url) continue;
+ const pathname = new URL(url).pathname.toLowerCase();
+ if (!pathname.endsWith(".m3u8") && !pathname.endsWith(".mpd")) {
+ return url;
+ }
+ streamingUrl ??= url;
+ }
+ return streamingUrl ?? fetchPublicLoomPlaybackUrl(videoId);
+}
diff --git a/apps/web/lib/loom-file-cleanup.ts b/apps/web/lib/loom-file-cleanup.ts
new file mode 100644
index 000000000..3f1aa44c2
--- /dev/null
+++ b/apps/web/lib/loom-file-cleanup.ts
@@ -0,0 +1,47 @@
+import { db } from "@cap/database";
+import { videos } from "@cap/database/schema";
+import { Storage } from "@cap/web-backend/src/Storage/index";
+import { Video } from "@cap/web-domain";
+import { eq } from "drizzle-orm";
+import { Effect } from "effect";
+import { isStorageObjectNotFound } from "@/lib/storage-not-found";
+import { decodeStorageVideo } from "@/lib/video-storage";
+import { runWorkflowPromise } from "@/lib/workflow-runtime";
+
+export async function cleanupLoomFileSource({
+ videoId,
+ userId,
+ rawFileKey,
+}: {
+ videoId: string;
+ userId: string;
+ rawFileKey: string;
+}): Promise<void> {
+ "use step";
+
+ const outputKey = `${userId}/${videoId}/result.mp4`;
+ if (
+ rawFileKey === outputKey ||
+ !rawFileKey.startsWith(`${userId}/${videoId}/`)
+ ) {
+ return;
+ }
+ try {
+ const [video] = await db()
+ .select()
+ .from(videos)
+ .where(eq(videos.id, Video.VideoId.make(videoId)));
+ if (!video || video.ownerId !== userId) return;
+ const [bucket] = await Storage.getAccessForVideo(
+ decodeStorageVideo(video),
+ ).pipe(runWorkflowPromise);
+ const output = await bucket.headObject(outputKey).pipe(
+ Effect.catchIf(isStorageObjectNotFound, () => Effect.succeed(null)),
+ runWorkflowPromise,
+ );
+ if (!output?.ContentLength || output.ContentLength <= 0) return;
+ await bucket.deleteObject(rawFileKey).pipe(runWorkflowPromise);
+ } catch (error) {
+ console.warn("[loom-file] Failed to clean up the processed source", error);
+ }
+}
diff --git a/apps/web/lib/loom-file-processing.ts b/apps/web/lib/loom-file-processing.ts
new file mode 100644
index 000000000..0c9d1c70d
--- /dev/null
+++ b/apps/web/lib/loom-file-processing.ts
@@ -0,0 +1,59 @@
+import {
+ LoomImportStartError,
+ startLoomImportWorkflow,
+} from "@/lib/loom-import-start";
+import {
+ setVideoProcessingError,
+ type startVideoProcessingWorkflow,
+ transitionVideoToProcessing,
+ type VideoProcessingStartStatus,
+} from "@/lib/video-processing";
+
+export const LOOM_FILE_SINGLE_UPLOAD_MAX_BYTES = 5 * 1024 ** 3;
+
+export async function startLoomFileProcessingWorkflow({
+ loomVideoId,
+ ...input
+}: Pick<
+ Parameters<typeof startVideoProcessingWorkflow>[0],
+ | "videoId"
+ | "userId"
+ | "rawFileKey"
+ | "bucketId"
+ | "processingMessage"
+ | "startFailureMessage"
+> & {
+ loomVideoId: string;
+}): Promise<VideoProcessingStartStatus> {
+ if (!/^[a-f0-9]{32}$/i.test(loomVideoId)) {
+ throw new Error("The uploaded file has no valid Loom source mapping.");
+ }
+ const status = await transitionVideoToProcessing({
+ videoId: input.videoId,
+ rawFileKey: input.rawFileKey,
+ processingMessage: input.processingMessage,
+ });
+ if (status !== "started") return status;
+
+ try {
+ await startLoomImportWorkflow({
+ videoId: input.videoId,
+ userId: input.userId,
+ rawFileKey: input.rawFileKey,
+ bucketId: input.bucketId,
+ loomVideoId,
+ reuseExistingRawUpload: true,
+ cleanupRawUpload: true,
+ });
+ return "started";
+ } catch (error) {
+ if (!(error instanceof LoomImportStartError) || error.canRetry) {
+ await setVideoProcessingError(
+ input.videoId,
+ input.startFailureMessage,
+ error,
+ );
+ }
+ throw error;
+ }
+}
diff --git a/apps/web/lib/loom-import-limits.ts b/apps/web/lib/loom-import-limits.ts
new file mode 100644
index 000000000..c40e9853e
--- /dev/null
+++ b/apps/web/lib/loom-import-limits.ts
@@ -0,0 +1,2 @@
+export const LOOM_IMPORT_MAX_BYTES = 10 * 1024 ** 3;
+export const LOOM_IMPORT_PART_BYTES = 64 * 1024 ** 2;
diff --git a/apps/web/lib/loom-import-start.ts b/apps/web/lib/loom-import-start.ts
new file mode 100644
index 000000000..d85097379
--- /dev/null
+++ b/apps/web/lib/loom-import-start.ts
@@ -0,0 +1,179 @@
+import { db } from "@cap/database";
+import { videos, videoUploads } from "@cap/database/schema";
+import type { VideoMetadata } from "@cap/database/types";
+import { User, Video } from "@cap/web-domain";
+import { and, eq, sql } from "drizzle-orm";
+import { getRun, start } from "workflow/api";
+import { getWorld } from "workflow/runtime";
+import { isLoomMediaJobFailed } from "@/lib/loom-media-dispatch";
+import { importLoomVideoWorkflow } from "@/workflows/import-loom-video";
+
+type LoomImportRun = NonNullable<VideoMetadata["loomImportRun"]>;
+type ImportPayload = Parameters<typeof importLoomVideoWorkflow>[0];
+
+export async function restoreLoomImportStartError(
+ videoId: string,
+ phase: "uploading" | "processing",
+ claimedAt: Date,
+ message: string,
+) {
+ await db()
+ .update(videoUploads)
+ .set({
+ phase: "error",
+ processingProgress: 0,
+ processingMessage: null,
+ processingError: message,
+ updatedAt: new Date(),
+ })
+ .where(
+ and(
+ eq(videoUploads.videoId, Video.VideoId.make(videoId)),
+ eq(videoUploads.phase, phase),
+ eq(videoUploads.updatedAt, claimedAt),
+ ),
+ );
+}
+
+export class LoomImportStartError extends Error {
+ constructor(
+ readonly canRetry: boolean,
+ cause: unknown,
+ ) {
+ super(
+ canRetry
+ ? "Loom import could not start. Please try again."
+ : "We are confirming whether your Loom import started. Please check its progress before retrying.",
+ { cause },
+ );
+ this.name = "LoomImportStartError";
+ }
+}
+
+export const LOOM_IMPORT_CONFIRMATION_MESSAGE =
+ "We're confirming whether your Loom import started. Please contact support before retrying.";
+
+export async function getLoomImportRunState(
+ receipt?: LoomImportRun,
+): Promise<"inactive" | "pending" | "active" | "unconfirmed"> {
+ if (!receipt || receipt.dispatch === "rejected") return "inactive";
+ try {
+ const status = await getRun(receipt.runId).status;
+ if (
+ status === "completed" ||
+ status === "failed" ||
+ status === "cancelled"
+ ) {
+ const mediaDispatch = receipt.mediaDispatch;
+ if (mediaDispatch?.state === "accepted") {
+ return mediaDispatch.jobId &&
+ mediaDispatch.videoId &&
+ (await isLoomMediaJobFailed(
+ mediaDispatch.jobId,
+ mediaDispatch.videoId,
+ ))
+ ? "inactive"
+ : "unconfirmed";
+ }
+ return receipt.mediaDispatch?.state === "uncertain"
+ ? "unconfirmed"
+ : "inactive";
+ }
+ return status === "pending" ? "pending" : "active";
+ } catch {
+ return "unconfirmed";
+ }
+}
+
+async function saveReceipt(payload: ImportPayload, receipt: LoomImportRun) {
+ const result = await db()
+ .update(videos)
+ .set({
+ metadata:
+ receipt.dispatch === "pending"
+ ? sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.loomImportRun', JSON_EXTRACT(${JSON.stringify(receipt)}, '$'))`
+ : sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.loomImportRun', JSON_MERGE_PATCH(COALESCE(JSON_EXTRACT(${videos.metadata}, '$.loomImportRun'), JSON_OBJECT()), JSON_EXTRACT(${JSON.stringify(receipt)}, '$')))`,
+ })
+ .where(
+ and(
+ eq(videos.id, Video.VideoId.make(payload.videoId)),
+ eq(videos.ownerId, User.UserId.make(payload.userId)),
+ receipt.dispatch === "pending"
+ ? undefined
+ : sql`JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.loomImportRun.runId')) = ${receipt.runId}`,
+ ),
+ );
+ const affectedRows = Array.isArray(result)
+ ? (result[0] as { affectedRows?: number } | undefined)?.affectedRows
+ : (result as { affectedRows?: number }).affectedRows;
+ if (affectedRows !== 1)
+ throw new Error("Could not record Loom import startup");
+}
+
+function isQueueRejection(error: unknown) {
+ return (
+ error instanceof Error &&
+ [
+ "BadRequestError",
+ "UnauthorizedError",
+ "ForbiddenError",
+ "TooManyRequestsError",
+ ].includes(error.name)
+ );
+}
+
+export async function startLoomImportWorkflow(payload: ImportPayload) {
+ let attempted = false;
+ let accepted = false;
+ let rejected = false;
+ let receipt: LoomImportRun | undefined;
+ try {
+ const world = getWorld();
+ const observedWorld: ReturnType<typeof getWorld> = {
+ ...world,
+ queue: async (name, message, options) => {
+ if (!("runId" in message))
+ throw new Error("Missing Loom import run ID");
+ receipt = { runId: message.runId, dispatch: "pending" };
+ await saveReceipt(payload, receipt);
+ attempted = true;
+ try {
+ const result = await world.queue(name, message, options);
+ accepted = true;
+ return result;
+ } catch (error) {
+ rejected = isQueueRejection(error);
+ throw error;
+ }
+ },
+ };
+ await start(importLoomVideoWorkflow, [payload], { world: observedWorld });
+ } catch (error) {
+ if (!accepted) {
+ let canRetry = !attempted || rejected;
+ if (receipt) {
+ try {
+ await saveReceipt(payload, {
+ ...receipt,
+ dispatch: canRetry ? "rejected" : "uncertain",
+ });
+ } catch (receiptError) {
+ canRetry = false;
+ console.error(
+ "Could not record Loom import dispatch failure",
+ receiptError,
+ );
+ }
+ }
+ throw new LoomImportStartError(canRetry, error);
+ }
+ console.error("Loom import queued despite a startup error", error);
+ }
+ if (receipt) {
+ await saveReceipt(payload, { ...receipt, dispatch: "accepted" }).catch(
+ (error: unknown) => {
+ console.error("Could not record accepted Loom import dispatch", error);
+ },
+ );
+ }
+}
diff --git a/apps/web/lib/loom-import-upload.ts b/apps/web/lib/loom-import-upload.ts
new file mode 100644
index 000000000..ba779b31b
--- /dev/null
+++ b/apps/web/lib/loom-import-upload.ts
@@ -0,0 +1,188 @@
+import { db } from "@cap/database";
+import { videos } from "@cap/database/schema";
+import type { VideoMetadata } from "@cap/database/types";
+import type { Storage } from "@cap/web-backend/src/Storage/index";
+import { and, eq, isNull, sql } from "drizzle-orm";
+import { Effect } from "effect";
+import {
+ LOOM_IMPORT_MAX_BYTES,
+ LOOM_IMPORT_PART_BYTES,
+} from "@/lib/loom-import-limits";
+import { isStorageObjectNotFound } from "@/lib/storage-not-found";
+import { runWorkflowPromise } from "@/lib/workflow-runtime";
+
+export {
+ LOOM_IMPORT_MAX_BYTES,
+ LOOM_IMPORT_PART_BYTES,
+} from "@/lib/loom-import-limits";
+
+type Bucket = Effect.Effect.Success<
+ ReturnType<typeof Storage.getAccessForVideo>
+>[0];
+type UploadReceipt = NonNullable<VideoMetadata["loomImportUpload"]>;
+
+export type LoomMultipartUploadTarget = {
+ type: "multipart";
+ videoId: string;
+ key: string;
+ uploadId: string;
+ partSize: number;
+ signPartUrl: string;
+ completeUrl: string;
+ abortUrl: string;
+ webhookSecret: string;
+ loomImportAttemptId: string;
+};
+
+export async function prepareLoomMultipartUploads({
+ video,
+ bucket,
+ attemptId,
+ sourceKey,
+ webhookBaseUrl,
+ webhookSecret,
+}: {
+ video: typeof videos.$inferSelect;
+ bucket: Bucket;
+ attemptId: string;
+ sourceKey?: string;
+ webhookBaseUrl: string;
+ webhookSecret?: string;
+}): Promise<{
+ sourceUpload?: LoomMultipartUploadTarget;
+ outputUpload?: LoomMultipartUploadTarget;
+ existingOutput?: { url: string; objectIdentity: string; fileSize: number };
+}> {
+ if (bucket.provider !== "s3") return {};
+ if (!webhookSecret)
+ throw new Error(
+ "Loom multipart uploads require media server authentication",
+ );
+ const run = video.metadata?.loomImportRun;
+ if (run && !attemptId.startsWith(`${run.runId}:`)) {
+ throw new Error("Loom import workflow ownership changed");
+ }
+ const previous = video.metadata?.loomImportUpload;
+ const outputKey = `${video.ownerId}/${video.id}/result.mp4`;
+ if (
+ previous?.ownerId === video.ownerId &&
+ previous.organizationId === video.orgId &&
+ previous.output.key === outputKey
+ ) {
+ const output = await bucket.headObject(outputKey).pipe(
+ Effect.catchIf(isStorageObjectNotFound, () => Effect.succeed(null)),
+ runWorkflowPromise,
+ );
+ if (output) {
+ if (
+ !output.ETag ||
+ !/^"[^"\r\n]+"$/.test(output.ETag) ||
+ !output.ContentLength ||
+ output.ContentLength > LOOM_IMPORT_MAX_BYTES
+ ) {
+ throw new Error("Existing Loom output requires storage reconciliation");
+ }
+ return {
+ existingOutput: {
+ url: await bucket
+ .getInternalSignedObjectUrl(outputKey, { expiresIn: 3 * 60 * 60 })
+ .pipe(runWorkflowPromise),
+ objectIdentity: output.ETag,
+ fileSize: output.ContentLength,
+ },
+ };
+ }
+ }
+ if (
+ sourceKey &&
+ (!sourceKey.startsWith(`${video.ownerId}/${video.id}/`) ||
+ sourceKey === outputKey)
+ ) {
+ throw new Error("Loom import source key does not match the recording");
+ }
+ const reusable =
+ previous?.attemptId === attemptId &&
+ previous.ownerId === video.ownerId &&
+ previous.organizationId === video.orgId &&
+ previous.output.key === outputKey &&
+ (!sourceKey || previous.source?.key === sourceKey);
+ let receipt = reusable ? previous : undefined;
+ const created: Array<{ key: string; uploadId: string }> = [];
+ const create = async (key: string) => {
+ const { UploadId: uploadId } = await bucket.multipart
+ .create(key, { ContentType: "video/mp4" })
+ .pipe(runWorkflowPromise);
+ if (!uploadId)
+ throw new Error("Storage did not return a multipart upload id");
+ const upload = { key, uploadId };
+ created.push(upload);
+ return upload;
+ };
+ try {
+ if (!receipt) {
+ receipt = {
+ attemptId,
+ ownerId: video.ownerId,
+ organizationId: video.orgId,
+ source: sourceKey ? await create(sourceKey) : undefined,
+ output: await create(outputKey),
+ };
+ const result = await db()
+ .update(videos)
+ .set({
+ metadata: sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.loomImportUpload', JSON_EXTRACT(${JSON.stringify(receipt)}, '$'))`,
+ })
+ .where(
+ and(
+ eq(videos.id, video.id),
+ eq(videos.ownerId, video.ownerId),
+ video.orgId ? eq(videos.orgId, video.orgId) : isNull(videos.orgId),
+ run
+ ? sql`JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.loomImportRun.runId')) = ${run.runId}`
+ : sql`JSON_EXTRACT(${videos.metadata}, '$.loomImportRun') IS NULL`,
+ previous
+ ? sql`JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.loomImportUpload.attemptId')) = ${previous.attemptId}`
+ : sql`JSON_EXTRACT(${videos.metadata}, '$.loomImportUpload') IS NULL`,
+ ),
+ );
+ const affectedRows = Array.isArray(result)
+ ? (result[0] as { affectedRows?: number } | undefined)?.affectedRows
+ : (result as { affectedRows?: number }).affectedRows;
+ if (affectedRows !== 1)
+ throw new Error("Loom import upload ownership changed");
+ }
+ } catch (error) {
+ await Promise.all(
+ created.map(({ key, uploadId }) =>
+ bucket.multipart.abort(key, uploadId).pipe(
+ Effect.catchAll(() => Effect.void),
+ runWorkflowPromise,
+ ),
+ ),
+ );
+ throw error;
+ }
+ const target = (
+ upload: UploadReceipt["output"],
+ ): LoomMultipartUploadTarget => {
+ const callback = (action: string) =>
+ new URL(`/api/webhooks/media-server/multipart/${action}`, webhookBaseUrl)
+ .href;
+ return {
+ type: "multipart",
+ videoId: video.id,
+ ...upload,
+ partSize: LOOM_IMPORT_PART_BYTES,
+ signPartUrl: callback("sign-part"),
+ completeUrl: callback("complete"),
+ abortUrl: callback("abort"),
+ webhookSecret,
+ loomImportAttemptId: attemptId,
+ };
+ };
+ return {
+ sourceUpload:
+ sourceKey && receipt.source ? target(receipt.source) : undefined,
+ outputUpload: target(receipt.output),
+ };
+}
diff --git a/apps/web/lib/loom-media-dispatch.ts b/apps/web/lib/loom-media-dispatch.ts
new file mode 100644
index 000000000..a9b73e8a6
--- /dev/null
+++ b/apps/web/lib/loom-media-dispatch.ts
@@ -0,0 +1,76 @@
+import { db } from "@cap/database";
+import { videos } from "@cap/database/schema";
+import { serverEnv } from "@cap/env";
+import { User, Video } from "@cap/web-domain";
+import { and, eq, or, sql } from "drizzle-orm";
+
+export async function recordLoomMediaDispatch({
+ videoId,
+ userId,
+ runId,
+ attemptId,
+ state,
+ jobId,
+}: {
+ videoId: string;
+ userId: string;
+ runId: string;
+ attemptId: string;
+ state: "accepted" | "rejected" | "uncertain";
+ jobId?: string;
+}) {
+ const receipt = {
+ runId,
+ dispatch: "accepted",
+ mediaDispatch: { attemptId, videoId, state, jobId: jobId ?? null },
+ };
+ const result = await db()
+ .update(videos)
+ .set({
+ metadata: sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.loomImportRun', JSON_MERGE_PATCH(COALESCE(JSON_EXTRACT(${videos.metadata}, '$.loomImportRun'), JSON_OBJECT()), JSON_EXTRACT(${JSON.stringify(receipt)}, '$')))`,
+ })
+ .where(
+ and(
+ eq(videos.id, Video.VideoId.make(videoId)),
+ eq(videos.ownerId, User.UserId.make(userId)),
+ or(
+ sql`JSON_EXTRACT(${videos.metadata}, '$.loomImportRun') IS NULL`,
+ sql`JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.loomImportRun.runId')) = ${runId}`,
+ ),
+ ),
+ );
+ const affectedRows = Array.isArray(result)
+ ? (result[0] as { affectedRows?: number } | undefined)?.affectedRows
+ : (result as { affectedRows?: number }).affectedRows;
+ if (affectedRows !== 1)
+ throw new Error("Loom media dispatch ownership changed");
+}
+
+export async function isLoomMediaJobFailed(jobId: string, videoId: string) {
+ const env = serverEnv();
+ if (!env.MEDIA_SERVER_URL || !env.MEDIA_SERVER_WEBHOOK_SECRET) return false;
+ try {
+ const response = await fetch(
+ `${env.MEDIA_SERVER_URL}/video/process/${encodeURIComponent(jobId)}/status`,
+ {
+ headers: { "x-media-server-secret": env.MEDIA_SERVER_WEBHOOK_SECRET },
+ signal: AbortSignal.timeout(15_000),
+ redirect: "error",
+ },
+ );
+ if (!response.ok) return false;
+ const progress: unknown = await response.json();
+ return (
+ typeof progress === "object" &&
+ progress !== null &&
+ "jobId" in progress &&
+ progress.jobId === jobId &&
+ "videoId" in progress &&
+ progress.videoId === videoId &&
+ "phase" in progress &&
+ progress.phase === "error"
+ );
+ } catch {
+ return false;
+ }
+}
diff --git a/apps/web/lib/loom-migration-inventory.ts b/apps/web/lib/loom-migration-inventory.ts
new file mode 100644
index 000000000..6c335a4c7
--- /dev/null
+++ b/apps/web/lib/loom-migration-inventory.ts
@@ -0,0 +1,90 @@
+import { db } from "@cap/database";
+import {
+ loomMigrationImports,
+ loomMigrationRequests,
+ videos,
+} from "@cap/database/schema";
+import { and, eq, inArray, isNull, notExists } from "drizzle-orm";
+import { requireMigrationOperator } from "@/lib/loom-concierge";
+import { LoomMigrationError } from "@/lib/loom-migration-state";
+
+const RECONCILE_BATCH_SIZE = 100;
+
+export async function reconcileMissingLoomMigrationImports({
+ requestId,
+ verified,
+}: {
+ requestId: string;
+ verified: boolean;
+}) {
+ const operator = await requireMigrationOperator();
+ if (typeof requestId !== "string" || !/^[A-Za-z0-9_-]{15}$/.test(requestId))
+ throw new LoomMigrationError("Invalid migration request.");
+ if (verified !== true)
+ throw new LoomMigrationError(
+ "Reconcile deleted recordings against the source list first.",
+ );
+ return db().transaction(async (tx) => {
+ const [request] = await tx
+ .select()
+ .from(loomMigrationRequests)
+ .where(eq(loomMigrationRequests.id, requestId))
+ .limit(1)
+ .for("update");
+ if (
+ !request ||
+ request.status === "completed" ||
+ request.activeOrganizationId !== request.organizationId ||
+ (request.activeImportCount > 0 &&
+ (!request.activeImportLeaseUntil ||
+ request.activeImportLeaseUntil > new Date()))
+ ) {
+ throw new LoomMigrationError(
+ "The migration is inactive or imports are starting. Refresh and try again.",
+ );
+ }
+ const missing = await tx
+ .select({ videoId: loomMigrationImports.videoId })
+ .from(loomMigrationImports)
+ .leftJoin(videos, eq(videos.id, loomMigrationImports.videoId))
+ .where(
+ and(eq(loomMigrationImports.requestId, requestId), isNull(videos.id)),
+ )
+ .limit(RECONCILE_BATCH_SIZE + 1)
+ .for("update");
+ if (missing.length === 0) return { removedCount: 0, hasMore: false };
+ const result = await tx.delete(loomMigrationImports).where(
+ and(
+ eq(loomMigrationImports.requestId, requestId),
+ inArray(
+ loomMigrationImports.videoId,
+ missing.slice(0, RECONCILE_BATCH_SIZE).map((row) => row.videoId),
+ ),
+ notExists(
+ tx
+ .select({ id: videos.id })
+ .from(videos)
+ .where(eq(videos.id, loomMigrationImports.videoId)),
+ ),
+ ),
+ );
+ const removedCount = (Array.isArray(result) ? result[0] : result) as {
+ affectedRows?: number;
+ };
+ if (typeof removedCount.affectedRows !== "number")
+ throw new Error("Could not verify inventory reconciliation");
+ const now = new Date();
+ await tx
+ .update(loomMigrationRequests)
+ .set({
+ lastOperatorUserId: operator.id,
+ lastOperatorAt: now,
+ updatedAt: now,
+ })
+ .where(eq(loomMigrationRequests.id, requestId));
+ return {
+ removedCount: removedCount.affectedRows,
+ hasMore: missing.length > RECONCILE_BATCH_SIZE,
+ };
+ });
+}
diff --git a/apps/web/lib/loom-migration-state.ts b/apps/web/lib/loom-migration-state.ts
index 79ab2a6c4..34c29adb1 100644
--- a/apps/web/lib/loom-migration-state.ts
+++ b/apps/web/lib/loom-migration-state.ts
@@ -44,6 +44,41 @@ export function normalizeMigrationText(
return normalized;
}
+export function validateLoomMigrationInventory({
+ organizationId,
+ imports,
+}: {
+ organizationId: string;
+ imports: ReadonlyArray<{
+ videoId: string | null;
+ videoOrganizationId: string | null;
+ mappingId: string | null;
+ mappingOrganizationId: string | null;
+ source: string | null;
+ phase: string | null;
+ }>;
+}) {
+ if (
+ imports.some(
+ (row) =>
+ !row.videoId ||
+ row.videoOrganizationId !== organizationId ||
+ row.mappingId !== row.videoId ||
+ row.mappingOrganizationId !== organizationId ||
+ row.source !== "loom",
+ )
+ ) {
+ throw new LoomMigrationError(
+ "Loom import recordings or source mappings are missing or changed. Re-import missing sources, then use Reconcile deleted recordings for deleted inventory entries. Restore changed mappings before completion.",
+ );
+ }
+ if (imports.some((row) => row.phase !== null && row.phase !== "complete")) {
+ throw new LoomMigrationError(
+ "Loom imports are still processing or have errors. Resolve them before completion.",
+ );
+ }
+}
+
export function validateOperatorMigrationUpdate({
currentStatus,
nextStatus,
diff --git a/apps/web/lib/s3-endpoint-policy.ts b/apps/web/lib/s3-endpoint-policy.ts
new file mode 100644
index 000000000..f30b64a45
--- /dev/null
+++ b/apps/web/lib/s3-endpoint-policy.ts
@@ -0,0 +1,134 @@
+import { lookup as dnsLookup, type LookupAddress } from "node:dns";
+import { lookup } from "node:dns/promises";
+import { Agent as HttpAgent } from "node:http";
+import { Agent as HttpsAgent } from "node:https";
+import { BlockList, isIP, type LookupFunction } from "node:net";
+import { buildEnv, NODE_ENV } from "@cap/env";
+
+const privateRanges = new BlockList();
+
+for (const [network, prefix] of [
+ ["0.0.0.0", 8],
+ ["10.0.0.0", 8],
+ ["100.64.0.0", 10],
+ ["127.0.0.0", 8],
+ ["169.254.0.0", 16],
+ ["172.16.0.0", 12],
+ ["192.0.0.0", 24],
+ ["192.168.0.0", 16],
+ ["198.18.0.0", 15],
+ ["224.0.0.0", 4],
+ ["240.0.0.0", 4],
+] as const) {
+ privateRanges.addSubnet(network, prefix, "ipv4");
+}
+
+// BlockList already applies the IPv4 rules to IPv4-mapped addresses
+// (::ffff:a.b.c.d and ::ffff:7f00:1). Adding ::ffff:0:0/96 here would also
+// match every plain IPv4 address, so only the IPv4-compatible (::/96) and NAT64
+// (64:ff9b::/96) forms are blocked outright.
+for (const [network, prefix] of [
+ ["::", 96],
+ ["64:ff9b::", 96],
+ ["fc00::", 7],
+ ["fe80::", 10],
+ ["fec0::", 10],
+ ["ff00::", 8],
+] as const) {
+ privateRanges.addSubnet(network, prefix, "ipv6");
+}
+
+export function isPrivateAddress(address: string) {
+ const family = isIP(address);
+ if (family === 4) return privateRanges.check(address, "ipv4");
+ if (family === 6) return privateRanges.check(address, "ipv6");
+ return true;
+}
+
+export function shouldBlockPrivateS3Endpoints() {
+ return buildEnv.NEXT_PUBLIC_IS_CAP === "true" && NODE_ENV === "production";
+}
+
+export async function isBlockedS3Endpoint(
+ endpoint: string,
+ resolve: (hostname: string) => Promise<string[]> = async (hostname) =>
+ (await lookup(hostname, { all: true, verbatim: true })).map(
+ (entry) => entry.address,
+ ),
+) {
+ let url: URL;
+ try {
+ url = new URL(endpoint);
+ } catch {
+ return true;
+ }
+
+ if (url.protocol !== "http:" && url.protocol !== "https:") return true;
+ if (url.username || url.password) return true;
+
+ const hostname = url.hostname
+ .toLowerCase()
+ .replace(/^\[|\]$/g, "")
+ .replace(/\.$/, "");
+ if (!hostname) return true;
+ if (
+ hostname === "localhost" ||
+ hostname.endsWith(".localhost") ||
+ hostname.endsWith(".internal")
+ ) {
+ return true;
+ }
+
+ if (isIP(hostname)) return isPrivateAddress(hostname);
+
+ try {
+ const addresses = await resolve(hostname);
+ return addresses.length === 0 || addresses.some(isPrivateAddress);
+ } catch {
+ return true;
+ }
+}
+
+export const publicOnlyLookup: LookupFunction = (
+ hostname,
+ options,
+ callback,
+) => {
+ dnsLookup(hostname, options, (error, address, family) => {
+ if (error) {
+ callback(error, address, family);
+ return;
+ }
+
+ const addresses: LookupAddress[] = Array.isArray(address)
+ ? address
+ : [{ address, family }];
+ const blocked = addresses.find((entry) => isPrivateAddress(entry.address));
+ if (blocked) {
+ const blockedError: NodeJS.ErrnoException = new Error(
+ `Refused to connect to private address ${blocked.address}`,
+ );
+ blockedError.code = "ECONNREFUSED";
+ callback(blockedError, address, family);
+ return;
+ }
+
+ callback(null, address, family);
+ });
+};
+
+export const blockedS3EndpointMessage =
+ "Invalid endpoint. Please provide a public S3-compatible endpoint URL.";
+
+export const isDisallowedS3Endpoint = async (endpoint: string) =>
+ Boolean(endpoint) &&
+ shouldBlockPrivateS3Endpoints() &&
+ (await isBlockedS3Endpoint(endpoint));
+
+export const publicOnlyS3Agents = () =>
+ shouldBlockPrivateS3Endpoints()
+ ? {
+ httpAgent: new HttpAgent({ lookup: publicOnlyLookup }),
+ httpsAgent: new HttpsAgent({ lookup: publicOnlyLookup }),
+ }
+ : undefined;
diff --git a/apps/web/lib/segments-audio-download.ts b/apps/web/lib/segments-audio-download.ts
index 3103e6515..8c7342c98 100644
--- a/apps/web/lib/segments-audio-download.ts
+++ b/apps/web/lib/segments-audio-download.ts
@@ -1,4 +1,5 @@
import { createWriteStream } from "node:fs";
+import { pipeline } from "node:stream/promises";
/**
* Byte-level segment assembly. Lives apart from the pure planning helpers in
@@ -83,15 +84,9 @@ export async function downloadConcatenatedSegments(
);
const fetchImpl = options.fetchImpl ?? fetch;
- const stream = createWriteStream(outputPath);
let totalBytes = 0;
- const write = (chunk: Buffer) =>
- new Promise<void>((resolve, reject) => {
- stream.write(chunk, (error) => (error ? reject(error) : resolve()));
- });
-
- try {
+ async function* downloadSegments() {
for (let start = 0; start < urls.length; start += concurrency) {
const batch = urls.slice(start, start + concurrency);
const buffers = await Promise.all(
@@ -113,12 +108,11 @@ export async function downloadConcatenatedSegments(
`Segment audio exceeds ${MAX_SEGMENTS_AUDIO_BYTES} bytes`,
);
}
- await write(buffer);
+ yield buffer;
}
}
- } finally {
- await new Promise<void>((resolve) => stream.end(() => resolve()));
}
+ await pipeline(downloadSegments(), createWriteStream(outputPath));
return totalBytes;
}
diff --git a/apps/web/lib/seo-metadata.ts b/apps/web/lib/seo-metadata.ts
index dade659ba..99da8b2cb 100644
--- a/apps/web/lib/seo-metadata.ts
+++ b/apps/web/lib/seo-metadata.ts
@@ -229,7 +229,7 @@ export const seoMetadata = {
"open-source-screen-recorder": {
title: "Open Source Screen Recorder — Free, Private, Self-Hostable | Cap",
description:
- "Cap is the leading open-source screen recorder for Mac and Windows. Audit the code, self-host your recordings, and own your data. MIT-licensed, 4K quality, no watermarks.",
+ "Cap is the leading open-source screen recorder for Mac and Windows. Audit the code, self-host your recordings, and own your data. AGPLv3 and MIT crates, 4K quality, no watermarks.",
keywords: [
"open source screen recorder",
"open source screen recording",
@@ -317,7 +317,7 @@ export const seoMetadata = {
title:
"Self-Hosted Screen Recording — Own Your Data, No Vendor Lock-In | Cap",
description:
- "Cap lets you self-host screen recordings on your own S3-compatible storage. Connect AWS S3, Cloudflare R2, or MinIO — recordings go directly to your infrastructure. Open source, MIT-licensed, free to use.",
+ "Cap lets you self-host screen recordings on your own S3-compatible storage. Connect AWS S3, Cloudflare R2, or MinIO — recordings go directly to your infrastructure. Open source with most code under AGPLv3, free to use.",
keywords: [
"self hosted screen recording",
"self hosted screen recorder",
diff --git a/apps/web/lib/share-playback.ts b/apps/web/lib/share-playback.ts
index 00da54028..bb8625a2c 100644
--- a/apps/web/lib/share-playback.ts
+++ b/apps/web/lib/share-playback.ts
@@ -20,9 +20,9 @@ export const getSharePlaybackUrl = (video: SharePlaybackVideo) =>
updatedAt: video.updatedAt.toISOString(),
});
const [bucket] = yield* Storage.getAccessForVideo(loadedVideo);
- return yield* bucket.getSignedObjectUrl(
- `${video.owner.id}/${video.id}/result.mp4`,
- );
+ const source = Video.Video.getSource(loadedVideo);
+ if (!(source instanceof Video.Mp4Source)) return null;
+ return yield* bucket.getSignedObjectUrl(source.getFileKey());
})
.pipe(runPromise)
.catch(() => null);
diff --git a/apps/web/lib/share-video-metadata.ts b/apps/web/lib/share-video-metadata.ts
index 969d25e53..73fb96cd2 100644
--- a/apps/web/lib/share-video-metadata.ts
+++ b/apps/web/lib/share-video-metadata.ts
@@ -15,6 +15,8 @@ export type ShareVideoMetadataInput = {
name: string;
sourceType: ShareVideoSourceType;
webUrl: string;
+ width: number | null;
+ height: number | null;
/**
* Two surfaces only answer on the default Cap origin: `proxy.ts` redirects
* `/embed/` away from a custom domain, and `parseCapShareUrl` accepts share
@@ -79,6 +81,8 @@ export const buildShareVideoMetadata = ({
webUrl,
canonicalWebUrl,
advertiseIframelyPlayer = false,
+ width,
+ height,
}: ShareVideoMetadataInput): Metadata => {
const urls = getShareVideoUrls({
videoId,
@@ -88,6 +92,10 @@ export const buildShareVideoMetadata = ({
});
const title = `${name} | Cap Recording`;
const description = "Watch this video on Cap";
+ const hasDimensions =
+ width != null && height != null && width > 0 && height > 0;
+ const playerWidth = hasDimensions ? width : PLAYER_WIDTH;
+ const playerHeight = hasDimensions ? height : PLAYER_HEIGHT;
return {
title,
@@ -100,7 +108,7 @@ export const buildShareVideoMetadata = ({
rel: "iframely player",
url: urls.playerUrl,
type: "text/html",
- media: "(aspect-ratio: 16/9)",
+ media: `(aspect-ratio: ${playerWidth}/${playerHeight})`,
},
],
},
@@ -142,8 +150,8 @@ export const buildShareVideoMetadata = ({
{
url: urls.streamUrl,
secureUrl: urls.streamUrl,
- width: PLAYER_WIDTH,
- height: PLAYER_HEIGHT,
+ width: playerWidth,
+ height: playerHeight,
type: urls.streamContentType,
},
],
@@ -156,8 +164,8 @@ export const buildShareVideoMetadata = ({
players: {
playerUrl: urls.playerUrl,
streamUrl: urls.streamUrl,
- width: PLAYER_WIDTH,
- height: PLAYER_HEIGHT,
+ width: playerWidth,
+ height: playerHeight,
},
},
other: {
diff --git a/apps/web/lib/storage-not-found.ts b/apps/web/lib/storage-not-found.ts
new file mode 100644
index 000000000..642ff1873
--- /dev/null
+++ b/apps/web/lib/storage-not-found.ts
@@ -0,0 +1,25 @@
+// A transient HEAD failure (5xx, SlowDown, network) must not read as "missing":
+// save-edits would then copy the edited result over the preserved original.
+export function isStorageObjectNotFound(error: unknown): boolean {
+ const visited = new Set<unknown>();
+ let value: unknown = error;
+ for (let depth = 0; depth < 16; depth++) {
+ if (typeof value !== "object" || value === null || visited.has(value))
+ return false;
+ visited.add(value);
+ const record = value as Record<string, unknown>;
+ if (record.name === "NoSuchKey" || record.name === "NotFound") return true;
+ if (record.name === "GoogleDriveRequestError" && record.status === 404)
+ return true;
+ const metadata = record.$metadata;
+ if (
+ typeof metadata === "object" &&
+ metadata !== null &&
+ "httpStatusCode" in metadata &&
+ metadata.httpStatusCode === 404
+ )
+ return true;
+ value = record.cause;
+ }
+ return false;
+}
diff --git a/apps/web/lib/video-edits.ts b/apps/web/lib/video-edits.ts
index 94ce746ee..83ad86853 100644
--- a/apps/web/lib/video-edits.ts
+++ b/apps/web/lib/video-edits.ts
@@ -520,8 +520,17 @@ export function getTimelineDisplaySegments(
const normalized = normalizeTimelineState(state);
const keepRanges = getTimelineKeepRanges(normalized);
const segments: VideoTimelineDisplaySegment[] = [];
+ let deletedRangeIndex = 0;
+ let deletedBefore = 0;
for (const range of keepRanges) {
+ let deletedRange = normalized.deletedRanges[deletedRangeIndex];
+ while (deletedRange && deletedRange.end <= range.start + EPSILON) {
+ deletedBefore += deletedRange.end - deletedRange.start;
+ deletedRangeIndex++;
+ deletedRange = normalized.deletedRanges[deletedRangeIndex];
+ }
+
const boundaries = [
range.start,
...normalized.splitPoints.filter(
@@ -536,11 +545,8 @@ export function getTimelineDisplaySegments(
if (end - start < MIN_RANGE_DURATION) continue;
const id = getSegmentId(start, end);
- const displayStart = mapTimelineSourceTimeToDisplayTime(
- normalized,
- start,
- );
- const displayEnd = mapTimelineSourceTimeToDisplayTime(normalized, end);
+ const displayStart = roundEditTime(Math.max(0, start - deletedBefore));
+ const displayEnd = roundEditTime(Math.max(0, end - deletedBefore));
if (displayEnd - displayStart < MIN_RANGE_DURATION) continue;
segments.push({
@@ -565,6 +571,7 @@ export function getTimelineDisplaySplitPoints(
const segments = getTimelineDisplaySegments(normalized);
const sortedSplitPoints = [...normalized.splitPoints].sort((a, b) => a - b);
const markers: VideoTimelineDisplaySplitPoint[] = [];
+ let splitIndex = 0;
for (let index = 0; index < segments.length - 1; index++) {
const current = segments[index];
@@ -576,11 +583,17 @@ export function getTimelineDisplaySplitPoints(
sourceTimes.push(next.start);
}
- const splitIndices = sortedSplitPoints.flatMap((point, splitIndex) =>
- sourceTimes.some((sourceTime) => Math.abs(point - sourceTime) <= EPSILON)
- ? [splitIndex]
- : [],
- );
+ const splitIndices: number[] = [];
+ for (const sourceTime of sourceTimes) {
+ let point = sortedSplitPoints[splitIndex];
+ while (point !== undefined && point - sourceTime <= EPSILON) {
+ if (Math.abs(point - sourceTime) <= EPSILON) {
+ splitIndices.push(splitIndex);
+ }
+ splitIndex++;
+ point = sortedSplitPoints[splitIndex];
+ }
+ }
const time = current.displayEnd;
markers.push({
id: `${roundEditTime(time)}:${sourceTimes.map(roundEditTime).join(":")}`,
diff --git a/apps/web/lib/video-pipeline-recovery.ts b/apps/web/lib/video-pipeline-recovery.ts
index 060df3439..67b2fda08 100644
--- a/apps/web/lib/video-pipeline-recovery.ts
+++ b/apps/web/lib/video-pipeline-recovery.ts
@@ -23,8 +23,13 @@ import {
import { start } from "workflow/api";
import { isAiGenerationEnabledForUser } from "@/lib/ai-generation-entitlement";
import { startAiGeneration } from "@/lib/generate-ai";
+import {
+ getLoomImportRunState,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE,
+ LoomImportStartError,
+ startLoomImportWorkflow,
+} from "@/lib/loom-import-start";
import { transcribeVideo } from "@/lib/transcribe";
-import { importLoomVideoWorkflow } from "@/workflows/import-loom-video";
import { processVideoWorkflow } from "@/workflows/process-video";
export const STALLED_PIPELINE_MIN_AGE_MS = 60 * 60 * 1000;
@@ -40,11 +45,13 @@ const STALLED_MEDIA_PROCESSING_MESSAGES = [
"Queued for video processing...",
"Queued for Loom import processing...",
"Processing recovery will retry automatically",
+ LOOM_IMPORT_CONFIRMATION_MESSAGE,
] as const;
type PipelineRecoveryStatus =
| "started"
| "already-claimed"
+ | "reconciliation-required"
| "retry-scheduled"
| "failed";
@@ -128,6 +135,7 @@ type MediaCandidate = {
bucketId: string | null;
rawFileKey: string;
loomVideoId: string | null;
+ metadata: VideoMetadata | null;
processingMessage: string;
updatedAt: Date;
};
@@ -135,13 +143,43 @@ type MediaCandidate = {
async function recoverMediaCandidate(
candidate: MediaCandidate,
): Promise<PipelineRecoveryStatus> {
+ const receipt = candidate.metadata?.loomImportRun;
+ const receiptCondition = candidate.loomVideoId
+ ? receipt
+ ? sql`EXISTS (SELECT 1 FROM ${videos} WHERE ${videos.id} = ${candidate.videoId} AND JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.loomImportRun.runId')) = ${receipt.runId})`
+ : sql`EXISTS (SELECT 1 FROM ${videos} WHERE ${videos.id} = ${candidate.videoId} AND JSON_EXTRACT(${videos.metadata}, '$.loomImportRun') IS NULL)`
+ : undefined;
+ if (candidate.loomVideoId) {
+ const state = await getLoomImportRunState(receipt);
+ if (state === "active") return "already-claimed";
+ if (!receipt || state !== "inactive") {
+ const result = await db()
+ .update(videoUploads)
+ .set({
+ processingMessage: LOOM_IMPORT_CONFIRMATION_MESSAGE,
+ updatedAt: new Date(),
+ })
+ .where(
+ and(
+ eq(videoUploads.videoId, candidate.videoId),
+ eq(videoUploads.phase, "processing"),
+ eq(videoUploads.updatedAt, candidate.updatedAt),
+ receiptCondition,
+ ),
+ );
+ return getAffectedRows(result) === 1
+ ? "reconciliation-required"
+ : "already-claimed";
+ }
+ }
+ const claimedAt = new Date(Math.floor(Date.now() / 1000) * 1000);
const claimResult = await db()
.update(videoUploads)
.set({
processingProgress: 0,
processingMessage: "Restarting video processing",
processingError: null,
- updatedAt: new Date(),
+ updatedAt: claimedAt,
})
.where(
and(
@@ -151,6 +189,7 @@ async function recoverMediaCandidate(
eq(videoUploads.processingMessage, candidate.processingMessage),
eq(videoUploads.rawFileKey, candidate.rawFileKey),
eq(videoUploads.updatedAt, candidate.updatedAt),
+ receiptCondition,
),
);
@@ -158,16 +197,14 @@ async function recoverMediaCandidate(
try {
if (candidate.loomVideoId) {
- await start(importLoomVideoWorkflow, [
- {
- videoId: candidate.videoId,
- userId: candidate.userId,
- rawFileKey: candidate.rawFileKey,
- bucketId: candidate.bucketId,
- loomVideoId: candidate.loomVideoId,
- reuseExistingRawUpload: true,
- },
- ]);
+ await startLoomImportWorkflow({
+ videoId: candidate.videoId,
+ userId: candidate.userId,
+ rawFileKey: candidate.rawFileKey,
+ bucketId: candidate.bucketId,
+ loomVideoId: candidate.loomVideoId,
+ reuseExistingRawUpload: true,
+ });
} else {
await start(processVideoWorkflow, [
{
@@ -180,20 +217,27 @@ async function recoverMediaCandidate(
}
return "started";
- } catch {
+ } catch (error) {
+ const needsReconciliation =
+ Boolean(candidate.loomVideoId) &&
+ (!(error instanceof LoomImportStartError) || !error.canRetry);
await db()
.update(videoUploads)
.set({
- processingMessage: "Processing recovery will retry automatically",
+ processingMessage: needsReconciliation
+ ? LOOM_IMPORT_CONFIRMATION_MESSAGE
+ : "Processing recovery will retry automatically",
updatedAt: new Date(),
})
.where(
and(
eq(videoUploads.videoId, candidate.videoId),
eq(videoUploads.phase, "processing"),
+ eq(videoUploads.updatedAt, claimedAt),
+ eq(videoUploads.processingMessage, "Restarting video processing"),
),
);
- return "retry-scheduled";
+ return needsReconciliation ? "reconciliation-required" : "retry-scheduled";
}
}
@@ -300,6 +344,7 @@ export async function recoverStalledVideoPipeline({
bucketId: videos.bucket,
rawFileKey: videoUploads.rawFileKey,
loomVideoId: importedVideos.sourceId,
+ metadata: videos.metadata,
processingMessage: videoUploads.processingMessage,
updatedAt: videoUploads.updatedAt,
})
diff --git a/apps/web/lib/video-processing-recovery.ts b/apps/web/lib/video-processing-recovery.ts
index c7fce3e66..4bdd3d8a7 100644
--- a/apps/web/lib/video-processing-recovery.ts
+++ b/apps/web/lib/video-processing-recovery.ts
@@ -1,5 +1,6 @@
import { db } from "@cap/database";
import { importedVideos, videos, videoUploads } from "@cap/database/schema";
+import type { VideoMetadata } from "@cap/database/types";
import type { S3Bucket, Video } from "@cap/web-domain";
import {
and,
@@ -14,9 +15,14 @@ import {
sql,
} from "drizzle-orm";
import { start } from "workflow/api";
+import {
+ getLoomImportRunState,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE,
+ LoomImportStartError,
+ startLoomImportWorkflow,
+} from "@/lib/loom-import-start";
import { setVideoProcessingError } from "@/lib/video-processing";
import { WORKFLOW_UPGRADE_ERROR_FRAGMENT } from "@/lib/workflow-recovery";
-import { importLoomVideoWorkflow } from "@/workflows/import-loom-video";
import { processVideoWorkflow } from "@/workflows/process-video";
export const VIDEO_PROCESSING_RECOVERY_BATCH_SIZE = 20;
@@ -44,6 +50,7 @@ export type FailedVideoProcessingRecoveryStatus =
| "already-claimed"
| "retry-scheduled"
| "missing-source"
+ | "reconciliation-required"
| "failed";
export type FailedVideoProcessingRecoverySummary = {
@@ -129,16 +136,62 @@ async function recoverLoomCandidate({
loomVideoId,
rawFileKey: existingRawFileKey,
phase,
+ uploadUpdatedAt,
+ receipt,
}: {
videoId: Video.VideoId;
userId: string;
bucketId: S3Bucket.S3BucketId | null;
loomVideoId: string;
rawFileKey: string | null;
- phase: "uploading" | "error";
+ phase: "uploading" | "processing" | "error";
+ uploadUpdatedAt: Date;
+ receipt: VideoMetadata["loomImportRun"];
}): Promise<FailedVideoProcessingRecoveryStatus> {
+ const runState = await getLoomImportRunState(receipt);
+ const receiptCondition = receipt
+ ? sql`EXISTS (SELECT 1 FROM ${videos} WHERE ${videos.id} = ${videoId} AND JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.loomImportRun.runId')) = ${receipt.runId})`
+ : sql`EXISTS (SELECT 1 FROM ${videos} WHERE ${videos.id} = ${videoId} AND JSON_EXTRACT(${videos.metadata}, '$.loomImportRun') IS NULL)`;
+ if (runState === "active") {
+ await db()
+ .update(videoUploads)
+ .set({ updatedAt: new Date() })
+ .where(
+ and(
+ eq(videoUploads.videoId, videoId),
+ eq(videoUploads.phase, phase),
+ eq(videoUploads.updatedAt, uploadUpdatedAt),
+ receiptCondition,
+ ),
+ );
+ return "already-claimed";
+ }
+ if (
+ runState === "pending" ||
+ runState === "unconfirmed" ||
+ (phase === "processing" && !receipt)
+ ) {
+ const result = await db()
+ .update(videoUploads)
+ .set({
+ processingMessage: LOOM_IMPORT_CONFIRMATION_MESSAGE,
+ updatedAt: new Date(),
+ })
+ .where(
+ and(
+ eq(videoUploads.videoId, videoId),
+ eq(videoUploads.phase, phase),
+ eq(videoUploads.updatedAt, uploadUpdatedAt),
+ receiptCondition,
+ ),
+ );
+ return getAffectedRows(result) === 1
+ ? "reconciliation-required"
+ : "already-claimed";
+ }
const rawFileKey =
existingRawFileKey ?? `${userId}/${videoId}/raw-upload.mp4`;
+ const claimedAt = new Date(Math.floor(Date.now() / 1000) * 1000);
const claimResult = await db()
.update(videoUploads)
.set({
@@ -147,21 +200,38 @@ async function recoverLoomCandidate({
processingMessage: LOOM_IMPORT_RECOVERY_MESSAGE,
processingError: null,
rawFileKey,
- updatedAt: new Date(),
+ updatedAt: claimedAt,
})
.where(
and(
eq(videoUploads.videoId, videoId),
eq(videoUploads.phase, phase),
+ eq(videoUploads.updatedAt, uploadUpdatedAt),
+ receiptCondition,
...(phase === "error"
? [
eq(videoUploads.rawFileKey, rawFileKey),
recoverableProcessingError(),
]
- : [
- isNull(videoUploads.rawFileKey),
- eq(videoUploads.processingMessage, LOOM_IMPORT_PENDING_MESSAGE),
- ]),
+ : phase === "processing"
+ ? [
+ existingRawFileKey
+ ? eq(videoUploads.rawFileKey, existingRawFileKey)
+ : isNull(videoUploads.rawFileKey),
+ ]
+ : [
+ isNull(videoUploads.rawFileKey),
+ or(
+ eq(
+ videoUploads.processingMessage,
+ LOOM_IMPORT_PENDING_MESSAGE,
+ ),
+ eq(
+ videoUploads.processingMessage,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE,
+ ),
+ ),
+ ]),
),
);
@@ -170,21 +240,23 @@ async function recoverLoomCandidate({
}
try {
- await start(importLoomVideoWorkflow, [
- {
- videoId,
- userId,
- rawFileKey,
- bucketId,
- loomDownloadUrl: "",
- loomVideoId,
- reuseExistingRawUpload: phase === "error",
- },
- ]);
+ await startLoomImportWorkflow({
+ videoId,
+ userId,
+ rawFileKey,
+ bucketId,
+ loomDownloadUrl: "",
+ loomVideoId,
+ reuseExistingRawUpload:
+ phase !== "uploading" && existingRawFileKey !== null,
+ });
return "started";
} catch (error) {
+ if (!(error instanceof LoomImportStartError) || !error.canRetry) {
+ return "already-claimed";
+ }
const message = error instanceof Error ? error.message : String(error);
- if (phase === "error") {
+ if (phase !== "uploading") {
await db()
.update(videoUploads)
.set({
@@ -199,6 +271,7 @@ async function recoverLoomCandidate({
and(
eq(videoUploads.videoId, videoId),
eq(videoUploads.phase, "processing"),
+ eq(videoUploads.updatedAt, claimedAt),
eq(videoUploads.rawFileKey, rawFileKey),
eq(videoUploads.processingMessage, LOOM_IMPORT_RECOVERY_MESSAGE),
),
@@ -220,6 +293,7 @@ async function recoverLoomCandidate({
and(
eq(videoUploads.videoId, videoId),
eq(videoUploads.phase, "processing"),
+ eq(videoUploads.updatedAt, claimedAt),
eq(videoUploads.rawFileKey, rawFileKey),
eq(videoUploads.processingMessage, LOOM_IMPORT_RECOVERY_MESSAGE),
),
@@ -236,36 +310,90 @@ export async function recoverFailedVideoProcessing({
const staleLoomBefore = new Date(
Date.now() - LOOM_IMPORT_RECOVERY_MIN_AGE_MS,
);
- const pendingLoomCandidates = await db()
- .select({
- videoId: videos.id,
- userId: videos.ownerId,
- bucketId: videos.bucket,
- loomVideoId: importedVideos.sourceId,
- rawFileKey: videoUploads.rawFileKey,
- })
- .from(videos)
- .innerJoin(videoUploads, eq(videos.id, videoUploads.videoId))
- .innerJoin(
- importedVideos,
- and(
- eq(videos.id, importedVideos.id),
- eq(videos.orgId, importedVideos.orgId),
- eq(importedVideos.source, "loom"),
- ),
- )
- .where(
- and(
- eq(importedVideos.source, "loom"),
- eq(videoUploads.phase, "uploading"),
- isNull(videoUploads.rawFileKey),
- eq(videoUploads.processingMessage, LOOM_IMPORT_PENDING_MESSAGE),
- lte(videoUploads.updatedAt, staleLoomBefore),
- ),
+ const selectStaleLoomCandidates = (phase: "uploading" | "processing") =>
+ db()
+ .select({
+ videoId: videos.id,
+ userId: videos.ownerId,
+ bucketId: videos.bucket,
+ loomVideoId: importedVideos.sourceId,
+ rawFileKey: videoUploads.rawFileKey,
+ uploadUpdatedAt: videoUploads.updatedAt,
+ metadata: videos.metadata,
+ phase: videoUploads.phase,
+ })
+ .from(videos)
+ .innerJoin(videoUploads, eq(videos.id, videoUploads.videoId))
+ .innerJoin(
+ importedVideos,
+ and(
+ eq(videos.id, importedVideos.id),
+ eq(videos.orgId, importedVideos.orgId),
+ eq(importedVideos.source, "loom"),
+ ),
+ )
+ .where(
+ and(
+ eq(importedVideos.source, "loom"),
+ eq(videoUploads.phase, phase),
+ phase === "uploading"
+ ? or(
+ and(
+ isNull(videoUploads.rawFileKey),
+ eq(
+ videoUploads.processingMessage,
+ LOOM_IMPORT_PENDING_MESSAGE,
+ ),
+ ),
+ and(
+ sql`JSON_EXTRACT(${videos.metadata}, '$.loomImportRun.runId') IS NOT NULL`,
+ eq(
+ videoUploads.processingMessage,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE,
+ ),
+ ),
+ )
+ : and(
+ sql`JSON_EXTRACT(${videos.metadata}, '$.loomImportRun.runId') IS NOT NULL`,
+ or(
+ eq(
+ videoUploads.processingMessage,
+ LOOM_IMPORT_RECOVERY_MESSAGE,
+ ),
+ eq(videoUploads.processingMessage, "Retrying Loom import..."),
+ eq(
+ videoUploads.processingMessage,
+ "Processing Loom video...",
+ ),
+ eq(
+ videoUploads.processingMessage,
+ LOOM_IMPORT_CONFIRMATION_MESSAGE,
+ ),
+ ),
+ ),
+ lte(videoUploads.updatedAt, staleLoomBefore),
+ ),
+ )
+ .orderBy(asc(videoUploads.updatedAt))
+ .limit(Math.min(limit, LOOM_IMPORT_RECOVERY_BATCH_SIZE));
+ const pendingLoomCandidates = (
+ await Promise.all([
+ selectStaleLoomCandidates("uploading"),
+ selectStaleLoomCandidates("processing"),
+ ])
+ )
+ .flat()
+ .sort(
+ (left, right) =>
+ left.uploadUpdatedAt.getTime() - right.uploadUpdatedAt.getTime(),
)
- .orderBy(asc(videoUploads.updatedAt))
- .limit(Math.min(limit, LOOM_IMPORT_RECOVERY_BATCH_SIZE));
+ .slice(0, Math.min(limit, LOOM_IMPORT_RECOVERY_BATCH_SIZE));
+ const loomRecoveryBudget = Math.min(limit, LOOM_IMPORT_RECOVERY_BATCH_SIZE);
+ const startupCountWithFailedImport = Math.min(
+ pendingLoomCandidates.length,
+ Math.max(0, loomRecoveryBudget - 1),
+ );
const failedLoomUploads = await db()
.select({
videoId: videos.id,
@@ -273,6 +401,8 @@ export async function recoverFailedVideoProcessing({
orgId: videos.orgId,
bucketId: videos.bucket,
rawFileKey: videoUploads.rawFileKey,
+ uploadUpdatedAt: videoUploads.updatedAt,
+ metadata: videos.metadata,
})
.from(videos)
.innerJoin(videoUploads, eq(videos.id, videoUploads.videoId))
@@ -285,13 +415,7 @@ export async function recoverFailedVideoProcessing({
),
)
.orderBy(asc(videoUploads.updatedAt))
- .limit(
- Math.max(
- 0,
- Math.min(limit, LOOM_IMPORT_RECOVERY_BATCH_SIZE) -
- pendingLoomCandidates.length,
- ),
- );
+ .limit(Math.max(0, loomRecoveryBudget - startupCountWithFailedImport));
const failedLoomCandidates = (
await Promise.all(
@@ -315,10 +439,15 @@ export async function recoverFailedVideoProcessing({
).filter((candidate) => candidate !== null);
const loomCandidates = [
- ...pendingLoomCandidates.map((candidate) => ({
- ...candidate,
- phase: "uploading" as const,
- })),
+ ...pendingLoomCandidates
+ .slice(0, Math.max(0, loomRecoveryBudget - failedLoomCandidates.length))
+ .map((candidate) => ({
+ ...candidate,
+ phase:
+ candidate.phase === "processing"
+ ? ("processing" as const)
+ : ("uploading" as const),
+ })),
...failedLoomCandidates.map((candidate) => ({
...candidate,
phase: "error" as const,
@@ -374,6 +503,8 @@ export async function recoverFailedVideoProcessing({
loomVideoId: candidate.loomVideoId,
rawFileKey: candidate.rawFileKey,
phase: candidate.phase,
+ uploadUpdatedAt: candidate.uploadUpdatedAt,
+ receipt: candidate.metadata?.loomImportRun,
});
} catch (error) {
status = "failed";
diff --git a/apps/web/lib/video-processing.ts b/apps/web/lib/video-processing.ts
index 54333399d..1e43663f6 100644
--- a/apps/web/lib/video-processing.ts
+++ b/apps/web/lib/video-processing.ts
@@ -67,6 +67,7 @@ export async function transitionVideoToProcessing({
: and(
eq(videoUploads.videoId, videoId),
ne(videoUploads.phase, "processing"),
+ ne(videoUploads.phase, "generating_thumbnail"),
// "complete" is terminal for implicit starts: a multipart
// completion retried after an uncertain network outcome can
// land after the first processing run already finished, and
@@ -89,7 +90,10 @@ export async function transitionVideoToProcessing({
throw new Error("No upload record found");
}
- if (upload.phase === "processing") {
+ if (
+ upload.phase === "processing" ||
+ upload.phase === "generating_thumbnail"
+ ) {
return "already-processing";
}
diff --git a/apps/web/lib/video-status-query.ts b/apps/web/lib/video-status-query.ts
new file mode 100644
index 000000000..722dcbbe4
--- /dev/null
+++ b/apps/web/lib/video-status-query.ts
@@ -0,0 +1,28 @@
+import type { Video } from "@cap/web-domain";
+import {
+ getVideoStatus,
+ type VideoStatusResult,
+} from "@/actions/videos/get-status";
+
+export class VideoStatusNotFoundError extends Error {
+ constructor() {
+ super("This video is no longer available");
+ this.name = "VideoStatusNotFoundError";
+ }
+}
+
+export function videoStatusQueryOptions(videoId: Video.VideoId) {
+ return {
+ queryKey: ["videoStatus", videoId],
+ queryFn: async (): Promise<VideoStatusResult> => {
+ const result = await getVideoStatus(videoId);
+ if ("success" in result) {
+ if (result.reason === "not_found") throw new VideoStatusNotFoundError();
+ throw new Error("Failed to fetch video status");
+ }
+ return result;
+ },
+ retry: (failureCount: number, error: Error) =>
+ !(error instanceof VideoStatusNotFoundError) && failureCount < 3,
+ };
+}
diff --git a/apps/web/lib/video-storage-key.ts b/apps/web/lib/video-storage-key.ts
new file mode 100644
index 000000000..6d22be47d
--- /dev/null
+++ b/apps/web/lib/video-storage-key.ts
@@ -0,0 +1,12 @@
+export function isVideoStorageKey(
+ video: { id: string; ownerId: string },
+ key: string,
+) {
+ return (
+ key.startsWith(`${video.ownerId}/${video.id}/`) &&
+ !key.includes("\\") &&
+ key
+ .split("/")
+ .every((part) => part.length > 0 && part !== "." && part !== "..")
+ );
+}
diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs
index 657f6a3bb..ce8e40d17 100644
--- a/apps/web/next.config.mjs
+++ b/apps/web/next.config.mjs
@@ -96,6 +96,23 @@ const nextConfig = {
},
].filter(Boolean),
},
+ async headers() {
+ return [
+ {
+ source: "/(.*)",
+ headers: [
+ { key: "X-Content-Type-Options", value: "nosniff" },
+ { key: "X-DNS-Prefetch-Control", value: "off" },
+ ],
+ },
+ {
+ source: "/((?!api/auth/).*)",
+ headers: [
+ { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
+ ],
+ },
+ ];
+ },
async rewrites() {
return [
{
diff --git a/apps/web/package.json b/apps/web/package.json
index 66e0418cf..3508f64ca 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -136,6 +136,7 @@
"react-rnd": "^10.4.1",
"react-scroll-parallax": "^3.4.5",
"react-tooltip": "^5.26.3",
+ "react-tweet": "^3.3.1",
"recharts": "^3.3.0",
"rehype-pretty-code": "^0.14.1",
"remark-gfm": "^4.0.1",
diff --git a/apps/web/proxy.ts b/apps/web/proxy.ts
index 3f2a43bac..80acfcc16 100644
--- a/apps/web/proxy.ts
+++ b/apps/web/proxy.ts
@@ -35,7 +35,42 @@ const mainOrigins = [
addHttps(serverEnv().VERCEL_PROJECT_PRODUCTION_URL_HOST),
].filter(Boolean) as string[];
+const FRAME_PROTECTED_PATHS = [
+ "/dashboard",
+ "/onboarding",
+ "/invite",
+ "/cli/authorize",
+ "/mcp/authorize",
+ "/admin",
+ "/signup",
+ "/verify-otp",
+];
+
+const isFrameProtectedPath = (path: string) =>
+ FRAME_PROTECTED_PATHS.some(
+ (protectedPath) =>
+ path === protectedPath || path.startsWith(`${protectedPath}/`),
+ );
+
+const setFrameHeaders = (response: NextResponse) => {
+ response.headers.set("X-Frame-Options", "SAMEORIGIN");
+ response.headers.set(
+ "Content-Security-Policy",
+ "frame-ancestors https://cap.so",
+ );
+ return response;
+};
+
export async function proxy(request: NextRequest) {
+ const path = new URL(request.url).pathname;
+
+ if (path.startsWith("/login")) return setFrameHeaders(NextResponse.next());
+
+ const response = await route(request);
+ return isFrameProtectedPath(path) ? setFrameHeaders(response) : response;
+}
+
+async function route(request: NextRequest) {
const url = new URL(request.url);
const path = url.pathname;
@@ -43,16 +78,6 @@ export async function proxy(request: NextRequest) {
return NextResponse.redirect(new URL("/dashboard/caps", url.origin));
}
- if (path.startsWith("/login")) {
- const response = NextResponse.next();
- response.headers.set("X-Frame-Options", "SAMEORIGIN");
- response.headers.set(
- "Content-Security-Policy",
- "frame-ancestors https://cap.so",
- );
- return response;
- }
-
const shareIframeRedirectUrl = getShareIframeRedirectUrl({
method: request.method,
requestUrl: request.url,
diff --git a/apps/web/public/backgrounds/hero/rays-far.webp b/apps/web/public/backgrounds/hero/rays-far.webp
new file mode 100644
index 000000000..8b3a49b8c
Binary files /dev/null and b/apps/web/public/backgrounds/hero/rays-far.webp differ
diff --git a/apps/web/public/backgrounds/hero/rays-near.webp b/apps/web/public/backgrounds/hero/rays-near.webp
new file mode 100644
index 000000000..d48a34cb0
Binary files /dev/null and b/apps/web/public/backgrounds/hero/rays-near.webp differ
diff --git a/apps/web/public/blog/gpui/cursor-macos.svg b/apps/web/public/blog/gpui/cursor-macos.svg
new file mode 100644
index 000000000..06d64fb1b
--- /dev/null
+++ b/apps/web/public/blog/gpui/cursor-macos.svg
@@ -0,0 +1,18 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="17" height="24" viewBox="0 0 17 24" fill="none">
+<g filter="url(#filter0_d_1382_341)">
+<path fill-rule="evenodd" clip-rule="evenodd" d="M4.501 3.2601L12.884 11.6611C13.937 12.7171 13.19 14.5191 11.699 14.5191L10.475 14.519L11.6908 17.4067C11.9038 17.9127 11.9068 18.4727 11.6998 18.9817C11.4918 19.4917 11.0978 19.8897 10.5898 20.1027C10.3338 20.2097 10.0658 20.2637 9.7918 20.2637C8.9608 20.2637 8.2158 19.7687 7.8938 19.0027L6.616 15.965L5.784 16.7031C4.703 17.6591 3 16.8921 3 15.4481V3.8811C3 3.0971 3.947 2.7051 4.501 3.2601Z" fill="white"/>
+</g>
+<path fill-rule="evenodd" clip-rule="evenodd" d="M4 4.53033C4 4.39933 4.159 4.33333 4.251 4.42633L12.159 12.3513C12.59 12.7833 12.284 13.5203 11.674 13.5203L8.97 13.5188L10.7696 17.7947C10.9966 18.3347 10.7426 18.9557 10.2036 19.1817C9.6626 19.4087 9.0426 19.1557 8.8166 18.6167L6.999 14.2928L5.139 15.9403C4.723 16.3083 4.0811 16.0518 4.007 15.5285L4 15.4273V4.53033Z" fill="black"/>
+<defs>
+<filter id="filter0_d_1382_341" x="0" y="0" width="16.375" height="23.2637" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
+<feFlood flood-opacity="0" result="BackgroundImageFix"/>
+<feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>
+<feOffset/>
+<feGaussianBlur stdDeviation="1.5"/>
+<feComposite in2="hardAlpha" operator="out"/>
+<feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.6 0"/>
+<feBlend mode="normal" in2="BackgroundImageFix" result="effect1_dropShadow_1382_341"/>
+<feBlend mode="normal" in="SourceGraphic" in2="effect1_dropShadow_1382_341" result="shape"/>
+</filter>
+</defs>
+</svg>
\ No newline at end of file
diff --git a/apps/web/public/blog/gpui/cursor-windows.svg b/apps/web/public/blog/gpui/cursor-windows.svg
new file mode 100644
index 000000000..81a3415ad
--- /dev/null
+++ b/apps/web/public/blog/gpui/cursor-windows.svg
@@ -0,0 +1,18 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="18" height="25" viewBox="0 0 18 25" fill="none">
+<g filter="url(#filter0_d_1382_318)">
+<path d="M3.3125 18.3749V3.75391L13.9027 14.3874H8.84609L11.4221 20.5586L8.84609 21.508L6.36552 15.4317L3.3125 18.3749Z" fill="white"/>
+<path d="M3.3125 18.3749V3.75391L13.9027 14.3874H8.84609L11.4221 20.5586L8.84609 21.508L6.36552 15.4317L3.3125 18.3749Z" stroke="black" stroke-width="0.622951"/>
+</g>
+<defs>
+<filter id="filter0_d_1382_318" x="0" y="0" width="17.6523" height="24.9043" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
+<feFlood flood-opacity="0" result="BackgroundImageFix"/>
+<feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>
+<feOffset/>
+<feGaussianBlur stdDeviation="1.5"/>
+<feComposite in2="hardAlpha" operator="out"/>
+<feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.6 0"/>
+<feBlend mode="normal" in2="BackgroundImageFix" result="effect1_dropShadow_1382_318"/>
+<feBlend mode="normal" in="SourceGraphic" in2="effect1_dropShadow_1382_318" result="shape"/>
+</filter>
+</defs>
+</svg>
\ No newline at end of file
diff --git a/apps/web/public/blog/gpui/editor-061.webp b/apps/web/public/blog/gpui/editor-061.webp
new file mode 100644
index 000000000..89799cb67
Binary files /dev/null and b/apps/web/public/blog/gpui/editor-061.webp differ
diff --git a/apps/web/public/blog/gpui/main-window-060-glass.png b/apps/web/public/blog/gpui/main-window-060-glass.png
new file mode 100644
index 000000000..6b327e9fb
Binary files /dev/null and b/apps/web/public/blog/gpui/main-window-060-glass.png differ
diff --git a/apps/web/public/blog/gpui/main-window-061-glass.png b/apps/web/public/blog/gpui/main-window-061-glass.png
new file mode 100644
index 000000000..3261aad2b
Binary files /dev/null and b/apps/web/public/blog/gpui/main-window-061-glass.png differ
diff --git a/apps/web/public/blog/gpui/settings-061.webp b/apps/web/public/blog/gpui/settings-061.webp
new file mode 100644
index 000000000..3298a4fd2
Binary files /dev/null and b/apps/web/public/blog/gpui/settings-061.webp differ
diff --git a/apps/web/public/blog/rebuilding-cap-in-gpui-poster.jpg b/apps/web/public/blog/rebuilding-cap-in-gpui-poster.jpg
new file mode 100644
index 000000000..628f5e5f5
Binary files /dev/null and b/apps/web/public/blog/rebuilding-cap-in-gpui-poster.jpg differ
diff --git a/apps/web/public/blog/rebuilding-cap-in-gpui.jpg b/apps/web/public/blog/rebuilding-cap-in-gpui.jpg
new file mode 100644
index 000000000..e60cb1bb0
Binary files /dev/null and b/apps/web/public/blog/rebuilding-cap-in-gpui.jpg differ
diff --git a/apps/web/public/blog/rebuilding-cap-in-gpui.mp4 b/apps/web/public/blog/rebuilding-cap-in-gpui.mp4
new file mode 100644
index 000000000..2ca2e6116
Binary files /dev/null and b/apps/web/public/blog/rebuilding-cap-in-gpui.mp4 differ
diff --git a/apps/web/public/llms-full.txt b/apps/web/public/llms-full.txt
index c7413553e..dae751a68 100644
--- a/apps/web/public/llms-full.txt
+++ b/apps/web/public/llms-full.txt
@@ -169,7 +169,7 @@ Current desktop app version: 0.5.3.
- **Enterprise** — Custom. SLAs & priority support, SAML SSO & SCIM provisioning, managed self-hosting, volume discounts, advanced security controls, dedicated onboarding.
- **Discounts** — STUDENT50 (30% off, students), MIGRATE20 (20% off first year, Loom switchers); volume discounts for teams over 10 users.
-Early-adopter pricing is locked in for the lifetime of the subscription. Trusted by 50,000+ users, teams, and creators.
+Early-adopter pricing is locked in for the lifetime of the subscription. Trusted by 80,000+ users, teams, and creators.
## How Cap Compares
diff --git a/apps/web/utils/blog.ts b/apps/web/utils/blog.ts
index 6c25bb99a..73c87dffe 100644
--- a/apps/web/utils/blog.ts
+++ b/apps/web/utils/blog.ts
@@ -11,6 +11,7 @@ export type PostMetadata = {
description: string;
tags?: string;
image?: string;
+ video?: string;
};
export type DocMetadata = {
diff --git a/apps/web/workflows/finalize-desktop-recording.ts b/apps/web/workflows/finalize-desktop-recording.ts
index 43706a882..cc8323318 100644
--- a/apps/web/workflows/finalize-desktop-recording.ts
+++ b/apps/web/workflows/finalize-desktop-recording.ts
@@ -4,6 +4,7 @@ import { serverEnv } from "@cap/env";
import { Storage } from "@cap/web-backend/src/Storage/index";
import { type User, Video } from "@cap/web-domain";
import { and, eq } from "drizzle-orm";
+import { Option } from "effect";
import { sleep } from "workflow";
import { z } from "zod";
import { isAiGenerationEnabledForUser } from "@/lib/ai-generation-entitlement";
@@ -83,8 +84,12 @@ const COMPLETION_POLL_INTERVAL_MS = 15_000;
const ATTEMPT_MAX_DURATION_MS = 6 * 60 * 60 * 1_000;
const PRESIGNED_EXPIRES_SECONDS = 6 * 60 * 60;
const MULTIPART_OUTPUT_PART_SIZE_BYTES = 64 * 1024 * 1024;
+// A remux is about as large as its source, so a source that fits in one part is
+// uploaded with one PUT instead of sign-part + PUT + complete round trips.
+const SINGLE_PUT_OUTPUT_MAX_SOURCE_BYTES = MULTIPART_OUTPUT_PART_SIZE_BYTES;
const SOURCE_COMMIT_BATCH_MAX_CHECKPOINTS = 32;
const SOURCE_COMMIT_BATCH_DURATION_MS = 15_000;
+const TRANSCRIPTION_QUEUE_MAX_ATTEMPTS = 12;
function getErrorMessage(error: unknown) {
return error instanceof Error ? error.message : String(error);
@@ -115,7 +120,8 @@ export async function finalizeDesktopRecordingWorkflow(
): Promise<WorkflowResult> {
"use workflow";
- const generation = await ensureWorkflowJob(payload);
+ // Every step costs a queue hop (~0.7 s in prod), so skip the lookup when the caller already knows the generation.
+ const generation = payload.generation ?? (await ensureWorkflowJob(payload));
let completedJobId: string | undefined;
let mediaServerUnavailable = false;
let retainedAttempt: DesktopRecordingAttempt | undefined;
@@ -137,19 +143,21 @@ export async function finalizeDesktopRecordingWorkflow(
}
const attempt = next.attempt;
try {
- let committed = await commitDesktopRecordingAttempt(attempt);
- while (committed === "progress") {
- committed = await commitDesktopRecordingAttempt(attempt);
+ let committed = await commitAndStartDesktopRecordingAttempt(attempt);
+ while (committed.committed === "progress") {
+ committed = await commitAndStartDesktopRecordingAttempt(attempt);
}
- if (committed !== "ready") {
- return { success: false, reason: committed };
+ if (committed.committed !== "ready") {
+ return { success: false, reason: committed.committed };
}
- if (await completeWithoutMediaServerIfUnavailable(attempt)) {
- mediaServerUnavailable = true;
- break;
- }
- const started = await startDesktopRecordingJob(attempt);
+ const started = committed.dispatched
+ ? committed.started
+ : await startDesktopRecordingJob(attempt);
if (typeof started === "object") {
+ if (started.status === "media-server-unconfigured") {
+ mediaServerUnavailable = true;
+ break;
+ }
if (started.status === "capacity") {
retainedAttempt = attempt;
await sleep(started.retryAfterMs);
@@ -190,7 +198,7 @@ export async function finalizeDesktopRecordingWorkflow(
if (retained !== "retry") return { success: false, reason: retained };
}
}
- for (let attempt = 0; ; attempt++) {
+ for (let attempt = 0; attempt < TRANSCRIPTION_QUEUE_MAX_ATTEMPTS; attempt++) {
let queued: boolean;
try {
queued = await queueFinalizedRecordingTranscription(
@@ -201,7 +209,9 @@ export async function finalizeDesktopRecordingWorkflow(
queued = false;
}
if (queued) break;
- await sleep(Math.min(15_000 * 2 ** Math.min(attempt, 5), 300_000));
+ if (attempt + 1 < TRANSCRIPTION_QUEUE_MAX_ATTEMPTS) {
+ await sleep(Math.min(15_000 * 2 ** Math.min(attempt, 5), 300_000));
+ }
}
return mediaServerUnavailable
? { success: false, reason: "media-server-unconfigured" }
@@ -268,6 +278,36 @@ export async function commitDesktopRecordingAttempt(
): Promise<SourceCommitResult> {
"use step";
+ return commitDesktopRecordingBatch(attempt);
+}
+
+// Each step costs a queue hop, so a commit that finishes inside its batch budget dispatches in the same step.
+// A slow commit returns without dispatching and the workflow runs startDesktopRecordingJob as its own step.
+export async function commitAndStartDesktopRecordingAttempt(
+ attempt: DesktopRecordingAttempt,
+): Promise<
+ | { committed: SourceCommitResult; dispatched?: undefined }
+ | { committed: "ready"; dispatched: true; started: DesktopRecordingDispatch }
+> {
+ "use step";
+
+ const startedAt = Date.now();
+ const committed = await commitDesktopRecordingBatch(attempt);
+ if (
+ committed !== "ready" ||
+ Date.now() - startedAt >= SOURCE_COMMIT_BATCH_DURATION_MS
+ )
+ return { committed };
+ return {
+ committed,
+ dispatched: true,
+ started: await dispatchDesktopRecordingJob(attempt),
+ };
+}
+
+async function commitDesktopRecordingBatch(
+ attempt: DesktopRecordingAttempt,
+): Promise<SourceCommitResult> {
const startedAt = Date.now();
for (let index = 0; index < SOURCE_COMMIT_BATCH_MAX_CHECKPOINTS; index++) {
try {
@@ -293,6 +333,7 @@ export async function commitDesktopRecordingAttempt(
async function advanceDesktopRecordingAttempt(
attempt: DesktopRecordingAttempt,
): Promise<SourceCommitResult> {
+ const startedAt = performance.now();
const current = await getProcessingState(attempt);
if (!current || current.attemptId !== attempt.attemptId) return "superseded";
if (current.source) return "ready";
@@ -306,24 +347,45 @@ async function advanceDesktopRecordingAttempt(
try {
const checkpoint = await initializeSourceCommitCheckpoint(attempt);
if (!checkpoint) return "superseded";
+ const loadedAt = performance.now();
+ let heartbeatMs = 0;
const result = await advanceDesktopRecordingSourceCommit(
video,
checkpoint,
current.verification ?? undefined,
async () => {
- if (!(await heartbeatAttempt(attempt))) {
+ const heartbeatAt = performance.now();
+ const alive = await heartbeatAttempt(attempt);
+ heartbeatMs += performance.now() - heartbeatAt;
+ if (!alive) {
throw new Error("Recording source commit lease was superseded");
}
},
);
- if ("checkpoint" in result) {
- return (await persistSourceCommitCheckpoint(attempt, result.checkpoint))
- ? "progress"
- : "superseded";
- }
- return (await persistCommittedSource(attempt, result.source))
- ? "ready"
- : "superseded";
+ const advancedAt = performance.now();
+ const outcome =
+ "checkpoint" in result
+ ? (await persistSourceCommitCheckpoint(attempt, result.checkpoint))
+ ? "progress"
+ : "superseded"
+ : (await persistCommittedSource(attempt, result.source))
+ ? "ready"
+ : "superseded";
+ console.info(
+ JSON.stringify({
+ event: "desktop_source_commit_advance",
+ videoId: attempt.videoId,
+ attemptId: attempt.attemptId,
+ phase: checkpoint.phase,
+ next: "checkpoint" in result ? result.checkpoint.phase : "committed",
+ outcome,
+ loadMs: Math.round(loadedAt - startedAt),
+ heartbeatMs: Math.round(heartbeatMs),
+ advanceMs: Math.round(advancedAt - loadedAt),
+ persistMs: Math.round(performance.now() - advancedAt),
+ }),
+ );
+ return outcome;
} catch (error) {
const code = getSourceErrorCode(error);
if (!code) throw error;
@@ -339,8 +401,6 @@ async function advanceDesktopRecordingAttempt(
async function completeWithoutMediaServerIfUnavailable(
attempt: DesktopRecordingAttempt,
): Promise<boolean> {
- "use step";
-
if (serverEnv().MEDIA_SERVER_URL) return false;
const deferred = await deferWithoutMediaServer(attempt);
if (deferred) {
@@ -356,11 +416,13 @@ async function completeWithoutMediaServerIfUnavailable(
async function buildDesktopSegmentsOutput({
video,
attempt,
+ sourceBytes,
}: {
video: typeof videos.$inferSelect;
attempt: DesktopRecordingAttemptFence;
+ sourceBytes: number;
}) {
- const [bucket] = await Storage.getAccessForVideo(
+ const [bucket, customBucket] = await Storage.getAccessForVideo(
decodeStorageVideo(video),
).pipe(runWorkflowPromise);
const outputKey = getDesktopRecordingOutputKey(
@@ -390,7 +452,11 @@ async function buildDesktopSegmentsOutput({
url: outputPresignedUrl,
ifNoneMatch: "*",
};
- if (bucket.provider === "s3" && webhookSecret) {
+ const singlePut =
+ bucket.provider === "s3" &&
+ Option.isNone(customBucket) &&
+ sourceBytes <= SINGLE_PUT_OUTPUT_MAX_SOURCE_BYTES;
+ if (bucket.provider === "s3" && webhookSecret && !singlePut) {
const multipart = await bucket.multipart
.create(outputKey, { ContentType: "video/mp4" })
.pipe(runWorkflowPromise);
@@ -446,16 +512,28 @@ async function buildDesktopSegmentsOutput({
};
}
-export async function startDesktopRecordingJob(
- attempt: DesktopRecordingAttempt,
-): Promise<
+type DesktopRecordingDispatch =
| string
| undefined
| { status: "deferred" }
| { status: "capacity"; retryAfterMs: number }
-> {
+ | { status: "media-server-unconfigured" };
+
+export async function startDesktopRecordingJob(
+ attempt: DesktopRecordingAttempt,
+): Promise<DesktopRecordingDispatch> {
"use step";
+ return dispatchDesktopRecordingJob(attempt);
+}
+
+async function dispatchDesktopRecordingJob(
+ attempt: DesktopRecordingAttempt,
+): Promise<DesktopRecordingDispatch> {
+ if (await completeWithoutMediaServerIfUnavailable(attempt)) {
+ return { status: "media-server-unconfigured" };
+ }
+
const current = await getProcessingState(attempt);
if (!current || current.attemptId !== attempt.attemptId || !current.source) {
throw new Error("Recording processing attempt was superseded");
@@ -497,16 +575,17 @@ export async function startDesktopRecordingJob(
}
throw error;
});
+ const sourceBytes = urls.sourceObjects.reduce(
+ (total, object) => total + object.size,
+ 0,
+ );
let downloadBudgetBytes: number;
try {
downloadBudgetBytes = await reserveMediaProcessingBudget({
videoId: current.videoId,
generation: current.generation,
attemptId: attempt.attemptId,
- sourceBytes: urls.sourceObjects.reduce(
- (total, object) => total + object.size,
- 0,
- ),
+ sourceBytes,
});
} catch (error) {
if (error instanceof MediaProcessingBudgetError) {
@@ -574,7 +653,7 @@ export async function startDesktopRecordingJob(
audioLevels: true,
...context,
...urls,
- ...(await buildDesktopSegmentsOutput({ video, attempt })),
+ ...(await buildDesktopSegmentsOutput({ video, attempt, sourceBytes })),
manifestSha256: current.source.manifestSha256,
};
}
@@ -780,7 +859,7 @@ async function queueFinalizedRecordingTranscription(
);
if (result.success) return true;
console.warn(
- "[finalizeDesktopRecordingWorkflow] Transcription enqueue will retry",
+ "[finalizeDesktopRecordingWorkflow] Transcription enqueue failed",
{ videoId, message: result.message },
);
} catch (error) {
diff --git a/apps/web/workflows/import-loom-video.ts b/apps/web/workflows/import-loom-video.ts
index e821e92d8..2177e5f00 100644
--- a/apps/web/workflows/import-loom-video.ts
+++ b/apps/web/workflows/import-loom-video.ts
@@ -1,4 +1,3 @@
-import { randomUUID } from "node:crypto";
import { db } from "@cap/database";
import { agentApiOperations, videos, videoUploads } from "@cap/database/schema";
import { serverEnv } from "@cap/env";
@@ -6,11 +5,32 @@ import { Storage } from "@cap/web-backend/src/Storage/index";
import { Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { Effect } from "effect";
-import { FatalError, sleep } from "workflow";
+import {
+ FatalError,
+ getWorkflowMetadata,
+ RetryableError,
+ sleep,
+} from "workflow";
+import {
+ getLoomDownloadUrl,
+ getReusableLoomDownloadUrl,
+ LoomDownloadTemporaryError,
+} from "@/lib/loom-download-url";
+import { cleanupLoomFileSource } from "@/lib/loom-file-cleanup";
+import {
+ LOOM_IMPORT_MAX_BYTES,
+ type LoomMultipartUploadTarget,
+ prepareLoomMultipartUploads,
+} from "@/lib/loom-import-upload";
+import {
+ isLoomMediaJobFailed,
+ recordLoomMediaDispatch,
+} from "@/lib/loom-media-dispatch";
import {
createMediaServerCapacityError,
isMediaServerCapacityError,
} from "@/lib/media-server-backpressure";
+import { isStorageObjectNotFound } from "@/lib/storage-not-found";
import { runWorkflowPromise } from "@/lib/workflow-runtime";
import {
type ProcessedVideoMetadata,
@@ -27,6 +47,7 @@ interface ImportLoomPayload {
loomDownloadUrl?: string;
agentOperationId?: string;
reuseExistingRawUpload?: boolean;
+ cleanupRawUpload?: boolean;
}
const MINIMUM_VIDEO_SIZE = 1024;
@@ -44,72 +65,32 @@ function isStreamingUrl(url: string): boolean {
return path.endsWith(".m3u8") || path.endsWith(".mpd");
}
-async function fetchLoomCdnUrl(
- videoId: string,
- endpoint: string,
- includeBody: boolean,
-): Promise<string | null> {
+async function fetchFreshLoomDownloadUrl(
+ loomVideoId: string,
+ existingUrl?: string,
+): Promise<string> {
+ "use step";
+
+ const reusableUrl = getReusableLoomDownloadUrl(existingUrl);
+ if (reusableUrl) return reusableUrl;
try {
- const options: RequestInit = {
- method: "POST",
- signal: AbortSignal.timeout(15_000),
- };
- if (includeBody) {
- options.headers = {
- "Content-Type": "application/json",
- Accept: "application/json",
- };
- options.body = JSON.stringify({
- anonID: randomUUID(),
- deviceID: null,
- force_original: false,
- password: null,
+ const url = await getLoomDownloadUrl(loomVideoId);
+ if (url) return url;
+ } catch (error) {
+ if (error instanceof LoomDownloadTemporaryError) {
+ throw new RetryableError(error.message, {
+ retryAfter: error.retryAfterMs,
});
}
-
- const response = await fetch(
- `https://www.loom.com/api/campaigns/sessions/${videoId}/${endpoint}`,
- options,
- );
-
- if (!response.ok || response.status === 204) return null;
-
- const text = await response.text();
- if (!text.trim()) return null;
-
- const data = JSON.parse(text) as { url?: string };
- return data.url ?? null;
- } catch {
- return null;
- }
-}
-
-async function fetchFreshLoomDownloadUrl(loomVideoId: string): Promise<string> {
- const requestVariants: Array<{ endpoint: string; includeBody: boolean }> = [
- { endpoint: "transcoded-url", includeBody: true },
- { endpoint: "raw-url", includeBody: true },
- { endpoint: "transcoded-url", includeBody: false },
- { endpoint: "raw-url", includeBody: false },
- ];
-
- let fallbackStreamingUrl: string | null = null;
-
- for (const { endpoint, includeBody } of requestVariants) {
- const url = await fetchLoomCdnUrl(loomVideoId, endpoint, includeBody);
- if (!url) continue;
-
- if (!isStreamingUrl(url)) return url;
-
- if (!fallbackStreamingUrl) fallbackStreamingUrl = url;
+ throw error;
}
-
- if (fallbackStreamingUrl) return fallbackStreamingUrl;
-
throw new FatalError(
"Could not retrieve a download URL from Loom. The video may be private, password-protected, or the link may have expired.",
);
}
+fetchFreshLoomDownloadUrl.maxRetries = 8;
+
interface VideoProcessingResult {
success: boolean;
message: string;
@@ -125,6 +106,41 @@ interface LoomProcessingInput {
importFromLoom?: boolean;
}
+async function requireLoomMediaServerSupport() {
+ "use step";
+
+ const env = serverEnv();
+ if (!env.MEDIA_SERVER_URL)
+ throw new FatalError("MEDIA_SERVER_URL is not configured");
+ const response = await fetch(`${env.MEDIA_SERVER_URL}/video/status`, {
+ headers: env.MEDIA_SERVER_WEBHOOK_SECRET
+ ? { "x-media-server-secret": env.MEDIA_SERVER_WEBHOOK_SECRET }
+ : undefined,
+ signal: AbortSignal.timeout(15_000),
+ });
+ if (!response.ok)
+ throw new RetryableError(
+ "Could not verify Loom import worker capabilities",
+ { retryAfter: 60_000 },
+ );
+ const capabilities: unknown = await response.json();
+ if (
+ typeof capabilities !== "object" ||
+ capabilities === null ||
+ !("loomMultipartUploads" in capabilities) ||
+ capabilities.loomMultipartUploads !== true ||
+ !("loomImportMaxBytes" in capabilities) ||
+ capabilities.loomImportMaxBytes !== LOOM_IMPORT_MAX_BYTES
+ ) {
+ throw new RetryableError(
+ "The media server must be updated for 10 GB Loom imports",
+ { retryAfter: 60_000 },
+ );
+ }
+}
+
+requireLoomMediaServerSupport.maxRetries = 60;
+
async function claimAgentImport(operationId: string | undefined) {
"use step";
@@ -195,6 +211,7 @@ export async function importLoomVideoWorkflow(
): Promise<VideoProcessingResult> {
"use workflow";
+ const { workflowRunId } = getWorkflowMetadata();
if (!(await claimAgentImport(payload.agentOperationId))) {
return { success: true, message: "Loom import is already running" };
}
@@ -204,13 +221,29 @@ export async function importLoomVideoWorkflow(
let processingInput: LoomProcessingInput = reuseExistingRawUpload
? {}
: { importFromLoom: true };
+ await requireLoomMediaServerSupport();
let metadata: ProcessedVideoMetadata;
for (let processingAttempt = 0; ; processingAttempt++) {
+ let mediaJobId: string;
let capacityRetryCount = 0;
+ let previousSourceUrl =
+ processingAttempt === 0 ? payload.loomDownloadUrl : undefined;
while (true) {
try {
- await processVideoOnMediaServer(payload, processingInput);
+ const loomSourceUrl = processingInput.importFromLoom
+ ? await fetchFreshLoomDownloadUrl(
+ payload.loomVideoId,
+ previousSourceUrl,
+ )
+ : undefined;
+ previousSourceUrl = loomSourceUrl;
+ mediaJobId = await processVideoOnMediaServer(
+ payload,
+ loomSourceUrl,
+ `${workflowRunId}:${processingAttempt}`,
+ workflowRunId,
+ );
break;
} catch (error) {
if (!isMediaServerCapacityError(error)) throw error;
@@ -229,6 +262,11 @@ export async function importLoomVideoWorkflow(
) {
throw error;
}
+ if (!(await confirmLoomMediaJobFailure(mediaJobId, payload.videoId))) {
+ throw new FatalError(
+ "The previous Loom worker must be reconciled before retrying",
+ );
+ }
await markLoomImportWaitingForCapacity(payload.videoId);
if (await hasExistingLoomUpload(payload)) processingInput = {};
await sleep(15_000 * (processingAttempt + 1));
@@ -236,6 +274,7 @@ export async function importLoomVideoWorkflow(
}
await saveMetadataAndComplete(payload.videoId, metadata);
await completeAgentImport(payload.agentOperationId, payload.videoId);
+ if (payload.cleanupRawUpload) await cleanupLoomFileSource(payload);
return {
success: true,
@@ -268,6 +307,12 @@ function getInputExtension(url: string): string | undefined {
return undefined;
}
+async function confirmLoomMediaJobFailure(jobId: string, videoId: string) {
+ "use step";
+
+ return isLoomMediaJobFailed(jobId, videoId);
+}
+
async function hasExistingLoomUpload(
payload: ImportLoomPayload,
): Promise<boolean> {
@@ -290,18 +335,33 @@ async function hasExistingLoomUpload(
metadata: video.metadata,
});
const [bucket] = yield* Storage.getAccessForVideo(videoDomain);
- const object = yield* bucket.headObject(payload.rawFileKey);
- return (object.ContentLength ?? 0) >= MINIMUM_VIDEO_SIZE;
- }).pipe(
- Effect.catchAll(() => Effect.succeed(false)),
- runWorkflowPromise,
- );
+ const previous = video.metadata?.loomImportUpload;
+ if (
+ previous?.ownerId === video.ownerId &&
+ previous.organizationId === video.orgId &&
+ previous.output.key === `${video.ownerId}/${video.id}/result.mp4`
+ ) {
+ const savedOutput = yield* bucket.headObject(previous.output.key).pipe(
+ Effect.map(
+ (object) => (object.ContentLength ?? 0) >= MINIMUM_VIDEO_SIZE,
+ ),
+ Effect.catchIf(isStorageObjectNotFound, () => Effect.succeed(false)),
+ );
+ if (savedOutput) return true;
+ }
+ return yield* bucket.headObject(payload.rawFileKey).pipe(
+ Effect.map((object) => (object.ContentLength ?? 0) >= MINIMUM_VIDEO_SIZE),
+ Effect.catchIf(isStorageObjectNotFound, () => Effect.succeed(false)),
+ );
+ }).pipe(runWorkflowPromise);
}
async function waitForRetry(delayMs: number): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, delayMs));
}
+class LoomMediaRequestRejectedError extends Error {}
+
async function startMediaServerProcessJob(
mediaServerUrl: string,
body: {
@@ -309,6 +369,10 @@ async function startMediaServerProcessJob(
userId: string;
videoUrl: string;
sourcePresignedUrl?: string;
+ sourceUpload?: LoomMultipartUploadTarget;
+ outputUpload?: LoomMultipartUploadTarget;
+ existingOutput?: { url: string; objectIdentity: string; fileSize: number };
+ maxImportBytes: number;
outputPresignedUrl: string;
thumbnailPresignedUrl: string;
previewGifPresignedUrl: string;
@@ -317,6 +381,10 @@ async function startMediaServerProcessJob(
inputExtension?: string;
priority: "bulk";
},
+ recordDispatch: (
+ state: "accepted" | "rejected" | "uncertain",
+ jobId?: string,
+ ) => Promise<void>,
): Promise<string> {
for (let attempt = 0; attempt < MEDIA_SERVER_START_MAX_ATTEMPTS; attempt++) {
const headers: Record<string, string> = {
@@ -326,7 +394,9 @@ async function startMediaServerProcessJob(
headers["x-media-server-secret"] = body.webhookSecret;
}
- const endpoint = body.sourcePresignedUrl ? "import" : "process";
+ const endpoint =
+ body.sourcePresignedUrl || body.sourceUpload ? "import" : "process";
+ await recordDispatch("uncertain");
const response = await fetch(`${mediaServerUrl}/video/${endpoint}`, {
method: "POST",
headers,
@@ -334,10 +404,22 @@ async function startMediaServerProcessJob(
});
if (response.ok) {
- const { jobId } = (await response.json()) as { jobId: string };
+ const accepted: unknown = await response.json();
+ if (
+ typeof accepted !== "object" ||
+ accepted === null ||
+ !("jobId" in accepted) ||
+ typeof accepted.jobId !== "string" ||
+ !accepted.jobId
+ ) {
+ throw new Error("Loom worker acceptance requires job reconciliation");
+ }
+ const jobId = accepted.jobId;
+ await recordDispatch("accepted", jobId).catch((error) => {
+ console.error("Could not record accepted Loom media dispatch", error);
+ });
return jobId;
}
-
const errorData = (await response.json().catch(() => ({}))) as {
error?: string;
code?: string;
@@ -348,9 +430,9 @@ async function startMediaServerProcessJob(
errorData.details ||
"Video processing failed to start";
const shouldRetry =
- response.status === 503 &&
- (errorData.code === "SERVER_BUSY" ||
- errorMessage.includes("Server is busy"));
+ response.status === 503 && errorData.code === "SERVER_BUSY";
+ if ((response.status >= 400 && response.status < 500) || shouldRetry)
+ await recordDispatch("rejected");
if (shouldRetry && attempt < MEDIA_SERVER_START_MAX_ATTEMPTS - 1) {
await waitForRetry(MEDIA_SERVER_START_RETRY_BASE_MS * 2 ** attempt);
@@ -366,6 +448,8 @@ async function startMediaServerProcessJob(
});
}
+ if (response.status >= 400 && response.status < 500)
+ throw new LoomMediaRequestRejectedError(errorMessage);
throw new Error(errorMessage);
}
@@ -374,11 +458,13 @@ async function startMediaServerProcessJob(
async function processVideoOnMediaServer(
payload: ImportLoomPayload,
- processingInput: LoomProcessingInput,
-): Promise<void> {
+ loomSourceUrl: string | undefined,
+ attemptId: string,
+ runId: string,
+): Promise<string> {
"use step";
- const { videoId, userId, rawFileKey, loomVideoId } = payload;
+ const { videoId, userId, rawFileKey } = payload;
const mediaServerUrl = serverEnv().MEDIA_SERVER_URL;
if (!mediaServerUrl) {
@@ -388,15 +474,16 @@ async function processVideoOnMediaServer(
const webhookBaseUrl =
serverEnv().MEDIA_SERVER_WEBHOOK_URL || serverEnv().WEB_URL;
- const loomSourceUrl = processingInput.importFromLoom
- ? await fetchFreshLoomDownloadUrl(loomVideoId)
- : undefined;
const {
rawVideoUrl,
sourcePresignedUrl,
outputPresignedUrl,
thumbnailPresignedUrl,
previewGifPresignedUrl,
+ sourceUpload,
+ outputUpload,
+ existingOutput,
+ abortUploads,
} = await Effect.gen(function* () {
const [video] = yield* Effect.promise(() =>
db()
@@ -407,6 +494,9 @@ async function processVideoOnMediaServer(
if (!video) {
return yield* Effect.fail(new FatalError("Video does not exist"));
}
+ if (video.ownerId !== userId) {
+ return yield* Effect.fail(new FatalError("Loom import owner changed"));
+ }
const videoDomain = Video.Video.decodeSync({
...video,
bucketId: video.bucket,
@@ -456,45 +546,105 @@ async function processVideoOnMediaServer(
},
{ expiresIn: MEDIA_SERVER_PRESIGNED_PUT_EXPIRES_SECONDS },
);
+ const multipart = yield* Effect.promise(() =>
+ prepareLoomMultipartUploads({
+ video,
+ bucket,
+ attemptId,
+ sourceKey:
+ loomSourceUrl && !isStreamingUrl(loomSourceUrl)
+ ? rawFileKey
+ : undefined,
+ webhookBaseUrl,
+ webhookSecret: serverEnv().MEDIA_SERVER_WEBHOOK_SECRET || undefined,
+ }),
+ );
return {
- rawVideoUrl,
- sourcePresignedUrl,
+ rawVideoUrl: multipart.existingOutput?.url ?? rawVideoUrl,
+ sourcePresignedUrl:
+ multipart.sourceUpload || multipart.existingOutput
+ ? undefined
+ : sourcePresignedUrl,
outputPresignedUrl,
thumbnailPresignedUrl,
previewGifPresignedUrl,
+ ...multipart,
+ abortUploads: async () => {
+ await Promise.all(
+ [multipart.sourceUpload, multipart.outputUpload]
+ .filter(
+ (upload): upload is LoomMultipartUploadTarget =>
+ upload !== undefined,
+ )
+ .map((upload) =>
+ bucket.multipart.abort(upload.key, upload.uploadId).pipe(
+ Effect.catchAll(() => Effect.void),
+ runWorkflowPromise,
+ ),
+ ),
+ );
+ },
};
}).pipe(runWorkflowPromise);
const webhookUrl = `${webhookBaseUrl}/api/webhooks/media-server/progress?retryable=true`;
const webhookSecret = serverEnv().MEDIA_SERVER_WEBHOOK_SECRET;
- await db()
- .update(videoUploads)
- .set({
- phase: "processing",
- processingProgress: 0,
- processingMessage: "Starting video processing...",
- processingError: null,
- updatedAt: new Date(),
- })
- .where(eq(videoUploads.videoId, videoId as Video.VideoId));
+ try {
+ await db()
+ .update(videoUploads)
+ .set({
+ phase: "processing",
+ processingProgress: 0,
+ processingMessage: "Starting video processing...",
+ processingError: null,
+ updatedAt: new Date(),
+ })
+ .where(eq(videoUploads.videoId, videoId as Video.VideoId));
+ } catch (error) {
+ await abortUploads();
+ throw error;
+ }
- await startMediaServerProcessJob(mediaServerUrl, {
- videoId,
- userId,
- videoUrl: rawVideoUrl,
- sourcePresignedUrl,
- outputPresignedUrl,
- thumbnailPresignedUrl,
- previewGifPresignedUrl,
- webhookUrl,
- webhookSecret: webhookSecret || undefined,
- inputExtension: getInputExtension(rawVideoUrl),
- priority: "bulk",
- });
+ try {
+ return await startMediaServerProcessJob(
+ mediaServerUrl,
+ {
+ videoId,
+ userId,
+ videoUrl: rawVideoUrl,
+ sourcePresignedUrl,
+ sourceUpload,
+ outputUpload,
+ existingOutput,
+ maxImportBytes: LOOM_IMPORT_MAX_BYTES,
+ outputPresignedUrl,
+ thumbnailPresignedUrl,
+ previewGifPresignedUrl,
+ webhookUrl,
+ webhookSecret: webhookSecret || undefined,
+ inputExtension: getInputExtension(rawVideoUrl),
+ priority: "bulk",
+ },
+ (state, jobId) =>
+ recordLoomMediaDispatch({
+ videoId,
+ userId,
+ runId,
+ attemptId,
+ state,
+ jobId,
+ }),
+ );
+ } catch (error) {
+ if (error instanceof LoomMediaRequestRejectedError) await abortUploads();
+ throw error;
+ }
}
+processVideoOnMediaServer.maxRetries = 0;
+
async function saveMetadataAndComplete(
videoId: string,
metadata: { duration: number; width: number; height: number; fps: number },
diff --git a/apps/web/workflows/live-transcribe.ts b/apps/web/workflows/live-transcribe.ts
index 04cab1044..591dc8948 100644
--- a/apps/web/workflows/live-transcribe.ts
+++ b/apps/web/workflows/live-transcribe.ts
@@ -8,13 +8,13 @@ import {
type User,
Video,
} from "@cap/web-domain";
-import { AssemblyAI } from "assemblyai";
import { and, eq, isNull, sql } from "drizzle-orm";
import { Either, Option, Schema } from "effect";
import { isAiGenerationEnabledForUser } from "@/lib/ai-generation-entitlement";
import {
ASSEMBLYAI_SPEECH_MODELS,
ASSEMBLYAI_SUPPORTED_LANGUAGES,
+ createAssemblyAIClient,
getAssemblyAITranscriptionOptions,
} from "@/lib/assemblyai";
import {
@@ -433,9 +433,7 @@ async function processNextLiveChunk(options: {
// ffmpeg — the binary doesn't exist in the serverless runtime.
const audioBuffer = await downloadConcatenatedSegmentsToBuffer(segmentUrls);
- const client = new AssemblyAI({
- apiKey: serverEnv().ASSEMBLY_API_KEY as string,
- });
+ const client = createAssemblyAIClient();
const language = (
ASSEMBLYAI_SUPPORTED_LANGUAGES as readonly string[]
).includes(options.language)
diff --git a/apps/web/workflows/transcribe.ts b/apps/web/workflows/transcribe.ts
index a281d25f1..7be4f9171 100644
--- a/apps/web/workflows/transcribe.ts
+++ b/apps/web/workflows/transcribe.ts
@@ -17,12 +17,14 @@ import {
parseAiGenerationLanguage,
Video,
} from "@cap/web-domain";
-import { AssemblyAI } from "assemblyai";
import { and, eq, isNull, sql } from "drizzle-orm";
import { Either, Option, Schema } from "effect";
import { FatalError } from "workflow";
import { start } from "workflow/api";
-import { getAssemblyAITranscriptionOptions } from "@/lib/assemblyai";
+import {
+ createAssemblyAIClient,
+ getAssemblyAITranscriptionOptions,
+} from "@/lib/assemblyai";
import {
ENHANCED_AUDIO_CONTENT_TYPE,
ENHANCED_AUDIO_EXTENSION,
@@ -782,9 +784,7 @@ async function transcribeWithAssemblyAI(
}
const audioBuffer = Buffer.from(await audioResponse.arrayBuffer());
- const client = new AssemblyAI({
- apiKey: serverEnv().ASSEMBLY_API_KEY as string,
- });
+ const client = createAssemblyAIClient();
const transcript = await client.transcripts.transcribe({
audio: audioBuffer,
@@ -835,9 +835,7 @@ async function transcribeEditTranscriptWithAssemblyAI(
}
const audioBuffer = Buffer.from(await audioResponse.arrayBuffer());
- const client = new AssemblyAI({
- apiKey: serverEnv().ASSEMBLY_API_KEY as string,
- });
+ const client = createAssemblyAIClient();
const transcript = await client.transcripts.transcribe({
audio: audioBuffer,
...getAssemblyAITranscriptionOptions("auto"),
diff --git a/bun.lock b/bun.lock
index c16651409..7d6d401f8 100644
--- a/bun.lock
+++ b/bun.lock
@@ -367,6 +367,7 @@
"react-rnd": "^10.4.1",
"react-scroll-parallax": "^3.4.5",
"react-tooltip": "^5.26.3",
+ "react-tweet": "^3.3.1",
"recharts": "^3.3.0",
"rehype-pretty-code": "^0.14.1",
"remark-gfm": "^4.0.1",
@@ -4999,6 +5000,8 @@
"react-tooltip": ["[email protected]", "", { "dependencies": { "@floating-ui/dom": "1.7.6", "classnames": "2.5.1" }, "peerDependencies": { "react": ">=16.14.0", "react-dom": ">=16.14.0" } }, "sha512-1lSPLQXuVooePxadUpmcwLgOsF1mIty7UZTJ9XnyfX4drOzStYs4JMXnazcDLguQr41W5OUZddOp9kfvArdpEQ=="],
+ "react-tweet": ["[email protected]", "", { "dependencies": { "@swc/helpers": "^0.5.3", "clsx": "^2.0.0", "swr": "^2.2.4" }, "peerDependencies": { "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0" } }, "sha512-0Gj1YgBTe1K85NBMoWBlUc17Rdc67gIJLlacrVgj+3jWIoXpFfxPdZ1U5OnWkkF9zxhphqs2pY1i//JBfP3Qog=="],
+
"read-cache": ["[email protected]", "", { "dependencies": { "pify": "2.3.0" } }, "sha512-Owdv/Ft7IjOgm/i0xvNDZ1LrRANRfew4b2prF3OWMQLxLfu3bS8FVhCsrSCMK4lR56Y9ya+AThoTpDCTxCmpRA=="],
"read-cmd-shim": ["[email protected]", "", {}, "sha512-1zM5HuOfagXCBWMN83fuFI/x+T/UhZ7k+KIzhrHXcQoeX5+7gmaDYjELQHmmzIodumBHeByBJT4QYS7ufAgs7A=="],
@@ -5419,6 +5422,8 @@
"supports-preserve-symlinks-flag": ["[email protected]", "", {}, "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w=="],
+ "swr": ["[email protected]", "", { "dependencies": { "dequal": "^2.0.3", "use-sync-external-store": "^1.6.0" }, "peerDependencies": { "react": "^16.11.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" } }, "sha512-BRw55e8r0B7SpDN20CAzoQAHl7y1yP7/Zt7oqUjMv0vSt2u2Xnkm88Ws+VypbV9BXHQVuSuyVq7zMjO16wSExw=="],
+
"symbol-tree": ["[email protected]", "", {}, "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw=="],
"synckit": ["[email protected]", "", { "dependencies": { "@pkgr/core": "0.3.6" } }, "sha512-eNRKgb3z66Yp3D2CixVujOUvXLFUTij/zVnV8KRyvFdQwpz7I5DS8UfRkTeLzb64u+dkzDSdelE24izu+zSSUg=="],
@@ -6997,6 +7002,8 @@
"react-rnd/tslib": ["[email protected]", "", {}, "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q=="],
+ "react-tweet/@swc/helpers": ["@swc/[email protected]", "", { "dependencies": { "tslib": "2.8.1" } }, "sha512-5lSsMOTXURePglDfvuAQUqkGek9Hg2kksOYay2m0+XR++b2NWYL/4sWyuvVBIs8oKnJaxkdi9whaL/sqN13afw=="],
+
"readdir-glob/minimatch": ["[email protected]", "", { "dependencies": { "brace-expansion": "2.1.1" } }, "sha512-7o1wEA2RyMP7Iu7GNba9vc0RWWGACJOCZBJX2GJWip0ikV+wcOsgVuY9uE8CPiyQhkGFSlhuSkZPavN7u1c2Fw=="],
"recast/source-map": ["[email protected]", "", {}, "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="],
diff --git a/crates/audio/src/progressive.rs b/crates/audio/src/progressive.rs
index ba9299f37..702524e85 100644
--- a/crates/audio/src/progressive.rs
+++ b/crates/audio/src/progressive.rs
@@ -17,6 +17,8 @@ use std::{
use tokio::sync::watch;
const BLOCK_FRAMES: usize = 32_768;
+// Eight lanes never run on a decoded block: 8 * 4_800 > BLOCK_FRAMES.
+const WAVEFORM_PEAK_LANES: usize = 4;
pub fn waveform_peaks<'a>(slices: impl IntoIterator<Item = &'a [f32]>, channels: u16) -> Vec<f32> {
let block_samples = AudioData::SAMPLE_RATE as usize / 10 * usize::from(channels.max(1));
@@ -25,14 +27,20 @@ pub fn waveform_peaks<'a>(slices: impl IntoIterator<Item = &'a [f32]>, channels:
let mut count = 0;
for mut slice in slices {
while !slice.is_empty() {
- let (window, rest) = slice.split_at((block_samples - count).min(slice.len()));
- sum = window.iter().fold(sum, |sum, sample| sum + sample.abs());
- count += window.len();
- slice = rest;
- if count == block_samples {
- peaks.push(sum / count as f32);
- sum = 0.0;
- count = 0;
+ if count == 0 && slice.len() >= block_samples * WAVEFORM_PEAK_LANES {
+ let (group, rest) = slice.split_at(block_samples * WAVEFORM_PEAK_LANES);
+ push_waveform_peak_lanes(&mut peaks, group, block_samples);
+ slice = rest;
+ } else {
+ let (window, rest) = slice.split_at((block_samples - count).min(slice.len()));
+ sum = window.iter().fold(sum, |sum, sample| sum + sample.abs());
+ count += window.len();
+ slice = rest;
+ if count == block_samples {
+ peaks.push(sum / count as f32);
+ sum = 0.0;
+ count = 0;
+ }
}
}
}
@@ -49,6 +57,27 @@ pub fn waveform_peaks<'a>(slices: impl IntoIterator<Item = &'a [f32]>, channels:
peaks
}
+fn push_waveform_peak_lanes(peaks: &mut Vec<f32>, group: &[f32], block_samples: usize) {
+ let (first, rest) = group.split_at(block_samples);
+ let (second, rest) = rest.split_at(block_samples);
+ let (third, fourth) = rest.split_at(block_samples);
+ let mut first_sum = 0.0_f32;
+ let mut second_sum = 0.0_f32;
+ let mut third_sum = 0.0_f32;
+ let mut fourth_sum = 0.0_f32;
+ for index in 0..block_samples {
+ first_sum += first[index].abs();
+ second_sum += second[index].abs();
+ third_sum += third[index].abs();
+ fourth_sum += fourth[index].abs();
+ }
+ let scale = block_samples as f32;
+ peaks.push(first_sum / scale);
+ peaks.push(second_sum / scale);
+ peaks.push(third_sum / scale);
+ peaks.push(fourth_sum / scale);
+}
+
pub struct DecodedAudio {
storage: AudioStorage,
channels: u16,
@@ -1202,4 +1231,176 @@ mod tests {
.is_err()
);
}
+
+ fn xorshift_samples(len: usize) -> Vec<f32> {
+ let mut state = 0x1234_5678_u32;
+ (0..len)
+ .map(|_| {
+ state ^= state << 13;
+ state ^= state >> 17;
+ state ^= state << 5;
+ state as f32 / u32::MAX as f32 * 2.0 - 1.0
+ })
+ .collect()
+ }
+
+ fn sequential_waveform_peaks(samples: &[f32], channels: u16) -> Vec<f32> {
+ let window_samples = 4_800 * usize::from(channels.max(1));
+ samples
+ .chunks(window_samples)
+ .map(|window| {
+ let average = window
+ .iter()
+ .fold(0.0_f32, |sum, sample| sum + sample.abs())
+ / window.len() as f32;
+ if average > 0.0 {
+ 20.0 * average.log10()
+ } else {
+ -60.0
+ }
+ })
+ .collect()
+ }
+
+ fn assert_peak_bits(actual: &[f32], expected: &[f32], channels: u16, block_len: usize) {
+ assert_eq!(actual.len(), expected.len(), "{channels} {block_len}");
+ for (index, (actual, expected)) in actual.iter().zip(expected).enumerate() {
+ assert_eq!(
+ actual.to_bits(),
+ expected.to_bits(),
+ "{channels} {block_len} at {index}"
+ );
+ }
+ }
+
+ #[test]
+ fn waveform_peaks_match_a_sequential_sum_across_uneven_blocks() {
+ let samples = xorshift_samples(48_000 * 3 + 1_234);
+ for channels in [1, 2] {
+ let expected = sequential_waveform_peaks(&samples, channels);
+ let window = 4_800 * usize::from(channels);
+ for block_len in [
+ 1,
+ 7,
+ 4_799,
+ window - 1,
+ window,
+ window * WAVEFORM_PEAK_LANES - 1,
+ window * WAVEFORM_PEAK_LANES,
+ window * WAVEFORM_PEAK_LANES + 1,
+ 32_768 * usize::from(channels),
+ ] {
+ let blocks: Vec<&[f32]> = samples.chunks(block_len).collect();
+ assert_peak_bits(
+ &waveform_peaks(blocks.iter().copied(), channels),
+ &expected,
+ channels,
+ block_len,
+ );
+ }
+ let mut with_empty: Vec<&[f32]> = Vec::new();
+ for chunk in samples.chunks(7) {
+ with_empty.push(chunk);
+ with_empty.push(&[]);
+ }
+ assert_peak_bits(
+ &waveform_peaks(with_empty, channels),
+ &expected,
+ channels,
+ 7,
+ );
+ assert_peak_bits(
+ &waveform_peaks([&samples[..]], channels),
+ &expected,
+ channels,
+ samples.len(),
+ );
+ }
+
+ let contiguous = xorshift_samples(48_000 * 120 + 17);
+ for channels in [1, 2] {
+ let expected = sequential_waveform_peaks(&contiguous, channels);
+ assert_peak_bits(
+ &waveform_peaks([&contiguous[..]], channels),
+ &expected,
+ channels,
+ contiguous.len(),
+ );
+ let block_len = 32_768 * usize::from(channels);
+ let blocks: Vec<&[f32]> = contiguous.chunks(block_len).collect();
+ assert_peak_bits(
+ &waveform_peaks(blocks, channels),
+ &expected,
+ channels,
+ block_len,
+ );
+ }
+ }
+
+ fn serial_waveform_peaks<'a>(
+ slices: impl IntoIterator<Item = &'a [f32]>,
+ channels: u16,
+ ) -> Vec<f32> {
+ let block_samples = AudioData::SAMPLE_RATE as usize / 10 * usize::from(channels.max(1));
+ let mut peaks = Vec::new();
+ let mut sum = 0.0_f32;
+ let mut count = 0;
+ for mut slice in slices {
+ while !slice.is_empty() {
+ let (window, rest) = slice.split_at((block_samples - count).min(slice.len()));
+ sum = window.iter().fold(sum, |sum, sample| sum + sample.abs());
+ count += window.len();
+ slice = rest;
+ if count == block_samples {
+ peaks.push(sum / count as f32);
+ sum = 0.0;
+ count = 0;
+ }
+ }
+ }
+ if count > 0 {
+ peaks.push(sum / count as f32);
+ }
+ for peak in &mut peaks {
+ *peak = if *peak > 0.0 {
+ 20.0 * peak.log10()
+ } else {
+ -60.0
+ };
+ }
+ peaks
+ }
+
+ #[test]
+ #[ignore]
+ fn benchmark_waveform_peaks() {
+ use std::{hint::black_box, time::Instant};
+
+ for (track, channels) in [("mic", 1_u16), ("system audio", 2)] {
+ let samples = xorshift_samples(48_000 * 3_600 * usize::from(channels));
+ let blocks: Vec<&[f32]> = samples.chunks(32_768 * usize::from(channels)).collect();
+ let serial = serial_waveform_peaks(blocks.iter().copied(), channels);
+ let lanes = waveform_peaks(blocks.iter().copied(), channels);
+ assert_peak_bits(&lanes, &serial, channels, 32_768 * usize::from(channels));
+ let mut serial_runs = Vec::new();
+ let mut lane_runs = Vec::new();
+ for _ in 0..5 {
+ let start = Instant::now();
+ black_box(serial_waveform_peaks(blocks.iter().copied(), channels));
+ serial_runs.push(start.elapsed().as_secs_f64() * 1000.0);
+ let start = Instant::now();
+ black_box(waveform_peaks(blocks.iter().copied(), channels));
+ lane_runs.push(start.elapsed().as_secs_f64() * 1000.0);
+ }
+ serial_runs.sort_by(f64::total_cmp);
+ lane_runs.sort_by(f64::total_cmp);
+ println!(
+ "{{\"track\":\"{track}\",\"channels\":{channels},\"duration_seconds\":3600,\"block_frames\":32768,\"serial_median_ms\":{:.1},\"serial_min_ms\":{:.1},\"lanes_median_ms\":{:.1},\"lanes_min_ms\":{:.1}}}",
+ serial_runs[serial_runs.len() / 2],
+ serial_runs[0],
+ lane_runs[lane_runs.len() / 2],
+ lane_runs[0]
+ );
+ }
+ }
}
diff --git a/crates/camera-effects/src/lib.rs b/crates/camera-effects/src/lib.rs
index e6c0d9c45..2251507c9 100644
--- a/crates/camera-effects/src/lib.rs
+++ b/crates/camera-effects/src/lib.rs
@@ -404,6 +404,7 @@ pub struct BlurProcessor {
last_output_status: Option<BlurOutputStatus>,
frame_synchronous: bool,
frame_time: Option<f32>,
+ output_format: wgpu::TextureFormat,
// Keep last: must drop after every other field (see BlurSessionHandle).
_blur_session: BlurSessionHandle,
}
@@ -560,6 +561,7 @@ impl BlurProcessor {
last_output_status: None,
frame_synchronous: false,
frame_time: None,
+ output_format,
_blur_session: blur_session,
})
}
@@ -824,13 +826,8 @@ impl BlurProcessor {
wgpu::TextureFormat::Rgba16Float,
tex_usage,
);
- let output_texture = create_rgba_texture(
- "Blur Output",
- width,
- height,
- wgpu::TextureFormat::Rgba8Unorm,
- tex_usage,
- );
+ let output_texture =
+ create_rgba_texture("Blur Output", width, height, self.output_format, tex_usage);
let mask_texture = create_mask_texture(device, self.mask_dimensions);
self.textures = Some(ProcessorTextures {
diff --git a/crates/editor/examples/editor-startup-benchmark.rs b/crates/editor/examples/editor-startup-benchmark.rs
index e4a8b44d4..8af88febe 100644
--- a/crates/editor/examples/editor-startup-benchmark.rs
+++ b/crates/editor/examples/editor-startup-benchmark.rs
@@ -32,6 +32,18 @@ fn percentile(data: &[f64], p: f64) -> f64 {
sorted[idx.min(sorted.len() - 1)]
}
+fn clone_shared_device(
+ shared: &cap_rendering::SharedWgpuDevice,
+) -> cap_rendering::SharedWgpuDevice {
+ cap_rendering::SharedWgpuDevice {
+ instance: shared.instance.clone(),
+ adapter: shared.adapter.clone(),
+ device: shared.device.clone(),
+ queue: shared.queue.clone(),
+ is_software_adapter: shared.is_software_adapter,
+ }
+}
+
fn has_flag(args: &[String], flag: &str) -> bool {
args.iter().any(|arg| arg == flag)
}
@@ -43,6 +55,7 @@ fn push_stage(stages: &mut Vec<(&'static str, f64)>, name: &'static str, start:
async fn profile_startup_stages(
recording_path: PathBuf,
+ shared_device: Option<cap_rendering::SharedWgpuDevice>,
) -> Result<Vec<(&'static str, f64)>, String> {
let mut stages = Vec::new();
let total_start = Instant::now();
@@ -64,15 +77,23 @@ async fn profile_startup_stages(
)?);
push_stage(&mut stages, "recordings metadata", &mut stage_start);
- let render_constants = Arc::new(
+ let render_constants = Arc::new(if let Some(shared) = shared_device {
+ RenderVideoConstants::new_with_device(
+ shared,
+ &recordings.segments,
+ recording_meta.clone(),
+ (**meta).clone(),
+ )
+ .map_err(|e| format!("Failed to create render constants: {e}"))?
+ } else {
RenderVideoConstants::new(
&recordings.segments,
recording_meta.clone(),
(**meta).clone(),
)
.await
- .map_err(|e| format!("Failed to create render constants: {e}"))?,
- );
+ .map_err(|e| format!("Failed to create render constants: {e}"))?
+ });
push_stage(&mut stages, "render constants", &mut stage_start);
let layers_rx = start_renderer_layers_creation(&render_constants, &project);
@@ -100,13 +121,20 @@ async fn profile_startup_stages(
Ok(stages)
}
-async fn run_stage_profile(recording_path: PathBuf, runs: usize) {
+async fn run_stage_profile(
+ recording_path: PathBuf,
+ runs: usize,
+ shared_device: Option<&cap_rendering::SharedWgpuDevice>,
+) {
let mut stage_values: Vec<(&'static str, Vec<f64>)> = Vec::new();
for _ in 0..runs {
- let stages = profile_startup_stages(recording_path.clone())
- .await
- .expect("Failed to profile startup stages");
+ let stages = profile_startup_stages(
+ recording_path.clone(),
+ shared_device.map(clone_shared_device),
+ )
+ .await
+ .expect("Failed to profile startup stages");
for (stage_idx, (name, value)) in stages.into_iter().enumerate() {
if stage_values.len() <= stage_idx {
@@ -167,6 +195,8 @@ async fn main() {
let profile_stages = has_flag(&args, "--profile-stages");
let profile_stages_only = has_flag(&args, "--profile-stages-only");
let prewarm_fonts = has_flag(&args, "--prewarm-fonts");
+ let shared_device_mode = has_flag(&args, "--shared-device");
+ let prewarm_renderer = has_flag(&args, "--prewarm-renderer");
let resolution_base = cap_project::XY::new(1248, 702);
println!("{}", "=".repeat(64));
@@ -187,8 +217,44 @@ async fn main() {
started.elapsed().as_secs_f64() * 1000.0
);
}
+ let mut shared_device = None;
+ if prewarm_renderer {
+ let started = Instant::now();
+ tokio::task::spawn_blocking(cap_rendering::prewarm_fonts)
+ .await
+ .expect("Font preparation failed");
+ let shared = cap_rendering::create_shared_device()
+ .await
+ .expect("Failed to create shared device");
+ let (device, queue, software) = (
+ shared.device.clone(),
+ shared.queue.clone(),
+ shared.is_software_adapter,
+ );
+ let mut layers = tokio::task::spawn_blocking(move || {
+ cap_rendering::RendererLayers::new_with_options(&device, &queue, software)
+ })
+ .await
+ .expect("Renderer warmup failed");
+ cap_rendering::prewarm_first_frame(clone_shared_device(&shared), &mut layers)
+ .await
+ .expect("Renderer warmup frame failed");
+ drop(layers);
+ shared_device = Some(shared);
+ println!(
+ "Renderer warmup before startup timing: {:.3}ms",
+ started.elapsed().as_secs_f64() * 1000.0
+ );
+ }
if profile_stages_only {
- run_stage_profile(recording_path, runs).await;
+ if shared_device_mode && shared_device.is_none() {
+ shared_device = Some(
+ cap_rendering::create_shared_device()
+ .await
+ .expect("Failed to create shared device"),
+ );
+ }
+ run_stage_profile(recording_path, runs, shared_device.as_ref()).await;
return;
}
@@ -203,13 +269,20 @@ async fn main() {
let (frame_tx, frame_rx) = mpsc::channel::<()>();
let start = Instant::now();
+ if (shared_device_mode || prewarm_renderer) && shared_device.is_none() {
+ shared_device = Some(
+ cap_rendering::create_shared_device()
+ .await
+ .expect("Failed to create shared device"),
+ );
+ }
let editor = EditorInstance::new(
recording_path.clone(),
|_| {},
Box::new(move |_: EditorFrameOutput, _: FrameLayout| {
let _ = frame_tx.send(());
}),
- None,
+ shared_device.as_ref().map(clone_shared_device),
)
.await
.expect("Failed to create editor instance");
@@ -267,6 +340,6 @@ async fn main() {
);
if profile_stages {
- run_stage_profile(recording_path, runs).await;
+ run_stage_profile(recording_path, runs, shared_device.as_ref()).await;
}
}
diff --git a/crates/editor/src/audio.rs b/crates/editor/src/audio.rs
index f9abf9602..278a40fa6 100644
--- a/crates/editor/src/audio.rs
+++ b/crates/editor/src/audio.rs
@@ -20,8 +20,8 @@ use ringbuf::{
use std::{
collections::{HashMap, VecDeque},
sync::{
- Arc,
- atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering},
+ Arc, Condvar, Mutex, MutexGuard, PoisonError, Weak,
+ atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize, Ordering},
},
};
use tracing::{info, warn};
@@ -125,6 +125,15 @@ impl AudioSegmentTrack {
pub fn offset(&self, offsets: &ClipOffsets) -> f32 {
(self.get_offset)(offsets) + self.timing_offset_secs
}
+
+ fn same_source(&self, other: &Self) -> bool {
+ Arc::ptr_eq(&self.data, &other.data)
+ && std::ptr::fn_addr_eq(self.get_gain, other.get_gain)
+ && std::ptr::fn_addr_eq(self.get_stereo_mode, other.get_stereo_mode)
+ && std::ptr::fn_addr_eq(self.get_offset, other.get_offset)
+ && self.timing_offset_secs.to_bits() == other.timing_offset_secs.to_bits()
+ && self.is_microphone == other.is_microphone
+ }
}
struct TimelineCursor<'a> {
@@ -1467,6 +1476,10 @@ fn silent_audio_frame(samples: usize) -> FFAudio {
frame
}
+fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
+ mutex.lock().unwrap_or_else(PoisonError::into_inner)
+}
+
/// Shared state between the background render thread and the audio callback.
///
/// The timeline mix is rendered progressively into `samples` (f32 bit
@@ -1487,9 +1500,55 @@ struct ProgressiveTimeline {
/// Tells the producer to bail out early (playback stopped).
stop: AtomicBool,
complete: AtomicBool,
+ /// Pass 1 has written everything it ever will.
+ pass1_done: AtomicBool,
+ /// A playback cache keeps this render once its readers are gone.
+ retained: AtomicBool,
+ retain_slot: Mutex<RetainSlot>,
+ readers: AtomicUsize,
+ wake: Mutex<u64>,
+ woken: Condvar,
+ created: std::time::Instant,
+ paused_at_nanos: AtomicU64,
+ retired: Mutex<Retired>,
+}
+
+/// A replaced cached render, released only once this render stops working
+/// (finished, stopped or parked), so freeing its buffer never competes with
+/// the start of playback.
+#[derive(Default)]
+struct Retired {
+ idle: bool,
+ render: Option<CachedProgressiveRender>,
+}
+
+#[derive(Default)]
+struct RetainSlot {
+ release: Option<CacheRelease>,
+ exited: bool,
}
impl ProgressiveTimeline {
+ fn new(len: usize, primary_start: usize) -> Self {
+ Self {
+ samples: Self::allocate_samples(len),
+ primary_start,
+ primary_end: AtomicUsize::new(primary_start),
+ wrap_end: AtomicUsize::new(0),
+ stop: AtomicBool::new(false),
+ complete: AtomicBool::new(false),
+ pass1_done: AtomicBool::new(false),
+ retained: AtomicBool::new(false),
+ retain_slot: Mutex::default(),
+ readers: AtomicUsize::new(1),
+ wake: Mutex::new(0),
+ woken: Condvar::new(),
+ created: std::time::Instant::now(),
+ paused_at_nanos: AtomicU64::new(0),
+ retired: Mutex::default(),
+ }
+ }
+
/// `AtomicU32` has the same size, alignment and bit validity as `u32`
/// (documented guarantee), so a zeroed `u32` allocation — which the
/// allocator can hand out as untouched zero pages, unlike constructing
@@ -1498,6 +1557,107 @@ impl ProgressiveTimeline {
let raw = vec![0u32; len].into_boxed_slice();
unsafe { Box::from_raw(Box::into_raw(raw) as *mut [AtomicU32]) }
}
+
+ /// Whether every sample in `[from, to)` is already rendered (or will
+ /// never be written, so it reads as silence either way).
+ fn covers(&self, from: usize, to: usize) -> bool {
+ let to = to.min(self.samples.len());
+ if from >= to || self.complete.load(Ordering::Acquire) {
+ return true;
+ }
+ let primary = to <= self.primary_start
+ || self.pass1_done.load(Ordering::Acquire)
+ || self.primary_end.load(Ordering::Acquire) >= to;
+ let wrap = from >= self.primary_start
+ || self.wrap_end.load(Ordering::Acquire) >= to.min(self.primary_start);
+ primary && wrap
+ }
+
+ /// Hands the render to a playback cache. Fails once the render thread
+ /// has exited, since nothing would release it then.
+ fn retain(&self, release: CacheRelease) -> bool {
+ let mut slot = lock(&self.retain_slot);
+ if slot.exited {
+ return false;
+ }
+ slot.release = Some(release);
+ self.retained.store(true, Ordering::Release);
+ true
+ }
+
+ fn retire(&self, previous: Option<CachedProgressiveRender>) {
+ let mut retired = lock(&self.retired);
+ if retired.idle {
+ drop(retired);
+ drop(previous);
+ } else {
+ retired.render = previous;
+ }
+ }
+
+ /// Releases the retired render. `idle` marks a render that will not
+ /// resume, so later retirements are released at once.
+ fn release_retired(&self, idle: bool) {
+ let previous = {
+ let mut retired = lock(&self.retired);
+ retired.idle |= idle;
+ retired.render.take()
+ };
+ drop(previous);
+ }
+
+ fn attach_reader(&self) {
+ self.readers.fetch_add(1, Ordering::AcqRel);
+ self.notify();
+ }
+
+ fn notify(&self) {
+ *lock(&self.wake) += 1;
+ self.woken.notify_all();
+ }
+
+ fn wake_generation(&self) -> u64 {
+ *lock(&self.wake)
+ }
+
+ /// Parks the render thread until a `notify` after `seen` was read, or
+ /// the timeout. Callers read `seen` before checking the conditions they
+ /// wait on, so a notify racing those checks is never missed.
+ fn wait(&self, seen: u64, timeout: std::time::Duration) {
+ let deadline = std::time::Instant::now() + timeout;
+ let mut wake = lock(&self.wake);
+ while *wake == seen {
+ let now = std::time::Instant::now();
+ if now >= deadline {
+ return;
+ }
+ wake = self
+ .woken
+ .wait_timeout(wake, deadline - now)
+ .unwrap_or_else(PoisonError::into_inner)
+ .0;
+ }
+ }
+
+ fn detach_reader(&self) {
+ if self.readers.fetch_sub(1, Ordering::AcqRel) == 1 {
+ self.paused_at_nanos
+ .store(self.created.elapsed().as_nanos() as u64, Ordering::Release);
+ }
+ }
+
+ fn paused_for(&self) -> std::time::Duration {
+ self.created
+ .elapsed()
+ .saturating_sub(std::time::Duration::from_nanos(
+ self.paused_at_nanos.load(Ordering::Acquire),
+ ))
+ }
+
+ fn shut_down(&self) {
+ self.stop.store(true, Ordering::Release);
+ self.notify();
+ }
}
const MAX_PROGRESSIVE_BUFFER_BYTES: usize = 512 * 1024 * 1024;
@@ -1588,6 +1748,326 @@ pub(crate) fn preparing_audio_output_policy(
})
}
+const PLAYBACK_AUDIO_IDLE_RELEASE: std::time::Duration = std::time::Duration::from_secs(120);
+
+type CacheEntry = Arc<Mutex<Option<CachedProgressiveRender>>>;
+
+static PLAYBACK_AUDIO_CACHES: Mutex<Vec<Weak<Mutex<Option<CachedProgressiveRender>>>>> =
+ Mutex::new(Vec::new());
+
+/// Drops every cached playback render in the process, so memory returns to
+/// what it is on main while paused. Called before exports and on memory
+/// pressure.
+pub fn release_playback_audio_caches() {
+ let caches = lock(&PLAYBACK_AUDIO_CACHES)
+ .iter()
+ .filter_map(Weak::upgrade)
+ .collect::<Vec<_>>();
+ for cache in caches {
+ *lock(&cache) = None;
+ }
+}
+
+#[derive(Clone)]
+pub struct PlaybackAudioCache {
+ entry: CacheEntry,
+ idle_release: std::time::Duration,
+}
+
+impl Default for PlaybackAudioCache {
+ fn default() -> Self {
+ Self::with_idle_release(PLAYBACK_AUDIO_IDLE_RELEASE)
+ }
+}
+
+impl PlaybackAudioCache {
+ fn with_idle_release(idle_release: std::time::Duration) -> Self {
+ #[cfg(target_os = "macos")]
+ memory_pressure::watch();
+ let entry = CacheEntry::default();
+ let mut caches = lock(&PLAYBACK_AUDIO_CACHES);
+ caches.retain(|cache| cache.strong_count() > 0);
+ caches.push(Arc::downgrade(&entry));
+ Self {
+ entry,
+ idle_release,
+ }
+ }
+
+ pub fn clear(&self) {
+ *lock(&self.entry) = None;
+ }
+}
+
+#[cfg(target_os = "macos")]
+mod memory_pressure {
+ use std::ffi::c_void;
+
+ #[repr(C)]
+ struct DispatchSourceType {
+ _private: [u8; 0],
+ }
+
+ const DISPATCH_MEMORYPRESSURE_WARN: usize = 0x02;
+ const DISPATCH_MEMORYPRESSURE_CRITICAL: usize = 0x04;
+ const QOS_CLASS_UTILITY: isize = 0x11;
+
+ unsafe extern "C" {
+ static _dispatch_source_type_memorypressure: DispatchSourceType;
+ fn dispatch_get_global_queue(identifier: isize, flags: usize) -> *mut c_void;
+ fn dispatch_source_create(
+ kind: *const DispatchSourceType,
+ handle: usize,
+ mask: usize,
+ queue: *mut c_void,
+ ) -> *mut c_void;
+ fn dispatch_source_set_event_handler_f(
+ source: *mut c_void,
+ handler: extern "C" fn(*mut c_void),
+ );
+ fn dispatch_resume(object: *mut c_void);
+ }
+
+ pub(super) extern "C" fn on_memory_pressure(_context: *mut c_void) {
+ super::release_playback_audio_caches();
+ }
+
+ /// Installs one process-wide memory pressure source. It lives for the
+ /// rest of the process, so it is never released.
+ ///
+ /// Not installed in unit tests: a real pressure event on the host would
+ /// release the caches of every test running in the process at once.
+ /// Tests drive `on_memory_pressure` directly instead.
+ pub(super) fn watch() {
+ if cfg!(test) {
+ return;
+ }
+ static WATCH: std::sync::Once = std::sync::Once::new();
+ WATCH.call_once(|| unsafe {
+ let source = dispatch_source_create(
+ &_dispatch_source_type_memorypressure,
+ 0,
+ DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL,
+ dispatch_get_global_queue(QOS_CLASS_UTILITY, 0),
+ );
+ if source.is_null() {
+ return;
+ }
+ dispatch_source_set_event_handler_f(source, on_memory_pressure);
+ dispatch_resume(source);
+ });
+ }
+}
+
+struct CachePlan {
+ key: ProgressiveRenderKey,
+ shift_from: usize,
+ shift: usize,
+ ready_at: usize,
+ rate: u32,
+ channels: usize,
+}
+
+struct CachedProgressiveRender {
+ key: ProgressiveRenderKey,
+ render: Arc<ProgressiveRender>,
+}
+
+struct ProgressiveRenderKey {
+ segments: Vec<AudioSegment>,
+ music: Vec<(String, Arc<AudioData>)>,
+ project: Vec<u8>,
+ duration_bits: u64,
+ sample_rate: u32,
+ channels: usize,
+}
+
+impl ProgressiveRenderKey {
+ fn new(
+ segments: Vec<AudioSegment>,
+ music: &MusicTracks,
+ project: &ProjectConfiguration,
+ duration_secs: f64,
+ output_info: AudioInfo,
+ ) -> Option<Self> {
+ let mut music = music
+ .iter()
+ .map(|(path, data)| (path.clone(), data.clone()))
+ .collect::<Vec<_>>();
+ music.sort_by(|a, b| a.0.cmp(&b.0));
+ Some(Self {
+ segments,
+ music,
+ project: audio_project_key(project)?,
+ duration_bits: duration_secs.to_bits(),
+ sample_rate: output_info.sample_rate,
+ channels: output_info.channels,
+ })
+ }
+}
+
+impl PartialEq for ProgressiveRenderKey {
+ fn eq(&self, other: &Self) -> bool {
+ self.project == other.project
+ && self.duration_bits == other.duration_bits
+ && self.sample_rate == other.sample_rate
+ && self.channels == other.channels
+ && self.music.len() == other.music.len()
+ && self
+ .music
+ .iter()
+ .zip(&other.music)
+ .all(|(a, b)| a.0 == b.0 && Arc::ptr_eq(&a.1, &b.1))
+ && self.segments.len() == other.segments.len()
+ && self.segments.iter().zip(&other.segments).all(|(a, b)| {
+ a.tracks.len() == b.tracks.len()
+ && a.tracks
+ .iter()
+ .zip(&b.tracks)
+ .all(|(a, b)| a.same_source(b))
+ })
+ }
+}
+
+/// Serializes everything the audio renderer reads from a project. Both
+/// structs are destructured without `..`, so a new field fails to compile
+/// here until someone decides whether it changes playback audio. Fields
+/// below `segments`, `clips` and `audio` are kept whole, so new nested
+/// fields change the key on their own.
+fn audio_project_key(project: &ProjectConfiguration) -> Option<Vec<u8>> {
+ let ProjectConfiguration {
+ aspect_ratio: _,
+ background: _,
+ camera: _,
+ audio,
+ cursor: _,
+ hotkeys: _,
+ timeline,
+ overlay_order: _,
+ captions: _,
+ keyboard: _,
+ clips,
+ annotations: _,
+ hidden_text_segments: _,
+ screen_motion_blur: _,
+ screen_movement_spring: _,
+ color_correction: _,
+ text_size_version: _,
+ text_anim_version: _,
+ } = project;
+ let timeline = timeline.as_ref().map(|timeline| {
+ let TimelineConfiguration {
+ segments,
+ transitions,
+ zoom_segments: _,
+ scene_segments: _,
+ mask_segments: _,
+ text_segments: _,
+ caption_segments: _,
+ keyboard_segments: _,
+ audio_segments,
+ style_segments: _,
+ image_segments: _,
+ camera3d_segments: _,
+ } = timeline;
+ let segments = segments
+ .iter()
+ .map(|segment| cap_project::TimelineSegment {
+ name: None,
+ hide_cursor: None,
+ ..segment.clone()
+ })
+ .collect::<Vec<_>>();
+ (
+ segments,
+ transitions,
+ audio_segments,
+ timeline.hold_windows(),
+ )
+ });
+ serde_json::to_vec(&(audio, clips, timeline)).ok()
+}
+
+/// Owner handle of a progressive render. Dropping the last one (readers and
+/// the cache) stops its render thread.
+struct ProgressiveRender {
+ timeline: Arc<ProgressiveTimeline>,
+}
+
+impl Drop for ProgressiveRender {
+ fn drop(&mut self) {
+ self.timeline.shut_down();
+ }
+}
+
+/// Whether every sample of a progressive render is a pure function of its
+/// timeline position, so a render started anywhere holds the same samples a
+/// render started at another playhead would. Studio Sound, retimed audio,
+/// transitions, text holds and resampling all carry state from wherever the
+/// render began, so any of them rules reuse out.
+fn render_is_origin_independent(
+ project: &ProjectConfiguration,
+ output_info: AudioInfo,
+ duration_secs: f64,
+) -> bool {
+ if output_info.sample_rate != AudioData::SAMPLE_RATE
+ || output_info.channels != usize::from(AudioRenderer::CHANNELS)
+ || (project.audio.improve && !project.audio.mute)
+ {
+ return false;
+ }
+ let Some(timeline) = &project.timeline else {
+ return false;
+ };
+ if !timeline.transitions.is_empty() || !timeline.hold_windows().is_empty() {
+ return false;
+ }
+ let rate = AudioData::SAMPLE_RATE as f64;
+ let mut content_end = 0.0;
+ let segments_stateless = timeline.segments.iter().all(|segment| {
+ content_end += segment.duration();
+ if segment.speed_audio_mode == Some(ClipSpeedAudioMode::Mute) {
+ return true;
+ }
+ let start = segment.start * rate;
+ segment.timescale == 1.0 && (start - start.floor() - 0.5).abs() > 1e-4
+ });
+ segments_stateless
+ && duration_secs.to_bits() == timeline.duration().to_bits()
+ && (content_end * rate).round() as usize <= (duration_secs * rate) as usize
+}
+
+/// Frames between the slot pass 1 starts writing at and the source sample it
+/// starts rendering from. The two round differently, so a render begun at
+/// `pass1_start_secs` holds the origin independent samples shifted by this
+/// many frames from its first slot on.
+fn origin_shift(pass1_start_secs: f64) -> Option<usize> {
+ let slot = output_sample_index(pass1_start_secs, AudioData::SAMPLE_RATE, 1, usize::MAX);
+ let source = (pass1_start_secs * AudioData::SAMPLE_RATE as f64).round() as usize;
+ source.checked_sub(slot).filter(|shift| *shift <= 1)
+}
+
+struct CacheRelease {
+ cache: Weak<Mutex<Option<CachedProgressiveRender>>>,
+ idle_release: std::time::Duration,
+}
+
+impl CacheRelease {
+ fn release(self, timeline: &Arc<ProgressiveTimeline>) {
+ timeline.retained.store(false, Ordering::Release);
+ let Some(cache) = self.cache.upgrade() else {
+ return;
+ };
+ let mut entry = lock(&cache);
+ if entry
+ .as_ref()
+ .is_some_and(|cached| Arc::ptr_eq(&cached.render.timeline, timeline))
+ {
+ *entry = None;
+ }
+ }
+}
+
enum PrerenderedAudioBufferMode<T: FromSampleBytes> {
Progressive(ProgressiveAudioBuffer<T>),
Streaming(Box<StreamingAudioBuffer<T>>),
@@ -1606,8 +2086,13 @@ pub struct PrerenderedAudioBuffer<T: FromSampleBytes> {
}
struct ProgressiveAudioBuffer<T: FromSampleBytes> {
- timeline: Arc<ProgressiveTimeline>,
- ready_rx: std::sync::mpsc::Receiver<()>,
+ render: Arc<ProgressiveRender>,
+ ready_rx: Option<std::sync::mpsc::Receiver<()>>,
+ /// A reader replaying a cached render reads slot `index + shift` from
+ /// `shift_from` on, which is where its own fresh render would have put
+ /// each sample.
+ shift_from: usize,
+ shift: usize,
read_position: usize,
sample_rate: u32,
channels: usize,
@@ -1616,18 +2101,63 @@ struct ProgressiveAudioBuffer<T: FromSampleBytes> {
impl<T: FromSampleBytes> Drop for ProgressiveAudioBuffer<T> {
fn drop(&mut self) {
- self.timeline.stop.store(true, Ordering::Release);
+ self.render.timeline.detach_reader();
}
}
impl<T: FromSampleBytes + cpal::FromSample<f32>> PrerenderedAudioBuffer<T> {
- pub fn new(
+ #[cfg(test)]
+ fn new(
+ segments: Vec<AudioSegment>,
+ music: MusicTracks,
+ project: &ProjectConfiguration,
+ output_info: AudioInfo,
+ duration_secs: f64,
+ start_playhead_secs: f64,
+ ) -> Self {
+ Self::create(
+ segments,
+ music,
+ project,
+ output_info,
+ duration_secs,
+ start_playhead_secs,
+ None,
+ )
+ }
+
+ /// Like [`Self::new`], but replays the cache's render when it provably
+ /// holds the same samples a fresh render would, and otherwise keeps the
+ /// fresh render for the next play when it can be replayed later.
+ #[allow(clippy::too_many_arguments)]
+ pub fn new_cached(
+ segments: Vec<AudioSegment>,
+ music: MusicTracks,
+ project: &ProjectConfiguration,
+ output_info: AudioInfo,
+ duration_secs: f64,
+ start_playhead_secs: f64,
+ cache: &PlaybackAudioCache,
+ ) -> Self {
+ Self::create(
+ segments,
+ music,
+ project,
+ output_info,
+ duration_secs,
+ start_playhead_secs,
+ Some(cache),
+ )
+ }
+
+ fn create(
segments: Vec<AudioSegment>,
music: MusicTracks,
project: &ProjectConfiguration,
output_info: AudioInfo,
duration_secs: f64,
start_playhead_secs: f64,
+ cache: Option<&PlaybackAudioCache>,
) -> Self {
let output_info = output_info.for_ffmpeg_output();
let estimated_output_samples = progressive_buffer_samples(
@@ -1638,16 +2168,42 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> PrerenderedAudioBuffer<T> {
let estimated_memory_bytes = progressive_buffer_bytes(estimated_output_samples);
let mode = if estimated_memory_bytes <= MAX_PROGRESSIVE_BUFFER_BYTES {
- PrerenderedAudioBufferMode::Progressive(ProgressiveAudioBuffer::new(
- segments,
- music,
- project,
- output_info,
- duration_secs,
- start_playhead_secs,
- estimated_output_samples,
- ))
+ let plan = cache.and_then(|_| {
+ ProgressiveAudioBuffer::<T>::cache_plan(
+ segments.clone(),
+ &music,
+ project,
+ output_info,
+ duration_secs,
+ start_playhead_secs,
+ estimated_output_samples,
+ )
+ });
+ let cached = cache
+ .zip(plan.as_ref())
+ .and_then(|(cache, plan)| ProgressiveAudioBuffer::from_cache(cache, plan));
+ PrerenderedAudioBufferMode::Progressive(match cached {
+ Some(cached) => cached,
+ None => {
+ let fresh = ProgressiveAudioBuffer::new(
+ segments,
+ music,
+ project,
+ output_info,
+ duration_secs,
+ start_playhead_secs,
+ estimated_output_samples,
+ );
+ match cache {
+ Some(cache) => fresh.retain_in_cache(cache, plan),
+ None => fresh,
+ }
+ }
+ })
} else {
+ if let Some(cache) = cache {
+ cache.clear();
+ }
info!(
duration_secs = duration_secs,
estimated_memory_mb = estimated_memory_bytes / (1024 * 1024),
@@ -1803,9 +2359,7 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
"Starting progressive audio pre-render"
);
- let pass1_start_secs = (start_playhead_secs - Self::START_PREROLL_SECS)
- .max(0.0)
- .min(duration_secs);
+ let pass1_start_secs = Self::pass1_start_secs(start_playhead_secs, duration_secs);
let primary_start = output_sample_index(
pass1_start_secs,
output_info.sample_rate,
@@ -1821,26 +2375,16 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
output_info.channels,
estimated_output_samples,
);
- let ready_window_samples = output_sample_index(
- Self::READY_WINDOW_SECS,
- output_info.sample_rate,
- output_info.channels,
- usize::MAX,
- );
- let ready_at_index = start_index.saturating_add(ready_window_samples);
+ let ready_at_index = start_index.saturating_add(Self::ready_window_samples(output_info));
- let timeline = Arc::new(ProgressiveTimeline {
- samples: ProgressiveTimeline::allocate_samples(estimated_output_samples),
+ let timeline = Arc::new(ProgressiveTimeline::new(
+ estimated_output_samples,
primary_start,
- primary_end: AtomicUsize::new(primary_start),
- wrap_end: AtomicUsize::new(0),
- stop: AtomicBool::new(false),
- complete: AtomicBool::new(false),
- });
+ ));
let (ready_tx, ready_rx) = std::sync::mpsc::channel();
- spawn_progressive_render(
+ if !spawn_progressive_render(
timeline.clone(),
segments,
music,
@@ -1850,11 +2394,15 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
pass1_start_secs,
ready_at_index,
ready_tx,
- );
+ ) {
+ lock(&timeline.retain_slot).exited = true;
+ }
Self {
- timeline,
- ready_rx,
+ render: Arc::new(ProgressiveRender { timeline }),
+ ready_rx: Some(ready_rx),
+ shift_from: usize::MAX,
+ shift: 0,
read_position: 0,
sample_rate: output_info.sample_rate,
channels: output_info.channels,
@@ -1862,36 +2410,146 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
}
}
- /// Blocks until the initial window around the start position is rendered
- /// (or the timeout elapses; unrendered reads are silence, never garbage).
- pub fn wait_until_ready(&self, timeout: std::time::Duration) {
- let _ = self.ready_rx.recv_timeout(timeout);
+ fn pass1_start_secs(start_playhead_secs: f64, duration_secs: f64) -> f64 {
+ (start_playhead_secs - Self::START_PREROLL_SECS)
+ .max(0.0)
+ .min(duration_secs)
}
- #[cfg(test)]
- fn wait_until_fully_rendered(&self) {
- let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
- while !self.timeline.complete.load(Ordering::Acquire) {
- assert!(
- std::time::Instant::now() < deadline,
- "progressive audio render did not complete in time"
- );
- std::thread::sleep(std::time::Duration::from_millis(1));
- }
+ fn ready_window_samples(output_info: AudioInfo) -> usize {
+ output_sample_index(
+ Self::READY_WINDOW_SECS,
+ output_info.sample_rate,
+ output_info.channels,
+ usize::MAX,
+ )
}
- /// Whether the next `samples` reads are rendered; `None` once the
- /// render can no longer produce them.
- #[cfg(feature = "test-hooks")]
- fn rendered(&self, samples: usize) -> Option<bool> {
- let timeline = &self.timeline;
- let start = self.read_position.min(timeline.samples.len());
- let end = (self.read_position + samples).min(timeline.samples.len());
- if start >= end || timeline.complete.load(Ordering::Acquire) {
- return Some(true);
- }
- let primary_end = timeline.primary_end.load(Ordering::Acquire);
- let wrap_end = timeline.wrap_end.load(Ordering::Acquire);
+ /// Runs after this fresh render is already on its way, so a play that
+ /// misses the cache starts exactly like an uncached one. Replays the
+ /// cached render when it holds this play's samples and has rendered at
+ /// least what the fresh render would have by now; otherwise keeps the
+ /// fresh render for later plays when its samples are origin independent.
+ #[allow(clippy::too_many_arguments)]
+ /// The cache key and sample offsets a render starting at
+ /// `start_playhead_secs` would use, when its output can be shared.
+ fn cache_plan(
+ segments: Vec<AudioSegment>,
+ music: &MusicTracks,
+ project: &ProjectConfiguration,
+ output_info: AudioInfo,
+ duration_secs: f64,
+ start_playhead_secs: f64,
+ len: usize,
+ ) -> Option<CachePlan> {
+ let pass1_start_secs = Self::pass1_start_secs(start_playhead_secs, duration_secs);
+ let shift = origin_shift(pass1_start_secs)?;
+ let key = render_is_origin_independent(project, output_info, duration_secs)
+ .then(|| {
+ ProgressiveRenderKey::new(segments, music, project, duration_secs, output_info)
+ })
+ .flatten()?;
+ let (rate, channels) = (output_info.sample_rate, output_info.channels);
+ Some(CachePlan {
+ key,
+ shift_from: output_sample_index(pass1_start_secs, rate, channels, len),
+ shift: shift * channels,
+ ready_at: output_sample_index(start_playhead_secs, rate, channels, len)
+ .saturating_add(Self::ready_window_samples(output_info)),
+ rate,
+ channels,
+ })
+ }
+
+ /// A reader over the cached render when it matches `plan` and already
+ /// covers the start window. Checked before a fresh render is built, so a
+ /// replay never allocates a second full-timeline buffer.
+ fn from_cache(cache: &PlaybackAudioCache, plan: &CachePlan) -> Option<Self> {
+ let entry = lock(&cache.entry);
+ let cached = entry.as_ref()?;
+ if cached.key != plan.key
+ || cached.render.timeline.stop.load(Ordering::Acquire)
+ || !cached.render.timeline.covers(
+ plan.shift_from + plan.shift,
+ plan.ready_at.saturating_add(plan.shift),
+ )
+ {
+ return None;
+ }
+ let render = cached.render.clone();
+ drop(entry);
+ render.timeline.attach_reader();
+ info!("Reusing rendered playback audio");
+ Some(Self {
+ render,
+ ready_rx: None,
+ shift_from: plan.shift_from,
+ shift: plan.shift,
+ read_position: 0,
+ sample_rate: plan.rate,
+ channels: plan.channels,
+ _format: std::marker::PhantomData,
+ })
+ }
+
+ fn retain_in_cache(self, cache: &PlaybackAudioCache, plan: Option<CachePlan>) -> Self {
+ let Some(plan) = plan else {
+ let previous = lock(&cache.entry).take();
+ self.render.timeline.retire(previous);
+ return self;
+ };
+ let mut entry = lock(&cache.entry);
+ let previous = if plan.shift == 0
+ && self.render.timeline.retain(CacheRelease {
+ cache: Arc::downgrade(&cache.entry),
+ idle_release: cache.idle_release,
+ }) {
+ entry.replace(CachedProgressiveRender {
+ key: plan.key,
+ render: self.render.clone(),
+ })
+ } else if entry.as_ref().is_some_and(|cached| cached.key != plan.key) {
+ entry.take()
+ } else {
+ None
+ };
+ drop(entry);
+ self.render.timeline.retire(previous);
+ self
+ }
+
+ /// Blocks until the initial window around the start position is rendered
+ /// (or the timeout elapses; unrendered reads are silence, never garbage).
+ pub fn wait_until_ready(&self, timeout: std::time::Duration) {
+ if let Some(ready_rx) = &self.ready_rx {
+ let _ = ready_rx.recv_timeout(timeout);
+ }
+ }
+
+ #[cfg(test)]
+ fn wait_until_fully_rendered(&self) {
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
+ while !self.render.timeline.complete.load(Ordering::Acquire) {
+ assert!(
+ std::time::Instant::now() < deadline,
+ "progressive audio render did not complete in time"
+ );
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ }
+ }
+
+ /// Whether the next `samples` reads are rendered; `None` once the
+ /// render can no longer produce them.
+ #[cfg(feature = "test-hooks")]
+ fn rendered(&self, samples: usize) -> Option<bool> {
+ let timeline = &self.render.timeline;
+ let start = self.read_position.min(timeline.samples.len());
+ let end = (self.read_position + samples).min(timeline.samples.len());
+ if start >= end || timeline.complete.load(Ordering::Acquire) {
+ return Some(true);
+ }
+ let primary_end = timeline.primary_end.load(Ordering::Acquire);
+ let wrap_end = timeline.wrap_end.load(Ordering::Acquire);
let primary = timeline.primary_start;
let covered = if start >= primary {
primary_end >= end
@@ -1911,7 +2569,7 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
pub fn set_playhead(&mut self, playhead_secs: f64) {
let sample_position = (playhead_secs * self.sample_rate as f64) as usize * self.channels;
- self.read_position = sample_position.min(self.timeline.samples.len());
+ self.read_position = sample_position.min(self.render.timeline.samples.len());
}
pub fn current_audible_playhead(&self, device_latency_secs: f64) -> f64 {
@@ -1925,22 +2583,27 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
}
pub fn fill(&mut self, buffer: &mut [T]) {
- let samples = &self.timeline.samples;
+ let timeline = &self.render.timeline;
+ let samples = &timeline.samples;
let available = samples.len().saturating_sub(self.read_position);
let to_copy = buffer.len().min(available);
if to_copy > 0 {
- let primary_start = self.timeline.primary_start;
- let primary_end = self.timeline.primary_end.load(Ordering::Acquire);
- let wrap_end = self.timeline.wrap_end.load(Ordering::Acquire);
+ let primary_start = timeline.primary_start;
+ let primary_end = timeline.primary_end.load(Ordering::Acquire);
+ let wrap_end = timeline.wrap_end.load(Ordering::Acquire);
for (i, slot) in buffer[..to_copy].iter_mut().enumerate() {
- let idx = self.read_position + i;
- let rendered = if idx >= primary_start {
- idx < primary_end
- } else {
- idx < wrap_end
- };
+ let mut idx = self.read_position + i;
+ if idx >= self.shift_from {
+ idx += self.shift;
+ }
+ let rendered = idx < samples.len()
+ && if idx >= primary_start {
+ idx < primary_end
+ } else {
+ idx < wrap_end
+ };
*slot = if rendered {
<T as cpal::Sample>::from_sample(f32::from_bits(
samples[idx].load(Ordering::Relaxed),
@@ -2381,11 +3044,16 @@ fn spawn_progressive_render(
pass1_start_secs: f64,
ready_at_index: usize,
ready_tx: std::sync::mpsc::Sender<()>,
-) {
+) -> bool {
let spawn_result = std::thread::Builder::new()
.name("cap-audio-prerender".into())
.spawn(move || {
let render_start = std::time::Instant::now();
+ let mut control = RenderControl {
+ timeline: timeline.clone(),
+ release: None,
+ adopted: false,
+ };
let mut renderer = AudioRenderer::new(segments).with_music(music);
let chunk_size = 1024usize;
@@ -2406,6 +3074,7 @@ fn spawn_progressive_render(
// original blocking pre-render loop chunk for chunk so the sample
// values are identical.
let render_pass = |renderer: &mut AudioRenderer,
+ control: &mut RenderControl,
start_secs: f64,
from_source: usize,
to_source: usize,
@@ -2423,7 +3092,7 @@ fn spawn_progressive_render(
let mut rendered_source = from_source;
while rendered_source < to_source {
- if timeline.stop.load(Ordering::Acquire) {
+ if !control.should_continue() {
return false;
}
@@ -2468,6 +3137,7 @@ fn spawn_progressive_render(
// Pass 1: playhead (minus pre-roll) to the end of the timeline.
let pass1_ok = render_pass(
&mut renderer,
+ &mut control,
pass1_start_secs,
pass1_source_start,
total_source_samples,
@@ -2478,10 +3148,15 @@ fn spawn_progressive_render(
&mut ready_sent,
);
+ if pass1_ok {
+ timeline.pass1_done.store(true, Ordering::Release);
+ }
+
// Pass 2: wrap around to cover [0, playhead) for backwards seeks.
let pass2_ok = if pass1_ok && pass1_source_start > 0 {
render_pass(
&mut renderer,
+ &mut control,
0.0,
0,
pass1_source_start,
@@ -2497,7 +3172,8 @@ fn spawn_progressive_render(
send_ready(&mut ready_sent);
- if pass1_ok && pass2_ok {
+ let completed = pass1_ok && pass2_ok;
+ if completed {
timeline.complete.store(true, Ordering::Release);
info!(
total_samples = buffer_len,
@@ -2506,19 +3182,115 @@ fn spawn_progressive_render(
"Progressive audio pre-render complete"
);
}
+ drop(renderer);
+ timeline.release_retired(true);
+ control.finish(completed);
});
- if let Err(e) = spawn_result {
+ if let Err(e) = &spawn_result {
tracing::error!("Failed to spawn audio pre-render thread: {e}");
}
+ spawn_result.is_ok()
+}
+
+/// Lets a cached render outlive its readers: it parks while nobody is
+/// playing, resumes when a replay attaches, and frees itself once the editor
+/// stays paused for the idle timeout.
+struct RenderControl {
+ timeline: Arc<ProgressiveTimeline>,
+ release: Option<CacheRelease>,
+ adopted: bool,
+}
+
+impl RenderControl {
+ fn should_continue(&mut self) -> bool {
+ loop {
+ let seen = self.timeline.wake_generation();
+ if self.timeline.stop.load(Ordering::Acquire) {
+ return false;
+ }
+ if !self.timeline.retained.load(Ordering::Acquire) {
+ return true;
+ }
+ self.adopt_release();
+ if self.release_if_idle() {
+ continue;
+ }
+ if self.timeline.readers.load(Ordering::Acquire) > 0 {
+ return true;
+ }
+ self.timeline.release_retired(false);
+ self.wait_for_wake(seen);
+ }
+ }
+
+ fn adopt_release(&mut self) {
+ if !self.adopted {
+ self.adopted = true;
+ self.release = lock(&self.timeline.retain_slot).release.take();
+ }
+ }
+
+ fn finish(mut self, completed: bool) {
+ {
+ let mut slot = lock(&self.timeline.retain_slot);
+ slot.exited = true;
+ if !self.adopted {
+ self.adopted = true;
+ self.release = slot.release.take();
+ }
+ }
+ if completed {
+ loop {
+ let seen = self.timeline.wake_generation();
+ if self.release.is_none() || self.timeline.stop.load(Ordering::Acquire) {
+ break;
+ }
+ if self.release_if_idle() {
+ return;
+ }
+ self.wait_for_wake(seen);
+ }
+ }
+ if let Some(release) = self.release.take() {
+ release.release(&self.timeline);
+ }
+ }
+
+ fn release_if_idle(&mut self) -> bool {
+ let Some(release) = &self.release else {
+ return false;
+ };
+ if self.timeline.readers.load(Ordering::Acquire) == 0
+ && self.timeline.paused_for() >= release.idle_release
+ && let Some(release) = self.release.take()
+ {
+ release.release(&self.timeline);
+ return true;
+ }
+ false
+ }
+
+ fn wait_for_wake(&self, seen: u64) {
+ let idle_release = self
+ .release
+ .as_ref()
+ .map_or(PLAYBACK_AUDIO_IDLE_RELEASE, |release| release.idle_release);
+ let timeout = if self.timeline.readers.load(Ordering::Acquire) > 0 {
+ idle_release
+ } else {
+ idle_release.saturating_sub(self.timeline.paused_for())
+ };
+ self.timeline.wait(seen, timeout);
+ }
}
#[cfg(test)]
mod tests {
use super::*;
use cap_project::{
- ClipConfiguration, ClipTransition, ProjectConfiguration, TimelineConfiguration,
- TimelineSegment,
+ ClipConfiguration, ClipTransition, ClipTransitionType, ProjectConfiguration,
+ TimelineConfiguration, TimelineSegment,
};
use std::{path::Path, sync::Arc};
use tempfile::TempDir;
@@ -3955,4 +4727,733 @@ mod tests {
assert!((left_at_second(&stream, 0) - expected(4_000)).abs() < 0.02);
assert!((left_at_second(&stream, 1) - expected(10_000)).abs() < 0.02);
}
+
+ static CACHE_RELEASE: std::sync::RwLock<()> = std::sync::RwLock::new(());
+
+ struct ProgressiveFixture {
+ _dir: TempDir,
+ segments: Vec<AudioSegment>,
+ music: MusicTracks,
+ music_path: String,
+ project: ProjectConfiguration,
+ }
+
+ impl ProgressiveFixture {
+ fn new() -> Self {
+ let _ = ffmpeg::init();
+ let dir = tempfile::tempdir().unwrap();
+ let mic_path = dir.path().join("mic.wav");
+ let system_path = dir.path().join("system.wav");
+ let music_path = dir.path().join("music.wav");
+ write_sine_wav(&mic_path, 440.0, 8.0);
+ write_sine_wav(&system_path, 220.0, 8.0);
+ write_step_wav(&music_path, &[3_000, 6_000]);
+ let load = |path: &Path| {
+ Arc::new(DecodedAudio::from(Arc::new(
+ AudioData::from_file(path).unwrap(),
+ )))
+ };
+ let segments = vec![crate::segments::audio_segment_from_decoded(
+ Some(load(&mic_path)),
+ Some(load(&system_path)),
+ crate::SegmentAudioTimingRepair::default(),
+ )];
+ let mut quiet = segment(0, 6.0, 8.0, 1.0);
+ quiet.volume = Some(0.5);
+ let music_path = music_path.to_string_lossy().into_owned();
+ let project = ProjectConfiguration {
+ timeline: Some(TimelineConfiguration {
+ segments: vec![segment(0, 0.0, 3.0, 1.0), segment(0, 3.5, 6.0, 1.0), quiet],
+ transitions: Vec::new(),
+ zoom_segments: Vec::new(),
+ scene_segments: Vec::new(),
+ style_segments: Vec::new(),
+ image_segments: Vec::new(),
+ mask_segments: Vec::new(),
+ text_segments: Vec::new(),
+ caption_segments: Vec::new(),
+ keyboard_segments: Vec::new(),
+ audio_segments: vec![music_track_segment(&music_path, 0.5, 4.0, 0.3, 0.4)],
+ camera3d_segments: Vec::new(),
+ }),
+ clips: vec![ClipConfiguration {
+ index: 0,
+ ..Default::default()
+ }],
+ ..Default::default()
+ };
+ let mut music = MusicTracks::new();
+ music.insert(
+ music_path.clone(),
+ Arc::new(AudioData::from_file(&music_path).unwrap()),
+ );
+ Self {
+ _dir: dir,
+ segments,
+ music,
+ music_path,
+ project,
+ }
+ }
+
+ fn timeline(&mut self) -> &mut TimelineConfiguration {
+ self.project.timeline.as_mut().unwrap()
+ }
+
+ fn duration(&self) -> f64 {
+ self.project.timeline.as_ref().unwrap().duration()
+ }
+
+ fn buffer(&self, output_info: AudioInfo, start: f64) -> PrerenderedAudioBuffer<f32> {
+ PrerenderedAudioBuffer::new(
+ self.segments.clone(),
+ self.music.clone(),
+ &self.project,
+ output_info,
+ self.duration(),
+ start,
+ )
+ }
+
+ fn cached(
+ &self,
+ cache: &PlaybackAudioCache,
+ output_info: AudioInfo,
+ start: f64,
+ ) -> PrerenderedAudioBuffer<f32> {
+ PrerenderedAudioBuffer::new_cached(
+ self.segments.clone(),
+ self.music.clone(),
+ &self.project,
+ output_info,
+ self.duration(),
+ start,
+ cache,
+ )
+ }
+
+ fn reference(&self, output_info: AudioInfo, start: f64) -> Vec<f32> {
+ reference_progressive_render(
+ self.segments.clone(),
+ self.music.clone(),
+ &self.project,
+ output_info,
+ self.duration(),
+ start,
+ )
+ }
+ }
+
+ type FixtureEdit = fn(&mut ProgressiveFixture);
+
+ fn stateful_edits() -> [(&'static str, FixtureEdit); 5] {
+ [
+ ("studio sound", |f| f.project.audio.improve = true),
+ ("speed", |f| f.timeline().segments[1].timescale = 1.5),
+ ("transition", |f| {
+ f.timeline().transitions = vec![ClipTransition {
+ segment_index: 1,
+ kind: ClipTransitionType::CrossFade,
+ duration: 0.5,
+ }]
+ }),
+ ("text hold", |f| {
+ let mut text: cap_project::TextSegment =
+ serde_json::from_value(serde_json::json!({ "start": 2.0, "end": 3.0 }))
+ .unwrap();
+ text.enabled = true;
+ text.layout = cap_project::TextLayout::Fullscreen;
+ f.timeline().text_segments = vec![text];
+ }),
+ ("segment start on a sample tie", |f| {
+ f.timeline().segments[1].start = 3.5 + 0.5 / AudioData::SAMPLE_RATE as f64
+ }),
+ ]
+ }
+
+ fn stereo_48k() -> AudioInfo {
+ AudioInfo::new_raw(AudioData::SAMPLE_FORMAT, 48_000, 2)
+ }
+
+ #[allow(clippy::too_many_arguments)]
+ fn reference_pass(
+ out: &mut [f32],
+ renderer: &mut AudioRenderer,
+ project: &ProjectConfiguration,
+ render_info: AudioInfo,
+ start_secs: f64,
+ from_source: usize,
+ to_source: usize,
+ mut out_idx: usize,
+ out_limit: usize,
+ ) {
+ let mut resampler = AudioResampler::new(render_info).unwrap();
+ renderer.set_playhead(start_secs, project);
+ let mut store = |bytes: &[u8], out_idx: &mut usize| {
+ for chunk in bytes.chunks_exact(f32::BYTE_SIZE) {
+ if *out_idx >= out_limit {
+ break;
+ }
+ out[*out_idx] = f32::from_bytes(chunk);
+ *out_idx += 1;
+ }
+ };
+ let mut rendered = from_source;
+ while rendered < to_source {
+ let frame = match renderer.render_frame(1024, project) {
+ Some(frame) => frame,
+ None => silent_audio_frame(1024),
+ };
+ store(resampler.queue_and_process_frame(&frame), &mut out_idx);
+ rendered += 1024;
+ }
+ while let Some(flushed) = resampler.flush_frame() {
+ if flushed.is_empty() || out_idx >= out_limit {
+ break;
+ }
+ store(flushed, &mut out_idx);
+ }
+ }
+
+ fn reference_progressive_render(
+ segments: Vec<AudioSegment>,
+ music: MusicTracks,
+ project: &ProjectConfiguration,
+ output_info: AudioInfo,
+ duration: f64,
+ start: f64,
+ ) -> Vec<f32> {
+ let output_info = output_info.for_ffmpeg_output();
+ let (rate, channels) = (output_info.sample_rate, output_info.channels);
+ let render_info = AudioInfo::new_raw(AudioData::SAMPLE_FORMAT, rate, channels as u16);
+ let len = progressive_buffer_samples(duration, rate, channels);
+ let mut out = vec![0.0f32; len];
+ let mut renderer = AudioRenderer::new(segments).with_music(music);
+ let pass1_start = (start - 1.0).max(0.0).min(duration);
+ let primary_start = output_sample_index(pass1_start, rate, channels, len);
+ let total = (duration * AudioData::SAMPLE_RATE as f64) as usize;
+ let pass1_source = (pass1_start * AudioData::SAMPLE_RATE as f64) as usize;
+ reference_pass(
+ &mut out,
+ &mut renderer,
+ project,
+ render_info,
+ pass1_start,
+ pass1_source,
+ total,
+ primary_start,
+ len,
+ );
+ if pass1_source > 0 {
+ reference_pass(
+ &mut out,
+ &mut renderer,
+ project,
+ render_info,
+ 0.0,
+ 0,
+ pass1_source,
+ 0,
+ primary_start,
+ );
+ }
+ out
+ }
+
+ fn progressive_timeline(buffer: &PrerenderedAudioBuffer<f32>) -> Arc<ProgressiveTimeline> {
+ let PrerenderedAudioBufferMode::Progressive(progressive) = &buffer.mode else {
+ panic!("expected a progressive buffer");
+ };
+ progressive.render.timeline.clone()
+ }
+
+ fn read_everything(buffer: &mut PrerenderedAudioBuffer<f32>) -> Vec<f32> {
+ buffer.wait_until_fully_rendered();
+ let len = progressive_timeline(buffer).samples.len();
+ buffer.set_playhead(0.0);
+ let mut out = vec![1.0f32; len];
+ buffer.fill(&mut out);
+ out
+ }
+
+ fn assert_bit_identical(actual: &[f32], expected: &[f32], context: &str) {
+ assert_eq!(actual.len(), expected.len(), "{context}");
+ if let Some(index) = actual
+ .iter()
+ .zip(expected)
+ .position(|(a, b)| a.to_bits() != b.to_bits())
+ {
+ panic!(
+ "{context}: sample {index} differs: {} vs {}",
+ actual[index], expected[index]
+ );
+ }
+ }
+
+ fn wait_until_released(timeline: Weak<ProgressiveTimeline>, within: std::time::Duration) {
+ let deadline = std::time::Instant::now() + within;
+ while timeline.strong_count() > 0 {
+ assert!(
+ std::time::Instant::now() < deadline,
+ "cached render was not released"
+ );
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ }
+ }
+
+ #[test]
+ fn origin_shift_matches_the_render_rounding() {
+ let rate = AudioData::SAMPLE_RATE as f64;
+ assert_eq!(origin_shift(0.0), Some(0));
+ assert_eq!(origin_shift(1.0), Some(0));
+ assert_eq!(origin_shift(2.0 + 0.4 / rate), Some(0));
+ assert_eq!(origin_shift(2.0 + 0.6 / rate), Some(1));
+ for frame in 0..2_000u32 {
+ let secs = (f64::from(frame) / 30.0 + 0.0123 - 1.0).max(0.0);
+ let slot = output_sample_index(secs, AudioData::SAMPLE_RATE, 1, usize::MAX);
+ let source = (secs * rate).round() as usize;
+ assert_eq!(origin_shift(secs), Some(source - slot));
+ }
+ }
+
+ #[test]
+ fn uncached_progressive_playback_is_one_uninterrupted_render() {
+ let mut fixture = ProgressiveFixture::new();
+ for improve in [false, true] {
+ fixture.project.audio.improve = improve;
+ for output_info in [
+ stereo_48k(),
+ AudioInfo::new_raw(AudioData::SAMPLE_FORMAT, 44_100, 2),
+ ] {
+ for start in [0.0, 2.7] {
+ let context = format!(
+ "improve {improve}, {} Hz, start {start}",
+ output_info.sample_rate
+ );
+ let mut buffer = fixture.buffer(output_info, start);
+ buffer.set_playhead(start);
+ assert!(buffer.wait_until_ready(std::time::Duration::from_secs(30)));
+ assert_bit_identical(
+ &read_everything(&mut buffer),
+ &fixture.reference(output_info, start),
+ &context,
+ );
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn replays_from_any_start_match_a_fresh_render_from_that_start() {
+ let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
+ let fixture = ProgressiveFixture::new();
+ let output_info = stereo_48k();
+ let rate = AudioData::SAMPLE_RATE as f64;
+ let cache = PlaybackAudioCache::default();
+ let first = fixture.cached(&cache, output_info, 1.0);
+ first.wait_until_fully_rendered();
+ let timeline = progressive_timeline(&first);
+ drop(first);
+
+ let mut shifts = [0usize; 2];
+ let starts = [
+ 0.0,
+ 0.4,
+ 2.7,
+ 3.0 + 1.0 / 3.0,
+ 4.0 + 0.6 / rate,
+ 5.0 + 0.4 / rate,
+ 6.123_456_789,
+ 7.4,
+ 7.5,
+ 9.0,
+ ]
+ .into_iter()
+ .chain((0..40).map(|frame| f64::from(frame) * 0.187 + 0.0213));
+ for start in starts {
+ let context = format!("start {start}");
+ shifts[origin_shift((start - 1.0).clamp(0.0, fixture.duration())).unwrap()] += 1;
+ let mut replay = fixture.cached(&cache, output_info, start);
+ assert!(
+ Arc::ptr_eq(&timeline, &progressive_timeline(&replay)),
+ "{context}: must replay the cached render"
+ );
+ replay.set_playhead(start);
+ assert!(replay.wait_until_ready(std::time::Duration::from_secs(1)));
+ let replayed = read_everything(&mut replay);
+ drop(replay);
+ assert_bit_identical(&replayed, &fixture.reference(output_info, start), &context);
+ let mut fresh = fixture.buffer(output_info, start);
+ assert_bit_identical(&replayed, &read_everything(&mut fresh), &context);
+ }
+ assert!(shifts.iter().all(|count| *count > 0), "{shifts:?}");
+ }
+
+ #[test]
+ fn stateful_audio_and_resampled_output_never_replay() {
+ let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
+ let mut cases: Vec<(String, ProgressiveFixture, AudioInfo)> = stateful_edits()
+ .into_iter()
+ .map(|(name, edit)| {
+ let mut fixture = ProgressiveFixture::new();
+ edit(&mut fixture);
+ (name.to_string(), fixture, stereo_48k())
+ })
+ .collect();
+ for (rate, channels) in [(44_100, 2), (96_000, 2), (48_000, 1), (48_000, 6)] {
+ cases.push((
+ format!("{rate} Hz x{channels}"),
+ ProgressiveFixture::new(),
+ AudioInfo::new_raw(AudioData::SAMPLE_FORMAT, rate, channels),
+ ));
+ }
+ for (name, fixture, output_info) in cases {
+ let cache = PlaybackAudioCache::default();
+ let first = fixture.cached(&cache, output_info, 1.0);
+ first.wait_until_fully_rendered();
+ let timeline = progressive_timeline(&first);
+ drop(first);
+ assert!(lock(&cache.entry).is_none(), "{name} must not be cached");
+ for start in [1.0, 3.3] {
+ let mut replay = fixture.cached(&cache, output_info, start);
+ assert!(
+ !Arc::ptr_eq(&timeline, &progressive_timeline(&replay)),
+ "{name} must render fresh"
+ );
+ assert_bit_identical(
+ &read_everything(&mut replay),
+ &fixture.reference(output_info, start),
+ &name,
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn an_unrendered_start_renders_fresh() {
+ let fixture = ProgressiveFixture::new();
+ let output_info = stereo_48k();
+ let len = progressive_buffer_samples(fixture.duration(), 48_000, 2);
+ let primary_start = output_sample_index(4.0, 48_000, 2, len);
+ let timeline = ProgressiveTimeline::new(len, primary_start);
+ timeline
+ .primary_end
+ .store(primary_start + 1_000, Ordering::Release);
+ assert!(timeline.covers(primary_start, primary_start + 1_000));
+ assert!(!timeline.covers(primary_start, primary_start + 1_001));
+ assert!(!timeline.covers(primary_start - 2, primary_start + 10));
+ timeline.wrap_end.store(primary_start, Ordering::Release);
+ assert!(timeline.covers(0, primary_start + 1_000));
+ timeline.pass1_done.store(true, Ordering::Release);
+ assert!(timeline.covers(0, len + 10));
+
+ let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
+ let cache = PlaybackAudioCache::default();
+ let parked = fixture.cached(&cache, output_info, 0.0);
+ let cached = progressive_timeline(&parked);
+ cached.stop.store(true, Ordering::Release);
+ drop(parked);
+ let replay = fixture.cached(&cache, output_info, 0.0);
+ assert!(!Arc::ptr_eq(&cached, &progressive_timeline(&replay)));
+ }
+
+ #[test]
+ fn every_audio_change_renders_fresh_and_visual_changes_reuse() {
+ let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
+ let audio_edits: [(&str, FixtureEdit); 11] = [
+ ("mic volume", |f| f.project.audio.mic_volume_db = -6.0),
+ ("system volume", |f| f.project.audio.system_volume_db = -6.0),
+ ("mute", |f| f.project.audio.mute = true),
+ ("stereo mode", |f| {
+ f.project.audio.mic_stereo_mode = cap_project::StereoMode::MonoL;
+ f.project.audio.mic_volume_db = -1.0;
+ }),
+ ("clip volume", |f| {
+ f.timeline().segments[0].volume = Some(0.25)
+ }),
+ ("muted speed", |f| {
+ let segment = &mut f.timeline().segments[1];
+ segment.timescale = 1.5;
+ segment.speed_audio_mode = Some(ClipSpeedAudioMode::Mute);
+ }),
+ ("trim", |f| f.timeline().segments[2].end = 7.0),
+ ("clip offset", |f| f.project.clips[0].offsets.mic = 0.2),
+ ("music volume", |f| {
+ f.timeline().audio_segments[0].volume_db = -9.0
+ }),
+ ("music", |f| {
+ let path = f.music_path.clone();
+ f.timeline()
+ .audio_segments
+ .push(music_track_segment(&path, 5.0, 6.0, 0.0, 0.0))
+ }),
+ ("split", |f| {
+ let timeline = f.timeline();
+ let mut second = timeline.segments[0].clone();
+ timeline.segments[0].end = 1.5;
+ second.start = 1.5;
+ timeline.segments.insert(1, second);
+ }),
+ ];
+ let visual_edits: [(&str, FixtureEdit); 4] = [
+ ("zoom", |f| {
+ f.timeline().zoom_segments = vec![cap_project::ZoomSegment {
+ start: 1.0,
+ end: 2.0,
+ amount: 2.0,
+ mode: cap_project::ZoomMode::Auto,
+ glide_direction: Default::default(),
+ glide_speed: 1.0,
+ instant_animation: false,
+ edge_snap_ratio: 0.1,
+ }]
+ }),
+ ("motion blur", |f| f.project.screen_motion_blur = 0.9),
+ ("clip name", |f| {
+ f.timeline().segments[0].name = Some("Intro".into())
+ }),
+ ("hide cursor", |f| {
+ f.timeline().segments[0].hide_cursor = Some(true)
+ }),
+ ];
+ let output_info = stereo_48k();
+
+ for (name, edit) in audio_edits.into_iter().chain(stateful_edits()) {
+ let mut fixture = ProgressiveFixture::new();
+ let cache = PlaybackAudioCache::default();
+ let mut before = fixture.cached(&cache, output_info, 0.0);
+ let before_audio = read_everything(&mut before);
+ let before_timeline = progressive_timeline(&before);
+ drop(before);
+ edit(&mut fixture);
+ let mut after = fixture.cached(&cache, output_info, 0.0);
+ assert!(
+ !Arc::ptr_eq(&before_timeline, &progressive_timeline(&after)),
+ "{name} must not reuse the cached render"
+ );
+ let after_audio = read_everything(&mut after);
+ assert_bit_identical(&after_audio, &fixture.reference(output_info, 0.0), name);
+ assert!(
+ name == "split"
+ || before_audio.len() != after_audio.len()
+ || before_audio
+ .iter()
+ .zip(&after_audio)
+ .any(|(a, b)| a.to_bits() != b.to_bits()),
+ "{name} must change the rendered audio"
+ );
+ }
+
+ for (name, edit) in visual_edits {
+ let mut fixture = ProgressiveFixture::new();
+ let cache = PlaybackAudioCache::default();
+ let before = fixture.cached(&cache, output_info, 0.0);
+ before.wait_until_fully_rendered();
+ let before_timeline = progressive_timeline(&before);
+ drop(before);
+ edit(&mut fixture);
+ let after = fixture.cached(&cache, output_info, 0.0);
+ assert!(
+ Arc::ptr_eq(&before_timeline, &progressive_timeline(&after)),
+ "{name} must reuse the cached render"
+ );
+ }
+ }
+
+ #[test]
+ fn a_stale_render_is_released_once_the_next_render_finishes() {
+ let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
+ let mut fixture = ProgressiveFixture::new();
+ let output_info = stereo_48k();
+ let within = std::time::Duration::from_secs(10);
+
+ let cache = PlaybackAudioCache::default();
+ let mut playing = fixture.cached(&cache, output_info, 0.0);
+ read_everything(&mut playing);
+ let stale = Arc::downgrade(&progressive_timeline(&playing));
+ drop(playing);
+ std::thread::sleep(std::time::Duration::from_millis(50));
+ assert!(stale.strong_count() > 0, "a paused render stays cached");
+
+ fixture.project.audio.mic_volume_db = -12.0;
+ let mut edited = fixture.cached(&cache, output_info, 2.0);
+ let fresh = progressive_timeline(&edited);
+ assert!(!Arc::ptr_eq(&fresh, &stale.upgrade().unwrap()));
+ edited.set_playhead(2.0);
+ assert!(edited.wait_until_ready(std::time::Duration::from_secs(30)));
+ wait_until_released(stale, within);
+ assert!(
+ lock(&cache.entry)
+ .as_ref()
+ .is_some_and(|cached| Arc::ptr_eq(&cached.render.timeline, &fresh))
+ );
+ drop(fresh);
+ read_everything(&mut edited);
+ let kept = Arc::downgrade(&progressive_timeline(&edited));
+ drop(edited);
+
+ fixture.project.audio.improve = true;
+ let mut stateful = fixture.cached(&cache, output_info, 2.0);
+ stateful.set_playhead(2.0);
+ assert!(stateful.wait_until_ready(std::time::Duration::from_secs(30)));
+ wait_until_released(kept, within);
+ assert!(lock(&cache.entry).is_none());
+ drop(stateful);
+
+ fixture.project.audio.improve = false;
+ let closed = fixture.cached(&cache, output_info, 0.0);
+ let timeline = Arc::downgrade(&progressive_timeline(&closed));
+ drop(closed);
+ drop(cache);
+ wait_until_released(timeline, within);
+ }
+
+ #[test]
+ fn cache_key_changes_with_every_audio_input_and_ignores_visual_fields() {
+ let fixture = ProgressiveFixture::new();
+ let original = audio_project_key(&fixture.project).unwrap();
+ let key = |edit: FixtureEdit| {
+ let mut edited = ProgressiveFixture::new();
+ edited.project = fixture.project.clone();
+ edit(&mut edited);
+ audio_project_key(&edited.project).unwrap()
+ };
+
+ let audio: [(&str, FixtureEdit); 9] = [
+ ("audio", |f| f.project.audio.system_volume_db = -3.0),
+ ("clip offsets", |f| {
+ f.project.clips[0].offsets.system_audio = 0.1
+ }),
+ ("segment", |f| f.timeline().segments[0].end = 2.5),
+ ("segment volume", |f| {
+ f.timeline().segments[0].volume = Some(0.8)
+ }),
+ ("speed mode", |f| {
+ f.timeline().segments[0].speed_audio_mode = Some(ClipSpeedAudioMode::Mute)
+ }),
+ ("music", |f| f.timeline().audio_segments[0].fade_in = 0.1),
+ ("transition", |f| {
+ f.timeline().transitions = vec![ClipTransition {
+ segment_index: 1,
+ kind: ClipTransitionType::CrossFade,
+ duration: 0.25,
+ }]
+ }),
+ ("text hold", stateful_edits()[3].1),
+ ("no timeline", |f| f.project.timeline = None),
+ ];
+ for (name, edit) in audio {
+ assert_ne!(key(edit), original, "{name} must change the key");
+ }
+
+ let visual: [(&str, FixtureEdit); 6] = [
+ ("background", |f| f.project.background.padding = 12.5),
+ ("cursor", |f| f.project.cursor.size = 150),
+ ("motion blur", |f| f.project.screen_motion_blur = 0.9),
+ ("clip name", |f| {
+ f.timeline().segments[0].name = Some("Intro".into())
+ }),
+ ("hide cursor", |f| {
+ f.timeline().segments[0].hide_cursor = Some(true)
+ }),
+ ("overlay text", |f| {
+ let mut text: cap_project::TextSegment =
+ serde_json::from_value(serde_json::json!({ "start": 2.0, "end": 3.0 }))
+ .unwrap();
+ text.enabled = true;
+ f.timeline().text_segments = vec![text];
+ }),
+ ];
+ for (name, edit) in visual {
+ assert_eq!(key(edit), original, "{name} must keep the key");
+ }
+ }
+
+ #[test]
+ fn a_replay_resumes_a_parked_render() {
+ let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
+ let fixture = ProgressiveFixture::new();
+ let output_info = stereo_48k();
+ for _ in 0..20 {
+ let cache = PlaybackAudioCache::default();
+ let mut first = fixture.cached(&cache, output_info, 0.0);
+ first.set_playhead(0.0);
+ assert!(first.wait_until_ready(std::time::Duration::from_secs(30)));
+ let timeline = progressive_timeline(&first);
+ drop(first);
+ let mut replay = fixture.cached(&cache, output_info, 0.0);
+ assert!(Arc::ptr_eq(&timeline, &progressive_timeline(&replay)));
+ assert_bit_identical(
+ &read_everything(&mut replay),
+ &fixture.reference(output_info, 0.0),
+ "resumed render",
+ );
+ }
+ }
+
+ #[test]
+ fn idle_pause_releases_the_cached_render() {
+ let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
+ let fixture = ProgressiveFixture::new();
+ let output_info = stereo_48k();
+ let idle = std::time::Duration::from_millis(300);
+
+ for complete in [true, false] {
+ let cache = PlaybackAudioCache::with_idle_release(idle);
+ let mut playing = fixture.cached(&cache, output_info, 0.0);
+ if complete {
+ read_everything(&mut playing);
+ }
+ std::thread::sleep(idle * 2);
+ assert!(
+ lock(&cache.entry).is_some(),
+ "a playing render must stay cached"
+ );
+ let timeline = Arc::downgrade(&progressive_timeline(&playing));
+ let paused = std::time::Instant::now();
+ drop(playing);
+ wait_until_released(timeline, std::time::Duration::from_secs(10));
+ assert!(paused.elapsed() >= idle);
+ assert!(lock(&cache.entry).is_none());
+ }
+ }
+
+ #[test]
+ fn export_and_memory_pressure_release_every_cached_render() {
+ let _release = CACHE_RELEASE
+ .write()
+ .unwrap_or_else(PoisonError::into_inner);
+ let fixture = ProgressiveFixture::new();
+ let output_info = stereo_48k();
+ type Release = fn();
+ let releases: Vec<(&str, Release)> = vec![
+ ("export", release_playback_audio_caches),
+ #[cfg(target_os = "macos")]
+ ("memory pressure", || {
+ memory_pressure::on_memory_pressure(std::ptr::null_mut())
+ }),
+ ];
+ for (name, release) in releases {
+ let caches = [PlaybackAudioCache::default(), PlaybackAudioCache::default()];
+ let timelines = caches
+ .iter()
+ .map(|cache| {
+ let paused = fixture.cached(cache, output_info, 0.0);
+ Arc::downgrade(&progressive_timeline(&paused))
+ })
+ .collect::<Vec<_>>();
+ assert!(
+ caches.iter().all(|cache| lock(&cache.entry).is_some()),
+ "{name}"
+ );
+ release();
+ for timeline in timelines {
+ wait_until_released(timeline, std::time::Duration::from_secs(10));
+ }
+ assert!(
+ caches.iter().all(|cache| lock(&cache.entry).is_none()),
+ "{name}"
+ );
+ }
+ }
}
diff --git a/crates/editor/src/audio_output.rs b/crates/editor/src/audio_output.rs
index d7d5f7218..3de0d2f2b 100644
--- a/crates/editor/src/audio_output.rs
+++ b/crates/editor/src/audio_output.rs
@@ -33,7 +33,7 @@ use cpal::{
use tokio::sync::watch;
use tracing::{error, info};
-use crate::audio::{AudioSegment, MusicTracks, PrerenderedAudioBuffer};
+use crate::audio::{AudioSegment, MusicTracks, PlaybackAudioCache, PrerenderedAudioBuffer};
use crate::preparing_audio::{
PreparingAudioBuffer, PreparingAudioOutputHandle, PreparingAudioOutputSnapshot,
PreparingAudioSources,
@@ -950,6 +950,7 @@ fn install_source<T: FromSampleBytes + cpal::FromSample<f32>>(
output_info: AudioInfo,
use_device_latency_hint: bool,
install_tx: &std_mpsc::Sender<SourceCommand<T>>,
+ cache: &PlaybackAudioCache,
) -> Result<(), String> {
let preparing_request = ack.preparing_request();
if preparing_request
@@ -1009,13 +1010,14 @@ fn install_source<T: FromSampleBytes + cpal::FromSample<f32>>(
let mut buffer = if matches!(ack, SourceAcknowledgement::Refresh(..)) {
PrerenderedAudioBuffer::<T>::bounded(segments, music, &project, output_info, start_playhead)
} else {
- PrerenderedAudioBuffer::<T>::new(
+ PrerenderedAudioBuffer::<T>::new_cached(
segments,
music,
&project,
output_info,
duration_secs,
start_playhead,
+ cache,
)
};
if !matches!(ack, SourceAcknowledgement::Refresh(..)) {
@@ -1134,6 +1136,7 @@ fn control_thread_headless(control_rx: std_mpsc::Receiver<ControlMsg>, mut tap:
let (source_tx, source_rx) = std_mpsc::channel::<SourceCommand<f32>>();
let stop = Arc::new(AtomicBool::new(false));
+ let cache = PlaybackAudioCache::default();
let pump = {
let stop = stop.clone();
@@ -1191,6 +1194,7 @@ fn control_thread_headless(control_rx: std_mpsc::Receiver<ControlMsg>, mut tap:
output_info,
false,
&source_tx,
+ &cache,
) {
error!(%error, "Could not update headless audio playback");
}
@@ -1210,6 +1214,7 @@ fn control_thread_headless(control_rx: std_mpsc::Receiver<ControlMsg>, mut tap:
output_info,
false,
&source_tx,
+ &cache,
) {
Ok(()) => ack_rx.recv_timeout(SOURCE_ACK_TIMEOUT).is_ok(),
Err(e) => {
@@ -1235,6 +1240,7 @@ fn control_thread_headless(control_rx: std_mpsc::Receiver<ControlMsg>, mut tap:
output_info,
false,
&source_tx,
+ &cache,
) {
error!("Failed to install preparing headless audio source: {error}");
request.complete(false);
@@ -1352,6 +1358,7 @@ fn control_thread_offline(
HEADLESS_SAMPLE_RATE,
HEADLESS_CHANNELS,
);
+ let cache = PlaybackAudioCache::default();
while let Ok(msg) = control_rx.recv() {
match msg {
ControlMsg::Refresh {
@@ -1367,6 +1374,7 @@ fn control_thread_offline(
output_info,
false,
&source_tx,
+ &cache,
) {
error!(%error, "Could not update offline audio playback");
}
@@ -1384,6 +1392,7 @@ fn control_thread_offline(
output_info,
false,
&source_tx,
+ &cache,
)
.inspect_err(|e| error!("Failed to install offline audio source: {e}"))
.is_ok();
@@ -1566,9 +1575,18 @@ where
.map_err(|e| format!("Failed to start audio output stream: {e}"))?;
let install_tx = source_tx.clone();
+ let cache = PlaybackAudioCache::default();
let install = Box::new(
move |spec: Box<PlaySpec>, generation: u64, ack: SourceAcknowledgement| {
- install_source::<T>(spec, generation, ack, output_info, true, &install_tx)
+ install_source::<T>(
+ spec,
+ generation,
+ ack,
+ output_info,
+ true,
+ &install_tx,
+ &cache,
+ )
},
);
@@ -1636,6 +1654,7 @@ mod tests {
output_info,
use_device_latency_hint,
&install_tx,
+ &PlaybackAudioCache::default(),
)
.unwrap();
let SourceCommand::Install(source) = install_rx.recv().unwrap() else {
diff --git a/crates/editor/src/audio_output/native_tests.rs b/crates/editor/src/audio_output/native_tests.rs
index d64b46ebc..3f7c2b7fd 100644
--- a/crates/editor/src/audio_output/native_tests.rs
+++ b/crates/editor/src/audio_output/native_tests.rs
@@ -310,6 +310,7 @@ async fn preparing_dropped_acknowledgement_wakes_and_cancels_without_waiting_for
AudioInfo::new_raw(AudioData::SAMPLE_FORMAT, 48_000, 2),
false,
&source_tx,
+ &crate::audio::PlaybackAudioCache::default(),
)
.unwrap();
drop(source_rx.recv().unwrap());
@@ -337,6 +338,7 @@ fn preparing_callback_silences_active_cancelled_source_without_a_remove_command(
AudioInfo::new_raw(AudioData::SAMPLE_FORMAT, 48_000, 2),
false,
&source_tx,
+ &crate::audio::PlaybackAudioCache::default(),
)
.unwrap();
let SourceCommand::Install(mut source) = source_rx.recv().unwrap() else {
@@ -366,6 +368,7 @@ fn preparing_expired_install_does_not_replace_an_active_ordinary_source() {
AudioInfo::new_raw(AudioData::SAMPLE_FORMAT, 48_000, 2),
false,
&source_tx,
+ &crate::audio::PlaybackAudioCache::default(),
)
.unwrap();
request.cancel();
diff --git a/crates/editor/src/completed_audio.rs b/crates/editor/src/completed_audio.rs
index 357581e94..c2e8c7886 100644
--- a/crates/editor/src/completed_audio.rs
+++ b/crates/editor/src/completed_audio.rs
@@ -187,6 +187,7 @@ pub(crate) mod tests {
start_time: Some(0.125),
device_id: Some("mic".into()),
gap_summary: None,
+ digital_silence_ms: None,
}),
system_audio: Some(AudioMeta {
path: format!("content/segments/segment-{index}/system_audio.ogg")
@@ -194,6 +195,7 @@ pub(crate) mod tests {
start_time: Some(-0.25),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
}),
cursor: None,
keyboard: None,
diff --git a/crates/editor/src/editor_instance.rs b/crates/editor/src/editor_instance.rs
index cecbe1db9..4a726bc66 100644
--- a/crates/editor/src/editor_instance.rs
+++ b/crates/editor/src/editor_instance.rs
@@ -8,8 +8,8 @@ use cap_project::{
};
use cap_rendering::{
PrecomputedCursorTimeline, ProjectRecordingsMeta, ProjectUniforms, RecordingSegmentDecoders,
- RenderVideoConstants, SegmentVideoPaths, SharedWgpuDevice, Video, ZoomTransformTimeline,
- get_duration, spring_mass_damper::SpringMassDamperSimulationConfig,
+ RenderVideoConstants, RendererLayers, SegmentVideoPaths, SharedWgpuDevice, Video,
+ ZoomTransformTimeline, get_duration, spring_mass_damper::SpringMassDamperSimulationConfig,
};
use std::{
collections::HashMap,
@@ -248,6 +248,7 @@ pub struct EditorInstance {
meta: RecordingMeta,
pub export_preview_active: AtomicBool,
pub export_active: AtomicBool,
+ pub preview_layers: std::sync::Mutex<Option<RendererLayers>>,
runtime_handle: tokio::runtime::Handle,
audio_output: Arc<crate::AudioOutput>,
}
@@ -655,6 +656,7 @@ impl EditorInstance {
playback_active_rx,
export_preview_active: AtomicBool::new(false),
export_active: AtomicBool::new(false),
+ preview_layers: std::sync::Mutex::new(None),
playback_epoch: AtomicU64::new(0),
runtime_handle: tokio::runtime::Handle::current(),
audio_output,
@@ -781,6 +783,7 @@ impl EditorInstance {
meta: self.meta.clone(),
export_preview_active: AtomicBool::new(false),
export_active: AtomicBool::new(false),
+ preview_layers: std::sync::Mutex::new(None),
runtime_handle: self.runtime_handle.clone(),
audio_output: self.audio_output.clone(),
});
diff --git a/crates/editor/src/export_audio.rs b/crates/editor/src/export_audio.rs
index 58973230d..b65ad5370 100644
--- a/crates/editor/src/export_audio.rs
+++ b/crates/editor/src/export_audio.rs
@@ -324,6 +324,17 @@ impl ExportAudioRenderer {
{
return false;
}
+ // Studio Sound re-reads up to VOICE_PREROLL_SAMPLES before each cut to warm up the
+ // enhancer. The streaming reader only moves forward, so a cut shorter than that
+ // within one clip has to use the in-memory export path instead. An exact split
+ // (nothing removed) keeps the enhancer contiguous and still streams.
+ let studio_sound =
+ project.audio.improve && !project.audio.mute && project.audio.mic_volume_db > -30.0;
+ let min_gap = if studio_sound {
+ (VOICE_PREROLL_SAMPLES + VOICE_WINDOW_PADDING_SAMPLES) as f64 / 48_000.0
+ } else {
+ 0.0
+ };
let mut ends = HashMap::new();
let mut duration = holds.iter().map(|(start, end)| end - start).sum::<f64>();
if !duration.is_finite() || duration * 48_000.0 >= isize::MAX as f64 / 2.0 {
@@ -344,7 +355,11 @@ impl ExportAudioRenderer {
return false;
}
if let Some(previous_end) = ends.insert(segment.recording_clip, segment.end)
- && (!timeline.transitions.is_empty() || segment.start < previous_end)
+ && (!timeline.transitions.is_empty()
+ || segment.start < previous_end
+ || (min_gap > 0.0
+ && segment.start > previous_end
+ && segment.start < previous_end + min_gap))
{
return false;
}
@@ -1481,6 +1496,42 @@ mod tests {
assert_bounded_sink_variants(22..26, &[1, 997, 4_800, 48_001]);
}
+ #[test]
+ fn eligibility_rejects_studio_sound_cuts_shorter_than_voice_preroll() {
+ let mut short_cut = project();
+ short_cut
+ .timeline
+ .as_mut()
+ .unwrap()
+ .segments
+ .push(segment(0, 0.34, 0.36));
+ assert!(ExportAudioRenderer::eligible_sources(&short_cut, 3));
+ short_cut.audio.improve = true;
+ assert!(!ExportAudioRenderer::eligible_sources(&short_cut, 3));
+ short_cut.audio.mute = true;
+ assert!(ExportAudioRenderer::eligible_sources(&short_cut, 3));
+
+ let mut long_cut = project();
+ long_cut.audio.improve = true;
+ long_cut
+ .timeline
+ .as_mut()
+ .unwrap()
+ .segments
+ .push(segment(0, 1.0, 1.5));
+ assert!(ExportAudioRenderer::eligible_sources(&long_cut, 3));
+
+ let mut exact_split = project();
+ exact_split.audio.improve = true;
+ exact_split
+ .timeline
+ .as_mut()
+ .unwrap()
+ .segments
+ .push(segment(0, 0.33, 0.9));
+ assert!(ExportAudioRenderer::eligible_sources(&exact_split, 3));
+ }
+
#[test]
fn studio_sound_transition_with_single_sample_requests_keeps_lookahead() {
assert_bounded_sink_variants([19], &[1]);
diff --git a/crates/editor/src/lib.rs b/crates/editor/src/lib.rs
index 16a618e21..afe97ba7b 100644
--- a/crates/editor/src/lib.rs
+++ b/crates/editor/src/lib.rs
@@ -14,7 +14,7 @@ mod segments;
mod telemetry;
mod thumbnail;
-pub use audio::{AudioRenderer, AudioSegment, MusicTracks};
+pub use audio::{AudioRenderer, AudioSegment, MusicTracks, release_playback_audio_caches};
#[cfg(feature = "test-hooks")]
pub use audio_output::OfflineAudioSink;
pub use audio_output::{
diff --git a/crates/enc-ffmpeg/src/audio/aac.rs b/crates/enc-ffmpeg/src/audio/aac.rs
index e6291de13..ce3a9a289 100644
--- a/crates/enc-ffmpeg/src/audio/aac.rs
+++ b/crates/enc-ffmpeg/src/audio/aac.rs
@@ -162,7 +162,7 @@ impl AACEncoder {
let Some(&rate) = rates
.iter()
.find(|r| **r >= input_config.rate())
- .or(rates.first())
+ .or(rates.last())
else {
return Err(AACEncoderError::RateNotSupported(input_config.rate()));
};
@@ -250,6 +250,10 @@ impl AACEncoder {
) -> Result<(), ffmpeg::Error> {
self.base.send_frame(frame, timestamp, sink)
}
+
+ pub fn flush_to(&mut self, sink: &mut impl PacketSink) -> Result<(), ffmpeg::Error> {
+ self.base.flush(sink)
+ }
}
impl AudioEncoder for AACEncoder {
@@ -275,6 +279,74 @@ mod tests {
use super::*;
use ffmpeg::ChannelLayout;
+ #[test]
+ fn sample_rate_selection_preserves_supported_rates_and_clamps_outliers() {
+ let directory = tempfile::tempdir().unwrap();
+ for (input_rate, expected_rate) in [
+ (4_000, 7_350),
+ (44_100, 44_100),
+ (45_000, 48_000),
+ (96_000, 96_000),
+ (192_000, 96_000),
+ ] {
+ let mut output =
+ format::output(&directory.path().join(format!("{input_rate}.mp4"))).unwrap();
+ let encoder = AACEncoder::init(
+ AudioInfo::new_raw(Sample::F32(Type::Packed), input_rate, 1),
+ &mut output,
+ )
+ .unwrap();
+ let stream = output.stream(encoder.stream_index()).unwrap();
+ let decoder = context::Context::from_parameters(stream.parameters())
+ .unwrap()
+ .decoder()
+ .audio()
+ .unwrap();
+ assert_eq!(decoder.rate(), expected_rate, "input rate {input_rate}");
+ }
+ }
+
+ #[test]
+ fn high_rate_input_keeps_high_frequency_audio_through_encode_decode() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("high-rate.mp4");
+ let mut output = format::output(&path).unwrap();
+ let mut encoder = AACEncoder::init(
+ AudioInfo::new_raw(Sample::F32(Type::Packed), 192_000, 1),
+ &mut output,
+ )
+ .unwrap();
+ output.write_header().unwrap();
+
+ let mut audio = frame::Audio::new(Sample::F32(Type::Packed), 192_000, ChannelLayout::MONO);
+ audio.set_rate(192_000);
+ audio.set_pts(Some(0));
+ for (index, sample) in audio.plane_mut::<f32>(0).iter_mut().enumerate() {
+ *sample = 0.5 * (std::f32::consts::TAU * 12_000.0 * index as f32 / 192_000.0).sin();
+ }
+ encoder
+ .send_frame(audio, Duration::MAX, &mut output)
+ .unwrap();
+ encoder.flush(&mut output).unwrap();
+ output.write_trailer().unwrap();
+ drop(encoder);
+ drop(output);
+
+ let (samples, track_end) = decode_first_channel(&path);
+ assert_eq!(track_end, 48_000);
+ assert!(samples.len() >= 96_000, "decoded {} samples", samples.len());
+ let mut sine = 0.0;
+ let mut cosine = 0.0;
+ let signal = &samples[9_600..86_400];
+ for (index, sample) in signal.iter().enumerate() {
+ let phase = std::f64::consts::TAU * 12_000.0 * index as f64 / 96_000.0;
+ sine += f64::from(*sample) * phase.sin();
+ cosine += f64::from(*sample) * phase.cos();
+ }
+ let amplitude = 2.0 * sine.hypot(cosine) / signal.len() as f64;
+ assert!(amplitude > 0.4, "12 kHz tone amplitude {amplitude}");
+ }
+
fn input_frame(start: i64, samples: usize) -> frame::Audio {
let mut audio =
frame::Audio::new(Sample::F32(Type::Packed), samples, ChannelLayout::STEREO);
diff --git a/crates/enc-ffmpeg/src/audio/opus.rs b/crates/enc-ffmpeg/src/audio/opus.rs
index 3bcfb974c..5182c2968 100644
--- a/crates/enc-ffmpeg/src/audio/opus.rs
+++ b/crates/enc-ffmpeg/src/audio/opus.rs
@@ -106,6 +106,19 @@ impl OpusEncoder {
pub fn flush(&mut self, output: &mut format::context::Output) -> Result<(), ffmpeg::Error> {
self.base.flush(output)
}
+
+ pub fn queue_frame_to(
+ &mut self,
+ frame: frame::Audio,
+ timestamp: Duration,
+ sink: &mut impl crate::PacketSink,
+ ) -> Result<(), ffmpeg::Error> {
+ self.base.send_frame(frame, timestamp, sink)
+ }
+
+ pub fn flush_to(&mut self, sink: &mut impl crate::PacketSink) -> Result<(), ffmpeg::Error> {
+ self.base.flush(sink)
+ }
}
fn select_output_rate(input_rate: i32, supported_rates: &[i32]) -> Option<i32> {
diff --git a/crates/enc-ffmpeg/src/mux/dash_audio.rs b/crates/enc-ffmpeg/src/mux/dash_audio.rs
index b3dfcc007..9fef83849 100644
--- a/crates/enc-ffmpeg/src/mux/dash_audio.rs
+++ b/crates/enc-ffmpeg/src/mux/dash_audio.rs
@@ -1,6 +1,9 @@
use super::fragment_metadata::read_fragment_metadata;
use crate::audio::aac::{AACEncoder, AACEncoderError};
-use crate::mux::segmented_stream::{SegmentCompletedEvent, SegmentMediaType};
+use crate::mux::segmented_stream::{
+ SegmentCompletedEvent, SegmentMediaType, SegmentSender, notify_segment,
+};
+use crate::mux::write_behind::{AUDIO_BUDGET_BYTES, MuxSink, MuxTarget};
use cap_media_info::AudioInfo;
use ffmpeg::{format, frame};
use serde::Serialize;
@@ -47,6 +50,12 @@ fn sync_file(path: &Path) {
pub struct DashAudioSegmentEncoder {
base_path: PathBuf,
encoder: AACEncoder,
+ mux: MuxSink<AudioSegmentMux>,
+ segment_tx: SegmentSender,
+}
+
+struct AudioSegmentMux {
+ base_path: PathBuf,
output: format::context::Output,
initial_padding: Duration,
@@ -56,10 +65,30 @@ pub struct DashAudioSegmentEncoder {
frames_since_segment_scan: u32,
- segment_tx: Option<std::sync::mpsc::Sender<SegmentCompletedEvent>>,
+ segment_tx: SegmentSender,
init_notified: bool,
}
+impl MuxTarget for AudioSegmentMux {
+ fn output(&self) -> &format::context::Output {
+ &self.output
+ }
+
+ fn output_mut(&mut self) -> &mut format::context::Output {
+ &mut self.output
+ }
+
+ fn packet_written(&mut self) {
+ self.try_notify_init_segment();
+
+ self.frames_since_segment_scan += 1;
+ if self.frames_since_segment_scan >= 10 {
+ self.frames_since_segment_scan = 0;
+ self.flush_pending_segments();
+ }
+ }
+}
+
#[derive(Debug, Clone)]
pub struct AudioSegmentInfo {
pub path: PathBuf,
@@ -197,33 +226,103 @@ impl DashAudioSegmentEncoder {
"Initialized DASH audio segment encoder (init.mp4 + m4s segments)"
);
- let instance = Self {
- base_path,
- encoder,
+ let mux = AudioSegmentMux {
+ base_path: base_path.clone(),
output,
initial_padding,
current_index: 1,
completed_segments: Vec::new(),
frames_since_segment_scan: 0,
- segment_tx: None,
+ segment_tx: SegmentSender::default(),
init_notified: false,
};
+ let segment_tx = mux.segment_tx.clone();
+
+ mux.write_in_progress_manifest();
+
+ Ok(Self {
+ base_path,
+ encoder,
+ mux: MuxSink::Direct(mux),
+ segment_tx,
+ })
+ }
- instance.write_in_progress_manifest();
+ /// Moves muxing, segment publication and manifest writes onto a
+ /// dedicated writer thread so a stalled output volume no longer blocks
+ /// the caller. Bytes on disk and their order are unchanged.
+ pub fn enable_write_behind(&mut self) -> std::io::Result<()> {
+ self.mux
+ .enable_write_behind("audio-segments", AUDIO_BUDGET_BYTES)
+ }
- Ok(instance)
+ pub fn is_write_behind(&self) -> bool {
+ self.mux.is_write_behind()
}
pub fn set_segment_callback(&mut self, tx: std::sync::mpsc::Sender<SegmentCompletedEvent>) {
- self.segment_tx = Some(tx);
- self.try_notify_init_segment();
+ *self
+ .segment_tx
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(tx);
+ if !self.mux.is_write_behind() {
+ self.mux
+ .with_target(AudioSegmentMux::try_notify_init_segment);
+ }
+ }
+
+ pub fn queue_frame(
+ &mut self,
+ frame: frame::Audio,
+ timestamp: Duration,
+ ) -> Result<(), QueueFrameError> {
+ self.encoder
+ .send_frame_to(frame, timestamp, &mut self.mux)?;
+
+ self.mux.frame_queued();
+
+ Ok(())
}
+ pub fn finish(&mut self) -> Result<(), FinishError> {
+ let encoder = &mut self.encoder;
+ self.mux
+ .finish(
+ |mux| encoder.flush_to(mux),
+ |mux: &mut AudioSegmentMux, flush_result| {
+ let flush_result = flush_result.inspect_err(|error| {
+ tracing::warn!(%error, "Audio encoder flush failed");
+ });
+ mux.finish(flush_result)
+ },
+ )
+ .map_err(std::io::Error::from)?
+ }
+
+ pub fn finish_with_timestamp(&mut self, _timestamp: Duration) -> Result<(), FinishError> {
+ self.finish()
+ }
+
+ pub fn completed_segments(&self) -> Vec<AudioSegmentInfo> {
+ self.mux
+ .with_target_ref(|mux| mux.completed_segments.clone())
+ }
+
+ pub fn base_path(&self) -> &Path {
+ &self.base_path
+ }
+
+ pub fn init_segment_path(&self) -> PathBuf {
+ self.base_path.join(INIT_SEGMENT_NAME)
+ }
+}
+
+impl AudioSegmentMux {
fn try_notify_init_segment(&mut self) {
if self.init_notified {
return;
}
- let init_path = self.init_segment_path();
+ let init_path = self.base_path.join(INIT_SEGMENT_NAME);
if let Ok(meta) = std::fs::metadata(&init_path)
&& meta.len() > 0
{
@@ -240,30 +339,7 @@ impl DashAudioSegmentEncoder {
}
fn notify_segment(&self, event: SegmentCompletedEvent) {
- if let Some(tx) = &self.segment_tx
- && let Err(e) = tx.send(event)
- {
- tracing::warn!("Failed to send audio segment completed event: {e}");
- }
- }
-
- pub fn queue_frame(
- &mut self,
- frame: frame::Audio,
- timestamp: Duration,
- ) -> Result<(), QueueFrameError> {
- self.encoder
- .send_frame(frame, timestamp, &mut self.output)?;
-
- self.try_notify_init_segment();
-
- self.frames_since_segment_scan += 1;
- if self.frames_since_segment_scan >= 10 {
- self.frames_since_segment_scan = 0;
- self.flush_pending_segments();
- }
-
- Ok(())
+ notify_segment(&self.segment_tx, event);
}
fn flush_pending_segments(&mut self) {
@@ -367,10 +443,7 @@ impl DashAudioSegmentEncoder {
}
}
- pub fn finish(&mut self) -> Result<(), FinishError> {
- let flush_result = self.encoder.flush(&mut self.output).inspect_err(|error| {
- tracing::warn!(%error, "Audio encoder flush failed");
- });
+ fn finish(&mut self, flush_result: Result<(), ffmpeg::Error>) -> Result<(), FinishError> {
let trailer_result = self.output.write_trailer().inspect_err(|error| {
tracing::warn!(%error, "Audio trailer publication failed");
});
@@ -394,10 +467,6 @@ impl DashAudioSegmentEncoder {
Ok(())
}
- pub fn finish_with_timestamp(&mut self, _timestamp: Duration) -> Result<(), FinishError> {
- self.finish()
- }
-
fn finalize_pending_tmp_files(&self) {
let Ok(entries) = std::fs::read_dir(&self.base_path) else {
return;
@@ -523,18 +592,6 @@ impl DashAudioSegmentEncoder {
let manifest_path = self.base_path.join("manifest.json");
atomic_write_json(&manifest_path, &manifest)
}
-
- pub fn completed_segments(&self) -> &[AudioSegmentInfo] {
- &self.completed_segments
- }
-
- pub fn base_path(&self) -> &Path {
- &self.base_path
- }
-
- pub fn init_segment_path(&self) -> PathBuf {
- self.base_path.join(INIT_SEGMENT_NAME)
- }
}
#[cfg(test)]
@@ -570,6 +627,62 @@ mod tests {
frame
}
+ fn record_audio_segments(base_path: &Path, write_behind: bool) -> Vec<(u32, u64, bool)> {
+ let mut encoder = DashAudioSegmentEncoder::init(
+ base_path.to_path_buf(),
+ test_audio_info(),
+ DashAudioSegmentEncoderConfig::default(),
+ )
+ .unwrap();
+ if write_behind {
+ encoder.enable_write_behind().unwrap();
+ }
+ let (tx, rx) = mpsc::channel();
+ encoder.set_segment_callback(tx);
+ for i in 0..500u64 {
+ encoder
+ .queue_frame(
+ create_test_audio_frame(1024, i * 1024),
+ Duration::from_secs_f64(i as f64 * 1024.0 / 48_000.0),
+ )
+ .unwrap();
+ }
+ encoder.finish().unwrap();
+ drop(encoder);
+ rx.try_iter()
+ .map(|event| (event.index, event.file_size, event.is_init))
+ .collect()
+ }
+
+ #[test]
+ fn write_behind_publishes_the_same_audio_segments_as_inline_muxing() {
+ ffmpeg::init().unwrap();
+ let temp = tempfile::tempdir().unwrap();
+ let inline = temp.path().join("inline");
+ let behind = temp.path().join("behind");
+
+ let inline_events = record_audio_segments(&inline, false);
+ let behind_events = record_audio_segments(&behind, true);
+
+ assert!(inline_events.len() >= 3);
+ assert_eq!(inline_events, behind_events);
+ let files = |path: &Path| {
+ let mut files: Vec<(String, Vec<u8>)> = std::fs::read_dir(path)
+ .unwrap()
+ .flatten()
+ .map(|entry| {
+ (
+ entry.file_name().to_string_lossy().into_owned(),
+ std::fs::read(entry.path()).unwrap(),
+ )
+ })
+ .collect();
+ files.sort();
+ files
+ };
+ assert_eq!(files(&inline), files(&behind));
+ }
+
#[test]
fn audio_manifest_and_events_match_muxer_timeline() {
ffmpeg::init().unwrap();
diff --git a/crates/enc-ffmpeg/src/mux/fragmented_audio.rs b/crates/enc-ffmpeg/src/mux/fragmented_audio.rs
index cf865c77c..c5e9d8db7 100644
--- a/crates/enc-ffmpeg/src/mux/fragmented_audio.rs
+++ b/crates/enc-ffmpeg/src/mux/fragmented_audio.rs
@@ -2,15 +2,28 @@ use cap_media_info::AudioInfo;
use ffmpeg::{format, frame};
use std::{path::PathBuf, time::Duration};
+use super::write_behind::{AUDIO_BUDGET_BYTES, MuxSink, MuxTarget};
use crate::audio::aac::{AACEncoder, AACEncoderError};
pub struct FragmentedAudioFile {
encoder: AACEncoder,
- output: format::context::Output,
+ output: MuxSink<AudioOutput>,
finished: bool,
has_frames: bool,
}
+struct AudioOutput(format::context::Output);
+
+impl MuxTarget for AudioOutput {
+ fn output(&self) -> &format::context::Output {
+ &self.0
+ }
+
+ fn output_mut(&mut self) -> &mut format::context::Output {
+ &mut self.0
+ }
+}
+
#[derive(thiserror::Error, Debug)]
pub enum InitError {
#[error("FFmpeg: {0}")]
@@ -103,12 +116,19 @@ impl FragmentedAudioFile {
Ok(Self {
encoder,
- output,
+ output: MuxSink::Direct(AudioOutput(output)),
finished: false,
has_frames: false,
})
}
+ /// Hands encoded packets to a dedicated writer thread so a stalled
+ /// output volume no longer blocks the caller (an async runtime worker in
+ /// the recording pipeline). Bytes on disk and their order are unchanged.
+ pub fn enable_write_behind(&mut self) -> std::io::Result<()> {
+ self.output.enable_write_behind("audio", AUDIO_BUDGET_BYTES)
+ }
+
pub fn encoder(&self) -> &AACEncoder {
&self.encoder
}
@@ -118,8 +138,16 @@ impl FragmentedAudioFile {
frame: frame::Audio,
timestamp: Duration,
) -> Result<(), ffmpeg::Error> {
+ if self.finished && self.output.is_write_behind() {
+ return Err(ffmpeg::Error::Exit);
+ }
self.has_frames = true;
- self.encoder.send_frame(frame, timestamp, &mut self.output)
+ self.encoder
+ .send_frame_to(frame, timestamp, &mut self.output)
+ }
+
+ pub fn is_write_behind(&self) -> bool {
+ self.output.is_write_behind()
}
pub fn finish(&mut self) -> Result<Result<(), ffmpeg::Error>, FinishError> {
@@ -136,12 +164,21 @@ impl FragmentedAudioFile {
self.finished = true;
- let flush_result = if self.has_frames {
- self.encoder.flush(&mut self.output)
- } else {
- Ok(())
- };
- let trailer_result = self.output.write_trailer();
+ let has_frames = self.has_frames;
+ let encoder = &mut self.encoder;
+ let (flush_result, trailer_result) = self
+ .output
+ .finish(
+ |output| {
+ if has_frames {
+ encoder.flush_to(output)
+ } else {
+ Ok(())
+ }
+ },
+ |output: &mut AudioOutput, flush_result| (flush_result, output.0.write_trailer()),
+ )
+ .map_err(|stalled| FinishError::WriteTrailerFailed(stalled.into()))?;
finish_result(flush_result, trailer_result)
}
}
@@ -242,6 +279,48 @@ mod publication_tests {
output.finish().unwrap().unwrap();
}
+ #[test]
+ fn write_behind_audio_matches_inline_bytes() {
+ let directory = tempfile::tempdir().unwrap();
+ let info = AudioInfo::new_raw(Sample::F32(Type::Packed), 48_000, 1);
+ let inline_path = directory.path().join("inline.m4a");
+ let mut inline = FragmentedAudioFile::init(inline_path.clone(), info).unwrap();
+ queue_tone(&mut inline, 300);
+ inline.finish().unwrap().unwrap();
+
+ let behind_path = directory.path().join("behind.m4a");
+ let mut behind = FragmentedAudioFile::init(behind_path.clone(), info).unwrap();
+ behind.enable_write_behind().unwrap();
+ queue_tone(&mut behind, 300);
+ behind.output.drain();
+ let mut input = format::input(&behind_path).unwrap();
+ assert!(input.packets().next().is_some());
+ drop(input);
+ behind.finish().unwrap().unwrap();
+
+ assert_eq!(
+ std::fs::read(&inline_path).unwrap(),
+ std::fs::read(&behind_path).unwrap()
+ );
+ }
+
+ #[test]
+ fn write_behind_audio_rejects_frames_after_finish() {
+ let directory = tempfile::tempdir().unwrap();
+ let info = AudioInfo::new_raw(Sample::F32(Type::Packed), 48_000, 1);
+ let mut output =
+ FragmentedAudioFile::init(directory.path().join("finished.m4a"), info).unwrap();
+ output.enable_write_behind().unwrap();
+ queue_tone(&mut output, 10);
+ output.finish().unwrap().unwrap();
+ let frame =
+ ffmpeg::frame::Audio::new(Sample::F32(Type::Packed), 1024, ffmpeg::ChannelLayout::MONO);
+ assert_eq!(
+ output.queue_frame(frame, Duration::from_secs(1)),
+ Err(ffmpeg::Error::Exit)
+ );
+ }
+
#[test]
fn short_fragmented_audio_remains_readable_after_finish() {
let directory = tempfile::tempdir().unwrap();
@@ -306,7 +385,7 @@ mod publication_tests {
fn set_output_error(output: &mut FragmentedAudioFile) {
unsafe {
- let context = output.output.as_mut_ptr();
+ let context = output.output.with_target(|output| output.0.as_mut_ptr());
assert!(!(*context).pb.is_null());
(*(*context).pb).error = ffmpeg::Error::Other {
errno: ffmpeg::error::EIO,
diff --git a/crates/enc-ffmpeg/src/mux/mod.rs b/crates/enc-ffmpeg/src/mux/mod.rs
index c31ea535e..67a71c343 100644
--- a/crates/enc-ffmpeg/src/mux/mod.rs
+++ b/crates/enc-ffmpeg/src/mux/mod.rs
@@ -10,3 +10,4 @@ pub mod mp4;
pub mod ogg;
pub mod segmented_audio;
pub mod segmented_stream;
+pub(crate) mod write_behind;
diff --git a/crates/enc-ffmpeg/src/mux/ogg.rs b/crates/enc-ffmpeg/src/mux/ogg.rs
index c8d700c66..e5649d6c8 100644
--- a/crates/enc-ffmpeg/src/mux/ogg.rs
+++ b/crates/enc-ffmpeg/src/mux/ogg.rs
@@ -1,14 +1,27 @@
use ffmpeg::{format, frame};
use std::{path::PathBuf, time::Duration};
+use super::write_behind::{AUDIO_BUDGET_BYTES, MuxSink, MuxTarget};
use crate::audio::opus::{OpusEncoder, OpusEncoderError};
pub struct OggFile {
encoder: OpusEncoder,
- output: format::context::Output,
+ output: MuxSink<OggOutput>,
finished: bool,
}
+struct OggOutput(format::context::Output);
+
+impl MuxTarget for OggOutput {
+ fn output(&self) -> &format::context::Output {
+ &self.0
+ }
+
+ fn output_mut(&mut self) -> &mut format::context::Output {
+ &mut self.0
+ }
+}
+
#[derive(thiserror::Error, Debug)]
pub enum FinishError {
#[error("Already finished")]
@@ -32,11 +45,18 @@ impl OggFile {
Ok(Self {
encoder,
- output,
+ output: MuxSink::Direct(OggOutput(output)),
finished: false,
})
}
+ /// Hands encoded packets to a dedicated writer thread so a stalled
+ /// output volume no longer blocks the caller. Bytes on disk and their
+ /// order are unchanged.
+ pub fn enable_write_behind(&mut self) -> std::io::Result<()> {
+ self.output.enable_write_behind("ogg", AUDIO_BUDGET_BYTES)
+ }
+
pub fn encoder(&self) -> &OpusEncoder {
&self.encoder
}
@@ -46,7 +66,15 @@ impl OggFile {
frame: frame::Audio,
timestamp: Duration,
) -> Result<(), ffmpeg::Error> {
- self.encoder.queue_frame(frame, timestamp, &mut self.output)
+ if self.finished && self.output.is_write_behind() {
+ return Err(ffmpeg::Error::Exit);
+ }
+ self.encoder
+ .queue_frame_to(frame, timestamp, &mut self.output)
+ }
+
+ pub fn is_write_behind(&self) -> bool {
+ self.output.is_write_behind()
}
pub fn finish(&mut self) -> Result<Result<(), ffmpeg::Error>, FinishError> {
@@ -56,10 +84,15 @@ impl OggFile {
self.finished = true;
- let flush_result = self.encoder.flush(&mut self.output);
- self.output
- .write_trailer()
- .map_err(FinishError::WriteTrailerFailed)?;
+ let encoder = &mut self.encoder;
+ let (flush_result, trailer_result) = self
+ .output
+ .finish(
+ |output| encoder.flush_to(output),
+ |output: &mut OggOutput, flush_result| (flush_result, output.0.write_trailer()),
+ )
+ .map_err(|stalled| FinishError::WriteTrailerFailed(stalled.into()))?;
+ trailer_result.map_err(FinishError::WriteTrailerFailed)?;
Ok(flush_result)
}
@@ -79,6 +112,15 @@ mod tests {
use ffmpeg::ChannelLayout;
fn write_recording(path: std::path::PathBuf, input_rate: u32, total_samples: usize) {
+ write_recording_with(path, input_rate, total_samples, false);
+ }
+
+ fn write_recording_with(
+ path: std::path::PathBuf,
+ input_rate: u32,
+ total_samples: usize,
+ write_behind: bool,
+ ) {
let info = AudioInfo::new_raw(
format::Sample::F32(format::sample::Type::Packed),
input_rate,
@@ -86,6 +128,9 @@ mod tests {
);
let mut file = OggFile::init(path, OpusEncoder::factory(info)).unwrap();
+ if write_behind {
+ file.enable_write_behind().unwrap();
+ }
let chunk = 1024usize;
let mut written = 0usize;
@@ -142,6 +187,22 @@ mod tests {
);
}
+ #[test]
+ fn write_behind_output_has_the_same_duration_and_size() {
+ let dir = tempfile::tempdir().unwrap();
+ let inline = dir.path().join("inline.ogg");
+ let behind = dir.path().join("behind.ogg");
+
+ write_recording_with(inline.clone(), 48_000, 48_000 * 5, false);
+ write_recording_with(behind.clone(), 48_000, 48_000 * 5, true);
+
+ assert_eq!(
+ std::fs::metadata(&inline).unwrap().len(),
+ std::fs::metadata(&behind).unwrap().len()
+ );
+ assert!((probed_duration_secs(&behind) - probed_duration_secs(&inline)).abs() < 1e-6);
+ }
+
#[test]
fn sub_frame_input_is_flushed_not_dropped() {
// Less input than one opus frame (20ms): finish() must drain the
diff --git a/crates/enc-ffmpeg/src/mux/segmented_stream.rs b/crates/enc-ffmpeg/src/mux/segmented_stream.rs
index d252a921c..0ea15ad15 100644
--- a/crates/enc-ffmpeg/src/mux/segmented_stream.rs
+++ b/crates/enc-ffmpeg/src/mux/segmented_stream.rs
@@ -1,4 +1,5 @@
use super::fragment_metadata::read_fragment_metadata;
+use super::write_behind::{MuxSink, MuxTarget};
use cap_media_info::VideoInfo;
use ffmpeg::{format, frame};
use serde::Serialize;
@@ -26,6 +27,22 @@ pub struct DiskSpaceWarning {
pub type DiskSpaceCallback = Arc<dyn Fn(DiskSpaceWarning) + Send + Sync>;
+/// Shared so a write-behind muxer can install the callback without waiting
+/// for the writer thread, which may be blocked on storage.
+pub(crate) type SegmentSender =
+ Arc<std::sync::Mutex<Option<std::sync::mpsc::Sender<SegmentCompletedEvent>>>>;
+
+pub(crate) fn notify_segment(sender: &SegmentSender, event: SegmentCompletedEvent) {
+ let sender = sender
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ if let Some(tx) = sender.as_ref()
+ && let Err(e) = tx.send(event)
+ {
+ tracing::warn!("Failed to send segment completed event: {e}");
+ }
+}
+
fn atomic_write_json<T: Serialize>(path: &Path, data: &T) -> std::io::Result<()> {
let temp_path = path.with_extension("json.tmp");
let json = serde_json::to_string_pretty(data)
@@ -76,10 +93,19 @@ pub struct SegmentedVideoEncoder {
base_path: PathBuf,
encoder: H264Encoder,
+ mux: MuxSink<SegmentMux>,
+ segment_tx: SegmentSender,
+
+ segment_duration: Duration,
+
+ disk_space_callback: Option<DiskSpaceCallback>,
+}
+
+struct SegmentMux {
+ base_path: PathBuf,
output: format::context::Output,
current_index: u32,
- segment_duration: Duration,
completed_segments: Vec<VideoSegmentInfo>,
@@ -87,9 +113,36 @@ pub struct SegmentedVideoEncoder {
codec_info: CodecInfo,
- disk_space_callback: Option<DiskSpaceCallback>,
- segment_tx: Option<std::sync::mpsc::Sender<SegmentCompletedEvent>>,
+ segment_tx: SegmentSender,
init_notified: bool,
+ initial_manifest_pending: bool,
+}
+
+impl MuxTarget for SegmentMux {
+ fn output(&self) -> &format::context::Output {
+ &self.output
+ }
+
+ fn output_mut(&mut self) -> &mut format::context::Output {
+ &mut self.output
+ }
+
+ fn writer_started(&mut self) {
+ self.write_initial_manifest();
+ }
+
+ fn packet_written(&mut self) {
+ // The encoder holds one packet back to stamp real durations, so the
+ // init segment materializes a frame later than the first queue call;
+ // keep trying until it lands (no-op once notified).
+ self.try_notify_init_segment();
+
+ self.frames_since_segment_scan += 1;
+ if self.frames_since_segment_scan >= 10 {
+ self.frames_since_segment_scan = 0;
+ self.flush_pending_segments();
+ }
+ }
}
#[derive(Debug, Clone)]
@@ -198,7 +251,7 @@ impl SegmentedVideoEncoder {
video_config: VideoInfo,
config: SegmentedVideoEncoderConfig,
) -> Result<Self, InitError> {
- Self::init_with_timed_fragments(base_path, video_config, config, false)
+ Self::init_with_options(base_path, video_config, config, false, false)
}
pub fn init_with_timed_fragments(
@@ -206,6 +259,31 @@ impl SegmentedVideoEncoder {
video_config: VideoInfo,
config: SegmentedVideoEncoderConfig,
timed_fragments: bool,
+ ) -> Result<Self, InitError> {
+ Self::init_with_options(base_path, video_config, config, timed_fragments, false)
+ }
+
+ /// Like [`Self::init_with_timed_fragments`], but muxing, segment
+ /// publication and manifest writes (including the initial manifest) run on
+ /// a dedicated writer thread with a memory budget sized from the stream
+ /// bitrate, so storage stalls stop blocking the encoder. Output bytes and
+ /// their order are unchanged. Falls back to inline muxing if the thread
+ /// cannot start.
+ pub fn init_write_behind(
+ base_path: PathBuf,
+ video_config: VideoInfo,
+ config: SegmentedVideoEncoderConfig,
+ timed_fragments: bool,
+ ) -> Result<Self, InitError> {
+ Self::init_with_options(base_path, video_config, config, timed_fragments, true)
+ }
+
+ fn init_with_options(
+ base_path: PathBuf,
+ video_config: VideoInfo,
+ config: SegmentedVideoEncoderConfig,
+ timed_fragments: bool,
+ write_behind: bool,
) -> Result<Self, InitError> {
std::fs::create_dir_all(&base_path)?;
@@ -309,21 +387,40 @@ impl SegmentedVideoEncoder {
"Initialized segmented video encoder with FFmpeg DASH muxer (init.mp4 + m4s segments)"
);
- let instance = Self {
- base_path,
- encoder,
+ let mux = SegmentMux {
+ base_path: base_path.clone(),
output,
current_index: 1,
- segment_duration: config.segment_duration,
completed_segments: Vec::new(),
frames_since_segment_scan: 0,
codec_info,
- disk_space_callback: None,
- segment_tx: None,
+ segment_tx: SegmentSender::default(),
init_notified: false,
+ initial_manifest_pending: true,
};
+ let segment_tx = mux.segment_tx.clone();
- instance.write_in_progress_manifest();
+ let mut instance = Self {
+ base_path,
+ encoder,
+ mux: MuxSink::Direct(mux),
+ segment_tx,
+ segment_duration: config.segment_duration,
+ disk_space_callback: None,
+ };
+
+ if write_behind {
+ let budget = super::write_behind::video_budget_bytes(instance.encoder.bit_rate());
+ match instance.mux.enable_write_behind("video", budget) {
+ Ok(()) => tracing::debug!(budget_bytes = budget, "Video write-behind enabled"),
+ Err(error) => {
+ tracing::warn!(%error, "Video write-behind unavailable, muxing inline");
+ }
+ }
+ }
+ if !instance.mux.is_write_behind() {
+ instance.mux.with_target(SegmentMux::write_initial_manifest);
+ }
Ok(instance)
}
@@ -333,27 +430,12 @@ impl SegmentedVideoEncoder {
}
pub fn set_segment_callback(&mut self, tx: std::sync::mpsc::Sender<SegmentCompletedEvent>) {
- self.segment_tx = Some(tx);
- self.try_notify_init_segment();
- }
-
- fn try_notify_init_segment(&mut self) {
- if self.init_notified {
- return;
- }
- let init_path = self.init_segment_path();
- if let Ok(meta) = std::fs::metadata(&init_path)
- && meta.len() > 0
- {
- self.init_notified = true;
- self.notify_segment(SegmentCompletedEvent {
- path: init_path,
- index: 0,
- duration: 0.0,
- file_size: meta.len(),
- is_init: true,
- media_type: SegmentMediaType::Video,
- });
+ *self
+ .segment_tx
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(tx);
+ if !self.mux.is_write_behind() {
+ self.mux.with_target(SegmentMux::try_notify_init_segment);
}
}
@@ -391,30 +473,123 @@ impl SegmentedVideoEncoder {
// stream restarts, dropped frames) stay in the timeline instead of
// compressing it and drifting video ahead of audio.
self.encoder
- .queue_frame(frame, timestamp, &mut self.output)?;
+ .queue_frame_to(frame, timestamp, &mut self.mux)?;
- // The encoder holds one packet back to stamp real durations, so the
- // init segment materializes a frame later than the first queue call;
- // keep trying until it lands (no-op once notified).
- self.try_notify_init_segment();
+ self.mux.frame_queued();
- self.frames_since_segment_scan += 1;
- if self.frames_since_segment_scan >= 10 {
- self.frames_since_segment_scan = 0;
- self.flush_pending_segments();
+ Ok(())
+ }
+
+ pub fn finish(&mut self) -> Result<(), FinishError> {
+ let encoder = &mut self.encoder;
+ self.mux
+ .finish(
+ |mux| encoder.flush_to(mux),
+ |mux: &mut SegmentMux, flush_result| {
+ let flush_result = flush_result.inspect_err(|error| {
+ tracing::warn!(%error, "Video encoder flush failed");
+ });
+ mux.finish(flush_result)
+ },
+ )
+ .map_err(std::io::Error::from)?
+ }
+
+ pub fn finish_with_timestamp(&mut self, _timestamp: Duration) -> Result<(), FinishError> {
+ self.finish()
+ }
+
+ pub fn completed_segments(&self) -> Vec<VideoSegmentInfo> {
+ self.mux
+ .with_target_ref(|mux| mux.completed_segments.clone())
+ }
+
+ pub fn current_encoder(&self) -> Option<&H264Encoder> {
+ Some(&self.encoder)
+ }
+
+ pub fn current_encoder_mut(&mut self) -> Option<&mut H264Encoder> {
+ Some(&mut self.encoder)
+ }
+
+ pub fn base_path(&self) -> &Path {
+ &self.base_path
+ }
+
+ pub fn segment_duration(&self) -> Duration {
+ self.segment_duration
+ }
+
+ pub fn current_index(&self) -> u32 {
+ self.mux.with_target_ref(|mux| mux.current_index)
+ }
+
+ pub fn init_segment_path(&self) -> PathBuf {
+ self.base_path.join(INIT_SEGMENT_NAME)
+ }
+
+ pub fn validate_init_segment(&self) -> Result<(), String> {
+ let init_path = self.init_segment_path();
+
+ if !init_path.exists() {
+ return Err(format!(
+ "init.mp4 is missing at {}. M4S segments will be unplayable without it.",
+ init_path.display()
+ ));
}
- Ok(())
+ match std::fs::metadata(&init_path) {
+ Ok(metadata) => {
+ let size = metadata.len();
+ if size < 100 {
+ return Err(format!(
+ "init.mp4 at {} is too small ({} bytes). It may be corrupted.",
+ init_path.display(),
+ size
+ ));
+ }
+ Ok(())
+ }
+ Err(e) => Err(format!(
+ "Cannot read init.mp4 metadata at {}: {}",
+ init_path.display(),
+ e
+ )),
+ }
+ }
+}
+
+impl SegmentMux {
+ fn write_initial_manifest(&mut self) {
+ if std::mem::take(&mut self.initial_manifest_pending) {
+ self.write_in_progress_manifest();
+ }
}
- fn notify_segment(&self, event: SegmentCompletedEvent) {
- if let Some(tx) = &self.segment_tx
- && let Err(e) = tx.send(event)
+ fn try_notify_init_segment(&mut self) {
+ if self.init_notified {
+ return;
+ }
+ let init_path = self.base_path.join(INIT_SEGMENT_NAME);
+ if let Ok(meta) = std::fs::metadata(&init_path)
+ && meta.len() > 0
{
- tracing::warn!("Failed to send segment completed event: {e}");
+ self.init_notified = true;
+ self.notify_segment(SegmentCompletedEvent {
+ path: init_path,
+ index: 0,
+ duration: 0.0,
+ file_size: meta.len(),
+ is_init: true,
+ media_type: SegmentMediaType::Video,
+ });
}
}
+ fn notify_segment(&self, event: SegmentCompletedEvent) {
+ notify_segment(&self.segment_tx, event);
+ }
+
fn flush_pending_segments(&mut self) {
let first_index = self.current_index;
loop {
@@ -507,10 +682,7 @@ impl SegmentedVideoEncoder {
}
}
- pub fn finish(&mut self) -> Result<(), FinishError> {
- let flush_result = self.encoder.flush(&mut self.output).inspect_err(|error| {
- tracing::warn!(%error, "Video encoder flush failed");
- });
+ fn finish(&mut self, flush_result: Result<(), ffmpeg::Error>) -> Result<(), FinishError> {
let trailer_result = self.output.write_trailer().inspect_err(|error| {
tracing::warn!(%error, "Video trailer publication failed");
});
@@ -534,10 +706,6 @@ impl SegmentedVideoEncoder {
Ok(())
}
- pub fn finish_with_timestamp(&mut self, _timestamp: Duration) -> Result<(), FinishError> {
- self.finish()
- }
-
fn finalize_pending_tmp_files(&self) {
let Ok(entries) = std::fs::read_dir(&self.base_path) else {
return;
@@ -702,64 +870,6 @@ impl SegmentedVideoEncoder {
let manifest_path = self.base_path.join("manifest.json");
atomic_write_json(&manifest_path, &manifest)
}
-
- pub fn completed_segments(&self) -> &[VideoSegmentInfo] {
- &self.completed_segments
- }
-
- pub fn current_encoder(&self) -> Option<&H264Encoder> {
- Some(&self.encoder)
- }
-
- pub fn current_encoder_mut(&mut self) -> Option<&mut H264Encoder> {
- Some(&mut self.encoder)
- }
-
- pub fn base_path(&self) -> &Path {
- &self.base_path
- }
-
- pub fn segment_duration(&self) -> Duration {
- self.segment_duration
- }
-
- pub fn current_index(&self) -> u32 {
- self.current_index
- }
-
- pub fn init_segment_path(&self) -> PathBuf {
- self.base_path.join(INIT_SEGMENT_NAME)
- }
-
- pub fn validate_init_segment(&self) -> Result<(), String> {
- let init_path = self.init_segment_path();
-
- if !init_path.exists() {
- return Err(format!(
- "init.mp4 is missing at {}. M4S segments will be unplayable without it.",
- init_path.display()
- ));
- }
-
- match std::fs::metadata(&init_path) {
- Ok(metadata) => {
- let size = metadata.len();
- if size < 100 {
- return Err(format!(
- "init.mp4 at {} is too small ({} bytes). It may be corrupted.",
- init_path.display(),
- size
- ));
- }
- Ok(())
- }
- Err(e) => Err(format!(
- "Cannot read init.mp4 metadata at {}: {}",
- init_path.display(),
- e
- )),
- }
- }
}
#[cfg(test)]
@@ -852,6 +962,128 @@ mod tests {
);
}
+ fn record_segments(base_path: &Path, write_behind: bool) -> Vec<(u32, bool, u64)> {
+ let mut encoder = if write_behind {
+ SegmentedVideoEncoder::init_write_behind(
+ base_path.to_path_buf(),
+ test_video_info(),
+ SegmentedVideoEncoderConfig::default(),
+ false,
+ )
+ } else {
+ SegmentedVideoEncoder::init(
+ base_path.to_path_buf(),
+ test_video_info(),
+ SegmentedVideoEncoderConfig::default(),
+ )
+ }
+ .unwrap();
+ assert_eq!(encoder.mux.is_write_behind(), write_behind);
+ let (tx, rx) = mpsc::channel();
+ encoder.set_segment_callback(tx);
+ for i in 0..200 {
+ encoder
+ .queue_frame(
+ create_test_frame(320, 240),
+ Duration::from_micros(i * 33_320),
+ )
+ .unwrap();
+ }
+ encoder
+ .finish_with_timestamp(Duration::from_secs(7))
+ .unwrap();
+ drop(encoder);
+ rx.try_iter()
+ .map(|event| {
+ assert_eq!(
+ event.file_size,
+ std::fs::metadata(&event.path).unwrap().len()
+ );
+ (event.index, event.is_init, (event.duration * 1e6) as u64)
+ })
+ .collect()
+ }
+
+ fn published_index(base_path: &Path) -> (Vec<String>, String, serde_json::Value) {
+ let mut names: Vec<String> = std::fs::read_dir(base_path)
+ .unwrap()
+ .flatten()
+ .map(|entry| entry.file_name().to_string_lossy().into_owned())
+ .collect();
+ names.sort();
+ let mpd = std::fs::read_to_string(base_path.join("dash_manifest.mpd")).unwrap();
+ let mut manifest: serde_json::Value =
+ serde_json::from_slice(&std::fs::read(base_path.join("manifest.json")).unwrap())
+ .unwrap();
+ for segment in manifest["segments"].as_array_mut().unwrap() {
+ segment.as_object_mut().unwrap().remove("file_size");
+ }
+ (names, mpd, manifest)
+ }
+
+ #[test]
+ fn write_behind_publishes_the_initial_manifest_from_the_writer_thread() {
+ ffmpeg::init().unwrap();
+ let temp = tempfile::tempdir().unwrap();
+ let encoder = SegmentedVideoEncoder::init_write_behind(
+ temp.path().to_path_buf(),
+ test_video_info(),
+ SegmentedVideoEncoderConfig::default(),
+ false,
+ )
+ .unwrap();
+ let manifest_path = temp.path().join("manifest.json");
+ let deadline = std::time::Instant::now() + Duration::from_secs(2);
+ while !manifest_path.exists() && std::time::Instant::now() < deadline {
+ std::thread::sleep(Duration::from_millis(5));
+ }
+ let manifest: serde_json::Value =
+ serde_json::from_slice(&std::fs::read(&manifest_path).unwrap()).unwrap();
+ assert_eq!(manifest["is_complete"], false);
+ assert_eq!(manifest["segments"][0]["index"], 1);
+ drop(encoder);
+ }
+
+ #[test]
+ fn write_behind_segment_callback_does_not_wait_for_stalled_storage() {
+ ffmpeg::init().unwrap();
+ let temp = tempfile::tempdir().unwrap();
+ let mut encoder = SegmentedVideoEncoder::init_write_behind(
+ temp.path().to_path_buf(),
+ test_video_info(),
+ SegmentedVideoEncoderConfig::default(),
+ false,
+ )
+ .unwrap();
+ encoder.mux.drain();
+ let stall = encoder.mux.stall_storage_for(Duration::from_millis(800));
+ let started = std::time::Instant::now();
+ let (tx, _rx) = mpsc::channel();
+ encoder.set_segment_callback(tx);
+ encoder
+ .queue_frame(create_test_frame(320, 240), Duration::ZERO)
+ .unwrap();
+ assert!(started.elapsed() < Duration::from_millis(400));
+ stall.join().unwrap();
+ }
+
+ #[test]
+ fn write_behind_publishes_the_same_segments_as_inline_muxing() {
+ ffmpeg::init().unwrap();
+ let temp = tempfile::tempdir().unwrap();
+ let inline = temp.path().join("inline");
+ let behind = temp.path().join("behind");
+
+ let inline_events = record_segments(&inline, false);
+ let behind_events = record_segments(&behind, true);
+
+ // VideoToolbox payload sizes are not reproducible across sessions, so
+ // the published timeline is compared rather than media bytes.
+ assert!(inline_events.len() >= 3);
+ assert_eq!(inline_events, behind_events);
+ assert_eq!(published_index(&inline), published_index(&behind));
+ }
+
#[test]
fn failed_dash_manifest_publication_retains_video_without_claiming_completion() {
ffmpeg::init().unwrap();
diff --git a/crates/enc-ffmpeg/src/mux/write_behind.rs b/crates/enc-ffmpeg/src/mux/write_behind.rs
new file mode 100644
index 000000000..d0155f1c4
--- /dev/null
+++ b/crates/enc-ffmpeg/src/mux/write_behind.rs
@@ -0,0 +1,1005 @@
+//! Write-behind muxing: encoders hand finished packets to a dedicated writer
+//! thread that owns the `AVFormatContext`, so a stalled output volume (slow
+//! SD card, external or network drive) blocks only that thread instead of
+//! the encoder or the async runtime driving it.
+//!
+//! Packets are written in exactly the order they were produced, so the
+//! on-disk bytes are identical to writing inline. Memory is bounded by a byte
+//! budget; once it is full the producer blocks until the writer catches up,
+//! which is the inline behaviour.
+//!
+//! Finishing is one bounded operation: the writer drains the queue, then
+//! runs the finalization (trailer, final manifest) itself. When storage is
+//! still accepting writes after [`FINISH_DRAIN_LIMIT`], the packets still
+//! queued are discarded so finalization can start. If finalization has not
+//! completed within [`FINISH_LIMIT`], finishing
+//! returns [`StorageStalled`]: finalization is cancelled, the files keep the
+//! state recovery expects from an interrupted recording, and the packets
+//! still queued reach disk once storage recovers, as they would from an
+//! encoder blocked in an inline write. Dropping a writer that was never
+//! finished discards its queue and touches nothing after its current call.
+
+use crate::base::PacketSink;
+use ffmpeg::{Packet, Rational, format};
+use std::{
+ collections::VecDeque,
+ sync::{
+ Arc, Condvar, Mutex, MutexGuard,
+ atomic::{AtomicBool, Ordering},
+ mpsc,
+ },
+ thread::JoinHandle,
+ time::{Duration, Instant},
+};
+
+const VIDEO_BUFFER_WINDOW_SECS: u64 = 40;
+const MIN_VIDEO_BUDGET_BYTES: usize = 32 * 1024 * 1024;
+const MAX_VIDEO_BUDGET_BYTES: usize = 512 * 1024 * 1024;
+const VIDEO_BUDGET_MEMORY_DIVISOR: u64 = 32;
+pub(crate) const AUDIO_BUDGET_BYTES: usize = 16 * 1024 * 1024;
+const STALL_LOG_THRESHOLD: Duration = Duration::from_secs(1);
+const WRITE_ERROR_LOG_INTERVAL: u64 = 100;
+// Muxers join their encoder thread for up to 5 s before finishing, and an
+// unjoined pipeline stop gives up after 10 s; finishing within 4 s keeps the
+// whole stop inside that window.
+pub(crate) const FINISH_DRAIN_LIMIT: Duration = Duration::from_secs(3);
+pub(crate) const FINISH_LIMIT: Duration = Duration::from_secs(4);
+const STORAGE_MOVING_WINDOW: Duration = Duration::from_secs(1);
+const DROP_JOIN_LIMIT: Duration = Duration::from_millis(250);
+const WAIT_POLL: Duration = Duration::from_millis(50);
+
+pub(crate) fn video_budget_bytes(bits_per_second: u64) -> usize {
+ video_budget_for(bits_per_second, total_memory_bytes())
+}
+
+fn video_budget_for(bits_per_second: u64, total_memory: Option<u64>) -> usize {
+ let ceiling = total_memory
+ .map(|memory| usize::try_from(memory / VIDEO_BUDGET_MEMORY_DIVISOR).unwrap_or(usize::MAX))
+ .unwrap_or(MAX_VIDEO_BUDGET_BYTES)
+ .clamp(MIN_VIDEO_BUDGET_BYTES, MAX_VIDEO_BUDGET_BYTES);
+ let bytes = (bits_per_second / 8).saturating_mul(VIDEO_BUFFER_WINDOW_SECS);
+ usize::try_from(bytes)
+ .unwrap_or(usize::MAX)
+ .clamp(MIN_VIDEO_BUDGET_BYTES, ceiling)
+}
+
+fn total_memory_bytes() -> Option<u64> {
+ let system = sysinfo::System::new_with_specifics(
+ sysinfo::RefreshKind::new().with_memory(sysinfo::MemoryRefreshKind::new().with_ram()),
+ );
+ Some(system.total_memory()).filter(|memory| *memory > 0)
+}
+
+pub(crate) trait MuxTarget: Send + 'static {
+ fn output(&self) -> &format::context::Output;
+
+ fn output_mut(&mut self) -> &mut format::context::Output;
+
+ fn writer_started(&mut self) {}
+
+ fn packet_written(&mut self) {}
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub(crate) struct StorageStalled;
+
+impl From<StorageStalled> for std::io::Error {
+ fn from(_: StorageStalled) -> Self {
+ std::io::Error::new(
+ std::io::ErrorKind::TimedOut,
+ "output storage stalled while finishing; recording left for recovery",
+ )
+ }
+}
+
+impl From<StorageStalled> for ffmpeg::Error {
+ fn from(_: StorageStalled) -> Self {
+ ffmpeg::Error::Other {
+ errno: ffmpeg::error::EIO,
+ }
+ }
+}
+
+pub(crate) enum MuxSink<M: MuxTarget> {
+ Direct(M),
+ Behind(WriteBehind<M>),
+ Detached,
+}
+
+impl<M: MuxTarget> MuxSink<M> {
+ pub fn enable_write_behind(
+ &mut self,
+ label: &'static str,
+ budget: usize,
+ ) -> std::io::Result<()> {
+ if !matches!(self, Self::Direct(_)) {
+ return Ok(());
+ }
+ let Self::Direct(target) = std::mem::replace(self, Self::Detached) else {
+ unreachable!("checked above");
+ };
+ match WriteBehind::spawn(label, target, budget, FinishLimits::DEFAULT) {
+ Ok(writer) => {
+ *self = Self::Behind(writer);
+ Ok(())
+ }
+ Err((target, error)) => {
+ *self = Self::Direct(target);
+ Err(error)
+ }
+ }
+ }
+
+ pub fn is_write_behind(&self) -> bool {
+ matches!(self, Self::Behind(_))
+ }
+
+ pub fn with_target<R>(&mut self, f: impl FnOnce(&mut M) -> R) -> R {
+ match self {
+ Self::Direct(target) => f(target),
+ Self::Behind(writer) => f(&mut writer.target()),
+ Self::Detached => unreachable!("mux sink detached"),
+ }
+ }
+
+ pub fn with_target_ref<R>(&self, f: impl FnOnce(&M) -> R) -> R {
+ match self {
+ Self::Direct(target) => f(target),
+ Self::Behind(writer) => f(&writer.target()),
+ Self::Detached => unreachable!("mux sink detached"),
+ }
+ }
+
+ pub fn frame_queued(&mut self) {
+ if let Self::Direct(target) = self {
+ target.packet_written();
+ }
+ }
+
+ /// Runs the encoder's end-of-stream `flush` into this sink, then
+ /// `finalize` with the flush result, which also carries write errors of
+ /// the end-of-stream packets as an inline flush would. With write-behind
+ /// both run within [`FINISH_LIMIT`]; `Err` means storage stalled and the
+ /// output was left unfinalized.
+ pub fn finish<R: Send + 'static>(
+ &mut self,
+ flush: impl FnOnce(&mut Self) -> Result<(), ffmpeg::Error>,
+ finalize: impl FnOnce(&mut M, Result<(), ffmpeg::Error>) -> R + Send + 'static,
+ ) -> Result<R, StorageStalled> {
+ if let Self::Behind(writer) = self {
+ writer.begin_finish();
+ }
+ let flush_result = flush(self);
+ match self {
+ Self::Direct(target) => Ok(finalize(target, flush_result)),
+ Self::Behind(writer) => writer.finish(flush_result, finalize),
+ Self::Detached => unreachable!("mux sink detached"),
+ }
+ }
+
+ #[cfg(test)]
+ pub fn drain(&self) {
+ if let Self::Behind(writer) = self {
+ writer.drain();
+ }
+ }
+
+ #[cfg(test)]
+ pub fn stall_storage_for(&self, duration: Duration) -> std::thread::JoinHandle<()> {
+ let Self::Behind(writer) = self else {
+ panic!("write-behind not enabled");
+ };
+ let shared = writer.shared.clone();
+ let (held_tx, held_rx) = mpsc::channel();
+ let handle = std::thread::spawn(move || {
+ let _held = lock(&shared.target);
+ held_tx.send(()).unwrap();
+ std::thread::sleep(duration);
+ });
+ held_rx.recv().unwrap();
+ handle
+ }
+}
+
+impl<M: MuxTarget> PacketSink for MuxSink<M> {
+ fn stream_time_base(&self, stream_index: usize) -> Rational {
+ match self {
+ Self::Direct(target) => target.output().stream_time_base(stream_index),
+ Self::Behind(writer) => writer.time_bases[stream_index],
+ Self::Detached => unreachable!("mux sink detached"),
+ }
+ }
+
+ fn write_packet(&mut self, packet: Packet) -> Result<(), ffmpeg::Error> {
+ match self {
+ Self::Direct(target) => packet.write_interleaved(target.output_mut()),
+ Self::Behind(writer) => writer.write_packet(packet),
+ Self::Detached => unreachable!("mux sink detached"),
+ }
+ }
+}
+
+#[derive(Clone, Copy)]
+struct FinishLimits {
+ drain: Duration,
+ total: Duration,
+}
+
+impl FinishLimits {
+ const DEFAULT: Self = Self {
+ drain: FINISH_DRAIN_LIMIT,
+ total: FINISH_LIMIT,
+ };
+}
+
+type Finalizer<M> = Box<dyn FnOnce(&mut M, Result<(), ffmpeg::Error>) + Send>;
+
+enum Job<M> {
+ Packet { packet: Packet, end_of_stream: bool },
+ Finalize(Finalizer<M>),
+}
+
+struct Queue<M> {
+ jobs: VecDeque<Job<M>>,
+ bytes: usize,
+ in_flight: bool,
+ closed: bool,
+ finishing: bool,
+ exited: bool,
+ last_write_completed: Option<Instant>,
+ write_errors: u64,
+ end_of_stream_error: Option<ffmpeg::Error>,
+}
+
+struct Shared<M> {
+ queue: Mutex<Queue<M>>,
+ changed: Condvar,
+ abandoned: AtomicBool,
+ finalize_cancelled: AtomicBool,
+ target: Mutex<M>,
+}
+
+fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
+ mutex
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+}
+
+impl<M> Shared<M> {
+ fn wait<'a>(
+ &self,
+ queue: MutexGuard<'a, Queue<M>>,
+ timeout: Duration,
+ ) -> MutexGuard<'a, Queue<M>> {
+ self.changed
+ .wait_timeout(queue, timeout)
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .0
+ }
+
+ fn discard_packets(&self, queue: &mut Queue<M>) -> (usize, usize) {
+ let mut packets = 0;
+ let mut bytes = 0;
+ queue.jobs.retain(|job| match job {
+ Job::Packet { packet, .. } => {
+ packets += 1;
+ bytes += packet.size();
+ false
+ }
+ Job::Finalize(_) => true,
+ });
+ queue.bytes -= bytes;
+ self.changed.notify_all();
+ (packets, bytes)
+ }
+
+ fn abandon(&self, queue: &mut Queue<M>) {
+ self.abandoned.store(true, Ordering::Release);
+ self.finalize_cancelled.store(true, Ordering::Release);
+ queue.jobs.clear();
+ queue.bytes = 0;
+ self.changed.notify_all();
+ }
+}
+
+pub(crate) struct WriteBehind<M: MuxTarget> {
+ shared: Arc<Shared<M>>,
+ time_bases: Vec<Rational>,
+ budget: usize,
+ label: &'static str,
+ limits: FinishLimits,
+ thread: Option<JoinHandle<()>>,
+}
+
+impl<M: MuxTarget> WriteBehind<M> {
+ fn spawn(
+ label: &'static str,
+ target: M,
+ budget: usize,
+ limits: FinishLimits,
+ ) -> Result<Self, (M, std::io::Error)> {
+ let time_bases = target
+ .output()
+ .streams()
+ .map(|stream| stream.time_base())
+ .collect();
+ let shared = Arc::new(Shared {
+ queue: Mutex::new(Queue {
+ jobs: VecDeque::new(),
+ bytes: 0,
+ in_flight: false,
+ closed: false,
+ finishing: false,
+ exited: false,
+ last_write_completed: None,
+ write_errors: 0,
+ end_of_stream_error: None,
+ }),
+ changed: Condvar::new(),
+ abandoned: AtomicBool::new(false),
+ finalize_cancelled: AtomicBool::new(false),
+ target: Mutex::new(target),
+ });
+ let spawned = std::thread::Builder::new()
+ .name(format!("mux-writer-{label}"))
+ .spawn({
+ let shared = shared.clone();
+ move || run_writer(&shared, label)
+ });
+ let thread = match spawned {
+ Ok(thread) => thread,
+ Err(error) => {
+ let target = Arc::try_unwrap(shared)
+ .ok()
+ .expect("writer thread never started")
+ .target
+ .into_inner()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ return Err((target, error));
+ }
+ };
+ Ok(Self {
+ shared,
+ time_bases,
+ budget,
+ label,
+ limits,
+ thread: Some(thread),
+ })
+ }
+
+ fn target(&self) -> MutexGuard<'_, M> {
+ lock(&self.shared.target)
+ }
+
+ fn begin_finish(&mut self) {
+ lock(&self.shared.queue).finishing = true;
+ }
+
+ fn write_packet(&mut self, packet: Packet) -> Result<(), ffmpeg::Error> {
+ let size = packet.size();
+ let mut queue = lock(&self.shared.queue);
+ let mut blocked_since = None;
+ loop {
+ if queue.exited {
+ return Err(ffmpeg::Error::Bug);
+ }
+ if queue.finishing || queue.bytes == 0 || queue.bytes + size <= self.budget {
+ break;
+ }
+ if blocked_since.is_none() {
+ blocked_since = Some(Instant::now());
+ tracing::warn!(
+ writer = self.label,
+ queued_bytes = queue.bytes,
+ budget_bytes = self.budget,
+ "Output write-behind budget exhausted; encoder now waits for storage"
+ );
+ }
+ queue = self.shared.wait(queue, WAIT_POLL);
+ }
+ queue.bytes += size;
+ let end_of_stream = queue.finishing;
+ queue.jobs.push_back(Job::Packet {
+ packet,
+ end_of_stream,
+ });
+ drop(queue);
+ self.shared.changed.notify_all();
+
+ if let Some(since) = blocked_since {
+ tracing::warn!(
+ writer = self.label,
+ waited_ms = since.elapsed().as_millis() as u64,
+ "Output write-behind budget recovered"
+ );
+ }
+
+ Ok(())
+ }
+
+ #[cfg(test)]
+ fn drain(&self) {
+ let mut queue = lock(&self.shared.queue);
+ while (!queue.jobs.is_empty() || queue.in_flight) && !queue.exited {
+ queue = self.shared.wait(queue, WAIT_POLL);
+ }
+ }
+
+ fn finish<R: Send + 'static>(
+ &mut self,
+ flush_result: Result<(), ffmpeg::Error>,
+ finalize: impl FnOnce(&mut M, Result<(), ffmpeg::Error>) -> R + Send + 'static,
+ ) -> Result<R, StorageStalled> {
+ let Some(thread) = self.thread.take() else {
+ return Err(StorageStalled);
+ };
+ let (result_tx, result_rx) = mpsc::sync_channel(1);
+ {
+ let mut queue = lock(&self.shared.queue);
+ queue
+ .jobs
+ .push_back(Job::Finalize(Box::new(move |target, end_of_stream| {
+ let _ = result_tx.send(finalize(target, flush_result.and(end_of_stream)));
+ })));
+ queue.closed = true;
+ }
+ self.shared.changed.notify_all();
+
+ let started = Instant::now();
+ let mut truncated = false;
+ loop {
+ match result_rx.recv_timeout(WAIT_POLL) {
+ Ok(result) => {
+ let _ = thread.join();
+ return Ok(result);
+ }
+ Err(mpsc::RecvTimeoutError::Disconnected) => {
+ let _ = thread.join();
+ tracing::error!(writer = self.label, "Output writer exited before finishing");
+ return Err(StorageStalled);
+ }
+ Err(mpsc::RecvTimeoutError::Timeout) => {}
+ }
+ let storage_moving = lock(&self.shared.queue)
+ .last_write_completed
+ .is_some_and(|completed| completed.elapsed() < STORAGE_MOVING_WINDOW);
+ if !truncated && storage_moving && started.elapsed() >= self.limits.drain {
+ truncated = true;
+ let (packets, bytes) = self.shared.discard_packets(&mut lock(&self.shared.queue));
+ if packets > 0 {
+ tracing::warn!(
+ writer = self.label,
+ discarded_packets = packets,
+ discarded_bytes = bytes,
+ "Output storage too slow to drain the write-behind buffer; finishing without its tail"
+ );
+ }
+ }
+ if started.elapsed() >= self.limits.total {
+ self.shared
+ .finalize_cancelled
+ .store(true, Ordering::Release);
+ let queue = lock(&self.shared.queue);
+ tracing::error!(
+ writer = self.label,
+ queued_bytes = queue.bytes,
+ "Output storage stalled while finishing; leaving the recording for recovery and writing the buffer once storage recovers"
+ );
+ return Err(StorageStalled);
+ }
+ }
+ }
+}
+
+impl<M: MuxTarget> Drop for WriteBehind<M> {
+ fn drop(&mut self) {
+ let Some(thread) = self.thread.take() else {
+ return;
+ };
+ let mut queue = lock(&self.shared.queue);
+ queue.closed = true;
+ self.shared.abandon(&mut queue);
+ let deadline = Instant::now() + DROP_JOIN_LIMIT;
+ while !queue.exited && Instant::now() < deadline {
+ queue = self.shared.wait(queue, WAIT_POLL.min(DROP_JOIN_LIMIT));
+ }
+ let exited = queue.exited;
+ drop(queue);
+ if exited {
+ let _ = thread.join();
+ }
+ }
+}
+
+struct WriterExit<'a, M>(&'a Shared<M>);
+
+impl<M> Drop for WriterExit<'_, M> {
+ fn drop(&mut self) {
+ let mut queue = lock(&self.0.queue);
+ queue.exited = true;
+ queue.in_flight = false;
+ drop(queue);
+ self.0.changed.notify_all();
+ }
+}
+
+fn run_writer<M: MuxTarget>(shared: &Shared<M>, label: &'static str) {
+ let _exit = WriterExit(shared);
+ {
+ let mut target = lock(&shared.target);
+ if !shared.abandoned.load(Ordering::Acquire) {
+ target.writer_started();
+ }
+ }
+ loop {
+ let next = {
+ let mut queue = lock(&shared.queue);
+ loop {
+ if shared.abandoned.load(Ordering::Acquire) {
+ break None;
+ }
+ if let Some(next) = queue.jobs.pop_front() {
+ queue.in_flight = true;
+ break Some(next);
+ }
+ if queue.closed {
+ break None;
+ }
+ queue = shared
+ .changed
+ .wait(queue)
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ }
+ };
+ let Some(job) = next else {
+ return;
+ };
+
+ let (packet, end_of_stream) = match job {
+ Job::Packet {
+ packet,
+ end_of_stream,
+ } => (packet, end_of_stream),
+ Job::Finalize(finalize) => {
+ let end_of_stream_error = lock(&shared.queue).end_of_stream_error.take();
+ let mut target = lock(&shared.target);
+ if !shared.finalize_cancelled.load(Ordering::Acquire) {
+ finalize(&mut target, end_of_stream_error.map_or(Ok(()), Err));
+ }
+ drop(target);
+ lock(&shared.queue).in_flight = false;
+ shared.changed.notify_all();
+ continue;
+ }
+ };
+
+ let size = packet.size();
+ let started = Instant::now();
+ let result = {
+ let mut target = lock(&shared.target);
+ if shared.abandoned.load(Ordering::Acquire) {
+ None
+ } else {
+ let result = packet.write_interleaved(target.output_mut());
+ if !shared.abandoned.load(Ordering::Acquire) {
+ target.packet_written();
+ }
+ Some(result)
+ }
+ };
+ let elapsed = started.elapsed();
+
+ let (backlog, error_count) = {
+ let mut queue = lock(&shared.queue);
+ queue.bytes = queue.bytes.saturating_sub(size);
+ queue.in_flight = false;
+ queue.last_write_completed = Some(Instant::now());
+ if let Some(Err(error)) = &result {
+ queue.write_errors += 1;
+ if end_of_stream && queue.end_of_stream_error.is_none() {
+ queue.end_of_stream_error = Some(*error);
+ }
+ }
+ (queue.bytes, queue.write_errors)
+ };
+ shared.changed.notify_all();
+
+ if let Some(Err(error)) = result
+ && (error_count == 1 || error_count.is_multiple_of(WRITE_ERROR_LOG_INTERVAL))
+ {
+ tracing::warn!(
+ writer = label,
+ %error,
+ failed_writes = error_count,
+ "Failed to write output packet"
+ );
+ }
+
+ if elapsed >= STALL_LOG_THRESHOLD {
+ tracing::warn!(
+ writer = label,
+ stalled_ms = elapsed.as_millis() as u64,
+ backlog_bytes = backlog,
+ "Output storage write stalled; capture continued into the write-behind buffer"
+ );
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ struct NutTarget {
+ output: format::context::Output,
+ write_delay: Duration,
+ panic_on_write: bool,
+ }
+
+ impl MuxTarget for NutTarget {
+ fn output(&self) -> &format::context::Output {
+ &self.output
+ }
+
+ fn output_mut(&mut self) -> &mut format::context::Output {
+ assert!(!self.panic_on_write, "simulated writer failure");
+ std::thread::sleep(self.write_delay);
+ &mut self.output
+ }
+ }
+
+ fn nut_target(path: &std::path::Path) -> NutTarget {
+ ffmpeg::init().unwrap();
+ let mut output = format::output_as(path, "nut").unwrap();
+ let codec = ffmpeg::encoder::find(ffmpeg::codec::Id::PCM_S16LE).unwrap();
+ let mut stream = output.add_stream(codec).unwrap();
+ stream.set_time_base(Rational::new(1, 48_000));
+ unsafe {
+ let parameters = (*stream.as_mut_ptr()).codecpar;
+ (*parameters).codec_type = ffmpeg::ffi::AVMediaType::AVMEDIA_TYPE_AUDIO;
+ (*parameters).codec_id = ffmpeg::ffi::AVCodecID::AV_CODEC_ID_PCM_S16LE;
+ (*parameters).sample_rate = 48_000;
+ ffmpeg::ffi::av_channel_layout_default(&mut (*parameters).ch_layout, 1);
+ }
+ output.write_header().unwrap();
+ NutTarget {
+ output,
+ write_delay: Duration::ZERO,
+ panic_on_write: false,
+ }
+ }
+
+ fn packet(pts: i64, size: usize) -> Packet {
+ let mut packet = Packet::copy(&vec![0u8; size]);
+ packet.set_stream(0);
+ packet.set_pts(Some(pts));
+ packet.set_dts(Some(pts));
+ packet.set_duration(1024);
+ packet
+ }
+
+ fn invalid_packet(pts: i64) -> Packet {
+ let mut packet = packet(pts, 16);
+ packet.set_stream(7);
+ packet
+ }
+
+ fn behind(target: NutTarget, budget: usize, limits: FinishLimits) -> MuxSink<NutTarget> {
+ MuxSink::Behind(
+ WriteBehind::spawn("test", target, budget, limits)
+ .ok()
+ .unwrap(),
+ )
+ }
+
+ fn shared(sink: &MuxSink<NutTarget>) -> Arc<Shared<NutTarget>> {
+ match sink {
+ MuxSink::Behind(writer) => writer.shared.clone(),
+ _ => panic!("write-behind not enabled"),
+ }
+ }
+
+ fn header_only_len(directory: &std::path::Path) -> u64 {
+ let path = directory.join("header-only.nut");
+ drop(nut_target(&path));
+ std::fs::metadata(path).unwrap().len()
+ }
+
+ fn finish_with_trailer(sink: &mut MuxSink<NutTarget>) -> Result<(), ffmpeg::Error> {
+ sink.finish(
+ |_| Ok(()),
+ |target, result| {
+ target.output.write_trailer().unwrap();
+ result
+ },
+ )
+ .unwrap()
+ }
+
+ fn written_pts(path: &std::path::Path) -> Vec<i64> {
+ let mut input = format::input(path).unwrap();
+ input
+ .packets()
+ .filter_map(|(_, packet)| packet.pts())
+ .collect()
+ }
+
+ #[test]
+ fn producer_runs_ahead_of_a_stalled_writer_and_order_is_preserved() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("out.nut");
+ let mut sink = MuxSink::Direct(nut_target(&path));
+ sink.enable_write_behind("test", 1 << 20).unwrap();
+ assert!(sink.is_write_behind());
+
+ let shared = shared(&sink);
+ let stalled = lock(&shared.target);
+ let started = Instant::now();
+ for pts in 0..64 {
+ sink.write_packet(packet(pts * 1024, 2048)).unwrap();
+ }
+ assert!(started.elapsed() < Duration::from_secs(1));
+ drop(stalled);
+
+ finish_with_trailer(&mut sink).unwrap();
+ assert_eq!(
+ written_pts(&path),
+ (0..64).map(|pts| pts * 1024).collect::<Vec<_>>()
+ );
+ }
+
+ #[test]
+ fn exhausted_budget_falls_back_to_waiting_for_storage() {
+ let directory = tempfile::tempdir().unwrap();
+ let mut sink = MuxSink::Direct(nut_target(&directory.path().join("out.nut")));
+ sink.enable_write_behind("test", 8 * 1024).unwrap();
+ let shared = shared(&sink);
+
+ let stalled = lock(&shared.target);
+ for pts in 0..4 {
+ sink.write_packet(packet(pts * 1024, 2048)).unwrap();
+ }
+ let producer = std::thread::spawn(move || {
+ let started = Instant::now();
+ sink.write_packet(packet(4 * 1024, 2048)).unwrap();
+ (sink, started.elapsed())
+ });
+ std::thread::sleep(Duration::from_millis(300));
+ drop(stalled);
+ let (mut sink, waited) = producer.join().unwrap();
+ assert!(waited >= Duration::from_millis(250));
+ finish_with_trailer(&mut sink).unwrap();
+ }
+
+ #[test]
+ fn oversized_packet_is_accepted_into_an_empty_queue() {
+ let directory = tempfile::tempdir().unwrap();
+ let mut sink = MuxSink::Direct(nut_target(&directory.path().join("out.nut")));
+ sink.enable_write_behind("test", 1024).unwrap();
+ sink.write_packet(packet(0, 4096)).unwrap();
+ finish_with_trailer(&mut sink).unwrap();
+ }
+
+ #[test]
+ fn earlier_write_errors_never_abort_the_end_of_stream_flush() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("out.nut");
+ let mut sink = MuxSink::Direct(nut_target(&path));
+ sink.enable_write_behind("test", 1 << 20).unwrap();
+
+ sink.write_packet(packet(0, 64)).unwrap();
+ sink.write_packet(invalid_packet(1024)).unwrap();
+ sink.drain();
+ sink.write_packet(packet(2048, 64)).unwrap();
+ let result = sink
+ .finish(
+ |sink| sink.write_packet(packet(3072, 64)),
+ |target, result| {
+ target.output.write_trailer().unwrap();
+ result
+ },
+ )
+ .unwrap();
+
+ assert_eq!(result, Ok(()));
+ assert_eq!(written_pts(&path), vec![0, 2048, 3072]);
+ }
+
+ #[test]
+ fn end_of_stream_write_errors_are_reported_like_an_inline_flush() {
+ let directory = tempfile::tempdir().unwrap();
+ let mut sink = MuxSink::Direct(nut_target(&directory.path().join("out.nut")));
+ sink.enable_write_behind("test", 1 << 20).unwrap();
+ sink.write_packet(packet(0, 64)).unwrap();
+ let result = sink
+ .finish(
+ |sink| sink.write_packet(invalid_packet(1024)),
+ |_, result| result,
+ )
+ .unwrap();
+ assert!(result.is_err());
+ }
+
+ #[test]
+ fn stalled_finish_returns_in_time_and_keeps_the_buffer_for_when_storage_recovers() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("out.nut");
+ let packets = 8usize;
+ let packet_size = 64 * 1024;
+ let finalized = Arc::new(AtomicBool::new(false));
+ {
+ let limits = FinishLimits {
+ drain: Duration::from_millis(100),
+ total: Duration::from_millis(300),
+ };
+ let mut sink = behind(nut_target(&path), 1 << 20, limits);
+ let shared = shared(&sink);
+ let stalled = lock(&shared.target);
+ for pts in 0..packets {
+ sink.write_packet(packet(pts as i64 * 1024, packet_size))
+ .unwrap();
+ }
+ let started = Instant::now();
+ let finalized_flag = finalized.clone();
+ let result = sink.finish(
+ |_| Ok(()),
+ move |_, _| finalized_flag.store(true, Ordering::Release),
+ );
+ assert_eq!(result, Err(StorageStalled));
+ assert!(started.elapsed() < Duration::from_secs(1));
+ drop(sink);
+ drop(stalled);
+ let deadline = Instant::now() + Duration::from_secs(2);
+ while Arc::strong_count(&shared) > 1 && Instant::now() < deadline {
+ std::thread::sleep(Duration::from_millis(10));
+ }
+ assert_eq!(Arc::strong_count(&shared), 1);
+ }
+ assert!(!finalized.load(Ordering::Acquire));
+ assert!(
+ std::fs::metadata(&path).unwrap().len()
+ >= header_only_len(directory.path()) + (packets * packet_size) as u64
+ );
+ }
+
+ #[test]
+ fn slow_storage_finishes_without_the_tail_within_the_limit() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("out.nut");
+ let mut target = nut_target(&path);
+ target.write_delay = Duration::from_millis(60);
+ let limits = FinishLimits {
+ drain: Duration::from_millis(300),
+ total: Duration::from_secs(2),
+ };
+ let mut sink = behind(target, 1 << 20, limits);
+ for pts in 0..40 {
+ sink.write_packet(packet(pts * 1024, 64)).unwrap();
+ }
+ let started = Instant::now();
+ let result = sink
+ .finish(
+ |_| Ok(()),
+ |target, result| {
+ target.write_delay = Duration::ZERO;
+ target.output.write_trailer().unwrap();
+ result
+ },
+ )
+ .unwrap();
+ assert_eq!(result, Ok(()));
+ assert!(started.elapsed() < Duration::from_secs(2));
+ let pts = written_pts(&path);
+ assert!(!pts.is_empty() && pts.len() < 40);
+ assert_eq!(
+ pts,
+ (0..pts.len() as i64)
+ .map(|pts| pts * 1024)
+ .collect::<Vec<_>>()
+ );
+ }
+
+ #[test]
+ fn slow_finalization_counts_against_the_finish_limit() {
+ let directory = tempfile::tempdir().unwrap();
+ let limits = FinishLimits {
+ drain: Duration::from_millis(100),
+ total: Duration::from_millis(300),
+ };
+ let mut sink = behind(
+ nut_target(&directory.path().join("out.nut")),
+ 1 << 20,
+ limits,
+ );
+ sink.write_packet(packet(0, 64)).unwrap();
+ let started = Instant::now();
+ let result = sink.finish(
+ |_| Ok(()),
+ |_, _| std::thread::sleep(Duration::from_secs(1)),
+ );
+ assert_eq!(result, Err(StorageStalled));
+ assert!(started.elapsed() < Duration::from_millis(900));
+ }
+
+ #[test]
+ fn dropping_without_finish_stops_the_writer_promptly() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("out.nut");
+ let shared;
+ {
+ let mut sink = MuxSink::Direct(nut_target(&path));
+ sink.enable_write_behind("test", 1 << 20).unwrap();
+ shared = self::shared(&sink);
+ let stalled = lock(&shared.target);
+ for pts in 0..16 {
+ sink.write_packet(packet(pts * 1024, 64 * 1024)).unwrap();
+ }
+ let dropping = std::thread::spawn(move || {
+ let started = Instant::now();
+ drop(sink);
+ started.elapsed()
+ });
+ std::thread::sleep(Duration::from_millis(50));
+ let drop_time = dropping.join().unwrap();
+ assert!(drop_time < Duration::from_millis(600));
+ drop(stalled);
+ }
+ let deadline = Instant::now() + Duration::from_secs(2);
+ while Arc::strong_count(&shared) > 1 && Instant::now() < deadline {
+ std::thread::sleep(Duration::from_millis(10));
+ }
+ assert_eq!(Arc::strong_count(&shared), 1);
+ drop(shared);
+ assert_eq!(
+ std::fs::metadata(&path).unwrap().len(),
+ header_only_len(directory.path())
+ );
+ }
+
+ #[test]
+ fn idle_writer_drop_joins_the_thread() {
+ let directory = tempfile::tempdir().unwrap();
+ let mut sink = MuxSink::Direct(nut_target(&directory.path().join("out.nut")));
+ sink.enable_write_behind("test", 1 << 20).unwrap();
+ let shared = shared(&sink);
+ drop(sink);
+ assert_eq!(Arc::strong_count(&shared), 1);
+ }
+
+ #[test]
+ fn writer_panic_is_reported_on_the_next_enqueue() {
+ let directory = tempfile::tempdir().unwrap();
+ let mut target = nut_target(&directory.path().join("out.nut"));
+ target.panic_on_write = true;
+ let mut sink = behind(target, 1 << 20, FinishLimits::DEFAULT);
+ sink.write_packet(packet(0, 64)).unwrap();
+ let shared = shared(&sink);
+ let deadline = Instant::now() + Duration::from_secs(2);
+ while !lock(&shared.queue).exited && Instant::now() < deadline {
+ std::thread::sleep(Duration::from_millis(10));
+ }
+ assert_eq!(sink.write_packet(packet(1024, 64)), Err(ffmpeg::Error::Bug));
+ assert_eq!(
+ sink.finish(|_| Ok(()), |_, result| result),
+ Err(StorageStalled)
+ );
+ }
+
+ #[test]
+ fn video_budget_scales_with_bitrate_and_machine_memory() {
+ const GIB: u64 = 1024 * 1024 * 1024;
+ assert_eq!(video_budget_for(0, Some(16 * GIB)), MIN_VIDEO_BUDGET_BYTES);
+ assert_eq!(video_budget_for(20_000_000, Some(16 * GIB)), 100_000_000);
+ assert_eq!(
+ video_budget_for(u64::MAX, Some(16 * GIB)),
+ MAX_VIDEO_BUDGET_BYTES
+ );
+ assert_eq!(video_budget_for(u64::MAX, Some(8 * GIB)), 256 * 1024 * 1024);
+ assert_eq!(
+ video_budget_for(u64::MAX, Some(GIB)),
+ MIN_VIDEO_BUDGET_BYTES
+ );
+ assert_eq!(video_budget_for(u64::MAX, None), MAX_VIDEO_BUDGET_BYTES);
+ }
+}
diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rs
index 2cdc2f610..2bc025dcb 100644
--- a/crates/enc-ffmpeg/src/video/h264.rs
+++ b/crates/enc-ffmpeg/src/video/h264.rs
@@ -11,7 +11,7 @@ use ffmpeg::{
};
use tracing::{debug, error, info, trace, warn};
-use crate::base::EncoderBase;
+use crate::base::{EncoderBase, PacketSink};
use crate::video::h264_packet::H264PacketEncoder;
#[cfg(target_os = "macos")]
use crate::video::videotoolbox_hw::VideoToolboxHwFrames;
@@ -879,6 +879,14 @@ impl H264Encoder {
.map_err(|e| H264EncoderError::VideoToolboxHwInput(e.to_string()))
}
+ /// Whether the selected codec is a hardware encoder, which can fail at
+ /// runtime after opening (a busy or reset media engine).
+ pub fn is_hardware(&self) -> bool {
+ self.encoder
+ .codec()
+ .is_some_and(|codec| is_hardware_h264(codec.name()))
+ }
+
pub fn conversion_requirements(&self) -> ConversionRequirements {
let needs_conversion = self.input_format != self.output_format
|| self.input_width != self.output_width
@@ -896,9 +904,18 @@ impl H264Encoder {
pub fn queue_frame(
&mut self,
- mut frame: frame::Video,
+ frame: frame::Video,
timestamp: Duration,
output: &mut format::context::Output,
+ ) -> Result<(), QueueFrameError> {
+ self.queue_frame_to(frame, timestamp, output)
+ }
+
+ pub fn queue_frame_to<S: PacketSink + ?Sized>(
+ &mut self,
+ mut frame: frame::Video,
+ timestamp: Duration,
+ output: &mut S,
) -> Result<(), QueueFrameError> {
self.base
.update_pts(&mut frame, timestamp, &mut self.encoder);
@@ -906,8 +923,7 @@ impl H264Encoder {
let frame_to_send = if let Some(converter) = &mut self.converter {
let pts = frame.pts();
let converted = self.converted_frame_pool.as_mut().unwrap();
- converter
- .run(&frame, converted)
+ super::packed_yuv::convert_or_scale(&frame, converted, converter)
.map_err(QueueFrameError::Converter)?;
converted.set_pts(pts);
converted as &frame::Video
@@ -936,8 +952,7 @@ impl H264Encoder {
let converted = converted_frame.get_or_insert_with(|| {
frame::Video::new(self.output_format, self.output_width, self.output_height)
});
- converter
- .run(frame, converted)
+ super::packed_yuv::convert_or_scale(frame, converted, converter)
.map_err(QueueFrameError::Converter)?;
converted.set_pts(pts);
converted as &frame::Video
@@ -982,6 +997,28 @@ impl H264Encoder {
self.base.process_eof(output, &mut self.encoder)
}
+ pub fn flush_to<S: PacketSink + ?Sized>(
+ &mut self,
+ output: &mut S,
+ ) -> Result<(), ffmpeg::Error> {
+ self.base.process_eof(output, &mut self.encoder)
+ }
+
+ /// Configured target bitrate in bits per second, estimated from the
+ /// output geometry when the encoder runs without a bitrate target.
+ pub fn bit_rate(&self) -> u64 {
+ let configured = unsafe { (*self.encoder.as_ptr()).bit_rate };
+ if configured > 0 {
+ return configured as u64;
+ }
+ get_bitrate(
+ self.output_width,
+ self.output_height,
+ 30.0,
+ H264EncoderBuilder::QUALITY_BPP,
+ ) as u64
+ }
+
pub fn set_packet_stats(&mut self, stats: Arc<crate::EncodedPacketStats>) {
self.base.set_packet_stats(stats);
}
diff --git a/crates/enc-ffmpeg/src/video/h264_packet.rs b/crates/enc-ffmpeg/src/video/h264_packet.rs
index fb7596f7c..e344a09e5 100644
--- a/crates/enc-ffmpeg/src/video/h264_packet.rs
+++ b/crates/enc-ffmpeg/src/video/h264_packet.rs
@@ -140,8 +140,7 @@ impl H264PacketEncoder {
let converted = self.converted_frame_pool.as_mut().expect(
"converted_frame_pool present whenever converter is Some (invariant preserved by from_opened)",
);
- converter
- .run(&frame, converted)
+ super::packed_yuv::convert_or_scale(&frame, converted, converter)
.map_err(EncodePacketError::Converter)?;
converted.set_pts(pts);
converted as &frame::Video
@@ -173,8 +172,7 @@ impl H264PacketEncoder {
let converted = converted_frame.get_or_insert_with(|| {
frame::Video::new(self.output_format, self.output_width, self.output_height)
});
- converter
- .run(frame, converted)
+ super::packed_yuv::convert_or_scale(frame, converted, converter)
.map_err(EncodePacketError::Converter)?;
converted.set_pts(pts);
converted as &frame::Video
diff --git a/crates/enc-ffmpeg/src/video/mod.rs b/crates/enc-ffmpeg/src/video/mod.rs
index 104261fc0..040480d0f 100644
--- a/crates/enc-ffmpeg/src/video/mod.rs
+++ b/crates/enc-ffmpeg/src/video/mod.rs
@@ -2,6 +2,7 @@ pub mod colour;
pub mod h264;
pub mod h264_packet;
pub mod hevc;
+mod packed_yuv;
pub mod prores;
#[cfg(target_os = "macos")]
pub mod videotoolbox_hw;
diff --git a/crates/enc-ffmpeg/src/video/packed_yuv.rs b/crates/enc-ffmpeg/src/video/packed_yuv.rs
new file mode 100644
index 000000000..2bc80f474
--- /dev/null
+++ b/crates/enc-ffmpeg/src/video/packed_yuv.rs
@@ -0,0 +1,906 @@
+use ffmpeg::format::Pixel;
+use ffmpeg::frame;
+
+#[derive(Clone, Copy, PartialEq, Eq)]
+enum Packed422 {
+ Uyvy,
+ Yuyv,
+}
+
+#[cfg(target_arch = "x86_64")]
+struct Job {
+ src: *const u8,
+ src_stride: usize,
+ width: usize,
+ height: usize,
+ kind: Packed422,
+ y: *mut u8,
+ y_stride: usize,
+ u: *mut u8,
+ u_stride: usize,
+ v: *mut u8,
+ v_stride: usize,
+ uv: *mut u8,
+ uv_stride: usize,
+ nv12: bool,
+}
+
+#[cfg(target_arch = "x86_64")]
+fn positive_linesize(frame: &frame::Video, plane: usize) -> Option<usize> {
+ if plane >= frame.planes() {
+ return None;
+ }
+ let raw = unsafe { (*frame.as_ptr()).linesize[plane] };
+ if raw <= 0 { None } else { Some(raw as usize) }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn covers(stride: usize, row_bytes: usize, height: usize, len: usize) -> bool {
+ if row_bytes == 0 || height == 0 {
+ return true;
+ }
+ if stride < row_bytes {
+ return false;
+ }
+ stride
+ .checked_mul(height - 1)
+ .and_then(|last| last.checked_add(row_bytes))
+ .is_some_and(|need| need <= len)
+}
+
+/// Same-size UYVY422 or YUYV422 into `dst` when both dimensions are even,
+/// AVX2 is available at runtime, and `dst` is YUV420P or NV12. Any other
+/// input returns false so the caller keeps swscale.
+pub(crate) fn try_convert(src: &frame::Video, dst: &mut frame::Video) -> bool {
+ if src.width() != dst.width() || src.height() != dst.height() {
+ return false;
+ }
+ let kind = match src.format() {
+ Pixel::UYVY422 => Packed422::Uyvy,
+ Pixel::YUYV422 => Packed422::Yuyv,
+ _ => return false,
+ };
+ let nv12 = match dst.format() {
+ Pixel::YUV420P => false,
+ Pixel::NV12 => true,
+ _ => return false,
+ };
+ let width = src.width() as usize;
+ let height = src.height() as usize;
+ if !width.is_multiple_of(2) || !height.is_multiple_of(2) {
+ return false;
+ }
+ if width == 0 || height == 0 {
+ return true;
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ {
+ let _ = (kind, nv12);
+ false
+ }
+ #[cfg(target_arch = "x86_64")]
+ {
+ if !std::arch::is_x86_feature_detected!("avx2") {
+ return false;
+ }
+ let Some(src_stride) = positive_linesize(src, 0) else {
+ return false;
+ };
+ let Some(y_stride) = positive_linesize(dst, 0) else {
+ return false;
+ };
+ if !covers(src_stride, width * 2, height, src.data(0).len())
+ || !covers(y_stride, width, height, dst.data(0).len())
+ {
+ return false;
+ }
+ let chroma_w = width / 2;
+ let chroma_h = height / 2;
+ let (u_stride, v_stride, uv_stride) = if nv12 {
+ let Some(uv_stride) = positive_linesize(dst, 1) else {
+ return false;
+ };
+ if !covers(uv_stride, chroma_w * 2, chroma_h, dst.data(1).len()) {
+ return false;
+ }
+ (0, 0, uv_stride)
+ } else {
+ let Some(u_stride) = positive_linesize(dst, 1) else {
+ return false;
+ };
+ let Some(v_stride) = positive_linesize(dst, 2) else {
+ return false;
+ };
+ if !covers(u_stride, chroma_w, chroma_h, dst.data(1).len())
+ || !covers(v_stride, chroma_w, chroma_h, dst.data(2).len())
+ {
+ return false;
+ }
+ (u_stride, v_stride, 0)
+ };
+
+ let src_ptr = src.data(0).as_ptr();
+ let y_ptr = dst.data_mut(0).as_mut_ptr();
+ let (u_ptr, v_ptr, uv_ptr) = if nv12 {
+ (
+ std::ptr::null_mut(),
+ std::ptr::null_mut(),
+ dst.data_mut(1).as_mut_ptr(),
+ )
+ } else {
+ let u_ptr = dst.data_mut(1).as_mut_ptr();
+ let v_ptr = dst.data_mut(2).as_mut_ptr();
+ (u_ptr, v_ptr, std::ptr::null_mut())
+ };
+
+ let job = Job {
+ src: src_ptr,
+ src_stride,
+ width,
+ height,
+ kind,
+ y: y_ptr,
+ y_stride,
+ u: u_ptr,
+ u_stride,
+ v: v_ptr,
+ v_stride,
+ uv: uv_ptr,
+ uv_stride,
+ nv12,
+ };
+ // SAFETY: AVX2 was detected, and `covers` checked every active plane.
+ unsafe { avx2::execute(&job) };
+ true
+ }
+}
+
+pub(crate) fn convert_or_scale(
+ src: &frame::Video,
+ dst: &mut frame::Video,
+ scaler: &mut ffmpeg::software::scaling::Context,
+) -> Result<(), ffmpeg::Error> {
+ if try_convert(src, dst) {
+ Ok(())
+ } else {
+ scaler.run(src, dst)
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+mod avx2 {
+ use std::arch::x86_64::*;
+
+ use super::{Job, Packed422};
+
+ #[target_feature(enable = "avx2")]
+ pub(super) unsafe fn execute(job: &Job) {
+ unsafe {
+ let masks = match job.kind {
+ Packed422::Uyvy => Masks {
+ y: uyvy_y_mask(),
+ u: uyvy_u_mask(),
+ v: uyvy_v_mask(),
+ },
+ Packed422::Yuyv => Masks {
+ y: yuyv_y_mask(),
+ u: yuyv_u_mask(),
+ v: yuyv_v_mask(),
+ },
+ };
+ if job.nv12 {
+ convert_frame::<true>(job, &masks);
+ } else {
+ convert_frame::<false>(job, &masks);
+ }
+ }
+ }
+
+ struct Masks {
+ y: __m256i,
+ u: __m256i,
+ v: __m256i,
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn convert_frame<const NV12: bool>(job: &Job, masks: &Masks) {
+ unsafe {
+ let mut top = 0;
+ while top < job.height {
+ let row = Row16 {
+ src0: job.src.add(top * job.src_stride),
+ src1: job.src.add((top + 1) * job.src_stride),
+ y0: job.y.add(top * job.y_stride),
+ y1: job.y.add((top + 1) * job.y_stride),
+ top,
+ };
+ let mut x = 0;
+ while x + 32 <= job.width {
+ convert16::<NV12>(job, masks, &row, x);
+ convert16::<NV12>(job, masks, &row, x + 16);
+ x += 32;
+ }
+ while x + 16 <= job.width {
+ convert16::<NV12>(job, masks, &row, x);
+ x += 16;
+ }
+ if x < job.width {
+ scalar_tail::<NV12>(job, top, x);
+ }
+ top += 2;
+ }
+ }
+ }
+
+ struct Row16 {
+ src0: *const u8,
+ src1: *const u8,
+ y0: *mut u8,
+ y1: *mut u8,
+ top: usize,
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn convert16<const NV12: bool>(job: &Job, masks: &Masks, row: &Row16, x: usize) {
+ unsafe {
+ let packed0 = _mm256_loadu_si256(row.src0.add(x * 2).cast());
+ store_luma(packed0, masks.y, row.y0.add(x));
+ let u0 = pack_chroma8(packed0, masks.u);
+ let v0 = pack_chroma8(packed0, masks.v);
+ let packed1 = _mm256_loadu_si256(row.src1.add(x * 2).cast());
+ store_luma(packed1, masks.y, row.y1.add(x));
+ let u1 = pack_chroma8(packed1, masks.u);
+ let v1 = pack_chroma8(packed1, masks.v);
+ let u = _mm_avg_epu8(u0, u1);
+ let v = _mm_avg_epu8(v0, v1);
+ store_chroma8::<NV12>(job, row.top / 2, x / 2, u, v);
+ }
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn store_luma(packed: __m256i, mask: __m256i, dst: *mut u8) {
+ unsafe {
+ let shuffled = _mm256_shuffle_epi8(packed, mask);
+ let lo = _mm256_castsi256_si128(shuffled);
+ let hi = _mm256_extracti128_si256(shuffled, 1);
+ let y = _mm_unpacklo_epi64(lo, hi);
+ _mm_storeu_si128(dst.cast(), y);
+ }
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn pack_chroma8(packed: __m256i, mask: __m256i) -> __m128i {
+ let shuffled = _mm256_shuffle_epi8(packed, mask);
+ let lo = _mm256_castsi256_si128(shuffled);
+ let hi = _mm256_extracti128_si256(shuffled, 1);
+ _mm_unpacklo_epi32(lo, hi)
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn store_chroma8<const NV12: bool>(
+ job: &Job,
+ row: usize,
+ x: usize,
+ u: __m128i,
+ v: __m128i,
+ ) {
+ unsafe {
+ if NV12 {
+ let interleaved = _mm_unpacklo_epi8(u, v);
+ _mm_storeu_si128(job.uv.add(row * job.uv_stride + x * 2).cast(), interleaved);
+ } else {
+ _mm_storel_epi64(job.u.add(row * job.u_stride + x).cast(), u);
+ _mm_storel_epi64(job.v.add(row * job.v_stride + x).cast(), v);
+ }
+ }
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn uyvy_y_mask() -> __m256i {
+ _mm256_setr_epi8(
+ 1, 3, 5, 7, 9, 11, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, 1, 3, 5, 7, 9, 11, 13, 15,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ )
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn uyvy_u_mask() -> __m256i {
+ _mm256_setr_epi8(
+ 0, 4, 8, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 4, 8, 12, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ )
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn uyvy_v_mask() -> __m256i {
+ _mm256_setr_epi8(
+ 2, 6, 10, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 6, 10, 14, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ )
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn yuyv_y_mask() -> __m256i {
+ _mm256_setr_epi8(
+ 0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1, 0, 2, 4, 6, 8, 10, 12, 14,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ )
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn yuyv_u_mask() -> __m256i {
+ _mm256_setr_epi8(
+ 1, 5, 9, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 5, 9, 13, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ )
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn yuyv_v_mask() -> __m256i {
+ _mm256_setr_epi8(
+ 3, 7, 11, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 7, 11, 15, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ )
+ }
+
+ unsafe fn scalar_tail<const NV12: bool>(job: &Job, top: usize, x_start: usize) {
+ let mut x = x_start;
+ while x < job.width {
+ unsafe {
+ let (y0, y1, u0, v0) = load_macro(job, top, x);
+ let (y2, y3, u1, v1) = load_macro(job, top + 1, x);
+ *job.y.add(top * job.y_stride + x) = y0;
+ *job.y.add(top * job.y_stride + x + 1) = y1;
+ *job.y.add((top + 1) * job.y_stride + x) = y2;
+ *job.y.add((top + 1) * job.y_stride + x + 1) = y3;
+ let cx = x / 2;
+ let cy = top / 2;
+ let u = avg(u0, u1);
+ let v = avg(v0, v1);
+ if NV12 {
+ let dst = job.uv.add(cy * job.uv_stride + cx * 2);
+ *dst = u;
+ *dst.add(1) = v;
+ } else {
+ *job.u.add(cy * job.u_stride + cx) = u;
+ *job.v.add(cy * job.v_stride + cx) = v;
+ }
+ }
+ x += 2;
+ }
+ }
+
+ unsafe fn load_macro(job: &Job, row: usize, x: usize) -> (u8, u8, u8, u8) {
+ unsafe {
+ let pixel = job.src.add(row * job.src_stride + x * 2);
+ let b0 = *pixel;
+ let b1 = *pixel.add(1);
+ let b2 = *pixel.add(2);
+ let b3 = *pixel.add(3);
+ match job.kind {
+ Packed422::Uyvy => (b1, b3, b0, b2),
+ Packed422::Yuyv => (b0, b2, b1, b3),
+ }
+ }
+ }
+
+ fn avg(a: u8, b: u8) -> u8 {
+ ((u16::from(a) + u16::from(b) + 1) >> 1) as u8
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use ffmpeg::software::scaling::{self, flag::Flags};
+
+ fn avx2_ready() -> bool {
+ #[cfg(target_arch = "x86_64")]
+ {
+ std::arch::is_x86_feature_detected!("avx2")
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ {
+ false
+ }
+ }
+
+ fn fast_scaler(src: Pixel, dst: Pixel, width: u32, height: u32) -> scaling::Context {
+ scaling::Context::get(src, width, height, dst, width, height, Flags::FAST_BILINEAR).unwrap()
+ }
+
+ fn zero_frame(frame: &mut frame::Video) {
+ for plane in 0..frame.planes() {
+ frame.data_mut(plane).fill(0);
+ }
+ }
+
+ fn fill_packed(frame: &mut frame::Video) {
+ let uyvy = frame.format() == Pixel::UYVY422;
+ let width = frame.width() as usize;
+ let height = frame.height() as usize;
+ let stride = frame.stride(0);
+ let data = frame.data_mut(0);
+ for y in 0..height {
+ let mut x = 0;
+ while x + 1 < width {
+ let y0 = ((x * 17 + y * 3) % 256) as u8;
+ let y1 = ((x * 13 + y * 11 + 40) % 256) as u8;
+ let u = ((x * 5 + y * 7 + 16) % 256) as u8;
+ let v = ((x * 9 + y * 2 + 80) % 256) as u8;
+ let off = y * stride + x * 2;
+ if uyvy {
+ data[off..off + 4].copy_from_slice(&[u, y0, v, y1]);
+ } else {
+ data[off..off + 4].copy_from_slice(&[y0, u, y1, v]);
+ }
+ x += 2;
+ }
+ }
+ }
+
+ fn reference(src: &frame::Video) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
+ let width = src.width() as usize;
+ let height = src.height() as usize;
+ let stride = src.stride(0);
+ let data = src.data(0);
+ let uyvy = src.format() == Pixel::UYVY422;
+ let cw = width / 2;
+ let ch = height / 2;
+ let mut y_plane = vec![0u8; width * height];
+ let mut u_plane = vec![0u8; cw * ch];
+ let mut v_plane = vec![0u8; cw * ch];
+ let mut row = 0;
+ while row < height {
+ let mut x = 0;
+ while x < width {
+ let (y0, y1, u0, v0) = macro_at(data, stride, row, x, uyvy);
+ let (y2, y3, u1, v1) = macro_at(data, stride, row + 1, x, uyvy);
+ y_plane[row * width + x] = y0;
+ y_plane[row * width + x + 1] = y1;
+ y_plane[(row + 1) * width + x] = y2;
+ y_plane[(row + 1) * width + x + 1] = y3;
+ let cx = x / 2;
+ let cy = row / 2;
+ u_plane[cy * cw + cx] = ((u16::from(u0) + u16::from(u1) + 1) >> 1) as u8;
+ v_plane[cy * cw + cx] = ((u16::from(v0) + u16::from(v1) + 1) >> 1) as u8;
+ x += 2;
+ }
+ row += 2;
+ }
+ (y_plane, u_plane, v_plane)
+ }
+
+ fn macro_at(data: &[u8], stride: usize, row: usize, x: usize, uyvy: bool) -> (u8, u8, u8, u8) {
+ let off = row * stride + x * 2;
+ let b0 = data[off];
+ let b1 = data[off + 1];
+ let b2 = data[off + 2];
+ let b3 = data[off + 3];
+ if uyvy {
+ (b1, b3, b0, b2)
+ } else {
+ (b0, b2, b1, b3)
+ }
+ }
+
+ fn assert_matches_reference(actual: &frame::Video, y: &[u8], u: &[u8], v: &[u8]) {
+ let width = actual.width() as usize;
+ let height = actual.height() as usize;
+ let cw = width / 2;
+ let ch = height / 2;
+ for row in 0..height {
+ let got = &actual.data(0)[row * actual.stride(0)..][..width];
+ assert_eq!(got, &y[row * width..][..width], "y row {row}");
+ }
+ if actual.format() == Pixel::NV12 {
+ for row in 0..ch {
+ let got = &actual.data(1)[row * actual.stride(1)..][..cw * 2];
+ for cx in 0..cw {
+ assert_eq!(got[cx * 2], u[row * cw + cx], "nv12 u {cx},{row}");
+ assert_eq!(got[cx * 2 + 1], v[row * cw + cx], "nv12 v {cx},{row}");
+ }
+ }
+ } else {
+ for row in 0..ch {
+ let got_u = &actual.data(1)[row * actual.stride(1)..][..cw];
+ let got_v = &actual.data(2)[row * actual.stride(2)..][..cw];
+ assert_eq!(got_u, &u[row * cw..][..cw], "u row {row}");
+ assert_eq!(got_v, &v[row * cw..][..cw], "v row {row}");
+ }
+ }
+ }
+
+ struct PlaneStats {
+ max_diff: u8,
+ mean: f64,
+ col: usize,
+ row: usize,
+ }
+
+ fn plane_delta(
+ actual: &frame::Video,
+ expected: &frame::Video,
+ plane: usize,
+ width: usize,
+ height: usize,
+ ) -> PlaneStats {
+ let mut max_diff = 0u8;
+ let mut sum = 0u64;
+ let mut count = 0u64;
+ let mut col = 0;
+ let mut row_at = 0;
+ for row in 0..height {
+ let actual_row = &actual.data(plane)[row * actual.stride(plane)..];
+ let expected_row = &expected.data(plane)[row * expected.stride(plane)..];
+ for x in 0..width {
+ let diff = actual_row[x].abs_diff(expected_row[x]);
+ sum += u64::from(diff);
+ count += 1;
+ if diff > max_diff {
+ max_diff = diff;
+ col = x;
+ row_at = row;
+ }
+ }
+ }
+ PlaneStats {
+ max_diff,
+ mean: sum as f64 / count as f64,
+ col,
+ row: row_at,
+ }
+ }
+
+ fn assert_within_swscale(actual: &frame::Video, expected: &frame::Video) {
+ let width = actual.width() as usize;
+ let height = actual.height() as usize;
+ let y_stats = plane_delta(actual, expected, 0, width, height);
+ let y_limit = if actual.format() == Pixel::NV12 { 4 } else { 2 };
+ assert!(
+ y_stats.max_diff <= y_limit && y_stats.mean < 1.0,
+ "{width}x{height} luma max {} mean {:.3} at {},{}",
+ y_stats.max_diff,
+ y_stats.mean,
+ y_stats.col,
+ y_stats.row
+ );
+ let cw = width / 2;
+ let ch = height / 2;
+ if actual.format() == Pixel::NV12 {
+ let uv_stats = plane_delta(actual, expected, 1, cw * 2, ch);
+ assert!(
+ uv_stats.max_diff <= 3 && uv_stats.mean < 1.5,
+ "{width}x{height} nv12 max {} mean {:.3} at {},{}",
+ uv_stats.max_diff,
+ uv_stats.mean,
+ uv_stats.col,
+ uv_stats.row
+ );
+ } else {
+ let u_stats = plane_delta(actual, expected, 1, cw, ch);
+ let v_stats = plane_delta(actual, expected, 2, cw, ch);
+ assert!(
+ u_stats.max_diff <= 3
+ && v_stats.max_diff <= 3
+ && u_stats.mean < 1.5
+ && v_stats.mean < 1.5,
+ "{width}x{height} chroma u max {} mean {:.3} at {},{} v max {} mean {:.3} at {},{}",
+ u_stats.max_diff,
+ u_stats.mean,
+ u_stats.col,
+ u_stats.row,
+ v_stats.max_diff,
+ v_stats.mean,
+ v_stats.col,
+ v_stats.row
+ );
+ }
+ }
+
+ fn assert_active_eq(actual: &frame::Video, expected: &frame::Video) {
+ for plane in 0..expected.planes() {
+ let row_bytes = if expected.format() == Pixel::NV12 && plane == 1 {
+ expected.width() as usize
+ } else {
+ expected.plane_width(plane) as usize
+ };
+ for row in 0..expected.plane_height(plane) as usize {
+ let got = &actual.data(plane)[row * actual.stride(plane)..][..row_bytes];
+ let want = &expected.data(plane)[row * expected.stride(plane)..][..row_bytes];
+ assert_eq!(got, want, "plane {plane} row {row}");
+ }
+ }
+ }
+
+ fn check_even(format: Pixel, width: u32, height: u32, dst_format: Pixel) {
+ let mut src = frame::Video::new(format, width, height);
+ fill_packed(&mut src);
+ let (y, u, v) = reference(&src);
+ let mut kernel = frame::Video::new(dst_format, width, height);
+ let mut sws = frame::Video::new(dst_format, width, height);
+ let mut scaler = fast_scaler(format, dst_format, width, height);
+ scaler.run(&src, &mut sws).unwrap();
+ if avx2_ready() {
+ assert!(
+ try_convert(&src, &mut kernel),
+ "{format:?} {width}x{height} -> {dst_format:?}"
+ );
+ assert_matches_reference(&kernel, &y, &u, &v);
+ assert_within_swscale(&kernel, &sws);
+ let mut via = frame::Video::new(dst_format, width, height);
+ let mut via_scaler = fast_scaler(format, dst_format, width, height);
+ convert_or_scale(&src, &mut via, &mut via_scaler).unwrap();
+ assert_active_eq(&via, &kernel);
+ } else {
+ assert!(!try_convert(&src, &mut kernel));
+ let mut via = frame::Video::new(dst_format, width, height);
+ let mut via_scaler = fast_scaler(format, dst_format, width, height);
+ convert_or_scale(&src, &mut via, &mut via_scaler).unwrap();
+ assert_active_eq(&via, &sws);
+ }
+ }
+
+ #[test]
+ fn even_uyvy_and_yuyv_match_reference_and_swscale() {
+ ffmpeg::init().unwrap();
+ let sizes = [
+ (2u32, 2u32),
+ (16, 2),
+ (18, 4),
+ (32, 8),
+ (48, 6),
+ (64, 48),
+ (66, 40),
+ (320, 180),
+ ];
+ for format in [Pixel::UYVY422, Pixel::YUYV422] {
+ for (width, height) in sizes {
+ check_even(format, width, height, Pixel::YUV420P);
+ }
+ }
+ }
+
+ #[test]
+ fn even_nv12_matches_where_the_encoder_outputs_nv12() {
+ ffmpeg::init().unwrap();
+ for format in [Pixel::UYVY422, Pixel::YUYV422] {
+ for (width, height) in [(16u32, 2u32), (18, 4), (64, 48), (66, 40)] {
+ check_even(format, width, height, Pixel::NV12);
+ }
+ }
+ }
+
+ #[test]
+ fn odd_sizes_fall_back_to_swscale() {
+ ffmpeg::init().unwrap();
+ let mut compared = 0u32;
+ for format in [Pixel::UYVY422, Pixel::YUYV422] {
+ for (width, height) in [
+ (2u32, 1u32),
+ (64, 63),
+ (32, 15),
+ (1, 2),
+ (65, 64),
+ (31, 17),
+ (65, 63),
+ ] {
+ let mut src = frame::Video::new(format, width, height);
+ fill_packed(&mut src);
+ let mut probe = frame::Video::new(Pixel::YUV420P, width, height);
+ assert!(
+ !try_convert(&src, &mut probe),
+ "{format:?} {width}x{height} should stay on swscale"
+ );
+ let Ok(mut via_scaler) = scaling::Context::get(
+ format,
+ width,
+ height,
+ Pixel::YUV420P,
+ width,
+ height,
+ Flags::FAST_BILINEAR,
+ ) else {
+ continue;
+ };
+ let Ok(mut direct_scaler) = scaling::Context::get(
+ format,
+ width,
+ height,
+ Pixel::YUV420P,
+ width,
+ height,
+ Flags::FAST_BILINEAR,
+ ) else {
+ continue;
+ };
+ let mut via = frame::Video::new(Pixel::YUV420P, width, height);
+ let mut direct = frame::Video::new(Pixel::YUV420P, width, height);
+ // av_frame_get_buffer does not clear memory, and swscale can
+ // leave the odd-height chroma tail untouched.
+ zero_frame(&mut via);
+ zero_frame(&mut direct);
+ convert_or_scale(&src, &mut via, &mut via_scaler).unwrap();
+ direct_scaler.run(&src, &mut direct).unwrap();
+ assert_active_eq(&via, &direct);
+ compared += 1;
+ }
+ }
+ assert!(compared > 0, "swscale rejected every odd size");
+ }
+
+ #[test]
+ fn scaling_and_other_formats_stay_on_swscale() {
+ ffmpeg::init().unwrap();
+ let src = frame::Video::new(Pixel::UYVY422, 64, 48);
+ let mut scaled = frame::Video::new(Pixel::YUV420P, 32, 24);
+ assert!(!try_convert(&src, &mut scaled));
+ let mut yuv422 = frame::Video::new(Pixel::YUV422P, 64, 48);
+ assert!(!try_convert(&src, &mut yuv422));
+ for format in [Pixel::BGRA, Pixel::YUV422P, Pixel::NV12, Pixel::RGB24] {
+ let other = frame::Video::new(format, 32, 16);
+ let mut yuv = frame::Video::new(Pixel::YUV420P, 32, 16);
+ assert!(!try_convert(&other, &mut yuv), "{format:?}");
+ }
+
+ let mut packed = frame::Video::new(Pixel::YUYV422, 64, 48);
+ fill_packed(&mut packed);
+ let mut scaler = scaling::Context::get(
+ Pixel::YUYV422,
+ 64,
+ 48,
+ Pixel::YUV420P,
+ 32,
+ 24,
+ Flags::BICUBIC,
+ )
+ .unwrap();
+ let mut direct_scaler = scaling::Context::get(
+ Pixel::YUYV422,
+ 64,
+ 48,
+ Pixel::YUV420P,
+ 32,
+ 24,
+ Flags::BICUBIC,
+ )
+ .unwrap();
+ let mut via = frame::Video::new(Pixel::YUV420P, 32, 24);
+ let mut direct = frame::Video::new(Pixel::YUV420P, 32, 24);
+ convert_or_scale(&packed, &mut via, &mut scaler).unwrap();
+ direct_scaler.run(&packed, &mut direct).unwrap();
+ assert_active_eq(&via, &direct);
+ }
+
+ #[test]
+ fn libx264_queue_accepts_packed_yuv() {
+ ffmpeg::init().unwrap();
+ queue_one(Pixel::UYVY422);
+ encode_one(Pixel::YUYV422);
+ }
+
+ fn queue_one(format: Pixel) {
+ if ffmpeg::codec::encoder::find_by_name("libx264").is_none() {
+ return;
+ }
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("packed.mp4");
+ let mut output = ffmpeg::format::output_as(&path, "mp4").unwrap();
+ let info = cap_media_info::VideoInfo::from_raw_ffmpeg(format, 64, 48, 30);
+ let mut encoder = super::super::h264::H264Encoder::builder(info)
+ .with_preset(super::super::h264::H264Preset::Ultrafast)
+ .with_crf(18)
+ .with_encoder_priority_override(&["libx264"])
+ .build(&mut output)
+ .unwrap();
+ let requirements = encoder.conversion_requirements();
+ assert_eq!(requirements.output_format, Pixel::YUV420P);
+ assert!(requirements.needs_conversion);
+ let mut frame = frame::Video::new(format, 64, 48);
+ fill_packed(&mut frame);
+ let mut sink = crate::PacketQueue::new(ffmpeg::Rational(1, 90_000));
+ encoder
+ .queue_frame_to(frame, std::time::Duration::ZERO, &mut sink)
+ .unwrap();
+ encoder.flush_to(&mut sink).unwrap();
+ assert!(!sink.take().is_empty());
+ }
+
+ fn encode_one(format: Pixel) {
+ if ffmpeg::codec::encoder::find_by_name("libx264").is_none() {
+ return;
+ }
+ let info = cap_media_info::VideoInfo::from_raw_ffmpeg(format, 64, 48, 30);
+ let mut encoder = super::super::h264::H264Encoder::builder(info)
+ .with_preset(super::super::h264::H264Preset::Ultrafast)
+ .with_crf(18)
+ .with_encoder_priority_override(&["libx264"])
+ .build_standalone()
+ .unwrap();
+ assert_eq!(encoder.output_format(), Pixel::YUV420P);
+ assert!(encoder.needs_conversion());
+ let mut frame = frame::Video::new(format, 64, 48);
+ fill_packed(&mut frame);
+ let mut packets = Vec::new();
+ encoder
+ .encode_frame(frame, std::time::Duration::ZERO, |packet| {
+ packets.push(packet.data);
+ Ok(())
+ })
+ .unwrap();
+ encoder
+ .flush(|packet| {
+ packets.push(packet.data);
+ Ok(())
+ })
+ .unwrap();
+ assert!(!packets.is_empty());
+ }
+
+ #[test]
+ #[ignore = "release timing bench, run with --ignored --nocapture"]
+ fn bench_packed_yuv_conversion() {
+ ffmpeg::init().unwrap();
+ let model = std::fs::read_to_string("/proc/cpuinfo")
+ .ok()
+ .and_then(|text| {
+ text.lines()
+ .find(|line| line.starts_with("model name"))
+ .and_then(|line| line.split(':').nth(1))
+ .map(|value| value.trim().to_string())
+ })
+ .unwrap_or_else(|| "unknown".into());
+ println!("cpu: {model}");
+ println!("avx2: {}", avx2_ready());
+ println!("warmup 5 excluded; median is the middle of 31 sorted samples");
+ for format in [Pixel::UYVY422, Pixel::YUYV422] {
+ for (width, height) in [(1280u32, 720u32), (1920, 1080), (3840, 2160)] {
+ bench_conversion(format, width, height);
+ }
+ }
+ }
+
+ fn bench_conversion(format: Pixel, width: u32, height: u32) {
+ let mut src = frame::Video::new(format, width, height);
+ fill_packed(&mut src);
+ let mut kernel_dst = frame::Video::new(Pixel::YUV420P, width, height);
+ let mut sws_dst = frame::Video::new(Pixel::YUV420P, width, height);
+ let mut kernel_scaler = fast_scaler(format, Pixel::YUV420P, width, height);
+ let mut sws_scaler = fast_scaler(format, Pixel::YUV420P, width, height);
+ let sws = time_median(5, 31, || {
+ sws_scaler.run(&src, &mut sws_dst).unwrap();
+ let _ = std::hint::black_box(sws_dst.data(0));
+ let _ = std::hint::black_box(sws_dst.data(1));
+ let _ = std::hint::black_box(sws_dst.data(2));
+ });
+ let kernel = time_median(5, 31, || {
+ convert_or_scale(&src, &mut kernel_dst, &mut kernel_scaler).unwrap();
+ let _ = std::hint::black_box(kernel_dst.data(0));
+ let _ = std::hint::black_box(kernel_dst.data(1));
+ let _ = std::hint::black_box(kernel_dst.data(2));
+ });
+ println!(
+ "{format:?} {width}x{height} swscale_fast_bilinear median {:.3} ms n=31",
+ sws / 1_000_000.0
+ );
+ println!(
+ "{format:?} {width}x{height} convert_or_scale median {:.3} ms n=31 speedup {:.2}x",
+ kernel / 1_000_000.0,
+ sws / kernel
+ );
+ }
+
+ fn time_median(warmup: usize, runs: usize, mut body: impl FnMut()) -> f64 {
+ for _ in 0..warmup {
+ body();
+ }
+ let mut samples = Vec::with_capacity(runs);
+ for _ in 0..runs {
+ let started = std::time::Instant::now();
+ body();
+ samples.push(started.elapsed().as_nanos());
+ }
+ samples.sort_unstable();
+ samples[runs / 2] as f64
+ }
+}
diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml
index b1cae02e4..9a214783f 100644
--- a/crates/export/Cargo.toml
+++ b/crates/export/Cargo.toml
@@ -3,6 +3,10 @@ name = "cap-export"
version = "0.1.0"
edition = "2024"
+[features]
+# Encoder failure injection for the cap-verify suite. Never enabled by the app.
+test-hooks = []
+
[dependencies]
cap-utils = { path = "../utils" }
cap-project = { path = "../project" }
diff --git a/crates/export/src/estimates.rs b/crates/export/src/estimates.rs
index 99bd390dd..3d245e931 100644
--- a/crates/export/src/estimates.rs
+++ b/crates/export/src/estimates.rs
@@ -289,6 +289,8 @@ pub async fn estimate_export(
audio_cancellation: None,
sample_windows: Some(windows.clone()),
sample_timing: Some(timing.clone()),
+ #[cfg(feature = "test-hooks")]
+ video_encoder_failure_at: None,
};
let progress_editor = editor.clone();
let progress_cancel = cancel.clone();
diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs
index 86b5514ed..805ace277 100644
--- a/crates/export/src/lib.rs
+++ b/crates/export/src/lib.rs
@@ -120,6 +120,7 @@ impl ExporterBuilder {
) -> Result<ExporterBase, ExporterBuildError> {
type Error = ExporterBuildError;
+ cap_editor::release_playback_audio_caches();
let mut project_config = self.load_project_config()?;
let recording_meta =
@@ -167,6 +168,12 @@ impl ExporterBuilder {
let output_path = self
.output_path
.unwrap_or_else(|| recording_meta.output_path());
+
+ if let Some(parent) = output_path.parent() {
+ std::fs::create_dir_all(parent)
+ .map_err(|e| ExporterBuildError::IO(parent.to_path_buf(), e))?;
+ }
+
let streaming_output = prepare_streaming_output(
&output_path,
cancellation.is_some() && ExportAudioRenderer::eligible(&project_config, studio_meta),
@@ -235,11 +242,6 @@ impl ExporterBuilder {
}
}
- if let Some(parent) = output_path.parent() {
- std::fs::create_dir_all(parent)
- .map_err(|e| ExporterBuildError::IO(parent.to_path_buf(), e))?;
- }
-
Ok(ExporterBase {
output_path,
studio_meta: studio_meta.clone(),
@@ -254,6 +256,8 @@ impl ExporterBuilder {
audio_cancellation,
sample_windows: None,
sample_timing: None,
+ #[cfg(feature = "test-hooks")]
+ video_encoder_failure_at: None,
})
}
}
@@ -440,6 +444,11 @@ impl Mp4ExporterBase {
pub fn uses_streaming_audio(&self) -> bool {
self.0.streaming_audio.is_some()
}
+
+ #[cfg(feature = "test-hooks")]
+ pub fn fail_video_encoder_at(&mut self, frames: u32) {
+ self.0.fail_video_encoder_at(frames);
+ }
}
pub struct ExporterBase {
@@ -456,9 +465,55 @@ pub struct ExporterBase {
audio_cancellation: Option<ExportAudioCancellation>,
sample_windows: Option<cap_rendering::FrameWindows>,
sample_timing: Option<Arc<estimates::SampleTiming>>,
+ #[cfg(feature = "test-hooks")]
+ video_encoder_failure_at: Option<u32>,
}
impl ExporterBase {
+ /// Makes the MP4 export's video encoder fail after `frames` frames the
+ /// way a hardware encoder does at runtime, once, so the software
+ /// encoder fallback runs on every machine.
+ #[cfg(feature = "test-hooks")]
+ pub fn fail_video_encoder_at(&mut self, frames: u32) {
+ self.video_encoder_failure_at = Some(frames);
+ }
+
+ /// Reopens what an MP4 export consumes (the staged output and the
+ /// streaming audio with its cancellation) so the export can start over.
+ async fn reopen_streaming(
+ &mut self,
+ user_cancellation: Option<Arc<AtomicBool>>,
+ ) -> Result<(), String> {
+ let Some(user) = user_cancellation else {
+ return Ok(());
+ };
+ let control = ExportAudioCancellation::new(user);
+ let recording = self.recording_meta.clone();
+ let studio = self.studio_meta.clone();
+ let cancellation = control.user.clone();
+ let abort = control.stop.clone();
+ let preparation = tokio::task::spawn_blocking(move || {
+ ExportAudioPreparation::open(&recording, &studio, cancellation, abort)
+ })
+ .await
+ .map_err(|error| error.to_string())?
+ .map_err(|error| error.to_string())?;
+ self.streaming_audio = Some(
+ preparation
+ .finish(&self.segments)
+ .map_err(|error| error.to_string())?,
+ );
+ // The first attempt staged its output, so the restart must too: if
+ // something created the destination meanwhile, writing in place
+ // would truncate it.
+ self.streaming_output = Some(
+ prepare_streaming_output(&self.output_path, true)
+ .ok_or("Could not stage the export output to restart on the software encoder")?,
+ );
+ self.audio_cancellation = Some(control);
+ Ok(())
+ }
+
pub fn total_frames(&self, fps: u32) -> u32 {
let duration = cap_rendering::get_duration(
&self.recordings,
@@ -799,6 +854,7 @@ mod clip_offset_tests {
start_time: Some(start),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
};
StudioRecordingMeta::MultipleSegments {
inner: MultipleSegments {
diff --git a/crates/export/src/mov.rs b/crates/export/src/mov.rs
index a71d1da8a..c26d17dc3 100644
--- a/crates/export/src/mov.rs
+++ b/crates/export/src/mov.rs
@@ -5,9 +5,9 @@ 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};
+use crate::{ExportError, ExporterBase, mp4::temporary_export_output};
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Type)]
pub struct MovExportSettings {
@@ -79,13 +79,17 @@ 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 = 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 mut mov_encoder = MOVFile::init(mov_output_path.clone(), |output| {
+ let _temporary_output_guard = encoder_temporary_output;
+ let mut mov_encoder = MOVFile::init(encoder_output_path, |output| {
ProResEncoder::builder(video_info).build(output)
})
.map_err(|e| ExportError::Other(format!("Failed to create MOV encoder: {e}")))?;
@@ -131,7 +135,7 @@ impl MovExportSettings {
.finish()
.map_err(|e| ExportError::Other(format!("Failed to finish MOV: {e}")))?;
- Ok(mov_output_path)
+ Ok(())
})
.then(|f| async {
f.map_err(|e| e.to_string())
@@ -160,10 +164,15 @@ impl MovExportSettings {
)
.then(|f| async { f.map_err(|v| v.to_string()) });
- let (output_path, _) =
- tokio::try_join!(encoder_thread, render_video_task).map_err(|e| e.to_string())?;
+ let (encoder_result, render_result) = tokio::join!(encoder_thread, render_video_task);
+ encoder_result?;
+ render_result?;
+ 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())?;
- Ok(output_path)
+ Ok(mov_output_path)
}
}
diff --git a/crates/export/src/mp4.rs b/crates/export/src/mp4.rs
index 8395ed740..be25ee2e6 100644
--- a/crates/export/src/mp4.rs
+++ b/crates/export/src/mp4.rs
@@ -16,7 +16,7 @@ use specta::Type;
use std::{
path::PathBuf,
sync::{
- Arc, Mutex,
+ Arc, Mutex, PoisonError,
atomic::{AtomicBool, AtomicU64, Ordering},
},
time::Duration,
@@ -169,6 +169,7 @@ impl ExportCompression {
#[derive(Clone, Default)]
struct ExportNv12Mode {
stop_after_frames_sent: Option<u32>,
+ software_encoder: bool,
record_first_queued_ms_since_pipeline: Option<Arc<AtomicU64>>,
nv12_render_startup_breakdown_ms:
Option<Arc<Mutex<Option<cap_rendering::Nv12RenderStartupBreakdownMs>>>>,
@@ -294,6 +295,7 @@ impl Mp4ExportSettings {
|_| true,
ExportNv12Mode {
stop_after_frames_sent: Some(1),
+ software_encoder: false,
record_first_queued_ms_since_pipeline: Some(first_ms_enc),
nv12_render_startup_breakdown_ms: Some(breakdown_enc),
},
@@ -326,7 +328,64 @@ impl Mp4ExportSettings {
output_size: (u32, u32),
fps: u32,
on_progress: impl FnMut(u32) -> bool + Send + 'static,
+ mut mode: ExportNv12Mode,
+ ) -> Result<PathBuf, String> {
+ let on_progress = Arc::new(Mutex::new(on_progress));
+ let user_cancellation = base
+ .audio_cancellation
+ .as_ref()
+ .map(|control| control.user.clone());
+ loop {
+ let progress = on_progress.clone();
+ let hardware_encoder_failed = Arc::new(AtomicBool::new(false));
+ let result = self
+ .export_nv12_attempt(
+ &mut base,
+ output_size,
+ fps,
+ move |frame| {
+ let mut on_progress =
+ progress.lock().unwrap_or_else(PoisonError::into_inner);
+ (*on_progress)(frame)
+ },
+ mode.clone(),
+ hardware_encoder_failed.clone(),
+ )
+ .await;
+ // A hardware encoder can fail mid-stream when the media engine is
+ // busy or resets. The MP4 cannot switch codecs partway, so the
+ // export starts over once with the software encoder.
+ match result {
+ Err(error)
+ if restart_on_software_encoder(
+ mode.software_encoder,
+ base.sample_timing.is_some(),
+ hardware_encoder_failed.load(Ordering::Relaxed),
+ user_cancellation
+ .as_ref()
+ .is_some_and(|cancel| cancel.load(Ordering::Relaxed)),
+ ) =>
+ {
+ warn!(
+ %error,
+ "Hardware video encoder failed during export, restarting with the software encoder"
+ );
+ base.reopen_streaming(user_cancellation.clone()).await?;
+ mode.software_encoder = true;
+ }
+ result => return result,
+ }
+ }
+ }
+
+ async fn export_nv12_attempt(
+ self,
+ base: &mut ExporterBase,
+ output_size: (u32, u32),
+ fps: u32,
+ on_progress: impl FnMut(u32) -> bool + Send + 'static,
mode: ExportNv12Mode,
+ hardware_encoder_failed: Arc<AtomicBool>,
) -> Result<PathBuf, String> {
let pipeline_start = std::time::Instant::now();
let sample_timing = base.sample_timing.clone();
@@ -379,6 +438,9 @@ impl Mp4ExportSettings {
let has_audio = has_recording_audio || !music.is_empty();
let record_first_queued_ms = mode.record_first_queued_ms_since_pipeline;
+ let software_encoder = mode.software_encoder;
+ #[cfg(feature = "test-hooks")]
+ let injected_failure_at = base.video_encoder_failure_at.filter(|_| !software_encoder);
let nv12_render_startup_breakdown_ms = mode.nv12_render_startup_breakdown_ms;
let project_for_audio = base.project_config.clone();
@@ -401,6 +463,7 @@ impl Mp4ExportSettings {
}
#[cfg(target_os = "macos")]
let want_hw_input = !self.optimize_filesize
+ && !software_encoder
&& matches!(
first_frame.as_ref().map(|frame| &frame.payload),
Some(ExportFramePayload::Surface(_))
@@ -442,6 +505,8 @@ impl Mp4ExportSettings {
.with_external_conversion();
let builder = if self.optimize_filesize {
builder.with_crf(self.compression.crf_value())
+ } else if software_encoder {
+ builder.with_encoder_priority_override(&["libx264"])
} else {
builder
};
@@ -465,8 +530,17 @@ impl Mp4ExportSettings {
encoder.video_mut().set_packet_stats(timing.packet_stats());
}
+ let hardware_video = encoder.video().is_hardware();
+ // The injected failure stands in for a hardware encoder's.
+ #[cfg(feature = "test-hooks")]
+ let hardware_video = hardware_video || injected_failure_at.is_some();
+ let video_failure = |error: String| {
+ video_encoder_failure(hardware_video, &hardware_encoder_failed, error)
+ };
+
info!(
zero_copy_input = encoder_is_hw,
+ hardware_video,
"Created MP4File encoder (NV12, export settings)"
);
@@ -535,7 +609,7 @@ impl Mp4ExportSettings {
&mut converted_frame,
Duration::MAX,
)
- .map_err(|err| err.to_string())?;
+ .map_err(|err| video_failure(err.to_string()))?;
}
#[cfg(target_os = "macos")]
ExportFramePayload::Surface(surface) => {
@@ -545,7 +619,7 @@ impl Mp4ExportSettings {
.video_encoder()
.wrap_videotoolbox_pixel_buffer(surface.as_pixel_buffer_ptr())
}
- .map_err(|err| err.to_string())?;
+ .map_err(|err| video_failure(err.to_string()))?;
frame.set_pts(Some(input.frame_number as i64));
encoder
.queue_video_frame_reusable(
@@ -553,7 +627,7 @@ impl Mp4ExportSettings {
&mut converted_frame,
Duration::MAX,
)
- .map_err(|err| err.to_string())?;
+ .map_err(|err| video_failure(err.to_string()))?;
} else {
fill_nv12_frame_from_surface(
&mut reusable_frame,
@@ -571,7 +645,7 @@ impl Mp4ExportSettings {
&mut converted_frame,
Duration::MAX,
)
- .map_err(|err| err.to_string())?;
+ .map_err(|err| video_failure(err.to_string()))?;
}
}
}
@@ -579,6 +653,13 @@ impl Mp4ExportSettings {
worker.write_ready(&mut encoder)?;
}
encoded_frames += 1;
+ #[cfg(feature = "test-hooks")]
+ if injected_failure_at == Some(encoded_frames) {
+ return Err(video_failure(
+ cap_enc_ffmpeg::h264::QueueFrameError::Encode(ffmpeg::Error::External)
+ .to_string(),
+ ));
+ }
if let Some(timing) = &sample_timing {
timing.record_frame(input.timeline_frame);
}
@@ -617,7 +698,7 @@ impl Mp4ExportSettings {
.map_err(|e| format!("Failed to finish encoding: {e}"))?;
if let Err(e) = res.video_finish {
- return Err(Mp4PipelineError::Failure(format!("Video encoding failed: {e}")));
+ return Err(video_failure(format!("Video encoding failed: {e}")));
}
if let Err(e) = res.audio_finish {
return Err(Mp4PipelineError::Failure(format!("Audio encoding failed: {e}")));
@@ -735,6 +816,27 @@ impl Mp4ExportSettings {
}
}
+/// A video encoder error that fails this export attempt. A hardware
+/// encoder's error also sets `restart`, so the export starts over on libx264.
+fn video_encoder_failure(hardware: bool, restart: &AtomicBool, error: String) -> Mp4PipelineError {
+ if hardware {
+ restart.store(true, Ordering::Relaxed);
+ }
+ Mp4PipelineError::Failure(error)
+}
+
+/// Whether a failed attempt starts over on the software encoder: after a
+/// hardware encoder error, once, and not for size-estimate samples or a
+/// cancelled export.
+fn restart_on_software_encoder(
+ attempt_was_software: bool,
+ sampled: bool,
+ hardware_encoder_failed: bool,
+ cancelled: bool,
+) -> bool {
+ hardware_encoder_failed && !attempt_was_software && !sampled && !cancelled
+}
+
enum ExportFramePayload {
Cpu(SharedNv12Buffer),
#[cfg(target_os = "macos")]
@@ -1158,25 +1260,35 @@ impl TemporaryExportOutput {
self.path.to_path_buf()
}
+ #[cfg(unix)]
+ fn restore_permissions(&self, output: Option<&std::path::Path>) -> std::io::Result<()> {
+ use std::os::unix::fs::MetadataExt;
+ let path_metadata = std::fs::symlink_metadata(&self.path)?;
+ let file_metadata = self.file.metadata()?;
+ if !path_metadata.is_file()
+ || path_metadata.dev() != file_metadata.dev()
+ || path_metadata.ino() != file_metadata.ino()
+ {
+ return Err(std::io::Error::other("Export temporary file was replaced"));
+ }
+ let existing_permissions = match output {
+ Some(path) => match std::fs::symlink_metadata(path) {
+ Ok(metadata) if metadata.is_file() => Some(metadata.permissions()),
+ Err(error) if error.kind() != std::io::ErrorKind::NotFound => return Err(error),
+ _ => None,
+ },
+ None => None,
+ };
+ self.file
+ .set_permissions(existing_permissions.unwrap_or_else(|| self.final_permissions.clone()))
+ }
+
pub(crate) fn persist_noclobber(
self,
output: &std::path::Path,
) -> Result<(), tempfile::PathPersistError> {
#[cfg(unix)]
- let prepare_permissions = || {
- use std::os::unix::fs::MetadataExt;
- let path_metadata = std::fs::symlink_metadata(&self.path)?;
- let file_metadata = self.file.metadata()?;
- if !path_metadata.is_file()
- || path_metadata.dev() != file_metadata.dev()
- || path_metadata.ino() != file_metadata.ino()
- {
- return Err(std::io::Error::other("Export temporary file was replaced"));
- }
- self.file.set_permissions(self.final_permissions.clone())
- };
- #[cfg(unix)]
- if let Err(error) = prepare_permissions() {
+ if let Err(error) = self.restore_permissions(None) {
return Err(tempfile::PathPersistError {
error,
path: self.path,
@@ -1184,6 +1296,20 @@ impl TemporaryExportOutput {
}
self.path.persist_noclobber(output)
}
+
+ pub(crate) fn persist_replace(
+ self,
+ output: &std::path::Path,
+ ) -> Result<(), tempfile::PathPersistError> {
+ #[cfg(unix)]
+ if let Err(error) = self.restore_permissions(Some(output)) {
+ return Err(tempfile::PathPersistError {
+ error,
+ path: self.path,
+ });
+ }
+ self.path.persist(output)
+ }
}
pub(crate) fn temporary_export_output(
@@ -1606,6 +1732,40 @@ async fn export_render_to_channel(
mod tests {
use super::*;
+ #[test]
+ fn hardware_encoder_errors_restart_on_software() {
+ let queue = cap_enc_ffmpeg::h264::QueueFrameError::Encode(ffmpeg::Error::External);
+ let flush = format!("Video encoding failed: {}", ffmpeg::Error::External);
+ for error in [queue.to_string(), flush] {
+ let restart = AtomicBool::new(false);
+ let failure = video_encoder_failure(true, &restart, error.clone());
+ assert_eq!(failure.message(), error);
+ assert!(restart_on_software_encoder(
+ false,
+ false,
+ restart.load(Ordering::Relaxed),
+ false
+ ));
+ }
+ }
+
+ #[test]
+ fn software_encoder_errors_do_not_restart() {
+ let restart = AtomicBool::new(false);
+ let error = cap_enc_ffmpeg::h264::QueueFrameError::Encode(ffmpeg::Error::External);
+ video_encoder_failure(false, &restart, error.to_string());
+ assert!(!restart_on_software_encoder(
+ false,
+ false,
+ restart.load(Ordering::Relaxed),
+ false
+ ));
+ // Already on libx264, a size-estimate sample, or cancelled.
+ assert!(!restart_on_software_encoder(true, false, true, false));
+ assert!(!restart_on_software_encoder(false, true, true, false));
+ assert!(!restart_on_software_encoder(false, false, true, true));
+ }
+
fn sum_samples(sample_rate: u64, fps: u64, frames: u64) -> u64 {
(0..frames)
.map(|n| {
diff --git a/crates/export/src/preview.rs b/crates/export/src/preview.rs
index c092ec0fd..09e1172af 100644
--- a/crates/export/src/preview.rs
+++ b/crates/export/src/preview.rs
@@ -100,6 +100,7 @@ struct PreviewSource<'a> {
render_constants: &'a RenderVideoConstants,
segments: Vec<PreviewSegment<'a>>,
total_duration: f64,
+ layers_cache: Option<&'a std::sync::Mutex<Option<RendererLayers>>>,
}
struct PreviewSegment<'a> {
@@ -130,6 +131,7 @@ async fn render_preview_with_base(
})
.collect(),
total_duration,
+ layers_cache: None,
};
render_preview_frame(source, frame_time, settings).await
}
@@ -184,6 +186,7 @@ pub async fn render_preview_with_editor(
})
.collect(),
total_duration,
+ layers_cache: Some(&editor.preview_layers),
};
render_preview_frame(source, frame_time, settings).await
}
@@ -219,6 +222,7 @@ async fn render_preview_frame(
render_constants,
segments,
total_duration,
+ layers_cache,
} = source;
let project_config = &Arc::new(project_config.clone());
let transition_mapping = project_config.timeline.as_ref().and_then(|timeline| {
@@ -286,11 +290,22 @@ async fn render_preview_frame(
);
let mut frame_renderer = FrameRenderer::new(render_constants);
- let mut layers = RendererLayers::new_with_options(
- &render_constants.device,
- &render_constants.queue,
- render_constants.is_software_adapter,
- );
+ let mut layers = layers_cache
+ .and_then(|cache| {
+ let mut guard = cache.lock().ok()?;
+ guard.take()
+ })
+ .map(|mut cached| {
+ cached.reset_frame_state();
+ cached
+ })
+ .unwrap_or_else(|| {
+ RendererLayers::new_with_options(
+ &render_constants.device,
+ &render_constants.queue,
+ render_constants.is_software_adapter,
+ )
+ });
let frame = if let Some((outgoing, kind, progress)) = transition_mapping {
let outgoing_media = segments
@@ -392,6 +407,12 @@ async fn render_preview_frame(
(total_bitrate * encoder_efficiency * duration_seconds) / (8.0 * 1024.0 * 1024.0)
};
+ if let Some(cache) = layers_cache
+ && let Ok(mut guard) = cache.lock()
+ {
+ *guard = Some(layers);
+ }
+
Ok(ExportPreviewResult {
jpeg_base64: STANDARD.encode(&jpeg_buffer),
estimated_size_mb,
diff --git a/crates/export/tests/mov_cancellation.rs b/crates/export/tests/mov_cancellation.rs
new file mode 100644
index 000000000..e6f8f47c4
--- /dev/null
+++ b/crates/export/tests/mov_cancellation.rs
@@ -0,0 +1,205 @@
+use cap_export::{ExporterBase, mov::MovExportSettings};
+use cap_project::{ProjectConfiguration, XY};
+use std::{fs, path::Path, process::Command};
+
+fn create_project(path: &Path) {
+ fs::create_dir_all(path).unwrap();
+ let video = Command::new("ffmpeg")
+ .args([
+ "-v",
+ "error",
+ "-f",
+ "lavfi",
+ "-i",
+ "testsrc2=size=64x64:rate=10:duration=1",
+ "-c:v",
+ "libx264",
+ "-preset",
+ "ultrafast",
+ "-threads",
+ "1",
+ ])
+ .arg(path.join("display.mp4"))
+ .output()
+ .unwrap();
+ assert!(
+ video.status.success(),
+ "{}",
+ String::from_utf8_lossy(&video.stderr)
+ );
+ fs::write(
+ path.join("recording-meta.json"),
+ serde_json::to_vec(&serde_json::json!({
+ "pretty_name": "MOV cancellation",
+ "display": { "path": "display.mp4", "fps": 10 }
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+}
+
+fn assert_playable_mov(path: &Path) {
+ let decoded = Command::new("ffmpeg")
+ .args(["-v", "error", "-i"])
+ .arg(path)
+ .args(["-f", "null", "-"])
+ .output()
+ .unwrap();
+ assert!(
+ decoded.status.success(),
+ "{}",
+ String::from_utf8_lossy(&decoded.stderr)
+ );
+}
+
+async fn export(
+ project: &Path,
+ output: &Path,
+ on_progress: impl FnMut(u32) -> bool + Send + 'static,
+) -> Result<std::path::PathBuf, String> {
+ let base = Box::pin(
+ ExporterBase::builder(project.to_path_buf())
+ .with_config(ProjectConfiguration::default())
+ .with_output_path(output.to_path_buf())
+ .build_video_only(),
+ )
+ .await
+ .unwrap();
+ Box::pin(
+ MovExportSettings {
+ fps: 10,
+ resolution_base: XY::new(64, 64),
+ cursor_only: true,
+ }
+ .export(base, on_progress),
+ )
+ .await
+}
+
+#[tokio::test]
+#[ignore = "requires ffmpeg and a wgpu adapter"]
+async fn cancelled_mov_preserves_existing_destination() {
+ 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();
+
+ assert!(export(&project, &output, |frame| frame < 2).await.is_err());
+
+ assert_eq!(fs::read(&output).unwrap(), b"existing export");
+ 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() {
+ 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();
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ fs::set_permissions(&output, fs::Permissions::from_mode(0o600)).unwrap();
+ }
+ let destination = output.clone();
+
+ let result = export(&project, &output, move |_| {
+ assert_eq!(fs::read(&destination).unwrap(), b"existing export");
+ true
+ })
+ .await
+ .unwrap();
+
+ assert_eq!(result, output);
+ assert_playable_mov(&output);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ assert_eq!(
+ fs::metadata(&output).unwrap().permissions().mode() & 0o777,
+ 0o600
+ );
+ }
+ assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 2);
+}
+
+#[tokio::test]
+#[ignore = "requires ffmpeg and a wgpu adapter"]
+async fn mov_reports_destination_replacement_failure() {
+ let directory = tempfile::tempdir().unwrap();
+ let project = directory.path().join("project.cap");
+ create_project(&project);
+ let output = directory.path().join("export.mov");
+ fs::create_dir(&output).unwrap();
+
+ assert!(export(&project, &output, |_| true).await.is_err());
+
+ assert!(output.is_dir());
+ assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 2);
+}
+
+#[tokio::test]
+#[ignore = "requires ffmpeg and a wgpu adapter"]
+async fn finished_mov_creates_new_destination() {
+ let directory = tempfile::tempdir().unwrap();
+ let project = directory.path().join("project.cap");
+ create_project(&project);
+ let output = directory.path().join("export.mov");
+ let destination = output.clone();
+
+ let result = export(&project, &output, move |_| {
+ assert!(!destination.exists());
+ true
+ })
+ .await
+ .unwrap();
+
+ assert_eq!(result, output);
+ assert_playable_mov(&output);
+ assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 2);
+}
diff --git a/crates/ffmpeg-hw-device/src/lib.rs b/crates/ffmpeg-hw-device/src/lib.rs
index 8691bebe3..0bfd452cb 100644
--- a/crates/ffmpeg-hw-device/src/lib.rs
+++ b/crates/ffmpeg-hw-device/src/lib.rs
@@ -1,6 +1,10 @@
use std::{
cell::Cell,
ptr::{null, null_mut},
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ },
};
use ffmpeg::{
@@ -9,16 +13,18 @@ use ffmpeg::{
sys::{
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX, AVBufferRef, AVCodecContext, AVCodecHWConfig,
AVHWDeviceType, AVPixelFormat, av_buffer_ref, av_buffer_unref, av_dict_free, av_dict_set,
- av_hwdevice_ctx_create, av_hwframe_transfer_data, avcodec_get_hw_config,
+ av_hwdevice_ctx_create, av_hwframe_transfer_data, avcodec_default_get_format,
+ avcodec_get_hw_config,
},
};
+use tracing::warn;
thread_local! {
static HW_PIX_FMT: Cell<AVPixelFormat> = const { Cell::new(AVPixelFormat::AV_PIX_FMT_NONE) };
}
unsafe extern "C" fn get_format(
- _: *mut AVCodecContext,
+ ctx: *mut AVCodecContext,
pix_fmts: *const AVPixelFormat,
) -> AVPixelFormat {
unsafe {
@@ -30,19 +36,50 @@ unsafe extern "C" fn get_format(
}
if *fmt == HW_PIX_FMT.get() {
+ set_software_fallback(ctx, false);
return *fmt;
}
fmt = fmt.offset(1);
}
- AVPixelFormat::AV_PIX_FMT_NONE
+ // When the hardware format's setup fails (a busy or reset media
+ // engine), FFmpeg drops it from the list and asks again. Choosing
+ // nothing here would fail every packet, so take FFmpeg's default
+ // software format and keep decoding on the CPU.
+ warn!(
+ hw_pix_fmt = ?HW_PIX_FMT.get(),
+ "Hardware decoder setup failed, falling back to software decode"
+ );
+ set_software_fallback(ctx, true);
+ avcodec_default_get_format(ctx, pix_fmts)
+ }
+}
+
+/// Records `get_format`'s latest choice in the [`HwDevice`] flag that
+/// [`install_get_format`] put in the codec context's `opaque`.
+unsafe fn set_software_fallback(ctx: *mut AVCodecContext, software: bool) {
+ unsafe {
+ if let Some(flag) = (*ctx).opaque.cast::<AtomicBool>().cast_const().as_ref() {
+ flag.store(software, Ordering::Relaxed);
+ }
+ }
+}
+
+/// Points the codec context at [`get_format`] and at the flag it records the
+/// chosen format in. The flag outlives the context as long as the context is
+/// freed before the [`HwDevice`] holding the flag.
+unsafe fn install_get_format(context: *mut AVCodecContext, software_fallback: &Arc<AtomicBool>) {
+ unsafe {
+ (*context).get_format = Some(get_format);
+ (*context).opaque = Arc::as_ptr(software_fallback).cast_mut().cast();
}
}
pub struct HwDevice {
pub device_type: AVHWDeviceType,
ctx: *mut AVBufferRef,
+ software_fallback: Arc<AtomicBool>,
}
fn linux_gpu_frames() -> bool {
@@ -51,6 +88,12 @@ fn linux_gpu_frames() -> bool {
}
impl HwDevice {
+ /// Whether FFmpeg took a software format because the hardware setup
+ /// failed, so the decoder is running on the CPU despite this device.
+ pub fn is_software_fallback(&self) -> bool {
+ self.software_fallback.load(Ordering::Relaxed)
+ }
+
pub fn get_hwframe(&self, src: &Video) -> Option<Video> {
// Linux GPU render hosts keep CUDA frames on the device; the renderer
// copies them device-to-device instead of via system memory.
@@ -131,13 +174,15 @@ impl CodecContextExt for codec::decoder::decoder::Decoder {
HW_PIX_FMT.set((*hw_config).pix_fmt);
let context = self.as_mut_ptr();
+ let software_fallback = Arc::new(AtomicBool::new(false));
- (*context).get_format = Some(get_format);
+ install_get_format(context, &software_fallback);
(*context).hw_device_ctx = av_buffer_ref(hw_device_ctx);
Ok(HwDevice {
device_type,
ctx: hw_device_ctx,
+ software_fallback,
})
}
}
@@ -194,3 +239,40 @@ impl CodecExt for codec::codec::Codec {
// }
// }
// }
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use ffmpeg::sys::{avcodec_alloc_context3, avcodec_free_context};
+
+ #[test]
+ fn get_format_falls_back_to_software_when_hardware_setup_fails() {
+ HW_PIX_FMT.set(AVPixelFormat::AV_PIX_FMT_VIDEOTOOLBOX);
+ let offered = [
+ AVPixelFormat::AV_PIX_FMT_VIDEOTOOLBOX,
+ AVPixelFormat::AV_PIX_FMT_YUV420P,
+ AVPixelFormat::AV_PIX_FMT_NONE,
+ ];
+ let device = HwDevice {
+ device_type: AVHWDeviceType::AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
+ ctx: null_mut(),
+ software_fallback: Arc::new(AtomicBool::new(false)),
+ };
+ unsafe {
+ let mut ctx = avcodec_alloc_context3(null());
+ install_get_format(ctx, &device.software_fallback);
+ assert_eq!(
+ get_format(ctx, offered.as_ptr()),
+ AVPixelFormat::AV_PIX_FMT_VIDEOTOOLBOX
+ );
+ assert!(!device.is_software_fallback());
+ // FFmpeg drops a hardware format whose setup failed and asks again.
+ assert_eq!(
+ get_format(ctx, offered[1..].as_ptr()),
+ AVPixelFormat::AV_PIX_FMT_YUV420P
+ );
+ assert!(device.is_software_fallback());
+ avcodec_free_context(&mut ctx);
+ }
+ }
+}
diff --git a/crates/project/src/caption_timing.rs b/crates/project/src/caption_timing.rs
index e1e5eb915..b6fb822de 100644
--- a/crates/project/src/caption_timing.rs
+++ b/crates/project/src/caption_timing.rs
@@ -205,14 +205,10 @@ fn map_captions_to_edited_timeline(
timeline: &TimelineConfiguration,
recording_durations: &[f64],
) -> Vec<MappedCaption> {
- let sanitized: Vec<CaptionSegment> = raw_segments
- .iter()
- .map(clamp_caption_segment_words)
- .collect();
-
if timeline.segments.is_empty() || recording_durations.is_empty() {
- return sanitized
- .into_iter()
+ return raw_segments
+ .iter()
+ .map(clamp_caption_segment_words)
.map(|segment| MappedCaption {
id: segment.id,
start: f64::from(segment.start),
@@ -238,7 +234,7 @@ fn map_captions_to_edited_timeline(
let mut result = Vec::new();
- for caption in &sanitized {
+ for caption in raw_segments {
let mut mapped_caption_segments: Vec<MappedCaption> = Vec::new();
let word_bounds = if caption.words.is_empty() {
None
@@ -247,7 +243,7 @@ fn map_captions_to_edited_timeline(
(f64::INFINITY, f64::NEG_INFINITY),
|(start, end), word| {
let word_start = f64::from(word.start);
- let word_end = f64::from(word.end);
+ let word_end = f64::from(word.end.min(word.start + MAX_CAPTION_WORD_DURATION));
if word_start.is_nan() || word_end.is_nan() {
None
} else {
@@ -269,7 +265,7 @@ fn map_captions_to_edited_timeline(
for word in &caption.words {
let Some((start, end)) = map_time_range_within_mapping(
f64::from(word.start),
- f64::from(word.end),
+ f64::from(word.end.min(word.start + MAX_CAPTION_WORD_DURATION)),
mapping,
) else {
continue;
@@ -446,6 +442,9 @@ pub fn synchronize_captions(project: &mut ProjectConfiguration, recording_durati
derive_caption_track_segments(&captions.segments, timeline, recording_durations);
}
+#[cfg(test)]
+mod projection_tests;
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/crates/project/src/caption_timing/projection_tests.rs b/crates/project/src/caption_timing/projection_tests.rs
new file mode 100644
index 000000000..dd7b09710
--- /dev/null
+++ b/crates/project/src/caption_timing/projection_tests.rs
@@ -0,0 +1,349 @@
+use super::*;
+
+fn assert_projection_matches(
+ captions: &[CaptionSegment],
+ timeline: &TimelineConfiguration,
+ durations: &[f64],
+) {
+ let expected = baseline_projection(captions, timeline, durations);
+ let actual = map_captions_to_edited_timeline(captions, timeline, durations);
+ assert_eq!(actual.len(), expected.len());
+ for (actual, expected) in actual.iter().zip(&expected) {
+ assert_eq!(actual.id, expected.id);
+ assert_eq!(actual.start.to_bits(), expected.start.to_bits());
+ assert_eq!(actual.end.to_bits(), expected.end.to_bits());
+ assert_eq!(actual.text, expected.text);
+ assert_eq!(actual.words.len(), expected.words.len());
+ for (actual, expected) in actual.words.iter().zip(&expected.words) {
+ assert_eq!(actual.text, expected.text);
+ assert_eq!(actual.start.to_bits(), expected.start.to_bits());
+ assert_eq!(actual.end.to_bits(), expected.end.to_bits());
+ }
+ }
+}
+
+fn timeline(ranges: &[(f64, f64)]) -> TimelineConfiguration {
+ serde_json::from_value(serde_json::json!({
+ "segments": ranges.iter().enumerate().map(|(index, (start, end))| {
+ serde_json::json!({"start": start, "end": end, "timescale": ([0.5, 1.0, 2.0][index % 3])})
+ }).collect::<Vec<_>>(),
+ "zoomSegments": [],
+ "transitions": [{"segmentIndex": 1, "type": "cross-fade", "duration": 0.2}],
+ "textSegments": [{"start": 1.0, "end": 2.0, "layout": "fullscreen"}]
+ }))
+ .unwrap()
+}
+
+#[test]
+fn borrowed_caption_projection_matches_sanitized_copy() {
+ let times = [
+ -1.0,
+ -0.0,
+ 0.0,
+ 1.0,
+ 2.0,
+ 3.0,
+ 4.0,
+ 5.0,
+ 6.0,
+ 8.0,
+ 10.0,
+ f32::NAN,
+ f32::NEG_INFINITY,
+ f32::INFINITY,
+ ];
+ let captions: Vec<_> = times
+ .iter()
+ .enumerate()
+ .flat_map(|(index, start)| {
+ times.iter().enumerate().flat_map(move |(other, end)| {
+ [false, true].map(|with_words| CaptionSegment {
+ id: format!("caption-{index}-{other}-{with_words}"),
+ start: *start,
+ end: *end,
+ text: "parent".into(),
+ words: if with_words {
+ vec![
+ CaptionWord {
+ text: "late".into(),
+ start: *end,
+ end: *end + 0.5,
+ },
+ CaptionWord {
+ text: "early".into(),
+ start: *start,
+ end: *start + 0.5,
+ },
+ CaptionWord {
+ text: ",".into(),
+ start: 4.0,
+ end: 10.0,
+ },
+ ]
+ } else {
+ Vec::new()
+ },
+ })
+ })
+ })
+ .collect();
+ for ranges in [
+ vec![(0.0, 2.0), (2.0, 4.0), (6.0, 10.0)],
+ vec![(0.0, 4.0), (2.0, 6.0), (2.0, 6.0), (6.0, 10.0)],
+ vec![(0.0, 10.0), (2.0, 4.0)],
+ vec![(6.0, 10.0), (0.0, 4.0), (0.0, 4.0)],
+ vec![(0.0, 0.0), (2.0, 1.0), (6.0, 10.0)],
+ vec![(0.0, 10.0)],
+ Vec::new(),
+ ] {
+ let timeline = timeline(&ranges);
+ assert_projection_matches(&captions, &timeline, &[10.0]);
+ assert_projection_matches(&captions, &timeline, &[]);
+ }
+}
+
+#[test]
+fn borrowed_projection_preserves_take_offsets_and_nonfinite_timestamps() {
+ let captions: Vec<_> = (0..40)
+ .map(|index| {
+ let start = index as f32 * 0.5;
+ CaptionSegment {
+ id: format!("caption-{index}"),
+ start,
+ end: start + 0.75,
+ text: "word".into(),
+ words: vec![CaptionWord {
+ text: "word".into(),
+ start,
+ end: start + 0.75,
+ }],
+ }
+ })
+ .collect();
+ let mut timeline = timeline(&[(0.0, 4.0), (0.0, 6.0), (6.0, 10.0)]);
+ timeline.segments[1].recording_clip = 1;
+ timeline.segments[2].recording_clip = 1;
+ assert_projection_matches(&captions, &timeline, &[4.0, 10.0]);
+ for invalid in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
+ let mut changed = timeline.clone();
+ changed.segments[1].start = invalid;
+ assert_projection_matches(&captions, &changed, &[4.0, 10.0]);
+ changed.segments[1].start = 0.0;
+ changed.segments[1].end = invalid;
+ assert_projection_matches(&captions, &changed, &[4.0, 10.0]);
+ assert_projection_matches(&captions, &timeline, &[invalid, 10.0]);
+ }
+}
+
+#[test]
+#[ignore]
+fn benchmark_caption_projection_allocations() {
+ use std::{hint::black_box, time::Instant};
+ let captions: Vec<_> = (0..900)
+ .map(|index| {
+ let start = index as f32 * 4.0;
+ CaptionSegment {
+ id: format!("caption-{index}"),
+ start,
+ end: start + 4.0,
+ text: "spoken words".into(),
+ words: (0..10)
+ .map(|word| {
+ let start = start + word as f32 * 0.4;
+ CaptionWord {
+ text: "word".into(),
+ start,
+ end: start + 0.3,
+ }
+ })
+ .collect(),
+ }
+ })
+ .collect();
+ for clips in [1, 20, 60, 500] {
+ let ranges: Vec<_> = (0..clips)
+ .map(|index| {
+ let start = (index * 3600 / clips) as f64;
+ (start, if clips == 1 { 3600.0 } else { start + 4.0 })
+ })
+ .collect();
+ for reversed in [false, true] {
+ let mut timeline = timeline(&ranges);
+ timeline.transitions.clear();
+ timeline.text_segments.clear();
+ if reversed {
+ timeline.segments.reverse();
+ }
+ assert_projection_matches(&captions, &timeline, &[3600.0]);
+ let run = |baseline: bool, iterations| {
+ let started = Instant::now();
+ for _ in 0..iterations {
+ let output = if baseline {
+ baseline_projection(
+ black_box(&captions),
+ black_box(&timeline),
+ black_box(&[3600.0]),
+ )
+ } else {
+ map_captions_to_edited_timeline(
+ black_box(&captions),
+ black_box(&timeline),
+ black_box(&[3600.0]),
+ )
+ };
+ black_box(output);
+ }
+ started.elapsed().as_secs_f64() * 1000.0 / f64::from(iterations)
+ };
+ run(true, 3);
+ run(false, 3);
+ let mut baseline = Vec::new();
+ let mut borrowed = Vec::new();
+ for sample in 0..7 {
+ if sample % 2 == 0 {
+ baseline.push(run(true, 20));
+ borrowed.push(run(false, 20));
+ } else {
+ borrowed.push(run(false, 20));
+ baseline.push(run(true, 20));
+ }
+ }
+ baseline.sort_by(f64::total_cmp);
+ borrowed.sort_by(f64::total_cmp);
+ println!(
+ "clips={clips} reversed={reversed} baseline_ms={:.6} borrowed_ms={:.6} speedup={:.2}",
+ baseline[3],
+ borrowed[3],
+ baseline[3] / borrowed[3]
+ );
+ }
+ }
+}
+
+fn baseline_projection(
+ raw_segments: &[CaptionSegment],
+ timeline: &TimelineConfiguration,
+ recording_durations: &[f64],
+) -> Vec<MappedCaption> {
+ let sanitized: Vec<CaptionSegment> = raw_segments
+ .iter()
+ .map(clamp_caption_segment_words)
+ .collect();
+
+ if timeline.segments.is_empty() || recording_durations.is_empty() {
+ return sanitized
+ .into_iter()
+ .map(|segment| MappedCaption {
+ id: segment.id,
+ start: f64::from(segment.start),
+ end: f64::from(segment.end),
+ text: segment.text,
+ words: segment.words,
+ })
+ .collect();
+ }
+
+ let mappings = build_source_to_edited_mappings(timeline, recording_durations);
+ let holds = timeline.hold_windows();
+ let hold_adjusted = |start: f64, end: f64| {
+ if holds.is_empty() {
+ (start, end)
+ } else {
+ (
+ effective_to_output(&holds, start),
+ effective_to_output_end(&holds, end),
+ )
+ }
+ };
+
+ let mut result = Vec::new();
+
+ for caption in &sanitized {
+ let mut mapped_caption_segments: Vec<MappedCaption> = Vec::new();
+ let word_bounds = if caption.words.is_empty() {
+ None
+ } else {
+ caption.words.iter().try_fold(
+ (f64::INFINITY, f64::NEG_INFINITY),
+ |(start, end), word| {
+ let word_start = f64::from(word.start);
+ let word_end = f64::from(word.end);
+ if word_start.is_nan() || word_end.is_nan() {
+ None
+ } else {
+ Some((start.min(word_start), end.max(word_end)))
+ }
+ },
+ )
+ };
+
+ for mapping in &mappings {
+ if !caption.words.is_empty() {
+ if let Some((word_start, word_end)) = word_bounds
+ && (word_end <= mapping.source_start || word_start >= mapping.source_end)
+ {
+ continue;
+ }
+
+ let mut mapped_words = Vec::new();
+ for word in &caption.words {
+ let Some((start, end)) = map_time_range_within_mapping(
+ f64::from(word.start),
+ f64::from(word.end),
+ mapping,
+ ) else {
+ continue;
+ };
+ let (start, end) = hold_adjusted(start, end);
+ mapped_words.push(CaptionWord {
+ text: word.text.clone(),
+ start: start as f32,
+ end: end as f32,
+ });
+ }
+
+ if mapped_words.is_empty() {
+ continue;
+ }
+
+ let start = mapped_words
+ .first()
+ .map_or(f64::from(caption.start), |word| f64::from(word.start));
+ let end = mapped_words
+ .last()
+ .map_or(f64::from(caption.end), |word| f64::from(word.end));
+ mapped_caption_segments.push(MappedCaption {
+ id: caption.id.clone(),
+ start,
+ end,
+ text: caption_text_from_words(&mapped_words),
+ words: mapped_words,
+ });
+ } else {
+ let Some((start, end)) = map_time_range_within_mapping(
+ f64::from(caption.start),
+ f64::from(caption.end),
+ mapping,
+ ) else {
+ continue;
+ };
+ let (start, end) = hold_adjusted(start, end);
+ mapped_caption_segments.push(MappedCaption {
+ id: caption.id.clone(),
+ start,
+ end,
+ text: caption.text.clone(),
+ words: Vec::new(),
+ });
+ }
+ }
+
+ let total = mapped_caption_segments.len();
+ for (index, mut segment) in mapped_caption_segments.into_iter().enumerate() {
+ segment.id = mapped_caption_segment_id(&caption.id, index, total);
+ result.push(segment);
+ }
+ }
+
+ result
+}
diff --git a/crates/project/src/config_write_tests.rs b/crates/project/src/config_write_tests.rs
new file mode 100644
index 000000000..994e664ff
--- /dev/null
+++ b/crates/project/src/config_write_tests.rs
@@ -0,0 +1,83 @@
+use crate::ProjectConfiguration;
+use std::io::Read;
+
+#[test]
+fn replacing_config_keeps_the_previous_file_intact() {
+ let project = tempfile::tempdir().unwrap();
+ let path = project.path().join("project-config.json");
+ let mut config = ProjectConfiguration::default();
+ config.write(project.path()).unwrap();
+ let original = std::fs::read(&path).unwrap();
+ let mut previous = std::fs::File::open(&path).unwrap();
+ config.background.padding = 23.0;
+
+ config.write(project.path()).unwrap();
+
+ let mut retained = Vec::new();
+ previous.read_to_end(&mut retained).unwrap();
+ assert_eq!(retained, original);
+ assert_eq!(
+ std::fs::read(&path).unwrap(),
+ serde_json::to_vec_pretty(&config).unwrap()
+ );
+ assert_eq!(std::fs::read_dir(project.path()).unwrap().count(), 1);
+}
+
+#[test]
+fn failed_replacement_keeps_the_destination_and_cleans_up() {
+ let project = tempfile::tempdir().unwrap();
+ let path = project.path().join("project-config.json");
+ std::fs::create_dir(&path).unwrap();
+ std::fs::write(path.join("retained"), b"original").unwrap();
+
+ assert!(
+ ProjectConfiguration::default()
+ .write(project.path())
+ .is_err()
+ );
+
+ assert_eq!(std::fs::read(path.join("retained")).unwrap(), b"original");
+ assert_eq!(std::fs::read_dir(project.path()).unwrap().count(), 1);
+}
+
+#[cfg(unix)]
+#[test]
+fn failed_write_keeps_the_previous_config_and_cleans_up() {
+ const CHILD_PROJECT: &str = "CAP_PROJECT_LIMITED_WRITE_TEST";
+ if let Some(project) = std::env::var_os(CHILD_PROJECT) {
+ let config = ProjectConfiguration::default();
+ assert!(serde_json::to_vec_pretty(&config).unwrap().len() > 1024);
+ assert!(config.write(project).is_err());
+ return;
+ }
+
+ let project = tempfile::tempdir().unwrap();
+ let path = project.path().join("project-config.json");
+ let mut config = ProjectConfiguration::default();
+ config.background.padding = 23.0;
+ config.write(project.path()).unwrap();
+ let original = std::fs::read(&path).unwrap();
+
+ let output = std::process::Command::new("sh")
+ .args([
+ "-c",
+ "trap '' XFSZ; ulimit -f 1; exec \"$@\"",
+ "config-write-test",
+ ])
+ .arg(std::env::current_exe().unwrap())
+ .args([
+ "--exact",
+ "config_write_tests::failed_write_keeps_the_previous_config_and_cleans_up",
+ ])
+ .env(CHILD_PROJECT, project.path())
+ .output()
+ .unwrap();
+
+ assert!(output.status.success(), "{output:?}");
+ assert!(
+ String::from_utf8_lossy(&output.stdout).contains("1 passed"),
+ "{output:?}"
+ );
+ assert_eq!(std::fs::read(path).unwrap(), original);
+ assert_eq!(std::fs::read_dir(project.path()).unwrap().count(), 1);
+}
diff --git a/crates/project/src/configuration.rs b/crates/project/src/configuration.rs
index 9a2aa35eb..5696447ed 100644
--- a/crates/project/src/configuration.rs
+++ b/crates/project/src/configuration.rs
@@ -2,6 +2,7 @@ use std::{
borrow::Cow,
collections::BTreeMap,
fmt,
+ io::Write,
ops::{Add, Div, Mul, Sub, SubAssign},
path::Path,
sync::LazyLock,
@@ -13,6 +14,9 @@ use specta::Type;
use crate::DisplayNotch;
+#[cfg(test)]
+mod style_resolution_tests;
+
#[derive(Type, Serialize, Deserialize, Clone, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub enum AspectRatio {
@@ -1824,6 +1828,15 @@ where
Ok(by_segment.into_values().collect())
}
+fn deserialize_scene_segments<'de, D>(deserializer: D) -> Result<Vec<SceneSegment>, D::Error>
+where
+ D: serde::Deserializer<'de>,
+{
+ let mut segments = Vec::<SceneSegment>::deserialize(deserializer)?;
+ segments.sort_by(|left, right| left.start.total_cmp(&right.start));
+ Ok(segments)
+}
+
#[derive(Type, Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TimelineConfiguration {
@@ -1835,7 +1848,7 @@ pub struct TimelineConfiguration {
)]
pub transitions: Vec<ClipTransition>,
pub zoom_segments: Vec<ZoomSegment>,
- #[serde(default)]
+ #[serde(default, deserialize_with = "deserialize_scene_segments")]
pub scene_segments: Vec<SceneSegment>,
#[serde(default)]
pub mask_segments: Vec<MaskSegment>,
@@ -2827,16 +2840,40 @@ impl ProjectConfiguration {
}
pub fn style_at(&self, time: f64) -> Cow<'_, Self> {
+ let Some(timeline) = &self.timeline else {
+ return Cow::Borrowed(self);
+ };
+ let mut selected: [Option<&StyleSegment>; 3] = [None; 3];
+ for segment in &timeline.style_segments {
+ if !segment.is_active_at(time) {
+ continue;
+ }
+ for (current, present) in selected.iter_mut().zip([
+ segment.overrides.background.is_some(),
+ segment.overrides.camera.is_some(),
+ segment.overrides.cursor.is_some(),
+ ]) {
+ if present
+ && current.is_none_or(|previous| {
+ segment.track > previous.track
+ || (segment.track == previous.track && segment.start >= previous.start)
+ })
+ {
+ *current = Some(segment);
+ }
+ }
+ }
+ let [background, camera, cursor] = selected;
let mut configuration = Cow::Borrowed(self);
if let Some(background) =
- self.style_override_at(time, |overrides| overrides.background.as_ref())
+ background.and_then(|segment| segment.overrides.background.as_ref())
{
configuration.to_mut().background = background.clone();
}
- if let Some(camera) = self.style_override_at(time, |overrides| overrides.camera.as_ref()) {
+ if let Some(camera) = camera.and_then(|segment| segment.overrides.camera.as_ref()) {
configuration.to_mut().camera = camera.clone();
}
- if let Some(cursor) = self.style_override_at(time, |overrides| overrides.cursor.as_ref()) {
+ if let Some(cursor) = cursor.and_then(|segment| segment.overrides.cursor.as_ref()) {
configuration.to_mut().cursor = cursor.clone();
}
configuration
@@ -2985,17 +3022,16 @@ impl ProjectConfiguration {
let project_path = project_path.as_ref();
let config_path = project_path.join("project-config.json");
- let temp_path =
- project_path.join(format!(".project-config-{}.json.tmp", uuid::Uuid::new_v4()));
-
- std::fs::write(&temp_path, serde_json::to_string_pretty(self)?)?;
-
- if let Err(error) = std::fs::rename(&temp_path, &config_path) {
- let _ = std::fs::remove_file(&temp_path);
- return Err(error);
- }
+ let temp_path = tempfile::TempPath::from_path(
+ project_path.join(format!(".project-config-{}.json.tmp", uuid::Uuid::new_v4())),
+ );
+ let config = serde_json::to_string_pretty(self)?;
+ let mut file = std::fs::File::create(&temp_path)?;
+ file.write_all(config.as_bytes())?;
+ file.sync_all()?;
+ drop(file);
- Ok(())
+ std::fs::rename(&temp_path, &config_path)
}
pub fn get_segment_time(&self, frame_time: f64) -> Option<(f64, &TimelineSegment)> {
diff --git a/crates/project/src/configuration/style_resolution_tests.rs b/crates/project/src/configuration/style_resolution_tests.rs
new file mode 100644
index 000000000..9b5b73271
--- /dev/null
+++ b/crates/project/src/configuration/style_resolution_tests.rs
@@ -0,0 +1,177 @@
+use super::*;
+
+fn baseline_style_at(project: &ProjectConfiguration, time: f64) -> Cow<'_, ProjectConfiguration> {
+ let mut configuration = Cow::Borrowed(project);
+ if let Some(background) =
+ project.style_override_at(time, |overrides| overrides.background.as_ref())
+ {
+ configuration.to_mut().background = background.clone();
+ }
+ if let Some(camera) = project.style_override_at(time, |overrides| overrides.camera.as_ref()) {
+ configuration.to_mut().camera = camera.clone();
+ }
+ if let Some(cursor) = project.style_override_at(time, |overrides| overrides.cursor.as_ref()) {
+ configuration.to_mut().cursor = cursor.clone();
+ }
+ configuration
+}
+
+fn fixture(count: usize, overlap: bool) -> ProjectConfiguration {
+ let mut timeline: TimelineConfiguration = serde_json::from_value(serde_json::json!({
+ "segments": [{"start": 0.0, "end": 3600.0, "timescale": 1.0}],
+ "zoomSegments": []
+ }))
+ .unwrap();
+ timeline.style_segments = (0..count)
+ .map(|index| {
+ let start = if overlap {
+ (index % 7) as f64 * 0.5
+ } else {
+ index as f64 * 2.0
+ };
+ StyleSegment {
+ start,
+ end: start + 1.0,
+ track: (index % 4) as u32,
+ enabled: index % 11 != 10,
+ overrides: StyleOverrides {
+ background: (index % 3 != 1).then(|| BackgroundConfiguration {
+ padding: index as f64,
+ ..Default::default()
+ }),
+ camera: (index % 3 != 2).then(|| Camera {
+ size: index as f32,
+ ..Default::default()
+ }),
+ cursor: (index % 3 != 0).then(|| CursorConfiguration {
+ size: index as u32,
+ ..Default::default()
+ }),
+ camera_only_padding: Some(index as f64),
+ },
+ ..Default::default()
+ }
+ })
+ .collect();
+ ProjectConfiguration {
+ timeline: Some(timeline),
+ ..Default::default()
+ }
+}
+
+#[test]
+fn one_pass_styles_match_independent_group_selection() {
+ let mut project = fixture(70, true);
+ let timeline = project.timeline.as_mut().unwrap();
+ for (index, start, end) in [
+ (0, -0.0, 5.0),
+ (1, 0.0, 5.0),
+ (2, -1.0, 5.0),
+ (3, f64::NAN, 5.0),
+ (4, 0.0, f64::INFINITY),
+ (5, 3.0, 2.0),
+ (6, 2.0, 2.0),
+ ] {
+ timeline.style_segments[index].start = start;
+ timeline.style_segments[index].end = end;
+ }
+ let original = serde_json::to_value(&project).unwrap();
+ for time in [
+ -1.0,
+ -0.0,
+ 0.0,
+ 0.5,
+ 0.5_f64.next_down(),
+ 0.5_f64.next_up(),
+ 1.0,
+ 2.0,
+ 3.0,
+ 3.5,
+ 4.0,
+ 5.0,
+ 10.0,
+ f64::NAN,
+ f64::NEG_INFINITY,
+ f64::INFINITY,
+ ] {
+ let expected = baseline_style_at(&project, time);
+ let actual = project.style_at(time);
+ assert_eq!(
+ matches!(actual, Cow::Borrowed(_)),
+ matches!(expected, Cow::Borrowed(_))
+ );
+ assert_eq!(
+ serde_json::to_value(actual.as_ref()).unwrap(),
+ serde_json::to_value(expected.as_ref()).unwrap()
+ );
+ }
+ assert_eq!(serde_json::to_value(&project).unwrap(), original);
+ let project = ProjectConfiguration::default();
+ assert!(matches!(project.style_at(1.0), Cow::Borrowed(_)));
+}
+
+#[test]
+#[ignore]
+fn benchmark_style_resolution() {
+ use std::{hint::black_box, time::Instant};
+ for count in [0, 20, 200, 1000] {
+ let project = fixture(count, false);
+ for active in [false, true] {
+ let time = if active && count > 0 {
+ let segment = project
+ .timeline
+ .as_ref()
+ .unwrap()
+ .style_segments
+ .iter()
+ .skip(count / 2)
+ .find(|segment| segment.enabled)
+ .unwrap();
+ segment.start + 0.25
+ } else {
+ count as f64 * 2.0 + 0.25
+ };
+ let expected = baseline_style_at(&project, time);
+ let actual = project.style_at(time);
+ assert_eq!(matches!(actual, Cow::Owned(_)), active && count > 0);
+ assert_eq!(
+ serde_json::to_value(actual.as_ref()).unwrap(),
+ serde_json::to_value(expected.as_ref()).unwrap()
+ );
+ let run = |baseline: bool, iterations| {
+ let started = Instant::now();
+ for _ in 0..iterations {
+ let output = if baseline {
+ baseline_style_at(black_box(&project), black_box(time))
+ } else {
+ black_box(&project).style_at(black_box(time))
+ };
+ black_box(output);
+ }
+ started.elapsed().as_secs_f64() * 1e9 / f64::from(iterations)
+ };
+ let iterations = if active && count > 0 { 1000 } else { 10000 };
+ run(true, 10);
+ run(false, 10);
+ let mut baseline = Vec::new();
+ let mut single = Vec::new();
+ for sample in 0..7 {
+ if sample % 2 == 0 {
+ baseline.push(run(true, iterations));
+ single.push(run(false, iterations));
+ } else {
+ single.push(run(false, iterations));
+ baseline.push(run(true, iterations));
+ }
+ }
+ baseline.sort_by(f64::total_cmp);
+ single.sort_by(f64::total_cmp);
+ println!(
+ "styles={count} active={active} before_ns={:.3} after_ns={:.3} speedup={:.2}",
+ baseline[3],
+ single[3],
+ baseline[3] / single[3]
+ );
+ }
+ }
+}
diff --git a/crates/project/src/cursor.rs b/crates/project/src/cursor.rs
index 186e41247..3c0787e9c 100644
--- a/crates/project/src/cursor.rs
+++ b/crates/project/src/cursor.rs
@@ -129,12 +129,19 @@ impl CursorEvents {
}
let last_move_time = self.moves.last().map(|event| event.time_ms).unwrap_or(0.0);
+ let final_segment = segments.last().unwrap();
+ let last_confirmed_time = self
+ .clicks
+ .iter()
+ .filter(|click| click.cursor_id == final_segment.id)
+ .map(|click| click.time_ms)
+ .fold(last_move_time, f64::max);
for i in 0..segments.len() {
let end_time = if i + 1 < segments.len() {
segments[i + 1].start_time
} else {
- last_move_time
+ last_confirmed_time
};
let duration = (end_time - segments[i].start_time).max(0.0);
@@ -529,6 +536,23 @@ mod tests {
assert_eq!(events.clicks[0].cursor_id, "ibeam");
}
+ #[test]
+ fn final_shape_is_preserved_when_a_later_click_confirms_its_duration() {
+ let pointer_ids = HashSet::from(["pointer".to_string()]);
+ let mut events = CursorEvents {
+ moves: vec![move_event(0.0, "pointer"), move_event(1500.0, "ibeam")],
+ clicks: vec![click_event(2700.0, "ibeam")],
+ };
+
+ events.stabilize_short_lived_cursor_shapes(
+ Some(&pointer_ids),
+ SHORT_CURSOR_SHAPE_DEBOUNCE_MS,
+ );
+
+ assert_eq!(events.moves[1].cursor_id, "ibeam");
+ assert_eq!(events.clicks[0].cursor_id, "ibeam");
+ }
+
#[test]
fn falls_back_to_dominant_cursor_without_pointer_metadata() {
let mut events = CursorEvents {
diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs
index dea389ee1..080355b05 100644
--- a/crates/project/src/lib.rs
+++ b/crates/project/src/lib.rs
@@ -1,5 +1,7 @@
mod animated_gradient;
mod caption_timing;
+#[cfg(test)]
+mod config_write_tests;
mod configuration;
pub mod cursor;
pub mod keyboard;
diff --git a/crates/project/src/meta.rs b/crates/project/src/meta.rs
index 08b5a4094..a276cbd2f 100644
--- a/crates/project/src/meta.rs
+++ b/crates/project/src/meta.rs
@@ -55,6 +55,10 @@ pub struct AudioMeta {
pub device_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub gap_summary: Option<AudioGapSummary>,
+ /// Milliseconds the unmuted microphone delivered sustained bit-exact silence,
+ /// e.g. while a VoIP call held the input on macOS.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub digital_silence_ms: Option<u32>,
}
/// Overlap-trim accounting captured by the recorder's audio gap tracker, persisted so the
@@ -1142,6 +1146,7 @@ mod test {
start_time,
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
}
}
diff --git a/crates/project/tests/configuration_loading.rs b/crates/project/tests/configuration_loading.rs
new file mode 100644
index 000000000..940b9ff17
--- /dev/null
+++ b/crates/project/tests/configuration_loading.rs
@@ -0,0 +1,52 @@
+use cap_project::ProjectConfiguration;
+use serde_json::json;
+
+#[test]
+fn loading_sorts_scenes_without_changing_clip_edits() {
+ let project = tempfile::tempdir().unwrap();
+ let path = project.path().join("project-config.json");
+ std::fs::write(
+ &path,
+ serde_json::to_vec(&json!({
+ "timeline": {
+ "segments": [
+ { "recordingSegment": 1, "start": 20.0, "end": 22.0, "timescale": 0.25, "name": "Opening", "volume": 0.5 },
+ { "recordingSegment": 0, "start": 10.0, "end": 10.0, "timescale": 1.0 },
+ { "recordingSegment": 0, "start": 0.0, "end": 32.0, "timescale": 8.0 }
+ ],
+ "zoomSegments": [],
+ "sceneSegments": [
+ { "start": 8.0, "end": 10.0, "mode": "cameraOnly" },
+ { "start": 1.0, "end": 3.0, "mode": "cameraOnly" },
+ { "start": 5.0, "end": 7.0, "mode": "hideCamera" }
+ ]
+ }
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+
+ let loaded = ProjectConfiguration::load(project.path()).unwrap();
+ loaded.write(project.path()).unwrap();
+ let reloaded = ProjectConfiguration::load(project.path()).unwrap();
+ for config in [&loaded, &reloaded] {
+ let timeline = config.timeline.as_ref().unwrap();
+ assert_eq!(
+ timeline
+ .scene_segments
+ .iter()
+ .map(|scene| scene.start)
+ .collect::<Vec<_>>(),
+ [1.0, 5.0, 8.0]
+ );
+ assert_eq!(timeline.duration(), 12.0);
+ let (source_time, clip) = timeline.get_segment_time(0.0).unwrap();
+ assert_eq!((source_time, clip.recording_clip), (20.0, 1));
+ assert_eq!(clip.name.as_deref(), Some("Opening"));
+ assert_eq!(clip.volume(), 0.5);
+ let (source_time, clip) = timeline.get_segment_time(9.0).unwrap();
+ assert_eq!((source_time, clip.recording_clip), (8.0, 0));
+ assert_eq!(timeline.segments.len(), 3);
+ assert_eq!(timeline.segments[1].duration(), 0.0);
+ }
+}
diff --git a/crates/recording/examples/synthetic-test-runner.rs b/crates/recording/examples/synthetic-test-runner.rs
index f2ec2c355..8722e2235 100644
--- a/crates/recording/examples/synthetic-test-runner.rs
+++ b/crates/recording/examples/synthetic-test-runner.rs
@@ -1,5 +1,6 @@
use cap_recording::{
- Mp4Muxer, OggMuxer, OutputPipeline, SegmentedVideoMuxer, SegmentedVideoMuxerConfig,
+ Mp4Muxer, OggMuxer, OggMuxerConfig, OutputPipeline, SegmentedVideoMuxer,
+ SegmentedVideoMuxerConfig,
test_sources::{
AudioGenerator, AudioTestConfig, OutputFormat, RecordingValidator, SyntheticAudioSource,
SyntheticAudioSourceConfig, TestConfig, TestPattern, TestPatternVideoSource,
@@ -468,7 +469,7 @@ async fn run_audio_only_ogg_recording(
let pipeline = OutputPipeline::builder(output_path.to_path_buf())
.with_timestamps(timestamps)
.with_audio_source::<SyntheticAudioSource>(audio_source_config)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await?;
tokio::time::sleep(duration + Duration::from_millis(500)).await;
diff --git a/crates/recording/src/feeds/camera.rs b/crates/recording/src/feeds/camera.rs
index 47e354584..e70151f06 100644
--- a/crates/recording/src/feeds/camera.rs
+++ b/crates/recording/src/feeds/camera.rs
@@ -25,7 +25,7 @@ use tokio::{sync::oneshot, task::LocalSet};
use tracing::{debug, error, trace, warn};
use crate::ffmpeg::FFmpegVideoFrame;
-use crate::output_pipeline::NativeCameraFrame;
+use crate::output_pipeline::{NativeCameraFrame, VideoFrame};
const CAMERA_INIT_TIMEOUT: Duration = Duration::from_secs(4);
/// Outer deadline for camera readiness. Must cover both capture attempts on
@@ -114,6 +114,22 @@ impl<T> FanoutTarget<T> for CountedSender<T> {
}
}
+trait FanoutFrame {
+ fn share(&self) -> Self;
+}
+
+impl FanoutFrame for FFmpegVideoFrame {
+ fn share(&self) -> Self {
+ self.duplicate().unwrap_or_else(|| self.clone())
+ }
+}
+
+impl FanoutFrame for NativeCameraFrame {
+ fn share(&self) -> Self {
+ self.clone()
+ }
+}
+
/// Rate limit for drop warnings: the first drop is reported at once, later
/// drops at most once per interval with the count since the last report, so
/// an isolated loss is never hidden and a sustained loss cannot flood the log.
@@ -1619,7 +1635,7 @@ impl FanoutDrops<'_> {
}
}
-fn send_frame_to_camera_senders<T: Clone, S: FanoutTarget<T>>(
+fn send_frame_to_camera_senders<T: FanoutFrame, S: FanoutTarget<T>>(
senders: &mut Vec<S>,
frame: T,
frame_num: u64,
@@ -1639,7 +1655,8 @@ fn send_frame_to_camera_senders<T: Clone, S: FanoutTarget<T>>(
);
}
- // The last sender with room takes the frame itself instead of a clone.
+ // The last sender with room takes the frame itself; the others share its
+ // pixel storage.
let last_ready_sender = senders
.iter()
.rposition(|sender| !sender.channel().is_full());
@@ -1665,7 +1682,7 @@ fn send_frame_to_camera_senders<T: Clone, S: FanoutTarget<T>>(
let Some(frame) = frame.as_ref() else {
break;
};
- sender.channel().try_send(frame.clone())
+ sender.channel().try_send(frame.share())
};
match send_result {
@@ -2759,6 +2776,12 @@ mod ingress_tests {
type Output = (flume::Receiver<u32>, Arc<AtomicU64>);
+ impl FanoutFrame for u32 {
+ fn share(&self) -> Self {
+ *self
+ }
+ }
+
struct Fanout {
senders: Vec<CountedSender<u32>>,
total: u64,
@@ -2861,6 +2884,58 @@ mod ingress_tests {
assert_eq!(fanout.total, 14);
}
+ #[test]
+ fn camera_fanout_delivers_identical_pixels_without_copying() {
+ let mut inner = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::NV12, 64, 48);
+ for plane in 0..inner.planes() {
+ for (index, byte) in inner.data_mut(plane).iter_mut().enumerate() {
+ *byte = (index * 7 + plane * 31) as u8;
+ }
+ }
+ inner.set_pts(Some(1234));
+ let expected: Vec<_> = (0..inner.planes())
+ .map(|plane| (inner.stride(plane), inner.data(plane).to_vec()))
+ .collect();
+ let pixels = inner.data(0).as_ptr();
+ let captured_at = std::time::Instant::now();
+ let (mut senders, receivers): (Vec<_>, Vec<_>) = (0..3).map(|_| flume::bounded(1)).unzip();
+ let mut total = 0;
+ let mut log = DropLog::default();
+
+ send_frame_to_camera_senders(
+ &mut senders,
+ FFmpegVideoFrame {
+ inner,
+ timestamp: Timestamp::Instant(captured_at),
+ },
+ 0,
+ "Camera",
+ FanoutDrops {
+ total: &mut total,
+ log: &mut log,
+ },
+ );
+
+ let frames: Vec<FFmpegVideoFrame> = receivers
+ .iter()
+ .map(|rx| rx.try_recv().expect("fanned-out frame"))
+ .collect();
+ for frame in &frames {
+ assert_eq!(frame.inner.data(0).as_ptr(), pixels);
+ assert_eq!(frame.inner.format(), ffmpeg::format::Pixel::NV12);
+ assert_eq!((frame.inner.width(), frame.inner.height()), (64, 48));
+ assert_eq!(frame.inner.pts(), Some(1234));
+ assert!(matches!(frame.timestamp, Timestamp::Instant(value) if value == captured_at));
+ for (plane, (stride, bytes)) in expected.iter().enumerate() {
+ assert_eq!(frame.inner.stride(plane), *stride);
+ assert_eq!(frame.inner.data(plane), bytes.as_slice());
+ }
+ }
+ let buffer = unsafe { (*frames[0].inner.as_ptr()).buf[0] };
+ assert_eq!(unsafe { ffmpeg::ffi::av_buffer_get_ref_count(buffer) }, 3);
+ assert_eq!(total, 0);
+ }
+
/// Consumers draining concurrently with the fan-out: every frame is
/// either received or counted as dropped for each queue, never both.
#[test]
diff --git a/crates/recording/src/feeds/microphone.rs b/crates/recording/src/feeds/microphone.rs
index 5efa225f5..a3e77414b 100644
--- a/crates/recording/src/feeds/microphone.rs
+++ b/crates/recording/src/feeds/microphone.rs
@@ -3079,7 +3079,7 @@ impl Message<Unlock> for MicrophoneFeed {
/// Test-only microphone feeds driven by a synthetic device instead of
/// hardware, so recording tests exercise the real feed, lock and pipeline
/// paths with samples whose capture times are known exactly.
-#[cfg(feature = "test-utils")]
+#[cfg(any(test, feature = "test-utils"))]
pub mod synthetic {
use super::*;
diff --git a/crates/recording/src/feeds/mod.rs b/crates/recording/src/feeds/mod.rs
index 09263bf3b..6f084fa23 100644
--- a/crates/recording/src/feeds/mod.rs
+++ b/crates/recording/src/feeds/mod.rs
@@ -6,7 +6,7 @@ pub mod microphone;
/// full or the feed has stopped. Test-only sources hand over the same way so
/// a feed that cannot keep up is visible as a rejection, not as a late
/// source.
-#[cfg(feature = "test-utils")]
+#[cfg(any(test, feature = "test-utils"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Admission {
Accepted,
@@ -14,7 +14,7 @@ pub enum Admission {
Closed,
}
-#[cfg(feature = "test-utils")]
+#[cfg(any(test, feature = "test-utils"))]
impl Admission {
fn of<M, E>(result: Result<(), kameo::error::SendError<M, E>>) -> Self {
match result {
@@ -27,5 +27,5 @@ impl Admission {
/// Test-only slow consumer: the feed actor handles nothing else for the
/// given time, as a feed blocked on a downstream consumer would.
-#[cfg(feature = "test-utils")]
+#[cfg(any(test, feature = "test-utils"))]
pub struct Stall(pub std::time::Duration);
diff --git a/crates/recording/src/instant_recording.rs b/crates/recording/src/instant_recording.rs
index bae7bf008..84a510440 100644
--- a/crates/recording/src/instant_recording.rs
+++ b/crates/recording/src/instant_recording.rs
@@ -6,9 +6,9 @@ mod linux_camera;
#[cfg(target_os = "linux")]
pub use linux_camera::{
LINUX_CAMERA_MAX_MASK_AGE, LinuxCameraBlur, LinuxCameraEffect, LinuxCameraMaskReceipt,
- LinuxCameraPresentation, LinuxCameraPresentationError, LinuxCameraProcessing,
- LinuxCameraPublisher, LinuxCameraRect, LinuxCameraShape, LinuxProcessedCameraFrame,
- LinuxProcessedCameraSource,
+ LinuxCameraPixels, LinuxCameraPresentation, LinuxCameraPresentationError,
+ LinuxCameraProcessing, LinuxCameraPublisher, LinuxCameraRect, LinuxCameraShape,
+ LinuxProcessedCameraFrame, LinuxProcessedCameraSource,
};
use crate::{
@@ -822,6 +822,7 @@ async fn create_pipeline(
output_pipeline::DashSegmentedAudioMuxerConfig {
shared_pause_state: None,
segment_tx: segment_tx_for_audio,
+ write_behind: cfg!(target_os = "macos"),
..Default::default()
},
)
diff --git a/crates/recording/src/instant_recording/linux_camera.rs b/crates/recording/src/instant_recording/linux_camera.rs
index e87fc756e..5ece2ad2c 100644
--- a/crates/recording/src/instant_recording/linux_camera.rs
+++ b/crates/recording/src/instant_recording/linux_camera.rs
@@ -202,9 +202,34 @@ pub struct LinuxCameraMaskReceipt {
pub completed_at: Instant,
}
+#[derive(Clone)]
+pub struct LinuxCameraPixels(Arc<dyn AsRef<[u8]> + Send + Sync>);
+
+impl LinuxCameraPixels {
+ pub fn new(pixels: impl AsRef<[u8]> + Send + Sync + 'static) -> Self {
+ Self(Arc::new(pixels))
+ }
+}
+
+impl std::ops::Deref for LinuxCameraPixels {
+ type Target = [u8];
+
+ fn deref(&self) -> &[u8] {
+ (*self.0).as_ref()
+ }
+}
+
+impl std::fmt::Debug for LinuxCameraPixels {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("LinuxCameraPixels")
+ .field("len", &self.len())
+ .finish()
+ }
+}
+
#[derive(Debug, Clone)]
pub struct LinuxProcessedCameraFrame {
- pub bgra: Arc<[u8]>,
+ pub bgra: LinuxCameraPixels,
pub dimensions: (u32, u32),
pub stride: usize,
pub timestamp: Timestamp,
@@ -788,6 +813,152 @@ fn camera_freshness_anchor(frame: &FFmpegVideoFrame, received_at: Instant) -> In
}
}
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum OverlaySpanKind {
+ Opaque,
+ Transparent,
+ Edge,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+struct OverlaySpan {
+ start: u32,
+ len: u32,
+ kind: OverlaySpanKind,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Default)]
+struct OverlayRuns {
+ width: usize,
+ height: usize,
+ spans: Vec<OverlaySpan>,
+ row_starts: Vec<u32>,
+}
+
+impl OverlayRuns {
+ fn row(&self, index: usize) -> &[OverlaySpan] {
+ let start = self.row_starts[index] as usize;
+ let end = self.row_starts[index + 1] as usize;
+ &self.spans[start..end]
+ }
+}
+
+#[derive(Debug, Clone, PartialEq)]
+struct OverlayRunKey {
+ generation: u64,
+ width: u32,
+ height: u32,
+ mask: Arc<[f64]>,
+}
+
+#[derive(Debug, Clone, PartialEq, Default)]
+struct OverlayRunCache {
+ key: Option<OverlayRunKey>,
+ runs: OverlayRuns,
+ builds: u64,
+}
+
+impl OverlayRunCache {
+ fn matches(&self, generation: u64, mask: &Arc<[f64]>, width: u32, height: u32) -> bool {
+ let Some(key) = &self.key else {
+ return false;
+ };
+ key.generation == generation
+ && key.width == width
+ && key.height == height
+ && (Arc::ptr_eq(&key.mask, mask) || key.mask.as_ref() == mask.as_ref())
+ }
+
+ fn install(
+ &mut self,
+ generation: u64,
+ width: u32,
+ height: u32,
+ mask: Arc<[f64]>,
+ runs: OverlayRuns,
+ ) {
+ self.builds += 1;
+ self.key = Some(OverlayRunKey {
+ generation,
+ width,
+ height,
+ mask,
+ });
+ self.runs = runs;
+ }
+
+ fn resolve(
+ &mut self,
+ generation: u64,
+ mask: &Arc<[f64]>,
+ frame: &ffmpeg::frame::Video,
+ ) -> &OverlayRuns {
+ let width = frame.width();
+ let height = frame.height();
+ if self.matches(generation, mask, width, height) {
+ return &self.runs;
+ }
+ let runs = overlay_runs(frame);
+ self.install(generation, width, height, Arc::clone(mask), runs);
+ &self.runs
+ }
+}
+
+fn span_kind(alpha: u8) -> OverlaySpanKind {
+ match alpha {
+ 0 => OverlaySpanKind::Transparent,
+ 255 => OverlaySpanKind::Opaque,
+ _ => OverlaySpanKind::Edge,
+ }
+}
+
+fn push_alpha_runs(spans: &mut Vec<OverlaySpan>, alphas: impl Iterator<Item = u8>) {
+ let mut alphas = alphas.peekable();
+ let mut index = 0u32;
+ while let Some(alpha) = alphas.next() {
+ let kind = span_kind(alpha);
+ let start = index;
+ index += 1;
+ while alphas.peek().is_some_and(|next| span_kind(*next) == kind) {
+ let _ = alphas.next();
+ index += 1;
+ }
+ spans.push(OverlaySpan {
+ start,
+ len: index - start,
+ kind,
+ });
+ }
+}
+
+#[cfg(test)]
+fn classify_alpha_row(alphas: &[u8]) -> Vec<OverlaySpan> {
+ let mut spans = Vec::new();
+ push_alpha_runs(&mut spans, alphas.iter().copied());
+ spans
+}
+
+fn overlay_runs(frame: &ffmpeg::frame::Video) -> OverlayRuns {
+ let width = frame.width() as usize;
+ let height = frame.height() as usize;
+ let stride = frame.stride(0);
+ let data = frame.data(0);
+ let mut spans = Vec::new();
+ let mut row_starts = Vec::with_capacity(height + 1);
+ for row in 0..height {
+ row_starts.push(spans.len() as u32);
+ let row_bytes = &data[row * stride..][..width * 4];
+ push_alpha_runs(&mut spans, row_bytes.chunks_exact(4).map(|p| p[3]));
+ }
+ row_starts.push(spans.len() as u32);
+ OverlayRuns {
+ width,
+ height,
+ spans,
+ row_starts,
+ }
+}
+
struct Compositor {
info: VideoInfo,
presentation: LinuxCameraPresentation,
@@ -795,7 +966,8 @@ struct Compositor {
camera_to_rgba: Option<RgbaConverter>,
rgba_to_screen: Option<RgbaConverter>,
camera_overlay: Option<(u64, ffmpeg::frame::Video)>,
- camera_mask: Option<Vec<f64>>,
+ camera_mask: Option<Arc<[f64]>>,
+ run_cache: OverlayRunCache,
}
impl Compositor {
@@ -808,7 +980,43 @@ impl Compositor {
rgba_to_screen: None,
camera_overlay: None,
camera_mask: None,
+ run_cache: OverlayRunCache::default(),
+ }
+ }
+
+ fn overlay_is_current(&self, generation: u64, width: u32, height: u32) -> bool {
+ let frame_ok = self.camera_overlay.as_ref().is_some_and(|(cached, frame)| {
+ *cached == generation && frame.width() == width && frame.height() == height
+ });
+ if !frame_ok {
+ return false;
+ }
+ let Some(mask) = self.camera_mask.as_ref() else {
+ return false;
+ };
+ self.run_cache.matches(generation, mask, width, height)
+ }
+
+ fn ensure_camera_overlay(
+ &mut self,
+ input: &ffmpeg::frame::Video,
+ generation: u64,
+ ) -> anyhow::Result<()> {
+ let presentation = self.presentation;
+ let width = presentation.rect.width;
+ let height = presentation.rect.height;
+ if self.camera_mask.is_none() {
+ self.camera_mask = Some(Arc::from(camera_mask(presentation)));
+ }
+ if self.overlay_is_current(generation, width, height) {
+ return Ok(());
}
+ let rgba = self.convert_camera(input, width, height)?;
+ let mask = Arc::clone(self.camera_mask.as_ref().expect("camera mask"));
+ let frame = crop_camera(&rgba, presentation, mask.as_ref())?;
+ self.run_cache.resolve(generation, &mask, &frame);
+ self.camera_overlay = Some((generation, frame));
+ Ok(())
}
fn compose(
@@ -823,13 +1031,20 @@ impl Compositor {
self.presentation
.validate(self.info.width, self.info.height)?;
let rect = self.presentation.rect;
- let camera_overlay = self.camera_overlay(&camera.inner, camera_generation)?;
- blend_rgba_into_packed(
+ self.ensure_camera_overlay(&camera.inner, camera_generation)?;
+ let camera_overlay = &self
+ .camera_overlay
+ .as_ref()
+ .expect("camera overlay prepared")
+ .1;
+ let runs = &self.run_cache.runs;
+ blend_rgba_into_packed_with_runs(
&mut screen.inner,
layout,
camera_overlay,
rect.x as usize,
rect.y as usize,
+ Some(runs),
)?;
Ok(screen)
}
@@ -856,12 +1071,19 @@ impl Compositor {
self.presentation
.validate(self.info.width, self.info.height)?;
let rect = self.presentation.rect;
- let camera_overlay = self.camera_overlay(&camera.inner, camera_generation)?;
- blend_rgba(
+ self.ensure_camera_overlay(&camera.inner, camera_generation)?;
+ let camera_overlay = &self
+ .camera_overlay
+ .as_ref()
+ .expect("camera overlay prepared")
+ .1;
+ let runs = &self.run_cache.runs;
+ blend_rgba_with_runs(
&mut composed,
camera_overlay,
rect.x as usize,
rect.y as usize,
+ Some(runs),
)?;
let mut output = self.convert_to_screen(&composed)?;
output.set_pts(screen.inner.pts());
@@ -877,21 +1099,7 @@ impl Compositor {
input: &ffmpeg::frame::Video,
camera_generation: u64,
) -> anyhow::Result<&ffmpeg::frame::Video> {
- let cached = self
- .camera_overlay
- .as_ref()
- .is_some_and(|(generation, _)| *generation == camera_generation);
- if !cached {
- self.camera_overlay = None;
- let rect = self.presentation.rect;
- let rgba = self.convert_camera(input, rect.width, rect.height)?;
- let presentation = self.presentation;
- let mask = self
- .camera_mask
- .get_or_insert_with(|| camera_mask(presentation));
- let overlay = crop_camera(&rgba, presentation, mask)?;
- self.camera_overlay = Some((camera_generation, overlay));
- }
+ self.ensure_camera_overlay(input, camera_generation)?;
Ok(&self
.camera_overlay
.as_ref()
@@ -1117,16 +1325,40 @@ fn crop_camera(
}
let crop_x = (source.width() - rect.width) as usize / 2;
let crop_y = (source.height() - rect.height) as usize / 2;
+ let width = rect.width as usize;
+ let height = rect.height as usize;
let mut output = ffmpeg::frame::Video::new(Pixel::RGBA, rect.width, rect.height);
let source_stride = source.stride(0);
let output_stride = output.stride(0);
let source_data = source.data(0);
let output_data = output.data_mut(0);
- for y in 0..rect.height as usize {
- for x in 0..rect.width as usize {
+ if mask.is_empty()
+ && rgba_window_is_opaque(source_data, source_stride, crop_x, crop_y, width, height)
+ && rgba_rows_fit(output_data.len(), output_stride, 0, width * 4, 0, height)
+ {
+ copy_cropped_rgba_rows(
+ output_data,
+ source_data,
+ PlaneWindow {
+ source_stride,
+ destination_stride: output_stride,
+ source_x: crop_x,
+ source_y: crop_y,
+ destination_x: 0,
+ destination_y: 0,
+ width,
+ height,
+ },
+ presentation.mirrored,
+ );
+ output.set_pts(source.pts());
+ return Ok(output);
+ }
+ for y in 0..height {
+ for x in 0..width {
let source_x = crop_x
+ if presentation.mirrored {
- rect.width as usize - 1 - x
+ width - 1 - x
} else {
x
};
@@ -1139,7 +1371,7 @@ fn crop_camera(
.get_mut(output_offset..output_offset + 4)
.context("RGBA camera output layout is smaller than dimensions")?;
destination.copy_from_slice(source_pixel);
- if let Some(&coverage) = mask.get(y * rect.width as usize + x) {
+ if let Some(&coverage) = mask.get(y * width + x) {
destination[3] = (f64::from(destination[3]) * coverage).round() as u8;
}
}
@@ -1148,48 +1380,126 @@ fn crop_camera(
Ok(output)
}
+#[cfg(test)]
+fn rgba_plane_is_opaque(data: &[u8], stride: usize, width: usize, height: usize) -> bool {
+ rgba_window_is_opaque(data, stride, 0, 0, width, height)
+}
+
+fn rgba_window_is_opaque(
+ data: &[u8],
+ stride: usize,
+ origin_x: usize,
+ origin_y: usize,
+ width: usize,
+ height: usize,
+) -> bool {
+ let row_bytes = width * 4;
+ (0..height).all(|row| {
+ let start = (origin_y + row) * stride + origin_x * 4;
+ data.get(start..start + row_bytes)
+ .is_some_and(|pixels| pixels.chunks_exact(4).all(|pixel| pixel[3] == 255))
+ })
+}
+
+fn rgba_rows_fit(
+ len: usize,
+ stride: usize,
+ origin_bytes: usize,
+ row_bytes: usize,
+ origin_y: usize,
+ height: usize,
+) -> bool {
+ (0..height).all(|row| {
+ let start = (origin_y + row) * stride + origin_bytes;
+ start.checked_add(row_bytes).is_some_and(|end| end <= len)
+ })
+}
+
+struct PlaneWindow {
+ source_stride: usize,
+ destination_stride: usize,
+ source_x: usize,
+ source_y: usize,
+ destination_x: usize,
+ destination_y: usize,
+ width: usize,
+ height: usize,
+}
+
+fn copy_cropped_rgba_rows(
+ destination: &mut [u8],
+ source: &[u8],
+ window: PlaneWindow,
+ mirrored: bool,
+) {
+ let row_bytes = window.width * 4;
+ for row in 0..window.height {
+ let src_start = (window.source_y + row) * window.source_stride + window.source_x * 4;
+ let src = &source[src_start..src_start + row_bytes];
+ let dst_start =
+ (window.destination_y + row) * window.destination_stride + window.destination_x * 4;
+ let dst = &mut destination[dst_start..dst_start + row_bytes];
+ if mirrored {
+ for (dst_pixel, src_pixel) in dst.chunks_exact_mut(4).zip(src.chunks_exact(4).rev()) {
+ dst_pixel.copy_from_slice(src_pixel);
+ }
+ } else {
+ dst.copy_from_slice(src);
+ }
+ }
+}
+
+const RGBA_PACKED: PackedRgbLayout = PackedRgbLayout {
+ bytes_per_pixel: 4,
+ red: 0,
+ green: 1,
+ blue: 2,
+ opaque: Some(3),
+};
+
+const BGRA_PACKED: PackedRgbLayout = PackedRgbLayout {
+ bytes_per_pixel: 4,
+ red: 2,
+ green: 1,
+ blue: 0,
+ opaque: Some(3),
+};
+
+#[cfg(test)]
fn blend_rgba(
destination: &mut ffmpeg::frame::Video,
source: &ffmpeg::frame::Video,
x: usize,
y: usize,
+) -> anyhow::Result<()> {
+ blend_rgba_with_runs(destination, source, x, y, None)
+}
+
+fn blend_rgba_with_runs(
+ destination: &mut ffmpeg::frame::Video,
+ source: &ffmpeg::frame::Video,
+ x: usize,
+ y: usize,
+ runs: Option<&OverlayRuns>,
) -> anyhow::Result<()> {
if destination.format() != ffmpeg::format::Pixel::RGBA
|| source.format() != ffmpeg::format::Pixel::RGBA
{
return Err(anyhow!("RGBA blend requires RGBA frames"));
}
- let destination_stride = destination.stride(0);
- let source_stride = source.stride(0);
- let destination_width = destination.width() as usize;
- let destination_height = destination.height() as usize;
- let source_width = source.width() as usize;
- let source_height = source.height() as usize;
- if x.saturating_add(source_width) > destination_width
- || y.saturating_add(source_height) > destination_height
- {
- return Err(anyhow!("camera overlay exceeds screen frame"));
- }
- let source_data = source.data(0);
- let destination_data = destination.data_mut(0);
- for row in 0..source_height {
- for column in 0..source_width {
- let source_offset = row * source_stride + column * 4;
- let destination_offset = (y + row) * destination_stride + (x + column) * 4;
- let alpha = u16::from(source_data[source_offset + 3]);
- let inverse_alpha = 255 - alpha;
- for channel in 0..3 {
- destination_data[destination_offset + channel] =
- ((u16::from(source_data[source_offset + channel]) * alpha
- + u16::from(destination_data[destination_offset + channel])
- * inverse_alpha
- + 127)
- / 255) as u8;
- }
- destination_data[destination_offset + 3] = 255;
- }
+ blend_overlay(destination, RGBA_PACKED, source, x, y, runs)
+}
+
+#[cfg(test)]
+fn copy_rgba_rows(destination: &mut [u8], source: &[u8], window: PlaneWindow) {
+ let row_bytes = window.width * 4;
+ for row in 0..window.height {
+ let src_start = (window.source_y + row) * window.source_stride + window.source_x * 4;
+ let dst_start =
+ (window.destination_y + row) * window.destination_stride + window.destination_x * 4;
+ destination[dst_start..dst_start + row_bytes]
+ .copy_from_slice(&source[src_start..src_start + row_bytes]);
}
- Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -1229,16 +1539,39 @@ impl PackedRgbLayout {
}
}
+#[cfg(test)]
fn blend_rgba_into_packed(
destination: &mut ffmpeg::frame::Video,
layout: PackedRgbLayout,
source: &ffmpeg::frame::Video,
x: usize,
y: usize,
+) -> anyhow::Result<()> {
+ blend_rgba_into_packed_with_runs(destination, layout, source, x, y, None)
+}
+
+fn blend_rgba_into_packed_with_runs(
+ destination: &mut ffmpeg::frame::Video,
+ layout: PackedRgbLayout,
+ source: &ffmpeg::frame::Video,
+ x: usize,
+ y: usize,
+ runs: Option<&OverlayRuns>,
) -> anyhow::Result<()> {
if source.format() != Pixel::RGBA {
return Err(anyhow!("camera overlay must be RGBA"));
}
+ blend_overlay(destination, layout, source, x, y, runs)
+}
+
+fn blend_overlay(
+ destination: &mut ffmpeg::frame::Video,
+ layout: PackedRgbLayout,
+ source: &ffmpeg::frame::Video,
+ x: usize,
+ y: usize,
+ runs: Option<&OverlayRuns>,
+) -> anyhow::Result<()> {
let source_width = source.width() as usize;
let source_height = source.height() as usize;
if x.saturating_add(source_width) > destination.width() as usize
@@ -1246,37 +1579,206 @@ fn blend_rgba_into_packed(
{
return Err(anyhow!("camera overlay exceeds screen frame"));
}
- let bytes_per_pixel = layout.bytes_per_pixel;
let source_stride = source.stride(0);
let destination_stride = destination.stride(0);
+ let owned_runs;
+ let runs = match runs {
+ Some(runs) if runs.width == source_width && runs.height == source_height => runs,
+ _ => {
+ owned_runs = overlay_runs(source);
+ &owned_runs
+ }
+ };
let source_data = source.data(0);
let destination_data = destination.data_mut(0);
+ let bytes_per_pixel = layout.bytes_per_pixel;
for row in 0..source_height {
let source_row = &source_data[row * source_stride..][..source_width * 4];
let destination_row = &mut destination_data
[(y + row) * destination_stride + x * bytes_per_pixel..]
[..source_width * bytes_per_pixel];
- for (source_pixel, destination_pixel) in source_row
- .chunks_exact(4)
- .zip(destination_row.chunks_exact_mut(bytes_per_pixel))
- {
- let alpha = u16::from(source_pixel[3]);
- let inverse_alpha = 255 - alpha;
- for (channel, offset) in [layout.red, layout.green, layout.blue]
- .into_iter()
- .enumerate()
+ for span in runs.row(row) {
+ apply_overlay_span(source_row, destination_row, *span, layout);
+ }
+ }
+ Ok(())
+}
+
+fn apply_overlay_span(
+ source_row: &[u8],
+ destination_row: &mut [u8],
+ span: OverlaySpan,
+ layout: PackedRgbLayout,
+) {
+ let start = span.start as usize;
+ let len = span.len as usize;
+ let source_span = &source_row[start * 4..][..len * 4];
+ let destination_span =
+ &mut destination_row[start * layout.bytes_per_pixel..][..len * layout.bytes_per_pixel];
+ match span.kind {
+ OverlaySpanKind::Opaque => copy_opaque_span(source_span, destination_span, layout),
+ OverlaySpanKind::Transparent => {
+ // Alpha 0 leaves color untouched. The blend formula still forces a packed opaque byte to 255.
+ if let Some(opaque) = layout.opaque {
+ for pixel in destination_span.chunks_exact_mut(layout.bytes_per_pixel) {
+ pixel[opaque] = 255;
+ }
+ }
+ }
+ OverlaySpanKind::Edge => {
+ for (source_pixel, destination_pixel) in source_span
+ .chunks_exact(4)
+ .zip(destination_span.chunks_exact_mut(layout.bytes_per_pixel))
{
- destination_pixel[offset] = ((u16::from(source_pixel[channel]) * alpha
- + u16::from(destination_pixel[offset]) * inverse_alpha
- + 127)
- / 255) as u8;
+ let alpha = u16::from(source_pixel[3]);
+ let inverse_alpha = 255 - alpha;
+ for (channel, offset) in [layout.red, layout.green, layout.blue]
+ .into_iter()
+ .enumerate()
+ {
+ destination_pixel[offset] = ((u16::from(source_pixel[channel]) * alpha
+ + u16::from(destination_pixel[offset]) * inverse_alpha
+ + 127)
+ / 255) as u8;
+ }
+ if let Some(opaque) = layout.opaque {
+ destination_pixel[opaque] = 255;
+ }
}
- if let Some(opaque) = layout.opaque {
- destination_pixel[opaque] = 255;
+ }
+ }
+}
+
+fn copy_opaque_span(source: &[u8], destination: &mut [u8], layout: PackedRgbLayout) {
+ if layout == RGBA_PACKED {
+ destination.copy_from_slice(source);
+ return;
+ }
+ if layout == BGRA_PACKED {
+ copy_rgba_to_bgra(source, destination);
+ return;
+ }
+ copy_opaque_rgba_pixels(source, destination, layout);
+}
+
+#[cfg(test)]
+fn copy_opaque_rgba_into_packed(
+ destination: &mut [u8],
+ source: &[u8],
+ window: PlaneWindow,
+ layout: PackedRgbLayout,
+) {
+ if layout == RGBA_PACKED {
+ copy_rgba_rows(destination, source, window);
+ return;
+ }
+ if layout == BGRA_PACKED {
+ copy_rgba_rows_to_bgra(destination, source, window);
+ return;
+ }
+ let source_row_bytes = window.width * 4;
+ let destination_row_bytes = window.width * layout.bytes_per_pixel;
+ for row in 0..window.height {
+ let src_start = (window.source_y + row) * window.source_stride + window.source_x * 4;
+ let dst_start = (window.destination_y + row) * window.destination_stride
+ + window.destination_x * layout.bytes_per_pixel;
+ copy_opaque_rgba_pixels(
+ &source[src_start..src_start + source_row_bytes],
+ &mut destination[dst_start..dst_start + destination_row_bytes],
+ layout,
+ );
+ }
+}
+
+fn copy_opaque_rgba_pixels(source: &[u8], destination: &mut [u8], layout: PackedRgbLayout) {
+ for (source_pixel, destination_pixel) in source
+ .chunks_exact(4)
+ .zip(destination.chunks_exact_mut(layout.bytes_per_pixel))
+ {
+ destination_pixel[layout.red] = source_pixel[0];
+ destination_pixel[layout.green] = source_pixel[1];
+ destination_pixel[layout.blue] = source_pixel[2];
+ if let Some(opaque) = layout.opaque {
+ destination_pixel[opaque] = 255;
+ }
+ }
+}
+
+fn copy_rgba_to_bgra(source: &[u8], destination: &mut [u8]) {
+ #[cfg(target_arch = "x86_64")]
+ {
+ if std::arch::is_x86_feature_detected!("avx2") {
+ unsafe {
+ copy_rgba_to_bgra_avx2_bytes(source, destination);
}
+ return;
+ }
+ }
+ copy_rgba_to_bgra_scalar(source, destination);
+}
+
+#[cfg(test)]
+fn copy_rgba_rows_to_bgra(destination: &mut [u8], source: &[u8], window: PlaneWindow) {
+ let row_bytes = window.width * 4;
+ for row in 0..window.height {
+ let src_start = (window.source_y + row) * window.source_stride + window.source_x * 4;
+ let dst_start =
+ (window.destination_y + row) * window.destination_stride + window.destination_x * 4;
+ copy_rgba_to_bgra(
+ &source[src_start..src_start + row_bytes],
+ &mut destination[dst_start..dst_start + row_bytes],
+ );
+ }
+}
+
+fn copy_rgba_to_bgra_scalar(source: &[u8], destination: &mut [u8]) {
+ for (source_pixel, destination_pixel) in
+ source.chunks_exact(4).zip(destination.chunks_exact_mut(4))
+ {
+ destination_pixel[0] = source_pixel[2];
+ destination_pixel[1] = source_pixel[1];
+ destination_pixel[2] = source_pixel[0];
+ destination_pixel[3] = source_pixel[3];
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+#[target_feature(enable = "avx2")]
+unsafe fn copy_rgba_to_bgra_avx2_bytes(source: &[u8], destination: &mut [u8]) {
+ use std::arch::x86_64::*;
+
+ let row_bytes = source.len();
+ let shuffle = _mm256_setr_epi8(
+ 2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15, 18, 17, 16, 19, 22, 21, 20, 23, 26,
+ 25, 24, 27, 30, 29, 28, 31,
+ );
+ let mut offset = 0;
+ while offset + 32 <= row_bytes {
+ unsafe {
+ let loaded = _mm256_loadu_si256(source.as_ptr().add(offset).cast());
+ let shuffled = _mm256_shuffle_epi8(loaded, shuffle);
+ _mm256_storeu_si256(destination.as_mut_ptr().add(offset).cast(), shuffled);
+ }
+ offset += 32;
+ }
+ copy_rgba_to_bgra_scalar(&source[offset..], &mut destination[offset..]);
+}
+
+#[cfg(all(test, target_arch = "x86_64"))]
+#[target_feature(enable = "avx2")]
+unsafe fn copy_rgba_rows_to_bgra_avx2(destination: &mut [u8], source: &[u8], window: PlaneWindow) {
+ let row_bytes = window.width * 4;
+ for row in 0..window.height {
+ let src_start = (window.source_y + row) * window.source_stride + window.source_x * 4;
+ let dst_start =
+ (window.destination_y + row) * window.destination_stride + window.destination_x * 4;
+ unsafe {
+ copy_rgba_to_bgra_avx2_bytes(
+ &source[src_start..src_start + row_bytes],
+ &mut destination[dst_start..dst_start + row_bytes],
+ );
}
}
- Ok(())
}
#[cfg(test)]
@@ -1446,9 +1948,9 @@ mod tests {
fn processed_frame(now: Instant, generation: u64) -> LinuxProcessedCameraFrame {
LinuxProcessedCameraFrame {
- bgra: Arc::from([
+ bgra: LinuxCameraPixels::new([
10, 20, 30, 255, 40, 50, 60, 255, 99, 99, 99, 99, 70, 80, 90, 255, 100, 110, 120,
- 255, 99, 99, 99, 99,
+ 255, 99, 99, 99, 99u8,
]),
dimensions: (2, 2),
stride: 12,
@@ -1531,7 +2033,7 @@ mod tests {
invalid.stride = 7;
assert!(validate_processed_frame(&invalid, frame.processing, 7, now, now).is_err());
invalid = frame.clone();
- invalid.bgra = Arc::from([0u8; 23]);
+ invalid.bgra = LinuxCameraPixels::new([0u8; 23]);
assert!(validate_processed_frame(&invalid, frame.processing, 7, now, now).is_err());
invalid = frame.clone();
invalid.timestamp = Timestamp::SystemTime(std::time::SystemTime::now());
@@ -2205,38 +2707,169 @@ mod tests {
);
}
+ fn plane_buffer_refcount(frame: &ffmpeg::frame::Video) -> i32 {
+ let buffer = unsafe { (*frame.as_ptr()).buf[0] };
+ assert!(!buffer.is_null());
+ unsafe { ffmpeg::ffi::av_buffer_get_ref_count(buffer) }
+ }
+
#[test]
- fn compositor_converts_nv12_screen_and_preserves_input() {
+ fn shared_camera_pixels_survive_one_opaque_composite() {
ffmpeg::init().expect("FFmpeg initializes");
- let timestamp = Timestamp::Instant(Instant::now());
- let info = VideoInfo::from_raw_ffmpeg(Pixel::NV12, 8, 8, 30);
- let mut screen = ffmpeg::frame::Video::new(Pixel::NV12, 8, 8);
- screen.data_mut(0).fill(16);
- screen.data_mut(1).fill(128);
- screen.set_pts(Some(4321));
- let original_y = screen.data(0).to_vec();
- let mut camera = ffmpeg::frame::Video::new(Pixel::BGRA, 4, 4);
- for pixel in camera.data_mut(0).chunks_exact_mut(4) {
- pixel.copy_from_slice(&[0, 0, 255, 255]);
+ let now = Instant::now();
+ let size = 16u32;
+ let row = size as usize * 4;
+ let mut bytes = vec![0u8; row * size as usize];
+ for pixel in bytes.chunks_exact_mut(4) {
+ pixel.copy_from_slice(&[11, 22, 33, 255]);
}
- let screen_frame = FFmpegVideoFrame {
- inner: screen,
- timestamp,
+ let bgra = LinuxCameraPixels::new(bytes);
+ let shared_pixels = bgra.clone();
+ let source = shared_pixels.as_ptr();
+ let processed = LinuxProcessedCameraFrame {
+ bgra,
+ dimensions: (size, size),
+ stride: row,
+ timestamp: Timestamp::Instant(now),
+ generation: 3,
+ processing: LinuxCameraProcessing {
+ mirrored: false,
+ blur: LinuxCameraBlur::Off,
+ },
+ mask: None,
};
+ let converted = processed_to_ffmpeg(&processed).expect("processed frame");
+ assert_eq!(processed.bgra.as_ptr(), source);
+ assert_eq!(shared_pixels.as_ptr(), source);
+ assert_ne!(converted.inner.data(0).as_ptr(), source);
+ let camera_pixels = converted.inner.data(0).as_ptr();
+ let shared_frame = shared_ref(&converted.inner);
+ assert_eq!(plane_buffer_refcount(&converted.inner), 2);
+ assert_eq!(shared_frame.data(0).as_ptr(), camera_pixels);
+
+ let timestamp = converted.timestamp;
let camera_frame = FFmpegVideoFrame {
- inner: camera,
+ inner: converted.inner,
timestamp,
};
- let mut compositor = Compositor::new(
- info,
- LinuxCameraPresentation::default_for(info.width, info.height),
- );
- let shared = shared_ref(&screen_frame.inner);
- let output = compositor
- .compose(screen_frame, &camera_frame, 0)
- .expect("composition succeeds");
- assert_eq!(shared.data(0), original_y.as_slice());
- assert_eq!(output.inner.format(), Pixel::NV12);
+ let screen_size = 20u32;
+ let origin_x = 2usize;
+ let origin_y = 1usize;
+ let info = VideoInfo::from_raw_ffmpeg(Pixel::BGRA, screen_size, screen_size, 30);
+ let presentation = LinuxCameraPresentation {
+ rect: LinuxCameraRect {
+ x: origin_x as u32,
+ y: origin_y as u32,
+ width: size,
+ height: size,
+ },
+ shape: LinuxCameraShape::Round,
+ mirrored: false,
+ effect: LinuxCameraEffect::None,
+ };
+ let mut template = ffmpeg::frame::Video::new(Pixel::BGRA, screen_size, screen_size);
+ template.data_mut(0).fill(9);
+ template.set_pts(Some(42));
+ let mut compositor = Compositor::new(info, presentation);
+ let layout = PackedRgbLayout::of(Pixel::BGRA).expect("bgra layout");
+ let first = compositor
+ .compose(
+ FFmpegVideoFrame {
+ inner: copy_frame(&template),
+ timestamp,
+ },
+ &camera_frame,
+ 4,
+ )
+ .expect("first composite");
+ assert_eq!(compositor.run_cache.builds, 1);
+ assert!(
+ compositor
+ .run_cache
+ .runs
+ .spans
+ .iter()
+ .any(|span| span.kind == OverlaySpanKind::Opaque)
+ );
+ let overlay_pixels = compositor
+ .camera_overlay
+ .as_ref()
+ .expect("overlay")
+ .1
+ .data(0)
+ .as_ptr();
+ let mut expected = copy_frame(&template);
+ formula_blend_packed(
+ &mut expected,
+ layout,
+ &compositor.camera_overlay.as_ref().expect("overlay").1,
+ origin_x,
+ origin_y,
+ );
+ assert_planes_equal(&first.inner, &expected);
+
+ let second = compositor
+ .compose(
+ FFmpegVideoFrame {
+ inner: copy_frame(&template),
+ timestamp,
+ },
+ &camera_frame,
+ 4,
+ )
+ .expect("second composite");
+ assert_eq!(compositor.run_cache.builds, 1);
+ assert_eq!(
+ compositor
+ .camera_overlay
+ .as_ref()
+ .expect("overlay")
+ .1
+ .data(0)
+ .as_ptr(),
+ overlay_pixels
+ );
+ assert_planes_equal(&second.inner, &expected);
+ assert_eq!(camera_frame.inner.data(0).as_ptr(), camera_pixels);
+ assert_eq!(shared_frame.data(0).as_ptr(), camera_pixels);
+ assert_eq!(plane_buffer_refcount(&camera_frame.inner), 2);
+ assert_eq!(processed.bgra.as_ptr(), source);
+ assert_eq!(shared_pixels.as_ptr(), source);
+ assert_eq!(&shared_pixels[..4], &[11, 22, 33, 255]);
+ }
+
+ #[test]
+ fn compositor_converts_nv12_screen_and_preserves_input() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ let timestamp = Timestamp::Instant(Instant::now());
+ let info = VideoInfo::from_raw_ffmpeg(Pixel::NV12, 8, 8, 30);
+ let mut screen = ffmpeg::frame::Video::new(Pixel::NV12, 8, 8);
+ screen.data_mut(0).fill(16);
+ screen.data_mut(1).fill(128);
+ screen.set_pts(Some(4321));
+ let original_y = screen.data(0).to_vec();
+ let mut camera = ffmpeg::frame::Video::new(Pixel::BGRA, 4, 4);
+ for pixel in camera.data_mut(0).chunks_exact_mut(4) {
+ pixel.copy_from_slice(&[0, 0, 255, 255]);
+ }
+ let screen_frame = FFmpegVideoFrame {
+ inner: screen,
+ timestamp,
+ };
+ let camera_frame = FFmpegVideoFrame {
+ inner: camera,
+ timestamp,
+ };
+ let mut compositor = Compositor::new(
+ info,
+ LinuxCameraPresentation::default_for(info.width, info.height),
+ );
+ let shared = shared_ref(&screen_frame.inner);
+ let output = compositor
+ .compose(screen_frame, &camera_frame, 0)
+ .expect("composition succeeds");
+ assert_eq!(shared.data(0), original_y.as_slice());
+ assert_eq!(output.inner.format(), Pixel::NV12);
assert_eq!(output.inner.pts(), Some(4321));
assert_ne!(output.inner.data(0)[output.inner.stride(0) * 7 + 7], 16);
}
@@ -2391,8 +3024,11 @@ mod tests {
timestamp,
};
let first = compositor.compose(screen(), &camera(1), 1).unwrap();
+ assert_eq!(compositor.run_cache.builds, 1);
let reused = compositor.compose(screen(), &camera(2), 1).unwrap();
+ assert_eq!(compositor.run_cache.builds, 1);
let refreshed = compositor.compose(screen(), &camera(2), 2).unwrap();
+ assert_eq!(compositor.run_cache.builds, 2);
assert_eq!(first.inner.data(0), reused.inner.data(0));
assert_ne!(first.inner.data(0), refreshed.inner.data(0));
}
@@ -2445,4 +3081,1080 @@ mod tests {
run("in place, 30 fps camera / 60 fps", false, 2);
}
}
+
+ fn paint_rgba(frame: &mut ffmpeg::frame::Video, pixel: impl Fn(usize, usize) -> [u8; 4]) {
+ let stride = frame.stride(0);
+ let width = frame.width() as usize;
+ let height = frame.height() as usize;
+ let data = frame.data_mut(0);
+ for y in 0..height {
+ for x in 0..width {
+ let offset = y * stride + x * 4;
+ data[offset..offset + 4].copy_from_slice(&pixel(x, y));
+ }
+ }
+ }
+
+ fn mark_padding(frame: &mut ffmpeg::frame::Video, bytes_per_pixel: usize) {
+ let stride = frame.stride(0);
+ let row_bytes = frame.width() as usize * bytes_per_pixel;
+ let height = frame.height() as usize;
+ let data = frame.data_mut(0);
+ for y in 0..height {
+ let start = y * stride + row_bytes;
+ let end = y * stride + stride;
+ if row_bytes < stride && end <= data.len() {
+ data[start..end].fill(0xA5);
+ }
+ }
+ }
+
+ fn formula_blend_rgba(
+ destination: &mut ffmpeg::frame::Video,
+ source: &ffmpeg::frame::Video,
+ x: usize,
+ y: usize,
+ ) {
+ let destination_stride = destination.stride(0);
+ let source_stride = source.stride(0);
+ let source_width = source.width() as usize;
+ let source_height = source.height() as usize;
+ let source_data = source.data(0);
+ let destination_data = destination.data_mut(0);
+ for row in 0..source_height {
+ for column in 0..source_width {
+ let source_offset = row * source_stride + column * 4;
+ let destination_offset = (y + row) * destination_stride + (x + column) * 4;
+ let alpha = u16::from(source_data[source_offset + 3]);
+ let inverse_alpha = 255 - alpha;
+ for channel in 0..3 {
+ destination_data[destination_offset + channel] =
+ ((u16::from(source_data[source_offset + channel]) * alpha
+ + u16::from(destination_data[destination_offset + channel])
+ * inverse_alpha
+ + 127)
+ / 255) as u8;
+ }
+ destination_data[destination_offset + 3] = 255;
+ }
+ }
+ }
+
+ fn formula_blend_packed(
+ destination: &mut ffmpeg::frame::Video,
+ layout: PackedRgbLayout,
+ source: &ffmpeg::frame::Video,
+ x: usize,
+ y: usize,
+ ) {
+ let bytes_per_pixel = layout.bytes_per_pixel;
+ let source_stride = source.stride(0);
+ let destination_stride = destination.stride(0);
+ let source_width = source.width() as usize;
+ let source_height = source.height() as usize;
+ let source_data = source.data(0);
+ let destination_data = destination.data_mut(0);
+ for row in 0..source_height {
+ let source_row = &source_data[row * source_stride..][..source_width * 4];
+ let destination_row = &mut destination_data
+ [(y + row) * destination_stride + x * bytes_per_pixel..]
+ [..source_width * bytes_per_pixel];
+ for (source_pixel, destination_pixel) in source_row
+ .chunks_exact(4)
+ .zip(destination_row.chunks_exact_mut(bytes_per_pixel))
+ {
+ let alpha = u16::from(source_pixel[3]);
+ let inverse_alpha = 255 - alpha;
+ for (channel, offset) in [layout.red, layout.green, layout.blue]
+ .into_iter()
+ .enumerate()
+ {
+ destination_pixel[offset] = ((u16::from(source_pixel[channel]) * alpha
+ + u16::from(destination_pixel[offset]) * inverse_alpha
+ + 127)
+ / 255) as u8;
+ }
+ if let Some(opaque) = layout.opaque {
+ destination_pixel[opaque] = 255;
+ }
+ }
+ }
+ }
+
+ fn formula_crop_camera(
+ source: &ffmpeg::frame::Video,
+ presentation: LinuxCameraPresentation,
+ mask: &[f64],
+ ) -> ffmpeg::frame::Video {
+ let rect = presentation.rect;
+ let crop_x = (source.width() - rect.width) as usize / 2;
+ let crop_y = (source.height() - rect.height) as usize / 2;
+ let mut output = ffmpeg::frame::Video::new(Pixel::RGBA, rect.width, rect.height);
+ let source_stride = source.stride(0);
+ let output_stride = output.stride(0);
+ let source_data = source.data(0);
+ let output_data = output.data_mut(0);
+ for y in 0..rect.height as usize {
+ for x in 0..rect.width as usize {
+ let source_x = crop_x
+ + if presentation.mirrored {
+ rect.width as usize - 1 - x
+ } else {
+ x
+ };
+ let source_offset = (crop_y + y) * source_stride + source_x * 4;
+ let output_offset = y * output_stride + x * 4;
+ let source_pixel = &source_data[source_offset..source_offset + 4];
+ let destination = &mut output_data[output_offset..output_offset + 4];
+ destination.copy_from_slice(source_pixel);
+ if let Some(&coverage) = mask.get(y * rect.width as usize + x) {
+ destination[3] = (f64::from(destination[3]) * coverage).round() as u8;
+ }
+ }
+ }
+ output.set_pts(source.pts());
+ output
+ }
+
+ fn opaque_pixel(x: usize, y: usize) -> [u8; 4] {
+ let mixed = (x.wrapping_mul(17) + y.wrapping_mul(29)) as u8;
+ [mixed, mixed.wrapping_mul(3), mixed.wrapping_add(40), 255]
+ }
+
+ fn assert_planes_equal(actual: &ffmpeg::frame::Video, expected: &ffmpeg::frame::Video) {
+ assert_eq!(actual.format(), expected.format());
+ assert_eq!(actual.width(), expected.width());
+ assert_eq!(actual.height(), expected.height());
+ assert_eq!(actual.stride(0), expected.stride(0));
+ assert_eq!(actual.pts(), expected.pts());
+ let layout = PackedRgbLayout::of(actual.format()).expect("packed layout");
+ let row_bytes = actual.width() as usize * layout.bytes_per_pixel;
+ let height = actual.height() as usize;
+ let actual_data = actual.data(0);
+ let expected_data = expected.data(0);
+ for row in 0..height {
+ let start = row * actual.stride(0);
+ assert_eq!(
+ &actual_data[start..start + row_bytes],
+ &expected_data[start..start + row_bytes],
+ "row {row}"
+ );
+ }
+ }
+
+ #[test]
+ fn opaque_copy_blend_rgba_matches_formula() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ for (source_width, source_height, origin_x, origin_y) in [
+ (1, 1, 0, 0),
+ (7, 3, 1, 2),
+ (8, 2, 0, 0),
+ (9, 4, 3, 1),
+ (15, 2, 0, 1),
+ (17, 3, 2, 0),
+ (324, 2, 5, 1),
+ ] {
+ let mut source = ffmpeg::frame::Video::new(Pixel::RGBA, source_width, source_height);
+ source.set_pts(Some(44));
+ paint_rgba(&mut source, opaque_pixel);
+ mark_padding(&mut source, 4);
+ let mut fast = ffmpeg::frame::Video::new(
+ Pixel::RGBA,
+ source_width + origin_x,
+ source_height + origin_y + 2,
+ );
+ paint_rgba(&mut fast, |_, _| [1, 2, 3, 9]);
+ mark_padding(&mut fast, 4);
+ let mut slow = copy_frame(&fast);
+ blend_rgba(&mut fast, &source, origin_x as usize, origin_y as usize)
+ .expect("opaque rgba blend");
+ formula_blend_rgba(&mut slow, &source, origin_x as usize, origin_y as usize);
+ assert_planes_equal(&fast, &slow);
+ }
+ }
+
+ #[test]
+ fn opaque_copy_blend_packed_matches_formula() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ let formats = [
+ Pixel::BGRA,
+ Pixel::BGRZ,
+ Pixel::RGBA,
+ Pixel::RGBZ,
+ Pixel::ARGB,
+ Pixel::ABGR,
+ Pixel::RGB24,
+ Pixel::BGR24,
+ ];
+ for format in formats {
+ let layout = PackedRgbLayout::of(format).expect("packed layout");
+ for (source_width, source_height, origin_x, origin_y) in [
+ (1, 1, 0, 0),
+ (7, 3, 1, 1),
+ (9, 2, 0, 0),
+ (17, 2, 4, 0),
+ (324, 1, 1, 0),
+ ] {
+ let mut source =
+ ffmpeg::frame::Video::new(Pixel::RGBA, source_width, source_height);
+ paint_rgba(&mut source, opaque_pixel);
+ let mut fast = ffmpeg::frame::Video::new(
+ format,
+ source_width + origin_x,
+ source_height + origin_y + 1,
+ );
+ fast.data_mut(0).fill(17);
+ mark_padding(&mut fast, layout.bytes_per_pixel);
+ let mut slow = copy_frame(&fast);
+ blend_rgba_into_packed(
+ &mut fast,
+ layout,
+ &source,
+ origin_x as usize,
+ origin_y as usize,
+ )
+ .expect("opaque packed blend");
+ formula_blend_packed(
+ &mut slow,
+ layout,
+ &source,
+ origin_x as usize,
+ origin_y as usize,
+ );
+ assert_planes_equal(&fast, &slow);
+ }
+ }
+ }
+
+ #[test]
+ fn opaque_copy_partial_alpha_matches_formula() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ let mut source = ffmpeg::frame::Video::new(Pixel::RGBA, 1, 1);
+ paint_rgba(&mut source, |_, _| [10, 20, 30, 128]);
+ let mut fast = ffmpeg::frame::Video::new(Pixel::RGBA, 1, 1);
+ paint_rgba(&mut fast, |_, _| [1, 2, 3, 4]);
+ let mut slow = copy_frame(&fast);
+ blend_rgba(&mut fast, &source, 0, 0).expect("partial rgba blend");
+ formula_blend_rgba(&mut slow, &source, 0, 0);
+ assert_eq!(&fast.data(0)[..4], &[6, 11, 17, 255]);
+ assert_planes_equal(&fast, &slow);
+
+ let mut bgra = ffmpeg::frame::Video::new(Pixel::BGRA, 1, 1);
+ paint_rgba(&mut bgra, |_, _| [1, 2, 3, 4]);
+ let layout = PackedRgbLayout::of(Pixel::BGRA).expect("bgra layout");
+ let mut bgra_slow = copy_frame(&bgra);
+ blend_rgba_into_packed(&mut bgra, layout, &source, 0, 0).expect("partial bgra blend");
+ formula_blend_packed(&mut bgra_slow, layout, &source, 0, 0);
+ assert_eq!(&bgra.data(0)[..4], &[16, 11, 7, 255]);
+ assert_planes_equal(&bgra, &bgra_slow);
+
+ for (width, height) in [(7, 2), (9, 3), (17, 1), (324, 2)] {
+ let mut source = ffmpeg::frame::Video::new(Pixel::RGBA, width, height);
+ paint_rgba(&mut source, |x, y| {
+ let alpha = if x + 1 == width as usize && y + 1 == height as usize {
+ 254
+ } else {
+ ((x * 13 + y * 5) % 255) as u8
+ };
+ [x as u8, y as u8, alpha, alpha]
+ });
+ let mut rgba = ffmpeg::frame::Video::new(Pixel::RGBA, width, height);
+ rgba.data_mut(0).fill(9);
+ let mut rgba_slow = copy_frame(&rgba);
+ blend_rgba(&mut rgba, &source, 0, 0).expect("partial rgba plane");
+ formula_blend_rgba(&mut rgba_slow, &source, 0, 0);
+ assert_planes_equal(&rgba, &rgba_slow);
+ assert_ne!(rgba.data(0), source.data(0));
+
+ let mut packed = ffmpeg::frame::Video::new(Pixel::BGRA, width, height);
+ packed.data_mut(0).fill(9);
+ let mut packed_slow = copy_frame(&packed);
+ blend_rgba_into_packed(&mut packed, layout, &source, 0, 0).expect("partial bgra plane");
+ formula_blend_packed(&mut packed_slow, layout, &source, 0, 0);
+ assert_planes_equal(&packed, &packed_slow);
+ }
+ }
+
+ #[test]
+ fn opaque_copy_crop_matches_formula() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ for (source_width, source_height, width, height, mirrored, partial) in [
+ (1, 1, 1, 1, false, false),
+ (7, 3, 7, 3, false, false),
+ (11, 5, 7, 3, true, false),
+ (9, 4, 9, 4, false, false),
+ (20, 6, 17, 5, true, false),
+ (324, 3, 324, 3, false, false),
+ (15, 4, 9, 3, false, true),
+ (16, 4, 15, 3, true, true),
+ ] {
+ let mut source = ffmpeg::frame::Video::new(Pixel::RGBA, source_width, source_height);
+ source.set_pts(Some(18));
+ paint_rgba(&mut source, |x, y| {
+ let mut pixel = opaque_pixel(x, y);
+ if partial {
+ pixel[3] = ((x * 19 + y * 7) % 251) as u8;
+ }
+ pixel
+ });
+ mark_padding(&mut source, 4);
+ let presentation = LinuxCameraPresentation {
+ rect: LinuxCameraRect {
+ x: 0,
+ y: 0,
+ width,
+ height,
+ },
+ shape: LinuxCameraShape::RoundedRectangle { radius_pixels: 0 },
+ mirrored,
+ effect: LinuxCameraEffect::None,
+ };
+ let actual = crop_camera(&source, presentation, &[]).expect("empty mask crop");
+ let expected = formula_crop_camera(&source, presentation, &[]);
+ assert_planes_equal(&actual, &expected);
+ if partial {
+ assert!(actual.data(0).chunks_exact(4).any(|pixel| pixel[3] != 255));
+ }
+ }
+ }
+
+ #[test]
+ fn opaque_copy_nonempty_mask_crop_matches_formula() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ for (width, height, radius, mirrored) in [
+ (8, 8, 3, false),
+ (9, 7, 2, true),
+ (17, 15, 4, false),
+ (15, 15, 7, true),
+ ] {
+ let mut source = ffmpeg::frame::Video::new(Pixel::RGBA, width + 4, height + 2);
+ source.set_pts(Some(3));
+ paint_rgba(&mut source, opaque_pixel);
+ let presentation = LinuxCameraPresentation {
+ rect: LinuxCameraRect {
+ x: 0,
+ y: 0,
+ width,
+ height,
+ },
+ shape: LinuxCameraShape::RoundedRectangle {
+ radius_pixels: radius,
+ },
+ mirrored,
+ effect: LinuxCameraEffect::None,
+ };
+ let mask = camera_mask(presentation);
+ assert!(!mask.is_empty());
+ let actual = crop_camera(&source, presentation, &mask).expect("masked crop");
+ let expected = formula_crop_camera(&source, presentation, &mask);
+ assert_planes_equal(&actual, &expected);
+ let unmasked = formula_crop_camera(
+ &source,
+ LinuxCameraPresentation {
+ shape: LinuxCameraShape::RoundedRectangle { radius_pixels: 0 },
+ ..presentation
+ },
+ &[],
+ );
+ assert_ne!(
+ defined_bytes(&actual),
+ defined_bytes(&unmasked),
+ "mask changed no pixels"
+ );
+ }
+
+ let mut source = ffmpeg::frame::Video::new(Pixel::RGBA, 5, 1);
+ paint_rgba(&mut source, |x, _| [x as u8, 4, 5, 200]);
+ let presentation = presentation(5, 1);
+ let mask = [0.0_f64, 0.25, 1.0];
+ let actual = crop_camera(&source, presentation, &mask).expect("short mask crop");
+ let expected = formula_crop_camera(&source, presentation, &mask);
+ assert_planes_equal(&actual, &expected);
+ assert_eq!(actual.data(0)[3], 0);
+ assert_eq!(actual.data(0)[7], 50);
+ assert_eq!(actual.data(0)[11], 200);
+ assert_eq!(actual.data(0)[19], 200);
+ }
+
+ #[test]
+ fn opaque_copy_scalar_bgra_matches_formula() {
+ for width in [1_usize, 7, 8, 9, 15, 17, 324] {
+ let source: Vec<u8> = (0..width * 4)
+ .map(|index| {
+ if index % 4 == 3 {
+ 255
+ } else {
+ (index * 13 + 5) as u8
+ }
+ })
+ .collect();
+ let mut scalar = vec![9_u8; width * 4];
+ let mut formula = scalar.clone();
+ copy_rgba_to_bgra_scalar(&source, &mut scalar);
+ for (src, dst) in source.chunks_exact(4).zip(formula.chunks_exact_mut(4)) {
+ let alpha = u16::from(src[3]);
+ let inverse_alpha = 255 - alpha;
+ let offsets = [2_usize, 1, 0];
+ for (channel, offset) in offsets.into_iter().enumerate() {
+ dst[offset] = ((u16::from(src[channel]) * alpha
+ + u16::from(dst[offset]) * inverse_alpha
+ + 127)
+ / 255) as u8;
+ }
+ dst[3] = 255;
+ }
+ assert_eq!(scalar, formula, "scalar width {width}");
+ assert_eq!(scalar[0], source[2]);
+ assert_eq!(scalar[2], source[0]);
+ }
+ }
+
+ #[test]
+ #[cfg(target_arch = "x86_64")]
+ fn opaque_copy_avx2_bgra_matches_scalar_fallback() {
+ if !std::arch::is_x86_feature_detected!("avx2") {
+ return;
+ }
+ for (width, height, origin_x) in [
+ (1_usize, 1_usize, 0_usize),
+ (7, 2, 1),
+ (8, 2, 0),
+ (9, 3, 3),
+ (17, 2, 1),
+ (324, 2, 5),
+ ] {
+ let row_bytes = width * 4;
+ let source_stride = row_bytes + 12;
+ let destination_stride = (origin_x + width) * 4 + 8;
+ let mut source = vec![0_u8; source_stride * height];
+ for (index, byte) in source.iter_mut().enumerate() {
+ *byte = if index % 4 == 3 {
+ 255
+ } else {
+ (index * 19 + 3) as u8
+ };
+ }
+ let mut avx = vec![8_u8; destination_stride * (height + 1)];
+ let mut scalar = avx.clone();
+ unsafe {
+ copy_rgba_rows_to_bgra_avx2(
+ &mut avx,
+ &source,
+ PlaneWindow {
+ source_stride,
+ destination_stride,
+ source_x: 0,
+ source_y: 0,
+ destination_x: origin_x,
+ destination_y: 0,
+ width,
+ height,
+ },
+ );
+ }
+ for row in 0..height {
+ copy_rgba_to_bgra_scalar(
+ &source[row * source_stride..row * source_stride + row_bytes],
+ &mut scalar[row * destination_stride + origin_x * 4..][..row_bytes],
+ );
+ }
+ assert_eq!(avx, scalar, "avx2 width {width}");
+ }
+ }
+
+ fn span(start: u32, len: u32, kind: OverlaySpanKind) -> OverlaySpan {
+ OverlaySpan { start, len, kind }
+ }
+
+ #[test]
+ fn alpha_row_runs_cover_opaque_transparent_edge_and_ends() {
+ assert_eq!(
+ classify_alpha_row(&[255, 255, 255, 255]),
+ vec![span(0, 4, OverlaySpanKind::Opaque)]
+ );
+ assert_eq!(
+ classify_alpha_row(&[0, 0, 0]),
+ vec![span(0, 3, OverlaySpanKind::Transparent)]
+ );
+ assert_eq!(
+ classify_alpha_row(&[0, 0, 255, 255, 255, 40, 41, 0]),
+ vec![
+ span(0, 2, OverlaySpanKind::Transparent),
+ span(2, 3, OverlaySpanKind::Opaque),
+ span(5, 2, OverlaySpanKind::Edge),
+ span(7, 1, OverlaySpanKind::Transparent),
+ ]
+ );
+ assert_eq!(
+ classify_alpha_row(&[128]),
+ vec![span(0, 1, OverlaySpanKind::Edge)]
+ );
+ assert_eq!(
+ classify_alpha_row(&[255, 255, 10, 0, 0]),
+ vec![
+ span(0, 2, OverlaySpanKind::Opaque),
+ span(2, 1, OverlaySpanKind::Edge),
+ span(3, 2, OverlaySpanKind::Transparent),
+ ]
+ );
+ assert_eq!(
+ classify_alpha_row(&[0, 12, 255, 255]),
+ vec![
+ span(0, 1, OverlaySpanKind::Transparent),
+ span(1, 1, OverlaySpanKind::Edge),
+ span(2, 2, OverlaySpanKind::Opaque),
+ ]
+ );
+ }
+
+ #[test]
+ fn overlay_run_cache_invalidates_when_generation_or_mask_changes() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ let mut frame = ffmpeg::frame::Video::new(Pixel::RGBA, 4, 1);
+ paint_rgba(&mut frame, |_, _| [1, 2, 3, 255]);
+ let mask = std::sync::Arc::<[f64]>::from(vec![1.0_f64, 0.25]);
+ let mut cache = OverlayRunCache::default();
+ let first = cache.resolve(1, &mask, &frame).clone();
+ assert_eq!(cache.builds, 1);
+ assert_eq!(first.spans, vec![span(0, 4, OverlaySpanKind::Opaque)]);
+
+ frame.data_mut(0)[3] = 0;
+ let cached = cache.resolve(1, &mask, &frame).clone();
+ assert_eq!(cache.builds, 1);
+ assert_eq!(cached, first);
+
+ let rebuilt = cache.resolve(2, &mask, &frame).clone();
+ assert_eq!(cache.builds, 2);
+ assert_eq!(
+ rebuilt.spans,
+ vec![
+ span(0, 1, OverlaySpanKind::Transparent),
+ span(1, 3, OverlaySpanKind::Opaque),
+ ]
+ );
+
+ let same_mask = std::sync::Arc::<[f64]>::from(vec![1.0_f64, 0.25]);
+ let still = cache.resolve(2, &same_mask, &frame).clone();
+ assert_eq!(cache.builds, 2);
+ assert_eq!(still, rebuilt);
+
+ frame.data_mut(0)[7] = 40;
+ let changed_mask = std::sync::Arc::<[f64]>::from(vec![0.0_f64, 1.0]);
+ let masked = cache.resolve(2, &changed_mask, &frame).clone();
+ assert_eq!(cache.builds, 3);
+ assert_ne!(masked, rebuilt);
+ assert!(
+ masked
+ .spans
+ .iter()
+ .any(|span| span.kind == OverlaySpanKind::Edge)
+ );
+
+ let mut wider = ffmpeg::frame::Video::new(Pixel::RGBA, 5, 1);
+ paint_rgba(&mut wider, |_, _| [4, 5, 6, 255]);
+ cache.resolve(2, &changed_mask, &wider);
+ assert_eq!(cache.builds, 4);
+ assert_eq!(cache.runs.width, 5);
+ cache.resolve(2, &changed_mask, &wider);
+ assert_eq!(cache.builds, 4);
+ }
+
+ #[test]
+ fn compositor_run_cache_invalidates_when_mask_changes() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ let info = VideoInfo::from_raw_ffmpeg(Pixel::BGRA, 32, 32, 30);
+ let presentation = LinuxCameraPresentation {
+ rect: LinuxCameraRect {
+ x: 0,
+ y: 0,
+ width: 16,
+ height: 16,
+ },
+ shape: LinuxCameraShape::Round,
+ mirrored: false,
+ effect: LinuxCameraEffect::None,
+ };
+ let mut compositor = Compositor::new(info, presentation);
+ let camera = pattern_frame(Pixel::RGBA, 16, 16, 4);
+ compositor
+ .ensure_camera_overlay(&camera, 7)
+ .expect("first overlay");
+ assert_eq!(compositor.run_cache.builds, 1);
+ let cached_runs = compositor.run_cache.runs.clone();
+ compositor
+ .ensure_camera_overlay(&camera, 7)
+ .expect("cached overlay");
+ assert_eq!(compositor.run_cache.builds, 1);
+ assert_eq!(compositor.run_cache.runs, cached_runs);
+ let mask = compositor.camera_mask.clone().expect("camera mask");
+ assert!(!mask.is_empty());
+ compositor.camera_mask = Some(std::sync::Arc::from(
+ mask.iter()
+ .map(|coverage| 1.0 - coverage)
+ .collect::<Vec<_>>(),
+ ));
+ compositor
+ .ensure_camera_overlay(&camera, 7)
+ .expect("mask change rebuilds overlay");
+ assert_eq!(compositor.run_cache.builds, 2);
+ compositor
+ .ensure_camera_overlay(&camera, 8)
+ .expect("generation change rebuilds overlay");
+ assert_eq!(compositor.run_cache.builds, 3);
+ }
+
+ #[test]
+ fn shaped_overlay_blend_matches_formula() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ let cases = [
+ (17_u32, LinuxCameraShape::Round),
+ (17, LinuxCameraShape::RoundedRectangle { radius_pixels: 4 }),
+ (48, LinuxCameraShape::RoundedRectangle { radius_pixels: 24 }),
+ ];
+ for (size, shape) in cases {
+ let mut camera = ffmpeg::frame::Video::new(Pixel::RGBA, size, size);
+ paint_rgba(&mut camera, opaque_pixel);
+ let presentation = LinuxCameraPresentation {
+ rect: LinuxCameraRect {
+ x: 1,
+ y: 2,
+ width: size,
+ height: size,
+ },
+ shape,
+ mirrored: false,
+ effect: LinuxCameraEffect::None,
+ };
+ let mask = camera_mask(presentation);
+ assert!(!mask.is_empty(), "{shape:?}");
+ let overlay = crop_camera(&camera, presentation, &mask).expect("shaped overlay");
+ let runs = overlay_runs(&overlay);
+ assert!(
+ runs.spans
+ .iter()
+ .any(|span| span.kind == OverlaySpanKind::Opaque),
+ "{shape:?} opaque"
+ );
+ assert!(
+ runs.spans
+ .iter()
+ .any(|span| span.kind == OverlaySpanKind::Transparent),
+ "{shape:?} transparent"
+ );
+ assert!(
+ runs.spans
+ .iter()
+ .any(|span| span.kind == OverlaySpanKind::Edge),
+ "{shape:?} edge"
+ );
+ let origin_x = 1_usize;
+ let origin_y = 2_usize;
+ let mut rgba = ffmpeg::frame::Video::new(Pixel::RGBA, size + 3, size + 4);
+ rgba.data_mut(0).fill(9);
+ mark_padding(&mut rgba, 4);
+ let mut rgba_formula = copy_frame(&rgba);
+ blend_rgba_with_runs(&mut rgba, &overlay, origin_x, origin_y, Some(&runs))
+ .expect("rgba runs blend");
+ formula_blend_rgba(&mut rgba_formula, &overlay, origin_x, origin_y);
+ assert_planes_equal(&rgba, &rgba_formula);
+
+ let mut bgra = ffmpeg::frame::Video::new(Pixel::BGRA, size + 3, size + 4);
+ bgra.data_mut(0).fill(9);
+ mark_padding(&mut bgra, 4);
+ let mut bgra_formula = copy_frame(&bgra);
+ let layout = PackedRgbLayout::of(Pixel::BGRA).expect("bgra layout");
+ blend_rgba_into_packed_with_runs(
+ &mut bgra,
+ layout,
+ &overlay,
+ origin_x,
+ origin_y,
+ Some(&runs),
+ )
+ .expect("bgra runs blend");
+ formula_blend_packed(&mut bgra_formula, layout, &overlay, origin_x, origin_y);
+ assert_planes_equal(&bgra, &bgra_formula);
+ }
+ }
+
+ fn median_nanos(samples: &mut [u128]) -> u128 {
+ samples.sort_unstable();
+ samples[samples.len() / 2]
+ }
+
+ fn previous_blend_rgba(
+ destination: &mut ffmpeg::frame::Video,
+ source: &ffmpeg::frame::Video,
+ x: usize,
+ y: usize,
+ ) -> anyhow::Result<()> {
+ if destination.format() != ffmpeg::format::Pixel::RGBA
+ || source.format() != ffmpeg::format::Pixel::RGBA
+ {
+ return Err(anyhow!("RGBA blend requires RGBA frames"));
+ }
+ let destination_stride = destination.stride(0);
+ let source_stride = source.stride(0);
+ let destination_width = destination.width() as usize;
+ let destination_height = destination.height() as usize;
+ let source_width = source.width() as usize;
+ let source_height = source.height() as usize;
+ if x.saturating_add(source_width) > destination_width
+ || y.saturating_add(source_height) > destination_height
+ {
+ return Err(anyhow!("camera overlay exceeds screen frame"));
+ }
+ let source_data = source.data(0);
+ let destination_data = destination.data_mut(0);
+ if rgba_plane_is_opaque(source_data, source_stride, source_width, source_height) {
+ copy_rgba_rows(
+ destination_data,
+ source_data,
+ PlaneWindow {
+ source_stride,
+ destination_stride,
+ source_x: 0,
+ source_y: 0,
+ destination_x: x,
+ destination_y: y,
+ width: source_width,
+ height: source_height,
+ },
+ );
+ return Ok(());
+ }
+ for row in 0..source_height {
+ for column in 0..source_width {
+ let source_offset = row * source_stride + column * 4;
+ let destination_offset = (y + row) * destination_stride + (x + column) * 4;
+ let alpha = u16::from(source_data[source_offset + 3]);
+ let inverse_alpha = 255 - alpha;
+ for channel in 0..3 {
+ destination_data[destination_offset + channel] =
+ ((u16::from(source_data[source_offset + channel]) * alpha
+ + u16::from(destination_data[destination_offset + channel])
+ * inverse_alpha
+ + 127)
+ / 255) as u8;
+ }
+ destination_data[destination_offset + 3] = 255;
+ }
+ }
+ Ok(())
+ }
+
+ fn previous_blend_rgba_into_packed(
+ destination: &mut ffmpeg::frame::Video,
+ layout: PackedRgbLayout,
+ source: &ffmpeg::frame::Video,
+ x: usize,
+ y: usize,
+ ) -> anyhow::Result<()> {
+ if source.format() != Pixel::RGBA {
+ return Err(anyhow!("camera overlay must be RGBA"));
+ }
+ let source_width = source.width() as usize;
+ let source_height = source.height() as usize;
+ if x.saturating_add(source_width) > destination.width() as usize
+ || y.saturating_add(source_height) > destination.height() as usize
+ {
+ return Err(anyhow!("camera overlay exceeds screen frame"));
+ }
+ let bytes_per_pixel = layout.bytes_per_pixel;
+ let source_stride = source.stride(0);
+ let destination_stride = destination.stride(0);
+ let source_data = source.data(0);
+ let destination_data = destination.data_mut(0);
+ if rgba_plane_is_opaque(source_data, source_stride, source_width, source_height)
+ && rgba_rows_fit(
+ destination_data.len(),
+ destination_stride,
+ x * bytes_per_pixel,
+ source_width * bytes_per_pixel,
+ y,
+ source_height,
+ )
+ {
+ copy_opaque_rgba_into_packed(
+ destination_data,
+ source_data,
+ PlaneWindow {
+ source_stride,
+ destination_stride,
+ source_x: 0,
+ source_y: 0,
+ destination_x: x,
+ destination_y: y,
+ width: source_width,
+ height: source_height,
+ },
+ layout,
+ );
+ return Ok(());
+ }
+ for row in 0..source_height {
+ let source_row = &source_data[row * source_stride..][..source_width * 4];
+ let destination_row = &mut destination_data
+ [(y + row) * destination_stride + x * bytes_per_pixel..]
+ [..source_width * bytes_per_pixel];
+ for (source_pixel, destination_pixel) in source_row
+ .chunks_exact(4)
+ .zip(destination_row.chunks_exact_mut(bytes_per_pixel))
+ {
+ let alpha = u16::from(source_pixel[3]);
+ let inverse_alpha = 255 - alpha;
+ for (channel, offset) in [layout.red, layout.green, layout.blue]
+ .into_iter()
+ .enumerate()
+ {
+ destination_pixel[offset] = ((u16::from(source_pixel[channel]) * alpha
+ + u16::from(destination_pixel[offset]) * inverse_alpha
+ + 127)
+ / 255) as u8;
+ }
+ if let Some(opaque) = layout.opaque {
+ destination_pixel[opaque] = 255;
+ }
+ }
+ }
+ Ok(())
+ }
+
+ fn span_pixel_counts(runs: &OverlayRuns) -> (u64, u64, u64) {
+ let mut opaque = 0_u64;
+ let mut transparent = 0_u64;
+ let mut edge = 0_u64;
+ for span in &runs.spans {
+ let len = u64::from(span.len);
+ match span.kind {
+ OverlaySpanKind::Opaque => opaque += len,
+ OverlaySpanKind::Transparent => transparent += len,
+ OverlaySpanKind::Edge => edge += len,
+ }
+ }
+ (opaque, transparent, edge)
+ }
+
+ struct BenchRect {
+ x: usize,
+ y: usize,
+ width: usize,
+ height: usize,
+ }
+
+ fn copy_overlay_rect(
+ destination: &mut ffmpeg::frame::Video,
+ template: &ffmpeg::frame::Video,
+ rect: &BenchRect,
+ ) {
+ let bytes_per_pixel = PackedRgbLayout::of(destination.format())
+ .expect("packed layout")
+ .bytes_per_pixel;
+ let row_bytes = rect.width * bytes_per_pixel;
+ let destination_stride = destination.stride(0);
+ let template_stride = template.stride(0);
+ let template_data = template.data(0);
+ let destination_data = destination.data_mut(0);
+ for row in 0..rect.height {
+ let source_offset = (rect.y + row) * template_stride + rect.x * bytes_per_pixel;
+ let destination_offset = (rect.y + row) * destination_stride + rect.x * bytes_per_pixel;
+ destination_data[destination_offset..destination_offset + row_bytes]
+ .copy_from_slice(&template_data[source_offset..source_offset + row_bytes]);
+ }
+ }
+
+ fn time_overlay(
+ template: &ffmpeg::frame::Video,
+ rect: &BenchRect,
+ warmup: usize,
+ runs: usize,
+ mut blend: impl FnMut(&mut ffmpeg::frame::Video),
+ ) -> (u128, u128, u128) {
+ let mut frame = copy_frame(template);
+ for _ in 0..warmup {
+ blend(&mut frame);
+ copy_overlay_rect(&mut frame, template, rect);
+ }
+ let mut samples = Vec::with_capacity(runs);
+ for _ in 0..runs {
+ copy_overlay_rect(&mut frame, template, rect);
+ let started = Instant::now();
+ blend(std::hint::black_box(&mut frame));
+ let _ = std::hint::black_box(frame.data(0)[rect.y * frame.stride(0) + rect.x]);
+ samples.push(started.elapsed().as_nanos());
+ }
+ let min = samples.iter().copied().min().unwrap_or(0);
+ let max = samples.iter().copied().max().unwrap_or(0);
+ (min, median_nanos(&mut samples), max)
+ }
+
+ fn print_overlay_time(label: &str, sample: (u128, u128, u128)) {
+ let millis = |nanos: u128| nanos as f64 / 1_000_000.0;
+ println!(
+ "{label} median_ms={:.4} min_ms={:.4} max_ms={:.4}",
+ millis(sample.1),
+ millis(sample.0),
+ millis(sample.2),
+ );
+ }
+
+ #[test]
+ #[ignore = "release timing benchmark; run with --release --ignored --nocapture"]
+ fn opaque_copy_benchmark() {
+ ffmpeg::init().expect("FFmpeg initializes");
+ const WARMUP: usize = 2;
+ const RUNS: usize = 11;
+ let screen_width = 1920_u32;
+ let screen_height = 1080_u32;
+ let (origin_x, origin_y, size) = overlay_rect(screen_width, screen_height);
+ assert_eq!(size, 324);
+ println!(
+ "opaque_copy_bench screen={screen_width}x{screen_height} camera={size}x{size} origin={origin_x},{origin_y} warmup={WARMUP} runs={RUNS} median"
+ );
+ let shapes = [
+ ("round", LinuxCameraShape::Round),
+ (
+ "square_r24",
+ LinuxCameraShape::RoundedRectangle { radius_pixels: 24 },
+ ),
+ (
+ "rounded_rectangle_r64",
+ LinuxCameraShape::RoundedRectangle { radius_pixels: 64 },
+ ),
+ ];
+ for (name, shape) in shapes {
+ let mut camera = ffmpeg::frame::Video::new(Pixel::RGBA, size, size);
+ paint_rgba(&mut camera, opaque_pixel);
+ let presentation = LinuxCameraPresentation {
+ rect: LinuxCameraRect {
+ x: origin_x as u32,
+ y: origin_y as u32,
+ width: size,
+ height: size,
+ },
+ shape,
+ mirrored: false,
+ effect: LinuxCameraEffect::None,
+ };
+ presentation
+ .validate(screen_width, screen_height)
+ .expect("presentation fits");
+ let mask = camera_mask(presentation);
+ assert!(!mask.is_empty(), "{name}");
+ let overlay = crop_camera(&camera, presentation, &mask).expect("shaped overlay");
+ let started = Instant::now();
+ let runs = overlay_runs(&overlay);
+ let runs_ms = started.elapsed().as_secs_f64() * 1000.0;
+ let (opaque, transparent, edge) = span_pixel_counts(&runs);
+ println!(
+ "presentation={name} radius={:.1} runs_build_ms={runs_ms:.4} opaque={opaque} transparent={transparent} edge={edge}",
+ presentation.radius()
+ );
+ for (format_name, format) in [("bgra", Pixel::BGRA), ("rgba", Pixel::RGBA)] {
+ let layout = PackedRgbLayout::of(format).expect("packed layout");
+ let template = pattern_frame(format, screen_width, screen_height, 9);
+ let mut original = copy_frame(&template);
+ let mut previous = copy_frame(&template);
+ let mut rework = copy_frame(&template);
+ if format == Pixel::RGBA {
+ formula_blend_rgba(&mut original, &overlay, origin_x, origin_y);
+ previous_blend_rgba(&mut previous, &overlay, origin_x, origin_y)
+ .expect("previous rgba");
+ blend_rgba_with_runs(&mut rework, &overlay, origin_x, origin_y, Some(&runs))
+ .expect("rework rgba");
+ } else {
+ formula_blend_packed(&mut original, layout, &overlay, origin_x, origin_y);
+ previous_blend_rgba_into_packed(
+ &mut previous,
+ layout,
+ &overlay,
+ origin_x,
+ origin_y,
+ )
+ .expect("previous bgra");
+ blend_rgba_into_packed_with_runs(
+ &mut rework,
+ layout,
+ &overlay,
+ origin_x,
+ origin_y,
+ Some(&runs),
+ )
+ .expect("rework bgra");
+ }
+ assert_planes_equal(&previous, &original);
+ assert_planes_equal(&rework, &original);
+ let rect = BenchRect {
+ x: origin_x,
+ y: origin_y,
+ width: size as usize,
+ height: size as usize,
+ };
+ let original_time = time_overlay(&template, &rect, WARMUP, RUNS, |frame| {
+ if format == Pixel::RGBA {
+ formula_blend_rgba(
+ frame,
+ std::hint::black_box(&overlay),
+ origin_x,
+ origin_y,
+ );
+ } else {
+ formula_blend_packed(
+ frame,
+ layout,
+ std::hint::black_box(&overlay),
+ origin_x,
+ origin_y,
+ );
+ }
+ });
+ let previous_time = time_overlay(&template, &rect, WARMUP, RUNS, |frame| {
+ if format == Pixel::RGBA {
+ previous_blend_rgba(
+ frame,
+ std::hint::black_box(&overlay),
+ origin_x,
+ origin_y,
+ )
+ .expect("timed previous rgba");
+ } else {
+ previous_blend_rgba_into_packed(
+ frame,
+ layout,
+ std::hint::black_box(&overlay),
+ origin_x,
+ origin_y,
+ )
+ .expect("timed previous bgra");
+ }
+ });
+ let rework_time = time_overlay(&template, &rect, WARMUP, RUNS, |frame| {
+ if format == Pixel::RGBA {
+ blend_rgba_with_runs(
+ frame,
+ std::hint::black_box(&overlay),
+ origin_x,
+ origin_y,
+ Some(std::hint::black_box(&runs)),
+ )
+ .expect("timed rework rgba");
+ } else {
+ blend_rgba_into_packed_with_runs(
+ frame,
+ layout,
+ std::hint::black_box(&overlay),
+ origin_x,
+ origin_y,
+ Some(std::hint::black_box(&runs)),
+ )
+ .expect("timed rework bgra");
+ }
+ });
+ let prefix = format!("presentation={name} format={format_name} algo");
+ print_overlay_time(&format!("{prefix}=original"), original_time);
+ print_overlay_time(&format!("{prefix}=previous"), previous_time);
+ print_overlay_time(&format!("{prefix}=rework"), rework_time);
+ }
+ }
+ }
}
diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rs
index 9045431f6..c7fb51597 100644
--- a/crates/recording/src/output_pipeline/core.rs
+++ b/crates/recording/src/output_pipeline/core.rs
@@ -426,6 +426,7 @@ pub enum PipelineHealthEvent {
source: String,
starvation_ms: u64,
},
+ MicrophoneDigitalSilence,
RecoveryFragmentCorrupt {
path: String,
reason: String,
@@ -496,6 +497,9 @@ pub fn emit_health(tx: &HealthSender, event: PipelineHealthEvent) {
PipelineHealthEvent::CaptureTargetLost { .. } => {
health_event(13, "capture_target_lost", &[])
}
+ PipelineHealthEvent::MicrophoneDigitalSilence => {
+ health_event(14, "microphone_digital_silence", &[])
+ }
}
let _ = tx.try_send(event);
}
@@ -2273,6 +2277,7 @@ pub struct SetupCtx {
master_clock: Arc<MasterClock>,
stop_token: CancellationToken,
stop_signal: PipelineStopSignal,
+ digital_silence_ms: Arc<AtomicU64>,
}
impl SetupCtx {
@@ -2288,9 +2293,14 @@ impl SetupCtx {
master_clock,
stop_token,
stop_signal,
+ digital_silence_ms: Arc::new(AtomicU64::new(0)),
}
}
+ pub fn digital_silence_ms(&self) -> Arc<AtomicU64> {
+ self.digital_silence_ms.clone()
+ }
+
pub fn tasks(&mut self) -> &mut TaskPool {
&mut self.tasks
}
@@ -3005,6 +3015,7 @@ impl<TVideo: VideoSource> OutputPipelineBuilder<HasVideo<TVideo>> {
);
let audio_gap_summary = Arc::new(OnceLock::new());
+ let digital_silence_ms = setup_ctx.digital_silence_ms();
finish_build(
setup_ctx,
@@ -3047,6 +3058,7 @@ impl<TVideo: VideoSource> OutputPipelineBuilder<HasVideo<TVideo>> {
video_timestamp_span,
health_rx: Some(build_ctx.health_rx),
audio_gap_summary,
+ digital_silence_ms,
})
}
}
@@ -3104,6 +3116,7 @@ impl OutputPipelineBuilder<NoVideo> {
let shared_pause = SharedWallClockPause::new(build_ctx.pause_flag.clone());
let audio_gap_summary = Arc::new(OnceLock::new());
+ let digital_silence_ms = setup_ctx.digital_silence_ms();
finish_build(
setup_ctx,
@@ -3139,6 +3152,7 @@ impl OutputPipelineBuilder<NoVideo> {
video_timestamp_span: Arc::new(VideoTimestampSpan::default()),
health_rx: Some(build_ctx.health_rx),
audio_gap_summary,
+ digital_silence_ms,
})
}
}
@@ -4931,6 +4945,7 @@ pub struct OutputPipeline {
video_timestamp_span: Arc<VideoTimestampSpan>,
health_rx: Option<HealthReceiver>,
audio_gap_summary: Arc<OnceLock<AudioGapSummary>>,
+ digital_silence_ms: Arc<AtomicU64>,
}
pub struct FinishedOutputPipeline {
@@ -4942,6 +4957,7 @@ pub struct FinishedOutputPipeline {
/// media span for VFR content.
pub video_timestamp_span: Option<(Duration, Duration)>,
pub audio_gap_summary: Option<AudioGapSummary>,
+ pub digital_silence_ms: Option<u32>,
}
pub(crate) struct OutputPipelineStopOutcome {
@@ -5075,6 +5091,10 @@ impl OutputPipeline {
video_frame_count: self.video_frame_count.load(Ordering::Acquire),
video_timestamp_span: self.video_timestamp_span.get(),
audio_gap_summary: self.audio_gap_summary.get().copied(),
+ digital_silence_ms: match self.digital_silence_ms.load(Ordering::Relaxed) {
+ 0 => None,
+ ms => Some(u32::try_from(ms).unwrap_or(u32::MAX)),
+ },
},
media_error,
})
diff --git a/crates/recording/src/output_pipeline/ffmpeg.rs b/crates/recording/src/output_pipeline/ffmpeg.rs
index 013528c27..bc96016fa 100644
--- a/crates/recording/src/output_pipeline/ffmpeg.rs
+++ b/crates/recording/src/output_pipeline/ffmpeg.rs
@@ -141,11 +141,24 @@ impl AudioMuxer for Mp4Muxer {
pub struct OggMuxer(OggFile);
+/// `write_behind` moves file writes off the caller (an async runtime worker)
+/// onto a dedicated writer thread. Only macOS recordings opt in.
+#[derive(Default)]
+pub struct OggMuxerConfig {
+ pub write_behind: bool,
+}
+
+fn warn_inline_fallback(result: std::io::Result<()>) {
+ if let Err(error) = result {
+ warn!(%error, "Output write-behind unavailable, muxing inline");
+ }
+}
+
impl Muxer for OggMuxer {
- type Config = ();
+ type Config = OggMuxerConfig;
async fn setup(
- _: Self::Config,
+ config: Self::Config,
output_path: PathBuf,
_: Option<VideoInfo>,
audio_config: Option<AudioInfo>,
@@ -158,10 +171,13 @@ impl Muxer for OggMuxer {
let audio_config =
audio_config.ok_or_else(|| anyhow!("No audio configuration provided"))?;
- Ok(Self(
- OggFile::init(output_path, |o| OpusEncoder::init(audio_config, o))
- .map_err(|e| anyhow!("Failed to initialize Opus encoder: {e}"))?,
- ))
+ let mut file = OggFile::init(output_path, |o| OpusEncoder::init(audio_config, o))
+ .map_err(|e| anyhow!("Failed to initialize Opus encoder: {e}"))?;
+ if config.write_behind {
+ warn_inline_fallback(file.enable_write_behind());
+ }
+
+ Ok(Self(file))
}
fn finish(&mut self, _: Duration) -> anyhow::Result<anyhow::Result<()>> {
@@ -186,6 +202,8 @@ pub struct FragmentedAudioMuxer {
#[derive(Default)]
pub struct FragmentedAudioMuxerConfig {
pub shared_pause_state: Option<SharedPauseState>,
+ /// See [`OggMuxerConfig::write_behind`].
+ pub write_behind: bool,
}
impl Muxer for FragmentedAudioMuxer {
@@ -205,9 +223,14 @@ impl Muxer for FragmentedAudioMuxer {
let audio_config =
audio_config.ok_or_else(|| anyhow!("No audio configuration provided"))?;
+ let mut encoder = FragmentedAudioFile::init(output_path, audio_config)
+ .map_err(|e| anyhow!("Failed to initialize fragmented audio encoder: {e}"))?;
+ if config.write_behind {
+ warn_inline_fallback(encoder.enable_write_behind());
+ }
+
Ok(Self {
- encoder: FragmentedAudioFile::init(output_path, audio_config)
- .map_err(|e| anyhow!("Failed to initialize fragmented audio encoder: {e}"))?,
+ encoder,
pause: config.shared_pause_state,
})
}
@@ -825,6 +848,8 @@ pub struct DashSegmentedAudioMuxerConfig {
pub segment_duration: Duration,
pub shared_pause_state: Option<SharedPauseState>,
pub segment_tx: Option<std::sync::mpsc::Sender<SegmentCompletedEvent>>,
+ /// See [`OggMuxerConfig::write_behind`].
+ pub write_behind: bool,
}
impl Default for DashSegmentedAudioMuxerConfig {
@@ -833,6 +858,7 @@ impl Default for DashSegmentedAudioMuxerConfig {
segment_duration: Duration::from_secs(3),
shared_pause_state: None,
segment_tx: None,
+ write_behind: false,
}
}
}
@@ -862,6 +888,9 @@ impl Muxer for DashSegmentedAudioMuxer {
},
)
.map_err(|e| anyhow!("Failed to initialize DASH audio segment encoder: {e}"))?;
+ if config.write_behind {
+ warn_inline_fallback(encoder.enable_write_behind());
+ }
if let Some(tx) = config.segment_tx {
encoder.set_segment_callback(tx);
@@ -907,6 +936,76 @@ mod tests {
use cap_timestamp::Timestamp;
use std::time::Instant;
+ mod write_behind_opt_in {
+ use super::super::{
+ DashSegmentedAudioMuxer, DashSegmentedAudioMuxerConfig, FragmentedAudioMuxer,
+ FragmentedAudioMuxerConfig, OggMuxer, OggMuxerConfig,
+ };
+ use crate::{Muxer, TaskPool};
+ use cap_media_info::AudioInfo;
+ use std::sync::{Arc, atomic::AtomicBool};
+
+ fn audio_info() -> AudioInfo {
+ AudioInfo::new_raw(
+ ffmpeg::format::Sample::F32(ffmpeg::format::sample::Type::Packed),
+ 48_000,
+ 1,
+ )
+ }
+
+ async fn setup<M: Muxer>(config: M::Config, path: std::path::PathBuf) -> M {
+ M::setup(
+ config,
+ path,
+ None,
+ Some(audio_info()),
+ Arc::new(AtomicBool::new(false)),
+ &mut TaskPool::default(),
+ )
+ .await
+ .unwrap()
+ }
+
+ #[test]
+ fn audio_muxers_write_inline_unless_the_call_site_opts_in() {
+ assert!(!FragmentedAudioMuxerConfig::default().write_behind);
+ assert!(!DashSegmentedAudioMuxerConfig::default().write_behind);
+ assert!(!OggMuxerConfig::default().write_behind);
+ }
+
+ #[tokio::test]
+ async fn audio_muxers_honour_the_write_behind_flag() {
+ ffmpeg::init().unwrap();
+ let directory = tempfile::tempdir().unwrap();
+ for write_behind in [false, true] {
+ let dir = directory.path().join(write_behind.to_string());
+ let fragmented = setup::<FragmentedAudioMuxer>(
+ FragmentedAudioMuxerConfig {
+ shared_pause_state: None,
+ write_behind,
+ },
+ dir.join("audio.m4a"),
+ )
+ .await;
+ assert_eq!(fragmented.encoder.is_write_behind(), write_behind);
+
+ let dash = setup::<DashSegmentedAudioMuxer>(
+ DashSegmentedAudioMuxerConfig {
+ write_behind,
+ ..Default::default()
+ },
+ dir.join("audio"),
+ )
+ .await;
+ assert_eq!(dash.encoder.is_write_behind(), write_behind);
+
+ let ogg =
+ setup::<OggMuxer>(OggMuxerConfig { write_behind }, dir.join("audio.ogg")).await;
+ assert_eq!(ogg.0.is_write_behind(), write_behind);
+ }
+ }
+ }
+
#[test]
fn segmented_encoder_preserves_frames_when_a_full_queue_recovers() {
let (sender, receiver) = flume::bounded(1);
diff --git a/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs b/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs
index d2c6f7cec..cd36e3bff 100644
--- a/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs
+++ b/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs
@@ -299,7 +299,7 @@ impl MacOSFragmentedM4SMuxer {
prefer_videotoolbox_hw_input: true,
};
- let mut encoder = SegmentedVideoEncoder::init_with_timed_fragments(
+ let mut encoder = SegmentedVideoEncoder::init_write_behind(
self.base_path.clone(),
self.video_config,
encoder_config,
@@ -690,7 +690,7 @@ impl MacOSFragmentedM4SCameraMuxer {
prefer_videotoolbox_hw_input: false,
};
- let mut encoder = SegmentedVideoEncoder::init_with_timed_fragments(
+ let mut encoder = SegmentedVideoEncoder::init_write_behind(
self.base_path.clone(),
self.video_config,
encoder_config,
diff --git a/crates/recording/src/output_pipeline/start_gate.rs b/crates/recording/src/output_pipeline/start_gate.rs
index 428407d3a..595f6e599 100644
--- a/crates/recording/src/output_pipeline/start_gate.rs
+++ b/crates/recording/src/output_pipeline/start_gate.rs
@@ -50,7 +50,7 @@ impl RecordingStartGate {
/// timeline starts where audio anchored at the recording epoch does.
/// Instant merges its tracks by timestamp; Studio aligns them with each
/// track's recorded start time instead and leaves this off.
- pub(crate) fn set_video_origin_at_arm(&self) {
+ pub fn set_video_origin_at_arm(&self) {
self.0.video_origin_at_arm.store(true, Ordering::Release);
}
diff --git a/crates/recording/src/recovery.rs b/crates/recording/src/recovery.rs
index f85d1070d..a2efd2da3 100644
--- a/crates/recording/src/recovery.rs
+++ b/crates/recording/src/recovery.rs
@@ -2637,6 +2637,9 @@ impl RecoveryManager {
.and_then(|s| s.mic.as_ref())
.and_then(|m| m.device_id.clone()),
gap_summary: None,
+ digital_silence_ms: original_segment
+ .and_then(|s| s.mic.as_ref())
+ .and_then(|m| m.digital_silence_ms),
})
} else {
None
@@ -2663,6 +2666,7 @@ impl RecoveryManager {
.and_then(|s| s.system_audio.as_ref())
.and_then(|a| a.device_id.clone()),
gap_summary: None,
+ digital_silence_ms: None,
})
} else {
None
@@ -5234,12 +5238,14 @@ mod clean_studio_snapshot_tests {
start_time: Some(-0.125),
device_id: Some("benchmark microphone".into()),
gap_summary: None,
+ digital_silence_ms: Some(190_000),
});
inner.segments[0].system_audio = Some(AudioMeta {
path: "content/segments/segment-0/system_audio.m4a".into(),
start_time: Some(0.25),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
meta.save_for_project().unwrap();
@@ -5256,6 +5262,7 @@ mod clean_studio_snapshot_tests {
);
assert_eq!(mic.start_time, Some(-0.125));
assert_eq!(mic.device_id.as_deref(), Some("benchmark microphone"));
+ assert_eq!(mic.digital_silence_ms, Some(190_000));
assert_eq!(
segment.system_audio.as_ref().unwrap().start_time,
Some(0.25)
@@ -5302,6 +5309,7 @@ mod clean_studio_snapshot_tests {
start_time: Some(-0.125),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
meta.save_for_project().unwrap();
let recording = RecoveryManager::analyze_incomplete(project, &meta).unwrap();
diff --git a/crates/recording/src/recovery/preparing_projection.rs b/crates/recording/src/recovery/preparing_projection.rs
index 38a5bed98..37ce3e5a1 100644
--- a/crates/recording/src/recovery/preparing_projection.rs
+++ b/crates/recording/src/recovery/preparing_projection.rs
@@ -990,12 +990,14 @@ mod tests {
overlap_dropped_frames: 2,
startup_overlap_drops: 1,
}),
+ digital_silence_ms: None,
});
metadata.segments[0].system_audio = Some(AudioMeta {
path: RelativePathBuf::from(format!("{base}/system_audio.ogg")),
start_time: Some(0.375),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
metadata.segments[0].cursor =
Some(RelativePathBuf::from(format!("{base}/cursor.json")));
diff --git a/crates/recording/src/screenshot.rs b/crates/recording/src/screenshot.rs
index 12381d209..537c4fdb7 100644
--- a/crates/recording/src/screenshot.rs
+++ b/crates/recording/src/screenshot.rs
@@ -53,15 +53,15 @@ fn unsupported_error() -> anyhow::Error {
anyhow!(WINDOWS_CAPTURE_UNSUPPORTED)
}
-#[cfg(not(target_os = "linux"))]
-#[derive(Clone, Copy)]
+#[cfg(any(target_os = "macos", target_os = "windows", test))]
+#[derive(Clone, Copy, Debug)]
enum ChannelOrder {
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
Rgba,
Bgra,
}
-#[cfg(not(target_os = "linux"))]
+#[cfg(any(target_os = "macos", target_os = "windows", test))]
fn rgb_from_rgba(
data: &[u8],
width: usize,
@@ -83,28 +83,105 @@ fn rgb_from_rgba(
let rgb_len = height.checked_mul(width_stride)?;
let mut rgb = vec![0u8; rgb_len];
+ #[cfg(target_arch = "x86_64")]
+ {
+ if std::is_x86_feature_detected!("ssse3") {
+ for y in 0..height {
+ let (src_row, dst_row) =
+ rgb_row_pair(data, &mut rgb, y, bytes_per_row, row_bytes, width_stride)?;
+ unsafe {
+ reorder_rgb_row_ssse3(src_row, dst_row, order);
+ }
+ }
+ return RgbImage::from_raw(width as u32, height as u32, rgb);
+ }
+ }
+
for y in 0..height {
- let src_start = y.checked_mul(bytes_per_row)?;
- let src_end = src_start.checked_add(row_bytes)?;
- let dst_start = y.checked_mul(width_stride)?;
- let dst_end = dst_start.checked_add(width_stride)?;
+ let (src_row, dst_row) =
+ rgb_row_pair(data, &mut rgb, y, bytes_per_row, row_bytes, width_stride)?;
+ reorder_rgb_row_scalar(src_row, dst_row, order);
+ }
- let src_row = data.get(src_start..src_end)?;
- let dst_row = rgb.get_mut(dst_start..dst_end)?;
+ RgbImage::from_raw(width as u32, height as u32, rgb)
+}
- for (src, dst) in src_row.chunks_exact(4).zip(dst_row.chunks_exact_mut(3)) {
- let (r, b) = match order {
- ChannelOrder::Rgba => (src[0], src[2]),
- ChannelOrder::Bgra => (src[2], src[0]),
- };
+#[cfg(any(target_os = "macos", target_os = "windows", test))]
+fn rgb_row_pair<'a>(
+ data: &'a [u8],
+ rgb: &'a mut [u8],
+ y: usize,
+ bytes_per_row: usize,
+ row_bytes: usize,
+ width_stride: usize,
+) -> Option<(&'a [u8], &'a mut [u8])> {
+ let src_start = y.checked_mul(bytes_per_row)?;
+ let src_end = src_start.checked_add(row_bytes)?;
+ let dst_start = y.checked_mul(width_stride)?;
+ let dst_end = dst_start.checked_add(width_stride)?;
+ let src_row = data.get(src_start..src_end)?;
+ let dst_row = rgb.get_mut(dst_start..dst_end)?;
+ Some((src_row, dst_row))
+}
- dst[0] = r;
- dst[1] = src[1];
- dst[2] = b;
- }
+#[cfg(any(target_os = "macos", target_os = "windows", test))]
+fn reorder_rgb_row_scalar(src: &[u8], dst: &mut [u8], order: ChannelOrder) {
+ match order {
+ ChannelOrder::Rgba => rgba_to_rgb_scalar(src, dst),
+ ChannelOrder::Bgra => bgra_to_rgb_scalar(src, dst),
}
+}
- RgbImage::from_raw(width as u32, height as u32, rgb)
+#[cfg(any(target_os = "macos", target_os = "windows", test))]
+fn rgba_to_rgb_scalar(src: &[u8], dst: &mut [u8]) {
+ for (src, dst) in src.chunks_exact(4).zip(dst.chunks_exact_mut(3)) {
+ dst[0] = src[0];
+ dst[1] = src[1];
+ dst[2] = src[2];
+ }
+}
+
+#[cfg(any(target_os = "macos", target_os = "windows", test))]
+fn bgra_to_rgb_scalar(src: &[u8], dst: &mut [u8]) {
+ for (src, dst) in src.chunks_exact(4).zip(dst.chunks_exact_mut(3)) {
+ dst[0] = src[2];
+ dst[1] = src[1];
+ dst[2] = src[0];
+ }
+}
+
+#[cfg(all(
+ any(target_os = "macos", target_os = "windows", test),
+ target_arch = "x86_64"
+))]
+#[target_feature(enable = "ssse3")]
+unsafe fn reorder_rgb_row_ssse3(src: &[u8], dst: &mut [u8], order: ChannelOrder) {
+ use std::arch::x86_64::*;
+
+ unsafe {
+ let mask = match order {
+ ChannelOrder::Rgba => {
+ _mm_setr_epi8(0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, -1, -1, -1, -1)
+ }
+ ChannelOrder::Bgra => {
+ _mm_setr_epi8(2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1)
+ }
+ };
+
+ let pixels = (src.len() / 4).min(dst.len() / 3);
+ let simd_pixels = pixels & !3;
+ for i in (0..simd_pixels).step_by(4) {
+ let v = _mm_loadu_si128(src.as_ptr().add(i * 4) as *const __m128i);
+ let shuffled = _mm_shuffle_epi8(v, mask);
+ let dst_ptr = dst.as_mut_ptr().add(i * 3);
+ let lo = _mm_cvtsi128_si64(shuffled);
+ let hi = _mm_cvtsi128_si32(_mm_srli_si128(shuffled, 8));
+ std::ptr::write_unaligned(dst_ptr.cast::<i64>(), lo);
+ std::ptr::write_unaligned(dst_ptr.add(8).cast::<i32>(), hi);
+ }
+
+ reorder_rgb_row_scalar(&src[simd_pixels * 4..], &mut dst[simd_pixels * 3..], order);
+ }
}
#[cfg(not(target_os = "linux"))]
@@ -155,31 +232,113 @@ fn rgba_from_raw(
}
fn apply_window_rounded_corners(image: RgbImage, target: &ScreenCaptureTarget) -> RgbaImage {
+ round_rgb_corners(image, window_corner_radius_px(target))
+}
+
+fn round_rgb_corners(image: RgbImage, corner_radius: f32) -> RgbaImage {
let width = image.width();
let height = image.height();
- let corner_radius = window_corner_radius_px(target);
-
if corner_radius <= 0.0 {
return image::buffer::ConvertBuffer::convert(&image);
}
- let fw = width as f32;
- let fh = height as f32;
+ let geom = CornerGeom {
+ fw: width as f32,
+ fh: height as f32,
+ radius: corner_radius,
+ };
+ let corner_x = corner_band_px(corner_radius, width) as usize;
+ let corner_y = corner_band_px(corner_radius, height) as usize;
+ let width_us = width as usize;
+ let height_us = height as usize;
+ let src = image.into_raw();
let mut rgba = RgbaImage::new(width, height);
+ let dst = rgba.as_mut();
+ let src_stride = width_us * 3;
+ let dst_stride = width_us * 4;
+ let right = width_us.saturating_sub(corner_x);
+ let x_covers_row = corner_x >= right;
+
+ for y in 0..height_us {
+ let src_off = y * src_stride;
+ let dst_off = y * dst_stride;
+ let src_row = &src[src_off..src_off + src_stride];
+ let dst_row = &mut dst[dst_off..dst_off + dst_stride];
+ let y_in_band = y < corner_y || y.saturating_add(corner_y) >= height_us;
+
+ if !y_in_band || corner_x == 0 {
+ copy_rgb_opaque(src_row, dst_row);
+ continue;
+ }
+
+ if x_covers_row {
+ fill_corner_span(src_row, dst_row, 0, width_us, y, geom);
+ continue;
+ }
- for (x, y, rgb_pixel) in image.enumerate_pixels() {
- let alpha = rounded_corner_alpha(x as f32 + 0.5, y as f32 + 0.5, fw, fh, corner_radius);
- rgba.put_pixel(
- x,
- y,
- image::Rgba([rgb_pixel[0], rgb_pixel[1], rgb_pixel[2], alpha]),
+ fill_corner_span(src_row, dst_row, 0, corner_x, y, geom);
+ copy_rgb_opaque(
+ &src_row[corner_x * 3..right * 3],
+ &mut dst_row[corner_x * 4..right * 4],
);
+ fill_corner_span(src_row, dst_row, right, width_us, y, geom);
}
rgba
}
+#[derive(Clone, Copy)]
+struct CornerGeom {
+ fw: f32,
+ fh: f32,
+ radius: f32,
+}
+
+fn corner_band_px(radius: f32, extent: u32) -> u32 {
+ if extent == 0 {
+ return 0;
+ }
+
+ let rounded = radius.ceil();
+ if !rounded.is_finite() || rounded >= extent as f32 {
+ extent
+ } else if rounded <= 0.0 {
+ 0
+ } else {
+ rounded as u32
+ }
+}
+
+fn copy_rgb_opaque(src: &[u8], dst: &mut [u8]) {
+ for (src_px, dst_px) in src.chunks_exact(3).zip(dst.chunks_exact_mut(4)) {
+ dst_px[0] = src_px[0];
+ dst_px[1] = src_px[1];
+ dst_px[2] = src_px[2];
+ dst_px[3] = 255;
+ }
+}
+
+fn fill_corner_span(
+ src_row: &[u8],
+ dst_row: &mut [u8],
+ x_start: usize,
+ x_end: usize,
+ y: usize,
+ geom: CornerGeom,
+) {
+ let y_center = y as f32 + 0.5;
+ for x in x_start..x_end {
+ let src_i = x * 3;
+ let dst_i = x * 4;
+ let alpha = rounded_corner_alpha(x as f32 + 0.5, y_center, geom.fw, geom.fh, geom.radius);
+ dst_row[dst_i] = src_row[src_i];
+ dst_row[dst_i + 1] = src_row[src_i + 1];
+ dst_row[dst_i + 2] = src_row[src_i + 2];
+ dst_row[dst_i + 3] = alpha;
+ }
+}
+
fn window_corner_radius_px(target: &ScreenCaptureTarget) -> f32 {
match target {
ScreenCaptureTarget::Window { id } => {
@@ -1673,3 +1832,330 @@ mod wayland_screenshot_tests {
assert!(wayland_screenshot_display_id(&ScreenCaptureTarget::CameraOnly).is_err());
}
}
+
+#[cfg(test)]
+mod screenshot_pixel_tests {
+ use super::*;
+
+ fn paint(len: usize, seed: u32) -> Vec<u8> {
+ let mut state = seed;
+ let mut out = vec![0u8; len];
+ for byte in &mut out {
+ state = state.wrapping_mul(1664525).wrapping_add(1013904223);
+ *byte = (state >> 16) as u8;
+ }
+ out
+ }
+
+ fn assert_bytes_eq(got: &[u8], expected: &[u8], label: &str) {
+ assert_eq!(got.len(), expected.len(), "{label} length");
+ let mismatch = got
+ .iter()
+ .zip(expected.iter())
+ .position(|(got_byte, expected_byte)| got_byte != expected_byte);
+ if let Some(index) = mismatch {
+ panic!(
+ "{label} mismatch at {index}: got {:02x} expected {:02x}",
+ got[index], expected[index]
+ );
+ }
+ }
+
+ fn reference_rgb(
+ data: &[u8],
+ width: usize,
+ height: usize,
+ bytes_per_row: usize,
+ order: ChannelOrder,
+ ) -> Option<Vec<u8>> {
+ let row_bytes = width.checked_mul(4)?;
+ if bytes_per_row < row_bytes {
+ return None;
+ }
+
+ let required_len = height.checked_mul(bytes_per_row)?;
+ if data.len() < required_len {
+ return None;
+ }
+
+ let width_stride = width.checked_mul(3)?;
+ let rgb_len = height.checked_mul(width_stride)?;
+ let mut rgb = vec![0u8; rgb_len];
+
+ for y in 0..height {
+ let src_start = y.checked_mul(bytes_per_row)?;
+ let src_end = src_start.checked_add(row_bytes)?;
+ let dst_start = y.checked_mul(width_stride)?;
+ let dst_end = dst_start.checked_add(width_stride)?;
+ let src_row = data.get(src_start..src_end)?;
+ let dst_row = rgb.get_mut(dst_start..dst_end)?;
+
+ for (src, dst) in src_row.chunks_exact(4).zip(dst_row.chunks_exact_mut(3)) {
+ let (r, b) = match order {
+ ChannelOrder::Rgba => (src[0], src[2]),
+ ChannelOrder::Bgra => (src[2], src[0]),
+ };
+ dst[0] = r;
+ dst[1] = src[1];
+ dst[2] = b;
+ }
+ }
+
+ Some(rgb)
+ }
+
+ fn scalar_rgb(
+ data: &[u8],
+ width: usize,
+ height: usize,
+ bytes_per_row: usize,
+ order: ChannelOrder,
+ ) -> Option<Vec<u8>> {
+ let row_bytes = width.checked_mul(4)?;
+ if bytes_per_row < row_bytes {
+ return None;
+ }
+ let required_len = height.checked_mul(bytes_per_row)?;
+ if data.len() < required_len {
+ return None;
+ }
+ let width_stride = width.checked_mul(3)?;
+ let rgb_len = height.checked_mul(width_stride)?;
+ let mut rgb = vec![0u8; rgb_len];
+ for y in 0..height {
+ let (src_row, dst_row) =
+ rgb_row_pair(data, &mut rgb, y, bytes_per_row, row_bytes, width_stride)?;
+ reorder_rgb_row_scalar(src_row, dst_row, order);
+ }
+ Some(rgb)
+ }
+
+ fn rgb_cases() -> Vec<(usize, usize, usize)> {
+ let mut cases = Vec::new();
+ let widths = [
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 31, 63, 64, 65, 191, 1920, 1921,
+ ];
+ let heights = [0, 1, 2, 3, 5, 7];
+ let pads = [0usize, 1, 7, 32];
+ for width in widths {
+ for height in heights {
+ for pad in pads {
+ cases.push((width, height, pad));
+ }
+ }
+ }
+ cases.push((1920, 1080, 0));
+ cases.push((1920, 1080, 64));
+ cases.push((1919, 17, 3));
+ cases.push((1, 1080, 0));
+ cases
+ }
+
+ fn assert_rgb_path_matches_original(
+ path: &str,
+ packed: Option<Vec<u8>>,
+ expected: &Option<Vec<u8>>,
+ label: &str,
+ ) {
+ match (packed.as_deref(), expected.as_deref()) {
+ (Some(got), Some(expected)) => {
+ assert_bytes_eq(got, expected, &format!("{path} {label}"));
+ }
+ (None, None) => {}
+ _ => panic!(
+ "{path} {label} presence got {} expected {}",
+ packed.is_some(),
+ expected.is_some()
+ ),
+ }
+ }
+
+ #[test]
+ fn rgb_dispatch_matches_original_for_both_channel_orders() {
+ for (width, height, pad) in rgb_cases() {
+ let stride = width * 4 + pad;
+ let data = paint(
+ height * stride + 8,
+ (width as u32).wrapping_mul(19).wrapping_add(height as u32),
+ );
+ for order in [ChannelOrder::Rgba, ChannelOrder::Bgra] {
+ let label = format!("{width}x{height} stride {stride} {order:?}");
+ let expected = reference_rgb(&data, width, height, stride, order);
+ let got = rgb_from_rgba(&data, width, height, stride, order)
+ .map(|image| image.into_raw());
+ assert_rgb_path_matches_original("dispatch", got, &expected, &label);
+ }
+ }
+ }
+
+ #[test]
+ fn rgb_scalar_fallback_matches_original_for_both_channel_orders() {
+ for (width, height, pad) in rgb_cases() {
+ let stride = width * 4 + pad;
+ let data = paint(
+ height * stride + 8,
+ (width as u32).wrapping_mul(23).wrapping_add(pad as u32),
+ );
+ for order in [ChannelOrder::Rgba, ChannelOrder::Bgra] {
+ let label = format!("{width}x{height} stride {stride} {order:?}");
+ let expected = reference_rgb(&data, width, height, stride, order);
+ let got = scalar_rgb(&data, width, height, stride, order);
+ assert_rgb_path_matches_original("scalar", got, &expected, &label);
+ }
+ }
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ fn ssse3_rgb(
+ data: &[u8],
+ width: usize,
+ height: usize,
+ bytes_per_row: usize,
+ order: ChannelOrder,
+ ) -> Option<Vec<u8>> {
+ let row_bytes = width.checked_mul(4)?;
+ if bytes_per_row < row_bytes {
+ return None;
+ }
+ let required_len = height.checked_mul(bytes_per_row)?;
+ if data.len() < required_len {
+ return None;
+ }
+ let width_stride = width.checked_mul(3)?;
+ let rgb_len = height.checked_mul(width_stride)?;
+ let mut rgb = vec![0u8; rgb_len];
+ for y in 0..height {
+ let (src_row, dst_row) =
+ rgb_row_pair(data, &mut rgb, y, bytes_per_row, row_bytes, width_stride)?;
+ unsafe {
+ reorder_rgb_row_ssse3(src_row, dst_row, order);
+ }
+ }
+ Some(rgb)
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn rgb_ssse3_matches_original_including_scalar_tail() {
+ if !std::is_x86_feature_detected!("ssse3") {
+ eprintln!("ssse3 unavailable, skipping shuffled rgb path");
+ return;
+ }
+ for (width, height, pad) in rgb_cases() {
+ let stride = width * 4 + pad;
+ let data = paint(
+ height * stride + 8,
+ (width as u32).wrapping_mul(29).wrapping_add(height as u32),
+ );
+ for order in [ChannelOrder::Rgba, ChannelOrder::Bgra] {
+ let label = format!("{width}x{height} stride {stride} {order:?}");
+ let expected = reference_rgb(&data, width, height, stride, order);
+ let got = ssse3_rgb(&data, width, height, stride, order);
+ assert_rgb_path_matches_original("ssse3", got, &expected, &label);
+ }
+ }
+ }
+
+ #[test]
+ fn rgb_packing_rejects_undersized_input() {
+ assert!(rgb_from_rgba(&[0, 1, 2, 3], 2, 1, 4, ChannelOrder::Rgba).is_none());
+ assert!(rgb_from_rgba(&[0, 1, 2, 3], 1, 2, 4, ChannelOrder::Bgra).is_none());
+ assert!(rgb_from_rgba(&[0, 1, 2, 3, 4, 5, 6], 1, 1, 8, ChannelOrder::Rgba).is_none());
+ let image = rgb_from_rgba(&[10, 20, 30, 40, 1, 2, 3, 4], 1, 1, 8, ChannelOrder::Bgra)
+ .expect("padded row");
+ assert_eq!(image.as_raw(), &[30, 20, 10]);
+ }
+
+ fn rgb_image(width: u32, height: u32, seed: u32) -> RgbImage {
+ let buf = paint((width as usize) * (height as usize) * 3, seed);
+ RgbImage::from_raw(width, height, buf).expect("rgb image")
+ }
+
+ fn reference_round(image: &RgbImage, radius: f32) -> RgbaImage {
+ if radius <= 0.0 {
+ return image::buffer::ConvertBuffer::convert(image);
+ }
+
+ let width = image.width();
+ let height = image.height();
+ let fw = width as f32;
+ let fh = height as f32;
+ let mut rgba = RgbaImage::new(width, height);
+ for (x, y, rgb_pixel) in image.enumerate_pixels() {
+ let alpha = rounded_corner_alpha(x as f32 + 0.5, y as f32 + 0.5, fw, fh, radius);
+ rgba.put_pixel(
+ x,
+ y,
+ image::Rgba([rgb_pixel[0], rgb_pixel[1], rgb_pixel[2], alpha]),
+ );
+ }
+ rgba
+ }
+
+ fn assert_corners(width: u32, height: u32, radius: f32) {
+ let image = rgb_image(width, height, width.wrapping_mul(17).wrapping_add(height));
+ let expected = reference_round(&image, radius);
+ let got = round_rgb_corners(image, radius);
+ assert_bytes_eq(
+ got.as_raw(),
+ expected.as_raw(),
+ &format!("corners {width}x{height} radius {radius}"),
+ );
+ }
+
+ #[test]
+ fn rounded_corners_match_original_for_varied_sizes() {
+ let sizes = [
+ (0, 0),
+ (0, 4),
+ (4, 0),
+ (1, 1),
+ (1, 2),
+ (2, 1),
+ (2, 2),
+ (3, 3),
+ (3, 5),
+ (5, 3),
+ (7, 9),
+ (8, 8),
+ (15, 17),
+ (16, 9),
+ (31, 20),
+ (64, 63),
+ (100, 50),
+ (127, 13),
+ (320, 180),
+ ];
+ let base_radii = [-1.0, 0.0, 0.25, 0.5, 1.0, 1.5, 2.2, 8.0, 10.5, 12.0];
+ for (width, height) in sizes {
+ for radius in base_radii {
+ assert_corners(width, height, radius);
+ }
+ let min_dim = width.min(height) as f32;
+ let max_dim = width.max(height) as f32;
+ assert_corners(width, height, min_dim / 2.0);
+ assert_corners(width, height, min_dim / 2.0 + 0.5);
+ assert_corners(width, height, max_dim + 10.0);
+ }
+ assert_corners(1920, 1080, 0.0);
+ assert_corners(1920, 1080, 12.0);
+ assert_corners(1919, 17, 6.5);
+ }
+
+ #[test]
+ fn rounded_corners_match_original_for_non_finite_radius() {
+ for (width, height) in [(1, 1), (2, 3), (8, 5), (17, 9)] {
+ assert_corners(width, height, f32::INFINITY);
+ assert_corners(width, height, f32::NAN);
+ assert_corners(width, height, f32::MIN_POSITIVE);
+ }
+ }
+
+ #[test]
+ fn non_window_screenshot_keeps_opaque_alpha() {
+ let image = rgb_image(13, 9, 99);
+ let expected: RgbaImage = image::buffer::ConvertBuffer::convert(&image);
+ let got = apply_window_rounded_corners(image, &ScreenCaptureTarget::CameraOnly);
+ assert_bytes_eq(got.as_raw(), expected.as_raw(), "camera only");
+ }
+}
diff --git a/crates/recording/src/sources/audio_mixer.rs b/crates/recording/src/sources/audio_mixer.rs
index 498c73aeb..9d30de182 100644
--- a/crates/recording/src/sources/audio_mixer.rs
+++ b/crates/recording/src/sources/audio_mixer.rs
@@ -1,6 +1,7 @@
use anyhow::Context as _;
use cap_media_info::AudioInfo;
use cap_timestamp::{MasterClock, SourceClockOutcome, SourceClockState, Timestamp, Timestamps};
+use futures::StreamExt as _;
use futures::channel::{mpsc, oneshot};
use std::time::Instant;
use std::{
@@ -151,6 +152,7 @@ impl AudioMixerBuilder {
required_stop: self.required_stop,
drain_deadline: None,
finishing: false,
+ input_waker: std::task::Waker::noop().clone(),
})
}
@@ -173,6 +175,7 @@ impl AudioMixerBuilder {
};
let _ = ready_tx.send(Ok(()));
+ mixer.input_waker = std::task::Waker::from(Arc::new(ThreadUnpark(std::thread::current())));
loop {
if stop_flag.load(Ordering::Relaxed)
@@ -193,7 +196,7 @@ impl AudioMixerBuilder {
break;
}
- std::thread::sleep(Duration::from_millis(5));
+ std::thread::park_timeout(mixer.idle_wait());
}
for source in &mixer.sources {
if source.late_samples_dropped > 0 {
@@ -229,6 +232,7 @@ pub struct AudioMixer {
required_stop: Option<CancellationToken>,
drain_deadline: Option<Instant>,
finishing: bool,
+ input_waker: std::task::Waker,
}
#[derive(Debug, Clone)]
@@ -520,6 +524,8 @@ impl AudioMixer {
fn buffer_sources(&mut self, now: Timestamp) -> anyhow::Result<()> {
let mut format_change: Option<(usize, AudioInfo, AudioInfo)> = None;
let clock_anchor = self.master_clock.timestamps().instant();
+ let waker = self.input_waker.clone();
+ let mut input_context = std::task::Context::from_waker(&waker);
for (index, source) in self.sources.iter_mut().enumerate() {
let rate = source.info.rate();
@@ -533,9 +539,9 @@ impl AudioMixer {
let AudioFrame {
inner: frame,
timestamp: raw_timestamp,
- } = match source.rx.try_next() {
- Ok(Some(frame)) => frame,
- Ok(None) => {
+ } = match source.rx.poll_next_unpin(&mut input_context) {
+ std::task::Poll::Ready(Some(frame)) => frame,
+ std::task::Poll::Ready(None) => {
source.ended = true;
anyhow::ensure!(
self.required_stop
@@ -545,7 +551,7 @@ impl AudioMixer {
);
break;
}
- Err(_) => break,
+ std::task::Poll::Pending => break,
};
source.last_input_timestamp = Some(raw_timestamp);
@@ -874,6 +880,13 @@ impl AudioMixer {
pub fn builder() -> AudioMixerBuilder {
AudioMixerBuilder::new()
}
+
+ /// How long the run loop sleeps when no input arrives first: half the
+ /// shortest source buffer timeout, so a source that went quiet is still
+ /// filled with silence on time, while arriving input wakes it at once.
+ fn idle_wait(&self) -> Duration {
+ idle_wait_for(self.sources.iter().map(|source| source.buffer_timeout))
+ }
}
fn mixer_now() -> Timestamp {
@@ -1099,6 +1112,30 @@ fn mixer_source_label(index: usize) -> &'static str {
LABELS.get(index).copied().unwrap_or("mixer-src-overflow")
}
+const MIN_IDLE_WAIT: Duration = Duration::from_millis(5);
+const MAX_IDLE_WAIT: Duration = Duration::from_millis(25);
+
+fn idle_wait_for(timeouts: impl IntoIterator<Item = Duration>) -> Duration {
+ timeouts
+ .into_iter()
+ .map(|timeout| timeout / 2)
+ .min()
+ .unwrap_or(MIN_IDLE_WAIT)
+ .clamp(MIN_IDLE_WAIT, MAX_IDLE_WAIT)
+}
+
+struct ThreadUnpark(std::thread::Thread);
+
+impl std::task::Wake for ThreadUnpark {
+ fn wake(self: Arc<Self>) {
+ self.0.unpark();
+ }
+
+ fn wake_by_ref(self: &Arc<Self>) {
+ self.0.unpark();
+ }
+}
+
fn buffer_timeout_for(info: &AudioInfo) -> Duration {
if info.sample_rate == 0 || info.buffer_size == 0 {
return DEFAULT_BUFFER_TIMEOUT;
@@ -2031,6 +2068,59 @@ mod test {
}
}
+ mod input_wake {
+ use super::*;
+
+ struct CountingWaker(std::sync::atomic::AtomicUsize);
+
+ impl std::task::Wake for CountingWaker {
+ fn wake(self: Arc<Self>) {
+ self.0.fetch_add(1, Ordering::SeqCst);
+ }
+ }
+
+ #[tokio::test]
+ async fn a_frame_arriving_after_a_drain_wakes_the_mixer() {
+ let (output_tx, _) = mpsc::channel::<AudioFrame>(4);
+ let mut builder = AudioMixerBuilder::new();
+ let (mut tx, rx) = mpsc::channel(4);
+ builder.add_source(SOURCE_INFO, rx);
+ let mut mixer = builder.build(output_tx).unwrap();
+ let wakes = Arc::new(CountingWaker(std::sync::atomic::AtomicUsize::new(0)));
+ mixer.input_waker = std::task::Waker::from(wakes.clone());
+
+ let now = Timestamp::Instant(mixer.timestamps.instant());
+ mixer.buffer_sources(now).unwrap();
+ assert_eq!(wakes.0.load(Ordering::SeqCst), 0);
+
+ tx.send(AudioFrame::new(
+ SOURCE_INFO.wrap_frame(&vec![0; SAMPLES_SECOND / 10]),
+ now,
+ ))
+ .await
+ .unwrap();
+ assert_eq!(wakes.0.load(Ordering::SeqCst), 1);
+
+ mixer.buffer_sources(now).unwrap();
+ assert_eq!(mixer.sources[0].buffer.len(), 1);
+ }
+
+ #[test]
+ fn idle_wait_is_half_the_shortest_buffer_timeout_within_bounds() {
+ assert_eq!(
+ idle_wait_for([Duration::from_millis(40), Duration::from_millis(100)]),
+ Duration::from_millis(20)
+ );
+ assert_eq!(
+ idle_wait_for([MIN_BUFFER_TIMEOUT_WIRED]),
+ Duration::from_millis(10)
+ );
+ assert_eq!(idle_wait_for([MAX_BUFFER_TIMEOUT]), MAX_IDLE_WAIT);
+ assert_eq!(idle_wait_for([Duration::from_millis(4)]), MIN_IDLE_WAIT);
+ assert_eq!(idle_wait_for([]), MIN_IDLE_WAIT);
+ }
+ }
+
mod source_buffer {
use super::*;
diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs
index a81ea9da8..ce214d905 100644
--- a/crates/recording/src/sources/microphone.rs
+++ b/crates/recording/src/sources/microphone.rs
@@ -75,6 +75,119 @@ fn reset_reconnect_after_frame(
*next_reconnect_after = MIC_RECONNECT_AFTER;
}
+// macOS can hand an active VoIP call the input while CoreAudio keeps delivering
+// on-time buffers of exact 0.0 (#2303). Those are real captured frames, so the
+// stall/keepalive path never fires. A live microphone's noise floor is never
+// bit-exact zero, even in a quiet room (~4% non-zero samples at -91 dBFS in the
+// issue's measurements), so a sustained run of exact zeros is the signal.
+const DIGITAL_SILENCE_WINDOW: Duration = Duration::from_secs(1);
+const DIGITAL_SILENCE_MIN_WINDOWS: u32 = 5;
+const DIGITAL_SILENCE_MAX_NONZERO_PER_MILLE: u64 = 1;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum DigitalSilenceUpdate {
+ Unchanged,
+ RunStarted,
+ RunExtended,
+ RunEnded,
+}
+
+struct DigitalSilenceDetector {
+ sample_rate: u64,
+ window_len: u64,
+ window_total: u64,
+ window_nonzero: u64,
+ silent_windows: u32,
+ run_samples: u64,
+ flagged_samples: u64,
+}
+
+impl DigitalSilenceDetector {
+ fn new(sample_rate: u32) -> Self {
+ let sample_rate = u64::from(sample_rate).max(1);
+ Self {
+ sample_rate,
+ window_len: (sample_rate * DIGITAL_SILENCE_WINDOW.as_millis() as u64 / 1000).max(1),
+ window_total: 0,
+ window_nonzero: 0,
+ silent_windows: 0,
+ run_samples: 0,
+ flagged_samples: 0,
+ }
+ }
+
+ fn reset(&mut self) -> DigitalSilenceUpdate {
+ let was_flagged = self.silent_windows >= DIGITAL_SILENCE_MIN_WINDOWS;
+ self.window_total = 0;
+ self.window_nonzero = 0;
+ self.silent_windows = 0;
+ self.run_samples = 0;
+ if was_flagged {
+ DigitalSilenceUpdate::RunEnded
+ } else {
+ DigitalSilenceUpdate::Unchanged
+ }
+ }
+
+ fn observe(&mut self, total: u64, nonzero: u64) -> DigitalSilenceUpdate {
+ self.window_total = self.window_total.saturating_add(total);
+ self.window_nonzero = self.window_nonzero.saturating_add(nonzero);
+ if self.window_total < self.window_len {
+ return DigitalSilenceUpdate::Unchanged;
+ }
+
+ let window_total = self.window_total;
+ let silent = self.window_nonzero.saturating_mul(1000)
+ <= window_total.saturating_mul(DIGITAL_SILENCE_MAX_NONZERO_PER_MILLE);
+ self.window_total = 0;
+ self.window_nonzero = 0;
+
+ if !silent {
+ return self.reset();
+ }
+
+ self.silent_windows = self.silent_windows.saturating_add(1);
+ self.run_samples = self.run_samples.saturating_add(window_total);
+ match self.silent_windows.cmp(&DIGITAL_SILENCE_MIN_WINDOWS) {
+ std::cmp::Ordering::Less => DigitalSilenceUpdate::Unchanged,
+ std::cmp::Ordering::Equal => {
+ self.flagged_samples = self.flagged_samples.saturating_add(self.run_samples);
+ DigitalSilenceUpdate::RunStarted
+ }
+ std::cmp::Ordering::Greater => {
+ self.flagged_samples = self.flagged_samples.saturating_add(window_total);
+ DigitalSilenceUpdate::RunExtended
+ }
+ }
+ }
+
+ fn run_ms(&self) -> u64 {
+ self.run_samples.saturating_mul(1000) / self.sample_rate
+ }
+
+ fn flagged_ms(&self) -> u64 {
+ self.flagged_samples.saturating_mul(1000) / self.sample_rate
+ }
+}
+
+fn count_nonzero_samples(frame: &ffmpeg::frame::Audio) -> Option<(u64, u64)> {
+ if frame.format() != ffmpeg::format::Sample::F32(ffmpeg::format::sample::Type::Packed) {
+ return None;
+ }
+ let samples = frame.samples() * usize::from(frame.channels());
+ // An empty device callback yields a frame with no planes, where
+ // `data(0)` panics instead of returning an empty slice.
+ if samples == 0 || frame.planes() == 0 {
+ return None;
+ }
+ let bytes = frame.data(0).get(..samples * size_of::<f32>())?;
+ let nonzero = bytes
+ .chunks_exact(size_of::<f32>())
+ .filter(|sample| f32::from_ne_bytes([sample[0], sample[1], sample[2], sample[3]]) != 0.0)
+ .count();
+ Some((samples as u64, nonzero as u64))
+}
+
pub struct Microphone {
#[cfg(windows)]
capture_scope: crate::output_pipeline::PipelineBuildScope,
@@ -263,6 +376,7 @@ impl AudioSource for Microphone {
Self: Sized,
{
let health_tx = ctx.health_tx().clone();
+ let digital_silence_ms = ctx.digital_silence_ms();
#[cfg(windows)]
let capture_scope = crate::output_pipeline::PipelineBuildScope::new();
#[cfg(windows)]
@@ -375,6 +489,7 @@ impl AudioSource for Microphone {
let health_tx = health_tx.clone();
let cancel = cancel.clone();
let recording_muted = recording_muted.clone();
+ let digital_silence_ms = digital_silence_ms.clone();
#[cfg(windows)]
let capture_scope = capture_scope.clone();
#[cfg(windows)]
@@ -382,6 +497,7 @@ impl AudioSource for Microphone {
#[cfg(target_os = "linux")]
let required_health = required_health.clone();
async move {
+ let mut digital_silence = DigitalSilenceDetector::new(audio_info.sample_rate);
let mut resampler: Option<MicResampler> = None;
let mut silence_mode = false;
let mut silence_start: Option<Instant> = None;
@@ -511,10 +627,52 @@ impl AudioSource for Microphone {
// frame: cadence, sample counts, and timestamps must stay
// identical to the unmuted stream so the muxer timeline
// and the stall/keepalive machinery see no difference.
- if recording_muted.load(Ordering::Relaxed) {
+ let muted = recording_muted.load(Ordering::Relaxed);
+ if muted {
silence_frame_payload(&mut audio_frame.inner);
}
+ let silence_update = if muted {
+ digital_silence.reset()
+ } else {
+ count_nonzero_samples(&audio_frame.inner)
+ .map(|(total, nonzero)| {
+ digital_silence.observe(total, nonzero)
+ })
+ .unwrap_or(DigitalSilenceUpdate::Unchanged)
+ };
+ match silence_update {
+ DigitalSilenceUpdate::RunStarted => {
+ warn!(
+ run_ms = digital_silence.run_ms(),
+ "Microphone is delivering exact digital silence while unmuted; another app may be holding the input"
+ );
+ digital_silence_ms
+ .store(digital_silence.flagged_ms(), Ordering::Relaxed);
+ emit_health(
+ &health_tx,
+ PipelineHealthEvent::MicrophoneDigitalSilence,
+ );
+ }
+ DigitalSilenceUpdate::RunExtended => {
+ digital_silence_ms
+ .store(digital_silence.flagged_ms(), Ordering::Relaxed);
+ }
+ DigitalSilenceUpdate::RunEnded => {
+ info!(
+ total_digital_silence_ms = digital_silence.flagged_ms(),
+ muted, "Microphone digital silence ended"
+ );
+ if !muted {
+ emit_health(
+ &health_tx,
+ PipelineHealthEvent::SourceRestarted,
+ );
+ }
+ }
+ DigitalSilenceUpdate::Unchanged => {}
+ }
+
let sample_count = audio_frame.samples();
last_frame_duration = Duration::from_secs_f64(
sample_count as f64 / audio_info.rate() as f64,
@@ -1569,3 +1727,270 @@ mod owned_microphone_tests {
assert!(cancel.is_cancelled());
}
}
+
+#[cfg(test)]
+mod digital_silence_tests {
+ use super::*;
+ use crate::{
+ feeds::microphone::synthetic::SyntheticMicrophone,
+ output_pipeline::{AudioMuxer, Muxer, OutputPipeline, TaskPool},
+ };
+ use cap_timestamp::Timestamp;
+
+ const RATE: u32 = 48_000;
+ const CHUNK: usize = 480;
+
+ fn target_info() -> AudioInfo {
+ AudioInfo::new_raw(
+ cap_media_info::Sample::F32(cap_media_info::Type::Packed),
+ RATE,
+ 1,
+ )
+ }
+
+ fn frame_with(samples: &[f32]) -> ffmpeg::frame::Audio {
+ let mut frame = create_silence_frame(&target_info(), samples.len());
+ let bytes: Vec<u8> = samples.iter().flat_map(|s| s.to_ne_bytes()).collect();
+ frame.data_mut(0)[..bytes.len()].copy_from_slice(&bytes);
+ frame
+ }
+
+ fn quiet_room(len: usize) -> Vec<f32> {
+ (0..len)
+ .map(|i| if i % 25 == 0 { 3.0e-5 } else { 0.0 })
+ .collect()
+ }
+
+ fn feed_seconds(
+ detector: &mut DigitalSilenceDetector,
+ samples: &[f32],
+ seconds: f64,
+ ) -> Vec<DigitalSilenceUpdate> {
+ let frame = frame_with(samples);
+ let (total, nonzero) = count_nonzero_samples(&frame).unwrap();
+ let chunks = (seconds * f64::from(RATE) / samples.len() as f64).round() as usize;
+ (0..chunks)
+ .map(|_| detector.observe(total, nonzero))
+ .filter(|update| *update != DigitalSilenceUpdate::Unchanged)
+ .collect()
+ }
+
+ #[test]
+ fn exact_zero_frames_flag_after_a_sustained_window() {
+ let mut detector = DigitalSilenceDetector::new(RATE);
+ let zeros = vec![0.0; CHUNK];
+ assert!(feed_seconds(&mut detector, &zeros, 4.99).is_empty());
+ assert_eq!(
+ feed_seconds(&mut detector, &zeros, 0.01),
+ vec![DigitalSilenceUpdate::RunStarted]
+ );
+ assert_eq!(detector.flagged_ms(), 5_000);
+ assert_eq!(
+ feed_seconds(&mut detector, &zeros, 2.0),
+ vec![DigitalSilenceUpdate::RunExtended; 2]
+ );
+ assert_eq!(detector.flagged_ms(), 7_000);
+ }
+
+ #[test]
+ fn quiet_room_noise_floor_is_not_digital_silence() {
+ let mut detector = DigitalSilenceDetector::new(RATE);
+ assert!(feed_seconds(&mut detector, &quiet_room(CHUNK), 30.0).is_empty());
+ assert_eq!(detector.flagged_ms(), 0);
+ }
+
+ #[test]
+ fn muting_breaks_a_digital_silence_run() {
+ let mut detector = DigitalSilenceDetector::new(RATE);
+ let zeros = vec![0.0; CHUNK];
+ assert!(feed_seconds(&mut detector, &zeros, 4.0).is_empty());
+ assert_eq!(detector.reset(), DigitalSilenceUpdate::Unchanged);
+ assert!(feed_seconds(&mut detector, &zeros, 4.0).is_empty());
+ assert_eq!(detector.flagged_ms(), 0);
+ }
+
+ #[test]
+ fn real_audio_ends_a_run_and_later_runs_accumulate() {
+ let mut detector = DigitalSilenceDetector::new(RATE);
+ let zeros = vec![0.0; CHUNK];
+ feed_seconds(&mut detector, &zeros, 6.0);
+ assert_eq!(
+ feed_seconds(&mut detector, &quiet_room(CHUNK), 1.0),
+ vec![DigitalSilenceUpdate::RunEnded]
+ );
+ assert_eq!(detector.flagged_ms(), 6_000);
+ assert_eq!(
+ feed_seconds(&mut detector, &zeros, 5.0),
+ vec![DigitalSilenceUpdate::RunStarted]
+ );
+ assert_eq!(detector.flagged_ms(), 11_000);
+ }
+
+ #[test]
+ fn only_bit_exact_zero_counts_as_silent() {
+ let tiny = frame_with(&[1.0e-30; CHUNK]);
+ assert_eq!(
+ count_nonzero_samples(&tiny),
+ Some((CHUNK as u64, CHUNK as u64))
+ );
+ let negative_zero = frame_with(&[-0.0; CHUNK]);
+ assert_eq!(
+ count_nonzero_samples(&negative_zero),
+ Some((CHUNK as u64, 0))
+ );
+ }
+
+ #[test]
+ fn empty_frames_are_skipped_without_touching_planes() {
+ let empty = create_silence_frame(&target_info(), 0);
+ assert_eq!(count_nonzero_samples(&empty), None);
+ }
+
+ struct SinkMuxer;
+
+ impl Muxer for SinkMuxer {
+ type Config = ();
+
+ async fn setup(
+ _: (),
+ _: std::path::PathBuf,
+ _: Option<cap_media_info::VideoInfo>,
+ _: Option<AudioInfo>,
+ _: Arc<AtomicBool>,
+ _: &mut TaskPool,
+ ) -> anyhow::Result<Self> {
+ Ok(Self)
+ }
+
+ fn finish(&mut self, _: Duration) -> anyhow::Result<anyhow::Result<()>> {
+ Ok(Ok(()))
+ }
+ }
+
+ impl AudioMuxer for SinkMuxer {
+ fn send_audio_frame(&mut self, _: AudioFrame, _: Duration) -> anyhow::Result<()> {
+ Ok(())
+ }
+ }
+
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ enum Reported {
+ DigitalSilence,
+ Restarted,
+ }
+
+ enum Push<'a> {
+ Samples(&'a [f32], u32),
+ EmptyCallbacks(u32),
+ }
+
+ // Synchronizes on the source's own health events rather than on what the
+ // muxer accepted: downstream timing stages may drop frames whose
+ // timestamps run ahead of the wall clock, but the detector sits upstream.
+ async fn record_through_feed(pushes: &[Push<'_>], expected: &[Reported]) -> Option<u32> {
+ let mic = SyntheticMicrophone::spawn("voip-held-input", RATE, 1);
+ let lock = Arc::new(mic.lock().await.unwrap());
+ let temp = tempfile::tempdir().unwrap();
+ let mut pipeline = OutputPipeline::builder(temp.path().join("audio-input.ogg"))
+ .with_audio_source::<Microphone>(lock.clone())
+ .build::<SinkMuxer>(())
+ .await
+ .unwrap();
+ let mut health = pipeline.take_health_rx().unwrap();
+ let wanted = expected.len();
+ let collector = tokio::spawn(async move {
+ let mut seen = Vec::new();
+ while seen.len() < wanted {
+ match health.recv().await {
+ Some(PipelineHealthEvent::MicrophoneDigitalSilence) => {
+ seen.push(Reported::DigitalSilence)
+ }
+ Some(PipelineHealthEvent::SourceRestarted) => seen.push(Reported::Restarted),
+ Some(_) => {}
+ None => break,
+ }
+ }
+ seen
+ });
+
+ let start = Instant::now();
+ let mut pushed_samples: u64 = 0;
+ for push in pushes {
+ match push {
+ Push::Samples(samples, seconds) => {
+ let chunks = RATE as usize / samples.len() * *seconds as usize;
+ for _ in 0..chunks {
+ let at = start
+ + Duration::from_secs_f64(pushed_samples as f64 / f64::from(RATE));
+ mic.push(samples, Timestamp::Instant(at)).await;
+ pushed_samples += samples.len() as u64;
+ tokio::task::yield_now().await;
+ }
+ }
+ Push::EmptyCallbacks(count) => {
+ for _ in 0..*count {
+ let at = start
+ + Duration::from_secs_f64(pushed_samples as f64 / f64::from(RATE));
+ mic.push(&[], Timestamp::Instant(at)).await;
+ tokio::task::yield_now().await;
+ }
+ }
+ }
+ }
+
+ let seen = tokio::time::timeout(Duration::from_secs(20), collector)
+ .await
+ .expect("the source never reported the expected health events")
+ .unwrap();
+ assert_eq!(seen, expected);
+ pipeline.stop().await.unwrap().digital_silence_ms
+ }
+
+ #[tokio::test]
+ async fn all_zero_frames_through_the_feed_surface_on_stop() {
+ let digital_silence_ms = record_through_feed(
+ &[Push::Samples(&[0.0; 4_800], 8)],
+ &[Reported::DigitalSilence],
+ )
+ .await;
+ assert!(
+ digital_silence_ms.is_some_and(|ms| ms >= 5_000),
+ "expected the stop result to carry the silent span, got {digital_silence_ms:?}"
+ );
+ }
+
+ #[tokio::test]
+ async fn quiet_room_through_the_feed_reports_nothing() {
+ let room = quiet_room(4_800);
+ let digital_silence_ms = record_through_feed(
+ &[Push::Samples(&room, 8), Push::Samples(&[0.0; 4_800], 6)],
+ &[Reported::DigitalSilence],
+ )
+ .await;
+ assert!(
+ digital_silence_ms.is_some_and(|ms| (5_000..=6_000).contains(&ms)),
+ "only the trailing zeros may count, got {digital_silence_ms:?}"
+ );
+ }
+
+ #[tokio::test]
+ async fn real_audio_after_the_call_clears_the_live_warning() {
+ let room = quiet_room(4_800);
+ let digital_silence_ms = record_through_feed(
+ &[Push::Samples(&[0.0; 4_800], 6), Push::Samples(&room, 2)],
+ &[Reported::DigitalSilence, Reported::Restarted],
+ )
+ .await;
+ assert_eq!(digital_silence_ms, Some(6_000));
+ }
+
+ #[tokio::test]
+ async fn empty_device_callbacks_do_not_stop_the_forwarder() {
+ let digital_silence_ms = record_through_feed(
+ &[Push::EmptyCallbacks(50), Push::Samples(&[0.0; 4_800], 6)],
+ &[Reported::DigitalSilence],
+ )
+ .await;
+ assert!(digital_silence_ms.is_some_and(|ms| ms >= 5_000));
+ }
+}
diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs
index e77a7e611..9e8a19c51 100644
--- a/crates/recording/src/sources/screen_capture/mod.rs
+++ b/crates/recording/src/sources/screen_capture/mod.rs
@@ -436,8 +436,8 @@ fn logical_axis_to_physical(
let start = position.clamp(0.0, logical_extent);
let end = (position + size).clamp(0.0, logical_extent);
let scale = physical_extent / logical_extent;
- let physical_start = start.min(end) * scale;
- let physical_end = start.max(end) * scale;
+ let physical_start = (start.min(end) * scale).round();
+ let physical_end = (start.max(end) * scale).round();
let min_size = 2.0_f64.min(physical_extent);
let size = (physical_end - physical_start).clamp(min_size, physical_extent);
let position = physical_start.min(physical_extent - size).max(0.0);
@@ -616,8 +616,12 @@ pub fn list_displays() -> Vec<(CaptureDisplay, Display)> {
.collect()
}
+// NSFloatingWindowLevel is 3; Stay On Top moves Simulator windows to this level.
#[cfg(target_os = "macos")]
-fn is_listable_macos_window(
+const MACOS_FLOATING_WINDOW_LEVEL: i32 = 3;
+
+#[cfg(target_os = "macos")]
+pub fn is_listable_macos_window(
level: Option<i32>,
owner_name: &str,
bundle_identifier: Option<&str>,
@@ -632,12 +636,18 @@ fn is_listable_macos_window(
return true;
}
- matches!(level, Some(level) if level > 0)
- && include_accessory_panels
- && is_accessory_application
- && bundle_identifier.is_some_and(|identifier| {
- !identifier.starts_with("com.apple.") && !identifier.starts_with("so.cap.desktop")
- })
+ let is_third_party_bundle = bundle_identifier.is_some_and(|identifier| {
+ !identifier.starts_with("com.apple.") && !identifier.starts_with("so.cap.desktop")
+ });
+
+ if is_accessory_application {
+ return matches!(level, Some(level) if level > 0)
+ && include_accessory_panels
+ && is_third_party_bundle;
+ }
+
+ level == Some(MACOS_FLOATING_WINDOW_LEVEL)
+ && bundle_identifier.is_some_and(|identifier| !identifier.starts_with("so.cap.desktop"))
}
pub fn list_windows() -> Vec<(CaptureWindow, Window)> {
@@ -663,8 +673,7 @@ fn list_windows_inner(_include_accessory_panels: bool) -> Vec<(CaptureWindow, Wi
let owner_name = v.owner_name()?;
let level = v.raw_handle().level();
let bundle_identifier = v.raw_handle().bundle_identifier();
- let is_accessory_application = _include_accessory_panels
- && matches!(level, Some(level) if level > 0)
+ let is_accessory_application = matches!(level, Some(level) if level > 0)
&& v.raw_handle().is_accessory_application();
if !is_listable_macos_window(
@@ -795,7 +804,7 @@ mod tests {
true,
true,
));
- assert!(!is_listable_macos_window(
+ assert!(is_listable_macos_window(
Some(3),
"Example",
Some("com.example.app"),
@@ -832,6 +841,123 @@ mod tests {
));
}
+ #[test]
+ #[cfg(target_os = "macos")]
+ fn macos_window_filter_lists_floating_app_windows_in_the_picker() {
+ assert!(is_listable_macos_window(
+ Some(MACOS_FLOATING_WINDOW_LEVEL),
+ "Simulator",
+ Some("com.apple.iphonesimulator"),
+ false,
+ false,
+ ));
+ assert!(is_listable_macos_window(
+ Some(MACOS_FLOATING_WINDOW_LEVEL),
+ "Example",
+ Some("com.example.app"),
+ false,
+ false,
+ ));
+ assert!(!is_listable_macos_window(
+ Some(MACOS_FLOATING_WINDOW_LEVEL),
+ "Cap",
+ Some("so.cap.desktop.dev"),
+ false,
+ false,
+ ));
+ assert!(!is_listable_macos_window(
+ Some(MACOS_FLOATING_WINDOW_LEVEL),
+ "cap-gpui",
+ None,
+ false,
+ false,
+ ));
+ assert!(!is_listable_macos_window(
+ Some(MACOS_FLOATING_WINDOW_LEVEL + 1),
+ "Example",
+ Some("com.example.app"),
+ false,
+ false,
+ ));
+ assert!(!is_listable_macos_window(
+ Some(25),
+ "Control Centre",
+ Some("com.apple.controlcenter"),
+ false,
+ false,
+ ));
+ }
+
+ #[test]
+ #[cfg(target_os = "macos")]
+ fn macos_window_filter_preserves_normal_windows_and_rejects_other_levels() {
+ for accessory in [false, true] {
+ assert!(is_listable_macos_window(
+ Some(0),
+ "Example",
+ None,
+ false,
+ accessory
+ ));
+ }
+ for level in [None, Some(-1), Some(1), Some(2), Some(4), Some(25)] {
+ for include_accessory_panels in [false, true] {
+ assert!(!is_listable_macos_window(
+ level,
+ "Example",
+ Some("com.example.app"),
+ include_accessory_panels,
+ false,
+ ));
+ }
+ }
+ }
+
+ #[test]
+ #[cfg(target_os = "macos")]
+ fn macos_window_filter_preserves_accessory_panel_exclusions() {
+ for level in [Some(3), Some(25)] {
+ for bundle in [
+ None,
+ Some("com.apple.controlcenter"),
+ Some("so.cap.desktop.dev"),
+ ] {
+ for include_accessory_panels in [false, true] {
+ assert!(!is_listable_macos_window(
+ level,
+ "Panel",
+ bundle,
+ include_accessory_panels,
+ true,
+ ));
+ }
+ }
+ assert!(!is_listable_macos_window(
+ level,
+ "Panel",
+ Some("com.example.app"),
+ false,
+ true,
+ ));
+ assert!(is_listable_macos_window(
+ level,
+ "Panel",
+ Some("com.example.app"),
+ true,
+ true,
+ ));
+ }
+ for level in [Some(0), Some(3)] {
+ assert!(!is_listable_macos_window(
+ level,
+ "Window Server",
+ Some("com.example.app"),
+ false,
+ false,
+ ));
+ }
+ }
+
#[test]
fn logical_area_to_physical_bounds_clamps_to_display() {
let bounds = LogicalBounds::new(
@@ -928,6 +1054,38 @@ mod tests {
}
}
+ #[test]
+ fn fractional_area_edges_round_to_pixels_and_stay_inside_the_display() {
+ for (position, size, expected) in [
+ (100.2, 800.4, (150.0, 1201.0)),
+ (-0.2, 100.4, (0.0, 150.0)),
+ (1279.8, 10.0, (1918.0, 2.0)),
+ ] {
+ assert_eq!(
+ logical_axis_to_physical(position, size, 1280.0, 1920.0),
+ Some(expected),
+ );
+ }
+ }
+
+ #[test]
+ fn full_overlay_selection_keeps_native_resolution_at_fractional_scales() {
+ for scale in [1.25, 1.5, 1.75] {
+ let physical = PhysicalSize::new(1920.0, 1080.0);
+ let logical = LogicalSize::new(physical.width() / scale, physical.height() / scale);
+ let overlay_size = LogicalSize::new(
+ f64::from(logical.width() as f32),
+ f64::from(logical.height() as f32),
+ );
+ let selection = LogicalBounds::new(LogicalPosition::new(0.0, 0.0), overlay_size);
+ let crop = logical_area_to_physical_bounds(selection, logical, physical).unwrap();
+ let output = constrain_capture_size(crop.size(), None);
+
+ assert_eq!(output.width(), physical.width(), "scale {scale}");
+ assert_eq!(output.height(), physical.height(), "scale {scale}");
+ }
+ }
+
#[test]
fn partial_selection_scales_by_display_dpi() {
// 150% display: logical 1280x720 backed by physical 1920x1080.
diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs
index 8c962a50f..f0786c6ca 100644
--- a/crates/recording/src/studio_recording.rs
+++ b/crates/recording/src/studio_recording.rs
@@ -16,7 +16,7 @@ use crate::{
CursorActor, CursorActorResponse, Cursors, IncrementalCaptureOutputs, spawn_cursor_recorder,
},
feeds::{camera::CameraFeedLock, microphone::MicrophoneFeedLock},
- ffmpeg::{FragmentedAudioMuxer, FragmentedAudioMuxerConfig, OggMuxer},
+ ffmpeg::{FragmentedAudioMuxer, FragmentedAudioMuxerConfig, OggMuxer, OggMuxerConfig},
output_pipeline::{
AudioAnchor, AudioGapSummary, DoneFut, FinishedOutputPipeline, OutputPipeline,
PipelineDoneError,
@@ -2838,12 +2838,14 @@ async fn stop_recording(
start_time: mic_start_time,
device_id: s.mic_device_id.clone(),
gap_summary: to_project_gap_summary(mic.audio_gap_summary),
+ digital_silence_ms: mic.digital_silence_ms,
}),
system_audio: s.pipeline.system_audio.map(|audio| AudioMeta {
path: make_relative(&audio.path),
start_time: start_times.system_audio,
device_id: None,
gap_summary: to_project_gap_summary(audio.audio_gap_summary),
+ digital_silence_ms: None,
}),
cursor: s
.pipeline
@@ -3562,6 +3564,7 @@ async fn create_segment_pipeline(
.with_start_gate(start_gate.clone())
.build::<FragmentedAudioMuxer>(FragmentedAudioMuxerConfig {
shared_pause_state: shared_pause_state.clone(),
+ write_behind: cfg!(target_os = "macos"),
})
.instrument(error_span!("mic-out"))
.await
@@ -3570,7 +3573,9 @@ async fn create_segment_pipeline(
.with_audio_source::<sources::Microphone>(mic_feed)
.with_timestamps(start_time)
.with_start_gate(start_gate.clone())
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig {
+ write_behind: cfg!(target_os = "macos"),
+ })
.instrument(error_span!("mic-out"))
.await
};
@@ -3594,6 +3599,7 @@ async fn create_segment_pipeline(
.with_audio_anchor(AudioAnchor::PipelineEpoch)
.build::<FragmentedAudioMuxer>(FragmentedAudioMuxerConfig {
shared_pause_state: shared_pause_state.clone(),
+ write_behind: cfg!(target_os = "macos"),
})
.instrument(error_span!("system-audio-out"))
.await
@@ -3603,7 +3609,9 @@ async fn create_segment_pipeline(
.with_timestamps(start_time)
.with_start_gate(start_gate.clone())
.with_audio_anchor(AudioAnchor::PipelineEpoch)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig {
+ write_behind: cfg!(target_os = "macos"),
+ })
.instrument(error_span!("system-audio-out"))
.await
};
@@ -4607,6 +4615,7 @@ mod tests {
video_frame_count: 30,
video_timestamp_span: Some((Duration::ZERO, Duration::from_millis(967))),
audio_gap_summary: None,
+ digital_silence_ms: None,
},
microphone: None,
camera: None,
@@ -5399,6 +5408,7 @@ mod tests {
video_frame_count,
video_timestamp_span: None,
audio_gap_summary: None,
+ digital_silence_ms: None,
}
}
diff --git a/crates/recording/tests/recovery.rs b/crates/recording/tests/recovery.rs
index 590815539..d27e5e52f 100644
--- a/crates/recording/tests/recovery.rs
+++ b/crates/recording/tests/recovery.rs
@@ -1745,12 +1745,14 @@ fn set_recovery_optional_tracks(project: &Path) {
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
segment.system_audio = Some(cap_project::AudioMeta {
path: RelativePathBuf::from("content/segments/segment-0/system_audio.ogg"),
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
meta.save_for_project().unwrap();
}
diff --git a/crates/recording/tests/sync_matrix.rs b/crates/recording/tests/sync_matrix.rs
index f321266bd..1db057022 100644
--- a/crates/recording/tests/sync_matrix.rs
+++ b/crates/recording/tests/sync_matrix.rs
@@ -22,7 +22,8 @@ use cap_recording::{
AudioFrame, ChannelAudioSource, ChannelAudioSourceConfig, ChannelVideoSource,
ChannelVideoSourceConfig, OutputPipeline,
ffmpeg::{
- FFmpegVideoFrame, Mp4Muxer, OggMuxer, SegmentedVideoMuxer, SegmentedVideoMuxerConfig,
+ FFmpegVideoFrame, Mp4Muxer, OggMuxer, OggMuxerConfig, SegmentedVideoMuxer,
+ SegmentedVideoMuxerConfig,
},
};
use cap_timestamp::{Timestamp, Timestamps};
@@ -767,7 +768,7 @@ async fn run_audio_case(case: AudioCase) -> Result<String, String> {
let pipeline = OutputPipeline::builder(out_path.clone())
.with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(info, rx))
.with_timestamps(timestamps)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await
.map_err(|e| format!("pipeline build: {e}"))?;
@@ -996,7 +997,7 @@ async fn run_system_audio_case(case: SystemAudioCase) -> Result<String, String>
.with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(info, rx))
.with_timestamps(timestamps)
.with_audio_anchor(AudioAnchor::PipelineEpoch)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await
.map_err(|e| format!("pipeline build: {e}"))?;
@@ -1078,14 +1079,14 @@ async fn run_mic_with_system_audio_case() -> Result<String, String> {
let mic_pipeline = OutputPipeline::builder(mic_path.clone())
.with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(info, mic_rx))
.with_timestamps(timestamps)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await
.map_err(|e| format!("mic pipeline build: {e}"))?;
let sys_pipeline = OutputPipeline::builder(sys_path.clone())
.with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(info, sys_rx))
.with_timestamps(timestamps)
.with_audio_anchor(AudioAnchor::PipelineEpoch)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await
.map_err(|e| format!("system pipeline build: {e}"))?;
diff --git a/crates/rendering/examples/mask-preparation-benchmark.rs b/crates/rendering/examples/mask-preparation-benchmark.rs
new file mode 100644
index 000000000..a50c8a91d
--- /dev/null
+++ b/crates/rendering/examples/mask-preparation-benchmark.rs
@@ -0,0 +1,188 @@
+use cap_project::{CursorEvents, MaskKind, MaskSegment, ProjectConfiguration, RecordingMeta, XY};
+use cap_rendering::{
+ DecodedFrame, DecodedSegmentFrames, FrameRenderer, MaskRenderMode, ProjectUniforms,
+ RenderOptions, RenderVideoConstants, RendererLayers, ZoomTransformTimeline,
+};
+use std::hint::black_box;
+use std::time::Instant;
+
+fn segment(index: usize, total: usize, active: usize) -> MaskSegment {
+ let active = total > 0 && (index * 97) % total < active;
+ MaskSegment {
+ start: if active { 0.0 } else { 20.0 + index as f64 },
+ end: if active { 10.0 } else { 30.0 + index as f64 },
+ track: ((index * 97) % 31) as u32,
+ enabled: true,
+ mask_type: if index.is_multiple_of(2) {
+ MaskKind::Sensitive
+ } else {
+ MaskKind::Highlight
+ },
+ center: XY::new((index % 10) as f64 / 10.0, 0.5),
+ size: XY::new(0.25, 0.25),
+ feather: 0.1,
+ opacity: 0.7,
+ pixelation: 18.0,
+ darkness: 0.5,
+ fade_duration: 0.15,
+ keyframes: Default::default(),
+ }
+}
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() {
+ let directory = tempfile::tempdir().unwrap();
+ let mut metadata: RecordingMeta = serde_json::from_value(serde_json::json!({
+ "pretty_name": "mask probe",
+ "display": {"path": "unused.mp4", "fps": 60},
+ "camera": null, "audio": null, "cursor": null
+ }))
+ .unwrap();
+ metadata.project_path = directory.path().to_path_buf();
+ let studio = metadata.studio_meta().unwrap().clone();
+ let constants = RenderVideoConstants::new_with_options(
+ RenderOptions {
+ screen_size: XY::new(320, 180),
+ camera_size: None,
+ preserve_screen_alpha: false,
+ },
+ metadata,
+ studio,
+ )
+ .await
+ .unwrap();
+ println!(
+ "PROBE_INFO {}",
+ serde_json::json!({"adapter": constants.adapter_name(), "software": constants.is_software_adapter,
+ "scope": "Native full ProjectUniforms preparation. Synthetic decoded input. Excludes GPU submission, decode, encode, GPUI scheduling and display."})
+ );
+ let cursor = CursorEvents::default();
+ let frames = DecodedSegmentFrames {
+ screen_size: XY::new(320, 180),
+ screen_frame: None,
+ camera_frame: None,
+ segment_time: 0.0,
+ recording_time: 0.0,
+ segment_has_camera: false,
+ };
+ let samples =
+ std::env::var("CAP_MASK_PROBE_SAMPLES").map_or(16, |value| value.parse::<usize>().unwrap());
+ let output = std::path::PathBuf::from(std::env::var("CAP_MASK_PROBE_OUTPUT").unwrap());
+ std::fs::create_dir_all(&output).unwrap();
+ for (name, total, active) in [
+ ("empty", 0, 0),
+ ("eight_all_active", 8, 8),
+ ("64_one_active", 64, 1),
+ ("256_four_active", 256, 4),
+ ("1024_four_active", 1024, 4),
+ ("256_all_active", 256, 256),
+ ] {
+ let mut project = ProjectConfiguration {
+ background: cap_project::BackgroundConfiguration { shadow: 0.0, advanced_shadow: None, ..Default::default() },
+ timeline: Some(serde_json::from_value(serde_json::json!({
+ "segments": [{"recordingSegment": 0, "start": 0.0, "end": 10.0, "timescale": 1.0}],
+ "zoomSegments": []
+ })).unwrap()),
+ screen_motion_blur: 0.0,
+ ..Default::default()
+ };
+ project.timeline.as_mut().unwrap().mask_segments = (0..total)
+ .map(|index| segment(index, total, active))
+ .collect();
+ let zoom = ZoomTransformTimeline::from_project(
+ &project,
+ &cursor,
+ 10.0,
+ constants.options.screen_size,
+ );
+ let project = std::sync::Arc::new(project);
+ let prepare = |frame: u32| {
+ ProjectUniforms::new(
+ &constants,
+ &project,
+ frame,
+ 60,
+ XY::new(1920, 1080),
+ &cursor,
+ &frames,
+ 10.0,
+ &zoom,
+ )
+ };
+ let calibration = Instant::now();
+ for frame in 0..1200 {
+ black_box(prepare(60 + frame % 120));
+ }
+ let per_frame = calibration.elapsed().as_nanos() as f64 / 1200.0;
+ let iterations =
+ ((30_000_000.0 / per_frame).clamp(120.0, 1_000_000.0) as usize / 120).max(1) * 120;
+ for sample in 0..samples {
+ let start = Instant::now();
+ for frame in 0..iterations {
+ black_box(prepare(60 + (frame % 120) as u32));
+ }
+ let elapsed = start.elapsed().as_nanos();
+ println!(
+ "PROBE_SAMPLE {}",
+ serde_json::json!({"case": name, "total": total,
+ "active": active, "sample": sample, "iterations": iterations,
+ "elapsed_ns": elapsed, "ns_per_frame": elapsed as f64 / iterations as f64})
+ );
+ }
+ let uniforms = prepare(120);
+ assert_eq!(uniforms.masks.len(), active);
+ let mask_bits = uniforms
+ .masks
+ .iter()
+ .map(|mask| {
+ [
+ mask.track as u64,
+ mask.center.x.to_bits() as u64,
+ mask.center.y.to_bits() as u64,
+ mask.size.x.to_bits() as u64,
+ mask.size.y.to_bits() as u64,
+ mask.feather.to_bits() as u64,
+ mask.opacity.to_bits() as u64,
+ mask.effect_size.to_bits() as u64,
+ mask.darkness.to_bits() as u64,
+ match mask.mode {
+ MaskRenderMode::Pixelate => 0,
+ MaskRenderMode::Highlight => 1,
+ MaskRenderMode::Blur => 2,
+ },
+ ]
+ })
+ .collect::<Vec<_>>();
+ let mut layers = RendererLayers::new_with_options(
+ &constants.device,
+ &constants.queue,
+ constants.is_software_adapter,
+ );
+ let pixels = (0..(320 * 180))
+ .flat_map(|index| [(index % 255) as u8, ((index / 320) % 255) as u8, 89, 255])
+ .collect();
+ let image_frames = DecodedSegmentFrames {
+ screen_frame: Some(DecodedFrame::new(pixels, 320, 180)),
+ ..frames.clone()
+ };
+ let rendered = FrameRenderer::new(&constants)
+ .render_immediate(image_frames, uniforms, &cursor, true, &mut layers)
+ .await
+ .unwrap();
+ let packed = rendered
+ .data
+ .chunks_exact(rendered.padded_bytes_per_row as usize)
+ .take(rendered.height as usize)
+ .flat_map(|row| row[..rendered.width as usize * 4].iter().copied())
+ .collect::<Vec<_>>();
+ std::fs::write(output.join(format!("{name}.rgba")), packed).unwrap();
+ std::fs::write(
+ output.join(format!("{name}.json")),
+ serde_json::to_vec_pretty(&serde_json::json!({
+ "width": rendered.width, "height": rendered.height, "mask_bits": mask_bits,
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+ }
+}
diff --git a/crates/rendering/src/cursor_interpolation.rs b/crates/rendering/src/cursor_interpolation.rs
index a8ccaa95e..002283bb6 100644
--- a/crates/rendering/src/cursor_interpolation.rs
+++ b/crates/rendering/src/cursor_interpolation.rs
@@ -21,7 +21,7 @@ const SHAKE_DETECTION_WINDOW_MS: f64 = 100.0;
const DECIMATE_FPS: f64 = 60.0;
const DECIMATE_MIN_DIST_UV: f64 = 1.0 / 1920.0;
const SIMULATION_STEP_MS: f64 = 1000.0 / 60.0;
-const SPRING_SETTLE_EXTRA_MS: f64 = 300.0;
+const SPRING_SETTLE_TIMEOUT_MS: f64 = 5000.0;
/// Per-step one-pole coefficient for adapting the phase lead when the active
/// spring profile changes; ~130ms time constant at the 60Hz simulation step,
/// so profile switches ease in without popping the target.
@@ -485,9 +485,9 @@ fn build_smoothed_timeline(
sim.set_target_position(start_pos);
let end_time_ms = moves.last().unwrap().time_ms;
- let settle_end = end_time_ms + SPRING_SETTLE_EXTRA_MS;
+ let settle_deadline = end_time_ms + SPRING_SETTLE_TIMEOUT_MS;
- let capacity = ((settle_end / SIMULATION_STEP_MS).ceil() as usize) + 2;
+ let capacity = ((settle_deadline / SIMULATION_STEP_MS).ceil() as usize) + 2;
let mut events = Vec::with_capacity(capacity);
let mut target_hint: usize = 0;
let mut cid_hint: usize = 0;
@@ -502,7 +502,7 @@ fn build_smoothed_timeline(
let mut t_ms = SIMULATION_STEP_MS;
let mut lead_ms = spring_lag_ms(&smoothing_config);
- while t_ms <= settle_end {
+ while t_ms <= settle_deadline {
let clamped_t = t_ms.min(end_time_ms);
context.advance_to(t_ms);
@@ -539,6 +539,10 @@ fn build_smoothed_timeline(
cursor_id: cid,
});
+ if t_ms >= end_time_ms && sim.is_at_rest() {
+ break;
+ }
+
t_ms += SIMULATION_STEP_MS;
}
@@ -938,6 +942,26 @@ mod tests {
prev = pos;
}
}
+
+ #[test]
+ fn smoothed_cursor_reaches_final_recorded_position() {
+ let cursor = CursorEvents {
+ moves: vec![cursor_move(0.0, 0.1, 0.2), cursor_move(1000.0, 0.9, 0.8)],
+ clicks: vec![],
+ };
+ let smoothing = SpringMassDamperSimulationConfig {
+ tension: 80.0,
+ mass: 2.5,
+ friction: 28.0,
+ };
+
+ let settled = interpolate_cursor(&cursor, 10.0, Some(smoothing)).unwrap();
+
+ assert!((settled.position.coord.x - 0.9).abs() < 1e-6);
+ assert!((settled.position.coord.y - 0.8).abs() < 1e-6);
+ assert_eq!(settled.velocity, XY::new(0.0, 0.0));
+ }
+
fn linear_position_at_time(moves: &[CursorMoveEvent], time_ms: f64) -> (f64, f64) {
if moves.is_empty() {
return (0.0, 0.0);
@@ -997,9 +1021,9 @@ mod tests {
sim.set_target_position(start_pos);
let end_time_ms = moves.last().unwrap().time_ms;
- let settle_end = end_time_ms + SPRING_SETTLE_EXTRA_MS;
+ let settle_deadline = end_time_ms + SPRING_SETTLE_TIMEOUT_MS;
- let capacity = ((settle_end / SIMULATION_STEP_MS).ceil() as usize) + 2;
+ let capacity = ((settle_deadline / SIMULATION_STEP_MS).ceil() as usize) + 2;
let mut events = Vec::with_capacity(capacity);
let mut target_hint: usize = 0;
let mut cid_hint: usize = 0;
@@ -1014,7 +1038,7 @@ mod tests {
let mut t_ms = SIMULATION_STEP_MS;
let mut lead_ms = spring_lag_ms(&smoothing_config);
- while t_ms <= settle_end {
+ while t_ms <= settle_deadline {
let clamped_t = t_ms.min(end_time_ms);
context.advance_to(t_ms);
@@ -1047,6 +1071,10 @@ mod tests {
cursor_id: cid,
});
+ if t_ms >= end_time_ms && sim.is_at_rest() {
+ break;
+ }
+
t_ms += SIMULATION_STEP_MS;
}
diff --git a/crates/rendering/src/decoder/avassetreader.rs b/crates/rendering/src/decoder/avassetreader.rs
index fff82508e..7982571cf 100644
--- a/crates/rendering/src/decoder/avassetreader.rs
+++ b/crates/rendering/src/decoder/avassetreader.rs
@@ -45,7 +45,13 @@ const SEQUENTIAL_FRAME_CACHE_SIZE: usize = 8;
// Paused recordings can contain hundreds of screen/camera pairs. AVAssetReader
// starts decoding eagerly, so only requested tracks may hold native readers.
const MAX_ACTIVE_READERS: usize = 16;
-const IDLE_READER_TIMEOUT: Duration = Duration::from_millis(250);
+// An idle reader keeps its native reader, cache and warm-ahead frames for
+// IDLE_READER_RETAIN, so a hover or Play shortly after a pause continues
+// from them. It releases them sooner, at the next IDLE_READER_POLL, when every
+// reader permit is taken and another track may be waiting for one.
+const IDLE_READER_POLL: Duration = Duration::from_millis(250);
+const IDLE_READER_RETAIN: Duration = Duration::from_secs(5);
+const MAX_CACHED_FRAMES_BELOW_REQUEST: u32 = 10;
fn reader_capacity() -> &'static Arc<Semaphore> {
static CAPACITY: OnceLock<Arc<Semaphore>> = OnceLock::new();
@@ -733,6 +739,7 @@ impl AVAssetReaderDecoder {
}
let mut deferred_requests = VecDeque::<PendingRequest>::new();
+ let mut idle_since = None::<std::time::Instant>;
loop {
let _autorelease_pool = cidre::objc::AutoreleasePoolPage::push();
@@ -758,14 +765,24 @@ impl AVAssetReaderDecoder {
}
} else {
let received = if this.is_some() {
- rx.recv_timeout(IDLE_READER_TIMEOUT)
+ rx.recv_timeout(IDLE_READER_POLL)
} else {
rx.recv().map_err(|_| mpsc::RecvTimeoutError::Disconnected)
};
let message = match received {
- Ok(message) => message,
+ Ok(message) => {
+ idle_since = None;
+ message
+ }
Err(mpsc::RecvTimeoutError::Disconnected) => break,
Err(mpsc::RecvTimeoutError::Timeout) => {
+ let idle = *idle_since.get_or_insert_with(std::time::Instant::now);
+ if idle.elapsed() < IDLE_READER_RETAIN
+ && reader_capacity().available_permits() > 0
+ {
+ continue;
+ }
+ idle_since = None;
cache.clear();
cache_bytes = 0;
*last_sent_frame.borrow_mut() = None;
@@ -1005,6 +1022,7 @@ impl AVAssetReaderDecoder {
let frame_limit =
request_direction.frame_cache_limit(sequential_access.load(Ordering::Relaxed));
let mut exit = false;
+ let mut abandoned = false;
let mut frames_iterated = 0u32;
let mut last_decoded_position: Option<f32> = None;
// Newest vended frame below the fallback floor: after a seek the
@@ -1060,6 +1078,39 @@ impl AVAssetReaderDecoder {
// at-or-before content the reader will ever vend.
if let Some(buf) = frame.image_buf() {
hold_candidate = Some((current_frame, buf.retained()));
+ // A seek decodes from the keyframe before the
+ // request, and scrubbing backwards asks for the
+ // frames just below it next; caching them makes
+ // those requests hits instead of another seek over
+ // the same span. Only the last few are kept: each
+ // pins a buffer of the reader's own pool, and with
+ // dozens outstanding the reader stops vending.
+ if current_frame >= cache_min
+ && current_frame + MAX_CACHED_FRAMES_BELOW_REQUEST
+ >= minimum_fallback_frame
+ {
+ insert_cached_frame(
+ &mut cache,
+ &mut cache_bytes,
+ current_frame,
+ CachedFrame::new(&processor, buf.retained(), current_frame),
+ frame_limit,
+ requested_frame,
+ last_active_frame,
+ |frame| {
+ Some(frame) == vended_before
+ || pinned_frames.contains(&frame)
+ },
+ );
+ }
+ }
+ // Every request in the batch was given up on (a newer
+ // scrub position, a live seek dropping its prefetches):
+ // reading on towards their frames only delays the
+ // request that replaced them.
+ if pending_requests.iter().all(|r| r.sender.is_closed()) {
+ abandoned = true;
+ break;
}
continue;
}
@@ -1082,41 +1133,16 @@ impl AVAssetReaderDecoder {
let too_small_for_cache_bounds = current_frame < cache_min;
if !too_small_for_cache_bounds {
- let frame_bytes = cache_frame.byte_len();
- if let Some(replaced) = cache.remove(&current_frame) {
- cache_bytes = cache_bytes.saturating_sub(replaced.byte_len());
- }
- while !cache.is_empty()
- && frame_cache_needs_eviction(
- cache.len(),
- cache_bytes,
- frame_bytes,
- frame_limit,
- )
- {
- if let Some(last_active) = &last_active_frame {
- let from_oldest = if requested_frame > *last_active {
- true
- } else if requested_frame < *last_active {
- false
- } else {
- current_frame > *cache.keys().next_back().unwrap()
- };
- let victim = cache_eviction_victim(&cache, from_oldest, |frame| {
- Some(frame) == vended_before || pinned_frames.contains(&frame)
- });
- if let Some(removed) = victim.and_then(|frame| cache.remove(&frame))
- {
- cache_bytes = cache_bytes.saturating_sub(removed.byte_len());
- }
- } else {
- cache.clear();
- cache_bytes = 0;
- }
- }
-
- cache_bytes = cache_bytes.saturating_add(frame_bytes);
- cache.insert(current_frame, cache_frame.clone());
+ insert_cached_frame(
+ &mut cache,
+ &mut cache_bytes,
+ current_frame,
+ cache_frame.clone(),
+ frame_limit,
+ requested_frame,
+ last_active_frame,
+ |frame| Some(frame) == vended_before || pinned_frames.contains(&frame),
+ );
let mut remaining_requests = Vec::with_capacity(pending_requests.len());
for req in pending_requests.drain(..) {
@@ -1215,9 +1241,43 @@ impl AVAssetReaderDecoder {
if pending_requests.is_empty() || exit {
break;
}
+ if pending_requests.iter().all(|r| r.sender.is_closed()) {
+ abandoned = true;
+ break;
+ }
}
decoder.is_done = true;
+
+ // A failed reader (a decode error, seen with many decode
+ // sessions on a loaded machine) vends nothing more, and the
+ // newest cached frame is stale for every request still
+ // waiting. Read them again from a fresh reader; when that one
+ // fails too, leave them unanswered so the handle switches to
+ // software decoding instead of showing an older frame.
+ if !pending_requests.is_empty()
+ && let Some(error) = decoder.inner.failure()
+ {
+ tracing::warn!(
+ decoder_idx,
+ requested_frame,
+ %error,
+ "Video reader failed; reading the requests again from a fresh reader"
+ );
+ retry_requests.extend(
+ pending_requests
+ .drain(..)
+ .filter(|request| !request.retried)
+ .map(|request| PendingRequest {
+ retried: true,
+ ..request
+ }),
+ );
+ }
+ }
+
+ if abandoned {
+ pending_requests.clear();
}
for request in retry_requests.into_iter().rev() {
@@ -1413,6 +1473,45 @@ fn answer_misses_previous_vend(
.is_some_and(|expected| answer != Some(expected))
}
+#[allow(clippy::too_many_arguments)]
+fn insert_cached_frame(
+ cache: &mut BTreeMap<u32, CachedFrame>,
+ cache_bytes: &mut usize,
+ number: u32,
+ frame: CachedFrame,
+ frame_limit: usize,
+ requested_frame: u32,
+ last_active_frame: Option<u32>,
+ is_pinned: impl Fn(u32) -> bool,
+) {
+ let frame_bytes = frame.byte_len();
+ if let Some(replaced) = cache.remove(&number) {
+ *cache_bytes = cache_bytes.saturating_sub(replaced.byte_len());
+ }
+ while !cache.is_empty()
+ && frame_cache_needs_eviction(cache.len(), *cache_bytes, frame_bytes, frame_limit)
+ {
+ if let Some(last_active) = last_active_frame {
+ let from_oldest = if requested_frame > last_active {
+ true
+ } else if requested_frame < last_active {
+ false
+ } else {
+ number > *cache.keys().next_back().unwrap()
+ };
+ let victim = cache_eviction_victim(cache, from_oldest, &is_pinned);
+ if let Some(removed) = victim.and_then(|frame| cache.remove(&frame)) {
+ *cache_bytes = cache_bytes.saturating_sub(removed.byte_len());
+ }
+ } else {
+ cache.clear();
+ *cache_bytes = 0;
+ }
+ }
+ *cache_bytes = cache_bytes.saturating_add(frame_bytes);
+ cache.insert(number, frame);
+}
+
// Drops from the end the cache is trimmed from, skipping pinned frames unless
// every frame is pinned.
fn cache_eviction_victim<V>(
diff --git a/crates/rendering/src/decoder/ffmpeg.rs b/crates/rendering/src/decoder/ffmpeg.rs
index 34f49a242..8be75965e 100644
--- a/crates/rendering/src/decoder/ffmpeg.rs
+++ b/crates/rendering/src/decoder/ffmpeg.rs
@@ -1158,7 +1158,9 @@ impl FfmpegDecoder {
);
}
- let decoder_type = if is_hw {
+ // A failed hardware setup falls back to software inside FFmpeg, which
+ // only shows once the first frame has been decoded.
+ let decoder_type = if frames.is_hardware_accelerated() {
DecoderType::FFmpegHardware
} else {
DecoderType::FFmpegSoftware
diff --git a/crates/rendering/src/decoder/mod.rs b/crates/rendering/src/decoder/mod.rs
index 419dc5956..186ad71f1 100644
--- a/crates/rendering/src/decoder/mod.rs
+++ b/crates/rendering/src/decoder/mod.rs
@@ -727,6 +727,10 @@ pub(crate) const EXPORT_STALL_LIMIT: Duration = Duration::from_secs(120);
enum FrameDeadline {
/// Playback: a missed deadline returns no frame so the caller can move on.
Drop,
+ /// The first frame after a seek or for a paused preview. Nothing asks for
+ /// it again, so on macOS a missed deadline switches to software decoding
+ /// at once, which then answers it.
+ Initial,
/// Offline export: a missed deadline keeps waiting for the same reply.
Wait,
}
@@ -754,6 +758,60 @@ struct MacOsDecoderFallback {
path: PathBuf,
fps: u32,
decoder: tokio::sync::OnceCell<Result<AsyncVideoDecoderHandle, String>>,
+ stall: Mutex<PrimaryStall>,
+}
+
+#[cfg(target_os = "macos")]
+#[derive(Default)]
+struct PrimaryStall {
+ timed_out_rounds: u32,
+ last_timeout: Option<tokio::time::Instant>,
+}
+
+#[cfg(target_os = "macos")]
+impl MacOsDecoderFallback {
+ /// A single playback timeout is a busy machine, not a dead reader: only a
+ /// reader that misses this many playback rounds in a row without
+ /// delivering any frame is replaced by software decoding.
+ const MAX_TIMED_OUT_ROUNDS: u32 = 3;
+
+ fn new(name: &'static str, path: PathBuf, fps: u32) -> Self {
+ Self {
+ name,
+ path,
+ fps,
+ decoder: tokio::sync::OnceCell::new(),
+ stall: Mutex::new(PrimaryStall::default()),
+ }
+ }
+
+ fn primary_answered(&self) {
+ *self
+ .stall
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = PrimaryStall::default();
+ }
+
+ /// Playback keeps many requests in flight, and one slow decode times them
+ /// out together; only a request sent after the previous timeout starts a
+ /// new round.
+ fn primary_stalled(&self, sent_at: tokio::time::Instant) -> bool {
+ let mut stall = self
+ .stall
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ if stall.last_timeout.is_none_or(|last| sent_at >= last) {
+ stall.timed_out_rounds += 1;
+ stall.last_timeout = Some(tokio::time::Instant::now());
+ }
+ stall.timed_out_rounds >= Self::MAX_TIMED_OUT_ROUNDS
+ }
+}
+
+enum FrameReply {
+ Frame(DecodedFrame),
+ NoFrame,
+ TimedOut,
}
impl AsyncVideoDecoderHandle {
@@ -789,7 +847,7 @@ impl AsyncVideoDecoderHandle {
Self::INITIAL_SEEK_TIMEOUT_MS,
self.max_fallback_distance
.min(Self::INITIAL_MAX_FALLBACK_DISTANCE),
- FrameDeadline::Drop,
+ FrameDeadline::Initial,
)
.await
.ok()
@@ -836,12 +894,24 @@ impl AsyncVideoDecoderHandle {
Err(_) => Ok(None),
};
}
- let primary = self
- .request_frame(time, timeout_ms, max_fallback_distance, deadline)
- .await;
- if let Ok(Some(frame)) = primary {
- return Ok(Some(frame));
- }
+ let sent_at = tokio::time::Instant::now();
+ let (fallback_reason, primary) = match self
+ .send_frame_request(time, timeout_ms, max_fallback_distance, deadline)
+ .await
+ {
+ Ok(FrameReply::Frame(frame)) => {
+ fallback.primary_answered();
+ return Ok(Some(frame));
+ }
+ Ok(FrameReply::TimedOut)
+ if deadline == FrameDeadline::Drop && !fallback.primary_stalled(sent_at) =>
+ {
+ return Ok(None);
+ }
+ Ok(FrameReply::TimedOut) => ("AVAssetReader stopped answering", Ok(None)),
+ Ok(FrameReply::NoFrame) => ("AVAssetReader returned no frame", Ok(None)),
+ Err(stalled) => ("AVAssetReader stalled", Err(stalled)),
+ };
let result = fallback
.decoder
.get_or_init(|| async {
@@ -858,8 +928,7 @@ impl AsyncVideoDecoderHandle {
false,
)
.await?;
- decoder.status.fallback_reason =
- Some("AVAssetReader returned no frame".to_string());
+ decoder.status.fallback_reason = Some(fallback_reason.to_string());
Ok(decoder)
})
.await;
@@ -886,6 +955,21 @@ impl AsyncVideoDecoderHandle {
max_fallback_distance: u32,
deadline: FrameDeadline,
) -> Result<Option<DecodedFrame>, DecodeStalled> {
+ self.send_frame_request(time, timeout_ms, max_fallback_distance, deadline)
+ .await
+ .map(|reply| match reply {
+ FrameReply::Frame(frame) => Some(frame),
+ FrameReply::NoFrame | FrameReply::TimedOut => None,
+ })
+ }
+
+ async fn send_frame_request(
+ &self,
+ time: f32,
+ timeout_ms: u64,
+ max_fallback_distance: u32,
+ deadline: FrameDeadline,
+ ) -> Result<FrameReply, DecodeStalled> {
let (tx, mut rx) = tokio::sync::oneshot::channel();
let adjusted_time = self.get_time(time);
@@ -902,13 +986,14 @@ impl AsyncVideoDecoderHandle {
time = adjusted_time,
"decoder thread is gone; frame request dropped"
);
- return Ok(None);
+ return Ok(FrameReply::NoFrame);
}
let started = tokio::time::Instant::now();
loop {
match tokio::time::timeout(Duration::from_millis(timeout_ms), &mut rx).await {
- Ok(result) => return Ok(result.ok()),
+ Ok(Ok(frame)) => return Ok(FrameReply::Frame(frame)),
+ Ok(Err(_)) => return Ok(FrameReply::NoFrame),
Err(_) if deadline == FrameDeadline::Wait => {
let waited = started.elapsed();
if waited >= EXPORT_STALL_LIMIT {
@@ -932,7 +1017,7 @@ impl AsyncVideoDecoderHandle {
timeout_ms = timeout_ms,
"Frame decode request timed out"
);
- return Ok(None);
+ return Ok(FrameReply::TimedOut);
}
}
}
@@ -1515,12 +1600,11 @@ pub async fn spawn_decoder(
status,
max_fallback_distance: DEFAULT_MAX_FALLBACK_DISTANCE,
#[cfg(target_os = "macos")]
- fallback: Some(Arc::new(MacOsDecoderFallback {
+ fallback: Some(Arc::new(MacOsDecoderFallback::new(
name,
- path: path.clone(),
+ path.clone(),
fps,
- decoder: tokio::sync::OnceCell::new(),
- })),
+ ))),
#[cfg(target_os = "macos")]
sequential_access: Some(sequential_access),
})
@@ -2448,15 +2532,22 @@ mod managed_worker_tests {
fallback_reason: None,
},
max_fallback_distance: 2,
- fallback: Some(Arc::new(MacOsDecoderFallback {
- name: "screen",
- path: directory.path().to_path_buf(),
- fps: 30,
- decoder: tokio::sync::OnceCell::new(),
- })),
+ fallback: Some(Arc::new(MacOsDecoderFallback::new(
+ "screen",
+ directory.path().to_path_buf(),
+ 30,
+ ))),
sequential_access: None,
};
let cloned = decoder.clone();
+ for _ in 1..MacOsDecoderFallback::MAX_TIMED_OUT_ROUNDS {
+ let (first, second) = tokio::join!(
+ decoder.get_frame_with_timeout(0.0, 200, 2, FrameDeadline::Drop),
+ cloned.get_frame_with_timeout(0.0, 200, 2, FrameDeadline::Drop)
+ );
+ assert!(matches!((first, second), (Ok(None), Ok(None))));
+ assert_eq!(decoder.decoder_type(), DecoderType::AVAssetReader);
+ }
let (first, second) = tokio::join!(
decoder.get_frame_with_timeout(0.0, 200, 2, FrameDeadline::Drop),
cloned.get_frame_with_timeout(0.0, 200, 2, FrameDeadline::Drop)
@@ -2468,14 +2559,20 @@ mod managed_worker_tests {
);
assert_eq!(decoder.decoder_type(), DecoderType::FFmpegSoftware);
assert_eq!(cloned.decoder_type(), DecoderType::FFmpegSoftware);
- assert!(decoder.fallback_reason().is_some());
+ assert_eq!(
+ decoder.fallback_reason(),
+ Some("AVAssetReader stopped answering")
+ );
let (reference, join) = ordinary_worker(directory.path().to_path_buf(), 0.25, false).await;
assert_eq!(
frame_pixels(&first),
frame_pixels(&reference.get_frame_initial(0.0).await.unwrap())
);
let pending_before = receiver.try_iter().count();
- assert_eq!(pending_before, 2);
+ assert_eq!(
+ pending_before,
+ 2 * MacOsDecoderFallback::MAX_TIMED_OUT_ROUNDS as usize
+ );
assert!(decoder.get_frame_initial(0.5).await.is_some());
assert_eq!(receiver.try_iter().count(), 0);
drop(reference);
diff --git a/crates/rendering/src/decoder/model/mod.rs b/crates/rendering/src/decoder/model/mod.rs
index 6e97159a7..d3be6de1c 100644
--- a/crates/rendering/src/decoder/model/mod.rs
+++ b/crates/rendering/src/decoder/model/mod.rs
@@ -1092,7 +1092,244 @@ fn run_suite(decoder: Decoder) {
#[cfg(target_os = "macos")]
#[test]
fn avassetreader_answers_match_the_reference_model() {
+ // Engine verify runs only this test of the module on macOS.
+ avassetreader_answers_stay_exact_when_its_reader_fails();
+ avassetreader_switches_to_software_only_when_its_reader_stops_answering();
run_suite(Decoder::Avf);
+ avassetreader_scrubbing_backwards_reuses_the_frames_below_each_seek();
+ avassetreader_stops_reading_only_once_every_request_is_abandoned();
+}
+
+/// A native reader can fail mid-stream with a decode error and vend nothing
+/// more. Every later answer must still be the frame on screen: read from a
+/// fresh reader, or from software decoding when that one fails too.
+#[cfg(target_os = "macos")]
+#[test]
+fn avassetreader_answers_stay_exact_when_its_reader_fails() {
+ let script = Script {
+ time_base: (1, 30),
+ frames: (0..120)
+ .map(|pts| scripted::ScriptFrame {
+ pts,
+ keyframe: pts % 30 == 0,
+ })
+ .collect(),
+ last_duration: 1,
+ };
+ let runtime = runtime();
+ let patterns: [(&str, Vec<u32>, bool); 2] = [
+ ("playback", (0..90).collect(), false),
+ ("scrub", (0..112).step_by(7).collect(), true),
+ ];
+ for failed_reads in [1, 2] {
+ for (pattern, frames, initial) in &patterns {
+ let registration = scripted::register(script.clone());
+ for _ in 0..failed_reads {
+ registration.fail_avf_read_at(40);
+ }
+ let path = registration.path().to_path_buf();
+ runtime.block_on(async {
+ let handle = spawn_decoder("model", path, 30, 0.0, false)
+ .await
+ .unwrap()
+ .with_max_fallback_distance(2);
+ for &frame in frames {
+ let time = frame as f32 / 30.0;
+ let decoded = if *initial {
+ handle.get_frame_initial(time).await
+ } else {
+ handle.get_frame(time).await
+ };
+ assert_eq!(
+ decoded.as_ref().and_then(frame_identity),
+ Some(frame),
+ "{pattern} with {failed_reads} failed reads at sample 40: request {frame}"
+ );
+ }
+ assert_eq!(
+ handle.decoder_type() == DecoderType::AVAssetReader,
+ failed_reads == 1,
+ "{pattern} with {failed_reads} failed reads ended on {}",
+ handle.decoder_type()
+ );
+ });
+ }
+ }
+}
+
+/// A busy machine can miss one playback deadline without anything being
+/// wrong with the reader: that request gets no frame, and hardware decoding
+/// stays. A reader that answers without a frame, misses every playback
+/// deadline round after round, or misses the deadline of a first frame that
+/// nothing will ask for again is replaced by software decoding.
+#[cfg(target_os = "macos")]
+#[test]
+fn avassetreader_switches_to_software_only_when_its_reader_stops_answering() {
+ use super::{FrameDeadline, MacOsDecoderFallback};
+
+ let script = Script {
+ time_base: (1, 30),
+ frames: (0..120)
+ .map(|pts| scripted::ScriptFrame {
+ pts,
+ keyframe: pts % 30 == 0,
+ })
+ .collect(),
+ last_duration: 1,
+ };
+ let runtime = runtime();
+ let time = |frame: u32| frame as f32 / 30.0;
+ let slow_vend = Duration::from_millis(1500);
+ let deadline_ms = 250;
+
+ let registration = scripted::register(script.clone());
+ let path = registration.path().to_path_buf();
+ runtime.block_on(async {
+ let handle = spawn_decoder("model", path, 30, 0.0, false)
+ .await
+ .unwrap()
+ .with_max_fallback_distance(2);
+ let mut frame = 0;
+ for episode in 0..3 {
+ registration.set_vend_delay(slow_vend);
+ for _ in 1..MacOsDecoderFallback::MAX_TIMED_OUT_ROUNDS {
+ frame += 1;
+ let late = handle
+ .get_frame_with_timeout(time(frame), deadline_ms, 2, FrameDeadline::Drop)
+ .await;
+ assert!(
+ matches!(late, Ok(None)),
+ "slow episode {episode}: request {frame} beat a {deadline_ms} ms deadline"
+ );
+ assert_eq!(handle.decoder_type(), DecoderType::AVAssetReader);
+ }
+ registration.set_vend_delay(Duration::ZERO);
+ frame += 1;
+ assert_eq!(
+ handle
+ .get_frame_initial(time(frame))
+ .await
+ .as_ref()
+ .and_then(frame_identity),
+ Some(frame),
+ "after slow episode {episode}"
+ );
+ assert_eq!(
+ handle.decoder_type(),
+ DecoderType::AVAssetReader,
+ "slow episode {episode} switched to {}",
+ handle.decoder_type()
+ );
+ }
+
+ registration.set_vend_delay(slow_vend);
+ for round in 1..=MacOsDecoderFallback::MAX_TIMED_OUT_ROUNDS {
+ frame += 1;
+ let request =
+ handle.get_frame_with_timeout(time(frame), deadline_ms, 2, FrameDeadline::Drop);
+ if round < MacOsDecoderFallback::MAX_TIMED_OUT_ROUNDS {
+ assert!(
+ matches!(request.await, Ok(None)),
+ "stalled round {round}: request {frame} beat a {deadline_ms} ms deadline"
+ );
+ assert_eq!(handle.decoder_type(), DecoderType::AVAssetReader);
+ continue;
+ }
+ // The reader is still inside the vend it started with the slow
+ // delay in round 1, so lifting the delay now lets only the
+ // software decoder answer this request within its deadline.
+ let lift_delay = async {
+ tokio::time::sleep(Duration::from_millis(deadline_ms / 2)).await;
+ registration.set_vend_delay(Duration::ZERO);
+ };
+ let (answered, ()) = tokio::join!(request, lift_delay);
+ assert_eq!(
+ answered.unwrap().as_ref().and_then(frame_identity),
+ Some(frame),
+ "the request that switched to software decoding"
+ );
+ }
+ assert_eq!(handle.decoder_type(), DecoderType::FFmpegSoftware);
+ assert_eq!(
+ handle.fallback_reason(),
+ Some("AVAssetReader stopped answering")
+ );
+ frame += 1;
+ assert_eq!(
+ handle
+ .get_frame(time(frame))
+ .await
+ .as_ref()
+ .and_then(frame_identity),
+ Some(frame)
+ );
+ });
+
+ let registration = scripted::register(script.clone());
+ let path = registration.path().to_path_buf();
+ runtime.block_on(async {
+ let handle = spawn_decoder("model", path, 30, 0.0, false)
+ .await
+ .unwrap()
+ .with_max_fallback_distance(2);
+ assert_eq!(
+ handle
+ .get_frame_initial(time(5))
+ .await
+ .as_ref()
+ .and_then(frame_identity),
+ Some(5)
+ );
+ registration.set_vend_delay(slow_vend);
+ let late = handle
+ .get_frame_with_timeout(time(6), deadline_ms, 2, FrameDeadline::Drop)
+ .await;
+ assert!(
+ matches!(late, Ok(None)),
+ "request 6 beat a {deadline_ms} ms deadline"
+ );
+ assert_eq!(handle.decoder_type(), DecoderType::AVAssetReader);
+ let seek = handle.get_frame_with_timeout(time(10), deadline_ms, 2, FrameDeadline::Initial);
+ let lift_delay = async {
+ tokio::time::sleep(Duration::from_millis(deadline_ms / 2)).await;
+ registration.set_vend_delay(Duration::ZERO);
+ };
+ let (answered, ()) = tokio::join!(seek, lift_delay);
+ assert_eq!(
+ answered.unwrap().as_ref().and_then(frame_identity),
+ Some(10),
+ "the first timed-out initial request"
+ );
+ assert_eq!(handle.decoder_type(), DecoderType::FFmpegSoftware);
+ assert_eq!(
+ handle.fallback_reason(),
+ Some("AVAssetReader stopped answering")
+ );
+ });
+
+ let registration = scripted::register(script);
+ registration.fail_avf_read_at(40);
+ registration.fail_avf_read_at(40);
+ let path = registration.path().to_path_buf();
+ runtime.block_on(async {
+ let handle = spawn_decoder("model", path, 30, 0.0, false)
+ .await
+ .unwrap()
+ .with_max_fallback_distance(2);
+ assert_eq!(
+ handle
+ .get_frame(time(45))
+ .await
+ .as_ref()
+ .and_then(frame_identity),
+ Some(45)
+ );
+ assert_eq!(handle.decoder_type(), DecoderType::FFmpegSoftware);
+ assert_eq!(
+ handle.fallback_reason(),
+ Some("AVAssetReader returned no frame")
+ );
+ });
}
#[test]
@@ -1141,3 +1378,102 @@ fn ffmpeg_preserves_late_pts_across_playback_and_seeks() {
assert_eq!(outcome.requests, case.requests.len());
}
}
+
+#[cfg(target_os = "macos")]
+#[test]
+fn avassetreader_scrubbing_backwards_reuses_the_frames_below_each_seek() {
+ let script = Script {
+ time_base: (1, 30),
+ frames: (0..120)
+ .map(|pts| scripted::ScriptFrame {
+ pts,
+ keyframe: pts % 30 == 0,
+ })
+ .collect(),
+ last_duration: 1,
+ };
+ let case = Case {
+ name: "backward-scrub".into(),
+ seed_index: 0,
+ seed: 0,
+ decoder: Decoder::Avf,
+ mode: Mode::Playback,
+ fps: 30,
+ max_fallback_distance: 2,
+ script,
+ requests: (0..60)
+ .map(|step| Request {
+ time: (118 - step * 2) as f32 / 30.0,
+ initial: true,
+ })
+ .collect(),
+ };
+ let outcome = run_case(&case, &runtime(), false);
+ assert!(outcome.failures.is_empty(), "{:?}", outcome.failures);
+ assert_eq!(outcome.requests, case.requests.len());
+ assert!(
+ outcome.starts <= 16,
+ "{} reader starts and seeks for 60 backward scrub steps",
+ outcome.starts
+ );
+}
+
+#[cfg(target_os = "macos")]
+#[test]
+fn avassetreader_stops_reading_only_once_every_request_is_abandoned() {
+ let script = Script {
+ time_base: (1, 30),
+ frames: (0..900)
+ .map(|pts| scripted::ScriptFrame {
+ pts,
+ keyframe: pts % 600 == 0,
+ })
+ .collect(),
+ last_duration: 1,
+ };
+ let registration = scripted::register(script);
+ let path = registration.path().to_path_buf();
+ runtime().block_on(async {
+ let handle = spawn_decoder("model", path, 30, 0.0, false)
+ .await
+ .unwrap()
+ .with_max_fallback_distance(2);
+ registration.set_vend_delay(Duration::from_millis(2));
+ registration.take_vended();
+ let time = |frame: u32| frame as f32 / 30.0;
+
+ let abandoned = tokio::time::timeout(
+ Duration::from_millis(40),
+ handle.get_frame_initial(time(550)),
+ );
+ let batch = async {
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ tokio::join!(
+ tokio::time::timeout(
+ Duration::from_millis(150),
+ handle.get_frame_initial(time(760)),
+ ),
+ handle.get_frame_initial(time(765)),
+ )
+ };
+ let (abandoned, (dropped, kept)) = tokio::join!(abandoned, batch);
+
+ assert!(abandoned.is_err(), "frame 550 arrived before 40 ms");
+ assert!(dropped.is_err(), "frame 760 arrived before 160 ms");
+ assert_eq!(
+ kept.as_ref().and_then(frame_identity),
+ Some(765),
+ "the open request of a batch whose other request was dropped"
+ );
+ let read_below_keyframe = registration
+ .take_vended()
+ .iter()
+ .filter(|&&index| index < 600)
+ .count();
+ assert!(
+ read_below_keyframe < 450,
+ "{read_below_keyframe} frames read towards frame 550 after its request was dropped at 40 ms"
+ );
+ assert_eq!(handle.decoder_type(), DecoderType::AVAssetReader);
+ });
+}
diff --git a/crates/rendering/src/frame_pipeline.rs b/crates/rendering/src/frame_pipeline.rs
index 458687ab3..def4d9b9e 100644
--- a/crates/rendering/src/frame_pipeline.rs
+++ b/crates/rendering/src/frame_pipeline.rs
@@ -217,9 +217,7 @@ pub struct RgbaToNv12Converter {
cached_width: u32,
cached_height: u32,
cached_stride: u32,
- cached_bind_groups: Option<[wgpu::BindGroup; 2]>,
- cached_texture_view: Option<wgpu::TextureView>,
- cached_texture_ptr: usize,
+ source_bind_groups: Vec<(wgpu::Texture, wgpu::BindGroup)>,
surface_output: bool,
#[cfg(target_os = "macos")]
surface_ring: Option<Nv12SurfaceRing>,
@@ -568,9 +566,7 @@ impl RgbaToNv12Converter {
cached_width: 0,
cached_height: 0,
cached_stride: 0,
- cached_bind_groups: None,
- cached_texture_view: None,
- cached_texture_ptr: 0,
+ source_bind_groups: Vec::new(),
surface_output: false,
#[cfg(target_os = "macos")]
surface_ring: None,
@@ -704,9 +700,47 @@ impl RgbaToNv12Converter {
self.cached_width = width;
self.cached_height = height;
self.cached_stride = stride;
- self.cached_bind_groups = None;
- self.cached_texture_view = None;
- self.cached_texture_ptr = 0;
+ self.source_bind_groups.clear();
+ }
+
+ fn source_bind_group(
+ &mut self,
+ device: &wgpu::Device,
+ source_texture: &wgpu::Texture,
+ ) -> Option<wgpu::BindGroup> {
+ if let Some((_, bind_group)) = self
+ .source_bind_groups
+ .iter()
+ .find(|(texture, _)| texture == source_texture)
+ {
+ return Some(bind_group.clone());
+ }
+ let nv12_buffer = self.nv12_buffer.as_ref()?;
+ let source_view = source_texture.create_view(&Default::default());
+ let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("RGBA to NV12 Bind Group"),
+ layout: &self.bind_group_layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::TextureView(&source_view),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: nv12_buffer.as_entire_binding(),
+ },
+ wgpu::BindGroupEntry {
+ binding: 2,
+ resource: self.params_buffer.as_entire_binding(),
+ },
+ ],
+ });
+ if self.source_bind_groups.len() >= 2 {
+ self.source_bind_groups.remove(0);
+ }
+ self.source_bind_groups
+ .push((source_texture.clone(), bind_group.clone()));
+ Some(bind_group)
}
#[allow(clippy::too_many_arguments)]
@@ -727,6 +761,9 @@ impl RgbaToNv12Converter {
self.ensure_buffers(device, width, height);
+ let Some(bind_group) = self.source_bind_group(device, source_texture) else {
+ return Ok(false);
+ };
let Some(nv12_buffer) = self.nv12_buffer.as_ref() else {
return Ok(false);
};
@@ -745,51 +782,13 @@ impl RgbaToNv12Converter {
};
queue.write_buffer(&self.params_buffer, 0, bytemuck::cast_slice(&[params]));
- let texture_ptr = source_texture as *const wgpu::Texture as usize;
- let needs_rebind =
- self.cached_texture_ptr != texture_ptr || self.cached_bind_groups.is_none();
-
- if needs_rebind {
- let source_view = source_texture.create_view(&Default::default());
-
- let make_bind_group = |view: &wgpu::TextureView| {
- device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("RGBA to NV12 Bind Group"),
- layout: &self.bind_group_layout,
- entries: &[
- wgpu::BindGroupEntry {
- binding: 0,
- resource: wgpu::BindingResource::TextureView(view),
- },
- wgpu::BindGroupEntry {
- binding: 1,
- resource: nv12_buffer.as_entire_binding(),
- },
- wgpu::BindGroupEntry {
- binding: 2,
- resource: self.params_buffer.as_entire_binding(),
- },
- ],
- })
- };
-
- let bg0 = make_bind_group(&source_view);
- let bg1 = make_bind_group(&source_view);
-
- self.cached_texture_view = Some(source_view);
- self.cached_bind_groups = Some([bg0, bg1]);
- self.cached_texture_ptr = texture_ptr;
- }
-
- let bind_groups = self.cached_bind_groups.as_ref().unwrap();
-
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("RGBA to NV12 Conversion"),
..Default::default()
});
pass.set_pipeline(&self.pipeline);
- pass.set_bind_group(0, &bind_groups[readback_idx], &[]);
+ pass.set_bind_group(0, &bind_group, &[]);
pass.dispatch_workgroups(width.div_ceil(4 * 8), height.div_ceil(2 * 8), 1);
}
@@ -1657,6 +1656,42 @@ fn active_readback_byte_len(
.filter(|&active_bytes| active_bytes > 0 && active_bytes <= mapped_bytes)
}
+fn reserve_readback(
+ data: &mut Vec<u8>,
+ active_bytes: usize,
+ frame_number: u32,
+) -> Result<(), RenderingError> {
+ let allocation_failed = || RenderingError::ReadbackAllocationFailed { frame_number };
+ let capacity = active_bytes.checked_add(24).ok_or_else(allocation_failed)?;
+ #[cfg(test)]
+ let capacity = readback_allocation_failure::capacity(frame_number, capacity);
+ data.try_reserve_exact(capacity)
+ .map_err(|_| allocation_failed())
+}
+
+#[cfg(test)]
+pub(crate) mod readback_allocation_failure {
+ use std::cell::Cell;
+
+ thread_local! {
+ static FRAME: Cell<Option<u32>> = const { Cell::new(None) };
+ }
+
+ pub(crate) fn inject(frame_number: u32) {
+ FRAME.set(Some(frame_number));
+ }
+
+ pub(super) fn capacity(frame_number: u32, capacity: usize) -> usize {
+ if FRAME.get() == Some(frame_number) {
+ // Fail one reservation without exhausting memory; a retry would succeed.
+ FRAME.set(None);
+ usize::MAX
+ } else {
+ capacity
+ }
+ }
+}
+
impl PendingReadback {
fn unmap(&mut self) {
if self.mapped {
@@ -1670,7 +1705,11 @@ impl PendingReadback {
RenderingError::BufferMapWaitingFailed
}
- pub async fn wait(mut self, device: &wgpu::Device) -> Result<RenderedFrame, RenderingError> {
+ async fn wait(
+ mut self,
+ device: &wgpu::Device,
+ recycled: &mut RecycledReadbackFrames,
+ ) -> Result<RenderedFrame, RenderingError> {
let start_time = Instant::now();
let completed = wait_for_submission(
device,
@@ -1713,9 +1752,14 @@ impl PendingReadback {
self.unmap();
return Err(RenderingError::BufferMapWaitingFailed);
};
+ let mut data_vec = if self.export_deadlines {
+ recycled.take().unwrap_or_default()
+ } else {
+ Vec::new()
+ };
+ reserve_readback(&mut data_vec, active_bytes, self.frame_number)?;
let buffer_slice = self.buffer.slice(..active_bytes as u64);
let data = buffer_slice.get_mapped_range();
- let mut data_vec = Vec::with_capacity(active_bytes + 24);
data_vec.extend_from_slice(&data);
drop(data);
@@ -1727,8 +1771,12 @@ impl PendingReadback {
if let Some(phase) = self.readiness_phase.take() {
phase.finish("frame_ready");
}
+ let data = Arc::new(data_vec);
+ if self.export_deadlines {
+ recycled.keep(&data);
+ }
Ok(RenderedFrame {
- data: Arc::new(data_vec),
+ data,
padded_bytes_per_row: self.padded_bytes_per_row,
width: self.width,
height: self.height,
@@ -1744,8 +1792,34 @@ impl Drop for PendingReadback {
}
}
+const MAX_RECYCLED_READBACK_FRAMES: usize = 8;
+
+/// Export readbacks are handed downstream as `Arc<Vec<u8>>`; keeping a clone
+/// lets the next readback reuse a frame once every consumer has dropped it.
+#[derive(Default)]
+struct RecycledReadbackFrames(Vec<Arc<Vec<u8>>>);
+
+impl RecycledReadbackFrames {
+ fn take(&mut self) -> Option<Vec<u8>> {
+ let index = self
+ .0
+ .iter()
+ .position(|data| Arc::strong_count(data) == 1)?;
+ let mut data = Arc::try_unwrap(self.0.swap_remove(index)).ok()?;
+ data.clear();
+ Some(data)
+ }
+
+ fn keep(&mut self, data: &Arc<Vec<u8>>) {
+ if self.0.len() < MAX_RECYCLED_READBACK_FRAMES {
+ self.0.push(Arc::clone(data));
+ }
+ }
+}
+
pub struct PipelinedGpuReadback {
buffers: [Arc<wgpu::Buffer>; 3],
+ recycled_frames: RecycledReadbackFrames,
buffer_size: u64,
current_index: usize,
pending: Option<PendingReadback>,
@@ -1768,6 +1842,7 @@ impl PipelinedGpuReadback {
Self {
buffers: [make_buffer(), make_buffer(), make_buffer()],
+ recycled_frames: RecycledReadbackFrames::default(),
buffer_size: initial_size,
current_index: 0,
pending: None,
@@ -2171,7 +2246,7 @@ pub async fn finish_encoder_timed(
let wait_start = Instant::now();
let previous_frame = if let Some(prev) = previous {
- match prev.wait(device).await {
+ match prev.wait(device, &mut readback.recycled_frames).await {
Ok(frame) => Some(frame),
Err(error) => {
drain_submitted_work(device, queue).await;
@@ -2305,7 +2380,7 @@ pub async fn flush_pending_readback(
) -> Option<Result<RenderedFrame, RenderingError>> {
let readback = session.pipelined_readback.as_mut()?;
if let Some(pending) = readback.take_pending() {
- Some(pending.wait(device).await)
+ Some(pending.wait(device, &mut readback.recycled_frames).await)
} else {
None
}
@@ -2313,7 +2388,83 @@ pub async fn flush_pending_readback(
#[cfg(test)]
mod readback_output_tests {
- use super::active_readback_byte_len;
+ use super::*;
+
+ #[test]
+ fn readback_allocation_reserves_frame_and_metadata_without_changing_length() {
+ let mut data = Vec::new();
+ reserve_readback(&mut data, 8_294_400, 7).expect("readback allocation");
+ assert!(data.is_empty());
+ assert!(data.capacity() >= 8_294_424);
+ }
+
+ #[test]
+ fn readback_allocation_overflow_is_terminal() {
+ for active_bytes in [usize::MAX, usize::MAX - 24] {
+ assert!(matches!(
+ reserve_readback(&mut Vec::new(), active_bytes, 7),
+ Err(RenderingError::ReadbackAllocationFailed { frame_number: 7 })
+ ));
+ }
+ }
+
+ #[tokio::test]
+ async fn readback_allocation_failure_unmaps_the_buffer() {
+ let instance = crate::create_wgpu_instance_sync();
+ let Ok(adapter) = instance
+ .request_adapter(&wgpu::RequestAdapterOptions::default())
+ .await
+ else {
+ eprintln!("No graphics adapter available for readback cleanup test");
+ return;
+ };
+ let (device, queue) = adapter
+ .request_device(&wgpu::DeviceDescriptor::default())
+ .await
+ .expect("graphics device");
+ let buffer = Arc::new(device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("readback allocation cleanup"),
+ size: 256,
+ usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ }));
+ for fail in [true, false] {
+ let submission = queue.submit([]);
+ let (tx, rx) = oneshot::channel();
+ buffer
+ .slice(..)
+ .map_async(wgpu::MapMode::Read, move |result| {
+ tx.send(result).expect("map receiver");
+ });
+ let pending = PendingReadback {
+ readiness_phase: None,
+ rx,
+ submission,
+ mapped: true,
+ buffer: buffer.clone(),
+ padded_bytes_per_row: 256,
+ width: 64,
+ height: 1,
+ frame_number: 7,
+ frame_rate: 30,
+ export_deadlines: false,
+ };
+ if fail {
+ readback_allocation_failure::inject(7);
+ assert!(matches!(
+ pending.wait(&device, &mut Default::default()).await,
+ Err(RenderingError::ReadbackAllocationFailed { frame_number: 7 })
+ ));
+ } else {
+ let frame = pending
+ .wait(&device, &mut Default::default())
+ .await
+ .expect("remapped readback");
+ assert_eq!(frame.data.as_slice(), &[0; 256]);
+ assert!(frame.data.capacity() >= 280);
+ }
+ }
+ }
#[test]
fn oversized_readback_buffers_only_include_active_rows() {
@@ -2362,6 +2513,40 @@ mod readback_output_tests {
}
}
+#[cfg(test)]
+mod recycled_readback_tests {
+ use super::*;
+
+ #[test]
+ fn readback_frames_are_reused_only_after_every_consumer_drops_them() {
+ let mut recycled = RecycledReadbackFrames::default();
+ let frame = Arc::new(vec![7u8; 64]);
+ let address = frame.as_ptr();
+ recycled.keep(&frame);
+ let held = frame.clone();
+ drop(frame);
+ assert!(recycled.take().is_none());
+ drop(held);
+ let reused = recycled.take().expect("released frame is reused");
+ assert!(reused.is_empty());
+ assert!(reused.capacity() >= 64);
+ assert_eq!(reused.as_ptr(), address);
+ assert!(recycled.take().is_none());
+ }
+
+ #[test]
+ fn recycled_readback_frames_are_bounded() {
+ let mut recycled = RecycledReadbackFrames::default();
+ let frames: Vec<_> = (0..MAX_RECYCLED_READBACK_FRAMES + 2)
+ .map(|_| Arc::new(vec![0u8; 4]))
+ .collect();
+ for frame in &frames {
+ recycled.keep(frame);
+ }
+ assert_eq!(recycled.0.len(), MAX_RECYCLED_READBACK_FRAMES);
+ }
+}
+
#[cfg(all(test, target_os = "macos"))]
mod surface_output_tests {
use super::*;
@@ -2527,6 +2712,51 @@ mod surface_output_tests {
/// the readback path maps, so every pixel must match byte-for-byte. Both
/// strides differ (4-aligned vs 256-aligned vs the IOSurface's own), which
/// is exactly what this guards.
+ #[tokio::test]
+ async fn alternating_sources_reuse_their_bind_groups() {
+ let Some((device, queue)) = device() else {
+ eprintln!("no GPU adapter available, skipping");
+ return;
+ };
+ let (width, height) = (64, 32);
+ let first = gradient_texture(&device, &queue, width, height);
+ let second = gradient_texture(&device, &queue, width, height);
+ queue.write_texture(
+ second.as_image_copy(),
+ &[200u8, 40, 90, 255].repeat((width * height) as usize),
+ wgpu::TexelCopyBufferLayout {
+ offset: 0,
+ bytes_per_row: Some(width * 4),
+ rows_per_image: None,
+ },
+ wgpu::Extent3d {
+ width,
+ height,
+ depth_or_array_layers: 1,
+ },
+ );
+ let mut expected = Vec::new();
+ for source in [&first, &second] {
+ let mut converter = RgbaToNv12Converter::new(&device);
+ expected.push(convert(&device, &queue, &mut converter, source, width, height).await);
+ }
+ assert_ne!(expected[0].data.as_ref(), expected[1].data.as_ref());
+
+ let mut converter = RgbaToNv12Converter::new(&device);
+ let mut cached = Vec::new();
+ for round in 0..3 {
+ for (source, expected) in [&first, &second].into_iter().zip(&expected) {
+ let frame = convert(&device, &queue, &mut converter, source, width, height).await;
+ assert_eq!(frame.data.as_ref(), expected.data.as_ref());
+ }
+ if round == 0 {
+ cached = converter.source_bind_groups.clone();
+ }
+ assert_eq!(converter.source_bind_groups.len(), 2);
+ assert!(converter.source_bind_groups == cached);
+ }
+ }
+
#[tokio::test]
async fn the_surface_output_matches_the_readback_output() {
let Some((device, queue)) = device() else {
@@ -2703,7 +2933,7 @@ mod surface_output_tests {
.await;
assert!(surface_a.surface.is_some());
assert!(resized_converter.surface_ring.is_some());
- assert!(resized_converter.cached_bind_groups.is_some());
+ assert!(!resized_converter.source_bind_groups.is_empty());
assert!(resized_converter.nv12_buffer.is_some());
assert!(
resized_converter
@@ -3031,7 +3261,7 @@ mod surface_output_tests {
started.elapsed(),
nv12.surface_ring.as_ref().unwrap().slots.len()
);
- assert_eq!(nv12.surface_ring.as_ref().unwrap().slots.len(), 1);
+ assert!(nv12.surface_ring.as_ref().unwrap().slots.len() <= 2);
let started = Instant::now();
for _ in 0..120 {
drop(convert_bgra(&device, &queue, &mut bgra, &source, 1920, 1080).await);
@@ -3041,6 +3271,6 @@ mod surface_output_tests {
started.elapsed(),
bgra.surface_ring.len()
);
- assert_eq!(bgra.surface_ring.len(), 1);
+ assert!(bgra.surface_ring.len() <= 2);
}
}
diff --git a/crates/rendering/src/layers/background.rs b/crates/rendering/src/layers/background.rs
index 956820cd8..62e3c212f 100644
--- a/crates/rendering/src/layers/background.rs
+++ b/crates/rendering/src/layers/background.rs
@@ -303,11 +303,7 @@ impl From<BackgroundSource> for Background {
noise_scale: noise_scale.unwrap_or(3.0),
}),
BackgroundSource::Image { path } | BackgroundSource::Wallpaper { path } => {
- if let Some(clean_path) = path.as_deref().and_then(clean_background_path) {
- Background::Image { path: clean_path }
- } else {
- Background::Color([1.0, 1.0, 1.0, 1.0])
- }
+ Background::image_or_white(path.as_deref().and_then(clean_background_path))
}
}
}
@@ -324,6 +320,13 @@ fn background_source_is_empty(source: &BackgroundSource) -> bool {
}
impl Background {
+ fn image_or_white(path: Option<String>) -> Self {
+ match path {
+ Some(path) => Background::Image { path },
+ None => Background::Color([1.0, 1.0, 1.0, 1.0]),
+ }
+ }
+
pub fn from_source(source: BackgroundSource, transparent_when_empty: bool) -> Self {
if transparent_when_empty && background_source_is_empty(&source) {
Background::Color([0.0, 0.0, 0.0, 0.0])
@@ -333,6 +336,38 @@ impl Background {
}
}
+/// The last image source path and what it resolved to on disk, so the path
+/// is only cleaned and checked again when the source changes.
+#[derive(Default)]
+struct ResolvedImagePath(Option<(String, Option<String>)>);
+
+impl ResolvedImagePath {
+ fn background(
+ &mut self,
+ source: &BackgroundSource,
+ transparent_when_empty: bool,
+ ) -> Background {
+ let (BackgroundSource::Image { path: Some(path) }
+ | BackgroundSource::Wallpaper { path: Some(path) }) = source
+ else {
+ return Background::from_source(source.clone(), transparent_when_empty);
+ };
+ if path.is_empty() {
+ return Background::from_source(source.clone(), transparent_when_empty);
+ }
+
+ let resolved = match &self.0 {
+ Some((cached, resolved)) if cached == path => resolved.clone(),
+ _ => {
+ let resolved = clean_background_path(path);
+ self.0 = Some((path.clone(), resolved.clone()));
+ resolved
+ }
+ };
+ Background::image_or_white(resolved)
+ }
+}
+
pub enum Inner {
AnimatedGradient(Box<AnimatedGradientLayer>),
Image {
@@ -353,6 +388,7 @@ pub struct BackgroundLayer {
/// Bumped whenever `inner` is replaced, so consumers can cache anything
/// derived from a static background (e.g. its blurred copy).
generation: u64,
+ resolved_image_path: ResolvedImagePath,
image_pipeline: ImageBackgroundPipeline,
color_pipeline: GradientOrColorPipeline,
}
@@ -362,11 +398,21 @@ impl BackgroundLayer {
Self {
inner: None,
generation: 0,
+ resolved_image_path: ResolvedImagePath::default(),
image_pipeline: ImageBackgroundPipeline::new(device),
color_pipeline: GradientOrColorPipeline::new(device),
}
}
+ pub fn background_for(
+ &mut self,
+ source: &BackgroundSource,
+ transparent_when_empty: bool,
+ ) -> Background {
+ self.resolved_image_path
+ .background(source, transparent_when_empty)
+ }
+
pub async fn prepare(
&mut self,
constants: &RenderVideoConstants,
@@ -836,6 +882,49 @@ mod tests {
}
}
+ #[test]
+ fn image_path_is_checked_on_disk_only_when_the_source_changes() {
+ let directory = tempfile::tempdir().unwrap();
+ let first = directory.path().join("first.png");
+ let second = directory.path().join("second.png");
+ std::fs::write(&first, b"").unwrap();
+ std::fs::write(&second, b"").unwrap();
+ let source = |path: &std::path::Path| BackgroundSource::Wallpaper {
+ path: Some(path.to_string_lossy().into_owned()),
+ };
+ let is_image = |background: Background, path: &std::path::Path| match background {
+ Background::Image { path: resolved } => resolved == path.to_string_lossy(),
+ _ => false,
+ };
+
+ let mut resolved = ResolvedImagePath::default();
+ assert!(is_image(
+ resolved.background(&source(&first), false),
+ &first
+ ));
+
+ std::fs::remove_file(&first).unwrap();
+ for _ in 0..3 {
+ assert!(is_image(
+ resolved.background(&source(&first), false),
+ &first
+ ));
+ }
+
+ assert!(is_image(
+ resolved.background(&source(&second), false),
+ &second
+ ));
+ assert!(matches!(
+ resolved.background(&source(&first), false),
+ Background::Color([1.0, 1.0, 1.0, 1.0])
+ ));
+ assert!(matches!(
+ resolved.background(&BackgroundSource::Wallpaper { path: None }, true),
+ Background::Color([0.0, 0.0, 0.0, 0.0])
+ ));
+ }
+
#[test]
fn test_gradient_conversion() {
let source = BackgroundSource::Gradient {
diff --git a/crates/rendering/src/layers/blur.rs b/crates/rendering/src/layers/blur.rs
index e294ecffa..8fff196c4 100644
--- a/crates/rendering/src/layers/blur.rs
+++ b/crates/rendering/src/layers/blur.rs
@@ -1,6 +1,7 @@
use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
+use super::SourceBindGroups;
use crate::ProjectUniforms;
pub struct BlurLayer {
@@ -11,6 +12,7 @@ pub struct BlurLayer {
pipeline: BlurPipeline,
cached_uniforms: Option<BlurUniforms>,
result: Option<(BlurResultKey, wgpu::Texture)>,
+ bind_groups: SourceBindGroups,
}
/// Everything the blurred background depends on. While it matches, the
@@ -53,6 +55,7 @@ impl BlurLayer {
pipeline: BlurPipeline::new(device),
cached_uniforms: None,
result: None,
+ bind_groups: SourceBindGroups::default(),
}
}
@@ -132,39 +135,53 @@ impl BlurLayer {
}
}
+ pub fn retain_views(&mut self, views: [&wgpu::TextureView; 2]) {
+ self.bind_groups.retain_views(views);
+ }
+
+ #[cfg(test)]
+ pub fn bind_groups_created(&self) -> usize {
+ self.bind_groups.created()
+ }
+
pub fn render_h(
- &self,
+ &mut self,
pass: &mut wgpu::RenderPass<'_>,
device: &wgpu::Device,
source_texture: &wgpu::TextureView,
) {
- self.render_pass(pass, device, source_texture, &self.uniforms_buffer_h);
+ self.render_pass(pass, device, source_texture, false);
}
pub fn render_v(
- &self,
+ &mut self,
pass: &mut wgpu::RenderPass<'_>,
device: &wgpu::Device,
source_texture: &wgpu::TextureView,
) {
- self.render_pass(pass, device, source_texture, &self.uniforms_buffer_v);
+ self.render_pass(pass, device, source_texture, true);
}
fn render_pass(
- &self,
+ &mut self,
pass: &mut wgpu::RenderPass<'_>,
device: &wgpu::Device,
source_texture: &wgpu::TextureView,
- uniforms_buffer: &wgpu::Buffer,
+ vertical: bool,
) {
+ let uniforms_buffer = if vertical {
+ &self.uniforms_buffer_v
+ } else {
+ &self.uniforms_buffer_h
+ };
+ let bind_group = self
+ .bind_groups
+ .get_or_create(uniforms_buffer, source_texture, || {
+ self.pipeline
+ .bind_group(device, uniforms_buffer, source_texture, &self.sampler)
+ });
pass.set_pipeline(&self.pipeline.render_pipeline);
- pass.set_bind_group(
- 0,
- &self
- .pipeline
- .bind_group(device, uniforms_buffer, source_texture, &self.sampler),
- &[],
- );
+ pass.set_bind_group(0, bind_group, &[]);
pass.draw(0..4, 0..1);
}
}
diff --git a/crates/rendering/src/layers/camera3d.rs b/crates/rendering/src/layers/camera3d.rs
index bcb9e8110..cd6edcfd9 100644
--- a/crates/rendering/src/layers/camera3d.rs
+++ b/crates/rendering/src/layers/camera3d.rs
@@ -1,6 +1,7 @@
use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
+use super::SourceBindGroups;
use crate::ProjectUniforms;
use crate::camera3d::{CAMERA3D_BLUR_BASE_HEIGHT, camera3d_inverse_homography};
use cap_project::Camera3DBlurMode;
@@ -30,6 +31,7 @@ pub struct Camera3DLayer {
blur_v_buffer: wgpu::Buffer,
blur_pipeline: Camera3DBlurPipeline,
cached_blur_uniforms: Option<(Camera3DBlurUniforms, Camera3DBlurUniforms)>,
+ bind_groups: SourceBindGroups,
}
impl Camera3DLayer {
@@ -64,6 +66,7 @@ impl Camera3DLayer {
blur_v_buffer: make_blur_buffer("Camera3D Blur V Uniform Buffer"),
blur_pipeline: Camera3DBlurPipeline::new(device),
cached_blur_uniforms: None,
+ bind_groups: SourceBindGroups::default(),
}
}
@@ -153,61 +156,74 @@ impl Camera3DLayer {
}
}
+ pub fn retain_views(&mut self, views: [&wgpu::TextureView; 2]) {
+ self.bind_groups.retain_views(views);
+ }
+
+ #[cfg(test)]
+ pub fn bind_groups_created(&self) -> usize {
+ self.bind_groups.created()
+ }
+
pub fn render(
- &self,
+ &mut self,
pass: &mut wgpu::RenderPass<'_>,
device: &wgpu::Device,
content_texture: &wgpu::TextureView,
) {
+ let bind_group =
+ self.bind_groups
+ .get_or_create(&self.warp_uniforms_buffer, content_texture, || {
+ self.warp_pipeline.bind_group(
+ device,
+ &self.warp_uniforms_buffer,
+ content_texture,
+ &self.sampler,
+ )
+ });
pass.set_pipeline(&self.warp_pipeline.render_pipeline);
- pass.set_bind_group(
- 0,
- &self.warp_pipeline.bind_group(
- device,
- &self.warp_uniforms_buffer,
- content_texture,
- &self.sampler,
- ),
- &[],
- );
+ pass.set_bind_group(0, bind_group, &[]);
pass.draw(0..3, 0..1);
}
/// Horizontal gaussian pass (or the single bokeh pass, which uses the V
/// buffer via [`Self::render_blur_v`] instead).
pub fn render_blur_h(
- &self,
+ &mut self,
pass: &mut wgpu::RenderPass<'_>,
device: &wgpu::Device,
source_texture: &wgpu::TextureView,
) {
- self.render_blur(pass, device, &self.blur_h_buffer, source_texture);
+ self.render_blur(pass, device, false, source_texture);
}
pub fn render_blur_v(
- &self,
+ &mut self,
pass: &mut wgpu::RenderPass<'_>,
device: &wgpu::Device,
source_texture: &wgpu::TextureView,
) {
- self.render_blur(pass, device, &self.blur_v_buffer, source_texture);
+ self.render_blur(pass, device, true, source_texture);
}
fn render_blur(
- &self,
+ &mut self,
pass: &mut wgpu::RenderPass<'_>,
device: &wgpu::Device,
- buffer: &wgpu::Buffer,
+ vertical: bool,
source_texture: &wgpu::TextureView,
) {
+ let buffer = if vertical {
+ &self.blur_v_buffer
+ } else {
+ &self.blur_h_buffer
+ };
+ let bind_group = self.bind_groups.get_or_create(buffer, source_texture, || {
+ self.blur_pipeline
+ .bind_group(device, buffer, source_texture, &self.sampler)
+ });
pass.set_pipeline(&self.blur_pipeline.render_pipeline);
- pass.set_bind_group(
- 0,
- &self
- .blur_pipeline
- .bind_group(device, buffer, source_texture, &self.sampler),
- &[],
- );
+ pass.set_bind_group(0, bind_group, &[]);
pass.draw(0..3, 0..1);
}
}
diff --git a/crates/rendering/src/layers/captions.rs b/crates/rendering/src/layers/captions.rs
index 3240e9360..988988060 100644
--- a/crates/rendering/src/layers/captions.rs
+++ b/crates/rendering/src/layers/captions.rs
@@ -1,3 +1,6 @@
+use std::borrow::Cow;
+use std::ops::Range;
+
use bytemuck::{Pod, Zeroable};
use cap_project::{CaptionWord, XY};
use glyphon::cosmic_text::LayoutRunIter;
@@ -261,6 +264,83 @@ fn calculate_word_highlight(
0.0
}
+fn caption_rgba(rgb: [f32; 3], alpha: f32) -> Color {
+ Color::rgba(
+ (rgb[0] * 255.0) as u8,
+ (rgb[1] * 255.0) as u8,
+ (rgb[2] * 255.0) as u8,
+ (alpha * 255.0) as u8,
+ )
+}
+
+struct WordHighlight {
+ base_rgb: [f32; 3],
+ highlight_rgb: [f32; 3],
+ base_alpha: f32,
+ highlight_alpha: f32,
+ transition_duration: f32,
+}
+
+fn word_highlight_spans(
+ text: &str,
+ words: &[CaptionWord],
+ uppercase: bool,
+ current_time: f32,
+ highlight: &WordHighlight,
+) -> Vec<(Range<usize>, Color)> {
+ let base = caption_rgba(highlight.base_rgb, highlight.base_alpha);
+ let mut spans = Vec::with_capacity(words.len() * 2 + 1);
+ let mut last_end = 0usize;
+
+ for (idx, word) in words.iter().enumerate() {
+ let needle = if uppercase {
+ Cow::Owned(word.text.to_uppercase())
+ } else {
+ Cow::Borrowed(word.text.as_str())
+ };
+ let Some(start_pos) = text.get(last_end..).and_then(|s| s.find(needle.as_ref())) else {
+ continue;
+ };
+ let abs_start = last_end + start_pos;
+ if abs_start > last_end {
+ spans.push((last_end..abs_start, base));
+ }
+
+ let word_highlight = calculate_word_highlight(
+ current_time,
+ word,
+ idx,
+ words,
+ highlight.transition_duration,
+ );
+ let blended_rgb: [f32; 3] = std::array::from_fn(|channel| {
+ highlight.base_rgb[channel]
+ + (highlight.highlight_rgb[channel] - highlight.base_rgb[channel]) * word_highlight
+ });
+ let blended_alpha = highlight.base_alpha
+ + (highlight.highlight_alpha - highlight.base_alpha) * word_highlight;
+
+ let word_end = abs_start + needle.len();
+ spans.push((
+ abs_start..word_end,
+ caption_rgba(blended_rgb, blended_alpha),
+ ));
+ last_end = word_end;
+ }
+
+ if last_end < text.len() {
+ spans.push((last_end..text.len(), base));
+ }
+
+ spans
+}
+
+#[derive(PartialEq)]
+enum CaptionTextColors {
+ Uniform(Color),
+ Spans(Vec<(Range<usize>, Color)>),
+}
+
#[derive(PartialEq)]
struct ShapedCaptionText {
text: String,
@@ -268,7 +348,56 @@ struct ShapedCaptionText {
wrap_width: u32,
family: Family<'static>,
weight: Weight,
- color: Color,
+ colors: CaptionTextColors,
+}
+
+fn shape_caption_text(font_system: &mut FontSystem, shaped: &ShapedCaptionText) -> Buffer {
+ let font_size = f32::from_bits(shaped.font_size);
+ let mut buffer = Buffer::new(font_system, Metrics::new(font_size, font_size * 1.2));
+ buffer.set_size(font_system, Some(f32::from_bits(shaped.wrap_width)), None);
+ buffer.set_wrap(font_system, glyphon::Wrap::None);
+ let attrs = Attrs::new().family(shaped.family).weight(shaped.weight);
+ match &shaped.colors {
+ CaptionTextColors::Uniform(color) => buffer.set_text(
+ font_system,
+ &shaped.text,
+ &attrs.color(*color),
+ Shaping::Advanced,
+ ),
+ CaptionTextColors::Spans(spans) => buffer.set_rich_text(
+ font_system,
+ spans
+ .iter()
+ .map(|(range, color)| (&shaped.text[range.clone()], attrs.clone().color(*color))),
+ &attrs,
+ Shaping::Advanced,
+ None,
+ ),
+ }
+ buffer
+}
+
+struct CaptionText {
+ buffer: Buffer,
+ shaped: Option<ShapedCaptionText>,
+}
+
+impl CaptionText {
+ fn new() -> Self {
+ Self {
+ buffer: Buffer::new_empty(Metrics::new(24.0, 24.0 * 1.2)),
+ shaped: None,
+ }
+ }
+
+ fn update(&mut self, font_system: &mut FontSystem, shaped: ShapedCaptionText) -> bool {
+ if self.shaped.as_ref() == Some(&shaped) {
+ return false;
+ }
+ self.buffer = shape_caption_text(font_system, &shaped);
+ self.shaped = Some(shaped);
+ true
+ }
}
pub struct CaptionsLayer {
@@ -277,8 +406,7 @@ pub struct CaptionsLayer {
swash_cache: SwashCache,
text_atlas: TextAtlas,
text_renderer: TextRenderer,
- text_buffer: Buffer,
- shaped: Option<ShapedCaptionText>,
+ text: CaptionText,
current_text: Option<String>,
current_segment_start: f32,
current_segment_end: f32,
@@ -319,9 +447,6 @@ impl CaptionsLayer {
);
glyph_phase.finish("returned");
- let metrics = Metrics::new(24.0, 24.0 * 1.2);
- let text_buffer = Buffer::new_empty(metrics);
-
let background_uniforms = CaptionBackgroundUniforms {
rect: [0.0; 4],
color: [0.0; 4],
@@ -422,8 +547,7 @@ impl CaptionsLayer {
swash_cache,
text_atlas,
text_renderer,
- text_buffer,
- shaped: None,
+ text: CaptionText::new(),
current_text: None,
current_segment_start: 0.0,
current_segment_end: 0.0,
@@ -618,13 +742,6 @@ impl CaptionsLayer {
let font_size = caption_data.settings.size as f32 * (height as f32 / 1080.0);
let wrap_width = (width as f32 - margin * 2.0).max(font_size);
- let new_buffer = |font_system: &mut FontSystem| {
- let mut buffer = Buffer::new(font_system, Metrics::new(font_size, font_size * 1.2));
- buffer.set_size(font_system, Some(wrap_width), None);
- buffer.set_wrap(font_system, glyphon::Wrap::None);
- buffer
- };
-
let font_family = match caption_data.settings.font.as_str() {
"System Serif" => Family::Serif,
"System Monospace" => Family::Monospace,
@@ -642,127 +759,39 @@ impl CaptionsLayer {
let base_alpha = (fade_opacity * BASE_TEXT_OPACITY).clamp(0.0, 1.0);
let highlight_alpha = fade_opacity.clamp(0.0, 1.0);
- if use_color_highlight {
- let mut rich_text: Vec<(&str, Attrs)> = Vec::new();
- let full_text = caption_text.as_str();
- let mut last_end = 0usize;
-
- for (idx, word) in caption_words.iter().enumerate() {
- let needle = if uppercase {
- word.text.to_uppercase()
- } else {
- word.text.clone()
- };
- if let Some(start_pos) = full_text.get(last_end..).and_then(|s| s.find(&needle)) {
- let abs_start = last_end + start_pos;
-
- if abs_start > last_end {
- let space = &full_text[last_end..abs_start];
- rich_text.push((
- space,
- Attrs::new()
- .family(font_family)
- .weight(weight)
- .color(Color::rgba(
- (base_color[0] * 255.0) as u8,
- (base_color[1] * 255.0) as u8,
- (base_color[2] * 255.0) as u8,
- (base_alpha * 255.0) as u8,
- )),
- ));
- }
-
- let word_highlight = calculate_word_highlight(
- current_time as f32,
- word,
- idx,
- caption_words,
- word_transition_duration,
- );
-
- let blended_color = [
- base_color[0] + (highlight_color_rgb[0] - base_color[0]) * word_highlight,
- base_color[1] + (highlight_color_rgb[1] - base_color[1]) * word_highlight,
- base_color[2] + (highlight_color_rgb[2] - base_color[2]) * word_highlight,
- ];
-
- let blended_alpha =
- base_alpha + (highlight_alpha - base_alpha) * word_highlight;
-
- let word_end = abs_start + needle.len();
- rich_text.push((
- &full_text[abs_start..word_end],
- Attrs::new()
- .family(font_family)
- .weight(weight)
- .color(Color::rgba(
- (blended_color[0] * 255.0) as u8,
- (blended_color[1] * 255.0) as u8,
- (blended_color[2] * 255.0) as u8,
- (blended_alpha * 255.0) as u8,
- )),
- ));
- last_end = word_end;
- }
- }
-
- if last_end < full_text.len() {
- rich_text.push((
- &full_text[last_end..],
- Attrs::new()
- .family(font_family)
- .weight(weight)
- .color(Color::rgba(
- (base_color[0] * 255.0) as u8,
- (base_color[1] * 255.0) as u8,
- (base_color[2] * 255.0) as u8,
- (base_alpha * 255.0) as u8,
- )),
- ));
- }
-
- let mut buffer = new_buffer(&mut self.font_system);
- buffer.set_rich_text(
- &mut self.font_system,
- rich_text,
- &Attrs::new().family(font_family).weight(weight),
- Shaping::Advanced,
- None,
- );
- self.text_buffer = buffer;
- self.shaped = None;
+ let colors = if use_color_highlight {
+ CaptionTextColors::Spans(word_highlight_spans(
+ &caption_text,
+ caption_words,
+ uppercase,
+ current_time as f32,
+ &WordHighlight {
+ base_rgb: base_color,
+ highlight_rgb: highlight_color_rgb,
+ base_alpha,
+ highlight_alpha,
+ transition_duration: word_transition_duration,
+ },
+ ))
} else {
- let color = Color::rgba(
- (base_color[0] * 255.0) as u8,
- (base_color[1] * 255.0) as u8,
- (base_color[2] * 255.0) as u8,
- (highlight_alpha * 255.0) as u8,
- );
- let shaped = ShapedCaptionText {
+ CaptionTextColors::Uniform(caption_rgba(base_color, highlight_alpha))
+ };
+ self.text.update(
+ &mut self.font_system,
+ ShapedCaptionText {
text: caption_text,
font_size: font_size.to_bits(),
wrap_width: wrap_width.to_bits(),
family: font_family,
weight,
- color,
- };
- if self.shaped.as_ref() != Some(&shaped) {
- let mut buffer = new_buffer(&mut self.font_system);
- buffer.set_text(
- &mut self.font_system,
- &shaped.text,
- &Attrs::new().family(font_family).weight(weight).color(color),
- Shaping::Advanced,
- );
- self.text_buffer = buffer;
- self.shaped = Some(shaped);
- }
- }
+ colors,
+ },
+ );
let mut layout_width: f32 = 0.0;
let mut layout_height: f32 = 0.0;
let mut highlight_extent: Option<(f32, f32, f32, f32)> = None;
- for run in LayoutRunIter::new(&self.text_buffer) {
+ for run in LayoutRunIter::new(&self.text.buffer) {
layout_width = layout_width.max(run.line_w);
layout_height = layout_height.max(run.line_top + run.line_height);
@@ -902,7 +931,7 @@ impl CaptionsLayer {
for (offset_x, offset_y) in outline_offsets.iter() {
text_areas.push(TextArea {
- buffer: &self.text_buffer,
+ buffer: &self.text.buffer,
left: text_left + offset_x,
top: text_top + offset_y,
scale: render_scale,
@@ -921,7 +950,7 @@ impl CaptionsLayer {
);
text_areas.push(TextArea {
- buffer: &self.text_buffer,
+ buffer: &self.text.buffer,
left: text_left,
top: text_top,
scale: render_scale,
@@ -1288,6 +1317,379 @@ mod tests {
}
}
+#[cfg(test)]
+mod shaping_tests {
+ use cap_project::CaptionWord;
+ use glyphon::{Attrs, Buffer, Color, Family, FontSystem, Metrics, Shaping, Weight};
+
+ use super::{
+ BASE_TEXT_OPACITY, CaptionText, CaptionTextColors, ShapedCaptionText, WordHighlight,
+ calculate_caption_fade, calculate_word_highlight, caption_rgba, word_highlight_spans,
+ };
+
+ const FPS: f32 = 60.0;
+ const FADE: f64 = 0.15;
+
+ fn spoken_words(sentence: &str, start: f32) -> Vec<CaptionWord> {
+ sentence
+ .split_whitespace()
+ .enumerate()
+ .map(|(index, text)| CaptionWord {
+ text: text.to_string(),
+ start: start + index as f32 * 0.3,
+ end: start + index as f32 * 0.3 + 0.25,
+ })
+ .collect()
+ }
+
+ struct Frame<'a> {
+ text: String,
+ words: &'a [CaptionWord],
+ uppercase: bool,
+ current_time: f32,
+ highlight: WordHighlight,
+ font_size: f32,
+ wrap_width: f32,
+ family: Family<'static>,
+ weight: Weight,
+ }
+
+ impl Frame<'_> {
+ fn key(&self) -> ShapedCaptionText {
+ ShapedCaptionText {
+ text: self.text.clone(),
+ font_size: self.font_size.to_bits(),
+ wrap_width: self.wrap_width.to_bits(),
+ family: self.family,
+ weight: self.weight,
+ colors: CaptionTextColors::Spans(word_highlight_spans(
+ &self.text,
+ self.words,
+ self.uppercase,
+ self.current_time,
+ &self.highlight,
+ )),
+ }
+ }
+
+ fn uncached_buffer(&self, font_system: &mut FontSystem) -> Buffer {
+ let WordHighlight {
+ base_rgb: base_color,
+ highlight_rgb: highlight_color_rgb,
+ base_alpha,
+ highlight_alpha,
+ transition_duration: word_transition_duration,
+ } = self.highlight;
+ let (font_family, weight, font_size) = (self.family, self.weight, self.font_size);
+ let mut rich_text: Vec<(&str, Attrs)> = Vec::new();
+ let full_text = self.text.as_str();
+ let mut last_end = 0usize;
+
+ for (idx, word) in self.words.iter().enumerate() {
+ let needle = if self.uppercase {
+ word.text.to_uppercase()
+ } else {
+ word.text.clone()
+ };
+ if let Some(start_pos) = full_text.get(last_end..).and_then(|s| s.find(&needle)) {
+ let abs_start = last_end + start_pos;
+
+ if abs_start > last_end {
+ let space = &full_text[last_end..abs_start];
+ rich_text.push((
+ space,
+ Attrs::new()
+ .family(font_family)
+ .weight(weight)
+ .color(Color::rgba(
+ (base_color[0] * 255.0) as u8,
+ (base_color[1] * 255.0) as u8,
+ (base_color[2] * 255.0) as u8,
+ (base_alpha * 255.0) as u8,
+ )),
+ ));
+ }
+
+ let word_highlight = calculate_word_highlight(
+ self.current_time,
+ word,
+ idx,
+ self.words,
+ word_transition_duration,
+ );
+
+ let blended_color = [
+ base_color[0] + (highlight_color_rgb[0] - base_color[0]) * word_highlight,
+ base_color[1] + (highlight_color_rgb[1] - base_color[1]) * word_highlight,
+ base_color[2] + (highlight_color_rgb[2] - base_color[2]) * word_highlight,
+ ];
+
+ let blended_alpha =
+ base_alpha + (highlight_alpha - base_alpha) * word_highlight;
+
+ let word_end = abs_start + needle.len();
+ rich_text.push((
+ &full_text[abs_start..word_end],
+ Attrs::new()
+ .family(font_family)
+ .weight(weight)
+ .color(Color::rgba(
+ (blended_color[0] * 255.0) as u8,
+ (blended_color[1] * 255.0) as u8,
+ (blended_color[2] * 255.0) as u8,
+ (blended_alpha * 255.0) as u8,
+ )),
+ ));
+ last_end = word_end;
+ }
+ }
+
+ if last_end < full_text.len() {
+ rich_text.push((
+ &full_text[last_end..],
+ Attrs::new()
+ .family(font_family)
+ .weight(weight)
+ .color(Color::rgba(
+ (base_color[0] * 255.0) as u8,
+ (base_color[1] * 255.0) as u8,
+ (base_color[2] * 255.0) as u8,
+ (base_alpha * 255.0) as u8,
+ )),
+ ));
+ }
+
+ let mut buffer = Buffer::new(font_system, Metrics::new(font_size, font_size * 1.2));
+ buffer.set_size(font_system, Some(self.wrap_width), None);
+ buffer.set_wrap(font_system, glyphon::Wrap::None);
+ buffer.set_rich_text(
+ font_system,
+ rich_text,
+ &Attrs::new().family(font_family).weight(weight),
+ Shaping::Advanced,
+ None,
+ );
+ buffer
+ }
+ }
+
+ fn glyphs(buffer: &Buffer) -> Vec<String> {
+ buffer
+ .layout_runs()
+ .flat_map(|run| {
+ std::iter::once(format!(
+ "line {} y {} top {} height {} width {}",
+ run.line_i, run.line_y, run.line_top, run.line_height, run.line_w
+ ))
+ .chain(run.glyphs.iter().map(|glyph| format!("{glyph:?}")))
+ })
+ .collect()
+ }
+
+ fn highlight(
+ highlight_rgb: [f32; 3],
+ transition_duration: f32,
+ fade_opacity: f32,
+ ) -> WordHighlight {
+ WordHighlight {
+ base_rgb: [1.0, 1.0, 1.0],
+ highlight_rgb,
+ base_alpha: (fade_opacity * BASE_TEXT_OPACITY).clamp(0.0, 1.0),
+ highlight_alpha: fade_opacity.clamp(0.0, 1.0),
+ transition_duration,
+ }
+ }
+
+ #[test]
+ fn cached_word_highlight_layout_matches_an_uncached_shape_every_frame() {
+ let mut cached_fonts = super::super::new_font_system();
+ let mut uncached_fonts = super::super::new_font_system();
+ let sentence = "Ship it today, then share the link with Straße ʼn everyone";
+ let words = spoken_words(sentence, 0.3);
+ let start = f64::from(words[0].start);
+ let end = f64::from(words.last().unwrap().end);
+ let variants = [
+ (false, [1.0, 1.0, 1.0], 0.25),
+ (true, [1.0, 0.843, 0.0], 0.1),
+ (false, [0.2, 0.6, 1.0], 0.0),
+ ];
+
+ for (uppercase, highlight_rgb, transition_duration) in variants {
+ let text = if uppercase {
+ sentence.to_uppercase()
+ } else {
+ sentence.to_string()
+ };
+ let mut cache = CaptionText::new();
+ let (mut reshapes, mut visible_frames) = (0, 0);
+ let mut distinct_layouts = Vec::new();
+ for frame in 0..((end + FADE + 0.2) * f64::from(FPS)) as u32 {
+ let current_time = f64::from(frame) / f64::from(FPS);
+ let fade_opacity = calculate_caption_fade(current_time, start, end, FADE);
+ if fade_opacity <= 0.0 {
+ continue;
+ }
+ visible_frames += 1;
+ let width = [1920.0, 1280.0, 640.0][(frame / 45) as usize % 3];
+ let font_size = 48.0;
+ let inputs = Frame {
+ text: text.clone(),
+ words: &words,
+ uppercase,
+ current_time: current_time as f32,
+ highlight: highlight(highlight_rgb, transition_duration, fade_opacity),
+ font_size,
+ wrap_width: (width - width * 0.1_f32).max(font_size),
+ family: Family::SansSerif,
+ weight: Weight::BOLD,
+ };
+
+ reshapes += usize::from(cache.update(&mut cached_fonts, inputs.key()));
+ let expected = glyphs(&inputs.uncached_buffer(&mut uncached_fonts));
+ assert_eq!(
+ glyphs(&cache.buffer),
+ expected,
+ "uppercase {uppercase}, transition {transition_duration}, time {current_time}"
+ );
+ if !distinct_layouts.contains(&expected) {
+ distinct_layouts.push(expected);
+ }
+ }
+
+ assert!(distinct_layouts.len() > 2);
+ assert!(reshapes >= distinct_layouts.len());
+ assert!(
+ reshapes < visible_frames,
+ "{reshapes} reshapes for {visible_frames} frames"
+ );
+ }
+ }
+
+ #[test]
+ fn word_highlight_cache_reshapes_when_any_layout_input_changes() {
+ let mut cached_fonts = super::super::new_font_system();
+ let mut uncached_fonts = super::super::new_font_system();
+ let sentence = "Ship it today then share the link";
+ let words = spoken_words(sentence, 0.0);
+ let other_words = spoken_words("Ship it now then share the link", 0.0);
+ let base = || Frame {
+ text: sentence.to_string(),
+ words: &words,
+ uppercase: false,
+ current_time: 0.35,
+ highlight: highlight([1.0, 0.843, 0.0], 0.25, 1.0),
+ font_size: 48.0,
+ wrap_width: 1728.0,
+ family: Family::SansSerif,
+ weight: Weight::BOLD,
+ };
+ let mutations = [
+ (
+ "text",
+ Frame {
+ text: "Ship it now then share the link".to_string(),
+ words: &other_words,
+ ..base()
+ },
+ ),
+ (
+ "uppercase",
+ Frame {
+ text: sentence.to_uppercase(),
+ uppercase: true,
+ ..base()
+ },
+ ),
+ (
+ "font size",
+ Frame {
+ font_size: 60.0,
+ ..base()
+ },
+ ),
+ (
+ "wrap width",
+ Frame {
+ wrap_width: 1152.0,
+ ..base()
+ },
+ ),
+ (
+ "font family",
+ Frame {
+ family: Family::Monospace,
+ ..base()
+ },
+ ),
+ (
+ "font weight",
+ Frame {
+ weight: Weight::NORMAL,
+ ..base()
+ },
+ ),
+ (
+ "highlight color",
+ Frame {
+ highlight: highlight([0.0, 1.0, 0.0], 0.25, 1.0),
+ ..base()
+ },
+ ),
+ (
+ "transition duration",
+ Frame {
+ highlight: highlight([1.0, 0.843, 0.0], 0.5, 1.0),
+ ..base()
+ },
+ ),
+ (
+ "active word",
+ Frame {
+ current_time: 0.65,
+ ..base()
+ },
+ ),
+ (
+ "caption fade",
+ Frame {
+ highlight: highlight([1.0, 0.843, 0.0], 0.25, 0.5),
+ ..base()
+ },
+ ),
+ ];
+
+ for (name, changed) in mutations {
+ let mut cache = CaptionText::new();
+ assert!(cache.update(&mut cached_fonts, base().key()));
+ assert!(
+ !cache.update(&mut cached_fonts, base().key()),
+ "{name}: identical inputs reshaped"
+ );
+ assert!(
+ cache.update(&mut cached_fonts, changed.key()),
+ "{name}: the cached layout was reused"
+ );
+ assert_eq!(
+ glyphs(&cache.buffer),
+ glyphs(&changed.uncached_buffer(&mut uncached_fonts)),
+ "{name}"
+ );
+ }
+
+ let mut cache = CaptionText::new();
+ let plain = |color: Color| ShapedCaptionText {
+ colors: CaptionTextColors::Uniform(color),
+ ..base().key()
+ };
+ let white = caption_rgba([1.0, 1.0, 1.0], 1.0);
+ assert!(cache.update(&mut cached_fonts, plain(white)));
+ assert!(!cache.update(&mut cached_fonts, plain(white)));
+ assert!(cache.update(&mut cached_fonts, base().key()));
+ assert!(cache.update(&mut cached_fonts, plain(white)));
+ assert!(cache.update(&mut cached_fonts, plain(caption_rgba([1.0, 1.0, 1.0], 0.5))));
+ }
+}
+
#[cfg(test)]
mod gpu_tests {
use cap_project::{
@@ -1521,6 +1923,21 @@ mod gpu_tests {
},
CaptionSettings {
active_word_highlight: true,
+ ..enabled.clone()
+ },
+ CaptionSettings {
+ active_word_highlight: true,
+ highlight_color: "#FFD700".to_string(),
+ outline: true,
+ uppercase: true,
+ animation: "pop".to_string(),
+ ..enabled.clone()
+ },
+ CaptionSettings {
+ active_word_highlight: true,
+ highlight_color: "#3399FF".to_string(),
+ word_transition_duration: 0.0,
+ fade_duration: 0.4,
..enabled
},
];
@@ -1632,6 +2049,81 @@ mod gpu_tests {
plain.clone(),
base_size,
),
+ (
+ "highlighted font size",
+ color_highlight.clone(),
+ CaptionSettings {
+ size: 80,
+ ..color_highlight.clone()
+ },
+ base_size,
+ ),
+ (
+ "highlighted font family",
+ color_highlight.clone(),
+ CaptionSettings {
+ font: "System Monospace".to_string(),
+ ..color_highlight.clone()
+ },
+ base_size,
+ ),
+ (
+ "highlighted font weight",
+ color_highlight.clone(),
+ CaptionSettings {
+ font_weight: 400,
+ ..color_highlight.clone()
+ },
+ base_size,
+ ),
+ (
+ "highlighted uppercase",
+ color_highlight.clone(),
+ CaptionSettings {
+ uppercase: true,
+ ..color_highlight.clone()
+ },
+ base_size,
+ ),
+ (
+ "highlight color",
+ color_highlight.clone(),
+ CaptionSettings {
+ highlight_color: "#00FF00".to_string(),
+ ..color_highlight.clone()
+ },
+ base_size,
+ ),
+ (
+ "base color under highlight",
+ color_highlight.clone(),
+ CaptionSettings {
+ color: "#00FFFF".to_string(),
+ ..color_highlight.clone()
+ },
+ base_size,
+ ),
+ (
+ "word transition",
+ color_highlight.clone(),
+ CaptionSettings {
+ word_transition_duration: 0.0,
+ ..color_highlight.clone()
+ },
+ base_size,
+ ),
+ (
+ "highlighted output height",
+ color_highlight.clone(),
+ color_highlight.clone(),
+ XY::new(512, 216),
+ ),
+ (
+ "highlighted output width",
+ color_highlight.clone(),
+ color_highlight.clone(),
+ XY::new(384, 288),
+ ),
];
for (name, before, after, size) in cases {
@@ -1669,6 +2161,42 @@ mod gpu_tests {
);
}
+ #[test]
+ #[ignore = "requires a working wgpu adapter"]
+ fn highlighted_frames_follow_output_size_changes_like_a_fresh_layer() {
+ let sizes = [XY::new(512, 288), XY::new(384, 288), XY::new(512, 216)];
+ let constants = constants(sizes[0]);
+ let segments = spoken_segments(&["Hello there", "Ship it today"]);
+ let settings = CaptionSettings {
+ enabled: true,
+ size: 60,
+ active_word_highlight: true,
+ highlight_color: "#FFD700".to_string(),
+ ..Default::default()
+ };
+ let last_frame = ((segments.last().unwrap().end + 0.3) * f64::from(FPS)) as u32;
+ let mut reused = CaptionsLayer::new(&constants.device, &constants.queue);
+ let mut drawn_frames = 0;
+ for frame in 0..last_frame {
+ let size = sizes[(frame / 7) as usize % sizes.len()];
+ let decoded = frames(size);
+ let mut uniforms = uniforms(&constants, settings.clone(), &segments, size);
+ uniforms.frame_number = frame;
+ reused.prepare(&uniforms, &decoded, size, &constants);
+ let mut fresh = CaptionsLayer::new(&constants.device, &constants.queue);
+ fresh.prepare(&uniforms, &decoded, size, &constants);
+ assert_eq!(reused.has_caption, fresh.has_caption, "frame {frame}");
+ let pixels = render_pixels(&constants, &reused, size);
+ assert_eq!(
+ pixels,
+ render_pixels(&constants, &fresh, size),
+ "frame {frame}"
+ );
+ drawn_frames += usize::from(pixels.chunks_exact(4).any(|pixel| pixel[3] != 0));
+ }
+ assert!(drawn_frames > 60);
+ }
+
#[test]
#[ignore = "benchmark; requires a working wgpu adapter"]
fn benchmark_captions_layer_prepare() {
@@ -1737,4 +2265,105 @@ mod gpu_tests {
);
}
}
+
+ fn pixel_digest(digest: u64, pixels: &[u8]) -> u64 {
+ pixels.chunks_exact(8).fold(digest, |hash, chunk| {
+ (hash ^ u64::from_le_bytes(chunk.try_into().unwrap())).wrapping_mul(0x0100_0000_01b3)
+ })
+ }
+
+ #[test]
+ #[ignore = "benchmark; requires a working wgpu adapter"]
+ fn benchmark_word_highlight_prepare() {
+ const FRAMES: u32 = 600;
+ const PASSES: usize = 5;
+ let size = XY::new(1920, 1080);
+ let constants = constants(size);
+ let frames = frames(size);
+ let spaced = spoken_segments(&[
+ "Today I want to walk you through the new export flow",
+ "First pick a preset and the resolution you need",
+ "Then trim the start and end so the video gets to the point",
+ "When you are happy with it press export and share the link",
+ ]);
+ let mut back_to_back = spaced.clone();
+ for segment in &mut back_to_back {
+ let next_starts = segment
+ .words
+ .iter()
+ .skip(1)
+ .map(|word| word.start)
+ .collect::<Vec<_>>();
+ for (word, next_start) in segment.words.iter_mut().zip(next_starts) {
+ word.end = next_start;
+ }
+ }
+ let base = CaptionSettings {
+ enabled: true,
+ ..Default::default()
+ };
+ let highlight = CaptionSettings {
+ active_word_highlight: true,
+ ..base.clone()
+ };
+ let cases = [
+ ("plain", base, &spaced),
+ ("color highlight", highlight.clone(), &spaced),
+ (
+ "color highlight gold outline",
+ CaptionSettings {
+ highlight_color: "#FFD700".to_string(),
+ outline: true,
+ ..highlight.clone()
+ },
+ &spaced,
+ ),
+ (
+ "color highlight back-to-back words",
+ highlight,
+ &back_to_back,
+ ),
+ ];
+ for (name, settings, segments) in cases {
+ let mut uniforms = uniforms(&constants, settings, segments, size);
+ let mut layer = CaptionsLayer::new(&constants.device, &constants.queue);
+ let mut samples = Vec::new();
+ let mut pass_totals = Vec::new();
+ for pass in 0..PASSES {
+ let mut pass_total = 0.0;
+ for frame in 0..FRAMES {
+ uniforms.frame_number = frame;
+ let started = std::time::Instant::now();
+ layer.prepare(&uniforms, &frames, size, &constants);
+ let elapsed = started.elapsed().as_secs_f64() * 1e6;
+ pass_total += elapsed;
+ if pass > 0 && layer.has_caption {
+ samples.push(elapsed);
+ }
+ }
+ if pass > 0 {
+ pass_totals.push(pass_total / 1e3);
+ }
+ constants.device.poll(wgpu::PollType::Wait).unwrap();
+ }
+ samples.sort_by(f64::total_cmp);
+ pass_totals.sort_by(f64::total_cmp);
+
+ let mut checked = CaptionsLayer::new(&constants.device, &constants.queue);
+ let mut digest = 0xcbf2_9ce4_8422_2325;
+ for frame in 0..FRAMES {
+ uniforms.frame_number = frame;
+ checked.prepare(&uniforms, &frames, size, &constants);
+ digest = pixel_digest(digest, &render_pixels(&constants, &checked, size));
+ }
+
+ println!(
+ "word highlight prepare [{name}] ({FRAMES} frames, {} visible samples): p50 {:.1} us, p95 {:.1} us, {FRAMES}-frame total p50 {:.2} ms, pixels {digest:016x}",
+ samples.len(),
+ samples[samples.len() / 2],
+ samples[samples.len() * 95 / 100],
+ pass_totals[pass_totals.len() / 2],
+ );
+ }
+ }
}
diff --git a/crates/rendering/src/layers/color_grade.rs b/crates/rendering/src/layers/color_grade.rs
index 5ef1942f4..10d60708a 100644
--- a/crates/rendering/src/layers/color_grade.rs
+++ b/crates/rendering/src/layers/color_grade.rs
@@ -1,6 +1,7 @@
use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
+use super::SourceBindGroups;
use crate::ProjectUniforms;
/// Full-frame pass applying the screen grade to the background canvas so the
@@ -9,6 +10,7 @@ pub struct ColorGradeLayer {
active: bool,
uniforms_buffer: wgpu::Buffer,
pipeline: ColorGradePipeline,
+ bind_groups: SourceBindGroups,
}
impl ColorGradeLayer {
@@ -21,6 +23,7 @@ impl ColorGradeLayer {
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
}),
pipeline: ColorGradePipeline::new(device),
+ bind_groups: SourceBindGroups::default(),
}
}
@@ -48,20 +51,29 @@ impl ColorGradeLayer {
);
}
+ pub fn retain_views(&mut self, views: [&wgpu::TextureView; 2]) {
+ self.bind_groups.retain_views(views);
+ }
+
+ #[cfg(test)]
+ pub fn bind_groups_created(&self) -> usize {
+ self.bind_groups.created()
+ }
+
pub fn render(
- &self,
+ &mut self,
pass: &mut wgpu::RenderPass<'_>,
device: &wgpu::Device,
source_texture: &wgpu::TextureView,
) {
+ let bind_group =
+ self.bind_groups
+ .get_or_create(&self.uniforms_buffer, source_texture, || {
+ self.pipeline
+ .bind_group(device, &self.uniforms_buffer, source_texture)
+ });
pass.set_pipeline(&self.pipeline.render_pipeline);
- pass.set_bind_group(
- 0,
- &self
- .pipeline
- .bind_group(device, &self.uniforms_buffer, source_texture),
- &[],
- );
+ pass.set_bind_group(0, bind_group, &[]);
pass.draw(0..3, 0..1);
}
}
diff --git a/crates/rendering/src/layers/cursor.rs b/crates/rendering/src/layers/cursor.rs
index 25f5a5c3a..10c06b999 100644
--- a/crates/rendering/src/layers/cursor.rs
+++ b/crates/rendering/src/layers/cursor.rs
@@ -56,6 +56,7 @@ fn recorded_image_shape(image: &image::RgbaImage) -> Option<cap_cursor_info::Cur
pub struct CursorLayer {
statics: Statics,
bind_group: Option<BindGroup>,
+ texture_bind_group: Option<(wgpu::Texture, BindGroup)>,
cursors: HashMap<String, CursorTexture>,
circle_cursor: Option<CursorTexture>,
prev_is_svg_assets_enabled: Option<bool>,
@@ -216,6 +217,7 @@ impl CursorLayer {
Self {
statics,
bind_group: None,
+ texture_bind_group: None,
cursors: Default::default(),
circle_cursor: None,
prev_is_svg_assets_enabled: None,
@@ -453,6 +455,7 @@ impl CursorLayer {
let time_s = segment_frames.recording_time;
let Some(interpolated_cursor) = &uniforms.interpolated_cursor else {
+ self.bind_group = None;
return;
};
@@ -563,6 +566,7 @@ impl CursorLayer {
}
let Some(tex) = self.cursors.get(&interpolated_cursor.cursor_id) else {
error!("Cursor {:?} not found!", interpolated_cursor.cursor_id);
+ self.bind_group = None;
return;
};
tex
@@ -651,10 +655,26 @@ impl CursorLayer {
bytemuck::cast_slice(&[cursor_uniforms]),
);
- self.bind_group = Some(
- self.statics
- .create_bind_group(&constants.device, &cursor_texture.texture),
- );
+ let texture = &cursor_texture.texture;
+ if self
+ .texture_bind_group
+ .as_ref()
+ .is_none_or(|(cached, _)| cached != texture)
+ {
+ self.texture_bind_group = Some((
+ texture.clone(),
+ self.statics.create_bind_group(&constants.device, texture),
+ ));
+ }
+ self.bind_group = self
+ .texture_bind_group
+ .as_ref()
+ .map(|(_, bind_group)| bind_group.clone());
+ }
+
+ #[cfg(test)]
+ pub fn bind_group(&self) -> Option<&BindGroup> {
+ self.bind_group.as_ref()
}
pub fn render(&self, pass: &mut wgpu::RenderPass<'_>) {
diff --git a/crates/rendering/src/layers/mask.rs b/crates/rendering/src/layers/mask.rs
index 2d3a9dda7..16684f34f 100644
--- a/crates/rendering/src/layers/mask.rs
+++ b/crates/rendering/src/layers/mask.rs
@@ -1,6 +1,7 @@
use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
+use super::SourceBindGroups;
use crate::{MaskRenderMode, PreparedMask, RenderSession};
const PIXELATE_MODE: u32 = 0;
@@ -11,12 +12,17 @@ const BLUR_VERTICAL_MODE: u32 = 3;
pub struct MaskLayer {
sampler: wgpu::Sampler,
pipeline: MaskPipeline,
+ /// One uniform buffer per mask pass in a frame. Every pass of a frame is
+ /// encoded before it is submitted, so passes cannot share a buffer.
+ uniform_buffers: Vec<(wgpu::Buffer, MaskUniforms)>,
+ next_uniform_buffer: usize,
+ bind_groups: SourceBindGroups,
}
struct MaskPass<'a> {
source_texture_view: &'a wgpu::TextureView,
target_texture_view: &'a wgpu::TextureView,
- render_pipeline: &'a wgpu::RenderPipeline,
+ blur_composite: bool,
load: wgpu::LoadOp<wgpu::Color>,
uniforms: MaskUniforms,
}
@@ -34,13 +40,31 @@ impl MaskLayer {
..Default::default()
}),
pipeline: MaskPipeline::new(device),
+ uniform_buffers: Vec::new(),
+ next_uniform_buffer: 0,
+ bind_groups: SourceBindGroups::default(),
}
}
+ pub fn begin_frame(&mut self, views: [&wgpu::TextureView; 2]) {
+ self.next_uniform_buffer = 0;
+ self.bind_groups.retain_views(views);
+ }
+
+ #[cfg(test)]
+ pub fn bind_groups_created(&self) -> usize {
+ self.bind_groups.created()
+ }
+
+ #[cfg(test)]
+ pub fn uniform_buffer_count(&self) -> usize {
+ self.uniform_buffers.len()
+ }
+
pub fn render(
- &self,
+ &mut self,
device: &wgpu::Device,
- _queue: &wgpu::Queue,
+ queue: &wgpu::Queue,
session: &mut RenderSession,
encoder: &mut wgpu::CommandEncoder,
mask: &PreparedMask,
@@ -49,39 +73,42 @@ impl MaskLayer {
MaskRenderMode::Blur => {
self.render_pass(
device,
+ queue,
encoder,
MaskPass {
source_texture_view: session.current_texture_view(),
target_texture_view: session.other_texture_view(),
- render_pipeline: &self.pipeline.render_pipeline,
+ blur_composite: false,
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
uniforms: MaskUniforms::from_mask(mask, BLUR_HORIZONTAL_MODE),
},
);
self.render_pass(
device,
+ queue,
encoder,
MaskPass {
source_texture_view: session.other_texture_view(),
target_texture_view: session.current_texture_view(),
- render_pipeline: &self.pipeline.blur_composite_pipeline,
+ blur_composite: true,
load: wgpu::LoadOp::Load,
uniforms: MaskUniforms::from_mask(mask, BLUR_VERTICAL_MODE),
},
);
}
MaskRenderMode::Pixelate => {
- self.render_single_pass(device, session, encoder, mask, PIXELATE_MODE);
+ self.render_single_pass(device, queue, session, encoder, mask, PIXELATE_MODE);
}
MaskRenderMode::Highlight => {
- self.render_single_pass(device, session, encoder, mask, HIGHLIGHT_MODE);
+ self.render_single_pass(device, queue, session, encoder, mask, HIGHLIGHT_MODE);
}
}
}
fn render_single_pass(
- &self,
+ &mut self,
device: &wgpu::Device,
+ queue: &wgpu::Queue,
session: &mut RenderSession,
encoder: &mut wgpu::CommandEncoder,
mask: &PreparedMask,
@@ -89,11 +116,12 @@ impl MaskLayer {
) {
self.render_pass(
device,
+ queue,
encoder,
MaskPass {
source_texture_view: session.current_texture_view(),
target_texture_view: session.other_texture_view(),
- render_pipeline: &self.pipeline.render_pipeline,
+ blur_composite: false,
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
uniforms: MaskUniforms::from_mask(mask, mode),
},
@@ -102,23 +130,42 @@ impl MaskLayer {
}
fn render_pass(
- &self,
+ &mut self,
device: &wgpu::Device,
+ queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
config: MaskPass<'_>,
) {
- let uniforms_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
- label: Some("Mask Uniform Buffer"),
- contents: bytemuck::cast_slice(&[config.uniforms]),
- usage: wgpu::BufferUsages::UNIFORM,
- });
-
- let bind_group = self.pipeline.bind_group(
- device,
- &uniforms_buffer,
- config.source_texture_view,
- &self.sampler,
- );
+ let index = self.next_uniform_buffer;
+ self.next_uniform_buffer += 1;
+ match self.uniform_buffers.get_mut(index) {
+ Some((buffer, written)) => {
+ if *written != config.uniforms {
+ queue.write_buffer(buffer, 0, bytemuck::cast_slice(&[config.uniforms]));
+ *written = config.uniforms;
+ }
+ }
+ None => self.uniform_buffers.push((
+ device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
+ label: Some("Mask Uniform Buffer"),
+ contents: bytemuck::cast_slice(&[config.uniforms]),
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ }),
+ config.uniforms,
+ )),
+ }
+ let uniforms_buffer = &self.uniform_buffers[index].0;
+
+ let bind_group =
+ self.bind_groups
+ .get_or_create(uniforms_buffer, config.source_texture_view, || {
+ self.pipeline.bind_group(
+ device,
+ uniforms_buffer,
+ config.source_texture_view,
+ &self.sampler,
+ )
+ });
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Mask Pass"),
@@ -135,8 +182,12 @@ impl MaskLayer {
occlusion_query_set: None,
});
- pass.set_pipeline(config.render_pipeline);
- pass.set_bind_group(0, &bind_group, &[]);
+ pass.set_pipeline(if config.blur_composite {
+ &self.pipeline.blur_composite_pipeline
+ } else {
+ &self.pipeline.render_pipeline
+ });
+ pass.set_bind_group(0, bind_group, &[]);
config.uniforms.draw(&mut pass);
drop(pass);
diff --git a/crates/rendering/src/layers/mod.rs b/crates/rendering/src/layers/mod.rs
index 91988f762..9143787bd 100644
--- a/crates/rendering/src/layers/mod.rs
+++ b/crates/rendering/src/layers/mod.rs
@@ -76,6 +76,54 @@ pub fn new_font_system() -> glyphon::FontSystem {
result
}
+/// Bind groups for full-frame passes that sample one of the render session's
+/// two textures. Each is keyed by the uniform buffer and source view it binds,
+/// so it is built once per pairing instead of once per frame.
+#[derive(Default)]
+pub struct SourceBindGroups {
+ entries: Vec<(wgpu::Buffer, wgpu::TextureView, wgpu::BindGroup)>,
+ #[cfg(test)]
+ created: usize,
+}
+
+impl SourceBindGroups {
+ pub fn get_or_create(
+ &mut self,
+ buffer: &wgpu::Buffer,
+ view: &wgpu::TextureView,
+ create: impl FnOnce() -> wgpu::BindGroup,
+ ) -> &wgpu::BindGroup {
+ let index = match self
+ .entries
+ .iter()
+ .position(|(cached_buffer, cached_view, _)| {
+ cached_buffer == buffer && cached_view == view
+ }) {
+ Some(index) => index,
+ None => {
+ #[cfg(test)]
+ {
+ self.created += 1;
+ }
+ self.entries.push((buffer.clone(), view.clone(), create()));
+ self.entries.len() - 1
+ }
+ };
+ &self.entries[index].2
+ }
+
+ /// Drops entries for views the session no longer owns, so a resized
+ /// session's old textures are released.
+ pub fn retain_views(&mut self, views: [&wgpu::TextureView; 2]) {
+ self.entries.retain(|(_, view, _)| views.contains(&view));
+ }
+
+ #[cfg(test)]
+ pub fn created(&self) -> usize {
+ self.created
+ }
+}
+
pub use animated_gradient::*;
pub use background::*;
pub use blur::*;
diff --git a/crates/rendering/src/layers/text.rs b/crates/rendering/src/layers/text.rs
index fe4e40ed5..4a368f71c 100644
--- a/crates/rendering/src/layers/text.rs
+++ b/crates/rendering/src/layers/text.rs
@@ -1054,6 +1054,85 @@ mod gpu_tests {
assert_eq!(after_edit, fresh(&frame));
}
+ #[test]
+ #[ignore = "requires a working wgpu adapter"]
+ fn reused_layouts_follow_each_shaping_input() {
+ type Edit = (&'static str, fn(&mut PreparedText));
+ let (device, queue) = device().expect("A working wgpu adapter is required for this test");
+ let base = PreparedText {
+ color: [0.0, 0.0, 0.0, 1.0],
+ background_color: None,
+ content: "Hi yo\ngq".to_string(),
+ font_size: 16.0,
+ line_height: 1.25,
+ ..text([4.0, 4.0, 60.0, 60.0], [0.0; 4])
+ };
+ // Family, weight, and italic only change pixels when the host has a
+ // distinct face. The cache key still has to include them: a miss leaves
+ // the previous buffer unused.
+ let face_edits: [Edit; 3] = [
+ ("font family", |text| {
+ text.font_family = "monospace".to_string()
+ }),
+ ("font weight", |text| text.font_weight = 800.0),
+ ("italic", |text| text.italic = true),
+ ];
+ let layout_edits: [Edit; 5] = [
+ ("font size", |text| {
+ text.font_size = 20.0;
+ text.line_height = 1.0;
+ }),
+ ("letter spacing", |text| text.letter_spacing = 3.0),
+ ("alignment", |text| text.align = TextAlign::Right),
+ ("line height", |text| text.line_height = 2.0),
+ ("wrap width", |text| text.bounds[2] = 24.0),
+ ];
+ let render_fresh = |text: &PreparedText| {
+ let mut layer = TextLayer::new(&device, &queue);
+ layer.prepare(
+ &device,
+ &queue,
+ (OUTPUT, OUTPUT),
+ std::slice::from_ref(text),
+ );
+ render_pixels(&device, &queue, &layer)
+ };
+ let base_pixels = render_fresh(&base);
+ assert!(base_pixels.chunks_exact(4).any(|pixel| pixel != [255; 4]));
+
+ let check = |name: &str, edit: fn(&mut PreparedText), require_pixel_change: bool| {
+ let mut edited = base.clone();
+ edit(&mut edited);
+ let expected = render_fresh(&edited);
+ if require_pixel_change {
+ assert!(
+ expected != base_pixels,
+ "{name} edit should change the output"
+ );
+ }
+
+ let mut reused = TextLayer::new(&device, &queue);
+ reused.prepare(&device, &queue, (OUTPUT, OUTPUT), &[base.clone()]);
+ reused.prepare(&device, &queue, (OUTPUT, OUTPUT), &[edited]);
+ assert_eq!(
+ reused.previous_buffers.len(),
+ 1,
+ "{name} edit reused the previous layout"
+ );
+ assert_eq!(
+ render_pixels(&device, &queue, &reused),
+ expected,
+ "{name} edit drew a stale layout"
+ );
+ };
+ for (name, edit) in face_edits {
+ check(name, edit, false);
+ }
+ for (name, edit) in layout_edits {
+ check(name, edit, true);
+ }
+ }
+
#[test]
#[ignore = "requires a working wgpu adapter"]
fn text_background_timeline_preserves_order_and_shrinks_without_stale_layers() {
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index cfb5df9ea..15ddef1bf 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -20,9 +20,8 @@ use frame_pipeline::{
};
use futures::future::OptionFuture;
use layers::{
- Background, BackgroundLayer, BlurLayer, Camera3DBlurKind, Camera3DLayer, CameraLayer,
- ClickRippleLayer, ColorGradeLayer, CursorLayer, DisplayLayer, FrameLayer, MaskLayer,
- NotchLayer, NotchUniforms,
+ BackgroundLayer, BlurLayer, Camera3DBlurKind, Camera3DLayer, CameraLayer, ClickRippleLayer,
+ ColorGradeLayer, CursorLayer, DisplayLayer, FrameLayer, MaskLayer, NotchLayer, NotchUniforms,
};
use specta::Type;
use spring_mass_damper::SpringMassDamperSimulationConfig;
@@ -96,6 +95,92 @@ pub fn prewarm_fonts() {
drop(layers::new_font_system());
}
+/// Renders one small RGBA frame and one NV12 frame through the full frame
+/// pipeline with `layers`, then discards them. A fresh process waits on the
+/// driver finishing each pipeline the first time it draws (the first preview
+/// of the first editor took ~50 ms longer than any later one); building
+/// `layers` and drawing once off the hot path moves both costs there.
+pub async fn prewarm_first_frame(
+ shared: SharedWgpuDevice,
+ layers: &mut RendererLayers,
+) -> Result<(), RenderingError> {
+ const SIZE: u32 = 64;
+ let studio_meta = StudioRecordingMeta::SingleSegment {
+ segment: cap_project::SingleSegment {
+ display: cap_project::VideoMeta {
+ path: "prewarm.png".into(),
+ fps: 30,
+ start_time: Some(0.0),
+ device_id: None,
+ },
+ camera: None,
+ audio: None,
+ cursor: None,
+ },
+ };
+ let recording_meta = RecordingMeta {
+ platform: None,
+ project_path: std::env::temp_dir(),
+ pretty_name: "Prewarm".to_string(),
+ sharing: None,
+ inner: cap_project::RecordingMetaInner::Studio(Box::new(studio_meta.clone())),
+ upload: None,
+ };
+ let constants = RenderVideoConstants::from_shared_device(
+ shared,
+ RenderOptions {
+ screen_size: XY::new(SIZE, SIZE),
+ camera_size: None,
+ preserve_screen_alpha: true,
+ },
+ studio_meta,
+ recording_meta,
+ Arc::new(BackgroundTextureCache::default()),
+ );
+ let config = Arc::new(ProjectConfiguration::default());
+ let cursor_events = CursorEvents::default();
+ let mut zoom_timeline = ZoomTransformTimeline::new(
+ &[],
+ None,
+ &cursor_events,
+ config.screen_movement_spring,
+ 0.0,
+ None,
+ );
+ zoom_timeline.ensure_precomputed_until(1.0 / 30.0);
+ let (base_w, base_h) = ProjectUniforms::get_base_size(&constants.options, &config);
+ let mut frame_renderer = FrameRenderer::new(&constants);
+ let pixels = (SIZE * SIZE) as usize;
+ for screen_frame in [
+ DecodedFrame::new(vec![255; pixels * 4], SIZE, SIZE),
+ DecodedFrame::new_nv12(vec![128; pixels * 3 / 2], SIZE, SIZE, SIZE, SIZE),
+ ] {
+ let segment_frames = DecodedSegmentFrames {
+ screen_size: XY::new(SIZE, SIZE),
+ screen_frame: Some(screen_frame),
+ camera_frame: None,
+ segment_time: 0.0,
+ recording_time: 0.0,
+ segment_has_camera: false,
+ };
+ let uniforms = ProjectUniforms::new(
+ &constants,
+ &config,
+ 0,
+ 30,
+ XY::new(base_w, base_h),
+ &cursor_events,
+ &segment_frames,
+ 0.0,
+ &zoom_timeline,
+ );
+ frame_renderer
+ .render_immediate(segment_frames, uniforms, &cursor_events, true, layers)
+ .await?;
+ }
+ Ok(())
+}
+
/// Unique installed font family names, sorted, for the editor's font picker.
/// Reuses the process-wide font database scan (see [`prewarm_fonts`]).
/// Dot-prefixed families (macOS-internal UI fonts) are hidden the same way
@@ -729,6 +814,10 @@ pub enum RenderingError {
BufferMapWaitingFailed,
#[error(transparent)]
BufferMapFailed(#[from] wgpu::BufferAsyncError),
+ #[error(
+ "Not enough memory to read back frame {frame_number}. Close other apps or lower the export resolution and try again."
+ )]
+ ReadbackAllocationFailed { frame_number: u32 },
#[error("Sending frame to channel failed")]
ChannelSendFrameFailed(#[from] mpsc::error::SendError<(RenderedFrame, u32)>),
#[error("Sending NV12 frame to channel failed")]
@@ -1158,6 +1247,7 @@ pub async fn render_video_to_channel(
}
}
Ok(None) => in_flight.rendered(current_frame_number, None)?,
+ Err(e @ RenderingError::ReadbackAllocationFailed { .. }) => return Err(e),
Err(e) => {
tracing::error!(
frame_number = current_frame_number,
@@ -1903,6 +1993,7 @@ pub async fn render_video_to_channel_nv12(
}
}
Ok(None) => in_flight.rendered(current_frame_number, None)?,
+ Err(e @ RenderingError::ReadbackAllocationFailed { .. }) => return Err(e),
Err(e) => {
tracing::error!(
frame_number = current_frame_number,
@@ -2102,6 +2193,7 @@ impl InFlightFrame {
let expected = self.0.take();
let frame = match flushed.transpose() {
Ok(frame) => frame,
+ Err(error @ RenderingError::ReadbackAllocationFailed { .. }) => return Err(error),
Err(error) => {
if let Some(frame_number) = expected {
tracing::error!(frame_number, %error, "In-flight export frame readback failed");
@@ -5235,6 +5327,33 @@ impl ProjectUniforms {
mod tests {
use super::*;
+ #[test]
+ fn color_components_parse_rgb_hex_with_either_case_and_optional_hash() {
+ for hex in ["#4785FF", "4785ff", "#4785ff"] {
+ assert_eq!(parse_color_component(hex, 0), 71.0 / 255.0);
+ assert_eq!(parse_color_component(hex, 1), 133.0 / 255.0);
+ assert_eq!(parse_color_component(hex, 2), 1.0);
+ }
+ }
+
+ #[test]
+ fn color_components_fall_back_for_unsupported_hex_formats() {
+ for hex in ["", "#abc", "#abcd", "#12345678", " #123456 ", "#GGHHII"] {
+ for index in 0..3 {
+ assert_eq!(parse_color_component(hex, index), 1.0, "{hex:?}");
+ }
+ }
+ }
+
+ #[test]
+ fn color_components_fall_back_for_six_byte_unicode_colors() {
+ for hex in ["#白色", "白色", "#a白bc", "#ab白c", "#abc白", "#💙ab"] {
+ for index in 0..3 {
+ assert_eq!(parse_color_component(hex, index), 1.0, "{hex:?}");
+ }
+ }
+ }
+
#[test]
fn camera_blur_output_rejects_missing_pending_and_failed_masks() {
use cap_camera_effects::{BlurFailure, BlurMaskStatus, BlurMode, BlurOutputStatus};
@@ -6581,6 +6700,7 @@ mod export_order_tests {
async fn export(
constants: &RenderVideoConstants,
screen: AsyncVideoDecoderHandle,
+ rgba: bool,
) -> (Result<(), RenderingError>, Vec<u32>) {
let project = ProjectConfiguration {
timeline: Some(
@@ -6612,6 +6732,29 @@ mod export_order_tests {
render_display: true,
}];
let recordings = ProjectRecordingsMeta { segments: vec![] };
+ if rgba {
+ let (sender, mut receiver) = mpsc::channel(8);
+ let render = render_video_to_channel(
+ constants,
+ &project,
+ sender,
+ &constants.recording_meta,
+ &constants.meta,
+ segments,
+ FPS,
+ XY::new(SIZE, SIZE),
+ &recordings,
+ None,
+ );
+ let collect = async {
+ let mut order = Vec::new();
+ while let Some((frame, _)) = receiver.recv().await {
+ order.push(frame.frame_number);
+ }
+ order
+ };
+ return tokio::join!(render, collect);
+ }
let (sender, mut receiver) = mpsc::channel(8);
let render = render_video_to_channel_nv12(
constants,
@@ -6644,6 +6787,16 @@ mod export_order_tests {
software_path: bool,
slow_decode: Option<(u32, Duration)>,
slow_readback: Option<(u32, Duration)>,
+ ) -> Option<(Result<(), RenderingError>, Vec<u32>)> {
+ run_export_with_allocation_failure(software_path, slow_decode, slow_readback, None, false)
+ }
+
+ fn run_export_with_allocation_failure(
+ software_path: bool,
+ slow_decode: Option<(u32, Duration)>,
+ slow_readback: Option<(u32, Duration)>,
+ allocation_failure: Option<u32>,
+ rgba: bool,
) -> Option<(Result<(), RenderingError>, Vec<u32>)> {
let _one_export_at_a_time = EXPORTS
.lock()
@@ -6657,14 +6810,18 @@ mod export_order_tests {
.build()
.expect("runtime")
.block_on(async move {
- let Some(constants) = constants(software_path).await else {
+ let Some(mut constants) = constants(software_path).await else {
eprintln!("No graphics adapter available for export order test");
return None;
};
if let Some((frame_number, delay)) = slow_readback {
frame_pipeline::readback_delay::inject(frame_number, delay);
}
- Some(export(&constants, scripted_decoder(slow_decode)).await)
+ if let Some(frame_number) = allocation_failure {
+ constants.is_software_adapter = true;
+ frame_pipeline::readback_allocation_failure::inject(frame_number);
+ }
+ Some(export(&constants, scripted_decoder(slow_decode), rgba).await)
})
})
.expect("export thread")
@@ -6677,6 +6834,32 @@ mod export_order_tests {
result
}
+ #[test]
+ fn readback_allocation_failure_stops_export_without_retry_or_substitution() {
+ for rgba in [true, false] {
+ for failed_frame in [0, 5, FRAMES - 1] {
+ let Some((result, order)) =
+ run_export_with_allocation_failure(false, None, None, Some(failed_frame), rgba)
+ else {
+ return;
+ };
+ assert!(
+ matches!(
+ result,
+ Err(RenderingError::ReadbackAllocationFailed { frame_number })
+ if frame_number == failed_frame
+ ),
+ "rgba={rgba}, failed_frame={failed_frame}: {result:?}"
+ );
+ assert_eq!(
+ order,
+ (0..failed_frame).collect::<Vec<_>>(),
+ "rgba={rgba}, failed_frame={failed_frame}"
+ );
+ }
+ }
+ }
+
#[test]
fn slow_decode_keeps_every_frame_in_order() {
let decode_deadline = Duration::from_secs(2);
@@ -6749,6 +6932,18 @@ mod export_order_tests {
}
}
+ #[test]
+ fn in_flight_frame_preserves_readback_allocation_failure() {
+ let mut in_flight = InFlightFrame::default();
+ in_flight.rendered(5, None).unwrap();
+ assert!(matches!(
+ in_flight.drain::<RenderedFrame>(Some(Err(RenderingError::ReadbackAllocationFailed {
+ frame_number: 5,
+ }))),
+ Err(RenderingError::ReadbackAllocationFailed { frame_number: 5 })
+ ));
+ }
+
#[test]
fn in_flight_frame_must_come_out_next() {
let mut in_flight = InFlightFrame::default();
@@ -7921,7 +8116,7 @@ fn join_layer<T>(handle: std::thread::ScopedJoinHandle<'_, T>) -> T {
}
impl RendererLayers {
- fn reset_frame_state(&mut self) {
+ pub fn reset_frame_state(&mut self) {
self.display.reset_frame_state();
self.camera.reset_frame_state();
self.camera_only.reset_frame_state();
@@ -8227,15 +8422,12 @@ impl RendererLayers {
) -> Result<(), RenderingError> {
self.validate_overlay_uniforms(uniforms)?;
self.camera_blur_unavailable = false;
+ let background = self.background.background_for(
+ &uniforms.project.background.source,
+ constants.options.preserve_screen_alpha,
+ );
self.background
- .prepare(
- constants,
- uniforms,
- Background::from_source(
- uniforms.project.background.source.clone(),
- constants.options.preserve_screen_alpha,
- ),
- )
+ .prepare(constants, uniforms, background)
.await?;
self.background_blur.prepare(&constants.queue, uniforms);
@@ -8379,15 +8571,12 @@ impl RendererLayers {
let mut timings = FrameRenderStageTimings::default();
let start = Instant::now();
+ let background = self.background.background_for(
+ &uniforms.project.background.source,
+ constants.options.preserve_screen_alpha,
+ );
self.background
- .prepare(
- constants,
- uniforms,
- Background::from_source(
- uniforms.project.background.source.clone(),
- constants.options.preserve_screen_alpha,
- ),
- )
+ .prepare(constants, uniforms, background)
.await?;
timings.background_prepare_duration = start.elapsed();
@@ -8539,6 +8728,12 @@ impl RendererLayers {
) -> Result<(), RenderingError> {
self.validate_overlay_uniforms(uniforms)?;
+ let session_views = [session.current_texture_view(), session.other_texture_view()];
+ self.background_blur.retain_views(session_views);
+ self.background_color_grade.retain_views(session_views);
+ self.camera3d.retain_views(session_views);
+ self.mask.begin_frame(session_views);
+
if render_display {
self.display.copy_to_texture(encoder);
}
@@ -9045,7 +9240,7 @@ fn blur_mode_from_config(
fn parse_color_component(hex_color: &str, index: usize) -> f32 {
let color = hex_color.trim_start_matches('#');
- if color.len() == 6 {
+ if color.len() == 6 && color.is_ascii() {
let start = index * 2;
if let Ok(value) = u8::from_str_radix(&color[start..start + 2], 16) {
return value as f32 / 255.0;
@@ -10555,3 +10750,137 @@ mod initial_decode_recovery_tests {
assert!(times.last().copied().unwrap_or_default() >= 0.0);
}
}
+
+#[cfg(test)]
+mod per_frame_cache_tests {
+ use super::*;
+ use cap_project::{BackgroundSource, CursorMoveEvent, CursorType};
+
+ #[tokio::test]
+ async fn steady_frames_reuse_bind_groups_and_the_resolved_wallpaper() {
+ let recording_meta: RecordingMeta = serde_json::from_value(serde_json::json!({
+ "pretty_name": "per-frame-caches",
+ "display": { "path": "display.mp4", "fps": 30 },
+ "camera": null, "audio": null, "cursor": null
+ }))
+ .expect("metadata");
+ let meta = recording_meta.studio_meta().expect("studio").clone();
+ let size = XY::new(160, 90);
+ let constants = RenderVideoConstants::new_with_options(
+ RenderOptions {
+ screen_size: size,
+ camera_size: None,
+ preserve_screen_alpha: false,
+ },
+ recording_meta,
+ meta,
+ )
+ .await
+ .expect("graphics device");
+
+ let directory = tempfile::tempdir().expect("temp dir");
+ let wallpaper = directory.path().join("wallpaper.png");
+ image::RgbaImage::from_pixel(64, 48, image::Rgba([40, 90, 160, 255]))
+ .save(&wallpaper)
+ .expect("wallpaper");
+
+ let mut project = ProjectConfiguration::default();
+ project.background.source = BackgroundSource::Wallpaper {
+ path: Some(wallpaper.to_string_lossy().into_owned()),
+ };
+ project.background.padding = 10.0;
+ project.cursor.set_cursor_type(CursorType::Circle);
+ project.color_correction.screen.exposure = 0.4;
+ project.timeline = Some(
+ serde_json::from_value(serde_json::json!({
+ "segments": [], "zoomSegments": [],
+ "maskSegments": [
+ {
+ "start": 0.0, "end": 10.0, "maskType": "sensitive",
+ "center": { "x": 0.3, "y": 0.4 }, "size": { "x": 0.3, "y": 0.3 },
+ "pixelation": 1024.0
+ },
+ {
+ "start": 0.0, "end": 10.0, "maskType": "sensitive",
+ "center": { "x": 0.6, "y": 0.5 }, "size": { "x": 0.2, "y": 0.2 },
+ "pixelation": 18.0
+ }
+ ]
+ }))
+ .expect("timeline"),
+ );
+ let project = Arc::new(project);
+
+ let frame_count = 10;
+ let cursor = CursorEvents {
+ clicks: vec![],
+ moves: (0..frame_count)
+ .map(|frame| CursorMoveEvent {
+ active_modifiers: vec![],
+ cursor_id: "0".to_string(),
+ time_ms: frame as f64 * 1000.0 / 30.0,
+ x: 0.2 + frame as f64 * 0.05,
+ y: 0.5,
+ })
+ .collect(),
+ };
+ let zoom = ZoomTransformTimeline::from_project(&project, &cursor, 1.0, size);
+ let mut renderer = FrameRenderer::new(&constants);
+ let mut layers = RendererLayers::new_for_preparing_preview(
+ &constants.device,
+ &constants.queue,
+ constants.is_software_adapter,
+ &project,
+ )
+ .expect("layers");
+
+ let mut first_cursor_bind_group = None;
+ let mut first_background_generation = None;
+ for frame_number in 0..frame_count {
+ let time = frame_number as f32 / 30.0;
+ let frames = DecodedSegmentFrames {
+ screen_size: size,
+ screen_frame: Some(DecodedFrame::new(
+ [200, 120, 60, 255].repeat(160 * 90),
+ 160,
+ 90,
+ )),
+ camera_frame: None,
+ segment_time: time,
+ recording_time: time,
+ segment_has_camera: false,
+ };
+ let uniforms = ProjectUniforms::new(
+ &constants,
+ &project,
+ frame_number,
+ 30,
+ size,
+ &cursor,
+ &frames,
+ 1.0,
+ &zoom,
+ );
+ assert_eq!(uniforms.masks.len(), 2);
+ renderer
+ .render_immediate(frames, uniforms, &cursor, true, &mut layers)
+ .await
+ .expect("frame");
+
+ let bind_group = layers.cursor.bind_group().cloned().expect("cursor drawn");
+ let generation = layers.background.static_generation();
+ if frame_number == 0 {
+ first_cursor_bind_group = Some(bind_group);
+ first_background_generation = generation;
+ std::fs::remove_file(&wallpaper).expect("remove wallpaper");
+ } else {
+ assert_eq!(Some(bind_group), first_cursor_bind_group);
+ assert_eq!(generation, first_background_generation);
+ }
+ }
+
+ assert_eq!(layers.mask.uniform_buffer_count(), 3);
+ assert!(layers.mask.bind_groups_created() <= 6);
+ assert!(layers.background_color_grade.bind_groups_created() <= 2);
+ }
+}
diff --git a/crates/rendering/src/mask.rs b/crates/rendering/src/mask.rs
index c2c753d33..1f212619c 100644
--- a/crates/rendering/src/mask.rs
+++ b/crates/rendering/src/mask.rs
@@ -1,3 +1,5 @@
+use std::borrow::Cow;
+
use cap_project::{
MaskKind, MaskScalarKeyframe, MaskSegment, MaskVectorKeyframe, XY, mask_effect_contract,
};
@@ -6,17 +8,26 @@ use crate::{MaskRenderMode, PreparedMask};
const MASK_EFFECT_BASE_HEIGHT: f32 = 1080.0;
-fn interpolate_vector(base: XY<f64>, keys: &[MaskVectorKeyframe], time: f64) -> XY<f64> {
- if keys.is_empty() {
- return base;
+fn sorted_by_time<T: Clone>(keys: &[T], time: impl Fn(&T) -> f64) -> Cow<'_, [T]> {
+ if keys.is_sorted_by(|a, b| time(a) <= time(b)) {
+ return Cow::Borrowed(keys);
}
let mut sorted = keys.to_vec();
sorted.sort_by(|a, b| {
- a.time
- .partial_cmp(&b.time)
+ time(a)
+ .partial_cmp(&time(b))
.unwrap_or(std::cmp::Ordering::Equal)
});
+ Cow::Owned(sorted)
+}
+
+fn interpolate_vector(base: XY<f64>, keys: &[MaskVectorKeyframe], time: f64) -> XY<f64> {
+ if keys.is_empty() {
+ return base;
+ }
+
+ let sorted = sorted_by_time(keys, |key| key.time);
if time <= sorted[0].time {
return XY::new(sorted[0].x, sorted[0].y);
@@ -43,12 +54,7 @@ fn interpolate_scalar(base: f64, keys: &[MaskScalarKeyframe], time: f64) -> f64
return base;
}
- let mut sorted = keys.to_vec();
- sorted.sort_by(|a, b| {
- a.time
- .partial_cmp(&b.time)
- .unwrap_or(std::cmp::Ordering::Equal)
- });
+ let sorted = sorted_by_time(keys, |key| key.time);
if time <= sorted[0].time {
return sorted[0].value;
@@ -72,18 +78,20 @@ pub fn interpolate_masks(
frame_time: f64,
segments: &[MaskSegment],
) -> Vec<PreparedMask> {
+ if segments.is_empty() {
+ return Vec::new();
+ }
+
let mut prepared = Vec::new();
let mut ordered = segments
.iter()
- .filter(|segment| segment.enabled)
+ .filter(|segment| {
+ segment.enabled && !(frame_time < segment.start || frame_time > segment.end)
+ })
.collect::<Vec<_>>();
ordered.sort_by_key(|segment| segment.track);
for segment in ordered {
- if frame_time < segment.start || frame_time > segment.end {
- continue;
- }
-
let relative_time = (frame_time - segment.start).max(0.0);
let position =
@@ -200,6 +208,194 @@ mod tests {
}
}
+ fn old_interpolate_vector(base: XY<f64>, keys: &[MaskVectorKeyframe], time: f64) -> XY<f64> {
+ if keys.is_empty() {
+ return base;
+ }
+
+ let mut sorted = keys.to_vec();
+ sorted.sort_by(|a, b| {
+ a.time
+ .partial_cmp(&b.time)
+ .unwrap_or(std::cmp::Ordering::Equal)
+ });
+
+ if time <= sorted[0].time {
+ return XY::new(sorted[0].x, sorted[0].y);
+ }
+
+ for window in sorted.windows(2) {
+ let prev = &window[0];
+ let next = &window[1];
+ if time <= next.time {
+ let span = (next.time - prev.time).max(1e-6);
+ let t = ((time - prev.time) / span).clamp(0.0, 1.0);
+ let x = prev.x + (next.x - prev.x) * t;
+ let y = prev.y + (next.y - prev.y) * t;
+ return XY::new(x, y);
+ }
+ }
+
+ let last = sorted.last().unwrap();
+ XY::new(last.x, last.y)
+ }
+
+ fn old_interpolate_scalar(base: f64, keys: &[MaskScalarKeyframe], time: f64) -> f64 {
+ if keys.is_empty() {
+ return base;
+ }
+
+ let mut sorted = keys.to_vec();
+ sorted.sort_by(|a, b| {
+ a.time
+ .partial_cmp(&b.time)
+ .unwrap_or(std::cmp::Ordering::Equal)
+ });
+
+ if time <= sorted[0].time {
+ return sorted[0].value;
+ }
+
+ for window in sorted.windows(2) {
+ let prev = &window[0];
+ let next = &window[1];
+ if time <= next.time {
+ let span = (next.time - prev.time).max(1e-6);
+ let t = ((time - prev.time) / span).clamp(0.0, 1.0);
+ return prev.value + (next.value - prev.value) * t;
+ }
+ }
+
+ sorted.last().map(|k| k.value).unwrap_or(base)
+ }
+
+ fn vector_key(time: f64, x: f64, y: f64) -> MaskVectorKeyframe {
+ MaskVectorKeyframe { time, x, y }
+ }
+
+ fn scalar_key(time: f64, value: f64) -> MaskScalarKeyframe {
+ MaskScalarKeyframe { time, value }
+ }
+
+ const QUERY_TIMES: [f64; 13] = [
+ f64::NEG_INFINITY,
+ -1.0,
+ 0.0,
+ 0.25,
+ 0.5,
+ 1.0,
+ 1.5,
+ 2.0,
+ 2.5,
+ 3.0,
+ f64::INFINITY,
+ f64::NAN,
+ -0.0,
+ ];
+
+ #[test]
+ fn keyframe_order_does_not_change_interpolation() {
+ let sorted = vec![
+ vector_key(0.0, 0.1, 0.2),
+ vector_key(1.0, 0.4, 0.3),
+ vector_key(1.0, 0.6, 0.1),
+ vector_key(2.5, 0.9, 0.8),
+ ];
+ let mut unsorted = sorted.clone();
+ unsorted.swap(0, 3);
+ unsorted.swap(1, 3);
+ let vector_cases = [
+ sorted.clone(),
+ unsorted,
+ vec![],
+ vec![vector_key(1.0, 0.3, 0.7)],
+ vec![
+ vector_key(2.0, 0.9, 0.9),
+ vector_key(f64::NAN, 0.5, 0.5),
+ vector_key(0.0, 0.1, 0.1),
+ ],
+ vec![
+ vector_key(0.0, 0.1, 0.1),
+ vector_key(f64::NAN, 0.5, 0.5),
+ vector_key(2.0, 0.9, 0.9),
+ ],
+ vec![
+ vector_key(f64::NEG_INFINITY, 0.2, 0.2),
+ vector_key(f64::INFINITY, 0.8, 0.8),
+ ],
+ ];
+
+ let scalar_sorted = vec![
+ scalar_key(0.5, 0.2),
+ scalar_key(1.0, 0.4),
+ scalar_key(1.0, 0.7),
+ scalar_key(2.0, 0.9),
+ ];
+ let scalar_cases = [
+ scalar_sorted.iter().rev().cloned().collect::<Vec<_>>(),
+ scalar_sorted,
+ vec![],
+ vec![scalar_key(1.0, 0.6)],
+ vec![
+ scalar_key(1.0, 0.6),
+ scalar_key(f64::NAN, 0.1),
+ scalar_key(0.0, 0.3),
+ ],
+ vec![
+ scalar_key(0.0, 0.3),
+ scalar_key(f64::NAN, 0.1),
+ scalar_key(1.0, 0.6),
+ ],
+ ];
+
+ let base = XY::new(0.5, 0.5);
+ for time in QUERY_TIMES {
+ for keys in &vector_cases {
+ let expected = old_interpolate_vector(base, keys, time);
+ let actual = interpolate_vector(base, keys, time);
+ assert_eq!(
+ (actual.x.to_bits(), actual.y.to_bits()),
+ (expected.x.to_bits(), expected.y.to_bits()),
+ "time {time} keys {keys:?}"
+ );
+ }
+ for keys in &scalar_cases {
+ assert_eq!(
+ interpolate_scalar(0.3, keys, time).to_bits(),
+ old_interpolate_scalar(0.3, keys, time).to_bits(),
+ "time {time} keys {keys:?}"
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn only_out_of_order_or_non_finite_keyframes_are_cloned() {
+ let time = |key: &MaskVectorKeyframe| key.time;
+ let in_order = [
+ vector_key(0.0, 0.0, 0.0),
+ vector_key(1.0, 1.0, 1.0),
+ vector_key(1.0, 0.5, 0.5),
+ vector_key(2.0, 0.2, 0.2),
+ ];
+ assert!(matches!(sorted_by_time(&in_order, time), Cow::Borrowed(_)));
+ assert!(matches!(
+ sorted_by_time(&in_order[..1], time),
+ Cow::Borrowed(_)
+ ));
+ assert!(matches!(sorted_by_time(&[], time), Cow::Borrowed(_)));
+
+ let out_of_order = [vector_key(1.0, 0.0, 0.0), vector_key(0.0, 1.0, 1.0)];
+ assert!(matches!(sorted_by_time(&out_of_order, time), Cow::Owned(_)));
+
+ let with_nan = [
+ vector_key(0.0, 0.0, 0.0),
+ vector_key(f64::NAN, 0.5, 0.5),
+ vector_key(2.0, 1.0, 1.0),
+ ];
+ assert!(matches!(sorted_by_time(&with_nan, time), Cow::Owned(_)));
+ }
+
#[test]
fn mask_layers_follow_lane_order_with_stable_ties() {
let mut front = sample_segment();
@@ -220,6 +416,53 @@ mod tests {
assert!(masks[2].center.x < masks[1].center.x);
}
+ #[test]
+ fn active_masks_preserve_order_and_include_time_boundaries() {
+ let mut first = sample_segment();
+ first.track = 2;
+ first.center.x = 0.25;
+ let mut inactive = first.clone();
+ inactive.start = 20.0;
+ inactive.end = 30.0;
+ inactive.track = 0;
+ let mut last = first.clone();
+ last.center.x = 0.75;
+ let mut disabled = first.clone();
+ disabled.enabled = false;
+ disabled.track = 0;
+ let segments = [first, inactive, last, disabled];
+
+ for time in [0.0, 5.0, 10.0] {
+ let masks = interpolate_masks(XY::new(1920, 1080), time, &segments);
+ assert_eq!(masks.len(), 2);
+ assert_eq!(masks[0].center.x, 0.25);
+ assert_eq!(masks[1].center.x, 0.75);
+ }
+ for time in [-1.0, 15.0, 31.0] {
+ assert!(interpolate_masks(XY::new(1920, 1080), time, &segments).is_empty());
+ }
+ }
+
+ #[test]
+ fn mask_time_filter_preserves_nonfinite_comparison_behavior() {
+ let mut segment = sample_segment();
+ segment.start = f64::NAN;
+ let masks = interpolate_masks(XY::new(1920, 1080), 5.0, std::slice::from_ref(&segment));
+ assert_eq!(masks.len(), 1);
+ assert!(interpolate_masks(XY::new(1920, 1080), 11.0, &[segment]).is_empty());
+
+ let mut disabled = sample_segment();
+ disabled.enabled = false;
+ let segments = [sample_segment(), disabled];
+ assert_eq!(
+ interpolate_masks(XY::new(1920, 1080), f64::NAN, &segments).len(),
+ 1
+ );
+ for time in [f64::NEG_INFINITY, f64::INFINITY] {
+ assert!(interpolate_masks(XY::new(1920, 1080), time, &segments).is_empty());
+ }
+ }
+
#[test]
fn sensitive_mask_effect_scales_with_output_height() {
let segment = sample_segment();
diff --git a/crates/rendering/src/scene.rs b/crates/rendering/src/scene.rs
index 6c12e8152..302ff8e15 100644
--- a/crates/rendering/src/scene.rs
+++ b/crates/rendering/src/scene.rs
@@ -108,7 +108,9 @@ impl InterpolatedScene {
let (current_mode, next_mode, transition_progress) = if let Some(segment) = cursor.segment {
let transition_in = segment.transition_in.max(0.0);
- let transition_out = segment.transition_out.max(0.0);
+ let transition_out = segment
+ .transition_out
+ .clamp(0.0, (segment.end - segment.start).max(0.0));
let transition_start = segment.start - transition_in;
let transition_end = segment.end - transition_out;
@@ -382,6 +384,78 @@ mod tests {
}
}
+ #[test]
+ fn long_exit_transition_does_not_jump_at_scene_start() {
+ let values = |scene: InterpolatedScene| {
+ [
+ scene.camera_opacity,
+ scene.camera_only_transition_opacity(),
+ scene.camera_only_zoom,
+ scene.split_factor,
+ scene.floating_factor,
+ ]
+ };
+ for mode in [
+ SceneMode::CameraOnly,
+ SceneMode::HideCamera,
+ SceneMode::SplitScreen,
+ SceneMode::Floating,
+ ] {
+ for duration in [0.1, 1.0] {
+ let segments = [SceneSegment {
+ end: 2.0 + duration,
+ mode,
+ transition_out: 2.0,
+ ..camera_scene(0.3)
+ }];
+ let before = values(scene_at(2.0 - 1e-6, &segments));
+ let start = values(scene_at(2.0, &segments));
+ for (before, start) in before.into_iter().zip(start) {
+ assert!(
+ (before - start).abs() < 1e-5,
+ "{mode:?}, {duration}s: jumped from {before} to {start}"
+ );
+ }
+ let midpoint = scene_at(2.0 + duration / 2.0, &segments);
+ assert!((midpoint.transition_progress - 0.5).abs() < 1e-5);
+ let end = scene_at(segments[0].end, &segments);
+ assert!(matches!(end.scene_mode, SceneMode::Default));
+ }
+ }
+ }
+
+ #[test]
+ fn loaded_unsorted_scenes_fade_into_the_next_scene() {
+ let project = tempfile::tempdir().unwrap();
+ std::fs::write(
+ project.path().join("project-config.json"),
+ r#"{
+ "timeline": {
+ "segments": [{ "recordingSegment": 0, "start": 0.0, "end": 12.0, "timescale": 1.0 }],
+ "zoomSegments": [],
+ "sceneSegments": [
+ { "start": 8.0, "end": 10.0, "mode": "cameraOnly" },
+ { "start": 1.0, "end": 3.0, "mode": "cameraOnly" },
+ { "start": 5.0, "end": 7.0, "mode": "hideCamera" }
+ ]
+ }
+ }"#,
+ )
+ .unwrap();
+ let config = cap_project::ProjectConfiguration::load(project.path()).unwrap();
+ let segments = &config.timeline.as_ref().unwrap().scene_segments;
+ let before = scene_at(4.6, segments);
+ let during = scene_at(4.85, segments);
+ let after = scene_at(5.0, segments);
+ assert_eq!(before.camera_opacity, 1.0);
+ assert!(
+ (during.camera_opacity - 0.5).abs() < 1e-5,
+ "expected the camera halfway faded out, got {}",
+ during.camera_opacity
+ );
+ assert_eq!(after.camera_opacity, 0.0);
+ }
+
#[test]
fn camera_only_blur_tracks_motion_at_both_boundaries() {
let segments = [camera_scene(0.3)];
diff --git a/crates/rendering/src/spring_mass_damper.rs b/crates/rendering/src/spring_mass_damper.rs
index 966cec52e..825efa1e3 100644
--- a/crates/rendering/src/spring_mass_damper.rs
+++ b/crates/rendering/src/spring_mass_damper.rs
@@ -189,6 +189,10 @@ impl SpringMassDamperSimulation {
self.target_position = target_position;
}
+ pub(crate) fn is_at_rest(&self) -> bool {
+ self.position == self.target_position && self.velocity == XY::new(0.0, 0.0)
+ }
+
fn step(&mut self, dt_ms: f32) -> SpringStep {
let key = [dt_ms, self.tension, self.mass, self.friction].map(f32::to_bits);
if let Some(cached) = self.cached_step
diff --git a/crates/rendering/tests/camera_frame_geometry.rs b/crates/rendering/tests/camera_frame_geometry.rs
index 16b626f84..0d077b2db 100644
--- a/crates/rendering/tests/camera_frame_geometry.rs
+++ b/crates/rendering/tests/camera_frame_geometry.rs
@@ -71,10 +71,11 @@ async fn camera_geometry_matches_a_project_with_the_current_clips_dimensions() {
for number in [56, 90, 145] {
let zoom =
ZoomTransformTimeline::from_project(&project, &cursor, 6.0, screen_size);
+ let shared = std::sync::Arc::new(project.clone());
constants.options.camera_size = Some(first_camera_size);
let actual = ProjectUniforms::new(
&constants,
- &project,
+ &shared,
number,
30,
screen_size,
@@ -86,7 +87,7 @@ async fn camera_geometry_matches_a_project_with_the_current_clips_dimensions() {
constants.options.camera_size = Some(camera_size);
let expected = ProjectUniforms::new(
&constants,
- &project,
+ &shared,
number,
30,
screen_size,
diff --git a/crates/rendering/tests/display_layout.rs b/crates/rendering/tests/display_layout.rs
index 0a8ef57c8..8f331ec74 100644
--- a/crates/rendering/tests/display_layout.rs
+++ b/crates/rendering/tests/display_layout.rs
@@ -167,7 +167,8 @@ fn uniforms_for(
let cursor = CursorEvents::default();
let zoom =
ZoomTransformTimeline::from_project(project, &cursor, 1.0, constants.options.screen_size);
+ let shared = std::sync::Arc::new(project.clone());
ProjectUniforms::new(
- constants, project, 0, 30, resolution, &cursor, frames, 1.0, &zoom,
+ constants, &shared, 0, 30, resolution, &cursor, frames, 1.0, &zoom,
)
}
diff --git a/crates/rendering/tests/per_frame_caches.rs b/crates/rendering/tests/per_frame_caches.rs
new file mode 100644
index 000000000..cd98e8300
--- /dev/null
+++ b/crates/rendering/tests/per_frame_caches.rs
@@ -0,0 +1,268 @@
+use std::sync::Arc;
+
+use cap_project::{
+ AspectRatio, BackgroundSource, CursorClickEvent, CursorEvents, CursorMoveEvent, CursorType,
+ ProjectConfiguration, RecordingMeta, XY,
+};
+use cap_rendering::{
+ DecodedSegmentFrames, FrameRenderer, ProjectUniforms, RenderOptions, RenderVideoConstants,
+ RendererLayers, ZoomTransformTimeline, decoder::DecodedFrame,
+};
+
+const FPS: u32 = 30;
+const FRAMES: u32 = 8;
+const SCREEN: XY<u32> = XY { x: 160, y: 90 };
+
+fn cursor_events() -> CursorEvents {
+ let moves = (0..FRAMES)
+ .map(|frame| CursorMoveEvent {
+ active_modifiers: vec![],
+ cursor_id: if frame < FRAMES / 2 { "a" } else { "b" }.to_string(),
+ time_ms: frame as f64 * 1000.0 / FPS as f64,
+ x: 0.2 + frame as f64 * 0.05,
+ y: 0.3 + (frame % 3) as f64 * 0.1,
+ })
+ .collect();
+ let click = |time_ms, down| CursorClickEvent {
+ active_modifiers: vec![],
+ cursor_num: 0,
+ cursor_id: "a".to_string(),
+ time_ms,
+ down,
+ };
+ CursorEvents {
+ clicks: vec![click(90.0, true), click(150.0, false)],
+ moves,
+ }
+}
+
+fn screen_frame(time: f64) -> DecodedSegmentFrames {
+ let pixels = (0..SCREEN.x * SCREEN.y)
+ .flat_map(|index| {
+ let x = index % SCREEN.x;
+ let y = index / SCREEN.x;
+ [(x * 3) as u8, (y * 5) as u8, ((x + y) * 2) as u8, 255]
+ })
+ .collect();
+ DecodedSegmentFrames {
+ screen_size: SCREEN,
+ screen_frame: Some(DecodedFrame::new(pixels, SCREEN.x, SCREEN.y)),
+ camera_frame: None,
+ segment_time: time as f32,
+ recording_time: time as f32,
+ segment_has_camera: false,
+ }
+}
+
+async fn constants(directory: &std::path::Path) -> RenderVideoConstants {
+ for (name, color) in [("a.png", [255, 40, 40]), ("b.png", [40, 40, 255])] {
+ image::RgbaImage::from_fn(24, 32, |x, y| {
+ let alpha = if x + y < 40 { 255 } else { 0 };
+ image::Rgba([color[0], color[1], color[2], alpha])
+ })
+ .save(directory.join(name))
+ .unwrap();
+ }
+ let mut recording_meta: RecordingMeta = serde_json::from_value(serde_json::json!({
+ "pretty_name": "per-frame-caches",
+ "segments": [{ "display": { "path": "display.mp4", "fps": FPS } }],
+ "cursors": {
+ "a": { "imagePath": "a.png", "hotspot": { "x": 0.1, "y": 0.1 } },
+ "b": { "imagePath": "b.png", "hotspot": { "x": 0.5, "y": 0.5 } }
+ }
+ }))
+ .unwrap();
+ recording_meta.project_path = directory.to_path_buf();
+ let meta = recording_meta.studio_meta().unwrap().clone();
+ RenderVideoConstants::new_with_options(
+ RenderOptions {
+ screen_size: SCREEN,
+ camera_size: None,
+ preserve_screen_alpha: false,
+ },
+ recording_meta,
+ meta,
+ )
+ .await
+ .unwrap()
+}
+
+fn wallpaper(directory: &std::path::Path) -> BackgroundSource {
+ let path = directory.join("wallpaper.png");
+ image::RgbaImage::from_fn(96, 64, |x, y| {
+ image::Rgba([(x * 2) as u8, (y * 3) as u8, 90, 255])
+ })
+ .save(&path)
+ .unwrap();
+ BackgroundSource::Wallpaper {
+ path: Some(path.to_string_lossy().into_owned()),
+ }
+}
+
+fn timeline(value: serde_json::Value) -> Option<cap_project::TimelineConfiguration> {
+ let mut timeline = serde_json::json!({ "segments": [], "zoomSegments": [] });
+ timeline
+ .as_object_mut()
+ .unwrap()
+ .extend(value.as_object().unwrap().clone());
+ Some(serde_json::from_value(timeline).unwrap())
+}
+
+fn scenarios(directory: &std::path::Path) -> Vec<(&'static str, ProjectConfiguration)> {
+ let base = || {
+ let mut project = ProjectConfiguration::default();
+ project.background.source = wallpaper(directory);
+ project.background.padding = 12.0;
+ project
+ };
+
+ let mut graded = base();
+ graded.color_correction.screen.exposure = 0.4;
+ graded.color_correction.screen.saturation = -0.3;
+
+ let mut circle = base();
+ circle.cursor.set_cursor_type(CursorType::Circle);
+
+ let mut masks = base();
+ masks.timeline = timeline(serde_json::json!({
+ "maskSegments": [
+ {
+ "start": 0.0, "end": 10.0, "maskType": "sensitive",
+ "center": { "x": 0.3, "y": 0.4 }, "size": { "x": 0.3, "y": 0.3 },
+ "feather": 0.1, "pixelation": 1024.0
+ },
+ {
+ "start": 0.0, "end": 10.0, "maskType": "sensitive",
+ "center": { "x": 0.6, "y": 0.5 }, "size": { "x": 0.25, "y": 0.2 },
+ "pixelation": 18.0
+ },
+ {
+ "start": 0.0, "end": 10.0, "maskType": "highlight",
+ "center": { "x": 0.5, "y": 0.6 }, "size": { "x": 0.4, "y": 0.3 },
+ "darkness": 0.5
+ }
+ ]
+ }));
+
+ let camera3d = |bokeh: bool| {
+ let mut project = base();
+ project.timeline = timeline(serde_json::json!({
+ "camera3dSegments": [{
+ "start": 0.0, "end": 10.0,
+ "properties": { "tiltX": -18.0, "tiltY": 14.0, "zoom": 1.8 },
+ "blur": { "mode": "radial", "strength": 12.0, "falloff": 0.5, "bokeh": bokeh }
+ }]
+ }));
+ project
+ };
+
+ let mut animated = base();
+ animated.background.source = BackgroundSource::AnimatedGradient {
+ config: Default::default(),
+ };
+ animated.background.blur = 30.0;
+ animated.color_correction.screen.contrast = 0.3;
+
+ vec![
+ ("graded", graded),
+ ("circle", circle),
+ ("masks", masks),
+ ("camera3d-gaussian", camera3d(false)),
+ ("camera3d-bokeh", camera3d(true)),
+ ("animated-blur", animated),
+ ]
+}
+
+fn first_difference(
+ actual: &cap_rendering::RenderedFrame,
+ expected: &cap_rendering::RenderedFrame,
+) -> Option<usize> {
+ if (actual.width, actual.height) != (expected.width, expected.height) {
+ return Some(0);
+ }
+ (0..actual.height as usize).find(|y| {
+ let start = y * actual.padded_bytes_per_row as usize;
+ let end = start + actual.width as usize * 4;
+ actual.data[start..end] != expected.data[start..end]
+ })
+}
+
+async fn render_fresh(
+ constants: &RenderVideoConstants,
+ project: &ProjectConfiguration,
+ frames: DecodedSegmentFrames,
+ uniforms: ProjectUniforms,
+ cursor: &CursorEvents,
+) -> cap_rendering::RenderedFrame {
+ let mut renderer = FrameRenderer::new(constants);
+ let mut layers = RendererLayers::new_for_preparing_preview(
+ &constants.device,
+ &constants.queue,
+ constants.is_software_adapter,
+ project,
+ )
+ .unwrap();
+ renderer
+ .render_immediate(frames, uniforms, cursor, true, &mut layers)
+ .await
+ .unwrap()
+}
+
+/// Layers kept across frames cache bind groups, uniform buffers and the
+/// resolved wallpaper path. Every frame must match layers built from scratch
+/// for that frame, including across a session resize.
+#[tokio::test]
+async fn reused_layers_match_fresh_layers_every_frame() {
+ let directory = tempfile::tempdir().unwrap();
+ let constants = constants(directory.path()).await;
+ let cursor = cursor_events();
+ let duration = FRAMES as f64 / FPS as f64;
+
+ for (name, mut project) in scenarios(directory.path()) {
+ if name == "masks" && cfg!(windows) && constants.is_software_adapter {
+ eprintln!("WARP renders the mask scene differently on identical runs, skipping it");
+ continue;
+ }
+ let mut renderer = FrameRenderer::new(&constants);
+ let mut layers = RendererLayers::new_for_preparing_preview(
+ &constants.device,
+ &constants.queue,
+ constants.is_software_adapter,
+ &project,
+ )
+ .unwrap();
+ let mut shared = Arc::new(project.clone());
+
+ for frame_number in 0..FRAMES {
+ if frame_number == FRAMES - 3 {
+ project.aspect_ratio = Some(AspectRatio::Square);
+ shared = Arc::new(project.clone());
+ }
+ let time = frame_number as f64 / FPS as f64;
+ let frames = screen_frame(time);
+ let zoom = ZoomTransformTimeline::from_project(&project, &cursor, duration, SCREEN);
+ let uniforms = ProjectUniforms::new(
+ &constants,
+ &shared,
+ frame_number,
+ FPS,
+ SCREEN,
+ &cursor,
+ &frames,
+ duration,
+ &zoom,
+ );
+
+ let actual = renderer
+ .render_immediate(frames.clone(), uniforms.clone(), &cursor, true, &mut layers)
+ .await
+ .unwrap();
+
+ let expected = render_fresh(&constants, &project, frames, uniforms, &cursor).await;
+
+ if let Some(row) = first_difference(&actual, &expected) {
+ panic!("{name} frame {frame_number}: row {row} differs");
+ }
+ }
+ }
+}
diff --git a/crates/scap-targets/src/platform/linux.rs b/crates/scap-targets/src/platform/linux.rs
index 1dbb6c3b9..510a37cbf 100644
--- a/crates/scap-targets/src/platform/linux.rs
+++ b/crates/scap-targets/src/platform/linux.rs
@@ -294,17 +294,7 @@ impl WindowImpl {
}
pub fn display(&self) -> Option<DisplayImpl> {
- let bounds = self.physical_bounds()?;
- let center = PhysicalPosition::new(
- bounds.position().x() + bounds.size().width() / 2.0,
- bounds.position().y() + bounds.size().height() / 2.0,
- );
-
- DisplayImpl::list().into_iter().find(|display| {
- display
- .physical_bounds()
- .is_some_and(|display_bounds| display_bounds.contains_point(center))
- })
+ display_for_bounds(self.physical_bounds()?, &DisplayImpl::list())
}
pub fn name(&self) -> Option<String> {
@@ -322,6 +312,24 @@ impl WindowImpl {
}
}
+fn display_for_bounds(bounds: PhysicalBounds, displays: &[DisplayImpl]) -> Option<DisplayImpl> {
+ displays
+ .iter()
+ .filter_map(|display| {
+ let display_bounds = display.physical_bounds()?;
+ let left = bounds.position().x().max(display_bounds.position().x());
+ let top = bounds.position().y().max(display_bounds.position().y());
+ let right = (bounds.position().x() + bounds.size().width())
+ .min(display_bounds.position().x() + display_bounds.size().width());
+ let bottom = (bounds.position().y() + bounds.size().height())
+ .min(display_bounds.position().y() + display_bounds.size().height());
+ let area = (right - left).max(0.0) * (bottom - top).max(0.0);
+ (area > 0.0).then_some((*display, area))
+ })
+ .max_by(|(_, a), (_, b)| a.total_cmp(b))
+ .map(|(display, _)| display)
+}
+
fn filter_window_candidates(windows: Vec<Window>) -> Vec<WindowImpl> {
windows
.into_iter()
@@ -491,3 +499,72 @@ fn get_cursor_position() -> Option<PhysicalPosition> {
reply.root_y.into(),
))
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn display(id: u32, x: i32, y: i32, width: u32, height: u32) -> DisplayImpl {
+ DisplayImpl {
+ id,
+ x,
+ y,
+ width,
+ height,
+ refresh_rate: 60.0,
+ logical_bounds: None,
+ wayland_uuid: None,
+ }
+ }
+
+ fn bounds(x: f64, y: f64, width: f64, height: f64) -> PhysicalBounds {
+ PhysicalBounds::new(
+ PhysicalPosition::new(x, y),
+ PhysicalSize::new(width, height),
+ )
+ }
+
+ #[test]
+ fn window_center_in_monitor_gap_still_resolves_to_visible_display() {
+ let displays = [display(0, 0, 0, 1000, 800), display(1, 1200, 0, 1000, 800)];
+ let window = bounds(900.0, 100.0, 350.0, 200.0);
+ assert_eq!(display_for_bounds(window, &displays).unwrap().id, 0);
+ }
+
+ #[test]
+ fn window_uses_largest_overlap_on_offset_monitors() {
+ let displays = [
+ display(0, -1920, 0, 1920, 1080),
+ display(1, 0, -900, 1440, 900),
+ ];
+ let window = bounds(-150.0, -100.0, 400.0, 300.0);
+ assert_eq!(display_for_bounds(window, &displays).unwrap().id, 0);
+ assert_eq!(
+ display_for_bounds(window, &[displays[1], displays[0]])
+ .unwrap()
+ .id,
+ 0
+ );
+ assert_eq!(
+ display_for_bounds(bounds(-150.0, -200.0, 400.0, 300.0), &displays)
+ .unwrap()
+ .id,
+ 1
+ );
+ }
+
+ #[test]
+ fn nonoverlapping_and_edge_touching_windows_have_no_display() {
+ let displays = [display(0, 0, 0, 1920, 1080)];
+ for window in [
+ bounds(-320.0, 100.0, 320.0, 240.0),
+ bounds(1920.0, 100.0, 320.0, 240.0),
+ bounds(100.0, -240.0, 320.0, 240.0),
+ bounds(100.0, 1080.0, 320.0, 240.0),
+ bounds(-400.0, -400.0, 320.0, 240.0),
+ ] {
+ assert!(display_for_bounds(window, &displays).is_none());
+ }
+ assert!(display_for_bounds(bounds(0.0, 0.0, 320.0, 240.0), &[]).is_none());
+ }
+}
diff --git a/crates/scap-targets/tests/linux_targets.rs b/crates/scap-targets/tests/linux_targets.rs
new file mode 100644
index 000000000..73327eebf
--- /dev/null
+++ b/crates/scap-targets/tests/linux_targets.rs
@@ -0,0 +1,166 @@
+#![cfg(target_os = "linux")]
+
+use std::{
+ io::{BufRead, BufReader},
+ process::{Child, Command, Stdio},
+};
+
+use scap_targets::{Display, Window, WindowId};
+use x11rb::{
+ COPY_DEPTH_FROM_PARENT,
+ connection::Connection,
+ protocol::xproto::{
+ AtomEnum, ConfigureWindowAux, ConnectionExt as _, CreateWindowAux, PropMode, WindowClass,
+ },
+ wrapper::ConnectionExt as _,
+};
+
+struct Xvfb(Child);
+
+impl Xvfb {
+ fn start() -> Option<(Self, String)> {
+ let (reader, writer) = std::io::pipe().ok()?;
+ let child = Command::new("Xvfb")
+ .args([
+ "-displayfd",
+ "1",
+ "-screen",
+ "0",
+ "1280x720x24",
+ "-nolisten",
+ "tcp",
+ ])
+ .stdin(Stdio::null())
+ .stdout(writer)
+ .stderr(Stdio::null())
+ .spawn();
+ let child = match child {
+ Ok(child) => child,
+ Err(error) => {
+ eprintln!("skipping X11 targets test: cannot start Xvfb: {error}");
+ return None;
+ }
+ };
+ let server = Self(child);
+ let mut line = String::new();
+ BufReader::new(reader)
+ .read_line(&mut line)
+ .expect("Xvfb display number");
+ let number: u32 = line.trim().parse().expect("Xvfb display number");
+ Some((server, format!(":{number}")))
+ }
+}
+
+impl Drop for Xvfb {
+ fn drop(&mut self) {
+ let _ = self.0.kill();
+ let _ = self.0.wait();
+ }
+}
+
+#[test]
+fn partially_offscreen_window_remains_a_recording_target() {
+ let Some((_server, display)) = Xvfb::start() else {
+ return;
+ };
+ // SAFETY: this binary has one test, which sets the variables before calling scap_targets.
+ unsafe {
+ std::env::set_var("DISPLAY", &display);
+ std::env::set_var("XDG_SESSION_TYPE", "x11");
+ }
+ let (conn, screen_num) = x11rb::connect(Some(&display)).unwrap();
+ let screen = &conn.setup().roots[screen_num];
+ let id = conn.generate_id().unwrap();
+ conn.create_window(
+ COPY_DEPTH_FROM_PARENT,
+ id,
+ screen.root,
+ 100,
+ 100,
+ 320,
+ 240,
+ 0,
+ WindowClass::INPUT_OUTPUT,
+ 0,
+ &CreateWindowAux::new(),
+ )
+ .unwrap()
+ .check()
+ .unwrap();
+ conn.change_property8(
+ PropMode::REPLACE,
+ id,
+ AtomEnum::WM_NAME,
+ AtomEnum::STRING,
+ b"Cap offscreen target regression",
+ )
+ .unwrap()
+ .check()
+ .unwrap();
+ conn.change_property8(
+ PropMode::REPLACE,
+ id,
+ AtomEnum::WM_CLASS,
+ AtomEnum::STRING,
+ b"cap-target-fixture\0CapTargetFixture\0",
+ )
+ .unwrap()
+ .check()
+ .unwrap();
+ conn.map_window(id).unwrap().check().unwrap();
+
+ let target_id: WindowId = id.to_string().parse().unwrap();
+ let display_id = Display::primary().id();
+ let width = i32::from(screen.width_in_pixels);
+ let height = i32::from(screen.height_in_pixels);
+ for (x, y) in [
+ (100, 100),
+ (-250, 100),
+ (width - 70, 100),
+ (100, -200),
+ (100, height - 40),
+ (-250, -200),
+ ] {
+ conn.configure_window(id, &ConfigureWindowAux::new().x(x).y(y))
+ .unwrap()
+ .check()
+ .unwrap();
+ let target = Window::from_id(&target_id)
+ .unwrap_or_else(|| panic!("visible window at ({x}, {y}) must remain selectable"));
+ assert_eq!(target.owner_name().as_deref(), Some("cap-target-fixture"));
+ assert_eq!(target.display().unwrap().id(), display_id);
+ let bounds = target.display_relative_logical_bounds().unwrap();
+ assert_eq!(bounds.position().x(), f64::from(x));
+ assert_eq!(bounds.position().y(), f64::from(y));
+ assert_eq!(bounds.size().width(), 320.0);
+ assert_eq!(bounds.size().height(), 240.0);
+ conn.warp_pointer(
+ x11rb::NONE,
+ screen.root,
+ 0,
+ 0,
+ 0,
+ 0,
+ i16::try_from(x.max(0) + 10).unwrap(),
+ i16::try_from(y.max(0) + 10).unwrap(),
+ )
+ .unwrap()
+ .check()
+ .unwrap();
+ assert_eq!(Window::get_topmost_at_cursor().unwrap().id(), target_id);
+ }
+
+ conn.configure_window(id, &ConfigureWindowAux::new().x(-320).y(100))
+ .unwrap()
+ .check()
+ .unwrap();
+ assert!(Window::from_id(&target_id).is_none());
+
+ conn.configure_window(id, &ConfigureWindowAux::new().x(100).y(100))
+ .unwrap()
+ .check()
+ .unwrap();
+ assert!(Window::from_id(&target_id).is_some());
+ conn.destroy_window(id).unwrap().check().unwrap();
+ assert!(Window::from_id(&target_id).is_none());
+}
diff --git a/crates/verify/Cargo.toml b/crates/verify/Cargo.toml
index f81f0e553..9266ed705 100644
--- a/crates/verify/Cargo.toml
+++ b/crates/verify/Cargo.toml
@@ -33,7 +33,7 @@ cidre = { workspace = true }
cap-recording = { path = "../recording", features = ["test-utils"] }
cap-editor = { path = "../editor", features = ["test-hooks"] }
cap-audio = { path = "../audio" }
-cap-export = { path = "../export" }
+cap-export = { path = "../export", features = ["test-hooks"] }
cap-rendering = { path = "../rendering" }
cpal = { workspace = true }
tempfile = "3"
diff --git a/crates/verify/expectations.toml b/crates/verify/expectations.toml
index ea47214b2..089cead29 100644
--- a/crates/verify/expectations.toml
+++ b/crates/verify/expectations.toml
@@ -112,20 +112,6 @@ reason = "NEW: same anchor, loopback-style system audio (WASAPI delivers packets
link = ""
-[[xfail]]
-case = "export/instant-gated-*mic*/output/mp4/native"
-class = "sync-mic"
-bound = { count = 1, offset-ms = [5, 34] }
-reason = "NEW: after the start gate opens, the screen and audio pipelines each take their own first admitted frame as zero, so audio is late by up to one video frame (33.3 ms at 30 fps; 12.6 to 17.2 ms measured with the gate opening between frames)"
-link = ""
-
-[[xfail]]
-case = "export/instant-gated-system-*/output/mp4/native"
-class = "sync-system"
-bound = { count = 1, offset-ms = [5, 34] }
-reason = "NEW: the gate's per-pipeline zero, as the gated sync-mic entry: system audio late by up to one video frame (12.2 to 15.1 ms measured)"
-link = ""
-
[[xfail]]
case = "export/instant-gated-system-only/output/mp4/native"
class = "audio-system"
@@ -134,21 +120,6 @@ bound = { count = 1, missing = 2, lost = 2 }
reason = "NEW: gated loopback-only system audio sometimes loses markers (2 of 11 lost in 1 of 2 fresh recordings, with the track 958 ms early). Bound: at most 2 markers lost, none unexpected"
link = ""
-[[xfail]]
-case = "export/instant/output/mp4/native"
-class = "sync-mic"
-bound = { count = 1, offset-ms = [5, 34] }
-reason = "NEW: see export/instant-gated-*: mic audio up to one video frame late after the start gate (14.2 to 15.3 ms measured)"
-link = ""
-
-
-[[xfail]]
-case = "export/instant/output/mp4/native"
-class = "sync-system"
-bound = { count = 1, offset-ms = [5, 34], mic-offset-ms = [5, 34], system-minus-mic-ms = [-1, 1] }
-reason = "NEW: the mixer places system audio on the microphone's timeline (system minus mic 0 ms), so it shares the gated mic offset of the sync-mic entry above (13.3 to 15.4 ms measured)"
-link = ""
-
[[xfail]]
case = "*/basic/transition/*60*/avf"
class = "stale"
@@ -318,66 +289,15 @@ link = "https://github.com/CapSoftware/Cap/actions/runs/38845236912"
# First picture after a static screen. During a scripted hold ScreenCaptureKit
# delivers only Idle status buffers; its first Complete frame after the hold
-# arrives 70 to 82 ms after the new picture was presented (steady state 23 to
-# 31 ms after each present), and it already shows the next picture. Frame-level
-# logs (run 38745219281, macos-15: every SCK screen buffer with its
-# SCStreamFrameInfoStatus, every Cap send, every mux call) show 0 Complete
-# frames between the post-hold picture and the next one, and 1797 Complete
-# frames = 1797 Cap sends = 1797 mux calls: the picture never reaches Cap's
-# capture callback and Cap drops nothing. The stimulus presents through the
-# standard Core Animation path, so a user's first change after a static screen
-# is affected the same way: it is recorded ~50 to 80 ms late and anything
-# shown for less than that is lost. Whether a given post-hold picture is lost
-# depends on whether the 70 to 82 ms resume outlasts that picture, so the
-# entries are bounded by the number of holds. Blacksmith VMs resume in time
-# (these cases pass there), so the entries apply to hosted runners and
-# developer Macs only (hosts).
-
-[[xfail]]
-case = "recording/studio-screen/mp4-holds"
-class = "missing-after-hold"
-platforms = ["macos"]
-hosts = ["github-hosted", "local"]
-bound = { count = 3, screen-pictures = 3 }
-reason = "NEW (ScreenCaptureKit idle resume, see above): the first 20 fps picture after each of the 3 holds is never delivered by ScreenCaptureKit (first Complete frame 80 to 82 ms after it was presented); runners 18 of 18 holds in 6 runs. Bound: one picture per hold"
-link = "https://github.com/CapSoftware/Cap/actions/runs/38745219281"
-
-[[xfail]]
-case = "recording/instant/long-holds"
-class = "missing-after-hold"
-platforms = ["macos"]
-flaky = true
-hosts = ["github-hosted", "local"]
-bound = { count = 2, screen-pictures = 2 }
-reason = "NEW (ScreenCaptureKit idle resume, see above): the first 15 fps picture after a hold is never delivered when the resume takes longer than its 67 ms (first Complete frame 70 to 74 ms after it was presented); runners: hold at 6 s 6 of 6 runs, hold at 18 s 4 of 6. Flaky: when ScreenCaptureKit happens to resume within the picture's 67 ms both pictures are caught (fix-video-timestamps track). Bound: one picture per hold (2 holds)"
-link = "https://github.com/CapSoftware/Cap/actions/runs/38745219281"
-
-[[xfail]]
-case = "recording/studio-camera/long"
-class = "missing-after-hold"
-platforms = ["macos"]
-flaky = true
-hosts = ["github-hosted", "local"]
-bound = { count = 1, screen-pictures = 1 }
-reason = "NEW (ScreenCaptureKit idle resume, see above): the first 15 fps picture after the 3 s hold is missing when the resume takes longer than its 67 ms; runners 5 of 6 runs (flaky because the 70 to 82 ms resume straddles the 67 ms picture). Bound: the one hold's picture"
-link = "https://github.com/CapSoftware/Cap/actions/runs/38745219281"
-# Crash safety (crates/verify/tests/crash.rs). Measured on Blacksmith runners
-# (run 38850661948: macos-15 libx264, ubuntu-2404, windows-2025) with the suite
-# merged into main 3ec0c9497; each fix has its own proof branch
-# ci/fixproof-crash-<a|b1|c|ce> (runs 38850662667, 38850661576, 38850663436,
-# 38850662102), which removes exactly its entries. Fixes: #2640 (bug A),
-# #2641 (bug B1), #2643 (bug C), #2644 (bug E).
-
-[[xfail]]
-case = "crash/recorder-kill/inproc/static-screen"
-class = "loss-time"
-bound = { count = 1, secs = [2.5, 20.5] }
-reason = "NEW (bug B1): fragments are cut only at keyframes and every recording encoder's keyframe interval is a frame count (g = 2 s x nominal fps; libx264 keyint 60 here, VideoToolbox 12), so when the screen delivers 3 frames per second a fragment spans 20 s of capture and a crash after 11 s loses all of it (3 of 3 platforms; macOS runners use libx264). Bound: at most one 20 s fragment of capture"
-link = "https://github.com/CapSoftware/Cap/pull/2641"
-
-[[xfail]]
-case = "crash/recorder-kill/inproc/static-screen"
-class = "missing"
-bound = { count = 1 }
-reason = "NEW (bug B1, see loss-time): no fragment of the static recording is complete when the recorder dies, so recovery has nothing (no frames)"
-link = "https://github.com/CapSoftware/Cap/pull/2641"
+# can arrive 70 to 82 ms after the new picture was presented (steady state 23
+# to 31 ms after each present), when it already shows the next picture.
+# Frame-level logs (run 38745219281, macos-15) showed the picture never
+# reaches Cap's capture callback and Cap drops nothing. GitHub-hosted runners
+# and developer Macs resume that late, and Blacksmith VMs have since about
+# 2026-10-23 22:00 UTC (runs 40054572525 and 40056780767). No xfail covers
+# it: the suite excuses a post-hold picture only when the stimulus's own
+# ScreenCaptureKit stream also received no frame while it was on screen,
+# and then requires the first later picture that stream did receive
+# ("capture-service drops" in the case notes; see `missing_required` in
+# tests/recording.rs). A post-hold picture the stimulus's stream received
+# but the recording lacks still fails as missing-after-hold.
diff --git a/crates/verify/src/fixture.rs b/crates/verify/src/fixture.rs
index de6ac9197..8c5da2342 100644
--- a/crates/verify/src/fixture.rs
+++ b/crates/verify/src/fixture.rs
@@ -412,6 +412,7 @@ pub fn write_fixture(spec: &FixtureSpec, dir: &Path) -> anyhow::Result<()> {
start_time: Some(a.start_time),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
})
}
None => None,
diff --git a/crates/verify/src/pipeline.rs b/crates/verify/src/pipeline.rs
index 3e4e091c0..c41fde2ce 100644
--- a/crates/verify/src/pipeline.rs
+++ b/crates/verify/src/pipeline.rs
@@ -17,7 +17,7 @@ use cap_project::{AudioMeta, MultipleSegment, VideoMeta};
use cap_recording::{
AudioFrame, ChannelAudioSource, ChannelAudioSourceConfig, ChannelVideoSource,
ChannelVideoSourceConfig, OutputPipeline,
- ffmpeg::{FFmpegVideoFrame, Mp4Muxer, OggMuxer},
+ ffmpeg::{FFmpegVideoFrame, Mp4Muxer, OggMuxer, OggMuxerConfig},
};
use cap_timestamp::{Timestamp, Timestamps};
use relative_path::RelativePathBuf;
@@ -376,7 +376,7 @@ async fn record_segment(
let pipeline = OutputPipeline::builder(dir.join(rel("audio-input.ogg").as_str()))
.with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(mono_info, rx))
.with_timestamps(timestamps)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await
.map_err(|e| anyhow!("mic pipeline: {e}"))?;
mic = Some((pipeline, emit, rel("audio-input.ogg")));
@@ -415,7 +415,7 @@ async fn record_segment(
.with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(stereo_info, rx))
.with_timestamps(timestamps)
.with_audio_anchor(cap_recording::AudioAnchor::PipelineEpoch)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await
.map_err(|e| anyhow!("system audio pipeline: {e}"))?;
system = Some((pipeline, emit, rel("system_audio.ogg")));
@@ -495,6 +495,7 @@ async fn record_segment(
overlap_dropped_frames: s.overlap_dropped_frames,
startup_overlap_drops: s.startup_overlap_drops,
}),
+ digital_silence_ms: finished.digital_silence_ms,
};
if name == "mic" {
mic_meta = Some(meta);
@@ -606,9 +607,13 @@ pub async fn record_instant(spec: &InstantSpec, dir: &Path) -> anyhow::Result<()
std::fs::create_dir_all(&content)?;
let timestamps = Timestamps::now();
let base = timestamps.instant();
- let gate = spec
- .arm_at
- .map(|_| cap_recording::RecordingStartGate::new());
+ // Mirrors the product's Instant gate: the screen track starts at the arm
+ // point, where the epoch-anchored audio below starts.
+ let gate = spec.arm_at.map(|_| {
+ let gate = cap_recording::RecordingStartGate::new();
+ gate.set_video_origin_at_arm();
+ gate
+ });
let mut emitted = Emitted {
fps: spec.screen.fps,
..Default::default()
@@ -862,7 +867,7 @@ pub async fn record_epoch_audio(
.with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(stereo_info, rx))
.with_timestamps(timestamps)
.with_audio_anchor(cap_recording::AudioAnchor::PipelineEpoch)
- .build::<OggMuxer>(())
+ .build::<OggMuxer>(OggMuxerConfig::default())
.await
.map_err(|e| anyhow!("epoch audio pipeline: {e}"))?;
{
diff --git a/crates/verify/src/report.rs b/crates/verify/src/report.rs
index c35b36d83..102002911 100644
--- a/crates/verify/src/report.rs
+++ b/crates/verify/src/report.rs
@@ -645,43 +645,4 @@ mod tests {
Some("track absent")
);
}
- #[test]
- fn instant_system_audio_expectation_requires_the_microphone_timeline() {
- let entries = cap_verify_expect::parse(include_str!("../expectations.toml")).unwrap();
- let entries: Vec<_> = entries
- .iter()
- .filter(|entry| {
- entry.class == "sync-system" && entry.case == "export/instant/output/mp4/native"
- })
- .collect();
- assert_eq!(entries.len(), 1);
- assert!(!entries[0].flaky);
- let metrics = |offset: f64, mic: f64| {
- BTreeMap::from([
- ("count".into(), cap_verify_expect::Range::at(1.0)),
- ("offset-ms".into(), cap_verify_expect::Range::at(offset)),
- ("mic-offset-ms".into(), cap_verify_expect::Range::at(mic)),
- (
- "system-minus-mic-ms".into(),
- cap_verify_expect::Range::at(((offset - mic) * 10.0_f64).round() / 10.0),
- ),
- ])
- };
- let accepts = |metrics: &BTreeMap<String, cap_verify_expect::Range>| {
- cap_verify_expect::within(&entries[0].bound, Some(metrics)).is_ok()
- };
- for (offset, mic) in [(14.0, 14.0), (13.3, 13.3), (15.4, 15.0)] {
- assert!(accepts(&metrics(offset, mic)));
- }
- // The -60 ms and -6.7 ms mixer modes, and system audio off the mic.
- for (offset, mic) in [(-46.1, 13.9), (7.8, 14.5), (16.0, 14.5), (14.0, 35.0)] {
- assert!(!accepts(&metrics(offset, mic)));
- }
- let mut absent = metrics(14.0, 14.0);
- absent.remove("system-minus-mic-ms");
- assert!(!accepts(&absent));
- for platform in ["macos", "windows", "linux"] {
- assert!(entries[0].matches("export/instant/output/mp4/native", platform));
- }
- }
}
diff --git a/crates/verify/tests/audio.rs b/crates/verify/tests/audio.rs
index 714ee5fbb..8789c9697 100644
--- a/crates/verify/tests/audio.rs
+++ b/crates/verify/tests/audio.rs
@@ -161,7 +161,7 @@ const FPS: u32 = 30;
const RES: (u32, u32) = (320, 180);
fn export_case(spec: &FixtureSpec, variant: &Variant, path: ExportPath) -> Option<String> {
- export_case_at(spec, variant, path, FPS)
+ export_case_at(spec, variant, path, FPS, None)
}
fn export_case_at(
@@ -169,6 +169,7 @@ fn export_case_at(
variant: &Variant,
path: ExportPath,
fps: u32,
+ video_encoder_failure_at: Option<u32>,
) -> Option<String> {
let id = format!("audio/{}/{}/{}{fps}", spec.name, variant.edit, path.label());
if !report::selected(&id) {
@@ -180,13 +181,14 @@ fn export_case_at(
(variant.apply)(&mut config);
let mut case = Case::new(&id);
let dir = common::scratch_dir(&id);
- match rt.block_on(audio_support::export(
+ match rt.block_on(audio_support::export_failing_encoder_at(
&project,
config.clone(),
path,
fps,
RES,
dir.path(),
+ video_encoder_failure_at,
)) {
Err(e) => case.problem("error", format!("export failed: {e}")),
Ok(out) => match path {
@@ -201,6 +203,17 @@ fn export_case_at(
);
}
_ => {
+ // libx264 tags its stream with a version SEI; the restarted
+ // export must come from it.
+ if video_encoder_failure_at.is_some()
+ && !std::fs::read(&out)
+ .is_ok_and(|bytes| bytes.windows(11).any(|w| w == b"x264 - core"))
+ {
+ case.problem(
+ "error",
+ "export after the video encoder failed was not encoded by libx264",
+ );
+ }
let model = audio_support::audio_model(&project, &config);
match audio_support::exported_mono(&out) {
Err(e) => case.problem("error", e),
@@ -266,12 +279,31 @@ fn export_studio_sound_low_fps() {
},
ExportPath::App,
10,
+ None,
)
.into_iter()
.collect();
assert!(failures.is_empty(), "{}", failures.join("\n"));
}
+/// A hardware video encoder can fail mid-export when the media engine is
+/// busy or resets (VideoToolbox error -19092 on shared macOS runners). The
+/// export restarts on libx264 and must still match the audio and video
+/// models, on the streaming (app) and in-memory (plain) audio paths.
+#[test]
+fn encoder_fallback() {
+ let spec = audio_fixture::mix(report::tier());
+ let variant = Variant {
+ edit: "encoder-fallback",
+ apply: full,
+ };
+ let failures: Vec<String> = [ExportPath::App, ExportPath::Plain]
+ .into_iter()
+ .filter_map(|path| export_case_at(&spec, &variant, path, FPS, Some(FPS * 3)))
+ .collect();
+ assert!(failures.is_empty(), "{}", failures.join("\n"));
+}
+
#[test]
fn studio_sound_music() {
let spec = audio_fixture::mix(report::tier());
diff --git a/crates/verify/tests/audio_support/mod.rs b/crates/verify/tests/audio_support/mod.rs
index 07e8500e3..c92fe3db9 100644
--- a/crates/verify/tests/audio_support/mod.rs
+++ b/crates/verify/tests/audio_support/mod.rs
@@ -265,6 +265,20 @@ pub async fn export(
fps: u32,
resolution: (u32, u32),
dir: &Path,
+) -> Result<PathBuf, String> {
+ export_failing_encoder_at(project, config, path, fps, resolution, dir, None).await
+}
+
+/// [`export`], with the MP4 video encoder failing after
+/// `video_encoder_failure_at` frames as a hardware encoder does at runtime.
+pub async fn export_failing_encoder_at(
+ project: &Project,
+ config: ProjectConfiguration,
+ path: ExportPath,
+ fps: u32,
+ resolution: (u32, u32),
+ dir: &Path,
+ video_encoder_failure_at: Option<u32>,
) -> Result<PathBuf, String> {
let out = dir.join(format!("out.{}", path.extension()));
let builder = cap_export::ExporterBase::builder(project.path.clone())
@@ -282,17 +296,23 @@ pub async fn export(
match path {
ExportPath::App => {
let cancel = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
- let base = builder
+ let mut base = builder
.build_for_mp4(cancel)
.await
.map_err(|e| format!("exporter build: {e}"))?;
+ if let Some(frames) = video_encoder_failure_at {
+ base.fail_video_encoder_at(frames);
+ }
mp4.export_prepared(base, |_| true).await
}
ExportPath::Plain => {
- let base = builder
+ let mut base = builder
.build()
.await
.map_err(|e| format!("exporter build: {e}"))?;
+ if let Some(frames) = video_encoder_failure_at {
+ base.fail_video_encoder_at(frames);
+ }
mp4.export(base, |_| true).await
}
ExportPath::Mov => {
diff --git a/crates/verify/tests/chaos_support/fragments.rs b/crates/verify/tests/chaos_support/fragments.rs
index 6ef3366c5..f3976f917 100644
--- a/crates/verify/tests/chaos_support/fragments.rs
+++ b/crates/verify/tests/chaos_support/fragments.rs
@@ -69,6 +69,7 @@ async fn record(dir: &Path) -> anyhow::Result<()> {
.with_timestamps(timestamps)
.build::<FragmentedAudioMuxer>(FragmentedAudioMuxerConfig {
shared_pause_state: None,
+ write_behind: false,
})
.await
.map_err(|e| anyhow!("mic pipeline: {e:#}"))?;
diff --git a/crates/verify/tests/chaos_support/project.rs b/crates/verify/tests/chaos_support/project.rs
index 043c5471d..d9a489afa 100644
--- a/crates/verify/tests/chaos_support/project.rs
+++ b/crates/verify/tests/chaos_support/project.rs
@@ -4,10 +4,7 @@
use std::{
path::{Path, PathBuf},
- sync::{
- Arc,
- atomic::{AtomicUsize, Ordering},
- },
+ sync::Arc,
time::Duration,
};
@@ -965,18 +962,25 @@ pub fn open(dir: &Path) -> anyhow::Result<Verdict> {
)))
}
+const PREVIEW_FRAMES: [u32; 6] = [0, 1, 29, 59, 10_000, u32::MAX];
+
+// The scripted sequence below takes about 2.3 s. On a loaded macOS runner the
+// first decode can miss its 2 s deadline (crates/rendering/src/decoder) and
+// leave the sequence without a frame, so a usable project that has delivered
+// nothing yet gets the same preview requests again until a frame lands or
+// this deadline passes. A project that never delivers still fails, only later.
+const FIRST_FRAME_DEADLINE: Duration = Duration::from_secs(25);
+const FIRST_FRAME_RETRY: Duration = Duration::from_secs(1);
+
/// The editor: `EditorInstance` (decoders, renderer, offline audio), preview
/// frames at hostile positions, playback, seeks, stop and dispose.
pub async fn editor(dir: &Path, expect_frames: bool) -> anyhow::Result<Verdict> {
- let rendered = Arc::new(AtomicUsize::new(0));
- let counter = rendered.clone();
+ let (delivered, frames) = tokio::sync::watch::channel(0usize);
let (audio, _sink) = cap_editor::AudioOutput::new_offline();
let instance = match cap_editor::EditorInstance::new_with_audio_output(
dir.to_path_buf(),
|_| {},
- Box::new(move |_, _| {
- counter.fetch_add(1, Ordering::Relaxed);
- }),
+ Box::new(move |_, _| delivered.send_modify(|n| *n += 1)),
None,
Arc::new(audio),
)
@@ -986,7 +990,7 @@ pub async fn editor(dir: &Path, expect_frames: bool) -> anyhow::Result<Verdict>
Err(e) => return Ok(Verdict::rejected(format!("editor: {e}"))),
};
let resolution = XY::new(320, 180);
- for frame in [0, 1, 29, 59, 10_000, u32::MAX] {
+ for frame in PREVIEW_FRAMES {
let _ = instance.preview_tx.send(Some((frame, 30, resolution)));
tokio::time::sleep(Duration::from_millis(150)).await;
}
@@ -998,15 +1002,46 @@ pub async fn editor(dir: &Path, expect_frames: bool) -> anyhow::Result<Verdict>
instance.seek_playback(frame).await;
tokio::time::sleep(Duration::from_millis(80)).await;
}
+ if expect_frames && *frames.borrow() == 0 {
+ await_first_frame(&instance, frames.clone(), resolution).await;
+ }
instance.dispose().await;
- let n = rendered.load(Ordering::Relaxed);
+ let n = *frames.borrow();
Ok(if n == 0 && expect_frames {
- Verdict::Wrong("the editor opened but delivered no frame".into())
+ Verdict::Wrong(format!(
+ "the editor opened but delivered no frame within {} s",
+ FIRST_FRAME_DEADLINE.as_secs()
+ ))
} else {
Verdict::Ok(format!("{n} frames delivered"))
})
}
+async fn await_first_frame(
+ instance: &cap_editor::EditorInstance,
+ mut frames: tokio::sync::watch::Receiver<usize>,
+ resolution: XY<u32>,
+) {
+ let mut playing = instance.playback_watch();
+ let requests = async {
+ for &frame in PREVIEW_FRAMES.iter().cycle() {
+ if playing.wait_for(|active| !*active).await.is_err() {
+ break;
+ }
+ let _ = instance.preview_tx.send(Some((frame, 30, resolution)));
+ tokio::time::sleep(FIRST_FRAME_RETRY).await;
+ }
+ std::future::pending::<()>().await;
+ };
+ let _ = tokio::time::timeout(FIRST_FRAME_DEADLINE, async {
+ tokio::select! {
+ _ = frames.wait_for(|n| *n > 0) => {}
+ () = requests => {}
+ }
+ })
+ .await;
+}
+
#[derive(Clone, Copy, Debug)]
pub enum Format {
Mp4 { bpp: Option<f32> },
diff --git a/crates/verify/tests/chaos_support/record.rs b/crates/verify/tests/chaos_support/record.rs
index b9562cb0d..8dc1ece6b 100644
--- a/crates/verify/tests/chaos_support/record.rs
+++ b/crates/verify/tests/chaos_support/record.rs
@@ -16,8 +16,8 @@ use cap_recording::{
ChannelVideoSourceConfig, OutputPipeline,
ffmpeg::{
DashSegmentedAudioMuxer, DashSegmentedAudioMuxerConfig, FFmpegVideoFrame,
- FragmentedAudioMuxer, FragmentedAudioMuxerConfig, Mp4Muxer, OggMuxer, SegmentedVideoMuxer,
- SegmentedVideoMuxerConfig,
+ FragmentedAudioMuxer, FragmentedAudioMuxerConfig, Mp4Muxer, OggMuxer, OggMuxerConfig,
+ SegmentedVideoMuxer, SegmentedVideoMuxerConfig,
},
};
use cap_timestamp::{Timestamp, Timestamps};
@@ -882,11 +882,12 @@ async fn run_audio(
second = Some(tx2);
}
let built = match muxer {
- AudioMuxerKind::Ogg => builder.build::<OggMuxer>(()).await,
+ AudioMuxerKind::Ogg => builder.build::<OggMuxer>(OggMuxerConfig::default()).await,
AudioMuxerKind::FragmentedM4a => {
builder
.build::<FragmentedAudioMuxer>(FragmentedAudioMuxerConfig {
shared_pause_state: None,
+ write_behind: false,
})
.await
}
@@ -1112,10 +1113,11 @@ async fn run_mic(ctx: &Ctx, script: &MicScript, fragmented: bool) -> anyhow::Res
builder
.build::<FragmentedAudioMuxer>(FragmentedAudioMuxerConfig {
shared_pause_state: None,
+ write_behind: false,
})
.await
} else {
- builder.build::<OggMuxer>(()).await
+ builder.build::<OggMuxer>(OggMuxerConfig::default()).await
};
let pipeline = match built {
Ok(p) => p,
diff --git a/crates/verify/tests/crash.rs b/crates/verify/tests/crash.rs
index 33c3b8c20..68e534073 100644
--- a/crates/verify/tests/crash.rs
+++ b/crates/verify/tests/crash.rs
@@ -18,7 +18,7 @@
//! time. Frames lost to a failure must be the newest ones and fit in the
//! fragment being written: one GOP (`GOP_SECS` x fps frames, the encoder's
//! keyframe interval, so the longest a segment can be when frames arrive at
-//! the nominal rate) plus `IN_FLIGHT` frames still inside the pipeline.
+//! the nominal rate) plus the writer's frames still inside the pipeline.
mod common;
@@ -42,6 +42,7 @@ use cap_verify::{
report::{self, Case, Tier},
video_code::{self, FrameMarker, StreamKind},
};
+use tracing_subscriber::fmt::writer::MakeWriterExt;
const FPS: u32 = 30;
const WIDTH: u32 = 640;
@@ -57,6 +58,34 @@ const GOP: u32 = (GOP_SECS as u32) * FPS;
/// The emitter runs at the nominal rate on an idle encoder, so the channel
/// holds at most one frame at a time.
const IN_FLIGHT: u32 = 3;
+
+fn recorder_in_flight(arm: Arm, encoder: &str) -> u32 {
+ // VideoToolbox can retain four frames before emitting their packets,
+ // in addition to the duration holder, active encode and channel slots.
+ // The OOP writer drains its received packets after recorder EOF.
+ IN_FLIGHT + u32::from(encoder == "h264_videotoolbox" && arm == Arm::InProcess) * 4
+}
+fn selected_encoder(log: &str) -> Option<&str> {
+ log.lines().find_map(|line| {
+ if line.contains("Selected hardware H264 encoder with zero-copy VideoToolbox input") {
+ Some("h264_videotoolbox")
+ } else {
+ [
+ "Selected hardware H264 encoder ",
+ "Using SOFTWARE H264 encoder (high CPU usage expected) ",
+ "Using SOFTWARE encoder for high throughput (hardware cannot keep up at this resolution/fps) ",
+ ]
+ .into_iter()
+ .find_map(|message| line.split_once(message))
+ .and_then(|(_, fields)| {
+ fields
+ .split_whitespace()
+ .find_map(|field| field.strip_prefix("encoder="))
+ })
+ }
+ })
+}
+
/// Recorded length of every case.
const SECONDS: f64 = 8.0;
/// Truth timestamps vs presentation times: the encoder time base and the
@@ -182,7 +211,10 @@ mod source {
Arm::InProcess => {
builder
.build::<ObservedMuxer<MacOSFragmentedM4SMuxer>>(
- MacOSFragmentedM4SMuxerConfig::default(),
+ MacOSFragmentedM4SMuxerConfig {
+ timed_fragments: true,
+ ..Default::default()
+ },
)
.await
}
@@ -245,6 +277,7 @@ mod source {
// The Studio display config (capture_pipeline.rs, X11Capture):
// 2 s segments.
.build::<ObservedMuxer<SegmentedVideoMuxer>>(SegmentedVideoMuxerConfig {
+ timed_fragments: true,
segment_duration: Duration::from_secs(2),
..Default::default()
})
@@ -472,7 +505,6 @@ fn crash_child_recorder() {
let Ok(spec) = std::env::var(CHILD_ENV) else {
return;
};
- common::init();
let fields: BTreeMap<&str, &str> = spec
.split(';')
.filter_map(|kv| kv.split_once('='))
@@ -482,6 +514,25 @@ fn crash_child_recorder() {
let truth = PathBuf::from(fields["truth"]);
let seconds: f64 = fields["seconds"].parse().expect("seconds");
let step: u32 = fields["step"].parse().expect("step");
+ let encoder_log = std::fs::File::create(truth.with_extension("encoder.log"))
+ .expect("create recorder encoder log");
+ tracing_subscriber::fmt()
+ .with_env_filter(
+ tracing_subscriber::EnvFilter::new(std::env::var("CAP_VERIFY_LOG").unwrap_or_default())
+ .add_directive(
+ "cap_enc_ffmpeg::video::h264=info"
+ .parse()
+ .expect("encoder filter"),
+ ),
+ )
+ .without_time()
+ .with_level(false)
+ .with_target(false)
+ .with_ansi(false)
+ .with_writer(encoder_log.and(std::io::stderr))
+ .try_init()
+ .expect("capture recorder encoder selection");
+ common::init();
if arm == Arm::OutOfProcess {
cap_recording::oop_muxer::resolve_muxer_binary().expect("child recorder: cap-muxer binary");
}
@@ -670,6 +721,49 @@ fn fragment_complete(path: &Path) -> bool {
at == data.len() && moof && mdat
}
+// A segment can contain several moof/mdat fragments and end mid-write.
+// This oracle deliberately does not use the recovery parser being tested.
+fn complete_fragment_ends(data: &[u8]) -> Vec<usize> {
+ let mut ends = Vec::new();
+ let mut at = 0usize;
+ let mut pending_moof = false;
+ while let Some(header) = data.get(at..).and_then(|tail| tail.get(..8)) {
+ let size32 = u32::from_be_bytes(header[..4].try_into().unwrap());
+ let (size, header_len) = match size32 {
+ 0 => break,
+ 1 => {
+ let Some(extended) = data.get(at..).and_then(|tail| tail.get(8..16)) else {
+ break;
+ };
+ let Ok(size) = usize::try_from(u64::from_be_bytes(extended.try_into().unwrap()))
+ else {
+ break;
+ };
+ (size, 16)
+ }
+ size => (size as usize, 8),
+ };
+ let Some(end) = at.checked_add(size).filter(|end| *end <= data.len()) else {
+ break;
+ };
+ if size < header_len {
+ break;
+ }
+ match &header[4..8] {
+ b"moof" if pending_moof => break,
+ b"moof" => pending_moof = true,
+ b"mdat" if pending_moof => {
+ ends.push(end);
+ pending_moof = false;
+ }
+ b"mdat" => break,
+ _ => {}
+ }
+ at = end;
+ }
+ ends
+}
+
fn segment_index(name: &str) -> Option<u32> {
name.strip_prefix("segment_")?
.trim_end_matches(".tmp")
@@ -678,9 +772,8 @@ fn segment_index(name: &str) -> Option<u32> {
.ok()
}
-/// Physically complete fragments of one writer directory (finished
-/// `segment_NNN.m4s`, or a finished `.m4s.tmp` the writer had not renamed
-/// yet), in index order.
+/// Segments with at least one complete on-disk fragment, including a
+/// complete prefix of an interrupted `.m4s.tmp`, in index order.
fn complete_fragments(dir: &Path) -> Vec<PathBuf> {
let Ok(entries) = std::fs::read_dir(dir) else {
return Vec::new();
@@ -694,7 +787,10 @@ fn complete_fragments(dir: &Path) -> Vec<PathBuf> {
let Some(index) = segment_index(name) else {
continue;
};
- if !fragment_complete(&path) {
+ if std::fs::read(&path)
+ .map(|data| complete_fragment_ends(&data).is_empty())
+ .unwrap_or(true)
+ {
continue;
}
// Prefer the renamed file when both exist.
@@ -737,7 +833,10 @@ fn decode_fragments(
}
let mut joined = std::fs::read(dir.join("init.mp4")).context("init.mp4")?;
for fragment in fragments {
- joined.extend(std::fs::read(fragment)?);
+ let data = std::fs::read(fragment)?;
+ if let Some(&end) = complete_fragment_ends(&data).last() {
+ joined.extend_from_slice(&data[..end]);
+ }
}
let path = scratch.join(format!(
"joined-{}.mp4",
@@ -772,11 +871,21 @@ fn fragments_on_disk(display: &Path, scratch: &Path) -> anyhow::Result<Vec<Vec<(
let mut out = Vec::new();
for dir in writer_dirs(display) {
for fragment in complete_fragments(&dir) {
- out.push(decode_fragments(
- &dir,
- std::slice::from_ref(&fragment),
- scratch,
- )?);
+ let data = std::fs::read(&fragment)?;
+ let init = std::fs::read(dir.join("init.mp4"))?;
+ let path = scratch.join("fragment-prefix.mp4");
+ let mut previous = Vec::new();
+ for end in complete_fragment_ends(&data) {
+ std::fs::write(&path, [&init[..], &data[..end]].concat())?;
+ let frames = decode_markers(&path)?;
+ anyhow::ensure!(
+ frames.starts_with(&previous),
+ "decoding a longer fragment prefix changed earlier frames"
+ );
+ out.push(frames[previous.len()..].to_vec());
+ previous = frames;
+ }
+ let _ = std::fs::remove_file(&path);
}
}
Ok(out)
@@ -836,8 +945,8 @@ fn on_disk(display: &Path, scratch: &Path) -> anyhow::Result<Vec<(u32, f64)>> {
Ok(fragments_on_disk(display, scratch)?.concat())
}
-/// A fragment covers the capture time from its first frame to the first
-/// frame of the next one, and the encoders must start a new one once
+/// A timed fragment covers capture time from its first frame to the first
+/// frame of the next one, and the writer must flush a new one once
/// `GOP_SECS` have passed: the span may exceed `GOP_SECS` only by the
/// interval before the frame that closed it.
fn check_fragment_spans(case: &mut Case, fragments: &[Vec<(u32, f64)>], handed: &[Handed]) {
@@ -976,6 +1085,7 @@ fn check_timeline(case: &mut Case, label: &str, frames: &[(u32, f64)], handed: &
/// fragment being written plus the frames in flight, all at the end.
fn check_recovery(
case: &mut Case,
+ in_flight: u32,
recovered: &[(u32, f64)],
physical: &[(u32, f64)],
handed: &[Handed],
@@ -1044,7 +1154,7 @@ fn check_recovery(
),
);
}
- let bound = GOP + IN_FLIGHT;
+ let bound = GOP + in_flight;
// In time: one fragment of capture time, plus the in-flight frames and
// the frame that closes the fragment at the delivered interval (a static
// screen delivers far fewer frames than the nominal rate).
@@ -1052,7 +1162,7 @@ fn check_recovery(
.windows(2)
.map(|w| w[1].secs - w[0].secs)
.fold(1.0 / f64::from(FPS), f64::max);
- let bound_secs = GOP_SECS + f64::from(IN_FLIGHT + 1) * interval;
+ let bound_secs = GOP_SECS + f64::from(in_flight + 1) * interval;
case.note(format!(
"handed {} frames, complete on disk up to {:?}, lost tail {lost_tail} frames / {lost_secs:.2}s (bound {bound} frames / {bound_secs:.2}s)",
handed.len(),
@@ -1063,7 +1173,7 @@ fn check_recovery(
case.problem(
"loss",
format!(
- "the crash lost the last {lost_tail} handed frames, more than one fragment ({GOP}) plus {IN_FLIGHT} in flight"
+ "the crash lost the last {lost_tail} handed frames, more than one fragment ({GOP}) plus {in_flight} in flight"
),
);
}
@@ -1072,7 +1182,7 @@ fn check_recovery(
case.problem(
"loss-time",
format!(
- "the crash lost {lost_secs:.2}s of capture, more than one {GOP_SECS}s fragment plus {} frames in flight ({bound_secs:.2}s at the delivered interval)", IN_FLIGHT + 1
+ "the crash lost {lost_secs:.2}s of capture, more than one {GOP_SECS}s fragment plus {} frames in flight ({bound_secs:.2}s at the delivered interval)", in_flight + 1
),
);
}
@@ -1263,7 +1373,11 @@ fn recorder_kill(
check_open_fragment(case, &display);
}
let fragments = fragments_on_disk(&display, &scratch)?;
- check_fragment_spans(case, &fragments, &handed);
+ // Studio flushes timed fragments; the OOP writer instead drains its
+ // keyframe-delimited segment after the recorder exits (#2646, #2644).
+ if arm == Arm::InProcess {
+ check_fragment_spans(case, &fragments, &handed);
+ }
let physical = fragments.concat();
let pending = pending_fragment_frames(&display, &scratch)?;
let recovered = match finalize(&display, &scratch.join("recovered.mp4")) {
@@ -1274,7 +1388,16 @@ fn recorder_kill(
}
Err(e) => return Err(e),
};
- check_recovery(case, &recovered, &physical, &handed, &pending);
+ let in_flight = if arm == Arm::InProcess {
+ let log = std::fs::read_to_string(truth.with_extension("encoder.log"))?;
+ let encoder =
+ selected_encoder(&log).context("recorder did not report its selected encoder")?;
+ case.note(format!("recorder encoder: {encoder}"));
+ recorder_in_flight(arm, encoder)
+ } else {
+ IN_FLIGHT
+ };
+ check_recovery(case, in_flight, &recovered, &physical, &handed, &pending);
if std::env::var_os("CAP_VERIFY_KEEP").is_none() {
let _ = std::fs::remove_dir_all(&scratch);
}
@@ -1575,8 +1698,7 @@ fn crash_safety() {
}
settle(case);
}
- // A mostly static screen: one frame every STATIC_STEP slots. A
- // fragment must still close every GOP_SECS of capture time.
+ // A mostly static screen still has the same crash-loss budget.
let id = format!("crash/recorder-kill/{}/static-screen", arm.label());
if report::selected(&id) {
let mut case = Case::new(&id);
@@ -1722,6 +1844,7 @@ fn pending_fragment_bound_rejects_other_complete_frame_loss() {
let mut case = Case::new("pending-fragment-test");
check_recovery(
&mut case,
+ IN_FLIGHT,
&physical[..1],
&physical,
&handed,
@@ -1730,3 +1853,134 @@ fn pending_fragment_bound_rejects_other_complete_frame_loss() {
assert!(case.classes.contains("dropped-complete"));
assert!(!case.classes.contains("loss"));
}
+
+#[test]
+fn recorder_encoder_selection_ignores_failed_candidates() {
+ let failed = "Hardware H264 encoder failed pre-flight self-test, trying next candidate encoder=h264_videotoolbox";
+ assert_eq!(selected_encoder(failed), None);
+ for (selection, expected) in [
+ (
+ "Selected hardware H264 encoder with zero-copy VideoToolbox input width=640",
+ "h264_videotoolbox",
+ ),
+ (
+ "Selected hardware H264 encoder encoder=h264_videotoolbox width=640",
+ "h264_videotoolbox",
+ ),
+ (
+ "Using SOFTWARE H264 encoder (high CPU usage expected) encoder=libx264 width=640",
+ "libx264",
+ ),
+ (
+ "Using SOFTWARE encoder for high throughput (hardware cannot keep up at this resolution/fps) encoder=libx264 width=640",
+ "libx264",
+ ),
+ ] {
+ assert_eq!(
+ selected_encoder(&format!("{failed}\nrecording: {selection}\n")),
+ Some(expected)
+ );
+ }
+}
+
+#[test]
+fn recorder_loss_budget_accounts_for_codec_buffering_without_hiding_excess_loss() {
+ let physical: Vec<_> = (0..120).map(|i| (i, f64::from(i) / 30.0)).collect();
+ for arm in [Arm::InProcess, Arm::OutOfProcess] {
+ for encoder in ["libx264", "h264_videotoolbox"] {
+ for lost in [63, 64, 65, 67, 68, 69] {
+ let handed: Vec<_> = (0..120 + lost)
+ .map(|index| Handed {
+ index,
+ secs: f64::from(index) / 30.0,
+ })
+ .collect();
+ let mut case = Case::new("recorder-loss-budget");
+ check_recovery(
+ &mut case,
+ recorder_in_flight(arm, encoder),
+ &physical,
+ &physical,
+ &handed,
+ &Default::default(),
+ );
+ let videotoolbox = encoder == "h264_videotoolbox" && arm == Arm::InProcess;
+ assert_eq!(
+ case.classes.contains("loss"),
+ if videotoolbox { lost >= 68 } else { lost >= 64 },
+ "{arm:?}, {encoder}, {lost} lost frames"
+ );
+ assert_eq!(
+ case.classes.contains("loss-time"),
+ if videotoolbox { lost >= 69 } else { lost >= 65 },
+ "{arm:?}, {encoder}, {lost} lost frames"
+ );
+ assert!(!case.classes.contains("dropped-complete"));
+ }
+ }
+ }
+}
+
+#[test]
+fn complete_prefix_keeps_whole_fragments_before_every_truncated_tail() {
+ let atom = |kind: &[u8; 4], payload: &[u8]| {
+ let mut bytes = ((payload.len() + 8) as u32).to_be_bytes().to_vec();
+ bytes.extend_from_slice(kind);
+ bytes.extend_from_slice(payload);
+ bytes
+ };
+ let first = [atom(b"moof", &[1, 2]), atom(b"mdat", &[3, 4, 5])].concat();
+ let second = [atom(b"moof", &[6]), atom(b"mdat", &[7, 8, 9])].concat();
+ for cut in 0..first.len() {
+ assert!(complete_fragment_ends(&first[..cut]).is_empty(), "{cut}");
+ }
+ for cut in 0..second.len() {
+ let bytes = [&first[..], &second[..cut]].concat();
+ assert_eq!(complete_fragment_ends(&bytes), vec![first.len()], "{cut}");
+ }
+ let joined = [first.clone(), second].concat();
+ assert_eq!(
+ complete_fragment_ends(&joined),
+ vec![first.len(), joined.len()]
+ );
+ for tail in [
+ vec![0; 8],
+ [4u32.to_be_bytes().as_slice(), b"moof"].concat(),
+ [u32::MAX.to_be_bytes().as_slice(), b"moof"].concat(),
+ [atom(b"moof", &[]), atom(b"moof", &[]), atom(b"mdat", &[])].concat(),
+ ] {
+ assert_eq!(
+ complete_fragment_ends(&[first.clone(), tail].concat()),
+ vec![first.len()]
+ );
+ }
+ let extended = [
+ atom(b"moof", &[]),
+ [
+ 1u32.to_be_bytes().as_slice(),
+ b"mdat",
+ 19u64.to_be_bytes().as_slice(),
+ &[1, 2, 3],
+ ]
+ .concat(),
+ ]
+ .concat();
+ assert_eq!(complete_fragment_ends(&extended), vec![extended.len()]);
+}
+
+#[test]
+fn timed_fragment_span_still_rejects_a_delayed_flush() {
+ let handed: Vec<_> = (0..=120)
+ .step_by(10)
+ .map(|index| Handed {
+ index,
+ secs: f64::from(index) / f64::from(FPS),
+ })
+ .collect();
+ for (count, fails) in [(6, false), (12, true)] {
+ let fragment = handed[..count].iter().map(|h| (h.index, h.secs)).collect();
+ let mut case = Case::new("timed-fragment-test");
+ check_fragment_spans(&mut case, &[fragment], &handed);
+ assert_eq!(case.classes.contains("fragment-span"), fails);
+ }
+}
diff --git a/crates/verify/tests/export.rs b/crates/verify/tests/export.rs
index 46d062216..a4080e834 100644
--- a/crates/verify/tests/export.rs
+++ b/crates/verify/tests/export.rs
@@ -654,7 +654,6 @@ fn instant_case(spec: cap_verify::pipeline::InstantSpec) -> Option<String> {
}
let audio = media::decode_audio(&output, None).expect("decode instant audio");
let mono = audio.mono();
- let mut mic_offset_ms = None;
for source in [AudioSource::Mic, AudioSource::System] {
let expected: Vec<(u32, f64)> = emitted
.audio
@@ -795,21 +794,7 @@ fn instant_case(spec: cap_verify::pipeline::InstantSpec) -> Option<String> {
),
);
}
- if source == AudioSource::Mic {
- mic_offset_ms = Some((av * 1e4).round() / 10.0);
- }
if av.abs() > 0.005 {
- if source == AudioSource::System
- && case.id == "export/instant/output/mp4/native"
- && let Some(mic_offset_ms) = mic_offset_ms
- {
- case.measure(&sync_class, "mic-offset-ms", mic_offset_ms);
- case.measure(
- &sync_class,
- "system-minus-mic-ms",
- ((av * 1e3 - mic_offset_ms) * 10.0).round() / 10.0,
- );
- }
case.measure(&sync_class, "offset-ms", (av * 1e4).round() / 10.0);
case.problem(
&sync_class,
diff --git a/crates/verify/tests/recording.rs b/crates/verify/tests/recording.rs
index 61abf3a06..b88caa195 100644
--- a/crates/verify/tests/recording.rs
+++ b/crates/verify/tests/recording.rs
@@ -1807,6 +1807,68 @@ fn observer_silence_excuses_only_a_covered_window() {
assert!(!check(Vec::new()).dropped_for_every_client(4));
}
+#[test]
+fn post_hold_pictures_are_excused_only_when_the_reference_missed_them() {
+ // 20 fps pictures, held from 0.5 s to 1.5 s: picture 10 stays on screen
+ // and picture 31 is the first change after the hold.
+ let truth: Truth = (1..=10u32)
+ .chain(31..=50)
+ .map(|i| (i, f64::from(i - 1) * 0.05))
+ .collect();
+ let required = vec![(
+ 31u32,
+ "first picture after the 1 s hold at 0.5 s".to_string(),
+ )];
+ let check = |observed: Vec<f64>| VideoCheck {
+ name: "screen",
+ kind: StreamKind::Screen,
+ truth: &truth,
+ not_sent: &[],
+ may_skip: Box::new(|_| false),
+ allow_duplicates: true,
+ required: required.clone(),
+ window: (0.0, 2.5),
+ observed,
+ };
+ let recorded_except = |lost: &'static [u32]| move |i: u32| !lost.contains(&i);
+ // The reference stream receives each picture 20 ms after it is presented
+ // and nothing during the hold, as on a quiet screen.
+ let prompt: Vec<f64> = truth.values().map(|t| t + 0.02).collect();
+ // The capture service resumes 70 ms after the hold: its first frame
+ // already shows picture 32, so picture 31 reached no client.
+ let late: Vec<f64> = prompt
+ .iter()
+ .map(|&t| if (1.51..1.53).contains(&t) { 1.57 } else { t })
+ .collect();
+
+ let resumed_late = check(late);
+ assert!(resumed_late.dropped_for_every_client(31));
+ let (missing, dropped) = resumed_late.missing_required(recorded_except(&[31]), 1, 50);
+ assert!(missing.is_empty(), "excused: {missing:?}");
+ assert_eq!(dropped, vec![31]);
+ // The first picture after the hold that reached the reference must be
+ // recorded in its place.
+ let (missing, _) = resumed_late.missing_required(recorded_except(&[31, 32]), 1, 50);
+ assert_eq!(missing.iter().map(|m| m.0).collect::<Vec<_>>(), vec![32]);
+
+ // The reference received picture 31: the recorder lost it.
+ let resumed_in_time = check(prompt);
+ assert!(!resumed_in_time.dropped_for_every_client(31));
+ let (missing, dropped) = resumed_in_time.missing_required(recorded_except(&[31]), 1, 50);
+ assert_eq!(missing.iter().map(|m| m.0).collect::<Vec<_>>(), vec![31]);
+ assert!(dropped.is_empty());
+ assert!(
+ resumed_in_time
+ .missing_required(recorded_except(&[]), 1, 50)
+ .0
+ .is_empty()
+ );
+
+ // Without a reference stream (Windows) nothing is excused.
+ let (missing, _) = check(Vec::new()).missing_required(recorded_except(&[31]), 1, 50);
+ assert_eq!(missing.iter().map(|m| m.0).collect::<Vec<_>>(), vec![31]);
+}
+
fn test_trace(name: &'static str, handoffs: Vec<Handoff>) -> SourceTrace {
let constraint = sched::Constraint {
period_ms: 33.333,
@@ -2654,6 +2716,10 @@ enum Injection {
TruncateEnd,
/// The first second of the screen track is lost (a late start).
LateStart,
+ /// The first picture after a scripted hold is lost although the
+ /// stimulus's own capture stream received it: a recorder drop, which
+ /// must fail even where the capture service resumes late after a hold.
+ DropAfterHold,
}
thread_local! {
@@ -2662,13 +2728,14 @@ thread_local! {
static INJECTED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
-const INJECTIONS: [Injection; 6] = [
+const INJECTIONS: [Injection; 7] = [
Injection::DropRun,
Injection::Freeze,
Injection::Stale,
Injection::Late,
Injection::TruncateEnd,
Injection::LateStart,
+ Injection::DropAfterHold,
];
const INJECTED_LATENESS: f64 = 0.15;
@@ -2681,6 +2748,7 @@ impl Injection {
Injection::Late => "screen 150 ms late",
Injection::TruncateEnd => "lose the last second",
Injection::LateStart => "start one second late",
+ Injection::DropAfterHold => "lose a picture after a hold that reached the reference",
}
}
@@ -2691,12 +2759,15 @@ impl Injection {
Injection::Stale => ("timestamps", "screen:"),
Injection::Late => ("sync-screen-mic", "screen vs"),
Injection::TruncateEnd | Injection::LateStart => ("coverage", "screen:"),
+ Injection::DropAfterHold => ("missing-after-hold", "screen:"),
}
}
/// First of three consecutive presented indices, around 60 % of the
- /// recording, that were all captured and each stayed on screen long
- /// enough that the capture rate guarantees a sample.
+ /// recording, that were all captured, each stayed on screen long
+ /// enough that the capture rate guarantees a sample, and each reached
+ /// the stimulus's own capture stream (a picture that did not would be
+ /// excused as a capture-service drop once removed).
fn target(frames: &[(f64, Readout)], check: &VideoCheck) -> Option<u32> {
let mut seen: Vec<u32> = Vec::new();
for (_, readout) in frames {
@@ -2713,15 +2784,65 @@ impl Injection {
i > 1
&& captured.contains(&(i - 1))
&& (i..i + 3).all(|j| {
- captured.contains(&j) && check.truth.contains_key(&j) && !(check.may_skip)(j)
+ captured.contains(&j)
+ && check.truth.contains_key(&j)
+ && !(check.may_skip)(j)
+ && !check.dropped_for_every_client(j)
})
})
}
- fn apply(self, frames: &mut Vec<(f64, Readout)>, check: &VideoCheck) {
+ fn apply(self, frames: &mut Vec<(f64, Readout)>, check: &mut VideoCheck) {
let before = frames.clone();
- self.mutate(frames, check);
- INJECTED.set(*frames != before);
+ let observed = check.observed.len();
+ if self == Injection::DropAfterHold {
+ Self::drop_after_hold(frames, check);
+ } else {
+ self.mutate(frames, check);
+ }
+ INJECTED.set(*frames != before || check.observed.len() != observed);
+ }
+
+ /// Takes the first required picture after a hold that lies inside the
+ /// recording (one the recorder captured if there is one), removes it
+ /// from the recording and gives the stimulus's own capture stream a
+ /// frame while it was on screen.
+ fn drop_after_hold(frames: &mut Vec<(f64, Readout)>, check: &mut VideoCheck) {
+ let index_of = |r: &Readout| match r {
+ Readout::Marker { marker, .. } if marker.kind == StreamKind::Screen => {
+ Some(marker.index)
+ }
+ _ => None,
+ };
+ let recorded: std::collections::BTreeSet<u32> =
+ frames.iter().filter_map(|(_, r)| index_of(r)).collect();
+ let (Some(&lo), Some(&hi)) = (recorded.first(), recorded.last()) else {
+ return;
+ };
+ let inside: Vec<u32> = check
+ .required
+ .iter()
+ .map(|(index, _)| *index)
+ .filter(|index| *index > lo && *index < hi)
+ .collect();
+ let Some(index) = inside
+ .iter()
+ .copied()
+ .find(|index| recorded.contains(index))
+ .or_else(|| inside.first().copied())
+ else {
+ return;
+ };
+ let (Some(&shown), Some((_, &replaced))) = (
+ check.truth.get(&index),
+ check.truth.range(index + 1..).next(),
+ ) else {
+ return;
+ };
+ frames.retain(|(_, r)| index_of(r) != Some(index));
+ let at = (shown + replaced) / 2.0;
+ let position = check.observed.partition_point(|t| *t < at);
+ check.observed.insert(position, at);
}
fn mutate(self, frames: &mut Vec<(f64, Readout)>, check: &VideoCheck) {
@@ -2730,6 +2851,7 @@ impl Injection {
_ => None,
};
match self {
+ Injection::DropAfterHold => {}
Injection::Late => {
for (pts, _) in frames.iter_mut() {
*pts += INJECTED_LATENESS;
@@ -2813,6 +2935,9 @@ fn self_check(rec: &Recorded, reference: u64, case: &mut Case) -> anyhow::Result
));
continue;
}
+ if injection == Injection::DropAfterHold && rec.holds.is_empty() {
+ continue;
+ }
if injection == Injection::Late && case.unobserved.contains_key(class) {
caught.push(format!("{}: unobserved (track absent)", injection.label()));
continue;
@@ -2838,8 +2963,13 @@ fn self_check(rec: &Recorded, reference: u64, case: &mut Case) -> anyhow::Result
case.problem(
"oracle",
format!(
- "self-check: no frames to inject '{}' into: no 3 consecutive pictures the capture rate must sample",
- injection.label()
+ "self-check: no frames to inject '{}' into: {}",
+ injection.label(),
+ if injection == Injection::DropAfterHold {
+ "no picture after a hold inside the recording"
+ } else {
+ "no 3 consecutive pictures the capture rate must sample"
+ }
),
);
} else if probe.classes.contains(class) && !new.is_empty() {
@@ -2900,10 +3030,24 @@ struct VideoCheck<'a> {
const OBSERVER_COVER_SECS: f64 = 0.5;
impl VideoCheck<'_> {
+ /// Whether the stimulus's own capture stream received a frame while
+ /// `index` was on screen.
+ fn observed_while_shown(&self, index: u32) -> bool {
+ let (Some(&shown), Some((_, &replaced))) =
+ (self.truth.get(&index), self.truth.range(index + 1..).next())
+ else {
+ return false;
+ };
+ self.observed.iter().any(|&t| t >= shown && t <= replaced)
+ }
+
/// A picture the capture service never delivered to any client: the
/// stimulus's own capture stream received no frame while it was on
/// screen, although that stream delivered frames on both sides of it
- /// (so it covered exactly that window).
+ /// (so it covered exactly that window). The stream receives frames only
+ /// when the screen changes, so its frames before the picture are looked
+ /// for from the previous picture on: after a scripted hold that is when
+ /// the screen went static, and during the hold the stream is silent.
fn dropped_for_every_client(&self, index: u32) -> bool {
let Some(&shown) = self.truth.get(&index) else {
return false;
@@ -2911,16 +3055,67 @@ impl VideoCheck<'_> {
let Some((_, &replaced)) = self.truth.range(index + 1..).next() else {
return false;
};
+ let since = self
+ .truth
+ .range(..index)
+ .next_back()
+ .map_or(shown, |(_, &previous)| previous.min(shown));
let before = self
.observed
.iter()
- .any(|&t| t < shown && t >= shown - OBSERVER_COVER_SECS);
+ .any(|&t| t < shown && t >= since - OBSERVER_COVER_SECS);
let after = self
.observed
.iter()
.any(|&t| t > replaced && t <= replaced + OBSERVER_COVER_SECS);
- let during = self.observed.iter().any(|&t| t >= shown && t <= replaced);
- before && after && !during
+ before && after && !self.observed_while_shown(index)
+ }
+
+ /// Required pictures missing from the recording (indices strictly
+ /// between `lo` and `hi`, the first and last recorded), with their
+ /// description, and the required pictures excused because the capture
+ /// service dropped them for every client. An excused picture is
+ /// replaced by the first later one, within `OBSERVER_COVER_SECS`, that
+ /// the stimulus's own capture stream received while it was on screen
+ /// and that the capture rate must sample: the first change after the
+ /// hold that reached any client must be in the recording.
+ fn missing_required(
+ &self,
+ recorded: impl Fn(u32) -> bool,
+ lo: u32,
+ hi: u32,
+ ) -> (Vec<(u32, String)>, Vec<u32>) {
+ let mut missing = Vec::new();
+ let mut dropped = Vec::new();
+ for (index, what) in &self.required {
+ let index = *index;
+ let Some(&shown) = self.truth.get(&index) else {
+ continue;
+ };
+ if index <= lo || index >= hi || recorded(index) {
+ continue;
+ }
+ if !self.dropped_for_every_client(index) {
+ missing.push((index, what.clone()));
+ continue;
+ }
+ dropped.push(index);
+ let stand_in = self
+ .truth
+ .range(index + 1..hi)
+ .take_while(|(_, t)| **t <= shown + OBSERVER_COVER_SECS)
+ .map(|(i, _)| *i)
+ .find(|&i| self.observed_while_shown(i) && !(self.may_skip)(i));
+ if let Some(stand_in) = stand_in.filter(|&i| !recorded(i)) {
+ missing.push((
+ stand_in,
+ format!(
+ "{what} to reach the stimulus's own capture stream (index {index} reached no client)"
+ ),
+ ));
+ }
+ }
+ (missing, dropped)
}
}
@@ -2931,7 +3126,7 @@ impl VideoCheck<'_> {
/// recorded picture is the first one presented, 9 to 69 ms after the start.
const START_COVERAGE_SECS: f64 = 0.3;
-fn check_video(case: &mut Case, track: &Track, check: VideoCheck) -> anyhow::Result<Offsets> {
+fn check_video(case: &mut Case, track: &Track, mut check: VideoCheck) -> anyhow::Result<Offsets> {
let mut frames: Vec<(f64, Readout)> = Vec::new();
media::decode_video(&track.path, |picture| {
let readout = video_code::read_stream(&picture.view(), check.kind);
@@ -2940,7 +3135,7 @@ fn check_video(case: &mut Case, track: &Track, check: VideoCheck) -> anyhow::Res
if check.kind == StreamKind::Screen
&& let Some(injection) = INJECTION.get()
{
- injection.apply(&mut frames, &check);
+ injection.apply(&mut frames, &mut check);
}
let name = check.name;
if frames.is_empty() {
@@ -3111,10 +3306,17 @@ fn check_video(case: &mut Case, track: &Track, check: VideoCheck) -> anyhow::Res
}
// Pictures the capture service dropped for every client (approved
// policy): counted and reported, never a threshold change.
- let (for_every_client, rest): (Vec<u32>, Vec<u32>) = skipped
+ // The first picture after a hold that reached no client counts here too
+ // (see `missing_required`).
+ let (mut for_every_client, rest): (Vec<u32>, Vec<u32>) = skipped
.into_iter()
.partition(|&index| check.dropped_for_every_client(index));
skipped = rest;
+ let (missing_required, dropped_required) =
+ check.missing_required(|index| first_seen.contains_key(&index), lo, hi);
+ for_every_client.extend(dropped_required);
+ for_every_client.sort_unstable();
+ for_every_client.dedup();
if !for_every_client.is_empty() {
case.note(format!(
"capture-service drops: {name}: {} missing pictures also never reached the stimulus's own capture stream, which covered their window: {:?}",
@@ -3152,19 +3354,13 @@ fn check_video(case: &mut Case, track: &Track, check: VideoCheck) -> anyhow::Res
(best.max(run), run, Some(i))
})
.0;
- for (index, what) in &check.required {
- if check.truth.contains_key(index)
- && *index > lo
- && *index < hi
- && !first_seen.contains_key(index)
- {
- case.count("missing-after-hold", &format!("{name}-pictures"), 1.0);
- case.problem(
- "missing-after-hold",
- format!("{name}: {what} (index {index}) is missing from the recording"),
- );
- implicate(case, check.truth.get(index).copied());
- }
+ for (index, what) in &missing_required {
+ case.count("missing-after-hold", &format!("{name}-pictures"), 1.0);
+ case.problem(
+ "missing-after-hold",
+ format!("{name}: {what} (index {index}) is missing from the recording"),
+ );
+ implicate(case, check.truth.get(index).copied());
}
if skipped.len() > budget || longest_run >= 3 {
case.count("missing", &format!("{name}-frames"), skipped.len() as f64);
diff --git a/crates/video-decode/src/avassetreader.rs b/crates/video-decode/src/avassetreader.rs
index c1dc1f7ef..770e2013f 100644
--- a/crates/video-decode/src/avassetreader.rs
+++ b/crates/video-decode/src/avassetreader.rs
@@ -664,6 +664,25 @@ impl AVAssetReaderDecoder {
}
}
+ /// Why the reader stopped vending, when it failed instead of reaching the
+ /// end of its range. A failed reader never vends again.
+ pub fn failure(&self) -> Option<String> {
+ match &self.source {
+ ReaderSource::Native { reader, .. } => {
+ (reader.status() == av::AssetReaderStatus::Failed).then(|| {
+ reader.error().map_or_else(
+ || "unknown error".to_string(),
+ |error| format!("{} ({})", error.localized_description(), error.code()),
+ )
+ })
+ }
+ #[cfg(feature = "scripted")]
+ ReaderSource::Scripted(reader) => reader
+ .failed()
+ .then(|| "scripted decode failure".to_string()),
+ }
+ }
+
pub fn width(&self) -> u32 {
self.width
}
diff --git a/crates/video-decode/src/ffmpeg.rs b/crates/video-decode/src/ffmpeg.rs
index e7d94ce15..83198ccc0 100644
--- a/crates/video-decode/src/ffmpeg.rs
+++ b/crates/video-decode/src/ffmpeg.rs
@@ -179,6 +179,8 @@ fn configure_software_threading(decoder: &mut avcodec::decoder::Decoder, width:
pub struct FFmpegDecoder {
input: DecoderInput,
+ // Declared before `hw_device` so the codec context, whose get_format
+ // callback writes to the device's fallback flag, is freed first.
decoder: avcodec::decoder::Video,
stream_index: usize,
hw_device: Option<HwDevice>,
@@ -459,8 +461,13 @@ impl FFmpegDecoder {
self.start_time
}
+ /// Whether frames come from the hardware decoder. False once FFmpeg has
+ /// fallen back to software because the hardware setup failed, which is
+ /// only known after the first packet is decoded.
pub fn is_hardware_accelerated(&self) -> bool {
- self.hw_device.is_some()
+ self.hw_device
+ .as_ref()
+ .is_some_and(|device| !device.is_software_fallback())
}
}
@@ -478,6 +485,13 @@ impl FramesIter<'_> {
pub fn decoder(&self) -> &avcodec::decoder::Video {
self.decoder
}
+
+ /// [`FFmpegDecoder::is_hardware_accelerated`] while frames are read.
+ pub fn is_hardware_accelerated(&self) -> bool {
+ self.hw_device
+ .as_ref()
+ .is_some_and(|device| !device.is_software_fallback())
+ }
}
impl<'a> Iterator for FramesIter<'a> {
diff --git a/crates/video-decode/src/scripted.rs b/crates/video-decode/src/scripted.rs
index a44e2a929..08e516f52 100644
--- a/crates/video-decode/src/scripted.rs
+++ b/crates/video-decode/src/scripted.rs
@@ -13,7 +13,9 @@
//! - AVAssetReader: a reader started at time `T` vends the sample on screen at
//! `T` (the newest at or before it) and every later one. When that sample
//! starts before `T` its presentation time is trimmed to `T`. A reader started
-//! at or after the end of the track vends nothing.
+//! at or after the end of the track vends nothing. A read set up with
+//! [`Registration::fail_avf_read_at`] fails like a decode error: the reader
+//! vends nothing more and reports the failure.
//!
//! Every vended frame carries its sample index in its first luma row, see
//! [`frame_id`].
@@ -25,6 +27,7 @@ use std::{
Arc, Mutex, OnceLock,
atomic::{AtomicU64, Ordering},
},
+ time::Duration,
};
const MAGIC: [u8; 4] = *b"CAPS";
@@ -95,16 +98,32 @@ struct Counters {
pub(crate) struct ScriptState {
pub(crate) script: Script,
counters: Counters,
+ vend_delay_micros: AtomicU64,
vended: Mutex<Vec<u32>>,
+ failing_samples: Mutex<Vec<usize>>,
#[cfg(target_os = "macos")]
samples: Mutex<HashMap<usize, avf::SharedSample>>,
}
impl ScriptState {
fn record_vend(&self, index: usize) {
+ let delay = self.vend_delay_micros.load(Ordering::Relaxed);
+ if delay > 0 {
+ std::thread::sleep(Duration::from_micros(delay));
+ }
self.vended.lock().unwrap().push(index as u32);
}
+ #[cfg(target_os = "macos")]
+ fn take_failure(&self, index: usize) -> bool {
+ let mut failing = self.failing_samples.lock().unwrap();
+ let Some(position) = failing.iter().position(|&sample| sample == index) else {
+ return false;
+ };
+ failing.swap_remove(position);
+ true
+ }
+
/// Whether `pts` (in stream ticks) is at or before `value / scale` seconds.
#[cfg(target_os = "macos")]
fn at_or_before(&self, pts: i64, value: i64, scale: i32) -> bool {
@@ -156,6 +175,15 @@ impl Registration {
std::mem::take(&mut *self.state.vended.lock().unwrap())
}
+ /// Every reader of this script sleeps this long before each vend, so a
+ /// test can act while a read is still in progress.
+ pub fn set_vend_delay(&self, delay: Duration) {
+ let micros = u64::try_from(delay.as_micros()).unwrap_or(u64::MAX);
+ self.state
+ .vend_delay_micros
+ .store(micros, Ordering::Relaxed);
+ }
+
/// AVAssetReader starts: creations and resets.
pub fn reader_starts(&self) -> u64 {
self.state.counters.reader_starts.load(Ordering::Relaxed)
@@ -165,6 +193,13 @@ impl Registration {
pub fn seeks(&self) -> u64 {
self.state.counters.seeks.load(Ordering::Relaxed)
}
+
+ /// The next AVAssetReader that reaches `index` fails there instead of
+ /// vending it, as a native reader does with a decode error: it vends
+ /// nothing more and reports the failure. Once per call.
+ pub fn fail_avf_read_at(&self, index: usize) {
+ self.state.failing_samples.lock().unwrap().push(index);
+ }
}
impl Drop for Registration {
@@ -184,7 +219,9 @@ pub fn register(script: Script) -> Registration {
let state = Arc::new(ScriptState {
script,
counters: Counters::default(),
+ vend_delay_micros: AtomicU64::new(0),
vended: Mutex::new(Vec::new()),
+ failing_samples: Mutex::new(Vec::new()),
#[cfg(target_os = "macos")]
samples: Mutex::new(HashMap::new()),
});
@@ -314,6 +351,7 @@ mod avf {
state: Arc<ScriptState>,
next: usize,
trimmed_start: Option<cm::Time>,
+ failed: bool,
}
impl AvfReader {
@@ -339,12 +377,24 @@ mod avf {
state,
next,
trimmed_start,
+ failed: false,
}
}
+ pub(crate) fn failed(&self) -> bool {
+ self.failed
+ }
+
pub(crate) fn next_sample_buf(&mut self) -> Option<R<cm::SampleBuf>> {
+ if self.failed {
+ return None;
+ }
let script = &self.state.script;
let frame = script.frames.get(self.next)?;
+ if self.state.take_failure(self.next) {
+ self.failed = true;
+ return None;
+ }
let index = self.next;
self.next += 1;
self.state.record_vend(index);
diff --git a/docker-compose.coolify.env.example b/docker-compose.coolify.env.example
index b57c74417..b1bf99562 100644
--- a/docker-compose.coolify.env.example
+++ b/docker-compose.coolify.env.example
@@ -62,6 +62,7 @@ RESEND_FROM_DOMAIN=
# For video transcription
ASSEMBLY_API_KEY=
+# ASSEMBLY_BASE_URL=https://api.eu.assemblyai.com
# API keys for AI summaries, chat, and docs search. When AI_PROVIDER is not
# set, providers are auto-detected from whichever of these keys are present.
diff --git a/docker-compose.coolify.yml b/docker-compose.coolify.yml
index 539d245f2..628af5069 100644
--- a/docker-compose.coolify.yml
+++ b/docker-compose.coolify.yml
@@ -24,6 +24,8 @@ services:
S3_PATH_STYLE: 'true'
RESEND_API_KEY: '${RESEND_API_KEY:-}'
RESEND_FROM_DOMAIN: '${RESEND_FROM_DOMAIN:-}'
+ ASSEMBLY_API_KEY: '${ASSEMBLY_API_KEY:-}'
+ ASSEMBLY_BASE_URL: '${ASSEMBLY_BASE_URL:-}'
GOOGLE_CLIENT_ID: '${GOOGLE_CLIENT_ID:-}'
GOOGLE_CLIENT_SECRET: '${GOOGLE_CLIENT_SECRET:-}'
APPLE_CLIENT_ID: '${APPLE_CLIENT_ID:-}'
diff --git a/docker-compose.yml b/docker-compose.yml
index 7759c6b63..98badf026 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -26,6 +26,8 @@ services:
S3_PATH_STYLE: "true"
RESEND_API_KEY: ${RESEND_API_KEY:-}
RESEND_FROM_DOMAIN: ${RESEND_FROM_DOMAIN:-}
+ ASSEMBLY_API_KEY: ${ASSEMBLY_API_KEY:-}
+ ASSEMBLY_BASE_URL: ${ASSEMBLY_BASE_URL:-}
GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID:-}
GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET:-}
APPLE_CLIENT_ID: ${APPLE_CLIENT_ID:-}
diff --git a/package.json b/package.json
index c91d86eeb..13fabcc35 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,7 @@
"dev": "bun run docker:up && trap 'bun run docker:stop' EXIT && dotenv -e .env -- turbo run dev --filter=!@cap/chrome-extension --filter=!@cap/status --env-mode=loose --ui tui",
"dev:desktop": "bun run --cwd apps/desktop dev",
"dev:extension": "bun run docker:up && trap 'bun run docker:stop' EXIT && dotenv -e .env -- turbo run dev --filter=@cap/web --filter=@cap/chrome-extension --env-mode=loose --ui tui",
- "dev:manual": "bun run docker:up && trap 'bun run docker:stop' EXIT && dotenv -e .env -- turbo run dev --filter=!@cap/chrome-extension --filter=!@cap/status --no-cache --concurrency 1",
+ "dev:manual": "bun run docker:up && trap 'bun run docker:stop' EXIT && dotenv -e .env -- turbo run dev --filter=!@cap/chrome-extension --filter=!@cap/status --concurrency 1",
"dev:mobile": "bun run docker:up && trap 'bun run docker:stop' EXIT && CAP_MOBILE_DISABLE_ASSOCIATED_DOMAINS=1 EXPO_PUBLIC_CAP_WEB_URL=${EXPO_PUBLIC_CAP_WEB_URL:-http://localhost:3000} dotenv -e .env -- turbo run dev --filter=@cap/web --filter=@cap/mobile --env-mode=loose --ui tui",
"dev:mobile:physical": "bun run docker:up && trap 'bun run docker:stop' EXIT && CAP_MOBILE_DEVICE=physical CAP_MOBILE_DISABLE_ASSOCIATED_DOMAINS=1 dotenv -e .env -- turbo run dev --filter=@cap/web --filter=@cap/mobile --env-mode=loose --ui tui",
"dev:web": "bun run dev --filter=!@cap/desktop --filter=!@cap/mobile",
diff --git a/packages/database/auth/auth-options.ts b/packages/database/auth/auth-options.ts
index 8d9c00f72..0446c990f 100644
--- a/packages/database/auth/auth-options.ts
+++ b/packages/database/auth/auth-options.ts
@@ -7,6 +7,7 @@ import { getServerSession as _getServerSession } from "next-auth";
import type { Adapter } from "next-auth/adapters";
import { decode, type JWT, type JWTDecodeParams } from "next-auth/jwt";
import AppleProvider from "next-auth/providers/apple";
+import CredentialsProvider from "next-auth/providers/credentials";
import EmailProvider from "next-auth/providers/email";
import GoogleProvider from "next-auth/providers/google";
import type { Provider } from "next-auth/providers/index";
@@ -32,6 +33,10 @@ export const maxDuration = 120;
const OTP_CODE_MAX_AGE_SECONDS = 10 * 60;
+// authOptions() is rebuilt per request (getServerSession, getCurrentUser), so
+// the trusted-proxy warning is gated at module scope to log once per process.
+let trustedProxyWarned = false;
+
export async function decodeSessionToken(
params: JWTDecodeParams,
): Promise<JWT | null> {
@@ -104,6 +109,16 @@ export const authOptions = (ssoContext?: SsoAuthContext): NextAuthOptions => {
const oidcIssuer = serverEnv().OIDC_ISSUER;
const oidcClientId = serverEnv().OIDC_CLIENT_ID;
const oidcClientSecret = serverEnv().OIDC_CLIENT_SECRET;
+ const trustedProxyEnabled =
+ serverEnv().TRUSTED_PROXY_AUTH_HEADER &&
+ serverEnv().TRUSTED_PROXY_AUTH_EMAIL &&
+ serverEnv().TRUSTED_PROXY_AUTH_SECRET?.trim();
+ if (!trustedProxyWarned && trustedProxyEnabled) {
+ trustedProxyWarned = true;
+ console.warn(
+ "[auth] trusted-proxy sign-in is ENABLED. Keep the origin private and use an authenticating reverse proxy that strips client-sent copies of both the trust and secret headers and sets them only on owner-authenticated requests.",
+ );
+ }
_providers = [
...(oidcIssuer && oidcClientId && oidcClientSecret
? [
@@ -181,6 +196,59 @@ export const authOptions = (ssoContext?: SsoAuthContext): NextAuthOptions => {
}),
]
: []),
+ ...(trustedProxyEnabled
+ ? [
+ CredentialsProvider({
+ id: "trusted-proxy",
+ name: "Reverse Proxy",
+ credentials: {},
+ // SECURITY: this header is trusted ONLY because the operator
+ // opted in via env, asserting Cap runs behind a reverse proxy
+ // that strips client-sent copies of both trust headers and sets
+ // them only on owner-authenticated requests. Never enable without such a
+ // proxy — it would be a login bypass. Fails closed below.
+ async authorize(_credentials, req) {
+ const env = serverEnv();
+ const header = env.TRUSTED_PROXY_AUTH_HEADER?.toLowerCase();
+ const email = env.TRUSTED_PROXY_AUTH_EMAIL?.toLowerCase();
+ const secret = env.TRUSTED_PROXY_AUTH_SECRET;
+ if (!header || !email || !secret?.trim()) return null;
+ // The router is the sole authority for this header (it strips
+ // any client-sent copy), so its presence means the request was
+ // owner-authenticated. Fail closed on anything else.
+ if (req?.headers?.[header] !== "true") return null;
+
+ // Always require the proxy's secret, even if a caller can reach
+ // the origin directly. Compare fixed-size digests in constant time.
+ const provided = req?.headers?.["x-trusted-proxy-secret"];
+ if (typeof provided !== "string" || !provided) return null;
+ const digest = (s: string) =>
+ crypto.createHash("sha256").update(s).digest();
+ if (!crypto.timingSafeEqual(digest(provided), digest(secret)))
+ return null;
+
+ const [user] = await db()
+ .select({
+ id: users.id,
+ email: users.email,
+ name: users.name,
+ image: users.image,
+ })
+ .from(users)
+ .where(eq(users.email, email))
+ .limit(1);
+ if (!user) return null;
+
+ return {
+ id: user.id,
+ email: user.email,
+ name: user.name,
+ image: user.image,
+ };
+ },
+ }),
+ ]
+ : []),
EmailProvider({
// next-auth defaults to 24h, but the code is 6 digits and the
// verify path has no attempt limiting, so a day-long window is a
diff --git a/packages/database/auth/drizzle-adapter.ts b/packages/database/auth/drizzle-adapter.ts
index 810b6b75e..3d1b7c054 100644
--- a/packages/database/auth/drizzle-adapter.ts
+++ b/packages/database/auth/drizzle-adapter.ts
@@ -18,6 +18,15 @@ import {
import type { ValidatedSsoIdentity } from "./sso.ts";
import { provisionStripeCustomer } from "./stripe-customer.ts";
+const getAffectedRows = (result: unknown) => {
+ if (Array.isArray(result)) {
+ return (
+ (result[0] as { affectedRows?: number } | undefined)?.affectedRows ?? 0
+ );
+ }
+ return (result as { affectedRows?: number } | undefined)?.affectedRows ?? 0;
+};
+
type CreateUserData = Parameters<NonNullable<Adapter["createUser"]>>[0];
type LinkAccountData = Parameters<NonNullable<Adapter["linkAccount"]>>[0];
type UnlinkAccountData = Parameters<NonNullable<Adapter["unlinkAccount"]>>[0];
@@ -456,31 +465,40 @@ export function DrizzleAdapter(
return row;
},
async useVerificationToken({ identifier, token }) {
+ const normalizedIdentifier = identifier?.toLowerCase() ?? "";
const rows = await db
.select()
.from(verificationTokens)
- .where(eq(verificationTokens.token, token))
+ .where(eq(verificationTokens.identifier, normalizedIdentifier))
.limit(1);
const row = rows[0];
if (!row) {
console.warn("[useVerificationToken] No token found");
return null;
}
- const normalizedIdentifier = identifier?.toLowerCase() ?? "";
- const storedIdentifier = row.identifier?.toLowerCase() ?? "";
- if (normalizedIdentifier !== storedIdentifier) {
- console.warn("[useVerificationToken] Identifier mismatch");
- return null;
- }
- await db
+ // Claim the exact row we just read (identifier + token) with a single
+ // delete, and only proceed if we actually removed it. This makes
+ // consumption atomic: concurrent requests race on the same delete, so
+ // at most one of them can claim the row, whether the guess is right or
+ // wrong. Scoping by token too (not identifier alone) means the delete
+ // can't clobber a resend that replaced this row after we read it.
+ const result = await db
.delete(verificationTokens)
.where(
and(
- eq(verificationTokens.token, token),
eq(verificationTokens.identifier, row.identifier),
+ eq(verificationTokens.token, row.token),
),
);
- return { ...row, identifier: storedIdentifier };
+ if (getAffectedRows(result) !== 1) {
+ console.warn("[useVerificationToken] Token already consumed");
+ return null;
+ }
+ if (row.token !== token) {
+ console.warn("[useVerificationToken] Token mismatch");
+ return null;
+ }
+ return { ...row, identifier: normalizedIdentifier };
},
};
}
diff --git a/packages/database/auth/request-site.ts b/packages/database/auth/request-site.ts
new file mode 100644
index 000000000..c9e82a039
--- /dev/null
+++ b/packages/database/auth/request-site.ts
@@ -0,0 +1,56 @@
+import { serverEnv } from "@cap/env";
+
+const READ_ONLY_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);
+
+export type SiteCheckedRequest = {
+ method: string;
+ url: string;
+ headers: Pick<Headers, "get">;
+};
+
+const parseUrl = (value: string | null | undefined) => {
+ if (!value) return null;
+ try {
+ return new URL(value);
+ } catch {
+ return null;
+ }
+};
+
+const isOwnOrigin = (origin: URL, request: SiteCheckedRequest) => {
+ const ownHosts = [
+ request.headers.get("x-forwarded-host")?.split(",")[0]?.trim(),
+ request.headers.get("host"),
+ parseUrl(request.url)?.host,
+ ];
+ if (ownHosts.includes(origin.host)) return true;
+
+ let webUrl: string | undefined;
+ try {
+ webUrl = serverEnv().WEB_URL;
+ } catch {
+ return false;
+ }
+ return parseUrl(webUrl)?.origin === origin.origin;
+};
+
+// Callers must only consult this on the cookie-auth path: a successfully
+// authenticated bearer token returns before the check is reached. The presence
+// of an Authorization header is not proof of bearer auth, so it is ignored
+// here — an invalid bearer that falls back to the cookie still gets checked.
+export const isCrossSiteCookieWrite = (request: SiteCheckedRequest) => {
+ if (READ_ONLY_METHODS.has(request.method.toUpperCase())) return false;
+
+ // Sec-Fetch-Site is set by the browser and cannot be forged by page script,
+ // so it decides when present. Origin is the fallback for clients that
+ // predate fetch metadata and for plain-http self-hosted installs, where
+ // browsers do not send Sec-Fetch-* headers.
+ const fetchSite = request.headers.get("sec-fetch-site");
+ if (fetchSite) return fetchSite === "cross-site";
+
+ const origin = request.headers.get("origin");
+ if (!origin) return false;
+
+ const originUrl = parseUrl(origin);
+ return !originUrl || !isOwnOrigin(originUrl, request);
+};
diff --git a/packages/database/package.json b/packages/database/package.json
index ae6d39b6c..45a780af1 100644
--- a/packages/database/package.json
+++ b/packages/database/package.json
@@ -60,6 +60,7 @@
".": "./index.ts",
"./auth/auth-options": "./auth/auth-options.ts",
"./auth/domain-utils": "./auth/domain-utils.ts",
+ "./auth/request-site": "./auth/request-site.ts",
"./auth/session": "./auth/session.ts",
"./auth/sso": "./auth/sso.ts",
"./auth/sso-state": "./auth/sso-state.ts",
@@ -78,6 +79,7 @@
".": "./dist/index.js",
"./auth/auth-options": "./dist/auth/auth-options.js",
"./auth/domain-utils": "./dist/auth/domain-utils.js",
+ "./auth/request-site": "./dist/auth/request-site.js",
"./auth/session": "./dist/auth/session.js",
"./schema": "./dist/schema.js",
"./crypto": "./dist/crypto.js",
diff --git a/packages/database/types/metadata.ts b/packages/database/types/metadata.ts
index 9712ba202..882832b5e 100644
--- a/packages/database/types/metadata.ts
+++ b/packages/database/types/metadata.ts
@@ -6,6 +6,23 @@
* Video metadata structure
*/
export interface VideoMetadata {
+ loomImportRun?: {
+ runId: string;
+ dispatch: "pending" | "accepted" | "rejected" | "uncertain";
+ mediaDispatch?: {
+ attemptId: string;
+ videoId?: string;
+ state: "accepted" | "rejected" | "uncertain";
+ jobId?: string;
+ };
+ };
+ loomImportUpload?: {
+ attemptId: string;
+ ownerId: string;
+ organizationId: string | null;
+ source?: { key: string; uploadId: string };
+ output: { key: string; uploadId: string };
+ };
editProcessing?: {
token: string;
startedAt: string;
diff --git a/packages/env/server.ts b/packages/env/server.ts
index 4ae865b54..6e882f61c 100644
--- a/packages/env/server.ts
+++ b/packages/env/server.ts
@@ -85,6 +85,30 @@ function createServerEnv() {
OIDC_CLIENT_SECRET: z.string().optional(),
OIDC_NAME: z.string().optional(),
+ /// Reverse-proxy (trusted header) auth
+ // Sign in via an authenticating reverse proxy (e.g. OpenHost). Set all
+ // three values to enable. SECURITY: keep the origin private; the proxy
+ // must strip client-sent trust/secret headers and set both only after auth.
+ TRUSTED_PROXY_AUTH_HEADER: z
+ .string()
+ .optional()
+ .describe(
+ "Header an authenticating reverse proxy sets to 'true' on owner-authenticated requests (e.g. 'x-openhost-is-owner'). Unset disables the provider.",
+ ),
+ TRUSTED_PROXY_AUTH_EMAIL: z
+ .string()
+ .email()
+ .optional()
+ .describe(
+ "Email of the Cap account to sign in when the trusted header is present.",
+ ),
+ TRUSTED_PROXY_AUTH_SECRET: z
+ .string()
+ .optional()
+ .describe(
+ "Required to enable trusted-proxy auth: a non-blank shared secret the proxy must send as 'x-trusted-proxy-secret'. Unset or blank disables the provider. Keep this server-side, never in browser code.",
+ ),
+
/// Settings
CAP_VIDEOS_DEFAULT_PUBLIC: boolString(true).describe(
"Should videos be public or private by default",
@@ -106,6 +130,12 @@ function createServerEnv() {
/// AI providers
ASSEMBLY_API_KEY: z.string().optional().describe("Audio transcription"),
+ ASSEMBLY_BASE_URL: z
+ .string()
+ .optional()
+ .describe(
+ "Transcription API base URL (eg. https://api.eu.assemblyai.com)",
+ ),
ANTHROPIC_API_KEY: z.string().optional().describe("AI chat"),
OPENAI_API_KEY: z.string().optional().describe("AI summaries"),
GROQ_API_KEY: z.string().optional().describe("AI summaries"),
diff --git a/packages/recorder-core/__tests__/instant-recording-uploader.test.ts b/packages/recorder-core/__tests__/instant-recording-uploader.test.ts
index 45334fa20..b1255f39a 100644
--- a/packages/recorder-core/__tests__/instant-recording-uploader.test.ts
+++ b/packages/recorder-core/__tests__/instant-recording-uploader.test.ts
@@ -466,6 +466,82 @@ describe("InstantRecordingUploader", () => {
);
});
+ it.each([
+ [DRIVE_PART_BYTES - 1, false],
+ [DRIVE_PART_BYTES, false],
+ [DRIVE_PART_BYTES, true],
+ [DRIVE_PART_BYTES + 1, false],
+ [2 * DRIVE_PART_BYTES, false],
+ [2 * DRIVE_PART_BYTES, true],
+ ] as const)(
+ "declares the final Drive size for %i streamed bytes, final Blob: %s",
+ async (totalBytes, withFinalBlob) => {
+ const partCount = Math.ceil(totalBytes / DRIVE_PART_BYTES);
+ const fetchMock = vi.fn(
+ async (input: RequestInfo | URL, init?: RequestInit) => {
+ if (input.toString().endsWith("/presign-part")) {
+ return makeJsonResponse({
+ presignedUrl:
+ "https://www.googleapis.com/upload/drive/v3/files/session",
+ provider: "googleDrive",
+ });
+ }
+ expect(input.toString()).toBe("/api/upload/multipart/complete");
+ const body = JSON.parse(String(init?.body));
+ expect(body.parts).toEqual(
+ Array.from({ length: partCount }, (_, index) => ({
+ partNumber: index + 1,
+ etag: `drive-${index + 1}`,
+ size: Math.min(
+ DRIVE_PART_BYTES,
+ totalBytes - index * DRIVE_PART_BYTES,
+ ),
+ })),
+ );
+ return makeJsonResponse({ success: true });
+ },
+ );
+ vi.stubGlobal("fetch", fetchMock);
+ MockXMLHttpRequest.setOutcomes(
+ Array.from({ length: partCount }, (_, index) => ({
+ type: "success",
+ status: index === partCount - 1 ? 200 : 308,
+ })),
+ );
+ const uploader = new InstantRecordingUploader({
+ videoId,
+ uploadId: "drive-session",
+ provider: "googleDrive",
+ mimeType: "video/webm",
+ subpath: "raw-upload.webm",
+ setUploadStatus: vi.fn(),
+ sendProgressUpdate: vi.fn().mockResolvedValue(undefined),
+ });
+ const blob = makeBlob(totalBytes, "video/webm");
+ uploader.handleChunk(blob, totalBytes);
+ await new Promise<void>((resolve) => setImmediate(resolve));
+
+ const streamedPartCount = MockXMLHttpRequest.recordedParts.length;
+ await uploader.finalize({
+ finalBlob: withFinalBlob ? blob : undefined,
+ durationSeconds: 20,
+ subpath: "raw-upload.webm",
+ });
+
+ expect(
+ MockXMLHttpRequest.recordedHeaders.map((headers) =>
+ headers.get("content-range"),
+ ),
+ ).toEqual(
+ Array.from({ length: partCount }, (_, index) => {
+ const end = Math.min((index + 1) * DRIVE_PART_BYTES, totalBytes);
+ return `bytes ${index * DRIVE_PART_BYTES}-${end - 1}/${end === totalBytes ? totalBytes : "*"}`;
+ }),
+ );
+ expect(streamedPartCount).toBe(partCount - 1);
+ },
+ );
+
it("completes multipart uploads with parts ordered by part number", async () => {
const fetchMock = vi.fn(
async (input: RequestInfo | URL, init?: RequestInit) => {
diff --git a/packages/recorder-core/src/instant-mp4-uploader.ts b/packages/recorder-core/src/instant-mp4-uploader.ts
index 11f33a33a..164a219b9 100644
--- a/packages/recorder-core/src/instant-mp4-uploader.ts
+++ b/packages/recorder-core/src/instant-mp4-uploader.ts
@@ -505,7 +505,14 @@ export class InstantRecordingUploader {
? DRIVE_PART_SIZE_BYTES
: MIN_PART_SIZE_BYTES;
while (this.bufferedBytes > 0) {
- if (!force && this.bufferedBytes < targetPartSize) return;
+ if (
+ !force &&
+ (this.bufferedBytes < targetPartSize ||
+ (this.provider === "googleDrive" &&
+ this.bufferedBytes === targetPartSize))
+ ) {
+ return;
+ }
const partSize = Math.min(this.bufferedBytes, targetPartSize);
const { part, remainingChunks, remainingBytes } =
diff --git a/packages/sdk-embed/__tests__/autoplay.test.ts b/packages/sdk-embed/__tests__/autoplay.test.ts
new file mode 100644
index 000000000..a71a2e821
--- /dev/null
+++ b/packages/sdk-embed/__tests__/autoplay.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, test } from "bun:test";
+import { createEmbedUrl } from "../src/vanilla/cap-embed";
+
+describe("embed autoplay", () => {
+ test("requests autoplay using the embed route's true value", () => {
+ const url = new URL(
+ createEmbedUrl({
+ videoId: "video123",
+ publicKey: "pk_test_123",
+ autoplay: true,
+ }),
+ );
+
+ expect(url.searchParams.get("autoplay")).toBe("true");
+ });
+
+ test.each([false, undefined])("omits autoplay when %s", (autoplay) => {
+ const url = new URL(
+ createEmbedUrl({
+ videoId: "video123",
+ publicKey: "pk_test_123",
+ autoplay,
+ }),
+ );
+
+ expect(url.searchParams.has("autoplay")).toBe(false);
+ });
+});
diff --git a/packages/sdk-embed/package.json b/packages/sdk-embed/package.json
index 0c732bce5..3212da37d 100644
--- a/packages/sdk-embed/package.json
+++ b/packages/sdk-embed/package.json
@@ -24,6 +24,7 @@
],
"scripts": {
"build": "tsup",
+ "test": "bun test",
"typecheck": "tsc --noEmit"
},
"dependencies": {},
diff --git a/packages/sdk-embed/src/vanilla/cap-embed.ts b/packages/sdk-embed/src/vanilla/cap-embed.ts
index 6a02f70b7..5c7720484 100644
--- a/packages/sdk-embed/src/vanilla/cap-embed.ts
+++ b/packages/sdk-embed/src/vanilla/cap-embed.ts
@@ -7,7 +7,7 @@ export function createEmbedUrl(options: EmbedOptions): string {
const params = new URLSearchParams();
params.set("sdk", "1");
params.set("pk", options.publicKey);
- if (options.autoplay) params.set("autoplay", "1");
+ if (options.autoplay) params.set("autoplay", "true");
if (options.branding?.logoUrl) params.set("logo", options.branding.logoUrl);
if (options.branding?.accentColor)
params.set("accent", options.branding.accentColor);
diff --git a/packages/sdk-recorder/__tests__/duration-limit.test.ts b/packages/sdk-recorder/__tests__/duration-limit.test.ts
deleted file mode 100644
index f793953ba..000000000
--- a/packages/sdk-recorder/__tests__/duration-limit.test.ts
+++ /dev/null
@@ -1,89 +0,0 @@
-import { afterEach, expect, mock, test } from "bun:test";
-import { CapRecorder } from "../src/index";
-
-class Track {
- readonly kind = "video";
- readyState = "live";
- onended: (() => void) | null = null;
- stop() {
- this.readyState = "ended";
- }
-}
-
-class Stream {
- constructor(private tracks: Track[]) {}
- getTracks() {
- return this.tracks;
- }
- getVideoTracks() {
- return this.tracks;
- }
- getAudioTracks() {
- return [];
- }
-}
-
-class Encoder {
- state = "inactive";
- static isTypeSupported() {
- return true;
- }
- start() {
- this.state = "recording";
- }
- stop() {
- this.state = "inactive";
- }
-}
-
-const originals = new Map(
- [
- "navigator",
- "MediaStream",
- "MediaRecorder",
- "setTimeout",
- "clearTimeout",
- ].map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]),
-);
-
-afterEach(() => {
- for (const [key, descriptor] of originals) {
- if (descriptor) Object.defineProperty(globalThis, key, descriptor);
- else Reflect.deleteProperty(globalThis, key);
- }
-});
-
-test.each([
- { constructorLimit: undefined, startLimit: undefined, expected: 14_400_000 },
- { constructorLimit: 1500, startLimit: undefined, expected: 1500 },
- { constructorLimit: 1500, startLimit: 700, expected: 700 },
- { constructorLimit: 1500, startLimit: 0, expected: 0 },
-])("schedules $expected ms for the recording", async (scenario) => {
- const stream = new Stream([new Track()]);
- const schedule = mock((_callback: () => void, _delay?: number) => 1);
- const globals = {
- navigator: { mediaDevices: { getUserMedia: async () => stream } },
- MediaStream: Stream,
- MediaRecorder: Encoder,
- setTimeout: schedule,
- clearTimeout: mock(() => {}),
- };
- for (const [key, value] of Object.entries(globals)) {
- Object.defineProperty(globalThis, key, { configurable: true, value });
- }
- const recorder = new CapRecorder({
- publicKey: "test-only",
- mode: "camera",
- microphone: { enabled: false },
- maxDurationMs: scenario.constructorLimit,
- });
- try {
- await recorder.start({ maxDurationMs: scenario.startLimit });
- expect(recorder.phase).toBe("recording");
- expect(schedule.mock.calls).toEqual([
- [expect.any(Function), scenario.expected],
- ]);
- } finally {
- recorder.destroy();
- }
-});
diff --git a/packages/sdk-recorder/__tests__/recorder-options.test.ts b/packages/sdk-recorder/__tests__/recorder-options.test.ts
new file mode 100644
index 000000000..4e575d8c7
--- /dev/null
+++ b/packages/sdk-recorder/__tests__/recorder-options.test.ts
@@ -0,0 +1,245 @@
+import { afterEach, beforeEach, expect, mock, test } from "bun:test";
+import { CapRecorder } from "../src/index";
+
+class Track {
+ readonly kind = "video";
+ readyState = "live";
+ onended: (() => void) | null = null;
+ stop() {
+ this.readyState = "ended";
+ }
+}
+
+class Stream {
+ constructor(private tracks: Track[]) {}
+ getTracks() {
+ return this.tracks;
+ }
+ getVideoTracks() {
+ return this.tracks;
+ }
+ getAudioTracks() {
+ return [];
+ }
+}
+
+class Encoder {
+ static latest: Encoder;
+ constructor() {
+ Encoder.latest = this;
+ }
+ state = "inactive";
+ ondataavailable: ((event: { data: Blob }) => void) | null = null;
+ onstop: (() => void) | null = null;
+ static isTypeSupported() {
+ return true;
+ }
+ start() {
+ this.state = "recording";
+ }
+ pause() {
+ this.state = "paused";
+ }
+ stop() {
+ if (this.state === "inactive") return;
+ this.state = "inactive";
+ if (!this.onstop) return;
+ this.ondataavailable?.({ data: new Blob(["recording"]) });
+ queueMicrotask(this.onstop);
+ }
+}
+
+const originals = new Map(
+ [
+ "navigator",
+ "MediaStream",
+ "MediaRecorder",
+ "setTimeout",
+ "clearTimeout",
+ "fetch",
+ ].map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]),
+);
+
+beforeEach(() => {
+ const stream = new Stream([new Track()]);
+ const globals = {
+ navigator: {
+ mediaDevices: {
+ getUserMedia: async () => stream,
+ getDisplayMedia: async () => stream,
+ },
+ },
+ MediaStream: Stream,
+ MediaRecorder: Encoder,
+ };
+ for (const [key, value] of Object.entries(globals)) {
+ Object.defineProperty(globalThis, key, { configurable: true, value });
+ }
+});
+
+afterEach(() => {
+ for (const [key, descriptor] of originals) {
+ if (descriptor) Object.defineProperty(globalThis, key, descriptor);
+ else Reflect.deleteProperty(globalThis, key);
+ }
+});
+
+test.each([
+ { constructorLimit: undefined, startLimit: undefined, expected: 14_400_000 },
+ { constructorLimit: 1500, startLimit: undefined, expected: 1500 },
+ { constructorLimit: 1500, startLimit: 700, expected: 700 },
+ { constructorLimit: 1500, startLimit: 0, expected: 0 },
+])("schedules $expected ms for the recording", async (scenario) => {
+ const schedule = mock((_callback: () => void, _delay?: number) => 1);
+ const globals = {
+ setTimeout: schedule,
+ clearTimeout: mock(() => {}),
+ };
+ for (const [key, value] of Object.entries(globals)) {
+ Object.defineProperty(globalThis, key, { configurable: true, value });
+ }
+ const recorder = new CapRecorder({
+ publicKey: "test-only",
+ mode: "camera",
+ microphone: { enabled: false },
+ maxDurationMs: scenario.constructorLimit,
+ });
+ try {
+ await recorder.start({ maxDurationMs: scenario.startLimit });
+ expect(recorder.phase).toBe("recording");
+ expect(schedule.mock.calls).toEqual([
+ [expect.any(Function), scenario.expected],
+ ]);
+ } finally {
+ recorder.destroy();
+ }
+});
+
+test.each([
+ { options: {}, expectedBase: "https://cap.so" },
+ {
+ options: { apiBase: undefined, mode: undefined },
+ expectedBase: "https://cap.so",
+ },
+ {
+ options: { apiBase: "https://custom.invalid" },
+ expectedBase: "https://custom.invalid",
+ },
+ { options: { apiBase: "" }, expectedBase: "" },
+])("uploads to '$expectedBase' with options $options", async (scenario) => {
+ const requests: string[] = [];
+ const fetch = mock(async (input: string) => {
+ requests.push(input);
+ if (input.endsWith("/videos/create"))
+ return Response.json({
+ videoId: "test-video",
+ s3Key: "test-key",
+ shareUrl: "https://cap.so/s/test-video",
+ embedUrl: "https://cap.so/embed/test-video",
+ });
+ if (input.endsWith("/initiate"))
+ return Response.json({ uploadId: "test-upload" });
+ if (input.endsWith("/presign-part"))
+ return Response.json({ presignedUrl: "https://upload.invalid/part" });
+ if (input === "https://upload.invalid/part")
+ return new Response(null, { headers: { ETag: "test-etag" } });
+ if (input.endsWith("/complete")) return Response.json({ success: true });
+ throw new Error(`Unexpected request: ${input}`);
+ });
+ Object.defineProperty(globalThis, "fetch", {
+ configurable: true,
+ value: fetch,
+ });
+ const recorder = new CapRecorder({
+ publicKey: "test-only",
+ microphone: { enabled: false },
+ ...scenario.options,
+ });
+ try {
+ await recorder.start();
+ await recorder.stop();
+ expect(recorder.phase).toBe("complete");
+ const base = `${scenario.expectedBase}/api/developer/sdk/v1`;
+ expect(requests).toEqual([
+ `${base}/videos/create`,
+ `${base}/upload/multipart/initiate`,
+ `${base}/upload/multipart/presign-part`,
+ "https://upload.invalid/part",
+ `${base}/upload/multipart/complete`,
+ ]);
+ } finally {
+ recorder.destroy();
+ }
+});
+
+test.each([
+ { paused: false, pendingStop: false },
+ { paused: true, pendingStop: false },
+ { paused: false, pendingStop: true },
+ { paused: true, pendingStop: true },
+])(
+ "saves an ended source with paused=$paused and pendingStop=$pendingStop",
+ async ({ paused, pendingStop }) => {
+ const requests: string[] = [];
+ let uploaded = "";
+ const fetch = mock(async (input: string, init?: RequestInit) => {
+ requests.push(input);
+ if (input.endsWith("/videos/create"))
+ return Response.json({
+ videoId: "test-video",
+ s3Key: "test-key",
+ shareUrl: "https://cap.so/s/test-video",
+ embedUrl: "https://cap.so/embed/test-video",
+ });
+ if (input.endsWith("/initiate"))
+ return Response.json({ uploadId: "test-upload" });
+ if (input.endsWith("/presign-part"))
+ return Response.json({ presignedUrl: "https://upload.invalid/part" });
+ if (input === "https://upload.invalid/part") {
+ uploaded = await new Response(init?.body).text();
+ return new Response(null, { headers: { ETag: "test-etag" } });
+ }
+ if (input.endsWith("/complete")) return Response.json({ success: true });
+ throw new Error(`Unexpected request: ${input}`);
+ });
+ Object.defineProperty(globalThis, "fetch", {
+ configurable: true,
+ value: fetch,
+ });
+ const recorder = new CapRecorder({
+ publicKey: "fixture",
+ mode: "camera",
+ microphone: { enabled: false },
+ });
+ try {
+ await recorder.start();
+ if (paused) recorder.pause();
+ const encoder = Encoder.latest;
+ encoder.ondataavailable?.({ data: new Blob(["recording"]) });
+ encoder.state = "inactive";
+ if (!pendingStop) encoder.onstop?.();
+ let settled = false;
+ const stopping = recorder.stop().then((result) => {
+ settled = true;
+ return result;
+ });
+ await new Promise<void>((resolve) => setImmediate(resolve));
+ expect(settled).toBe(!pendingStop);
+ if (pendingStop) {
+ expect(requests).toEqual([]);
+ encoder.ondataavailable?.({ data: new Blob(["final"]) });
+ encoder.onstop?.();
+ }
+ expect(await stopping).toEqual({
+ videoId: "test-video",
+ shareUrl: "https://cap.so/s/test-video",
+ embedUrl: "https://cap.so/embed/test-video",
+ });
+ expect(recorder.phase).toBe("complete");
+ expect(requests).toContain("https://upload.invalid/part");
+ expect(uploaded).toBe(pendingStop ? "recordingfinal" : "recording");
+ } finally {
+ recorder.destroy();
+ }
+ },
+);
diff --git a/packages/sdk-recorder/__tests__/startup-cancellation.test.ts b/packages/sdk-recorder/__tests__/startup-cancellation.test.ts
new file mode 100644
index 000000000..b773acb05
--- /dev/null
+++ b/packages/sdk-recorder/__tests__/startup-cancellation.test.ts
@@ -0,0 +1,295 @@
+import { afterEach, beforeEach, expect, mock, test } from "bun:test";
+import { CapRecorder } from "../src/index";
+
+class Track {
+ readyState = "live";
+ onended: (() => void) | null = null;
+
+ constructor(readonly kind: "video" | "audio") {}
+
+ stop() {
+ this.readyState = "ended";
+ }
+}
+
+class Stream {
+ constructor(private readonly tracks: Track[]) {}
+
+ getTracks() {
+ return this.tracks;
+ }
+
+ getVideoTracks() {
+ return this.tracks.filter((track) => track.kind === "video");
+ }
+
+ getAudioTracks() {
+ return this.tracks.filter((track) => track.kind === "audio");
+ }
+}
+
+class Encoder {
+ static starts = 0;
+ state = "inactive";
+
+ static isTypeSupported() {
+ return true;
+ }
+
+ start() {
+ Encoder.starts++;
+ this.state = "recording";
+ }
+
+ stop() {
+ this.state = "inactive";
+ }
+}
+
+function deferred<T>() {
+ let resolve!: (value: T) => void;
+ let reject!: (error: Error) => void;
+ const promise = new Promise<T>((res, rej) => {
+ resolve = res;
+ reject = rej;
+ });
+ return { promise, resolve, reject };
+}
+
+const descriptors = [
+ "navigator",
+ "MediaStream",
+ "MediaRecorder",
+ "AudioContext",
+].map(
+ (name) => [name, Object.getOwnPropertyDescriptor(globalThis, name)] as const,
+);
+
+function setGlobal(name: string, value: unknown) {
+ Object.defineProperty(globalThis, name, { configurable: true, value });
+}
+
+beforeEach(() => {
+ Encoder.starts = 0;
+ setGlobal("MediaStream", Stream);
+ setGlobal("MediaRecorder", Encoder);
+});
+
+afterEach(() => {
+ for (const [name, descriptor] of descriptors) {
+ if (descriptor) Object.defineProperty(globalThis, name, descriptor);
+ else Reflect.deleteProperty(globalThis, name);
+ }
+});
+
+test("destroy cancels pending camera startup without requesting the microphone", async () => {
+ const permission = deferred<Stream>();
+ const camera = new Track("video");
+ const microphone = new Track("audio");
+ const getUserMedia = mock((constraints: MediaStreamConstraints) =>
+ constraints.video
+ ? permission.promise
+ : Promise.resolve(new Stream([microphone])),
+ );
+ setGlobal("navigator", { mediaDevices: { getUserMedia } });
+ const recorder = new CapRecorder({ publicKey: "fixture", mode: "camera" });
+
+ try {
+ const starting = recorder.start();
+ expect(recorder.phase).toBe("requesting-permission");
+ recorder.destroy();
+ permission.resolve(new Stream([camera]));
+ await starting;
+
+ expect(recorder.phase).toBe("idle");
+ expect(camera.readyState).toBe("ended");
+ expect(getUserMedia).toHaveBeenCalledTimes(1);
+ expect(Encoder.starts).toBe(0);
+ } finally {
+ recorder.destroy();
+ }
+});
+
+test("destroy releases the screen while microphone permission is pending", async () => {
+ const permission = deferred<Stream>();
+ const screen = new Track("video");
+ const microphone = new Track("audio");
+ const getUserMedia = mock(() => permission.promise);
+ setGlobal("navigator", {
+ mediaDevices: {
+ getDisplayMedia: async () => new Stream([screen]),
+ getUserMedia,
+ },
+ });
+ const recorder = new CapRecorder({
+ publicKey: "fixture",
+ camera: { enabled: false },
+ });
+
+ try {
+ const starting = recorder.start();
+ await new Promise<void>((resolve) => setImmediate(resolve));
+ expect(getUserMedia).toHaveBeenCalledTimes(1);
+ recorder.destroy();
+ const screenAfterDestroy = screen.readyState;
+ permission.resolve(new Stream([microphone]));
+ await starting;
+
+ expect(screenAfterDestroy).toBe("ended");
+ expect(microphone.readyState).toBe("ended");
+ expect(recorder.phase).toBe("idle");
+ expect(Encoder.starts).toBe(0);
+ } finally {
+ recorder.destroy();
+ }
+});
+
+test("destroy releases streams returned before start resumes", async () => {
+ const permission = deferred<Stream>();
+ const camera = new Track("video");
+ setGlobal("navigator", {
+ mediaDevices: { getUserMedia: () => permission.promise },
+ });
+ const recorder = new CapRecorder({
+ publicKey: "fixture",
+ mode: "camera",
+ microphone: { enabled: false },
+ });
+
+ try {
+ const starting = recorder.start();
+ permission.resolve(new Stream([camera]));
+ queueMicrotask(() => recorder.destroy());
+ await starting;
+
+ expect(recorder.phase).toBe("idle");
+ expect(camera.readyState).toBe("ended");
+ expect(Encoder.starts).toBe(0);
+ } finally {
+ recorder.destroy();
+ }
+});
+
+test.each(["grant", "deny"] as const)(
+ "a late screen permission %s cannot change a replacement recording",
+ async (outcome) => {
+ const permission = deferred<Stream>();
+ const oldScreen = new Track("video");
+ const newScreen = new Track("video");
+ const getDisplayMedia = mock()
+ .mockImplementationOnce(() => permission.promise)
+ .mockImplementationOnce(async () => new Stream([newScreen]));
+ setGlobal("navigator", { mediaDevices: { getDisplayMedia } });
+ const recorder = new CapRecorder({
+ publicKey: "fixture",
+ camera: { enabled: false },
+ microphone: { enabled: false },
+ });
+ const errors = mock(() => {});
+ recorder.on("error", errors);
+
+ try {
+ const starting = recorder.start().catch((error: unknown) => error);
+ recorder.destroy();
+ await recorder.start();
+ if (outcome === "grant") permission.resolve(new Stream([oldScreen]));
+ else permission.reject(new Error("Permission denied"));
+ expect(await starting).toBeUndefined();
+
+ expect(recorder.phase).toBe("recording");
+ expect(newScreen.readyState).toBe("live");
+ if (outcome === "grant") expect(oldScreen.readyState).toBe("ended");
+ expect(errors).not.toHaveBeenCalled();
+ expect(Encoder.starts).toBe(1);
+ } finally {
+ recorder.destroy();
+ }
+ },
+);
+
+test.each(["resolve", "reject"] as const)(
+ "destroy cancels audio startup when resume later %ss",
+ async (outcome) => {
+ const startup = deferred<void>();
+ const screen = new Track("video");
+ const system = new Track("audio");
+ const microphone = new Track("audio");
+ const mixed = new Track("audio");
+ const close = mock(async () => {});
+ const resume = mock(() => startup.promise);
+ setGlobal("navigator", {
+ mediaDevices: {
+ getDisplayMedia: async () => new Stream([screen, system]),
+ getUserMedia: async () => new Stream([microphone]),
+ },
+ });
+ setGlobal(
+ "AudioContext",
+ class {
+ close = close;
+ resume = resume;
+ createMediaStreamDestination() {
+ return { stream: new Stream([mixed]) };
+ }
+ createGain() {
+ return { gain: { value: 1 }, connect() {} };
+ }
+ createMediaStreamSource() {
+ return { connect() {} };
+ }
+ },
+ );
+ const recorder = new CapRecorder({
+ publicKey: "fixture",
+ camera: { enabled: false },
+ systemAudio: true,
+ });
+
+ try {
+ const starting = recorder.start().catch((error: unknown) => error);
+ await new Promise<void>((resolve) => setImmediate(resolve));
+ expect(resume).toHaveBeenCalledTimes(1);
+ recorder.destroy();
+ for (const track of [screen, system, microphone, mixed]) {
+ expect(track.readyState).toBe("ended");
+ }
+ if (outcome === "resolve") startup.resolve();
+ else startup.reject(new Error("Audio context closed"));
+ expect(await starting).toBeUndefined();
+ expect(recorder.phase).toBe("idle");
+ expect(close).toHaveBeenCalledTimes(1);
+ expect(Encoder.starts).toBe(0);
+ } finally {
+ recorder.destroy();
+ }
+ },
+);
+
+test("an error handler can destroy and start a replacement recording", async () => {
+ const camera = new Track("video");
+ const getDisplayMedia = mock()
+ .mockRejectedValueOnce(new Error("Permission failed"))
+ .mockResolvedValueOnce(new Stream([camera]));
+ setGlobal("navigator", { mediaDevices: { getDisplayMedia } });
+ const recorder = new CapRecorder({
+ publicKey: "fixture",
+ camera: { enabled: false },
+ microphone: { enabled: false },
+ });
+ let replacement: Promise<void> | undefined;
+ recorder.on("error", () => {
+ recorder.destroy();
+ replacement = recorder.start();
+ });
+
+ try {
+ await expect(recorder.start()).rejects.toThrow("Permission failed");
+ await replacement;
+
+ expect(recorder.phase).toBe("recording");
+ expect(camera.readyState).toBe("live");
+ expect(Encoder.starts).toBe(1);
+ } finally {
+ recorder.destroy();
+ }
+});
diff --git a/packages/sdk-recorder/src/core/stream-manager.ts b/packages/sdk-recorder/src/core/stream-manager.ts
index f93156d09..1f6c99d25 100644
--- a/packages/sdk-recorder/src/core/stream-manager.ts
+++ b/packages/sdk-recorder/src/core/stream-manager.ts
@@ -16,6 +16,7 @@ export interface ManagedStreams {
export async function acquireStreams(
config: StreamConfig,
+ signal: AbortSignal,
): Promise<ManagedStreams> {
const result: ManagedStreams = {
display: null,
@@ -23,50 +24,65 @@ export async function acquireStreams(
microphone: null,
};
- if (config.mode !== "camera") {
- const displayMediaOptions: DisplayMediaStreamOptions = {
- video: true,
- audio: config.systemAudio ?? false,
- };
+ signal.throwIfAborted();
+ const onAbort = () => releaseStreams(result);
+ signal.addEventListener("abort", onAbort, { once: true });
- if (config.mode === "window") {
- (displayMediaOptions as Record<string, unknown>).preferCurrentTab = false;
- }
+ try {
+ if (config.mode !== "camera") {
+ const displayMediaOptions: DisplayMediaStreamOptions = {
+ video: true,
+ audio: config.systemAudio ?? false,
+ };
+
+ if (config.mode === "window") {
+ (displayMediaOptions as Record<string, unknown>).preferCurrentTab =
+ false;
+ }
- result.display =
- await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
- }
+ result.display =
+ await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
+ signal.throwIfAborted();
+ }
- if (config.camera?.enabled !== false) {
- const constraints: MediaStreamConstraints = {
- video: config.camera?.deviceId
- ? { deviceId: { exact: config.camera.deviceId } }
- : true,
- audio: false,
- };
- try {
- result.camera = await navigator.mediaDevices.getUserMedia(constraints);
- } catch {
- result.camera = null;
+ if (config.camera?.enabled !== false) {
+ const constraints: MediaStreamConstraints = {
+ video: config.camera?.deviceId
+ ? { deviceId: { exact: config.camera.deviceId } }
+ : true,
+ audio: false,
+ };
+ try {
+ result.camera = await navigator.mediaDevices.getUserMedia(constraints);
+ } catch {
+ result.camera = null;
+ }
+ signal.throwIfAborted();
}
- }
- if (config.microphone?.enabled !== false) {
- const constraints: MediaStreamConstraints = {
- video: false,
- audio: config.microphone?.deviceId
- ? { deviceId: { exact: config.microphone.deviceId } }
- : true,
- };
- try {
- result.microphone =
- await navigator.mediaDevices.getUserMedia(constraints);
- } catch {
- result.microphone = null;
+ if (config.microphone?.enabled !== false) {
+ const constraints: MediaStreamConstraints = {
+ video: false,
+ audio: config.microphone?.deviceId
+ ? { deviceId: { exact: config.microphone.deviceId } }
+ : true,
+ };
+ try {
+ result.microphone =
+ await navigator.mediaDevices.getUserMedia(constraints);
+ } catch {
+ result.microphone = null;
+ }
+ signal.throwIfAborted();
}
- }
- return result;
+ return result;
+ } catch (error) {
+ releaseStreams(result);
+ throw error;
+ } finally {
+ signal.removeEventListener("abort", onAbort);
+ }
}
export function releaseStreams(streams: ManagedStreams) {
diff --git a/packages/sdk-recorder/src/index.ts b/packages/sdk-recorder/src/index.ts
index 2da81477c..5d10d99de 100644
--- a/packages/sdk-recorder/src/index.ts
+++ b/packages/sdk-recorder/src/index.ts
@@ -35,7 +35,9 @@ export class CapRecorder {
private _videoId: string | null = null;
private _durationMs = 0;
private recorder: MediaRecorder | null = null;
+ private recordingStopped = Promise.resolve();
private streams: ManagedStreams | null = null;
+ private startup: AbortController | null = null;
private chunks: Blob[] = [];
private durationInterval: ReturnType<typeof setInterval> | null = null;
private accumulatedMs = 0;
@@ -51,9 +53,9 @@ export class CapRecorder {
constructor(options: RecorderOptions) {
this.options = {
- apiBase: DEFAULT_API_BASE,
- mode: "fullscreen",
...options,
+ apiBase: options.apiBase ?? DEFAULT_API_BASE,
+ mode: options.mode ?? "fullscreen",
};
this.maxDurationMs = options.maxDurationMs ?? 4 * 60 * 60 * 1000;
this.client = new MultipartClient(
@@ -114,27 +116,42 @@ export class CapRecorder {
throw new Error(`Cannot start from phase "${this._phase}"`);
}
const maxDurationMs = options?.maxDurationMs ?? this.maxDurationMs;
+ const startup = new AbortController();
+ this.startup = startup;
try {
this.setPhase("requesting-permission");
- this.streams = await acquireStreams({
- mode: this.options.mode,
- camera: this.options.camera,
- microphone: this.options.microphone,
- systemAudio: this.options.systemAudio,
- });
+ const streams = await acquireStreams(
+ {
+ mode: this.options.mode,
+ camera: this.options.camera,
+ microphone: this.options.microphone,
+ systemAudio: this.options.systemAudio,
+ },
+ startup.signal,
+ );
+ if (startup.signal.aborted) {
+ releaseStreams(streams);
+ return;
+ }
+ this.streams = streams;
- const composite = await buildCompositeStream(this.streams);
+ const composite = await buildCompositeStream(streams);
+ if (startup.signal.aborted) return;
if (composite.getTracks().length === 0) {
throw new Error("No media tracks available");
}
const mimeType = getSupportedMimeType();
this.selectedMimeType = mimeType;
- this.recorder = new MediaRecorder(composite, {
+ const recorder = new MediaRecorder(composite, {
mimeType: mimeType || undefined,
});
+ this.recorder = recorder;
+ this.recordingStopped = new Promise<void>((resolve) => {
+ recorder.onstop = () => resolve();
+ });
this.chunks = [];
this.recorder.ondataavailable = (e) => {
@@ -161,11 +178,12 @@ export class CapRecorder {
}, maxDurationMs);
this.setPhase("recording");
} catch (error) {
+ if (startup.signal.aborted) return;
+ this.cleanup();
this.setPhase("error");
this.emit("error", {
error: error instanceof Error ? error : new Error(String(error)),
});
- this.cleanup();
throw error;
}
}
@@ -198,17 +216,10 @@ export class CapRecorder {
this.stopDurationTimer();
const recorder = this.recorder;
- return new Promise((resolve, reject) => {
- recorder.onstop = async () => {
- try {
- const result = await this.uploadRecording();
- resolve(result);
- } catch (err) {
- reject(err);
- }
- };
- recorder.stop();
- });
+ const stopped = this.recordingStopped;
+ if (recorder.state !== "inactive") recorder.stop();
+ await stopped;
+ return this.uploadRecording();
}
destroy(): void {
@@ -275,6 +286,8 @@ export class CapRecorder {
}
private cleanup() {
+ this.startup?.abort();
+ this.startup = null;
this.stopDurationTimer();
if (this.maxDurationTimeout) {
clearTimeout(this.maxDurationTimeout);
diff --git a/packages/utils/src/constants/plans.ts b/packages/utils/src/constants/plans.ts
index 92c4ebcdb..c93f216c0 100644
--- a/packages/utils/src/constants/plans.ts
+++ b/packages/utils/src/constants/plans.ts
@@ -16,6 +16,14 @@ export const STRIPE_PLAN_IDS = {
},
};
+export const isValidStripePlanPriceId = (
+ priceId: string,
+ environment: keyof typeof STRIPE_PLAN_IDS,
+): boolean => {
+ const envPlans = STRIPE_PLAN_IDS[environment];
+ return priceId === envPlans.yearly || priceId === envPlans.monthly;
+};
+
export const STRIPE_SIGNED_BAA_PRICE_IDS: Record<string, string> = {
development: "price_1U5xKIFJxA1XpeSsdg4Q8H3Z",
production: "price_1U6C99FJxA1XpeSsUg1rXHo2",
diff --git a/packages/web-backend/src/Auth.ts b/packages/web-backend/src/Auth.ts
index aa6ebda01..cab0346dc 100644
--- a/packages/web-backend/src/Auth.ts
+++ b/packages/web-backend/src/Auth.ts
@@ -1,5 +1,6 @@
import { getServerSession } from "@cap/database/auth/auth-options";
import { isBlockedAccountEmail } from "@cap/database/auth/domain-utils";
+import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site";
import * as Db from "@cap/database/schema";
import {
CurrentUser,
@@ -15,6 +16,19 @@ import { Database } from "./Database.ts";
export const getCurrentUser = Effect.gen(function* () {
const db = yield* Database;
+ const request = yield* Effect.serviceOption(
+ HttpServerRequest.HttpServerRequest,
+ );
+
+ if (
+ Option.isSome(request) &&
+ isCrossSiteCookieWrite({
+ method: request.value.method,
+ url: request.value.originalUrl,
+ headers: new Headers(request.value.headers),
+ })
+ )
+ return Option.none<typeof Db.users.$inferSelect>();
return yield* Option.fromNullable(
yield* Effect.tryPromise(() => getServerSession()),
diff --git a/packages/web-backend/src/S3Buckets/S3ConnectionPool.ts b/packages/web-backend/src/S3Buckets/S3ConnectionPool.ts
index be0a15c8a..d63722a9b 100644
--- a/packages/web-backend/src/S3Buckets/S3ConnectionPool.ts
+++ b/packages/web-backend/src/S3Buckets/S3ConnectionPool.ts
@@ -20,22 +20,27 @@ function boundIdleConnections<T extends HttpAgent>(agent: T) {
return agent;
}
-export const s3ConnectionPool = Effect.acquireRelease(
- Effect.sync(() => {
- const options = {
- keepAlive: true,
- maxSockets: 50,
- maxTotalSockets: 128,
- maxFreeSockets: 8,
- };
- return {
- httpAgent: boundIdleConnections(new HttpAgent(options)),
- httpsAgent: boundIdleConnections(new HttpsAgent(options)),
- };
- }),
- (pool) =>
+const makeS3ConnectionPool = (maxFreeSockets: number, maxSockets = 50) =>
+ Effect.acquireRelease(
Effect.sync(() => {
- pool.httpAgent.destroy();
- pool.httpsAgent.destroy();
+ const options = {
+ keepAlive: true,
+ maxSockets,
+ maxTotalSockets: Math.max(128, maxSockets * 2),
+ maxFreeSockets,
+ };
+ return {
+ httpAgent: boundIdleConnections(new HttpAgent(options)),
+ httpsAgent: boundIdleConnections(new HttpsAgent(options)),
+ };
}),
-);
+ (pool) =>
+ Effect.sync(() => {
+ pool.httpAgent.destroy();
+ pool.httpsAgent.destroy();
+ }),
+ );
+
+export const s3ConnectionPool = makeS3ConnectionPool(8);
+// Desktop source commits copy 128 objects at once in Cap's own bucket.
+export const defaultBucketS3ConnectionPool = makeS3ConnectionPool(32, 128);
diff --git a/packages/web-backend/src/S3Buckets/cloudfront-signed-url.ts b/packages/web-backend/src/S3Buckets/cloudfront-signed-url.ts
new file mode 100644
index 000000000..9085482db
--- /dev/null
+++ b/packages/web-backend/src/S3Buckets/cloudfront-signed-url.ts
@@ -0,0 +1,48 @@
+import * as CloudFrontPresigner from "@aws-sdk/cloudfront-signer";
+
+const encodeKeySegment = (segment: string) =>
+ encodeURIComponent(segment).replace(
+ /[!'()*]/g,
+ (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
+ );
+
+export const getCloudFrontObjectUrl = (bucketUrl: string, key: string) =>
+ `${bucketUrl}/${key.split("/").map(encodeKeySegment).join("/")}`;
+
+export const createCloudFrontSignedUrl = ({
+ bucketUrl,
+ key,
+ keyPairId,
+ privateKey,
+ expiresAt,
+}: {
+ bucketUrl: string;
+ key: string;
+ keyPairId: string;
+ privateKey: string;
+ expiresAt: number;
+}) => {
+ const policy = {
+ Statement: [
+ {
+ Resource: getCloudFrontObjectUrl(bucketUrl, key),
+ Condition: {
+ DateLessThan: {
+ "AWS:EpochTime": expiresAt,
+ },
+ },
+ },
+ ],
+ };
+
+ // No `url` here on purpose: the signer re-encodes `url` with
+ // encodeURIComponent(decodeURIComponent(segment)), which turns %2A back into
+ // a literal `*`. Without `url` it uses the policy Resource verbatim, so the
+ // returned URL matches the signed Resource byte for byte, and CloudFront
+ // compares against the raw request URL.
+ return CloudFrontPresigner.getSignedUrl({
+ keyPairId,
+ privateKey,
+ policy: JSON.stringify(policy),
+ });
+};
diff --git a/packages/web-backend/src/S3Buckets/index.ts b/packages/web-backend/src/S3Buckets/index.ts
index 49ecb912e..e3edfe307 100644
--- a/packages/web-backend/src/S3Buckets/index.ts
+++ b/packages/web-backend/src/S3Buckets/index.ts
@@ -1,5 +1,4 @@
import * as S3 from "@aws-sdk/client-s3";
-import * as CloudFrontPresigner from "@aws-sdk/cloudfront-signer";
import { decrypt } from "@cap/database/crypto";
import type { Organisation, S3Bucket, User } from "@cap/web-domain";
import type { RequestPresigningArguments } from "@smithy/types";
@@ -7,16 +6,21 @@ import { Config, Effect, Layer, Option } from "effect";
import { AwsCredentials } from "../Aws.ts";
import { Database } from "../Database.ts";
+import { createCloudFrontSignedUrl } from "./cloudfront-signed-url.ts";
import { createS3BucketAccess } from "./S3BucketAccess.ts";
import { S3BucketClientProvider } from "./S3BucketClientProvider.ts";
import { S3BucketsRepo } from "./S3BucketsRepo.ts";
-import { s3ConnectionPool } from "./S3ConnectionPool.ts";
+import {
+ defaultBucketS3ConnectionPool,
+ s3ConnectionPool,
+} from "./S3ConnectionPool.ts";
export class S3Buckets extends Effect.Service<S3Buckets>()("S3Buckets", {
scoped: Effect.gen(function* () {
const repo = yield* S3BucketsRepo;
const { credentials } = yield* AwsCredentials;
const requestHandler = yield* s3ConnectionPool;
+ const defaultBucketRequestHandler = yield* defaultBucketS3ConnectionPool;
const defaultConfigs = {
publicEndpoint: yield* Config.string("S3_PUBLIC_ENDPOINT").pipe(
@@ -45,7 +49,7 @@ export class S3Buckets extends Effect.Service<S3Buckets>()("S3Buckets", {
credentials: defaultConfigs.credentials,
forcePathStyle: defaultConfigs.forcePathStyle,
requestStreamBufferSize: 16 * 1024,
- requestHandler,
+ requestHandler: defaultBucketRequestHandler,
});
const defaultInternalClient = createDefaultClient(true);
const defaultPublicClient = createDefaultClient(false);
@@ -107,29 +111,15 @@ export class S3Buckets extends Effect.Service<S3Buckets>()("S3Buckets", {
key: string,
signingArgs?: RequestPresigningArguments,
) => {
- const url = `${cloudfrontEnvs.bucketUrl}/${key}`;
const expiresIn = signingArgs?.expiresIn ?? 3600;
- const expires = Math.floor((Date.now() + expiresIn * 1000) / 1000);
-
- const policy = {
- Statement: [
- {
- Resource: url,
- Condition: {
- DateLessThan: {
- "AWS:EpochTime": Math.floor(expires),
- },
- },
- },
- ],
- };
return Effect.succeed(
- CloudFrontPresigner.getSignedUrl({
- url,
+ createCloudFrontSignedUrl({
+ bucketUrl: cloudfrontEnvs.bucketUrl,
+ key,
keyPairId: cloudfrontEnvs.keypairId,
privateKey: cloudfrontEnvs.privateKey,
- policy: JSON.stringify(policy),
+ expiresAt: Math.floor((Date.now() + expiresIn * 1000) / 1000),
}),
);
};
diff --git a/packages/web-backend/src/Videos/VideosRepo.ts b/packages/web-backend/src/Videos/VideosRepo.ts
index bf9cd10e6..5832c41eb 100644
--- a/packages/web-backend/src/Videos/VideosRepo.ts
+++ b/packages/web-backend/src/Videos/VideosRepo.ts
@@ -130,9 +130,19 @@ export class VideosRepo extends Effect.Service<VideosRepo>()("VideosRepo", {
yield* db.use((db) =>
db.transaction(async (db) => {
+ const {
+ createdAt: _createdAt,
+ updatedAt: _updatedAt,
+ id: _id,
+ ...insertData
+ } = data as CreateVideoInput & {
+ createdAt?: unknown;
+ updatedAt?: unknown;
+ id?: unknown;
+ };
await db.insert(Db.videos).values([
{
- ...data,
+ ...insertData,
id,
orgId: data.orgId,
bucket: Option.getOrNull(data.bucketId ?? Option.none()),
diff --git a/packages/web-backend/src/Videos/index.ts b/packages/web-backend/src/Videos/index.ts
index 717a6209f..2d27caa09 100644
--- a/packages/web-backend/src/Videos/index.ts
+++ b/packages/web-backend/src/Videos/index.ts
@@ -241,14 +241,36 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
if (videoIds.length === 0)
return [] as Array<Exit.Exit<{ count: number }, unknown>>;
+ const rows = yield* db.use((database) =>
+ database
+ .select({
+ id: Db.videos.id,
+ orgId: Db.videos.orgId,
+ ownerId: Db.videos.ownerId,
+ public: Db.videos.public,
+ password: Db.videos.password,
+ })
+ .from(Db.videos)
+ .where(Dz.inArray(Db.videos.id, [...new Set(videoIds)])),
+ );
+ const videosById = new Map(rows.map((video) => [video.id, video]));
const videoExits = yield* Effect.forEach(
videoIds,
- (videoId) => getByIdForViewing(videoId).pipe(Effect.exit),
+ (videoId) => {
+ const video = videosById.get(videoId);
+ if (!video)
+ return Effect.succeed(
+ Exit.succeed(Option.none<(typeof rows)[number]>()),
+ );
+ return policy
+ .canViewLoaded(video, Option.fromNullable(video.password))
+ .pipe(Effect.as(Option.some(video)), Effect.exit);
+ },
{ concurrency: 10 },
);
const analyticsVideos = videoExits.flatMap((exit) => {
if (!Exit.isSuccess(exit) || Option.isNone(exit.value)) return [];
- const [video] = exit.value.value;
+ const video = exit.value.value;
return [{ id: video.id, orgId: video.orgId }];
});
const countsByPathname = yield* getAnalyticsCounts(analyticsVideos);
@@ -352,7 +374,7 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
const maybeVideo = yield* policy.getOwnedById(videoId);
if (Option.isNone(maybeVideo))
return yield* Effect.fail(new Video.NotFoundError());
- const [video] = maybeVideo.value;
+ const [video, password] = maybeVideo.value;
const [bucket] = yield* storage.getAccessForVideo(video);
@@ -413,9 +435,13 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
}
} while (continuationToken);
publicationAttempted = true;
+ const hasPassword = Option.isSome(password);
yield* repo.create(
{
- ...video,
+ ownerId: video.ownerId,
+ orgId: video.orgId,
+ name: video.name,
+ public: video.public,
source:
publishedKeys.size > 0
? {
@@ -428,8 +454,17 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
metadata: Option.map(video.metadata, (metadata) => {
const copied = { ...metadata };
delete copied.desktopRecordingUpload;
+ delete copied.customCreatedAt;
return copied;
}),
+ bucketId: video.bucketId,
+ storageIntegrationId: video.storageIntegrationId,
+ folderId: video.folderId,
+ transcriptionStatus: video.transcriptionStatus,
+ width: video.width,
+ height: video.height,
+ duration: video.duration,
+ ...(hasPassword ? { password: password.value } : {}),
},
{ id: newVideoId },
);
diff --git a/packages/web-domain/src/Mobile.ts b/packages/web-domain/src/Mobile.ts
index 5c4dc4cde..ac922556a 100644
--- a/packages/web-domain/src/Mobile.ts
+++ b/packages/web-domain/src/Mobile.ts
@@ -3,6 +3,7 @@ import {
HttpApiEndpoint,
HttpApiError,
HttpApiGroup,
+ HttpApiSchema,
OpenApi,
} from "@effect/platform";
import { Schema } from "effect";
@@ -152,6 +153,13 @@ export const MobileEmailSessionVerifyInput = Schema.Struct({
code: Schema.String,
});
+export class MobileTooManyRequests extends HttpApiSchema.EmptyError<MobileTooManyRequests>()(
+ {
+ tag: "TooManyRequests",
+ status: 429,
+ },
+) {}
+
export const MobileAccountDeletionInput = Schema.Struct({
confirmation: Schema.Literal("DELETE"),
});
@@ -547,7 +555,8 @@ export class MobileHttpApi extends HttpApiGroup.make("mobile")
.addError(HttpApiError.InternalServerError)
.addError(HttpApiError.BadRequest)
.addError(HttpApiError.Forbidden)
- .addError(HttpApiError.NotFound),
+ .addError(HttpApiError.NotFound)
+ .addError(MobileTooManyRequests),
)
.add(
HttpApiEndpoint.post("verifyEmailSession", "/session/email/verify")
@@ -556,7 +565,8 @@ export class MobileHttpApi extends HttpApiGroup.make("mobile")
.addError(HttpApiError.InternalServerError)
.addError(HttpApiError.BadRequest)
.addError(HttpApiError.Forbidden)
- .addError(HttpApiError.NotFound),
+ .addError(HttpApiError.NotFound)
+ .addError(MobileTooManyRequests),
)
.add(
HttpApiEndpoint.post("revokeSession", "/session/revoke")
diff --git a/scripts/prepare-gpui-dependency.sh b/scripts/prepare-gpui-dependency.sh
index bffbd90c1..18f1b8eb9 100755
--- a/scripts/prepare-gpui-dependency.sh
+++ b/scripts/prepare-gpui-dependency.sh
@@ -8,6 +8,7 @@ patch_paths=(
"apps/desktop-gpui/patches/zed-windows.patch"
"apps/desktop-gpui/patches/zed-linux.patch"
"apps/desktop-gpui/patches/zed-idle-frames.patch"
+ "apps/desktop-gpui/patches/zed-streaming-image.patch"
)
patch_files=("${patch_paths[@]/#/$repo_root/}")
base_revision="5d1f83d9f27a19bec1fb241dc33b42238af9cf8d"
diff --git a/scripts/prepare-gpui-dependency.test.mjs b/scripts/prepare-gpui-dependency.test.mjs
index e66507100..98a920eb1 100644
--- a/scripts/prepare-gpui-dependency.test.mjs
+++ b/scripts/prepare-gpui-dependency.test.mjs
@@ -122,6 +122,7 @@ test("dependency verification handles overlapping patches without changing the c
writeFileSync(path.join(upstream, "renderer.rs"), "base\n");
writeFileSync(path.join(upstream, "windows.rs"), "base\n");
writeFileSync(path.join(upstream, "frames.rs"), "base\n");
+ writeFileSync(path.join(upstream, "atlas.rs"), "base\n");
commit();
const base = git(upstream, "rev-parse", "HEAD").trim();
const patch = (name) => {
@@ -142,6 +143,8 @@ test("dependency verification handles overlapping patches without changing the c
patch("zed-linux.patch");
writeFileSync(path.join(upstream, "frames.rs"), "paced frames\n");
patch("zed-idle-frames.patch");
+ writeFileSync(path.join(upstream, "atlas.rs"), "streaming images\n");
+ patch("zed-streaming-image.patch");
const script = path.join(cap, "scripts/prepare-gpui-dependency.sh");
writeFileSync(
script,
@@ -165,6 +168,10 @@ test("dependency verification handles overlapping patches without changing the c
readFileSync(path.join(zed, "frames.rs"), "utf8"),
"paced frames\n",
);
+ assert.equal(
+ readFileSync(path.join(zed, "atlas.rs"), "utf8"),
+ "streaming images\n",
+ );
});
writeFileSync(path.join(zed, "unrelated.txt"), "preserve staged work\n");
diff --git a/scripts/setup.js b/scripts/setup.js
index d61d0898c..c400a297b 100644
--- a/scripts/setup.js
+++ b/scripts/setup.js
@@ -71,9 +71,9 @@ async function main() {
if (!(await fileExists(nativeDepsTarPath))) {
console.log(`Downloading ${nativeDepsTar}`);
- const nativeDepsBytes = await fetch(`${NATIVE_DEPS_URL}/${nativeDepsTar}`)
- .then((r) => r.blob())
- .then((b) => b.arrayBuffer());
+ const nativeDepsBytes = await downloadBytes(
+ `${NATIVE_DEPS_URL}/${nativeDepsTar}`,
+ );
await fs.writeFile(nativeDepsTarPath, Buffer.from(nativeDepsBytes));
console.log("Downloaded native deps");
downloadedNativeDeps = true;
@@ -148,9 +148,7 @@ async function main() {
const ffmpegZip = `ffmpeg-${FFMPEG_VERSION}.zip`;
const ffmpegZipPath = path.join(targetDir, ffmpegZip);
if (!(await fileExists(ffmpegZipPath))) {
- const ffmpegZipBytes = await fetch(FFMPEG_ZIP_URL)
- .then((r) => r.blob())
- .then((b) => b.arrayBuffer());
+ const ffmpegZipBytes = await downloadBytes(FFMPEG_ZIP_URL);
await fs.writeFile(ffmpegZipPath, Buffer.from(ffmpegZipBytes));
console.log(`Downloaded ${ffmpegZip}`);
downloadedFfmpeg = true;
@@ -273,9 +271,9 @@ async function main() {
let downloadedNativeDeps = false;
if (!(await fileExists(nativeDepsTarPath))) {
console.log(`Downloading ${nativeDepsTar}`);
- const bytes = await fetch(`${NATIVE_DEPS_URL}/${nativeDepsTar}`)
- .then((r) => r.blob())
- .then((b) => b.arrayBuffer());
+ const bytes = await downloadBytes(
+ `${NATIVE_DEPS_URL}/${nativeDepsTar}`,
+ );
await fs.writeFile(nativeDepsTarPath, Buffer.from(bytes));
console.log("Downloaded native deps");
downloadedNativeDeps = true;
@@ -441,9 +439,7 @@ async function setupMacOSOnnxRuntime() {
if (!(await fileExists(archivePath))) {
console.log(`Downloading ${asset.name}`);
- const bytes = await fetch(url)
- .then((r) => r.blob())
- .then((b) => b.arrayBuffer());
+ const bytes = await downloadBytes(url);
await fs.writeFile(archivePath, Buffer.from(bytes));
console.log(`Downloaded ${asset.name}`);
} else console.log(`Using cached ${asset.name}`);
@@ -495,9 +491,7 @@ async function setupWindowsOnnxRuntime() {
if (!(await fileExists(archivePath))) {
console.log(`Downloading ${asset.name}`);
- const bytes = await fetch(url)
- .then((r) => r.blob())
- .then((b) => b.arrayBuffer());
+ const bytes = await downloadBytes(url);
await fs.writeFile(archivePath, Buffer.from(bytes));
console.log(`Downloaded ${asset.name}`);
} else console.log(`Using cached ${asset.name}`);
@@ -706,6 +700,13 @@ async function signMacOSDylib(filePath) {
);
}
+async function downloadBytes(url) {
+ const response = await fetch(url);
+ if (!response.ok)
+ throw new Error(`Failed to download ${url}: HTTP ${response.status}`);
+ return response.arrayBuffer();
+}
+
async function fileExists(path) {
return await fs
.access(path)
diff --git a/scripts/setup.test.mjs b/scripts/setup.test.mjs
new file mode 100644
index 000000000..2f6f979e6
--- /dev/null
+++ b/scripts/setup.test.mjs
@@ -0,0 +1,180 @@
+import assert from "node:assert/strict";
+import { execFile as execFileCallback } from "node:child_process";
+import { once } from "node:events";
+import {
+ copyFileSync,
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
+import { createServer } from "node:http";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import test from "node:test";
+import { fileURLToPath } from "node:url";
+import { promisify } from "node:util";
+
+const execFile = promisify(execFileCallback);
+const repositoryRoot = fileURLToPath(new URL("../", import.meta.url));
+
+test(
+ "setup retries failed downloads without poisoning the archive cache",
+ {
+ skip: process.platform !== "linux" && "Linux setup fixture",
+ timeout: 60_000,
+ },
+ async (t) => {
+ const fixture = mkdtempSync(path.join(tmpdir(), "cap-setup-"));
+ t.after(() => rmSync(fixture, { recursive: true, force: true }));
+ for (const file of [
+ "package.json",
+ "scripts/setup.js",
+ "scripts/linux-bundle-config.mjs",
+ "apps/desktop/src-tauri/tauri.conf.json",
+ "apps/desktop-gpui/tauri.conf.json",
+ ]) {
+ const destination = path.join(fixture, file);
+ mkdirSync(path.dirname(destination), { recursive: true });
+ copyFileSync(path.join(repositoryRoot, file), destination);
+ }
+
+ const nativeName = "native-deps-x86_64-linux-gnu.tar.xz";
+ const onnxName = "onnxruntime-linux-x64-1.24.3.tgz";
+ const nativeUrl = `/spacedriveapp/native-deps/releases/download/v0.26/${nativeName}`;
+ const onnxUrl = `/microsoft/onnxruntime/releases/download/v1.24.3/${onnxName}`;
+ const nativeLibrary = "synthetic native library\n";
+ const onnxLibrary = "synthetic ONNX library\n";
+ const archives = new Map();
+ for (const [url, entry, contents] of [
+ [nativeUrl, "lib/libavcodec.so.61", nativeLibrary],
+ [onnxUrl, `${onnxName.slice(0, -4)}/lib/libonnxruntime.so`, onnxLibrary],
+ ]) {
+ const archive = path.join(fixture, "assets", path.basename(url));
+ const directory = `${archive}.contents`;
+ mkdirSync(path.dirname(path.join(directory, entry)), { recursive: true });
+ writeFileSync(path.join(directory, entry), contents);
+ await execFile(
+ "tar",
+ [url.endsWith(".xz") ? "-cJf" : "-czf", archive, "-C", directory, "."],
+ { signal: t.signal },
+ );
+ archives.set(url, readFileSync(archive));
+ }
+
+ let failNative = true;
+ const requests = [];
+ const server = createServer((request, response) => {
+ requests.push(request.url);
+ if (failNative && request.url === nativeUrl) {
+ response.writeHead(503).end("Service Unavailable\n");
+ return;
+ }
+ const archive = archives.get(request.url);
+ response.writeHead(archive ? 200 : 404);
+ response.end(archive ?? "Unexpected setup download");
+ });
+ server.listen(0, "127.0.0.1");
+ await once(server, "listening");
+ t.after(async () => {
+ server.closeAllConnections();
+ await promisify(server.close.bind(server))();
+ });
+ const origin = `http://127.0.0.1:${server.address().port}`;
+ const preload = path.join(fixture, "http-fixture.mjs");
+ writeFileSync(
+ preload,
+ `const networkFetch = globalThis.fetch;
+globalThis.fetch = (input, options) => {
+ const url = new URL(input);
+ if (url.origin !== "https://github.com")
+ throw new Error(\`Unexpected external request: \${url}\`);
+ return networkFetch(new URL(url.pathname, ${JSON.stringify(origin)}), options);
+};
+`,
+ );
+ const runSetup = () =>
+ execFile(process.execPath, ["--import", preload, "scripts/setup.js"], {
+ cwd: fixture,
+ signal: t.signal,
+ timeout: 20_000,
+ env: {
+ ...process.env,
+ NODE_OPTIONS: "",
+ RUST_TARGET_TRIPLE: "x86_64-unknown-linux-gnu",
+ CAP_USE_SCCACHE: "0",
+ TMPDIR: fixture,
+ },
+ }).then(
+ ({ stdout, stderr }) => ({ code: 0, stdout, stderr }),
+ (error) => {
+ assert.equal(typeof error.code, "number", error.message);
+ return error;
+ },
+ );
+
+ const cache = path.join(fixture, "target", `v0.26-${nativeName}`);
+ const failed = await runSetup();
+ assert.equal(failed.code, 1, failed.stderr);
+ assert.deepEqual(requests, [nativeUrl]);
+ assert.equal(
+ existsSync(cache),
+ false,
+ "failed downloads must not be cached",
+ );
+ assert.match(failed.stderr, /HTTP 503/);
+
+ failNative = false;
+ for (const expectedRequests of [[nativeUrl, onnxUrl], []]) {
+ requests.length = 0;
+ const result = await runSetup();
+ assert.equal(result.code, 0, result.stderr);
+ assert.deepEqual(requests, expectedRequests);
+ assert.deepEqual(readFileSync(cache), archives.get(nativeUrl));
+ for (const [name, contents] of [
+ ["libavcodec.so.61", nativeLibrary],
+ ["libonnxruntime.so", onnxLibrary],
+ ["libonnxruntime.so.1", onnxLibrary],
+ ]) {
+ for (const directory of [
+ "target/debug",
+ "target/release",
+ "target/x86_64-unknown-linux-gnu/debug",
+ "target/x86_64-unknown-linux-gnu/release",
+ ]) {
+ assert.equal(
+ readFileSync(path.join(fixture, directory, name), "utf8"),
+ contents,
+ );
+ }
+ for (const directory of [
+ "apps/desktop/src-tauri",
+ "apps/desktop-gpui",
+ ]) {
+ const config = JSON.parse(
+ readFileSync(
+ path.join(fixture, directory, "tauri.linux.conf.json"),
+ "utf8",
+ ),
+ );
+ const source = config.bundle.linux.deb.files[`/usr/lib/cap/${name}`];
+ assert.equal(
+ readFileSync(path.resolve(fixture, directory, source), "utf8"),
+ contents,
+ );
+ }
+ }
+ const cargo = readFileSync(
+ path.join(fixture, ".cargo/config.toml"),
+ "utf8",
+ );
+ assert.match(cargo, /\[target\.x86_64-unknown-linux-gnu\]/);
+ assert.match(
+ cargo,
+ /ORT_DYLIB_PATH = .*native-deps\/onnxruntime\/lib\/libonnxruntime\.so/,
+ );
+ }
+ },
+);
diff --git a/scripts/verify/perf-ab.py b/scripts/verify/perf-ab.py
index 2d48a702c..0e4c8de5b 100644
--- a/scripts/verify/perf-ab.py
+++ b/scripts/verify/perf-ab.py
@@ -50,7 +50,7 @@ def run_once(side, target, case, tier, fixtures, report):
CAP_VERIFY_CASE=case,
CAP_VERIFY_TIER=tier,
)
- report.mkdir(parents=True, exist_ok=True)
+ report.mkdir(parents=True)
started = time.monotonic()
with (report / "test.log").open("w") as log:
process = subprocess.Popen(
@@ -87,7 +87,7 @@ def main():
parser.add_argument("--base", required=True)
parser.add_argument("--head", required=True)
parser.add_argument("--fixtures", required=True)
- parser.add_argument("--out", required=True)
+ parser.add_argument("--out", required=True, help="use a fresh path for each comparison; existing reports are preserved")
parser.add_argument("--pairs", type=int, default=3)
parser.add_argument("--summary")
args = parser.parse_args()
diff --git a/scripts/verify/suites.json b/scripts/verify/suites.json
index cdc47dadb..3f19a2759 100644
--- a/scripts/verify/suites.json
+++ b/scripts/verify/suites.json
@@ -191,14 +191,14 @@
"cases": {
"prefix": "audio/",
"fast": {
- "linux": 56,
- "macos": 40,
- "windows": 39
+ "linux": 58,
+ "macos": 41,
+ "windows": 40
},
"full": {
- "linux": 58,
- "macos": 58,
- "windows": 58
+ "linux": 60,
+ "macos": 60,
+ "windows": 60
}
}
},
diff --git a/scripts/verify/test-perf-ab.py b/scripts/verify/test-perf-ab.py
new file mode 100644
index 000000000..8b7f91256
--- /dev/null
+++ b/scripts/verify/test-perf-ab.py
@@ -0,0 +1,91 @@
+import importlib.util
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+import tempfile
+import unittest
+
+
+SCRIPT = Path(__file__).with_name("perf-ab.py").resolve()
+SPEC = importlib.util.spec_from_file_location("perf_ab", SCRIPT)
+PERF = importlib.util.module_from_spec(SPEC)
+SPEC.loader.exec_module(PERF)
+
+
[email protected](hasattr(os, "wait4"), "The performance runner requires POSIX wait4")
+class PerformanceReportTests(unittest.TestCase):
+ def setUp(self):
+ self.temporary = tempfile.TemporaryDirectory()
+ self.addCleanup(self.temporary.cleanup)
+ self.root = Path(self.temporary.name).resolve()
+ (self.root / "crates" / "verify").mkdir(parents=True)
+
+ def side(self, records, exit_code=0):
+ binary = self.root / "fixture.py"
+ binary.with_suffix(".json").write_text(json.dumps({"records": records, "exit": exit_code}))
+ binary.write_text(f"#!{sys.executable}\n" + r"""import json
+import os
+from pathlib import Path
+import sys
+
+Path(__file__).with_suffix(".started").touch()
+fixture = json.loads(Path(__file__).with_suffix(".json").read_text())
+if fixture["records"]:
+ report = Path(os.environ["CAP_VERIFY_REPORT_DIR"])
+ path = report / f"cap-verify-{os.getpid()}.jsonl"
+ path.write_text("".join(json.dumps(row) + "\n" for row in fixture["records"]))
+print("fixture completed")
+sys.exit(fixture["exit"])
+""")
+ binary.chmod(0o700)
+ return {
+ "worktree": str(self.root),
+ "binaries": {target: str(binary) for _, target, _, _, _ in PERF.WORKLOADS},
+ }
+
+ def test_existing_reports_are_preserved_and_never_used(self):
+ side = self.side([])
+ report = self.root / "reports"
+ report.mkdir()
+ stale = report / "cap-verify-123.jsonl"
+ previous = json.dumps({"case": "export/basic/output/mp4/native", "outcome": "pass"}) + "\n"
+ stale.write_text(previous)
+ log = report / "test.log"
+ log.write_text("previous run\n")
+ with self.assertRaises(FileExistsError):
+ PERF.run_once(side, "export", "export/basic/*", "fast", self.root / "fixtures", report)
+ self.assertEqual(stale.read_text(), previous)
+ self.assertEqual(log.read_text(), "previous run\n")
+ self.assertFalse((self.root / "fixture.started").exists())
+
+ def test_fresh_reports_preserve_exit_status_and_outcomes(self):
+ outcomes = ["pass", "xfail", "xpass", "fail", "unsupported", "inconclusive"]
+ rows = [{"case": f"export/basic/{outcome}", "outcome": outcome} for outcome in outcomes]
+ result = PERF.run_once(
+ self.side(rows, 101), "export", "export/basic/*", "fast",
+ self.root / "fixtures", self.root / "reports",
+ )
+ self.assertEqual(result["exit"], 101)
+ self.assertEqual(result["outcomes"], {
+ f"export/basic/{outcome}": "ran" if outcome in {"pass", "xfail", "xpass"} else outcome
+ for outcome in outcomes
+ })
+
+ def test_cli_rejects_fresh_runs_that_report_no_cases(self):
+ manifest = self.root / "binaries.json"
+ manifest.write_text(json.dumps(self.side([])))
+ output = self.root / "output"
+ output.mkdir()
+ result = subprocess.run([
+ sys.executable, "-B", str(SCRIPT), "--base", str(manifest), "--head", str(manifest),
+ "--fixtures", str(self.root / "fixtures"), "--out", str(output), "--pairs", "1",
+ ], capture_output=True, text=True, timeout=30)
+ self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
+ self.assertIn("not measured:", result.stdout)
+ self.assertEqual((output / "perf-budget.md").read_text().count("BROKEN:"), len(PERF.WORKLOADS))
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/turbo.json b/turbo.json
index bf4056ab1..4f39d6f34 100644
--- a/turbo.json
+++ b/turbo.json
@@ -1,6 +1,6 @@
{
"$schema": "https://turbo.build/schema.json",
- "globalDependencies": [".env", ".env"],
+ "globalDependencies": [".env"],
"globalEnv": ["*"],
"tasks": {
"build": {