From b0a64d1e6fc07169cc7f0bd7e38675cb8d32216a Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Fri, 9 Feb 2018 15:50:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B9=BF=E6=92=AD=E6=B7=BB=E5=8A=A0sender?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/MediaSource.cpp | 6 ++++-- src/Common/config.h | 18 +++++++++--------- src/Http/HttpSession.cpp | 6 +++--- src/MediaFile/Mp4Maker.cpp | 2 +- src/Rtmp/RtmpSession.cpp | 7 ++++--- src/Rtsp/RtspSession.cpp | 18 +++++++++++++----- src/Shell/ShellSession.cpp | 2 +- 7 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index d28aadbb..4f3355b7 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -85,7 +85,8 @@ bool MediaSource::regist() { m_strSchema, m_strVhost, m_strApp, - m_strId); + m_strId, + *this); } return success; } @@ -114,7 +115,8 @@ void MediaSource::unregisted(){ m_strSchema, m_strVhost, m_strApp, - m_strId); + m_strId, + *this); } void MediaInfo::parse(const string &url){ diff --git a/src/Common/config.h b/src/Common/config.h index c8dc03cf..037d2ae8 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -73,24 +73,24 @@ namespace Broadcast { //注册或反注册MediaSource事件广播 extern const char kBroadcastMediaChanged[]; -#define BroadcastMediaChangedArgs const bool &bRegist, const string &schema,const string &vhost,const string &app,const string &stream +#define BroadcastMediaChangedArgs const bool &bRegist, const string &schema,const string &vhost,const string &app,const string &stream,MediaSource &sender //录制mp4文件成功后广播 extern const char kBroadcastRecordMP4[]; -#define BroadcastRecordMP4Args const Mp4Info &info +#define BroadcastRecordMP4Args const Mp4Info &info,Mp4Maker &sender //收到http api请求广播 extern const char kBroadcastHttpRequest[]; -#define BroadcastHttpRequestArgs const Parser &parser,const HttpSession::HttpResponseInvoker &invoker,bool &consumed +#define BroadcastHttpRequestArgs const Parser &parser,const HttpSession::HttpResponseInvoker &invoker,bool &consumed,TcpSession &sender //该流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm.如果该事件不监听则不认证 extern const char kBroadcastOnGetRtspRealm[]; -#define BroadcastOnGetRtspRealmArgs const string &app,const string &stream,const RtspSession::onGetRealm &invoker +#define BroadcastOnGetRtspRealmArgs const string &app,const string &stream,const RtspSession::onGetRealm &invoker,TcpSession &sender //请求认证用户密码事件,user_name为用户名,must_no_encrypt如果为true,则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败 //获取到密码后请调用invoker并输入对应类型的密码和密码类型,invoker执行时会匹配密码 extern const char kBroadcastOnRtspAuth[]; -#define BroadcastOnRtspAuthArgs const string &user_name,const bool &must_no_encrypt,const RtspSession::onAuth &invoker +#define BroadcastOnRtspAuthArgs const string &user_name,const bool &must_no_encrypt,const RtspSession::onAuth &invoker,TcpSession &sender //鉴权结果回调对象 //如果errMessage为空则代表鉴权成功 @@ -98,19 +98,19 @@ typedef std::function AuthInvoker; //收到rtmp推流事件广播,通过该事件控制推流鉴权 extern const char kBroadcastRtmpPublish[]; -#define BroadcastRtmpPublishArgs const MediaInfo &args,const Broadcast::AuthInvoker &invoker +#define BroadcastRtmpPublishArgs const MediaInfo &args,const Broadcast::AuthInvoker &invoker,TcpSession &sender //播放rtsp/rtmp/http-flv事件广播,通过该事件控制播放鉴权 extern const char kBroadcastMediaPlayed[]; -#define BroadcastMediaPlayedArgs const MediaInfo &args,const Broadcast::AuthInvoker &invoker +#define BroadcastMediaPlayedArgs const MediaInfo &args,const Broadcast::AuthInvoker &invoker,TcpSession &sender //shell登录鉴权 extern const char kBroadcastShellLogin[]; -#define BroadcastShellLoginArgs const string &user_name,const string &passwd,const Broadcast::AuthInvoker &invoker +#define BroadcastShellLoginArgs const string &user_name,const string &passwd,const Broadcast::AuthInvoker &invoker,TcpSession &sender //停止rtsp/rtmp/http-flv会话后流量汇报事件广播 extern const char kBroadcastFlowReport[]; -#define BroadcastFlowReportArgs const MediaInfo &args,const uint64_t &totalBytes +#define BroadcastFlowReportArgs const MediaInfo &args,const uint64_t &totalBytes,TcpSession &sender //流量汇报事件流量阈值,单位KB,默认1MB extern const char kFlowThreshold[]; diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index a062c840..cc1e1020 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -169,7 +169,7 @@ void HttpSession::onError(const SockException& err) { if(m_previousTagSize > iFlowThreshold * 1024){ uint64_t totalBytes = m_previousTagSize; - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport,m_mediaInfo,totalBytes); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport,m_mediaInfo,totalBytes,*this); } } @@ -300,7 +300,7 @@ inline bool HttpSession::checkLiveFlvStream(){ onRes(err); }); }; - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker); + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker,*this); if(!flag){ //该事件无人监听,默认不鉴权 onRes(""); @@ -616,7 +616,7 @@ inline bool HttpSession::emitHttpEvent(bool doInvoke){ }; ///////////////////广播HTTP事件/////////////////////////// bool consumed = false;//该事件是否被消费 - NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastHttpRequest,m_parser,invoker,consumed); + NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastHttpRequest,m_parser,invoker,consumed,*this); if(!consumed && doInvoke){ //该事件无人消费,所以返回404 invoker("404 Not Found",KeyValue(),""); diff --git a/src/MediaFile/Mp4Maker.cpp b/src/MediaFile/Mp4Maker.cpp index 7e3e0d51..6ff2b715 100644 --- a/src/MediaFile/Mp4Maker.cpp +++ b/src/MediaFile/Mp4Maker.cpp @@ -223,7 +223,7 @@ void Mp4Maker::closeFile() { stat(m_strFile.data(), &fileData); m_info.ui64FileSize = fileData.st_size; //----record 业务逻辑----// - NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastRecordMP4,m_info); + NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastRecordMP4,m_info,*this); } } diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index 9aa16cda..c9319d8f 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -57,7 +57,7 @@ void RtmpSession::onError(const SockException& err) { GET_CONFIG_AND_REGISTER(uint32_t,iFlowThreshold,Broadcast::kFlowThreshold); if(m_ui64TotalBytes > iFlowThreshold * 1024){ - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport,m_mediaInfo,m_ui64TotalBytes); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport,m_mediaInfo,m_ui64TotalBytes,*this); } } @@ -179,7 +179,8 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { }; auto flag = NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastRtmpPublish, m_mediaInfo, - invoker); + invoker, + *this); if(!flag){ //该事件无人监听,默认鉴权成功 onRes(""); @@ -313,7 +314,7 @@ void RtmpSession::doPlay(AMFDecoder &dec){ onRes(err); }); }; - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker); + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker,*this); if(!flag){ //该事件无人监听,默认不鉴权 onRes(""); diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index e35bdacb..c317ea8d 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -129,7 +129,7 @@ void RtspSession::onError(const SockException& err) { //流量统计事件广播 GET_CONFIG_AND_REGISTER(uint32_t,iFlowThreshold,Broadcast::kFlowThreshold); if(m_ui64TotalBytes > iFlowThreshold * 1024){ - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport,m_mediaInfo,m_ui64TotalBytes); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport,m_mediaInfo,m_ui64TotalBytes,*this); } } @@ -227,7 +227,8 @@ bool RtspSession::handleReq_Describe() { if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnGetRtspRealm, m_mediaInfo.m_app, m_mediaInfo.m_streamid, - invoker)){ + invoker, + *this)){ //无人监听此事件,说明无需认证 invoker(""); } @@ -328,9 +329,16 @@ void RtspSession::onAuthBasic(const weak_ptr &weakSelf,const string onAuthFailed(weakSelf,realm); } }; + + auto strongSelf = weakSelf.lock(); + if(!strongSelf){ + //本对象已销毁 + return; + } + //此时必须提供明文密码 bool must_no_encrypt = true; - if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnRtspAuth,user,must_no_encrypt,invoker)){ + if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnRtspAuth,user,must_no_encrypt,invoker,*strongSelf)){ //表明该流需要认证却没监听请求密码事件,这一般是大意的程序所为,警告之 WarnL << "请监听kBroadcastOnRtspAuth事件!"; //但是我们还是忽略认证以便完成播放 @@ -412,7 +420,7 @@ void RtspSession::onAuthDigest(const weak_ptr &weakSelf,const strin //此时可以提供明文或md5加密的密码 bool must_no_encrypt = false; - if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnRtspAuth,username,must_no_encrypt,invoker)){ + if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnRtspAuth,username,must_no_encrypt,invoker,*strongSelf)){ //表明该流需要认证却没监听请求密码事件,这一般是大意的程序所为,警告之 WarnL << "请监听kBroadcastOnRtspAuth事件!"; //但是我们还是忽略认证以便完成播放 @@ -756,7 +764,7 @@ bool RtspSession::handleReq_Play() { onRes(err); }); }; - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker); + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,m_mediaInfo,invoker,*this); if(!flag){ //该事件无人监听,默认不鉴权 onRes(""); diff --git a/src/Shell/ShellSession.cpp b/src/Shell/ShellSession.cpp index 7db83742..1147d730 100644 --- a/src/Shell/ShellSession.cpp +++ b/src/Shell/ShellSession.cpp @@ -148,7 +148,7 @@ inline void ShellSession::pleaseInputPasswd() { }); }; - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastShellLogin,m_strUserName,passwd,invoker); + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastShellLogin,m_strUserName,passwd,invoker,*this); if(!flag){ //如果无人监听shell登录事件,那么默认shell无法登录 onAuth("please listen kBroadcastShellLogin event");