mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 02:39:50 +08:00
docs: split the Display page into Display and Input, and Sound and Camera
This commit is contained in:
@@ -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.
|
||||
::
|
||||
::
|
||||
|
||||
@@ -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).
|
||||
|
||||
61
docs/1.sdk/11.sound.md
Normal file
61
docs/1.sdk/11.sound.md
Normal file
@@ -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.
|
||||
Reference in New Issue
Block a user