All files / player-browser/src/media color-key-video.ts

91.48% Statements 43/47
86.36% Branches 19/22
100% Functions 5/5
94.87% Lines 37/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73        53x   24x                           6x 6x 6x 6x   6x 6x 6x 6x 6x   6x 11x 11x       11x 11x 1x 1x   10x 10x 10x   10x 10x 10x 10x         6x 10x 5x 5x   18x 6x 6x 6x     6x 6x 18x 6x        
import { applyBMSColorKey, type BmsARGB } from '@be-music/chart-model';
import { Texture } from 'pixi.js';
import { canDrawBGAPixelsOnGPU, drawBGAPixelsOnGPU } from './bga-gpu-pixels.ts';
 
const dynamicCanvases = new WeakSet<object>();
export function isColorKeyVideoCanvas(resource: object): boolean {
  return dynamicCanvases.has(resource);
}
 
/**
 * Key decoded video pixels before sub-region views are cut; every alias shares the live native-size canvas. Each frame
 * is keyed on the shared GPU context (`bga-gpu-pixels.ts`), or on the CPU where WebGL2 is unavailable.
 */
export function createColorKeyVideoTexture(
  video: HTMLVideoElement,
  keys: { bmsColorKey?: BmsARGB; swBGAColorKey?: BmsARGB },
): {
  texture: Texture;
  dispose: () => void;
} {
  const canvas = document.createElement('canvas');
  canvas.width = Math.max(1, video.videoWidth);
  canvas.height = Math.max(1, video.videoHeight);
  const onGPU = canDrawBGAPixelsOnGPU();
  // Only the CPU path reads the pixels back; on the GPU path the canvas stays on the GPU.
  const context = canvas.getContext('2d', { willReadFrequently: !onGPU })!;
  const texture = Texture.from(canvas);
  texture.source.scaleMode = 'nearest';
  dynamicCanvases.add(canvas);
  let disposed = false;
  let callback: number | undefined;
  const update = (): void => {
    Iif (disposed || !video.videoWidth || !video.videoHeight || video.readyState < 2) return;
    Iif (canvas.width !== video.videoWidth || canvas.height !== video.videoHeight) {
      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;
    }
    try {
      if (onGPU && drawBGAPixelsOnGPU(video, canvas.width, canvas.height, keys, context)) {
        texture.source.update();
        return;
      }
      context.clearRect(0, 0, canvas.width, canvas.height);
      context.drawImage(video, 0, 0);
      const pixels = context.getImageData(0, 0, canvas.width, canvas.height);
      // EXBMP compares the inserted alpha of opaque movies against zero.
      if (keys.bmsColorKey) applyBMSColorKey(pixels.data, keys.bmsColorKey, false);
      if (keys.swBGAColorKey) applyBMSColorKey(pixels.data, keys.swBGAColorKey, true);
      context.putImageData(pixels, 0, 0);
      texture.source.update();
    } catch {
      /* A seeking/not-yet-buffered video is retried on the next decoded frame. */
    }
  };
  const next = (): void => {
    if (disposed) return;
    update();
    callback = video.requestVideoFrameCallback?.(next);
  };
  for (const event of ['loadeddata', 'seeked', 'timeupdate']) video.addEventListener(event, update);
  update();
  callback = video.requestVideoFrameCallback?.(next);
  return {
    texture,
    dispose: () => {
      disposed = true;
      Eif (callback !== undefined) video.cancelVideoFrameCallback?.(callback);
      for (const event of ['loadeddata', 'seeked', 'timeupdate']) video.removeEventListener(event, update);
      dynamicCanvases.delete(canvas);
    },
  };
}