2008-07-16 16:35:59 +08:00
|
|
|
//
|
2008-07-25 17:08:15 +08:00
|
|
|
// ZoneMinder RTP Source Class Implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2008-07-16 16:35:59 +08:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "zm_rtp_source.h"
|
|
|
|
|
|
|
|
#include "zm_time.h"
|
|
|
|
#include "zm_rtp_data.h"
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2014-01-02 00:19:42 +08:00
|
|
|
#if HAVE_LIBAVCODEC
|
|
|
|
|
2013-11-29 05:32:06 +08:00
|
|
|
RtpSource::RtpSource( int id, const std::string &localHost, int localPortBase, const std::string &remoteHost, int remotePortBase, uint32_t ssrc, uint16_t seq, uint32_t rtpClock, uint32_t rtpTime, _AVCODECID codecId ) :
|
2008-07-16 16:35:59 +08:00
|
|
|
mId( id ),
|
|
|
|
mSsrc( ssrc ),
|
|
|
|
mLocalHost( localHost ),
|
|
|
|
mRemoteHost( remoteHost ),
|
|
|
|
mRtpClock( rtpClock ),
|
2013-11-29 05:32:06 +08:00
|
|
|
mCodecId( codecId ),
|
2008-07-16 16:35:59 +08:00
|
|
|
mFrame( 65536 ),
|
|
|
|
mFrameCount( 0 ),
|
|
|
|
mFrameGood( true ),
|
|
|
|
mFrameReady( false ),
|
|
|
|
mFrameProcessed( false )
|
|
|
|
{
|
|
|
|
char hostname[256] = "";
|
|
|
|
gethostname( hostname, sizeof(hostname) );
|
|
|
|
|
|
|
|
mCname = stringtf( "zm-%d@%s", mId, hostname );
|
|
|
|
Debug( 3, "RTP CName = %s", mCname.c_str() );
|
|
|
|
|
|
|
|
init( seq );
|
|
|
|
mMaxSeq = seq - 1;
|
|
|
|
mProbation = MIN_SEQUENTIAL;
|
|
|
|
|
|
|
|
mLocalPortChans[0] = localPortBase;
|
|
|
|
mLocalPortChans[1] = localPortBase+1;
|
|
|
|
|
|
|
|
mRemotePortChans[0] = remotePortBase;
|
|
|
|
mRemotePortChans[1] = remotePortBase+1;
|
|
|
|
|
|
|
|
mRtpFactor = mRtpClock;
|
|
|
|
|
|
|
|
mBaseTimeReal = tvNow();
|
|
|
|
mBaseTimeNtp = tvZero();
|
|
|
|
mBaseTimeRtp = rtpTime;
|
|
|
|
|
|
|
|
mLastSrTimeReal = tvZero();
|
|
|
|
mLastSrTimeNtp = tvZero();
|
|
|
|
mLastSrTimeRtp = 0;
|
2013-11-29 07:07:46 +08:00
|
|
|
|
2014-02-05 17:13:27 +08:00
|
|
|
if(mCodecId != AV_CODEC_ID_H264 && mCodecId != AV_CODEC_ID_MPEG4)
|
2013-11-29 07:07:46 +08:00
|
|
|
Warning( "The device is using a codec that may not be supported. Do not be surprised if things don't work." );
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
void RtpSource::init( uint16_t seq )
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
Debug( 3, "Initialising sequence" );
|
|
|
|
mBaseSeq = seq;
|
|
|
|
mMaxSeq = seq;
|
|
|
|
mBadSeq = RTP_SEQ_MOD + 1; // so seq == mBadSeq is false
|
|
|
|
mCycles = 0;
|
|
|
|
mReceivedPackets = 0;
|
|
|
|
mReceivedPrior = 0;
|
|
|
|
mExpectedPrior = 0;
|
|
|
|
// other initialization
|
|
|
|
mJitter = 0;
|
|
|
|
mTransit = 0;
|
|
|
|
}
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
bool RtpSource::updateSeq( uint16_t seq )
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
2010-07-05 02:00:35 +08:00
|
|
|
uint16_t uDelta = seq - mMaxSeq;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
// Source is not valid until MIN_SEQUENTIAL packets with
|
|
|
|
// sequential sequence numbers have been received.
|
2008-07-21 23:20:52 +08:00
|
|
|
Debug( 5, "Seq: %d", seq );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
if ( mProbation)
|
|
|
|
{
|
|
|
|
// packet is in sequence
|
|
|
|
if ( seq == mMaxSeq + 1)
|
|
|
|
{
|
2010-07-05 02:00:35 +08:00
|
|
|
Debug( 3, "Sequence in probation %d, in sequence", mProbation );
|
2008-07-16 16:35:59 +08:00
|
|
|
mProbation--;
|
|
|
|
mMaxSeq = seq;
|
|
|
|
if ( mProbation == 0 )
|
|
|
|
{
|
|
|
|
init( seq );
|
|
|
|
mReceivedPackets++;
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-05 02:00:35 +08:00
|
|
|
Warning( "Sequence in probation %d, out of sequence", mProbation );
|
2008-07-16 16:35:59 +08:00
|
|
|
mProbation = MIN_SEQUENTIAL - 1;
|
|
|
|
mMaxSeq = seq;
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
else if ( uDelta < MAX_DROPOUT )
|
|
|
|
{
|
|
|
|
if ( uDelta == 1 )
|
|
|
|
{
|
|
|
|
Debug( 3, "Packet in sequence, gap %d", uDelta );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Warning( "Packet in sequence, gap %d", uDelta );
|
|
|
|
}
|
|
|
|
|
|
|
|
// in order, with permissible gap
|
|
|
|
if ( seq < mMaxSeq )
|
|
|
|
{
|
|
|
|
// Sequence number wrapped - count another 64K cycle.
|
|
|
|
mCycles += RTP_SEQ_MOD;
|
|
|
|
}
|
|
|
|
mMaxSeq = seq;
|
|
|
|
}
|
|
|
|
else if ( uDelta <= RTP_SEQ_MOD - MAX_MISORDER )
|
|
|
|
{
|
|
|
|
Warning( "Packet out of sequence, gap %d", uDelta );
|
|
|
|
// the sequence number made a very large jump
|
|
|
|
if ( seq == mBadSeq )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mBadSeq = (seq + 1) & (RTP_SEQ_MOD-1);
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Warning( "Packet duplicate or reordered, gap %d", uDelta );
|
|
|
|
// duplicate or reordered packet
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
mReceivedPackets++;
|
|
|
|
return( uDelta==1?true:false );
|
|
|
|
}
|
|
|
|
|
|
|
|
void RtpSource::updateJitter( const RtpDataHeader *header )
|
|
|
|
{
|
|
|
|
if ( mRtpFactor > 0 )
|
|
|
|
{
|
|
|
|
Debug( 5, "Delta rtp = %.6f", tvDiffSec( mBaseTimeReal ) );
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t localTimeRtp = mBaseTimeRtp + uint32_t( tvDiffSec( mBaseTimeReal ) * mRtpFactor );
|
|
|
|
Debug( 5, "Local RTP time = %x", localTimeRtp );
|
2008-07-16 16:35:59 +08:00
|
|
|
Debug( 5, "Packet RTP time = %x", ntohl(header->timestampN) );
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t packetTransit = localTimeRtp - ntohl(header->timestampN);
|
|
|
|
Debug( 5, "Packet transit RTP time = %x", packetTransit );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
if ( mTransit > 0 )
|
|
|
|
{
|
|
|
|
// Jitter
|
|
|
|
int d = packetTransit - mTransit;
|
|
|
|
Debug( 5, "Jitter D = %d", d );
|
|
|
|
if ( d < 0 )
|
|
|
|
d = -d;
|
|
|
|
//mJitter += (1./16.) * ((double)d - mJitter);
|
|
|
|
mJitter += d - ((mJitter + 8) >> 4);
|
|
|
|
}
|
|
|
|
mTransit = packetTransit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mJitter = 0;
|
|
|
|
}
|
2010-07-05 02:00:35 +08:00
|
|
|
Debug( 5, "RTP Jitter: %d", mJitter );
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
void RtpSource::updateRtcpData( uint32_t ntpTimeSecs, uint32_t ntpTimeFrac, uint32_t rtpTime )
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
struct timeval ntpTime = tvMake( ntpTimeSecs, suseconds_t((USEC_PER_SEC*(ntpTimeFrac>>16))/(1<<16)) );
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
Debug( 5, "ntpTime: %ld.%06ld, rtpTime: %x", ntpTime.tv_sec, ntpTime.tv_usec, rtpTime );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
if ( mBaseTimeNtp.tv_sec == 0 )
|
|
|
|
{
|
|
|
|
mBaseTimeReal = tvNow();
|
|
|
|
mBaseTimeNtp = ntpTime;
|
|
|
|
mBaseTimeRtp = rtpTime;
|
|
|
|
}
|
|
|
|
else if ( !mRtpClock )
|
|
|
|
{
|
2010-07-05 02:00:35 +08:00
|
|
|
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 );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
double diffNtpTime = tvDiffSec( mBaseTimeNtp, ntpTime );
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t diffRtpTime = rtpTime - mBaseTimeRtp;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
//Debug( 5, "Real-diff: %.6f", diffRealTime );
|
|
|
|
Debug( 5, "NTP-diff: %.6f", diffNtpTime );
|
2010-07-05 02:00:35 +08:00
|
|
|
Debug( 5, "RTP-diff: %d", diffRtpTime );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
mRtpFactor = (uint32_t)(diffRtpTime / diffNtpTime);
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
Debug( 5, "RTPfactor: %d", mRtpFactor );
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
mLastSrTimeNtpSecs = ntpTimeSecs;
|
|
|
|
mLastSrTimeNtpFrac = ntpTimeFrac;
|
|
|
|
mLastSrTimeNtp = ntpTime;
|
|
|
|
mLastSrTimeRtp = rtpTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RtpSource::updateRtcpStats()
|
|
|
|
{
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t extendedMax = mCycles + mMaxSeq;
|
2008-07-16 16:35:59 +08:00
|
|
|
mExpectedPackets = extendedMax - mBaseSeq + 1;
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
Debug( 5, "Expected packets = %d", mExpectedPackets );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
// The number of packets lost is defined to be the number of packets
|
|
|
|
// expected less the number of packets actually received:
|
|
|
|
mLostPackets = mExpectedPackets - mReceivedPackets;
|
2010-07-05 02:00:35 +08:00
|
|
|
Debug( 5, "Lost packets = %d", mLostPackets );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t expectedInterval = mExpectedPackets - mExpectedPrior;
|
|
|
|
Debug( 5, "Expected interval = %d", expectedInterval );
|
2008-07-16 16:35:59 +08:00
|
|
|
mExpectedPrior = mExpectedPackets;
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t receivedInterval = mReceivedPackets - mReceivedPrior;
|
|
|
|
Debug( 5, "Received interval = %d", receivedInterval );
|
2008-07-16 16:35:59 +08:00
|
|
|
mReceivedPrior = mReceivedPackets;
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t lostInterval = expectedInterval - receivedInterval;
|
|
|
|
Debug( 5, "Lost interval = %d", lostInterval );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
if ( expectedInterval == 0 || lostInterval <= 0 )
|
|
|
|
mLostFraction = 0;
|
|
|
|
else
|
|
|
|
mLostFraction = (lostInterval << 8) / expectedInterval;
|
|
|
|
Debug( 5, "Lost fraction = %d", mLostFraction );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RtpSource::handlePacket( const unsigned char *packet, size_t packetLen )
|
|
|
|
{
|
|
|
|
const RtpDataHeader *rtpHeader;
|
|
|
|
rtpHeader = (RtpDataHeader *)packet;
|
2013-11-29 05:32:06 +08:00
|
|
|
int rtpHeaderSize = 12 + rtpHeader->cc * 4;
|
2013-12-07 19:21:10 +08:00
|
|
|
// No need to check for nal type as non fragmented packets already have 001 start sequence appended
|
2014-02-05 17:13:27 +08:00
|
|
|
bool h264FragmentEnd = (mCodecId == AV_CODEC_ID_H264) && (packet[rtpHeaderSize+1] & 0x40);
|
2013-12-07 19:21:10 +08:00
|
|
|
bool thisM = rtpHeader->m || h264FragmentEnd;
|
2013-09-27 19:08:11 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( updateSeq( ntohs(rtpHeader->seqN) ) )
|
|
|
|
{
|
2013-11-29 05:32:06 +08:00
|
|
|
Hexdump( 4, packet+rtpHeaderSize, 16 );
|
2013-12-01 06:00:52 +08:00
|
|
|
|
|
|
|
if ( mFrameGood )
|
2013-11-29 05:32:06 +08:00
|
|
|
{
|
2013-12-01 06:00:52 +08:00
|
|
|
int extraHeader = 0;
|
|
|
|
|
2014-02-05 17:13:27 +08:00
|
|
|
if( mCodecId == AV_CODEC_ID_H264 )
|
2013-11-29 05:32:06 +08:00
|
|
|
{
|
|
|
|
int nalType = (packet[rtpHeaderSize] & 0x1f);
|
|
|
|
|
2013-12-01 06:00:52 +08:00
|
|
|
switch (nalType)
|
2013-11-29 05:32:06 +08:00
|
|
|
{
|
2013-12-01 06:00:52 +08:00
|
|
|
case 24:
|
2013-11-29 05:32:06 +08:00
|
|
|
{
|
2013-12-01 06:00:52 +08:00
|
|
|
extraHeader = 2;
|
|
|
|
break;
|
2013-11-29 05:32:06 +08:00
|
|
|
}
|
2013-12-01 06:00:52 +08:00
|
|
|
case 25: case 26: case 27:
|
|
|
|
{
|
|
|
|
extraHeader = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FU-A and FU-B
|
|
|
|
case 28: case 29:
|
|
|
|
{
|
|
|
|
// Is this NAL the first NAL in fragmentation sequence
|
|
|
|
if ( packet[rtpHeaderSize+1] & 0x80 )
|
|
|
|
{
|
|
|
|
// Now we will form new header of frame
|
|
|
|
mFrame.append( "\x0\x0\x1\x0", 4 );
|
|
|
|
// Reconstruct NAL header from FU headers
|
|
|
|
*(mFrame+3) = (packet[rtpHeaderSize+1] & 0x1f) |
|
|
|
|
(packet[rtpHeaderSize] & 0xe0);
|
|
|
|
}
|
2013-11-29 05:32:06 +08:00
|
|
|
|
2013-12-01 06:00:52 +08:00
|
|
|
extraHeader = 2;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-29 05:32:06 +08:00
|
|
|
}
|
2013-12-01 06:00:52 +08:00
|
|
|
|
|
|
|
// Append NAL frame start code
|
|
|
|
if ( !mFrame.size() )
|
|
|
|
mFrame.append( "\x0\x0\x1", 3 );
|
2013-11-29 05:32:06 +08:00
|
|
|
}
|
2013-12-01 06:00:52 +08:00
|
|
|
mFrame.append( packet+rtpHeaderSize+extraHeader, packetLen-rtpHeaderSize-extraHeader );
|
2013-11-29 05:32:06 +08:00
|
|
|
}
|
2013-11-30 00:15:56 +08:00
|
|
|
|
2008-10-09 17:15:23 +08:00
|
|
|
Hexdump( 4, mFrame.head(), 16 );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2013-12-07 19:21:10 +08:00
|
|
|
if ( thisM )
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
if ( mFrameGood )
|
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
Debug( 2, "Got new frame %d, %d bytes", mFrameCount, mFrame.size() );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
mFrameProcessed.setValueImmediate( false );
|
|
|
|
mFrameReady.updateValueSignal( true );
|
|
|
|
if ( !mFrameProcessed.getValueImmediate() )
|
|
|
|
{
|
|
|
|
for ( int count = 0; !mFrameProcessed.getUpdatedValue( 1 ); count++ )
|
|
|
|
if( count > 1 )
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
mFrameCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
Warning( "Discarding incomplete frame %d, %d bytes", mFrameCount, mFrame.size() );
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
2008-10-09 17:15:23 +08:00
|
|
|
mFrame.clear();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
if ( mFrame.size() )
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
Warning( "Discarding partial frame %d, %d bytes", mFrameCount, mFrame.size() );
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Warning( "Discarding frame %d", mFrameCount );
|
|
|
|
}
|
|
|
|
mFrameGood = false;
|
2008-10-09 17:15:23 +08:00
|
|
|
mFrame.clear();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
2013-12-07 19:21:10 +08:00
|
|
|
if ( thisM )
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
mFrameGood = true;
|
2013-09-27 19:08:11 +08:00
|
|
|
prevM = true;
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
2013-09-27 19:08:11 +08:00
|
|
|
else
|
|
|
|
prevM = false;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
updateJitter( rtpHeader );
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RtpSource::getFrame( Buffer &buffer )
|
|
|
|
{
|
|
|
|
Debug( 3, "Getting frame" );
|
|
|
|
if ( !mFrameReady.getValueImmediate() )
|
|
|
|
{
|
|
|
|
// Allow for a couple of spurious returns
|
|
|
|
for ( int count = 0; !mFrameReady.getUpdatedValue( 1 ); count++ )
|
|
|
|
if ( count > 1 )
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
buffer = mFrame;
|
|
|
|
mFrameReady.setValueImmediate( false );
|
|
|
|
mFrameProcessed.updateValueSignal( true );
|
2008-10-09 17:15:23 +08:00
|
|
|
Debug( 3, "Copied %d bytes", buffer.size() );
|
2008-07-16 16:35:59 +08:00
|
|
|
return( true );
|
|
|
|
}
|
2013-11-29 05:32:06 +08:00
|
|
|
|
2014-01-02 00:19:42 +08:00
|
|
|
#endif // HAVE_LIBAVCODEC
|