Co-authored-by: Arnaud Dartois <admin@tordais.cc>
12 KiB
Live WebRTC implementation and acceptance
The current vertical provides Agent RTSP re-publishing, Central camera-scoped
Data Protection tickets and MediaMTX auth, and a camera detail WHEP player.
FEAT-01 stays open until the disposable synthetic acceptance below passes
and external network behavior is measured.
Required architecture
- Keep camera assignment and stream lifecycle in Agent. Only an enabled camera present in the current Central assignment snapshot may be published. Stop the publisher when assignment, enablement, or Agent lifetime ends. A source URL change for the same camera must also stop the old publisher and start one from the new configuration before synchronization is considered complete.
- Use MediaMTX as the WebRTC/WHEP media server. Agent publishes the camera's browser-compatible H.264 stream to a camera-ID path. The relay passes video through and optionally maps the first audio track, transcoding it to Opus for WebRTC browser compatibility; a source without audio must still publish. Prefer passthrough when the source video codec is compatible; otherwise make video transcoding an explicit, bounded option. PTZ remains separate.
- Central issues random, short-lived, purpose-separated publish and read claims scoped to one camera and one action. Protect claims with the shared Heimdall Data Protection key ring. MediaMTX HTTP auth must validate exact path, action, protocol, expiry, Agent assignment for publish, and current user camera rights for read. Never put RTSP credentials, Central API keys, or reusable TURN credentials in browser-visible URLs or logs.
- Browser obtains a read session from the authenticated camera API and performs WHEP with that short-lived bearer token. ICE servers and public WHEP URL must come from deployment configuration, never source-code hostnames. Do not issue static TURN credentials to the browser.
- Bound simultaneous Agent publishers and viewers, request duration, retries, FFmpeg process count, and session lifetime. Ensure cancellation/config changes reap child processes and release permits.
Source-change lifecycle acceptance
LiveCameraRelayService compares the effective RTSP source configured for each
camera (substream when configured, otherwise primary stream) with the source attached to its running FFmpeg relay. If the source
changes while the CameraId remains assigned and enabled, synchronization stops
and disposes the old process before starting a relay with the updated source.
The normal synchronization interval is five seconds.
Run bash skills/webrtc-live/source-change-acceptance.sh for the focused
disposable Compose acceptance. It packages tracked and non-ignored source into
an isolated .NET 10 SDK container and runs the Agent relay tests, including the
same-CameraId primary and substream source changes, unchanged-source stability, renewal-boundary,
child-process stop, and log-redaction assertions. It publishes no host ports,
uses no persistent volumes or deployment credentials, builds no local image,
and keeps only the upstream SDK image. The EXIT trap tears down the unique
Compose project and verifies its containers, volumes, and network are absent.
Implementation references
Use MediaMTX's maintained documentation as the protocol reference:
- WHEP/WebRTC read
- HTTP authentication callbacks
- ICE, TURN, codec and latency details
- Configuration reference
- RTSP camera/source behavior
- MediaMTX license
Use H.264 without B-frames for browser compatibility and latency. Validate actual negotiation in browsers; a successful RTSP publish does not prove WHEP.
User-facing documentation source of truth
When updating docs/specs.md or docs/ui-pages.md, trace each shipped claim to
the current implementation:
- The camera detail route and embedded Watch/Stop controls are in
src/Heimdall.Web/Components/Pages/CameraDetail.razor; the browser module issrc/Heimdall.Web/wwwroot/js/live-view.js. Do not document a separate/cameras/{id}/liveroute or rename the module toliveView.js. - When documenting role visibility in
docs/auth-roles.md, use the camera detail route for the embedded live-view and trace session permission toLiveMediaController; the endpoint requiresCamera.Readplus camera-level access, not a separateCamera.Liveendpoint policy. - Session creation/release, authorization, short-lived tickets, WHEP URL and
ICE configuration are in
src/Heimdall.Web/Controllers/LiveMediaController.cs. - Agent RTSP publication and FFmpeg lifecycle are in
src/Heimdall.Agent/Services/LiveCameraRelayService.cs. - Describe the current media path as Agent RTSP(S) publication to MediaMTX and browser WHEP/WebRTC playback. SignalR hubs carry no video media or live-view SDP/ICE signaling.
- Separate implementation and synthetic acceptance evidence from real camera, external ICE/TURN and latency measurements. Track those external results in #161; keep the 503/configuration investigation in #341.
- Before preserving a proposed control or performance target in a UI mockup, mark it as planned and verify it is not presented as shipped behavior.
Required acceptance
Run from changed source in an isolated disposable Compose project with unique
project name, generated credentials, ports and volumes. The optional
docker/docker-compose.webrtc-test.yml starts a local-only generated H.264
camera feed. Use docker/docker-compose.webrtc-local-test.yml plus
docker/docker-compose.webrtc-test.yml with the core docker-compose.yml in one
unique project. Set unique generated database/JWT credentials, loopback ports,
HEIMDALL_NETWORK_NAME, HEIMDALL_MEDIAMTX_AUTH_URL, WHEP/publish URLs, and the
Central browser origin in an env file outside the repository. Start Central,
MediaMTX and the synthetic feed first; bootstrap the first admin and site, then
place the generated synthetic site token in the disposable env file and start
the Agent. Configure its assigned camera against the host loopback RTSP port. Do not depend on a physical camera. Verify all of the following:
- An assigned Agent receives only its assigned camera's short-lived publish credential and publishes synthetic H.264 to that camera path.
- An authorized user obtains a short-lived read claim and a browser-compatible WHEP session; the browser receives decoded frames. With synthetic audio, prove the browser negotiates audio and video; playback starts muted for autoplay compatibility and the native controls allow the viewer to enable it.
- An unassigned Agent, cross-camera claim, wrong action/path/protocol, expired token, and user without camera access are rejected.
- Disable/reassign camera, stop Agent, end viewer, and expire claim; verify the matching processes/sessions terminate and resource counts return to baseline.
- Logs and API errors contain no credentials, raw tokens, RTSP URLs, or ICE secrets. Verify configured ICE/TURN candidate behavior separately.
- Install an EXIT trap before the first image build/start. Tear down only that
Compose project with volumes/orphans, then verify its containers, volumes
and network are gone. Independently remove and verify absence of every
project-unique local image tag built by this recipe (Central, Agent, or
helper), even after a failed build/assertion; preserve shared MediaMTX and
other upstream base images. Preserve the test exit status and report cleanup
failures. Do not use
--rmi allor broad image-prune commands.
For this recipe the locally built tags are Central and Agent; record their
exact project-unique values before building. The synthetic RTSP source uses
the upstream MediaMTX and FFmpeg images, which remain installed. The cleanup
record is complete only after both local tags have been removed and an
exact-tag docker image inspect confirms absence.
Record LAN browser and external TURN runs independently. Focused API/Web ticket/session tests (7/7) and Agent relay tests pass. On 5 November 2026, disposable project hmd-wrtc-75d1f49f was built from this source with a synthetic H.264 RTSP source. Central bootstrap/site creation, Agent registration and camera assignment passed. Headless Chromium ran the actual live-view.js WHEP path and decoded a 640×360 frame (one video track); WHEP resource deletion returned 200 and Central session release 204. The MediaMTX auth callback accepted the valid read ticket and rejected altered action/path/protocol and a forged ticket (401); unknown camera access was denied. The API disable action stopped the scoped Agent publisher process from one to zero. Log scans found no admin/session/site credentials or RTSP source URL. The run exposed and fixed MediaMTX env interpolation and Docker ICE port mapping in the local recipe. The project was torn down with down --volumes --remove-orphans; project-scoped checks found zero containers, volumes and networks, and temporary secrets were deleted. Its historical record lacks exact-tag image absence checks, so repeat this recipe under the cleanup requirements above before treating cleanup as fully verified. Physical camera, LAN, external TURN and PTZ remain unvalidated. Synthetic audio codec and browser-track acceptance is recorded separately below; keep FEAT-01 open for its remaining external items.
Synthetic audio acceptance
Run a disposable Compose source build plus a synthetic H.264+sine RTSP publisher. The source-built Agent tests must assert the relay command maps 0:a:0?, converts audio to Opus, and does not disable audio. The Compose stream must be pulled from the relay and inspected to confirm H.264 video and Opus audio. Also run the authenticated WHEP browser flow and assert its media stream contains both tracks; no physical camera or external TURN claim follows from this recipe. Build app images with unique tags and remove/verify those exact tags in the same EXIT cleanup trap, or explicitly record local image tags: none for SDK/upstream-only test runs.
LiveViewTests.Live_view_negotiates_audio_and_video_and_starts_muted_for_autoplay uses its fake peer by default for an isolated UI contract test. For the real-media path, set HEIMDALL_TEST_LIVE_WHEP_URL to the disposable MediaMTX /path/whep endpoint before running the same test. It then uses Chromium's real RTCPeerConnection and waits for a decoded video frame (videoWidth > 0) plus a live audio track. Pair this with a project-isolated MediaMTX service and synthetic H.264/Opus publisher, configured with a loopback WHEP port, a matching loopback ICE UDP candidate, and wildcard origins only on the throwaway test server. The test still mocks only Central's short-lived read-session response; the earlier Central permission/ticket test remains the evidence for that boundary. Teardown must remove and verify all project resources and any exact local image tags.
On 5 November 2026, disposable project hmd-whep-audio-1793905588-381987 ran MediaMTX plus an animated 640×360 H.264 and 48 kHz sine/Opus publisher. The source-built Web/E2E assembly used the real live-view.js and Chromium against http://127.0.0.1:<ephemeral>/live/whep; LiveViewTests passed 1/1 with a decoded video frame, live audio track, and muted autoplay. Central's session response was stubbed, so this proves browser/media negotiation and decoding, not production auth or external ICE/TURN. Cleanup verified zero project containers, volumes and networks; no local images were built and the temporary Compose file was removed.