Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(web): start share page MP4 playback from the server HTML

Richie McIlroy222ad29d0269
diff --git a/apps/web/__tests__/unit/replay-missed-media-events.test.ts b/apps/web/__tests__/unit/replay-missed-media-events.test.ts
new file mode 100644
index 000000000..d04412dd8
--- /dev/null
+++ b/apps/web/__tests__/unit/replay-missed-media-events.test.ts
@@ -0,0 +1,99 @@
+// @vitest-environment jsdom
+
+import { describe, expect, it, vi } from "vitest";
+import { replayMissedMediaEvents } from "@/app/s/[videoId]/_components/replay-missed-media-events";
+
+function mediaState(
+ video: HTMLVideoElement,
+ state: { readyState: number; paused: boolean; error: MediaError | null },
+) {
+ Object.defineProperties(video, {
+ readyState: { configurable: true, get: () => state.readyState },
+ paused: { configurable: true, get: () => state.paused },
+ error: { configurable: true, get: () => state.error },
+ });
+}
+
+function recordEvents(video: HTMLVideoElement) {
+ const events: string[] = [];
+ for (const type of [
+ "loadedmetadata",
+ "loadeddata",
+ "canplay",
+ "play",
+ "error",
+ ])
+ video.addEventListener(type, () => events.push(type));
+ return events;
+}
+
+function manualSchedule() {
+ let replay: (() => void) | undefined;
+ const cancel = vi.fn();
+ return {
+ schedule: (fn: () => void) => {
+ replay = fn;
+ return cancel;
+ },
+ run: () => replay?.(),
+ cancel,
+ };
+}
+
+describe("replaying media events missed before hydration", () => {
+ it("replays every state the element reached, in order", () => {
+ const video = document.createElement("video");
+ mediaState(video, { readyState: 4, paused: false, error: null });
+ const events = recordEvents(video);
+ const timer = manualSchedule();
+ replayMissedMediaEvents(video, timer.schedule);
+ timer.run();
+ expect(events).toEqual(["loadedmetadata", "loadeddata", "canplay", "play"]);
+ });
+
+ it("does not replay states the element has not reached", () => {
+ const video = document.createElement("video");
+ mediaState(video, { readyState: 1, paused: true, error: null });
+ const events = recordEvents(video);
+ const timer = manualSchedule();
+ replayMissedMediaEvents(video, timer.schedule);
+ timer.run();
+ expect(events).toEqual(["loadedmetadata"]);
+ });
+
+ it("replays an error", () => {
+ const video = document.createElement("video");
+ mediaState(video, {
+ readyState: 0,
+ paused: true,
+ error: { code: 4 } as MediaError,
+ });
+ const events = recordEvents(video);
+ const timer = manualSchedule();
+ replayMissedMediaEvents(video, timer.schedule);
+ timer.run();
+ expect(events).toEqual(["error"]);
+ });
+
+ it("does not repeat an event that fired after listeners were added", () => {
+ const video = document.createElement("video");
+ mediaState(video, { readyState: 3, paused: true, error: null });
+ const events = recordEvents(video);
+ const timer = manualSchedule();
+ replayMissedMediaEvents(video, timer.schedule);
+ video.dispatchEvent(new Event("canplay"));
+ timer.run();
+ expect(events).toEqual(["canplay", "loadedmetadata", "loadeddata"]);
+ });
+
+ it("replays nothing once cleaned up", () => {
+ const video = document.createElement("video");
+ mediaState(video, { readyState: 4, paused: false, error: null });
+ const events = recordEvents(video);
+ const timer = manualSchedule();
+ const cleanup = replayMissedMediaEvents(video, timer.schedule);
+ cleanup();
+ expect(timer.cancel).toHaveBeenCalledOnce();
+ expect(events).toEqual([]);
+ });
+});
diff --git a/apps/web/__tests__/unit/share-page-loading.test.ts b/apps/web/__tests__/unit/share-page-loading.test.ts
index 9c20da562..cd21fa47c 100644
--- a/apps/web/__tests__/unit/share-page-loading.test.ts
+++ b/apps/web/__tests__/unit/share-page-loading.test.ts
@@ -7,6 +7,12 @@ const mocks = vi.hoisted(() => ({
policy: vi.fn(),
thumbnailUrl: vi.fn(),
playbackUrl: vi.fn(),
+ defaultStorage: true,
+ userAgent: "",
+ buildEnv: {
+ NEXT_PUBLIC_WEB_URL: "https://cap.so",
+ NEXT_PUBLIC_IS_CAP: undefined as string | undefined,
+ },
quota: vi.fn(),
user: vi.fn(),
authenticated: false,
@@ -21,7 +27,7 @@ const mocks = vi.hoisted(() => ({
vi.mock("@cap/database", () => ({ db: () => ({ select: mocks.select }) }));
vi.mock("@cap/database/auth/session", () => ({ getCurrentUser: mocks.user }));
vi.mock("@cap/env", () => ({
- buildEnv: { NEXT_PUBLIC_WEB_URL: "https://cap.so" },
+ buildEnv: mocks.buildEnv,
serverEnv: () => ({}),
}));
vi.mock("@cap/ui", () => ({ Logo: () => null }));
@@ -64,7 +70,9 @@ vi.mock("@/lib/server", () => ({
Effect.runPromise,
),
}));
-vi.mock("next/headers", () => ({ headers: async () => new Headers() }));
+vi.mock("next/headers", () => ({
+ headers: async () => new Headers({ "user-agent": mocks.userAgent }),
+}));
vi.mock("next/navigation", () => ({
notFound: () => {
throw new Error("NOT_FOUND");
@@ -82,8 +90,14 @@ vi.mock("@/lib/desktop-segments-recovery", () => ({
}));
vi.mock("@/lib/Notification", () => ({ createNotification: vi.fn() }));
vi.mock("@/lib/public-share-video", () => ({ getPublicShareVideo: vi.fn() }));
-vi.mock("@/lib/share-playback", () => ({
- getSharePlaybackUrl: mocks.playbackUrl,
+vi.mock("@/lib/share-playback", async (importActual) => ({
+ canStartPlaybackInHtml: (
+ await importActual<typeof import("@/lib/share-playback")>()
+ ).canStartPlaybackInHtml,
+ getSharePlaybackSource: async (video: unknown) => {
+ const url = (await mocks.playbackUrl(video)) as string | null;
+ return url ? { url, defaultStorage: mocks.defaultStorage } : null;
+ },
}));
vi.mock("@/lib/shareable-link-quota", () => ({
isVideoOverShareableLinkLimit: mocks.quota,
@@ -165,6 +179,7 @@ async function renderAuthorizedContent() {
)) as ReactElement<{
children: ReactElement<{
initialPlaybackUrl?: Promise<string | null>;
+ serverPlaybackUrl?: string | null;
screenshotImageUrl?: string | null;
data: {
sharedOrganizations: unknown[];
@@ -183,6 +198,9 @@ describe("share page loading", () => {
);
mocks.playbackUrl.mockResolvedValue("https://media.example.com/result.mp4");
mocks.quota.mockResolvedValue(false);
+ mocks.defaultStorage = true;
+ mocks.userAgent = "";
+ mocks.buildEnv.NEXT_PUBLIC_IS_CAP = undefined;
mocks.user.mockResolvedValue(null);
mocks.authenticated = false;
mocks.ownerIsPro = false;
@@ -329,3 +347,96 @@ describe("share page loading", () => {
);
});
});
+
+describe("share page MP4 source in the HTML", () => {
+ const CHROME =
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36";
+ const SIGNED = "https://media.example.com/result.mp4";
+
+ beforeEach(() => {
+ mocks.policy.mockReturnValue(Effect.void);
+ mocks.thumbnailUrl.mockReturnValue(
+ Effect.succeed(Option.some("https://media.example.com/image.jpg")),
+ );
+ mocks.playbackUrl.mockResolvedValue(SIGNED);
+ mocks.quota.mockResolvedValue(false);
+ mocks.user.mockResolvedValue(null);
+ mocks.authenticated = false;
+ mocks.ownerIsPro = false;
+ mocks.sharedOrganizations = [];
+ mocks.defaultStorage = true;
+ mocks.userAgent = CHROME;
+ mocks.buildEnv.NEXT_PUBLIC_IS_CAP = "true";
+ arrangeRows([createVideo()]);
+ });
+
+ it("renders the signed URL for Cap's own storage", async () => {
+ const content = await renderAuthorizedContent();
+ expect(content.props.children.props.serverPlaybackUrl).toBe(SIGNED);
+ expect(await content.props.children.props.initialPlaybackUrl).toBe(SIGNED);
+ expect(mocks.playbackUrl).toHaveBeenCalledOnce();
+ });
+
+ it.each([
+ [
+ "outside Cap Cloud",
+ () => {
+ mocks.buildEnv.NEXT_PUBLIC_IS_CAP = undefined;
+ },
+ ],
+ [
+ "for a storage integration reported by the signer",
+ () => {
+ mocks.defaultStorage = false;
+ },
+ ],
+ [
+ "over the shareable link quota",
+ () => {
+ mocks.quota.mockResolvedValue(true);
+ },
+ ],
+ [
+ "for iPhone Safari",
+ () => {
+ mocks.userAgent =
+ "Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.7 Mobile/15E148 Safari/604.1";
+ },
+ ],
+ [
+ "for desktop Safari, which may be an iPad",
+ () => {
+ mocks.userAgent =
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Safari/605.1.15";
+ },
+ ],
+ ])("keeps client-side resolution %s", async (_name, arrange) => {
+ arrange();
+ const content = await renderAuthorizedContent();
+ expect(content.props.children.props.serverPlaybackUrl).toBeNull();
+ });
+
+ it.each([{ bucket: "custom-bucket" }, { storageIntegrationId: "drive" }])(
+ "does not wait for signing for %j",
+ async (changes) => {
+ arrangeRows([{ ...createVideo(), ...changes }]);
+ mocks.playbackUrl.mockReturnValue(new Promise(() => {}));
+ const content = await renderAuthorizedContent();
+ expect(content.props.children.props.serverPlaybackUrl).toBeNull();
+ expect(content.props.children.props.initialPlaybackUrl).toBeInstanceOf(
+ Promise,
+ );
+ },
+ );
+
+ it("renders without the URL when signing is slow and still streams it to the player", async () => {
+ mocks.playbackUrl.mockReturnValue(
+ new Promise((resolve) => setTimeout(() => resolve(SIGNED), 200)),
+ );
+ const started = Date.now();
+ const content = await renderAuthorizedContent();
+ expect(Date.now() - started).toBeLessThan(180);
+ expect(content.props.children.props.serverPlaybackUrl).toBeNull();
+ expect(await content.props.children.props.initialPlaybackUrl).toBe(SIGNED);
+ });
+});
diff --git a/apps/web/__tests__/unit/share-page-round-trips.test.ts b/apps/web/__tests__/unit/share-page-round-trips.test.ts
index 27db7cf59..38eb06392 100644
--- a/apps/web/__tests__/unit/share-page-round-trips.test.ts
+++ b/apps/web/__tests__/unit/share-page-round-trips.test.ts
@@ -75,7 +75,11 @@ vi.mock("@/lib/loom-deferred-processing", () => ({
}));
vi.mock("@/lib/public-share-video", () => ({ getPublicShareVideo: vi.fn() }));
vi.mock("@/lib/share-playback", () => ({
- getSharePlaybackUrl: async () => "https://media.example.com/result.mp4",
+ canStartPlaybackInHtml: () => true,
+ getSharePlaybackSource: async () => ({
+ url: "https://media.example.com/result.mp4",
+ defaultStorage: true,
+ }),
}));
vi.mock("@/lib/share-web-url", () => ({ resolveShareWebUrl: vi.fn() }));
vi.mock("@/lib/transcribe", () => ({ transcribeVideo: vi.fn() }));
diff --git a/apps/web/__tests__/unit/share-playback.test.ts b/apps/web/__tests__/unit/share-playback.test.ts
index 900b503f8..18fb82b96 100644
--- a/apps/web/__tests__/unit/share-playback.test.ts
+++ b/apps/web/__tests__/unit/share-playback.test.ts
@@ -1,6 +1,10 @@
import { Effect, Option } from "effect";
import { describe, expect, it, vi } from "vitest";
-import { getSharePlaybackUrl } from "@/lib/share-playback";
+import {
+ canStartPlaybackInHtml,
+ getSharePlaybackSource,
+ getSharePlaybackUrl,
+} from "@/lib/share-playback";
const mocks = vi.hoisted(() => ({
access: vi.fn(),
@@ -127,3 +131,99 @@ describe("share page playback URL", () => {
expect(mocks.access).not.toHaveBeenCalled();
});
});
+
+describe("share page playback source storage", () => {
+ const signed = "https://media.example.com/video.mp4";
+ type Case = {
+ name: string;
+ bucket: typeof video.bucket;
+ storageIntegrationId: typeof video.storageIntegrationId;
+ customBucket: Option.Option<{ id: string }>;
+ defaultStorage: boolean;
+ };
+ const cases: Case[] = [
+ {
+ name: "Cap's own bucket",
+ bucket: null,
+ storageIntegrationId: null,
+ customBucket: Option.none(),
+ defaultStorage: true,
+ },
+ {
+ name: "a custom bucket",
+ bucket: video.bucket,
+ storageIntegrationId: null,
+ customBucket: Option.some({ id: "custom-bucket" }),
+ defaultStorage: false,
+ },
+ {
+ name: "a storage integration",
+ bucket: null,
+ storageIntegrationId: "drive-integration" as NonNullable<
+ typeof video.storageIntegrationId
+ >,
+ customBucket: Option.none(),
+ defaultStorage: false,
+ },
+ ];
+
+ it.each(cases)(
+ "reports $name",
+ async ({ bucket, storageIntegrationId, customBucket, defaultStorage }) => {
+ mocks.sign.mockReturnValue(Effect.succeed(signed));
+ mocks.access.mockReturnValue(
+ Effect.succeed([{ getSignedObjectUrl: mocks.sign }, customBucket]),
+ );
+ expect(
+ await getSharePlaybackSource({
+ ...video,
+ bucket,
+ storageIntegrationId,
+ }),
+ ).toEqual({ url: signed, defaultStorage });
+ },
+ );
+});
+
+describe("starting playback from the HTML", () => {
+ it.each([
+ [
+ "Chrome on macOS",
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36",
+ true,
+ ],
+ [
+ "Edge on Windows",
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0",
+ true,
+ ],
+ [
+ "Firefox on macOS",
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 15.7; rv:144.0) Gecko/20100101 Firefox/144.0",
+ true,
+ ],
+ [
+ "Chrome on Android",
+ "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36",
+ true,
+ ],
+ [
+ "Safari on iPhone",
+ "Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.7 Mobile/15E148 Safari/604.1",
+ false,
+ ],
+ [
+ "Chrome on iPad",
+ "Mozilla/5.0 (iPad; CPU OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/141.0.0.0 Mobile/15E148 Safari/604.1",
+ false,
+ ],
+ [
+ "Safari on macOS, which iPadOS also reports",
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Safari/605.1.15",
+ false,
+ ],
+ ["no user agent", null, false],
+ ] as const)("%s: %s", (_name, userAgent, expected) => {
+ expect(canStartPlaybackInHtml(userAgent)).toBe(expected);
+ });
+});
diff --git a/apps/web/__tests__/unit/share-player-server-source.test.ts b/apps/web/__tests__/unit/share-player-server-source.test.ts
new file mode 100644
index 000000000..6fe27fb82
--- /dev/null
+++ b/apps/web/__tests__/unit/share-player-server-source.test.ts
@@ -0,0 +1,389 @@
+// @vitest-environment jsdom
+
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { act, type ComponentProps, createElement, createRef } from "react";
+import { hydrateRoot } from "react-dom/client";
+import { renderToString } from "react-dom/server";
+import {
+ afterAll,
+ afterEach,
+ beforeAll,
+ describe,
+ expect,
+ it,
+ vi,
+} from "vitest";
+import { CapVideoPlayer } from "@/app/s/[videoId]/_components/CapVideoPlayer";
+
+vi.mock("@cap/ui", () => ({ LogoSpinner: () => null }));
+vi.mock("@cap/utils", () => ({
+ calculateStrokeDashoffset: () => 0,
+ getProgressCircleConfig: () => ({ circumference: 100 }),
+}));
+vi.mock("@fortawesome/react-fontawesome", () => ({
+ FontAwesomeIcon: () => null,
+}));
+vi.mock("lucide-react", () => ({
+ AlertTriangleIcon: () => null,
+ InfoIcon: () => null,
+}));
+vi.mock("motion/react", async () => {
+ const { createElement } = await import("react");
+ return {
+ AnimatePresence: ({ children }: { children?: React.ReactNode }) => children,
+ motion: {
+ div: ({ children }: { children?: React.ReactNode }) =>
+ createElement("div", null, children),
+ },
+ };
+});
+vi.mock("next/dynamic", () => ({ default: () => () => null }));
+vi.mock("@/actions/video/retry-processing", () => ({
+ retryVideoProcessing: vi.fn(),
+}));
+vi.mock("@/app/s/[videoId]/_components/CommentStamp", () => ({
+ default: () => null,
+}));
+vi.mock("@/app/s/[videoId]/_components/VideoPreviewGif", () => ({
+ VideoPreviewGif: () => null,
+}));
+vi.mock("@/app/s/[videoId]/_components/caption-tracks", () => ({
+ bindCaptionTrackCueText: () => () => undefined,
+}));
+vi.mock("@/app/s/[videoId]/_components/mp4-level-patch", () => ({
+ AVC_LEVEL_IOS_HARDWARE_CEILING: 42,
+ createLevelPatchedMp4ObjectUrl: vi.fn(),
+ isIosSafari: () => false,
+ probeAvcLevelFromUrl: vi.fn(),
+}));
+vi.mock("@/app/s/[videoId]/_components/video-frame-thumbnail", () => ({
+ captureVideoFrameDataUrl: () => undefined,
+}));
+vi.mock("@/app/s/[videoId]/_components/video/tooltip", async () => {
+ const { createElement } = await import("react");
+ const WithChildren = ({ children }: { children?: React.ReactNode }) =>
+ createElement("div", null, children);
+ return {
+ Tooltip: WithChildren,
+ TooltipContent: WithChildren,
+ TooltipTrigger: WithChildren,
+ };
+});
+vi.mock("@/app/s/[videoId]/_components/video/media-player", async () => {
+ const {
+ createContext,
+ createElement,
+ forwardRef,
+ useContext,
+ useEffect,
+ useState,
+ } = await import("react");
+ const MediaErrorContext = createContext({
+ hasError: false,
+ setHasError: (_hasError: boolean): void => undefined,
+ });
+ const WithChildren = ({ children }: { children?: React.ReactNode }) =>
+ createElement("div", null, children);
+ const Empty = () => null;
+ const MediaPlayer = ({ children }: { children?: React.ReactNode }) => {
+ const [hasError, setHasError] = useState(false);
+ return createElement(
+ MediaErrorContext.Provider,
+ { value: { hasError, setHasError } },
+ createElement("div", null, children),
+ );
+ };
+ const Video = forwardRef<HTMLVideoElement, ComponentProps<"video">>(
+ (props, ref) => {
+ const { setHasError } = useContext(MediaErrorContext);
+ useEffect(() => {
+ setHasError(false);
+ const video =
+ typeof ref === "object" && ref !== null ? ref.current : null;
+ if (!video) return;
+ const handleError = () => setHasError(true);
+ video.addEventListener("error", handleError);
+ return () => video.removeEventListener("error", handleError);
+ }, [props.src, ref, setHasError]);
+ return createElement("video", { ...props, ref }, props.children);
+ },
+ );
+ const MediaPlayerError = () => {
+ const { hasError } = useContext(MediaErrorContext);
+ return hasError
+ ? createElement("div", {
+ role: "alert",
+ "data-testid": "media-player-error",
+ })
+ : null;
+ };
+ return {
+ MediaPlayer,
+ MediaPlayerCaptions: Empty,
+ MediaPlayerControls: WithChildren,
+ MediaPlayerControlsOverlay: Empty,
+ MediaPlayerError,
+ MediaPlayerFullscreen: Empty,
+ MediaPlayerLoading: Empty,
+ MediaPlayerPiP: Empty,
+ MediaPlayerPlay: Empty,
+ MediaPlayerPlaybackSpeedDial: Empty,
+ MediaPlayerSeek: Empty,
+ MediaPlayerSeekBackward: Empty,
+ MediaPlayerSeekForward: Empty,
+ MediaPlayerSettings: Empty,
+ MediaPlayerTime: Empty,
+ MediaPlayerVideo: Video,
+ MediaPlayerVolume: Empty,
+ MediaPlayerVolumeIndicator: Empty,
+ };
+});
+
+const actEnvironment = globalThis as typeof globalThis & {
+ IS_REACT_ACT_ENVIRONMENT?: boolean;
+};
+
+function response(url: string, status: number, redirected = false): Response {
+ const result = new Response(null, { status });
+ Object.defineProperties(result, {
+ url: { value: url },
+ redirected: { value: redirected },
+ });
+ return result;
+}
+
+function waitForQueryNotification() {
+ return new Promise<void>((resolve) => setTimeout(resolve, 0));
+}
+
+async function settleFetch(
+ fetchImpl: ReturnType<typeof vi.fn<typeof fetch>>,
+ count: number,
+) {
+ await act(async () => {
+ await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalledTimes(count));
+ await waitForQueryNotification();
+ });
+}
+
+const SERVER_URL = "https://v.cap.so/owner/video/result.mp4?Signature=fixture";
+const PLAYLIST_PROBE = /^\/api\/playlist\?videoType=mp4&_t=\d+$/;
+const RAW_PROBE = /^\/api\/playlist\?videoType=raw-preview&_t=\d+$/;
+
+function playerProps(
+ overrides: Partial<ComponentProps<typeof CapVideoPlayer>> = {},
+) {
+ const videoRef = createRef<HTMLVideoElement>();
+ const props: ComponentProps<typeof CapVideoPlayer> = {
+ videoSrc: "/api/playlist?videoType=mp4",
+ initialPlaybackUrl: Promise.resolve(SERVER_URL),
+ serverPlaybackUrl: SERVER_URL,
+ enableCrossOrigin: true,
+ videoId: "fixture-video" as ComponentProps<
+ typeof CapVideoPlayer
+ >["videoId"],
+ chaptersSrc: "",
+ captionsSrc: "",
+ videoRef,
+ hasActiveUpload: false,
+ disablePreviewGif: true,
+ ...overrides,
+ };
+ return { props, videoRef };
+}
+
+function tree(
+ client: QueryClient,
+ props: ComponentProps<typeof CapVideoPlayer>,
+) {
+ return createElement(
+ QueryClientProvider,
+ { client },
+ createElement(CapVideoPlayer, props),
+ );
+}
+
+function newClient() {
+ return new QueryClient({ defaultOptions: { queries: { retry: false } } });
+}
+
+// Server-renders the player, then lets the test change the parsed <video>
+// before hydrating, the way the browser loads media while JS is downloading.
+async function hydratePlayer(
+ props: ComponentProps<typeof CapVideoPlayer>,
+ beforeHydrate: (video: HTMLVideoElement) => void = () => undefined,
+) {
+ const html = renderToString(tree(newClient(), props));
+ const container = document.createElement("div");
+ container.innerHTML = html;
+ document.body.append(container);
+ const serverVideo = container.querySelector("video");
+ if (serverVideo) beforeHydrate(serverVideo);
+ const recoverableErrors: unknown[] = [];
+ const client = newClient();
+ let root: ReturnType<typeof hydrateRoot> | undefined;
+ await act(async () => {
+ root = hydrateRoot(container, tree(client, props), {
+ onRecoverableError: (error) => recoverableErrors.push(error),
+ });
+ });
+ // Let the scheduled replay run.
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ });
+ return { html, container, serverVideo, client, recoverableErrors, root };
+}
+
+function setMediaState(
+ video: HTMLVideoElement,
+ state: { readyState?: number; error?: MediaError | null },
+) {
+ if (state.readyState !== undefined)
+ Object.defineProperty(video, "readyState", {
+ configurable: true,
+ get: () => state.readyState,
+ });
+ if (state.error !== undefined)
+ Object.defineProperty(video, "error", {
+ configurable: true,
+ get: () => state.error,
+ });
+}
+
+const loadingOverlay = (container: HTMLElement) =>
+ container.querySelector(".z-10.bg-black.transition-opacity");
+
+describe("share MP4 source rendered on the server", () => {
+ beforeAll(() => {
+ actEnvironment.IS_REACT_ACT_ENVIRONMENT = true;
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ document.body.replaceChildren();
+ });
+
+ afterAll(() => {
+ delete actEnvironment.IS_REACT_ACT_ENVIRONMENT;
+ });
+
+ it("puts the signed URL in the server HTML", () => {
+ const { props } = playerProps();
+ const html = renderToString(tree(newClient(), props));
+ const video = new DOMParser()
+ .parseFromString(html, "text/html")
+ .querySelector("video");
+ expect(video?.getAttribute("src")).toBe(SERVER_URL);
+ expect(video?.getAttribute("crossorigin")).toBe("anonymous");
+ });
+
+ it("renders no video on the server without a server URL", () => {
+ const { props } = playerProps({ serverPlaybackUrl: null });
+ const html = renderToString(tree(newClient(), props));
+ expect(html).not.toContain("<video");
+ });
+
+ it("hydrates the same element without probing or refetching", async () => {
+ const fetchImpl = vi.fn<typeof fetch>();
+ vi.stubGlobal("fetch", fetchImpl);
+ const { props, videoRef } = playerProps();
+ const player = await hydratePlayer(props);
+ try {
+ expect(player.recoverableErrors).toEqual([]);
+ expect(videoRef.current).toBe(player.serverVideo);
+ expect(videoRef.current?.getAttribute("src")).toBe(SERVER_URL);
+ expect(fetchImpl).not.toHaveBeenCalled();
+ } finally {
+ await act(async () => player.root?.unmount());
+ player.client.clear();
+ }
+ });
+
+ it("treats media that loaded before hydration as loaded", async () => {
+ vi.stubGlobal("fetch", vi.fn<typeof fetch>());
+ const { props } = playerProps();
+ const player = await hydratePlayer(props, (video) =>
+ setMediaState(video, { readyState: 3 }),
+ );
+ try {
+ expect(loadingOverlay(player.container)?.className).toContain(
+ "opacity-0",
+ );
+ } finally {
+ await act(async () => player.root?.unmount());
+ player.client.clear();
+ }
+ });
+
+ it("keeps the loading state while media is still loading at hydration", async () => {
+ vi.stubGlobal("fetch", vi.fn<typeof fetch>());
+ const { props } = playerProps();
+ const player = await hydratePlayer(props, (video) =>
+ setMediaState(video, { readyState: 0 }),
+ );
+ try {
+ expect(loadingOverlay(player.container)?.className).toContain(
+ "opacity-100",
+ );
+ } finally {
+ await act(async () => player.root?.unmount());
+ player.client.clear();
+ }
+ });
+
+ it("recovers from an error that happened before hydration through the playlist route", async () => {
+ const freshUrl = "https://v.cap.so/owner/video/result.mp4?Signature=fresh";
+ const fetchImpl = vi
+ .fn<typeof fetch>()
+ .mockResolvedValueOnce(response(freshUrl, 206, true));
+ vi.stubGlobal("fetch", fetchImpl);
+ const { props, videoRef } = playerProps();
+ const player = await hydratePlayer(props, (video) =>
+ setMediaState(video, {
+ readyState: 0,
+ error: { code: 2, message: "network" } as MediaError,
+ }),
+ );
+ try {
+ await settleFetch(fetchImpl, 1);
+ expect(fetchImpl.mock.calls[0]?.[0]).toMatch(PLAYLIST_PROBE);
+ await vi.waitFor(() => {
+ expect(videoRef.current?.getAttribute("src")).toBe(freshUrl);
+ });
+ } finally {
+ await act(async () => player.root?.unmount());
+ player.client.clear();
+ }
+ });
+
+ it("falls back like a failed probe when a raw fallback exists: playlist first, then raw", async () => {
+ const fetchImpl = vi
+ .fn<typeof fetch>()
+ .mockResolvedValueOnce(response("/api/playlist?videoType=mp4", 404))
+ .mockResolvedValueOnce(
+ new Response(null, {
+ status: 206,
+ headers: { "content-type": "video/mp4" },
+ }),
+ );
+ vi.stubGlobal("fetch", fetchImpl);
+ const { props, videoRef } = playerProps({
+ rawFallbackSrc: "/api/playlist?videoType=raw-preview",
+ });
+ const player = await hydratePlayer(props);
+ try {
+ await act(async () => {
+ videoRef.current?.dispatchEvent(new Event("error"));
+ });
+ await settleFetch(fetchImpl, 2);
+ expect(fetchImpl.mock.calls[0]?.[0]).toMatch(PLAYLIST_PROBE);
+ expect(fetchImpl.mock.calls[1]?.[0]).toMatch(RAW_PROBE);
+ await vi.waitFor(() => {
+ expect(videoRef.current?.getAttribute("src")).toMatch(RAW_PROBE);
+ });
+ } finally {
+ await act(async () => player.root?.unmount());
+ player.client.clear();
+ }
+ });
+});
diff --git a/apps/web/app/s/[videoId]/Share.tsx b/apps/web/app/s/[videoId]/Share.tsx
index 69b04ff8e..57477b211 100644
--- a/apps/web/app/s/[videoId]/Share.tsx
+++ b/apps/web/app/s/[videoId]/Share.tsx
@@ -186,6 +186,7 @@ type TranscriptionStatus =
interface ShareProps {
data: VideoData;
initialPlaybackUrl?: Promise<string | null>;
+ serverPlaybackUrl?: string | null;
comments: MaybePromise<CommentWithAuthor[]>;
views: MaybePromise<number>;
screenshotImageUrl?: string | null;
@@ -321,6 +322,7 @@ const useVideoStatus = (
export const Share = ({
data,
initialPlaybackUrl,
+ serverPlaybackUrl = null,
comments,
views,
screenshotImageUrl,
@@ -962,6 +964,7 @@ export const Share = ({
) : (
<ShareVideo
initialPlaybackUrl={initialPlaybackUrl}
+ serverPlaybackUrl={serverPlaybackUrl}
data={shareVideoData}
comments={comments}
areChaptersDisabled={areChaptersDisabled}
diff --git a/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx b/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
index cd8c6ccba..ccd451528 100644
--- a/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
+++ b/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
@@ -30,6 +30,7 @@ import {
resolvePlaybackSource,
shouldFallbackToRawPlaybackSource,
} from "./playback-source";
+import { replayMissedMediaEvents } from "./replay-missed-media-events";
import {
canRetryFailedProcessing,
getUploadFailureMessage,
@@ -97,6 +98,9 @@ interface CaptionOption {
interface Props {
videoSrc: string;
initialPlaybackUrl?: Promise<string | null>;
+ // The same signed MP4 URL, resolved on the server so the <video> is in the
+ // HTML and loads before hydration. Only set for Cap's own CDN.
+ serverPlaybackUrl?: string | null;
rawFallbackSrc?: string;
videoId: Video.VideoId;
chaptersSrc: string;
@@ -148,6 +152,7 @@ interface Props {
export function CapVideoPlayer({
videoSrc,
initialPlaybackUrl,
+ serverPlaybackUrl = null,
rawFallbackSrc,
videoId,
chaptersSrc,
@@ -204,8 +209,10 @@ export function CapVideoPlayer({
null,
);
const queryClient = useQueryClient();
+ // A server-rendered source has already used the initial URL, so a refetch
+ // after a media error goes straight to the playlist route and raw fallback.
const initialPlaybackUrlUsed = useRef<Promise<string | null> | undefined>(
- undefined,
+ serverPlaybackUrl ? initialPlaybackUrl : undefined,
);
const hasRetriedInitialPlaybackSource = useRef(false);
const initialPlaybackSourceRetryPending = useRef(false);
@@ -279,6 +286,16 @@ export function CapVideoPlayer({
preferredSource,
});
},
+ initialData:
+ serverPlaybackUrl && preferredSource === "mp4"
+ ? {
+ url: serverPlaybackUrl,
+ type: "mp4",
+ supportsCrossOrigin: enableCrossOrigin,
+ fromInitialUrl: true,
+ fromServer: true,
+ }
+ : undefined,
refetchOnWindowFocus: false,
staleTime: Number.POSITIVE_INFINITY,
retry: false,
@@ -480,8 +497,10 @@ export function CapVideoPlayer({
const handleError = () => {
if (initialPlaybackSourceRetryPending.current) return;
+ // A server-rendered source never ran the client probe, so its failure
+ // takes the path a failed probe would: playlist route, then raw.
if (
- !rawFallbackSrc &&
+ (!rawFallbackSrc || resolvedSrc.data?.fromServer) &&
resolvedSrc.data?.fromInitialUrl &&
!hasRetriedInitialPlaybackSource.current
) {
@@ -620,6 +639,7 @@ export function CapVideoPlayer({
isRefreshingInitialPlaybackSource,
rawFallbackSrc,
resolvedSrc.data?.fromInitialUrl,
+ resolvedSrc.data?.fromServer,
resolvedSrc.data?.type,
resolvedSrc.data?.url,
resolvedSrc.refetch,
@@ -627,6 +647,13 @@ export function CapVideoPlayer({
videoRef.current,
]);
+ const isServerRenderedSource = Boolean(resolvedSrc.data?.fromServer);
+ useEffect(() => {
+ const video = videoRef.current;
+ if (!video || !isServerRenderedSource) return;
+ return replayMissedMediaEvents(video);
+ }, [isServerRenderedSource, videoRef]);
+
// The seek tooltip asks for a thumbnail on every render while the pointer is
// over the bar (per animation frame). The capture snapshots the *displayed*
// frame, so re-encoding is pure waste until the video has actually moved;
diff --git a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx b/apps/web/app/s/[videoId]/_components/ShareVideo.tsx
index e105fadcc..8d4db0a80 100644
--- a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx
+++ b/apps/web/app/s/[videoId]/_components/ShareVideo.tsx
@@ -85,6 +85,7 @@ export const ShareVideo = forwardRef<
hasActiveUpload?: boolean;
};
initialPlaybackUrl?: Promise<string | null>;
+ serverPlaybackUrl?: string | null;
comments: MaybePromise<CommentWithAuthor[]>;
chapters?: { title: string; start: number }[];
areChaptersDisabled?: boolean;
@@ -112,6 +113,7 @@ export const ShareVideo = forwardRef<
{
data,
initialPlaybackUrl,
+ serverPlaybackUrl = null,
comments,
chapters = NO_CHAPTERS,
areCaptionsDisabled,
@@ -564,6 +566,7 @@ export const ShareVideo = forwardRef<
videoSrc={videoSrc}
rawFallbackSrc={rawFallbackSrc}
initialPlaybackUrl={initialPlaybackUrl}
+ serverPlaybackUrl={serverPlaybackUrl}
duration={data.duration}
defaultPlaybackSpeed={defaultPlaybackSpeed}
showPlaybackStatusBadge={showPlaybackStatusBadge}
diff --git a/apps/web/app/s/[videoId]/_components/playback-source.ts b/apps/web/app/s/[videoId]/_components/playback-source.ts
index d2c2b4b49..b0af6a185 100644
--- a/apps/web/app/s/[videoId]/_components/playback-source.ts
+++ b/apps/web/app/s/[videoId]/_components/playback-source.ts
@@ -5,6 +5,7 @@ export type ResolvedPlaybackSource = {
type: "mp4" | "raw";
supportsCrossOrigin: boolean;
fromInitialUrl?: true;
+ fromServer?: true;
};
type ProbeResult = {
diff --git a/apps/web/app/s/[videoId]/_components/replay-missed-media-events.ts b/apps/web/app/s/[videoId]/_components/replay-missed-media-events.ts
new file mode 100644
index 000000000..243494516
--- /dev/null
+++ b/apps/web/app/s/[videoId]/_components/replay-missed-media-events.ts
@@ -0,0 +1,66 @@
+// A <video> rendered into the server HTML starts loading before React hydrates,
+// so loadedmetadata, loadeddata, canplay, an autoplay start or an error can all
+// fire before any effect has added its listener. Replaying the ones the element
+// has already reached, once every hydration effect has subscribed, gives each
+// listener the same events it would get from a video mounted on the client.
+
+type ReplayableVideo = Pick<
+ HTMLVideoElement,
+ | "addEventListener"
+ | "removeEventListener"
+ | "dispatchEvent"
+ | "readyState"
+ | "paused"
+ | "error"
+>;
+
+const HAVE_METADATA = 1;
+const HAVE_CURRENT_DATA = 2;
+const HAVE_FUTURE_DATA = 3;
+
+const REPLAYABLE_EVENTS: ReadonlyArray<
+ readonly [type: string, reached: (video: ReplayableVideo) => boolean]
+> = [
+ ["loadedmetadata", (video) => video.readyState >= HAVE_METADATA],
+ ["loadeddata", (video) => video.readyState >= HAVE_CURRENT_DATA],
+ ["canplay", (video) => video.readyState >= HAVE_FUTURE_DATA],
+ ["play", (video) => !video.paused],
+ ["error", (video) => video.error != null],
+];
+
+export function replayMissedMediaEvents(
+ video: ReplayableVideo,
+ schedule: (replay: () => void) => () => void = (replay) => {
+ const timer = setTimeout(replay, 0);
+ return () => clearTimeout(timer);
+ },
+): () => void {
+ const seen = new Set<string>();
+ const markSeen = (event: Event) => {
+ seen.add(event.type);
+ };
+ for (const [type] of REPLAYABLE_EVENTS) {
+ video.addEventListener(type, markSeen);
+ }
+ const stopListening = () => {
+ for (const [type] of REPLAYABLE_EVENTS) {
+ video.removeEventListener(type, markSeen);
+ }
+ };
+
+ // Scheduled rather than run inline so listeners added by effects that run
+ // after this one, in parent components, are subscribed first.
+ const cancel = schedule(() => {
+ stopListening();
+ for (const [type, reached] of REPLAYABLE_EVENTS) {
+ if (!seen.has(type) && reached(video)) {
+ video.dispatchEvent(new Event(type));
+ }
+ }
+ });
+
+ return () => {
+ cancel();
+ stopListening();
+ };
+}
diff --git a/apps/web/app/s/[videoId]/page.tsx b/apps/web/app/s/[videoId]/page.tsx
index 1c82858a6..5475a6840 100644
--- a/apps/web/app/s/[videoId]/page.tsx
+++ b/apps/web/app/s/[videoId]/page.tsx
@@ -62,7 +62,10 @@ import { runPromise } from "@/lib/server";
import { getSharePageBranding } from "@/lib/share-branding";
import { parseShareCallToAction } from "@/lib/share-call-to-action";
import { getShareDashboardDestination } from "@/lib/share-dashboard-destination";
-import { getSharePlaybackUrl } from "@/lib/share-playback";
+import {
+ canStartPlaybackInHtml,
+ getSharePlaybackSource,
+} from "@/lib/share-playback";
import { buildShareVideoMetadata } from "@/lib/share-video-metadata";
import { resolveShareWebUrl } from "@/lib/share-web-url";
import { isVideoOverShareableLinkLimit } from "@/lib/shareable-link-quota";
@@ -126,6 +129,25 @@ function toShareVideo<
}
// Helper function to fetch shared spaces data for a video
+// The longest the page waits for the playback URL beyond its other queries.
+const SERVER_PLAYBACK_WAIT_MS = 50;
+
+function resolveWithin<T>(promise: Promise<T>, ms: number): Promise<T | null> {
+ return new Promise((resolve) => {
+ const timer = setTimeout(() => resolve(null), ms);
+ promise.then(
+ (value) => {
+ clearTimeout(timer);
+ resolve(value);
+ },
+ () => {
+ clearTimeout(timer);
+ resolve(null);
+ },
+ );
+ });
+}
+
async function getSharedSpacesForVideo(videoId: Video.VideoId) {
// Space-level and organization-level sharing are independent queries.
const [spaceSharing, orgSharing] = await Promise.all([
@@ -549,14 +571,31 @@ async function AuthorizedContent({
);
return false;
});
- const initialPlaybackUrlPromise =
+ const initialPlaybackSourcePromise =
!video.isScreenshot &&
!hasActiveUpload &&
(video.source.type === "desktopMP4" || video.source.type === "webMP4")
? overShareLimitPromise.then((overLimit) =>
- overLimit ? null : getSharePlaybackUrl(video),
+ overLimit ? null : getSharePlaybackSource(video),
)
: undefined;
+ const initialPlaybackUrlPromise = initialPlaybackSourcePromise?.then(
+ (source) => source?.url ?? null,
+ );
+ // On Cap's own CDN the signed URL is ready as soon as the share-limit check
+ // is (signing is local), so it is awaited with the rest of the page and
+ // rendered into the HTML: the browser starts the video download before the
+ // player hydrates. Custom buckets and storage integrations keep resolving it
+ // on the client, behind the probe that falls back to the playlist route, and
+ // so does this source if it is not ready in time.
+ const serverPlaybackSourcePromise =
+ buildEnv.NEXT_PUBLIC_IS_CAP === "true" &&
+ !video.bucket &&
+ !video.storageIntegrationId &&
+ initialPlaybackSourcePromise &&
+ canStartPlaybackInHtml((await headers()).get("user-agent"))
+ ? resolveWithin(initialPlaybackSourcePromise, SERVER_PLAYBACK_WAIT_MS)
+ : Promise.resolve(null);
const aiGenerationEnabledPromise = isAiGenerationEnabled(video.owner);
const imagesPromise = Effect.gen(function* () {
@@ -815,6 +854,7 @@ async function AuthorizedContent({
ownerIsOverShareLimit,
resolvedImages,
dashboardDestination,
+ serverPlaybackSource,
] = await Promise.all([
spacesDataPromise,
sharedSpacesPromise,
@@ -830,6 +870,7 @@ async function AuthorizedContent({
overShareLimitPromise,
imagesPromise,
dashboardDestinationPromise,
+ serverPlaybackSourcePromise,
viewNotificationPromise,
]);
@@ -991,6 +1032,9 @@ async function AuthorizedContent({
dashboardDestination={dashboardDestination}
data={videoWithOrganizationInfo}
initialPlaybackUrl={initialPlaybackUrlPromise}
+ serverPlaybackUrl={
+ serverPlaybackSource?.defaultStorage ? serverPlaybackSource.url : null
+ }
screenshotImageUrl={screenshotImageUrl}
videoSettings={videoWithOrganizationInfo.settings}
comments={commentsPromise}
diff --git a/apps/web/lib/share-playback.ts b/apps/web/lib/share-playback.ts
index bb8625a2c..50f947f9d 100644
--- a/apps/web/lib/share-playback.ts
+++ b/apps/web/lib/share-playback.ts
@@ -1,7 +1,7 @@
import type { videos } from "@cap/database/schema";
import { Storage } from "@cap/web-backend";
import { type User, Video } from "@cap/web-domain";
-import { Effect, Schema } from "effect";
+import { Effect, Option, Schema } from "effect";
import { runPromise } from "@/lib/server";
type SharePlaybackVideo = Omit<
@@ -9,7 +9,15 @@ type SharePlaybackVideo = Omit<
"folderId" | "password" | "settings" | "ownerId"
> & { owner: { id: User.UserId } };
-export const getSharePlaybackUrl = (video: SharePlaybackVideo) =>
+export type SharePlaybackSource = {
+ url: string;
+ // Cap's own bucket, not a custom bucket or a storage integration.
+ defaultStorage: boolean;
+};
+
+export const getSharePlaybackSource = (
+ video: SharePlaybackVideo,
+): Promise<SharePlaybackSource | null> =>
Effect.gen(function* () {
const loadedVideo = yield* Schema.decodeUnknown(Video.Video)({
...video,
@@ -19,10 +27,34 @@ export const getSharePlaybackUrl = (video: SharePlaybackVideo) =>
createdAt: video.createdAt.toISOString(),
updatedAt: video.updatedAt.toISOString(),
});
- const [bucket] = yield* Storage.getAccessForVideo(loadedVideo);
+ const [bucket, customBucket] =
+ yield* Storage.getAccessForVideo(loadedVideo);
const source = Video.Video.getSource(loadedVideo);
if (!(source instanceof Video.Mp4Source)) return null;
- return yield* bucket.getSignedObjectUrl(source.getFileKey());
+ const url = yield* bucket.getSignedObjectUrl(source.getFileKey());
+ return {
+ url,
+ defaultStorage:
+ Option.isNone(customBucket) &&
+ Option.isNone(loadedVideo.storageIntegrationId),
+ };
})
.pipe(runPromise)
.catch(() => null);
+
+export const getSharePlaybackUrl = (video: SharePlaybackVideo) =>
+ getSharePlaybackSource(video).then((source) => source?.url ?? null);
+
+// iOS Safari may need the player to rewrite an MP4's AVC level before it can
+// decode it, and iPadOS reports a desktop Safari user agent. Those browsers
+// keep resolving the source after hydration, where that check runs first.
+export function canStartPlaybackInHtml(userAgent: string | null): boolean {
+ if (!userAgent) return false;
+ const ua = userAgent.toLowerCase();
+ if (/iphone|ipod|ipad/.test(ua)) return false;
+ const isDesktopSafari =
+ ua.includes("macintosh") &&
+ ua.includes("safari/") &&
+ !/chrome\/|chromium\/|crios\/|fxios\/|edg\/|firefox\//.test(ua);
+ return !isDesktopSafari;
+}