Null-DRM-Official/docs/streamd.md
404errordeveloper ac8701b5a6
All checks were successful
release / build-and-upload (push) Successful in 6m20s
modules-release / build-and-upload (push) Successful in 4m51s
streamd: optional keys, per-stream proxy, auto deps; agent module.yaml sync; RTE AutoPlay; Telegram release announces
- streamd: MPD-only start, proxy field, ffmpeg/NRE/mp4decrypt auto-install
- GET/PUT /api/modules for module.yaml sync; agent sync_modules
- RTE Live-tab AutoPlay fixes; modules zip named bbc/rte/tg4.zip
- Telegram notify scripts wired into Forgejo release workflows
2026-10-09 00:17:23 +02:00

200 lines
6.6 KiB
Markdown

# Control plane and agent
Two long-running pieces: `drm serve` holds the state and the dashboard, `drm agent
run` keeps credentials fresh using real phones.
## drm serve
```bash
./bin/drm serve --bind 127.0.0.1:8083 --data .cache/streamd --token SECRET
```
| Flag | Default | Purpose |
|---|---|---|
| `--bind` | `0.0.0.0:8083` | listen address |
| `--data` | `.cache/streamd` | SQLite, work dirs, HLS output, logs |
| `--token` | `$STREAMD_TOKEN` | bearer token for mutating routes |
| `--nre` | `$NRE_PATH`, `bin/`, `PATH`, auto-download | N_m3u8DL-RE |
| `--ffmpeg` | `$FFMPEG_PATH`, `bin/`, `PATH`, auto-download | ffmpeg |
| `--mp4decrypt` | `$MP4DECRYPT_PATH`, `bin/`, `PATH`, auto-download | Bento4 mp4decrypt |
| `--bin-dir` | `<repo>/bin` | where auto-downloaded tools are installed |
| `--skip-deps` | off | do not download missing tools |
On start, `serve` resolves media tools and **downloads** ffmpeg (BtbN static),
N_m3u8DL-RE (v0.6.0-beta), and mp4decrypt (Bento4) into `bin/` when missing.
macOS ffmpeg is not auto-fetched — install with `brew install ffmpeg`.
Open <http://127.0.0.1:8083>. GET routes are unauthenticated; anything mutating
needs `Authorization: Bearer <token>`. The header shows ffmpeg / NRE / mp4decrypt
status from `/api/health` → `deps`.
### Keys and proxy
- **MPD** is required to start. **Keys are optional** (clear streams such as BBC).
- Per-stream **`proxy`** (`socks5://127.0.0.1:1080` or `http://…`) is passed to
N_m3u8DL-RE as `--custom-proxy` so origin fetches can egress via a UK SOCKS
sidecar while the host itself stays elsewhere. The local MPD rewrite fetch
uses the same proxy. Outbound HLS you serve is unaffected.
### API
| Route | Purpose |
|---|---|
| `GET /api/health` | liveness + uptime + `deps` paths |
| `GET /api/apps` | modules under `modules/`, their channels, registered rewriters |
| `GET /api/modules` | list `modules/<name>/` with `has_yaml` + `yaml_sha256` |
| `PUT /api/modules/:name` | write `modules/<name>/module.yaml` (bearer; raw YAML or `{"yaml":…}`) |
| `GET /api/streams` | all streams with runtime state |
| `POST /api/streams` | create |
| `PATCH /api/streams/:id` | update |
| `DELETE /api/streams/:id` | remove |
| `POST /api/streams/:id/start\|stop\|restart` | desired state |
| `POST /api/streams/:id/credentials` | push a fresh manifest + keys |
| `POST /api/streams/:id/claim` | agent claims the stream |
The dashboard's app and channel pickers are filled from `/api/apps`, so they reflect
whatever modules the binary was built with. Nothing about a provider is hardcoded in
the UI or the schema.
### Creating a stream
```bash
./bin/drm catalog --app tg4 --channel ioi --keys --wvd data/device.wvd --json > s.json
curl -X POST http://127.0.0.1:8083/api/streams \
-H 'Authorization: Bearer SECRET' -H 'Content-Type: application/json' \
--data @s.json
```
A stream created without `app`, `video_select`, `audio_select` or `rewriter` keeps
those empty on purpose. They are resolved when the stream starts:
```text
stored row -> the stream's app module (app.StreamDefaults) -> neutral defaults
```
So a module decides its own downloader settings, an operator override in the row
still wins, and an unknown app gets something harmless rather than another
provider's preferences.
### Refreshing credentials
```bash
curl -X POST http://127.0.0.1:8083/api/streams/1/credentials \
-H 'Authorization: Bearer SECRET' -H 'Content-Type: application/json' \
-d '{"mpd":"https://...","key":"KID:KEY\nKID:KEY","headers_json":"{}"}'
```
This is exactly what the agent does after a successful capture.
### Manifest rewriting
If a stream's `rewriter` names a registered rewriter and the manifest is not HLS,
the supervisor starts a local rewrite server and points the downloader at it:
```text
stream rewriter="rte" -> mpd.Lookup("rte") -> http://127.0.0.1:PORT/manifest.mpd
```
The log line is `stream X: rewritten MPD http://127.0.0.1:…`. Streamd never imports
a provider package to do this — the module registered the rewriter under that name
at init time.
## drm agent run
```bash
export STREAMD_TOKEN=SECRET
./bin/drm agent run --config apps/agent/agent.yaml
```
What it does, in a loop:
1. poll streamd for enabled streams that are down or missing credentials
2. enqueue a job in a durable SQLite queue
3. claim a free ADB device that already has the app installed
4. run a phone capture through the stream's app module
5. `POST /api/streams/:id/credentials`
6. force-stop the app and release the device
It never installs apps and never runs a downloader. A websocket heartbeat keeps its
claims alive, so claims expire automatically if the agent dies.
### Subcommands
```bash
./bin/drm agent devices # phones it can use
./bin/drm agent status # recent jobs
./bin/drm agent enqueue --stream tg4-ioi --reason manual
./bin/drm agent cancel --job 3
```
### Config
`apps/agent/agent.yaml`:
```yaml
streamd_url: http://127.0.0.1:8083
# token: taken from $STREAMD_TOKEN when unset
poll_interval_sec: 30
data_dir: .cache/agent
workers: 1
max_attempts: 5
capture_wait_sec: 180
# devices: [SERIAL1, SERIAL2] # empty = any authorised device
# python: path to python for wvkey.py
# wvd: data/device.wvd
# user_agent: ""
# agent_id: defaults to hostname
```
Providers are discovered under `modules/<name>/` next to the install (built and
run by the host; not linked into `drm.exe`).
`wvd` must be set (here or on the phone capture) or key fetches fail.
## Running both locally
```bash
# terminal 1
./bin/drm serve --bind 127.0.0.1:8083 --data .cache/streamd --token SECRET
# terminal 2
export STREAMD_TOKEN=SECRET
./bin/drm agent run --config apps/agent/agent.yaml
```
Then create a stream, enable it, and watch the agent pick it up:
```bash
./bin/drm agent status
```
## External tools
The media supervisor shells out to three binaries. Put them on `PATH`, in `bin/`,
or name them with flags/env:
| Tool | Flag | Env |
|---|---|---|
| N_m3u8DL-RE | `--nre` | `NRE_PATH`, `N_M3U8DL_RE` |
| ffmpeg | `--ffmpeg` | `FFMPEG_PATH`, `FFMPEG` |
| mp4decrypt | `--mp4decrypt` | `MP4DECRYPT_PATH`, `MP4DECRYPT` |
Streams can be created, claimed and credentialed without any of them; only actually
pulling media needs them. The supervisor is the least exercised part of the system —
treat its health reporting as control-plane bookkeeping.
## Data
```text
.cache/streamd/
streamd.db SQLite: streams, runtime, claims
work/<name>/ downloader scratch + keys.txt
www/<name>/ HLS output
logs/ per-stream downloader + ffmpeg logs
.cache/agent/
queue.db durable job queue
```
Back up `streamd.db` before upgrading — schema defaults change between versions.