之前发布过一个基于testRtspClient.cpp 改装的 rtsp client模块 http://blog.csdn.net/mtour/article/details/40073371
该模块支持多路rtsp流,基于这个模块做了一个录像服务
录像服务需要稳定性比较好,在测试的过程中发现 运行几个小时 1天 后 程序出现崩溃。 经过仔细排查 做了如下优化解决
更改 BasicUsageEnvironment 库的 doeventloop函数
void BasicTaskScheduler0::doEventLoop(char* watchVariable) {
// Repeatedly loop, handling readble sockets and timed events:
/*while (1) {
if (watchVariable != NULL && *watchVariable != 0) break;
SingleStep();
}*/
SingleStep();
}
在库外层进行循环操作如下
static DWORD WINAPI StartRtspEventLoop(LPVOID pUser)
{
theApp.g_scheduler=BasicTaskScheduler::createNew();
theApp.g_env = BasicUsageEnvironment::createNew(*theApp.g_scheduler);
while (theApp.g_WatchVariable)
{
//theApp.g_env->taskScheduler().doEventLoop(&eventLoopWatchVariable);
WaitForSingleObject(theApp.g_eventLoopMutex,INFINITE);
theApp.g_env->taskScheduler().doEventLoop();
ReleaseMutex(theApp.g_eventLoopMutex);
}
return 0;
}
注意加互斥锁, 在 rtspclient对象的删除也加互斥锁
void CloseClientFun(void *data)
{
if (!data)
{
return;
}
RTSPClient* pClient=(RTSPClient*)data;
shutdownStream(pClient,0);
}
int CStreamItem::Close()
{
if (m_pRtspClient)
{
m_pRtspClient->m_lpStreamCallBack=NULL;
m_pRtspClient->m_pUserData=NULL;
WaitForSingleObject(theApp.g_eventLoopMutex,INFINITE);
theApp.g_scheduler->scheduleDelayedTask(1000*1000, CloseClientFun, m_pRtspClient);
ReleaseMutex(theApp.g_eventLoopMutex);
m_pRtspClient=NULL;
}
return 0;
}
客户端打开的代码 增加互斥锁
WaitForSingleObject(theApp.g_eventLoopMutex,INFINITE);
m_pRtspClient->sendDescribeCommand(continueAfterDESCRIBE);
ReleaseMutex(theApp.g_eventLoopMutex);