修复HLS按需生成相关bug

This commit is contained in:
xiongziliang 2020-09-12 20:42:58 +08:00
parent 3b5053af50
commit 1b9550cfc4
4 changed files with 14 additions and 5 deletions

View File

@ -557,6 +557,10 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
}
//hls文件不存在我们等待其生成并延后回复
MediaSource::findAsync(mediaInfo, strongSession, [response_file, cookie, cb, strFile, parser](const MediaSource::Ptr &src) {
if(cookie){
//尝试添加HlsMediaSource的观看人数
(*cookie)[kCookieName].get<HttpCookieAttachment>()._hls_data->addByteUsage(0);
}
if (src && File::is_file(strFile.data())) {
//流和m3u8文件都存在那么直接返回文件
response_file(cookie, cb, strFile, parser);
@ -570,7 +574,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
}
//流存在但是m3u8文件不存在那么等待生成m3u8文件
hls->waitForHls([response_file, cookie, cb, strFile, parser]() {
hls->waitForFile([response_file, cookie, cb, strFile, parser]() {
response_file(cookie, cb, strFile, parser);
});
});

View File

@ -89,7 +89,7 @@ void HlsMakerImp::onWriteHls(const char *data, int len) {
fwrite(data, len, 1, hls.get());
hls.reset();
if (_media_src) {
_media_src->registHls();
_media_src->registHls(true);
}
} else {
WarnL << "create hls file failed," << _path_hls << " " << get_uv_errmsg();

View File

@ -45,14 +45,19 @@ public:
/**
* m3u8文件时触发
* @param file_created hls文件
*/
void registHls(){
void registHls(bool file_created){
if (!_is_regist) {
_is_regist = true;
onReaderChanged(0);
regist();
}
if (!file_created) {
//没产生文件
return;
}
//m3u8文件生成发送给播放器
decltype(_list_cb) copy;
{
@ -64,7 +69,7 @@ public:
});
}
void waitForHls(function<void()> cb){
void waitForFile(function<void()> cb){
//等待生成m3u8文件
lock_guard<mutex> lck(_mtx_cb);
_list_cb.emplace_back(std::move(cb));

View File

@ -37,7 +37,7 @@ public:
_listener = listener;
_hls->getMediaSource()->setListener(shared_from_this());
//先注册媒体流,后续可以按需生成
_hls->getMediaSource()->registHls();
_hls->getMediaSource()->registHls(false);
}
int readerCount() {