From 94ccd27f47cc63c877566f58c66c9eeeadb9e56c Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 28 May 2019 09:25:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AD=89=E5=BE=85=E6=B5=81=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=8F=AF=E9=85=8D=E7=BD=AE=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/MediaSource.cpp | 12 +++++++----- src/Common/MediaSource.h | 1 - src/Common/config.cpp | 3 ++- src/Common/config.h | 5 +++++ src/Http/HttpSession.cpp | 2 +- src/Rtmp/RtmpSession.cpp | 2 +- src/Rtsp/RtspSession.cpp | 2 +- 7 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index d885c473..0313118a 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -43,7 +43,6 @@ MediaSource::SchemaVhostAppStreamMap MediaSource::g_mapMediaSrc; void MediaSource::findAsync(const MediaInfo &info, const std::shared_ptr &session, bool retry, - int maxWaitMs, const function &cb){ auto src = MediaSource::find(info._schema, @@ -61,15 +60,18 @@ void MediaSource::findAsync(const MediaInfo &info, //广播未找到流,此时可以立即去拉流,这样还来得及 NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastNotFoundStream,info,*session); + //最多等待一定时间,如果这个时间内,流未注册上,那么返回未找到流 + GET_CONFIG_AND_REGISTER(int,maxWaitMS,Broadcast::kMaxStreamWaitTimeMS); + //若干秒后执行等待媒体注册超时回调 - auto onRegistTimeout = session->getPoller()->doDelayTask(maxWaitMs,[cb,listener_tag](){ + auto onRegistTimeout = session->getPoller()->doDelayTask(maxWaitMS,[cb,listener_tag](){ //取消监听该事件 NoticeCenter::Instance().delListener(listener_tag,Broadcast::kBroadcastMediaChanged); cb(nullptr); return 0; }); - auto onRegist = [listener_tag,weakSession,info,cb,maxWaitMs,onRegistTimeout](BroadcastMediaChangedArgs) { + auto onRegist = [listener_tag,weakSession,info,cb,onRegistTimeout](BroadcastMediaChangedArgs) { if(!bRegist || schema != info._schema || vhost != info._vhost || app != info._app ||stream != info._streamid){ //不是自己感兴趣的事件,忽略之 return; @@ -85,14 +87,14 @@ void MediaSource::findAsync(const MediaInfo &info, } //切换到自己的线程再回复 - strongSession->async([listener_tag,weakSession,info,cb,maxWaitMs](){ + strongSession->async([listener_tag,weakSession,info,cb](){ auto strongSession = weakSession.lock(); if(!strongSession) { return; } DebugL << "收到媒体注册事件,回复播放器:" << info._schema << "/" << info._vhost << "/" << info._app << "/" << info._streamid; //再找一遍媒体源,一般能找到 - findAsync(info,strongSession,false,maxWaitMs,cb); + findAsync(info,strongSession,false,cb); //取消事件监听 NoticeCenter::Instance().delListener(listener_tag,Broadcast::kBroadcastMediaChanged); }, false); diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 887b1c0c..436f8128 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -128,7 +128,6 @@ public: static void findAsync(const MediaInfo &info, const std::shared_ptr &session, bool retry, - int maxWaitMs, const function &cb); const string& getSchema() const { diff --git a/src/Common/config.cpp b/src/Common/config.cpp index d8cc3b47..9ba8c38a 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -69,11 +69,12 @@ const char kBroadcastStreamNoneReader[] = "kBroadcastStreamNoneReader"; const char kFlowThreshold[] = "broadcast.flowThreshold"; const char kStreamNoneReaderDelayMS[] = "broadcast.streamNoneReaderDelayMS"; - +const char kMaxStreamWaitTimeMS[] = "kMaxStreamWaitTimeMS"; onceToken token([](){ mINI::Instance()[kFlowThreshold] = 1024; mINI::Instance()[kStreamNoneReaderDelayMS] = 5 * 1000; + mINI::Instance()[kMaxStreamWaitTimeMS] = 5 * 1000; },nullptr); } //namespace Broadcast diff --git a/src/Common/config.h b/src/Common/config.h index 24c98208..a96878cd 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -127,9 +127,14 @@ extern const char kFlowThreshold[]; //默认连续5秒无人观看然后触发kBroadcastStreamNoneReader事件 extern const char kStreamNoneReaderDelayMS[]; +//等待流注册超时时间,收到播放器后请求后,如果未找到相关流,服务器会等待一定时间, +//如果在这个时间内,相关流注册上了,那么服务器会立即响应播放器播放成功, +//否则会最多等待kMaxStreamWaitTimeMS毫秒,然后响应播放器播放失败 +extern const char kMaxStreamWaitTimeMS[]; //更新配置文件事件广播,执行loadIniConfig函数加载配置文件成功后会触发该广播 extern const char kBroadcastReloadConfig[]; + #define BroadcastReloadConfigArgs void #define ReloadConfigTag ((void *)(0xFF)) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 6ab76a38..ce73239d 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -222,7 +222,7 @@ inline bool HttpSession::checkLiveFlvStream(){ bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt); weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,5000,[weakSelf,bClose,this](const MediaSource::Ptr &src){ + MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf,bClose,this](const MediaSource::Ptr &src){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ //本对象已经销毁 diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index 835ee27a..c164ebd8 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -306,7 +306,7 @@ void RtmpSession::doPlayResponse(const string &err,const std::function weakSelf = dynamic_pointer_cast(shared_from_this()); - MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,5000,[weakSelf,cb](const MediaSource::Ptr &src){ + MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf,cb](const MediaSource::Ptr &src){ auto rtmp_src = dynamic_pointer_cast(src); auto strongSelf = weakSelf.lock(); if(strongSelf){ diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index b80daaa7..82980ada 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -304,7 +304,7 @@ bool RtspSession::handleReq_Describe(const Parser &parser) { _mediaInfo._schema = RTSP_SCHEMA; auto authorization = parser["Authorization"]; weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,5000,[weakSelf,authorization](const MediaSource::Ptr &src){ + MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf,authorization](const MediaSource::Ptr &src){ auto strongSelf = weakSelf.lock(); if(!strongSelf){ return;