Skip to content

Commit fc41fa2

Browse files
fix(engine): local renders lay out width-only videos like the browser (#5771)
* fix(engine): local renders lay out width-only videos like the browser * chore(engine): name the poster instead of commenting it
1 parent a498cf2 commit fc41fa2

2 files changed

Lines changed: 77 additions & 13 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { spawnSync } from "node:child_process";
2+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
3+
import puppeteer, { type Browser } from "puppeteer-core";
4+
import { resolveHeadlessShellPath } from "./browserManager.js";
5+
import { applyVideoMetadataHints } from "./frameCapture.js";
6+
7+
const chromePath = resolveHeadlessShellPath();
8+
const hasBrowser = chromePath !== undefined && spawnSync(chromePath, ["--version"]).status === 0;
9+
10+
// Boxes Chrome gives these styles once a 640x360 video's metadata has loaded.
11+
const cases = {
12+
noSize: { style: "", box: "640x360" },
13+
widthOnly: { style: "width:200px", box: "200x112.5" },
14+
heightOnly: { style: "height:90px", box: "160x90" },
15+
bothSizes: { style: "width:200px;height:300px;object-fit:cover", box: "200x300" },
16+
maxWidthOnly: { style: "max-width:100px", box: "100x56.25" },
17+
// A flex item may not shrink below its natural width, so its border overflows the 400px row.
18+
borderedFlexItem: { style: "width:100%;border:8px solid", box: "416x241" },
19+
};
20+
const ids = Object.keys(cases);
21+
22+
describe.skipIf(!hasBrowser)("applyVideoMetadataHints in Chrome", () => {
23+
let browser: Browser;
24+
25+
beforeAll(async () => {
26+
browser = await puppeteer.launch({
27+
executablePath: chromePath,
28+
headless: true,
29+
args: ["--no-sandbox", "--disable-dev-shm-usage"],
30+
});
31+
});
32+
afterAll(async () => {
33+
await browser?.close();
34+
});
35+
36+
it("lays out a video without loaded metadata as it would with it", async () => {
37+
const page = await browser.newPage();
38+
const videos = Object.entries(cases).map(
39+
([id, { style }]) =>
40+
`<div style="display:flex;align-items:center;width:400px"><video id="${id}" style="${style}"></video></div>`,
41+
);
42+
await page.setContent(`<body style="margin:0">${videos.join("")}</body>`);
43+
44+
await applyVideoMetadataHints(
45+
page,
46+
ids.map((id) => ({ id, width: 640, height: 360 })),
47+
);
48+
49+
const boxes = await page.evaluate((videoIds) => {
50+
return Object.fromEntries(
51+
videoIds.map((id) => {
52+
const rect = document.getElementById(id)!.getBoundingClientRect();
53+
return [id, `${rect.width}x${rect.height}`];
54+
}),
55+
);
56+
}, ids);
57+
expect(boxes).toEqual(
58+
Object.fromEntries(Object.entries(cases).map(([id, { box }]) => [id, box])),
59+
);
60+
await page.close();
61+
});
62+
});

‎packages/engine/src/services/frameCapture.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,15 +2217,16 @@ async function decodeAllImages(page: Page): Promise<void> {
22172217
});
22182218
}
22192219

2220-
async function applyVideoMetadataHints(
2220+
export async function applyVideoMetadataHints(
22212221
page: Page,
22222222
hints: readonly CaptureVideoMetadataHint[] | undefined,
22232223
): Promise<void> {
22242224
if (!hints || hints.length === 0) return;
22252225

22262226
// fallow-ignore-next-line complexity
22272227
await page.evaluate(
2228-
(metadataHints: CaptureVideoMetadataHint[]) => {
2228+
async (metadataHints: CaptureVideoMetadataHint[]) => {
2229+
const posters = new Set<string>();
22292230
for (const hint of metadataHints) {
22302231
if (
22312232
!hint.id ||
@@ -2239,19 +2240,20 @@ async function applyVideoMetadataHints(
22392240

22402241
const video = (window.__hfMediaEl?.(hint.id) ??
22412242
document.getElementById(hint.id)) as HTMLVideoElement | null;
2242-
if (!video) continue;
2243+
if (!video || video.hasAttribute("poster")) continue;
22432244

2244-
if (!video.hasAttribute("width")) video.setAttribute("width", String(hint.width));
2245-
if (!video.hasAttribute("height")) video.setAttribute("height", String(hint.height));
2246-
2247-
const computed = window.getComputedStyle(video);
2248-
if (
2249-
!video.style.aspectRatio &&
2250-
(!computed.aspectRatio || computed.aspectRatio === "auto")
2251-
) {
2252-
video.style.aspectRatio = `${hint.width} / ${hint.height}`;
2253-
}
2245+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${hint.width}" height="${hint.height}"/>`;
2246+
const intrinsicSizePoster = `data:image/svg+xml,${encodeURIComponent(svg)}`;
2247+
video.poster = intrinsicSizePoster;
2248+
posters.add(intrinsicSizePoster);
22542249
}
2250+
await Promise.all(
2251+
[...posters].map((src) => {
2252+
const image = new Image();
2253+
image.src = src;
2254+
return image.decode();
2255+
}),
2256+
);
22552257
},
22562258
[...hints],
22572259
);

0 commit comments

Comments
 (0)