mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-20 07:12:21 +08:00
整理webrtc c接口
This commit is contained in:
@@ -261,11 +261,48 @@ API_EXPORT uint16_t API_CALL mk_rtc_server_start(uint16_t port) {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
WarnL << "未启用该功能!";
|
||||
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
class WebRtcArgsUrl : public mediakit::WebRtcArgs {
|
||||
public:
|
||||
WebRtcArgsUrl(std::string url) { _url = std::move(url); }
|
||||
~WebRtcArgsUrl() = default;
|
||||
|
||||
toolkit::variant operator[](const std::string &key) const override {
|
||||
if (key == "url") {
|
||||
return _url;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private:
|
||||
std::string _url;
|
||||
};
|
||||
|
||||
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type,
|
||||
const char *offer, const char *url) {
|
||||
#ifdef ENABLE_WEBRTC
|
||||
assert(type && offer && url && cb);
|
||||
auto session = std::make_shared<HttpSession>(Socket::createSocket());
|
||||
std::string offer_str = offer;
|
||||
WebRtcPluginManager::Instance().getAnswerSdp(*session, type, WebRtcArgsUrl(url),
|
||||
[offer_str, session, user_data, cb](const WebRtcInterface &exchanger) mutable {
|
||||
std::string sdp_answer;
|
||||
try {
|
||||
sdp_answer = const_cast<WebRtcInterface &>(exchanger).getAnswerSdp(offer_str);
|
||||
cb(user_data, sdp_answer.data(), nullptr);
|
||||
} catch (std::exception &ex) {
|
||||
cb(user_data, nullptr, ex.what());
|
||||
}
|
||||
});
|
||||
#else
|
||||
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
|
||||
#endif
|
||||
}
|
||||
|
||||
API_EXPORT uint16_t API_CALL mk_srt_server_start(uint16_t port) {
|
||||
#ifdef ENABLE_SRT
|
||||
try {
|
||||
|
||||
@@ -18,11 +18,6 @@
|
||||
#include "Http/HttpClient.h"
|
||||
#include "Rtsp/RtspSession.h"
|
||||
|
||||
#ifdef ENABLE_WEBRTC
|
||||
#include "jsoncpp/json.h"
|
||||
#include "mk_webrtc_private.h"
|
||||
#endif
|
||||
|
||||
using namespace toolkit;
|
||||
using namespace mediakit;
|
||||
|
||||
@@ -257,25 +252,6 @@ API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_s
|
||||
}, schema ? schema : "", vhost ? vhost : "", app ? app : "", stream ? stream : "");
|
||||
}
|
||||
|
||||
API_EXPORT void API_CALL mk_media_source_answersdp(void *user_data, on_mk_media_source_answersdp_cb cb, const char *offer,
|
||||
const char *schema, const char *vhost, const char *app, const char *stream, int from_mp4) {
|
||||
#ifdef ENABLE_WEBRTC
|
||||
assert(offer && schema && vhost && app && stream && cb);
|
||||
auto srcfound = MediaSource::find(schema, vhost, app, stream, from_mp4);
|
||||
mediakit::MediaInfo info;
|
||||
info._schema = RTC_SCHEMA;
|
||||
info._host = vhost;
|
||||
info._app = app;
|
||||
info._streamid = stream;
|
||||
try {
|
||||
auto rtc = WebRtcPlayer::create(EventPollerPool::Instance().getPoller(), std::dynamic_pointer_cast<RtspMediaSource>(srcfound), info);
|
||||
cb(user_data, srcfound.get(), rtc->getAnswerSdp(offer).c_str(), "");
|
||||
} catch (std::exception &ex) {
|
||||
cb(user_data, nullptr, nullptr, ex.what());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
///////////////////////////////////////////HttpBody/////////////////////////////////////////////
|
||||
API_EXPORT mk_http_body API_CALL mk_http_body_from_string(const char *str, size_t len){
|
||||
assert(str);
|
||||
@@ -349,53 +325,6 @@ API_EXPORT void API_CALL mk_http_response_invoker_do(const mk_http_response_invo
|
||||
(*invoker)(response_code,header,*body);
|
||||
}
|
||||
|
||||
API_EXPORT void API_CALL mk_webrtc_http_response_invoker_do(const mk_http_response_invoker ctx_invoker,
|
||||
const mk_parser ctx_parser,
|
||||
const mk_sock_info ctx_sock ) {
|
||||
assert(ctx_parser && ctx_invoker && ctx_sock);
|
||||
#ifdef ENABLE_WEBRTC
|
||||
static auto webrtc_cb = [](API_ARGS_STRING_ASYNC){
|
||||
CHECK_ARGS("type");
|
||||
auto type = allArgs["type"];
|
||||
auto offer = allArgs.getArgs();
|
||||
CHECK(!offer.empty(), "http body(webrtc offer sdp) is empty");
|
||||
|
||||
WebRtcPluginManager::Instance().getAnswerSdp(
|
||||
*(static_cast<Session *>(&sender)), type, offer, WebRtcArgsImp(allArgs, sender.getIdentifier()),
|
||||
[invoker, val, offer, headerOut](const WebRtcInterface &exchanger) mutable {
|
||||
//设置返回类型
|
||||
headerOut["Content-Type"] = HttpFileManager::getContentType(".json");
|
||||
//设置跨域
|
||||
headerOut["Access-Control-Allow-Origin"] = "*";
|
||||
|
||||
try {
|
||||
val["sdp"] = const_cast<WebRtcInterface &>(exchanger).getAnswerSdp(offer);
|
||||
val["id"] = exchanger.getIdentifier();
|
||||
val["type"] = "answer";
|
||||
invoker(200, headerOut, val.toStyledString());
|
||||
} catch (std::exception &ex) {
|
||||
val["code"] = API::Exception;
|
||||
val["msg"] = ex.what();
|
||||
invoker(200, headerOut, val.toStyledString());
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Parser *parser = (Parser *)ctx_parser;
|
||||
HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx_invoker;
|
||||
SockInfo* sender = (SockInfo*)ctx_sock;
|
||||
|
||||
GET_CONFIG(std::string, charSet, Http::kCharSet);
|
||||
HttpSession::KeyValue headerOut;
|
||||
headerOut["Content-Type"] = std::string("application/json; charset=") + charSet;
|
||||
|
||||
Json::Value val;
|
||||
val["code"] = API::Success;
|
||||
|
||||
webrtc_cb(*sender, headerOut, HttpAllArgs<std::string>(*parser, (std::string &)parser->Content()), val, *invoker);
|
||||
#endif
|
||||
};
|
||||
|
||||
API_EXPORT mk_http_response_invoker API_CALL mk_http_response_invoker_clone(const mk_http_response_invoker ctx){
|
||||
assert(ctx);
|
||||
HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx;
|
||||
|
||||
@@ -1,228 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
||||
*
|
||||
* Use of this source code is governed by MIT license that can be found in the
|
||||
* LICENSE file in the root of the source tree. All contributing project authors
|
||||
* may be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifdef ENABLE_WEBRTC
|
||||
#ifndef MK_WEBRTC_API_H
|
||||
#define MK_WEBRTC_API_H
|
||||
|
||||
#include "Common/Parser.h"
|
||||
#include "Http/HttpSession.h"
|
||||
#include "Network/Socket.h"
|
||||
#include "jsoncpp/json.h"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "../webrtc/WebRtcEchoTest.h"
|
||||
#include "../webrtc/WebRtcPlayer.h"
|
||||
#include "../webrtc/WebRtcPusher.h"
|
||||
#include "../webrtc/WebRtcTransport.h"
|
||||
|
||||
using namespace mediakit;
|
||||
|
||||
namespace API {
|
||||
typedef enum {
|
||||
NotFound = -500, //未找到
|
||||
Exception = -400, //代码抛异常
|
||||
InvalidArgs = -300, //参数不合法
|
||||
SqlFailed = -200, // sql执行失败
|
||||
AuthFailed = -100, //鉴权失败
|
||||
OtherFailed = -1, //业务代码执行失败,
|
||||
Success = 0 //执行成功
|
||||
} ApiErr;
|
||||
} // namespace API
|
||||
|
||||
class ApiRetException : public std::runtime_error {
|
||||
public:
|
||||
ApiRetException(const char *str = "success", int code = API::Success)
|
||||
: runtime_error(str) {
|
||||
_code = code;
|
||||
}
|
||||
~ApiRetException() = default;
|
||||
int code() { return _code; }
|
||||
|
||||
private:
|
||||
int _code;
|
||||
};
|
||||
|
||||
class AuthException : public ApiRetException {
|
||||
public:
|
||||
AuthException(const char *str)
|
||||
: ApiRetException(str, API::AuthFailed) {}
|
||||
~AuthException() = default;
|
||||
};
|
||||
|
||||
class InvalidArgsException : public ApiRetException {
|
||||
public:
|
||||
InvalidArgsException(const char *str)
|
||||
: ApiRetException(str, API::InvalidArgs) {}
|
||||
~InvalidArgsException() = default;
|
||||
};
|
||||
|
||||
class SuccessException : public ApiRetException {
|
||||
public:
|
||||
SuccessException()
|
||||
: ApiRetException("success", API::Success) {}
|
||||
~SuccessException() = default;
|
||||
};
|
||||
|
||||
using ApiArgsType = std::map<std::string, std::string, mediakit::StrCaseCompare>;
|
||||
template <typename Args, typename First>
|
||||
std::string getValue(Args &args, const First &first) {
|
||||
return args[first];
|
||||
}
|
||||
|
||||
template <typename First>
|
||||
std::string getValue(Json::Value &args, const First &first) {
|
||||
return args[first].asString();
|
||||
}
|
||||
|
||||
template <typename First>
|
||||
std::string getValue(std::string &args, const First &first) {
|
||||
return "";
|
||||
}
|
||||
|
||||
template <typename First>
|
||||
std::string getValue(const mediakit::Parser &parser, const First &first) {
|
||||
auto ret = parser.getUrlArgs()[first];
|
||||
if (!ret.empty()) {
|
||||
return ret;
|
||||
}
|
||||
return parser.getHeader()[first];
|
||||
}
|
||||
|
||||
template <typename First>
|
||||
std::string getValue(mediakit::Parser &parser, const First &first) {
|
||||
return getValue((const mediakit::Parser &)parser, first);
|
||||
}
|
||||
|
||||
template <typename Args, typename First>
|
||||
std::string getValue(const mediakit::Parser &parser, Args &args, const First &first) {
|
||||
auto ret = getValue(args, first);
|
||||
if (!ret.empty()) {
|
||||
return ret;
|
||||
}
|
||||
return getValue(parser, first);
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
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() = default;
|
||||
|
||||
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()); }
|
||||
|
||||
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
|
||||
#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_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_ASYNC API_ARGS_STRING, const mediakit::HttpSession::HttpResponseInvoker &invoker
|
||||
#define API_ARGS_VALUE sender, headerOut, allArgs, val
|
||||
|
||||
//注册http请求参数是http原始请求信息的异步回复的http api
|
||||
void api_regist(const std::string &api_path, const std::function<void(API_ARGS_STRING_ASYNC)> &func);
|
||||
|
||||
template <typename Args, typename First>
|
||||
bool checkArgs(Args &args, const First &first) {
|
||||
return !args[first].empty();
|
||||
}
|
||||
|
||||
template <typename Args, typename First, typename... KeyTypes>
|
||||
bool checkArgs(Args &args, const First &first, const KeyTypes &...keys) {
|
||||
return checkArgs(args, first) && checkArgs(args, keys...);
|
||||
}
|
||||
|
||||
//检查http url中或body中或http header参数是否为空的宏
|
||||
#define CHECK_ARGS(...) \
|
||||
if (!checkArgs(allArgs, ##__VA_ARGS__)) { \
|
||||
throw InvalidArgsException("缺少必要参数:" #__VA_ARGS__); \
|
||||
}
|
||||
|
||||
#ifdef ENABLE_WEBRTC
|
||||
class WebRtcArgsImp : public WebRtcArgs {
|
||||
public:
|
||||
WebRtcArgsImp(const HttpAllArgs<std::string> &args, std::string session_id)
|
||||
: _args(args)
|
||||
, _session_id(std::move(session_id)) {}
|
||||
~WebRtcArgsImp() override = default;
|
||||
|
||||
variant operator[](const std::string &key) const override {
|
||||
if (key == "url") {
|
||||
return getUrl();
|
||||
}
|
||||
return _args[key];
|
||||
}
|
||||
|
||||
private:
|
||||
std::string getUrl() const {
|
||||
auto &allArgs = _args;
|
||||
CHECK_ARGS("app", "stream");
|
||||
|
||||
return StrPrinter << RTC_SCHEMA << "://" << _args["Host"] << "/" << _args["app"] << "/" << _args["stream"]
|
||||
<< "?" << _args.getParser().Params() + "&session=" + _session_id;
|
||||
}
|
||||
|
||||
private:
|
||||
HttpAllArgs<std::string> _args;
|
||||
std::string _session_id;
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif // MK_WEBRTC_API_H
|
||||
#endif // ENABLE_WEBRTC
|
||||
Reference in New Issue
Block a user