diff --git a/src/Common/config.h b/src/Common/config.h index 2bfe5a8d..cb98ddf2 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -85,7 +85,8 @@ extern const char kBroadcastOnRtspAuth[]; #define BroadcastOnRtspAuthArgs const string &user_name,const bool &must_no_encrypt,const RtspSession::onAuth &invoker //鉴权结果回调对象 -typedef std::function AuthInvoker; +//如果errMessage为空则代表鉴权成功 +typedef std::function AuthInvoker; //收到rtmp推流事件广播,通过该事件控制推流鉴权 extern const char kBroadcastRtmpPublish[]; diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index de0d7362..31acbcd3 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -206,7 +206,8 @@ inline bool HttpSession::checkLiveFlvStream(){ return true; } - auto onRes = [this,mediaSrc](bool authSuccess,const string &err){ + auto onRes = [this,mediaSrc](const string &err){ + bool authSuccess = err.empty(); if(!authSuccess){ sendResponse("401 Unauthorized", makeHttpHeader(true,err.size()),err); shutdown(); @@ -279,23 +280,23 @@ inline bool HttpSession::checkLiveFlvStream(){ }; weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - Broadcast::AuthInvoker invoker = [weakSelf,onRes](bool authSuccess,const string &err){ + Broadcast::AuthInvoker invoker = [weakSelf,onRes](const string &err){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ return; } - strongSelf->async([weakSelf,onRes,authSuccess,err](){ + strongSelf->async([weakSelf,onRes,err](){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ return; } - onRes(authSuccess,err); + onRes(err); }); }; auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker); if(!flag){ //该事件无人监听,默认不鉴权 - onRes(true,""); + onRes(""); } return true; } diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index 5e1ed8c4..9c9d533a 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -134,12 +134,13 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { dec.load();/* NULL */ m_mediaInfo.parse(m_strTcUrl + "/" + dec.load()); - auto onRes = [this](bool authSuccess,const string &err){ + auto onRes = [this](const string &err){ auto src = dynamic_pointer_cast(MediaSource::find(RTMP_SCHEMA, m_mediaInfo.m_vhost, m_mediaInfo.m_app, m_mediaInfo.m_streamid, false)); + bool authSuccess = err.empty(); bool ok = (!src && !m_pPublisherSrc && authSuccess); AMFValue status(AMF_OBJECT); status.set("level", ok ? "status" : "error"); @@ -162,17 +163,17 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { }; weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - Broadcast::AuthInvoker invoker = [weakSelf,onRes](bool success,const string &err){ + Broadcast::AuthInvoker invoker = [weakSelf,onRes](const string &err){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ return; } - strongSelf->async([weakSelf,onRes,success,err](){ + strongSelf->async([weakSelf,onRes,err](){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ return; } - onRes(success,err); + onRes(err); }); }; auto flag = NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastRtmpPublish, @@ -180,7 +181,7 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { invoker); if(!flag){ //该事件无人监听,默认鉴权成功 - onRes(true,""); + onRes(""); } } @@ -194,12 +195,13 @@ void RtmpSession::onCmd_deleteStream(AMFDecoder &dec) { } void RtmpSession::doPlay(AMFDecoder &dec){ - auto onRes = [this](bool authSuccess,const string &err) { + auto onRes = [this](const string &err) { auto src = dynamic_pointer_cast(MediaSource::find(RTMP_SCHEMA, m_mediaInfo.m_vhost, m_mediaInfo.m_app, m_mediaInfo.m_streamid, true)); + bool authSuccess = err.empty(); bool ok = (src.operator bool() && authSuccess); if(ok){ ok = ok && src->ready(); @@ -297,23 +299,23 @@ void RtmpSession::doPlay(AMFDecoder &dec){ }; weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - Broadcast::AuthInvoker invoker = [weakSelf,onRes](bool authSuccess,const string &err){ + Broadcast::AuthInvoker invoker = [weakSelf,onRes](const string &err){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ return; } - strongSelf->async([weakSelf,onRes,authSuccess,err](){ + strongSelf->async([weakSelf,onRes,err](){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ return; } - onRes(authSuccess,err); + onRes(err); }); }; auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker); if(!flag){ //该事件无人监听,默认不鉴权 - onRes(true,""); + onRes(""); } } void RtmpSession::onCmd_play2(AMFDecoder &dec) { diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 28853e78..68da37a9 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -649,7 +649,8 @@ bool RtspSession::handleReq_Play() { send_SessionNotFound(); return false; } - auto onRes = [this](bool authSuccess,const string &err){ + auto onRes = [this](const string &err){ + bool authSuccess = err.empty(); char response[2 * 1024]; m_pcBuf = response; if(!authSuccess && m_bFirstPlay){ @@ -742,23 +743,23 @@ bool RtspSession::handleReq_Play() { }; weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - Broadcast::AuthInvoker invoker = [weakSelf,onRes](bool authSuccess,const string &err){ + Broadcast::AuthInvoker invoker = [weakSelf,onRes](const string &err){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ return; } - strongSelf->async([weakSelf,onRes,authSuccess,err](){ + strongSelf->async([weakSelf,onRes,err](){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ return; } - onRes(authSuccess,err); + onRes(err); }); }; auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker); if(!flag){ //该事件无人监听,默认不鉴权 - onRes(true,""); + onRes(""); } return true; }