- make jitter calculation closer to RFC

This commit is contained in:
dmytro.bogovych 2019-03-04 19:11:54 +01:00
parent 3cd89fe12c
commit 34d963be40
2 changed files with 236 additions and 195 deletions

View File

@ -9,7 +9,38 @@ using namespace MT;
void JitterStatistics::process(jrtplib::RTPPacket* packet, int rate)
{
uint32_t arrival = 0;
int d = 0;
uint32_t transit = 0;
uint64_t current_time = TimeHelper::getTimestamp();
int interarrival_time_ms = current_time - mPrevRxTimestamp;
// get the 'arrival time' of this packet as measured in 'timestamp units' and offset
// to match the timestamp range in this stream
arrival = interarrival_time_ms * (rate / 1000);
if (mPrevArrival == 0)
arrival = packet->GetTimestamp();
else
arrival += mPrevArrival;;
mPrevArrival = packet->GetTimestamp();
transit = arrival - packet->GetTimestamp();
jrtplib::RTPTime receiveTime = packet->GetReceiveTime();
d = transit - mPrevTransit;
mPrevTransit = transit;
if (d < 0)
d = -d;
mJitter += (1.0/16.0) * ((double)d - mJitter);
mPrevRxTimestamp = current_time;
if (mMaxJitter < mJitter)
mMaxJitter = mJitter;
uint32_t timestamp = packet->GetTimestamp();
if (!mLastJitter.is_initialized())
@ -20,7 +51,14 @@ void JitterStatistics::process(jrtplib::RTPPacket* packet, int rate)
}
else
{
double delta = (receiveTime.GetDouble() - mReceiveTime.GetDouble()) - double(timestamp - mReceiveTimestamp) / rate;
double receiveDelta = receiveTime.GetDouble() - mReceiveTime.GetDouble();
double timestampDelta = double(timestamp - mReceiveTimestamp);
if (timestampDelta == 0.0)
// Skip current packet silently. Most probably it is error in RTP stream like duplicated packet.
return;
double delta = receiveDelta / timestampDelta;
if (fabs(delta) > mMaxDelta)
mMaxDelta = fabs(delta);

View File

@ -15,9 +15,9 @@ using std::experimental::optional;
namespace MT
{
template<typename T>
struct Average
{
template<typename T>
struct Average
{
int mCount = 0;
T mSum = 0;
T getAverage() const
@ -32,11 +32,11 @@ namespace MT
mCount++;
mSum += value;
}
};
};
template<typename T, int minimum = 100000, int maximum = 0>
struct ProbeStats
{
template<typename T, int minimum = 100000, int maximum = 0>
struct ProbeStats
{
T mMin = minimum;
T mMax = maximum;
Average<T> mAverage;
@ -64,36 +64,39 @@ namespace MT
else
return 0;
}
};
};
template<typename T>
struct StreamStats
{
template<typename T>
struct StreamStats
{
T mChunk;
T mTotal;
};
};
class JitterStatistics
{
public:
class JitterStatistics
{
public:
void process(jrtplib::RTPPacket* packet, int samplerate);
ProbeStats<double> get() const { return mJitter; }
double getMaxDelta() const { return mMaxDelta; }
protected:
protected:
// Jitter calculation
jrtplib::RTPTime mReceiveTime = jrtplib::RTPTime(0,0);
uint32_t mReceiveTimestamp = 0;
optional<double> mLastJitter;
ProbeStats<double> mJitter;
double mMaxDelta = 0.0;
};
uint64_t mPrevRxTimestamp = 0;
uint64_t mPrevArrival = 0;
uint64_t mPrevTransit = 0;
};
class Statistics
{
public:
class Statistics
{
public:
int mReceived, // Received traffic in bytes
mSent, // Sent traffic in bytes
mReceivedRtp, // Number of received rtp packets
@ -136,13 +139,13 @@ namespace MT
Statistics& operator -= (const Statistics& src);
float mNetworkMos = 0.0;
#if defined(USE_PVQA_LIBRARY) && !defined(PVQA_SERVER)
#if defined(USE_PVQA_LIBRARY) && !defined(PVQA_SERVER)
float mPvqaMos = 0.0;
std::string mPvqaReport;
#endif
#endif
std::string toShortString() const;
};
};
} // end of namespace MT