live555自带的客户端例程testRTSPClient.c中不具备重连服务器的功能,通过对testRTSPClient.c改进可实现客户端以下功能:
1、连接器服务器失败后,可以定时重连服务器;
2、连接服务器成功后,网络异常,客户端可以重新连接服务器。
//main
while(1)
{
eventLoopWatchVariable = 0;
// There are argc-1 URLs: argv[1] through argv[argc-1]. Open and start streaming each one:
for (int i = 1; i <= argc-1; ++i) {
openURL(*env, argv[0], argv[i]);
}
// All subsequent activity takes place within the event loop:
env->taskScheduler().doEventLoop(&eventLoopWatchVariable);
// This function call does not return, unless, at some point in time, "eventLoopWatchVariable" gets set to something non-zero.
Sleep(2000);
}
//void shutdownStream(RTSPClient* rtspClient, int exitCode)
if (--rtspClientCount == 0) {
// The final stream has ended, so exit the application now.
// (Of course, if you're embedding this code into your own application, you might want to comment this out,
// and replace it with "eventLoopWatchVariable = 1;", so that we leave the LIVE555 event loop, and continue running "main()".)
//exit(exitCode);
eventLoopWatchVariable = 1;
}
1、main函数添加一个死循环。
2、shutdownStream注释程序退出exit(exitCode),把eventLoopWatchVariable 设为1,因为当eventLoopWatchVariable 不为0时,env->taskScheduler().doEventLoop(&eventLoopWatchVariable)会退出,这样可以跳到main函数while的开始,重新请求服务器连接。
//void continueAfterPLAY(RTSPClient* rtspClient, int resultCode, char* resultString)
scs.duration=30;
// Set a timer to be handled at the end of the stream's expected duration (if the stream does not already signal its end
// using a RTCP "BYE"). This is optional. If, instead, you want to keep the stream active - e.g., so you can later
// 'seek' back within it and do another RTSP "PLAY" - then you can omit this code.
// (Alternatively, if you don't want to receive the entire stream, you could set this timer for some shorter value.)
if (scs.duration > 0) {
unsigned const delaySlop = 2; // number of seconds extra to delay, after the stream's expected duration. (This is optional.)
scs.duration += delaySlop;
unsigned uSecsToDelay = (unsigned)(scs.duration*1000000);
scs.streamTimerTask = env.taskScheduler().scheduleDelayedTask(uSecsToDelay, (TaskFunc*)streamTimerHandler, rtspClient);
}
1、continueAfterPLAY函数里设置主循环超时时间,并注册超时处理函数:shutdownStream