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

1.26% Statements 1/79
0% Branches 0/38
0% Functions 0/19
1.49% Lines 1/67

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                        104x                                                                                                                                                                                                                                                                                                    
/**
 * Encode side of the streaming BGA transcode. Runs in a module worker the host bundles (see
 * {@link configureBGAVideoWorkers}); call {@link runBGAVideoEncodeWorker} from its entry.
 *
 * Takes the decode worker's I420 frames, encodes them to H.264 with WebCodecs (hardware first where the platform has
 * an encoder) and muxes them into fragmented MP4 with Mediabunny, posting the bytes to the main thread as each
 * fragment closes.
 */
import type { Output, VideoSampleSource, VideoSample as VideoSampleClass } from 'mediabunny';
import { signalFrameTaken } from './flow.ts';
import { estimateBGATranscodeBitrate, type BGATranscodeQuality } from './ffmpeg-args.ts';
import type { BGAEncodeStartMessage, BGAFrameMessage, BGAStreamWorkerMessage } from './protocol.ts';
import type { Y4MHeader } from './y4m.ts';
 
interface WorkerScope {
  onmessage: ((event: MessageEvent) => void) | null;
  postMessage(message: unknown, transfer?: Transferable[]): void;
}
 
/** Short fragments start playback sooner; MSE takes them as they come. */
const FRAGMENT_SECONDS = 0.5;
 
export function runBGAVideoEncodeWorker(scope: WorkerScope = globalThis as unknown as WorkerScope): void {
  scope.onmessage = (event) => {
    const message = event.data as BGAEncodeStartMessage | BGAFrameMessage;
    // The main thread sends `end` itself when the decode worker dies without saying so.
    if (message.type === 'end') {
      encoder?.end();
      return;
    }
    if (message.type !== 'start' || encoder) return;
    encoder = new FrameEncoder(scope, message.flow, message.quality);
    message.framePort.onmessage = (frameEvent) => encoder!.receive(frameEvent.data as BGAFrameMessage);
  };
  let encoder: FrameEncoder | undefined;
}
 
class FrameEncoder {
  private readonly scope: WorkerScope;
  /** Settles once the output is ready; every later step chains on it, so frames are added in order. */
  private chain: Promise<void> = Promise.resolve();
  private header: Y4MHeader | undefined;
  private output: Output | undefined;
  private source: VideoSampleSource | undefined;
  private VideoSample: typeof VideoSampleClass | undefined;
  private frameIndex = 0;
  private encoded = 0;
  private ended = false;
  private failed = false;
  private readonly flow: Int32Array<SharedArrayBuffer>;
  private readonly quality: BGATranscodeQuality | undefined;
 
  public constructor(scope: WorkerScope, flow: Int32Array<SharedArrayBuffer>, quality?: BGATranscodeQuality) {
    this.scope = scope;
    this.flow = flow;
    this.quality = quality;
  }
 
  public receive(message: BGAFrameMessage): void {
    if (this.failed) return;
    switch (message.type) {
      case 'header':
        this.header = message;
        this.chain = this.start(message).catch((error: unknown) => this.fail(error));
        return;
      case 'frame':
        this.addFrame(message.data);
        return;
      case 'end':
        this.end();
        return;
    }
  }
 
  public end(): void {
    if (this.ended || this.failed) return;
    this.ended = true;
    this.chain = this.chain
      .then(async () => {
        if (this.failed) return;
        if (!this.output || this.encoded === 0) throw new Error('no frames to encode');
        await this.output.finalize();
        this.post({ type: 'done', frames: this.encoded });
      })
      .catch((error: unknown) => this.fail(error));
  }
 
  private async start(header: Y4MHeader): Promise<void> {
    const mediabunny = await import('mediabunny');
    const { width, height } = header;
    const frameRate = header.frameRateNumerator / header.frameRateDenominator;
    const quality = new mediabunny.Quality({
      bitrate: estimateBGATranscodeBitrate(width, height, frameRate, this.quality),
    });
    const encodable = await mediabunny.canEncodeVideo('avc', {
      width,
      height,
      quality,
      frameRate,
      hardwareAcceleration: 'prefer-hardware',
    });
    if (!encodable) throw new Error(`WebCodecs can't encode H.264 at ${width}x${height}`);
    const output = new mediabunny.Output({
      format: new mediabunny.Mp4OutputFormat({ fastStart: 'fragmented', minimumFragmentDuration: FRAGMENT_SECONDS }),
      target: new mediabunny.AppendOnlyStreamTarget(
        new WritableStream<Uint8Array>({
          write: (chunk) => {
            // The target may reuse its buffer: post a copy.
            const data = chunk.slice();
            this.post({ type: 'chunk', data: data.buffer }, [data.buffer]);
          },
        }),
      ),
    });
    const source = new mediabunny.VideoSampleSource({
      codec: 'avc',
      quality,
      // A key frame a second bounds how far back a seek has to decode.
      keyFrameInterval: 1,
      hardwareAcceleration: 'prefer-hardware',
    });
    output.addVideoTrack(source, { frameRate });
    await output.start();
    this.output = output;
    this.source = source;
    this.VideoSample = mediabunny.VideoSample;
  }
 
  private addFrame(data: ArrayBuffer): void {
    const header = this.header;
    if (!header || this.ended) return;
    const index = this.frameIndex++;
    const frameSeconds = header.frameRateDenominator / header.frameRateNumerator;
    this.chain = this.chain
      .then(async () => {
        if (this.failed || !this.source || !this.VideoSample) return;
        const sample = new this.VideoSample(new Uint8Array(data), {
          format: 'I420',
          codedWidth: header.width,
          codedHeight: header.height,
          timestamp: index * frameSeconds,
          duration: frameSeconds,
        });
        try {
          await this.source.add(sample);
          this.encoded++;
        } finally {
          sample.close();
        }
      })
      .catch((error: unknown) => this.fail(error))
      // Taken once the encoder has it (or has failed, when the main thread stops the decoder anyway).
      .finally(() => signalFrameTaken(this.flow));
  }
 
  private fail(error: unknown): void {
    if (this.failed) return;
    this.failed = true;
    this.post({ type: 'error', message: error instanceof Error ? error.message : String(error) });
    void this.output?.cancel().catch(() => {});
  }
 
  private post(message: BGAStreamWorkerMessage, transfer: Transferable[] = []): void {
    this.scope.postMessage(message, transfer);
  }
}