Optimize the code

1. change param_strs to params
2. move params from MediaInfo to MediaTuple
3. passing MediaTuple as a parameter for some functions
This commit is contained in:
xia-chu 2024-03-30 14:41:20 +08:00
parent ecc05dae28
commit 390c374086
13 changed files with 31 additions and 39 deletions

View File

@ -130,7 +130,7 @@ API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx, size_
API_EXPORT const char* API_CALL mk_media_info_get_params(const mk_media_info ctx){ API_EXPORT const char* API_CALL mk_media_info_get_params(const mk_media_info ctx){
assert(ctx); assert(ctx);
MediaInfo *info = (MediaInfo *)ctx; MediaInfo *info = (MediaInfo *)ctx;
return info->param_strs.c_str(); return info->params.c_str();
} }
API_EXPORT const char* API_CALL mk_media_info_get_schema(const mk_media_info ctx){ API_EXPORT const char* API_CALL mk_media_info_get_schema(const mk_media_info ctx){

View File

@ -225,7 +225,7 @@ static ArgsType make_json(const MediaInfo &args) {
ArgsType body; ArgsType body;
body["schema"] = args.schema; body["schema"] = args.schema;
dumpMediaTuple(args, body); dumpMediaTuple(args, body);
body["params"] = args.param_strs; body["params"] = args.params;
return body; return body;
} }
@ -286,7 +286,7 @@ static string getPullUrl(const string &origin_fmt, const MediaInfo &info) {
return ""; return "";
} }
// 告知源站这是来自边沿站的拉流请求,如果未找到流请立即返回拉流失败 // 告知源站这是来自边沿站的拉流请求,如果未找到流请立即返回拉流失败
return string(url) + '?' + kEdgeServerParam + '&' + VHOST_KEY + '=' + info.vhost + '&' + info.param_strs; return string(url) + '?' + kEdgeServerParam + '&' + VHOST_KEY + '=' + info.vhost + '&' + info.params;
} }
static void pullStreamFromOrigin(const vector<string> &urls, size_t index, size_t failed_cnt, const MediaInfo &args, const function<void()> &closePlayer) { static void pullStreamFromOrigin(const vector<string> &urls, size_t index, size_t failed_cnt, const MediaInfo &args, const function<void()> &closePlayer) {
@ -498,7 +498,7 @@ void installWebHook() {
return; return;
} }
if (start_with(args.param_strs, kEdgeServerParam)) { if (start_with(args.params, kEdgeServerParam)) {
// 源站收到来自边沿站的溯源请求,流不存在时立即返回拉流失败 // 源站收到来自边沿站的溯源请求,流不存在时立即返回拉流失败
closePlayer(); closePlayer();
return; return;

View File

@ -583,7 +583,7 @@ void MediaInfo::parse(const std::string &url_in){
auto url = url_in; auto url = url_in;
auto pos = url.find("?"); auto pos = url.find("?");
if (pos != string::npos) { if (pos != string::npos) {
param_strs = url.substr(pos + 1); params = url.substr(pos + 1);
url.erase(pos); url.erase(pos);
} }
@ -616,9 +616,10 @@ void MediaInfo::parse(const std::string &url_in){
stream = stream_id; stream = stream_id;
} }
auto params = Parser::parseArgs(param_strs); auto kv = Parser::parseArgs(params);
if (params.find(VHOST_KEY) != params.end()) { auto it = kv.find(VHOST_KEY);
vhost = params[VHOST_KEY]; if (it != kv.end()) {
vhost = it->second;
} }
GET_CONFIG(bool, enableVhost, General::kEnableVhost); GET_CONFIG(bool, enableVhost, General::kEnableVhost);

View File

@ -299,7 +299,6 @@ public:
std::string full_url; std::string full_url;
std::string schema; std::string schema;
std::string host; std::string host;
std::string param_strs;
}; };
bool equalMediaTuple(const MediaTuple& a, const MediaTuple& b); bool equalMediaTuple(const MediaTuple& a, const MediaTuple& b);

View File

@ -195,10 +195,8 @@ std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file, bool setbuf) {
return ret; return ret;
} }
void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const string &stream_id) { void HlsMakerImp::setMediaSource(const MediaTuple& tuple) {
_info.app = app; static_cast<MediaTuple &>(_info) = tuple;
_info.stream = stream_id;
_info.vhost = vhost;
_media_src = std::make_shared<HlsMediaSource>(isFmp4() ? HLS_FMP4_SCHEMA : HLS_SCHEMA, _info); _media_src = std::make_shared<HlsMediaSource>(isFmp4() ? HLS_FMP4_SCHEMA : HLS_SCHEMA, _info);
} }

View File

@ -27,11 +27,8 @@ public:
/** /**
* *
* @param vhost
* @param app
* @param stream_id id
*/ */
void setMediaSource(const std::string &vhost, const std::string &app, const std::string &stream_id); void setMediaSource(const MediaTuple& tuple);
/** /**
* MediaSource * MediaSource

View File

@ -34,7 +34,7 @@ public:
} }
void setMediaSource(const MediaTuple& tuple) { void setMediaSource(const MediaTuple& tuple) {
_hls->setMediaSource(tuple.vhost, tuple.app, tuple.stream); _hls->setMediaSource(tuple);
} }
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) { void setListener(const std::weak_ptr<MediaSourceEvent> &listener) {

View File

@ -22,12 +22,10 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
MP4Recorder::MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second) { MP4Recorder::MP4Recorder(const MediaTuple &tuple, const string &path, size_t max_second) {
_folder_path = path; _folder_path = path;
/////record 业务逻辑////// /////record 业务逻辑//////
_info.app = app; static_cast<MediaTuple &>(_info) = tuple;
_info.stream = stream_id;
_info.vhost = vhost;
_info.folder = path; _info.folder = path;
GET_CONFIG(uint32_t, s_max_second, Protocol::kMP4MaxSecond); GET_CONFIG(uint32_t, s_max_second, Protocol::kMP4MaxSecond);
_max_second = max_second ? max_second : s_max_second; _max_second = max_second ? max_second : s_max_second;

View File

@ -26,7 +26,7 @@ class MP4Recorder final : public MediaSinkInterface {
public: public:
using Ptr = std::shared_ptr<MP4Recorder>; using Ptr = std::shared_ptr<MP4Recorder>;
MP4Recorder(const std::string &path, const std::string &vhost, const std::string &app, const std::string &stream_id, size_t max_second); MP4Recorder(const MediaTuple &tuple, const std::string &path, size_t max_second);
~MP4Recorder() override; ~MP4Recorder() override;
/** /**

View File

@ -68,8 +68,7 @@ string Recorder::getRecordPath(Recorder::type type, const MediaTuple& tuple, con
} }
return File::absolutePath(m3u8FilePath, hlsPath); return File::absolutePath(m3u8FilePath, hlsPath);
} }
default: default: return "";
return "";
} }
} }
@ -85,13 +84,12 @@ std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const Me
#else #else
throw std::invalid_argument("hls相关功能未打开请开启ENABLE_HLS宏后编译再测试"); throw std::invalid_argument("hls相关功能未打开请开启ENABLE_HLS宏后编译再测试");
#endif #endif
} }
case Recorder::type_mp4: { case Recorder::type_mp4: {
#if defined(ENABLE_MP4) #if defined(ENABLE_MP4)
auto path = Recorder::getRecordPath(type, tuple, option.mp4_save_path); auto path = Recorder::getRecordPath(type, tuple, option.mp4_save_path);
return std::make_shared<MP4Recorder>(path, tuple.vhost, tuple.app, tuple.stream, option.mp4_max_second); return std::make_shared<MP4Recorder>(tuple, path, option.mp4_max_second);
#else #else
throw std::invalid_argument("mp4相关功能未打开请开启ENABLE_MP4宏后编译再测试"); throw std::invalid_argument("mp4相关功能未打开请开启ENABLE_MP4宏后编译再测试");
#endif #endif

View File

@ -22,6 +22,7 @@ struct MediaTuple {
std::string vhost; std::string vhost;
std::string app; std::string app;
std::string stream; std::string stream;
std::string params;
std::string shortUrl() const { std::string shortUrl() const {
return vhost + '/' + app + '/' + stream; return vhost + '/' + app + '/' + stream;
} }

View File

@ -63,8 +63,8 @@ void SrtTransportImp::onHandShakeFinished(std::string &streamid, struct sockaddr
return; return;
} }
auto params = Parser::parseArgs(_media_info.param_strs); auto kv = Parser::parseArgs(_media_info.params);
if (params["m"] == "publish") { if (kv["m"] == "publish") {
_is_pusher = true; _is_pusher = true;
_decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, this); _decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, this);
emitOnPublish(); emitOnPublish();
@ -98,10 +98,10 @@ bool SrtTransportImp::parseStreamid(std::string &streamid) {
app = tmps[0]; app = tmps[0];
stream_name = tmps[1]; stream_name = tmps[1];
} else { } else {
if (_media_info.param_strs.empty()) { if (_media_info.params.empty()) {
_media_info.param_strs = it.first + "=" + it.second; _media_info.params = it.first + "=" + it.second;
} else { } else {
_media_info.param_strs += "&" + it.first + "=" + it.second; _media_info.params += "&" + it.first + "=" + it.second;
} }
} }
} }
@ -118,7 +118,7 @@ bool SrtTransportImp::parseStreamid(std::string &streamid) {
_media_info.app = app; _media_info.app = app;
_media_info.stream = stream_name; _media_info.stream = stream_name;
TraceL << " mediainfo=" << _media_info.shortUrl() << " params=" << _media_info.param_strs; TraceL << " mediainfo=" << _media_info.shortUrl() << " params=" << _media_info.params;
return true; return true;
} }

View File

@ -90,7 +90,7 @@ void initEventListener() {
static onceToken s_token([]() { static onceToken s_token([]() {
//监听kBroadcastOnGetRtspRealm事件决定rtsp链接是否需要鉴权(传统的rtsp鉴权方案)才能访问 //监听kBroadcastOnGetRtspRealm事件决定rtsp链接是否需要鉴权(传统的rtsp鉴权方案)才能访问
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastOnGetRtspRealm, [](BroadcastOnGetRtspRealmArgs) { NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastOnGetRtspRealm, [](BroadcastOnGetRtspRealmArgs) {
DebugL << "RTSP是否需要鉴权事件" << args.getUrl() << " " << args.param_strs; DebugL << "RTSP是否需要鉴权事件" << args.getUrl() << " " << args.params;
if (string("1") == args.stream) { if (string("1") == args.stream) {
// live/1需要认证 // live/1需要认证
//该流需要认证并且设置realm //该流需要认证并且设置realm
@ -104,7 +104,7 @@ void initEventListener() {
//监听kBroadcastOnRtspAuth事件返回正确的rtsp鉴权用户密码 //监听kBroadcastOnRtspAuth事件返回正确的rtsp鉴权用户密码
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastOnRtspAuth, [](BroadcastOnRtspAuthArgs) { NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastOnRtspAuth, [](BroadcastOnRtspAuthArgs) {
DebugL << "RTSP播放鉴权:" << args.getUrl() << " " << args.param_strs; DebugL << "RTSP播放鉴权:" << args.getUrl() << " " << args.params;
DebugL << "RTSP用户" << user_name << (must_no_encrypt ? " Base64" : " MD5") << " 方式登录"; DebugL << "RTSP用户" << user_name << (must_no_encrypt ? " Base64" : " MD5") << " 方式登录";
string user = user_name; string user = user_name;
//假设我们异步读取数据库 //假设我们异步读取数据库
@ -134,14 +134,14 @@ void initEventListener() {
//监听rtsp/rtmp推流事件返回结果告知是否有推流权限 //监听rtsp/rtmp推流事件返回结果告知是否有推流权限
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPublish, [](BroadcastMediaPublishArgs) { NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPublish, [](BroadcastMediaPublishArgs) {
DebugL << "推流鉴权:" << args.getUrl() << " " << args.param_strs; DebugL << "推流鉴权:" << args.getUrl() << " " << args.params;
invoker("", ProtocolOption());//鉴权成功 invoker("", ProtocolOption());//鉴权成功
//invoker("this is auth failed message");//鉴权失败 //invoker("this is auth failed message");//鉴权失败
}); });
//监听rtsp/rtsps/rtmp/http-flv播放事件返回结果告知是否有播放权限(rtsp通过kBroadcastOnRtspAuth或此事件都可以实现鉴权) //监听rtsp/rtsps/rtmp/http-flv播放事件返回结果告知是否有播放权限(rtsp通过kBroadcastOnRtspAuth或此事件都可以实现鉴权)
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPlayed, [](BroadcastMediaPlayedArgs) { NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPlayed, [](BroadcastMediaPlayedArgs) {
DebugL << "播放鉴权:" << args.getUrl() << " " << args.param_strs; DebugL << "播放鉴权:" << args.getUrl() << " " << args.params;
invoker("");//鉴权成功 invoker("");//鉴权成功
//invoker("this is auth failed message");//鉴权失败 //invoker("this is auth failed message");//鉴权失败
}); });
@ -183,13 +183,13 @@ void initEventListener() {
* *
* ZLMediaKit会把其立即转发给播放器(55) * ZLMediaKit会把其立即转发给播放器(55)
*/ */
DebugL << "未找到流事件:" << args.getUrl() << " " << args.param_strs; DebugL << "未找到流事件:" << args.getUrl() << " " << args.params;
}); });
//监听播放或推流结束时消耗流量事件 //监听播放或推流结束时消耗流量事件
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastFlowReport, [](BroadcastFlowReportArgs) { NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastFlowReport, [](BroadcastFlowReportArgs) {
DebugL << "播放器(推流器)断开连接事件:" << args.getUrl() << " " << args.param_strs << "\r\n使用流量:" << totalBytes << " bytes,连接时长:" << totalDuration << ""; DebugL << "播放器(推流器)断开连接事件:" << args.getUrl() << " " << args.params << "\r\n使用流量:" << totalBytes << " bytes,连接时长:" << totalDuration << "";
}); });