等待流注册时间可配置化

This commit is contained in:
xiongziliang 2019-05-28 09:25:41 +08:00
parent 22e39dc240
commit 94ccd27f47
7 changed files with 17 additions and 10 deletions

View File

@ -43,7 +43,6 @@ MediaSource::SchemaVhostAppStreamMap MediaSource::g_mapMediaSrc;
void MediaSource::findAsync(const MediaInfo &info,
const std::shared_ptr<TcpSession> &session,
bool retry,
int maxWaitMs,
const function<void(const MediaSource::Ptr &src)> &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);

View File

@ -128,7 +128,6 @@ public:
static void findAsync(const MediaInfo &info,
const std::shared_ptr<TcpSession> &session,
bool retry,
int maxWaitMs,
const function<void(const MediaSource::Ptr &src)> &cb);
const string& getSchema() const {

View File

@ -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

View File

@ -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))

View File

@ -222,7 +222,7 @@ inline bool HttpSession::checkLiveFlvStream(){
bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt);
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(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){
//本对象已经销毁

View File

@ -306,7 +306,7 @@ void RtmpSession::doPlayResponse(const string &err,const std::function<void(bool
//鉴权成功,查找媒体源并回复
_mediaInfo._schema = RTMP_SCHEMA;
weak_ptr<RtmpSession> weakSelf = dynamic_pointer_cast<RtmpSession>(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<RtmpMediaSource>(src);
auto strongSelf = weakSelf.lock();
if(strongSelf){

View File

@ -304,7 +304,7 @@ bool RtspSession::handleReq_Describe(const Parser &parser) {
_mediaInfo._schema = RTSP_SCHEMA;
auto authorization = parser["Authorization"];
weak_ptr<RtspSession> weakSelf = dynamic_pointer_cast<RtspSession>(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;