Skip to content

Commit

Permalink
[core] Slightly refactored DriftTracer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko authored and rndi committed Sep 3, 2019
1 parent 6f2c941 commit 980492d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
4 changes: 2 additions & 2 deletions srtcore/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1384,15 +1384,15 @@ uint64_t CRcvBuffer::getTsbPdTimeBase(uint32_t timestamp)
/* Exiting wrap check period (if for packet delivery head) */
m_bTsbPdWrapCheck = false;
m_ullTsbPdTimeBase += uint64_t(CPacket::MAX_TIMESTAMP) + 1;
tslog.Debug("tsppd wrap period ends");
tslog.Debug("tsbpd wrap period ends");
}
}
// Check if timestamp is in the last 30 seconds before reaching the MAX_TIMESTAMP.
else if (timestamp > (CPacket::MAX_TIMESTAMP - TSBPD_WRAP_PERIOD))
{
/* Approching wrap around point, start wrap check period (if for packet delivery head) */
m_bTsbPdWrapCheck = true;
tslog.Debug("tsppd wrap period begins");
tslog.Debug("tsbpd wrap period begins");
}
return(m_ullTsbPdTimeBase + carryover);
}
Expand Down
1 change: 0 additions & 1 deletion srtcore/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ class CRcvBuffer
int getAvailBufSize() const;

/// Query how many data has been continuously received (for reading) and ready to play (tsbpdtime < now).
/// @param [out] tsbpdtime localtime-based (uSec) packet time stamp including buffering delay
/// @return size of valid (continous) data for reading.

int getRcvDataSize() const;
Expand Down
4 changes: 2 additions & 2 deletions srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ size_t CUDT::fillSrtHandshake_HSREQ(uint32_t* srtdata, size_t /* srtlen - unused

size_t CUDT::fillSrtHandshake_HSRSP(uint32_t* srtdata, size_t /* srtlen - unused */, int hs_version)
{
// Setting m_ullRcvPeerStartTime is done ine processSrtMsg_HSREQ(), so
// Setting m_ullRcvPeerStartTime is done in processSrtMsg_HSREQ(), so
// this condition will be skipped only if this function is called without
// getting first received HSREQ. Doesn't look possible in both HSv4 and HSv5.
if (m_ullRcvPeerStartTime != 0)
Expand Down Expand Up @@ -4433,7 +4433,7 @@ void* CUDT::tsbpd(void* param)
}
else if (passack)
{
/* Packets ready to play but not yet acknowledged (should occurs withing 10ms) */
/* Packets ready to play but not yet acknowledged (should happen within 10ms) */
rxready = false;
tsbpdtime = 0; //Next sent ack will unblock
} /* else packet ready to play */
Expand Down
73 changes: 36 additions & 37 deletions srtcore/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -637,62 +637,61 @@ inline std::string FormatBinaryString(const uint8_t* bytes, size_t size)
}


// This function is useful in multiple uses where
// the time drift should be traced. It's currently in use in every
// solution that implements any kind of TSBPD (AKA Stower).
/// This class is useful in every place where
/// the time drift should be traced. It's currently in use in every
/// solution that implements any kind of TSBPD.
template<unsigned MAX_SPAN, int MAX_DRIFT, bool CLEAR_ON_UPDATE = true>
class DriftTracer
{
int64_t m_qDrift;
int64_t m_qOverdrift;
int64_t m_qDrift;
int64_t m_qOverdrift;

int64_t m_qDriftSum;
int64_t m_qDriftSum;
unsigned m_uDriftSpan;

public:
DriftTracer()
: m_qDrift(),
m_qOverdrift(),
m_qDriftSum(),
m_uDriftSpan()
: m_qDrift(0)
, m_qOverdrift(0)
, m_qDriftSum(0)
, m_uDriftSpan(0)
{}

bool update(int64_t driftval)
{
m_qDriftSum += driftval;
++m_uDriftSpan;

if ( m_uDriftSpan >= MAX_SPAN )
{
if ( CLEAR_ON_UPDATE )
m_qOverdrift = 0;
if (m_uDriftSpan < MAX_SPAN)
return false;

// Calculate the median of all drift values.
// In most cases, the divisor should be == MAX_SPAN.
m_qDrift = m_qDriftSum / m_uDriftSpan;
if (CLEAR_ON_UPDATE)
m_qOverdrift = 0;

// And clear the collection
m_qDriftSum = 0;
m_uDriftSpan = 0;
// Calculate the median of all drift values.
// In most cases, the divisor should be == MAX_SPAN.
m_qDrift = m_qDriftSum / m_uDriftSpan;

// In case of "overdrift", save the overdriven value in 'm_qOverdrift'.
// In clear mode, you should add this value to the time base when update()
// returns true. The drift value will be since now measured with the
// overdrift assumed to be added to the base.
if (std::abs(m_qDrift) > MAX_DRIFT)
{
m_qOverdrift = m_qDrift < 0 ? -MAX_DRIFT : MAX_DRIFT;
m_qDrift -= m_qOverdrift;
}
// And clear the collection
m_qDriftSum = 0;
m_uDriftSpan = 0;

// printDriftOffset(m_qOverdrift, m_qDrift);
// In case of "overdrift", save the overdriven value in 'm_qOverdrift'.
// In clear mode, you should add this value to the time base when update()
// returns true. The drift value will be since now measured with the
// overdrift assumed to be added to the base.
if (std::abs(m_qDrift) > MAX_DRIFT)
{
m_qOverdrift = m_qDrift < 0 ? -MAX_DRIFT : MAX_DRIFT;
m_qDrift -= m_qOverdrift;
}

// Timebase is separate
// m_qTimeBase += m_qOverdrift;
// printDriftOffset(m_qOverdrift, m_qDrift);

return true;
}
return false;
// Timebase is separate
// m_qTimeBase += m_qOverdrift;

return true;
}

// These values can be read at any time, however if you want
Expand All @@ -716,8 +715,8 @@ class DriftTracer
// any changes in overdrift. By manipulating the MAX_DRIFT parameter
// you can decide how high the drift can go relatively to stay below
// overdrift.
int64_t drift() { return m_qDrift; }
int64_t overdrift() { return m_qOverdrift; }
int64_t drift() const { return m_qDrift; }
int64_t overdrift() const { return m_qOverdrift; }
};

template <class KeyType, class ValueType>
Expand Down

0 comments on commit 980492d

Please sign in to comment.