Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(media-server): stop chunked verify as soon as chunk headers differ
Richie McIlroy477d4f71193d
diff --git a/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts b/apps/media-server/src/__tests__/lib/recording-verification.integration.test.tsindex 7141d3195..0eb494da0 100644--- a/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts+++ b/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts@@ -2645,6 +2645,102 @@ describe("chunked packet-proven verification", () => {expect(chunked.video.frameCount).toBe(120);});+ test("falls back as soon as a later chunk starts at a different resolution", async () => {+ const name = "chunk-resize";+ const halves: string[] = [];+ for (const size of ["320x180", "352x180"]) {+ const half = join(directory, `${name}-${size}.h264`);+ await run([+ "ffmpeg",+ "-v",+ "error",+ "-f",+ "lavfi",+ "-i",+ `testsrc2=size=${size}:rate=30`,+ "-frames:v",+ "90",+ "-c:v",+ "libx264",+ "-preset",+ "ultrafast",+ "-g",+ "15",+ "-keyint_min",+ "15",+ "-sc_threshold",+ "0",+ "-pix_fmt",+ "yuv420p",+ "-bsf:v",+ "h264_mp4toannexb",+ "-f",+ "h264",+ half,+ ]);+ halves.push(half);+ }+ const stream = join(directory, `${name}.h264`);+ await writeFile(+ stream,+ Buffer.concat(await Promise.all(halves.map((path) => readFile(path)))),+ );+ const fragments = await mkdtemp(join(directory, `${name}-`));+ await run([+ "ffmpeg",+ "-v",+ "error",+ "-framerate",+ "30",+ "-f",+ "h264",+ "-i",+ stream,+ "-c",+ "copy",+ "-f",+ "dash",+ "-seg_duration",+ "0.5",+ "-use_template",+ "1",+ "-use_timeline",+ "1",+ "-init_seg_name",+ "init.mp4",+ "-media_seg_name",+ "fragment-$Number%05d$.m4s",+ join(fragments, "manifest.mpd"),+ ]);+ const init = join(fragments, "init.mp4");+ const segments = (await readdir(fragments))+ .filter((file) => /^fragment-\d+\.m4s$/.test(file))+ .sort()+ .map((file) => join(fragments, file));+ expect(segments.length).toBeGreaterThanOrEqual(6);+ const combined = join(directory, `${name}-combined.mp4`);+ await writeFile(+ combined,+ Buffer.concat(+ await Promise.all([init, ...segments].map((path) => readFile(path))),+ ),+ );+ const output = join(directory, `${name}-output.mp4`);+ await muxMediaTracksToMp4(combined, silent, output);+ const { single, chunked, fallbacks } = await both({+ init,+ segments,+ combined,+ output,+ });+ expect(fallbacks).toHaveLength(1);+ expect(String(fallbacks[0][0])).toContain(+ "dimensions 0: 320x180 -> 352x180",+ );+ expect(chunked).toEqual(single);+ expect(chunked.video.frameCount).toBe(180);+ });+test("copies many fragments into one run without piling up stream listeners", async () => {const recording = await segmentedRecording("chunk-many", 6, 240, 0.2);expect(recording.segments.length).toBeGreaterThanOrEqual(30);diff --git a/apps/media-server/src/lib/recording-verification.ts b/apps/media-server/src/lib/recording-verification.tsindex 5ec9f4d6b..5d02c33a7 100644--- a/apps/media-server/src/lib/recording-verification.ts+++ b/apps/media-server/src/lib/recording-verification.ts@@ -686,6 +686,32 @@ class ChunkedDecodeUnavailable extends Error {}const DECODED_STREAM_METADATA =/^#(tb|media_type|sample_rate|dimensions|sar|channel_layout_name) (\d+):/;+const metadataOf = (lines: string[]) =>+ lines.filter((line) => DECODED_STREAM_METADATA.test(line)).join("\n");++// Names the header fields that changed, e.g. "dimensions 0: 1340x720 -> 1344x720".+function describeHeaderChange(first: string, other: string): string {+ const fields = (header: string) =>+ new Map(+ header+ .split("\n")+ .filter(Boolean)+ .map((line) => {+ const [name, ...value] = line.slice(1).split(": ");+ return [name, value.join(": ")] as const;+ }),+ );+ const before = fields(first);+ const after = fields(other);+ const changes = [...new Set([...before.keys(), ...after.keys()])]+ .filter((name) => before.get(name) !== after.get(name))+ .map(+ (name) =>+ `${name}: ${before.get(name) ?? "none"} -> ${after.get(name) ?? "none"}`,+ );+ return changes.join("; ").slice(0, 200) || "unknown";+}+async function collectDecoderLines(stream: AsyncIterable<Uint8Array>,lines: string[],@@ -1048,13 +1074,39 @@ async function decodePacketProvenChunks(: []),];const errors: Promise<{ error: string }>[] = [];- for (const args of jobs) {+ // A chunk's header describes its first decoded frame, so a mismatch is known+ // as soon as each chunk emits one frame; stop then instead of decoding everything first.+ const headers = new Map<number, string>();+ const checkHeader = (index: number, lines: string[]) => {+ if (headers.has(index)) return;+ headers.set(index, metadataOf(lines));+ const first = headers.get(0);+ if (first === undefined) return;+ for (const [other, header] of headers) {+ if (other === 0 || header === first) continue;+ failure ??= new ChunkedDecodeUnavailable(+ `chunk stream parameters differ (${describeHeaderChange(first, header)})`,+ );+ stopAll();+ return;+ }+ };+ for (const [index, args] of jobs.entries()) {options.abortSignal?.throwIfAborted();const decoder = registerSubprocess(await spawnRecordingDecoder(args, 0));decoders.push(decoder);const lines: string[] = [];outputs.push(lines);- const frames = collectDecoderLines(decoder.stdout, lines, onFrame);+ const frames = collectDecoderLines(+ decoder.stdout,+ lines,+ index < chunkPaths.length+ ? () => {+ checkHeader(index, lines);+ onFrame();+ }+ : onFrame,+ );const stderr = readDecoderErrors(decoder.stderr, outputPath);frames.catch(stopAll);stderr.catch(stopAll);@@ -1073,13 +1125,14 @@ async function decodePacketProvenChunks(}const streams = new Map<number, DecodedStream>();const videoOutputs = outputs.slice(0, chunkPaths.length);- const metadataOf = (lines: string[]) =>- lines.filter((line) => DECODED_STREAM_METADATA.test(line)).join("\n");const videoMetadata = metadataOf(videoOutputs[0]);let offset = 0;for (const [index, lines] of videoOutputs.entries()) {- if (index > 0 && metadataOf(lines) !== videoMetadata)- throw new ChunkedDecodeUnavailable("chunk stream parameters differ");+ const metadata = metadataOf(lines);+ if (index > 0 && metadata !== videoMetadata)+ throw new ChunkedDecodeUnavailable(+ `chunk stream parameters differ (${describeHeaderChange(videoMetadata, metadata)})`,+ );let frames = 0;for (const line of lines) {if (line.startsWith("#")) {