但是在今天上午發生了一件事情,還是讓我很驚奇,這件事情就是RTP會話被超時結束了,但是設備(拉流)還是能建立RTSP會話,只不過拉取不到任何數據,從抓包來看,設備如果拉取不到數據,就會重新建立RTSP會話,具體可以看抓包日誌和流媒體服務器的日誌。
當推流端開始推送RTP包後,RTSP會話會調用到RTSPSession::HandleIncomingDataPacket(),在這個函數里調用了fRTPSession->RefreshTimeout(),於是我們可以看到fRTPSession裡的fTimeoutTask任務,fTimeoutTask的超時時間是又配置文件裡的
0 180 120
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。每天會更新論文和視頻,還有如果想學習c++知識在晚上8.30免費觀看這個直播:https://ke.qq.com/course/131973#tuin=b52b9a80
閱讀更多 IT布丁老師 的文章
關鍵字: Task RTPSession return