From f7599480de776636e5431c72b95f85838f808d26 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 18 Jun 2026 12:27:31 -0700 Subject: [PATCH 1/3] mediamtx: substitute hostname/IP into yaml before start The shipped mediamtx.yaml uses placeholder values (raspberrypi, 192.0.2.1) for webrtcAdditionalHosts that lib/mediamtx.js is supposed to replace at runtime. Previously the substitution only ran on the backend's wired_controller observable, so any reset or redeploy of /etc/mediamtx.yaml left the placeholders advertised in WebRTC ICE candidates. Browsers cannot resolve "raspberrypi", silently fail peer-connection negotiation, and the dashboard preview spins forever with no visible error. Run lib/mediamtx.js as ExecStartPre so every mediamtx start unconditionally substitutes the real values into the file before mediamtx loads it. Also order the unit after network-online.target so the IP detection in getWiredAddress() is valid when the script runs. Verified on Node 24.15 (the version that ships): the IIFE at the bottom of lib/mediamtx.js gates on import.meta.main, which Node now supports natively, so no source change to lib/mediamtx.js is needed. --- os/mediamtx/mediamtx.service | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/os/mediamtx/mediamtx.service b/os/mediamtx/mediamtx.service index 845a4a61e..5bbd0a9d7 100644 --- a/os/mediamtx/mediamtx.service +++ b/os/mediamtx/mediamtx.service @@ -1,10 +1,16 @@ # https://mediamtx.org/docs/usage/start-on-boot#linux [Unit] -After=network.target -Wants=network.target +After=network-online.target +Wants=network-online.target [Service] +# Rewrite /etc/mediamtx.yaml with this device's real hostname and IP before +# mediamtx loads it, replacing the placeholders shipped in the YAML template. +# Without this, WebRTC ICE candidates advertise "raspberrypi" which browsers +# cannot resolve, causing silent peer-connection failures (dashboard preview +# spins forever). +ExecStartPre=/usr/bin/node /opt/PlanktoScope/lib/mediamtx.js ExecStart=/usr/local/bin/mediamtx /etc/mediamtx.yaml [Install] From b46ccf9bba0463600619abd738ee0abe4b9a5f8a Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 25 Jun 2026 11:35:19 -0700 Subject: [PATCH 2/3] mediamtx: use runOnInit instead of ExecStartPre for yaml substitution Run lib/mediamtx.js from the cam path's runOnInit hook in mediamtx.yaml rather than a systemd ExecStartPre, so the hostname/IP substitution also runs when mediamtx is started outside systemd (e.g. `just dev`). mediamtx watches the config file and hot-reloads after the rewrite; runOnInit fires once because lib/mediamtx.js only edits the global webrtcAdditionalHosts, leaving the cam path config unchanged. Keep network-online.target so the wired IP is available when it runs. --- os/mediamtx/mediamtx.service | 10 ++++------ os/mediamtx/mediamtx.yaml | 5 +++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/os/mediamtx/mediamtx.service b/os/mediamtx/mediamtx.service index 5bbd0a9d7..b361a0a8a 100644 --- a/os/mediamtx/mediamtx.service +++ b/os/mediamtx/mediamtx.service @@ -5,12 +5,10 @@ After=network-online.target Wants=network-online.target [Service] -# Rewrite /etc/mediamtx.yaml with this device's real hostname and IP before -# mediamtx loads it, replacing the placeholders shipped in the YAML template. -# Without this, WebRTC ICE candidates advertise "raspberrypi" which browsers -# cannot resolve, causing silent peer-connection failures (dashboard preview -# spins forever). -ExecStartPre=/usr/bin/node /opt/PlanktoScope/lib/mediamtx.js +# The hostname/IP substitution into /etc/mediamtx.yaml is driven by the +# `runOnInit` hook in the YAML itself (see os/mediamtx/mediamtx.yaml), so it +# also runs when mediamtx is started outside systemd (e.g. `just dev`). +# network-online.target is kept so the wired IP is available when that runs. ExecStart=/usr/local/bin/mediamtx /etc/mediamtx.yaml [Install] diff --git a/os/mediamtx/mediamtx.yaml b/os/mediamtx/mediamtx.yaml index b1714a75b..e5de46de4 100644 --- a/os/mediamtx/mediamtx.yaml +++ b/os/mediamtx/mediamtx.yaml @@ -37,3 +37,8 @@ paths: alwaysAvailable: true alwaysAvailableTracks: - codec: H264 + # Rewrite this file's placeholder hostname/IP (above) with the device's real + # values at startup, so WebRTC ICE candidates resolve regardless of how + # mediamtx was launched (systemd, `just dev`, manual). mediamtx watches the + # config file and hot-reloads after the rewrite. See lib/mediamtx.js. + runOnInit: /usr/bin/node /opt/PlanktoScope/lib/mediamtx.js From b016edc1c286b0515179f5d1a8367488e0cbd7b5 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Mon, 29 Jun 2026 14:43:32 +0000 Subject: [PATCH 3/3] remove redundant line --- os/mediamtx/justfile | 1 - 1 file changed, 1 deletion(-) diff --git a/os/mediamtx/justfile b/os/mediamtx/justfile index ff2eb88f4..ff3b8be8e 100644 --- a/os/mediamtx/justfile +++ b/os/mediamtx/justfile @@ -8,7 +8,6 @@ setup: sudo cp mediamtx.service /etc/systemd/system/ sudo cp mediamtx.yaml /etc/mediamtx.yaml sudo chown pi:pi /etc/mediamtx.yaml - sudo node ../../lib/mediamtx.js sudo systemctl reenable mediamtx sudo systemctl restart mediamtx