EasyDarwin的RTP和RTSP会话的超时机制

今天是2016.08.05(这篇wiki写于公司内网的wiki平台上,现转载于此。),星期五,周末双休,又能回家了,但是在今天上午发生了一件事情,还是让我很惊奇,这件事情就是RTP会话被超时结束了,但是设备(拉流)还是能建立RTSP会话,只不过拉取不到任何数据,从抓包来看,设备如果拉取不到数据,就会重新建立RTSP会话,具体可以看抓包日志和流媒体服务器的日志。

当推流端开始推送RTP包后,RTSP会话会调用到RTSPSession::HandleIncomingDataPacket(),在这个函数里调用了fRTPSession->RefreshTimeout(),于是我们可以看到fRTPSession里的fTimeoutTask任务,fTimeoutTask的超时时间是又配置文件里的<SERVER>部分的rtp_timeout字段的值,也就是120秒,如果120秒没有收到任何推送的RTP包,fTimeoutTask会触发超时(详情请见TimeoutTaskThread::Run()函数),也就是会给RTPSession这个任务发送一个Task::kTimeoutEvent事件。

<PREF NAME="rtsp_timeout" TYPE="UInt32" >0</PREF>
<PREF NAME="real_rtsp_timeout" TYPE="UInt32" >180</PREF>
<PREF NAME="rtp_timeout" TYPE="UInt32" >120</PREF>
SInt64 TimeoutTaskThread::Run()
{
    //ok, check for timeouts now. Go through the whole queue
    OSMutexLocker locker(&fMutex);
    SInt64 curTime = OS::Milliseconds();
    SInt64 intervalMilli = kIntervalSeconds * 1000;//always default to 60 seconds but adjust to smallest interval > 0
    SInt64 taskInterval = intervalMilli;
    for (OSQueueIter iter(&fQueue); !iter.IsDone(); iter.Next())
    {
        TimeoutTask* theTimeoutTask = (TimeoutTask*)iter.GetCurrent()->GetEnclosingObject();
        
        //if it's time to time this task out, signal it
        if ((theTimeoutTask->fTimeoutAtThisTime > 0) && (curTime >= theTimeoutTask->fTimeoutAtThisTime))
        {
#if TIMEOUT_DEBUGGING
            qtss_printf("TimeoutTask %"_S32BITARG_" timed out. Curtime = %"_64BITARG_"d, timeout time = %"_64BITARG_"d\n",(SInt32)theTimeoutTask, curTime, theTimeoutTask->fTimeoutAtThisTime);
#endif
            theTimeoutTask->fTask->Signal(Task::kTimeoutEvent);
        }
        else
        {
            taskInterval = theTimeoutTask->fTimeoutAtThisTime - curTime;
            if ( (taskInterval > 0) && (theTimeoutTask->fTimeoutInMilSecs > 0) && (intervalMilli > taskInterval) )
                intervalMilli = taskInterval + 1000; // set timeout to 1 second past this task's timeout
#if TIMEOUT_DEBUGGING
            qtss_printf("TimeoutTask %"_S32BITARG_" not being timed out. Curtime = %"_64BITARG_"d. timeout time = %"_64BITARG_"d\n", (SInt32)theTimeoutTask, curTime, theTimeoutTask->fTimeoutAtThisTime);
#endif
        }
    }
    (void)this->GetEvents();//we must clear the event mask!
    
    OSThread::ThreadYield();
    
#if TIMEOUT_DEBUGGING
    qtss_printf ("TimeoutTaskThread::Run interval seconds= %"_S32BITARG_"\n", (SInt32) intervalMilli/1000);
#endif
    
    return intervalMilli;//don't delete me!
}

RTPSession收到kTimeoutEvent事件后会向其它模块发送QTSS_ClientSessionClosing_Role角色的事件,至此RTPSession会话任务结束掉。

SInt64 RTPSession::Run()
{
    //if we have been instructed to go away, then let's delete ourselves
    if ((events & Task::kKillEvent) || (events & Task::kTimeoutEvent) || (fModuleDoingAsyncStuff))
    {
        if (!fModuleDoingAsyncStuff)
    {
        if (events & Task::kTimeoutEvent)
            fClosingReason = qtssCliSesCloseTimeout;
            
        //deletion is a bit complicated. For one thing, it must happen from within
        //the Run function to ensure that we aren't getting events when we are deleting
        //ourselves. We also need to make sure that we aren't getting RTSP requests
        //(or, more accurately, that the stream object isn't being used by any other
        //threads). We do this by first removing the session from the session map.
        
#if RTPSESSION_DEBUGGING
        qtss_printf("RTPSession %"_S32BITARG_": about to be killed. Eventmask = %"_S32BITARG_"\n",(SInt32)this, (SInt32)events);
#endif
        // We cannot block waiting to UnRegister, because we have to
        // give the RTSPSessionTask a chance to release the RTPSession.
        OSRefTable* sessionTable = QTSServerInterface::GetServer()->GetRTPSessionMap();
        Assert(sessionTable != NULL);
        if (!sessionTable->TryUnRegister(&fRTPMapElem))
        {
            this->Signal(Task::kKillEvent);// So that we get back to this place in the code
            return kCantGetMutexIdleTime;
        }
        
            // The ClientSessionClosing role is allowed to do async stuff
            fModuleState.curTask = this;
            fModuleDoingAsyncStuff = true;  // So that we know to jump back to the
            fCurrentModule = 0;             // right place in the code
        
            // Set the reason parameter 
            theParams.clientSessionClosingParams.inReason = fClosingReason;
            
            // If RTCP packets are being generated internally for this stream, 
            // Send a BYE now.
            RTPStream** theStream = NULL;
            UInt32 theLen = 0;
            
            if (this->GetPlayFlags() & qtssPlayFlagsSendRTCP)
            {
                SInt64 byePacketTime = OS::Milliseconds();
                for (int x = 0; this->GetValuePtr(qtssCliSesStreamObjects, x, (void**)&theStream, &theLen) == QTSS_NoErr; x++)
                    if (theStream && *theStream != NULL)
                        (*theStream)->SendRTCPSR(byePacketTime, true);
            }
        }
        
        //at this point, we know no one is using this session, so invoke the
        //session cleanup role. We don't need to grab the session mutex before
        //invoking modules here, because the session is unregistered and
        //therefore there's no way another thread could get involved anyway
        UInt32 numModules = QTSServerInterface::GetNumModulesInRole(QTSSModule::kClientSessionClosingRole);
        {
            for (; fCurrentModule < numModules; fCurrentModule++)
            {  
                fModuleState.eventRequested = false;
                fModuleState.idleTime = 0;
                QTSSModule* theModule = QTSServerInterface::GetModule(QTSSModule::kClientSessionClosingRole, fCurrentModule);
                (void)theModule->CallDispatch(QTSS_ClientSessionClosing_Role, &theParams);
                // If this module has requested an event, return and wait for the event to transpire
                if (fModuleState.eventRequested)
                    return fModuleState.idleTime; // If the module has requested idle time...
            }
        }
        
        return -1;//doing this will cause the destructor to get called.
    }
}

RTSPSession的超时机制和RTPSession类似,RTSPSession的超时时间是配置文件的real_rtsp_timeout字段值,在kFilteringRequest状态时会去刷新fTimeoutTask的时间,如果超时,也会结束整个状态机的处理部分,同时调用fRTPSession的Teardown函数,结束RTPSession会话。

SInt64 RTSPSession::Run()
{
    ...
        
    //check for a timeout or a kill. If so, just consider the session dead
    if ((events & Task::kTimeoutEvent) || (events & Task::kKillEvent))
    {
    fLiveSession = false;
    }
    ...// 状态机的部分
    //fObjectHolders--  
    if(!IsLiveSession()&& fObjectHolders > 0){  
        
    OSRefTable* theMap = QTSServerInterface::GetServer()->GetRTPSessionMap();  
    OSRef* theRef = theMap->Resolve(&fLastRTPSessionIDPtr);  
    if (theRef != NULL){  
        fRTPSession = (RTPSession*)theRef->GetObject();  
        if(fRTPSession) fRTPSession->Teardown();  
        theMap->Release(fRTPSession->GetRef());  
        fRTPSession = NULL;  
    }  
    }    
    // Make absolutely sure there are no resources being occupied by the session
    // at this point.
    this->CleanupRequest();
    // Only delete if it is ok to delete!
    if (fObjectHolders == 0)
        return -1;
    // If we are here because of a timeout, but we can't delete because someone
    // is holding onto a reference to this session, just reschedule the timeout.
    //
    // At this point, however, the session is DEAD.
    return 0;

于是从以上可知,RTP会话超时是在120秒内没有收到任何RTP包就结束这个会话,此时RTSP会话还是存在的,也就是拉流端还是能够来请求拉流,鉴权也能通过,当RTSP会话在超过180秒内没有收到任何数据包则会结束整个RTSPSession。

技术交流可以入QQ群【554271674】

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,753评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,668评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,090评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,010评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,054评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,806评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,484评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,380评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,873评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,021评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,158评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,838评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,499评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,044评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,159评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,449评论 3 374
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,136评论 2 356

推荐阅读更多精彩内容