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

This commit is contained in:
Isaac Connor 2020-05-07 13:41:29 -04:00
parent 09740d72a4
commit 87d0eb88b7
1 changed files with 91 additions and 78 deletions

View File

@ -37,25 +37,25 @@ RtpSource::RtpSource(
uint32_t rtpClock,
uint32_t rtpTime,
_AVCODECID codecId ) :
mId( id ),
mSsrc( ssrc ),
mLocalHost( localHost ),
mRemoteHost( remoteHost ),
mRtpClock( rtpClock ),
mCodecId( codecId ),
mFrame( 65536 ),
mFrameCount( 0 ),
mFrameGood( true ),
mFrameReady( false ),
mFrameProcessed( false )
mId(id),
mSsrc(ssrc),
mLocalHost(localHost),
mRemoteHost(remoteHost),
mRtpClock(rtpClock),
mCodecId(codecId),
mFrame(65536),
mFrameCount(0),
mFrameGood(true),
mFrameReady(false),
mFrameProcessed(false)
{
char hostname[256] = "";
gethostname( hostname, sizeof(hostname) );
gethostname(hostname, sizeof(hostname));
mCname = stringtf( "zm-%d@%s", mId, hostname );
Debug( 3, "RTP CName = %s", mCname.c_str() );
mCname = stringtf("zm-%d@%s", mId, hostname);
Debug(3, "RTP CName = %s", mCname.c_str());
init( seq );
init(seq);
mMaxSeq = seq - 1;
mProbation = MIN_SEQUENTIAL;
@ -79,7 +79,7 @@ RtpSource::RtpSource(
Warning("The device is using a codec (%d) that may not be supported. Do not be surprised if things don't work.", mCodecId);
}
void RtpSource::init( uint16_t seq ) {
void RtpSource::init(uint16_t seq) {
Debug(3, "Initialising sequence");
mBaseSeq = seq;
mMaxSeq = seq;
@ -93,36 +93,36 @@ void RtpSource::init( uint16_t seq ) {
mTransit = 0;
}
bool RtpSource::updateSeq( uint16_t seq ) {
bool RtpSource::updateSeq(uint16_t seq) {
uint16_t uDelta = seq - mMaxSeq;
// Source is not valid until MIN_SEQUENTIAL packets with
// sequential sequence numbers have been received.
Debug( 5, "Seq: %d", seq );
Debug(5, "Seq: %d", seq);
if ( mProbation) {
// packet is in sequence
if ( seq == mMaxSeq + 1) {
Debug( 3, "Sequence in probation %d, in sequence", mProbation );
if ( seq == mMaxSeq + 1 ) {
Debug(3, "Sequence in probation %d, in sequence", mProbation);
mProbation--;
mMaxSeq = seq;
if ( mProbation == 0 ) {
init( seq );
init(seq);
mReceivedPackets++;
return( true );
return true;
}
} else {
Warning( "Sequence in probation %d, out of sequence", mProbation );
Warning("Sequence in probation %d, out of sequence", mProbation);
mProbation = MIN_SEQUENTIAL - 1;
mMaxSeq = seq;
return( false );
return false;
}
return( true );
return true;
} else if ( uDelta < MAX_DROPOUT ) {
if ( uDelta == 1 ) {
Debug( 4, "Packet in sequence, gap %d", uDelta );
Debug(4, "Packet in sequence, gap %d", uDelta);
} else {
Warning( "Packet in sequence, gap %d", uDelta );
Warning("Packet in sequence, gap %d", uDelta);
}
// in order, with permissible gap
@ -132,22 +132,22 @@ bool RtpSource::updateSeq( uint16_t seq ) {
}
mMaxSeq = seq;
} else if ( uDelta <= RTP_SEQ_MOD - MAX_MISORDER ) {
Warning( "Packet out of sequence, gap %d", uDelta );
Warning("Packet out of sequence, gap %d", uDelta);
// the sequence number made a very large jump
if ( seq == mBadSeq ) {
Debug( 3, "Restarting sequence" );
Debug(3, "Restarting sequence");
// Two sequential packets -- assume that the other side
// restarted without telling us so just re-sync
// (i.e., pretend this was the first packet).
init( seq );
init(seq);
} else {
mBadSeq = (seq + 1) & (RTP_SEQ_MOD-1);
return( false );
return false;
}
} else {
Warning( "Packet duplicate or reordered, gap %d", uDelta );
Warning("Packet duplicate or reordered, gap %d", uDelta);
// duplicate or reordered packet
return( false );
return false;
}
mReceivedPackets++;
return( uDelta==1?true:false );
@ -155,17 +155,22 @@ bool RtpSource::updateSeq( uint16_t seq ) {
void RtpSource::updateJitter( const RtpDataHeader *header ) {
if ( mRtpFactor > 0 ) {
Debug( 5, "Delta rtp = %.6f", tvDiffSec( mBaseTimeReal ) );
uint32_t localTimeRtp = mBaseTimeRtp + uint32_t( tvDiffSec( mBaseTimeReal ) * mRtpFactor );
Debug( 5, "Local RTP time = %x", localTimeRtp );
Debug( 5, "Packet RTP time = %x", ntohl(header->timestampN) );
uint32_t localTimeRtp = mBaseTimeRtp + uint32_t(tvDiffSec(mBaseTimeReal) * mRtpFactor);
uint32_t packetTransit = localTimeRtp - ntohl(header->timestampN);
Debug( 5, "Packet transit RTP time = %x", packetTransit );
Debug(5, "Delta rtp = %.6f\n"
"Local RTP time = %x",
"Packet RTP time = %x",
"Packet transit RTP time = %x",
tvDiffSec(mBaseTimeReal),
localTimeRtp,
ntohl(header->timestampN),
packetTransit);
if ( mTransit > 0 ) {
// Jitter
int d = packetTransit - mTransit;
Debug( 5, "Jitter D = %d", d );
Debug(5, "Jitter D = %d", d);
if ( d < 0 )
d = -d;
//mJitter += (1./16.) * ((double)d - mJitter);
@ -175,32 +180,33 @@ void RtpSource::updateJitter( const RtpDataHeader *header ) {
} else {
mJitter = 0;
}
Debug( 5, "RTP Jitter: %d", mJitter );
Debug(5, "RTP Jitter: %d", mJitter);
}
void RtpSource::updateRtcpData( uint32_t ntpTimeSecs, uint32_t ntpTimeFrac, uint32_t rtpTime ) {
struct timeval ntpTime = tvMake( ntpTimeSecs, suseconds_t((USEC_PER_SEC*(ntpTimeFrac>>16))/(1<<16)) );
void RtpSource::updateRtcpData(
uint32_t ntpTimeSecs,
uint32_t ntpTimeFrac,
uint32_t rtpTime) {
struct timeval ntpTime = tvMake(ntpTimeSecs, suseconds_t((USEC_PER_SEC*(ntpTimeFrac>>16))/(1<<16)));
Debug( 5, "ntpTime: %ld.%06ld, rtpTime: %x", ntpTime.tv_sec, ntpTime.tv_usec, rtpTime );
Debug(5, "ntpTime: %ld.%06ld, rtpTime: %x", ntpTime.tv_sec, ntpTime.tv_usec, rtpTime);
if ( mBaseTimeNtp.tv_sec == 0 ) {
mBaseTimeReal = tvNow();
mBaseTimeNtp = ntpTime;
mBaseTimeRtp = rtpTime;
} else if ( !mRtpClock ) {
Debug( 5, "lastSrNtpTime: %ld.%06ld, rtpTime: %x", mLastSrTimeNtp.tv_sec, mLastSrTimeNtp.tv_usec, rtpTime );
Debug( 5, "ntpTime: %ld.%06ld, rtpTime: %x", ntpTime.tv_sec, ntpTime.tv_usec, rtpTime );
Debug(5, "lastSrNtpTime: %ld.%06ld, rtpTime: %x"
"ntpTime: %ld.%06ld, rtpTime: %x",
mLastSrTimeNtp.tv_sec, mLastSrTimeNtp.tv_usec, rtpTime,
ntpTime.tv_sec, ntpTime.tv_usec, rtpTime);
double diffNtpTime = tvDiffSec( mBaseTimeNtp, ntpTime );
uint32_t diffRtpTime = rtpTime - mBaseTimeRtp;
//Debug( 5, "Real-diff: %.6f", diffRealTime );
Debug( 5, "NTP-diff: %.6f", diffNtpTime );
Debug( 5, "RTP-diff: %d", diffRtpTime );
mRtpFactor = (uint32_t)(diffRtpTime / diffNtpTime);
Debug( 5, "RTPfactor: %d", mRtpFactor );
Debug( 5, "NTP-diff: %.6f RTP-diff: %d RTPfactor: %d",
diffNtpTime, diffRtpTime, mRtpFactor);
}
mLastSrTimeNtpSecs = ntpTimeSecs;
mLastSrTimeNtpFrac = ntpTimeFrac;
@ -211,31 +217,37 @@ void RtpSource::updateRtcpData( uint32_t ntpTimeSecs, uint32_t ntpTimeFrac, uint
void RtpSource::updateRtcpStats() {
uint32_t extendedMax = mCycles + mMaxSeq;
mExpectedPackets = extendedMax - mBaseSeq + 1;
Debug( 5, "Expected packets = %d", mExpectedPackets );
// The number of packets lost is defined to be the number of packets
// expected less the number of packets actually received:
mLostPackets = mExpectedPackets - mReceivedPackets;
Debug( 5, "Lost packets = %d", mLostPackets );
uint32_t expectedInterval = mExpectedPackets - mExpectedPrior;
Debug( 5, "Expected interval = %d", expectedInterval );
mExpectedPrior = mExpectedPackets;
uint32_t receivedInterval = mReceivedPackets - mReceivedPrior;
Debug( 5, "Received interval = %d", receivedInterval );
mReceivedPrior = mReceivedPackets;
uint32_t lostInterval = expectedInterval - receivedInterval;
Debug( 5, "Lost interval = %d", lostInterval );
if ( expectedInterval == 0 || lostInterval <= 0 )
mLostFraction = 0;
else
mLostFraction = (lostInterval << 8) / expectedInterval;
Debug( 5, "Lost fraction = %d", mLostFraction );
Debug(5,
"Expected packets = %d\n",
"Lost packets = %d\n",
"Expected interval = %d\n",
"Received interval = %d\n",
"Lost interval = %d\n",
"Lost fraction = %d\n",
mExpectedPackets,
mLostPackets,
expectedInterval,
receivedInterval,
lostInterval,
mLostFraction);
}
bool RtpSource::handlePacket( const unsigned char *packet, size_t packetLen ) {
bool RtpSource::handlePacket(const unsigned char *packet, size_t packetLen) {
const RtpDataHeader *rtpHeader;
rtpHeader = (RtpDataHeader *)packet;
int rtpHeaderSize = 12 + rtpHeader->cc * 4;
@ -248,15 +260,15 @@ bool RtpSource::handlePacket( const unsigned char *packet, size_t packetLen ) {
// that there is no marker bit by changing the number of bits in the payload type field.
bool thisM = rtpHeader->m || h264FragmentEnd;
if ( updateSeq( ntohs(rtpHeader->seqN) ) ) {
Hexdump( 4, packet+rtpHeaderSize, 16 );
if ( updateSeq(ntohs(rtpHeader->seqN)) ) {
Hexdump(4, packet+rtpHeaderSize, 16);
if ( mFrameGood ) {
int extraHeader = 0;
if ( mCodecId == AV_CODEC_ID_H264 ) {
int nalType = (packet[rtpHeaderSize] & 0x1f);
Debug( 3, "Have H264 frame: nal type is %d", nalType );
Debug(3, "Have H264 frame: nal type is %d", nalType);
switch (nalType) {
case 24: // STAP-A
@ -281,45 +293,46 @@ bool RtpSource::handlePacket( const unsigned char *packet, size_t packetLen ) {
extraHeader = 2;
break;
default:
Debug(3, "Unhandled nalType %d", nalType );
Debug(3, "Unhandled nalType %d", nalType);
}
// Append NAL frame start code
if ( !mFrame.size() )
mFrame.append( "\x0\x0\x1", 3 );
mFrame.append("\x0\x0\x1", 3);
} // end if H264
mFrame.append( packet+rtpHeaderSize+extraHeader, packetLen-rtpHeaderSize-extraHeader );
mFrame.append(packet+rtpHeaderSize+extraHeader,
packetLen-rtpHeaderSize-extraHeader);
} else {
Debug( 3, "NOT H264 frame: type is %d", mCodecId );
Debug(3, "NOT H264 frame: type is %d", mCodecId);
}
Hexdump( 4, mFrame.head(), 16 );
Hexdump(4, mFrame.head(), 16);
if ( thisM ) {
if ( mFrameGood ) {
Debug( 3, "Got new frame %d, %d bytes", mFrameCount, mFrame.size() );
Debug(3, "Got new frame %d, %d bytes", mFrameCount, mFrame.size());
mFrameProcessed.setValueImmediate( false );
mFrameReady.updateValueSignal( true );
mFrameProcessed.setValueImmediate(false);
mFrameReady.updateValueSignal(true);
if ( !mFrameProcessed.getValueImmediate() ) {
// What is the point of this for loop? Is it just me, or will it call getUpdatedValue once or twice? Could it not be better written as
// if ( ! mFrameProcessed.getUpdatedValue( 1 ) && mFrameProcessed.getUpdatedValue( 1 ) ) return false;
for ( int count = 0; !mFrameProcessed.getUpdatedValue( 1 ); count++ )
if( count > 1 )
return( false );
if ( count > 1 )
return false;
}
mFrameCount++;
} else {
Warning( "Discarding incomplete frame %d, %d bytes", mFrameCount, mFrame.size() );
Warning("Discarding incomplete frame %d, %d bytes", mFrameCount, mFrame.size());
}
mFrame.clear();
}
} else {
if ( mFrame.size() ) {
Warning( "Discarding partial frame %d, %d bytes", mFrameCount, mFrame.size() );
Warning("Discarding partial frame %d, %d bytes", mFrameCount, mFrame.size());
} else {
Warning( "Discarding frame %d", mFrameCount );
Warning("Discarding frame %d", mFrameCount);
}
mFrameGood = false;
mFrame.clear();
@ -335,7 +348,7 @@ bool RtpSource::handlePacket( const unsigned char *packet, size_t packetLen ) {
return true;
}
bool RtpSource::getFrame( Buffer &buffer ) {
bool RtpSource::getFrame(Buffer &buffer) {
if ( !mFrameReady.getValueImmediate() ) {
Debug(3, "Getting frame but not ready");
// Allow for a couple of spurious returns