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
446c0feb
Commit
446c0feb
authored
Jun 19, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
main函数启动后添加事件监听
parent
c9c2e956
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
51 行增加
和
35 行删除
+51
-35
tests/test_server.cpp
+51
-35
没有找到文件。
tests/test_server.cpp
查看文件 @
446c0feb
...
...
@@ -102,15 +102,20 @@ onceToken token1([](){
#define REALM "realm_zlmedaikit"
static
map
<
string
,
FlvRecorder
::
Ptr
>
s_mapFlvRecorder
;
static
mutex
s_mtxFlvRecorder
;
static
onceToken
s_token
([](){
void
initEventListener
()
{
static
onceToken
s_token
([]()
{
//监听kBroadcastOnGetRtspRealm事件决定rtsp链接是否需要鉴权(传统的rtsp鉴权方案)才能访问
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastOnGetRtspRealm
,[](
BroadcastOnGetRtspRealmArgs
){
DebugL
<<
"RTSP是否需要鉴权事件:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
if
(
string
(
"1"
)
==
args
.
_streamid
){
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastOnGetRtspRealm
,
[](
BroadcastOnGetRtspRealmArgs
)
{
DebugL
<<
"RTSP是否需要鉴权事件:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
if
(
string
(
"1"
)
==
args
.
_streamid
)
{
// live/1需要认证
//该流需要认证,并且设置realm
invoker
(
REALM
);
}
else
{
}
else
{
//有时我们要查询redis或数据库来判断该流是否需要认证,通过invoker的方式可以做到完全异步
//该流我们不需要认证
invoker
(
""
);
...
...
@@ -118,96 +123,106 @@ static onceToken s_token([](){
});
//监听kBroadcastOnRtspAuth事件返回正确的rtsp鉴权用户密码
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastOnRtspAuth
,[](
BroadcastOnRtspAuthArgs
){
DebugL
<<
"RTSP播放鉴权:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
DebugL
<<
"RTSP用户:"
<<
user_name
<<
(
must_no_encrypt
?
" Base64"
:
" MD5"
)
<<
" 方式登录"
;
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastOnRtspAuth
,
[](
BroadcastOnRtspAuthArgs
)
{
DebugL
<<
"RTSP播放鉴权:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
DebugL
<<
"RTSP用户:"
<<
user_name
<<
(
must_no_encrypt
?
" Base64"
:
" MD5"
)
<<
" 方式登录"
;
string
user
=
user_name
;
//假设我们异步读取数据库
if
(
user
==
"test0"
)
{
if
(
user
==
"test0"
)
{
//假设数据库保存的是明文
invoker
(
false
,
"pwd0"
);
invoker
(
false
,
"pwd0"
);
return
;
}
if
(
user
==
"test1"
)
{
if
(
user
==
"test1"
)
{
//假设数据库保存的是密文
auto
encrypted_pwd
=
MD5
(
user
+
":"
+
REALM
+
":"
+
"pwd1"
).
hexdigest
();
invoker
(
true
,
encrypted_pwd
);
invoker
(
true
,
encrypted_pwd
);
return
;
}
if
(
user
==
"test2"
&&
must_no_encrypt
)
{
if
(
user
==
"test2"
&&
must_no_encrypt
)
{
//假设登录的是test2,并且以base64方式登录,此时我们提供加密密码,那么会导致认证失败
//可以通过这个方式屏蔽base64这种不安全的加密方式
invoker
(
true
,
"pwd2"
);
invoker
(
true
,
"pwd2"
);
return
;
}
//其他用户密码跟用户名一致
invoker
(
false
,
user
);
invoker
(
false
,
user
);
});
//监听rtsp/rtmp推流事件,返回结果告知是否有推流权限
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastMediaPublish
,[](
BroadcastMediaPublishArgs
){
DebugL
<<
"推流鉴权:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastMediaPublish
,
[](
BroadcastMediaPublishArgs
)
{
DebugL
<<
"推流鉴权:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
invoker
(
""
);
//鉴权成功
//invoker("this is auth failed message");//鉴权失败
});
//监听rtsp/rtsps/rtmp/http-flv播放事件,返回结果告知是否有播放权限(rtsp通过kBroadcastOnRtspAuth或此事件都可以实现鉴权)
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastMediaPlayed
,[](
BroadcastMediaPlayedArgs
){
DebugL
<<
"播放鉴权:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastMediaPlayed
,
[](
BroadcastMediaPlayedArgs
)
{
DebugL
<<
"播放鉴权:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
invoker
(
""
);
//鉴权成功
//invoker("this is auth failed message");//鉴权失败
});
//shell登录事件,通过shell可以登录进服务器执行一些命令
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastShellLogin
,[](
BroadcastShellLoginArgs
)
{
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastShellLogin
,
[](
BroadcastShellLoginArgs
)
{
DebugL
<<
"shell login:"
<<
user_name
<<
" "
<<
passwd
;
invoker
(
""
);
//鉴权成功
//invoker("this is auth failed message");//鉴权失败
});
//监听rtsp、rtmp源注册或注销事件;此处用于测试rtmp保存为flv录像,保存在http根目录下
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastMediaChanged
,[](
BroadcastMediaChangedArgs
)
{
if
(
schema
==
RTMP_SCHEMA
&&
app
==
"live"
)
{
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastMediaChanged
,
[](
BroadcastMediaChangedArgs
)
{
if
(
schema
==
RTMP_SCHEMA
&&
app
==
"live"
)
{
lock_guard
<
mutex
>
lck
(
s_mtxFlvRecorder
);
if
(
bRegist
)
{
if
(
bRegist
)
{
DebugL
<<
"开始录制RTMP:"
<<
schema
<<
" "
<<
vhost
<<
" "
<<
app
<<
" "
<<
stream
;
GET_CONFIG
(
string
,
http_root
,
Http
::
kRootPath
);
auto
path
=
http_root
+
"/"
+
vhost
+
"/"
+
app
+
"/"
+
stream
+
"_"
+
to_string
(
time
(
NULL
))
+
".flv"
;
GET_CONFIG
(
string
,
http_root
,
Http
::
kRootPath
);
auto
path
=
http_root
+
"/"
+
vhost
+
"/"
+
app
+
"/"
+
stream
+
"_"
+
to_string
(
time
(
NULL
))
+
".flv"
;
FlvRecorder
::
Ptr
recorder
(
new
FlvRecorder
);
try
{
recorder
->
startRecord
(
EventPollerPool
::
Instance
().
getPoller
(),
dynamic_pointer_cast
<
RtmpMediaSource
>
(
sender
.
shared_from_this
()),
path
);
try
{
recorder
->
startRecord
(
EventPollerPool
::
Instance
().
getPoller
(),
dynamic_pointer_cast
<
RtmpMediaSource
>
(
sender
.
shared_from_this
()),
path
);
s_mapFlvRecorder
[
vhost
+
"/"
+
app
+
"/"
+
stream
]
=
recorder
;
}
catch
(
std
::
exception
&
ex
)
{
}
catch
(
std
::
exception
&
ex
)
{
WarnL
<<
ex
.
what
();
}
}
else
{
}
else
{
s_mapFlvRecorder
.
erase
(
vhost
+
"/"
+
app
+
"/"
+
stream
);
}
}
});
//监听播放失败(未找到特定的流)事件
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastNotFoundStream
,[](
BroadcastNotFoundStreamArgs
){
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastNotFoundStream
,
[](
BroadcastNotFoundStreamArgs
)
{
/**
* 你可以在这个事件触发时再去拉流,这样就可以实现按需拉流
* 拉流成功后,ZLMediaKit会把其立即转发给播放器(最大等待时间约为5秒,如果5秒都未拉流成功,播放器会播放失败)
*/
DebugL
<<
"未找到流事件:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
DebugL
<<
"未找到流事件:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
;
});
//监听播放或推流结束时消耗流量事件
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastFlowReport
,[](
BroadcastFlowReportArgs
){
DebugL
<<
"播放器(推流器)断开连接事件:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
<<
"
\r\n
使用流量:"
<<
totalBytes
<<
" bytes,连接时长:"
<<
totalDuration
<<
"秒"
;
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastFlowReport
,
[](
BroadcastFlowReportArgs
)
{
DebugL
<<
"播放器(推流器)断开连接事件:"
<<
args
.
_schema
<<
" "
<<
args
.
_vhost
<<
" "
<<
args
.
_app
<<
" "
<<
args
.
_streamid
<<
" "
<<
args
.
_param_strs
<<
"
\r\n
使用流量:"
<<
totalBytes
<<
" bytes,连接时长:"
<<
totalDuration
<<
"秒"
;
});
},
nullptr
);
},
nullptr
);
}
#if !defined(SIGHUP)
#define SIGHUP 1
...
...
@@ -220,6 +235,7 @@ int main(int argc,char *argv[]) {
Logger
::
Instance
().
setWriter
(
std
::
make_shared
<
AsyncLogWriter
>
());
//加载配置文件,如果配置文件不存在就创建一个
loadIniConfig
();
initEventListener
();
//这里是拉流地址,支持rtmp/rtsp协议,负载必须是H264+AAC
//如果是其他不识别的音视频将会被忽略(譬如说h264+adpcm转发后会去除音频)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论