优化hls索引查找相关性能

This commit is contained in:
admin 2023-10-11 16:07:08 +08:00
parent c25e76311f
commit 3c07123448
19 changed files with 23 additions and 15 deletions

0
Android/gradlew vendored Executable file → Normal file
View File

0
api/include/mk_common.h Executable file → Normal file
View File

0
api/include/mk_httpclient.h Executable file → Normal file
View File

0
api/include/mk_media.h Executable file → Normal file
View File

0
api/include/mk_mediakit.h Executable file → Normal file
View File

0
api/include/mk_player.h Executable file → Normal file
View File

0
api/source/mk_httpclient.cpp Executable file → Normal file
View File

0
api/source/mk_media.cpp Executable file → Normal file
View File

0
api/source/mk_player.cpp Executable file → Normal file
View File

0
cmake/AndroidNdkGdb.cmake Executable file → Normal file
View File

0
cmake/AndroidNdkModules.cmake Executable file → Normal file
View File

0
cmake/android.toolchain.cmake Executable file → Normal file
View File

0
server/WebApi.cpp Executable file → Normal file
View File

0
server/WebApi.h Executable file → Normal file
View File

0
server/WebHook.cpp Executable file → Normal file
View File

0
server/WebHook.h Executable file → Normal file
View File

View File

@ -61,7 +61,7 @@ bool HttpServerCookie::isExpired() {
return _ticker.elapsedTime() > _max_elapsed * 1000; return _ticker.elapsedTime() > _max_elapsed * 1000;
} }
void HttpServerCookie::setAttach(std::shared_ptr<void> attach) { void HttpServerCookie::setAttach(toolkit::Any attach) {
_attach = std::move(attach); _attach = std::move(attach);
} }
@ -114,8 +114,7 @@ void HttpCookieManager::onManager() {
} }
} }
HttpServerCookie::Ptr HttpCookieManager::addCookie(const string &cookie_name, const string &uid_in, HttpServerCookie::Ptr HttpCookieManager::addCookie(const string &cookie_name, const string &uid_in, uint64_t max_elapsed, toolkit::Any attach, int max_client) {
uint64_t max_elapsed, std::shared_ptr<void> attach, int max_client) {
lock_guard<recursive_mutex> lck(_mtx_cookie); lock_guard<recursive_mutex> lck(_mtx_cookie);
auto cookie = _generator.obtain(); auto cookie = _generator.obtain();
auto uid = uid_in.empty() ? cookie : uid_in; auto uid = uid_in.empty() ? cookie : uid_in;

View File

@ -85,14 +85,14 @@ public:
/** /**
* *
*/ */
void setAttach(std::shared_ptr<void> attach); void setAttach(toolkit::Any attach);
/* /*
* *
*/ */
template <class T> template <class T>
T& getAttach() { T& getAttach() {
return *static_cast<T *>(_attach.get()); return _attach.get<T>();
} }
private: private:
@ -104,7 +104,7 @@ private:
std::string _cookie_uuid; std::string _cookie_uuid;
uint64_t _max_elapsed; uint64_t _max_elapsed;
toolkit::Ticker _ticker; toolkit::Ticker _ticker;
std::shared_ptr<void> _attach; toolkit::Any _attach;
std::weak_ptr<HttpCookieManager> _manager; std::weak_ptr<HttpCookieManager> _manager;
}; };
@ -163,7 +163,7 @@ public:
*/ */
HttpServerCookie::Ptr addCookie( HttpServerCookie::Ptr addCookie(
const std::string &cookie_name, const std::string &uid, uint64_t max_elapsed = COOKIE_DEFAULT_LIFE, const std::string &cookie_name, const std::string &uid, uint64_t max_elapsed = COOKIE_DEFAULT_LIFE,
std::shared_ptr<void> attach = nullptr, toolkit::Any = toolkit::Any{},
int max_client = 1); int max_client = 1);
/** /**

View File

@ -31,13 +31,16 @@ namespace mediakit {
// 每次访问一次该cookie那么将重新刷新cookie有效期 // 每次访问一次该cookie那么将重新刷新cookie有效期
// 假如播放器在60秒内都未访问该cookie那么将重新触发hls播放鉴权 // 假如播放器在60秒内都未访问该cookie那么将重新触发hls播放鉴权
static int kHlsCookieSecond = 60; static int kHlsCookieSecond = 60;
static int kFindSrcIntervalSecond = 3;
static const string kCookieName = "ZL_COOKIE"; static const string kCookieName = "ZL_COOKIE";
static const string kHlsSuffix = "/hls.m3u8"; static const string kHlsSuffix = "/hls.m3u8";
static const string kHlsFMP4Suffix = "/hls.fmp4.m3u8"; static const string kHlsFMP4Suffix = "/hls.fmp4.m3u8";
struct HttpCookieAttachment { struct HttpCookieAttachment {
//是否已经查找到过MediaSource // 是否已经查找到过MediaSource
bool _find_src = false; bool _find_src = false;
// 查找MediaSource计时
Ticker _find_src_ticker;
//cookie生效作用域本cookie只对该目录下的文件生效 //cookie生效作用域本cookie只对该目录下的文件生效
string _path; string _path;
//上次鉴权失败信息,为空则上次鉴权成功 //上次鉴权失败信息,为空则上次鉴权成功
@ -414,7 +417,9 @@ static void canAccessPath(Session &sender, const Parser &parser, const MediaInfo
// hls相关信息 // hls相关信息
attach->_hls_data = std::make_shared<HlsCookieData>(media_info, info); attach->_hls_data = std::make_shared<HlsCookieData>(media_info, info);
} }
callback(err_msg, HttpCookieManager::Instance().addCookie(kCookieName, uid, life_second, attach)); toolkit::Any any;
any.set(std::move(attach));
callback(err_msg, HttpCookieManager::Instance().addCookie(kCookieName, uid, life_second, std::move(any)));
} else { } else {
callback(err_msg, nullptr); callback(err_msg, nullptr);
} }
@ -532,14 +537,15 @@ static void accessFile(Session &sender, const Parser &parser, const MediaInfo &m
return; return;
} }
auto src = cookie->getAttach<HttpCookieAttachment>()._hls_data->getMediaSource(); auto &attach = cookie->getAttach<HttpCookieAttachment>();
auto src = attach._hls_data->getMediaSource();
if (src) { if (src) {
//直接从内存获取m3u8索引文件(而不是从文件系统) // 直接从内存获取m3u8索引文件(而不是从文件系统)
response_file(cookie, cb, file_path, parser, src->getIndexFile()); response_file(cookie, cb, file_path, parser, src->getIndexFile());
return; return;
} }
if (cookie->getAttach<HttpCookieAttachment>()._find_src) { if (attach._find_src && attach._find_src_ticker.elapsedTime() < kFindSrcIntervalSecond * 1000) {
//查找过MediaSource但是流已经注销了不用再查找 // 最近已经查找过MediaSource了为了防止频繁查找导致占用全局互斥锁的问题我们尝试直接从磁盘返回hls索引文件
response_file(cookie, cb, file_path, parser); response_file(cookie, cb, file_path, parser);
return; return;
} }
@ -555,11 +561,14 @@ static void accessFile(Session &sender, const Parser &parser, const MediaInfo &m
auto &attach = cookie->getAttach<HttpCookieAttachment>(); auto &attach = cookie->getAttach<HttpCookieAttachment>();
attach._hls_data->setMediaSource(hls); attach._hls_data->setMediaSource(hls);
//添加HlsMediaSource的观看人数(HLS是按需生成的这样可以触发HLS文件的生成) // 添加HlsMediaSource的观看人数(HLS是按需生成的这样可以触发HLS文件的生成)
attach._hls_data->addByteUsage(0); attach._hls_data->addByteUsage(0);
//标记找到MediaSource // 标记找到MediaSource
attach._find_src = true; attach._find_src = true;
// 重置查找MediaSource计时
attach._find_src_ticker.resetTime();
// m3u8文件可能不存在, 等待m3u8索引文件按需生成 // m3u8文件可能不存在, 等待m3u8索引文件按需生成
hls->getIndexFile([response_file, file_path, cookie, cb, parser](const string &file) { hls->getIndexFile([response_file, file_path, cookie, cb, parser](const string &file) {
response_file(cookie, cb, file_path, parser, file); response_file(cookie, cb, file_path, parser, file);