libRTSPServer 实现主动关闭指定通道

##libRTSPServer 实现主动关闭指定通道

#####主动关闭通道API:

int RTSPSERVER_API	libRTSPSvr_ResetChannel(RTSP_SVR_HANDLE handle, RTSP_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;
	}

工作线程中做如下修改:

	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;
		}

因本代码中增加了多线程及其它功能,所以参数上会与live555官方代码有些许不同;

####下载地址:
https://pan.baidu.com/s/129_dxIrui3YB69L3YAKqVw

交流QQ: 760983740

你可能感兴趣的:(live555)