 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 "path.h"
#include "../recorder/recorder.h"
namespace mtx {
Path::Path(const PathConf & conf, const Conf & global) : LogParent("[path " + conf.name + "] "), m_conf(conf), m_global(global) {}
Path::~Path(){ stop();}
void Path::start(){ m_pSource.reset(new RtspSource(m_conf, m_global, this)); m_pSource->start();}
void Path::stop(){ if (m_pSource) { m_pSource->stop(); // calls sourceSetNotReady() if it was ready
m_pSource.reset(); }}
PathInfo Path::info() const{ std::lock_guard<std::mutex> lock(m_mutex); PathInfo tI; tI.name = m_conf.name; tI.source = m_conf.source; tI.record = m_conf.record; tI.ready = (m_pStream != nullptr); tI.readySinceUs = m_tReadySinceUs; tI.readers = m_readers.size(); if (m_pStream) { tI.desc = m_pStream->desc(); tI.bytesReceived = m_pStream->bytesReceived(); } return tI;}
bool Path::addReader(PathReader * r, StreamDesc & desc, std::string & err){ std::lock_guard<std::mutex> lock(m_mutex); if (! m_pStream) { err = "no stream is available on path '" + m_conf.name + "'"; return false; } desc = m_pStream->desc(); m_readers.insert(r); m_pStream->addReader(r); return true;}
void Path::removeReader(PathReader * r){ std::lock_guard<std::mutex> lock(m_mutex); if (m_readers.erase(r) > 0 && m_pStream) { m_pStream->removeReader(r); }}
void Path::sourceSetReady(const StreamDesc & desc){ std::lock_guard<std::mutex> lock(m_mutex); m_pStream = std::make_shared<Stream>(desc); m_tReadySinceUs = nowUs(); log(LogLevel::Info, "stream is available and online, 1 track (H264 %dx%d)", desc.width, desc.height); if (m_conf.record) { m_pRecorder.reset(new Recorder(m_conf.name, m_global, m_pStream, this)); m_pRecorder->start(); }}
void Path::sourceSetNotReady(){ std::unique_ptr<Recorder> tRec; { std::lock_guard<std::mutex> lock(m_mutex); if (! m_pStream) { return; } for (PathReader * r : m_readers) { m_pStream->removeReader(r); r->pathNotReady(); } m_readers.clear(); tRec = std::move(m_pRecorder); m_pStream.reset(); m_tReadySinceUs = 0; } tRec.reset(); // closes the segment
log(LogLevel::Info, "stream is not available anymore");}
void Path::sourceWriteUnit(const UnitPtr & unit){ std::shared_ptr<Stream> tStream; { std::lock_guard<std::mutex> lock(m_mutex); tStream = m_pStream; } if (tStream) { tStream->writeUnit(unit); }}
} // namespace mtx
|