All files / player-browser/src/media/bga-stream decode-worker.ts

94.91% Statements 56/59
73.91% Branches 17/23
88.23% Functions 15/17
96.36% Lines 53/55

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 140 141 142 143 144 145 146 147                                                                        105x   105x   105x   105x     3x 3x 3x 3x             3x 3x 18x   3x 3x     3x           3x 3x   3x     3x 3x 79x 79x 79x     3x 3x 1x   14x 14x   14x     3x 3x 3x 3x               5x 5x 5x 1x   4x 4x   5x     2x     3x   3x 3x   3x             3x   3x 3x 1x 1x   2x   3x 3x         7x    
/**
 * Decode side of the streaming BGA transcode. Runs in a module worker the host bundles (see
 * {@link configureBGAVideoWorkers}); call {@link runBGAVideoDecodeWorker} from its entry.
 *
 * It boots ffmpeg.wasm's core directly rather than through `@ffmpeg/ffmpeg`, so it can register a character device
 * as ffmpeg's output: the device's `write` runs synchronously inside `exec`, which hands each piece of output over
 * while the decode is still going — the stock wrapper only reads files back once `exec` returns. The core's wasm is
 * the stock build, unchanged.
 */
import { buildBGAStreamArgs } from './ffmpeg-args.ts';
import { waitForEncoder } from './flow.ts';
import type { BGADecodeStartMessage, BGAFrameMessage, BGAStreamWorkerMessage } from './protocol.ts';
import { Y4MParser } from './y4m.ts';
 
/** The parts of the Emscripten module `createFFmpegCore` resolves to that this worker uses. */
interface FFmpegCore {
  FS: {
    makedev(major: number, minor: number): number;
    registerDevice(device: number, ops: Record<string, unknown>): void;
    mkdev(path: string, device: number): void;
    mkdir(path: string): void;
    mount(type: unknown, options: { blobs: Array<{ name: string; data: Blob }> }, mountPoint: string): void;
    filesystems: { WORKERFS: unknown };
    ErrnoError: new (errno: number) => Error;
  };
  setLogger(logger: (data: { type: string; message: string }) => void): void;
  exec(...args: string[]): number;
}
 
type CreateFFmpegCore = (options: Record<string, unknown>) => Promise<FFmpegCore>;
 
interface WorkerScope {
  onmessage: ((event: MessageEvent) => void) | null;
  postMessage(message: unknown, transfer?: Transferable[]): void;
}
 
const OUTPUT_DEVICE = '/dev/bga-stream';
/** Where the source file is mounted. */
const INPUT_DIRECTORY = '/bga-input';
/** ffmpeg's stderr lines kept for the error report. */
const LOG_TAIL_LINES = 12;
/** `ESPIPE`: the device can't seek, which tells ffmpeg the output is a pipe. */
const ESPIPE = 70;
 
export function runBGAVideoDecodeWorker(scope: WorkerScope = globalThis as unknown as WorkerScope): void {
  scope.onmessage = (event) => {
    scope.onmessage = null;
    const message = event.data as BGADecodeStartMessage;
    void decode(scope, message).catch((error: unknown) => {
      post(scope, { type: 'error', message: error instanceof Error ? error.message : String(error) });
    });
  };
}
 
async function decode(scope: WorkerScope, message: BGADecodeStartMessage): Promise<void> {
  const framePort = message.framePort;
  const sendFrame = (frame: BGAFrameMessage, transfer: Transferable[] = []): void => {
    framePort?.postMessage(frame, transfer);
  };
  try {
    const { default: createFFmpegCore } = (await import(/* @vite-ignore */ message.coreURL)) as {
      default: CreateFFmpegCore;
    };
    const core = await createFFmpegCore({
      // Instantiate the module the main thread compiled instead of fetching and compiling 30 MB per worker.
      instantiateWasm(
        imports: WebAssembly.Imports,
        receive: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void,
      ) {
        void WebAssembly.instantiate(message.wasmModule, imports).then((instance) =>
          receive(instance, message.wasmModule),
        );
        return {};
      },
    });
    const log: string[] = [];
    core.setLogger(({ type, message: line }) => {
      Iif (type !== 'stderr') return;
      log.push(line);
      if (log.length > LOG_TAIL_LINES) log.shift();
    });
 
    let frames = 0;
    const parser = new Y4MParser({
      header: (header) => sendFrame({ type: 'header', ...header }),
      frame: (data) => {
        frames++;
        sendFrame({ type: 'frame', data: data.buffer as ArrayBuffer }, [data.buffer]);
        // Holds ffmpeg here, mid-`exec`, while the encoder is behind.
        Eif (message.flow) waitForEncoder(message.flow, frames);
      },
    });
    let bytes = 0;
    const { FS } = core;
    const device = FS.makedev(64, 0);
    FS.registerDevice(device, {
      open() {},
      close() {},
      read() {
        return 0;
      },
      // `buffer` is a view of the wasm heap: copy out what is needed before returning.
      write(_stream: unknown, buffer: Int8Array, offset: number, length: number) {
        const view = new Uint8Array(buffer.buffer, buffer.byteOffset + offset, length);
        bytes += length;
        if (message.mode === 'raw') {
          parser.push(view);
        } else {
          const data = view.slice();
          post(scope, { type: 'chunk', data: data.buffer }, [data.buffer]);
        }
        return length;
      },
      llseek() {
        throw new FS.ErrnoError(ESPIPE);
      },
    });
    FS.mkdev(OUTPUT_DEVICE, device);
    // WORKERFS reads the blob in slices (`FileReaderSync`) as ffmpeg asks, instead of copying the file into the heap.
    FS.mkdir(INPUT_DIRECTORY);
    FS.mount(FS.filesystems.WORKERFS, { blobs: [{ name: message.inputName, data: message.input }] }, INPUT_DIRECTORY);
 
    const args = buildBGAStreamArgs({
      input: `${INPUT_DIRECTORY}/${message.inputName}`,
      output: OUTPUT_DEVICE,
      mode: message.mode,
      ...(message.maxLongEdge !== undefined ? { maxLongEdge: message.maxLongEdge } : {}),
      ...(message.quality !== undefined ? { quality: message.quality } : {}),
    });
    const exitCode = core.exec(...args);
    // A truncated or damaged file often decodes most of the way and then fails: what came out still plays.
    const produced = message.mode === 'raw' ? frames > 0 : bytes > 0;
    if (exitCode !== 0 && !produced) {
      post(scope, { type: 'error', message: `ffmpeg exited with code ${exitCode}`, log });
      return;
    }
    post(scope, { type: 'done', frames });
  } finally {
    sendFrame({ type: 'end' });
    framePort?.close();
  }
}
 
function post(scope: WorkerScope, message: BGAStreamWorkerMessage, transfer: Transferable[] = []): void {
  scope.postMessage(message, transfer);
}