Skip to content

Commit 692ec34

Browse files
committed
fix(producer): preserve runtime-inserted speed ramps
1 parent 7f31d31 commit 692ec34

4 files changed

Lines changed: 147 additions & 87 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,42 @@ describe("discoverMediaFromBrowser", () => {
153153
expect(media.map((element) => element.playbackRate)).toEqual([1.5, 0.5, 1.25, 10, 1]);
154154
});
155155

156+
it.each(["video", "audio"])(
157+
"discovers a %s rate lane after the browser callback is serialized",
158+
async (tag) => {
159+
const lane = {
160+
target: "rate",
161+
points: [
162+
{ t: 0, v: 1 },
163+
{ t: 1, v: 2 },
164+
],
165+
};
166+
const automation = JSON.stringify({ version: 1, lanes: [lane] });
167+
const media = await discover(
168+
`<${tag} id="clip" src="clip.mp4" data-start="0" data-duration="1" data-playback-rate="3" data-automation='${automation}'></${tag}>`,
169+
{},
170+
true,
171+
{},
172+
{ clip: 0.5 },
173+
);
174+
175+
expect(media[0]?.playbackRate).toEqual(lane);
176+
},
177+
);
178+
179+
it("uses scalar rates when automation is invalid or has no rate lane", async () => {
180+
const media = await discover(
181+
`<video id="invalid" src="clip.mp4" data-start="0" data-playback-rate="1.5" data-automation="{nope"></video>
182+
<audio id="other" src="clip.wav" data-start="0" data-automation='{"version":1,"lanes":[{"target":"volume","points":[{"t":0,"v":0.5}]}]}'></audio>`,
183+
{},
184+
true,
185+
{},
186+
{ other: 0.5 },
187+
);
188+
189+
expect(media.map((element) => element.playbackRate)).toEqual([1.5, 0.5]);
190+
});
191+
156192
it("reports colliding empty-src videos by render id, not author id", async () => {
157193
const media = await discover(
158194
`<video id="clip" data-hf-render-id="clip" src="" data-start="0" data-end="4" data-media-start="10"></video>` +

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
} from "@hyperframes/core";
3838
import { MAX_AUDIO_GAIN } from "@hyperframes/core/audio-gain";
3939
import { readPlaybackRate } from "@hyperframes/parsers/media-duration";
40+
import { resolveRateSpec } from "@hyperframes/core/speed-ramp";
4041
import { gsapCdnDist } from "@hyperframes/core/gsap-cdn";
4142
import {
4243
assignBundledRuntimeCompositionIds,
@@ -2236,7 +2237,7 @@ export interface BrowserMediaElement {
22362237
/** True when compilation inferred duration from the fallback source. */
22372238
durationInferred: boolean;
22382239
mediaStart: number;
2239-
playbackRate: number;
2240+
playbackRate: RateSpec;
22402241
loop: boolean;
22412242
hasAudio: boolean;
22422243
volume: number;
@@ -2263,6 +2264,7 @@ export async function discoverMediaFromBrowser(page: Page): Promise<BrowserMedia
22632264
playbackStartRaw: string | null;
22642265
mediaStartRaw: string | null;
22652266
playbackRateRaw: string | null;
2267+
automationRaw: string | null;
22662268
defaultPlaybackRate: number;
22672269
loop: boolean;
22682270
hasAudio: boolean;
@@ -2315,6 +2317,7 @@ export async function discoverMediaFromBrowser(page: Page): Promise<BrowserMedia
23152317
const playbackStartRaw = htmlEl.getAttribute("data-playback-start");
23162318
const mediaStartRaw = htmlEl.getAttribute("data-media-start");
23172319
const playbackRateRaw = htmlEl.getAttribute("data-playback-rate");
2320+
const automationRaw = htmlEl.getAttribute("data-automation");
23182321
// playbackRate can include the transport speed; defaultPlaybackRate is the clip's own rate.
23192322
const defaultPlaybackRate = isImage
23202323
? 1
@@ -2338,6 +2341,7 @@ export async function discoverMediaFromBrowser(page: Page): Promise<BrowserMedia
23382341
playbackStartRaw,
23392342
mediaStartRaw,
23402343
playbackRateRaw,
2344+
automationRaw,
23412345
defaultPlaybackRate,
23422346
loop,
23432347
hasAudio,
@@ -2357,6 +2361,7 @@ export async function discoverMediaFromBrowser(page: Page): Promise<BrowserMedia
23572361
playbackStartRaw,
23582362
mediaStartRaw,
23592363
playbackRateRaw,
2364+
automationRaw,
23602365
defaultPlaybackRate,
23612366
...element
23622367
}) => ({
@@ -2373,9 +2378,12 @@ export async function discoverMediaFromBrowser(page: Page): Promise<BrowserMedia
23732378
return null;
23742379
},
23752380
}),
2376-
playbackRate: readPlaybackRate(
2377-
(name) => (name === "data-playback-rate" ? playbackRateRaw : null),
2378-
defaultPlaybackRate,
2381+
playbackRate: resolveRateSpec(
2382+
automationRaw,
2383+
readPlaybackRate(
2384+
(name) => (name === "data-playback-rate" ? playbackRateRaw : null),
2385+
defaultPlaybackRate,
2386+
),
23792387
),
23802388
}),
23812389
);

‎packages/producer/src/services/render/stages/probeStage.test.ts‎

Lines changed: 93 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -414,98 +414,116 @@ describe("runProbeStage — forceScreenshot threading", () => {
414414
});
415415

416416
// Scene in-point 5 and clip trim 1: the collector starts picture and sound at source 6.
417-
it.each([
418-
{ tagName: "video", existing: false },
419-
{ tagName: "audio", existing: false },
420-
{ tagName: "video", existing: true },
421-
{ tagName: "audio", existing: true },
422-
])("retains a runtime $tagName rate (existing: $existing)", async ({ tagName, existing }) => {
423-
resetRetryMocks();
424-
browserMediaResults = [
425-
{
417+
const runtimeRates = [
418+
1.5,
419+
{
420+
target: "rate",
421+
points: [
422+
{ t: 0, v: 1 },
423+
{ t: 3, v: 2 },
424+
],
425+
} satisfies RateSpec,
426+
];
427+
it.each(
428+
runtimeRates.flatMap((playbackRate) => [
429+
{ tagName: "video", existing: false, playbackRate },
430+
{ tagName: "audio", existing: false, playbackRate },
431+
{ tagName: "video", existing: true, playbackRate },
432+
{ tagName: "audio", existing: true, playbackRate },
433+
]),
434+
)(
435+
"retains a runtime $tagName rate (existing: $existing, rate: $playbackRate)",
436+
async ({ tagName, existing, playbackRate }) => {
437+
resetRetryMocks();
438+
browserMediaResults = [
439+
{
440+
id: "clip",
441+
tagName,
442+
src: "clip.mp4",
443+
start: 0,
444+
end: 3,
445+
duration: 3,
446+
mediaStart: 0.5,
447+
playbackRate,
448+
loop: false,
449+
hasAudio: true,
450+
volume: 1,
451+
muted: false,
452+
},
453+
];
454+
const { runProbeStage } = await import("./probeStage.js");
455+
const input = makeProbeInput({});
456+
const clip = {
426457
id: "clip",
427-
tagName,
428458
src: "clip.mp4",
429459
start: 0,
430460
end: 3,
431-
duration: 3,
432461
mediaStart: 0.5,
433-
playbackRate: 1.5,
434-
loop: false,
435-
hasAudio: true,
436-
volume: 1,
437-
muted: false,
438-
},
439-
];
440-
const { runProbeStage } = await import("./probeStage.js");
441-
const input = makeProbeInput({});
442-
const clip = {
443-
id: "clip",
444-
src: "clip.mp4",
445-
start: 0,
446-
end: 3,
447-
mediaStart: 0.5,
448-
playbackRate: 1,
449-
};
450-
if (existing && tagName === "video") {
451-
input.composition.videos.push({ ...clip, loop: false, hasAudio: true });
452-
input.composition.audios.push({ ...clip, id: "clip-audio", layer: 0, type: "video" });
453-
}
454-
if (existing && tagName === "audio") {
455-
input.composition.audios.push({ ...clip, layer: 0, type: "audio" });
456-
}
462+
playbackRate: 1,
463+
};
464+
if (existing && tagName === "video") {
465+
input.composition.videos.push({ ...clip, loop: false, hasAudio: true });
466+
input.composition.audios.push({ ...clip, id: "clip-audio", layer: 0, type: "video" });
467+
}
468+
if (existing && tagName === "audio") {
469+
input.composition.audios.push({ ...clip, layer: 0, type: "audio" });
470+
}
457471

458-
await runProbeStage(input);
472+
await runProbeStage(input);
459473

460-
const media = tagName === "video" ? input.composition.videos : input.composition.audios;
461-
expect(media[0]).toMatchObject({ playbackRate: 1.5, mediaStart: 0.5 });
462-
if (existing && tagName === "video") {
463-
expect(input.composition.audios[0]).toMatchObject({ playbackRate: 1.5, mediaStart: 0.5 });
464-
}
465-
});
474+
const media = tagName === "video" ? input.composition.videos : input.composition.audios;
475+
expect(media[0]).toMatchObject({ playbackRate, mediaStart: 0.5 });
476+
if (existing && tagName === "video") {
477+
expect(input.composition.audios[0]).toMatchObject({ playbackRate, mediaStart: 0.5 });
478+
}
479+
},
480+
);
466481

467-
it("preserves a collected speed ramp during scalar browser reconciliation", async () => {
468-
resetRetryMocks();
469-
browserMediaResults = [
470-
{
482+
it.each(runtimeRates)(
483+
"preserves a collected speed ramp during browser rate %j reconciliation",
484+
async (browserRate) => {
485+
resetRetryMocks();
486+
browserMediaResults = [
487+
{
488+
id: "clip",
489+
tagName: "video",
490+
src: "clip.mp4",
491+
start: 0,
492+
end: 3,
493+
duration: 3,
494+
mediaStart: 0,
495+
playbackRate: browserRate,
496+
loop: false,
497+
hasAudio: false,
498+
volume: 1,
499+
muted: false,
500+
},
501+
];
502+
const { runProbeStage } = await import("./probeStage.js");
503+
const input = makeProbeInput({});
504+
const playbackRate = {
505+
target: "rate",
506+
points: [
507+
{ t: 0, v: 1.25 },
508+
{ t: 2, v: 2 },
509+
],
510+
} satisfies RateSpec;
511+
input.composition.videos.push({
471512
id: "clip",
472-
tagName: "video",
473513
src: "clip.mp4",
474514
start: 0,
475515
end: 3,
476-
duration: 3,
477516
mediaStart: 0,
478-
playbackRate: 1,
517+
playbackRate,
479518
loop: false,
480519
hasAudio: false,
481-
volume: 1,
482-
muted: false,
483-
},
484-
];
485-
const { runProbeStage } = await import("./probeStage.js");
486-
const input = makeProbeInput({});
487-
const playbackRate = {
488-
target: "rate",
489-
points: [
490-
{ t: 0, v: 1 },
491-
{ t: 3, v: 2 },
492-
],
493-
} satisfies RateSpec;
494-
input.composition.videos.push({
495-
id: "clip",
496-
src: "clip.mp4",
497-
start: 0,
498-
end: 3,
499-
mediaStart: 0,
500-
playbackRate,
501-
loop: false,
502-
hasAudio: false,
503-
});
520+
});
504521

505-
await runProbeStage(input);
522+
await runProbeStage(input);
506523

507-
expect(input.composition.videos[0]?.playbackRate).toEqual(playbackRate);
508-
});
524+
expect(input.composition.videos[0]?.playbackRate).toEqual(playbackRate);
525+
},
526+
);
509527

510528
it.each([
511529
{ pageStart: 1, expected: 6 },

‎packages/producer/src/services/render/stages/probeStage.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ function authoredMediaStarts(html: string): Map<string, number> {
192192
return starts;
193193
}
194194

195+
function isVideoAudio(audio: CompositionMetadata["audios"][number], videoId: string): boolean {
196+
return audio.type === "video" && audio.id === `${videoId}-audio`;
197+
}
198+
195199
/**
196200
* Take a start point the page changed by moving the collected one by the same amount, so the
197201
* in-point its scene added survives, and a video's sound track moves with its picture.
@@ -207,9 +211,7 @@ function adoptRuntimeMediaStart(
207211
el.tagName === "video"
208212
? [
209213
...composition.videos.filter((video) => video.id === el.id),
210-
...composition.audios.filter(
211-
(audio) => audio.type === "video" && audio.id === `${el.id}-audio`,
212-
),
214+
...composition.audios.filter((audio) => isVideoAudio(audio, el.id)),
213215
]
214216
: composition.audios.filter((audio) => audio.type !== "video" && audio.id === el.id);
215217
for (const entry of entries) entry.mediaStart += shift;
@@ -603,11 +605,7 @@ export async function runProbeStage(input: ProbeStageInput): Promise<ProbeStageR
603605
if (typeof existing.playbackRate !== "object") {
604606
existing.playbackRate = el.playbackRate;
605607
for (const audio of composition.audios) {
606-
if (
607-
audio.type === "video" &&
608-
audio.id === `${el.id}-audio` &&
609-
typeof audio.playbackRate !== "object"
610-
) {
608+
if (isVideoAudio(audio, el.id) && typeof audio.playbackRate !== "object") {
611609
audio.playbackRate = el.playbackRate;
612610
}
613611
}

0 commit comments

Comments
 (0)