Browse Source

MediaMtx: use dr_frucht's JsonCpp instead of nlohmann/json

The control API now uses the JsonCpp 1.8.4 of dr_frucht (lib/include/json,
lib/data/json), which the makefile compiles in, like la_mojo does.
nlohmann/json is no longer needed; the README also no longer lists it as a
libdatachannel dependency (it is only used by libdatachannel's examples).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
feature/mediamtx-cpp
Claude 21 hours ago
parent
commit
39700129ec
Failed to extract signature
  1. 7
      Projects/MediaMtx/README.md
  2. 122
      Projects/MediaMtx/internal/api/api.cpp
  3. 16
      Projects/MediaMtx/makefile

7
Projects/MediaMtx/README.md

@ -46,11 +46,12 @@ Dependencies:
```
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 nlohmann-json3-dev libssl-dev cmake
cmake -B build -DCMAKE_BUILD_TYPE=Release -DUSE_SYSTEM_SRTP=ON -DUSE_SYSTEM_USRSCTP=ON -DUSE_SYSTEM_JSON=ON -DNO_EXAMPLES=ON -DNO_TESTS=ON -DNO_WEBSOCKET=ON
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, nlohmann-json (header only)
* OpenSSL
* JSON: JsonCpp of dr_frucht (`lib/include/json`, `lib/data/json`), compiled in by the makefile
```
make # Release/mediamtx

122
Projects/MediaMtx/internal/api/api.cpp

@ -2,11 +2,12 @@
#include <ctime>
#include <nlohmann/json.hpp>
#include <memory>
#include <sstream>
namespace mtx {
#include <json/json.h> // JsonCpp of dr_frucht (lib/include/json, lib/data/json)
using json = nlohmann::json;
namespace mtx {
namespace {
@ -24,70 +25,104 @@ std::string isoTime(int64_t us)
return tB;
}
json pathConfJson(const PathConf & p)
std::string toText(const Json::Value & v)
{
Json::StreamWriterBuilder tB;
tB["indentation"] = "";
return Json::writeString(tB, v);
}
bool parseJson(const std::string & text, Json::Value & out, std::string & err)
{
return json{{"name", p.name}, {"source", p.source}, {"record", p.record}, {"rtspTransport", p.transport == RtspTransport::Udp ? "udp" : "tcp"}};
Json::CharReaderBuilder tB;
std::unique_ptr<Json::CharReader> tR(tB.newCharReader());
return tR->parse(text.data(), text.data() + text.size(), &out, &err);
}
json pathInfoJson(const PathInfo & i)
Json::Value pathConfJson(const PathConf & p)
{
json j = {{"name", i.name},
{"confName", i.name},
{"source", {{"type", "rtspSource"}, {"id", ""}}},
{"ready", i.ready},
{"readyTime", i.ready ? json(isoTime(i.readySinceUs)) : json(nullptr)},
{"tracks", i.ready ? json::array({"H264"}) : json::array()},
{"bytesReceived", i.bytesReceived},
{"record", i.record},
{"readers", i.readers}};
Json::Value j(Json::objectValue);
j["name"] = p.name;
j["source"] = p.source;
j["record"] = p.record;
j["rtspTransport"] = p.transport == RtspTransport::Udp ? "udp" : "tcp";
return j;
}
Json::Value pathInfoJson(const PathInfo & i)
{
Json::Value j(Json::objectValue);
j["name"] = i.name;
j["confName"] = i.name;
j["source"]["type"] = "rtspSource";
j["source"]["id"] = "";
j["ready"] = i.ready;
j["readyTime"] = i.ready ? Json::Value(isoTime(i.readySinceUs)) : Json::Value(Json::nullValue);
j["tracks"] = Json::Value(Json::arrayValue);
if (i.ready)
{
j["tracks"].append("H264");
j["width"] = i.desc.width;
j["height"] = i.desc.height;
}
j["bytesReceived"] = (Json::UInt64) i.bytesReceived;
j["record"] = i.record;
j["readers"] = (Json::UInt64) i.readers;
return j;
}
Json::Value listJson(const Json::Value & items)
{
Json::Value j(Json::objectValue);
j["itemCount"] = items.size();
j["pageCount"] = 1;
j["items"] = items;
return j;
}
void error(HttpResponse & res, int status, const std::string & msg)
{
Json::Value j(Json::objectValue);
j["error"] = msg;
res.status = status;
res.setHeader("Content-Type", "application/json");
res.body = json{{"error", msg}}.dump();
res.body = toText(j);
}
void ok(HttpResponse & res, const json & body)
void ok(HttpResponse & res, const Json::Value & body)
{
res.status = 200;
res.setHeader("Content-Type", "application/json");
res.body = body.dump();
res.body = toText(body);
}
// fields of a path configuration from JSON into pc
bool applyFields(const json & j, PathConf & pc, std::string & err)
bool applyFields(const Json::Value & j, PathConf & pc, std::string & err)
{
if (! j.is_object())
if (! j.isObject())
{
err = "body must be a JSON object";
return false;
}
for (auto it = j.begin(); it != j.end(); ++it)
for (const std::string & k : j.getMemberNames())
{
if (it.key() == "source" && it->is_string())
const Json::Value & v = j[k];
if (k == "source" && v.isString())
{
pc.source = it->get<std::string>();
pc.source = v.asString();
}
else if (it.key() == "record" && it->is_boolean())
else if (k == "record" && v.isBool())
{
pc.record = it->get<bool>();
pc.record = v.asBool();
}
else if (it.key() == "rtspTransport" && it->is_string())
else if (k == "rtspTransport" && v.isString())
{
std::string v = it->get<std::string>();
if (v == "tcp" || v == "automatic")
std::string t = v.asString();
if (t == "tcp" || t == "automatic")
{
pc.transport = RtspTransport::Tcp;
}
else if (v == "udp")
else if (t == "udp")
{
pc.transport = RtspTransport::Udp;
}
@ -97,13 +132,13 @@ bool applyFields(const json & j, PathConf & pc, std::string & err)
return false;
}
}
else if (it.key() == "name")
else if (k == "name")
{
continue;
}
else
{
err = "unsupported field '" + it.key() + "'";
err = "unsupported field '" + k + "'";
return false;
}
}
@ -146,12 +181,12 @@ void Api::handle(const HttpRequest & req, HttpResponse & res)
if (req.method == "GET" && p == "/v3/paths/list")
{
json tItems = json::array();
Json::Value tItems(Json::arrayValue);
for (const PathInfo & i : m_pPaths->list())
{
tItems.push_back(pathInfoJson(i));
tItems.append(pathInfoJson(i));
}
ok(res, json{{"itemCount", tItems.size()}, {"pageCount", 1}, {"items", tItems}});
ok(res, listJson(tItems));
return;
}
if (req.method == "GET" && starts("/v3/paths/get/"))
@ -167,12 +202,12 @@ void Api::handle(const HttpRequest & req, HttpResponse & res)
}
if (req.method == "GET" && p == "/v3/config/paths/list")
{
json tItems = json::array();
Json::Value tItems(Json::arrayValue);
for (const PathConf & c : m_pPaths->confs())
{
tItems.push_back(pathConfJson(c));
tItems.append(pathConfJson(c));
}
ok(res, json{{"itemCount", tItems.size()}, {"pageCount", 1}, {"items", tItems}});
ok(res, listJson(tItems));
return;
}
if (req.method == "GET" && starts("/v3/config/paths/get/"))
@ -222,16 +257,13 @@ void Api::handle(const HttpRequest & req, HttpResponse & res)
}
std::vector<PathConf> tConfs = m_pPaths->confs();
auto tIt = std::find_if(tConfs.begin(), tConfs.end(), [&tName](const PathConf & c) { return c.name == tName; });
json tBody;
if (tOp != DEL)
Json::Value tBody(Json::objectValue);
if (tOp != DEL && ! req.body.empty())
{
try
{
tBody = json::parse(req.body.empty() ? std::string("{}") : req.body);
}
catch (const std::exception & e)
std::string tParseErr;
if (! parseJson(req.body, tBody, tParseErr))
{
error(res, 400, std::string("invalid JSON: ") + e.what());
error(res, 400, "invalid JSON: " + tParseErr);
return;
}
}

16
Projects/MediaMtx/makefile

@ -1,7 +1,7 @@
# MediaMtx (C++) - make CFG=Release (default) or make CFG=Debug
#
# Needs: live555 (pkg-config live555, as for la_mojo), libdatachannel (>= 0.20, installed in /usr/local),
# OpenSSL, nlohmann-json (header only).
# OpenSSL. JSON: JsonCpp of dr_frucht (../../lib/include/json, ../../lib/data/json).
# Other locations: make LIVE555_CFLAGS=... LIVE555_LIBS=... DATACHANNEL_CFLAGS=-I... DATACHANNEL_LIBS="-L... -ldatachannel"
CFG ?= Release
@ -19,7 +19,8 @@ DATACHANNEL_CFLAGS ?=
DATACHANNEL_LIBS ?= -ldatachannel
CXX ?= g++
CXXFLAGS += -std=c++17 -Wall -Wextra -Wno-unused-parameter $(OPT) $(LIVE555_CFLAGS) $(DATACHANNEL_CFLAGS)
DFSL = ../..
CXXFLAGS += -std=c++17 -Wall -Wextra -Wno-unused-parameter $(OPT) $(LIVE555_CFLAGS) $(DATACHANNEL_CFLAGS) -I$(DFSL)/lib/include
LIBS = $(DATACHANNEL_LIBS) $(LIVE555_LIBS) -lssl -lcrypto -lpthread
TARGET = $(OUTDIR)/mediamtx
@ -40,7 +41,12 @@ SRCS = main.cpp \
internal/servers/webrtc/webrtcServer.cpp \
internal/api/api.cpp
OBJS = $(patsubst %.cpp,$(OUTDIR)/obj/%.o,$(SRCS))
# JsonCpp from dr_frucht
JSON_SRCS = $(DFSL)/lib/data/json/json_reader.cpp \
$(DFSL)/lib/data/json/json_value.cpp \
$(DFSL)/lib/data/json/json_writer.cpp
OBJS = $(patsubst %.cpp,$(OUTDIR)/obj/%.o,$(SRCS)) $(patsubst $(DFSL)/lib/data/json/%.cpp,$(OUTDIR)/obj/jsoncpp/%.o,$(JSON_SRCS))
DEPS = $(OBJS:.o=.d)
all: $(TARGET)
@ -52,6 +58,10 @@ $(OUTDIR)/obj/%.o: %.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@
$(OUTDIR)/obj/jsoncpp/%.o: $(DFSL)/lib/data/json/%.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -w -MMD -MP -c $< -o $@
clean:
rm -rf $(OUTDIR)

Loading…
Cancel
Save