|
| 1 | +import { afterEach, expect, it, vi } from "vitest"; |
| 2 | +import { createPausedVideoFrames, getPausedVideoFrame } from "../pausedVideoFrames"; |
| 3 | +import { |
| 4 | + patchCanvasVideoFrameCompat, |
| 5 | + patchExternalVideoTextureCompat, |
| 6 | + patchVideoTextureCompat, |
| 7 | + patchWebGLVideoTextureCompat, |
| 8 | +} from "./video-texture-compat"; |
| 9 | + |
| 10 | +const sessions: ReturnType<typeof createPausedVideoFrames>[] = []; |
| 11 | +afterEach(() => { |
| 12 | + for (const session of sessions.splice(0)) session.destroy(); |
| 13 | + document.body.innerHTML = ""; |
| 14 | + vi.restoreAllMocks(); |
| 15 | + vi.unstubAllGlobals(); |
| 16 | +}); |
| 17 | + |
| 18 | +function surfaces() { |
| 19 | + const draw = vi.fn(); |
| 20 | + const offscreenDraw = vi.fn(); |
| 21 | + const image = vi.fn(); |
| 22 | + const subImage = vi.fn(); |
| 23 | + const copy = vi.fn(); |
| 24 | + const imported = vi.fn((_descriptor: unknown) => ({ texture: true })); |
| 25 | + const constructed = vi.fn(); |
| 26 | + class Context2D { |
| 27 | + drawImage(...args: unknown[]) { |
| 28 | + draw.apply(this, args); |
| 29 | + } |
| 30 | + } |
| 31 | + class Offscreen2D { |
| 32 | + drawImage(...args: unknown[]) { |
| 33 | + offscreenDraw.apply(this, args); |
| 34 | + } |
| 35 | + } |
| 36 | + class GL { |
| 37 | + texImage2D(...args: unknown[]) { |
| 38 | + image.apply(this, args); |
| 39 | + } |
| 40 | + texSubImage2D(...args: unknown[]) { |
| 41 | + subImage.apply(this, args); |
| 42 | + } |
| 43 | + } |
| 44 | + class GL2 extends GL {} |
| 45 | + class Queue { |
| 46 | + copyExternalImageToTexture(...args: unknown[]) { |
| 47 | + copy.apply(this, args); |
| 48 | + } |
| 49 | + } |
| 50 | + class Device { |
| 51 | + importExternalTexture(descriptor: unknown) { |
| 52 | + return imported.call(this, descriptor); |
| 53 | + } |
| 54 | + } |
| 55 | + class Frame { |
| 56 | + close = vi.fn(); |
| 57 | + constructor(source: unknown, options: unknown) { |
| 58 | + constructed(source, options); |
| 59 | + } |
| 60 | + } |
| 61 | + vi.stubGlobal("CanvasRenderingContext2D", Context2D); |
| 62 | + vi.stubGlobal("OffscreenCanvasRenderingContext2D", Offscreen2D); |
| 63 | + vi.stubGlobal("WebGLRenderingContext", GL); |
| 64 | + vi.stubGlobal("WebGL2RenderingContext", GL2); |
| 65 | + vi.stubGlobal("GPUQueue", Queue); |
| 66 | + vi.stubGlobal("GPUDevice", Device); |
| 67 | + vi.stubGlobal("VideoFrame", Frame); |
| 68 | + vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({ |
| 69 | + clearRect: vi.fn(), |
| 70 | + drawImage: vi.fn(), |
| 71 | + } as unknown as CanvasRenderingContext2D); |
| 72 | + return { |
| 73 | + Context2D, |
| 74 | + Offscreen2D, |
| 75 | + GL, |
| 76 | + GL2, |
| 77 | + Queue, |
| 78 | + Device, |
| 79 | + draw, |
| 80 | + offscreenDraw, |
| 81 | + image, |
| 82 | + subImage, |
| 83 | + copy, |
| 84 | + imported, |
| 85 | + constructed, |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +async function presented() { |
| 90 | + const session = createPausedVideoFrames(); |
| 91 | + sessions.push(session); |
| 92 | + const video = document.createElement("video"); |
| 93 | + video.src = "/original.mp4"; |
| 94 | + document.body.append(video); |
| 95 | + Object.defineProperties(video, { |
| 96 | + videoWidth: { value: 1920 }, |
| 97 | + videoHeight: { value: 1080 }, |
| 98 | + }); |
| 99 | + const source = document.createElement("canvas"); |
| 100 | + source.width = 320; |
| 101 | + source.height = 180; |
| 102 | + session.setProvider({ |
| 103 | + decode: async (requests) => |
| 104 | + requests.map(({ time }) => ({ |
| 105 | + source, |
| 106 | + timestamp: time, |
| 107 | + duration: 1 / 30, |
| 108 | + width: 1920, |
| 109 | + height: 1080, |
| 110 | + release: vi.fn(), |
| 111 | + })), |
| 112 | + dispose: vi.fn(), |
| 113 | + }); |
| 114 | + const show = async (time: number) => { |
| 115 | + const batch = await session.prepare( |
| 116 | + [{ video, source: video.src, time }], |
| 117 | + new AbortController().signal, |
| 118 | + ); |
| 119 | + expect(batch?.present()).toBe(true); |
| 120 | + batch!.refresh(); |
| 121 | + }; |
| 122 | + await show(3); |
| 123 | + const canvas = getPausedVideoFrame(video)!; |
| 124 | + expect([canvas.width, canvas.height]).toEqual([video.videoWidth, video.videoHeight]); |
| 125 | + return { session, video, canvas, show }; |
| 126 | +} |
| 127 | + |
| 128 | +it.each([ |
| 129 | + ["three arguments", [11, 17]], |
| 130 | + ["five arguments", [11, 17, 640, 360]], |
| 131 | + ["nine arguments", [120, 60, 960, 540, 11, 17, 640, 360]], |
| 132 | +])("preserves %s and source coordinates for both canvas surfaces", async (_name, coordinates) => { |
| 133 | + const { Context2D, Offscreen2D, draw, offscreenDraw } = surfaces(); |
| 134 | + const { session, video, canvas } = await presented(); |
| 135 | + patchCanvasVideoFrameCompat(); |
| 136 | + patchCanvasVideoFrameCompat(); |
| 137 | + const context = new Context2D(); |
| 138 | + const offscreen = new Offscreen2D(); |
| 139 | + context.drawImage(video, ...coordinates); |
| 140 | + offscreen.drawImage(video, ...coordinates); |
| 141 | + expect(draw).toHaveBeenCalledExactlyOnceWith(canvas, ...coordinates); |
| 142 | + expect(offscreenDraw).toHaveBeenCalledExactlyOnceWith(canvas, ...coordinates); |
| 143 | + expect(draw.mock.contexts[0]).toBe(context); |
| 144 | + expect(offscreenDraw.mock.contexts[0]).toBe(offscreen); |
| 145 | + session.clear(); |
| 146 | + context.drawImage(video, ...coordinates); |
| 147 | + expect(draw).toHaveBeenLastCalledWith(video, ...coordinates); |
| 148 | + const image = document.createElement("img"); |
| 149 | + offscreen.drawImage(image, ...coordinates); |
| 150 | + expect(offscreenDraw).toHaveBeenLastCalledWith(image, ...coordinates); |
| 151 | +}); |
| 152 | + |
| 153 | +it("uploads paused pixels through both WebGL versions without changing crop or upload parameters", async () => { |
| 154 | + const { GL, GL2, image, subImage } = surfaces(); |
| 155 | + const { session, video, canvas } = await presented(); |
| 156 | + patchWebGLVideoTextureCompat(); |
| 157 | + for (const Context of [GL, GL2]) { |
| 158 | + const gl = new Context(); |
| 159 | + const imageArgs = [0x0de1, 2, 0x1908, 0x1908, 0x1401]; |
| 160 | + const subArgs = [0x0de1, 2, 13, 29, 0x1908, 0x1401]; |
| 161 | + gl.texImage2D(...imageArgs, video); |
| 162 | + gl.texSubImage2D(...subArgs, video); |
| 163 | + expect(image).toHaveBeenLastCalledWith(...imageArgs, canvas); |
| 164 | + expect(subImage).toHaveBeenLastCalledWith(...subArgs, canvas); |
| 165 | + expect(image.mock.contexts.at(-1)).toBe(gl); |
| 166 | + const pixels = new Uint8Array([10, 20, 30, 255]); |
| 167 | + gl.texImage2D(0x0de1, 0, 0x1908, 1, 1, 0, 0x1908, 0x1401, pixels); |
| 168 | + expect(image).toHaveBeenLastCalledWith(0x0de1, 0, 0x1908, 1, 1, 0, 0x1908, 0x1401, pixels); |
| 169 | + } |
| 170 | + session.clear(); |
| 171 | + new GL().texImage2D(0x0de1, 0, 0x1908, 0x1908, 0x1401, video); |
| 172 | + expect(image.mock.lastCall?.at(-1)).toBe(video); |
| 173 | +}); |
| 174 | + |
| 175 | +it("preserves WebGPU crop, orientation, destination and full intrinsic copy extent", async () => { |
| 176 | + const { Queue, copy } = surfaces(); |
| 177 | + const { session, video, canvas } = await presented(); |
| 178 | + patchVideoTextureCompat(); |
| 179 | + const queue = new Queue(); |
| 180 | + const source = { source: video, origin: { x: 37, y: 41 }, flipY: true }; |
| 181 | + const destination = { texture: {}, origin: [5, 7], mipLevel: 1, premultipliedAlpha: true }; |
| 182 | + const extent = { |
| 183 | + width: video.videoWidth - 37, |
| 184 | + height: video.videoHeight - 41, |
| 185 | + depthOrArrayLayers: 1, |
| 186 | + }; |
| 187 | + queue.copyExternalImageToTexture(source, destination, extent); |
| 188 | + expect(copy).toHaveBeenCalledExactlyOnceWith({ ...source, source: canvas }, destination, extent); |
| 189 | + expect(copy.mock.contexts[0]).toBe(queue); |
| 190 | + expect(source.source).toBe(video); |
| 191 | + expect(copy.mock.calls[0]?.[1]).toBe(destination); |
| 192 | + expect(copy.mock.calls[0]?.[2]).toBe(extent); |
| 193 | + session.clear(); |
| 194 | + queue.copyExternalImageToTexture(source, destination, extent); |
| 195 | + expect(copy).toHaveBeenLastCalledWith(source, destination, extent); |
| 196 | + expect(copy.mock.lastCall?.[0]).toBe(source); |
| 197 | +}); |
| 198 | + |
| 199 | +it("reuses an external VideoFrame until presentation changes, then closes it on replacement and clear", async () => { |
| 200 | + const { Device, imported, constructed } = surfaces(); |
| 201 | + const { session, video, canvas, show } = await presented(); |
| 202 | + patchExternalVideoTextureCompat(); |
| 203 | + const once = Device.prototype.importExternalTexture; |
| 204 | + patchExternalVideoTextureCompat(); |
| 205 | + expect(Device.prototype.importExternalTexture).toBe(once); |
| 206 | + const device = new Device(); |
| 207 | + const descriptor = { source: video, colorSpace: "srgb", label: "authored texture" }; |
| 208 | + const texture = device.importExternalTexture(descriptor); |
| 209 | + const first = imported.mock.lastCall?.[0] as { |
| 210 | + source: { close: ReturnType<typeof vi.fn> }; |
| 211 | + colorSpace: string; |
| 212 | + label: string; |
| 213 | + }; |
| 214 | + expect(texture).toEqual({ texture: true }); |
| 215 | + expect(constructed).toHaveBeenCalledExactlyOnceWith(canvas, { timestamp: 3_000_000 }); |
| 216 | + expect(first).toEqual({ ...descriptor, source: first.source }); |
| 217 | + expect(first.source).not.toBe(video); |
| 218 | + expect(imported.mock.contexts[0]).toBe(device); |
| 219 | + expect(descriptor.source).toBe(video); |
| 220 | + device.importExternalTexture(descriptor); |
| 221 | + expect(imported.mock.lastCall?.[0]).toEqual(first); |
| 222 | + expect(constructed).toHaveBeenCalledTimes(1); |
| 223 | + expect(first.source.close).not.toHaveBeenCalled(); |
| 224 | + await show(4); |
| 225 | + expect(first.source.close).toHaveBeenCalledTimes(1); |
| 226 | + device.importExternalTexture(descriptor); |
| 227 | + const second = imported.mock.lastCall?.[0] as typeof first; |
| 228 | + expect(second.source).not.toBe(first.source); |
| 229 | + expect(constructed).toHaveBeenLastCalledWith(getPausedVideoFrame(video), { |
| 230 | + timestamp: 4_000_000, |
| 231 | + }); |
| 232 | + session.clear(); |
| 233 | + expect(second.source.close).toHaveBeenCalledTimes(1); |
| 234 | + device.importExternalTexture(descriptor); |
| 235 | + expect(imported).toHaveBeenLastCalledWith(descriptor); |
| 236 | + expect(imported.mock.lastCall?.[0]).toBe(descriptor); |
| 237 | + expect(constructed).toHaveBeenCalledTimes(2); |
| 238 | +}); |
0 commit comments