code style, merge Debug lines and consult zm_terminate in the main while loop

pull/2942/head
Isaac Connor 2020-05-07 13:41:09 -04:00
parent 7489d66557
commit 09740d72a4
1 changed files with 42 additions and 59 deletions

View File

@ -27,98 +27,81 @@
#include <arpa/inet.h> #include <arpa/inet.h>
RtpDataThread::RtpDataThread( RtspThread &rtspThread, RtpSource &rtpSource ) : mRtspThread( rtspThread ), mRtpSource( rtpSource ), mStop( false ) RtpDataThread::RtpDataThread(RtspThread &rtspThread, RtpSource &rtpSource) :
mRtspThread(rtspThread), mRtpSource(rtpSource), mStop(false)
{ {
} }
bool RtpDataThread::recvPacket( const unsigned char *packet, size_t packetLen ) bool RtpDataThread::recvPacket(const unsigned char *packet, size_t packetLen) {
{
const RtpDataHeader *rtpHeader; const RtpDataHeader *rtpHeader;
rtpHeader = (RtpDataHeader *)packet; rtpHeader = (RtpDataHeader *)packet;
//printf( "D: " ); Debug(5, "Ver: %d P: %d Pt: %d Mk: %d Seq: %d T/S: %x SSRC: %x",
//for ( int i = 0; i < 32; i++ ) rtpHeader->version,
//printf( "%02x ", (unsigned char)packet[i] ); rtpHeader->p,
//printf( "\n" ); rtpHeader->pt,
rtpHeader->m,
Debug( 5, "Ver: %d", rtpHeader->version ); ntohs(rtpHeader->seqN),
Debug( 5, "P: %d", rtpHeader->p ); ntohl(rtpHeader->timestampN),
Debug( 5, "Pt: %d", rtpHeader->pt ); ntohl(rtpHeader->ssrcN));
Debug( 5, "Mk: %d", rtpHeader->m );
Debug( 5, "Seq: %d", ntohs(rtpHeader->seqN) );
Debug( 5, "T/S: %x", ntohl(rtpHeader->timestampN) );
Debug( 5, "SSRC: %x", ntohl(rtpHeader->ssrcN) );
//unsigned short seq = ntohs(rtpHeader->seqN); //unsigned short seq = ntohs(rtpHeader->seqN);
unsigned long ssrc = ntohl(rtpHeader->ssrcN); unsigned long ssrc = ntohl(rtpHeader->ssrcN);
if ( mRtpSource.getSsrc() && (ssrc != mRtpSource.getSsrc()) ) if ( mRtpSource.getSsrc() && (ssrc != mRtpSource.getSsrc()) ) {
{ Warning("Discarding packet for unrecognised ssrc %lx", ssrc);
Warning( "Discarding packet for unrecognised ssrc %lx", ssrc ); return false;
return( false );
} }
return( mRtpSource.handlePacket( packet, packetLen ) ); return mRtpSource.handlePacket(packet, packetLen);
} }
int RtpDataThread::run() int RtpDataThread::run() {
{ Debug(2, "Starting data thread %d on port %d",
Debug( 2, "Starting data thread %d on port %d", mRtpSource.getSsrc(), mRtpSource.getLocalDataPort() ); mRtpSource.getSsrc(), mRtpSource.getLocalDataPort());
SockAddrInet localAddr; SockAddrInet localAddr;
UdpInetServer rtpDataSocket; UdpInetServer rtpDataSocket;
if ( mRtpSource.getLocalHost() != "" ) { if ( mRtpSource.getLocalHost() != "" ) {
if ( !rtpDataSocket.bind( mRtpSource.getLocalHost().c_str(), mRtpSource.getLocalDataPort() ) ) if ( !rtpDataSocket.bind(mRtpSource.getLocalHost().c_str(), mRtpSource.getLocalDataPort()) )
Fatal( "Failed to bind RTP server" ); Fatal("Failed to bind RTP server");
Debug( 3, "Bound to %s:%d", mRtpSource.getLocalHost().c_str(), mRtpSource.getLocalDataPort() ); } else {
} if ( !rtpDataSocket.bind(
else mRtspThread.getAddressFamily() == AF_INET6 ? "::" : "0.0.0.0",
{ mRtpSource.getLocalDataPort() ) )
if ( !rtpDataSocket.bind( mRtspThread.getAddressFamily() == AF_INET6 ? "::" : "0.0.0.0", mRtpSource.getLocalDataPort() ) ) Fatal("Failed to bind RTP server");
Fatal( "Failed to bind RTP server" );
Debug( 3, "Bound to %s:%d", mRtpSource.getLocalHost().c_str(), mRtpSource.getLocalDataPort() );
} }
Debug(3, "Bound to %s:%d", mRtpSource.getLocalHost().c_str(), mRtpSource.getLocalDataPort());
Select select( 3 ); Select select(3);
select.addReader( &rtpDataSocket ); select.addReader(&rtpDataSocket);
unsigned char buffer[ZM_NETWORK_BUFSIZ]; unsigned char buffer[ZM_NETWORK_BUFSIZ];
while ( !mStop && select.wait() >= 0 ) while ( !zm_terminate && !mStop && (select.wait() >= 0) ) {
{
if ( mStop )
break;
Select::CommsList readable = select.getReadable(); Select::CommsList readable = select.getReadable();
if ( readable.size() == 0 ) if ( readable.size() == 0 ) {
{ Error("RTP timed out");
Error( "RTP timed out" );
mStop = true; mStop = true;
break; break;
} }
for ( Select::CommsList::iterator iter = readable.begin(); iter != readable.end(); ++iter ) for ( Select::CommsList::iterator iter = readable.begin(); iter != readable.end(); ++iter ) {
{ if ( UdpInetServer *socket = dynamic_cast<UdpInetServer *>(*iter) ) {
if ( UdpInetServer *socket = dynamic_cast<UdpInetServer *>(*iter) ) int nBytes = socket->recv(buffer, sizeof(buffer));
{ Debug(4, "Got %d bytes on sd %d", nBytes, socket->getReadDesc());
int nBytes = socket->recv( buffer, sizeof(buffer) ); if ( nBytes ) {
Debug( 4, "Got %d bytes on sd %d", nBytes, socket->getReadDesc() ); recvPacket(buffer, nBytes);
if ( nBytes ) } else {
{
recvPacket( buffer, nBytes );
}
else
{
mStop = true; mStop = true;
break; break;
} }
} else {
Panic("Barfed");
} }
else } // end foreach commsList
{
Panic( "Barfed" );
}
}
} }
rtpDataSocket.close(); rtpDataSocket.close();
mRtspThread.stop(); mRtspThread.stop();
return( 0 ); return 0;
} }
#endif // HAVE_LIBAVFORMAT #endif // HAVE_LIBAVFORMAT