mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
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:
@@ -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();
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user