 MediaMtx: C++ replacement for the MediaMTX parts la_mojo uses
New project Projects/MediaMtx, in MediaMTX's module layout (internal/core, conf,
stream, staticsources/rtsp, servers/webrtc, recorder, api, ...):
- RTSP pull of the cameras with live555 (H264, TCP/UDP), reconnect after errors or
when no data arrives for --readTimeout
- WebRTC WHEP on :8889 with libdatachannel, the MediaMTX URLs
(POST /stream_N/whep, PATCH/DELETE /stream_N/whep/<id>), so the dashboard works as is
- recording as fragmented MP4 segments (no transcoding), playable in browsers,
MediaMTX file names; a fragment is flushed every --recordPartDuration
- no mediamtx.yml: command line options + a paths file (SIGHUP reload) + the
v3 paths API (add / patch / replace / delete, written back to the file)
- log lines in the MediaMTX format that kernelHelperMediaMTX parses
Tested with a GStreamer RTSP camera and Chromium (WHEP, recordings), camera
loss / reconnect, API, SIGHUP; ASan + UBSan clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago  MediaMtx: C++ replacement for the MediaMTX parts la_mojo uses
New project Projects/MediaMtx, in MediaMTX's module layout (internal/core, conf,
stream, staticsources/rtsp, servers/webrtc, recorder, api, ...):
- RTSP pull of the cameras with live555 (H264, TCP/UDP), reconnect after errors or
when no data arrives for --readTimeout
- WebRTC WHEP on :8889 with libdatachannel, the MediaMTX URLs
(POST /stream_N/whep, PATCH/DELETE /stream_N/whep/<id>), so the dashboard works as is
- recording as fragmented MP4 segments (no transcoding), playable in browsers,
MediaMTX file names; a fragment is flushed every --recordPartDuration
- no mediamtx.yml: command line options + a paths file (SIGHUP reload) + the
v3 paths API (add / patch / replace / delete, written back to the file)
- log lines in the MediaMTX format that kernelHelperMediaMTX parses
Tested with a GStreamer RTSP camera and Chromium (WHEP, recordings), camera
loss / reconnect, API, SIGHUP; ASan + UBSan clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago  MediaMtx: C++ replacement for the MediaMTX parts la_mojo uses
New project Projects/MediaMtx, in MediaMTX's module layout (internal/core, conf,
stream, staticsources/rtsp, servers/webrtc, recorder, api, ...):
- RTSP pull of the cameras with live555 (H264, TCP/UDP), reconnect after errors or
when no data arrives for --readTimeout
- WebRTC WHEP on :8889 with libdatachannel, the MediaMTX URLs
(POST /stream_N/whep, PATCH/DELETE /stream_N/whep/<id>), so the dashboard works as is
- recording as fragmented MP4 segments (no transcoding), playable in browsers,
MediaMTX file names; a fragment is flushed every --recordPartDuration
- no mediamtx.yml: command line options + a paths file (SIGHUP reload) + the
v3 paths API (add / patch / replace / delete, written back to the file)
- log lines in the MediaMTX format that kernelHelperMediaMTX parses
Tested with a GStreamer RTSP camera and Chromium (WHEP, recordings), camera
loss / reconnect, API, SIGHUP; ASan + UBSan clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago |
|
# MediaMtx (C++)
A C++ replacement for the parts of [MediaMTX](https://github.com/bluenviron/mediamtx) that la_mojo uses:
* **RTSP pull** of the camera streams (live555), H264 video, TCP or UDP, automatic reconnect* **WebRTC / WHEP** playback in the browser on port 8889 (libdatachannel) - the same URLs as MediaMTX: `POST http://<host>:8889/stream_N/whep`, so `mojo_dashboard.js` works unchanged* **Recording** as fragmented MP4 segments (the camera's H264 as it is, no transcoding) that a browser plays with `<video src=...>`; same file names as MediaMTX: `recordings/stream_N/2026-12-02_16-58-35-953775.mp4`* a **control API** with the MediaMTX v3 URLs for paths (`/v3/paths/list`, `/v3/config/paths/add/...`)
Not included (MediaMTX has them): RTSP/RTMP/HLS/SRT servers, publishing, audio, H265, authentication,playback server, metrics. The source keeps MediaMTX's module layout, so they can be added the same way.
MediaMTX is MIT licensed (see `LICENSE.mediamtx`); this is a rewrite of its design, not a copy of its code.
## Layout
| MediaMtx (C++) | MediaMTX (Go) ||-----------------------------------------|--------------------------------|| `main.cpp` | `main.go` || `internal/core/core.*` | `internal/core/core.go` || `internal/core/pathManager.*`, `path.*` | `internal/core/path_manager.go`, `path.go` || `internal/conf/conf.*` | `internal/conf` || `internal/logger/logger.*` | `internal/logger` || `internal/stream/stream.*` | `internal/stream` || `internal/unit/unit.h` | `internal/unit` || `internal/staticsources/rtsp/rtspSource.*` | `internal/staticsources/rtsp` || `internal/servers/webrtc/*` | `internal/servers/webrtc` || `internal/recorder/*` | `internal/recorder` || `internal/api/api.*` | `internal/api` || `internal/protocols/httpp/httpServer.*` | `internal/protocols/httpp` || `internal/codecs/h264/h264.*` | `mediacommon/pkg/codecs/h264` |
Data flow: `RtspSource` -> `Path` -> `Stream` -> readers (`WebrtcSession`, `Recorder`). Every reader hasits own queue and thread (`AsyncReader`): a slow browser or disk never stalls the camera or the others;an overflowing reader drops units and continues at the next IDR.
## Build
Dependencies:
* live555 - the same as for la_mojo (`pkg-config live555`); old (`char volatile`) and new (`std::atomic_char`) event loop variables both build* libdatachannel >= 0.20 (not packaged in Ubuntu 24.04): ``` git clone --branch v0.22.6 https://github.com/paullouisageneau/libdatachannel cd libdatachannel && git submodule update --init --depth 1 deps/plog deps/libjuice sudo apt install libsrtp2-dev libusrsctp-dev libssl-dev cmake cmake -B build -DCMAKE_BUILD_TYPE=Release -DUSE_SYSTEM_SRTP=ON -DUSE_SYSTEM_USRSCTP=ON -DNO_EXAMPLES=ON -DNO_TESTS=ON -DNO_WEBSOCKET=ON cmake --build build -j8 && sudo cmake --install build && sudo ldconfig ```* OpenSSL* JSON: JsonCpp of dr_frucht (`lib/include/json`, `lib/data/json`), compiled in by the makefile
```make # Release/mediamtxmake CFG=Debug # Debug/mediamtx```Other library locations: `make LIVE555_CFLAGS=... LIVE555_LIBS=... DATACHANNEL_CFLAGS=-I... DATACHANNEL_LIBS="-L... -ldatachannel"`
## Run
No `mediamtx.yml`. Global settings are command line options (MediaMTX names), the streams are in apaths file:
```# name source options
stream_3 rtsp://user:pass@10.0.0.5:554/stream1 record=yes transport=tcpstream_4 rtsp://10.0.0.6/live record=no transport=udp```
```mediamtx --paths /srv/etc/mediamtx.paths --logFile mediamtx.log \ --recordPath ./recordings/%path/%Y-%m-%d_%H-%M-%S-%f --recordSegmentDuration 60s```
* `kill -HUP <pid>` reloads the paths file: only added / removed / changed paths restart* the API changes paths at once and writes the paths file: ``` curl -X POST -d '{"source":"rtsp://10.0.0.5/s1","record":true}' http://127.0.0.1:9997/v3/config/paths/add/stream_3 curl -X PATCH -d '{"record":false}' http://127.0.0.1:9997/v3/config/paths/patch/stream_3 curl -X DELETE http://127.0.0.1:9997/v3/config/paths/delete/stream_3 curl http://127.0.0.1:9997/v3/paths/list ```* `http://<host>:8889/stream_3/` is a small player page* `mediamtx --help` lists all options (WebRTC ICE servers, UDP port range, timeouts, ...)
Log lines have the MediaMTX format that `kernelHelperMediaMTX` parses, e.g.```2026/12/02 17:00:30 INF [path stream_3] [RTSP source] ready: 1 track (H264)2026/12/02 17:00:30 INF [path stream_3] stream is available and online, 1 track (H264 640x360)2026/12/02 17:00:20 ERR [path stream_3] [RTSP source] no data for 10 s```
## Network
* WebRTC media uses UDP ports 8189-8289 (`--webrtcUDPPorts`); open them like 8889/tcp.* Behind NAT give a STUN server (`--webrtcICEServers stun:stun.l.google.com:19302`), so the answer carries the public address.
## Tested
With a GStreamer RTSP test camera (H264 640x360, 25 fps) and Chromium:
* WHEP with the dashboard's code (offer without trickle) and with the player page: video plays; DELETE ends a session; a closed browser is detected (ICE timeout, ~25 s)* camera down: `no data for 10 s`, sessions closed, recording closed; camera back: reconnect, recording continues, the player page reconnects* recordings: every segment starts with an IDR, exact length (10 s = 250 frames), decodes with ffmpeg and plays to the end in Chromium, also the segment cut by a shutdown* API add / patch / delete, paths file written, SIGHUP reload* AddressSanitizer + UBSan build through all of the above: no reports
|