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

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

View File

@@ -299,7 +299,6 @@ public:
std::string full_url;
std::string schema;
std::string host;
std::string param_strs;
};
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;
}
void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const string &stream_id) {
_info.app = app;
_info.stream = stream_id;
_info.vhost = vhost;
void HlsMakerImp::setMediaSource(const MediaTuple& tuple) {
static_cast<MediaTuple &>(_info) = tuple;
_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

View File

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

View File

@@ -22,12 +22,10 @@ using namespace toolkit;
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;
/////record 业务逻辑//////
_info.app = app;
_info.stream = stream_id;
_info.vhost = vhost;
static_cast<MediaTuple &>(_info) = tuple;
_info.folder = path;
GET_CONFIG(uint32_t, s_max_second, Protocol::kMP4MaxSecond);
_max_second = max_second ? max_second : s_max_second;

View File

@@ -26,7 +26,7 @@ class MP4Recorder final : public MediaSinkInterface {
public:
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;
/**

View File

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

View File

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