RTPFileSession::~RTPFileSession() { // Check to see if we should destroy this file OSMutexLocker locker (sOpenFileMap.GetMutex()); #if RTPFILESESSIONDEBUG qtss_printf("Dropping refcount on file\n"); #endif if (fFile == NULL) return; sOpenFileMap.Release(fFile->GetRef()); if (fFile->GetRef()->GetRefCount() == 0) { #if RTPFILESESSIONDEBUG qtss_printf("Refcount dropped to 0. Deleting file\n"); #endif sOpenFileMap.UnRegister(fFile->GetRef()); delete fFile; } // Delete our data buffer delete [] fDataBuffer; delete [] fTrackInfo; }
SInt64 ReflectorSessionCheckTask::Run() { OSRefTable* reflectorSessionMap = QTSServerInterface::GetServer()->GetReflectorSessionMap(); OSMutexLocker locker(reflectorSessionMap->GetMutex()); SInt64 sNowTime = OS::Milliseconds(); for (OSRefHashTableIter theIter(reflectorSessionMap->GetHashTable()); !theIter.IsDone(); theIter.Next()) { OSRef* theRef = theIter.GetCurrent(); ReflectorSession* theSession = (ReflectorSession*)theRef->GetObject(); SInt64 sCreateTime = theSession->GetInitTimeMS(); if( (theSession->GetNumOutputs() == 0) && (sNowTime-sCreateTime >= 20*1000) ) { QTSS_RoleParams theParams; theParams.easyFreeStreamParams.inStreamName = theSession->GetStreamName(); UInt32 numModules = QTSServerInterface::GetNumModulesInRole(QTSSModule::kEasyCMSFreeStreamRole); for ( UInt32 currentModule=0;currentModule < numModules; currentModule++) { qtss_printf("没有客户端观看当前转发媒体\n"); QTSSModule* theModule = QTSServerInterface::GetModule(QTSSModule::kEasyCMSFreeStreamRole, currentModule); (void)theModule->CallDispatch(Easy_CMSFreeStream_Role, &theParams); } } } return 30*1000;//30s }
QTSS_Error EasyHLSClose(Easy_HLSClose_Params* inParams) { OSRefTable* sHLSSessionMap = QTSServerInterface::GetServer()->GetHLSSessionMap(); OSMutexLocker locker (sHLSSessionMap->GetMutex()); //首先查找Map里面是否已经有了对应的流 StrPtrLen streamName(inParams->inStreamName); OSRef* clientSesRef = sHLSSessionMap->Resolve(&streamName); if(NULL == clientSesRef) return QTSS_RequestFailed; EasyHLSSession* session = (EasyHLSSession*)clientSesRef->GetObject(); session->HLSSessionRelease(); sHLSSessionMap->Release(session->GetRef()); if (session->GetRef()->GetRefCount() == 0) { qtss_printf("EasyHLSModule.cpp:EasyHLSClose UnRegister and delete session =%p refcount=%"_U32BITARG_"\n", session->GetRef(), session->GetRef()->GetRefCount() ) ; sHLSSessionMap->UnRegister(session->GetRef()); delete session; } return QTSS_NoErr; }
void* QTSSCallbacks::Easy_GetRTSPPushSessions() { OSRefTable* reflectorSessionMap = QTSServerInterface::GetServer()->GetReflectorSessionMap(); EasyMsgSCRTSPPushSessionListACK ack; ack.SetHeaderValue(EASY_TAG_VERSION, "1.0"); ack.SetHeaderValue(EASY_TAG_CSEQ, "1"); UInt32 uIndex= 0; OSMutexLocker locker(reflectorSessionMap->GetMutex()); for (OSRefHashTableIter theIter(reflectorSessionMap->GetHashTable()); !theIter.IsDone(); theIter.Next()) { OSRef* theRef = theIter.GetCurrent(); ReflectorSession* theSession = (ReflectorSession*)theRef->GetObject(); EasyDarwinRTSPSession session; session.index = uIndex; char* fullRequestURL = NULL; RTPSession* clientSession = (RTPSession*) theSession->GetBroadcasterSession(); if(clientSession == NULL) continue; clientSession->GetValueAsString(qtssCliSesFullURL,0,&fullRequestURL); session.Url = fullRequestURL; session.Name = theSession->GetSessionName(); session.numOutputs = theSession->GetNumOutputs(); ack.AddSession(session); uIndex++; } char count[16] = { 0 }; sprintf(count,"%d", uIndex); ack.SetBodyValue(EASY_TAG_SESSION_COUNT, count); string msg = ack.GetMsg(); UInt32 theMsgLen = strlen(msg.c_str()); char* retMsg = new char[theMsgLen+1]; retMsg[theMsgLen] = '\0'; strncpy(retMsg, msg.c_str(), strlen(msg.c_str())); return (void*)retMsg; }
QTSS_Error EasyHLSOpen(Easy_RecordOpen_Params* inParams) { OSRefTable* sHLSSessionMap = QTSServerInterface::GetServer()->GetRecordSessionMap(); OSMutexLocker locker (sHLSSessionMap->GetMutex()); EasyRecordSession* session = NULL; //首先查找MAP里面是否已经有了对应的流 StrPtrLen streamName(inParams->inStreamName); OSRef* clientSesRef = sHLSSessionMap->Resolve(&streamName); if(clientSesRef != NULL) { session = (EasyRecordSession*)clientSesRef->GetObject(); } else { session = NEW EasyRecordSession(&streamName); OS_Error theErr = sHLSSessionMap->Register(session->GetRef()); Assert(theErr == QTSS_NoErr); //增加一次对RelaySession的无效引用,后面会统一释放 OSRef* debug = sHLSSessionMap->Resolve(&streamName); Assert(debug == session->GetRef()); } //到这里,肯定是有一个EasyRecordSession可用的 session->HLSSessionStart(inParams->inRTSPUrl, inParams->inTimeout); sHLSSessionMap->Release(session->GetRef()); return QTSS_NoErr; }
char* GetHLSUrl(char* inSessionName) { OSRefTable* sHLSSessionMap = QTSServerInterface::GetServer()->GetHLSSessionMap(); OSMutexLocker locker (sHLSSessionMap->GetMutex()); char* hlsURL = NULL; //首先查找Map里面是否已经有了对应的流 StrPtrLen streamName(inSessionName); OSRef* clientSesRef = sHLSSessionMap->Resolve(&streamName); if(NULL == clientSesRef) return NULL; EasyHLSSession* session = (EasyHLSSession*)clientSesRef->GetObject(); hlsURL = session->GetHLSURL(); sHLSSessionMap->Release(session->GetRef()); return hlsURL; }
void* QTSSCallbacks::Easy_GetHLSessions() { OSRefTable* hlsMap = QTSServerInterface::GetServer()->GetHLSSessionMap(); EasyMsgSCHLSessionListACK ack; ack.SetHeaderValue(EASY_TAG_VERSION, "1.0"); ack.SetHeaderValue(EASY_TAG_CSEQ, "1"); char count[16] = { 0 }; sprintf(count,"%d", hlsMap->GetNumRefsInTable()); ack.SetBodyValue(EASY_TAG_SESSION_COUNT, count ); OSRef* theSesRef = NULL; UInt32 uIndex= 0; OSMutexLocker locker(hlsMap->GetMutex()); for (OSRefHashTableIter theIter(hlsMap->GetHashTable()); !theIter.IsDone(); theIter.Next()) { OSRef* theRef = theIter.GetCurrent(); EasyHLSSession* theSession = (EasyHLSSession*)theRef->GetObject(); EasyDarwinHLSession session; session.index = uIndex; session.SessionName = string(theSession->GetSessionID()->Ptr); session.HlsUrl = string(theSession->GetHLSURL()); session.sourceUrl = string(theSession->GetSourceURL()); session.bitrate = theSession->GetLastStatBitrate(); ack.AddSession(session); uIndex++; } string msg = ack.GetMsg(); UInt32 theMsgLen = strlen(msg.c_str()); char* retMsg = new char[theMsgLen+1]; retMsg[theMsgLen] = '\0'; strncpy(retMsg, msg.c_str(), strlen(msg.c_str())); return (void*)retMsg; }
RTPFileSession::ErrorCode RTPFileSession::Initialize(StrPtrLen& inFilePath, Float32 inBufferSeconds) { Assert(fFile == NULL); // Check to see if this file is already open OSMutexLocker locker(sOpenFileMap.GetMutex()); OSRef* theFileRef = sOpenFileMap.Resolve((StrPtrLen*)&inFilePath); if (theFileRef == NULL) { //qtss_printf("Didn't find file in map. Creating new one\n"); fFile = NEW RTPFile(); ErrorCode theErr = fFile->Initialize(inFilePath); if (theErr != errNoError) { delete fFile; fFile = NULL; return theErr; } OS_Error osErr = sOpenFileMap.Register(fFile->GetRef()); Assert(osErr == OS_NoErr); //unless we do this, the refcount won't increment (and we'll delete the session prematurely OSRef* debug = sOpenFileMap.Resolve((StrPtrLen*)&inFilePath); Assert(debug == fFile->GetRef()); } else { //qtss_printf("Found file. Refcounting.\n"); fFile = (RTPFile*)theFileRef->GetObject(); } //Open the file no matter what //fFileSource.Set(inFilePath.Ptr); //Assert(fFileSource.GetLength() > 0); QTSS_Error theErr = QTSS_OpenFileObject(inFilePath.Ptr, 0, &fFileSource); Assert(theErr == QTSS_NoErr); // // Get the file length UInt32 theLen = sizeof(fFileLength); theErr = QTSS_GetValue(fFileSource, qtssFlObjLength, 0, &fFileLength, &theLen); Assert(theErr == QTSS_NoErr); Assert(theLen == sizeof(fFileLength)); // Allocate our data buffer fDataBufferSize = this->PowerOf2Floor((UInt32)(inBufferSeconds * fFile->GetBytesPerSecond())); // Check to see if the size is out of range. If so, adjust it if (fDataBufferSize > kMaxDataBufferSize) fDataBufferSize = kMaxDataBufferSize; if (fDataBufferSize < kBlockSize) fDataBufferSize = kBlockSize; fReadBuffer = fDataBuffer = NEW UInt8[fDataBufferSize]; // Allocate a buffer of TrackInfos fTrackInfo = NEW RTPFileSessionTrackInfo[fFile->GetMaxTrackNumber() + 1]; ::memset(fTrackInfo, 0, fFile->GetMaxTrackNumber() * sizeof(RTPFileSessionTrackInfo)); return errNoError; }
QTSS_Error RedisInit()//only called by RedisConnect after connect redis sucess { //每一次与redis连接后,都应该清除上一次的数据存储,使用覆盖或者直接清除的方式,串行命令使用管线更加高效 char chTemp[128] = { 0 }; do { //1,redis密码认证 sprintf(chTemp, "auth %s", sRedisPassword); sRedisClient->AppendCommand(chTemp); //2,EasyDarwin唯一信息存储(覆盖上一次的存储) sprintf(chTemp, "sadd EasyDarwinName %s:%d", sRTSPWanIP, sRTSPWanPort); sRedisClient->AppendCommand(chTemp); //3,EasyDarwin属性存储,设置多个filed使用hmset,单个使用hset(覆盖上一次的存储) sprintf(chTemp, "hmset %s:%d_Info IP %s PORT %d RTP %d", sRTSPWanIP, sRTSPWanPort, sRTSPWanIP, sRTSPWanPort, QTSServerInterface::GetServer()->GetNumRTPSessions()); sRedisClient->AppendCommand(chTemp); //4,清除推流名称存储 sprintf(chTemp, "del %s:%d_PushName", sRTSPWanIP, sRTSPWanPort); sRedisClient->AppendCommand(chTemp); char* strAllPushName; OSRefTable * reflectorTable = QTSServerInterface::GetServer()->GetReflectorSessionMap(); OSMutex *mutexMap = reflectorTable->GetMutex(); int iPos = 0, iLen = 0; mutexMap->Lock(); strAllPushName = new char[reflectorTable->GetNumRefsInTable() * 128 + 1];//为每一个推流名称分配128字节的内存 memset(strAllPushName, 0, reflectorTable->GetNumRefsInTable() * 128 + 1);//内存初始化为0 for (OSRefHashTableIter theIter(reflectorTable->GetHashTable()); !theIter.IsDone(); theIter.Next()) { OSRef* theRef = theIter.GetCurrent(); ReflectorSession * theSession = (ReflectorSession *)theRef->GetObject(); char * chPushName = theSession->GetSessionName(); if (chPushName) { strAllPushName[iPos++] = ' '; memcpy(strAllPushName + iPos, chPushName, strlen(chPushName)); iPos = iPos + strlen(chPushName); } } mutexMap->Unlock(); char *chNewTemp = new char[strlen(strAllPushName) + 128];//注意,这里不能再使用chTemp,因为长度不确定,可能导致缓冲区溢出 //5,推流名称存储 sprintf(chNewTemp, "sadd %s:%d_PushName%s", sRTSPWanIP, sRTSPWanPort, strAllPushName); sRedisClient->AppendCommand(chTemp); delete[] chNewTemp; delete[] strAllPushName; //6,保活,设置15秒,这之后当前EasyDarwin已经开始提供服务了 sprintf(chTemp, "setex %s:%d_Live 15 1", sRTSPWanIP, sRTSPWanPort); sRedisClient->AppendCommand(chTemp); bool bBreak = false; easyRedisReply* reply = NULL; for (int i = 0; i < 6; i++) { if (EASY_REDIS_OK != sRedisClient->GetReply((void**)&reply)) { bBreak = true; if (reply) EasyFreeReplyObject(reply); break; } EasyFreeReplyObject(reply); } if (bBreak)//说明redisGetReply出现了错误 break; return QTSS_NoErr; } while (0); //走到这说明出现了错误,需要进行重连,重连操作再下一次执行命令时进行,在这仅仅是置标志位 sRedisClient->Free(); sIfConSucess = false; return (QTSS_Error)false; }