Files
unkernel/js/node/camera.ts
Pooya Parsa 1b229902b2 Add a guest microphone and camera: kwsnd capture, kwcam (V4L2, raw and host-encoded H.264), and camera frames named to the kwva encoder
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).
2026-11-29 10:23:01 +00:00

40 lines
1.7 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// The CLI's camera (--camera): the synthetic source of core/camera.ts (a
// test pattern with a frame counter), with each stream logged; with
// --camera=h264 it also offers H.264 (core/camera-h264.ts H264Pattern: a
// stream made without an encoder, which Node does not have).
import { patternSource, type CameraFormat, type CameraSource, type CameraStats } from "../core/camera.ts";
/**
* --camera: a synthetic camera (scrolling colour bars and a frame counter);
* "h264" also H.264; without it, no camera. Logs each start and stop;
* close() prints the stats it is given (--stats).
*/
export function cliCamera(mode: boolean | "h264", log: (msg: string) => void): {
option: false | CameraSource;
close: (stats?: CameraStats) => void;
} {
if (!mode) return { option: false, close: () => {} };
const src = patternSource(undefined, { h264: mode === "h264" });
const describe = (f: CameraFormat) => `${f.pixelFormat} ${f.width}x${f.height} at ${f.fps} fps${f.bitrate ? `, ${f.bitrate} bit/s, a key frame every ${f.keyInterval} frames` : ""}`;
return {
option: {
...src,
onStream: (f, chunks) => {
log(f ? `[host] camera: ${describe(f)}` : "[host] camera: stopped");
src.onStream?.(f, chunks);
},
onControls: (f) => {
log(`[host] camera: controls: ${describe(f)}`);
src.onControls?.(f);
},
},
close: (stats) => {
if (!stats) return;
const c = stats.chunks;
log(`[host] camera: ${stats.starts} starts, ${stats.stops} stops, ${stats.frames} frames, ${stats.blackFrames} of them black${c ? `; H.264: ${c.delivered} chunks delivered, ${c.dropped} dropped` : ""}`);
},
};
}