Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send keepalive messages if the rtsp server supports this feature #560

Merged
merged 2 commits into from
Nov 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/zm_rtp_ctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ int RtpCtrlThread::generateRr( const unsigned char *packet, ssize_t packetLen )

mRtpSource.updateRtcpStats();

Debug( 5, "Ssrc = %d", mRtspThread.getSsrc() );
Debug( 5, "Ssrc = %d", mRtspThread.getSsrc()+1 );
Debug( 5, "Ssrc_1 = %d", mRtpSource.getSsrc() );
Debug( 5, "Last Seq = %d", mRtpSource.getMaxSeq() );
Debug( 5, "Jitter = %d", mRtpSource.getJitter() );
Debug( 5, "Last SR = %d", mRtpSource.getLastSrTimestamp() );

rtcpPacket->body.rr.ssrcN = htonl(mRtspThread.getSsrc());
rtcpPacket->body.rr.ssrcN = htonl(mRtspThread.getSsrc()+1);
rtcpPacket->body.rr.rr[0].ssrcN = htonl(mRtpSource.getSsrc());
rtcpPacket->body.rr.rr[0].lost = mRtpSource.getLostPackets();
rtcpPacket->body.rr.rr[0].fraction = mRtpSource.getLostFraction();
Expand All @@ -208,7 +208,7 @@ int RtpCtrlThread::generateSdes( const unsigned char *packet, ssize_t packetLen
rtcpPacket->header.count = 1;
rtcpPacket->header.lenN = htons(wordLen-1);

rtcpPacket->body.sdes.srcN = htonl(mRtpSource.getSsrc());
rtcpPacket->body.sdes.srcN = htonl(mRtpSource.getSsrc()+1);
rtcpPacket->body.sdes.item[0].type = RTCP_SDES_CNAME;
rtcpPacket->body.sdes.item[0].len = cname.size();
memcpy( rtcpPacket->body.sdes.item[0].data, cname.data(), cname.size() );
Expand Down
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