确保hls播放器持续播放 (#2896)

在hls注销后,hls cookie会继续存活60秒,在此期间,如果hls流重新注册,将导致无法继续播放;
通过此修改,在hls注销后每3秒查询一次MediaSource,可以在性能和功能间保持平衡。
This commit is contained in:
PioLing 2023-10-12 11:05:41 +08:00 committed by GitHub
parent c25e76311f
commit 6348e64cdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 15 deletions

View File

@ -61,7 +61,7 @@ bool HttpServerCookie::isExpired() {
return _ticker.elapsedTime() > _max_elapsed * 1000;
}
void HttpServerCookie::setAttach(std::shared_ptr<void> attach) {
void HttpServerCookie::setAttach(toolkit::Any attach) {
_attach = std::move(attach);
}
@ -114,8 +114,7 @@ void HttpCookieManager::onManager() {
}
}
HttpServerCookie::Ptr HttpCookieManager::addCookie(const string &cookie_name, const string &uid_in,
uint64_t max_elapsed, std::shared_ptr<void> attach, int max_client) {
HttpServerCookie::Ptr HttpCookieManager::addCookie(const string &cookie_name, const string &uid_in, uint64_t max_elapsed, toolkit::Any attach, int max_client) {
lock_guard<recursive_mutex> lck(_mtx_cookie);
auto cookie = _generator.obtain();
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>
T& getAttach() {
return *static_cast<T *>(_attach.get());
return _attach.get<T>();
}
private:
@ -104,7 +104,7 @@ private:
std::string _cookie_uuid;
uint64_t _max_elapsed;
toolkit::Ticker _ticker;
std::shared_ptr<void> _attach;
toolkit::Any _attach;
std::weak_ptr<HttpCookieManager> _manager;
};
@ -163,7 +163,7 @@ public:
*/
HttpServerCookie::Ptr addCookie(
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);
/**

View File

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