feat(media): shade live images — a webcam, a slideshow or a watched folder, end to end

Pictures were a live content type the watch page could show but nothing
could publish. `shade live images`:
- stdin: back-to-back JPEG/PNG, e.g.
  `ffmpeg -f v4l2 -i /dev/video0 -r 1 -c:v mjpeg -f image2pipe -`;
- files/directories: a slideshow, one picture every --every seconds;
- --watch DIR: each new picture once its size holds still (cameras
  write in pieces).
Each picture is a keyframe, so a late viewer opens on the latest. The meta
carries the first picture's type (MIME_RE rightly refuses image/*).

ImageStreamSplitter walks JPEG markers (lengths to SOS, then the
entropy-coded data, where every 0xFF is stuffed) and PNG chunks to IEND,
so an EXIF thumbnail — a whole JPEG inside APP1 — does not end a picture.
Bounded at 16 MiB per picture, junk refused at its first bytes.

The Android demo shows live picture channels and sealed pictures
(decoded off the UI thread, downsampled to the screen).

Verified: splitter on real ffmpeg MJPEG/PNG under random pipe chunking,
the thumbnail case, truncation, junk and the size cap (4 tests); in
Chromium a 2 fps MJPEG "webcam" through the relay (8 → 14 pictures in 3 s,
960×540); a watched folder with a PNG written in two halves (sent once,
whole) and a .txt ignored; slideshow; error paths. Demo APK builds; not
run on a device (none connected).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 22:53:37 +02:00
parent 1653060494
commit d87db5f4dc
9 changed files with 436 additions and 10 deletions

View File

@@ -48,6 +48,17 @@ ffmpeg -re -i camera.mp4 -c:v libvpx -deadline realtime -g 30 -c:a libopus \
ffmpeg -re -i camera.mp4 -c:v libx264 -tune zerolatency -g 30 -c:a aac \
-f mp4 -movflags frag_keyframe+empty_moov+default_base_moof -frag_duration 250000 - \
| shade live video --server https://relay.example --name "Kamera 1"
# Live radio: the same, with sound only — the page and the app give it an audio player.
ffmpeg -re -i radio.mp3 -c:a libopus -f webm -live 1 -cluster_time_limit 1000 - \
| shade live video --server https://relay.example --name "Radio"
# Live pictures: a webcam at one picture a second …
ffmpeg -f v4l2 -i /dev/video0 -r 1 -c:v mjpeg -f image2pipe - \
| shade live images --server https://relay.example --name "Hytta"
# … a slideshow, or every new picture that lands in a folder.
shade live images Ferie/ --every 5 --server https://relay.example
shade live images --watch ~/Pictures/Screenshots --server https://relay.example
```
`shade live video` recognises the container from the first bytes (`--format webm|fmp4`
@@ -55,6 +66,11 @@ pins it). A fragment is a starting point for late joiners when the first sample
video track is a sync sample, so fragments shorter than a GOP keep latency down without
breaking late joining.
`shade live images` splits a piped stream into whole JPEG/PNG pictures by walking
their markers and chunks (a JPEG's EXIF thumbnail is a whole JPEG and does not end the
picture). Each picture is a keyframe, so a late viewer opens on the latest. A watched
folder sends a file once its size has held still, because cameras write in pieces.
`--channel <file>` keeps a live channel's identity across restarts: the link keeps
working, and each run starts a new epoch.