 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 |
|
#include "pathManager.h"
namespace mtx {
PathManager::PathManager(const Conf & global) : LogParent("[path manager] "), m_global(global) {}
PathManager::~PathManager(){ closeAll();}
void PathManager::applyPaths(const std::vector<PathConf> & paths){ std::vector<std::shared_ptr<Path>> tStop; std::vector<std::shared_ptr<Path>> tStart; { std::lock_guard<std::mutex> lock(m_mutex); for (auto it = m_paths.begin(); it != m_paths.end();) { const PathConf * tNew = nullptr; for (const PathConf & p : paths) { if (p.name == it->first) { tNew = &p; } } if (tNew == nullptr || *tNew != it->second->conf()) { tStop.push_back(it->second); it = m_paths.erase(it); } else { ++it; } } for (const PathConf & p : paths) { if (m_paths.find(p.name) == m_paths.end()) { auto tPath = std::make_shared<Path>(p, m_global); m_paths[p.name] = tPath; tStart.push_back(tPath); } } } // outside the lock: stopping joins the source thread
for (auto & p : tStop) { log(LogLevel::Info, "removing path %s", p->conf().name.c_str()); p->stop(); } for (auto & p : tStart) { log(LogLevel::Info, "adding path %s (source %s%s)", p->conf().name.c_str(), hideCredentials(p->conf().source).c_str(), p->conf().record ? ", recording" : ""); p->start(); }}
void PathManager::closeAll(){ std::map<std::string, std::shared_ptr<Path>> tAll; { std::lock_guard<std::mutex> lock(m_mutex); tAll.swap(m_paths); } for (auto & p : tAll) { p.second->stop(); }}
std::shared_ptr<Path> PathManager::find(const std::string & name) const{ std::lock_guard<std::mutex> lock(m_mutex); auto it = m_paths.find(name); return it == m_paths.end() ? nullptr : it->second;}
std::vector<PathInfo> PathManager::list() const{ std::vector<std::shared_ptr<Path>> tAll; { std::lock_guard<std::mutex> lock(m_mutex); for (auto & p : m_paths) { tAll.push_back(p.second); } } std::vector<PathInfo> tOut; for (auto & p : tAll) { tOut.push_back(p->info()); } return tOut;}
std::vector<PathConf> PathManager::confs() const{ std::lock_guard<std::mutex> lock(m_mutex); std::vector<PathConf> tOut; for (auto & p : m_paths) { tOut.push_back(p.second->conf()); } return tOut;}
} // namespace mtx
|