EasyDarwin的RTP和RTSP會話的超時機(jī)制

今天是2016.08.05(這篇wiki寫于公司內(nèi)網(wǎng)的wiki平臺上,現(xiàn)轉(zhuǎn)載于此。),星期五,周末雙休,又能回家了,但是在今天上午發(fā)生了一件事情,還是讓我很驚奇,這件事情就是RTP會話被超時結(jié)束了,但是設(shè)備(拉流)還是能建立RTSP會話,只不過拉取不到任何數(shù)據(jù),從抓包來看,設(shè)備如果拉取不到數(shù)據(jù),就會重新建立RTSP會話,具體可以看抓包日志和流媒體服務(wù)器的日志。

當(dāng)推流端開始推送RTP包后,RTSP會話會調(diào)用到RTSPSession::HandleIncomingDataPacket(),在這個函數(shù)里調(diào)用了fRTPSession->RefreshTimeout(),于是我們可以看到fRTPSession里的fTimeoutTask任務(wù),fTimeoutTask的超時時間是又配置文件里的<SERVER>部分的rtp_timeout字段的值,也就是120秒,如果120秒沒有收到任何推送的RTP包,fTimeoutTask會觸發(fā)超時(詳情請見TimeoutTaskThread::Run()函數(shù)),也就是會給RTPSession這個任務(wù)發(fā)送一個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事件后會向其它模塊發(fā)送QTSS_ClientSessionClosing_Role角色的事件,至此RTPSession會話任務(wù)結(jié)束掉。

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的超時機(jī)制和RTPSession類似,RTSPSession的超時時間是配置文件的real_rtsp_timeout字段值,在kFilteringRequest狀態(tài)時會去刷新fTimeoutTask的時間,如果超時,也會結(jié)束整個狀態(tài)機(jī)的處理部分,同時調(diào)用fRTPSession的Teardown函數(shù),結(jié)束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;
    }
    ...// 狀態(tài)機(jī)的部分
    //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秒內(nèi)沒有收到任何RTP包就結(jié)束這個會話,此時RTSP會話還是存在的,也就是拉流端還是能夠來請求拉流,鑒權(quán)也能通過,當(dāng)RTSP會話在超過180秒內(nèi)沒有收到任何數(shù)據(jù)包則會結(jié)束整個RTSPSession。

技術(shù)交流可以入QQ群【554271674】

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容