All files / player-browser/src/media bga-camera.ts

1.53% Statements 1/65
0% Branches 0/34
0% Functions 0/9
1.78% Lines 1/56

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                              28x                                                                                                                                                                                                                                                      
/**
 * A camera picture for the BGA frame: the `MediaStream` the host captured (`getUserMedia`), played in a muted
 * `<video>` and wrapped in a Pixi `VideoSource`, as a chart's video BGA is. The play scenes draw its texture in place of
 * the chart's BGA (`renderBGA`, the beatoraja BGA layer), and a Be-Music Skin that composites the BGA itself gets
 * {@link BGACameraFeed.image}.
 *
 * The feed only shows the stream. The host owns it: it opens the camera, stops its tracks, and drops the feed when the
 * camera goes away. Nothing here sends the picture anywhere; it reaches only the canvas (and so a Capture recording of
 * the canvas).
 */
 
import { groupD8, Texture, VideoSource } from 'pixi.js';
import { fitBGACameraRect } from './bga-camera-fit.ts';
 
/** The side of the square picture a Canvas skin gets, the BMS BGA canvas. */
const SKIN_IMAGE_SIZE = 256;
 
export interface BGACameraFeedOptions {
  /** Flip the picture left to right, as a mirror shows it. */
  readonly mirror?: boolean;
}
 
/** A picture the BGA frame shows in place of the chart's BGA planes: the camera, or an LRX chart's composited BGA. */
export interface BGAFeed {
  /** The texture to draw this frame, or `undefined` while there is none. */
  texture(): Texture | undefined;
  /** A 256x256 picture for a Be-Music Skin that composites the BGA itself. */
  image(): HTMLCanvasElement | undefined;
}
 
export class BGACameraFeed implements BGAFeed {
  public readonly stream: MediaStream;
  public readonly video: HTMLVideoElement;
  /** Flip the picture left to right. Applies from the next frame. */
  public mirror: boolean;
  private source: VideoSource | undefined;
  private plain: Texture | undefined;
  private mirrored: Texture | undefined;
  private skinCanvas: HTMLCanvasElement | undefined;
  private disposed = false;
 
  public constructor(stream: MediaStream, options: BGACameraFeedOptions = {}) {
    this.stream = stream;
    this.mirror = options.mirror === true;
    const video = document.createElement('video');
    video.muted = true;
    video.playsInline = true;
    video.autoplay = true;
    video.srcObject = stream;
    this.video = video;
    void video.play().catch(() => {
      // The scene shows the camera after a click or key press, so autoplay is allowed; if it is not, the frame stays
      // black until `texture` tries to play it again.
    });
  }
 
  /**
   * The texture to draw this frame, mirrored when {@link mirror} is set, or `undefined` until the camera has a frame.
   * WebGPU cannot upload a video with no decoded frame, so the source is made only once one is there.
   */
  public texture(): Texture | undefined {
    if (this.disposed) return undefined;
    const video = this.video;
    if (video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA || video.videoWidth === 0 || video.videoHeight === 0) {
      if (video.paused) void video.play().catch(() => {});
      return undefined;
    }
    if (this.source === undefined) {
      this.source = new VideoSource({ resource: video, autoPlay: true, autoLoad: true });
      this.source.label = 'bga/camera';
      this.plain = new Texture({ source: this.source, label: 'bga/camera' });
      this.mirrored = new Texture({ source: this.source, rotate: groupD8.MIRROR_HORIZONTAL, label: 'bga/camera' });
    }
    // A camera can change its size (a phone turned, another resolution picked); the textures follow the source.
    if (this.source.width !== video.videoWidth || this.source.height !== video.videoHeight) {
      this.source.resize(video.videoWidth, video.videoHeight);
    }
    if (video.paused) void video.play().catch(() => {});
    return this.mirror ? this.mirrored : this.plain;
  }
 
  /**
   * The picture for a Be-Music Skin that composites the BGA itself: a 256x256 canvas with the camera fitted in, black
   * around it and mirrored when {@link mirror} is set, redrawn on each call. `undefined` until the camera has a frame.
   */
  public image(): HTMLCanvasElement | undefined {
    if (this.disposed) return undefined;
    const video = this.video;
    if (video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA || video.videoWidth === 0) return undefined;
    const canvas = (this.skinCanvas ??= Object.assign(document.createElement('canvas'), {
      width: SKIN_IMAGE_SIZE,
      height: SKIN_IMAGE_SIZE,
    }));
    const context = canvas.getContext('2d');
    if (context === null) return undefined;
    const placed = fitBGACameraRect(video.videoWidth, video.videoHeight, {
      x: 0,
      y: 0,
      w: SKIN_IMAGE_SIZE,
      h: SKIN_IMAGE_SIZE,
    });
    context.save();
    context.fillStyle = '#000';
    context.fillRect(0, 0, SKIN_IMAGE_SIZE, SKIN_IMAGE_SIZE);
    if (this.mirror) {
      context.translate(SKIN_IMAGE_SIZE, 0);
      context.scale(-1, 1);
    }
    try {
      context.drawImage(video, placed.x, placed.y, placed.w, placed.h);
    } catch {
      // No frame to copy yet: the black canvas shows until the next draw.
    }
    context.restore();
    return canvas;
  }
 
  /** Stops showing the stream. The host stops the stream's tracks itself. */
  public dispose(): void {
    if (this.disposed) return;
    this.disposed = true;
    this.video.pause();
    this.video.srcObject = null;
    const { plain, mirrored, source } = this;
    this.source = undefined;
    this.plain = undefined;
    this.mirrored = undefined;
    // A BGA sprite may still hold the texture until the scene's next frame draws without it, so the GPU side goes a
    // couple of frames later. Destroying the source also drops its video hooks.
    const release = (): void => {
      plain?.destroy(false);
      mirrored?.destroy(false);
      source?.destroy();
    };
    if (typeof requestAnimationFrame === 'function') requestAnimationFrame(() => requestAnimationFrame(release));
    else setTimeout(release, 50);
  }
}