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