Skip to content

Commit 5a783d2

Browse files
committed
fix(producer): close runtime media preflight gaps
1 parent c0360c4 commit 5a783d2

9 files changed

Lines changed: 230 additions & 69 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,19 @@ jobs:
262262

263263
test:
264264
name: Test
265-
needs: changes
266-
if: needs.changes.outputs.code == 'true'
265+
needs: [changes, producer-source-tests]
266+
# Keep the existing required `Test` context authoritative for producer
267+
# failures too. The dedicated producer matrix remains parallel and legible,
268+
# while this job fails closed if either lane fails or is cancelled.
269+
if: always() && needs.changes.outputs.code == 'true'
267270
runs-on: ubuntu-latest
268271
timeout-minutes: 10
269272
steps:
273+
- name: Require producer source tests
274+
if: needs.producer-source-tests.result != 'success'
275+
run: |
276+
echo "::error::Producer unit/integration tests did not succeed."
277+
exit 1
270278
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
271279
with:
272280
lfs: true

‎packages/producer/src/services/assetMediaType.ts‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
type MediaProbeProfile,
99
type VideoElement,
1010
} from "@hyperframes/engine";
11+
import { withMediaProbeSlot } from "../utils/mediaProbeConcurrency.js";
1112

1213
export const ASSET_MEDIA_TYPE_MISMATCH = "ASSET_MEDIA_TYPE_MISMATCH" as const;
13-
const MEDIA_PREFLIGHT_CONCURRENCY = 4;
1414

1515
export type AssetElementMediaType = "audio" | "image" | "video";
1616
export type DetectedAssetMediaType = AssetElementMediaType | "unknown";
@@ -134,29 +134,27 @@ export async function preflightCompositionAssetMediaTypes(input: {
134134

135135
const mismatches: AssetMediaTypeMismatch[] = [];
136136
const entries = [...byPath];
137-
for (let offset = 0; offset < entries.length; offset += MEDIA_PREFLIGHT_CONCURRENCY) {
138-
await Promise.all(
139-
entries
140-
.slice(offset, offset + MEDIA_PREFLIGHT_CONCURRENCY)
141-
.map(async ([resolvedPath, pathReferences]) => {
142-
let profile: MediaProbeProfile;
143-
try {
144-
profile = await probeMediaProfile(resolvedPath, { signal: input.signal });
145-
} catch (error) {
146-
if (input.signal?.aborted) throw input.signal.reason ?? error;
147-
return;
148-
}
149-
for (const reference of pathReferences) {
150-
if (mediaProfileMatchesElementType(reference.expected, profile)) continue;
151-
mismatches.push({
152-
expected: reference.expected,
153-
detected: detectedAssetMediaType(profile),
154-
elementFingerprint: fingerprintElementId(reference.id),
155-
});
156-
}
157-
}),
158-
);
159-
}
137+
await Promise.all(
138+
entries.map(([resolvedPath, pathReferences]) =>
139+
withMediaProbeSlot(async () => {
140+
let profile: MediaProbeProfile;
141+
try {
142+
profile = await probeMediaProfile(resolvedPath, { signal: input.signal });
143+
} catch (error) {
144+
if (input.signal?.aborted) throw input.signal.reason ?? error;
145+
return;
146+
}
147+
for (const reference of pathReferences) {
148+
if (mediaProfileMatchesElementType(reference.expected, profile)) continue;
149+
mismatches.push({
150+
expected: reference.expected,
151+
detected: detectedAssetMediaType(profile),
152+
elementFingerprint: fingerprintElementId(reference.id),
153+
});
154+
}
155+
}),
156+
),
157+
);
160158

161159
if (mismatches.length > 0) throw new AssetMediaTypeMismatchError(mismatches);
162160
}

‎packages/producer/src/services/htmlCompiler.mediaType.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { join } from "node:path";
55
import { compileForRender } from "./htmlCompiler.js";
66
import { synthesizeMediaFixture } from "./mediaTypeTestFixtures.js";
77
import { Semaphore } from "../utils/semaphore.js";
8+
import { sharedMediaProbeSemaphore } from "../utils/mediaProbeConcurrency.js";
89

910
describe("compileForRender media-type ownership", () => {
1011
const projectDir = mkdtempSync(join(tmpdir(), "hf-compiler-media-type-"));
@@ -117,6 +118,13 @@ describe("compileForRender media-type ownership", () => {
117118
);
118119

119120
await compileForRender(projectDir, htmlPath, downloadDir);
121+
await vi.waitFor(
122+
() => {
123+
expect(sharedMediaProbeSemaphore.activeCount).toBe(0);
124+
expect(sharedMediaProbeSemaphore.waitingCount).toBe(0);
125+
},
126+
{ timeout: 30_000, interval: 5 },
127+
);
120128

121129
expect(limiterInstances.size).toBe(1);
122130
expect(maxActive).toBe(4);

‎packages/producer/src/services/htmlCompiler.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ describe("discoverMediaFromBrowser", () => {
6666
expect(media).toHaveLength(1);
6767
expect(media[0]).toMatchObject({ id: "hf-img-1", tagName: "image" });
6868
});
69+
70+
it("discovers the owning image for a variable-bound picture source", async () => {
71+
const media = await discover(
72+
`<picture>
73+
<source src="fallback.webp" data-var-src="hero_src" />
74+
<img id="hero" src="fallback.png" />
75+
</picture>`,
76+
{ hero: "https://cdn.example/runtime.avif" },
77+
);
78+
79+
expect(media).toHaveLength(1);
80+
expect(media[0]).toMatchObject({
81+
id: "hero",
82+
tagName: "image",
83+
src: "https://cdn.example/runtime.avif",
84+
});
85+
});
6986
});
7087

7188
describe("injectSdkPositionEditsRenderScript", () => {

‎packages/producer/src/services/htmlCompiler.ts‎

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ import { createStudioPositionSeekReapplyScript } from "@hyperframes/studio-serve
6464
import { getPositionEditsRenderScript } from "@hyperframes/core/runtime/position-edits-render";
6565
import { defaultLogger, type ProducerLogger } from "../logger.js";
6666
import { assertAssetMediaTypeProfile } from "./assetMediaType.js";
67-
import { Semaphore } from "../utils/semaphore.js";
68-
69-
const MAX_COMPILER_MEDIA_PROBES = 4;
67+
import { withMediaProbeSlot } from "../utils/mediaProbeConcurrency.js";
7068

7169
export interface CompiledComposition {
7270
html: string;
@@ -415,7 +413,6 @@ async function resolveMediaDuration(
415413
downloadDir: string,
416414
tagName: string,
417415
elementIdentity: string,
418-
mediaProbeSemaphore: Semaphore,
419416
): Promise<{ duration: number; resolvedPath: string }> {
420417
let filePath = src;
421418

@@ -436,8 +433,7 @@ async function resolveMediaDuration(
436433
return { duration: 0, resolvedPath: filePath };
437434
}
438435

439-
const releaseProbe = await mediaProbeSemaphore.acquire();
440-
try {
436+
return withMediaProbeSlot(async () => {
441437
let profile: MediaProbeProfile;
442438
try {
443439
profile = await probeMediaProfile(filePath);
@@ -469,9 +465,7 @@ async function resolveMediaDuration(
469465
const duration = effectiveDuration > 0 ? effectiveDuration : fileDuration;
470466

471467
return { duration, resolvedPath: filePath };
472-
} finally {
473-
releaseProbe();
474-
}
468+
});
475469
}
476470

477471
/**
@@ -482,7 +476,6 @@ async function compileHtmlFile(
482476
html: string,
483477
baseDir: string,
484478
downloadDir: string,
485-
mediaProbeSemaphore: Semaphore,
486479
log?: ProducerLogger,
487480
): Promise<{ html: string; unresolvedCompositions: UnresolvedElement[] }> {
488481
const { html: staticCompiled, unresolved } = compileTimingAttrs(html);
@@ -496,15 +489,9 @@ async function compileHtmlFile(
496489
// Phase 1: Resolve missing durations (parallel ffprobe)
497490
const resolvedResults = await Promise.all(
498491
mediaUnresolved.map((el) =>
499-
resolveMediaDuration(
500-
el.src!,
501-
el.mediaStart,
502-
baseDir,
503-
downloadDir,
504-
el.tagName,
505-
el.id,
506-
mediaProbeSemaphore,
507-
).then(({ duration }) => ({ id: el.id, duration })),
492+
resolveMediaDuration(el.src!, el.mediaStart, baseDir, downloadDir, el.tagName, el.id).then(
493+
({ duration }) => ({ id: el.id, duration }),
494+
),
508495
),
509496
);
510497
const resolutions: ResolvedDuration[] = resolvedResults.filter((r) => r.duration > 0);
@@ -526,7 +513,6 @@ async function compileHtmlFile(
526513
downloadDir,
527514
el.tagName,
528515
el.id,
529-
mediaProbeSemaphore,
530516
);
531517
return { id: el.id, tagName: el.tagName, duration: el.duration, maxDuration, src: el.src! };
532518
}),
@@ -588,7 +574,6 @@ async function parseSubCompositions(
588574
html: string,
589575
projectDir: string,
590576
downloadDir: string,
591-
mediaProbeSemaphore: Semaphore,
592577
parentOffset: number = 0,
593578
parentEnd: number = Infinity,
594579
visited: Set<string> = new Set(),
@@ -652,14 +637,12 @@ async function parseSubCompositions(
652637
item.rawSubHtml,
653638
dirname(item.filePath),
654639
downloadDir,
655-
mediaProbeSemaphore,
656640
);
657641

658642
const nested = await parseSubCompositions(
659643
compiledSub,
660644
projectDir,
661645
downloadDir,
662-
mediaProbeSemaphore,
663646
item.absoluteStart,
664647
item.absoluteEnd,
665648
item.nestedVisited,
@@ -1856,15 +1839,10 @@ export async function compileForRender(
18561839
// start.
18571840
assertSubCompositionsUsable(rawHtml, projectDir);
18581841

1859-
// One limiter is shared by both compiler passes and every recursive
1860-
// sub-composition so nested Promise.all calls cannot multiply ffprobe load.
1861-
const mediaProbeSemaphore = new Semaphore(MAX_COMPILER_MEDIA_PROBES);
1862-
18631842
const { html: compiledHtml, unresolvedCompositions } = await compileHtmlFile(
18641843
rawHtml,
18651844
projectDir,
18661845
downloadDir,
1867-
mediaProbeSemaphore,
18681846
options.log,
18691847
);
18701848

@@ -1874,7 +1852,7 @@ export async function compileForRender(
18741852
audios: subAudios,
18751853
images: subImages,
18761854
subCompositions,
1877-
} = await parseSubCompositions(compiledHtml, projectDir, downloadDir, mediaProbeSemaphore);
1855+
} = await parseSubCompositions(compiledHtml, projectDir, downloadDir);
18781856

18791857
// Ensure the HTML is a full document before inlining sub-compositions.
18801858
// When index.html is a fragment (no <html>/<head>/<body>), linkedom.parseHTML()
@@ -2034,7 +2012,10 @@ export async function compileForRender(
20342012
if (isHttpUrl(video.src)) continue;
20352013
const videoPath = resolve(projectDir, video.src);
20362014
const reencode = `ffmpeg -i "${video.src}" -c:v libx264 -r 30 -g 30 -keyint_min 30 -movflags +faststart -c:a copy output.mp4`;
2037-
Promise.all([analyzeKeyframeIntervals(videoPath), extractMediaMetadata(videoPath)])
2015+
Promise.all([
2016+
withMediaProbeSlot(() => analyzeKeyframeIntervals(videoPath)),
2017+
withMediaProbeSlot(() => extractMediaMetadata(videoPath)),
2018+
])
20382019
.then(([analysis, metadata]) => {
20392020
if (analysis.isProblematic) {
20402021
defaultLogger.warn(
@@ -2138,9 +2119,15 @@ export async function discoverMediaFromBrowser(page: Page): Promise<BrowserMedia
21382119
if (!image.id) autoImageIds.set(image, `hf-img-${autoImageId++}`);
21392120
});
21402121

2141-
const mediaEls = document.querySelectorAll(
2142-
"video[data-start], audio[data-start], img[data-var-src]",
2122+
const mediaEls = new Set<Element>(
2123+
document.querySelectorAll("video[data-start], audio[data-start], img[data-var-src]"),
21432124
);
2125+
// A variable-bound <picture><source> changes the owning image's currentSrc;
2126+
// the <img> fallback itself does not necessarily carry data-var-src.
2127+
document.querySelectorAll("picture source[data-var-src]").forEach((source) => {
2128+
const image = source.closest("picture")?.querySelector("img");
2129+
if (image) mediaEls.add(image);
2130+
});
21442131
mediaEls.forEach((el) => {
21452132
const htmlEl = el as HTMLVideoElement | HTMLAudioElement | HTMLImageElement;
21462133
const isImage = htmlEl.tagName.toLowerCase() === "img";
@@ -2459,15 +2446,14 @@ export async function recompileWithResolutions(
24592446
if (resolutions.length === 0) return compiled;
24602447

24612448
const html = injectDurations(compiled.html, resolutions);
2462-
const mediaProbeSemaphore = new Semaphore(MAX_COMPILER_MEDIA_PROBES);
24632449

24642450
// Re-parse sub-compositions with the updated parent bounds
24652451
const {
24662452
videos: subVideos,
24672453
audios: subAudios,
24682454
images: subImages,
24692455
subCompositions,
2470-
} = await parseSubCompositions(html, projectDir, downloadDir, mediaProbeSemaphore);
2456+
} = await parseSubCompositions(html, projectDir, downloadDir);
24712457

24722458
const mainVideos = parseVideoElements(html);
24732459
const mainAudios = parseAudioElements(html);

0 commit comments

Comments
 (0)