# 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. **New to Linux servers?** Start with the copy-paste guide: [streamd-server-setup.md](streamd-server-setup.md) (download, chmod, add MPD streams, screen/systemd so it survives closing the terminal). ## drm serve ```bash ./bin/drm serve --bind 127.0.0.1:8083 --data .cache/streamd ``` | Flag | Default | Purpose | |---|---|---| | `--bind` | `127.0.0.1:8083` | listen address (loopback). Non-loopback logs a warning | | `--data` | `.cache/streamd` | SQLite, work dirs, HLS output, logs | | `--token` | `$STREAMD_TOKEN` | legacy admin API key (prefer dashboard API keys) | | `--admin-user` / `--admin-pass` | — | create first admin on empty install | | `--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` | `/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 and sign in (or complete first-run setup). Auth is session cookies for the dashboard and **API keys** (Settings → API keys) for agents/scripts. Roles: `admin`, `operator`, `viewer`. `GET /api/health` stays anonymous for liveness; stream list and mutations need a principal. Legacy `--token` / `STREAMD_TOKEN` still works as an admin key and prints a migration warning. ### 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. - **Variants** (FHD / HD / SD): FHD is the source copy at `/hls//index.m3u8`. Enabling HD/SD starts live ffmpeg ladders at `/hls/-hd/` and `/hls/-sd/`. The dashboard Copy button copies the absolute m3u8 URL without opening a player. ### 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//` with `has_yaml` + `yaml_sha256` | | `PUT /api/modules/:name` | write `modules//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/capture` | ask connected agents to refresh credentials now | | `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 # Prefer an operator API key from the streamd dashboard (Settings → API keys). export STREAMD_API_KEY=ndk_… # or: export STREAMD_TOKEN=… (legacy alias) ./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. Streamd can also push a capture immediately (`POST /api/streams/:id/capture` or the dashboard **Refresh keys** button). ### 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 # Prefer an API key created in the streamd dashboard (operator role): # api_key: ndk_… # token: … # alias for api_key (legacy name) # Or set STREAMD_API_KEY / STREAMD_TOKEN in the environment. 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//` 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 # terminal 2 — after creating an operator API key in the dashboard export STREAMD_API_KEY=ndk_… ./bin/drm agent run --config apps/agent/agent.yaml ``` Then create a stream, leave **Enabled** checked (required for on-demand wake), 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// downloader scratch + keys.txt www// 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.