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.
322 lines
6.8 KiB
322 lines
6.8 KiB
#include "api.h"
|
|
|
|
#include <ctime>
|
|
|
|
#include <memory>
|
|
#include <sstream>
|
|
|
|
#include <json/json.h> // JsonCpp of dr_frucht (lib/include/json, lib/data/json)
|
|
|
|
namespace mtx {
|
|
|
|
namespace {
|
|
|
|
std::string isoTime(int64_t us)
|
|
{
|
|
if (us <= 0)
|
|
{
|
|
return std::string();
|
|
}
|
|
time_t tSec = (time_t) (us / 1000000);
|
|
struct tm tTm;
|
|
gmtime_r(&tSec, &tTm);
|
|
char tB[40];
|
|
strftime(tB, sizeof(tB), "%Y-%m-%dT%H:%M:%SZ", &tTm);
|
|
return tB;
|
|
}
|
|
|
|
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)
|
|
{
|
|
Json::CharReaderBuilder tB;
|
|
std::unique_ptr<Json::CharReader> tR(tB.newCharReader());
|
|
return tR->parse(text.data(), text.data() + text.size(), &out, &err);
|
|
}
|
|
|
|
Json::Value pathConfJson(const PathConf & p)
|
|
{
|
|
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 = toText(j);
|
|
}
|
|
|
|
void ok(HttpResponse & res, const Json::Value & body)
|
|
{
|
|
res.status = 200;
|
|
res.setHeader("Content-Type", "application/json");
|
|
res.body = toText(body);
|
|
}
|
|
|
|
// fields of a path configuration from JSON into pc
|
|
bool applyFields(const Json::Value & j, PathConf & pc, std::string & err)
|
|
{
|
|
if (! j.isObject())
|
|
{
|
|
err = "body must be a JSON object";
|
|
return false;
|
|
}
|
|
for (const std::string & k : j.getMemberNames())
|
|
{
|
|
const Json::Value & v = j[k];
|
|
if (k == "source" && v.isString())
|
|
{
|
|
pc.source = v.asString();
|
|
}
|
|
else if (k == "record" && v.isBool())
|
|
{
|
|
pc.record = v.asBool();
|
|
}
|
|
else if (k == "rtspTransport" && v.isString())
|
|
{
|
|
std::string t = v.asString();
|
|
if (t == "tcp" || t == "automatic")
|
|
{
|
|
pc.transport = RtspTransport::Tcp;
|
|
}
|
|
else if (t == "udp")
|
|
{
|
|
pc.transport = RtspTransport::Udp;
|
|
}
|
|
else
|
|
{
|
|
err = "rtspTransport must be tcp or udp";
|
|
return false;
|
|
}
|
|
}
|
|
else if (k == "name")
|
|
{
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
err = "unsupported field '" + k + "'";
|
|
return false;
|
|
}
|
|
}
|
|
if (pc.source.compare(0, 7, "rtsp://") != 0)
|
|
{
|
|
err = "source must be an rtsp:// URL";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Api::Api(const Conf & global, PathManager * paths, ApplyFn apply) : LogParent("[API] "), m_global(global), m_pPaths(paths), m_apply(std::move(apply)), m_http(this) {}
|
|
|
|
Api::~Api()
|
|
{
|
|
stop();
|
|
}
|
|
|
|
bool Api::start(std::string & err)
|
|
{
|
|
if (! m_http.start(m_global.apiAddress, [this](const HttpRequest & q, HttpResponse & r) { handle(q, r); }, err))
|
|
{
|
|
return false;
|
|
}
|
|
log(LogLevel::Info, "listener opened on %s", m_global.apiAddress.c_str());
|
|
return true;
|
|
}
|
|
|
|
void Api::stop()
|
|
{
|
|
m_http.stop();
|
|
}
|
|
|
|
void Api::handle(const HttpRequest & req, HttpResponse & res)
|
|
{
|
|
const std::string & p = req.path;
|
|
auto starts = [&p](const char * prefix) { return p.compare(0, strlen(prefix), prefix) == 0; };
|
|
|
|
if (req.method == "GET" && p == "/v3/paths/list")
|
|
{
|
|
Json::Value tItems(Json::arrayValue);
|
|
for (const PathInfo & i : m_pPaths->list())
|
|
{
|
|
tItems.append(pathInfoJson(i));
|
|
}
|
|
ok(res, listJson(tItems));
|
|
return;
|
|
}
|
|
if (req.method == "GET" && starts("/v3/paths/get/"))
|
|
{
|
|
auto tPath = m_pPaths->find(p.substr(strlen("/v3/paths/get/")));
|
|
if (! tPath)
|
|
{
|
|
error(res, 404, "path not found");
|
|
return;
|
|
}
|
|
ok(res, pathInfoJson(tPath->info()));
|
|
return;
|
|
}
|
|
if (req.method == "GET" && p == "/v3/config/paths/list")
|
|
{
|
|
Json::Value tItems(Json::arrayValue);
|
|
for (const PathConf & c : m_pPaths->confs())
|
|
{
|
|
tItems.append(pathConfJson(c));
|
|
}
|
|
ok(res, listJson(tItems));
|
|
return;
|
|
}
|
|
if (req.method == "GET" && starts("/v3/config/paths/get/"))
|
|
{
|
|
auto tPath = m_pPaths->find(p.substr(strlen("/v3/config/paths/get/")));
|
|
if (! tPath)
|
|
{
|
|
error(res, 404, "path not found");
|
|
return;
|
|
}
|
|
ok(res, pathConfJson(tPath->conf()));
|
|
return;
|
|
}
|
|
|
|
// changes
|
|
std::string tName;
|
|
enum { NONE, ADD, PATCH, REPLACE, DEL } tOp = NONE;
|
|
if (req.method == "POST" && starts("/v3/config/paths/add/"))
|
|
{
|
|
tOp = ADD;
|
|
tName = p.substr(strlen("/v3/config/paths/add/"));
|
|
}
|
|
else if (req.method == "PATCH" && starts("/v3/config/paths/patch/"))
|
|
{
|
|
tOp = PATCH;
|
|
tName = p.substr(strlen("/v3/config/paths/patch/"));
|
|
}
|
|
else if (req.method == "POST" && starts("/v3/config/paths/replace/"))
|
|
{
|
|
tOp = REPLACE;
|
|
tName = p.substr(strlen("/v3/config/paths/replace/"));
|
|
}
|
|
else if (req.method == "DELETE" && starts("/v3/config/paths/delete/"))
|
|
{
|
|
tOp = DEL;
|
|
tName = p.substr(strlen("/v3/config/paths/delete/"));
|
|
}
|
|
if (tOp == NONE)
|
|
{
|
|
error(res, 404, "not found");
|
|
return;
|
|
}
|
|
if (! validPathName(tName))
|
|
{
|
|
error(res, 400, "invalid path name");
|
|
return;
|
|
}
|
|
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::Value tBody(Json::objectValue);
|
|
if (tOp != DEL && ! req.body.empty())
|
|
{
|
|
std::string tParseErr;
|
|
if (! parseJson(req.body, tBody, tParseErr))
|
|
{
|
|
error(res, 400, "invalid JSON: " + tParseErr);
|
|
return;
|
|
}
|
|
}
|
|
std::string tErr;
|
|
if (tOp == ADD)
|
|
{
|
|
if (tIt != tConfs.end())
|
|
{
|
|
error(res, 400, "path already exists");
|
|
return;
|
|
}
|
|
PathConf tPc;
|
|
tPc.name = tName;
|
|
if (! applyFields(tBody, tPc, tErr))
|
|
{
|
|
error(res, 400, tErr);
|
|
return;
|
|
}
|
|
tConfs.push_back(tPc);
|
|
}
|
|
else
|
|
{
|
|
if (tIt == tConfs.end())
|
|
{
|
|
error(res, 404, "path not found");
|
|
return;
|
|
}
|
|
if (tOp == DEL)
|
|
{
|
|
tConfs.erase(tIt);
|
|
}
|
|
else
|
|
{
|
|
PathConf tPc;
|
|
if (tOp == PATCH)
|
|
{
|
|
tPc = *tIt;
|
|
}
|
|
tPc.name = tName;
|
|
if (! applyFields(tBody, tPc, tErr))
|
|
{
|
|
error(res, 400, tErr);
|
|
return;
|
|
}
|
|
*tIt = tPc;
|
|
}
|
|
}
|
|
if (! m_apply(tConfs, tErr))
|
|
{
|
|
error(res, 500, tErr);
|
|
return;
|
|
}
|
|
res.status = 200;
|
|
}
|
|
|
|
} // namespace mtx
|