From 43e5b7cecdd66d2df14cee219bb08db27da3d88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 17 Nov 2026 12:09:33 +0000 Subject: [PATCH] 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 --- .../src/runtime/pausedVideoFrames.test.ts | 39 +++++++++++++++++++ .../core/src/runtime/pausedVideoFrames.ts | 13 ++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/core/src/runtime/pausedVideoFrames.test.ts b/packages/core/src/runtime/pausedVideoFrames.test.ts index fe46060754..772a6f7f64 100644 --- a/packages/core/src/runtime/pausedVideoFrames.test.ts +++ b/packages/core/src/runtime/pausedVideoFrames.test.ts @@ -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(); +}); diff --git a/packages/core/src/runtime/pausedVideoFrames.ts b/packages/core/src/runtime/pausedVideoFrames.ts index 322b967ef5..908cf6b0b6 100644 --- a/packages/core/src/runtime/pausedVideoFrames.ts +++ b/packages/core/src/runtime/pausedVideoFrames.ts @@ -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; } }