Skip to content

Commit

Permalink
Cleaner shutdown for RTP streams
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@2670 e3e1d417-86f3-4887-817a-d78f3d33393f
  • Loading branch information
stan committed Oct 22, 2008
1 parent 012ac78 commit 9ecb501
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/zm_rtp_ctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ int RtpCtrlThread::run()
unsigned char buffer[BUFSIZ];
while ( !mStop && select.wait() >= 0 )
{
if ( mStop )
break;
Select::CommsList readable = select.getReadable();
if ( readable.size() == 0 )
{
Expand Down
2 changes: 2 additions & 0 deletions src/zm_rtp_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ int RtpDataThread::run()
unsigned char buffer[BUFSIZ];
while ( !mStop && select.wait() >= 0 )
{
if ( mStop )
break;
Select::CommsList readable = select.getReadable();
if ( readable.size() == 0 )
{
Expand Down
41 changes: 32 additions & 9 deletions src/zm_rtsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "zm_db.h"

#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>

Expand All @@ -40,11 +41,11 @@ bool RtspThread::sendCommand( std::string message )
if ( !mAuth.empty() )
message += stringtf( "Authorization: Basic %s\r\n", mAuth.c_str() );
message += stringtf( "CSeq: %d\r\n\r\n", ++mSeq );
Debug( 4, "Sending RTSP message: %s", message.c_str() );
Debug( 2, "Sending RTSP message: %s", message.c_str() );
if ( mMethod == RTP_RTSP_HTTP )
{
message = base64Encode( message );
Debug( 4, "Sending encoded RTSP message: %s", message.c_str() );
Debug( 2, "Sending encoded RTSP message: %s", message.c_str() );
if ( mRtspSocket2.send( message.c_str(), message.size() ) != (int)message.length() )
{
Error( "Unable to send message '%s': %s", message.c_str(), strerror(errno) );
Expand All @@ -66,13 +67,22 @@ bool RtspThread::recvResponse( std::string &response )
{
if ( mRtspSocket.recv( response ) < 0 )
Error( "Recv failed; %s", strerror(errno) );
Debug( 4, "Received RTSP response: %s (%d bytes)", response.c_str(), response.size() );
Debug( 2, "Received RTSP response: %s (%d bytes)", response.c_str(), response.size() );
float respVer = 0;
int respCode = -1;
char respText[BUFSIZ];
if ( sscanf( response.c_str(), "RTSP/%f %3d %[^\r\n]\r\n", &respVer, &respCode, respText ) != 3 )
{
Error( "Response parse failure in '%s'", response.c_str() );
if ( isalnum(response[0]) )
{
Error( "Response parse failure in '%s'", response.c_str() );
}
else
{
Error( "Response parse failure, %d bytes follow", response.size() );
if ( response.size() )
Hexdump( ZM_DBG_ERR, response.data(), min(response.size(),16) );
}
return( false );
}
if ( respCode != 200 )
Expand Down Expand Up @@ -209,7 +219,7 @@ int RtspThread::run()
if ( !mAuth.empty() )
message += stringtf( "Authorization: Basic %s\r\n", mAuth.c_str() );
message += "\r\n";
Debug( 4, "Sending HTTP message: %s", message.c_str() );
Debug( 2, "Sending HTTP message: %s", message.c_str() );
if ( mRtspSocket.send( message.c_str(), message.size() ) != (int)message.length() )
{
Error( "Unable to send message '%s': %s", message.c_str(), strerror(errno) );
Expand All @@ -221,13 +231,22 @@ int RtspThread::run()
return( -1 );
}

Debug( 4, "Received HTTP response: %s (%d bytes)", response.c_str(), response.size() );
Debug( 2, "Received HTTP response: %s (%d bytes)", response.c_str(), response.size() );
float respVer = 0;
int respCode = -1;
char respText[256];
if ( sscanf( response.c_str(), "HTTP/%f %3d %[^\r\n]\r\n", &respVer, &respCode, respText ) != 3 )
{
Error( "Response parse failure in '%s'", response.c_str() );
if ( isalnum(response[0]) )
{
Error( "Response parse failure in '%s'", response.c_str() );
}
else
{
Error( "Response parse failure, %d bytes follow", response.size() );
if ( response.size() )
Hexdump( ZM_DBG_ERR, response.data(), min(response.size(),16) );
}
return( -1 );
}
if ( respCode != 200 )
Expand All @@ -243,7 +262,7 @@ int RtspThread::run()
message += "Content-Length: 32767\r\n";
message += "Content-Type: application/x-rtsp-tunnelled\r\n";
message += "\r\n";
Debug( 4, "Sending HTTP message: %s", message.c_str() );
Debug( 2, "Sending HTTP message: %s", message.c_str() );
if ( mRtspSocket2.send( message.c_str(), message.size() ) != (int)message.length() )
{
Error( "Unable to send message '%s': %s", message.c_str(), strerror(errno) );
Expand Down Expand Up @@ -451,12 +470,13 @@ int RtspThread::run()
{
usleep( 100000 );
}

#if 0
message = "PAUSE "+mUrl+" RTSP/1.0\r\nSession: "+session+"\r\n";
if ( !sendCommand( message ) )
return( -1 );
if ( !recvResponse( response ) )
return( -1 );
#endif

message = "TEARDOWN "+mUrl+" RTSP/1.0\r\nSession: "+session+"\r\n";
if ( !sendCommand( message ) )
Expand All @@ -467,6 +487,9 @@ int RtspThread::run()
rtpDataThread.stop();
rtpCtrlThread.stop();

rtpDataThread.kill( SIGTERM );
rtpCtrlThread.kill( SIGTERM );

rtpDataThread.join();
rtpCtrlThread.join();

Expand Down
6 changes: 6 additions & 0 deletions src/zm_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "zm_utils.h"

#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/time.h>

Expand Down Expand Up @@ -325,5 +326,10 @@ void Thread::join()
Debug( 1, "Joined thread %d", mPid );
}

void Thread::kill( int signal )
{
pthread_kill( mThread, signal );
}

// Some explicit template instantiations
#include "zm_threaddata.cpp"
1 change: 1 addition & 0 deletions src/zm_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class Thread

void start();
void join();
void kill( int signal );
bool isThread()
{
return( mPid > -1 && pthread_equal( pthread_self(), mThread ) );
Expand Down
10 changes: 10 additions & 0 deletions src/zm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ StringVector split( const std::string &string, const std::string chars );

const std::string base64Encode( const std::string &inString );

inline int max( int a, int b )
{
return( a>=b?a:b );
}

inline int min( int a, int b )
{
return( a<=b?a:b );
}

#endif // ZM_UTILS_H

0 comments on commit 9ecb501

Please sign in to comment.