Skip to content

Commit

Permalink
feat: update negotiateSdp and WebRtcArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
johzzy committed Mar 13, 2024
1 parent 8e16a69 commit 670baa1
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 194 deletions.
7 changes: 4 additions & 3 deletions api/source/mk_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,11 @@ API_EXPORT void API_CALL mk_webrtc_get_answer_sdp2(void *user_data, on_user_data
std::string offer_str = offer;
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
auto args = std::make_shared<WebRtcArgsUrl>(url);
WebRtcPluginManager::Instance().getAnswerSdp(*session, type, *args,
[offer_str, session, ptr, cb](const WebRtcInterface &exchanger) mutable {
WebRtcPluginManager::Instance().negotiateSdp(*session, type, *args, [offer_str, session, ptr, cb, args](const WebRtcInterface &exchanger) mutable {
auto& handler = const_cast<WebRtcInterface &>(exchanger);
handler.setArgs(*args);
try {
auto sdp_answer = exchangeSdp(exchanger, offer_str);
auto sdp_answer = handler.getAnswerSdp(offer_str);
cb(ptr.get(), sdp_answer.data(), nullptr);
} catch (std::exception &ex) {
cb(ptr.get(), nullptr, ex.what());
Expand Down
88 changes: 37 additions & 51 deletions server/WebApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static HttpApi toApi(const function<void(API_ARGS_MAP_ASYNC)> &cb) {

//参数解析成map
auto args = getAllArgs(parser);
cb(sender, headerOut, HttpAllArgs<decltype(args)>(parser, args), val, invoker);
cb(sender, headerOut, ArgsMap(parser, args), val, invoker);
};
}

Expand Down Expand Up @@ -143,7 +143,7 @@ static HttpApi toApi(const function<void(API_ARGS_JSON_ASYNC)> &cb) {
Json::Reader reader;
reader.parse(parser.content(), args);

cb(sender, headerOut, HttpAllArgs<decltype(args)>(parser, args), val, invoker);
cb(sender, headerOut, ArgsJson(parser, args), val, invoker);
};
}

Expand All @@ -163,7 +163,7 @@ static HttpApi toApi(const function<void(API_ARGS_STRING_ASYNC)> &cb) {
Json::Value val;
val["code"] = API::Success;

cb(sender, headerOut, HttpAllArgs<string>(parser, (string &)parser.content()), val, invoker);
cb(sender, headerOut, ArgsString(parser, (string &)parser.content()), val, invoker);
};
}

Expand Down Expand Up @@ -656,13 +656,6 @@ void addStreamPusherProxy(const string &schema,
pusher->publish(url);
}

template <typename Type>
static void getArgsValue(const HttpAllArgs<ApiArgsType> &allArgs, const string &key, Type &value) {
auto val = allArgs[key];
if (!val.empty()) {
value = (Type)val;
}
}

/**
* 安装api接口
Expand Down Expand Up @@ -729,7 +722,7 @@ void installWebApi() {
CHECK_SECRET();
auto &ini = mINI::Instance();
int changed = API::Success;
for (auto &pr : allArgs.getArgs()) {
for (auto &pr : allArgs.args) {
if (ini.find(pr.first) == ini.end()) {
#if 1
//没有这个key
Expand Down Expand Up @@ -1087,7 +1080,7 @@ void installWebApi() {
CHECK_ARGS("vhost","app","stream","url");

mINI args;
for (auto &pr : allArgs.getArgs()) {
for (auto &pr : allArgs.args) {
args.emplace(pr.first, pr.second);
}

Expand Down Expand Up @@ -1184,7 +1177,7 @@ void installWebApi() {
//测试url http://127.0.0.1/index/api/downloadBin
api_regist("/index/api/downloadBin",[](API_ARGS_MAP_ASYNC){
CHECK_SECRET();
invoker.responseFile(allArgs.getParser().getHeader(),StrCaseMap(),exePath());
invoker.responseFile(allArgs.parser.getHeader(), StrCaseMap(), exePath());
});

#if defined(ENABLE_RTPPROXY)
Expand Down Expand Up @@ -1691,7 +1684,7 @@ void installWebApi() {

//截图存在,且未过期,那么返回之
res_old_snap = true;
responseSnap(path, allArgs.getParser().getHeader(), invoker);
responseSnap(path, allArgs.parser.getHeader(), invoker);
//中断遍历
return false;
});
Expand Down Expand Up @@ -1722,7 +1715,7 @@ void installWebApi() {
File::delete_file(new_snap);
rename(new_snap_tmp.data(), new_snap.data());
}
responseSnap(new_snap, allArgs.getParser().getHeader(), invoker, err_msg);
responseSnap(new_snap, allArgs.parser.getHeader(), invoker, err_msg);
});
});

Expand All @@ -1737,7 +1730,7 @@ void installWebApi() {
#ifdef ENABLE_WEBRTC
class WebRtcArgsImp : public WebRtcArgs {
public:
WebRtcArgsImp(const HttpAllArgs<string> &args, std::string session_id)
WebRtcArgsImp(const ArgsString &args, std::string session_id)
: _args(args)
, _session_id(std::move(session_id)) {}
~WebRtcArgsImp() override = default;
Expand All @@ -1755,40 +1748,32 @@ void installWebApi() {
CHECK_ARGS("app", "stream");

return StrPrinter << "rtc://" << _args["Host"] << "/" << _args["app"] << "/"
<< _args["stream"] << "?" << _args.getParser().params() + "&session=" + _session_id;
<< _args["stream"] << "?" << _args.parser.params() + "&session=" + _session_id;
}

private:
HttpAllArgs<string> _args;
ArgsString _args;
std::string _session_id;
};

api_regist("/index/api/webrtc",[](API_ARGS_STRING_ASYNC){
CHECK_ARGS("type");
auto type = allArgs["type"];
auto offer = allArgs.getArgs();
auto offer = allArgs.args;
CHECK(!offer.empty(), "http body(webrtc offer sdp) is empty");
std::string host = allArgs.getParser()["Host"];
std::string localIp = host.substr(0, host.find(':'));

auto isVaildIP = [](std::string ip)-> bool {
int a,b,c,d;
return sscanf(ip.c_str(),"%d.%d.%d.%d", &a, &b, &c, &d) == 4;
};
if (!isVaildIP(localIp) || localIp=="127.0.0.1") {
localIp = "";
}

auto &session = static_cast<Session&>(sender);
auto args = std::make_shared<WebRtcArgsImp>(allArgs, sender.getIdentifier());
WebRtcPluginManager::Instance().getAnswerSdp(static_cast<Session&>(sender), type, *args, [invoker, val, offer, headerOut, localIp](const WebRtcInterface &exchanger) mutable {
WebRtcPluginManager::Instance().negotiateSdp(session, type, *args, [invoker, val, offer, headerOut, args](const WebRtcInterface &exchanger) mutable {
//设置返回类型
headerOut["Content-Type"] = HttpFileManager::getContentType(".json");
//设置跨域
headerOut["Access-Control-Allow-Origin"] = "*";

auto& handler = const_cast<WebRtcInterface &>(exchanger);
handler.setArgs(*args);
try {
setLocalIp(exchanger,localIp);
val["sdp"] = exchangeSdp(exchanger, offer);
val["sdp"] = handler.getAnswerSdp(offer);
val["id"] = exchanger.getIdentifier();
val["type"] = "answer";
invoker(200, headerOut, val.toStyledString());
Expand All @@ -1802,34 +1787,35 @@ void installWebApi() {

static constexpr char delete_webrtc_url [] = "/index/api/delete_webrtc";
static auto whip_whep_func = [](const char *type, API_ARGS_STRING_ASYNC) {
auto offer = allArgs.getArgs();
auto offer = allArgs.args;
CHECK(!offer.empty(), "http body(webrtc offer sdp) is empty");

auto &session = static_cast<Session&>(sender);
auto location = std::string("http") + (session.overSsl() ? "s" : "") + "://" + allArgs["host"] + delete_webrtc_url;
auto location = std::string(session.overSsl() ? "https://" : "http://") + allArgs["host"] + delete_webrtc_url;
auto args = std::make_shared<WebRtcArgsImp>(allArgs, sender.getIdentifier());
WebRtcPluginManager::Instance().getAnswerSdp(session, type, *args,
[invoker, offer, headerOut, location](const WebRtcInterface &exchanger) mutable {
// 设置跨域
headerOut["Access-Control-Allow-Origin"] = "*";
try {
// 设置返回类型
headerOut["Content-Type"] = "application/sdp";
headerOut["Location"] = location + "?id=" + exchanger.getIdentifier() + "&token=" + exchanger.deleteRandStr();
invoker(201, headerOut, exchangeSdp(exchanger, offer));
} catch (std::exception &ex) {
headerOut["Content-Type"] = "text/plain";
invoker(406, headerOut, ex.what());
}
});
WebRtcPluginManager::Instance().negotiateSdp(session, type, *args, [invoker, offer, headerOut, location, args](const WebRtcInterface &exchanger) mutable {
// 设置跨域
headerOut["Access-Control-Allow-Origin"] = "*";
auto& handler = const_cast<WebRtcInterface &>(exchanger);
handler.setArgs(*args);
try {
// 设置返回类型
headerOut["Content-Type"] = "application/sdp";
headerOut["Location"] = location + "?id=" + exchanger.getIdentifier() + "&token=" + exchanger.deleteRandStr();
invoker(201, headerOut, handler.getAnswerSdp(offer));
} catch (std::exception &ex) {
headerOut["Content-Type"] = "text/plain";
invoker(406, headerOut, ex.what());
}
});
};

api_regist("/index/api/whip", [](API_ARGS_STRING_ASYNC) { whip_whep_func("push", API_ARGS_VALUE, invoker); });
api_regist("/index/api/whep", [](API_ARGS_STRING_ASYNC) { whip_whep_func("play", API_ARGS_VALUE, invoker); });

api_regist(delete_webrtc_url, [](API_ARGS_MAP_ASYNC) {
CHECK_ARGS("id", "token");
CHECK(allArgs.getParser().method() == "DELETE", "http method is not DELETE: " + allArgs.getParser().method());
CHECK(allArgs.parser.method() == "DELETE", "http method is not DELETE: " + allArgs.parser.method());
auto obj = WebRtcTransportManager::Instance().getItem(allArgs["id"]);
if (!obj) {
invoker(404, headerOut, "id not found");
Expand Down Expand Up @@ -1915,11 +1901,11 @@ void installWebApi() {
if (!save_name.empty()) {
res_header.emplace("Content-Disposition", "attachment;filename=\"" + save_name + "\"");
}
invoker.responseFile(allArgs.getParser().getHeader(), res_header, allArgs["file_path"]);
invoker.responseFile(allArgs.parser.getHeader(), res_header, allArgs["file_path"]);
}
};

bool flag = NOTICE_EMIT(BroadcastHttpAccessArgs, Broadcast::kBroadcastHttpAccess, allArgs.getParser(), file_path, false, file_invoker, sender);
bool flag = NOTICE_EMIT(BroadcastHttpAccessArgs, Broadcast::kBroadcastHttpAccess, allArgs.parser, file_path, false, file_invoker, sender);
if (!flag) {
// 文件下载鉴权事件无人监听,不允许下载
invoker(401, StrCaseMap {}, "None http access event listener");
Expand Down
69 changes: 13 additions & 56 deletions server/WebApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,71 +116,28 @@ std::string getValue(const mediakit::Parser &parser, Args &args, const First &fi
template<typename Args>
class HttpAllArgs {
public:
HttpAllArgs(const mediakit::Parser &parser, Args &args) {
_get_args = [&args]() {
return (void *) &args;
};
_get_parser = [&parser]() -> const mediakit::Parser & {
return parser;
};
_get_value = [](HttpAllArgs &that, const std::string &key) {
return getValue(that.getParser(), that.getArgs(), key);
};
_clone = [&](HttpAllArgs &that) {
that._get_args = [args]() {
return (void *) &args;
};
that._get_parser = [parser]() -> const mediakit::Parser & {
return parser;
};
that._get_value = [](HttpAllArgs &that, const std::string &key) {
return getValue(that.getParser(), that.getArgs(), key);
};
that._cache_able = true;
};
}
const mediakit::Parser& parser;
Args& args;

HttpAllArgs(const HttpAllArgs &that) {
if (that._cache_able) {
_get_args = that._get_args;
_get_parser = that._get_parser;
_get_value = that._get_value;
_cache_able = true;
} else {
that._clone(*this);
}
}
HttpAllArgs(const mediakit::Parser &parser, Args &args): parser(parser), args(args) {}

HttpAllArgs(const HttpAllArgs &that): parser(that.parser), args(that.args) {}

template<typename Key>
toolkit::variant operator[](const Key &key) const {
return (toolkit::variant)_get_value(*(HttpAllArgs*)this, key);
}

const mediakit::Parser &getParser() const {
return _get_parser();
}

Args &getArgs() {
return *((Args *) _get_args());
}

const Args &getArgs() const {
return *((Args *) _get_args());
return (toolkit::variant)getValue(parser, args, key);
}

private:
bool _cache_able = false;
std::function<void *() > _get_args;
std::function<const mediakit::Parser &() > _get_parser;
std::function<std::string(HttpAllArgs &that, const std::string &key)> _get_value;
std::function<void(HttpAllArgs &that) > _clone;
};

#define API_ARGS_MAP toolkit::SockInfo &sender, mediakit::HttpSession::KeyValue &headerOut, const HttpAllArgs<ApiArgsType> &allArgs, Json::Value &val
using ArgsMap = HttpAllArgs<ApiArgsType>;
using ArgsJson = HttpAllArgs<Json::Value>;
using ArgsString = HttpAllArgs<std::string>;

#define API_ARGS_MAP toolkit::SockInfo &sender, mediakit::HttpSession::KeyValue &headerOut, const ArgsMap &allArgs, Json::Value &val
#define API_ARGS_MAP_ASYNC API_ARGS_MAP, const mediakit::HttpSession::HttpResponseInvoker &invoker
#define API_ARGS_JSON toolkit::SockInfo &sender, mediakit::HttpSession::KeyValue &headerOut, const HttpAllArgs<Json::Value> &allArgs, Json::Value &val
#define API_ARGS_JSON toolkit::SockInfo &sender, mediakit::HttpSession::KeyValue &headerOut, const ArgsJson &allArgs, Json::Value &val
#define API_ARGS_JSON_ASYNC API_ARGS_JSON, const mediakit::HttpSession::HttpResponseInvoker &invoker
#define API_ARGS_STRING toolkit::SockInfo &sender, mediakit::HttpSession::KeyValue &headerOut, const HttpAllArgs<std::string> &allArgs, Json::Value &val
#define API_ARGS_STRING toolkit::SockInfo &sender, mediakit::HttpSession::KeyValue &headerOut, const ArgsString &allArgs, Json::Value &val
#define API_ARGS_STRING_ASYNC API_ARGS_STRING, const mediakit::HttpSession::HttpResponseInvoker &invoker
#define API_ARGS_VALUE sender, headerOut, allArgs, val

Expand Down
18 changes: 9 additions & 9 deletions src/Common/MediaSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ class MediaSourceEvent {
toolkit::Timer::Ptr _async_close_timer;
};


template <typename MAP, typename KEY, typename TYPE>
static void getArgsValue(const MAP &allArgs, const KEY &key, TYPE &value) {
auto val = ((MAP &)allArgs)[key];
if (!val.empty()) {
value = (TYPE)val;
}
}

class ProtocolOption {
public:
ProtocolOption();
Expand Down Expand Up @@ -243,15 +252,6 @@ class ProtocolOption {
GET_OPT_VALUE(stream_replace);
GET_OPT_VALUE(max_track);
}

private:
template <typename MAP, typename KEY, typename TYPE>
static void getArgsValue(const MAP &allArgs, const KEY &key, TYPE &value) {
auto val = ((MAP &)allArgs)[key];
if (!val.empty()) {
value = (TYPE)val;
}
}
};

//该对象用于拦截感兴趣的MediaSourceEvent事件
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct StrCaseCompare {

class StrCaseMap : public std::multimap<std::string, std::string, StrCaseCompare> {
public:
using Super = multimap<std::string, std::string, StrCaseCompare>;
using Super = std::multimap<std::string, std::string, StrCaseCompare>;

std::string &operator[](const std::string &k) {
auto it = find(k);
Expand Down
1 change: 0 additions & 1 deletion webrtc/WebRtcEchoTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class WebRtcEchoTest : public WebRtcTransportImp {
void onRtp(const char *buf, size_t len, uint64_t stamp_ms) override;
void onRtcp(const char *buf, size_t len) override;

void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) override {};
void onBeforeEncryptRtp(const char *buf, int &len, void *ctx) override {};
void onBeforeEncryptRtcp(const char *buf, int &len, void *ctx) override {};

Expand Down
1 change: 0 additions & 1 deletion webrtc/WebRtcPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class WebRtcPlayer : public WebRtcTransportImp {
void onStartWebRTC() override;
void onDestory() override;
void onRtcConfigure(RtcConfigure &configure) const override;
void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) override {};

private:
WebRtcPlayer(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const MediaInfo &info, bool preferred_tcp);
Expand Down
Loading

0 comments on commit 670baa1

Please sign in to comment.