Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// The guest's sound, microphone and camera on the page, for the Sound and// media applet (components/applets/Media.tsx). boot.ts makes the SDK's sink// and sources here, once per load and only for the devices this boot has:// soundOut() (js/web/audio.ts, Web Audio), micIn() (js/web/audio-in.ts) and// cameraIn() (js/web/camera.ts). media$ follows what they report, as it// happens: the sink's state and `playing` (onChange, onPlaying: the guest's// stream runs with more than silence), and each source's state (onChange:// "running" while the host stream is live and the guest gets it, "asking"// while the browser asks, "denied" or "unavailable" while a program has the// device open and gets silence or black frames; "idle" once it lets go).// - Mute (setMuted()) silences the page's output; the guest plays on. Kept// as `kw.sound-muted` (prefs.ts) and applied from the load's first sound.// - The browser's permission for the microphone and the camera// (watchPermissions(), from the panel: the Permissions API where it knows// the names, else null) and allow(), called from a click: for a program// that has the device open and was refused, the source asks again// (retry()); with no program on it and the browser still to ask, the page// asks now and lets the device go at once, so that a program later is not// held up by the question.// Nothing here polls: the applet reads the SDK's figures (stats(), the// kernel's vm.sound, vm.soundIn, vm.camera) when it renders.
import { useSyncExternalStore } from "react";import { capitalize } from "./format.ts";import { KW } from "./kw.ts";import { store } from "./prefs.ts";import { createAudioIn, createAudioOut, createCamera } from "./sdk.ts";import { createStore } from "./store.ts";import type { AudioIn, AudioInState } from "../../js/web/audio-in.ts";import type { AudioOut, AudioOutState } from "../../js/web/audio.ts";import type { Camera, CameraOptions, CameraState } from "../../js/web/camera.ts";import { requestUserMedia, stopUserMedia } from "../../js/web/user-media.ts";
/** The browser's permission for a device: granted, denied (blocked for the site), prompt (it will ask), or null (not known here). */export type MediaPermission = "granted" | "denied" | "prompt" | null;
export interface MediaView { /** The sound card's output (null: no sound card this boot). */ sound: AudioOutState | null; /** The guest plays sound now (AudioOut.playing). */ playing: boolean; muted: boolean; /** The microphone (null: no capture this boot). */ mic: AudioInState | null; /** The camera (null: no camera this boot). */ cam: CameraState | null; micPerm: MediaPermission; camPerm: MediaPermission; /** allow() is asking for it with no program on it. */ micAsking: boolean; camAsking: boolean;}
export const media$ = createStore<MediaView>({ sound: null, playing: false, muted: false, mic: null, cam: null, micPerm: null, camPerm: null, micAsking: false, camAsking: false,});
export const useMedia = (): MediaView => useSyncExternalStore(media$.subscribe, media$.get);
let out: AudioOut | null = null, mic: AudioIn | null = null, cam: Camera | null = null;
/** The page's sink and sources this load (null: not on this boot), for their figures. */export const mediaDevices = () => ({ out, mic, cam });
const MUTED_KEY = "sound-muted";
/** The sound card's sink (boot.ts, with the sound option). */export function soundOut(log: (m: string) => void): AudioOut { out = createAudioOut({ log, muted: store(MUTED_KEY) === "1", onChange: (s) => media$.set({ sound: s }), onPlaying: (p) => media$.set({ playing: p }), }); media$.set({ sound: out.state, playing: out.playing, muted: out.muted }); return out;}
/** The capture source (boot.ts, with the mic option). */export function micIn(log: (m: string) => void): AudioIn { mic = createAudioIn({ log, onChange: (s) => media$.set({ mic: s }) }); media$.set({ mic: mic.state }); return mic;}
/** The camera (boot.ts, with the camera). */export function cameraIn(opts: CameraOptions): Camera { cam = createCamera({ ...opts, onChange: (s) => media$.set({ cam: s }) }); media$.set({ cam: cam.state }); return cam;}
/** Mute or unmute the page's output, at once, and keep it for the next loads. */export function setMuted(on: boolean): void { if (out) out.muted = on; store(MUTED_KEY, on ? "1" : "0"); media$.set({ muted: on });}
let watching = false;
/** Follow the browser's microphone and camera permissions (once per load; where the Permissions API knows them). */export function watchPermissions(): void { if (watching) return; watching = true; const perms = (globalThis.navigator as Partial<Navigator> | undefined)?.permissions; if (!perms?.query) return; for (const [name, key] of [["microphone", "micPerm"], ["camera", "camPerm"]] as const) { perms.query({ name: name as PermissionName }).then((st) => { const put = () => media$.set({ [key]: st.state === "granted" || st.state === "denied" || st.state === "prompt" ? st.state : null }); put(); st.addEventListener("change", put); }, () => {}); }}
/** The device's state is one where a program has it open and gets nothing from the host (a retry() may help). */const refused = (s: AudioInState | CameraState | null): boolean => s === "denied" || s === "unavailable";
/** allow() would do something for `kind` now (see the top of this file). */export function canAllow(kind: "mic" | "cam", v: MediaView): boolean { const s = kind === "mic" ? v.mic : v.cam, perm = kind === "mic" ? v.micPerm : v.camPerm; // Blocked for the site: the browser does not ask again (its site settings do). if (s === null || perm === "denied") return false; return refused(s) || (s === "idle" && perm === "prompt" && !(kind === "mic" ? v.micAsking : v.camAsking));}
/** From a click (user activation): ask the browser for the microphone or the camera (see the top of this file). */export function allow(kind: "mic" | "cam"): void { const src = kind === "mic" ? mic : cam; if (!src) return; if (refused(src.state)) { src.retry(); return; } const asking = kind === "mic" ? "micAsking" : "camAsking"; if (src.state !== "idle" || media$.get()[asking]) return; const what = kind === "mic" ? "microphone" : "camera"; media$.set({ [asking]: true }); void requestUserMedia(kind === "mic" ? { audio: true, video: false } : { video: true, audio: false }, what).then((r) => { if ("stream" in r) stopUserMedia(r.stream); else KW.ui?.toast(capitalize(r.message)); media$.set({ [asking]: false, ...("stream" in r ? { [kind === "mic" ? "micPerm" : "camPerm"]: "granted" } : {}) }); });}