Merge branch 'master' of github.com:johzzy/ZLMediaKit
This commit is contained in:
commit
f81f62c8ff
|
|
@ -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 {
|
||||
setWebRtcArgs(*args, exchanger);
|
||||
auto& handler = const_cast<WebRtcInterface &>(exchanger);
|
||||
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());
|
||||
|
|
|
|||
|
|
@ -119,7 +119,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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -147,7 +147,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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -167,7 +167,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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -662,13 +662,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接口
|
||||
|
|
@ -735,7 +728,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
|
||||
|
|
@ -1093,7 +1086,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);
|
||||
}
|
||||
|
||||
|
|
@ -1190,7 +1183,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)
|
||||
|
|
@ -1697,7 +1690,7 @@ void installWebApi() {
|
|||
|
||||
//截图存在,且未过期,那么返回之
|
||||
res_old_snap = true;
|
||||
responseSnap(path, allArgs.getParser().getHeader(), invoker);
|
||||
responseSnap(path, allArgs.parser.getHeader(), invoker);
|
||||
//中断遍历
|
||||
return false;
|
||||
});
|
||||
|
|
@ -1728,7 +1721,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);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -1743,7 +1736,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;
|
||||
|
|
@ -1761,40 +1754,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"] = "*";
|
||||
|
||||
setWebRtcArgs(*args, exchanger);
|
||||
auto& handler = const_cast<WebRtcInterface &>(exchanger);
|
||||
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());
|
||||
|
|
@ -1808,21 +1793,22 @@ 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 {
|
||||
WebRtcPluginManager::Instance().negotiateSdp(session, type, *args, [invoker, offer, headerOut, location, args](const WebRtcInterface &exchanger) mutable {
|
||||
// 设置跨域
|
||||
headerOut["Access-Control-Allow-Origin"] = "*";
|
||||
setWebRtcArgs(*args, exchanger);
|
||||
auto& handler = const_cast<WebRtcInterface &>(exchanger);
|
||||
try {
|
||||
// 设置返回类型
|
||||
headerOut["Content-Type"] = "application/sdp";
|
||||
headerOut["Location"] = location + "?id=" + exchanger.getIdentifier() + "&token=" + exchanger.deleteRandStr();
|
||||
invoker(201, headerOut, exchangeSdp(exchanger, offer));
|
||||
invoker(201, headerOut, handler.getAnswerSdp(offer));
|
||||
} catch (std::exception &ex) {
|
||||
headerOut["Content-Type"] = "text/plain";
|
||||
invoker(406, headerOut, ex.what());
|
||||
|
|
@ -1835,7 +1821,7 @@ void installWebApi() {
|
|||
|
||||
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");
|
||||
|
|
@ -1921,11 +1907,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");
|
||||
|
|
|
|||
|
|
@ -115,72 +115,41 @@ std::string getValue(const mediakit::Parser &parser, Args &args, const First &fi
|
|||
|
||||
template<typename Args>
|
||||
class HttpAllArgs {
|
||||
mediakit::Parser* _parser = nullptr;
|
||||
Args* _args = nullptr;
|
||||
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 &p, Args &a): parser(p), args(a) {}
|
||||
|
||||
HttpAllArgs(const HttpAllArgs &that): _parser(new mediakit::Parser(that.parser)),
|
||||
_args(new Args(that.args)),
|
||||
parser(*_parser), args(*_args) {}
|
||||
~HttpAllArgs() {
|
||||
if (_parser) {
|
||||
delete _parser;
|
||||
}
|
||||
if (_args) {
|
||||
delete _args;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Key>
|
||||
toolkit::variant operator[](const Key &key) const {
|
||||
return (toolkit::variant)_get_value(*(HttpAllArgs*)this, key);
|
||||
return (toolkit::variant)getValue(parser, args, key);
|
||||
}
|
||||
|
||||
const mediakit::Parser &getParser() const {
|
||||
return _get_parser();
|
||||
}
|
||||
|
||||
Args &getArgs() {
|
||||
return *((Args *) _get_args());
|
||||
}
|
||||
|
||||
const Args &getArgs() const {
|
||||
return *((Args *) _get_args());
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -136,6 +136,15 @@ private:
|
|||
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();
|
||||
|
|
@ -243,15 +252,6 @@ public:
|
|||
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事件
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ protected:
|
|||
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 {};
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ protected:
|
|||
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);
|
||||
|
|
|
|||
|
|
@ -378,6 +378,12 @@ void WebRtcTransport::setRemoteDtlsFingerprint(const RtcSession &remote) {
|
|||
}
|
||||
|
||||
void WebRtcTransport::onRtcConfigure(RtcConfigure &configure) const {
|
||||
SdpAttrFingerprint fingerprint;
|
||||
fingerprint.algorithm = _offer_sdp->media[0].fingerprint.algorithm;
|
||||
fingerprint.hash = getFingerprint(fingerprint.algorithm, _dtls_transport);
|
||||
configure.setDefaultSetting(
|
||||
_ice_server->GetUsernameFragment(), _ice_server->GetPassword(), RtpDirection::sendrecv, fingerprint);
|
||||
|
||||
// 开启remb后关闭twcc,因为开启twcc后remb无效
|
||||
GET_CONFIG(size_t, remb_bit_rate, Rtc::kRembBitRate);
|
||||
configure.enableTWCC(!remb_bit_rate);
|
||||
|
|
@ -407,12 +413,7 @@ std::string WebRtcTransport::getAnswerSdp(const string &offer) {
|
|||
setRemoteDtlsFingerprint(*_offer_sdp);
|
||||
|
||||
//// sdp 配置 ////
|
||||
SdpAttrFingerprint fingerprint;
|
||||
fingerprint.algorithm = _offer_sdp->media[0].fingerprint.algorithm;
|
||||
fingerprint.hash = getFingerprint(fingerprint.algorithm, _dtls_transport);
|
||||
RtcConfigure configure;
|
||||
configure.setDefaultSetting(
|
||||
_ice_server->GetUsernameFragment(), _ice_server->GetPassword(), RtpDirection::sendrecv, fingerprint);
|
||||
onRtcConfigure(configure);
|
||||
|
||||
//// 生成answer sdp ////
|
||||
|
|
@ -431,10 +432,6 @@ static bool isDtls(char *buf) {
|
|||
return ((*buf > 19) && (*buf < 64));
|
||||
}
|
||||
|
||||
static string getPeerAddress(RTC::TransportTuple *tuple) {
|
||||
return tuple->get_peer_ip();
|
||||
}
|
||||
|
||||
void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tuple) {
|
||||
if (RTC::StunPacket::IsStun((const uint8_t *)buf, len)) {
|
||||
std::unique_ptr<RTC::StunPacket> packet(RTC::StunPacket::Parse((const uint8_t *)buf, len));
|
||||
|
|
@ -451,7 +448,7 @@ void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tup
|
|||
}
|
||||
if (isRtp(buf, len)) {
|
||||
if (!_srtp_session_recv) {
|
||||
WarnL << "received rtp packet when dtls not completed from:" << getPeerAddress(tuple);
|
||||
WarnL << "received rtp packet when dtls not completed from:" << tuple->get_peer_ip();
|
||||
return;
|
||||
}
|
||||
if (_srtp_session_recv->DecryptSrtp((uint8_t *)buf, &len)) {
|
||||
|
|
@ -461,7 +458,7 @@ void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tup
|
|||
}
|
||||
if (isRtcp(buf, len)) {
|
||||
if (!_srtp_session_recv) {
|
||||
WarnL << "received rtcp packet when dtls not completed from:" << getPeerAddress(tuple);
|
||||
WarnL << "received rtcp packet when dtls not completed from:" << tuple->get_peer_ip();
|
||||
return;
|
||||
}
|
||||
if (_srtp_session_recv->DecryptSrtcp((uint8_t *)buf, &len)) {
|
||||
|
|
@ -1278,21 +1275,14 @@ void WebRtcPluginManager::registerPlugin(const string &type, Plugin cb) {
|
|||
_map_creator[type] = std::move(cb);
|
||||
}
|
||||
|
||||
std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& offer) {
|
||||
return const_cast<WebRtcInterface &>(exchanger).getAnswerSdp(offer);
|
||||
}
|
||||
|
||||
void setLocalIp(const WebRtcInterface& exchanger, const std::string& localIp) {
|
||||
return const_cast<WebRtcInterface &>(exchanger).setLocalIp(localIp);
|
||||
}
|
||||
|
||||
void WebRtcPluginManager::setListener(Listener cb) {
|
||||
lock_guard<mutex> lck(_mtx_creator);
|
||||
_listener = std::move(cb);
|
||||
}
|
||||
|
||||
void WebRtcPluginManager::getAnswerSdp(Session &sender, const string &type, const WebRtcArgs &args, const onCreateRtc &cb_in) {
|
||||
onCreateRtc cb;
|
||||
void WebRtcPluginManager::negotiateSdp(Session &sender, const string &type, const WebRtcArgs &args, const onCreateWebRtc &cb_in) {
|
||||
onCreateWebRtc cb;
|
||||
lock_guard<mutex> lck(_mtx_creator);
|
||||
if (_listener) {
|
||||
auto listener = _listener;
|
||||
|
|
@ -1308,17 +1298,17 @@ void WebRtcPluginManager::getAnswerSdp(Session &sender, const string &type, cons
|
|||
|
||||
auto it = _map_creator.find(type);
|
||||
if (it == _map_creator.end()) {
|
||||
cb(WebRtcException(SockException(Err_other, "the type can not supported")));
|
||||
cb_in(WebRtcException(SockException(Err_other, "the type can not supported")));
|
||||
return;
|
||||
}
|
||||
it->second(sender, args, cb);
|
||||
}
|
||||
|
||||
void echo_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
|
||||
void echo_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &cb) {
|
||||
cb(*WebRtcEchoTest::create(EventPollerPool::Instance().getPoller()));
|
||||
}
|
||||
|
||||
void push_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
|
||||
void push_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &cb) {
|
||||
MediaInfo info(args["url"]);
|
||||
bool preferred_tcp = args["preferred_tcp"];
|
||||
|
||||
|
|
@ -1374,7 +1364,7 @@ void push_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana
|
|||
}
|
||||
}
|
||||
|
||||
void play_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginManager::onCreateRtc &cb) {
|
||||
void play_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &cb) {
|
||||
MediaInfo info(args["url"]);
|
||||
bool preferred_tcp = args["preferred_tcp"];
|
||||
|
||||
|
|
@ -1408,6 +1398,23 @@ void play_plugin(Session &sender, const WebRtcArgs &args, const WebRtcPluginMana
|
|||
}
|
||||
}
|
||||
|
||||
bool isVaildIP(const std::string& ip) {
|
||||
int a,b,c,d;
|
||||
return sscanf(ip.c_str(),"%d.%d.%d.%d", &a, &b, &c, &d) == 4;
|
||||
}
|
||||
|
||||
static void set_local_ip(const WebRtcArgs &args, const WebRtcInterface &rtc) {
|
||||
std::string host = args["Host"];
|
||||
if (!host.empty()) {
|
||||
std::string localIp = host.substr(0, host.find(':'));
|
||||
|
||||
if (!isVaildIP(localIp) || localIp == "127.0.0.1") {
|
||||
localIp = "";
|
||||
}
|
||||
const_cast<WebRtcInterface &>(rtc).setLocalIp(std::move(localIp));
|
||||
}
|
||||
}
|
||||
|
||||
static void set_webrtc_cands(const WebRtcArgs &args, const WebRtcInterface &rtc) {
|
||||
vector<SdpAttrCandidate> cands;
|
||||
{
|
||||
|
|
@ -1434,14 +1441,15 @@ static void set_webrtc_cands(const WebRtcArgs &args, const WebRtcInterface &rtc)
|
|||
}
|
||||
}
|
||||
|
||||
void setWebRtcArgs(const WebRtcArgs &args, const WebRtcInterface &rtc) {
|
||||
set_local_ip(args, rtc);
|
||||
set_webrtc_cands(args, rtc);
|
||||
}
|
||||
|
||||
static onceToken s_rtc_auto_register([]() {
|
||||
WebRtcPluginManager::Instance().registerPlugin("echo", echo_plugin);
|
||||
WebRtcPluginManager::Instance().registerPlugin("push", push_plugin);
|
||||
WebRtcPluginManager::Instance().registerPlugin("play", play_plugin);
|
||||
|
||||
WebRtcPluginManager::Instance().setListener([](Session &sender, const std::string &type, const WebRtcArgs &args, const WebRtcInterface &rtc) {
|
||||
set_webrtc_cands(args, rtc);
|
||||
});
|
||||
});
|
||||
|
||||
}// namespace mediakit
|
||||
|
|
|
|||
|
|
@ -35,6 +35,8 @@ extern const std::string kTcpPort;
|
|||
extern const std::string kTimeOutSec;
|
||||
}//namespace RTC
|
||||
|
||||
class WebRtcArgs;
|
||||
|
||||
class WebRtcInterface {
|
||||
public:
|
||||
virtual ~WebRtcInterface() = default;
|
||||
|
|
@ -45,9 +47,7 @@ public:
|
|||
virtual void setLocalIp(const std::string &localIp) {}
|
||||
};
|
||||
|
||||
std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& offer);
|
||||
|
||||
void setLocalIp(const WebRtcInterface &exchanger, const std::string &localIp);
|
||||
void setWebRtcArgs(const WebRtcArgs &args, const WebRtcInterface &rtc);
|
||||
|
||||
class WebRtcException : public WebRtcInterface {
|
||||
public:
|
||||
|
|
@ -88,7 +88,7 @@ public:
|
|||
* @param offer offer sdp
|
||||
* @return answer sdp
|
||||
*/
|
||||
std::string getAnswerSdp(const std::string &offer) override;
|
||||
std::string getAnswerSdp(const std::string &offer) override final;
|
||||
|
||||
/**
|
||||
* 获取对象唯一id
|
||||
|
|
@ -273,7 +273,7 @@ protected:
|
|||
void onCreate() override;
|
||||
void onDestory() override;
|
||||
void onShutdown(const SockException &ex) override;
|
||||
virtual void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) = 0;
|
||||
virtual void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) {}
|
||||
void updateTicker();
|
||||
float getLossRate(TrackType type);
|
||||
void onRtcpBye() override;
|
||||
|
|
@ -337,17 +337,17 @@ public:
|
|||
virtual variant operator[](const std::string &key) const = 0;
|
||||
};
|
||||
|
||||
using onCreateWebRtc = std::function<void(const WebRtcInterface &rtc)>;
|
||||
class WebRtcPluginManager {
|
||||
public:
|
||||
using onCreateRtc = std::function<void(const WebRtcInterface &rtc)>;
|
||||
using Plugin = std::function<void(Session &sender, const WebRtcArgs &args, const onCreateRtc &cb)>;
|
||||
using Plugin = std::function<void(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &cb)>;
|
||||
using Listener = std::function<void(Session &sender, const std::string &type, const WebRtcArgs &args, const WebRtcInterface &rtc)>;
|
||||
|
||||
static WebRtcPluginManager &Instance();
|
||||
|
||||
void registerPlugin(const std::string &type, Plugin cb);
|
||||
void getAnswerSdp(Session &sender, const std::string &type, const WebRtcArgs &args, const onCreateRtc &cb);
|
||||
void setListener(Listener cb);
|
||||
void negotiateSdp(Session &sender, const std::string &type, const WebRtcArgs &args, const onCreateWebRtc &cb);
|
||||
|
||||
private:
|
||||
WebRtcPluginManager() = default;
|
||||
|
|
|
|||
Loading…
Reference in New Issue