 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++) - small HTTP/1.1 server (MediaMTX: internal/protocols/httpp) Used by the WebRTC (WHEP) server and the control API. One thread per connection, keep-alive, Content-Length bodies only (no chunked uploads), small requests (signalling / API).*/#pragma once
#include <atomic>
#include <functional>
#include <map>
#include <mutex>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include "../../logger/logger.h"
namespace mtx {
struct HttpRequest{ std::string method; std::string path; // decoded, without the query
std::string query; std::map<std::string, std::string> headers; // lower case names
std::string body; std::string remoteAddr;
std::string header(const std::string & lowerName) const { auto it = headers.find(lowerName); return it == headers.end() ? std::string() : it->second; }};
struct HttpResponse{ int status = 200; std::vector<std::pair<std::string, std::string>> headers; std::string body;
void setHeader(const std::string & name, const std::string & value) { headers.emplace_back(name, value); }};
class HttpServer{ public: typedef std::function<void(const HttpRequest &, HttpResponse &)> Handler;
explicit HttpServer(const LogParent * parent) : m_pParent(parent) {} ~HttpServer() { stop(); }
bool start(const std::string & address, Handler handler, std::string & err); void stop();
private: void acceptLoop(); void serveConnection(int fd, std::string remote);
const LogParent * m_pParent; Handler m_handler; int m_nListenFd = -1; int m_aWake[2] = {-1, -1}; std::atomic<bool> m_bRun{false}; std::atomic<int> m_nConnections{0}; std::thread m_thread; std::mutex m_connMutex; std::vector<int> m_vConnFds;};
const char * httpStatusText(int status);
} // namespace mtx
|