2008-07-16 16:35:59 +08:00
|
|
|
//
|
2008-07-25 17:08:15 +08:00
|
|
|
// ZoneMinder RTP Source Class Interface, $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.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ZM_RTP_SOURCE_H
|
|
|
|
#define ZM_RTP_SOURCE_H
|
|
|
|
|
|
|
|
#include "zm_buffer.h"
|
2013-11-29 05:32:06 +08:00
|
|
|
#include "zm_ffmpeg.h"
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm_thread.h"
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
2010-07-05 02:09:44 +08:00
|
|
|
#include <stdint.h>
|
2008-07-16 16:35:59 +08:00
|
|
|
#include <string>
|
|
|
|
|
2013-11-29 05:32:06 +08:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54,25,0)
|
|
|
|
#define _AVCODECID AVCodecID
|
|
|
|
#else
|
|
|
|
#define _AVCODECID CodecID
|
|
|
|
#endif
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
struct RtpDataHeader;
|
|
|
|
|
|
|
|
class RtpSource
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef enum { EMPTY, FILLING, READY } FrameState;
|
|
|
|
private:
|
|
|
|
static const int RTP_SEQ_MOD = 1<<16;
|
|
|
|
static const int MAX_DROPOUT = 3000;
|
|
|
|
static const int MAX_MISORDER = 100;
|
|
|
|
static const int MIN_SEQUENTIAL = 2;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Identity
|
|
|
|
int mId; // General id (usually monitor id)
|
|
|
|
std::string mCname; // Canonical name, for SDES
|
|
|
|
|
|
|
|
// RTP/RTCP fields
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t mSsrc;
|
|
|
|
uint16_t mMaxSeq; // highest seq. number seen
|
|
|
|
uint32_t mCycles; // shifted count of seq. number cycles
|
|
|
|
uint32_t mBaseSeq; // base seq number
|
|
|
|
uint32_t mBadSeq; // last 'bad' seq number + 1
|
|
|
|
uint32_t mProbation; // sequ. packets till source is valid
|
|
|
|
uint32_t mReceivedPackets; // packets received
|
|
|
|
uint32_t mExpectedPrior; // packet expected at last interval
|
|
|
|
uint32_t mReceivedPrior; // packet received at last interval
|
|
|
|
uint32_t mTransit; // relative trans time for prev pkt
|
|
|
|
uint32_t mJitter; // estimated jitter
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
// Ports/Channels
|
|
|
|
std::string mLocalHost;
|
|
|
|
int mLocalPortChans[2];
|
|
|
|
std::string mRemoteHost;
|
|
|
|
int mRemotePortChans[2];
|
|
|
|
|
|
|
|
// Time keys
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t mRtpClock;
|
|
|
|
uint32_t mRtpFactor;
|
2008-07-16 16:35:59 +08:00
|
|
|
struct timeval mBaseTimeReal;
|
|
|
|
struct timeval mBaseTimeNtp;
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t mBaseTimeRtp;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
struct timeval mLastSrTimeReal;
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t mLastSrTimeNtpSecs;
|
|
|
|
uint32_t mLastSrTimeNtpFrac;
|
2008-07-16 16:35:59 +08:00
|
|
|
struct timeval mLastSrTimeNtp;
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t mLastSrTimeRtp;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
// Stats, intermittently updated
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t mExpectedPackets;
|
|
|
|
uint32_t mLostPackets;
|
|
|
|
uint8_t mLostFraction;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2013-11-29 05:32:06 +08:00
|
|
|
_AVCODECID mCodecId
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
Buffer mFrame;
|
|
|
|
int mFrameCount;
|
|
|
|
bool mFrameGood;
|
2013-09-27 19:08:11 +08:00
|
|
|
bool prevM;
|
2008-07-16 16:35:59 +08:00
|
|
|
ThreadData<bool> mFrameReady;
|
|
|
|
ThreadData<bool> mFrameProcessed;
|
|
|
|
|
|
|
|
private:
|
2010-07-05 02:00:35 +08:00
|
|
|
void init( uint16_t seq );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
public:
|
2013-11-29 05:32:06 +08:00
|
|
|
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 );
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
bool updateSeq( uint16_t seq );
|
2008-07-16 16:35:59 +08:00
|
|
|
void updateJitter( const RtpDataHeader *header );
|
2010-07-05 02:00:35 +08:00
|
|
|
void updateRtcpData( uint32_t ntpTimeSecs, uint32_t ntpTimeFrac, uint32_t rtpTime );
|
2008-07-16 16:35:59 +08:00
|
|
|
void updateRtcpStats();
|
|
|
|
|
|
|
|
bool handlePacket( const unsigned char *packet, size_t packetLen );
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t getSsrc() const
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
return( mSsrc );
|
|
|
|
}
|
2010-07-05 02:00:35 +08:00
|
|
|
void setSsrc( uint32_t ssrc )
|
2009-05-28 16:47:59 +08:00
|
|
|
{
|
|
|
|
mSsrc = ssrc;
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
bool getFrame( Buffer &buffer );
|
|
|
|
|
|
|
|
const std::string &getCname() const
|
|
|
|
{
|
|
|
|
return( mCname );
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &getLocalHost() const
|
|
|
|
{
|
|
|
|
return( mLocalHost );
|
|
|
|
}
|
|
|
|
|
|
|
|
int getLocalDataPort() const
|
|
|
|
{
|
|
|
|
return( mLocalPortChans[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
int getLocalCtrlPort() const
|
|
|
|
{
|
|
|
|
return( mLocalPortChans[1] );
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &getRemoteHost() const
|
|
|
|
{
|
|
|
|
return( mRemoteHost );
|
|
|
|
}
|
|
|
|
|
|
|
|
int getRemoteDataPort() const
|
|
|
|
{
|
|
|
|
return( mRemotePortChans[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
int getRemoteCtrlPort() const
|
|
|
|
{
|
|
|
|
return( mRemotePortChans[1] );
|
|
|
|
}
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t getMaxSeq() const
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
return( mCycles + mMaxSeq );
|
|
|
|
}
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t getExpectedPackets() const
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
return( mExpectedPackets );
|
|
|
|
}
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t getLostPackets() const
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
return( mLostPackets );
|
|
|
|
}
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
uint8_t getLostFraction() const
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
return( mLostFraction );
|
|
|
|
}
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t getJitter() const
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
return( mJitter >> 4 );
|
|
|
|
}
|
|
|
|
|
2010-07-05 02:00:35 +08:00
|
|
|
uint32_t getLastSrTimestamp() const
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
return( ((mLastSrTimeNtpSecs&0xffff)<<16)|(mLastSrTimeNtpFrac>>16) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-29 05:32:06 +08:00
|
|
|
#undef _AVCODECID
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
#endif // ZM_RTP_SOURCE_H
|