mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-25 20:27:34 +08:00
修复android测试程序bug
This commit is contained in:
parent
4122084e05
commit
c0a6981662
@ -102,112 +102,158 @@ static onceToken token1([](){
|
|||||||
#define REALM "realm_zlmedaikit"
|
#define REALM "realm_zlmedaikit"
|
||||||
static map<string,FlvRecorder::Ptr> s_mapFlvRecorder;
|
static map<string,FlvRecorder::Ptr> s_mapFlvRecorder;
|
||||||
static mutex s_mtxFlvRecorder;
|
static mutex s_mtxFlvRecorder;
|
||||||
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 ){
|
|
||||||
// live/1需要认证
|
|
||||||
//该流需要认证,并且设置realm
|
|
||||||
invoker(REALM);
|
|
||||||
}else{
|
|
||||||
//有时我们要查询redis或数据库来判断该流是否需要认证,通过invoker的方式可以做到完全异步
|
|
||||||
//该流我们不需要认证
|
|
||||||
invoker("");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//监听kBroadcastOnRtspAuth事件返回正确的rtsp鉴权用户密码
|
static void initEvent() {
|
||||||
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastOnRtspAuth,[](BroadcastOnRtspAuthArgs){
|
static onceToken s_token([]() {
|
||||||
DebugL << "RTSP播放鉴权:" << args._schema << " " << args._vhost << " " << args._app << " " << args._streamid << " " << args._param_strs ;
|
//监听kBroadcastOnGetRtspRealm事件决定rtsp链接是否需要鉴权(传统的rtsp鉴权方案)才能访问
|
||||||
DebugL << "RTSP用户:" << user_name << (must_no_encrypt ? " Base64" : " MD5" )<< " 方式登录";
|
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastOnGetRtspRealm,
|
||||||
string user = user_name;
|
[](BroadcastOnGetRtspRealmArgs) {
|
||||||
//假设我们异步读取数据库
|
DebugL << "RTSP是否需要鉴权事件:" << args._schema << " "
|
||||||
if(user == "test0"){
|
<< args._vhost << " " << args._app << " "
|
||||||
//假设数据库保存的是明文
|
<< args._streamid << " "
|
||||||
invoker(false,"pwd0");
|
<< args._param_strs;
|
||||||
return;
|
if (string("1") == args._streamid) {
|
||||||
}
|
// live/1需要认证
|
||||||
|
//该流需要认证,并且设置realm
|
||||||
|
invoker(REALM);
|
||||||
|
} else {
|
||||||
|
//有时我们要查询redis或数据库来判断该流是否需要认证,通过invoker的方式可以做到完全异步
|
||||||
|
//该流我们不需要认证
|
||||||
|
invoker("");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if(user == "test1"){
|
//监听kBroadcastOnRtspAuth事件返回正确的rtsp鉴权用户密码
|
||||||
//假设数据库保存的是密文
|
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastOnRtspAuth,
|
||||||
auto encrypted_pwd = MD5(user + ":" + REALM + ":" + "pwd1").hexdigest();
|
[](BroadcastOnRtspAuthArgs) {
|
||||||
invoker(true,encrypted_pwd);
|
DebugL << "RTSP播放鉴权:" << args._schema << " "
|
||||||
return;
|
<< args._vhost << " " << args._app << " "
|
||||||
}
|
<< args._streamid << " "
|
||||||
if(user == "test2" && must_no_encrypt){
|
<< args._param_strs;
|
||||||
//假设登录的是test2,并且以base64方式登录,此时我们提供加密密码,那么会导致认证失败
|
DebugL << "RTSP用户:" << user_name
|
||||||
//可以通过这个方式屏蔽base64这种不安全的加密方式
|
<< (must_no_encrypt ? " Base64" : " MD5")
|
||||||
invoker(true,"pwd2");
|
<< " 方式登录";
|
||||||
return;
|
string user = user_name;
|
||||||
}
|
//假设我们异步读取数据库
|
||||||
|
if (user == "test0") {
|
||||||
|
//假设数据库保存的是明文
|
||||||
|
invoker(false, "pwd0");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//其他用户密码跟用户名一致
|
if (user == "test1") {
|
||||||
invoker(false,user);
|
//假设数据库保存的是密文
|
||||||
});
|
auto encrypted_pwd = MD5(
|
||||||
|
user + ":" + REALM + ":" +
|
||||||
|
"pwd1").hexdigest();
|
||||||
|
invoker(true, encrypted_pwd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (user == "test2" && must_no_encrypt) {
|
||||||
|
//假设登录的是test2,并且以base64方式登录,此时我们提供加密密码,那么会导致认证失败
|
||||||
|
//可以通过这个方式屏蔽base64这种不安全的加密方式
|
||||||
|
invoker(true, "pwd2");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//其他用户密码跟用户名一致
|
||||||
|
invoker(false, user);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
//监听rtsp/rtmp推流事件,返回结果告知是否有推流权限
|
//监听rtsp/rtmp推流事件,返回结果告知是否有推流权限
|
||||||
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastMediaPublish,[](BroadcastMediaPublishArgs){
|
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPublish,
|
||||||
DebugL << "推流鉴权:" << args._schema << " " << args._vhost << " " << args._app << " " << args._streamid << " " << args._param_strs ;
|
[](BroadcastMediaPublishArgs) {
|
||||||
invoker("");//鉴权成功
|
DebugL << "推流鉴权:" << args._schema << " "
|
||||||
//invoker("this is auth failed message");//鉴权失败
|
<< args._vhost << " " << args._app << " "
|
||||||
});
|
<< args._streamid << " "
|
||||||
|
<< args._param_strs;
|
||||||
|
invoker("");//鉴权成功
|
||||||
|
//invoker("this is auth failed message");//鉴权失败
|
||||||
|
});
|
||||||
|
|
||||||
//监听rtsp/rtsps/rtmp/http-flv播放事件,返回结果告知是否有播放权限(rtsp通过kBroadcastOnRtspAuth或此事件都可以实现鉴权)
|
//监听rtsp/rtsps/rtmp/http-flv播放事件,返回结果告知是否有播放权限(rtsp通过kBroadcastOnRtspAuth或此事件都可以实现鉴权)
|
||||||
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastMediaPlayed,[](BroadcastMediaPlayedArgs){
|
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPlayed,
|
||||||
DebugL << "播放鉴权:" << args._schema << " " << args._vhost << " " << args._app << " " << args._streamid << " " << args._param_strs ;
|
[](BroadcastMediaPlayedArgs) {
|
||||||
invoker("");//鉴权成功
|
DebugL << "播放鉴权:" << args._schema << " "
|
||||||
//invoker("this is auth failed message");//鉴权失败
|
<< args._vhost << " " << args._app << " "
|
||||||
});
|
<< args._streamid << " "
|
||||||
|
<< args._param_strs;
|
||||||
|
invoker("");//鉴权成功
|
||||||
|
//invoker("this is auth failed message");//鉴权失败
|
||||||
|
});
|
||||||
|
|
||||||
//shell登录事件,通过shell可以登录进服务器执行一些命令
|
//shell登录事件,通过shell可以登录进服务器执行一些命令
|
||||||
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastShellLogin,[](BroadcastShellLoginArgs){
|
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastShellLogin,
|
||||||
DebugL << "shell login:" << user_name << " " << passwd;
|
[](BroadcastShellLoginArgs) {
|
||||||
invoker("");//鉴权成功
|
DebugL << "shell login:" << user_name << " "
|
||||||
//invoker("this is auth failed message");//鉴权失败
|
<< passwd;
|
||||||
});
|
invoker("");//鉴权成功
|
||||||
|
//invoker("this is auth failed message");//鉴权失败
|
||||||
|
});
|
||||||
|
|
||||||
//监听rtsp、rtmp源注册或注销事件;此处用于测试rtmp保存为flv录像,保存在http根目录下
|
//监听rtsp、rtmp源注册或注销事件;此处用于测试rtmp保存为flv录像,保存在http根目录下
|
||||||
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastMediaChanged,[](BroadcastMediaChangedArgs){
|
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaChanged,
|
||||||
if(schema == RTMP_SCHEMA && app == "live"){
|
[](BroadcastMediaChangedArgs) {
|
||||||
lock_guard<mutex> lck(s_mtxFlvRecorder);
|
if (schema == RTMP_SCHEMA && app == "live") {
|
||||||
if(bRegist){
|
lock_guard<mutex> lck(s_mtxFlvRecorder);
|
||||||
DebugL << "开始录制RTMP:" << schema << " " << vhost << " " << app << " " << stream;
|
if (bRegist) {
|
||||||
GET_CONFIG(string,http_root,Http::kRootPath);
|
DebugL << "开始录制RTMP:" << schema << " "
|
||||||
auto path = http_root + "/" + vhost + "/" + app + "/" + stream + "_" + to_string(time(NULL)) + ".flv";
|
<< vhost << " " << app << " "
|
||||||
FlvRecorder::Ptr recorder(new FlvRecorder);
|
<< stream;
|
||||||
try{
|
GET_CONFIG(string, http_root,
|
||||||
recorder->startRecord(EventPollerPool::Instance().getPoller(),dynamic_pointer_cast<RtmpMediaSource>(sender.shared_from_this()),path);
|
Http::kRootPath);
|
||||||
s_mapFlvRecorder[vhost + "/" + app + "/" + stream] = recorder;
|
auto path = http_root + "/" + vhost + "/" +
|
||||||
}catch(std::exception &ex){
|
app + "/" + stream + "_" +
|
||||||
WarnL << ex.what();
|
to_string(time(NULL)) + ".flv";
|
||||||
}
|
FlvRecorder::Ptr recorder(new FlvRecorder);
|
||||||
}else{
|
try {
|
||||||
s_mapFlvRecorder.erase(vhost + "/" + app + "/" + stream);
|
recorder->startRecord(
|
||||||
}
|
EventPollerPool::Instance().getPoller(),
|
||||||
}
|
dynamic_pointer_cast<RtmpMediaSource>(
|
||||||
});
|
sender.shared_from_this()),
|
||||||
|
path);
|
||||||
|
s_mapFlvRecorder[vhost + "/" + app +
|
||||||
|
"/" +
|
||||||
|
stream] = recorder;
|
||||||
|
} catch (std::exception &ex) {
|
||||||
|
WarnL << ex.what();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
s_mapFlvRecorder.erase(
|
||||||
|
vhost + "/" + app + "/" + stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//监听播放失败(未找到特定的流)事件
|
//监听播放失败(未找到特定的流)事件
|
||||||
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastNotFoundStream,[](BroadcastNotFoundStreamArgs){
|
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastNotFoundStream,
|
||||||
/**
|
[](BroadcastNotFoundStreamArgs) {
|
||||||
* 你可以在这个事件触发时再去拉流,这样就可以实现按需拉流
|
/**
|
||||||
* 拉流成功后,ZLMediaKit会把其立即转发给播放器(最大等待时间约为5秒,如果5秒都未拉流成功,播放器会播放失败)
|
* 你可以在这个事件触发时再去拉流,这样就可以实现按需拉流
|
||||||
*/
|
* 拉流成功后,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){
|
NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastFlowReport,
|
||||||
DebugL << "播放器(推流器)断开连接事件:" << args._schema << " " << args._vhost << " " << args._app << " " << args._streamid << " " << args._param_strs
|
[](BroadcastFlowReportArgs) {
|
||||||
<< "\r\n使用流量:" << totalBytes << " bytes,连接时长:" << totalDuration << "秒" ;
|
DebugL << "播放器(推流器)断开连接事件:" << args._schema << " "
|
||||||
|
<< args._vhost << " " << args._app << " "
|
||||||
|
<< args._streamid << " " << args._param_strs
|
||||||
|
<< "\r\n使用流量:" << totalBytes
|
||||||
|
<< " bytes,连接时长:" << totalDuration << "秒";
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}, nullptr);
|
}, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
#if !defined(SIGHUP)
|
#if !defined(SIGHUP)
|
||||||
#define SIGHUP 1
|
#define SIGHUP 1
|
||||||
@ -296,6 +342,7 @@ static int do_main(string ini_file) {
|
|||||||
TcpServer::Ptr rtspSSLSrv(new TcpServer());
|
TcpServer::Ptr rtspSSLSrv(new TcpServer());
|
||||||
rtspSSLSrv->start<RtspSessionWithSSL>(rtspsPort);//默认322
|
rtspSSLSrv->start<RtspSessionWithSSL>(rtspsPort);//默认322
|
||||||
|
|
||||||
|
initEvent();
|
||||||
//服务器支持动态切换端口(不影响现有连接)
|
//服务器支持动态切换端口(不影响现有连接)
|
||||||
NoticeCenter::Instance().addListener(ReloadConfigTag,Broadcast::kBroadcastReloadConfig,[&](BroadcastReloadConfigArgs){
|
NoticeCenter::Instance().addListener(ReloadConfigTag,Broadcast::kBroadcastReloadConfig,[&](BroadcastReloadConfigArgs){
|
||||||
//重新创建服务器
|
//重新创建服务器
|
||||||
|
Loading…
Reference in New Issue
Block a user