Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
e37982ad
Unverified
Commit
e37982ad
authored
Feb 12, 2022
by
夏楚
Committed by
GitHub
Feb 12, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化hls媒体流注册与无人观看清理机制 (#1421)
* 优化hls媒体流注册与无人观看清理机制 * 优化hls cookie管理 * bug fixed
parent
2fbd8de7
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
62 行增加
和
91 行删除
+62
-91
src/Http/HttpFileManager.cpp
+34
-59
src/Record/HlsMakerImp.cpp
+1
-1
src/Record/HlsMediaSource.cpp
+1
-1
src/Record/HlsMediaSource.h
+14
-15
src/Record/HlsRecorder.h
+12
-15
没有找到文件。
src/Http/HttpFileManager.cpp
查看文件 @
e37982ad
...
@@ -38,15 +38,8 @@ public:
...
@@ -38,15 +38,8 @@ public:
string
_path
;
string
_path
;
//上次鉴权失败信息,为空则上次鉴权成功
//上次鉴权失败信息,为空则上次鉴权成功
string
_err_msg
;
string
_err_msg
;
//本cookie是否为hls直播的
bool
_is_hls
=
false
;
//hls直播时的其他一些信息,主要用于播放器个数计数以及流量计数
//hls直播时的其他一些信息,主要用于播放器个数计数以及流量计数
HlsCookieData
::
Ptr
_hls_data
;
HlsCookieData
::
Ptr
_hls_data
;
//如果是hls直播,那么判断该cookie是否使用过MediaSource::findAsync查找过
//如果程序未正常退出,会残余上次的hls文件,所以判断hls直播是否存在的关键不是文件存在与否
//而是应该判断HlsMediaSource是否已注册,但是这样会每次获取m3u8文件时都会用MediaSource::findAsync判断一次
//会导致程序性能低下,所以我们应该在cookie声明周期的第一次判断HlsMediaSource是否已经注册,后续通过文件存在与否判断
bool
_have_find_media_source
=
false
;
};
};
const
string
&
HttpFileManager
::
getContentType
(
const
char
*
name
)
{
const
string
&
HttpFileManager
::
getContentType
(
const
char
*
name
)
{
...
@@ -245,40 +238,39 @@ public:
...
@@ -245,40 +238,39 @@ public:
* 5、触发kBroadcastHttpAccess事件
* 5、触发kBroadcastHttpAccess事件
*/
*/
static
void
canAccessPath
(
TcpSession
&
sender
,
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
bool
is_dir
,
static
void
canAccessPath
(
TcpSession
&
sender
,
const
Parser
&
parser
,
const
MediaInfo
&
media_info
,
bool
is_dir
,
const
function
<
void
(
const
string
&
err
M
sg
,
const
HttpServerCookie
::
Ptr
&
cookie
)
>
&
callback
)
{
const
function
<
void
(
const
string
&
err
_m
sg
,
const
HttpServerCookie
::
Ptr
&
cookie
)
>
&
callback
)
{
//获取用户唯一id
//获取用户唯一id
auto
uid
=
parser
.
Params
();
auto
uid
=
parser
.
Params
();
auto
path
=
parser
.
Url
();
auto
path
=
parser
.
Url
();
//先根据http头中的cookie字段获取cookie
//先根据http头中的cookie字段获取cookie
HttpServerCookie
::
Ptr
cookie
=
HttpCookieManager
::
Instance
().
getCookie
(
kCookieName
,
parser
.
getHeader
());
HttpServerCookie
::
Ptr
cookie
=
HttpCookieManager
::
Instance
().
getCookie
(
kCookieName
,
parser
.
getHeader
());
//
如果不是从http头中找到的cookie,我们让http客户端设置下
cookie
//
是否需要更新
cookie
bool
cookie_from_header
=
tru
e
;
bool
update_cookie
=
fals
e
;
if
(
!
cookie
&&
!
uid
.
empty
())
{
if
(
!
cookie
&&
!
uid
.
empty
())
{
//客户端请求中无cookie,再根据该用户的用户id获取cookie
//客户端请求中无cookie,再根据该用户的用户id获取cookie
cookie
=
HttpCookieManager
::
Instance
().
getCookieByUid
(
kCookieName
,
uid
);
cookie
=
HttpCookieManager
::
Instance
().
getCookieByUid
(
kCookieName
,
uid
);
cookie_from_header
=
fals
e
;
update_cookie
=
tru
e
;
}
}
if
(
cookie
)
{
if
(
cookie
)
{
//找到了cookie,对cookie上锁先
auto
&
attach
=
cookie
->
getAttach
<
HttpCookieAttachment
>
();
auto
&
attach
=
cookie
->
getAttach
<
HttpCookieAttachment
>
();
if
(
path
.
find
(
attach
.
_path
)
==
0
)
{
if
(
path
.
find
(
attach
.
_path
)
==
0
)
{
//上次cookie是限定本目录
//上次cookie是限定本目录
if
(
attach
.
_err_msg
.
empty
())
{
if
(
attach
.
_err_msg
.
empty
())
{
//上次鉴权成功
//上次鉴权成功
if
(
attach
.
_
is_hls
)
{
if
(
attach
.
_
hls_data
)
{
//如果播放的是hls,那么刷新hls的cookie(获取ts文件也会刷新)
//如果播放的是hls,那么刷新hls的cookie(获取ts文件也会刷新)
cookie
->
updateTime
();
cookie
->
updateTime
();
cookie_from_header
=
fals
e
;
update_cookie
=
tru
e
;
}
}
callback
(
""
,
cookie_from_header
?
nullptr
:
cookie
);
callback
(
""
,
update_cookie
?
cookie
:
nullptr
);
return
;
return
;
}
}
//上次鉴权失败,但是如果url参数发生变更,那么也重新鉴权下
//上次鉴权失败,但是如果url参数发生变更,那么也重新鉴权下
if
(
parser
.
Params
().
empty
()
||
parser
.
Params
()
==
cookie
->
getUid
())
{
if
(
parser
.
Params
().
empty
()
||
parser
.
Params
()
==
cookie
->
getUid
())
{
//url参数未变,或者本来就没有url参数,那么判断本次请求为重复请求,无访问权限
//url参数未变,或者本来就没有url参数,那么判断本次请求为重复请求,无访问权限
callback
(
attach
.
_err_msg
,
cookie_from_header
?
nullptr
:
cookie
);
callback
(
attach
.
_err_msg
,
update_cookie
?
cookie
:
nullptr
);
return
;
return
;
}
}
}
}
...
@@ -312,13 +304,9 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI
...
@@ -312,13 +304,9 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI
attach
->
_path
=
cookie_path
;
attach
->
_path
=
cookie_path
;
//记录能否访问
//记录能否访问
attach
->
_err_msg
=
err_msg
;
attach
->
_err_msg
=
err_msg
;
//记录访问的是否为hls
attach
->
_is_hls
=
is_hls
;
if
(
is_hls
)
{
if
(
is_hls
)
{
// hls相关信息
// hls相关信息
attach
->
_hls_data
=
std
::
make_shared
<
HlsCookieData
>
(
media_info
,
info
);
attach
->
_hls_data
=
std
::
make_shared
<
HlsCookieData
>
(
media_info
,
info
);
// hls未查找MediaSource
attach
->
_have_find_media_source
=
false
;
}
}
callback
(
err_msg
,
HttpCookieManager
::
Instance
().
addCookie
(
kCookieName
,
uid
,
life_second
,
attach
));
callback
(
err_msg
,
HttpCookieManager
::
Instance
().
addCookie
(
kCookieName
,
uid
,
life_second
,
attach
));
}
else
{
}
else
{
...
@@ -383,19 +371,19 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
...
@@ -383,19 +371,19 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
weak_ptr
<
TcpSession
>
weakSession
=
sender
.
shared_from_this
();
weak_ptr
<
TcpSession
>
weakSession
=
sender
.
shared_from_this
();
//判断是否有权限访问该文件
//判断是否有权限访问该文件
canAccessPath
(
sender
,
parser
,
media_info
,
false
,
[
cb
,
file_path
,
parser
,
is_hls
,
media_info
,
weakSession
,
file_exist
](
const
string
&
err
M
sg
,
const
HttpServerCookie
::
Ptr
&
cookie
)
{
canAccessPath
(
sender
,
parser
,
media_info
,
false
,
[
cb
,
file_path
,
parser
,
is_hls
,
media_info
,
weakSession
,
file_exist
](
const
string
&
err
_m
sg
,
const
HttpServerCookie
::
Ptr
&
cookie
)
{
auto
strongSession
=
weakSession
.
lock
();
auto
strongSession
=
weakSession
.
lock
();
if
(
!
strongSession
)
{
if
(
!
strongSession
)
{
//http客户端已经断开,不需要回复
//http客户端已经断开,不需要回复
return
;
return
;
}
}
if
(
!
err
M
sg
.
empty
())
{
if
(
!
err
_m
sg
.
empty
())
{
//文件鉴权失败
//文件鉴权失败
StrCaseMap
headerOut
;
StrCaseMap
headerOut
;
if
(
cookie
)
{
if
(
cookie
)
{
headerOut
[
"Set-Cookie"
]
=
cookie
->
getCookie
(
cookie
->
getAttach
<
HttpCookieAttachment
>
().
_path
);
headerOut
[
"Set-Cookie"
]
=
cookie
->
getCookie
(
cookie
->
getAttach
<
HttpCookieAttachment
>
().
_path
);
}
}
cb
(
401
,
"text/html"
,
headerOut
,
std
::
make_shared
<
HttpStringBody
>
(
err
M
sg
));
cb
(
401
,
"text/html"
,
headerOut
,
std
::
make_shared
<
HttpStringBody
>
(
err
_m
sg
));
return
;
return
;
}
}
...
@@ -408,7 +396,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
...
@@ -408,7 +396,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
HttpSession
::
HttpResponseInvoker
invoker
=
[
&
](
int
code
,
const
StrCaseMap
&
headerOut
,
const
HttpBody
::
Ptr
&
body
)
{
HttpSession
::
HttpResponseInvoker
invoker
=
[
&
](
int
code
,
const
StrCaseMap
&
headerOut
,
const
HttpBody
::
Ptr
&
body
)
{
if
(
cookie
&&
file_exist
)
{
if
(
cookie
&&
file_exist
)
{
auto
&
attach
=
cookie
->
getAttach
<
HttpCookieAttachment
>
();
auto
&
attach
=
cookie
->
getAttach
<
HttpCookieAttachment
>
();
if
(
attach
.
_
is_hls
)
{
if
(
attach
.
_
hls_data
)
{
attach
.
_hls_data
->
addByteUsage
(
body
->
remainSize
());
attach
.
_hls_data
->
addByteUsage
(
body
->
remainSize
());
}
}
}
}
...
@@ -417,50 +405,37 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
...
@@ -417,50 +405,37 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
invoker
.
responseFile
(
parser
.
getHeader
(),
httpHeader
,
file_content
.
empty
()
?
file_path
:
file_content
,
!
is_hls
,
file_content
.
empty
());
invoker
.
responseFile
(
parser
.
getHeader
(),
httpHeader
,
file_content
.
empty
()
?
file_path
:
file_content
,
!
is_hls
,
file_content
.
empty
());
};
};
if
(
!
is_hls
)
{
if
(
!
is_hls
||
!
cookie
)
{
//不是hls
,
直接回复文件或404
//不是hls
或访问m3u8文件不带cookie,
直接回复文件或404
response_file
(
cookie
,
cb
,
file_path
,
parser
);
response_file
(
cookie
,
cb
,
file_path
,
parser
);
if
(
is_hls
)
{
WarnL
<<
"access m3u8 file without cookie:"
<<
file_path
;
}
return
;
return
;
}
}
//是hls直播,判断HLS直播流是否已经注册
auto
src
=
cookie
->
getAttach
<
HttpCookieAttachment
>
().
_hls_data
->
getMediaSource
();
bool
have_find_media_src
=
false
;
if
(
src
)
{
if
(
cookie
)
{
//直接从内存获取m3u8索引文件(而不是从文件系统)
auto
&
attach
=
cookie
->
getAttach
<
HttpCookieAttachment
>
();
response_file
(
cookie
,
cb
,
file_path
,
parser
,
src
->
getIndexFile
());
have_find_media_src
=
attach
.
_have_find_media_source
;
if
(
!
have_find_media_src
)
{
const_cast
<
HttpCookieAttachment
&>
(
attach
).
_have_find_media_source
=
true
;
}
else
{
auto
src
=
attach
.
_hls_data
->
getMediaSource
();
if
(
src
)
{
//直接从内存获取m3u8索引文件(而不是从文件系统)
response_file
(
cookie
,
cb
,
file_path
,
parser
,
src
->
getIndexFile
());
return
;
}
}
}
if
(
have_find_media_src
)
{
//之前该cookie已经通过MediaSource::findAsync查找过了,所以现在只以文件系统查找结果为准
response_file
(
cookie
,
cb
,
file_path
,
parser
);
return
;
return
;
}
}
//hls文件不存在,我们等待其生成并延后回复
MediaSource
::
findAsync
(
media_info
,
strongSession
,
[
response_file
,
cookie
,
cb
,
file_path
,
parser
](
const
MediaSource
::
Ptr
&
src
)
{
if
(
cookie
)
{
//尝试添加HlsMediaSource的观看人数(HLS是按需生成的,这样可以触发HLS文件的生成)
auto
&
attach
=
cookie
->
getAttach
<
HttpCookieAttachment
>
();
attach
.
_hls_data
->
addByteUsage
(
0
);
attach
.
_hls_data
->
setMediaSource
(
dynamic_pointer_cast
<
HlsMediaSource
>
(
src
));
}
//hls流可能未注册,MediaSource::findAsync可以触发not_found事件,然后再按需推拉流
MediaSource
::
findAsync
(
media_info
,
strongSession
,
[
response_file
,
cookie
,
cb
,
file_path
,
parser
](
const
MediaSource
::
Ptr
&
src
)
{
auto
hls
=
dynamic_pointer_cast
<
HlsMediaSource
>
(
src
);
auto
hls
=
dynamic_pointer_cast
<
HlsMediaSource
>
(
src
);
if
(
!
hls
)
{
if
(
!
hls
)
{
//流不
存在,那么直接返回文件(相当于纯粹的HLS文件服务器,但是会挂起播放器15秒左右(用于等待HLS流的注册))
//流不
在线
response_file
(
cookie
,
cb
,
file_path
,
parser
);
response_file
(
cookie
,
cb
,
file_path
,
parser
);
return
;
return
;
}
}
//可能异步获取m3u8索引文件
auto
&
attach
=
cookie
->
getAttach
<
HttpCookieAttachment
>
();
attach
.
_hls_data
->
setMediaSource
(
hls
);
//添加HlsMediaSource的观看人数(HLS是按需生成的,这样可以触发HLS文件的生成)
attach
.
_hls_data
->
addByteUsage
(
0
);
// 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
);
});
});
...
@@ -519,15 +494,15 @@ void HttpFileManager::onAccessPath(TcpSession &sender, Parser &parser, const Htt
...
@@ -519,15 +494,15 @@ void HttpFileManager::onAccessPath(TcpSession &sender, Parser &parser, const Htt
return
;
return
;
}
}
//判断是否有权限访问该目录
//判断是否有权限访问该目录
canAccessPath
(
sender
,
parser
,
media_info
,
true
,
[
strMenu
,
cb
](
const
string
&
err
M
sg
,
const
HttpServerCookie
::
Ptr
&
cookie
)
mutable
{
canAccessPath
(
sender
,
parser
,
media_info
,
true
,
[
strMenu
,
cb
](
const
string
&
err
_m
sg
,
const
HttpServerCookie
::
Ptr
&
cookie
)
mutable
{
if
(
!
err
M
sg
.
empty
())
{
if
(
!
err
_m
sg
.
empty
())
{
strMenu
=
err
M
sg
;
strMenu
=
err
_m
sg
;
}
}
StrCaseMap
headerOut
;
StrCaseMap
headerOut
;
if
(
cookie
)
{
if
(
cookie
)
{
headerOut
[
"Set-Cookie"
]
=
cookie
->
getCookie
(
cookie
->
getAttach
<
HttpCookieAttachment
>
().
_path
);
headerOut
[
"Set-Cookie"
]
=
cookie
->
getCookie
(
cookie
->
getAttach
<
HttpCookieAttachment
>
().
_path
);
}
}
cb
(
err
M
sg
.
empty
()
?
200
:
401
,
"text/html"
,
headerOut
,
std
::
make_shared
<
HttpStringBody
>
(
strMenu
));
cb
(
err
_m
sg
.
empty
()
?
200
:
401
,
"text/html"
,
headerOut
,
std
::
make_shared
<
HttpStringBody
>
(
strMenu
));
});
});
return
;
return
;
}
}
...
...
src/Record/HlsMakerImp.cpp
查看文件 @
e37982ad
...
@@ -117,7 +117,7 @@ void HlsMakerImp::onWriteHls(const std::string &data) {
...
@@ -117,7 +117,7 @@ void HlsMakerImp::onWriteHls(const std::string &data) {
fwrite
(
data
.
data
(),
data
.
size
(),
1
,
hls
.
get
());
fwrite
(
data
.
data
(),
data
.
size
(),
1
,
hls
.
get
());
hls
.
reset
();
hls
.
reset
();
if
(
_media_src
)
{
if
(
_media_src
)
{
_media_src
->
registHls
(
data
);
_media_src
->
setIndexFile
(
data
);
}
}
}
else
{
}
else
{
WarnL
<<
"create hls file failed,"
<<
_path_hls
<<
" "
<<
get_uv_errmsg
();
WarnL
<<
"create hls file failed,"
<<
_path_hls
<<
" "
<<
get_uv_errmsg
();
...
...
src/Record/HlsMediaSource.cpp
查看文件 @
e37982ad
...
@@ -23,7 +23,7 @@ HlsCookieData::HlsCookieData(const MediaInfo &info, const std::shared_ptr<SockIn
...
@@ -23,7 +23,7 @@ HlsCookieData::HlsCookieData(const MediaInfo &info, const std::shared_ptr<SockIn
void
HlsCookieData
::
addReaderCount
()
{
void
HlsCookieData
::
addReaderCount
()
{
if
(
!*
_added
)
{
if
(
!*
_added
)
{
auto
src
=
std
::
dynamic_pointer_cast
<
HlsMediaSource
>
(
MediaSource
::
find
(
HLS_SCHEMA
,
_info
.
_vhost
,
_info
.
_app
,
_info
.
_streamid
)
);
auto
src
=
getMediaSource
(
);
if
(
src
)
{
if
(
src
)
{
*
_added
=
true
;
*
_added
=
true
;
_ring_reader
=
src
->
getRing
()
->
attach
(
EventPollerPool
::
Instance
().
getPoller
());
_ring_reader
=
src
->
getRing
()
->
attach
(
EventPollerPool
::
Instance
().
getPoller
());
...
...
src/Record/HlsMediaSource.h
查看文件 @
e37982ad
...
@@ -39,12 +39,10 @@ public:
...
@@ -39,12 +39,10 @@ public:
int
readerCount
()
override
{
return
_ring
?
_ring
->
readerCount
()
:
0
;
}
int
readerCount
()
override
{
return
_ring
?
_ring
->
readerCount
()
:
0
;
}
/**
/**
* 生成m3u8文件时触发
* 设置或清空m3u8索引文件内容
* @param index_file m3u8文件内容
*/
*/
void
registHls
(
std
::
string
index_file
)
{
void
setIndexFile
(
std
::
string
index_file
)
{
if
(
!
_is_regist
)
{
if
(
!
_ring
)
{
_is_regist
=
true
;
std
::
weak_ptr
<
HlsMediaSource
>
weakSelf
=
std
::
dynamic_pointer_cast
<
HlsMediaSource
>
(
shared_from_this
());
std
::
weak_ptr
<
HlsMediaSource
>
weakSelf
=
std
::
dynamic_pointer_cast
<
HlsMediaSource
>
(
shared_from_this
());
auto
lam
=
[
weakSelf
](
int
size
)
{
auto
lam
=
[
weakSelf
](
int
size
)
{
auto
strongSelf
=
weakSelf
.
lock
();
auto
strongSelf
=
weakSelf
.
lock
();
...
@@ -58,19 +56,19 @@ public:
...
@@ -58,19 +56,19 @@ public:
regist
();
regist
();
}
}
if
(
index_file
.
empty
())
{
//没产生索引文件, 只是为了触发媒体注册
return
;
}
//赋值m3u8索引文件内容
//赋值m3u8索引文件内容
std
::
lock_guard
<
std
::
mutex
>
lck
(
_mtx_index
);
std
::
lock_guard
<
std
::
mutex
>
lck
(
_mtx_index
);
_index_file
=
std
::
move
(
index_file
);
_index_file
=
std
::
move
(
index_file
);
_list_cb
.
for_each
([
&
](
const
std
::
function
<
void
(
const
std
::
string
&
str
)
>
&
cb
)
{
cb
(
_index_file
);
});
if
(
!
_index_file
.
empty
())
{
_list_cb
.
clear
();
_list_cb
.
for_each
([
&
](
const
std
::
function
<
void
(
const
std
::
string
&
str
)
>
&
cb
)
{
cb
(
_index_file
);
});
_list_cb
.
clear
();
}
}
}
/**
* 异步获取m3u8文件
*/
void
getIndexFile
(
std
::
function
<
void
(
const
std
::
string
&
str
)
>
cb
)
{
void
getIndexFile
(
std
::
function
<
void
(
const
std
::
string
&
str
)
>
cb
)
{
std
::
lock_guard
<
std
::
mutex
>
lck
(
_mtx_index
);
std
::
lock_guard
<
std
::
mutex
>
lck
(
_mtx_index
);
if
(
!
_index_file
.
empty
())
{
if
(
!
_index_file
.
empty
())
{
...
@@ -81,6 +79,9 @@ public:
...
@@ -81,6 +79,9 @@ public:
_list_cb
.
emplace_back
(
std
::
move
(
cb
));
_list_cb
.
emplace_back
(
std
::
move
(
cb
));
}
}
/**
* 同步获取m3u8文件
*/
std
::
string
getIndexFile
()
const
{
std
::
string
getIndexFile
()
const
{
std
::
lock_guard
<
std
::
mutex
>
lck
(
_mtx_index
);
std
::
lock_guard
<
std
::
mutex
>
lck
(
_mtx_index
);
return
_index_file
;
return
_index_file
;
...
@@ -89,12 +90,10 @@ public:
...
@@ -89,12 +90,10 @@ public:
void
onSegmentSize
(
size_t
bytes
)
{
_speed
[
TrackVideo
]
+=
bytes
;
}
void
onSegmentSize
(
size_t
bytes
)
{
_speed
[
TrackVideo
]
+=
bytes
;
}
private
:
private
:
bool
_is_regist
=
false
;
RingType
::
Ptr
_ring
;
RingType
::
Ptr
_ring
;
std
::
string
_index_file
;
std
::
string
_index_file
;
mutable
std
::
mutex
_mtx_index
;
mutable
std
::
mutex
_mtx_index
;
toolkit
::
List
<
std
::
function
<
void
(
const
std
::
string
&
)
>
>
_list_cb
;
toolkit
::
List
<
std
::
function
<
void
(
const
std
::
string
&
)
>>
_list_cb
;
};
};
class
HlsCookieData
{
class
HlsCookieData
{
...
...
src/Record/HlsRecorder.h
查看文件 @
e37982ad
...
@@ -38,36 +38,28 @@ public:
...
@@ -38,36 +38,28 @@ public:
void
setListener
(
const
std
::
weak_ptr
<
MediaSourceEvent
>
&
listener
)
{
void
setListener
(
const
std
::
weak_ptr
<
MediaSourceEvent
>
&
listener
)
{
setDelegate
(
listener
);
setDelegate
(
listener
);
_hls
->
getMediaSource
()
->
setListener
(
shared_from_this
());
_hls
->
getMediaSource
()
->
setListener
(
shared_from_this
());
//先注册媒体流,后续可以按需生成
_hls
->
getMediaSource
()
->
registHls
(
""
);
}
}
int
readerCount
()
{
int
readerCount
()
{
return
_hls
->
getMediaSource
()
->
readerCount
();
}
return
_hls
->
getMediaSource
()
->
readerCount
();
}
void
onReaderChanged
(
MediaSource
&
sender
,
int
size
)
override
{
void
onReaderChanged
(
MediaSource
&
sender
,
int
size
)
override
{
GET_CONFIG
(
bool
,
hls_demand
,
General
::
kHlsDemand
);
GET_CONFIG
(
bool
,
hls_demand
,
General
::
kHlsDemand
);
//hls保留切片个数为0时代表为hls录制(不删除切片),那么不管有无观看者都一直生成hls
//
hls保留切片个数为0时代表为hls录制(不删除切片),那么不管有无观看者都一直生成hls
_enabled
=
hls_demand
?
(
_hls
->
isLive
()
?
size
:
true
)
:
true
;
_enabled
=
hls_demand
?
(
_hls
->
isLive
()
?
size
:
true
)
:
true
;
if
(
!
size
&&
_hls
->
isLive
()
&&
hls_demand
)
{
if
(
!
size
&&
_hls
->
isLive
()
&&
hls_demand
)
{
//hls直播时,如果无人观看就删除视频缓存,目的是为了防止视频跳跃
//
hls直播时,如果无人观看就删除视频缓存,目的是为了防止视频跳跃
_clear_cache
=
true
;
_clear_cache
=
true
;
}
}
MediaSourceEventInterceptor
::
onReaderChanged
(
sender
,
size
);
MediaSourceEventInterceptor
::
onReaderChanged
(
sender
,
size
);
}
}
bool
isEnabled
()
{
GET_CONFIG
(
bool
,
hls_demand
,
General
::
kHlsDemand
);
//缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
return
hls_demand
?
(
_clear_cache
?
true
:
_enabled
)
:
true
;
}
bool
inputFrame
(
const
Frame
::
Ptr
&
frame
)
override
{
bool
inputFrame
(
const
Frame
::
Ptr
&
frame
)
override
{
GET_CONFIG
(
bool
,
hls_demand
,
General
::
kHlsDemand
);
GET_CONFIG
(
bool
,
hls_demand
,
General
::
kHlsDemand
);
if
(
_clear_cache
&&
hls_demand
)
{
if
(
_clear_cache
&&
hls_demand
)
{
_clear_cache
=
false
;
_clear_cache
=
false
;
//清空旧的m3u8索引文件于ts切片
_hls
->
clearCache
();
_hls
->
clearCache
();
_hls
->
getMediaSource
()
->
setIndexFile
(
""
);
}
}
if
(
_enabled
||
!
hls_demand
)
{
if
(
_enabled
||
!
hls_demand
)
{
return
MpegMuxer
::
inputFrame
(
frame
);
return
MpegMuxer
::
inputFrame
(
frame
);
...
@@ -75,6 +67,12 @@ public:
...
@@ -75,6 +67,12 @@ public:
return
false
;
return
false
;
}
}
bool
isEnabled
()
{
GET_CONFIG
(
bool
,
hls_demand
,
General
::
kHlsDemand
);
//缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
return
hls_demand
?
(
_clear_cache
?
true
:
_enabled
)
:
true
;
}
private
:
private
:
void
onWrite
(
std
::
shared_ptr
<
toolkit
::
Buffer
>
buffer
,
uint32_t
timestamp
,
bool
key_pos
)
override
{
void
onWrite
(
std
::
shared_ptr
<
toolkit
::
Buffer
>
buffer
,
uint32_t
timestamp
,
bool
key_pos
)
override
{
if
(
!
buffer
)
{
if
(
!
buffer
)
{
...
@@ -85,8 +83,7 @@ private:
...
@@ -85,8 +83,7 @@ private:
}
}
private
:
private
:
//默认不生成hls文件,有播放器时再生成
bool
_enabled
=
true
;
bool
_enabled
=
false
;
bool
_clear_cache
=
false
;
bool
_clear_cache
=
false
;
std
::
shared_ptr
<
HlsMakerImp
>
_hls
;
std
::
shared_ptr
<
HlsMakerImp
>
_hls
;
};
};
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论