Skip to content

Commit f3bc1e4

Browse files
feat(demo): auto-bootstrap WebRTC demo stack and simplify setup
- start/reuse MediaMTX and FFmpeg test publisher from demo command - wait for service readiness and clean up helper processes on exit - auto-detect MediaMTX architecture with override support - remove obsolete manual setup and test helper scripts - streamline WebRTC setup documentation
1 parent c816b09 commit f3bc1e4

5 files changed

Lines changed: 156 additions & 100 deletions

File tree

README.md

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,20 @@ WebRTC playback (`localPlayer: "webrtc"` or `remotePlayer: "webrtc"`) requires a
187187

188188
**Quick Setup:**
189189

190-
1. Run the setup script (downloads and starts MediaMTX):
190+
1. Prepare MediaMTX (downloads binary, validates local RTSP/WHEP stack):
191191

192192
```bash
193-
cd ~/MagicMirror/modules/MMM-RTSPStream/scripts
194-
./setup_mediamtx.sh
193+
cd ~/MagicMirror/modules/MMM-RTSPStream
194+
RTSPSTREAM_SKIP_MM=1 node --run demo
195195
```
196196

197-
For Raspberry Pi, edit `setup_mediamtx.sh` and change `ARCH="linux_amd64"` to `ARCH="linux_arm64v8"`.
197+
On Raspberry Pi (or other non-default architectures), set `MEDIAMTX_ARCH` explicitly, for example:
198198

199-
2. Configure your stream in `mediamtx.yml`:
199+
```bash
200+
MEDIAMTX_ARCH=linux_arm64v8 RTSPSTREAM_SKIP_MM=1 node --run demo
201+
```
202+
203+
2. Configure your stream in `mediamtx/mediamtx.yml`:
200204

201205
```yaml
202206
paths:
@@ -380,25 +384,14 @@ Please note that this project is released with a [Contributor Code of Conduct](C
380384
- `node --run lint` - Run linting and formatter checks.
381385
- `node --run lint:fix` - Fix linting and formatter issues.
382386
- `node --run test` - Run linting and formatter checks.
383-
- `node --run demo` - Start MagicMirror with demo config for testing the module.
387+
- `node --run demo` - Start local WebRTC demo (auto-starts MediaMTX + FFmpeg test stream, then MagicMirror with `demo.config.js`).
384388

385-
**Testing with a local stream:**
389+
**WebRTC demo flow (`node --run demo`):**
386390

387-
1. Start MediaMTX and test stream:
388-
389-
```bash
390-
cd scripts
391-
./setup_mediamtx.sh # Start MediaMTX server
392-
./start_test_stream.sh # Start FFmpeg test pattern stream
393-
```
394-
395-
This creates a test stream at `http://localhost:8889/test/whep`.
396-
397-
2. Start MagicMirror demo (already configured to use the test stream):
398-
399-
```bash
400-
node --run demo
401-
```
391+
1. Starts (or reuses) MediaMTX and checks API readiness on `http://127.0.0.1:9997`.
392+
2. Publishes a local FFmpeg test stream to `rtsp://127.0.0.1:8554/test`.
393+
3. Launches MagicMirror with the bundled demo config (`whepUrl: http://localhost:8889/test/whep`).
394+
4. Stops the helper processes it started when MagicMirror exits.
402395

403396
The demo config uses WebRTC with the local test stream. You can switch between `localPlayer: "vlc"`, `"mplayer"`, or `"webrtc"` to test different playback methods.
404397

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"author": "shbatm",
1313
"license": "MIT",
1414
"scripts": {
15-
"demo": "cd ../../ && MM_CONFIG_FILE=modules/MMM-RTSPStream/demo.config.js node --run start:dev",
15+
"demo": "bash ./scripts/run_demo_webrtc.sh",
1616
"lint": "eslint && prettier . --check",
1717
"lint:fix": "eslint --fix && prettier . --write",
1818
"preinstall": "./scripts/preinstall.sh",

scripts/run_demo_webrtc.sh

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
MODULE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
7+
MM_DIR="$(cd "${MODULE_DIR}/../.." && pwd)"
8+
9+
MEDIAMTX_VERSION="${MEDIAMTX_VERSION:-v1.13.1}"
10+
UNAME_ARCH="$(uname -m)"
11+
12+
case "${UNAME_ARCH}" in
13+
x86_64)
14+
DEFAULT_MEDIAMTX_ARCH="linux_amd64"
15+
;;
16+
aarch64|arm64)
17+
DEFAULT_MEDIAMTX_ARCH="linux_arm64v8"
18+
;;
19+
armv7l|armv7*)
20+
DEFAULT_MEDIAMTX_ARCH="linux_armv7"
21+
;;
22+
armv6l|armv6*)
23+
DEFAULT_MEDIAMTX_ARCH="linux_armv6"
24+
;;
25+
*)
26+
DEFAULT_MEDIAMTX_ARCH="linux_amd64"
27+
;;
28+
esac
29+
30+
MEDIAMTX_ARCH="${MEDIAMTX_ARCH:-${DEFAULT_MEDIAMTX_ARCH}}"
31+
MEDIAMTX_ARCHIVE="${SCRIPT_DIR}/mediamtx.tar.gz"
32+
MEDIAMTX_DIR="${MODULE_DIR}/mediamtx"
33+
MEDIAMTX_BIN="${MEDIAMTX_DIR}/mediamtx"
34+
MEDIAMTX_CFG_SRC="${SCRIPT_DIR}/mediamtx.yml"
35+
MEDIAMTX_CFG_DST="${MEDIAMTX_DIR}/mediamtx.yml"
36+
37+
FFMPEG_PID=""
38+
MEDIAMTX_PID=""
39+
STARTED_MEDIAMTX="0"
40+
41+
require_cmd () {
42+
if ! command -v "$1" >/dev/null 2>&1; then
43+
echo "Missing required command: $1" >&2
44+
exit 1
45+
fi
46+
}
47+
48+
wait_for_http () {
49+
local url="$1"
50+
local attempts="${2:-30}"
51+
local sleep_s="${3:-0.5}"
52+
local i
53+
54+
for ((i = 1; i <= attempts; i++)); do
55+
if curl -fsS --max-time 2 "$url" >/dev/null 2>&1; then
56+
return 0
57+
fi
58+
sleep "$sleep_s"
59+
done
60+
61+
return 1
62+
}
63+
64+
cleanup () {
65+
set +e
66+
67+
if [[ -n "${FFMPEG_PID}" ]]; then
68+
kill "${FFMPEG_PID}" >/dev/null 2>&1 || true
69+
wait "${FFMPEG_PID}" >/dev/null 2>&1 || true
70+
fi
71+
72+
if [[ "${STARTED_MEDIAMTX}" == "1" && -n "${MEDIAMTX_PID}" ]]; then
73+
kill "${MEDIAMTX_PID}" >/dev/null 2>&1 || true
74+
wait "${MEDIAMTX_PID}" >/dev/null 2>&1 || true
75+
fi
76+
}
77+
78+
trap cleanup EXIT INT TERM
79+
80+
require_cmd curl
81+
require_cmd tar
82+
require_cmd ffmpeg
83+
84+
mkdir -p "${MEDIAMTX_DIR}"
85+
86+
if [[ ! -x "${MEDIAMTX_BIN}" ]]; then
87+
if [[ ! -f "${MEDIAMTX_ARCHIVE}" ]]; then
88+
echo "Downloading MediaMTX ${MEDIAMTX_VERSION} (${MEDIAMTX_ARCH})..."
89+
curl -L -o "${MEDIAMTX_ARCHIVE}" "https://github.com/bluenviron/mediamtx/releases/download/${MEDIAMTX_VERSION}/mediamtx_${MEDIAMTX_VERSION}_${MEDIAMTX_ARCH}.tar.gz"
90+
fi
91+
92+
echo "Extracting MediaMTX..."
93+
tar -xzf "${MEDIAMTX_ARCHIVE}" -C "${MEDIAMTX_DIR}"
94+
fi
95+
96+
cp "${MEDIAMTX_CFG_SRC}" "${MEDIAMTX_CFG_DST}"
97+
98+
if curl -fsS --max-time 2 http://127.0.0.1:9997/v3/config/global/get >/dev/null 2>&1; then
99+
echo "Using existing MediaMTX instance on 127.0.0.1:9997"
100+
else
101+
echo "Starting local MediaMTX..."
102+
(
103+
cd "${MEDIAMTX_DIR}"
104+
./mediamtx "${MEDIAMTX_CFG_DST}"
105+
) &
106+
MEDIAMTX_PID="$!"
107+
STARTED_MEDIAMTX="1"
108+
109+
if ! wait_for_http "http://127.0.0.1:9997/v3/config/global/get" 40 0.5; then
110+
echo "MediaMTX did not become ready on port 9997" >&2
111+
exit 1
112+
fi
113+
fi
114+
115+
echo "Starting FFmpeg test publisher on rtsp://127.0.0.1:8554/test..."
116+
ffmpeg -nostdin -re \
117+
-f lavfi -i testsrc=size=640x480:rate=25 \
118+
-f lavfi -i sine=frequency=1000 \
119+
-pix_fmt yuv420p \
120+
-c:v libx264 -preset ultrafast -tune zerolatency -b:v 500k \
121+
-c:a aac -b:a 128k \
122+
-f rtsp rtsp://127.0.0.1:8554/test \
123+
-loglevel warning >/dev/null 2>&1 &
124+
FFMPEG_PID="$!"
125+
126+
sleep 1
127+
if ! kill -0 "${FFMPEG_PID}" >/dev/null 2>&1; then
128+
echo "FFmpeg test stream failed to start. Check your ffmpeg installation/logs." >&2
129+
exit 1
130+
fi
131+
132+
echo "WHEP endpoint expected at http://127.0.0.1:8889/test/whep"
133+
134+
if [[ "${RTSPSTREAM_SKIP_MM:-0}" == "1" ]]; then
135+
echo "RTSPSTREAM_SKIP_MM=1 set, not starting MagicMirror."
136+
exit 0
137+
fi
138+
139+
cd "${MM_DIR}"
140+
MM_CONFIG_FILE=modules/MMM-RTSPStream/demo.config.js node --run start:dev

scripts/setup_mediamtx.sh

Lines changed: 0 additions & 58 deletions
This file was deleted.

scripts/start_test_stream.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)