fix(core): a held video picture is drawn through a VideoFrame closed after the draw (#7971)

* test(core): a held picture is drawn through a closed VideoFrame

* fix(core): a held video picture is drawn through a VideoFrame closed after the draw

* test(core): read the held frame's closed flag only once it is a VideoFrame
This commit is contained in:
Miguel Ángel
2026-11-17 12:09:33 +00:00
committed by GitHub
parent 34f98e6a04
commit 43e5b7cecd
2 changed files with 51 additions and 1 deletions
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import {
captureNativeVideoFrame,
createPausedVideoFrames,
getPausedVideoFrame,
ownsPausedVideoTime,
@@ -311,3 +312,41 @@ it("leaves the paused frame of a source the scan did not report as it was decode
expect(getPausedVideoFrame(video)!.style.filter).not.toContain("__hf_native_video_look");
vi.unstubAllGlobals();
});
// fails without the fix: drawImage(video) left each decoded frame in the renderer's image cache; a strip capture of a
// 1080p film held 7.9 MB per seek there, never reused.
it("draws a held picture through a VideoFrame closed after the draw, else from the video itself", () => {
const draws: Array<{ source: unknown; closed: boolean }> = [];
class FakeVideoFrame {
closed = false;
constructor(readonly video: HTMLVideoElement) {}
close() {
this.closed = true;
}
}
vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({
drawImage: (source: unknown) =>
draws.push({ source, closed: source instanceof FakeVideoFrame && source.closed }),
} as unknown as CanvasRenderingContext2D);
const video = document.createElement("video");
Object.defineProperty(video, "videoWidth", { value: 64 });
Object.defineProperty(video, "videoHeight", { value: 36 });
vi.stubGlobal("VideoFrame", FakeVideoFrame);
expect(captureNativeVideoFrame(video)?.width).toBe(64);
const [first] = draws;
const source = first?.source;
expect(source).toBeInstanceOf(FakeVideoFrame);
expect(first?.closed).toBe(false);
expect(source instanceof FakeVideoFrame && source.closed).toBe(true);
vi.stubGlobal(
"VideoFrame",
class {
constructor() {
throw new DOMException("tainted", "SecurityError");
}
},
);
expect(captureNativeVideoFrame(video)).not.toBeNull();
expect(draws[1]?.source).toBe(video);
vi.unstubAllGlobals();
});
+12 -1
View File
@@ -80,11 +80,22 @@ export function captureNativeVideoFrame(video: HTMLVideoElement): HTMLCanvasElem
canvas.height = video.videoHeight;
const context = canvas.getContext("2d");
if (!context) return null;
const frame = videoFrameOf(video);
try {
context.drawImage(video, 0, 0);
context.drawImage(frame ?? video, 0, 0);
return canvas;
} catch {
return null;
} finally {
frame?.close();
}
}
function videoFrameOf(video: HTMLVideoElement): VideoFrame | null {
try {
return new VideoFrame(video);
} catch {
return null;
}
}