You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
5.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. # MediaMtx (C++)
  2. A C++ replacement for the parts of [MediaMTX](https://github.com/bluenviron/mediamtx) that la_mojo uses:
  3. * **RTSP pull** of the camera streams (live555), H264 video, TCP or UDP, automatic reconnect
  4. * **WebRTC / WHEP** playback in the browser on port 8889 (libdatachannel) - the same URLs as MediaMTX:
  5. `POST http://<host>:8889/stream_N/whep`, so `mojo_dashboard.js` works unchanged
  6. * **Recording** as fragmented MP4 segments (the camera's H264 as it is, no transcoding) that a browser
  7. plays with `<video src=...>`; same file names as MediaMTX: `recordings/stream_N/2026-12-02_16-58-35-953775.mp4`
  8. * a **control API** with the MediaMTX v3 URLs for paths (`/v3/paths/list`, `/v3/config/paths/add/...`)
  9. Not included (MediaMTX has them): RTSP/RTMP/HLS/SRT servers, publishing, audio, H265, authentication,
  10. playback server, metrics. The source keeps MediaMTX's module layout, so they can be added the same way.
  11. MediaMTX is MIT licensed (see `LICENSE.mediamtx`); this is a rewrite of its design, not a copy of its code.
  12. ## Layout
  13. | MediaMtx (C++) | MediaMTX (Go) |
  14. |-----------------------------------------|--------------------------------|
  15. | `main.cpp` | `main.go` |
  16. | `internal/core/core.*` | `internal/core/core.go` |
  17. | `internal/core/pathManager.*`, `path.*` | `internal/core/path_manager.go`, `path.go` |
  18. | `internal/conf/conf.*` | `internal/conf` |
  19. | `internal/logger/logger.*` | `internal/logger` |
  20. | `internal/stream/stream.*` | `internal/stream` |
  21. | `internal/unit/unit.h` | `internal/unit` |
  22. | `internal/staticsources/rtsp/rtspSource.*` | `internal/staticsources/rtsp` |
  23. | `internal/servers/webrtc/*` | `internal/servers/webrtc` |
  24. | `internal/recorder/*` | `internal/recorder` |
  25. | `internal/api/api.*` | `internal/api` |
  26. | `internal/protocols/httpp/httpServer.*` | `internal/protocols/httpp` |
  27. | `internal/codecs/h264/h264.*` | `mediacommon/pkg/codecs/h264` |
  28. Data flow: `RtspSource` -> `Path` -> `Stream` -> readers (`WebrtcSession`, `Recorder`). Every reader has
  29. its own queue and thread (`AsyncReader`): a slow browser or disk never stalls the camera or the others;
  30. an overflowing reader drops units and continues at the next IDR.
  31. ## Build
  32. Dependencies:
  33. * live555 - the same as for la_mojo (`pkg-config live555`); old (`char volatile`) and new
  34. (`std::atomic_char`) event loop variables both build
  35. * libdatachannel >= 0.20 (not packaged in Ubuntu 24.04):
  36. ```
  37. git clone --branch v0.22.6 https://github.com/paullouisageneau/libdatachannel
  38. cd libdatachannel && git submodule update --init --depth 1 deps/plog deps/libjuice
  39. sudo apt install libsrtp2-dev libusrsctp-dev libssl-dev cmake
  40. cmake -B build -DCMAKE_BUILD_TYPE=Release -DUSE_SYSTEM_SRTP=ON -DUSE_SYSTEM_USRSCTP=ON -DNO_EXAMPLES=ON -DNO_TESTS=ON -DNO_WEBSOCKET=ON
  41. cmake --build build -j8 && sudo cmake --install build && sudo ldconfig
  42. ```
  43. * OpenSSL
  44. * JSON: JsonCpp of dr_frucht (`lib/include/json`, `lib/data/json`), compiled in by the makefile
  45. ```
  46. make # Release/mediamtx
  47. make CFG=Debug # Debug/mediamtx
  48. ```
  49. Other library locations: `make LIVE555_CFLAGS=... LIVE555_LIBS=... DATACHANNEL_CFLAGS=-I... DATACHANNEL_LIBS="-L... -ldatachannel"`
  50. ## Run
  51. No `mediamtx.yml`. Global settings are command line options (MediaMTX names), the streams are in a
  52. paths file:
  53. ```
  54. # name source options
  55. stream_3 rtsp://user:pass@10.0.0.5:554/stream1 record=yes transport=tcp
  56. stream_4 rtsp://10.0.0.6/live record=no transport=udp
  57. ```
  58. ```
  59. mediamtx --paths /srv/etc/mediamtx.paths --logFile mediamtx.log \
  60. --recordPath ./recordings/%path/%Y-%m-%d_%H-%M-%S-%f --recordSegmentDuration 60s
  61. ```
  62. * `kill -HUP <pid>` reloads the paths file: only added / removed / changed paths restart
  63. * the API changes paths at once and writes the paths file:
  64. ```
  65. 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
  66. curl -X PATCH -d '{"record":false}' http://127.0.0.1:9997/v3/config/paths/patch/stream_3
  67. curl -X DELETE http://127.0.0.1:9997/v3/config/paths/delete/stream_3
  68. curl http://127.0.0.1:9997/v3/paths/list
  69. ```
  70. * `http://<host>:8889/stream_3/` is a small player page
  71. * `mediamtx --help` lists all options (WebRTC ICE servers, UDP port range, timeouts, ...)
  72. Log lines have the MediaMTX format that `kernelHelperMediaMTX` parses, e.g.
  73. ```
  74. 2026/12/02 17:00:30 INF [path stream_3] [RTSP source] ready: 1 track (H264)
  75. 2026/12/02 17:00:30 INF [path stream_3] stream is available and online, 1 track (H264 640x360)
  76. 2026/12/02 17:00:20 ERR [path stream_3] [RTSP source] no data for 10 s
  77. ```
  78. ## Network
  79. * WebRTC media uses UDP ports 8189-8289 (`--webrtcUDPPorts`); open them like 8889/tcp.
  80. * Behind NAT give a STUN server (`--webrtcICEServers stun:stun.l.google.com:19302`), so the answer
  81. carries the public address.
  82. ## Tested
  83. With a GStreamer RTSP test camera (H264 640x360, 25 fps) and Chromium:
  84. * WHEP with the dashboard's code (offer without trickle) and with the player page: video plays;
  85. DELETE ends a session; a closed browser is detected (ICE timeout, ~25 s)
  86. * camera down: `no data for 10 s`, sessions closed, recording closed; camera back: reconnect, recording
  87. continues, the player page reconnects
  88. * recordings: every segment starts with an IDR, exact length (10 s = 250 frames), decodes with ffmpeg
  89. and plays to the end in Chromium, also the segment cut by a shutdown
  90. * API add / patch / delete, paths file written, SIGHUP reload
  91. * AddressSanitizer + UBSan build through all of the above: no reports