Open source CapCut alternative.
TypeScript
fix(site): play HDR phone clips in pages without SharedArrayBuffer
David Lecadeda793cef
diff --git a/site/src/cut/lib/mediaRead.test.ts b/site/src/cut/lib/mediaRead.test.tsindex e177509e..c5479d27 100644--- a/site/src/cut/lib/mediaRead.test.ts+++ b/site/src/cut/lib/mediaRead.test.ts@@ -1,6 +1,6 @@import { describe, expect, test } from "bun:test";import { CHUNK_SIZE } from "./chunkCache";-import { urlParallelism } from "./mediaRead";+import { spsHooks, urlParallelism } from "./mediaRead";/** The cache one reader is given by the memory budget on an ordinary machine. */const READER_CACHE = 8 * 2 ** 20;@@ -30,3 +30,23 @@ describe("read width", () => {expect(urlParallelism("http://127.0.0.1:41417/file?p=a.mp4", READER_CACHE)).toBe(2);});});++describe("SPS color hooks", () => {+ // A page served without cross-origin isolation has no SharedArrayBuffer+ // global; the hooks run there for every HDR phone clip.+ test("retag an HEVC HLG description in a page without SharedArrayBuffer", () => {+ const hevc10hlg =+ "AQIgAAAAsAAAAAAAHvAA/P36+gAADwOgAAEAGEABDAH//wIgAAADALAAAAMAAAMAHhcCQKEAAQAqQgEBAiAAAAMAsAAAAwAAAwAeoBQgQcGO2IF7kWRS/8ufxP6wFqEiQSAQogABAAdEAcBy9FNk";+ const description = Uint8Array.from(atob(hevc10hlg), (c) => c.charCodeAt(0));+ const sab = Object.getOwnPropertyDescriptor(globalThis, "SharedArrayBuffer")!;+ delete (globalThis as { SharedArrayBuffer?: unknown }).SharedArrayBuffer;+ try {+ const hooks = spsHooks({ primaries: "bt709", transfer: "bt709", matrix: "bt709", fullRange: false });+ const config = hooks.transformConfig!({ codec: "hvc1.2.4.L123.B0", description });+ expect(config.description).toBeDefined();+ expect(config.description).not.toEqual(description);+ } finally {+ Object.defineProperty(globalThis, "SharedArrayBuffer", sab);+ }+ });+});diff --git a/site/src/cut/lib/mediaRead.ts b/site/src/cut/lib/mediaRead.tsindex 248a1ec6..9cd0e321 100644--- a/site/src/cut/lib/mediaRead.ts+++ b/site/src/cut/lib/mediaRead.ts@@ -509,7 +509,7 @@ export interface FrameSinkOptions {/** Decoder hooks that write `colorSpace` into the bitstream's own SPS tags* for H.264 and HEVC; empty for every other codec. */-function spsHooks(colorSpace: VideoColorSpaceInit): {+export function spsHooks(colorSpace: VideoColorSpaceInit): {transformConfig?: (config: VideoDecoderConfig) => VideoDecoderConfig;transformPacket?: (data: Uint8Array, config: VideoDecoderConfig) => Uint8Array | null;} {@@ -530,9 +530,9 @@ function spsHooks(colorSpace: VideoColorSpaceInit): {}const bytesOf = (buffer: AllowSharedBufferSource): Uint8Array =>- buffer instanceof ArrayBuffer || buffer instanceof SharedArrayBuffer- ? new Uint8Array(buffer)- : new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);+ ArrayBuffer.isView(buffer)+ ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)+ : new Uint8Array(buffer);export type FrameSinkFactory = (track: InputVideoTrack,