mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-05 16:19:36 +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).
41 lines
2.0 KiB
TypeScript
41 lines
2.0 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// getUserMedia() for the guest's devices that read the user's microphone
|
|
// or camera (js/web/audio-in.ts, js/web/camera.ts): asked for only when a guest program
|
|
// starts using the device, with one message per way it can fail, and the
|
|
// tracks stopped when the guest is done (the browser's indicator goes off).
|
|
|
|
export type UserMediaResult =
|
|
| { stream: MediaStream }
|
|
/** unavailable: no getUserMedia here (an insecure context) or no such device; denied: the user or a policy refused; failed: anything else. */
|
|
| { error: "unavailable" | "denied" | "failed"; message: string };
|
|
|
|
/**
|
|
* Ask for `constraints`. `what` names the device in the messages (e.g.
|
|
* "microphone"). Never rejects.
|
|
*/
|
|
export async function requestUserMedia(constraints: MediaStreamConstraints, what: string): Promise<UserMediaResult> {
|
|
const md = (globalThis.navigator as Partial<Navigator> | undefined)?.mediaDevices;
|
|
if (!md?.getUserMedia) {
|
|
return { error: "unavailable", message: `${what}: this browser offers no media devices to the page (getUserMedia needs a secure context: https or localhost)` };
|
|
}
|
|
try {
|
|
return { stream: await md.getUserMedia(constraints) };
|
|
} catch (e) {
|
|
const name = (e as { name?: string })?.name ?? "";
|
|
const msg = (e as { message?: string })?.message || String(e);
|
|
if (name === "NotAllowedError" || name === "SecurityError" || name === "PermissionDeniedError")
|
|
return { error: "denied", message: `${what}: permission denied (${msg}); allow it in the site's settings, then start again` };
|
|
if (name === "NotFoundError" || name === "OverconstrainedError" || name === "DevicesNotFoundError")
|
|
return { error: "unavailable", message: `${what}: none found (${msg})` };
|
|
return { error: "failed", message: `${what}: ${name || "error"}: ${msg}` };
|
|
}
|
|
}
|
|
|
|
/** Stop every track of `stream` (null: nothing). */
|
|
export function stopUserMedia(stream: MediaStream | null | undefined): void {
|
|
for (const t of stream?.getTracks() ?? []) {
|
|
try { t.stop(); } catch {}
|
|
}
|
|
}
|