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

Ilyaa78df697dd27
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 828ca0a31..7a2bf6b2e 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -128,6 +128,8 @@ jobs:
- name: Test web React compatibility
run: >-
bun run --cwd apps/web test
+ __tests__/unit/analytics-track-route.test.ts
+ __tests__/unit/caps-analytics-round-trips.test.ts
__tests__/unit/caps-keyboard.test.ts
__tests__/unit/comment-composer-composition.test.ts
__tests__/unit/dashboard-analytics-cap-access.test.ts
@@ -143,15 +145,21 @@ jobs:
__tests__/unit/media-format-converter-selection.test.ts
__tests__/unit/media-format-converter.test.ts
__tests__/unit/media-player-store-refs.test.ts
+ __tests__/unit/mobile-caps-list.test.ts
__tests__/unit/notification-read-state.test.ts
__tests__/unit/playback-store.test.ts
__tests__/unit/remove-organization-member.test.ts
+ __tests__/unit/reply-notification.test.ts
__tests__/unit/seo-demo-videos.test.ts
+ __tests__/unit/share-page-round-trips.test.ts
+ __tests__/unit/share-transcript-rendering.test.ts
+ __tests__/unit/share-video-quota-loading.test.ts
__tests__/unit/signed-baa-card.test.ts
__tests__/unit/space-folder-page-access.test.ts
__tests__/unit/space-page-round-trips.test.ts
__tests__/unit/speed-controller-playback-failure.test.ts
__tests__/unit/speed-controller.test.ts
+ __tests__/unit/summary-chapters.test.ts
__tests__/unit/summary-editor.test.ts
__tests__/unit/sso-login-pages.test.ts
__tests__/unit/timeline-paused-window.test.ts
@@ -159,6 +167,9 @@ jobs:
__tests__/unit/transcript-hours.test.ts
__tests__/unit/transcript-numbers.test.ts
__tests__/unit/video-analytics-access.test.ts
+ __tests__/unit/video-analytics-bulk.test.ts
+ __tests__/unit/video-create-atomicity.test.ts
+ __tests__/unit/video-download-permissions.test.ts
__tests__/unit/voice-note-playback.test.ts
- name: Typecheck
diff --git a/.github/workflows/desktop-release.yml b/.github/workflows/desktop-release.yml
index db087d09b..f4c6d54a1 100644
--- a/.github/workflows/desktop-release.yml
+++ b/.github/workflows/desktop-release.yml
@@ -745,6 +745,17 @@ jobs:
echo "::error::The Cap AppImage bundles PipeWire that can conflict with host ALSA plugins"
exit 1
fi
+ if [[ -n "$(find "$APPROOT/usr/lib" -maxdepth 1 -name 'libva*.so*' -print -quit)" ]]; then
+ echo "::error::The Cap AppImage puts its libva where Cap loads it ahead of the host's, which breaks host PipeWire plugins"
+ exit 1
+ fi
+ test -e "$APPROOT/usr/lib/cap/libva/libva.so.2"
+ test -e "$APPROOT/usr/lib/cap/libva/libva-drm.so.2"
+ grep -qx 'exec "$cap_appdir/usr/bin/Cap" "$@"' "$APPROOT/AppRun"
+ if grep -q 'AppRun.cap-original' "$APPROOT/AppRun"; then
+ echo "::error::The Cap AppImage still starts through the generic AppImage launcher"
+ exit 1
+ fi
- name: Build Arch Linux package
id: build_arch
diff --git a/.github/workflows/docker-build-media-server.yml b/.github/workflows/docker-build-media-server.yml
index 12eca1b33..8e27a8239 100644
--- a/.github/workflows/docker-build-media-server.yml
+++ b/.github/workflows/docker-build-media-server.yml
@@ -129,6 +129,10 @@ jobs:
src/__tests__/lib/media-routes-real-world.integration.test.ts
docker run --rm --network none --entrypoint bun "$MEDIA_IMAGE" test \
src/__tests__/lib/media-video.integration.test.ts
+ docker run --rm --network none --entrypoint bun "$MEDIA_IMAGE" test \
+ src/__tests__/lib/video-input-validation.integration.test.ts
+ docker run --rm --network none --entrypoint bun "$MEDIA_IMAGE" test \
+ src/__tests__/loom-importer/
- name: Verify long recording performance in the production image
env:
diff --git a/.github/workflows/instant-reliability.yml b/.github/workflows/instant-reliability.yml
index 137864ea6..a0655c46e 100644
--- a/.github/workflows/instant-reliability.yml
+++ b/.github/workflows/instant-reliability.yml
@@ -5,7 +5,8 @@ name: Instant reliability (real app)
# server video's creation through the first seconds of capture, and a crash
# followed by Recover. Each case checks what reached the mock server and what
# was left on disk, so a regression that loses a recording, deletes one with
-# media, or leaves an empty video behind fails here.
+# media, or leaves an empty video behind fails here. The Linux job runs the
+# same cases on Xvfb, recording another app's window.
on:
pull_request:
@@ -185,3 +186,88 @@ jobs:
name: instant-reliability-sandboxes
path: ${{ runner.temp }}/real-app
retention-days: 7
+
+ real-app-linux:
+ name: Instant upload and recovery on Linux (real app)
+ needs: scope
+ if: needs.scope.outputs.run == 'true'
+ runs-on: blacksmith-8vcpu-ubuntu-2404
+ timeout-minutes: 60
+ env:
+ CARGO_PROFILE_DEV_DEBUG: "0"
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Install Linux desktop dependencies
+ uses: ./.github/actions/install-desktop-deps
+
+ - name: Rust setup
+ uses: dtolnay/[email protected]
+ with:
+ targets: x86_64-unknown-linux-gnu
+
+ - uses: ./.github/actions/setup-rust-cache
+ with:
+ target: x86_64-unknown-linux-gnu
+ prefix-key: v1-rust-gpui
+ save-cache: "false"
+ workspaces: apps/desktop-gpui -> target
+
+ - name: Run setup
+ run: node scripts/setup.js
+ env:
+ RUST_TARGET_TRIPLE: x86_64-unknown-linux-gnu
+
+ - name: Prepare GPUI dependency
+ run: |
+ node scripts/sync-desktop-versions.mjs
+ ./scripts/prepare-gpui-dependency.sh
+
+ - name: Build the app and its tests
+ run: |
+ set -euo pipefail
+ cargo +1.95.0 build --manifest-path apps/desktop-gpui/Cargo.toml --locked --bin cap-gpui
+ cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --bin cap-gpui --no-run --message-format=json \
+ | python3 -c 'import json, sys
+ for line in sys.stdin:
+ m = json.loads(line)
+ if m.get("reason") == "compiler-artifact" and m.get("executable") and m["target"]["name"] == "cap-gpui" and m["profile"]["test"]:
+ print(m["executable"])' > "$RUNNER_TEMP/test-binary"
+ test -s "$RUNNER_TEMP/test-binary"
+
+ # X11 captures without a portal. Display and area recordings wait for
+ # the Stop shortcut to start, so the cases record another app's window.
+ # The runner has no audio device: a PulseAudio null sink plays the cue.
+ - name: Real-app Instant upload and recovery on Xvfb
+ run: |
+ set -euo pipefail
+ sudo apt-get install -y -qq xvfb x11-utils dbus pulseaudio pulseaudio-utils libasound2-plugins >/dev/null
+ # An output device for the start cue, which the start-cue case waits on.
+ pulse_dir=$(mktemp -d)
+ pulseaudio -n --daemonize=yes --exit-idle-time=-1 \
+ -L "module-native-protocol-unix socket=$pulse_dir/native auth-anonymous=1" \
+ -L "module-null-sink sink_name=cap_output" -L "module-always-sink"
+ export PULSE_SERVER="unix:$pulse_dir/native"
+ for _ in $(seq 40); do pactl info >/dev/null 2>&1 && break; sleep 0.25; done
+ pactl set-default-sink cap_output
+ Xvfb :78 -screen 0 1920x1080x24 >/dev/null 2>&1 &
+ sleep 1
+ DISPLAY=:78 xmessage -geometry 640x360+200+200 "A window to record" >/dev/null 2>&1 &
+ env -u WAYLAND_DISPLAY DISPLAY=:78 XDG_SESSION_TYPE=x11 \
+ LD_LIBRARY_PATH="$GITHUB_WORKSPACE/target/native-deps/lib" \
+ CAP_GPUI_REAL_APP_BIN="$GITHUB_WORKSPACE/apps/desktop-gpui/target/debug/cap-gpui" \
+ CAP_GPUI_REAL_APP_ARTIFACTS="$RUNNER_TEMP/real-app" \
+ CAP_GPUI_AUTO_OVERLAY=window CAP_GPUI_AUTO_WINDOW=xmessage \
+ dbus-run-session -- "$(cat "$RUNNER_TEMP/test-binary")" --ignored --test-threads=1 --nocapture \
+ real_app_instant_recording_uploads_and_verifies real_app_recovered_recording_uploads \
+ real_app_start_cue_waits_for_a_slow_server_video real_app_cancelled_start_deletes_its_video \
+ real_app_crashes_never_delete_a_recording_with_media
+
+ - name: Upload sandboxes
+ if: failure()
+ uses: actions/upload-artifact@v4
+ with:
+ name: instant-reliability-sandboxes-linux
+ path: ${{ runner.temp }}/real-app
+ retention-days: 7
diff --git a/.github/workflows/linux-desktop-reliability.yml b/.github/workflows/linux-desktop-reliability.yml
index 4e20876e3..bcf160af5 100644
--- a/.github/workflows/linux-desktop-reliability.yml
+++ b/.github/workflows/linux-desktop-reliability.yml
@@ -15,9 +15,14 @@ on:
- "crates/scap-targets/**"
- "scripts/linux-desktop-harness/**"
- "scripts/prepare-gpui-dependency.sh"
+ - "scripts/finalize-linux-appimage.mjs"
- ".github/workflows/linux-desktop-reliability.yml"
workflow_dispatch:
+permissions:
+ contents: read
+ pull-requests: read
+
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
@@ -27,6 +32,9 @@ env:
# binary under test is this commit's build.
CAP_DEB_URL: https://cdn.crabnebula.app/asset/01M5CHFS77QM2HSZ69KT784SAV
CAP_DEB_SHA256: bb03dddc5268f64d3e6eb4c11534462e5b7a8a5c1c7bb5bca5f5bdaa99d11c56
+ # The 0.6.1 AppImage, whose libraries the NixOS check lays out both ways.
+ CAP_APPIMAGE_URL: https://cdn.crabnebula.app/asset/01M5CHFYYW4JXX4M9SPZ1NF6HM
+ CAP_APPIMAGE_SHA256: dd579d76b91173a2e620034c407e63ec3ae4ae5420540de80f5fededb7519b97
jobs:
# A pull request from a fork that changes only this workflow or the harness
@@ -52,7 +60,7 @@ jobs:
echo "run=true" >> "$GITHUB_OUTPUT"; exit 0
fi
gh api "repos/$GITHUB_REPOSITORY/pulls/$PR/files" --paginate --jq '.[].filename' > "$RUNNER_TEMP/changed.txt"
- if grep -qE '^(apps/desktop-gpui/|crates/(recording|utils|scap-targets)/|scripts/prepare-gpui-dependency\.sh$)' "$RUNNER_TEMP/changed.txt"; then
+ if grep -qE '^(apps/desktop-gpui/|crates/(recording|utils|scap-targets)/|scripts/(prepare-gpui-dependency\.sh|finalize-linux-appimage\.mjs)$)' "$RUNNER_TEMP/changed.txt"; then
echo "run=true" >> "$GITHUB_OUTPUT"
else
echo "run=false" >> "$GITHUB_OUTPUT"
@@ -99,12 +107,35 @@ jobs:
- name: Build cap-gpui
run: cargo +1.95.0 build --manifest-path apps/desktop-gpui/Cargo.toml --locked --bin cap-gpui
+ # niri 26.04 shares screencasts only as DMA-BUFs with a mandatory
+ # modifier (GitHub #2941). niri itself refuses software rendering, so a
+ # producer that negotiates and shares frames the way it does stands in.
+ - name: Capture from a niri-style DMA-BUF screencast
+ env:
+ LD_LIBRARY_PATH: ${{ github.workspace }}/target/native-deps/lib
+ run: |
+ sudo apt-get install -y -qq pipewire wireplumber libpipewire-0.3-dev dbus >/dev/null
+ RUSTUP_TOOLCHAIN=1.95.0 bash scripts/linux-desktop-harness/niri-screencast/run.sh
+
- name: Confirmation dialog without zenity (X11 input)
run: |
sudo apt-get install -y -qq xdotool xvfb >/dev/null
bash scripts/linux-desktop-harness/dialog-x11.sh \
apps/desktop-gpui/target/debug/cap-gpui target/native-deps/lib
+ - name: X11 display, area and window recordings, audio and a screenshot
+ run: |
+ sudo apt-get install -y -qq x11-utils pulseaudio pulseaudio-utils libasound2-plugins ffmpeg >/dev/null
+ bash scripts/linux-desktop-harness/x11-capture.sh \
+ apps/desktop-gpui/target/debug/cap-gpui target/native-deps/lib "$RUNNER_TEMP/out/x11-capture"
+
+ # A systemd user environment keeps WAYLAND_DISPLAY after a Wayland
+ # session ends.
+ - name: X11 launch with a stale WAYLAND_DISPLAY
+ run: |
+ bash scripts/linux-desktop-harness/x11-stale-wayland.sh \
+ apps/desktop-gpui/target/debug/cap-gpui target/native-deps/lib "$RUNNER_TEMP/out/x11-stale-wayland"
+
- name: Fetch the Cap package for desktop integration
run: |
curl --fail --location --retry 3 -o "$RUNNER_TEMP/cap.deb" "$CAP_DEB_URL"
@@ -124,7 +155,53 @@ jobs:
DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
run: |
sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
- "$RUNNER_TEMP/out/gnome-tray" LAUNCH=terminal TRAY_CLICK=pointer GNOME_UNSAFE=1
+ "$RUNNER_TEMP/out/gnome-tray" LAUNCH=terminal TRAY_CLICK=pointer GNOME_UNSAFE=1 \
+ CAP_GPUI_PORTAL_APP_ID=0
+
+ # A terminal launch has no launcher unit for the portals to name Cap
+ # after; Cap registers its own app id so GlobalShortcuts still works.
+ - name: GNOME 50 shortcut from a terminal launch
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-terminal" LAUNCH=terminal APPROVE_SHORTCUT_AFTER=6
+ cat "$RUNNER_TEMP/out/gnome-terminal/result.txt"
+ grep -qx attempt1_control=shortcut "$RUNNER_TEMP/out/gnome-terminal/result.txt"
+ grep -qx app_id_required=0 "$RUNNER_TEMP/out/gnome-terminal/result.txt"
+
+ # Every shell launch names itself to the portals, including the ones
+ # that have just written the desktop entry the portal looks the id up in.
+ - name: GNOME 50 shell launches register Cap's app id
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ SCENARIO: register
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-register" ROUNDS=4
+ cat "$RUNNER_TEMP/out/gnome-register/result.txt"
+ for line in fresh_registered=4 again_registered=4 entry_written=yes panics=0; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-register/result.txt"
+ done
+
+ # Fedora's GNOME without the AppIndicator extension, launched where the
+ # portal grants no shortcut: no tray and no Stop shortcut. Cap records
+ # with its controls visible instead of refusing to start.
+ - name: GNOME 50 without a tray or a shortcut records with visible controls
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-no-control" LAUNCH=terminal GNOME_TRAY=0 CAP_GPUI_PORTAL_APP_ID=0 \
+ GNOME_UNSAFE=1
+ cat "$RUNNER_TEMP/out/gnome-no-control/result.txt"
+ for line in attempt1_control=visible visible_controls_shown=yes attempt1_finished=yes blocked=0 \
+ restoration_unconfirmed=0 panics=0; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-no-control/result.txt"
+ done
- name: GNOME 50 settings window closes from its own button
env:
@@ -148,10 +225,105 @@ jobs:
cat "$RUNNER_TEMP/out/gnome-reopen/result.txt"
grep -qx reopen_forwarded=yes "$RUNNER_TEMP/out/gnome-reopen/result.txt"
+ - name: GNOME 50 relaunch brings Cap to the front over its settings
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ SCENARIO: raise
+ run: |
+ for window in main onboarding; do
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-raise-$window" GNOME_UNSAFE=1 RAISE_WINDOW=$window
+ cat "$RUNNER_TEMP/out/gnome-raise-$window/result.txt"
+ grep -qx main_raised=yes "$RUNNER_TEMP/out/gnome-raise-$window/result.txt"
+ done
+
+ - name: GNOME 50 editor background image from the portal file chooser
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ SCENARIO: pick-background
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-pick-background" GNOME_UNSAFE=1 LAUNCH=dock APPROVE_SHORTCUT_AFTER=6
+ cat "$RUNNER_TEMP/out/gnome-pick-background/result.txt"
+ for line in recorded=yes editor_opened=yes chooser_opened=yes background_chosen=yes cap_alive=yes; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-pick-background/result.txt"
+ done
+
+ # GNOME's default wallpaper is JPEG XL.
+ - name: GNOME 50 editor background from the desktop wallpaper
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ SCENARIO: wallpaper
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-wallpaper" LAUNCH=dock APPROVE_SHORTCUT_AFTER=6
+ cat "$RUNNER_TEMP/out/gnome-wallpaper/result.txt"
+ for line in recorded=yes expected_exists=yes editor_opened=yes wallpaper_found=yes wallpaper_imported=yes \
+ cap_alive=yes; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-wallpaper/result.txt"
+ done
+
+ # GNOME asks once whether Cap may take screenshots, and only while Cap
+ # has focus.
+ - name: GNOME 50 screenshot, allowed when the portal asks
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ SCENARIO: screenshot
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-screenshot" LAUNCH=dock GNOME_UNSAFE=1
+ cat "$RUNNER_TEMP/out/gnome-screenshot/result.txt" "$RUNNER_TEMP/out/gnome-screenshot/actions.log"
+ for line in screenshot_taken=yes screenshot_size=yes capture_failed=0 cap_alive=yes panics=0; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-screenshot/result.txt"
+ done
+
+ - name: GNOME 50 screenshot from a terminal launch (Cap names itself so.cap.Cap)
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ SCENARIO: screenshot
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-screenshot-terminal" LAUNCH=terminal PRELAUNCH=1 GNOME_UNSAFE=1
+ cat "$RUNNER_TEMP/out/gnome-screenshot-terminal/result.txt" "$RUNNER_TEMP/out/gnome-screenshot-terminal/actions.log"
+ for line in screenshot_taken=yes screenshot_size=yes capture_failed=0 cap_alive=yes panics=0; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-screenshot-terminal/result.txt"
+ done
+
+ - name: GNOME 50 area recording (portal stream cropped to the area)
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-area" LAUNCH=dock APPROVE_SHORTCUT_AFTER=6 AREA=200,150,800,450
+ cat "$RUNNER_TEMP/out/gnome-area/result.txt"
+ for line in attempt1_finished=yes area_video=yes panics=0; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-area/result.txt"
+ done
+
+ - name: GNOME 50 editor export to MP4 and GIF
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ SCENARIO: export
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-export" LAUNCH=dock APPROVE_SHORTCUT_AFTER=6
+ cat "$RUNNER_TEMP/out/gnome-export/result.txt"
+ for line in recorded=yes mp4_exported=yes mp4_has_video=yes mp4_cap_alive=yes gif_exported=yes \
+ gif_has_video=yes gif_cap_alive=yes; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-export/result.txt"
+ done
+
- name: Check results
run: |
bash scripts/linux-desktop-harness/check-results.sh \
- "$RUNNER_TEMP/out/gnome" "$RUNNER_TEMP/out/gnome-tray"
+ "$RUNNER_TEMP/out/gnome" "$RUNNER_TEMP/out/gnome-tray" "$RUNNER_TEMP/out/gnome-terminal"
- name: Package the build for the Hyprland job
run: |
@@ -204,8 +376,78 @@ jobs:
bash scripts/linux-desktop-harness/run-desktop.sh hyprland "$RUNNER_TEMP/build/cap-gpui" \
"$RUNNER_TEMP/out/hyprland" LAUNCH=dock
+ # A GPU without a Vulkan driver renders through wgpu's GL backend, whose
+ # surfaces have no Mailbox present mode. Hiding the Vulkan drivers puts
+ # Cap there; showing its hidden windows again must not panic.
+ - name: Hyprland on OpenGL (no Vulkan driver)
+ run: |
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ bash scripts/linux-desktop-harness/run-desktop.sh hyprland "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/hyprland-gl" LAUNCH=dock VK_ICD_FILENAMES=/nonexistent.json
+ grep -q 'Selected GPU adapter: .*(Gl)' "$RUNNER_TEMP/out/hyprland-gl/cap.log"
+
+ - name: Hyprland screenshot
+ env:
+ SCENARIO: screenshot
+ run: |
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ bash scripts/linux-desktop-harness/run-desktop.sh hyprland "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/hyprland-screenshot" LAUNCH=dock
+ cat "$RUNNER_TEMP/out/hyprland-screenshot/result.txt"
+ for line in screenshot_taken=yes screenshot_size=yes capture_failed=0 cap_alive=yes panics=0; do
+ grep -qx "$line" "$RUNNER_TEMP/out/hyprland-screenshot/result.txt"
+ done
+
+ # The hosted runner can build v4l2loopback; Blacksmith's kernel cannot.
+ - name: Install Linux desktop dependencies
+ uses: ./.github/actions/install-desktop-deps
+
+ - name: X11 recording with a camera
+ run: |
+ sudo apt-get install -y -qq "linux-headers-$(uname -r)" v4l2loopback-dkms xvfb xdotool ffmpeg dbus >/dev/null
+ chmod +x "$RUNNER_TEMP/build/cap-gpui"
+ sudo install -d -o "$(id -u)" -g "$(id -g)" "$RUNNER_TEMP/out/x11-camera"
+ bash scripts/linux-desktop-harness/x11-camera.sh "$RUNNER_TEMP/build/cap-gpui" "$RUNNER_TEMP/build/libs" \
+ "$RUNNER_TEMP/out/x11-camera"
+
+ - name: Hyprland area recording
+ run: |
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ bash scripts/linux-desktop-harness/run-desktop.sh hyprland "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/hyprland-area" LAUNCH=dock AREA=200,150,800,450
+ cat "$RUNNER_TEMP/out/hyprland-area/result.txt"
+ for line in attempt1_finished=yes area_video=yes panics=0; do
+ grep -qx "$line" "$RUNNER_TEMP/out/hyprland-area/result.txt"
+ done
+
+ - name: Hyprland clipboard (Ctrl+V into and Ctrl+C out of a text field)
+ env:
+ SCENARIO: clipboard
+ run: |
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ bash scripts/linux-desktop-harness/run-desktop.sh hyprland "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/hyprland-clipboard"
+ for key in typing_reached_field paste_reached_field copy_reached_clipboard; do
+ grep -qx "$key=yes" "$RUNNER_TEMP/out/hyprland-clipboard/result.txt"
+ done
+
+ # Omarchy draws its wallpaper with swaybg.
+ - name: Hyprland editor background from the swaybg wallpaper
+ env:
+ SCENARIO: wallpaper
+ run: |
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ bash scripts/linux-desktop-harness/run-desktop.sh hyprland "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/hyprland-wallpaper" LAUNCH=dock WALLPAPER=swaybg
+ cat "$RUNNER_TEMP/out/hyprland-wallpaper/result.txt"
+ for line in recorded=yes editor_opened=yes wallpaper_found=yes wallpaper_imported=yes cap_alive=yes; do
+ grep -qx "$line" "$RUNNER_TEMP/out/hyprland-wallpaper/result.txt"
+ done
+
- name: Check results
- run: bash scripts/linux-desktop-harness/check-results.sh "$RUNNER_TEMP/out/hyprland"
+ run: |
+ bash scripts/linux-desktop-harness/check-results.sh \
+ "$RUNNER_TEMP/out/hyprland" "$RUNNER_TEMP/out/hyprland-gl"
- name: Upload logs
if: always()
@@ -214,3 +456,118 @@ jobs:
name: linux-desktop-reliability-hyprland
path: ${{ runner.temp }}/out
if-no-files-found: ignore
+
+ appimage-nixos:
+ name: AppImage screen capture on NixOS
+ needs: scope
+ if: needs.scope.outputs.run == 'true'
+ # NixOS users run the AppImage through appimage-run. The PipeWire plugins
+ # that load into Cap there link NixOS's FFmpeg, which needs a newer libva
+ # than the one 0.6.1 bundled ahead of the host's (GitHub #2941). This
+ # connects a capture stream through the real launchers, with the AppDir as
+ # 0.6.1 shipped it and as scripts/finalize-linux-appimage.mjs lays it out.
+ runs-on: ubuntu-24.04
+ timeout-minutes: 30
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Fetch the 0.6.1 AppImage
+ run: |
+ curl --fail --location --retry 3 -o "$RUNNER_TEMP/Cap.AppImage" "$CAP_APPIMAGE_URL"
+ echo "$CAP_APPIMAGE_SHA256 $RUNNER_TEMP/Cap.AppImage" | sha256sum --check
+
+ - name: Connect a capture stream in NixOS appimage-run
+ run: |
+ sudo bash scripts/linux-desktop-harness/appimage-pipewire/run.sh \
+ "$RUNNER_TEMP/Cap.AppImage" "$RUNNER_TEMP/out/appimage-nixos"
+
+ - name: Check results
+ run: |
+ set -euo pipefail
+ result="$RUNNER_TEMP/out/appimage-nixos/result.txt"
+ cat "$result"
+ # The shipped layout is the control: it must still reproduce the
+ # failure, or the check proves nothing.
+ grep -qx "shipped_connects=no" "$result"
+ grep -qx "fixed_connects=yes" "$result"
+ # Programs Cap starts (xdg-open, file managers) no longer inherit
+ # the bundle's libraries.
+ grep -qx "shipped_children_get_bundle_libraries=yes" "$result"
+ grep -qx "fixed_children_get_bundle_libraries=no" "$result"
+
+ - name: Upload logs
+ if: always()
+ uses: actions/upload-artifact@v4
+ with:
+ name: linux-desktop-reliability-appimage-nixos
+ path: ${{ runner.temp }}/out
+ if-no-files-found: ignore
+
+ niri:
+ name: Record on niri
+ needs: desktops
+ # niri 26.04 shares screencasts only as DMA-BUFs with a mandatory
+ # modifier (GitHub #2941). It refuses software rendering, so this builds
+ # the release with only that check removed and runs it on vkms.
+ runs-on: ubuntu-24.04
+ timeout-minutes: 60
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - uses: actions/download-artifact@v4
+ with:
+ name: cap-gpui-linux
+ path: ${{ runner.temp }}/build
+
+ - name: Virtual display devices
+ run: |
+ sudo apt-get update -qq
+ sudo apt-get install -y -qq "linux-modules-extra-$(uname -r)"
+ sudo modprobe vkms
+ ls -la /dev/dri
+
+ - name: Build niri
+ run: |
+ cd scripts/linux-desktop-harness
+ sudo docker build -q -f arch.Dockerfile -t cap-harness-arch . >/dev/null
+ sudo docker build -q -f niri-software.Dockerfile -t cap-harness-arch-niri . >/dev/null
+
+ # The runner's Hyper-V display (card1, Virtual-1) is a second output; with
+ # two, the screen-share dialog waits for a pick the harness never makes.
+ - name: niri (DMA-BUF screencast, tray Stop control)
+ run: |
+ chmod +x "$RUNNER_TEMP/build/cap-gpui"
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ HARNESS_IMAGE=cap-harness-arch-niri \
+ bash scripts/linux-desktop-harness/run-desktop.sh niri "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/niri" LAUNCH=dock NIRI_RENDER_DEVICE=vkms NIRI_OUTPUTS_OFF=Virtual-1 \
+ GSK_RENDERER=cairo
+
+ - name: niri clipboard (Ctrl+V into and Ctrl+C out of a text field)
+ env:
+ SCENARIO: clipboard
+ run: |
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ HARNESS_IMAGE=cap-harness-arch-niri \
+ bash scripts/linux-desktop-harness/run-desktop.sh niri "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/niri-clipboard" NIRI_RENDER_DEVICE=vkms NIRI_OUTPUTS_OFF=Virtual-1 \
+ GSK_RENDERER=cairo
+ for key in typing_reached_field paste_reached_field copy_reached_clipboard; do
+ grep -qx "$key=yes" "$RUNNER_TEMP/out/niri-clipboard/result.txt"
+ done
+
+ - name: Check results
+ run: |
+ bash scripts/linux-desktop-harness/check-results.sh "$RUNNER_TEMP/out/niri"
+ grep -q "negotiated DMA-BUF frames" "$RUNNER_TEMP/out/niri/cap.log"
+
+ - name: Upload logs
+ if: always()
+ uses: actions/upload-artifact@v4
+ with:
+ name: linux-desktop-reliability-niri
+ path: ${{ runner.temp }}/out
+ if-no-files-found: ignore
+
diff --git a/.github/workflows/sync-tests.yml b/.github/workflows/sync-tests.yml
index 0e7cc1049..2fe441dd9 100644
--- a/.github/workflows/sync-tests.yml
+++ b/.github/workflows/sync-tests.yml
@@ -160,6 +160,7 @@ jobs:
shell: bash
run: |
cargo test --locked -p cap-audio --lib
+ cargo test --locked -p cap-media-info --lib
cargo test --locked -p cap-export --lib
cargo test --locked -p cap-editor --lib audio::tests::
cargo test --locked -p cap-editor --lib audio_output::tests::
diff --git a/Cargo.lock b/Cargo.lock
index cd41e5611..2034ad314 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1953,11 +1953,13 @@ dependencies = [
"flume",
"foreign-types-shared 0.3.1",
"futures",
+ "glow",
"hex",
"image 0.25.8",
"indexmap 2.11.4",
"inquire",
"kameo",
+ "khronos-egl",
"libc",
"libproc",
"objc",
diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock
index 8697134c9..ef41d6aa3 100644
--- a/apps/desktop-gpui/Cargo.lock
+++ b/apps/desktop-gpui/Cargo.lock
@@ -1569,6 +1569,7 @@ dependencies = [
"cosmic-text 0.14.2",
"cpal 0.15.3 (git+https://github.com/CapSoftware/cpal?rev=6013cb5f8bd3)",
"dirs 6.0.0",
+ "dunce",
"ffmpeg-next",
"flume 0.11.1",
"futures-util",
@@ -1579,6 +1580,7 @@ dependencies = [
"hound",
"image 0.25.10",
"jpeg-decoder",
+ "jxl-oxide",
"kameo",
"libc",
"lyon_tessellation",
@@ -1905,11 +1907,13 @@ dependencies = [
"flume 0.11.1",
"foreign-types-shared 0.3.1",
"futures",
+ "glow 0.16.0",
"hex",
"image 0.25.10",
"indexmap 2.14.0",
"inquire",
"kameo",
+ "khronos-egl",
"libc",
"objc",
"objc2 0.6.5",
@@ -5835,6 +5839,186 @@ dependencies = [
"serde_json",
]
+[[package]]
+name = "jxl-bitstream"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b480e752277e29eb4054f69546887a9b84656fe78c08f54ba5850ced98a378fe"
+dependencies = [
+ "tracing",
+]
+
+[[package]]
+name = "jxl-coding"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cd972bcd125e776f1eb241ac50e39f956095a1c2770c64736c968f8946bd9a3c"
+dependencies = [
+ "jxl-bitstream",
+ "tracing",
+]
+
+[[package]]
+name = "jxl-color"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f316b1358c1711755b3ee8e8cb5c4a1dad12e796233088a7a513440782de80b2"
+dependencies = [
+ "jxl-bitstream",
+ "jxl-coding",
+ "jxl-grid",
+ "jxl-image",
+ "jxl-oxide-common",
+ "jxl-threadpool",
+ "tracing",
+]
+
+[[package]]
+name = "jxl-frame"
+version = "0.13.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2d967c6fd669c7c01060b5022d8835fa82fd46b06ffc98b549f17600a097c2b3"
+dependencies = [
+ "jxl-bitstream",
+ "jxl-coding",
+ "jxl-grid",
+ "jxl-image",
+ "jxl-modular",
+ "jxl-oxide-common",
+ "jxl-threadpool",
+ "jxl-vardct",
+ "tracing",
+]
+
+[[package]]
+name = "jxl-grid"
+version = "0.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "01671307879a033bfa52e6e8784b941aca770b3f3a7d33830b455b6844f793fb"
+dependencies = [
+ "tracing",
+]
+
+[[package]]
+name = "jxl-image"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c5f752d62577c702a94dbbce4045caf08cb58639e8a4d56464b40ecf33ffe565"
+dependencies = [
+ "jxl-bitstream",
+ "jxl-grid",
+ "jxl-oxide-common",
+ "tracing",
+]
+
+[[package]]
+name = "jxl-jbr"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e35d032bcec660647828527ff42c6f5776d2fd44b8357f9f6d9ac6dc07218e46"
+dependencies = [
+ "brotli-decompressor",
+ "jxl-bitstream",
+ "jxl-frame",
+ "jxl-grid",
+ "jxl-image",
+ "jxl-modular",
+ "jxl-oxide-common",
+ "jxl-threadpool",
+ "jxl-vardct",
+ "tracing",
+]
+
+[[package]]
+name = "jxl-modular"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2a2f045b24c738dd91d482be385512b512721ae08a671bd4b27bf1c47f215235"
+dependencies = [
+ "jxl-bitstream",
+ "jxl-coding",
+ "jxl-grid",
+ "jxl-oxide-common",
+ "jxl-threadpool",
+ "tracing",
+]
+
+[[package]]
+name = "jxl-oxide"
+version = "0.12.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d36c662923f47586880211f3bc7c0d83fb3a9b410d278c7bde93450748abeef3"
+dependencies = [
+ "brotli-decompressor",
+ "bytemuck",
+ "image 0.25.10",
+ "jxl-bitstream",
+ "jxl-color",
+ "jxl-frame",
+ "jxl-grid",
+ "jxl-image",
+ "jxl-jbr",
+ "jxl-oxide-common",
+ "jxl-render",
+ "jxl-threadpool",
+ "tracing",
+]
+
+[[package]]
+name = "jxl-oxide-common"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b62394c5021b3a9e7e0dbb2d639d555d019090c9946c39f6d3b09d390db4157b"
+dependencies = [
+ "jxl-bitstream",
+]
+
+[[package]]
+name = "jxl-render"
+version = "0.12.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d34386bfdb6a19b5a30cc9beb4d475d537422c31ae8c39bb69640fcce3fcaf19"
+dependencies = [
+ "bytemuck",
+ "jxl-bitstream",
+ "jxl-coding",
+ "jxl-color",
+ "jxl-frame",
+ "jxl-grid",
+ "jxl-image",
+ "jxl-modular",
+ "jxl-oxide-common",
+ "jxl-threadpool",
+ "jxl-vardct",
+ "tracing",
+]
+
+[[package]]
+name = "jxl-threadpool"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "25f15eb830aa77a7f21148d72e153562a26bfe570139bd4922eab1908dd499d3"
+dependencies = [
+ "rayon",
+ "rayon-core",
+ "tracing",
+]
+
+[[package]]
+name = "jxl-vardct"
+version = "0.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ce72a18c6d3a47172ab6c479be2bdb56f22066b5d7092663f03b4490820b4511"
+dependencies = [
+ "jxl-bitstream",
+ "jxl-coding",
+ "jxl-grid",
+ "jxl-modular",
+ "jxl-oxide-common",
+ "jxl-threadpool",
+ "tracing",
+]
+
[[package]]
name = "kameo"
version = "0.17.2"
diff --git a/apps/desktop-gpui/Cargo.toml b/apps/desktop-gpui/Cargo.toml
index 1a62219b6..8b9c003a1 100644
--- a/apps/desktop-gpui/Cargo.toml
+++ b/apps/desktop-gpui/Cargo.toml
@@ -172,6 +172,7 @@ reqwest = { version = "0.12", default-features = false, features = [
] }
futures-util = "0.3"
dirs = "6"
+dunce = "1.0.5"
# Caption transcription (`src/transcription.rs`): the exact engine pin the
# Tauri app carries (`apps/desktop/src-tauri/Cargo.toml:75`), so a model file
# downloaded by either app loads identically in both.
@@ -226,6 +227,8 @@ windows-sys = { version = "0.59", features = [
[target.'cfg(target_os = "linux")'.dependencies]
ashpd = { version = "0.11", default-features = false, features = ["tokio"] }
+# GNOME's wallpapers are JPEG XL, which `image` cannot read.
+jxl-oxide = { version = "0.12", features = ["image"] }
wayland-client = "0.31"
[target.'cfg(all(unix, not(target_os = "macos")))'.dependencies]
diff --git a/apps/desktop-gpui/README.md b/apps/desktop-gpui/README.md
index f27c62630..6830cfa41 100644
--- a/apps/desktop-gpui/README.md
+++ b/apps/desktop-gpui/README.md
@@ -697,7 +697,7 @@ Recording-specific deviations:
| **Settings button is inert** | The bar's recording-settings popover menu is not built; the same applies to the gear button in the overlay's start cluster. |
| **No overlay blur** | The recording overlay is `bg-gray-1/80` without `backdrop-blur-xs`; this gpui rev has no per-element backdrop blur hook. The target-select overlay's liquid-glass surfaces drop their `backdrop-blur-xl` for the same reason. |
| **Escape is a focused key handler** | The Tauri app registers Escape as a *global* shortcut while the overlays are up; here it is a key handler on the overlay that holds focus (the one on the cursor's display). Escape pressed while another app is active does not dismiss. |
-| **Overlay cluster is start-only** | The device row (camera/microphone selects under the start pill), the mode dropdown behind the caret, and the "What is X Mode?" link are deferred — device pickers live in the main window. The area toolbar shows the size readout but not the aspect-ratio/reset/fill/lock controls. |
+| **Overlay cluster is start-only** | The device row (camera/microphone selects under the start pill), the mode dropdown behind the caret, and the "What is X Mode?" link are deferred — device pickers live in the main window. The area toolbar shows the size readout and an aspect-ratio control, but not the reset/fill/lock controls. |
| **Camera-only keeps the bubble** | The TSX inlines a camera preview into the camera-only overlay; ours keeps the separate camera preview window it already has. |
| **Camera id is DeviceID-only** | The Tauri app persists `ModelID` when a camera advertises one, so the same camera survives re-plugging into a different port. |
| **Defaults are the builder's** | `desktop_recording_defaults` (studio quality, fps caps, custom cursor) is not applied yet — it reads the Tauri settings store. |
diff --git a/apps/desktop-gpui/locales/de.toml b/apps/desktop-gpui/locales/de.toml
index ea20dfc88..7efe52697 100644
--- a/apps/desktop-gpui/locales/de.toml
+++ b/apps/desktop-gpui/locales/de.toml
@@ -111,7 +111,10 @@ system_default = "Systemstandard ({language})"
title = "Sprache"
[settings.general.appearance]
+dark = "Dunkel"
description = "Passen Sie Cap an das Design Ihres Systems an oder wählen Sie ein festes Erscheinungsbild."
+light = "Hell"
+system = "System"
title = "Erscheinungsbild"
[settings.general.app]
diff --git a/apps/desktop-gpui/locales/en.toml b/apps/desktop-gpui/locales/en.toml
index 22bb75c55..d6cb83384 100644
--- a/apps/desktop-gpui/locales/en.toml
+++ b/apps/desktop-gpui/locales/en.toml
@@ -113,7 +113,10 @@ system_default = { text = "System default ({language})", context = "{language} i
title = "Language"
[settings.general.appearance]
+dark = "Dark"
description = "Match Cap to your system theme or pick a fixed look."
+light = "Light"
+system = "System"
title = "Appearance"
[settings.general.app]
diff --git a/apps/desktop-gpui/locales/es.toml b/apps/desktop-gpui/locales/es.toml
index 0f8676adf..8d89271b9 100644
--- a/apps/desktop-gpui/locales/es.toml
+++ b/apps/desktop-gpui/locales/es.toml
@@ -111,7 +111,10 @@ system_default = "Predeterminado del sistema ({language})"
title = "Idioma"
[settings.general.appearance]
+dark = "Oscuro"
description = "Adapta Cap al tema de tu sistema o elige un aspecto fijo."
+light = "Claro"
+system = "Sistema"
title = "Apariencia"
[settings.general.app]
diff --git a/apps/desktop-gpui/locales/fr.toml b/apps/desktop-gpui/locales/fr.toml
index bd998a09e..84c3fed94 100644
--- a/apps/desktop-gpui/locales/fr.toml
+++ b/apps/desktop-gpui/locales/fr.toml
@@ -111,7 +111,10 @@ system_default = "Langue du système ({language})"
title = "Langue"
[settings.general.appearance]
+dark = "Sombre"
description = "Adaptez Cap au thème de votre système ou choisissez une apparence fixe."
+light = "Clair"
+system = "Système"
title = "Apparence"
[settings.general.app]
diff --git a/apps/desktop-gpui/locales/hi.toml b/apps/desktop-gpui/locales/hi.toml
index 6ac18a8e1..894c461c4 100644
--- a/apps/desktop-gpui/locales/hi.toml
+++ b/apps/desktop-gpui/locales/hi.toml
@@ -111,7 +111,10 @@ system_default = "सिस्टम डिफ़ॉल्ट ({language})"
title = "भाषा"
[settings.general.appearance]
+dark = "डार्क"
description = "Cap को अपने सिस्टम की थीम के अनुसार रखें या एक तय रूप चुनें।"
+light = "लाइट"
+system = "सिस्टम"
title = "रूप"
[settings.general.app]
diff --git a/apps/desktop-gpui/locales/ja.toml b/apps/desktop-gpui/locales/ja.toml
index 3dbbbe8b1..a79e312d4 100644
--- a/apps/desktop-gpui/locales/ja.toml
+++ b/apps/desktop-gpui/locales/ja.toml
@@ -111,7 +111,10 @@ system_default = "システムのデフォルト({language})"
title = "言語"
[settings.general.appearance]
+dark = "ダーク"
description = "Capの外観をシステムのテーマに合わせるか、固定の外観を選択します。"
+light = "ライト"
+system = "システム"
title = "外観"
[settings.general.app]
diff --git a/apps/desktop-gpui/locales/zh-CN.toml b/apps/desktop-gpui/locales/zh-CN.toml
index b3b0e0e5b..8891d3c7e 100644
--- a/apps/desktop-gpui/locales/zh-CN.toml
+++ b/apps/desktop-gpui/locales/zh-CN.toml
@@ -111,7 +111,10 @@ system_default = "跟随系统({language})"
title = "语言"
[settings.general.appearance]
+dark = "深色"
description = "让 Cap 跟随系统主题,或选择固定外观。"
+light = "浅色"
+system = "跟随系统"
title = "外观"
[settings.general.app]
diff --git a/apps/desktop-gpui/patches/zed-closed-window-events.patch b/apps/desktop-gpui/patches/zed-closed-window-events.patch
new file mode 100644
index 000000000..4d1903d3f
--- /dev/null
+++ b/apps/desktop-gpui/patches/zed-closed-window-events.patch
@@ -0,0 +1,188 @@
+diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
+index c543c6b..9b3e51d 100644
+--- a/crates/gpui/src/window.rs
++++ b/crates/gpui/src/window.rs
+@@ -75,6 +75,24 @@ use crate::util::{
+ };
+ pub use prompts::*;
+
++/// A platform can still report focus, hover, input or a frame request for a
++/// window gpui has already removed while it closes. There is nothing left to
++/// update, so that one failure is not an error worth logging.
++trait LogUnlessClosed<T> {
++ fn log_unless_closed(self) -> Option<T>;
++}
++
++impl<T> LogUnlessClosed<T> for Result<T> {
++ #[track_caller]
++ fn log_unless_closed(self) -> Option<T> {
++ match self {
++ Ok(value) => Some(value),
++ Err(error) if error.to_string() == "window not found" => None,
++ Err(error) => Err::<T, _>(error).log_err(),
++ }
++ }
++}
++
+ /// Default window size used when no explicit size is provided.
+ pub const DEFAULT_WINDOW_SIZE: Size<Pixels> = size(px(1536.), px(1095.));
+
+@@ -1624,7 +1642,7 @@ impl Window {
+ // `complete_frame`) or the compositor won't send another callback.
+ handle
+ .update(&mut cx, |_, window, _| window.complete_frame())
+- .log_err();
++ .log_unless_closed();
+ frame_pacer.frame_finished(true, true, now);
+ return;
+ }
+@@ -1642,7 +1660,7 @@ impl Window {
+ callback(window, cx);
+ }
+ })
+- .log_err();
++ .log_unless_closed();
+ }
+
+ // Keep presenting if input was recently arriving at a high rate (>= 60fps).
+@@ -1666,20 +1684,20 @@ impl Window {
+ window.present();
+ arena_clear_needed.clear();
+ })
+- .log_err();
++ .log_unless_closed();
+ })
+ } else if needs_present {
+ busy = true;
+ handle
+ .update(&mut cx, |_, window, _| window.present())
+- .log_err();
++ .log_unless_closed();
+ }
+
+ handle
+ .update(&mut cx, |_, window, _| {
+ window.complete_frame();
+ })
+- .log_err();
++ .log_unless_closed();
+
+ let pending = invalidator.is_dirty() || !pending_frame_callbacks.borrow().is_empty();
+ frame_pacer.frame_finished(busy, pending, now);
+@@ -1690,7 +1708,7 @@ impl Window {
+ move |_, _| {
+ handle
+ .update(&mut cx, |_, window, cx| window.bounds_changed(cx))
+- .log_err();
++ .log_unless_closed();
+ }
+ }));
+ platform_window.on_moved(Box::new({
+@@ -1698,7 +1716,7 @@ impl Window {
+ move || {
+ handle
+ .update(&mut cx, |_, window, cx| window.bounds_changed(cx))
+- .log_err();
++ .log_unless_closed();
+ }
+ }));
+ platform_window.on_appearance_changed(Box::new({
+@@ -1706,7 +1724,7 @@ impl Window {
+ move || {
+ handle
+ .update(&mut cx, |_, window, cx| window.appearance_changed(cx))
+- .log_err();
++ .log_unless_closed();
+ }
+ }));
+ platform_window.on_button_layout_changed(Box::new({
+@@ -1714,7 +1732,7 @@ impl Window {
+ move || {
+ handle
+ .update(&mut cx, |_, window, cx| window.button_layout_changed(cx))
+- .log_err();
++ .log_unless_closed();
+ }
+ }));
+ platform_window.on_active_status_change(Box::new({
+@@ -1735,7 +1753,7 @@ impl Window {
+
+ SystemWindowTabController::update_last_active(cx, window.handle.id);
+ })
+- .log_err();
++ .log_unless_closed();
+ }
+ }));
+ platform_window.on_hover_status_change(Box::new({
+@@ -1746,7 +1764,7 @@ impl Window {
+ window.hovered.set(active);
+ window.refresh();
+ })
+- .log_err();
++ .log_unless_closed();
+ }
+ }));
+ platform_window.on_input({
+@@ -1754,7 +1772,7 @@ impl Window {
+ Box::new(move |event| {
+ handle
+ .update(&mut cx, |_, window, cx| window.dispatch_event(event, cx))
+- .log_err()
++ .log_unless_closed()
+ .unwrap_or(DispatchEventResult::default())
+ })
+ });
+@@ -1770,7 +1788,7 @@ impl Window {
+ }
+ None
+ })
+- .log_err()
++ .log_unless_closed()
+ .unwrap_or(None)
+ })
+ });
+@@ -1781,7 +1799,7 @@ impl Window {
+ .update(&mut cx, |_, _window, cx| {
+ SystemWindowTabController::move_tab_to_new_window(cx, handle.window_id());
+ })
+- .log_err();
++ .log_unless_closed();
+ })
+ });
+ platform_window.on_merge_all_windows({
+@@ -1791,7 +1809,7 @@ impl Window {
+ .update(&mut cx, |_, _window, cx| {
+ SystemWindowTabController::merge_all_windows(cx, handle.window_id());
+ })
+- .log_err();
++ .log_unless_closed();
+ })
+ });
+ platform_window.on_select_next_tab({
+@@ -1801,7 +1819,7 @@ impl Window {
+ .update(&mut cx, |_, _window, cx| {
+ SystemWindowTabController::select_next_tab(cx, handle.window_id());
+ })
+- .log_err();
++ .log_unless_closed();
+ })
+ });
+ platform_window.on_select_previous_tab({
+@@ -1811,7 +1829,7 @@ impl Window {
+ .update(&mut cx, |_, _window, cx| {
+ SystemWindowTabController::select_previous_tab(cx, handle.window_id())
+ })
+- .log_err();
++ .log_unless_closed();
+ })
+ });
+ platform_window.on_toggle_tab_bar({
+@@ -1822,7 +1840,7 @@ impl Window {
+ let tab_bar_visible = window.platform_window.tab_bar_visible();
+ SystemWindowTabController::set_visible(cx, tab_bar_visible);
+ })
+- .log_err();
++ .log_unless_closed();
+ })
+ });
+
diff --git a/apps/desktop-gpui/patches/zed-linux.patch b/apps/desktop-gpui/patches/zed-linux.patch
index 8229cb962..d2fd75637 100644
--- a/apps/desktop-gpui/patches/zed-linux.patch
+++ b/apps/desktop-gpui/patches/zed-linux.patch
@@ -1000,7 +1000,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
// Limit interactive resizes to once per vblank
if configure.resizing && state.resize_throttle {
state.surface_state.ack_configure(serial);
-@@ -945,6 +1313,43 @@
+@@ -945,6 +1313,47 @@
window_geometry.size.height,
);
@@ -1028,7 +1028,11 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
+ let config = WgpuSurfaceConfig {
+ size: state.renderer.viewport_size(),
+ transparent: state.is_transparent(),
-+ preferred_present_mode: Some(wgpu::PresentMode::Mailbox),
++ // Keep the present mode chosen from this surface's
++ // capabilities when the window was created: GL
++ // surfaces have no Mailbox, and a configure they
++ // refuse leaves the next frame to panic.
++ preferred_present_mode: None,
+ };
+ state.renderer.replace_surface(&raw, config, &gpu.instance)
+ })
@@ -1044,7 +1048,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
let request_frame_callback = !state.acknowledged_first_configure;
if request_frame_callback {
state.acknowledged_first_configure = true;
-@@ -958,7 +1363,11 @@
+@@ -958,7 +1367,11 @@
if let zxdg_toplevel_decoration_v1::Event::Configure { mode } = event {
match mode {
WEnum::Value(zxdg_toplevel_decoration_v1::Mode::ServerSide) => {
@@ -1057,7 +1061,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
let callback = self.callbacks.borrow_mut().appearance_changed.take();
if let Some(mut fun) = callback {
fun();
-@@ -966,7 +1375,11 @@
+@@ -966,7 +1379,11 @@
}
}
WEnum::Value(zxdg_toplevel_decoration_v1::Mode::ClientSide) => {
@@ -1070,7 +1074,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
// Update background to be transparent
let callback = self.callbacks.borrow_mut().appearance_changed.take();
if let Some(mut fun) = callback {
-@@ -1271,6 +1684,7 @@
+@@ -1271,6 +1688,7 @@
}
if let Some(scale) = scale {
state.scale = scale;
@@ -1078,7 +1082,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
}
let device_bounds = state.bounds.to_device_pixels(state.scale);
state.renderer.update_drawable_size(device_bounds.size);
-@@ -1428,6 +1842,10 @@
+@@ -1428,6 +1846,10 @@
}
impl PlatformWindow for WaylandWindow {
@@ -1089,7 +1093,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
fn bounds(&self) -> Bounds<Pixels> {
self.borrow().bounds
}
-@@ -1466,7 +1884,7 @@
+@@ -1466,7 +1888,7 @@
}
fn resize(&mut self, size: Size<Pixels>) {
@@ -1098,7 +1102,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
let state_ptr = self.0.clone();
// A popup's placement is the compositor's, so a resize re-runs the positioner and the
-@@ -1486,6 +1904,11 @@
+@@ -1486,6 +1908,11 @@
return;
}
@@ -1110,7 +1114,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
// Keep window geometry consistent with configure handling. On Wayland, window geometry is
// surface-local: resizing should not attempt to translate the window; the compositor
// controls placement. We also account for client-side decoration insets and tiling.
-@@ -1566,6 +1989,20 @@
+@@ -1566,6 +1993,20 @@
_answers: &[PromptButton],
) -> Option<Receiver<usize>> {
None
@@ -1131,7 +1135,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
}
fn activate(&self) {
-@@ -1596,7 +2033,9 @@
+@@ -1596,7 +2037,9 @@
}
fn set_title(&mut self, title: &str) {
@@ -1142,7 +1146,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
toplevel.set_title(title.to_string());
}
}
-@@ -1650,7 +2089,7 @@
+@@ -1650,7 +2093,7 @@
let state = self.borrow();
if let Some(toplevel) = state.surface_state.toplevel() {
if !state.fullscreen {
@@ -1151,7 +1155,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
} else {
toplevel.unset_fullscreen();
}
-@@ -1706,6 +2145,13 @@
+@@ -1706,6 +2149,13 @@
fn draw(&self, scene: &Scene) {
let mut state = self.borrow_mut();
@@ -1165,7 +1169,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
if state.renderer.device_lost() {
let raw_window = RawWindow {
-@@ -1730,6 +2176,12 @@
+@@ -1730,6 +2180,12 @@
}
state.renderer_presented = state.renderer.draw(scene);
@@ -1178,7 +1182,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
if state.renderer.needs_redraw() {
state.force_render_after_recovery = true;
-@@ -1738,6 +2190,13 @@
+@@ -1738,6 +2194,13 @@
fn completed_frame(&self) {
let mut state = self.borrow_mut();
@@ -1192,7 +1196,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
// Work around a bug in old versions of wlroots where committing without a buffer attached
// can cause invalid synchronization that leads to graphical corruption.
-@@ -1776,6 +2235,9 @@
+@@ -1776,6 +2239,9 @@
fn start_window_resize(&self, edge: gpui::ResizeEdge) {
let state = self.borrow();
@@ -1202,7 +1206,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
if let Some(toplevel) = state.surface_state.toplevel() {
toplevel.resize(
&state.globals.seat,
-@@ -1835,7 +2297,13 @@
+@@ -1835,7 +2301,13 @@
// Commit so the new input region applies immediately. Otherwise it
// waits for the next frame, which could be the very click we want to
// allow passing through.
@@ -1217,7 +1221,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
}
fn window_decorations(&self) -> Decorations {
-@@ -1854,6 +2322,7 @@
+@@ -1854,6 +2326,7 @@
Some(decoration) => {
decoration.set_mode(decorations.to_xdg());
state.decorations = decorations;
@@ -1225,7 +1229,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
update_window(state);
}
None => {
-@@ -1863,6 +2332,7 @@
+@@ -1863,6 +2336,7 @@
);
}
state.decorations = WindowDecorations::Client;
@@ -1233,7 +1237,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
update_window(state);
}
}
-@@ -1876,6 +2346,7 @@
+@@ -1876,6 +2350,7 @@
let mut state = self.borrow_mut();
if Some(inset) != state.client_inset {
state.client_inset = Some(inset);
@@ -1241,7 +1245,7 @@ diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/s
update_window(state);
}
}
-@@ -2082,3 +2553,337 @@
+@@ -2082,3 +2557,337 @@
bounds
}
diff --git a/apps/desktop-gpui/patches/zed-wayland-activation.patch b/apps/desktop-gpui/patches/zed-wayland-activation.patch
new file mode 100644
index 000000000..3ad72f9fb
--- /dev/null
+++ b/apps/desktop-gpui/patches/zed-wayland-activation.patch
@@ -0,0 +1,57 @@
+diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs
+index 7dd2d38..b597e4d 100644
+--- a/crates/gpui/src/platform.rs
++++ b/crates/gpui/src/platform.rs
+@@ -844,6 +844,14 @@ pub trait PlatformWindow: HasWindowHandle + HasDisplayHandle {
+ anyhow::bail!("Retained window visibility is unsupported by this backend")
+ }
+ fn activate(&self);
++ /// Activates the window with a token the compositor issued to another
++ /// client, such as the `XDG_ACTIVATION_TOKEN` a launcher gave a second
++ /// instance of the app. Focus stealing prevention honours it where a
++ /// token the window requests for itself is refused.
++ fn activate_with_token(&self, token: String) {
++ let _ = token;
++ self.activate();
++ }
+ /// Requests that the operating system draw attention to this window.
+ fn request_attention(&self) {}
+ fn is_active(&self) -> bool;
+diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
+index 75c3539..c543c6b 100644
+--- a/crates/gpui/src/window.rs
++++ b/crates/gpui/src/window.rs
+@@ -5810,6 +5810,12 @@ impl Window {
+ self.platform_window.activate();
+ }
+
++ /// Activates this window with a token the compositor issued to another
++ /// client. See [`PlatformWindow::activate_with_token`].
++ pub fn activate_window_with_token(&self, token: String) {
++ self.platform_window.activate_with_token(token);
++ }
++
+ /// Requests that the operating system draw attention to this window.
+ pub fn request_attention(&self) {
+ self.platform_window.request_attention();
+diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/src/linux/wayland/window.rs
+index 5d8e991..e98c640 100644
+--- a/crates/gpui_linux/src/linux/wayland/window.rs
++++ b/crates/gpui_linux/src/linux/wayland/window.rs
+@@ -2134,6 +2134,16 @@ impl PlatformWindow for WaylandWindow {
+ }
+ }
+
++ fn activate_with_token(&self, token: String) {
++ let state = self.borrow();
++ if let Some(activation) = &state.globals.activation {
++ activation.activate(token, &state.surface);
++ return;
++ }
++ drop(state);
++ self.activate();
++ }
++
+ fn request_attention(&self) {}
+
+ fn is_active(&self) -> bool {
diff --git a/apps/desktop-gpui/patches/zed-wayland-visibility.patch b/apps/desktop-gpui/patches/zed-wayland-visibility.patch
index f25ceefcf..fbe7a8a8c 100644
--- a/apps/desktop-gpui/patches/zed-wayland-visibility.patch
+++ b/apps/desktop-gpui/patches/zed-wayland-visibility.patch
@@ -209,7 +209,7 @@
+ }
+ _ => return,
+ }
-+ log::warn!("no configure after remapping a window; replacing its toplevel role");
++ log::info!("no configure after remapping a window; replacing its toplevel role");
+ let xdg_surface =
+ state
+ .globals
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index fbacd95d0..6ed179ab2 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -89,11 +89,13 @@ fn platform_display_for_capture(
cx: &App,
) -> Option<std::rc::Rc<dyn gpui::PlatformDisplay>> {
#[cfg(target_os = "linux")]
- if let Some(uuid) = target.raw_handle().wayland_uuid() {
- return cx
+ if let Some(uuid) = target.raw_handle().wayland_uuid()
+ && let Some(display) = cx
.displays()
.into_iter()
- .find(|display| display.uuid().is_ok_and(|candidate| candidate == uuid));
+ .find(|display| display.uuid().is_ok_and(|candidate| candidate == uuid))
+ {
+ return Some(display);
}
#[cfg(target_os = "windows")]
if let Ok(id) = target.id().to_string().parse::<u64>()
@@ -101,17 +103,45 @@ fn platform_display_for_capture(
{
return Some(display);
}
- let bounds = capture_display_bounds(target)?;
- let mut matching = cx
- .displays()
- .into_iter()
- .filter(|display| display.bounds().contains(&bounds.center()));
- let display = matching.next()?;
+ let displays = cx.displays();
+ // A named output that matched nothing is not matched by position either.
#[cfg(target_os = "linux")]
- if uses_wayland() && matching.next().is_some() {
- return None;
+ let named = target.raw_handle().wayland_uuid().is_some();
+ #[cfg(not(target_os = "linux"))]
+ let named = false;
+ let bounds = capture_display_bounds(target).filter(|_| !named);
+ let mut matching = displays
+ .iter()
+ .filter(|display| bounds.is_some_and(|bounds| display.bounds().contains(&bounds.center())));
+ let found = matching.next().cloned();
+ #[cfg(target_os = "linux")]
+ let found = found.filter(|_| !(uses_wayland() && matching.next().is_some()));
+ #[cfg(target_os = "linux")]
+ if found.is_none() && uses_wayland() {
+ // One output on each side cannot be the wrong one, whatever its name
+ // or position (a compositor may report neither the same way to both).
+ if displays.len() == 1 && scap_targets::Display::list().len() == 1 {
+ return displays.into_iter().next();
+ }
+ tracing::warn!(
+ uuid = ?target.raw_handle().wayland_uuid(),
+ ?bounds,
+ candidates = ?displays
+ .iter()
+ .map(|display| {
+ (
+ display
+ .uuid()
+ .map(|uuid| uuid.to_string())
+ .unwrap_or_else(|error| error.to_string()),
+ display.bounds(),
+ )
+ })
+ .collect::<Vec<_>>(),
+ "no Wayland output matches the capture display"
+ );
}
- Some(display)
+ found
}
fn gpui_display_id(target: Option<&scap_targets::Display>, cx: &App) -> Option<gpui::DisplayId> {
@@ -279,6 +309,9 @@ struct CleanCaptureUi {
retained_windows: Vec<(gpui::AnyWindowHandle, bool)>,
restoring: bool,
restoration_error: Option<String>,
+ /// A start that had no Stop control to hide behind: it runs with Cap's
+ /// controls visible once the windows this lease hid are back.
+ visible_fallback: Option<StartConfig>,
}
#[cfg(target_os = "linux")]
@@ -1202,7 +1235,7 @@ pub fn open_settings(page: Page, cx: &mut App) {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// A real titlebar this time, transparent with the buttons moved
@@ -1383,7 +1416,7 @@ pub fn open_onboarding(cx: &mut App) {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
titlebar: None,
@@ -1536,7 +1569,7 @@ pub fn open_mode_select(cx: &mut App) -> bool {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// `traffic_lights_position` has no ModeSelect arm, so it takes the
@@ -1658,7 +1691,7 @@ pub fn open_teleprompter(cx: &mut App) {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// `decorations: true`, `titleBarStyle: "overlay"`, `hiddenTitle:
@@ -2489,7 +2522,7 @@ fn open_overlay(
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
// The origin is corrected right after opening -- gpui's own
// window-origin math cannot express "cover this display" (see
// `platform::set_window_frame_cg`). The size is honoured, and it is
@@ -2844,7 +2877,13 @@ fn append_own_excluded_window_ids(
continue;
}
let Some(number) = window.number else {
- tracing::warn!(title, "excluded Cap window has no native window id");
+ // Wayland gives clients no window ids, so every Cap window lands
+ // here on every Linux Wayland recording.
+ if cfg!(target_os = "linux") {
+ tracing::debug!(title, "excluded Cap window has no native window id");
+ } else {
+ tracing::warn!(title, "excluded Cap window has no native window id");
+ }
continue;
};
let Ok(id) = number.to_string().parse::<scap_targets::WindowId>() else {
@@ -2937,6 +2976,18 @@ pub fn restore_content_protection(cx: &mut App) {
#[cfg(target_os = "linux")]
pub(crate) const APP_ID: &str = "Cap";
+/// The app id Cap's windows carry. GNOME Shell asks the portals' questions
+/// (screenshot access) only for the focused app, matched as the window's app
+/// id against the id Cap gave the portals, so a launch that named itself
+/// `so.cap.Cap` there names its windows the same.
+#[cfg(target_os = "linux")]
+pub(crate) fn app_id() -> &'static str {
+ if crate::linux_portal_id::windows_use_portal_app_id() {
+ return crate::linux_portal_id::PORTAL_APP_ID;
+ }
+ APP_ID
+}
+
#[cfg(target_os = "linux")]
fn clean_capture_supported(
mode: RecordingMode,
@@ -3211,7 +3262,12 @@ pub fn handle_owned_clean_capture_shortcut(
}
#[cfg(target_os = "linux")]
-pub fn clean_capture_stop_unavailable(generation: u64, error: String, cx: &mut App) {
+pub fn clean_capture_stop_unavailable(
+ generation: u64,
+ error: String,
+ stop_was_ready: bool,
+ cx: &mut App,
+) {
if !clean_capture_generation_matches(generation, cx) {
return;
}
@@ -3225,6 +3281,29 @@ pub fn clean_capture_stop_unavailable(generation: u64, error: String, cx: &mut A
}
let session = RecordingSession::global(cx);
if session.read(cx).phase == Phase::Idle {
+ if let Some(config) = visible_fallback_start(&error, stop_was_ready, cx) {
+ // Without a tray or a granted shortcut, nothing could stop a
+ // recording that hides Cap. Record with the controls on screen
+ // instead of not at all.
+ tracing::warn!(%error, "no recording Stop control is available; recording with Cap's controls visible");
+ let wayland = cx
+ .global::<AppWindows>()
+ .clean_capture
+ .as_ref()
+ .is_some_and(|lease| lease.wayland);
+ if wayland {
+ if let Some(lease) = cx.global_mut::<AppWindows>().clean_capture.as_mut() {
+ lease.visible_fallback = Some(config);
+ }
+ cancel_clean_capture(cx);
+ } else {
+ cancel_clean_capture(cx);
+ if cx.global::<AppWindows>().clean_capture.is_none() {
+ begin_recording_after_preflight(config, cx);
+ }
+ }
+ return;
+ }
cancel_clean_capture(cx);
report_clean_capture_error(anyhow::Error::msg(error), cx);
} else {
@@ -3235,6 +3314,32 @@ pub fn clean_capture_stop_unavailable(generation: u64, error: String, cx: &mut A
}
}
+/// The start a Stop control failure can still record, with visible controls:
+/// only before the control ever worked, when its registration was cleaned up,
+/// and not for a recording appended to an open editor.
+#[cfg(target_os = "linux")]
+fn visible_fallback_start(error: &str, stop_was_ready: bool, cx: &mut App) -> Option<StartConfig> {
+ if !visible_fallback_allowed(error, stop_was_ready)
+ || RecordingSession::global(cx)
+ .read(cx)
+ .editor_recording_target()
+ .is_some()
+ {
+ return None;
+ }
+ cx.global_mut::<AppWindows>()
+ .clean_capture
+ .as_mut()
+ .and_then(|lease| lease.config.take())
+}
+
+#[cfg(target_os = "linux")]
+fn visible_fallback_allowed(error: &str, stop_was_ready: bool) -> bool {
+ !stop_was_ready
+ && !error.contains("could not be cleaned up safely")
+ && !error.contains("cleanup is unconfirmed")
+}
+
#[cfg(target_os = "linux")]
fn defer_window_until_capture_safe(cx: &mut App) -> bool {
if !cx
@@ -3303,6 +3408,7 @@ fn prepare_clean_capture(config: StartConfig, cx: &mut App) -> anyhow::Result<()
wayland,
restoring: false,
restoration_error: None,
+ visible_fallback: None,
retained_windows: if wayland {
vec![(main.into(), main_was_visible)]
} else {
@@ -3574,13 +3680,19 @@ fn begin_retained_capture_restore(cx: &mut App) {
Ok(()) => {
tracing::info!("recording UI restored after a clean capture");
crate::hotkeys::complete_clean_capture_stop(generation, cx);
- let _restored = cx.global_mut::<AppWindows>().clean_capture.take();
+ let restored = cx.global_mut::<AppWindows>().clean_capture.take();
update_camera_presentation(true, cx);
RecordingSession::global(cx).update(cx, |_, cx| cx.notify());
+ if let Some(config) = restored.and_then(|lease| lease.visible_fallback) {
+ begin_recording_after_preflight(config, cx);
+ }
}
Err(error) => {
if let Some(lease) = cx.global_mut::<AppWindows>().clean_capture.as_mut() {
lease.restoration_error = Some(error.clone());
+ // A later retry only brings the windows back; it does
+ // not start a recording nobody is asking for any more.
+ lease.visible_fallback = None;
}
tracing::error!(%error, "Recording UI restoration or Stop cleanup is unconfirmed");
RecordingSession::global(cx).update(cx, |session, cx| {
@@ -3929,7 +4041,7 @@ pub fn open_camera_window(cx: &mut App) {
})
.or_else(scap_targets::Display::get_containing_cursor)
.unwrap_or_else(scap_targets::Display::primary);
- let (x, y) = camera_window::opening_position(&display);
+ let (x, y) = camera_window::opening_position(&display, (width, height));
let display_id = gpui_display_id(Some(&display), cx);
let inline = camera_preview_is_inline(cx);
@@ -3948,7 +4060,7 @@ pub fn open_camera_window(cx: &mut App) {
..Default::default()
}),
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
#[cfg(target_os = "linux")]
window_decorations: Some(gpui::WindowDecorations::Client),
// Non-activating panel, same as the bar: the bubble is clickable
@@ -4810,7 +4922,7 @@ fn open_editor_window(
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// Native traffic lights, inset to (20, 32):
@@ -5703,6 +5815,10 @@ async fn drive_auto_sidebar(handle: WindowHandle<EditorWindow>, cx: &mut gpui::A
let add_track = std::env::var("CAP_GPUI_AUTO_ADD_TRACK").ok();
let generate_zoom =
std::env::var("CAP_GPUI_AUTO_GENERATE_ZOOM").is_ok_and(|value| value == "1");
+ let pick_background =
+ std::env::var("CAP_GPUI_AUTO_PICK_BACKGROUND").is_ok_and(|value| value == "1");
+ let desktop_background =
+ std::env::var("CAP_GPUI_AUTO_DESKTOP_BACKGROUND").is_ok_and(|value| value == "1");
if tab.is_none()
&& select.is_none()
&& canvas.is_none()
@@ -5711,6 +5827,8 @@ async fn drive_auto_sidebar(handle: WindowHandle<EditorWindow>, cx: &mut gpui::A
&& camera3d.is_none()
&& add_track.is_none()
&& !generate_zoom
+ && !pick_background
+ && !desktop_background
{
return;
}
@@ -5718,6 +5836,14 @@ async fn drive_auto_sidebar(handle: WindowHandle<EditorWindow>, cx: &mut gpui::A
.timer(std::time::Duration::from_millis(1200))
.await;
+ if desktop_background {
+ handle
+ .update(cx, |view, window, cx| {
+ view.import_desktop_background(window, cx)
+ })
+ .ok();
+ }
+
if let Some(spec) = tab {
let (name, scroll) = match spec.split_once(':') {
Some((name, scroll)) => (name.to_string(), scroll.parse::<f32>().ok()),
@@ -5745,6 +5871,14 @@ async fn drive_auto_sidebar(handle: WindowHandle<EditorWindow>, cx: &mut gpui::A
.ok();
}
+ if pick_background {
+ handle
+ .update(cx, |view, window, cx| {
+ view.pick_background_image(window, cx)
+ })
+ .ok();
+ }
+
if let Some(spec) = select {
handle
.update(cx, |view, _window, cx| view.auto_select_segments(&spec, cx))
@@ -6590,7 +6724,7 @@ fn open_controls(
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// No titlebar at all: with one, the panel still draws standard
@@ -6773,7 +6907,7 @@ pub fn open_screenshot_editor(path: PathBuf, cx: &mut App) {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
titlebar: Some(gpui::TitlebarOptions {
@@ -6939,6 +7073,33 @@ mod tests {
use super::*;
use crate::store::{DEFAULT_EXCLUDED_WINDOW_TITLES, WindowExclusion, default_excluded_windows};
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn a_start_without_any_stop_control_records_with_visible_controls() {
+ // No tray watcher and no granted shortcut, before either ever worked.
+ assert!(visible_fallback_allowed(
+ "Portal request failed: org.freedesktop.portal.Error.NotAllowed: An app id is required",
+ false,
+ ));
+ assert!(visible_fallback_allowed(
+ "The recording Stop worker ended",
+ false
+ ));
+ // A control that worked and then vanished keeps the existing stop.
+ assert!(!visible_fallback_allowed(
+ "The recording Stop tray is no longer available",
+ true
+ ));
+ // A registration that may still be live is not left behind.
+ assert!(!visible_fallback_allowed(
+ "The recording shortcut registration could not be cleaned up safely: timeout",
+ false,
+ ));
+ assert!(!visible_fallback_allowed(
+ "Recording Stop tray registration cleanup is unconfirmed: timeout",
+ false,
+ ));
+ }
#[test]
fn small_display_keeps_editor_and_minimum_size_inside_the_work_area() {
let available = inset_work_area(Bounds {
@@ -7215,6 +7376,7 @@ mod tests {
retained_windows: Vec::new(),
restoring: false,
restoration_error: None,
+ visible_fallback: None,
}
}
diff --git a/apps/desktop-gpui/src/camera_window.rs b/apps/desktop-gpui/src/camera_window.rs
index bcbce3eb7..defcf1f9f 100644
--- a/apps/desktop-gpui/src/camera_window.rs
+++ b/apps/desktop-gpui/src/camera_window.rs
@@ -2950,20 +2950,106 @@ fn saved_position(preferred: &scap_targets::Display) -> Option<(f64, f64)> {
/// Tauri default slot -- bottom-right of the preferred monitor measured with
/// `DEFAULT_WINDOW_SIZE = 230.0 * 2.0` (`windows.rs:2160, 2340-2344`; the
/// constant, not the actual window size, is what the shipping app subtracts).
-pub fn opening_position(preferred: &scap_targets::Display) -> (f32, f32) {
- if let Some((x, y)) = saved_position(preferred) {
- tracing::info!(x, y, "camera window restored to its saved position");
+/// Clamp against the actual size before the camera can deliver its first frame.
+pub fn opening_position(preferred: &scap_targets::Display, window_size: (f32, f32)) -> (f32, f32) {
+ let saved = saved_position(preferred);
+ let bounds = saved
+ .and_then(|(x, y)| display_for_point(x, y))
+ .and_then(|display| display.raw_handle().logical_bounds())
+ .or_else(|| preferred.raw_handle().logical_bounds());
+ opening_position_in_bounds(saved, bounds, window_size)
+}
+
+fn opening_position_in_bounds(
+ saved: Option<(f64, f64)>,
+ bounds: Option<scap_targets::bounds::LogicalBounds>,
+ window_size: (f32, f32),
+) -> (f32, f32) {
+ let Some(bounds) = bounds else {
+ let (x, y) = saved.unwrap_or((100., 100.));
return (x as f32, y as f32);
- }
+ };
const TAURI_DEFAULT_WINDOW_SIZE: f64 = 230.0 * 2.0;
- match preferred.raw_handle().logical_bounds() {
- Some(bounds) => (
- (bounds.position().x() + bounds.size().width() - TAURI_DEFAULT_WINDOW_SIZE - 100.)
- as f32,
- (bounds.position().y() + bounds.size().height() - TAURI_DEFAULT_WINDOW_SIZE - 100.)
- as f32,
- ),
- None => (100., 100.),
+ let (dx, dy) = (bounds.position().x(), bounds.position().y());
+ let (dw, dh) = (bounds.size().width(), bounds.size().height());
+ let (x, y) = saved.unwrap_or((
+ dx + dw - TAURI_DEFAULT_WINDOW_SIZE - 100.,
+ dy + dh - TAURI_DEFAULT_WINDOW_SIZE - 100.,
+ ));
+ (
+ x.clamp(dx, dx + (dw - f64::from(window_size.0)).max(0.)) as f32,
+ y.clamp(dy, dy + (dh - f64::from(window_size.1)).max(0.)) as f32,
+ )
+}
+
+#[cfg(test)]
+mod opening_position_tests {
+ use super::*;
+ use scap_targets::bounds::{LogicalBounds, LogicalPosition, LogicalSize};
+
+ fn display(x: f64, y: f64, width: f64, height: f64) -> Option<LogicalBounds> {
+ Some(LogicalBounds::new(
+ LogicalPosition::new(x, y),
+ LogicalSize::new(width, height),
+ ))
+ }
+
+ #[test]
+ fn restored_wide_preview_fits_after_display_resolution_changes() {
+ let state = CameraWindowState {
+ size: 600.,
+ shape: CameraShape::Full,
+ ..Default::default()
+ };
+ let dimensions = window_size(&state, Some(16. / 9.));
+ let (x, y) = opening_position_in_bounds(
+ Some((1800., 1000.)),
+ display(0., 0., 1920., 1080.),
+ dimensions,
+ );
+ assert!((x + dimensions.0 - 1920.).abs() < 0.001);
+ assert_eq!(y + dimensions.1, 1080.);
+ }
+
+ #[test]
+ fn fresh_large_preview_fits_on_a_display_with_negative_origin() {
+ assert_eq!(
+ opening_position_in_bounds(None, display(-1280., -800., 1280., 800.), (600., 656.)),
+ (-600., -656.),
+ );
+ }
+
+ #[test]
+ fn oversized_preview_is_anchored_to_the_display_origin() {
+ assert_eq!(
+ opening_position_in_bounds(None, display(-640., 200., 640., 480.), (1066., 656.)),
+ (-640., 200.),
+ );
+ }
+
+ #[test]
+ fn valid_saved_position_and_default_slot_are_preserved() {
+ let bounds = display(1920., 0., 1920., 1080.);
+ assert_eq!(
+ opening_position_in_bounds(Some((2000., 400.)), bounds, (230., 286.)),
+ (2000., 400.),
+ );
+ assert_eq!(
+ opening_position_in_bounds(None, bounds, (230., 286.)),
+ (3280., 520.),
+ );
+ }
+
+ #[test]
+ fn missing_display_geometry_preserves_the_existing_fallback() {
+ assert_eq!(
+ opening_position_in_bounds(None, None, (600., 656.)),
+ (100., 100.),
+ );
+ assert_eq!(
+ opening_position_in_bounds(Some((300., 400.)), None, (600., 656.)),
+ (300., 400.),
+ );
}
}
diff --git a/apps/desktop-gpui/src/deeplink.rs b/apps/desktop-gpui/src/deeplink.rs
index 206f60ba7..8a1523477 100644
--- a/apps/desktop-gpui/src/deeplink.rs
+++ b/apps/desktop-gpui/src/deeplink.rs
@@ -558,7 +558,7 @@ impl DeepLinkAction {
RecordingMetaInner::Instant(_) => {
let mp4_path = project_path.join("content/output.mp4");
if mp4_path.exists() && mp4_path.is_file() {
- library::open_path(&mp4_path);
+ library::open_with_system(&mp4_path, cx);
app_windows::hide_main_window(cx);
}
}
diff --git a/apps/desktop-gpui/src/devices.rs b/apps/desktop-gpui/src/devices.rs
index c2fa1cf56..745d9d46b 100644
--- a/apps/desktop-gpui/src/devices.rs
+++ b/apps/desktop-gpui/src/devices.rs
@@ -990,6 +990,11 @@ fn linux_window_targets_from_snapshot() -> Option<Vec<(WindowOption, Window)>> {
if app == "Window Server" {
return None;
}
+ // As on macOS: recording one of our own windows records a
+ // window the recording hides.
+ if snapshot.owner_pid == Some(std::process::id()) {
+ return None;
+ }
Some((
WindowOption {
id,
diff --git a/apps/desktop-gpui/src/editor_bench.rs b/apps/desktop-gpui/src/editor_bench.rs
index 824d5cd19..55cc468fd 100644
--- a/apps/desktop-gpui/src/editor_bench.rs
+++ b/apps/desktop-gpui/src/editor_bench.rs
@@ -141,7 +141,7 @@ fn seed_caption_fields(
) {
let step = view.total_duration() / SEEDED_CAPTION_SEGMENTS as f64;
let Some(timeline) = view.project.timeline.as_mut() else {
- return;
+ panic!("timeline-bench: the fixture has no timeline to seed caption fields");
};
timeline.caption_segments = (0..SEEDED_CAPTION_SEGMENTS)
.map(|index| cap_project::CaptionTrackSegment {
@@ -167,6 +167,11 @@ fn seed_caption_fields(
view.ensure_field(key, window, cx);
}
}
+ assert!(
+ view.fields.len() >= SEEDED_CAPTION_SEGMENTS * 3,
+ "timeline-bench: only {} caption fields were seeded",
+ view.fields.len()
+ );
}
pub(crate) async fn run(handle: WindowHandle<EditorWindow>, cx: &mut AsyncApp) {
@@ -288,6 +293,21 @@ pub(crate) async fn run(handle: WindowHandle<EditorWindow>, cx: &mut AsyncApp) {
);
}
+ // The visible case models the production sidebar on the Captions tab with
+ // caption 0 selected. `TickFields` seeds the fields without changing the
+ // tab, so set that state explicitly before either sync loop.
+ let Ok(()) = handle.update(cx, |view, _, cx| {
+ view.sidebar.tab = SidebarTab::Captions;
+ view.selection = Some(crate::editor_edits::Selection::single(
+ editor_timeline::TrackKind::Caption,
+ 0,
+ ));
+ cx.notify();
+ }) else {
+ eprintln!("timeline-bench: the editor window closed");
+ std::process::exit(1);
+ };
+
// The per-render field sync on its own, with the seeded fields in place.
let mut sync_us = Vec::with_capacity(MEASURED_FRAMES);
for iteration in 0..WARMUP_FRAMES + MEASURED_FRAMES {
@@ -311,5 +331,57 @@ pub(crate) async fn run(handle: WindowHandle<EditorWindow>, cx: &mut AsyncApp) {
}
draw_bench::report("timeline-bench", "sync-fields", &mut sync_us);
+ // The production path over the same seeded state: the Captions tab with
+ // caption 0 selected makes `prepare_sidebar_fields` list exactly these
+ // three keys (`TrackKind::Caption`'s arm), so this is what a render now
+ // syncs. The
+ // all-fields case above is the faithful old path. The keys are written out
+ // rather than derived by calling `prepare_sidebar_fields` because that
+ // call would also time key derivation and field creation, not the sync.
+ let visible = [
+ FieldKey::CaptionText(0),
+ FieldKey::CaptionStart(0),
+ FieldKey::CaptionEnd(0),
+ ];
+ let Ok(missing) = handle.update(cx, |view, _, _| {
+ visible
+ .iter()
+ .filter(|key| !view.fields.contains_key(key))
+ .count()
+ }) else {
+ eprintln!("timeline-bench: the editor window closed");
+ std::process::exit(1);
+ };
+ assert_eq!(
+ missing, 0,
+ "timeline-bench: the visible caption fields were not seeded"
+ );
+ let mut visible_sync_us = Vec::with_capacity(MEASURED_FRAMES);
+ for iteration in 0..WARMUP_FRAMES + MEASURED_FRAMES {
+ let Ok(elapsed) = handle.update(cx, |view, window, cx| {
+ if view.playing {
+ view.stop_for_measurement(cx);
+ }
+ let started = Instant::now();
+ view.sync_visible_fields(&visible, window, cx);
+ started.elapsed()
+ }) else {
+ eprintln!("timeline-bench: the editor window closed");
+ std::process::exit(1);
+ };
+ if iteration >= WARMUP_FRAMES {
+ visible_sync_us.push(elapsed.as_secs_f64() * 1e6);
+ }
+ }
+ println!(
+ "timeline-bench: sync-visible-fields over {} fields",
+ visible.len()
+ );
+ draw_bench::report(
+ "timeline-bench",
+ "sync-visible-fields",
+ &mut visible_sync_us,
+ );
+
std::process::exit(0);
}
diff --git a/apps/desktop-gpui/src/editor_clips.rs b/apps/desktop-gpui/src/editor_clips.rs
index 81cc72e22..865968054 100644
--- a/apps/desktop-gpui/src/editor_clips.rs
+++ b/apps/desktop-gpui/src/editor_clips.rs
@@ -379,6 +379,10 @@ impl Default for ClipsState {
}
impl ClipsState {
+ pub(crate) fn has_open_menu(&self) -> bool {
+ self.import_menu.is_some()
+ }
+
pub(crate) fn is_importing(&self) -> bool {
self.importing
}
diff --git a/apps/desktop-gpui/src/editor_color.rs b/apps/desktop-gpui/src/editor_color.rs
index a3948bee9..03311d6cd 100644
--- a/apps/desktop-gpui/src/editor_color.rs
+++ b/apps/desktop-gpui/src/editor_color.rs
@@ -35,7 +35,7 @@
//! Nine 216x122 tiles are generated once, on first paint, and cached for the
//! process: the catalogue is static, so nothing can invalidate them.
-use std::sync::Arc;
+use std::sync::{Arc, OnceLock};
pub use cap_gpui_kernels::texture::{Filter, Overlay};
use cap_project::{ColorCorrection, ProjectConfiguration};
@@ -326,6 +326,25 @@ pub enum GradeSlider {
}
impl GradeSlider {
+ /// Whether this slider writes a field
+ /// [`crate::editor_timeline::TimelineModel::build`] or
+ /// [`crate::editor_timeline::TrackLanes::from_project`] reads. The match
+ /// is exhaustive so a new slider has to make the choice explicitly.
+ pub(crate) fn changes_timeline_model(self) -> bool {
+ match self {
+ Self::Grain
+ | Self::Intensity
+ | Self::Exposure
+ | Self::Contrast
+ | Self::Saturation
+ | Self::Temperature
+ | Self::Tint
+ | Self::Fade
+ | Self::SplitTone
+ | Self::Vignette => false,
+ }
+ }
+
/// The nine rows inside "Fine-tune colors", in order.
pub const ADJUST: [GradeSlider; 9] = [
Self::Intensity,
@@ -492,29 +511,34 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- self.edit_project("grade", window, cx, move |project| {
- let grade = Self::grade_mut(project, target);
- if (slider.read(grade) - value).abs() < f32::EPSILON {
- return false;
- }
- slider.write(grade, value);
- if !slider.keeps_preset() {
- grade.preset = PRESET_CUSTOM.to_string();
- }
- true
- });
+ self.edit_project_with_rebuild(
+ slider.changes_timeline_model(),
+ "grade",
+ window,
+ cx,
+ move |project| {
+ let grade = Self::grade_mut(project, target);
+ if (slider.read(grade) - value).abs() < f32::EPSILON {
+ return false;
+ }
+ slider.write(grade, value);
+ if !slider.keeps_preset() {
+ grade.preset = PRESET_CUSTOM.to_string();
+ }
+ true
+ },
+ );
}
- /// The tile grid's images, generated once per process.
- fn preset_image(&self, index: usize) -> Option<Arc<RenderImage>> {
+ fn preset_image(index: usize) -> Option<Arc<RenderImage>> {
let preset = COLOR_CORRECTION_PRESETS.get(index)?;
- let mut cache = self.sidebar.grade_previews.borrow_mut();
- if let Some(image) = cache.get(preset.id) {
- return Some(image.clone());
- }
- let image = preset_preview(preset, PREVIEW_WIDTH, PREVIEW_HEIGHT);
- cache.insert(preset.id, image.clone());
- Some(image)
+ static PREVIEWS: [OnceLock<Arc<RenderImage>>; COLOR_CORRECTION_PRESETS.len()] =
+ [const { OnceLock::new() }; COLOR_CORRECTION_PRESETS.len()];
+ Some(
+ PREVIEWS[index]
+ .get_or_init(|| preset_preview(preset, PREVIEW_WIDTH, PREVIEW_HEIGHT))
+ .clone(),
+ )
}
/// The whole section: the preset grid, Grain, the screen-only "Apply to
@@ -577,7 +601,7 @@ impl EditorWindow {
.h(px(60.75))
.rounded(px(6.))
.overflow_hidden()
- .children(self.preset_image(index).map(|image| {
+ .children(Self::preset_image(index).map(|image| {
img(image).size_full().object_fit(gpui::ObjectFit::Fill)
})),
)
@@ -679,6 +703,24 @@ mod tests {
gradient_position, overlay_blend, vignette_alpha,
};
+ #[test]
+ fn cached_previews_preserve_pixels_and_share_images() {
+ for (index, preset) in COLOR_CORRECTION_PRESETS.iter().enumerate() {
+ let image = EditorWindow::preset_image(index).unwrap();
+ let expected = preset_preview(preset, PREVIEW_WIDTH, PREVIEW_HEIGHT);
+ assert_eq!(image.as_bytes(0), expected.as_bytes(0));
+ assert!(Arc::ptr_eq(
+ &image,
+ &EditorWindow::preset_image(index).unwrap(),
+ ));
+ let id = image.id;
+ drop(image);
+ assert_eq!(EditorWindow::preset_image(index).unwrap().id, id);
+ }
+ assert!(EditorWindow::preset_image(COLOR_CORRECTION_PRESETS.len()).is_none());
+ assert!(EditorWindow::preset_image(usize::MAX).is_none());
+ }
+
#[test]
fn the_catalogue_matches_the_source() {
assert_eq!(COLOR_CORRECTION_PRESETS.len(), 9);
diff --git a/apps/desktop-gpui/src/editor_export.rs b/apps/desktop-gpui/src/editor_export.rs
index 53252dccb..e3c53ce48 100644
--- a/apps/desktop-gpui/src/editor_export.rs
+++ b/apps/desktop-gpui/src/editor_export.rs
@@ -2649,9 +2649,9 @@ impl EditorWindow {
ui::ButtonSize::Md,
)
.label("Open File")
- .on_click(move |_, _, _| {
+ .on_click(move |_, _, cx| {
if let Some(path) = &path {
- library::open_path(path);
+ library::open_with_system(path, cx);
}
}),
)
@@ -2736,6 +2736,7 @@ async fn confirm_export_resources(
let result = async {
let warning = check.map_err(|error| error.to_string())??;
if let Some(message) = warning {
+ tracing::info!(%message, "export waits for the user to confirm its resources");
#[cfg(target_os = "linux")]
let confirmed = {
let response = loop {
diff --git a/apps/desktop-gpui/src/editor_panels.rs b/apps/desktop-gpui/src/editor_panels.rs
index ec9bdbc5d..cdedff72c 100644
--- a/apps/desktop-gpui/src/editor_panels.rs
+++ b/apps/desktop-gpui/src/editor_panels.rs
@@ -2727,27 +2727,60 @@ impl EditorWindow {
self.fields.insert(key, input);
}
- /// The `createWritableMemo` half: re-derive a field's text whenever the
- /// value moves under it (an undo, a preset, a timeline drag), but never
- /// while it has focus. Runs from `render`, like `sync_hex_inputs`, because
- /// the focus test needs a `&Window`.
+ /// The all-fields path: every field ever created, whether the frame draws
+ /// it or not. Kept as the faithful "before" for the benchmark and the
+ /// equivalence tests; production renders go through
+ /// [`Self::sync_visible_fields`].
pub(crate) fn sync_fields(&mut self, window: &mut Window, cx: &mut Context<Self>) {
- // Every field ever created is checked on every render, so the value is
- // written into one reused buffer and only copied out when it moved.
+ // The value is written into one reused buffer and only copied out
+ // when it moved.
let mut value = String::new();
for (key, input) in &self.fields {
- let state = input.read(cx);
- if state.is_focused(window) {
- continue;
- }
- value.clear();
- if self.write_field_value(*key, &mut value).is_none() {
+ self.sync_field(input, *key, &mut value, window, cx);
+ }
+ }
+
+ /// The production path: only the fields [`Self::prepare_sidebar_fields`]
+ /// says the next frame draws. A field that is not listed keeps its old
+ /// text until a later frame lists it again -- that is how a hidden field
+ /// catches up when it becomes visible. Focused fields are never written,
+ /// exactly as on the all-fields path.
+ pub(crate) fn sync_visible_fields(
+ &mut self,
+ keys: &[FieldKey],
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ let mut value = String::new();
+ for key in keys {
+ let Some(input) = self.fields.get(key) else {
continue;
- }
- if state.text() != value {
- let text = value.clone();
- input.update(cx, |input, cx| input.set_text(text, cx));
- }
+ };
+ self.sync_field(input, *key, &mut value, window, cx);
+ }
+ }
+
+ /// One field, shared by both sync paths so the visible-only path cannot
+ /// drift from the all-fields one.
+ fn sync_field(
+ &self,
+ input: &Entity<ui::TextInputState>,
+ key: FieldKey,
+ value: &mut String,
+ window: &Window,
+ cx: &mut Context<Self>,
+ ) {
+ let state = input.read(cx);
+ if state.is_focused(window) {
+ return;
+ }
+ value.clear();
+ if self.write_field_value(key, value).is_none() {
+ return;
+ }
+ if state.text() != value {
+ let text = value.clone();
+ input.update(cx, |input, cx| input.set_text(text, cx));
}
}
@@ -7930,7 +7963,7 @@ impl EditorWindow {
}
impl EditorWindow {
- /// Every text field the frame about to be built will draw.
+ /// Every text field the frame about to be built may draw.
///
/// Creating one needs `&mut Window`, and the sidebar's render chain is
/// threaded with `&self`, so the set is computed here from the same state
@@ -7942,9 +7975,8 @@ impl EditorWindow {
let mut colors: Vec<ColorTarget> = Vec::new();
// The crop dialog's four `BoundInput`s, while it is open. Created up
- // front rather than pushed onto `fields`, because the match below
- // bails early when the project has no timeline and the dialog is
- // independent of it.
+ // front because the match below bails early when the project has no
+ // timeline and the dialog is independent of it.
if self.crop.is_some() {
use crate::editor_crop::CropField;
for field in [
@@ -7953,7 +7985,9 @@ impl EditorWindow {
CropField::X,
CropField::Y,
] {
- self.ensure_field(FieldKey::Crop(field), window, cx);
+ let key = FieldKey::Crop(field);
+ self.ensure_field(key, window, cx);
+ fields.push(key);
}
}
@@ -7966,6 +8000,9 @@ impl EditorWindow {
warm_installed_fonts();
}
let Some(timeline) = self.timeline() else {
+ // The crop dialog does not need the timeline, and its
+ // fields were already ensured above.
+ self.sync_visible_fields(&fields, window, cx);
return;
};
let indices = |length: usize| -> Vec<usize> {
@@ -8068,13 +8105,13 @@ impl EditorWindow {
},
}
- for key in fields {
+ for &key in &fields {
self.ensure_field(key, window, cx);
}
for target in colors {
self.ensure_hex_input(target, window, cx);
}
- self.sync_fields(window, cx);
+ self.sync_visible_fields(&fields, window, cx);
}
}
diff --git a/apps/desktop-gpui/src/editor_sidebar.rs b/apps/desktop-gpui/src/editor_sidebar.rs
index 186d4e180..ee004dbae 100644
--- a/apps/desktop-gpui/src/editor_sidebar.rs
+++ b/apps/desktop-gpui/src/editor_sidebar.rs
@@ -653,6 +653,31 @@ pub enum BgSlider {
}
impl BgSlider {
+ /// Whether this slider writes a field
+ /// [`crate::editor_timeline::TimelineModel::build`] or
+ /// [`crate::editor_timeline::TrackLanes::from_project`] reads. The match
+ /// is exhaustive so a new slider has to make the choice explicitly.
+ pub(crate) fn changes_timeline_model(self) -> bool {
+ match self {
+ Self::Blur
+ | Self::Padding
+ | Self::Rounding
+ | Self::MotionBlur
+ | Self::BorderWidth
+ | Self::BorderOpacity
+ | Self::NotchWidth
+ | Self::NotchHeight
+ | Self::NotchX
+ | Self::Shadow
+ | Self::ShadowSize
+ | Self::ShadowOpacity
+ | Self::ShadowBlur
+ | Self::GradientAngle
+ | Self::GradientNoise
+ | Self::GradientGrain => false,
+ }
+ }
+
/// `minValue` / `maxValue` / `step`, per call site. `NotchX`'s maximum is
/// `notchXMax()` and therefore not constant; it is patched in by
/// [`EditorWindow::slider_limits`].
@@ -800,9 +825,6 @@ pub struct SidebarState {
/// `ColorCorrectionSection`'s `adjustOpen` signal -- one per instance, so
/// the Background tab's and the Camera tab's open independently.
grade_adjust: [CollapsibleState; 2],
- /// The nine preset tiles, generated once for the process. The catalogue is
- /// static, so nothing can invalidate them.
- pub(crate) grade_previews: std::cell::RefCell<HashMap<&'static str, Arc<RenderImage>>>,
/// The camera tab's own `ShadowSettings` reveal, and the cursor tab's
/// `KCollapsible open={!project.cursor.raw}` physics panel.
pub camera_shadow_open: CollapsibleState,
@@ -906,7 +928,6 @@ impl SidebarState {
pads: std::cell::RefCell::new(HashMap::new()),
pad_drag: None,
grade_adjust: [CollapsibleState::new(false), CollapsibleState::new(false)],
- grade_previews: std::cell::RefCell::new(HashMap::new()),
camera_shadow_open: CollapsibleState::new(false),
cursor_physics_open: CollapsibleState::new(!config.cursor.raw),
cursor_ripple_open: CollapsibleState::new(config.cursor.ripple.enabled),
@@ -1065,6 +1086,32 @@ impl EditorWindow {
change: impl FnOnce(&mut ProjectConfiguration) -> bool,
window: &mut Window,
cx: &mut Context<Self>,
+ ) {
+ self.edit_background_inner(true, reason, change, window, cx);
+ }
+
+ /// [`Self::edit_background`] for edits that cannot change the timeline
+ /// model or its lanes -- the background tab's sliders and the
+ /// animated-gradient add/remove/stop/reset edits, whose fields
+ /// [`crate::editor_timeline::TimelineModel::build`] and
+ /// [`crate::editor_timeline::TrackLanes::from_project`] do not read.
+ fn edit_background_without_timeline(
+ &mut self,
+ reason: &'static str,
+ change: impl FnOnce(&mut ProjectConfiguration) -> bool,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ self.edit_background_inner(false, reason, change, window, cx);
+ }
+
+ fn edit_background_inner(
+ &mut self,
+ rebuild_timeline: bool,
+ reason: &'static str,
+ change: impl FnOnce(&mut ProjectConfiguration) -> bool,
+ window: &mut Window,
+ cx: &mut Context<Self>,
) {
if !self.project_ready() {
return;
@@ -1080,7 +1127,11 @@ impl EditorWindow {
if !self.apply_control_change(change) {
return;
}
- self.project_changed(window, cx);
+ if rebuild_timeline {
+ self.project_changed(window, cx);
+ } else {
+ self.project_changed_without_timeline(window, cx);
+ }
self.remember_animated_gradient_selection(previous_animated_gradient, window, cx);
self.note_sidebar_edit(reason);
}
@@ -1095,6 +1146,31 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
change: impl FnOnce(&mut ProjectConfiguration) -> bool,
+ ) {
+ self.edit_project_with_rebuild(true, reason, window, cx, change);
+ }
+
+ /// [`Self::edit_project`] for edits that cannot change the timeline model.
+ pub(crate) fn edit_project_without_timeline(
+ &mut self,
+ reason: &'static str,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ change: impl FnOnce(&mut ProjectConfiguration) -> bool,
+ ) {
+ self.edit_project_with_rebuild(false, reason, window, cx, change);
+ }
+
+ /// The shared project write. `rebuild_timeline` is true for edits that
+ /// touch a field [`crate::editor_timeline::TimelineModel::build`] or
+ /// [`crate::editor_timeline::TrackLanes::from_project`] reads.
+ pub(crate) fn edit_project_with_rebuild(
+ &mut self,
+ rebuild_timeline: bool,
+ reason: &'static str,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ change: impl FnOnce(&mut ProjectConfiguration) -> bool,
) {
if !self.project_ready() {
return;
@@ -1103,7 +1179,11 @@ impl EditorWindow {
if !self.apply_control_change(change) {
return;
}
- self.project_changed(window, cx);
+ if rebuild_timeline {
+ self.project_changed(window, cx);
+ } else {
+ self.project_changed_without_timeline(window, cx);
+ }
self.note_project_edit(reason);
}
@@ -1349,7 +1429,8 @@ impl EditorWindow {
cx: &mut Context<Self>,
) {
let base = self.notch_base();
- self.edit_background(
+ self.edit_background_inner(
+ slider.changes_timeline_model(),
"slider",
|project| {
let background = &mut project.background;
@@ -1999,8 +2080,12 @@ fn write_desktop_background_snapshot(source: &Path, output: &Path) -> Result<(),
}
}
- let decoded = cap_gpui_kernels::codec::decode_image_file(source)
- .map_err(|err| format!("failed to decode desktop background: {err}"))?;
+ #[cfg(target_os = "linux")]
+ let decoded = crate::linux_wallpaper::decode_jpeg_xl(source)
+ .unwrap_or_else(|| cap_gpui_kernels::codec::decode_image_file(source));
+ #[cfg(not(target_os = "linux"))]
+ let decoded = cap_gpui_kernels::codec::decode_image_file(source);
+ let decoded = decoded.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
{
@@ -2342,7 +2427,7 @@ impl EditorWindow {
/// goes through a task rather than the click's update, and the chosen file
/// is copied into the app-data dir as `bg-<ts>-<name>` exactly as the
/// `<input type="file">` handler does (`:2513-2541`).
- fn pick_background_image(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ pub(crate) fn pick_background_image(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.sidebar.picking_image {
return;
}
@@ -2350,6 +2435,14 @@ impl EditorWindow {
let style_target = self.sidebar.style_target;
let style_fingerprint = self.style_target_fingerprint();
self.sidebar.picker_task = Some(cx.spawn_in(window, async move |this, cx| {
+ #[cfg(target_os = "linux")]
+ let picked = crate::platform::open_file_panel_async(
+ &[("Image", &BACKGROUND_IMAGE_EXTENSIONS[..])],
+ None,
+ cx,
+ )
+ .await;
+ #[cfg(not(target_os = "linux"))]
let picked = cx
.update(|_, _| crate::platform::open_image_panel(&BACKGROUND_IMAGE_EXTENSIONS))
.ok()
@@ -2397,6 +2490,7 @@ impl EditorWindow {
}
match stored {
Ok(path) => {
+ tracing::info!(path = %path.display(), "background image chosen");
this.edit_background(
"image",
|project| {
@@ -2422,7 +2516,7 @@ impl EditorWindow {
}
/// `importDesktopBackground` (`:2033-2052`).
- fn import_desktop_background(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ pub(crate) fn import_desktop_background(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.sidebar.importing_desktop {
return;
}
@@ -2446,6 +2540,7 @@ impl EditorWindow {
}
match imported {
Ok(path) => {
+ tracing::info!(path = %path.display(), "desktop wallpaper imported");
this.sidebar.desktop_background = Some(path.clone());
this.sidebar.source_tab = SourceTab::Desktop;
this.ensure_background_presentation(from_none);
@@ -2578,10 +2673,11 @@ impl EditorWindow {
SidebarTab::Captions => self.render_captions_tab(cx),
};
+ // Block, not a flex column: its one child lays out the same, without
+ // the extra layout pass a flex column spends sizing it on every rebuild.
div()
.id("sidebar-scroll")
- .flex()
- .flex_col()
+ .block()
.flex_1()
.min_h_0()
.overflow_x_hidden()
diff --git a/apps/desktop-gpui/src/editor_sidebar/animated_gradient.rs b/apps/desktop-gpui/src/editor_sidebar/animated_gradient.rs
index 2e9256356..eeda7203a 100644
--- a/apps/desktop-gpui/src/editor_sidebar/animated_gradient.rs
+++ b/apps/desktop-gpui/src/editor_sidebar/animated_gradient.rs
@@ -503,7 +503,7 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- self.edit_background(
+ self.edit_background_without_timeline(
"animated-gradient-setting",
|project| match &mut project.background.source {
BackgroundSource::AnimatedGradient { config } => change(config),
diff --git a/apps/desktop-gpui/src/editor_tabs.rs b/apps/desktop-gpui/src/editor_tabs.rs
index 9ab09bb06..7801a8d0f 100644
--- a/apps/desktop-gpui/src/editor_tabs.rs
+++ b/apps/desktop-gpui/src/editor_tabs.rs
@@ -354,6 +354,22 @@ pub enum CameraSlider {
}
impl CameraSlider {
+ /// Whether this slider writes a field
+ /// [`crate::editor_timeline::TimelineModel::build`] or
+ /// [`crate::editor_timeline::TrackLanes::from_project`] reads. The match
+ /// is exhaustive so a new slider has to make the choice explicitly.
+ pub(crate) fn changes_timeline_model(self) -> bool {
+ match self {
+ Self::Size
+ | Self::ZoomSize
+ | Self::Rounding
+ | Self::Shadow
+ | Self::ShadowSize
+ | Self::ShadowOpacity
+ | Self::ShadowBlur => false,
+ }
+ }
+
pub fn limits(self) -> (f32, f32, f32) {
match self {
// `minValue={20} maxValue={80} step={0.1}` (`:3204-3206`).
@@ -385,6 +401,15 @@ pub enum AudioSlider {
}
impl AudioSlider {
+ /// Both audio sliders write levels [`TimelineModel::build`] reads; the
+ /// lanes do not read audio levels. The match is exhaustive so a new
+ /// slider has to make the choice explicitly.
+ pub(crate) fn changes_timeline_model(self) -> bool {
+ match self {
+ Self::MicVolume | Self::SystemVolume => true,
+ }
+ }
+
/// `minValue={-30} maxValue={10} step={0.1}` on both (`:787-791`,
/// `:806-810`).
pub fn limits(self) -> (f32, f32, f32) {
@@ -413,6 +438,24 @@ pub enum CursorSlider {
}
impl CursorSlider {
+ /// Whether this slider writes a field
+ /// [`crate::editor_timeline::TimelineModel::build`] or
+ /// [`crate::editor_timeline::TrackLanes::from_project`] reads. The match
+ /// is exhaustive so a new slider has to make the choice explicitly.
+ pub(crate) fn changes_timeline_model(self) -> bool {
+ match self {
+ Self::Size
+ | Self::Tilt
+ | Self::IdleDelay
+ | Self::Tension
+ | Self::Friction
+ | Self::Mass
+ | Self::RippleStrength
+ | Self::RippleSize
+ | Self::RippleDuration => false,
+ }
+ }
+
pub fn limits(self) -> (f32, f32, f32) {
match self {
Self::Size => (20., 300., 1.),
@@ -454,15 +497,83 @@ pub enum CaptionSlider {
Size,
BackgroundOpacity,
FadeDuration,
+ ManualX,
+ ManualY,
+}
+
+/// Where Manual placement starts for a preset no caption was drawn at yet, as
+/// fractions of the frame (`captionPositionCenter`, `CaptionsTab.tsx:362-378`).
+pub fn caption_position_center(position: &str) -> (f32, f32) {
+ match position {
+ "top-left" => (0.05, 0.08),
+ "top-center" | "top" => (0.5, 0.08),
+ "top-right" => (0.95, 0.08),
+ "bottom-left" => (0.05, 0.85),
+ "bottom-right" => (0.95, 0.85),
+ _ => (0.5, 0.85),
+ }
+}
+
+pub fn caption_manual_center(settings: &CaptionSettings) -> (f32, f32) {
+ settings.manual_position.map_or_else(
+ || caption_position_center(&settings.position),
+ |xy| (xy.x, xy.y),
+ )
+}
+
+pub fn set_caption_manual_center(settings: &mut CaptionSettings, x: Option<f32>, y: Option<f32>) {
+ let (current_x, current_y) = caption_manual_center(settings);
+ let fraction = |value: f32| (value / 100.).clamp(0., 1.);
+ settings.position = "manual".to_string();
+ settings.manual_position = Some(cap_project::XY::new(
+ x.map_or(current_x, fraction),
+ y.map_or(current_y, fraction),
+ ));
+}
+
+/// Switching the caption position. Entering Manual from a preset seeds the
+/// manual centre from that preset (`updateCaptionPosition`,
+/// `CaptionsTab.tsx:380-394`); without it Manual drew bottom-centre and had
+/// nothing to adjust.
+pub fn set_caption_position(settings: &mut CaptionSettings, position: &str) {
+ set_caption_position_from(settings, position, cap_rendering::last_caption_center);
+}
+
+/// [`set_caption_position`] with the renderer's last drawn caption centre
+/// supplied: left and right presets align the box's edge to a margin, so
+/// only the drawn box says where its centre is.
+fn set_caption_position_from(
+ settings: &mut CaptionSettings,
+ position: &str,
+ drawn_center: impl FnOnce(&str) -> Option<(f32, f32)>,
+) {
+ if position == "manual" && settings.position != "manual" {
+ let (x, y) = drawn_center(&settings.position)
+ .unwrap_or_else(|| caption_position_center(&settings.position));
+ settings.manual_position = Some(cap_project::XY::new(x, y));
+ }
+ settings.position = position.to_string();
}
impl CaptionSlider {
+ /// Whether this slider writes a field
+ /// [`crate::editor_timeline::TimelineModel::build`] or
+ /// [`crate::editor_timeline::TrackLanes::from_project`] reads. The match
+ /// is exhaustive so a new slider has to make the choice explicitly.
+ pub(crate) fn changes_timeline_model(self) -> bool {
+ match self {
+ Self::Size | Self::BackgroundOpacity | Self::FadeDuration => false,
+ Self::ManualX | Self::ManualY => false,
+ }
+ }
+
pub fn limits(self) -> (f32, f32, f32) {
match self {
Self::Size => (12., 100., 1.),
Self::BackgroundOpacity => (0., 100., 1.),
// `value={[getSetting("fadeDuration") * 100]}` over 0..50.
Self::FadeDuration => (0., 50., 1.),
+ Self::ManualX | Self::ManualY => (0., 100., 1.),
}
}
@@ -472,6 +583,8 @@ impl CaptionSlider {
Self::Size => settings.size as f32,
Self::BackgroundOpacity => settings.background_opacity as f32,
Self::FadeDuration => settings.fade_duration * 100.,
+ Self::ManualX => caption_manual_center(&settings).0 * 100.,
+ Self::ManualY => caption_manual_center(&settings).1 * 100.,
}
}
}
@@ -486,6 +599,20 @@ pub enum KeyboardSlider {
}
impl KeyboardSlider {
+ /// Whether this slider writes a field
+ /// [`crate::editor_timeline::TimelineModel::build`] or
+ /// [`crate::editor_timeline::TrackLanes::from_project`] reads. The match
+ /// is exhaustive so a new slider has to make the choice explicitly.
+ pub(crate) fn changes_timeline_model(self) -> bool {
+ match self {
+ Self::Size
+ | Self::BackgroundOpacity
+ | Self::FadeDuration
+ | Self::LingerDuration
+ | Self::GroupingThreshold => false,
+ }
+ }
+
pub fn limits(self) -> (f32, f32, f32) {
match self {
Self::Size => (12., 100., 1.),
@@ -894,9 +1021,8 @@ impl EditorWindow {
let Some((value, _)) = CAPTION_POSITIONS.get(index) else {
return;
};
- let value = value.to_string();
self.set_caption_setting("caption-position", window, cx, move |settings| {
- settings.position = value
+ set_caption_position(settings, value)
});
}
SidebarMenu::CaptionAnimation => {
@@ -959,7 +1085,18 @@ impl EditorWindow {
cx: &mut Context<Self>,
change: impl FnOnce(&mut CaptionSettings),
) {
- self.edit_project(reason, window, cx, |project| {
+ self.set_caption_setting_inner(true, reason, window, cx, change);
+ }
+
+ fn set_caption_setting_inner(
+ &mut self,
+ rebuild_timeline: bool,
+ reason: &'static str,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ change: impl FnOnce(&mut CaptionSettings),
+ ) {
+ let apply = |project: &mut ProjectConfiguration| {
with_caption_settings(project, |settings| {
change(settings);
// `STYLE_PRESET_KEYS` knocks the grade to "custom"
@@ -970,7 +1107,12 @@ impl EditorWindow {
settings.preset = "custom".to_string();
}
})
- });
+ };
+ if rebuild_timeline {
+ self.edit_project(reason, window, cx, apply);
+ } else {
+ self.edit_project_without_timeline(reason, window, cx, apply);
+ }
}
pub(crate) fn set_keyboard_setting(
@@ -980,9 +1122,23 @@ impl EditorWindow {
cx: &mut Context<Self>,
change: impl FnOnce(&mut KeyboardSettings),
) {
- self.edit_project(reason, window, cx, |project| {
- with_keyboard_settings(project, change)
- });
+ self.set_keyboard_setting_inner(true, reason, window, cx, change);
+ }
+
+ fn set_keyboard_setting_inner(
+ &mut self,
+ rebuild_timeline: bool,
+ reason: &'static str,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ change: impl FnOnce(&mut KeyboardSettings),
+ ) {
+ let apply = |project: &mut ProjectConfiguration| with_keyboard_settings(project, change);
+ if rebuild_timeline {
+ self.edit_project(reason, window, cx, apply);
+ } else {
+ self.edit_project_without_timeline(reason, window, cx, apply);
+ }
}
}
@@ -998,29 +1154,36 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- self.edit_project("camera-slider", window, cx, move |project| {
- let camera = &mut project.camera;
- match slider {
- CameraSlider::Size => camera.size = 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})`
- // (`:3253-3259`) -- the same UI fallback the background tab has.
- CameraSlider::ShadowSize
- | CameraSlider::ShadowOpacity
- | CameraSlider::ShadowBlur => {
- let mut shadow = camera.advanced_shadow.clone().unwrap_or(UI_SHADOW_FALLBACK);
- match slider {
- CameraSlider::ShadowSize => shadow.size = value,
- CameraSlider::ShadowOpacity => shadow.opacity = value,
- _ => shadow.blur = value,
+ self.edit_project_with_rebuild(
+ slider.changes_timeline_model(),
+ "camera-slider",
+ window,
+ cx,
+ move |project| {
+ let camera = &mut project.camera;
+ match slider {
+ CameraSlider::Size => camera.size = 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})`
+ // (`:3253-3259`) -- the same UI fallback the background tab has.
+ CameraSlider::ShadowSize
+ | CameraSlider::ShadowOpacity
+ | CameraSlider::ShadowBlur => {
+ let mut shadow =
+ camera.advanced_shadow.clone().unwrap_or(UI_SHADOW_FALLBACK);
+ match slider {
+ CameraSlider::ShadowSize => shadow.size = value,
+ CameraSlider::ShadowOpacity => shadow.opacity = value,
+ _ => shadow.blur = value,
+ }
+ camera.advanced_shadow = Some(shadow);
}
- camera.advanced_shadow = Some(shadow);
}
- }
- true
- });
+ true
+ },
+ );
}
pub(crate) fn apply_audio_slider(
@@ -1030,13 +1193,19 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- self.edit_project("audio-slider", window, cx, move |project| {
- match slider {
- AudioSlider::MicVolume => project.audio.mic_volume_db = value,
- AudioSlider::SystemVolume => project.audio.system_volume_db = value,
- }
- true
- });
+ self.edit_project_with_rebuild(
+ slider.changes_timeline_model(),
+ "audio-slider",
+ window,
+ cx,
+ move |project| {
+ match slider {
+ AudioSlider::MicVolume => project.audio.mic_volume_db = value,
+ AudioSlider::SystemVolume => project.audio.system_volume_db = value,
+ }
+ true
+ },
+ );
}
pub(crate) fn apply_cursor_slider(
@@ -1046,34 +1215,40 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- self.edit_project("cursor-slider", window, cx, move |project| {
- let cursor = &mut project.cursor;
- match slider {
- CursorSlider::Size => cursor.size = value.round() as u32,
- CursorSlider::Tilt => cursor.rotation_amount = value,
- // `clampIdleDelay`: `round(min(5, max(0.5, v)) * 10) / 10`
- // (`:525-526`).
- CursorSlider::IdleDelay => {
- cursor.hide_when_idle_delay = (value.clamp(0.5, 5.) * 10.).round() / 10.
- }
- // `setCursorPhysics` (`:531-546`): write the field, then
- // re-match the four presets and fall to `custom`.
- CursorSlider::Tension | CursorSlider::Friction | CursorSlider::Mass => {
- match slider {
- CursorSlider::Tension => cursor.tension = value,
- CursorSlider::Friction => cursor.friction = value,
- _ => cursor.mass = value,
+ self.edit_project_with_rebuild(
+ slider.changes_timeline_model(),
+ "cursor-slider",
+ window,
+ cx,
+ move |project| {
+ let cursor = &mut project.cursor;
+ match slider {
+ CursorSlider::Size => cursor.size = value.round() as u32,
+ CursorSlider::Tilt => cursor.rotation_amount = value,
+ // `clampIdleDelay`: `round(min(5, max(0.5, v)) * 10) / 10`
+ // (`:525-526`).
+ CursorSlider::IdleDelay => {
+ cursor.hide_when_idle_delay = (value.clamp(0.5, 5.) * 10.).round() / 10.
+ }
+ // `setCursorPhysics` (`:531-546`): write the field, then
+ // re-match the four presets and fall to `custom`.
+ CursorSlider::Tension | CursorSlider::Friction | CursorSlider::Mass => {
+ match slider {
+ CursorSlider::Tension => cursor.tension = value,
+ CursorSlider::Friction => cursor.friction = value,
+ _ => cursor.mass = value,
+ }
+ cursor.animation_style =
+ match_cursor_preset(cursor.tension, cursor.mass, cursor.friction)
+ .unwrap_or(CursorAnimationStyle::Custom);
}
- cursor.animation_style =
- match_cursor_preset(cursor.tension, cursor.mass, cursor.friction)
- .unwrap_or(CursorAnimationStyle::Custom);
+ CursorSlider::RippleStrength => cursor.ripple.strength = value / 100.,
+ CursorSlider::RippleSize => cursor.ripple.size = value / 100.,
+ CursorSlider::RippleDuration => cursor.ripple.duration = value,
}
- CursorSlider::RippleStrength => cursor.ripple.strength = value / 100.,
- CursorSlider::RippleSize => cursor.ripple.size = value / 100.,
- CursorSlider::RippleDuration => cursor.ripple.duration = value,
- }
- true
- });
+ true
+ },
+ );
}
pub(crate) fn apply_caption_slider(
@@ -1083,11 +1258,21 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- self.set_caption_setting("caption-slider", window, cx, move |settings| match slider {
- CaptionSlider::Size => settings.size = value.round() as u32,
- CaptionSlider::BackgroundOpacity => settings.background_opacity = value.round() as u32,
- CaptionSlider::FadeDuration => settings.fade_duration = value / 100.,
- });
+ self.set_caption_setting_inner(
+ slider.changes_timeline_model(),
+ "caption-slider",
+ window,
+ cx,
+ move |settings| match slider {
+ CaptionSlider::Size => settings.size = value.round() as u32,
+ CaptionSlider::BackgroundOpacity => {
+ settings.background_opacity = value.round() as u32
+ }
+ CaptionSlider::FadeDuration => settings.fade_duration = value / 100.,
+ CaptionSlider::ManualX => set_caption_manual_center(settings, Some(value), None),
+ CaptionSlider::ManualY => set_caption_manual_center(settings, None, Some(value)),
+ },
+ );
}
pub(crate) fn apply_keyboard_slider(
@@ -1097,7 +1282,8 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- self.set_keyboard_setting(
+ self.set_keyboard_setting_inner(
+ slider.changes_timeline_model(),
"keyboard-slider",
window,
cx,
@@ -2847,6 +3033,30 @@ impl EditorWindow {
cx,
)),
)
+ .children((settings.position == "manual").then(|| {
+ div()
+ .flex()
+ .flex_col()
+ .gap(px(12.))
+ .child(
+ self.slider_field(
+ "Horizontal Position",
+ SliderKey::Caption(CaptionSlider::ManualX),
+ "%",
+ cx,
+ )
+ .into_any_element(),
+ )
+ .child(
+ self.slider_field(
+ "Vertical Position",
+ SliderKey::Caption(CaptionSlider::ManualY),
+ "%",
+ cx,
+ )
+ .into_any_element(),
+ )
+ }))
.child(
ui::Field::section(&theme, "Animation").child(
div()
@@ -3477,6 +3687,85 @@ impl EditorWindow {
mod tests {
use super::*;
+ /// A left-aligned caption sits a margin in from the edge, its centre a
+ /// half box width further in. Manual starts at that drawn centre, so
+ /// switching does not move it.
+ #[test]
+ fn manual_caption_position_starts_at_the_drawn_caption() {
+ let mut settings = CaptionSettings {
+ position: "bottom-left".into(),
+ ..CaptionSettings::default()
+ };
+ set_caption_position_from(&mut settings, "manual", |position| {
+ (position == "bottom-left").then_some((0.16, 0.86))
+ });
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.16, 0.86))
+ );
+
+ // Nothing drawn yet: the preset's table entry.
+ let mut settings = CaptionSettings {
+ position: "top-right".into(),
+ ..CaptionSettings::default()
+ };
+ set_caption_position_from(&mut settings, "manual", |_| None);
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.95, 0.08))
+ );
+ }
+
+ /// A 0.6.1 report: with the caption position set to Manual, the captions
+ /// stayed bottom-centre and nothing could move them. Manual now starts
+ /// where the preset drew them and the sliders move it.
+ #[test]
+ fn manual_caption_position_starts_at_the_preset_and_moves() {
+ let mut settings = CaptionSettings {
+ position: "top-center".into(),
+ ..CaptionSettings::default()
+ };
+ set_caption_position_from(&mut settings, "manual", |_| None);
+ assert_eq!(settings.position, "manual");
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.5, 0.08))
+ );
+
+ set_caption_manual_center(&mut settings, Some(20.), None);
+ set_caption_manual_center(&mut settings, None, Some(130.));
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.2, 1.0))
+ );
+ let mut project = ProjectConfiguration::default();
+ project.captions = Some(CaptionsData {
+ settings: settings.clone(),
+ ..CaptionsData::default()
+ });
+ assert_eq!(CaptionSlider::ManualX.read(&project), 20.);
+ assert_eq!(CaptionSlider::ManualY.read(&project), 100.);
+
+ // Picking Manual again keeps the adjusted spot.
+ set_caption_position(&mut settings, "manual");
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.2, 1.0))
+ );
+
+ // A slider on a preset switches it to Manual from the preset's spot.
+ let mut preset = CaptionSettings {
+ position: "bottom-right".into(),
+ ..CaptionSettings::default()
+ };
+ set_caption_manual_center(&mut preset, None, Some(50.));
+ assert_eq!(preset.position, "manual");
+ assert_eq!(
+ preset.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.95, 0.5))
+ );
+ }
+
#[test]
fn every_tab_slider_has_the_range_its_call_site_declares() {
assert_eq!(CameraSlider::Size.limits(), (20., 80., 0.1));
@@ -3584,4 +3873,642 @@ mod tests {
// The rest of the defaults come along, not just the written key.
assert_eq!(keyboard_settings(&project).position, "bottom-center");
}
+
+ fn slider_model_project(timeline_segments: usize) -> ProjectConfiguration {
+ let segments: Vec<serde_json::Value> = (0..timeline_segments)
+ .map(|index| {
+ serde_json::json!({
+ "recordingSegment": 0,
+ "timescale": 1.0,
+ "start": index as f64,
+ "end": index as f64 + 1.0,
+ })
+ })
+ .collect();
+ let config = serde_json::json!({
+ "timeline": {
+ "segments": segments,
+ "transitions": [
+ {"segmentIndex": 1, "type": "cross-fade", "duration": 0.4}
+ ],
+ "zoomSegments": [
+ {"start": 0.0, "end": 1.0, "amount": 1.5, "mode": "auto"}
+ ],
+ "sceneSegments": [{"start": 0.0, "end": 1.0}],
+ "maskSegments": [{
+ "start": 0.0,
+ "end": 1.0,
+ "track": 0,
+ "maskType": "sensitive",
+ "center": {"x": 0.5, "y": 0.5},
+ "size": {"x": 0.2, "y": 0.2}
+ }],
+ "textSegments": [{
+ "start": 0.0,
+ "end": 1.0,
+ "track": 0,
+ "content": "title"
+ }],
+ "captionSegments": [{
+ "id": "caption",
+ "start": 0.0,
+ "end": 1.0,
+ "text": "hi"
+ }],
+ "keyboardSegments": [{
+ "id": "keyboard",
+ "start": 0.0,
+ "end": 1.0,
+ "displayText": "A"
+ }],
+ "audioSegments": [{
+ "start": 0.0,
+ "end": 1.0,
+ "track": 0,
+ "path": "/tmp/none.mp3"
+ }],
+ "styleSegments": [{
+ "start": 0.0,
+ "end": 1.0,
+ "track": 0,
+ "enabled": true,
+ "name": "Style",
+ "overrides": {}
+ }],
+ "imageSegments": [{
+ "start": 0.0,
+ "end": 1.0,
+ "track": 0,
+ "enabled": true,
+ "path": "/tmp/none.png"
+ }],
+ "camera3dSegments": [{"start": 0.0, "end": 1.0}]
+ },
+ "captions": {"settings": {"enabled": true}, "segments": []},
+ "keyboard": {"settings": {"enabled": true}}
+ });
+ serde_json::from_value(config).expect("the slider fixture parses")
+ }
+
+ fn animated_gradient_project() -> ProjectConfiguration {
+ let mut project = slider_model_project(64);
+ project.background.source = cap_project::BackgroundSource::AnimatedGradient {
+ config: cap_project::AnimatedGradientConfig::default(),
+ };
+ project
+ }
+
+ fn gradient_source() -> cap_project::BackgroundSource {
+ cap_project::BackgroundSource::Gradient {
+ from: [0, 0, 0],
+ to: [255, 255, 255],
+ angle: 90,
+ noise_intensity: Some(0.2),
+ noise_scale: Some(1.0),
+ animated: Some(false),
+ animation_speed: Some(1.0),
+ }
+ }
+
+ macro_rules! slider_case {
+ ($name:literal, |$project:ident| $body:block) => {
+ (
+ $name,
+ (|$project: &mut ProjectConfiguration| $body) as fn(&mut ProjectConfiguration),
+ )
+ };
+ ($name:literal, |$project:ident| $body:expr) => {
+ (
+ $name,
+ (|$project: &mut ProjectConfiguration| $body) as fn(&mut ProjectConfiguration),
+ )
+ };
+ }
+
+ fn slider_change_cases() -> Vec<(&'static str, fn(&mut ProjectConfiguration))> {
+ vec![
+ slider_case!("background.blur", |project| project.background.blur = 42.),
+ slider_case!("background.padding", |project| {
+ project.background.padding = 12.;
+ project.background.source = cap_project::BackgroundSource::Color {
+ value: [255, 255, 255],
+ alpha: 255,
+ };
+ }),
+ slider_case!("background.rounding", |project| {
+ project.background.rounding = 18.;
+ project.background.source = cap_project::BackgroundSource::Color {
+ value: [0, 0, 0],
+ alpha: 255,
+ };
+ }),
+ slider_case!("background.motion_blur", |project| {
+ project.cursor.motion_blur = 0.5;
+ project.screen_motion_blur = 0.5;
+ }),
+ slider_case!("background.border.width", |project| {
+ let mut border = project.background.border.clone().unwrap_or_default();
+ border.width = 8.;
+ project.background.border = Some(border);
+ }),
+ slider_case!("background.border.opacity", |project| {
+ let mut border = project.background.border.clone().unwrap_or_default();
+ border.opacity = 60.;
+ project.background.border = Some(border);
+ }),
+ slider_case!("background.notch.width", |project| {
+ let mut notch = project.background.notch.unwrap_or_default();
+ notch.enabled = true;
+ notch.width = Some(0.2);
+ project.background.notch = Some(notch);
+ }),
+ slider_case!("background.notch.height", |project| {
+ let mut notch = project.background.notch.unwrap_or_default();
+ notch.enabled = true;
+ notch.height = Some(0.05);
+ project.background.notch = Some(notch);
+ }),
+ slider_case!("background.notch.x", |project| {
+ let mut notch = project.background.notch.unwrap_or_default();
+ notch.enabled = true;
+ notch.x = Some(0.25);
+ project.background.notch = Some(notch);
+ }),
+ slider_case!("background.shadow", |project| project.background.shadow =
+ 22.),
+ slider_case!("background.advanced_shadow.size", |project| {
+ project.background.advanced_shadow = Some(ShadowConfiguration {
+ size: 22.,
+ opacity: 50.,
+ blur: 4.,
+ });
+ }),
+ slider_case!("background.advanced_shadow.opacity", |project| {
+ project.background.advanced_shadow = Some(ShadowConfiguration {
+ size: 20.,
+ opacity: 80.,
+ blur: 4.,
+ });
+ }),
+ slider_case!("background.advanced_shadow.blur", |project| {
+ project.background.advanced_shadow = Some(ShadowConfiguration {
+ size: 20.,
+ opacity: 50.,
+ blur: 12.,
+ });
+ }),
+ slider_case!("background.gradient.angle", |project| {
+ project.background.source = gradient_source();
+ if let cap_project::BackgroundSource::Gradient { angle, .. } =
+ &mut project.background.source
+ {
+ *angle = 45;
+ }
+ }),
+ slider_case!("background.gradient.noise_intensity", |project| {
+ project.background.source = gradient_source();
+ if let cap_project::BackgroundSource::Gradient {
+ noise_intensity, ..
+ } = &mut project.background.source
+ {
+ *noise_intensity = Some(0.8);
+ }
+ }),
+ slider_case!("background.gradient.noise_scale", |project| {
+ project.background.source = gradient_source();
+ if let cap_project::BackgroundSource::Gradient { noise_scale, .. } =
+ &mut project.background.source
+ {
+ *noise_scale = Some(2.0);
+ }
+ }),
+ slider_case!("camera.size", |project| project.camera.size = 55.),
+ slider_case!("camera.zoom_size", |project| project
+ .camera
+ .scale_during_zoom = 1.5),
+ slider_case!("camera.rounding", |project| project.camera.rounding = 18.),
+ slider_case!("camera.shadow", |project| project.camera.shadow = 30.),
+ slider_case!("camera.advanced_shadow.size", |project| {
+ project.camera.advanced_shadow = Some(ShadowConfiguration {
+ size: 22.,
+ opacity: 50.,
+ blur: 4.,
+ });
+ }),
+ slider_case!("camera.advanced_shadow.opacity", |project| {
+ project.camera.advanced_shadow = Some(ShadowConfiguration {
+ size: 20.,
+ opacity: 80.,
+ blur: 4.,
+ });
+ }),
+ slider_case!("camera.advanced_shadow.blur", |project| {
+ project.camera.advanced_shadow = Some(ShadowConfiguration {
+ size: 20.,
+ opacity: 50.,
+ blur: 12.,
+ });
+ }),
+ slider_case!("cursor.size", |project| project.cursor.size = 2),
+ slider_case!("cursor.rotation_amount", |project| project
+ .cursor
+ .rotation_amount =
+ 15.),
+ slider_case!("cursor.hide_when_idle_delay", |project| project
+ .cursor
+ .hide_when_idle_delay =
+ 2.5),
+ slider_case!("cursor.tension", |project| project.cursor.tension = 45.),
+ slider_case!("cursor.friction", |project| project.cursor.friction = 45.),
+ slider_case!("cursor.mass", |project| project.cursor.mass = 1.5),
+ slider_case!("cursor.animation_style", |project| project
+ .cursor
+ .animation_style =
+ CursorAnimationStyle::Custom),
+ slider_case!("cursor.ripple.strength", |project| project
+ .cursor
+ .ripple
+ .strength = 45.),
+ slider_case!("cursor.ripple.size", |project| project.cursor.ripple.size =
+ 45.),
+ slider_case!("cursor.ripple.duration", |project| project
+ .cursor
+ .ripple
+ .duration = 900.),
+ slider_case!("caption.size", |project| project
+ .captions
+ .as_mut()
+ .unwrap()
+ .settings
+ .size = 72),
+ slider_case!("caption.background_opacity", |project| project
+ .captions
+ .as_mut()
+ .unwrap()
+ .settings
+ .background_opacity =
+ 80),
+ slider_case!("caption.fade_duration", |project| project
+ .captions
+ .as_mut()
+ .unwrap()
+ .settings
+ .fade_duration = 0.5),
+ slider_case!("caption.preset", |project| project
+ .captions
+ .as_mut()
+ .unwrap()
+ .settings
+ .preset = "custom".into()),
+ slider_case!("keyboard.size", |project| project
+ .keyboard
+ .as_mut()
+ .unwrap()
+ .settings
+ .size = 72),
+ slider_case!("keyboard.background_opacity", |project| project
+ .keyboard
+ .as_mut()
+ .unwrap()
+ .settings
+ .background_opacity =
+ 80),
+ slider_case!("keyboard.fade_duration", |project| project
+ .keyboard
+ .as_mut()
+ .unwrap()
+ .settings
+ .fade_duration = 0.5),
+ slider_case!("keyboard.linger_duration", |project| project
+ .keyboard
+ .as_mut()
+ .unwrap()
+ .settings
+ .linger_duration =
+ 1.5),
+ slider_case!("keyboard.grouping_threshold_ms", |project| project
+ .keyboard
+ .as_mut()
+ .unwrap()
+ .settings
+ .grouping_threshold_ms =
+ 750.),
+ ]
+ }
+ fn assert_timeline_model_unchanged_in(
+ mut project: ProjectConfiguration,
+ name: &str,
+ change: impl FnOnce(&mut ProjectConfiguration),
+ ) {
+ use crate::editor_timeline::{TimelineModel, TrackLanes};
+
+ let lanes_before = TrackLanes::from_project(&project, true);
+ let before = TimelineModel::build_with_lanes(&project, true, true, &lanes_before);
+ change(&mut project);
+ let lanes_after = TrackLanes::from_project(&project, true);
+ assert_eq!(
+ lanes_before, lanes_after,
+ "{name} must not change the track lanes"
+ );
+ let after = TimelineModel::build_with_lanes(&project, true, true, &lanes_after);
+ assert_eq!(
+ format!("{before:?}"),
+ format!("{after:?}"),
+ "{name} must not change the timeline model"
+ );
+ }
+
+ fn assert_timeline_model_unchanged(name: &str, change: impl FnOnce(&mut ProjectConfiguration)) {
+ assert_timeline_model_unchanged_in(slider_model_project(64), name, change);
+ }
+
+ fn assert_timeline_model_changes_in(
+ mut project: ProjectConfiguration,
+ name: &str,
+ change: impl FnOnce(&mut ProjectConfiguration),
+ ) {
+ use crate::editor_timeline::TimelineModel;
+
+ let before = TimelineModel::build(&project, true, true);
+ change(&mut project);
+ let after = TimelineModel::build(&project, true, true);
+ assert_ne!(
+ format!("{before:?}"),
+ format!("{after:?}"),
+ "{name} must change the timeline model"
+ );
+ }
+
+ fn assert_timeline_model_changes(name: &str, change: impl FnOnce(&mut ProjectConfiguration)) {
+ assert_timeline_model_changes_in(slider_model_project(64), name, change);
+ }
+
+ #[test]
+ fn slider_edits_do_not_change_the_timeline_model() {
+ for (name, change) in slider_change_cases() {
+ assert_timeline_model_unchanged(name, change);
+ }
+
+ for target in [GradeTarget::Screen, GradeTarget::Camera] {
+ for slider in crate::editor_color::GradeSlider::ADJUST
+ .iter()
+ .copied()
+ .chain(std::iter::once(crate::editor_color::GradeSlider::Grain))
+ {
+ let target_name = match target {
+ GradeTarget::Screen => "screen",
+ GradeTarget::Camera => "camera",
+ };
+ let name = format!("grade.{target_name}.{}", slider.label());
+ assert_timeline_model_unchanged(&name, move |project| {
+ let grade = match target {
+ GradeTarget::Screen => &mut project.color_correction.screen,
+ GradeTarget::Camera => &mut project.color_correction.camera,
+ };
+ slider.write(grade, 0.5);
+ grade.preset = "custom".into();
+ });
+ }
+ }
+
+ for parameter in cap_project::AnimatedGradientParameter::ALL {
+ let name = format!("animated_gradient.{}", parameter.control().label);
+ assert_timeline_model_unchanged_in(
+ animated_gradient_project(),
+ &name,
+ move |project| {
+ let current = {
+ let cap_project::BackgroundSource::AnimatedGradient { config } =
+ &project.background.source
+ else {
+ panic!("the fixture is an animated gradient");
+ };
+ parameter.get(config)
+ };
+ let control = parameter.control();
+ let next = if current + control.step <= control.max {
+ current + control.step
+ } else {
+ control.min + control.step
+ };
+ let cap_project::BackgroundSource::AnimatedGradient { config } =
+ &mut project.background.source
+ else {
+ panic!("the fixture is an animated gradient");
+ };
+ parameter.set(config, next);
+ },
+ );
+ }
+
+ assert_timeline_model_unchanged_in(
+ animated_gradient_project(),
+ "animated_gradient.stop_position",
+ |project| {
+ let cap_project::BackgroundSource::AnimatedGradient { config } =
+ &mut project.background.source
+ else {
+ panic!("the fixture is an animated gradient");
+ };
+ config.color_stops[0].position = 75.0;
+ },
+ );
+ }
+
+ #[test]
+ fn audio_levels_still_change_the_timeline_model() {
+ assert_timeline_model_changes("audio.mic_volume_db", |project| {
+ project.audio.mic_volume_db = 1.5;
+ });
+ assert_timeline_model_changes("audio.system_volume_db", |project| {
+ project.audio.system_volume_db = 1.5;
+ });
+ let mut camera_project = slider_model_project(64);
+ camera_project
+ .timeline
+ .as_mut()
+ .unwrap()
+ .scene_segments
+ .clear();
+ assert_timeline_model_changes_in(camera_project, "camera.hide", |project| {
+ project.camera.hide = true;
+ });
+ assert_timeline_model_changes("captions.settings.enabled", |project| {
+ project.captions.as_mut().unwrap().settings.enabled = false;
+ });
+ assert_timeline_model_changes("keyboard.settings.enabled", |project| {
+ project.keyboard.as_mut().unwrap().settings.enabled = false;
+ });
+ }
+
+ /// Pin every variant's classification, not just the project fields the
+ /// slider happens to write. A new variant still fails to compile until it
+ /// picks a side; this catches flipping an existing variant without
+ /// updating the routing tests.
+ #[test]
+ fn slider_classification_is_pinned_per_variant() {
+ use crate::editor_color::GradeSlider;
+ use crate::editor_sidebar::BgSlider;
+
+ for slider in [
+ BgSlider::Blur,
+ BgSlider::Padding,
+ BgSlider::Rounding,
+ BgSlider::MotionBlur,
+ BgSlider::BorderWidth,
+ BgSlider::BorderOpacity,
+ BgSlider::NotchWidth,
+ BgSlider::NotchHeight,
+ BgSlider::NotchX,
+ BgSlider::Shadow,
+ BgSlider::ShadowSize,
+ BgSlider::ShadowOpacity,
+ BgSlider::ShadowBlur,
+ BgSlider::GradientAngle,
+ BgSlider::GradientNoise,
+ BgSlider::GradientGrain,
+ ] {
+ assert!(
+ !slider.changes_timeline_model(),
+ "background {slider:?} must not rebuild the timeline"
+ );
+ }
+
+ for slider in [
+ CameraSlider::Size,
+ CameraSlider::ZoomSize,
+ CameraSlider::Rounding,
+ CameraSlider::Shadow,
+ CameraSlider::ShadowSize,
+ CameraSlider::ShadowOpacity,
+ CameraSlider::ShadowBlur,
+ ] {
+ assert!(
+ !slider.changes_timeline_model(),
+ "camera {slider:?} must not rebuild the timeline"
+ );
+ }
+
+ for slider in [AudioSlider::MicVolume, AudioSlider::SystemVolume] {
+ assert!(
+ slider.changes_timeline_model(),
+ "audio {slider:?} must rebuild the timeline"
+ );
+ }
+
+ for slider in [
+ CursorSlider::Size,
+ CursorSlider::Tilt,
+ CursorSlider::IdleDelay,
+ CursorSlider::Tension,
+ CursorSlider::Friction,
+ CursorSlider::Mass,
+ CursorSlider::RippleStrength,
+ CursorSlider::RippleSize,
+ CursorSlider::RippleDuration,
+ ] {
+ assert!(
+ !slider.changes_timeline_model(),
+ "cursor {slider:?} must not rebuild the timeline"
+ );
+ }
+
+ for slider in [
+ CaptionSlider::Size,
+ CaptionSlider::BackgroundOpacity,
+ CaptionSlider::FadeDuration,
+ CaptionSlider::ManualX,
+ CaptionSlider::ManualY,
+ ] {
+ assert!(
+ !slider.changes_timeline_model(),
+ "caption {slider:?} must not rebuild the timeline"
+ );
+ }
+
+ for slider in [
+ KeyboardSlider::Size,
+ KeyboardSlider::BackgroundOpacity,
+ KeyboardSlider::FadeDuration,
+ KeyboardSlider::LingerDuration,
+ KeyboardSlider::GroupingThreshold,
+ ] {
+ assert!(
+ !slider.changes_timeline_model(),
+ "keyboard {slider:?} must not rebuild the timeline"
+ );
+ }
+
+ for slider in [
+ GradeSlider::Grain,
+ GradeSlider::Intensity,
+ GradeSlider::Exposure,
+ GradeSlider::Contrast,
+ GradeSlider::Saturation,
+ GradeSlider::Temperature,
+ GradeSlider::Tint,
+ GradeSlider::Fade,
+ GradeSlider::SplitTone,
+ GradeSlider::Vignette,
+ ] {
+ assert!(
+ !slider.changes_timeline_model(),
+ "grade {slider:?} must not rebuild the timeline"
+ );
+ }
+ }
+
+ /// Times the rebuild the new route skips; the rest of `project_changed`
+ /// (history, publish, save, notify) is unchanged and not measured here.
+ ///
+ /// The fixture is a synthetic stress case: 2,000 contiguous clips plus
+ /// every track kind, not a typical project. Quote these numbers only as
+ /// the cost of the skipped `TimelineModel::build_with_lanes`.
+ ///
+ /// `cargo test --release --manifest-path apps/desktop-gpui/Cargo.toml --bin cap-gpui -- --ignored benchmark_slider_edit_skips_timeline_rebuild --nocapture`
+ #[test]
+ #[ignore = "manual slider edit benchmark"]
+ fn benchmark_slider_edit_skips_timeline_rebuild() {
+ use std::{hint::black_box, time::Instant};
+
+ fn median(samples: &mut [f64]) -> f64 {
+ assert!(samples.len() > 1, "median needs more than one sample");
+ samples.sort_by(f64::total_cmp);
+ let middle = samples.len() / 2;
+ if samples.len() % 2 == 0 {
+ (samples[middle - 1] + samples[middle]) / 2.
+ } else {
+ samples[middle]
+ }
+ }
+
+ use crate::editor_timeline::{TimelineModel, TrackLanes};
+
+ let project = slider_model_project(2_000);
+ let lanes = TrackLanes::from_project(&project, true);
+
+ // Warm caches and the allocator before sampling.
+ for _ in 0..3 {
+ drop(black_box(TimelineModel::build_with_lanes(
+ &project, true, true, &lanes,
+ )));
+ }
+
+ let mut samples = Vec::new();
+ for _ in 0..10 {
+ let started = Instant::now();
+ for _ in 0..10 {
+ drop(black_box(TimelineModel::build_with_lanes(
+ &project, true, true, &lanes,
+ )));
+ }
+ samples.push(started.elapsed().as_secs_f64() * 1_000_000. / 10.);
+ }
+
+ let skipped = median(&mut samples);
+ println!(
+ "slider_edit timeline_segments=2000 samples=10 iterations_per_sample=10 \
+ skipped_rebuild_us={skipped:.3}"
+ );
+ }
}
diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs
index ecbc4b425..046a18b1f 100644
--- a/apps/desktop-gpui/src/editor_window.rs
+++ b/apps/desktop-gpui/src/editor_window.rs
@@ -49,6 +49,7 @@ use std::{
use cap_cursor_info::CursorFamily;
use cap_editor::{EditorFrameOutput, EditorInstance, EditorState};
+use cap_project::caption_words::caption_text_from_words;
use cap_project::{
ClipSpeedAudioMode, Cursors, ProjectConfiguration, RecordingMeta, RecordingMetaInner,
StudioRecordingMeta, TimelineConfiguration, XY,
@@ -80,6 +81,7 @@ use crate::{
mod frame;
mod loading;
mod scenes;
+mod snap;
// ---------------------------------------------------------------------------
// Window geometry
@@ -288,6 +290,11 @@ fn frame_seconds(frame: u32) -> f64 {
frame as f64 / EDITOR_PREVIEW_FPS as f64
}
+/// The new end of a timeline that an edit shortened past the playhead.
+fn playhead_past_end(playhead: f64, total: f64) -> Option<f64> {
+ (total > 0.0 && playhead > total).then_some(total)
+}
+
/// 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.
@@ -1458,7 +1465,7 @@ pub struct EditorWindow {
minimap_drag: Option<timeline::MinimapDrag>,
/// Magnetic edges for the live drag: playhead, clip cuts and every other
/// segment's edges, gathered once when the drag arms.
- drag_snap_targets: Vec<f64>,
+ drag_snap_targets: snap::SnapTargets,
/// The target the drag is currently snapped onto, for the guide line.
drag_snap_time: Option<f64>,
/// Blip-style ghost trim: while a clip handle drags, only this draft
@@ -2099,7 +2106,7 @@ impl EditorWindow {
clip_speed: None,
timeline_scroll: gpui::ScrollHandle::new(),
minimap_drag: None,
- drag_snap_targets: Vec::new(),
+ drag_snap_targets: snap::SnapTargets::default(),
drag_snap_time: None,
clip_draft: None,
clip_anim: None,
@@ -2291,6 +2298,12 @@ impl EditorWindow {
self.timeline.waveform_paths = waveform_paths;
if self.timeline.total_duration > 0.0 {
self.total = self.timeline.total_duration;
+ // Deleting or trimming the clip under the playhead shortens the
+ // timeline past it; bring the playhead back to the new end the way
+ // `setPlayhead` clamps it (`ED/context.ts:2424`).
+ if let Some(end) = playhead_past_end(self.playhead, self.total) {
+ self.apply_seek(end);
+ }
}
}
@@ -2428,14 +2441,27 @@ impl EditorWindow {
self.project_changed_inner(true, window, cx);
}
- /// [`Self::project_changed`] for edits that only write `background.frame`.
+ /// [`Self::project_changed`] for edits that cannot change the drawn
+ /// timeline model.
+ ///
+ /// [`crate::editor_timeline::TimelineModel::build`] reads segment arrays,
+ /// track enabled flags, overlay order, audio levels, and whether a camera
+ /// exists; [`crate::editor_timeline::TrackLanes::from_project`] reads the
+ /// same segment and track state. Neither reads the frame, background
+ /// styling, camera geometry, cursor styling, caption/keyboard styling, or
+ /// color correction, so edits to those fields can skip the rebuild.
///
- /// [`crate::editor_timeline::TimelineModel::build`] never reads the frame
- /// or the background (the style track draws a segment's name and enabled
- /// flag only), so a frame edit can skip the rebuild. The frame text fields
- /// call this once per keystroke; any edit that touches a field the timeline
- /// model reads must go through [`Self::project_changed`] instead.
- fn project_changed_without_timeline(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ /// The frame text fields, the classified background/camera/cursor/
+ /// caption/keyboard sliders, color-grade sliders, and animated-gradient
+ /// parameter/stop/add/remove/reset edits call this. Audio levels, clip
+ /// volume, panel sliders, animated-gradient preset selection, and any edit
+ /// that touches a field the timeline model or lanes read still go through
+ /// [`Self::project_changed`].
+ pub(crate) fn project_changed_without_timeline(
+ &mut self,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
self.project_changed_inner(false, window, cx);
}
@@ -3448,6 +3474,15 @@ impl EditorWindow {
if !self.visual_ready() {
return;
}
+ self.on_ready_key(event, window, cx);
+ }
+
+ fn on_ready_key(
+ &mut self,
+ event: &gpui::KeyDownEvent,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
if self.frame_controls.is_open() && event.keystroke.key == "escape" {
self.close_frame_controls(window, cx);
cx.stop_propagation();
@@ -3670,6 +3705,33 @@ impl EditorWindow {
return;
}
match keystroke.key.as_str() {
+ "up" | "down" | "home" | "end" => {
+ if keystroke.modifiers.function
+ || self.crop.is_some()
+ || self.canvas_selection.is_some()
+ || self.sidebar.menu.is_some()
+ || self.toolbar_menu.is_some()
+ || self.text_menu.is_some()
+ || self.add_track.is_some()
+ || self.clip_speed.is_some()
+ || self.sidebar.color_picker.is_some()
+ || self.presets_menu.is_some()
+ || self.preset_dialog.is_some()
+ || self.clips.has_open_menu()
+ || self.audio_picker.is_some()
+ || self.camera3d_picker
+ || self.frame_controls.is_open()
+ {
+ return;
+ }
+ cx.stop_propagation();
+ self.view.preview_time = None;
+ self.scene_preview_time = None;
+ match keystroke.key.as_str() {
+ "up" | "home" => self.jump_to_start(cx),
+ _ => self.jump_to_end(cx),
+ }
+ }
// `e.code === "Backspace" || (e.code === "Delete" &&
// hasNoModifiers)` (`TL/index.tsx:963`). gpui reports the main
// delete key as `backspace` and forward-delete as `delete`, which
@@ -4801,7 +4863,7 @@ impl EditorWindow {
targets.push(segment.end);
}
}
- self.drag_snap_targets = targets;
+ self.drag_snap_targets = snap::SnapTargets::new(targets);
self.drag_snap_time = None;
}
@@ -4816,14 +4878,7 @@ impl EditorWindow {
return (value, None);
}
let radius = DRAG_SNAP_PX * secs_per_pixel;
- let mut best: Option<f64> = None;
- for &target in &self.drag_snap_targets {
- let distance = (target - value).abs();
- if distance <= radius && best.is_none_or(|b| (b - value).abs() > distance) {
- best = Some(target);
- }
- }
- match best {
+ match self.drag_snap_targets.nearest(value, radius) {
Some(target) => (target, Some(target)),
None => (value, None),
}
@@ -4868,22 +4923,14 @@ impl EditorWindow {
let mut engaged = None;
if !snap_disabled {
let radius = DRAG_SNAP_PX * secs_per_pixel;
- let mut best = f64::INFINITY;
- for &target in &self.drag_snap_targets {
- for edge in [start + raw, end + raw] {
- let adjust = target - edge;
- if adjust.abs() <= radius && adjust.abs() < best.abs() {
- best = adjust;
- engaged = Some(target);
- }
- }
- }
- if best.is_finite() {
- let snapped = bounds.clamp(raw + best);
- if (snapped - (raw + best)).abs() < 1e-9 {
+ if let Some((adjustment, target)) =
+ self.drag_snap_targets
+ .move_adjustment(start + raw, end + raw, radius)
+ {
+ let snapped = bounds.clamp(raw + adjustment);
+ if (snapped - (raw + adjustment)).abs() < 1e-9 {
shift = snapped;
- } else {
- engaged = None;
+ engaged = Some(target);
}
}
}
@@ -5300,44 +5347,6 @@ pub(crate) struct EditorNotice {
/// How long a notice stays up.
const EDITOR_NOTICE_DURATION: Duration = Duration::from_millis(2500);
-fn caption_text_from_words(words: &[cap_project::CaptionWord]) -> String {
- let mut text = String::new();
- for word in words {
- let value = word.text.trim();
- if value.is_empty() {
- continue;
- }
- let attaches = value.chars().next().is_some_and(|value| {
- matches!(
- value,
- ',' | '.'
- | '!'
- | '?'
- | ';'
- | ':'
- | '%'
- | ')'
- | ']'
- | '}'
- | '\''
- | '’'
- | '、'
- | '。'
- | '!'
- | '?'
- | ';'
- | ':'
- | ','
- )
- });
- if !text.is_empty() && !attaches {
- text.push(' ');
- }
- text.push_str(value);
- }
- text
-}
-
fn fresh_caption_source_id(project: &ProjectConfiguration) -> String {
use std::hash::{BuildHasher, Hasher};
@@ -5902,7 +5911,7 @@ impl EditorWindow {
/// select instead, which also moves the playhead
/// (`props.handleUpdatePlayhead(e)`).
fn drag_mouse_up(&mut self, window: &mut Window, cx: &mut Context<Self>) {
- self.drag_snap_targets = Vec::new();
+ self.drag_snap_targets = snap::SnapTargets::default();
self.drag_snap_time = None;
let Some(drag) = self.drag.take() else {
self.clip_draft = None;
@@ -7084,10 +7093,7 @@ impl EditorWindow {
fn open_recording_bundle(&mut self, cx: &mut Context<Self>) {
self.set_selection(None, cx);
- crate::library::open_recording_folder(
- &self.project_path,
- crate::library::RecordingMode::Studio,
- );
+ crate::library::reveal_path(&self.project_path, cx);
}
fn delete_recording(&mut self, window: &mut Window, cx: &mut Context<Self>) {
@@ -12256,6 +12262,244 @@ fn hex_to_color(rgba: [u8; 4]) -> cap_project::Color {
mod tests {
use super::*;
+ /// A 0.6.1 log: the user deleted the clip under the playhead, the
+ /// timeline shrank to before it, and every preview request past the new
+ /// end found no segment. The playhead goes back to the end instead.
+ #[test]
+ fn a_shortened_timeline_brings_the_playhead_back_to_its_end() {
+ assert_eq!(playhead_past_end(10.0, 4.0), Some(4.0));
+ assert_eq!(playhead_past_end(2.0, 4.0), None);
+ assert_eq!(playhead_past_end(4.0, 4.0), None);
+ assert_eq!(playhead_past_end(3.0, 0.0), None);
+ }
+
+ #[cfg(target_os = "linux")]
+ mod boundary_navigation {
+ use super::*;
+
+ struct KeyboardView {
+ editor: Entity<EditorWindow>,
+ check_readiness: bool,
+ _project: tempfile::TempDir,
+ }
+
+ impl Render for KeyboardView {
+ fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
+ let theme = Theme::for_window(window, cx, false);
+ let check_readiness = self.check_readiness;
+ self.editor.update(cx, |editor, cx| {
+ div()
+ .size_full()
+ .track_focus(&editor.focus)
+ .on_key_down(cx.listener(move |editor, event, window, cx| {
+ if check_readiness {
+ editor.on_key(event, window, cx);
+ } else {
+ editor.on_ready_key(event, window, cx);
+ }
+ }))
+ .on_key_up(cx.listener(|editor, event: &gpui::KeyUpEvent, _, cx| {
+ editor.crop_key_up(event, cx);
+ }))
+ .child(ui::TextInput::plain(&theme, "name", &editor.name_input))
+ })
+ }
+ }
+
+ // The ready-handler fixture bypasses media loading; the gated fixture separately
+ // checks that a loading project cannot issue transport commands.
+ fn fixture(
+ cx: &mut gpui::TestAppContext,
+ check_readiness: bool,
+ ) -> (
+ Entity<EditorWindow>,
+ TransportDriver,
+ &mut gpui::VisualTestContext,
+ ) {
+ let project = tempfile::tempdir().unwrap();
+ let (handle, driver, _) = transport();
+ let (view, cx) = cx.add_window_view(move |window, cx| {
+ ui::bind_text_input_keys(cx);
+ let editor = cx.new(|cx| EditorWindow::new(project.path().into(), window, cx));
+ editor.update(cx, |editor, cx| {
+ editor.transport = Some(handle);
+ editor.total = 30.;
+ editor.name_input.update(cx, |input, cx| {
+ input.set_disabled(false, cx);
+ input.set_text("Recording", cx);
+ });
+ window.focus(&editor.focus, cx);
+ });
+ KeyboardView {
+ editor,
+ check_readiness,
+ _project: project,
+ }
+ });
+ let editor = view.read_with(cx, |view, _| view.editor.clone());
+ (editor, driver, cx)
+ }
+
+ fn desired(driver: &TransportDriver) -> Desired {
+ *driver.desired.lock().unwrap()
+ }
+
+ #[gpui::test]
+ fn boundary_keys_pause_and_seek_the_existing_transport(cx: &mut gpui::TestAppContext) {
+ let (editor, driver, cx) = fixture(cx, false);
+ for (key, seconds) in [("up", 0.), ("down", 30.), ("home", 0.), ("end", 30.)] {
+ editor.update(cx, |editor, _| {
+ editor.playing = true;
+ editor.view.playing = true;
+ editor.playhead = 12.;
+ editor.view.preview_time = Some(12.);
+ editor.scene_preview_time = Some(12.);
+ editor.transport.as_ref().unwrap().play_from(720);
+ });
+ let generation = desired(&driver).seek_gen;
+ cx.simulate_keystrokes(key);
+ let requested = desired(&driver);
+ assert_eq!(requested.seek, Some((seconds * 60.) as u32), "{key}");
+ assert_eq!(requested.seek_gen, generation + 1, "{key}");
+ assert!(!requested.playing, "{key}");
+ editor.read_with(cx, |editor, _| {
+ assert_eq!(editor.playhead, seconds, "{key}");
+ assert_eq!(editor.view.playhead, seconds, "{key}");
+ assert_eq!(editor.preview_or_playhead(), seconds, "{key}");
+ assert!(editor.view.preview_time.is_none(), "{key}");
+ assert!(editor.scene_preview_time.is_none(), "{key}");
+ assert!(!editor.playing && !editor.view.playing, "{key}");
+ });
+ }
+ }
+
+ #[gpui::test]
+ fn boundary_keys_preserve_input_menu_and_canvas_ownership(cx: &mut gpui::TestAppContext) {
+ let (editor, driver, cx) = fixture(cx, false);
+ let initial = desired(&driver);
+ let (input, focus) = editor.read_with(cx, |editor, cx| {
+ (
+ editor.name_input.read(cx).focus_handle(),
+ editor.focus.clone(),
+ )
+ });
+ cx.update(|window, cx| window.focus(&input, cx));
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+
+ cx.update(|window, cx| window.focus(&focus, cx));
+ editor.update(cx, |editor, _| {
+ editor.sidebar.menu = open_sidebar_menu_for_test(
+ crate::editor_tabs::SidebarMenu::BackgroundCornerStyle,
+ );
+ });
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+ editor.read_with(cx, |editor, _| {
+ assert_eq!(
+ editor.sidebar.menu.as_ref().unwrap().state.highlighted,
+ Some(1)
+ );
+ });
+
+ editor.update(cx, |editor, _| {
+ editor.sidebar.menu = None;
+ editor.canvas_selection = Some(crate::editor_canvas::CanvasSelection::Text(99));
+ });
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+
+ editor.update(cx, |editor, _| {
+ editor.canvas_selection = None;
+ editor.crop = Some(crate::editor_crop::CropState::new(
+ (640, 480),
+ (802., 602.),
+ crate::editor_crop::CROP_ZERO,
+ ));
+ });
+ cx.simulate_keystrokes("home end");
+ assert_eq!(desired(&driver), initial);
+
+ editor.update(cx, |editor, _| {
+ editor.crop = None;
+ editor.camera3d_picker = true;
+ });
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+ }
+
+ #[gpui::test]
+ fn boundary_keys_ignore_modifiers_and_repeats(cx: &mut gpui::TestAppContext) {
+ let (_, driver, cx) = fixture(cx, false);
+ let initial = desired(&driver);
+ cx.simulate_keystrokes("shift-up ctrl-down alt-home cmd-end");
+ for key in ["up", "down", "home", "end"] {
+ let keystroke = gpui::Keystroke::parse(key).unwrap();
+ cx.simulate_event(gpui::KeyDownEvent {
+ keystroke: keystroke.clone(),
+ is_held: true,
+ prefer_character_input: false,
+ });
+ let mut modified = keystroke;
+ modified.modifiers.function = true;
+ cx.simulate_event(gpui::KeyDownEvent {
+ keystroke: modified,
+ is_held: false,
+ prefer_character_input: false,
+ });
+ }
+ assert_eq!(desired(&driver), initial);
+ }
+
+ #[gpui::test]
+ fn boundary_keys_wait_for_a_ready_editor(cx: &mut gpui::TestAppContext) {
+ let (_, driver, cx) = fixture(cx, true);
+ let initial = desired(&driver);
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+ }
+ }
+
+ #[cfg(target_os = "linux")]
+ fn field_text(
+ editor: &Entity<EditorWindow>,
+ key: crate::editor_panels::FieldKey,
+ cx: &mut gpui::VisualTestContext,
+ ) -> String {
+ editor.read_with(cx, |editor, cx| {
+ editor.field(key).unwrap().read(cx).text().to_string()
+ })
+ }
+
+ #[cfg(target_os = "linux")]
+ fn set_caption_timeline(editor: &mut EditorWindow, captions: &[(&str, f64, f64)]) {
+ editor.project.timeline = Some(TimelineConfiguration {
+ style_segments: Vec::new(),
+ image_segments: Vec::new(),
+ segments: Vec::new(),
+ transitions: Vec::new(),
+ zoom_segments: Vec::new(),
+ scene_segments: Vec::new(),
+ mask_segments: Vec::new(),
+ text_segments: Vec::new(),
+ caption_segments: captions
+ .iter()
+ .map(|(id, start, end)| {
+ serde_json::from_value(serde_json::json!({
+ "id": id,
+ "start": start,
+ "end": end,
+ "text": format!("caption {id}"),
+ }))
+ .unwrap()
+ })
+ .collect(),
+ keyboard_segments: Vec::new(),
+ audio_segments: Vec::new(),
+ camera3d_segments: Vec::new(),
+ });
+ }
+
#[cfg(target_os = "linux")]
#[gpui::test]
#[ignore = "manual preset menu repaint benchmark"]
@@ -13281,6 +13525,238 @@ mod tests {
assert_eq!(text(&editor, FieldKey::CaptionStart(0), cx), "1.25");
}
+ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.
+ #[cfg(target_os = "linux")]
+ #[gpui::test]
+ fn visible_field_sync_leaves_hidden_fields_until_they_are_visible(
+ cx: &mut gpui::TestAppContext,
+ ) {
+ use crate::editor_panels::FieldKey;
+
+ 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));
+ let visible = [
+ FieldKey::CaptionText(0),
+ FieldKey::CaptionStart(0),
+ FieldKey::CaptionEnd(0),
+ ];
+ let hidden = [
+ FieldKey::CaptionText(1),
+ FieldKey::CaptionStart(1),
+ FieldKey::CaptionEnd(1),
+ ];
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_error("fields".into(), window, cx);
+ set_caption_timeline(editor, &[("first", 1.25, 3.5), ("second", 4., 6.)]);
+ for key in visible.into_iter().chain(hidden) {
+ editor.ensure_field(key, window, cx);
+ }
+ editor.sync_fields(window, cx);
+ });
+ assert_eq!(
+ hidden.map(|key| field_text(&editor, key, cx)),
+ ["caption second", "4.00", "6.00"]
+ );
+
+ // A hidden field is not touched by a sync that only lists the visible
+ // caption, even after its value moved.
+ editor.update_in(cx, |editor, window, cx| {
+ let segment = &mut editor.project.timeline.as_mut().unwrap().caption_segments[1];
+ segment.text = "caught up".into();
+ segment.start = 5.;
+ segment.end = 7.;
+ editor.sync_visible_fields(&visible, window, cx);
+ });
+ assert_eq!(
+ hidden.map(|key| field_text(&editor, key, cx)),
+ ["caption second", "4.00", "6.00"]
+ );
+
+ // Becoming visible lists it again, and it catches up then.
+ editor.update_in(cx, |editor, window, cx| {
+ editor.sync_visible_fields(&hidden, window, cx);
+ });
+ assert_eq!(
+ hidden.map(|key| field_text(&editor, key, cx)),
+ ["caught up", "5.00", "7.00"]
+ );
+
+ // The all-fields path still catches every hidden field up, which is
+ // the old behaviour the benchmark compares against.
+ editor.update_in(cx, |editor, window, cx| {
+ editor.project.timeline.as_mut().unwrap().caption_segments[1].text = "all".into();
+ editor.sync_fields(window, cx);
+ });
+ assert_eq!(field_text(&editor, FieldKey::CaptionText(1), cx), "all");
+ }
+
+ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.
+ #[cfg(target_os = "linux")]
+ #[gpui::test]
+ fn visible_field_sync_never_overwrites_a_focused_field(cx: &mut gpui::TestAppContext) {
+ use crate::editor_panels::FieldKey;
+
+ 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));
+ let visible = [
+ FieldKey::CaptionText(0),
+ FieldKey::CaptionStart(0),
+ FieldKey::CaptionEnd(0),
+ ];
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_error("fields".into(), window, cx);
+ set_caption_timeline(editor, &[("first", 1.25, 3.5)]);
+ for key in visible {
+ editor.ensure_field(key, window, cx);
+ }
+ editor.sync_fields(window, cx);
+ });
+ let start_focus = editor.read_with(cx, |editor, cx| {
+ editor
+ .field(FieldKey::CaptionStart(0))
+ .unwrap()
+ .read(cx)
+ .focus_handle()
+ });
+
+ // The visible caption moved, but its start box is being typed into.
+ editor.update_in(cx, |editor, window, cx| {
+ window.focus(&start_focus, cx);
+ let segment = &mut editor.project.timeline.as_mut().unwrap().caption_segments[0];
+ segment.start = 2.;
+ segment.text = "Bye".into();
+ editor.sync_visible_fields(&visible, window, cx);
+ });
+ assert_eq!(field_text(&editor, FieldKey::CaptionText(0), cx), "Bye");
+ assert_eq!(field_text(&editor, FieldKey::CaptionStart(0), cx), "1.25");
+ }
+
+ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.
+ #[cfg(target_os = "linux")]
+ #[gpui::test]
+ fn prepare_sidebar_fields_syncs_only_the_selected_caption_and_the_crop(
+ cx: &mut gpui::TestAppContext,
+ ) {
+ use crate::{
+ editor_crop::{CropBounds, CropField, CropState},
+ editor_panels::FieldKey,
+ };
+
+ 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));
+ let caption0 = [
+ FieldKey::CaptionText(0),
+ FieldKey::CaptionStart(0),
+ FieldKey::CaptionEnd(0),
+ ];
+ let caption1 = [
+ FieldKey::CaptionText(1),
+ FieldKey::CaptionStart(1),
+ FieldKey::CaptionEnd(1),
+ ];
+ let crop_fields = [
+ FieldKey::Crop(CropField::Width),
+ FieldKey::Crop(CropField::Height),
+ FieldKey::Crop(CropField::X),
+ FieldKey::Crop(CropField::Y),
+ ];
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_error("fields".into(), window, cx);
+ set_caption_timeline(editor, &[("first", 1.25, 3.5), ("second", 4., 6.)]);
+ // Both captions were selected at some point, so both sets of
+ // fields exist and start in sync.
+ for key in caption0.into_iter().chain(caption1) {
+ editor.ensure_field(key, window, cx);
+ }
+ editor.sync_fields(window, cx);
+ editor.crop = Some(CropState::new(
+ (1920, 1080),
+ (510., 331.),
+ CropBounds::new(96., 54., 960., 540.),
+ ));
+ editor.selection = Some(Selection::single(TrackKind::Caption, 0));
+ });
+ assert_eq!(
+ caption1.map(|key| field_text(&editor, key, cx)),
+ ["caption second", "4.00", "6.00"]
+ );
+
+ // Move both captions, then run the production prepare path. It draws
+ // caption 0 and the crop dialog, so caption 1 stays behind.
+ editor.update_in(cx, |editor, window, cx| {
+ let timeline = editor.project.timeline.as_mut().unwrap();
+ timeline.caption_segments[0].text = "visible".into();
+ timeline.caption_segments[0].start = 1.5;
+ timeline.caption_segments[0].end = 3.0;
+ timeline.caption_segments[1].text = "hidden".into();
+ editor.prepare_sidebar_fields(window, cx);
+ });
+ assert_eq!(field_text(&editor, FieldKey::CaptionText(0), cx), "visible");
+ assert_eq!(field_text(&editor, FieldKey::CaptionStart(0), cx), "1.50");
+ assert_eq!(field_text(&editor, FieldKey::CaptionEnd(0), cx), "3.00");
+ assert_eq!(
+ field_text(&editor, FieldKey::CaptionText(1), cx),
+ "caption second"
+ );
+
+ // The crop boxes are part of the visible set.
+ let crop = editor.read_with(cx, |editor, _| editor.crop.as_ref().unwrap().real());
+ assert_eq!(
+ crop_fields.map(|key| {
+ let FieldKey::Crop(_) = key else {
+ unreachable!()
+ };
+ field_text(&editor, key, cx)
+ }),
+ crop_fields.map(|key| {
+ let FieldKey::Crop(field) = key else {
+ unreachable!()
+ };
+ crate::ui::format_number(field.read(crop))
+ })
+ );
+
+ // Selecting caption 1 makes it visible, and it catches up.
+ editor.update_in(cx, |editor, window, cx| {
+ editor.selection = Some(Selection::single(TrackKind::Caption, 1));
+ editor.prepare_sidebar_fields(window, cx);
+ });
+ assert_eq!(field_text(&editor, FieldKey::CaptionText(1), cx), "hidden");
+
+ // The crop dialog is independent of the timeline: its four fields
+ // still sync through the early-return path.
+ editor.update_in(cx, |editor, window, cx| {
+ editor.project.timeline = None;
+ editor.crop = Some(CropState::new(
+ (1920, 1080),
+ (510., 331.),
+ CropBounds::new(10., 20., 320., 480.),
+ ));
+ editor.prepare_sidebar_fields(window, cx);
+ });
+ let crop = editor.read_with(cx, |editor, _| editor.crop.as_ref().unwrap().real());
+ assert_eq!(
+ crop_fields.map(|key| {
+ let FieldKey::Crop(_) = key else {
+ unreachable!()
+ };
+ field_text(&editor, key, cx)
+ }),
+ crop_fields.map(|key| {
+ let FieldKey::Crop(field) = key else {
+ unreachable!()
+ };
+ crate::ui::format_number(field.read(crop))
+ })
+ );
+ }
+
/// `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.
diff --git a/apps/desktop-gpui/src/editor_window/snap.rs b/apps/desktop-gpui/src/editor_window/snap.rs
new file mode 100644
index 000000000..78326a22e
--- /dev/null
+++ b/apps/desktop-gpui/src/editor_window/snap.rs
@@ -0,0 +1,360 @@
+const INDEX_MIN_TARGETS: usize = 32;
+
+#[derive(Default)]
+pub(super) struct SnapTargets {
+ targets: Vec<Target>,
+}
+
+struct Target {
+ time: f64,
+ order: usize,
+}
+
+impl SnapTargets {
+ pub(super) fn new(times: Vec<f64>) -> Self {
+ let mut targets: Vec<_> = times
+ .into_iter()
+ .enumerate()
+ .filter(|(_, time)| !time.is_nan())
+ .map(|(order, time)| Target { time, order })
+ .collect();
+ if targets.len() >= INDEX_MIN_TARGETS {
+ targets.sort_unstable_by(|left, right| left.time.total_cmp(&right.time));
+ targets.dedup_by(|target, previous| {
+ if target.time != previous.time {
+ return false;
+ }
+ if target.order < previous.order {
+ previous.time = target.time;
+ previous.order = target.order;
+ }
+ true
+ });
+ }
+ Self { targets }
+ }
+
+ pub(super) fn nearest(&self, value: f64, radius: f64) -> Option<f64> {
+ self.nearest_target(value, radius)
+ .map(|(target, _)| target.time)
+ }
+
+ pub(super) fn move_adjustment(&self, start: f64, end: f64, radius: f64) -> Option<(f64, f64)> {
+ let mut best: Option<(f64, &Target)> = None;
+ for edge in [start, end] {
+ let Some((target, distance)) = self.nearest_target(edge, radius) else {
+ continue;
+ };
+ if distance.is_finite()
+ && best.is_none_or(|(adjustment, previous)| {
+ distance < adjustment.abs()
+ || (distance == adjustment.abs() && target.order < previous.order)
+ })
+ {
+ best = Some((target.time - edge, target));
+ }
+ }
+ best.map(|(adjustment, target)| (adjustment, target.time))
+ }
+
+ fn nearest_target(&self, value: f64, radius: f64) -> Option<(&Target, f64)> {
+ if radius < 0.0 {
+ return None;
+ }
+ let candidates =
+ if self.targets.len() >= INDEX_MIN_TARGETS && value.is_finite() && radius.is_finite() {
+ let split = self.targets.partition_point(|target| target.time < value);
+ let nearest_distance = self.targets[..split]
+ .last()
+ .into_iter()
+ .chain(self.targets[split..].first())
+ .map(|target| (target.time - value).abs())
+ .fold(f64::INFINITY, f64::min);
+ if nearest_distance > radius {
+ return None;
+ }
+ // Distinct timestamps can round to the same distance and retain their original priority.
+ let mut start = split;
+ while start > 0 && (self.targets[start - 1].time - value).abs() <= nearest_distance
+ {
+ start -= 1;
+ }
+ let mut end = split;
+ while end < self.targets.len()
+ && (self.targets[end].time - value).abs() <= nearest_distance
+ {
+ end += 1;
+ }
+ &self.targets[start..end]
+ } else {
+ &self.targets
+ };
+ let mut best: Option<(&Target, f64)> = None;
+ for target in candidates {
+ let distance = (target.time - value).abs();
+ if distance <= radius
+ && best.is_none_or(|(previous, previous_distance)| {
+ distance < previous_distance
+ || (distance == previous_distance && target.order < previous.order)
+ })
+ {
+ best = Some((target, distance));
+ }
+ }
+ best
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{INDEX_MIN_TARGETS, SnapTargets};
+
+ fn indexed_targets(times: Vec<f64>) -> SnapTargets {
+ SnapTargets::new(
+ times
+ .into_iter()
+ .chain((0..INDEX_MIN_TARGETS).map(|index| -1_000_000.0 - index as f64))
+ .collect(),
+ )
+ }
+
+ fn linear_nearest(targets: &[f64], value: f64, radius: f64) -> Option<f64> {
+ let mut best: Option<f64> = None;
+ for &target in targets {
+ let distance = (target - value).abs();
+ if distance <= radius && best.is_none_or(|previous| (previous - value).abs() > distance)
+ {
+ best = Some(target);
+ }
+ }
+ best
+ }
+
+ fn linear_move(targets: &[f64], start: f64, end: f64, radius: f64) -> Option<(f64, f64)> {
+ let mut best = f64::INFINITY;
+ let mut engaged = None;
+ for &target in targets {
+ for edge in [start, end] {
+ let adjust = target - edge;
+ if adjust.abs() <= radius && adjust.abs() < best.abs() {
+ best = adjust;
+ engaged = Some(target);
+ }
+ }
+ }
+ engaged.map(|target| (best, target))
+ }
+
+ fn move_bits(result: Option<(f64, f64)>) -> Option<(u64, u64)> {
+ result.map(|(adjustment, target)| (adjustment.to_bits(), target.to_bits()))
+ }
+
+ #[test]
+ fn nearest_preserves_target_order_for_equal_distances() {
+ let targets = indexed_targets(vec![12.0, 8.0, 8.0]);
+ assert_eq!(targets.nearest(10.0, 2.0), Some(12.0));
+ assert_eq!(targets.nearest(10.0, 1.0), None);
+ assert_eq!(targets.nearest(8.0, 0.0), Some(8.0));
+ assert_eq!(targets.nearest(8.0, -1.0), None);
+ assert_eq!(SnapTargets::default().nearest(8.0, 2.0), None);
+ }
+
+ #[test]
+ fn moves_prioritize_target_order_then_the_start_edge() {
+ assert_eq!(
+ indexed_targets(vec![16.0, 10.0]).move_adjustment(9.0, 15.0, 1.0),
+ Some((1.0, 16.0))
+ );
+ assert_eq!(
+ indexed_targets(vec![10.0]).move_adjustment(9.0, 11.0, 1.0),
+ Some((1.0, 10.0))
+ );
+ }
+
+ #[test]
+ fn rounded_radius_boundaries_remain_inclusive() {
+ let lower = f64::from_bits(0.5_f64.to_bits() - 1);
+ assert!(lower < 1.0 - 0.5);
+ assert_eq!(1.0 - lower, 0.5);
+ assert_eq!(indexed_targets(vec![lower]).nearest(1.0, 0.5), Some(lower));
+ let upper = -lower;
+ assert_eq!(indexed_targets(vec![upper]).nearest(-1.0, 0.5), Some(upper));
+ }
+
+ #[test]
+ fn rounded_distance_ties_can_span_distinct_targets() {
+ let value = 2_f64.powi(55);
+ let targets = indexed_targets(vec![0.0, 1.0, 2.0]);
+ assert_eq!(targets.nearest(value, value), Some(0.0));
+ assert_eq!(
+ targets.move_adjustment(value, value, value),
+ Some((-value, 0.0))
+ );
+ }
+
+ #[test]
+ fn signed_zero_keeps_the_original_target_bits() {
+ for times in [vec![0.0, -0.0], vec![-0.0, 0.0]] {
+ let targets = indexed_targets(times.clone());
+ for value in [0.0, -0.0] {
+ assert_eq!(
+ targets.nearest(value, 0.0).map(f64::to_bits),
+ Some(times[0].to_bits())
+ );
+ assert_eq!(
+ move_bits(targets.move_adjustment(value, -value, 0.0)),
+ move_bits(linear_move(&times, value, -value, 0.0))
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn extreme_values_match_the_original_scans() {
+ let values = [
+ f64::NAN,
+ f64::INFINITY,
+ f64::NEG_INFINITY,
+ f64::MAX,
+ f64::MIN,
+ -1.0,
+ -f64::MIN_POSITIVE,
+ -0.0,
+ 0.0,
+ f64::MIN_POSITIVE,
+ f64::from_bits(1),
+ 1.0,
+ ];
+ for offset in 0..values.len() {
+ let mut times = values.to_vec();
+ times.rotate_left(offset);
+ times.extend((0..INDEX_MIN_TARGETS).map(|index| -1_000_000.0 - index as f64));
+ let targets = SnapTargets::new(times.clone());
+ for value in values {
+ for radius in values {
+ assert_eq!(
+ targets.nearest(value, radius).map(f64::to_bits),
+ linear_nearest(&times, value, radius).map(f64::to_bits)
+ );
+ assert_eq!(
+ move_bits(targets.move_adjustment(value, -value, radius)),
+ move_bits(linear_move(&times, value, -value, radius))
+ );
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn indexed_lookups_match_the_original_scans_for_varied_timelines() {
+ let mut state = 0x5c7e_42b1_09a3_d6f8_u64;
+ let mut next = || {
+ state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
+ state
+ };
+ for count in [0, 1, 2, 16, 31, 32, 33, 128, 1024] {
+ let times: Vec<_> = (0..count).map(|_| (next() % 12000) as f64 / 20.0).collect();
+ let targets = SnapTargets::new(times.clone());
+ for _ in 0..200 {
+ let start = (next() % 12000) as f64 / 20.0;
+ let end = start + (next() % 200) as f64 / 20.0;
+ let radius = (next() % 200) as f64 / 100.0;
+ assert_eq!(
+ targets.nearest(start, radius).map(f64::to_bits),
+ linear_nearest(&times, start, radius).map(f64::to_bits)
+ );
+ assert_eq!(
+ move_bits(targets.move_adjustment(start, end, radius)),
+ move_bits(linear_move(&times, start, end, radius))
+ );
+ }
+ }
+ }
+
+ #[test]
+ #[ignore]
+ fn benchmark_drag_snapping() {
+ use std::{hint::black_box, time::Instant};
+ println!("targets,duration,queries,radius,movement,sample,before_ns,after_ns");
+ for (count, duration, queries, radius) in [
+ (8usize, 600., 120, 0.08),
+ (32, 600., 120, 4.8),
+ (256, 600., 120, 4.8),
+ (2048, 600., 120, 4.8),
+ (16384, 3600., 240, 28.8),
+ ] {
+ let mut times = vec![0., duration * 0.47, duration];
+ times.extend((3..count).map(|i| {
+ let position = (i * 7919) % count;
+ let position = if i.is_multiple_of(19) {
+ position / 8 * 8
+ } else {
+ position
+ };
+ duration * position as f64 / count as f64
+ }));
+ let values: Vec<_> = (0..queries)
+ .map(|i| duration * 0.4 + duration * 0.15 * (i as f64 / (queries - 1) as f64 - 0.5))
+ .collect();
+ let targets = SnapTargets::new(times.clone());
+ for &value in &values {
+ assert_eq!(
+ targets.nearest(value, radius).map(f64::to_bits),
+ linear_nearest(&times, value, radius).map(f64::to_bits)
+ );
+ assert_eq!(
+ move_bits(targets.move_adjustment(value, value + 5., radius)),
+ move_bits(linear_move(&times, value, value + 5., radius))
+ );
+ }
+ for movement in [false, true] {
+ let run = |indexed| {
+ let start = Instant::now();
+ if indexed {
+ let targets = SnapTargets::new(black_box(times.clone()));
+ for &value in &values {
+ if movement {
+ black_box(targets.move_adjustment(
+ black_box(value),
+ value + 5.,
+ radius,
+ ));
+ } else {
+ black_box(targets.nearest(black_box(value), radius));
+ }
+ }
+ } else {
+ let targets = black_box(times.clone());
+ for &value in &values {
+ if movement {
+ black_box(linear_move(
+ &targets,
+ black_box(value),
+ value + 5.,
+ radius,
+ ));
+ } else {
+ black_box(linear_nearest(&targets, black_box(value), radius));
+ }
+ }
+ }
+ start.elapsed().as_nanos()
+ };
+ for sample in 0..35 {
+ let (before, after) = if sample % 2 == 0 {
+ (run(false), run(true))
+ } else {
+ let after = run(true);
+ (run(false), after)
+ };
+ if sample >= 5 {
+ println!(
+ "{count},{duration},{queries},{radius},{movement},{},{before},{after}",
+ sample - 5
+ );
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/apps/desktop-gpui/src/hotkeys.rs b/apps/desktop-gpui/src/hotkeys.rs
index d96db1dad..d2fa8cc52 100644
--- a/apps/desktop-gpui/src/hotkeys.rs
+++ b/apps/desktop-gpui/src/hotkeys.rs
@@ -441,21 +441,26 @@ fn reserve_wayland_stop(generation: u64, cx: &mut App) -> anyhow::Result<()> {
);
}
WaylandStopEvent::Unavailable(error) => {
+ let was_ready = stop.ready;
stop.ready = false;
- app_windows::clean_capture_stop_unavailable(generation, error, cx);
+ app_windows::clean_capture_stop_unavailable(
+ generation, error, was_ready, cx,
+ );
}
_ => {}
}
});
}
cx.update(|cx| {
- if cx
+ if let Some(was_ready) = cx
.try_global::<WaylandStop>()
- .is_some_and(|stop| stop.generation == generation && stop.cleanup.is_none())
+ .filter(|stop| stop.generation == generation && stop.cleanup.is_none())
+ .map(|stop| stop.ready)
{
app_windows::clean_capture_stop_unavailable(
generation,
"The recording Stop worker ended".into(),
+ was_ready,
cx,
);
}
@@ -498,15 +503,37 @@ async fn run_wayland_stop(
if let Err(error) = result {
tracing::info!(%error, "Using the recording Stop tray instead of GlobalShortcuts");
}
- let result = run_tray_stop(generation, &cancel, &events)
- .await
- .map_err(|error| error.to_string());
- if let Err(error) = &result {
- let _ = events.send(WaylandStopEvent::Unavailable(error.clone()));
+ match run_tray_stop(generation, &cancel, &events).await {
+ Ok(()) => Ok(()),
+ Err(error) => {
+ let _ = events.send(WaylandStopEvent::Unavailable(error.to_string()));
+ // A tray that never registered left nothing to clean up; failing
+ // here would block every later recording behind a cleanup error.
+ if error.is::<StopTrayNeverRegistered>() {
+ Ok(())
+ } else {
+ Err(error.to_string())
+ }
+ }
+ }
+}
+
+/// The recording Stop tray could not register at all (no StatusNotifier
+/// watcher, as on GNOME without the AppIndicator extension).
+#[cfg(target_os = "linux")]
+#[derive(Debug)]
+struct StopTrayNeverRegistered(String);
+
+#[cfg(target_os = "linux")]
+impl std::fmt::Display for StopTrayNeverRegistered {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str(&self.0)
}
- result
}
+#[cfg(target_os = "linux")]
+impl std::error::Error for StopTrayNeverRegistered {}
+
#[cfg(target_os = "linux")]
fn wayland_stop_icon() -> anyhow::Result<cap_utils::linux_recording_stop::StopTrayIcon> {
let image = image::load_from_memory(include_bytes!("../assets/tray/tray-stop-icon.png"))?
@@ -537,10 +564,11 @@ async fn run_tray_stop(
events: &flume::Sender<WaylandStopEvent>,
) -> anyhow::Result<()> {
use cap_utils::linux_recording_stop::{StopTray, StopTrayEvent};
- let icon = wayland_stop_icon()?;
+ let icon = wayland_stop_icon()
+ .map_err(|error| anyhow::Error::new(StopTrayNeverRegistered(error.to_string())))?;
let tray = StopTray::open(generation, icon).await.map_err(|error| {
if error.can_fallback() {
- anyhow::Error::msg(error)
+ anyhow::Error::new(StopTrayNeverRegistered(error.to_string()))
} else {
anyhow::anyhow!("Recording Stop tray registration cleanup is unconfirmed: {error}")
}
diff --git a/apps/desktop-gpui/src/i18n/tests.rs b/apps/desktop-gpui/src/i18n/tests.rs
index 62d3f2359..d88dd7fe2 100644
--- a/apps/desktop-gpui/src/i18n/tests.rs
+++ b/apps/desktop-gpui/src/i18n/tests.rs
@@ -114,6 +114,9 @@ fn english_matches_the_literals_it_replaced() {
tr::settings::general::appearance::title(),
"Appearance".into(),
),
+ (tr::settings::general::appearance::system(), "System".into()),
+ (tr::settings::general::appearance::light(), "Light".into()),
+ (tr::settings::general::appearance::dark(), "Dark".into()),
(
tr::settings::general::app::description(),
"Choose how Cap shows up on your system.".into(),
@@ -491,6 +494,54 @@ fn english_matches_the_literals_it_replaced() {
const NEW_IN_THE_LANGUAGE_PICKER: &str = "settings.general.language.";
+#[test]
+fn appearance_labels_follow_language_changes_without_changing_saved_values() {
+ use crate::store::{AppTheme, SettingsEnum};
+
+ const CHILD: &str = "CAP_GPUI_TEST_APPEARANCE_LABELS";
+ if std::env::var_os(CHILD).is_none() {
+ let output = std::process::Command::new(std::env::current_exe().unwrap())
+ .args([
+ "--exact",
+ "i18n::tests::appearance_labels_follow_language_changes_without_changing_saved_values",
+ "--nocapture",
+ ])
+ .env(CHILD, "1")
+ .env_remove(LOCALE_ENV)
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "{}\n{}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ return;
+ }
+
+ for (locale, labels) in [
+ (Locale::En, ["System", "Light", "Dark"]),
+ (Locale::ZhCn, ["跟随系统", "浅色", "深色"]),
+ (Locale::Ja, ["システム", "ライト", "ダーク"]),
+ (Locale::Fr, ["Système", "Clair", "Sombre"]),
+ (Locale::Es, ["Sistema", "Claro", "Oscuro"]),
+ (Locale::De, ["System", "Hell", "Dunkel"]),
+ (Locale::Hi, ["सिस्टम", "लाइट", "डार्क"]),
+ (Locale::En, ["System", "Light", "Dark"]),
+ ] {
+ init(Some(locale.code()));
+ for ((theme, label), saved) in AppTheme::ALL
+ .iter()
+ .zip(labels)
+ .zip(["system", "light", "dark"])
+ {
+ assert_eq!(theme.label(), label, "{} {saved}", locale.code());
+ assert_eq!(theme.as_json(), saved);
+ assert_eq!(AppTheme::from_json(saved), Some(*theme));
+ }
+ }
+}
+
#[test]
fn language_picker_english() {
assert_eq!(tr::settings::general::language::title(), "Language");
diff --git a/apps/desktop-gpui/src/library.rs b/apps/desktop-gpui/src/library.rs
index 194287426..5e3f9c31f 100644
--- a/apps/desktop-gpui/src/library.rs
+++ b/apps/desktop-gpui/src/library.rs
@@ -28,7 +28,7 @@ use cap_project::{
StudioRecordingStatus,
};
use cap_recording::{recovery::RecoveryManager, upload_resume::UploadLock};
-use gpui::RenderImage;
+use gpui::{App, RenderImage};
/// `RECENT_MEDIA_LIMIT` in `new-main/index.tsx:129`.
pub const RECENT_MEDIA_LIMIT: usize = 9;
@@ -279,20 +279,17 @@ impl RecordingItem {
}
}
-/// `RecordingMetaWithMetadata::new` (`lib.rs:3888-3925`).
-fn recording_item(path: PathBuf, meta: RecordingMeta, sort_time_millis: f64) -> RecordingItem {
- let mode = match &meta.inner {
+/// `RecordingMetaWithMetadata::new`'s mode (`lib.rs:3888-3925`).
+fn recording_mode(meta: &RecordingMeta) -> RecordingMode {
+ match &meta.inner {
RecordingMetaInner::Studio(_) => RecordingMode::Studio,
RecordingMetaInner::Instant(_) => RecordingMode::Instant,
- };
- let clip_count = match &meta.inner {
- RecordingMetaInner::Studio(studio) => match &**studio {
- StudioRecordingMeta::MultipleSegments { inner } => inner.segments.len() as u32,
- StudioRecordingMeta::SingleSegment { .. } => 1,
- },
- RecordingMetaInner::Instant(_) => 1,
- };
- let status = match &meta.inner {
+ }
+}
+
+/// `RecordingMetaWithMetadata::new`'s status (`lib.rs:3888-3925`).
+fn recording_status(meta: &RecordingMeta) -> RecordingStatus {
+ match &meta.inner {
RecordingMetaInner::Studio(studio) => match &**studio {
// A `MultipleSegments` meta with no `status` key at all is
// Complete -- `.unwrap_or(StudioRecordingStatus::Complete)`.
@@ -320,7 +317,31 @@ fn recording_item(path: PathBuf, meta: RecordingMeta, sort_time_millis: f64) ->
RecordingMetaInner::Instant(InstantRecordingMeta::Complete { .. }) => {
RecordingStatus::Complete
}
+ }
+}
+
+#[cfg(test)]
+thread_local! {
+ /// Full [`RecordingItem`]s built on this test thread. Recovery must stay on
+ /// [`ScannedRecording`], so its tests can pin that this does not move.
+ static RECORDING_ITEM_BUILDS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
+}
+
+/// `RecordingMetaWithMetadata::new` (`lib.rs:3888-3925`), plus the filesystem
+/// lookups a card needs.
+fn recording_item(path: PathBuf, meta: RecordingMeta, sort_time_millis: f64) -> RecordingItem {
+ let mode = recording_mode(&meta);
+ let clip_count = match &meta.inner {
+ RecordingMetaInner::Studio(studio) => match &**studio {
+ StudioRecordingMeta::MultipleSegments { inner } => inner.segments.len() as u32,
+ StudioRecordingMeta::SingleSegment { .. } => 1,
+ },
+ RecordingMetaInner::Instant(_) => 1,
};
+ let status = recording_status(&meta);
+
+ #[cfg(test)]
+ RECORDING_ITEM_BUILDS.with(|builds| builds.set(builds.get() + 1));
let preview = path.join("screenshots/preview.jpg");
let thumbnail = if preview.is_file() {
@@ -360,6 +381,37 @@ pub fn list_recordings_in(dirs: &[PathBuf]) -> Vec<RecordingItem> {
}
fn recording_items_in(dirs: &[PathBuf]) -> impl Iterator<Item = RecordingItem> {
+ scanned_recordings_in(dirs).map(ScannedRecording::into_item)
+}
+
+/// A recording directory after its metadata has been parsed, before the
+/// filesystem work a card needs (upload state, thumbnails, preview mtimes).
+///
+/// Both the library listing and the recovery scan walk every bundle, but
+/// recovery drops complete recordings before it needs any of those fields.
+/// Keeping this intermediate means the scan parses each `recording-meta.json`
+/// once and recovery only pays for recordings it can actually return.
+///
+/// Skipping the fields is state-neutral: `upload::queue::status` only reads the
+/// saved upload state (and settings for legacy records), and the thumbnail
+/// checks are `stat`s.
+struct ScannedRecording {
+ path: PathBuf,
+ meta: RecordingMeta,
+ sort_time_millis: f64,
+}
+
+impl ScannedRecording {
+ fn status(&self) -> RecordingStatus {
+ recording_status(&self.meta)
+ }
+
+ fn into_item(self) -> RecordingItem {
+ recording_item(self.path, self.meta, self.sort_time_millis)
+ }
+}
+
+fn scanned_recordings_in(dirs: &[PathBuf]) -> impl Iterator<Item = ScannedRecording> {
let mut paths = Vec::new();
for dir in dirs {
let Ok(entries) = std::fs::read_dir(dir) else {
@@ -377,7 +429,11 @@ fn recording_items_in(dirs: &[PathBuf]) -> impl Iterator<Item = RecordingItem> {
paths.sort_by(|(_, a), (_, b)| b.total_cmp(a));
paths.into_iter().filter_map(|(path, sort_time_millis)| {
let meta = RecordingMeta::load_for_project(&path).ok()?;
- Some(recording_item(path, meta, sort_time_millis))
+ Some(ScannedRecording {
+ path,
+ meta,
+ sort_time_millis,
+ })
})
}
@@ -396,18 +452,21 @@ pub fn find_incomplete_recordings_in(
dirs: &[PathBuf],
active_recording: Option<&Path>,
) -> Vec<IncompleteRecordingItem> {
- list_recordings_in(dirs)
- .into_iter()
- .filter(|item| {
+ scanned_recordings_in(dirs)
+ .filter(|recording| {
matches!(
- item.status,
+ recording.status(),
RecordingStatus::InProgress | RecordingStatus::NeedsRemux
- ) && active_recording != Some(item.path.as_path())
- && is_recording_after_recovery_cutoff(&item.pretty_name, item.sort_time_millis)
+ ) && active_recording != Some(recording.path.as_path())
+ && is_recording_after_recovery_cutoff(
+ &recording.meta.pretty_name,
+ recording.sort_time_millis,
+ )
})
- .filter_map(|item| {
- if item.mode == RecordingMode::Instant {
- let display = item.path.join("content/display");
+ .filter_map(|recording| {
+ let ScannedRecording { path, meta, .. } = recording;
+ if recording_mode(&meta) == RecordingMode::Instant {
+ let display = path.join("content/display");
if !display.join("init.mp4").is_file() {
return None;
}
@@ -417,16 +476,16 @@ pub fn find_incomplete_recordings_in(
.filter(|entry| RecoveryManager::is_m4s_complete(&entry.path()))
.count();
return (segment_count > 0).then_some(IncompleteRecordingItem {
- project_path: item.path,
- pretty_name: item.pretty_name,
+ project_path: path,
+ pretty_name: meta.pretty_name,
segment_count,
estimated_duration_secs: 0.0,
});
}
- let incomplete = RecoveryManager::inspect_recording(&item.path)?;
+ let incomplete = RecoveryManager::inspect_recording(&path)?;
(!incomplete.recoverable_segments.is_empty()).then_some(IncompleteRecordingItem {
- project_path: item.path,
- pretty_name: item.pretty_name,
+ project_path: path,
+ pretty_name: meta.pretty_name,
segment_count: incomplete.recoverable_segments.len(),
estimated_duration_secs: incomplete.estimated_duration.as_secs_f64(),
})
@@ -595,61 +654,23 @@ pub fn delete_recording_directory(path: &Path) -> Result<(), String> {
/// An instant recording opens its `content` directory --
/// `commands.openFilePath` is `open <dir>` on macOS -- and anything else, or a
/// bundle with no `content` directory, is revealed in the file manager.
-pub fn open_recording_folder(path: &Path, mode: RecordingMode) {
+pub fn open_recording_folder(path: &Path, mode: RecordingMode, cx: &App) {
let content = path.join("content");
if mode == RecordingMode::Instant && content.is_dir() {
- open_path(&content);
+ open_with_system(&content, cx);
} else {
- reveal_in_folder(path);
+ reveal_path(path, cx);
}
}
-pub fn open_path(path: &Path) {
- let result = {
- #[cfg(target_os = "macos")]
- {
- std::process::Command::new("open").arg(path).spawn()
- }
- #[cfg(target_os = "windows")]
- {
- std::process::Command::new("cmd")
- .args(["/C", "start", "", &path.to_string_lossy()])
- .spawn()
- }
- #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))]
- {
- std::process::Command::new("xdg-open").arg(path).spawn()
- }
- };
- if let Err(error) = result {
- tracing::warn!(path = %path.display(), "opening a path failed: {error}");
- }
+// `std::fs::canonicalize` yields `\\?\` verbatim paths on Windows, which the
+// shell APIs behind GPUI's reveal and open reject with E_INVALIDARG.
+pub fn reveal_path(path: &Path, cx: &App) {
+ cx.reveal_path(dunce::simplified(path));
}
-pub fn reveal_in_folder(path: &Path) {
- let result = {
- #[cfg(target_os = "macos")]
- {
- std::process::Command::new("open")
- .arg("-R")
- .arg(path)
- .spawn()
- }
- #[cfg(target_os = "windows")]
- {
- std::process::Command::new("explorer")
- .arg(format!("/select,{}", path.display()))
- .spawn()
- }
- #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))]
- {
- let parent = path.parent().unwrap_or(path);
- std::process::Command::new("xdg-open").arg(parent).spawn()
- }
- };
- if let Err(error) = result {
- tracing::warn!(path = %path.display(), "revealing a path failed: {error}");
- }
+pub fn open_with_system(path: &Path, cx: &App) {
+ cx.open_with_system(dunce::simplified(path));
}
pub fn copy_file_to_path(src: &Path, dest: &Path) -> Result<(), String> {
@@ -2004,6 +2025,232 @@ mod tests {
);
}
+ /// Same-process old vs new benchmark for the startup recovery scan.
+ ///
+ /// Old is the pre-change implementation: `list_recordings_in` (a full
+ /// `RecordingItem` per recording) followed by the recovery filter. New is
+ /// `find_incomplete_recordings_in`, which filters on parsed metadata first.
+ ///
+ /// Set `CAP_GPUI_RECOVERY_BENCH_DIR` to measure a real library. Without it
+ /// the benchmark generates 600 complete Studio + 300 complete Instant
+ /// recordings, with thumbnails and saved upload state where real ones have
+ /// them, plus four recoverable incomplete recordings, one pre-cutoff
+ /// recording, one corrupt meta and one missing meta: 907 directories,
+ /// 905 parsed recordings. Run with:
+ ///
+ /// `cargo test -p cap-desktop-gpui recovery_scan_benchmark -- --ignored --nocapture`
+ #[test]
+ #[ignore = "manual filesystem benchmark; set CAP_GPUI_RECOVERY_BENCH_DIR for a real library"]
+ fn recovery_scan_benchmark() {
+ fn write_complete_studio(recordings: &Path, index: usize) {
+ let bundle = recordings.join(format!("complete-studio-{index:04}.cap"));
+ std::fs::create_dir_all(&bundle).unwrap();
+ let segment_count = if index % 5 == 0 { 3 } else { 1 };
+ let segments: Vec<String> = (0..segment_count)
+ .map(|segment| {
+ format!(
+ r#"{{"display":{{"path":"content/segments/segment-{segment}/display.mp4","fps":30}}}}"#
+ )
+ })
+ .collect();
+ let sharing = if index % 4 == 0 {
+ r#"{"id":"share","link":"https://cap.so/s/share"}"#
+ } else {
+ "null"
+ };
+ std::fs::write(
+ bundle.join("recording-meta.json"),
+ format!(
+ r#"{{"pretty_name":"Studio {index}","sharing":{sharing},"segments":[{}]}}"#,
+ segments.join(",")
+ ),
+ )
+ .unwrap();
+ if index % 3 == 0 {
+ let screenshots = bundle.join("screenshots");
+ std::fs::create_dir_all(&screenshots).unwrap();
+ std::fs::write(
+ screenshots.join(if index % 6 == 0 {
+ "preview.jpg"
+ } else {
+ "display.jpg"
+ }),
+ b"thumbnail",
+ )
+ .unwrap();
+ }
+ }
+
+ fn write_complete_instant(recordings: &Path, index: usize) {
+ let bundle = recordings.join(format!("complete-instant-{index:04}.cap"));
+ std::fs::create_dir_all(&bundle).unwrap();
+ std::fs::write(
+ bundle.join("recording-meta.json"),
+ format!(
+ r#"{{"pretty_name":"Instant {index}","sharing":null,"fps":30,"sample_rate":48000}}"#
+ ),
+ )
+ .unwrap();
+ if index % 2 == 0 {
+ let state = serde_json::json!({
+ "version": 1,
+ "video_id": format!("bench-{index}"),
+ "server_url": "https://cap.example.invalid",
+ "owner_id": null,
+ "kind": "Mp4",
+ "requested_audio": null,
+ "preserve_local": true,
+ "phase": "Verified",
+ "attempt_count": 0,
+ "last_error": null,
+ "next_retry_at": null,
+ "verification": null,
+ "receipt": null,
+ });
+ std::fs::write(
+ bundle.join("instant-upload.json"),
+ serde_json::to_vec(&state).unwrap(),
+ )
+ .unwrap();
+ }
+ if index % 3 == 1 {
+ let screenshots = bundle.join("screenshots");
+ std::fs::create_dir_all(&screenshots).unwrap();
+ std::fs::write(screenshots.join("display.jpg"), b"thumbnail").unwrap();
+ }
+ }
+
+ let from_env = std::env::var("CAP_GPUI_RECOVERY_BENCH_DIR").ok();
+ let generated = from_env.is_none().then(|| temp_dir("recovery-bench"));
+ let dirs = match (&from_env, &generated) {
+ (Some(dir), _) => vec![PathBuf::from(dir)],
+ (None, Some(root)) => {
+ let recordings = root.join("recordings");
+ std::fs::create_dir_all(&recordings).unwrap();
+ for index in 0..600 {
+ write_complete_studio(&recordings, index);
+ }
+ for index in 0..300 {
+ write_complete_instant(&recordings, index);
+ }
+ for index in 0..2 {
+ write_incomplete_bundle(
+ &recordings,
+ &format!("incomplete-studio-{index}"),
+ &format!("Cap 2026-02-0{index} at 10.00.00"),
+ "InProgress",
+ true,
+ );
+ }
+ write_incomplete_bundle(
+ &recordings,
+ "remux-studio",
+ "Cap 2026-02-03 at 10.00.00",
+ "NeedsRemux",
+ true,
+ );
+ write_incomplete_instant_bundle(
+ &recordings,
+ "incomplete-instant",
+ "Cap 2026-02-04 at 10.00.00",
+ false,
+ );
+ write_incomplete_bundle(
+ &recordings,
+ "pre-cutoff",
+ "Cap 2025-12-31 at 10.00.00",
+ "InProgress",
+ true,
+ );
+ write_bundle(&recordings, "corrupt-meta", "{ not json ");
+ std::fs::create_dir_all(recordings.join("missing-meta")).unwrap();
+ assert_eq!(
+ std::fs::read_dir(&recordings).unwrap().count(),
+ 907,
+ "the generated fixture must keep its documented shape"
+ );
+ vec![recordings]
+ }
+ (None, None) => unreachable!(),
+ };
+
+ let expected = find_incomplete_recordings_in(&dirs, None);
+ if from_env.is_none() {
+ assert_eq!(
+ expected.len(),
+ 4,
+ "the generated fixture has four recoverable recordings"
+ );
+ }
+ assert_eq!(
+ find_incomplete_recordings_by_full_listing(&dirs, None),
+ expected,
+ "both implementations must agree before timing"
+ );
+
+ let old = || find_incomplete_recordings_by_full_listing(&dirs, None);
+ let new = || find_incomplete_recordings_in(&dirs, None);
+ let time = |scan: &dyn Fn() -> Vec<IncompleteRecordingItem>| {
+ let started = std::time::Instant::now();
+ let items = std::hint::black_box(scan());
+ let millis = started.elapsed().as_secs_f64() * 1000.0;
+ assert_eq!(items, expected);
+ millis
+ };
+
+ for _ in 0..3 {
+ std::hint::black_box(old());
+ std::hint::black_box(new());
+ }
+ let mut old_samples = Vec::new();
+ let mut new_samples = Vec::new();
+ for sample in 0..15 {
+ // Alternate the order so neither side systematically warms the
+ // directory and metadata caches for the other.
+ let (old_ms, new_ms) = if sample % 2 == 0 {
+ (time(&old), time(&new))
+ } else {
+ let new_ms = time(&new);
+ let old_ms = time(&old);
+ (old_ms, new_ms)
+ };
+ old_samples.push(old_ms);
+ new_samples.push(new_ms);
+ }
+ old_samples.sort_by(f64::total_cmp);
+ new_samples.sort_by(f64::total_cmp);
+ let median = |samples: &[f64]| samples[samples.len() / 2];
+
+ let recordings = scanned_recordings_in(&dirs).count();
+ let complete = scanned_recordings_in(&dirs)
+ .filter(|recording| recording.status() == RecordingStatus::Complete)
+ .count();
+ if from_env.is_none() {
+ assert_eq!(recordings, 905, "the generated fixture parses 905 bundles");
+ assert_eq!(
+ complete, 900,
+ "the generated fixture has 900 complete bundles"
+ );
+ }
+ println!(
+ "{}",
+ serde_json::json!({
+ "fixture": if from_env.is_some() { "env" } else { "generated" },
+ "recordings": recordings,
+ "complete_recordings": complete,
+ "recovered_recordings": expected.len(),
+ "samples": old_samples.len(),
+ "old_median_ms": median(&old_samples),
+ "new_median_ms": median(&new_samples),
+ "speedup": median(&old_samples) / median(&new_samples),
+ })
+ );
+
+ if let Some(root) = generated {
+ std::fs::remove_dir_all(root).ok();
+ }
+ }
+
/// `RecordingMetaWithMetadata::new`: the mode, the clip count and the
/// status for each shape of meta the two recorders write.
#[test]
@@ -2044,11 +2291,31 @@ mod tests {
),
);
std::thread::sleep(std::time::Duration::from_millis(6));
- write_bundle(
+ let instant_complete = write_bundle(
&recordings,
"instant-complete",
r#"{"pretty_name":"Instant complete","sharing":null,"fps":30,"sample_rate":48000}"#,
);
+ std::fs::write(
+ instant_complete.join("instant-upload.json"),
+ serde_json::to_vec(&serde_json::json!({
+ "version": 1,
+ "video_id": "listing-instant",
+ "server_url": "https://cap.example.invalid",
+ "owner_id": null,
+ "kind": "Mp4",
+ "requested_audio": null,
+ "preserve_local": true,
+ "phase": "Verified",
+ "attempt_count": 0,
+ "last_error": null,
+ "next_retry_at": null,
+ "verification": null,
+ "receipt": null,
+ }))
+ .unwrap(),
+ )
+ .unwrap();
std::thread::sleep(std::time::Duration::from_millis(6));
write_bundle(
&recordings,
@@ -2132,6 +2399,10 @@ mod tests {
assert_eq!(complete.mode, RecordingMode::Instant);
assert_eq!(complete.clip_count, 1, "an instant recording is one clip");
assert_eq!(complete.status, RecordingStatus::Complete);
+ assert!(
+ complete.upload.is_some(),
+ "the listing still reads saved instant upload state"
+ );
assert!(
!complete.opens_editor(),
"instant rows never open the editor, complete or not"
@@ -2190,6 +2461,292 @@ mod tests {
bundle
}
+ fn write_incomplete_instant_bundle(
+ recordings: &Path,
+ name: &str,
+ pretty_name: &str,
+ recording: bool,
+ ) -> PathBuf {
+ let bundle = write_bundle(
+ recordings,
+ name,
+ &format!(r#"{{"pretty_name":"{pretty_name}","sharing":null,"recording":{recording}}}"#),
+ );
+ let display = bundle.join("content/display");
+ std::fs::create_dir_all(&display).unwrap();
+ std::fs::write(display.join("init.mp4"), [0; 8]).unwrap();
+ std::fs::write(display.join("segment_001.m4s"), complete_m4s_fragment()).unwrap();
+ bundle
+ }
+
+ /// The recovery scan before the lean path: list every recording (building
+ /// every `RecordingItem`), then filter. This is the reference implementation
+ /// production must agree with, and the "old" side of the benchmark.
+ fn find_incomplete_recordings_by_full_listing(
+ dirs: &[PathBuf],
+ active_recording: Option<&Path>,
+ ) -> Vec<IncompleteRecordingItem> {
+ list_recordings_in(dirs)
+ .into_iter()
+ .filter(|item| {
+ matches!(
+ item.status,
+ RecordingStatus::InProgress | RecordingStatus::NeedsRemux
+ ) && active_recording != Some(item.path.as_path())
+ && is_recording_after_recovery_cutoff(&item.pretty_name, item.sort_time_millis)
+ })
+ .filter_map(|item| {
+ if item.mode == RecordingMode::Instant {
+ let display = item.path.join("content/display");
+ if !display.join("init.mp4").is_file() {
+ return None;
+ }
+ let segment_count = std::fs::read_dir(display)
+ .ok()?
+ .filter_map(Result::ok)
+ .filter(|entry| RecoveryManager::is_m4s_complete(&entry.path()))
+ .count();
+ return (segment_count > 0).then_some(IncompleteRecordingItem {
+ project_path: item.path,
+ pretty_name: item.pretty_name,
+ segment_count,
+ estimated_duration_secs: 0.0,
+ });
+ }
+ let incomplete = RecoveryManager::inspect_recording(&item.path)?;
+ (!incomplete.recoverable_segments.is_empty()).then_some(IncompleteRecordingItem {
+ project_path: item.path,
+ pretty_name: item.pretty_name,
+ segment_count: incomplete.recoverable_segments.len(),
+ estimated_duration_secs: incomplete.estimated_duration.as_secs_f64(),
+ })
+ })
+ .collect()
+ }
+
+ fn recording_item_builds() -> usize {
+ RECORDING_ITEM_BUILDS.with(std::cell::Cell::get)
+ }
+
+ #[test]
+ fn lean_recovery_matches_the_full_listing_on_a_mixed_library() {
+ let root = temp_dir("recovery-equivalence");
+ let recordings = root.join("recordings");
+ std::fs::create_dir_all(&recordings).unwrap();
+
+ // Created oldest first; the recovery scan's order is creation order
+ // reversed, so the expected output below is the reverse of the
+ // recoverable fixtures' creation order.
+ let pre_cutoff = write_incomplete_bundle(
+ &recordings,
+ "pre-cutoff",
+ "Cap 2025-12-31 at 23.59.59",
+ "InProgress",
+ true,
+ );
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let complete_studio = write_incomplete_bundle(
+ &recordings,
+ "complete-studio",
+ "Cap 2026-01-02 at 10.00.00",
+ "Complete",
+ true,
+ );
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ write_bundle(&recordings, "corrupt-meta", "{ not json ");
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ std::fs::create_dir_all(recordings.join("missing-meta")).unwrap();
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let active = write_incomplete_bundle(
+ &recordings,
+ "active",
+ "Cap 2026-01-05 at 10.00.00",
+ "InProgress",
+ true,
+ );
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let in_progress = write_incomplete_bundle(
+ &recordings,
+ "in-progress",
+ "Cap 2026-01-01 at 10.00.00",
+ "InProgress",
+ true,
+ );
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let remux = write_incomplete_bundle(
+ &recordings,
+ "remux",
+ "Cap 2026-01-04 at 10.00.00",
+ "NeedsRemux",
+ true,
+ );
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let instant = write_incomplete_instant_bundle(
+ &recordings,
+ "instant-incomplete",
+ "Cap 2026-01-03 at 10.00.00",
+ false,
+ );
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let complete_instant = write_incomplete_instant_bundle(
+ &recordings,
+ "complete-instant",
+ "Cap 2026-01-06 at 10.00.00",
+ false,
+ );
+ // Complete status with fragments still on disk: recovery must trust
+ // the metadata and leave them alone.
+ std::fs::write(
+ complete_instant.join("recording-meta.json"),
+ r#"{"pretty_name":"Cap 2026-01-06 at 10.00.00","sharing":null,"fps":30,"sample_rate":48000}"#,
+ )
+ .unwrap();
+ std::thread::sleep(std::time::Duration::from_millis(6));
+ let unrecoverable = write_incomplete_bundle(
+ &recordings,
+ "unrecoverable",
+ "Cap 2026-01-07 at 10.00.00",
+ "InProgress",
+ false,
+ );
+
+ let dirs = std::slice::from_ref(&recordings);
+ let found = find_incomplete_recordings_in(dirs, Some(active.as_path()));
+ let reference = find_incomplete_recordings_by_full_listing(dirs, Some(active.as_path()));
+ assert_eq!(
+ found, reference,
+ "the lean scan must stay byte-for-byte equivalent to listing everything"
+ );
+
+ let names: Vec<&str> = found.iter().map(|item| item.pretty_name.as_str()).collect();
+ assert_eq!(
+ names,
+ [
+ "Cap 2026-01-03 at 10.00.00",
+ "Cap 2026-01-04 at 10.00.00",
+ "Cap 2026-01-01 at 10.00.00",
+ ],
+ "newest first, with only the eligible recoverable recordings"
+ );
+ assert_eq!(
+ found
+ .iter()
+ .map(|item| (item.project_path.clone(), item.segment_count))
+ .collect::<Vec<_>>(),
+ [
+ (instant.clone(), 1),
+ (remux.clone(), 1),
+ (in_progress.clone(), 1),
+ ]
+ );
+ assert!(
+ found
+ .iter()
+ .find(|item| item.project_path == instant)
+ .is_some_and(|item| item.estimated_duration_secs == 0.0),
+ "an Instant recording reports its measured segment count and zero duration"
+ );
+ for excluded in [
+ &pre_cutoff,
+ &complete_studio,
+ &complete_instant,
+ &active,
+ &unrecoverable,
+ ] {
+ assert!(
+ !found.iter().any(|item| &item.project_path == excluded),
+ "{} must not be recoverable",
+ excluded.display()
+ );
+ }
+
+ std::fs::remove_dir_all(root).unwrap();
+ }
+
+ #[test]
+ fn recovery_cutoff_is_strictly_after_2025() {
+ assert!(!is_recording_after_recovery_cutoff(
+ "Cap 2025-12-31 at 23.59.59",
+ 0.0
+ ));
+ assert!(is_recording_after_recovery_cutoff(
+ "Cap 2026-01-01 at 00.00.00",
+ 0.0
+ ));
+ assert!(
+ !is_recording_after_recovery_cutoff("Custom instant recording", 0.0),
+ "a name without a date falls back to the sort time"
+ );
+ }
+
+ #[test]
+ fn recovery_skips_full_item_work_and_inspection_for_complete_recordings() {
+ let root = temp_dir("recovery-lean");
+ let recordings = root.join("recordings");
+ std::fs::create_dir_all(&recordings).unwrap();
+
+ // The complete Studio bundle contains exactly the fragments that make
+ // `inspect_recording` return a recoverable recording. The complete
+ // Instant bundle contains the init + m4s fragments the Instant path
+ // counts. Recovery must filter both out on metadata alone.
+ let complete_studio = write_incomplete_bundle(
+ &recordings,
+ "complete-studio",
+ "Cap 2026-01-10 at 10.00.00",
+ "Complete",
+ true,
+ );
+ let complete_instant = write_incomplete_instant_bundle(
+ &recordings,
+ "complete-instant",
+ "Cap 2026-01-11 at 10.00.00",
+ false,
+ );
+ std::fs::write(
+ complete_instant.join("recording-meta.json"),
+ r#"{"pretty_name":"Cap 2026-01-11 at 10.00.00","sharing":null,"fps":30,"sample_rate":48000}"#,
+ )
+ .unwrap();
+ let in_progress = write_incomplete_bundle(
+ &recordings,
+ "in-progress",
+ "Cap 2026-01-12 at 10.00.00",
+ "InProgress",
+ true,
+ );
+ let remux_instant = write_incomplete_instant_bundle(
+ &recordings,
+ "remux-instant",
+ "Cap 2026-01-13 at 10.00.00",
+ false,
+ );
+
+ let builds_before = recording_item_builds();
+ let found = find_incomplete_recordings_in(std::slice::from_ref(&recordings), None);
+ assert_eq!(
+ recording_item_builds(),
+ builds_before,
+ "recovery must not construct a full RecordingItem, even for complete recordings"
+ );
+
+ assert_eq!(found.len(), 2);
+ assert!(found.iter().any(|item| item.project_path == in_progress));
+ assert!(found.iter().any(|item| item.project_path == remux_instant));
+
+ // Behavior preservation: `inspect_recording` creates `.recovery.lock`
+ // in the bundle it examines (and leaves it there after unlocking), so
+ // a complete recording without one was never inspected.
+ assert!(in_progress.join(".recovery.lock").is_file());
+ assert!(!complete_studio.join(".recovery.lock").exists());
+ assert!(!complete_instant.join(".recovery.lock").exists());
+ assert!(
+ !remux_instant.join(".recovery.lock").exists(),
+ "instant recovery reads fragments directly and never calls inspect_recording"
+ );
+
+ std::fs::remove_dir_all(root).unwrap();
+ }
+
#[test]
fn recovery_scan_preserves_active_terminal_legacy_and_unrecoverable_recordings() {
let _ = ffmpeg::init();
diff --git a/apps/desktop-gpui/src/linux_dialog.rs b/apps/desktop-gpui/src/linux_dialog.rs
index 359d817d3..6b6165923 100644
--- a/apps/desktop-gpui/src/linux_dialog.rs
+++ b/apps/desktop-gpui/src/linux_dialog.rs
@@ -116,7 +116,7 @@ fn open(request: Request, cx: &mut App) {
title: Some(title.into()),
..Default::default()
}),
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
focus: true,
show: true,
is_resizable: false,
diff --git a/apps/desktop-gpui/src/linux_display.rs b/apps/desktop-gpui/src/linux_display.rs
new file mode 100644
index 000000000..12eb8d613
--- /dev/null
+++ b/apps/desktop-gpui/src/linux_display.rs
@@ -0,0 +1,82 @@
+//! A `WAYLAND_DISPLAY` that names no running compositor, as a systemd user
+//! environment keeps after the Wayland session that set it has ended. gpui
+//! picks Wayland whenever the variable is set and cannot start without the
+//! compositor, so when the session also has an X display Cap uses that.
+
+use std::{
+ ffi::{OsStr, OsString},
+ io::ErrorKind,
+ os::unix::net::UnixStream,
+ path::{Path, PathBuf},
+};
+
+fn socket_path(display: &OsStr, runtime_dir: Option<&OsStr>) -> Option<PathBuf> {
+ let display = Path::new(display);
+ if display.is_absolute() {
+ return Some(display.to_path_buf());
+ }
+ let runtime_dir = Path::new(runtime_dir?);
+ runtime_dir
+ .is_absolute()
+ .then(|| runtime_dir.join(display))
+}
+
+/// Only a missing socket or a refused connection counts: anything else, such
+/// as a permission error, leaves the session as it is.
+fn nothing_listens(path: &Path) -> bool {
+ match UnixStream::connect(path) {
+ Ok(_) => false,
+ Err(error) => matches!(
+ error.kind(),
+ ErrorKind::NotFound | ErrorKind::ConnectionRefused
+ ),
+ }
+}
+
+pub fn stale_wayland_display() -> Option<OsString> {
+ if std::env::var_os("WAYLAND_SOCKET").is_some() {
+ return None;
+ }
+ let display = std::env::var_os("WAYLAND_DISPLAY").filter(|display| !display.is_empty())?;
+ std::env::var_os("DISPLAY").filter(|display| !display.is_empty())?;
+ let path = socket_path(&display, std::env::var_os("XDG_RUNTIME_DIR").as_deref())?;
+ nothing_listens(&path).then_some(display)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::os::unix::net::UnixListener;
+
+ #[test]
+ fn display_names_resolve_in_the_runtime_dir_unless_absolute() {
+ assert_eq!(
+ socket_path(OsStr::new("wayland-0"), Some(OsStr::new("/run/user/1000"))),
+ Some(PathBuf::from("/run/user/1000/wayland-0"))
+ );
+ assert_eq!(
+ socket_path(OsStr::new("/tmp/wl"), None),
+ Some(PathBuf::from("/tmp/wl"))
+ );
+ assert_eq!(socket_path(OsStr::new("wayland-0"), None), None);
+ assert_eq!(
+ socket_path(OsStr::new("wayland-0"), Some(OsStr::new("relative"))),
+ None
+ );
+ }
+
+ #[test]
+ fn only_a_missing_or_refusing_socket_is_stale() {
+ let dir = tempfile::tempdir().unwrap();
+ let live = dir.path().join("wayland-live");
+ let listener = UnixListener::bind(&live).unwrap();
+ assert!(!nothing_listens(&live));
+
+ let gone = dir.path().join("wayland-gone");
+ drop(UnixListener::bind(&gone).unwrap());
+ assert!(nothing_listens(&gone));
+
+ assert!(nothing_listens(&dir.path().join("wayland-missing")));
+ drop(listener);
+ }
+}
diff --git a/apps/desktop-gpui/src/linux_portal_id.rs b/apps/desktop-gpui/src/linux_portal_id.rs
new file mode 100644
index 000000000..f501e0695
--- /dev/null
+++ b/apps/desktop-gpui/src/linux_portal_id.rs
@@ -0,0 +1,312 @@
+//! Gives Cap an app id the desktop portals accept when nothing else does.
+//!
+//! xdg-desktop-portal names a host app after the systemd unit that launched
+//! it (`app-gnome-Cap-1234.scope` from GNOME's app grid). A launch from a
+//! terminal or an AppImage has no such unit, and the GlobalShortcuts portal
+//! then refuses Cap ("An app id is required"), so the recording Stop control
+//! falls back to the tray. `org.freedesktop.host.portal.Registry` lets the app
+//! name itself instead, once, before its first portal call, and only with an
+//! id a `.desktop` file carries.
+
+use std::{
+ ffi::OsString,
+ path::{Path, PathBuf},
+ time::Duration,
+};
+
+pub const PORTAL_APP_ID: &str = "so.cap.Cap";
+
+static REGISTERED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+
+/// Whether Cap's windows take [`PORTAL_APP_ID`]: only after this launch
+/// registered it, and only on GNOME, the one desktop that matches windows to
+/// it. Elsewhere the windows keep their usual id, which window rules may name.
+pub fn windows_use_portal_app_id() -> bool {
+ REGISTERED.load(std::sync::atomic::Ordering::Acquire)
+ && is_gnome(&std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default())
+}
+
+fn is_gnome(current_desktop: &str) -> bool {
+ current_desktop
+ .split(':')
+ .any(|desktop| desktop.eq_ignore_ascii_case("gnome"))
+}
+const PACKAGED_APP_ID: &str = "Cap";
+
+/// The app id xdg-desktop-portal reads from a launcher's systemd unit:
+/// `app-[<launcher>-]<id>-<random>.scope` or `app-[<launcher>-]<id>[@<random>].service`.
+pub fn unit_app_id(cgroup: &str) -> Option<String> {
+ let unit = cgroup.lines().find_map(|line| {
+ let path = line.rsplit(':').next()?;
+ path.rsplit('/')
+ .find(|part| part.starts_with("app-"))
+ .map(str::to_string)
+ })?;
+ let name = unit.strip_prefix("app-")?;
+ let name = if let Some(name) = name.strip_suffix(".scope") {
+ name.rsplit_once('-')?.0
+ } else {
+ let name = name.strip_suffix(".service")?;
+ name.split_once('@').map_or(name, |(name, _)| name)
+ };
+ let id = name.rsplit('-').next()?;
+ (!id.is_empty()).then(|| id.to_string())
+}
+
+fn application_dirs(
+ data_home: Option<OsString>,
+ data_dirs: Option<OsString>,
+ home: Option<OsString>,
+) -> Option<(PathBuf, Vec<PathBuf>)> {
+ let user = data_home
+ .filter(|dir| !dir.is_empty())
+ .map(PathBuf::from)
+ .or_else(|| {
+ home.filter(|home| !home.is_empty())
+ .map(|home| PathBuf::from(home).join(".local/share"))
+ })
+ .filter(|dir| dir.is_absolute())?
+ .join("applications");
+ let system = data_dirs
+ .filter(|dirs| !dirs.is_empty())
+ .unwrap_or_else(|| "/usr/local/share:/usr/share".into());
+ let mut dirs = vec![user.clone()];
+ dirs.extend(std::env::split_paths(&system).map(|dir| dir.join("applications")));
+ Some((user, dirs))
+}
+
+/// A hidden entry for [`PORTAL_APP_ID`]: the portal needs one to accept the
+/// id, and GNOME names the app from it in the shortcut dialog. GLib, which
+/// the portal loads it with, looks the program up before expanding `%%`, so
+/// `env` is the program and Cap's path its argument.
+pub fn desktop_entry(exec: &Path) -> String {
+ format!(
+ "[Desktop Entry]\nType=Application\nName=Cap\nIcon=Cap\nExec=env {}\nNoDisplay=true\nStartupWMClass=Cap\n",
+ exec_argument(&exec.to_string_lossy())
+ )
+}
+
+/// One quoted `Exec` argument. Inside quotes `"`, `` ` ``, `$` and `\` take a
+/// backslash; the string escape then doubles every backslash, and `%` (a
+/// field code anywhere in the line) becomes `%%`.
+fn exec_argument(value: &str) -> String {
+ let mut quoted = String::with_capacity(value.len() + 2);
+ for c in value.chars() {
+ if matches!(c, '"' | '`' | '$' | '\\') {
+ quoted.push('\\');
+ }
+ quoted.push(c);
+ }
+ format!("\"{}\"", quoted.replace('\\', "\\\\").replace('%', "%%"))
+}
+
+#[derive(Debug, PartialEq, Eq)]
+pub enum DesktopEntry {
+ Found,
+ Written,
+ Unavailable,
+}
+
+/// Finds or writes the `.desktop` file for [`PORTAL_APP_ID`].
+pub fn ensure_desktop_entry(user_dir: &Path, dirs: &[PathBuf], exec: &Path) -> DesktopEntry {
+ let file = format!("{PORTAL_APP_ID}.desktop");
+ if dirs.iter().any(|dir| dir.join(&file).is_file()) {
+ return DesktopEntry::Found;
+ }
+ let path = user_dir.join(&file);
+ let written =
+ std::fs::create_dir_all(user_dir).and_then(|()| std::fs::write(&path, desktop_entry(exec)));
+ match written {
+ Ok(()) => DesktopEntry::Written,
+ Err(error) => {
+ tracing::info!(path = %path.display(), %error, "could not write the portal app id's desktop entry");
+ DesktopEntry::Unavailable
+ }
+ }
+}
+
+/// The portal can look an entry up before its index has seen the new file and
+/// answer "App info not found". Delays before each retry of a new entry.
+const NEW_ENTRY_RETRY_DELAYS: [Duration; 4] = [
+ Duration::from_millis(50),
+ Duration::from_millis(150),
+ Duration::from_millis(400),
+ Duration::from_millis(1000),
+];
+
+fn app_info_not_found(error: &str) -> bool {
+ error.contains("App info not found")
+}
+
+/// Registers [`PORTAL_APP_ID`] when the portal has no app id for Cap. Must
+/// run before any portal call on the process's portal connection, gpui's
+/// appearance read included, and on the runtime that keeps that connection.
+pub async fn register() {
+ // The desktop harness turns this off to keep covering the tray Stop
+ // control, which a terminal launch otherwise no longer needs.
+ if std::env::var_os("CAP_GPUI_PORTAL_APP_ID").is_some_and(|value| value == "0") {
+ return;
+ }
+ let cgroup = std::fs::read_to_string("/proc/self/cgroup").unwrap_or_default();
+ if let Some(id) = unit_app_id(&cgroup)
+ && (id == PACKAGED_APP_ID || id == PORTAL_APP_ID)
+ {
+ tracing::info!(%id, "portals know Cap from its launcher's unit");
+ return;
+ }
+ let exec = std::env::var_os("APPIMAGE")
+ .map(PathBuf::from)
+ .or_else(|| std::env::current_exe().ok());
+ let Some(exec) = exec else {
+ return;
+ };
+ let Some((user_dir, dirs)) = application_dirs(
+ std::env::var_os("XDG_DATA_HOME"),
+ std::env::var_os("XDG_DATA_DIRS"),
+ std::env::var_os("HOME"),
+ ) else {
+ return;
+ };
+ let entry = ensure_desktop_entry(&user_dir, &dirs, &exec);
+ if entry == DesktopEntry::Unavailable {
+ return;
+ }
+ let Ok(app_id) = ashpd::AppID::try_from(PORTAL_APP_ID) else {
+ return;
+ };
+ let mut retries = match entry {
+ DesktopEntry::Written => NEW_ENTRY_RETRY_DELAYS.as_slice(),
+ _ => &[],
+ }
+ .iter();
+ let mut attempt = 1;
+ loop {
+ match tokio::time::timeout(
+ Duration::from_secs(3),
+ ashpd::register_host_app(app_id.clone()),
+ )
+ .await
+ {
+ Ok(Ok(())) => {
+ REGISTERED.store(true, std::sync::atomic::Ordering::Release);
+ tracing::info!(
+ app_id = PORTAL_APP_ID,
+ attempt,
+ "registered Cap's app id with the portals"
+ );
+ return;
+ }
+ Ok(Err(error)) => {
+ let error = error.to_string();
+ if app_info_not_found(&error)
+ && let Some(delay) = retries.next()
+ {
+ tokio::time::sleep(*delay).await;
+ attempt += 1;
+ continue;
+ }
+ tracing::info!(%error, attempt, "the portals did not take Cap's app id");
+ return;
+ }
+ Err(_) => {
+ tracing::info!("the portal registry did not answer");
+ return;
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn launcher_units_name_the_app_the_way_the_portal_reads_them() {
+ assert_eq!(
+ unit_app_id("0::/user.slice/user-1000.slice/[email protected]/app.slice/app-gnome-Cap-4721.scope").as_deref(),
+ Some("Cap")
+ );
+ assert_eq!(
+ unit_app_id("0::/user.slice/user-1000.slice/[email protected]/app.slice/app-so.cap.Cap-21974.scope").as_deref(),
+ Some("so.cap.Cap")
+ );
+ assert_eq!(
+ unit_app_id("0::/user.slice/user-1000.slice/[email protected]/app.slice/[email protected]").as_deref(),
+ Some("org.gnome.Ptyxis")
+ );
+ // A terminal's tab, a login shell, an AppImage started from either.
+ assert_eq!(
+ unit_app_id(
+ "0::/user.slice/user-1000.slice/[email protected]/app.slice/vte-spawn-8d1c.scope"
+ ),
+ None
+ );
+ assert_eq!(
+ unit_app_id("0::/user.slice/user-1000.slice/session-2.scope"),
+ None
+ );
+ assert_eq!(unit_app_id(""), None);
+ }
+
+ #[test]
+ fn the_desktop_entry_is_written_once_and_found_anywhere_on_the_data_path() {
+ let root = tempfile::tempdir().unwrap();
+ let (user, dirs) = application_dirs(
+ None,
+ Some(root.path().join("system").into_os_string()),
+ Some(root.path().join("home").into_os_string()),
+ )
+ .unwrap();
+ assert_eq!(user, root.path().join("home/.local/share/applications"));
+ let exec = Path::new(r#"/opt/100% Cap/"beta" $HOME `x`\.AppImage"#);
+ assert_eq!(ensure_desktop_entry(&user, &dirs, exec), DesktopEntry::Written);
+ assert_eq!(ensure_desktop_entry(&user, &dirs, exec), DesktopEntry::Found);
+ let entry = std::fs::read_to_string(user.join("so.cap.Cap.desktop")).unwrap();
+ assert!(entry.contains("NoDisplay=true"));
+ assert!(entry.contains(
+ r#"Exec=env "/opt/100%% Cap/\\"beta\\" \\$HOME \\`x\\`\\\\.AppImage""#
+ ));
+
+ // An entry a package installed wins: nothing is written for the user.
+ let packaged = tempfile::tempdir().unwrap();
+ std::fs::create_dir_all(packaged.path().join("applications")).unwrap();
+ std::fs::write(packaged.path().join("applications/so.cap.Cap.desktop"), "x").unwrap();
+ let home = tempfile::tempdir().unwrap();
+ let (user, dirs) = application_dirs(
+ Some(home.path().join("data").into_os_string()),
+ Some(packaged.path().as_os_str().to_owned()),
+ None,
+ )
+ .unwrap();
+ assert_eq!(ensure_desktop_entry(&user, &dirs, exec), DesktopEntry::Found);
+ assert!(!user.join("so.cap.Cap.desktop").exists());
+
+ // Without a home there is nowhere safe to write.
+ assert!(application_dirs(None, None, None).is_none());
+ assert!(application_dirs(Some("relative".into()), None, None).is_none());
+ }
+
+ #[test]
+ fn windows_follow_the_portal_id_only_on_gnome() {
+ assert!(is_gnome("ubuntu:GNOME"));
+ assert!(is_gnome("GNOME"));
+ assert!(is_gnome("GNOME-Classic:GNOME"));
+ assert!(!is_gnome("KDE"));
+ assert!(!is_gnome("Hyprland"));
+ assert!(!is_gnome("niri"));
+ assert!(!is_gnome(""));
+ }
+
+ #[test]
+ fn only_a_missing_entry_is_retried() {
+ assert!(app_info_not_found(
+ "ZBus Error: org.freedesktop.portal.Error.Failed: Could not register app ID: App info not found for 'so.cap.Cap'"
+ ));
+ assert!(!app_info_not_found(
+ "ZBus Error: org.freedesktop.portal.Error.Failed: Could not register app ID: Connection already associated with an application ID"
+ ));
+ assert!(!app_info_not_found(
+ "ZBus Error: org.freedesktop.DBus.Error.UnknownMethod: No such interface"
+ ));
+ }
+}
diff --git a/apps/desktop-gpui/src/linux_wallpaper.rs b/apps/desktop-gpui/src/linux_wallpaper.rs
new file mode 100644
index 000000000..837b5fcad
--- /dev/null
+++ b/apps/desktop-gpui/src/linux_wallpaper.rs
@@ -0,0 +1,598 @@
+//! The desktop's wallpaper on Linux, for the editor's "Use the wallpaper from
+//! your desktop" background. Every desktop keeps it somewhere else: GNOME and
+//! its forks in GSettings, Plasma in its applets config, XFCE in xfconf, COSMIC
+//! in its config dir, and wlroots compositors in whichever program draws it
+//! (swaybg, swww, hyprpaper).
+
+use std::{
+ io::Read,
+ path::{Path, PathBuf},
+ process::{Command, Stdio},
+ time::{Duration, Instant},
+};
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum Source {
+ Gnome,
+ Cinnamon,
+ Mate,
+ Plasma,
+ Xfce,
+ Cosmic,
+ Swaybg,
+ Swww,
+ Hyprpaper,
+}
+
+/// Where to look, in order, for an `XDG_CURRENT_DESKTOP` value. GSettings is
+/// only read for desktops that use it: elsewhere its schema still answers,
+/// with GNOME's default picture rather than the one on screen.
+fn sources(current_desktop: &str) -> Vec<Source> {
+ let desktops: Vec<String> = current_desktop
+ .split(':')
+ .map(|desktop| desktop.trim().to_ascii_lowercase())
+ .collect();
+ let has = |names: &[&str]| desktops.iter().any(|desktop| names.contains(&desktop.as_str()));
+ let mut sources = Vec::new();
+ if has(&["kde"]) {
+ sources.push(Source::Plasma);
+ }
+ if has(&["x-cinnamon", "cinnamon"]) {
+ sources.push(Source::Cinnamon);
+ }
+ if has(&["mate"]) {
+ sources.push(Source::Mate);
+ }
+ if has(&["xfce"]) {
+ sources.push(Source::Xfce);
+ }
+ if has(&["cosmic"]) {
+ sources.push(Source::Cosmic);
+ }
+ if sources.is_empty() && has(&["gnome", "unity", "budgie", "pantheon", "ubuntu"]) {
+ sources.push(Source::Gnome);
+ }
+ sources.extend([Source::Swaybg, Source::Swww, Source::Hyprpaper]);
+ sources
+}
+
+pub fn current() -> Option<PathBuf> {
+ let desktop = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default();
+ let config = std::env::var_os("XDG_CONFIG_HOME")
+ .map(PathBuf::from)
+ .filter(|dir| dir.is_absolute())
+ .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".config")));
+ for source in sources(&desktop) {
+ let found = match source {
+ Source::Gnome => gnome(),
+ Source::Cinnamon => gsettings("org.cinnamon.desktop.background", "picture-uri")
+ .and_then(|value| file_uri_path(&value)),
+ Source::Mate => gsettings("org.mate.background", "picture-filename")
+ .filter(|value| !value.is_empty())
+ .map(PathBuf::from),
+ Source::Plasma => config.as_deref().and_then(plasma),
+ Source::Xfce => xfce(),
+ Source::Cosmic => config.as_deref().and_then(cosmic),
+ Source::Swaybg => swaybg(),
+ Source::Swww => run("swww", &["query"]).and_then(|out| swww_image(&out)),
+ Source::Hyprpaper => std::env::var_os("HYPRLAND_INSTANCE_SIGNATURE")
+ .and_then(|_| run("hyprctl", &["hyprpaper", "listactive"]))
+ .and_then(|out| hyprpaper_image(&out)),
+ };
+ if let Some(path) = found.and_then(|path| resolve(&path)) {
+ tracing::info!(?source, path = %path.display(), "found the desktop wallpaper");
+ return Some(path);
+ }
+ }
+ tracing::info!(%desktop, "found no desktop wallpaper");
+ None
+}
+
+fn resolve(path: &Path) -> Option<PathBuf> {
+ if path.is_dir() {
+ return wallpaper_package_image(path);
+ }
+ if is_xml(path) {
+ // One step only: a slideshow that names another slideshow, or
+ // itself, is not followed.
+ let xml = std::fs::read_to_string(path).ok()?;
+ return slideshow_image(&xml).filter(|image| image.is_file() && !is_xml(image));
+ }
+ path.is_file().then(|| path.to_path_buf())
+}
+
+fn is_xml(path: &Path) -> bool {
+ path.extension().is_some_and(|extension| extension == "xml")
+}
+
+/// Runs a desktop's settings tool for one value. Two seconds at most: the
+/// wallpaper is never worth a stuck editor.
+fn run(program: &str, args: &[&str]) -> Option<String> {
+ let mut child = Command::new(program)
+ .args(args)
+ .stdin(Stdio::null())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::null())
+ .spawn()
+ .ok()?;
+ let deadline = Instant::now() + Duration::from_secs(2);
+ loop {
+ match child.try_wait() {
+ Ok(Some(status)) if status.success() => break,
+ Ok(Some(_)) => return None,
+ Ok(None) if Instant::now() < deadline => std::thread::sleep(Duration::from_millis(20)),
+ _ => {
+ let _ = child.kill();
+ let _ = child.wait();
+ return None;
+ }
+ }
+ }
+ let mut out = String::new();
+ child.stdout.take()?.read_to_string(&mut out).ok()?;
+ Some(out)
+}
+
+fn gsettings(schema: &str, key: &str) -> Option<String> {
+ run("gsettings", &["get", schema, key]).and_then(|out| gvariant_string(&out))
+}
+
+/// GNOME shows `picture-uri-dark` under the dark style, when it is set.
+fn gnome() -> Option<PathBuf> {
+ let dark = gsettings("org.gnome.desktop.interface", "color-scheme")
+ .is_some_and(|scheme| scheme == "prefer-dark");
+ let schema = "org.gnome.desktop.background";
+ dark.then(|| gsettings(schema, "picture-uri-dark"))
+ .flatten()
+ .and_then(|value| file_uri_path(&value))
+ .or_else(|| gsettings(schema, "picture-uri").and_then(|value| file_uri_path(&value)))
+}
+
+/// `gsettings get` prints a GVariant: `'file:///a/b.png'`, quotes escaped.
+fn gvariant_string(printed: &str) -> Option<String> {
+ let printed = printed.trim();
+ let inner = printed
+ .strip_prefix('\'')
+ .and_then(|rest| rest.strip_suffix('\''))
+ .or_else(|| printed.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')))?;
+ let mut value = String::with_capacity(inner.len());
+ let mut chars = inner.chars();
+ while let Some(c) = chars.next() {
+ if c == '\\' {
+ value.extend(chars.next());
+ } else {
+ value.push(c);
+ }
+ }
+ Some(value)
+}
+
+fn file_uri_path(value: &str) -> Option<PathBuf> {
+ let value = value.trim();
+ if value.starts_with('/') {
+ return Some(PathBuf::from(value));
+ }
+ let rest = value.strip_prefix("file://")?;
+ let path = rest.strip_prefix("localhost").unwrap_or(rest);
+ if !path.starts_with('/') {
+ return None;
+ }
+ let bytes = path.as_bytes();
+ let mut decoded = Vec::with_capacity(bytes.len());
+ let mut index = 0;
+ while index < bytes.len() {
+ if bytes[index] == b'%'
+ && let Some(byte) = bytes
+ .get(index + 1..index + 3)
+ .and_then(|hex| std::str::from_utf8(hex).ok())
+ .and_then(|hex| u8::from_str_radix(hex, 16).ok())
+ {
+ decoded.push(byte);
+ index += 3;
+ } else {
+ decoded.push(bytes[index]);
+ index += 1;
+ }
+ }
+ use std::os::unix::ffi::OsStringExt;
+ Some(PathBuf::from(std::ffi::OsString::from_vec(decoded)))
+}
+
+/// The first `Image=` of a desktop containment's wallpaper; Plasma's own
+/// default package when it has none.
+fn plasma(config: &Path) -> Option<PathBuf> {
+ let text = std::fs::read_to_string(config.join("plasma-org.kde.plasma.desktop-appletsrc"))
+ .unwrap_or_default();
+ plasma_image(&text).or_else(|| {
+ let default = PathBuf::from("/usr/share/wallpapers/Next");
+ default.is_dir().then_some(default)
+ })
+}
+
+fn plasma_image(appletsrc: &str) -> Option<PathBuf> {
+ let mut in_image_section = false;
+ for line in appletsrc.lines() {
+ let line = line.trim();
+ if line.starts_with('[') {
+ in_image_section = line.starts_with("[Containments]")
+ && line.ends_with("[Wallpaper][org.kde.image][General]");
+ continue;
+ }
+ if in_image_section
+ && let Some(value) = line.strip_prefix("Image=")
+ && let Some(path) = file_uri_path(value)
+ {
+ return Some(path);
+ }
+ }
+ None
+}
+
+fn wallpaper_package_image(package: &Path) -> Option<PathBuf> {
+ let images = std::fs::read_dir(package.join("contents/images")).ok()?;
+ images
+ .filter_map(Result::ok)
+ .map(|entry| entry.path())
+ .filter(|path| path.is_file())
+ .max_by_key(|path| {
+ path.file_stem()
+ .and_then(|stem| stem.to_str())
+ .and_then(|stem| stem.split_once('x'))
+ .and_then(|(width, height)| Some(width.parse::<u64>().ok()? * height.parse::<u64>().ok()?))
+ .unwrap_or(0)
+ })
+}
+
+fn slideshow_image(xml: &str) -> Option<PathBuf> {
+ let mut best: Option<(u64, &str)> = None;
+ for part in xml.split("<size").skip(1) {
+ let Some((attributes, rest)) = part.split_once('>') else {
+ continue;
+ };
+ let Some((path, _)) = rest.split_once("</size>") else {
+ continue;
+ };
+ let attribute = |name: &str| {
+ attributes
+ .split_once(&format!("{name}=\""))
+ .and_then(|(_, value)| value.split_once('"'))
+ .and_then(|(value, _)| value.parse::<u64>().ok())
+ .unwrap_or(0)
+ };
+ let area = attribute("width") * attribute("height");
+ if best.is_none_or(|(largest, _)| area > largest) {
+ best = Some((area, path.trim()));
+ }
+ }
+ if let Some((_, path)) = best {
+ return Some(PathBuf::from(xml_text(path)));
+ }
+ let (_, rest) = xml.split_once("<file>")?;
+ let (path, _) = rest.split_once("</file>")?;
+ Some(PathBuf::from(xml_text(path.trim())))
+}
+
+fn xml_text(text: &str) -> String {
+ let mut decoded = String::with_capacity(text.len());
+ let mut rest = text;
+ while let Some(start) = rest.find('&') {
+ decoded.push_str(&rest[..start]);
+ rest = &rest[start..];
+ let entity = rest.find(';').map(|end| (&rest[1..end], end));
+ let character = entity.and_then(|(name, _)| match name {
+ "amp" => Some('&'),
+ "lt" => Some('<'),
+ "gt" => Some('>'),
+ "quot" => Some('"'),
+ "apos" => Some('\''),
+ _ => name
+ .strip_prefix("#x")
+ .or_else(|| name.strip_prefix("#X"))
+ .map(|hex| u32::from_str_radix(hex, 16))
+ .or_else(|| name.strip_prefix('#').map(str::parse::<u32>))
+ .and_then(Result::ok)
+ .and_then(char::from_u32),
+ });
+ match (character, entity) {
+ (Some(character), Some((_, end))) => {
+ decoded.push(character);
+ rest = &rest[end + 1..];
+ }
+ _ => {
+ decoded.push('&');
+ rest = &rest[1..];
+ }
+ }
+ }
+ decoded.push_str(rest);
+ decoded
+}
+
+fn xfce() -> Option<PathBuf> {
+ let properties = run("xfconf-query", &["-c", "xfce4-desktop", "-l"])?;
+ let mut images: Vec<&str> = properties
+ .lines()
+ .map(str::trim)
+ .filter(|property| property.ends_with("/last-image"))
+ .collect();
+ images.sort_unstable();
+ images.into_iter().find_map(|property| {
+ run("xfconf-query", &["-c", "xfce4-desktop", "-p", property])
+ .map(|value| PathBuf::from(value.trim()))
+ .filter(|path| path.is_absolute())
+ })
+}
+
+fn cosmic(config: &Path) -> Option<PathBuf> {
+ let text =
+ std::fs::read_to_string(config.join("cosmic/com.system76.CosmicBackground/v1/all")).ok()?;
+ cosmic_image(&text)
+}
+
+fn cosmic_image(entry: &str) -> Option<PathBuf> {
+ let (_, rest) = entry.split_once("Path(\"")?;
+ let (path, _) = rest.split_once("\")")?;
+ Some(PathBuf::from(path))
+}
+
+/// The picture swaybg draws, as Omarchy runs it. Only a swaybg of this user,
+/// on this Wayland display when both name one: another session's swaybg
+/// shows another session's wallpaper.
+fn swaybg() -> Option<PathBuf> {
+ use std::os::unix::fs::MetadataExt;
+ let uid = std::fs::metadata("/proc/self").ok()?.uid();
+ let display = std::env::var_os("WAYLAND_DISPLAY");
+ for entry in std::fs::read_dir("/proc").ok()?.filter_map(Result::ok) {
+ let dir = entry.path();
+ if std::fs::read_to_string(dir.join("comm")).is_ok_and(|comm| comm.trim() == "swaybg")
+ && std::fs::metadata(&dir).is_ok_and(|metadata| metadata.uid() == uid)
+ && same_display(
+ display.as_deref(),
+ std::fs::read(dir.join("environ")).ok().as_deref(),
+ )
+ && let Ok(cmdline) = std::fs::read(dir.join("cmdline"))
+ && let Some(image) = swaybg_image(&cmdline)
+ {
+ if image.is_absolute() {
+ return Some(image);
+ }
+ if let Ok(cwd) = std::fs::read_link(dir.join("cwd")) {
+ return Some(cwd.join(image));
+ }
+ }
+ }
+ None
+}
+
+fn same_display(ours: Option<&std::ffi::OsStr>, environ: Option<&[u8]>) -> bool {
+ use std::os::unix::ffi::OsStrExt;
+ let theirs = environ.and_then(|environ| {
+ environ
+ .split(|byte| *byte == 0)
+ .find_map(|variable| variable.strip_prefix(b"WAYLAND_DISPLAY="))
+ });
+ match (ours, theirs) {
+ (Some(ours), Some(theirs)) => ours.as_bytes() == theirs,
+ _ => true,
+ }
+}
+
+fn swaybg_image(cmdline: &[u8]) -> Option<PathBuf> {
+ let mut args = cmdline
+ .split(|byte| *byte == 0)
+ .map(|arg| String::from_utf8_lossy(arg).into_owned());
+ while let Some(arg) = args.next() {
+ if arg == "-i" || arg == "--image" {
+ return args.next().map(PathBuf::from);
+ }
+ if let Some(path) = arg.strip_prefix("--image=") {
+ return Some(PathBuf::from(path));
+ }
+ }
+ None
+}
+
+fn swww_image(query: &str) -> Option<PathBuf> {
+ query.lines().find_map(|line| {
+ let (_, path) = line.split_once("image: ")?;
+ let path = PathBuf::from(path.trim());
+ path.is_absolute().then_some(path)
+ })
+}
+
+fn hyprpaper_image(listed: &str) -> Option<PathBuf> {
+ listed.lines().find_map(|line| {
+ let (_, path) = line.split_once(" = ")?;
+ let path = PathBuf::from(path.trim());
+ path.is_absolute().then_some(path)
+ })
+}
+
+/// JPEG XL, GNOME's own wallpaper format, which the image crate cannot read.
+/// `None` for any other file.
+pub fn decode_jpeg_xl(path: &Path) -> Option<Result<image::DynamicImage, String>> {
+ let mut file = std::fs::File::open(path).ok()?;
+ let mut signature = [0u8; 12];
+ let read = file.read(&mut signature).ok()?;
+ if !is_jpeg_xl(&signature[..read]) {
+ return None;
+ }
+ let file = std::fs::File::open(path).ok()?;
+ Some(
+ jxl_oxide::integration::JxlDecoder::new(std::io::BufReader::new(file))
+ .and_then(image::DynamicImage::from_decoder)
+ .map_err(|error| format!("Failed to decode JPEG XL image: {error}")),
+ )
+}
+
+fn is_jpeg_xl(head: &[u8]) -> bool {
+ head.starts_with(&[0xFF, 0x0A])
+ || head.starts_with(&[0, 0, 0, 0x0C, b'J', b'X', b'L', b' ', 0x0D, 0x0A, 0x87, 0x0A])
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn each_desktop_reads_its_own_setting_then_the_wallpaper_programs() {
+ let programs = [Source::Swaybg, Source::Swww, Source::Hyprpaper];
+ let with = |first: &[Source]| [first, &programs[..]].concat();
+ assert_eq!(sources("ubuntu:GNOME"), with(&[Source::Gnome]));
+ assert_eq!(sources("Budgie:GNOME"), with(&[Source::Gnome]));
+ assert_eq!(sources("KDE"), with(&[Source::Plasma]));
+ assert_eq!(sources("X-Cinnamon"), with(&[Source::Cinnamon]));
+ assert_eq!(sources("MATE"), with(&[Source::Mate]));
+ assert_eq!(sources("XFCE"), with(&[Source::Xfce]));
+ assert_eq!(sources("COSMIC"), with(&[Source::Cosmic]));
+ // Hyprland, niri, sway: GSettings would answer with GNOME's default.
+ assert_eq!(sources("Hyprland"), with(&[]));
+ assert_eq!(sources("niri"), with(&[]));
+ assert_eq!(sources(""), with(&[]));
+ }
+
+ #[test]
+ fn gsettings_values_and_file_uris_become_paths() {
+ assert_eq!(
+ gvariant_string("'file:///usr/share/backgrounds/gnome/adwaita-l.jxl'\n").as_deref(),
+ Some("file:///usr/share/backgrounds/gnome/adwaita-l.jxl")
+ );
+ assert_eq!(gvariant_string(r"'it\'s'").as_deref(), Some("it's"));
+ assert_eq!(gvariant_string("''").as_deref(), Some(""));
+ assert_eq!(gvariant_string("@as []"), None);
+ assert_eq!(
+ file_uri_path("file:///home/a/My%20Pictures/caf%C3%A9.png"),
+ Some(PathBuf::from("/home/a/My Pictures/café.png"))
+ );
+ assert_eq!(
+ file_uri_path("file://localhost/a.png"),
+ Some(PathBuf::from("/a.png"))
+ );
+ assert_eq!(file_uri_path("/a/b.png"), Some(PathBuf::from("/a/b.png")));
+ assert_eq!(file_uri_path("100%.png"), None);
+ assert_eq!(file_uri_path("https://example.com/a.png"), None);
+ assert_eq!(file_uri_path(""), None);
+ }
+
+ #[test]
+ fn plasma_reads_the_desktop_containments_image() {
+ let appletsrc = "\
+[Containments][1][General]
+Image=file:///not/this.png
+
+[Containments][2][Wallpaper][org.kde.image][General]
+Image=file:///usr/share/wallpapers/Mountain/
+SlidePaths=/usr/share/wallpapers/
+
+[Containments][3][Wallpaper][org.kde.image][General]
+Image=/home/a/second.png
+";
+ assert_eq!(
+ plasma_image(appletsrc),
+ Some(PathBuf::from("/usr/share/wallpapers/Mountain/"))
+ );
+ assert_eq!(plasma_image("[Containments][2][Applets]\nImage=/x.png\n"), None);
+ }
+
+ #[test]
+ fn wallpaper_packages_and_slideshows_give_their_largest_picture() {
+ let root = tempfile::tempdir().unwrap();
+ let images = root.path().join("Next/contents/images");
+ std::fs::create_dir_all(&images).unwrap();
+ for name in ["1920x1080.png", "5120x2880.png", "1366x768.png", "screenshot.png"] {
+ std::fs::write(images.join(name), b"x").unwrap();
+ }
+ assert_eq!(
+ resolve(&root.path().join("Next")),
+ Some(images.join("5120x2880.png"))
+ );
+
+ let slideshow = format!(
+ "<wallpapers><wallpaper><filename>{}</filename></wallpaper></wallpapers>\
+ <background><static><file>\
+ <size width=\"1024\" height=\"768\">/small.png</size>\
+ <size width=\"3840\" height=\"2160\">{}</size>\
+ </file></static></background>",
+ "/ignored.png",
+ images.join("1920x1080.png").display()
+ );
+ let xml = root.path().join("show.xml");
+ std::fs::write(&xml, slideshow).unwrap();
+ assert_eq!(resolve(&xml), Some(images.join("1920x1080.png")));
+ assert_eq!(
+ slideshow_image("<static><file>\n /a/day.jpg\n</file></static>"),
+ Some(PathBuf::from("/a/day.jpg"))
+ );
+ assert_eq!(resolve(&root.path().join("missing.png")), None);
+
+ // A slideshow naming itself or another slideshow stops there.
+ let looping = root.path().join("loop.xml");
+ std::fs::write(
+ &looping,
+ format!("<static><file>{}</file></static>", looping.display()),
+ )
+ .unwrap();
+ assert_eq!(resolve(&looping), None);
+ }
+
+ #[test]
+ fn slideshow_paths_decode_xml_text() {
+ assert_eq!(
+ slideshow_image("<static><file>/a/Sea &amp; Sky &#233;t&#xE9;.jpg</file></static>"),
+ Some(PathBuf::from("/a/Sea & Sky été.jpg"))
+ );
+ assert_eq!(xml_text("&lt;&gt;&quot;&apos;"), "<>\"'");
+ assert_eq!(xml_text("AT&T &bogus; 5 & 6"), "AT&T &bogus; 5 & 6");
+ }
+
+ #[test]
+ fn swaybg_is_only_read_on_this_wayland_display() {
+ let ours = Some(std::ffi::OsStr::new("wayland-1"));
+ assert!(same_display(ours, Some(b"HOME=/h\0WAYLAND_DISPLAY=wayland-1\0")));
+ assert!(!same_display(ours, Some(b"WAYLAND_DISPLAY=wayland-0\0")));
+ assert!(same_display(ours, Some(b"HOME=/h\0")));
+ assert!(same_display(ours, None));
+ assert!(same_display(None, Some(b"WAYLAND_DISPLAY=wayland-0\0")));
+ }
+
+ #[test]
+ fn wallpaper_programs_name_their_picture() {
+ assert_eq!(
+ swaybg_image(b"swaybg\0-i\0/home/a/.config/omarchy/current/background\0-m\0fill\0"),
+ Some(PathBuf::from("/home/a/.config/omarchy/current/background"))
+ );
+ assert_eq!(
+ swaybg_image(b"swaybg\0-o\0DP-1\0--image=/a.png\0"),
+ Some(PathBuf::from("/a.png"))
+ );
+ assert_eq!(swaybg_image(b"swaybg\0-c\0#000000\0"), None);
+ assert_eq!(
+ swww_image("eDP-1: 2880x1800, scale: 2, currently displaying: image: /w/a.jpg\n"),
+ Some(PathBuf::from("/w/a.jpg"))
+ );
+ assert_eq!(
+ swww_image("eDP-1: 2880x1800, scale: 2, currently displaying: color: 000000\n"),
+ None
+ );
+ assert_eq!(
+ hyprpaper_image("eDP-1 = /w/b.png\nDP-2 = /w/c.png\n"),
+ Some(PathBuf::from("/w/b.png"))
+ );
+ assert_eq!(hyprpaper_image("no wallpapers active\n"), None);
+ assert_eq!(
+ cosmic_image("(\n output: \"all\",\n source: Path(\"/usr/share/backgrounds/cosmic/a.jpg\"),\n)"),
+ Some(PathBuf::from("/usr/share/backgrounds/cosmic/a.jpg"))
+ );
+ }
+
+ #[test]
+ fn jpeg_xl_is_recognised_by_its_signature() {
+ assert!(is_jpeg_xl(&[0xFF, 0x0A, 0x00]));
+ assert!(is_jpeg_xl(&[
+ 0, 0, 0, 0x0C, b'J', b'X', b'L', b' ', 0x0D, 0x0A, 0x87, 0x0A
+ ]));
+ assert!(!is_jpeg_xl(&[0x89, b'P', b'N', b'G']));
+ assert!(!is_jpeg_xl(&[0xFF, 0xD8, 0xFF]));
+ assert!(!is_jpeg_xl(&[]));
+ }
+}
diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs
index ebeaeec01..bea07dbc5 100644
--- a/apps/desktop-gpui/src/main.rs
+++ b/apps/desktop-gpui/src/main.rs
@@ -50,6 +50,12 @@ mod jpeg_thumbnail;
mod library;
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
mod linux_dialog;
+#[cfg(target_os = "linux")]
+mod linux_display;
+#[cfg(target_os = "linux")]
+mod linux_portal_id;
+#[cfg(target_os = "linux")]
+mod linux_wallpaper;
mod main_window;
mod media_access;
mod menus;
@@ -158,6 +164,12 @@ fn end_windows_session() {
crash_capture::mark_clean_exit();
}
+/// zbus warns whenever a portal request object is gone before its
+/// properties are cached, which every finished portal request is; at `warn`
+/// that filled each Linux log with lines that say nothing.
+const DEFAULT_LOG_FILTER: &str =
+ "warn,cap_gpui=info,wgpu_core=error,wgpu_hal=error,zbus::proxy=error";
+
/// stdout, plus a rolling daily file the Feedback page can upload.
///
/// Mirrors `src-tauri/src/main.rs`: `tracing_appender::rolling::daily` into
@@ -173,7 +185,7 @@ fn init_logging() -> Option<tracing_appender::non_blocking::WorkerGuard> {
// matches nothing.
let filter = || {
tracing_subscriber::EnvFilter::try_from_default_env()
- .unwrap_or_else(|_| "warn,cap_gpui=info,wgpu_core=error,wgpu_hal=error".into())
+ .unwrap_or_else(|_| DEFAULT_LOG_FILTER.into())
};
let file = create_log_appender(&diagnostics::logs_dir(), diagnostics::LOG_FILE_PREFIX).map(
@@ -283,6 +295,17 @@ fn main() {
}
}
+ #[cfg(target_os = "linux")]
+ let stale_wayland_display = linux_display::stale_wayland_display();
+ #[cfg(target_os = "linux")]
+ if stale_wayland_display.is_some() {
+ // gpui and screen capture both choose Wayland from this variable; drop
+ // it before logging starts threads.
+ unsafe {
+ std::env::remove_var("WAYLAND_DISPLAY");
+ }
+ }
+
// The AppImage CLI launcher re-enters the AppImage with `--cap-cli`; hand
// it to the bundled CLI before logging, single-instance or the GUI start.
#[cfg(target_os = "linux")]
@@ -301,6 +324,13 @@ fn main() {
let _log_guard = init_logging();
crash_capture::install(&diagnostics::logs_dir());
+ #[cfg(target_os = "linux")]
+ if let Some(wayland_display) = stale_wayland_display {
+ tracing::warn!(
+ wayland_display = %wayland_display.to_string_lossy(),
+ "WAYLAND_DISPLAY names no running compositor; using the X display"
+ );
+ }
single_instance::acquire();
let previous_session = crash_capture::begin_session(&diagnostics::logs_dir());
@@ -318,6 +348,12 @@ fn main() {
}
}
+ let runtime = app_runtime();
+ // Before gpui exists: its appearance read is a portal call, and the
+ // portals only take an app id before the first one.
+ #[cfg(target_os = "linux")]
+ runtime.block_on(linux_portal_id::register());
+
let app = gpui_platform::application().with_assets(Assets);
// `RunEvent::Reopen`: the dock icon clicked while the app runs. Must be
// registered on the builder -- gpui exposes it nowhere else -- with the
@@ -329,7 +365,6 @@ fn main() {
crate::deeplink::submit_deep_link(&url);
}
});
- let runtime = app_runtime();
let runtime_handle = runtime.handle().clone();
app.run(move |cx: &mut App| {
@@ -401,7 +436,7 @@ fn main() {
..Default::default()
}),
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
#[cfg(target_os = "linux")]
window_min_size: Some(size(px(MAIN_WINDOW_WIDTH), px(MAIN_WINDOW_HEIGHT))),
#[cfg(target_os = "linux")]
@@ -578,6 +613,14 @@ fn main() {
})
.expect("failed to start device enumeration");
+ // Before the launch thumbnail sweep can reach ReplayKit, which would
+ // otherwise add the standard titlebar buttons next to the hand-drawn
+ // close light (`platform::lock_borderless_style`).
+ #[cfg(target_os = "macos")]
+ if let Some(native) = &native_main {
+ platform::lock_borderless_style(native);
+ }
+
// The native window material: `NSGlassEffectView` on macOS 26+,
// `NSVisualEffectView` vibrancy below that -- what
// `applyMacOSWindowMaterial("panel")` does in the Tauri app. Nothing
@@ -851,6 +894,7 @@ mod runtime_tests {
mod logging_tests {
use super::create_log_appender;
use std::{io::Write, path::PathBuf};
+ use tracing_subscriber::Layer as _;
struct LogDirectory(PathBuf);
@@ -873,6 +917,52 @@ mod logging_tests {
}
}
+ #[test]
+ fn the_default_filter_drops_zbus_property_cache_warnings_only() {
+ use std::sync::{Arc, Mutex};
+ use tracing_subscriber::layer::SubscriberExt as _;
+
+ #[derive(Clone, Default)]
+ struct Lines(Arc<Mutex<Vec<u8>>>);
+ impl Write for Lines {
+ fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
+ self.0.lock().unwrap().extend_from_slice(bytes);
+ Ok(bytes.len())
+ }
+ fn flush(&mut self) -> std::io::Result<()> {
+ Ok(())
+ }
+ }
+
+ let lines = Lines::default();
+ let writer = lines.clone();
+ let subscriber = tracing_subscriber::registry().with(
+ tracing_subscriber::fmt::layer()
+ .with_ansi(false)
+ .with_writer(move || writer.clone())
+ .with_filter(tracing_subscriber::EnvFilter::new(
+ super::DEFAULT_LOG_FILTER,
+ )),
+ );
+ tracing::subscriber::with_default(subscriber, || {
+ tracing::warn!(target: "zbus::proxy", "Failed to populate properties cache");
+ tracing::error!(target: "zbus::proxy", "a zbus error still shows");
+ tracing::warn!(target: "zbus::connection", "other zbus warnings still show");
+ tracing::info!(target: "cap_gpui::session", "recording started");
+ });
+ let logged = String::from_utf8(lines.0.lock().unwrap().clone()).unwrap();
+ assert!(
+ !logged.contains("Failed to populate properties cache"),
+ "{logged}"
+ );
+ assert!(logged.contains("a zbus error still shows"), "{logged}");
+ assert!(
+ logged.contains("other zbus warnings still show"),
+ "{logged}"
+ );
+ assert!(logged.contains("recording started"), "{logged}");
+ }
+
#[test]
fn healthy_log_destination_preserves_existing_records() {
let directory = LogDirectory::new();
diff --git a/apps/desktop-gpui/src/main_window.rs b/apps/desktop-gpui/src/main_window.rs
index e8552297e..82377d3bf 100644
--- a/apps/desktop-gpui/src/main_window.rs
+++ b/apps/desktop-gpui/src/main_window.rs
@@ -2001,6 +2001,9 @@ impl MainWindow {
self.set_microphone_selection(None, cx);
}
let required_window = auto_window_title();
+ // Pause and stop go to the session: on Linux the main window is not
+ // there while a window or Instant recording runs.
+ let session = self.session.clone();
cx.spawn_in(window, async move |this, cx| {
// Give enumeration and the first paint a moment; the recorder
@@ -2126,18 +2129,14 @@ impl MainWindow {
}
let third = std::time::Duration::from_secs(record_secs.div_ceil(3));
- let toggle = |this: &gpui::WeakEntity<Self>, cx: &mut gpui::AsyncWindowContext| {
- this.update_in(cx, |this, _, cx| {
- this.session
- .update(cx, |session, cx| session.toggle_pause(cx));
- })
- .ok();
+ let toggle = |cx: &gpui::AsyncApp| {
+ cx.update(|cx| session.update(cx, |session, cx| session.toggle_pause(cx)));
};
if pause_wiggle {
cx.background_executor().timer(third).await;
- toggle(&this, cx);
+ toggle(cx);
cx.background_executor().timer(third).await;
- toggle(&this, cx);
+ toggle(cx);
cx.background_executor().timer(third).await;
} else {
cx.background_executor()
@@ -2145,10 +2144,8 @@ impl MainWindow {
.await;
}
tracing::info!("auto-record stop requested");
- this.update_in(cx, |this, _, cx| {
- this.session.update(cx, |session, cx| session.stop(cx));
- })
- .ok();
+ let app: &gpui::AsyncApp = cx;
+ app.update(|cx| session.update(cx, |session, cx| session.stop(cx)));
})
.detach();
}
@@ -4786,7 +4783,7 @@ impl MainWindow {
} else if !studio && let Some(url) = &sharing {
cx.open_url(url);
} else if !studio {
- library::open_recording_folder(&path, mode);
+ library::open_recording_folder(&path, mode, cx);
}
}),
actions,
@@ -4863,7 +4860,7 @@ impl MainWindow {
row = row.child(self.library_action(
("lib-rec-folder", index),
"icons/folder.svg",
- move |_, _, _| library::open_recording_folder(&folder, mode),
+ move |_, _, cx| library::open_recording_folder(&folder, mode, cx),
));
row.child(self.library_action(
("lib-rec-delete", index),
@@ -4945,7 +4942,7 @@ impl MainWindow {
.child(
self.library_action(("lib-ss-open", index), "icons/folder.svg", {
let bundle = bundle.clone();
- move |_, _, _| library::reveal_in_folder(&bundle)
+ move |_, _, cx| library::reveal_path(&bundle, cx)
}),
)
.child(self.library_action(
diff --git a/apps/desktop-gpui/src/permission_guide.rs b/apps/desktop-gpui/src/permission_guide.rs
index f9b766a3b..5bd32bdcb 100644
--- a/apps/desktop-gpui/src/permission_guide.rs
+++ b/apps/desktop-gpui/src/permission_guide.rs
@@ -113,7 +113,7 @@ pub fn start(permission: OSPermission, cx: &mut App) {
let opened = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(Bounds {
origin: point(px(0.), px(0.)),
size: size(px(PANEL_W), px(PANEL_H)),
diff --git a/apps/desktop-gpui/src/platform.rs b/apps/desktop-gpui/src/platform.rs
index 6f4f9cfac..d5a7aaf88 100644
--- a/apps/desktop-gpui/src/platform.rs
+++ b/apps/desktop-gpui/src/platform.rs
@@ -1264,22 +1264,106 @@ mod mac {
/// `titlebar: None`). During the recording flow -- after the hidden main
/// window's `orderOut:` and the in-process ScreenCaptureKit start -- macOS
/// 26 adds `NSMiniaturizableWindowMask` to the hidden window on its own
- /// (no app or gpui code writes the mask; verified with a breakpoint on
- /// `setStyleMask:` and a mask poll), which materializes all three standard
- /// titlebar buttons on top of the hand-drawn lights. Setting the mask back
- /// tears the buttons down. Same borrow rule as [`show_native`]: call from
- /// a task, never inside a gpui update.
+ /// (no app or gpui code writes the mask), which materializes all three
+ /// standard titlebar buttons on top of the hand-drawn lights. Setting the
+ /// mask back tears the buttons down. [`lock_borderless_style`] refuses
+ /// the writes that go through `setStyleMask:`; this stays as the backstop
+ /// for any that don't. Same borrow rule as [`show_native`]: call from a
+ /// task, never inside a gpui update.
pub fn restore_borderless_style(native: &NativeWindow) {
use objc2::msg_send;
- const TITLED: usize = 1 << 0;
- const FULL_SIZE_CONTENT_VIEW: usize = 1 << 15;
- let want = TITLED | FULL_SIZE_CONTENT_VIEW;
unsafe {
let mask: usize = msg_send![&*native.0, styleMask];
- if mask != want {
+ if mask != BORDERLESS_STYLE_MASK {
tracing::info!(mask, "clearing foreign style-mask bits on the main window");
- let _: () = msg_send![&*native.0, setStyleMask: want];
+ let _: () = msg_send![&*native.0, setStyleMask: BORDERLESS_STYLE_MASK];
+ }
+ }
+ }
+
+ /// `NSTitledWindowMask | NSFullSizeContentViewWindowMask`: what gpui's
+ /// `titlebar: None` creates the main window with.
+ const BORDERLESS_STYLE_MASK: usize = (1 << 0) | (1 << 15);
+
+ static STYLE_MASK_LOCK_KEY: u8 = 0;
+ static SET_STYLE_MASK_SUPER: std::sync::OnceLock<(usize, usize)> = std::sync::OnceLock::new();
+
+ /// Pin the main window to its buttonless style mask by refusing every
+ /// later `setStyleMask:` that asks for something else.
+ ///
+ /// The thumbnail sweep's in-process ScreenCaptureKit capture at launch
+ /// makes ReplayKit run `-[RPScreenRecorder setWindowRotationLocked:]` on
+ /// the main queue, and that calls `setStyleMask:` on every app window to
+ /// add `NSResizableWindowMask` (`0x8001` -> `0x8009`, caught with a
+ /// `setStyleMask:` override logging `callStackSymbols`). AppKit then
+ /// materializes all three standard titlebar buttons next to the
+ /// hand-drawn close light until [`restore_borderless_style`] runs at the
+ /// end of the sweep. Rejecting the write here means the buttons are never
+ /// built.
+ ///
+ /// The override goes on the window's class, so it sees every
+ /// `GPUIPanel`; windows without the lock pass straight through to the
+ /// inherited implementation.
+ pub fn lock_borderless_style(native: &NativeWindow) {
+ use objc2::{ffi, msg_send, runtime::AnyClass};
+
+ unsafe extern "C" fn guarded_set_style_mask(this: *mut AnyObject, sel: Sel, mask: usize) {
+ let Some(&(_, imp)) = SET_STYLE_MASK_SUPER.get() else {
+ return;
+ };
+ let locked = unsafe {
+ ffi::objc_getAssociatedObject(this.cast(), (&raw const STYLE_MASK_LOCK_KEY).cast())
+ };
+ let mask = if locked.is_null() || mask == BORDERLESS_STYLE_MASK {
+ mask
+ } else {
+ tracing::info!(mask, "kept the main window's borderless style mask");
+ BORDERLESS_STYLE_MASK
+ };
+ let imp: unsafe extern "C" fn(*mut AnyObject, Sel, usize) =
+ unsafe { std::mem::transmute(imp) };
+ unsafe { imp(this, sel, mask) };
+ }
+
+ unsafe {
+ let class: *const AnyClass = msg_send![&*native.0, class];
+ let sel = objc2::sel!(setStyleMask:);
+ let &(installed_on, _) = SET_STYLE_MASK_SUPER.get_or_init(|| {
+ let superclass = ffi::class_getSuperclass(class.cast());
+ let Some(imp) = ffi::method_getImplementation(ffi::class_getInstanceMethod(
+ superclass,
+ sel.as_ptr(),
+ )) else {
+ tracing::warn!(
+ "no inherited setStyleMask: for the main window style-mask lock"
+ );
+ return (0, 0);
+ };
+ let added = ffi::class_addMethod(
+ class.cast_mut().cast(),
+ sel.as_ptr(),
+ Some(std::mem::transmute::<
+ unsafe extern "C" fn(*mut AnyObject, Sel, usize),
+ unsafe extern "C" fn(),
+ >(guarded_set_style_mask)),
+ c"v@:Q".as_ptr(),
+ );
+ if !objc2::runtime::Bool::from_raw(added).as_bool() {
+ tracing::warn!("could not install the main window style-mask lock");
+ return (0, 0);
+ }
+ (class as usize, imp as usize)
+ });
+ if installed_on != class as usize {
+ return;
}
+ let marker: *mut AnyObject = msg_send![objc2::class!(NSNull), null];
+ ffi::objc_setAssociatedObject(
+ (&*native.0 as *const NSWindow).cast_mut().cast(),
+ (&raw const STYLE_MASK_LOCK_KEY).cast(),
+ marker.cast(),
+ ffi::OBJC_ASSOCIATION_RETAIN_NONATOMIC,
+ );
}
}
@@ -3077,7 +3161,14 @@ mod stub {
.map_err(|error| format!("Could not copy the image to the clipboard: {error}"))
}
pub fn desktop_picture_path() -> Option<std::path::PathBuf> {
- None
+ #[cfg(target_os = "linux")]
+ {
+ crate::linux_wallpaper::current()
+ }
+ #[cfg(not(target_os = "linux"))]
+ {
+ None
+ }
}
pub fn set_activation_policy(_regular: bool) -> bool {
false
diff --git a/apps/desktop-gpui/src/recording.rs b/apps/desktop-gpui/src/recording.rs
index 1cf1b8a6e..c9658a5c0 100644
--- a/apps/desktop-gpui/src/recording.rs
+++ b/apps/desktop-gpui/src/recording.rs
@@ -118,6 +118,9 @@ pub(crate) struct InstantAttempt(Arc<InstantAttemptInner>);
#[cfg(target_os = "linux")]
struct InstantAttemptInner {
lifecycle: Mutex<Option<instant_recording::InstantLifecycle>>,
+ /// The server video and folder of a start still being built. A cancelled
+ /// start drops the build before it can clean up, so the attempt does it.
+ video: Mutex<Option<(String, PathBuf)>>,
cancelled: tokio::sync::watch::Sender<bool>,
startup: tokio::sync::watch::Sender<StartupState>,
}
@@ -136,6 +139,7 @@ impl InstantAttempt {
pub fn new() -> Self {
Self(Arc::new(InstantAttemptInner {
lifecycle: Mutex::new(None),
+ video: Mutex::new(None),
cancelled: tokio::sync::watch::channel(false).0,
startup: tokio::sync::watch::channel(StartupState::Unstarted).0,
}))
@@ -313,9 +317,14 @@ where
if result.is_err() {
attempt.cancel();
let lifecycle = attempt.0.lifecycle.lock().unwrap().clone();
- if let Some(lifecycle) = lifecycle {
- lifecycle.wait_for_quiescence().await;
- }
+ let joined = match lifecycle {
+ Some(lifecycle) => {
+ lifecycle.wait_for_quiescence().await
+ == instant_recording::InstantQuiescence::Joined
+ }
+ None => true,
+ };
+ discard_unstarted_video(&attempt, joined).await;
}
if let Err(Ok(active)) = sender.send(result) {
let _ = cleanup(active).await;
@@ -1123,21 +1132,32 @@ impl ActiveRecording {
/// delete and restart flows. Deleting a directory this app just created is
/// app behavior, same as the Tauri delete button.
pub async fn cancel_and_delete(self) -> anyhow::Result<()> {
- self.cancel_and_delete_with_query(None).await
+ self.cancel_and_delete_with_query(None, false).await
}
/// `cancel_and_delete` for a start that was superseded before it began,
- /// telling the server why its Instant video is deleted.
- #[cfg(not(target_os = "linux"))]
+ /// telling the server why its Instant video is deleted. On Linux a start
+ /// that wrote media by the time capture stopped keeps it, as after a crash.
pub(crate) async fn discard_cancelled_start(self) -> anyhow::Result<()> {
+ #[cfg(target_os = "linux")]
+ if !matches!(self.handle, Handle::Instant(_)) {
+ return self.cancel_preserving().await;
+ }
let query = crate::upload::instant_cleanup::delete_query(
crate::upload::instant_cleanup::CleanupReason::StartCancelled,
None,
);
- self.cancel_and_delete_with_query(query).await
+ self.cancel_and_delete_with_query(query, cfg!(target_os = "linux"))
+ .await
}
- async fn cancel_and_delete_with_query(self, query: Option<String>) -> anyhow::Result<()> {
+ /// `keep_media`: once capture has stopped, a folder holding media is kept
+ /// as a stopped recording with its server video instead of being deleted.
+ async fn cancel_and_delete_with_query(
+ self,
+ query: Option<String>,
+ keep_media: bool,
+ ) -> anyhow::Result<()> {
if let Some(gate) = &self.start_gate {
gate.arm();
}
@@ -1167,9 +1187,15 @@ impl ActiveRecording {
Handle::Instant(handle) => {
#[cfg(target_os = "linux")]
{
- let (_, result) =
+ let (joined, result) =
joined_instant_result(handle.lifecycle(), handle.cancel()).await;
- result
+ if joined {
+ result
+ } else {
+ Err(anyhow!(
+ "Instant capture cleanup is unconfirmed; local files preserved"
+ ))
+ }
}
#[cfg(not(target_os = "linux"))]
{
@@ -1185,6 +1211,26 @@ impl ActiveRecording {
}
return Err(error);
}
+ #[cfg(target_os = "linux")]
+ if keep_media && crate::upload::instant_cleanup::project_holds_media(&self.project_dir) {
+ let mut upload = match self.instant_upload.as_ref() {
+ Some(upload) => Some(upload.lock().await),
+ None => None,
+ };
+ let upload = upload.as_deref_mut().and_then(Option::as_mut);
+ if let Some(upload) = upload {
+ upload.abort_segments().await;
+ return persist_preserved_instant_stop(
+ &self.project_dir,
+ Some(upload.metadata_lock()),
+ true,
+ Ok(()),
+ );
+ }
+ return persist_preserved_instant_stop(&self.project_dir, None, true, Ok(()));
+ }
+ #[cfg(not(target_os = "linux"))]
+ let _ = keep_media;
let mut upload = match self.instant_upload.as_ref() {
Some(upload) => upload.lock().await.take(),
None => None,
@@ -2137,6 +2183,35 @@ pub async fn start(config: StartConfig) -> anyhow::Result<ActiveRecording> {
.await
}
+/// The server video of a start whose build was dropped: deleted with its
+/// folder when capture stopped without writing media, as a cancelled start is
+/// on macOS and Windows. Otherwise the cleanup journal keeps it for the sweep,
+/// which never deletes a recording that holds media.
+#[cfg(target_os = "linux")]
+async fn discard_unstarted_video(attempt: &InstantAttempt, joined: bool) {
+ use crate::upload::instant_cleanup::{self, CleanupReason};
+ let Some((video_id, project_dir)) = attempt.0.video.lock().unwrap().take() else {
+ return;
+ };
+ if !joined || instant_cleanup::project_holds_media(&project_dir) {
+ instant_cleanup::abandon(&video_id, CleanupReason::StartCancelled, None);
+ return;
+ }
+ let query = instant_cleanup::delete_query(CleanupReason::StartCancelled, None);
+ match crate::upload::delete_instant_video_with_query(&video_id, query.as_deref()).await {
+ Ok(()) => {
+ instant_cleanup::resolved(&video_id);
+ if let Err(error) = tokio::fs::remove_dir_all(&project_dir).await {
+ tracing::warn!(%error, path = %project_dir.display(), "Could not remove a cancelled start's folder");
+ }
+ }
+ Err(error) => {
+ tracing::warn!(%error, video_id, "Could not delete a cancelled start's video");
+ instant_cleanup::abandon(&video_id, CleanupReason::StartCancelled, None);
+ }
+ }
+}
+
#[cfg(target_os = "linux")]
pub(crate) fn start_tracked(
config: StartConfig,
@@ -2145,7 +2220,7 @@ pub(crate) fn start_tracked(
owned_instant_start(
attempt.clone(),
start_internal(config, Some(attempt)),
- ActiveRecording::cancel_preserving,
+ ActiveRecording::discard_cancelled_start,
)
}
@@ -2188,6 +2263,10 @@ async fn start_internal(
.await
.map_err(anyhow::Error::msg)?;
crate::upload::instant_cleanup::begin(&video.id, &project_dir);
+ #[cfg(target_os = "linux")]
+ if let Some(attempt) = &attempt {
+ *attempt.0.video.lock().unwrap() = Some((video.id.clone(), project_dir.clone()));
+ }
Some(video)
} else {
None
@@ -2226,10 +2305,18 @@ async fn start_internal(
Err(error) => {
instant_cleanup::abandon(&video.id, CleanupReason::StartFailed, Some(cause));
tracing::error!(video_id = %video.id, "Failed to clean up instant recording: {error}");
+ #[cfg(target_os = "linux")]
+ if let Some(attempt) = &attempt {
+ attempt.0.video.lock().unwrap().take();
+ }
return Err(anyhow!("Failed to clean up instant recording: {error}"));
}
}
}
+ #[cfg(target_os = "linux")]
+ if let Some(attempt) = &attempt {
+ attempt.0.video.lock().unwrap().take();
+ }
result
}
diff --git a/apps/desktop-gpui/src/screenshot.rs b/apps/desktop-gpui/src/screenshot.rs
index b01af4894..af88148f5 100644
--- a/apps/desktop-gpui/src/screenshot.rs
+++ b/apps/desktop-gpui/src/screenshot.rs
@@ -29,6 +29,43 @@ pub fn take_screenshot(target: ScreenCaptureTarget, cx: &mut App) {
}
tracing::info!(kind = target.kind_str(), "taking a screenshot");
+ #[cfg(target_os = "linux")]
+ if cap_recording::screenshot::uses_wayland_portal()
+ && matches!(
+ target,
+ ScreenCaptureTarget::Display { .. } | ScreenCaptureTarget::Area { .. }
+ )
+ && !WAYLAND_SCREENSHOTS_ALLOWED.load(std::sync::atomic::Ordering::Acquire)
+ {
+ // Asked while the picker still has focus: GNOME Shell shows the
+ // permission question only for the focused app, and the capture below
+ // hides the picker first.
+ let request = gpui_tokio::Tokio::spawn_result(
+ cx,
+ cap_recording::screenshot::request_wayland_screenshot_permission(),
+ );
+ cx.spawn(async move |cx| match request.await {
+ Ok(()) => {
+ WAYLAND_SCREENSHOTS_ALLOWED.store(true, std::sync::atomic::Ordering::Release);
+ tracing::info!("the desktop allows Cap's screenshots");
+ cx.update(|cx| capture(target, cx));
+ }
+ // The picker is still up, so the user can try again.
+ Err(error) => tracing::error!("screenshot capture failed: {error:#}"),
+ })
+ .detach();
+ return;
+ }
+ capture(target, cx);
+}
+
+/// Set once the screenshot portal has answered a request: the permission it
+/// may have asked for is granted for the rest of the session.
+#[cfg(target_os = "linux")]
+static WAYLAND_SCREENSHOTS_ALLOWED: std::sync::atomic::AtomicBool =
+ std::sync::atomic::AtomicBool::new(false);
+
+fn capture(target: ScreenCaptureTarget, cx: &mut App) {
let settle = app_windows::prepare_for_screenshot_capture(cx);
let task = gpui_tokio::Tokio::spawn_result(cx, async move {
diff --git a/apps/desktop-gpui/src/screenshot_editor.rs b/apps/desktop-gpui/src/screenshot_editor.rs
index 0eddf7d36..af9409705 100644
--- a/apps/desktop-gpui/src/screenshot_editor.rs
+++ b/apps/desktop-gpui/src/screenshot_editor.rs
@@ -1537,7 +1537,16 @@ impl ScreenshotEditorWindow {
fn pick_background_image(&mut self, window: &mut Window, cx: &mut Context<Self>) {
cx.spawn_in(window, async move |this, cx| {
- let Some(path) = crate::platform::open_image_panel(&BACKGROUND_IMAGE_EXTENSIONS) else {
+ #[cfg(target_os = "linux")]
+ let picked = crate::platform::open_file_panel_async(
+ &[("Image", &BACKGROUND_IMAGE_EXTENSIONS[..])],
+ None,
+ cx,
+ )
+ .await;
+ #[cfg(not(target_os = "linux"))]
+ let picked = crate::platform::open_image_panel(&BACKGROUND_IMAGE_EXTENSIONS);
+ let Some(path) = picked else {
return;
};
this.update_in(cx, |this, window, cx| {
@@ -2466,7 +2475,7 @@ impl ScreenshotEditorWindow {
MenuKind::More => {
self.menu = None;
match index {
- 0 => crate::library::reveal_in_folder(&self.bundle.join("original.png")),
+ 0 => crate::library::reveal_path(&self.bundle.join("original.png"), cx),
1 => self.delete_screenshot(window, cx),
_ => {}
}
@@ -4684,20 +4693,52 @@ fn slider_id(slider: StyleSlider) -> &'static str {
}
}
+fn decode_large_preview_jpeg(bytes: &[u8]) -> Option<(image::DynamicImage, u32, u32)> {
+ let mut decoder = jpeg_decoder::Decoder::new(bytes);
+ decoder.read_info().ok()?;
+ let info = decoder.info()?;
+ if info.width.max(info.height) <= 1536 {
+ return None;
+ }
+ crate::jpeg_thumbnail::decode_jpeg_thumbnail(bytes, |width, height| {
+ let scale = (768. / width.max(height) as f32).min(1.);
+ (
+ ((width as f32 * scale) as u32).max(1),
+ ((height as f32 * scale) as u32).max(1),
+ )
+ })
+}
+
/// The Image tab's `h-48` preview, at 2x.
fn decode_preview_image(path: &Path) -> Option<Arc<RenderImage>> {
let bytes = std::fs::read(path).ok()?;
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 scaled_jpeg = if format == image::ImageFormat::Jpeg {
+ decode_large_preview_jpeg(&bytes)
+ } else {
+ None
+ };
+ let has_scaled_jpeg = scaled_jpeg.is_some();
+ let (decoded, width, height) = scaled_jpeg.or_else(|| {
+ let decoded = image::load_from_memory_with_format(&bytes, format).ok()?;
+ let dimensions = (decoded.width().max(1), decoded.height().max(1));
+ Some((decoded, dimensions.0, dimensions.1))
+ })?;
let scale = (768. / width.max(height) as f32).min(1.);
let scaled = if scale < 1. {
- decoded
- .thumbnail(
- ((width as f32 * scale) as u32).max(1),
- ((height as f32 * scale) as u32).max(1),
- )
- .into_rgba8()
+ let target_width = ((width as f32 * scale) as u32).max(1);
+ let target_height = ((height as f32 * scale) as u32).max(1);
+ if has_scaled_jpeg {
+ decoded
+ .resize(
+ target_width,
+ target_height,
+ image::imageops::FilterType::Triangle,
+ )
+ .into_rgba8()
+ } else {
+ decoded.thumbnail(target_width, target_height).into_rgba8()
+ }
} else {
decoded.into_rgba8()
};
@@ -4868,6 +4909,111 @@ fn aspect_eq(a: &Option<cap_project::AspectRatio>, b: &Option<cap_project::Aspec
mod tests {
use super::*;
+ #[test]
+ fn background_preview_jpeg_scaling_starts_above_the_size_boundary() {
+ let root = tempfile::tempdir().unwrap();
+ for (width, height, expected) in [
+ (768, 384, (768, 384)),
+ (1536, 768, (768, 384)),
+ (1537, 769, (767, 384)),
+ (769, 1537, (384, 767)),
+ ] {
+ let path = root.path().join("preview.bin");
+ image::RgbImage::from_pixel(width, height, image::Rgb([40, 120, 200]))
+ .save_with_format(&path, image::ImageFormat::Jpeg)
+ .unwrap();
+ let bytes = std::fs::read(&path).unwrap();
+ let scaled = decode_large_preview_jpeg(&bytes);
+ if width.max(height) <= 1536 {
+ assert!(scaled.is_none());
+ } else {
+ let (decoded, source_width, source_height) = scaled.unwrap();
+ assert_eq!((source_width, source_height), (width, height));
+ assert!(decoded.width() < width && decoded.height() < height);
+ }
+ let preview = decode_preview_image(&path).unwrap();
+ let size = preview.size(0);
+ assert_eq!(
+ size,
+ gpui::size(
+ gpui::DevicePixels::from(expected.0),
+ gpui::DevicePixels::from(expected.1)
+ )
+ );
+
+ for pixel in preview.as_bytes(0).unwrap().chunks_exact(4) {
+ assert!(pixel[0].abs_diff(200) <= 4);
+ assert!(pixel[1].abs_diff(120) <= 4);
+ assert!(pixel[2].abs_diff(40) <= 4);
+ assert_eq!(pixel[3], 255);
+ }
+ }
+ }
+
+ #[test]
+ fn background_preview_large_grayscale_jpeg_remains_opaque() {
+ let root = tempfile::tempdir().unwrap();
+ let path = root.path().join("preview.jpg");
+ image::GrayImage::from_pixel(2048, 1024, image::Luma([90]))
+ .save(&path)
+ .unwrap();
+ let preview = decode_preview_image(&path).unwrap();
+ for pixel in preview.as_bytes(0).unwrap().chunks_exact(4) {
+ assert!(pixel[..3].iter().all(|channel| channel.abs_diff(90) <= 1));
+ assert_eq!(pixel[3], 255);
+ }
+ }
+
+ #[test]
+ fn background_preview_fallback_matches_the_existing_decode_and_resize() {
+ let root = tempfile::tempdir().unwrap();
+ for (format, width) in [
+ (image::ImageFormat::Png, 1601),
+ (image::ImageFormat::Jpeg, 767),
+ (image::ImageFormat::Jpeg, 1536),
+ ] {
+ let path = root.path().join("preview.bin");
+ let original =
+ image::DynamicImage::ImageRgba8(image::RgbaImage::from_fn(width, 7, |x, y| {
+ image::Rgba([(x % 256) as u8, (y * 20) as u8, 90, (x % 255) as u8])
+ }));
+ if format == image::ImageFormat::Jpeg {
+ original.to_rgb8().save_with_format(&path, format).unwrap();
+ } else {
+ original.save_with_format(&path, format).unwrap();
+ }
+ let decoded =
+ image::load_from_memory_with_format(&std::fs::read(&path).unwrap(), format)
+ .unwrap();
+ let scale = (768. / width as f32).min(1.);
+ let mut expected = decoded
+ .thumbnail((width as f32 * scale) as u32, (7. * scale) as u32)
+ .into_rgba8();
+ let size = expected.dimensions();
+ cap_gpui_kernels::frame::swap_red_blue(&mut expected);
+ let preview = decode_preview_image(&path).unwrap();
+ assert_eq!(preview.as_bytes(0).unwrap(), expected.as_raw());
+ assert_eq!(
+ preview.size(0),
+ gpui::size(
+ gpui::DevicePixels::from(size.0),
+ gpui::DevicePixels::from(size.1)
+ )
+ );
+ }
+ }
+
+ #[test]
+ fn background_preview_rejects_missing_and_malformed_images() {
+ let root = tempfile::tempdir().unwrap();
+ let path = root.path().join("preview.jpg");
+ assert!(decode_preview_image(&path).is_none());
+ for bytes in [b"not an image".as_slice(), &[0xff, 0xd8, 0xff, 0xd9]] {
+ std::fs::write(&path, bytes).unwrap();
+ assert!(decode_preview_image(&path).is_none());
+ }
+ }
+
/// The checkerboard is one 240x240 tile of 10px cells -- an even number of
/// cells per axis, so tiling it leaves no seam.
#[test]
diff --git a/apps/desktop-gpui/src/screenshot_export.rs b/apps/desktop-gpui/src/screenshot_export.rs
index 5533f1884..525858d4f 100644
--- a/apps/desktop-gpui/src/screenshot_export.rs
+++ b/apps/desktop-gpui/src/screenshot_export.rs
@@ -385,6 +385,12 @@ fn skia_stroke(width: f64, round: bool) -> tiny_skia::Stroke {
/// order -- rect, ellipse, arrow, freehand with tiny-skia, text with
/// cosmic-text, each under its own `globalAlpha`.
fn draw_annotations_onto(canvas: &mut [u8], width: u32, height: u32, scaled: &[Annotation]) {
+ if scaled
+ .iter()
+ .all(|annotation| annotation.annotation_type == AnnotationType::Mask)
+ {
+ return;
+ }
pixels::premultiply(canvas);
if let Some(mut pixmap) = tiny_skia::PixmapMut::from_bytes(canvas, width, height) {
for annotation in scaled {
@@ -659,6 +665,79 @@ mod tests {
};
}
+ #[test]
+ fn mask_only_draw_preserves_straight_alpha_pixels() {
+ let original: Vec<u8> = (0..=255u8).flat_map(|alpha| [17, 30, 77, alpha]).collect();
+ for scaled in [
+ Vec::new(),
+ vec![annotation(AnnotationType::Mask, 0., 0., 16., 16.)],
+ ] {
+ let mut canvas = original.clone();
+ draw_annotations_onto(&mut canvas, 16, 16, &scaled);
+ assert_eq!(canvas, original);
+ }
+ }
+
+ #[test]
+ fn off_canvas_mask_does_not_change_translucent_export_pixels() {
+ let raw = RawFrame {
+ rgba: vec![17, 30, 77, 1, 123, 45, 67, 127],
+ width: 2,
+ height: 1,
+ base_width: 2,
+ base_height: 1,
+ };
+ let mut config = ProjectConfiguration::default();
+ invisible_background(&mut config);
+ config
+ .annotations
+ .push(annotation(AnnotationType::Mask, 3., 0., 2., 1.));
+
+ let out = composite(&raw, &config);
+
+ assert_eq!((out.width, out.height), (2, 1));
+ assert_eq!(out.rgba, raw.rgba);
+ }
+
+ #[test]
+ fn pixelated_export_preserves_pixels_outside_the_mask_and_draws_shapes() {
+ let raw = RawFrame {
+ rgba: vec![
+ 255, 0, 0, 255, 0, 255, 0, 255, 17, 30, 77, 1, 123, 45, 67, 127, 255, 255, 0, 255,
+ 0, 0, 255, 255, 23, 41, 83, 3, 111, 55, 99, 129,
+ ],
+ width: 4,
+ height: 2,
+ base_width: 4,
+ base_height: 2,
+ };
+ let mut config = ProjectConfiguration::default();
+ invisible_background(&mut config);
+ let mut mask = annotation(AnnotationType::Mask, 0., 0., 2., 2.);
+ mask.mask_type = Some(cap_project::MaskType::Pixelate);
+ mask.mask_level = Some(2.);
+ config.annotations.push(mask);
+ let mut expected = raw.rgba.clone();
+ for index in [0, 1, 4, 5] {
+ expected[index * 4..index * 4 + 4].copy_from_slice(&[0, 0, 255, 255]);
+ }
+
+ let out = composite(&raw, &config);
+
+ assert_eq!((out.width, out.height), (4, 2));
+ assert_eq!(out.rgba, expected);
+
+ let mut rectangle = annotation(AnnotationType::Rectangle, 2., 0., 2., 2.);
+ rectangle.fill_color = "#ff0000".into();
+ rectangle.stroke_width = 0.;
+ config.annotations.push(rectangle);
+ for index in [2, 3, 6, 7] {
+ expected[index * 4..index * 4 + 4].copy_from_slice(&[255, 0, 0, 255]);
+ }
+
+ assert_eq!(composite(&raw, &config).rgba, expected);
+ }
+
/// `stableValue`: keys sorted at every depth, arrays kept in order.
#[test]
fn stable_stringify_sorts_keys_recursively() {
@@ -904,6 +983,66 @@ mod tests {
);
}
+ #[test]
+ #[ignore]
+ fn benchmark_mask_only_screenshot_composite() {
+ use std::{hint::black_box, time::Instant};
+ for (width, height) in [(1920, 1080), (3840, 2160)] {
+ for translucent_border in [false, true] {
+ let raw = RawFrame {
+ rgba: (0..width * height)
+ .flat_map(|i: u32| {
+ let x = i % width;
+ let y = i / width;
+ let border = x < 32 || y < 32 || x >= width - 32 || y >= height - 32;
+ let alpha = if translucent_border && border {
+ (i % 254 + 1) as u8
+ } else {
+ 255
+ };
+ [
+ i as u8,
+ i.wrapping_mul(17) as u8,
+ i.wrapping_mul(31) as u8,
+ alpha,
+ ]
+ })
+ .collect(),
+ width,
+ height,
+ base_width: width,
+ base_height: height,
+ };
+ let mut config = ProjectConfiguration::default();
+ invisible_background(&mut config);
+ let mut mask = annotation(AnnotationType::Mask, 100., 100., 256., 256.);
+ mask.mask_type = Some(cap_project::MaskType::Pixelate);
+ mask.mask_level = Some(16.);
+ config.annotations.push(mask);
+ let mut samples_ns = Vec::new();
+ for iteration in 0..35 {
+ let start = Instant::now();
+ black_box(composite(black_box(&raw), black_box(&config)));
+ let elapsed = start.elapsed().as_nanos();
+ if iteration >= 5 {
+ samples_ns.push(elapsed);
+ }
+ }
+ samples_ns.sort_unstable();
+ let median_ms = (samples_ns[14] + samples_ns[15]) as f64 / 2_000_000.;
+ println!(
+ "{}",
+ serde_json::json!({
+ "width": width, "height": height,
+ "translucent_border": translucent_border,
+ "median_ms": median_ms, "samples_ns": samples_ns,
+ "scope": "CPU mask-only composite; excludes GPU, PNG, disk and native UI"
+ })
+ );
+ }
+ }
+ }
+
#[test]
#[ignore]
fn benchmark_screenshot_copy_preparation() {
diff --git a/apps/desktop-gpui/src/session.rs b/apps/desktop-gpui/src/session.rs
index b964bbead..20ef9f7ec 100644
--- a/apps/desktop-gpui/src/session.rs
+++ b/apps/desktop-gpui/src/session.rs
@@ -78,6 +78,24 @@ enum CountdownAdvance {
Start,
}
+/// Waits for the pipeline to be primed, then plays the cue. A start that is
+/// dropped before it primes plays nothing.
+async fn play_cue_when_primed<F, Fut>(
+ primed: Option<tokio::sync::watch::Receiver<bool>>,
+ gate: cap_recording::RecordingStartGate,
+ play: F,
+) where
+ F: FnOnce(cap_recording::RecordingStartGate) -> Fut,
+ Fut: Future<Output = ()>,
+{
+ if let Some(mut primed) = primed
+ && primed.wait_for(|primed| *primed).await.is_err()
+ {
+ return;
+ }
+ play(gate).await;
+}
+
fn countdown_advance(
phase: Phase,
current_generation: u64,
@@ -440,6 +458,10 @@ pub struct RecordingSession {
/// A capture pipeline that finished warming up while the countdown or the
/// start cue was still running; activated the moment the cue ends.
primed: Option<ActiveRecording>,
+ /// Set once the pipeline is primed. The start cue waits for it, so the cue
+ /// never promises a recording that has not begun capturing (an Instant
+ /// start waits on the server's video before it builds the pipeline).
+ primed_ready: Option<tokio::sync::watch::Sender<bool>>,
/// Teardown of a cancelled primed start still releasing its devices; the
/// next start waits for it before locking the mic and camera again.
discarding: Option<Task<Result<(), tokio::task::JoinError>>>,
@@ -488,6 +510,7 @@ impl RecordingSession {
start_gate: None,
start_cue: None,
primed: None,
+ primed_ready: None,
discarding: None,
pause_control: PauseControlState::default(),
pause_control_error: None,
@@ -665,6 +688,7 @@ impl RecordingSession {
self.start_gate = Some(start_gate);
self.start_cue = Some(crate::app_sounds::prime_recording_start_sound());
self.discard_primed(cx);
+ self.primed_ready = Some(tokio::sync::watch::channel(false).0);
self.pause_control.invalidate();
self.pause_control.uncertain = false;
self.pause_control_error = None;
@@ -687,7 +711,8 @@ impl RecordingSession {
cx.notify();
// The pipeline warms up during the final countdown second so capture is
- // live when the cue ends; without a countdown it warms up under the cue.
+ // live when the cue ends; without a countdown it warms up before the cue.
+ // Either way the cue waits until the pipeline is primed.
if countdown <= 1 {
self.prime_engine(config.clone(), generation, cx);
}
@@ -742,27 +767,29 @@ impl RecordingSession {
.detach();
}
- /// Plays the start cue; the returned future completes once the recording's
- /// start gate is armed, whether the cue played, failed or was aborted.
+ /// Plays the start cue once the pipeline is primed; the returned future
+ /// completes once the recording's start gate is armed, whether the cue
+ /// played, failed or was aborted.
fn play_start_cue(
&mut self,
cx: &mut Context<Self>,
) -> std::pin::Pin<Box<dyn Future<Output = ()>>> {
let gate = self.start_gate.clone().unwrap_or_default();
let cue = self.start_cue.take();
+ let primed = self.primed_ready.as_ref().map(|ready| ready.subscribe());
let (abort, registration) = futures_util::future::AbortHandle::new_pair();
self.countdown_cue_abort = Some(abort);
let task = gpui_tokio::Tokio::spawn(
cx,
futures_util::future::Abortable::new(
- async move {
+ play_cue_when_primed(primed, gate, move |gate| async move {
match cue {
Some(cue) => crate::app_sounds::play_recording_start_sound(cue, gate).await,
None => {
gate.arm();
}
}
- },
+ }),
registration,
),
);
@@ -842,6 +869,9 @@ impl RecordingSession {
Ok(Ok(active)) => {
if this.countdown_remaining.is_some() {
this.primed = Some(active);
+ if let Some(ready) = &this.primed_ready {
+ ready.send_replace(true);
+ }
} else {
this.activate(active, cx);
}
@@ -960,9 +990,6 @@ impl RecordingSession {
if let Some(previous) = previous {
let _ = previous.await;
}
- #[cfg(target_os = "linux")]
- let discarded = active.cancel_preserving().await;
- #[cfg(not(target_os = "linux"))]
let discarded = active.discard_cancelled_start().await;
match discarded {
Ok(()) => {
@@ -1945,6 +1972,7 @@ impl RecordingSession {
fn finish(&mut self, cx: &mut Context<Self>) {
self.abort_start_cue();
self.discard_primed(cx);
+ self.primed_ready = None;
self.start_gate = None;
#[cfg(target_os = "linux")]
if !self.instant_cleanup_safe() {
@@ -1991,6 +2019,47 @@ fn recording_failure_is_current(phase: Phase, current: u64, completed: u64) -> b
mod countdown_tests {
use super::*;
+ /// The start cue promises that recording has begun, so it must not play
+ /// before the pipeline is built (an Instant start first waits for the
+ /// server to create the video).
+ #[tokio::test]
+ async fn start_cue_waits_for_the_primed_pipeline() {
+ let (ready, primed) = tokio::sync::watch::channel(false);
+ let played = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
+ let gate = cap_recording::RecordingStartGate::new();
+ let cue = tokio::spawn({
+ let played = played.clone();
+ let gate = gate.clone();
+ play_cue_when_primed(Some(primed), gate, move |gate| async move {
+ played.store(true, std::sync::atomic::Ordering::SeqCst);
+ gate.arm();
+ })
+ });
+ tokio::time::sleep(Duration::from_millis(200)).await;
+ assert!(!played.load(std::sync::atomic::Ordering::SeqCst));
+ assert!(!gate.is_armed());
+ ready.send_replace(true);
+ cue.await.unwrap();
+ assert!(played.load(std::sync::atomic::Ordering::SeqCst));
+ assert!(gate.is_armed());
+ }
+
+ #[tokio::test]
+ async fn start_cue_stays_silent_when_the_start_is_dropped_before_it_primes() {
+ let (ready, primed) = tokio::sync::watch::channel(false);
+ let gate = cap_recording::RecordingStartGate::new();
+ let cue = tokio::spawn(play_cue_when_primed(
+ Some(primed),
+ gate.clone(),
+ |_| async {
+ panic!("the cue played for a start that never primed");
+ },
+ ));
+ drop(ready);
+ cue.await.unwrap();
+ assert!(!gate.is_armed());
+ }
+
#[cfg(target_os = "linux")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn countdown_stop_cancels_an_unpolled_instant_start() {
diff --git a/apps/desktop-gpui/src/settings_pages.rs b/apps/desktop-gpui/src/settings_pages.rs
index 530a8df63..536f5a2c8 100644
--- a/apps/desktop-gpui/src/settings_pages.rs
+++ b/apps/desktop-gpui/src/settings_pages.rs
@@ -3532,7 +3532,7 @@ impl SettingsWindow {
ui::ButtonSize::Md,
)
.label("Show File")
- .on_click(move |_, _, _| crate::library::reveal_in_folder(&path)),
+ .on_click(move |_, _, cx| crate::library::reveal_path(&path, cx)),
);
}
buttons = buttons.child(
diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs
index 1e2d7fe97..2c65832aa 100644
--- a/apps/desktop-gpui/src/settings_window.rs
+++ b/apps/desktop-gpui/src/settings_window.rs
@@ -2653,7 +2653,7 @@ impl SettingsWindow {
"Open folder",
false,
cx,
- move |_this, _window, _cx| library::reveal_in_folder(&bundle),
+ move |_this, _window, cx| library::reveal_path(&bundle, cx),
))
.child(self.row_button(
("screenshot-delete", index),
@@ -3142,7 +3142,7 @@ impl SettingsWindow {
"Open recording bundle",
false,
cx,
- move |_this, _window, _cx| library::open_recording_folder(&folder_path, mode),
+ move |_this, _window, cx| library::open_recording_folder(&folder_path, mode, cx),
));
actions.child(self.row_button(
("recording-delete", index),
diff --git a/apps/desktop-gpui/src/single_instance.rs b/apps/desktop-gpui/src/single_instance.rs
index 3ecf64b33..f08ded5b6 100644
--- a/apps/desktop-gpui/src/single_instance.rs
+++ b/apps/desktop-gpui/src/single_instance.rs
@@ -635,6 +635,38 @@ fn read_linux_reopen_actions(
reader: &mut impl std::io::Read,
owner: LinuxInstanceIdentity,
) -> std::io::Result<LinuxReopenActions> {
+ read_linux_reopen_request(reader, owner).map(|(actions, _)| actions)
+}
+
+/// The longest activation token a relaunch forwards. Compositors issue short
+/// opaque strings; anything this long is not one.
+#[cfg(target_os = "linux")]
+const MAX_ACTIVATION_TOKEN_BYTES: usize = 4096;
+
+#[cfg(target_os = "linux")]
+fn valid_activation_token(token: &str) -> bool {
+ !token.is_empty()
+ && token.len() <= MAX_ACTIVATION_TOKEN_BYTES
+ && token.chars().all(|c| c.is_ascii_graphic())
+}
+
+/// The relaunch's activation token, from the launcher that started it
+/// (`XDG_ACTIVATION_TOKEN` on Wayland). Focus stealing prevention lets the
+/// running instance raise its window with it.
+#[cfg(target_os = "linux")]
+fn launch_activation_token() -> Option<String> {
+ std::env::var("XDG_ACTIVATION_TOKEN")
+ .ok()
+ .filter(|token| valid_activation_token(token))
+}
+
+/// A forwarded relaunch: its actions, and the activation token it came with
+/// (`CAPACTV1` frames, which carry the token ahead of the actions).
+#[cfg(target_os = "linux")]
+fn read_linux_reopen_request(
+ reader: &mut impl std::io::Read,
+ owner: LinuxInstanceIdentity,
+) -> std::io::Result<(LinuxReopenActions, Option<String>)> {
let invalid = || {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
@@ -645,7 +677,27 @@ fn read_linux_reopen_actions(
reader.read_exact(&mut request)?;
let mut header = linux_reopen_payload(owner);
if request == header {
- return Ok(Vec::new());
+ return Ok((Vec::new(), None));
+ }
+ let mut activation = header;
+ activation[..8].copy_from_slice(b"CAPACTV1");
+ let mut token = None;
+ if request == activation {
+ let mut size = [0; 4];
+ reader.read_exact(&mut size)?;
+ let size = u32::from_be_bytes(size) as usize;
+ if size > MAX_ACTIVATION_TOKEN_BYTES {
+ return Err(invalid());
+ }
+ let mut raw = vec![0; size];
+ reader.read_exact(&mut raw)?;
+ let raw = String::from_utf8(raw).map_err(|_| invalid())?;
+ if !valid_activation_token(&raw) {
+ return Err(invalid());
+ }
+ token = Some(raw);
+ request = header;
+ request[..8].copy_from_slice(b"CAPREOP2");
}
header[..8].copy_from_slice(b"CAPREOP2");
if request != header {
@@ -676,7 +728,42 @@ fn read_linux_reopen_actions(
actions.push(parse_linux_reopen_action(raw)?);
remaining = rest;
}
- Ok(actions)
+ Ok((actions, token))
+}
+
+/// Writes a relaunch for the running instance, with the activation token
+/// when there is one.
+#[cfg(target_os = "linux")]
+fn write_linux_reopen_request(
+ writer: &mut impl std::io::Write,
+ owner: LinuxInstanceIdentity,
+ actions: &[u8],
+ token: Option<&str>,
+) -> std::io::Result<()> {
+ let Some(token) = token.filter(|token| valid_activation_token(token)) else {
+ return write_linux_reopen_actions(writer, owner, actions);
+ };
+ if actions.len() > MAX_FORWARDED_DEEP_LINK_BYTES {
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ "Too many forwarded Cap action bytes",
+ ));
+ }
+ let mut header = linux_reopen_payload(owner);
+ header[..8].copy_from_slice(b"CAPACTV1");
+ writer.write_all(&header)?;
+ writer.write_all(&(token.len() as u32).to_be_bytes())?;
+ writer.write_all(token.as_bytes())?;
+ writer.write_all(&(actions.len() as u32).to_be_bytes())?;
+ writer.write_all(actions)
+}
+
+/// The newest activation token a relaunch brought, until the window it
+/// raises is shown.
+#[cfg(target_os = "linux")]
+fn pending_activation_token() -> &'static std::sync::Mutex<Option<String>> {
+ static TOKEN: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
+ &TOKEN
}
#[cfg(target_os = "linux")]
@@ -730,7 +817,12 @@ fn receive_linux_reopen(
stream.set_read_timeout(Some(std::time::Duration::from_millis(250)))?;
stream.set_write_timeout(Some(std::time::Duration::from_millis(250)))?;
linux_peer(stream)?;
- let actions = read_linux_reopen_actions(stream, owner)?;
+ let (actions, token) = read_linux_reopen_request(stream, owner)?;
+ if let Some(token) = token {
+ *pending_activation_token()
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(token);
+ }
match requests.try_send(actions) {
Ok(()) => {}
Err(flume::TrySendError::Full(actions)) if actions.is_empty() => {}
@@ -771,6 +863,16 @@ fn send_linux_reopen(
address: std::os::unix::net::SocketAddr,
owner: LinuxInstanceIdentity,
actions: &[u8],
+) -> std::io::Result<()> {
+ send_linux_reopen_with_token(address, owner, actions, None)
+}
+
+#[cfg(target_os = "linux")]
+fn send_linux_reopen_with_token(
+ address: std::os::unix::net::SocketAddr,
+ owner: LinuxInstanceIdentity,
+ actions: &[u8],
+ token: Option<&str>,
) -> std::io::Result<()> {
use std::io::Read;
let started = std::time::Instant::now();
@@ -799,7 +901,7 @@ fn send_linux_reopen(
"The activation socket belongs to another process",
));
}
- write_linux_reopen_actions(&mut stream, owner, actions)?;
+ write_linux_reopen_request(&mut stream, owner, actions, token)?;
let mut reply = [0];
stream.read_exact(&mut reply)?;
if reply != [1] || linux_instance_identity(owner.pid)? != owner {
@@ -810,6 +912,20 @@ fn send_linux_reopen(
Ok(())
}
+/// Whether the running Cap hung up without acknowledging: how an instance from
+/// before activation tokens rejects their frame, and how a current one fails
+/// before it queues anything. A timeout may come after the actions were
+/// queued, so resending then could run them twice.
+#[cfg(target_os = "linux")]
+fn closed_without_queueing(error: &std::io::Error) -> bool {
+ matches!(
+ error.kind(),
+ std::io::ErrorKind::UnexpectedEof
+ | std::io::ErrorKind::ConnectionReset
+ | std::io::ErrorKind::BrokenPipe
+ )
+}
+
#[cfg(target_os = "linux")]
fn forward_linux_reopen(path: &Path, pid: u32) -> std::io::Result<()> {
let actions = linux_reopen_action_payload(std::env::args().skip(1))?;
@@ -819,7 +935,20 @@ fn forward_linux_reopen(path: &Path, pid: u32) -> std::io::Result<()> {
std::thread::Builder::new()
.name("cap-gpui-reopen-request".into())
.spawn(move || {
- let _ = send.send(send_linux_reopen(address, owner, &actions));
+ // An instance from before activation tokens rejects the frame
+ // that carries one; it still reopens without it.
+ let token = launch_activation_token();
+ if token.is_some() {
+ tracing::info!("forwarding the launcher's activation token");
+ }
+ let result = match send_linux_reopen_with_token(address.clone(), owner, &actions, token.as_deref()) {
+ Err(error) if token.is_some() && closed_without_queueing(&error) => {
+ tracing::info!(%error, "the running Cap refused the activation token; reopening without it");
+ send_linux_reopen(address, owner, &actions)
+ }
+ result => result,
+ };
+ let _ = send.send(result);
})?;
receive.recv_timeout(LINUX_REOPEN_TIMEOUT).map_err(|_| {
std::io::Error::new(
@@ -841,6 +970,35 @@ fn linux_reopen_allowed(
&& mode == Some(crate::recording::RecordingMode::Studio)))
}
+/// A hidden main window maps again a moment after it is shown, and an
+/// activation sent before then has no window to raise; the compositor drops
+/// repeats once the token is used.
+#[cfg(target_os = "linux")]
+fn raise_shown_window_with_token(token: String, cx: &mut gpui::App) {
+ let windows = cx.global::<crate::app_windows::AppWindows>();
+ // The reopen brings onboarding forward in place of the main window, which
+ // stays hidden (and unmapped) until onboarding is done.
+ let target: gpui::AnyWindowHandle = match windows.onboarding {
+ Some(onboarding) => onboarding.into(),
+ None => windows.main.into(),
+ };
+ cx.spawn(async move |cx| {
+ for delay in [0, 150, 400, 1000] {
+ cx.background_executor()
+ .timer(std::time::Duration::from_millis(delay))
+ .await;
+ let token = token.clone();
+ if let Err(error) =
+ target.update(cx, |_, window, _| window.activate_window_with_token(token))
+ {
+ tracing::warn!(%error, "the reopened window was gone before it could be raised");
+ return;
+ }
+ }
+ })
+ .detach();
+}
+
#[cfg(target_os = "linux")]
pub fn init_linux_reopen(cx: &mut gpui::App) {
let requests = linux_reopen_channel().1.clone();
@@ -855,8 +1013,16 @@ pub fn init_linux_reopen(cx: &mut gpui::App) {
}
let session = crate::session::RecordingSession::global(cx);
let current = session.read(cx);
+ let token = pending_activation_token()
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .take();
if linux_reopen_allowed(current.phase, current.mode(), current.instant_cleanup_safe()) {
crate::app_windows::show_main_window(cx);
+ if let Some(token) = token {
+ tracing::info!("raising the reopened window with the relaunch's activation token");
+ raise_shown_window_with_token(token, cx);
+ }
} else {
tracing::info!("Cap controls remain hidden while recording is changing state or cannot pause");
}
@@ -2370,6 +2536,91 @@ mod linux_reopen_tests {
payload
}
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn only_a_hang_up_resends_the_request_without_its_token() {
+ use std::io::{Read, Write};
+ let owner = linux_instance_identity(std::process::id()).unwrap();
+ let reply = |server: Box<dyn FnOnce(std::os::unix::net::UnixStream) + Send>| {
+ let (mut client, server_end) = std::os::unix::net::UnixStream::pair().unwrap();
+ client
+ .set_read_timeout(Some(std::time::Duration::from_millis(200)))
+ .unwrap();
+ let serving = std::thread::spawn(move || server(server_end));
+ write_linux_reopen_request(&mut client, owner, &[], Some("token")).unwrap();
+ let mut ack = [0];
+ let error = client.read_exact(&mut ack).unwrap_err();
+ serving.join().unwrap();
+ error
+ };
+
+ // An instance from before activation tokens reads the frame, fails to
+ // parse it and hangs up.
+ let rejected = reply(Box::new(|mut stream| {
+ let mut frame = [0; 8];
+ stream.read_exact(&mut frame).unwrap();
+ drop(stream);
+ }));
+ assert!(closed_without_queueing(&rejected), "{rejected:?}");
+
+ // One that queued the actions but has not acknowledged them yet.
+ let slow = reply(Box::new(|mut stream| {
+ let mut frame = [0; 8];
+ stream.read_exact(&mut frame).unwrap();
+ std::thread::sleep(std::time::Duration::from_millis(400));
+ let _ = stream.write_all(&[1]);
+ }));
+ assert!(!closed_without_queueing(&slow), "{slow:?}");
+ }
+
+ /// A relaunch from a launcher brings an activation token, which the
+ /// running instance needs to raise its window past focus stealing
+ /// prevention. Without one the frame stays the legacy one.
+ #[test]
+ fn a_relaunch_forwards_its_activation_token_with_its_actions() {
+ let owner = linux_instance_identity(std::process::id()).unwrap();
+ let mut plain = Vec::new();
+ write_linux_reopen_request(&mut plain, owner, &[], None).unwrap();
+ assert_eq!(plain, linux_reopen_payload(owner));
+
+ let mut bare = Vec::new();
+ write_linux_reopen_request(&mut bare, owner, &[], Some("gnome-shell/Cap/1234-5-host_TIME7")).unwrap();
+ assert_eq!(&bare[..8], b"CAPACTV1");
+ assert_eq!(
+ read_linux_reopen_request(&mut bare.as_slice(), owner).unwrap(),
+ (Vec::new(), Some("gnome-shell/Cap/1234-5-host_TIME7".to_string()))
+ );
+
+ let payload =
+ linux_reopen_action_payload([action_url(&DeepLinkAction::StopRecording)]).unwrap();
+ let mut with_actions = Vec::new();
+ write_linux_reopen_request(&mut with_actions, owner, &payload, Some("token")).unwrap();
+ assert_eq!(
+ read_linux_reopen_request(&mut with_actions.as_slice(), owner).unwrap(),
+ (vec![DeepLinkAction::StopRecording], Some("token".to_string()))
+ );
+
+ // A token with spaces or control characters is not a compositor's.
+ let mut odd = Vec::new();
+ write_linux_reopen_request(&mut odd, owner, &[], Some("two words")).unwrap();
+ assert_eq!(odd, linux_reopen_payload(owner));
+ let mut forged = linux_reopen_payload(owner).to_vec();
+ forged[..8].copy_from_slice(b"CAPACTV1");
+ forged.extend_from_slice(&3u32.to_be_bytes());
+ forged.extend_from_slice(b"a\nb");
+ forged.extend_from_slice(&0u32.to_be_bytes());
+ assert!(read_linux_reopen_request(&mut forged.as_slice(), owner).is_err());
+
+ // Another instance's frame is refused, token or not.
+ let other = LinuxInstanceIdentity {
+ pid: owner.pid.wrapping_add(1),
+ ..owner
+ };
+ let mut foreign = Vec::new();
+ write_linux_reopen_request(&mut foreign, other, &[], Some("token")).unwrap();
+ assert!(read_linux_reopen_request(&mut foreign.as_slice(), owner).is_err());
+ }
+
#[test]
fn duplicate_launch_during_startup_is_authenticated_and_coalesced() {
let owner = linux_instance_identity(std::process::id()).unwrap();
diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs
index 5d10069aa..227371fd3 100644
--- a/apps/desktop-gpui/src/store.rs
+++ b/apps/desktop-gpui/src/store.rs
@@ -24,6 +24,8 @@ use std::{
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
+use crate::i18n::tr;
+
/// `CameraPreviewShape` in the web app.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
@@ -711,7 +713,7 @@ macro_rules! settings_enum {
(
$(#[$meta:meta])*
$name:ident {
- $($variant:ident = $json:literal, $label:literal;)+
+ $($variant:ident = $json:literal, $label:expr;)+
}
default = $default:ident
) => {
@@ -756,9 +758,9 @@ settings_enum! {
/// `AppTheme`. The previews are labelled System/Light/Dark in
/// `AppearanceSection`.
AppTheme {
- System = "system", "System";
- Light = "light", "Light";
- Dark = "dark", "Dark";
+ System = "system", tr::settings::general::appearance::system();
+ Light = "light", tr::settings::general::appearance::light();
+ Dark = "dark", tr::settings::general::appearance::dark();
}
default = System
}
diff --git a/apps/desktop-gpui/src/target_overlay.rs b/apps/desktop-gpui/src/target_overlay.rs
index 3afd73bc7..430a049bf 100644
--- a/apps/desktop-gpui/src/target_overlay.rs
+++ b/apps/desktop-gpui/src/target_overlay.rs
@@ -481,6 +481,180 @@ impl TargetSelect {
}
}
+/// The aspect ratios the area toolbar cycles through, `None` first for a free
+/// selection. The 0.6.0 picker offered the same set (`COMMON_RATIOS`).
+const AREA_RATIOS: [Option<(u32, u32)>; 9] = [
+ None,
+ Some((16, 9)),
+ Some((4, 3)),
+ Some((1, 1)),
+ Some((9, 16)),
+ Some((16, 10)),
+ Some((21, 9)),
+ Some((3, 2)),
+ Some((2, 1)),
+];
+
+const AREA_SELECTION_STORE: &str = "gpui_area_selection";
+
+fn stored_area_ratio() -> Option<(u32, u32)> {
+ let section = crate::store::store_section(AREA_SELECTION_STORE);
+ let ratio = section.get("aspectRatio")?.as_array()?;
+ let [width, height] = ratio.as_slice() else {
+ return None;
+ };
+ let ratio = (width.as_u64()? as u32, height.as_u64()? as u32);
+ AREA_RATIOS.contains(&Some(ratio)).then_some(ratio)
+}
+
+fn store_area_ratio(ratio: Option<(u32, u32)>) {
+ crate::store::update_store_section(AREA_SELECTION_STORE, |section| {
+ section.insert(
+ "aspectRatio".into(),
+ ratio.map_or(serde_json::Value::Null, |(width, height)| {
+ serde_json::json!([width, height])
+ }),
+ );
+ true
+ });
+}
+
+fn next_area_ratio(current: Option<(u32, u32)>) -> Option<(u32, u32)> {
+ let index = AREA_RATIOS
+ .iter()
+ .position(|ratio| *ratio == current)
+ .unwrap_or(0);
+ AREA_RATIOS[(index + 1) % AREA_RATIOS.len()]
+}
+
+fn ratio_value((width, height): (u32, u32)) -> f32 {
+ width as f32 / height.max(1) as f32
+}
+
+/// An area at `ratio` (width over height) spanning from the fixed `anchor`
+/// towards `pointer`: the longer span wins, and the area shrinks to stay on
+/// the display.
+fn area_from_anchor_with_ratio(
+ anchor: (f32, f32),
+ pointer: (f32, f32),
+ ratio: f32,
+ display: (f32, f32),
+) -> AreaRect {
+ let (dx, dy) = (pointer.0 - anchor.0, pointer.1 - anchor.1);
+ let (mut width, mut height) = (dx.abs(), dy.abs());
+ if width / ratio >= height {
+ height = width / ratio;
+ } else {
+ width = height * ratio;
+ }
+ let room_x = if dx >= 0. {
+ display.0 - anchor.0
+ } else {
+ anchor.0
+ };
+ let room_y = if dy >= 0. {
+ display.1 - anchor.1
+ } else {
+ anchor.1
+ };
+ let fit = [
+ 1.,
+ room_x / width.max(f32::EPSILON),
+ room_y / height.max(f32::EPSILON),
+ ]
+ .into_iter()
+ .fold(f32::INFINITY, f32::min)
+ .max(0.);
+ width *= fit;
+ height *= fit;
+ AreaRect {
+ x: if dx >= 0. { anchor.0 } else { anchor.0 - width },
+ y: if dy >= 0. {
+ anchor.1
+ } else {
+ anchor.1 - height
+ },
+ width,
+ height,
+ }
+}
+
+/// An edge-handle resize at `ratio`: the opposite edge stays put, the dragged
+/// edge follows the pointer to whichever side of it the pointer is, and the
+/// other span grows around the area's centre line, shrinking to stay on the
+/// display.
+fn area_edge_resize_with_ratio(
+ handle: AreaHandle,
+ start: AreaRect,
+ pointer: (f32, f32),
+ ratio: f32,
+ display: (f32, f32),
+) -> AreaRect {
+ let (center_x, center_y) = (start.x + start.width / 2., start.y + start.height / 2.);
+ if handle.east() || handle.west() {
+ let anchor = if handle.west() {
+ start.right()
+ } else {
+ start.x
+ };
+ let before = pointer.0 < anchor;
+ let room_x = if before { anchor } else { display.0 - anchor };
+ let room_y = 2. * center_y.min(display.1 - center_y);
+ let width = (pointer.0 - anchor)
+ .abs()
+ .min(room_x)
+ .min(room_y * ratio)
+ .max(0.);
+ let height = width / ratio;
+ AreaRect {
+ x: if before { anchor - width } else { anchor },
+ y: center_y - height / 2.,
+ width,
+ height,
+ }
+ } else {
+ let anchor = if handle.north() {
+ start.bottom()
+ } else {
+ start.y
+ };
+ let before = pointer.1 < anchor;
+ let room_y = if before { anchor } else { display.1 - anchor };
+ let room_x = 2. * center_x.min(display.0 - center_x);
+ let height = (pointer.1 - anchor)
+ .abs()
+ .min(room_y)
+ .min(room_x / ratio)
+ .max(0.);
+ let width = height * ratio;
+ AreaRect {
+ x: center_x - width / 2.,
+ y: if before { anchor - height } else { anchor },
+ width,
+ height,
+ }
+ }
+}
+
+/// An existing area brought to `ratio` around its centre, keeping its width
+/// unless that would not fit the display.
+fn area_with_ratio(area: AreaRect, ratio: f32, display: (f32, f32)) -> AreaRect {
+ let (center_x, center_y) = (area.x + area.width / 2., area.y + area.height / 2.);
+ let mut width = area.width.min(display.0);
+ let mut height = width / ratio;
+ if height > display.1 {
+ height = display.1;
+ width = height * ratio;
+ }
+ AreaRect {
+ x: center_x - width / 2.,
+ y: center_y - height / 2.,
+ width,
+ height,
+ }
+ .clamped(display)
+}
+
/// A rectangle in the overlay's own coordinate space: display-relative logical
/// points, which is also what `ScreenCaptureTarget::Area` wants.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -654,6 +828,8 @@ pub struct OverlayWindow {
crop: Option<AreaRect>,
drag: Option<AreaDrag>,
snap_guides: Vec<SnapGuide>,
+ /// The aspect ratio a drawn or resized area keeps, `None` for free.
+ area_ratio: Option<(u32, u32)>,
recording_area: bool,
/// The mode menu the start pill's caret drops down -- `menuModes()`
/// popped up at the event location (`target-select-overlay.tsx:2064-2090,
@@ -799,6 +975,7 @@ impl OverlayWindow {
crop: None,
drag: None,
snap_guides: Vec::new(),
+ area_ratio: stored_area_ratio(),
recording_area: false,
mode_menu: None,
countdown_menu: None,
@@ -1786,7 +1963,7 @@ impl OverlayWindow {
if crop.is_none() && !interacting {
root = root.child(self.render_selection_hint(hint_started, window, cx));
}
- root = root.child(self.render_area_toolbar(crop, valid));
+ root = root.child(self.render_area_toolbar(crop, valid, cx));
if crop.is_some() {
root.child(self.render_area_controls(crop, valid, cx))
} else {
@@ -2033,21 +2210,52 @@ impl OverlayWindow {
}
/// The floating readout: `top-12 left-1/2 -translate-x-1/2` over the
- /// liquid-glass surface, `min-w-28 px-2 text-base tabular-nums`. The
- /// aspect-ratio, reset, fill and lock controls that share this bar in the
- /// TSX are deferred (README).
- fn render_area_toolbar(&self, crop: Option<AreaRect>, valid: bool) -> impl IntoElement {
+ /// liquid-glass surface, `min-w-28 px-2 text-base tabular-nums`, and the
+ /// aspect-ratio control beside it. The reset, fill and lock controls that
+ /// share this bar in the TSX are deferred (README).
+ fn render_area_toolbar(
+ &self,
+ crop: Option<AreaRect>,
+ valid: bool,
+ cx: &mut Context<Self>,
+ ) -> impl IntoElement {
let label = match crop.filter(|_| valid) {
Some(crop) => format!("{} × {}", crop.width.round(), crop.height.round()),
None => "Draw an area".to_string(),
};
+ let ratio_label = match self.area_ratio {
+ Some((width, height)) => format!("{width}:{height}"),
+ None => "Free".to_string(),
+ };
+ let ratio = div()
+ .id("area-aspect-ratio")
+ .h(px(36.))
+ .min_w(px(56.))
+ .px(px(10.))
+ .flex()
+ .items_center()
+ .justify_center()
+ .rounded(px(10.))
+ .cursor_pointer()
+ .text_size(px(14.))
+ .font_weight(FontWeight::MEDIUM)
+ .bg(self.theme.gray_3)
+ .hover({
+ let hover = self.theme.gray_4;
+ move |style| style.bg(hover)
+ })
+ .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
+ .on_click(cx.listener(|this, _, _, cx| {
+ this.set_area_ratio(next_area_ratio(this.area_ratio), cx);
+ }))
+ .child(ratio_label);
div()
.absolute()
// `top-12` on macOS, centred by hand: gpui has no translate.
.top(px(48.))
- .left(px((self.display_size.0 - 200.) / 2.))
- .w(px(200.))
+ .left(px((self.display_size.0 - 280.) / 2.))
+ .w(px(280.))
.flex()
.justify_center()
.child(
@@ -2070,10 +2278,23 @@ impl OverlayWindow {
// that the window root carries Medium.
.font_weight(FontWeight::NORMAL)
.child(label),
- ),
+ )
+ .child(ratio),
)
}
+ /// Locks drawn and resized areas to `ratio`, reshaping the current one
+ /// around its centre, and remembers the choice for the next picker.
+ fn set_area_ratio(&mut self, ratio: Option<(u32, u32)>, cx: &mut Context<Self>) {
+ self.area_ratio = ratio;
+ if let (Some(ratio), Some(crop)) = (ratio, self.crop) {
+ self.crop = Some(area_with_ratio(crop, ratio_value(ratio), self.display_size));
+ self.sync_area_camera(cx);
+ }
+ store_area_ratio(ratio);
+ cx.notify();
+ }
+
/// The controls cluster, placed against the crop the way `controlsStyle`
/// places it: below when it fits, above when it does not, inside the crop
/// otherwise.
@@ -2545,8 +2766,33 @@ impl OverlayWindow {
let x = f32::from(position.x).clamp(0., self.display_size.0);
let y = f32::from(position.y).clamp(0., self.display_size.1);
- let (next, guides) = match drag {
- AreaDrag::Draw { anchor } => {
+ let ratio = self.area_ratio.map(ratio_value);
+ let (next, guides) = match (drag, ratio) {
+ (AreaDrag::Draw { anchor }, Some(ratio)) => (
+ area_from_anchor_with_ratio(*anchor, (x, y), ratio, self.display_size),
+ Vec::new(),
+ ),
+ (AreaDrag::Resize { handle, start }, Some(ratio)) => (
+ if handle.east() != handle.west() && handle.north() != handle.south() {
+ let anchor = (
+ if handle.west() {
+ start.right()
+ } else {
+ start.x
+ },
+ if handle.north() {
+ start.bottom()
+ } else {
+ start.y
+ },
+ );
+ area_from_anchor_with_ratio(anchor, (x, y), ratio, self.display_size)
+ } else {
+ area_edge_resize_with_ratio(*handle, *start, (x, y), ratio, self.display_size)
+ },
+ Vec::new(),
+ ),
+ (AreaDrag::Draw { anchor }, None) => {
let ((x, y), guides) = snap_area_point((x, y), self.display_size);
(
AreaRect {
@@ -2558,7 +2804,7 @@ impl OverlayWindow {
guides,
)
}
- AreaDrag::Move { grab, start } => snap_area_rect(
+ (AreaDrag::Move { grab, start }, _) => snap_area_rect(
AreaRect {
x: start.x + (x - grab.0),
y: start.y + (y - grab.1),
@@ -2566,7 +2812,7 @@ impl OverlayWindow {
},
self.display_size,
),
- AreaDrag::Resize { handle, start } => {
+ (AreaDrag::Resize { handle, start }, None) => {
let mut left = start.x;
let mut top = start.y;
let mut right = start.right();
@@ -2658,6 +2904,114 @@ fn request_media_access(permission: crate::permissions::OSPermission, cx: &mut A
mod tests {
use super::*;
+ fn ratio_of(area: AreaRect) -> f32 {
+ area.width / area.height
+ }
+
+ fn inside(area: AreaRect, display: (f32, f32)) -> bool {
+ area.x >= -0.01
+ && area.y >= -0.01
+ && area.right() <= display.0 + 0.01
+ && area.bottom() <= display.1 + 0.01
+ }
+
+ /// A 0.6.1 report: the 0.6.0 picker could hold an area at 16:9, and the
+ /// new one could not. Drawing with a ratio keeps it from any direction.
+ #[test]
+ fn a_drawn_area_keeps_the_chosen_aspect_ratio() {
+ let display = (1920., 1080.);
+ let ratio = ratio_value((16, 9));
+ for pointer in [(900., 300.), (100., 900.), (1500., 1000.), (120., 140.)] {
+ let area = area_from_anchor_with_ratio((800., 500.), pointer, ratio, display);
+ assert!(
+ (ratio_of(area) - ratio).abs() < 0.001,
+ "{pointer:?}: {area:?}"
+ );
+ assert!(inside(area, display), "{pointer:?}: {area:?}");
+ }
+ // The longer span wins: a wide drag sets the width.
+ let wide = area_from_anchor_with_ratio((100., 100.), (900., 300.), ratio, display);
+ assert_eq!((wide.x, wide.y, wide.width), (100., 100., 800.));
+ assert!((wide.height - 450.).abs() < 0.01);
+ // A drag that would leave the display shrinks to fit instead.
+ let edge = area_from_anchor_with_ratio((1800., 100.), (1900., 1000.), ratio, display);
+ assert!(inside(edge, display) && (ratio_of(edge) - ratio).abs() < 0.001);
+ }
+
+ #[test]
+ fn resizing_an_area_keeps_the_chosen_aspect_ratio() {
+ let display = (1920., 1080.);
+ let ratio = ratio_value((1, 1));
+ let start = AreaRect {
+ x: 400.,
+ y: 300.,
+ width: 400.,
+ height: 400.,
+ };
+ for handle in AreaHandle::ALL {
+ for pointer in [(1000., 900.), (100., 50.), (650., 520.)] {
+ let area = if handle.east() != handle.west() && handle.north() != handle.south() {
+ let anchor = (
+ if handle.west() {
+ start.right()
+ } else {
+ start.x
+ },
+ if handle.north() {
+ start.bottom()
+ } else {
+ start.y
+ },
+ );
+ area_from_anchor_with_ratio(anchor, pointer, ratio, display)
+ } else {
+ area_edge_resize_with_ratio(handle, start, pointer, ratio, display)
+ };
+ assert!(
+ area.height == 0. || (ratio_of(area) - ratio).abs() < 0.001,
+ "{handle:?} {pointer:?}: {area:?}"
+ );
+ assert!(inside(area, display), "{handle:?} {pointer:?}: {area:?}");
+ }
+ }
+ // An east edge drag keeps the west edge and the centre line.
+ let east =
+ area_edge_resize_with_ratio(AreaHandle::East, start, (1000., 0.), ratio, display);
+ assert_eq!((east.x, east.width), (400., 600.));
+ assert_eq!(east.y + east.height / 2., 500.);
+ // Past the opposite edge, the selection follows the pointer over it.
+ let crossed =
+ area_edge_resize_with_ratio(AreaHandle::East, start, (300., 0.), ratio, display);
+ assert_eq!((crossed.x, crossed.width), (300., 100.));
+ let crossed =
+ area_edge_resize_with_ratio(AreaHandle::South, start, (0., 200.), ratio, display);
+ assert_eq!((crossed.y, crossed.height), (200., 100.));
+ let crossed =
+ area_edge_resize_with_ratio(AreaHandle::West, start, (900., 0.), ratio, display);
+ assert_eq!((crossed.x, crossed.width), (800., 100.));
+ }
+
+ #[test]
+ fn choosing_a_ratio_reshapes_the_area_around_its_centre() {
+ let display = (1920., 1080.);
+ let area = area_with_ratio(
+ AreaRect {
+ x: 100.,
+ y: 300.,
+ width: 800.,
+ height: 200.,
+ },
+ ratio_value((16, 9)),
+ display,
+ );
+ assert_eq!((area.x, area.width), (100., 800.));
+ assert!((area.height - 450.).abs() < 0.01);
+ assert!((area.y + area.height / 2. - 400.).abs() < 0.01);
+ assert!(inside(area, display));
+ assert_eq!(next_area_ratio(None), Some((16, 9)));
+ assert_eq!(next_area_ratio(Some((2, 1))), None);
+ }
+
#[test]
fn a_single_overlay_remains_interactive_without_a_global_cursor_probe() {
let display = "1".parse::<DisplayId>().unwrap();
diff --git a/apps/desktop-gpui/src/transcription.rs b/apps/desktop-gpui/src/transcription.rs
index 5ea9ccdaa..92fa942a9 100644
--- a/apps/desktop-gpui/src/transcription.rs
+++ b/apps/desktop-gpui/src/transcription.rs
@@ -1856,167 +1856,10 @@ fn extract_audio_from_video(video_path: &Path, output_path: &Path) -> Result<(),
// Caption words & chunking
// ---------------------------------------------------------------------------
-/// `MAX_CAPTION_WORD_DURATION` (`captions.rs:75`): Whisper/Parakeet sometimes
-/// stretch a trailing word's end across a following silence, which leaves the
-/// rendered caption stuck on screen and duplicates the word across timeline
-/// cuts once projected.
-pub const MAX_CAPTION_WORD_DURATION: f32 = 2.5;
-/// `captions.rs:67-69`.
-const TARGET_CAPTION_WORDS_PER_SEGMENT: usize = 6;
-const MAX_CAPTION_WORDS_PER_SEGMENT: usize = 8;
-const MIN_FINAL_CAPTION_WORDS: usize = 3;
-
-/// `caption_char_attaches_to_previous` (`captions.rs:745-767`).
-fn caption_char_attaches_to_previous(value: char) -> bool {
- matches!(
- value,
- ',' | '.'
- | '!'
- | '?'
- | ';'
- | ':'
- | '%'
- | ')'
- | ']'
- | '}'
- | '\''
- | '’'
- | '、'
- | '。'
- | '!'
- | '?'
- | ';'
- | ':'
- | ','
- )
-}
-
-fn caption_token_attaches_to_previous(text: &str) -> bool {
- text.trim()
- .chars()
- .next()
- .is_some_and(caption_char_attaches_to_previous)
-}
-
-/// `caption_boundary_word_is_weak` (`captions.rs:769-801`).
-fn caption_boundary_word_is_weak(word: &CaptionWord) -> bool {
- let normalized = word
- .text
- .trim()
- .chars()
- .filter(|c| c.is_alphanumeric())
- .collect::<String>()
- .to_lowercase();
-
- normalized.chars().count() <= 1
- || matches!(
- normalized.as_str(),
- "an" | "as"
- | "at"
- | "be"
- | "by"
- | "do"
- | "he"
- | "if"
- | "in"
- | "is"
- | "it"
- | "me"
- | "my"
- | "of"
- | "on"
- | "or"
- | "so"
- | "to"
- | "up"
- | "we"
- )
-}
-
-/// `normalize_caption_words` (`captions.rs:803-831`).
-fn normalize_caption_words(words: Vec<CaptionWord>) -> Vec<CaptionWord> {
- let mut normalized: Vec<CaptionWord> = Vec::with_capacity(words.len());
-
- for word in words {
- let text = word.text.trim();
- if text.is_empty() {
- continue;
- }
-
- if caption_token_attaches_to_previous(text)
- && let Some(previous) = normalized.last_mut()
- {
- previous.text.push_str(text);
- previous.end = word.end;
- } else {
- normalized.push(CaptionWord {
- text: text.to_string(),
- start: word.start,
- end: word.end,
- });
- }
- }
-
- for word in &mut normalized {
- word.end = word.end.min(word.start + MAX_CAPTION_WORD_DURATION);
- }
-
- normalized
-}
-
-/// `caption_text_from_words` (`captions.rs:833-849`).
-fn caption_text_from_words<'a>(words: impl IntoIterator<Item = &'a CaptionWord>) -> String {
- let mut text = String::new();
-
- for word in words {
- let word_text = word.text.trim();
- if word_text.is_empty() {
- continue;
- }
-
- if !text.is_empty() && !caption_token_attaches_to_previous(word_text) {
- text.push(' ');
- }
- text.push_str(word_text);
- }
-
- text
-}
-
-/// `caption_word_chunks` (`captions.rs:851-883`).
-fn caption_word_chunks(words: &[CaptionWord]) -> Vec<&[CaptionWord]> {
- let mut chunks = Vec::new();
- let mut start = 0;
-
- while start < words.len() {
- let remaining = words.len() - start;
- if remaining <= TARGET_CAPTION_WORDS_PER_SEGMENT {
- chunks.push(&words[start..]);
- break;
- }
-
- let mut end = (start + TARGET_CAPTION_WORDS_PER_SEGMENT).min(words.len());
- while end < words.len()
- && caption_boundary_word_is_weak(&words[end - 1])
- && end - start < MAX_CAPTION_WORDS_PER_SEGMENT
- {
- end += 1;
- }
-
- let remaining_after = words.len() - end;
- if remaining_after > 0
- && remaining_after < MIN_FINAL_CAPTION_WORDS
- && caption_boundary_word_is_weak(&words[end])
- {
- end = words.len();
- }
-
- chunks.push(&words[start..end]);
- start = end;
- }
-
- chunks
-}
+use cap_project::caption_words::{
+ caption_text_from_words, caption_word_chunks, normalize_caption_words,
+ MAX_CAPTION_WORD_DURATION,
+};
// ---------------------------------------------------------------------------
// Track derivation -- deriveCaptionTrackSegments, in Rust
diff --git a/apps/desktop-gpui/src/tray.rs b/apps/desktop-gpui/src/tray.rs
index db131a103..3e6d9c9e7 100644
--- a/apps/desktop-gpui/src/tray.rs
+++ b/apps/desktop-gpui/src/tray.rs
@@ -807,35 +807,17 @@ pub(crate) fn open_previous_item(path: PathBuf, cx: &mut App) {
}
RecordingMetaInner::Instant(_) => {
if let Some(sharing) = &meta.sharing {
- #[cfg(target_os = "windows")]
cx.open_url(&sharing.link);
- #[cfg(not(target_os = "windows"))]
- open_with_finder(&sharing.link);
} else {
let mp4 = path.join("content/output.mp4");
if mp4.exists() {
- #[cfg(target_os = "windows")]
- cx.open_with_system(&mp4);
- #[cfg(not(target_os = "windows"))]
- open_with_finder(&mp4.to_string_lossy());
+ library::open_with_system(&mp4, cx);
}
}
}
}
}
-/// `tauri_plugin_opener`'s `open_url` / `open_path`, which on macOS are both
-/// `open <thing>` -- the same spawn `library::open_recording_folder` uses.
-#[cfg(not(target_os = "windows"))]
-fn open_with_finder(target: &str) {
- #[cfg(target_os = "macos")]
- if let Err(error) = std::process::Command::new("open").arg(target).spawn() {
- tracing::warn!(target, "opening from the tray failed: {error}");
- }
- #[cfg(not(target_os = "macos"))]
- let _ = target;
-}
-
/// The status item was clicked while a recording is running: stop it, exactly
/// as `on_tray_icon_event` does.
fn stop_recording(cx: &mut App) {
diff --git a/apps/desktop-gpui/src/ui/menu.rs b/apps/desktop-gpui/src/ui/menu.rs
index 01bbd8af4..e57dab93e 100644
--- a/apps/desktop-gpui/src/ui/menu.rs
+++ b/apps/desktop-gpui/src/ui/menu.rs
@@ -13,13 +13,19 @@
use std::{cell::Cell, rc::Rc};
use gpui::{
- Anchor, App, Bounds, ClickEvent, ElementId, Hsla, InteractiveElement, IntoElement,
- ParentElement, Pixels, Point, RenderOnce, SharedString, Size, StatefulInteractiveElement,
- Styled, Window, div, point, prelude::FluentBuilder, px, svg,
+ Anchor, App, Bounds, ClickEvent, ElementId, Hsla, InteractiveElement, IntoElement, MouseButton,
+ MouseUpEvent, ParentElement, Pixels, Point, RenderOnce, ScrollHandle, SharedString, Size,
+ StatefulInteractiveElement, Styled, TextStyle, WhiteSpace, Window, div, point,
+ prelude::FluentBuilder, px, svg,
};
use crate::theme::Theme;
+const MENU_ROW_HEIGHT: f32 = 24.;
+const MENU_PADDING: f32 = 4.;
+const MENU_BORDER: f32 = 1.;
+const MENU_CHROME_HEIGHT: f32 = 2. * (MENU_PADDING + MENU_BORDER);
+
pub(crate) type OpenHandler = Box<dyn Fn(&Bounds<Pixels>, &mut Window, &mut App) + 'static>;
/// One row: a label and whether it is the value currently in force.
@@ -294,7 +300,7 @@ fn anchored_placement(
let margin = px(12.);
let below = (viewport.height - margin - trigger.bottom() - gap).max(px(0.));
let above = (trigger.top() - gap - margin).max(px(0.));
- let height = (px(item_count as f32 * 24. + 10.)).min(max_height);
+ let height = px(item_count as f32 * MENU_ROW_HEIGHT + MENU_CHROME_HEIGHT).min(max_height);
if height <= below || below >= above {
(
trigger.bottom_left() + point(px(0.), gap),
@@ -310,6 +316,54 @@ fn anchored_placement(
}
}
+// Offscreen labels still determine the menu's intrinsic width.
+#[derive(Default)]
+struct MenuLayoutState {
+ scroll: ScrollHandle,
+ pressed: Rc<Cell<bool>>,
+ labels: Vec<SharedString>,
+ style: Option<TextStyle>,
+ widest: usize,
+}
+
+impl MenuLayoutState {
+ fn widest(&mut self, items: &[MenuItem], style: TextStyle, window: &Window) -> usize {
+ if self.style.as_ref() != Some(&style)
+ || self.labels.len() != items.len()
+ || self
+ .labels
+ .iter()
+ .zip(items)
+ .any(|(label, item)| label != &item.label)
+ {
+ let mut widest = 0;
+ let mut width = px(0.);
+ for (index, item) in items.iter().enumerate() {
+ let runs = [style.to_run(item.label.len())];
+ if let Ok(lines) = window.text_system().shape_text(
+ item.label.clone(),
+ px(12.),
+ &runs,
+ None,
+ style.line_clamp,
+ ) {
+ for line in lines {
+ let line_width = line.size(px(MENU_ROW_HEIGHT)).width.ceil();
+ if line_width > width {
+ width = line_width;
+ widest = index;
+ }
+ }
+ }
+ }
+ self.labels = items.iter().map(|item| item.label.clone()).collect();
+ self.style = Some(style);
+ self.widest = widest;
+ }
+ self.widest
+ }
+}
+
impl RenderOnce for Menu {
fn render(self, window: &mut Window, _cx: &mut App) -> impl IntoElement {
let Menu {
@@ -343,6 +397,124 @@ impl RenderOnce for Menu {
.map_or((origin, Anchor::TopLeft, max_height), |bounds| {
anchored_placement(bounds, items.len(), max_height, viewport)
});
+ let mut style = window.text_style();
+ style.font_size = px(12.).into();
+ style.color = text;
+ style.white_space = WhiteSpace::Nowrap;
+ let line_height = window.pixel_snap(
+ style
+ .line_height
+ .to_pixels(px(12.).into(), window.rem_size()),
+ );
+ // Tall labels can overlap adjacent rows, so row-box culling would hide visible text.
+ let virtualized = !items.is_empty()
+ && max_height > px(MENU_CHROME_HEIGHT)
+ && px(items.len() as f32 * MENU_ROW_HEIGHT + MENU_CHROME_HEIGHT) > max_height
+ && line_height > px(0.)
+ && line_height <= px(MENU_ROW_HEIGHT)
+ && items.iter().all(|item| !item.label.contains('\n'));
+ let (widest, scroll, pressed) = window.with_element_namespace(id.clone(), |window| {
+ window.with_global_id("menu-layout".into(), |global_id, window| {
+ window.with_element_state(global_id, |state: Option<MenuLayoutState>, window| {
+ let mut state = state.unwrap_or_default();
+ // Keep GPUI's pending click state alive if a pressed row scrolls offscreen.
+ let widest = (virtualized && !state.pressed.get())
+ .then(|| state.widest(&items, style, window));
+ ((widest, state.scroll.clone(), state.pressed.clone()), state)
+ })
+ })
+ });
+ let row = move |row_prefix: &SharedString, index: usize, item: MenuItem| {
+ let handler = handler.clone();
+ div()
+ .id(SharedString::from(format!("{row_prefix}-item-{index}")))
+ .flex()
+ .flex_row()
+ .flex_shrink_0()
+ .items_center()
+ .gap(px(6.))
+ .h(px(MENU_ROW_HEIGHT))
+ .px(px(6.))
+ .rounded(px(4.))
+ .when(highlighted == Some(index), |this| this.bg(hover))
+ .hover(move |style| style.bg(hover))
+ .child(
+ div().w(px(12.)).flex_shrink_0().children(
+ item.checked
+ .then(|| svg().path("icons/check.svg").size(px(12.)).text_color(text)),
+ ),
+ )
+ .child(div().flex_1().min_w_0().truncate().child(item.label))
+ .when_some(handler, |this, handler| {
+ this.on_click(move |_, window, cx| handler(&index, window, cx))
+ })
+ };
+ let pressed_on_down = pressed.clone();
+ let end_press = move |_: &MouseUpEvent, window: &mut Window, _: &mut App| {
+ if pressed.replace(false) {
+ window.refresh();
+ }
+ };
+ let panel = div()
+ .id(id.clone())
+ .occlude()
+ .flex()
+ .flex_col()
+ .min_w(min_width.min(max_width))
+ .max_w(max_width)
+ .max_h(max_height)
+ .overflow_y_scroll()
+ .track_scroll(&scroll)
+ .on_mouse_down(MouseButton::Left, move |_, window, _| {
+ pressed_on_down.set(true);
+ window.refresh();
+ })
+ .on_mouse_up(MouseButton::Left, end_press.clone())
+ .on_mouse_up_out(MouseButton::Left, end_press)
+ .p(px(MENU_PADDING))
+ .rounded(radius)
+ .shadow(shadow)
+ .border(px(MENU_BORDER))
+ .border_color(border)
+ .bg(bg)
+ .text_color(text)
+ .text_size(px(12.));
+ let panel = if let Some(widest) = widest {
+ let scroll_top = (-scroll.offset().y).clamp(
+ px(0.),
+ (px(items.len() as f32 * MENU_ROW_HEIGHT) - (max_height - px(MENU_CHROME_HEIGHT)))
+ .max(px(0.)),
+ );
+ let start =
+ ((f32::from(scroll_top) / MENU_ROW_HEIGHT).floor() as usize).saturating_sub(1);
+ let end = ((f32::from(scroll_top + max_height) / MENU_ROW_HEIGHT).ceil() as usize)
+ .saturating_add(1)
+ .min(items.len());
+ let width_prefix: SharedString = format!("{prefix}-width-row").into();
+ panel
+ .child(
+ div()
+ .id(SharedString::from(format!("{prefix}-intrinsic")))
+ .h(px(0.))
+ .flex_shrink_0()
+ .overflow_hidden()
+ .child(row(&width_prefix, widest, items[widest].clone())),
+ )
+ .child(div().h(px(start as f32 * MENU_ROW_HEIGHT)).flex_shrink_0())
+ .children((start..end).map(|index| row(&prefix, index, items[index].clone())))
+ .child(
+ div()
+ .h(px((items.len() - end) as f32 * MENU_ROW_HEIGHT))
+ .flex_shrink_0(),
+ )
+ } else {
+ panel.children(
+ items
+ .into_iter()
+ .enumerate()
+ .map(|(index, item)| row(&prefix, index, item)),
+ )
+ };
div()
.absolute()
@@ -350,7 +522,6 @@ impl RenderOnce for Menu {
.left_0()
.size_full()
.child(
- // Click-away dismiss, the way a native menu closes.
div()
.id(SharedString::from(format!("{prefix}-backdrop")))
.occlude()
@@ -367,54 +538,7 @@ impl RenderOnce for Menu {
.position(origin)
.anchor(anchor)
.snap_to_window_with_margin(px(12.))
- .child(
- div()
- .id(id)
- .occlude()
- .flex()
- .flex_col()
- .min_w(min_width.min(max_width))
- .max_w(max_width)
- .max_h(max_height)
- .overflow_y_scroll()
- .p(px(4.))
- .rounded(radius)
- .shadow(shadow)
- .border_1()
- .border_color(border)
- .bg(bg)
- .text_color(text)
- .text_size(px(12.))
- .children(items.into_iter().enumerate().map(|(index, item)| {
- let handler = handler.clone();
- div()
- .id(SharedString::from(format!("{prefix}-item-{index}")))
- .flex()
- .flex_row()
- .flex_shrink_0()
- .items_center()
- .gap(px(6.))
- .h(px(24.))
- .px(px(6.))
- .rounded(px(4.))
- .when(highlighted == Some(index), |this| this.bg(hover))
- .hover(move |style| style.bg(hover))
- .child(div().w(px(12.)).flex_shrink_0().children(
- item.checked.then(|| {
- svg()
- .path("icons/check.svg")
- .size(px(12.))
- .text_color(text)
- }),
- ))
- .child(div().flex_1().min_w_0().truncate().child(item.label))
- .when_some(handler, |this, handler| {
- this.on_click(move |_, window, cx| {
- handler(&index, window, cx)
- })
- })
- })),
- ),
+ .child(panel),
)
}
}
@@ -562,3 +686,196 @@ mod tests {
assert_eq!(menu.highlighted, Some(0));
}
}
+
+#[cfg(test)]
+mod virtual_menu_tests {
+ use super::*;
+ use gpui::{
+ AvailableSpace, FontFallbacks, FontFeatures, FontStyle, FontWeight, Modifiers, ScrollDelta,
+ ScrollWheelEvent, TestAppContext, TouchPhase, VisualTestContext, WindowHandle, size,
+ };
+
+ fn row_width(item: &MenuItem, style: &TextStyle, window: &mut Window, cx: &mut App) -> Pixels {
+ let mut label = div()
+ .flex_1()
+ .min_w_0()
+ .truncate()
+ .child(item.label.clone());
+ label.text_style().line_clamp = style.line_clamp;
+ let mut row = div()
+ .flex()
+ .flex_row()
+ .flex_shrink_0()
+ .items_center()
+ .gap(px(6.))
+ .h(px(24.))
+ .px(px(6.))
+ .font(style.font())
+ .line_height(style.line_height)
+ .text_size(px(12.))
+ .text_color(style.color)
+ .child(div().w(px(12.)).flex_shrink_0())
+ .child(label)
+ .into_any_element();
+ row.layout_as_root(
+ size(AvailableSpace::MaxContent, AvailableSpace::MinContent),
+ window,
+ cx,
+ )
+ .width
+ }
+
+ #[gpui::test]
+ fn menu_width_uses_the_widest_actual_row(cx: &mut TestAppContext) {
+ let window = cx.open_window(size(px(800.), px(600.)), |_, _| gpui::Empty);
+ let mut visual = VisualTestContext::from_window(window.into(), cx);
+ visual.draw(
+ point(px(0.), px(0.)),
+ size(AvailableSpace::MaxContent, AvailableSpace::MaxContent),
+ |window, cx| {
+ let items = [
+ "",
+ "Small",
+ "Device\tTab\tName",
+ "日本語のフォント",
+ "اسم جهاز בעברית",
+ "e\u{301} and ligatures ffi",
+ "short\nA much wider second line of the label",
+ ]
+ .into_iter()
+ .map(|label| MenuItem::new(label, false))
+ .collect::<Vec<_>>();
+ let mut cache = MenuLayoutState::default();
+ for line_clamp in [None, Some(1), Some(2)] {
+ let style = TextStyle {
+ font_family: "Menu test family".into(),
+ font_features: FontFeatures::disable_ligatures(),
+ font_fallbacks: Some(FontFallbacks::from_fonts(vec![
+ "CJK fallback".into(),
+ "RTL fallback".into(),
+ ])),
+ font_weight: FontWeight::BOLD,
+ font_style: FontStyle::Italic,
+ font_size: px(12.).into(),
+ white_space: WhiteSpace::Nowrap,
+ line_clamp,
+ ..TextStyle::default()
+ };
+ let widest = cache.widest(&items, style.clone(), window);
+ let widths = items
+ .iter()
+ .map(|item| row_width(item, &style, window, cx))
+ .collect::<Vec<_>>();
+ assert_eq!(widths[widest], widths.iter().copied().max().unwrap());
+ }
+ div()
+ },
+ );
+ }
+
+ #[derive(Default)]
+ struct HeldClickMenu {
+ selected: Option<usize>,
+ }
+
+ impl gpui::Render for HeldClickMenu {
+ fn render(&mut self, _: &mut Window, cx: &mut gpui::Context<Self>) -> impl IntoElement {
+ let items = (0..200)
+ .map(|index| MenuItem::new(format!("Choice {index}"), false))
+ .collect::<Vec<_>>();
+ let state = MenuState::new(point(px(100.), px(100.)), &items);
+ div().size_full().relative().child(
+ Menu::editor(&Theme::light(), "held-click-menu", items, &state).on_select(
+ cx.listener(|this, index, _, cx| {
+ this.selected = Some(*index);
+ cx.notify();
+ }),
+ ),
+ )
+ }
+ }
+
+ fn draw_held_menu(cx: &mut TestAppContext, window: WindowHandle<HeldClickMenu>) {
+ VisualTestContext::from_window(window.into(), cx).update(|window, cx| {
+ window.refresh();
+ window.draw(cx).clear();
+ });
+ }
+
+ fn scroll_held_menu(
+ cx: &mut TestAppContext,
+ window: WindowHandle<HeldClickMenu>,
+ position: Point<Pixels>,
+ delta: f32,
+ ) {
+ VisualTestContext::from_window(window.into(), cx).simulate_event(ScrollWheelEvent {
+ position,
+ delta: ScrollDelta::Pixels(point(px(0.), px(delta))),
+ modifiers: Modifiers::none(),
+ touch_phase: TouchPhase::Moved,
+ });
+ for _ in 0..3 {
+ draw_held_menu(cx, window);
+ }
+ }
+
+ #[gpui::test]
+ fn a_menu_click_survives_scrolling_away_and_back_while_held(cx: &mut TestAppContext) {
+ let window = cx.open_window(size(px(1000.), px(700.)), |_, _| HeldClickMenu::default());
+ let fourth_row_center = point(px(140.), px(189.));
+ draw_held_menu(cx, window);
+ {
+ let mut visual = VisualTestContext::from_window(window.into(), cx);
+ visual.simulate_mouse_move(fourth_row_center, None, Modifiers::none());
+ visual.simulate_mouse_down(fourth_row_center, MouseButton::Left, Modifiers::none());
+ }
+ draw_held_menu(cx, window);
+ scroll_held_menu(cx, window, fourth_row_center, -100_000.);
+ scroll_held_menu(cx, window, fourth_row_center, 100_000.);
+ assert_eq!(window.read_with(cx, |menu, _| menu.selected).unwrap(), None);
+ VisualTestContext::from_window(window.into(), cx).simulate_mouse_up(
+ fourth_row_center,
+ MouseButton::Left,
+ Modifiers::none(),
+ );
+ assert_eq!(
+ window.read_with(cx, |menu, _| menu.selected).unwrap(),
+ Some(3)
+ );
+ }
+
+ #[gpui::test]
+ fn releasing_a_held_menu_click_outside_cancels_it_without_breaking_the_next_click(
+ cx: &mut TestAppContext,
+ ) {
+ let window = cx.open_window(size(px(1000.), px(700.)), |_, _| HeldClickMenu::default());
+ let fourth_row_center = point(px(140.), px(189.));
+ draw_held_menu(cx, window);
+ {
+ let mut visual = VisualTestContext::from_window(window.into(), cx);
+ visual.simulate_mouse_move(fourth_row_center, None, Modifiers::none());
+ visual.simulate_mouse_down(fourth_row_center, MouseButton::Left, Modifiers::none());
+ }
+ draw_held_menu(cx, window);
+ scroll_held_menu(cx, window, fourth_row_center, -100_000.);
+ VisualTestContext::from_window(window.into(), cx).simulate_mouse_up(
+ point(px(20.), px(20.)),
+ MouseButton::Left,
+ Modifiers::none(),
+ );
+ draw_held_menu(cx, window);
+ assert_eq!(window.read_with(cx, |menu, _| menu.selected).unwrap(), None);
+ VisualTestContext::from_window(window.into(), cx).simulate_mouse_move(
+ fourth_row_center,
+ None,
+ Modifiers::none(),
+ );
+ scroll_held_menu(cx, window, fourth_row_center, 100_000.);
+ VisualTestContext::from_window(window.into(), cx)
+ .simulate_click(fourth_row_center, Modifiers::none());
+ assert_eq!(
+ window.read_with(cx, |menu, _| menu.selected).unwrap(),
+ Some(3)
+ );
+ }
+}
diff --git a/apps/desktop-gpui/src/ui/text_input.rs b/apps/desktop-gpui/src/ui/text_input.rs
index b63f87883..dfbe702ca 100644
--- a/apps/desktop-gpui/src/ui/text_input.rs
+++ b/apps/desktop-gpui/src/ui/text_input.rs
@@ -66,14 +66,14 @@ use std::{cell::RefCell, ops::Range, rc::Rc};
use gpui::{
App, Bounds, ClipboardItem, Context, Element, ElementId, ElementInputHandler, Entity,
- EntityInputHandler, EventEmitter, FocusHandle, Focusable, FontWeight, GlobalElementId, Hsla,
- InteractiveElement, IntoElement, KeyBinding, LayoutId, MouseButton, MouseDownEvent,
+ EntityInputHandler, EventEmitter, FocusHandle, Focusable, Font, FontWeight, GlobalElementId,
+ Hsla, InteractiveElement, IntoElement, KeyBinding, LayoutId, MouseButton, MouseDownEvent,
MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point, RenderOnce, SharedString, Size,
Style, Styled, TextAlign, TextRun, UTF16Selection, UnderlineStyle, WeakEntity, Window,
WrappedLine, actions, div, fill, point, prelude::*, px, relative, size, svg,
};
use smallvec::SmallVec;
-use unicode_segmentation::UnicodeSegmentation;
+use unicode_segmentation::GraphemeCursor;
use super::keyboard_nav;
use crate::theme::Theme;
@@ -310,11 +310,16 @@ struct Row {
/// The shaped picture of the content, rebuilt only when its key changes.
struct InputLayout {
lines: SmallVec<[WrappedLine; 1]>,
+ max_glyph_height: Pixels,
/// Byte offset of each paragraph's first character in the whole content.
paragraph_starts: Vec<usize>,
/// Index of each paragraph's first visual row.
paragraph_rows: Vec<usize>,
rows: Vec<Row>,
+ wrap_width: Option<Pixels>,
+ size: Size<Pixels>,
+ intrinsic_size: Size<Pixels>,
+ measurements: SmallVec<[(Pixels, Size<Pixels>); 4]>,
key: LayoutKey,
}
@@ -322,10 +327,10 @@ struct InputLayout {
#[derive(Debug, Clone, PartialEq)]
struct LayoutKey {
text: SharedString,
- wrap_width: Option<Pixels>,
font_size: Pixels,
line_height: Pixels,
- weight: FontWeight,
+ font: Font,
+ color: Hsla,
marked: Option<Range<usize>>,
}
@@ -1459,15 +1464,19 @@ impl EntityInputHandler for TextInputState {
// -- Pure text helpers, unit-tested -------------------------------------------
fn previous_grapheme(text: &str, offset: usize) -> usize {
- text.grapheme_indices(true)
- .rev()
- .find_map(|(index, _)| (index < offset).then_some(index))
+ // Rounding up keeps the containing grapheme eligible for interior byte offsets.
+ let offset = text.ceil_char_boundary(offset);
+ GraphemeCursor::new(offset, text.len(), true)
+ .prev_boundary(text, 0)
+ .expect("the complete string supplies all grapheme context")
.unwrap_or(0)
}
fn next_grapheme(text: &str, offset: usize) -> usize {
- text.grapheme_indices(true)
- .find_map(|(index, _)| (index > offset).then_some(index))
+ let offset = text.floor_char_boundary(offset);
+ GraphemeCursor::new(offset, text.len(), true)
+ .next_boundary(text, 0)
+ .expect("the complete string supplies all grapheme context")
.unwrap_or(text.len())
}
@@ -2033,48 +2042,36 @@ impl IntoElement for TextBody {
}
impl TextBody {
- /// Shape the content if the cached layout is stale, and hand back the size
- /// it occupies. The only place `shape_text` is called.
- fn ensure_layout(
- &self,
- wrap_width: Option<Pixels>,
- window: &mut Window,
- cx: &mut App,
- ) -> Size<Pixels> {
+ fn layout_key(&self, window: &Window, cx: &App) -> LayoutKey {
let mut font = window.text_style().font();
font.weight = self.weight;
-
- let (text, marked) = {
- let read = self.state.read(cx);
- (read.content.clone(), read.marked_range.clone())
- };
- let key = LayoutKey {
- text: text.clone(),
- wrap_width,
+ let state = self.state.read(cx);
+ LayoutKey {
+ text: state.content.clone(),
font_size: self.text_size,
line_height: self.line_height,
- weight: self.weight,
- marked: marked.clone(),
- };
-
- if let Some(layout) = self.layout.borrow().as_ref()
- && layout.key == key
- {
- return layout_size(layout, self.line_height);
+ font,
+ color: self.color,
+ marked: state.marked_range.clone(),
}
+ }
- // The composing run is underlined, which is the whole visual contract
- // of marked text.
+ fn shape_lines(
+ &self,
+ key: &LayoutKey,
+ wrap_width: Option<Pixels>,
+ window: &mut Window,
+ ) -> SmallVec<[WrappedLine; 1]> {
let base = TextRun {
- len: text.len(),
- font: font.clone(),
- color: self.color,
+ len: key.text.len(),
+ font: key.font.clone(),
+ color: key.color,
background_color: None,
underline: None,
strikethrough: None,
};
- let runs: Vec<TextRun> = match marked.as_ref() {
- Some(marked) if marked.end <= text.len() && marked.start <= marked.end => vec![
+ let runs: Vec<TextRun> = match key.marked.as_ref() {
+ Some(marked) if marked.end <= key.text.len() && marked.start <= marked.end => vec![
TextRun {
len: marked.start,
..base.clone()
@@ -2082,14 +2079,14 @@ impl TextBody {
TextRun {
len: marked.end - marked.start,
underline: Some(UnderlineStyle {
- color: Some(self.color),
+ color: Some(key.color),
thickness: px(1.),
wavy: false,
}),
..base.clone()
},
TextRun {
- len: text.len() - marked.end,
+ len: key.text.len() - marked.end,
..base
},
]
@@ -2098,11 +2095,88 @@ impl TextBody {
.collect(),
_ => vec![base],
};
-
- let lines = window
+ window
.text_system()
- .shape_text(text, self.text_size, &runs, wrap_width, None)
- .unwrap_or_default();
+ .shape_text(key.text.clone(), key.font_size, &runs, wrap_width, None)
+ .unwrap_or_default()
+ }
+
+ fn measure(
+ &self,
+ key: &LayoutKey,
+ wrap_width: Option<Pixels>,
+ window: &mut Window,
+ ) -> Size<Pixels> {
+ if let Some(layout) = self.layout.borrow().as_ref()
+ && layout.key == *key
+ {
+ match wrap_width {
+ None => return layout.intrinsic_size,
+ Some(width) if layout.wrap_width == wrap_width => {
+ return size(width, layout.size.height);
+ }
+ Some(width) => {
+ if let Some((_, size)) = layout
+ .measurements
+ .iter()
+ .find(|(cached_width, _)| *cached_width == width)
+ {
+ return *size;
+ }
+ }
+ }
+ }
+
+ // Flex measurement probes several widths; only prepaint needs the interaction rows.
+ let lines = self.shape_lines(key, wrap_width, window);
+ let measured = match wrap_width {
+ Some(width) => {
+ let rows = lines
+ .iter()
+ .map(|line| line.wrap_boundaries().len() + 1)
+ .sum::<usize>()
+ .max(1);
+ size(width, self.line_height * rows as f32)
+ }
+ None => intrinsic_text_size(&lines, self.line_height),
+ };
+ if let Some(width) = wrap_width
+ && let Some(layout) = self.layout.borrow_mut().as_mut()
+ && layout.key == *key
+ {
+ if layout.measurements.len() == 4 {
+ layout.measurements.remove(0);
+ }
+ layout.measurements.push((width, measured));
+ }
+ measured
+ }
+
+ fn ensure_layout(
+ &self,
+ key: LayoutKey,
+ wrap_width: Option<Pixels>,
+ window: &mut Window,
+ cx: &mut App,
+ ) -> Size<Pixels> {
+ if let Some(layout) = self.layout.borrow().as_ref()
+ && layout.key == key
+ && layout.wrap_width == wrap_width
+ {
+ return layout.size;
+ }
+
+ let lines = self.shape_lines(&key, wrap_width, window);
+ let max_glyph_height = lines.iter().fold(px(0.), |height, line| {
+ line.runs().iter().fold(height, |height, run| {
+ height.max(
+ cx.text_system()
+ .bounding_box(run.font_id, line.font_size())
+ .size
+ .height,
+ )
+ })
+ });
let mut paragraph_starts = Vec::with_capacity(lines.len());
let mut paragraph_rows = Vec::with_capacity(lines.len());
@@ -2126,15 +2200,24 @@ impl TextBody {
});
}
- let layout = InputLayout {
+ let width = rows
+ .iter()
+ .map(|row| row.width)
+ .fold(px(0.), |a, b| if b > a { b } else { a });
+ let size = size(width, self.line_height * rows.len() as f32);
+ let intrinsic_size = intrinsic_text_size(&lines, self.line_height);
+ *self.layout.borrow_mut() = Some(InputLayout {
lines,
+ max_glyph_height,
paragraph_starts,
paragraph_rows,
rows,
+ wrap_width,
+ size,
+ intrinsic_size,
+ measurements: SmallVec::new(),
key,
- };
- let size = layout_size(&layout, self.line_height);
- *self.layout.borrow_mut() = Some(layout);
+ });
self.state.update(cx, |state, _| {
state.line_height = self.line_height;
state.align = self.align;
@@ -2143,13 +2226,41 @@ impl TextBody {
}
}
-fn layout_size(layout: &InputLayout, line_height: Pixels) -> Size<Pixels> {
- let width = layout
- .rows
+fn intrinsic_text_size(lines: &[WrappedLine], line_height: Pixels) -> Size<Pixels> {
+ let width = lines
.iter()
- .map(|row| row.width)
- .fold(px(0.), |a: Pixels, b| if b > a { b } else { a });
- size(width, line_height * layout.rows.len() as f32)
+ .map(|line| {
+ let layout = &line.unwrapped_layout;
+ layout.width - layout.x_for_index(0)
+ })
+ .fold(px(0.), |a, b| if b > a { b } else { a });
+ size(width, line_height * lines.len().max(1) as f32)
+}
+
+fn paragraph_is_visible(
+ line: &WrappedLine,
+ origin_y: Pixels,
+ line_height: Pixels,
+ max_glyph_height: Pixels,
+ clip: Bounds<Pixels>,
+) -> bool {
+ if line_height <= px(0.) {
+ return true;
+ }
+ if origin_y > clip.bottom() {
+ return false;
+ }
+ if origin_y + line.size(line_height).height + max_glyph_height < clip.top() {
+ // GPUI advances wrapped rows with repeated f32 addition, which can exceed n * height.
+ let last_row_y = line
+ .wrap_boundaries()
+ .iter()
+ .fold(origin_y, |y, _| y + line_height);
+ if last_row_y + max_glyph_height < clip.top() {
+ return false;
+ }
+ }
+ true
}
impl Element for TextBody {
@@ -2190,13 +2301,14 @@ impl Element for TextBody {
style.size.width = relative(1.).into();
}
let body = self.clone();
+ let key = self.layout_key(window, cx);
let fit_content = self.fit_content;
let line_height = self.line_height;
let layout_id = window.request_measured_layout(
style,
move |known_dimensions, available_space, window, cx| {
if fit_content {
- let size = body.ensure_layout(None, window, cx);
+ let size = body.ensure_layout(key.clone(), None, window, cx);
// One pixel of slack so the caret at the very end is inside
// the element rather than on its edge.
return gpui::size(size.width + px(1.), line_height);
@@ -2205,8 +2317,7 @@ impl Element for TextBody {
gpui::AvailableSpace::Definite(width) => Some(width),
_ => None,
});
- let size = body.ensure_layout(wrap_width, window, cx);
- gpui::size(wrap_width.unwrap_or(size.width), size.height)
+ body.measure(&key, wrap_width, window)
},
);
(layout_id, ())
@@ -2222,7 +2333,7 @@ impl Element for TextBody {
cx: &mut App,
) -> Self::PrepaintState {
let wrap_width = (self.multi_line && !self.fit_content).then_some(bounds.size.width);
- self.ensure_layout(wrap_width, window, cx);
+ self.ensure_layout(self.layout_key(window, cx), wrap_width, window, cx);
let placeholder = {
let read = self.state.read(cx);
@@ -2391,17 +2502,28 @@ impl Element for TextBody {
// and not inside the entity.
let layout = self.layout.borrow();
if let Some(layout) = layout.as_ref() {
+ let clip = window.content_mask().bounds;
for line in layout.lines.iter() {
let height = line.size(self.line_height).height;
- line.paint(
- origin,
- self.line_height,
- self.align,
- Some(bounds),
- window,
- cx,
- )
- .ok();
+ if layout.key.marked.is_some()
+ || paragraph_is_visible(
+ line,
+ origin.y,
+ self.line_height,
+ layout.max_glyph_height,
+ clip,
+ )
+ {
+ line.paint(
+ origin,
+ self.line_height,
+ self.align,
+ Some(bounds),
+ window,
+ cx,
+ )
+ .ok();
+ }
origin.y += height;
}
}
@@ -2438,6 +2560,7 @@ impl Element for TextBody {
#[cfg(test)]
mod tests {
use super::*;
+ use unicode_segmentation::UnicodeSegmentation;
// -- Key bindings ---------------------------------------------------------
@@ -2597,6 +2720,58 @@ mod tests {
}
}
+ struct PasteProbe {
+ state: Entity<TextInputState>,
+ }
+
+ impl Render for PasteProbe {
+ fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
+ div().size_full().child(TextInput::settings(
+ &Theme::light(),
+ "paste-probe",
+ &self.state,
+ ))
+ }
+ }
+
+ /// A 0.6.1 report from Linux: pasting into Cap's text fields did nothing.
+ /// The fields bound only `cmd-v`, which the fork reads as Super off macOS.
+ /// This presses the platform's paste chord through the real key dispatch,
+ /// and copies back out with the copy chord.
+ #[gpui::test]
+ fn the_platform_paste_and_copy_chords_reach_a_focused_field(cx: &mut gpui::TestAppContext) {
+ cx.update(bind_keys);
+ let (probe, cx) = cx.add_window_view(|window, cx| PasteProbe {
+ state: cx.new(|cx| TextInputState::single_line(window, cx)),
+ });
+ let state = probe.read_with(cx, |probe, _| probe.state.clone());
+ cx.update(|window, cx| {
+ let focus = state.read(cx).focus_handle();
+ window.focus(&focus, cx);
+ cx.write_to_clipboard(ClipboardItem::new_string(
+ "pasted from the clipboard".into(),
+ ));
+ });
+ cx.run_until_parked();
+ let (paste, select_all, copy) = if cfg!(target_os = "macos") {
+ ("cmd-v", "cmd-a", "cmd-c")
+ } else {
+ ("ctrl-v", "ctrl-a", "ctrl-c")
+ };
+ cx.simulate_keystrokes(paste);
+ assert_eq!(
+ state.read_with(cx, |state, _| state.text().to_string()),
+ "pasted from the clipboard"
+ );
+
+ cx.update(|_, cx| cx.write_to_clipboard(ClipboardItem::new_string(String::new())));
+ cx.simulate_keystrokes(&format!("{select_all} {copy}"));
+ assert_eq!(
+ cx.update(|_, cx| cx.read_from_clipboard().and_then(|item| item.text())),
+ Some("pasted from the clipboard".to_string())
+ );
+ }
+
// -- Word navigation ------------------------------------------------------
/// `alt-left` skips the run of separators first, then the word, which is
@@ -2725,6 +2900,50 @@ mod tests {
assert_eq!(previous_grapheme(text, text.len()), 0);
}
+ #[test]
+ fn grapheme_navigation_matches_boundaries_at_every_byte_offset() {
+ let texts = [
+ "",
+ "a",
+ "plain text\r\nnext line",
+ "é中😀",
+ "e\u{301}\u{308}x",
+ "\u{301}\u{308}a",
+ "a🇬🇧🇯🇵🇨b",
+ "a👩🏽‍💻👨‍👩‍👧‍👦b",
+ "aक्ष\u{200d}कb",
+ "a\u{600}\u{600}بb",
+ "a\u{1100}\u{1161}\u{11a8}b",
+ "a\u{915}\u{93e}b",
+ "a\u{200d}b\u{200c}c",
+ ]
+ .into_iter()
+ .map(str::to_owned)
+ .chain([
+ format!("a{}b", "🇬".repeat(65)),
+ format!("a{}b", "\u{301}".repeat(64)),
+ format!("a{}b", "\u{600}".repeat(64)),
+ format!("a{}👩b", "👩\u{200d}".repeat(32)),
+ ]);
+ for text in texts {
+ let boundaries: Vec<_> = text.grapheme_indices(true).map(|(i, _)| i).collect();
+ for offset in (0..=text.len() + 3).chain([usize::MAX]) {
+ let before = boundaries.partition_point(|&i| i < offset);
+ let after = boundaries.partition_point(|&i| i <= offset);
+ assert_eq!(
+ previous_grapheme(&text, offset),
+ before.checked_sub(1).map_or(0, |i| boundaries[i]),
+ "previous at {offset} in {text:?}"
+ );
+ assert_eq!(
+ next_grapheme(&text, offset),
+ boundaries.get(after).copied().unwrap_or(text.len()),
+ "next at {offset} in {text:?}"
+ );
+ }
+ }
+ }
+
// -- utf-16, the input protocol's unit ------------------------------------
#[test]
@@ -2823,9 +3042,250 @@ mod tests {
assert_eq!(starts, vec![0]);
}
+ #[gpui::test]
+ fn paragraph_culling_keeps_fractional_wrapped_rows(cx: &mut gpui::TestAppContext) {
+ cx.add_empty_window().update(|window, cx| {
+ let body = measurement_body(&"a".repeat(15_000), window, cx);
+ body.ensure_layout(body.layout_key(window, cx), Some(px(1.)), window, cx);
+ let layout = body.layout.borrow();
+ let layout = layout.as_ref().unwrap();
+ let line = &layout.lines[0];
+ assert_eq!(line.wrap_boundaries().len(), 14_999);
+ let height = px(26.4);
+ let glyph_height = layout.max_glyph_height;
+ let origin = -line.size(height).height - glyph_height - px(1.);
+ let clip = Bounds::new(point(px(0.), px(0.)), size(px(240.), px(240.)));
+ // Multiplication says this is clipped; GPUI's repeated row additions leave visible glyphs.
+ assert!(origin + line.size(height).height + glyph_height < clip.top());
+ assert!(paragraph_is_visible(
+ line,
+ origin,
+ height,
+ glyph_height,
+ clip
+ ));
+ assert!(!paragraph_is_visible(
+ line,
+ clip.bottom() + px(1.),
+ height,
+ glyph_height,
+ clip
+ ));
+ assert!(!paragraph_is_visible(
+ line,
+ origin - px(10_000.),
+ height,
+ glyph_height,
+ clip
+ ));
+ for height in [px(0.), px(-1.)] {
+ assert!(paragraph_is_visible(
+ line,
+ clip.bottom() + px(1.),
+ height,
+ glyph_height,
+ clip
+ ));
+ }
+ });
+ }
+
+ fn measurement_body(text: &str, window: &mut Window, cx: &mut App) -> TextBody {
+ let state = cx.new(|cx| TextInputState::multi_line(window, cx));
+ state.update(cx, |state, cx| state.set_text(text.to_owned(), cx));
+ TextBody {
+ layout: state.read(cx).shared_layout(),
+ state,
+ fit_content: false,
+ text_size: px(30.),
+ line_height: px(45.),
+ weight: FontWeight::MEDIUM,
+ align: TextAlign::Left,
+ color: gpui::black(),
+ placeholder_color: gpui::black(),
+ caret_color: gpui::black(),
+ selection_color: gpui::black(),
+ multi_line: true,
+ focused: false,
+ }
+ }
+
+ fn fresh_measurement(
+ body: &TextBody,
+ wrap_width: Option<Pixels>,
+ window: &mut Window,
+ cx: &mut App,
+ ) -> Size<Pixels> {
+ let mut fresh = body.clone();
+ fresh.layout = Rc::new(RefCell::new(None));
+ let measured = fresh.ensure_layout(fresh.layout_key(window, cx), wrap_width, window, cx);
+ size(wrap_width.unwrap_or(measured.width), measured.height)
+ }
+
+ fn check_measurement(
+ body: &TextBody,
+ width: Option<Pixels>,
+ window: &mut Window,
+ cx: &mut App,
+ ) {
+ let expected = fresh_measurement(body, width, window, cx);
+ for _ in 0..2 {
+ assert_eq!(
+ body.measure(&body.layout_key(window, cx), width, window),
+ expected
+ );
+ }
+ }
+
+ #[gpui::test]
+ fn measurement_preserves_rows_across_width_probes(cx: &mut gpui::TestAppContext) {
+ cx.add_empty_window().update(|window, cx| {
+ for text in [
+ "",
+ "\n\n",
+ "A paragraph with wrapping.\nSecond.\n",
+ "مرحبا café e\u{301} हिन्दी 👩🏽‍💻 🇬🇧\n中文。",
+ ] {
+ let mut body = measurement_body(text, window, cx);
+ for height in [16.25, 45.] {
+ body.line_height = px(height);
+ for width in [None, Some(px(0.)), Some(px(1.)), Some(px(70.5))] {
+ check_measurement(&body, width, window, cx);
+ assert!(body.layout.borrow().is_none());
+ }
+ }
+ body.ensure_layout(body.layout_key(window, cx), Some(px(496.)), window, cx);
+ body.layout.borrow_mut().as_mut().unwrap().max_glyph_height = px(-123.);
+ for width in [None, Some(px(1368.)), Some(px(496.))]
+ .into_iter()
+ .chain((1..=20).map(|i| Some(px(i as f32 * 40.))))
+ .chain([Some(px(40.))])
+ {
+ check_measurement(&body, width, window, cx);
+ body.ensure_layout(body.layout_key(window, cx), Some(px(496.)), window, cx);
+ let cache = body.layout.borrow();
+ let cache = cache.as_ref().unwrap();
+ assert_eq!(cache.wrap_width, Some(px(496.)));
+ assert_eq!(cache.max_glyph_height, px(-123.));
+ if let Some(width) = width.filter(|w| *w != px(496.)) {
+ assert!(cache.measurements.iter().any(|(w, _)| *w == width));
+ assert!(cache.measurements.len() <= 4);
+ }
+ }
+ }
+ });
+ }
+
+ fn check_invalidated(body: &TextBody, window: &mut Window, cx: &mut App) {
+ body.layout.borrow_mut().as_mut().unwrap().max_glyph_height = px(-123.);
+ check_measurement(body, Some(px(1368.)), window, cx);
+ let key = body.layout_key(window, cx);
+ body.ensure_layout(key.clone(), Some(px(250.)), window, cx);
+ let cache = body.layout.borrow();
+ let cache = cache.as_ref().unwrap();
+ assert_eq!(cache.key, key);
+ assert_eq!(cache.wrap_width, Some(px(250.)));
+ assert_ne!(cache.max_glyph_height, px(-123.));
+ assert!(cache.measurements.is_empty());
+ }
+
+ #[gpui::test]
+ fn measurement_invalidates_on_resize_text_and_style(cx: &mut gpui::TestAppContext) {
+ cx.add_empty_window().draw(
+ point(px(0.), px(0.)),
+ size(px(800.), px(600.)),
+ |window, cx| {
+ let mut body = measurement_body("A paragraph with wrapping.", window, cx);
+ body.ensure_layout(body.layout_key(window, cx), Some(px(496.)), window, cx);
+ check_invalidated(&body, window, cx);
+ body.state
+ .update(cx, |s, _| s.content = "Changed text.\nNext.".into());
+ check_invalidated(&body, window, cx);
+ body.text_size = px(24.);
+ check_invalidated(&body, window, cx);
+ body.line_height = px(36.5);
+ check_invalidated(&body, window, cx);
+ body.weight = FontWeight::BOLD;
+ check_invalidated(&body, window, cx);
+ body.color = gpui::white();
+ check_invalidated(&body, window, cx);
+ body.state.update(cx, |s, _| s.marked_range = Some(0..7));
+ check_invalidated(&body, window, cx);
+ body.state.update(cx, |s, _| s.marked_range = None);
+ check_invalidated(&body, window, cx);
+ for style in [
+ gpui::TextStyleRefinement {
+ font_family: Some("test-font".into()),
+ ..Default::default()
+ },
+ gpui::TextStyleRefinement {
+ font_style: Some(gpui::FontStyle::Italic),
+ ..Default::default()
+ },
+ ] {
+ window.with_text_style(Some(style), |window| {
+ check_invalidated(&body, window, cx)
+ });
+ }
+ div()
+ },
+ );
+ }
+
+ #[gpui::test]
+ fn deferred_measurement_retains_the_inherited_font(cx: &mut gpui::TestAppContext) {
+ let cx = cx.add_empty_window();
+ for fit_content in [false, true] {
+ let text = "A paragraph with several words to wrap. ".repeat(8);
+ let mut body = cx.update(|window, cx| measurement_body(&text, window, cx));
+ body.fit_content = fit_content;
+ let draw = |cx: &mut gpui::VisualTestContext| {
+ cx.draw(
+ point(px(0.), px(0.)),
+ size(
+ gpui::AvailableSpace::Definite(px(496.)),
+ gpui::AvailableSpace::MaxContent,
+ ),
+ |_, _| {
+ div()
+ .w(px(496.))
+ .flex()
+ .flex_col()
+ .items_start()
+ .font_family("measurement-parent-font")
+ .child(body.clone())
+ },
+ );
+ };
+ draw(cx);
+ {
+ let mut cached = body.layout.borrow_mut();
+ let cached = cached.as_mut().unwrap();
+ assert_eq!(cached.key.font.family.as_ref(), "measurement-parent-font");
+ assert_eq!(cached.wrap_width, (!fit_content).then_some(px(496.)));
+ cached.max_glyph_height = px(-123.);
+ if !fit_content {
+ cached.size.height = px(123.);
+ }
+ }
+ draw(cx);
+ assert_eq!(
+ body.layout.borrow().as_ref().unwrap().max_glyph_height,
+ px(-123.)
+ );
+ if !fit_content {
+ assert_eq!(
+ cx.update(|_, cx| body.state.read(cx).text_bounds.unwrap().size.height),
+ px(123.),
+ );
+ }
+ }
+ }
+
fn layout_of(rows: &[Range<usize>], widths: &[f32]) -> InputLayout {
InputLayout {
lines: SmallVec::new(),
+ max_glyph_height: px(0.),
paragraph_starts: vec![0],
paragraph_rows: vec![0],
rows: rows
@@ -2838,12 +3298,19 @@ mod tests {
width: px(widths[index]),
})
.collect(),
+ wrap_width: None,
+ size: size(
+ widths.iter().copied().map(px).fold(px(0.), Pixels::max),
+ px(20.) * rows.len() as f32,
+ ),
+ intrinsic_size: size(px(0.), px(20.)),
+ measurements: SmallVec::new(),
key: LayoutKey {
text: SharedString::default(),
- wrap_width: None,
font_size: px(12.),
line_height: px(20.),
- weight: FontWeight::NORMAL,
+ font: Font::default(),
+ color: gpui::black(),
marked: None,
},
}
diff --git a/apps/desktop-gpui/src/upload/instant_cleanup.rs b/apps/desktop-gpui/src/upload/instant_cleanup.rs
index 1e1fcd71e..fc58e1f0c 100644
--- a/apps/desktop-gpui/src/upload/instant_cleanup.rs
+++ b/apps/desktop-gpui/src/upload/instant_cleanup.rs
@@ -353,11 +353,8 @@ fn abandon_at(
/// Records a pre-created Instant video before capture starts, so a crash,
/// panic or aborted discard during startup still gets its server copy
-/// deleted on the next sweep. Linux keeps its own startup cleanup contract.
+/// deleted on the next sweep.
pub(crate) fn begin(video_id: &str, project_dir: &Path) {
- if cfg!(target_os = "linux") {
- return;
- }
let Some(owner_id) = current_owner_id() else {
return;
};
@@ -372,16 +369,10 @@ pub(crate) fn begin(video_id: &str, project_dir: &Path) {
}
pub(crate) fn started(video_id: &str) {
- if cfg!(target_os = "linux") {
- return;
- }
remove_at(&journal_path(), video_id);
}
pub(crate) fn resolved(video_id: &str) {
- if cfg!(target_os = "linux") {
- return;
- }
remove_at(&journal_path(), video_id);
}
@@ -390,16 +381,10 @@ pub(crate) fn resolved(video_id: &str) {
/// removed and delete the uploaded video. False when the entry is still there,
/// so the caller keeps the local copy.
pub(crate) fn forget_verified(video_id: &str) -> bool {
- if cfg!(target_os = "linux") {
- return true;
- }
forget_at(&journal_path(), video_id)
}
pub(crate) fn abandon(video_id: &str, reason: CleanupReason, cause: Option<StartFailureCause>) {
- if cfg!(target_os = "linux") {
- return;
- }
let identity = current_owner_id().map(|owner| (auth::server_url(), owner));
abandon_at(&journal_path(), video_id, reason, cause, identity, now());
}
@@ -433,6 +418,12 @@ fn meta_claims(meta: &RecordingMeta, video_id: &str) -> bool {
|| upload_meta_video_id(meta) == Some(video_id)
}
+/// Whether a recording folder holds media a crash or a cancelled start must
+/// not delete.
+pub(crate) fn project_holds_media(project: &Path) -> bool {
+ has_recorded_media(&project.join("content"), MEDIA_SCAN_DEPTH)
+}
+
/// True when the folder holds recorded media, or when part of it cannot be
/// read: media that cannot be checked counts as present. Only a missing folder
/// counts as empty. A segment still named `.m4s.tmp` counts too, since a crash
@@ -732,6 +723,18 @@ async fn sweep(path: &Path, session: &str, now: i64, backend: &impl SweepBackend
summary
}
+/// Whether the sweep keeps an entry's server video while its folder holds
+/// media. A crashed start's folder may have recorded before the crash; on
+/// Linux a cancelled start that wrote media keeps it too
+/// (`ActiveRecording::discard_cancelled_start`), so its video must stay.
+fn guards_media(reason: CleanupReason) -> bool {
+ match reason {
+ CleanupReason::Interrupted => true,
+ CleanupReason::StartCancelled => cfg!(target_os = "linux"),
+ CleanupReason::StartFailed => false,
+ }
+}
+
struct LiveSweep;
impl SweepBackend for LiveSweep {
@@ -741,13 +744,13 @@ impl SweepBackend for LiveSweep {
async fn protected(&self, entry: &Entry) -> bool {
let video_id = entry.video_id.clone();
- let interrupted = entry.effective_reason() == CleanupReason::Interrupted;
+ let guards_media = guards_media(entry.effective_reason());
let start_project = entry.project_dir.clone();
tokio::task::spawn_blocking(move || {
locally_protected(
&crate::library::known_recordings_dirs(),
&video_id,
- interrupted,
+ guards_media,
start_project.as_deref(),
)
})
@@ -809,9 +812,6 @@ async fn run() {
}
pub(crate) fn init(cx: &mut gpui::App) {
- if cfg!(target_os = "linux") {
- return;
- }
session_id();
launched_at();
gpui_tokio::Tokio::spawn(cx, run()).detach();
@@ -1355,6 +1355,29 @@ mod tests {
assert!(!locally_protected(&[recordings], "video", false, None));
}
+ #[test]
+ fn a_cancelled_start_guards_media_where_it_keeps_it() {
+ assert!(guards_media(CleanupReason::Interrupted));
+ assert!(!guards_media(CleanupReason::StartFailed));
+ assert_eq!(
+ guards_media(CleanupReason::StartCancelled),
+ cfg!(target_os = "linux")
+ );
+ let fixture = Fixture::new();
+ let recordings = fixture.recordings();
+ let project = write_project(&recordings, "cancelled.cap", "video", in_progress());
+ std::fs::write(project.join("content/display/segment_001.m4s"), b"media").unwrap();
+ assert_eq!(
+ locally_protected(
+ &[recordings],
+ "video",
+ guards_media(CleanupReason::StartCancelled),
+ Some(&project),
+ ),
+ cfg!(target_os = "linux")
+ );
+ }
+
#[cfg(unix)]
#[test]
fn media_that_cannot_be_read_protects_an_interrupted_start() {
diff --git a/apps/desktop-gpui/src/upload/real_app_tests.rs b/apps/desktop-gpui/src/upload/real_app_tests.rs
index 8db668677..51851157a 100644
--- a/apps/desktop-gpui/src/upload/real_app_tests.rs
+++ b/apps/desktop-gpui/src/upload/real_app_tests.rs
@@ -8,7 +8,7 @@
//! cargo test --bin cap-gpui real_app -- --ignored --test-threads=1
//! ```
-use super::verify_tests::{API_KEY, MockCap, OWNER};
+use super::verify_tests::{API_KEY, Fault, MockCap, OWNER, Route, rule};
use serde_json::{Value, json};
use std::{
path::{Path, PathBuf},
@@ -252,6 +252,23 @@ fn kill(app: App) {
drop(app);
}
+fn without_ansi(line: &str) -> String {
+ let mut plain = String::with_capacity(line.len());
+ let mut chars = line.chars();
+ while let Some(c) = chars.next() {
+ if c == '\u{1b}' {
+ for c in chars.by_ref() {
+ if c.is_ascii_alphabetic() {
+ break;
+ }
+ }
+ } else {
+ plain.push(c);
+ }
+ }
+ plain
+}
+
/// A normal Instant recording uploads, verifies, and leaves nothing for the
/// cleanup journal.
#[ignore]
@@ -460,3 +477,68 @@ async fn real_app_cancelled_start_deletes_its_video() {
}
panic!("no stop landed while the start was in flight");
}
+
+/// The start cue tells the user they are recording. An Instant start first
+/// waits for the server to create its video, which takes seconds when earlier
+/// takes are still uploading. The cue must wait for capture too: it used to
+/// play and arm the start gate long before the pipeline existed, so the
+/// opening words of the take were never recorded.
+#[ignore]
+#[tokio::test(flavor = "multi_thread")]
+async fn real_app_start_cue_waits_for_a_slow_server_video() {
+ let mock = MockCap::start(vec![rule(
+ Route::Api("video/create"),
+ Fault::Delay(Duration::from_secs(4)),
+ )])
+ .await;
+ let sandbox = Sandbox::new(&mock.url, 0);
+ let app = sandbox.launch("slow-create", &[("CAP_GPUI_AUTO_RECORD", "instant:6")]);
+ wait_for_app(
+ &sandbox,
+ &app,
+ "a verified upload",
+ Duration::from_secs(180),
+ || {
+ sandbox
+ .projects()
+ .first()
+ .and_then(|project| state(project))
+ .is_some_and(|state| state["phase"] == "Verified")
+ },
+ )
+ .await;
+ kill(app);
+ let log = sandbox.log("slow-create");
+ let line = |needle: &str| {
+ log.lines()
+ .position(|line| line.contains(needle))
+ .unwrap_or_else(|| panic!("no {needle:?} in the app log:\n{log}"))
+ };
+ let building = line("starting recording");
+ assert!(
+ line("Recording start cue playing") > building,
+ "the start cue played before capture was being built"
+ );
+ // Capture that is already running when the cue ends holds its first frame
+ // behind the start gate. A cue that played first opened the gate before
+ // any capture existed, so nothing was ever held.
+ let first_frame = log
+ .lines()
+ .map(without_ansi)
+ .find(|line| {
+ line.contains("Start gate")
+ && (line.contains("video frame") || line.contains("frame on screen"))
+ })
+ .unwrap_or_else(|| panic!("no first video frame in the app log:\n{log}"));
+ let held = first_frame
+ .split("held_frames=")
+ .nth(1)
+ .and_then(|rest| rest.split(|c: char| !c.is_ascii_digit()).next())
+ .and_then(|count| count.parse::<u64>().ok())
+ .unwrap_or_else(|| panic!("no held_frames in {first_frame:?}"));
+ assert!(
+ held >= 1,
+ "capture was not running when the start cue ended: {first_frame}"
+ );
+ assert_eq!(mock.created().len(), 1);
+}
diff --git a/apps/desktop/src-tauri/src/recording_start_sound.rs b/apps/desktop/src-tauri/src/recording_start_sound.rs
index 241600816..c28daaeb4 100644
--- a/apps/desktop/src-tauri/src/recording_start_sound.rs
+++ b/apps/desktop/src-tauri/src/recording_start_sound.rs
@@ -12,6 +12,14 @@ const CANCEL_POLL_INTERVAL: Duration = Duration::from_millis(5);
// done. Shared-mode WASAPI and PulseAudio devices commonly sit at 30-100ms, so
// err towards the cue preceding capture rather than landing inside it.
const FALLBACK_OUTPUT_LATENCY: Duration = Duration::from_millis(150);
+// An output that has not taken a single sample by now is stalled (a suspended
+// or unplugged sink): its cue is stopped so it can never play late into the
+// recording, and the recording starts instead of waiting out the timeout.
+const FALLBACK_STALL_TIMEOUT: Duration = Duration::from_millis(300);
+// Opening an output can block for seconds when the sound server is slow or
+// missing. A cue whose output is not open by the time recording starts is
+// skipped rather than waited for; the worker then never plays it.
+const OUTPUT_OPEN_TIMEOUT: Duration = Duration::from_millis(300);
struct PlayCommand {
gate: RecordingStartGate,
@@ -35,34 +43,55 @@ impl PlayCommand {
/// Dropping it unused releases the device.
pub struct StartCue {
command: mpsc::Sender<PlayCommand>,
+ ready: tokio::sync::watch::Receiver<bool>,
}
impl StartCue {
#[cfg(test)]
fn stub() -> (Self, mpsc::Receiver<PlayCommand>) {
let (command, rx) = mpsc::channel();
- (Self { command }, rx)
+ let (_, ready) = tokio::sync::watch::channel(true);
+ (Self { command, ready }, rx)
+ }
+
+ #[cfg(test)]
+ fn still_opening() -> (
+ Self,
+ mpsc::Receiver<PlayCommand>,
+ tokio::sync::watch::Sender<bool>,
+ ) {
+ let (command, rx) = mpsc::channel();
+ let (opened, ready) = tokio::sync::watch::channel(false);
+ (Self { command, ready }, rx, opened)
}
}
pub fn prime(bytes: &'static [u8]) -> StartCue {
let (command, rx) = mpsc::channel();
+ let (opened, ready) = tokio::sync::watch::channel(false);
if let Err(error) = std::thread::Builder::new()
.name("recording-start-sound".into())
- .spawn(move || worker(bytes, rx))
+ .spawn(move || worker(bytes, rx, opened))
{
tracing::warn!(%error, "Could not start recording sound worker");
}
- StartCue { command }
+ StartCue { command, ready }
}
-fn worker(bytes: &'static [u8], rx: mpsc::Receiver<PlayCommand>) {
+fn worker(
+ bytes: &'static [u8],
+ rx: mpsc::Receiver<PlayCommand>,
+ opened: tokio::sync::watch::Sender<bool>,
+) {
#[cfg(target_os = "macos")]
match native::Output::prepare(bytes) {
- Ok(output) => return output.run(rx),
+ Ok(output) => {
+ opened.send_replace(true);
+ return output.run(rx);
+ }
Err(error) => tracing::warn!(%error, "Native start sound unavailable; using fallback"),
}
- fallback::run(bytes, rx);
+ fallback::run(bytes, rx, opened);
}
/// Plays the start cue and arms `gate` the instant its last sample has left the
@@ -74,6 +103,15 @@ pub async fn play(cue: StartCue, gate: RecordingStartGate) {
async fn play_with_timeout(cue: StartCue, gate: RecordingStartGate, timeout: Duration) {
let _arm_on_drop = ArmOnDrop(gate.clone());
+ let mut ready = cue.ready.clone();
+ if tokio::time::timeout(OUTPUT_OPEN_TIMEOUT, ready.wait_for(|ready| *ready))
+ .await
+ .is_err()
+ {
+ // Dropping `cue` closes the worker's queue, so a late open never plays.
+ tracing::warn!("Recording start sound output is still opening; starting without the cue");
+ return;
+ }
let (done, done_rx) = tokio::sync::oneshot::channel();
if cue.command.send(PlayCommand { gate, done }).is_err() {
tracing::warn!("Recording start sound worker is not running");
@@ -95,10 +133,18 @@ impl Drop for ArmOnDrop {
}
}
+fn stalled(position: Duration, elapsed: Duration) -> bool {
+ position.is_zero() && elapsed >= FALLBACK_STALL_TIMEOUT
+}
+
mod fallback {
use super::*;
- pub fn run(bytes: &'static [u8], rx: mpsc::Receiver<PlayCommand>) {
+ pub fn run(
+ bytes: &'static [u8],
+ rx: mpsc::Receiver<PlayCommand>,
+ opened: tokio::sync::watch::Sender<bool>,
+ ) {
let output = OutputStream::try_default()
.map_err(|error| error.to_string())
.and_then(|(stream, handle)| {
@@ -106,9 +152,13 @@ mod fallback {
let source = Decoder::new(Cursor::new(bytes)).map_err(|error| error.to_string())?;
Ok((stream, sink, source))
});
+ opened.send_replace(true);
let Ok(command) = rx.recv() else {
return;
};
+ if command.cancelled() {
+ return;
+ }
let (_stream, sink, source) = match output {
Ok(output) => output,
Err(error) => return command.finish(Err(error)),
@@ -121,6 +171,10 @@ mod fallback {
sink.stop();
return;
}
+ if stalled(sink.get_pos(), started.elapsed()) {
+ sink.stop();
+ return command.finish(Err("the output device is not playing".to_string()));
+ }
std::thread::sleep(CANCEL_POLL_INTERVAL);
}
std::thread::sleep(FALLBACK_OUTPUT_LATENCY);
@@ -328,6 +382,45 @@ mod tests {
assert!(command.cancelled());
}
+ #[tokio::test]
+ async fn an_output_still_opening_starts_the_recording_without_the_cue() {
+ let (cue, rx, _opened) = StartCue::still_opening();
+ let gate = RecordingStartGate::new();
+ let started = std::time::Instant::now();
+ play(cue, gate.clone()).await;
+ assert!(gate.is_armed());
+ assert!(started.elapsed() < PLAYBACK_TIMEOUT);
+ // Nothing was queued, and the worker's queue is closed: it never plays.
+ assert!(rx.recv().is_err());
+ }
+
+ #[tokio::test]
+ async fn an_output_that_opens_in_time_still_plays_the_cue() {
+ let (cue, rx, opened) = StartCue::still_opening();
+ let gate = RecordingStartGate::new();
+ let playback = tokio::spawn(play(cue, gate.clone()));
+ tokio::time::sleep(Duration::from_millis(50)).await;
+ opened.send_replace(true);
+ let command = tokio::task::spawn_blocking(move || rx.recv().unwrap())
+ .await
+ .unwrap();
+ command.finish(Ok(()));
+ playback.await.unwrap();
+ assert!(gate.is_armed());
+ }
+
+ #[test]
+ fn a_stalled_output_is_given_up_on_long_before_the_timeout() {
+ assert!(!stalled(Duration::ZERO, Duration::from_millis(100)));
+ assert!(stalled(Duration::ZERO, FALLBACK_STALL_TIMEOUT));
+ // Any progress at all means the device is playing; the timeout covers it.
+ assert!(!stalled(
+ Duration::from_millis(5),
+ Duration::from_millis(900)
+ ));
+ assert!(FALLBACK_STALL_TIMEOUT < PLAYBACK_TIMEOUT);
+ }
+
#[tokio::test]
async fn missing_worker_arms_the_gate() {
let (cue, rx) = StartCue::stub();
diff --git a/apps/media-server/src/__tests__/lib/media-video.integration.test.ts b/apps/media-server/src/__tests__/lib/media-video.integration.test.ts
index f7234458a..e3b8d3e7f 100644
--- a/apps/media-server/src/__tests__/lib/media-video.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/media-video.integration.test.ts
@@ -811,6 +811,57 @@ describe("processVideo integration tests", () => {
}
});
+ test("skips the zero-length closing entry Loom adds to DASH timelines", async () => {
+ const originalFetch = globalThis.fetch;
+ const manifestDir = mkdtempSync(join(tmpdir(), "cap-mpd-zero-"));
+
+ globalThis.fetch = (async () =>
+ new Response(
+ `<MPD mediaPresentationDuration="PT7.001S">
+ <Period id="0">
+ <AdaptationSet id="1" contentType="video">
+ <Representation id="1" bandwidth="500000" codecs="vp9" mimeType="video/webm" width="1280" height="720">
+ <SegmentTemplate timescale="1000000" initialization="loom-video-init.webm" media="loom-video-$Number$.webm" startNumber="0">
+ <SegmentTimeline>
+ <S t="0" d="3539000"/>
+ <S t="3522000" d="3479000"/>
+ <S t="7001000" d="0"/>
+ </SegmentTimeline>
+ </SegmentTemplate>
+ </Representation>
+ </AdaptationSet>
+ </Period>
+ </MPD>`,
+ { status: 200, statusText: "OK" },
+ )) as unknown as typeof fetch;
+
+ try {
+ const masterPath = await materializeMpdAsHlsPlaylist(
+ "https://luna.example/id/x/resource/dash/playlist.mpd?Signature=s",
+ manifestDir,
+ );
+ const masterLines = readFileSync(masterPath, "utf8").split("\n");
+ const videoPlaylist = readFileSync(
+ masterLines[
+ masterLines.findIndex((line) =>
+ line.startsWith("#EXT-X-STREAM-INF:"),
+ ) + 1
+ ] ?? "",
+ "utf8",
+ );
+
+ expect(videoPlaylist).toContain("loom-video-0.webm?Signature=s");
+ expect(videoPlaylist).toContain("loom-video-1.webm?Signature=s");
+ expect(videoPlaylist).not.toContain("loom-video-2.webm");
+ expect(videoPlaylist.match(/#EXTINF:/g)?.length).toBe(2);
+ expect(videoPlaylist).toContain("#EXTINF:3.539000,");
+ expect(videoPlaylist).toContain("#EXTINF:3.479000,");
+ } finally {
+ globalThis.fetch = originalFetch;
+ rmSync(manifestDir, { recursive: true, force: true });
+ }
+ });
+
test("falls back to generic DASH manifest materialization for unsupported MPD shapes", async () => {
const originalFetch = globalThis.fetch;
const manifestDir = mkdtempSync(join(tmpdir(), "cap-mpd-fallback-"));
diff --git a/apps/media-server/src/__tests__/lib/video-input-validation.integration.test.ts b/apps/media-server/src/__tests__/lib/video-input-validation.integration.test.ts
index be9c875a5..1ed4b1930 100644
--- a/apps/media-server/src/__tests__/lib/video-input-validation.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/video-input-validation.integration.test.ts
@@ -30,6 +30,7 @@ describe("raw recording input validation", () => {
const corrupt = join(directory, "corrupt.webm");
const cleanIvf = join(directory, "clean.ivf");
const cleanMp4 = join(directory, "clean.mp4");
+ const truncated = join(directory, "truncated.webm");
beforeAll(() => {
execFileSync("ffmpeg", [
@@ -90,6 +91,11 @@ describe("raw recording input validation", () => {
output,
]);
}
+ const cleanBytes = readFileSync(clean);
+ writeFileSync(
+ truncated,
+ cleanBytes.subarray(0, Math.floor(cleanBytes.length / 2)),
+ );
execFileSync("ffmpeg", [
"-v",
"error",
@@ -119,10 +125,26 @@ describe("raw recording input validation", () => {
expect(readFileSync(corrupt).length).toBeGreaterThan(0);
});
- for (const [name, source, expectedPhase, extension] of [
- ["clean", clean, "complete", ".webm"],
- ["damaged", corrupt, "error", ".webm"],
- ["fragmented-mp4", cleanMp4, "complete", ".mp4"],
+ for (const [
+ name,
+ source,
+ expectedPhase,
+ extension,
+ importedOriginal,
+ expectedError,
+ ] of [
+ ["clean", clean, "complete", ".webm", false, null],
+ ["damaged", corrupt, "error", ".webm", false, "damaged or unreadable"],
+ ["imported-damaged", corrupt, "complete", ".webm", true, null],
+ [
+ "imported-truncated",
+ truncated,
+ "error",
+ ".webm",
+ true,
+ "could not be fully recovered",
+ ],
+ ["fragmented-mp4", cleanMp4, "complete", ".mp4", false, null],
] as const) {
test(`processing route handles ${name} source before conversion`, async () => {
spyOn(jobManager, "canAcceptNewVideoProcess").mockReturnValue(true);
@@ -155,6 +177,7 @@ describe("raw recording input validation", () => {
outputPresignedUrl: "https://example.com/result.mp4",
inputExtension: extension,
preset: "ultrafast",
+ ...(importedOriginal ? { importedOriginal } : {}),
}),
}),
);
@@ -169,9 +192,9 @@ describe("raw recording input validation", () => {
await Bun.sleep(10);
}
expect(getJob(jobId)?.phase).toBe(expectedPhase);
- if (expectedPhase === "error") {
- expect(getJob(jobId)?.error).toContain("damaged or unreadable");
- expect(process).not.toHaveBeenCalled();
+ if (expectedError) {
+ expect(getJob(jobId)?.error).toContain(expectedError);
+ expect(process).toHaveBeenCalledTimes(importedOriginal ? 1 : 0);
expect(upload).not.toHaveBeenCalled();
} else {
expect(process).toHaveBeenCalledTimes(1);
diff --git a/apps/media-server/src/__tests__/loom-importer/import.integration.test.ts b/apps/media-server/src/__tests__/loom-importer/import.integration.test.ts
index b003dde6c..eb0ed2900 100644
--- a/apps/media-server/src/__tests__/loom-importer/import.integration.test.ts
+++ b/apps/media-server/src/__tests__/loom-importer/import.integration.test.ts
@@ -1,5 +1,13 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
-import { mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
+import {
+ cp,
+ mkdir,
+ mkdtemp,
+ readdir,
+ readFile,
+ rm,
+ writeFile,
+} from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { spawnSync } from "bun";
@@ -170,6 +178,77 @@ describe("runLoomImport", () => {
],
dash,
);
+ const manifest = await readFile(join(dash, "manifest.mpd"), "utf8");
+ await writeFile(
+ join(dash, "manifest-zero.mpd"),
+ manifest.replaceAll(
+ "</SegmentTimeline>",
+ '<S d="0" /></SegmentTimeline>',
+ ),
+ );
+ await cp(hls, join(root, "hls-gap"), { recursive: true });
+ await rm(join(root, "hls-gap", "stream2-002.ts"));
+ await cp(hls, join(root, "hls-holes"), { recursive: true });
+ await rm(join(root, "hls-holes", "stream2-001.ts"));
+ await rm(join(root, "hls-holes", "stream2-003.ts"));
+ const audioOnly = join(root, "hls-audio");
+ await mkdir(audioOnly);
+ ffmpeg(
+ [
+ "-f",
+ "lavfi",
+ "-i",
+ "sine=frequency=440:sample_rate=48000",
+ "-t",
+ "7",
+ "-c:a",
+ "aac",
+ "-f",
+ "hls",
+ "-hls_time",
+ "2",
+ "-hls_playlist_type",
+ "vod",
+ "-hls_segment_filename",
+ "audio-%03d.ts",
+ "audio.m3u8",
+ ],
+ audioOnly,
+ );
+ const audioPlaylist = await readFile(join(audioOnly, "audio.m3u8"), "utf8");
+ await writeFile(
+ join(audioOnly, "video.m3u8"),
+ audioPlaylist.replaceAll("audio-", "video-"),
+ );
+ for (const entry of await readdir(audioOnly))
+ if (entry.startsWith("audio-"))
+ await writeFile(join(audioOnly, entry.replace("audio-", "video-")), "");
+ await writeFile(
+ join(audioOnly, "master.m3u8"),
+ [
+ "#EXTM3U",
+ "#EXT-X-VERSION:3",
+ '#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="audio",DEFAULT="YES",URI="audio.m3u8"',
+ '#EXT-X-STREAM-INF:BANDWIDTH=1500000,RESOLUTION=NaNx720,AUDIO="audio"',
+ "video.m3u8",
+ ].join("\n"),
+ );
+ await cp(audioOnly, join(root, "hls-audio-gap"), { recursive: true });
+ await rm(join(root, "hls-audio-gap", "audio-001.ts"));
+ ffmpeg(
+ [
+ "-f",
+ "lavfi",
+ "-i",
+ "sine=frequency=330:sample_rate=48000",
+ "-t",
+ "5",
+ "-c:a",
+ "libopus",
+ "opus-only.webm",
+ ],
+ root,
+ );
server = Bun.serve({
port: 0,
hostname: "127.0.0.1",
@@ -355,6 +434,126 @@ describe("runLoomImport", () => {
expect(complete?.deferredSourceKey).toBe("user/dash-video/raw-upload.webm");
}, 60_000);
+ test("imports DASH timelines that end with a zero-length entry", async () => {
+ const job = await run(
+ request(
+ "dash-zero",
+ `${base}/media/dash/manifest-zero.mpd?${SIGNATURE}`,
+ {
+ originalUpload: {
+ key: "user/dash-zero/raw-upload.webm",
+ upload: { type: "put", url: `${base}/put/dash-zero/raw.webm` },
+ },
+ },
+ ),
+ );
+ expect(job?.phase).toBe("complete");
+ expect(job?.deferredSourceKey).toBe("user/dash-zero/raw-upload.webm");
+ expect(job?.metadata?.duration).toBeCloseTo(9, 0);
+ }, 60_000);
+
+ test("skips one segment Loom no longer has and keeps later audio in place", async () => {
+ const job = await run(
+ request("hls-gap", `${base}/media/hls-gap/master.m3u8?${SIGNATURE}`),
+ );
+ expect(job?.phase).toBe("complete");
+ const imported = join(root, "gap.mp4");
+ await writeFile(
+ imported,
+ uploads.get("hls-gap/result.mp4") ?? new Uint8Array(),
+ );
+ expect(Number(probe(imported).format.duration)).toBeCloseTo(9, 0);
+ const audioTimes = spawnSync({
+ cmd: [
+ "ffprobe",
+ "-v",
+ "error",
+ "-select_streams",
+ "a:0",
+ "-show_entries",
+ "packet=pts_time",
+ "-of",
+ "csv=p=0",
+ imported,
+ ],
+ })
+ .stdout.toString()
+ .trim()
+ .split("\n")
+ .map(Number);
+ const gaps = audioTimes
+ .slice(1)
+ .map((time, index) => time - (audioTimes[index] ?? time))
+ .filter((delta) => delta > 0.5);
+ expect(gaps).toHaveLength(1);
+ expect(gaps[0]).toBeCloseTo(2, 0);
+ }, 60_000);
+
+ test("fails when Loom is missing more segments than can be skipped", async () => {
+ const job = await run(
+ request("hls-holes", `${base}/media/hls-holes/master.m3u8?${SIGNATURE}`),
+ );
+ expect(job?.phase).toBe("error");
+ expect(job?.error).toContain("Loom is missing 2 of 5 audio segments");
+ }, 60_000);
+
+ test("imports an audio-only HLS recording with a still video track", async () => {
+ const job = await run(
+ request("hls-audio", `${base}/media/hls-audio/master.m3u8?${SIGNATURE}`),
+ );
+ expect(job?.phase).toBe("complete");
+ expect(job?.deferredSourceKey).toBeUndefined();
+ expect(job?.metadata).toMatchObject({
+ width: 1280,
+ height: 720,
+ videoCodec: "h264",
+ audioCodec: "aac",
+ });
+ expect(job?.metadata?.duration).toBeCloseTo(7, 0);
+ expect(uploads.get("hls-audio/thumbnail.jpg")?.byteLength).toBeGreaterThan(
+ 0,
+ );
+ }, 60_000);
+
+ test("keeps the full length of an audio-only recording with a missing segment", async () => {
+ const job = await run(
+ request(
+ "hls-audio-gap",
+ `${base}/media/hls-audio-gap/master.m3u8?${SIGNATURE}`,
+ ),
+ );
+ expect(job?.phase).toBe("complete");
+ const imported = join(root, "audio-gap.mp4");
+ await writeFile(
+ imported,
+ uploads.get("hls-audio-gap/result.mp4") ?? new Uint8Array(),
+ );
+ const output = probe(imported);
+ expect(Number(output.format.duration)).toBeCloseTo(7, 0);
+ expect(
+ Number(
+ output.streams.find((stream) => stream.codec_type === "video")
+ ?.start_time,
+ ),
+ ).toBe(0);
+ }, 60_000);
+
+ test("imports an Opus-only file as an MP4 with AAC audio", async () => {
+ const job = await run(
+ request("opus-only", `${base}/media/opus-only.webm?${SIGNATURE}`),
+ );
+ expect(job?.phase).toBe("complete");
+ expect(job?.deferredSourceKey).toBeUndefined();
+ expect(job?.metadata).toMatchObject({
+ videoCodec: "h264",
+ audioCodec: "aac",
+ });
+ expect(job?.metadata?.duration).toBeCloseTo(5, 0);
+ expect(uploads.get("opus-only/result.mp4")?.byteLength).toBeGreaterThan(
+ 10_000,
+ );
+ }, 60_000);
+
test("fails VP9 sources that have nowhere to store the original", async () => {
const job = await run(
request("dash-no-target", `${base}/media/dash/manifest.mpd?${SIGNATURE}`),
diff --git a/apps/media-server/src/__tests__/loom-importer/parsing.test.ts b/apps/media-server/src/__tests__/loom-importer/parsing.test.ts
index a210bf23d..66627b1f1 100644
--- a/apps/media-server/src/__tests__/loom-importer/parsing.test.ts
+++ b/apps/media-server/src/__tests__/loom-importer/parsing.test.ts
@@ -11,6 +11,7 @@ import {
resolveFps,
} from "../../loom-importer/media";
import {
+ canSkipMissingSegments,
listMediaPlaylistResources,
parseHlsAttributes,
selectHlsTracks,
@@ -240,3 +241,37 @@ describe("Semaphore", () => {
expect(await semaphore.run(async () => "ok")).toBe("ok");
});
});
+
+describe("canSkipMissingSegments", () => {
+ const segments = (count: number, duration: number) =>
+ Array.from({ length: count }, () => duration);
+
+ test("accepts a source with nothing missing", () => {
+ expect(canSkipMissingSegments([], segments(3, 4))).toBe(true);
+ });
+
+ test("accepts one missing segment in a long recording", () => {
+ expect(canSkipMissingSegments([5], segments(1183, 5))).toBe(true);
+ });
+
+ test("accepts a missing short final segment in a short recording", () => {
+ expect(canSkipMissingSegments([3.07], [...segments(33, 6), 3.07])).toBe(
+ true,
+ );
+ });
+
+ test("rejects more missing segments than one percent allows", () => {
+ expect(canSkipMissingSegments([6, 6], segments(34, 6))).toBe(false);
+ expect(canSkipMissingSegments(segments(12, 5), segments(1183, 5))).toBe(
+ false,
+ );
+ });
+
+ test("rejects a missing segment that is too long to skip", () => {
+ expect(canSkipMissingSegments([20], segments(30, 20))).toBe(false);
+ });
+
+ test("rejects losing every segment", () => {
+ expect(canSkipMissingSegments([2], [2])).toBe(false);
+ });
+});
diff --git a/apps/media-server/src/lib/media-video.ts b/apps/media-server/src/lib/media-video.ts
index 61b2bd52e..ce0cbd2ad 100644
--- a/apps/media-server/src/lib/media-video.ts
+++ b/apps/media-server/src/lib/media-video.ts
@@ -621,8 +621,13 @@ function buildDashSegments(
if (timeline) {
const timelineSegments = getXmlElementBlocks(timeline.content, "S");
let currentTime = 0;
+ let number = startNumber;
for (const [index, segment] of timelineSegments.entries()) {
+ if (parseNumber(segment.attributes.d) === 0) {
+ number++;
+ continue;
+ }
const duration = parsePositiveNumber(segment.attributes.d);
if (!duration) {
throw new Error("DASH SegmentTimeline entry is missing duration");
@@ -647,10 +652,9 @@ function buildDashSegments(
for (let offset = 0; offset < count; offset++) {
const time = currentTime + duration * offset;
- const number = startNumber + segments.length;
const resource = substituteDashTemplate(media, {
bandwidth,
- number,
+ number: number++,
representationId,
time,
});
diff --git a/apps/media-server/src/loom-importer/fetch.ts b/apps/media-server/src/loom-importer/fetch.ts
index bdb4f2b40..8313e1126 100644
--- a/apps/media-server/src/loom-importer/fetch.ts
+++ b/apps/media-server/src/loom-importer/fetch.ts
@@ -2,6 +2,12 @@ import { open, rename, rm } from "node:fs/promises";
export class LoomSourceRejectedError extends Error {}
+export class LoomSegmentStatusError extends Error {
+ constructor(readonly status: number) {
+ super(`Loom segment request failed with ${status}`);
+ }
+}
+
export class Semaphore {
private available: number;
private readonly waiters: Array<() => void> = [];
@@ -127,9 +133,7 @@ export async function downloadToFile(
});
if (!response.ok) {
await response.body?.cancel().catch(() => {});
- const error = new Error(
- `Loom segment request failed with ${response.status}`,
- );
+ const error = new LoomSegmentStatusError(response.status);
if (!RETRYABLE_STATUS.has(response.status)) {
permanentFailures++;
if (permanentFailures >= PERMANENT_STATUS_ATTEMPTS) throw error;
diff --git a/apps/media-server/src/loom-importer/job.ts b/apps/media-server/src/loom-importer/job.ts
index b21c52006..c12854ca5 100644
--- a/apps/media-server/src/loom-importer/job.ts
+++ b/apps/media-server/src/loom-importer/job.ts
@@ -27,6 +27,7 @@ import {
type Semaphore,
} from "./fetch";
import {
+ buildAudioOnlyArgs,
buildRemuxArgs,
chooseContainer,
PLAYLIST_INPUT_ARGS,
@@ -175,6 +176,7 @@ function progressUpdate(
interface PreparedSource {
input: string;
inputArgs: string[];
+ skippedSeconds: number;
}
async function prepareSource(
@@ -202,7 +204,7 @@ async function prepareSource(
);
if (result.bytes > maxBytes)
throw new Error("Loom imports are limited to 10 GB");
- return { input: sourcePath, inputArgs: [] };
+ return { input: sourcePath, inputArgs: [], skippedSeconds: 0 };
}
const manifestDir = join(workDir, "manifest");
const segmentDir = join(workDir, "segments");
@@ -242,6 +244,7 @@ async function prepareSource(
return {
input: prefetched.masterPath,
inputArgs: PLAYLIST_INPUT_ARGS(await getFfmpegHlsCapabilities()),
+ skippedSeconds: prefetched.skippedSeconds,
};
}
@@ -345,7 +348,10 @@ export async function runLoomImport(
signal,
);
const codecs = await probeCodecs(source.inputArgs, source.input, signal);
- const container = chooseContainer(codecs);
+ const container =
+ codecs.video === null && codecs.audio !== null
+ ? "audio-only"
+ : chooseContainer(codecs);
if (!container)
throw new UnsupportedStreamError(
`Loom source codecs are not supported (${codecs.video ?? "none"}/${codecs.audio ?? "none"})`,
@@ -357,16 +363,26 @@ export async function runLoomImport(
progressUpdate(job.jobId, "processing", 65, "Packaging video...");
outputPath = join(
workDir,
- container === "mp4" ? "result.mp4" : "original.webm",
+ container === "webm" ? "original.webm" : "result.mp4",
);
await runCommand(
- buildRemuxArgs(
- source.inputArgs,
- source.input,
- outputPath,
- container,
- codecs.audio !== null,
- ),
+ container === "audio-only" && codecs.audio
+ ? buildAudioOnlyArgs(
+ source.inputArgs,
+ source.input,
+ outputPath,
+ codecs.audio,
+ codecs.duration === null
+ ? null
+ : codecs.duration + source.skippedSeconds,
+ )
+ : buildRemuxArgs(
+ source.inputArgs,
+ source.input,
+ outputPath,
+ container === "webm" ? "webm" : "mp4",
+ codecs.audio !== null,
+ ),
settings.remuxTimeoutMs,
signal,
);
diff --git a/apps/media-server/src/loom-importer/media.ts b/apps/media-server/src/loom-importer/media.ts
index cc0689a50..0fb221ee9 100644
--- a/apps/media-server/src/loom-importer/media.ts
+++ b/apps/media-server/src/loom-importer/media.ts
@@ -76,6 +76,7 @@ interface ProbeOutput {
export interface SourceCodecs {
video: string | null;
audio: string | null;
+ duration: number | null;
}
export async function probeCodecs(
@@ -91,7 +92,7 @@ export async function probeCodecs(
"error",
...inputArgs,
"-show_entries",
- "stream=codec_type,codec_name",
+ "stream=codec_type,codec_name:format=duration",
"-of",
"json",
input,
@@ -101,6 +102,7 @@ export async function probeCodecs(
),
) as ProbeOutput;
const streams = output.streams ?? [];
+ const duration = Number(output.format?.duration);
return {
video:
streams.find((stream) => stream.codec_type === "video")?.codec_name ??
@@ -108,6 +110,7 @@ export async function probeCodecs(
audio:
streams.find((stream) => stream.codec_type === "audio")?.codec_name ??
null,
+ duration: Number.isFinite(duration) && duration > 0 ? duration : null,
};
}
@@ -184,7 +187,9 @@ export async function probeOutput(
export type ImportContainer = "mp4" | "webm";
-export function chooseContainer(codecs: SourceCodecs): ImportContainer | null {
+export function chooseContainer(
+ codecs: Pick<SourceCodecs, "video" | "audio">,
+): ImportContainer | null {
if (
codecs.video === "h264" &&
(codecs.audio === null || codecs.audio === "aac")
@@ -232,3 +237,52 @@ export function buildRemuxArgs(
output,
];
}
+
+export function buildAudioOnlyArgs(
+ inputArgs: string[],
+ input: string,
+ output: string,
+ audioCodec: string,
+ duration: number | null,
+): string[] {
+ return [
+ "ffmpeg",
+ "-hide_banner",
+ "-nostdin",
+ "-loglevel",
+ "error",
+ "-f",
+ "lavfi",
+ "-i",
+ `color=c=black:s=1280x720:r=30${duration ? `:d=${duration.toFixed(3)}` : ""}`,
+ ...inputArgs,
+ "-i",
+ input,
+ "-map",
+ "0:v:0",
+ "-map",
+ "1:a:0",
+ "-shortest",
+ "-dn",
+ "-sn",
+ "-c:v",
+ "libx264",
+ "-preset",
+ "ultrafast",
+ "-tune",
+ "stillimage",
+ "-pix_fmt",
+ "yuv420p",
+ "-threads",
+ "2",
+ ...(audioCodec === "aac"
+ ? ["-c:a", "copy"]
+ : ["-c:a", "aac", "-b:a", "128k"]),
+ "-movflags",
+ "+faststart",
+ "-f",
+ "mp4",
+ "-y",
+ output,
+ ];
+}
diff --git a/apps/media-server/src/loom-importer/streaming.ts b/apps/media-server/src/loom-importer/streaming.ts
index d6935c0e0..8a8ec5dff 100644
--- a/apps/media-server/src/loom-importer/streaming.ts
+++ b/apps/media-server/src/loom-importer/streaming.ts
@@ -3,6 +3,7 @@ import { extname, join } from "node:path";
import {
type DownloadToFileOptions,
downloadToFile,
+ LoomSegmentStatusError,
type Semaphore,
} from "./fetch";
@@ -185,6 +186,51 @@ export interface PrefetchResult {
masterPath: string;
bytes: number;
segments: number;
+ skippedSeconds: number;
+}
+
+const MISSING_SEGMENT_FRACTION = 0.01;
+const MISSING_SEGMENT_SECONDS = 6;
+
+function segmentDuration(lines: string[], lineIndex: number): number {
+ for (let index = lineIndex - 1; index >= 0; index--) {
+ const line = lines[index]?.trim() ?? "";
+ if (line.startsWith("#EXTINF:"))
+ return Number.parseFloat(tagValue(line)) || 0;
+ if (line && !line.startsWith("#")) return 0;
+ }
+ return 0;
+}
+
+function missingSegmentLimit(segments: number): number {
+ return Math.max(1, Math.floor(segments * MISSING_SEGMENT_FRACTION));
+}
+
+export function canSkipMissingSegments(
+ missingDurations: number[],
+ segmentDurations: number[],
+): boolean {
+ if (missingDurations.length === 0) return true;
+ if (missingDurations.length >= segmentDurations.length) return false;
+ const total = segmentDurations.reduce((sum, value) => sum + value, 0);
+ const missing = missingDurations.reduce((sum, value) => sum + value, 0);
+ return (
+ missingDurations.length <= missingSegmentLimit(segmentDurations.length) &&
+ missing <=
+ Math.max(MISSING_SEGMENT_SECONDS, total * MISSING_SEGMENT_FRACTION)
+ );
+}
+
+function removeSegmentLines(lines: string[], lineIndex: number) {
+ lines[lineIndex] = "";
+ for (let index = lineIndex - 1; index >= 0; index--) {
+ const line = lines[index]?.trim() ?? "";
+ if (line.startsWith("#EXTINF:")) {
+ lines[index] = "";
+ return;
+ }
+ if (line && !line.startsWith("#")) return;
+ }
}
export async function prefetchMaterializedHls(
@@ -205,10 +251,15 @@ export async function prefetchMaterializedHls(
const playlists = await Promise.all(
tracks.map(async (track) => {
const content = await readFile(track.path, "utf8");
+ const resources = listMediaPlaylistResources(content);
+ const lines = content.split(/\r?\n/);
return {
...track,
- lines: content.split(/\r?\n/),
- resources: listMediaPlaylistResources(content),
+ lines,
+ resources,
+ durations: resources
+ .filter((resource) => !resource.attribute)
+ .map((resource) => segmentDuration(lines, resource.lineIndex)),
};
}),
);
@@ -216,6 +267,7 @@ export async function prefetchMaterializedHls(
playlist.resources.map((resource, index) => ({
playlist,
resource,
+ duration: segmentDuration(playlist.lines, resource.lineIndex),
path: join(
workDir,
`${playlist.name}-${String(index).padStart(6, "0")}${segmentExtension(resource.url)}`,
@@ -229,17 +281,47 @@ export async function prefetchMaterializedHls(
totalSegments: downloads.length,
bytes: 0,
};
+ const missing = new Set<(typeof downloads)[number]>();
+ const playlistBytes = new Map<string, number>();
const results = await Promise.allSettled(
downloads.map((download) =>
options.jobSemaphore
.run(() =>
options.globalSemaphore.run(async () => {
signal.throwIfAborted();
- const bytes = await downloadToFile(
- download.resource.url,
- download.path,
- { ...options, signal },
- );
+ let bytes: number;
+ try {
+ bytes = await downloadToFile(
+ download.resource.url,
+ download.path,
+ { ...options, signal },
+ );
+ } catch (error) {
+ if (
+ !(error instanceof LoomSegmentStatusError) ||
+ error.status !== 404 ||
+ download.resource.attribute
+ )
+ throw error;
+ missing.add(download);
+ progress.completedSegments++;
+ const lost = [...missing]
+ .filter((entry) => entry.playlist === download.playlist)
+ .map((entry) => entry.duration);
+ if (!canSkipMissingSegments(lost, download.playlist.durations)) {
+ const tooMany = new Error(
+ `Loom is missing ${lost.length} of ${download.playlist.durations.length} ${download.playlist.name} segments`,
+ );
+ failure.abort(tooMany);
+ throw tooMany;
+ }
+ return;
+ }
+ if (!download.resource.attribute)
+ playlistBytes.set(
+ download.playlist.name,
+ (playlistBytes.get(download.playlist.name) ?? 0) + bytes,
+ );
progress.bytes += bytes;
progress.completedSegments++;
if (progress.bytes > options.maxBytes) {
@@ -264,6 +346,10 @@ export async function prefetchMaterializedHls(
throw failure.signal.reason ?? rejected.reason;
}
for (const download of downloads) {
+ if (missing.has(download)) {
+ removeSegmentLines(download.playlist.lines, download.resource.lineIndex);
+ continue;
+ }
const line = download.playlist.lines[download.resource.lineIndex] ?? "";
download.playlist.lines[download.resource.lineIndex] = download.resource
.attribute
@@ -277,7 +363,21 @@ export async function prefetchMaterializedHls(
localPlaylists.set(playlist.path, localPath);
}
const localMasterPath = join(workDir, "master-local.m3u8");
- if (selection.masterLines.length > 0) {
+ const audioPlaylist = selection.audio
+ ? localPlaylists.get(selection.audio)
+ : undefined;
+ const audioOnly =
+ audioPlaylist !== undefined &&
+ (playlistBytes.get("video") ?? 0) === 0 &&
+ (playlistBytes.get("audio") ?? 0) > 0;
+ const skippedSeconds = [...missing]
+ .filter(
+ (download) => download.playlist.name === (audioOnly ? "audio" : "video"),
+ )
+ .reduce((sum, download) => sum + download.duration, 0);
+ if (audioOnly && audioPlaylist) {
+ await writeFile(localMasterPath, await readFile(audioPlaylist, "utf8"));
+ } else if (selection.masterLines.length > 0) {
await writeFile(
localMasterPath,
selection.masterLines
@@ -301,5 +401,6 @@ export async function prefetchMaterializedHls(
masterPath: localMasterPath,
bytes: progress.bytes,
segments: downloads.length,
+ skippedSeconds,
};
}
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index 0bb5ae53c..baf27cac4 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -194,8 +194,16 @@ const processSchema = z.object({
preset: z.enum(["ultrafast", "fast", "medium", "slow"]).optional(),
remuxOnly: z.boolean().optional(),
normalizeH264Level: z.boolean().optional(),
+ importedOriginal: z.boolean().optional(),
});
+export function isCompleteRecovery(
+ outputDuration: number,
+ inputDuration: number,
+): boolean {
+ return outputDuration >= inputDuration - Math.max(1, inputDuration * 0.01);
+}
+
const editRangeSchema = z
.object({
start: z.number().min(0),
@@ -1586,7 +1594,7 @@ async function processVideoAsync(
}
const isWebm = isWebmInput(options.inputExtension);
- if (isWebm) {
+ if (isWebm && !options.importedOriginal) {
updateJob(jobId, { message: "Checking the original recording..." });
await withJobHeartbeat(jobId, () =>
validateVideoInput(input.path, signal),
@@ -1643,6 +1651,15 @@ async function processVideoAsync(
lastResortRepairFile = processed.lastResortRepairFile;
const outputPath = processed.outputFile.path;
updateJob(jobId, { outputTempFile: processed.outputFile });
+ if (options.importedOriginal) {
+ const recovered = await withJobHeartbeat(jobId, () =>
+ probeVideoFile(outputPath),
+ );
+ if (!isCompleteRecovery(recovered.duration, metadata.duration))
+ throw new Error(
+ "The imported original could not be fully recovered. The original has been preserved.",
+ );
+ }
if (
options.maxImportBytes &&
file(outputPath).size > options.maxImportBytes
diff --git a/apps/mobile/app/(tabs)/index.tsx b/apps/mobile/app/(tabs)/index.tsx
index 4651a10c7..eb17b5974 100644
--- a/apps/mobile/app/(tabs)/index.tsx
+++ b/apps/mobile/app/(tabs)/index.tsx
@@ -321,6 +321,7 @@ export default function CapsScreen() {
const auth = useAuth();
const authStatus = auth.status;
const apiClient = auth.client;
+ const activeOrganizationId = auth.bootstrap?.activeOrganizationId;
const libraryRevision = useRecordingUploadLibraryRevision();
const [selectedSpaceId, setSelectedSpaceId] = useState<string | null>(null);
const [folder, setFolder] = useState<MobileFolder | null>(null);
@@ -387,8 +388,9 @@ export default function CapsScreen() {
useFocusEffect(
useCallback(() => {
+ if (activeOrganizationId === undefined) return;
void load(libraryRevision === 0);
- }, [libraryRevision, load]),
+ }, [activeOrganizationId, libraryRevision, load]),
);
const loadMore = useCallback(async () => {
diff --git a/apps/mobile/src/screens/dashboard-upload-visibility.test.tsx b/apps/mobile/src/screens/dashboard-upload-visibility.test.tsx
index 9e32cb706..0eb07513d 100644
--- a/apps/mobile/src/screens/dashboard-upload-visibility.test.tsx
+++ b/apps/mobile/src/screens/dashboard-upload-visibility.test.tsx
@@ -1,5 +1,9 @@
import { Video } from "@cap/web-domain";
-import React, { type ReactElement, type ReactNode } from "react";
+import React, {
+ type ComponentProps,
+ type ReactElement,
+ type ReactNode,
+} from "react";
import { ActionSheetIOS, Alert } from "react-native";
import TestRenderer, {
act,
@@ -8,7 +12,9 @@ import TestRenderer, {
} from "react-test-renderer";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { MobileCapSummary } from "@/api/mobile";
+import type { useAuth } from "@/auth/AuthContext";
import { CapSettingsSheet } from "@/caps/CapSettingsSheet";
+import type { OrgSwitcher } from "@/components/OrgSwitcher";
import CapsScreen from "../../app/(tabs)";
import UploadScreen from "../../app/(tabs)/upload";
@@ -16,7 +22,7 @@ type AuthStub = {
status: "signedIn" | "signedOut";
apiKey: string;
bootstrap: {
- activeOrganizationId: string;
+ activeOrganizationId: string | null;
spaces?: Array<{
id: string;
name: string;
@@ -31,7 +37,7 @@ type AuthStub = {
email: string;
name: string | null;
};
- };
+ } | null;
client: {
createFolder: (input: {
color?: string;
@@ -82,6 +88,7 @@ type AuthStub = {
) => Promise<unknown>;
};
refresh: () => Promise<void>;
+ setActiveOrganization: ReturnType<typeof useAuth>["setActiveOrganization"];
};
type HostProps = {
@@ -91,7 +98,9 @@ type HostProps = {
type JsonNode = ReactTestRendererJSON | ReactTestRendererJSON[] | string | null;
-const createAuth = (): AuthStub => ({
+const createAuth = (): AuthStub & {
+ bootstrap: NonNullable<AuthStub["bootstrap"]>;
+} => ({
status: "signedIn",
apiKey: "api-key",
bootstrap: {
@@ -142,6 +151,7 @@ const createAuth = (): AuthStub => ({
),
},
refresh: () => Promise.resolve(),
+ setActiveOrganization: () => Promise.resolve(),
});
const createDeferred = <T,>() => {
@@ -496,7 +506,8 @@ vi.mock("@/components/CapCard", async () => {
vi.mock("@/components/OrgSwitcher", async () => {
const React = await import("react");
return {
- OrgSwitcher: () => React.createElement("OrgSwitcher"),
+ OrgSwitcher: (props: ComponentProps<typeof OrgSwitcher>) =>
+ React.createElement("OrgSwitcher", props),
};
});
@@ -2242,6 +2253,112 @@ describe("upload and dashboard visibility", () => {
expect(push).toHaveBeenCalledWith("/upload");
});
+ it("reloads My Caps after switching organizations with the same API client", async () => {
+ const auth = createAuth();
+ const emptyList = auth.client.listCaps;
+ const requestedOrganizations: Array<string | null> = [];
+ auth.client.listCaps = vi.fn(async () => {
+ const organizationId = auth.bootstrap.activeOrganizationId;
+ requestedOrganizations.push(organizationId);
+ return {
+ ...(await emptyList()),
+ caps: [{ id: organizationId, title: organizationId, upload: null }],
+ };
+ });
+ auth.setActiveOrganization = vi.fn<AuthStub["setActiveOrganization"]>(
+ async (organizationId) => {
+ auth.bootstrap = {
+ ...auth.bootstrap,
+ activeOrganizationId: organizationId,
+ };
+ },
+ );
+ authState.value = auth;
+ const renderer = await renderComponent(<CapsScreen />);
+ try {
+ expect(
+ renderer.root
+ .findAll((node) => String(node.type) === "CapCard")
+ .map((card) => card.props.cap.title),
+ ).toEqual(["org_123"]);
+ const switcher = renderer.root.find(
+ (node) => String(node.type) === "OrgSwitcher",
+ );
+ const onChange: ComponentProps<typeof OrgSwitcher>["onChange"] =
+ switcher.props.onChange;
+ await act(async () => {
+ await onChange("org_456");
+ renderer.update(<CapsScreen />);
+ });
+
+ expect(auth.setActiveOrganization).toHaveBeenCalledWith("org_456");
+ expect(requestedOrganizations).toEqual(["org_123", "org_456"]);
+ expect(
+ renderer.root
+ .findAll((node) => String(node.type) === "CapCard")
+ .map((card) => card.props.cap.title),
+ ).toEqual(["org_456"]);
+ } finally {
+ await act(async () => {
+ renderer.unmount();
+ });
+ }
+ });
+
+ it("keeps the library when bootstrap refreshes the same organization", async () => {
+ const auth = createAuth();
+ auth.client.listCaps = vi.fn(auth.client.listCaps);
+ authState.value = auth;
+ const renderer = await renderComponent(<CapsScreen />);
+ try {
+ await act(async () => {
+ authState.value = { ...auth, bootstrap: { ...auth.bootstrap } };
+ renderer.update(<CapsScreen />);
+ });
+ expect(auth.client.listCaps).toHaveBeenCalledTimes(1);
+ } finally {
+ await act(async () => {
+ renderer.unmount();
+ });
+ }
+ });
+
+ it("loads a personal library with no active organization", async () => {
+ const auth = createAuth();
+ auth.bootstrap.activeOrganizationId = null;
+ auth.client.listCaps = vi.fn(auth.client.listCaps);
+ authState.value = auth;
+ const renderer = await renderComponent(<CapsScreen />);
+ try {
+ expect(auth.client.listCaps).toHaveBeenCalledTimes(1);
+ expect(getTextNodes(renderer.toJSON())).toContain("My Caps");
+ } finally {
+ await act(async () => {
+ renderer.unmount();
+ });
+ }
+ });
+
+ it("waits for the initial bootstrap before loading the library", async () => {
+ const auth = createAuth();
+ auth.client.listCaps = vi.fn(auth.client.listCaps);
+ authState.value = { ...auth, bootstrap: null };
+ const renderer = await renderComponent(<CapsScreen />);
+ try {
+ expect(auth.client.listCaps).not.toHaveBeenCalled();
+ await act(async () => {
+ authState.value = auth;
+ renderer.update(<CapsScreen />);
+ });
+ expect(auth.client.listCaps).toHaveBeenCalledTimes(1);
+ expect(getTextNodes(renderer.toJSON())).toContain("My Caps");
+ } finally {
+ await act(async () => {
+ renderer.unmount();
+ });
+ }
+ });
+
it("switches from My Caps into an available space", async () => {
const auth = createAuth();
auth.bootstrap.spaces = [
diff --git a/apps/web/__tests__/unit/analytics-track-route.test.ts b/apps/web/__tests__/unit/analytics-track-route.test.ts
new file mode 100644
index 000000000..084835470
--- /dev/null
+++ b/apps/web/__tests__/unit/analytics-track-route.test.ts
@@ -0,0 +1,165 @@
+import type { Effect as EffectModule } from "effect";
+import type { NextRequest } from "next/server";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const VIDEO_ID = "video-1";
+const ORG_ID = "org-1";
+const OWNER_ID = "owner-1";
+
+const mocks = vi.hoisted(() => ({
+ video: undefined as Record<string, unknown> | undefined,
+ userId: null as string | null,
+ appendEvents: vi.fn(),
+}));
+
+vi.mock("server-only", () => ({}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({
+ from: () => ({
+ leftJoin: () => ({
+ where: () => ({
+ limit: async () => (mocks.video ? [mocks.video] : []),
+ }),
+ }),
+ }),
+ }),
+ }),
+}));
+
+vi.mock("@cap/database/auth/request-site", () => ({
+ isCrossSiteCookieWrite: () => false,
+}));
+
+vi.mock("@cap/web-backend", async () => {
+ const { Context, Effect } = await import("effect");
+ const { CurrentUser } = await import("@cap/web-domain");
+ const Tinybird = Context.GenericTag<{
+ appendEvents: (events: unknown[]) => EffectModule.Effect<void>;
+ }>("Tinybird");
+ return {
+ Tinybird,
+ provideOptionalAuth: (
+ app: EffectModule.Effect<unknown, unknown, never>,
+ ) => {
+ const withTinybird = app.pipe(
+ Effect.provideService(Tinybird, {
+ appendEvents: (events) =>
+ Effect.sync(() => mocks.appendEvents(events)),
+ }),
+ );
+ return mocks.userId
+ ? withTinybird.pipe(
+ Effect.provideService(CurrentUser, {
+ id: mocks.userId,
+ email: "[email protected]",
+ activeOrganizationId: ORG_ID,
+ } as never),
+ )
+ : withTinybird;
+ },
+ };
+});
+
+vi.mock("@/lib/server", async () => {
+ const { Effect } = await import("effect");
+ return { runPromise: Effect.runPromise };
+});
+
+vi.mock("@/lib/Notification", () => ({
+ createAnonymousViewNotification: vi.fn(async () => undefined),
+ sendFirstViewEmail: vi.fn(async () => undefined),
+}));
+
+const { POST } = await import("@/app/api/analytics/track/route");
+
+const track = (body: Record<string, unknown>) =>
+ POST(
+ new Request("https://cap.so/api/analytics/track", {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify(body),
+ }) as unknown as NextRequest,
+ );
+
+const recordedEvents = () =>
+ mocks.appendEvents.mock.calls.flatMap(
+ ([events]) => events as Array<Record<string, unknown>>,
+ );
+
+describe("POST /api/analytics/track", () => {
+ beforeEach(() => {
+ mocks.appendEvents.mockClear();
+ mocks.userId = null;
+ mocks.video = {
+ orgId: ORG_ID,
+ ownerId: OWNER_ID,
+ firstViewEmailSentAt: new Date("2025-01-01T00:00:00Z"),
+ videoName: "Demo",
+ createdAt: new Date("2025-01-01T00:00:00Z"),
+ updatedAt: new Date("2025-01-01T00:00:00Z"),
+ activeUploadVideoId: null,
+ };
+ });
+
+ it("records the view under the video's own org and share path", async () => {
+ const response = await track({
+ videoId: VIDEO_ID,
+ orgId: "someone-elses-org",
+ ownerId: "someone-else",
+ pathname: "/s/a-different-video",
+ hostname: "attacker.example",
+ sessionId: "session-1",
+ });
+
+ expect(response.status).toBe(200);
+ expect(recordedEvents()).toEqual([
+ expect.objectContaining({
+ tenant_id: ORG_ID,
+ video_id: VIDEO_ID,
+ pathname: `/s/${VIDEO_ID}`,
+ session_id: "session-1",
+ }),
+ ]);
+ });
+
+ it("does not record views for videos that do not exist", async () => {
+ mocks.video = undefined;
+
+ const response = await track({
+ videoId: "made-up-video",
+ orgId: ORG_ID,
+ pathname: `/s/${VIDEO_ID}`,
+ sessionId: "session-1",
+ });
+
+ expect(response.status).toBe(200);
+ expect(recordedEvents()).toEqual([]);
+ });
+
+ it("does not record the owner's own views", async () => {
+ mocks.userId = OWNER_ID;
+
+ await track({ videoId: VIDEO_ID, sessionId: "session-1" });
+
+ expect(recordedEvents()).toEqual([]);
+ });
+
+ it("keeps a client timestamp close to now and replaces one far from it", async () => {
+ const recent = new Date(Date.now() - 30_000).toISOString();
+ await track({ videoId: VIDEO_ID, occurredAt: recent });
+
+ const before = Date.now();
+ await track({ videoId: VIDEO_ID, occurredAt: "2020-01-01T00:00:00Z" });
+ await track({ videoId: VIDEO_ID, occurredAt: "not a date" });
+
+ const [kept, backdated, invalid] = recordedEvents();
+ expect(kept?.timestamp).toBe(recent);
+ for (const event of [backdated, invalid]) {
+ expect(Date.parse(event?.timestamp as string)).toBeGreaterThanOrEqual(
+ before,
+ );
+ }
+ });
+});
diff --git a/apps/web/__tests__/unit/caps-analytics-round-trips.test.ts b/apps/web/__tests__/unit/caps-analytics-round-trips.test.ts
new file mode 100644
index 000000000..036a2b382
--- /dev/null
+++ b/apps/web/__tests__/unit/caps-analytics-round-trips.test.ts
@@ -0,0 +1,359 @@
+import { CurrentUser, Organisation, User, Video } from "@cap/web-domain";
+import { Cause, Effect, Exit, Logger, LogLevel, Option } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+type AccessFixture = {
+ id: string;
+ public?: boolean;
+ owner?: boolean;
+ orgMember?: boolean;
+ spaceMember?: boolean;
+ password?: string;
+ spacePasswords?: string[];
+ domain?: string;
+ grant?: boolean;
+};
+const mocks = vi.hoisted(() => ({
+ query: vi.fn(),
+ latency: 0,
+ size: 50,
+ owner: false,
+ fixtures: null as AccessFixture[] | null,
+ failTable: "",
+}));
+vi.mock("@cap/database", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ return { db: () => drizzle(mocks.query) };
+});
+vi.mock("@cap/utils", async () => {
+ const { isEmailAllowedByRestriction } = await import(
+ "../../../../packages/utils/src/helpers"
+ );
+ return { dub: {}, isEmailAllowedByRestriction };
+});
+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", {
+ succeed: {
+ querySql: () =>
+ Effect.succeed({
+ data: Array.from({ length: mocks.size }, (_, index) => ({
+ pathname: `/s/video-${index}`,
+ views: index + 1,
+ })),
+ }),
+ },
+ }) {}
+ 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 user: CurrentUser["Type"] = {
+ id: User.UserId.make("viewer"),
+ email: "[email protected]",
+ activeOrganizationId: Organisation.OrganisationId.make("org"),
+ iconUrlOrKey: Option.none(),
+};
+function ids() {
+ return Array.from({ length: mocks.size }, (_, index) =>
+ Video.VideoId.make(`video-${index}`),
+ );
+}
+function run() {
+ return Effect.flatMap(Videos, (videos) =>
+ videos.getAnalyticsBulk(ids()),
+ ).pipe(
+ Effect.provide(Videos.DefaultWithoutDependencies),
+ Effect.provide([
+ VideosPolicy.Default,
+ VideosRepo.Default,
+ Storage.Default,
+ Tinybird.Default,
+ Database.Default,
+ ]),
+ Effect.provideService(CurrentUser, user),
+ Logger.withMinimumLogLevel(LogLevel.None),
+ Effect.runPromise,
+ );
+}
+function reply(sql: string, params: unknown[] = []) {
+ if (mocks.failTable && sql.includes(`from \`${mocks.failTable}\``))
+ throw new Error("Database unavailable");
+ if (mocks.fixtures) {
+ const selected = mocks.fixtures.filter((fixture) =>
+ params.includes(fixture.id),
+ );
+ if (sql.includes("from `organization_members`")) {
+ expect(params[0]).toBe("viewer");
+ return selected
+ .filter((fixture) => fixture.orgMember)
+ .map((fixture) =>
+ sql.includes("`shared_videos`.`videoId`,")
+ ? [fixture.id, "org-membership"]
+ : ["org-membership"],
+ );
+ }
+ if (sql.includes("from `space_members`")) {
+ expect(params[0]).toBe("viewer");
+ return selected
+ .filter((fixture) => fixture.spaceMember)
+ .map((fixture) =>
+ sql.includes("`space_videos`.`videoId`,")
+ ? [fixture.id, "space-membership"]
+ : ["space-membership"],
+ );
+ }
+ if (sql.includes("from `space_videos`"))
+ return selected.flatMap((fixture) =>
+ (fixture.spacePasswords ?? []).map((password) =>
+ sql.startsWith("select `space_videos`.`videoId`")
+ ? [fixture.id, password]
+ : ["space", "Space", password],
+ ),
+ );
+ if (sql.includes("from `organizations`"))
+ return mocks.fixtures
+ .filter((fixture) => params.includes(`org-${fixture.id}`))
+ .map((fixture) => [fixture.domain ?? null]);
+ if (sql.includes("from `video_viewer_grants`"))
+ return selected.filter((fixture) => fixture.grant).map(() => ["grant"]);
+ throw new Error(`Unexpected policy query: ${sql}`);
+ }
+ if (sql.includes("from `videos`"))
+ return ids().map((id) => [
+ id,
+ "org",
+ mocks.owner ? "viewer" : "owner",
+ 0,
+ null,
+ ]);
+ if (sql.includes("from `organization_members`"))
+ return sql.includes("`shared_videos`.`videoId`,")
+ ? ids().map((id) => [id, "membership"])
+ : [["membership"]];
+ if (
+ sql.includes("from `space_members`") ||
+ sql.includes("from `space_videos`")
+ )
+ return [];
+ throw new Error(`Unexpected query: ${sql}`);
+}
+async function countRoundTrips() {
+ const pending: Array<() => void> = [];
+ mocks.query.mockImplementation(
+ (sql: string, params: unknown[]) =>
+ new Promise((resolve) =>
+ pending.push(() => resolve({ rows: reply(sql, params) })),
+ ),
+ );
+ let settled = false;
+ const result = run().finally(() => {
+ settled = true;
+ });
+ let rounds = 0;
+ let idleTicks = 0;
+ while (!settled) {
+ const issued = mocks.query.mock.calls.length;
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ if (mocks.query.mock.calls.length !== issued || pending.length === 0) {
+ idleTicks = 0;
+ continue;
+ }
+ if (++idleTicks < 3) continue;
+ idleTicks = 0;
+ rounds++;
+ for (const release of pending.splice(0)) release();
+ }
+ await result;
+ return rounds;
+}
+function timedQueries() {
+ mocks.query.mockImplementation(async (sql: string, params: unknown[]) => {
+ if (mocks.latency)
+ await new Promise((resolve) => setTimeout(resolve, mocks.latency));
+ return { rows: reply(sql, params) };
+ });
+}
+beforeEach(() => {
+ mocks.size = 50;
+ mocks.fixtures = null;
+ mocks.failTable = "";
+ mocks.owner = false;
+ mocks.latency = 0;
+ timedQueries();
+});
+describe("Caps grid analytics database load", () => {
+ it.each([50, 200])("returns every shared count for %i Caps", async (size) => {
+ mocks.size = size;
+ const results = await run();
+ expect(results.map((result) => Exit.getOrElse(result, () => null))).toEqual(
+ ids().map((_, index) => ({ count: index + 1 })),
+ );
+ expect(mocks.query).toHaveBeenCalledTimes(4);
+ });
+ it("keeps owner analytics to one query", async () => {
+ mocks.owner = true;
+ await run();
+ expect(mocks.query).toHaveBeenCalledTimes(1);
+ });
+ it.skipIf(!process.env.CAP_BENCH_CAPS_ANALYTICS)(
+ "measures 20 ms database latency for My Caps and Shared Caps",
+ async () => {
+ for (const owner of [true, false]) {
+ for (const size of [15, 50, 200]) {
+ mocks.owner = owner;
+ mocks.size = size;
+ mocks.latency = 20;
+ const rounds = await countRoundTrips();
+ timedQueries();
+ const times: number[] = [];
+ let queries = 0;
+ for (let iteration = 0; iteration < 7; iteration++) {
+ mocks.query.mockClear();
+ const start = performance.now();
+ const results = await run();
+ times.push(performance.now() - start);
+ queries = mocks.query.mock.calls.length;
+ expect(results.every(Exit.isSuccess)).toBe(true);
+ }
+ times.sort((a, b) => a - b);
+ console.log(
+ JSON.stringify({
+ owner,
+ size,
+ queries,
+ rounds,
+ medianMs: times[3],
+ }),
+ );
+ }
+ }
+ },
+ );
+});
+
+const fixtures: AccessFixture[] = [
+ {
+ id: "owned",
+ owner: true,
+ password: "video-hash",
+ spacePasswords: ["space-hash"],
+ },
+ { id: "org-member", orgMember: true },
+ { id: "space-member", spaceMember: true },
+ { id: "public", public: true },
+ { id: "private" },
+ { id: "protected", orgMember: true, password: "video-hash" },
+ {
+ id: "inherited",
+ spaceMember: true,
+ spacePasswords: ["space-hash", "other-hash"],
+ },
+ { id: "invited", grant: true },
+ { id: "restricted", public: true, domain: "example.com" },
+ { id: "denied-domain", public: true, domain: "other.com" },
+ { id: "domain-invited", public: true, domain: "other.com", grant: true },
+];
+
+function runPolicies(
+ bulk: boolean,
+ authenticated = true,
+ passwords: string[] = [],
+) {
+ const loaded = (mocks.fixtures ?? []).map(
+ (fixture) =>
+ [
+ {
+ id: Video.VideoId.make(fixture.id),
+ orgId: Organisation.OrganisationId.make(`org-${fixture.id}`),
+ ownerId: User.UserId.make(fixture.owner ? "viewer" : "owner"),
+ public: fixture.public ?? false,
+ },
+ Option.fromNullable(fixture.password),
+ ] as const,
+ );
+ const program = Effect.flatMap(VideosPolicy, (policy) =>
+ bulk
+ ? policy.canViewLoadedBulk(loaded)
+ : Effect.forEach(loaded, ([video, password]) =>
+ policy.canViewLoaded(video, password).pipe(Effect.exit),
+ ),
+ );
+ const withUser = authenticated
+ ? program.pipe(Effect.provideService(CurrentUser, user))
+ : program;
+ return withUser.pipe(
+ Effect.provide(VideosPolicy.Default),
+ Effect.provideService(Video.VideoPasswordAttachment, { passwords }),
+ Logger.withMinimumLogLevel(LogLevel.None),
+ Effect.map((results) =>
+ results.map((result) =>
+ Exit.isSuccess(result)
+ ? "allowed"
+ : Option.getOrThrow(Cause.failureOption(result.cause))._tag,
+ ),
+ ),
+ Effect.runPromise,
+ );
+}
+
+describe("batched Caps access parity", () => {
+ it.each([
+ { authenticated: true, passwords: [] },
+ { authenticated: false, passwords: [] },
+ { authenticated: true, passwords: ["video-hash", "other-hash"] },
+ { authenticated: false, passwords: ["video-hash", "other-hash"] },
+ { authenticated: true, passwords: ["wrong-hash"] },
+ ])(
+ "matches individual policies for $authenticated and $passwords",
+ async ({ authenticated, passwords }) => {
+ mocks.fixtures = fixtures;
+ const single = await runPolicies(false, authenticated, passwords);
+ expect(await runPolicies(true, authenticated, passwords)).toEqual(single);
+ },
+ );
+
+ it.each(["space_videos", "organization_members", "space_members"])(
+ "preserves per-video failures from %s",
+ async (table) => {
+ mocks.fixtures = fixtures;
+ mocks.failTable = table;
+ const single = await runPolicies(false);
+ expect(single[0]).toBe("allowed");
+ expect(single.slice(1)).toContain("DatabaseError");
+ expect(await runPolicies(true)).toEqual(single);
+ },
+ );
+
+ it("rechecks access after membership and passwords change", async () => {
+ mocks.fixtures = [{ id: "shared", spaceMember: true }];
+ expect(await runPolicies(true)).toEqual(["allowed"]);
+ mocks.fixtures = [{ id: "shared" }];
+ expect(await runPolicies(true)).toEqual(["PolicyDenied"]);
+ mocks.fixtures = [
+ { id: "shared", spaceMember: true, spacePasswords: ["new-hash"] },
+ ];
+ expect(await runPolicies(true)).toEqual(["VerifyVideoPasswordError"]);
+ });
+
+ it("does not query an empty policy batch", async () => {
+ mocks.fixtures = [];
+ expect(await runPolicies(true)).toEqual([]);
+ expect(mocks.query).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/media-processing-workflows.test.ts b/apps/web/__tests__/unit/media-processing-workflows.test.ts
index 939f74b20..1702588d4 100644
--- a/apps/web/__tests__/unit/media-processing-workflows.test.ts
+++ b/apps/web/__tests__/unit/media-processing-workflows.test.ts
@@ -195,6 +195,51 @@ describe("media processing workflows", () => {
expect(mocks.remove).toHaveBeenCalledWith(payload.rawFileKey);
});
+ it("marks deferred Loom originals so the worker accepts their decoder glitches", async () => {
+ const loomVideo = {
+ ...video,
+ metadata: {
+ loomDeferredProcessing: {
+ status: "started",
+ rawFileKey: payload.rawFileKey,
+ },
+ },
+ };
+ mocks.rows = [
+ [loomVideo],
+ [{ ...pending, rawFileKey: payload.rawFileKey }],
+ [loomVideo],
+ [pending],
+ [],
+ [metadata],
+ [loomVideo],
+ [],
+ ];
+ await expect(processVideoWorkflow(payload)).resolves.toMatchObject({
+ success: true,
+ });
+ const body = JSON.parse(String(mocks.fetch.mock.calls[0]?.[1]?.body));
+ expect(body.importedOriginal).toBe(true);
+ });
+
+ it("checks ordinary uploads strictly", async () => {
+ mocks.rows = [
+ [video],
+ [{ ...pending, rawFileKey: payload.rawFileKey }],
+ [video],
+ [pending],
+ [],
+ [metadata],
+ [video],
+ [],
+ ];
+ await expect(processVideoWorkflow(payload)).resolves.toMatchObject({
+ success: true,
+ });
+ const body = JSON.parse(String(mocks.fetch.mock.calls[0]?.[1]?.body));
+ expect(body.importedOriginal).toBeUndefined();
+ });
+
it("keeps uploaded source data when processing fails", async () => {
mocks.rows = [
[video],
diff --git a/apps/web/__tests__/unit/mobile-caps-list.test.ts b/apps/web/__tests__/unit/mobile-caps-list.test.ts
new file mode 100644
index 000000000..55823a413
--- /dev/null
+++ b/apps/web/__tests__/unit/mobile-caps-list.test.ts
@@ -0,0 +1,379 @@
+import { setTimeout as delay } from "node:timers/promises";
+import {
+ CurrentUser,
+ DatabaseError,
+ HttpAuthMiddleware,
+ type Mobile,
+ Organisation,
+ User,
+ type Video,
+} from "@cap/web-domain";
+import { type HttpApi, HttpApiBuilder, HttpServer } from "@effect/platform";
+import { drizzle } from "drizzle-orm/mysql-proxy";
+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";
+
+vi.mock("server-only", () => ({}));
+vi.mock("@cap/env", () => ({
+ serverEnv: () => ({ WEB_URL: "http://localhost" }),
+}));
+vi.mock("@cap/database/auth/auth-options", () => ({ authOptions: () => ({}) }));
+vi.mock("@cap/database/emails/config", () => ({ sendEmail: vi.fn() }));
+vi.mock("@/actions/loom", () => ({}));
+vi.mock("@/app/(org)/dashboard/analytics/data", () => ({}));
+vi.mock("@/lib/account-deletion-request", () => ({}));
+vi.mock("@/lib/desktop-segments-finalization", () => ({}));
+vi.mock("@/lib/Notification", () => ({}));
+vi.mock("@/lib/sync-video-storage-names", () => ({}));
+vi.mock("@/lib/video-processing", () => ({}));
+vi.mock("@/workflows/import-loom-video", () => ({}));
+vi.mock("workflow/api", () => ({}));
+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-caps-test/Videos"),
+ ImageUploads: Context.GenericTag("mobile-caps-test/ImageUploads"),
+ };
+});
+
+const user = {
+ id: User.UserId.make("viewer"),
+ email: "[email protected]",
+ activeOrganizationId: Organisation.OrganisationId.make("org"),
+ iconUrlOrKey: Option.none(),
+};
+const date = "2026-10-24 12:00:00";
+let size = 30;
+let sharedList = false;
+let empty = false;
+let denied = false;
+let failDatabase = false;
+let failAnalytics = false;
+let latency = 0;
+let activeQueries = 0;
+let peakQueries = 0;
+let queries: string[] = [];
+let analyticsIds: readonly Video.VideoId[] = [];
+
+function owner(index: number) {
+ return !sharedList || index % 4 === 0 ? "viewer" : `owner-${index % 4}`;
+}
+
+function preferences(id: string) {
+ return {
+ blockedUserIds:
+ id === "viewer" ? ["owner-1"] : id === "owner-2" ? ["viewer"] : [],
+ };
+}
+
+const client = drizzle(async (sql, params) => {
+ queries.push(sql);
+ peakQueries = Math.max(peakQueries, ++activeQueries);
+ try {
+ if (latency) await delay(latency);
+ if (failDatabase) throw new Error("database unavailable");
+ if (sql.includes("from `organizations`")) {
+ return {
+ rows: denied ? [] : [["org", "Organization", "viewer", "owner"]],
+ };
+ }
+ if (sql.includes("from `spaces`")) {
+ return { rows: [["team", "Team", "Public", "viewer", "admin", false]] };
+ }
+ if (sql.includes(" group by ")) {
+ return {
+ rows: empty
+ ? []
+ : Array.from({ length: size }, (_, index) => {
+ const id = owner(index);
+ return [
+ `video-${index}`,
+ id,
+ `Cap ${index}`,
+ date,
+ date,
+ id === "owner-3" ? null : id,
+ ...(sql.includes("`users`.`preferences`")
+ ? [id === "owner-3" ? null : preferences(id)]
+ : []),
+ 60 + index,
+ null,
+ true,
+ index === 3,
+ index === 7,
+ 2,
+ 1,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ ];
+ }),
+ };
+ }
+ if (sql.startsWith("select `preferences`")) {
+ return { rows: [[preferences("viewer")]] };
+ }
+ if (sql.startsWith("select `id`, `preferences`")) {
+ return {
+ rows: ["viewer", "owner-1", "owner-2"].map((id) => [
+ id,
+ preferences(id),
+ ]),
+ };
+ }
+ if (sql.includes("from `folders`")) {
+ return { rows: [["folder", "Folder", "normal", null, 4]] };
+ }
+ if (sql.includes("COUNT(")) return { rows: [[61, 81]] };
+ if (sql.includes(" order by `videos`.`effectiveCreatedAt` desc limit ")) {
+ return {
+ rows: empty
+ ? []
+ : Array.from({ length: size }, (_, index) => [
+ `video-${index}`,
+ params.includes("folder") ? "folder" : null,
+ ]),
+ };
+ }
+ throw new Error(`Unexpected query: ${sql}`);
+ } finally {
+ activeQueries--;
+ }
+});
+
+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(client as unknown as DbClient),
+ catch: (cause) => new DatabaseError({ cause }),
+ }),
+ }),
+ ),
+ ),
+ Layer.provide(
+ Layer.mock(Videos, {
+ _tag: "Videos",
+ getAnalyticsBulkForOwner: (ids: readonly Video.VideoId[]) =>
+ Effect.gen(function* () {
+ analyticsIds = ids;
+ if (latency) yield* Effect.promise(() => delay(latency));
+ if (failAnalytics)
+ return yield* new DatabaseError({
+ cause: new Error("analytics unavailable"),
+ });
+ return ids.map((_, index) => ({ count: index + 10 }));
+ }),
+ }),
+ ),
+ 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 { GET } = await import("@/app/api/mobile/[...route]/route");
+
+async function list(params = "") {
+ sharedList = params.includes("spaceId=");
+ return GET(
+ new Request(`http://localhost/api/mobile/caps?limit=${size}${params}`),
+ );
+}
+
+function expectedCaps(folderId: string | null = null) {
+ return Array.from({ length: size }, (_, index) => index)
+ .filter((index) => !sharedList || index % 4 === 0 || index % 4 === 3)
+ .map((index) => {
+ const protectedCap = index === 3 || index === 7;
+ const owned = owner(index) === "viewer";
+ const hasThumbnail = owned || !protectedCap;
+ return {
+ id: `video-${index}`,
+ ownerId: owner(index),
+ shareUrl: `http://localhost/s/video-${index}`,
+ title: `Cap ${index}`,
+ createdAt: "2026-10-24T12:00:00.000Z",
+ updatedAt: "2026-10-24T12:00:00.000Z",
+ ownerName: owner(index) === "owner-3" ? "" : owner(index),
+ durationSeconds: 60 + index,
+ thumbnailUrl: hasThumbnail
+ ? `http://localhost/api/mobile/caps/video-${index}/thumbnail?v=1792843200000`
+ : null,
+ thumbnailCacheKey: hasThumbnail
+ ? `cap-thumbnail:video-${index}:1792843200000`
+ : null,
+ folderId,
+ public: true,
+ protected: protectedCap,
+ viewCount:
+ owned && !failAnalytics ? (sharedList ? index / 4 : index) + 10 : 0,
+ commentCount: 2,
+ reactionCount: 1,
+ upload: null,
+ ownedByCurrentUser: owned,
+ };
+ });
+}
+
+beforeEach(() => {
+ size = 30;
+ empty = false;
+ denied = false;
+ failDatabase = false;
+ failAnalytics = false;
+ latency = 0;
+ activeQueries = 0;
+ peakQueries = 0;
+ queries = [];
+ analyticsIds = [];
+});
+afterAll(() => dispose());
+
+describe("mobile Caps list", () => {
+ it.each(["", "&spaceId=org", "&spaceId=team", "&folderId=folder"])(
+ "preserves summaries, blocks, pagination and locations for %s",
+ async (params) => {
+ const response = await list(`${params}&page=2`);
+ expect(response.status).toBe(200);
+ const body: typeof Mobile.MobileCapsListResponse.Type =
+ await response.json();
+ expect(body).toEqual({
+ caps: expectedCaps(params.includes("folderId") ? "folder" : null),
+ folders: params.includes("folderId")
+ ? []
+ : [
+ {
+ id: "folder",
+ name: "Folder",
+ color: "normal",
+ parentId: null,
+ videoCount: 4,
+ },
+ ],
+ page: 2,
+ limit: 30,
+ total: 61,
+ collectionTotal: 81,
+ hasMore: true,
+ });
+ expect(analyticsIds).toEqual(
+ body.caps.filter((cap) => cap.ownedByCurrentUser).map((cap) => cap.id),
+ );
+ },
+ );
+
+ it.each([
+ ["", 6, 4],
+ ["&spaceId=org", 7, 4],
+ ["&spaceId=team", 8, 4],
+ ["&folderId=folder", 5, 3],
+ ] as const)(
+ "bounds SQL requests and overlaps independent reads for %s",
+ async (params, count, peak) => {
+ latency = 5;
+ expect((await list(params)).status).toBe(200);
+ expect(queries).toHaveLength(count);
+ expect(peakQueries).toBe(peak);
+ },
+ );
+
+ it("keeps collection totals on an empty page", async () => {
+ empty = true;
+ const response = await list("&page=4");
+ expect(response.status).toBe(200);
+ expect(await response.json()).toMatchObject({
+ caps: [],
+ total: 61,
+ collectionTotal: 81,
+ hasMore: false,
+ });
+ expect(queries).toHaveLength(4);
+ expect(analyticsIds).toEqual([]);
+ });
+
+ it("keeps zero views when analytics fails", async () => {
+ failAnalytics = true;
+ const response = await list();
+ expect(response.status).toBe(200);
+ expect((await response.json()).caps).toEqual(expectedCaps());
+ });
+
+ it("rejects inaccessible spaces before loading caps", async () => {
+ denied = true;
+ expect((await list("&spaceId=team")).status).toBe(403);
+ expect(queries).toHaveLength(1);
+ expect(analyticsIds).toEqual([]);
+ });
+
+ it("returns an error when a required database read fails", async () => {
+ failDatabase = true;
+ expect((await list()).status).toBe(500);
+ expect(analyticsIds).toEqual([]);
+ });
+
+ it.skipIf(!process.env.CAP_API_LIST_BENCH)(
+ "benchmarks the GET handler with 40 ms dependency latency",
+ async () => {
+ latency = 40;
+ for (const pageSize of [20, 30, 50]) {
+ size = pageSize;
+ await list();
+ const samples: number[] = [];
+ for (let sample = 0; sample < 7; sample++) {
+ queries = [];
+ peakQueries = 0;
+ const start = performance.now();
+ const response = await list();
+ expect(response.status).toBe(200);
+ expect((await response.json()).caps).toEqual(expectedCaps());
+ samples.push(performance.now() - start);
+ }
+ console.log(
+ JSON.stringify({
+ pageSize,
+ latency,
+ medianMs: samples.sort((a, b) => a - b)[3],
+ sqlRequests: queries.length,
+ peakQueries,
+ }),
+ );
+ }
+ },
+ );
+});
diff --git a/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts b/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
index ee7e21f39..440b89e24 100644
--- a/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
+++ b/apps/web/__tests__/unit/recording-storage-lifecycle.test.ts
@@ -156,6 +156,16 @@ function databaseFixture(video = recording(), password?: string | null) {
],
]);
const uploads = new Map<string, typeof Db.videoUploads.$inferSelect>();
+ const shares = new Map<unknown, Map<string, { videoId: string }>>(
+ [Db.sharedVideos, Db.spaceVideos].map((table) => [
+ table,
+ new Map([
+ ["root-share", { videoId }],
+ ["folder-share", { videoId }],
+ ["other-share", { videoId: "other-video" }],
+ ]),
+ ]),
+ );
const mutations: DatabaseMutation[] = [];
const events: string[] = [];
const objectDeleteConditions: SQL[] = [];
@@ -248,6 +258,12 @@ function databaseFixture(video = recording(), password?: string | null) {
: mutation.table === Db.videoProcessingJobs
? jobs
: null;
+ const shareRows = shares.get(mutation.table);
+ if (shareRows && mutation.operation === "delete") {
+ for (const [id, row] of shareRows) {
+ if (row.videoId === mutation.id) shareRows.delete(id);
+ }
+ }
if (!table) continue;
if (mutation.operation === "delete" && mutation.id)
table.delete(mutation.id);
@@ -304,6 +320,7 @@ function databaseFixture(video = recording(), password?: string | null) {
rows,
jobs,
uploads,
+ shares,
mutations,
events,
objectDeleteConditions,
@@ -1082,6 +1099,19 @@ describe("recording storage lifecycle", () => {
},
);
+ it("removes organization and space shares only for the deleted recording", async () => {
+ const database = databaseFixture();
+ await storageFixture([[outputKey, "verified-video"]]);
+
+ expect(Exit.isSuccess(await runVideoOperation("delete"))).toBe(true);
+ expect(database.rows.has(videoId)).toBe(false);
+ for (const table of [Db.sharedVideos, Db.spaceVideos]) {
+ expect(database.shares.get(table)).toEqual(
+ new Map([["other-share", { videoId: "other-video" }]]),
+ );
+ }
+ });
+
it("waits for job deletion before deleting any dependent database row", async () => {
const database = databaseFixture();
let release: (() => void) | undefined;
@@ -1137,6 +1167,13 @@ describe("recording storage lifecycle", () => {
state: "source-blocked",
errorCode: "video-deleting",
});
+ for (const shares of database.shares.values()) {
+ expect([...shares.values()].map((share) => share.videoId)).toEqual([
+ videoId,
+ videoId,
+ "other-video",
+ ]);
+ }
storage.deniedDeletes.clear();
expect(Exit.isSuccess(await runVideoOperation("delete"))).toBe(true);
expect(database.rows.has(videoId)).toBe(false);
diff --git a/apps/web/__tests__/unit/reply-notification.test.ts b/apps/web/__tests__/unit/reply-notification.test.ts
new file mode 100644
index 000000000..4202e92c7
--- /dev/null
+++ b/apps/web/__tests__/unit/reply-notification.test.ts
@@ -0,0 +1,101 @@
+import { Comment } from "@cap/web-domain";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ query: vi.fn(),
+ sendEmail: vi.fn(),
+}));
+
+vi.mock("server-only", () => ({}));
+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/emails/config", () => ({
+ sendEmail: mocks.sendEmail,
+}));
+
+import { createNotification } from "@/lib/Notification";
+
+function prepareReply({
+ recipientId = "comment-author",
+ pauseReplies = false,
+ existingReply = false,
+}: {
+ recipientId?: string | null;
+ pauseReplies?: boolean;
+ existingReply?: boolean;
+} = {}) {
+ mocks.query
+ .mockResolvedValueOnce({ rows: [["video", "owner"]] })
+ .mockResolvedValueOnce({ rows: [["owner", "owner-org", null]] })
+ .mockResolvedValueOnce({ rows: recipientId ? [[recipientId]] : [] })
+ .mockResolvedValueOnce({
+ rows: [[{ notifications: { pauseReplies } }, "reply-org"]],
+ })
+ .mockResolvedValueOnce({ rows: existingReply ? [["existing-reply"]] : [] })
+ .mockResolvedValueOnce({ rows: [{ affectedRows: 1, insertId: 0 }] });
+}
+
+function reply(authorId: string) {
+ return createNotification({
+ type: "reply",
+ videoId: "video",
+ authorId,
+ comment: { id: "reply", content: "Following up" },
+ parentCommentId: Comment.CommentId.make("parent"),
+ });
+}
+
+function notificationInserts() {
+ return mocks.query.mock.calls.filter(([query]) =>
+ query.startsWith("insert into `notifications`"),
+ );
+}
+
+beforeEach(() => {
+ mocks.query.mockReset();
+ mocks.query.mockRejectedValue(new Error("Unexpected query"));
+});
+
+describe("reply notifications", () => {
+ it("does not notify someone replying to their own comment on another person's Cap", async () => {
+ prepareReply();
+
+ await expect(reply("comment-author")).resolves.toBeUndefined();
+
+ expect(notificationInserts()).toHaveLength(0);
+ expect(mocks.sendEmail).not.toHaveBeenCalled();
+ });
+
+ it.each(["another-viewer", "owner"])(
+ "notifies the parent author when %s replies",
+ async (authorId) => {
+ prepareReply();
+
+ await expect(reply(authorId)).resolves.toMatchObject({ success: true });
+
+ const inserts = notificationInserts();
+ expect(inserts).toHaveLength(1);
+ expect(inserts[0]?.[1]).toEqual(
+ expect.arrayContaining(["comment-author", "reply-org", "reply"]),
+ );
+ expect(mocks.sendEmail).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each([
+ { name: "replies are paused", pauseReplies: true },
+ { name: "the reply was already delivered", existingReply: true },
+ { name: "the parent belongs to the video owner", recipientId: "owner" },
+ { name: "the parent no longer exists", recipientId: null },
+ ])("does not notify when $name", async ({ name: _name, ...options }) => {
+ prepareReply(options);
+
+ await expect(reply("another-viewer")).resolves.toBeUndefined();
+
+ expect(notificationInserts()).toHaveLength(0);
+ expect(mocks.sendEmail).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/__tests__/unit/share-page-round-trips.test.ts b/apps/web/__tests__/unit/share-page-round-trips.test.ts
new file mode 100644
index 000000000..27db7cf59
--- /dev/null
+++ b/apps/web/__tests__/unit/share-page-round-trips.test.ts
@@ -0,0 +1,324 @@
+import { createHash } from "node:crypto";
+import { organizations } from "@cap/database/schema";
+import { getTableColumns } from "drizzle-orm";
+import { Context, Effect } from "effect";
+import { type ComponentProps, isValidElement, type ReactElement } from "react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({ query: vi.fn() }));
+
+vi.mock("server-only", () => ({}));
+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: "viewer", activeOrganizationId: "org-1" }),
+}));
+vi.mock("@cap/env", () => ({
+ buildEnv: { NEXT_PUBLIC_WEB_URL: "https://cap.so" },
+ serverEnv: () => ({}),
+}));
+vi.mock("@cap/ui", () => ({ Logo: () => null }));
+vi.mock("@cap/utils", () => ({ userIsPro: () => true }));
+vi.mock("@cap/web-backend", async () => {
+ const { Context, Effect } = await import("effect");
+ const { resolveEffectiveVideoRules } = await import(
+ "../../../../packages/web-backend/src/Videos/EffectiveVideoRules"
+ );
+ return {
+ Database: Context.GenericTag("SharePerfDatabase"),
+ ImageUploads: Context.GenericTag("SharePerfImages"),
+ Videos: Context.GenericTag("SharePerfVideos"),
+ Tinybird: Context.GenericTag("SharePerfAnalytics"),
+ provideOptionalAuth: <A, E, R>(effect: Effect.Effect<A, E, R>) => effect,
+ resolveEffectiveVideoRules,
+ };
+});
+vi.mock("@cap/web-backend/src/Videos/VideosPolicy", () => ({
+ VideosPolicy: Context.GenericTag("SharePerfPolicy"),
+}));
+vi.mock("@/lib/server", async () => {
+ const { db } = await import("@cap/database");
+ return {
+ runPromise: <A, E>(effect: Effect.Effect<A, E, unknown>) =>
+ effect.pipe(
+ Effect.provideService(Context.GenericTag("SharePerfPolicy"), {
+ canViewLoaded: () => Effect.void,
+ }),
+ Effect.provideService(Context.GenericTag("SharePerfDatabase"), {
+ use: <T>(callback: (client: ReturnType<typeof db>) => Promise<T>) =>
+ Effect.promise(() => callback(db())),
+ }),
+ Effect.provideService(Context.GenericTag("SharePerfImages"), {
+ resolveImageUrl: (url: string) => Effect.succeed(url),
+ }),
+ Effect.provideService(Context.GenericTag("SharePerfAnalytics"), {
+ querySql: () => Effect.succeed({ data: [{ views: 120 }] }),
+ }),
+ Effect.runPromise,
+ ),
+ };
+});
+vi.mock("next/headers", () => ({ headers: async () => new Headers() }));
+vi.mock("next/navigation", () => ({ notFound: vi.fn() }));
+vi.mock("@/lib/ai/provider", () => ({ isAiConfigured: () => false }));
+vi.mock("@/lib/desktop-segments-recovery", () => ({
+ completeDesktopSegmentsManifestAndQueue: vi.fn(),
+}));
+vi.mock("@/lib/Notification", () => ({
+ createNotification: async () => undefined,
+}));
+vi.mock("@/lib/loom-deferred-processing", () => ({
+ getPendingLoomProcessing: () => null,
+}));
+vi.mock("@/lib/public-share-video", () => ({ getPublicShareVideo: vi.fn() }));
+vi.mock("@/lib/share-playback", () => ({
+ getSharePlaybackUrl: async () => "https://media.example.com/result.mp4",
+}));
+vi.mock("@/lib/share-web-url", () => ({ resolveShareWebUrl: vi.fn() }));
+vi.mock("@/lib/transcribe", () => ({ transcribeVideo: vi.fn() }));
+vi.mock("@/utils/flags", () => ({ isAiGenerationEnabled: async () => true }));
+vi.mock("@/app/s/[videoId]/_components/PasswordGate", () => ({
+ PasswordGate: () => null,
+}));
+vi.mock("@/app/s/[videoId]/_components/PendingRecordingShare", () => ({
+ PendingRecordingShare: () => null,
+}));
+vi.mock("@/app/s/[videoId]/_components/ShareHeader", () => ({
+ ShareHeader: () => null,
+}));
+vi.mock("@/app/s/[videoId]/Share", () => ({ Share: () => null }));
+
+import type { ShareHeader } from "@/app/s/[videoId]/_components/ShareHeader";
+import ShareVideoPage from "@/app/s/[videoId]/page";
+import type { Share } from "@/app/s/[videoId]/Share";
+
+const createdAt = "2026-10-20 12:00:00";
+const organization = {
+ id: "org-1",
+ name: "Team",
+ ownerId: "viewer",
+ settings: {},
+};
+const recording = {
+ id: "video-1",
+ name: "Ten minute walkthrough",
+ ownerId: "owner",
+ orgId: "org-1",
+ createdAt,
+ updatedAt: createdAt,
+ effectiveCreatedAt: createdAt,
+ source: { type: "desktopMP4" },
+ metadata: { summary: "Project walkthrough" },
+ duration: 600,
+ public: 1,
+ isScreenshot: 0,
+ transcriptionStatus: "COMPLETE",
+};
+
+function project(
+ query: string,
+ tables: Record<string, Record<string, unknown>>,
+) {
+ return query
+ .slice(7, query.indexOf(" from "))
+ .split(", ")
+ .map((column) => {
+ if (column.includes(" IS NOT NULL")) return 0;
+ const match = /^`(\w+)`\.`(\w+)`$/.exec(column);
+ if (!match) throw new Error(`Unexpected projection: ${column}`);
+ return tables[match[1] ?? ""]?.[match[2] ?? ""] ?? null;
+ });
+}
+
+function rowsFor(query: string) {
+ if (
+ query.includes("from `videos` inner join") ||
+ query.includes("from `videos` left join")
+ ) {
+ return [
+ project(query, {
+ videos: recording,
+ users: { id: "owner", name: "Owner", email: "[email protected]" },
+ organizations: organization,
+ shared_videos: { organizationId: "shared-org" },
+ }),
+ ];
+ }
+ if (query.includes("from `comments`")) {
+ return Array.from({ length: 30 }, (_, index) =>
+ project(query, {
+ comments: {
+ id: `comment-${index}`,
+ videoId: recording.id,
+ authorId: `author-${index % 5}`,
+ content: `Review note ${index}`,
+ type: "text",
+ timestamp: index * 20,
+ createdAt,
+ updatedAt: createdAt,
+ },
+ users: {
+ name: `Reviewer ${index % 5}`,
+ image: "https://example.com/avatar.png",
+ },
+ }),
+ );
+ }
+ if (query.includes("from `organizations`")) {
+ if (query.includes("left join")) return [["viewer", "member"]];
+ if (query.includes("inner join")) return [];
+ if (query.startsWith("select `id`, `name` from"))
+ return [["org-1", "Team"]];
+ return [
+ Object.values(getTableColumns(organizations)).map(
+ (column) =>
+ (organization as Record<string, unknown>)[column.name] ?? null,
+ ),
+ ];
+ }
+ if (query.includes("from `organization_members`")) {
+ if (query.includes("count(")) return [[5]];
+ if (query.startsWith("select `role`")) return [["member"]];
+ return [];
+ }
+ if (query.includes("from `spaces`"))
+ return Array.from({ length: 3 }, (_, i) => [
+ `dashboard-space-${i}`,
+ 0,
+ "Public",
+ 0,
+ `Space ${i}`,
+ null,
+ "org-1",
+ "viewer",
+ null,
+ {},
+ "member",
+ 0,
+ 5,
+ 10,
+ ]);
+ if (query.includes("from `videos`")) return [["org-1"]];
+ if (query.includes("from `shared_videos`")) {
+ if (query.includes("COUNT(")) return [[10]];
+ if (query.includes("inner join `organization_members`")) return [];
+ if (query.includes("inner join `organizations`"))
+ return [["shared-org", "Shared team", "shared-org", null]];
+ return [["shared-org"]];
+ }
+ if (query.includes("from `space_videos`")) {
+ if (query.includes("inner join `space_members`"))
+ return query.includes("inner join `spaces`")
+ ? [["space-1", "Project", "org-1"]]
+ : [["space-member"]];
+ if (query.includes("inner join `spaces`"))
+ return [["space-1", "Project", "org-1", null, {}, 0]];
+ return [["space-1"]];
+ }
+ if (query.includes("from `space_members`")) return [["space-member"]];
+ if (query.includes("from `video_edits`")) return [];
+ throw new Error(`Unexpected query: ${query}`);
+}
+
+async function loadPage() {
+ const page = (await ShareVideoPage({
+ params: Promise.resolve({ videoId: recording.id }),
+ searchParams: Promise.resolve({}),
+ })) as ReactElement<{ children: unknown }>;
+ const authorized = page.props.children;
+ if (!isValidElement(authorized) || typeof authorized.type !== "function") {
+ throw new Error("Expected authorized content");
+ }
+ const content = await (
+ authorized.type as (
+ props: unknown,
+ ) => Promise<
+ ReactElement<{ children: ReactElement<ComponentProps<typeof Share>> }>
+ >
+ )(authorized.props);
+ const props = content.props.children.props;
+ const { header, comments, views, initialPlaybackUrl, ...data } = props;
+ const headerElement = header as ReactElement<
+ ComponentProps<typeof ShareHeader>
+ >;
+ return {
+ ...data,
+ header: { ...headerElement.props, views: await headerElement.props.views },
+ comments: await comments,
+ views: await views,
+ initialPlaybackUrl: await initialPlaybackUrl,
+ };
+}
+
+beforeEach(() => mocks.query.mockReset());
+
+describe("share page database round trips", () => {
+ it("keeps download permission and edit checks within the existing page load", async () => {
+ const pending: Array<() => void> = [];
+ mocks.query.mockImplementation(
+ (query: string) =>
+ new Promise((resolve) => {
+ pending.push(() => resolve({ rows: rowsFor(query) }));
+ }),
+ );
+ let settled = false;
+ const page = loadPage().finally(() => {
+ settled = true;
+ });
+ let roundTrips = 0;
+ let idleTicks = 0;
+ while (!settled) {
+ const issued = mocks.query.mock.calls.length;
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ if (mocks.query.mock.calls.length !== issued || pending.length === 0) {
+ idleTicks = 0;
+ continue;
+ }
+ if (++idleTicks < 3) continue;
+ idleTicks = 0;
+ roundTrips++;
+ for (const release of pending.splice(0)) release();
+ }
+ const result = await page;
+ expect(result.comments).toHaveLength(30);
+ expect(result.data).toMatchObject({ duration: 600 });
+ expect(result.header.spacesData).toHaveLength(4);
+ expect(result.header).toMatchObject({ canDownload: true, hasEdits: false });
+ expect({ roundTrips, queries: mocks.query.mock.calls.length }).toEqual({
+ roundTrips: 4,
+ queries: 20,
+ });
+ });
+
+ it.skipIf(!process.env.CAP_SHARE_PAGE_BENCH)(
+ "measures simulated 20ms database latency",
+ async () => {
+ const samples: number[] = [];
+ const outputs = new Set<string>();
+ mocks.query.mockImplementation(async (query: string) => {
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ return { rows: rowsFor(query) };
+ });
+ for (let iteration = 0; iteration < 22; iteration++) {
+ const start = performance.now();
+ const result = await loadPage();
+ if (iteration > 0) samples.push(performance.now() - start);
+ outputs.add(
+ createHash("sha256").update(JSON.stringify(result)).digest("hex"),
+ );
+ }
+ samples.sort((a, b) => a - b);
+ expect(outputs.size).toBe(1);
+ console.log(
+ JSON.stringify({
+ medianMs: samples[10],
+ queriesPerLoad: mocks.query.mock.calls.length / 22,
+ output: [...outputs][0],
+ }),
+ );
+ },
+ );
+});
diff --git a/apps/web/__tests__/unit/share-transcript-rendering.test.ts b/apps/web/__tests__/unit/share-transcript-rendering.test.ts
new file mode 100644
index 000000000..900746d2c
--- /dev/null
+++ b/apps/web/__tests__/unit/share-transcript-rendering.test.ts
@@ -0,0 +1,365 @@
+// @vitest-environment jsdom
+
+import {
+ act,
+ createElement,
+ Profiler,
+ type ProfilerOnRenderCallback,
+} from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { Transcript } from "@/app/s/[videoId]/_components/tabs/Transcript";
+import { parseTranscriptVTT } from "@/app/s/[videoId]/_components/utils/parse-transcript-vtt";
+import type { VideoData } from "@/app/s/[videoId]/types";
+
+const mocks = vi.hoisted(() => ({
+ content: "",
+ rowRenders: 0,
+ userId: "owner",
+ language: "original",
+ edit: vi.fn(),
+ invalidate: vi.fn(),
+}));
+
+vi.mock("@cap/ui", () => ({ Button: () => null }));
+vi.mock("@tanstack/react-query", () => ({
+ useQueryClient: () => ({ invalidateQueries: vi.fn() }),
+ useMutation: () => ({ isPending: false, isError: false }),
+}));
+vi.mock("hooks/use-transcript", () => ({
+ useTranscript: () => ({ data: mocks.content, isLoading: false }),
+ useInvalidateTranscript: () => mocks.invalidate,
+}));
+vi.mock("hooks/use-live-transcript", () => ({
+ useLiveTranscript: () => ({ data: null }),
+}));
+vi.mock("@/app/Layout/AuthContext", () => ({
+ useCurrentUser: () => ({ id: mocks.userId }),
+}));
+vi.mock("@/app/s/[videoId]/_components/CaptionContext", () => ({
+ useCaptionContext: () => ({
+ selectedLanguage: mocks.language,
+ currentVttContent: mocks.content,
+ availableTranslations: [],
+ }),
+}));
+vi.mock("@/actions/videos/edit-transcript", () => ({
+ editTranscriptEntry: mocks.edit,
+}));
+vi.mock("lucide-react", async (importOriginal) => {
+ const icons = await importOriginal<typeof import("lucide-react")>();
+ return {
+ ...icons,
+ Edit3: (props: Parameters<typeof icons.Edit3>[0]) => {
+ mocks.rowRenders++;
+ return createElement(icons.Edit3, props);
+ },
+ };
+});
+
+const video: VideoData = {
+ id: "share-transcript" as VideoData["id"],
+ owner: { id: "owner" as VideoData["owner"]["id"], isPro: true },
+ orgId: "organization" as VideoData["orgId"],
+ name: "One-hour recording",
+ source: { type: "desktopMP4" },
+ duration: 3600,
+ width: 1920,
+ height: 1080,
+ fps: 30,
+ public: true,
+ isScreenshot: false,
+ skipProcessing: false,
+ createdAt: new Date("2026-01-01"),
+ updatedAt: new Date("2026-01-01"),
+ effectiveCreatedAt: new Date("2026-01-01"),
+ transcriptionStatus: "COMPLETE",
+ bucket: null,
+ storageIntegrationId: null,
+ metadata: null,
+ settings: null,
+ folderId: null,
+ password: null,
+ xStreamInfo: null,
+ firstViewEmailSentAt: null,
+ awsRegion: null,
+ awsBucket: null,
+ videoStartTime: null,
+ audioStartTime: null,
+ jobId: null,
+ jobStatus: null,
+};
+
+function transcript(cues: number) {
+ const timestamp = (seconds: number) =>
+ `${new Date(seconds * 1000).toISOString().slice(11, 19)}.000`;
+ return `WEBVTT\n\n${Array.from(
+ { length: cues },
+ (_, i) =>
+ `${i + 1}\n${timestamp(i * 3)} --> ${timestamp((i + 1) * 3)}\nCue ${i + 1}: We reviewed the recording and agreed on the next steps.\n`,
+ ).join("\n")}`;
+}
+
+let root: Root;
+let container: HTMLDivElement;
+let durations: number[];
+const onSeek = vi.fn();
+const onRender: ProfilerOnRenderCallback = (_id, phase, actualDuration) => {
+ if (phase === "update") durations.push(actualDuration);
+};
+
+beforeEach(() => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ mocks.content = transcript(1200);
+ mocks.userId = "owner";
+ mocks.language = "original";
+ mocks.rowRenders = 0;
+ mocks.edit.mockResolvedValue({ success: true });
+ durations = [];
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+});
+
+afterEach(async () => {
+ await act(async () => root.unmount());
+ container.remove();
+ vi.unstubAllGlobals();
+});
+
+async function render(seek = onSeek) {
+ await act(async () => {
+ root.render(
+ createElement(
+ Profiler,
+ { id: video.id, onRender },
+ createElement(Transcript, { data: video, onSeek: seek }),
+ ),
+ );
+ });
+}
+
+function cue(index: number) {
+ const button = Array.from(container.querySelectorAll("button")).find(
+ (button) => button.textContent?.includes(`Cue ${index}:`),
+ );
+ if (!button) throw new Error(`Missing cue ${index}`);
+ return button;
+}
+
+async function click(button: HTMLElement) {
+ await act(async () => button.click());
+}
+
+function button(text: string) {
+ const button = Array.from(container.querySelectorAll("button")).find(
+ (button) => button.textContent === text,
+ );
+ if (!button) throw new Error(`Missing button ${text}`);
+ return button;
+}
+
+async function editCue(index: number) {
+ const edit = cue(index).parentElement?.querySelector<HTMLButtonElement>(
+ 'button[title="Edit transcript entry"]',
+ );
+ if (!edit) throw new Error("Missing edit button");
+ await click(edit);
+}
+
+async function type(text: string) {
+ const textarea = container.querySelector("textarea");
+ if (!textarea) throw new Error("Missing transcript editor");
+ await act(async () => {
+ Object.getOwnPropertyDescriptor(
+ HTMLTextAreaElement.prototype,
+ "value",
+ )?.set?.call(textarea, text);
+ textarea.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+}
+
+describe("share transcript rendering", () => {
+ it("only renders the changed rows when selecting a cue in a one-hour transcript", async () => {
+ await render();
+ expect(mocks.rowRenders).toBe(1200);
+ const rows = () =>
+ Array.from(
+ container.querySelectorAll('button[title="Edit transcript entry"]'),
+ (button) => button.parentElement?.textContent,
+ );
+ const expectedRows = parseTranscriptVTT(mocks.content).map(
+ (entry) => `${entry.timestamp}${entry.text}`,
+ );
+ expect(rows()).toEqual(expectedRows);
+ mocks.rowRenders = 0;
+ await click(cue(300));
+ expect(onSeek).toHaveBeenLastCalledWith(897);
+ expect(cue(300).parentElement?.className).toContain("bg-gray-3");
+ expect(mocks.rowRenders).toBe(1);
+ mocks.rowRenders = 0;
+ await click(cue(600));
+ expect(cue(300).parentElement?.className).not.toContain("bg-gray-3");
+ expect(cue(600).parentElement?.className).toContain("bg-gray-3");
+ expect(mocks.rowRenders).toBe(2);
+ expect(rows()).toEqual(expectedRows);
+ });
+
+ it("does not render other rows while typing and preserves save and cancel", async () => {
+ await render();
+ await editCue(300);
+ mocks.rowRenders = 0;
+ await type("Updated cue");
+ expect(container.querySelector("textarea")?.value).toBe("Updated cue");
+ expect(mocks.rowRenders).toBe(0);
+ await click(button("Save"));
+ expect(mocks.edit).toHaveBeenCalledWith(video.id, 300, "Updated cue");
+ expect(mocks.invalidate).toHaveBeenCalledWith(video.id);
+ expect(container.textContent).toContain("Updated cue");
+ expect(container.querySelector("textarea")).toBeNull();
+ await editCue(600);
+ await type("Discarded edit");
+ await click(button("Cancel"));
+ expect(cue(600).textContent).toContain("We reviewed the recording");
+ expect(container.textContent).not.toContain("Discarded edit");
+ expect(mocks.edit).toHaveBeenCalledTimes(1);
+ });
+
+ it("keeps an unsuccessful edit open without changing the other cues", async () => {
+ mocks.content = transcript(3);
+ mocks.edit.mockResolvedValue({ success: false, message: "Save failed" });
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ await render();
+ await editCue(2);
+ await type("Unsaved edit");
+ await click(button("Save"));
+ expect(container.querySelector("textarea")?.value).toBe("Unsaved edit");
+ expect(button("Save").disabled).toBe(false);
+ expect(cue(1).textContent).toContain("We reviewed the recording");
+ expect(cue(3).textContent).toContain("We reviewed the recording");
+ expect(mocks.invalidate).not.toHaveBeenCalled();
+ });
+
+ it("refreshes cue content, language, permissions and seek callbacks", async () => {
+ mocks.content = transcript(3);
+ await render();
+ mocks.content = mocks.content.replace("Cue 2:", "Translated Cue 2:");
+ mocks.language = "fr";
+ await render();
+ expect(container.textContent).toContain("Translated Cue 2:");
+ expect(
+ container.querySelector('button[title="Edit transcript entry"]'),
+ ).toBeNull();
+ await click(cue(1));
+ expect(onSeek).toHaveBeenLastCalledWith(0);
+ mocks.language = "original";
+ mocks.userId = "viewer";
+ await render();
+ expect(
+ container.querySelector('button[title="Edit transcript entry"]'),
+ ).toBeNull();
+ mocks.userId = "owner";
+ await render();
+ expect(
+ container.querySelectorAll('button[title="Edit transcript entry"]'),
+ ).toHaveLength(3);
+ const updatedSeek = vi.fn();
+ await render(updatedSeek);
+ await click(cue(2));
+ expect(updatedSeek).toHaveBeenCalledWith(3);
+ });
+
+ it("starts with the new cue's text when switching editors", async () => {
+ mocks.content = transcript(3);
+ await render();
+ await editCue(1);
+ await type("Draft for cue one");
+ await editCue(2);
+ expect(container.querySelector("textarea")?.value).toMatch(/^Cue 2:/);
+ await click(button("Cancel"));
+ expect(cue(1).textContent).toContain("We reviewed the recording");
+ expect(mocks.edit).not.toHaveBeenCalled();
+ });
+
+ it("keeps save and cancel disabled until a pending save completes", async () => {
+ mocks.content = transcript(3);
+ let finish!: (result: { success: boolean }) => void;
+ mocks.edit.mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ finish = resolve;
+ }),
+ );
+ await render();
+ await editCue(2);
+ await type("Saved after waiting");
+ await click(button("Save"));
+ expect(button("Save").disabled).toBe(true);
+ expect(button("Cancel").disabled).toBe(true);
+ await editCue(3);
+ expect(container.querySelector("textarea")?.value).toMatch(/^Cue 3:/);
+ expect(button("Save").disabled).toBe(true);
+ expect(button("Cancel").disabled).toBe(true);
+ await act(async () => finish({ success: true }));
+ expect(container.querySelector("textarea")).toBeNull();
+ expect(container.textContent).toContain("Saved after waiting");
+ });
+
+ it("keeps an empty transcript empty", async () => {
+ mocks.content = "WEBVTT\n\n";
+ await render();
+ expect(
+ container.querySelector('button[title="Edit transcript entry"]'),
+ ).toBeNull();
+ expect(container.querySelector("textarea")).toBeNull();
+ expect(mocks.rowRenders).toBe(0);
+ });
+
+ it.skipIf(!process.env.CAP_TRANSCRIPT_BENCH)(
+ "profiles selection and typing with 1200 three-second cues",
+ async () => {
+ await render();
+ const median = (values: number[]) =>
+ [...values].sort((a, b) => a - b)[Math.floor(values.length / 2)];
+ const selection: number[] = [];
+ const typing: number[] = [];
+ const selectionElapsed: number[] = [];
+ const typingElapsed: number[] = [];
+ for (let i = 0; i < 40; i++) {
+ durations = [];
+ const target = cue(i % 2 ? 300 : 600);
+ const start = performance.now();
+ await click(target);
+ if (i >= 10) {
+ selectionElapsed.push(performance.now() - start);
+ selection.push(...durations);
+ }
+ }
+ await editCue(300);
+ mocks.rowRenders = 0;
+ for (let i = 0; i < 40; i++) {
+ durations = [];
+ const start = performance.now();
+ await type(`Updated cue ${i}`);
+ if (i >= 10) {
+ typingElapsed.push(performance.now() - start);
+ typing.push(...durations);
+ }
+ }
+ expect(selection).toHaveLength(30);
+ expect(typing).toHaveLength(30);
+ console.log(
+ JSON.stringify({
+ cues: 1200,
+ samples: 30,
+ selectionMedianMs: median(selection),
+ typingMedianMs: median(typing),
+ selectionElapsedMedianMs: median(selectionElapsed),
+ typingElapsedMedianMs: median(typingElapsed),
+ typingRowRenders: mocks.rowRenders,
+ }),
+ );
+ },
+ 60000,
+ );
+});
diff --git a/apps/web/__tests__/unit/share-video-quota-loading.test.ts b/apps/web/__tests__/unit/share-video-quota-loading.test.ts
new file mode 100644
index 000000000..f7d38c13e
--- /dev/null
+++ b/apps/web/__tests__/unit/share-video-quota-loading.test.ts
@@ -0,0 +1,120 @@
+import { type ComponentProps, createElement } from "react";
+import { renderToReadableStream, renderToStaticMarkup } from "react-dom/server";
+import { describe, expect, it, vi } from "vitest";
+import { ShareableLinkLimitOverlay } from "@/app/s/[videoId]/_components/ShareableLinkLimitOverlay";
+import { ShareVideo } from "@/app/s/[videoId]/_components/ShareVideo";
+
+vi.mock("@cap/env", () => ({
+ NODE_ENV: "production",
+ buildEnv: { NEXT_PUBLIC_WEB_URL: "https://cap.so" },
+}));
+
+vi.mock("@cap/ui", async () => {
+ const { createElement } = await import("react");
+ return {
+ Logo: () => createElement("span", null, "Cap"),
+ Button: ({ href, children }: ComponentProps<"a">) =>
+ href
+ ? createElement("a", { href }, children)
+ : createElement("button", { type: "button" }, children),
+ };
+});
+
+vi.mock("next/navigation", () => ({
+ useRouter: () => ({ refresh: vi.fn() }),
+}));
+
+vi.mock("next/dynamic", () =>
+ vi.importActual<typeof import("next/dynamic")>(
+ "next/dist/esm/shared/lib/app-dynamic",
+ ),
+);
+
+vi.mock("hooks/use-transcript", () => ({
+ useTranscript: () => ({ data: null, error: null }),
+}));
+
+vi.mock("hooks/use-live-transcript", () => ({
+ useLiveTranscript: () => ({ data: null }),
+}));
+
+vi.mock("@/actions/video/finalize-desktop-segments", () => ({
+ finalizeDesktopSegmentsRecording: vi.fn(),
+}));
+
+vi.mock("@/app/s/[videoId]/_components/CaptionContext", () => ({
+ useCaptionContext: () => ({ selectedLanguage: "off" }),
+}));
+
+vi.mock("@/app/s/[videoId]/_components/CapVideoPlayer", () => ({
+ CapVideoPlayer: () => createElement("video", { "data-player": "mp4" }),
+}));
+
+vi.mock("@/app/s/[videoId]/_components/HLSVideoPlayer", () => ({
+ HLSVideoPlayer: () => createElement("video", { "data-player": "hls" }),
+}));
+
+vi.mock("@/app/s/[videoId]/_components/RecordingInProgress", () => ({
+ PreparingVideoOverlay: () => createElement("div", null, "Preparing video"),
+ RecordingInProgressOverlay: () => null,
+}));
+
+type ShareVideoProps = ComponentProps<typeof ShareVideo>;
+
+function props(
+ overrides: Partial<ShareVideoProps["data"]> = {},
+): ShareVideoProps {
+ return {
+ data: {
+ id: "video-id",
+ owner: { id: "owner-id", isPro: true },
+ duration: 60,
+ metadata: null,
+ transcriptionStatus: null,
+ source: { type: "desktopMP4" },
+ ...overrides,
+ } as ShareVideoProps["data"],
+ comments: [],
+ isEditProcessing: false,
+ };
+}
+
+async function render(props: ShareVideoProps) {
+ const stream = await renderToReadableStream(createElement(ShareVideo, props));
+ await stream.allReady;
+ return new Response(stream).text();
+}
+
+describe("share video quota loading", () => {
+ it.each([false, true])(
+ "preserves the quota HTML for owner=%s",
+ async (isOwner) => {
+ const html = await render({
+ ...props({ ownerIsOverShareLimit: true }),
+ viewerIsOwner: isOwner,
+ });
+ const overlay = renderToStaticMarkup(
+ createElement(ShareableLinkLimitOverlay, {
+ isOwner,
+ onUpgrade: vi.fn(),
+ className: "h-full",
+ }),
+ );
+ expect(html).toContain(overlay);
+ expect(html).not.toContain("<video");
+ },
+ );
+
+ it.each([
+ ["desktopMP4", "mp4"],
+ ["MediaConvert", "hls"],
+ ] as const)(
+ "keeps %s playback available below the limit",
+ async (type, player) => {
+ const html = await render(props({ source: { type } }));
+ expect(html).toContain(`data-player="${player}"`);
+ expect(html).not.toContain("free limit");
+ expect(html).not.toContain("Upgrade to Cap Pro");
+ },
+ );
+});
diff --git a/apps/web/__tests__/unit/summary-chapters.test.ts b/apps/web/__tests__/unit/summary-chapters.test.ts
new file mode 100644
index 000000000..2450941b2
--- /dev/null
+++ b/apps/web/__tests__/unit/summary-chapters.test.ts
@@ -0,0 +1,123 @@
+// @vitest-environment jsdom
+
+import React, { act } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("@cap/ui", () => ({
+ Button: ({ children, ...props }: React.ComponentProps<"button">) =>
+ React.createElement("button", { type: "button", ...props }, children),
+}));
+vi.mock("@fortawesome/react-fontawesome", () => ({
+ FontAwesomeIcon: () => null,
+}));
+vi.mock("@fortawesome/free-solid-svg-icons", () => ({
+ faRectangleList: {},
+ faRotateRight: {},
+}));
+vi.mock("lucide-react", () => ({ Edit3: () => null }));
+vi.mock("react-markdown", () => ({
+ default: ({ children }: { children?: string }) =>
+ React.createElement("div", null, children),
+}));
+vi.mock("@/app/s/[videoId]/_components/tabs/SummaryEditor", () => ({
+ SummaryEditor: () => null,
+}));
+
+import SummaryChapters from "@/app/s/[videoId]/_components/SummaryChapters";
+import { Summary } from "@/app/s/[videoId]/_components/tabs/Summary";
+
+type Chapter = { title: string; start: number };
+
+let container: HTMLDivElement;
+let root: Root;
+
+beforeEach(() => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ root = createRoot(container);
+});
+
+afterEach(() => {
+ act(() => root.unmount());
+ container.remove();
+ vi.unstubAllGlobals();
+});
+
+const chapterViews: [string, (chapters: Chapter[]) => React.ReactElement][] = [
+ [
+ "Summary tab",
+ (chapters) =>
+ React.createElement(Summary, {
+ videoId: "vid" as never,
+ ownerIsPro: true,
+ initialAiData: {
+ summary: "A summary",
+ chapters,
+ aiGenerationStatus: "COMPLETE",
+ },
+ }),
+ ],
+ [
+ "SummaryChapters",
+ (chapters) =>
+ React.createElement(SummaryChapters, {
+ isSummaryDisabled: false,
+ areChaptersDisabled: false,
+ handleSeek: () => {},
+ aiData: {
+ title: null,
+ summary: "A summary",
+ chapters,
+ aiGenerationStatus: "COMPLETE",
+ },
+ aiLoading: false,
+ }),
+ ],
+];
+
+describe.each(chapterViews)("%s chapter timestamps", (_name, view) => {
+ const renderChapters = (chapters: Chapter[]) => {
+ act(() => {
+ root.render(view(chapters));
+ });
+ return Array.from(container.querySelectorAll("button"))
+ .map((button) => button.textContent ?? "")
+ .filter((text) => /\d:\d\d/.test(text));
+ };
+
+ it("rolls over into hours for chapters past the hour mark", () => {
+ const rendered = renderChapters([
+ { title: "Intro", start: 0 },
+ { title: "Middle", start: 95 },
+ { title: "Just under", start: 3599 },
+ { title: "On the hour", start: 3600 },
+ { title: "Past the hour", start: 3905 },
+ { title: "Two hours in", start: 7384 },
+ ]);
+
+ expect(rendered).toEqual([
+ "00:00Intro",
+ "01:35Middle",
+ "59:59Just under",
+ "1:00:00On the hour",
+ "1:05:05Past the hour",
+ "2:03:04Two hours in",
+ ]);
+ });
+
+ it("never renders a minute count above 59", () => {
+ const rendered = renderChapters([
+ { title: "a", start: 3600 },
+ { title: "b", start: 5400 },
+ { title: "c", start: 36000 },
+ ]);
+
+ for (const text of rendered) {
+ const minutes = Number(text.split(":")[0]);
+ expect(minutes).toBeLessThanOrEqual(59);
+ }
+ expect(rendered).toEqual(["1:00:00a", "1:30:00b", "10:00:00c"]);
+ });
+});
diff --git a/apps/web/__tests__/unit/video-analytics-bulk.test.ts b/apps/web/__tests__/unit/video-analytics-bulk.test.ts
index 4d68e8d13..c03f42e37 100644
--- a/apps/web/__tests__/unit/video-analytics-bulk.test.ts
+++ b/apps/web/__tests__/unit/video-analytics-bulk.test.ts
@@ -1,4 +1,10 @@
-import { CurrentUser, Organisation, User, Video } from "@cap/web-domain";
+import {
+ CurrentUser,
+ Organisation,
+ Policy,
+ User,
+ Video,
+} from "@cap/web-domain";
import { Effect, Exit, Option } from "effect";
import { beforeEach, describe, expect, it, vi } from "vitest";
@@ -55,18 +61,24 @@ 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 check = vi.fn(
+ (
+ video: { id: string },
+ _password: Option.Option<string>,
+ ): Effect.Effect<
+ void,
+ Policy.PolicyDeniedError | Video.VerifyVideoPasswordError
+ > =>
+ 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"),
@@ -85,6 +97,10 @@ function run(ids: string[]) {
VideosPolicy,
VideosPolicy.make({
canViewLoaded: check,
+ canViewLoadedBulk: (loaded) =>
+ Effect.forEach(loaded, ([video, password]) =>
+ check(video, password).pipe(Effect.exit),
+ ),
canView: vi.fn(),
isOwner: vi.fn(),
isOwnerLoaded: vi.fn(),
diff --git a/apps/web/__tests__/unit/video-create-atomicity.test.ts b/apps/web/__tests__/unit/video-create-atomicity.test.ts
index 69f0aa87f..ec5e08797 100644
--- a/apps/web/__tests__/unit/video-create-atomicity.test.ts
+++ b/apps/web/__tests__/unit/video-create-atomicity.test.ts
@@ -1,4 +1,4 @@
-import { Organisation, User, Video } from "@cap/web-domain";
+import { Folder, Organisation, User, Video } from "@cap/web-domain";
import { Effect, Option } from "effect";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
@@ -7,15 +7,18 @@ import {
} from "../../../../packages/web-backend/src/Videos/VideosRepo";
type Row = Record<string, unknown>;
+type Predicate = (row: Row) => boolean;
type Store = {
videos: Map<string, Row>;
uploads: Map<string, Row>;
+ folders: Map<string, Row>;
};
const mocks = vi.hoisted(() => ({
store: {
videos: new Map<string, Row>(),
uploads: new Map<string, Row>(),
+ folders: new Map<string, Row>(),
} as Store,
rejectUpload: false,
transactionCalls: 0,
@@ -27,11 +30,13 @@ const mocks = vi.hoisted(() => ({
}));
vi.mock("@cap/database", () => {
- type Table = { name: "video" | "upload" };
+ type Table = { name: "video" | "upload" | "folder" };
type Client = {
insert: (table: Table) => { values: (row: Row | Row[]) => Promise<void> };
select: () => {
- from: (table: Table) => { where: () => Promise<Row[]> };
+ from: (table: Table) => {
+ where: (predicate: Predicate) => Promise<Row[]>;
+ };
};
transaction: (run: (tx: Client) => Promise<unknown>) => Promise<unknown>;
};
@@ -51,8 +56,10 @@ vi.mock("@cap/database", () => {
}),
select: () => ({
from: (table) => ({
- where: async () =>
- table.name === "video" ? [...store.videos.values()] : [],
+ where: async (predicate) => {
+ const rows = table.name === "folder" ? store.folders : store.videos;
+ return [...rows.values()].filter(predicate);
+ },
}),
}),
transaction: async (run) => {
@@ -60,6 +67,7 @@ vi.mock("@cap/database", () => {
const candidate = {
videos: new Map(store.videos),
uploads: new Map(store.uploads),
+ folders: new Map(store.folders),
};
const result = await run(makeClient(candidate));
mocks.store = candidate;
@@ -77,6 +85,13 @@ vi.mock("@cap/database/auth/session", () => ({
vi.mock("@cap/database/helpers", () => ({ nanoId: () => "fixture-video" }));
vi.mock("@cap/database/schema", () => ({
videos: { name: "video", id: "id" },
+ folders: {
+ name: "folder",
+ id: "id",
+ organizationId: "organizationId",
+ createdById: "createdById",
+ spaceId: "spaceId",
+ },
videoUploads: { name: "upload", videoId: "videoId" },
}));
// The real lookup reads `organizations`, which this transaction mock does not
@@ -102,7 +117,18 @@ vi.mock("@/lib/server", async () => ({
}));
vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath }));
vi.mock("drizzle-orm", () => ({
- eq: (_field: unknown, value: unknown) => value,
+ eq:
+ (field: string, value: unknown): Predicate =>
+ (row) =>
+ row[field] === value,
+ and:
+ (...predicates: Predicate[]): Predicate =>
+ (row) =>
+ predicates.every((predicate) => predicate(row)),
+ isNull:
+ (field: string): Predicate =>
+ (row) =>
+ row[field] === null,
}));
import { createVideoForServerProcessing } from "@/actions/video/create-for-processing";
@@ -145,7 +171,7 @@ function createRepoVideo(initialUpload?: {
}
beforeEach(() => {
- mocks.store = { videos: new Map(), uploads: new Map() };
+ mocks.store = { videos: new Map(), uploads: new Map(), folders: new Map() };
mocks.rejectUpload = false;
mocks.transactionCalls = 0;
mocks.currentUser.mockResolvedValue({ id: "fixture-user" });
@@ -288,3 +314,82 @@ describe("VideosRepo.create", () => {
expect(mocks.store.uploads.size).toBe(0);
});
});
+
+describe.each([
+ {
+ name: "video file import",
+ create: (folderId?: Folder.FolderId) =>
+ createVideoForServerProcessing({ orgId, folderId }),
+ },
+ {
+ name: "image file import",
+ create: (folderId?: Folder.FolderId) =>
+ createVideoAndGetUploadUrl({
+ orgId,
+ folderId,
+ isUpload: true,
+ isScreenshot: true,
+ }),
+ },
+])("$name destination", ({ create }) => {
+ const folderId = Folder.FolderId.make("fixture-folder");
+ const personalFolder = {
+ id: folderId,
+ organizationId: orgId,
+ createdById: "fixture-user",
+ spaceId: null,
+ };
+
+ it.each([
+ { name: "missing folder", folder: undefined },
+ {
+ name: "a different folder",
+ folder: { ...personalFolder, id: "different-folder" },
+ },
+ {
+ name: "another user's personal folder",
+ folder: { ...personalFolder, createdById: "other-user" },
+ },
+ {
+ name: "own folder in another organization",
+ folder: { ...personalFolder, organizationId: "other-org" },
+ },
+ {
+ name: "space folder",
+ folder: { ...personalFolder, spaceId: "fixture-space" },
+ },
+ {
+ name: "organization folder",
+ folder: { ...personalFolder, spaceId: orgId },
+ },
+ ])(
+ "rejects $name before signing or creating an upload",
+ async ({ folder }) => {
+ if (folder) mocks.store.folders.set(folderId, folder);
+
+ await expect(create(folderId)).rejects.toThrow("Folder not found");
+
+ expect(mocks.createUploadTargetForUser).not.toHaveBeenCalled();
+ expect(mocks.store.videos.size).toBe(0);
+ expect(mocks.store.uploads.size).toBe(0);
+ expect(mocks.transactionCalls).toBe(0);
+ expect(mocks.revalidatePath).not.toHaveBeenCalled();
+ },
+ );
+
+ it("accepts an owned personal folder in the selected organization", async () => {
+ mocks.store.folders.set(folderId, personalFolder);
+
+ const result = await create(folderId);
+
+ expect(mocks.store.videos.get(result.id)?.folderId).toBe(folderId);
+ expect(mocks.createUploadTargetForUser).toHaveBeenCalledOnce();
+ });
+
+ it("accepts an import without a folder", async () => {
+ const result = await create();
+
+ expect(mocks.store.videos.get(result.id)?.folderId).toBeUndefined();
+ expect(mocks.createUploadTargetForUser).toHaveBeenCalledOnce();
+ });
+});
diff --git a/apps/web/__tests__/unit/video-download-permissions.test.ts b/apps/web/__tests__/unit/video-download-permissions.test.ts
index 5737566ef..6534b8e2c 100644
--- a/apps/web/__tests__/unit/video-download-permissions.test.ts
+++ b/apps/web/__tests__/unit/video-download-permissions.test.ts
@@ -1,114 +1,152 @@
-import type { Organisation, User, Video } from "@cap/web-domain";
+import { User, Video } from "@cap/web-domain";
import { beforeEach, describe, expect, it, vi } from "vitest";
-const schema = {
- organizationMembers: { table: "organizationMembers" },
- sharedVideos: { table: "sharedVideos" },
- spaceMembers: { table: "spaceMembers" },
- spaceVideos: { table: "spaceVideos" },
-};
+const mocks = vi.hoisted(() => ({ query: vi.fn() }));
-vi.mock("@cap/database/schema", () => schema);
-
-// Each select() call resolves against the next queued result, and the table it
-// read is recorded so a test can assert which lookups actually happened.
-let queued: unknown[][] = [];
-const tablesRead: string[] = [];
-
-const mockDb = {
- select: () => mockDb,
- from: (table: { table: string }) => {
- tablesRead.push(table.table);
- return mockDb;
- },
- where: () => {
- const rows = queued.shift() ?? [];
- const result = Promise.resolve(rows) as Promise<unknown[]> & {
- limit: () => Promise<unknown[]>;
- };
- result.limit = () => Promise.resolve(rows);
- return result;
- },
-};
-
-vi.mock("@cap/database", () => ({ db: () => mockDb }));
+vi.mock("@cap/database", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ const client = drizzle(mocks.query);
+ return { db: () => client };
+});
-const { canUserDownloadVideo } = await import(
- "../../lib/video-download-permissions"
-);
+import { canUserDownloadVideo } from "@/lib/video-download-permissions";
-const OWNER = "user-owner" as User.UserId;
-const OTHER = "user-other" as User.UserId;
-const VIDEO = "video-1" as Video.VideoId;
-const VIDEO_ORG = "org-owning-the-video" as Organisation.OrganisationId;
+const OWNER = User.UserId.make("user-owner");
+const OTHER = User.UserId.make("user-other");
+const VIDEO = Video.VideoId.make("video-1");
+let organizationShared: boolean;
+let organizationMember: boolean;
+let spaceShared: boolean;
+let spaceMember: boolean;
-function call(userId: User.UserId) {
+function call(userId = OTHER) {
return canUserDownloadVideo({ userId, ownerId: OWNER, videoId: VIDEO });
}
-describe("canUserDownloadVideo", () => {
- beforeEach(() => {
- queued = [];
- tablesRead.length = 0;
+beforeEach(() => {
+ organizationShared = false;
+ organizationMember = false;
+ spaceShared = false;
+ spaceMember = false;
+ mocks.query.mockImplementation(async (query: string, params: unknown[]) => {
+ if (query.includes("inner join")) {
+ const organization = query.includes("`organization_members`");
+ const membershipTable = organization
+ ? "organization_members"
+ : "space_members";
+ const shareTable = organization ? "shared_videos" : "space_videos";
+ const key = organization ? "organizationId" : "spaceId";
+ expect(query).toContain(
+ `\`${shareTable}\`.\`${key}\` = \`${membershipTable}\`.\`${key}\``,
+ );
+ expect(query).toContain(`\`${membershipTable}\`.\`userId\` = ?`);
+ expect(query).toContain(`\`${shareTable}\`.\`videoId\` = ?`);
+ expect(query).toContain("limit ?");
+ expect(params).toEqual([OTHER, VIDEO, 1]);
+ const allowed = organization
+ ? organizationShared && organizationMember
+ : spaceShared && spaceMember;
+ return { rows: allowed ? [["membership-1"]] : [] };
+ }
+ if (query.includes("from `shared_videos`")) {
+ return { rows: organizationShared ? [["org-1"]] : [] };
+ }
+ if (query.includes("from `organization_members`")) {
+ return { rows: organizationMember ? [["membership-1"]] : [] };
+ }
+ if (query.includes("from `space_videos`")) {
+ return { rows: spaceShared ? [["space-1"]] : [] };
+ }
+ if (query.includes("from `space_members`")) {
+ return { rows: spaceMember ? [["membership-1"]] : [] };
+ }
+ throw new Error(`Unexpected query: ${query}`);
});
+});
+describe("canUserDownloadVideo", () => {
it("allows the owner without querying shares", async () => {
expect(await call(OWNER)).toBe(true);
- expect(tablesRead).toEqual([]);
+ expect(mocks.query).not.toHaveBeenCalled();
});
- // The video's own orgId must not grant download access: VideosPolicy.canView
- // requires an explicit sharedVideos row, and no creation path writes one, so
- // trusting orgId let org colleagues download videos they cannot open.
- it("denies an org colleague when the video was never explicitly shared", async () => {
- queued = [
- [], // sharedVideos: no explicit org share
- [], // spaceVideos: no space share
- ];
-
- expect(await call(OTHER)).toBe(false);
- expect(tablesRead).toContain("sharedVideos");
- expect(tablesRead).not.toContain("organizationMembers");
+ it.each([
+ [false, false, false, false],
+ [false, false, false, true],
+ [false, false, true, false],
+ [false, false, true, true],
+ [false, true, false, false],
+ [false, true, false, true],
+ [false, true, true, false],
+ [false, true, true, true],
+ [true, false, false, false],
+ [true, false, false, true],
+ [true, false, true, false],
+ [true, false, true, true],
+ [true, true, false, false],
+ [true, true, false, true],
+ [true, true, true, false],
+ [true, true, true, true],
+ ])(
+ "requires an explicit share and membership: org %s/%s, space %s/%s",
+ async (orgShared, orgMember, sharedSpace, memberOfSpace) => {
+ organizationShared = orgShared;
+ organizationMember = orgMember;
+ spaceShared = sharedSpace;
+ spaceMember = memberOfSpace;
+ expect(await call()).toBe(
+ (orgShared && orgMember) || (sharedSpace && memberOfSpace),
+ );
+ },
+ );
+
+ it("checks both memberships in one round trip without loading share lists", async () => {
+ const pending: Array<() => void> = [];
+ mocks.query.mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ pending.push(() => resolve({ rows: [] }));
+ }),
+ );
+ const result = call();
+ await vi.waitFor(() => expect(pending).toHaveLength(2));
+ for (const release of pending) release();
+ expect(await result).toBe(false);
+ expect(mocks.query).toHaveBeenCalledTimes(2);
+ expect(
+ mocks.query.mock.calls.every(([query]) => query.includes("inner join")),
+ ).toBe(true);
});
- it("allows a member of an org the video was explicitly shared with", async () => {
- queued = [
- [{ organizationId: VIDEO_ORG }], // sharedVideos
- [{ id: "membership-1" }], // organizationMembers
- ];
-
- expect(await call(OTHER)).toBe(true);
- expect(tablesRead).toEqual(["sharedVideos", "organizationMembers"]);
+ it("allows an org member even when the prefetched space lookup fails", async () => {
+ mocks.query.mockImplementation(async (query: string) => {
+ if (query.includes("`space_videos`"))
+ throw new Error("Space lookup failed");
+ return { rows: [["organization-member"]] };
+ });
+ expect(await call()).toBe(true);
});
- it("denies a non-member even when the video is shared with some org", async () => {
- queued = [
- [{ organizationId: VIDEO_ORG }], // sharedVideos
- [], // organizationMembers: not a member
- [], // spaceVideos
- ];
-
- expect(await call(OTHER)).toBe(false);
+ it("propagates an org lookup failure even when the space lookup succeeds", async () => {
+ const failure = new Error("Organization lookup failed");
+ mocks.query.mockImplementation(async (query: string) => {
+ if (query.includes("`shared_videos`")) throw failure;
+ return { rows: [["space-member"]] };
+ });
+ await expect(call()).rejects.toMatchObject({ cause: failure });
});
- it("allows a member of a space the video was shared into", async () => {
- queued = [
- [], // sharedVideos
- [{ spaceId: "space-1" }], // spaceVideos
- [{ id: "space-membership-1" }], // spaceMembers
- ];
-
- expect(await call(OTHER)).toBe(true);
- expect(tablesRead).toContain("spaceMembers");
+ it("propagates a space lookup failure when there is no org membership", async () => {
+ const failure = new Error("Space lookup failed");
+ mocks.query.mockImplementation(async (query: string) => {
+ if (query.includes("`space_videos`")) throw failure;
+ return { rows: [] };
+ });
+ await expect(call()).rejects.toMatchObject({ cause: failure });
});
- it("denies a non-member of the space the video was shared into", async () => {
- queued = [
- [], // sharedVideos
- [{ spaceId: "space-1" }], // spaceVideos
- [], // spaceMembers
- ];
-
- expect(await call(OTHER)).toBe(false);
+ it("propagates database failures", async () => {
+ mocks.query.mockRejectedValue(new Error("Database unavailable"));
+ await expect(call()).rejects.toThrow();
});
});
diff --git a/apps/web/actions/folders/require-personal-folder.ts b/apps/web/actions/folders/require-personal-folder.ts
new file mode 100644
index 000000000..6fb7ad1f5
--- /dev/null
+++ b/apps/web/actions/folders/require-personal-folder.ts
@@ -0,0 +1,23 @@
+import { db } from "@cap/database";
+import { folders } from "@cap/database/schema";
+import type { Folder, Organisation, User } from "@cap/web-domain";
+import { and, eq, isNull } from "drizzle-orm";
+
+export async function requirePersonalFolder(
+ userId: User.UserId,
+ organizationId: Organisation.OrganisationId,
+ folderId: Folder.FolderId,
+) {
+ const [folder] = await db()
+ .select({ id: folders.id })
+ .from(folders)
+ .where(
+ and(
+ eq(folders.id, folderId),
+ eq(folders.organizationId, organizationId),
+ eq(folders.createdById, userId),
+ isNull(folders.spaceId),
+ ),
+ );
+ if (!folder) throw new Error("Folder not found");
+}
diff --git a/apps/web/actions/video/create-for-processing.ts b/apps/web/actions/video/create-for-processing.ts
index fde4b44b7..b39fce716 100644
--- a/apps/web/actions/video/create-for-processing.ts
+++ b/apps/web/actions/video/create-for-processing.ts
@@ -15,6 +15,7 @@ import {
} from "@cap/web-domain";
import { Option } from "effect";
import { revalidatePath } from "next/cache";
+import { requirePersonalFolder } from "@/actions/folders/require-personal-folder";
import { requireOrganizationAccess } from "@/actions/organization/authorization";
import { runPromise } from "@/lib/server";
@@ -61,6 +62,7 @@ export async function createVideoForServerProcessing({
}
await requireOrganizationAccess(user.id, orgId);
+ if (folderId) await requirePersonalFolder(user.id, orgId, folderId);
const videoId = Video.VideoId.make(nanoId());
diff --git a/apps/web/actions/video/upload.ts b/apps/web/actions/video/upload.ts
index ca88c6ac4..0907b19df 100644
--- a/apps/web/actions/video/upload.ts
+++ b/apps/web/actions/video/upload.ts
@@ -16,6 +16,7 @@ import {
import { eq } from "drizzle-orm";
import { Effect, Option } from "effect";
import { revalidatePath } from "next/cache";
+import { requirePersonalFolder } from "@/actions/folders/require-personal-folder";
import { requireOrganizationAccess } from "@/actions/organization/authorization";
import { runPromise } from "@/lib/server";
@@ -196,6 +197,7 @@ export async function createVideoAndGetUploadUrl({
}
const idToUse = Video.VideoId.make(videoId || nanoId());
+ if (folderId) await requirePersonalFolder(user.id, orgId, folderId);
const videoTitle = `Cap ${
isScreenshot ? "Screenshot" : isUpload ? "Upload" : "Recording"
} - ${formattedDate}`;
diff --git a/apps/web/app/api/analytics/track/route.ts b/apps/web/app/api/analytics/track/route.ts
index 01746e860..d6aa452a9 100644
--- a/apps/web/app/api/analytics/track/route.ts
+++ b/apps/web/app/api/analytics/track/route.ts
@@ -17,16 +17,22 @@ import { runPromise } from "@/lib/server";
interface TrackPayload {
videoId: string;
- orgId?: string | null;
- ownerId?: string | null;
sessionId?: string;
- pathname?: string;
- hostname?: string | null;
userAgent?: string;
occurredAt?: string;
}
const VIEW_TRACKING_DELAY_MS = 2 * 60 * 1000;
+const MAX_CLIENT_CLOCK_SKEW_MS = 5 * 60 * 1000;
+
+const viewTimestamp = (occurredAt: unknown, now: number) => {
+ const time =
+ typeof occurredAt === "string" ? Date.parse(occurredAt) : Number.NaN;
+ return Number.isFinite(time) &&
+ Math.abs(time - now) <= MAX_CLIENT_CLOCK_SKEW_MS
+ ? new Date(time)
+ : new Date(now);
+};
const sanitizeString = (value?: string | null) => {
const trimmed = value?.trim();
@@ -69,7 +75,7 @@ export async function POST(request: NextRequest) {
const osName = parser.getOS().name ?? "unknown";
const deviceType = parser.getDevice().type ?? "desktop";
- const timestamp = body.occurredAt ? new Date(body.occurredAt) : new Date();
+ const timestamp = viewTimestamp(body.occurredAt, Date.now());
const country =
sanitizeString(request.headers.get("x-vercel-ip-country")) || "";
@@ -80,13 +86,6 @@ export async function POST(request: NextRequest) {
decodeUrlEncodedHeaderValue(request.headers.get("x-vercel-ip-city")),
) || "";
- const hostname =
- sanitizeString(body.hostname) ||
- sanitizeString(request.nextUrl.hostname) ||
- "";
-
- const pathname = body.pathname ?? `/s/${body.videoId}`;
-
await runPromise(
Effect.gen(function* () {
const maybeUser = yield* Effect.serviceOption(CurrentUser);
@@ -108,6 +107,7 @@ export async function POST(request: NextRequest) {
const [videoRecord] = yield* Effect.tryPromise(() =>
db()
.select({
+ orgId: videos.orgId,
ownerId: videos.ownerId,
firstViewEmailSentAt: videos.firstViewEmailSentAt,
videoName: videos.name,
@@ -123,6 +123,7 @@ export async function POST(request: NextRequest) {
Effect.orElseSucceed(
() =>
[] as {
+ orgId: string;
ownerId: string;
firstViewEmailSentAt: Date | null;
videoName: string;
@@ -133,24 +134,17 @@ export async function POST(request: NextRequest) {
),
);
- if (videoRecord && userId === videoRecord.ownerId) {
+ if (!videoRecord || userId === videoRecord.ownerId) {
return;
}
if (
- videoRecord &&
- (videoRecord.activeUploadVideoId ||
- Date.now() - videoRecord.updatedAt.getTime() < VIEW_TRACKING_DELAY_MS)
+ videoRecord.activeUploadVideoId ||
+ Date.now() - videoRecord.updatedAt.getTime() < VIEW_TRACKING_DELAY_MS
) {
return;
}
- const tenantId =
- body.orgId ||
- videoRecord?.ownerId ||
- body.ownerId ||
- (hostname ? `domain:${hostname}` : "public");
-
const tinybird = yield* Tinybird;
yield* tinybird.appendEvents([
{
@@ -158,9 +152,9 @@ export async function POST(request: NextRequest) {
session_id: sessionId ?? "anon",
action: "page_hit",
version: "1.0",
- tenant_id: tenantId,
+ tenant_id: videoRecord.orgId || videoRecord.ownerId,
video_id: body.videoId,
- pathname,
+ pathname: `/s/${body.videoId}`,
country,
region,
city,
@@ -171,8 +165,7 @@ export async function POST(request: NextRequest) {
},
]);
- const isNewVideo =
- videoRecord && videoRecord.createdAt >= ANON_NOTIF_CUTOFF;
+ const isNewVideo = videoRecord.createdAt >= ANON_NOTIF_CUTOFF;
const shouldSendFirstViewEmail =
isNewVideo && !videoRecord.firstViewEmailSentAt;
diff --git a/apps/web/app/api/mobile/[...route]/route.ts b/apps/web/app/api/mobile/[...route]/route.ts
index b1b3d2562..45df2ef4c 100644
--- a/apps/web/app/api/mobile/[...route]/route.ts
+++ b/apps/web/app/api/mobile/[...route]/route.ts
@@ -1403,31 +1403,40 @@ const getCapLocations = Effect.fn("Mobile.getCapLocations")(function* ({
isNull(Db.organizations.tombstoneAt),
);
const whereClause = and(collectionWhereClause, folderFilter);
- const [locations, [countRow]] = yield* Effect.all([
- database.use((db) =>
- db
- .select({ id: Db.videos.id, folderId: Db.videos.folderId })
- .from(Db.videos)
- .leftJoin(Db.organizations, eq(Db.videos.orgId, Db.organizations.id))
- .where(whereClause)
- .orderBy(desc(Db.videos.effectiveCreatedAt))
- .limit(limit)
- .offset(offset),
- ),
- database.use((db) =>
- db
- .select({
- total:
- sql<number>`COUNT(CASE WHEN ${folderFilter} THEN 1 END)`.mapWith(
- Number,
- ),
- collectionTotal: count(),
- })
- .from(Db.videos)
- .leftJoin(Db.organizations, eq(Db.videos.orgId, Db.organizations.id))
- .where(collectionWhereClause),
- ),
- ]);
+ const [locations, [countRow]] = yield* Effect.all(
+ [
+ database.use((db) =>
+ db
+ .select({ id: Db.videos.id, folderId: Db.videos.folderId })
+ .from(Db.videos)
+ .leftJoin(
+ Db.organizations,
+ eq(Db.videos.orgId, Db.organizations.id),
+ )
+ .where(whereClause)
+ .orderBy(desc(Db.videos.effectiveCreatedAt))
+ .limit(limit)
+ .offset(offset),
+ ),
+ database.use((db) =>
+ db
+ .select({
+ total:
+ sql<number>`COUNT(CASE WHEN ${folderFilter} THEN 1 END)`.mapWith(
+ Number,
+ ),
+ collectionTotal: count(),
+ })
+ .from(Db.videos)
+ .leftJoin(
+ Db.organizations,
+ eq(Db.videos.orgId, Db.organizations.id),
+ )
+ .where(collectionWhereClause),
+ ),
+ ],
+ { concurrency: 2 },
+ );
return {
locations,
total: countRow?.total ?? 0,
@@ -1444,15 +1453,73 @@ const getCapLocations = Effect.fn("Mobile.getCapLocations")(function* ({
isNull(Db.organizations.tombstoneAt),
);
const whereClause = and(collectionWhereClause, folderFilter);
- const [locations, [countRow]] = yield* Effect.all([
+ const [locations, [countRow]] = yield* Effect.all(
+ [
+ database.use((db) =>
+ db
+ .select({
+ id: Db.sharedVideos.videoId,
+ folderId: Db.sharedVideos.folderId,
+ })
+ .from(Db.sharedVideos)
+ .innerJoin(Db.videos, eq(Db.sharedVideos.videoId, Db.videos.id))
+ .leftJoin(
+ Db.organizations,
+ eq(Db.videos.orgId, Db.organizations.id),
+ )
+ .where(whereClause)
+ .orderBy(desc(Db.videos.effectiveCreatedAt))
+ .limit(limit)
+ .offset(offset),
+ ),
+ database.use((db) =>
+ db
+ .select({
+ total:
+ sql<number>`COUNT(DISTINCT CASE WHEN ${folderFilter} THEN ${Db.sharedVideos.videoId} END)`.mapWith(
+ Number,
+ ),
+ collectionTotal:
+ sql<number>`COUNT(DISTINCT ${Db.sharedVideos.videoId})`.mapWith(
+ Number,
+ ),
+ })
+ .from(Db.sharedVideos)
+ .innerJoin(Db.videos, eq(Db.sharedVideos.videoId, Db.videos.id))
+ .leftJoin(
+ Db.organizations,
+ eq(Db.videos.orgId, Db.organizations.id),
+ )
+ .where(collectionWhereClause),
+ ),
+ ],
+ { concurrency: 2 },
+ );
+ return {
+ locations,
+ total: countRow?.total ?? 0,
+ collectionTotal: countRow?.collectionTotal ?? 0,
+ };
+ }
+
+ const folderFilter = folderId
+ ? eq(Db.spaceVideos.folderId, folderId)
+ : isNull(Db.spaceVideos.folderId);
+ const collectionWhereClause = and(
+ eq(Db.spaceVideos.spaceId, space.id),
+ isNull(Db.organizations.tombstoneAt),
+ );
+ const whereClause = and(collectionWhereClause, folderFilter);
+ const [locations, [countRow]] = yield* Effect.all(
+ [
database.use((db) =>
db
.select({
- id: Db.sharedVideos.videoId,
- folderId: Db.sharedVideos.folderId,
+ id: Db.spaceVideos.videoId,
+ folderId: Db.spaceVideos.folderId,
})
- .from(Db.sharedVideos)
- .innerJoin(Db.videos, eq(Db.sharedVideos.videoId, Db.videos.id))
+ .from(Db.spaceVideos)
+ .innerJoin(Db.videos, eq(Db.spaceVideos.videoId, Db.videos.id))
.leftJoin(Db.organizations, eq(Db.videos.orgId, Db.organizations.id))
.where(whereClause)
.orderBy(desc(Db.videos.effectiveCreatedAt))
@@ -1463,68 +1530,22 @@ const getCapLocations = Effect.fn("Mobile.getCapLocations")(function* ({
db
.select({
total:
- sql<number>`COUNT(DISTINCT CASE WHEN ${folderFilter} THEN ${Db.sharedVideos.videoId} END)`.mapWith(
+ sql<number>`COUNT(DISTINCT CASE WHEN ${folderFilter} THEN ${Db.spaceVideos.videoId} END)`.mapWith(
Number,
),
collectionTotal:
- sql<number>`COUNT(DISTINCT ${Db.sharedVideos.videoId})`.mapWith(
+ sql<number>`COUNT(DISTINCT ${Db.spaceVideos.videoId})`.mapWith(
Number,
),
})
- .from(Db.sharedVideos)
- .innerJoin(Db.videos, eq(Db.sharedVideos.videoId, Db.videos.id))
+ .from(Db.spaceVideos)
+ .innerJoin(Db.videos, eq(Db.spaceVideos.videoId, Db.videos.id))
.leftJoin(Db.organizations, eq(Db.videos.orgId, Db.organizations.id))
.where(collectionWhereClause),
),
- ]);
- return {
- locations,
- total: countRow?.total ?? 0,
- collectionTotal: countRow?.collectionTotal ?? 0,
- };
- }
-
- const folderFilter = folderId
- ? eq(Db.spaceVideos.folderId, folderId)
- : isNull(Db.spaceVideos.folderId);
- const collectionWhereClause = and(
- eq(Db.spaceVideos.spaceId, space.id),
- isNull(Db.organizations.tombstoneAt),
+ ],
+ { concurrency: 2 },
);
- const whereClause = and(collectionWhereClause, folderFilter);
- const [locations, [countRow]] = yield* Effect.all([
- database.use((db) =>
- db
- .select({
- id: Db.spaceVideos.videoId,
- folderId: Db.spaceVideos.folderId,
- })
- .from(Db.spaceVideos)
- .innerJoin(Db.videos, eq(Db.spaceVideos.videoId, Db.videos.id))
- .leftJoin(Db.organizations, eq(Db.videos.orgId, Db.organizations.id))
- .where(whereClause)
- .orderBy(desc(Db.videos.effectiveCreatedAt))
- .limit(limit)
- .offset(offset),
- ),
- database.use((db) =>
- db
- .select({
- total:
- sql<number>`COUNT(DISTINCT CASE WHEN ${folderFilter} THEN ${Db.spaceVideos.videoId} END)`.mapWith(
- Number,
- ),
- collectionTotal:
- sql<number>`COUNT(DISTINCT ${Db.spaceVideos.videoId})`.mapWith(
- Number,
- ),
- })
- .from(Db.spaceVideos)
- .innerJoin(Db.videos, eq(Db.spaceVideos.videoId, Db.videos.id))
- .leftJoin(Db.organizations, eq(Db.videos.orgId, Db.organizations.id))
- .where(collectionWhereClause),
- ),
- ]);
return {
locations,
total: countRow?.total ?? 0,
@@ -1648,16 +1669,20 @@ const getCapsList = Effect.fn("Mobile.getCapsList")(function* (
const user = yield* CurrentUser;
const space = yield* getSelectedSpace(params.spaceId);
- const [{ locations, total, collectionTotal }, folders] = yield* Effect.all([
- getCapLocations({ folderId, page, limit, space }),
- folderId
- ? Effect.succeed([])
- : space
- ? getSpaceRootFolders(space)
- : getRootFolders(user.activeOrganizationId),
- ]);
+ const [{ locations, total, collectionTotal }, folders, blockedUserIds] =
+ yield* Effect.all(
+ [
+ getCapLocations({ folderId, page, limit, space }),
+ folderId
+ ? Effect.succeed([])
+ : space
+ ? getSpaceRootFolders(space)
+ : getRootFolders(user.activeOrganizationId),
+ getCurrentBlockedUserIds(),
+ ],
+ { concurrency: 3 },
+ );
const rows = yield* getCapRows(locations);
- const blockedUserIds = yield* getCurrentBlockedUserIds();
const visibleRows = rows.filter(
(row) =>
!blockedUserIds.includes(row.ownerId) &&
diff --git a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx b/apps/web/app/s/[videoId]/_components/ShareVideo.tsx
index 30ab8682f..e105fadcc 100644
--- a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx
+++ b/apps/web/app/s/[videoId]/_components/ShareVideo.tsx
@@ -28,7 +28,6 @@ import {
PreparingVideoOverlay,
RecordingInProgressOverlay,
} from "./RecordingInProgress";
-import { ShareableLinkLimitOverlay } from "./ShareableLinkLimitOverlay";
import {
isRecordingUpload,
shouldDeferPlaybackSource,
@@ -52,6 +51,11 @@ const CapVideoPlayer = dynamic(() =>
const HLSVideoPlayer = dynamic(() =>
import("./HLSVideoPlayer").then((m) => m.HLSVideoPlayer),
);
+const ShareableLinkLimitOverlay = dynamic(() =>
+ import("./ShareableLinkLimitOverlay").then(
+ (m) => m.ShareableLinkLimitOverlay,
+ ),
+);
// Both ride outside the first paint: the tracker only mounts mid-upload (its
// RPC client drags the Effect runtime along), and the upgrade modal — which
diff --git a/apps/web/app/s/[videoId]/_components/SummaryChapters.tsx b/apps/web/app/s/[videoId]/_components/SummaryChapters.tsx
index c1b8f9d68..90bba426f 100644
--- a/apps/web/app/s/[videoId]/_components/SummaryChapters.tsx
+++ b/apps/web/app/s/[videoId]/_components/SummaryChapters.tsx
@@ -1,5 +1,5 @@
import ReactMarkdown from "react-markdown";
-import { formatTimeMinutes } from "./utils/transcript-utils";
+import { formatPlayerTime } from "@/lib/player-time";
type AiGenerationStatus =
| "QUEUED"
@@ -64,7 +64,7 @@ const SummaryChapters = ({
onClick={() => handleSeek(chapter.start)}
>
<span className="w-16 text-xs text-gray-500">
- {formatTimeMinutes(chapter.start)}
+ {formatPlayerTime(chapter.start)}
</span>
<span className="ml-2 text-sm">{chapter.title}</span>
</button>
diff --git a/apps/web/app/s/[videoId]/_components/tabs/Activity/Comment.tsx b/apps/web/app/s/[videoId]/_components/tabs/Activity/Comment.tsx
index 8edf569e7..0738262b4 100644
--- a/apps/web/app/s/[videoId]/_components/tabs/Activity/Comment.tsx
+++ b/apps/web/app/s/[videoId]/_components/tabs/Activity/Comment.tsx
@@ -146,7 +146,7 @@ const CommentComponent: React.FC<{
)}
</div>
) : (
- <p className="mt-2 text-sm text-gray-11">
+ <p className="mt-2 whitespace-pre-wrap text-sm text-gray-11">
<LinkifiedText text={comment.content} />
</p>
)}
diff --git a/apps/web/app/s/[videoId]/_components/tabs/Summary.tsx b/apps/web/app/s/[videoId]/_components/tabs/Summary.tsx
index a5cd915f4..d276a9481 100644
--- a/apps/web/app/s/[videoId]/_components/tabs/Summary.tsx
+++ b/apps/web/app/s/[videoId]/_components/tabs/Summary.tsx
@@ -10,6 +10,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Edit3 } from "lucide-react";
import { useRef, useState } from "react";
import ReactMarkdown from "react-markdown";
+import { formatPlayerTime } from "@/lib/player-time";
import { type SummaryEditingState, SummaryEditor } from "./SummaryEditor";
type AiGenerationStatus =
@@ -42,14 +43,6 @@ interface SummaryProps {
onEditingStateChange?: (state: SummaryEditingState) => void;
}
-const formatTime = (time: number) => {
- const minutes = Math.floor(time / 60);
- const seconds = Math.floor(time % 60);
- return `${minutes.toString().padStart(2, "0")}:${seconds
- .toString()
- .padStart(2, "0")}`;
-};
-
const SkeletonLoader = () => (
<div className="p-4 space-y-6 animate-pulse">
<div>
@@ -318,7 +311,7 @@ export const Summary: React.FC<SummaryProps> = ({
onClick={() => handleSeek(chapter.start)}
>
<span className="w-16 text-xs text-gray-500">
- {formatTime(chapter.start)}
+ {formatPlayerTime(chapter.start)}
</span>
<span className="ml-2 text-sm">{chapter.title}</span>
</button>
diff --git a/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx b/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx
index 2915c4dd2..5bfb575bc 100644
--- a/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx
+++ b/apps/web/app/s/[videoId]/_components/tabs/Transcript.tsx
@@ -9,12 +9,11 @@ import {
ChevronDown,
Copy,
Download,
- Edit3,
Globe,
LoaderCircle,
MessageSquare,
} from "lucide-react";
-import { useEffect, useMemo, useRef, useState } from "react";
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { editTranscriptEntry } from "@/actions/videos/edit-transcript";
import {
type LanguageCode,
@@ -27,6 +26,7 @@ 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";
+import { TranscriptEditor, TranscriptRow } from "./transcript-row";
interface TranscriptProps {
data: VideoData;
@@ -49,7 +49,6 @@ export const Transcript: React.FC<TranscriptProps> = ({ data, onSeek }) => {
const [selectedEntry, setSelectedEntry] = useState<number | null>(null);
const [retryTriggered, setRetryTriggered] = useState(false);
const [editingEntry, setEditingEntry] = useState<number | null>(null);
- const [editText, setEditText] = useState<string>("");
const [isSaving, setIsSaving] = useState(false);
const [isCopying, setIsCopying] = useState(false);
const [copyPressed, setCopyPressed] = useState(false);
@@ -249,37 +248,33 @@ export const Transcript: React.FC<TranscriptProps> = ({ data, onSeek }) => {
);
}, [data.createdAt, data.transcriptionStatus, retryTriggered]);
- const handleTranscriptClick = (entry: TranscriptEntry) => {
- if (editingEntry === entry.id) {
- return;
- }
-
- setSelectedEntry(entry.id);
+ const handleTranscriptClick = useCallback(
+ (entry: TranscriptEntry) => {
+ if (editingEntry === entry.id) {
+ return;
+ }
- onSeek?.(entry.startTime);
- };
+ setSelectedEntry(entry.id);
+ onSeek?.(entry.startTime);
+ },
+ [editingEntry, onSeek],
+ );
- const startEditing = (entry: TranscriptEntry) => {
+ const startEditing = useCallback((entry: TranscriptEntry) => {
setEditingEntry(entry.id);
- setEditText(entry.text);
- };
+ }, []);
const cancelEditing = () => {
setEditingEntry(null);
- setEditText("");
};
- const saveEdit = async () => {
+ const saveEdit = async (editText: string) => {
const normalizedEditText = normalizeTranscriptCueText(editText);
if (!editingEntry || !normalizedEditText) {
return;
}
- const _originalEntry = transcriptData.find(
- (entry) => entry.id === editingEntry,
- );
-
setIsSaving(true);
try {
const result = await editTranscriptEntry(
@@ -297,7 +292,6 @@ export const Transcript: React.FC<TranscriptProps> = ({ data, onSeek }) => {
),
);
setEditingEntry(null);
- setEditText("");
invalidateTranscript(data.id);
} else {
console.error("[Transcript] Failed to save transcript edit:", {
@@ -796,87 +790,30 @@ export const Transcript: React.FC<TranscriptProps> = ({ data, onSeek }) => {
</div>
)}
<div className="px-3 py-3">
- {transcriptData.map((entry) => (
- <div
- key={entry.id}
- className={`group flex items-start gap-1 rounded-lg px-2 transition-colors ${
- editingEntry === entry.id
- ? "bg-gray-2"
- : selectedEntry === entry.id
- ? "bg-gray-3"
- : "hover:bg-gray-2"
- }`}
- >
- {editingEntry === entry.id ? (
- <div className="min-w-0 flex-1 py-2">
- <textarea
- value={editText}
- onChange={(e) => setEditText(e.target.value)}
- className="w-full resize-none rounded-lg border border-gray-4 bg-gray-1 p-2.5 text-[13px] leading-[22px] text-gray-12 outline-none transition placeholder:text-gray-8 focus:border-blue-500/60 focus:ring-2 focus:ring-blue-500/20"
- rows={Math.max(2, Math.ceil(editText.length / 60))}
- onClick={(e) => e.stopPropagation()}
- placeholder="Edit transcript text..."
- />
- <div className="mt-2 flex justify-end gap-1.5">
- <button
- type="button"
- onClick={(e) => {
- e.stopPropagation();
- cancelEditing();
- }}
- disabled={isSaving}
- className="inline-flex h-7 items-center rounded-full px-3 text-[11px] font-medium text-gray-11 transition hover:bg-gray-3 hover:text-gray-12 disabled:opacity-50"
- >
- Cancel
- </button>
- <button
- type="button"
- onClick={(e) => {
- e.stopPropagation();
- saveEdit();
- }}
- disabled={isSaving || !editText.trim()}
- className="inline-flex h-7 items-center gap-1.5 rounded-full bg-gray-12 px-3 text-[11px] font-semibold text-white transition hover:bg-gray-11 disabled:pointer-events-none disabled:opacity-50"
- >
- {isSaving && (
- <LoaderCircle className="size-3 animate-spin" />
- )}
- Save
- </button>
- </div>
- </div>
- ) : (
- <>
- <button
- className="flex min-w-0 flex-1 items-baseline gap-2.5 py-1.5 text-left"
- onClick={() => handleTranscriptClick(entry)}
- type="button"
- >
- <span className="min-w-9 shrink-0 font-mono text-[10px] leading-[22px] tabular-nums text-gray-8">
- {entry.timestamp}
- </span>
- <span className="min-w-0 flex-1 text-[13px] leading-[22px] text-gray-11">
- {entry.text}
- </span>
- </button>
- {canEdit && (
- <button
- onClick={(e) => {
- e.stopPropagation();
- startEditing(entry);
- }}
- type="button"
- aria-label="Edit transcript entry"
- title="Edit transcript entry"
- className="mt-1.5 flex size-6 shrink-0 items-center justify-center rounded-md text-gray-9 opacity-0 transition-all hover:bg-gray-4 hover:text-gray-12 focus-visible:opacity-100 group-hover:opacity-100"
- >
- <Edit3 className="size-3" />
- </button>
- )}
- </>
- )}
- </div>
- ))}
+ {transcriptData.map((entry) =>
+ editingEntry === entry.id ? (
+ <div
+ key={entry.id}
+ className="group flex items-start gap-1 rounded-lg bg-gray-2 px-2 transition-colors"
+ >
+ <TranscriptEditor
+ entry={entry}
+ isSaving={isSaving}
+ onSave={saveEdit}
+ onCancel={cancelEditing}
+ />
+ </div>
+ ) : (
+ <TranscriptRow
+ key={entry.id}
+ entry={entry}
+ selected={selectedEntry === entry.id}
+ canEdit={canEdit}
+ onSelect={handleTranscriptClick}
+ onEdit={startEditing}
+ />
+ ),
+ )}
</div>
</div>
</div>
diff --git a/apps/web/app/s/[videoId]/_components/tabs/transcript-row.tsx b/apps/web/app/s/[videoId]/_components/tabs/transcript-row.tsx
new file mode 100644
index 000000000..c0bb2d4eb
--- /dev/null
+++ b/apps/web/app/s/[videoId]/_components/tabs/transcript-row.tsx
@@ -0,0 +1,104 @@
+import { Edit3, LoaderCircle } from "lucide-react";
+import { memo, useState } from "react";
+import type { TranscriptEntry } from "../utils/parse-transcript-vtt";
+
+export const TranscriptRow = memo(function TranscriptRow({
+ entry,
+ selected,
+ canEdit,
+ onSelect,
+ onEdit,
+}: {
+ entry: TranscriptEntry;
+ selected: boolean;
+ canEdit: boolean;
+ onSelect: (entry: TranscriptEntry) => void;
+ onEdit: (entry: TranscriptEntry) => void;
+}) {
+ return (
+ <div
+ className={`group flex items-start gap-1 rounded-lg px-2 transition-colors ${
+ selected ? "bg-gray-3" : "hover:bg-gray-2"
+ }`}
+ >
+ <button
+ className="flex min-w-0 flex-1 items-baseline gap-2.5 py-1.5 text-left"
+ onClick={() => onSelect(entry)}
+ type="button"
+ >
+ <span className="min-w-9 shrink-0 font-mono text-[10px] leading-[22px] tabular-nums text-gray-8">
+ {entry.timestamp}
+ </span>
+ <span className="min-w-0 flex-1 text-[13px] leading-[22px] text-gray-11">
+ {entry.text}
+ </span>
+ </button>
+ {canEdit && (
+ <button
+ onClick={(event) => {
+ event.stopPropagation();
+ onEdit(entry);
+ }}
+ type="button"
+ aria-label="Edit transcript entry"
+ title="Edit transcript entry"
+ className="mt-1.5 flex size-6 shrink-0 items-center justify-center rounded-md text-gray-9 opacity-0 transition-all hover:bg-gray-4 hover:text-gray-12 focus-visible:opacity-100 group-hover:opacity-100"
+ >
+ <Edit3 className="size-3" />
+ </button>
+ )}
+ </div>
+ );
+});
+
+export function TranscriptEditor({
+ entry,
+ isSaving,
+ onSave,
+ onCancel,
+}: {
+ entry: TranscriptEntry;
+ isSaving: boolean;
+ onSave: (text: string) => Promise<void>;
+ onCancel: () => void;
+}) {
+ const [editText, setEditText] = useState(entry.text);
+
+ return (
+ <div className="min-w-0 flex-1 py-2">
+ <textarea
+ value={editText}
+ onChange={(e) => setEditText(e.target.value)}
+ className="w-full resize-none rounded-lg border border-gray-4 bg-gray-1 p-2.5 text-[13px] leading-[22px] text-gray-12 outline-none transition placeholder:text-gray-8 focus:border-blue-500/60 focus:ring-2 focus:ring-blue-500/20"
+ rows={Math.max(2, Math.ceil(editText.length / 60))}
+ onClick={(e) => e.stopPropagation()}
+ placeholder="Edit transcript text..."
+ />
+ <div className="mt-2 flex justify-end gap-1.5">
+ <button
+ type="button"
+ onClick={(e) => {
+ e.stopPropagation();
+ onCancel();
+ }}
+ disabled={isSaving}
+ className="inline-flex h-7 items-center rounded-full px-3 text-[11px] font-medium text-gray-11 transition hover:bg-gray-3 hover:text-gray-12 disabled:opacity-50"
+ >
+ Cancel
+ </button>
+ <button
+ type="button"
+ onClick={(e) => {
+ e.stopPropagation();
+ void onSave(editText);
+ }}
+ disabled={isSaving || !editText.trim()}
+ className="inline-flex h-7 items-center gap-1.5 rounded-full bg-gray-12 px-3 text-[11px] font-semibold text-white transition hover:bg-gray-11 disabled:pointer-events-none disabled:opacity-50"
+ >
+ {isSaving && <LoaderCircle className="size-3 animate-spin" />}
+ Save
+ </button>
+ </div>
+ </div>
+ );
+}
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 97a16a64b..8855f6ec7 100644
--- a/apps/web/app/s/[videoId]/_components/utils/transcript-utils.ts
+++ b/apps/web/app/s/[videoId]/_components/utils/transcript-utils.ts
@@ -20,14 +20,6 @@ export const formatTime = (seconds: number): string => {
.padStart(3, "0")}`;
};
-export const formatTimeMinutes = (time: number) => {
- const minutes = Math.floor(time / 60);
- const seconds = Math.floor(time % 60);
- return `${minutes.toString().padStart(2, "0")}:${seconds
- .toString()
- .padStart(2, "0")}`;
-};
-
/**
* Formats transcript entries as VTT format for subtitles
*/
diff --git a/apps/web/components/ChromeExtensionButton.tsx b/apps/web/components/ChromeExtensionButton.tsx
index 1b2a8e5a2..4a32dcb00 100644
--- a/apps/web/components/ChromeExtensionButton.tsx
+++ b/apps/web/components/ChromeExtensionButton.tsx
@@ -1,4 +1,4 @@
-import { Button, type ButtonProps } from "@cap/ui";
+import { Button, type ButtonProps } from "@cap/ui/button";
import Image from "next/image";
import { CAP_CHROME_EXTENSION_URL } from "@/lib/chrome-extension";
diff --git a/apps/web/components/ReadyToGetStarted.tsx b/apps/web/components/ReadyToGetStarted.tsx
index b4eea963d..0b1332fff 100644
--- a/apps/web/components/ReadyToGetStarted.tsx
+++ b/apps/web/components/ReadyToGetStarted.tsx
@@ -1,6 +1,6 @@
"use client";
-import { Button } from "@cap/ui";
+import { Button } from "@cap/ui/button";
import { useDetectPlatform } from "hooks/useDetectPlatform";
import Link from "next/link";
import { getPlatformIcon } from "@/utils/platform";
diff --git a/apps/web/components/blog/BlogTemplate.tsx b/apps/web/components/blog/BlogTemplate.tsx
index e94f3b8e4..66a8f8f07 100644
--- a/apps/web/components/blog/BlogTemplate.tsx
+++ b/apps/web/components/blog/BlogTemplate.tsx
@@ -1,6 +1,6 @@
"use client";
-import { Button } from "@cap/ui";
+import { Button } from "@cap/ui/button";
import Image from "next/image";
import Link from "next/link";
import { useEffect, useId } from "react";
diff --git a/apps/web/components/pages/DownloadPage.tsx b/apps/web/components/pages/DownloadPage.tsx
index 2c1e8a584..c59a6a6df 100644
--- a/apps/web/components/pages/DownloadPage.tsx
+++ b/apps/web/components/pages/DownloadPage.tsx
@@ -1,6 +1,6 @@
"use client";
-import { Button } from "@cap/ui";
+import { Button } from "@cap/ui/button";
import { useDetectPlatform } from "hooks/useDetectPlatform";
import Link from "next/link";
import { type CSSProperties, type ReactNode, useId, useState } from "react";
diff --git a/apps/web/components/pages/HomeTwo/Agents.tsx b/apps/web/components/pages/HomeTwo/Agents.tsx
deleted file mode 100644
index d738f1ced..000000000
--- a/apps/web/components/pages/HomeTwo/Agents.tsx
+++ /dev/null
@@ -1,250 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import { ArrowUpRight } from "lucide-react";
-import Link from "next/link";
-import { useRef, useState } from "react";
-import { Eyebrow } from "./Eyebrow";
-import { AGENT } from "./scenes";
-import { LazyMount, useInView, useReducedMotion } from "./scenes/engine";
-import {
- BAND,
- BODY_TEXT,
- BTN_SECONDARY,
- GRAIN,
- grainBg,
- H_SECTION,
- MODE_THEME,
- MONO,
-} from "./theme";
-
-const DARK = {
- backgroundColor: "#111111",
- backgroundImage: GRAIN,
- backgroundSize: "200px 200px",
-} as const;
-
-const SURFACES = ["CLI", "MCP server", "JSON on every command", "REST API"];
-
-const FACTS = [
- {
- title: "One prompt to set up",
- body: "Paste one prompt from the docs and your agent installs the CLI, the Cap skill, and the MCP server for Claude Code, Cursor, or Codex.",
- snippet: "cap agents install --target claude --component all",
- mode: "instant" as const,
- },
- {
- title: "Every command speaks JSON",
- body: "Add one flag and the answer comes back as data. Recording and export stream NDJSON events as they happen.",
- snippet: "cap guide --json",
- mode: "studio" as const,
- },
- {
- title: "76 MCP tools, zero secrets",
- body: "Read and manage Caps, comments, sharing, and analytics over MCP. Capture and upload stay in the CLI, so no secret passes through the model.",
- snippet: "cap mcp serve",
- mode: "screenshot" as const,
- },
- {
- title: "Least privilege login",
- body: "Sign in with the creator profile by default, and the agent asks before anything records, uploads, or costs money.",
- snippet: "cap auth login",
- mode: "share" as const,
- },
-];
-
-type Tab = { key: "cli" | "mcp"; label: string; lines: string[]; note: string };
-
-const CLI_TAB: Tab = {
- key: "cli",
- label: "CLI",
- lines: [
- "cap record start --screen 1 --mode instant --duration 20 --json",
- 'cap upload ./recording.cap --export --name "Checkout bug repro" --json',
- ],
- note: "Record in the background, stop it later, and upload a .cap or .mp4 for a link. Works from any shell, script, or agent tool call.",
-};
-
-const MCP_TAB: Tab = {
- key: "mcp",
- label: "MCP",
- lines: [
- "{",
- ' "mcpServers": {',
- ' "cap": { "command": "cap", "args": ["mcp", "serve"] }',
- " }",
- "}",
- ],
- note: "Point any MCP client at the Cap server and it gets 76 tools for listing, reading, commenting on, and sharing Caps. Capture and upload stay in the CLI.",
-};
-
-const TABS: Tab[] = [CLI_TAB, MCP_TAB];
-
-export const Agents = () => {
- const [chapter, setChapter] = useState(0);
- const [tab, setTab] = useState(0);
- const cardRef = useRef<HTMLDivElement | null>(null);
- const inView = useInView(cardRef);
- const reducedMotion = useReducedMotion();
- const playing = inView && !reducedMotion;
- const active = TABS[tab] ?? CLI_TAB;
-
- return (
- <section className="px-5 py-20 lg:py-28">
- <div className="mx-auto max-w-[1200px]">
- <div className="mx-auto flex max-w-[760px] flex-col items-center text-center">
- <Eyebrow accent={MODE_THEME.studio.accent}>Cap for Agents</Eyebrow>
- <h2
- className={`${H_SECTION} mt-6 text-balance text-[clamp(38px,5vw,56px)]`}
- >
- Your agent can record, share, and read Caps
- </h2>
- <p
- className={`${BODY_TEXT} mt-6 max-w-[640px] text-balance text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[17.5px]`}
- >
- Cap ships a CLI and a local MCP server, so Claude Code, Cursor,
- Codex, OpenCode, or any shell capable agent can record your screen,
- upload the result, and read back the transcript and summary. One
- pasted prompt installs everything, and the agent asks before
- anything records, uploads, or costs money.
- </p>
- </div>
-
- <div
- ref={cardRef}
- className="mt-14 rounded-[24px] p-4 lg:p-6"
- style={DARK}
- >
- <LazyMount w={1200} h={520}>
- <AGENT.Scene
- chapter={chapter}
- playing={playing}
- onChapterEnd={() =>
- setChapter((current) => (current + 1) % AGENT.chapters.length)
- }
- />
- </LazyMount>
- <ul className="mt-5 flex flex-wrap items-center gap-2">
- {SURFACES.map((surface) => (
- <li
- key={surface}
- className={classNames(
- MONO,
- "rounded-full border border-white/15 px-3 py-1.5 text-[11px] uppercase leading-none tracking-[0.05em] text-[rgba(255,255,255,0.72)]",
- )}
- >
- {surface}
- </li>
- ))}
- <li className="ml-auto">
- <Link
- href="/docs/agents"
- className={classNames(
- MONO,
- "flex items-center gap-1.5 rounded-full bg-white px-3.5 py-2 text-[11px] uppercase leading-none tracking-[0.05em] text-[#111111] transition-colors duration-200 hover:bg-[#EDF1F6]",
- )}
- >
- Agent docs
- <ArrowUpRight className="size-3" />
- </Link>
- </li>
- </ul>
- </div>
-
- <div className="mt-4 grid gap-4 lg:grid-cols-[1.05fr_1fr]">
- <ul className="grid gap-4 sm:grid-cols-2">
- {FACTS.map((fact) => {
- const theme = MODE_THEME[fact.mode];
- return (
- <li
- key={fact.title}
- className="flex flex-col justify-between rounded-[20px] p-6"
- style={grainBg(BAND)}
- >
- <div>
- <span
- aria-hidden="true"
- className="block size-[7px]"
- style={{ background: theme.accent }}
- />
- <h3 className="mt-5 text-[19px] font-normal leading-[1.1] tracking-[-0.02em] text-[#111111]">
- {fact.title}
- </h3>
- <p
- className={`${BODY_TEXT} mt-2 text-[14.5px] leading-[1.45] text-[rgba(17,17,17,0.7)]`}
- >
- {fact.body}
- </p>
- </div>
- <code
- className={classNames(
- MONO,
- "mt-5 block whitespace-pre-wrap break-words rounded-[10px] bg-white px-3 py-2 text-[12px] leading-[1.6] text-[#111111] shadow-[0_0_0_1px_rgba(17,17,17,0.06)]",
- )}
- >
- <span className="text-[rgba(17,17,17,0.4)]">$ </span>
- {fact.snippet}
- </code>
- </li>
- );
- })}
- </ul>
-
- <div
- className="flex flex-col rounded-[20px] p-6"
- style={grainBg(BAND)}
- >
- <div className="flex items-center gap-1 self-start rounded-full bg-white p-1 shadow-[0_0_0_1px_rgba(17,17,17,0.06)]">
- {TABS.map((item, i) => (
- <button
- key={item.key}
- type="button"
- aria-pressed={i === tab}
- onClick={() => setTab(i)}
- className={classNames(
- "rounded-full px-4 py-1.5 text-[13px] font-medium transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111]",
- i === tab
- ? "bg-[#111111] text-white"
- : "text-[rgba(17,17,17,0.6)] hover:text-[#111111]",
- )}
- >
- {item.label}
- </button>
- ))}
- </div>
- <pre
- className={classNames(
- MONO,
- "mt-4 rounded-[14px] p-5 text-[12.5px] leading-[1.75] text-[#F8FAFC]",
- )}
- style={DARK}
- >
- {active.lines.map((line) => (
- <span
- key={line}
- className="block whitespace-pre-wrap break-words"
- >
- {active.key === "cli" ? (
- <span className="text-[rgba(255,255,255,0.4)]">$ </span>
- ) : null}
- {line}
- </span>
- ))}
- </pre>
- <p
- className={`${BODY_TEXT} mb-5 mt-4 text-[14.5px] leading-[1.45] text-[rgba(17,17,17,0.7)]`}
- >
- {active.note}
- </p>
- <Link
- href="/agents"
- className={classNames(BTN_SECONDARY, "self-start")}
- >
- Explore Cap for Agents
- </Link>
- </div>
- </div>
- </div>
- </section>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/DeepDive.tsx b/apps/web/components/pages/HomeTwo/DeepDive.tsx
deleted file mode 100644
index d601483f2..000000000
--- a/apps/web/components/pages/HomeTwo/DeepDive.tsx
+++ /dev/null
@@ -1,198 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import { ArrowUpRight } from "lucide-react";
-import Link from "next/link";
-import type { ComponentType } from "react";
-import { useCallback, useId, useRef, useState } from "react";
-import { Eyebrow } from "./Eyebrow";
-import { Fit, SCENES, STAGE } from "./scenes";
-import { LazyMount, useInView, useReducedMotion } from "./scenes/engine";
-import {
- BODY_TEXT,
- H_SECTION,
- MODE_THEME,
- type ModeKey,
- meshStyle,
-} from "./theme";
-
-export type DiveItem = {
- title: string;
- body: string;
-};
-
-export type DiveChip = {
- title: string;
- description: string;
- Icon: ComponentType<{ className?: string }>;
-};
-
-export type DiveConfig = {
- mode: ModeKey;
- eyebrow: string;
- heading: string;
- intro?: string;
- items: DiveItem[];
- chips?: DiveChip[];
- href: string;
- flip?: boolean;
-};
-
-export const DeepDive = ({ config }: { config: DiveConfig }) => {
- const [active, setActive] = useState(0);
- const baseId = useId();
- const theme = MODE_THEME[config.mode];
- const scene = SCENES[config.mode];
- const visualRef = useRef<HTMLDivElement | null>(null);
- const progressRef = useRef<HTMLSpanElement | null>(null);
- const inView = useInView(visualRef);
- const reducedMotion = useReducedMotion();
- const playing = inView && !reducedMotion;
- const count = config.items.length;
-
- const advance = useCallback(() => {
- setActive((current) => (current + 1) % count);
- }, [count]);
-
- return (
- <section className="px-5 py-16 lg:py-24">
- <div className="mx-auto grid max-w-[1200px] items-start gap-12 lg:grid-cols-2 lg:gap-16">
- <div
- className={classNames(
- "order-first",
- config.flip ? "lg:order-first" : "lg:order-last",
- )}
- >
- <div ref={visualRef} className="lg:sticky lg:top-24">
- <div
- className="rounded-[24px] p-3 shadow-[0_30px_60px_-40px_rgba(17,17,17,0.35)]"
- style={meshStyle(theme)}
- >
- <LazyMount w={STAGE.w} h={STAGE.h} className="mx-auto">
- <Fit w={STAGE.w} h={STAGE.h}>
- <scene.Scene
- chapter={active}
- playing={playing}
- onChapterEnd={advance}
- progressRef={progressRef}
- />
- </Fit>
- </LazyMount>
- </div>
- </div>
- </div>
-
- <div className={config.flip ? "lg:order-last" : "lg:order-first"}>
- <Eyebrow accent={theme.accent}>{config.eyebrow}</Eyebrow>
-
- <h2
- className={`${H_SECTION} mt-6 max-w-[480px] text-balance text-[clamp(34px,3.9vw,50px)]`}
- >
- {config.heading}
- </h2>
-
- {config.intro ? (
- <p
- className={`${BODY_TEXT} mt-6 max-w-[460px] text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)]`}
- >
- {config.intro}
- </p>
- ) : null}
-
- <div className="mt-10">
- {config.items.map((item, i) => {
- const open = i === active;
- const panelId = `${baseId}-panel-${i}`;
- const headerId = `${baseId}-header-${i}`;
- return (
- <div
- key={item.title}
- className="relative border-t border-[#E1E7EE] last:border-b"
- >
- <span
- aria-hidden="true"
- className="absolute inset-x-0 top-[-1px] h-[2px] overflow-hidden"
- style={{ opacity: open ? 1 : 0 }}
- >
- <span
- ref={open ? progressRef : undefined}
- className="block h-full w-full origin-left"
- style={{
- background: theme.accent,
- transform: `scaleX(${open && !playing ? 1 : 0})`,
- }}
- />
- </span>
- <h3>
- <button
- type="button"
- id={headerId}
- aria-expanded={open}
- aria-controls={panelId}
- onClick={() => setActive(i)}
- className={classNames(
- "flex w-full items-center justify-between gap-6 py-[24px] text-left text-[19px] font-normal tracking-[-0.02em] transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111] focus-visible:ring-offset-4 focus-visible:ring-offset-[#F8FAFC] lg:text-[21px]",
- open
- ? "text-[#111111]"
- : "text-[rgba(17,17,17,0.4)] hover:text-[rgba(17,17,17,0.7)]",
- )}
- >
- {item.title}
- </button>
- </h3>
- <div
- id={panelId}
- aria-hidden={!open}
- className="grid transition-[grid-template-rows] duration-300 ease-out"
- style={{ gridTemplateRows: open ? "1fr" : "0fr" }}
- >
- <div className="overflow-hidden">
- <div className="flex items-start justify-between gap-6 pb-8">
- <p
- className={`${BODY_TEXT} max-w-[420px] text-[16px] leading-[1.5] text-[rgba(17,17,17,0.78)]`}
- >
- {item.body}
- </p>
- <Link
- href={config.href}
- tabIndex={open ? undefined : -1}
- aria-label={`Learn more about ${item.title}`}
- className="grid size-10 shrink-0 place-items-center rounded-[8px] bg-[#E7EDF3] text-[#111111] transition-colors duration-200 hover:bg-[#DCE4EC] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111]"
- >
- <ArrowUpRight className="size-4" />
- </Link>
- </div>
- </div>
- </div>
- </div>
- );
- })}
- </div>
-
- {config.chips ? (
- <ul className="mt-10 grid gap-x-8 gap-y-6 sm:grid-cols-2">
- {config.chips.map((chip) => (
- <li key={chip.title} className="flex items-start gap-3">
- <span
- className="grid size-8 shrink-0 place-items-center rounded-[8px]"
- style={{ background: theme.chip, color: theme.glyph }}
- >
- <chip.Icon className="size-4" />
- </span>
- <span className="min-w-0">
- <span className="block text-[15px] font-medium text-[#111111]">
- {chip.title}
- </span>
- <span className="mt-0.5 block text-[13.5px] leading-snug text-[rgba(17,17,17,0.55)]">
- {chip.description}
- </span>
- </span>
- </li>
- ))}
- </ul>
- ) : null}
- </div>
- </div>
- </section>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/DeepDives.tsx b/apps/web/components/pages/HomeTwo/DeepDives.tsx
deleted file mode 100644
index b5fbdbdc5..000000000
--- a/apps/web/components/pages/HomeTwo/DeepDives.tsx
+++ /dev/null
@@ -1,218 +0,0 @@
-"use client";
-
-import {
- BookOpen,
- Captions,
- Copy,
- EyeOff,
- FileText,
- Globe,
- Layers,
- LayoutGrid,
- Link2,
- Maximize2,
- MousePointerClick,
- MoveUpRight,
- Palette,
- Square,
- Type,
- Upload,
-} from "lucide-react";
-import { DeepDive, type DiveConfig } from "./DeepDive";
-import { StudioFeatures } from "./studio/StudioFeatures";
-
-const INSTANT: DiveConfig = {
- mode: "instant",
- eyebrow: "Instant Mode",
- heading: "Share your screen or webcam instantly",
- intro:
- "Hit record, stop, and share. Your video is live with a title, summary, chapters, and transcript, all written for you.",
- href: "/features",
- items: [
- {
- title: "Uploads while you record",
- body: "Instant Mode streams to the cloud as you go, so the video is ready the moment you press stop.",
- },
- {
- title: "The link is on your clipboard",
- body: "Press stop and it is already copied, ready to paste into Slack, a ticket, or an email.",
- },
- {
- title: "Share publicly or privately",
- body: "Password protect the sensitive ones, keep a link to your team, or leave it public. You decide who can watch.",
- },
- ],
- chips: [
- {
- title: "Instant links",
- description: "Shareable URL, immediately",
- Icon: Link2,
- },
- {
- title: "Background upload",
- description: "Uploads while you record",
- Icon: Upload,
- },
- {
- title: "Auto transcription",
- description: "Captions on every recording",
- Icon: Captions,
- },
- {
- title: "AI summaries",
- description: "Titles and descriptions",
- Icon: FileText,
- },
- {
- title: "Smart chapters",
- description: "Auto segmented timeline",
- Icon: BookOpen,
- },
- {
- title: "Browser viewing",
- description: "No downloads required",
- Icon: Globe,
- },
- ],
-};
-
-const STUDIO: DiveConfig = {
- mode: "studio",
- eyebrow: "Studio Mode",
- heading: "Record in full quality, edit before you share",
- intro:
- "Studio records at the highest quality straight to your device, with no compression and no upload. Then blur what is private, switch scenes between screen and camera, add text and captions, and set the look before anyone sees it.",
- href: "/features",
- flip: true,
- items: [
- {
- title: "Records locally in full quality",
- body: "4K at 60fps straight to your device, with no compression and no upload. Screen, camera, and mic each stay on their own track.",
- },
- {
- title: "An editor built in",
- body: "Custom backgrounds, adjustable padding, rounded corners, shadows and borders, all set before anyone sees it.",
- },
- {
- title: "Automatic zoom and cursor effects",
- body: "Cap smooths the cursor, adds click effects and natural motion blur, and zooms in where the action is.",
- },
- ],
- chips: [
- {
- title: "Custom backgrounds",
- description: "Gradients, wallpapers, colours",
- Icon: Palette,
- },
- {
- title: "Adjustable padding",
- description: "Scale from 0% to 40%",
- Icon: Maximize2,
- },
- {
- title: "Blur sensitive areas",
- description: "Mask, pixelate, or highlight",
- Icon: EyeOff,
- },
- {
- title: "Scenes and split screen",
- description: "Camera only, split, or floating",
- Icon: LayoutGrid,
- },
- {
- title: "Text and captions",
- description: "Animated titles, local captions",
- Icon: Type,
- },
- {
- title: "Cursor effects",
- description: "Sizing, smoothing, click effects",
- Icon: MousePointerClick,
- },
- ],
-};
-
-const SCREENSHOT: DiveConfig = {
- mode: "screenshot",
- eyebrow: "Screenshot Mode",
- heading: "Capture, beautify, share",
- intro:
- "One hotkey grabs the window, and the same editing tools that polish your videos turn the still into something worth pasting.",
- href: "/features",
- items: [
- {
- title: "Capture and beautify in one hotkey",
- body: "Grab any window or area and Cap drops it on a background with padding, rounded corners, and a shadow.",
- },
- {
- title: "Annotate, then copy",
- body: "Point out the bug or the button you mean, then send the result straight to your clipboard or save it as a file.",
- },
- ],
- chips: [
- {
- title: "Beautiful backgrounds",
- description: "Gradients, wallpapers, solid colours",
- Icon: Palette,
- },
- {
- title: "Adjustable padding",
- description: "Clean spacing around the capture",
- Icon: Maximize2,
- },
- {
- title: "Rounded corners",
- description: "Squircle or rounded styles",
- Icon: Square,
- },
- {
- title: "Shadow and borders",
- description: "Depth without the fuss",
- Icon: Layers,
- },
- {
- title: "Annotation tools",
- description: "Arrows, shapes, text, masks",
- Icon: MoveUpRight,
- },
- {
- title: "Instant copy and save",
- description: "One click to clipboard or file",
- Icon: Copy,
- },
- ],
-};
-
-const UNDERSTAND: DiveConfig = {
- mode: "share",
- eyebrow: "Cap AI · On every recording",
- heading: "Every recording, understood",
- intro:
- "Titles, summaries, chapters, and a searchable transcript arrive with the recording, so the work after the recording is already done.",
- href: "/features",
- flip: true,
- items: [
- {
- title: "Auto transcription",
- body: "Every word transcribed and timestamped, so viewers can search the recording instead of scrubbing it.",
- },
- {
- title: "Titles, summaries, and chapters",
- body: "Written the moment you finish recording, with clickable chapters viewers can skim.",
- },
- {
- title: "Comments and reactions",
- body: "Feedback lands at the exact timestamp it is about, so the conversation stays with the recording.",
- },
- ],
-};
-
-export const DeepDives = () => (
- <>
- <DeepDive config={INSTANT} />
- <DeepDive config={STUDIO} />
- <StudioFeatures />
- <DeepDive config={SCREENSHOT} />
- <DeepDive config={UNDERSTAND} />
- </>
-);
diff --git a/apps/web/components/pages/HomeTwo/Features.tsx b/apps/web/components/pages/HomeTwo/Features.tsx
deleted file mode 100644
index e9aa410b8..000000000
--- a/apps/web/components/pages/HomeTwo/Features.tsx
+++ /dev/null
@@ -1,136 +0,0 @@
-import { classNames } from "@cap/utils/helpers";
-import {
- ArrowDownToLine,
- Captions,
- Gauge,
- Github,
- HardDrive,
- Lock,
- MessageSquare,
- MonitorSmartphone,
-} from "lucide-react";
-import Link from "next/link";
-import type { ComponentType } from "react";
-import { Eyebrow } from "./Eyebrow";
-import {
- BAND,
- BODY_TEXT,
- BTN_SECONDARY,
- grainBg,
- H_SECTION,
- MODE_THEME,
- type ModeKey,
-} from "./theme";
-
-type Feature = {
- title: string;
- body: string;
- accent: ModeKey;
- Icon: ComponentType<{ className?: string }>;
-};
-
-const FEATURES: Feature[] = [
- {
- title: "Your storage, your rules",
- body: "Connect your own Google Drive or S3 bucket, use Cap Cloud, or keep everything local. You are never locked into our infrastructure.",
- accent: "instant",
- Icon: HardDrive,
- },
- {
- title: "Privacy by default, sharing by choice",
- body: "Share publicly or privately, password protect the sensitive ones, or keep a recording on your machine and never upload it at all.",
- accent: "studio",
- Icon: Lock,
- },
- {
- title: "Async collaboration that works",
- body: "Comments, reactions, and transcripts keep the conversation moving. See who watched and turn a recording into the next step, not another meeting.",
- accent: "screenshot",
- Icon: MessageSquare,
- },
- {
- title: "Cross platform for your whole team",
- body: "Native apps for macOS and Windows that feel at home on each, plus a Chrome extension when recording in the browser is the right call.",
- accent: "share",
- Icon: MonitorSmartphone,
- },
- {
- title: "Quality that makes you look good",
- body: "4K recording, 60fps capture, and compression smart enough to keep the file sizes sane.",
- accent: "studio",
- Icon: Gauge,
- },
- {
- title: "Truly open source",
- body: "See exactly how Cap works, contribute the feature you need, or self host the whole thing. No black boxes.",
- accent: "instant",
- Icon: Github,
- },
- {
- title: "Cap AI does the busywork",
- body: "Titles, summaries, clickable chapters, and transcripts write themselves for every recording, with no usage limits on Pro.",
- accent: "share",
- Icon: Captions,
- },
- {
- title: "Import your Loom videos",
- body: "Switching from Loom? Bring your existing library across with the built in importer and keep everything in one place.",
- accent: "screenshot",
- Icon: ArrowDownToLine,
- },
-];
-
-export const Features = () => (
- <section className="px-5 py-20 lg:py-28">
- <div className="mx-auto max-w-[1200px]">
- <div className="mx-auto flex max-w-[760px] flex-col items-center text-center">
- <Eyebrow accent={MODE_THEME.studio.accent}>Why Cap</Eyebrow>
- <h2
- className={`${H_SECTION} mt-6 text-balance text-[clamp(38px,5vw,56px)]`}
- >
- Built for how you actually work
- </h2>
- <p
- className={`${BODY_TEXT} mt-6 max-w-[560px] text-balance text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[17.5px]`}
- >
- We obsessed over the details so you do not have to. Every feature here
- exists to save you time and make you look good.
- </p>
- </div>
-
- <ul className="mt-14 grid gap-3 sm:grid-cols-2 lg:grid-cols-4 lg:gap-4">
- {FEATURES.map((feature) => {
- const theme = MODE_THEME[feature.accent];
- return (
- <li
- key={feature.title}
- className="flex flex-col rounded-[14px] p-6"
- style={grainBg(BAND)}
- >
- <span
- className="grid size-9 shrink-0 place-items-center rounded-[9px]"
- style={{ background: theme.chip, color: theme.glyph }}
- >
- <feature.Icon className="size-[18px]" />
- </span>
- <h3 className="mt-5 text-balance text-[19px] font-normal leading-[1.15] tracking-[-0.02em] text-[#111111]">
- {feature.title}
- </h3>
- <p
- className={`${BODY_TEXT} mt-3 text-[15px] leading-[1.5] text-[rgba(17,17,17,0.72)]`}
- >
- {feature.body}
- </p>
- </li>
- );
- })}
- </ul>
-
- <div className="mt-10 flex justify-center">
- <Link href="/features" className={classNames(BTN_SECONDARY)}>
- View all features
- </Link>
- </div>
- </div>
- </section>
-);
diff --git a/apps/web/components/pages/HomeTwo/FinalCta.tsx b/apps/web/components/pages/HomeTwo/FinalCta.tsx
index e4780fc11..ec560be5d 100644
--- a/apps/web/components/pages/HomeTwo/FinalCta.tsx
+++ b/apps/web/components/pages/HomeTwo/FinalCta.tsx
@@ -1,32 +1,14 @@
-"use client";
-
import Link from "next/link";
-import { Eyebrow } from "./Eyebrow";
-import { Fit, SCENES, STAGE } from "./scenes";
-import { LazyMount } from "./scenes/engine";
-import {
- BODY_TEXT,
- BTN_PRIMARY,
- BTN_SECONDARY,
- H_HERO,
- MODE_THEME,
- type ModeKey,
- meshStyle,
-} from "./theme";
-
-const REEL: ModeKey[] = ["instant", "studio", "screenshot"];
+import { BODY_TEXT, BTN_PRIMARY, BTN_SECONDARY, H_HERO } from "./theme";
export const FinalCta = () => (
- <section className="overflow-hidden pb-24 pt-20 lg:pb-28 lg:pt-28">
- <div className="mx-auto flex max-w-[900px] flex-col items-center px-5 text-center">
- <Eyebrow accent={MODE_THEME.share.accent}>Get started</Eyebrow>
- <h2
- className={`${H_HERO} mt-6 text-balance text-[clamp(44px,6.4vw,78px)]`}
- >
+ <section className="px-5 pb-28 pt-24 lg:pb-40 lg:pt-36">
+ <div className="mx-auto flex max-w-[900px] flex-col items-center text-center">
+ <h2 className={`${H_HERO} text-balance text-[clamp(44px,6.4vw,78px)]`}>
Ready to upgrade how you communicate?
</h2>
<p
- className={`${BODY_TEXT} mt-7 max-w-[560px] text-balance text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[18px]`}
+ className={`${BODY_TEXT} mt-7 max-w-[560px] text-balance text-[17px] leading-[1.5] text-[rgba(17,17,17,0.64)] sm:text-[18px]`}
>
All of the work around screen recordings, handled in one place. Capture,
polish, and share without the busywork.
@@ -40,36 +22,5 @@ export const FinalCta = () => (
</Link>
</div>
</div>
-
- <div
- aria-hidden="true"
- className="mt-20 flex items-start justify-center gap-5 lg:gap-6"
- >
- {REEL.map((mode, i) => {
- const scene = SCENES[mode];
- const centre = i === 1;
- return (
- <div
- key={mode}
- className={
- centre
- ? "w-[min(92vw,640px)] shrink-0 rounded-[22px] p-3"
- : "hidden w-[460px] shrink-0 rounded-[22px] p-3 sm:block"
- }
- style={meshStyle(MODE_THEME[mode])}
- >
- <LazyMount w={STAGE.w} h={STAGE.h}>
- <Fit w={STAGE.w} h={STAGE.h} still>
- <scene.Scene
- chapter={0}
- playing={false}
- staticT={scene.poster}
- />
- </Fit>
- </LazyMount>
- </div>
- );
- })}
- </div>
</section>
);
diff --git a/apps/web/components/pages/HomeTwo/Hero.tsx b/apps/web/components/pages/HomeTwo/Hero.tsx
index 2b45c665a..32388f4ac 100644
--- a/apps/web/components/pages/HomeTwo/Hero.tsx
+++ b/apps/web/components/pages/HomeTwo/Hero.tsx
@@ -40,11 +40,13 @@ const PhoneStage = () => {
className="relative overflow-hidden rounded-[14px] bg-black"
style={{ width: PHONE_STAGE.w, height: PHONE_STAGE.h }}
>
+ {/* The stage is scaled down to phone width, so a pre-sized 960px
+ copy beats the 1920px variant next/image picks on dense screens. */}
<Image
- src="/backgrounds/sf.webp"
+ src="/backgrounds/landing/sf-960.webp"
alt=""
fill
- sizes="440px"
+ unoptimized
draggable={false}
className="object-cover"
/>
@@ -129,7 +131,7 @@ export const Hero = () => {
release: "v0.6.1",
})
}
- 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"
+ className="ht-rise group mb-7 inline-flex [--d:0.04s] 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]`}
@@ -145,16 +147,19 @@ export const Hero = () => {
<HeroHeadline />
<p
- 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`}
+ className={`ht-rise [--d:0.16s] ${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`}
>
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="ht-rise mt-9 flex flex-col items-stretch gap-3 [--d:0.9s] sm:flex-row sm:items-center">
+ <div className="ht-rise mt-9 flex flex-col items-stretch gap-3 [--d:0.2s] sm:flex-row sm:items-center">
<Link
href={downloadHref}
+ // Redirects to the installer on the CDN; prefetching it only
+ // produced a failed cross-origin request on every visit.
+ prefetch={false}
onClick={() =>
trackEvent("download_cta_clicked", {
source_page: "home_header",
@@ -207,7 +212,7 @@ export const Hero = () => {
cta_location: "under_primary",
})
}
- 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]"
+ className="ht-rise group mt-5 inline-flex items-center gap-2 rounded-[6px] [--d:0.24s] 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 +227,7 @@ export const Hero = () => {
<ArrowRight className="size-3.5 transition-transform duration-200 group-hover:translate-x-0.5" />
</Link>
- <div className="ht-rise mt-3 flex items-center gap-2 text-[14px] text-[rgba(17,17,17,0.5)] [--d:1.15s]">
+ <div className="ht-rise mt-3 flex items-center gap-2 text-[14px] text-[rgba(17,17,17,0.5)] [--d:0.28s]">
<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 f76fb8b17..7fe3f14c7 100644
--- a/apps/web/components/pages/HomeTwo/HeroHeadline.tsx
+++ b/apps/web/components/pages/HomeTwo/HeroHeadline.tsx
@@ -251,7 +251,7 @@ export const HeroHeadline = () => {
{CSS}
</style>
- <div className="ht-rise relative pb-10 [--d:0.35s] md:pb-14">
+ <div className="ht-rise relative pb-10 [--d:0.08s] 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;
@@ -302,7 +302,7 @@ export const HeroHeadline = () => {
</div>
<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`}
+ className={`ht-rise ht-rise-soft [--d:0.1s] ${H_HERO} max-w-[15ch] text-balance text-[clamp(34px,4.4vw,60px)] sm:max-w-none`}
>
<span className="sr-only">
The open source screen recorder for everyone. Cap has three recording
@@ -330,8 +330,8 @@ 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; }
+.ht-rise { animation: ht-rise 0.7s cubic-bezier(0.16, 1, 0.3, 1) var(--d, 0s) both; }
+.ht-rise-soft { animation-name: ht-rise-soft; animation-duration: 0.8s; }
@keyframes ht-rise {
from { opacity: 0; transform: translate3d(0, 14px, 0); }
to { opacity: 1; transform: none; }
diff --git a/apps/web/components/pages/HomeTwo/HeroSky.tsx b/apps/web/components/pages/HomeTwo/HeroSky.tsx
index be1c23bc0..ee1c9f294 100644
--- a/apps/web/components/pages/HomeTwo/HeroSky.tsx
+++ b/apps/web/components/pages/HomeTwo/HeroSky.tsx
@@ -190,18 +190,23 @@ const CSS = `
to { transform: translate3d(100%, 0, 0); }
}
-/* Clouds and light arrive after the wash starts lifting. */
+/* Light arrives after the wash starts lifting. Clouds are already faintly
+ there on the first frame and fill in: a cloud is the page's largest
+ image, and one that starts fully transparent holds LCP back until its
+ fade becomes visible. */
.ht-clouds, .ht-light {
position: absolute;
inset: 0;
animation: ht-fade-in 2.2s cubic-bezier(0.4, 0, 0.2, 1) 0.25s both;
}
+.ht-clouds { animation-name: ht-clouds-in; animation-delay: 0s; }
.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; } }
+@keyframes ht-clouds-in { from { opacity: 0.25; } to { opacity: 1; } }
.ht-mist {
position: absolute;
@@ -284,36 +289,29 @@ const CSS = `
background: linear-gradient(180deg, ${BAND}00 0%, ${BAND} 100%);
}
-/* Film grain, jumping to a new offset 12 times a second. */
+/* Film grain. Held still: a layer that changes 12 times a second keeps the
+ page from ever looking finished, which pushed LCP and Speed Index out by
+ seconds, and at this opacity the motion is barely visible anyway. */
.ht-grain {
position: absolute;
- inset: -200px;
+ inset: 0;
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;
+ animation: ht-wash 1s cubic-bezier(0.4, 0, 0.2, 1) both;
}
@keyframes ht-wash { from { opacity: 1; } to { opacity: 0; } }
.ht-sky-idle *, .ht-sky-idle *::before { animation-play-state: paused; }
+/* Idle pauses the drifting, never the entrance: the sky renders idle until
+ hydration, and a paused fade kept the clouds invisible (and LCP waiting)
+ until then. */
+.ht-sky-idle .ht-clouds, .ht-sky-idle .ht-light, .ht-sky-idle .ht-wash { animation-play-state: running; }
@media (prefers-reduced-motion: reduce) {
.ht-sky * { animation: none !important; }
.ht-wash { opacity: 0; }
diff --git a/apps/web/components/pages/HomeTwo/LoomBridge.tsx b/apps/web/components/pages/HomeTwo/LoomBridge.tsx
index f07877b1f..03adb97aa 100644
--- a/apps/web/components/pages/HomeTwo/LoomBridge.tsx
+++ b/apps/web/components/pages/HomeTwo/LoomBridge.tsx
@@ -6,24 +6,17 @@ import Link from "next/link";
import { trackEvent } from "@/app/utils/analytics";
import { LoomMark } from "@/components/icons/LoomMark";
import { MigratePromoBadge } from "@/components/MigratePromoBadge";
-import { Eyebrow } from "./Eyebrow";
-import { BAND, BODY_TEXT, BTN_PRIMARY, grainBg, MODE_THEME } from "./theme";
+import { BODY_TEXT, BTN_PRIMARY } from "./theme";
export const LoomBridge = () => (
<section className="px-5 pb-4 pt-2">
- <div
- className="mx-auto flex max-w-[1200px] flex-col items-start justify-between gap-8 rounded-[20px] p-8 lg:flex-row lg:items-center lg:p-12"
- style={grainBg(BAND)}
- >
+ <div className="mx-auto flex max-w-[1120px] flex-col items-start justify-between gap-8 rounded-[24px] bg-white p-8 shadow-[0_0_0_1px_rgba(17,17,17,0.06),0_1px_2px_rgba(17,17,17,0.03)] lg:flex-row lg:items-center lg:p-12">
<div className="max-w-[560px]">
- <Eyebrow accent={MODE_THEME.instant.accent}>
- Switching from Loom
- </Eyebrow>
- <h2 className="mt-5 text-balance text-[clamp(28px,3.2vw,40px)] font-normal leading-[1.05] tracking-[-0.03em] text-[#111111]">
+ <h2 className="text-balance text-[clamp(28px,3.2vw,40px)] font-normal leading-[1.05] tracking-[-0.03em] text-[#111111]">
Bring your whole Loom library with you
</h2>
<p
- className={`${BODY_TEXT} mt-4 max-w-[500px] text-[16px] leading-[1.5] text-[rgba(17,17,17,0.78)]`}
+ className={`${BODY_TEXT} mt-4 max-w-[500px] text-[16px] leading-[1.5] text-[rgba(17,17,17,0.64)]`}
>
Paste a Loom link or upload a CSV of your library and Cap re-hosts
every video with its title, a transcript and chapters. Your recordings
diff --git a/apps/web/components/pages/HomeTwo/ModeWalkthrough.tsx b/apps/web/components/pages/HomeTwo/ModeWalkthrough.tsx
index 91f5274ab..65207181a 100644
--- a/apps/web/components/pages/HomeTwo/ModeWalkthrough.tsx
+++ b/apps/web/components/pages/HomeTwo/ModeWalkthrough.tsx
@@ -115,7 +115,7 @@ const StackedSteps = ({ className }: { className?: string }) => (
</span>
</span>
{step.extraChip ? (
- <span className="rounded-full bg-[#E7EDF3] px-2.5 py-1 text-[12px] font-normal text-[rgba(17,17,17,0.55)]">
+ <span className="rounded-full bg-[#E7EDF3] px-2.5 py-1 text-[12px] font-normal text-[rgba(17,17,17,0.66)]">
{step.extraChip}
</span>
) : null}
diff --git a/apps/web/components/pages/HomeTwo/Ownership.tsx b/apps/web/components/pages/HomeTwo/Ownership.tsx
deleted file mode 100644
index 8e207a02f..000000000
--- a/apps/web/components/pages/HomeTwo/Ownership.tsx
+++ /dev/null
@@ -1,439 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import { Server } from "lucide-react";
-import Image from "next/image";
-import { type ReactNode, useEffect, useRef, useState } from "react";
-import { CapLogoMark } from "./demo/capIcons";
-import { Eyebrow } from "./Eyebrow";
-import { Scaled } from "./scenes/engine";
-import {
- BAND,
- BODY_TEXT,
- EYEBROW,
- grainBg,
- H_SECTION,
- MODE_THEME,
- type ModeKey,
-} from "./theme";
-import { useInView, usePageVisible, useReducedMotion } from "./visibility";
-
-const QUOTE = {
- name: "Steven Tey",
- handle: "Founder, Dub.co",
- image: "/testimonials/steven_tey.jpg",
- content:
- "Cap is one of my favorite pieces of software I've used in the recent years. Best part is you get to own your data since they're fully open-source + via their S3 integration.",
-};
-
-type Destination = {
- key: string;
- name: string;
- label: string;
- detail: string;
- mode: ModeKey;
- icon: ReactNode;
-};
-
-const DESTINATIONS: Destination[] = [
- {
- key: "cloud",
- name: "Cap Cloud",
- label: "Cap Cloud",
- detail: "Zero setup. Encrypted, and shareable the second you stop.",
- mode: "instant",
- icon: <CapLogoMark className="size-6" />,
- },
- {
- key: "s3",
- name: "Your own S3 bucket",
- label: "Your S3 bucket",
- detail: "Any S3 compatible bucket, in any region you choose.",
- mode: "studio",
- icon: (
- <span className="grid size-6 place-items-center rounded-[6px] bg-[#111111] text-[10px] font-semibold text-white">
- S3
- </span>
- ),
- },
- {
- key: "drive",
- name: "Google Drive",
- label: "Google Drive",
- detail: "Recordings land in a Drive folder you pick.",
- mode: "screenshot",
- icon: (
- <Image
- src="/logos/google-drive.svg"
- alt=""
- width={24}
- height={24}
- className="size-6"
- />
- ),
- },
- {
- key: "self",
- name: "Self hosted",
- label: "Self hosted",
- detail: "Run the whole platform on your own infrastructure.",
- mode: "share",
- icon: <Server className="size-5 text-[#111111]" />,
- },
-];
-
-const PROOF = [
- {
- label: "Open source",
- body: "Every line is on GitHub. Audit it, fork it, or contribute the feature you need.",
- },
- {
- label: "SOC 2 Type II · ISO 27001",
- body: "Independently audited controls behind Cap Cloud.",
- },
- {
- label: "HIPAA compliant",
- body: "Signed BAAs for organizations handling PHI.",
- },
- {
- label: "No lock in",
- body: "Recordings are plain video files. Take them anywhere, any time.",
- },
-];
-
-const DIAGRAM = { w: 560, h: 340 };
-const SOURCE = { x: 168, y: DIAGRAM.h / 2 };
-const NODE = { x: 372, w: 176, h: 56, gap: 22 };
-const nodeY = (i: number) =>
- (DIAGRAM.h - (NODE.h * 4 + NODE.gap * 3)) / 2 + i * (NODE.h + NODE.gap);
-const routePath = (i: number) => {
- const cy = nodeY(i) + NODE.h / 2;
- const mid = (SOURCE.x + NODE.x) / 2;
- return `M ${SOURCE.x} ${SOURCE.y} C ${mid} ${SOURCE.y}, ${mid} ${cy}, ${NODE.x} ${cy}`;
-};
-
-const ROUTE_CSS = `
- @keyframes ht-route-packet {
- from { offset-distance: 0%; opacity: 0; }
- 8% { opacity: 1; }
- 92% { opacity: 1; }
- to { offset-distance: 100%; opacity: 0; }
- }
- @keyframes ht-route-dash {
- to { stroke-dashoffset: -28; }
- }
- @keyframes ht-route-land {
- 0% { box-shadow: 0 0 0 0 var(--ht-glow); }
- 100% { box-shadow: 0 0 0 14px transparent; }
- }
-`;
-
-const StorageRoute = ({
- active,
- playing,
-}: {
- active: number;
- playing: boolean;
-}) => {
- const theme = MODE_THEME[DESTINATIONS[active]?.mode ?? "instant"];
- const animationPlayState = playing ? "running" : "paused";
- return (
- <Scaled w={DIAGRAM.w} h={DIAGRAM.h} className="mx-auto">
- <style>{ROUTE_CSS}</style>
- <svg
- aria-hidden="true"
- viewBox={`0 0 ${DIAGRAM.w} ${DIAGRAM.h}`}
- className="absolute inset-0 h-full w-full"
- >
- {DESTINATIONS.map((destination, i) => (
- <path
- key={destination.key}
- d={routePath(i)}
- fill="none"
- stroke={i === active ? theme.glyph : "rgba(17,17,17,0.14)"}
- strokeWidth={i === active ? 2 : 1.5}
- strokeDasharray={i === active ? "8 6" : undefined}
- strokeLinecap="round"
- className="transition-[stroke] duration-500"
- style={
- i === active
- ? {
- animationName: "ht-route-dash",
- animationDuration: "1.1s",
- animationTimingFunction: "linear",
- animationIterationCount: "infinite",
- animationPlayState,
- }
- : undefined
- }
- />
- ))}
- </svg>
-
- {[0, 1].map((packet) => (
- <span
- key={packet}
- className="pointer-events-none absolute left-0 top-0 size-3 rounded-full"
- style={{
- background: theme.glyph,
- boxShadow: `0 0 0 4px ${theme.chip}`,
- offsetPath: `path("${routePath(active)}")`,
- offsetRotate: "0deg",
- animationName: "ht-route-packet",
- animationDuration: "2.2s",
- animationTimingFunction: "linear",
- animationDelay: `${packet * 1.1}s`,
- animationIterationCount: "infinite",
- animationPlayState,
- }}
- />
- ))}
-
- <div
- className="absolute flex w-[150px] -translate-x-full -translate-y-1/2 flex-col gap-2 rounded-[14px] bg-white p-2.5"
- style={{
- left: SOURCE.x - 6,
- top: SOURCE.y,
- boxShadow:
- "0 0 0 1px rgba(17,17,17,0.06), 0 20px 40px -24px rgba(17,17,17,0.5)",
- }}
- >
- <div
- className="relative h-[68px] overflow-hidden rounded-[9px]"
- style={{
- background: "linear-gradient(135deg,#71a3f5 0%,#b7d4fa 100%)",
- }}
- >
- <span className="absolute inset-x-4 inset-y-3 rounded-[5px] bg-white/95 shadow-[0_8px_20px_-10px_rgba(16,24,40,0.5)]">
- <span className="absolute left-2 top-2 h-[3px] w-10 rounded-full bg-[rgba(17,17,17,0.35)]" />
- <span className="absolute left-2 top-[15px] h-[3px] w-16 rounded-full bg-[rgba(17,17,17,0.12)]" />
- </span>
- <span className="absolute bottom-2 left-2 size-4 rounded-full bg-[#111111] ring-2 ring-white/70" />
- </div>
- <div className="flex items-center gap-2 px-0.5">
- <CapLogoMark className="size-4 shrink-0" />
- <span className="min-w-0 flex-1 leading-tight">
- <span className="block truncate text-[12px] font-medium text-[#111111]">
- team-update.cap
- </span>
- <span className="block text-[10.5px] text-[rgba(17,17,17,0.5)]">
- 4K · 1.2 GB
- </span>
- </span>
- </div>
- </div>
-
- {DESTINATIONS.map((destination, i) => {
- const on = i === active;
- const tone = MODE_THEME[destination.mode];
- return (
- <div
- key={destination.key}
- className={classNames(
- "absolute flex items-center gap-3 rounded-[12px] px-3 transition-[background-color,box-shadow,transform] duration-500",
- on ? "bg-white" : "bg-white/60",
- )}
- style={
- {
- left: NODE.x,
- top: nodeY(i),
- width: NODE.w,
- height: NODE.h,
- "--ht-glow": tone.pill,
- boxShadow: on
- ? `0 0 0 1.5px ${tone.accent}, 0 18px 36px -22px rgba(17,17,17,0.5)`
- : "0 0 0 1px rgba(17,17,17,0.06)",
- animationName: on ? "ht-route-land" : "none",
- animationDuration: "2.2s",
- animationTimingFunction: "ease-out",
- animationIterationCount: "infinite",
- animationPlayState,
- } as React.CSSProperties
- }
- >
- <span
- className="grid size-9 shrink-0 place-items-center rounded-[9px]"
- style={{ background: on ? tone.chip : "#F1F4F8" }}
- >
- {destination.icon}
- </span>
- <span
- className={classNames(
- "truncate text-[13px] font-medium transition-colors duration-300",
- on ? "text-[#111111]" : "text-[rgba(17,17,17,0.6)]",
- )}
- >
- {destination.label}
- </span>
- </div>
- );
- })}
- </Scaled>
- );
-};
-
-export const Ownership = () => {
- const [active, setActive] = useState(0);
- const [pinned, setPinned] = useState(false);
- const cardRef = useRef<HTMLDivElement | null>(null);
- const inView = useInView(cardRef);
- const pageVisible = usePageVisible();
- const reducedMotion = useReducedMotion();
- const playing = inView && pageVisible && !reducedMotion;
-
- useEffect(() => {
- if (!playing || pinned) return;
- const timer = setInterval(
- () => setActive((current) => (current + 1) % DESTINATIONS.length),
- 3200,
- );
- return () => clearInterval(timer);
- }, [playing, pinned]);
-
- useEffect(() => {
- if (!pinned) return;
- const release = setTimeout(() => setPinned(false), 9000);
- return () => clearTimeout(release);
- }, [pinned]);
-
- const choose = (i: number) => {
- setActive(i);
- setPinned(true);
- };
-
- return (
- <section className="px-5 py-20 lg:py-28">
- <div className="mx-auto max-w-[1200px]">
- <div className="mx-auto flex max-w-[760px] flex-col items-center text-center">
- <Eyebrow accent={MODE_THEME.studio.accent}>Open source</Eyebrow>
- <h2
- className={`${H_SECTION} mt-6 text-balance text-[clamp(38px,5vw,56px)]`}
- >
- Your recordings. Actually yours.
- </h2>
- <p
- className={`${BODY_TEXT} mt-6 max-w-[600px] text-balance text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[17.5px]`}
- >
- Cap is open source, and every recording goes exactly where you say.
- Use Cap Cloud, point it at your own bucket or Drive, or self host
- the whole thing.
- </p>
- </div>
-
- <div
- ref={cardRef}
- className="mt-14 grid gap-4 rounded-[24px] p-3 lg:grid-cols-[1.3fr_1fr] lg:p-4"
- style={grainBg(BAND)}
- >
- <div className="flex items-center rounded-[16px] bg-[#F8FAFC] px-6 py-8 shadow-[0_0_0_1px_rgba(17,17,17,0.04)] lg:px-10">
- <StorageRoute active={active} playing={playing} />
- </div>
-
- <div className="flex flex-col gap-2">
- {DESTINATIONS.map((destination, i) => {
- const on = i === active;
- const tone = MODE_THEME[destination.mode];
- return (
- <button
- key={destination.key}
- type="button"
- aria-pressed={on}
- onClick={() => choose(i)}
- className={classNames(
- "flex flex-1 items-start gap-3.5 rounded-[14px] px-4 py-4 text-left transition-[background-color,box-shadow] duration-300 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111]",
- on
- ? "bg-white shadow-[0_0_0_1px_rgba(17,17,17,0.06),0_18px_36px_-26px_rgba(17,17,17,0.45)]"
- : "hover:bg-white/50",
- )}
- >
- <span
- className="mt-[3px] inline-block size-[7px] shrink-0 transition-colors duration-300"
- style={{
- background: on ? tone.accent : "rgba(17,17,17,0.18)",
- }}
- />
- <span className="min-w-0">
- <span
- className={classNames(
- "block text-[16px] font-medium transition-colors duration-300",
- on ? "text-[#111111]" : "text-[rgba(17,17,17,0.55)]",
- )}
- >
- {destination.name}
- </span>
- <span
- className={classNames(
- BODY_TEXT,
- "mt-1 block text-[14.5px] leading-[1.45] transition-colors duration-300",
- on
- ? "text-[rgba(17,17,17,0.72)]"
- : "text-[rgba(17,17,17,0.4)]",
- )}
- >
- {destination.detail}
- </span>
- </span>
- </button>
- );
- })}
- </div>
- </div>
-
- <div className="mt-4 grid gap-4 lg:grid-cols-[1.3fr_1fr]">
- <figure className="flex flex-col justify-center rounded-[20px] bg-white p-8 shadow-[0_0_0_1px_rgba(17,17,17,0.05)] lg:p-10">
- <blockquote
- className={`${BODY_TEXT} text-balance text-[clamp(19px,1.9vw,24px)] leading-[1.4] text-[#111111]`}
- >
- &ldquo;{QUOTE.content}&rdquo;
- </blockquote>
- <figcaption className="mt-7 flex flex-wrap items-center gap-2">
- <span className="inline-flex items-center gap-2 rounded-full bg-[#EDF1F6] py-1.5 pl-1.5 pr-4">
- <Image
- src={QUOTE.image}
- alt=""
- width={28}
- height={28}
- className="size-7 rounded-full object-cover"
- />
- <span className="text-[13.5px] font-medium text-[#111111]">
- {QUOTE.name}
- </span>
- </span>
- <span className="rounded-full bg-[#EDF1F6] px-4 py-2 text-[13.5px] text-[rgba(17,17,17,0.55)]">
- {QUOTE.handle}
- </span>
- </figcaption>
- </figure>
-
- <ul
- className="grid grid-cols-1 gap-px overflow-hidden rounded-[20px] sm:grid-cols-2"
- style={grainBg(BAND)}
- >
- {PROOF.map((item) => (
- <li key={item.label} className="flex flex-col gap-2 p-6">
- <p
- className={classNames(
- EYEBROW,
- "flex items-center gap-2 text-[11px] text-[#111111]",
- )}
- >
- <span
- aria-hidden="true"
- className="inline-block size-[7px]"
- style={{ background: MODE_THEME.studio.accent }}
- />
- {item.label}
- </p>
- <p
- className={`${BODY_TEXT} text-[14.5px] leading-[1.45] text-[rgba(17,17,17,0.72)]`}
- >
- {item.body}
- </p>
- </li>
- ))}
- </ul>
- </div>
- </div>
- </section>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/Platforms.tsx b/apps/web/components/pages/HomeTwo/Platforms.tsx
deleted file mode 100644
index 75c62d533..000000000
--- a/apps/web/components/pages/HomeTwo/Platforms.tsx
+++ /dev/null
@@ -1,239 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import { useDetectPlatform } from "hooks/useDetectPlatform";
-import { ArrowUpRight } from "lucide-react";
-import Image from "next/image";
-import Link from "next/link";
-import { getDownloadUrl } from "@/utils/platform";
-import { Eyebrow } from "./Eyebrow";
-import { AppleGlyph } from "./glyphs";
-import {
- BAND,
- BODY_TEXT,
- BTN_PRIMARY,
- grainBg,
- H_CARD,
- H_SECTION,
- MODE_THEME,
- MONO,
- meshStyle,
-} from "./theme";
-
-const WindowsGlyph = ({ className }: { className?: string }) => (
- <svg
- aria-hidden="true"
- className={className}
- viewBox="0 0 24 24"
- fill="currentColor"
- >
- <path d="M0,0H11.377V11.372H0ZM12.623,0H24V11.372H12.623ZM0,12.623H11.377V24H0Zm12.623,0H24V24H12.623" />
- </svg>
-);
-
-const TERMINAL = [
- { prompt: true, text: "cap record start --mode studio --duration 30" },
- { prompt: true, text: "cap export ./recording.cap --quality 4k" },
- { prompt: true, text: "cap upload ./recording.mp4 --json" },
- { prompt: false, text: '{ "url": "https://cap.so/s/x7f2k9" }' },
-];
-
-const Tile = ({
- href,
- title,
- body,
- icon,
-}: {
- href: string;
- title: string;
- body: string;
- icon: React.ReactNode;
-}) => (
- <Link
- href={href}
- className="group flex flex-col justify-between rounded-[20px] p-7 transition-colors duration-200 hover:bg-[#E5EAF1] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111]"
- style={grainBg(BAND)}
- >
- <span className="flex items-start justify-between">
- {icon}
- <ArrowUpRight className="size-4 text-[rgba(17,17,17,0.4)] transition-colors duration-200 group-hover:text-[#111111]" />
- </span>
- <span className="mt-10 block">
- <span className="block text-[19px] font-normal leading-[1.1] tracking-[-0.02em] text-[#111111]">
- {title}
- </span>
- <span
- className={`${BODY_TEXT} mt-2 block text-[14.5px] leading-[1.45] text-[rgba(17,17,17,0.7)]`}
- >
- {body}
- </span>
- </span>
- </Link>
-);
-
-export const Platforms = () => {
- const { platform, isIntel } = useDetectPlatform();
- const current = platform ?? "macos";
-
- const desktops = [
- {
- key: "macos",
- name: "macOS",
- note: "Apple silicon and Intel. macOS 13.1 or newer.",
- href: getDownloadUrl("macos", isIntel),
- glyph: <AppleGlyph className="size-7" />,
- mode: "instant" as const,
- },
- {
- key: "windows",
- name: "Windows",
- note: "Windows 10 or newer.",
- href: getDownloadUrl("windows", false),
- glyph: <WindowsGlyph className="size-6" />,
- mode: "studio" as const,
- },
- {
- key: "linux",
- name: "Linux",
- note: "Debian and Ubuntu, as a .deb package.",
- href: getDownloadUrl("linux", false),
- glyph: (
- <Image
- src="/logos/os/linux.svg"
- alt=""
- width={28}
- height={28}
- className="size-7"
- />
- ),
- mode: "screenshot" as const,
- },
- ];
-
- return (
- <section className="px-5 py-20 lg:py-28">
- <div className="mx-auto max-w-[1200px]">
- <div className="mx-auto flex max-w-[760px] flex-col items-center text-center">
- <Eyebrow accent={MODE_THEME.screenshot.accent}>
- Every platform
- </Eyebrow>
- <h2
- className={`${H_SECTION} mt-6 text-balance text-[clamp(38px,5vw,56px)]`}
- >
- Native on Mac, Windows and Linux
- </h2>
- <p
- className={`${BODY_TEXT} mt-6 max-w-[540px] text-balance text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[17.5px]`}
- >
- A real desktop app on all three, with the same recorder, editor, and
- share link. Plus a Chrome extension for the browser and a CLI for
- everything else.
- </p>
- </div>
-
- <div className="mt-14 grid gap-4 md:grid-cols-3">
- {desktops.map((desktop) => {
- const yours = desktop.key === current;
- return (
- <div
- key={desktop.key}
- className="flex flex-col justify-between rounded-[24px] p-8 md:min-h-[300px]"
- style={meshStyle(MODE_THEME[desktop.mode])}
- >
- <div className="flex items-start justify-between gap-4">
- <span className="grid size-14 place-items-center rounded-[16px] bg-white text-[#111111] shadow-[0_0_0_1px_rgba(17,17,17,0.06),0_14px_30px_-18px_rgba(17,17,17,0.5)]">
- {desktop.glyph}
- </span>
- {yours ? (
- <span
- className={classNames(
- MONO,
- "rounded-full bg-white/70 px-3 py-1.5 text-[11px] uppercase leading-none tracking-[0.05em] text-[rgba(17,17,17,0.7)]",
- )}
- >
- Your device
- </span>
- ) : null}
- </div>
- <div className="mt-10">
- <h3 className={`${H_CARD} text-[clamp(28px,2.6vw,34px)]`}>
- {desktop.name}
- </h3>
- <p
- className={`${BODY_TEXT} mt-2 text-[15.5px] leading-[1.45] text-[rgba(17,17,17,0.7)]`}
- >
- {desktop.note}
- </p>
- <Link
- href={desktop.href}
- className={classNames(
- BTN_PRIMARY,
- "mt-6 h-[46px] px-5 text-[15px]",
- )}
- >
- Download for {desktop.name}
- </Link>
- </div>
- </div>
- );
- })}
- </div>
-
- <div className="mt-4 grid gap-4 md:grid-cols-2">
- <Tile
- href="/download"
- title="Chrome extension"
- body="Record a tab or your whole screen without leaving the browser."
- icon={
- <Image
- src="/logos/browsers/google-chrome.svg"
- alt=""
- width={36}
- height={36}
- className="size-9"
- />
- }
- />
- <Link
- href="/docs"
- className="group flex flex-col rounded-[20px] p-7 transition-colors duration-200 hover:bg-[#E5EAF1] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111]"
- style={grainBg(BAND)}
- >
- <span className="flex items-start justify-between">
- <span className="block text-[19px] font-normal leading-[1.1] tracking-[-0.02em] text-[#111111]">
- Built for developers
- </span>
- <ArrowUpRight className="size-4 shrink-0 text-[rgba(17,17,17,0.4)] transition-colors duration-200 group-hover:text-[#111111]" />
- </span>
- <span
- className={`${BODY_TEXT} mt-2 block text-[14.5px] leading-[1.45] text-[rgba(17,17,17,0.7)]`}
- >
- Record, export, and publish from the terminal, the API, or the MCP
- server.
- </span>
- <span
- className={classNames(
- MONO,
- "mt-5 block rounded-[12px] bg-[#111111] p-4 text-[11.5px] leading-[1.7] text-[#F8FAFC]",
- )}
- >
- {TERMINAL.map((line) => (
- <span
- key={line.text}
- className="block whitespace-pre-wrap break-words"
- >
- {line.prompt ? (
- <span className="text-[rgba(255,255,255,0.4)]">$ </span>
- ) : null}
- <span className={line.prompt ? undefined : "text-[#8FDCBB]"}>
- {line.text}
- </span>
- </span>
- ))}
- </span>
- </Link>
- </div>
- </div>
- </section>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/Pricing.tsx b/apps/web/components/pages/HomeTwo/Pricing.tsx
index 71f01200e..05bc58de3 100644
--- a/apps/web/components/pages/HomeTwo/Pricing.tsx
+++ b/apps/web/components/pages/HomeTwo/Pricing.tsx
@@ -6,17 +6,12 @@ import Link from "next/link";
import { useState } from "react";
import { trackEvent } from "@/app/utils/analytics";
import { homepageCopy } from "@/data/homepage-copy";
-import { Eyebrow } from "./Eyebrow";
import {
- BAND,
BODY_TEXT,
BTN_PRIMARY,
BTN_SECONDARY,
- CARD_BG,
- grainBg,
H_SECTION,
MODE_THEME,
- MONO,
type ModeTheme,
} from "./theme";
@@ -60,18 +55,12 @@ const PlanCard = ({
primary?: boolean;
theme: ModeTheme;
}) => (
- <div
- className="flex flex-col rounded-[14px] p-7 lg:p-8"
- style={grainBg(CARD_BG)}
- >
+ <div className="flex flex-col rounded-[20px] bg-white p-7 shadow-[0_0_0_1px_rgba(17,17,17,0.06),0_1px_2px_rgba(17,17,17,0.03)] lg:p-8">
<div className="flex items-center gap-2.5">
<span className="text-[15px] font-medium text-[#111111]">{name}</span>
{badge ? (
<span
- className={classNames(
- MONO,
- "rounded-full px-2.5 py-1 text-[10.5px] uppercase leading-none tracking-[0.05em]",
- )}
+ className="rounded-full px-2.5 py-1 text-[12px] leading-none"
style={{ background: theme.chip, color: theme.glyph }}
>
{badge}
@@ -89,9 +78,9 @@ const PlanCard = ({
<span className="text-[46px] font-normal leading-none tracking-[-0.03em] text-[#111111]">
{price}
</span>
- <span className="text-[14px] text-[rgba(17,17,17,0.5)]">{cadence}</span>
+ <span className="text-[14px] text-[rgba(17,17,17,0.64)]">{cadence}</span>
</div>
- <p className="mt-2 text-[13.5px] text-[rgba(17,17,17,0.5)]">{note}</p>
+ <p className="mt-2 text-[13.5px] text-[rgba(17,17,17,0.64)]">{note}</p>
<Link
href={href}
@@ -127,19 +116,18 @@ export const Pricing = () => {
<section className="px-5 py-20 lg:py-28">
<div className="mx-auto max-w-[1060px]">
<div className="mx-auto flex max-w-[760px] flex-col items-center text-center">
- <Eyebrow accent={MODE_THEME.instant.accent}>Pricing</Eyebrow>
<h2
- className={`${H_SECTION} mt-6 text-balance text-[clamp(38px,5vw,56px)]`}
+ className={`${H_SECTION} text-balance text-[clamp(34px,4.6vw,54px)] leading-[1.02]`}
>
Simple, honest pricing
</h2>
<p
- className={`${BODY_TEXT} mt-6 max-w-[560px] text-balance text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[17.5px]`}
+ className={`${BODY_TEXT} mt-5 max-w-[560px] text-balance text-[17px] leading-[1.5] text-[rgba(17,17,17,0.64)] sm:text-[18px]`}
>
{subtitle}
</p>
- <div className="mt-8 flex items-center gap-1 rounded-full border border-[#DDE4EB] bg-white/70 p-1">
+ <div className="mt-8 flex items-center gap-0.5 rounded-full bg-[#EEF1F5] p-1 shadow-[inset_0_0_0_1px_rgba(17,17,17,0.04)]">
{(["annual", "monthly"] as const).map((option) => {
const active = billing === option;
return (
@@ -151,27 +139,21 @@ export const Pricing = () => {
className={classNames(
"h-8 rounded-full px-4 text-[13px] font-medium transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111] focus-visible:ring-offset-1 focus-visible:ring-offset-white",
active
- ? "text-[#111111]"
- : "text-[rgba(17,17,17,0.5)] hover:bg-[#EDF1F6] hover:text-[#111111]",
+ ? "bg-white text-[#111111] shadow-[0_1px_2px_rgba(17,17,17,0.08),0_0_0_1px_rgba(17,17,17,0.06)]"
+ : "text-[rgba(17,17,17,0.64)] hover:text-[#111111]",
)}
- style={
- active ? { background: MODE_THEME.instant.chip } : undefined
- }
>
{option === "annual" ? "Annual" : "Monthly"}
</button>
);
})}
</div>
- <p className="mt-4 text-[13.5px] text-[rgba(17,17,17,0.5)]">
+ <p className="mt-4 text-[13.5px] text-[rgba(17,17,17,0.64)]">
{lovedBy}
</p>
</div>
- <div
- className="mt-14 grid gap-3 rounded-[20px] p-3 lg:grid-cols-2 lg:gap-4 lg:p-4"
- style={grainBg(BAND)}
- >
+ <div className="mt-14 grid gap-3 lg:grid-cols-2">
<PlanCard
name={commercial.title}
blurb={BLURB.commercial}
diff --git a/apps/web/components/pages/HomeTwo/Testimonials.tsx b/apps/web/components/pages/HomeTwo/Testimonials.tsx
index 1bf400f02..e3408fad0 100644
--- a/apps/web/components/pages/HomeTwo/Testimonials.tsx
+++ b/apps/web/components/pages/HomeTwo/Testimonials.tsx
@@ -67,7 +67,7 @@ export const Testimonials = ({ eyebrow = true }: { eyebrow?: boolean }) => (
<span className="block truncate text-[14px] font-medium text-[#111111]">
{quote.name}
</span>
- <span className="block truncate text-[13px] text-[rgba(17,17,17,0.5)]">
+ <span className="block truncate text-[13px] text-[rgba(17,17,17,0.64)]">
{quote.handle}
</span>
</span>
diff --git a/apps/web/components/pages/HomeTwo/Workflow.tsx b/apps/web/components/pages/HomeTwo/Workflow.tsx
deleted file mode 100644
index 716f92275..000000000
--- a/apps/web/components/pages/HomeTwo/Workflow.tsx
+++ /dev/null
@@ -1,173 +0,0 @@
-import { classNames } from "@cap/utils/helpers";
-import Link from "next/link";
-import type { ComponentType } from "react";
-import {
- InstantIcon,
- ScreenshotIcon,
- StudioIcon,
-} from "@/components/pages/HomePage/modeIcons";
-import { Eyebrow } from "./Eyebrow";
-import {
- BAND,
- BODY_TEXT,
- BTN_PRIMARY,
- CARD_BG,
- grainBg,
- H_SECTION,
- MODE_THEME,
- MONO,
- type ModeKey,
- type ModeTheme,
-} from "./theme";
-
-type Mode = {
- key: ModeKey;
- name: string;
- promise: string;
- steps: string[];
- bestFor: string;
- Icon: ComponentType<{ className?: string }>;
-};
-
-const MODES: Mode[] = [
- {
- key: "instant",
- name: "Instant Mode",
- promise: "From recording to shared in one step",
- steps: [
- "Press record. Cap uploads while you talk, so there is nothing to wait for.",
- "Press stop. The share link is already on your clipboard.",
- "Paste it anywhere. Viewers watch in the browser, no account needed.",
- ],
- bestFor: "Bug reports, quick answers, async standups",
- Icon: InstantIcon,
- },
- {
- key: "studio",
- name: "Studio Mode",
- promise: "Full quality, polished before anyone sees it",
- steps: [
- "Record locally in 4K. Screen, camera, and mic each get their own track.",
- "The editor opens when you stop: backgrounds, auto zoom, cursor effects, captions.",
- "Export in 4K, or share it as a Cap link like everything else.",
- ],
- bestFor: "Product demos, tutorials, launch videos",
- Icon: StudioIcon,
- },
- {
- key: "screenshot",
- name: "Screenshot Mode",
- promise: "Stills that look designed",
- steps: [
- "Hit the hotkey and grab any window or area.",
- "Beautify with one click: background, padding, shadow.",
- "It's on your clipboard, ready to paste or share as a link.",
- ],
- bestFor: "Docs, pull requests, social posts",
- Icon: ScreenshotIcon,
- },
-];
-
-const StepRow = ({
- index,
- text,
- theme,
-}: {
- index: number;
- text: string;
- theme: ModeTheme;
-}) => (
- <li className="flex items-start gap-3">
- <span
- className="mt-px flex size-6 shrink-0 items-center justify-center rounded-full text-[12px] font-medium tabular-nums"
- style={{ background: theme.chip, color: theme.glyph }}
- >
- {index}
- </span>
- <span className="text-[13.5px] leading-snug text-[rgba(17,17,17,0.72)]">
- {text}
- </span>
- </li>
-);
-
-export const Workflow = () => (
- // biome-ignore lint/correctness/useUniqueElementIds: anchor target for the demo's "Learn more"
- <section id="workflow" className="scroll-mt-10 px-5 py-20 lg:py-28">
- <div className="mx-auto max-w-[1200px]">
- <div className="mx-auto flex max-w-[760px] flex-col items-center text-center">
- <Eyebrow accent={MODE_THEME.instant.accent}>Cap has 3 modes</Eyebrow>
- <h2
- className={`${H_SECTION} mt-6 text-balance text-[clamp(38px,5vw,56px)]`}
- >
- One app for every workflow
- </h2>
- <p
- className={`${BODY_TEXT} mt-6 max-w-[600px] text-balance text-[16.5px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[17.5px]`}
- >
- Record and share with Instant, edit your videos with Studio, or
- capture and customize images with Screenshot.
- </p>
- <Link href="/download" className={classNames(BTN_PRIMARY, "mt-8")}>
- Download Cap free
- </Link>
- </div>
-
- <div className="mt-16 rounded-[20px] p-3 lg:p-4" style={grainBg(BAND)}>
- <div className="grid gap-3 md:grid-cols-3 lg:gap-4">
- {MODES.map((mode) => {
- const theme = MODE_THEME[mode.key];
- return (
- <div
- key={mode.key}
- className="flex flex-col rounded-[14px] p-6 text-left"
- style={grainBg(CARD_BG)}
- >
- <div className="flex items-center gap-2.5">
- <span
- className="grid size-8 shrink-0 place-items-center rounded-[8px]"
- style={{ background: theme.chip, color: theme.glyph }}
- >
- <mode.Icon className="size-4" />
- </span>
- <span className="text-[15px] font-medium text-[#111111]">
- {mode.name}
- </span>
- </div>
-
- <h3 className="mt-5 max-w-[300px] text-balance text-[22px] font-normal leading-[1.1] tracking-[-0.02em] text-[#111111]">
- {mode.promise}
- </h3>
-
- <div className="flex flex-1 flex-col justify-between">
- <div>
- <ol className="mt-5 space-y-3">
- {mode.steps.map((text, i) => (
- <StepRow
- key={text}
- index={i + 1}
- text={text}
- theme={theme}
- />
- ))}
- </ol>
-
- <div className="mt-6 border-t border-[#E1E7EE] pt-4">
- <p
- className={`${MONO} text-[11px] font-normal uppercase leading-none tracking-[0.05em] text-[rgba(17,17,17,0.45)]`}
- >
- Best for
- </p>
- <p className="mt-2 text-[13.5px] leading-snug text-[rgba(17,17,17,0.72)]">
- {mode.bestFor}
- </p>
- </div>
- </div>
- </div>
- </div>
- );
- })}
- </div>
- </div>
- </div>
- </section>
-);
diff --git a/apps/web/components/pages/HomeTwo/demo/CapEditorWindow.tsx b/apps/web/components/pages/HomeTwo/demo/CapEditorWindow.tsx
index 51f231fd5..7b804f631 100644
--- a/apps/web/components/pages/HomeTwo/demo/CapEditorWindow.tsx
+++ b/apps/web/components/pages/HomeTwo/demo/CapEditorWindow.tsx
@@ -34,7 +34,7 @@ import {
LucideZoomOut,
} from "./capIcons";
import { TrafficLights, WindowsCaptionControls } from "./chrome";
-import { useVideoAttrs, VIDEO_POSTERS } from "./media";
+import { PresenterSources, useVideoAttrs, VIDEO_POSTERS } from "./media";
import { useIsWindowsDemo } from "./platform";
const C = {
@@ -64,7 +64,7 @@ const WALLPAPERS = [
].map((city) => ({
id: city,
thumb: `/backgrounds/thumbs/${city}.webp`,
- full: `/backgrounds/${city}.webp`,
+ full: `/backgrounds/landing/${city}.webp`,
}));
const WALLPAPER_THEMES = [
@@ -419,12 +419,13 @@ export const CapEditorWindow = ({
<video
ref={camVideoRef}
className="h-full w-full object-cover"
- src="/videos/home-two/webcam.mp4"
muted
loop
playsInline
{...cameraVideo}
- />
+ >
+ <PresenterSources />
+ </video>
</div>
{canvasChildren}
</div>
diff --git a/apps/web/components/pages/HomeTwo/demo/CapSurfaces.tsx b/apps/web/components/pages/HomeTwo/demo/CapSurfaces.tsx
index 85576b285..f2580c400 100644
--- a/apps/web/components/pages/HomeTwo/demo/CapSurfaces.tsx
+++ b/apps/web/components/pages/HomeTwo/demo/CapSurfaces.tsx
@@ -21,7 +21,7 @@ import {
CapTrash,
CapX,
} from "./capIcons";
-import { useVideoAttrs, VIDEO_POSTERS } from "./media";
+import { PresenterSources, useVideoAttrs, VIDEO_POSTERS } from "./media";
import { OS_FONT, useIsWindowsDemo } from "./platform";
const ToolButton = ({
@@ -178,12 +178,13 @@ export const CameraWindow = ({
<video
ref={videoRef}
className="h-full w-full object-cover"
- src="/videos/home-two/webcam.mp4"
muted
loop
playsInline
{...videoAttrs}
- />
+ >
+ <PresenterSources />
+ </video>
</div>
</div>
);
diff --git a/apps/web/components/pages/HomeTwo/demo/DesktopDemo.tsx b/apps/web/components/pages/HomeTwo/demo/DesktopDemo.tsx
index afbe89cf5..ad85dae60 100644
--- a/apps/web/components/pages/HomeTwo/demo/DesktopDemo.tsx
+++ b/apps/web/components/pages/HomeTwo/demo/DesktopDemo.tsx
@@ -1361,7 +1361,7 @@ export const DesktopDemo = ({
onClick={(event) => {
event.stopPropagation();
document
- .getElementById("workflow")
+ .getElementById("instant")
?.scrollIntoView({ behavior: "smooth" });
}}
className="pointer-events-auto flex h-[56px] cursor-pointer items-center gap-2 rounded-full border border-white/40 bg-black/30 px-9 text-[20px] font-medium text-white backdrop-blur-md transition-colors duration-200 hover:bg-black/50 focus-visible:outline-none focus-visible:ring-4 focus-visible:ring-white/70"
diff --git a/apps/web/components/pages/HomeTwo/demo/media.tsx b/apps/web/components/pages/HomeTwo/demo/media.tsx
index c488eb532..39d1636c2 100644
--- a/apps/web/components/pages/HomeTwo/demo/media.tsx
+++ b/apps/web/components/pages/HomeTwo/demo/media.tsx
@@ -8,8 +8,22 @@ export const SceneMediaProvider = SceneMediaContext.Provider;
export const useSceneMedia = () => useContext(SceneMediaContext);
+export const PRESENTER = {
+ av1: "/videos/landing/presenter.webm",
+ mp4: "/videos/landing/presenter.mp4",
+ poster: "/videos/landing/presenter-poster.webp",
+ avatar: "/videos/landing/presenter-avatar.webp",
+} as const;
+
+export const PresenterSources = () => (
+ <>
+ <source src={PRESENTER.av1} type='video/webm; codecs="av01.0.05M.08"' />
+ <source src={PRESENTER.mp4} type="video/mp4" />
+ </>
+);
+
export const VIDEO_POSTERS = {
- webcam: "/videos/home-two/webcam-poster.jpg",
+ webcam: PRESENTER.poster,
screen: "/illustrations/homepage-animation-poster.jpg",
} as const;
diff --git a/apps/web/components/pages/HomeTwo/fonts.ts b/apps/web/components/pages/HomeTwo/fonts.ts
index da3b85942..d428db2ce 100644
--- a/apps/web/components/pages/HomeTwo/fonts.ts
+++ b/apps/web/components/pages/HomeTwo/fonts.ts
@@ -7,7 +7,6 @@ import {
export const htSans = Instrument_Sans({
subsets: ["latin"],
- weight: ["400", "500"],
display: "swap",
variable: "--font-ht-sans",
});
@@ -20,10 +19,13 @@ export const htSerif = Source_Serif_4({
variable: "--font-ht-serif",
});
+// Geist and DM Mono only appear inside the animated stages below the fold, so
+// they are fetched when first used instead of competing with the hero fonts.
export const htGeist = Geist({
subsets: ["latin"],
weight: ["400", "500", "600"],
display: "swap",
+ preload: false,
variable: "--font-ht-geist",
});
@@ -31,5 +33,6 @@ export const htMono = DM_Mono({
subsets: ["latin"],
weight: ["400"],
display: "swap",
+ preload: false,
variable: "--font-ht-mono",
});
diff --git a/apps/web/components/pages/HomeTwo/index.tsx b/apps/web/components/pages/HomeTwo/index.tsx
index aa5f4a8a4..fce3f8916 100644
--- a/apps/web/components/pages/HomeTwo/index.tsx
+++ b/apps/web/components/pages/HomeTwo/index.tsx
@@ -1,20 +1,24 @@
-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";
import { htMono, htSans, htSerif } from "@/components/pages/HomeTwo/fonts";
import { Hero } from "@/components/pages/HomeTwo/Hero";
import { LoomBridge } from "@/components/pages/HomeTwo/LoomBridge";
import { ModeWalkthrough } from "@/components/pages/HomeTwo/ModeWalkthrough";
-import { Ownership } from "@/components/pages/HomeTwo/Ownership";
-import { Platforms } from "@/components/pages/HomeTwo/Platforms";
import { Pricing } from "@/components/pages/HomeTwo/Pricing";
import { Testimonials } from "@/components/pages/HomeTwo/Testimonials";
import { BAND, CREAM, grainBg, SHELL } from "@/components/pages/HomeTwo/theme";
-import { Workflow } from "@/components/pages/HomeTwo/Workflow";
import { HomeTwoSchema } from "./Schema";
+import { FADE_CSS, POP_CSS } from "./showcase/layout";
+import { PlatformsSection } from "./showcase/platforms";
+import {
+ AgentsSection,
+ AiSection,
+ InstantSection,
+ OwnershipSection,
+ ScreenshotSection,
+ StudioSection,
+} from "./showcase/sections";
const CARD_RADIUS = "rounded-[24px]";
@@ -28,6 +32,7 @@ export function HomeTwoPage() {
className={`${htSans.className} ${htSans.variable} ${htSerif.variable} ${htMono.variable} text-[#111111]`}
style={grainBg(SHELL)}
>
+ <style>{FADE_CSS + POP_CSS}</style>
<HomeTwoSchema />
<DiscordLinkPreview />
<div className="px-2.5 pb-2.5 pt-[68px] sm:px-5 sm:pb-4 lg:px-8 lg:pt-[76px]">
@@ -42,15 +47,16 @@ export function HomeTwoPage() {
<Hero />
<ModeWalkthrough />
</div>
- <Workflow />
- <DeepDives />
- <Features />
- <Ownership />
- <Platforms />
- <Agents />
- <Testimonials />
+ <InstantSection />
+ <StudioSection />
+ <ScreenshotSection />
+ <AiSection />
+ <OwnershipSection />
+ <PlatformsSection />
+ <AgentsSection />
+ <Testimonials eyebrow={false} />
<Pricing />
- <Faq />
+ <Faq eyebrow={false} />
<LoomBridge />
<FinalCta />
</div>
diff --git a/apps/web/components/pages/HomeTwo/scenes/AgentScene.tsx b/apps/web/components/pages/HomeTwo/scenes/AgentScene.tsx
index 734efb61e..f66930540 100644
--- a/apps/web/components/pages/HomeTwo/scenes/AgentScene.tsx
+++ b/apps/web/components/pages/HomeTwo/scenes/AgentScene.tsx
@@ -396,7 +396,7 @@ export const AgentScene = (props: SceneProps) => {
Checkout bug repro
</span>
<span className="block text-[12px] text-[rgba(17,17,17,0.55)]">
- cap.so/s/x7f2k9 · 0:20 · AI summary ready
+ Cap.so/s/x7f2k9 · 0:20 · AI summary ready
</span>
</span>
</div>
@@ -429,7 +429,7 @@ export const AgentScene = (props: SceneProps) => {
commentVisible={ui.comment}
videoRef={shareVideoRef}
title="Checkout bug repro"
- url="cap.so/s/x7f2k9"
+ url="Cap.so/s/x7f2k9"
duration="0:20"
/>
</div>
diff --git a/apps/web/components/pages/HomeTwo/seo.ts b/apps/web/components/pages/HomeTwo/seo.ts
index 955042454..53ec2d884 100644
--- a/apps/web/components/pages/HomeTwo/seo.ts
+++ b/apps/web/components/pages/HomeTwo/seo.ts
@@ -1,3 +1,4 @@
+import { homepageCopy } from "@/data/homepage-copy";
import { PRICING } from "@/data/pricing";
export const homepageSeo = {
@@ -63,12 +64,25 @@ export const homepageSchema = {
mainEntityOfPage: { "@id": "https://cap.so/#webpage" },
featureList: [
"Screen, webcam, and audio recording",
- "Instant video sharing",
- "Local recording with a built-in video editor",
- "Screenshot capture and annotation",
- "Loom video importer for single videos and CSV libraries",
- "Custom backgrounds and automatic zoom",
+ "Instant share links that upload while you record",
+ "Local 4K recording at 60fps with a built-in video editor",
+ "Automatic zoom on clicks",
+ "Cursor smoothing and click effects",
+ "Camera layouts: webcam bubble, split screen, camera only",
+ "Automatic captions in 19 languages, generated on device",
+ "Blur and pixelate sensitive information",
+ "Custom backgrounds, padding, rounded corners, and shadows",
+ "Animated text, titles, and lower thirds",
+ "3D camera effects and color grading",
+ "Trim, split, and speed controls from 0.25x to 8x",
+ "MP4 and GIF export",
+ "Screenshot capture with annotation tools",
+ "AI titles, summaries, chapters, and transcripts",
+ "Timestamped comments, reactions, and viewer analytics",
+ "Password protected links and custom domains",
"Google Drive and S3 storage integrations",
+ "CLI and MCP server for AI agents",
+ "Loom video importer for single videos and CSV libraries",
"Open source and self-hostable",
],
offers: [
@@ -99,5 +113,15 @@ export const homepageSchema = {
},
],
},
+ {
+ "@type": "FAQPage",
+ "@id": "https://cap.so/#faq",
+ isPartOf: { "@id": "https://cap.so/#webpage" },
+ mainEntity: homepageCopy.faq.items.map((item) => ({
+ "@type": "Question",
+ name: item.question,
+ acceptedAnswer: { "@type": "Answer", text: item.answer },
+ })),
+ },
],
};
diff --git a/apps/web/components/pages/HomeTwo/showcase/agents.tsx b/apps/web/components/pages/HomeTwo/showcase/agents.tsx
new file mode 100644
index 000000000..41250e448
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/agents.tsx
@@ -0,0 +1,181 @@
+"use client";
+
+import { classNames } from "@cap/utils/helpers";
+import { CapLogoMark } from "../demo/capIcons";
+import { useIsWindowsDemo } from "../demo/platform";
+import { MONO } from "../theme";
+import { Loop, seg, TONES } from "./kit";
+import { CAP_TITLE } from "./share";
+import { AppBar, INVOICE, InvoicePage } from "./subjects";
+
+export const CANVAS = { w: 1040, h: 450 } as const;
+
+const PROMPT =
+ "Record a 20 second repro of the invoice bug and send me the link";
+
+const STEPS = [
+ {
+ at: 2100,
+ done: 4200,
+ command: "cap record start --mode instant --duration 20 --json",
+ result: "Recorded 0:20 of screen 1",
+ },
+ {
+ at: 4400,
+ done: 5400,
+ command: 'cap upload ./recording.cap --name "Invoice bug repro" --json',
+ result: "Uploaded, link ready",
+ },
+ {
+ at: 5600,
+ done: 6400,
+ command: "cap caps context m4k8x2qz --json",
+ result: "Read the transcript and summary",
+ },
+];
+
+const Spinner = () => (
+ <svg aria-hidden="true" viewBox="0 0 16 16" className="size-4 animate-spin">
+ <circle
+ cx="8"
+ cy="8"
+ r="6"
+ fill="none"
+ stroke="rgba(255,255,255,0.18)"
+ strokeWidth="2"
+ />
+ <path
+ d="M8 2a6 6 0 0 1 6 6"
+ fill="none"
+ stroke="#FFFFFF"
+ strokeWidth="2"
+ strokeLinecap="round"
+ />
+ </svg>
+);
+
+const Check = () => (
+ <svg aria-hidden="true" viewBox="0 0 16 16" className="size-4">
+ <circle cx="8" cy="8" r="7" fill="#22C55E" />
+ <path
+ d="M4.8 8.3l2.1 2.1 4.3-4.6"
+ fill="none"
+ stroke="#0E0F11"
+ strokeWidth="1.8"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ />
+ </svg>
+);
+
+const AgentView = ({ t }: { t: number; playing: boolean }) => {
+ const windows = useIsWindowsDemo();
+ const typed = PROMPT.slice(0, Math.floor(seg(t, 200, 1700) * PROMPT.length));
+ const answer = t > 6700;
+ const card = seg(t, 6600, 7100);
+ return (
+ <div className="absolute inset-0 bg-[#0E0F11] text-white">
+ <AppBar
+ dark
+ className="absolute inset-x-0 top-0 h-10 border-b border-white/[0.07] bg-white/[0.03]"
+ >
+ <span
+ className={classNames(
+ MONO,
+ "mx-auto flex items-center gap-2 text-[12.5px] text-white/50",
+ )}
+ >
+ <span className="size-1.5 rounded-full bg-[#22C55E]" />
+ {windows ? "agent · C:\\ledgerly" : "agent · ~/ledgerly"}
+ </span>
+ </AppBar>
+ <div
+ className={classNames(
+ MONO,
+ "absolute left-10 top-[52px] w-[560px] text-[14px] leading-[1.6]",
+ )}
+ >
+ <div className="mt-6 flex gap-3">
+ <span className="text-[rgba(255,255,255,0.4)]">›</span>
+ <span>
+ {typed}
+ {t < 1900 ? (
+ <span className="ml-0.5 inline-block h-4 w-2 translate-y-[3px] animate-pulse bg-white/80" />
+ ) : null}
+ </span>
+ </div>
+ <div className="mt-6 flex flex-col gap-4">
+ {STEPS.filter((step) => t > step.at).map((step) => {
+ const done = t > step.done;
+ return (
+ <div key={step.command} className="ht-fade-in flex gap-3">
+ <span className="mt-[3px]">
+ {done ? <Check /> : <Spinner />}
+ </span>
+ <span className="min-w-0">
+ <span className="block break-words text-[rgba(255,255,255,0.78)]">
+ {step.command}
+ </span>
+ <span className="block text-[12.5px] text-[rgba(255,255,255,0.45)]">
+ {done ? step.result : "Running"}
+ </span>
+ </span>
+ </div>
+ );
+ })}
+ </div>
+ {answer ? (
+ <p className="ht-fade-in mt-6 font-sans text-[15px] leading-[1.55] text-[rgba(255,255,255,0.88)] [font-family:var(--font-ht-geist),system-ui,sans-serif]">
+ Here is the repro. At 0:12 the total stays at $4,200 after the
+ second line item is removed.
+ </p>
+ ) : null}
+ </div>
+
+ <div
+ className="absolute right-10 top-[88px] w-[340px] overflow-hidden rounded-[16px] bg-white text-[#1C1D21] shadow-[0_30px_80px_-30px_rgba(0,0,0,0.8)]"
+ style={{
+ opacity: card,
+ transform: `translateY(${(1 - card) * 16}px)`,
+ }}
+ >
+ <div className="relative h-[190px] overflow-hidden bg-[#DCE6F1]">
+ <div
+ className="absolute left-[22px] top-[16px] overflow-hidden rounded-[6px]"
+ style={{
+ width: INVOICE.w,
+ height: INVOICE.h,
+ transform: "scale(0.435)",
+ transformOrigin: "top left",
+ }}
+ >
+ <InvoicePage />
+ </div>
+ </div>
+ <div className="p-4">
+ <div className="flex items-center gap-1.5 text-[12px] text-[#8A8F98]">
+ <CapLogoMark className="size-3.5" /> Cap.so/s/m4k8x2qz
+ </div>
+ <div className="mt-1 text-[15px] font-semibold">
+ {CAP_TITLE.replace("Sending an invoice", "Invoice bug repro")}
+ </div>
+ <div className="mt-2 text-[13px] leading-[1.5] text-[#4B5059]">
+ Removing a line item leaves the total unchanged until the page
+ reloads.
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export const AgentsStage = () => (
+ <Loop
+ View={AgentView}
+ duration={10500}
+ pose={9000}
+ canvas={CANVAS}
+ crop={{ x: 16, w: 620 }}
+ tone={TONES.agents}
+ />
+);
diff --git a/apps/web/components/pages/HomeTwo/showcase/ai.tsx b/apps/web/components/pages/HomeTwo/showcase/ai.tsx
new file mode 100644
index 000000000..1ac14b721
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/ai.tsx
@@ -0,0 +1,356 @@
+"use client";
+
+import { AlignLeft, ListOrdered, ScrollText } from "lucide-react";
+import type { ReactNode } from "react";
+import { CapLogoMark } from "../demo/capIcons";
+import { AI_COPY, copyOf } from "./copy";
+import { CANVAS, type Scene, Showcase, seg, TONES } from "./kit";
+import { Avatar, RecordingFrame } from "./share";
+
+export { CANVAS };
+
+const SURFACE = { x: 70, y: 34, w: 900, h: 470 } as const;
+const PLAYER = { x: 32, y: 32, w: 520 } as const;
+const PANEL = { x: 572, y: 32, w: 296, h: 406 } as const;
+const VIDEO_H = Math.round((PLAYER.w - 16) * (9 / 16));
+
+const DURATION = 24;
+
+const fmt = (s: number) =>
+ `${Math.floor(s / 60)}:${String(Math.floor(s % 60)).padStart(2, "0")}`;
+
+const SUMMARY =
+ "Richie walks through sending invoice #1042 to Northwind Labs in the new flow. He picks the client, adds two line items for a total of $4,200, and sends it in under thirty seconds.";
+
+const AI_TITLE = "Sending an invoice in Ledgerly";
+const RAW_TITLE = "Recording 2026-10-25 at 10.41";
+
+const CHAPTERS = [
+ [0, "Opening a new invoice"],
+ [6, "Choosing the client"],
+ [11, "Adding the line items"],
+ [19, "Sending it to Northwind"],
+] as const;
+
+const LINES = [
+ [2, "So this is the new invoice flow."],
+ [6, "First, pick the client, so Northwind Labs."],
+ [11, "Then add your line items, the retainer and the study."],
+ [16, "The total updates as you go, $4,200."],
+ [19, "And hit send. That's the whole thing."],
+] as const;
+
+const Surface = ({ children }: { children: ReactNode }) => (
+ <div
+ className="absolute overflow-hidden rounded-[22px] shadow-[0_0_0_1px_rgba(120,72,16,0.06)]"
+ style={{
+ left: SURFACE.x,
+ top: SURFACE.y,
+ width: SURFACE.w,
+ height: SURFACE.h,
+ background:
+ "radial-gradient(120% 90% at 0% 0%, #FFE7C2 0%, transparent 60%), radial-gradient(110% 100% at 100% 100%, #FFD3C4 0%, transparent 55%), linear-gradient(135deg, #FFF1DC 0%, #FFE6D2 100%)",
+ }}
+ >
+ {children}
+ </div>
+);
+
+const Player = ({
+ seconds,
+ playing,
+ title,
+ ticks = 0,
+ caption,
+ chapter,
+}: {
+ seconds: number;
+ playing: boolean;
+ title: ReactNode;
+ ticks?: number;
+ caption?: string;
+ chapter?: string;
+}) => {
+ const progress = Math.min(1, seconds / DURATION);
+ return (
+ <div
+ className="absolute rounded-[16px] bg-white p-2 shadow-[0_0_0_1px_rgba(17,17,17,0.06),0_30px_60px_-30px_rgba(120,60,20,0.45)]"
+ style={{ left: PLAYER.x, top: PLAYER.y, width: PLAYER.w }}
+ >
+ <div className="flex items-center gap-2.5 px-2 pb-2.5 pt-1.5">
+ <Avatar size={26} />
+ <div className="min-w-0 leading-tight">
+ <div className="truncate text-[15px] text-[#1C1C1C]">{title}</div>
+ <div className="text-[11.5px] text-[#8F8F8F]">Richie · just now</div>
+ </div>
+ </div>
+ <div
+ className="relative overflow-hidden rounded-[10px] bg-black"
+ style={{ height: VIDEO_H }}
+ >
+ <RecordingFrame playing={playing} pageScale={0.52} />
+ {chapter ? (
+ <div
+ key={chapter}
+ className="ht-fade-in absolute left-3 top-3 rounded-full bg-black/70 px-3 py-1 text-[12px] text-white"
+ >
+ {chapter}
+ </div>
+ ) : null}
+ {caption ? (
+ <div className="absolute inset-x-0 bottom-3 flex justify-center">
+ <span
+ key={caption}
+ className="ht-fade-in rounded-[7px] bg-black/90 px-2.5 py-1 text-[14px] font-bold text-white"
+ >
+ {caption}
+ </span>
+ </div>
+ ) : null}
+ </div>
+ <div className="px-2 pb-1.5 pt-3">
+ <div className="relative h-[5px] rounded-full bg-[#EEEEEE]">
+ <div
+ className="absolute inset-y-0 left-0 rounded-full bg-[#3B82F6]"
+ style={{ width: `${progress * 100}%` }}
+ />
+ {CHAPTERS.slice(1, Math.max(1, ticks)).map(([at]) => (
+ <span
+ key={at}
+ className="ht-pop-in absolute top-1/2 h-[11px] w-[3px] -translate-x-1/2 -translate-y-1/2 rounded-full bg-[#1C1C1C]"
+ style={{ left: `${(at / DURATION) * 100}%` }}
+ />
+ ))}
+ <span
+ className="absolute top-1/2 size-3 -translate-x-1/2 -translate-y-1/2 rounded-full bg-white shadow-[0_0_0_1.5px_#3B82F6,0_1px_3px_rgba(0,0,0,0.2)]"
+ style={{ left: `${progress * 100}%` }}
+ />
+ </div>
+ <div className="mt-2 flex justify-between text-[11px] tabular-nums text-[#8F8F8F]">
+ <span>{fmt(seconds)}</span>
+ <span>{fmt(DURATION)}</span>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+const Panel = ({
+ heading,
+ status,
+ children,
+}: {
+ heading: string;
+ status?: ReactNode;
+ children: ReactNode;
+}) => (
+ <div
+ className="absolute flex flex-col overflow-hidden rounded-[16px] bg-white shadow-[0_0_0_1px_rgba(17,17,17,0.06),0_30px_60px_-30px_rgba(120,60,20,0.45)]"
+ style={{
+ left: PANEL.x,
+ top: PANEL.y,
+ width: PANEL.w,
+ height: PANEL.h,
+ }}
+ >
+ <div className="flex items-center justify-between border-b border-[#F0F0F0] px-4 py-3">
+ <span className="flex items-center gap-2 text-[15px] font-medium text-[#1C1C1C]">
+ <CapLogoMark className="size-4" />
+ {heading}
+ </span>
+ {status}
+ </div>
+ <div className="min-h-0 flex-1 overflow-hidden">{children}</div>
+ </div>
+);
+
+const Writing = () => (
+ <span className="inline-flex items-center gap-1.5 text-[11.5px] text-[#8F8F8F]">
+ <span className="relative flex size-1.5">
+ <span className="absolute inline-flex size-full animate-ping rounded-full bg-[#F59E0B] opacity-60" />
+ <span className="relative inline-flex size-1.5 rounded-full bg-[#F59E0B]" />
+ </span>
+ Writing
+ </span>
+);
+
+const Done = ({ label }: { label: string }) => (
+ <span className="ht-fade-in text-[11.5px] text-[#16A34A]">{label}</span>
+);
+
+const Typed = ({ text, k }: { text: string; k: number }) => {
+ const n = Math.round(text.length * Math.min(1, Math.max(0, k)));
+ return (
+ <>
+ {text.slice(0, n)}
+ {n > 0 && n < text.length ? (
+ <span className="ml-px inline-block h-[0.95em] w-[1.5px] translate-y-[2px] bg-current" />
+ ) : null}
+ </>
+ );
+};
+
+const SummaryView = ({ t, playing }: { t: number; playing: boolean }) => {
+ const titleK = seg(t, 500, 1300);
+ const bodyK = Math.min(1, Math.max(0, (t - 1400) / 3400));
+ const done = bodyK >= 1;
+ return (
+ <Surface>
+ <Player
+ seconds={2 + t / 1000}
+ playing={playing}
+ title={
+ titleK <= 0 ? (
+ <span className="text-[#8F8F8F]">{RAW_TITLE}</span>
+ ) : (
+ <Typed text={AI_TITLE} k={titleK} />
+ )
+ }
+ />
+ <Panel
+ heading="Summary"
+ status={done ? <Done label="Ready" /> : <Writing />}
+ >
+ <div className="px-4 py-3.5">
+ <div className="text-[11.5px] text-[#8F8F8F]">Title</div>
+ <div className="mt-1 min-h-[21px] text-[14px] font-medium text-[#1C1C1C]">
+ <Typed text={AI_TITLE} k={titleK} />
+ </div>
+ <div className="mt-4 text-[11.5px] text-[#8F8F8F]">Summary</div>
+ <p className="mt-1 text-[14px] leading-[1.6] text-[#1C1C1C]">
+ <Typed text={SUMMARY} k={bodyK} />
+ </p>
+ </div>
+ </Panel>
+ </Surface>
+ );
+};
+
+const ChaptersView = ({ t, playing }: { t: number; playing: boolean }) => {
+ const shown = Math.min(
+ CHAPTERS.length,
+ Math.floor(Math.max(0, t - 300) / 280) + 1,
+ );
+ const target = 2;
+ const clickAt = 2600;
+ const clicked = t > clickAt;
+ const start = CHAPTERS[target]?.[0] ?? 0;
+ const seconds = clicked ? start + (t - clickAt) / 1000 : 1 + t / 1000;
+ const active = clicked
+ ? target
+ : CHAPTERS.findLastIndex(([at]) => at <= seconds);
+ const tap = (t - clickAt + 150) / 520;
+ return (
+ <Surface>
+ <Player
+ seconds={seconds}
+ playing={playing}
+ title={AI_TITLE}
+ ticks={shown}
+ chapter={CHAPTERS[active]?.[1]}
+ />
+ <Panel
+ heading="Chapters"
+ status={
+ shown < CHAPTERS.length ? (
+ <Writing />
+ ) : (
+ <Done label={`${CHAPTERS.length} chapters`} />
+ )
+ }
+ >
+ <div className="p-2">
+ {CHAPTERS.slice(0, shown).map(([at, title], i) => (
+ <div
+ key={at}
+ className="ht-pop-in relative flex h-[46px] items-center gap-3 overflow-hidden rounded-[10px] px-3 transition-colors duration-300"
+ style={{
+ background: i === active ? "#FFF4E2" : "transparent",
+ }}
+ >
+ <span className="w-9 text-[12px] tabular-nums text-[#B45309]">
+ {fmt(at)}
+ </span>
+ <span className="text-[14px] text-[#1C1C1C]">{title}</span>
+ {i === target && tap > 0 && tap < 1 ? (
+ <span
+ className="pointer-events-none absolute left-1/2 top-1/2 size-10 rounded-full bg-[rgba(245,158,11,0.3)]"
+ style={{
+ transform: `translate(-50%, -50%) scale(${0.5 + tap})`,
+ opacity: 1 - tap,
+ }}
+ />
+ ) : null}
+ </div>
+ ))}
+ </div>
+ </Panel>
+ </Surface>
+ );
+};
+
+const TranscriptView = ({ t, playing }: { t: number; playing: boolean }) => {
+ const seconds = Math.min(23.5, 1.5 + t / 260);
+ const current = LINES.findLastIndex(([at]) => at <= seconds);
+ return (
+ <Surface>
+ <Player
+ seconds={seconds}
+ playing={playing}
+ title={AI_TITLE}
+ caption={LINES[current]?.[1]}
+ />
+ <Panel heading="Transcript" status={<Done label="Searchable" />}>
+ <div className="px-2 py-2">
+ {LINES.map(([at, text], i) => (
+ <div
+ key={at}
+ className="flex items-baseline gap-2.5 rounded-[10px] px-2.5 py-2 transition-colors duration-300"
+ style={{
+ background: i === current ? "#FFF4E2" : "transparent",
+ }}
+ >
+ <span className="w-8 shrink-0 text-[11px] tabular-nums text-[#B45309]">
+ {fmt(at)}
+ </span>
+ <span
+ className="text-[13.5px] leading-[1.5] transition-colors duration-300"
+ style={{ color: i === current ? "#1C1C1C" : "#8F8F8F" }}
+ >
+ {text}
+ </span>
+ </div>
+ ))}
+ </div>
+ </Panel>
+ </Surface>
+ );
+};
+
+const scene = (
+ key: string,
+ icon: ReactNode,
+ duration: number,
+ pose: number,
+ View: Scene["View"],
+): Scene => {
+ const copy = copyOf(AI_COPY, key);
+ return {
+ key,
+ label: copy.label,
+ icon,
+ lead: copy.lead,
+ body: copy.body,
+ duration,
+ pose,
+ View,
+ };
+};
+
+const SCENES: Scene[] = [
+ scene("summary", <AlignLeft />, 6400, 5600, SummaryView),
+ scene("chapters", <ListOrdered />, 6200, 4000, ChaptersView),
+ scene("transcript", <ScrollText />, 6000, 3000, TranscriptView),
+];
+
+export const AiStage = () => <Showcase scenes={SCENES} tone={TONES.ai} />;
diff --git a/apps/web/components/pages/HomeTwo/showcase/copy.ts b/apps/web/components/pages/HomeTwo/showcase/copy.ts
new file mode 100644
index 000000000..b23f85347
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/copy.ts
@@ -0,0 +1,120 @@
+/**
+ * Copy for the tabs inside each animated stage. Lives in a plain module so
+ * the server-rendered sections can put the same words in the HTML for
+ * crawlers and screen readers, while the client stages show them as tab
+ * labels and captions.
+ */
+export type StageCopy = {
+ key: string;
+ label: string;
+ lead: string;
+ body: string;
+};
+
+export const INSTANT_COPY: StageCopy[] = [
+ {
+ key: "record",
+ label: "Record",
+ lead: "Instant share links.",
+ body: "Your Cap.so link is live the moment you hit record, and the video uploads while you are still talking.",
+ },
+ {
+ key: "share",
+ label: "Stop and paste",
+ lead: "Link copied when you stop.",
+ body: "Press stop and paste the link into Slack, a ticket, or an email. The video is ready when it lands.",
+ },
+ {
+ key: "watch",
+ label: "Watch",
+ lead: "Watch in any browser.",
+ body: "No download and no account needed. Viewers leave timestamped comments and emoji reactions on the share page.",
+ },
+];
+
+export const STUDIO_COPY: StageCopy[] = [
+ {
+ key: "zoom",
+ label: "Zoom",
+ lead: "Automatic zoom.",
+ body: "Cap zooms in on every click and follows your cursor with the same spring physics as the editor, then eases back out.",
+ },
+ {
+ key: "layouts",
+ label: "Camera",
+ lead: "Camera layouts.",
+ body: "Switch any part of the timeline between a webcam bubble, split screen, camera only, or screen only.",
+ },
+ {
+ key: "captions",
+ label: "Captions",
+ lead: "Automatic captions.",
+ body: "Cap transcribes your voice on your own machine in 19 languages and shows each word as you say it.",
+ },
+ {
+ key: "blur",
+ label: "Blur",
+ lead: "Blur and pixelate.",
+ body: "Hide an email, a password, or a face with blur or pixelate, or use highlight to dim everything else.",
+ },
+ {
+ key: "background",
+ label: "Background",
+ lead: "Backgrounds and padding.",
+ body: "Pick a wallpaper, gradient, or color, and Cap adds padding, rounded corners, and a shadow.",
+ },
+];
+
+export const AI_COPY: StageCopy[] = [
+ {
+ key: "summary",
+ label: "Summary",
+ lead: "AI titles and summaries.",
+ body: "Cap renames the recording and writes a summary as soon as you stop, so viewers know what it covers before pressing play.",
+ },
+ {
+ key: "chapters",
+ label: "Chapters",
+ lead: "Automatic chapters.",
+ body: "Recordings split into chapters on the timeline. Click one and the video jumps straight to that part.",
+ },
+ {
+ key: "transcript",
+ label: "Transcript",
+ lead: "Searchable transcripts.",
+ body: "Every word is transcribed and timestamped, follows along as the video plays, and doubles as captions.",
+ },
+];
+
+export const STORAGE_COPY: StageCopy[] = [
+ {
+ key: "cloud",
+ label: "Cap Cloud",
+ lead: "Cap Cloud.",
+ body: "Zero setup. Encrypted, and shareable the second you stop.",
+ },
+ {
+ key: "s3",
+ label: "Your S3 bucket",
+ lead: "Your own S3 bucket.",
+ body: "Any S3 compatible bucket, in any region you choose.",
+ },
+ {
+ key: "drive",
+ label: "Google Drive",
+ lead: "Google Drive.",
+ body: "Recordings land in a Drive folder you pick.",
+ },
+ {
+ key: "self",
+ label: "Self hosted",
+ lead: "Self hosting.",
+ body: "Run the whole Cap platform on your own infrastructure.",
+ },
+];
+
+export const copyOf = (list: StageCopy[], key: string) => {
+ const item = list.find((entry) => entry.key === key);
+ if (!item) throw new Error(`Missing stage copy for ${key}`);
+ return item;
+};
diff --git a/apps/web/components/pages/HomeTwo/showcase/instant.tsx b/apps/web/components/pages/HomeTwo/showcase/instant.tsx
new file mode 100644
index 000000000..a43d77f39
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/instant.tsx
@@ -0,0 +1,565 @@
+"use client";
+
+import { Link2, MonitorPlay, Radio } from "lucide-react";
+import type { CSSProperties, ReactNode } from "react";
+import {
+ CapLogoMark,
+ CapMicrophone,
+ CapPauseCircle,
+ CapRestart,
+ CapStopCircle,
+ CapTrash,
+} from "../demo/capIcons";
+import { OS_FONT, useIsWindowsDemo } from "../demo/platform";
+import { copyOf, INSTANT_COPY } from "./copy";
+import { CANVAS, lerp, type Scene, Showcase, seg, TONES } from "./kit";
+import {
+ CAP_TITLE,
+ CommentBubble,
+ RecordingFrame,
+ SHARE_URL,
+ SHARE_W,
+ type ShareComment,
+ SharePage,
+} from "./share";
+import {
+ AppBar,
+ BrowserBar,
+ Cursor,
+ INVOICE,
+ InvoicePage,
+ Ripple,
+ WALLPAPERS,
+ Webcam,
+} from "./subjects";
+
+const SCREEN = { w: 800, h: 478 } as const;
+const SCREEN_LEFT = (CANVAS.w - SCREEN.w) / 2;
+const SCREEN_TOP = 26;
+
+const TASKBAR = 40;
+
+const useFloor = () => (useIsWindowsDemo() ? TASKBAR : 0);
+
+const Taskbar = () => (
+ <div
+ className="absolute inset-x-0 bottom-0 z-20 flex items-center justify-center gap-1.5 border-t border-white/60 bg-[rgba(243,243,243,0.9)] backdrop-blur-xl"
+ style={{ height: TASKBAR }}
+ >
+ <span className="grid size-8 place-items-center rounded-md">
+ <svg aria-hidden="true" viewBox="0 0 24 24" className="size-[17px]">
+ <path
+ d="M0,0H11.377V11.372H0ZM12.623,0H24V11.372H12.623ZM0,12.623H11.377V24H0Zm12.623,0H24V24H12.623"
+ fill="#0078D4"
+ />
+ </svg>
+ </span>
+ <span className="grid size-8 place-items-center rounded-md">
+ <span className="size-[17px] rounded-full bg-[conic-gradient(#0EA5E9,#22C55E,#0EA5E9)]" />
+ </span>
+ <span className="relative grid size-8 place-items-center rounded-md bg-white/70 shadow-[0_0_0_1px_rgba(0,0,0,0.05)]">
+ <CapLogoMark className="size-[18px]" />
+ <span className="absolute bottom-[3px] h-[3px] w-3 rounded-full bg-[#0078D4]" />
+ </span>
+ </div>
+);
+
+// New York for this section only: the hero has San Francisco and the
+// painted sky, so the two never read as the same scene.
+const Desktop = ({ children }: { children: ReactNode }) => {
+ const windows = useIsWindowsDemo();
+ return (
+ <div
+ className="absolute overflow-hidden rounded-[16px] shadow-[0_0_0_1px_rgba(17,17,17,0.06)]"
+ style={{
+ left: SCREEN_LEFT,
+ top: SCREEN_TOP,
+ width: SCREEN.w,
+ height: SCREEN.h,
+ backgroundImage: `url(${WALLPAPERS.nyc})`,
+ backgroundSize: "cover",
+ backgroundPosition: "center",
+ }}
+ >
+ {children}
+ {windows ? <Taskbar /> : null}
+ </div>
+ );
+};
+
+const PAGE_SCALE = 1.0;
+const PAGE_LEFT = (SCREEN.w - INVOICE.w * PAGE_SCALE) / 2;
+const PAGE_TOP = 24;
+
+const PageWindow = ({ style }: { style?: CSSProperties }) => (
+ <div
+ className="absolute overflow-hidden rounded-[10px] shadow-[0_0_0_1px_rgba(0,0,0,0.08),0_20px_50px_-20px_rgba(17,24,39,0.35)]"
+ style={{
+ left: PAGE_LEFT,
+ top: PAGE_TOP,
+ width: INVOICE.w,
+ height: INVOICE.h,
+ ...style,
+ }}
+ >
+ <InvoicePage />
+ </div>
+);
+
+const formatTime = (ms: number) => {
+ const s = Math.floor(ms / 1000);
+ return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
+};
+
+const Toolbar = ({
+ elapsed,
+ style,
+}: {
+ elapsed: number;
+ style?: CSSProperties;
+}) => (
+ <div
+ className="absolute flex h-11 items-center gap-1 rounded-2xl border border-[#E0E0E0] bg-[#FCFCFC] px-1.5 text-[15px] shadow-[0_8px_24px_-10px_rgba(0,0,0,0.3)]"
+ style={style}
+ >
+ <span className="flex items-center gap-1.5 rounded-lg px-2 py-1 text-[#FF4766]">
+ <CapStopCircle className="size-5" />
+ <span className="tabular-nums">{formatTime(elapsed)}</span>
+ </span>
+ <span className="mx-1 flex items-center gap-1 text-[#646464]">
+ <CapMicrophone className="size-5 text-[#202020]" />
+ <CapPauseCircle className="ml-2 size-5" />
+ <CapRestart className="ml-2 size-5" />
+ <CapTrash className="ml-2 size-5" />
+ </span>
+ </div>
+);
+
+const formatClock = (ms: number) => {
+ const sec = Math.floor(ms / 1000);
+ return `${Math.floor(sec / 60)}:${String(sec % 60).padStart(2, "0")}`;
+};
+
+const CHUNK_MS = 2000;
+const CHUNKS = 10;
+const CHUNK_SLOTS = Array.from({ length: CHUNKS }, (_, i) => ({
+ id: `chunk-${i}`,
+ index: i,
+}));
+
+const LiveLink = ({
+ elapsed,
+ playing,
+}: {
+ elapsed: number;
+ playing: boolean;
+}) => {
+ const landed = Math.floor(elapsed / CHUNK_MS);
+ return (
+ <div className="absolute right-[18px] top-[26px] w-[214px] overflow-hidden rounded-[14px] bg-white shadow-[0_0_0_1px_rgba(0,0,0,0.06),0_20px_44px_-18px_rgba(17,24,39,0.45)]">
+ <div className="flex items-center gap-1.5 px-3 pb-2 pt-2.5 text-[11.5px] text-[#4B5059]">
+ <CapLogoMark className="size-3.5" />
+ <span className="truncate">{SHARE_URL}</span>
+ <span className="ml-auto flex items-center gap-1 rounded-full bg-[#FEE2E2] px-1.5 py-[1px] text-[10px] font-semibold text-[#DC2626]">
+ <span className="size-1.5 animate-pulse rounded-full bg-[#DC2626]" />
+ Live
+ </span>
+ </div>
+ <div className="relative mx-2 h-[112px] overflow-hidden rounded-[9px]">
+ <RecordingFrame playing={playing} pageScale={0.27} />
+ <span className="absolute bottom-1.5 right-1.5 rounded-[5px] bg-black/70 px-1.5 py-0.5 text-[10.5px] tabular-nums text-white">
+ {formatClock(landed * CHUNK_MS)}
+ </span>
+ </div>
+ <div className="flex gap-[3px] px-3 pt-2.5">
+ {CHUNK_SLOTS.map(({ id, index }) => {
+ const done = index < landed;
+ const sending = index === landed;
+ return (
+ <span
+ key={id}
+ className="h-1.5 flex-1 rounded-full transition-colors duration-300"
+ style={{
+ background: done ? "#3B82F6" : sending ? "#BFDBFE" : "#EEF1F5",
+ }}
+ />
+ );
+ })}
+ </div>
+ <div className="px-3 pb-2.5 pt-1.5 text-[11px] text-[#6B7079]">
+ <span className="tabular-nums text-[#1C1D21]">
+ {formatClock(landed * CHUNK_MS)}
+ </span>{" "}
+ already on Cap.so
+ </div>
+ </div>
+ );
+};
+
+const RECORD_SCALE = 0.78;
+
+const RecordView = ({ t, playing }: { t: number; playing: boolean }) => {
+ const elapsed = 8000 + t;
+ const cursor = {
+ x: lerp(lerp(200, 400, seg(t, 300, 1800)), 330, seg(t, 2600, 4200)),
+ y: lerp(lerp(170, 210, seg(t, 300, 1800)), 270, seg(t, 2600, 4200)),
+ };
+ const floor = useFloor();
+ return (
+ <Desktop>
+ <div
+ className="absolute overflow-hidden rounded-[10px] shadow-[0_0_0_1px_rgba(0,0,0,0.08),0_20px_50px_-20px_rgba(17,24,39,0.35)]"
+ style={{
+ left: 22,
+ top: 26,
+ width: INVOICE.w * RECORD_SCALE,
+ height: INVOICE.h * RECORD_SCALE,
+ }}
+ >
+ <div
+ style={{
+ width: INVOICE.w,
+ height: INVOICE.h,
+ transform: `scale(${RECORD_SCALE})`,
+ transformOrigin: "top left",
+ }}
+ >
+ <InvoicePage />
+ </div>
+ </div>
+ <LiveLink elapsed={elapsed} playing={playing} />
+ <Webcam
+ playing={playing}
+ className="absolute shadow-[0_16px_40px_-14px_rgba(0,0,0,0.45)]"
+ style={{
+ left: 28,
+ bottom: 28 + floor,
+ width: 112,
+ height: 112,
+ borderRadius: 56,
+ }}
+ />
+ <Toolbar
+ elapsed={elapsed}
+ style={{ left: SCREEN.w / 2 - 110, bottom: 22 + floor }}
+ />
+ <Cursor x={cursor.x} y={cursor.y} size={24} />
+ </Desktop>
+ );
+};
+
+const Notification = ({ k }: { k: number }) => {
+ const windows = useIsWindowsDemo();
+ if (windows) {
+ return (
+ <div
+ className="absolute z-30 flex w-[330px] flex-col gap-1 rounded-[8px] border border-black/[0.07] bg-[#F3F3F3] p-3.5 shadow-[0_16px_40px_rgba(0,0,0,0.25)]"
+ style={{
+ right: 12,
+ bottom: TASKBAR + 12,
+ fontFamily: OS_FONT.windows,
+ opacity: k,
+ transform: `translateY(${(1 - k) * 24}px)`,
+ }}
+ >
+ <span className="flex items-center gap-2 text-[12px] text-[#5C5C5C]">
+ <CapLogoMark className="size-4" /> Cap
+ <span className="flex-1" />
+ now
+ </span>
+ <span className="text-[14px] font-semibold text-[#1B1B1B]">
+ Link copied
+ </span>
+ <span className="truncate text-[13px] text-[#404040]">
+ {SHARE_URL} is on your clipboard
+ </span>
+ </div>
+ );
+ }
+ return (
+ <MacNotification
+ style={{
+ right: 18,
+ top: 18,
+ opacity: k,
+ transform: `translateX(${(1 - k) * 24}px)`,
+ }}
+ />
+ );
+};
+
+const MacNotification = ({ style }: { style?: CSSProperties }) => (
+ <div
+ className="absolute z-30 flex w-[330px] items-center gap-3 rounded-2xl bg-[rgba(246,246,248,0.96)] p-3 shadow-[0_18px_44px_-14px_rgba(0,0,0,0.35),0_0_0_1px_rgba(0,0,0,0.06)]"
+ style={style}
+ >
+ <span className="grid size-10 shrink-0 place-items-center rounded-[10px] bg-white shadow-[0_0_0_1px_rgba(0,0,0,0.06)]">
+ <CapLogoMark className="size-6" />
+ </span>
+ <span className="min-w-0 flex-1">
+ <span className="flex justify-between text-[13px] font-semibold text-[#1C1D21]">
+ Link copied
+ <span className="font-normal text-[#8A8F98]">now</span>
+ </span>
+ <span className="block truncate text-[12.5px] text-[#4B5059]">
+ {SHARE_URL} is on your clipboard
+ </span>
+ </span>
+ </div>
+);
+
+const ChatWindow = ({
+ typed,
+ sent,
+ reply,
+ playing,
+}: {
+ typed: string;
+ sent: boolean;
+ reply: number;
+ playing: boolean;
+}) => {
+ const floor = useFloor();
+ return (
+ <div
+ style={{ bottom: floor }}
+ className="absolute inset-x-[90px] top-[34px] flex flex-col overflow-hidden rounded-t-[12px] bg-white shadow-[0_0_0_1px_rgba(0,0,0,0.08),0_20px_50px_-20px_rgba(17,24,39,0.35)]"
+ >
+ <AppBar className="h-10 border-b border-[#ECEDF0] text-[13.5px] text-[#1C1D21]">
+ <span className="font-semibold"># billing</span>
+ <span className="text-[#8A8F98]">4 members</span>
+ </AppBar>
+ <div className="flex flex-1 flex-col justify-end gap-4 px-5 pb-4">
+ <div className="flex gap-3 text-[13.5px]">
+ <span className="grid size-8 shrink-0 place-items-center rounded-lg bg-[#FDE68A] text-[12px]">
+ M
+ </span>
+ <span>
+ <span className="font-semibold">Mia</span>{" "}
+ <span className="text-[11.5px] text-[#8A8F98]">10:41</span>
+ <span className="block text-[#33363D]">
+ How do I send the Northwind invoice from the new flow?
+ </span>
+ </span>
+ </div>
+ {sent ? (
+ <div className="ht-fade-in flex gap-3 text-[13.5px]">
+ <span className="grid size-8 shrink-0 place-items-center rounded-lg bg-[#C7D2FE] text-[12px]">
+ R
+ </span>
+ <span className="min-w-0 flex-1">
+ <span className="font-semibold">Richie</span>{" "}
+ <span className="text-[11.5px] text-[#8A8F98]">10:42</span>
+ <span className="block text-[#2563EB]">{SHARE_URL}</span>
+ <span className="mt-2 flex w-[360px] overflow-hidden rounded-[10px] border border-[#ECEDF0]">
+ <span className="block h-[86px] w-[138px] shrink-0">
+ <RecordingFrame playing={playing} pageScale={0.17} />
+ </span>
+ <span className="flex flex-col justify-center gap-1 px-3">
+ <span className="flex items-center gap-1.5 text-[11.5px] text-[#6B7079]">
+ <CapLogoMark className="size-3.5" /> Cap
+ </span>
+ <span className="text-[13px] font-semibold leading-tight text-[#1C1D21]">
+ {CAP_TITLE}
+ </span>
+ <span className="text-[11.5px] text-[#6B7079]">
+ 0:24 · Watch in the browser
+ </span>
+ </span>
+ </span>
+ </span>
+ </div>
+ ) : null}
+ {reply > 0 ? (
+ <div className="ht-fade-in flex gap-3 text-[13.5px]">
+ <span className="grid size-8 shrink-0 place-items-center rounded-lg bg-[#FDE68A] text-[12px]">
+ M
+ </span>
+ <span>
+ <span className="font-semibold">Mia</span>{" "}
+ <span className="text-[11.5px] text-[#8A8F98]">10:42</span>
+ <span className="block text-[#33363D]">
+ Perfect, that's so much easier than a doc.
+ </span>
+ </span>
+ </div>
+ ) : null}
+ </div>
+ <div className="mx-4 mb-4 flex h-11 items-center rounded-[10px] border border-[#DADDE2] px-3.5 text-[13.5px] text-[#1C1D21]">
+ {typed ? (
+ <span className="text-[#2563EB]">{typed}</span>
+ ) : (
+ <span className="text-[#9AA0A8]">Message #billing</span>
+ )}
+ {typed && !sent ? (
+ <span className="ml-px h-4 w-px animate-pulse bg-[#1C1D21]" />
+ ) : null}
+ </div>
+ </div>
+ );
+};
+
+const ShareView = ({ t, playing }: { t: number; playing: boolean }) => {
+ const toolbarX = SCREEN.w / 2 - 110;
+ const floor = useFloor();
+ const stop = { x: toolbarX + 26, y: SCREEN.h - 22 - 22 - floor };
+ const toChat = seg(t, 2300, 2800);
+ const cursor =
+ t < 1300
+ ? {
+ x: lerp(420, stop.x, seg(t, 200, 1100)),
+ y: lerp(260, stop.y, seg(t, 200, 1100)),
+ }
+ : { x: lerp(stop.x, 420, toChat), y: lerp(stop.y, 455, toChat) };
+ const stopped = t > 1250;
+ const note = seg(t, 1400, 1800) * (1 - seg(t, 4600, 5000));
+ const pasted = t > 3200 ? SHARE_URL : "";
+ const sent = t > 3900;
+ return (
+ <Desktop>
+ <div style={{ opacity: 1 - toChat }}>
+ <PageWindow />
+ <Toolbar
+ elapsed={24000}
+ style={{
+ left: toolbarX,
+ bottom: 22 + floor,
+ opacity: stopped ? 0 : 1,
+ transform: stopped ? "translateY(8px)" : "none",
+ transition: "opacity 250ms, transform 250ms",
+ }}
+ />
+ </div>
+ {toChat > 0 ? (
+ <div style={{ opacity: toChat }}>
+ <ChatWindow
+ typed={sent ? "" : pasted}
+ sent={sent}
+ reply={t > 5200 ? 1 : 0}
+ playing={playing}
+ />
+ </div>
+ ) : null}
+ <Ripple
+ x={stop.x}
+ y={stop.y}
+ k={(t - 1150) / 450}
+ color="rgba(255,71,102,0.35)"
+ />
+ <Notification k={note} />
+ <Cursor x={cursor.x} y={cursor.y} size={24} />
+ </Desktop>
+ );
+};
+
+const COMMENTS: ShareComment[] = [
+ {
+ at: 1400,
+ who: "Mia",
+ letter: "M",
+ color: "#F59E0B",
+ when: "just now",
+ seconds: 8,
+ text: "Didn't know you could send it from here, nice.",
+ },
+ {
+ at: 3300,
+ who: "Sam",
+ letter: "S",
+ color: "#10B981",
+ when: "just now",
+ seconds: 19,
+ text: "Can we make this the default flow?",
+ },
+];
+
+const REACTIONS = [
+ { emoji: "🙌", at: 4400, x: 150 },
+ { emoji: "😍", at: 4800, x: 60 },
+ { emoji: "👍", at: 5200, x: 200 },
+];
+
+const WINDOW = { left: 40, top: 22, w: 720, h: 456 } as const;
+
+const WatchView = ({ t, playing }: { t: number; playing: boolean }) => {
+ const scale = WINDOW.w / SHARE_W;
+ const pressed = REACTIONS.find((r) => t > r.at && t < r.at + 260)?.emoji;
+ const height = WINDOW.h - useFloor();
+ return (
+ <Desktop>
+ <div
+ className="absolute overflow-hidden rounded-t-[12px] bg-white shadow-[0_0_0_1px_rgba(0,0,0,0.08),0_24px_60px_-24px_rgba(17,24,39,0.45)]"
+ style={{
+ left: WINDOW.left,
+ top: WINDOW.top,
+ width: WINDOW.w,
+ height,
+ }}
+ >
+ <BrowserBar url={SHARE_URL} small className="h-8" />
+ <div
+ style={{
+ width: SHARE_W,
+ transform: `scale(${scale})`,
+ transformOrigin: "top left",
+ }}
+ >
+ <SharePage
+ height={(height - 32) / scale}
+ seconds={Math.min(23, 4 + t / 380)}
+ playing={playing}
+ views={Math.min(12, 3 + Math.floor(t / 500))}
+ pressed={pressed}
+ floating={REACTIONS.filter((r) => t > r.at && t < r.at + 1400).map(
+ (r) => ({ ...r, k: (t - r.at) / 1400 }),
+ )}
+ sidebar={
+ <div className="flex flex-col gap-4 p-4">
+ {COMMENTS.filter((c) => t > c.at).map((c) => (
+ <CommentBubble key={c.who} comment={c} />
+ ))}
+ </div>
+ }
+ />
+ </div>
+ </div>
+ </Desktop>
+ );
+};
+
+const SCENES: Scene[] = [
+ {
+ key: "record",
+ label: copyOf(INSTANT_COPY, "record").label,
+ icon: <Radio />,
+ lead: copyOf(INSTANT_COPY, "record").lead,
+ body: copyOf(INSTANT_COPY, "record").body,
+ duration: 5200,
+ pose: 2600,
+ View: RecordView,
+ },
+ {
+ key: "share",
+ label: copyOf(INSTANT_COPY, "share").label,
+ icon: <Link2 />,
+ lead: copyOf(INSTANT_COPY, "share").lead,
+ body: copyOf(INSTANT_COPY, "share").body,
+ duration: 7000,
+ pose: 4800,
+ View: ShareView,
+ },
+ {
+ key: "watch",
+ label: copyOf(INSTANT_COPY, "watch").label,
+ icon: <MonitorPlay />,
+ lead: copyOf(INSTANT_COPY, "watch").lead,
+ body: copyOf(INSTANT_COPY, "watch").body,
+ duration: 7000,
+ pose: 5000,
+ View: WatchView,
+ },
+];
+
+export const InstantStage = () => (
+ <Showcase scenes={SCENES} tone={TONES.instant} />
+);
diff --git a/apps/web/components/pages/HomeTwo/showcase/kit.tsx b/apps/web/components/pages/HomeTwo/showcase/kit.tsx
new file mode 100644
index 000000000..9d6520e75
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/kit.tsx
@@ -0,0 +1,482 @@
+"use client";
+
+import { classNames } from "@cap/utils/helpers";
+import {
+ type ComponentType,
+ type ReactNode,
+ useEffect,
+ useLayoutEffect,
+ useRef,
+ useState,
+} from "react";
+import { Fit, LazyMount } from "../scenes/engine";
+import { useInView, usePageVisible, useReducedMotion } from "../visibility";
+import { Panel, TONES, type Tone } from "./layout";
+
+export const CANVAS = { w: 1040, h: 600 } as const;
+
+export const clamp01 = (x: number) => Math.min(1, Math.max(0, x));
+
+export const seg = (t: number, from: number, to: number) =>
+ ease(clamp01((t - from) / (to - from)));
+
+export const ease = (x: number) =>
+ x < 0.5 ? 4 * x * x * x : 1 - (-2 * x + 2) ** 3 / 2;
+
+export const lerp = (a: number, b: number, k: number) => a + (b - a) * k;
+
+export type SceneView = ComponentType<{
+ t: number;
+ playing: boolean;
+ pick: number | null;
+}>;
+
+export type PickerOption = { key: string; label: string; icon: ReactNode };
+
+export type Scene = {
+ key: string;
+ label: string;
+ icon: ReactNode;
+ lead: string;
+ body: string;
+ duration: number;
+ pose: number;
+ View: SceneView;
+ picker?: {
+ options: PickerOption[];
+ at: (t: number) => number;
+ swatches?: boolean;
+ };
+};
+
+const useClock = ({
+ playing,
+ duration,
+ resetKey,
+ onEnd,
+}: {
+ playing: boolean;
+ duration: number;
+ resetKey: string;
+ onEnd: () => void;
+}) => {
+ const [t, setT] = useState(0);
+ const tRef = useRef(0);
+ const endRef = useRef(onEnd);
+ endRef.current = onEnd;
+ const resetRef = useRef(resetKey);
+
+ useEffect(() => {
+ if (resetRef.current !== resetKey) {
+ resetRef.current = resetKey;
+ tRef.current = 0;
+ setT(0);
+ }
+ if (!playing) return;
+ let raf = 0;
+ let last = performance.now();
+ const frame = (now: number) => {
+ const dt = Math.min(48, now - last);
+ last = now;
+ const next = tRef.current + dt;
+ if (next >= duration) {
+ tRef.current = duration;
+ setT(duration);
+ endRef.current();
+ return;
+ }
+ tRef.current = next;
+ setT(next);
+ raf = requestAnimationFrame(frame);
+ };
+ raf = requestAnimationFrame(frame);
+ return () => cancelAnimationFrame(raf);
+ }, [playing, duration, resetKey]);
+
+ return t;
+};
+
+const Tabs = ({
+ scenes,
+ active,
+ progress,
+ tone,
+ onPick,
+ className,
+}: {
+ scenes: Scene[];
+ active: number;
+ progress: number;
+ tone: Tone;
+ onPick: (index: number) => void;
+ className?: string;
+}) => {
+ const refs = useRef<(HTMLButtonElement | null)[]>([]);
+ const [thumb, setThumb] = useState<{ x: number; w: number } | null>(null);
+
+ useLayoutEffect(() => {
+ const measure = () => {
+ const el = refs.current[active];
+ if (el) setThumb({ x: el.offsetLeft, w: el.offsetWidth });
+ };
+ measure();
+ const ro = new ResizeObserver(measure);
+ const parent = refs.current[active]?.parentElement;
+ if (parent) ro.observe(parent);
+ return () => ro.disconnect();
+ }, [active]);
+
+ return (
+ <div
+ className={classNames(
+ "relative inline-flex max-w-full gap-0.5 overflow-x-auto rounded-full bg-white p-1.5 shadow-[0_0_0_1px_rgba(17,17,17,0.07),0_1px_2px_rgba(17,17,17,0.05),0_12px_28px_-14px_rgba(17,24,39,0.3)] [scrollbar-width:none]",
+ className,
+ )}
+ >
+ {thumb ? (
+ <span
+ aria-hidden="true"
+ className="absolute bottom-1.5 top-1.5 overflow-hidden rounded-full transition-[transform,width] duration-500 [transition-timing-function:cubic-bezier(0.34,1.4,0.5,1)]"
+ style={{
+ width: thumb.w,
+ transform: `translateX(${thumb.x - 6}px)`,
+ left: 6,
+ background: `color-mix(in srgb, ${tone.accent} 12%, white)`,
+ boxShadow: `inset 0 0 0 1px color-mix(in srgb, ${tone.accent} 22%, transparent)`,
+ }}
+ >
+ <span
+ className="absolute inset-y-0 left-0"
+ style={{
+ width: `${progress * 100}%`,
+ background: `color-mix(in srgb, ${tone.accent} 12%, transparent)`,
+ }}
+ />
+ </span>
+ ) : null}
+ {scenes.map((scene, i) => {
+ const on = i === active;
+ return (
+ <button
+ key={scene.key}
+ ref={(el) => {
+ refs.current[i] = el;
+ }}
+ type="button"
+ aria-pressed={on}
+ onClick={() => onPick(i)}
+ className={classNames(
+ "relative z-10 flex h-9 shrink-0 cursor-pointer items-center gap-2 rounded-full px-3.5 text-[14px] tracking-[-0.01em] transition-colors duration-300 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111]",
+ on
+ ? "font-medium"
+ : "text-[rgba(17,17,17,0.64)] hover:text-[#111111]",
+ )}
+ style={on ? { color: tone.ink } : undefined}
+ >
+ <span
+ aria-hidden="true"
+ className="grid size-4 place-items-center transition-transform duration-300 [&>svg]:size-4"
+ style={{ transform: on ? "scale(1.12)" : "none" }}
+ >
+ {scene.icon}
+ </span>
+ {scene.label}
+ </button>
+ );
+ })}
+ </div>
+ );
+};
+
+const Picker = ({
+ options,
+ active,
+ tone,
+ swatches,
+ onPick,
+ vertical,
+}: {
+ options: PickerOption[];
+ active: number;
+ tone: Tone;
+ swatches?: boolean;
+ onPick: (index: number) => void;
+ vertical?: boolean;
+}) => (
+ <div
+ className={classNames(
+ "flex gap-1 rounded-[18px] bg-white p-1.5 shadow-[0_0_0_1px_rgba(17,17,17,0.07),0_12px_28px_-14px_rgba(17,24,39,0.3)]",
+ vertical ? "flex-col" : "flex-row",
+ )}
+ >
+ {options.map((option, i) => {
+ const on = i === active;
+ return (
+ <button
+ key={option.key}
+ type="button"
+ aria-pressed={on}
+ aria-label={option.label}
+ title={option.label}
+ onClick={() => onPick(i)}
+ className={classNames(
+ "group relative grid size-10 cursor-pointer place-items-center rounded-[12px] transition-[background-color,box-shadow,color] duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111]",
+ on
+ ? ""
+ : "text-[rgba(17,17,17,0.45)] hover:bg-[#F3F5F8] hover:text-[#111111]",
+ )}
+ style={
+ on && !swatches
+ ? {
+ background: `color-mix(in srgb, ${tone.accent} 12%, white)`,
+ color: tone.ink,
+ boxShadow: `inset 0 0 0 1px color-mix(in srgb, ${tone.accent} 25%, transparent)`,
+ }
+ : undefined
+ }
+ >
+ {swatches ? (
+ <span
+ className="block size-7 rounded-full transition-shadow duration-200"
+ style={{
+ boxShadow: on
+ ? `0 0 0 2px #FFFFFF, 0 0 0 4px ${tone.accent}`
+ : "inset 0 0 0 1px rgba(17,17,17,0.1)",
+ }}
+ >
+ {option.icon}
+ </span>
+ ) : (
+ option.icon
+ )}
+ {vertical ? (
+ <span className="pointer-events-none absolute right-full mr-2 whitespace-nowrap rounded-md bg-[#111111] px-2 py-1 text-[12px] text-white opacity-0 transition-opacity duration-150 group-hover:opacity-100">
+ {option.label}
+ </span>
+ ) : null}
+ </button>
+ );
+ })}
+ </div>
+);
+
+export const Showcase = ({
+ scenes,
+ tone = TONES.instant,
+ canvas = CANVAS,
+ className,
+ caption = true,
+}: {
+ scenes: Scene[];
+ tone?: Tone;
+ canvas?: { w: number; h: number };
+ className?: string;
+ caption?: boolean;
+}) => {
+ const ref = useRef<HTMLDivElement | null>(null);
+ const inView = useInView(ref, "-15% 0px -15% 0px");
+ const pageVisible = usePageVisible();
+ const reduced = useReducedMotion();
+ const [active, setActive] = useState(0);
+ const [round, setRound] = useState(0);
+ const [picked, setPicked] = useState<number | null>(null);
+ const scene = (scenes[active] ?? scenes[0]) as Scene;
+ const playing = inView && pageVisible && picked === null && !reduced;
+
+ const t = useClock({
+ playing,
+ duration: scene.duration,
+ resetKey: `${scene.key}:${round}`,
+ onEnd: () => {
+ setActive((i) => (i + 1) % scenes.length);
+ setRound((r) => r + 1);
+ setPicked(null);
+ },
+ });
+
+ const shown = reduced ? scene.pose : t;
+ const pickTab = (i: number) => {
+ setActive(i);
+ setRound((r) => r + 1);
+ setPicked(null);
+ };
+ const pickOption = (i: number) => setPicked(i);
+ useEffect(() => {
+ if (picked === null) return;
+ const resume = setTimeout(() => setPicked(null), 8000);
+ return () => clearTimeout(resume);
+ }, [picked]);
+ const picker = scene.picker;
+ const pickerActive = picker ? (picked ?? picker.at(shown)) : 0;
+ const progress = reduced ? 0 : shown / scene.duration;
+
+ return (
+ <div ref={ref} className={className}>
+ <Panel tone={tone}>
+ <LazyMount w={canvas.w} h={canvas.h} grow>
+ <Fit w={canvas.w} h={canvas.h} grow>
+ <scene.View
+ key={`${scene.key}:${round}`}
+ t={shown}
+ playing={playing}
+ pick={picked}
+ />
+ </Fit>
+ </LazyMount>
+ {picker ? (
+ <div
+ key={scene.key}
+ className="ht-fade-in absolute hidden -translate-x-1/2 -translate-y-1/2 sm:block"
+ style={{ left: "92.9%", top: "44.6%" }}
+ >
+ <Picker
+ options={picker.options}
+ active={pickerActive}
+ tone={tone}
+ swatches={picker.swatches}
+ onPick={pickOption}
+ vertical
+ />
+ </div>
+ ) : null}
+ <div className="pointer-events-none absolute inset-x-0 bottom-4 hidden justify-center sm:flex lg:bottom-6">
+ <Tabs
+ scenes={scenes}
+ active={active}
+ progress={progress}
+ tone={tone}
+ onPick={pickTab}
+ className="pointer-events-auto"
+ />
+ </div>
+ </Panel>
+ {picker ? (
+ <div className="mt-4 flex justify-center sm:hidden">
+ <Picker
+ options={picker.options}
+ active={pickerActive}
+ tone={tone}
+ swatches={picker.swatches}
+ onPick={pickOption}
+ />
+ </div>
+ ) : null}
+ <div className="mt-4 flex justify-center sm:hidden">
+ <Tabs
+ scenes={scenes}
+ active={active}
+ progress={progress}
+ tone={tone}
+ onPick={pickTab}
+ />
+ </div>
+ {caption ? (
+ <p
+ key={scene.key}
+ aria-hidden="true"
+ className="ht-fade-in mx-auto mt-7 max-w-[560px] text-balance text-center text-[15px] leading-[1.55] text-[rgba(17,17,17,0.64)]"
+ >
+ <span className="font-medium text-[#111111]">{scene.lead}</span>{" "}
+ {scene.body}
+ </p>
+ ) : null}
+ </div>
+ );
+};
+
+const useNarrow = () => {
+ const [narrow, setNarrow] = useState(false);
+ useEffect(() => {
+ const query = window.matchMedia("(max-width: 639px)");
+ const sync = () => setNarrow(query.matches);
+ sync();
+ query.addEventListener("change", sync);
+ return () => query.removeEventListener("change", sync);
+ }, []);
+ return narrow;
+};
+
+const Cropped = ({
+ canvas,
+ crop,
+ children,
+}: {
+ canvas: { w: number; h: number };
+ crop?: { x: number; w: number };
+ children: ReactNode;
+}) => {
+ const narrow = useNarrow();
+ if (!narrow || !crop) return <>{children}</>;
+ return (
+ <div style={{ aspectRatio: `${crop.w} / ${canvas.h}`, overflow: "hidden" }}>
+ <div
+ style={{
+ width: `${(canvas.w / crop.w) * 100}%`,
+ marginLeft: `${(-crop.x / crop.w) * 100}%`,
+ }}
+ >
+ {children}
+ </div>
+ </div>
+ );
+};
+
+export const Loop = ({
+ View,
+ duration,
+ pose,
+ canvas,
+ crop,
+ tone,
+ className,
+}: {
+ tone?: Tone;
+ View: SceneView;
+ duration: number;
+ pose: number;
+ canvas: { w: number; h: number };
+ crop?: { x: number; w: number };
+ className?: string;
+}) => {
+ const ref = useRef<HTMLDivElement | null>(null);
+ const inView = useInView(ref, "-15% 0px -15% 0px");
+ const pageVisible = usePageVisible();
+ const reduced = useReducedMotion();
+ const [round, setRound] = useState(0);
+ const playing = inView && pageVisible && !reduced;
+ const t = useClock({
+ playing,
+ duration,
+ resetKey: String(round),
+ onEnd: () => setRound((r) => r + 1),
+ });
+ return (
+ <div ref={ref} className={className}>
+ <Panel tone={tone}>
+ <Cropped canvas={canvas} crop={crop}>
+ <LazyMount w={canvas.w} h={canvas.h} grow>
+ <Fit w={canvas.w} h={canvas.h} grow>
+ <View
+ key={round}
+ t={reduced ? pose : t}
+ playing={playing}
+ pick={null}
+ />
+ </Fit>
+ </LazyMount>
+ </Cropped>
+ </Panel>
+ </div>
+ );
+};
+
+export {
+ Badge,
+ Details,
+ Em,
+ Section,
+ SectionHead,
+ TONES,
+ type Tone,
+ toneVars,
+} from "./layout";
diff --git a/apps/web/components/pages/HomeTwo/showcase/layout.tsx b/apps/web/components/pages/HomeTwo/showcase/layout.tsx
new file mode 100644
index 000000000..2110976a2
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/layout.tsx
@@ -0,0 +1,229 @@
+import { classNames } from "@cap/utils/helpers";
+import type { CSSProperties, ReactNode } from "react";
+import { BODY_TEXT, GRAIN, H_SECTION } from "../theme";
+
+// Server-safe building blocks for the landing sections: headings, badges,
+// detail lists and the stage panel. No client code, so the copy renders as
+// plain HTML and ships no JavaScript.
+
+export type Tone = { accent: string; ink: string; wash: string };
+
+export const TONES = {
+ instant: { accent: "#3B82F6", ink: "#1D4ED8", wash: "#EAF2FF" },
+ studio: { accent: "#8B5CF6", ink: "#6D28D9", wash: "#F2EDFF" },
+ screenshot: { accent: "#10B981", ink: "#047857", wash: "#E8F8F1" },
+ ai: { accent: "#F59E0B", ink: "#B45309", wash: "#FFF4E2" },
+ storage: { accent: "#0EA5E9", ink: "#0369A1", wash: "#E6F5FD" },
+ agents: { accent: "#22C55E", ink: "#15803D", wash: "#EAF9EF" },
+ platforms: { accent: "#64748B", ink: "#334155", wash: "#F1F4F8" },
+} satisfies Record<string, Tone>;
+
+export const Em = ({ children }: { children: ReactNode }) => (
+ <span className="font-normal text-[var(--tone-ink,#111111)]">{children}</span>
+);
+
+export const toneVars = (tone?: Tone) =>
+ (tone
+ ? {
+ "--tone-ink": tone.ink,
+ "--tone-accent": tone.accent,
+ "--tone-wash": tone.wash,
+ }
+ : {}) as CSSProperties;
+
+export const Badge = ({
+ icon,
+ children,
+}: {
+ icon: ReactNode;
+ children: ReactNode;
+}) => (
+ <span
+ className="inline-flex items-center gap-2 rounded-full bg-white py-1 pl-1 pr-3.5 text-[14px] font-medium tracking-[-0.01em] text-[var(--tone-ink,#111111)]"
+ style={{
+ boxShadow:
+ "0 0 0 1px color-mix(in srgb, var(--tone-accent, #111111) 22%, transparent), 0 8px 20px -12px color-mix(in srgb, var(--tone-accent, #111111) 70%, transparent)",
+ }}
+ >
+ <span
+ aria-hidden="true"
+ className="grid size-6 place-items-center rounded-full text-white shadow-[inset_0_1px_0_rgba(255,255,255,0.35),inset_0_-1px_0_rgba(0,0,0,0.12)] [&>svg]:size-3"
+ style={{
+ background:
+ "linear-gradient(160deg, var(--tone-accent, #111111) 0%, var(--tone-ink, #111111) 100%)",
+ }}
+ >
+ {icon}
+ </span>
+ {children}
+ </span>
+);
+
+export const SectionHead = ({
+ title,
+ badge,
+ children,
+ align = "center",
+ className,
+}: {
+ title: ReactNode;
+ badge?: ReactNode;
+ children?: ReactNode;
+ align?: "center" | "left";
+ className?: string;
+}) => (
+ <div
+ className={classNames(
+ "flex flex-col",
+ align === "center"
+ ? "mx-auto max-w-[760px] items-center text-center"
+ : "max-w-[520px] items-start text-left",
+ className,
+ )}
+ >
+ {badge ? <div className="mb-6">{badge}</div> : null}
+ <h2
+ className={`${H_SECTION} text-balance text-[clamp(34px,4.6vw,54px)] leading-[1.02]`}
+ >
+ {title}
+ </h2>
+ {children ? (
+ <p
+ className={classNames(
+ BODY_TEXT,
+ "mt-5 text-balance text-[17px] leading-[1.5] text-[rgba(17,17,17,0.64)] sm:text-[18px]",
+ align === "center" ? "max-w-[600px]" : "max-w-[480px]",
+ )}
+ >
+ {children}
+ </p>
+ ) : null}
+ </div>
+);
+
+export const Panel = ({
+ tone,
+ className,
+ children,
+}: {
+ tone?: Tone;
+ className?: string;
+ children: ReactNode;
+}) => (
+ <div
+ className={classNames(
+ "relative overflow-hidden rounded-[28px] bg-white shadow-[0_0_0_1px_rgba(17,17,17,0.06),0_1px_2px_rgba(17,17,17,0.03),0_30px_60px_-40px_rgba(17,24,39,0.25)]",
+ className,
+ )}
+ style={
+ tone
+ ? {
+ backgroundColor: "#FFFFFF",
+ backgroundImage: `${GRAIN}, linear-gradient(180deg, ${tone.wash} 0%, #FFFFFF 78%)`,
+ backgroundSize: "200px 200px, auto",
+ }
+ : undefined
+ }
+ >
+ {children}
+ </div>
+);
+
+export type Detail = { lead: string; body: string; icon?: ReactNode };
+
+export const DetailIcon = ({
+ children,
+ className = "mb-3",
+}: {
+ children: ReactNode;
+ className?: string;
+}) => (
+ <span
+ aria-hidden="true"
+ className={`${className} grid size-8 shrink-0 place-items-center rounded-[10px] text-[var(--tone-ink,#111111)] shadow-[inset_0_0_0_1px_rgba(17,17,17,0.05)] [&>svg]:size-4`}
+ style={{
+ background: "color-mix(in srgb, var(--tone-accent, #111111) 12%, white)",
+ }}
+ >
+ {children}
+ </span>
+);
+
+export const Details = ({
+ items,
+ className,
+}: {
+ items: Detail[];
+ className?: string;
+}) => (
+ <ul
+ className={classNames(
+ "grid gap-x-10 gap-y-6 border-t border-[#E3E8EE] pt-8 sm:grid-cols-2 lg:grid-cols-3",
+ className,
+ )}
+ >
+ {items.map((item) => (
+ <li
+ key={item.lead}
+ className="text-[15px] leading-[1.55] text-[rgba(17,17,17,0.64)]"
+ >
+ {item.icon ? <DetailIcon>{item.icon}</DetailIcon> : null}
+ <h3 className="m-0 inline text-[length:inherit] font-medium leading-[inherit] tracking-normal text-[#111111]">
+ {item.lead}
+ </h3>{" "}
+ {item.body}
+ </li>
+ ))}
+ </ul>
+);
+
+export const Section = ({
+ anchor,
+ tone,
+ className,
+ children,
+}: {
+ anchor?: string;
+ tone?: Tone;
+ className?: string;
+ children: ReactNode;
+}) => (
+ <section
+ id={anchor}
+ className={classNames("scroll-mt-24 px-5 py-20 lg:py-32", className)}
+ style={toneVars(tone)}
+ >
+ <div className="mx-auto max-w-[1120px]">{children}</div>
+ </section>
+);
+
+export const FADE_CSS = `
+ @keyframes ht-fade-in { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }
+ .ht-fade-in { animation: ht-fade-in 400ms cubic-bezier(0.16,1,0.3,1) both; }
+ @media (prefers-reduced-motion: reduce) { .ht-fade-in { animation: none; } }
+`;
+
+export const POP_CSS = `
+ @keyframes ht-pop-in { 0% { opacity: 0; transform: translateY(8px) scale(0.96); } 60% { opacity: 1; transform: translateY(-2px) scale(1.01); } 100% { opacity: 1; transform: none; } }
+ .ht-pop-in { animation: ht-pop-in 520ms cubic-bezier(0.16,1,0.3,1) both; }
+ @media (prefers-reduced-motion: reduce) { .ht-pop-in { animation: none; } }
+`;
+
+/**
+ * What each tab of a stage shows, as real text: crawlers and screen readers
+ * get every feature, while sighted visitors see the same words as the
+ * stage's tabs and caption.
+ */
+export const StageFeatures = ({
+ items,
+}: {
+ items: { key: string; lead: string; body: string }[];
+}) => (
+ <ul className="sr-only">
+ {items.map((item) => (
+ <li key={item.key}>
+ <h3>{item.lead}</h3> {item.body}
+ </li>
+ ))}
+ </ul>
+);
diff --git a/apps/web/components/pages/HomeTwo/showcase/ownership.tsx b/apps/web/components/pages/HomeTwo/showcase/ownership.tsx
new file mode 100644
index 000000000..d9893f013
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/ownership.tsx
@@ -0,0 +1,240 @@
+"use client";
+
+import { Check, Server } from "lucide-react";
+import Image from "next/image";
+import type { ReactNode } from "react";
+import { CapLogoMark } from "../demo/capIcons";
+import { copyOf, STORAGE_COPY } from "./copy";
+import { type Scene, Showcase, seg, TONES } from "./kit";
+import { RecordingFrame } from "./share";
+
+export const CANVAS = { w: 1040, h: 440 } as const;
+
+type Destination = {
+ key: string;
+ label: string;
+ name: string;
+ path: string;
+ note: string;
+ icon: ReactNode;
+ tab: ReactNode;
+ lead: string;
+ body: string;
+};
+
+const DESTINATIONS: Destination[] = [
+ {
+ key: "cloud",
+ label: copyOf(STORAGE_COPY, "cloud").label,
+ name: "Cap Cloud",
+ path: "Cap.so/s/m4k8x2qz",
+ note: "Encrypted, shareable the second it lands",
+ icon: <CapLogoMark className="size-7" />,
+ tab: <CapLogoMark />,
+ lead: copyOf(STORAGE_COPY, "cloud").lead,
+ body: copyOf(STORAGE_COPY, "cloud").body,
+ },
+ {
+ key: "s3",
+ label: copyOf(STORAGE_COPY, "s3").label,
+ name: "acme-recordings",
+ path: "s3://acme-recordings",
+ note: "eu-west-2 · your keys, your bill",
+ icon: (
+ <span className="grid size-7 place-items-center rounded-[7px] bg-[#111111] text-[11px] font-semibold text-white">
+ S3
+ </span>
+ ),
+ tab: (
+ <span className="grid size-4 place-items-center rounded-[4px] bg-current text-[7px] font-bold">
+ <span className="text-white">S3</span>
+ </span>
+ ),
+ lead: copyOf(STORAGE_COPY, "s3").lead,
+ body: copyOf(STORAGE_COPY, "s3").body,
+ },
+ {
+ key: "drive",
+ label: copyOf(STORAGE_COPY, "drive").label,
+ name: "Google Drive",
+ path: "My Drive / Cap",
+ note: "A folder your team already uses",
+ icon: (
+ <Image
+ src="/logos/google-drive.svg"
+ alt=""
+ width={28}
+ height={28}
+ className="size-7"
+ />
+ ),
+ tab: <Image src="/logos/google-drive.svg" alt="" width={16} height={16} />,
+ lead: copyOf(STORAGE_COPY, "drive").lead,
+ body: copyOf(STORAGE_COPY, "drive").body,
+ },
+ {
+ key: "self",
+ label: copyOf(STORAGE_COPY, "self").label,
+ name: "Cap on your servers",
+ path: "recordings.acme.internal",
+ note: "The whole platform, on your servers",
+ icon: <Server className="size-6 text-[#111111]" />,
+ tab: <Server />,
+ lead: copyOf(STORAGE_COPY, "self").lead,
+ body: copyOf(STORAGE_COPY, "self").body,
+ },
+];
+
+const FILE = { x: 96, y: 96, w: 312, h: 236 };
+const DEST = { x: 632, y: 132, w: 312, h: 164 };
+const ACCENT = TONES.storage.accent;
+
+const DOTS = {
+ backgroundImage:
+ "radial-gradient(circle at 1px 1px, rgba(17,17,17,0.09) 1px, transparent 0)",
+ backgroundSize: "18px 18px",
+ maskImage:
+ "radial-gradient(ellipse 70% 75% at 50% 45%, #000 40%, transparent 100%)",
+} as const;
+
+const RouteView =
+ (destination: Destination) =>
+ ({ t, playing }: { t: number; playing: boolean }) => {
+ const travel = seg(t, 500, 2400);
+ const done = t > 2500;
+ const startX = FILE.x + FILE.w;
+ const endX = DEST.x;
+ const y = DEST.y + DEST.h / 2;
+ const pct = Math.round(travel * 100);
+ return (
+ <div className="absolute inset-0">
+ <div className="absolute inset-0" style={DOTS} />
+
+ <div
+ className="absolute overflow-hidden rounded-[18px] bg-white shadow-[0_0_0_1px_rgba(17,17,17,0.07),0_24px_60px_-30px_rgba(17,24,39,0.45)]"
+ style={{ left: FILE.x, top: FILE.y, width: FILE.w, height: FILE.h }}
+ >
+ <div className="relative m-2 h-[160px] overflow-hidden rounded-[11px]">
+ <RecordingFrame playing={playing} pageScale={0.4} />
+ </div>
+ <div className="flex items-center justify-between px-4 pb-3 pt-1">
+ <div>
+ <div className="text-[14px] font-semibold text-[#1C1D21]">
+ invoice-walkthrough.mp4
+ </div>
+ <div className="mt-0.5 text-[12.5px] text-[#8A8F98]">
+ 4K · 0:24 · 96 MB
+ </div>
+ </div>
+ <CapLogoMark className="size-5" />
+ </div>
+ </div>
+
+ <svg
+ aria-hidden="true"
+ className="absolute inset-0"
+ width={CANVAS.w}
+ height={CANVAS.h}
+ >
+ <path
+ d={`M ${startX} ${FILE.y + FILE.h / 2} C ${startX + 110} ${FILE.y + FILE.h / 2}, ${endX - 110} ${y}, ${endX} ${y}`}
+ fill="none"
+ stroke="#D5DBE3"
+ strokeWidth={2}
+ strokeDasharray="3 6"
+ strokeLinecap="round"
+ />
+ <path
+ d={`M ${startX} ${FILE.y + FILE.h / 2} C ${startX + 110} ${FILE.y + FILE.h / 2}, ${endX - 110} ${y}, ${endX} ${y}`}
+ fill="none"
+ stroke={ACCENT}
+ strokeWidth={2.5}
+ strokeLinecap="round"
+ pathLength={1}
+ strokeDasharray={1}
+ strokeDashoffset={1 - travel}
+ />
+ </svg>
+
+ <div
+ className="absolute flex -translate-x-1/2 -translate-y-1/2 items-center gap-2 rounded-full bg-white px-3 py-1.5 text-[12.5px] tabular-nums text-[#1C1D21] shadow-[0_0_0_1px_rgba(17,17,17,0.07),0_10px_24px_-12px_rgba(17,24,39,0.35)] transition-opacity duration-300"
+ style={{
+ left: (startX + endX) / 2,
+ top: (FILE.y + FILE.h / 2 + y) / 2,
+ opacity: travel > 0 ? 1 : 0,
+ }}
+ >
+ {done ? (
+ <>
+ <Check className="size-3.5 text-[#16A34A]" strokeWidth={2.6} />
+ Uploaded
+ </>
+ ) : (
+ <>
+ <span className="relative h-1.5 w-16 overflow-hidden rounded-full bg-[#E8EDF3]">
+ <span
+ className="absolute inset-y-0 left-0 rounded-full"
+ style={{ width: `${pct}%`, background: ACCENT }}
+ />
+ </span>
+ {pct}%
+ </>
+ )}
+ </div>
+
+ <div
+ className="absolute flex flex-col justify-between rounded-[18px] bg-white p-5 transition-shadow duration-500"
+ style={{
+ left: DEST.x,
+ top: DEST.y,
+ width: DEST.w,
+ height: DEST.h,
+ boxShadow: done
+ ? `0 0 0 2px color-mix(in srgb, ${ACCENT} 55%, transparent), 0 0 0 7px color-mix(in srgb, ${ACCENT} 12%, transparent), 0 24px 60px -30px rgba(17,24,39,0.45)`
+ : "0 0 0 1px rgba(17,17,17,0.07), 0 24px 60px -30px rgba(17,24,39,0.45)",
+ }}
+ >
+ <div className="flex items-center gap-3">
+ <span className="grid size-11 shrink-0 place-items-center rounded-[12px] bg-[#F4F6F9]">
+ {destination.icon}
+ </span>
+ <div className="min-w-0">
+ <div className="truncate text-[15px] font-semibold text-[#1C1D21]">
+ {destination.name}
+ </div>
+ <div className="truncate text-[12.5px] text-[#8A8F98]">
+ {destination.note}
+ </div>
+ </div>
+ </div>
+ <div className="flex items-center justify-between gap-3 rounded-[10px] bg-[#F4F6F9] px-3 py-2.5">
+ <span className="truncate font-mono text-[12.5px] text-[#33363D]">
+ {destination.path}
+ </span>
+ <span
+ className="flex shrink-0 items-center gap-1 text-[12.5px] text-[#16A34A] transition-opacity duration-300"
+ style={{ opacity: done ? 1 : 0 }}
+ >
+ <Check className="size-3.5" strokeWidth={2.6} />
+ Saved
+ </span>
+ </div>
+ </div>
+ </div>
+ );
+ };
+
+const SCENES: Scene[] = DESTINATIONS.map((destination) => ({
+ key: destination.key,
+ label: destination.label,
+ icon: destination.tab,
+ lead: destination.lead,
+ body: destination.body,
+ duration: 4200,
+ pose: 3000,
+ View: RouteView(destination),
+}));
+
+export const OwnershipStage = () => (
+ <Showcase scenes={SCENES} tone={TONES.storage} canvas={CANVAS} />
+);
diff --git a/apps/web/components/pages/HomeTwo/showcase/physics.ts b/apps/web/components/pages/HomeTwo/showcase/physics.ts
new file mode 100644
index 000000000..a88e0d82a
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/physics.ts
@@ -0,0 +1,119 @@
+/**
+ * The desktop editor's motion, ported so the landing demos move the way an
+ * exported Cap does. Constants mirror the Rust defaults:
+ *
+ * - ZOOM_SPRING: `ScreenMovementSpring::default()` (crates/project), which
+ * drives both the zoom amount and the zoom centre
+ * (crates/rendering/src/zoom_spring.rs).
+ * - CURSOR_SPRING: the default "Mellow" cursor smoothing preset.
+ * - CLICK: `CLICK_SHRINK_SIZE` / `CURSOR_CLICK_DURATION` in
+ * crates/rendering/src/layers/cursor.rs.
+ * - AUTO_ZOOM: the click padding in `generate_zoom_segments_from_clicks`,
+ * the 1.5x default zoom amount, and `ZoomSegment::default_edge_snap_ratio`.
+ */
+
+export type SpringConfig = { tension: number; mass: number; friction: number };
+
+export const ZOOM_SPRING: SpringConfig = {
+ tension: 200,
+ mass: 2.25,
+ friction: 40,
+};
+
+export const CURSOR_SPRING: SpringConfig = {
+ tension: 470,
+ mass: 3,
+ friction: 70,
+};
+
+export const CLICK = { shrink: 0.8, ms: 130 } as const;
+
+export const AUTO_ZOOM = {
+ amount: 1.5,
+ preMs: 300,
+ postMs: 2500,
+ edgeSnap: 0.25,
+} as const;
+
+/** `SegmentBounds::snap_to_edges`: focus near an edge sticks to it. */
+export const snapToEdges = (scalar: number, ratio = AUTO_ZOOM.edgeSnap) => {
+ if (ratio <= 0) return scalar;
+ const lo = ratio;
+ const hi = 1 - ratio + 0.0001;
+ if (hi <= lo) return 0.5;
+ return Math.min(1, Math.max(0, (scalar - lo) / (hi - lo)));
+};
+
+/**
+ * Runs a spring-mass-damper toward a moving target and samples it on a fixed
+ * grid, like the editor's precomputed zoom timeline. Integration uses 1 ms
+ * semi-implicit Euler steps; the springs here are near critical damping, so
+ * that matches the editor's closed-form solution to well under a pixel.
+ */
+export const simulate = (
+ target: (ms: number) => number[],
+ durationMs: number,
+ spring: SpringConfig,
+ sampleMs = 4,
+): number[][] => {
+ const start = target(0);
+ const position = [...start];
+ const velocity = start.map(() => 0);
+ const samples: number[][] = [[...position]];
+ const dt = 0.001;
+ for (let ms = 1; ms <= durationMs; ms++) {
+ const goal = target(ms);
+ for (let i = 0; i < position.length; i++) {
+ const p = position[i] ?? 0;
+ const v = velocity[i] ?? 0;
+ const force =
+ -spring.tension * (p - (goal[i] ?? 0)) - spring.friction * v;
+ const next = v + (force / spring.mass) * dt;
+ velocity[i] = next;
+ position[i] = p + next * dt;
+ }
+ if (ms % sampleMs === 0) samples.push([...position]);
+ }
+ return samples;
+};
+
+export const sampleAt = (samples: number[][], ms: number, sampleMs = 4) => {
+ const f = Math.max(0, ms / sampleMs);
+ const i = Math.min(samples.length - 1, Math.floor(f));
+ const j = Math.min(samples.length - 1, i + 1);
+ const k = f - i;
+ const a = samples[i] ?? [];
+ const b = samples[j] ?? a;
+ return a.map((v, n) => v + ((b[n] ?? v) - v) * k);
+};
+
+export const minJerk = (s: number) => {
+ const x = Math.min(1, Math.max(0, s));
+ return x * x * x * (10 - 15 * x + 6 * x * x);
+};
+
+export type Waypoint = { at: number; x: number; y: number; hold?: number };
+
+export const rawPath = (points: Waypoint[]) => (ms: number) => {
+ const first = points[0];
+ if (!first) return [0, 0];
+ let prev = first;
+ for (const point of points) {
+ if (ms < point.at) {
+ const from = prev.at + (prev.hold ?? 0);
+ const k = minJerk((ms - from) / Math.max(1, point.at - from));
+ return [prev.x + (point.x - prev.x) * k, prev.y + (point.y - prev.y) * k];
+ }
+ prev = point;
+ }
+ return [prev.x, prev.y];
+};
+
+export const clickShrink = (ms: number, downAt: number) => {
+ const t = ms - downAt;
+ if (t < -CLICK.ms || t > CLICK.ms * 2) return 1;
+ const k =
+ t < 0 ? 1 + t / CLICK.ms : t < CLICK.ms ? 0 : (t - CLICK.ms) / CLICK.ms;
+ const eased = Math.min(1, Math.max(0, k));
+ return CLICK.shrink + (1 - CLICK.shrink) * eased;
+};
diff --git a/apps/web/components/pages/HomeTwo/showcase/platforms.tsx b/apps/web/components/pages/HomeTwo/showcase/platforms.tsx
new file mode 100644
index 000000000..dace623af
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/platforms.tsx
@@ -0,0 +1,185 @@
+"use client";
+
+import { classNames } from "@cap/utils/helpers";
+import { useDetectPlatform } from "hooks/useDetectPlatform";
+import { ArrowUpRight, Download, Monitor } from "lucide-react";
+import Image from "next/image";
+import Link from "next/link";
+import type { ReactNode } from "react";
+import { getDownloadUrl } from "@/utils/platform";
+import { AppleGlyph } from "../glyphs";
+import { MONO } from "../theme";
+import { Badge, Em, Section, SectionHead, TONES } from "./kit";
+
+const WindowsGlyph = ({ className }: { className?: string }) => (
+ <svg
+ aria-hidden="true"
+ className={className}
+ viewBox="0 0 24 24"
+ fill="currentColor"
+ >
+ <path d="M0,0H11.377V11.372H0ZM12.623,0H24V11.372H12.623ZM0,12.623H11.377V24H0Zm12.623,0H24V24H12.623" />
+ </svg>
+);
+
+const TILE =
+ "flex flex-col rounded-[20px] bg-white p-6 shadow-[0_0_0_1px_rgba(17,17,17,0.06),0_1px_2px_rgba(17,17,17,0.03)]";
+
+const Desktop = ({
+ name,
+ note,
+ href,
+ glyph,
+ yours,
+}: {
+ name: string;
+ note: string;
+ href: string;
+ glyph: ReactNode;
+ yours: boolean;
+}) => (
+ <div className={TILE}>
+ <div className="flex items-center justify-between">
+ <span className="grid size-11 place-items-center rounded-[12px] bg-[#F1F3F6] text-[#111111]">
+ {glyph}
+ </span>
+ {yours ? (
+ <span className="text-[13px] text-[rgba(17,17,17,0.64)]">
+ Your device
+ </span>
+ ) : null}
+ </div>
+ <h3 className="mt-8 text-[22px] font-normal tracking-[-0.02em] text-[#111111]">
+ {name}
+ </h3>
+ <div className="mt-1 text-[14.5px] leading-[1.5] text-[rgba(17,17,17,0.64)]">
+ {note}
+ </div>
+ <Link
+ href={href}
+ prefetch={false}
+ className={classNames(
+ "mt-6 inline-flex h-10 items-center justify-center gap-2 rounded-[10px] text-[14.5px] transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#111111]",
+ yours
+ ? "bg-[#111111] text-white hover:bg-[#2A2A2A]"
+ : "bg-[#F1F3F6] text-[#111111] hover:bg-[#E6EAEF]",
+ )}
+ >
+ <Download className="size-4" />
+ Download for {name}
+ </Link>
+ </div>
+);
+
+const TERMINAL = [
+ "cap record start --mode studio --duration 30",
+ "cap export ./recording.cap --quality 4k",
+ "cap upload ./recording.mp4 --json",
+];
+
+export const PlatformsSection = () => {
+ const { platform, isIntel } = useDetectPlatform();
+ const current = platform ?? "macos";
+ return (
+ <Section anchor="platforms" tone={TONES.platforms}>
+ <SectionHead
+ title="Native on Mac, Windows and Linux"
+ badge={<Badge icon={<Monitor />}>Every platform</Badge>}
+ >
+ A real desktop app on all three, with{" "}
+ <Em>the same recorder, editor, and share link</Em>. Plus a Chrome
+ extension and a CLI for everything else.
+ </SectionHead>
+ <div className="mt-14 grid gap-3 md:grid-cols-3">
+ <Desktop
+ name="macOS"
+ note="Apple silicon and Intel. macOS 13.1 or newer."
+ href={getDownloadUrl("macos", isIntel)}
+ glyph={<AppleGlyph className="size-5" />}
+ yours={current === "macos"}
+ />
+ <Desktop
+ name="Windows"
+ note="Windows 10 or newer."
+ href={getDownloadUrl("windows", false)}
+ glyph={<WindowsGlyph className="size-[18px]" />}
+ yours={current === "windows"}
+ />
+ <Desktop
+ name="Linux"
+ note="Debian and Ubuntu, as a .deb package."
+ href={getDownloadUrl("linux", false)}
+ glyph={
+ <Image
+ src="/logos/os/linux.svg"
+ alt=""
+ width={22}
+ height={22}
+ className="size-[22px]"
+ />
+ }
+ yours={current === "linux"}
+ />
+ </div>
+ <div className="mt-3 grid gap-3 md:grid-cols-[1fr_1.4fr]">
+ <Link
+ href="/download"
+ className={classNames(
+ TILE,
+ "group justify-between transition-shadow duration-200 hover:shadow-[0_0_0_1px_rgba(17,17,17,0.12)]",
+ )}
+ >
+ <span className="flex items-start justify-between">
+ <Image
+ src="/logos/browsers/google-chrome.svg"
+ alt=""
+ width={36}
+ height={36}
+ className="size-9"
+ />
+ <ArrowUpRight className="size-4 text-[rgba(17,17,17,0.4)] transition-colors group-hover:text-[#111111]" />
+ </span>
+ <div className="mt-8">
+ <h3 className="block text-[18px] font-normal tracking-[-0.02em] text-[#111111]">
+ Chrome extension
+ </h3>
+ <span className="mt-1 block text-[14.5px] text-[rgba(17,17,17,0.64)]">
+ Record a tab or your whole screen without leaving the browser.
+ </span>
+ </div>
+ </Link>
+ <Link
+ href="/docs"
+ className={classNames(
+ TILE,
+ "group transition-shadow duration-200 hover:shadow-[0_0_0_1px_rgba(17,17,17,0.12)]",
+ )}
+ >
+ <div className="flex items-start justify-between">
+ <h3 className="block text-[18px] font-normal tracking-[-0.02em] text-[#111111]">
+ Built for developers
+ </h3>
+ <ArrowUpRight className="size-4 text-[rgba(17,17,17,0.4)] transition-colors group-hover:text-[#111111]" />
+ </div>
+ <span className="mt-1 block text-[14.5px] text-[rgba(17,17,17,0.64)]">
+ Record, export, and publish from the terminal, the API, or the MCP
+ server.
+ </span>
+ <span
+ className={classNames(
+ MONO,
+ "mt-5 block rounded-[12px] bg-[#F4F5F7] p-4 text-[12px] leading-[1.8] text-[#33363D]",
+ )}
+ >
+ {TERMINAL.map((line) => (
+ <span key={line} className="block break-words">
+ <span className="text-[rgba(17,17,17,0.35)]">$ </span>
+ {line}
+ </span>
+ ))}
+ </span>
+ </Link>
+ </div>
+ </Section>
+ );
+};
diff --git a/apps/web/components/pages/HomeTwo/showcase/screenshot.tsx b/apps/web/components/pages/HomeTwo/showcase/screenshot.tsx
new file mode 100644
index 000000000..f88cab15e
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/screenshot.tsx
@@ -0,0 +1,426 @@
+"use client";
+
+import { clamp01, Loop, lerp, seg, TONES } from "./kit";
+import { INVOICE, InvoicePage } from "./subjects";
+
+export const CANVAS = { w: 640, h: 520 } as const;
+
+const INK = "#F43F5E";
+
+const WIN = { x: 55, y: 96, scale: 0.78 } as const;
+
+const CROP = { x: 364, y: 277, w: 304, h: 133 } as const;
+
+const PAD = 34;
+const IMG = { x: 84, y: 172, w: 472 } as const;
+const SCALE_OUT = IMG.w / CROP.w;
+const IMG_H = CROP.h * SCALE_OUT;
+const CARD = {
+ x: IMG.x - PAD,
+ y: IMG.y - PAD,
+ w: IMG.w + PAD * 2,
+ h: IMG_H + PAD * 2,
+};
+
+const at = (px: number, py: number) => ({
+ x: IMG.x + (px - CROP.x) * SCALE_OUT,
+ y: IMG.y + (py - CROP.y) * SCALE_OUT,
+});
+
+const SEL = {
+ x: WIN.x + CROP.x * WIN.scale,
+ y: WIN.y + CROP.y * WIN.scale,
+ w: CROP.w * WIN.scale,
+ h: CROP.h * WIN.scale,
+};
+
+type Pt = { x: number; y: number };
+
+/**
+ * A marker arrow built from its geometry: a curved shaft that stops at the
+ * base of the head, and a head aimed along the curve's tangent at the tip,
+ * so the two always meet cleanly whatever the angle.
+ */
+const Arrow = ({
+ from,
+ ctrl,
+ to,
+ k,
+ width = 5,
+ head = 17,
+}: {
+ from: Pt;
+ ctrl: Pt;
+ to: Pt;
+ k: number;
+ width?: number;
+ head?: number;
+}) => {
+ const dx = to.x - ctrl.x;
+ const dy = to.y - ctrl.y;
+ const len = Math.hypot(dx, dy) || 1;
+ const ux = dx / len;
+ const uy = dy / len;
+ const base = { x: to.x - ux * head, y: to.y - uy * head };
+ const wing = head * 0.62;
+ const left = { x: base.x - uy * wing, y: base.y + ux * wing };
+ const right = { x: base.x + uy * wing, y: base.y - ux * wing };
+ const shaft = Math.min(1, k / 0.82);
+ const tip = seg(k, 0.8, 1);
+ return (
+ <g>
+ <path
+ d={`M ${from.x} ${from.y} Q ${ctrl.x} ${ctrl.y} ${base.x} ${base.y}`}
+ fill="none"
+ stroke={INK}
+ strokeWidth={width}
+ strokeLinecap="round"
+ pathLength={1}
+ strokeDasharray={1}
+ strokeDashoffset={1 - shaft}
+ />
+ <path
+ d={`M ${to.x} ${to.y} L ${left.x} ${left.y} L ${right.x} ${right.y} Z`}
+ fill={INK}
+ stroke={INK}
+ strokeWidth={2.5}
+ strokeLinejoin="round"
+ style={{
+ opacity: tip > 0 ? 1 : 0,
+ transform: `scale(${0.4 + tip * 0.6})`,
+ transformOrigin: `${to.x}px ${to.y}px`,
+ }}
+ />
+ </g>
+ );
+};
+
+const Box = ({
+ x,
+ y,
+ w,
+ h,
+ k,
+}: {
+ x: number;
+ y: number;
+ w: number;
+ h: number;
+ k: number;
+}) => (
+ <rect
+ x={x}
+ y={y}
+ width={w}
+ height={h}
+ rx={10}
+ fill="none"
+ stroke={INK}
+ strokeWidth={4}
+ strokeLinecap="round"
+ pathLength={1}
+ strokeDasharray={1}
+ strokeDashoffset={1 - k}
+ />
+);
+
+const Handle = ({ x, y }: Pt) => (
+ <span
+ className="absolute size-[9px] -translate-x-1/2 -translate-y-1/2 rounded-full border-[1.5px] border-white bg-[#2563EB] shadow-[0_1px_2px_rgba(0,0,0,0.3)]"
+ style={{ left: x, top: y }}
+ />
+);
+
+const Crosshair = ({ x, y }: Pt) => (
+ <svg
+ aria-hidden="true"
+ className="absolute z-30 -translate-x-1/2 -translate-y-1/2"
+ style={{ left: x, top: y }}
+ width={23}
+ height={23}
+ viewBox="0 0 23 23"
+ >
+ <path d="M11.5 0v23M0 11.5h23" stroke="#FFFFFF" strokeWidth={3.5} />
+ <path d="M11.5 0v23M0 11.5h23" stroke="#111111" strokeWidth={1.5} />
+ </svg>
+);
+
+const TOOLS = [
+ {
+ key: "box",
+ d: "M5 5h14v14H5z",
+ },
+ {
+ key: "arrow",
+ d: "M6 18 18 6M9 6h9v9",
+ },
+ {
+ key: "text",
+ d: "M6 6h12M12 6v13",
+ },
+ {
+ key: "blur",
+ d: "M4 4h16v16H4zM4 12h16M12 4v16M4 4h8v8H4zM12 12h8v8h-8z",
+ },
+] as const;
+
+const Toolbar = ({ active, k }: { active: string | null; k: number }) => (
+ <div
+ className="absolute flex -translate-x-1/2 items-center gap-1 rounded-[14px] bg-white p-1.5 shadow-[0_0_0_1px_rgba(17,17,17,0.07),0_12px_28px_-12px_rgba(17,24,39,0.35)]"
+ style={{
+ left: CANVAS.w / 2,
+ top: CARD.y - 64,
+ opacity: k,
+ transform: `translate(-50%, ${(1 - k) * 8}px)`,
+ }}
+ >
+ {TOOLS.map((tool) => {
+ const on = tool.key === active;
+ return (
+ <span
+ key={tool.key}
+ className="grid size-9 place-items-center rounded-[10px] transition-colors duration-200"
+ style={{
+ background: on ? "#FFE4E8" : "transparent",
+ color: on ? INK : "#5B6069",
+ }}
+ >
+ <svg
+ aria-hidden="true"
+ viewBox="0 0 24 24"
+ className="size-[18px]"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth={2}
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ >
+ <path d={tool.d} />
+ </svg>
+ </span>
+ );
+ })}
+ <span className="mx-1 h-5 w-px bg-[#E5E7EB]" />
+ <span className="size-5 rounded-full" style={{ background: INK }} />
+ <span className="ml-1 mr-1.5 text-[13px] text-[#1C1D21]">Copy</span>
+ </div>
+);
+
+const LABEL = "Ready to send";
+
+const ScreenshotView = ({ t }: { t: number; playing: boolean }) => {
+ const drag = seg(t, 450, 1350);
+ const flash = t > 1420 && t < 1760 ? 1 - (t - 1420) / 340 : 0;
+ const lift = seg(t, 1600, 2500);
+ const tools = seg(t, 2400, 2750);
+ const boxK = seg(t, 2900, 3450);
+ const arrowK = clamp01((t - 3600) / 650);
+ const typed = LABEL.slice(0, Math.max(0, Math.floor((t - 4400) / 55)));
+ const toast = seg(t, 5300, 5600) * (1 - seg(t, 7000, 7300));
+ const active =
+ t > 2800 && t < 3500
+ ? "box"
+ : t > 3500 && t < 4350
+ ? "arrow"
+ : t > 4350 && t < 5300
+ ? "text"
+ : null;
+
+ const crop = {
+ x: lerp(SEL.x, IMG.x, lift),
+ y: lerp(SEL.y, IMG.y, lift),
+ w: lerp(SEL.w, IMG.w, lift),
+ h: lerp(SEL.h, IMG_H, lift),
+ };
+ const scale = crop.w / CROP.w;
+ const card = {
+ x: lerp(crop.x, CARD.x, lift),
+ y: lerp(crop.y, CARD.y, lift),
+ w: lerp(crop.w, CARD.w, lift),
+ h: lerp(crop.h, CARD.h, lift),
+ };
+
+ const selW = SEL.w * drag;
+ const selH = SEL.h * drag;
+ const capturing = drag > 0 && t < 1600;
+
+ const total = { a: at(564, 276), b: at(660, 307) };
+ const tail = at(394, 318);
+ const tip = at(515, 378);
+ const ctrl = at(404, 390);
+ const label = at(376, 284);
+
+ return (
+ <div className="absolute inset-0 overflow-hidden">
+ <div
+ className="absolute overflow-hidden rounded-[10px] shadow-[0_0_0_1px_rgba(0,0,0,0.08),0_18px_44px_-20px_rgba(17,24,39,0.45)]"
+ style={{
+ left: WIN.x,
+ top: WIN.y,
+ width: INVOICE.w * WIN.scale,
+ height: INVOICE.h * WIN.scale,
+ opacity: 1 - lift,
+ }}
+ >
+ <div
+ style={{
+ width: INVOICE.w,
+ height: INVOICE.h,
+ transform: `scale(${WIN.scale})`,
+ transformOrigin: "top left",
+ }}
+ >
+ <InvoicePage />
+ </div>
+ </div>
+
+ {capturing ? (
+ <>
+ <div
+ className="absolute inset-0 bg-[rgba(17,17,17,0.16)]"
+ style={{
+ clipPath: `polygon(0 0, 100% 0, 100% 100%, 0 100%, 0 ${SEL.y}px, ${SEL.x}px ${SEL.y}px, ${SEL.x}px ${SEL.y + selH}px, ${SEL.x + selW}px ${SEL.y + selH}px, ${SEL.x + selW}px ${SEL.y}px, 0 ${SEL.y}px)`,
+ }}
+ />
+ <div
+ className="absolute border border-white shadow-[0_0_0_1px_rgba(37,99,235,0.6)]"
+ style={{ left: SEL.x, top: SEL.y, width: selW, height: selH }}
+ />
+ {drag >= 1 ? (
+ <>
+ <Handle x={SEL.x} y={SEL.y} />
+ <Handle x={SEL.x + SEL.w} y={SEL.y} />
+ <Handle x={SEL.x} y={SEL.y + SEL.h} />
+ <Handle x={SEL.x + SEL.w} y={SEL.y + SEL.h} />
+ </>
+ ) : null}
+ <div
+ className="absolute whitespace-nowrap rounded-[6px] bg-[rgba(17,17,17,0.86)] px-2 py-[3px] text-[11.5px] tabular-nums text-white"
+ style={{
+ left: SEL.x + selW,
+ top: SEL.y + selH + 12,
+ transform: "translateX(-100%)",
+ }}
+ >
+ {Math.round((selW / WIN.scale) * 2)} ×{" "}
+ {Math.round((selH / WIN.scale) * 2)}
+ </div>
+ <Crosshair x={SEL.x + selW} y={SEL.y + selH} />
+ </>
+ ) : null}
+ {t < 450 ? <Crosshair x={SEL.x} y={SEL.y} /> : null}
+
+ <div
+ className="absolute overflow-hidden"
+ style={{
+ left: card.x,
+ top: card.y,
+ width: card.w,
+ height: card.h,
+ borderRadius: 18 * lift,
+ opacity: lift,
+ background:
+ "radial-gradient(120% 140% at 0% 0%, #A7F3D0 0%, transparent 55%), radial-gradient(120% 140% at 100% 100%, #93C5FD 0%, transparent 55%), linear-gradient(135deg, #6EE7B7 0%, #60A5FA 100%)",
+ boxShadow: `0 ${30 * lift}px ${60 * lift}px -30px rgba(17,24,39,0.45)`,
+ }}
+ />
+
+ {t >= 1420 ? (
+ <div
+ className="absolute overflow-hidden bg-white"
+ style={{
+ left: crop.x,
+ top: crop.y,
+ width: crop.w,
+ height: crop.h,
+ borderRadius: lerp(2, 12, lift),
+ boxShadow: `0 ${10 * lift}px ${30 * lift}px -12px rgba(17,24,39,${0.4 * lift})`,
+ }}
+ >
+ <div
+ style={{
+ width: INVOICE.w,
+ height: INVOICE.h,
+ transform: `scale(${scale}) translate(${-CROP.x}px, ${-CROP.y}px)`,
+ transformOrigin: "top left",
+ }}
+ >
+ <InvoicePage />
+ </div>
+ </div>
+ ) : null}
+
+ <Toolbar active={active} k={tools} />
+
+ <svg
+ aria-hidden="true"
+ className="pointer-events-none absolute inset-0"
+ width={CANVAS.w}
+ height={CANVAS.h}
+ >
+ {boxK > 0 ? (
+ <Box
+ x={total.a.x}
+ y={total.a.y}
+ w={total.b.x - total.a.x}
+ h={total.b.y - total.a.y}
+ k={boxK}
+ />
+ ) : null}
+ {arrowK > 0 ? (
+ <Arrow from={tail} ctrl={ctrl} to={tip} k={arrowK} />
+ ) : null}
+ </svg>
+
+ {typed ? (
+ <div
+ className="absolute text-[19px] font-bold tracking-[-0.01em]"
+ style={{ left: label.x, top: label.y, color: INK }}
+ >
+ {typed}
+ {typed.length < LABEL.length ? (
+ <span className="ml-0.5 inline-block h-[18px] w-[2px] translate-y-[3px] bg-current" />
+ ) : null}
+ </div>
+ ) : null}
+
+ <div
+ className="absolute inset-0 bg-white"
+ style={{ opacity: flash * 0.85 }}
+ />
+
+ <div
+ className="absolute left-1/2 flex items-center gap-2.5 rounded-full bg-[#111111] py-1.5 pl-1.5 pr-4 text-[14px] text-white shadow-[0_12px_30px_-12px_rgba(0,0,0,0.5)]"
+ style={{
+ top: CARD.y + CARD.h + 26,
+ opacity: toast,
+ transform: `translate(-50%, ${(1 - toast) * 10}px)`,
+ }}
+ >
+ <span className="grid size-6 place-items-center rounded-full bg-[#22C55E]">
+ <svg aria-hidden="true" viewBox="0 0 16 16" className="size-3.5">
+ <path
+ d="M3.5 8.5l3 3 6-7"
+ fill="none"
+ stroke="#FFFFFF"
+ strokeWidth={2.2}
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ />
+ </svg>
+ </span>
+ Copied to clipboard
+ </div>
+ </div>
+ );
+};
+
+export const ScreenshotStage = () => (
+ <Loop
+ View={ScreenshotView}
+ duration={7600}
+ pose={6200}
+ canvas={CANVAS}
+ tone={TONES.screenshot}
+ />
+);
diff --git a/apps/web/components/pages/HomeTwo/showcase/sections.tsx b/apps/web/components/pages/HomeTwo/showcase/sections.tsx
new file mode 100644
index 000000000..53880a71e
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/sections.tsx
@@ -0,0 +1,377 @@
+import { classNames } from "@cap/utils/helpers";
+import {
+ ArrowUpRight,
+ AudioLines,
+ Bot,
+ Braces,
+ Captions,
+ ChartNoAxesColumn,
+ ClipboardCheck,
+ Code,
+ Crop,
+ Download,
+ Github,
+ Globe,
+ Hand,
+ HardDrive,
+ HeartPulse,
+ Image as ImageIcon,
+ Import,
+ Layers,
+ Lock,
+ LockOpen,
+ MessageSquare,
+ MousePointer2,
+ MoveUpRight,
+ Palette,
+ PencilLine,
+ Plug,
+ Rotate3d,
+ Scissors,
+ ScrollText,
+ ShieldCheck,
+ SquareDashedMousePointer,
+ SquareTerminal,
+ Type,
+ UserRound,
+ Wand,
+ Zap,
+} from "lucide-react";
+import Link from "next/link";
+import {
+ InstantIcon,
+ ScreenshotIcon,
+ StudioIcon,
+} from "@/components/pages/HomePage/modeIcons";
+import { BTN_SECONDARY } from "../theme";
+import { AI_COPY, INSTANT_COPY, STORAGE_COPY, STUDIO_COPY } from "./copy";
+import {
+ Badge,
+ Details,
+ Em,
+ Section,
+ SectionHead,
+ StageFeatures,
+ TONES,
+} from "./layout";
+import { SplitSection } from "./split";
+import { Stage } from "./stage";
+
+// Server rendered: headings, copy and detail lists are plain HTML for
+// crawlers and first paint. Only the animated stages are client code, and
+// those load as the visitor scrolls near them. Canvas sizes mirror the
+// CANVAS constants in each stage module.
+
+const WIDE = { w: 1040, h: 600 };
+
+export const InstantSection = () => (
+ <Section anchor="instant" tone={TONES.instant}>
+ <SectionHead
+ title="Stop recording. The link is already copied."
+ badge={<Badge icon={<InstantIcon />}>Instant Mode</Badge>}
+ >
+ <Em>Instant Mode</Em> uploads while you talk, so there is nothing to wait
+ for. Paste the link anywhere and <Em>people watch in the browser</Em>, no
+ account needed.
+ </SectionHead>
+ <Stage
+ name="instant"
+ canvas={WIDE}
+ tone={TONES.instant}
+ className="mt-14"
+ />
+ <StageFeatures items={INSTANT_COPY} />
+ <Details
+ className="mt-16"
+ items={[
+ {
+ lead: "Screen, window, or area.",
+ icon: <SquareDashedMousePointer />,
+ body: "Record a whole display, a single window, a custom area, or just your camera.",
+ },
+ {
+ lead: "Microphone and system audio.",
+ icon: <AudioLines />,
+ body: "Capture your voice and the sound from your computer together.",
+ },
+ {
+ lead: "Password protected links.",
+ icon: <Lock />,
+ body: "Lock a Cap behind a password, keep it to your organization, or make it public.",
+ },
+ {
+ lead: "Viewer analytics.",
+ icon: <ChartNoAxesColumn />,
+ body: "Views, comments, and reactions on every Cap, broken down by country, city, browser, and device.",
+ },
+ {
+ lead: "Custom domains.",
+ icon: <Globe />,
+ body: "Share from cap.yourcompany.com instead of Cap.so on Cap Pro.",
+ },
+ {
+ lead: "Embed anywhere.",
+ icon: <Code />,
+ body: "Drop a Cap into docs, a help center, or your site with an embed code.",
+ },
+ ]}
+ />
+ </Section>
+);
+
+export const StudioSection = () => (
+ <Section anchor="studio" tone={TONES.studio}>
+ <SectionHead
+ title="Polish it before anyone sees it"
+ badge={<Badge icon={<StudioIcon />}>Studio Mode</Badge>}
+ >
+ <Em>Studio Mode</Em> records in full quality straight to your computer,
+ then opens an editor made for screen recordings.{" "}
+ <Em>Zooms, layouts, captions, and privacy</Em> take a click each.
+ </SectionHead>
+ <Stage name="studio" canvas={WIDE} tone={TONES.studio} className="mt-14" />
+ <StageFeatures items={STUDIO_COPY} />
+ <Details
+ className="mt-16"
+ items={[
+ {
+ lead: "Animated text and titles.",
+ icon: <Type />,
+ body: "Titles, lower thirds, and callouts that fade, slide, pop, or type in word by word.",
+ },
+ {
+ lead: "Cursor smoothing and click effects.",
+ icon: <MousePointer2 />,
+ body: "Smooth out shaky mouse movement, resize the cursor, add click animations and motion blur, and hide it when idle.",
+ },
+ {
+ lead: "3D camera effects.",
+ icon: <Rotate3d />,
+ body: "Tilt, rotate, and pan the frame in 3D, with radial, directional, or tilt shift blur.",
+ },
+ {
+ lead: "Color grading.",
+ icon: <Palette />,
+ body: "Presets like Cinematic, Noir, Vintage, and Golden, plus exposure, contrast, saturation, and vignette for the screen and camera.",
+ },
+ {
+ lead: "Trim, split, and speed up.",
+ icon: <Scissors />,
+ body: "Cut and rearrange clips, change speed from 0.25x to 8x, and add music from the built in library.",
+ },
+ {
+ lead: "4K export at 60fps.",
+ icon: <Download />,
+ body: "Export an MP4 or a GIF, or share it as a Cap.so link like everything else.",
+ },
+ {
+ lead: "Camera background blur.",
+ icon: <UserRound />,
+ body: "Blur the room behind you on your webcam, without a green screen.",
+ },
+ {
+ lead: "Separate screen, camera, and mic tracks.",
+ icon: <Layers />,
+ body: "Each is recorded on its own track, so you can fix one without touching the others.",
+ },
+ {
+ lead: "Crop and aspect ratios.",
+ icon: <Crop />,
+ body: "Crop to the part that matters and export wide, square, or vertical for social.",
+ },
+ ]}
+ />
+ </Section>
+);
+
+export const ScreenshotSection = () => (
+ <SplitSection
+ anchor="screenshot"
+ tone={TONES.screenshot}
+ title="Screenshots that look designed"
+ badge={<Badge icon={<ScreenshotIcon />}>Screenshot Mode</Badge>}
+ lede={
+ <>
+ <Em>Screenshot Mode</Em> grabs any window or area with one hotkey and
+ drops it on a background with padding and a shadow.{" "}
+ <Em>It is on your clipboard</Em> before you let go.
+ </>
+ }
+ details={[
+ {
+ lead: "Screenshot backgrounds.",
+ icon: <ImageIcon />,
+ body: "Wallpapers, gradients, or a solid color, with rounded corners and a soft shadow.",
+ },
+ {
+ lead: "Annotation tools.",
+ icon: <MoveUpRight />,
+ body: "Arrows, rectangles, circles, text, and blur to point out the bug or the button you mean.",
+ },
+ {
+ lead: "Copy to clipboard.",
+ icon: <ClipboardCheck />,
+ body: "Your screenshot is copied the moment you let go, or saved as a PNG or shared as a Cap.so link.",
+ },
+ {
+ lead: "Any window or area.",
+ icon: <SquareDashedMousePointer />,
+ body: "Grab a full display, one window, or a region you draw, from a single hotkey.",
+ },
+ ]}
+ visual={
+ <Stage
+ name="screenshot"
+ canvas={{ w: 640, h: 520 }}
+ tone={TONES.screenshot}
+ caption={false}
+ />
+ }
+ />
+);
+
+export const AiSection = () => (
+ <Section anchor="ai" tone={TONES.ai}>
+ <SectionHead
+ title="Every recording, understood"
+ badge={<Badge icon={<ScrollText />}>Cap AI</Badge>}
+ >
+ Titles, summaries, chapters, and a transcript arrive with every recording,
+ right on the share page, so{" "}
+ <Em>the work after recording is already done</Em>.
+ </SectionHead>
+ <Stage name="ai" canvas={WIDE} tone={TONES.ai} className="mt-14" />
+ <StageFeatures items={AI_COPY} />
+ <Details
+ className="mt-16"
+ items={[
+ {
+ lead: "No prompts needed.",
+ icon: <Zap />,
+ body: "AI runs on every Cap automatically. There is nothing to write and no button to press.",
+ },
+ {
+ lead: "Edit the transcript and summary.",
+ icon: <PencilLine />,
+ body: "Fix a misheard word or rewrite the summary right on the share page.",
+ },
+ {
+ lead: "Captions for viewers.",
+ icon: <Captions />,
+ body: "The transcript doubles as captions viewers can switch on in the player.",
+ },
+ {
+ lead: "Timestamped comments.",
+ icon: <MessageSquare />,
+ body: "Feedback lands on the second it is about, next to the transcript.",
+ },
+ {
+ lead: "Readable by your AI agents.",
+ icon: <Bot />,
+ body: "Transcripts and summaries come back as data from the Cap CLI and MCP server.",
+ },
+ {
+ lead: "Works with imported Loom videos.",
+ icon: <Import />,
+ body: "Videos you bring over from Loom get a title, transcript, and chapters too.",
+ },
+ ]}
+ />
+ </Section>
+);
+
+export const OwnershipSection = () => (
+ <Section anchor="ownership" tone={TONES.storage}>
+ <SectionHead
+ title="Your recordings. Actually yours."
+ badge={<Badge icon={<HardDrive />}>Storage you own</Badge>}
+ >
+ Cap is open source, and every recording goes{" "}
+ <Em>exactly where you say</Em>. Use Cap Cloud, point it at your own bucket
+ or Drive, or self host the whole thing.
+ </SectionHead>
+ <Stage
+ name="ownership"
+ canvas={{ w: 1040, h: 440 }}
+ tone={TONES.storage}
+ className="mt-14"
+ />
+ <StageFeatures items={STORAGE_COPY} />
+ <Details
+ className="mt-16 lg:grid-cols-4"
+ items={[
+ {
+ lead: "Open source.",
+ icon: <Github />,
+ body: "Every line is on GitHub. Audit it, fork it, or contribute the feature you need.",
+ },
+ {
+ lead: "SOC 2 Type II and ISO 27001.",
+ icon: <ShieldCheck />,
+ body: "Independently audited controls behind Cap Cloud.",
+ },
+ {
+ lead: "HIPAA compliant.",
+ icon: <HeartPulse />,
+ body: "Signed BAAs for organizations handling PHI.",
+ },
+ {
+ lead: "No vendor lock in.",
+ icon: <LockOpen />,
+ body: "Recordings are plain video files. Take them anywhere, any time.",
+ },
+ ]}
+ />
+ </Section>
+);
+
+export const AgentsSection = () => (
+ <Section anchor="agents" tone={TONES.agents}>
+ <SectionHead
+ title="Your agent can record, share, and read Caps"
+ badge={<Badge icon={<SquareTerminal />}>Cap for Agents</Badge>}
+ >
+ Cap ships a CLI and a local MCP server, so{" "}
+ <Em>Claude Code, Cursor, Codex, or any shell capable agent</Em> can record
+ your screen, upload it, and read back the transcript.
+ </SectionHead>
+ <Stage
+ name="agents"
+ canvas={{ w: 1040, h: 450 }}
+ tone={TONES.agents}
+ caption={false}
+ className="mt-14"
+ />
+ <Details
+ className="mt-16 lg:grid-cols-4"
+ items={[
+ {
+ lead: "One prompt setup.",
+ icon: <Wand />,
+ body: "Your agent installs the CLI, the Cap skill, and the MCP server for Claude Code, Cursor, or Codex.",
+ },
+ {
+ lead: "JSON output on every command.",
+ icon: <Braces />,
+ body: "Add one flag and the answer comes back as data, with live events while recording.",
+ },
+ {
+ lead: "MCP server with 76 tools.",
+ icon: <Plug />,
+ body: "Read and manage Caps, comments, and sharing. Capture and upload stay in the CLI, so no secret passes through the model.",
+ },
+ {
+ lead: "Permission prompts.",
+ icon: <Hand />,
+ body: "The agent checks with you before anything records, uploads, or costs money.",
+ },
+ ]}
+ />
+ <div className="mt-10 flex justify-center">
+ <Link href="/agents" className={classNames(BTN_SECONDARY, "gap-2")}>
+ Explore Cap for Agents
+ <ArrowUpRight className="size-4" />
+ </Link>
+ </div>
+ </Section>
+);
diff --git a/apps/web/components/pages/HomeTwo/showcase/share.tsx b/apps/web/components/pages/HomeTwo/showcase/share.tsx
new file mode 100644
index 000000000..5d6bd44b3
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/share.tsx
@@ -0,0 +1,310 @@
+"use client";
+
+import { classNames } from "@cap/utils/helpers";
+import type { ReactNode } from "react";
+import { INVOICE, InvoicePage, PRESENTER, Webcam } from "./subjects";
+
+export const SHARE_W = 1200;
+export const SHARE_URL = "Cap.so/s/m4k8x2qz";
+export const CAP_TITLE = "Sending an invoice in Ledgerly";
+export const DURATION_S = 24;
+
+export const Avatar = ({
+ size = 24,
+ letter,
+ color,
+}: {
+ size?: number;
+ letter?: string;
+ color?: string;
+}) =>
+ letter ? (
+ <span
+ className="grid shrink-0 place-items-center rounded-full font-medium text-white"
+ style={{
+ width: size,
+ height: size,
+ background: color,
+ fontSize: size * 0.45,
+ }}
+ >
+ {letter}
+ </span>
+ ) : (
+ <span
+ className="block shrink-0 rounded-full bg-cover bg-center"
+ style={{
+ width: size,
+ height: size,
+ backgroundImage: `url(${PRESENTER.avatar})`,
+ }}
+ />
+ );
+
+export const stamp = (seconds: number) =>
+ `00:00:${String(Math.floor(seconds)).padStart(2, "0")}`;
+
+const clock = (seconds: number) =>
+ `${Math.floor(seconds / 60)}:${String(Math.floor(seconds % 60)).padStart(2, "0")}`;
+
+export const RecordingFrame = ({
+ playing,
+ pageScale,
+}: {
+ playing: boolean;
+ pageScale: number;
+}) => (
+ <div className="relative h-full w-full overflow-hidden bg-[#DCE6F1]">
+ <div
+ className="absolute left-1/2 top-1/2 overflow-hidden rounded-[8px] shadow-[0_10px_30px_-12px_rgba(0,0,0,0.35)]"
+ style={{
+ width: INVOICE.w,
+ height: INVOICE.h,
+ transform: `translate(-50%, -50%) scale(${pageScale})`,
+ }}
+ >
+ <InvoicePage />
+ </div>
+ <Webcam
+ playing={playing}
+ className="absolute bottom-[7%] right-[5%] aspect-square w-[14%] rounded-full shadow-[0_10px_24px_-10px_rgba(0,0,0,0.5)]"
+ />
+ </div>
+);
+
+const Icon = ({ d, className }: { d: string; className?: string }) => (
+ <svg
+ aria-hidden="true"
+ viewBox="0 0 24 24"
+ className={classNames("size-[18px]", className)}
+ fill="none"
+ stroke="currentColor"
+ strokeWidth={1.8}
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ >
+ <path d={d} />
+ </svg>
+);
+
+const PlayerControls = ({ seconds }: { seconds: number }) => {
+ const progress = seconds / DURATION_S;
+ return (
+ <div className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-[rgba(0,0,0,0.55)] to-transparent px-4 pb-3 pt-10 text-white">
+ <div className="relative h-[3px] rounded-full bg-white/30">
+ <div
+ className="absolute inset-y-0 left-0 rounded-full bg-white"
+ style={{ width: `${progress * 100}%` }}
+ />
+ <div
+ className="absolute top-1/2 size-3 -translate-x-1/2 -translate-y-1/2 rounded-full bg-white"
+ style={{ left: `${progress * 100}%` }}
+ />
+ </div>
+ <div className="mt-3 flex items-center gap-4 text-[13px]">
+ <svg aria-hidden="true" viewBox="0 0 24 24" className="size-[18px]">
+ <rect x="6" y="5" width="4" height="14" rx="1" fill="currentColor" />
+ <rect x="14" y="5" width="4" height="14" rx="1" fill="currentColor" />
+ </svg>
+ <Icon d="M11 7l-5 5 5 5M18 7l-5 5 5 5" />
+ <Icon d="M6 7l5 5-5 5M13 7l5 5-5 5" />
+ <Icon d="M4 10v4h3l4 4V6L7 10H4zM15 9.5a3.5 3.5 0 0 1 0 5" />
+ <span className="tabular-nums">
+ {clock(seconds)} / {clock(DURATION_S)}
+ </span>
+ <span className="flex-1" />
+ <Icon d="M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 0 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 0 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 0 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 0 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z" />
+ <Icon d="M4 8V4h4M20 8V4h-4M4 16v4h4M20 16v4h-4" />
+ </div>
+ </div>
+ );
+};
+
+const EMOJIS = ["😂", "😍", "😮", "🙌", "👍", "👎"];
+
+export type ShareComment = {
+ at: number;
+ who: string;
+ letter?: string;
+ color?: string;
+ when: string;
+ seconds?: number;
+ text: string;
+};
+
+export const CommentBubble = ({ comment }: { comment: ShareComment }) => (
+ <div className="ht-pop-in flex items-start gap-2.5">
+ <Avatar size={24} letter={comment.letter} color={comment.color} />
+ <div className="flex-1 rounded-xl border border-[#EEEEEE] bg-[#F9F9F9] p-3">
+ <div className="flex items-center justify-between gap-3">
+ <span className="truncate text-[14px] font-medium text-[#1C1C1C]">
+ {comment.who}
+ </span>
+ <span className="flex items-center gap-2 whitespace-nowrap text-[12px]">
+ <span className="text-[#8F8F8F]">{comment.when}</span>
+ {comment.seconds !== undefined ? (
+ <span className="text-[#3B82F6]">{stamp(comment.seconds)}</span>
+ ) : null}
+ </span>
+ </div>
+ <p className="mt-2 text-[14px] text-[#5D5D5D]">{comment.text}</p>
+ </div>
+ </div>
+);
+
+export type SidebarTab = "comments" | "summary" | "transcript";
+
+export const ShareSidebar = ({
+ tab,
+ children,
+ footer = true,
+ className,
+}: {
+ tab: SidebarTab;
+ children: ReactNode;
+ footer?: boolean;
+ className?: string;
+}) => (
+ <div className={classNames("flex flex-col bg-white", className)}>
+ <div className="flex border-b border-[#E8E8E8]">
+ {(
+ [
+ ["comments", "Comments"],
+ ["summary", "Summary"],
+ ["transcript", "Transcript"],
+ ] as const
+ ).map(([key, label]) => (
+ <div
+ key={key}
+ className={classNames(
+ "relative flex-1 px-5 py-3 text-center text-[14px] font-medium transition-colors duration-300",
+ key === tab ? "bg-[#F0F0F0] text-[#1C1C1C]" : "text-[#8F8F8F]",
+ )}
+ >
+ {label}
+ {key === tab ? (
+ <span className="absolute inset-x-0 bottom-0 h-0.5 bg-[#3B82F6]" />
+ ) : null}
+ </div>
+ ))}
+ </div>
+ <div className="min-h-0 flex-1 overflow-hidden">{children}</div>
+ {footer ? (
+ <div className="m-3 flex items-center gap-2.5 rounded-xl border border-[#E8E8E8] px-3 py-2.5 text-[14px]">
+ <span className="grid size-7 place-items-center rounded-full bg-[#EEEEEE] text-[#8F8F8F]">
+ <svg aria-hidden="true" viewBox="0 0 16 16" className="size-3.5">
+ <circle cx="8" cy="5.5" r="3" fill="currentColor" />
+ <path d="M2.5 14a5.5 5.5 0 0 1 11 0z" fill="currentColor" />
+ </svg>
+ </span>
+ <span className="flex-1 text-[#A3A3A3]">Respond to Richie...</span>
+ </div>
+ ) : null}
+ </div>
+);
+
+export const SharePage = ({
+ height,
+ seconds,
+ playing,
+ views,
+ sidebar,
+ floating = [],
+ pressed,
+}: {
+ height: number;
+ seconds: number;
+ playing: boolean;
+ views: number;
+ sidebar: ReactNode;
+ floating?: { emoji: string; k: number; x: number }[];
+ pressed?: string;
+}) => (
+ <div
+ className="relative flex flex-col overflow-hidden bg-white text-[#1C1C1C]"
+ style={{ width: SHARE_W, height, fontWeight: 400 }}
+ >
+ <div className="border-b border-[#E8E8E8] px-6 pb-4 pt-5">
+ <div className="text-[24px] tracking-[-0.01em]">{CAP_TITLE}</div>
+ <div className="mt-3 flex items-center gap-5">
+ <div className="flex items-center gap-2.5">
+ <Avatar size={32} />
+ <div className="leading-tight">
+ <div className="text-[14px]">Richie</div>
+ <div className="text-[12.5px] text-[#8F8F8F]">
+ just now · {views} views
+ </div>
+ </div>
+ </div>
+ <span className="flex h-9 items-center gap-1.5 rounded-full bg-[#3B82F6] px-4 text-[14px] font-medium text-white shadow-[inset_0_1px_0_rgba(255,255,255,0.25)]">
+ <svg aria-hidden="true" viewBox="0 0 16 16" className="size-3.5">
+ <path
+ d="M9.5 2.5 14 7l-4.5 4.5V9C6 9 3.8 10 2 13c.5-4 2.8-6.6 7.5-7z"
+ fill="currentColor"
+ />
+ </svg>
+ Share
+ </span>
+ </div>
+ </div>
+ <div className="flex min-h-0 flex-1">
+ <div className="relative flex-1 bg-[#F7F7F7] px-6 pt-5">
+ <div className="rounded-[18px] border border-[#E8E8E8] bg-white p-2">
+ <div className="relative aspect-video overflow-hidden rounded-[12px] bg-black">
+ <RecordingFrame playing={playing} pageScale={0.95} />
+ <PlayerControls seconds={seconds} />
+ </div>
+ </div>
+ <div className="mt-4 flex items-center gap-6">
+ <div className="flex rounded-full border border-[#E8E8E8] bg-[#F0F0F0] p-1 text-[14px]">
+ <span className="rounded-full bg-white px-3.5 py-1.5 shadow-[0_1px_2px_rgba(0,0,0,0.08)]">
+ Player
+ </span>
+ <span className="px-3.5 py-1.5 text-[#646464]">Timeline</span>
+ </div>
+ <div className="relative mx-auto flex items-center gap-1 rounded-full border border-[#E8E8E8] bg-white p-1.5">
+ {EMOJIS.map((emoji) => (
+ <span
+ key={emoji}
+ className="grid size-10 place-items-center rounded-full text-[24px] transition-[background-color,transform] duration-200"
+ style={{
+ background: pressed === emoji ? "#DBEAFE" : "transparent",
+ transform: pressed === emoji ? "scale(1.15)" : "none",
+ }}
+ >
+ {emoji}
+ </span>
+ ))}
+ <span className="mx-2 h-6 w-px bg-[#E8E8E8]" />
+ <span className="flex h-10 items-center gap-2 rounded-full bg-[#1C1C1C] pl-5 pr-2 text-[15px] text-white">
+ Comment
+ <span className="grid size-6 place-items-center rounded-full bg-white/20 text-[12px]">
+ C
+ </span>
+ </span>
+ {floating.map((item) => (
+ <span
+ key={`${item.emoji}-${item.x}`}
+ className="pointer-events-none absolute text-[28px]"
+ style={{
+ left: item.x,
+ bottom: 40 + item.k * 160,
+ opacity: item.k < 0.8 ? 1 : (1 - item.k) * 5,
+ transform: `scale(${0.8 + item.k * 0.5}) rotate(${(item.k - 0.5) * 20}deg)`,
+ }}
+ >
+ {item.emoji}
+ </span>
+ ))}
+ </div>
+ </div>
+ </div>
+ <ShareSidebar
+ tab="comments"
+ className="w-[360px] shrink-0 border-l border-[#E8E8E8]"
+ >
+ {sidebar}
+ </ShareSidebar>
+ </div>
+ </div>
+);
diff --git a/apps/web/components/pages/HomeTwo/showcase/split.tsx b/apps/web/components/pages/HomeTwo/showcase/split.tsx
new file mode 100644
index 000000000..8cfd5911d
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/split.tsx
@@ -0,0 +1,77 @@
+import { classNames } from "@cap/utils/helpers";
+import type { ReactNode } from "react";
+import { BODY_TEXT, H_SECTION } from "../theme";
+import { type Detail, DetailIcon, type Tone, toneVars } from "./layout";
+
+export const SplitSection = ({
+ anchor,
+ title,
+ lede,
+ details,
+ visual,
+ flip,
+ footer,
+ tone,
+ badge,
+}: {
+ tone?: Tone;
+ badge?: ReactNode;
+ anchor?: string;
+ title: ReactNode;
+ lede: ReactNode;
+ details: Detail[];
+ visual: ReactNode;
+ flip?: boolean;
+ footer?: ReactNode;
+}) => (
+ <section
+ id={anchor}
+ className="scroll-mt-24 px-5 py-20 lg:py-32"
+ style={toneVars(tone)}
+ >
+ <div
+ className={classNames(
+ "mx-auto grid max-w-[1120px] items-center gap-12 lg:gap-16",
+ flip
+ ? "lg:grid-cols-[minmax(0,1.15fr)_minmax(0,0.85fr)]"
+ : "lg:grid-cols-[minmax(0,0.85fr)_minmax(0,1.15fr)]",
+ )}
+ >
+ <div className={classNames("min-w-0", flip && "lg:order-2")}>
+ {badge ? <div className="mb-6">{badge}</div> : null}
+ <h2
+ className={`${H_SECTION} text-balance text-[clamp(34px,4vw,48px)] leading-[1.02]`}
+ >
+ {title}
+ </h2>
+ <p
+ className={`${BODY_TEXT} mt-5 max-w-[460px] text-[17px] leading-[1.5] text-[rgba(17,17,17,0.64)] sm:text-[18px]`}
+ >
+ {lede}
+ </p>
+ <ul className="mt-9 max-w-[460px] border-t border-[#E3E8EE]">
+ {details.map((item) => (
+ <li
+ key={item.lead}
+ className="flex gap-3.5 border-b border-[#E3E8EE] py-4 text-[15px] leading-[1.55] text-[rgba(17,17,17,0.64)]"
+ >
+ {item.icon ? (
+ <DetailIcon className="-mt-0.5">{item.icon}</DetailIcon>
+ ) : null}
+ <div>
+ <h3 className="m-0 inline text-[length:inherit] font-medium leading-[inherit] tracking-normal text-[#111111]">
+ {item.lead}
+ </h3>{" "}
+ {item.body}
+ </div>
+ </li>
+ ))}
+ </ul>
+ {footer}
+ </div>
+ <div className={classNames("min-w-0", flip && "lg:order-1")}>
+ {visual}
+ </div>
+ </div>
+ </section>
+);
diff --git a/apps/web/components/pages/HomeTwo/showcase/stage.tsx b/apps/web/components/pages/HomeTwo/showcase/stage.tsx
new file mode 100644
index 000000000..39ed98430
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/stage.tsx
@@ -0,0 +1,110 @@
+"use client";
+
+import { classNames } from "@cap/utils/helpers";
+import { useDetectPlatform } from "hooks/useDetectPlatform";
+import {
+ type ComponentType,
+ lazy,
+ Suspense,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
+import { DemoPlatformProvider } from "../demo/platform";
+import { Panel, type Tone } from "./layout";
+
+type StageName =
+ | "instant"
+ | "studio"
+ | "screenshot"
+ | "ai"
+ | "ownership"
+ | "agents";
+
+const STAGES: Record<StageName, ComponentType> = {
+ instant: lazy(() =>
+ import("./instant").then((m) => ({ default: m.InstantStage })),
+ ),
+ studio: lazy(() =>
+ import("./studio").then((m) => ({ default: m.StudioStage })),
+ ),
+ screenshot: lazy(() =>
+ import("./screenshot").then((m) => ({ default: m.ScreenshotStage })),
+ ),
+ ai: lazy(() => import("./ai").then((m) => ({ default: m.AiStage }))),
+ ownership: lazy(() =>
+ import("./ownership").then((m) => ({ default: m.OwnershipStage })),
+ ),
+ agents: lazy(() =>
+ import("./agents").then((m) => ({ default: m.AgentsStage })),
+ ),
+};
+
+const Placeholder = ({
+ canvas,
+ tone,
+ caption,
+}: {
+ canvas: { w: number; h: number };
+ tone: Tone;
+ caption: boolean;
+}) => (
+ <div aria-hidden="true">
+ <Panel tone={tone}>
+ <div style={{ aspectRatio: `${canvas.w} / ${canvas.h}` }} />
+ </Panel>
+ {caption ? <div className="h-[76px]" /> : null}
+ </div>
+);
+
+export const Stage = ({
+ name,
+ canvas,
+ tone,
+ caption = true,
+ className,
+}: {
+ name: StageName;
+ canvas: { w: number; h: number };
+ tone: Tone;
+ caption?: boolean;
+ className?: string;
+}) => {
+ const ref = useRef<HTMLDivElement | null>(null);
+ const [near, setNear] = useState(false);
+ // Windows visitors see Windows windows, cursors and notifications;
+ // everyone else gets macOS, the same rule as the hero demo.
+ const { platform } = useDetectPlatform();
+ const look = platform === "windows" ? "windows" : "macos";
+
+ useEffect(() => {
+ const el = ref.current;
+ if (!el || near) return;
+ const io = new IntersectionObserver(
+ ([entry]) => {
+ if (entry?.isIntersecting) setNear(true);
+ },
+ { rootMargin: "1200px 0px" },
+ );
+ io.observe(el);
+ return () => io.disconnect();
+ }, [near]);
+
+ const Loaded = STAGES[name];
+ const placeholder = (
+ <Placeholder canvas={canvas} tone={tone} caption={caption} />
+ );
+ return (
+ <div ref={ref} className={classNames("min-w-0", className)}>
+ {near ? (
+ <DemoPlatformProvider value={look}>
+ <Suspense fallback={placeholder}>
+ <Loaded />
+ </Suspense>
+ </DemoPlatformProvider>
+ ) : (
+ placeholder
+ )}
+ </div>
+ );
+};
diff --git a/apps/web/components/pages/HomeTwo/showcase/studio.tsx b/apps/web/components/pages/HomeTwo/showcase/studio.tsx
new file mode 100644
index 000000000..d3de0d2ab
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/studio.tsx
@@ -0,0 +1,724 @@
+"use client";
+
+import {
+ Captions,
+ EyeOff,
+ Image as ImageIcon,
+ Video,
+ ZoomIn,
+} from "lucide-react";
+import { type CSSProperties, type ReactNode, useRef } from "react";
+import { copyOf, STUDIO_COPY } from "./copy";
+import { CANVAS, lerp, type Scene, Showcase, seg, TONES } from "./kit";
+import {
+ AUTO_ZOOM,
+ CLICK,
+ CURSOR_SPRING,
+ clickShrink,
+ rawPath,
+ sampleAt,
+ simulate,
+ snapToEdges,
+ ZOOM_SPRING,
+} from "./physics";
+import {
+ Cursor,
+ FrameChip,
+ INVOICE,
+ INVOICE_SPOTS,
+ InvoicePage,
+ thumbOf,
+ WALLPAPERS,
+ Wallpaper,
+ Webcam,
+} from "./subjects";
+
+const FRAME = { w: 744, h: 465 } as const;
+const FRAME_LEFT = (CANVAS.w - FRAME.w) / 2;
+const FRAME_TOP = 36;
+
+const Frame = ({
+ children,
+ style,
+}: {
+ children: ReactNode;
+ style?: CSSProperties;
+}) => (
+ <div
+ className="absolute overflow-hidden rounded-[16px] shadow-[0_0_0_1px_rgba(17,17,17,0.08),0_24px_60px_-28px_rgba(17,17,17,0.35)]"
+ style={{
+ left: FRAME_LEFT,
+ top: FRAME_TOP,
+ width: FRAME.w,
+ height: FRAME.h,
+ ...style,
+ }}
+ >
+ {children}
+ </div>
+);
+
+const fitRecording = (pad: number) => {
+ const scale = Math.min(
+ (FRAME.w - pad * 2) / INVOICE.w,
+ (FRAME.h - pad * 2) / INVOICE.h,
+ );
+ const w = INVOICE.w * scale;
+ const h = INVOICE.h * scale;
+ return {
+ scale,
+ left: (FRAME.w - w) / 2,
+ top: (FRAME.h - h) / 2,
+ w,
+ h,
+ };
+};
+
+const Recording = ({
+ pad,
+ radius = 10,
+ shadow = 1,
+ children,
+ page,
+}: {
+ pad: number;
+ radius?: number;
+ shadow?: number;
+ children?: ReactNode;
+ page?: ReactNode;
+}) => {
+ const box = fitRecording(pad);
+ return (
+ <div
+ className="absolute overflow-hidden"
+ style={{
+ left: box.left,
+ top: box.top,
+ width: box.w,
+ height: box.h,
+ borderRadius: radius,
+ boxShadow: `0 ${18 * shadow}px ${44 * shadow}px -16px rgba(0,0,0,${0.45 * shadow})`,
+ }}
+ >
+ <div
+ style={{
+ width: INVOICE.w,
+ height: INVOICE.h,
+ transform: `scale(${box.scale})`,
+ transformOrigin: "top left",
+ }}
+ >
+ {page ?? <InvoicePage />}
+ </div>
+ {children}
+ </div>
+ );
+};
+
+const toFrame = (pad: number, x: number, y: number) => {
+ const box = fitRecording(pad);
+ return { x: box.left + x * box.scale, y: box.top + y * box.scale };
+};
+
+// Zoom, played the way an exported Cap plays it: the raw mouse path is
+// smoothed by the cursor spring, a click opens an auto zoom segment 300 ms
+// before it and closes it 2.5 s after, and the zoom amount and centre chase
+// their targets on the editor's screen movement spring. Only the recording
+// grows; the background stays put.
+const ZOOM_PAD = 44;
+const CLICK_AT = 1350;
+const ZOOM_MS = 6800;
+const ZOOM_SEGMENT = {
+ start: CLICK_AT - AUTO_ZOOM.preMs,
+ end: CLICK_AT + AUTO_ZOOM.postMs,
+};
+const SEND_CENTER = {
+ x: INVOICE_SPOTS.send.x + INVOICE_SPOTS.send.w / 2 + 8,
+ y: INVOICE_SPOTS.send.y + INVOICE_SPOTS.send.h / 2 + 2,
+};
+const mouse = rawPath([
+ { at: 0, x: 318, y: 214, hold: 250 },
+ { at: 1150, x: SEND_CENTER.x, y: SEND_CENTER.y, hold: 900 },
+ { at: 2900, x: 606, y: 292, hold: 1700 },
+ { at: 5700, x: 318, y: 214 },
+]);
+const CURSOR_SAMPLES = simulate(mouse, ZOOM_MS, CURSOR_SPRING);
+
+const ZOOM_SAMPLES = (() => {
+ let held = [0.5, 0.5];
+ return simulate(
+ (ms) => {
+ const inside = ms >= ZOOM_SEGMENT.start && ms <= ZOOM_SEGMENT.end;
+ if (inside) {
+ const [mx = 0, my = 0] = sampleAt(CURSOR_SAMPLES, ms);
+ held = [snapToEdges(mx / INVOICE.w), snapToEdges(my / INVOICE.h)];
+ return [AUTO_ZOOM.amount, held[0] ?? 0.5, held[1] ?? 0.5];
+ }
+ return [1, held[0] ?? 0.5, held[1] ?? 0.5];
+ },
+ ZOOM_MS,
+ ZOOM_SPRING,
+ );
+})();
+
+const ZoomView = ({ t }: { t: number; playing: boolean }) => {
+ const box = fitRecording(ZOOM_PAD);
+ const [amount = 1, cx = 0.5, cy = 0.5] = sampleAt(ZOOM_SAMPLES, t);
+ const [mx = 0, my = 0] = sampleAt(CURSOR_SAMPLES, t);
+ const a = Math.max(1, amount);
+ // SegmentBounds::from_amount_center: scale the display about its origin,
+ // then slide it so the centre maps across the travel range.
+ const w = box.w * a;
+ const h = box.h * a;
+ const left = box.left + cx * (1 - a) * box.w;
+ const top = box.top + cy * (1 - a) * box.h;
+ const sent = t > CLICK_AT + CLICK.ms ? 1 : 0;
+ const pressed = t > CLICK_AT && t < CLICK_AT + CLICK.ms ? 1 : 0;
+ return (
+ <Frame>
+ <Wallpaper src={WALLPAPERS.rome} />
+ <div
+ className="absolute overflow-hidden"
+ style={{
+ left,
+ top,
+ width: w,
+ height: h,
+ borderRadius: 10,
+ boxShadow: "0 18px 44px -16px rgba(0,0,0,0.45)",
+ }}
+ >
+ <div
+ style={{
+ width: INVOICE.w,
+ height: INVOICE.h,
+ transform: `scale(${box.scale * a})`,
+ transformOrigin: "top left",
+ }}
+ >
+ <InvoicePage sent={sent} press={pressed} />
+ </div>
+ </div>
+ <Cursor
+ x={left + (mx / INVOICE.w) * w}
+ y={top + (my / INVOICE.h) * h}
+ size={22 * a * clickShrink(t, CLICK_AT)}
+ />
+ <FrameChip
+ className="left-4 top-4 tabular-nums transition-opacity duration-300"
+ style={{ opacity: a > 1.03 ? 1 : 0 }}
+ >
+ Auto zoom {a.toFixed(1)}x
+ </FrameChip>
+ </Frame>
+ );
+};
+
+type Rect = { x: number; y: number; w: number; h: number; r: number };
+
+type Layout = {
+ name: string;
+ screen: Rect;
+ screenOpacity: number;
+ camera: Rect;
+ cameraOpacity: number;
+};
+
+const PAD = 40;
+const SCREEN_FULL: Rect = {
+ x: PAD,
+ y: PAD,
+ w: FRAME.w - PAD * 2,
+ h: FRAME.h - PAD * 2,
+ r: 10,
+};
+
+const LAYOUTS: Layout[] = [
+ {
+ name: "Screen and camera",
+ screen: SCREEN_FULL,
+ screenOpacity: 1,
+ camera: { x: FRAME.w - 196, y: FRAME.h - 196, w: 164, h: 164, r: 82 },
+ cameraOpacity: 1,
+ },
+ {
+ name: "Split screen",
+ screen: { x: 28, y: 92, w: 432, h: 281, r: 10 },
+ screenOpacity: 1,
+ camera: { x: 476, y: 92, w: 240, h: 281, r: 14 },
+ cameraOpacity: 1,
+ },
+ {
+ name: "Camera only",
+ screen: { x: 28, y: 92, w: 432, h: 281, r: 10 },
+ screenOpacity: 0,
+ camera: { x: 0, y: 0, w: FRAME.w, h: FRAME.h, r: 0 },
+ cameraOpacity: 1,
+ },
+ {
+ name: "Screen only",
+ screen: SCREEN_FULL,
+ screenOpacity: 1,
+ camera: { x: FRAME.w - 120, y: FRAME.h - 120, w: 80, h: 80, r: 40 },
+ cameraOpacity: 0,
+ },
+];
+
+const STEP = 2300;
+const MORPH = 650;
+
+const layoutAt = (t: number) =>
+ Math.min(LAYOUTS.length - 1, Math.floor(t / STEP));
+
+const MORPH_EASE = `${MORPH}ms cubic-bezier(0.65,0,0.35,1)`;
+const morph = (props: string[]) =>
+ props.map((prop) => `${prop} ${MORPH_EASE}`).join(", ");
+const BOX = ["left", "top", "width", "height", "border-radius", "opacity"];
+
+const ScenesView = ({
+ t,
+ playing,
+ pick,
+}: {
+ t: number;
+ playing: boolean;
+ pick: number | null;
+}) => {
+ const layout = (LAYOUTS[pick ?? layoutAt(t)] ?? LAYOUTS[0]) as Layout;
+ const { screen, camera } = layout;
+ return (
+ <Frame>
+ <Wallpaper src={WALLPAPERS.santorini} />
+ <div
+ className="absolute overflow-hidden shadow-[0_18px_44px_-16px_rgba(0,0,0,0.45)]"
+ style={{
+ left: screen.x,
+ top: screen.y,
+ width: screen.w,
+ height: screen.h,
+ borderRadius: screen.r,
+ opacity: layout.screenOpacity,
+ transition: morph(BOX),
+ }}
+ >
+ <div
+ style={{
+ width: INVOICE.w,
+ height: INVOICE.h,
+ transform: `scale(${screen.w / INVOICE.w})`,
+ transformOrigin: "top left",
+ transition: morph(["transform"]),
+ }}
+ >
+ <InvoicePage />
+ </div>
+ </div>
+ <Webcam
+ playing={playing}
+ className="absolute shadow-[0_18px_44px_-16px_rgba(0,0,0,0.5)]"
+ style={{
+ left: camera.x,
+ top: camera.y,
+ width: camera.w,
+ height: camera.h,
+ borderRadius: camera.r,
+ opacity: layout.cameraOpacity,
+ transition: morph(BOX),
+ }}
+ />
+ <FrameChip key={layout.name} className="ht-fade-in left-4 top-4">
+ {layout.name}
+ </FrameChip>
+ </Frame>
+ );
+};
+
+const LayoutGlyph = ({ kind }: { kind: number }) => (
+ <svg aria-hidden="true" viewBox="0 0 24 18" className="h-[15px] w-5">
+ <rect
+ x="1"
+ y="1"
+ width="22"
+ height="16"
+ rx="3"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="1.5"
+ />
+ {kind === 0 ? (
+ <>
+ <rect
+ x="4"
+ y="4"
+ width="11"
+ height="8"
+ rx="1"
+ fill="currentColor"
+ opacity="0.35"
+ />
+ <circle cx="17.5" cy="12.5" r="2.6" fill="currentColor" />
+ </>
+ ) : null}
+ {kind === 1 ? (
+ <>
+ <rect
+ x="3.5"
+ y="5"
+ width="9"
+ height="8"
+ rx="1"
+ fill="currentColor"
+ opacity="0.35"
+ />
+ <rect
+ x="14"
+ y="5"
+ width="6.5"
+ height="8"
+ rx="1.2"
+ fill="currentColor"
+ />
+ </>
+ ) : null}
+ {kind === 2 ? <circle cx="12" cy="8" r="4" fill="currentColor" /> : null}
+ {kind === 3 ? (
+ <rect
+ x="4"
+ y="4"
+ width="16"
+ height="10"
+ rx="1"
+ fill="currentColor"
+ opacity="0.35"
+ />
+ ) : null}
+ </svg>
+);
+
+const LINES = [
+ "So this is the new invoice flow.",
+ "Pick the client,",
+ "add your line items,",
+ "and hit send.",
+];
+const WORD_MS = 300;
+const LINE_GAP = 350;
+
+const timeline = (() => {
+ let at = 400;
+ return LINES.map((line) => {
+ const words = line.split(" ").map((word) => {
+ const start = at;
+ at += WORD_MS;
+ return { word, start };
+ });
+ at += LINE_GAP;
+ return words;
+ });
+})();
+const CAPTIONS_END = (timeline.at(-1)?.at(-1)?.start ?? 0) + 1600;
+
+const CaptionsView = ({ t, playing }: { t: number; playing: boolean }) => {
+ const lineIndex = Math.max(
+ 0,
+ timeline.findLastIndex((line) => (line[0]?.start ?? 0) <= t),
+ );
+ const line = (timeline[lineIndex] ?? []).filter(({ start }) => t >= start);
+ return (
+ <Frame>
+ <Wallpaper src={WALLPAPERS.miami} />
+ <Recording pad={40} />
+ <Webcam
+ playing={playing}
+ className="absolute shadow-[0_18px_44px_-16px_rgba(0,0,0,0.5)]"
+ style={{
+ right: 56,
+ top: 56,
+ width: 112,
+ height: 112,
+ borderRadius: 56,
+ }}
+ />
+ <div className="absolute inset-x-0 bottom-[22px] flex justify-center">
+ <div
+ key={lineIndex}
+ className="ht-fade-in flex rounded-[8px] bg-[rgba(0,0,0,0.9)] px-3 py-1.5 text-[19px] font-bold leading-[1.25] text-white"
+ >
+ {line.map(({ word, start }) => {
+ const k = Math.min(1, Math.max(0, (t - start) / 250));
+ const bounce = k < 1 ? 1 + Math.sin(k * Math.PI) * 0.12 : 1;
+ return (
+ <span
+ key={`${word}-${start}`}
+ className="inline-block px-[2.5px]"
+ style={{
+ opacity: k,
+ transform: `translateY(${(1 - k) * 4}px) scale(${bounce})`,
+ }}
+ >
+ {word}
+ </span>
+ );
+ })}
+ </div>
+ </div>
+ </Frame>
+ );
+};
+
+type MaskMode = "Blur" | "Pixelate" | "Highlight";
+const MASK_MODES: MaskMode[] = ["Blur", "Pixelate", "Highlight"];
+
+const Mosaic = ({ w, h }: { w: number; h: number }) => {
+ const size = 8;
+ const cols = Math.ceil(w / size);
+ const rows = Math.ceil(h / size);
+ const tones = ["#C9CDD4", "#E3E6EA", "#AEB3BC", "#D6D9DE", "#9CA2AC"];
+ return (
+ <div className="absolute inset-0 flex flex-wrap">
+ {Array.from({ length: rows * cols }, (_, n) => ({
+ id: `cell-${n}`,
+ tone: tones[(((n * 2654435761) >>> 0) % 97) % tones.length],
+ })).map((cell) => (
+ <span
+ key={cell.id}
+ style={{
+ width: size,
+ height: size,
+ background: cell.tone,
+ }}
+ />
+ ))}
+ </div>
+ );
+};
+
+const BlurView = ({ t }: { t: number; playing: boolean }) => {
+ const pad = 28;
+ const box = fitRecording(pad);
+ const email = INVOICE_SPOTS.email;
+ const draw = seg(t, 500, 1400);
+ const modeIndex = t < 3200 ? 0 : t < 5200 ? 1 : 2;
+ const mode = MASK_MODES[modeIndex] ?? "Blur";
+ const blurAmount = seg(t, 1400, 1900) * 7;
+ const x = box.left + (email.x - 6) * box.scale;
+ const y = box.top + (email.y - 6) * box.scale;
+ const fullW = (email.w + 12) * box.scale;
+ const h = (email.h + 12) * box.scale;
+ const w = fullW * draw;
+ const pickerX = x + fullW / 2;
+ const cursorStart = toFrame(pad, email.x - 4, email.y - 4);
+ const cursorEnd = toFrame(pad, email.x + email.w + 6, email.y + email.h + 6);
+ const cursorK = seg(t, 400, 1400);
+ const cursorAway = seg(t, 1600, 2400);
+ const privacy: CSSProperties =
+ mode === "Blur" ? { filter: `blur(${blurAmount}px)` } : {};
+ return (
+ <Frame>
+ <Wallpaper src={WALLPAPERS.london} />
+ <Recording pad={pad} page={<InvoicePage emailStyle={privacy} />} />
+ {mode === "Pixelate" ? (
+ <div
+ className="absolute overflow-hidden rounded-[4px]"
+ style={{ left: x, top: y, width: fullW, height: h }}
+ >
+ <Mosaic w={fullW} h={h} />
+ </div>
+ ) : null}
+ {mode === "Highlight" ? (
+ <div
+ className="absolute rounded-[6px]"
+ style={{
+ left: x,
+ top: y,
+ width: fullW,
+ height: h,
+ boxShadow: "0 0 0 2000px rgba(10,12,16,0.55)",
+ }}
+ />
+ ) : null}
+ {draw > 0 ? (
+ <div
+ className="absolute rounded-[6px] border-[1.5px] border-[#FF4766]"
+ style={{ left: x, top: y, width: w, height: h }}
+ />
+ ) : null}
+ <div
+ className="absolute flex -translate-x-1/2 gap-0.5 rounded-full bg-white p-1 text-[13px] shadow-[0_8px_24px_-8px_rgba(0,0,0,0.35),0_0_0_1px_rgba(0,0,0,0.06)] transition-opacity duration-300"
+ style={{
+ left: pickerX,
+ top: y + h + 12,
+ opacity: draw >= 1 ? 1 : 0,
+ }}
+ >
+ {MASK_MODES.map((item) => (
+ <span
+ key={item}
+ className="rounded-full px-3 py-1 transition-colors duration-200"
+ style={{
+ background: item === mode ? "#111111" : "transparent",
+ color: item === mode ? "#FFFFFF" : "#555A63",
+ }}
+ >
+ {item}
+ </span>
+ ))}
+ </div>
+ <Cursor
+ x={lerp(
+ lerp(cursorStart.x, cursorEnd.x, cursorK),
+ cursorEnd.x + 60,
+ cursorAway,
+ )}
+ y={lerp(
+ lerp(cursorStart.y, cursorEnd.y, cursorK),
+ cursorEnd.y + 90,
+ cursorAway,
+ )}
+ size={24}
+ opacity={1 - cursorAway}
+ />
+ </Frame>
+ );
+};
+
+const SWATCHES = [
+ WALLPAPERS.rome,
+ WALLPAPERS.santorini,
+ "linear-gradient(135deg,#C3DCF8 0%,#DACBF9 55%,#F6D9EC 100%)",
+ "#1C1D21",
+ WALLPAPERS.monaco,
+];
+
+const SWATCH_NAMES = [
+ "Rome",
+ "Santorini",
+ "Soft gradient",
+ "Charcoal",
+ "Monaco",
+];
+
+const swatchBg = (value: string): CSSProperties =>
+ value.startsWith("/")
+ ? {
+ backgroundImage: `url(${value})`,
+ backgroundSize: "cover",
+ backgroundPosition: "center",
+ }
+ : { background: value };
+
+const BG_FIRST = 1900;
+const BG_EACH = 1300;
+const backgroundAt = (t: number) =>
+ t < BG_FIRST
+ ? 0
+ : Math.min(SWATCHES.length - 1, Math.floor((t - BG_FIRST) / BG_EACH) + 1);
+
+const BackgroundView = ({
+ t,
+ pick,
+}: {
+ t: number;
+ playing: boolean;
+ pick: number | null;
+}) => {
+ const grow = pick === null ? seg(t, 600, 1700) : 1;
+ const pad = lerp(0, 48, grow);
+ const index = pick ?? backgroundAt(t);
+ // Only fetch a wallpaper once it has been shown, then keep it for the fade.
+ const seen = useRef(new Set<number>());
+ seen.current.add(index);
+ return (
+ <Frame style={{ background: "#F7F7F8" }}>
+ {SWATCHES.map((swatch, n) =>
+ seen.current.has(n) ? (
+ <div
+ key={swatch}
+ className="absolute inset-0 transition-opacity duration-500"
+ style={{ ...swatchBg(swatch), opacity: n === index ? grow : 0 }}
+ />
+ ) : null,
+ )}
+ <Recording pad={pad} radius={lerp(0, 12, grow)} shadow={grow} />
+ </Frame>
+ );
+};
+
+const SCENES: Scene[] = [
+ {
+ key: "zoom",
+ label: copyOf(STUDIO_COPY, "zoom").label,
+ icon: <ZoomIn />,
+ lead: copyOf(STUDIO_COPY, "zoom").lead,
+ body: copyOf(STUDIO_COPY, "zoom").body,
+ duration: ZOOM_MS,
+ pose: 2600,
+ View: ZoomView,
+ },
+ {
+ key: "layouts",
+ label: copyOf(STUDIO_COPY, "layouts").label,
+ icon: <Video />,
+ picker: {
+ options: LAYOUTS.map((layout, i) => ({
+ key: layout.name,
+ label: layout.name,
+ icon: <LayoutGlyph kind={i} />,
+ })),
+ at: layoutAt,
+ },
+ lead: copyOf(STUDIO_COPY, "layouts").lead,
+ body: copyOf(STUDIO_COPY, "layouts").body,
+ duration: STEP * LAYOUTS.length,
+ pose: STEP + MORPH,
+ View: ScenesView,
+ },
+ {
+ key: "captions",
+ label: copyOf(STUDIO_COPY, "captions").label,
+ icon: <Captions />,
+ lead: copyOf(STUDIO_COPY, "captions").lead,
+ body: copyOf(STUDIO_COPY, "captions").body,
+ duration: CAPTIONS_END,
+ pose: 2400,
+ View: CaptionsView,
+ },
+ {
+ key: "blur",
+ label: copyOf(STUDIO_COPY, "blur").label,
+ icon: <EyeOff />,
+ lead: copyOf(STUDIO_COPY, "blur").lead,
+ body: copyOf(STUDIO_COPY, "blur").body,
+ duration: 7200,
+ pose: 2600,
+ View: BlurView,
+ },
+ {
+ key: "background",
+ label: copyOf(STUDIO_COPY, "background").label,
+ icon: <ImageIcon />,
+ picker: {
+ options: SWATCHES.map((swatch, i) => ({
+ key: swatch,
+ label: SWATCH_NAMES[i] ?? "Background",
+ icon: (
+ <span
+ className="block size-full rounded-full"
+ style={swatchBg(swatch.startsWith("/") ? thumbOf(swatch) : swatch)}
+ />
+ ),
+ })),
+ at: backgroundAt,
+ swatches: true,
+ },
+ lead: copyOf(STUDIO_COPY, "background").lead,
+ body: copyOf(STUDIO_COPY, "background").body,
+ duration: BG_FIRST + BG_EACH * 4 + 900,
+ pose: 3400,
+ View: BackgroundView,
+ },
+];
+
+export const StudioStage = () => (
+ <Showcase scenes={SCENES} tone={TONES.studio} />
+);
diff --git a/apps/web/components/pages/HomeTwo/showcase/subjects.tsx b/apps/web/components/pages/HomeTwo/showcase/subjects.tsx
new file mode 100644
index 000000000..c97d9dcaa
--- /dev/null
+++ b/apps/web/components/pages/HomeTwo/showcase/subjects.tsx
@@ -0,0 +1,397 @@
+"use client";
+
+import { classNames } from "@cap/utils/helpers";
+import { type CSSProperties, type ReactNode, useRef } from "react";
+import { MacCursor, WindowsCursor } from "../cursors";
+import { TrafficLights, WindowsCaptionControls } from "../demo/chrome";
+import { PRESENTER, PresenterSources, useVideoAttrs } from "../demo/media";
+import { OS_FONT, useIsWindowsDemo } from "../demo/platform";
+
+export { PRESENTER };
+
+import { useVideo } from "../scenes/engine";
+
+export const INVOICE = { w: 680, h: 425 } as const;
+
+export const INVOICE_SPOTS = {
+ send: { x: 524, y: 360, w: 128, h: 34 },
+ email: { x: 326, y: 140, w: 160, h: 20 },
+ bank: { x: 172, y: 317, w: 176, h: 24 },
+ total: { x: 540, y: 276, w: 112, h: 28 },
+} as const;
+
+const NAV = ["Overview", "Invoices", "Clients", "Payments", "Settings"];
+
+const Field = ({
+ label,
+ value,
+ style,
+ valueStyle,
+}: {
+ label: string;
+ value: ReactNode;
+ style?: CSSProperties;
+ valueStyle?: CSSProperties;
+}) => (
+ <div className="absolute" style={style}>
+ <div className="text-[10.5px] font-medium text-[#8A8F98]">{label}</div>
+ <div
+ className="mt-1 text-[13.5px] font-medium text-[#1C1D21]"
+ style={valueStyle}
+ >
+ {value}
+ </div>
+ </div>
+);
+
+export const BrowserBar = ({
+ url,
+ className,
+ small,
+}: {
+ url: string;
+ className?: string;
+ small?: boolean;
+}) => {
+ const windows = useIsWindowsDemo();
+ const pill = classNames(
+ "flex items-center rounded-md bg-white text-[#6B7079] shadow-[0_0_0_1px_rgba(0,0,0,0.06)]",
+ small ? "h-[20px] text-[10.5px]" : "h-[22px] text-[11px]",
+ );
+ if (windows) {
+ return (
+ <div
+ className={classNames(
+ "flex items-stretch border-b border-[#E5E5E5] bg-[#F3F3F3]",
+ className,
+ )}
+ style={{ fontFamily: OS_FONT.windows }}
+ >
+ <div className="flex flex-1 items-center gap-2 pl-3">
+ <span
+ className={classNames(
+ pill,
+ "rounded-full px-3",
+ small ? "w-[230px]" : "w-[270px]",
+ )}
+ >
+ {url}
+ </span>
+ </div>
+ <WindowsCaptionControls className="scale-90" />
+ </div>
+ );
+ }
+ return (
+ <div
+ className={classNames(
+ "flex items-center border-b border-[#ECEDF0] bg-[#F7F7F8]",
+ small ? "px-3" : "px-3.5",
+ className,
+ )}
+ >
+ <TrafficLights size={small ? 9 : 10} />
+ <div
+ className={classNames(
+ pill,
+ "mx-auto justify-center",
+ small ? "w-[240px]" : "w-[260px]",
+ )}
+ >
+ {url}
+ </div>
+ <div className={small ? "w-[40px]" : "w-[46px]"} />
+ </div>
+ );
+};
+
+export const AppBar = ({
+ children,
+ className,
+ dark,
+}: {
+ children: ReactNode;
+ className?: string;
+ dark?: boolean;
+}) => {
+ const windows = useIsWindowsDemo();
+ return (
+ <div className={classNames("flex items-stretch", className)}>
+ {windows ? null : (
+ <span className="flex items-center pl-4 pr-1">
+ <TrafficLights size={dark ? 12 : 10} />
+ </span>
+ )}
+ <div
+ className="flex min-w-0 flex-1 items-center gap-3 px-3"
+ style={windows ? { fontFamily: OS_FONT.windows } : undefined}
+ >
+ {children}
+ </div>
+ {windows ? <WindowsCaptionControls light={dark} /> : null}
+ </div>
+ );
+};
+
+export const InvoicePage = ({
+ sent = 0,
+ press = 0,
+ emailStyle,
+ chrome = true,
+}: {
+ sent?: number;
+ press?: number;
+ emailStyle?: CSSProperties;
+ chrome?: boolean;
+}) => (
+ <div
+ className="relative overflow-hidden bg-white text-[#1C1D21]"
+ style={{ width: INVOICE.w, height: INVOICE.h, fontWeight: 500 }}
+ >
+ {chrome ? (
+ <BrowserBar
+ url="ledgerly.app/invoices/new"
+ className="absolute inset-x-0 top-0 h-9"
+ />
+ ) : null}
+
+ <div
+ className="absolute bottom-0 left-0 w-[150px] border-r border-[#ECEDF0] bg-[#FAFAFB] px-3 pt-4"
+ style={{ top: chrome ? 36 : 0 }}
+ >
+ <div className="flex items-center gap-2 px-2 text-[13px] font-semibold">
+ <span className="grid size-[18px] place-items-center rounded-[5px] bg-[#1C1D21] text-[10px] text-white">
+ L
+ </span>
+ Ledgerly
+ </div>
+ <div className="mt-4 flex flex-col gap-0.5">
+ {NAV.map((item) => (
+ <div
+ key={item}
+ className={classNames(
+ "rounded-md px-2 py-[5px] text-[12px]",
+ item === "Invoices"
+ ? "bg-[#EDEEF1] text-[#1C1D21]"
+ : "text-[#6B7079]",
+ )}
+ >
+ {item}
+ </div>
+ ))}
+ </div>
+ </div>
+
+ <div
+ className="absolute right-0 bottom-0 left-[150px]"
+ style={{ top: chrome ? 36 : 0 }}
+ >
+ <div className="absolute left-7 top-[18px] text-[11px] text-[#8A8F98]">
+ Invoices <span className="px-1 text-[#C2C5CC]">/</span> New invoice
+ </div>
+ <div className="absolute left-7 top-[38px] text-[21px] font-semibold tracking-[-0.02em]">
+ Invoice #1042
+ </div>
+ <div className="absolute right-7 top-[42px] rounded-full bg-[#F1F2F4] px-2.5 py-[3px] text-[11px] text-[#6B7079]">
+ {sent > 0.5 ? "Sent" : "Draft"}
+ </div>
+
+ <Field
+ label="Bill to"
+ value="Northwind Labs"
+ style={{ left: 28, top: 88 }}
+ />
+ <Field
+ label="Email"
+ style={{ left: 180, top: 88 }}
+ valueStyle={emailStyle}
+ />
+ <Field label="Due" value="Nov 14, 2026" style={{ left: 378, top: 88 }} />
+
+ <div className="absolute inset-x-7 top-[146px] flex border-b border-[#ECEDF0] pb-1.5 text-[10.5px] text-[#8A8F98]">
+ <span className="flex-1">Item</span>
+ <span className="w-12 text-right">Qty</span>
+ <span className="w-24 text-right">Amount</span>
+ </div>
+ {[
+ ["Design retainer, October", "1", "$3,600.00"],
+ ["Usability study", "1", "$600.00"],
+ ].map(([item, qty, amount], i) => (
+ <div
+ key={item}
+ className="absolute inset-x-7 flex border-b border-[#F2F3F5] py-2 text-[12.5px]"
+ style={{ top: 172 + i * 33 }}
+ >
+ <span className="flex-1">{item}</span>
+ <span className="w-12 text-right text-[#6B7079]">{qty}</span>
+ <span className="w-24 text-right tabular-nums">{amount}</span>
+ </div>
+ ))}
+ <div className="absolute right-7 top-[244px] flex items-baseline gap-6 text-[12.5px]">
+ <span className="text-[#8A8F98]">Total</span>
+ <span className="text-[17px] font-semibold tabular-nums">
+ $4,200.00
+ </span>
+ </div>
+
+ <Field
+ label="Pay to"
+ value="Ledgerly Bank •••• 4821"
+ style={{ left: 28, top: 268 }}
+ />
+
+ <div
+ className="absolute right-7 top-[324px] flex h-[34px] w-[128px] items-center justify-center rounded-[9px] text-[12.5px] text-white"
+ style={{
+ background: sent > 0.5 ? "#16A34A" : "#2563EB",
+ transform: `scale(${1 - press * 0.05})`,
+ boxShadow: "0 1px 2px rgba(0,0,0,0.12)",
+ transition: "background 250ms",
+ }}
+ >
+ {sent > 0.5 ? "Sent to client" : "Send invoice"}
+ </div>
+ </div>
+ </div>
+);
+
+export const Webcam = ({
+ playing,
+ className,
+ style,
+}: {
+ playing: boolean;
+ className?: string;
+ style?: CSSProperties;
+}) => {
+ const ref = useRef<HTMLVideoElement | null>(null);
+ useVideo(playing, ref);
+ const attrs = useVideoAttrs(PRESENTER.poster);
+ return (
+ <div
+ className={classNames("overflow-hidden bg-[#CFC6B8]", className)}
+ style={style}
+ >
+ <video
+ ref={ref}
+ muted
+ loop
+ playsInline
+ {...attrs}
+ className="size-full object-cover object-[50%_30%]"
+ >
+ <PresenterSources />
+ </video>
+ </div>
+ );
+};
+
+export const Cursor = ({
+ x,
+ y,
+ size = 26,
+ opacity = 1,
+}: {
+ x: number;
+ y: number;
+ size?: number;
+ opacity?: number;
+}) => {
+ const windows = useIsWindowsDemo();
+ // Arrow tips: (4, 3.3) in the 17x24 macOS art, (3.3, 3.75) in the 18x25
+ // Windows art, so the hotspot lands exactly on (x, y) for either.
+ const ratio = windows ? 18 / 25 : 17 / 24;
+ const tip = windows
+ ? { x: 3.3 / 25, y: 3.75 / 25 }
+ : { x: 4 / 24, y: 3.3 / 24 };
+ return (
+ <div
+ className="pointer-events-none absolute left-0 top-0 z-30"
+ style={{
+ width: size * ratio,
+ height: size,
+ transform: `translate(${x - size * tip.x}px, ${y - size * tip.y}px)`,
+ opacity,
+ filter: "drop-shadow(0 1px 1.5px rgba(0,0,0,0.45))",
+ }}
+ >
+ {windows ? (
+ <WindowsCursor className="block size-full" />
+ ) : (
+ <MacCursor className="block size-full" />
+ )}
+ </div>
+ );
+};
+
+export const Ripple = ({
+ x,
+ y,
+ k,
+ color = "rgba(37,99,235,0.35)",
+}: {
+ x: number;
+ y: number;
+ k: number;
+ color?: string;
+}) =>
+ k <= 0 || k >= 1 ? null : (
+ <div
+ className="pointer-events-none absolute z-20 rounded-full"
+ style={{
+ left: x - 22,
+ top: y - 22,
+ width: 44,
+ height: 44,
+ background: color,
+ transform: `scale(${0.4 + k * 1.1})`,
+ opacity: 1 - k,
+ }}
+ />
+ );
+
+export const WALLPAPERS = {
+ sf: "/backgrounds/landing/sf.webp",
+ santorini: "/backgrounds/landing/santorini.webp",
+ london: "/backgrounds/landing/london.webp",
+ miami: "/backgrounds/landing/miami.webp",
+ monaco: "/backgrounds/landing/monaco.webp",
+ nyc: "/backgrounds/landing/nyc.webp",
+ rome: "/backgrounds/landing/rome.webp",
+} as const;
+
+export const thumbOf = (src: string) => src.replace(/\.webp$/, "-thumb.webp");
+
+export const Wallpaper = ({
+ src,
+ opacity = 1,
+}: {
+ src: string;
+ opacity?: number;
+}) => (
+ <div
+ className="absolute inset-0 bg-cover bg-center"
+ style={{ backgroundImage: `url(${src})`, opacity }}
+ />
+);
+
+export const FrameChip = ({
+ children,
+ className,
+ style,
+}: {
+ children: ReactNode;
+ className?: string;
+ style?: CSSProperties;
+}) => (
+ <div
+ className={classNames(
+ "absolute z-20 rounded-full bg-[rgba(17,17,17,0.72)] px-3 py-1.5 text-[13px] text-white backdrop-blur-md",
+ className,
+ )}
+ style={style}
+ >
+ {children}
+ </div>
+);
diff --git a/apps/web/components/pages/HomeTwo/studio/StudioFeatures.tsx b/apps/web/components/pages/HomeTwo/studio/StudioFeatures.tsx
deleted file mode 100644
index 4081c94a4..000000000
--- a/apps/web/components/pages/HomeTwo/studio/StudioFeatures.tsx
+++ /dev/null
@@ -1,87 +0,0 @@
-"use client";
-
-import { useRef } from "react";
-import { Eyebrow } from "../Eyebrow";
-import { Fit, LazyMount } from "../scenes/engine";
-import { BAND, BODY_TEXT, grainBg, H_SECTION, MODE_THEME } from "../theme";
-import { useInView, useReducedMotion } from "../visibility";
-import { CARDS_A, CARDS_B } from "./catalog";
-import { CANVAS, type StudioCard } from "./shared";
-
-const ORDER = [
- "mask",
- "scenes",
- "zoom",
- "captions",
- "text",
- "three-d",
- "canvas",
- "grades",
- "clips",
-];
-const ALL = [...CARDS_A, ...CARDS_B];
-const CARDS = ORDER.flatMap((key) => {
- const card = ALL.find((item) => item.key === key);
- return card ? [card] : [];
-});
-
-const Card = ({ card }: { card: StudioCard }) => {
- const ref = useRef<HTMLLIElement | null>(null);
- const inView = useInView(ref, "-5% 0px -5% 0px");
- const reduced = useReducedMotion();
- return (
- <li
- ref={ref}
- className="flex flex-col rounded-[18px] p-1.5 shadow-[0_0_0_1px_rgba(17,17,17,0.05)] md:last:col-span-2 lg:last:col-span-1"
- style={grainBg(BAND)}
- >
- <div className="overflow-hidden rounded-[13px] shadow-[0_0_0_1px_rgba(17,17,17,0.06)]">
- <LazyMount w={CANVAS.w} h={CANVAS.h} grow>
- <Fit w={CANVAS.w} h={CANVAS.h} grow>
- <card.Visual playing={inView && !reduced} />
- </Fit>
- </LazyMount>
- </div>
- <div className="px-3.5 pb-3.5 pt-3.5">
- <h3 className="text-[17px] font-medium leading-[1.2] tracking-[-0.02em] text-[#111111]">
- {card.title}
- </h3>
- <p
- className={`${BODY_TEXT} mt-1.5 text-[14px] leading-[1.5] text-[rgba(17,17,17,0.68)]`}
- >
- {card.body}
- </p>
- </div>
- </li>
- );
-};
-
-export const StudioFeatures = () => (
- <section className="px-5 py-16 lg:py-24">
- <div className="mx-auto max-w-[1200px]">
- <div className="mx-auto flex max-w-[720px] flex-col items-center text-center">
- <Eyebrow accent={MODE_THEME.studio.accent}>
- Studio Mode · The editor
- </Eyebrow>
- <h2
- className={`${H_SECTION} mt-6 text-balance text-[clamp(34px,4.4vw,48px)]`}
- >
- Polish it before anyone sees it
- </h2>
- <p
- className={`${BODY_TEXT} mt-5 max-w-[560px] text-balance text-[16px] leading-[1.5] text-[rgba(17,17,17,0.78)] sm:text-[17px]`}
- >
- Studio Mode opens straight into an editor made for screen recordings.
- Blur what is private, switch scenes between screen and camera, add
- text and captions, grade the color, and export in 4K or as a link.
- </p>
- </div>
-
- <ul className="mt-10 grid gap-3 md:grid-cols-2 lg:grid-cols-3">
- {CARDS.map((card) => (
- <Card key={card.key} card={card} />
- ))}
- </ul>
- </div>
- </section>
-);
diff --git a/apps/web/components/pages/HomeTwo/studio/cards/canvas.tsx b/apps/web/components/pages/HomeTwo/studio/cards/canvas.tsx
deleted file mode 100644
index 5c3971fc5..000000000
--- a/apps/web/components/pages/HomeTwo/studio/cards/canvas.tsx
+++ /dev/null
@@ -1,428 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import Image from "next/image";
-import { useRef } from "react";
-import { useCursor, useSceneState, type Way } from "../../scenes/engine";
-import {
- Chip,
- ED,
- EditorButton,
- Ground,
- Panel,
- RecordedWindow,
- Segmented,
- Slider,
- useLoop,
-} from "../shared";
-
-const SOURCES = [
- "Desktop",
- "Wallpaper",
- "Image",
- "Color",
- "Gradient",
- "Animated",
-] as const;
-const CATEGORIES = ["macOS", "Dark", "Blue", "Cities", "Purple", "Orange"];
-const CITIES = ["liverpool", "santorini", "miami", "monaco", "london", "rome"];
-const GRADIENTS = [
- "linear-gradient(135deg, #FF7E5F 0%, #FEB47B 100%)",
- "linear-gradient(135deg, #667EEA 0%, #764BA2 100%)",
- "linear-gradient(135deg, #43CEA2 0%, #185A9D 100%)",
- "linear-gradient(135deg, #F857A6 0%, #FF5858 100%)",
- "linear-gradient(135deg, #1FA2FF 0%, #12D8FA 55%, #A6FFCB 100%)",
- "linear-gradient(135deg, #0F2027 0%, #203A43 50%, #2C5364 100%)",
-];
-
-const WALL_FROM = 3;
-const WALL_TO = 1;
-const GRADIENT_TO = 1;
-
-const DURATION = 10000;
-const WALL_AT = 1400;
-const GRADIENT_AT = 3600;
-const SWATCH_AT = 4600;
-const BROWSER_AT = 5800;
-const MACBOOK_AT = 7600;
-const UNFRAME_AT = 9400;
-
-const S = 1.15;
-const PANEL = { left: 12, top: 211, w: 500, pad: 12 } as const;
-const ROW = { pill: 12, chips: 52, grid: 84 } as const;
-const THUMB = { w: 34, h: 34, gap: 6 } as const;
-const RESULT = { w: 336, h: 189, left: 114, top: 12 } as const;
-const WIN_SCALE = 0.46;
-const WIN = {
- left: Math.round((RESULT.w - 360 * WIN_SCALE) / 2),
- top: 46,
-} as const;
-
-const toCanvas = (x: number, y: number) => ({
- x: PANEL.left + x * S,
- y: PANEL.top + y * S,
-});
-const thumbCenter = (i: number) =>
- toCanvas(
- PANEL.pad + i * (THUMB.w + THUMB.gap) + THUMB.w / 2,
- ROW.grid + THUMB.h / 2,
- );
-const pillCenter = (i: number) => {
- const inner = PANEL.w - PANEL.pad * 2 - 4;
- const item = (inner - 10) / 6;
- return toCanvas(PANEL.pad + 2 + item * (i + 0.5) + 2 * i, ROW.pill + 15);
-};
-
-const PATH: Way[] = [
- { t: 0, x: 380, y: 130 },
- { t: WALL_AT - 500, ...thumbCenter(WALL_TO) },
- { t: WALL_AT, ...thumbCenter(WALL_TO), click: true },
- { t: GRADIENT_AT - 600, ...pillCenter(4) },
- { t: GRADIENT_AT, ...pillCenter(4), click: true },
- { t: SWATCH_AT - 400, ...thumbCenter(GRADIENT_TO) },
- { t: SWATCH_AT, ...thumbCenter(GRADIENT_TO), click: true },
- { t: SWATCH_AT + 900, x: 470, y: 150 },
- { t: DURATION, x: 470, y: 150 },
-];
-
-type Frame = "none" | "browser" | "macbook";
-
-const uiAt = (t: number) => {
- const source = t >= GRADIENT_AT ? "Gradient" : "Wallpaper";
- const wall = t >= WALL_AT ? WALL_TO : WALL_FROM;
- const gradient = t >= SWATCH_AT ? GRADIENT_TO : 0;
- const frame: Frame =
- t >= UNFRAME_AT
- ? "none"
- : t >= MACBOOK_AT
- ? "macbook"
- : t >= BROWSER_AT
- ? "browser"
- : "none";
- return { source, wall, gradient, frame };
-};
-
-const title = (city: string) => city.charAt(0).toUpperCase() + city.slice(1);
-
-const MiniField = ({
- label,
- value,
- fill,
-}: {
- label: string;
- value: string;
- fill: number;
-}) => (
- <div className="flex h-[26px] items-center gap-2">
- <span
- className="w-[54px] shrink-0 text-[13px] font-normal"
- style={{ color: ED.text1 }}
- >
- {label}
- </span>
- <div className="min-w-0 flex-1">
- <Slider fill={fill} />
- </div>
- <span
- className="min-w-8 text-right text-[11px] tabular-nums"
- style={{ color: ED.text3 }}
- >
- {value}
- </span>
- </div>
-);
-
-const Swatch = ({
- selected,
- children,
-}: {
- selected: boolean;
- children: React.ReactNode;
-}) => (
- <span
- className={classNames(
- "relative block overflow-hidden rounded-md transition-shadow duration-200",
- selected && "ring-2 ring-offset-2",
- )}
- style={{
- width: THUMB.w,
- height: THUMB.h,
- ["--tw-ring-color" as string]: ED.accent,
- ["--tw-ring-offset-color" as string]: ED.card,
- }}
- >
- {children}
- </span>
-);
-
-export const Visual = ({ playing }: { playing: boolean }) => {
- const rootRef = useRef<HTMLDivElement | null>(null);
- const [ui, setUi] = useSceneState(uiAt(0));
- const cursor = useCursor(rootRef);
-
- useLoop({
- duration: DURATION,
- playing,
- pose: 2400,
- tick: (t, seek) => {
- setUi(uiAt(t));
- cursor.tick(PATH, t, seek);
- },
- });
-
- const wallpaper = ui.source === "Wallpaper";
- const browser = ui.frame === "browser";
- const macbook = ui.frame === "macbook";
- const label =
- ui.frame === "browser"
- ? "Frame · Browser"
- : ui.frame === "macbook"
- ? "Frame · MacBook"
- : wallpaper
- ? `Wallpaper · ${title(CITIES[ui.wall] ?? "")}`
- : "Gradient";
-
- return (
- <div ref={rootRef} className="relative">
- <Ground tone="light">
- <div
- className="absolute overflow-hidden rounded-[10px]"
- style={{
- left: RESULT.left,
- top: RESULT.top,
- width: RESULT.w,
- height: RESULT.h,
- boxShadow:
- "0 12px 32px -8px rgba(0,0,0,0.35), 0 0 0 0.5px rgba(0,0,0,0.12)",
- }}
- >
- {[WALL_FROM, WALL_TO].map((index) => (
- <Image
- key={CITIES[index]}
- src={`/backgrounds/${CITIES[index]}.webp`}
- alt=""
- fill
- sizes="336px"
- draggable={false}
- className="object-cover transition-opacity duration-700"
- style={{ opacity: wallpaper && ui.wall === index ? 1 : 0 }}
- />
- ))}
- <div
- className="absolute inset-0 transition-opacity duration-700"
- style={{
- background: GRADIENTS[ui.gradient],
- opacity: wallpaper ? 0 : 1,
- }}
- />
-
- <div
- className="absolute"
- style={{
- left: WIN.left,
- top: WIN.top,
- width: 360 * WIN_SCALE,
- height: 250 * WIN_SCALE,
- }}
- >
- <div
- className="absolute left-0 top-0"
- style={{
- width: 360,
- height: 250,
- transform: `scale(${WIN_SCALE})`,
- transformOrigin: "top left",
- }}
- >
- <div
- className="pointer-events-none absolute transition-[opacity,transform] duration-500"
- style={{
- left: -14,
- top: -14,
- right: -14,
- bottom: -14,
- borderRadius: 22,
- background:
- "linear-gradient(180deg, #2b2d33 0%, #101114 12%)",
- boxShadow:
- "inset 0 0 0 1px rgba(255,255,255,0.08), 0 30px 60px -30px rgba(9,12,20,0.7)",
- opacity: macbook ? 1 : 0,
- transform: macbook ? "scale(1)" : "scale(1.04)",
- }}
- />
- <div
- className="pointer-events-none absolute transition-opacity duration-500"
- style={{
- left: -48,
- right: -48,
- bottom: -26,
- height: 12,
- borderRadius: "0 0 14px 14px",
- background: "linear-gradient(180deg, #d7d9de, #9a9ea6)",
- boxShadow: "0 14px 30px -12px rgba(9,12,20,0.6)",
- opacity: macbook ? 1 : 0,
- }}
- />
- <div
- className="pointer-events-none absolute inset-x-0 flex items-center gap-2 px-3 transition-[opacity,transform] duration-500"
- style={{
- top: -34,
- height: 36,
- borderRadius: "10px 10px 0 0",
- background: "#f6f7f9",
- border: "1px solid rgba(0,0,0,0.08)",
- borderBottom: "none",
- opacity: browser ? 1 : 0,
- transform: browser ? "translateY(0)" : "translateY(10px)",
- }}
- >
- <span className="flex items-center gap-1.5">
- {["#FF5F57", "#FEBC2E", "#28C840"].map((color) => (
- <span
- key={color}
- className="size-[9px] rounded-full"
- style={{ background: color }}
- />
- ))}
- </span>
- <span
- className="ml-1 flex h-6 items-center rounded-t-md px-3 text-[10.5px] font-medium"
- style={{ background: "#ffffff", color: "#202020" }}
- >
- Dashboard
- </span>
- <span
- className="ml-auto flex h-5 w-[46%] items-center justify-center rounded-md text-[10px]"
- style={{
- background: "rgba(17,17,17,0.06)",
- color: "rgba(17,17,17,0.6)",
- }}
- >
- acme.com/dashboard
- </span>
- </div>
- <div
- className="absolute inset-0 overflow-hidden transition-[border-radius] duration-500"
- style={{
- borderRadius: 10,
- boxShadow: "0 24px 50px -20px rgba(9,12,20,0.6)",
- }}
- >
- <div
- className="absolute inset-x-0 transition-[top] duration-500"
- style={{ top: browser ? -40 : 0 }}
- >
- <RecordedWindow left={0} top={0} />
- </div>
- </div>
- </div>
- </div>
- </div>
-
- <div
- className="absolute z-20"
- style={{ left: RESULT.left + 10, top: RESULT.top + 6 }}
- >
- <Chip className="h-6">
- <span
- className="size-2 rounded-full"
- style={{ background: ED.accent }}
- />
- {label}
- </Chip>
- </div>
-
- <Panel
- className="z-30 gap-0"
- style={{
- left: PANEL.left,
- top: PANEL.top,
- width: PANEL.w,
- padding: PANEL.pad,
- transform: `scale(${S})`,
- transformOrigin: "top left",
- }}
- >
- <Segmented options={SOURCES} value={ui.source} />
-
- <div className="mt-2.5 flex h-6 items-center justify-between">
- {wallpaper ? (
- <div className="flex items-center gap-1">
- {CATEGORIES.map((category) => {
- const on = category === "Cities";
- return (
- <span
- key={category}
- className="flex h-6 items-center rounded-[7px] px-2.5 text-[12px] font-medium"
- style={{
- background: on ? ED.ctlHover : "transparent",
- color: on ? ED.text1 : ED.text2,
- }}
- >
- {category}
- </span>
- );
- })}
- </div>
- ) : (
- <>
- <span
- className="text-[12px] font-medium"
- style={{ color: ED.text2 }}
- >
- Presets
- </span>
- <EditorButton className="h-6 text-[12px]">
- Randomize
- </EditorButton>
- </>
- )}
- </div>
-
- <div className="mt-2 flex items-start gap-4">
- <div
- className="grid shrink-0 grid-cols-6"
- style={{ gap: THUMB.gap }}
- >
- {wallpaper
- ? CITIES.map((city, i) => (
- <Swatch key={city} selected={ui.wall === i}>
- <Image
- src={`/backgrounds/thumbs/${city}.webp`}
- alt=""
- width={THUMB.w}
- height={THUMB.h}
- draggable={false}
- className="h-full w-full object-cover"
- />
- </Swatch>
- ))
- : GRADIENTS.map((gradient, i) => (
- <Swatch key={gradient} selected={ui.gradient === i}>
- <span
- className="block h-full w-full"
- style={{ background: gradient }}
- />
- </Swatch>
- ))}
- </div>
- <div className="flex min-w-0 flex-1 flex-col">
- {wallpaper ? (
- <>
- <MiniField label="Padding" value="10%" fill={0.25} />
- <MiniField label="Corners" value="7.5%" fill={0.075} />
- </>
- ) : (
- <>
- <MiniField label="Angle" value="90°" fill={0.25} />
- <MiniField label="Noise" value="0" fill={0} />
- </>
- )}
- </div>
- </div>
- </Panel>
- {cursor.Cursor}
- </Ground>
- </div>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/studio/cards/captions.tsx b/apps/web/components/pages/HomeTwo/studio/cards/captions.tsx
deleted file mode 100644
index 3c65ac356..000000000
--- a/apps/web/components/pages/HomeTwo/studio/cards/captions.tsx
+++ /dev/null
@@ -1,207 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import { useRef } from "react";
-import { LucideChevronDown } from "../../demo/capIcons";
-import { useCursor, useSceneState, type Way } from "../../scenes/engine";
-import {
- ED,
- EditorButton,
- Field,
- Ground,
- Panel,
- RecordedWindow,
- Toggle,
- useLoop,
-} from "../shared";
-
-const WIN = { left: 10, top: 38, scale: 1.12 };
-const PANEL = { left: 368, top: 12, w: 196, scale: 1.1 };
-const LINES = [
- { start: 500, words: "Here is the new dashboard".split(" ") },
- { start: 4400, words: "Every card pulls live data".split(" ") },
-];
-const WORD_MS = 380;
-const TOGGLE_AT = 7600;
-const DURATION = 9000;
-const TOGGLE = {
- x: PANEL.left + 151 * PANEL.scale,
- y: PANEL.top + 283 * PANEL.scale,
-};
-
-const PATH: Way[] = [
- { t: 0, x: 300, y: 190 },
- { t: 6400, x: 300, y: 190 },
- { t: 7300, ...TOGGLE },
- { t: TOGGLE_AT, ...TOGGLE, click: true },
- { t: 8400, x: TOGGLE.x - 40, y: TOGGLE.y + 60 },
- { t: DURATION, x: TOGGLE.x - 40, y: TOGGLE.y + 60 },
-];
-
-const uiAt = (t: number) => {
- let line = 0;
- for (let i = 0; i < LINES.length; i++) {
- if (t >= (LINES[i]?.start ?? 0)) line = i;
- }
- const current = LINES[line];
- const elapsed = t - (current?.start ?? 0);
- const active = Math.min(
- (current?.words.length ?? 1) - 1,
- Math.floor(Math.max(0, elapsed) / WORD_MS),
- );
- return {
- line,
- active,
- shown: t >= (LINES[0]?.start ?? 0),
- exportOn: t >= TOGGLE_AT,
- };
-};
-
-const SelectBox = ({
- children,
- sub,
- right,
-}: {
- children: React.ReactNode;
- sub?: string;
- right?: string;
-}) => (
- <div
- className="flex items-center justify-between gap-2 rounded-lg px-2.5"
- style={{
- minHeight: sub ? 44 : 32,
- boxShadow: `inset 0 0 0 1px ${ED.lineStrong}`,
- background: ED.card,
- }}
- >
- <span className="flex min-w-0 flex-col leading-tight">
- <span className="text-[13px]" style={{ color: ED.text1 }}>
- {children}
- </span>
- {sub ? (
- <span
- className="whitespace-nowrap text-[10px]"
- style={{ color: ED.text3 }}
- >
- {sub}
- </span>
- ) : null}
- </span>
- <span className="flex shrink-0 items-center gap-1.5">
- {right ? (
- <span className="text-[11px] tabular-nums" style={{ color: ED.text3 }}>
- {right}
- </span>
- ) : null}
- <LucideChevronDown className="size-3.5" style={{ color: ED.text3 }} />
- </span>
- </div>
-);
-
-export const Visual = ({ playing }: { playing: boolean }) => {
- const rootRef = useRef<HTMLDivElement | null>(null);
- const [ui, setUi] = useSceneState(uiAt(0));
- const cursor = useCursor(rootRef);
-
- useLoop({
- duration: DURATION,
- playing,
- pose: 2200,
- tick: (t, seek) => {
- setUi(uiAt(t));
- cursor.tick(PATH, t, seek);
- },
- });
-
- const line = LINES[ui.line] ?? LINES[0];
-
- return (
- <div ref={rootRef} className="relative">
- <Ground tone="light">
- <RecordedWindow left={WIN.left} top={WIN.top} scale={WIN.scale} />
- <div
- className="pointer-events-none absolute rounded-[10px]"
- style={{
- left: WIN.left,
- top: WIN.top,
- width: 360 * WIN.scale,
- height: 250 * WIN.scale,
- background: "rgba(9,12,20,0.16)",
- }}
- />
-
- <div
- className="absolute z-20 flex items-center gap-x-1.5 whitespace-nowrap rounded-xl px-4 py-2 transition-opacity duration-300"
- style={{
- left: 22,
- top: 299,
- background: "rgba(17,17,17,0.74)",
- opacity: ui.shown ? 1 : 0,
- }}
- >
- {line?.words.map((word, i) => (
- <span
- key={`${ui.line}-${word}-${i}`}
- className={classNames(
- "rounded-md px-1.5 py-1 text-[20px] font-medium leading-none",
- i === ui.active ? "text-white" : "text-white/90",
- )}
- style={{ background: i === ui.active ? ED.accent : undefined }}
- >
- {word}
- </span>
- ))}
- </div>
-
- <div
- className="absolute z-30"
- style={{
- left: PANEL.left,
- top: PANEL.top,
- width: PANEL.w,
- transform: `scale(${PANEL.scale})`,
- transformOrigin: "top left",
- }}
- >
- <Panel className="left-0 top-0 w-full gap-3">
- <div className="flex min-h-[22px] items-center gap-1.5">
- <span
- className="text-[12px] font-medium"
- style={{ color: ED.text2 }}
- >
- Captions
- </span>
- <span
- className="rounded-full px-1.5 py-0.5 text-[10px] font-medium"
- style={{ background: ED.ctl, color: ED.text2 }}
- >
- Beta
- </span>
- </div>
- <Field label="Model">
- <SelectBox sub="parakeet-tdt-0.6b-v3 int8" right="~640MB">
- Recommended
- </SelectBox>
- </Field>
- <Field label="Language">
- <SelectBox>English</SelectBox>
- </Field>
- <EditorButton primary className="w-full justify-center">
- Generate Captions
- </EditorButton>
- <div className="flex items-center justify-between gap-3 pt-1">
- <span
- className="text-[13px] leading-tight"
- style={{ color: ED.text1 }}
- >
- Export with Subtitles
- </span>
- <Toggle on={ui.exportOn} />
- </div>
- </Panel>
- </div>
- {cursor.Cursor}
- </Ground>
- </div>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/studio/cards/clips.tsx b/apps/web/components/pages/HomeTwo/studio/cards/clips.tsx
deleted file mode 100644
index 495319452..000000000
--- a/apps/web/components/pages/HomeTwo/studio/cards/clips.tsx
+++ /dev/null
@@ -1,322 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import { useRef } from "react";
-import { CapAudioOn, CapClapperboard, LucidePlus } from "../../demo/capIcons";
-import {
- easeInOut,
- lerp,
- span,
- useCursor,
- useSceneState,
- type Way,
-} from "../../scenes/engine";
-import {
- CANVAS,
- ED,
- Ground,
- HUE,
- Lane,
- mix,
- Playhead,
- RULER_H,
- Ruler,
- Segment,
- SpeedChip,
- TRACK_GAP,
- TRACK_GUTTER,
- TRACK_H,
- TrackLabel,
- useLoop,
- Waveform,
-} from "../shared";
-
-const S = 1.5;
-const LOGICAL = { w: CANVAS.w / S, h: CANVAS.h / S };
-const INSET = 14;
-const PAD = 12;
-const CARD = {
- x: INSET,
- w: LOGICAL.w - INSET * 2,
- h: 10 + RULER_H + TRACK_GAP + TRACK_H + TRACK_GAP + TRACK_H + PAD,
-};
-const CARD_Y = Math.round((LOGICAL.h - CARD.h) / 2) + 4;
-const LANE_X = CARD.x + PAD + TRACK_GUTTER;
-const LANE_W = CARD.w - PAD * 2 - TRACK_GUTTER;
-const VIDEO_Y = CARD_Y + 10 + RULER_H + TRACK_GAP;
-const AUDIO_Y = VIDEO_Y + TRACK_H + TRACK_GAP;
-
-const DURATION = 10000;
-const CLIP_A = {
- start: Math.round(LANE_W * 0.48),
- trimmed: Math.round(LANE_W * 0.38),
-};
-const CLIP_B = {
- start: Math.round(LANE_W * 0.44),
- fast: Math.round(LANE_W * 0.32),
-};
-const JOIN = 4;
-const TRIM = { start: 2200, end: 3400 };
-const SPEED_AT = 3900;
-const FADE_AT = 5200;
-const AUDIO_AT = 6400;
-
-const g = (x: number, y: number) => ({ x: x * S, y: y * S });
-const mid = VIDEO_Y + TRACK_H / 2;
-
-const PATH: Way[] = [
- { t: 0, ...g(LANE_X + 40, CARD_Y - 6) },
- { t: 1900, ...g(LANE_X + CLIP_A.start + 1, mid) },
- { t: TRIM.start, ...g(LANE_X + CLIP_A.start + 1, mid) },
- { t: TRIM.end, ...g(LANE_X + CLIP_A.trimmed + 1, mid) },
- {
- t: SPEED_AT - 150,
- ...g(LANE_X + CLIP_A.trimmed + JOIN + CLIP_B.start - 22, mid),
- },
- {
- t: SPEED_AT,
- ...g(LANE_X + CLIP_A.trimmed + JOIN + CLIP_B.start - 22, mid),
- click: true,
- },
- { t: FADE_AT - 150, ...g(LANE_X + CLIP_A.trimmed + JOIN / 2, mid) },
- { t: FADE_AT, ...g(LANE_X + CLIP_A.trimmed + JOIN / 2, mid), click: true },
- { t: AUDIO_AT + 400, ...g(LANE_X + LANE_W - 24, AUDIO_Y + TRACK_H + 14) },
- { t: DURATION, ...g(LANE_X + LANE_W - 24, AUDIO_Y + TRACK_H + 14) },
-];
-
-const uiAt = (t: number) => ({
- trimmed: t >= TRIM.start + 400,
- speed: t >= SPEED_AT,
- fade: t >= FADE_AT,
- audio: t >= AUDIO_AT,
-});
-
-const widthsAt = (t: number) => {
- const trim = easeInOut(span(t, TRIM.start, TRIM.end));
- const a = lerp(CLIP_A.start, CLIP_A.trimmed, trim);
- const b = t >= SPEED_AT ? CLIP_B.fast : CLIP_B.start;
- return { a, b };
-};
-
-export const Visual = ({ playing }: { playing: boolean }) => {
- const rootRef = useRef<HTMLDivElement | null>(null);
- const clipARef = useRef<HTMLDivElement | null>(null);
- const clipBRef = useRef<HTMLDivElement | null>(null);
- const seamRef = useRef<HTMLDivElement | null>(null);
- const playheadRef = useRef<HTMLDivElement | null>(null);
- const [ui, setUi] = useSceneState(uiAt(0));
- const cursor = useCursor(rootRef);
-
- useLoop({
- duration: DURATION,
- playing,
- pose: 7200,
- tick: (t, seek) => {
- setUi(uiAt(t));
- const { a, b } = widthsAt(t);
- if (clipARef.current) clipARef.current.style.width = `${a}px`;
- if (clipBRef.current) {
- clipBRef.current.style.left = `${a + JOIN}px`;
- clipBRef.current.style.width = `${b}px`;
- }
- if (seamRef.current) seamRef.current.style.left = `${a - 6}px`;
- const frac = (t % 3200) / 3200;
- const split = t >= SPEED_AT ? 0.66 : a / (a + b);
- const head =
- frac < split
- ? lerp(0, a, frac / split)
- : lerp(a + JOIN, a + JOIN + b, (frac - split) / (1 - split));
- if (playheadRef.current) {
- playheadRef.current.style.transform = `translateX(${head}px)`;
- }
- cursor.tick(PATH, t, seek);
- },
- });
-
- return (
- <div ref={rootRef} className="relative">
- <Ground tone="chrome">
- <div
- className="absolute left-0 top-0"
- style={{
- width: LOGICAL.w,
- height: LOGICAL.h,
- transform: `scale(${S})`,
- transformOrigin: "top left",
- }}
- >
- <span
- className="absolute left-1/2 h-1 w-9 -translate-x-1/2 rounded-full"
- style={{ top: CARD_Y - 12, background: ED.lineStrong }}
- />
- <div
- className="absolute rounded-xl"
- style={{
- left: CARD.x,
- top: CARD_Y,
- width: CARD.w,
- height: CARD.h,
- background: ED.card,
- boxShadow: ED.cardShadow,
- }}
- />
-
- <div
- className="absolute flex items-end"
- style={{
- left: CARD.x + PAD,
- top: CARD_Y + 10,
- width: CARD.w - PAD * 2,
- }}
- >
- <div
- className="flex shrink-0 items-end pb-1"
- style={{ width: TRACK_GUTTER }}
- >
- <span
- className="inline-flex h-6 items-center gap-1 rounded-md pl-1.5 pr-2 text-[12px] font-medium"
- style={{ background: ED.ctl, color: ED.text2 }}
- >
- <LucidePlus className="size-3" />
- Add track
- </span>
- </div>
- <Ruler
- labels={["0:00", "0:05", "0:10", "0:15", "0:20", "0:25"]}
- span={5.6}
- />
- </div>
-
- <div
- className="absolute flex"
- style={{
- left: CARD.x + PAD,
- top: VIDEO_Y,
- width: CARD.w - PAD * 2,
- }}
- >
- <TrackLabel
- hue={HUE.clip}
- icon={<CapClapperboard className="size-3.5" />}
- >
- Clip
- </TrackLabel>
- <Lane>
- <Segment
- hue={HUE.clip}
- segmentRef={clipARef}
- label="Clip 1"
- sublabel={ui.trimmed ? undefined : "0:14"}
- trailing={<SpeedChip>1x</SpeedChip>}
- style={{ left: 0, width: CLIP_A.start }}
- >
- <Waveform from={0} to={34} />
- </Segment>
- <Segment
- hue={HUE.clip}
- segmentRef={clipBRef}
- label="Clip 2"
- sublabel={ui.speed ? undefined : "0:10"}
- trailing={
- <SpeedChip active={ui.speed}>
- {ui.speed ? "2x" : "1x"}
- </SpeedChip>
- }
- style={{ left: CLIP_A.start + JOIN, width: CLIP_B.start }}
- >
- <Waveform from={18} to={44} />
- </Segment>
- <div
- ref={seamRef}
- className={classNames(
- "pointer-events-none absolute inset-y-0 w-4 rounded-sm border-x transition-opacity duration-300",
- ui.fade ? "opacity-100" : "opacity-0",
- )}
- style={{
- left: CLIP_A.start - 6,
- borderColor: mix(ED.accent, 60, "transparent"),
- background: `repeating-linear-gradient(135deg, ${mix(ED.accent, 22, "transparent")} 0 3px, transparent 3px 6px)`,
- }}
- >
- <span
- className="absolute -top-[26px] left-1/2 -translate-x-1/2 whitespace-nowrap rounded-md px-1.5 py-0.5 text-[10px] font-medium"
- style={{
- background: ED.card,
- color: ED.text1,
- boxShadow: ED.popShadow,
- }}
- >
- Crossfade · 0.5s
- </span>
- </div>
- </Lane>
- </div>
-
- <div
- className="absolute flex"
- style={{
- left: CARD.x + PAD,
- top: AUDIO_Y,
- width: CARD.w - PAD * 2,
- }}
- >
- <TrackLabel
- hue={HUE.audio}
- icon={<CapAudioOn className="size-3.5" />}
- >
- Audio
- </TrackLabel>
- <Lane
- empty={ui.audio ? undefined : "Add audio, music or other sounds"}
- >
- <Segment
- hue={HUE.audio}
- label="Lofi Beats"
- sublabel="24s"
- className={classNames(
- "transition-[opacity,transform] duration-500",
- ui.audio
- ? "translate-y-0 opacity-100"
- : "translate-y-1 opacity-0",
- )}
- style={{ left: 0, width: LANE_W * 0.92 }}
- >
- <svg
- aria-hidden="true"
- className="pointer-events-none absolute inset-0 h-full w-full"
- viewBox="0 0 100 44"
- preserveAspectRatio="none"
- >
- <path
- d="M0 44 L0 40 L8 8 L92 8 L100 40 L100 44 Z"
- fill={mix(HUE.audio, 10, "transparent")}
- />
- <path
- d="M0 40 L8 8 L92 8 L100 40"
- fill="none"
- stroke={mix(HUE.audio, 55, "transparent")}
- strokeWidth="1"
- vectorEffect="non-scaling-stroke"
- />
- </svg>
- <Waveform color={mix(HUE.audio, 40, "transparent")} />
- </Segment>
- </Lane>
- </div>
-
- <div
- className="pointer-events-none absolute"
- style={{ left: LANE_X, top: VIDEO_Y - 12, width: LANE_W }}
- >
- <Playhead
- playheadRef={playheadRef}
- top={0}
- bottom={-(TRACK_H * 2 + TRACK_GAP + 12)}
- />
- </div>
- </div>
- {cursor.Cursor}
- </Ground>
- </div>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/studio/cards/grades.tsx b/apps/web/components/pages/HomeTwo/studio/cards/grades.tsx
deleted file mode 100644
index 2b8073297..000000000
--- a/apps/web/components/pages/HomeTwo/studio/cards/grades.tsx
+++ /dev/null
@@ -1,328 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import { useRef } from "react";
-import {
- easeOut,
- span,
- useCursor,
- useSceneState,
- type Way,
-} from "../../scenes/engine";
-import {
- CANVAS,
- ED,
- Field,
- Ground,
- Panel,
- RecordedWindow,
- SectionTitle,
- Slider,
- useLoop,
-} from "../shared";
-
-type Grade = { name: string; filter: string; tint: string };
-
-const GRADES: Grade[] = [
- { name: "None", filter: "none", tint: "transparent" },
- {
- name: "Cinematic",
- filter: "contrast(1.15) saturate(0.82) sepia(0.12)",
- tint: "rgba(24,52,92,0.18)",
- },
- {
- name: "Noir",
- filter: "grayscale(1) contrast(1.25) brightness(0.96)",
- tint: "rgba(0,0,0,0.08)",
- },
- {
- name: "Vintage",
- filter: "sepia(0.45) contrast(0.95) brightness(1.04) saturate(0.9)",
- tint: "rgba(255,196,120,0.16)",
- },
- {
- name: "Frost",
- filter: "saturate(0.75) brightness(1.06) hue-rotate(-8deg)",
- tint: "rgba(168,208,255,0.24)",
- },
- {
- name: "Golden",
- filter: "sepia(0.3) saturate(1.2) brightness(1.03)",
- tint: "rgba(255,186,74,0.2)",
- },
- {
- name: "Midnight",
- filter: "brightness(0.82) contrast(1.15) saturate(0.9) hue-rotate(12deg)",
- tint: "rgba(22,30,86,0.3)",
- },
- {
- name: "Vivid",
- filter: "saturate(1.55) contrast(1.08)",
- tint: "transparent",
- },
- {
- name: "Dreamy",
- filter: "brightness(1.08) contrast(0.88) saturate(1.15)",
- tint: "rgba(255,190,230,0.18)",
- },
-];
-
-const GROUND_GRADIENT =
- "linear-gradient(135deg, #C3DCF8 0%, #DACBF9 55%, #F6D9EC 100%)";
-
-const DURATION = 9600;
-const STOPS: { name: string; at: number }[] = [
- { name: "Cinematic", at: 1000 },
- { name: "Noir", at: 2400 },
- { name: "Golden", at: 3800 },
- { name: "Midnight", at: 5200 },
- { name: "Vivid", at: 6600 },
- { name: "Dreamy", at: 8000 },
-];
-const SWEEP = 900;
-const WIPE_MAX = 0.52;
-
-const WIN = { left: 10, top: 46, scale: 0.95 } as const;
-const S = 1.3;
-const PANEL = { w: 190, pad: 12 } as const;
-const PANEL_POS = { left: CANVAS.w - PANEL.w * S - 12, top: 18 } as const;
-const SWATCH = { w: 50, h: 28, gap: 8, label: 14 } as const;
-const GRID_TOP = PANEL.pad + 22 + 10;
-
-const swatchCenter = (index: number) => {
- const row = Math.floor(index / 3);
- const col = index % 3;
- return {
- x:
- PANEL_POS.left +
- (PANEL.pad + col * (SWATCH.w + SWATCH.gap) + SWATCH.w / 2) * S,
- y:
- PANEL_POS.top +
- (GRID_TOP + row * (SWATCH.h + SWATCH.label + SWATCH.gap) + SWATCH.h / 2) *
- S,
- };
-};
-
-const PATH: Way[] = [
- { t: 0, x: 300, y: 330 },
- ...STOPS.flatMap((stop) => {
- const point = swatchCenter(GRADES.findIndex((g) => g.name === stop.name));
- return [
- { t: stop.at - 220, ...point },
- { t: stop.at, ...point, click: true },
- ];
- }),
- { t: DURATION - 700, x: 290, y: 330 },
- { t: DURATION, x: 290, y: 330 },
-];
-
-const gradeAt = (t: number) => {
- let current = "None";
- for (const stop of STOPS) if (t >= stop.at) current = stop.name;
- return current;
-};
-
-const wipeAt = (t: number) => {
- let latest: (typeof STOPS)[number] | undefined;
- for (const stop of STOPS) if (t >= stop.at) latest = stop;
- if (!latest) return 0;
- return easeOut(span(t, latest.at, latest.at + SWEEP)) * WIPE_MAX;
-};
-
-const Scene = ({ grade }: { grade: Grade }) => (
- <span
- className="relative block overflow-hidden rounded-md"
- style={{
- width: SWATCH.w,
- height: SWATCH.h,
- background:
- "linear-gradient(160deg, #f7c27a 0%, #e98a9c 45%, #5a7fd8 100%)",
- filter: grade.filter,
- }}
- >
- <span
- className="absolute rounded-full"
- style={{
- left: 8,
- top: 6,
- width: 9,
- height: 9,
- background: "#fff4d6",
- boxShadow: "0 0 6px rgba(255,244,214,0.9)",
- }}
- />
- <span
- className="absolute inset-x-0 bottom-0"
- style={{ height: 9, background: "rgba(26,32,64,0.55)" }}
- />
- <span
- className="absolute inset-0"
- style={{ background: grade.tint, mixBlendMode: "multiply" }}
- />
- </span>
-);
-
-export const Visual = ({ playing }: { playing: boolean }) => {
- const rootRef = useRef<HTMLDivElement | null>(null);
- const gradedRef = useRef<HTMLDivElement | null>(null);
- const lineRef = useRef<HTMLDivElement | null>(null);
- const [ui, setUi] = useSceneState({ grade: "None" });
- const cursor = useCursor(rootRef);
-
- useLoop({
- duration: DURATION,
- playing,
- pose: 4600,
- tick: (t, seek) => {
- setUi({ grade: gradeAt(t) });
- const wipe = wipeAt(t);
- if (gradedRef.current) {
- gradedRef.current.style.clipPath = `inset(0 ${(1 - wipe) * 100}% 0 0)`;
- }
- if (lineRef.current) {
- lineRef.current.style.transform = `translateX(${wipe * CANVAS.w}px)`;
- lineRef.current.style.opacity = wipe > 0.005 ? "1" : "0";
- }
- cursor.tick(PATH, t, seek);
- },
- });
-
- const grade = GRADES.find((item) => item.name === ui.grade) ?? GRADES[0];
- const graded = grade?.name !== "None";
-
- return (
- <div ref={rootRef} className="relative">
- <Ground tone="gradient">
- <RecordedWindow left={WIN.left} top={WIN.top} scale={WIN.scale} />
-
- <div
- ref={gradedRef}
- className="pointer-events-none absolute inset-0 transition-[filter] duration-500"
- style={{
- clipPath: "inset(0 100% 0 0)",
- filter: grade?.filter,
- }}
- >
- <div
- className="absolute inset-0"
- style={{ background: GROUND_GRADIENT }}
- />
- <RecordedWindow left={WIN.left} top={WIN.top} scale={WIN.scale} />
- <div
- className="absolute inset-0 transition-[background-color] duration-500"
- style={{ background: grade?.tint, mixBlendMode: "multiply" }}
- />
- <div
- className="absolute inset-0"
- style={{
- background:
- "radial-gradient(85% 75% at 50% 50%, rgba(9,12,20,0) 55%, rgba(9,12,20,0.45) 100%)",
- }}
- />
- </div>
-
- <div
- ref={lineRef}
- className="pointer-events-none absolute inset-y-0 left-0 z-20 w-px opacity-0 transition-opacity duration-300 will-change-transform"
- style={{
- background: "rgba(255,255,255,0.95)",
- boxShadow: "0 0 0 1px rgba(0,0,0,0.12)",
- }}
- >
- <span
- className="absolute left-1/2 top-1/2 flex size-7 -translate-x-1/2 -translate-y-1/2 items-center justify-center rounded-full"
- style={{
- background: "#ffffff",
- boxShadow: "0 4px 14px rgba(0,0,0,0.25)",
- }}
- >
- <svg
- aria-hidden="true"
- viewBox="0 0 16 16"
- className="size-3"
- fill="none"
- stroke={ED.text1}
- strokeWidth="1.6"
- strokeLinecap="round"
- strokeLinejoin="round"
- >
- <path d="M6 4 2 8l4 4M10 4l4 4-4 4" />
- </svg>
- </span>
- </div>
-
- <div
- className="absolute z-20 flex h-7 items-center gap-1.5 rounded-lg px-2.5 text-[12px] font-medium transition-opacity duration-300"
- style={{
- left: 20,
- top: 10,
- background: ED.card,
- color: ED.text1,
- boxShadow: ED.popShadow,
- opacity: graded ? 1 : 0,
- }}
- >
- <span
- className="size-2 rounded-full"
- style={{ background: ED.accent }}
- />
- {grade?.name}
- </div>
-
- <Panel
- className="z-30 gap-2.5"
- style={{
- left: PANEL_POS.left,
- top: PANEL_POS.top,
- width: PANEL.w,
- padding: PANEL.pad,
- transform: `scale(${S})`,
- transformOrigin: "top left",
- }}
- >
- <SectionTitle>Color correction</SectionTitle>
- <div
- className="grid grid-cols-3"
- style={{ gap: SWATCH.gap, rowGap: SWATCH.gap }}
- >
- {GRADES.map((item) => {
- const selected = ui.grade === item.name;
- return (
- <span
- key={item.name}
- className="flex flex-col items-start gap-0.5"
- >
- <span
- className={classNames(
- "rounded-md transition-shadow duration-200",
- selected && "ring-2 ring-offset-2",
- )}
- style={{
- boxShadow: selected
- ? undefined
- : `0 0 0 0.5px ${ED.line}`,
- ["--tw-ring-color" as string]: ED.accent,
- ["--tw-ring-offset-color" as string]: ED.card,
- }}
- >
- <Scene grade={item} />
- </span>
- <span
- className="text-[10px] leading-[14px]"
- style={{ color: selected ? ED.text1 : ED.text2 }}
- >
- {item.name}
- </span>
- </span>
- );
- })}
- </div>
- <Field label="Grain" value="0%">
- <Slider fill={0} />
- </Field>
- </Panel>
- {cursor.Cursor}
- </Ground>
- </div>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/studio/cards/mask.tsx b/apps/web/components/pages/HomeTwo/studio/cards/mask.tsx
deleted file mode 100644
index 50eaf7df3..000000000
--- a/apps/web/components/pages/HomeTwo/studio/cards/mask.tsx
+++ /dev/null
@@ -1,194 +0,0 @@
-"use client";
-
-import { useRef } from "react";
-import {
- easeOut,
- lerp,
- span,
- useCursor,
- useSceneState,
- type Way,
-} from "../../scenes/engine";
-import { ED, Ground, RecordedWindow, Segmented, useLoop } from "../shared";
-
-const WIN = { left: 40, top: 44, scale: 1.45 };
-const MASK = {
- x: WIN.left + Math.round(72 * WIN.scale),
- y: WIN.top + Math.round(112 * WIN.scale),
- w: Math.round(268 * WIN.scale),
- h: Math.round(52 * WIN.scale),
-};
-const DURATION = 8200;
-const DRAG = { start: 600, end: 1700 };
-const BLUR_AT = 1900;
-const PIXELATE_AT = 3600;
-const HIGHLIGHT_AT = 5400;
-const KIND = ["Sensitive", "Highlight"] as const;
-const EFFECT = ["Blur", "Pixelate"] as const;
-const ITEM_W = 78;
-const CARD = { x: MASK.x, y: MASK.y + MASK.h + 12, pad: 8, gap: 12 };
-const kindCenter = (i: number) => ({
- x: CARD.x + CARD.pad + 2 + ITEM_W * (i + 0.5),
- y: CARD.y + CARD.pad + 2 + 13,
-});
-const EFFECT_X = CARD.x + CARD.pad + ITEM_W * 2 + 4 + CARD.gap + 46;
-const effectCenter = (i: number) => ({
- x: EFFECT_X + 2 + ITEM_W * (i + 0.5),
- y: CARD.y + CARD.pad + 2 + 13,
-});
-
-const PATH: Way[] = [
- { t: 0, x: MASK.x - 40, y: MASK.y - 30 },
- { t: DRAG.start, x: MASK.x, y: MASK.y },
- { t: DRAG.end, x: MASK.x + MASK.w, y: MASK.y + MASK.h },
- { t: BLUR_AT, x: MASK.x + MASK.w, y: MASK.y + MASK.h },
- { t: 3400, ...effectCenter(1) },
- { t: PIXELATE_AT, ...effectCenter(1), click: true },
- { t: 5200, ...kindCenter(1) },
- { t: HIGHLIGHT_AT, ...kindCenter(1), click: true },
- { t: 6600, x: MASK.x + MASK.w + 40, y: CARD.y + 70 },
- { t: DURATION, x: MASK.x + MASK.w + 40, y: CARD.y + 70 },
-];
-
-const PIXELS = Array.from({ length: 22 * 4 }, (_, i) => {
- const seed = Math.sin(i * 12.9898 + 4.1414) * 43758.5453;
- const v = seed - Math.floor(seed);
- const l = 62 + Math.floor(v * 30);
- return `hsl(216 22% ${l}%)`;
-});
-
-const HANDLES = [
- [0, 0],
- [0.5, 0],
- [1, 0],
- [0, 0.5],
- [1, 0.5],
- [0, 1],
- [0.5, 1],
- [1, 1],
-] as const;
-
-const uiAt = (t: number) => ({
- drawing: t >= DRAG.start && t < BLUR_AT,
- kind: t >= HIGHLIGHT_AT ? "Highlight" : t >= BLUR_AT ? "Sensitive" : null,
- effect: t >= PIXELATE_AT ? "Pixelate" : "Blur",
-});
-
-export const Visual = ({ playing }: { playing: boolean }) => {
- const rootRef = useRef<HTMLDivElement | null>(null);
- const boxRef = useRef<HTMLDivElement | null>(null);
- const [ui, setUi] = useSceneState(uiAt(0));
- const cursor = useCursor(rootRef);
-
- useLoop({
- duration: DURATION,
- playing,
- pose: 4400,
- tick: (t, seek) => {
- setUi(uiAt(t));
- const drag = easeOut(span(t, DRAG.start, DRAG.end));
- if (boxRef.current) {
- boxRef.current.style.width = `${lerp(0, MASK.w, drag)}px`;
- boxRef.current.style.height = `${lerp(0, MASK.h, drag)}px`;
- boxRef.current.style.opacity = t >= DRAG.start ? "1" : "0";
- }
- cursor.tick(PATH, t, seek);
- },
- });
-
- const applied = ui.kind !== null;
- const highlight = ui.kind === "Highlight";
- const blur = applied && !highlight && ui.effect === "Blur";
- const pixelate = applied && !highlight && ui.effect === "Pixelate";
-
- return (
- <div ref={rootRef} className="relative">
- <Ground tone="light">
- <RecordedWindow left={WIN.left} top={WIN.top} scale={WIN.scale} />
-
- <div
- ref={boxRef}
- className="pointer-events-none absolute z-20 overflow-hidden rounded-md"
- style={{
- left: MASK.x,
- top: MASK.y,
- width: 0,
- height: 0,
- opacity: 0,
- border: `2px ${applied ? "solid" : "dashed"} #202020`,
- boxShadow: highlight
- ? "0 0 0 9999px rgba(9,12,20,0.5)"
- : "0 0 0 9999px rgba(9,12,20,0)",
- transition: "box-shadow 420ms ease",
- }}
- >
- <div
- className="absolute inset-0 transition-opacity duration-300"
- style={{
- opacity: blur ? 1 : 0,
- backdropFilter: "blur(10px) saturate(1.2)",
- WebkitBackdropFilter: "blur(10px) saturate(1.2)",
- background: "rgba(236,240,246,0.35)",
- }}
- />
- <div
- className="absolute inset-0 grid transition-opacity duration-300"
- style={{
- opacity: pixelate ? 1 : 0,
- gridTemplateColumns: "repeat(22, 1fr)",
- gridTemplateRows: "repeat(4, 1fr)",
- }}
- >
- {PIXELS.map((color, i) => (
- <span key={`${i}-${color}`} style={{ background: color }} />
- ))}
- </div>
- </div>
-
- {HANDLES.map(([fx, fy]) => (
- <span
- key={`${fx}-${fy}`}
- className="pointer-events-none absolute z-30 size-3 rounded-full border border-white transition-opacity duration-300"
- style={{
- left: MASK.x + MASK.w * fx - 6,
- top: MASK.y + MASK.h * fy - 6,
- background: "#202020",
- opacity: applied ? 1 : 0,
- }}
- />
- ))}
-
- <div
- className="absolute z-30 flex items-center rounded-xl transition-[opacity,transform] duration-300"
- style={{
- left: CARD.x,
- top: CARD.y,
- padding: CARD.pad,
- gap: CARD.gap,
- background: ED.card,
- boxShadow: ED.popShadow,
- opacity: applied ? 1 : 0,
- transform: applied ? "translateY(0)" : "translateY(6px)",
- }}
- >
- <Segmented options={KIND} value={ui.kind} itemWidth={ITEM_W} />
- <div className="flex items-center gap-2">
- <span
- className="text-[12px] font-medium"
- style={{ color: ED.text2 }}
- >
- Effect
- </span>
- <Segmented
- options={EFFECT}
- value={ui.effect}
- itemWidth={ITEM_W}
- disabled={highlight}
- />
- </div>
- </div>
- {cursor.Cursor}
- </Ground>
- </div>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/studio/cards/scenes.tsx b/apps/web/components/pages/HomeTwo/studio/cards/scenes.tsx
deleted file mode 100644
index 83fdf1569..000000000
--- a/apps/web/components/pages/HomeTwo/studio/cards/scenes.tsx
+++ /dev/null
@@ -1,302 +0,0 @@
-"use client";
-
-import { useRef } from "react";
-import { LucideVideo } from "../../demo/capIcons";
-import { useVideoAttrs, VIDEO_POSTERS } from "../../demo/media";
-import {
- useCursor,
- useSceneState,
- useVideo,
- type Way,
-} from "../../scenes/engine";
-import {
- CANVAS,
- ED,
- Field,
- Ground,
- HUE,
- Lane,
- Panel,
- Playhead,
- RecordedWindow,
- Segment,
- Tile,
- TRACK_GUTTER,
- TRACK_H,
- TrackLabel,
- useLoop,
-} from "../shared";
-
-const MODES = [
- "Default",
- "Camera Only",
- "Hide Camera",
- "Split Screen",
- "Floating",
-] as const;
-type Mode = (typeof MODES)[number];
-const SEQUENCE: Mode[] = ["Default", "Camera Only", "Split Screen", "Floating"];
-const SLOT = 3000;
-const DURATION = SLOT * SEQUENCE.length;
-
-const FRAME = { left: 16, top: 48, w: 306, h: 172 };
-const SCREEN = { w: 278, h: 150 };
-const PANEL = { left: 338, top: 44, w: 196, scale: 1.25 };
-
-type Box = {
- left: number;
- top: number;
- w: number;
- h: number;
- r: number;
- opacity: number;
-};
-
-const LAYOUT: Record<Mode, { screen: Box & { scale: number }; camera: Box }> = {
- Default: {
- screen: { left: 14, top: 11, w: 278, h: 150, r: 8, opacity: 1, scale: 1 },
- camera: { left: 12, top: 80, w: 80, h: 80, r: 40, opacity: 1 },
- },
- "Camera Only": {
- screen: { left: 14, top: 11, w: 278, h: 150, r: 8, opacity: 0, scale: 1 },
- camera: { left: 0, top: 0, w: FRAME.w, h: FRAME.h, r: 8, opacity: 1 },
- },
- "Hide Camera": {
- screen: { left: 14, top: 11, w: 278, h: 150, r: 8, opacity: 1, scale: 1 },
- camera: { left: 12, top: 80, w: 80, h: 80, r: 40, opacity: 0 },
- },
- "Split Screen": {
- screen: {
- left: 8,
- top: 22,
- w: 145,
- h: 128,
- r: 8,
- opacity: 1,
- scale: 128 / SCREEN.h,
- },
- camera: { left: 153, top: 22, w: 145, h: 128, r: 8, opacity: 1 },
- },
- Floating: {
- screen: {
- left: 10,
- top: 35,
- w: 190,
- h: 102,
- r: 8,
- opacity: 1,
- scale: 190 / SCREEN.w,
- },
- camera: { left: 206, top: 40, w: 92, h: 92, r: 14, opacity: 1 },
- },
-};
-
-const EASE = "cubic-bezier(0.22, 1, 0.36, 1)";
-const MOVE = `left 480ms ${EASE}, top 480ms ${EASE}, width 480ms ${EASE}, height 480ms ${EASE}, border-radius 480ms ${EASE}, opacity 320ms ease, transform 480ms ${EASE}`;
-
-const tileCenter = (mode: Mode) => {
- const i = MODES.indexOf(mode);
- const col = i % 2;
- const row = Math.floor(i / 2);
- const x = 16 + col * 86 + 39;
- const y = 43.5 + row * 38 + 15;
- return { x: PANEL.left + x * PANEL.scale, y: PANEL.top + y * PANEL.scale };
-};
-
-const PATH: Way[] = [
- { t: 0, ...tileCenter("Default") },
- { t: 150, ...tileCenter("Default"), click: true },
- { t: 1200, ...tileCenter("Default") },
- { t: SLOT - 300, ...tileCenter("Camera Only") },
- { t: SLOT, ...tileCenter("Camera Only"), click: true },
- { t: SLOT + 1200, ...tileCenter("Camera Only") },
- { t: SLOT * 2 - 300, ...tileCenter("Split Screen") },
- { t: SLOT * 2, ...tileCenter("Split Screen"), click: true },
- { t: SLOT * 2 + 1200, ...tileCenter("Split Screen") },
- { t: SLOT * 3 - 300, ...tileCenter("Floating") },
- { t: SLOT * 3, ...tileCenter("Floating"), click: true },
- { t: SLOT * 3 + 1200, ...tileCenter("Floating") },
- { t: DURATION - 300, ...tileCenter("Default") },
- { t: DURATION, ...tileCenter("Default") },
-];
-
-const modeAt = (t: number): Mode =>
- SEQUENCE[Math.min(SEQUENCE.length - 1, Math.floor(t / SLOT))] ?? "Default";
-
-const S = 1.15;
-const LOGICAL = { w: CANVAS.w / S, h: CANVAS.h / S };
-const STRIP = { left: 12, top: 254, pad: 8 };
-const STRIP_W = LOGICAL.w - STRIP.left * 2;
-const LANE_W = STRIP_W - STRIP.pad * 2 - TRACK_GUTTER;
-
-const CameraPane = ({ playing, box }: { playing: boolean; box: Box }) => {
- const ref = useRef<HTMLVideoElement | null>(null);
- const attrs = useVideoAttrs(VIDEO_POSTERS.webcam);
- useVideo(playing, ref);
- return (
- <div
- className="absolute overflow-hidden bg-[#111111]"
- style={{
- left: box.left,
- top: box.top,
- width: box.w,
- height: box.h,
- borderRadius: box.r,
- opacity: box.opacity,
- boxShadow: "0 14px 34px rgba(0,0,0,0.28)",
- transition: MOVE,
- }}
- >
- <video
- ref={ref}
- className="h-full w-full object-cover"
- src="/videos/home-two/webcam.mp4"
- muted
- loop
- playsInline
- {...attrs}
- />
- </div>
- );
-};
-
-export const Visual = ({ playing }: { playing: boolean }) => {
- const rootRef = useRef<HTMLDivElement | null>(null);
- const playheadRef = useRef<HTMLDivElement | null>(null);
- const [mode, setMode] = useSceneState<Mode>("Default");
- const cursor = useCursor(rootRef);
-
- useLoop({
- duration: DURATION,
- playing,
- pose: 7000,
- tick: (t, seek) => {
- setMode(modeAt(t));
- if (playheadRef.current) {
- playheadRef.current.style.transform = `translateX(${(t / DURATION) * LANE_W}px)`;
- }
- cursor.tick(PATH, t, seek);
- },
- });
-
- const layout = LAYOUT[mode];
-
- return (
- <div ref={rootRef} className="relative">
- <Ground tone="light">
- <div
- className="absolute overflow-hidden rounded-lg"
- style={{
- left: FRAME.left,
- top: FRAME.top,
- width: FRAME.w,
- height: FRAME.h,
- background:
- "linear-gradient(135deg, #C3DCF8 0%, #DACBF9 55%, #F6D9EC 100%)",
- boxShadow:
- "0 12px 32px -8px rgba(0,0,0,0.25), 0 0 0 0.5px rgba(0,0,0,0.12)",
- }}
- >
- <div
- className="absolute overflow-hidden"
- style={{
- left: layout.screen.left,
- top: layout.screen.top,
- width: layout.screen.w,
- height: layout.screen.h,
- borderRadius: layout.screen.r,
- opacity: layout.screen.opacity,
- transform: layout.screen.opacity ? "scale(1)" : "scale(0.94)",
- boxShadow: "0 18px 40px rgba(0,0,0,0.22)",
- transition: MOVE,
- }}
- >
- <RecordedWindow
- left={0}
- top={0}
- width={SCREEN.w}
- height={SCREEN.h}
- scale={layout.screen.scale}
- style={{ transition: `transform 480ms ${EASE}` }}
- />
- </div>
- <CameraPane playing={playing} box={layout.camera} />
- </div>
-
- <div
- className="absolute"
- style={{
- left: PANEL.left,
- top: PANEL.top,
- width: PANEL.w,
- transform: `scale(${PANEL.scale})`,
- transformOrigin: "top left",
- }}
- >
- <Panel className="left-0 top-0 w-full">
- <Field label="Camera Layout">
- <div className="grid grid-cols-2 gap-2">
- {MODES.map((item) => (
- <Tile
- key={item}
- selected={mode === item}
- className="overflow-hidden px-1"
- >
- {item}
- </Tile>
- ))}
- </div>
- </Field>
- </Panel>
- </div>
-
- <div
- className="absolute left-0 top-0"
- style={{
- width: LOGICAL.w,
- height: LOGICAL.h,
- transform: `scale(${S})`,
- transformOrigin: "top left",
- }}
- >
- <div
- className="absolute flex rounded-xl"
- style={{
- left: STRIP.left,
- top: STRIP.top,
- width: STRIP_W,
- padding: STRIP.pad,
- background: ED.card,
- boxShadow: ED.cardShadow,
- }}
- >
- <TrackLabel
- hue={HUE.scene}
- icon={<LucideVideo className="size-3.5" />}
- >
- Scene
- </TrackLabel>
- <Lane>
- {SEQUENCE.map((item, i) => (
- <Segment
- key={item}
- hue={HUE.scene}
- label={item}
- selected={mode === item}
- style={{
- left: `calc(${(i / SEQUENCE.length) * 100}% + ${i ? 3 : 0}px)`,
- width: `calc(${100 / SEQUENCE.length}% - 3px)`,
- }}
- />
- ))}
- <Playhead playheadRef={playheadRef} top={-6} bottom={0} />
- </Lane>
- </div>
- </div>
- {cursor.Cursor}
- </Ground>
- </div>
- );
-};
-
-export const SCENE_TRACK_H = TRACK_H;
diff --git a/apps/web/components/pages/HomeTwo/studio/cards/text.tsx b/apps/web/components/pages/HomeTwo/studio/cards/text.tsx
deleted file mode 100644
index 0f445b305..000000000
--- a/apps/web/components/pages/HomeTwo/studio/cards/text.tsx
+++ /dev/null
@@ -1,256 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import { useRef } from "react";
-import { easeOut, lerp, span, typed, useSceneState } from "../../scenes/engine";
-import { Chip, ED, Ground, RecordedWindow, useLoop } from "../shared";
-
-const WIN = { left: 120, top: 12, scale: 1 };
-const DURATION = 10000;
-const LOWER = { enter: 400, exit: 2900 };
-const STAT = { enter: 3700, exit: 6100 };
-const TYPE = { enter: 6600, exit: 9400 };
-const TYPEWRITER_TEXT = "Try the new flow →";
-
-const TEMPLATES = [
- "Title",
- "Lower Third",
- "Kicker",
- "Big Stat",
- "Quote",
- "Typewriter",
-] as const;
-type Template = (typeof TEMPLATES)[number];
-
-const ROW = { left: 16, top: 282, scale: 1.2, card: 73, gap: 8 };
-
-const activeAt = (t: number): { template: Template; anim: string } | null =>
- t >= TYPE.enter - 100 && t < TYPE.exit + 300
- ? { template: "Typewriter", anim: "Typewriter" }
- : t >= STAT.enter - 100 && t < STAT.exit + 300
- ? { template: "Big Stat", anim: "Pop" }
- : t >= LOWER.enter - 100 && t < LOWER.exit + 300
- ? { template: "Lower Third", anim: "Slide up" }
- : null;
-
-const Sample = ({ template }: { template: Template }) => {
- switch (template) {
- case "Title":
- return <span className="text-[16px] font-bold text-white">Title</span>;
- case "Lower Third":
- return (
- <span className="flex items-stretch gap-1.5">
- <span className="w-[2px] rounded-full bg-[#8FC1F7]" />
- <span className="whitespace-nowrap text-[10px] font-semibold leading-none text-white">
- Lower third
- </span>
- </span>
- );
- case "Kicker":
- return (
- <span className="text-[9px] font-bold uppercase tracking-[3px] text-white">
- Kicker
- </span>
- );
- case "Big Stat":
- return <span className="text-[22px] font-extrabold text-white">42%</span>;
- case "Quote":
- return (
- <span className="font-serif text-[13px] font-medium italic text-white">
- “Quote”
- </span>
- );
- case "Typewriter":
- return (
- <span className="font-mono text-[10px] text-white">
- Typewriter<span className="animate-pulse">_</span>
- </span>
- );
- }
-};
-
-const Selection = ({
- children,
- className,
- style,
- boxRef,
-}: {
- children: React.ReactNode;
- className?: string;
- style?: React.CSSProperties;
- boxRef?: React.RefObject<HTMLDivElement | null>;
-}) => (
- <div
- ref={boxRef}
- className={classNames("absolute z-20 rounded-md", className)}
- style={{
- border: `2px solid ${ED.accent}`,
- padding: "8px 12px",
- ...style,
- }}
- >
- <span
- className="absolute left-0 rounded px-1.5 py-0.5 text-[11px] font-medium text-white"
- style={{ top: -24, background: ED.accent }}
- >
- Text
- </span>
- {[
- { left: -7, top: -7 },
- { right: -7, top: -7 },
- { left: -7, bottom: -7 },
- { right: -7, bottom: -7 },
- ].map((dot) => (
- <span
- key={Object.keys(dot).join("-")}
- className="absolute size-3 rounded-full"
- style={{
- ...dot,
- background: ED.accent,
- boxShadow: "0 0 0 2px #ffffff",
- }}
- />
- ))}
- {children}
- </div>
-);
-
-export const Visual = ({ playing }: { playing: boolean }) => {
- const lowerRef = useRef<HTMLDivElement | null>(null);
- const statRef = useRef<HTMLDivElement | null>(null);
- const typeRef = useRef<HTMLDivElement | null>(null);
- const typeTextRef = useRef<HTMLSpanElement | null>(null);
- const [ui, setUi] = useSceneState(activeAt(0));
- const lastRef = useRef<{ template: Template; anim: string }>({
- template: "Lower Third",
- anim: "Slide up",
- });
- if (ui) lastRef.current = ui;
-
- useLoop({
- duration: DURATION,
- playing,
- pose: 4800,
- tick: (t) => {
- setUi(activeAt(t));
- if (lowerRef.current) {
- const inF = easeOut(span(t, LOWER.enter, LOWER.enter + 480));
- const outF = easeOut(span(t, LOWER.exit, LOWER.exit + 360));
- lowerRef.current.style.opacity = `${inF * (1 - outF)}`;
- lowerRef.current.style.transform = `translateY(${lerp(28, 0, inF) + outF * 18}px)`;
- }
- if (statRef.current) {
- const inF = span(t, STAT.enter, STAT.enter + 520);
- const overshoot = 1 + Math.sin(inF * Math.PI) * 0.12;
- const outF = easeOut(span(t, STAT.exit, STAT.exit + 320));
- statRef.current.style.opacity = `${easeOut(inF) * (1 - outF)}`;
- statRef.current.style.transform = `translateX(-50%) scale(${lerp(0.6, 1, easeOut(inF)) * overshoot})`;
- }
- if (typeRef.current && typeTextRef.current) {
- const outF = easeOut(span(t, TYPE.exit, TYPE.exit + 300));
- typeRef.current.style.opacity = `${t >= TYPE.enter ? 1 - outF : 0}`;
- typeTextRef.current.textContent =
- t >= TYPE.enter
- ? typed(TYPEWRITER_TEXT, t, TYPE.enter + 200, 22)
- : "";
- }
- },
- });
-
- const shown = ui ?? lastRef.current;
-
- return (
- <Ground tone="gradient">
- <RecordedWindow left={WIN.left} top={WIN.top} scale={WIN.scale} />
- <div
- className="pointer-events-none absolute rounded-[10px]"
- style={{
- left: WIN.left,
- top: WIN.top,
- width: 360,
- height: 250,
- background: "rgba(9,12,20,0.22)",
- }}
- />
-
- <Selection
- boxRef={lowerRef}
- className="opacity-0"
- style={{ left: 138, top: 186 }}
- >
- <span className="flex items-stretch gap-3">
- <span className="w-[3px] rounded-full bg-[#8FC1F7]" />
- <span className="leading-tight [text-shadow:0_2px_12px_rgba(0,0,0,0.45)]">
- <span className="block text-[22px] font-semibold text-white">
- Sofia Chen
- </span>
- <span className="block text-[13px] font-medium text-white/85">
- Head of Product
- </span>
- </span>
- </span>
- </Selection>
-
- <Selection
- boxRef={statRef}
- className="left-1/2 flex -translate-x-1/2 flex-col items-center opacity-0"
- style={{ top: 78, background: "rgba(17,17,17,0.72)" }}
- >
- <span className="text-[68px] font-extrabold leading-none tracking-[-0.04em] text-white [text-shadow:0_6px_30px_rgba(0,0,0,0.45)]">
- 3.2×
- </span>
- <span className="mt-2 text-[13px] font-medium uppercase tracking-[0.08em] text-white/85">
- faster onboarding
- </span>
- </Selection>
-
- <Selection
- boxRef={typeRef}
- className="opacity-0"
- style={{ left: 286, top: 52, background: "rgba(17,17,17,0.82)" }}
- >
- <span className="font-mono text-[17px] font-medium text-white">
- <span ref={typeTextRef} />
- <span className="ml-0.5 inline-block h-[16px] w-[2px] translate-y-[2px] animate-pulse bg-white/85" />
- </span>
- </Selection>
-
- <div className="absolute left-4 top-3.5 z-30">
- <Chip>In · {shown.anim}</Chip>
- </div>
-
- <div
- className="absolute flex"
- style={{
- left: ROW.left,
- top: ROW.top,
- gap: ROW.gap,
- transform: `scale(${ROW.scale})`,
- transformOrigin: "top left",
- }}
- >
- {TEMPLATES.map((template) => {
- const on = shown.template === template;
- return (
- <div
- key={template}
- className="relative flex h-16 shrink-0 items-center justify-center overflow-hidden rounded-lg pb-3 transition-[box-shadow] duration-300"
- style={{
- width: ROW.card,
- background: "linear-gradient(135deg,#17181c,#2a2c33)",
- boxShadow: on
- ? `0 0 0 1px ${ED.accent}, 0 0 0 3px ${ED.card}, 0 0 0 4px ${ED.accent}`
- : `0 0 0 1px rgba(255,255,255,0.08)`,
- }}
- >
- <Sample template={template} />
- <span className="absolute inset-x-0 bottom-1 text-center text-[10px] text-white/50">
- {template}
- </span>
- </div>
- );
- })}
- </div>
- </Ground>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/studio/cards/three-d.tsx b/apps/web/components/pages/HomeTwo/studio/cards/three-d.tsx
deleted file mode 100644
index e695c863f..000000000
--- a/apps/web/components/pages/HomeTwo/studio/cards/three-d.tsx
+++ /dev/null
@@ -1,226 +0,0 @@
-"use client";
-
-import { useRef } from "react";
-import { easeInOut, lerp, span, useSceneState } from "../../scenes/engine";
-import { CANVAS, Ground, HUE, mix, RecordedWindow, useLoop } from "../shared";
-
-const DARK = {
- card: "#1b1b1e",
- line: "rgba(255,255,255,0.08)",
- text1: "#f4f4f5",
- text2: "#9a9aa3",
- ctl: "rgba(255,255,255,0.06)",
- accent: "#0a84ff",
-} as const;
-
-const DURATION = 9000;
-const MOVES = [
- {
- name: "Glide across",
- helper: "Move smoothly across the details",
- from: 0,
- to: 3000,
- },
- {
- name: "Unfold",
- helper: "Reveal your screen with a gentle tilt",
- from: 3000,
- to: 6000,
- },
- {
- name: "Pull back",
- helper: "Pull out to show the bigger picture",
- from: 6000,
- to: DURATION,
- },
-] as const;
-
-const WIN_SCALE = 0.85;
-const WIN = {
- w: 360 * WIN_SCALE,
- h: 250 * WIN_SCALE,
- left: (CANVAS.w - 360 * WIN_SCALE) / 2,
- top: 30,
-} as const;
-const S = 1.3;
-const ROW = { left: 24, top: 272, gap: 10 } as const;
-const ROW_W = (CANVAS.w - ROW.left * 2) / S;
-const CARD_W = (ROW_W - ROW.gap * 2) / 3;
-
-type Pose = { ry: number; rx: number; tx: number; s: number };
-
-const poseAt = (t: number): Pose => {
- if (t < 3000) {
- const f = easeInOut(span(t, 0, 3000));
- return { ry: lerp(-16, 16, f), rx: 0, tx: lerp(-26, 26, f), s: 1 };
- }
- if (t < 6000) {
- const f = easeInOut(span(t, 3000, 6000));
- const arc = Math.sin(f * Math.PI);
- return {
- ry: lerp(16, 0, f),
- rx: arc * 24,
- tx: lerp(26, 0, f),
- s: lerp(1, 0.97, arc),
- };
- }
- if (t < 8300) {
- const f = easeInOut(span(t, 6000, 8300));
- return {
- ry: lerp(0, -8, f),
- rx: lerp(0, 9, f),
- tx: lerp(0, -10, f),
- s: lerp(1, 0.8, f),
- };
- }
- const f = easeInOut(span(t, 8300, DURATION));
- return {
- ry: lerp(-8, -16, f),
- rx: lerp(9, 0, f),
- tx: lerp(-10, -26, f),
- s: lerp(0.8, 1, f),
- };
-};
-
-const moveAt = (t: number) =>
- MOVES.find((move) => t >= move.from && t < move.to)?.name ?? MOVES[0].name;
-
-export const Visual = ({ playing }: { playing: boolean }) => {
- const groupRef = useRef<HTMLDivElement | null>(null);
- const sideRef = useRef<HTMLDivElement | null>(null);
- const topRef = useRef<HTMLDivElement | null>(null);
- const floorRef = useRef<HTMLDivElement | null>(null);
- const [ui, setUi] = useSceneState({ move: moveAt(0) });
-
- useLoop({
- duration: DURATION,
- playing,
- pose: 700,
- tick: (t) => {
- setUi({ move: moveAt(t) });
- const pose = poseAt(t);
- const tilt = Math.min(1, Math.abs(pose.ry) / 24);
- const lean = Math.min(1, Math.abs(pose.rx) / 24);
- if (groupRef.current) {
- groupRef.current.style.transform = `translateX(${pose.tx}px) scale(${pose.s}) rotateY(${pose.ry}deg) rotateX(${pose.rx}deg)`;
- }
- if (sideRef.current) {
- sideRef.current.style.opacity = `${tilt * 0.55}`;
- sideRef.current.style.background =
- pose.ry > 0
- ? "linear-gradient(to right, rgba(9,12,20,0) 35%, rgba(9,12,20,0.8))"
- : "linear-gradient(to left, rgba(9,12,20,0) 35%, rgba(9,12,20,0.8))";
- }
- if (topRef.current) {
- topRef.current.style.opacity = `${lean * 0.5}`;
- }
- if (floorRef.current) {
- floorRef.current.style.transform = `translateX(${pose.tx * 1.15}px) scaleX(${0.7 + pose.s * 0.4}) scaleY(${pose.s})`;
- floorRef.current.style.opacity = `${0.5 + tilt * 0.3}`;
- }
- },
- });
-
- return (
- <Ground tone="dark">
- <div
- className="pointer-events-none absolute inset-0"
- style={{
- background:
- "radial-gradient(60% 52% at 50% 42%, rgba(99,102,241,0.16), rgba(99,102,241,0) 70%)",
- }}
- />
- <div
- ref={floorRef}
- className="pointer-events-none absolute rounded-full"
- style={{
- left: WIN.left + 30,
- top: WIN.top + WIN.h + 4,
- width: WIN.w - 60,
- height: 30,
- background: `radial-gradient(50% 50% at 50% 50%, ${mix(HUE.threeD, 45, "transparent")}, rgba(99,102,241,0))`,
- filter: "blur(10px)",
- opacity: 0.5,
- }}
- />
- <div
- className="absolute inset-0"
- style={{ perspective: 900, perspectiveOrigin: "50% 42%" }}
- >
- <div
- ref={groupRef}
- className="absolute inset-0 will-change-transform"
- style={{ transformStyle: "preserve-3d" }}
- >
- <RecordedWindow left={WIN.left} top={WIN.top} scale={WIN_SCALE} />
- <div
- ref={sideRef}
- className="pointer-events-none absolute rounded-[10px]"
- style={{
- left: WIN.left,
- top: WIN.top,
- width: WIN.w,
- height: WIN.h,
- opacity: 0,
- }}
- />
- <div
- ref={topRef}
- className="pointer-events-none absolute rounded-[10px]"
- style={{
- left: WIN.left,
- top: WIN.top,
- width: WIN.w,
- height: WIN.h,
- opacity: 0,
- background:
- "linear-gradient(to bottom, rgba(9,12,20,0.7), rgba(9,12,20,0) 55%)",
- }}
- />
- </div>
- </div>
-
- <div
- className="absolute flex"
- style={{
- left: ROW.left,
- top: ROW.top,
- width: ROW_W,
- gap: ROW.gap,
- transform: `scale(${S})`,
- transformOrigin: "top left",
- }}
- >
- {MOVES.map((move) => {
- const active = ui.move === move.name;
- return (
- <div
- key={move.name}
- className="flex flex-col gap-1 rounded-xl px-3 py-2.5 transition-[box-shadow,background-color] duration-300"
- style={{
- width: CARD_W,
- background: DARK.card,
- boxShadow: active
- ? `inset 0 0 0 1.5px ${DARK.accent}`
- : `inset 0 0 0 1px ${DARK.line}`,
- }}
- >
- <span
- className="whitespace-nowrap text-[12px] font-medium"
- style={{ color: DARK.text1 }}
- >
- {move.name}
- </span>
- <span
- className="text-[10.5px] leading-[1.3]"
- style={{ color: DARK.text2 }}
- >
- {move.helper}
- </span>
- </div>
- );
- })}
- </div>
- </Ground>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/studio/cards/zoom.tsx b/apps/web/components/pages/HomeTwo/studio/cards/zoom.tsx
deleted file mode 100644
index 7490e08ec..000000000
--- a/apps/web/components/pages/HomeTwo/studio/cards/zoom.tsx
+++ /dev/null
@@ -1,180 +0,0 @@
-"use client";
-
-import { useRef } from "react";
-import { LucideSearch } from "../../demo/capIcons";
-import {
- easeInOut,
- span,
- useCursor,
- useSceneState,
- type Way,
-} from "../../scenes/engine";
-import {
- CANVAS,
- ED,
- Field,
- Ground,
- HUE,
- Lane,
- Panel,
- Playhead,
- RecordedWindow,
- Segment,
- Segmented,
- Slider,
- TRACK_GUTTER,
- TrackLabel,
- useLoop,
-} from "../shared";
-
-const WIN = { left: 196, top: 18, scale: 1.05 };
-const FOCUS = { x: WIN.left + 300 * WIN.scale, y: WIN.top + 74 * WIN.scale };
-const FRAME_H = 292;
-const CLICK_AT = 1500;
-const ZOOM_IN = { start: 1600, end: 2500 };
-const ZOOM_OUT = { start: 5200, end: 6100 };
-const DURATION = 8000;
-const AMOUNT = 1.5;
-const PANEL = { left: 16, top: 18, w: 150, scale: 1.15 };
-
-const PATH: Way[] = [
- { t: 0, x: 330, y: 226 },
- { t: 1400, ...FOCUS },
- { t: CLICK_AT, ...FOCUS, click: true },
- { t: 3400, ...FOCUS },
- { t: 4600, x: FOCUS.x - 30, y: FOCUS.y + 44 },
- { t: DURATION, x: FOCUS.x - 30, y: FOCUS.y + 44 },
-];
-
-const scaleAt = (t: number) =>
- 1 +
- (AMOUNT - 1) *
- (easeInOut(span(t, ZOOM_IN.start, ZOOM_IN.end)) -
- easeInOut(span(t, ZOOM_OUT.start, ZOOM_OUT.end)));
-
-const S = 1.3;
-const LOGICAL = { w: CANVAS.w / S, h: CANVAS.h / S };
-const STRIP = { left: 12, top: 226, pad: 8 };
-const STRIP_W = LOGICAL.w - STRIP.left * 2;
-const LANE_W = STRIP_W - STRIP.pad * 2 - TRACK_GUTTER;
-const SEGMENT = { left: 0.16, width: 0.56 };
-
-export const Visual = ({ playing }: { playing: boolean }) => {
- const rootRef = useRef<HTMLDivElement | null>(null);
- const zoomRef = useRef<HTMLDivElement | null>(null);
- const playheadRef = useRef<HTMLDivElement | null>(null);
- const [ui, setUi] = useSceneState({ segment: false });
- const cursor = useCursor(rootRef);
-
- useLoop({
- duration: DURATION,
- playing,
- pose: 3600,
- tick: (t, seek) => {
- setUi({ segment: t >= CLICK_AT });
- if (zoomRef.current) {
- zoomRef.current.style.transform = `scale(${scaleAt(t)})`;
- }
- if (playheadRef.current) {
- playheadRef.current.style.transform = `translateX(${(t / DURATION) * LANE_W}px)`;
- }
- cursor.tick(PATH, t, seek);
- },
- });
-
- return (
- <div ref={rootRef} className="relative">
- <Ground tone="light">
- <div
- className="absolute inset-x-0 top-0 overflow-hidden"
- style={{ height: FRAME_H }}
- >
- <div
- ref={zoomRef}
- className="absolute inset-0 will-change-transform"
- style={{ transformOrigin: `${FOCUS.x}px ${FOCUS.y}px` }}
- >
- <RecordedWindow left={WIN.left} top={WIN.top} scale={WIN.scale} />
- </div>
- </div>
-
- <div
- className="absolute transition-[opacity,transform] duration-300"
- style={{
- left: PANEL.left,
- top: PANEL.top,
- width: PANEL.w,
- transform: `scale(${PANEL.scale}) translateY(${ui.segment ? 0 : 6}px)`,
- transformOrigin: "top left",
- opacity: ui.segment ? 1 : 0,
- }}
- >
- <Panel className="left-0 top-0 w-full gap-3">
- <Field label="Zoom 1" value="1.50x">
- <Slider fill={(AMOUNT - 1) / 3.5} />
- </Field>
- <Field label="Zoom Mode">
- <Segmented options={["Auto", "Manual"]} value="Auto" />
- </Field>
- </Panel>
- </div>
-
- <div
- className="absolute left-0 top-0"
- style={{
- width: LOGICAL.w,
- height: LOGICAL.h,
- transform: `scale(${S})`,
- transformOrigin: "top left",
- }}
- >
- <div
- className="absolute flex rounded-xl"
- style={{
- left: STRIP.left,
- top: STRIP.top,
- width: STRIP_W,
- padding: STRIP.pad,
- background: ED.card,
- boxShadow: ED.cardShadow,
- }}
- >
- <TrackLabel
- hue={HUE.zoom}
- icon={<LucideSearch className="size-3.5" />}
- >
- Zoom
- </TrackLabel>
- <Lane
- empty={
- ui.segment ? undefined : (
- <>
- <span>Generate zoom segments automatically</span>
- <span style={{ color: ED.text2 }}>· Generate</span>
- </>
- )
- }
- >
- <Segment
- hue={HUE.zoom}
- label="Automatic Zoom"
- sublabel="1.5x"
- selected
- className="transition-[opacity,transform] duration-300"
- style={{
- left: `${SEGMENT.left * 100}%`,
- width: `${SEGMENT.width * 100}%`,
- opacity: ui.segment ? 1 : 0,
- transform: ui.segment ? "scaleX(1)" : "scaleX(0.6)",
- transformOrigin: "left center",
- }}
- />
- <Playhead playheadRef={playheadRef} top={-6} bottom={0} />
- </Lane>
- </div>
- </div>
- {cursor.Cursor}
- </Ground>
- </div>
- );
-};
diff --git a/apps/web/components/pages/HomeTwo/studio/catalog.ts b/apps/web/components/pages/HomeTwo/studio/catalog.ts
deleted file mode 100644
index b1468cee7..000000000
--- a/apps/web/components/pages/HomeTwo/studio/catalog.ts
+++ /dev/null
@@ -1,67 +0,0 @@
-"use client";
-
-import dynamic from "next/dynamic";
-import type { StudioCard } from "./shared";
-
-const visual = (load: () => Promise<{ Visual: StudioCard["Visual"] }>) =>
- dynamic(() => load().then((module) => module.Visual));
-
-export const CARDS_A: StudioCard[] = [
- {
- key: "mask",
- title: "Blur what is private",
- body: "Drop a mask over a password, an email, or a face and choose blur or pixelate. Or flip it to a highlight that dims everything else.",
- Visual: visual(() => import("./cards/mask")),
- },
- {
- key: "scenes",
- title: "Scenes for screen and camera",
- body: "Switch any stretch of the timeline to camera only, hide camera, split screen, or floating cards, each with its own transition.",
- Visual: visual(() => import("./cards/scenes")),
- },
- {
- key: "text",
- title: "Text that animates",
- body: "Titles, lower thirds, big stats, and typewriter callouts as stackable tracks, with fade, slide, pop, or typewriter in and out.",
- Visual: visual(() => import("./cards/text")),
- },
- {
- key: "zoom",
- title: "Automatic zoom",
- body: "Generate zooms from your recorded clicks, or draw your own from 1x to 4.5x with a fixed focal point.",
- Visual: visual(() => import("./cards/zoom")),
- },
- {
- key: "captions",
- title: "Captions, generated locally",
- body: "Transcribe on your machine in 19 languages, fix the words, style them, and burn them in with the active word highlighted.",
- Visual: visual(() => import("./cards/captions")),
- },
-];
-
-export const CARDS_B: StudioCard[] = [
- {
- key: "three-d",
- title: "3D camera moves",
- body: "Tilt the frame into perspective and glide, sweep, or pull back across it with focus blur.",
- Visual: visual(() => import("./cards/three-d")),
- },
- {
- key: "canvas",
- title: "Any canvas, one recording",
- body: "Wallpapers, gradients, or your own image, then padding, corners, shadows, and a macOS, Windows, browser, or MacBook frame.",
- Visual: visual(() => import("./cards/canvas")),
- },
- {
- key: "grades",
- title: "Color grades",
- body: "Cinematic, Noir, Vintage, Frost, Golden, Midnight, Vivid, or Dreamy, then dial exposure, contrast, and vignette.",
- Visual: visual(() => import("./cards/grades")),
- },
- {
- key: "clips",
- title: "Clips, speed, and music",
- body: "Trim, split, reorder, speed up to 8x, crossfade between clips, and lay a track from the built in library underneath.",
- Visual: visual(() => import("./cards/clips")),
- },
-];
diff --git a/apps/web/components/pages/HomeTwo/studio/shared.tsx b/apps/web/components/pages/HomeTwo/studio/shared.tsx
deleted file mode 100644
index 352079e4c..000000000
--- a/apps/web/components/pages/HomeTwo/studio/shared.tsx
+++ /dev/null
@@ -1,742 +0,0 @@
-"use client";
-
-import { classNames } from "@cap/utils/helpers";
-import {
- type ComponentType,
- type CSSProperties,
- type ReactNode,
- type RefObject,
- useEffect,
- useRef,
-} from "react";
-import { ContentWindow } from "../demo/MacDesktop";
-import { useVideoAttrs, VIDEO_POSTERS } from "../demo/media";
-import { SCENE_CSS, useVideo } from "../scenes/engine";
-import { useReducedMotion } from "../visibility";
-
-export type StudioCard = {
- key: string;
- title: string;
- body: string;
- Visual: ComponentType<{ playing: boolean }>;
-};
-
-export const CANVAS = { w: 600, h: 375 } as const;
-
-// Mirrors the desktop editor's ed-* light tokens (apps/desktop/src/styles/theme.css) and
-// Timeline/styles.css; keep in sync when the editor theme changes.
-export const ED = {
- window: "#f1f1f3",
- card: "#ffffff",
- card2: "#f6f6f7",
- stage: "#e9e9ec",
- line: "rgba(0,0,0,0.075)",
- lineStrong: "rgba(0,0,0,0.12)",
- text1: "#1c1c1e",
- text2: "#6e6e75",
- text3: "#a0a0a8",
- ctl: "rgba(0,0,0,0.045)",
- ctlHover: "rgba(0,0,0,0.075)",
- ctlActive: "rgba(0,0,0,0.11)",
- accent: "#007aff",
- playhead: "#ff3b30",
- cardShadow: "0 1px 2px rgba(0,0,0,0.04), 0 0 0 1px rgba(0,0,0,0.075)",
- popShadow: "0 12px 32px -12px rgba(0,0,0,0.25), 0 0 0 1px rgba(0,0,0,0.075)",
-} as const;
-
-export const HUE = {
- clip: "#3b82f6",
- zoom: "#64748b",
- caption: "#0ea5e9",
- text: "#14b8a6",
- mask: "#ef4444",
- scene: "#8b5cf6",
- audio: "#22c55e",
- threeD: "#6366f1",
-} as const;
-
-export const mix = (hue: string, pct: number, base = "#ffffff") =>
- `color-mix(in srgb, ${hue} ${pct}%, ${base})`;
-
-export const useLoop = ({
- duration,
- playing,
- pose,
- tick,
-}: {
- duration: number;
- playing: boolean;
- pose: number;
- tick: (t: number, seek: boolean) => void;
-}) => {
- const tRef = useRef(0);
- const tickRef = useRef(tick);
- tickRef.current = tick;
- const reduced = useReducedMotion();
-
- useEffect(() => {
- if (reduced) {
- tickRef.current(pose, true);
- return;
- }
- if (!playing) return;
- let raf = 0;
- let last = performance.now();
- let seek = true;
- const frame = (now: number) => {
- const dt = Math.min(48, now - last);
- last = now;
- const next = tRef.current + dt;
- if (next >= duration) seek = true;
- tRef.current = next % duration;
- tickRef.current(tRef.current, seek);
- seek = false;
- raf = requestAnimationFrame(frame);
- };
- raf = requestAnimationFrame(frame);
- return () => cancelAnimationFrame(raf);
- }, [playing, reduced, duration, pose]);
-};
-
-export type Tone = "light" | "chrome" | "dark" | "gradient";
-
-const GROUND_BG: Record<Tone, string> = {
- light: "#F4F6F9",
- chrome: ED.window,
- dark: "#141518",
- gradient: "linear-gradient(135deg, #C3DCF8 0%, #DACBF9 55%, #F6D9EC 100%)",
-};
-
-export const Ground = ({
- tone = "light",
- className,
- style,
- children,
-}: {
- tone?: Tone;
- className?: string;
- style?: CSSProperties;
- children: ReactNode;
-}) => (
- <div
- className={classNames("relative overflow-hidden", className)}
- style={{
- width: CANVAS.w,
- height: CANVAS.h,
- background: GROUND_BG[tone],
- color: tone === "dark" ? "#ffffff" : ED.text1,
- ...style,
- }}
- >
- <style>{SCENE_CSS}</style>
- {children}
- </div>
-);
-
-export const RECORDED = {
- left: 120,
- top: 62,
- width: 360,
- height: 250,
-} as const;
-
-export const RecordedWindow = ({
- left = RECORDED.left,
- top = RECORDED.top,
- width = RECORDED.width,
- height = RECORDED.height,
- scale = 1,
- className,
- style,
-}: {
- left?: number;
- top?: number;
- width?: number;
- height?: number;
- scale?: number;
- className?: string;
- style?: CSSProperties;
-}) => {
- const scrollRef = useRef<HTMLDivElement | null>(null);
- return (
- <div
- className={classNames("absolute", className)}
- style={{
- left,
- top,
- width,
- height,
- transform: scale === 1 ? undefined : `scale(${scale})`,
- transformOrigin: "top left",
- ...style,
- }}
- >
- <ContentWindow width={width} height={height} scrollRef={scrollRef} />
- </div>
- );
-};
-
-export const CameraBubble = ({
- playing,
- size = 96,
- left = 24,
- top = CANVAS.h - 24 - 96,
- className,
- style,
-}: {
- playing: boolean;
- size?: number;
- left?: number;
- top?: number;
- className?: string;
- style?: CSSProperties;
-}) => {
- const ref = useRef<HTMLVideoElement | null>(null);
- const attrs = useVideoAttrs(VIDEO_POSTERS.webcam);
- useVideo(playing, ref);
- return (
- <div
- className={classNames(
- "absolute overflow-hidden rounded-full bg-[#111111]",
- className,
- )}
- style={{
- left,
- top,
- width: size,
- height: size,
- boxShadow: "0 10px 26px rgba(0,0,0,0.35)",
- ...style,
- }}
- >
- <video
- ref={ref}
- className="h-full w-full object-cover"
- src="/videos/home-two/webcam.mp4"
- muted
- loop
- playsInline
- {...attrs}
- />
- </div>
- );
-};
-
-export const Chip = ({
- children,
- className,
- style,
-}: {
- children: ReactNode;
- className?: string;
- style?: CSSProperties;
-}) => (
- <span
- className={classNames(
- "inline-flex h-7 items-center gap-1.5 rounded-lg px-2.5 text-[12px] font-medium",
- className,
- )}
- style={{
- background: ED.card,
- color: ED.text1,
- boxShadow: ED.popShadow,
- ...style,
- }}
- >
- {children}
- </span>
-);
-
-export const Panel = ({
- className,
- style,
- children,
-}: {
- className?: string;
- style?: CSSProperties;
- children: ReactNode;
-}) => (
- <div
- className={classNames(
- "absolute flex flex-col gap-3.5 rounded-xl p-4",
- className,
- )}
- style={{ background: ED.card, boxShadow: ED.popShadow, ...style }}
- >
- {children}
- </div>
-);
-
-export const SectionTitle = ({
- children,
- right,
-}: {
- children: ReactNode;
- right?: ReactNode;
-}) => (
- <div className="flex min-h-[22px] items-center justify-between">
- <span className="text-[12px] font-medium" style={{ color: ED.text2 }}>
- {children}
- </span>
- {right}
- </div>
-);
-
-export const Field = ({
- label,
- value,
- badge,
- disabled,
- children,
-}: {
- label: ReactNode;
- value?: ReactNode;
- badge?: ReactNode;
- disabled?: boolean;
- children?: ReactNode;
-}) => (
- <div className="flex flex-col gap-2">
- <div className="flex items-center gap-1.5">
- <span
- className="text-[13px] font-normal"
- style={{ color: disabled ? ED.text3 : ED.text1 }}
- >
- {label}
- </span>
- {badge ? (
- <span
- className="rounded-full px-1.5 py-0.5 text-[10px] font-medium"
- style={{ background: ED.ctl, color: ED.text2 }}
- >
- {badge}
- </span>
- ) : null}
- {value !== undefined ? (
- <span
- className="ml-auto min-w-9 text-right text-[11px] tabular-nums"
- style={{ color: ED.text3 }}
- >
- {value}
- </span>
- ) : null}
- </div>
- {children}
- </div>
-);
-
-export const InlineField = ({
- label,
- value,
- children,
-}: {
- label: ReactNode;
- value?: ReactNode;
- children?: ReactNode;
-}) => (
- <div className="flex h-[34px] items-center gap-3">
- <span
- className="min-w-24 shrink-0 text-[13px] font-normal"
- style={{ color: ED.text1 }}
- >
- {label}
- </span>
- <div className="flex flex-1 items-center justify-end gap-2">
- {children}
- {value !== undefined ? (
- <span
- className="min-w-9 text-right text-[11px] tabular-nums"
- style={{ color: ED.text3 }}
- >
- {value}
- </span>
- ) : null}
- </div>
- </div>
-);
-
-export const Slider = ({
- fill,
- className,
-}: {
- fill: number;
- className?: string;
-}) => (
- <div className={classNames("relative flex h-8 items-center px-1", className)}>
- <div
- className="h-[3px] w-full overflow-hidden rounded-full"
- style={{ background: ED.ctlActive }}
- >
- <div
- className="h-full rounded-full transition-[width] duration-300 ease-out"
- style={{ width: `${fill * 100}%`, background: ED.accent }}
- />
- </div>
- <span
- className="absolute size-3.5 rounded-full transition-[left] duration-300 ease-out"
- style={{
- left: `calc(4px + (100% - 8px) * ${fill} - 7px)`,
- background: "#ffffff",
- boxShadow: "0 1px 3px rgba(0,0,0,0.25), 0 0 0 0.5px rgba(0,0,0,0.1)",
- }}
- />
- </div>
-);
-
-export const Segmented = ({
- options,
- value,
- itemWidth,
- disabled,
- className,
- style,
-}: {
- options: readonly string[];
- value: string | null;
- itemWidth?: number;
- disabled?: boolean;
- className?: string;
- style?: CSSProperties;
-}) => (
- <div
- className={classNames(
- "flex gap-0.5 rounded-lg p-0.5 transition-opacity duration-200",
- disabled && "opacity-50",
- className,
- )}
- style={{ background: ED.ctl, ...style }}
- >
- {options.map((option) => {
- const on = value === option;
- return (
- <span
- key={option}
- className="flex h-[26px] flex-1 items-center justify-center whitespace-nowrap rounded-md px-3 text-[11.5px] font-medium transition-[background-color,color,box-shadow] duration-200"
- style={{
- width: itemWidth,
- background: on ? ED.card : "transparent",
- color: on ? ED.text1 : ED.text2,
- boxShadow: on
- ? "0 1px 2px rgba(0,0,0,0.12), 0 0 0 0.5px rgba(0,0,0,0.06)"
- : undefined,
- }}
- >
- {option}
- </span>
- );
- })}
- </div>
-);
-
-export const Toggle = ({ on }: { on: boolean }) => (
- <span
- className="relative inline-flex h-6 w-11 shrink-0 rounded-full p-0.5 transition-colors duration-200"
- style={{ background: on ? ED.accent : ED.ctlActive }}
- >
- <span
- className="absolute top-0.5 size-5 rounded-full bg-white transition-[left] duration-200"
- style={{
- left: on ? 22 : 2,
- boxShadow: "0 1px 3px rgba(0,0,0,0.22)",
- }}
- />
- </span>
-);
-
-export const EditorButton = ({
- children,
- primary,
- className,
- style,
-}: {
- children: ReactNode;
- primary?: boolean;
- className?: string;
- style?: CSSProperties;
-}) => (
- <span
- className={classNames(
- "inline-flex h-7 items-center gap-1.5 whitespace-nowrap rounded-[7px] px-2.5 text-[13px] font-medium",
- className,
- )}
- style={{
- background: primary ? ED.accent : ED.ctl,
- color: primary ? "#ffffff" : ED.text1,
- ...style,
- }}
- >
- {children}
- </span>
-);
-
-export const Tile = ({
- selected,
- children,
- className,
- style,
-}: {
- selected?: boolean;
- children: ReactNode;
- className?: string;
- style?: CSSProperties;
-}) => (
- <span
- className={classNames(
- "flex h-[30px] items-center justify-center gap-1.5 whitespace-nowrap rounded-lg px-2 text-[11.5px] font-medium transition-colors duration-200",
- className,
- )}
- style={{
- background: selected ? ED.ctlHover : ED.ctl,
- color: selected ? ED.text1 : ED.text2,
- ...style,
- }}
- >
- {children}
- </span>
-);
-
-export const TRACK_H = 44;
-export const TRACK_GAP = 6;
-export const TRACK_GUTTER = 96;
-export const RULER_H = 26;
-
-export const TrackLabel = ({
- hue,
- icon,
- children,
- style,
-}: {
- hue: string;
- icon?: ReactNode;
- children: ReactNode;
- style?: CSSProperties;
-}) => (
- <div
- className="flex shrink-0 items-center gap-2"
- style={{ width: TRACK_GUTTER, height: TRACK_H, ...style }}
- >
- <span
- className="grid size-[22px] shrink-0 place-items-center rounded-md"
- style={{
- background: mix(hue, 16, "transparent"),
- color: mix(hue, 62, "#000000"),
- }}
- >
- {icon ? <span className="size-3.5">{icon}</span> : null}
- </span>
- <span
- className="truncate text-[11px] font-medium"
- style={{ color: ED.text2 }}
- >
- {children}
- </span>
- </div>
-);
-
-export const Lane = ({
- empty,
- className,
- style,
- children,
-}: {
- empty?: ReactNode;
- className?: string;
- style?: CSSProperties;
- children?: ReactNode;
-}) => (
- <div
- className={classNames("relative flex-1", className)}
- style={{ height: TRACK_H, ...style }}
- >
- {empty ? (
- <div
- className="absolute inset-0 flex items-center justify-center gap-1.5 rounded-lg border border-dashed text-[12px]"
- style={{ borderColor: ED.lineStrong, color: ED.text3 }}
- >
- {empty}
- </div>
- ) : null}
- {children}
- </div>
-);
-
-export const Segment = ({
- hue,
- label,
- sublabel,
- trailing,
- selected,
- segmentRef,
- className,
- style,
- children,
-}: {
- hue: string;
- label?: ReactNode;
- sublabel?: ReactNode;
- trailing?: ReactNode;
- selected?: boolean;
- segmentRef?: RefObject<HTMLDivElement | null>;
- className?: string;
- style?: CSSProperties;
- children?: ReactNode;
-}) => (
- <div
- ref={segmentRef}
- className={classNames(
- "absolute inset-y-0 overflow-hidden rounded-lg",
- className,
- )}
- style={{
- background: mix(hue, selected ? 23 : 13),
- boxShadow: selected
- ? `inset 0 0 0 1.5px ${ED.accent}`
- : `inset 0 0 0 1px ${mix(hue, 34, "transparent")}`,
- ...style,
- }}
- >
- <span
- className="absolute inset-y-0 left-0 w-[3px]"
- style={{ background: hue }}
- />
- {label !== undefined ? (
- <span className="absolute left-[13px] top-1/2 flex -translate-y-1/2 items-center gap-2 whitespace-nowrap">
- <span
- className="text-[12px] font-medium"
- style={{ color: mix(hue, 58, "#000000") }}
- >
- {label}
- </span>
- {sublabel !== undefined ? (
- <span
- className="text-[11px] tabular-nums"
- style={{ color: mix(hue, 45, ED.text3) }}
- >
- {sublabel}
- </span>
- ) : null}
- {trailing}
- </span>
- ) : null}
- {children}
- </div>
-);
-
-export const Ruler = ({
- labels,
- span: total,
- className,
- style,
-}: {
- labels: string[];
- span: number;
- className?: string;
- style?: CSSProperties;
-}) => (
- <div
- className={classNames(
- "relative flex-1 text-[11px] tabular-nums",
- className,
- )}
- style={{ height: RULER_H, color: ED.text3, ...style }}
- >
- {labels.map((label, i) => (
- <span
- key={label}
- className={classNames(
- "absolute top-1 flex flex-col gap-1",
- i === 0 ? "items-start" : "-translate-x-1/2 items-center",
- )}
- style={{ left: `${(i / total) * 100}%` }}
- >
- <span>{label}</span>
- <span className="h-1 w-px" style={{ background: ED.lineStrong }} />
- </span>
- ))}
- </div>
-);
-
-export const Playhead = ({
- playheadRef,
- top = 0,
- bottom = 0,
- left = 0,
-}: {
- playheadRef: RefObject<HTMLDivElement | null>;
- top?: number;
- bottom?: number;
- left?: number;
-}) => (
- <div
- ref={playheadRef}
- className="pointer-events-none absolute z-20 will-change-transform"
- style={{ top, bottom, left }}
- >
- <span
- className="absolute -left-[6px] top-0 size-3 rounded-full"
- style={{ background: ED.playhead, boxShadow: `0 0 0 2px ${ED.card}` }}
- />
- <span
- className="absolute left-0 top-1.5 h-full w-px"
- style={{ background: ED.playhead }}
- />
- </div>
-);
-
-export const WAVES = Array.from({ length: 64 }, (_, i) => {
- const a = Math.sin(i * 0.7) * 0.5 + 0.5;
- const b = Math.sin(i * 1.9 + 1) * 0.5 + 0.5;
- return { key: `w${i}`, h: 0.25 + 0.7 * (0.5 * a + 0.5 * b) };
-});
-
-export const Waveform = ({
- from = 0,
- to = WAVES.length,
- color,
- className,
-}: {
- from?: number;
- to?: number;
- color?: string;
- className?: string;
-}) => (
- <span
- className={classNames(
- "absolute inset-x-2 bottom-0 flex h-[18px] items-end gap-px",
- className,
- )}
- >
- {WAVES.slice(from, to).map((wave) => (
- <span
- key={wave.key}
- className="flex-1 rounded-[1px]"
- style={{
- height: `${wave.h * 100}%`,
- background: color ?? mix(HUE.clip, 45, "transparent"),
- }}
- />
- ))}
- </span>
-);
-
-export const SpeedChip = ({
- children,
- active,
- className,
-}: {
- children: ReactNode;
- active?: boolean;
- className?: string;
-}) => (
- <span
- className={classNames(
- "inline-flex h-4 items-center rounded-full px-1.5 text-[10px] font-medium leading-none transition-colors duration-300",
- className,
- )}
- style={{
- background: active ? ED.accent : ED.ctlActive,
- color: active ? "#ffffff" : ED.text1,
- }}
- >
- {children}
- </span>
-);
diff --git a/apps/web/components/pages/_components/UpgradeToPro.tsx b/apps/web/components/pages/_components/UpgradeToPro.tsx
index 3796675bf..4e983297b 100644
--- a/apps/web/components/pages/_components/UpgradeToPro.tsx
+++ b/apps/web/components/pages/_components/UpgradeToPro.tsx
@@ -1,6 +1,6 @@
"use client";
-import { Button } from "@cap/ui";
+import { Button } from "@cap/ui/button";
import { useCallback, useEffect, useRef, useState } from "react";
import { type Rive, useRive } from "@/lib/rive";
diff --git a/apps/web/lib/Notification.ts b/apps/web/lib/Notification.ts
index bc655f071..7fce48e06 100644
--- a/apps/web/lib/Notification.ts
+++ b/apps/web/lib/Notification.ts
@@ -110,7 +110,11 @@ export async function createNotification(
const recipientId = parentComment?.authorId;
if (!recipientId) return;
- if (recipientId === videoResult.ownerId) return;
+ if (
+ recipientId === videoResult.ownerId ||
+ recipientId === notification.authorId
+ )
+ return;
const [recipientUser] = await db()
.select({
diff --git a/apps/web/lib/video-download-permissions.ts b/apps/web/lib/video-download-permissions.ts
index d7d3e10ab..90b4dcb05 100644
--- a/apps/web/lib/video-download-permissions.ts
+++ b/apps/web/lib/video-download-permissions.ts
@@ -6,7 +6,7 @@ import {
spaceVideos,
} from "@cap/database/schema";
import type { User, Video } from "@cap/web-domain";
-import { and, eq, inArray } from "drizzle-orm";
+import { and, eq } from "drizzle-orm";
// Download access must not be broader than view access. VideosPolicy.canView
// grants org members access only through an explicit sharedVideos row (see
@@ -24,49 +24,37 @@ export async function canUserDownloadVideo({
}): Promise<boolean> {
if (userId === ownerId) return true;
- const sharedOrgs = await db()
- .select({ organizationId: sharedVideos.organizationId })
- .from(sharedVideos)
- .where(eq(sharedVideos.videoId, videoId));
-
- if (sharedOrgs.length > 0) {
- const [orgMembership] = await db()
+ const [organizationMembership, spaceMembership] = await Promise.allSettled([
+ db()
.select({ id: organizationMembers.id })
- .from(organizationMembers)
- .where(
+ .from(sharedVideos)
+ .innerJoin(
+ organizationMembers,
and(
+ eq(sharedVideos.organizationId, organizationMembers.organizationId),
eq(organizationMembers.userId, userId),
- inArray(
- organizationMembers.organizationId,
- sharedOrgs.map((org) => org.organizationId),
- ),
),
)
- .limit(1);
-
- if (orgMembership) return true;
- }
-
- const sharedSpaces = await db()
- .select({ spaceId: spaceVideos.spaceId })
- .from(spaceVideos)
- .where(eq(spaceVideos.videoId, videoId));
-
- if (sharedSpaces.length === 0) return false;
-
- const [spaceMembership] = await db()
- .select({ id: spaceMembers.id })
- .from(spaceMembers)
- .where(
- and(
- eq(spaceMembers.userId, userId),
- inArray(
- spaceMembers.spaceId,
- sharedSpaces.map((space) => space.spaceId),
+ .where(eq(sharedVideos.videoId, videoId))
+ .limit(1),
+ db()
+ .select({ id: spaceMembers.id })
+ .from(spaceVideos)
+ .innerJoin(
+ spaceMembers,
+ and(
+ eq(spaceVideos.spaceId, spaceMembers.spaceId),
+ eq(spaceMembers.userId, userId),
),
- ),
- )
- .limit(1);
+ )
+ .where(eq(spaceVideos.videoId, videoId))
+ .limit(1),
+ ]);
+
+ if (organizationMembership.status === "rejected")
+ throw organizationMembership.reason;
+ if (organizationMembership.value[0]) return true;
+ if (spaceMembership.status === "rejected") throw spaceMembership.reason;
- return Boolean(spaceMembership);
+ return Boolean(spaceMembership.value[0]);
}
diff --git a/apps/web/public/backgrounds/clouds/bank-a.webp b/apps/web/public/backgrounds/clouds/bank-a.webp
index 443d307cc..b61a17b2b 100644
Binary files a/apps/web/public/backgrounds/clouds/bank-a.webp and b/apps/web/public/backgrounds/clouds/bank-a.webp differ
diff --git a/apps/web/public/backgrounds/clouds/cumulus-a.webp b/apps/web/public/backgrounds/clouds/cumulus-a.webp
index a66987358..4097a7431 100644
Binary files a/apps/web/public/backgrounds/clouds/cumulus-a.webp and b/apps/web/public/backgrounds/clouds/cumulus-a.webp differ
diff --git a/apps/web/public/backgrounds/clouds/cumulus-b.webp b/apps/web/public/backgrounds/clouds/cumulus-b.webp
index 787e825e5..d18b92502 100644
Binary files a/apps/web/public/backgrounds/clouds/cumulus-b.webp and b/apps/web/public/backgrounds/clouds/cumulus-b.webp differ
diff --git a/apps/web/public/backgrounds/clouds/tuft-a.webp b/apps/web/public/backgrounds/clouds/tuft-a.webp
index c5a75272e..f55cba90d 100644
Binary files a/apps/web/public/backgrounds/clouds/tuft-a.webp and b/apps/web/public/backgrounds/clouds/tuft-a.webp differ
diff --git a/apps/web/public/backgrounds/clouds/wisp-a.webp b/apps/web/public/backgrounds/clouds/wisp-a.webp
index 6c473b7b8..8548182d5 100644
Binary files a/apps/web/public/backgrounds/clouds/wisp-a.webp and b/apps/web/public/backgrounds/clouds/wisp-a.webp differ
diff --git a/apps/web/public/backgrounds/hero/rays-far.webp b/apps/web/public/backgrounds/hero/rays-far.webp
index 8b3a49b8c..9b9e5d2f3 100644
Binary files a/apps/web/public/backgrounds/hero/rays-far.webp 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
index d48a34cb0..c999d307d 100644
Binary files a/apps/web/public/backgrounds/hero/rays-near.webp and b/apps/web/public/backgrounds/hero/rays-near.webp differ
diff --git a/apps/web/public/backgrounds/landing/london-thumb.webp b/apps/web/public/backgrounds/landing/london-thumb.webp
new file mode 100644
index 000000000..ef6253176
Binary files /dev/null and b/apps/web/public/backgrounds/landing/london-thumb.webp differ
diff --git a/apps/web/public/backgrounds/landing/london.webp b/apps/web/public/backgrounds/landing/london.webp
new file mode 100644
index 000000000..b3d50eed2
Binary files /dev/null and b/apps/web/public/backgrounds/landing/london.webp differ
diff --git a/apps/web/public/backgrounds/landing/miami-thumb.webp b/apps/web/public/backgrounds/landing/miami-thumb.webp
new file mode 100644
index 000000000..0e653a0af
Binary files /dev/null and b/apps/web/public/backgrounds/landing/miami-thumb.webp differ
diff --git a/apps/web/public/backgrounds/landing/miami.webp b/apps/web/public/backgrounds/landing/miami.webp
new file mode 100644
index 000000000..d46f4ce2b
Binary files /dev/null and b/apps/web/public/backgrounds/landing/miami.webp differ
diff --git a/apps/web/public/backgrounds/landing/monaco-thumb.webp b/apps/web/public/backgrounds/landing/monaco-thumb.webp
new file mode 100644
index 000000000..83864dc3a
Binary files /dev/null and b/apps/web/public/backgrounds/landing/monaco-thumb.webp differ
diff --git a/apps/web/public/backgrounds/landing/monaco.webp b/apps/web/public/backgrounds/landing/monaco.webp
new file mode 100644
index 000000000..e698ab8ff
Binary files /dev/null and b/apps/web/public/backgrounds/landing/monaco.webp differ
diff --git a/apps/web/public/backgrounds/landing/nyc.webp b/apps/web/public/backgrounds/landing/nyc.webp
new file mode 100644
index 000000000..c7556f635
Binary files /dev/null and b/apps/web/public/backgrounds/landing/nyc.webp differ
diff --git a/apps/web/public/backgrounds/landing/rome-thumb.webp b/apps/web/public/backgrounds/landing/rome-thumb.webp
new file mode 100644
index 000000000..f5a24d410
Binary files /dev/null and b/apps/web/public/backgrounds/landing/rome-thumb.webp differ
diff --git a/apps/web/public/backgrounds/landing/rome.webp b/apps/web/public/backgrounds/landing/rome.webp
new file mode 100644
index 000000000..5c5d324b6
Binary files /dev/null and b/apps/web/public/backgrounds/landing/rome.webp differ
diff --git a/apps/web/public/backgrounds/landing/santorini-thumb.webp b/apps/web/public/backgrounds/landing/santorini-thumb.webp
new file mode 100644
index 000000000..05d1b4cf9
Binary files /dev/null and b/apps/web/public/backgrounds/landing/santorini-thumb.webp differ
diff --git a/apps/web/public/backgrounds/landing/santorini.webp b/apps/web/public/backgrounds/landing/santorini.webp
new file mode 100644
index 000000000..39978fdd5
Binary files /dev/null and b/apps/web/public/backgrounds/landing/santorini.webp differ
diff --git a/apps/web/public/backgrounds/landing/sf-960.webp b/apps/web/public/backgrounds/landing/sf-960.webp
new file mode 100644
index 000000000..9ea3bb5d0
Binary files /dev/null and b/apps/web/public/backgrounds/landing/sf-960.webp differ
diff --git a/apps/web/public/backgrounds/landing/sf-thumb.webp b/apps/web/public/backgrounds/landing/sf-thumb.webp
new file mode 100644
index 000000000..7bd425c97
Binary files /dev/null and b/apps/web/public/backgrounds/landing/sf-thumb.webp differ
diff --git a/apps/web/public/backgrounds/landing/sf.webp b/apps/web/public/backgrounds/landing/sf.webp
new file mode 100644
index 000000000..643fbe93a
Binary files /dev/null and b/apps/web/public/backgrounds/landing/sf.webp differ
diff --git a/apps/web/public/logos/os/linux.svg b/apps/web/public/logos/os/linux.svg
index 9301e081a..692432f9d 100644
--- a/apps/web/public/logos/os/linux.svg
+++ b/apps/web/public/logos/os/linux.svg
@@ -1,111 +1 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<!-- Created with Inkscape (http://www.inkscape.org/) -->
-
-<svg
- xmlns:dc="http://purl.org/dc/elements/1.1/"
- xmlns:cc="http://creativecommons.org/ns#"
- xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
- xmlns:svg="http://www.w3.org/2000/svg"
- xmlns="http://www.w3.org/2000/svg"
- xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
- xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
- width="708.66144"
- height="857.81506"
- id="svg3007"
- version="1.1"
- inkscape:version="0.91 r"
- sodipodi:docname="Linux Pinguino.svg">
- <defs
- id="defs3009" />
- <sodipodi:namedview
- id="base"
- pagecolor="#ffffff"
- bordercolor="#666666"
- borderopacity="1.0"
- inkscape:pageopacity="0.0"
- inkscape:pageshadow="2"
- inkscape:zoom="0.35"
- inkscape:cx="275.4208"
- inkscape:cy="480.09706"
- inkscape:document-units="px"
- inkscape:current-layer="layer2"
- showgrid="false"
- inkscape:window-width="1024"
- inkscape:window-height="719"
- inkscape:window-x="0"
- inkscape:window-y="25"
- inkscape:window-maximized="1"
- units="cm"
- fit-margin-top="0"
- fit-margin-left="0"
- fit-margin-right="0"
- fit-margin-bottom="0" />
- <metadata
- id="metadata3012">
- <rdf:RDF>
- <cc:Work
- rdf:about="">
- <dc:format>image/svg+xml</dc:format>
- <dc:type
- rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
- <dc:title></dc:title>
- </cc:Work>
- </rdf:RDF>
- </metadata>
- <g
- inkscape:label="Layer 1"
- inkscape:groupmode="layer"
- id="layer1"
- transform="translate(-35.433071,63.88618)"
- style="display:inline"
- sodipodi:insensitive="true">
- <g
- transform="matrix(0.91159133,0,0,0.91159133,-140.97298,1091.3033)"
- style="font-style:normal;font-weight:normal;font-size:429.75283813px;line-height:125%;font-family:Sans;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"
- id="text2999" />
- <g
- inkscape:groupmode="layer"
- id="layer2"
- inkscape:label="Layer 2"
- sodipodi:insensitive="true">
- <g
- id="g4241">
- <path
- id="path154"
- d="m 413.6538,-43.885882 c -5.21516,-0.0096 -10.65489,0.209315 -16.32753,0.66159 C 253.95642,-31.677866 291.9791,119.78498 289.85121,170.50502 c -2.61821,37.0912 -10.13858,66.32159 -35.64845,102.58276 -29.97027,35.63998 -72.16502,93.32376 -92.15377,153.37057 -9.43377,28.33951 -13.92295,57.22507 -9.7771,84.56334 -1.2987,1.16299 -2.53507,2.3815 -3.71069,3.63655 -8.79926,9.39401 -15.30093,20.77622 -22.54542,28.43963 -6.76829,6.75151 -16.41138,9.31957 -27.008338,13.12148 -10.601011,3.80362 -22.240114,9.41125 -29.305119,22.96844 0,0 0,0.005 -0.0048,0.007 -0.01153,0.0225 -0.02411,0.0437 -0.03612,0.0666 l 0,0 c -6.3956,11.94556 -4.494289,25.70181 -2.541029,38.44671 1.953317,12.74475 3.930749,24.7881 1.305968,32.95903 -8.38998,22.93199 -9.461144,38.7945 -3.55487,50.30059 5.918866,11.53004 18.088545,16.61924 31.842924,19.49201 27.508834,5.7455 64.763004,4.31771 94.123604,19.87912 l 2.52358,-4.76164 -2.49746,4.77583 c 31.43196,16.43348 63.29777,22.27051 88.72108,16.46363 18.43231,-4.21004 33.39499,-15.18545 41.0793,-32.07399 19.88509,-0.0978 41.70549,-8.51961 76.65906,-10.4442 23.71253,-1.91003 53.33366,8.42059 87.40527,6.5287 0.89062,3.69654 2.1796,7.25202 3.944,10.63061 0.018,0.0348 0.035,0.0692 0.0535,0.10345 13.20794,26.4114 37.74924,38.49201 63.90275,36.42795 26.188,-2.06669 54.03113,-17.50835 76.53804,-44.293 l -4.12607,-3.46751 4.15761,3.42935 c 21.45245,-26.01178 57.06,-36.79786 80.67066,-51.03831 11.80538,-7.12042 21.38456,-16.03623 22.12469,-28.98585 0.73973,-12.94311 -6.85904,-27.44794 -24.33217,-46.84376 0,0 -0.005,-0.005 -0.005,-0.005 -0.007,-0.009 -0.0137,-0.0161 -0.0204,-0.0241 -5.74108,-6.48783 -8.47992,-18.51237 -11.4232,-31.31741 -2.94127,-12.79725 -6.22443,-26.60189 -16.73081,-35.5481 l 0,0 c -0.021,-0.0185 -0.0422,-0.0352 -0.0633,-0.0535 0,0 0,0 0,0 -4.18139,-3.64359 -8.52658,-6.1165 -12.90241,-7.77789 14.60718,-43.32125 8.87598,-86.45868 -5.86907,-125.43994 C 642.55136,348.78328 610.95787,307.09735 586.8294,278.5805 559.82151,244.51423 533.40542,212.17103 533.92579,164.4012 534.73026,91.494028 541.94532,-43.710132 413.65068,-43.885228 Z m 17.36858,112.320623 c 7.28001,0 13.49762,2.133852 19.88447,6.769553 6.48829,4.709202 11.16263,10.604422 14.93004,18.836884 3.69759,8.023152 5.47664,15.871782 5.65107,25.185692 0,0.24419 0,0.45344 0.0697,0.69763 0.0697,9.55796 -1.56972,17.68576 -5.19761,25.98798 -2.07006,4.7373 -4.44735,8.71337 -7.29275,12.15569 -0.96599,-0.46377 -1.96925,-0.90968 -3.00978,-1.33756 l 0,0 c -7.21244,-3.0891 -12.74525,-5.05497 -17.35654,-6.65506 1.67157,-2.00927 3.06344,-4.39226 4.28736,-7.37124 1.84882,-4.49989 2.75578,-8.89527 2.93019,-14.12769 0,-0.20935 0.0698,-0.38379 0.0698,-0.62796 0.1046,-5.02309 -0.55814,-9.31379 -2.02322,-13.7091 -1.53486,-4.60454 -3.48834,-7.9184 -6.31386,-10.67418 -2.82554,-2.75589 -5.65107,-4.011584 -9.03478,-4.116338 -0.15864,-0.0096 -0.3168,-0.0096 -0.47306,-0.0096 -3.18281,0.0096 -5.94651,1.101008 -8.80584,3.494868 -2.99994,2.51169 -5.23246,5.72079 -7.08132,10.18588 -1.84879,4.465 -2.75577,8.89514 -2.93013,14.16257 -0.0351,0.20935 -0.0351,0.38379 -0.0351,0.59304 -0.0603,2.89536 0.12362,5.54776 0.57011,8.12338 -6.5125,-3.24509 -12.64971,-5.45748 -18.40956,-6.82838 -0.32833,-2.4907 -0.51826,-5.06094 -0.57889,-7.74834 l 0,-0.73263 c -0.10468,-9.52314 1.46509,-17.68575 5.12785,-25.987887 3.66275,-8.302245 8.19756,-14.267323 14.58117,-19.11607 6.38361,-4.848738 12.66256,-7.081238 20.09271,-7.15101 0.11611,0 0.2322,0 0.34774,0 z m -99.64166,7.889045 c 4.8294,0.01145 9.15083,1.624854 13.62079,5.194362 4.84876,3.871933 8.5115,8.82533 11.58123,15.802012 3.06971,6.97667 4.70922,13.95325 5.16268,22.18572 l 0,0.0693 c 0.21981,3.45756 0.19293,6.71217 -0.0796,9.8642 -0.95516,0.27148 -1.89117,0.56523 -2.80808,0.8809 -5.20733,1.79246 -9.7965,4.23523 -13.79631,6.87637 0.39112,-2.76279 0.44934,-5.56595 0.14934,-8.69145 -0.0352,-0.17444 -0.0352,-0.31394 -0.0352,-0.48829 -0.41861,-4.15112 -1.29067,-7.63942 -2.75577,-11.16265 -1.56971,-3.6626 -3.3139,-6.24404 -5.61621,-8.23244 -2.08641,-1.80185 -4.05777,-2.6293 -6.22663,-2.61291 -0.22438,0 -0.4513,0.0124 -0.6802,0.0314 -2.44184,0.20913 -4.46506,1.39515 -6.38366,3.7324 -1.91856,2.33715 -3.17433,5.23248 -4.0813,9.06954 -0.90697,3.83718 -1.15116,7.60458 -0.76745,11.93004 0,0.17434 0.0351,0.31403 0.0351,0.48836 0.41854,4.18604 1.25576,7.67427 2.75575,11.19759 1.53486,3.62783 3.31391,6.20915 5.61622,8.19746 0.38557,0.333 0.76701,0.63212 1.14674,0.89935 -2.39474,1.84307 -3.53823,2.69278 -5.50061,4.13473 -1.25862,0.92353 -2.75763,2.02314 -4.49991,3.30739 -3.79955,-3.55937 -6.76364,-8.03274 -9.35527,-13.92276 -3.06972,-6.97655 -4.70924,-13.95326 -5.19761,-22.18573 l 0,-0.0693 c -0.45346,-8.23236 0.34884,-15.31369 2.61625,-22.63913 2.26741,-7.325523 5.30227,-12.627687 9.69751,-16.988217 4.39529,-4.36034 8.82549,-6.557954 14.16262,-6.836939 0.41695,-0.02291 0.83019,-0.03332 1.23941,-0.03143 z m 45.31655,57.113524 c 11.22472,-0.0433 24.73218,3.63666 41.08476,14.15065 10.0445,6.53193 17.86033,7.07784 35.85774,14.78606 l 0.0216,0.01 0.022,0.009 c 8.6593,3.55287 13.74186,8.18385 16.22506,13.06041 2.48315,4.87664 2.54388,10.16488 0.46544,15.72795 -4.15672,11.12643 -17.425,22.83576 -36.04414,28.65006 l -0.0176,0.008 -0.0165,0.008 c -9.0786,2.94963 -16.99508,9.4634 -26.32808,14.80026 -9.33273,5.33659 -19.89342,9.6409 -34.25308,8.8102 0,-7e-5 0,0 0,0 -12.22465,-0.7125 -19.53208,-4.86257 -26.1395,-10.17284 -6.60761,-5.31033 -12.36554,-11.98164 -20.79693,-16.93251 l -0.0216,-0.013 -0.022,-0.013 c -13.58103,-7.67626 -20.99562,-16.55463 -23.32265,-24.25146 -2.32709,-7.69686 -0.14319,-14.26676 6.59509,-19.31662 7.57574,-5.67747 12.83828,-9.53942 16.3438,-12.11201 3.48245,-2.55543 4.93131,-3.51362 6.04029,-4.57847 0.006,-0.009 0.0117,-0.0122 0.0175,-0.0173 0,0 0.005,-0.009 0.005,-0.009 5.69223,-5.38806 14.76608,-15.20451 28.44393,-19.91278 4.70763,-1.62055 9.96064,-2.66609 15.84025,-2.68932 z m 78.49149,46.42197 c -4.84383,0.28881 -9.9169,2.78272 -15.82383,6.08057 -5.90699,3.29777 -12.54959,7.5163 -19.74829,11.67057 -14.39737,8.30854 -30.97057,16.25449 -47.4411,16.25449 -16.49612,0 -29.67558,-7.61859 -39.56081,-15.45215 -4.94263,-3.91679 -9.02198,-7.86202 -12.28106,-10.91405 -1.62954,-1.5261 -3.04279,-2.82674 -4.3615,-3.84264 -1.31846,-1.01589 -2.39167,-2.02321 -4.59804,-2.02321 l -0.10573,4.63619 c -0.15798,1.55131 -0.32031,1.50348 -0.25617,1.54576 0.10291,0.0676 0.61485,0.24409 1.2122,0.70422 0.96947,0.74684 2.31518,1.9643 3.91234,3.45997 3.19434,2.9914 7.43893,7.10425 12.66482,11.24548 10.45166,8.28238 24.98691,16.77988 43.37395,16.77988 18.41261,0 35.83311,-8.60615 50.51087,-17.07642 7.33882,-4.23516 14.00414,-8.46274 19.67195,-11.62698 5.66752,-3.16433 10.43602,-5.14855 13.19671,-5.31318 l -0.36631,-6.1285 z m 16.36678,22.03414 c 12.22931,48.20748 40.67315,117.83748 58.95478,151.81819 9.71733,18.02848 29.04987,56.33272 37.40244,102.48244 5.29253,-0.16214 11.12018,0.60683 17.36092,2.20307 21.83839,-56.61523 -18.51868,-117.59738 -36.97294,-134.5804 -7.45053,-7.22958 -7.80865,-10.46874 -4.11511,-10.31561 20.02212,17.70619 46.30676,53.31902 55.86978,93.51639 4.36058,18.3293 5.29026,37.5985 0.61156,56.61538 2.28908,0.95096 4.6139,1.98487 6.97007,3.10025 35.05467,17.06985 48.01909,31.91342 41.78569,52.17216 -2.05306,-0.072 -4.07258,-0.0639 -6.04239,-0.0165 -0.18384,0.005 -0.3663,0.01 -0.54946,0.014 5.07631,-16.05845 -6.16941,-27.90241 -36.13566,-41.4608 -31.08095,-13.6742 -55.84799,-12.31378 -60.03395,15.41838 -0.26791,1.4529 -0.4834,2.9334 -0.64754,4.43452 -2.32208,0.80636 -4.6568,1.8359 -6.99736,3.1166 -14.59457,7.98572 -22.56044,22.47561 -26.99089,40.24755 -4.42689,17.75756 -5.696,39.2254 -6.91777,63.35769 0,0.005 0,0.009 0,0.0125 -0.74649,12.13453 -5.73541,28.54688 -10.79526,45.93032 -50.91537,36.31882 -121.58105,52.05411 -181.57889,11.1081 -4.06607,-6.43216 -8.73037,-12.80519 -13.53147,-19.09081 -3.06591,-4.01401 -6.21536,-8.00347 -9.34651,-11.93673 6.15909,0.005 11.39435,-1.00386 15.6255,-2.92242 5.26169,-2.38587 8.95865,-6.21802 10.78763,-11.13759 3.65793,-9.83929 -0.016,-23.7194 -11.72621,-39.56967 -11.70948,-15.84918 -31.54117,-33.73646 -60.68259,-51.60847 0,0 -0.005,0 -0.005,-0.005 -21.40893,-13.31934 -33.38207,-29.64484 -38.98627,-47.37024 -5.60474,-17.72695 -4.81889,-36.89555 -0.49928,-55.82076 8.29109,-36.32625 29.59401,-71.65643 43.18646,-93.82814 3.65949,-2.69263 1.30747,4.99808 -13.76032,32.97879 -13.50408,25.58224 -38.75001,84.62032 -4.1838,130.70514 0.92373,-32.79629 8.75598,-66.24444 21.90444,-97.53121 19.15175,-43.41285 59.21029,-118.69606 62.39188,-178.69784 1.64039,1.19115 7.27736,4.99456 9.78909,6.42295 0.005,0.008 0.0111,0.008 0.0161,0.009 7.34881,4.32758 12.86808,10.65354 20.01643,16.39843 7.16251,5.75628 16.11041,10.72814 29.62889,11.51583 l 0,0 c 15.77886,0.91305 27.80467,-3.97416 37.65748,-9.6082 9.83799,-5.62549 17.69729,-11.85124 25.14318,-14.28142 0.0117,-0.008 0.0226,-0.008 0.0336,-0.0107 15.73384,-4.92021 28.24389,-13.62876 35.36281,-23.78598 z m 99.93381,275.98704 c 6.37287,-0.0295 14.09279,2.09518 22.54327,5.87563 22.74381,10.49987 29.85997,19.49973 23.65079,32.72048 -5.23245,9.97659 -27.62749,25.84843 -42.97609,21.66247 -15.66252,-4.04645 -23.30196,-26.61591 -20.75552,-43.67374 1.36405,-11.58345 7.91576,-16.53986 17.53755,-16.58484 z m -25.73289,31.17142 c 1.28565,20.85941 11.60602,42.13372 29.85781,46.73803 19.97321,5.26251 48.77045,-11.87469 60.92896,-25.8528 2.42438,-0.0971 4.79463,-0.21584 7.09654,-0.27141 10.66533,-0.25752 19.61062,0.3513 28.75032,8.34466 l 0.0316,0.0271 0.0326,0.0271 c 7.02363,5.95057 10.3622,17.19592 13.2578,29.79353 2.89545,12.59766 5.2109,26.3179 13.89441,36.0921 l 0.0129,0.014 0.0121,0.014 c 16.69426,18.52555 22.06033,31.0474 21.60357,39.03969 -0.45681,7.9925 -6.2436,13.92585 -16.9292,20.37065 -21.36193,12.88434 -59.21225,24.08827 -83.38827,53.37242 -20.99241,24.98213 -46.59034,38.70272 -69.13412,40.48196 -22.54377,1.7791 -41.99373,-7.57744 -53.4607,-30.59262 l -0.0261,-0.0522 -0.0271,-0.0513 c -7.11374,-13.53232 -4.15036,-34.88817 1.84224,-57.41439 5.99273,-22.52638 14.60269,-45.65605 15.75524,-64.45553 l 0,-0.0295 0,-0.0286 c 1.21925,-24.08721 2.56746,-45.1225 6.61366,-61.35291 4.04615,-16.23042 10.41733,-27.22266 21.70607,-33.39957 0.52569,-0.28761 1.04754,-0.55897 1.56645,-0.8154 z M 179.16687,510.626 c 1.6849,0.007 3.46796,0.14749 5.36766,0.43495 12.77836,1.93566 23.92156,10.87019 34.65749,25.43198 10.73602,14.56201 20.72566,34.44015 30.99484,56.49013 l 0.0131,0.0271 0.0121,0.0261 c 8.25231,17.22375 25.66603,36.1625 40.42309,55.48274 14.757,19.32024 26.17797,38.71341 24.69071,53.55657 l -0.007,0.0645 -0.005,0.0643 c -1.92392,25.31574 -16.20438,39.09735 -38.12629,44.10444 -21.91538,5.00542 -51.62501,0.0161 -81.29959,-15.49263 -0.009,-0.005 -0.0176,-0.009 -0.0261,-0.014 -32.83022,-17.38843 -71.88647,-15.66131 -96.939878,-20.89386 -12.530108,-2.61711 -20.706053,-6.55156 -24.458561,-13.86186 -3.752764,-7.31003 -3.836518,-20.06382 4.137967,-41.80949 l 0.03508,-0.096 0.03156,-0.097 c 3.943692,-12.16487 1.025174,-25.47508 -0.889521,-37.96816 -1.914695,-12.49316 -2.851052,-23.85678 1.414952,-31.77736 l 0.01963,-0.0362 0.01944,-0.0371 c 5.461901,-10.52828 13.47335,-14.2919 23.416451,-17.8591 9.94304,-3.5674 21.73338,-6.36903 31.04384,-15.69962 l 0.0481,-0.0492 0.0481,-0.0501 c 8.60716,-9.08005 15.07246,-20.4617 22.63917,-28.53991 6.38442,-6.81595 12.7735,-11.33336 22.40376,-11.40027 0.11143,0 0.22342,0 0.33575,0 z"
- style="color:#000000;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:medium;line-height:normal;font-family:'Bitstream Vera Sans';-inkscape-font-specification:'Bitstream Vera Sans';text-indent:0;text-align:start;text-decoration:none;text-decoration-line:none;letter-spacing:normal;word-spacing:normal;text-transform:none;direction:ltr;block-progression:tb;writing-mode:lr-tb;baseline-shift:baseline;text-anchor:start;display:inline;overflow:visible;visibility:visible;fill:#000000;fill-opacity:1;stroke:none;stroke-width:309;marker:none;enable-background:accumulate"
- inkscape:connector-curvature="0" />
- <path
- inkscape:connector-curvature="0"
- id="path4222"
- d="m 544.67511,509.42203 c -10.0634,4.23113 -16.30301,14.37681 -19.90096,24.29109 -8.7247,25.36839 -7.45231,52.64947 -10.57254,78.88797 -4.53879,27.69468 -16.08033,53.94226 -19.87578,81.8449 -0.3028,10.76998 -1.84176,22.34073 3.40479,32.26464 7.99417,15.98689 23.41995,29.52613 41.8312,30.79866 25.83787,2.6139 50.73511,-10.15426 69.13778,-27.41957 13.8228,-13.99522 26.76617,-29.40179 44.15466,-39.2056 19.15159,-11.78751 40.80437,-19.30927 59.31876,-32.21947 6.89217,-4.71596 11.49543,-13.35529 8.95729,-21.72491 -4.03232,-16.05133 -17.4286,-26.97055 -25.75184,-40.56001 -9.35878,-17.20794 -8.04187,-38.52355 -19.13096,-54.9746 -8.70828,-11.60778 -24.81913,-13.90401 -38.31079,-11.80481 -5.11651,1.89116 -8.14574,7.12109 -12.83008,9.85313 -14.45613,10.41796 -32.42379,19.53927 -50.56161,15.26609 -15.31494,-4.63006 -23.47716,-20.4166 -26.63674,-35.0512 -1.43201,-3.17002 0.046,-7.42312 -2.27564,-10.09311 -0.29074,-0.15617 -0.63369,-0.18986 -0.95754,-0.1532 z"
- style="fill:#ffd300;fill-opacity:1" />
- <path
- inkscape:connector-curvature="0"
- id="path4226"
- d="m 545.02764,509.37739 c -21.09095,8.64496 -23.01046,35.16219 -26.41087,54.64098 -3.12231,32.56625 -6.38607,65.04723 -16.67735,96.28065 -5.4922,24.26411 -15.50134,53.60619 1.71992,75.57305 11.01618,16.18734 31.04376,24.85292 50.37115,21.06891 37.22541,-2.22463 59.66084,-36.19017 86.76447,-57.03641 23.31522,-19.52408 55.43994,-25.82596 77.10246,-47.31453 10.19686,-15.04249 -4.68408,-31.55916 -12.92152,-43.65451 -18.87061,-19.13033 -15.49403,-48.66874 -30.96087,-69.55734 -9.42916,-10.80227 -27.0736,-12.80255 -39.78813,-7.48245 -16.92271,14.14016 -39.23526,27.46049 -61.9815,21.59565 -15.54932,-8.27635 -24.57799,-26.38365 -25.64132,-43.49763 -0.38717,-0.44564 -0.99316,-0.66383 -1.57644,-0.61637 z"
- style="fill:#ffbb00;fill-opacity:1" />
- <path
- inkscape:connector-curvature="0"
- id="path4232"
- d="m 178.82992,510.85201 c -21.98012,1.49026 -31.08312,25.74388 -45.45508,38.95137 -15.68046,16.39681 -45.996812,14.28443 -55.339555,37.29647 -6.07535,24.4518 8.516046,49.95462 -2.080743,73.81595 -4.034529,14.11271 -8.701381,35.71211 7.859755,43.81514 34.028393,12.48514 72.276793,8.15956 105.587433,23.65025 25.89746,11.54309 53.61203,23.71207 82.68807,18.46893 25.39148,-1.74667 46.3197,-25.84382 42.77663,-51.40968 -4.66389,-28.0145 -27.0576,-48.49685 -42.82894,-70.749 -27.46847,-31.48048 -35.90954,-76.01152 -67.89125,-103.9064 -7.05961,-5.90702 -15.92173,-10.26242 -25.31632,-9.93303 z"
- style="fill:#ffbb00;fill-opacity:1" />
- <path
- inkscape:connector-curvature="0"
- id="path4234"
- d="m 376.23952,132.51386 c -12.73508,-0.004 -24.93367,5.51551 -34.49226,13.72135 -6.22327,5.33503 -12.05072,11.16147 -18.87454,15.76378 -5.7256,4.40385 -11.88061,8.35022 -17.06907,13.39675 -4.61812,4.92478 -5.28625,12.46804 -2.68244,18.52606 3.29555,8.40294 10.31667,14.70129 17.56655,19.73916 6.9479,4.21641 13.65983,8.85716 19.55314,14.48577 7.62375,6.97026 16.23378,13.82766 26.74087,15.48994 7.14697,1.2682 14.54646,0.9137 21.67537,-0.29467 12.86908,-2.6775 23.78031,-10.37604 34.77384,-17.13927 5.27183,-3.47106 11.28651,-5.40397 17.1307,-7.63653 12.09605,-5.07743 23.92402,-13.05941 29.83709,-25.15232 3.37066,-6.64619 3.27213,-15.1973 -1.42461,-21.20113 -6.19133,-8.19738 -16.46837,-11.36612 -25.63733,-14.96041 -8.96525,-3.35539 -18.38296,-5.89213 -26.39991,-11.32229 -12.19032,-7.61139 -26.09058,-13.55238 -40.6974,-13.41619 z m 77.99647,48.43693 c 0.34876,1.35332 0.13429,2.82755 0.20273,4.2257 -7.77455,1.73819 -14.3439,6.58897 -21.21955,10.3601 -15.65318,9.17091 -31.53765,19.09888 -49.65812,22.41546 -5.9382,0.96725 -12.24954,0.97348 -18.34745,0.64824 -11.80751,-1.38548 -22.97401,-6.57883 -32.44474,-13.70917 -7.39356,-5.25787 -13.71146,-11.81093 -20.63688,-17.62455 0.35234,-1.43712 -0.0311,-3.43822 0.38698,-4.52552 4.53006,2.31246 7.68983,6.35539 11.59818,9.5619 8.95223,8.35838 19.0832,15.80212 30.83465,19.65161 5.68189,2.12534 11.75227,2.86421 17.79304,2.60633 10.41927,0.38729 20.52967,-3.08014 29.95933,-7.18067 15.02775,-6.6516 28.53143,-16.25298 43.10549,-23.69219 2.66889,-1.24476 5.48031,-2.40834 8.42634,-2.73724 z"
- style="fill:#ffbb00;fill-opacity:1" />
- <path
- inkscape:connector-curvature="0"
- id="path4236"
- d="m 570.8793,477.08619 c -7.04326,-0.19379 -14.03749,4.12166 -16.23409,10.96127 -3.04017,9.19884 -2.37642,19.3919 0.33527,28.59713 3.13795,9.75365 9.50196,20.04083 20.16303,22.495 11.12237,2.02086 22.16209,-3.21812 30.99838,-9.54895 7.02429,-5.28052 14.55411,-12.26753 14.54543,-21.76119 -0.23409,-8.77347 -8.00373,-14.62414 -14.81167,-18.82095 -10.65877,-6.09471 -22.45797,-11.86784 -34.99635,-11.92231 z"
- style="fill:#000000;fill-opacity:1" />
- <path
- inkscape:connector-curvature="0"
- id="path4238"
- d="m 375.3274,131.74691 c -14.44311,-0.0653 -27.7466,7.56086 -38.04292,17.17934 -9.86264,9.05679 -21.21991,16.34775 -31.266,25.20319 -6.71264,5.85643 -6.57917,16.40552 -2.09475,23.55592 6.21297,10.94523 17.51102,17.24629 27.43444,24.31891 9.71919,7.34143 18.06413,17.1304 29.9781,21.0408 9.51121,3.20973 19.94303,2.68753 29.6434,0.64918 14.50233,-3.79111 26.36236,-13.58778 39.49304,-20.16118 10.33536,-4.12318 21.04987,-8.15219 29.51674,-15.67507 5.81951,-5.26318 11.55339,-11.71634 13.004,-19.66954 0.63591,-5.49639 0.71001,-11.63359 -3.15911,-16.06357 -7.49882,-10.18719 -20.53383,-13.2908 -31.69686,-17.69848 -8.20119,-2.85523 -16.32083,-6.07463 -23.54368,-10.97644 -11.93666,-6.75756 -25.27817,-12.36278 -39.2664,-11.70306 z m 78.17729,50.28804 c 0.0251,1.17625 0.51188,2.90706 -1.2477,2.73729 -12.70626,5.02953 -23.96701,13.08263 -36.02169,19.35147 -13.25988,7.41286 -27.68839,13.79577 -43.12299,13.61096 -14.88236,0.97269 -29.07172,-5.59923 -40.76794,-14.30953 -6.54495,-5.13522 -12.70992,-10.72965 -18.97511,-16.19673 -0.14623,-1.77252 -0.30444,-3.96622 1.60539,-1.70812 11.48372,9.8128 22.485,21.01918 36.99389,26.28667 9.75837,4.25247 20.64035,4.03145 31.00423,2.87436 20.18345,-3.93297 37.47906,-15.8379 55.13585,-25.69167 4.893,-2.6383 9.99507,-5.88101 15.39607,-6.9547 z"
- style="fill:#ff9c00;fill-opacity:1" />
- </g>
- </g>
- </g>
-</svg>
+<svg xmlns="http://www.w3.org/2000/svg" width="708.66" height="857.82"><g style="display:inline"><path d="M413.65-43.89q-7.81-.01-16.32.67c-143.37 11.54-105.35 163-107.48 213.73-2.62 37.09-10.14 66.32-35.65 102.58-29.97 35.64-72.16 93.32-92.15 153.37-9.43 28.34-13.92 57.22-9.78 84.56a59 59 0 0 0-3.7 3.64c-8.8 9.4-15.31 20.77-22.55 28.44-6.77 6.75-16.42 9.32-27.01 13.12s-22.24 9.41-29.3 22.97h-.01l-.04.07c-6.4 11.95-4.5 25.7-2.54 38.45 1.95 12.74 3.93 24.79 1.3 32.96-8.38 22.93-9.45 38.8-3.55 50.3 5.92 11.53 18.09 16.62 31.84 19.49 27.51 5.75 64.77 4.32 94.13 19.88l2.52-4.76-2.5 4.77c31.44 16.44 63.3 22.27 88.73 16.47 18.43-4.21 33.39-15.19 41.07-32.08 19.89-.1 41.71-8.52 76.66-10.44 23.72-1.91 53.34 8.42 87.4 6.53q1.33 5.56 3.95 10.63l.06.1c13.2 26.41 37.75 38.5 63.9 36.43 26.19-2.07 54.03-17.5 76.54-44.3l-4.13-3.46 4.16 3.43c21.45-26.01 57.06-36.8 80.67-51.04 11.8-7.12 21.38-16.04 22.12-28.99s-6.86-27.44-24.33-46.84l-.02-.03c-5.74-6.49-8.48-18.51-11.43-31.32s-6.22-26.6-16.73-35.54l-.06-.06a42 42 0 0 0-12.9-7.78c14.6-43.32 8.87-86.45-5.87-125.44-18.1-47.84-49.7-89.52-73.82-118.04-27-34.07-53.42-66.4-52.9-114.18.8-72.9 8.02-208.11-120.28-208.29m17.37 112.32c7.28 0 13.5 2.14 19.89 6.77 6.49 4.71 11.16 10.6 14.93 18.84a60 60 0 0 1 5.65 25.19q-.02.34.07.7a61 61 0 0 1-5.2 25.98 51 51 0 0 1-7.3 12.16q-1.44-.7-3-1.34a247 247 0 0 0-17.36-6.65 28 28 0 0 0 4.29-7.38c1.85-4.5 2.75-8.9 2.93-14.12 0-.21.07-.39.07-.63.1-5.02-.56-9.32-2.02-13.71-1.54-4.6-3.5-7.92-6.32-10.67s-5.65-4.02-9.03-4.12l-.48-.01c-3.18 0-5.94 1.1-8.8 3.5-3 2.5-5.23 5.72-7.08 10.18a39 39 0 0 0-2.93 14.16q-.05.3-.04.6c-.06 2.9.12 5.54.57 8.12a86 86 0 0 0-18.4-6.83 72 72 0 0 1-.59-7.75v-.73c-.1-9.52 1.47-17.69 5.13-25.99s8.2-14.27 14.58-19.11 12.66-7.09 20.1-7.16zm-99.64 7.9c4.83 0 9.15 1.62 13.62 5.19 4.85 3.87 8.51 8.82 11.58 15.8a62 62 0 0 1 5.17 22.19v.07c.22 3.45.19 6.7-.08 9.86q-1.44.4-2.81.88a62 62 0 0 0-13.8 6.88c.4-2.77.45-5.57.15-8.7-.03-.17-.03-.3-.03-.48a38 38 0 0 0-2.76-11.17c-1.57-3.66-3.31-6.24-5.62-8.23-2.08-1.8-4.05-2.63-6.22-2.61q-.34 0-.68.03c-2.45.2-4.47 1.4-6.39 3.73s-3.17 5.24-4.08 9.07c-.9 3.84-1.15 7.6-.77 11.93 0 .18.04.32.04.5a37 37 0 0 0 2.76 11.19c1.53 3.63 3.3 6.2 5.61 8.2q.58.5 1.15.9c-2.4 1.84-3.54 2.69-5.5 4.13l-4.5 3.3c-3.8-3.55-6.77-8.03-9.36-13.92a62 62 0 0 1-5.2-22.18v-.07a62 62 0 0 1 2.62-22.64c2.27-7.33 5.3-12.63 9.7-16.99s8.82-6.56 14.16-6.83q.63-.04 1.24-.04m45.32 57.1c11.22-.04 24.73 3.64 41.08 14.16 10.05 6.53 17.86 7.08 35.86 14.78l.02.01.02.01c8.66 3.56 13.75 8.19 16.23 13.06s2.54 10.17.46 15.73c-4.15 11.13-17.42 22.84-36.04 28.65h-.02l-.01.02c-9.08 2.95-17 9.46-26.33 14.8-9.34 5.33-19.9 9.64-34.26 8.8-12.22-.7-19.53-4.85-26.14-10.16s-12.36-11.99-20.8-16.94l-.01-.01-.03-.01c-13.58-7.68-21-16.56-23.32-24.25s-.14-14.27 6.6-19.32c7.57-5.68 12.84-9.54 16.34-12.11 3.48-2.56 4.93-3.52 6.04-4.58l.02-.02c5.7-5.4 14.77-15.21 28.45-19.92a49 49 0 0 1 15.84-2.69zm78.49 46.43c-4.85.29-9.92 2.78-15.83 6.08s-12.54 7.52-19.74 11.67c-14.4 8.3-30.97 16.25-47.44 16.25-16.5 0-29.68-7.61-39.57-15.45-4.94-3.91-9.02-7.86-12.28-10.91a63 63 0 0 0-4.36-3.84c-1.32-1.02-2.39-2.03-4.6-2.03l-.1 4.64c-.16 1.55-.32 1.5-.26 1.54.1.07.62.25 1.21.7.97.76 2.32 1.97 3.92 3.47 3.2 2.99 7.44 7.1 12.66 11.24 10.45 8.29 24.99 16.78 43.38 16.78s35.83-8.6 50.5-17.07c7.35-4.24 14.01-8.46 19.68-11.63s10.43-5.15 13.2-5.31zm16.37 22.03c12.22 48.21 40.67 117.84 58.95 151.82 9.72 18.03 29.05 56.33 37.4 102.48 5.3-.16 11.12.61 17.36 2.2 21.84-56.6-18.52-117.59-36.97-134.57-7.45-7.23-7.8-10.47-4.11-10.32 20.02 17.7 46.3 53.32 55.87 93.52 4.36 18.33 5.29 37.6.6 56.61q3.44 1.43 6.98 3.1c35.05 17.07 48.02 31.92 41.78 52.18q-3.08-.1-6.04-.02l-.55.01c5.08-16.06-6.17-27.9-36.13-41.46-31.09-13.67-55.85-12.31-60.04 15.42a61 61 0 0 0-.65 4.43 46 46 0 0 0-7 3.12c-14.59 7.99-22.55 22.48-26.98 40.25s-5.7 39.22-6.92 63.36c-.75 12.14-5.74 28.56-10.8 45.94-50.91 36.32-121.58 52.05-181.58 11.1a267 267 0 0 0-13.53-19.08c-3.06-4.02-6.21-8-9.34-11.94 6.15 0 11.39-1 15.62-2.92 5.26-2.39 8.96-6.22 10.79-11.14 3.66-9.84-.02-23.72-11.73-39.57-11.7-15.85-31.54-33.74-60.68-51.6v-.01c-21.41-13.32-33.39-29.65-39-47.37-5.6-17.73-4.81-36.9-.5-55.82 8.3-36.33 29.6-71.66 43.2-93.83 3.66-2.7 1.3 5-13.76 32.98-13.5 25.58-38.75 84.62-4.19 130.7.93-32.8 8.76-66.24 21.9-97.53 19.16-43.41 59.22-118.7 62.4-178.7 1.64 1.2 7.28 5 9.79 6.43h.01c7.35 4.33 12.87 10.66 20.02 16.4 7.16 5.76 16.11 10.73 29.63 11.52 15.78.91 27.8-3.98 37.66-9.6 9.83-5.63 17.7-11.86 25.14-14.3h.03c15.74-4.92 28.25-13.63 35.37-23.79m99.93 275.99c6.37-.03 14.1 2.1 22.54 5.88 22.75 10.5 29.86 19.5 23.65 32.72-5.23 9.97-27.62 25.84-42.97 21.66-15.67-4.05-23.3-26.62-20.76-43.67 1.37-11.59 7.92-16.54 17.54-16.59m-25.73 31.17c1.28 20.86 11.6 42.14 29.85 46.74 19.98 5.26 48.77-11.87 60.93-25.85 2.43-.1 4.8-.22 7.1-.27 10.67-.26 19.61.35 28.75 8.34l.03.03.03.02c7.03 5.96 10.37 17.2 13.26 29.8s5.21 26.32 13.9 36.09v.01l.02.02c16.7 18.52 22.06 31.05 21.6 39.04s-6.24 13.92-16.92 20.37c-21.37 12.88-59.22 24.09-83.4 53.37-20.98 24.98-46.58 38.7-69.13 40.48s-41.99-7.57-53.46-30.59l-.02-.05-.03-.05c-7.11-13.54-4.15-34.9 1.84-57.42s14.6-45.65 15.76-64.45v-.06c1.22-24.09 2.56-45.12 6.61-61.35s10.42-27.23 21.7-33.4q.8-.44 1.57-.82zm-366.6 1.58q2.53 0 5.37.43c12.78 1.94 23.93 10.87 34.66 25.43 10.74 14.56 20.73 34.44 31 56.5l.01.02.01.03c8.25 17.22 25.67 36.16 40.43 55.48s26.17 38.71 24.69 53.56l-.01.06v.06c-1.93 25.32-16.21 39.1-38.13 44.1s-51.63.02-81.3-15.48l-.03-.02c-32.83-17.39-71.88-15.66-96.94-20.9-12.53-2.6-20.7-6.54-24.46-13.85-3.75-7.31-3.83-20.07 4.14-41.81l.04-.1.03-.1c3.94-12.16 1.02-25.47-.9-37.96-1.9-12.5-2.84-23.86 1.42-31.78l.02-.04.02-.03c5.46-10.53 13.48-14.3 23.42-17.86 9.94-3.57 21.73-6.37 31.04-15.7l.05-.05.05-.05c8.6-9.08 15.07-20.46 22.64-28.54 6.38-6.82 12.77-11.34 22.4-11.4z" style="color:#000;font-style:normal;font-variant:normal;font-weight:400;font-stretch:normal;font-size:medium;line-height:normal;font-family:&quot;Bitstream Vera Sans&quot;;-inkscape-font-specification:&quot;Bitstream Vera Sans&quot;;text-indent:0;text-align:start;text-decoration:none;text-decoration-line:none;letter-spacing:normal;word-spacing:normal;text-transform:none;direction:ltr;block-progression:tb;writing-mode:lr-tb;baseline-shift:baseline;text-anchor:start;display:inline;overflow:visible;visibility:visible;fill:#000;fill-opacity:1;stroke:none;stroke-width:309;marker:none" transform="translate(-35.43 63.89)"/><path d="M544.68 509.42c-10.07 4.23-16.3 14.38-19.9 24.3-8.73 25.36-7.46 52.64-10.58 78.88-4.54 27.7-16.08 53.94-19.87 81.85-.3 10.77-1.85 22.34 3.4 32.26 8 15.99 23.42 29.53 41.83 30.8 25.84 2.61 50.74-10.15 69.14-27.42 13.82-14 26.77-29.4 44.15-39.2 19.16-11.8 40.8-19.32 59.32-32.23 6.9-4.71 11.5-13.35 8.96-21.72-4.03-16.05-17.43-26.97-25.75-40.56-9.36-17.2-8.04-38.52-19.13-54.97-8.71-11.61-24.82-13.9-38.31-11.81-5.12 1.9-8.15 7.12-12.83 9.85-14.46 10.42-32.43 19.54-50.56 15.27-15.32-4.63-23.48-20.42-26.64-35.05-1.43-3.17.04-7.42-2.28-10.1q-.45-.21-.95-.15" style="fill:#ffd300;fill-opacity:1" transform="translate(-35.43 63.89)"/><path d="M545.03 509.38c-21.1 8.64-23.01 35.16-26.41 54.64-3.13 32.56-6.39 65.05-16.68 96.28-5.5 24.26-15.5 53.6 1.72 75.57 11.02 16.19 31.04 24.85 50.37 21.07 37.23-2.22 59.66-36.19 86.76-57.04 23.32-19.52 55.44-25.82 77.1-47.31 10.2-15.04-4.68-31.56-12.91-43.65-18.87-19.13-15.5-48.67-30.96-69.56-9.43-10.8-27.08-12.8-39.8-7.48-16.92 14.14-39.23 27.46-61.97 21.6-15.55-8.28-24.58-26.4-25.65-43.5a1.9 1.9 0 0 0-1.57-.62m-366.2 1.47c-21.98 1.5-31.08 25.75-45.46 38.95-15.68 16.4-46 14.29-55.33 37.3-6.08 24.45 8.51 49.95-2.09 73.82-4.03 14.1-8.7 35.7 7.86 43.81 34.03 12.49 72.28 8.16 105.6 23.65 25.89 11.54 53.6 23.71 82.68 18.47 25.4-1.75 46.32-25.84 42.78-51.41-4.67-28.01-27.06-48.5-42.83-70.75-27.47-31.48-35.91-76.01-67.9-103.9-7.05-5.91-15.92-10.27-25.31-9.94m197.41-378.34c-12.74 0-24.93 5.52-34.5 13.73-6.22 5.33-12.04 11.16-18.87 15.76-5.72 4.4-11.88 8.35-17.07 13.4-4.61 4.92-5.28 12.46-2.68 18.52 3.3 8.4 10.32 14.7 17.57 19.74 6.95 4.22 13.66 8.86 19.55 14.49 7.62 6.97 16.23 13.82 26.74 15.49 7.15 1.26 14.55.91 21.68-.3 12.87-2.68 23.78-10.37 34.77-17.14 5.27-3.47 11.29-5.4 17.13-7.63 12.1-5.08 23.93-13.06 29.84-25.16 3.37-6.64 3.27-15.2-1.43-21.2-6.19-8.2-16.46-11.36-25.63-14.96-8.97-3.35-18.39-5.89-26.4-11.32-12.2-7.61-26.1-13.55-40.7-13.42m78 48.44c.34 1.35.13 2.83.2 4.23-7.78 1.73-14.35 6.59-21.22 10.36-15.65 9.17-31.54 19.1-49.66 22.41-5.94.97-12.25.98-18.35.65-11.8-1.39-22.97-6.58-32.44-13.7-7.4-5.27-13.71-11.82-20.64-17.63.35-1.44-.03-3.44.39-4.53 4.53 2.31 7.69 6.36 11.6 9.56 8.95 8.36 19.08 15.8 30.83 19.65a45 45 0 0 0 17.8 2.61c10.41.39 20.52-3.08 29.95-7.18 15.03-6.65 28.54-16.25 43.11-23.7 2.67-1.24 5.48-2.4 8.43-2.73" style="fill:#fb0;fill-opacity:1" transform="translate(-35.43 63.89)"/><path d="M570.88 477.09c-7.04-.2-14.04 4.12-16.23 10.96-3.04 9.2-2.38 19.39.33 28.6 3.14 9.75 9.5 20.04 20.16 22.49 11.13 2.02 22.17-3.22 31-9.55 7.03-5.28 14.56-12.27 14.55-21.76-.24-8.77-8-14.62-14.81-18.82-10.66-6.1-22.46-11.87-35-11.92" style="fill:#000;fill-opacity:1" transform="translate(-35.43 63.89)"/><path d="M375.33 131.75c-14.45-.07-27.75 7.56-38.05 17.18-9.86 9.05-21.22 16.34-31.26 25.2-6.71 5.86-6.58 16.4-2.1 23.56 6.22 10.94 17.51 17.24 27.44 24.31 9.72 7.35 18.06 17.13 29.98 21.05 9.5 3.2 19.94 2.68 29.64.64 14.5-3.79 26.36-13.58 39.5-20.16 10.33-4.12 21.04-8.15 29.5-15.67 5.83-5.27 11.56-11.72 13.01-19.67.64-5.5.71-11.64-3.16-16.07-7.5-10.18-20.53-13.29-31.7-17.7-8.2-2.85-16.31-6.07-23.54-10.97-11.93-6.76-25.27-12.36-39.26-11.7m78.17 50.28c.03 1.18.52 2.91-1.24 2.74-12.7 5.03-23.97 13.08-36.02 19.35-13.26 7.42-27.7 13.8-43.13 13.61-14.88.98-29.07-5.6-40.77-14.3-6.54-5.14-12.7-10.73-18.97-16.2-.15-1.77-.3-3.97 1.6-1.71 11.49 9.81 22.49 21.02 37 26.29 9.76 4.25 20.64 4.03 31 2.87 20.19-3.93 37.48-15.84 55.14-25.69 4.9-2.64 10-5.88 15.4-6.96" style="fill:#ff9c00;fill-opacity:1" transform="translate(-35.43 63.89)"/></g></svg>
\ No newline at end of file
diff --git a/apps/web/public/videos/home-two/webcam-poster.jpg b/apps/web/public/videos/home-two/webcam-poster.jpg
deleted file mode 100644
index 4b6aa3b79..000000000
Binary files a/apps/web/public/videos/home-two/webcam-poster.jpg and /dev/null differ
diff --git a/apps/web/public/videos/home-two/webcam.mp4 b/apps/web/public/videos/home-two/webcam.mp4
deleted file mode 100644
index d56d9d384..000000000
Binary files a/apps/web/public/videos/home-two/webcam.mp4 and /dev/null differ
diff --git a/apps/web/public/videos/landing/presenter-avatar.webp b/apps/web/public/videos/landing/presenter-avatar.webp
new file mode 100644
index 000000000..8324aff4a
Binary files /dev/null and b/apps/web/public/videos/landing/presenter-avatar.webp differ
diff --git a/apps/web/public/videos/landing/presenter-poster.webp b/apps/web/public/videos/landing/presenter-poster.webp
new file mode 100644
index 000000000..5b8121130
Binary files /dev/null and b/apps/web/public/videos/landing/presenter-poster.webp differ
diff --git a/apps/web/public/videos/landing/presenter.mp4 b/apps/web/public/videos/landing/presenter.mp4
new file mode 100644
index 000000000..02e220752
Binary files /dev/null and b/apps/web/public/videos/landing/presenter.mp4 differ
diff --git a/apps/web/public/videos/landing/presenter.webm b/apps/web/public/videos/landing/presenter.webm
new file mode 100644
index 000000000..15c2f6bf0
Binary files /dev/null and b/apps/web/public/videos/landing/presenter.webm differ
diff --git a/apps/web/workflows/process-video.ts b/apps/web/workflows/process-video.ts
index 52cb4ca89..77bb20b63 100644
--- a/apps/web/workflows/process-video.ts
+++ b/apps/web/workflows/process-video.ts
@@ -177,6 +177,7 @@ async function startMediaServerProcessJob(
webhookSecret?: string;
inputExtension: string;
audioLevels?: boolean;
+ importedOriginal?: boolean;
},
): Promise<string> {
for (let attempt = 0; attempt < MEDIA_SERVER_START_MAX_ATTEMPTS; attempt++) {
@@ -350,6 +351,9 @@ async function processVideoOnMediaServer(
webhookUrl,
webhookSecret: webhookSecret || undefined,
inputExtension: getInputExtension(rawFileKey),
+ importedOriginal:
+ video.metadata?.loomDeferredProcessing?.rawFileKey === rawFileKey ||
+ undefined,
});
}
diff --git a/crates/audio/examples/single-track-mixing-benchmark.rs b/crates/audio/examples/single-track-mixing-benchmark.rs
new file mode 100644
index 000000000..efbc696d5
--- /dev/null
+++ b/crates/audio/examples/single-track-mixing-benchmark.rs
@@ -0,0 +1,97 @@
+use cap_audio::{
+ AudioData, AudioRendererTrack, AudioSampleSource, DecodedAudio, StereoMode, render_audio,
+};
+use std::{
+ collections::hash_map::DefaultHasher,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ sync::Arc,
+ time::Instant,
+};
+
+struct Indexed(DecodedAudio);
+impl AudioSampleSource for Indexed {
+ fn channels(&self) -> u16 {
+ self.0.channels()
+ }
+ fn sample_count(&self) -> usize {
+ self.0.sample_count()
+ }
+ fn sample(&self, index: usize) -> Option<&f32> {
+ self.0.sample(index)
+ }
+}
+
+fn source(channels: u16, seed: usize) -> DecodedAudio {
+ DecodedAudio::from(Arc::new(AudioData::from_samples(
+ (0..192_000 * usize::from(channels))
+ .map(|index| (((index * 97 + seed) % 1009) as f32 - 504.0) / 600.0)
+ .collect(),
+ channels,
+ )))
+}
+
+fn measure<T: AudioSampleSource>(name: &str, data: &[T], gain: f32, offset: isize, mode: u8) {
+ let tracks = data
+ .iter()
+ .map(|data| AudioRendererTrack {
+ data,
+ gain,
+ offset,
+ stereo_mode: match mode {
+ 1 => StereoMode::MonoL,
+ 2 => StereoMode::MonoR,
+ _ => StereoMode::Stereo,
+ },
+ })
+ .collect::<Vec<_>>();
+ for (frames, iterations) in [(64, 4000), (1024, 500), (4096, 200)] {
+ let mut output = vec![13.0_f32; frames * 2 + 4];
+ for sample in 0..18 {
+ let start = Instant::now();
+ let mut rendered = 0;
+ for call in 0..iterations {
+ let cursor = 256 + (call * frames) % (192_000 - frames - 512);
+ rendered += black_box(render_audio(
+ black_box(&tracks),
+ black_box(cursor),
+ black_box(frames),
+ 2,
+ black_box(&mut output),
+ ));
+ }
+ let elapsed = start.elapsed().as_nanos();
+ assert_eq!(rendered, iterations * frames);
+ assert_eq!(&output[..2], &[13.0; 2]);
+ assert_eq!(&output[output.len() - 2..], &[13.0; 2]);
+ if sample >= 3 {
+ let mut hash = DefaultHasher::new();
+ for value in &output {
+ value.to_bits().hash(&mut hash);
+ }
+ println!(
+ "MIX_BENCH {}",
+ serde_json::json!({
+ "case": name, "frames": frames, "sample": sample-3,
+ "ns_per_call": elapsed as f64 / iterations as f64,
+ "fingerprint": hash.finish(),
+ })
+ );
+ }
+ }
+ }
+}
+
+fn main() {
+ let stereo = [source(2, 0), source(2, 7)];
+ let mono = [source(1, 0)];
+ measure("one_stereo_unity", &stereo[..1], 0.0, 0, 0);
+ measure("one_stereo_gain", &stereo[..1], 6.0, 0, 0);
+ measure("two_stereo", &stereo, -6.0, 0, 0);
+ measure("mono", &mono, -6.0, 0, 0);
+ measure("offset", &stereo[..1], -6.0, -64, 0);
+ measure("left", &stereo[..1], -6.0, 0, 1);
+ measure("muted", &stereo[..1], -30.0, 0, 0);
+ let indexed = [Indexed(source(2, 0))];
+ measure("indexed", &indexed, -6.0, 0, 0);
+}
diff --git a/crates/audio/examples/transcription-assembly-benchmark.rs b/crates/audio/examples/transcription-assembly-benchmark.rs
index 04beef561..0050e351c 100644
--- a/crates/audio/examples/transcription-assembly-benchmark.rs
+++ b/crates/audio/examples/transcription-assembly-benchmark.rs
@@ -5,11 +5,22 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut args = std::env::args().skip(1);
let seconds: usize = args
.next()
- .ok_or_else(|| io::Error::other("Expected duration in seconds and mic or mic-system"))?
+ .ok_or_else(|| {
+ io::Error::other("Expected duration in seconds and mic, system or mic-system")
+ })?
.parse()?;
let mode = args.next().unwrap_or_else(|| "mic".to_string());
- if !matches!(mode.as_str(), "mic" | "mic-system") {
- return Err(io::Error::other("Expected mic or mic-system").into());
+ let repetitions: usize = args.next().unwrap_or_else(|| "25".into()).parse()?;
+ if seconds == 0
+ || seconds > 3600
+ || !matches!(mode.as_str(), "mic" | "system" | "mic-system")
+ || repetitions == 0
+ || args.next().is_some()
+ {
+ return Err(io::Error::other(
+ "Expected seconds (1..=3600), mic/system/mic-system, repetitions (>0)",
+ )
+ .into());
}
let rate = 48_000;
let frames = seconds * rate;
@@ -19,36 +30,50 @@ fn main() -> Result<(), Box<dyn Error>> {
.collect()
};
let mut sources = Vec::new();
- if mode == "mic-system" {
+ if mode == "mic-system" || mode == "system" {
sources.push(TranscriptionAudioSource {
samples: samples(2),
channels: 2,
offset_secs: 0.0,
});
}
- sources.push(TranscriptionAudioSource {
- samples: samples(1),
- channels: 1,
- offset_secs: if mode == "mic-system" { -0.125 } else { 0.0 },
- });
+ if mode != "system" {
+ sources.push(TranscriptionAudioSource {
+ samples: samples(1),
+ channels: 1,
+ offset_secs: if mode == "mic-system" { -0.125 } else { 0.0 },
+ });
+ }
let take = TranscriptionAudioTake {
display_duration_secs: seconds as f64,
sources,
};
- let mut output = Vec::new();
- let started = Instant::now();
- append_transcription_audio(black_box(&mut output), black_box(&take), rate as u32)
- .map_err(io::Error::other)?;
- let elapsed = started.elapsed();
- assert_eq!(output.len(), frames);
- let checksum = black_box(&output).iter().fold(0_u64, |hash, sample| {
- hash.wrapping_mul(31)
- .wrapping_add(u64::from(sample.to_bits()))
- });
- println!(
- "synthetic,{mode},{seconds}s,assembly_ms={:.3},samples={},checksum={checksum:016x}",
- elapsed.as_secs_f64() * 1000.0,
- output.len(),
- );
+ println!("mode,seconds,sample,assembly_ms,frames,fingerprint");
+ let mut expected_checksum = None;
+ for sample in 0..repetitions + 3 {
+ let mut output = Vec::new();
+ let started = Instant::now();
+ append_transcription_audio(black_box(&mut output), black_box(&take), rate as u32)
+ .map_err(io::Error::other)?;
+ let elapsed = started.elapsed();
+ assert_eq!(output.len(), frames);
+ let checksum = black_box(&output).iter().fold(0_u64, |hash, sample| {
+ hash.wrapping_mul(31)
+ .wrapping_add(u64::from(sample.to_bits()))
+ });
+ if let Some(expected) = expected_checksum {
+ assert_eq!(checksum, expected);
+ } else {
+ expected_checksum = Some(checksum);
+ }
+ if sample >= 3 {
+ println!(
+ "{mode},{seconds},{},{:.6},{},{checksum:016x}",
+ sample - 3,
+ elapsed.as_secs_f64() * 1000.0,
+ output.len(),
+ );
+ }
+ }
Ok(())
}
diff --git a/crates/audio/examples/voice-startup-benchmark.rs b/crates/audio/examples/voice-startup-benchmark.rs
new file mode 100644
index 000000000..4c5459f6e
--- /dev/null
+++ b/crates/audio/examples/voice-startup-benchmark.rs
@@ -0,0 +1,91 @@
+use cap_audio::{AudioSampleSource, VoiceEnhancer, VoiceProfile};
+use std::{
+ collections::hash_map::DefaultHasher,
+ error::Error,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ time::Instant,
+};
+
+struct Source {
+ samples: Vec<f32>,
+ channels: u16,
+}
+impl AudioSampleSource for Source {
+ fn channels(&self) -> u16 {
+ self.channels
+ }
+ fn sample_count(&self) -> usize {
+ self.samples.len() / usize::from(self.channels)
+ }
+ fn sample(&self, index: usize) -> Option<&f32> {
+ self.samples.get(index)
+ }
+}
+
+fn main() -> Result<(), Box<dyn Error>> {
+ ffmpeg::init()?;
+ println!("channels,profile,case,frames,sample,elapsed_ms,fingerprint");
+ for channels in [1, 2] {
+ let source = Source {
+ channels,
+ samples: (0..96_000 * usize::from(channels))
+ .map(|index| {
+ let seed = (index as u32)
+ .wrapping_mul(1_664_525)
+ .wrapping_add(1_013_904_223);
+ let time = (index / usize::from(channels)) as f32 / 48_000.0;
+ (std::f32::consts::TAU * 180.0 * time).sin() * 0.12
+ + (seed as f64 / u32::MAX as f64 * 0.04 - 0.02) as f32
+ })
+ .collect(),
+ };
+ for profile in [false, true] {
+ let create = || {
+ if profile {
+ VoiceEnhancer::with_settings(channels, 0.9, VoiceProfile::default())
+ } else {
+ VoiceEnhancer::new(channels)
+ }
+ };
+ for (case, frames) in [
+ ("startup", 1),
+ ("startup", 1_024),
+ ("startup", 4_096),
+ ("startup", 24_000),
+ ("seek", 4_096),
+ ("fresh-seek", 4_096),
+ ] {
+ for sample in 0..28 {
+ let mut prepared = (case == "seek").then(|| {
+ let mut enhancer = create();
+ black_box(enhancer.render(&source, 0, 4_096));
+ enhancer
+ });
+ let started = Instant::now();
+ let mut enhancer = prepared.take().unwrap_or_else(create);
+ let audio = black_box(enhancer.render(
+ black_box(&source),
+ if case.ends_with("seek") { 48_000 } else { 0 },
+ frames,
+ ));
+ let elapsed_ms = started.elapsed().as_secs_f64() * 1_000.0;
+ assert_eq!(audio.samples().len(), frames * usize::from(channels));
+ let mut hash = DefaultHasher::new();
+ for value in audio.samples() {
+ value.to_bits().hash(&mut hash);
+ }
+ enhancer.speech_frames().hash(&mut hash);
+ if sample >= 3 {
+ println!(
+ "{channels},{profile},{case},{frames},{},{elapsed_ms:.6},{}",
+ sample - 3,
+ hash.finish()
+ );
+ }
+ }
+ }
+ }
+ }
+ Ok(())
+}
diff --git a/crates/audio/examples/waveform-tail-benchmark.rs b/crates/audio/examples/waveform-tail-benchmark.rs
new file mode 100644
index 000000000..052a5137a
--- /dev/null
+++ b/crates/audio/examples/waveform-tail-benchmark.rs
@@ -0,0 +1,84 @@
+use cap_audio::waveform_peaks;
+use std::{
+ collections::hash_map::DefaultHasher,
+ error::Error,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ time::Instant,
+};
+
+#[inline(never)]
+fn peaks(blocks: &[&[f32]], channels: u16) -> Vec<f32> {
+ waveform_peaks(blocks.iter().copied(), channels)
+}
+
+fn main() -> Result<(), Box<dyn Error>> {
+ let mut arguments = std::env::args().skip(1);
+ let seconds: usize = arguments.next().unwrap_or_else(|| "600".into()).parse()?;
+ let channels: u16 = arguments.next().unwrap_or_else(|| "1".into()).parse()?;
+ let samples: usize = arguments.next().unwrap_or_else(|| "25".into()).parse()?;
+ if seconds == 0
+ || seconds > 3600
+ || !(1..=2).contains(&channels)
+ || samples == 0
+ || arguments.next().is_some()
+ {
+ return Err("expected seconds (1..=3600), channels (1..=2), samples (>0)".into());
+ }
+ let mut state = 0x1234_5678_u32;
+ let source: Vec<f32> = (0..seconds * 48_000 * usize::from(channels))
+ .map(|_| {
+ state ^= state << 13;
+ state ^= state >> 17;
+ state ^= state << 5;
+ state as f32 / u32::MAX as f32 * 2.0 - 1.0
+ })
+ .collect();
+ let window = 4800 * usize::from(channels);
+ let expected: Vec<f32> = source
+ .chunks(window)
+ .map(|chunk| {
+ let average =
+ chunk.iter().fold(0.0_f32, |sum, value| sum + value.abs()) / chunk.len() as f32;
+ if average > 0.0 {
+ 20.0 * average.log10()
+ } else {
+ -60.0
+ }
+ })
+ .collect();
+ let expected_bits: Vec<_> = expected.iter().map(|value| value.to_bits()).collect();
+ println!("seconds,channels,layout,sample,elapsed_ms,fingerprint");
+ for layout in ["contiguous", "decoded-blocks"] {
+ let owned: Vec<Vec<f32>> = if layout == "decoded-blocks" {
+ source
+ .chunks(32768 * usize::from(channels))
+ .map(<[f32]>::to_vec)
+ .collect()
+ } else {
+ Vec::new()
+ };
+ let blocks: Vec<&[f32]> = if layout == "decoded-blocks" {
+ owned.iter().map(Vec::as_slice).collect()
+ } else {
+ vec![&source]
+ };
+ for sample in 0..samples + 3 {
+ let started = Instant::now();
+ let actual = black_box(peaks(black_box(&blocks), black_box(channels)));
+ let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
+ let bits: Vec<_> = actual.iter().map(|value| value.to_bits()).collect();
+ assert_eq!(bits, expected_bits);
+ let mut hash = DefaultHasher::new();
+ bits.hash(&mut hash);
+ if sample >= 3 {
+ println!(
+ "{seconds},{channels},{layout},{},{elapsed_ms:.6},{}",
+ sample - 3,
+ hash.finish()
+ );
+ }
+ }
+ }
+ Ok(())
+}
diff --git a/crates/audio/src/progressive.rs b/crates/audio/src/progressive.rs
index 702524e85..bd44bcefc 100644
--- a/crates/audio/src/progressive.rs
+++ b/crates/audio/src/progressive.rs
@@ -31,6 +31,10 @@ pub fn waveform_peaks<'a>(slices: impl IntoIterator<Item = &'a [f32]>, channels:
let (group, rest) = slice.split_at(block_samples * WAVEFORM_PEAK_LANES);
push_waveform_peak_lanes(&mut peaks, group, block_samples);
slice = rest;
+ } else if count == 0 && slice.len() >= block_samples * 2 {
+ let (group, rest) = slice.split_at(block_samples * 2);
+ push_waveform_peak_pair(&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());
@@ -57,6 +61,18 @@ pub fn waveform_peaks<'a>(slices: impl IntoIterator<Item = &'a [f32]>, channels:
peaks
}
+fn push_waveform_peak_pair(peaks: &mut Vec<f32>, group: &[f32], block_samples: usize) {
+ let (first, second) = group.split_at(block_samples);
+ let mut first_sum = 0.0_f32;
+ let mut second_sum = 0.0_f32;
+ for index in 0..block_samples {
+ first_sum += first[index].abs();
+ second_sum += second[index].abs();
+ }
+ peaks.push(first_sum / block_samples as f32);
+ peaks.push(second_sum / block_samples as f32);
+}
+
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);
@@ -1273,6 +1289,49 @@ mod tests {
}
}
+ #[test]
+ fn waveform_pairs_match_sequential_bits_at_window_and_block_edges() {
+ for channels in [0, 1, 2, 6] {
+ let window = 4_800 * usize::from(channels.max(1));
+ for length in [
+ 2 * window - 1,
+ 2 * window,
+ 2 * window + 1,
+ 3 * window + 7,
+ 6 * window + 17,
+ ] {
+ for nonfinite in [false, true] {
+ let mut source = xorshift_samples(length);
+ if nonfinite {
+ for (index, value) in [
+ (0, -0.0),
+ (window - 1, f32::from_bits(0x7fc0_0123)),
+ (window, f32::INFINITY),
+ (window + 1, f32::NEG_INFINITY),
+ ] {
+ source[index] = value;
+ }
+ }
+ let expected = sequential_waveform_peaks(&source, channels);
+ for block in [
+ 2 * window,
+ 2 * window + 1,
+ 6 * window,
+ 32_768 * usize::from(channels.max(1)),
+ ] {
+ let slices: Vec<_> = source.chunks(block).collect();
+ assert_peak_bits(
+ &waveform_peaks(slices.iter().copied(), channels),
+ &expected,
+ channels,
+ block,
+ );
+ }
+ }
+ }
+ }
+ }
+
#[test]
fn waveform_peaks_match_a_sequential_sum_across_uneven_blocks() {
let samples = xorshift_samples(48_000 * 3 + 1_234);
diff --git a/crates/audio/src/renderer.rs b/crates/audio/src/renderer.rs
index f1fd08985..7bfbb86d4 100644
--- a/crates/audio/src/renderer.rs
+++ b/crates/audio/src/renderer.rs
@@ -68,6 +68,35 @@ pub fn render_audio<T: AudioSampleSource>(
return 0;
}
let out = &mut out[out_offset..out_offset + samples * 2];
+ if let [track] = tracks
+ && matches!(track.stereo_mode, StereoMode::Stereo)
+ && track.data.channels() == 2
+ {
+ let gain = gain_for_db(track.gain);
+ if gain == f32::NEG_INFINITY {
+ out.fill(0.0);
+ return samples;
+ }
+ if let Ok(first) = usize::try_from(offset as i128 + track.offset as i128)
+ && let Some(first) = first.checked_mul(2)
+ && let Some(end) = first.checked_add(out.len())
+ && let Some(input) = track.data.sample_slice(first..end)
+ && input.len() == out.len()
+ {
+ let mut index = 0;
+ while index < out.len() {
+ out[index] = (0.0 + input[index] * gain).clamp(-1.0, 1.0);
+ index += 1;
+ }
+ return samples;
+ }
+ }
+ if let [track] = tracks
+ && track.data.channels() == 1
+ && try_render_single_mono(track, offset, out)
+ {
+ return samples;
+ }
out.fill(0.0);
for track in tracks {
@@ -136,6 +165,37 @@ pub fn render_audio<T: AudioSampleSource>(
samples
}
+#[inline(never)]
+fn try_render_single_mono<T: AudioSampleSource>(
+ track: &AudioRendererTrack<'_, T>,
+ offset: usize,
+ out: &mut [f32],
+) -> bool {
+ if track.gain <= -30.0 {
+ out.fill(0.0);
+ return true;
+ }
+ let Ok(first) = usize::try_from(offset as i128 + track.offset as i128) else {
+ return false;
+ };
+ let samples = out.len() / 2;
+ let Some(end) = first.checked_add(samples) else {
+ return false;
+ };
+ let Some(input) = track.data.sample_slice(first..end) else {
+ return false;
+ };
+ if input.len() != samples {
+ return false;
+ }
+ let gain = gain_for_db(track.gain);
+ for (sample, frame) in input.iter().zip(out.chunks_exact_mut(2)) {
+ let sample = (0.0 + sample * 0.707 * gain).clamp(-1.0, 1.0);
+ frame.fill(sample);
+ }
+ true
+}
+
#[inline(always)]
fn mix_stereo_slice(
input: &[f32],
@@ -258,6 +318,208 @@ mod tests {
}
}
+ fn assert_single_track_mixing_matches_indexed_samples(channels: u16) {
+ let data = AudioData::from_raw_f32(
+ vec![
+ 0.0,
+ -0.0,
+ -2.0,
+ 2.0,
+ f32::NEG_INFINITY,
+ f32::INFINITY,
+ f32::from_bits(0x7fc0_1234),
+ f32::from_bits(1),
+ -f32::from_bits(1),
+ 0.25,
+ -0.25,
+ 1.0,
+ -1.0,
+ 0.5,
+ ],
+ channels,
+ );
+ for gain in [
+ f32::NEG_INFINITY,
+ -31.0,
+ -30.0,
+ -6.0,
+ 0.0,
+ 6.0,
+ f32::INFINITY,
+ f32::NAN,
+ ] {
+ for offset in [-3, 0, 2] {
+ for cursor in [0, 1, 4, 100] {
+ for count in [0, 1, 3, 16] {
+ for mode in [0, 1, 2] {
+ let render = |slices| {
+ let source = Source {
+ data: &data,
+ slices,
+ };
+ let track = AudioRendererTrack {
+ data: &source,
+ gain,
+ offset,
+ stereo_mode: match mode {
+ 1 => StereoMode::MonoL,
+ 2 => StereoMode::MonoR,
+ _ => StereoMode::Stereo,
+ },
+ };
+ let mut output = [13.0_f32; 36];
+ let rendered =
+ render_audio(&[track], cursor, count, 2, &mut output);
+ (rendered, output)
+ };
+ let (expected_count, expected) = render(false);
+ let (actual_count, actual) = render(true);
+ assert_eq!(actual_count, expected_count);
+ for (actual, expected) in actual.into_iter().zip(expected) {
+ if expected.is_nan() {
+ assert!(actual.is_nan());
+ } else {
+ assert_eq!(actual.to_bits(), expected.to_bits());
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn single_stereo_mixing_matches_indexed_samples_at_boundaries() {
+ assert_single_track_mixing_matches_indexed_samples(2);
+ }
+
+ #[test]
+ fn single_mono_mixing_matches_indexed_samples_at_boundaries() {
+ assert_single_track_mixing_matches_indexed_samples(1);
+ }
+
+ #[test]
+ fn partial_stereo_slices_keep_the_unavailable_tail_silent() {
+ struct PartialSlice;
+ impl AudioSampleSource for PartialSlice {
+ fn channels(&self) -> u16 {
+ 2
+ }
+ fn sample_count(&self) -> usize {
+ 4
+ }
+ fn sample(&self, _index: usize) -> Option<&f32> {
+ None
+ }
+ fn sample_slice(&self, _range: Range<usize>) -> Option<&[f32]> {
+ Some(&[0.25, -0.5, 0.75, -2.0])
+ }
+ }
+ let tracks = [AudioRendererTrack {
+ data: &PartialSlice,
+ gain: 0.0,
+ offset: 0,
+ stereo_mode: StereoMode::Stereo,
+ }];
+ let mut output = [13.0_f32; 12];
+ assert_eq!(render_audio(&tracks, 0, 4, 2, &mut output), 4);
+ assert_eq!(
+ output,
+ [
+ 13.0, 13.0, 0.25, -0.5, 0.75, -1.0, 0.0, 0.0, 0.0, 0.0, 13.0, 13.0
+ ]
+ );
+ }
+
+ #[test]
+ fn partial_mono_slices_keep_the_unavailable_tail_silent() {
+ struct PartialSlice;
+ impl AudioSampleSource for PartialSlice {
+ fn channels(&self) -> u16 {
+ 1
+ }
+ fn sample_count(&self) -> usize {
+ 4
+ }
+ fn sample(&self, _index: usize) -> Option<&f32> {
+ None
+ }
+ fn sample_slice(&self, _range: Range<usize>) -> Option<&[f32]> {
+ Some(&[0.25, -0.5])
+ }
+ }
+ let tracks = [AudioRendererTrack {
+ data: &PartialSlice,
+ gain: 0.0,
+ offset: 0,
+ stereo_mode: StereoMode::Stereo,
+ }];
+ let mut output = [13.0_f32; 12];
+ assert_eq!(render_audio(&tracks, 0, 4, 2, &mut output), 4);
+ assert_eq!(
+ output,
+ [
+ 13.0,
+ 13.0,
+ 0.25 * 0.707,
+ 0.25 * 0.707,
+ -0.5 * 0.707,
+ -0.5 * 0.707,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 13.0,
+ 13.0
+ ]
+ );
+ }
+
+ fn assert_muted_tracks_do_not_read_samples(channels: u16) {
+ struct Muted(u16);
+ impl AudioSampleSource for Muted {
+ fn channels(&self) -> u16 {
+ self.0
+ }
+ fn sample_count(&self) -> usize {
+ 4
+ }
+ fn sample(&self, _index: usize) -> Option<&f32> {
+ panic!("muted sample read")
+ }
+ fn sample_slice(&self, _range: Range<usize>) -> Option<&[f32]> {
+ panic!("muted slice read")
+ }
+ }
+ for gain in [f32::NEG_INFINITY, -31.0, -30.0] {
+ let tracks = [AudioRendererTrack {
+ data: &Muted(channels),
+ gain,
+ offset: 0,
+ stereo_mode: StereoMode::Stereo,
+ }];
+ let mut output = [13.0_f32; 12];
+ assert_eq!(render_audio(&tracks, 0, 4, 2, &mut output), 4);
+ assert_eq!(
+ output,
+ [
+ 13.0, 13.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 13.0, 13.0
+ ]
+ );
+ }
+ }
+
+ #[test]
+ fn muted_stereo_tracks_do_not_read_samples() {
+ assert_muted_tracks_do_not_read_samples(2);
+ }
+
+ #[test]
+ fn muted_mono_tracks_do_not_read_samples() {
+ assert_muted_tracks_do_not_read_samples(1);
+ }
+
#[test]
fn contiguous_and_indexed_mixing_preserve_sample_bits_and_output_bounds() {
let data = [
diff --git a/crates/audio/src/transcription_timing.rs b/crates/audio/src/transcription_timing.rs
index 601a32573..f2552e0d2 100644
--- a/crates/audio/src/transcription_timing.rs
+++ b/crates/audio/src/transcription_timing.rs
@@ -105,7 +105,7 @@ pub fn append_transcription_audio(
let mut count = 0_u32;
let mut start = 0;
for (end, starting) in boundaries {
- if count > 0 {
+ if count > 1 {
for sample in &mut sums[start..end] {
*sample /= count as f32;
}
@@ -322,6 +322,47 @@ mod tests {
}
}
+ #[test]
+ fn single_source_intervals_preserve_special_samples_and_existing_output() {
+ let values = [
+ 0.0,
+ -0.0,
+ f32::from_bits(1),
+ -f32::from_bits(1),
+ f32::MIN_POSITIVE,
+ f32::MAX,
+ f32::INFINITY,
+ f32::NEG_INFINITY,
+ f32::from_bits(0x7fc0_1234),
+ f32::from_bits(0x7f80_1234),
+ ];
+ for channels in [1, 2, 6] {
+ let samples: Vec<_> = values.into_iter().cycle().take(20 * channels).collect();
+ for offset in [-3.0, -1.25, 0.0, 0.5, 2.0] {
+ let take = take(
+ 10.0,
+ vec![
+ source(samples.clone(), channels, offset),
+ source(samples.clone(), channels, -6.0),
+ ],
+ );
+ let mut actual = vec![-0.0, f32::from_bits(0x7fc0_4321)];
+ let prefix = bits(&actual);
+ append_transcription_audio(&mut actual, &take, 4).unwrap();
+ assert_eq!(bits(&actual[..2]), prefix);
+ let expected = reference(&take, 4);
+ assert_eq!(actual.len() - 2, expected.len());
+ for (actual, expected) in actual[2..].iter().zip(expected) {
+ if expected.is_nan() {
+ assert!(actual.is_nan());
+ } else {
+ assert_eq!(actual.to_bits(), expected.to_bits());
+ }
+ }
+ }
+ }
+ }
+
#[test]
fn adjacent_overlapping_and_disjoint_source_boundaries_keep_exact_averages() {
let take = take(
diff --git a/crates/audio/src/voice.rs b/crates/audio/src/voice.rs
index f4013c418..049002b16 100644
--- a/crates/audio/src/voice.rs
+++ b/crates/audio/src/voice.rs
@@ -111,7 +111,9 @@ impl VoiceEnhancer {
self.pending_start = self.next_input;
self.pending.clear();
self.previous.fill(0.0);
- self.states = (0..channels).map(|_| DenoiseState::new()).collect();
+ if self.expected_start.is_some() {
+ self.states = (0..channels).map(|_| DenoiseState::new()).collect();
+ }
self.speech_frames = 0;
self.leveler = self.profile.and_then(|profile| {
VoiceFilter::new(channels as u16, &profile.filter(channels as u16))
@@ -319,6 +321,70 @@ mod tests {
assert_eq!(after_seek.samples(), fresh.samples());
}
+ #[test]
+ fn initial_state_reuse_matches_forced_reset_after_empty_reads_and_seeks() {
+ ffmpeg::init().unwrap();
+ for channels in [1, 2] {
+ let source = signal(96_001, channels);
+ for profile in [false, true] {
+ let create = || {
+ if profile {
+ VoiceEnhancer::with_settings(channels, 0.9, VoiceProfile::default())
+ } else {
+ VoiceEnhancer::new(channels)
+ }
+ };
+ for start in [0, 27_017] {
+ for count in [1, 4_096] {
+ let mut actual = create();
+ let mut reference = create();
+ reference.expected_start = Some(usize::MAX);
+ for empty_start in [0, 50_000] {
+ assert!(actual.render(&source, empty_start, 0).samples().is_empty());
+ assert!(
+ reference
+ .render(&source, empty_start, 0)
+ .samples()
+ .is_empty()
+ );
+ }
+ let first = actual.render(&source, start, count);
+ let expected = reference.render(&source, start, count);
+ assert_eq!(
+ first
+ .samples()
+ .iter()
+ .map(|value| value.to_bits())
+ .collect::<Vec<_>>(),
+ expected
+ .samples()
+ .iter()
+ .map(|value| value.to_bits())
+ .collect::<Vec<_>>()
+ );
+ assert_eq!(actual.speech_frames(), reference.speech_frames());
+ let seek = actual.render(&source, 48_017, 997);
+ let mut fresh = create();
+ fresh.expected_start = Some(usize::MAX);
+ let expected = fresh.render(&source, 48_017, 997);
+ assert_eq!(
+ seek.samples()
+ .iter()
+ .map(|value| value.to_bits())
+ .collect::<Vec<_>>(),
+ expected
+ .samples()
+ .iter()
+ .map(|value| value.to_bits())
+ .collect::<Vec<_>>()
+ );
+ assert_eq!(actual.speech_frames(), fresh.speech_frames());
+ }
+ }
+ }
+ }
+ }
+
#[test]
fn leveling_preserves_chunk_boundaries_timing_and_peak_headroom() {
ffmpeg::init().unwrap();
diff --git a/crates/editor/examples/imported-music-playback-benchmark.rs b/crates/editor/examples/imported-music-playback-benchmark.rs
new file mode 100644
index 000000000..da8a12503
--- /dev/null
+++ b/crates/editor/examples/imported-music-playback-benchmark.rs
@@ -0,0 +1,152 @@
+use cap_editor::{AudioOutput, EditorFrameOutput, EditorInstance, PlaybackEvent};
+use cap_project::{
+ AudioTrackSegment, ClipConfiguration, ProjectConfiguration, RecordingMeta, TimelineSegment, XY,
+};
+use std::{
+ path::PathBuf,
+ sync::Arc,
+ time::{Duration, Instant},
+};
+
+#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
+async fn main() {
+ ffmpeg::init().unwrap();
+ let root = PathBuf::from(std::env::args_os().nth(1).expect("fixture directory"));
+ let count: usize = std::env::args()
+ .nth(2)
+ .unwrap_or("2".into())
+ .parse()
+ .unwrap();
+ assert!((1..=2).contains(&count));
+ let mut project = ProjectConfiguration {
+ timeline: Some(
+ serde_json::from_value(serde_json::json!({"segments":[],"zoomSegments":[]})).unwrap(),
+ ),
+ ..Default::default()
+ };
+ project.camera.hide = true;
+ project.cursor.hide = true;
+ project.clips = vec![ClipConfiguration {
+ index: 0,
+ ..Default::default()
+ }];
+ let timeline = project.timeline.as_mut().unwrap();
+ timeline.segments = vec![TimelineSegment {
+ recording_clip: 0,
+ start: 0.0,
+ end: 1200.0,
+ timescale: 1.0,
+ ..Default::default()
+ }];
+ for path in ["music-a.m4a", "music-b.m4a"].into_iter().take(count) {
+ timeline.audio_segments.push(AudioTrackSegment {
+ start: 0.0,
+ end: 1200.0,
+ track: 0,
+ path: path.into(),
+ name: None,
+ enabled: true,
+ trim_start: 0.0,
+ volume_db: -12.0,
+ fade_in: 0.0,
+ fade_out: 0.0,
+ duration: Some(3600.0),
+ });
+ }
+ let delayed_cold = std::env::args().nth(3).as_deref() == Some("delayed-cold");
+ let playback_project = project.clone();
+ if delayed_cold {
+ project.timeline.as_mut().unwrap().audio_segments.clear();
+ }
+ project.write(&root).unwrap();
+ let mut meta: RecordingMeta = serde_json::from_value(serde_json::json!({
+ "platform":"Linux","pretty_name":"Imported music playback benchmark","sharing":null,
+ "segments":[{"display":{"path":"display.mp4","fps":1,"start_time":0.0}}],
+ "cursors":{},"status":{"status":"Complete"}
+ }))
+ .unwrap();
+ meta.project_path = root.clone();
+ meta.save_for_project().unwrap();
+ let (audio_tx, mut audio_rx) = tokio::sync::mpsc::unbounded_channel();
+ let output = Arc::new(AudioOutput::new_headless(Box::new(move |samples, _| {
+ if samples.iter().any(|sample| sample.abs() > 0.00001) {
+ let _ = audio_tx.send(Instant::now());
+ }
+ })));
+ let (frame_tx, mut frame_rx) = tokio::sync::mpsc::unbounded_channel();
+ let opened = Instant::now();
+ let editor = EditorInstance::new_with_audio_output(
+ root,
+ |_| {},
+ Box::new(move |frame, _| {
+ let number = match frame {
+ EditorFrameOutput::Rgba(frame) => frame.frame_number,
+ EditorFrameOutput::Nv12(frame) => frame.frame_number,
+ #[cfg(target_os = "macos")]
+ EditorFrameOutput::Surface(frame) => frame.frame_number,
+ };
+ let _ = frame_tx.send((number, Instant::now()));
+ }),
+ None,
+ output,
+ )
+ .await
+ .unwrap();
+ let open_seconds = opened.elapsed().as_secs_f64();
+ if delayed_cold {
+ tokio::time::sleep(Duration::from_millis(250)).await;
+ editor.project_config.0.send(playback_project).unwrap();
+ tokio::time::sleep(Duration::from_millis(250)).await;
+ }
+ for phase in [
+ if delayed_cold {
+ "delayed-cold"
+ } else {
+ "immediate"
+ },
+ "warm",
+ ] {
+ while audio_rx.try_recv().is_ok() {}
+ while frame_rx.try_recv().is_ok() {}
+ let start_frame = if phase == "warm" { 900 } else { 0 };
+ let started = Instant::now();
+ let mut handle = tokio::time::timeout(
+ Duration::from_secs(30),
+ editor.start_playback_with_handle(15, XY::new(640, 360), Some(start_frame)),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ let start_return_seconds = started.elapsed().as_secs_f64();
+ let first_audio = tokio::time::timeout(Duration::from_secs(20), audio_rx.recv())
+ .await
+ .unwrap()
+ .unwrap();
+ let (observed_frame, first_frame) = tokio::time::timeout(Duration::from_secs(20), async {
+ loop {
+ let (number, timestamp) = frame_rx.recv().await.unwrap();
+ if number >= start_frame && number < start_frame + 150 {
+ break (number, timestamp);
+ }
+ }
+ })
+ .await
+ .unwrap();
+ println!(
+ "MUSIC_PLAYBACK {}",
+ serde_json::json!({"tracks":count,"phase":phase,"start_frame":start_frame,"observed_frame":observed_frame,"open_seconds":open_seconds,"start_return_seconds":start_return_seconds,"first_audio_seconds":first_audio.saturating_duration_since(started).as_secs_f64(),"first_frame_seconds":first_frame.saturating_duration_since(started).as_secs_f64(),"open_to_audio_seconds":first_audio.saturating_duration_since(opened).as_secs_f64()})
+ );
+ handle.stop();
+ tokio::time::timeout(Duration::from_secs(5), async {
+ loop {
+ if matches!(*handle.receive_event().await, PlaybackEvent::Stop) {
+ break;
+ }
+ }
+ })
+ .await
+ .unwrap();
+ tokio::time::sleep(Duration::from_millis(250)).await;
+ }
+ editor.dispose().await;
+}
diff --git a/crates/editor/examples/zoom-seek-preview-benchmark.rs b/crates/editor/examples/zoom-seek-preview-benchmark.rs
new file mode 100644
index 000000000..957c27171
--- /dev/null
+++ b/crates/editor/examples/zoom-seek-preview-benchmark.rs
@@ -0,0 +1,135 @@
+use cap_editor::{EditorFrameOutput, EditorInstance};
+use cap_project::{
+ ClipConfiguration, CursorEvents, CursorMoveEvent, GlideDirection, ProjectConfiguration,
+ RecordingMeta, TimelineSegment, XY, ZoomMode, ZoomSegment,
+};
+use std::{
+ collections::hash_map::DefaultHasher,
+ hash::{Hash, Hasher},
+ path::PathBuf,
+ sync::mpsc,
+ time::{Duration, Instant},
+};
+
+#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
+async fn main() {
+ let root = PathBuf::from(std::env::args_os().nth(1).expect("fixture directory"));
+ let duration: f64 = std::env::var("CAP_ZOOM_DURATION").map_or(3600.0, |v| v.parse().unwrap());
+ let clips: usize = std::env::var("CAP_ZOOM_CLIPS").map_or(1000, |v| v.parse().unwrap());
+ let mut project = ProjectConfiguration {
+ timeline: Some(
+ serde_json::from_value(serde_json::json!({"segments":[],"zoomSegments":[]})).unwrap(),
+ ),
+ ..Default::default()
+ };
+ project.camera.hide = true;
+ project.cursor.hide = true;
+ project.cursor.raw = true;
+ project.clips = (0..4)
+ .map(|index| ClipConfiguration {
+ index,
+ ..Default::default()
+ })
+ .collect();
+ let timeline = project.timeline.as_mut().unwrap();
+ let length = duration / clips as f64;
+ timeline.segments = (0..clips)
+ .map(|index| TimelineSegment {
+ recording_clip: (index % 4) as u32,
+ start: (index / 4) as f64 * length,
+ end: ((index / 4) + 1) as f64 * length,
+ timescale: 1.0,
+ ..Default::default()
+ })
+ .collect();
+ timeline.zoom_segments = (0..30)
+ .map(|index| ZoomSegment {
+ start: index as f64 * duration / 30.0,
+ end: (index as f64 + 0.85) * duration / 30.0,
+ amount: 2.0,
+ mode: ZoomMode::Auto,
+ glide_direction: GlideDirection::default(),
+ glide_speed: 0.5,
+ instant_animation: false,
+ edge_snap_ratio: 0.25,
+ })
+ .collect();
+ project.write(&root).unwrap();
+ let cursor = CursorEvents {
+ moves: (0..=(duration / 4.0 * 60.0) as usize)
+ .map(|index| CursorMoveEvent {
+ time_ms: index as f64 * 1000.0 / 60.0,
+ x: 0.2 + (index % 700) as f64 / 1200.0,
+ y: 0.2 + (index % 900) as f64 / 1500.0,
+ cursor_id: "default".into(),
+ active_modifiers: Vec::new(),
+ })
+ .collect(),
+ ..Default::default()
+ };
+ std::fs::write(
+ root.join("cursor.json"),
+ serde_json::to_vec(&cursor).unwrap(),
+ )
+ .unwrap();
+ let mut meta: RecordingMeta = serde_json::from_value(serde_json::json!({
+ "platform":"Linux", "pretty_name":"Dense zoom seek benchmark", "sharing":null,
+ "segments":(0..4).map(|_|serde_json::json!({"display":{"path":"display.mp4","fps":1,"start_time":0.0},"cursor":"cursor.json"})).collect::<Vec<_>>(),
+ "cursors":{},"status":{"status":"Complete"}
+ })).unwrap();
+ meta.project_path = root.clone();
+ meta.save_for_project().unwrap();
+ let (tx, rx) = mpsc::channel();
+ let startup = Instant::now();
+ let editor = EditorInstance::new(
+ root,
+ |_| {},
+ Box::new(move |output, _| {
+ if let EditorFrameOutput::Rgba(frame) = output {
+ let _ = tx.send((Instant::now(), frame));
+ }
+ }),
+ None,
+ )
+ .await
+ .expect("editor startup");
+ let setup_seconds = startup.elapsed().as_secs_f64();
+ editor
+ .preview_tx
+ .send_modify(|value| *value = Some((0, 30, XY::new(640, 360))));
+ loop {
+ let (_, frame) = rx
+ .recv_timeout(Duration::from_secs(60))
+ .expect("initial frame");
+ if frame.frame_number == 0 {
+ break;
+ }
+ }
+ let target = (duration * 0.95 * 30.0) as u32;
+ let start = Instant::now();
+ editor
+ .preview_tx
+ .send_modify(|value| *value = Some((target, 30, XY::new(640, 360))));
+ let (ready, frame) = loop {
+ let value = rx
+ .recv_timeout(Duration::from_secs(60))
+ .expect("late seek frame");
+ if value.1.frame_number == target {
+ break value;
+ }
+ };
+ let elapsed = ready.saturating_duration_since(start).as_secs_f64();
+ let mut hash = DefaultHasher::new();
+ for row in frame
+ .data
+ .chunks_exact(frame.padded_bytes_per_row as usize)
+ .take(frame.height as usize)
+ {
+ row[..frame.width as usize * 4].hash(&mut hash);
+ }
+ println!(
+ "PREVIEW_BENCH {}",
+ serde_json::json!({"duration":duration,"clips":clips,"startup_seconds":setup_seconds,"seek_seconds":elapsed,"fingerprint":hash.finish(),"width":frame.width,"height":frame.height})
+ );
+ editor.dispose().await;
+}
diff --git a/crates/editor/src/audio.rs b/crates/editor/src/audio.rs
index 278a40fa6..f793e41d6 100644
--- a/crates/editor/src/audio.rs
+++ b/crates/editor/src/audio.rs
@@ -2192,10 +2192,10 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> PrerenderedAudioBuffer<T> {
output_info,
duration_secs,
start_playhead_secs,
- estimated_output_samples,
+ cache.filter(|_| plan.as_ref().is_some_and(|plan| plan.shift == 0)),
);
match cache {
- Some(cache) => fresh.retain_in_cache(cache, plan),
+ Some(cache) => fresh.publish_in_cache(cache, plan),
None => fresh,
}
}
@@ -2339,10 +2339,15 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
output_info: AudioInfo,
duration_secs: f64,
start_playhead_secs: f64,
- estimated_output_samples: usize,
+ cache: Option<&PlaybackAudioCache>,
) -> Self {
// Clamp output info for FFmpeg compatibility (max 8 channels)
let output_info = output_info.for_ffmpeg_output();
+ let estimated_output_samples = progressive_buffer_samples(
+ duration_secs,
+ output_info.sample_rate,
+ output_info.channels,
+ );
// The producer renders f32 at the device rate/channel count; the
// callback converts to the device sample format on the fly.
let render_info = AudioInfo::new_raw(
@@ -2383,6 +2388,14 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
));
let (ready_tx, ready_rx) = std::sync::mpsc::channel();
+ // A fast worker may finish before publication, so give it its retention owner first.
+ if let Some(cache) = cache {
+ let retained = timeline.retain(CacheRelease {
+ cache: Arc::downgrade(&cache.entry),
+ idle_release: cache.idle_release,
+ });
+ debug_assert!(retained);
+ }
if !spawn_progressive_render(
timeline.clone(),
@@ -2395,7 +2408,10 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
ready_at_index,
ready_tx,
) {
- lock(&timeline.retain_slot).exited = true;
+ let mut slot = lock(&timeline.retain_slot);
+ slot.exited = true;
+ slot.release = None;
+ timeline.retained.store(false, Ordering::Release);
}
Self {
@@ -2492,18 +2508,14 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
})
}
- fn retain_in_cache(self, cache: &PlaybackAudioCache, plan: Option<CachePlan>) -> Self {
+ fn publish_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,
- }) {
+ let previous = if plan.shift == 0 && self.render.timeline.retained.load(Ordering::Acquire) {
entry.replace(CachedProgressiveRender {
key: plan.key,
render: self.render.clone(),
@@ -4728,6 +4740,89 @@ mod tests {
assert!((left_at_second(&stream, 1) - expected(10_000)).abs() < 0.02);
}
+ #[test]
+ fn parallel_music_load_matches_serial_mix_and_preserves_failed_cache_entries() {
+ ffmpeg::init().unwrap();
+ let dir = tempfile::tempdir().unwrap();
+ let mut segments = Vec::new();
+ for (index, values) in [
+ [2_000, -4_000, 6_000],
+ [-3_000, 5_000, -7_000],
+ [1_000, 8_000, -2_000],
+ ]
+ .iter()
+ .enumerate()
+ {
+ let path = format!("music-{index}.wav");
+ write_step_wav(&dir.path().join(&path), values);
+ let mut segment = music_track_segment(&path, 0.0, 1.5, 0.125, 0.25);
+ segment.trim_start = 0.5;
+ segments.push(segment);
+ }
+ let project = music_project(segments.clone());
+ let mut reference = MusicTracks::new();
+ for segment in &segments {
+ reference.extend(crate::load_music_tracks_uncached(
+ &music_project(vec![segment.clone()]),
+ dir.path(),
+ ));
+ }
+ let mut cache = MusicTracks::new();
+ let actual = crate::load_music_tracks(&project, dir.path(), &mut cache);
+ assert_eq!(actual.len(), 3);
+ let repeated = crate::load_music_tracks(&project, dir.path(), &mut cache);
+ for (path, data) in &actual {
+ assert!(Arc::ptr_eq(data, &repeated[path]));
+ }
+ let render = |tracks| {
+ let mut renderer = AudioRenderer::new(vec![]).with_music(tracks);
+ render_export_audio(&mut renderer, &project, 30, 45)
+ .into_iter()
+ .map(f32::to_bits)
+ .collect::<Vec<_>>()
+ };
+ assert_eq!(render(actual.clone()), render(reference));
+ for retained in [1, 2] {
+ let mut partial = actual
+ .iter()
+ .take(retained)
+ .map(|(path, data)| (path.clone(), Arc::clone(data)))
+ .collect::<MusicTracks>();
+ let retained_entries = partial.clone();
+ let loaded = crate::load_music_tracks(&project, dir.path(), &mut partial);
+ assert_eq!(render(loaded), render(actual.clone()));
+ for (path, data) in &retained_entries {
+ assert!(Arc::ptr_eq(data, &partial[path]));
+ }
+ }
+ let corrupt_path = "corrupt.wav";
+ std::fs::write(dir.path().join(corrupt_path), b"invalid audio").unwrap();
+ let mut corrupt_segments = segments.clone();
+ corrupt_segments.push(music_track_segment(corrupt_path, 0.0, 1.5, 0.0, 0.0));
+ let mut corrupt_cache = MusicTracks::new();
+ let surviving = crate::load_music_tracks(
+ &music_project(corrupt_segments),
+ dir.path(),
+ &mut corrupt_cache,
+ );
+ assert_eq!(surviving.len(), 3);
+ assert!(!corrupt_cache.contains_key(corrupt_path));
+ assert_eq!(render(surviving), render(actual.clone()));
+ for segment in &mut segments {
+ std::fs::remove_file(dir.path().join(&segment.path)).unwrap();
+ segment.trim_start = 0.0;
+ }
+ let failed = crate::load_music_tracks(&music_project(segments), dir.path(), &mut cache);
+ assert!(failed.is_empty());
+ for (path, data) in &actual {
+ assert!(Arc::ptr_eq(data, &cache[path]));
+ }
+ let restored = crate::load_music_tracks(&project, dir.path(), &mut cache);
+ for (path, data) in &actual {
+ assert!(Arc::ptr_eq(data, &restored[path]));
+ }
+ }
+
static CACHE_RELEASE: std::sync::RwLock<()> = std::sync::RwLock::new(());
struct ProgressiveFixture {
@@ -5369,6 +5464,60 @@ mod tests {
}
}
+ #[test]
+ fn a_render_completed_before_cache_publication_is_reused() {
+ let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
+ let fixture = ProgressiveFixture::new();
+ let output_info = stereo_48k();
+ let cache = PlaybackAudioCache::default();
+ let plan = ProgressiveAudioBuffer::<f32>::cache_plan(
+ fixture.segments.clone(),
+ &fixture.music,
+ &fixture.project,
+ output_info,
+ fixture.duration(),
+ 0.0,
+ progressive_buffer_samples(
+ fixture.duration(),
+ output_info.sample_rate,
+ output_info.channels,
+ ),
+ )
+ .unwrap();
+ assert_eq!(plan.shift, 0);
+ let fresh = ProgressiveAudioBuffer::<f32>::new(
+ fixture.segments.clone(),
+ fixture.music.clone(),
+ &fixture.project,
+ output_info,
+ fixture.duration(),
+ 0.0,
+ Some(&cache),
+ );
+ fresh.wait_until_fully_rendered();
+ let timeline = fresh.render.timeline.clone();
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
+ while !lock(&timeline.retain_slot).exited {
+ assert!(std::time::Instant::now() < deadline);
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ }
+ assert!(lock(&cache.entry).is_none());
+ let fresh = fresh.publish_in_cache(&cache, Some(plan));
+ assert!(
+ lock(&cache.entry)
+ .as_ref()
+ .is_some_and(|cached| Arc::ptr_eq(&cached.render.timeline, &timeline))
+ );
+ drop(fresh);
+ 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),
+ "render completed before publication",
+ );
+ }
+
#[test]
fn a_replay_resumes_a_parked_render() {
let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
@@ -5377,9 +5526,15 @@ mod tests {
for _ in 0..20 {
let cache = PlaybackAudioCache::default();
let mut first = fixture.cached(&cache, output_info, 0.0);
+ let timeline = progressive_timeline(&first);
+ assert!(timeline.retained.load(Ordering::Acquire));
+ assert!(
+ lock(&cache.entry)
+ .as_ref()
+ .is_some_and(|cached| Arc::ptr_eq(&cached.render.timeline, &timeline))
+ );
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)));
diff --git a/crates/editor/src/export_audio.rs b/crates/editor/src/export_audio.rs
index 0a887bef0..fe3a1e0b5 100644
--- a/crates/editor/src/export_audio.rs
+++ b/crates/editor/src/export_audio.rs
@@ -20,6 +20,7 @@ pub(crate) const EXPORT_AUDIO_BLOCK_SAMPLES: usize = 4_096;
const MAX_PARALLEL_AUDIO_SOURCES: usize = 4;
const MAX_ENHANCED_WINDOW_SAMPLES: usize =
EXPORT_AUDIO_BLOCK_SAMPLES + VOICE_PREROLL_SAMPLES + VOICE_WINDOW_PADDING_SAMPLES;
+const STUDIO_SOUND_HISTORY_SAMPLES: usize = VOICE_PREROLL_SAMPLES + VOICE_WINDOW_PADDING_SAMPLES;
#[derive(Clone, Debug)]
pub enum ExportAudioError {
@@ -325,17 +326,6 @@ 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 {
@@ -356,11 +346,7 @@ impl ExportAudioRenderer {
return false;
}
if let Some(previous_end) = ends.insert(segment.recording_clip, segment.end)
- && (!timeline.transitions.is_empty()
- || segment.start < previous_end
- || (min_gap > 0.0
- && segment.start > previous_end
- && segment.start < previous_end + min_gap))
+ && (!timeline.transitions.is_empty() || segment.start < previous_end)
{
return false;
}
@@ -532,12 +518,16 @@ impl ExportAudioSources {
)
});
let range = enhancer.source_range(start, end.saturating_sub(start));
- track.prepare(range.start as i128, range.end as i128)?;
+ track.prepare(
+ range.start as i128,
+ range.end as i128,
+ STUDIO_SOUND_HISTORY_SAMPLES,
+ )?;
track.enhanced =
Some(enhancer.render(&track.view(), start, end.saturating_sub(start)));
track.enhancer = Some(enhancer);
} else {
- track.prepare(start, end)?;
+ track.prepare(start, end, 0)?;
track.enhanced = None;
track.enhancer = None;
}
@@ -657,20 +647,21 @@ impl ExportAudioTrack {
((offset + self.timing_offset_secs) * AudioRenderer::SAMPLE_RATE as f32).round() as isize
}
- fn prepare(&mut self, start: i128, end: i128) -> Result<(), ExportAudioError> {
+ fn prepare(&mut self, start: i128, end: i128, history: usize) -> Result<(), ExportAudioError> {
let start = usize::try_from(start.max(0)).map_err(|_| ExportAudioError::InvalidWindow)?;
let end = usize::try_from(end.max(0)).map_err(|_| ExportAudioError::InvalidWindow)?;
if end < start || end - start > MAX_ENHANCED_WINDOW_SAMPLES || start < self.source_start {
return Err(ExportAudioError::InvalidWindow);
}
let channels = self.source.channels() as usize;
- let discard = start
+ let retained_start = start.saturating_sub(history).max(self.source_start);
+ let discard = retained_start
.saturating_sub(self.source_start)
.min(self.samples.len() / channels)
* channels;
self.samples.copy_within(discard.., 0);
self.samples.truncate(self.samples.len() - discard);
- self.source_start = start;
+ self.source_start = retained_start;
while self.eof.is_none() && self.source.position() < end as u64 {
let wanted = (end as u64 - self.source.position()).min(12_000) as usize;
match self
@@ -684,7 +675,7 @@ impl ExportAudioTrack {
ChunkRead::Chunk(chunk) => {
let chunk_start = usize::try_from(chunk.source_start_sample)
.map_err(|_| ExportAudioError::InvalidWindow)?;
- let discard = start
+ let discard = retained_start
.saturating_sub(chunk_start)
.min(chunk.samples.len() / channels)
* channels;
@@ -1240,7 +1231,7 @@ mod tests {
.unwrap(),
);
for track in candidate.sources.tracks.iter_mut().flatten() {
- track.prepare(10, 100).unwrap();
+ track.prepare(10, 100, 0).unwrap();
}
let identity = |track: &ExportAudioTrack| {
(
@@ -1498,7 +1489,7 @@ mod tests {
}
#[test]
- fn eligibility_rejects_studio_sound_cuts_shorter_than_voice_preroll() {
+ fn eligibility_accepts_studio_sound_short_forward_cuts() {
let mut short_cut = project();
short_cut
.timeline
@@ -1508,7 +1499,7 @@ mod tests {
.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));
+ assert!(ExportAudioRenderer::eligible_sources(&short_cut, 3));
short_cut.audio.mute = true;
assert!(ExportAudioRenderer::eligible_sources(&short_cut, 3));
@@ -1531,6 +1522,9 @@ mod tests {
.segments
.push(segment(0, 0.33, 0.9));
assert!(ExportAudioRenderer::eligible_sources(&exact_split, 3));
+
+ short_cut.timeline.as_mut().unwrap().segments[2].start = 0.32;
+ assert!(!ExportAudioRenderer::eligible_sources(&short_cut, 3));
}
#[test]
@@ -1538,6 +1532,100 @@ mod tests {
assert_bounded_sink_variants([19], &[1]);
}
+ #[test]
+ fn short_studio_sound_cuts_match_preview_after_history_rolls() {
+ ffmpeg::init().unwrap();
+ let directory = tempfile::tempdir().unwrap();
+ let paths =
+ ["mic.wav", "system.wav", "incoming.wav"].map(|name| directory.path().join(name));
+ for ((path, frames), channels) in
+ paths.iter().zip([144_013, 96_007, 168_013]).zip([1, 2, 2])
+ {
+ write_wav(path, frames, channels);
+ }
+ let data = paths
+ .iter()
+ .map(|path| Arc::new(AudioData::from_file(path).unwrap()))
+ .collect::<Vec<_>>();
+ for (tier, isolation) in [
+ cap_project::VoiceIsolation::Light,
+ cap_project::VoiceIsolation::Balanced,
+ cap_project::VoiceIsolation::Strong,
+ ]
+ .into_iter()
+ .enumerate()
+ {
+ for offset in [0, 1] {
+ let mut project = project();
+ project.audio.improve = true;
+ project.audio.isolation = isolation;
+ if offset == 1 {
+ project.clips[0].offsets.mic = 0.137_125;
+ project.clips[0].offsets.system_audio = -0.031_25;
+ project.clips[1].offsets.mic = -0.117_125;
+ project.audio.mic_stereo_mode = cap_project::StereoMode::MonoR;
+ }
+ project.timeline.as_mut().unwrap().segments = vec![
+ segment(0, 0.001_01, 0.72),
+ segment(0, 0.73, 0.8),
+ segment(0, 0.800_02, 0.92),
+ segment(0, 1.0, 1.63),
+ segment(1, 0.35, 1.4),
+ segment(1, 1.5, 2.2),
+ segment(0, 1.8, 2.5),
+ segment(0, 2.500_02, 3.05),
+ ];
+ let requests = [997, 4_096, 48_001]
+ .into_iter()
+ .chain((tier == 1 && offset == 0).then_some(1));
+ for request in requests {
+ let (mut reference, mut candidate) = fixture(&paths, &data);
+ reference.set_playhead(0.0, &project);
+ loop {
+ let expected = reference.render_frame_raw(request, &project);
+ let mut actual = Vec::new();
+ let written = candidate
+ .render_chunks(request, &project, |offset, samples| {
+ assert_eq!(offset * 2, actual.len());
+ assert!(samples.len() <= EXPORT_AUDIO_BLOCK_SAMPLES * 2);
+ actual.extend_from_slice(samples);
+ Ok(())
+ })
+ .unwrap_or_else(|error| {
+ panic!("{isolation:?}, offset {offset}, request {request}: {error}")
+ });
+ assert_eq!(written, expected.as_ref().map(|(count, _)| *count));
+ let expected = expected.as_ref().map_or(&[][..], |(_, data)| data);
+ assert_eq!(actual.len(), expected.len());
+ assert!(
+ actual
+ .iter()
+ .map(|sample| sample.to_bits())
+ .eq(expected.iter().map(|sample| sample.to_bits())),
+ "{isolation:?}, offset {offset}, request {request}"
+ );
+ for track in candidate.sources.tracks.iter().flatten() {
+ let limit = if track.enhancer.is_some() {
+ MAX_ENHANCED_WINDOW_SAMPLES
+ + VOICE_PREROLL_SAMPLES
+ + VOICE_WINDOW_PADDING_SAMPLES
+ } else {
+ EXPORT_AUDIO_BLOCK_SAMPLES
+ };
+ assert!(
+ track.samples.len() <= limit * track.source.channels() as usize
+ );
+ }
+ if written.is_none() {
+ break;
+ }
+ }
+ candidate.validate_to_end().unwrap();
+ }
+ }
+ }
+ }
+
#[test]
fn fullscreen_holds_match_full_renderer_without_retaining_whole_tracks() {
assert_bounded_sink_variants(26..32, &[1, 7, 997, 4_096, 48_001]);
@@ -1804,7 +1892,7 @@ mod tests {
assert!(
track.samples.len()
<= (if track.enhancer.is_some() {
- MAX_ENHANCED_WINDOW_SAMPLES
+ MAX_ENHANCED_WINDOW_SAMPLES + STUDIO_SOUND_HISTORY_SAMPLES
} else {
EXPORT_AUDIO_BLOCK_SAMPLES
}) * track.source.channels() as usize
@@ -2045,12 +2133,12 @@ mod tests {
let cancellation = Arc::new(AtomicBool::new(false));
let mut track =
ExportAudioTrack::open(&path, 0, true, 0.0, &cancellation, &cancellation).unwrap();
- track.prepare(8_000, 9_000).unwrap();
- track.prepare(8_500, 9_501).unwrap();
+ track.prepare(8_000, 9_000, 0).unwrap();
+ track.prepare(8_500, 9_501, 0).unwrap();
let full = AudioData::from_file(&path).unwrap();
assert_eq!(track.samples, full.samples()[8_500 * 2..9_501 * 2]);
assert!(matches!(
- track.prepare(0, 1),
+ track.prepare(0, 1, 0),
Err(ExportAudioError::InvalidWindow)
));
let mut candidate = ExportAudioRenderer {
@@ -2077,6 +2165,55 @@ mod tests {
);
}
+ #[test]
+ fn source_history_reuses_decoded_samples_and_stays_bounded() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("source.wav");
+ write_wav(&path, 144_013, 2);
+ let cancellation = Arc::new(AtomicBool::new(false));
+ let mut track =
+ ExportAudioTrack::open(&path, 0, true, 0.0, &cancellation, &cancellation).unwrap();
+ let full = AudioData::from_file(&path).unwrap();
+ track
+ .prepare(50_000, 52_000, STUDIO_SOUND_HISTORY_SAMPLES)
+ .unwrap();
+ let position = track.source.position();
+ let oldest = track.source_start;
+ track
+ .prepare(25_000, 27_000, STUDIO_SOUND_HISTORY_SAMPLES)
+ .unwrap();
+ assert_eq!(track.source.position(), position);
+ assert_eq!(track.source_start, oldest);
+ assert_eq!(
+ track.view().sample_slice(25_000 * 2..27_000 * 2).unwrap(),
+ &full.samples()[25_000 * 2..27_000 * 2]
+ );
+ assert!(matches!(
+ track.prepare(
+ oldest as i128 - 1,
+ oldest as i128,
+ STUDIO_SOUND_HISTORY_SAMPLES
+ ),
+ Err(ExportAudioError::InvalidWindow)
+ ));
+ for start in (60_000..150_000).step_by(997) {
+ track
+ .prepare(start, start + 1_632, STUDIO_SOUND_HISTORY_SAMPLES)
+ .unwrap();
+ let end = track.source.position() as usize;
+ assert_eq!(
+ track.samples,
+ full.samples()[track.source_start * 2..end * 2]
+ );
+ assert!(
+ track.samples.len()
+ <= (MAX_ENHANCED_WINDOW_SAMPLES + STUDIO_SOUND_HISTORY_SAMPLES) * 2
+ );
+ }
+ track.prepare(144_013, 144_014, 0).unwrap();
+ assert!(track.samples.is_empty());
+ }
+
#[test]
fn sink_failure_stops_before_later_source_reads() {
let directory = tempfile::tempdir().unwrap();
diff --git a/crates/editor/src/segments.rs b/crates/editor/src/segments.rs
index 60953e588..5a485f19d 100644
--- a/crates/editor/src/segments.rs
+++ b/crates/editor/src/segments.rs
@@ -61,29 +61,57 @@ pub fn load_music_tracks(
.or_insert((trim_start, trim_end));
}
+ let mut missing = Vec::new();
for (path, (source_start, source_end)) in ranges {
if let Some(data) = cache.get(path)
&& data.covers_source_range(source_start, source_end)
{
result.insert(path.to_string(), Arc::clone(data));
- continue;
+ } else {
+ missing.push((path, source_start, source_end));
}
+ }
- let resolved = resolve_music_path(project_path, path);
- match AudioData::from_file_range(&resolved, source_start, source_end) {
- Ok(data) => {
- let data = Arc::new(data);
- cache.insert(path.to_string(), Arc::clone(&data));
- result.insert(path.to_string(), data);
- }
- Err(error) => {
- warn!(
- path = %resolved.display(),
- error,
- "Failed to load timeline audio track; skipping"
- );
+ let decode = |ranges: &[(&str, usize, usize)]| {
+ ranges
+ .iter()
+ .filter_map(|&(path, source_start, source_end)| {
+ let resolved = resolve_music_path(project_path, path);
+ match AudioData::from_file_range(&resolved, source_start, source_end) {
+ Ok(data) => Some((path.to_string(), Arc::new(data))),
+ Err(error) => {
+ warn!(
+ path = %resolved.display(),
+ error,
+ "Failed to load timeline audio track; skipping"
+ );
+ None
+ }
+ }
+ })
+ .collect::<Vec<_>>()
+ };
+ let decoded = if missing.len() < 2 {
+ decode(&missing)
+ } else {
+ std::thread::scope(|scope| {
+ let (left, right) = missing.split_at(missing.len().div_ceil(2));
+ match std::thread::Builder::new().spawn_scoped(scope, || decode(left)) {
+ Ok(worker) => {
+ let mut decoded = decode(right);
+ match worker.join() {
+ Ok(other) => decoded.extend(other),
+ Err(panic) => std::panic::resume_unwind(panic),
+ }
+ decoded
+ }
+ Err(_) => decode(&missing),
}
- }
+ })
+ };
+ for (path, data) in decoded {
+ cache.insert(path.clone(), Arc::clone(&data));
+ result.insert(path, data);
}
result
diff --git a/crates/editor/src/thumbnail.rs b/crates/editor/src/thumbnail.rs
index 2c078355f..9b1abe68a 100644
--- a/crates/editor/src/thumbnail.rs
+++ b/crates/editor/src/thumbnail.rs
@@ -1,6 +1,8 @@
use crate::{EditorInstance, editor::RendererTransitionInput};
use cap_project::{ProjectConfiguration, XY};
-use cap_rendering::{ProjectUniforms, RenderedFrame, ZoomTransformTimeline};
+use cap_rendering::{
+ PrecomputedCursorTimeline, ProjectUniforms, RenderedFrame, ZoomTransformTimeline,
+};
use std::{io::Write, path::Path, sync::Arc};
const THUMBNAIL_SIZE: XY<u32> = XY { x: 640, y: 640 };
@@ -85,7 +87,8 @@ impl EditorInstance {
segment.recording_clip,
);
zoom.ensure_precomputed_until(1.0 / THUMBNAIL_FPS as f32);
- let uniforms = ProjectUniforms::new(
+ let cursor_timeline = PrecomputedCursorTimeline::from_project(&media.cursor, &project);
+ let uniforms = ProjectUniforms::new_with_precomputed_cursor(
&self.render_constants,
&project,
0,
@@ -95,6 +98,7 @@ impl EditorInstance {
&frames,
duration,
&zoom,
+ &cursor_timeline,
);
let frame = self
.renderer
diff --git a/crates/enc-avfoundation/src/mp4.rs b/crates/enc-avfoundation/src/mp4.rs
index 89a13cfce..930d6d23b 100644
--- a/crates/enc-avfoundation/src/mp4.rs
+++ b/crates/enc-avfoundation/src/mp4.rs
@@ -4347,6 +4347,72 @@ mod tests {
assert!(packets >= 88, "{format:?}: {packets} of 90 frames written");
}
}
+
+ /// A virtual camera that delivers another size than it negotiated from
+ /// its very first frame (GitHub #2724: 1280x720 advertised, 3840x2160
+ /// delivered, and the reverse). Every frame was dropped, nothing was ever
+ /// written, and the camera track failed at stop with NotWriting.
+ #[test]
+ fn camera_frames_of_another_size_from_the_first_frame_are_kept() {
+ for ((writer_width, writer_height), (frame_width, frame_height)) in
+ [((1280, 720), (3840, 2160)), ((3840, 2160), (1280, 720))]
+ {
+ for format in [
+ cidre::cv::PixelFormat::_32_BGRA,
+ cidre::cv::PixelFormat::_2VUY,
+ cidre::cv::PixelFormat::_420V,
+ ] {
+ let case = format!(
+ "{writer_width}x{writer_height} writer, {frame_width}x{frame_height} {format:?} frames"
+ );
+ let output = test_output_path("camera_size_from_first_frame");
+ let pixel = if format == cidre::cv::PixelFormat::_32_BGRA {
+ ffmpeg::format::Pixel::BGRA
+ } else if format == cidre::cv::PixelFormat::_2VUY {
+ ffmpeg::format::Pixel::UYVY422
+ } else {
+ ffmpeg::format::Pixel::NV12
+ };
+ let config = VideoInfo::from_raw_ffmpeg(pixel, writer_width, writer_height, 30);
+ let mut encoder = MP4Encoder::init(output.clone(), config, None, None).unwrap();
+ let pool = create_pixel_buffer_pool_with_format(
+ frame_width as usize,
+ frame_height as usize,
+ format,
+ );
+ for index in 0..30i64 {
+ let frame = create_test_video_frame(&pool, index * 33_333, 33_333);
+ queue_video_frame_with_retry(
+ &mut encoder,
+ frame,
+ Duration::from_micros(index as u64 * 33_333),
+ )
+ .unwrap();
+ }
+ encoder
+ .finish(Some(Duration::from_micros(30 * 33_333)))
+ .unwrap_or_else(|error| panic!("{case}: finish failed: {error:?}"));
+
+ let mut input = ffmpeg::format::input(&output).unwrap();
+ let stream = input.streams().best(ffmpeg::media::Type::Video).unwrap();
+ let index = stream.index();
+ let decoder = ffmpeg::codec::Context::from_parameters(stream.parameters())
+ .unwrap()
+ .decoder()
+ .video()
+ .unwrap();
+ let size = (decoder.width(), decoder.height());
+ let packets = input
+ .packets()
+ .filter(|(stream, _)| stream.index() == index)
+ .count();
+ let _ = std::fs::remove_file(&output);
+
+ assert_eq!(size, (writer_width, writer_height), "{case}");
+ assert!(packets >= 28, "{case}: {packets} of 30 frames written");
+ }
+ }
+ }
}
trait SampleBufExt {
diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rs
index cbe05805f..01db20997 100644
--- a/crates/enc-ffmpeg/src/video/h264.rs
+++ b/crates/enc-ffmpeg/src/video/h264.rs
@@ -12,6 +12,7 @@ use ffmpeg::{
use tracing::{debug, error, info, trace, warn};
use crate::base::{EncoderBase, PacketSink};
+use crate::video::HwUpload;
use crate::video::h264_packet::H264PacketEncoder;
#[cfg(target_os = "macos")]
use crate::video::videotoolbox_hw::VideoToolboxHwFrames;
@@ -60,12 +61,14 @@ pub enum H264EncoderError {
SelfTest(String),
#[error("VideoToolbox hardware input unavailable: {0}")]
VideoToolboxHwInput(String),
+ #[error("VA-API: {0}")]
+ Vaapi(String),
}
fn is_hardware_h264(codec_name: &str) -> bool {
matches!(
codec_name,
- "h264_videotoolbox" | "h264_nvenc" | "h264_qsv" | "h264_amf" | "h264_mf"
+ "h264_videotoolbox" | "h264_nvenc" | "h264_qsv" | "h264_amf" | "h264_mf" | "h264_vaapi"
)
}
@@ -270,6 +273,7 @@ impl H264EncoderBuilder {
input_format,
input_width,
input_height,
+ hw_upload,
} = open_video_encoder(
codec,
encoder_options,
@@ -298,6 +302,7 @@ impl H264EncoderBuilder {
input_width,
input_height,
converted_frame_pool,
+ hw_upload,
#[cfg(target_os = "macos")]
videotoolbox_hw: None,
})
@@ -432,6 +437,7 @@ impl H264EncoderBuilder {
input_width: input_config.width,
input_height: input_config.height,
converted_frame_pool: None,
+ hw_upload: HwUpload::default(),
videotoolbox_hw: Some(Arc::new(hw_frames)),
})
}
@@ -567,6 +573,8 @@ pub(crate) struct OpenedVideoEncoder {
pub input_format: format::Pixel,
pub input_width: u32,
pub input_height: u32,
+ /// Where software frames go before the encoder takes them.
+ pub hw_upload: HwUpload,
}
#[allow(clippy::too_many_arguments)]
@@ -640,11 +648,18 @@ fn open_video_encoder_inner(
crf: Option<u8>,
global_header: bool,
) -> Result<OpenedVideoEncoder, H264EncoderError> {
- let encoder_supports_input_format = codec
- .video()
- .ok()
- .and_then(|codec_video| codec_video.formats())
- .is_some_and(|mut formats| formats.any(|f| f == input_config.pixel_format));
+ // `h264_vaapi` lists only VA surfaces; the frames it is handed are NV12
+ // that gets uploaded into one.
+ let vaapi = codec.name() == "h264_vaapi";
+ let encoder_supports_input_format = if vaapi {
+ input_config.pixel_format == ffmpeg::format::Pixel::NV12
+ } else {
+ codec
+ .video()
+ .ok()
+ .and_then(|codec_video| codec_video.formats())
+ .is_some_and(|mut formats| formats.any(|f| f == input_config.pixel_format))
+ };
let mut needs_pixel_conversion = false;
@@ -749,14 +764,26 @@ fn open_video_encoder_inner(
.map(|v| v.get())
.unwrap_or(1);
- let encoder = {
+ let open = |hw_upload: &HwUpload,
+ encoder_options: Dictionary<'static>|
+ -> Result<encoder::Video, ffmpeg::Error> {
let mut encoder_ctx = context::Context::new_with_codec(codec);
encoder_ctx.set_threading(Config::count(thread_count));
let mut encoder = encoder_ctx.encoder().video()?;
encoder.set_width(output_width);
encoder.set_height(output_height);
- encoder.set_format(output_format);
+ encoder.set_format(if vaapi {
+ ffmpeg::format::Pixel::VAAPI
+ } else {
+ output_format
+ });
+ #[cfg(target_os = "linux")]
+ if let Some(frames) = &hw_upload.vaapi {
+ unsafe { frames.attach(encoder.as_mut_ptr()) };
+ }
+ #[cfg(not(target_os = "linux"))]
+ let _ = hw_upload;
encoder.set_time_base(input_config.time_base);
encoder.set_frame_rate(Some(input_config.frame_rate));
encoder.set_colorspace(color::Space::BT709);
@@ -784,7 +811,22 @@ fn open_video_encoder_inner(
encoder.set_max_bit_rate(bitrate * 3 / 2);
}
- encoder.open_as_with(codec, encoder_options)?
+ encoder.open_as_with(codec, encoder_options)
+ };
+
+ #[cfg(target_os = "linux")]
+ let (encoder, hw_upload) = if vaapi {
+ open_on_a_vaapi_node(output_width, output_height, |hw_upload| {
+ open(hw_upload, clone_options(&encoder_options))
+ })?
+ } else {
+ let hw_upload = HwUpload::default();
+ (open(&hw_upload, encoder_options)?, hw_upload)
+ };
+ #[cfg(not(target_os = "linux"))]
+ let (encoder, hw_upload) = {
+ let hw_upload = HwUpload::default();
+ (open(&hw_upload, encoder_options)?, hw_upload)
};
let converted_frame_pool = if converter.is_some() {
@@ -807,9 +849,45 @@ fn open_video_encoder_inner(
input_format: input_config.pixel_format,
input_width: input_config.width,
input_height: input_config.height,
+ hw_upload,
})
}
+/// Opens the encoder on the first render node whose VA-API driver both makes
+/// NV12 surfaces and encodes H264: a GPU can do the first and not the second.
+#[cfg(target_os = "linux")]
+fn open_on_a_vaapi_node(
+ width: u32,
+ height: u32,
+ mut open: impl FnMut(&HwUpload) -> Result<encoder::Video, ffmpeg::Error>,
+) -> Result<(encoder::Video, HwUpload), H264EncoderError> {
+ let mut errors = Vec::new();
+ for node in super::vaapi::render_nodes() {
+ let frames = match super::vaapi::VaapiFrames::open(&node, width, height) {
+ Ok(frames) => frames,
+ Err(error) => {
+ errors.push(format!("{}: {error}", node.display()));
+ continue;
+ }
+ };
+ let hw_upload = HwUpload {
+ vaapi: Some(frames),
+ };
+ match open(&hw_upload) {
+ Ok(encoder) => {
+ debug!(node = %node.display(), "H264 encodes through VA-API");
+ return Ok((encoder, hw_upload));
+ }
+ Err(error) => errors.push(format!("{}: {error}", node.display())),
+ }
+ }
+ Err(H264EncoderError::Vaapi(if errors.is_empty() {
+ "no render node with a VA-API driver".to_string()
+ } else {
+ errors.join("; ")
+ }))
+}
+
pub struct H264Encoder {
base: EncoderBase,
encoder: encoder::Video,
@@ -821,6 +899,7 @@ pub struct H264Encoder {
input_width: u32,
input_height: u32,
converted_frame_pool: Option<frame::Video>,
+ hw_upload: HwUpload,
#[cfg(target_os = "macos")]
videotoolbox_hw: Option<Arc<VideoToolboxHwFrames>>,
}
@@ -930,6 +1009,10 @@ impl H264Encoder {
} else {
&frame
};
+ let frame_to_send = self
+ .hw_upload
+ .frame(frame_to_send)
+ .map_err(QueueFrameError::Encode)?;
self.base
.send_frame(frame_to_send, output, &mut self.encoder)
@@ -959,6 +1042,10 @@ impl H264Encoder {
} else {
frame as &frame::Video
};
+ let frame_to_send = self
+ .hw_upload
+ .frame(frame_to_send)
+ .map_err(QueueFrameError::Encode)?;
self.base
.send_frame(frame_to_send, output, &mut self.encoder)
@@ -985,9 +1072,13 @@ impl H264Encoder {
self.base
.update_pts(&mut frame, timestamp, &mut self.encoder);
+ let frame = self
+ .hw_upload
+ .frame(&frame)
+ .map_err(QueueFrameError::Encode)?;
self.base
- .send_frame(&frame, output, &mut self.encoder)
+ .send_frame(frame, output, &mut self.encoder)
.map_err(QueueFrameError::Encode)?;
Ok(())
@@ -1131,11 +1222,16 @@ fn requires_software_encoder(config: &VideoInfo, preset: H264Preset, is_export:
}
#[cfg(target_os = "linux")]
-fn linux_encoder_priority(nvidia_device_available: bool) -> &'static [&'static str] {
- if nvidia_device_available {
- &["h264_nvenc", "libx264"]
- } else {
- &["libx264"]
+fn linux_encoder_priority(
+ nvidia_device_available: bool,
+ vaapi_device_available: bool,
+) -> &'static [&'static str] {
+ match (nvidia_device_available, vaapi_device_available) {
+ // A laptop's NVIDIA GPU first, its integrated GPU if NVENC fails.
+ (true, true) => &["h264_nvenc", "h264_vaapi", "libx264"],
+ (true, false) => &["h264_nvenc", "libx264"],
+ (false, true) => &["h264_vaapi", "libx264"],
+ (false, false) => &["libx264"],
}
}
@@ -1172,7 +1268,10 @@ fn get_default_encoder_priority(_config: &VideoInfo) -> &'static [&'static str]
#[cfg(target_os = "linux")]
{
- linux_encoder_priority(std::path::Path::new("/dev/nvidiactl").exists())
+ linux_encoder_priority(
+ std::path::Path::new("/dev/nvidiactl").exists(),
+ !super::vaapi::render_nodes().is_empty(),
+ )
}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
@@ -1315,6 +1414,10 @@ fn get_codec_and_options(
}
options.set("g", &keyframe_interval_str);
}
+ "h264_vaapi" => {
+ options.set("profile", "high");
+ options.set("g", &keyframe_interval_str);
+ }
"h264_mf" => {
options.set("hw_encoding", "true");
if is_export {
@@ -1404,7 +1507,8 @@ fn cached_hardware_self_test(
sync::{Mutex, OnceLock},
};
- if !(cfg!(target_os = "windows") || (cfg!(target_os = "linux") && codec.name() == "h264_nvenc"))
+ if !(cfg!(target_os = "windows")
+ || (cfg!(target_os = "linux") && matches!(codec.name(), "h264_nvenc" | "h264_vaapi")))
{
return Ok(());
}
@@ -1502,6 +1606,7 @@ fn hardware_encoder_self_test(
)
.map_err(|e| format!("test encoder open: {e}"))?;
let mut encoder = opened.encoder;
+ let mut hw_upload = opened.hw_upload;
// Mid-gray is neutral in both RGB (128,128,128) and YUV (Y=128, U=V=128)
// representations, so filling every plane with 128 produces a valid gray
@@ -1532,8 +1637,11 @@ fn hardware_encoder_self_test(
const TEST_FRAME_COUNT: i64 = 8;
for i in 0..TEST_FRAME_COUNT {
test_frame.set_pts(Some(i * pts_step));
+ let frame = hw_upload
+ .frame(&test_frame)
+ .map_err(|e| format!("upload: {e}"))?;
encoder
- .send_frame(&test_frame)
+ .send_frame(frame)
.map_err(|e| format!("send_frame: {e}"))?;
drain_packets(&mut encoder, &mut packets);
}
@@ -1738,13 +1846,30 @@ mod self_test_tests {
#[cfg(target_os = "linux")]
#[test]
fn linux_nvidia_device_prefers_nvenc_and_keeps_software_fallback() {
- assert_eq!(linux_encoder_priority(true), &["h264_nvenc", "libx264"]);
+ assert_eq!(
+ linux_encoder_priority(true, false),
+ &["h264_nvenc", "libx264"]
+ );
}
#[cfg(target_os = "linux")]
#[test]
- fn linux_without_nvidia_preserves_software_encoder() {
- assert_eq!(linux_encoder_priority(false), &["libx264"]);
+ fn linux_amd_and_intel_gpus_encode_through_va_api() {
+ assert_eq!(
+ linux_encoder_priority(false, true),
+ &["h264_vaapi", "libx264"]
+ );
+ // A hybrid laptop falls back to its integrated GPU before software.
+ assert_eq!(
+ linux_encoder_priority(true, true),
+ &["h264_nvenc", "h264_vaapi", "libx264"]
+ );
+ }
+
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn linux_without_a_hardware_encoder_preserves_software_encoder() {
+ assert_eq!(linux_encoder_priority(false, false), &["libx264"]);
}
#[cfg(target_os = "linux")]
@@ -1756,7 +1881,7 @@ mod self_test_tests {
get_encoder_priority_with_override(
&config,
H264Preset::HighThroughput,
- Some(linux_encoder_priority(true)),
+ Some(linux_encoder_priority(true, true)),
false,
),
&["libx264"],
@@ -1771,7 +1896,7 @@ mod self_test_tests {
let codecs = get_codec_and_options(
&config,
H264Preset::Ultrafast,
- Some(linux_encoder_priority(true)),
+ Some(linux_encoder_priority(true, true)),
true,
Some(23),
);
@@ -1807,6 +1932,52 @@ mod self_test_tests {
cached_hardware_self_test(codec, &options, &config, 160, 120, 0.3, None)
.expect("real NVIDIA hardware preserves neutral gray through encode and decode");
}
+
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn linux_vaapi_self_test_decodes_real_gray_frames_when_available() {
+ if super::super::vaapi::render_nodes().is_empty() {
+ return;
+ }
+
+ ffmpeg::init().ok();
+ let Some(codec) = encoder::find_by_name("h264_vaapi") else {
+ return;
+ };
+ let config = VideoInfo::from_raw(cap_media_info::RawVideoFormat::Bgra, 640, 360, 30);
+ let options = get_codec_and_options(
+ &config,
+ H264Preset::Ultrafast,
+ Some(&["h264_vaapi", "libx264"]),
+ false,
+ None,
+ )
+ .into_iter()
+ .find(|(candidate, _)| candidate.name() == "h264_vaapi")
+ .expect("available VA-API encoder is configured")
+ .1;
+ // Only hardware that opens an H264 encoder is tested (a render node
+ // can be inaccessible, render-only, or lack its VA-API driver), but
+ // one that opens must keep the gray.
+ if open_video_encoder_inner(
+ codec,
+ clone_options(&options),
+ &config,
+ 640,
+ 360,
+ 0.3,
+ true,
+ None,
+ false,
+ )
+ .is_err()
+ {
+ return;
+ }
+
+ cached_hardware_self_test(codec, &options, &config, 640, 360, 0.3, None)
+ .expect("a VA-API GPU preserves neutral gray through upload, encode and decode");
+ }
}
#[cfg(test)]
diff --git a/crates/enc-ffmpeg/src/video/h264_packet.rs b/crates/enc-ffmpeg/src/video/h264_packet.rs
index dd0fe0e8a..9f702f1a2 100644
--- a/crates/enc-ffmpeg/src/video/h264_packet.rs
+++ b/crates/enc-ffmpeg/src/video/h264_packet.rs
@@ -7,12 +7,14 @@ use ffmpeg::{
frame,
};
+use crate::video::HwUpload;
use crate::video::h264::OpenedVideoEncoder;
pub struct H264PacketEncoder {
encoder: encoder::Video,
converter: Option<ffmpeg::software::scaling::Context>,
converted_frame_pool: Option<frame::Video>,
+ hw_upload: HwUpload,
output_format: format::Pixel,
output_width: u32,
output_height: u32,
@@ -56,6 +58,7 @@ impl H264PacketEncoder {
encoder: opened.encoder,
converter: opened.converter,
converted_frame_pool: opened.converted_frame_pool,
+ hw_upload: opened.hw_upload,
output_format: opened.output_format,
output_width: opened.output_width,
output_height: opened.output_height,
@@ -148,6 +151,10 @@ impl H264PacketEncoder {
&frame
};
+ let frame_to_send = self
+ .hw_upload
+ .frame(frame_to_send)
+ .map_err(EncodePacketError::Encode)?;
self.encoder
.send_frame(frame_to_send)
.map_err(EncodePacketError::Encode)?;
@@ -180,6 +187,10 @@ impl H264PacketEncoder {
frame as &frame::Video
};
+ let frame_to_send = self
+ .hw_upload
+ .frame(frame_to_send)
+ .map_err(EncodePacketError::Encode)?;
self.encoder
.send_frame(frame_to_send)
.map_err(EncodePacketError::Encode)?;
diff --git a/crates/enc-ffmpeg/src/video/mod.rs b/crates/enc-ffmpeg/src/video/mod.rs
index e69e981ed..c4fb740e8 100644
--- a/crates/enc-ffmpeg/src/video/mod.rs
+++ b/crates/enc-ffmpeg/src/video/mod.rs
@@ -7,9 +7,32 @@ pub mod hevc;
mod packed_yuv;
pub mod prores;
mod rgb_to_yuv;
+#[cfg(target_os = "linux")]
+pub(crate) mod vaapi;
#[cfg(target_os = "macos")]
pub mod videotoolbox_hw;
+/// Hands an encoder the frame it takes: a GPU surface for encoders that only
+/// encode those (VA-API), the software frame itself for every other.
+#[derive(Default)]
+pub(crate) struct HwUpload {
+ #[cfg(target_os = "linux")]
+ pub(crate) vaapi: Option<vaapi::VaapiFrames>,
+}
+
+impl HwUpload {
+ pub(crate) fn frame<'a>(
+ &'a mut self,
+ frame: &'a frame::Video,
+ ) -> Result<&'a frame::Video, ffmpeg::Error> {
+ #[cfg(target_os = "linux")]
+ if let Some(vaapi) = &mut self.vaapi {
+ return vaapi.upload(frame);
+ }
+ Ok(frame)
+ }
+}
+
pub(crate) fn convert_or_scale(
src: &frame::Video,
dst: &mut frame::Video,
diff --git a/crates/enc-ffmpeg/src/video/vaapi.rs b/crates/enc-ffmpeg/src/video/vaapi.rs
new file mode 100644
index 000000000..6e8a3970b
--- /dev/null
+++ b/crates/enc-ffmpeg/src/video/vaapi.rs
@@ -0,0 +1,158 @@
+//! H.264 on Linux AMD and Intel GPUs through VA-API.
+//!
+//! `h264_vaapi` only encodes frames that already live in a VA surface, so the
+//! encoder gets a pool of NV12 surfaces on a render node and each software
+//! NV12 frame is uploaded into one just before it is sent.
+
+use std::{
+ ffi::CString,
+ path::{Path, PathBuf},
+ ptr,
+};
+
+use ffmpeg::{ffi, frame};
+
+pub(crate) struct VaapiFrames {
+ device: *mut ffi::AVBufferRef,
+ frames: *mut ffi::AVBufferRef,
+ surface: frame::Video,
+}
+
+// The buffers are reference counted by FFmpeg and only touched from the
+// thread that owns the encoder.
+unsafe impl Send for VaapiFrames {}
+
+impl VaapiFrames {
+ pub(crate) fn open(node: &Path, width: u32, height: u32) -> Result<Self, String> {
+ let path = CString::new(node.as_os_str().as_encoded_bytes())
+ .map_err(|_| "render node path has a NUL byte".to_string())?;
+ let mut device = ptr::null_mut();
+ let ret = unsafe {
+ ffi::av_hwdevice_ctx_create(
+ &mut device,
+ ffi::AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI,
+ path.as_ptr(),
+ ptr::null_mut(),
+ 0,
+ )
+ };
+ if ret < 0 {
+ return Err(format!("VA-API device: {}", ffmpeg::Error::from(ret)));
+ }
+ let mut frames = unsafe { ffi::av_hwframe_ctx_alloc(device) };
+ if frames.is_null() {
+ unsafe { ffi::av_buffer_unref(&mut device) };
+ return Err("VA-API frame pool allocation failed".to_string());
+ }
+ unsafe {
+ let context = (*frames).data.cast::<ffi::AVHWFramesContext>();
+ (*context).format = ffi::AVPixelFormat::AV_PIX_FMT_VAAPI;
+ (*context).sw_format = ffi::AVPixelFormat::AV_PIX_FMT_NV12;
+ (*context).width = width as i32;
+ (*context).height = height as i32;
+ (*context).initial_pool_size = 20;
+ }
+ let ret = unsafe { ffi::av_hwframe_ctx_init(frames) };
+ if ret < 0 {
+ unsafe {
+ ffi::av_buffer_unref(&mut frames);
+ ffi::av_buffer_unref(&mut device);
+ }
+ return Err(format!(
+ "VA-API NV12 surfaces: {}",
+ ffmpeg::Error::from(ret)
+ ));
+ }
+ Ok(Self {
+ device,
+ frames,
+ surface: frame::Video::empty(),
+ })
+ }
+
+ /// # Safety
+ /// `encoder` must be an unopened codec context.
+ pub(crate) unsafe fn attach(&self, encoder: *mut ffi::AVCodecContext) {
+ unsafe { (*encoder).hw_frames_ctx = ffi::av_buffer_ref(self.frames) };
+ }
+
+ pub(crate) fn upload(&mut self, source: &frame::Video) -> Result<&frame::Video, ffmpeg::Error> {
+ unsafe {
+ let surface = self.surface.as_mut_ptr();
+ ffi::av_frame_unref(surface);
+ let ret = ffi::av_hwframe_get_buffer(self.frames, surface, 0);
+ if ret < 0 {
+ return Err(ffmpeg::Error::from(ret));
+ }
+ let ret = ffi::av_hwframe_transfer_data(surface, source.as_ptr(), 0);
+ if ret < 0 {
+ return Err(ffmpeg::Error::from(ret));
+ }
+ let ret = ffi::av_frame_copy_props(surface, source.as_ptr());
+ if ret < 0 {
+ return Err(ffmpeg::Error::from(ret));
+ }
+ }
+ Ok(&self.surface)
+ }
+}
+
+impl Drop for VaapiFrames {
+ fn drop(&mut self) {
+ unsafe {
+ ffi::av_frame_unref(self.surface.as_mut_ptr());
+ ffi::av_buffer_unref(&mut self.frames);
+ ffi::av_buffer_unref(&mut self.device);
+ }
+ }
+}
+
+/// Render nodes whose kernel driver has a VA-API driver: not NVIDIA (whose
+/// encoder is NVENC), and not the virtual vgem and vkms devices.
+pub(crate) fn render_nodes() -> Vec<PathBuf> {
+ let Ok(entries) = std::fs::read_dir("/dev/dri") else {
+ return Vec::new();
+ };
+ let mut nodes = entries
+ .filter_map(Result::ok)
+ .map(|entry| entry.path())
+ .filter(|path| {
+ path.file_name()
+ .and_then(|name| name.to_str())
+ .is_some_and(|name| name.starts_with("renderD"))
+ })
+ .filter(|path| vaapi_driver(kernel_driver(path).as_deref()))
+ .collect::<Vec<_>>();
+ nodes.sort();
+ nodes
+}
+
+fn kernel_driver(node: &Path) -> Option<String> {
+ let name = node.file_name()?;
+ let link =
+ std::fs::read_link(Path::new("/sys/class/drm").join(name).join("device/driver")).ok()?;
+ Some(link.file_name()?.to_string_lossy().into_owned())
+}
+
+fn vaapi_driver(driver: Option<&str>) -> bool {
+ match driver {
+ None => false,
+ Some(driver) => !(driver.starts_with("nvidia") || matches!(driver, "vgem" | "vkms")),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn only_gpus_with_a_va_api_driver_are_tried() {
+ for driver in ["amdgpu", "radeon", "i915", "xe", "nouveau", "virtio-pci"] {
+ assert!(vaapi_driver(Some(driver)), "{driver}");
+ }
+ for driver in ["nvidia", "nvidia-drm", "vgem", "vkms"] {
+ assert!(!vaapi_driver(Some(driver)), "{driver}");
+ }
+ assert!(!vaapi_driver(None));
+ }
+}
diff --git a/crates/export/examples/studio-sound-cut-benchmark.rs b/crates/export/examples/studio-sound-cut-benchmark.rs
new file mode 100644
index 000000000..8d4abab75
--- /dev/null
+++ b/crates/export/examples/studio-sound-cut-benchmark.rs
@@ -0,0 +1,118 @@
+use cap_export::{
+ ExporterBase,
+ mp4::{ExportCompression, Mp4ExportSettings},
+};
+use cap_project::{ClipConfiguration, ProjectConfiguration, RecordingMeta, TimelineSegment, XY};
+use std::{
+ path::PathBuf,
+ sync::{
+ Arc,
+ atomic::{AtomicBool, AtomicU64, Ordering},
+ },
+ time::Instant,
+};
+
+#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
+async fn main() {
+ let root = PathBuf::from(std::env::args_os().nth(1).expect("fixture directory"));
+ let output = PathBuf::from(std::env::args_os().nth(2).expect("output MP4 path"));
+ let gap: f64 = std::env::var("CAP_EXPORT_CUT_GAP").map_or(0.1, |v| v.parse().unwrap());
+ let mut project = ProjectConfiguration {
+ timeline: Some(
+ serde_json::from_value(serde_json::json!({"segments":[],"zoomSegments":[]})).unwrap(),
+ ),
+ ..Default::default()
+ };
+ project.audio.improve = true;
+ project.camera.hide = true;
+ project.cursor.hide = true;
+ project.cursor.raw = true;
+ project.clips = vec![ClipConfiguration {
+ index: 0,
+ ..Default::default()
+ }];
+ project.timeline.as_mut().unwrap().segments = vec![
+ TimelineSegment {
+ recording_clip: 0,
+ start: 0.0,
+ end: 4.0,
+ timescale: 1.0,
+ ..Default::default()
+ },
+ TimelineSegment {
+ recording_clip: 0,
+ start: 4.0 + gap,
+ end: 8.0 + gap,
+ timescale: 1.0,
+ ..Default::default()
+ },
+ ];
+ project.write(&root).unwrap();
+ let mut meta: RecordingMeta = serde_json::from_value(serde_json::json!({
+ "platform":"Linux", "pretty_name":"Studio Sound cut benchmark", "sharing":null,
+ "segments":[{
+ "display":{"path":"display.mp4","fps":1,"start_time":0.0},
+ "mic":{"path":"mic.wav","start_time":0.0}
+ }],
+ "cursors":{},"status":{"status":"Complete"}
+ }))
+ .unwrap();
+ meta.project_path = root.clone();
+ meta.save_for_project().unwrap();
+ let first_progress = Arc::new(AtomicU64::new(u64::MAX));
+ let recorded_progress = first_progress.clone();
+ let start = Instant::now();
+ let base = ExporterBase::builder(root)
+ .with_output_path(output.clone())
+ .with_force_ffmpeg_decoder(true)
+ .build_for_mp4(Arc::new(AtomicBool::new(false)))
+ .await
+ .expect("export preparation");
+ let build_seconds = start.elapsed().as_secs_f64();
+ let streaming = base.uses_streaming_audio();
+ let settings = Mp4ExportSettings {
+ fps: 15,
+ resolution_base: XY::new(640, 360),
+ compression: ExportCompression::Web,
+ custom_bpp: None,
+ force_ffmpeg_decoder: true,
+ optimize_filesize: false,
+ };
+ let export_start = Instant::now();
+ settings
+ .export_prepared(base, move |_| {
+ recorded_progress
+ .compare_exchange(
+ u64::MAX,
+ start.elapsed().as_micros() as u64,
+ Ordering::Relaxed,
+ Ordering::Relaxed,
+ )
+ .ok();
+ true
+ })
+ .await
+ .expect("complete export");
+ let export_seconds = export_start.elapsed().as_secs_f64();
+ let total_seconds = start.elapsed().as_secs_f64();
+ let maxrss_kib = std::fs::read_to_string("/proc/self/status")
+ .ok()
+ .and_then(|status| {
+ status.lines().find_map(|line| {
+ line.strip_prefix("VmHWM:")?
+ .split_whitespace()
+ .next()?
+ .parse::<u64>()
+ .ok()
+ })
+ });
+ println!(
+ "EXPORT_BENCH {}",
+ serde_json::json!({
+ "gap":gap,"streaming_audio":streaming,"build_seconds":build_seconds,
+ "first_progress_seconds":first_progress.load(Ordering::Relaxed) as f64 / 1_000_000.0,
+ "export_seconds":export_seconds,"total_seconds":total_seconds,"maxrss_kib":maxrss_kib,
+ "output":output
+ })
+ );
+}
diff --git a/crates/export/src/preview.rs b/crates/export/src/preview.rs
index 09e1172af..918bd05dd 100644
--- a/crates/export/src/preview.rs
+++ b/crates/export/src/preview.rs
@@ -4,8 +4,9 @@ use base64::{Engine, engine::general_purpose::STANDARD};
use cap_editor::EditorInstance;
use cap_project::{CursorEvents, ProjectConfiguration, RecordingMeta, TimelineFrameMapping, XY};
use cap_rendering::{
- FrameRenderer, ProjectUniforms, RecordingSegmentDecoders, RenderVideoConstants, RenderedFrame,
- RendererLayers, TransitionRenderInput, ZoomTransformTimeline,
+ FrameRenderer, PrecomputedCursorTimeline, ProjectUniforms, RecordingSegmentDecoders,
+ RenderVideoConstants, RenderedFrame, RendererLayers, TransitionRenderInput,
+ ZoomTransformTimeline,
};
use image::{
Rgba,
@@ -277,7 +278,9 @@ async fn render_preview_frame(
);
zoom_timeline.ensure_precomputed_until((frame_number as f32 + 1.0) / settings.fps as f32);
- let uniforms = ProjectUniforms::new(
+ let cursor_timeline =
+ PrecomputedCursorTimeline::from_project(segment_media.cursor, project_config);
+ let uniforms = ProjectUniforms::new_with_precomputed_cursor(
render_constants,
project_config,
frame_number,
@@ -287,6 +290,7 @@ async fn render_preview_frame(
&segment_frames,
total_duration,
&zoom_timeline,
+ &cursor_timeline,
);
let mut frame_renderer = FrameRenderer::new(render_constants);
@@ -341,7 +345,16 @@ async fn render_preview_frame(
outgoing.segment.recording_clip,
);
outgoing_zoom.ensure_precomputed_until((frame_number as f32 + 1.0) / settings.fps as f32);
- let outgoing_uniforms = ProjectUniforms::new(
+ let outgoing_cursor_timeline = if outgoing.segment.recording_clip == segment.recording_clip
+ {
+ None
+ } else {
+ Some(PrecomputedCursorTimeline::from_project(
+ outgoing_media.cursor,
+ project_config,
+ ))
+ };
+ let outgoing_uniforms = ProjectUniforms::new_with_precomputed_cursor(
render_constants,
project_config,
frame_number,
@@ -351,6 +364,9 @@ async fn render_preview_frame(
&outgoing_frames,
total_duration,
&outgoing_zoom,
+ outgoing_cursor_timeline
+ .as_ref()
+ .unwrap_or(&cursor_timeline),
);
frame_renderer
diff --git a/crates/media-info/examples/packed-audio-wrapping-benchmark.rs b/crates/media-info/examples/packed-audio-wrapping-benchmark.rs
new file mode 100644
index 000000000..56bc60a24
--- /dev/null
+++ b/crates/media-info/examples/packed-audio-wrapping-benchmark.rs
@@ -0,0 +1,49 @@
+use cap_media_info::AudioInfo;
+use ffmpeg::format::{Sample, sample::Type};
+use std::{
+ collections::hash_map::DefaultHasher,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ time::Instant,
+};
+
+fn main() {
+ println!("format,channels,frames,sample,ns_per_call,fingerprint");
+ for (format, channels) in [
+ (Sample::F32(Type::Packed), 1),
+ (Sample::F32(Type::Packed), 2),
+ (Sample::F32(Type::Packed), 8),
+ (Sample::F32(Type::Packed), 16),
+ (Sample::F32(Type::Packed), 32),
+ (Sample::I16(Type::Packed), 16),
+ (Sample::F64(Type::Packed), 16),
+ ] {
+ let info = AudioInfo::new_raw(format, 48_000, channels);
+ for (frames, iterations) in [(64, 1000), (480, 500), (4096, 100)] {
+ let bytes = (0..frames * usize::from(channels) * info.sample_size())
+ .map(|index| (index * 97 + 31) as u8)
+ .collect::<Vec<_>>();
+ let wrap = || info.wrap_frame_with_max_channels(black_box(&bytes), black_box(8));
+ let frame = wrap();
+ assert_eq!(frame.samples(), frames);
+ assert_eq!(frame.channels(), channels.min(8));
+ let mut hash = DefaultHasher::new();
+ let used = frames * usize::from(channels.min(8)) * info.sample_size();
+ frame.data(0)[..used].hash(&mut hash);
+ let fingerprint = hash.finish();
+ for sample in 0..18 {
+ let start = Instant::now();
+ for _ in 0..iterations {
+ black_box(wrap());
+ }
+ let elapsed = start.elapsed().as_nanos() as f64 / iterations as f64;
+ if sample >= 3 {
+ println!(
+ "{format:?},{channels},{frames},{},{elapsed:.3},{fingerprint}",
+ sample - 3
+ );
+ }
+ }
+ }
+ }
+}
diff --git a/crates/media-info/src/lib.rs b/crates/media-info/src/lib.rs
index b2eb91ead..67ba2b2e4 100644
--- a/crates/media-info/src/lib.rs
+++ b/crates/media-info/src/lib.rs
@@ -209,17 +209,12 @@ impl AudioInfo {
// frame is allocated with parameters derived from packed_data, so this is safe
frame.data_mut(0)[0..packed_data.len()].copy_from_slice(packed_data);
} else if frame.is_packed() && input_channels > out_channels {
- for (chunk_index, packed_chunk) in packed_data.chunks(packed_sample_size).enumerate() {
- let start = chunk_index * sample_size * out_channels;
-
- let copy_len = sample_size * out_channels;
-
- if let (Some(chunk_slice), Some(frame_slice)) = (
- packed_chunk.get(0..copy_len),
- frame.data_mut(0).get_mut(start..start + copy_len),
- ) {
- frame_slice.copy_from_slice(chunk_slice);
- }
+ let copy_len = sample_size * out_channels;
+ for (packed_chunk, frame_chunk) in packed_data
+ .chunks_exact(packed_sample_size)
+ .zip(frame.data_mut(0).chunks_exact_mut(copy_len))
+ {
+ frame_chunk.copy_from_slice(&packed_chunk[..copy_len]);
}
} else {
for (chunk_index, packed_chunk) in packed_data.chunks(packed_sample_size).enumerate() {
@@ -490,6 +485,45 @@ mod tests {
assert_eq!(&frame.data(0)[0..4], &[1, 2, 3, 4]);
}
+ #[test]
+ fn packed_channel_limits_preserve_complete_frames_and_sample_bytes() {
+ for format in [
+ Sample::U8(Type::Packed),
+ Sample::I16(Type::Packed),
+ Sample::I32(Type::Packed),
+ Sample::F32(Type::Packed),
+ Sample::F64(Type::Packed),
+ ] {
+ for channels in [0, 1, 2, 6, 8, 9, 16, 32] {
+ let info = AudioInfo::new_raw(format, 48_000, channels);
+ let input_stride = info.sample_size() * usize::from(channels.max(1));
+ for max in [0, 1, 2, 8, 64] {
+ let output_channels = usize::from(channels.max(1)).min(max.max(1)).min(8);
+ let output_stride = info.sample_size() * output_channels;
+ for samples in [0, 1, 3, 64] {
+ for tail in [0, input_stride - 1] {
+ let input = (0..samples * input_stride + tail)
+ .map(|index| (index * 97 + 31) as u8)
+ .collect::<Vec<_>>();
+ let expected = input
+ .chunks_exact(input_stride)
+ .flat_map(|frame| frame[..output_stride].iter().copied())
+ .collect::<Vec<_>>();
+ let frame = info.wrap_frame_with_max_channels(&input, max);
+ assert_eq!(frame.samples(), samples);
+ assert_eq!(frame.channels() as usize, output_channels);
+ assert_eq!(frame.rate(), 48_000);
+ assert_eq!(frame.format(), format);
+ if samples > 0 {
+ assert_eq!(&frame.data(0)[..expected.len()], expected);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
#[test]
fn wrap_packed_frame_max_channels() {
let info = AudioInfo::new_raw(Sample::U8(Type::Packed), 2, 4);
diff --git a/crates/project/examples/caption-loading-benchmark.rs b/crates/project/examples/caption-loading-benchmark.rs
new file mode 100644
index 000000000..3ed45cba7
--- /dev/null
+++ b/crates/project/examples/caption-loading-benchmark.rs
@@ -0,0 +1,92 @@
+use cap_project::{CaptionSegment, CaptionWord, CaptionsData, ProjectConfiguration, RecordingMeta};
+use std::{
+ collections::hash_map::DefaultHasher,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ time::Instant,
+};
+
+fn main() {
+ for (name, count, timeline, sidecar) in [
+ ("empty", 0, 0, false),
+ ("30_no_timeline", 30, 0, false),
+ ("150_no_timeline", 150, 0, false),
+ ("900_no_timeline", 900, 0, false),
+ ("900_sidecar_no_timeline", 900, 0, true),
+ ("900_empty_timeline", 900, 1, false),
+ ("900_existing_track", 900, 2, false),
+ ] {
+ let captions = CaptionsData {
+ source_timed: true,
+ segments: (0..count)
+ .map(|index| CaptionSegment {
+ id: format!("source-{index}"),
+ start: index as f32 * 4.0,
+ end: index as f32 * 4.0 + 4.0,
+ text: "A caption with café and Unicode 字 to preserve".into(),
+ words: (0..10)
+ .map(|word| CaptionWord {
+ text: format!("word{word} café 字"),
+ start: index as f32 * 4.0 + word as f32 * 0.4,
+ end: index as f32 * 4.0 + (word + 1) as f32 * 0.4,
+ })
+ .collect(),
+ })
+ .collect(),
+ ..Default::default()
+ };
+ let directory = tempfile::tempdir().unwrap();
+ let mut meta: RecordingMeta = serde_json::from_value(serde_json::json!({
+ "pretty_name": "Caption loading benchmark",
+ "display": {"path": "unused.mp4", "fps": 30},
+ "camera": null, "audio": null, "cursor": null,
+ }))
+ .unwrap();
+ meta.project_path = directory.path().to_path_buf();
+ let mut config = ProjectConfiguration {
+ captions: (!sidecar).then(|| captions.clone()),
+ ..Default::default()
+ };
+ if timeline > 0 {
+ config.timeline = Some(serde_json::from_value(serde_json::json!({
+ "segments": [{"start": 0.0, "end": 3600.0, "timescale": 1.0}], "zoomSegments": [],
+ "captionSegments": if timeline == 2 { serde_json::json!([{"id": "saved", "start": 0.0, "end": 1.0, "text": "Saved override", "words": []}]) } else { serde_json::json!([]) },
+ })).unwrap());
+ }
+ config.write(directory.path()).unwrap();
+ if sidecar {
+ std::fs::write(
+ directory.path().join("captions.json"),
+ serde_json::to_vec(&captions).unwrap(),
+ )
+ .unwrap();
+ }
+ for _ in 0..3 {
+ black_box(meta.project_config());
+ }
+ let output = meta.project_config();
+ assert_eq!(output.captions.as_ref().unwrap().segments.len(), count);
+ if timeline == 0 {
+ assert!(output.timeline.is_none());
+ } else {
+ assert_eq!(
+ output.timeline.as_ref().unwrap().caption_segments.len(),
+ if timeline == 2 { 1 } else { count }
+ );
+ }
+ let mut hash = DefaultHasher::new();
+ serde_json::to_vec(&output).unwrap().hash(&mut hash);
+ let fingerprint = hash.finish();
+ for sample in 0..15 {
+ let start = Instant::now();
+ for _ in 0..10 {
+ black_box(black_box(&meta).project_config());
+ }
+ println!(
+ "LOAD_BENCH {}",
+ serde_json::json!({"case": name, "sample": sample,
+ "ns_per_call": start.elapsed().as_nanos() as f64 / 10.0, "fingerprint": fingerprint})
+ );
+ }
+ }
+}
diff --git a/crates/project/examples/caption-projection-benchmark.rs b/crates/project/examples/caption-projection-benchmark.rs
index c2661c57f..a96421381 100644
--- a/crates/project/examples/caption-projection-benchmark.rs
+++ b/crates/project/examples/caption-projection-benchmark.rs
@@ -69,21 +69,29 @@ fn main() -> Result<(), Box<dyn Error>> {
return Err("iterations must be positive".into());
}
let selected: Option<usize> = arguments.next().map(|value| value.parse()).transpose()?;
+ let order = arguments.next().unwrap_or_else(|| "ordered".into());
let cases = [
(3600, 1, 0, false),
(600, 20, 28, false),
(3600, 60, 60, false),
(3600, 1, 0, true),
+ (3600, 500, 4, false),
];
- if selected.is_some_and(|case| case >= cases.len()) || arguments.next().is_some() {
- return Err("case must be 0, 1, 2 or 3; no additional arguments are accepted".into());
+ if selected.is_some_and(|case| case >= cases.len())
+ || !matches!(order.as_str(), "ordered" | "reversed")
+ || arguments.next().is_some()
+ {
+ return Err("expected case 0..=4 and order: ordered or reversed".into());
}
- println!("case,clips,captions,words,sample,elapsed_ms,fingerprint");
+ println!("case,order,clips,captions,words,sample,elapsed_ms,fingerprint");
for (case, (seconds, clips, spacing, styled)) in cases.into_iter().enumerate() {
if selected.is_some_and(|selected| selected != case) {
continue;
}
let mut project = fixture(seconds, clips, spacing);
+ if order == "reversed" {
+ project.timeline.as_mut().unwrap().segments.reverse();
+ }
let durations = [5.0, seconds as f64];
if styled {
synchronize_captions(&mut project, &durations);
@@ -124,7 +132,7 @@ fn main() -> Result<(), Box<dyn Error>> {
expected_captions
);
println!(
- "{case},{clips},{expected_captions},{words},{sample},{elapsed_ms:.6},{fingerprint}"
+ "{case},{order},{clips},{expected_captions},{words},{sample},{elapsed_ms:.6},{fingerprint}"
);
}
let mut hasher = DefaultHasher::new();
diff --git a/crates/project/src/caption_timing.rs b/crates/project/src/caption_timing.rs
index 93c855a14..5f9fa655b 100644
--- a/crates/project/src/caption_timing.rs
+++ b/crates/project/src/caption_timing.rs
@@ -1,10 +1,9 @@
+use crate::caption_words::{MAX_CAPTION_WORD_DURATION, caption_text_from_words};
use crate::{
CaptionSegment, CaptionTrackSegment, CaptionWord, ProjectConfiguration, TimelineConfiguration,
};
use std::collections::HashMap;
-const MAX_CAPTION_WORD_DURATION: f32 = 2.5;
-
pub(crate) fn clip_timeline_offsets(timeline: &TimelineConfiguration) -> Vec<f64> {
let mut offset = 0.0;
timeline
@@ -22,55 +21,6 @@ pub(crate) fn clip_timeline_offsets(timeline: &TimelineConfiguration) -> Vec<f64
.collect()
}
-fn caption_char_attaches_to_previous(value: char) -> bool {
- matches!(
- value,
- ',' | '.'
- | '!'
- | '?'
- | ';'
- | ':'
- | '%'
- | ')'
- | ']'
- | '}'
- | '\''
- | '’'
- | '、'
- | '。'
- | '!'
- | '?'
- | ';'
- | ':'
- | ','
- )
-}
-
-fn caption_token_attaches_to_previous(text: &str) -> bool {
- text.trim()
- .chars()
- .next()
- .is_some_and(caption_char_attaches_to_previous)
-}
-
-fn caption_text_from_words<'a>(words: impl IntoIterator<Item = &'a CaptionWord>) -> String {
- let mut text = String::new();
-
- for word in words {
- let word_text = word.text.trim();
- if word_text.is_empty() {
- continue;
- }
-
- if !text.is_empty() && !caption_token_attaches_to_previous(word_text) {
- text.push(' ');
- }
- text.push_str(word_text);
- }
-
- text
-}
-
const CAPTION_EDL_SEPARATOR: &str = "::edl";
pub fn source_caption_id(track_id: &str) -> &str {
@@ -220,6 +170,12 @@ fn map_captions_to_edited_timeline(
}
let mappings = build_source_to_edited_mappings(timeline, recording_durations);
+ let mappings_ordered = mappings
+ .iter()
+ .all(|mapping| mapping.source_start.is_finite() && mapping.source_end.is_finite())
+ && mappings.windows(2).all(|pair| {
+ pair[0].source_start <= pair[1].source_start && pair[0].source_end <= pair[1].source_end
+ });
let holds = timeline.hold_windows();
let hold_adjusted = |start: f64, end: f64| {
if holds.is_empty() {
@@ -253,7 +209,21 @@ fn map_captions_to_edited_timeline(
)
};
- for mapping in &mappings {
+ let bounds = if caption.words.is_empty() {
+ Some((f64::from(caption.start), f64::from(caption.end)))
+ } else {
+ word_bounds
+ };
+ let caption_mappings = match bounds {
+ Some((start, end)) if mappings_ordered && start.is_finite() && end.is_finite() => {
+ let first = mappings.partition_point(|mapping| mapping.source_end <= start);
+ let count = mappings[first..].partition_point(|mapping| mapping.source_start < end);
+ &mappings[first..first + count]
+ }
+ _ => &mappings,
+ };
+
+ for mapping in caption_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)
diff --git a/crates/project/src/caption_timing/projection_tests.rs b/crates/project/src/caption_timing/projection_tests.rs
index 1c88c4a5f..9c064903e 100644
--- a/crates/project/src/caption_timing/projection_tests.rs
+++ b/crates/project/src/caption_timing/projection_tests.rs
@@ -495,3 +495,61 @@ fn caption_overrides_preserve_nonfinite_float_bits() {
);
}
}
+
+#[test]
+fn mapping_bounds_preserve_shared_edges_overlaps_duplicates_and_fallbacks() {
+ let sequential: Vec<_> = (0..64)
+ .map(|index| (index as f64, index as f64 + 1.0))
+ .collect();
+ let overlapping: Vec<_> = (0..64)
+ .map(|index| (index as f64 * 0.5, index as f64 * 0.5 + 2.0))
+ .collect();
+ let duplicated: Vec<_> = sequential.iter().flat_map(|range| [*range; 2]).collect();
+ let mut nested = sequential.clone();
+ nested[0].1 = 100.0;
+ let mut reversed = sequential.clone();
+ reversed.reverse();
+ for with_words in [false, true] {
+ let captions: Vec<_> = [
+ -1.0,
+ -0.0,
+ 0.0,
+ f32::from_bits(1.0_f32.to_bits() - 1),
+ 1.0,
+ f32::from_bits(1.0_f32.to_bits() + 1),
+ 30.0,
+ 63.0,
+ 64.0,
+ 100.0,
+ ]
+ .into_iter()
+ .enumerate()
+ .map(|(index, start)| CaptionSegment {
+ id: format!("edge-{index}"),
+ start: if with_words { -100.0 } else { start },
+ end: if with_words { -99.0 } else { start + 2.5 },
+ text: "parent".into(),
+ words: if with_words {
+ vec![
+ CaptionWord {
+ text: "later".into(),
+ start: start + 2.0,
+ end: start + 2.5,
+ },
+ CaptionWord {
+ text: "earlier".into(),
+ start,
+ end: start + 0.5,
+ },
+ ]
+ } else {
+ Vec::new()
+ },
+ })
+ .collect();
+ for ranges in [&sequential, &overlapping, &duplicated, &nested, &reversed] {
+ assert_projection_matches(&captions, &timeline(ranges), &[100.0]);
+ }
+ assert_projection_matches(&[], &timeline(&sequential), &[100.0]);
+ }
+}
diff --git a/crates/project/src/caption_words.rs b/crates/project/src/caption_words.rs
new file mode 100644
index 000000000..8fdb6c39f
--- /dev/null
+++ b/crates/project/src/caption_words.rs
@@ -0,0 +1,274 @@
+use crate::CaptionWord;
+
+/// Whisper/Parakeet sometimes stretch a trailing word's end across a following
+/// silence (e.g. a 16s "seconds."), which leaves the rendered caption stuck on
+/// screen and duplicates the word across timeline cuts once projected. Real
+/// spoken words never approach this, so cap each word's duration to keep timing
+/// tied to speech rather than silence.
+pub const MAX_CAPTION_WORD_DURATION: f32 = 2.5;
+pub const TARGET_CAPTION_WORDS_PER_SEGMENT: usize = 6;
+pub const MAX_CAPTION_WORDS_PER_SEGMENT: usize = 8;
+pub const MIN_FINAL_CAPTION_WORDS: usize = 3;
+
+fn caption_char_attaches_to_previous(value: char) -> bool {
+ matches!(
+ value,
+ ',' | '.'
+ | '!'
+ | '?'
+ | ';'
+ | ':'
+ | '%'
+ | ')'
+ | ']'
+ | '}'
+ | '\''
+ | '’'
+ | '、'
+ | '。'
+ | '!'
+ | '?'
+ | ';'
+ | ':'
+ | ','
+ )
+}
+
+fn caption_token_attaches_to_previous(text: &str) -> bool {
+ text.trim()
+ .chars()
+ .next()
+ .is_some_and(caption_char_attaches_to_previous)
+}
+
+/// A caption segment should not end on a one letter word or a short connector
+/// when more words follow, since that reads badly on screen. Comparing
+/// `chars().count()` matters: a byte length check counts non-ASCII letters like
+/// "à" as two and treats them as strong boundary words.
+pub fn caption_boundary_word_is_weak(word: &CaptionWord) -> bool {
+ let normalized = word
+ .text
+ .trim()
+ .chars()
+ .filter(|c| c.is_alphanumeric())
+ .collect::<String>()
+ .to_lowercase();
+
+ normalized.chars().count() <= 1
+ || matches!(
+ normalized.as_str(),
+ "an" | "as"
+ | "at"
+ | "be"
+ | "by"
+ | "do"
+ | "he"
+ | "if"
+ | "in"
+ | "is"
+ | "it"
+ | "me"
+ | "my"
+ | "of"
+ | "on"
+ | "or"
+ | "so"
+ | "to"
+ | "up"
+ | "we"
+ )
+}
+
+pub fn normalize_caption_words(words: Vec<CaptionWord>) -> Vec<CaptionWord> {
+ let mut normalized: Vec<CaptionWord> = Vec::with_capacity(words.len());
+
+ for word in words {
+ let text = word.text.trim();
+ if text.is_empty() {
+ continue;
+ }
+
+ if caption_token_attaches_to_previous(text)
+ && let Some(previous) = normalized.last_mut()
+ {
+ previous.text.push_str(text);
+ previous.end = word.end;
+ } else {
+ normalized.push(CaptionWord {
+ text: text.to_string(),
+ start: word.start,
+ end: word.end,
+ });
+ }
+ }
+
+ for word in &mut normalized {
+ word.end = word.end.min(word.start + MAX_CAPTION_WORD_DURATION);
+ }
+
+ normalized
+}
+
+pub fn caption_text_from_words<'a>(words: impl IntoIterator<Item = &'a CaptionWord>) -> String {
+ let mut text = String::new();
+
+ for word in words {
+ let word_text = word.text.trim();
+ if word_text.is_empty() {
+ continue;
+ }
+
+ if !text.is_empty() && !caption_token_attaches_to_previous(word_text) {
+ text.push(' ');
+ }
+ text.push_str(word_text);
+ }
+
+ text
+}
+
+/// Groups normalized words into caption segments aiming for
+/// [`TARGET_CAPTION_WORDS_PER_SEGMENT`] words. The boundary word may move past
+/// the target, up to [`MAX_CAPTION_WORDS_PER_SEGMENT`], to avoid ending on a
+/// weak word, and a trailing fragment shorter than [`MIN_FINAL_CAPTION_WORDS`]
+/// merges into the previous segment when it starts with a weak word, which can
+/// put the merged segment over the max.
+pub fn caption_word_chunks(words: &[CaptionWord]) -> Vec<&[CaptionWord]> {
+ let mut chunks = Vec::new();
+ let mut start = 0;
+
+ while start < words.len() {
+ let remaining = words.len() - start;
+ if remaining <= TARGET_CAPTION_WORDS_PER_SEGMENT {
+ chunks.push(&words[start..]);
+ break;
+ }
+
+ let mut end = (start + TARGET_CAPTION_WORDS_PER_SEGMENT).min(words.len());
+ while end < words.len()
+ && caption_boundary_word_is_weak(&words[end - 1])
+ && end - start < MAX_CAPTION_WORDS_PER_SEGMENT
+ {
+ end += 1;
+ }
+
+ let remaining_after = words.len() - end;
+ if remaining_after > 0
+ && remaining_after < MIN_FINAL_CAPTION_WORDS
+ && caption_boundary_word_is_weak(&words[end])
+ {
+ end = words.len();
+ }
+
+ chunks.push(&words[start..end]);
+ start = end;
+ }
+
+ chunks
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn word(text: &str, index: usize) -> CaptionWord {
+ CaptionWord {
+ text: text.to_string(),
+ start: index as f32,
+ end: index as f32 + 0.5,
+ }
+ }
+
+ #[test]
+ fn normalize_caption_words_attaches_punctuation() {
+ let words = normalize_caption_words(vec![
+ word("test", 0),
+ word(",", 1),
+ word("test", 2),
+ word(".", 3),
+ ]);
+
+ assert_eq!(caption_text_from_words(&words), "test, test.");
+ assert_eq!(words.len(), 2);
+ }
+
+ #[test]
+ fn normalize_caption_words_clamps_inflated_trailing_word() {
+ let words = normalize_caption_words(vec![CaptionWord {
+ text: "seconds.".to_string(),
+ start: 53.92,
+ end: 70.16,
+ }]);
+
+ assert_eq!(words.len(), 1);
+ assert!((words[0].end - (53.92 + MAX_CAPTION_WORD_DURATION)).abs() < 1e-4);
+ }
+
+ #[test]
+ fn normalize_caption_words_keeps_normal_word_durations() {
+ let words = normalize_caption_words(vec![CaptionWord {
+ text: "hello".to_string(),
+ start: 1.0,
+ end: 1.4,
+ }]);
+
+ assert_eq!(words.len(), 1);
+ assert!((words[0].end - 1.4).abs() < 1e-4);
+ }
+
+ #[test]
+ fn caption_word_chunks_do_not_end_on_short_connector_when_more_words_follow() {
+ for connector in ["I", "à", "à,", "a\u{300}", "日"] {
+ let words = [
+ "This", "is", "where", "we", "record", connector, "want", "clean", "captions",
+ ]
+ .iter()
+ .enumerate()
+ .map(|(index, text)| word(text, index))
+ .collect::<Vec<_>>();
+
+ let chunks = caption_word_chunks(&words);
+
+ assert_eq!(
+ caption_text_from_words(chunks[0]),
+ format!("This is where we record {connector} want"),
+ "connector {connector}"
+ );
+ assert_eq!(caption_text_from_words(chunks[1]), "clean captions");
+ }
+ }
+
+ #[test]
+ fn caption_word_chunks_merge_short_tail_starting_with_connector() {
+ for connector in ["I", "à", "à,", "a\u{300}", "日"] {
+ let words = [
+ "These", "captions", "should", "stay", "grouped", "together", connector, "Paris",
+ ]
+ .iter()
+ .enumerate()
+ .map(|(index, text)| word(text, index))
+ .collect::<Vec<_>>();
+
+ let chunks = caption_word_chunks(&words);
+
+ assert_eq!(chunks.len(), 1, "connector {connector}");
+ assert_eq!(
+ caption_text_from_words(chunks[0]),
+ format!("These captions should stay grouped together {connector} Paris")
+ );
+ }
+ }
+
+ #[test]
+ fn caption_word_chunks_target_size_groups_strong_boundaries() {
+ let words = (0..12)
+ .map(|index| word(&format!("w{index}"), index))
+ .collect::<Vec<_>>();
+
+ let chunks = caption_word_chunks(&words);
+
+ assert_eq!(chunks.len(), 2);
+ assert_eq!(chunks[0].len(), 6);
+ assert_eq!(chunks[1].len(), 6);
+ }
+}
diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs
index 080355b05..f4bdcf586 100644
--- a/crates/project/src/lib.rs
+++ b/crates/project/src/lib.rs
@@ -1,5 +1,6 @@
mod animated_gradient;
mod caption_timing;
+pub mod caption_words;
#[cfg(test)]
mod config_write_tests;
mod configuration;
@@ -9,6 +10,11 @@ mod meta;
pub use animated_gradient::*;
pub use caption_timing::{derive_caption_track_segments, source_caption_id, synchronize_captions};
+pub use caption_words::{
+ MAX_CAPTION_WORD_DURATION, MAX_CAPTION_WORDS_PER_SEGMENT, MIN_FINAL_CAPTION_WORDS,
+ TARGET_CAPTION_WORDS_PER_SEGMENT, caption_boundary_word_is_weak, caption_text_from_words,
+ caption_word_chunks, normalize_caption_words,
+};
pub use configuration::*;
pub use cursor::*;
pub use keyboard::*;
diff --git a/crates/project/src/meta.rs b/crates/project/src/meta.rs
index f5388250f..6c5eb7599 100644
--- a/crates/project/src/meta.rs
+++ b/crates/project/src/meta.rs
@@ -276,13 +276,12 @@ impl RecordingMeta {
}
if let Some(ref captions) = config.captions {
- let timeline_has_captions = config
+ let timeline_needs_captions = config
.timeline
.as_ref()
- .map(|t| !t.caption_segments.is_empty())
- .unwrap_or(false);
+ .is_some_and(|timeline| timeline.caption_segments.is_empty());
- if !timeline_has_captions && !captions.segments.is_empty() {
+ if timeline_needs_captions && !captions.segments.is_empty() {
let caption_track_segments: Vec<crate::CaptionTrackSegment> = captions
.segments
.iter()
@@ -824,6 +823,80 @@ mod metadata_save_tests {
}));
}
+ #[test]
+ fn caption_loading_preserves_missing_empty_and_populated_timelines() {
+ let captions = CaptionsData {
+ source_timed: true,
+ segments: vec![crate::CaptionSegment {
+ id: "source-caption".into(),
+ start: 1.25,
+ end: 2.75,
+ text: "Café 字".into(),
+ words: vec![crate::CaptionWord {
+ text: "Café".into(),
+ start: 1.25,
+ end: 2.0,
+ }],
+ }],
+ ..Default::default()
+ };
+ let saved: crate::CaptionTrackSegment = serde_json::from_value(serde_json::json!({
+ "id": "saved", "start": 5.0, "end": 6.0, "text": "Saved", "words": [],
+ "colorOverride": "#123456", "fadeDurationOverride": 0.75,
+ }))
+ .unwrap();
+ let generated = serde_json::json!([{
+ "id": "source-caption", "start": 1.25, "end": 2.75, "text": "Café 字",
+ "words": [{"text": "Café", "start": 1.25, "end": 2.0}],
+ "fadeDurationOverride": null, "lingerDurationOverride": null,
+ "positionOverride": null, "colorOverride": null,
+ "backgroundColorOverride": null, "fontSizeOverride": null,
+ }]);
+ for sidecar in [false, true] {
+ for timeline in 0..3 {
+ let directory = tempfile::tempdir().unwrap();
+ let meta = recording(directory.path());
+ let mut config = ProjectConfiguration {
+ captions: (!sidecar).then(|| captions.clone()),
+ ..Default::default()
+ };
+ if timeline > 0 {
+ config.timeline = Some(serde_json::from_value(serde_json::json!({
+ "segments": [], "zoomSegments": [],
+ "captionSegments": if timeline == 2 { serde_json::to_value([&saved]).unwrap() } else { serde_json::json!([]) },
+ })).unwrap());
+ }
+ if !sidecar || timeline > 0 {
+ config.write(directory.path()).unwrap();
+ }
+ if sidecar {
+ std::fs::write(
+ directory.path().join("captions.json"),
+ serde_json::to_vec(&captions).unwrap(),
+ )
+ .unwrap();
+ }
+ let loaded = meta.project_config();
+ assert_eq!(
+ serde_json::to_value(loaded.captions.unwrap()).unwrap(),
+ serde_json::to_value(&captions).unwrap()
+ );
+ if timeline == 0 {
+ assert!(loaded.timeline.is_none());
+ } else {
+ assert_eq!(
+ serde_json::to_value(loaded.timeline.unwrap().caption_segments).unwrap(),
+ if timeline == 1 {
+ generated.clone()
+ } else {
+ serde_json::to_value([&saved]).unwrap()
+ }
+ );
+ }
+ }
+ }
+ }
+
#[test]
fn saved_caption_master_takes_precedence_over_legacy_sidecar() {
let directory = tempfile::tempdir().unwrap();
diff --git a/crates/recording/Cargo.toml b/crates/recording/Cargo.toml
index 29b1ee5fa..84d8000ac 100644
--- a/crates/recording/Cargo.toml
+++ b/crates/recording/Cargo.toml
@@ -103,6 +103,10 @@ scap-cpal = { path = "../scap-cpal" }
[target.'cfg(target_os = "linux")'.dependencies]
ashpd = { version = "0.11.0", default-features = false, features = ["tokio"] }
+# Reads screencast DMA-BUFs with GPU-tiled modifiers (niri on NVIDIA). Both
+# are already in the tree through wgpu's GL backend; libEGL loads at runtime.
+glow = "0.16"
+khronos-egl = { version = "6", features = ["dynamic"] }
pipewire = "0.10.0"
x11rb = { version = "0.13.2", features = ["composite", "shm", "xfixes"] }
diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rs
index 0d3398e86..45dcf467d 100644
--- a/crates/recording/src/output_pipeline/core.rs
+++ b/crates/recording/src/output_pipeline/core.rs
@@ -4127,11 +4127,7 @@ impl PreparedAudioSources {
scope.fail_required(format!("Required audio processing failed: {error:#}"));
}
let cancelled_at = Instant::now();
- let pause_at_stop = if mixed_audio {
- shared_pause.captured_pause_duration(cancelled_at)
- } else {
- shared_pause.total_pause_duration()
- };
+ let pause_at_stop = shared_pause.captured_pause_duration(cancelled_at);
let cancellation_target_elapsed = if was_cancelled && !audio_degraded {
Some(cancelled_at.saturating_duration_since(timestamps.instant()).saturating_sub(pause_at_stop))
} else {
@@ -10319,6 +10315,90 @@ mod tests {
assert_eq!(harness.overlap_dropped_frames(), 0);
}
+ #[tokio::test]
+ async fn single_source_stop_while_paused_excludes_unfinished_pause_from_tail() {
+ for cycles in [1, 8] {
+ let temp = tempfile::tempdir().unwrap();
+ let timestamps = Timestamps::now();
+ let sent = Arc::new(std::sync::Mutex::new(Vec::new()));
+ let pipeline = OutputPipeline::builder(temp.path().join("paused-audio.ogg"))
+ .with_timestamps(timestamps)
+ .with_audio_anchor(AudioAnchor::PipelineEpoch)
+ .with_audio_source::<StopFrameSource>(None)
+ .build::<RecordingAudioMuxer>(sent.clone())
+ .await
+ .unwrap();
+ let mut active_start = timestamps.instant();
+ let mut active_duration = Duration::ZERO;
+ for cycle in 0..cycles {
+ tokio::time::sleep(Duration::from_millis(80 / cycles)).await;
+ let paused_at = Instant::now();
+ pipeline.shared_pause.set_paused_at(true, paused_at);
+ active_duration += paused_at.duration_since(active_start);
+ tokio::time::sleep(Duration::from_millis(40)).await;
+ if cycle + 1 < cycles {
+ active_start = Instant::now();
+ pipeline.shared_pause.set_paused_at(false, active_start);
+ }
+ }
+ pipeline.stop().await.unwrap();
+ let samples = sent
+ .lock()
+ .unwrap()
+ .iter()
+ .map(|frame| frame.samples)
+ .sum::<usize>();
+ let encoded_duration =
+ Duration::from_secs_f64(samples as f64 / f64::from(TEST_SAMPLE_RATE));
+ assert!(
+ encoded_duration.abs_diff(active_duration) < Duration::from_millis(2),
+ "{cycles} cycles: encoded {encoded_duration:?}, active {active_duration:?}"
+ );
+ }
+ }
+
+ #[tokio::test]
+ async fn single_source_paused_stop_writes_only_active_time_to_ogg() {
+ let temp = tempfile::tempdir().unwrap();
+ let path = temp.path().join("paused.ogg");
+ let timestamps = Timestamps::now();
+ let info = test_audio_info();
+ let (mut sender, receiver) = mpsc::channel(4);
+ let pipeline = OutputPipeline::builder(path.clone())
+ .with_timestamps(timestamps)
+ .with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(
+ info, receiver,
+ ))
+ .build::<crate::ffmpeg::OggMuxer>(Default::default())
+ .await
+ .unwrap();
+ let mut first = pipeline.first_timestamp_watch();
+ sender
+ .send(AudioFrame::new(
+ make_test_frame(&info, 480, 0.25),
+ Timestamp::Instant(timestamps.instant()),
+ ))
+ .await
+ .unwrap();
+ tokio::time::timeout(Duration::from_secs(2), first.wait_for(Option::is_some))
+ .await
+ .unwrap()
+ .unwrap();
+ tokio::time::sleep(Duration::from_millis(100)).await;
+ let paused_at = Instant::now();
+ pipeline.shared_pause.set_paused_at(true, paused_at);
+ let active_seconds = paused_at.duration_since(timestamps.instant()).as_secs_f64();
+ tokio::time::sleep(Duration::from_millis(300)).await;
+ pipeline.stop().await.unwrap();
+ let input = ffmpeg::format::input(&path).unwrap();
+ let stream = input.streams().best(ffmpeg::media::Type::Audio).unwrap();
+ let encoded_seconds = stream.duration() as f64 * f64::from(stream.time_base());
+ assert!(
+ (encoded_seconds - active_seconds).abs() < 0.03,
+ "encoded {encoded_seconds:.3}s, active {active_seconds:.3}s"
+ );
+ }
+
#[tokio::test]
async fn mixed_pipeline_stop_while_paused_drains_pre_pause_capture() {
let temp = tempfile::tempdir().unwrap();
diff --git a/crates/recording/src/screenshot.rs b/crates/recording/src/screenshot.rs
index 4551794eb..ae656ff7d 100644
--- a/crates/recording/src/screenshot.rs
+++ b/crates/recording/src/screenshot.rs
@@ -11,6 +11,8 @@ use anyhow::anyhow;
use image::{DynamicImage, RgbImage, RgbaImage};
#[cfg(target_os = "macos")]
use scap_ffmpeg::AsFFmpeg;
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+use std::cell::RefCell;
#[cfg(not(target_os = "linux"))]
use std::sync::{Arc, Mutex};
#[cfg(not(target_os = "linux"))]
@@ -1172,6 +1174,31 @@ pub fn uses_wayland_portal() -> bool {
prefers_wayland_portal()
}
+/// Asks the screenshot portal for one screenshot and throws it away. Before
+/// an app's first non-interactive screenshot the portal asks the user whether
+/// to allow it, and GNOME Shell only shows that question for the focused app,
+/// so this runs while Cap's picker still has focus, before it hides for the
+/// real capture.
+#[cfg(target_os = "linux")]
+pub async fn request_wayland_screenshot_permission() -> anyhow::Result<()> {
+ let screenshot = ashpd::desktop::screenshot::Screenshot::request()
+ .interactive(false)
+ .send()
+ .await
+ .context("Request Wayland screenshot from desktop portal")?
+ .response()
+ .context("The desktop did not allow Cap to take screenshots")?;
+ let uri = screenshot.uri();
+ if is_local_wayland_screenshot_uri(uri.scheme(), uri.host_str())
+ && let Ok(path) = uri.to_file_path()
+ && std::fs::symlink_metadata(&path).is_ok_and(|metadata| metadata.file_type().is_file())
+ && let Err(error) = std::fs::remove_file(&path)
+ {
+ tracing::warn!(path = %path.display(), %error, "Could not remove the permission screenshot");
+ }
+ Ok(())
+}
+
#[cfg(target_os = "linux")]
async fn capture_screenshot_wayland(target: &ScreenCaptureTarget) -> anyhow::Result<RgbImage> {
let display_id = wayland_screenshot_display_id(target)?;
@@ -1723,48 +1750,359 @@ fn crop_area_if_needed(
Ok(image)
}
+/// `Video::alloc` asks swscale for 32-byte lines. A row that is already a multiple
+/// of that is tightly packed, matching the buffer swscale would allocate itself.
#[cfg(any(target_os = "macos", target_os = "linux"))]
-fn convert_ffmpeg_frame_to_image(frame: &ffmpeg::frame::Video) -> anyhow::Result<RgbImage> {
- let mut scaler = ffmpeg::software::scaling::context::Context::get(
+const SCREENSHOT_RGB_STRIDE_ALIGN: usize = 32;
+
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+struct ScreenshotScaler {
+ context: ffmpeg::software::scaling::context::Context,
+ src_format: ffmpeg::format::Pixel,
+ src_width: u32,
+ src_height: u32,
+ dst_format: ffmpeg::format::Pixel,
+ dst_width: u32,
+ dst_height: u32,
+}
+
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+impl ScreenshotScaler {
+ fn matches(&self, frame: &ffmpeg::frame::Video) -> bool {
+ self.src_format == frame.format()
+ && self.src_width == frame.width()
+ && self.src_height == frame.height()
+ && self.dst_format == ffmpeg::format::Pixel::RGB24
+ && self.dst_width == frame.width()
+ && self.dst_height == frame.height()
+ }
+}
+
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+struct ScreenshotScalerSlot {
+ scaler: Option<ScreenshotScaler>,
+ #[cfg(test)]
+ builds: u64,
+}
+
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+thread_local! {
+ static SCREENSHOT_SCALER: RefCell<ScreenshotScalerSlot> = const {
+ RefCell::new(ScreenshotScalerSlot {
+ scaler: None,
+ #[cfg(test)]
+ builds: 0,
+ })
+ };
+}
+
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+fn with_screenshot_scaler<R>(
+ frame: &ffmpeg::frame::Video,
+ body: impl FnOnce(&mut ffmpeg::software::scaling::context::Context) -> R,
+) -> anyhow::Result<R> {
+ SCREENSHOT_SCALER.with(|slot| {
+ let mut slot = slot.borrow_mut();
+ if slot
+ .scaler
+ .as_ref()
+ .is_none_or(|scaler| !scaler.matches(frame))
+ {
+ let context = ffmpeg::software::scaling::context::Context::get(
+ frame.format(),
+ frame.width(),
+ frame.height(),
+ ffmpeg::format::Pixel::RGB24,
+ frame.width(),
+ frame.height(),
+ ffmpeg::software::scaling::flag::Flags::BILINEAR,
+ )
+ .context("Failed to create scaler")?;
+ slot.scaler = Some(ScreenshotScaler {
+ context,
+ src_format: frame.format(),
+ src_width: frame.width(),
+ src_height: frame.height(),
+ dst_format: ffmpeg::format::Pixel::RGB24,
+ dst_width: frame.width(),
+ dst_height: frame.height(),
+ });
+ #[cfg(test)]
+ {
+ slot.builds = slot.builds.saturating_add(1);
+ }
+ }
+ let scaler = slot
+ .scaler
+ .as_mut()
+ .expect("screenshot scaler is inserted before use");
+ Ok(body(&mut scaler.context))
+ })
+}
+
+#[cfg(test)]
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+fn clear_screenshot_scaler_cache() {
+ SCREENSHOT_SCALER.with(|slot| {
+ *slot.borrow_mut() = ScreenshotScalerSlot {
+ scaler: None,
+ builds: 0,
+ };
+ });
+}
+
+#[cfg(test)]
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+fn screenshot_scaler_builds() -> u64 {
+ SCREENSHOT_SCALER.with(|slot| slot.borrow().builds)
+}
+
+/// Returns `None` when the geometry is unusable or SSSE3 is unavailable, so the
+/// caller can fall back to swscale.
+#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
+fn bgrx_plane_to_rgb24(
+ src: &[u8],
+ src_stride: usize,
+ width: usize,
+ height: usize,
+) -> Option<Vec<u8>> {
+ if width == 0 || height == 0 {
+ return None;
+ }
+ let row_bytes = width.checked_mul(4)?;
+ let dst_stride = width.checked_mul(3)?;
+ if src_stride < row_bytes {
+ return None;
+ }
+ let src_needed = (height - 1)
+ .checked_mul(src_stride)?
+ .checked_add(row_bytes)?;
+ if src.len() < src_needed {
+ return None;
+ }
+ if !std::is_x86_feature_detected!("ssse3") {
+ return None;
+ }
+ let dst_len = height.checked_mul(dst_stride)?;
+ let mut dst = vec![0u8; dst_len];
+ // SAFETY: the checks above keep every source row's `width * 4` bytes and every
+ // destination row's `width * 3` bytes inside the two buffers, and SSSE3 is present.
+ unsafe { bgrx_plane_to_rgb24_ssse3(src, src_stride, &mut dst, width, height) };
+ Some(dst)
+}
+
+/// # Safety
+///
+/// `src` holds at least `(height - 1) * src_stride + width * 4` bytes, `src_stride >= width * 4`,
+/// and `dst` holds `height * width * 3` bytes. Those products do not overflow. SSSE3 is enabled.
+#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
+#[target_feature(enable = "ssse3")]
+unsafe fn bgrx_plane_to_rgb24_ssse3(
+ src: &[u8],
+ src_stride: usize,
+ dst: &mut [u8],
+ width: usize,
+ height: usize,
+) {
+ use std::arch::x86_64::*;
+
+ unsafe {
+ // B,G,R,x → R,G,B. The control bytes with the high bit set zero the hole
+ // after each group of four pixels (12 RGB bytes in a 16-byte register).
+ let mask = _mm_setr_epi8(2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1);
+ let dst_stride = width * 3;
+ for y in 0..height {
+ let srow = src.as_ptr().add(y * src_stride);
+ let drow = dst.as_mut_ptr().add(y * dst_stride);
+ let simd = width & !15;
+ let mut x = 0;
+ while x < simd {
+ let s0 = _mm_shuffle_epi8(_mm_loadu_si128(srow.add(x * 4) as *const __m128i), mask);
+ let s1 = _mm_shuffle_epi8(
+ _mm_loadu_si128(srow.add(x * 4 + 16) as *const __m128i),
+ mask,
+ );
+ let s2 = _mm_shuffle_epi8(
+ _mm_loadu_si128(srow.add(x * 4 + 32) as *const __m128i),
+ mask,
+ );
+ let s3 = _mm_shuffle_epi8(
+ _mm_loadu_si128(srow.add(x * 4 + 48) as *const __m128i),
+ mask,
+ );
+ // Each shuffle leaves a 4-byte hole. Slide the next group's bytes
+ // into that hole so the store is 16 packed RGB bytes, not 12.
+ let out = drow.add(x * 3);
+ let out0 = _mm_or_si128(s0, _mm_slli_si128(s1, 12));
+ let out1 = _mm_or_si128(_mm_srli_si128(s1, 4), _mm_slli_si128(s2, 8));
+ let out2 = _mm_or_si128(_mm_srli_si128(s2, 8), _mm_slli_si128(s3, 4));
+ _mm_storeu_si128(out as *mut __m128i, out0);
+ _mm_storeu_si128(out.add(16) as *mut __m128i, out1);
+ _mm_storeu_si128(out.add(32) as *mut __m128i, out2);
+ x += 16;
+ }
+ for i in simd..width {
+ let s = srow.add(i * 4);
+ let d = drow.add(i * 3);
+ *d = *s.add(2);
+ *d.add(1) = *s.add(1);
+ *d.add(2) = *s;
+ }
+ }
+ }
+}
+
+#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
+fn try_convert_bgrx_screenshot_ssse3(
+ frame: &ffmpeg::frame::Video,
+) -> anyhow::Result<Option<RgbImage>> {
+ if !matches!(
frame.format(),
- frame.width(),
- frame.height(),
- ffmpeg::format::Pixel::RGB24,
- frame.width(),
- frame.height(),
- ffmpeg::software::scaling::flag::Flags::BILINEAR,
- )
- .context("Failed to create scaler")?;
+ ffmpeg::format::Pixel::BGRZ | ffmpeg::format::Pixel::BGRA
+ ) {
+ return Ok(None);
+ }
+ let width = frame.width() as usize;
+ let height = frame.height() as usize;
+ if width == 0 || height == 0 {
+ return Ok(None);
+ }
+ // `Video::stride` casts linesize to usize. A bottom-up (negative) stride would
+ // wrap and `data()` would build a slice past the plane, so read the sign first.
+ // SAFETY: `frame` points at a live AVFrame for this call.
+ let (linesize, data_ptr) = unsafe {
+ let raw = frame.as_ptr();
+ ((*raw).linesize[0], (*raw).data[0])
+ };
+ if linesize <= 0 || data_ptr.is_null() {
+ return Ok(None);
+ }
+ let src_stride = linesize as usize;
+ if src_stride.checked_mul(height).is_none() {
+ return Ok(None);
+ }
+ let Some(rgb) = bgrx_plane_to_rgb24(frame.data(0), src_stride, width, height) else {
+ return Ok(None);
+ };
+ RgbImage::from_raw(frame.width(), frame.height(), rgb)
+ .map(Some)
+ .ok_or_else(|| anyhow!("Failed to create image buffer"))
+}
+
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+fn convert_ffmpeg_frame_to_image(frame: &ffmpeg::frame::Video) -> anyhow::Result<RgbImage> {
+ #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
+ if let Some(image) = try_convert_bgrx_screenshot_ssse3(frame)? {
+ return Ok(image);
+ }
+ convert_ffmpeg_frame_via_swscale(frame)
+}
+
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+fn convert_ffmpeg_frame_via_swscale(frame: &ffmpeg::frame::Video) -> anyhow::Result<RgbImage> {
+ let width = frame.width() as usize;
+ let height = frame.height() as usize;
+ let row_bytes = width
+ .checked_mul(3)
+ .context("screenshot dimensions overflow")?;
+ let image_bytes = height
+ .checked_mul(row_bytes)
+ .context("screenshot dimensions overflow")?;
+ if width == 0 || height == 0 {
+ return RgbImage::from_raw(frame.width(), frame.height(), Vec::new())
+ .ok_or_else(|| anyhow!("Failed to create image buffer"));
+ }
+
+ if row_bytes % SCREENSHOT_RGB_STRIDE_ALIGN == 0
+ && let Some(image) = scale_screenshot_into_tight_rgb(frame, row_bytes, image_bytes)?
+ {
+ return Ok(image);
+ }
+
+ let mut rgb_frame = ffmpeg::frame::Video::empty();
+ with_screenshot_scaler(frame, |scaler| scaler.run(frame, &mut rgb_frame))?
+ .context("Failed to scale frame")?;
+ pack_rgb_frame(&rgb_frame)
+}
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+fn scale_screenshot_into_tight_rgb(
+ frame: &ffmpeg::frame::Video,
+ row_bytes: usize,
+ image_bytes: usize,
+) -> anyhow::Result<Option<RgbImage>> {
+ let mut image = vec![0u8; image_bytes];
let mut rgb_frame = ffmpeg::frame::Video::empty();
- scaler
- .run(frame, &mut rgb_frame)
+ rgb_frame.set_format(ffmpeg::format::Pixel::RGB24);
+ rgb_frame.set_width(frame.width());
+ rgb_frame.set_height(frame.height());
+ let linesize = libc::c_int::try_from(row_bytes).context("screenshot stride exceeds c_int")?;
+ // SAFETY: plane 0 aliases `image` and the frame does not own it (`buf[0]` stays
+ // null, so av_frame_free will not release it). `image` outlives `rgb_frame`.
+ // The stride equals width*3 and is a multiple of Video::alloc's 32-byte
+ // alignment, so this is the same layout swscale uses for its own buffer.
+ unsafe {
+ let raw = rgb_frame.as_mut_ptr();
+ (*raw).data[0] = image.as_mut_ptr();
+ (*raw).linesize[0] = linesize;
+ }
+
+ with_screenshot_scaler(frame, |scaler| scaler.run(frame, &mut rgb_frame))?
.context("Failed to scale frame")?;
+ let stride = rgb_frame.stride(0);
+ let same_buffer = std::ptr::eq(rgb_frame.data(0).as_ptr(), image.as_ptr());
+ drop(rgb_frame);
+ if stride != row_bytes || !same_buffer {
+ return Ok(None);
+ }
+
+ RgbImage::from_raw(frame.width(), frame.height(), image)
+ .map(Some)
+ .ok_or_else(|| anyhow!("Failed to create image buffer"))
+}
+
+#[cfg(any(target_os = "macos", target_os = "linux"))]
+fn pack_rgb_frame(rgb_frame: &ffmpeg::frame::Video) -> anyhow::Result<RgbImage> {
let width = rgb_frame.width() as usize;
let height = rgb_frame.height() as usize;
- let dst_stride = width * 3;
+ let dst_stride = width
+ .checked_mul(3)
+ .context("screenshot dimensions overflow")?;
let src_stride = rgb_frame.stride(0);
if src_stride < dst_stride {
return Err(anyhow!(
- "Source stride ({}) is less than destination stride ({}); width={}, height={}",
- src_stride,
- dst_stride,
- width,
- height
+ "Source stride ({src_stride}) is less than destination stride ({dst_stride}); width={width}, height={height}"
));
}
- let mut img_buffer = vec![0u8; height * dst_stride];
-
- for y in 0..height {
- let src_slice = &rgb_frame.data(0)[y * src_stride..y * src_stride + dst_stride];
- let dst_slice = &mut img_buffer[y * dst_stride..(y + 1) * dst_stride];
- dst_slice.copy_from_slice(src_slice);
+ let buf_len = height
+ .checked_mul(dst_stride)
+ .context("screenshot dimensions overflow")?;
+ let mut img_buffer = vec![0u8; buf_len];
+ let src = rgb_frame.data(0);
+ if src_stride == dst_stride {
+ let copy_len = height
+ .checked_mul(src_stride)
+ .context("screenshot dimensions overflow")?;
+ let src = src
+ .get(..copy_len)
+ .ok_or_else(|| anyhow!("Failed to create image buffer"))?;
+ img_buffer.copy_from_slice(src);
+ } else {
+ for y in 0..height {
+ let start = y * src_stride;
+ let end = start + dst_stride;
+ let dst_start = y * dst_stride;
+ let src_row = src
+ .get(start..end)
+ .ok_or_else(|| anyhow!("Failed to create image buffer"))?;
+ img_buffer[dst_start..dst_start + dst_stride].copy_from_slice(src_row);
+ }
}
- RgbImage::from_raw(width as u32, height as u32, img_buffer)
+ RgbImage::from_raw(rgb_frame.width(), rgb_frame.height(), img_buffer)
.ok_or_else(|| anyhow!("Failed to create image buffer"))
}
@@ -2423,3 +2761,458 @@ mod screenshot_pixel_tests {
assert_bytes_eq(got.as_raw(), expected.as_raw(), "camera only");
}
}
+
+#[cfg(all(test, any(target_os = "macos", target_os = "linux")))]
+mod screenshot_swscale_cache_tests {
+ use super::{
+ clear_screenshot_scaler_cache, convert_ffmpeg_frame_to_image,
+ convert_ffmpeg_frame_via_swscale, screenshot_scaler_builds,
+ };
+ use anyhow::Context;
+ use image::RgbImage;
+ use std::time::{Duration, Instant};
+
+ fn filled_bgrx(format: ffmpeg::format::Pixel, width: u32, height: u32) -> ffmpeg::frame::Video {
+ let mut frame = ffmpeg::frame::Video::new(format, width, height);
+ let stride = frame.stride(0);
+ let data = frame.data_mut(0);
+ for y in 0..height as usize {
+ for x in 0..width as usize {
+ let i = y * stride + x * 4;
+ data[i] = (x.wrapping_mul(3).wrapping_add(y)) as u8;
+ data[i + 1] = (x.wrapping_mul(5).wrapping_add(7)) as u8;
+ data[i + 2] = (x.wrapping_mul(11).wrapping_add(y.wrapping_mul(2))) as u8;
+ data[i + 3] = 0xA5;
+ }
+ }
+ frame
+ }
+
+ fn shift_bgrx_pixels(frame: &mut ffmpeg::frame::Video, bias: u8) {
+ let width = frame.width() as usize;
+ let height = frame.height() as usize;
+ let stride = frame.stride(0);
+ let data = frame.data_mut(0);
+ for row in data.chunks_mut(stride).take(height) {
+ for pixel in row.chunks_mut(4).take(width) {
+ pixel[0] = pixel[0].wrapping_add(bias);
+ pixel[1] = pixel[1].wrapping_add(bias);
+ pixel[2] = pixel[2].wrapping_add(bias);
+ }
+ }
+ }
+
+ fn expected_bgrx_rgb(frame: &ffmpeg::frame::Video) -> Vec<u8> {
+ let width = frame.width() as usize;
+ let height = frame.height() as usize;
+ let stride = frame.stride(0);
+ let src = frame.data(0);
+ let mut rgb = vec![0u8; width * height * 3];
+ for y in 0..height {
+ for x in 0..width {
+ let s = y * stride + x * 4;
+ let d = (y * width + x) * 3;
+ rgb[d] = src[s + 2];
+ rgb[d + 1] = src[s + 1];
+ rgb[d + 2] = src[s];
+ }
+ }
+ rgb
+ }
+
+ fn bgrx_ssse3_screenshot() -> bool {
+ #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
+ {
+ std::is_x86_feature_detected!("ssse3")
+ }
+ #[cfg(not(all(target_os = "linux", target_arch = "x86_64")))]
+ {
+ false
+ }
+ }
+
+ fn assert_scaler_builds(when_swscale: u64) {
+ let builds = screenshot_scaler_builds();
+ if bgrx_ssse3_screenshot() {
+ assert_eq!(builds, 0);
+ } else {
+ assert_eq!(builds, when_swscale);
+ }
+ }
+
+ /// The pre-cache conversion: a new swscale context and a row pack every call.
+ fn uncached_packed_rgb(frame: &ffmpeg::frame::Video) -> anyhow::Result<RgbImage> {
+ let mut scaler = ffmpeg::software::scaling::context::Context::get(
+ frame.format(),
+ frame.width(),
+ frame.height(),
+ ffmpeg::format::Pixel::RGB24,
+ frame.width(),
+ frame.height(),
+ ffmpeg::software::scaling::flag::Flags::BILINEAR,
+ )
+ .context("Failed to create scaler")?;
+ let mut rgb_frame = ffmpeg::frame::Video::empty();
+ scaler
+ .run(frame, &mut rgb_frame)
+ .context("Failed to scale frame")?;
+ let width = rgb_frame.width() as usize;
+ let height = rgb_frame.height() as usize;
+ let dst_stride = width * 3;
+ let src_stride = rgb_frame.stride(0);
+ anyhow::ensure!(
+ src_stride >= dst_stride,
+ "Source stride ({src_stride}) is less than destination stride ({dst_stride})"
+ );
+ let mut img_buffer = vec![0u8; height * dst_stride];
+ for y in 0..height {
+ let src_slice = &rgb_frame.data(0)[y * src_stride..y * src_stride + dst_stride];
+ img_buffer[y * dst_stride..(y + 1) * dst_stride].copy_from_slice(src_slice);
+ }
+ RgbImage::from_raw(rgb_frame.width(), rgb_frame.height(), img_buffer)
+ .ok_or_else(|| anyhow::anyhow!("Failed to create image buffer"))
+ }
+
+ #[test]
+ fn bgrx_screenshot_matches_channel_swap() {
+ ffmpeg::init().expect("ffmpeg");
+ clear_screenshot_scaler_cache();
+
+ let aligned = filled_bgrx(ffmpeg::format::Pixel::BGRZ, 3840, 2);
+ let image = convert_ffmpeg_frame_to_image(&aligned).expect("aligned convert");
+ assert_eq!(image.as_raw(), expected_bgrx_rgb(&aligned).as_slice());
+ assert_eq!(
+ image.as_raw(),
+ uncached_packed_rgb(&aligned)
+ .expect("aligned reference")
+ .as_raw()
+ );
+ assert_scaler_builds(1);
+ let again = convert_ffmpeg_frame_to_image(&aligned).expect("aligned reuse");
+ assert_eq!(again.as_raw(), image.as_raw());
+ assert_scaler_builds(1);
+
+ let mut aligned_changed = filled_bgrx(ffmpeg::format::Pixel::BGRZ, 3840, 2);
+ shift_bgrx_pixels(&mut aligned_changed, 17);
+ let aligned_changed_image =
+ convert_ffmpeg_frame_to_image(&aligned_changed).expect("aligned pixel change");
+ assert_eq!(
+ aligned_changed_image.as_raw(),
+ uncached_packed_rgb(&aligned_changed)
+ .expect("aligned pixel change reference")
+ .as_raw()
+ );
+ assert_ne!(aligned_changed_image.as_raw(), image.as_raw());
+ assert_scaler_builds(1);
+
+ let padded = filled_bgrx(ffmpeg::format::Pixel::BGRZ, 17, 3);
+ let padded_image = convert_ffmpeg_frame_to_image(&padded).expect("padded convert");
+ assert_eq!(padded_image.as_raw(), expected_bgrx_rgb(&padded).as_slice());
+ assert_eq!(
+ padded_image.as_raw(),
+ uncached_packed_rgb(&padded)
+ .expect("padded reference")
+ .as_raw()
+ );
+ assert_scaler_builds(2);
+ convert_ffmpeg_frame_to_image(&padded).expect("padded reuse");
+ assert_scaler_builds(2);
+
+ let mut padded_changed = filled_bgrx(ffmpeg::format::Pixel::BGRZ, 17, 3);
+ shift_bgrx_pixels(&mut padded_changed, 17);
+ let padded_changed_image =
+ convert_ffmpeg_frame_to_image(&padded_changed).expect("padded pixel change");
+ assert_eq!(
+ padded_changed_image.as_raw(),
+ uncached_packed_rgb(&padded_changed)
+ .expect("padded pixel change reference")
+ .as_raw()
+ );
+ assert_ne!(padded_changed_image.as_raw(), padded_image.as_raw());
+ assert_scaler_builds(2);
+
+ let bgra = filled_bgrx(ffmpeg::format::Pixel::BGRA, 32, 2);
+ let bgra_image = convert_ffmpeg_frame_to_image(&bgra).expect("bgra convert");
+ assert_eq!(bgra_image.as_raw(), expected_bgrx_rgb(&bgra).as_slice());
+ assert_scaler_builds(3);
+
+ convert_ffmpeg_frame_to_image(&aligned).expect("return to aligned");
+ assert_scaler_builds(4);
+
+ let tiny = filled_bgrx(ffmpeg::format::Pixel::BGRZ, 1, 1);
+ let tiny_image = convert_ffmpeg_frame_to_image(&tiny).expect("tiny convert");
+ assert_eq!(
+ tiny_image.as_raw(),
+ &[tiny.data(0)[2], tiny.data(0)[1], tiny.data(0)[0]]
+ );
+ }
+
+ #[test]
+ fn rgbz_screenshot_keeps_byte_order_and_uses_swscale() {
+ ffmpeg::init().expect("ffmpeg");
+ clear_screenshot_scaler_cache();
+ let frame = filled_bgrx(ffmpeg::format::Pixel::RGBZ, 32, 2);
+ let image = convert_ffmpeg_frame_to_image(&frame).expect("rgbz convert");
+ assert_eq!(screenshot_scaler_builds(), 1);
+ let src = frame.data(0);
+ assert_eq!(image.as_raw()[..3], [src[0], src[1], src[2]]);
+ assert_eq!(
+ image.as_raw(),
+ uncached_packed_rgb(&frame)
+ .expect("rgbz reference")
+ .as_raw()
+ );
+ convert_ffmpeg_frame_to_image(&frame).expect("rgbz reuse");
+ assert_eq!(screenshot_scaler_builds(), 1);
+ }
+
+ #[test]
+ fn bgrx_fast_path_leaves_swscale_cache_intact() {
+ ffmpeg::init().expect("ffmpeg");
+ clear_screenshot_scaler_cache();
+ let rgba = filled_bgrx(ffmpeg::format::Pixel::RGBA, 64, 2);
+ let first = convert_ffmpeg_frame_to_image(&rgba).expect("rgba convert");
+ assert_eq!(screenshot_scaler_builds(), 1);
+ assert_eq!(
+ first.as_raw(),
+ uncached_packed_rgb(&rgba).expect("rgba reference").as_raw()
+ );
+
+ let bgrz = filled_bgrx(ffmpeg::format::Pixel::BGRZ, 64, 2);
+ let bgrz_image = convert_ffmpeg_frame_to_image(&bgrz).expect("bgrz convert");
+ assert_eq!(bgrz_image.as_raw(), expected_bgrx_rgb(&bgrz).as_slice());
+ if bgrx_ssse3_screenshot() {
+ assert_eq!(screenshot_scaler_builds(), 1);
+ } else {
+ assert_eq!(screenshot_scaler_builds(), 2);
+ }
+
+ let again = convert_ffmpeg_frame_to_image(&rgba).expect("rgba reuse");
+ assert_eq!(again.as_raw(), first.as_raw());
+ if bgrx_ssse3_screenshot() {
+ assert_eq!(screenshot_scaler_builds(), 1);
+ } else {
+ assert_eq!(screenshot_scaler_builds(), 3);
+ }
+ }
+
+ fn thread_cpu_time() -> Duration {
+ let mut ts = libc::timespec {
+ tv_sec: 0,
+ tv_nsec: 0,
+ };
+ let rc = unsafe { libc::clock_gettime(libc::CLOCK_THREAD_CPUTIME_ID, &mut ts) };
+ assert_eq!(rc, 0);
+ Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32)
+ }
+
+ fn summarize(samples: &mut [Duration]) -> (Duration, Duration, Duration) {
+ samples.sort_unstable();
+ let min = samples[0];
+ let sum = samples.iter().copied().sum::<Duration>();
+ let mean = sum / u32::try_from(samples.len()).expect("sample count");
+ let mid = samples.len() / 2;
+ let median = if samples.len().is_multiple_of(2) {
+ (samples[mid - 1] + samples[mid]) / 2
+ } else {
+ samples[mid]
+ };
+ (min, median, mean)
+ }
+
+ fn time_convert(
+ frame: &ffmpeg::frame::Video,
+ runs: usize,
+ convert: impl Fn(&ffmpeg::frame::Video) -> RgbImage,
+ ) -> (Vec<Duration>, Vec<Duration>) {
+ let mut wall = Vec::with_capacity(runs);
+ let mut cpu = Vec::with_capacity(runs);
+ for _ in 0..runs {
+ let cpu_start = thread_cpu_time();
+ let start = Instant::now();
+ let image = convert(std::hint::black_box(frame));
+ std::hint::black_box(&image);
+ let elapsed = start.elapsed();
+ let cpu_elapsed = thread_cpu_time().saturating_sub(cpu_start);
+ wall.push(elapsed);
+ cpu.push(cpu_elapsed);
+ }
+ (wall, cpu)
+ }
+
+ fn host_model() -> String {
+ let Ok(cpuinfo) = std::fs::read_to_string("/proc/cpuinfo") else {
+ return "unknown".to_string();
+ };
+ cpuinfo
+ .lines()
+ .find_map(|line| {
+ let rest = line.strip_prefix("model name")?;
+ Some(rest.trim_start_matches(['\t', ':', ' ']).to_string())
+ })
+ .unwrap_or_else(|| "unknown".to_string())
+ }
+
+ #[test]
+ #[ignore = "manual 4K BGRZ screenshot conversion benchmark"]
+ fn bench_bgrz_screenshot_conversion() {
+ ffmpeg::init().expect("ffmpeg");
+ const WARMUP: usize = 10;
+ const RUNS: usize = 51;
+ println!(
+ "host={} ssse3_path={} warmup={WARMUP} runs={RUNS} profile=release opt-level=s lto=thin codegen-units=1",
+ host_model(),
+ bgrx_ssse3_screenshot()
+ );
+ for (width, height) in [(3840, 2160), (1920, 1080), (1366, 768)] {
+ let frame = filled_bgrx(ffmpeg::format::Pixel::BGRZ, width, height);
+ let scalar = expected_bgrx_rgb(&frame);
+ let uncached = uncached_packed_rgb(&frame).expect("uncached");
+ clear_screenshot_scaler_cache();
+ let swscale = convert_ffmpeg_frame_via_swscale(&frame).expect("swscale");
+ let screenshot = convert_ffmpeg_frame_to_image(&frame).expect("screenshot");
+ assert_eq!(scalar.as_slice(), swscale.as_raw());
+ assert_eq!(swscale.as_raw(), screenshot.as_raw());
+ assert_eq!(uncached.as_raw(), screenshot.as_raw());
+
+ for _ in 0..WARMUP {
+ std::hint::black_box(uncached_packed_rgb(&frame).expect("warmup uncached"));
+ }
+ let (mut uncached_wall, mut uncached_cpu) = time_convert(&frame, RUNS, |frame| {
+ uncached_packed_rgb(frame).expect("uncached")
+ });
+
+ clear_screenshot_scaler_cache();
+ for _ in 0..WARMUP {
+ std::hint::black_box(
+ convert_ffmpeg_frame_via_swscale(&frame).expect("warmup swscale"),
+ );
+ }
+ let (mut swscale_wall, mut swscale_cpu) = time_convert(&frame, RUNS, |frame| {
+ convert_ffmpeg_frame_via_swscale(frame).expect("swscale")
+ });
+
+ for _ in 0..WARMUP {
+ std::hint::black_box(
+ convert_ffmpeg_frame_to_image(&frame).expect("warmup screenshot"),
+ );
+ }
+ let builds_before = screenshot_scaler_builds();
+ let (mut screenshot_wall, mut screenshot_cpu) = time_convert(&frame, RUNS, |frame| {
+ convert_ffmpeg_frame_to_image(frame).expect("screenshot")
+ });
+ let builds_during = screenshot_scaler_builds() - builds_before;
+
+ let (uncached_wall_min, uncached_wall_med, uncached_wall_mean) =
+ summarize(&mut uncached_wall);
+ let (uncached_cpu_min, uncached_cpu_med, uncached_cpu_mean) =
+ summarize(&mut uncached_cpu);
+ let (swscale_wall_min, swscale_wall_med, swscale_wall_mean) =
+ summarize(&mut swscale_wall);
+ let (swscale_cpu_min, swscale_cpu_med, swscale_cpu_mean) = summarize(&mut swscale_cpu);
+ let (screenshot_wall_min, screenshot_wall_med, screenshot_wall_mean) =
+ summarize(&mut screenshot_wall);
+ let (screenshot_cpu_min, screenshot_cpu_med, screenshot_cpu_mean) =
+ summarize(&mut screenshot_cpu);
+ println!(
+ "size={width}x{height} screenshot_scaler_builds_during_timed_runs={builds_during}"
+ );
+ println!(
+ "swscale_uncached wall min={uncached_wall_min:?} median={uncached_wall_med:?} mean={uncached_wall_mean:?} cpu min={uncached_cpu_min:?} median={uncached_cpu_med:?} mean={uncached_cpu_mean:?}"
+ );
+ println!(
+ "swscale_cached wall min={swscale_wall_min:?} median={swscale_wall_med:?} mean={swscale_wall_mean:?} cpu min={swscale_cpu_min:?} median={swscale_cpu_med:?} mean={swscale_cpu_mean:?}"
+ );
+ println!(
+ "screenshot wall min={screenshot_wall_min:?} median={screenshot_wall_med:?} mean={screenshot_wall_mean:?} cpu min={screenshot_cpu_min:?} median={screenshot_cpu_med:?} mean={screenshot_cpu_mean:?}"
+ );
+ }
+ }
+}
+
+#[cfg(all(test, target_os = "linux", target_arch = "x86_64"))]
+mod bgrx_ssse3_tests {
+ use super::bgrx_plane_to_rgb24;
+
+ fn scalar_bgrx(src: &[u8], stride: usize, width: usize, height: usize) -> Vec<u8> {
+ let mut rgb = vec![0u8; width * height * 3];
+ for (src_row, dst_row) in src
+ .chunks(stride)
+ .zip(rgb.chunks_mut(width * 3))
+ .take(height)
+ {
+ for (src_px, dst_px) in src_row
+ .chunks_exact(4)
+ .take(width)
+ .zip(dst_row.chunks_exact_mut(3))
+ {
+ dst_px[0] = src_px[2];
+ dst_px[1] = src_px[1];
+ dst_px[2] = src_px[0];
+ }
+ }
+ rgb
+ }
+
+ fn fill_bgrx(width: usize, height: usize, stride: usize) -> Vec<u8> {
+ let mut src = vec![0x5Au8; stride * height];
+ for y in 0..height {
+ for x in 0..width {
+ let i = y * stride + x * 4;
+ src[i] = x.wrapping_mul(3).wrapping_add(y) as u8;
+ src[i + 1] = x.wrapping_mul(5).wrapping_add(7) as u8;
+ src[i + 2] = x.wrapping_mul(11).wrapping_add(y.wrapping_mul(2)) as u8;
+ src[i + 3] = 0xA5;
+ }
+ }
+ src
+ }
+
+ #[test]
+ fn rejects_empty_and_short_planes() {
+ assert!(bgrx_plane_to_rgb24(&[], 0, 0, 1).is_none());
+ assert!(bgrx_plane_to_rgb24(&[0; 4], 4, 1, 0).is_none());
+ assert!(bgrx_plane_to_rgb24(&[0; 3], 3, 1, 1).is_none());
+ assert!(bgrx_plane_to_rgb24(&[0; 4], 8, 1, 2).is_none());
+ }
+
+ #[test]
+ fn matches_scalar_reference_including_tails_and_padding() {
+ if !std::is_x86_feature_detected!("ssse3") {
+ eprintln!("ssse3 unavailable, skipping bgrx plane check");
+ return;
+ }
+ let widths = [
+ 1, 2, 3, 4, 5, 7, 8, 15, 16, 17, 31, 32, 33, 47, 48, 63, 64, 127, 128, 191, 192, 1366,
+ 1919, 1920, 3839, 3840,
+ ];
+ for width in widths {
+ for height in [1usize, 2, 3] {
+ for pad in [0usize, 3, 17] {
+ if width >= 1366 && !(height == 2 && pad == 0) {
+ continue;
+ }
+ let stride = width * 4 + pad;
+ let src = fill_bgrx(width, height, stride);
+ let got = bgrx_plane_to_rgb24(&src, stride, width, height)
+ .expect("ssse3 plane conversion");
+ let expected = scalar_bgrx(&src, stride, width, height);
+ assert_eq!(got, expected, "w={width} h={height} pad={pad}");
+ }
+ }
+ }
+
+ let stride = 80;
+ let width = 16;
+ let height = 2;
+ let mut exact = fill_bgrx(width, height, stride);
+ exact.truncate((height - 1) * stride + width * 4);
+ let got = bgrx_plane_to_rgb24(&exact, stride, width, height).expect("exact tail");
+ assert_eq!(got, scalar_bgrx(&exact, stride, width, height));
+
+ let mut unaligned = vec![0x5Au8; 1 + stride * height];
+ unaligned[1..].copy_from_slice(&fill_bgrx(width, height, stride));
+ let got = bgrx_plane_to_rgb24(&unaligned[1..], stride, width, height).expect("unaligned");
+ assert_eq!(got, scalar_bgrx(&unaligned[1..], stride, width, height));
+ }
+}
diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs
index ce214d905..b685ef123 100644
--- a/crates/recording/src/sources/microphone.rs
+++ b/crates/recording/src/sources/microphone.rs
@@ -363,18 +363,13 @@ impl RequiredMicrophoneHealth {
}
}
-impl AudioSource for Microphone {
- type Config = Arc<MicrophoneFeedLock>;
-
- #[allow(clippy::manual_async_fn)]
- fn setup(
- feed_lock: Self::Config,
+impl Microphone {
+ pub(crate) fn setup_with_channels(
+ feed_lock: Arc<MicrophoneFeedLock>,
mut audio_tx: mpsc::Sender<AudioFrame>,
ctx: &mut crate::SetupCtx,
- ) -> impl Future<Output = anyhow::Result<Self>> + 'static
- where
- Self: Sized,
- {
+ target_channels: usize,
+ ) -> impl Future<Output = anyhow::Result<Self>> + 'static {
let health_tx = ctx.health_tx().clone();
let digital_silence_ms = ctx.digital_silence_ms();
#[cfg(windows)]
@@ -409,10 +404,9 @@ impl AudioSource for Microphone {
let setup = async move {
let source_info = feed_lock.audio_info();
let audio_info = source_info
- .with_max_channels(MICROPHONE_TARGET_CHANNELS)
.with_sample_rate(AudioMixer::INFO.sample_rate)
.with_sample_format(AudioMixer::INFO.sample_format)
- .with_channels(MICROPHONE_TARGET_CHANNELS as usize);
+ .with_channels(target_channels);
let is_wireless = source_info.is_wireless_transport;
let device_name = feed_lock.device_name().to_string();
let reconnect_settings = microphone::MicrophoneDeviceSettings {
@@ -497,7 +491,9 @@ 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 digital_silence = DigitalSilenceDetector::new(
+ audio_info.sample_rate * audio_info.channels as u32,
+ );
let mut resampler: Option<MicResampler> = None;
let mut silence_mode = false;
let mut silence_start: Option<Instant> = None;
@@ -961,6 +957,23 @@ impl AudioSource for Microphone {
setup
}
}
+}
+
+impl AudioSource for Microphone {
+ type Config = Arc<MicrophoneFeedLock>;
+
+ fn setup(
+ feed_lock: Self::Config,
+ audio_tx: mpsc::Sender<AudioFrame>,
+ ctx: &mut crate::SetupCtx,
+ ) -> impl Future<Output = anyhow::Result<Self>> + 'static {
+ Self::setup_with_channels(
+ feed_lock,
+ audio_tx,
+ ctx,
+ usize::from(MICROPHONE_TARGET_CHANNELS),
+ )
+ }
fn audio_info(&self) -> AudioInfo {
self.info
diff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rs
index c82a28d19..7ca0ef3be 100644
--- a/crates/recording/src/sources/screen_capture/linux.rs
+++ b/crates/recording/src/sources/screen_capture/linux.rs
@@ -1,4 +1,5 @@
use super::cadence::FrameCadenceGate;
+use super::linux_dmabuf_import;
use super::linux_shm::{self, ShmSegment};
use super::*;
use crate::feeds::microphone::{self, MicrophoneFeed, MicrophoneFeedLock};
@@ -38,6 +39,9 @@ use x11rb::rust_connection::RustConnection;
#[cfg(test)]
mod x11_tests;
+#[cfg(test)]
+mod system_audio_channel_tests;
+
#[derive(Debug)]
pub struct X11Capture;
@@ -306,6 +310,9 @@ struct PipewireCaptureState {
rate_limited: Arc<AtomicU64>,
capture_clock: Instant,
cadence_gate: FrameCadenceGate,
+ /// Reads frames with a GPU-tiled modifier; `None` without a usable EGL.
+ importer: Option<linux_dmabuf_import::DmaBufImporter>,
+ imported: Vec<u8>,
}
impl PipewireCaptureState {
@@ -480,7 +487,43 @@ fn capture_wayland(
stop_token: CancellationToken,
health_tx: output_pipeline::HealthSender,
) -> anyhow::Result<()> {
- let _portal_session = input.portal_session;
+ let WaylandInputConfig {
+ fd,
+ node_id,
+ fps,
+ crop_bounds,
+ portal_session: _portal_session,
+ } = input;
+ capture_pipewire_node(
+ video_info,
+ PipewireNodeInput {
+ fd,
+ node_id,
+ fps,
+ crop_bounds,
+ },
+ video_tx,
+ stop_token,
+ health_tx,
+ )
+}
+
+/// A PipeWire video node to capture: the portal's remote and node, or in
+/// tests any producer on a PipeWire daemon.
+struct PipewireNodeInput {
+ fd: OwnedFd,
+ node_id: u32,
+ fps: u32,
+ crop_bounds: Option<CropBounds>,
+}
+
+fn capture_pipewire_node(
+ video_info: VideoInfo,
+ input: PipewireNodeInput,
+ video_tx: mpsc::Sender<FFmpegVideoFrame>,
+ stop_token: CancellationToken,
+ health_tx: output_pipeline::HealthSender,
+) -> anyhow::Result<()> {
let stop_requested = Arc::new(AtomicBool::new(false));
let fatal_error = Arc::new(parking_lot::Mutex::new(None));
let sent = Arc::new(AtomicU64::new(0));
@@ -496,6 +539,18 @@ fn capture_wayland(
.connect_fd(input.fd, None)
.context("connect to PipeWire remote")?;
+ let mut importer = match linux_dmabuf_import::DmaBufImporter::new() {
+ Ok(importer) => Some(importer),
+ Err(error) => {
+ tracing::info!(%error, "no GPU import for tiled DMA-BUF screencasts");
+ None
+ }
+ };
+ let imported_params = importer
+ .as_mut()
+ .map(|importer| pipewire_imported_format_params(input.fps, importer))
+ .unwrap_or_default();
+
let state = PipewireCaptureState {
format: Default::default(),
scaler: None,
@@ -510,6 +565,8 @@ fn capture_wayland(
rate_limited: rate_limited.clone(),
capture_clock: started,
cadence_gate: FrameCadenceGate::new(1_000_000_000 / i64::from(input.fps.max(1))),
+ importer,
+ imported: Vec::new(),
};
let stream = pw::stream::StreamBox::new(
@@ -530,9 +587,17 @@ fn capture_wayland(
state.set_fatal_error(format!("PipeWire screen capture stream failed: {error}"));
}
})
- .param_changed(|_, state, id, param| {
- if let Err(error) = update_pipewire_format(state, id, param) {
- state.set_fatal_error(error.to_string());
+ .param_changed(|stream, state, id, param| {
+ match update_pipewire_format(state, id, param) {
+ Ok(PipewireFormatChange::DmaBuf) => {
+ // A compositor that only shares DMA-BUFs (niri) still has to
+ // be told the consumer takes them.
+ if let Err(error) = request_dmabuf_buffers(stream) {
+ state.set_fatal_error(error.to_string());
+ }
+ }
+ Ok(PipewireFormatChange::None | PipewireFormatChange::Memory) => {}
+ Err(error) => state.set_fatal_error(error.to_string()),
}
})
.process(|stream, state| {
@@ -557,9 +622,23 @@ fn capture_wayland(
.register()
.context("register PipeWire stream listener")?;
- let param_bytes = pipewire_format_param(input.fps)?;
- let mut params = [spa::pod::Pod::from_bytes(&param_bytes)
- .ok_or_else(|| anyhow!("create PipeWire format parameter"))?];
+ // Shared memory first, so compositors that offer it (GNOME, KDE, wlroots)
+ // keep negotiating it; LINEAR DMA-BUFs for those that share nothing else.
+ // Shared memory, then LINEAR DMA-BUFs, both read on the CPU; then the
+ // tiled modifiers a GPU can import, for compositors that cannot render
+ // LINEAR. PipeWire takes the first offer the producer can meet.
+ let mut param_bytes = vec![
+ pipewire_format_param(input.fps)?,
+ pipewire_dmabuf_format_param(input.fps)?,
+ ];
+ param_bytes.extend(imported_params);
+ let mut params = param_bytes
+ .iter()
+ .map(|bytes| {
+ spa::pod::Pod::from_bytes(bytes)
+ .ok_or_else(|| anyhow!("create PipeWire format parameter"))
+ })
+ .collect::<anyhow::Result<Vec<_>>>()?;
stream
.connect(
@@ -598,16 +677,23 @@ fn capture_wayland(
}
}
+#[derive(Debug, PartialEq, Eq)]
+enum PipewireFormatChange {
+ None,
+ Memory,
+ DmaBuf,
+}
+
fn update_pipewire_format(
state: &mut PipewireCaptureState,
id: u32,
param: Option<&spa::pod::Pod>,
-) -> anyhow::Result<()> {
+) -> anyhow::Result<PipewireFormatChange> {
let Some(param) = param else {
- return Ok(());
+ return Ok(PipewireFormatChange::None);
};
if id != spa::param::ParamType::Format.as_raw() {
- return Ok(());
+ return Ok(PipewireFormatChange::None);
}
let (media_type, media_subtype) =
@@ -615,7 +701,7 @@ fn update_pipewire_format(
if media_type != spa::param::format::MediaType::Video
|| media_subtype != spa::param::format::MediaSubtype::Raw
{
- return Ok(());
+ return Ok(PipewireFormatChange::None);
}
let mut format = spa::param::video::VideoInfoRaw::default();
@@ -630,7 +716,141 @@ fn update_pipewire_format(
})?;
state.format = format;
- Ok(())
+ let dmabuf = param.as_object().is_ok_and(|object| {
+ object
+ .find_prop(spa::utils::Id(
+ spa::param::format::FormatProperties::VideoModifier.0,
+ ))
+ .is_some()
+ });
+ if dmabuf {
+ tracing::info!(
+ modifier = format.modifier(),
+ format = ?format.format(),
+ "PipeWire screen capture negotiated DMA-BUF frames"
+ );
+ }
+ Ok(if dmabuf {
+ PipewireFormatChange::DmaBuf
+ } else {
+ PipewireFormatChange::Memory
+ })
+}
+
+fn request_dmabuf_buffers(stream: &pw::stream::Stream) -> anyhow::Result<()> {
+ let dmabuf = 1 << spa::buffer::DataType::DmaBuf.as_raw();
+ let object = spa::pod::object!(
+ spa::utils::SpaTypes::ObjectParamBuffers,
+ spa::param::ParamType::Buffers,
+ spa::pod::Property::new(
+ spa::sys::SPA_PARAM_BUFFERS_dataType,
+ spa::pod::Value::Choice(spa::pod::ChoiceValue::Int(spa::utils::Choice(
+ spa::utils::ChoiceFlags::empty(),
+ spa::utils::ChoiceEnum::Flags {
+ default: dmabuf,
+ flags: vec![dmabuf],
+ },
+ ))),
+ )
+ );
+ let bytes = serialize_pod(object, "PipeWire buffers parameter")?;
+ let pod = spa::pod::Pod::from_bytes(&bytes)
+ .ok_or_else(|| anyhow!("create PipeWire buffers parameter"))?;
+ stream
+ .update_params(&mut [pod])
+ .context("ask PipeWire for DMA-BUF screen capture buffers")
+}
+
+/// A DMA-BUF the producer did not map, mapped for reading one frame. LINEAR
+/// buffers are plain rows a CPU can read once the kernel syncs the buffer.
+struct DmaBufMapping {
+ fd: std::os::fd::RawFd,
+ pointer: *mut libc::c_void,
+ len: usize,
+}
+
+const DMA_BUF_IOCTL_SYNC: libc::c_ulong = 0x4008_6200;
+const DMA_BUF_SYNC_READ: u64 = 1;
+const DMA_BUF_SYNC_END: u64 = 1 << 2;
+
+impl DmaBufMapping {
+ fn map(fd: std::os::fd::RawFd, minimum_len: usize) -> anyhow::Result<Self> {
+ if fd < 0 {
+ bail!("PipeWire screen capture DMA-BUF had no file descriptor");
+ }
+ let size = unsafe { libc::lseek(fd, 0, libc::SEEK_END) };
+ let len = usize::try_from(size)
+ .ok()
+ .filter(|len| *len > 0)
+ .unwrap_or(minimum_len);
+ if len < minimum_len {
+ bail!(
+ "PipeWire screen capture DMA-BUF holds {len} bytes, the frame needs {minimum_len}"
+ );
+ }
+ let pointer = unsafe {
+ libc::mmap(
+ std::ptr::null_mut(),
+ len,
+ libc::PROT_READ,
+ libc::MAP_SHARED,
+ fd,
+ 0,
+ )
+ };
+ if pointer == libc::MAP_FAILED {
+ return Err(std::io::Error::last_os_error())
+ .context("map the PipeWire screen capture DMA-BUF");
+ }
+ let mapping = Self { fd, pointer, len };
+ mapping
+ .sync(DMA_BUF_SYNC_READ)
+ .context("sync the PipeWire screen capture DMA-BUF for reading")?;
+ Ok(mapping)
+ }
+
+ /// The kernel's DMA-BUF read fence. A buffer that is not a DMA-BUF (a
+ /// memfd standing in for one) has none and answers ENOTTY.
+ fn sync(&self, flags: u64) -> std::io::Result<()> {
+ let sync = flags;
+ loop {
+ let result = unsafe { libc::ioctl(self.fd, DMA_BUF_IOCTL_SYNC, &sync as *const u64) };
+ if result == 0 {
+ return Ok(());
+ }
+ let error = std::io::Error::last_os_error();
+ match error.raw_os_error() {
+ Some(libc::EINTR | libc::EAGAIN) => continue,
+ Some(libc::ENOTTY) => return Ok(()),
+ _ => return Err(error),
+ }
+ }
+ }
+
+ fn bytes(&self) -> &[u8] {
+ unsafe { std::slice::from_raw_parts(self.pointer as *const u8, self.len) }
+ }
+}
+
+impl Drop for DmaBufMapping {
+ fn drop(&mut self) {
+ if let Err(error) = self.sync(DMA_BUF_SYNC_READ | DMA_BUF_SYNC_END) {
+ tracing::warn!(%error, "ending the PipeWire screen capture DMA-BUF read failed");
+ }
+ unsafe {
+ libc::munmap(self.pointer, self.len);
+ }
+ }
+}
+
+fn serialize_pod(object: spa::pod::Object, what: &str) -> anyhow::Result<Vec<u8>> {
+ Ok(spa::pod::serialize::PodSerializer::serialize(
+ std::io::Cursor::new(Vec::new()),
+ &spa::pod::Value::Object(object),
+ )
+ .map_err(|error| anyhow!("serialize {what}: {error:?}"))?
+ .0
+ .into_inner())
}
fn process_pipewire_frame(
@@ -657,8 +877,25 @@ fn process_pipewire_frame(
return Ok(None);
}
- let Some(raw_frame) = frame_from_pipewire_data(&mut datas[0], state.format, state.crop_bounds)?
- else {
+ let modifier = state.format.modifier();
+ let tiled = datas[0].type_() == spa::buffer::DataType::DmaBuf
+ && modifier != linux_dmabuf_import::DRM_FORMAT_MOD_LINEAR
+ && modifier != linux_dmabuf_import::DRM_FORMAT_MOD_INVALID;
+ let raw_frame = if tiled {
+ let Some(importer) = state.importer.as_mut() else {
+ bail!("PipeWire screen capture sent a tiled DMA-BUF Cap cannot import");
+ };
+ imported_pipewire_frame(
+ &datas[0],
+ state.format,
+ state.crop_bounds,
+ importer,
+ &mut state.imported,
+ )?
+ } else {
+ frame_from_pipewire_data(&mut datas[0], state.format, state.crop_bounds)?
+ };
+ let Some(raw_frame) = raw_frame else {
return Ok(Some(StallSendOutcome::StalledAndDropped { waited_ms: 0 }));
};
let frame = prepare_pipewire_frame(raw_frame, &mut state.scaler, state.video_info)?;
@@ -740,11 +977,22 @@ fn frame_from_pipewire_data(
};
let (crop_x, crop_y, crop_width, crop_height) =
pipewire_crop(source_width, source_height, crop_bounds)?;
- let source = data
- .data()
- .ok_or_else(|| anyhow!("PipeWire screen capture buffer was not memory-mapped"))?;
let offset = chunk_offset as usize;
- let source_limit = if chunk_size > 0 {
+ let dmabuf_fd = (data.type_() == spa::buffer::DataType::DmaBuf).then(|| data.fd() as i32);
+ let mapping;
+ let source: &[u8] = match data.data() {
+ Some(source) => source,
+ None => {
+ let Some(fd) = dmabuf_fd else {
+ bail!("PipeWire screen capture buffer was not memory-mapped");
+ };
+ mapping = DmaBufMapping::map(fd, offset + source_stride * source_height)?;
+ mapping.bytes()
+ }
+ };
+ // DMA-BUF producers may leave the chunk size unset (the PipeWire DMA-BUF
+ // docs tell consumers to ignore it), so the mapping bounds the frame.
+ let source_limit = if chunk_size > 0 && dmabuf_fd.is_none() {
offset
.checked_add(chunk_size as usize)
.map(|limit| limit.min(source.len()))
@@ -785,6 +1033,61 @@ fn frame_from_pipewire_data(
Ok(Some(frame))
}
+/// A frame with a GPU-tiled modifier, read back through EGL as RGBA.
+fn imported_pipewire_frame(
+ data: &spa::buffer::Data,
+ format: spa::param::video::VideoInfoRaw,
+ crop_bounds: Option<CropBounds>,
+ importer: &mut linux_dmabuf_import::DmaBufImporter,
+ scratch: &mut Vec<u8>,
+) -> anyhow::Result<Option<ffmpeg::frame::Video>> {
+ if data
+ .chunk()
+ .flags()
+ .contains(spa::buffer::ChunkFlags::CORRUPTED)
+ {
+ return Ok(None);
+ }
+ let fourcc = linux_dmabuf_import::drm_fourcc(format.format()).ok_or_else(|| {
+ anyhow!(
+ "Unsupported tiled PipeWire screen capture format: {:?}",
+ format.format()
+ )
+ })?;
+ let size = format.size();
+ let (width, height) = (size.width as usize, size.height as usize);
+ if width == 0 || height == 0 {
+ bail!("PipeWire screen capture stream did not provide frame dimensions");
+ }
+ let stride = u32::try_from(data.chunk().stride())
+ .map_err(|_| anyhow!("PipeWire screen capture frame used a negative stride"))?;
+ scratch.resize(width * height * 4, 0);
+ importer.read_rgba(
+ data.fd(),
+ fourcc,
+ format.modifier(),
+ size.width,
+ size.height,
+ data.chunk().offset(),
+ stride,
+ scratch,
+ )?;
+ let (crop_x, crop_y, crop_width, crop_height) = pipewire_crop(width, height, crop_bounds)?;
+ let mut frame = ffmpeg::frame::Video::new(
+ ffmpeg::format::Pixel::RGBA,
+ crop_width as u32,
+ crop_height as u32,
+ );
+ let target_stride = frame.stride(0);
+ let row_bytes = crop_width * 4;
+ for y in 0..crop_height {
+ let source = ((crop_y + y) * width + crop_x) * 4;
+ frame.data_mut(0)[y * target_stride..y * target_stride + row_bytes]
+ .copy_from_slice(&scratch[source..source + row_bytes]);
+ }
+ Ok(Some(frame))
+}
+
fn pipewire_crop(
source_width: usize,
source_height: usize,
@@ -907,6 +1210,136 @@ fn pipewire_format_param(fps: u32) -> anyhow::Result<Vec<u8>> {
.into_inner())
}
+/// The offer for compositors that share only DMA-BUFs with a mandatory
+/// modifier (niri 26.04): LINEAR, so the frames can be read on the CPU.
+fn pipewire_dmabuf_format_param(fps: u32) -> anyhow::Result<Vec<u8>> {
+ use spa::param::video::VideoFormat;
+ pipewire_dmabuf_format_param_for(
+ fps,
+ &[
+ VideoFormat::BGRx,
+ VideoFormat::BGRA,
+ VideoFormat::RGBx,
+ VideoFormat::RGBA,
+ ],
+ &[linux_dmabuf_import::DRM_FORMAT_MOD_LINEAR as i64],
+ )
+}
+
+/// A DMA-BUF offer for `formats` with `modifiers`. More than one modifier
+/// lets the producer choose (DONT_FIXATE), as the PipeWire DMA-BUF protocol
+/// asks.
+fn pipewire_dmabuf_format_param_for(
+ fps: u32,
+ formats: &[spa::param::video::VideoFormat],
+ modifiers: &[i64],
+) -> anyhow::Result<Vec<u8>> {
+ let fps = fps.max(1);
+ let (Some(first_format), Some(first_modifier)) = (formats.first(), modifiers.first()) else {
+ bail!("a DMA-BUF offer needs a format and a modifier");
+ };
+ let mut modifier_flags = spa::pod::PropertyFlags::MANDATORY;
+ if modifiers.len() > 1 {
+ modifier_flags = spa::pod::PropertyFlags::from_bits_retain(
+ modifier_flags.bits() | spa::sys::SPA_POD_PROP_FLAG_DONT_FIXATE,
+ );
+ }
+ let object = spa::pod::object!(
+ spa::utils::SpaTypes::ObjectParamFormat,
+ spa::param::ParamType::EnumFormat,
+ spa::pod::property!(
+ spa::param::format::FormatProperties::MediaType,
+ Id,
+ spa::param::format::MediaType::Video
+ ),
+ spa::pod::property!(
+ spa::param::format::FormatProperties::MediaSubtype,
+ Id,
+ spa::param::format::MediaSubtype::Raw
+ ),
+ spa::pod::Property::new(
+ spa::param::format::FormatProperties::VideoFormat.as_raw(),
+ spa::pod::Value::Choice(spa::pod::ChoiceValue::Id(spa::utils::Choice(
+ spa::utils::ChoiceFlags::empty(),
+ spa::utils::ChoiceEnum::Enum {
+ default: spa::utils::Id(first_format.as_raw()),
+ alternatives: formats
+ .iter()
+ .map(|format| spa::utils::Id(format.as_raw()))
+ .collect(),
+ },
+ ))),
+ ),
+ spa::pod::Property {
+ key: spa::param::format::FormatProperties::VideoModifier.as_raw(),
+ flags: modifier_flags,
+ value: spa::pod::Value::Choice(spa::pod::ChoiceValue::Long(spa::utils::Choice(
+ spa::utils::ChoiceFlags::empty(),
+ spa::utils::ChoiceEnum::Enum {
+ default: *first_modifier,
+ alternatives: modifiers.to_vec(),
+ },
+ ))),
+ },
+ spa::pod::property!(
+ spa::param::format::FormatProperties::VideoSize,
+ Choice,
+ Range,
+ Rectangle,
+ spa::utils::Rectangle {
+ width: 1920,
+ height: 1080
+ },
+ spa::utils::Rectangle {
+ width: 1,
+ height: 1
+ },
+ spa::utils::Rectangle {
+ width: 8192,
+ height: 8192
+ }
+ ),
+ spa::pod::property!(
+ spa::param::format::FormatProperties::VideoFramerate,
+ Choice,
+ Range,
+ Fraction,
+ spa::utils::Fraction { num: fps, denom: 1 },
+ spa::utils::Fraction { num: 0, denom: 1 },
+ spa::utils::Fraction {
+ num: 1000,
+ denom: 1
+ }
+ )
+ );
+ serialize_pod(object, "PipeWire DMA-BUF format parameter")
+}
+
+/// Offers for the GPU-tiled modifiers this machine can import, one per
+/// format: what a compositor that cannot render LINEAR (niri on NVIDIA)
+/// shares.
+fn pipewire_imported_format_params(
+ fps: u32,
+ importer: &mut linux_dmabuf_import::DmaBufImporter,
+) -> Vec<Vec<u8>> {
+ use spa::param::video::VideoFormat;
+ [VideoFormat::BGRx, VideoFormat::BGRA]
+ .into_iter()
+ .filter_map(|format| {
+ let fourcc = linux_dmabuf_import::drm_fourcc(format)?;
+ let modifiers = importer
+ .modifiers(fourcc)
+ .into_iter()
+ .filter(|modifier| *modifier != linux_dmabuf_import::DRM_FORMAT_MOD_LINEAR)
+ .map(|modifier| modifier as i64)
+ .collect::<Vec<_>>();
+ (!modifiers.is_empty())
+ .then(|| pipewire_dmabuf_format_param_for(fps, &[format], &modifiers).ok())
+ .flatten()
+ })
+ .collect()
+}
+
pub struct SystemAudioSourceConfig {
feed_lock: Arc<MicrophoneFeedLock>,
device_name: String,
@@ -1348,7 +1781,12 @@ impl AudioSource for SystemAudioSource {
Self: Sized,
{
let device_name = config.device_name.clone();
- let setup = <crate::sources::Microphone as AudioSource>::setup(config.feed_lock, tx, ctx);
+ let setup = crate::sources::Microphone::setup_with_channels(
+ config.feed_lock,
+ tx,
+ ctx,
+ X11Capture::audio_info().channels,
+ );
async move {
let inner = setup
.await
@@ -2983,9 +3421,189 @@ mod system_audio_tests {
#[cfg(test)]
mod pipewire_frame_tests {
use super::{
- FrameScaler, LinuxCaptureSource, VideoInfo, prefers_wayland_environment,
- prepare_pipewire_frame, wayland_area_matches_display, wayland_video_info,
+ FrameScaler, LinuxCaptureSource, PipewireNodeInput, VideoInfo, capture_pipewire_node,
+ frame_from_pipewire_data, pipewire_dmabuf_format_param, pipewire_format_param,
+ prefers_wayland_environment, prepare_pipewire_frame, spa, wayland_area_matches_display,
+ wayland_video_info,
};
+ use std::os::fd::AsRawFd;
+
+ /// GitHub #2941: niri 26.04 shares screencasts only as DMA-BUFs with a
+ /// mandatory modifier, which 0.6.1 never offered, so nothing was ever
+ /// captured. This records from a producer that negotiates and shares
+ /// frames the way niri does (scripts/linux-desktop-harness/niri-screencast)
+ /// through Cap's real PipeWire capture loop.
+ #[test]
+ #[ignore = "needs a PipeWire daemon and CAP_FAKE_NIRI_PRODUCER"]
+ fn frames_from_a_producer_that_shares_only_dmabufs_are_captured() {
+ use std::io::BufRead;
+
+ let producer = std::env::var("CAP_FAKE_NIRI_PRODUCER")
+ .expect("set CAP_FAKE_NIRI_PRODUCER to the built producer");
+ let mut child = std::process::Command::new(producer)
+ .stdout(std::process::Stdio::piped())
+ .spawn()
+ .unwrap();
+ struct Kill(std::process::Child);
+ impl Drop for Kill {
+ fn drop(&mut self) {
+ let _ = self.0.kill();
+ let _ = self.0.wait();
+ }
+ }
+ let stdout = child.stdout.take().unwrap();
+ let child = Kill(child);
+ let (node_tx, node_rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ for line in std::io::BufReader::new(stdout)
+ .lines()
+ .map_while(Result::ok)
+ {
+ if let Some(node) = line.strip_prefix("node ") {
+ let _ = node_tx.send(node.trim().parse::<u32>().unwrap());
+ }
+ }
+ });
+ let node_id = node_rx
+ .recv_timeout(std::time::Duration::from_secs(10))
+ .expect("the producer announced no node");
+
+ let runtime = std::env::var("PIPEWIRE_RUNTIME_DIR")
+ .or_else(|_| std::env::var("XDG_RUNTIME_DIR"))
+ .unwrap();
+ let socket =
+ std::os::unix::net::UnixStream::connect(format!("{runtime}/pipewire-0")).unwrap();
+ let (video_tx, mut video_rx) = futures::channel::mpsc::channel(4);
+ let (health_tx, _health_rx) = tokio::sync::mpsc::channel(16);
+ let stop = tokio_util::sync::CancellationToken::new();
+ let capture = std::thread::spawn({
+ let stop = stop.clone();
+ move || {
+ capture_pipewire_node(
+ VideoInfo::from_raw_ffmpeg(ffmpeg::format::Pixel::BGRZ, 640, 360, 30),
+ PipewireNodeInput {
+ fd: socket.into(),
+ node_id,
+ fps: 30,
+ crop_bounds: None,
+ },
+ video_tx,
+ stop,
+ health_tx,
+ )
+ }
+ });
+
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(15);
+ let mut frames = Vec::new();
+ loop {
+ if frames.len() >= 10 || std::time::Instant::now() >= deadline || capture.is_finished()
+ {
+ break;
+ }
+ match video_rx.try_next() {
+ Ok(Some(frame)) => frames.push(frame.inner),
+ Ok(None) => break,
+ Err(_) => std::thread::park_timeout(std::time::Duration::from_millis(10)),
+ }
+ }
+ stop.cancel();
+ let result = capture.join().unwrap();
+ drop(child);
+ assert!(
+ frames.len() >= 10,
+ "captured {} frames; capture ended with {result:?}",
+ frames.len()
+ );
+ result.unwrap();
+ let mut levels = std::collections::BTreeSet::new();
+ for frame in &frames {
+ assert_eq!((frame.width(), frame.height()), (640, 360));
+ let pixel = &frame.data(0)[..4];
+ assert_eq!(&pixel[1..3], &[0x40, 0x80], "{pixel:?}");
+ levels.insert(pixel[0]);
+ }
+ assert!(
+ levels.len() > 1,
+ "every captured frame was the same picture"
+ );
+ }
+
+ /// niri 26.04 shares only DMA-BUFs: the buffer is not mapped for the
+ /// consumer, `maxsize` is 1 and the chunk carries just stride and offset.
+ /// 0.6.1 rejected every such frame ("buffer was not memory-mapped").
+ #[test]
+ fn an_unmapped_dmabuf_frame_is_read_from_its_file_descriptor() {
+ let (width, height, stride) = (4usize, 3usize, 24usize);
+ let mut bytes = vec![0u8; stride * height];
+ for y in 0..height {
+ for x in 0..width {
+ let pixel = &mut bytes[y * stride + x * 4..][..4];
+ pixel.copy_from_slice(&[x as u8, y as u8, 0x80, 0xff]);
+ }
+ }
+ let file = tempfile::tempfile().unwrap();
+ std::io::Write::write_all(&mut &file, &bytes).unwrap();
+
+ let mut chunk = spa::sys::spa_chunk {
+ offset: 0,
+ size: 0,
+ stride: stride as i32,
+ flags: 0,
+ };
+ let mut raw: spa::sys::spa_data = unsafe { std::mem::zeroed() };
+ raw.type_ = spa::sys::SPA_DATA_DmaBuf;
+ raw.flags = spa::sys::SPA_DATA_FLAG_READWRITE;
+ raw.fd = i64::from(file.as_raw_fd());
+ raw.maxsize = 1;
+ raw.data = std::ptr::null_mut();
+ raw.chunk = &mut chunk;
+ let data = unsafe { &mut *(&mut raw as *mut spa::sys::spa_data as *mut spa::buffer::Data) };
+
+ let mut format = spa::param::video::VideoInfoRaw::new();
+ format.set_format(spa::param::video::VideoFormat::BGRx);
+ format.set_size(spa::utils::Rectangle {
+ width: width as u32,
+ height: height as u32,
+ });
+ format.set_modifier(0);
+ let frame = frame_from_pipewire_data(data, format, None)
+ .unwrap()
+ .expect("a frame");
+ assert_eq!((frame.width(), frame.height()), (4, 3));
+ assert_eq!(frame.format(), ffmpeg::format::Pixel::BGRZ);
+ for y in 0..height {
+ let row = &frame.data(0)[y * frame.stride(0)..][..width * 4];
+ assert_eq!(row, &bytes[y * stride..][..width * 4], "row {y}");
+ }
+ }
+
+ #[test]
+ fn dmabuf_offer_follows_shared_memory_with_a_mandatory_linear_modifier() {
+ let shm = pipewire_format_param(30).unwrap();
+ let shm = spa::pod::Pod::from_bytes(&shm)
+ .unwrap()
+ .as_object()
+ .unwrap();
+ let modifier = spa::utils::Id(spa::param::format::FormatProperties::VideoModifier.0);
+ assert!(shm.find_prop(modifier).is_none());
+
+ let bytes = pipewire_dmabuf_format_param(30).unwrap();
+ let object = spa::pod::Pod::from_bytes(&bytes)
+ .unwrap()
+ .as_object()
+ .unwrap();
+ let prop = object.find_prop(modifier).expect("a modifier property");
+ assert!(prop.flags().contains(spa::pod::PodPropFlags::MANDATORY));
+ let (_, value) = spa::pod::deserialize::PodDeserializer::deserialize_from::<
+ spa::utils::Choice<i64>,
+ >(prop.value().as_bytes())
+ .unwrap();
+ let spa::utils::ChoiceEnum::Enum { alternatives, .. } = value.1 else {
+ panic!("modifier is not an enum choice");
+ };
+ assert_eq!(alternatives, vec![0]);
+ }
#[test]
fn wayland_area_requires_the_selected_monitor_in_the_portal() {
diff --git a/crates/recording/src/sources/screen_capture/linux/system_audio_channel_tests.rs b/crates/recording/src/sources/screen_capture/linux/system_audio_channel_tests.rs
new file mode 100644
index 000000000..d7bb7570d
--- /dev/null
+++ b/crates/recording/src/sources/screen_capture/linux/system_audio_channel_tests.rs
@@ -0,0 +1,88 @@
+use super::*;
+use crate::{
+ feeds::microphone::synthetic::SyntheticMicrophone,
+ output_pipeline::{AudioMuxer, Muxer, OutputPipeline, TaskPool},
+};
+use std::ops::Deref;
+
+struct CaptureMuxer {
+ info: AudioInfo,
+ frames: flume::Sender<(AudioInfo, AudioFrame)>,
+}
+
+impl Muxer for CaptureMuxer {
+ type Config = flume::Sender<(AudioInfo, AudioFrame)>;
+
+ async fn setup(
+ frames: Self::Config,
+ _: std::path::PathBuf,
+ _: Option<VideoInfo>,
+ audio: Option<AudioInfo>,
+ _: Arc<AtomicBool>,
+ _: &mut TaskPool,
+ ) -> anyhow::Result<Self> {
+ Ok(Self {
+ info: audio.expect("audio source configured"),
+ frames,
+ })
+ }
+
+ fn finish(&mut self, _: Duration) -> anyhow::Result<anyhow::Result<()>> {
+ Ok(Ok(()))
+ }
+}
+
+impl AudioMuxer for CaptureMuxer {
+ fn send_audio_frame(&mut self, frame: AudioFrame, _: Duration) -> anyhow::Result<()> {
+ self.frames.send((self.info, frame))?;
+ Ok(())
+ }
+}
+
+#[tokio::test]
+async fn linux_system_audio_preserves_stereo_channels() {
+ for rate in [44_100, 48_000, 96_000] {
+ let input = SyntheticMicrophone::spawn("stereo-monitor", rate, 2);
+ let lock = input.lock().await.unwrap();
+ let actor = lock.deref().clone();
+ let config = SystemAudioSourceConfig {
+ feed_lock: Arc::new(lock),
+ device_name: "stereo-monitor".into(),
+ monitor_route: None,
+ feed: OwnedSystemAudioFeed {
+ actor,
+ build_scope: None,
+ stopped: false,
+ },
+ };
+ let (frames, received) = flume::unbounded();
+ let temp = tempfile::tempdir().unwrap();
+ let pipeline = OutputPipeline::builder(temp.path().join("system.ogg"))
+ .with_audio_source::<SystemAudioSource>(config)
+ .build::<CaptureMuxer>(frames)
+ .await
+ .unwrap();
+ let samples: Vec<f32> = (0..rate / 100).flat_map(|_| [0.25, -0.25]).collect();
+ input
+ .push(&samples, Timestamp::Instant(Instant::now()))
+ .await;
+ let (info, frame) = tokio::time::timeout(Duration::from_secs(2), received.recv_async())
+ .await
+ .expect("system audio reached the muxer")
+ .unwrap();
+ pipeline.stop().await.unwrap();
+
+ assert_eq!(info.channels, 2, "system audio at {rate} Hz became mono");
+ assert_eq!(info.sample_rate, 48_000);
+ assert_eq!(frame.inner.channels(), 2);
+ assert!(frame.inner.samples() > 100);
+ let samples = frame.inner.samples();
+ let bytes = &frame.inner.data(0)[..samples * 2 * size_of::<f32>()];
+ for pair in bytes.chunks_exact(2 * size_of::<f32>()).skip(32) {
+ let left = f32::from_ne_bytes(pair[..4].try_into().unwrap());
+ let right = f32::from_ne_bytes(pair[4..].try_into().unwrap());
+ assert!((left - 0.25).abs() < 0.001, "left channel: {left}");
+ assert!((right + 0.25).abs() < 0.001, "right channel: {right}");
+ }
+ }
+}
diff --git a/crates/recording/src/sources/screen_capture/linux_dmabuf_import.rs b/crates/recording/src/sources/screen_capture/linux_dmabuf_import.rs
new file mode 100644
index 000000000..b2a45547f
--- /dev/null
+++ b/crates/recording/src/sources/screen_capture/linux_dmabuf_import.rs
@@ -0,0 +1,604 @@
+//! Reads DMA-BUF screencast frames whose modifier a CPU cannot read directly.
+//!
+//! A compositor that shares only DMA-BUFs allocates them on its GPU with a
+//! modifier both sides accept. On NVIDIA no LINEAR buffer can be rendered to
+//! (`eglQueryDmaBufModifiersEXT` lists LINEAR as external only), so niri
+//! offers only NVIDIA's block-linear modifiers. Those frames are imported into
+//! EGL as images, attached to a framebuffer and read back as plain RGBA.
+
+use anyhow::{Context as _, anyhow, bail};
+use glow::HasContext as _;
+use khronos_egl as egl;
+use std::ffi::c_void;
+use std::os::fd::RawFd;
+
+const EGL_PLATFORM_DEVICE_EXT: egl::Enum = 0x313F;
+const EGL_DRM_RENDER_NODE_FILE_EXT: egl::Int = 0x3377;
+const EGL_LINUX_DMA_BUF_EXT: egl::Enum = 0x3270;
+const EGL_LINUX_DRM_FOURCC_EXT: egl::Attrib = 0x3271;
+const EGL_DMA_BUF_PLANE0_FD_EXT: egl::Attrib = 0x3272;
+const EGL_DMA_BUF_PLANE0_OFFSET_EXT: egl::Attrib = 0x3273;
+const EGL_DMA_BUF_PLANE0_PITCH_EXT: egl::Attrib = 0x3274;
+const EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT: egl::Attrib = 0x3443;
+const EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT: egl::Attrib = 0x3444;
+const EGL_NO_CONTEXT: egl::EGLContext = std::ptr::null_mut();
+
+pub(super) const DRM_FORMAT_MOD_LINEAR: u64 = 0;
+pub(super) const DRM_FORMAT_MOD_INVALID: u64 = 0x00ff_ffff_ffff_ffff;
+
+type QueryDevices = unsafe extern "C" fn(egl::Int, *mut *mut c_void, *mut egl::Int) -> egl::Boolean;
+type QueryDeviceString = unsafe extern "C" fn(*mut c_void, egl::Int) -> *const std::ffi::c_char;
+type GetPlatformDisplay =
+ unsafe extern "C" fn(egl::Enum, *mut c_void, *const egl::Int) -> egl::EGLDisplay;
+type QueryDmaBufModifiers = unsafe extern "C" fn(
+ egl::EGLDisplay,
+ egl::Int,
+ egl::Int,
+ *mut u64,
+ *mut egl::Boolean,
+ *mut egl::Int,
+) -> egl::Boolean;
+type ImageTargetTexture = unsafe extern "C" fn(u32, *mut c_void);
+
+/// The DRM fourcc for a PipeWire raw video format.
+pub(super) fn drm_fourcc(format: pipewire::spa::param::video::VideoFormat) -> Option<u32> {
+ use pipewire::spa::param::video::VideoFormat;
+ let code = |s: &[u8; 4]| u32::from_le_bytes(*s);
+ if format == VideoFormat::BGRx {
+ Some(code(b"XR24"))
+ } else if format == VideoFormat::BGRA {
+ Some(code(b"AR24"))
+ } else if format == VideoFormat::RGBx {
+ Some(code(b"XB24"))
+ } else if format == VideoFormat::RGBA {
+ Some(code(b"AB24"))
+ } else {
+ None
+ }
+}
+
+/// One GPU's EGL device, with a context that can read its imports back.
+struct GpuImporter {
+ egl: egl::DynamicInstance<egl::EGL1_5>,
+ display: egl::Display,
+ context: egl::Context,
+ gl: Option<glow::Context>,
+ image_target_texture: ImageTargetTexture,
+ query_modifiers: QueryDmaBufModifiers,
+ framebuffer: Option<glow::Framebuffer>,
+ texture: Option<glow::Texture>,
+ /// The kernel driver of the GPU's render node (`amdgpu`, `nvidia`, ...).
+ driver: Option<String>,
+ /// The GPU's render node, which identifies it between recordings.
+ node: Option<String>,
+}
+
+// The EGL context is made current on whichever single thread uses the
+// importer, and released before it moves.
+unsafe impl Send for GpuImporter {}
+
+/// Every GPU that can import screencast DMA-BUFs. The compositor allocates
+/// on whichever GPU it renders with, and a hybrid laptop's first GPU is often
+/// not that one, so modifiers are offered from all of them and each frame
+/// goes to a GPU that lists its modifier.
+pub(super) struct DmaBufImporter {
+ gpus: Vec<GpuImporter>,
+ supported: Vec<std::collections::HashMap<u32, Vec<u64>>>,
+ last: usize,
+ remembered: bool,
+}
+
+/// The render node of the GPU that last read a screencast frame, kept for the
+/// rest of the session.
+static LAST_IMPORT_NODE: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
+
+fn remembered_gpu(nodes: &[Option<&str>], remembered: Option<&str>) -> usize {
+ remembered
+ .and_then(|remembered| nodes.iter().position(|node| *node == Some(remembered)))
+ .unwrap_or(0)
+}
+
+impl DmaBufImporter {
+ pub(super) fn new() -> anyhow::Result<Self> {
+ let mut gpus = Vec::new();
+ let mut last_error = None;
+ for index in 0..16 {
+ match GpuImporter::open(index) {
+ Ok(Some(gpu)) => gpus.push(gpu),
+ Ok(None) => break,
+ Err(error) => {
+ tracing::debug!(index, %error, "EGL device cannot import DMA-BUFs");
+ last_error = Some(error);
+ }
+ }
+ }
+ if gpus.is_empty() {
+ return Err(last_error.unwrap_or_else(|| anyhow!("EGL lists no devices")));
+ }
+ let supported = gpus.iter().map(|_| Default::default()).collect();
+ // Several GPUs of one vendor can't be told apart by the frame's
+ // exporter; the one that read the previous recording is tried first.
+ let last = remembered_gpu(
+ &gpus
+ .iter()
+ .map(|gpu| gpu.node.as_deref())
+ .collect::<Vec<_>>(),
+ LAST_IMPORT_NODE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .as_deref(),
+ );
+ Ok(Self {
+ gpus,
+ supported,
+ last,
+ remembered: false,
+ })
+ }
+
+ fn supported(&mut self, gpu: usize, fourcc: u32) -> &[u64] {
+ let gpus = &self.gpus;
+ self.supported[gpu]
+ .entry(fourcc)
+ .or_insert_with(|| gpus[gpu].modifiers(fourcc))
+ }
+
+ /// The modifiers some GPU can import `fourcc` with and read back.
+ pub(super) fn modifiers(&mut self, fourcc: u32) -> Vec<u64> {
+ let mut all = Vec::new();
+ for gpu in 0..self.gpus.len() {
+ for &modifier in self.supported(gpu, fourcc) {
+ if !all.contains(&modifier) {
+ all.push(modifier);
+ }
+ }
+ }
+ all
+ }
+
+ /// Reads one single-plane DMA-BUF into `out` as tightly packed RGBA,
+ /// starting with the GPU that read the last frame.
+ #[allow(clippy::too_many_arguments)]
+ pub(super) fn read_rgba(
+ &mut self,
+ fd: RawFd,
+ fourcc: u32,
+ modifier: u64,
+ width: u32,
+ height: u32,
+ offset: u32,
+ stride: u32,
+ out: &mut [u8],
+ ) -> anyhow::Result<()> {
+ let exporter = dma_buf_exporter(fd);
+ let drivers = self
+ .gpus
+ .iter()
+ .map(|gpu| gpu.driver.as_deref())
+ .collect::<Vec<_>>();
+ let order = import_order(&drivers, exporter.as_deref(), self.last);
+ let mut last_error = None;
+ for gpu in order {
+ if !self.supported(gpu, fourcc).contains(&modifier) {
+ continue;
+ }
+ match self.gpus[gpu].read_rgba(fd, fourcc, modifier, width, height, offset, stride, out)
+ {
+ Ok(()) => {
+ if self.last != gpu || !self.remembered {
+ *LAST_IMPORT_NODE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) =
+ self.gpus[gpu].node.clone();
+ self.remembered = true;
+ }
+ self.last = gpu;
+ return Ok(());
+ }
+ Err(error) => last_error = Some(error),
+ }
+ }
+ Err(last_error
+ .unwrap_or_else(|| anyhow!("no GPU imports modifier {modifier:#x} for {fourcc:#x}")))
+ }
+}
+
+impl GpuImporter {
+ /// The `index`th GPU EGL lists, or `None` past the last.
+ fn open(index: usize) -> anyhow::Result<Option<Self>> {
+ let egl = unsafe {
+ egl::DynamicInstance::<egl::EGL1_5>::load_required_from_filename("libEGL.so.1")
+ }
+ .map_err(|error| anyhow!("load libEGL: {error}"))?;
+ let proc = |name: &str| {
+ egl.get_proc_address(name)
+ .map(|f| f as *const c_void)
+ .ok_or_else(|| anyhow!("EGL has no {name}"))
+ };
+ let query_devices: QueryDevices =
+ unsafe { std::mem::transmute(proc("eglQueryDevicesEXT")?) };
+ let query_device_string: QueryDeviceString =
+ unsafe { std::mem::transmute(proc("eglQueryDeviceStringEXT")?) };
+ let get_platform_display: GetPlatformDisplay =
+ unsafe { std::mem::transmute(proc("eglGetPlatformDisplayEXT")?) };
+ let query_modifiers: QueryDmaBufModifiers =
+ unsafe { std::mem::transmute(proc("eglQueryDmaBufModifiersEXT")?) };
+ let image_target_texture: ImageTargetTexture =
+ unsafe { std::mem::transmute(proc("glEGLImageTargetTexture2DOES")?) };
+
+ let mut devices = [std::ptr::null_mut(); 16];
+ let mut count = 0;
+ if unsafe { query_devices(devices.len() as egl::Int, devices.as_mut_ptr(), &mut count) }
+ == egl::FALSE
+ {
+ bail!("eglQueryDevicesEXT failed");
+ }
+ // A GPU (a device with a render node) can import what a GPU
+ // allocated; without one, the devices EGL has are all there is.
+ let devices = &devices[..count.max(0) as usize];
+ let device_string = |device: *mut c_void, name: egl::Int| {
+ let value = unsafe { query_device_string(device, name) };
+ (!value.is_null()).then(|| {
+ unsafe { std::ffi::CStr::from_ptr(value) }
+ .to_string_lossy()
+ .into_owned()
+ })
+ };
+ let gpus = devices
+ .iter()
+ .filter_map(|&device| {
+ let node = device_string(device, EGL_DRM_RENDER_NODE_FILE_EXT)?;
+ let driver = render_node_driver(&node);
+ let extensions = device_string(device, egl::EXTENSIONS).unwrap_or_default();
+ // Opening a device no EGL vendor can drive only logs errors.
+ drivable(driver.as_deref(), &extensions).then_some((device, driver, Some(node)))
+ })
+ .collect::<Vec<_>>();
+ let candidates = if gpus.is_empty() {
+ devices.iter().map(|&device| (device, None, None)).collect()
+ } else {
+ gpus
+ };
+ let Some((device, driver, node)) = candidates.get(index).cloned() else {
+ return Ok(None);
+ };
+ let raw =
+ unsafe { get_platform_display(EGL_PLATFORM_DEVICE_EXT, device, std::ptr::null()) };
+ if raw.is_null() {
+ bail!("no EGL display for the device");
+ }
+ let display = unsafe { egl::Display::from_ptr(raw) };
+ egl.initialize(display).context("initialize EGL")?;
+ let extensions = egl
+ .query_string(Some(display), egl::EXTENSIONS)
+ .context("query EGL extensions")?
+ .to_string_lossy()
+ .into_owned();
+ for wanted in [
+ "EGL_EXT_image_dma_buf_import_modifiers",
+ "EGL_KHR_surfaceless_context",
+ ] {
+ if !extensions.split(' ').any(|extension| extension == wanted) {
+ bail!("EGL has no {wanted}");
+ }
+ }
+ egl.bind_api(egl::OPENGL_ES_API).context("bind GLES")?;
+ let config = egl
+ .choose_first_config(
+ display,
+ // Surfaceless: any surface type. The default asks for window
+ // surfaces, which a GPU's EGL device (NVIDIA's) never has.
+ &[
+ egl::SURFACE_TYPE,
+ 0,
+ egl::RENDERABLE_TYPE,
+ egl::OPENGL_ES2_BIT,
+ egl::NONE,
+ ],
+ )
+ .context("choose an EGL config")?
+ .ok_or_else(|| anyhow!("no GLES EGL config"))?;
+ let context = egl
+ .create_context(
+ display,
+ config,
+ None,
+ &[egl::CONTEXT_CLIENT_VERSION, 2, egl::NONE],
+ )
+ .context("create a GLES context")?;
+ let mut importer = Self {
+ egl,
+ display,
+ context,
+ gl: None,
+ image_target_texture,
+ query_modifiers,
+ framebuffer: None,
+ texture: None,
+ driver,
+ node,
+ };
+ importer.make_current()?;
+ importer.release();
+ Ok(Some(importer))
+ }
+
+ fn make_current(&mut self) -> anyhow::Result<()> {
+ self.egl
+ .make_current(self.display, None, None, Some(self.context))
+ .context("make the EGL context current")?;
+ if self.gl.is_none() {
+ let egl = &self.egl;
+ let gl = unsafe {
+ glow::Context::from_loader_function(|name| {
+ egl.get_proc_address(name)
+ .map_or(std::ptr::null(), |f| f as *const c_void)
+ })
+ };
+ self.gl = Some(gl);
+ }
+ Ok(())
+ }
+
+ /// Lets another thread make the context current.
+ pub(super) fn release(&self) {
+ let _ = self.egl.make_current(self.display, None, None, None);
+ }
+
+ /// The modifiers this GPU can import `fourcc` with and render to, so a
+ /// framebuffer can read them back.
+ pub(super) fn modifiers(&self, fourcc: u32) -> Vec<u64> {
+ let mut modifiers = vec![0u64; 64];
+ let mut external = vec![egl::FALSE; 64];
+ let mut count = 0;
+ let ok = unsafe {
+ (self.query_modifiers)(
+ self.display.as_ptr(),
+ fourcc as egl::Int,
+ modifiers.len() as egl::Int,
+ modifiers.as_mut_ptr(),
+ external.as_mut_ptr(),
+ &mut count,
+ )
+ };
+ if ok == egl::FALSE {
+ return Vec::new();
+ }
+ modifiers
+ .into_iter()
+ .zip(external)
+ .take(count.max(0) as usize)
+ .filter(|(modifier, external)| {
+ *external == egl::FALSE && *modifier != DRM_FORMAT_MOD_INVALID
+ })
+ .map(|(modifier, _)| modifier)
+ .collect()
+ }
+
+ /// Reads one single-plane DMA-BUF into `out` as tightly packed RGBA.
+ #[allow(clippy::too_many_arguments)]
+ pub(super) fn read_rgba(
+ &mut self,
+ fd: RawFd,
+ fourcc: u32,
+ modifier: u64,
+ width: u32,
+ height: u32,
+ offset: u32,
+ stride: u32,
+ out: &mut [u8],
+ ) -> anyhow::Result<()> {
+ let needed = width as usize * height as usize * 4;
+ if out.len() < needed {
+ bail!(
+ "RGBA readback buffer holds {} bytes, needs {needed}",
+ out.len()
+ );
+ }
+ self.make_current()?;
+ let attributes = [
+ egl::WIDTH as egl::Attrib,
+ width as egl::Attrib,
+ egl::HEIGHT as egl::Attrib,
+ height as egl::Attrib,
+ EGL_LINUX_DRM_FOURCC_EXT,
+ fourcc as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_FD_EXT,
+ fd as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_OFFSET_EXT,
+ offset as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_PITCH_EXT,
+ stride as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT,
+ (modifier & 0xffff_ffff) as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT,
+ (modifier >> 32) as egl::Attrib,
+ egl::ATTRIB_NONE,
+ ];
+ let image = self
+ .egl
+ .create_image(
+ self.display,
+ unsafe { egl::Context::from_ptr(EGL_NO_CONTEXT) },
+ EGL_LINUX_DMA_BUF_EXT,
+ unsafe { egl::ClientBuffer::from_ptr(std::ptr::null_mut()) },
+ &attributes,
+ )
+ .context("import the DMA-BUF into EGL")?;
+ let result = self.read_image(&image, width, height, &mut out[..needed]);
+ let _ = self.egl.destroy_image(self.display, image);
+ self.release();
+ result
+ }
+
+ fn read_image(
+ &mut self,
+ image: &egl::Image,
+ width: u32,
+ height: u32,
+ out: &mut [u8],
+ ) -> anyhow::Result<()> {
+ let gl = self.gl.as_ref().expect("GL loaded with the context");
+ unsafe {
+ let texture = match self.texture {
+ Some(texture) => texture,
+ None => {
+ let texture = gl.create_texture().map_err(|error| anyhow!(error))?;
+ self.texture = Some(texture);
+ texture
+ }
+ };
+ gl.bind_texture(glow::TEXTURE_2D, Some(texture));
+ (self.image_target_texture)(glow::TEXTURE_2D, image.as_ptr());
+ let framebuffer = match self.framebuffer {
+ Some(framebuffer) => framebuffer,
+ None => {
+ let framebuffer = gl.create_framebuffer().map_err(|error| anyhow!(error))?;
+ self.framebuffer = Some(framebuffer);
+ framebuffer
+ }
+ };
+ gl.bind_framebuffer(glow::FRAMEBUFFER, Some(framebuffer));
+ gl.framebuffer_texture_2d(
+ glow::FRAMEBUFFER,
+ glow::COLOR_ATTACHMENT0,
+ glow::TEXTURE_2D,
+ Some(texture),
+ 0,
+ );
+ let status = gl.check_framebuffer_status(glow::FRAMEBUFFER);
+ if status != glow::FRAMEBUFFER_COMPLETE {
+ bail!("the imported DMA-BUF is not readable (framebuffer status {status:#x})");
+ }
+ gl.pixel_store_i32(glow::PACK_ALIGNMENT, 1);
+ gl.read_pixels(
+ 0,
+ 0,
+ width as i32,
+ height as i32,
+ glow::RGBA,
+ glow::UNSIGNED_BYTE,
+ glow::PixelPackData::Slice(Some(out)),
+ );
+ let error = gl.get_error();
+ if error != glow::NO_ERROR {
+ bail!("reading the imported DMA-BUF failed (GL error {error:#x})");
+ }
+ }
+ Ok(())
+ }
+}
+
+impl Drop for GpuImporter {
+ fn drop(&mut self) {
+ if self.make_current().is_ok() {
+ if let Some(gl) = &self.gl {
+ unsafe {
+ if let Some(framebuffer) = self.framebuffer.take() {
+ gl.delete_framebuffer(framebuffer);
+ }
+ if let Some(texture) = self.texture.take() {
+ gl.delete_texture(texture);
+ }
+ }
+ }
+ self.release();
+ }
+ let _ = self.egl.destroy_context(self.display, self.context);
+ }
+}
+
+/// The kernel driver bound to a DRM node, from sysfs.
+fn render_node_driver(node: &str) -> Option<String> {
+ let name = std::path::Path::new(node).file_name()?;
+ let link = std::fs::read_link(
+ std::path::Path::new("/sys/class/drm")
+ .join(name)
+ .join("device/driver"),
+ )
+ .ok()?;
+ Some(link.file_name()?.to_string_lossy().into_owned())
+}
+
+/// Whether an EGL device can be initialized at all. vgem and vkms have no
+/// GPU behind them, and Mesa lists NVIDIA's nodes but only NVIDIA's own EGL
+/// (the one with `EGL_NV_device_cuda`) drives them.
+fn drivable(driver: Option<&str>, extensions: &str) -> bool {
+ match driver {
+ Some("vgem" | "vkms") => false,
+ Some(driver) if driver.starts_with("nvidia") => extensions
+ .split_ascii_whitespace()
+ .any(|extension| extension == "EGL_NV_device_cuda"),
+ _ => true,
+ }
+}
+
+/// The driver that exported a DMA-BUF (`exp_name` in its fdinfo).
+fn dma_buf_exporter(fd: RawFd) -> Option<String> {
+ let info = std::fs::read_to_string(format!("/proc/self/fdinfo/{fd}")).ok()?;
+ info.lines()
+ .find_map(|line| line.strip_prefix("exp_name:"))
+ .map(|name| name.trim().to_string())
+}
+
+/// GPUs to try a frame on: those whose driver exported it, then the rest,
+/// each starting from the GPU that read the last frame.
+fn import_order(drivers: &[Option<&str>], exporter: Option<&str>, last: usize) -> Vec<usize> {
+ let count = drivers.len();
+ let rotated = (0..count).map(|step| (last + step) % count);
+ let exported_by = |gpu: usize| {
+ matches!((drivers[gpu], exporter), (Some(driver), Some(exporter))
+ if exporter.starts_with(driver) || driver.starts_with(exporter))
+ };
+ let (mut order, rest): (Vec<_>, Vec<_>) = rotated.partition(|&gpu| exported_by(gpu));
+ order.extend(rest);
+ order
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn devices_without_a_driver_for_them_are_not_opened() {
+ assert!(!drivable(Some("vgem"), "EGL_EXT_device_drm"));
+ assert!(!drivable(Some("vkms"), ""));
+ assert!(!drivable(
+ Some("nvidia"),
+ "EGL_EXT_device_drm EGL_EXT_device_drm_render_node"
+ ));
+ assert!(drivable(
+ Some("nvidia"),
+ "EGL_NV_device_cuda EGL_EXT_device_drm"
+ ));
+ assert!(drivable(Some("amdgpu"), "EGL_EXT_device_drm"));
+ assert!(drivable(None, ""));
+ }
+
+ #[test]
+ fn the_gpu_that_read_the_last_recording_is_tried_first() {
+ let nodes = [
+ Some("/dev/dri/renderD128"),
+ Some("/dev/dri/renderD129"),
+ Some("/dev/dri/renderD131"),
+ ];
+ assert_eq!(remembered_gpu(&nodes, Some("/dev/dri/renderD131")), 2);
+ assert_eq!(remembered_gpu(&nodes, None), 0);
+ // A GPU that has gone since (unplugged, driver reloaded) is ignored.
+ assert_eq!(remembered_gpu(&nodes, Some("/dev/dri/renderD130")), 0);
+ }
+
+ #[test]
+ fn a_frame_goes_first_to_the_gpu_whose_driver_exported_it() {
+ // An Intel and NVIDIA laptop: niri renders on NVIDIA, EGL lists Intel first.
+ let drivers = [Some("i915"), Some("nvidia")];
+ assert_eq!(import_order(&drivers, Some("nvidia-drm"), 0), vec![1, 0]);
+ assert_eq!(import_order(&drivers, Some("i915"), 1), vec![0, 1]);
+ // Unknown exporter: the GPU that read the last frame first.
+ assert_eq!(import_order(&drivers, None, 1), vec![1, 0]);
+ // Several GPUs of one vendor keep the last one first among them.
+ let same = [Some("nvidia"), Some("nvidia"), Some("nvidia")];
+ assert_eq!(import_order(&same, Some("nvidia-drm"), 2), vec![2, 0, 1]);
+ assert!(import_order(&[], None, 0).is_empty());
+ }
+}
diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs
index 9e8a19c51..688cee38b 100644
--- a/crates/recording/src/sources/screen_capture/mod.rs
+++ b/crates/recording/src/sources/screen_capture/mod.rs
@@ -23,6 +23,8 @@ pub use macos::*;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
+mod linux_dmabuf_import;
+#[cfg(target_os = "linux")]
mod linux_shm;
#[cfg(target_os = "linux")]
pub use linux::*;
diff --git a/crates/rendering/src/cpu_yuv.rs b/crates/rendering/src/cpu_yuv.rs
index e32b53cd7..c2c0ed237 100644
--- a/crates/rendering/src/cpu_yuv.rs
+++ b/crates/rendering/src/cpu_yuv.rs
@@ -34,12 +34,19 @@ impl YuvMatrix {
]
}
+ /// `(rv, gu, gv, bu)` in 8.8 fixed point. Shared with the AVX2 kernel so
+ /// the two paths cannot drift.
#[inline(always)]
- fn chroma_contributions(self, d: i32, e: i32) -> [i32; 3] {
- let (rv, gu, gv, bu) = match self {
+ fn chroma_gains(self) -> (i32, i32, i32, i32) {
+ match self {
Self::Bt601 => (409, 100, 208, 516),
Self::Bt709 => (459, 55, 136, 541),
- };
+ }
+ }
+
+ #[inline(always)]
+ fn chroma_contributions(self, d: i32, e: i32) -> [i32; 3] {
+ let (rv, gu, gv, bu) = self.chroma_gains();
[rv * e + 128, -gu * d - gv * e + 128, bu * d + 128]
}
@@ -115,6 +122,409 @@ pub(crate) fn bt709_chroma(r: i32, g: i32, b: i32) -> [u8; 2] {
]
}
+/// Limited-range BT.709 RGBA8 to tightly packed NV12.
+///
+/// Luma is one sample per pixel. Chroma is the mean of each complete 2×2
+/// block, `(sum + 2) / 4`, then [`bt709_chroma`]. A trailing odd column or row
+/// keeps its luma and does not contribute a chroma sample. Rows may be padded;
+/// `src_stride` is the byte distance between RGBA rows and must be at least
+/// `width * 4` for the vector path.
+pub(crate) fn rgba_to_nv12(
+ src: &[u8],
+ src_stride: usize,
+ width: usize,
+ height: usize,
+ dst: &mut [u8],
+) {
+ #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+ if is_x86_feature_detected!("avx2")
+ && rgba_nv12_bounds_ok(src.len(), src_stride, width, height, dst.len())
+ {
+ // SAFETY: AVX2 is enabled on this CPU, and the bounds check covers every
+ // byte the kernel reads or writes, including the width%8 scalar tail.
+ unsafe {
+ rgba_to_nv12_avx2(src, src_stride, width, height, dst);
+ }
+ return;
+ }
+ rgba_to_nv12_scalar(src, src_stride, width, height, dst);
+}
+
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+unsafe fn rgba_to_nv12_avx2(
+ src: &[u8],
+ src_stride: usize,
+ width: usize,
+ height: usize,
+ dst: &mut [u8],
+) {
+ unsafe {
+ rgba_nv12_avx2::convert(src, src_stride, width, height, dst);
+ }
+}
+
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+fn rgba_nv12_bounds_ok(
+ src_len: usize,
+ src_stride: usize,
+ width: usize,
+ height: usize,
+ dst_len: usize,
+) -> bool {
+ if width == 0 || height == 0 {
+ return false;
+ }
+ let Some(row_bytes) = width.checked_mul(4) else {
+ return false;
+ };
+ if src_stride < row_bytes {
+ return false;
+ }
+ let Some(src_need) = (height - 1)
+ .checked_mul(src_stride)
+ .and_then(|offset| offset.checked_add(row_bytes))
+ else {
+ return false;
+ };
+ if src_need > src_len {
+ return false;
+ }
+ let Some(y_bytes) = width.checked_mul(height) else {
+ return false;
+ };
+ let Some(uv_bytes) = width.checked_mul(height / 2) else {
+ return false;
+ };
+ y_bytes
+ .checked_add(uv_bytes)
+ .is_some_and(|dst_need| dst_need <= dst_len)
+}
+
+fn rgba_to_nv12_scalar(src: &[u8], src_stride: usize, width: usize, height: usize, dst: &mut [u8]) {
+ rgba_to_nv12_rect(src, src_stride, width, height, 0, width, 0, height, dst);
+}
+
+#[allow(clippy::too_many_arguments)]
+fn rgba_to_nv12_rect(
+ src: &[u8],
+ src_stride: usize,
+ width: usize,
+ height: usize,
+ x0: usize,
+ x1: usize,
+ y0: usize,
+ y1: usize,
+ dst: &mut [u8],
+) {
+ for row in y0..y1 {
+ let src_off = row * src_stride + x0 * 4;
+ let src_row = &src[src_off..src_off + (x1 - x0) * 4];
+ let y_off = row * width + x0;
+ let y_row = &mut dst[y_off..y_off + (x1 - x0)];
+ for (pixel, y) in src_row.chunks_exact(4).zip(y_row.iter_mut()) {
+ *y = bt709_luma(
+ i32::from(pixel[0]),
+ i32::from(pixel[1]),
+ i32::from(pixel[2]),
+ );
+ }
+ }
+
+ let uv_offset = width * height;
+ let col_start = x0 / 2;
+ let col_end = x1 / 2;
+ for row in y0.div_ceil(2)..(y1 / 2) {
+ let src0 = row * 2 * src_stride;
+ let src1 = src0 + src_stride;
+ let uv_off = uv_offset + row * width;
+ let uv_row = &mut dst[uv_off..uv_off + width];
+ for col in col_start..col_end {
+ let r = (i32::from(src[src0 + col * 8])
+ + i32::from(src[src0 + col * 8 + 4])
+ + i32::from(src[src1 + col * 8])
+ + i32::from(src[src1 + col * 8 + 4])
+ + 2)
+ / 4;
+ let g = (i32::from(src[src0 + col * 8 + 1])
+ + i32::from(src[src0 + col * 8 + 5])
+ + i32::from(src[src1 + col * 8 + 1])
+ + i32::from(src[src1 + col * 8 + 5])
+ + 2)
+ / 4;
+ let b = (i32::from(src[src0 + col * 8 + 2])
+ + i32::from(src[src0 + col * 8 + 6])
+ + i32::from(src[src1 + col * 8 + 2])
+ + i32::from(src[src1 + col * 8 + 6])
+ + 2)
+ / 4;
+ [uv_row[col * 2], uv_row[col * 2 + 1]] = bt709_chroma(r, g, b);
+ }
+ }
+}
+
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+mod rgba_nv12_avx2 {
+ #[cfg(target_arch = "x86")]
+ use std::arch::x86::*;
+ #[cfg(target_arch = "x86_64")]
+ use std::arch::x86_64::*;
+
+ use super::{BT709_CB, BT709_CR, BT709_Y};
+
+ struct Job {
+ src: *const u8,
+ src_stride: usize,
+ y: *mut u8,
+ uv: *mut u8,
+ stride: usize,
+ }
+
+ struct Coeffs {
+ y_r: __m256i,
+ y_g: __m256i,
+ y_b: __m256i,
+ cb_r: __m256i,
+ cb_g: __m256i,
+ cb_b: __m256i,
+ cr_r: __m256i,
+ cr_g: __m256i,
+ cr_b: __m256i,
+ round: __m256i,
+ two: __m256i,
+ y_bias: __m256i,
+ c_bias: __m256i,
+ y_lo: __m256i,
+ y_hi: __m256i,
+ c_lo: __m256i,
+ c_hi: __m256i,
+ mask: __m256i,
+ pair_mask: __m256i,
+ gather: __m256i,
+ }
+
+ impl Coeffs {
+ #[target_feature(enable = "avx2")]
+ unsafe fn new() -> Self {
+ Self {
+ y_r: _mm256_set1_epi32(BT709_Y[0]),
+ y_g: _mm256_set1_epi32(BT709_Y[1]),
+ y_b: _mm256_set1_epi32(BT709_Y[2]),
+ cb_r: _mm256_set1_epi32(BT709_CB[0]),
+ cb_g: _mm256_set1_epi32(BT709_CB[1]),
+ cb_b: _mm256_set1_epi32(BT709_CB[2]),
+ cr_r: _mm256_set1_epi32(BT709_CR[0]),
+ cr_g: _mm256_set1_epi32(BT709_CR[1]),
+ cr_b: _mm256_set1_epi32(BT709_CR[2]),
+ round: _mm256_set1_epi32(1 << 15),
+ two: _mm256_set1_epi32(2),
+ y_bias: _mm256_set1_epi32(16),
+ c_bias: _mm256_set1_epi32(128),
+ y_lo: _mm256_set1_epi32(16),
+ y_hi: _mm256_set1_epi32(235),
+ c_lo: _mm256_set1_epi32(16),
+ c_hi: _mm256_set1_epi32(240),
+ mask: _mm256_set1_epi32(0x00FF),
+ pair_mask: _mm256_set1_epi64x(0xFFFF_FFFF),
+ gather: _mm256_setr_epi32(0, 1, 4, 5, 2, 3, 6, 7),
+ }
+ }
+ }
+
+ pub(super) unsafe fn convert(
+ src: &[u8],
+ src_stride: usize,
+ width: usize,
+ height: usize,
+ dst: &mut [u8],
+ ) {
+ unsafe {
+ convert_avx2(src, src_stride, width, height, dst);
+ }
+ }
+
+ #[target_feature(enable = "avx2")]
+ unsafe fn convert_avx2(
+ src: &[u8],
+ src_stride: usize,
+ width: usize,
+ height: usize,
+ dst: &mut [u8],
+ ) {
+ unsafe {
+ let vector_width = (width / 8) * 8;
+ let coeffs = Coeffs::new();
+ let job = Job {
+ src: src.as_ptr(),
+ src_stride,
+ y: dst.as_mut_ptr(),
+ uv: dst.as_mut_ptr().add(width * height),
+ stride: width,
+ };
+ let mut y = 0;
+ while y + 1 < height {
+ let mut x = 0;
+ while x + 16 <= vector_width {
+ kernel8(&job, &coeffs, x, y);
+ kernel8(&job, &coeffs, x + 8, y);
+ x += 16;
+ }
+ if x + 8 <= vector_width {
+ kernel8(&job, &coeffs, x, y);
+ }
+ y += 2;
+ }
+ if vector_width < width {
+ let mut y = 0;
+ while y + 1 < height {
+ super::rgba_to_nv12_rect(
+ src,
+ src_stride,
+ width,
+ height,
+ vector_width,
+ width,
+ y,
+ y + 2,
+ dst,
+ );
+ y += 2;
+ }
+ }
+ if height % 2 == 1 {
+ super::rgba_to_nv12_rect(
+ src,
+ src_stride,
+ width,
+ height,
+ 0,
+ width,
+ height - 1,
+ height,
+ dst,
+ );
+ }
+ }
+ }
+
+ #[target_feature(enable = "avx2")]
+ #[inline]
+ unsafe fn kernel8(job: &Job, coeffs: &Coeffs, x: usize, y: usize) {
+ unsafe {
+ let row0 = job.src.add(y * job.src_stride + x * 4);
+ let row1 = job.src.add((y + 1) * job.src_stride + x * 4);
+ let (r0, g0, b0) = unpack8(_mm256_loadu_si256(row0.cast()), coeffs.mask);
+ let (r1, g1, b1) = unpack8(_mm256_loadu_si256(row1.cast()), coeffs.mask);
+ store_y8(job.y.add(y * job.stride + x), luma8(r0, g0, b0, coeffs));
+ store_y8(
+ job.y.add((y + 1) * job.stride + x),
+ luma8(r1, g1, b1, coeffs),
+ );
+ // Mean the 2×2 block in pixel units before the matrix. Shifting the
+ // weighted sum by 18 is a different rounding and misses `bt709_chroma`.
+ let r_avg = average8(pair_sum(r0, coeffs), pair_sum(r1, coeffs), coeffs);
+ let g_avg = average8(pair_sum(g0, coeffs), pair_sum(g1, coeffs), coeffs);
+ let b_avg = average8(pair_sum(b0, coeffs), pair_sum(b1, coeffs), coeffs);
+ let cb = chroma8(
+ r_avg,
+ g_avg,
+ b_avg,
+ coeffs.cb_r,
+ coeffs.cb_g,
+ coeffs.cb_b,
+ coeffs,
+ );
+ let cr = chroma8(
+ r_avg,
+ g_avg,
+ b_avg,
+ coeffs.cr_r,
+ coeffs.cr_g,
+ coeffs.cr_b,
+ coeffs,
+ );
+ store_uv8(job, x, y / 2, cb, cr, coeffs.gather);
+ }
+ }
+
+ #[target_feature(enable = "avx2")]
+ #[inline]
+ unsafe fn unpack8(pix: __m256i, mask: __m256i) -> (__m256i, __m256i, __m256i) {
+ let r = _mm256_and_si256(pix, mask);
+ let g = _mm256_and_si256(_mm256_srli_epi32(pix, 8), mask);
+ let b = _mm256_and_si256(_mm256_srli_epi32(pix, 16), mask);
+ (r, g, b)
+ }
+
+ #[target_feature(enable = "avx2")]
+ #[inline]
+ unsafe fn luma8(r: __m256i, g: __m256i, b: __m256i, coeffs: &Coeffs) -> __m256i {
+ let mut acc = _mm256_mullo_epi32(r, coeffs.y_r);
+ acc = _mm256_add_epi32(acc, _mm256_mullo_epi32(g, coeffs.y_g));
+ acc = _mm256_add_epi32(acc, _mm256_mullo_epi32(b, coeffs.y_b));
+ acc = _mm256_srai_epi32(_mm256_add_epi32(acc, coeffs.round), 16);
+ acc = _mm256_add_epi32(acc, coeffs.y_bias);
+ _mm256_min_epi32(_mm256_max_epi32(acc, coeffs.y_lo), coeffs.y_hi)
+ }
+
+ #[target_feature(enable = "avx2")]
+ #[inline]
+ unsafe fn pair_sum(v: __m256i, coeffs: &Coeffs) -> __m256i {
+ let shifted = _mm256_srli_epi64(v, 32);
+ let sum = _mm256_add_epi32(v, shifted);
+ _mm256_and_si256(sum, coeffs.pair_mask)
+ }
+
+ #[target_feature(enable = "avx2")]
+ #[inline]
+ unsafe fn average8(row0: __m256i, row1: __m256i, coeffs: &Coeffs) -> __m256i {
+ let sum = _mm256_add_epi32(row0, row1);
+ _mm256_srai_epi32(_mm256_add_epi32(sum, coeffs.two), 2)
+ }
+
+ #[target_feature(enable = "avx2")]
+ #[inline]
+ unsafe fn chroma8(
+ r: __m256i,
+ g: __m256i,
+ b: __m256i,
+ c_r: __m256i,
+ c_g: __m256i,
+ c_b: __m256i,
+ coeffs: &Coeffs,
+ ) -> __m256i {
+ let mut acc = _mm256_mullo_epi32(r, c_r);
+ acc = _mm256_add_epi32(acc, _mm256_mullo_epi32(g, c_g));
+ acc = _mm256_add_epi32(acc, _mm256_mullo_epi32(b, c_b));
+ acc = _mm256_srai_epi32(_mm256_add_epi32(acc, coeffs.round), 16);
+ acc = _mm256_add_epi32(acc, coeffs.c_bias);
+ _mm256_min_epi32(_mm256_max_epi32(acc, coeffs.c_lo), coeffs.c_hi)
+ }
+
+ #[target_feature(enable = "avx2")]
+ #[inline]
+ unsafe fn store_y8(ptr: *mut u8, v: __m256i) {
+ unsafe {
+ let packed = _mm256_packs_epi32(v, _mm256_setzero_si256());
+ let grouped = _mm256_permute4x64_epi64(packed, 0xD8);
+ let bytes = _mm_packus_epi16(_mm256_castsi256_si128(grouped), _mm_setzero_si128());
+ _mm_storel_epi64(ptr.cast(), bytes);
+ }
+ }
+
+ #[target_feature(enable = "avx2")]
+ #[inline]
+ unsafe fn store_uv8(job: &Job, x: usize, cy: usize, cb: __m256i, cr: __m256i, gather: __m256i) {
+ unsafe {
+ let cb = _mm256_permutevar8x32_epi32(_mm256_shuffle_epi32(cb, 0xD8), gather);
+ let cr = _mm256_permutevar8x32_epi32(_mm256_shuffle_epi32(cr, 0xD8), gather);
+ let lo = _mm_unpacklo_epi32(_mm256_castsi256_si128(cb), _mm256_castsi256_si128(cr));
+ let hi = _mm_unpackhi_epi32(_mm256_castsi256_si128(cb), _mm256_castsi256_si128(cr));
+ let bytes = _mm_packus_epi16(_mm_packs_epi32(lo, hi), _mm_setzero_si128());
+ _mm_storel_epi64(job.uv.add(cy * job.stride + x).cast(), bytes);
+ }
+ }
+}
+
/// BT.601 [`nv12_to_rgba_with_matrix`], the conversion this module always did.
pub fn nv12_to_rgba(
y_data: &[u8],
@@ -173,6 +583,28 @@ pub fn nv12_to_rgba_with_matrix(
uv_stride: u32,
output: &mut [u8],
matrix: YuvMatrix,
+) {
+ #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+ if nv12_simd_inputs_fit(y_data, uv_data, output, width, height, y_stride, uv_stride) {
+ return nv12_to_rgba_simd(
+ y_data, uv_data, width, height, y_stride, uv_stride, output, matrix,
+ );
+ }
+ nv12_to_rgba_scalar(
+ y_data, uv_data, width, height, y_stride, uv_stride, output, matrix,
+ );
+}
+
+#[allow(clippy::too_many_arguments)]
+fn nv12_to_rgba_scalar(
+ y_data: &[u8],
+ uv_data: &[u8],
+ width: u32,
+ height: u32,
+ y_stride: u32,
+ uv_stride: u32,
+ output: &mut [u8],
+ matrix: YuvMatrix,
) {
let width = width as usize;
let height = height as usize;
@@ -220,6 +652,31 @@ pub fn yuv420p_to_rgba_with_matrix(
uv_stride: u32,
output: &mut [u8],
matrix: YuvMatrix,
+) {
+ #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+ if yuv420p_simd_inputs_fit(
+ y_data, u_data, v_data, output, width, height, y_stride, uv_stride,
+ ) {
+ return yuv420p_to_rgba_simd(
+ y_data, u_data, v_data, width, height, y_stride, uv_stride, output, matrix,
+ );
+ }
+ yuv420p_to_rgba_scalar(
+ y_data, u_data, v_data, width, height, y_stride, uv_stride, output, matrix,
+ );
+}
+
+#[allow(clippy::too_many_arguments)]
+fn yuv420p_to_rgba_scalar(
+ y_data: &[u8],
+ u_data: &[u8],
+ v_data: &[u8],
+ width: u32,
+ height: u32,
+ y_stride: u32,
+ uv_stride: u32,
+ output: &mut [u8],
+ matrix: YuvMatrix,
) {
let width = width as usize;
let height = height as usize;
@@ -282,7 +739,7 @@ impl SimdLevel {
pub fn pixels_per_iteration(self) -> usize {
match self {
- SimdLevel::Avx2 => 16,
+ SimdLevel::Avx2 => 8,
SimdLevel::Sse2 => 8,
SimdLevel::Scalar => 1,
}
@@ -333,27 +790,8 @@ pub fn nv12_to_rgba_simd_with_progress(
return;
}
- let y_required = (height_usize - 1)
- .saturating_mul(y_stride_usize)
- .saturating_add(width_usize);
-
- let uv_height = height_usize.div_ceil(2);
- let uv_width_bytes = width_usize.div_ceil(2) * 2;
- let uv_required = uv_height
- .saturating_sub(1)
- .saturating_mul(uv_stride_usize)
- .saturating_add(uv_width_bytes);
-
- let output_required = width_usize.saturating_mul(height_usize).saturating_mul(4);
-
- let strides_valid = y_stride_usize >= width_usize && uv_stride_usize >= uv_width_bytes;
-
- if !strides_valid
- || y_data.len() < y_required
- || uv_data.len() < uv_required
- || output.len() < output_required
- {
- return nv12_to_rgba_with_matrix(
+ if !nv12_simd_inputs_fit(y_data, uv_data, output, width, height, y_stride, uv_stride) {
+ return nv12_to_rgba_scalar(
y_data, uv_data, width, height, y_stride, uv_stride, output, matrix,
);
}
@@ -571,16 +1009,32 @@ unsafe fn nv12_convert_row_avx2(
matrix: YuvMatrix,
) {
unsafe {
- nv12_convert_row_sse2(
- y_data,
- uv_data,
- width,
- y_row_start,
- uv_row_start,
- out_row_start,
- output,
- matrix,
- );
+ let simd_px = width & !7;
+ if simd_px != 0 {
+ avx2_yuv::nv12(
+ y_data,
+ uv_data,
+ simd_px,
+ y_row_start,
+ uv_row_start,
+ out_row_start,
+ output,
+ matrix,
+ );
+ }
+ if simd_px != width {
+ nv12_convert_span_at(
+ y_data,
+ uv_data,
+ width,
+ simd_px,
+ y_row_start,
+ uv_row_start,
+ out_row_start,
+ output,
+ matrix,
+ );
+ }
}
}
@@ -621,10 +1075,41 @@ fn nv12_convert_row_scalar(
output: &mut [u8],
matrix: YuvMatrix,
) {
- let y_row = &y_data[y_row_start..y_row_start + width];
- let uv_row = &uv_data[uv_row_start..uv_row_start + width.div_ceil(2) * 2];
- let output_row = &mut output[out_row_start..out_row_start + width * 4];
+ nv12_convert_span_at(
+ y_data,
+ uv_data,
+ width,
+ 0,
+ y_row_start,
+ uv_row_start,
+ out_row_start,
+ output,
+ matrix,
+ );
+}
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+#[allow(clippy::too_many_arguments)]
+fn nv12_convert_span_at(
+ y_data: &[u8],
+ uv_data: &[u8],
+ width: usize,
+ start_col: usize,
+ y_row_start: usize,
+ uv_row_start: usize,
+ out_row_start: usize,
+ output: &mut [u8],
+ matrix: YuvMatrix,
+) {
+ debug_assert!(start_col.is_multiple_of(2));
+ let y_row = &y_data[y_row_start + start_col..y_row_start + width];
+ let uv_row = &uv_data[uv_row_start + start_col..uv_row_start + width.div_ceil(2) * 2];
+ let output_row = &mut output[out_row_start + start_col * 4..out_row_start + width * 4];
+ nv12_convert_span(y_row, uv_row, output_row, matrix);
+}
+
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+fn nv12_convert_span(y_row: &[u8], uv_row: &[u8], output_row: &mut [u8], matrix: YuvMatrix) {
for ((luma, chroma), pixels) in y_row
.chunks(2)
.zip(uv_row.chunks_exact(2))
@@ -644,6 +1129,21 @@ fn nv12_convert_row_scalar(
]);
}
}
+ // `chunks_mut(8)` yields pairs only, so an odd width leaves its last pixel.
+ if !y_row.len().is_multiple_of(2) {
+ let col = y_row.len() - 1;
+ let chroma = &uv_row[col..col + 2];
+ let d = i32::from(chroma[0]) - 128;
+ let e = i32::from(chroma[1]) - 128;
+ let [red, green, blue] = matrix.chroma_contributions(d, e);
+ let c = 298 * (i32::from(y_row[col]) - 16);
+ output_row[col * 4..col * 4 + 4].copy_from_slice(&[
+ clamp_u8((c + red) >> 8),
+ clamp_u8((c + green) >> 8),
+ clamp_u8((c + blue) >> 8),
+ 255,
+ ]);
+ }
}
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
@@ -722,28 +1222,10 @@ pub fn yuv420p_to_rgba_simd_with_progress(
return;
}
- let y_required = (height_usize - 1)
- .saturating_mul(y_stride_usize)
- .saturating_add(width_usize);
-
- let uv_height = height_usize.div_ceil(2);
- let uv_width = width_usize.div_ceil(2);
- let uv_required = uv_height
- .saturating_sub(1)
- .saturating_mul(uv_stride_usize)
- .saturating_add(uv_width);
-
- let output_required = width_usize.saturating_mul(height_usize).saturating_mul(4);
-
- let strides_valid = y_stride_usize >= width_usize && uv_stride_usize >= uv_width;
-
- if !strides_valid
- || y_data.len() < y_required
- || u_data.len() < uv_required
- || v_data.len() < uv_required
- || output.len() < output_required
- {
- return yuv420p_to_rgba_with_matrix(
+ if !yuv420p_simd_inputs_fit(
+ y_data, u_data, v_data, output, width, height, y_stride, uv_stride,
+ ) {
+ return yuv420p_to_rgba_scalar(
y_data, u_data, v_data, width, height, y_stride, uv_stride, output, matrix,
);
}
@@ -972,17 +1454,34 @@ unsafe fn yuv420p_convert_row_avx2(
matrix: YuvMatrix,
) {
unsafe {
- yuv420p_convert_row_sse2(
- y_data,
- u_data,
- v_data,
- width,
- y_row_start,
- uv_row_start,
- out_row_start,
- output,
- matrix,
- );
+ let simd_px = width & !7;
+ if simd_px != 0 {
+ avx2_yuv::yuv420p(
+ y_data,
+ u_data,
+ v_data,
+ simd_px,
+ y_row_start,
+ uv_row_start,
+ out_row_start,
+ output,
+ matrix,
+ );
+ }
+ if simd_px != width {
+ yuv420p_convert_span_at(
+ y_data,
+ u_data,
+ v_data,
+ width,
+ simd_px,
+ y_row_start,
+ uv_row_start,
+ out_row_start,
+ output,
+ matrix,
+ );
+ }
}
}
@@ -1026,12 +1525,51 @@ fn yuv420p_convert_row_scalar(
output: &mut [u8],
matrix: YuvMatrix,
) {
- let y_row = &y_data[y_row_start..y_row_start + width];
+ yuv420p_convert_span_at(
+ y_data,
+ u_data,
+ v_data,
+ width,
+ 0,
+ y_row_start,
+ uv_row_start,
+ out_row_start,
+ output,
+ matrix,
+ );
+}
+
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+#[allow(clippy::too_many_arguments)]
+fn yuv420p_convert_span_at(
+ y_data: &[u8],
+ u_data: &[u8],
+ v_data: &[u8],
+ width: usize,
+ start_col: usize,
+ y_row_start: usize,
+ uv_row_start: usize,
+ out_row_start: usize,
+ output: &mut [u8],
+ matrix: YuvMatrix,
+) {
+ debug_assert!(start_col.is_multiple_of(2));
let chroma_width = width.div_ceil(2);
- let u_row = &u_data[uv_row_start..uv_row_start + chroma_width];
- let v_row = &v_data[uv_row_start..uv_row_start + chroma_width];
- let output_row = &mut output[out_row_start..out_row_start + width * 4];
+ let y_row = &y_data[y_row_start + start_col..y_row_start + width];
+ let u_row = &u_data[uv_row_start + start_col / 2..uv_row_start + chroma_width];
+ let v_row = &v_data[uv_row_start + start_col / 2..uv_row_start + chroma_width];
+ let output_row = &mut output[out_row_start + start_col * 4..out_row_start + width * 4];
+ yuv420p_convert_span(y_row, u_row, v_row, output_row, matrix);
+}
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+fn yuv420p_convert_span(
+ y_row: &[u8],
+ u_row: &[u8],
+ v_row: &[u8],
+ output_row: &mut [u8],
+ matrix: YuvMatrix,
+) {
for (((luma, u), v), pixels) in y_row
.chunks(2)
.zip(u_row)
@@ -1052,6 +1590,20 @@ fn yuv420p_convert_row_scalar(
]);
}
}
+ // `chunks_mut(8)` yields pairs only, so an odd width leaves its last pixel.
+ if !y_row.len().is_multiple_of(2) {
+ let col = y_row.len() - 1;
+ let d = i32::from(u_row[col / 2]) - 128;
+ let e = i32::from(v_row[col / 2]) - 128;
+ let [red, green, blue] = matrix.chroma_contributions(d, e);
+ let c = 298 * (i32::from(y_row[col]) - 16);
+ output_row[col * 4..col * 4 + 4].copy_from_slice(&[
+ clamp_u8((c + red) >> 8),
+ clamp_u8((c + green) >> 8),
+ clamp_u8((c + blue) >> 8),
+ 255,
+ ]);
+ }
}
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
@@ -1096,9 +1648,254 @@ fn clamp_u8(val: i32) -> u8 {
val.clamp(0, 255) as u8
}
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+#[allow(clippy::too_many_arguments)]
+fn nv12_simd_inputs_fit(
+ y_data: &[u8],
+ uv_data: &[u8],
+ output: &[u8],
+ width: u32,
+ height: u32,
+ y_stride: u32,
+ uv_stride: u32,
+) -> bool {
+ let width = width as usize;
+ let height = height as usize;
+ if width == 0 || height == 0 {
+ return false;
+ }
+ let y_stride = y_stride as usize;
+ let uv_stride = uv_stride as usize;
+ let y_required = (height - 1).saturating_mul(y_stride).saturating_add(width);
+ let uv_height = height.div_ceil(2);
+ let uv_width_bytes = width.div_ceil(2) * 2;
+ let uv_required = uv_height
+ .saturating_sub(1)
+ .saturating_mul(uv_stride)
+ .saturating_add(uv_width_bytes);
+ let output_required = width.saturating_mul(height).saturating_mul(4);
+ y_stride >= width
+ && uv_stride >= uv_width_bytes
+ && y_data.len() >= y_required
+ && uv_data.len() >= uv_required
+ && output.len() >= output_required
+}
+
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+#[allow(clippy::too_many_arguments)]
+fn yuv420p_simd_inputs_fit(
+ y_data: &[u8],
+ u_data: &[u8],
+ v_data: &[u8],
+ output: &[u8],
+ width: u32,
+ height: u32,
+ y_stride: u32,
+ uv_stride: u32,
+) -> bool {
+ let width = width as usize;
+ let height = height as usize;
+ if width == 0 || height == 0 {
+ return false;
+ }
+ let y_stride = y_stride as usize;
+ let uv_stride = uv_stride as usize;
+ let y_required = (height - 1).saturating_mul(y_stride).saturating_add(width);
+ let uv_height = height.div_ceil(2);
+ let uv_width = width.div_ceil(2);
+ let uv_required = uv_height
+ .saturating_sub(1)
+ .saturating_mul(uv_stride)
+ .saturating_add(uv_width);
+ let output_required = width.saturating_mul(height).saturating_mul(4);
+ y_stride >= width
+ && uv_stride >= uv_width
+ && y_data.len() >= y_required
+ && u_data.len() >= uv_required
+ && v_data.len() >= uv_required
+ && output.len() >= output_required
+}
+
+#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+mod avx2_yuv {
+ use super::YuvMatrix;
+
+ #[cfg(target_arch = "x86")]
+ use std::arch::x86::*;
+ #[cfg(target_arch = "x86_64")]
+ use std::arch::x86_64::*;
+
+ struct Coeffs {
+ rv: __m256i,
+ gu: __m256i,
+ gv: __m256i,
+ bu: __m256i,
+ bias128: __m256i,
+ y16: __m256i,
+ gain: __m256i,
+ alpha: __m256i,
+ zero: __m256i,
+ max: __m256i,
+ nv12_u: __m256i,
+ nv12_v: __m256i,
+ dup4: __m256i,
+ }
+
+ impl Coeffs {
+ #[inline]
+ #[target_feature(enable = "avx2")]
+ unsafe fn new(matrix: YuvMatrix) -> Self {
+ let (rv, gu, gv, bu) = matrix.chroma_gains();
+ Self {
+ rv: _mm256_set1_epi32(rv),
+ gu: _mm256_set1_epi32(gu),
+ gv: _mm256_set1_epi32(gv),
+ bu: _mm256_set1_epi32(bu),
+ bias128: _mm256_set1_epi32(128),
+ y16: _mm256_set1_epi32(16),
+ gain: _mm256_set1_epi32(298),
+ alpha: _mm256_set1_epi32(0xFF00_0000u32 as i32),
+ zero: _mm256_setzero_si256(),
+ max: _mm256_set1_epi32(255),
+ nv12_u: _mm256_setr_epi32(0, 0, 2, 2, 4, 4, 6, 6),
+ nv12_v: _mm256_setr_epi32(1, 1, 3, 3, 5, 5, 7, 7),
+ dup4: _mm256_setr_epi32(0, 0, 1, 1, 2, 2, 3, 3),
+ }
+ }
+ }
+
+ #[inline]
+ #[target_feature(enable = "avx2")]
+ unsafe fn clamp8(sum: __m256i, coeffs: &Coeffs) -> __m256i {
+ let shifted = _mm256_srai_epi32(sum, 8);
+ _mm256_min_epi32(_mm256_max_epi32(shifted, coeffs.zero), coeffs.max)
+ }
+
+ #[inline]
+ #[target_feature(enable = "avx2")]
+ unsafe fn store_rgba(out: *mut u8, luma: __m256i, d: __m256i, e: __m256i, coeffs: &Coeffs) {
+ unsafe {
+ let red = _mm256_add_epi32(_mm256_mullo_epi32(coeffs.rv, e), coeffs.bias128);
+ let green = _mm256_sub_epi32(
+ coeffs.bias128,
+ _mm256_add_epi32(
+ _mm256_mullo_epi32(coeffs.gu, d),
+ _mm256_mullo_epi32(coeffs.gv, e),
+ ),
+ );
+ let blue = _mm256_add_epi32(_mm256_mullo_epi32(coeffs.bu, d), coeffs.bias128);
+ let r = clamp8(_mm256_add_epi32(luma, red), coeffs);
+ let g = clamp8(_mm256_add_epi32(luma, green), coeffs);
+ let b = clamp8(_mm256_add_epi32(luma, blue), coeffs);
+ let pixels = _mm256_or_si256(
+ r,
+ _mm256_or_si256(
+ _mm256_slli_epi32(g, 8),
+ _mm256_or_si256(_mm256_slli_epi32(b, 16), coeffs.alpha),
+ ),
+ );
+ _mm256_storeu_si256(out.cast(), pixels);
+ }
+ }
+
+ #[inline]
+ #[target_feature(enable = "avx2")]
+ unsafe fn luma8(y: *const u8, coeffs: &Coeffs) -> __m256i {
+ unsafe {
+ let y8 = _mm_loadl_epi64(y.cast());
+ let y32 = _mm256_cvtepu8_epi32(y8);
+ _mm256_mullo_epi32(_mm256_sub_epi32(y32, coeffs.y16), coeffs.gain)
+ }
+ }
+
+ #[allow(clippy::too_many_arguments)]
+ #[target_feature(enable = "avx2")]
+ pub(super) unsafe fn nv12(
+ y_data: &[u8],
+ uv_data: &[u8],
+ width: usize,
+ y_row_start: usize,
+ uv_row_start: usize,
+ out_row_start: usize,
+ output: &mut [u8],
+ matrix: YuvMatrix,
+ ) {
+ unsafe {
+ debug_assert!(width.is_multiple_of(8));
+ let y = &y_data[y_row_start..y_row_start + width];
+ let uv = &uv_data[uv_row_start..uv_row_start + width];
+ let out = &mut output[out_row_start..out_row_start + width * 4];
+ let coeffs = Coeffs::new(matrix);
+ let mut x = 0;
+ while x < width {
+ let luma = luma8(y.as_ptr().add(x), &coeffs);
+ let uv32 = _mm256_cvtepu8_epi32(_mm_loadl_epi64(uv.as_ptr().add(x).cast()));
+ let d = _mm256_sub_epi32(
+ _mm256_permutevar8x32_epi32(uv32, coeffs.nv12_u),
+ coeffs.bias128,
+ );
+ let e = _mm256_sub_epi32(
+ _mm256_permutevar8x32_epi32(uv32, coeffs.nv12_v),
+ coeffs.bias128,
+ );
+ store_rgba(out.as_mut_ptr().add(x * 4), luma, d, e, &coeffs);
+ x += 8;
+ }
+ }
+ }
+
+ #[allow(clippy::too_many_arguments)]
+ #[target_feature(enable = "avx2")]
+ pub(super) unsafe fn yuv420p(
+ y_data: &[u8],
+ u_data: &[u8],
+ v_data: &[u8],
+ width: usize,
+ y_row_start: usize,
+ uv_row_start: usize,
+ out_row_start: usize,
+ output: &mut [u8],
+ matrix: YuvMatrix,
+ ) {
+ unsafe {
+ debug_assert!(width.is_multiple_of(8));
+ let chroma_width = width / 2;
+ let y = &y_data[y_row_start..y_row_start + width];
+ let u = &u_data[uv_row_start..uv_row_start + chroma_width];
+ let v = &v_data[uv_row_start..uv_row_start + chroma_width];
+ let out = &mut output[out_row_start..out_row_start + width * 4];
+ let coeffs = Coeffs::new(matrix);
+ let mut x = 0;
+ while x < width {
+ let luma = luma8(y.as_ptr().add(x), &coeffs);
+ let u32 = std::ptr::read_unaligned(u.as_ptr().add(x / 2).cast::<u32>());
+ let v32 = std::ptr::read_unaligned(v.as_ptr().add(x / 2).cast::<u32>());
+ let d = _mm256_sub_epi32(
+ _mm256_permutevar8x32_epi32(
+ _mm256_cvtepu8_epi32(_mm_cvtsi32_si128(u32 as i32)),
+ coeffs.dup4,
+ ),
+ coeffs.bias128,
+ );
+ let e = _mm256_sub_epi32(
+ _mm256_permutevar8x32_epi32(
+ _mm256_cvtepu8_epi32(_mm_cvtsi32_si128(v32 as i32)),
+ coeffs.dup4,
+ ),
+ coeffs.bias128,
+ );
+ store_rgba(out.as_mut_ptr().add(x * 4), luma, d, e, &coeffs);
+ x += 8;
+ }
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
+ #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+ use std::arch::is_x86_feature_detected;
#[test]
fn test_nv12_basic_conversion() {
@@ -1147,7 +1944,7 @@ mod tests {
let mut output_scalar = vec![0u8; (width * height * 4) as usize];
let mut output_simd = vec![0u8; (width * height * 4) as usize];
- nv12_to_rgba_with_matrix(
+ nv12_to_rgba_scalar(
&y_data,
&uv_data,
width,
@@ -1219,7 +2016,7 @@ mod tests {
let mut output1 = vec![0u8; (width * height * 4) as usize];
let mut output2 = vec![0u8; (width * height * 4) as usize];
- nv12_to_rgba_with_matrix(
+ nv12_to_rgba_scalar(
&y_data,
&uv_data,
width,
@@ -1270,7 +2067,7 @@ mod tests {
let mut output_scalar = vec![0u8; (width * height * 4) as usize];
let mut output_simd = vec![0u8; (width * height * 4) as usize];
- yuv420p_to_rgba_with_matrix(
+ yuv420p_to_rgba_scalar(
&y_data,
&u_data,
&v_data,
@@ -1323,8 +2120,9 @@ mod tests {
let uv_data: Vec<u8> = (0..uv_len).map(|i| (i * 53 + 97) as u8).collect();
let mut expected = vec![0; (width * height * 4) as usize];
let mut actual = vec![0xa5; expected.len()];
+ let mut dispatched = vec![0xa5; expected.len()];
- nv12_to_rgba_with_matrix(
+ nv12_to_rgba_scalar(
&y_data,
&uv_data,
width,
@@ -1344,11 +2142,25 @@ mod tests {
&mut actual,
matrix,
);
+ nv12_to_rgba_with_matrix(
+ &y_data,
+ &uv_data,
+ width,
+ height,
+ y_stride,
+ uv_stride,
+ &mut dispatched,
+ matrix,
+ );
assert_eq!(
actual, expected,
"NV12 {matrix:?} {width}x{height}, y_stride={y_stride}, uv_stride={uv_stride}"
);
+ assert_eq!(
+ dispatched, expected,
+ "NV12 dispatch {matrix:?} {width}x{height}, y_stride={y_stride}, uv_stride={uv_stride}"
+ );
}
}
}
@@ -1374,8 +2186,9 @@ mod tests {
let v_data: Vec<u8> = (0..uv_len).map(|i| (i * 71 + 193) as u8).collect();
let mut expected = vec![0; (width * height * 4) as usize];
let mut actual = vec![0xa5; expected.len()];
+ let mut dispatched = vec![0xa5; expected.len()];
- yuv420p_to_rgba_with_matrix(
+ yuv420p_to_rgba_scalar(
&y_data,
&u_data,
&v_data,
@@ -1397,11 +2210,26 @@ mod tests {
&mut actual,
matrix,
);
+ yuv420p_to_rgba_with_matrix(
+ &y_data,
+ &u_data,
+ &v_data,
+ width,
+ height,
+ y_stride,
+ uv_stride,
+ &mut dispatched,
+ matrix,
+ );
assert_eq!(
actual, expected,
"YUV420P {matrix:?} {width}x{height}, y_stride={y_stride}, uv_stride={uv_stride}"
);
+ assert_eq!(
+ dispatched, expected,
+ "YUV420P dispatch {matrix:?} {width}x{height}, y_stride={y_stride}, uv_stride={uv_stride}"
+ );
}
}
}
@@ -1617,13 +2445,279 @@ mod tests {
for matrix in [YuvMatrix::Bt601, YuvMatrix::Bt709] {
let mut simd = vec![0u8; (width * height * 4) as usize];
let mut scalar = simd.clone();
+ let mut dispatched = simd.clone();
nv12_to_rgba_simd(&y, &uv, width, height, width, width, &mut simd, matrix);
- nv12_to_rgba_with_matrix(&y, &uv, width, height, width, width, &mut scalar, matrix);
+ nv12_to_rgba_scalar(&y, &uv, width, height, width, width, &mut scalar, matrix);
+ nv12_to_rgba_with_matrix(
+ &y,
+ &uv,
+ width,
+ height,
+ width,
+ width,
+ &mut dispatched,
+ matrix,
+ );
assert_eq!(simd, scalar);
+ assert_eq!(dispatched, scalar);
assert_eq!(
&simd[..3],
&matrix.ycbcr_to_rgb(81 - 16, 90 - 128, 240 - 128)
);
}
}
+
+ #[test]
+ fn simd_matches_scalar_on_extremes_and_odd_tail() {
+ let samples = [0u8, 1, 15, 16, 17, 128, 234, 235, 236, 254, 255];
+ let (width, height) = (9u32, 3u32);
+ for matrix in [YuvMatrix::Bt601, YuvMatrix::Bt709] {
+ for &y0 in &samples {
+ for &u0 in &samples {
+ for &v0 in &samples {
+ let y = vec![y0; (width * height) as usize];
+ let uv_rows = height.div_ceil(2);
+ let nv12_row = width.div_ceil(2) * 2;
+ let mut uv = vec![0u8; (nv12_row * uv_rows) as usize];
+ for pair in uv.chunks_exact_mut(2) {
+ pair[0] = u0;
+ pair[1] = v0;
+ }
+ let chroma = (width.div_ceil(2) * uv_rows) as usize;
+ let u = vec![u0; chroma];
+ let v = vec![v0; chroma];
+ let mut expected = vec![7u8; (width * height * 4) as usize];
+ let mut nv12 = vec![9u8; expected.len()];
+ let mut yuv = vec![9u8; expected.len()];
+ nv12_to_rgba_scalar(
+ &y,
+ &uv,
+ width,
+ height,
+ width,
+ nv12_row,
+ &mut expected,
+ matrix,
+ );
+ nv12_to_rgba_with_matrix(
+ &y, &uv, width, height, width, nv12_row, &mut nv12, matrix,
+ );
+ assert_eq!(nv12, expected, "{matrix:?} y={y0} u={u0} v={v0}");
+ yuv420p_to_rgba_scalar(
+ &y,
+ &u,
+ &v,
+ width,
+ height,
+ width,
+ width.div_ceil(2),
+ &mut expected,
+ matrix,
+ );
+ yuv420p_to_rgba_with_matrix(
+ &y,
+ &u,
+ &v,
+ width,
+ height,
+ width,
+ width.div_ceil(2),
+ &mut yuv,
+ matrix,
+ );
+ assert_eq!(yuv, expected, "420 {matrix:?} y={y0} u={u0} v={v0}");
+ }
+ }
+ }
+ }
+ }
+
+ fn nv12_len(width: usize, height: usize) -> usize {
+ width * height + width * (height / 2)
+ }
+
+ fn sample_rgba(x: usize, y: usize) -> [u8; 4] {
+ [
+ x.wrapping_mul(17).wrapping_add(y.wrapping_mul(31)) as u8,
+ x.wrapping_mul(13)
+ .wrapping_add(y.wrapping_mul(7))
+ .wrapping_add(40) as u8,
+ x.wrapping_mul(3)
+ .wrapping_add(y.wrapping_mul(19))
+ .wrapping_add(90) as u8,
+ x.wrapping_mul(19).wrapping_add(y) as u8,
+ ]
+ }
+
+ fn rgba_frame(width: usize, height: usize, stride: usize) -> Vec<u8> {
+ let mut data = vec![0xA5; stride.saturating_mul(height)];
+ for y in 0..height {
+ for x in 0..width {
+ let pixel = sample_rgba(x, y);
+ let offset = y * stride + x * 4;
+ data[offset..offset + 4].copy_from_slice(&pixel);
+ }
+ }
+ data
+ }
+
+ fn assert_max_abs_diff(actual: &[u8], expected: &[u8], label: &str) {
+ assert_eq!(actual.len(), expected.len(), "{label} length");
+ let mut max_diff = 0i32;
+ let mut first = None;
+ for (index, (got, want)) in actual.iter().zip(expected).enumerate() {
+ let diff = i32::from(*got) - i32::from(*want);
+ let abs = diff.abs();
+ if abs > max_diff {
+ max_diff = abs;
+ }
+ if abs != 0 && first.is_none() {
+ first = Some((index, *want, *got));
+ }
+ }
+ assert!(
+ max_diff == 0,
+ "{label}: max abs diff {max_diff}, first mismatch {first:?}"
+ );
+ }
+
+ #[test]
+ fn rgba_to_nv12_empty_dimensions_are_a_no_op() {
+ let mut dst = Vec::new();
+ rgba_to_nv12(&[], 0, 0, 0, &mut dst);
+ rgba_to_nv12(&[], 0, 0, 4, &mut dst);
+ rgba_to_nv12(&[0; 16], 16, 4, 0, &mut dst);
+ }
+
+ #[test]
+ fn rgba_to_nv12_averages_the_block_before_the_matrix() {
+ // Blue 5 in one sample of the 2×2 block. The mean is (5 + 2) / 4 = 1,
+ // and BT.709 chroma of that mean is neutral. A >>18 weighted sum is Cb 129.
+ for (width, x) in [(2, 0), (8, 0), (10, 8), (16, 8)] {
+ let height = 2;
+ let mut rgba = vec![0u8; width * height * 4];
+ rgba[(x * 4) + 2] = 5;
+ let mut dst = vec![0u8; nv12_len(width, height)];
+ rgba_to_nv12(&rgba, width * 4, width, height, &mut dst);
+ let y_plane = width * height;
+ assert_eq!(dst[x], bt709_luma(0, 0, 5), "width {width} x {x}");
+ let chroma = bt709_chroma(0, 0, 1);
+ assert_eq!(chroma, [128, 128]);
+ let uv = y_plane + (x / 2) * 2;
+ assert_eq!(&dst[uv..uv + 2], &chroma, "width {width} x {x}");
+ }
+ }
+
+ #[test]
+ fn rgba_to_nv12_matches_scalar_on_odd_sizes_and_padding() {
+ let widths = [1, 2, 3, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65];
+ let heights = [1, 2, 3, 5, 8, 15, 17];
+ let pads = [0, 1, 7, 16, 63];
+ for width in widths {
+ for height in heights {
+ for pad in pads {
+ assert_rgba_nv12_exact(width, height, width * 4 + pad);
+ }
+ }
+ }
+ for (width, height, pad) in [(1919, 3, 7), (1920, 2, 0), (1921, 4, 13), (160, 9, 192)] {
+ assert_rgba_nv12_exact(width, height, width * 4 + pad);
+ }
+ }
+
+ fn assert_rgba_nv12_exact(width: usize, height: usize, stride: usize) {
+ let src = rgba_frame(width, height, stride);
+ let mut expected = vec![0x5A; nv12_len(width, height)];
+ let mut actual = expected.clone();
+ rgba_to_nv12_scalar(&src, stride, width, height, &mut expected);
+ rgba_to_nv12(&src, stride, width, height, &mut actual);
+ assert_max_abs_diff(
+ &actual,
+ &expected,
+ &format!("{width}x{height} stride {stride}"),
+ );
+ #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
+ if is_x86_feature_detected!("avx2") {
+ let mut direct = vec![0x5A; expected.len()];
+ unsafe {
+ rgba_to_nv12_avx2(&src, stride, width, height, &mut direct);
+ }
+ assert_max_abs_diff(
+ &direct,
+ &expected,
+ &format!("avx2 {width}x{height} stride {stride}"),
+ );
+ }
+ }
+
+ #[test]
+ #[ignore = "benchmark; run with --release --ignored --nocapture"]
+ fn benchmark_rgba_to_nv12() {
+ use std::time::Instant;
+
+ const WARMUP: usize = 8;
+ const SAMPLES: usize = 31;
+
+ fn median_ms(samples: &mut [std::time::Duration]) -> f64 {
+ samples.sort_unstable();
+ let mid = samples.len() / 2;
+ let median = if samples.len().is_multiple_of(2) {
+ (samples[mid - 1].as_secs_f64() + samples[mid].as_secs_f64()) / 2.0
+ } else {
+ samples[mid].as_secs_f64()
+ };
+ median * 1000.0
+ }
+
+ fn time_ms(mut invoke: impl FnMut()) -> f64 {
+ for _ in 0..WARMUP {
+ invoke();
+ }
+ let mut samples = Vec::with_capacity(SAMPLES);
+ for _ in 0..SAMPLES {
+ let start = Instant::now();
+ invoke();
+ samples.push(start.elapsed());
+ }
+ median_ms(&mut samples)
+ }
+
+ let cpu = std::fs::read_to_string("/proc/cpuinfo").unwrap_or_default();
+ let model = cpu
+ .lines()
+ .find(|line| line.starts_with("model name"))
+ .unwrap_or("model name: unknown");
+ println!("cpu: {model}");
+ println!("warmup: {WARMUP} samples: {SAMPLES} threads: 1");
+ println!("profile: opt-level=s codegen-units=1 lto=thin target-cpu=generic");
+ println!("rayon: not used by rgba_to_nv12");
+ println!("| size | stride | scalar ms | avx2 ms | speedup | max abs diff |");
+ println!("| --- | ---: | ---: | ---: | ---: | ---: |");
+
+ for (width, height) in [(1920, 1080), (3024, 1964), (3840, 2160)] {
+ let stride =
+ crate::frame_pipeline::padded_bytes_per_row((width as u32, height as u32)) as usize;
+ let src = rgba_frame(width, height, stride);
+ let mut scalar_dst = vec![0u8; nv12_len(width, height)];
+ let mut avx2_dst = scalar_dst.clone();
+ rgba_to_nv12_scalar(&src, stride, width, height, &mut scalar_dst);
+ rgba_to_nv12(&src, stride, width, height, &mut avx2_dst);
+ let mut max_diff = 0i32;
+ for (got, want) in avx2_dst.iter().zip(&scalar_dst) {
+ max_diff = max_diff.max((i32::from(*got) - i32::from(*want)).abs());
+ }
+ let scalar_ms = time_ms(|| {
+ rgba_to_nv12_scalar(&src, stride, width, height, &mut scalar_dst);
+ std::hint::black_box(scalar_dst.as_ptr());
+ });
+ let avx2_ms = time_ms(|| {
+ rgba_to_nv12(&src, stride, width, height, &mut avx2_dst);
+ std::hint::black_box(avx2_dst.as_ptr());
+ });
+ let speedup = scalar_ms / avx2_ms;
+ println!(
+ "| {width}x{height} | {stride} | {scalar_ms:.3} | {avx2_ms:.3} | {speedup:.2}x | {max_diff} |"
+ );
+ }
+ }
}
diff --git a/crates/rendering/src/cursor_interpolation.rs b/crates/rendering/src/cursor_interpolation.rs
index 002283bb6..5696c2d47 100644
--- a/crates/rendering/src/cursor_interpolation.rs
+++ b/crates/rendering/src/cursor_interpolation.rs
@@ -3,7 +3,9 @@ use std::{
sync::{Arc, Mutex},
};
-use cap_project::{ClickSpringConfig, CursorClickEvent, CursorEvents, CursorMoveEvent, XY};
+use cap_project::{
+ ClickSpringConfig, CursorClickEvent, CursorEvents, CursorMoveEvent, ProjectConfiguration, XY,
+};
use crate::{
Coord, RawDisplayUVSpace,
@@ -270,7 +272,7 @@ pub fn interpolate_cursor_with_click_spring(
smoothing_config,
click_spring,
);
- interpolate_timeline(&timeline, time_ms)
+ interpolate_timeline(&timeline.events, &timeline.cursor_ids, time_ms)
} else {
interpolate_raw_cursor(cursor, time_ms, false)
}
@@ -348,6 +350,7 @@ pub(crate) struct CursorEventOrder {
pub struct PrecomputedCursorTimeline {
timeline: Vec<SmoothedCursorEvent>,
+ cursor_ids: Vec<String>,
raw_cursor: CursorEvents,
raw_moves_are_ordered: bool,
clicks_are_ordered: bool,
@@ -372,6 +375,7 @@ impl PrecomputedCursorTimeline {
if cursor.moves.is_empty() || smoothing.is_none() {
return Self {
timeline: vec![],
+ cursor_ids: Vec::new(),
raw_cursor: cursor.clone(),
raw_moves_are_ordered,
clicks_are_ordered,
@@ -383,7 +387,7 @@ impl PrecomputedCursorTimeline {
let smoothing_config = smoothing.unwrap();
let filtered_moves = filter_cursor_shake(&cursor.moves);
let prepared_moves = decimate_cursor_moves(filtered_moves.as_ref());
- let timeline = build_smoothed_timeline(
+ let built = build_smoothed_timeline(
cursor,
prepared_moves.as_ref(),
smoothing_config,
@@ -391,7 +395,8 @@ impl PrecomputedCursorTimeline {
);
Self {
- timeline,
+ timeline: built.events,
+ cursor_ids: built.cursor_ids,
raw_cursor: CursorEvents::default(),
raw_moves_are_ordered,
clicks_are_ordered,
@@ -400,6 +405,21 @@ impl PrecomputedCursorTimeline {
}
}
+ /// Spring settings match playback and export: the base cursor, with style
+ /// overrides applied later by `ProjectUniforms::new_with_precomputed_cursor`.
+ pub fn from_project(cursor: &CursorEvents, project: &ProjectConfiguration) -> Self {
+ let smoothing = (!project.cursor.raw).then_some(SpringMassDamperSimulationConfig {
+ tension: project.cursor.tension,
+ mass: project.cursor.mass,
+ friction: project.cursor.friction,
+ });
+ Self::new(
+ cursor,
+ smoothing,
+ Some(project.cursor.click_spring_config()),
+ )
+ }
+
pub(crate) fn cached_variant(
&self,
cursor: &CursorEvents,
@@ -443,7 +463,7 @@ impl PrecomputedCursorTimeline {
pub fn interpolate(&self, time_secs: f32) -> Option<InterpolatedCursorPosition> {
let time_ms = (time_secs * 1000.0) as f64;
if self.has_smoothing {
- interpolate_timeline(&self.timeline, time_ms)
+ interpolate_timeline(&self.timeline, &self.cursor_ids, time_ms)
} else {
interpolate_raw_cursor(&self.raw_cursor, time_ms, self.raw_moves_are_ordered)
}
@@ -463,14 +483,59 @@ impl PrecomputedCursorTimeline {
}
}
+struct SmoothedTimeline {
+ events: Vec<SmoothedCursorEvent>,
+ cursor_ids: Vec<String>,
+}
+
+/// Unique cursor images for one spring build. Steps store an index so a long
+/// recording does not allocate a `String` at every 60 Hz sample.
+struct CursorIdIndex {
+ ids: Vec<String>,
+ last: Option<u32>,
+}
+
+impl CursorIdIndex {
+ fn new() -> Self {
+ Self {
+ ids: Vec::new(),
+ last: None,
+ }
+ }
+
+ fn intern(&mut self, id: &str) -> u32 {
+ if let Some(last) = self.last
+ && self.ids[last as usize] == id
+ {
+ return last;
+ }
+ if let Some(index) = self.ids.iter().position(|existing| existing == id) {
+ let index = index as u32;
+ self.last = Some(index);
+ return index;
+ }
+ let index = self.ids.len() as u32;
+ self.ids.push(id.to_owned());
+ self.last = Some(index);
+ index
+ }
+}
+
+fn cursor_id_string(cursor_ids: &[String], index: u32) -> String {
+ cursor_ids[index as usize].clone()
+}
+
fn build_smoothed_timeline(
cursor: &CursorEvents,
moves: &[CursorMoveEvent],
smoothing_config: SpringMassDamperSimulationConfig,
click_spring: Option<ClickSpringConfig>,
-) -> Vec<SmoothedCursorEvent> {
+) -> SmoothedTimeline {
if moves.is_empty() {
- return vec![];
+ return SmoothedTimeline {
+ events: vec![],
+ cursor_ids: Vec::new(),
+ };
}
let moves_are_ordered =
@@ -491,12 +556,13 @@ fn build_smoothed_timeline(
let mut events = Vec::with_capacity(capacity);
let mut target_hint: usize = 0;
let mut cid_hint: usize = 0;
+ let mut cursor_ids = CursorIdIndex::new();
events.push(SmoothedCursorEvent {
time: 0.0,
position: start_pos,
velocity: XY::new(0.0, 0.0),
- cursor_id: moves[0].cursor_id.clone(),
+ cursor_id: cursor_ids.intern(&moves[0].cursor_id),
});
let mut t_ms = SIMULATION_STEP_MS;
@@ -516,7 +582,7 @@ fn build_smoothed_timeline(
let lead_t = (clamped_t + lead_ms).min(end_time_ms);
let (cx, cy) = position_at_time_hinted(moves, lead_t, &mut target_hint);
let _ = position_at_time_hinted(moves, clamped_t, &mut cid_hint);
- let cid = cursor_id_at_time(moves, clamped_t, cid_hint).to_string();
+ let cursor_id = cursor_ids.intern(cursor_id_at_time(moves, clamped_t, cid_hint));
let target = if let Some(click) =
next_click_within(&cursor.clicks, t_ms, CLICK_LOOKAHEAD_TARGET_MS)
@@ -536,7 +602,7 @@ fn build_smoothed_timeline(
time: t_ms as f32,
position: sim.position,
velocity: sim.velocity,
- cursor_id: cid,
+ cursor_id,
});
if t_ms >= end_time_ms && sim.is_at_rest() {
@@ -546,11 +612,15 @@ fn build_smoothed_timeline(
t_ms += SIMULATION_STEP_MS;
}
- events
+ SmoothedTimeline {
+ events,
+ cursor_ids: cursor_ids.ids,
+ }
}
fn interpolate_timeline(
events: &[SmoothedCursorEvent],
+ cursor_ids: &[String],
query_ms: f64,
) -> Option<InterpolatedCursorPosition> {
if events.is_empty() {
@@ -564,7 +634,7 @@ fn interpolate_timeline(
return Some(InterpolatedCursorPosition {
position: Coord::new(XY::new(e.position.x as f64, e.position.y as f64)),
velocity: e.velocity,
- cursor_id: e.cursor_id.clone(),
+ cursor_id: cursor_id_string(cursor_ids, e.cursor_id),
});
}
@@ -573,7 +643,7 @@ fn interpolate_timeline(
return Some(InterpolatedCursorPosition {
position: Coord::new(XY::new(e.position.x as f64, e.position.y as f64)),
velocity: e.velocity,
- cursor_id: e.cursor_id.clone(),
+ cursor_id: cursor_id_string(cursor_ids, e.cursor_id),
});
}
@@ -603,7 +673,7 @@ fn interpolate_timeline(
return Some(InterpolatedCursorPosition {
position: Coord::new(XY::new(e.position.x as f64, e.position.y as f64)),
velocity: e.velocity,
- cursor_id: e.cursor_id.clone(),
+ cursor_id: cursor_id_string(cursor_ids, e.cursor_id),
});
}
}
@@ -626,7 +696,7 @@ fn interpolate_timeline(
a.velocity.x * inv + b.velocity.x * t,
a.velocity.y * inv + b.velocity.y * t,
),
- cursor_id: a.cursor_id.clone(),
+ cursor_id: cursor_id_string(cursor_ids, a.cursor_id),
})
}
@@ -732,7 +802,7 @@ struct SmoothedCursorEvent {
time: f32,
position: XY<f32>,
velocity: XY<f32>,
- cursor_id: String,
+ cursor_id: u32,
}
#[cfg(test)]
@@ -1001,12 +1071,19 @@ mod tests {
})
}
+ struct OracleCursorEvent {
+ time: f32,
+ position: XY<f32>,
+ velocity: XY<f32>,
+ cursor_id: String,
+ }
+
fn linear_build_smoothed_timeline(
cursor: &CursorEvents,
moves: &[CursorMoveEvent],
smoothing_config: SpringMassDamperSimulationConfig,
click_spring: Option<ClickSpringConfig>,
- ) -> Vec<SmoothedCursorEvent> {
+ ) -> Vec<OracleCursorEvent> {
if moves.is_empty() {
return vec![];
}
@@ -1028,7 +1105,7 @@ mod tests {
let mut target_hint: usize = 0;
let mut cid_hint: usize = 0;
- events.push(SmoothedCursorEvent {
+ events.push(OracleCursorEvent {
time: 0.0,
position: start_pos,
velocity: XY::new(0.0, 0.0),
@@ -1049,7 +1126,7 @@ mod tests {
let lead_t = (clamped_t + lead_ms).min(end_time_ms);
let (cx, cy) = position_at_time_hinted(moves, lead_t, &mut target_hint);
let _ = position_at_time_hinted(moves, clamped_t, &mut cid_hint);
- let cid = cursor_id_at_time(moves, clamped_t, cid_hint).to_string();
+ let cursor_id = cursor_id_at_time(moves, clamped_t, cid_hint).to_string();
let target = if let Some(click) =
next_click_within(&cursor.clicks, t_ms, CLICK_LOOKAHEAD_TARGET_MS)
@@ -1064,11 +1141,11 @@ mod tests {
sim.run(SIMULATION_STEP_MS as f32);
- events.push(SmoothedCursorEvent {
+ events.push(OracleCursorEvent {
time: t_ms as f32,
position: sim.position,
velocity: sim.velocity,
- cursor_id: cid,
+ cursor_id,
});
if t_ms >= end_time_ms && sim.is_at_rest() {
@@ -1081,8 +1158,8 @@ mod tests {
events
}
- fn linear_interpolate_timeline(
- events: &[SmoothedCursorEvent],
+ fn oracle_interpolate_timeline(
+ events: &[OracleCursorEvent],
query_ms: f64,
) -> Option<InterpolatedCursorPosition> {
if events.is_empty() {
@@ -1109,6 +1186,70 @@ mod tests {
});
}
+ let end = events.partition_point(|event| event.time <= query);
+ let Some(window) = end
+ .checked_sub(1)
+ .and_then(|start| events.get(start..=end))
+ .filter(|window| window[0].time <= query && query < window[1].time)
+ else {
+ let e = events.last().unwrap();
+ return Some(InterpolatedCursorPosition {
+ position: Coord::new(XY::new(e.position.x as f64, e.position.y as f64)),
+ velocity: e.velocity,
+ cursor_id: e.cursor_id.clone(),
+ });
+ };
+ let (a, b) = (&window[0], &window[1]);
+ let dt = b.time - a.time;
+ let t = if dt.abs() < 1e-6 {
+ 0.0
+ } else {
+ ((query - a.time) / dt).clamp(0.0, 1.0)
+ };
+ let inv = 1.0 - t;
+
+ Some(InterpolatedCursorPosition {
+ position: Coord::new(XY::new(
+ (a.position.x * inv + b.position.x * t) as f64,
+ (a.position.y * inv + b.position.y * t) as f64,
+ )),
+ velocity: XY::new(
+ a.velocity.x * inv + b.velocity.x * t,
+ a.velocity.y * inv + b.velocity.y * t,
+ ),
+ cursor_id: a.cursor_id.clone(),
+ })
+ }
+
+ fn linear_interpolate_timeline(
+ events: &[SmoothedCursorEvent],
+ cursor_ids: &[String],
+ query_ms: f64,
+ ) -> Option<InterpolatedCursorPosition> {
+ if events.is_empty() {
+ return None;
+ }
+
+ let query = query_ms as f32;
+
+ if query <= events[0].time {
+ let e = &events[0];
+ return Some(InterpolatedCursorPosition {
+ position: Coord::new(XY::new(e.position.x as f64, e.position.y as f64)),
+ velocity: e.velocity,
+ cursor_id: cursor_id_string(cursor_ids, e.cursor_id),
+ });
+ }
+
+ if query >= events.last().unwrap().time {
+ let e = events.last().unwrap();
+ return Some(InterpolatedCursorPosition {
+ position: Coord::new(XY::new(e.position.x as f64, e.position.y as f64)),
+ velocity: e.velocity,
+ cursor_id: cursor_id_string(cursor_ids, e.cursor_id),
+ });
+ }
+
let first_time = events[0].time;
let step = if events.len() > 1 {
events[1].time - events[0].time
@@ -1135,7 +1276,7 @@ mod tests {
return Some(InterpolatedCursorPosition {
position: Coord::new(XY::new(e.position.x as f64, e.position.y as f64)),
velocity: e.velocity,
- cursor_id: e.cursor_id.clone(),
+ cursor_id: cursor_id_string(cursor_ids, e.cursor_id),
});
}
}
@@ -1158,7 +1299,7 @@ mod tests {
a.velocity.x * inv + b.velocity.x * t,
a.velocity.y * inv + b.velocity.y * t,
),
- cursor_id: a.cursor_id.clone(),
+ cursor_id: cursor_id_string(cursor_ids, a.cursor_id),
})
}
@@ -1185,7 +1326,11 @@ mod tests {
}
}
- fn assert_timeline_bits(expected: &[SmoothedCursorEvent], actual: &[SmoothedCursorEvent]) {
+ fn assert_timeline_bits(
+ expected: &[OracleCursorEvent],
+ actual: &[SmoothedCursorEvent],
+ cursor_ids: &[String],
+ ) {
assert_eq!(expected.len(), actual.len());
for (index, (expected, actual)) in expected.iter().zip(actual).enumerate() {
assert_eq!(
@@ -1213,16 +1358,23 @@ mod tests {
actual.velocity.y.to_bits(),
"velocity.y at {index}"
);
- assert_eq!(expected.cursor_id, actual.cursor_id, "cursor_id at {index}");
+ assert_eq!(
+ expected.cursor_id, cursor_ids[actual.cursor_id as usize],
+ "cursor_id at {index}"
+ );
}
}
+ fn sample_cursor_ids() -> Vec<String> {
+ vec!["0".into(), "1".into(), "2".into()]
+ }
+
fn sampled_event(time: f32, index: usize) -> SmoothedCursorEvent {
SmoothedCursorEvent {
time,
position: XY::new(index as f32 * 0.013, index as f32 * -0.007),
velocity: XY::new(index as f32 * -0.002, index as f32 * 0.003),
- cursor_id: (index % 3).to_string(),
+ cursor_id: (index % 3) as u32,
}
}
@@ -1265,7 +1417,9 @@ mod tests {
assert!(timeline.has_smoothing);
assert!(timeline.raw_cursor.moves.is_empty());
assert!(timeline.raw_cursor.clicks.is_empty());
- assert_timeline_bits(&expected, &timeline.timeline);
+ assert!(timeline.cursor_ids.len() <= 3);
+ assert!(timeline.timeline.len() > timeline.cursor_ids.len());
+ assert_timeline_bits(&expected, &timeline.timeline, &timeline.cursor_ids);
drop(moves);
drop(filtered);
drop(cursor);
@@ -1273,7 +1427,7 @@ mod tests {
for index in 0..=500 {
let time_secs = index as f32 * 0.017_137;
assert_position_bits(
- linear_interpolate_timeline(&expected, (time_secs * 1000.0) as f64),
+ oracle_interpolate_timeline(&expected, (time_secs * 1000.0) as f64),
timeline.interpolate(time_secs),
);
}
@@ -1676,12 +1830,12 @@ mod tests {
let expected =
linear_build_smoothed_timeline(&cursor, &moves, config, click_spring);
let actual = build_smoothed_timeline(&cursor, &moves, config, click_spring);
- assert_timeline_bits(&expected, &actual);
+ assert_timeline_bits(&expected, &actual.events, &actual.cursor_ids);
for index in 0..=500 {
let query = index as f64 * 17.137;
assert_position_bits(
- linear_interpolate_timeline(&expected, query),
- interpolate_timeline(&actual, query),
+ oracle_interpolate_timeline(&expected, query),
+ interpolate_timeline(&actual.events, &actual.cursor_ids, query),
);
}
}
@@ -1692,9 +1846,10 @@ mod tests {
#[test]
fn timeline_lookup_matches_linear_reference_at_long_times_and_duplicate_timestamps() {
assert_position_bits(
- linear_interpolate_timeline(&[], 0.0),
- interpolate_timeline(&[], 0.0),
+ linear_interpolate_timeline(&[], &[], 0.0),
+ interpolate_timeline(&[], &[], 0.0),
);
+ let cursor_ids = sample_cursor_ids();
for duration_hours in [0.0, 1.0, 20.0] {
for interval_ms in [1.0, SIMULATION_STEP_MS] {
let origin_ms = duration_hours * 3_600_000.0;
@@ -1717,8 +1872,8 @@ mod tests {
origin_ms + 10_000.0,
] {
assert_position_bits(
- linear_interpolate_timeline(&events, query),
- interpolate_timeline(&events, query),
+ linear_interpolate_timeline(&events, &cursor_ids, query),
+ interpolate_timeline(&events, &cursor_ids, query),
);
}
for event in &events {
@@ -1729,8 +1884,8 @@ mod tests {
event.time as f64 + interval_ms * 0.5,
] {
assert_position_bits(
- linear_interpolate_timeline(&events, query),
- interpolate_timeline(&events, query),
+ linear_interpolate_timeline(&events, &cursor_ids, query),
+ interpolate_timeline(&events, &cursor_ids, query),
);
}
}
@@ -1743,8 +1898,8 @@ mod tests {
.collect();
for query in [-0.0, 0.0, 0.01, 8.0, 16.0, 16.01, 24.0, 32.0] {
assert_position_bits(
- linear_interpolate_timeline(&duplicate_origin, query),
- interpolate_timeline(&duplicate_origin, query),
+ linear_interpolate_timeline(&duplicate_origin, &cursor_ids, query),
+ interpolate_timeline(&duplicate_origin, &cursor_ids, query),
);
}
}
@@ -1765,12 +1920,16 @@ mod tests {
let started = Instant::now();
let actual = build_smoothed_timeline(&cursor, &moves, DEFAULT_CLICK_SPRING, None);
let optimized_time = started.elapsed();
- assert_timeline_bits(black_box(&expected), black_box(&actual));
+ assert_timeline_bits(
+ black_box(&expected),
+ black_box(&actual.events),
+ black_box(&actual.cursor_ids),
+ );
println!(
"cursor_precompute duration_secs={duration_secs} moves={} clicks={} samples={} baseline_ms={:.3} optimized_ms={:.3}",
moves.len(),
cursor.clicks.len(),
- actual.len(),
+ actual.events.len(),
baseline_time.as_secs_f64() * 1000.0,
optimized_time.as_secs_f64() * 1000.0
);
@@ -1779,23 +1938,26 @@ mod tests {
let events: Vec<_> = (0..=20 * 60 * 60 * 60)
.map(|index| {
let mut event = sampled_event((index as f64 * SIMULATION_STEP_MS) as f32, index);
- event.cursor_id = String::new();
+ event.cursor_id = 0;
event
})
.collect();
+ let cursor_ids = [String::new()];
let queries: Vec<_> = (0..128)
.map(|index| 72_000_000.0 - index as f64 * 171.13 - 1.0)
.collect();
let started = Instant::now();
let expected: Vec<_> = queries
.iter()
- .map(|&query| linear_interpolate_timeline(black_box(&events), black_box(query)))
+ .map(|&query| {
+ linear_interpolate_timeline(black_box(&events), &cursor_ids, black_box(query))
+ })
.collect();
let baseline_time = started.elapsed();
let started = Instant::now();
let actual: Vec<_> = queries
.iter()
- .map(|&query| interpolate_timeline(black_box(&events), black_box(query)))
+ .map(|&query| interpolate_timeline(black_box(&events), &cursor_ids, black_box(query)))
.collect();
let optimized_time = started.elapsed();
for (expected, actual) in expected.into_iter().zip(actual) {
diff --git a/crates/rendering/src/decoder/ffmpeg.rs b/crates/rendering/src/decoder/ffmpeg.rs
index 8be75965e..3c1fdb5da 100644
--- a/crates/rendering/src/decoder/ffmpeg.rs
+++ b/crates/rendering/src/decoder/ffmpeg.rs
@@ -8,7 +8,7 @@ use std::{
collections::BTreeMap,
path::PathBuf,
rc::Rc,
- sync::{Arc, mpsc},
+ sync::{Arc, OnceLock, mpsc},
};
use tokio::sync::oneshot;
use tracing::info;
@@ -21,8 +21,9 @@ use crate::cpu_yuv::YuvMatrix;
use super::{
DecoderInitResult, DecoderType, FRAME_CACHE_SIZE, ManagedVideoControl, ManagedVideoError,
- ManagedVideoInput, VideoDecoderMessage, frame_converter::FrameConverter, pts_to_frame,
- record_pts_hole,
+ ManagedVideoInput, VideoDecoderMessage,
+ frame_converter::{FrameConverter, PlanePool},
+ pts_to_frame, record_pts_hole,
};
#[cfg(test)]
@@ -38,10 +39,31 @@ struct ProcessedFrame {
matrix: YuvMatrix,
y_stride: u32,
uv_stride: u32,
+ planes: Arc<PlanePool>,
#[cfg(target_os = "linux")]
cuda: Option<Arc<crate::linux_gpu::CudaNv12Frame>>,
}
+fn empty_plane_slot() -> Arc<Vec<u8>> {
+ static EMPTY: OnceLock<Arc<Vec<u8>>> = OnceLock::new();
+ EMPTY.get_or_init(|| Arc::new(Vec::new())).clone()
+}
+
+impl Drop for ProcessedFrame {
+ fn drop(&mut self) {
+ // The cache and the renderer share this allocation. Recycle it only
+ // once both have released the frame; otherwise the next decode would
+ // write into a buffer the renderer still uploads.
+ if Arc::strong_count(&self.data) != 1 {
+ return;
+ }
+ let data = std::mem::replace(&mut self.data, empty_plane_slot());
+ if let Ok(buf) = Arc::try_unwrap(data) {
+ self.planes.recycle(buf);
+ }
+ }
+}
+
impl ProcessedFrame {
fn to_decoded_frame(&self) -> DecodedFrame {
#[cfg(target_os = "linux")]
@@ -89,7 +111,10 @@ const MAX_FRAME_CACHE_BYTES: usize = 128 * 1024 * 1024;
#[cfg(target_os = "linux")]
const CUDA_FRAME_CACHE_WEIGHT: usize = 16 * 1024 * 1024;
-fn extract_yuv_planes(frame: &frame::Video) -> Option<(Vec<u8>, PixelFormat, u32, u32)> {
+fn extract_yuv_planes(
+ frame: &frame::Video,
+ planes: &PlanePool,
+) -> Option<(Vec<u8>, PixelFormat, u32, u32)> {
let height = frame.height();
match frame.format() {
@@ -97,52 +122,71 @@ fn extract_yuv_planes(frame: &frame::Video) -> Option<(Vec<u8>, PixelFormat, u32
let y_stride = frame.stride(0) as u32;
let u_stride = frame.stride(1) as u32;
let v_stride = frame.stride(2) as u32;
-
- let y_size = (y_stride * height) as usize;
let uv_height = frame.plane_height(1);
+ let y_size = (y_stride * height) as usize;
let u_size = (u_stride * uv_height) as usize;
-
- let mut data = Vec::with_capacity(y_size + 2 * u_size);
- data.extend_from_slice(&frame.data(0)[..y_size]);
- data.extend_from_slice(&frame.data(1)[..u_size]);
-
- // Downstream consumers index both chroma planes with the single
- // advertised uv_stride (the U stride). FFmpeg almost always gives
- // U and V identical linesizes, but if they ever differ, repack the
- // V rows to the U stride — otherwise every V row shifts and the
- // whole image takes on a uniform wrong-chroma cast.
- if v_stride == u_stride {
- data.extend_from_slice(&frame.data(2)[..u_size]);
- } else {
- let row_bytes = u_stride.min(v_stride) as usize;
- let pad = u_stride as usize - row_bytes;
- let v_data = frame.data(2);
- for row in 0..uv_height as usize {
- let start = row * v_stride as usize;
- data.extend_from_slice(&v_data[start..start + row_bytes]);
- data.resize(data.len() + pad, 128);
- }
- }
-
+ let data = pack_yuv420p(
+ planes,
+ &frame.data(0)[..y_size],
+ &frame.data(1)[..u_size],
+ frame.data(2),
+ u_stride,
+ v_stride,
+ uv_height,
+ );
Some((data, PixelFormat::Yuv420p, y_stride, u_stride))
}
format::Pixel::NV12 => {
let y_stride = frame.stride(0) as u32;
let uv_stride = frame.stride(1) as u32;
-
let y_size = (y_stride * height) as usize;
let uv_size = (uv_stride * frame.plane_height(1)) as usize;
-
- let mut data = Vec::with_capacity(y_size + uv_size);
- data.extend_from_slice(&frame.data(0)[..y_size]);
- data.extend_from_slice(&frame.data(1)[..uv_size]);
-
+ let data = pack_nv12(planes, &frame.data(0)[..y_size], &frame.data(1)[..uv_size]);
Some((data, PixelFormat::Nv12, y_stride, uv_stride))
}
_ => None,
}
}
+fn pack_nv12(planes: &PlanePool, y: &[u8], uv: &[u8]) -> Vec<u8> {
+ let mut data = planes.take(y.len() + uv.len());
+ data.extend_from_slice(y);
+ data.extend_from_slice(uv);
+ data
+}
+
+fn pack_yuv420p(
+ planes: &PlanePool,
+ y: &[u8],
+ u: &[u8],
+ v: &[u8],
+ u_stride: u32,
+ v_stride: u32,
+ uv_height: u32,
+) -> Vec<u8> {
+ let mut data = planes.take(y.len() + u.len() + u.len());
+ data.extend_from_slice(y);
+ data.extend_from_slice(u);
+ // Downstream consumers index both chroma planes with the single
+ // advertised uv_stride (the U stride). FFmpeg almost always gives
+ // U and V identical linesizes, but if they ever differ, repack the
+ // V rows to the U stride — otherwise every V row shifts and the
+ // whole image takes on a uniform wrong-chroma cast.
+ if v_stride == u_stride {
+ data.extend_from_slice(&v[..u.len()]);
+ } else {
+ let u_stride = u_stride as usize;
+ let v_stride = v_stride as usize;
+ let row_bytes = u_stride.min(v_stride);
+ let pad = u_stride - row_bytes;
+ for row in v.chunks(v_stride).take(uv_height as usize) {
+ data.extend_from_slice(&row[..row_bytes]);
+ data.resize(data.len() + pad, 128);
+ }
+ }
+ data
+}
+
impl CachedFrame {
fn process_cpu(&mut self, converter: &mut FrameConverter) -> ProcessedFrame {
match self {
@@ -173,14 +217,16 @@ impl CachedFrame {
matrix: YuvMatrix::from_color_space(frame.color_space()),
y_stride: cuda.y_pitch as u32,
uv_stride: cuda.uv_pitch as u32,
+ planes: Arc::clone(converter.plane_pool()),
cuda: Some(Arc::new(cuda)),
}
};
*self = Self::Processed(data.clone());
return data;
}
+ let planes = Arc::clone(converter.plane_pool());
let data = if let Some((yuv_data, pixel_format, y_stride, uv_stride)) =
- extract_yuv_planes(frame)
+ extract_yuv_planes(frame, &planes)
{
ProcessedFrame {
data: Arc::new(yuv_data),
@@ -191,6 +237,7 @@ impl CachedFrame {
matrix: YuvMatrix::from_color_space(frame.color_space()),
y_stride,
uv_stride,
+ planes,
#[cfg(target_os = "linux")]
cuda: None,
}
@@ -205,6 +252,7 @@ impl CachedFrame {
matrix: YuvMatrix::from_color_space(frame.color_space()),
y_stride: frame.width() * 4,
uv_stride: 0,
+ planes,
#[cfg(target_os = "linux")]
cuda: None,
}
@@ -747,7 +795,7 @@ impl FfmpegDecoder {
if let Some(cached) = sw_cache.get_mut(&requested_frame) {
let data = cached.produce(&mut sw_converter);
if reply.send(data.frame.clone()).is_err() {
- log::warn!(
+ log::debug!(
"FFmpeg '{name}': Failed to send cached frame {requested_frame}: receiver dropped"
);
}
@@ -818,7 +866,7 @@ impl FfmpegDecoder {
if let Some(reply) = reply_for_respond.borrow_mut().take()
&& reply.send(data.frame).is_err()
{
- log::warn!(
+ log::debug!(
"Failed to send decoded frame {frame_number}: receiver dropped"
);
}
@@ -1312,7 +1360,7 @@ impl FfmpegDecoder {
let data = cached.produce(&mut converter);
if reply.send(data.frame.clone()).is_err() {
- log::warn!(
+ log::debug!(
"FFmpeg '{name}': Failed to send cached frame {requested_frame}: receiver dropped"
);
}
@@ -1389,7 +1437,7 @@ impl FfmpegDecoder {
if let Some(reply) = reply_for_respond.borrow_mut().take()
&& reply.send(data.frame).is_err()
{
- log::warn!(
+ log::debug!(
"Failed to send decoded frame {frame_number}: receiver dropped"
);
}
@@ -1715,6 +1763,7 @@ mod cache_tests {
matrix: YuvMatrix::Bt601,
y_stride: 2,
uv_stride: 2,
+ planes: Arc::new(PlanePool::new()),
#[cfg(target_os = "linux")]
cuda: None,
})
@@ -1765,6 +1814,193 @@ mod cache_tests {
assert_eq!(cache.keys().copied().collect::<Vec<_>>(), vec![2]);
assert_eq!(cache[&2].estimated_bytes(), 12);
}
+
+ #[test]
+ fn mismatched_v_stride_is_repacked_to_the_u_stride() {
+ let pool = PlanePool::new();
+ let y = vec![4u8; 8];
+ let u = vec![9u8; 8];
+ let wide_v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
+ let packed = pack_yuv420p(&pool, &y, &u, &wide_v, 4, 6, 2);
+ assert_eq!(
+ packed,
+ [
+ 4, 4, 4, 4, 4, 4, 4, 4, 9, 9, 9, 9, 9, 9, 9, 9, 1, 2, 3, 4, 7, 8, 9, 10,
+ ]
+ );
+
+ let short_v = vec![1, 2, 3, 4];
+ let packed = pack_yuv420p(&pool, &y, &u, &short_v, 4, 2, 2);
+ assert_eq!(
+ &packed[y.len() + u.len()..],
+ &[1, 2, 128, 128, 3, 4, 128, 128]
+ );
+
+ let v = [7, 8, 9];
+ let packed = pack_yuv420p(&pool, &[1, 2, 3, 4], &[5, 6], &v, 2, 2, 1);
+ assert_eq!(packed, [1, 2, 3, 4, 5, 6, 7, 8]);
+ }
+
+ #[test]
+ fn released_software_frame_reuses_its_plane_buffer() {
+ for pixel_format in [
+ format::Pixel::YUV420P,
+ format::Pixel::NV12,
+ format::Pixel::RGBA,
+ ] {
+ let mut converter = FrameConverter::new();
+ let (cached, output) = decoded_packed(&mut converter, pixel_format, 1);
+ let ptr = output.frame.data().as_ptr() as usize;
+ assert_eq!(output.frame.data()[0], 1);
+ drop(output);
+ drop(cached);
+ let (_cached, output) = decoded_packed(&mut converter, pixel_format, 2);
+ assert_eq!(
+ output.frame.data().as_ptr() as usize,
+ ptr,
+ "{pixel_format:?}"
+ );
+ assert_eq!(output.frame.data()[0], 2);
+ }
+ }
+
+ #[test]
+ fn renderer_held_frame_is_not_recycled() {
+ let mut converter = FrameConverter::new();
+ let (cached, output) = decoded_packed(&mut converter, format::Pixel::YUV420P, 7);
+ let ptr = output.frame.data().as_ptr() as usize;
+ let y = output.frame.y_plane().unwrap().to_vec();
+ drop(cached);
+ let (_cached, next) = decoded_packed(&mut converter, format::Pixel::YUV420P, 8);
+ assert_ne!(next.frame.data().as_ptr() as usize, ptr);
+ assert_eq!(output.frame.y_plane().unwrap(), y.as_slice());
+ assert_eq!(output.frame.data()[0], 7);
+ }
+
+ #[test]
+ fn releasing_the_cache_keeps_two_spare_planes() {
+ let mut converter = FrameConverter::new();
+ let mut held = Vec::new();
+ for fill in 1..7u8 {
+ held.push(decoded_packed(&mut converter, format::Pixel::YUV420P, fill));
+ }
+ for (cached, output) in held {
+ drop(output);
+ drop(cached);
+ }
+ assert_eq!(converter.plane_pool().spare_count(), 2);
+ }
+
+ #[test]
+ fn pooled_1080p_yuv_copy_reuses_one_allocation() {
+ let pool = PlanePool::new();
+ let width = 1920usize;
+ let height = 1080usize;
+ let uv_height = height / 2;
+ let u_stride = width / 2;
+ let y = vec![16u8; width * height];
+ let u = vec![128u8; u_stride * uv_height];
+ let v = vec![90u8; u_stride * uv_height];
+ let total = y.len() + u.len() + v.len();
+ let u_stride = u_stride as u32;
+ let uv_height = uv_height as u32;
+
+ let warm = pack_yuv420p(&pool, &y, &u, &v, u_stride, u_stride, uv_height);
+ assert_eq!(warm.len(), total);
+ assert_eq!(warm[0], 16);
+ assert_eq!(warm[y.len()], 128);
+ assert_eq!(warm[y.len() + u.len()], 90);
+ let ptr = warm.as_ptr() as usize;
+ pool.recycle(warm);
+
+ let reused = pack_yuv420p(&pool, &y, &u, &v, u_stride, u_stride, uv_height);
+ assert_eq!(reused.as_ptr() as usize, ptr);
+ assert_eq!(reused.len(), total);
+ pool.recycle(reused);
+
+ let iters = 40;
+ let fresh = time_fresh_copies(&y, &u, &v, iters);
+ let pooled = time_pooled_copies(&pool, &y, &u, &v, u_stride, uv_height, iters);
+ let fresh_again = time_fresh_copies(&y, &u, &v, iters);
+ let fresh_alloc = time_fresh_alloc(total, iters);
+ let pooled_alloc = time_pooled_alloc(&pool, total, iters);
+ eprintln!(
+ "yuv420p 1920x1080 bytes={total} iters={iters} fresh_copy_us={} pooled_copy_us={} fresh_copy_again_us={} fresh_alloc_us={} pooled_alloc_us={}",
+ fresh.as_micros(),
+ pooled.as_micros(),
+ fresh_again.as_micros(),
+ fresh_alloc.as_micros(),
+ pooled_alloc.as_micros()
+ );
+ }
+
+ fn decoded_packed(
+ converter: &mut FrameConverter,
+ pixel_format: format::Pixel,
+ fill: u8,
+ ) -> (CachedFrame, OutputFrame) {
+ let mut source = frame::Video::new(pixel_format, 32, 16);
+ for plane in 0..source.planes() {
+ source.data_mut(plane).fill(fill.wrapping_add(plane as u8));
+ }
+ let mut cached = CachedFrame::Raw {
+ frame: source,
+ number: u32::from(fill),
+ };
+ let output = cached.produce(converter);
+ (cached, output)
+ }
+
+ fn time_fresh_copies(y: &[u8], u: &[u8], v: &[u8], iters: u32) -> std::time::Duration {
+ let start = std::time::Instant::now();
+ for _ in 0..iters {
+ let mut buf = Vec::with_capacity(y.len() + u.len() + v.len());
+ buf.extend_from_slice(y);
+ buf.extend_from_slice(u);
+ buf.extend_from_slice(v);
+ std::hint::black_box(buf.as_ptr());
+ }
+ start.elapsed()
+ }
+
+ fn time_pooled_copies(
+ pool: &PlanePool,
+ y: &[u8],
+ u: &[u8],
+ v: &[u8],
+ u_stride: u32,
+ uv_height: u32,
+ iters: u32,
+ ) -> std::time::Duration {
+ let start = std::time::Instant::now();
+ for _ in 0..iters {
+ let buf = pack_yuv420p(pool, y, u, v, u_stride, u_stride, uv_height);
+ std::hint::black_box(buf.as_ptr());
+ pool.recycle(buf);
+ }
+ start.elapsed()
+ }
+
+ fn time_fresh_alloc(total: usize, iters: u32) -> std::time::Duration {
+ let start = std::time::Instant::now();
+ for _ in 0..iters {
+ let buf = Vec::<u8>::with_capacity(total);
+ std::hint::black_box(buf.capacity());
+ }
+ start.elapsed()
+ }
+
+ fn time_pooled_alloc(pool: &PlanePool, total: usize, iters: u32) -> std::time::Duration {
+ let warm = pool.take(total);
+ pool.recycle(warm);
+ let start = std::time::Instant::now();
+ for _ in 0..iters {
+ let buf = pool.take(total);
+ std::hint::black_box(buf.capacity());
+ pool.recycle(buf);
+ }
+ start.elapsed()
+ }
}
// pub fn find_decoder(
diff --git a/crates/rendering/src/decoder/frame_converter.rs b/crates/rendering/src/decoder/frame_converter.rs
index 1a156314a..7c9e66c02 100644
--- a/crates/rendering/src/decoder/frame_converter.rs
+++ b/crates/rendering/src/decoder/frame_converter.rs
@@ -1,12 +1,86 @@
#![allow(dead_code)]
+use std::sync::{Arc, Mutex, PoisonError};
+
use ffmpeg::{format, frame, software};
use crate::cpu_yuv::YuvMatrix;
+/// Spare packed YUV and RGBA planes for one software decoder.
+///
+/// A seek drops the whole cache at once; the cap keeps that from retaining
+/// every frame's buffer.
+pub(crate) struct PlanePool {
+ spare: Mutex<Vec<Vec<u8>>>,
+}
+
+const MAX_SPARE_PACKED_PLANES: usize = 2;
+
+impl PlanePool {
+ pub(crate) fn new() -> Self {
+ Self {
+ spare: Mutex::new(Vec::new()),
+ }
+ }
+
+ pub(crate) fn take(&self, min_capacity: usize) -> Vec<u8> {
+ if min_capacity == 0 {
+ return Vec::new();
+ }
+ let mut spare = self.spare.lock().unwrap_or_else(PoisonError::into_inner);
+ let index = spare
+ .iter()
+ .enumerate()
+ .filter(|(_, buf)| buf.capacity() >= min_capacity)
+ .min_by_key(|(_, buf)| buf.capacity())
+ .map(|(index, _)| index);
+ if let Some(index) = index {
+ let mut buf = spare.swap_remove(index);
+ buf.clear();
+ buf
+ } else {
+ drop(spare);
+ Vec::with_capacity(min_capacity)
+ }
+ }
+
+ pub(crate) fn recycle(&self, mut buf: Vec<u8>) {
+ if buf.capacity() == 0 {
+ return;
+ }
+ buf.clear();
+ let mut spare = self.spare.lock().unwrap_or_else(PoisonError::into_inner);
+ if spare.len() < MAX_SPARE_PACKED_PLANES {
+ spare.push(buf);
+ return;
+ }
+ let Some(index) = spare
+ .iter()
+ .enumerate()
+ .min_by_key(|(_, existing)| existing.capacity())
+ .map(|(index, _)| index)
+ else {
+ return;
+ };
+ if spare[index].capacity() < buf.capacity() {
+ spare.swap_remove(index);
+ spare.push(buf);
+ }
+ }
+
+ #[cfg(test)]
+ pub(crate) fn spare_count(&self) -> usize {
+ self.spare
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .len()
+ }
+}
+
pub struct FrameConverter {
scaler: Option<ScalerState>,
rgba_frame: frame::Video,
+ planes: Arc<PlanePool>,
}
struct ScalerState {
@@ -22,16 +96,21 @@ impl FrameConverter {
Self {
scaler: None,
rgba_frame: frame::Video::empty(),
+ planes: Arc::new(PlanePool::new()),
}
}
+ pub(crate) fn plane_pool(&self) -> &Arc<PlanePool> {
+ &self.planes
+ }
+
pub fn convert(&mut self, frame: &mut frame::Video) -> Vec<u8> {
if frame.format() == format::Pixel::RGBA {
let width = frame.width() as usize;
let height = frame.height() as usize;
let stride = frame.stride(0);
- return copy_rgba_plane(frame.data(0), stride, width, height);
+ return copy_rgba_plane_pooled(&self.planes, frame.data(0), stride, width, height);
}
let width = frame.width();
@@ -56,7 +135,8 @@ impl FrameConverter {
}
let rgba_frame = &self.rgba_frame;
- copy_rgba_plane(
+ copy_rgba_plane_pooled(
+ &self.planes,
rgba_frame.data(0),
rgba_frame.stride(0),
rgba_frame.width() as usize,
@@ -139,20 +219,44 @@ fn use_bt709_source(context: &mut software::scaling::Context) {
}
pub fn copy_rgba_plane(data: &[u8], stride: usize, width: usize, height: usize) -> Vec<u8> {
+ let mut frame_buffer = Vec::with_capacity(width * 4 * height);
+ fill_rgba_plane(&mut frame_buffer, data, stride, width, height);
+ frame_buffer
+}
+
+fn copy_rgba_plane_pooled(
+ pool: &PlanePool,
+ data: &[u8],
+ stride: usize,
+ width: usize,
+ height: usize,
+) -> Vec<u8> {
+ let mut frame_buffer = pool.take(width * 4 * height);
+ fill_rgba_plane(&mut frame_buffer, data, stride, width, height);
+ frame_buffer
+}
+
+fn fill_rgba_plane(
+ frame_buffer: &mut Vec<u8>,
+ data: &[u8],
+ stride: usize,
+ width: usize,
+ height: usize,
+) {
debug_assert!(stride >= width * 4, "stride too small for RGBA frame");
let row_len = width * 4;
let total = row_len * height;
+ frame_buffer.clear();
if stride == row_len && data.len() >= total {
- return data[..total].to_vec();
+ frame_buffer.extend_from_slice(&data[..total]);
+ return;
}
- let mut frame_buffer = Vec::with_capacity(total);
for row in data.chunks(stride).take(height) {
frame_buffer.extend_from_slice(&row[..row_len]);
}
- frame_buffer
}
#[cfg(target_os = "macos")]
@@ -219,3 +323,56 @@ pub fn copy_bgra_to_rgba(data: &[u8], stride: usize, width: usize, height: usize
frame_buffer
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn rgba_copy_drops_stride_padding() {
+ let mut data = vec![0u8; 24];
+ data[..8].fill(1);
+ data[12..20].fill(2);
+ let out = copy_rgba_plane(&data, 12, 2, 2);
+ assert_eq!(out.len(), 16);
+ assert_eq!(&out[..8], &[1; 8]);
+ assert_eq!(&out[8..], &[2; 8]);
+ }
+
+ #[test]
+ fn pooled_rgba_copy_reuses_the_released_buffer() {
+ let pool = PlanePool::new();
+ let pixels = [1u8, 2, 3, 4, 5, 6, 7, 8];
+ let first = copy_rgba_plane_pooled(&pool, &pixels, 8, 2, 1);
+ let ptr = first.as_ptr();
+ let capacity = first.capacity();
+ pool.recycle(first);
+ let second = copy_rgba_plane_pooled(&pool, &pixels, 8, 2, 1);
+ assert_eq!(second.as_ptr(), ptr);
+ assert_eq!(second.capacity(), capacity);
+ assert_eq!(second, pixels);
+ }
+
+ #[test]
+ fn live_rgba_buffer_is_not_handed_out_again() {
+ let pool = PlanePool::new();
+ let pixels = [9u8; 8];
+ let first = copy_rgba_plane_pooled(&pool, &pixels, 8, 2, 1);
+ let ptr = first.as_ptr();
+ let second = copy_rgba_plane_pooled(&pool, &pixels, 8, 2, 1);
+ assert_ne!(second.as_ptr(), ptr);
+ assert_eq!(first, pixels);
+ }
+
+ #[test]
+ fn spare_planes_are_capped_and_prefer_a_fit() {
+ let pool = PlanePool::new();
+ for size in [32, 64, 128, 256] {
+ pool.recycle(Vec::with_capacity(size));
+ }
+ assert_eq!(pool.spare_count(), MAX_SPARE_PACKED_PLANES);
+ let taken = pool.take(200);
+ assert!(taken.capacity() >= 200);
+ assert_eq!(pool.spare_count(), 1);
+ }
+}
diff --git a/crates/rendering/src/layers/captions.rs b/crates/rendering/src/layers/captions.rs
index 988988060..c26e715b3 100644
--- a/crates/rendering/src/layers/captions.rs
+++ b/crates/rendering/src/layers/captions.rs
@@ -101,13 +101,47 @@ pub struct CaptionOverlayLayout {
pub position: CaptionPosition,
}
+/// The centre of the caption box last drawn, as fractions of the frame, with
+/// the position setting it was drawn for. Left and right presets align the
+/// box's edge to the margin, so where its centre lands depends on the box
+/// width, which only this layer measures. The editor starts Manual placement
+/// here so switching to it leaves the captions where they were.
+static LAST_CAPTION_CENTER: std::sync::Mutex<Option<(CaptionPosition, [f32; 2])>> =
+ std::sync::Mutex::new(None);
+
+/// [`LAST_CAPTION_CENTER`] when the last caption was drawn at `position`.
+pub fn last_caption_center(position: &str) -> Option<(f32, f32)> {
+ let wanted = CaptionPosition::from_str(position);
+ let last = *LAST_CAPTION_CENTER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ last.and_then(|(drawn, [x, y])| (drawn == wanted).then_some((x, y)))
+}
+
+fn caption_center(rect: [f32; 4], frame: (u32, u32)) -> Option<[f32; 2]> {
+ (frame.0 > 0 && frame.1 > 0).then(|| {
+ [
+ ((rect[0] + rect[2] / 2.0) / frame.0 as f32).clamp(0.0, 1.0),
+ ((rect[1] + rect[3] / 2.0) / frame.1 as f32).clamp(0.0, 1.0),
+ ]
+ })
+}
+
+fn record_caption_center(position: CaptionPosition, rect: [f32; 4], frame: (u32, u32)) {
+ if let Some(center) = caption_center(rect, frame) {
+ *LAST_CAPTION_CENTER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Some((position, center));
+ }
+}
+
const BASE_TEXT_OPACITY: f32 = 0.8;
const BOUNCE_OFFSET_PIXELS: f32 = 8.0;
// Safety net for caption segments whose trailing word end was stretched across a
// silence by transcription (e.g. a 16s "seconds."). Without this, such a segment
-// stays on screen for the whole inflated duration. Kept in sync with
-// MAX_CAPTION_WORD_DURATION in the desktop transcription/projection layers.
-const MAX_CAPTION_WORD_DURATION: f64 = 2.5;
+// stays on screen for the whole inflated duration. Value comes from the shared
+// caption word utilities so it stays in sync with the transcription layers.
+const MAX_CAPTION_WORD_DURATION: f64 = cap_project::caption_words::MAX_CAPTION_WORD_DURATION as f64;
fn ease_out_cubic(t: f32) -> f32 {
let t = t.clamp(0.0, 1.0);
@@ -980,6 +1014,7 @@ impl CaptionsLayer {
];
self.active_layout = Some(CaptionOverlayLayout { rect, position });
+ record_caption_center(position, rect, (width, height));
let background_uniforms = CaptionBackgroundUniforms {
rect,
@@ -1223,8 +1258,8 @@ fn calculate_caption_bounce(current_time: f64, start: f64, end: f64, fade_durati
#[cfg(test)]
mod tests {
use super::{
- caption_segment_effective_end, find_active_caption_segment, find_active_word_index,
- word_byte_range,
+ caption_center, caption_segment_effective_end, find_active_caption_segment,
+ find_active_word_index, word_byte_range,
};
use cap_project::{CaptionTrackSegment, CaptionWord};
@@ -1252,6 +1287,20 @@ mod tests {
}
}
+ /// A 400 px box against the 96 px left margin of a 1920 px frame is
+ /// centred at 296 px, not at the 5% the preset table names.
+ #[test]
+ fn the_drawn_caption_centre_is_a_fraction_of_the_frame() {
+ let center = caption_center([96.0, 900.0, 400.0, 80.0], (1920, 1080)).unwrap();
+ assert!((center[0] - 296.0 / 1920.0).abs() < 1e-6);
+ assert!((center[1] - 940.0 / 1080.0).abs() < 1e-6);
+ assert!(caption_center([0.0, 0.0, 10.0, 10.0], (0, 1080)).is_none());
+ assert_eq!(
+ caption_center([1900.0, 0.0, 400.0, 10.0], (1920, 1080)).map(|c| c[0]),
+ Some(1.0)
+ );
+ }
+
#[test]
fn effective_end_clamps_inflated_trailing_word() {
let seg = segment(36.1, 42.31, vec![word(36.1, 42.31)]);
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index 56a1020e3..da01e6295 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -75,7 +75,9 @@ pub use frame_pipeline::{GpuOutputFormat, Nv12RenderedFrame, RenderedFrame, Shar
#[cfg(target_os = "macos")]
pub use frame_pipeline::{PendingSurface, RgbaToBgraSurfaceConverter};
pub use frame_windows::FrameWindows;
-pub use layers::{BackgroundTextureCache, clean_background_path, new_font_system};
+pub use layers::{
+ BackgroundTextureCache, clean_background_path, last_caption_center, new_font_system,
+};
pub use managed_segment::{
ManagedRecordingSegmentDecoders, ManagedSegmentDecoderStatus, ManagedSegmentStopHandles,
ManagedSegmentVideoError, ManagedSegmentVideoExit, ManagedSegmentVideoInput,
@@ -2593,7 +2595,7 @@ pub async fn create_shared_device() -> Result<SharedWgpuDevice, RenderingError>
}
let adapter_phase = readiness::Phase::start("wgpu.adapter");
- let hardware_adapter = if force_software_adapter {
+ let preferred = if force_software_adapter {
None
} else {
instance
@@ -2605,55 +2607,118 @@ pub async fn create_shared_device() -> Result<SharedWgpuDevice, RenderingError>
.await
.ok()
};
+ adapter_phase.finish(if preferred.is_some() {
+ "preferred"
+ } else {
+ "none"
+ });
- let (adapter, is_software_adapter) = if let Some(adapter) = hardware_adapter {
- let adapter_info = adapter.get_info();
- let is_software = is_software_wgpu_adapter(&adapter_info);
-
- if is_software {
- tracing::warn!(
- adapter_name = adapter_info.name,
- adapter_backend = ?adapter_info.backend,
- adapter_device_type = ?adapter_info.device_type,
- "Hardware adapter behaves like a software renderer"
- );
- } else {
- tracing::info!(
- adapter_name = adapter_info.name,
- adapter_backend = ?adapter_info.backend,
- adapter_device_type = ?adapter_info.device_type,
- "Using hardware GPU adapter"
- );
+ let device_phase = readiness::Phase::start("wgpu.device");
+ let mut last_error = None;
+ let mut created = None;
+ if let Some(adapter) = preferred {
+ match try_rendering_device(adapter).await {
+ Ok(found) => created = Some(found),
+ Err(error) => last_error = Some(error),
+ }
+ }
+ // A driver can list an adapter it cannot make a device on (a GL context
+ // without compute shaders, a broken install). Only then are the others
+ // enumerated, hardware first, then the fallback adapter.
+ if created.is_none() {
+ for adapter in other_wgpu_adapters(&instance, force_software_adapter).await {
+ match try_rendering_device(adapter).await {
+ Ok(found) => {
+ created = Some(found);
+ break;
+ }
+ Err(error) => last_error = Some(error),
+ }
}
+ }
+ let Some((adapter, device, queue)) = created else {
+ device_phase.finish("failed");
+ return Err(last_error.map_or(RenderingError::NoAdapter, Into::into));
+ };
+ device_phase.finish("returned");
- (adapter, is_software)
+ let adapter_info = adapter.get_info();
+ let is_software_adapter = is_software_wgpu_adapter(&adapter_info);
+ if is_software_adapter {
+ tracing::warn!(
+ adapter_name = adapter_info.name,
+ adapter_backend = ?adapter_info.backend,
+ adapter_device_type = ?adapter_info.device_type,
+ "Using software adapter (CPU rendering - performance may be reduced)"
+ );
} else {
- tracing::warn!("No hardware GPU adapter found, attempting software fallback");
- let software_adapter = instance
- .request_adapter(&wgpu::RequestAdapterOptions {
- power_preference: wgpu::PowerPreference::LowPower,
- force_fallback_adapter: true,
- compatible_surface: None,
- })
- .await
- .map_err(|_| RenderingError::NoAdapter)?;
-
- let adapter_info = software_adapter.get_info();
tracing::info!(
adapter_name = adapter_info.name,
adapter_backend = ?adapter_info.backend,
adapter_device_type = ?adapter_info.device_type,
- "Using software adapter (CPU rendering - performance may be reduced)"
+ "Using hardware GPU adapter"
);
- (software_adapter, true)
- };
+ }
+ log_uncaptured_errors(&device, "cap-rendering-device");
+ Ok(SharedWgpuDevice {
+ instance,
+ adapter,
+ device,
+ queue,
+ is_software_adapter,
+ })
+}
- adapter_phase.finish(if is_software_adapter {
- "software"
+// Identical cards report identical `AdapterInfo` (wgpu 25 has no bus id), so
+// the preferred adapter is not filtered out: one more failed attempt on it is
+// cheaper than skipping a working twin.
+#[cfg(not(target_arch = "wasm32"))]
+async fn other_wgpu_adapters(
+ instance: &wgpu::Instance,
+ force_software_adapter: bool,
+) -> Vec<wgpu::Adapter> {
+ let mut adapters = if force_software_adapter {
+ Vec::new()
} else {
- "hardware"
- });
+ instance.enumerate_adapters(wgpu::Backends::all())
+ };
+ adapters.sort_by_key(|adapter| is_software_wgpu_adapter(&adapter.get_info()));
+ if let Ok(fallback) = instance
+ .request_adapter(&wgpu::RequestAdapterOptions {
+ power_preference: wgpu::PowerPreference::LowPower,
+ force_fallback_adapter: true,
+ compatible_surface: None,
+ })
+ .await
+ {
+ adapters.push(fallback);
+ }
+ adapters
+}
+
+#[cfg(not(target_arch = "wasm32"))]
+async fn try_rendering_device(
+ adapter: wgpu::Adapter,
+) -> Result<(wgpu::Adapter, wgpu::Device, wgpu::Queue), wgpu::RequestDeviceError> {
+ let info = adapter.get_info();
+ match request_rendering_device(&adapter).await {
+ Ok((device, queue)) => Ok((adapter, device, queue)),
+ Err(error) => {
+ tracing::warn!(
+ adapter_name = info.name,
+ adapter_backend = ?info.backend,
+ %error,
+ "Could not create a rendering device; trying the next adapter"
+ );
+ Err(error)
+ }
+ }
+}
+#[cfg(not(target_arch = "wasm32"))]
+async fn request_rendering_device(
+ adapter: &wgpu::Adapter,
+) -> Result<(wgpu::Device, wgpu::Queue), wgpu::RequestDeviceError> {
let mut required_features = wgpu::Features::empty();
if adapter.features().contains(wgpu::Features::PIPELINE_CACHE) {
required_features |= wgpu::Features::PIPELINE_CACHE;
@@ -2664,24 +2729,13 @@ pub async fn create_shared_device() -> Result<SharedWgpuDevice, RenderingError>
{
required_features |= wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES;
}
-
- let device_descriptor = wgpu::DeviceDescriptor {
- label: Some("cap-rendering-device"),
- required_features,
- ..Default::default()
- };
-
- let device_phase = readiness::Phase::start("wgpu.device");
- let (device, queue) = adapter.request_device(&device_descriptor).await?;
- device_phase.finish("returned");
- log_uncaptured_errors(&device, "cap-rendering-device");
- Ok(SharedWgpuDevice {
- instance,
- adapter,
- device,
- queue,
- is_software_adapter,
- })
+ adapter
+ .request_device(&wgpu::DeviceDescriptor {
+ label: Some("cap-rendering-device"),
+ required_features,
+ ..Default::default()
+ })
+ .await
}
/// wgpu panics on an error nothing captured. After a device is lost (a driver
@@ -7979,56 +8033,17 @@ impl<'a> FrameRenderer<'a> {
let nv12_size = (width as usize) * (height as usize) * 3 / 2;
let mut nv12_buf = self.nv12_buffer_pool.acquire(nv12_size);
- let y_stride = width as usize;
- let uv_stride = width as usize;
- let y_plane_size = y_stride * height as usize;
- let uv_plane_size = uv_stride * (height as usize / 2);
+ let y_plane_size = width as usize * height as usize;
+ let uv_plane_size = width as usize * (height as usize / 2);
nv12_buf.resize(y_plane_size + uv_plane_size, 0);
- let src_data = &rgba_frame.data;
- let src_stride = padded_bytes_per_row as usize;
-
- for row in 0..height as usize {
- let src_row = &src_data[row * src_stride..row * src_stride + width as usize * 4];
- let y_row = &mut nv12_buf[row * y_stride..(row + 1) * y_stride];
- for col in 0..width as usize {
- let r = src_row[col * 4] as i32;
- let g = src_row[col * 4 + 1] as i32;
- let b = src_row[col * 4 + 2] as i32;
- y_row[col] = cpu_yuv::bt709_luma(r, g, b);
- }
- }
-
- let uv_offset = y_plane_size;
- for row in 0..(height as usize / 2) {
- let src_row0 =
- &src_data[row * 2 * src_stride..row * 2 * src_stride + width as usize * 4];
- let src_row1 = &src_data
- [(row * 2 + 1) * src_stride..(row * 2 + 1) * src_stride + width as usize * 4];
- let uv_row =
- &mut nv12_buf[uv_offset + row * uv_stride..uv_offset + (row + 1) * uv_stride];
- for col in 0..(width as usize / 2) {
- let r = (src_row0[col * 8] as i32
- + src_row0[col * 8 + 4] as i32
- + src_row1[col * 8] as i32
- + src_row1[col * 8 + 4] as i32
- + 2)
- / 4;
- let g = (src_row0[col * 8 + 1] as i32
- + src_row0[col * 8 + 5] as i32
- + src_row1[col * 8 + 1] as i32
- + src_row1[col * 8 + 5] as i32
- + 2)
- / 4;
- let b = (src_row0[col * 8 + 2] as i32
- + src_row0[col * 8 + 6] as i32
- + src_row1[col * 8 + 2] as i32
- + src_row1[col * 8 + 6] as i32
- + 2)
- / 4;
- [uv_row[col * 2], uv_row[col * 2 + 1]] = cpu_yuv::bt709_chroma(r, g, b);
- }
- }
+ cpu_yuv::rgba_to_nv12(
+ &rgba_frame.data,
+ padded_bytes_per_row as usize,
+ width as usize,
+ height as usize,
+ &mut nv12_buf,
+ );
frame_pipeline::Nv12RenderedFrame {
data: self.nv12_buffer_pool.wrap(nv12_buf),
diff --git a/crates/rendering/src/text.rs b/crates/rendering/src/text.rs
index e6dd650b0..cb87503fd 100644
--- a/crates/rendering/src/text.rs
+++ b/crates/rendering/src/text.rs
@@ -318,17 +318,17 @@ pub fn prepare_texts(
output_size.y as f32 / REFERENCE_HEIGHT
};
- let mut ordered = segments.iter().enumerate().collect::<Vec<_>>();
+ let mut ordered = segments
+ .iter()
+ .enumerate()
+ .filter(|(index, segment)| {
+ segment.enabled
+ && !hidden_indices.contains(index)
+ && !(frame_time < segment.start || frame_time > segment.end)
+ })
+ .collect::<Vec<_>>();
ordered.sort_by_key(|(index, segment)| (segment.track, *index));
- for (i, segment) in ordered {
- if !segment.enabled || hidden_indices.contains(&i) {
- continue;
- }
-
- if frame_time < segment.start || frame_time > segment.end {
- continue;
- }
-
+ for (_, segment) in ordered {
let center = XY::new(
segment.center.x.clamp(0.0, 1.0),
segment.center.y.clamp(0.0, 1.0),
@@ -463,6 +463,72 @@ mod tests {
}
}
+ #[test]
+ #[ignore]
+ fn benchmark_text_preparation() {
+ use std::{
+ hash::{DefaultHasher, Hash, Hasher},
+ hint::black_box,
+ time::Instant,
+ };
+
+ for (name, total, active, tracks) in [
+ ("empty", 0, 0, 1),
+ ("eight_all_active", 8, 8, 4),
+ ("64_one_active", 64, 1, 4),
+ ("256_four_active", 256, 4, 4),
+ ("1024_four_active", 1024, 4, 4),
+ ("256_all_active", 256, 256, 4),
+ ("256_four_active_single_track", 256, 4, 1),
+ ] {
+ let segments = (0..total)
+ .map(|index| {
+ let mut seg = segment(TextAnimation::None, TextAnimation::None);
+ seg.track = ((index * 97) % tracks) as u32;
+ seg.content = format!("Overlay {index}: café 字");
+ if (index * 97) % total >= active {
+ seg.start = 20.0 + index as f64;
+ seg.end = seg.start + 4.0;
+ }
+ seg
+ })
+ .collect::<Vec<_>>();
+ let prepare = || {
+ prepare_texts(
+ black_box(XY::new(1920, 1080)),
+ black_box(5.0),
+ black_box(&segments),
+ black_box(&[]),
+ )
+ };
+ let output = prepare();
+ assert_eq!(output.len(), active);
+ let mut hasher = DefaultHasher::new();
+ format!("{output:?}").hash(&mut hasher);
+ let fingerprint = hasher.finish();
+ let start = Instant::now();
+ for _ in 0..1000 {
+ black_box(prepare());
+ }
+ let ns_per_call = start.elapsed().as_nanos() as f64 / 1000.0;
+ let iterations = (20_000_000.0 / ns_per_call).clamp(100.0, 1_000_000.0) as usize;
+ for sample in 0..15 {
+ let start = Instant::now();
+ for _ in 0..iterations {
+ black_box(prepare());
+ }
+ println!(
+ "TEXT_BENCH {}",
+ serde_json::json!({
+ "case": name, "sample": sample, "iterations": iterations,
+ "ns_per_call": start.elapsed().as_nanos() as f64 / iterations as f64,
+ "fingerprint": fingerprint,
+ })
+ );
+ }
+ }
+ }
+
fn prepare_one(seg: TextSegment, time: f64) -> Option<PreparedText> {
prepare_texts(XY::new(1920, 1080), time, &[seg], &[])
.into_iter()
@@ -503,6 +569,62 @@ mod tests {
);
}
+ #[test]
+ fn filtering_preserves_original_indices_order_and_time_comparisons() {
+ let mut segments = (0..64)
+ .map(|index| {
+ let mut seg = segment(TextAnimation::None, TextAnimation::None);
+ seg.track = ((index * 11) % 7) as u32;
+ seg.content = format!("Text {index}: café 字");
+ seg.start = (index % 9) as f64 - 4.0;
+ seg.end = seg.start + 2.0;
+ seg.enabled = index % 5 != 0;
+ seg.uppercase = index % 3 == 0;
+ seg.center.x = index as f64 / 63.0;
+ seg.stroke_width = (index % 4) as f32;
+ seg
+ })
+ .collect::<Vec<_>>();
+ segments[7].start = f64::NAN;
+ segments[13].end = f64::NAN;
+ segments[19].end = segments[19].start - 1.0;
+ segments[31].end = f64::INFINITY;
+ segments[41].start = f64::NEG_INFINITY;
+ let hidden = [0, 1, 15, 60, 15, usize::MAX];
+ for time in [
+ f64::NEG_INFINITY,
+ -4.0,
+ -2.0,
+ 0.0,
+ 2.0,
+ 4.0,
+ 6.0,
+ f64::INFINITY,
+ f64::NAN,
+ ] {
+ for size in [XY::new(1920, 1080), XY::new(0, 0)] {
+ let mut ordered = segments.iter().enumerate().collect::<Vec<_>>();
+ ordered.sort_by_key(|(index, seg)| (seg.track, *index));
+ let mut expected = Vec::new();
+ for (index, seg) in ordered {
+ if !seg.enabled || hidden.contains(&index) || time < seg.start || time > seg.end
+ {
+ continue;
+ }
+ let mut single = prepare_texts(size, time, std::slice::from_ref(seg), &[]);
+ assert_eq!(single.len(), 1, "segment {index}, time {time}");
+ expected.append(&mut single);
+ }
+ let actual = prepare_texts(size, time, &segments, &hidden);
+ assert_eq!(
+ format!("{actual:?}"),
+ format!("{expected:?}"),
+ "time {time}"
+ );
+ }
+ }
+ }
+
#[test]
fn resting_text_has_identity_animation_state() {
let text = prepare_one(segment(TextAnimation::Fade, TextAnimation::Fade), 5.0).unwrap();
diff --git a/crates/rendering/src/yuv_converter.rs b/crates/rendering/src/yuv_converter.rs
index f7e8a7983..4fe9e522c 100644
--- a/crates/rendering/src/yuv_converter.rs
+++ b/crates/rendering/src/yuv_converter.rs
@@ -519,6 +519,7 @@ unsafe impl Sync for RetainedImageBuf {}
pub struct YuvToRgbaConverter {
pipelines: Arc<YuvConverterPipelines>,
matrix: YuvMatrix,
+ cpu_rgba_scratch: Vec<u8>,
y_texture: wgpu::Texture,
y_view: wgpu::TextureView,
uv_texture: wgpu::Texture,
@@ -588,6 +589,7 @@ impl YuvToRgbaConverter {
Self {
matrix: YuvMatrix::default(),
+ cpu_rgba_scratch: Vec::new(),
pipelines,
y_texture,
y_view,
@@ -1773,7 +1775,9 @@ impl YuvToRgbaConverter {
self.ensure_texture_size(device, effective_width, effective_height);
self.swap_output_buffer();
- let mut rgba_data = vec![0u8; (width * height * 4) as usize];
+ let byte_len = (width * height * 4) as usize;
+ let matrix = self.matrix;
+ let mut rgba_data = self.take_cpu_rgba_scratch(byte_len);
cpu_yuv::nv12_to_rgba_simd(
y_data,
@@ -1782,8 +1786,8 @@ impl YuvToRgbaConverter {
height,
y_stride,
uv_stride,
- &mut rgba_data,
- self.matrix,
+ &mut rgba_data[..byte_len],
+ matrix,
);
queue.write_texture(
@@ -1793,7 +1797,7 @@ impl YuvToRgbaConverter {
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
- &rgba_data,
+ &rgba_data[..byte_len],
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(width * 4),
@@ -1806,6 +1810,7 @@ impl YuvToRgbaConverter {
},
);
+ self.cpu_rgba_scratch = rgba_data;
Ok(self.current_output_view())
}
@@ -1827,7 +1832,9 @@ impl YuvToRgbaConverter {
self.ensure_texture_size(device, effective_width, effective_height);
self.swap_output_buffer();
- let mut rgba_data = vec![0u8; (width * height * 4) as usize];
+ let byte_len = (width * height * 4) as usize;
+ let matrix = self.matrix;
+ let mut rgba_data = self.take_cpu_rgba_scratch(byte_len);
cpu_yuv::yuv420p_to_rgba_simd(
y_data,
@@ -1837,8 +1844,8 @@ impl YuvToRgbaConverter {
height,
y_stride,
uv_stride,
- &mut rgba_data,
- self.matrix,
+ &mut rgba_data[..byte_len],
+ matrix,
);
queue.write_texture(
@@ -1848,7 +1855,7 @@ impl YuvToRgbaConverter {
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
- &rgba_data,
+ &rgba_data[..byte_len],
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(width * 4),
@@ -1861,9 +1868,20 @@ impl YuvToRgbaConverter {
},
);
+ self.cpu_rgba_scratch = rgba_data;
Ok(self.current_output_view())
}
+ fn take_cpu_rgba_scratch(&mut self, byte_len: usize) -> Vec<u8> {
+ let mut rgba = std::mem::take(&mut self.cpu_rgba_scratch);
+ // Kernels write every output byte, including alpha, so a reused
+ // buffer is cleared only when a larger frame grows it.
+ if rgba.len() < byte_len {
+ rgba.resize(byte_len, 0);
+ }
+ rgba
+ }
+
pub fn output_texture(&self) -> Option<&wgpu::Texture> {
Some(self.current_output_texture())
}
diff --git a/crates/rendering/src/zoom_spring.rs b/crates/rendering/src/zoom_spring.rs
index 0dee58234..5f66c1180 100644
--- a/crates/rendering/src/zoom_spring.rs
+++ b/crates/rendering/src/zoom_spring.rs
@@ -553,6 +553,28 @@ fn build_time_map(timeline: Option<&TimelineConfiguration>) -> Vec<TimeMapSegmen
out
}
+fn ordered_time_map(map: &[TimeMapSegment]) -> bool {
+ map.iter()
+ .all(|segment| segment.timeline_start.is_finite() && segment.timeline_end.is_finite())
+ && map.windows(2).all(|pair| {
+ pair[0].timeline_start <= pair[1].timeline_start
+ && pair[0].timeline_end <= pair[1].timeline_end
+ })
+}
+
+fn time_map_at(map: &[TimeMapSegment], time: f64, ordered: bool) -> &[TimeMapSegment] {
+ if !ordered || !time.is_finite() {
+ return map;
+ }
+ let first = map.partition_point(|segment| segment.timeline_end <= time);
+ let end = map.partition_point(|segment| segment.timeline_start <= time);
+ if first < end && time > map[first].timeline_start {
+ &map[first..end]
+ } else {
+ map
+ }
+}
+
/// Maps a timeline timestamp to recording seconds. Identity when no timeline
/// is configured; clamps to the nearest edit boundary outside the timeline.
fn map_timeline_to_recording_secs(
@@ -688,6 +710,7 @@ pub struct ZoomTransformTimeline {
/// Auto segments, `None` for Manual ones.
clusters: Vec<Option<ZoomClusters>>,
time_map: Vec<TimeMapSegment>,
+ time_map_ordered: bool,
recording_clip: Option<u32>,
prefer_outgoing: bool,
/// Total number of samples covering [0, duration] plus one lerp partner.
@@ -753,6 +776,7 @@ impl ZoomTransformTimeline {
} else {
build_time_map(timeline)
};
+ let time_map_ordered = ordered_time_map(&time_map);
let clusters = zoom_segments
.iter()
.map(|segment| match segment.mode {
@@ -799,6 +823,7 @@ impl ZoomTransformTimeline {
segment_index,
clusters,
time_map,
+ time_map_ordered,
recording_clip,
prefer_outgoing,
total_samples: 1,
@@ -818,6 +843,7 @@ impl ZoomTransformTimeline {
segment_index,
clusters,
time_map,
+ time_map_ordered,
recording_clip,
prefer_outgoing,
total_samples,
@@ -1132,7 +1158,7 @@ impl ZoomTransformTimeline {
}
ZoomMode::Auto => {
let recording_ms = map_timeline_to_recording_secs(
- &self.time_map,
+ time_map_at(&self.time_map, timeline_secs, self.time_map_ordered),
timeline_secs,
self.recording_clip,
self.prefer_outgoing,
@@ -1326,6 +1352,124 @@ mod tests {
project
}
+ #[test]
+ fn narrowed_time_maps_match_full_scans_at_boundaries_and_overlaps() {
+ let span = |start, end, clip| TimeMapSegment {
+ timeline_start: start,
+ timeline_end: end,
+ recording_start: -0.0,
+ timescale: 1.5,
+ recording_clip: clip,
+ };
+ for map in [
+ vec![],
+ vec![span(0.0, 2.0, 0)],
+ vec![span(0.0, 4.0, 0), span(3.0, 7.0, 1), span(6.0, 10.0, 0)],
+ vec![span(0.0, 10.0, 0), span(1.0, 3.0, 1)],
+ vec![span(3.0, 5.0, 1), span(0.0, 4.0, 0)],
+ vec![span(0.0, 3.0, 0), span(0.0, 4.0, 1)],
+ vec![span(0.0, 2.0, 0), span(3.0, 4.0, 1)],
+ vec![
+ span(0.0, 2.0, 0),
+ TimeMapSegment {
+ timescale: 0.0,
+ ..span(2.0, 4.0, 0)
+ },
+ span(4.0, 6.0, 1),
+ ],
+ vec![span(0.0, f64::INFINITY, 0)],
+ vec![span(f64::NAN, 4.0, 0)],
+ ] {
+ let ordered = ordered_time_map(&map);
+ let mut queries = vec![
+ f64::NEG_INFINITY,
+ -1.0,
+ -0.0,
+ 0.0,
+ 1.5,
+ 3.5,
+ 8.0,
+ 20.0,
+ f64::INFINITY,
+ f64::NAN,
+ ];
+ for segment in &map {
+ for boundary in [segment.timeline_start, segment.timeline_end] {
+ queries.extend([boundary.next_down(), boundary, boundary.next_up()]);
+ }
+ }
+ for query in queries {
+ let selected = time_map_at(&map, query, ordered);
+ if !ordered || !query.is_finite() {
+ assert!(std::ptr::eq(selected, map.as_slice()));
+ }
+ for clip in [None, Some(0), Some(1), Some(99)] {
+ for outgoing in [false, true] {
+ let expected = map_timeline_to_recording_secs(&map, query, clip, outgoing);
+ let actual =
+ map_timeline_to_recording_secs(selected, query, clip, outgoing);
+ if expected.is_nan() {
+ assert!(actual.is_nan());
+ } else {
+ assert_eq!(
+ actual.to_bits(),
+ expected.to_bits(),
+ "query {query}, clip {clip:?}, outgoing {outgoing}"
+ );
+ }
+ }
+ }
+ }
+ }
+ let map = [span(0.0, 2.0, 0), span(2.0, 4.0, 1), span(4.0, 6.0, 0)];
+ assert_eq!(time_map_at(&map, 3.0, ordered_time_map(&map)).len(), 1);
+ }
+
+ #[test]
+ fn indexed_mapping_preserves_complete_zoom_simulation_samples() {
+ let project = project_for_zoom_reuse();
+ let duration = project.timeline.as_ref().unwrap().duration();
+ let cursor = CursorEvents {
+ moves: vec![
+ move_event(0.0, 0.1, 0.2),
+ move_event(5000.0, 0.4, 0.8),
+ move_event(10000.0, 0.8, 0.3),
+ move_event(20000.0, 0.2, 0.6),
+ ],
+ clicks: vec![click_event(4500.0), click_event(11000.0)],
+ };
+ for clip in [0, 1] {
+ for outgoing in [false, true] {
+ let build = || {
+ if outgoing {
+ ZoomTransformTimeline::from_project_for_outgoing_clip(
+ &project,
+ &cursor,
+ duration,
+ XY::new(1920, 1080),
+ clip,
+ )
+ } else {
+ ZoomTransformTimeline::from_project_for_clip(
+ &project,
+ &cursor,
+ duration,
+ XY::new(1920, 1080),
+ clip,
+ )
+ }
+ };
+ let mut expected = build();
+ expected.time_map_ordered = false;
+ let mut actual = build();
+ assert!(actual.time_map_ordered);
+ expected.precompute();
+ actual.precompute();
+ assert_samples_identical(&expected, &actual);
+ }
+ }
+ }
+
#[test]
fn unrelated_project_edits_preserve_incoming_and_outgoing_zoom_samples() {
let project = project_for_zoom_reuse();
diff --git a/crates/verify/tests/audio.rs b/crates/verify/tests/audio.rs
index 8789c9697..881c109da 100644
--- a/crates/verify/tests/audio.rs
+++ b/crates/verify/tests/audio.rs
@@ -88,6 +88,14 @@ fn studio_sound(config: &mut ProjectConfiguration) {
config.audio.improve = true;
}
+/// Studio Sound across a cut shorter than its warm-up read-back inside one
+/// clip (GitHub #2790: every such export failed in 0.6.1 with "Invalid
+/// sequential export audio window").
+fn studio_sound_short_cut(config: &mut ProjectConfiguration) {
+ common::set_cuts(config, &[(0.5, 3.0, 1.0), (3.2, 5.0, 1.0), (5.3, 7.8, 1.0)]);
+ config.audio.improve = true;
+}
+
fn transition(config: &mut ProjectConfiguration) {
common::set_cuts(config, &[(0.3, 3.5, 1.0), (4.5, 7.8, 1.0)]);
common::add_transition(config, 1, 0.4);
@@ -142,6 +150,10 @@ fn variants() -> Vec<Variant> {
edit: "studio-sound",
apply: studio_sound,
},
+ Variant {
+ edit: "studio-sound-short-cut",
+ apply: studio_sound_short_cut,
+ },
Variant {
edit: "transition",
apply: transition,
diff --git a/packages/web-backend/src/Organisations/OrganisationsRepo.ts b/packages/web-backend/src/Organisations/OrganisationsRepo.ts
index 19db18843..a1368a01e 100644
--- a/packages/web-backend/src/Organisations/OrganisationsRepo.ts
+++ b/packages/web-backend/src/Organisations/OrganisationsRepo.ts
@@ -12,6 +12,31 @@ export class OrganisationsRepo extends Effect.Service<OrganisationsRepo>()(
const db = yield* Database;
return {
+ membershipsForVideos: (
+ userId: User.UserId,
+ videoIds: readonly Video.VideoId[],
+ ) =>
+ db.use((db) =>
+ db
+ .select({
+ videoId: Db.sharedVideos.videoId,
+ membershipId: Db.organizationMembers.id,
+ })
+ .from(Db.organizationMembers)
+ .innerJoin(
+ Db.sharedVideos,
+ Dz.eq(
+ Db.organizationMembers.organizationId,
+ Db.sharedVideos.organizationId,
+ ),
+ )
+ .where(
+ Dz.and(
+ Dz.eq(Db.organizationMembers.userId, userId),
+ Dz.inArray(Db.sharedVideos.videoId, [...videoIds]),
+ ),
+ ),
+ ),
membershipForVideo: (userId: User.UserId, videoId: Video.VideoId) =>
db.use((db) =>
db
diff --git a/packages/web-backend/src/Spaces/SpacesRepo.ts b/packages/web-backend/src/Spaces/SpacesRepo.ts
index 46035b912..6db938621 100644
--- a/packages/web-backend/src/Spaces/SpacesRepo.ts
+++ b/packages/web-backend/src/Spaces/SpacesRepo.ts
@@ -10,6 +10,39 @@ export class SpacesRepo extends Effect.Service<SpacesRepo>()("SpacesRepo", {
const db = yield* Database;
return {
+ membershipsForVideos: (
+ userId: User.UserId,
+ videoIds: readonly Video.VideoId[],
+ ) =>
+ db.use((db) =>
+ db
+ .select({
+ videoId: Db.spaceVideos.videoId,
+ membershipId: Db.spaceMembers.id,
+ })
+ .from(Db.spaceMembers)
+ .innerJoin(
+ Db.spaceVideos,
+ Dz.eq(Db.spaceMembers.spaceId, Db.spaceVideos.spaceId),
+ )
+ .where(
+ Dz.and(
+ Dz.eq(Db.spaceMembers.userId, userId),
+ Dz.inArray(Db.spaceVideos.videoId, [...videoIds]),
+ ),
+ ),
+ ),
+ passwordsForVideos: (videoIds: readonly Video.VideoId[]) =>
+ db.use((db) =>
+ db
+ .select({
+ videoId: Db.spaceVideos.videoId,
+ password: Db.spaces.password,
+ })
+ .from(Db.spaceVideos)
+ .innerJoin(Db.spaces, Dz.eq(Db.spaceVideos.spaceId, Db.spaces.id))
+ .where(Dz.inArray(Db.spaceVideos.videoId, [...videoIds])),
+ ),
membershipForVideo: (userId: User.UserId, videoId: Video.VideoId) =>
db
.use((db) =>
diff --git a/packages/web-backend/src/Videos/VideosPolicy.ts b/packages/web-backend/src/Videos/VideosPolicy.ts
index 1160dd0f9..c3270bdab 100644
--- a/packages/web-backend/src/Videos/VideosPolicy.ts
+++ b/packages/web-backend/src/Videos/VideosPolicy.ts
@@ -1,6 +1,6 @@
import { isEmailAllowedByRestriction } from "@cap/utils";
import {
- type CurrentUser,
+ CurrentUser,
type DatabaseError,
type Organisation,
Policy,
@@ -192,6 +192,81 @@ export class VideosPolicy extends Effect.Service<VideosPolicy>()(
password: Option.Option<string>,
) => buildCanViewLoaded(deps, video, password);
+ const canViewLoadedBulk = Effect.fn("VideosPolicy.canViewLoadedBulk")(
+ function* (
+ loaded: ReadonlyArray<
+ readonly [ViewableVideo, Option.Option<string>]
+ >,
+ ) {
+ const user = yield* Effect.serviceOption(CurrentUser);
+ const videoIds = loaded
+ .filter(
+ ([video]) =>
+ Option.isNone(user) || video.ownerId !== user.value.id,
+ )
+ .map(([video]) => video.id);
+ if (videoIds.length === 0)
+ return yield* Effect.forEach(loaded, ([video, password]) =>
+ canViewLoaded(video, password).pipe(Effect.exit),
+ );
+
+ // Retain lookup failures until each policy runs, including the owner bypass.
+ const [passwords, orgMemberships, spaceMemberships] =
+ yield* Effect.all(
+ [
+ spacesRepo.passwordsForVideos(videoIds).pipe(
+ Effect.map((rows) =>
+ Array.groupBy(rows, (row) => row.videoId),
+ ),
+ Effect.exit,
+ ),
+ (Option.isSome(user)
+ ? orgsRepo.membershipsForVideos(user.value.id, videoIds)
+ : Effect.succeed([])
+ ).pipe(
+ Effect.map((rows) =>
+ Array.groupBy(rows, (row) => row.videoId),
+ ),
+ Effect.exit,
+ ),
+ (Option.isSome(user)
+ ? spacesRepo.membershipsForVideos(user.value.id, videoIds)
+ : Effect.succeed([])
+ ).pipe(
+ Effect.map((rows) =>
+ Array.groupBy(rows, (row) => row.videoId),
+ ),
+ Effect.exit,
+ ),
+ ],
+ { concurrency: "unbounded" },
+ );
+ const bulkDeps: ViewDecisionDeps = {
+ ...deps,
+ orgsRepo: {
+ ...orgsRepo,
+ membershipForVideo: (_, id) =>
+ Effect.map(orgMemberships, (rows) => rows[id] ?? []),
+ },
+ spacesRepo: {
+ ...spacesRepo,
+ passwordsForVideo: (id) =>
+ Effect.map(passwords, (rows) => rows[id] ?? []),
+ membershipForVideo: (_, id) =>
+ Effect.map(spaceMemberships, (rows) =>
+ Option.fromNullable(rows[id]?.[0]),
+ ),
+ },
+ };
+ return yield* Effect.forEach(
+ loaded,
+ ([video, password]) =>
+ buildCanViewLoaded(bulkDeps, video, password).pipe(Effect.exit),
+ { concurrency: 10 },
+ );
+ },
+ );
+
const isOwner = (videoId: Video.VideoId) =>
Policy.policy((user) =>
repo.getById(videoId).pipe(
@@ -234,6 +309,7 @@ export class VideosPolicy extends Effect.Service<VideosPolicy>()(
return {
canView,
canViewLoaded,
+ canViewLoadedBulk,
isOwner,
isOwnerLoaded,
getViewableById,
diff --git a/packages/web-backend/src/Videos/VideosRepo.ts b/packages/web-backend/src/Videos/VideosRepo.ts
index 5832c41eb..99ae49674 100644
--- a/packages/web-backend/src/Videos/VideosRepo.ts
+++ b/packages/web-backend/src/Videos/VideosRepo.ts
@@ -109,6 +109,10 @@ export class VideosRepo extends Effect.Service<VideosRepo>()("VideosRepo", {
}
}
await Promise.all([
+ db
+ .delete(Db.sharedVideos)
+ .where(Dz.eq(Db.sharedVideos.videoId, id)),
+ db.delete(Db.spaceVideos).where(Dz.eq(Db.spaceVideos.videoId, id)),
db.delete(Db.importedVideos).where(Dz.eq(Db.importedVideos.id, id)),
db.delete(Db.videos).where(Dz.eq(Db.videos.id, id)),
db
diff --git a/packages/web-backend/src/Videos/index.ts b/packages/web-backend/src/Videos/index.ts
index 2d27caa09..fc5c08a9d 100644
--- a/packages/web-backend/src/Videos/index.ts
+++ b/packages/web-backend/src/Videos/index.ts
@@ -12,7 +12,7 @@ import {
Video,
} from "@cap/web-domain";
import * as Dz from "drizzle-orm";
-import { Effect, Exit, Option } from "effect";
+import { Array as Arr, Effect, Exit, Option } from "effect";
import type { Schema } from "effect/Schema";
import { Database } from "../Database.ts";
@@ -253,20 +253,23 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
.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) => {
- 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 policyExits = yield* policy.canViewLoadedBulk(
+ rows.map(
+ (video) => [video, Option.fromNullable(video.password)] as const,
+ ),
+ );
+ const resultsById = new Map(
+ Arr.zipWith(
+ rows,
+ policyExits,
+ (video, exit) =>
+ [video.id, Exit.map(exit, () => Option.some(video))] as const,
+ ),
+ );
+ const videoExits = videoIds.map(
+ (videoId) =>
+ resultsById.get(videoId) ??
+ Exit.succeed(Option.none<(typeof rows)[number]>()),
);
const analyticsVideos = videoExits.flatMap((exit) => {
if (!Exit.isSuccess(exit) || Option.isNone(exit.value)) return [];
diff --git a/scripts/finalize-linux-appimage.mjs b/scripts/finalize-linux-appimage.mjs
index 63c836932..adb38f432 100644
--- a/scripts/finalize-linux-appimage.mjs
+++ b/scripts/finalize-linux-appimage.mjs
@@ -6,6 +6,7 @@ import {
chmod,
copyFile,
lstat,
+ mkdir,
mkdtemp,
open,
readdir,
@@ -104,11 +105,142 @@ esac
exec "$cap_appdir/AppRun.cap-original" "$@"
`;
-export async function preserveAppImageWorkingDirectory(appDir) {
+// The GPUI app finds every bundled library through RUNPATH, so it starts Cap
+// directly. The generic AppImage launcher's LD_LIBRARY_PATH, PYTHONHOME and
+// friends reached the host's PipeWire plugins inside Cap and every program Cap
+// starts (file managers, xdg-open), which then loaded Ubuntu's libraries: on
+// NixOS and Arch screen capture failed to connect and folders did not open.
+// It keeps what that launcher did for Cap: `usr/bin` first on PATH (Cap runs
+// the bundled pactl), the bundled data directory, and `usr` as the working
+// directory. The bundled libva is only used when the host has none, because
+// host PipeWire plugins link a host FFmpeg that needs the host's newer libva.
+export const directAppRunWrapper = `#!/bin/sh
+unset OWD
+if cap_original_directory="$(pwd -P && printf '.')"; then
+ OWD="\${cap_original_directory%?}"
+ OWD="\${OWD%?}"
+ export OWD
+fi
+case "$0" in
+ */*) cap_appdir="\${0%/*}" ;;
+ *) cap_appdir=. ;;
+esac
+cap_appdir="$(cd "$cap_appdir" && pwd -P)" || exit 1
+export PATH="$cap_appdir/usr/bin:\${PATH:-/usr/local/bin:/usr/bin:/bin}"
+export XDG_DATA_DIRS="$cap_appdir/usr/share:\${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
+cap_host_libva=0
+for cap_directory in /usr/lib/x86_64-linux-gnu /lib/x86_64-linux-gnu /usr/lib/aarch64-linux-gnu /lib/aarch64-linux-gnu /usr/lib64 /lib64 /usr/lib /lib; do
+ if [ -e "$cap_directory/libva.so.2" ] && [ -e "$cap_directory/libva-drm.so.2" ]; then
+ cap_host_libva=1
+ break
+ fi
+done
+if [ "$cap_host_libva" = 0 ] && ldconfig -p 2>/dev/null | grep -q 'libva-drm[.]so[.]2 '; then
+ cap_host_libva=1
+fi
+if [ "$cap_host_libva" = 0 ]; then
+ export LD_LIBRARY_PATH="$cap_appdir/usr/lib/cap/libva\${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
+fi
+unset cap_original_directory cap_directory cap_host_libva
+cd "$cap_appdir/usr" || exit 1
+exec "$cap_appdir/usr/bin/Cap" "$@"
+`;
+
+const libvaPattern = /^libva(?:-drm)?\.so\.2(?:\..+)?$/;
+
+// The fallback libva, for hosts without one. It must be new enough for the
+// host's own FFmpeg, which host PipeWire plugins load into Cap: NixOS's
+// FFmpeg 9 needs vaMapBuffer2, which the build image's libva 2.20 lacks. A
+// newer libva keeps the ABI the bundled FFmpeg was built against. These need
+// glibc 2.38, older than the build image's.
+export const fallbackLibvaPackages = [
+ {
+ url: "http://archive.ubuntu.com/ubuntu/pool/main/libv/libva/libva2_2.23.0-1ubuntu1_amd64.deb",
+ sha256: "83053abb11b8b0169bfb2abbd209a44ddeb4e56223980cdcfc0218b09e845f00",
+ },
+ {
+ url: "http://archive.ubuntu.com/ubuntu/pool/main/libv/libva/libva-drm2_2.23.0-1ubuntu1_amd64.deb",
+ sha256: "f56b0cb850dcd8af45929b2da7cc1fbdf64245fbcec99033298499a8db2765a3",
+ },
+];
+
+/**
+ * The directory holding the fallback libva: `CAP_APPIMAGE_LIBVA_DIR`, or the
+ * pinned packages above, downloaded, checked and unpacked into `work`.
+ */
+export async function fallbackLibvaDirectory(
+ work,
+ { env = process.env, run = runCommand, download = fetch } = {},
+) {
+ if (env.CAP_APPIMAGE_LIBVA_DIR) return env.CAP_APPIMAGE_LIBVA_DIR;
+ const root = path.join(work, "libva");
+ await mkdir(root, { recursive: true });
+ for (const { url, sha256 } of fallbackLibvaPackages) {
+ const response = await download(url);
+ if (!response.ok)
+ throw new Error(`Downloading ${url} failed: ${response.status}`);
+ const bytes = Buffer.from(await response.arrayBuffer());
+ const digest = createHash("sha256").update(bytes).digest("hex");
+ if (digest !== sha256) {
+ throw new Error(`${url} has sha256 ${digest}, expected ${sha256}`);
+ }
+ const deb = path.join(root, path.basename(url));
+ await writeFile(deb, bytes);
+ await run("dpkg-deb", ["-x", deb, root], { env, stdio: "inherit" });
+ }
+ return path.join(root, "usr/lib/x86_64-linux-gnu");
+}
+
+/**
+ * Moves the bundled libva out of RUNPATH's reach into the launcher's fallback
+ * directory, replaced by the libraries in `replacement` when given.
+ */
+export async function isolateBundledLibva(appDir, { replacement } = {}) {
+ const libraries = path.join(appDir, "usr/lib");
+ const fallback = path.join(libraries, "cap/libva");
+ const moved = [];
+ for (const entry of await readdir(libraries, { withFileTypes: true })) {
+ if (!libvaPattern.test(entry.name) || entry.isDirectory()) continue;
+ await mkdir(fallback, { recursive: true });
+ await rename(
+ path.join(libraries, entry.name),
+ path.join(fallback, entry.name),
+ );
+ moved.push(entry.name);
+ }
+ if (replacement) {
+ const libraries = (await readdir(replacement)).filter((name) =>
+ libvaPattern.test(name),
+ );
+ for (const name of ["libva.so.2", "libva-drm.so.2"]) {
+ if (!libraries.includes(name)) {
+ throw new Error(`${replacement} has no ${name}`);
+ }
+ }
+ await rm(fallback, { recursive: true, force: true });
+ await mkdir(fallback, { recursive: true });
+ for (const name of libraries) {
+ const source = path.join(replacement, name);
+ const entry = await lstat(source);
+ if (entry.isSymbolicLink()) {
+ await symlink(await readlink(source), path.join(fallback, name));
+ } else {
+ await copyFile(source, path.join(fallback, name));
+ }
+ }
+ }
+ return moved.sort();
+}
+
+export async function preserveAppImageWorkingDirectory(
+ appDir,
+ { direct = false } = {},
+) {
+ const wrapper = direct ? directAppRunWrapper : appRunWrapper;
const launcher = path.join(appDir, "AppRun");
const original = path.join(appDir, "AppRun.cap-original");
if ((await readdir(appDir)).includes(path.basename(original))) {
- if ((await readFile(launcher, "utf8")) !== appRunWrapper) {
+ if ((await readFile(launcher, "utf8")) !== wrapper) {
throw new Error("AppImage already contains a different AppRun wrapper");
}
await access(original, constants.X_OK);
@@ -116,7 +248,37 @@ export async function preserveAppImageWorkingDirectory(appDir) {
}
await access(launcher, constants.X_OK);
await rename(launcher, original);
- await writeFile(launcher, appRunWrapper, { mode: 0o755, flag: "wx" });
+ await writeFile(launcher, wrapper, { mode: 0o755, flag: "wx" });
+}
+
+// Desktop launchers (GNOME's app grid among them) only hand an app an
+// XDG_ACTIVATION_TOKEN when its entry asks for startup notification. Without
+// one, Wayland focus stealing prevention keeps Cap's first window, and the
+// running Cap a relaunch raises, behind whatever had focus.
+export function withStartupNotify(entry) {
+ if (/^StartupNotify=/m.test(entry)) {
+ return entry.replace(/^StartupNotify=.*$/m, "StartupNotify=true");
+ }
+ return entry.replace(
+ /^\[Desktop Entry\]\n/m,
+ "[Desktop Entry]\nStartupNotify=true\n",
+ );
+}
+
+export async function notifyStartupInDesktopEntries(root, entries) {
+ const changed = [];
+ for (const entry of entries) {
+ const filename = path.join(root, entry);
+ const stats = await lstat(filename).catch(() => null);
+ if (!stats?.isFile()) continue;
+ const before = await readFile(filename, "utf8");
+ const after = withStartupNotify(before);
+ if (after !== before) {
+ await writeFile(filename, after);
+ changed.push(entry);
+ }
+ }
+ return changed;
}
export function runCommand(command, args, options = {}) {
@@ -492,8 +654,17 @@ export async function finalizeLinuxAppImage(
...(await removeWebviewRuntime(appDir, { env: tools, run })),
);
}
+ if (!webview) {
+ await isolateBundledLibva(appDir, {
+ replacement: await fallbackLibvaDirectory(work, { env: tools, run }),
+ });
+ await notifyStartupInDesktopEntries(appDir, [
+ "Cap.desktop",
+ "usr/share/applications/Cap.desktop",
+ ]);
+ }
await relativizeDirIcon(appDir);
- await preserveAppImageWorkingDirectory(appDir);
+ await preserveAppImageWorkingDirectory(appDir, { direct: !webview });
const output = path.join(work, path.basename(image));
await run(
outputPlugin,
diff --git a/scripts/finalize-linux-appimage.test.mjs b/scripts/finalize-linux-appimage.test.mjs
index a9f92995d..38f0287f9 100644
--- a/scripts/finalize-linux-appimage.test.mjs
+++ b/scripts/finalize-linux-appimage.test.mjs
@@ -20,8 +20,12 @@ import { tmpdir } from "node:os";
import path from "node:path";
import { test } from "node:test";
import {
+ directAppRunWrapper,
+ fallbackLibvaDirectory,
+ fallbackLibvaPackages,
finalizeLinuxAppImage,
findConflictingLibraries,
+ isolateBundledLibva,
preserveAppImageWorkingDirectory,
relativizeDirIcon,
removeWebviewRuntime,
@@ -852,14 +856,33 @@ test("webview-free finalization hands the pruned AppDir to the output plugin", a
stat(path.join(appDir, "usr/lib/libwebkit2gtk-4.1.so.0")),
);
await stat(path.join(appDir, "usr/lib/libssl.so.3"));
+ assert.equal(
+ await readFile(path.join(appDir, "AppRun"), "utf8"),
+ directAppRunWrapper,
+ );
+ await assert.rejects(stat(path.join(appDir, "usr/lib/libva.so.2")));
+ assert.equal(
+ await readFile(path.join(appDir, "usr/lib/cap/libva/libva.so.2"), "utf8"),
+ "libva 2.23",
+ );
await writeFile(options.env.OUTPUT, finalImage());
return {};
};
+ const newLibva = path.join(path.dirname(image), "libva-2.23");
+ await mkdir(newLibva);
+ await writeFile(path.join(newLibva, "libva.so.2.2300.0"), "libva 2.23");
+ await writeFile(
+ path.join(newLibva, "libva-drm.so.2.2300.0"),
+ "libva-drm 2.23",
+ );
+ await symlink("libva.so.2.2300.0", path.join(newLibva, "libva.so.2"));
+ await symlink("libva-drm.so.2.2300.0", path.join(newLibva, "libva-drm.so.2"));
const excluded = await finalizeLinuxAppImage(image, {
outputPlugin: plugin,
unsigned: true,
webview: false,
env: {
+ CAP_APPIMAGE_LIBVA_DIR: newLibva,
PATH: "/usr/bin",
TAURI_SIGNING_PRIVATE_KEY: "key",
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: "password",
@@ -872,3 +895,167 @@ test("webview-free finalization hands the pruned AppDir to the output plugin", a
assert.ok(excluded.includes("usr/share/glib-2.0"));
assert.deepEqual(await readFile(image), finalImage());
});
+
+async function directAppDir(root) {
+ const appDir = path.join(root, "Cap's AppDir");
+ await mkdir(path.join(appDir, "usr/bin"), { recursive: true });
+ await writeFile(
+ path.join(appDir, "usr/bin/Cap"),
+ `#!/bin/sh
+printf '%s\\0' "$PWD" "\${OWD-unset}" "\${LD_LIBRARY_PATH-unset}" "\${PATH%%:*}" "\${XDG_DATA_DIRS%%:*}" "\${PYTHONHOME-unset}" "$@"
+exit 23
+`,
+ { mode: 0o755 },
+ );
+ await writeFile(path.join(appDir, "AppRun"), "generic launcher", {
+ mode: 0o755,
+ });
+ await preserveAppImageWorkingDirectory(appDir, { direct: true });
+ return appDir;
+}
+
+test("the GPUI AppRun starts Cap directly without the generic launcher's library path", async (t) => {
+ const { root } = await fixture(t);
+ const appDir = await directAppDir(root);
+ assert.equal(
+ await readFile(path.join(appDir, "AppRun"), "utf8"),
+ directAppRunWrapper,
+ );
+ await preserveAppImageWorkingDirectory(appDir, { direct: true });
+ const caller = path.join(root, "caller's directory");
+ await mkdir(caller);
+ const args = ["--cap-cli", "two words", "$(literal)"];
+ const env = { ...process.env, PATH: "/usr/bin:/bin" };
+ delete env.LD_LIBRARY_PATH;
+ delete env.PYTHONHOME;
+ const result = spawnSync(path.join(appDir, "AppRun"), args, {
+ cwd: caller,
+ env,
+ });
+ assert.equal(result.status, 23, result.stderr.toString());
+ const [cwd, owd, libraries, firstPath, firstData, pythonHome, ...rest] =
+ result.stdout.toString().split("\0");
+ const real = await realpath(appDir);
+ assert.equal(cwd, path.join(real, "usr"));
+ assert.equal(owd, await realpath(caller));
+ assert.equal(firstPath, path.join(real, "usr/bin"));
+ assert.equal(firstData, path.join(real, "usr/share"));
+ assert.equal(pythonHome, "unset");
+ assert.deepEqual(rest, [...args, ""]);
+ // This machine either has libva, so nothing is added, or it does not, so
+ // only the bundled libva directory is.
+ assert.ok(
+ libraries === "unset" || libraries === path.join(real, "usr/lib/cap/libva"),
+ libraries,
+ );
+});
+
+test("the bundled libva moves out of RUNPATH's reach, symlinks and all", async (t) => {
+ const { root } = await fixture(t);
+ const libraries = path.join(root, "usr/lib");
+ await mkdir(libraries, { recursive: true });
+ for (const name of [
+ "libva.so.2.2100.0",
+ "libva-drm.so.2.2100.0",
+ "libvarlink.so.0",
+ "libavutil.so.59",
+ ]) {
+ await writeFile(path.join(libraries, name), name);
+ }
+ await symlink("libva.so.2.2100.0", path.join(libraries, "libva.so.2"));
+ await symlink(
+ "libva-drm.so.2.2100.0",
+ path.join(libraries, "libva-drm.so.2"),
+ );
+ assert.deepEqual(await isolateBundledLibva(root), [
+ "libva-drm.so.2",
+ "libva-drm.so.2.2100.0",
+ "libva.so.2",
+ "libva.so.2.2100.0",
+ ]);
+ assert.deepEqual((await readdir(libraries)).sort(), [
+ "cap",
+ "libavutil.so.59",
+ "libvarlink.so.0",
+ ]);
+ assert.equal(
+ await readFile(path.join(libraries, "cap/libva/libva.so.2"), "utf8"),
+ "libva.so.2.2100.0",
+ );
+ assert.deepEqual(await isolateBundledLibva(root), []);
+});
+
+test("the bundled libva is replaced by the fallback directory's", async (t) => {
+ const { root } = await fixture(t);
+ const libraries = path.join(root, "usr/lib");
+ const replacement = path.join(root, "new");
+ await mkdir(libraries, { recursive: true });
+ await mkdir(replacement);
+ await writeFile(path.join(libraries, "libva.so.2"), "old");
+ await writeFile(path.join(libraries, "libva-drm.so.2"), "old");
+ await writeFile(path.join(replacement, "libva.so.2.2300.0"), "new");
+ await writeFile(path.join(replacement, "libva-drm.so.2.2300.0"), "new drm");
+ await writeFile(path.join(replacement, "README"), "not a library");
+ await symlink("libva.so.2.2300.0", path.join(replacement, "libva.so.2"));
+ await symlink(
+ "libva-drm.so.2.2300.0",
+ path.join(replacement, "libva-drm.so.2"),
+ );
+ await isolateBundledLibva(root, { replacement });
+ const fallback = path.join(libraries, "cap/libva");
+ assert.deepEqual((await readdir(fallback)).sort(), [
+ "libva-drm.so.2",
+ "libva-drm.so.2.2300.0",
+ "libva.so.2",
+ "libva.so.2.2300.0",
+ ]);
+ assert.equal(
+ await readlink(path.join(fallback, "libva.so.2")),
+ "libva.so.2.2300.0",
+ );
+ assert.equal(
+ await readFile(path.join(fallback, "libva.so.2"), "utf8"),
+ "new",
+ );
+
+ const incomplete = path.join(root, "incomplete");
+ await mkdir(incomplete);
+ await writeFile(path.join(incomplete, "libva.so.2"), "new");
+ await assert.rejects(
+ isolateBundledLibva(root, { replacement: incomplete }),
+ /has no libva-drm\.so\.2/,
+ );
+});
+
+test("the pinned fallback libva is checked before it is unpacked", async (t) => {
+ const { root } = await fixture(t);
+ const unpacked = [];
+ const run = async (command, args) => {
+ assert.equal(command, "dpkg-deb");
+ unpacked.push(path.basename(args[1]));
+ };
+ const bytes = new Map(
+ fallbackLibvaPackages.map(({ url }) => [url, Buffer.from(url)]),
+ );
+ const download = async (url) => ({
+ ok: true,
+ status: 200,
+ arrayBuffer: async () => bytes.get(url),
+ });
+ await assert.rejects(
+ fallbackLibvaDirectory(root, { env: {}, run, download }),
+ /has sha256 [0-9a-f]{64}, expected/,
+ );
+ assert.deepEqual(unpacked, []);
+ assert.equal(
+ await fallbackLibvaDirectory(root, {
+ env: { CAP_APPIMAGE_LIBVA_DIR: "/given" },
+ run,
+ download,
+ }),
+ "/given",
+ );
+ for (const { url } of fallbackLibvaPackages) {
+ assert.match(url, /libva(-drm)?2_2\.23\.0-1ubuntu1_amd64\.deb$/);
+ }
+});
diff --git a/scripts/finalize-linux-gpui-packages.mjs b/scripts/finalize-linux-gpui-packages.mjs
index f9daf4ffd..5c56d3941 100644
--- a/scripts/finalize-linux-gpui-packages.mjs
+++ b/scripts/finalize-linux-gpui-packages.mjs
@@ -1,3 +1,4 @@
+import { createHash } from "node:crypto";
import {
mkdir,
mkdtemp,
@@ -13,11 +14,26 @@ import {
import path from "node:path";
import { pathToFileURL } from "node:url";
import {
+ notifyStartupInDesktopEntries,
runCommand,
signUpdaterArtifact,
withoutUpdaterSecrets,
} from "./finalize-linux-appimage.mjs";
+const DESKTOP_ENTRY = "usr/share/applications/Cap.desktop";
+
+export async function withUpdatedChecksums(md5sums, root, files) {
+ const lines = md5sums.split("\n");
+ for (const file of files) {
+ const digest = createHash("md5")
+ .update(await readFile(path.join(root, file)))
+ .digest("hex");
+ const index = lines.findIndex((line) => line.endsWith(` ${file}`));
+ if (index >= 0) lines[index] = `${digest} ${file}`;
+ }
+ return lines.join("\n");
+}
+
export const webviewDebPackages = new Set([
"libwebkit2gtk-4.1-0",
"libgtk-3-0",
@@ -315,6 +331,7 @@ export async function finalizeGpuiDeb(
const linked = await linkDuplicateLibraries(
path.join(root, "usr", "lib", "cap"),
);
+ const notified = await notifyStartupInDesktopEntries(root, [DESKTOP_ENTRY]);
const control = path.join(root, "DEBIAN", "control");
await verifyDebGlibc(root, await readFile(control, "utf8"), {
env: tools,
@@ -328,6 +345,16 @@ export async function finalizeGpuiDeb(
),
);
const md5sums = path.join(root, "DEBIAN", "md5sums");
+ if (notified.length > 0 && (await stat(md5sums).catch(() => null))) {
+ await writeFile(
+ md5sums,
+ await withUpdatedChecksums(
+ await readFile(md5sums, "utf8"),
+ root,
+ notified,
+ ),
+ );
+ }
if (linked.length > 0 && (await stat(md5sums).catch(() => null))) {
await writeFile(
md5sums,
@@ -430,6 +457,7 @@ export async function finalizeGpuiRpm(
await mkdir(path.join(top, directory), { recursive: true });
}
await run("bsdtar", ["-xf", rpm, "-C", staging], { env: tools });
+ await notifyStartupInDesktopEntries(staging, [DESKTOP_ENTRY]);
const linked = new Set(
(
await linkDuplicateLibraries(path.join(staging, "usr", "lib", "cap"))
diff --git a/scripts/finalize-linux-gpui-packages.test.mjs b/scripts/finalize-linux-gpui-packages.test.mjs
index eabee5c86..0ff62ad07 100644
--- a/scripts/finalize-linux-gpui-packages.test.mjs
+++ b/scripts/finalize-linux-gpui-packages.test.mjs
@@ -1,5 +1,6 @@
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
+import { createHash } from "node:crypto";
import {
lstat,
mkdir,
@@ -12,6 +13,10 @@ import {
import { tmpdir } from "node:os";
import path from "node:path";
import { test } from "node:test";
+import {
+ notifyStartupInDesktopEntries,
+ withStartupNotify,
+} from "./finalize-linux-appimage.mjs";
import {
finalizeGpuiDeb,
finalizeGpuiRpm,
@@ -23,6 +28,7 @@ import {
withoutLinkedFiles,
withoutWebviewDebDependencies,
withoutWebviewRpmRequirements,
+ withUpdatedChecksums,
} from "./finalize-linux-gpui-packages.mjs";
const available = (command) =>
@@ -677,3 +683,42 @@ for (const payload of ["usr/bin/Cap", "usr/lib/cap/libmedia.so"]) {
},
);
}
+
+test("Cap's desktop entries ask for startup notification, once", async (t) => {
+ const shipped = `[Desktop Entry]
+Categories=AudioVideo;Video;
+Exec=Cap
+StartupWMClass=Cap
+Name=Cap
+Type=Application
+`;
+ const notified = withStartupNotify(shipped);
+ assert.match(notified, /^\[Desktop Entry\]\nStartupNotify=true\n/);
+ assert.equal(withStartupNotify(notified), notified);
+ assert.equal(
+ withStartupNotify(
+ shipped.replace("Exec=Cap\n", "Exec=Cap\nStartupNotify=false\n"),
+ ),
+ shipped.replace("Exec=Cap\n", "Exec=Cap\nStartupNotify=true\n"),
+ );
+
+ const root = await mkdtemp(path.join(tmpdir(), "cap-desktop-entry-"));
+ t.after(() => rm(root, { recursive: true, force: true }));
+ const entry = "usr/share/applications/Cap.desktop";
+ await mkdir(path.join(root, "usr/share/applications"), { recursive: true });
+ await writeFile(path.join(root, entry), shipped);
+ assert.deepEqual(
+ await notifyStartupInDesktopEntries(root, [entry, "missing.desktop"]),
+ [entry],
+ );
+ assert.deepEqual(await notifyStartupInDesktopEntries(root, [entry]), []);
+ const sums = await withUpdatedChecksums(
+ `0123 usr/bin/Cap\nffff ${entry}\n`,
+ root,
+ [entry],
+ );
+ const digest = createHash("md5")
+ .update(await readFile(path.join(root, entry)))
+ .digest("hex");
+ assert.equal(sums, `0123 usr/bin/Cap\n${digest} ${entry}\n`);
+});
diff --git a/scripts/linux-desktop-harness/README.md b/scripts/linux-desktop-harness/README.md
index 9438b2a4a..42e7d24a1 100644
--- a/scripts/linux-desktop-harness/README.md
+++ b/scripts/linux-desktop-harness/README.md
@@ -9,7 +9,8 @@ runs it on every pull request that touches the desktop app.
| --- | --- | --- |
| `gnome` | Ubuntu 26.04, GNOME 50 headless, AppIndicator, xdg-desktop-portal 1.21+ | Ubuntu users: shortcut approval dialog, `ShortcutsChanged`, Share Screen dialog |
| `hyprland` | Arch, Hyprland on vkms, waybar tray, xdg-desktop-portal-hyprland | Omarchy users: the portal refuses host apps ("An app id is required"), tray Stop control, no configure after a remap |
-| `sway`, `niri` | Arch on vkms | Exploration only; their portals need a render node vkms does not have |
+| `kde` | Arch, KWin 6 on its virtual backend, plasmashell tray, xdg-desktop-portal-kde | Plasma users: the screen-share dialog (its screens are buttons), the tray Stop control with `LAUNCH=dock`. Needs a DRM render node (`modprobe vgem`): KWin streams screens only when it composites with OpenGL, so it does not run on CI's runners. KWin's virtual backend takes no injected keys, so a shell launch's Stop shortcut is approved (`APPROVE_SHORTCUT_AFTER`) but not pressed |
+| `sway`, `niri` | Arch on vkms | Exploration only: their portals need a render node vkms does not have, and niri refuses software rendering |
Each run (`run-desktop.sh <desktop> <package> <out-dir> [KEY=VALUE...]`):
@@ -27,11 +28,49 @@ Each run (`run-desktop.sh <desktop> <package> <out-dir> [KEY=VALUE...]`):
`mutter-pointer.py`);
4. writes `result.txt`, which `check-results.sh` turns into pass or fail.
+`SCENARIO=clipboard` (niri, Hyprland) opens Cap's teleprompter, pastes text
+another app copied with Ctrl+V and copies it back out with Ctrl+A, Ctrl+C,
+pressed through the compositor's virtual keyboard (`wtype`).
+`GNOME_TRAY=0` runs GNOME in its plain session without the AppIndicator
+extension and without the harness's own tray host, as Fedora ships it. With
+`CAP_GPUI_PORTAL_APP_ID=0` and `LAUNCH=terminal` the portal grants no shortcut
+either, and the scenario expects Cap to record with its controls visible
+(`attempt1_control=visible`): it finds the controls window and clicks their red Stop button with the pointer (needs `GNOME_UNSAFE=1`).
`SCENARIO=reopen` launches Cap from a terminal and again from the app grid,
-which must hand over to the running instance. `SCENARIO=settings-close` instead opens Cap's settings and closes it with the
+which must hand over to the running instance. `SCENARIO=raise` (with
+`GNOME_UNSAFE=1`) focuses Cap's settings, then has GNOME Shell launch Cap the
+way the app grid does; the activation token the relaunch hands over must bring
+onboarding, or the main window with `RAISE_WINDOW=main`, to the front.
+`MUTTER_DEBUG=focus,startup` writes mutter's reasoning to
+`compositor.debug.log`. `SCENARIO=settings-close` instead opens Cap's settings and closes it with the
window's own close button (GNOME draws no title bar for it). Clicks go through
`mutter-pointer.py`, one virtual pointer that exists before Cap starts: gpui
binds the seat's pointer once, so a pointer created per click never reaches it.
+`SCENARIO=pick-background` (with `GNOME_UNSAFE=1`) records a clip, opens it in
+the editor and presses the Image background's picker
+(`CAP_GPUI_AUTO_PICK_BACKGROUND=1`), then types a picture's path into the
+portal's file chooser through `mutter-keys.py --type` and presses Select.
+Mutter's RemoteDesktop drops the first key of each session, so `mutter-keys.py`
+leads every session with a Shift tap.
+`SCENARIO=screenshot` takes a screenshot of the display the way the
+screenshot mode does, with Cap focused as after a click, and allows it when
+the portal asks (GNOME Shell's question is answered from the Shell, so it needs
+`GNOME_UNSAFE=1`; elsewhere the permission is granted up front because the GTK
+portal's dialog aborts in the container). It checks a PNG of the display's size (`SCREEN`, 1920x1080) was saved.
+`PRELAUNCH=1` runs Cap once before, so a `LAUNCH=terminal` run finds the
+`so.cap.Cap.desktop` entry GNOME matches Cap's windows to.
+`SCENARIO=wallpaper` records a clip, opens it in the editor and imports the
+desktop's wallpaper as its background (`CAP_GPUI_AUTO_DESKTOP_BACKGROUND=1`).
+GNOME keeps its default JPEG XL wallpaper; `WALLPAPER=swaybg` starts swaybg
+with a PNG first, as Omarchy does on Hyprland.
+`SCENARIO=register` launches Cap from a shell `ROUNDS` times, each time once
+with its `so.cap.Cap.desktop` entry removed and once with it in place, and
+counts the launches that registered the app id with the portals.
+`AREA=x,y,width,height` makes the start-stop scenario record that area of the
+display and check the video is its size.
+`SCENARIO=export` records a clip, opens it in the editor and exports it to an
+MP4 and to a GIF (`CAP_GPUI_AUTO_EXPORT`), checking the length each file
+declares.
`report.sh <out-dir>` prints the results with Cap's own log lines. The
Hyprland image needs `modprobe vkms` on the host. With `GNOME_UNSAFE=1`,
@@ -41,3 +80,38 @@ Harness-only workarounds (not in the app): Mesa's device_select Vulkan layer is
disabled on the vkms images because it crashes on their virtual devices, and
the GNOME session gets a target bound to `graphical-session.target`, which a
real login activates.
+
+## Checks without a desktop
+
+- `x11-camera.sh <cap-gpui> <ffmpeg lib dir> <out-dir>` records the display on
+ Xvfb (X11 capture, no portal) with a camera: a v4l2loopback device that
+ ffmpeg feeds a 1280x720 test pattern. Cap waits for the Stop shortcut to start
+ and to stop, pressed with real X input (xdotool), well before its 60 s
+ automatic stop. It checks the camera track is 1280x720 and as long as the
+ display track, about five seconds, and fails if any result is missing. It
+ needs root for modprobe, and runs in the Hyprland job, whose hosted runner
+ can build the module.
+- `x11-capture.sh <cap-gpui> <ffmpeg lib dir> <out-dir>` records the display,
+ an area and another app's window on Xvfb (MIT-SHM capture, no portal),
+ pressing the Stop shortcut with real X input where Cap waits for it, records
+ the microphone and system audio from a tone a private PulseAudio plays, takes
+ a screenshot, and checks the size, length and loudness of each result. It also
+ checks the window picker offers the other app's window rather than one of
+ Cap's own.
+- `x11-stale-wayland.sh <cap-gpui> <ffmpeg lib dir> <out-dir>` starts Cap on
+ Xvfb with `WAYLAND_DISPLAY` naming a socket nothing listens on, as a systemd
+ user environment keeps it after a Wayland session ends, and checks Cap falls
+ back to the X display instead of failing to start.
+
+- `niri-screencast/run.sh` records through Cap's PipeWire capture loop from a
+ producer that negotiates and shares frames the way niri 26.04 does: a
+ mandatory DMA-BUF modifier, DMA-BUF buffers the consumer must map itself,
+ and a chunk size of 1 (GitHub #2941). It needs a host with PipeWire and
+ WirePlumber, and runs in the GNOME job.
+- `appimage-pipewire/run.sh <Cap.AppImage> <out-dir>` connects a screen
+ capture stream inside NixOS's `appimage-run`, through the AppImage's real
+ launchers, with the AppDir as the given AppImage shipped it and as
+ `scripts/finalize-linux-appimage.mjs` lays it out now. A stand-in for Cap
+ links the bundled FFmpeg the way Cap does, so the bundled libva loads ahead
+ of the host's PipeWire plugins, as in Cap.
+
diff --git a/scripts/linux-desktop-harness/appimage-pipewire/probe.c b/scripts/linux-desktop-harness/appimage-pipewire/probe.c
new file mode 100644
index 000000000..6b10b907a
--- /dev/null
+++ b/scripts/linux-desktop-harness/appimage-pipewire/probe.c
@@ -0,0 +1,61 @@
+// Stands in for Cap inside an AppImage: it links the bundled FFmpeg the way
+// Cap does (so the bundled libva loads with it, or the host's), then connects a
+// PipeWire screen-capture stream with Cap's format offer. pw_stream_connect
+// loads the host's video converter plugin, which links the host's FFmpeg.
+#define _GNU_SOURCE
+#include <errno.h>
+#include <link.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <pipewire/pipewire.h>
+#include <spa/param/video/format-utils.h>
+
+static int print_library(struct dl_phdr_info *info, size_t size, void *data) {
+ (void)size;
+ (void)data;
+ if (strstr(info->dlpi_name, "libva") || strstr(info->dlpi_name, "libavutil"))
+ printf("loaded %s\n", info->dlpi_name);
+ return 0;
+}
+
+// From the bundled libavutil, declared here so no FFmpeg headers are needed.
+const char *av_version_info(void);
+
+int main(int argc, char **argv) {
+ printf("bundled libavutil %s\n", av_version_info());
+ pw_init(&argc, &argv);
+ struct pw_thread_loop *loop = pw_thread_loop_new("probe", NULL);
+ struct pw_context *context = pw_context_new(pw_thread_loop_get_loop(loop), NULL, 0);
+ if (!context) {
+ printf("context failed: %s\n", strerror(errno));
+ return 1;
+ }
+ struct pw_core *core = pw_context_connect(context, NULL, 0);
+ if (!core) {
+ printf("core connect failed: %s\n", strerror(errno));
+ return 1;
+ }
+ struct pw_stream *stream = pw_stream_new(core, "cap-wayland-screen",
+ pw_properties_new(PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, "Capture",
+ PW_KEY_MEDIA_ROLE, "Screen", NULL));
+ uint8_t buffer[1024];
+ struct spa_pod_builder builder = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
+ const struct spa_pod *params[1];
+ params[0] = spa_pod_builder_add_object(&builder, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
+ SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
+ SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(7, SPA_VIDEO_FORMAT_BGRx, SPA_VIDEO_FORMAT_BGRx,
+ SPA_VIDEO_FORMAT_BGRA, SPA_VIDEO_FORMAT_RGBx, SPA_VIDEO_FORMAT_RGBA, SPA_VIDEO_FORMAT_RGB,
+ SPA_VIDEO_FORMAT_BGR),
+ SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(&SPA_RECTANGLE(1920, 1080),
+ &SPA_RECTANGLE(1, 1), &SPA_RECTANGLE(8192, 8192)),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(&SPA_FRACTION(30, 1),
+ &SPA_FRACTION(0, 1), &SPA_FRACTION(1000, 1)));
+ int result = pw_stream_connect(stream, PW_DIRECTION_INPUT, PW_ID_ANY,
+ PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params, 1);
+ printf("LD_LIBRARY_PATH=%s\n", getenv("LD_LIBRARY_PATH") ? getenv("LD_LIBRARY_PATH") : "(unset)");
+ dl_iterate_phdr(print_library, NULL);
+ printf("pw_stream_connect=%d (%s)\n", result, result < 0 ? strerror(-result) : "ok");
+ return result < 0 ? 2 : 0;
+}
diff --git a/scripts/linux-desktop-harness/appimage-pipewire/run.sh b/scripts/linux-desktop-harness/appimage-pipewire/run.sh
new file mode 100755
index 000000000..ce37cee21
--- /dev/null
+++ b/scripts/linux-desktop-harness/appimage-pipewire/run.sh
@@ -0,0 +1,97 @@
+#!/usr/bin/env bash
+# The AppImage's libraries against a NixOS PipeWire (GitHub #2941): a stand-in
+# for Cap that links the bundled FFmpeg connects a screen-capture stream
+# inside NixOS's appimage-run, once with the AppDir as shipped and once laid
+# out by scripts/finalize-linux-appimage.mjs. Prints key=value results.
+# Usage: run.sh <Cap.AppImage> <out-dir>
+set -euo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+repo=$(cd "$here/../../.." && pwd)
+appimage=$(realpath "$1")
+out=$(realpath -m "$2")
+mkdir -p "$out"
+work=$(mktemp -d)
+trap 'chmod -R u+w "$work" 2>/dev/null; find "$work" -mindepth 1 -delete 2>/dev/null' EXIT
+cd "$work"
+cp "$appimage" Cap.AppImage
+chmod +x Cap.AppImage
+./Cap.AppImage --appimage-extract >/dev/null
+chmod -R a+rX squashfs-root
+
+# Built like Cap: a host-ABI binary (Ubuntu's toolchain) that finds the
+# bundled FFmpeg through RUNPATH $ORIGIN/../lib.
+docker run --rm -v "$here":/src:ro -v "$work":/work -w /work ubuntu:24.04 bash -euc '
+ apt-get update -qq >/dev/null && apt-get install -y -qq gcc pkg-config libpipewire-0.3-dev >/dev/null
+ gcc -o probe /src/probe.c $(pkg-config --cflags --libs libpipewire-0.3) \
+ -L squashfs-root/usr/lib -l:libavutil.so.59 -Wl,--allow-shlib-undefined -Wl,-rpath,"\$ORIGIN/../lib"
+ chmod 755 probe'
+
+cp -a squashfs-root shipped
+cp -a squashfs-root fixed
+# The fixed layout is what scripts/finalize-linux-appimage.mjs ships: the
+# direct launcher and the pinned fallback libva (CAP_APPIMAGE_LIBVA_DIR to
+# use another).
+mkdir -p libva-work
+FINALIZE="$repo/scripts/finalize-linux-appimage.mjs" APPDIR_FIXED="$work/fixed" LIBVA_WORK="$work/libva-work" \
+ node --input-type=module -e '
+ const { isolateBundledLibva, directAppRunWrapper, fallbackLibvaDirectory } = await import(process.env.FINALIZE);
+ const { writeFile } = await import("node:fs/promises");
+ const replacement = await fallbackLibvaDirectory(process.env.LIBVA_WORK);
+ const moved = await isolateBundledLibva(process.env.APPDIR_FIXED, { replacement });
+ await writeFile(process.env.APPDIR_FIXED + "/AppRun", directAppRunWrapper, { mode: 0o755 });
+ console.log("moved " + moved.join(" ") + "; fallback from " + replacement);
+'
+for layout in shipped fixed; do
+ install -m 755 probe "$layout/usr/bin/Cap"
+done
+chmod -R a+rX "$work"
+# appimage-run takes AppImage files: the original runtime and each layout
+# packed the way the original was.
+offset=$(./Cap.AppImage --appimage-offset)
+head -c "$offset" Cap.AppImage >runtime
+docker run --rm -v "$work":/work -w /work ubuntu:24.04 bash -euc '
+ apt-get update -qq >/dev/null && apt-get install -y -qq squashfs-tools >/dev/null
+ for layout in shipped fixed; do
+ mksquashfs "$layout" "$layout.squashfs" -root-owned -noappend -comp zstd -quiet >/dev/null
+ cat runtime "$layout.squashfs" >"$layout.AppImage"
+ chmod 755 "$layout.AppImage"
+ done'
+
+docker run --rm --privileged -e PIPEWIRE_DEBUG -e SHOW_LINES -v "$work":/work:ro nixos/nix:latest bash -c '
+ export NIX_CONFIG="experimental-features = nix-command flakes
+sandbox = false
+filter-syscalls = false"
+ export XDG_RUNTIME_DIR=/tmp/xdg
+ mkdir -p $XDG_RUNTIME_DIR && chmod 700 $XDG_RUNTIME_DIR
+ nix build -o /tmp/pipewire nixpkgs#pipewire nixpkgs#appimage-run >/dev/null 2>&1
+ /tmp/pipewire/bin/pipewire >/tmp/pipewire.log 2>&1 &
+ sleep 4
+ run=$(ls -d /nix/store/*-appimage-run*/bin/appimage-run | head -1)
+ echo "versions $(/tmp/pipewire/bin/pipewire --version | tail -1)"
+ for layout in shipped fixed; do
+ echo "== $layout"
+ PIPEWIRE_DEBUG=${PIPEWIRE_DEBUG:-1} "$run" /work/$layout.AppImage >/tmp/$layout.log 2>&1
+ status=$?
+ grep -v "rtkit\|=====\|Uncompress\|is now installed" /tmp/$layout.log | tail -${SHOW_LINES:-40}
+ echo "exit $layout=$status"
+ done
+' 2>&1 | tee "$out/appimage-pipewire.log"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+: >"$out/result.txt"
+for layout in shipped fixed; do
+ if grep -q "^exit $layout=0" "$out/appimage-pipewire.log"; then
+ result "${layout}_connects" yes
+ else
+ result "${layout}_connects" no
+ fi
+ # What a program Cap starts (a file manager through xdg-open) inherits:
+ # any bundle library directory other than the libva fallback leaks Cap's
+ # libraries into it, which is why folders did not open on NixOS in 0.6.1.
+ libraries=$(sed -n "/^== $layout\$/,/^exit $layout=/p" "$out/appimage-pipewire.log" | sed -n 's/^LD_LIBRARY_PATH=//p')
+ if tr ':' '\n' <<<"$libraries" | grep '/appimage-run/' | grep -qv '/usr/lib/cap/libva$'; then
+ result "${layout}_children_get_bundle_libraries" yes
+ else
+ result "${layout}_children_get_bundle_libraries" no
+ fi
+done
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/arch-up.sh b/scripts/linux-desktop-harness/arch-up.sh
index 796ba750b..6a1a80288 100755
--- a/scripts/linux-desktop-harness/arch-up.sh
+++ b/scripts/linux-desktop-harness/arch-up.sh
@@ -40,16 +40,32 @@ CONF
env "${common[@]}" AQ_DRM_DEVICES="$kms" XDG_CURRENT_DESKTOP=Hyprland Hyprland
;;
niri)
- # vkms has no render node of its own; niri needs one named.
- render=$(ls /dev/dri/renderD* 2>/dev/null | head -1)
+ # vkms has no render node of its own; niri needs one named. On a host
+ # with no GPU, NIRI_RENDER_DEVICE=vkms renders on the vkms card itself,
+ # for a niri built to accept software rendering (niri-software.Dockerfile).
+ render=${NIRI_RENDER_DEVICE:-$(ls /dev/dri/renderD* 2>/dev/null | head -1)}
+ [[ $render == vkms ]] && render=$kms
cat >"$HOME/.config/niri/config.kdl" <<KDL
spawn-at-startup "sh" "-c" "$export_session; waybar"
debug {
render-drm-device "$render"
}
KDL
- systemd-run --user --unit=desktop --collect -p StandardOutput=file:"$out/compositor.log" -p StandardError=inherit \
- env "${common[@]}" XDG_CURRENT_DESKTOP=niri niri
+ # NIRI_OUTPUTS_OFF: outputs to switch off (a cloud GPU's EFI framebuffer),
+ # so the screen-share dialog has the one screen to pick.
+ for output in ${NIRI_OUTPUTS_OFF:-}; do
+ printf 'output "%s" {\n off\n}\n' "$output" >>"$HOME/.config/niri/config.kdl"
+ done
+ # The packaged session unit, as niri-session starts it: it brings up
+ # graphical-session.target, which the GNOME portal backend (niri's
+ # ScreenCast portal) requires.
+ mkdir -p "$HOME/.config/systemd/user/niri.service.d"
+ printf '[Service]\nStandardOutput=file:%s\nStandardError=inherit\nEnvironment=LIBSEAT_BACKEND=seatd XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=niri RUST_LOG=%s\n' \
+ "$out/compositor.log" "${NIRI_LOG:-niri=debug}" >"$HOME/.config/systemd/user/niri.service.d/harness.conf"
+ # GSK_RENDERER: the portal dialogs' GTK renderer (cairo on software GL).
+ [[ -n ${GSK_RENDERER:-} ]] && systemctl --user set-environment GSK_RENDERER="$GSK_RENDERER"
+ systemctl --user daemon-reload
+ systemctl --user start niri.service
;;
sway)
# xdg-desktop-portal-wlr: share the one output, as a person picks it.
diff --git a/scripts/linux-desktop-harness/arch.Dockerfile b/scripts/linux-desktop-harness/arch.Dockerfile
index 69fc07578..b2641e2d3 100644
--- a/scripts/linux-desktop-harness/arch.Dockerfile
+++ b/scripts/linux-desktop-harness/arch.Dockerfile
@@ -8,7 +8,7 @@ RUN pacman -Syu --noconfirm --needed \
systemd dbus seatd sway hyprland niri waybar \
xdg-desktop-portal xdg-desktop-portal-hyprland xdg-desktop-portal-gnome xdg-desktop-portal-wlr \
pipewire wireplumber pipewire-pulse mesa vulkan-swrast vulkan-icd-loader \
- python python-gobject glib2 procps-ng psmisc fuse2 ttf-dejavu \
+ python python-gobject glib2 procps-ng psmisc fuse2 ttf-dejavu wtype wl-clipboard swaybg \
&& pacman -Scc --noconfirm
RUN useradd -m -u 1001 -G seat,video,render tester \
&& mkdir -p /var/lib/systemd/linger && touch /var/lib/systemd/linger/tester \
diff --git a/scripts/linux-desktop-harness/atspi-press.py b/scripts/linux-desktop-harness/atspi-press.py
index 86877a9fc..4d96529da 100644
--- a/scripts/linux-desktop-harness/atspi-press.py
+++ b/scripts/linux-desktop-harness/atspi-press.py
@@ -44,7 +44,11 @@ def find(scope_re, button_re):
name = node.get_name() or ""
except Exception:
continue
- if role in (Atspi.Role.PUSH_BUTTON, Atspi.Role.TOGGLE_BUTTON) and button_re.search(name):
+ # Qt reports its buttons with the plain "button" role.
+ is_button = role in (Atspi.Role.PUSH_BUTTON, Atspi.Role.TOGGLE_BUTTON) or (
+ node.get_role_name() == "button"
+ )
+ if is_button and button_re.search(name):
return node
return None
diff --git a/scripts/linux-desktop-harness/gnome-up.sh b/scripts/linux-desktop-harness/gnome-up.sh
index 82373c111..0fb548484 100644
--- a/scripts/linux-desktop-harness/gnome-up.sh
+++ b/scripts/linux-desktop-harness/gnome-up.sh
@@ -8,8 +8,12 @@ export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
out=${HARNESS_OUT:-/out}
mkdir -p "$out"
+# GNOME_GPU=1 leaves rendering to the host's GPU (passed in with
+# DOCKER_EXTRA); otherwise everything renders in software, as on CI.
+software=(LIBGL_ALWAYS_SOFTWARE=1)
+[[ ${GNOME_GPU:-} == 1 ]] && software=()
systemctl --user set-environment XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=ubuntu:GNOME \
- XDG_SESSION_DESKTOP=ubuntu GNOME_SHELL_SESSION_MODE=ubuntu LIBGL_ALWAYS_SOFTWARE=1
+ XDG_SESSION_DESKTOP=ubuntu GNOME_SHELL_SESSION_MODE=ubuntu "${software[@]}"
# A logged-in GNOME session has graphical-session.target active, and the GNOME
# portal backend will not start without it. It refuses a manual start, so bind
# a harness target to it the way gnome-session's own targets do.
@@ -22,7 +26,15 @@ Before=graphical-session.target
UNIT
systemctl --user daemon-reload
systemctl --user start cap-harness-session.target
-gsettings set org.gnome.shell enabled-extensions "['[email protected]']"
+# GNOME_TRAY=0 runs GNOME as Fedora ships it: no AppIndicator extension, so
+# no tray for the recording Stop control.
+if [[ ${GNOME_TRAY:-1} == 0 ]]; then
+ gsettings set org.gnome.shell enabled-extensions "[]"
+ gsettings set org.gnome.shell disabled-extensions "['[email protected]']"
+ systemctl --user set-environment GNOME_SHELL_SESSION_MODE=user
+else
+ gsettings set org.gnome.shell enabled-extensions "['[email protected]']"
+fi
# The virtual pointer starts at the top-left corner; the hot corner would open
# the overview under every injected click.
gsettings set org.gnome.desktop.interface enable-hot-corners false
@@ -32,6 +44,7 @@ gsettings set org.gnome.desktop.interface toolkit-accessibility true
systemctl --user reset-failed gnome-headless.service >/dev/null 2>&1 || true
systemd-run --user --unit=gnome-headless --collect \
-p StandardOutput=file:"$out/compositor.log" -p StandardError=inherit \
+ ${MUTTER_DEBUG:+-p Environment=MUTTER_DEBUG=$MUTTER_DEBUG -p StandardError=file:"$out/compositor.debug.log"} \
gnome-shell --headless --wayland --no-x11 --virtual-monitor 1920x1080 ${GNOME_UNSAFE:+--unsafe-mode}
for _ in $(seq 600); do
@@ -44,7 +57,7 @@ for _ in $(seq 300); do
--method org.freedesktop.DBus.Properties.Get org.gnome.Shell ShellVersion >/dev/null 2>&1 && break
sleep 0.1
done
-export WAYLAND_DISPLAY=wayland-0 XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=ubuntu:GNOME XDG_SESSION_DESKTOP=ubuntu GSK_RENDERER=cairo LIBGL_ALWAYS_SOFTWARE=1
+export WAYLAND_DISPLAY=wayland-0 XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=ubuntu:GNOME XDG_SESSION_DESKTOP=ubuntu GSK_RENDERER=cairo "${software[@]}"
systemctl --user set-environment WAYLAND_DISPLAY=wayland-0
-dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_SESSION_TYPE XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP GSK_RENDERER LIBGL_ALWAYS_SOFTWARE
+dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_SESSION_TYPE XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP GSK_RENDERER "${software[@]%%=*}"
echo "gnome-up: $(gnome-shell --version) on wayland-0"
diff --git a/scripts/linux-desktop-harness/gnome.Dockerfile b/scripts/linux-desktop-harness/gnome.Dockerfile
index 6e82b1b48..44af40f5e 100644
--- a/scripts/linux-desktop-harness/gnome.Dockerfile
+++ b/scripts/linux-desktop-harness/gnome.Dockerfile
@@ -12,7 +12,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
gnome-shell-extension-ubuntu-dock gnome-settings-daemon \
gnome-shell-extension-appindicator pipewire wireplumber pipewire-pulse \
mesa-vulkan-drivers libgl1-mesa-dri libegl-mesa0 python3 python3-gi gir1.2-glib-2.0 \
- at-spi2-core gir1.2-atspi-2.0 \
+ at-spi2-core gir1.2-atspi-2.0 gnome-backgrounds \
procps psmisc ca-certificates xdg-utils \
&& apt-get clean
RUN useradd -m -u 1001 tester \
diff --git a/scripts/linux-desktop-harness/kde-up.sh b/scripts/linux-desktop-harness/kde-up.sh
new file mode 100755
index 000000000..172fa2c85
--- /dev/null
+++ b/scripts/linux-desktop-harness/kde-up.sh
@@ -0,0 +1,59 @@
+#!/usr/bin/env bash
+# Runs as the test user inside the systemd KDE container: KWin on its virtual
+# backend at 1920x1080 with plasmashell (the tray host) and the KDE portal, in
+# software. Exports the session to the user bus and systemd manager.
+set -euo pipefail
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+out=${HARNESS_OUT:-/out}
+mkdir -p "$out" "$HOME/.config/systemd/user"
+session=(XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=KDE XDG_SESSION_DESKTOP=KDE KDE_FULL_SESSION=true
+ KDE_SESSION_VERSION=6 LIBGL_ALWAYS_SOFTWARE=1 QT_QPA_PLATFORM=wayland QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
+ KWIN_COMPOSE=O2)
+systemctl --user set-environment "${session[@]}"
+# The KDE portal backend, like GNOME's, waits for graphical-session.target.
+cat >"$HOME/.config/systemd/user/cap-harness-session.target" <<'UNIT'
+[Unit]
+Description=Cap harness graphical session
+BindsTo=graphical-session.target
+Before=graphical-session.target
+UNIT
+systemctl --user daemon-reload
+systemctl --user start cap-harness-session.target
+
+# KWin streams screens only when it composites with OpenGL, which its virtual
+# backend does only on a DRM render node (load vgem on a host without a GPU).
+ls /dev/dri/renderD* >/dev/null 2>&1 ||
+ { echo "kde-up: no DRM render node; run 'modprobe vgem' on the host" >&2; exit 1; }
+systemctl --user reset-failed kwin-headless.service >/dev/null 2>&1 || true
+# KWin brings its own Qt platform; the session's is for the apps it hosts.
+kwin_env=()
+for variable in "${session[@]}"; do
+ [[ $variable == QT_QPA_PLATFORM=* ]] || kwin_env+=("$variable")
+done
+systemd-run --user --unit=kwin-headless --collect \
+ -p StandardOutput=file:"$out/compositor.log" -p StandardError=inherit \
+ env "${kwin_env[@]}" kwin_wayland --virtual --width 1920 --height 1080 --no-lockscreen --socket wayland-0
+for _ in $(seq 600); do
+ [[ -S "$XDG_RUNTIME_DIR/wayland-0" ]] && break
+ sleep 0.1
+done
+[[ -S "$XDG_RUNTIME_DIR/wayland-0" ]] || { echo "kde-up: no Wayland socket" >&2; exit 1; }
+export WAYLAND_DISPLAY=wayland-0 "${session[@]}"
+systemctl --user set-environment WAYLAND_DISPLAY=wayland-0
+dbus-update-activation-environment --systemd WAYLAND_DISPLAY "${session[@]%%=*}"
+
+systemd-run --user --unit=plasmashell --collect \
+ -p StandardOutput=file:"$out/plasmashell.log" -p StandardError=inherit plasmashell
+tray=no
+for _ in $(seq 300); do
+ if gdbus call --session --dest org.kde.StatusNotifierWatcher --object-path /StatusNotifierWatcher \
+ --method org.freedesktop.DBus.Properties.Get org.kde.StatusNotifierWatcher \
+ IsStatusNotifierHostRegistered 2>/dev/null | grep -q "<true>"; then
+ tray=yes
+ break
+ fi
+ sleep 0.1
+done
+[[ $tray == yes ]] || { echo "kde-up: plasmashell registered no tray host" >&2; exit 1; }
+echo "kde-up: $(kwin_wayland --version 2>/dev/null) on wayland-0"
diff --git a/scripts/linux-desktop-harness/kde.Dockerfile b/scripts/linux-desktop-harness/kde.Dockerfile
new file mode 100644
index 000000000..6c7d2a40a
--- /dev/null
+++ b/scripts/linux-desktop-harness/kde.Dockerfile
@@ -0,0 +1,18 @@
+# Arch Linux with KDE Plasma's compositor, shell and portal: KWin on its
+# virtual backend (no display device), plasmashell as the tray host and
+# kglobalacceld for global shortcuts, rendering with llvmpipe. systemd is PID 1
+# for the same reasons as the other images.
+FROM archlinux:latest
+RUN pacman -Syu --noconfirm --needed \
+ systemd dbus kwin plasma-workspace plasma-desktop kglobalacceld xdg-desktop-portal xdg-desktop-portal-kde \
+ breeze qt6-wayland pipewire wireplumber pipewire-pulse mesa vulkan-swrast vulkan-icd-loader \
+ at-spi2-core python python-gobject glib2 procps-ng psmisc fuse2 ttf-dejavu wl-clipboard \
+ && pacman -Scc --noconfirm
+RUN useradd -m -u 1001 tester \
+ && mkdir -p /var/lib/systemd/linger && touch /var/lib/systemd/linger/tester \
+ && systemctl mask systemd-binfmt.service proc-sys-fs-binfmt_misc.automount \
+ proc-sys-fs-binfmt_misc.mount systemd-udevd.service systemd-udevd-kernel.socket \
+ systemd-udevd-control.socket [email protected] console-getty.service \
+ systemd-firstboot.service systemd-homed.service
+STOPSIGNAL SIGRTMIN+3
+CMD ["/sbin/init"]
diff --git a/scripts/linux-desktop-harness/mutter-keys.py b/scripts/linux-desktop-harness/mutter-keys.py
index 7ee3bd5e4..787ce77aa 100644
--- a/scripts/linux-desktop-harness/mutter-keys.py
+++ b/scripts/linux-desktop-harness/mutter-keys.py
@@ -3,6 +3,7 @@
keyboard reaches GNOME Shell (and so its global shortcut grabs).
Usage: mutter-keys.py Control_L+Shift_L+F9 [--hold-ms 120]
+ mutter-keys.py --type TEXT (printable ASCII, one key per character)
"""
import sys
import time
@@ -23,8 +24,14 @@ for n in range(1, 13):
KEYSYMS[f"F{n}"] = 0xFFBE + n - 1
+def keysym(key: str) -> int:
+ # A printable ASCII character's keysym is its code point.
+ return KEYSYMS[key] if key in KEYSYMS else ord(key)
+
+
def main() -> int:
- chord = sys.argv[1].split("+")
+ typing = sys.argv[1] == "--type"
+ chord = [] if typing else sys.argv[1].split("+")
hold = 0.12
if "--hold-ms" in sys.argv:
hold = int(sys.argv[sys.argv.index("--hold-ms") + 1]) / 1000
@@ -54,12 +61,27 @@ def main() -> int:
call("Start")
time.sleep(0.3)
+ # A fresh session can drop its first key, so a Shift tap nothing reacts
+ # to goes first.
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (KEYSYMS["Shift_L"], True)))
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (KEYSYMS["Shift_L"], False)))
+ time.sleep(0.05)
+ if typing:
+ for character in sys.argv[2]:
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (keysym(character), True)))
+ time.sleep(0.02)
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (keysym(character), False)))
+ time.sleep(0.03)
+ time.sleep(0.2)
+ call("Stop")
+ print(f"typed {sys.argv[2]!r}")
+ return 0
for key in chord:
- call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (KEYSYMS[key], True)))
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (keysym(key), True)))
time.sleep(0.03)
time.sleep(hold)
for key in reversed(chord):
- call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (KEYSYMS[key], False)))
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (keysym(key), False)))
time.sleep(0.03)
time.sleep(0.2)
call("Stop")
diff --git a/scripts/linux-desktop-harness/niri-screencast/producer.c b/scripts/linux-desktop-harness/niri-screencast/producer.c
new file mode 100644
index 000000000..be45042ca
--- /dev/null
+++ b/scripts/linux-desktop-harness/niri-screencast/producer.c
@@ -0,0 +1,201 @@
+// A screencast producer that shares frames the way niri 26.04 does
+// (src/screencasting/pw_utils.rs): BGRx with a MANDATORY modifier offered
+// from a list (DONT_FIXATE), fixated to one modifier in a second round,
+// DMA-BUF buffers only, flagged READWRITE but not MAPPABLE, maxsize 1, and a
+// chunk size of 1. The buffers are memfds, which a consumer maps like a
+// LINEAR DMA-BUF. Each frame is a solid colour that steps every frame, so a
+// consumer can check what it read.
+//
+// Prints "node <id>" once the stream is ready, then runs until killed.
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <pipewire/pipewire.h>
+#include <spa/param/video/format-utils.h>
+#include <spa/param/buffers.h>
+#include <spa/pod/builder.h>
+#include <spa/pod/iter.h>
+
+#define WIDTH 640
+#define HEIGHT 360
+#define STRIDE (WIDTH * 4)
+#define LINEAR 0LL
+#define X_TILED 0x0100000000000001LL
+
+struct data {
+ struct pw_main_loop *loop;
+ struct pw_stream *stream;
+ struct spa_hook listener;
+ struct spa_source *timer;
+ int fixated;
+ uint32_t frame;
+ int announced;
+};
+
+static const struct spa_pod *video_format(struct spa_pod_builder *b, const int64_t *modifiers,
+ uint32_t count, int dont_fixate) {
+ struct spa_pod_frame object, choice;
+ spa_pod_builder_push_object(b, &object, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
+ spa_pod_builder_add(b, SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
+ SPA_FORMAT_VIDEO_format, SPA_POD_Id(SPA_VIDEO_FORMAT_BGRx), 0);
+ spa_pod_builder_prop(b, SPA_FORMAT_VIDEO_modifier,
+ SPA_POD_PROP_FLAG_MANDATORY | (dont_fixate ? SPA_POD_PROP_FLAG_DONT_FIXATE : 0));
+ spa_pod_builder_push_choice(b, &choice, SPA_CHOICE_Enum, 0);
+ spa_pod_builder_long(b, modifiers[0]);
+ for (uint32_t i = 0; i < count; i++)
+ spa_pod_builder_long(b, modifiers[i]);
+ spa_pod_builder_pop(b, &choice);
+ spa_pod_builder_add(b, SPA_FORMAT_VIDEO_size, SPA_POD_Rectangle(&SPA_RECTANGLE(WIDTH, HEIGHT)),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&SPA_FRACTION(0, 1)),
+ SPA_FORMAT_VIDEO_maxFramerate, SPA_POD_CHOICE_RANGE_Fraction(&SPA_FRACTION(60, 1),
+ &SPA_FRACTION(1, 1), &SPA_FRACTION(60, 1)), 0);
+ return spa_pod_builder_pop(b, &object);
+}
+
+static void on_param_changed(void *userdata, uint32_t id, const struct spa_pod *param) {
+ struct data *data = userdata;
+ if (param == NULL || id != SPA_PARAM_Format)
+ return;
+ const struct spa_pod_prop *modifier = spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier);
+ if (modifier == NULL) {
+ fprintf(stderr, "modifier prop missing\n");
+ pw_main_loop_quit(data->loop);
+ return;
+ }
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
+ if (modifier->flags & SPA_POD_PROP_FLAG_DONT_FIXATE) {
+ uint32_t count, choice;
+ const struct spa_pod *values = spa_pod_get_values(&modifier->value, &count, &choice);
+ const int64_t *modifiers = SPA_POD_BODY_CONST(values);
+ int linear = 0;
+ for (uint32_t i = 0; i < count; i++)
+ linear |= modifiers[i] == LINEAR;
+ if (!linear) {
+ fprintf(stderr, "consumer offers no LINEAR modifier\n");
+ pw_main_loop_quit(data->loop);
+ return;
+ }
+ const int64_t fixated[] = {LINEAR};
+ const struct spa_pod *params[1] = {video_format(&b, fixated, 1, 0)};
+ fprintf(stderr, "fixating LINEAR\n");
+ pw_stream_update_params(data->stream, params, 1);
+ return;
+ }
+ const struct spa_pod *params[1];
+ params[0] = spa_pod_builder_add_object(&b, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
+ SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(8, 2, 16),
+ SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
+ SPA_PARAM_BUFFERS_dataType, SPA_POD_CHOICE_FLAGS_Int(1 << SPA_DATA_DmaBuf));
+ pw_stream_update_params(data->stream, params, 1);
+ data->fixated = 1;
+ fprintf(stderr, "format fixated\n");
+}
+
+static void on_add_buffer(void *userdata, struct pw_buffer *buffer) {
+ (void)userdata;
+ struct spa_data *d = &buffer->buffer->datas[0];
+ if (!(d->type & (1 << SPA_DATA_DmaBuf))) {
+ fprintf(stderr, "consumer refused DMA-BUF buffers\n");
+ return;
+ }
+ int fd = memfd_create("fake-niri-dmabuf", MFD_CLOEXEC);
+ if (fd < 0 || ftruncate(fd, STRIDE * HEIGHT) < 0) {
+ perror("memfd");
+ return;
+ }
+ d->type = SPA_DATA_DmaBuf;
+ d->flags = SPA_DATA_FLAG_READWRITE;
+ d->fd = fd;
+ d->mapoffset = 0;
+ d->maxsize = 1;
+ d->data = NULL;
+ d->chunk->offset = 0;
+ d->chunk->stride = STRIDE;
+}
+
+static void on_remove_buffer(void *userdata, struct pw_buffer *buffer) {
+ (void)userdata;
+ close(buffer->buffer->datas[0].fd);
+}
+
+static void on_state_changed(void *userdata, enum pw_stream_state old, enum pw_stream_state state,
+ const char *error) {
+ struct data *data = userdata;
+ (void)old;
+ fprintf(stderr, "state %s %s\n", pw_stream_state_as_string(state), error ? error : "");
+ if (!data->announced && state == PW_STREAM_STATE_PAUSED) {
+ printf("node %u\n", pw_stream_get_node_id(data->stream));
+ fflush(stdout);
+ data->announced = 1;
+ }
+}
+
+static void on_timeout(void *userdata, uint64_t expirations) {
+ struct data *data = userdata;
+ (void)expirations;
+ if (data->fixated)
+ pw_stream_trigger_process(data->stream);
+}
+
+static void on_process(void *userdata) {
+ struct data *data = userdata;
+ struct pw_buffer *buffer = pw_stream_dequeue_buffer(data->stream);
+ if (buffer == NULL)
+ return;
+ struct spa_data *d = &buffer->buffer->datas[0];
+ uint8_t *pixels = mmap(NULL, STRIDE * HEIGHT, PROT_READ | PROT_WRITE, MAP_SHARED, d->fd, 0);
+ if (pixels != MAP_FAILED) {
+ uint8_t level = (uint8_t)(16 + (data->frame * 8) % 224);
+ for (int i = 0; i < STRIDE * HEIGHT; i += 4) {
+ pixels[i] = level;
+ pixels[i + 1] = 0x40;
+ pixels[i + 2] = 0x80;
+ pixels[i + 3] = 0xff;
+ }
+ munmap(pixels, STRIDE * HEIGHT);
+ }
+ d->chunk->offset = 0;
+ d->chunk->stride = STRIDE;
+ d->chunk->size = 1;
+ d->chunk->flags = SPA_CHUNK_FLAG_NONE;
+ data->frame++;
+ pw_stream_queue_buffer(data->stream, buffer);
+}
+
+static const struct pw_stream_events events = {
+ PW_VERSION_STREAM_EVENTS,
+ .state_changed = on_state_changed,
+ .param_changed = on_param_changed,
+ .add_buffer = on_add_buffer,
+ .remove_buffer = on_remove_buffer,
+ .process = on_process,
+};
+
+int main(int argc, char **argv) {
+ struct data data = {0};
+ pw_init(&argc, &argv);
+ data.loop = pw_main_loop_new(NULL);
+ data.stream = pw_stream_new_simple(pw_main_loop_get_loop(data.loop), "fake-niri-screencast",
+ pw_properties_new(PW_KEY_MEDIA_CLASS, "Video/Source", PW_KEY_MEDIA_TYPE, "Video",
+ PW_KEY_MEDIA_CATEGORY, "Source", PW_KEY_MEDIA_ROLE, "Screen", NULL),
+ &events, &data);
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
+ const int64_t modifiers[] = {X_TILED, LINEAR};
+ const struct spa_pod *params[1] = {video_format(&b, modifiers, 2, 1)};
+ if (pw_stream_connect(data.stream, PW_DIRECTION_OUTPUT, PW_ID_ANY,
+ PW_STREAM_FLAG_DRIVER | PW_STREAM_FLAG_ALLOC_BUFFERS, params, 1) < 0) {
+ fprintf(stderr, "connect failed\n");
+ return 1;
+ }
+ data.timer = pw_loop_add_timer(pw_main_loop_get_loop(data.loop), on_timeout, &data);
+ struct timespec interval = {0, 1000000000 / 30};
+ pw_loop_update_timer(pw_main_loop_get_loop(data.loop), data.timer, &interval, &interval, false);
+ pw_main_loop_run(data.loop);
+ return 0;
+}
diff --git a/scripts/linux-desktop-harness/niri-screencast/run.sh b/scripts/linux-desktop-harness/niri-screencast/run.sh
new file mode 100755
index 000000000..b6e99dfdb
--- /dev/null
+++ b/scripts/linux-desktop-harness/niri-screencast/run.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+# Records from a producer that shares frames the way niri 26.04 does, through
+# Cap's PipeWire capture loop (GitHub #2941). Starts a private PipeWire and
+# WirePlumber, so it runs on any Linux host with them installed.
+# Usage: run.sh (from the repository root; needs gcc, pkg-config,
+# libpipewire-0.3-dev, pipewire, wireplumber, dbus)
+set -euo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+work=$(mktemp -d)
+export XDG_RUNTIME_DIR="$work/runtime" CAP_FAKE_NIRI_PRODUCER="$work/producer"
+mkdir -m 700 "$XDG_RUNTIME_DIR"
+trap 'find "$work" -mindepth 1 -delete 2>/dev/null || true' EXIT
+gcc -o "$CAP_FAKE_NIRI_PRODUCER" "$here/producer.c" $(pkg-config --cflags --libs libpipewire-0.3)
+# The daemons belong to the inner shell, so it stops them before the outer
+# trap removes their runtime directory.
+WORK="$work" dbus-run-session -- bash -euc '
+ pipewire >"$WORK/pipewire.log" 2>&1 &
+ daemons=$!
+ trap "kill \$daemons 2>/dev/null; wait \$daemons 2>/dev/null || true" EXIT
+ for _ in $(seq 50); do [[ -S "$XDG_RUNTIME_DIR/pipewire-0" ]] && break; sleep 0.1; done
+ wireplumber >"$WORK/wireplumber.log" 2>&1 &
+ daemons="$daemons $!"
+ sleep 2
+ cargo test -p cap-recording --lib \
+ frames_from_a_producer_that_shares_only_dmabufs_are_captured -- --ignored --nocapture
+'
diff --git a/scripts/linux-desktop-harness/niri-software.Dockerfile b/scripts/linux-desktop-harness/niri-software.Dockerfile
new file mode 100644
index 000000000..dc67712ba
--- /dev/null
+++ b/scripts/linux-desktop-harness/niri-software.Dockerfile
@@ -0,0 +1,14 @@
+# niri 26.04 as released, with only its refusal of software renderers removed
+# so it runs on vkms with llvmpipe: CI has no GPU. Its screencast code (format
+# negotiation, GBM DMA-BUF allocation, PipeWire) is untouched.
+FROM cap-harness-arch AS build
+RUN pacman -Sy --noconfirm --needed base-devel git rust clang libinput libxkbcommon mesa \
+ pipewire seatd pango libdisplay-info libglvnd systemd-libs >/dev/null
+RUN git clone --depth 1 --branch v26.04 https://github.com/YaLTeR/niri /niri
+WORKDIR /niri
+RUN grep -n 'software EGL renderers are skipped' src/backend/tty.rs \
+ && sed -i 's/!egl_device.is_software(),/true,/' src/backend/tty.rs \
+ && grep -n 'software EGL renderers are skipped' -B2 src/backend/tty.rs
+RUN cargo build --release --locked 2>&1 | tail -3 && ls -la target/release/niri
+FROM cap-harness-arch
+COPY --from=build /niri/target/release/niri /usr/bin/niri
diff --git a/scripts/linux-desktop-harness/run-desktop.sh b/scripts/linux-desktop-harness/run-desktop.sh
index ec6543ce5..34f4f5177 100644
--- a/scripts/linux-desktop-harness/run-desktop.sh
+++ b/scripts/linux-desktop-harness/run-desktop.sh
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# Host side: one fresh desktop container, one Cap package, one scenario.
-# Usage: run-desktop.sh <gnome|hyprland|niri|sway> <package.deb|.AppImage|any file with DEV_BIN> <out-dir> [ENV=VALUE...]
+# Usage: run-desktop.sh <gnome|kde|hyprland|niri|sway> <package.deb|.AppImage|any file with DEV_BIN> <out-dir> [ENV=VALUE...]
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
desktop=$1
@@ -14,12 +14,19 @@ case "$desktop" in
# on the vgem/vkms faux devices this harness renders with; real GPUs are not
# affected, so the harness (not Cap) turns that layer off.
hyprland|niri|sway) image=cap-harness-arch dockerfile=arch.Dockerfile up=arch-up.sh extra_env=(-e VK_LOADER_LAYERS_DISABLE=VK_LAYER_MESA_device_select) ;;
+ kde) image=cap-harness-kde dockerfile=kde.Dockerfile up=kde-up.sh ;;
*) echo "unknown desktop $desktop" >&2; exit 2 ;;
esac
name=cap-$desktop-$$
[[ -d "$out" ]] && find "$out" -mindepth 1 -delete
mkdir -p "$out" && chmod 777 "$out"
-docker build -q -f "$here/$dockerfile" -t "$image" "$here" >/dev/null
+# HARNESS_IMAGE: an image built from this one (a patched compositor) to run
+# instead.
+if [[ -n "${HARNESS_IMAGE:-}" ]]; then
+ image=$HARNESS_IMAGE
+else
+ docker build -q -f "$here/$dockerfile" -t "$image" "$here" >/dev/null
+fi
# DEV_BIN=<path to a built cap-gpui> with DEV_LIBS=<its FFmpeg lib dir> runs a
# local build in place of the packaged binary (the package still provides the
# desktop file and dependencies).
@@ -27,7 +34,10 @@ dev_mounts=()
if [[ -n "${DEV_BIN:-}" ]]; then
dev_mounts=(-v "$(realpath "$DEV_BIN")":/dev-bin/cap-gpui:ro -v "$(realpath "${DEV_LIBS:?set DEV_LIBS}")":/dev-libs:ro)
fi
-docker run -d --name "$name" --privileged --cgroupns=host \
+# DOCKER_EXTRA: more `docker run` flags, e.g. `--gpus all -e
+# NVIDIA_DRIVER_CAPABILITIES=all` to hand a real GPU's userspace to the desktop.
+read -r -a docker_extra <<<"${DOCKER_EXTRA:-}"
+docker run -d --name "$name" --privileged --cgroupns=host "${docker_extra[@]}" \
-v /sys/fs/cgroup:/sys/fs/cgroup:rw --tmpfs /run --tmpfs /run/lock \
-v "$package":/pkgs/"$(basename "$package")":ro -v "$here":/harness:ro -v "$out":/out \
"${dev_mounts[@]}" "$image" >/dev/null
diff --git a/scripts/linux-desktop-harness/scenario-clipboard.sh b/scripts/linux-desktop-harness/scenario-clipboard.sh
new file mode 100755
index 000000000..2e164f454
--- /dev/null
+++ b/scripts/linux-desktop-harness/scenario-clipboard.sh
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+# Paste into and copy out of a Cap text field with the keys a person presses.
+# Cap opens its teleprompter, whose script field takes focus on open; the
+# compositor's virtual keyboard (wtype) presses Ctrl+V with text another app
+# put on the Wayland clipboard (wl-copy), then Ctrl+A, Ctrl+C, and wl-paste
+# reads back what Cap copied. The script is saved to Cap's store, so the paste
+# is checked there too. Needs a wlroots-style compositor (niri, Hyprland).
+# Prints key=value results to $HARNESS_OUT/result.txt.
+set -uo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+out=${HARNESS_OUT:-/out}
+eval "$(systemctl --user show-environment | grep -E "^(WAYLAND_DISPLAY|XDG_SESSION_TYPE|XDG_CURRENT_DESKTOP|XDG_SESSION_DESKTOP|HYPRLAND_INSTANCE_SIGNATURE)=" | sed 's/^/export /')"
+
+: >"$out/result.txt"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+wait_log() {
+ for _ in $(seq $(($2 * 4))); do
+ grep -aq -- "$1" "$out/cap.log" 2>/dev/null && return 0
+ sleep 0.25
+ done
+ return 1
+}
+# Hyprland drops the first key each new virtual keyboard sends (any client,
+# not only Cap), so every wtype run leads with a Shift tap no field reacts to.
+press() { wtype -k Shift_L "$@" 2>>"$out/actions.log" || echo "wtype $* failed" >>"$out/actions.log"; sleep 0.5; }
+chord() { press -M ctrl "$1" -m ctrl; }
+
+# A profile past onboarding, so the teleprompter is the window that has focus.
+mkdir -p "$out/profile"
+cat >"$out/profile/store" <<'JSON'
+{
+ "general_settings": {
+ "hasCompletedStartup": true,
+ "hasCompletedOnboarding": true,
+ "enableNotifications": false,
+ "enableTelemetry": false
+ }
+}
+JSON
+bash "$here/launch-cap.sh" cap CAP_GPUI_AUTO_TELEPROMPTER=1 >/dev/null
+if ! wait_log "teleprompter window opened" 30; then
+ result teleprompter_opened no
+ exit 0
+fi
+result teleprompter_opened yes
+sleep 2
+
+probe="pasted from another app $RANDOM"
+wl-copy "$probe" 2>>"$out/actions.log"
+echo "clipboard holds: $(timeout 5 wl-paste -n 2>&1)" >>"$out/actions.log"
+if [[ ${XDG_CURRENT_DESKTOP:-} == niri ]]; then
+ NIRI_SOCKET=$(ls "$XDG_RUNTIME_DIR"/niri.*.sock 2>/dev/null | head -1) niri msg focused-window >>"$out/actions.log" 2>&1
+fi
+# Plain typing first, so a failed paste is told apart from keys not arriving.
+press "typed by hand "
+typed=no
+for _ in $(seq 20); do
+ grep -aqF "typed by hand" "$out/profile/store" 2>/dev/null && { typed=yes; break; }
+ sleep 0.25
+done
+result typing_reached_field "$typed"
+chord v
+# The script saves on a short debounce.
+saved=no
+for _ in $(seq 20); do
+ grep -aqF "$probe" "$out/profile/store" 2>/dev/null && { saved=yes; break; }
+ sleep 0.25
+done
+result paste_reached_field "$saved"
+
+wl-copy "clipboard before copy"
+chord a
+chord c
+copied=$(timeout 5 wl-paste -n 2>/dev/null || true)
+[[ $copied == *"$probe"* ]] && result copy_reached_clipboard yes || result copy_reached_clipboard no
+kill -0 "$(cat "$XDG_RUNTIME_DIR/cap.pid" 2>/dev/null)" 2>/dev/null && result cap_alive yes || result cap_alive no
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/scenario-export.sh b/scripts/linux-desktop-harness/scenario-export.sh
new file mode 100755
index 000000000..d73efcf77
--- /dev/null
+++ b/scripts/linux-desktop-harness/scenario-export.sh
@@ -0,0 +1,111 @@
+#!/usr/bin/env bash
+set -uo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+out=${HARNESS_OUT:-/out}
+
+bash "$here/scenario-start-stop.sh" >/dev/null
+recorded=$(sed -n 's/^attempt1_finished=//p' "$out/result.txt")
+mv "$out/result.txt" "$out/recording-result.txt"
+: >"$out/result.txt"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+result recorded "${recorded:-no}"
+[[ $recorded == yes ]] || exit 0
+
+eval "$(systemctl --user show-environment | grep -E "^(WAYLAND_DISPLAY|XDG_SESSION_TYPE|XDG_CURRENT_DESKTOP|XDG_SESSION_DESKTOP|LIBGL_ALWAYS_SOFTWARE|HYPRLAND_INSTANCE_SIGNATURE|NIRI_SOCKET)=" | sed 's/^/export /')"
+
+mp4_seconds() {
+ python3 - "$1" <<'PY'
+import struct
+import sys
+
+data = open(sys.argv[1], "rb").read()
+
+
+def boxes(start, end):
+ while start + 8 <= end:
+ size, kind = struct.unpack(">I4s", data[start : start + 8])
+ header = 8
+ if size == 1:
+ size = struct.unpack(">Q", data[start + 8 : start + 16])[0]
+ header = 16
+ elif size == 0:
+ size = end - start
+ if size < header:
+ return
+ yield kind, start + header, start + size
+ start += size
+
+
+for kind, body, end in boxes(0, len(data)):
+ if kind != b"moov":
+ continue
+ for inner, inner_body, _ in boxes(body, end):
+ if inner == b"mvhd":
+ version = data[inner_body]
+ if version == 1:
+ scale, duration = struct.unpack(">IQ", data[inner_body + 20 : inner_body + 32])
+ else:
+ scale, duration = struct.unpack(">II", data[inner_body + 12 : inner_body + 20])
+ if scale:
+ print(f"{duration / scale:.2f}")
+PY
+}
+
+# The encoder merges identical frames into longer ones, so a GIF's length is
+# the sum of its frame delays.
+gif_frames_and_seconds() {
+ python3 - "$1" <<'PY'
+import sys
+
+data = open(sys.argv[1], "rb").read()
+if data[:6] in (b"GIF87a", b"GIF89a"):
+ frames, centiseconds, at = 0, 0, data.find(b"\x21\xf9\x04")
+ while at != -1 and at + 6 <= len(data):
+ frames += 1
+ centiseconds += data[at + 4] | data[at + 5] << 8
+ at = data.find(b"\x21\xf9\x04", at + 8)
+ print(frames, f"{centiseconds / 100:.2f}")
+PY
+}
+
+# An export holds the whole clip: its length is the recording's, give or take
+# half a second.
+matches_source() {
+ awk -v s="$1" -v want="${source_seconds:-0}" 'BEGIN { d = s - want; if (d < 0) d = -d; print (want >= 1 && d <= 0.5 ? "yes" : "no") }'
+}
+
+export_once() {
+ local kind=$1 file=$out/export.$1 name=cap-$1
+ bash "$here/launch-cap.sh" "$name" CAP_GPUI_AUTO_EDITOR=1 CAP_GPUI_AUTO_EXPORT="$file" >/dev/null
+ local done=""
+ for _ in $(seq 720); do
+ done=$(sed 's/\x1b\[[0-9;]*m//g' "$out/$name.log" 2>/dev/null |
+ grep -aoE "editor export completed|editor export failed.*|export waits for the user to confirm.*|panicked at.*" | head -1)
+ [[ -n $done ]] && break
+ sleep 0.25
+ done
+ echo "$kind: ${done:-no export line}" >>"$out/actions.log"
+ result "${kind}_exported" "$([[ $done == "editor export completed" ]] && echo yes || echo no)"
+ if [[ $kind == mp4 ]]; then
+ local seconds
+ seconds=$(mp4_seconds "$file" 2>/dev/null)
+ echo "mp4 seconds: ${seconds:-none}, bytes: $(stat -c %s "$file" 2>/dev/null)" >>"$out/actions.log"
+ result mp4_has_video "$(matches_source "${seconds:-0}")"
+ else
+ local frames seconds
+ read -r frames seconds < <(gif_frames_and_seconds "$file" 2>/dev/null)
+ echo "gif frames: ${frames:-none}, seconds: ${seconds:-none}, bytes: $(stat -c %s "$file" 2>/dev/null)" >>"$out/actions.log"
+ result gif_has_video "$(matches_source "${seconds:-0}")"
+ fi
+ result "${kind}_cap_alive" "$(kill -0 "$(cat "$XDG_RUNTIME_DIR/$name.pid" 2>/dev/null)" 2>/dev/null && echo yes || echo no)"
+ kill "$(cat "$XDG_RUNTIME_DIR/$name.pid" 2>/dev/null)" 2>/dev/null
+ sleep 2
+}
+
+source_seconds=$(mp4_seconds "$(find "$out/profile/recordings" -path "*segment-0/display.mp4" | head -1)" 2>/dev/null)
+echo "recording seconds: ${source_seconds:-none}" >>"$out/actions.log"
+export_once mp4
+export_once gif
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/scenario-pick-background.sh b/scripts/linux-desktop-harness/scenario-pick-background.sh
new file mode 100755
index 000000000..c1e45c3fb
--- /dev/null
+++ b/scripts/linux-desktop-harness/scenario-pick-background.sh
@@ -0,0 +1,88 @@
+#!/usr/bin/env bash
+# The editor's background image, chosen the way a person chooses it: record a
+# Studio clip, open it in the editor, press the Image tab's "choose an image"
+# (CAP_GPUI_AUTO_PICK_BACKGROUND), type a picture's path into the desktop
+# portal's file chooser and press Return. GNOME only (needs GNOME_UNSAFE=1 for
+# the recording's dialogs). Prints key=value results to $HARNESS_OUT/result.txt.
+set -uo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+out=${HARNESS_OUT:-/out}
+
+bash "$here/scenario-start-stop.sh" >/dev/null
+recorded=$(sed -n 's/^attempt1_finished=//p' "$out/result.txt")
+mv "$out/result.txt" "$out/recording-result.txt"
+: >"$out/result.txt"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+result recorded "${recorded:-no}"
+[[ $recorded == yes ]] || exit 0
+
+eval "$(systemctl --user show-environment | grep -E "^(WAYLAND_DISPLAY|XDG_SESSION_TYPE|XDG_CURRENT_DESKTOP|XDG_SESSION_DESKTOP|LIBGL_ALWAYS_SOFTWARE)=" | sed 's/^/export /')"
+picture=/tmp/cap-background.png
+python3 - "$picture" <<'PY'
+import sys
+
+import gi
+
+gi.require_version("GdkPixbuf", "2.0")
+from gi.repository import GdkPixbuf
+
+image = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, 320, 180)
+image.fill(0x3366CCFF)
+image.savev(sys.argv[1], "png", [], [])
+PY
+
+wait_log() {
+ for _ in $(seq $(($2 * 4))); do
+ grep -aq -- "$1" "$out/cap2.log" 2>/dev/null && return 0
+ sleep 0.25
+ done
+ return 1
+}
+
+bash "$here/launch-cap.sh" cap2 CAP_GPUI_AUTO_EDITOR=1 CAP_GPUI_AUTO_PICK_BACKGROUND=1 >/dev/null
+if ! wait_log "editor project validated" 60; then
+ result editor_opened no
+ exit 0
+fi
+result editor_opened yes
+gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \
+ --method org.gnome.Shell.Eval 'Main.overview.hide()' >/dev/null 2>&1
+
+chooser=no
+for _ in $(seq 60); do
+ if gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \
+ --method org.gnome.Shell.Eval "global.display.list_all_windows().some(w => /xdg-desktop-portal-gnome|org.gnome.Nautilus/i.test(w.get_wm_class() || ''))" 2>/dev/null |
+ grep -q "'true'"; then
+ chooser=yes
+ break
+ fi
+ sleep 0.5
+done
+result chooser_opened "$chooser"
+gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval \
+ "global.display.list_all_windows().map(w => (w.get_title() || '') + ' [' + (w.get_wm_class() || '') + ']').join(' | ')" >>"$out/actions.log" 2>&1
+if [[ $chooser == yes ]]; then
+ sleep 1
+ gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval \
+ "global.display.list_all_windows().filter(w => /xdg-desktop-portal-gnome|org.gnome.Nautilus/i.test(w.get_wm_class() || '')).forEach(w => w.activate(global.display.get_current_time_roundtrip()))" >/dev/null 2>&1
+ sleep 1
+ # Ctrl+L opens the location field; typing alone starts a search.
+ python3 "$here/mutter-keys.py" Control_L+l >>"$out/actions.log" 2>&1
+ sleep 0.5
+ python3 "$here/mutter-keys.py" --type "$picture" >>"$out/actions.log" 2>&1
+ sleep 1
+ bash "$here/shot.sh" "$out/chooser.png" >/dev/null 2>&1
+ # Return takes the completion; Select returns the file.
+ python3 "$here/mutter-keys.py" Return >>"$out/actions.log" 2>&1
+ sleep 1
+ python3 "$here/atspi-press.py" "Nautilus|Files|nautilus" "^Select$" 10 >>"$out/actions.log" 2>&1
+fi
+if wait_log "background image chosen" 20; then
+ result background_chosen yes
+else
+ result background_chosen no
+fi
+result cap_alive "$(kill -0 "$(cat "$XDG_RUNTIME_DIR/cap2.pid" 2>/dev/null)" 2>/dev/null && echo yes || echo no)"
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/scenario-raise.sh b/scripts/linux-desktop-harness/scenario-raise.sh
new file mode 100755
index 000000000..d997e282e
--- /dev/null
+++ b/scripts/linux-desktop-harness/scenario-raise.sh
@@ -0,0 +1,84 @@
+#!/usr/bin/env bash
+# Cap is running with another window focused (its settings); GNOME Shell then
+# launches Cap again the way the app grid launches any app, with an
+# activation token. The relaunch must hand over to the running Cap and bring
+# its main window to the front. Needs GNOME_UNSAFE=1 (Shell.Eval).
+# Prints key=value results to $HARNESS_OUT/result.txt.
+set -uo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+out=${HARNESS_OUT:-/out}
+eval "$(systemctl --user show-environment | grep -E "^(WAYLAND_DISPLAY|XDG_SESSION_TYPE|XDG_CURRENT_DESKTOP|XDG_SESSION_DESKTOP)=" | sed 's/^/export /')"
+
+: >"$out/result.txt"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+shell() {
+ gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \
+ --method org.gnome.Shell.Eval "$1" 2>/dev/null
+}
+focused() { shell "global.display.focus_window ? global.display.focus_window.get_title() : ''" | sed -n "s/^(true, '\"\\(.*\\)\"')$/\\1/p"; }
+wait_log() {
+ for _ in $(seq $(($3 * 4))); do
+ grep -q -- "$2" "$out/$1.log" 2>/dev/null && return 0
+ sleep 0.25
+ done
+ return 1
+}
+
+# RAISE_WINDOW=main reopens a Cap that finished setup; the default reopens one
+# still showing onboarding, which the reopen brings forward instead.
+expected="Welcome to Cap"
+if [[ ${RAISE_WINDOW:-onboarding} == main ]]; then
+ expected=Cap
+ mkdir -p "$out/profile"
+ cat >"$out/profile/store" <<'JSON'
+{
+ "general_settings": {
+ "hasCompletedStartup": true,
+ "hasCompletedOnboarding": true,
+ "enableNotifications": false,
+ "enableTelemetry": false,
+ "recordingCountdown": 0
+ }
+}
+JSON
+fi
+result window "$expected"
+
+CAP_ARGS='cap-desktop://action?value=%7B%22open_settings%22%3A%7B%22page%22%3Anull%7D%7D' \
+ bash "$here/launch-cap.sh" cap >/dev/null
+if ! wait_log cap "settings window opened" 30; then
+ result first_ready no
+ exit 0
+fi
+shell 'Main.overview.hide()' >/dev/null
+sleep 2
+shell "global.display.list_all_windows().find(w => w.get_title() === 'Cap Settings').activate(global.display.get_current_time_roundtrip())" >/dev/null
+sleep 1
+before=$(focused)
+result focused_before "${before:-none}"
+
+# The same build and profile the running Cap uses, as an app GNOME launches.
+profile="$out/profile"
+mkdir -p "$HOME/.local/share/applications"
+cat >"$HOME/.local/share/applications/cap-under-test.desktop" <<DESKTOP
+[Desktop Entry]
+Type=Application
+Name=Cap under test
+StartupNotify=true
+Exec=env CAP_GPUI_APP_DATA_DIR=$profile CAP_GPUI_RECORDINGS_DIR=$profile/recordings CAP_GPUI_LOG_DIR=$profile/logs LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-} RUST_LOG=info sh -c 'echo "launch token: \${XDG_ACTIVATION_TOKEN:-none}" >"$out/second.log"; exec "\$0" >>"$out/second.log" 2>&1' ${CAP_BIN:-/usr/bin/Cap}
+DESKTOP
+shell "imports.gi.Gio.DesktopAppInfo.new_from_filename('$HOME/.local/share/applications/cap-under-test.desktop').launch([], global.create_app_launch_context(global.display.get_current_time_roundtrip(), -1))" >>"$out/actions.log"
+wait_log second "Requested the existing Cap GPUI controls" 20 && result reopen_forwarded yes || result reopen_forwarded no
+grep -q "forwarding the launcher's activation token" "$out/second.log" && result relaunch_had_token yes || result relaunch_had_token no
+sleep 3
+after=$(focused)
+shell "global.display.list_all_windows().map(w => [w.get_title(), w.get_wm_class(), w.minimized, w.showing_on_its_workspace(), w.is_hidden ? w.is_hidden() : '', w.get_frame_rect().width + 'x' + w.get_frame_rect().height].join(':')).join(' | ')" >>"$out/actions.log"
+result focused_after "${after:-none}"
+if [[ $before == "Cap Settings" && $after == "$expected" ]]; then
+ result main_raised yes
+else
+ result main_raised no
+fi
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/scenario-register.sh b/scripts/linux-desktop-harness/scenario-register.sh
new file mode 100755
index 000000000..049adea78
--- /dev/null
+++ b/scripts/linux-desktop-harness/scenario-register.sh
@@ -0,0 +1,63 @@
+#!/usr/bin/env bash
+# Cap launched from a shell, which gives the portals no app id, names itself
+# so.cap.Cap through the host portal registry. Other apps' entries are written
+# and looked up first, as on a desktop in use: the portal then can miss a
+# just-written entry ("App info not found"). Launches Cap ROUNDS times
+# (default 4), first with its desktop entry removed (Cap writes it) and then
+# with the entry in place, and counts the launches that registered. Prints
+# key=value results to $HARNESS_OUT/result.txt.
+set -uo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+export HARNESS_OUT=${HARNESS_OUT:-/out}
+out=$HARNESS_OUT
+eval "$(systemctl --user show-environment | grep -E "^(WAYLAND_DISPLAY|XDG_SESSION_TYPE|XDG_CURRENT_DESKTOP|XDG_SESSION_DESKTOP|LIBGL_ALWAYS_SOFTWARE)=" | sed 's/^/export /')"
+entry=$HOME/.local/share/applications/so.cap.Cap.desktop
+rounds=${ROUNDS:-4}
+: >"$out/result.txt"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+
+launch() {
+ local name=$1 log=$out/$1.log line=""
+ bash "$here/launch-cap.sh" "$name" >/dev/null
+ for _ in $(seq 60); do
+ line=$(sed 's/\x1b\[[0-9;]*m//g' "$log" 2>/dev/null |
+ grep -aoE "registered Cap's app id.*|did not take Cap's app id.*|registry did not answer|portals know Cap.*|could not write the portal app id.*" | head -1)
+ [[ -n $line ]] && break
+ sleep 0.25
+ done
+ echo "$name: ${line:-no registration line}" >>"$out/actions.log"
+ kill "$(cat "$XDG_RUNTIME_DIR/$name.pid")" 2>/dev/null
+ for _ in $(seq 20); do
+ kill -0 "$(cat "$XDG_RUNTIME_DIR/$name.pid")" 2>/dev/null || break
+ sleep 0.25
+ done
+ [[ $line == "registered Cap's app id"* ]] && echo yes || echo no
+}
+
+register() {
+ gdbus call --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop \
+ --method org.freedesktop.host.portal.Registry.Register "$1" '{}' >/dev/null 2>&1
+}
+mkdir -p "$(dirname "$entry")"
+register so.cap.Missing
+for n in 1 2 3; do
+ printf '[Desktop Entry]\nType=Application\nName=Other %s\nExec=env true\nNoDisplay=true\n' "$n" \
+ >"$(dirname "$entry")/so.cap.Other$n.desktop"
+ register "so.cap.Other$n"
+ sleep 1
+done
+
+fresh=0 again=0
+for n in $(seq "$rounds"); do
+ rm -f "$entry"
+ [[ $(launch "fresh$n") == yes ]] && fresh=$((fresh + 1))
+ [[ $(launch "again$n") == yes ]] && again=$((again + 1))
+done
+result rounds "$rounds"
+result fresh_registered "$fresh"
+result again_registered "$again"
+result entry_written "$([[ -f $entry ]] && echo yes || echo no)"
+result panics "$(cat "$out"/fresh*.log "$out"/again*.log 2>/dev/null | grep -ac 'panicked at')"
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/scenario-screenshot.sh b/scripts/linux-desktop-harness/scenario-screenshot.sh
new file mode 100755
index 000000000..512d63c93
--- /dev/null
+++ b/scripts/linux-desktop-harness/scenario-screenshot.sh
@@ -0,0 +1,92 @@
+#!/usr/bin/env bash
+set -uo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+out=${HARNESS_OUT:-/out}
+eval "$(systemctl --user show-environment | grep -E "^(WAYLAND_DISPLAY|XDG_SESSION_TYPE|XDG_CURRENT_DESKTOP|XDG_SESSION_DESKTOP|LIBGL_ALWAYS_SOFTWARE|HYPRLAND_INSTANCE_SIGNATURE|NIRI_SOCKET)=" | sed 's/^/export /')"
+: >"$out/result.txt"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+
+# Outside GNOME the question comes from the GTK portal, which aborts in this
+# container (its image loader sandbox cannot start), so the permission is
+# granted up front there; GNOME Shell asks for real.
+if [[ $XDG_CURRENT_DESKTOP != *GNOME* ]]; then
+ for app in Cap so.cap.Cap ""; do
+ gdbus call --session --dest org.freedesktop.impl.portal.PermissionStore \
+ --object-path /org/freedesktop/impl/portal/PermissionStore \
+ --method org.freedesktop.impl.portal.PermissionStore.SetPermission \
+ screenshot true screenshot "$app" "['yes']" >/dev/null 2>&1
+ done
+fi
+
+# PRELAUNCH=1 runs Cap once first, as on a system Cap has run on: a shell
+# launch writes so.cap.Cap.desktop, which GNOME reads a few seconds later.
+if [[ ${PRELAUNCH:-} == 1 ]]; then
+ first=$(bash "$here/launch-cap.sh" first)
+ sleep 8
+ kill "$first" 2>/dev/null
+ sleep 3
+fi
+
+# LAUNCH=dock starts Cap in the unit GNOME's app grid gives it, as
+# scenario-start-stop.sh does; LAUNCH=terminal from a shell.
+if [[ ${LAUNCH:-dock} == dock ]]; then
+ export CAP_LAUNCH_PREFIX="systemd-run --user --scope --quiet --unit=app-gnome-Cap-$$.scope"
+fi
+pid=$(bash "$here/launch-cap.sh" cap CAP_GPUI_AUTO_RECORD=screenshot CAP_GPUI_AUTO_OVERLAY=display)
+
+# A person starts a screenshot by clicking Cap, so Cap has focus when the
+# portal asks; the automated start does not click.
+focus_cap() {
+ if [[ $XDG_CURRENT_DESKTOP == *GNOME* ]]; then
+ gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval \
+ "global.display.list_all_windows().filter(w => /^(Cap|so\.cap\.Cap)$/.test(w.get_wm_class() || '') && w.showing_on_its_workspace()).forEach(w => w.activate(global.display.get_current_time_roundtrip()))" \
+ >/dev/null 2>&1
+ elif [[ -n ${HYPRLAND_INSTANCE_SIGNATURE:-} ]]; then
+ hyprctl dispatch focuswindow class:Cap >/dev/null 2>&1
+ fi
+}
+for _ in $(seq 60); do
+ grep -aq "taking a screenshot" "$out/cap.log" 2>/dev/null && break
+ focus_cap
+ sleep 0.1
+done
+if [[ $XDG_CURRENT_DESKTOP == *GNOME* ]]; then
+ gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval \
+ "(imports.gi.Shell.WindowTracker.get_default().focus_app || {get_id: () => 'none'}).get_id()" \
+ 2>&1 | sed 's/^/focused app: /' >>"$out/actions.log"
+fi
+shot=""
+for _ in $(seq 120); do
+ shot=$(find "$out/profile" -path "*screenshots/*.cap/original.png" 2>/dev/null | head -1)
+ [[ -n $shot ]] && break
+ # The portal's one-time question, "Allow Cap to Take Screenshots?": GNOME
+ # Shell draws it (no accessibility tree here, so its button is clicked from
+ # the Shell), elsewhere the GTK portal does.
+ if [[ $XDG_CURRENT_DESKTOP == *GNOME* ]]; then
+ gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval \
+ "(() => { const find = a => { if (a instanceof imports.gi.St.Button && a.label === 'Allow') return a; for (const c of a.get_children()) { const f = find(c); if (f) return f; } return null; }; const b = find(global.stage); if (!b) return 'none'; b.emit('clicked', 1); return 'clicked Allow'; })()" \
+ 2>&1 | grep -o "clicked Allow" >>"$out/actions.log"
+ else
+ python3 "$here/atspi-press.py" "xdg-desktop-portal-gtk|xdg-desktop-portal-gnome" "^Allow$" 1 >>"$out/actions.log" 2>&1
+ fi
+ sleep 0.5
+done
+sleep 1
+size=""
+[[ -n $shot ]] && size=$(python3 -c 'import struct, sys
+data = open(sys.argv[1], "rb").read(24)
+if data[:8] == b"\x89PNG\r\n\x1a\n":
+ print(*struct.unpack(">II", data[16:24]))' "$shot")
+echo "screenshot: ${shot:-none} ${size:-}" >>"$out/actions.log"
+result screenshot_taken "$([[ -n $size ]] && echo yes || echo no)"
+result screenshot_size "$([[ ${size// /x} == "${SCREEN:-1920x1080}" ]] && echo yes || echo "${size// /x}")"
+result capture_failed "$(grep -ac 'screenshot capture failed' "$out/cap.log")"
+if [[ -z $shot ]]; then
+ find "$HOME" /tmp -newer "$out/profile/store" -iname "Screenshot*.png" -printf "portal file: %s bytes %p\n" 2>/dev/null >>"$out/actions.log"
+ journalctl --user --no-pager -n 300 2>/dev/null | grep -iE "access dialog|screenshot|portal-(gtk|hyprland|gnome)\[" | tail -8 >>"$out/actions.log"
+fi
+result cap_alive "$(kill -0 "$pid" 2>/dev/null && echo yes || echo no)"
+result panics "$(grep -ac 'panicked at' "$out/cap.log")"
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/scenario-start-stop.sh b/scripts/linux-desktop-harness/scenario-start-stop.sh
index fe9517714..40dc60dbe 100644
--- a/scripts/linux-desktop-harness/scenario-start-stop.sh
+++ b/scripts/linux-desktop-harness/scenario-start-stop.sh
@@ -6,7 +6,7 @@
#
# Env: HARNESS_OUT, LAUNCH (dock|terminal), RECORD_MODE (studio|instant),
# PRESS_AFTER (seconds; with no tray item by then, press the shortcut),
-# TRAY_CLICK (host|pointer).
+# TRAY_CLICK (host|pointer), AREA (x,y,width,height).
set -uo pipefail
here=$(cd "$(dirname "$0")" && pwd)
export XDG_RUNTIME_DIR=/run/user/$(id -u)
@@ -65,7 +65,30 @@ use_control() {
fi
;;
shortcut) python3 "$here/mutter-keys.py" Control_L+Shift_L+F9 >>"$out/actions.log" 2>&1 ;;
- approve) python3 "$here/atspi-press.py" "${2:-Add Keyboard Shortcuts}" "${3:-^Add$}" 20 >>"$out/actions.log" 2>&1 ;;
+ approve)
+ local scope="Add Keyboard Shortcuts" button="^Add$"
+ [[ ${DESKTOP:-gnome} == kde ]] && scope="xdg-desktop-portal-kde" button="^OK$"
+ python3 "$here/atspi-press.py" "${2:-$scope}" "${3:-$button}" 20 >>"$out/actions.log" 2>&1
+ ;;
+ # No tray and no shortcut: Cap records with its controls on screen. Find
+ # that window, then click the red Stop button in it with the pointer.
+ visible)
+ local rect
+ # GNOME starts in the overview, which draws windows elsewhere.
+ gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \
+ --method org.gnome.Shell.Eval 'Main.overview.hide()' >/dev/null 2>&1
+ sleep 1
+ rect=$(gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \
+ --method org.gnome.Shell.Eval "(() => { const w = global.display.list_all_windows().find(w => /^(Cap|so\.cap\.Cap)$/.test(w.get_wm_class() || '') && w.showing_on_its_workspace() && w.get_frame_rect().width === 320 && w.get_frame_rect().height === 150); if (!w) return ''; const r = w.get_frame_rect(); return [r.x, r.y, r.width, r.height].join(' '); })()" \
+ 2>/dev/null | sed -n "s/^(true, '\\(.*\\)')$/\\1/p" | tr -d '"')
+ echo "controls window: ${rect:-not shown}" >>"$out/actions.log"
+ result visible_controls_shown "$([[ -n "$rect" ]] && echo yes || echo no)"
+ if [[ -n "$rect" ]]; then
+ bash "$here/shot.sh" "$out/controls.png" >/dev/null 2>&1
+ # shellcheck disable=SC2086
+ point "$(python3 "$here/stop-locate.py" "$out/controls.png" $rect)"
+ fi
+ ;;
esac
}
@@ -96,14 +119,20 @@ attempt() {
if grep "recording Stop control ready" "$log" | tail -1 | grep -q "tray icon"; then control=tray; else control=shortcut; fi
break
fi
+ if [[ $(count "recording with Cap's controls visible") -gt 0 ]]; then control=visible; break; fi
if [[ $(tray_items) -gt 0 ]]; then control=tray; break; fi
if [[ -n "${PRESS_AFTER:-}" && $((SECONDS - began)) -ge $PRESS_AFTER ]]; then control=shortcut; break; fi
done
result "attempt${n}_control" "$control"
[[ $control == tray || $control == shortcut ]] && use_control "$control"
# The screen-share dialog: a person picks the screen and presses Share.
- if [[ ${DESKTOP:-gnome} == gnome ]] && ! wait_log "recording started" 4 "$started_before"; then
+ # GNOME's portal draws this dialog on GNOME, and on niri, which uses it;
+ # KDE's portal on Plasma.
+ if [[ ${DESKTOP:-gnome} == gnome || $DESKTOP == niri ]] && ! wait_log "recording started" 4 "$started_before"; then
use_control approve "Share Screen|xdg-desktop-portal-gnome" "^Share$"
+ elif [[ $DESKTOP == kde ]] && ! wait_log "recording started" 4 "$started_before"; then
+ # KDE's dialog lists each screen as a button that shares it.
+ use_control approve "xdg-desktop-portal-kde" "^Virtual-0$"
fi
if wait_log "recording started" 30 "$started_before"; then
result "attempt${n}_started" yes
@@ -134,15 +163,20 @@ attempt() {
fi
}
-# One tray host connection for the whole run, like a panel.
-coproc TRAY_HOST { python3 -u "$here/sni-click.py" "$out"; }
-if [[ ${TRAY_CLICK:-host} == pointer ]]; then
+# One tray host connection for the whole run, like a panel (none without a
+# tray: GNOME_TRAY=0).
+if [[ ${GNOME_TRAY:-1} != 0 ]]; then
+ coproc TRAY_HOST { python3 -u "$here/sni-click.py" "$out"; }
+fi
+if [[ ${TRAY_CLICK:-host} == pointer || ${GNOME_TRAY:-1} == 0 ]]; then
# The pointer must exist before Cap starts (see mutter-pointer.py).
coproc POINTER { python3 -u "$here/mutter-pointer.py" 2>>"$out/actions.log"; }
read -r -t 10 ready <&"${POINTER[0]}"
echo "$ready" >>"$out/actions.log"
fi
-pid=$("$here/launch-cap.sh" cap CAP_GPUI_AUTO_RECORD="$mode:${AUTO_STOP_SECS:-900}" CAP_GPUI_AUTO_OVERLAY=display)
+target=(CAP_GPUI_AUTO_OVERLAY=display)
+[[ -n ${AREA:-} ]] && target=(CAP_GPUI_AUTO_OVERLAY=area CAP_GPUI_AUTO_AREA="$AREA")
+pid=$("$here/launch-cap.sh" cap CAP_GPUI_AUTO_RECORD="$mode:${AUTO_STOP_SECS:-900}" "${target[@]}")
attempt 1
result cap_alive "$(kill -0 "$pid" 2>/dev/null && echo yes || echo no)"
@@ -158,6 +192,23 @@ result pipeline_failed "$(count 'recording pipeline failed')"
crashes="$out/profile/logs/cap-gpui.log.crashes"
result panics "$(( $(count 'panicked at\|cap_desktop_panic\|panic location') + $(grep -E 'panic thread=|native crash' "$crashes" 2>/dev/null | wc -l) ))"
result projects "$(find "$out/profile/recordings" -maxdepth 1 -name '*.cap' | wc -l)"
+if [[ -n ${AREA:-} ]]; then
+ video=$(find "$out/profile/recordings" -path "*segment-0/display.mp4" | head -1)
+ size=""
+ [[ -n $video ]] && size=$(python3 -c 'import struct, sys
+data = open(sys.argv[1], "rb").read()
+at = data.find(b"tkhd")
+while at != -1:
+ body = at + 4
+ width, height = struct.unpack(">II", data[body + (88 if data[body] == 1 else 76) :][:8])
+ if width and height:
+ print(width >> 16, height >> 16)
+ break
+ at = data.find(b"tkhd", at + 4)' "$video")
+ echo "area video: ${video:-none} ${size:-}" >>"$out/actions.log"
+ IFS=, read -r _ _ area_width area_height <<<"$AREA"
+ result area_video "$([[ $size == "$area_width $area_height" ]] && echo yes || echo no)"
+fi
kill "$pid" 2>/dev/null
sleep 1
cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/scenario-wallpaper.sh b/scripts/linux-desktop-harness/scenario-wallpaper.sh
new file mode 100755
index 000000000..f30b4ce08
--- /dev/null
+++ b/scripts/linux-desktop-harness/scenario-wallpaper.sh
@@ -0,0 +1,81 @@
+#!/usr/bin/env bash
+# The editor's "Use the wallpaper from your desktop" background: record a
+# Studio clip, open it in the editor and import the desktop's wallpaper
+# (CAP_GPUI_AUTO_DESKTOP_BACKGROUND). GNOME keeps its default wallpaper, a
+# JPEG XL file named in GSettings. WALLPAPER=swaybg starts swaybg with a PNG
+# first, the way Omarchy draws its wallpaper on Hyprland. Prints key=value
+# results to $HARNESS_OUT/result.txt.
+set -uo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+out=${HARNESS_OUT:-/out}
+
+bash "$here/scenario-start-stop.sh" >/dev/null
+recorded=$(sed -n 's/^attempt1_finished=//p' "$out/result.txt")
+mv "$out/result.txt" "$out/recording-result.txt"
+: >"$out/result.txt"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+result recorded "${recorded:-no}"
+[[ $recorded == yes ]] || exit 0
+
+eval "$(systemctl --user show-environment | grep -E "^(WAYLAND_DISPLAY|XDG_SESSION_TYPE|XDG_CURRENT_DESKTOP|XDG_SESSION_DESKTOP|LIBGL_ALWAYS_SOFTWARE|HYPRLAND_INSTANCE_SIGNATURE|NIRI_SOCKET)=" | sed 's/^/export /')"
+
+if [[ ${WALLPAPER:-} == swaybg ]]; then
+ expected=/tmp/cap-wallpaper.png
+ python3 - "$expected" <<'PY'
+import struct
+import sys
+import zlib
+
+width, height = 640, 360
+rows = b"".join(b"\x00" + bytes((0x2E, 0x8B, 0x57)) * width for _ in range(height))
+
+
+def chunk(kind, body):
+ return struct.pack(">I", len(body)) + kind + body + struct.pack(">I", zlib.crc32(kind + body))
+
+
+with open(sys.argv[1], "wb") as png:
+ png.write(b"\x89PNG\r\n\x1a\n")
+ png.write(chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)))
+ png.write(chunk(b"IDAT", zlib.compress(rows)))
+ png.write(chunk(b"IEND", b""))
+PY
+ swaybg -i "$expected" -m fill >"$out/swaybg.log" 2>&1 &
+ sleep 1
+else
+ uri=$(gsettings get org.gnome.desktop.background picture-uri | tr -d "'")
+ expected=${uri#file://}
+fi
+echo "expected wallpaper: $expected" >>"$out/actions.log"
+result expected_exists "$([[ -f $expected ]] && echo yes || echo no)"
+
+wait_log() {
+ for _ in $(seq $(($2 * 4))); do
+ grep -aqE -- "$1" "$out/cap2.log" 2>/dev/null && return 0
+ sleep 0.25
+ done
+ return 1
+}
+
+bash "$here/launch-cap.sh" cap2 CAP_GPUI_AUTO_EDITOR=1 CAP_GPUI_AUTO_DESKTOP_BACKGROUND=1 >/dev/null
+if ! wait_log "editor project validated" 60; then
+ result editor_opened no
+ exit 0
+fi
+result editor_opened yes
+wait_log "desktop wallpaper imported|couldn't import your desktop wallpaper" 60
+log=$(sed 's/\x1b\[[0-9;]*m//g' "$out/cap2.log")
+found=$(grep -a "found the desktop wallpaper" <<<"$log" | sed -n 's/.* path=\(.*\)$/\1/p' | tail -1)
+result wallpaper_found "$([[ -n $found && $found == "$expected" ]] && echo yes || echo "${found:-no}")"
+imported=$(grep -a "desktop wallpaper imported" <<<"$log" | sed -n 's/.* path=\(.*\)$/\1/p' | tail -1)
+if [[ -n $imported && $(head -c 3 "$imported" 2>/dev/null | od -An -tx1 | tr -d ' \n') == ffd8ff ]]; then
+ result wallpaper_imported yes
+ echo "imported $(stat -c %s "$imported") bytes of JPEG: $imported" >>"$out/actions.log"
+else
+ result wallpaper_imported no
+ grep -a "couldn't import your desktop wallpaper" <<<"$log" | tail -1 >>"$out/actions.log"
+fi
+result cap_alive "$(kill -0 "$(cat "$XDG_RUNTIME_DIR/cap2.pid" 2>/dev/null)" 2>/dev/null && echo yes || echo no)"
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/stop-locate.py b/scripts/linux-desktop-harness/stop-locate.py
new file mode 100755
index 000000000..0e816ac2a
--- /dev/null
+++ b/scripts/linux-desktop-harness/stop-locate.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+"""Where a person clicks to stop a recording in Cap's on-screen controls.
+
+The controls draw their Stop button (icon and elapsed time) in Cap's red,
+#ff4766. Given a screenshot and the controls window's frame, prints the centre
+of the red pixels inside it as "x y", or nothing when there are none.
+
+GNOME Shell writes an empty screenshot when it renders on a GPU or without a
+DRM device, so with no picture to read this prints where the button sits in
+the 320x150 controls.
+
+Usage: stop-locate.py <screenshot.png> <x> <y> <width> <height>
+"""
+import sys
+
+import gi
+
+gi.require_version("GdkPixbuf", "2.0")
+from gi.repository import GdkPixbuf, GLib # noqa: E402
+
+RED = (0xFF, 0x47, 0x66)
+TOLERANCE = 40
+# The red pixels' centre in a session on vkms.
+STOP_OFFSET = (48, 116)
+
+
+def main() -> int:
+ path = sys.argv[1]
+ left, top, width, height = (int(value) for value in sys.argv[2:6])
+ try:
+ image = GdkPixbuf.Pixbuf.new_from_file(path)
+ except GLib.Error as error:
+ print(f"no screenshot ({error.message}); using the Stop button's place", file=sys.stderr)
+ print(f"{left + STOP_OFFSET[0]} {top + STOP_OFFSET[1]}")
+ return 0
+ pixels = image.get_pixels()
+ stride = image.get_rowstride()
+ channels = image.get_n_channels()
+ xs, ys = [], []
+ for y in range(max(top, 0), min(top + height, image.get_height())):
+ row = y * stride
+ for x in range(max(left, 0), min(left + width, image.get_width())):
+ offset = row + x * channels
+ r, g, b = pixels[offset], pixels[offset + 1], pixels[offset + 2]
+ if all(abs(channel - want) <= TOLERANCE for channel, want in zip((r, g, b), RED)):
+ xs.append(x)
+ ys.append(y)
+ if not xs:
+ return 1
+ print(f"{sum(xs) // len(xs)} {sum(ys) // len(ys)}")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/scripts/linux-desktop-harness/x11-camera.sh b/scripts/linux-desktop-harness/x11-camera.sh
new file mode 100755
index 000000000..d98f21f83
--- /dev/null
+++ b/scripts/linux-desktop-harness/x11-camera.sh
@@ -0,0 +1,92 @@
+#!/usr/bin/env bash
+# Usage: x11-camera.sh <cap-gpui> <ffmpeg lib dir> <out-dir>
+set -uo pipefail
+for tool in Xvfb xdotool ffmpeg ffprobe; do
+ command -v "$tool" >/dev/null || { echo "install xvfb, xdotool and ffmpeg" >&2; exit 2; }
+done
+if [[ -z ${X11_CAMERA_BUS:-} ]]; then
+ exec env X11_CAMERA_BUS=1 dbus-run-session -- bash "$0" "$@"
+fi
+bin=$1 libs=$2 out=$3
+mkdir -p "$out" && : >"$out/result.txt" || { echo "x11-camera: cannot write to $out" >&2; exit 2; }
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+
+device=/dev/video42
+sudo modprobe v4l2loopback video_nr=42 card_label="Cap Test Camera" exclusive_caps=1 || exit 2
+sudo chmod 666 "$device"
+ffmpeg -hide_banner -loglevel error -nostdin -re -f lavfi -i testsrc2=size=1280x720:rate=30 -pix_fmt yuyv422 \
+ -f v4l2 "$device" >"$out/feeder.log" 2>&1 &
+feeder=$!
+unset WAYLAND_DISPLAY
+export DISPLAY=:73 XDG_SESSION_TYPE=x11
+Xvfb "$DISPLAY" -screen 0 1920x1080x24 >/dev/null 2>&1 &
+xvfb=$!
+trap 'kill $feeder $xvfb 2>/dev/null' EXIT
+sleep 2
+echo "feeder: $(kill -0 $feeder 2>/dev/null && echo running || echo exited) $(head -c 300 "$out/feeder.log")" >>"$out/actions.log"
+
+profile=$out/profile
+log=$out/cap.log
+mkdir -p "$profile/recordings" "$profile/logs"
+printf '%s' '{"general_settings":{"hasCompletedStartup":true,"hasCompletedOnboarding":true,"enableNotifications":false,"enableTelemetry":false,"recordingCountdown":0},"recording_start_safety":{"confirmBeforeRecordingWithoutMicrophone":false}}' >"$profile/store"
+env LD_LIBRARY_PATH="$libs" CAP_GPUI_APP_DATA_DIR="$profile" CAP_GPUI_RECORDINGS_DIR="$profile/recordings" \
+ CAP_GPUI_LOG_DIR="$profile/logs" CAP_GPUI_NO_TRAY=1 CAP_GPUI_AUTO_NO_MIC=1 RUST_LOG=info \
+ CAP_GPUI_AUTO_CAMERA=1 CAP_GPUI_AUTO_RECORD=studio:60 "$bin" >"$log" 2>&1 &
+pid=$!
+
+wait_log() {
+ for _ in $(seq $(($2 * 4))); do
+ grep -aq -- "$1" "$log" 2>/dev/null && return 0
+ sleep 0.25
+ done
+ return 1
+}
+
+camera=no started=no finished=no
+wait_log "auto-selecting camera" 30 && camera=yes
+wait_log "auto-record start requested" 30
+sleep 2
+xdotool key ctrl+shift+F9
+wait_log "recording started" 30 && started=yes
+if [[ $started == yes ]]; then
+ sleep 5
+ xdotool key ctrl+shift+F9
+ # Well before the 60 s automatic stop, so a Stop shortcut that does not
+ # work fails here.
+ wait_log "recording finished" 15 && finished=yes
+fi
+sleep 2
+result camera_selected "$camera"
+result started "$started"
+result finished "$finished"
+
+probe() {
+ ffprobe -v error -select_streams v:0 -count_packets \
+ -show_entries stream=width,height,nb_read_packets:format=duration -of csv=p=0 "$1" 2>/dev/null |
+ tr ',\n' ' '
+}
+display=$(find "$profile/recordings" -path "*segment-0/display.mp4" | head -1)
+camera_video=$(find "$profile/recordings" -path "*segment-0/camera.mp4" | head -1)
+read -r _ _ display_frames display_seconds <<<"$( [[ -n $display ]] && probe "$display")"
+read -r width height frames seconds <<<"$( [[ -n $camera_video ]] && probe "$camera_video")"
+echo "display: ${display:-none} frames=${display_frames:-} seconds=${display_seconds:-}" >>"$out/actions.log"
+echo "camera: ${camera_video:-none} ${width:-}x${height:-} frames=${frames:-} seconds=${seconds:-}" >>"$out/actions.log"
+result camera_size "$([[ $width == 1280 && $height == 720 ]] && echo yes || echo no)"
+result camera_matches_display "$(awk -v c="${seconds:-0}" -v d="${display_seconds:-0}" \
+ 'BEGIN { x = c - d; if (x < 0) x = -x; print (c >= 4 && c <= 8 && x <= 0.5 ? "yes" : "no") }')"
+result cap_alive "$(kill -0 "$pid" 2>/dev/null && echo yes || echo no)"
+result panics "$(grep -ac 'panicked at' "$log")"
+kill "$pid" 2>/dev/null
+
+cat "$out/result.txt" "$out/actions.log"
+failed=0
+for key in camera_selected started finished camera_size camera_matches_display cap_alive panics; do
+ grep -q "^$key=" "$out/result.txt" || { echo "x11-camera: no $key result" >&2; failed=1; }
+done
+while IFS== read -r key value; do
+ case $key in
+ panics) [[ $value == 0 ]] || failed=1 ;;
+ *) [[ $value == yes ]] || failed=1 ;;
+ esac
+done <"$out/result.txt"
+exit $failed
diff --git a/scripts/linux-desktop-harness/x11-capture.sh b/scripts/linux-desktop-harness/x11-capture.sh
new file mode 100755
index 000000000..b64ec0850
--- /dev/null
+++ b/scripts/linux-desktop-harness/x11-capture.sh
@@ -0,0 +1,227 @@
+#!/usr/bin/env bash
+# Usage: x11-capture.sh <cap-gpui> <ffmpeg lib dir> <out-dir>
+set -uo pipefail
+for tool in Xvfb xdotool xmessage pulseaudio pactl paplay ffmpeg; do
+ command -v "$tool" >/dev/null ||
+ { echo "install xvfb, xdotool, x11-utils, pulseaudio, pulseaudio-utils and ffmpeg" >&2; exit 2; }
+done
+if [[ -z ${X11_CAPTURE_BUS:-} ]]; then
+ exec env X11_CAPTURE_BUS=1 dbus-run-session -- bash "$0" "$@"
+fi
+bin=$1 libs=$2 out=$3
+mkdir -p "$out" && : >"$out/result.txt" || { echo "x11-capture: cannot write to $out" >&2; exit 2; }
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+unset WAYLAND_DISPLAY
+export DISPLAY=:79 XDG_SESSION_TYPE=x11
+Xvfb "$DISPLAY" -screen 0 1920x1080x24 >/dev/null 2>&1 &
+xvfb=$!
+sleep 1
+xmessage -geometry 480x270+700+300 -title "Window to record" "A window to record" >/dev/null 2>&1 &
+other=$!
+
+pulse_dir=$(mktemp -d)
+export PULSE_SERVER=unix:$pulse_dir/native
+pulseaudio -n --daemonize=no --exit-idle-time=-1 --log-target=stderr \
+ -L "module-native-protocol-unix socket=$pulse_dir/native auth-anonymous=1" \
+ -L "module-null-sink sink_name=cap_tone" \
+ -L "module-remap-source master=cap_tone.monitor source_name=cap_mic" >"$out/pulse.log" 2>&1 &
+pulse=$!
+for _ in $(seq 40); do pactl info >/dev/null 2>&1 && break; sleep 0.25; done
+pactl set-default-sink cap_tone
+pactl set-default-source cap_mic
+python3 - "$out/tone.wav" <<'PY'
+import math
+import struct
+import sys
+import wave
+
+rate = 48000
+with wave.open(sys.argv[1], "wb") as tone:
+ tone.setnchannels(2)
+ tone.setsampwidth(2)
+ tone.setframerate(rate)
+ tone.writeframes(b"".join(
+ struct.pack("<hh", sample, sample)
+ for sample in (int(12000 * math.sin(2 * math.pi * 440 * n / rate)) for n in range(rate * 30))
+ ))
+PY
+(while paplay --device=cap_tone "$out/tone.wav"; do :; done) &
+player=$!
+trap 'pkill -P $player 2>/dev/null; kill $player $pulse $other $xvfb 2>/dev/null' EXIT
+
+mp4_info() {
+ python3 - "$1" <<'PY'
+import struct
+import sys
+
+data = open(sys.argv[1], "rb").read()
+
+
+def boxes(start, end):
+ while start + 8 <= end:
+ size, kind = struct.unpack(">I4s", data[start : start + 8])
+ header = 8
+ if size == 1:
+ size = struct.unpack(">Q", data[start + 8 : start + 16])[0]
+ header = 16
+ elif size == 0:
+ size = end - start
+ if size < header:
+ return
+ yield kind, start + header, start + size
+ start += size
+
+
+seconds = width = height = None
+for kind, body, end in boxes(0, len(data)):
+ if kind != b"moov":
+ continue
+ for inner, inner_body, inner_end in boxes(body, end):
+ if inner == b"mvhd":
+ if data[inner_body] == 1:
+ scale, duration = struct.unpack(">IQ", data[inner_body + 20 : inner_body + 32])
+ else:
+ scale, duration = struct.unpack(">II", data[inner_body + 12 : inner_body + 20])
+ seconds = duration / scale if scale else None
+ if inner == b"trak" and width is None:
+ for track, track_body, _ in boxes(inner_body, inner_end):
+ if track == b"tkhd":
+ offset = track_body + (88 if data[track_body] == 1 else 76)
+ w, h = struct.unpack(">II", data[offset : offset + 8])
+ if w and h:
+ width, height = w >> 16, h >> 16
+if seconds is not None:
+ print(f"{seconds:.2f} {width} {height}")
+PY
+}
+
+png_size() {
+ python3 -c 'import struct, sys
+data = open(sys.argv[1], "rb").read(24)
+if data[:8] == b"\x89PNG\r\n\x1a\n":
+ print(*struct.unpack(">II", data[16:24]))' "$1"
+}
+
+launch() {
+ local name=$1; shift
+ local profile=$out/$name
+ mkdir -p "$profile/recordings" "$profile/logs"
+ printf '%s' '{"general_settings":{"hasCompletedStartup":true,"hasCompletedOnboarding":true,"enableNotifications":false,"enableTelemetry":false,"recordingCountdown":0},"recording_start_safety":{"confirmBeforeRecordingWithoutMicrophone":false}}' >"$profile/store"
+ env LD_LIBRARY_PATH="$libs" CAP_GPUI_APP_DATA_DIR="$profile" CAP_GPUI_RECORDINGS_DIR="$profile/recordings" \
+ CAP_GPUI_LOG_DIR="$profile/logs" CAP_GPUI_NO_TRAY=1 CAP_GPUI_AUTO_NO_MIC=1 RUST_LOG=info "$@" \
+ "$bin" >"$profile/cap.log" 2>&1 &
+ echo $! >"$profile/pid"
+}
+
+wait_log() {
+ local log=$1 pattern=$2 seconds=$3
+ for _ in $(seq $((seconds * 4))); do
+ grep -aq -- "$pattern" "$log" 2>/dev/null && return 0
+ sleep 0.25
+ done
+ return 1
+}
+
+finish() {
+ local name=$1 pid
+ pid=$(cat "$out/$name/pid")
+ result "${name}_cap_alive" "$(kill -0 "$pid" 2>/dev/null && echo yes || echo no)"
+ result "${name}_panics" "$(grep -ac 'panicked at' "$out/$name/cap.log")"
+ kill "$pid" 2>/dev/null
+ for _ in $(seq 20); do kill -0 "$pid" 2>/dev/null || break; sleep 0.25; done
+ kill -9 "$pid" 2>/dev/null
+}
+
+record() {
+ local name=$1 want_width=$2 want_height=$3 shortcut=$4; shift 4
+ local log=$out/$name/cap.log
+ launch "$name" "$@"
+ if [[ $shortcut == yes ]]; then
+ wait_log "$log" "auto-record start requested" 30
+ sleep 2
+ xdotool key ctrl+shift+F9
+ fi
+ local started=no finished=no
+ wait_log "$log" "recording started" 20 && started=yes
+ if [[ $started == yes && $shortcut == yes ]]; then
+ sleep 4
+ xdotool key ctrl+shift+F9
+ fi
+ # Well before the 60 s automatic stop of the shortcut cases, so a Stop
+ # shortcut that does not work fails here.
+ wait_log "$log" "recording finished" 15 && finished=yes
+ sleep 2
+ result "${name}_started" "$started"
+ result "${name}_finished" "$finished"
+ local project video info
+ project=$(find "$out/$name/recordings" -maxdepth 1 -name "*.cap" | head -1)
+ # A finish keeps its recovery workspace by design; the backup of the media
+ # it replaced must be gone.
+ result "${name}_backup_left" "$(find "$out/$name/recordings" -path '*/.recovery-*/original-segments' 2>/dev/null | wc -l)"
+ video=$(find "${project:-/nonexistent}" -path "*segment-0/display.mp4" 2>/dev/null | head -1)
+ info=$( [[ -n $video ]] && mp4_info "$video")
+ echo "$name: ${video:-no display.mp4} ${info:-}" >>"$out/actions.log"
+ read -r seconds width height <<<"${info:-0 0 0}"
+ result "${name}_video" "$(awk -v s="$seconds" -v w="$width" -v h="$height" -v W="$want_width" -v H="$want_height" \
+ 'BEGIN { print (s >= 3 && s <= 10 && w == W && h == H ? "yes" : "no") }')"
+ finish "$name"
+}
+
+record display 1920 1080 yes CAP_GPUI_AUTO_RECORD=studio:60
+record area 640 360 yes CAP_GPUI_AUTO_RECORD=studio:60 CAP_GPUI_AUTO_OVERLAY=area CAP_GPUI_AUTO_AREA=100,100,640,360
+record window 480 270 no CAP_GPUI_AUTO_RECORD=studio:6 CAP_GPUI_AUTO_OVERLAY=window \
+ CAP_GPUI_AUTO_WINDOW="Window to record"
+record audio 480 270 no CAP_GPUI_AUTO_RECORD=studio:6 CAP_GPUI_AUTO_OVERLAY=window \
+ CAP_GPUI_AUTO_WINDOW="Window to record" CAP_GPUI_AUTO_NO_MIC=0 CAP_GPUI_AUTO_SYSTEM_AUDIO=1
+# Both tracks span the recording: as long as its video, give or take half a
+# second.
+audio_video=$(find "$out/audio/recordings" -path "*segment-0/display.mp4" 2>/dev/null | head -1)
+read -r audio_video_seconds _ <<<"$( [[ -n $audio_video ]] && mp4_info "$audio_video")"
+for track in audio-input system_audio; do
+ file=$(find "$out/audio/recordings" -path "*segment-0/$track.m4a" 2>/dev/null | head -1)
+ seconds=0 level=-91
+ if [[ -n $file ]]; then
+ read -r seconds _ <<<"$(mp4_info "$file")"
+ level=$(ffmpeg -hide_banner -nostats -i "$file" -af volumedetect -f null - 2>&1 |
+ sed -n 's/.*mean_volume: \(-\{0,1\}[0-9.]*\) dB.*/\1/p')
+ fi
+ echo "audio $track: ${file:-missing} seconds=${seconds:-0} mean_volume=${level:--91}" >>"$out/actions.log"
+ result "audio_${track//-/_}" "$(awk -v s="${seconds:-0}" -v want="${audio_video_seconds:-0}" -v v="${level:--91}" \
+ 'BEGIN { d = s - want; if (d < 0) d = -d; print (want >= 3 && d <= 0.5 && v > -40 ? "yes" : "no") }')"
+done
+
+# With no title to match, the picker's first window must be the other app's:
+# Cap's own windows are not offered.
+launch picker CAP_GPUI_AUTO_OVERLAY=window
+wait_log "$out/picker/cap.log" "auto window target" 30
+picked=$(sed 's/\x1b\[[0-9;]*m//g' "$out/picker/cap.log" | grep -a "auto window target" | head -1)
+echo "picker: ${picked:-no window picked}" >>"$out/actions.log"
+result picker_offers_other_app "$([[ $picked == *'title=Window to record'* ]] && echo yes || echo no)"
+finish picker
+
+launch screenshot CAP_GPUI_AUTO_RECORD=screenshot
+shot=""
+for _ in $(seq 80); do
+ shot=$(find "$out/screenshot" -path "*.cap/original.png" 2>/dev/null | head -1)
+ [[ -n $shot ]] && break
+ sleep 0.25
+done
+sleep 1
+size=$( [[ -n $shot ]] && png_size "$shot")
+echo "screenshot: ${shot:-none} ${size:-}" >>"$out/actions.log"
+result screenshot_png "$([[ $size == "1920 1080" ]] && echo yes || echo no)"
+finish screenshot
+
+cat "$out/result.txt"
+failed=0
+for key in display_finished area_finished window_finished audio_finished display_video area_video window_video \
+ audio_video audio_audio_input audio_system_audio picker_offers_other_app screenshot_png; do
+ grep -q "^$key=" "$out/result.txt" || { echo "x11-capture: no $key result" >&2; failed=1; }
+done
+while IFS== read -r key value; do
+ case $key in
+ *_panics | *_backup_left) [[ $value == 0 ]] || failed=1 ;;
+ *) [[ $value == yes ]] || failed=1 ;;
+ esac
+done <"$out/result.txt"
+exit $failed
diff --git a/scripts/linux-desktop-harness/x11-stale-wayland.sh b/scripts/linux-desktop-harness/x11-stale-wayland.sh
new file mode 100755
index 000000000..42cc6fe88
--- /dev/null
+++ b/scripts/linux-desktop-harness/x11-stale-wayland.sh
@@ -0,0 +1,41 @@
+#!/usr/bin/env bash
+# Usage: x11-stale-wayland.sh <cap-gpui> <ffmpeg lib dir> <out-dir>
+set -uo pipefail
+command -v Xvfb >/dev/null || { echo "install xvfb" >&2; exit 2; }
+if [[ -z ${X11_STALE_BUS:-} ]]; then
+ exec env X11_STALE_BUS=1 dbus-run-session -- bash "$0" "$@"
+fi
+bin=$1 libs=$2 out=$3
+mkdir -p "$out" && : >"$out/result.txt" || { echo "x11-stale-wayland: cannot write to $out" >&2; exit 2; }
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+export DISPLAY=:72 XDG_SESSION_TYPE=wayland
+Xvfb "$DISPLAY" -screen 0 1280x800x24 >/dev/null 2>&1 &
+xvfb=$!
+trap 'kill $xvfb 2>/dev/null' EXIT
+sleep 1
+
+# A runtime dir whose Wayland socket outlived its compositor, as a systemd
+# user environment keeps WAYLAND_DISPLAY after a Wayland session ends.
+runtime=$(mktemp -d)
+python3 -c 'import socket, sys; socket.socket(socket.AF_UNIX).bind(sys.argv[1])' "$runtime/wayland-0"
+profile=$out/profile
+log=$out/cap.log
+mkdir -p "$profile/logs"
+printf '%s' '{"general_settings":{"hasCompletedStartup":true,"hasCompletedOnboarding":true,"enableNotifications":false,"enableTelemetry":false}}' >"$profile/store"
+env LD_LIBRARY_PATH="$libs" XDG_RUNTIME_DIR="$runtime" WAYLAND_DISPLAY=wayland-0 CAP_GPUI_APP_DATA_DIR="$profile" \
+ CAP_GPUI_LOG_DIR="$profile/logs" CAP_GPUI_NO_TRAY=1 RUST_LOG=info "$bin" >"$log" 2>&1 &
+pid=$!
+for _ in $(seq 120); do
+ grep -aq "main window opened\|panic" "$log" 2>/dev/null && break
+ sleep 0.25
+done
+sleep 2
+plain=$(sed 's/\x1b\[[0-9;]*m//g' "$log")
+result fell_back_to_x11 "$(grep -q "names no running compositor" <<<"$plain" && echo yes || echo no)"
+result main_window "$(grep -q "main window opened" <<<"$plain" && echo yes || echo no)"
+result cap_alive "$(kill -0 "$pid" 2>/dev/null && echo yes || echo no)"
+result panics "$(grep -c "panic" <<<"$plain")"
+kill "$pid" 2>/dev/null
+cat "$out/result.txt"
+grep -qx "fell_back_to_x11=yes" "$out/result.txt" && grep -qx "main_window=yes" "$out/result.txt" &&
+ grep -qx "cap_alive=yes" "$out/result.txt" && grep -qx "panics=0" "$out/result.txt"
diff --git a/scripts/prepare-gpui-dependency.sh b/scripts/prepare-gpui-dependency.sh
index af6575d31..bd26ac9bf 100755
--- a/scripts/prepare-gpui-dependency.sh
+++ b/scripts/prepare-gpui-dependency.sh
@@ -10,6 +10,8 @@ patch_paths=(
"apps/desktop-gpui/patches/zed-idle-frames.patch"
"apps/desktop-gpui/patches/zed-streaming-image.patch"
"apps/desktop-gpui/patches/zed-wayland-visibility.patch"
+ "apps/desktop-gpui/patches/zed-wayland-activation.patch"
+ "apps/desktop-gpui/patches/zed-closed-window-events.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 090cc9ba2..45a5b1eee 100644
--- a/scripts/prepare-gpui-dependency.test.mjs
+++ b/scripts/prepare-gpui-dependency.test.mjs
@@ -148,6 +148,10 @@ test("dependency verification handles overlapping patches without changing the c
patch("zed-streaming-image.patch");
writeFileSync(path.join(upstream, "visibility.rs"), "withheld first map\n");
patch("zed-wayland-visibility.patch");
+ writeFileSync(path.join(upstream, "activation.rs"), "launcher tokens\n");
+ patch("zed-wayland-activation.patch");
+ writeFileSync(path.join(upstream, "closed.rs"), "quiet closed windows\n");
+ patch("zed-closed-window-events.patch");
const script = path.join(cap, "scripts/prepare-gpui-dependency.sh");
writeFileSync(
script,