mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 19:36:04 +00:00
perf(cloud): unify shared storage with chunk-scoped range reads
This commit is contained in:
@@ -1322,7 +1322,6 @@
|
||||
|
||||
"bowser": ["bowser@2.14.1", "", {}, "sha512-tzPjzCxygAKWFOJP011oxFHs57HzIhOEracIgAePE4pqB3LikALKnSzUyU4MGs9/iCEUuHlAJTjTc5M+u7YEGg=="],
|
||||
|
||||
|
||||
"brace-expansion": ["brace-expansion@5.0.6", "", { "dependencies": { "balanced-match": "^4.0.2" } }, "sha512-kLpxurY4Z4r9sgMsyG0Z9uzsBlgiU/EFKhj/h91/8yHu0edo7XuixOIH3VcJ8kkxs6/jPzoI6U9Vj3WqbMQ94g=="],
|
||||
|
||||
"brotli": ["brotli@1.3.3", "", { "dependencies": { "base64-js": "^1.1.2" } }, "sha512-oTKjJdShmDuGW94SyyaoQvAjf30dZaHnjJ8uAF+u2/vGJkJbJPJAT1gDiOJP5v1Zb6f9KEyW/1HpuaWIXtGHPg=="],
|
||||
|
||||
@@ -23,6 +23,7 @@ import { dirname, join } from "node:path";
|
||||
import { S3Client } from "@aws-sdk/client-s3";
|
||||
import {
|
||||
CURRENT_PLAN_PROTOCOL,
|
||||
PLAN_V2_MATERIALIZATION_MARKER,
|
||||
PlanVideosMetadataError,
|
||||
type AssembleResult,
|
||||
type ChunkResult,
|
||||
@@ -35,6 +36,7 @@ import {
|
||||
import { recomputePlanHashFromPlanDir } from "../../producer/src/services/render/stages/freezePlan.js";
|
||||
import type { AssembleEvent, LambdaEvent, PlanEvent, RenderChunkEvent } from "./events.js";
|
||||
import { handler, unwrapEvent } from "./handler.js";
|
||||
import { S3PlanV2ArtifactPublisher } from "./s3PlanV2Publisher.js";
|
||||
|
||||
interface FakeS3Op {
|
||||
kind: "download" | "upload";
|
||||
@@ -52,6 +54,7 @@ class FakeS3Client {
|
||||
// Map S3 URIs → byte buffers the fake serves.
|
||||
objects = new Map<string, Buffer>();
|
||||
metadata = new Map<string, Record<string, string>>();
|
||||
checksums = new Map<string, string>();
|
||||
|
||||
// Methods called by the real S3 transport — minimal surface so the
|
||||
// handler's call sites don't need rewriting under test.
|
||||
@@ -85,6 +88,7 @@ class FakeS3Client {
|
||||
return {
|
||||
ContentLength: bytes.length,
|
||||
Metadata: this.metadata.get(uri),
|
||||
ChecksumSHA256: this.checksums.get(uri),
|
||||
};
|
||||
}
|
||||
if (cmdName === "PutObjectCommand") {
|
||||
@@ -104,12 +108,24 @@ class FakeS3Client {
|
||||
this.objects.set(uri, bytes);
|
||||
const metadata = (command as { input: { Metadata?: Record<string, string> } }).input.Metadata;
|
||||
if (metadata) this.metadata.set(uri, metadata);
|
||||
const checksum = (command as { input: { ChecksumSHA256?: string } }).input.ChecksumSHA256;
|
||||
if (checksum) this.checksums.set(uri, checksum);
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/** A real client (so it can pre-sign offline) whose requests go to the fake. */
|
||||
function presigningClient(fake: FakeS3Client): S3Client {
|
||||
const client = new S3Client({
|
||||
region: "us-east-2",
|
||||
credentials: { accessKeyId: "AKIDTEST", secretAccessKey: "test-secret" },
|
||||
});
|
||||
client.send = fake.send.bind(fake) as S3Client["send"];
|
||||
return client;
|
||||
}
|
||||
|
||||
const tmpDirs: string[] = [];
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -684,6 +700,85 @@ describe("handler dispatch", () => {
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("hands a chunk its deferred video as a pre-signed URL and downloads the rest", async () => {
|
||||
const tmpRoot = makeTmpRoot();
|
||||
const fake = new FakeS3Client();
|
||||
const s3 = presigningClient(fake);
|
||||
const v1Dir = join(tmpRoot, "v1");
|
||||
makeMinimalV1PlanDir(v1Dir, true, true);
|
||||
const publisher = new S3PlanV2ArtifactPublisher({
|
||||
s3,
|
||||
planOutputS3Prefix: "s3://bucket/renders/deferred/",
|
||||
temporaryRoot: tmpRoot,
|
||||
});
|
||||
const manifest = await publishPlanV2FromExecutionPlan(v1Dir, publisher);
|
||||
const video = manifest.artifacts.find((artifact) => artifact.path === "compiled/clip.mp4")!;
|
||||
expect(video.chunks).toEqual([0]);
|
||||
const blobUri = (digest: string) =>
|
||||
`${publisher.artifactPrefix}/${digest.slice(0, 2)}/${digest}`;
|
||||
|
||||
let marker: { remoteSources?: Record<string, string> } = {};
|
||||
const renderChunkMock = mock(
|
||||
async (planDir: string, _chunkIndex: number, outputPath: string): Promise<ChunkResult> => {
|
||||
expect(existsSync(join(planDir, "compiled", "clip.mp4"))).toBe(false);
|
||||
marker = JSON.parse(readFileSync(join(planDir, PLAN_V2_MATERIALIZATION_MARKER), "utf-8"));
|
||||
writeFileSync(outputPath, "CHUNK");
|
||||
return {
|
||||
outputPath,
|
||||
outputKind: "file",
|
||||
framesEncoded: 30,
|
||||
sha256: "c".repeat(64),
|
||||
durationMs: 1,
|
||||
planHashMs: 0,
|
||||
sessionBootMs: 0,
|
||||
captureStageMs: 1,
|
||||
encodeStageMs: 0,
|
||||
workers: 1,
|
||||
captureMode: "beginframe",
|
||||
perfPath: `${outputPath}.perf.json`,
|
||||
};
|
||||
},
|
||||
);
|
||||
const beforeChunk = fake.ops.length;
|
||||
await handler(
|
||||
{
|
||||
Action: "renderChunk",
|
||||
PlanV2ManifestS3Uri: publisher.manifestUri,
|
||||
PlanV2ArtifactS3Prefix: publisher.artifactPrefix,
|
||||
PlanHash: manifest.planHash,
|
||||
ChunkIndex: 0,
|
||||
ChunkOutputS3Prefix: "s3://bucket/renders/deferred/",
|
||||
Format: "mp4",
|
||||
},
|
||||
{
|
||||
s3,
|
||||
primitives: {
|
||||
plan: mock() as unknown as typeof import("@hyperframes/producer/distributed").plan,
|
||||
renderChunk:
|
||||
renderChunkMock as unknown as typeof import("@hyperframes/producer/distributed").renderChunk,
|
||||
assemble:
|
||||
mock() as unknown as typeof import("@hyperframes/producer/distributed").assemble,
|
||||
},
|
||||
tmpRoot,
|
||||
skipChromeResolution: true,
|
||||
},
|
||||
);
|
||||
|
||||
const downloads = fake.ops
|
||||
.slice(beforeChunk)
|
||||
.filter((operation) => operation.kind === "download")
|
||||
.map((operation) => operation.uri);
|
||||
const expectedDownloads = manifest.artifacts
|
||||
.filter((artifact) => artifact.chunks === "all")
|
||||
.map((artifact) => blobUri(artifact.sha256));
|
||||
expect(new Set(downloads)).toEqual(new Set([publisher.manifestUri, ...expectedDownloads]));
|
||||
const url = new URL(marker.remoteSources?.["compiled/clip.mp4"] ?? "");
|
||||
expect(url.protocol).toBe("https:");
|
||||
expect(url.pathname).toEndWith(`/${video.sha256}`);
|
||||
expect(url.searchParams.get("X-Amz-Expires")).toBe("3600");
|
||||
expect(url.searchParams.get("X-Amz-Signature")).toMatch(/^[a-f0-9]{64}$/);
|
||||
});
|
||||
|
||||
it("rejects unknown plan protocol values", async () => {
|
||||
const tmpRoot = makeTmpRoot();
|
||||
const s3 = new FakeS3Client();
|
||||
@@ -983,7 +1078,7 @@ async function makeMinimalPlanTar(): Promise<Buffer> {
|
||||
return rf(tarPath);
|
||||
}
|
||||
|
||||
function makeMinimalV1PlanDir(dir: string, withAudio: boolean): void {
|
||||
function makeMinimalV1PlanDir(dir: string, withAudio: boolean, withDeferredVideo = false): void {
|
||||
mkdirSync(join(dir, "meta"), { recursive: true });
|
||||
mkdirSync(join(dir, "compiled"), { recursive: true });
|
||||
writeFileSync(join(dir, "compiled", "index.html"), "<html>aws v2 fixture</html>");
|
||||
@@ -1003,6 +1098,54 @@ function makeMinimalV1PlanDir(dir: string, withAudio: boolean): void {
|
||||
);
|
||||
writeFileSync(join(dir, "meta", "encoder.json"), "{}");
|
||||
if (withAudio) writeFileSync(join(dir, "audio.m4a"), "AAC");
|
||||
if (withDeferredVideo) writeDeferredVideo(dir);
|
||||
planJson.planHash = recomputePlanHashFromPlanDir(dir);
|
||||
writeFileSync(join(dir, "plan.json"), JSON.stringify(planJson));
|
||||
}
|
||||
|
||||
/** One clip the chunk extracts from `compiled/clip.mp4` itself. */
|
||||
function writeDeferredVideo(dir: string): void {
|
||||
writeFileSync(join(dir, "compiled", "clip.mp4"), "VIDEO-BYTES");
|
||||
const metadata = {
|
||||
durationSeconds: 1,
|
||||
videoStreamDurationSeconds: 1,
|
||||
width: 640,
|
||||
height: 360,
|
||||
fps: 30,
|
||||
videoCodec: "h264",
|
||||
hasAudio: false,
|
||||
isVFR: false,
|
||||
hasAlpha: false,
|
||||
colorSpace: null,
|
||||
};
|
||||
const videos = {
|
||||
videos: [
|
||||
{
|
||||
id: "clip",
|
||||
src: "clip.mp4",
|
||||
start: 0,
|
||||
end: 1,
|
||||
mediaStart: 0,
|
||||
loop: false,
|
||||
hasAudio: false,
|
||||
},
|
||||
],
|
||||
extracted: [
|
||||
{
|
||||
videoId: "clip",
|
||||
srcPath: "/planner/clip.mp4",
|
||||
framePattern: "frame_%05d.jpg",
|
||||
fps: 30,
|
||||
totalFrames: 30,
|
||||
metadata,
|
||||
deferredRange: {
|
||||
sourcePath: "compiled/clip.mp4",
|
||||
startTime: 0,
|
||||
durationSeconds: 1,
|
||||
format: "jpg",
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
writeFileSync(join(dir, "meta", "videos.json"), JSON.stringify(videos));
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
resolvePlanAudioPath,
|
||||
planV2WithPublisher,
|
||||
type PlanResult,
|
||||
isPlanV2DeferredSourceArtifact,
|
||||
type PlanV2Artifact,
|
||||
type PlanV2Manifest,
|
||||
type PlanV2MaterializationTarget,
|
||||
@@ -57,6 +58,7 @@ import {
|
||||
downloadS3ObjectToFileVerified,
|
||||
mapConcurrent,
|
||||
parseS3Uri,
|
||||
presignVerifiedS3GetUrl,
|
||||
tarDirectory,
|
||||
untarDirectory,
|
||||
uploadFileToS3,
|
||||
@@ -723,17 +725,54 @@ async function downloadAndMaterializePlanV2(
|
||||
throwPlanHashMismatch(event.PlanHash, manifest.planHash);
|
||||
}
|
||||
const artifacts = listPlanV2ArtifactsForTarget(manifest, target);
|
||||
const remoteSources = await presignDeferredSources(s3, event.PlanV2ArtifactS3Prefix, artifacts);
|
||||
const uniqueArtifacts = [
|
||||
...new Map(artifacts.map((artifact) => [artifact.sha256, artifact])).values(),
|
||||
...new Map(
|
||||
artifacts
|
||||
.filter((artifact) => !Object.hasOwn(remoteSources, artifact.path))
|
||||
.map((artifact) => [artifact.sha256, artifact]),
|
||||
).values(),
|
||||
];
|
||||
await mapConcurrent(uniqueArtifacts, 16, async (artifact) => {
|
||||
await downloadPlanV2Artifact(s3, event.PlanV2ArtifactS3Prefix, transportDir, artifact);
|
||||
});
|
||||
const planDir = join(work, "plan");
|
||||
materializePlanV2Target(transportDir, target, planDir);
|
||||
materializePlanV2Target(transportDir, target, planDir, { remoteSources });
|
||||
return planDir;
|
||||
}
|
||||
|
||||
/** Outlives the longest chunk (Lambda caps an invocation at 900 s) with margin. */
|
||||
const DEFERRED_SOURCE_URL_TTL_SECONDS = 3600;
|
||||
|
||||
/**
|
||||
* A chunk's ffmpeg reads a deferred video by byte range from a pre-signed URL
|
||||
* instead of downloading it whole. The key is the content hash, written once.
|
||||
*/
|
||||
async function presignDeferredSources(
|
||||
s3: S3Client,
|
||||
artifactPrefix: string,
|
||||
artifacts: readonly Readonly<PlanV2Artifact>[],
|
||||
): Promise<Record<string, string>> {
|
||||
const entries = await Promise.all(
|
||||
artifacts
|
||||
.filter(isPlanV2DeferredSourceArtifact)
|
||||
.map(
|
||||
async (artifact) =>
|
||||
[
|
||||
artifact.path,
|
||||
await presignVerifiedS3GetUrl(
|
||||
s3,
|
||||
planV2BlobUri(artifactPrefix, artifact.sha256),
|
||||
artifact.sha256,
|
||||
artifact.sizeBytes,
|
||||
DEFERRED_SOURCE_URL_TTL_SECONDS,
|
||||
),
|
||||
] as const,
|
||||
),
|
||||
);
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
|
||||
async function downloadPlanV2Artifact(
|
||||
s3: S3Client,
|
||||
artifactPrefix: string,
|
||||
|
||||
@@ -10,10 +10,12 @@ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { CONTENT_STORE_REFRESH_AFTER_DAYS, contentStoreUri } from "./contentStore.js";
|
||||
import { HeadObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
||||
import {
|
||||
downloadS3ObjectToFileVerified,
|
||||
formatS3Uri,
|
||||
parseS3Uri,
|
||||
presignVerifiedS3GetUrl,
|
||||
sha256File,
|
||||
tarDirectory,
|
||||
untarDirectory,
|
||||
@@ -212,6 +214,54 @@ describe("content-addressed v2 artifacts", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("presignVerifiedS3GetUrl", () => {
|
||||
const digest = "ab".repeat(32);
|
||||
const uri = `s3://bucket/v2/artifacts/sha256/ab/${digest}`;
|
||||
function clientHolding(head: { ChecksumSHA256?: string; ContentLength: number }): S3Client {
|
||||
const client = new S3Client({
|
||||
region: "us-east-2",
|
||||
credentials: { accessKeyId: "AKIDTEST", secretAccessKey: "test-secret" },
|
||||
});
|
||||
client.send = (async (command: unknown) => {
|
||||
expect(command).toBeInstanceOf(HeadObjectCommand);
|
||||
expect((command as HeadObjectCommand).input.ChecksumMode).toBe("ENABLED");
|
||||
return head;
|
||||
}) as S3Client["send"];
|
||||
return client;
|
||||
}
|
||||
|
||||
it.each([
|
||||
{ uri, key: `/v2/artifacts/sha256/ab/${digest}` },
|
||||
{ uri: contentStoreUri("bucket", digest), key: `/cas/sha256/ab/${digest}` },
|
||||
])("signs an attested legacy or shared-content source at $key", async (source) => {
|
||||
const checksum = Buffer.from(digest, "hex").toString("base64");
|
||||
const url = new URL(
|
||||
await presignVerifiedS3GetUrl(
|
||||
clientHolding({ ChecksumSHA256: checksum, ContentLength: 42 }),
|
||||
source.uri,
|
||||
digest,
|
||||
42,
|
||||
600,
|
||||
),
|
||||
);
|
||||
expect(url.pathname).toBe(source.key);
|
||||
expect(url.searchParams.get("X-Amz-Expires")).toBe("600");
|
||||
});
|
||||
|
||||
it("refuses an object whose stored checksum is not the pinned digest", async () => {
|
||||
const other = Buffer.from("cd".repeat(32), "hex").toString("base64");
|
||||
await expect(
|
||||
presignVerifiedS3GetUrl(
|
||||
clientHolding({ ChecksumSHA256: other, ContentLength: 42 }),
|
||||
uri,
|
||||
digest,
|
||||
42,
|
||||
600,
|
||||
),
|
||||
).rejects.toMatchObject({ name: "PLAN_ARTIFACT_DIGEST_MISMATCH" });
|
||||
});
|
||||
});
|
||||
|
||||
class ContentAddressedFakeS3 {
|
||||
readonly objects = new Map<string, { bytes: Buffer; sha256: string; lastModified?: Date }>();
|
||||
readonly copies: Record<string, unknown>[] = [];
|
||||
|
||||
@@ -104,6 +104,35 @@ export async function downloadS3ObjectToFileVerified(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-sign a GET for a plan-v2 artifact, after S3 attests the object's stored
|
||||
* SHA-256 and size are the ones the manifest pins.
|
||||
*/
|
||||
export async function presignVerifiedS3GetUrl(
|
||||
client: S3Client,
|
||||
uri: string,
|
||||
expectedSha256: string,
|
||||
expectedSize: number,
|
||||
expiresInSeconds: number,
|
||||
): Promise<string> {
|
||||
assertSha256(expectedSha256);
|
||||
const { bucket, key } = parseS3Uri(uri);
|
||||
const head = await client.send(
|
||||
new HeadObjectCommand({ Bucket: bucket, Key: key, ChecksumMode: "ENABLED" }),
|
||||
);
|
||||
const expectedChecksum = Buffer.from(expectedSha256, "hex").toString("base64");
|
||||
if (head.ChecksumSHA256 !== expectedChecksum || head.ContentLength !== expectedSize) {
|
||||
const error = new Error(
|
||||
`[s3Transport] PLAN_ARTIFACT_DIGEST_MISMATCH: ${uri} expected ${expectedSha256} (${expectedSize} bytes), S3 holds checksum ${String(head.ChecksumSHA256)} (${String(head.ContentLength)} bytes)`,
|
||||
);
|
||||
error.name = "PLAN_ARTIFACT_DIGEST_MISMATCH";
|
||||
throw error;
|
||||
}
|
||||
return getSignedUrl(client, new GetObjectCommand({ Bucket: bucket, Key: key }), {
|
||||
expiresIn: expiresInSeconds,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a local file's contents to an S3 URI using a streaming
|
||||
* `PutObjectCommand`. PutObject's 5 GB cap comfortably exceeds the
|
||||
|
||||
@@ -297,7 +297,11 @@ export {
|
||||
quantizeTimeToFrame,
|
||||
type MediaVisualStyleProperty,
|
||||
} from "./inline-scripts/parityContract";
|
||||
export { redactKnownPaths, redactTelemetryString } from "./telemetryRedaction";
|
||||
export {
|
||||
redactKnownPaths,
|
||||
redactTelemetryString,
|
||||
redactUrlQueryStrings,
|
||||
} from "./telemetryRedaction";
|
||||
export {
|
||||
folderGone,
|
||||
isProjectRootMissing,
|
||||
|
||||
@@ -5,7 +5,7 @@ function truncateTelemetryString(value: string, maxLength: number): string {
|
||||
return `${value.slice(0, maxLength - 1)}…`;
|
||||
}
|
||||
|
||||
function redactUrlQueryStrings(value: string): string {
|
||||
export function redactUrlQueryStrings(value: string): string {
|
||||
return value.replace(/\b(https?:\/\/[^\s?]+)\?[^\s]*/g, "$1?…");
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ import {
|
||||
type VideoMetadata,
|
||||
} from "../utils/ffprobe.js";
|
||||
import { inputAlphaOpaqueWarning, probeInputAlphaPlane } from "../utils/alphaPlaneProbe.js";
|
||||
import { redactUrlQueriesInError, urlInputArgs } from "../utils/urlInput.js";
|
||||
import {
|
||||
analyzeCompositionHdr,
|
||||
isHdrColorSpace as isHdrColorSpaceUtil,
|
||||
@@ -816,7 +817,9 @@ export function parseImageElements(html: string): ImageElement[] {
|
||||
|
||||
/** The first shown frame's colour tags; see {@link firstFrameColourArgs}. */
|
||||
async function readFirstFrameColour(videoPath: string, signal?: AbortSignal) {
|
||||
const result = await runFfmpeg(firstFrameColourArgs(videoPath), { signal });
|
||||
const result = await runFfmpeg([...urlInputArgs(videoPath), ...firstFrameColourArgs(videoPath)], {
|
||||
signal,
|
||||
});
|
||||
return parseFirstFrameColour(result.stderr);
|
||||
}
|
||||
|
||||
@@ -928,6 +931,16 @@ async function runSegmentedExtraction(job: SegmentedExtraction): Promise<RunFfmp
|
||||
}
|
||||
|
||||
export async function extractVideoFramesRange(
|
||||
...args: Parameters<typeof extractVideoFramesRangeUnredacted>
|
||||
): Promise<ExtractedFrames> {
|
||||
try {
|
||||
return await extractVideoFramesRangeUnredacted(...args);
|
||||
} catch (error) {
|
||||
throw redactUrlQueriesInError(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function extractVideoFramesRangeUnredacted(
|
||||
videoPath: string,
|
||||
videoId: string,
|
||||
startTime: number,
|
||||
@@ -987,7 +1000,7 @@ export async function extractVideoFramesRange(
|
||||
throw new Error("frameRanges extraction needs a constant-frame-rate source");
|
||||
}
|
||||
|
||||
const args: string[] = [];
|
||||
const args: string[] = urlInputArgs(videoPath);
|
||||
if (decodeWithVideoToolbox) {
|
||||
args.push("-hwaccel", "videotoolbox");
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { chunkCrc32 } from "./crc32.js";
|
||||
import { StringDecoder } from "node:string_decoder";
|
||||
import { basename } from "path";
|
||||
import { redactTelemetryString } from "@hyperframes/core";
|
||||
import { urlInputArgs } from "./urlInput.js";
|
||||
import { FFPROBE_PATH_ENV, getFfprobeBinary } from "./ffmpegBinaries.js";
|
||||
import { ManagedChildProcess } from "./managedChildProcess.js";
|
||||
import { trackChildProcess } from "./processTracker.js";
|
||||
@@ -65,7 +66,15 @@ async function runFfprobe(
|
||||
}
|
||||
|
||||
const command = getFfprobeBinary();
|
||||
const proc = spawn(command, ["-v", "error", ...argsWithoutInput, "--", filePath], {
|
||||
const args = [
|
||||
"-v",
|
||||
"error",
|
||||
...urlInputArgs(filePath, { tool: "ffprobe" }),
|
||||
...argsWithoutInput,
|
||||
"--",
|
||||
filePath,
|
||||
];
|
||||
const proc = spawn(command, args, {
|
||||
// Nothing is ever written to the child's stdin; leaving it as a pipe is
|
||||
// what lets a stdin-reading invocation block indefinitely.
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { VideoSourceExtractionError } from "../services/videoFrameExtractor.js";
|
||||
import { redactUrlQueriesInError, urlInputArgs } from "./urlInput.js";
|
||||
|
||||
const signed =
|
||||
"https://bucket.s3.us-east-2.amazonaws.com/a/b?X-Amz-Credential=AKID&X-Amz-Signature=deadbeef";
|
||||
|
||||
describe("urlInputArgs", () => {
|
||||
const caBundles = ["/nonexistent.crt", import.meta.filename];
|
||||
const verifyAndReconnect = [
|
||||
"-tls_verify",
|
||||
"1",
|
||||
"-ca_file",
|
||||
import.meta.filename,
|
||||
"-rw_timeout",
|
||||
"30000000",
|
||||
"-reconnect",
|
||||
"1",
|
||||
"-reconnect_delay_max",
|
||||
"2",
|
||||
];
|
||||
|
||||
it("makes ffmpeg verify the certificate of an https input and reconnect on network errors", () => {
|
||||
expect(urlInputArgs(signed, { caBundles })).toEqual([
|
||||
...verifyAndReconnect,
|
||||
"-reconnect_on_network_error",
|
||||
"1",
|
||||
]);
|
||||
});
|
||||
|
||||
it("gives ffprobe only the options its 4.0 build accepts", () => {
|
||||
expect(urlInputArgs(signed, { tool: "ffprobe", caBundles })).toEqual(verifyAndReconnect);
|
||||
});
|
||||
|
||||
it("adds nothing for local paths and plain http", () => {
|
||||
expect(urlInputArgs("/plan/compiled/bg.mp4", { caBundles: [] })).toEqual([]);
|
||||
expect(urlInputArgs("http://127.0.0.1:8080/bg.mp4", { caBundles: [] })).toEqual([]);
|
||||
});
|
||||
|
||||
it("refuses an https input when no CA bundle exists", () => {
|
||||
expect(() => urlInputArgs(signed, { caBundles: ["/nonexistent.crt"] })).toThrow(/no CA bundle/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("redactUrlQueriesInError", () => {
|
||||
it("strips a signed URL's query from the message, diagnostic and stack", () => {
|
||||
const error = redactUrlQueriesInError(
|
||||
new VideoSourceExtractionError("ffmpeg_failed", false, `read ${signed}`, `${signed}: 403`),
|
||||
);
|
||||
for (const text of [error.message, error.diagnostic, error.stack]) {
|
||||
expect(text).toContain("https://bucket.s3.us-east-2.amazonaws.com/a/b?");
|
||||
expect(text).not.toContain("deadbeef");
|
||||
}
|
||||
});
|
||||
|
||||
it("also strips it from a wrapped cause", () => {
|
||||
const error = redactUrlQueriesInError(
|
||||
new Error("render failed", { cause: new Error(`read ${signed}`) }),
|
||||
);
|
||||
expect(String((error.cause as Error).message)).not.toContain("deadbeef");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { redactUrlQueryStrings } from "@hyperframes/core";
|
||||
|
||||
// The Lambda nodejs image (Amazon Linux 2023) ships the first; Debian/Ubuntu the second; macOS the third.
|
||||
const CA_BUNDLES = [
|
||||
"/etc/pki/tls/certs/ca-bundle.crt",
|
||||
"/etc/ssl/certs/ca-certificates.crt",
|
||||
"/etc/ssl/cert.pem",
|
||||
];
|
||||
|
||||
// Lambda's ffprobe 4.0 accepts these but not -reconnect_on_network_error; a stalled read fails after 30 s.
|
||||
const RECONNECT_ARGS = ["-rw_timeout", "30000000", "-reconnect", "1", "-reconnect_delay_max", "2"];
|
||||
|
||||
/** ffmpeg and ffprobe skip TLS certificate checks unless told to; an https input must be verified. */
|
||||
export function urlInputArgs(
|
||||
input: string,
|
||||
{
|
||||
tool = "ffmpeg",
|
||||
caBundles = CA_BUNDLES,
|
||||
}: { tool?: "ffmpeg" | "ffprobe"; caBundles?: readonly string[] } = {},
|
||||
): string[] {
|
||||
if (!/^https:\/\//i.test(input)) return [];
|
||||
const caFile = caBundles.find((path) => existsSync(path));
|
||||
if (!caFile) {
|
||||
throw new Error(
|
||||
`[FFmpeg] cannot verify TLS for an https input: no CA bundle at ${caBundles.join(", ")}`,
|
||||
);
|
||||
}
|
||||
const networkRetry = tool === "ffmpeg" ? ["-reconnect_on_network_error", "1"] : [];
|
||||
return ["-tls_verify", "1", "-ca_file", caFile, ...RECONNECT_ARGS, ...networkRetry];
|
||||
}
|
||||
|
||||
/** A signed URL grants access until it expires: strip query strings from every string field of an error and its causes. */
|
||||
export function redactUrlQueriesInError<T>(error: T): T {
|
||||
if (!(error instanceof Error)) return error;
|
||||
const fields = error as unknown as Record<string, unknown>;
|
||||
for (const key of ["message", "stack", ...Object.keys(error)]) {
|
||||
if (typeof fields[key] === "string") fields[key] = redactUrlQueryStrings(fields[key]);
|
||||
}
|
||||
if (error.cause !== error) redactUrlQueriesInError(error.cause);
|
||||
return error;
|
||||
}
|
||||
@@ -60,6 +60,7 @@ export {
|
||||
createPlanV2FromExecutionPlan,
|
||||
createPlanV2FromV1,
|
||||
getPlanV2ExecutionPlanHash,
|
||||
isPlanV2DeferredSourceArtifact,
|
||||
listPlanV2ArtifactsForTarget,
|
||||
materializePlanV2Target,
|
||||
planV2,
|
||||
@@ -74,8 +75,10 @@ export {
|
||||
type PlanV2Artifact,
|
||||
type PlanV2Limitations,
|
||||
type PlanV2Manifest,
|
||||
type PlanV2MaterializeOptions,
|
||||
type PlanV2MaterializationResult,
|
||||
type PlanV2MaterializationTarget,
|
||||
type PlanV2RemoteSources,
|
||||
type PlanV2Result,
|
||||
type PlanV2WithPublisherOptions,
|
||||
} from "./services/distributed/planV2.js";
|
||||
|
||||
@@ -27,8 +27,10 @@ import {
|
||||
publishPlanV2FromV1,
|
||||
publishPlanV2FromExecutionPlan,
|
||||
readPlanV2Manifest,
|
||||
readValidatedPlanV2Materialization,
|
||||
validatePlanV2MaterializedTarget,
|
||||
} from "./planV2.js";
|
||||
import { planV2BlobPath } from "./planV2Layout.js";
|
||||
import { LocalPlanV2ArtifactPublisher, type PlanV2ArtifactPublisher } from "./planV2Publisher.js";
|
||||
import { buildPlanVideosJson, type PlanVideosJson } from "./shared.js";
|
||||
|
||||
@@ -450,20 +452,60 @@ describe("Plan v2 manifest", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps a deferred source the page itself loads in every chunk", () => {
|
||||
it("ships each compiled/ video source only to the chunks that show it", () => {
|
||||
const root = tempPath("hf-plan-v2-deferred-compiled-source-");
|
||||
const v1 = createV1Plan(root, {
|
||||
video: true,
|
||||
videoStart: 1 / 30,
|
||||
deferredSource: "compiled/hero.mp4",
|
||||
});
|
||||
// A second clip shown in both chunks, also shipped from compiled/.
|
||||
const videosPath = join(v1, "meta", "videos.json");
|
||||
const videos = JSON.parse(readFileSync(videosPath, "utf-8")) as PlanVideosJson;
|
||||
const hero = videos.extracted[0]!;
|
||||
videos.videos.push({ ...videos.videos[0]!, id: "long", src: "long.mp4", start: 0 });
|
||||
videos.extracted.push({
|
||||
...hero,
|
||||
videoId: "long",
|
||||
deferredRange: { ...hero.deferredRange!, sourcePath: "compiled/long.mp4" },
|
||||
});
|
||||
writeFileSync(videosPath, JSON.stringify(videos));
|
||||
writeFileSync(join(v1, "compiled", "long.mp4"), "long source video");
|
||||
refreshV1PlanHash(v1);
|
||||
const manifest = readPlanV2Manifest(createPlanV2FromV1(v1, join(root, "v2")).planDir);
|
||||
const chunksOf = (path: string) =>
|
||||
manifest.artifacts.find((artifact) => artifact.path === path)?.chunks;
|
||||
|
||||
expect(manifest.limitations.videoDependencyMode).toBe("source-extract");
|
||||
for (const chunkIndex of [0, 1]) {
|
||||
const artifacts = listPlanV2ArtifactsForTarget(manifest, { role: "chunk", chunkIndex });
|
||||
expect(artifacts.some((artifact) => artifact.path === "compiled/hero.mp4")).toBe(true);
|
||||
}
|
||||
expect(chunksOf("compiled/hero.mp4")).toEqual([1]);
|
||||
expect(chunksOf("compiled/long.mp4")).toEqual([0, 1]);
|
||||
expect(chunksOf("compiled/index.html")).toBe("all");
|
||||
expect(chunksOf("compiled/asset.txt")).toBe("all");
|
||||
|
||||
const chunk0Dir = join(root, "chunk-0");
|
||||
materializePlanV2Target(join(root, "v2"), { role: "chunk", chunkIndex: 0 }, chunk0Dir);
|
||||
expect(existsSync(join(chunk0Dir, "compiled", "hero.mp4"))).toBe(false);
|
||||
expect(existsSync(join(chunk0Dir, "compiled", "long.mp4"))).toBe(true);
|
||||
expect(validatePlanV2MaterializedTarget(chunk0Dir, { role: "chunk", chunkIndex: 0 })).not.toBe(
|
||||
null,
|
||||
);
|
||||
|
||||
// A source read by URL is neither verified locally nor copied, and is recorded for the chunk.
|
||||
const longDigest = manifest.artifacts.find((a) => a.path === "compiled/long.mp4")!.sha256;
|
||||
rmSync(planV2BlobPath(join(root, "v2"), longDigest));
|
||||
const remoteSources = { "compiled/long.mp4": "https://example.test/long.mp4" };
|
||||
const byUrlDir = join(root, "chunk-0-by-url");
|
||||
const chunk0 = { role: "chunk", chunkIndex: 0 } as const;
|
||||
materializePlanV2Target(join(root, "v2"), chunk0, byUrlDir, { remoteSources });
|
||||
expect(existsSync(join(byUrlDir, "compiled", "long.mp4"))).toBe(false);
|
||||
expect(readValidatedPlanV2Materialization(byUrlDir, chunk0)?.remoteSources).toEqual(
|
||||
remoteSources,
|
||||
);
|
||||
expect(() =>
|
||||
materializePlanV2Target(join(root, "v2"), chunk0, join(root, "chunk-0-stray"), {
|
||||
remoteSources: { "compiled/index.html": "https://example.test/index.html" },
|
||||
}),
|
||||
).toThrow(/not a selected deferred video/);
|
||||
});
|
||||
|
||||
it("rejects a deferred video source outside the plan directory", () => {
|
||||
|
||||
@@ -161,6 +161,13 @@ export interface PlanV2MaterializationResult {
|
||||
readonly audioPath: string | null;
|
||||
}
|
||||
|
||||
export type PlanV2RemoteSources = Readonly<Record<string, string>>;
|
||||
|
||||
export interface PlanV2MaterializeOptions {
|
||||
/** Deferred video sources the chunk reads by URL; the caller vouches for their bytes. */
|
||||
readonly remoteSources?: PlanV2RemoteSources;
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
@@ -275,8 +282,18 @@ function artifactTargets(
|
||||
if (path === "plan.json" || path === "meta/chunks.json" || path === "meta/encoder.json") {
|
||||
return { chunks: "all", assembler: true };
|
||||
}
|
||||
// Each chunk's page loads every <video> src at init (a missing one fails the
|
||||
// chunk), so deferred sources under compiled/ still go to every chunk.
|
||||
// A compiled/ file in the map is a deferred video source. Chunks inject its
|
||||
// frames and skip its <video> readiness, so only chunks that show it get it.
|
||||
if (
|
||||
path.startsWith("video-frames/") ||
|
||||
path.startsWith("video-sources/") ||
|
||||
(path.startsWith("compiled/") && videoDependencies?.has(path))
|
||||
) {
|
||||
return {
|
||||
chunks: videoDependencies === null ? "all" : (videoDependencies.get(path) ?? []),
|
||||
assembler: false,
|
||||
};
|
||||
}
|
||||
if (
|
||||
path === "meta/composition.json" ||
|
||||
path === "meta/videos.json" ||
|
||||
@@ -284,17 +301,16 @@ function artifactTargets(
|
||||
) {
|
||||
return { chunks: "all", assembler: false };
|
||||
}
|
||||
if (path.startsWith("video-frames/") || path.startsWith("video-sources/")) {
|
||||
return {
|
||||
chunks: videoDependencies === null ? "all" : (videoDependencies.get(path) ?? []),
|
||||
assembler: false,
|
||||
};
|
||||
}
|
||||
// Unknown future execution files go to both roles. Over-including is safe;
|
||||
// silently omitting a new execution dependency is not.
|
||||
return { chunks: "all", assembler: true };
|
||||
}
|
||||
|
||||
/** A chunk-scoped compiled/ file is a deferred video source (see `artifactTargets`). */
|
||||
export function isPlanV2DeferredSourceArtifact(artifact: Readonly<PlanV2Artifact>): boolean {
|
||||
return artifact.path.startsWith("compiled/") && artifact.chunks !== "all";
|
||||
}
|
||||
|
||||
function listVideoFramePaths(executionPlanDir: string, videos: PlanVideosJson): ExtractedFrames[] {
|
||||
return videos.extracted.map((video) => {
|
||||
const outputDir = resolveExtractedVideoOutputDir(executionPlanDir, video.videoId);
|
||||
@@ -948,17 +964,22 @@ export function materializePlanV2Target(
|
||||
planV2Dir: string,
|
||||
target: PlanV2MaterializationTarget,
|
||||
destinationDir: string,
|
||||
options: Readonly<PlanV2MaterializeOptions> = {},
|
||||
): PlanV2MaterializationResult {
|
||||
if (existsSync(destinationDir)) {
|
||||
throw new PlanV2IntegrityError(`materialization destination already exists: ${destinationDir}`);
|
||||
}
|
||||
const manifest = readPlanV2Manifest(planV2Dir);
|
||||
const artifacts = listPlanV2ArtifactsForTarget(manifest, target);
|
||||
const remoteSources = options.remoteSources ?? {};
|
||||
assertRemoteSources(remoteSources, artifacts);
|
||||
const audioArtifact = artifacts.find((artifact) => isPlanAudioArtifactPath(artifact.path));
|
||||
const verified = artifacts.map((artifact) => ({
|
||||
artifact,
|
||||
sourcePath: verifyBlob(planV2Dir, artifact),
|
||||
}));
|
||||
const verified = artifacts
|
||||
.filter((artifact) => !Object.hasOwn(remoteSources, artifact.path))
|
||||
.map((artifact) => ({
|
||||
artifact,
|
||||
sourcePath: verifyBlob(planV2Dir, artifact),
|
||||
}));
|
||||
mkdirSync(dirname(destinationDir), { recursive: true });
|
||||
const tempDir = mkdtempSync(join(dirname(destinationDir), ".plan-v2-materialize-"));
|
||||
try {
|
||||
@@ -975,7 +996,11 @@ export function materializePlanV2Target(
|
||||
if (target.role === "chunk") materializeExtractedVideoDirectories(tempDir);
|
||||
writeFileSync(
|
||||
join(tempDir, PLAN_V2_MATERIALIZATION_MARKER),
|
||||
canonicalJsonStringify({ manifest, target }),
|
||||
canonicalJsonStringify({
|
||||
manifest,
|
||||
target,
|
||||
...(Object.keys(remoteSources).length > 0 ? { remoteSources } : {}),
|
||||
}),
|
||||
"utf-8",
|
||||
);
|
||||
renameSync(tempDir, destinationDir);
|
||||
@@ -1002,11 +1027,34 @@ export function materializePlanV2Target(
|
||||
};
|
||||
}
|
||||
|
||||
function assertRemoteSources(
|
||||
remoteSources: unknown,
|
||||
artifacts: readonly Readonly<PlanV2Artifact>[],
|
||||
): asserts remoteSources is PlanV2RemoteSources {
|
||||
if (!isRecord(remoteSources)) throw new PlanV2IntegrityError("remoteSources must be an object");
|
||||
for (const [path, url] of Object.entries(remoteSources)) {
|
||||
const artifact = artifacts.find((candidate) => candidate.path === path);
|
||||
if (!artifact || !isPlanV2DeferredSourceArtifact(artifact)) {
|
||||
throw new PlanV2IntegrityError(`remote source is not a selected deferred video: ${path}`);
|
||||
}
|
||||
if (typeof url !== "string" || !/^https?:\/\//.test(url)) {
|
||||
throw new PlanV2IntegrityError(`remote source for ${path} must be an http(s) URL`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Revalidate a materialized subset immediately before execution. */
|
||||
export function validatePlanV2MaterializedTarget(
|
||||
planDir: string,
|
||||
expectedTarget: PlanV2MaterializationTarget,
|
||||
): Readonly<PlanV2Manifest> | null {
|
||||
return readValidatedPlanV2Materialization(planDir, expectedTarget)?.manifest ?? null;
|
||||
}
|
||||
|
||||
export function readValidatedPlanV2Materialization(
|
||||
planDir: string,
|
||||
expectedTarget: PlanV2MaterializationTarget,
|
||||
): { manifest: Readonly<PlanV2Manifest>; remoteSources: PlanV2RemoteSources } | null {
|
||||
const markerPath = join(planDir, PLAN_V2_MATERIALIZATION_MARKER);
|
||||
if (!existsSync(markerPath)) return null;
|
||||
const marker = readJsonFile(markerPath, "materialization marker");
|
||||
@@ -1024,7 +1072,10 @@ export function validatePlanV2MaterializedTarget(
|
||||
}
|
||||
const manifest = parsePlanV2Manifest(marker.manifest);
|
||||
const required = listPlanV2ArtifactsForTarget(manifest, expectedTarget);
|
||||
const remoteSources = marker.remoteSources ?? {};
|
||||
assertRemoteSources(remoteSources, required);
|
||||
for (const artifact of required) {
|
||||
if (Object.hasOwn(remoteSources, artifact.path)) continue;
|
||||
const path = join(planDir, ...artifact.path.split("/"));
|
||||
if (!existsSync(path) || statSync(path).size !== artifact.sizeBytes) {
|
||||
throw new PlanV2IntegrityError(
|
||||
@@ -1035,5 +1086,5 @@ export function validatePlanV2MaterializedTarget(
|
||||
throw new PlanV2IntegrityError(`materialized artifact hash mismatch: ${artifact.path}`);
|
||||
}
|
||||
}
|
||||
return manifest;
|
||||
return { manifest, remoteSources };
|
||||
}
|
||||
|
||||
@@ -20,13 +20,29 @@
|
||||
|
||||
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import {
|
||||
copyFileSync,
|
||||
cpSync,
|
||||
createReadStream,
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
readdirSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { createServer } from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { type CaptureOptions, extractVideoFramesRange } from "@hyperframes/engine";
|
||||
import { HOST_CHROME_FAILURE_PATTERNS } from "./__test_utils__/hostChromeFailures.js";
|
||||
import { defaultLogger } from "../../logger.js";
|
||||
import { RenderQualityError } from "../renderOrchestrator.js";
|
||||
import { plan } from "./plan.js";
|
||||
import { materializePlanV2Target, planV2, readPlanV2Manifest } from "./planV2.js";
|
||||
import {
|
||||
CHUNK_INDEX_OUT_OF_RANGE,
|
||||
distributedCaptureSessionDependencies,
|
||||
@@ -53,6 +69,30 @@ const FIXTURE_HTML = `<!doctype html>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
/** Serves one file with Range support and counts the bytes it sent. */
|
||||
async function serveFileByRange(path: string) {
|
||||
const size = statSync(path).size;
|
||||
const served = { bytes: 0, requests: 0 };
|
||||
const server = createServer((req, res) => {
|
||||
served.requests++;
|
||||
const match = /bytes=(\d+)-(\d*)/.exec(req.headers.range ?? "");
|
||||
const start = match ? Number(match[1]) : 0;
|
||||
const end = match?.[2] ? Number(match[2]) : size - 1;
|
||||
res.writeHead(match ? 206 : 200, {
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Length": end - start + 1,
|
||||
...(match ? { "Content-Range": `bytes ${start}-${end}/${size}` } : {}),
|
||||
});
|
||||
const body = createReadStream(path, { start, end, highWaterMark: 16 * 1024 });
|
||||
body.on("data", (chunk) => (served.bytes += chunk.length));
|
||||
body.pipe(res);
|
||||
res.on("close", () => body.destroy());
|
||||
});
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const url = `http://127.0.0.1:${(server.address() as AddressInfo).port}/video.mp4`;
|
||||
return { url, size, served, close: () => server.close() };
|
||||
}
|
||||
|
||||
let runRoot: string;
|
||||
let projectDir: string;
|
||||
let planDir: string;
|
||||
@@ -530,6 +570,113 @@ describe("renderChunk() — script failure", () => {
|
||||
}, 60_000);
|
||||
});
|
||||
|
||||
describe("renderChunk() — a video the chunk does not show", () => {
|
||||
it("renders the same without the video's file, with no media warning", async () => {
|
||||
if (!hasChrome) {
|
||||
console.warn(
|
||||
"[renderChunk.test] skipping unshown-video test — chrome-headless-shell not available on this host",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const videoDir = join(runRoot, "project-two-videos");
|
||||
mkdirSync(videoDir, { recursive: true });
|
||||
for (const [name, seconds] of [
|
||||
["short.mp4", 2],
|
||||
["long.mp4", 3],
|
||||
] as const) {
|
||||
const synth = spawnSync("ffmpeg", [
|
||||
"-v",
|
||||
"error",
|
||||
"-y",
|
||||
"-f",
|
||||
"lavfi",
|
||||
"-i",
|
||||
`testsrc2=s=64x36:r=30:d=${seconds}`,
|
||||
"-c:v",
|
||||
"libx264",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
join(videoDir, name),
|
||||
]);
|
||||
if (synth.status !== 0) throw new Error(synth.stderr.toString());
|
||||
}
|
||||
writeFileSync(
|
||||
join(videoDir, "index.html"),
|
||||
// Clips over a second long, so the planner defers them to the chunks.
|
||||
FIXTURE_HTML.replace('data-duration="0.16667"', 'data-duration="2.4"').replace(
|
||||
"</div>",
|
||||
'<video id="short" src="short.mp4" muted data-start="0" data-duration="1.2" style="position:absolute;left:0;top:0;width:64px"></video>' +
|
||||
'<video id="long" src="long.mp4" muted data-start="0" data-duration="2.4" style="position:absolute;right:0;bottom:0;width:64px"></video></div>',
|
||||
),
|
||||
"utf-8",
|
||||
);
|
||||
const v2Dir = join(runRoot, "plan-two-videos");
|
||||
await planV2(
|
||||
videoDir,
|
||||
{ fps: 30, width: 160, height: 120, format: "png-sequence", chunkSize: 36 },
|
||||
v2Dir,
|
||||
);
|
||||
const manifest = readPlanV2Manifest(v2Dir);
|
||||
expect(manifest.artifacts.find((a) => a.path === "compiled/short.mp4")?.chunks).toEqual([0]);
|
||||
|
||||
const without = join(runRoot, "chunk1-without-short");
|
||||
const withShort = join(runRoot, "chunk1-with-short");
|
||||
materializePlanV2Target(v2Dir, { role: "chunk", chunkIndex: 1 }, without);
|
||||
cpSync(without, withShort, { recursive: true });
|
||||
copyFileSync(join(videoDir, "short.mp4"), join(withShort, "compiled", "short.mp4"));
|
||||
|
||||
const warnings: unknown[] = [];
|
||||
const skipped: (readonly string[] | undefined)[] = [];
|
||||
const originalWarn = defaultLogger.warn;
|
||||
const originalCreate = distributedCaptureSessionDependencies.createCaptureSession;
|
||||
defaultLogger.warn = (message, meta) => {
|
||||
warnings.push(meta);
|
||||
originalWarn(message, meta);
|
||||
};
|
||||
distributedCaptureSessionDependencies.createCaptureSession = ((...args) => {
|
||||
skipped.push(args[2].skipReadinessVideoIds);
|
||||
return originalCreate(...args);
|
||||
}) as typeof originalCreate;
|
||||
let a, b;
|
||||
try {
|
||||
a = await renderChunk(without, 1, join(runRoot, "chunk1-out-without"));
|
||||
b = await renderChunk(withShort, 1, join(runRoot, "chunk1-out-with"));
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
if (!HOST_CHROME_FAILURE_PATTERNS.test(message)) throw err;
|
||||
console.warn(
|
||||
"[renderChunk.test] skipping unshown-video test — host Chrome stack can't render. Diagnostic:",
|
||||
message.slice(0, 240),
|
||||
);
|
||||
return;
|
||||
} finally {
|
||||
defaultLogger.warn = originalWarn;
|
||||
distributedCaptureSessionDependencies.createCaptureSession = originalCreate;
|
||||
}
|
||||
|
||||
expect(JSON.stringify(warnings)).not.toContain("media_load_failed");
|
||||
// Only the unshown video is skipped; the shown one keeps today's readiness wait.
|
||||
expect(new Set(skipped.map(String))).toEqual(new Set(["short"]));
|
||||
expect(a.sha256).toBe(b.sha256);
|
||||
|
||||
// The shown video read by URL instead of from the plan renders the same chunk.
|
||||
const server = await serveFileByRange(join(videoDir, "long.mp4"));
|
||||
const viaUrl = join(runRoot, "chunk1-long-by-url");
|
||||
try {
|
||||
materializePlanV2Target(v2Dir, { role: "chunk", chunkIndex: 1 }, viaUrl, {
|
||||
remoteSources: { "compiled/long.mp4": server.url },
|
||||
});
|
||||
expect(existsSync(join(viaUrl, "compiled", "long.mp4"))).toBe(false);
|
||||
const byUrl = await renderChunk(viaUrl, 1, join(runRoot, "chunk1-out-by-url"));
|
||||
expect(server.served.requests).toBeGreaterThan(0);
|
||||
expect(byUrl.sha256).toBe(a.sha256);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
}, 120_000);
|
||||
});
|
||||
|
||||
describe("resolvePresetForLockedEncoder", () => {
|
||||
// Tiny fast tests for the codec-override helper. No Chrome, no ffmpeg —
|
||||
// exists so a refactor that moves the override (e.g. into
|
||||
@@ -669,6 +816,100 @@ describe("extractDeferredVideoFramesForChunk", () => {
|
||||
}
|
||||
});
|
||||
|
||||
// Lossless keeps the file large next to what one seek reads.
|
||||
const big = join(root, "big.mp4");
|
||||
const synthBig = () => {
|
||||
if (existsSync(big)) return;
|
||||
const synth = spawnSync("ffmpeg", [
|
||||
...["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=640x360:r=30:d=120"],
|
||||
...["-c:v", "libx264", "-preset", "ultrafast", "-qp", "0", "-g", "30", big],
|
||||
]);
|
||||
if (synth.status !== 0) throw new Error(synth.stderr.toString());
|
||||
};
|
||||
const bigVideos = () => {
|
||||
const videos = planVideos(false);
|
||||
const bigRange = {
|
||||
...range,
|
||||
sourcePath: "compiled/big.mp4",
|
||||
startTime: 0,
|
||||
durationSeconds: 120,
|
||||
};
|
||||
videos.videos[0] = { ...videos.videos[0]!, end: 120, mediaStart: 0 };
|
||||
videos.extracted[0] = { ...videos.extracted[0]!, totalFrames: 3600, deferredRange: bigRange };
|
||||
return videos;
|
||||
};
|
||||
const extract = (outputRoot: string, remoteSources?: Record<string, string>) =>
|
||||
extractDeferredVideoFramesForChunk({
|
||||
planDir: root,
|
||||
planVideos: bigVideos(),
|
||||
slice: { index: 5, startFrame: 1800, endFrame: 1815 },
|
||||
fps: { num: 30, den: 1 },
|
||||
outputRoot,
|
||||
cfg: {},
|
||||
remoteSources,
|
||||
});
|
||||
|
||||
it("reads a source by URL in byte ranges and extracts the same frames", async () => {
|
||||
synthBig();
|
||||
mkdirSync(join(root, "compiled"), { recursive: true });
|
||||
copyFileSync(big, join(root, "compiled", "big.mp4"));
|
||||
const server = await serveFileByRange(big);
|
||||
try {
|
||||
const [local] = await extract(join(root, "big-local"));
|
||||
rmSync(join(root, "compiled", "big.mp4"));
|
||||
const [remote] = await extract(join(root, "big-remote"), { "compiled/big.mp4": server.url });
|
||||
expect([...remote!.framePaths.keys()]).toEqual([...local!.framePaths.keys()]);
|
||||
for (const [index, path] of remote!.framePaths) {
|
||||
expect(readFileSync(path).equals(readFileSync(local!.framePaths.get(index)!))).toBe(true);
|
||||
}
|
||||
// What it sends past the seek is socket buffering (a few MB), not the file.
|
||||
expect(server.served.bytes).toBeLessThan(server.size / 3);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
}, 60_000);
|
||||
|
||||
it("never surfaces a signed URL's query when ffmpeg fails", async () => {
|
||||
synthBig();
|
||||
// ffmpeg echoes its arguments, the signed URL among them, the way a real read failure does.
|
||||
const failingFfmpeg = join(root, "failing-ffmpeg");
|
||||
writeFileSync(failingFfmpeg, '#!/bin/sh\necho "cannot read: $*" >&2\nexit 1\n', {
|
||||
mode: 0o755,
|
||||
});
|
||||
const server = await serveFileByRange(big);
|
||||
const originalFfmpeg = process.env.HYPERFRAMES_FFMPEG_PATH;
|
||||
process.env.HYPERFRAMES_FFMPEG_PATH = failingFfmpeg;
|
||||
try {
|
||||
const url = `${server.url}?X-Amz-Signature=supersecret`;
|
||||
const error = await extract(join(root, "big-failed"), { "compiled/big.mp4": url }).catch(
|
||||
(caught: unknown) => caught,
|
||||
);
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
const { message, stack } = error as Error;
|
||||
const text = JSON.stringify({ ...(error as object), message, stack });
|
||||
expect(text).toContain("/video.mp4?…");
|
||||
expect(text).not.toContain("supersecret");
|
||||
} finally {
|
||||
if (originalFfmpeg === undefined) delete process.env.HYPERFRAMES_FFMPEG_PATH;
|
||||
else process.env.HYPERFRAMES_FFMPEG_PATH = originalFfmpeg;
|
||||
server.close();
|
||||
}
|
||||
}, 60_000);
|
||||
|
||||
it("refuses a URL for a file no deferred video reads", async () => {
|
||||
await expect(
|
||||
extractDeferredVideoFramesForChunk({
|
||||
planDir,
|
||||
planVideos: planVideos(false),
|
||||
slice: { index: 0, startFrame: 0, endFrame: 5 },
|
||||
fps: { num: 30, den: 1 },
|
||||
outputRoot: join(root, "chunk-stray-url"),
|
||||
cfg: {},
|
||||
remoteSources: { "compiled/other.mp4": "http://127.0.0.1:1/other.mp4" },
|
||||
}),
|
||||
).rejects.toMatchObject({ code: MISSING_PLAN_ARTIFACT });
|
||||
});
|
||||
|
||||
it("extracts nothing for a chunk past the clip, and holds no frames", async () => {
|
||||
const [chunk] = await extractDeferredVideoFramesForChunk({
|
||||
planDir,
|
||||
|
||||
@@ -65,7 +65,7 @@ import {
|
||||
resolveConfig,
|
||||
} from "@hyperframes/engine";
|
||||
import { defaultLogger } from "../../logger.js";
|
||||
import { applyRenderWarningPolicy } from "../renderOrchestrator.js";
|
||||
import { applyRenderWarningPolicy, collectVideoMetadataHints } from "../renderOrchestrator.js";
|
||||
import { runEncodeStage } from "../render/stages/encodeStage.js";
|
||||
import { runCaptureStage } from "../render/stages/captureStage.js";
|
||||
import { resolveVideoCaptureBeyondViewport } from "../render/captureBeyondViewport.js";
|
||||
@@ -95,7 +95,7 @@ import {
|
||||
readFfmpegVersion,
|
||||
} from "./shared.js";
|
||||
import { DISTRIBUTED_RENDER_CAPABILITIES, readPlanProtocolV1 } from "./planProtocol.js";
|
||||
import { validatePlanV2MaterializedTarget } from "./planV2.js";
|
||||
import { type PlanV2RemoteSources, readValidatedPlanV2Materialization } from "./planV2.js";
|
||||
|
||||
/**
|
||||
* Non-retryable error codes raised when the planDir is structurally
|
||||
@@ -435,8 +435,18 @@ export async function extractDeferredVideoFramesForChunk(input: {
|
||||
fps: { num: number; den: number };
|
||||
outputRoot: string;
|
||||
cfg: Pick<EngineConfig, "ffmpegProcessTimeout">;
|
||||
remoteSources?: PlanV2RemoteSources;
|
||||
}): Promise<ExtractedFrames[]> {
|
||||
const deferred = input.planVideos.extracted.filter((video) => video.deferredRange);
|
||||
const remoteSources = input.remoteSources ?? {};
|
||||
for (const path of Object.keys(remoteSources)) {
|
||||
if (!deferred.some((video) => video.deferredRange!.sourcePath === path)) {
|
||||
throw new RenderChunkValidationError(
|
||||
MISSING_PLAN_ARTIFACT,
|
||||
`[renderChunk] ${path} is read by URL but no deferred video uses it`,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (deferred.length === 0) return [];
|
||||
const table = createFrameLookupTable(
|
||||
input.planVideos.videos,
|
||||
@@ -464,8 +474,9 @@ export async function extractDeferredVideoFramesForChunk(input: {
|
||||
return { ...video, outputDir, framePaths: new Map<number, string>(), ownedByLookup: false };
|
||||
}
|
||||
const range = video.deferredRange!;
|
||||
const sourcePath = join(input.planDir, ...range.sourcePath.split("/"));
|
||||
if (!existsSync(sourcePath)) {
|
||||
const remoteUrl = remoteSources[range.sourcePath];
|
||||
const sourcePath = remoteUrl ?? join(input.planDir, ...range.sourcePath.split("/"));
|
||||
if (remoteUrl === undefined && !existsSync(sourcePath)) {
|
||||
throw new RenderChunkValidationError(
|
||||
MISSING_PLAN_ARTIFACT,
|
||||
`[renderChunk] planDir is missing the source for video ${JSON.stringify(video.videoId)}: ${range.sourcePath}`,
|
||||
@@ -615,10 +626,12 @@ export async function renderChunk(
|
||||
const plan = JSON.parse(readFileSync(planJsonPath, "utf-8")) as PlanJson;
|
||||
readPlanProtocolV1(plan, DISTRIBUTED_RENDER_CAPABILITIES.roles.chunk);
|
||||
const planHashStarted = Date.now();
|
||||
const v2Manifest = validatePlanV2MaterializedTarget(planDir, {
|
||||
const v2Materialization = readValidatedPlanV2Materialization(planDir, {
|
||||
role: "chunk",
|
||||
chunkIndex,
|
||||
});
|
||||
const v2Manifest = v2Materialization?.manifest ?? null;
|
||||
const remoteSources = v2Materialization?.remoteSources ?? {};
|
||||
for (const required of [encoderJsonPath, chunksJsonPath]) {
|
||||
if (!existsSync(required)) {
|
||||
throw new RenderChunkValidationError(
|
||||
@@ -774,6 +787,27 @@ export async function renderChunk(
|
||||
// its own injector hook because the hook remembers the last injected frame
|
||||
// per video; sharing that state across a fresh retry page can suppress the
|
||||
// first injection and produce a blank/stale frame.
|
||||
const deferredVideoFrames = planVideos
|
||||
? await extractDeferredVideoFramesForChunk({
|
||||
planDir,
|
||||
planVideos,
|
||||
slice,
|
||||
fps: { num: plan.dimensions.fpsNum, den: plan.dimensions.fpsDen },
|
||||
outputRoot: videoFramesDir,
|
||||
cfg,
|
||||
remoteSources,
|
||||
})
|
||||
: [];
|
||||
// The browser cannot load a source read by URL; size its <video> from the plan instead.
|
||||
const remoteVideoIds = new Set(
|
||||
planVideos?.extracted
|
||||
.filter(
|
||||
(video) =>
|
||||
video.deferredRange && Object.hasOwn(remoteSources, video.deferredRange.sourcePath),
|
||||
)
|
||||
.map((video) => video.videoId),
|
||||
);
|
||||
const remoteVideos = deferredVideoFrames.filter((video) => remoteVideoIds.has(video.videoId));
|
||||
const videoFrameLookup =
|
||||
planVideos && planVideos.extracted.length > 0
|
||||
? createFrameLookupTable(
|
||||
@@ -784,14 +818,7 @@ export async function renderChunk(
|
||||
planVideos.extracted.filter((video) => !video.deferredRange),
|
||||
v2Manifest === null ? "dense-v1" : "sparse-v2",
|
||||
),
|
||||
...(await extractDeferredVideoFramesForChunk({
|
||||
planDir,
|
||||
planVideos,
|
||||
slice,
|
||||
fps: { num: plan.dimensions.fpsNum, den: plan.dimensions.fpsDen },
|
||||
outputRoot: videoFramesDir,
|
||||
cfg,
|
||||
})),
|
||||
...deferredVideoFrames,
|
||||
],
|
||||
resolveRenderFpsConfig(job.config.fps).value,
|
||||
)
|
||||
@@ -832,6 +859,11 @@ export async function renderChunk(
|
||||
// engine skips the `evaluateOnNewDocument` injection.
|
||||
variables: encoder.variables,
|
||||
subTimelineWaitMemo: {},
|
||||
// The plan ships a deferred source only to chunks that show it; don't wait on the rest.
|
||||
skipReadinessVideoIds: deferredVideoFrames
|
||||
.filter((video) => video.framePaths.size === 0 || remoteVideoIds.has(video.videoId))
|
||||
.map((video) => video.videoId),
|
||||
videoMetadataHints: collectVideoMetadataHints(remoteVideos),
|
||||
...(videoCaptureBeyondViewport !== undefined
|
||||
? { captureBeyondViewport: videoCaptureBeyondViewport }
|
||||
: {}),
|
||||
|
||||
Reference in New Issue
Block a user