mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 15:19:34 +08:00
Microphone: kwsnd gains a capture substream fed by the host (SDK option soundIn, the page's Microphone setting and ?mic=0, the CLI's --sound-in-wav and --sound-in). Camera: kwcam is a V4L2 capture device (/dev/video0) fed by the host: I420, NV12 and YUYV at 320x240 to 1280x720, and H.264 encoded by the host's WebCodecs encoder where it has one, with bitrate, GOP and key frame controls (SDK option camera, the page's Camera setting and ?cam=0, the CLI's --camera). The base's rc writes the udev database entry of /dev/video*, so programs that list cameras through libudev find it. kwva (protocol 5): an encode whose input surface is a camera buffer's dma-buf names the frame to the host instead of sending its pixels, and pixels an app writes into an exported surface reach the host (put).
656 lines
25 KiB
TypeScript
656 lines
25 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// Worker mode, the owner's side of the display, the evdev devices and the
|
|
// sound card (WorkerKernel, worker-host.ts). The devices live in the worker
|
|
// (worker-serve.ts); memory is not shared, so everything crosses as
|
|
// messages (worker-protocol.ts), which the worker handles only when the
|
|
// kernel yields (idle, or every 20 ms while user code runs).
|
|
//
|
|
// - Evdev (RemoteEvdev): the same methods as InputHost. Each call is
|
|
// forwarded in order to the worker's InputHost, which applies the
|
|
// queueing rules (presses only while open, releases of keys the guest saw,
|
|
// releases at close). The return values come from a mirror here of the
|
|
// open mask ("input-state") and of the keys and buttons held, so callers
|
|
// can route keys between the guest and a terminal synchronously. Calls are
|
|
// batched: one message per microtask, but a batch of nothing but motion
|
|
// and wheel waits MOTION_BATCH_MS for more (a key or button press sends
|
|
// it at once). Nothing is merged or reordered: the guest gets every call
|
|
// as inline (merging moves was tried and lost intermediate positions).
|
|
// - Frames (FrameClient): a "latest frame" model. The owner gives credits
|
|
// ("frame-want", at most FRAME_CREDITS outstanding), each with a returned
|
|
// frame buffer to reuse; the worker answers one credit once the picture
|
|
// may have changed (the kernel ran since the last frame), with the raw
|
|
// XRGB8888 rows transferred. RGBA conversion happens here, once per
|
|
// frame() call, into the caller's buffer: the worker thread only copies.
|
|
// - With the shared transport (worker-shared.ts) evdev calls go straight
|
|
// into a shared ring (no batching: there is no message to save), frame
|
|
// requests into the control block, and frames come back through a shared
|
|
// triple buffer that frame() reads at any time; readFrame() is answered
|
|
// by a "frame-ready" message. Without a frame buffer from the worker,
|
|
// frames fall back to the credits above.
|
|
// - Sound (SoundBridge): with the shared transport the PCM comes through
|
|
// the sound ring (sound-ring.ts), drained here into the sink's onChunk()
|
|
// on a "sound-wake" message, or handed to the sink's onRing() to read
|
|
// itself; the sink's answer (frames queued at the sink) goes back through
|
|
// the ring. Without it, chunks come as messages with a sequence number,
|
|
// and the answer goes back throttled to one per SOUND_REPORT_MS, with
|
|
// the time it was measured; the worker's answer to the kernel is that
|
|
// report, less what played since, plus the frames sent after it
|
|
// (worker-serve.ts).
|
|
// - Capture (SoundInBridge): the kernel's side (the reader, and its pacing)
|
|
// is the worker's (worker-serve.ts). A source with onRing gets the
|
|
// capture ring (sound-in-ring.ts) and writes it from wherever it runs (a
|
|
// microphone's AudioWorklet). A source with read() is pulled here every
|
|
// CAPTURE_PULL_MS while the guest's stream runs: into the ring up to
|
|
// CAPTURE_LEAD_S ahead of what the kernel has read, or without the ring
|
|
// as "sound-in-pcm" messages, paced by this thread's clock and CAPTURE_LEAD_S
|
|
// ahead of it (the worker drops what runs too far ahead).
|
|
// - Camera (CameraBridge): the worker's kernel asks for frames at the
|
|
// stream's rate and reports the stream ("camera-state", numbered); the
|
|
// source's read() is pulled here at that rate too, into the frame
|
|
// mailbox (camera.ts CameraFrames, the latest frame, read at any time in
|
|
// the worker's import) or, without shared memory, as "camera-frame"
|
|
// messages (the worker keeps the latest). The two clocks are not locked,
|
|
// so the guest may get a frame twice or miss one, as from a real camera.
|
|
// A compressed stream instead gets a sink at its start: the source's
|
|
// chunks go into the chunk ring (camera-chunks.ts, with the drop rules)
|
|
// as they come, or as "camera-chunk" messages (the worker's queue applies
|
|
// them); "camera-key" and "camera-controls" from the worker reach the
|
|
// source's keyFrame() and onControls().
|
|
|
|
import { xrgbToRgba, type DisplayFrame, type DisplayMode } from "./display.ts";
|
|
import { INPUT_KEYBOARD, INPUT_MOUSE, INPUT_TABLET, type Evdev, type InputState } from "./input.ts";
|
|
import type { SoundChunk, SoundEvent, SoundFormat, SoundSink, SoundState } from "./sound.ts";
|
|
import { SoundRingReader } from "./sound-ring.ts";
|
|
import { SoundInRingWriter } from "./sound-in-ring.ts";
|
|
import type { SoundSource } from "./sound.ts";
|
|
import { CameraFramesWriter, chunkAt, type CameraChunkSink, type CameraFormat, type CameraFrameStore, type CameraSource } from "./camera.ts";
|
|
import { CHUNK_CLOCK, ChunkRingWriter } from "./camera-chunks.ts";
|
|
import { cameraCompressed } from "./camera-pixels.ts";
|
|
import { EVDEV_OP, type ToWorker } from "./worker-protocol.ts";
|
|
import type { SharedOwner } from "./worker-shared.ts";
|
|
|
|
/** How long a batch of nothing but pointer motion and wheel clicks waits for more before it is sent. */
|
|
const MOTION_BATCH_MS = 8;
|
|
/** Frame credits outstanding at most: two lets the worker send a frame at every yield without waiting for this thread. */
|
|
const FRAME_CREDITS = 2;
|
|
/** At most one sink report per this many ms. */
|
|
const SOUND_REPORT_MS = 15;
|
|
/** How often a capture source with read() is pulled while the guest records. */
|
|
const CAPTURE_PULL_MS = 10;
|
|
/** How far ahead of the guest's reads it is kept. */
|
|
const CAPTURE_LEAD_S = 0.1;
|
|
|
|
/** performance.now() on a clock both threads share (ms). */
|
|
export const sharedNow = (): number => performance.timeOrigin + performance.now();
|
|
|
|
/** InputHost's surface, forwarded to the worker (see the top of this file). */
|
|
export class RemoteEvdev implements Evdev {
|
|
mask = 0;
|
|
#post: (msg: ToWorker) => void;
|
|
#ops: number[] = [];
|
|
#urgent = false;
|
|
#timer: ReturnType<typeof setTimeout> | undefined;
|
|
#micro = false;
|
|
#keys = new Set<number>();
|
|
#buttons = new Set<number>();
|
|
#tabletButtons = new Set<number>();
|
|
#shared: SharedOwner | undefined;
|
|
|
|
constructor(post: (msg: ToWorker) => void, shared?: SharedOwner) {
|
|
this.#post = post;
|
|
this.#shared = shared;
|
|
}
|
|
|
|
get keyboardOpen(): boolean { return !!(this.mask & (1 << INPUT_KEYBOARD)); }
|
|
get mouseOpen(): boolean { return !!(this.mask & (1 << INPUT_MOUSE)); }
|
|
get tabletOpen(): boolean { return !!(this.mask & (1 << INPUT_TABLET)); }
|
|
get state(): InputState {
|
|
return { keyboard: this.keyboardOpen, mouse: this.mouseOpen, tablet: this.tabletOpen };
|
|
}
|
|
|
|
/** @internal The worker reported the open mask: keys on a device that closed were released there. */
|
|
setMask(mask: number): void {
|
|
const closed = this.mask & ~mask;
|
|
this.mask = mask >>> 0;
|
|
if (closed & (1 << INPUT_KEYBOARD)) this.#keys.clear();
|
|
if (closed & (1 << INPUT_MOUSE)) this.#buttons.clear();
|
|
if (closed & (1 << INPUT_TABLET)) this.#tabletButtons.clear();
|
|
}
|
|
|
|
#held(set: Set<number>, open: boolean, op: number, code: number, down: boolean): boolean {
|
|
if (down) {
|
|
if (!open) return false;
|
|
if (!set.has(code)) {
|
|
set.add(code);
|
|
this.#queue(op, code, 1);
|
|
}
|
|
return true;
|
|
}
|
|
if (!set.delete(code)) return false;
|
|
this.#queue(op, code, 0);
|
|
return true;
|
|
}
|
|
|
|
key(code: number, down: boolean): boolean {
|
|
return this.#held(this.#keys, this.keyboardOpen, EVDEV_OP.key, code, down);
|
|
}
|
|
|
|
button(code: number, down: boolean): boolean {
|
|
return this.#held(this.#buttons, this.mouseOpen, EVDEV_OP.button, code, down);
|
|
}
|
|
|
|
tabletButton(code: number, down: boolean): boolean {
|
|
return this.#held(this.#tabletButtons, this.tabletOpen, EVDEV_OP.tabletButton, code, down);
|
|
}
|
|
|
|
move(dx: number, dy: number): boolean {
|
|
if (!this.mouseOpen) return false;
|
|
this.#queue(EVDEV_OP.move, dx, dy, true);
|
|
return true;
|
|
}
|
|
|
|
wheel(clicks: number): boolean {
|
|
if (!this.mouseOpen) return false;
|
|
if (Math.round(clicks)) this.#queue(EVDEV_OP.wheel, clicks, 0, true);
|
|
return true;
|
|
}
|
|
|
|
moveTo(x: number, y: number): boolean {
|
|
if (!this.tabletOpen) return false;
|
|
this.#queue(EVDEV_OP.moveTo, x, y, true);
|
|
return true;
|
|
}
|
|
|
|
tabletWheel(clicks: number): boolean {
|
|
if (!this.tabletOpen) return false;
|
|
if (Math.round(clicks)) this.#queue(EVDEV_OP.tabletWheel, clicks, 0, true);
|
|
return true;
|
|
}
|
|
|
|
releaseAll(): void {
|
|
for (const c of [...this.#keys]) this.key(c, false);
|
|
for (const b of [...this.#buttons]) this.button(b, false);
|
|
for (const b of [...this.#tabletButtons]) this.tabletButton(b, false);
|
|
}
|
|
|
|
#queue(op: number, a: number, b: number, motion = false): void {
|
|
if (this.#shared) return this.#shared.evdev(op, a, b);
|
|
this.#ops.push(op, a, b);
|
|
if (!motion) this.#urgent = true;
|
|
if (this.#urgent) {
|
|
if (!this.#micro) {
|
|
this.#micro = true;
|
|
queueMicrotask(() => this.flush());
|
|
}
|
|
} else this.#timer ??= setTimeout(() => this.flush(), MOTION_BATCH_MS);
|
|
}
|
|
|
|
/** @internal Send what is queued now. */
|
|
flush(): void {
|
|
clearTimeout(this.#timer);
|
|
this.#timer = undefined;
|
|
this.#micro = false;
|
|
this.#urgent = false;
|
|
if (!this.#ops.length) return;
|
|
const ops = this.#ops;
|
|
this.#ops = [];
|
|
this.#post({ t: "evdev", ops });
|
|
}
|
|
|
|
/** @internal The kernel exited: drop what is queued. */
|
|
close(): void {
|
|
clearTimeout(this.#timer);
|
|
this.#ops = [];
|
|
}
|
|
}
|
|
|
|
interface RawFrame {
|
|
mode: DisplayMode;
|
|
data: Uint8Array;
|
|
}
|
|
|
|
/** The frame credits, the latest frame and the buffers to return (see the top of this file). */
|
|
export class FrameClient {
|
|
/** The mode while a program has the display open, as the worker last reported it. */
|
|
mode: DisplayMode | null = null;
|
|
#seq = 0;
|
|
#post: (msg: ToWorker, transfer?: ArrayBuffer[]) => void;
|
|
#shared: SharedOwner | undefined;
|
|
#latest: RawFrame | null = null;
|
|
#spare: ArrayBuffer[] = [];
|
|
#outstanding = 0;
|
|
#nextId = 1;
|
|
#waiters = new Map<number, (ok: boolean) => void>();
|
|
#closed = false;
|
|
|
|
constructor(post: (msg: ToWorker, transfer?: ArrayBuffer[]) => void, shared?: SharedOwner) {
|
|
this.#post = post;
|
|
this.#shared = shared;
|
|
}
|
|
|
|
/** Frames come through the shared triple buffer (else by messages). */
|
|
get #viaShared(): boolean {
|
|
return !!this.#shared?.frames.ready;
|
|
}
|
|
|
|
/** Counts frames with new pixels (received, or published in the shared buffer). */
|
|
get seq(): number {
|
|
return this.#viaShared ? this.#shared!.frames.seq : this.#seq;
|
|
}
|
|
|
|
#want(now: boolean): number {
|
|
const id = this.#nextId++;
|
|
const buf = this.#spare.pop();
|
|
this.#outstanding++;
|
|
this.#post(buf ? { t: "frame-want", id, buf, now } : { t: "frame-want", id, now }, buf ? [buf] : []);
|
|
return id;
|
|
}
|
|
|
|
/** Ask for the next frame, unless enough are asked for already or nothing has the display. */
|
|
request(): void {
|
|
if (this.#closed || !this.mode) return;
|
|
if (this.#viaShared) return this.#shared!.wantFrame();
|
|
if (this.#outstanding >= FRAME_CREDITS) return;
|
|
this.#want(false);
|
|
}
|
|
|
|
/** The latest frame as RGBA (null while detached or before the first arrives); asks for the next. */
|
|
frame(out?: Uint8ClampedArray): DisplayFrame | null {
|
|
this.request();
|
|
if (this.#viaShared) return this.mode ? this.#shared!.frames.frame(out) : null;
|
|
const f = this.#latest;
|
|
if (!f || !this.mode) return null;
|
|
return { width: f.mode.width, height: f.mode.height, data: xrgbToRgba(f.data, f.mode.width, f.mode.height, out) };
|
|
}
|
|
|
|
/** A fresh frame: a round trip to the worker, answered at its next chance even if unchanged. */
|
|
read(out?: Uint8ClampedArray): Promise<DisplayFrame | null> {
|
|
if (this.#closed) return Promise.resolve(this.frame(out));
|
|
if (this.#viaShared) {
|
|
const id = this.#nextId++;
|
|
return new Promise((resolve) => {
|
|
this.#waiters.set(id, () => resolve(this.mode && this.#viaShared ? this.#shared!.frames.frame(out) : null));
|
|
this.#shared!.wantFrameNow(id);
|
|
});
|
|
}
|
|
return new Promise((resolve) => {
|
|
this.#waiters.set(this.#want(true), () => resolve(this.#latest && this.mode ? this.frame(out) : null));
|
|
});
|
|
}
|
|
|
|
/** @internal A "frame-buffer" message: the shared buffer for the next mode (null: frames by messages). */
|
|
adoptBuffer(buffer: SharedArrayBuffer | null, cap: number): void {
|
|
this.#shared?.frames.adopt(buffer, cap);
|
|
}
|
|
|
|
/** @internal A "frame-ready" message: readFrame() requests up to `id` are answered (the mode came in "display" before it). */
|
|
ready(id: number): void {
|
|
for (const [w, resolve] of this.#waiters) {
|
|
if (w > id) continue;
|
|
this.#waiters.delete(w);
|
|
resolve(true);
|
|
}
|
|
}
|
|
|
|
/** @internal A "display" message. */
|
|
setMode(mode: DisplayMode | null): void {
|
|
this.mode = mode;
|
|
if (!mode) this.#drop();
|
|
}
|
|
|
|
#drop(): void {
|
|
if (this.#latest) {
|
|
this.#keep(this.#latest.data.buffer as ArrayBuffer);
|
|
this.#latest = null;
|
|
}
|
|
}
|
|
|
|
#keep(buf: ArrayBuffer): void {
|
|
if (this.#spare.length < FRAME_CREDITS) this.#spare.push(buf);
|
|
}
|
|
|
|
/** @internal A "frame" message. */
|
|
receive(id: number, mode: DisplayMode | null, data: Uint8Array | null): void {
|
|
this.#outstanding = Math.max(0, this.#outstanding - 1);
|
|
if (data && mode) {
|
|
this.#drop();
|
|
this.#latest = { mode, data };
|
|
this.#seq++;
|
|
} else if (!mode) this.#drop();
|
|
const w = this.#waiters.get(id);
|
|
this.#waiters.delete(id);
|
|
w?.(true);
|
|
}
|
|
|
|
/** @internal The kernel exited: answer what waits with what is here. */
|
|
close(): void {
|
|
this.#closed = true;
|
|
for (const w of this.#waiters.values()) w(false);
|
|
this.#waiters.clear();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Feeds the owner's SoundSink from sound messages or the shared sound ring.
|
|
* With a ring read here, a state change is delivered where its record sits
|
|
* in the ring, between the chunks around it, not when its message arrives.
|
|
*/
|
|
export class SoundBridge {
|
|
state: SoundState = { open: false, playing: false, format: null };
|
|
#sink: SoundSink;
|
|
#post: (msg: ToWorker) => void;
|
|
#log: (msg: string) => void;
|
|
#lastReport = -Infinity;
|
|
#reader: SoundRingReader | undefined;
|
|
#ring: SharedArrayBuffer | undefined;
|
|
#ringDelivered = false;
|
|
/** Ring read here: state messages whose record the drain has not reached, oldest first. */
|
|
#states: Array<{ n: number; state: SoundState; event: SoundEvent }> = [];
|
|
/** The number of the latest state message. */
|
|
#arrived = 0;
|
|
#dropped = 0;
|
|
|
|
constructor(sink: SoundSink, post: (msg: ToWorker) => void, log: (msg: string) => void, ring?: SharedArrayBuffer) {
|
|
this.#sink = sink;
|
|
this.#post = post;
|
|
this.#log = log;
|
|
if (ring && sink.onRing) this.#ring = ring;
|
|
else if (ring && sink.onChunk) this.#reader = new SoundRingReader(ring);
|
|
if (ring) this.#dropped = new SoundRingReader(ring).droppedBytes();
|
|
}
|
|
|
|
get playing(): boolean {
|
|
return this.state.playing;
|
|
}
|
|
|
|
/** @internal Hand the shared sound ring to sink.onRing once the worker starts. */
|
|
startRing(): void {
|
|
if (!this.#ring || this.#ringDelivered) return;
|
|
this.#ringDelivered = true;
|
|
try { this.#sink.onRing?.(this.#ring); } catch (e) { this.#log(`[sound] onRing failed: ${e}`); }
|
|
}
|
|
|
|
/** @internal Drain the shared sound ring into sink.onChunk, and the state changes in its order. */
|
|
onWake(): void {
|
|
const reader = this.#reader;
|
|
if (!reader) return;
|
|
for (;;) {
|
|
const n = reader.nextEvent();
|
|
if (n >= 0) {
|
|
// Its message has not arrived (messages are slower than the ring): it drains again.
|
|
if (n > this.#arrived) break;
|
|
this.#deliverStates(n);
|
|
reader.readNext();
|
|
continue;
|
|
}
|
|
const rec = reader.readNext();
|
|
if (!rec) {
|
|
// Empty: a message still waiting had no room for its record.
|
|
this.#deliverStates(this.#arrived);
|
|
break;
|
|
}
|
|
const chunk: SoundChunk = { format: rec.format, data: rec.data, frames: Math.floor(rec.data.length / rec.format.frameBytes) };
|
|
let queued: number | void = 0;
|
|
try { queued = this.#sink.onChunk?.(chunk); } catch (e) { this.#log(`[sound] onChunk failed: ${e}`); }
|
|
reader.setQueuedFrames(queued || 0);
|
|
}
|
|
this.#reportDrops(reader.droppedBytes());
|
|
}
|
|
|
|
#deliverStates(upTo: number): void {
|
|
while (this.#states.length && this.#states[0]!.n <= upTo) {
|
|
const s = this.#states.shift()!;
|
|
this.#deliver(s.state, s.event);
|
|
}
|
|
}
|
|
|
|
#deliver(state: SoundState, event: SoundEvent): void {
|
|
this.state = state;
|
|
if (event === "start") this.#lastReport = -Infinity;
|
|
try { this.#sink.onState?.(state, event); } catch (e) { this.#log(`[sound] onState failed: ${e}`); }
|
|
}
|
|
|
|
#reportDrops(dropped: number): void {
|
|
if (dropped === this.#dropped) return;
|
|
this.#log(`[sound] the sound ring was full: ${(dropped - this.#dropped) >>> 0} bytes of PCM dropped`);
|
|
this.#dropped = dropped;
|
|
}
|
|
|
|
/** @internal A "sound-state" message (`n`: its number, as on its record in the ring). */
|
|
onState(state: SoundState, event: SoundEvent, n: number): void {
|
|
this.startRing();
|
|
if (this.#reader) {
|
|
this.#states.push({ n, state, event });
|
|
this.#arrived = n;
|
|
this.onWake();
|
|
return;
|
|
}
|
|
if (this.#ring) this.#reportDrops(new SoundRingReader(this.#ring).droppedBytes());
|
|
this.#deliver(state, event);
|
|
}
|
|
|
|
/** @internal A "sound-chunk" message. */
|
|
onChunk(seq: number, format: SoundFormat, data: Uint8Array): void {
|
|
const chunk: SoundChunk = { format, data, frames: Math.floor(data.length / format.frameBytes) };
|
|
let queued: number | void = 0;
|
|
try { queued = this.#sink.onChunk?.(chunk); } catch (e) { this.#log(`[sound] onChunk failed: ${e}`); }
|
|
const now = sharedNow();
|
|
if (now - this.#lastReport < SOUND_REPORT_MS) return;
|
|
this.#lastReport = now;
|
|
this.#post({ t: "sound-queued", seq, frames: Math.max(0, Math.round(queued || 0)), at: now });
|
|
}
|
|
}
|
|
|
|
/** Feeds the worker's capture stream from the owner's SoundSource (see the top of this file). */
|
|
export class SoundInBridge {
|
|
state: SoundState = { open: false, playing: false, format: null };
|
|
#source: SoundSource;
|
|
#post: (msg: ToWorker, transfer?: ArrayBuffer[]) => void;
|
|
#log: (msg: string) => void;
|
|
#ring: SharedArrayBuffer | undefined;
|
|
#writer: SoundInRingWriter | undefined;
|
|
#ringDelivered = false;
|
|
#timer: ReturnType<typeof setInterval> | undefined;
|
|
/** Without the ring: when the stream started (sharedNow) and samples sent since. */
|
|
#t0 = 0;
|
|
#sent = 0;
|
|
#scratch = new Float32Array(4096);
|
|
|
|
constructor(source: SoundSource, post: (msg: ToWorker, transfer?: ArrayBuffer[]) => void, log: (msg: string) => void, ring?: SharedArrayBuffer) {
|
|
this.#source = source;
|
|
this.#post = post;
|
|
this.#log = log;
|
|
this.#ring = ring;
|
|
if (ring && !source.onRing && source.read) this.#writer = new SoundInRingWriter(ring);
|
|
}
|
|
|
|
/** @internal Hand the capture ring to source.onRing once the worker starts. */
|
|
startRing(): void {
|
|
if (!this.#ring || this.#ringDelivered || !this.#source.onRing) return;
|
|
this.#ringDelivered = true;
|
|
try { this.#source.onRing(this.#ring); } catch (e) { this.#log(`[sound] capture onRing failed: ${e}`); }
|
|
}
|
|
|
|
/** @internal A "sound-in-state" message. */
|
|
onState(state: SoundState, event: SoundEvent): void {
|
|
this.startRing();
|
|
this.state = state;
|
|
try { this.#source.onState?.(state, event); } catch (e) { this.#log(`[sound] capture onState failed: ${e}`); }
|
|
const pulls = this.#source.read && !(this.#ring && this.#source.onRing);
|
|
if (event === "start" && state.format && pulls) {
|
|
this.#t0 = sharedNow();
|
|
this.#sent = 0;
|
|
this.#pull();
|
|
this.#timer ??= setInterval(() => this.#pull(), CAPTURE_PULL_MS);
|
|
} else if (event !== "start") this.#stopPulling();
|
|
}
|
|
|
|
#pull(): void {
|
|
const f = this.state.format;
|
|
if (!f || !this.state.playing) return;
|
|
const rate = f.rate, lead = Math.ceil(CAPTURE_LEAD_S * rate);
|
|
const want = this.#writer
|
|
? lead - this.#writer.level()
|
|
: Math.floor((sharedNow() - this.#t0) * rate / 1000) + lead - this.#sent;
|
|
if (want <= 0) return;
|
|
const out = this.#writer ? (this.#scratch.length >= want ? this.#scratch : (this.#scratch = new Float32Array(want))).subarray(0, want) : new Float32Array(want);
|
|
let n = 0;
|
|
try { n = Math.max(0, Math.min(want, Math.floor(Number(this.#source.read!(out, rate)) || 0))); } catch (e) { this.#log(`[sound] capture read failed: ${e}`); }
|
|
if (this.#writer) {
|
|
if (n) this.#writer.write(out, n);
|
|
return;
|
|
}
|
|
// The worker's clock paces it: what the source did not have is not asked again.
|
|
this.#sent += want;
|
|
if (n) {
|
|
const data = n === want ? out : out.slice(0, n);
|
|
this.#post({ t: "sound-in-pcm", data, rate }, [data.buffer as ArrayBuffer]);
|
|
}
|
|
}
|
|
|
|
#stopPulling(): void {
|
|
if (this.#timer !== undefined) clearInterval(this.#timer);
|
|
this.#timer = undefined;
|
|
}
|
|
|
|
/** @internal The kernel ended: a capture still open closes at the source (which lets its microphone go). */
|
|
close(): void {
|
|
this.#stopPulling();
|
|
if (!this.state.open) return;
|
|
this.state = { open: false, playing: false, format: null };
|
|
try { this.#source.onState?.(this.state, "close"); } catch (e) { this.#log(`[sound] capture onState failed: ${e}`); }
|
|
}
|
|
}
|
|
|
|
/** Feeds the worker's camera from the owner's CameraSource (see the top of this file). */
|
|
export class CameraBridge {
|
|
format: CameraFormat | null = null;
|
|
#source: CameraSource;
|
|
#post: (msg: ToWorker, transfer?: ArrayBuffer[]) => void;
|
|
#log: (msg: string) => void;
|
|
#writer: CameraFramesWriter | undefined;
|
|
#gen = 0;
|
|
#timer: ReturnType<typeof setInterval> | undefined;
|
|
#warned = false;
|
|
#store: CameraFrameStore | undefined;
|
|
#chunks: ChunkRingWriter | undefined;
|
|
|
|
/**
|
|
* `store`: where the frames published are kept for the VA encoder (on this thread, with the VA host); each frame then carries its id.
|
|
* `chunks`: the chunk ring for compressed streams (else "camera-chunk" messages).
|
|
*/
|
|
constructor(source: CameraSource, post: (msg: ToWorker, transfer?: ArrayBuffer[]) => void, log: (msg: string) => void, frames?: SharedArrayBuffer, store?: CameraFrameStore, chunks?: SharedArrayBuffer) {
|
|
this.#source = source;
|
|
this.#post = post;
|
|
this.#log = log;
|
|
this.#store = store;
|
|
if (frames) this.#writer = new CameraFramesWriter(frames);
|
|
if (chunks) this.#chunks = new ChunkRingWriter(chunks);
|
|
}
|
|
|
|
/** A compressed stream's sink: into the ring, or posted (the worker's queue drops by the same rules). */
|
|
#sink(format: CameraFormat, gen: number): CameraChunkSink {
|
|
const wantKey = () => {
|
|
try { this.#source.keyFrame?.(); } catch (e) { this.#log(`[camera] keyFrame failed: ${e}`); }
|
|
};
|
|
const ring = this.#chunks;
|
|
if (ring) {
|
|
ring.start(gen, format.bytes, wantKey);
|
|
return { push: (data, key, at) => this.#gen === gen && this.format !== null && data instanceof Uint8Array && ring.push(data, !!key, chunkAt(at)) };
|
|
}
|
|
return {
|
|
push: (data, key, at) => {
|
|
if (this.#gen !== gen || !this.format || !(data instanceof Uint8Array)) return false;
|
|
if (data.length > format.bytes || !data.length) {
|
|
wantKey();
|
|
return false;
|
|
}
|
|
const copy = data.slice();
|
|
this.#post({ t: "camera-chunk", gen, data: copy, key: !!key, at: chunkAt(at) ?? CHUNK_CLOCK() }, [copy.buffer as ArrayBuffer]);
|
|
return true;
|
|
},
|
|
};
|
|
}
|
|
|
|
/** @internal A "camera-key" message: the worker wants a key frame of the running stream. */
|
|
onKey(gen: number): void {
|
|
if (gen !== this.#gen || !this.format) return;
|
|
try { this.#source.keyFrame?.(); } catch (e) { this.#log(`[camera] keyFrame failed: ${e}`); }
|
|
}
|
|
|
|
/** @internal A "camera-controls" message. */
|
|
onControls(format: CameraFormat, gen: number): void {
|
|
if (gen !== this.#gen || !this.format) return;
|
|
this.format = { ...format };
|
|
try { this.#source.onControls?.({ ...format }); } catch (e) { this.#log(`[camera] onControls failed: ${e}`); }
|
|
}
|
|
|
|
/** The frame's id from the store (never in the way of the frame). */
|
|
#id(data: Uint8Array, f: CameraFormat): Uint8Array | null {
|
|
if (!this.#store) return null;
|
|
try {
|
|
const src = this.#source;
|
|
return this.#store.add(data, f, src.lastFrame ? () => src.lastFrame!() : undefined);
|
|
} catch (e) {
|
|
this.#log(`[camera] frame id: ${e}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** @internal A "camera-state" message. */
|
|
onState(format: CameraFormat | null, gen: number): void {
|
|
this.#stop();
|
|
this.#store?.stop();
|
|
this.#chunks?.stop();
|
|
this.format = format;
|
|
this.#gen = gen;
|
|
const compressed = !!format && cameraCompressed(format.pixelFormat);
|
|
const sink = compressed ? this.#sink(format!, gen) : undefined;
|
|
try { this.#source.onStream?.(format && { ...format }, sink); } catch (e) { this.#log(`[camera] onStream failed: ${e}`); }
|
|
if (format && !compressed && this.#source.read) {
|
|
this.#pull();
|
|
this.#timer = setInterval(() => this.#pull(), 1000 / format.fps);
|
|
}
|
|
}
|
|
|
|
#pull(): void {
|
|
const f = this.format;
|
|
if (!f) return;
|
|
let ok = false;
|
|
try {
|
|
if (this.#writer) {
|
|
if (f.bytes > this.#writer.max) {
|
|
if (!this.#warned) this.#log(`[camera] a ${f.bytes}-byte frame does not fit the frame mailbox`);
|
|
this.#warned = true;
|
|
return;
|
|
}
|
|
const slot = this.#writer.slot(f.bytes);
|
|
ok = !!this.#source.read!(slot, f);
|
|
if (ok) this.#writer.publish(this.#gen, f.bytes, this.#id(slot, f));
|
|
return;
|
|
}
|
|
const data = new Uint8Array(f.bytes);
|
|
ok = !!this.#source.read!(data, f);
|
|
const id = ok ? this.#id(data, f) : null;
|
|
if (ok) this.#post({ t: "camera-frame", gen: this.#gen, data, ...(id ? { id } : {}) }, [data.buffer as ArrayBuffer]);
|
|
} catch (e) { this.#log(`[camera] read failed: ${e}`); }
|
|
}
|
|
|
|
#stop(): void {
|
|
if (this.#timer !== undefined) clearInterval(this.#timer);
|
|
this.#timer = undefined;
|
|
}
|
|
|
|
/** @internal The kernel ended: a stream still running stops at the source too. */
|
|
close(): void {
|
|
this.#stop();
|
|
this.#store?.stop();
|
|
this.#chunks?.stop();
|
|
if (this.format) {
|
|
this.format = null;
|
|
try { this.#source.onStream?.(null); } catch (e) { this.#log(`[camera] onStream failed: ${e}`); }
|
|
}
|
|
}
|
|
}
|