转协议选项抽象为ProtocolOption对象

This commit is contained in:
xiongziliang
2022-03-12 13:19:21 +08:00
parent ed661b1cf1
commit 4dc621e1bb
27 changed files with 187 additions and 135 deletions

View File

@@ -448,7 +448,7 @@ void getStatisticJson(const function<void(Value &val)> &cb) {
}
void addStreamProxy(const string &vhost, const string &app, const string &stream, const string &url, int retry_count,
bool enable_hls, bool enable_mp4, int rtp_type, float timeout_sec,
const ProtocolOption &option, int rtp_type, float timeout_sec,
const function<void(const SockException &ex, const string &key)> &cb) {
auto key = getProxyKey(vhost, app, stream);
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
@@ -458,7 +458,7 @@ void addStreamProxy(const string &vhost, const string &app, const string &stream
return;
}
//添加拉流代理
auto player = std::make_shared<PlayerProxy>(vhost, app, stream, enable_hls, enable_mp4, retry_count ? retry_count : -1);
auto player = std::make_shared<PlayerProxy>(vhost, app, stream, option, retry_count ? retry_count : -1);
s_proxyMap[key] = player;
//指定RTP over TCP(播放rtsp时有效)
@@ -906,13 +906,16 @@ void installWebApi() {
api_regist("/index/api/addStreamProxy",[](API_ARGS_MAP_ASYNC){
CHECK_SECRET();
CHECK_ARGS("vhost","app","stream","url");
ProtocolOption option;
option.enable_hls = allArgs["enable_hls"];
option.enable_mp4 = allArgs["enable_mp4"];
addStreamProxy(allArgs["vhost"],
allArgs["app"],
allArgs["stream"],
allArgs["url"],
allArgs["retry_count"],
allArgs["enable_hls"],/* 是否hls转发 */
allArgs["enable_mp4"],/* 是否MP4录制 */
option,
allArgs["rtp_type"],
allArgs["timeout_sec"],
[invoker,val,headerOut](const SockException &ex,const string &key) mutable{
@@ -1473,7 +1476,12 @@ void installWebApi() {
api_regist("/index/hook/on_stream_not_found",[](API_ARGS_MAP_ASYNC){
//媒体未找到事件,我们都及时拉流hks作为替代品目的是为了测试按需拉流
CHECK_SECRET();
CHECK_ARGS("vhost","app","stream");
CHECK_ARGS("vhost","app","stream", "schema");
ProtocolOption option;
option.enable_hls = allArgs["schema"] == HLS_SCHEMA;
option.enable_mp4 = false;
//通过内置支持的rtsp/rtmp按需拉流
addStreamProxy(allArgs["vhost"],
allArgs["app"],
@@ -1481,8 +1489,7 @@ void installWebApi() {
/** 支持rtsp和rtmp方式拉流 rtsp支持h265/h264/aac,rtmp仅支持h264/aac **/
"rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov",
-1,/*无限重试*/
true,/* 开启hls转发 */
false,/* 禁用MP4录制 */
option,
0,//rtp over tcp方式拉流
10,//10秒超时
[invoker,val,headerOut](const SockException &ex,const string &key) mutable{

View File

@@ -17,6 +17,7 @@
#include "Common/Parser.h"
#include "Network/Socket.h"
#include "Http/HttpSession.h"
#include "Common/MultiMediaSourceMuxer.h"
//配置文件路径
extern std::string g_ini_file;
@@ -232,6 +233,6 @@ void unInstallWebApi();
Json::Value makeMediaSourceJson(mediakit::MediaSource &media);
void getStatisticJson(const std::function<void(Json::Value &val)> &cb);
void addStreamProxy(const std::string &vhost, const std::string &app, const std::string &stream, const std::string &url, int retry_count,
bool enable_hls, bool enable_mp4, int rtp_type, float timeout_sec,
const mediakit::ProtocolOption &option, int rtp_type, float timeout_sec,
const std::function<void(const toolkit::SockException &ex, const std::string &key)> &cb);
#endif //ZLMEDIAKIT_WEBAPI_H

View File

@@ -244,8 +244,12 @@ static void pullStreamFromOrigin(const vector<string>& urls, size_t index, size_
auto timeout_sec = cluster_timeout_sec / urls.size();
InfoL << "pull stream from origin, failed_cnt: " << failed_cnt << ", timeout_sec: " << timeout_sec << ", url: " << url;
addStreamProxy(args._vhost, args._app, args._streamid, url, -1, args._schema == HLS_SCHEMA, false,
Rtsp::RTP_TCP, timeout_sec, [=](const SockException &ex, const string &key) mutable {
ProtocolOption option;
option.enable_hls = args._schema == HLS_SCHEMA;
option.enable_mp4 = false;
addStreamProxy(args._vhost, args._app, args._streamid, url, -1, option, Rtsp::RTP_TCP, timeout_sec,
[=](const SockException &ex, const string &key) mutable {
if (!ex) {
return;
}
@@ -264,12 +268,10 @@ void installWebHook(){
GET_CONFIG(bool,hook_enable,Hook::kEnable);
GET_CONFIG(string,hook_adminparams,Hook::kAdminParams);
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastMediaPublish,[](BroadcastMediaPublishArgs){
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPublish, [](BroadcastMediaPublishArgs) {
GET_CONFIG(string,hook_publish,Hook::kOnPublish);
GET_CONFIG(bool,toHls,General::kPublishToHls);
GET_CONFIG(bool,toMP4,General::kPublishToMP4);
if(!hook_enable || args._param_strs == hook_adminparams || hook_publish.empty() || sender.get_peer_ip() == "127.0.0.1"){
invoker("", toHls, toMP4);
if (!hook_enable || args._param_strs == hook_adminparams || hook_publish.empty() || sender.get_peer_ip() == "127.0.0.1") {
invoker("", ProtocolOption());
return;
}
//异步执行该hook api防止阻塞NoticeCenter
@@ -280,25 +282,22 @@ void installWebHook(){
body["originType"] = (int) type;
body["originTypeStr"] = getOriginTypeString(type);
//执行hook
do_http_hook(hook_publish,body,[invoker](const Value &obj,const string &err){
if(err.empty()){
do_http_hook(hook_publish, body, [invoker](const Value &obj, const string &err) mutable {
ProtocolOption option;
if (err.empty()) {
//推流鉴权成功
bool enableHls = toHls;
bool enableMP4 = toMP4;
//兼容用户不传递enableHls、enableMP4参数
if (obj.isMember("enableHls")) {
enableHls = obj["enableHls"].asBool();
option.enable_hls = obj["enableHls"].asBool();
}
if (obj.isMember("enableMP4")) {
enableMP4 = obj["enableMP4"].asBool();
option.enable_mp4 = obj["enableMP4"].asBool();
}
invoker(err, enableHls, enableMP4);
invoker(err, option);
} else {
//推流鉴权失败
invoker(err, false, false);
invoker(err, option);
}
});
});