Skip to content

Commit

Permalink
Merge pull request #560 from manupap1/rtsp_fix
Browse files Browse the repository at this point in the history
Send keepalive messages if the rtsp server supports this feature
  • Loading branch information
connortechnology committed Nov 8, 2014
2 parents b4a8ba8 + ea8b93b commit 1d6dffd
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/zm_rtsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,23 @@ int RtspThread::run()
std::string localHost = "";
int localPorts[2] = { 0, 0 };

//message = "OPTIONS * RTSP/1.0\r\n";
//sendCommand( message );
//recvResponse( response );
// Request supported RTSP commands by the server
message = "OPTIONS * RTSP/1.0\r\n";
if ( !sendCommand( message ) )
return( -1 );
if ( !recvResponse( response ) )
return( -1 );

char publicLine[256] = "";
StringVector lines = split( response, "\r\n" );
for ( size_t i = 0; i < lines.size(); i++ )
sscanf( lines[i].c_str(), "Public: %[^\r\n]\r\n", publicLine );

// Check if the server supports the GET_PARAMETER command
// If yes, it is likely that the server will request this command as a keepalive message
bool sendKeepalive = false;
if ( publicLine[0] && strstr(publicLine, "GET_PARAMETER") )
sendKeepalive = true;

message = "DESCRIBE "+mUrl+" RTSP/1.0\r\n";
bool res;
Expand Down Expand Up @@ -466,7 +480,7 @@ int RtspThread::run()
if ( !recvResponse( response ) )
return( -1 );

StringVector lines = split( response, "\r\n" );
lines = split( response, "\r\n" );
char *session = 0;
int timeout = 0;
char transport[256] = "";
Expand Down Expand Up @@ -579,6 +593,9 @@ int RtspThread::run()
Debug( 2, "RTSP Seq is %d", seq );
Debug( 2, "RTSP Rtptime is %ld", rtpTime );

time_t lastKeepalive = time(NULL);
message = "GET_PARAMETER "+mUrl+" RTSP/1.0\r\nSession: "+session+"\r\n";

switch( mMethod )
{
case RTP_UNICAST :
Expand All @@ -593,6 +610,13 @@ int RtspThread::run()

while( !mStop )
{
// Send a keepalive message if the server supports this feature and we are close to the timeout expiration
if ( sendKeepalive && (timeout > 0) && ((time(NULL)-lastKeepalive) > (timeout-5)) )
{
if ( !sendCommand( message ) )
return( -1 );
lastKeepalive = time(NULL);
}
usleep( 100000 );
}
#if 0
Expand Down Expand Up @@ -638,7 +662,6 @@ int RtspThread::run()
select.addReader( &mRtspSocket );

Buffer buffer( ZM_NETWORK_BUFSIZ );
time_t lastKeepalive = time(NULL);
std::string keepaliveMessage = "OPTIONS * RTSP/1.0\r\n";
std::string keepaliveResponse = "RTSP/1.0 200 OK\r\n";
while ( !mStop && select.wait() >= 0 )
Expand Down Expand Up @@ -728,7 +751,9 @@ int RtspThread::run()
}
}
}
if ( (timeout > 0) && ((time(NULL)-lastKeepalive) > (timeout-5)) )
// Send a keepalive message if the server supports this feature and we are close to the timeout expiration
// FIXME: Is this really necessary when using tcp ?
if ( sendKeepalive && (timeout > 0) && ((time(NULL)-lastKeepalive) > (timeout-5)) )
{
if ( !sendCommand( message ) )
return( -1 );
Expand Down Expand Up @@ -766,6 +791,13 @@ int RtspThread::run()

while( !mStop )
{
// Send a keepalive message if the server supports this feature and we are close to the timeout expiration
if ( sendKeepalive && (timeout > 0) && ((time(NULL)-lastKeepalive) > (timeout-5)) )
{
if ( !sendCommand( message ) )
return( -1 );
lastKeepalive = time(NULL);
}
usleep( 100000 );
}
#if 0
Expand Down

0 comments on commit 1d6dffd

Please sign in to comment.