From f8322791e70eefecbf5dcdcfcf027cfc83a0ca75 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 30 Nov 2026 10:21:11 +0000 Subject: [PATCH] docs: split the Display page into Display and Input, and Sound and Camera --- docs/1.sdk/0.index.md | 13 +++- docs/1.sdk/10.display.md | 55 +++-------------- docs/1.sdk/11.sound.md | 61 +++++++++++++++++++ docs/1.sdk/{11.graphics.md => 12.graphics.md} | 0 .../{12.snapshots.md => 13.snapshots.md} | 0 docs/1.sdk/{13.worker.md => 14.worker.md} | 0 docs/1.sdk/{14.options.md => 15.options.md} | 0 docs/1.sdk/{15.serial.md => 16.serial.md} | 0 .../{16.bluetooth.md => 17.bluetooth.md} | 0 docs/1.sdk/{17.usb.md => 18.usb.md} | 0 10 files changed, 79 insertions(+), 50 deletions(-) create mode 100644 docs/1.sdk/11.sound.md rename docs/1.sdk/{11.graphics.md => 12.graphics.md} (100%) rename docs/1.sdk/{12.snapshots.md => 13.snapshots.md} (100%) rename docs/1.sdk/{13.worker.md => 14.worker.md} (100%) rename docs/1.sdk/{14.options.md => 15.options.md} (100%) rename docs/1.sdk/{15.serial.md => 16.serial.md} (100%) rename docs/1.sdk/{16.bluetooth.md => 17.bluetooth.md} (100%) rename docs/1.sdk/{17.usb.md => 18.usb.md} (100%) diff --git a/docs/1.sdk/0.index.md b/docs/1.sdk/0.index.md index 76c3293..7a401c1 100644 --- a/docs/1.sdk/0.index.md +++ b/docs/1.sdk/0.index.md @@ -90,10 +90,19 @@ Changes to the root file system stay in memory, unless a persistent data disk (l ::card --- - title: Display, input and sound + title: Display and input icon: ph:monitor-duotone to: /sdk/display --- - A framebuffer, evdev input and ALSA sound. + A framebuffer, keyboard and mouse. + :: + + ::card + --- + title: Sound and camera + icon: ph:speaker-high-duotone + to: /sdk/sound + --- + ALSA playback and capture, and a V4L2 camera. :: :: diff --git a/docs/1.sdk/10.display.md b/docs/1.sdk/10.display.md index d5569da..262f1b7 100644 --- a/docs/1.sdk/10.display.md +++ b/docs/1.sdk/10.display.md @@ -2,17 +2,16 @@ icon: ph:monitor-duotone --- -# Display, Input and Sound +# Display and Input -> A framebuffer, evdev input, ALSA sound, a microphone and a camera for guest programs. +> A framebuffer, a keyboard and a mouse for guest programs. ```js -import { createKernel, domCodeToLinux, pcmToFloat32 } from "unkernel"; +import { createKernel, domCodeToLinux } from "unkernel"; const vm = await createKernel({ display: { width: 800, height: 600 }, // the guest gets /dev/fb0 onDisplay: (mode) => console.log(mode), // a program opened it (its mode) or closed it (null) - sound: { onChunk: ({ data, format }) => play(pcmToFloat32(data, format)) }, }); const frame = vm.frame(); // RGBA, or null while nothing draws @@ -24,50 +23,10 @@ addEventListener("keydown", (e) => { }); ``` +`display` is `true` (640x480), a size, or a function that returns the size each time a program first opens the device, such as the window's size. A program may set its own mode, and `onDisplay` reports every change. `frame()` reads the latest picture and `readFrame()` waits for a fresh one. + ## Input -`evdev` also has `button()`, `move()`, `moveTo()` and `wheel()` for a mouse or tablet. `onInput` tells you when a program opens the keyboard and mouse; until then, send keys to the console. `readFrame()` waits for a fresh frame. +With a display, the guest also gets a keyboard and a mouse (evdev, `/dev/input/event0` and `event1`). `evdev` has `key()`, and `button()`, `move()`, `moveTo()` and `wheel()` for a mouse or tablet. `onInput` tells you when a program opens the keyboard and mouse; until then, send keys to the console. -## Sound in browsers - -`createAudioOut()` plays the guest's sound with Web Audio: - -```js -import { createAudioOut, createKernel } from "unkernel/web"; - -button.hidden = true; -const audio = createAudioOut({ - onChange: (state) => { - button.hidden = state !== "waiting"; // "click to play" - }, -}); -const vm = await createKernel({ sound: audio }); -button.onclick = () => audio.resume(); // or any other click or key press on the page -``` - -Browsers start sound only after a click, tap or key press on the page. Until then the latest `holdSeconds` (10) of what the guest plays are held: a clip that has ended by then plays whole, a stream joins live. `audio.muted = true` silences it. - -One sink serves one kernel at a time, and outlives it: pass it to the next kernel, or `close()` it. - -## Microphone - -`soundIn` gives the sound card a capture substream: what `arecord` records, or a read of `/dev/dsp`. `createAudioIn()` from `unkernel/web` is the user's microphone. It is asked for only when a guest program starts recording and released when the program closes the device; until then the guest records silence. Anywhere else, `true` records silence, or a source's `read(out, rate)` fills `out` with mono samples in [-1, 1]. - -```js -import { createAudioIn, createAudioOut, createKernel } from "unkernel/web"; - -const vm = await createKernel({ sound: createAudioOut(), soundIn: createAudioIn() }); -``` - -## Camera - -`camera` gives the guest `/dev/video0`, a V4L2 device with I420, NV12 and YUYV up to 1280x720 at 30 frames a second, for `v4l2-ctl`, ffmpeg, GStreamer or a browser in the guest. `createCamera()` from `unkernel/web` is the user's camera. It is asked for only when a guest program starts streaming and released when the program stops; until then the guest gets black frames. Anywhere else, `true` gives black frames, or a source's `read(out, format)` writes the current frame in the stream's format. - -```js -import { createCamera, createKernel } from "unkernel/web"; - -const vm = await createKernel({ camera: createCamera() }); -// in the guest: ffmpeg -f v4l2 -input_format h264 -i /dev/video0 -c:v copy out.mkv -``` - -Where the browser's `VideoEncoder` makes H.264, the camera offers it too, encoded on the host, so the guest copies the stream instead of encoding it, as with a webcam that encodes. The CLI's `--camera` shows a test pattern, and `--camera=h264` also offers it as H.264. +For guest GL, Vulkan and a virtio-gpu display, see [GL, Vulkan and Video](/sdk/graphics). For sound, see [Sound and Camera](/sdk/sound). diff --git a/docs/1.sdk/11.sound.md b/docs/1.sdk/11.sound.md new file mode 100644 index 0000000..0d9db02 --- /dev/null +++ b/docs/1.sdk/11.sound.md @@ -0,0 +1,61 @@ +--- +icon: ph:speaker-high-duotone +--- + +# Sound and Camera + +> A sound card with playback and capture, and a V4L2 camera, for guest programs. + +```js +import { createKernel, pcmToFloat32 } from "unkernel"; + +const vm = await createKernel({ + sound: { onChunk: ({ data, format }) => play(pcmToFloat32(data, format)) }, // ALSA hw:0,0 and /dev/dsp +}); +``` + +`sound` gives the guest a sound card (ALSA `hw:0,0` and OSS `/dev/dsp`): `true` drops what it plays, or a sink receives the PCM chunks with their format and the playback state. + +## Sound in browsers + +`createAudioOut()` plays the guest's sound with Web Audio: + +```js +import { createAudioOut, createKernel } from "unkernel/web"; + +button.hidden = true; +const audio = createAudioOut({ + onChange: (state) => { + button.hidden = state !== "waiting"; // "click to play" + }, +}); +const vm = await createKernel({ sound: audio }); +button.onclick = () => audio.resume(); // or any other click or key press on the page +``` + +Browsers start sound only after a click, tap or key press on the page. Until then the latest `holdSeconds` (10) of what the guest plays are held: a clip that has ended by then plays whole, a stream joins live. `audio.muted = true` silences it. + +One sink serves one kernel at a time, and outlives it: pass it to the next kernel, or `close()` it. + +## Microphone + +`soundIn` gives the sound card a capture substream: what `arecord` records, or a read of `/dev/dsp`. `createAudioIn()` from `unkernel/web` is the user's microphone. It is asked for only when a guest program starts recording and released when the program closes the device; until then the guest records silence. Anywhere else, `true` records silence, or a source's `read(out, rate)` fills `out` with mono samples in [-1, 1]. + +```js +import { createAudioIn, createAudioOut, createKernel } from "unkernel/web"; + +const vm = await createKernel({ sound: createAudioOut(), soundIn: createAudioIn() }); +``` + +## Camera + +`camera` gives the guest `/dev/video0`, a V4L2 device with I420, NV12 and YUYV up to 1280x720 at 30 frames a second, for `v4l2-ctl`, ffmpeg, GStreamer or a browser in the guest. `createCamera()` from `unkernel/web` is the user's camera. It is asked for only when a guest program starts streaming and released when the program stops; until then the guest gets black frames. Anywhere else, `true` gives black frames, or a source's `read(out, format)` writes the current frame in the stream's format. + +```js +import { createCamera, createKernel } from "unkernel/web"; + +const vm = await createKernel({ camera: createCamera() }); +// in the guest: ffmpeg -f v4l2 -input_format h264 -i /dev/video0 -c:v copy out.mkv +``` + +Where the browser's `VideoEncoder` makes H.264, the camera offers it too, encoded on the host, so the guest copies the stream instead of encoding it, as with a webcam that encodes. The CLI's `--camera` shows a test pattern, and `--camera=h264` also offers it as H.264. diff --git a/docs/1.sdk/11.graphics.md b/docs/1.sdk/12.graphics.md similarity index 100% rename from docs/1.sdk/11.graphics.md rename to docs/1.sdk/12.graphics.md diff --git a/docs/1.sdk/12.snapshots.md b/docs/1.sdk/13.snapshots.md similarity index 100% rename from docs/1.sdk/12.snapshots.md rename to docs/1.sdk/13.snapshots.md diff --git a/docs/1.sdk/13.worker.md b/docs/1.sdk/14.worker.md similarity index 100% rename from docs/1.sdk/13.worker.md rename to docs/1.sdk/14.worker.md diff --git a/docs/1.sdk/14.options.md b/docs/1.sdk/15.options.md similarity index 100% rename from docs/1.sdk/14.options.md rename to docs/1.sdk/15.options.md diff --git a/docs/1.sdk/15.serial.md b/docs/1.sdk/16.serial.md similarity index 100% rename from docs/1.sdk/15.serial.md rename to docs/1.sdk/16.serial.md diff --git a/docs/1.sdk/16.bluetooth.md b/docs/1.sdk/17.bluetooth.md similarity index 100% rename from docs/1.sdk/16.bluetooth.md rename to docs/1.sdk/17.bluetooth.md diff --git a/docs/1.sdk/17.usb.md b/docs/1.sdk/18.usb.md similarity index 100% rename from docs/1.sdk/17.usb.md rename to docs/1.sdk/18.usb.md