我们在基于live555研发音视频流媒体服务器时,在某些特定场景下, 需要主动关闭某一通道对应的所有连接,即要立即close所有观看当前视频通道的所有session;
解决方案
1.主动关闭通道API
Easy_API Easy_I32 Easy_APICALL EasyRtspServer_ResetChannel(EASY_CHANNEL_HANDLE channelHandle);
//在API函数中, 仅置标志位为复位(0x01)
int LiveRtspServer::ResetChannel(RTSP_CHANNEL_HANDLE channelHandle)
{
if (channelHandle < 0) return RTSP_SERVER_ERR_BadArgument;
if (NULL == pLiveChannel) return RTSP_SERVER_ERR_NotInitialized;
LockMutex(&mutexChannel); //Lock
LIVE_CHANNEL_OBJ_T *pStreamChannel = NULL;
LIVE_CHANNEL_OBJ_T *pChannel = pLiveChannel;
while (NULL != pChannel)
{
if (pChannel == channelHandle)
{
pStreamChannel = pChannel;
break;
}
pChannel = pChannel->next;
}
if (NULL == pStreamChannel)
{
UnlockMutex(&mutexChannel); //Unlock
return RTSP_SERVER_ERR_OPERATE;
}
ServerMediaSession* sms = RTSPServer::lookupServerMediaSession(NULL, 0, NULL, pStreamChannel->streamName);
if (NULL != sms)
{
pStreamChannel->resetFlag = 0x01;
}
else
{
memset(&pStreamChannel->mediainfo, 0x00, sizeof(RTSP_MEDIA_INFO_T));
}
UnlockMutex(&mutexChannel); //Unlock
return RTSP_SERVER_ERR_NoErr;
}
2.修改工作线程
BasicTaskScheduler0 *pTaskScheduler0 = NULL;
if (NULL != pThreadTask->pSubEnv && (pTaskScheduler0 = (BasicTaskScheduler0 *)&pThreadTask->pSubEnv->taskScheduler()))
{
if (pThreadTask->pSubEnv->IsResetChannel() > 0x00) //以下代码为主动关闭当前通道及所对应的客户端
{
if (pThreadTask->pSubEnv->IsResetChannel() == 0x01)
{
ServerMediaSession* sms = (ServerMediaSession*)pThreadTask->pSubEnv->GetTmpServerMediaSession();
char streamName[512] = {0};
if (NULL != sms)
{
strcpy(streamName, sms->streamName());
UsageEnvironment *pEnv = &sms->envir();
pThreadTask->pSubEnv->ResetChannel(0x02, NULL);
if (NULL != pEnv)
{
pThis->removeServerMediaSession(pEnv, streamName, False);
}
pThis->closeAllClientSessionsForServerMediaSession(sms);
pThis->CloseAllConnections(streamName);
}
pThreadTask->pSubEnv->ResetChannel(0x00, NULL);
pTaskScheduler0->ClearDelayQueue(); //清空队列
}
Sleep(10);
continue;
}
int ret = pTaskScheduler0->SingleStep(0, pThreadTask->id, pThreadTask->pSubEnv->GetEnvirName());
if (ret < 0)
{
break;
}