Skip to content

Commit

Permalink
[core] Fixed buffers avg size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko authored and rndi committed Aug 8, 2019
1 parent 9b7fade commit c929c61
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions srtcore/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,37 +501,37 @@ int CSndBuffer::getAvgBufSize(ref_t<int> r_bytes, ref_t<int> r_tsp)

void CSndBuffer::updAvgBufSize(uint64_t now)
{
uint64_t elapsed = (now - m_LastSamplingTime) / 1000; //ms since last sampling
const uint64_t elapsed_ms = (now - m_LastSamplingTime) / 1000; //ms since last sampling

if ((1000000 / SRT_MAVG_SAMPLING_RATE) / 1000 > elapsed)
if ((1000000 / SRT_MAVG_SAMPLING_RATE) / 1000 > elapsed_ms)
return;

if (1000000 < elapsed)
if (1000 < elapsed_ms)
{
/* No sampling in last 1 sec, initialize average */
m_iCountMAvg = getCurrBufSize(Ref(m_iBytesCountMAvg), Ref(m_TimespanMAvg));
m_LastSamplingTime = now;
}
else //((1000000 / SRT_MAVG_SAMPLING_RATE) / 1000 <= elapsed)
else //((1000000 / SRT_MAVG_SAMPLING_RATE) / 1000 <= elapsed_ms)
{
/*
* weight last average value between -1 sec and last sampling time (LST)
* and new value between last sampling time and now
* |elapsed|
* |elapsed_ms|
* +----------------------------------+-------+
* -1 LST 0(now)
*/
int instspan;
int bytescount;
int count = getCurrBufSize(Ref(bytescount), Ref(instspan));

HLOGC(dlog.Debug, log << "updAvgBufSize: " << elapsed
HLOGC(dlog.Debug, log << "updAvgBufSize: " << elapsed_ms
<< ": " << count << " " << bytescount
<< " " << instspan << "ms");

m_iCountMAvg = (int)(((count * (1000 - elapsed)) + (count * elapsed)) / 1000);
m_iBytesCountMAvg = (int)(((bytescount * (1000 - elapsed)) + (bytescount * elapsed)) / 1000);
m_TimespanMAvg = (int)(((instspan * (1000 - elapsed)) + (instspan * elapsed)) / 1000);
m_iCountMAvg = (int)(((count * (1000 - elapsed_ms)) + (count * elapsed_ms)) / 1000);
m_iBytesCountMAvg = (int)(((bytescount * (1000 - elapsed_ms)) + (bytescount * elapsed_ms)) / 1000);
m_TimespanMAvg = (int)(((instspan * (1000 - elapsed_ms)) + (instspan * elapsed_ms)) / 1000);
m_LastSamplingTime = now;
}
}
Expand Down Expand Up @@ -1196,40 +1196,40 @@ int CRcvBuffer::getRcvAvgDataSize(int &bytes, int &timespan)
/* Update moving average of acked data pkts, bytes, and timespan (ms) of the receive buffer */
void CRcvBuffer::updRcvAvgDataSize(uint64_t now)
{
uint64_t elapsed = (now - m_LastSamplingTime) / 1000; //ms since last sampling
const uint64_t elapsed_ms = (now - m_LastSamplingTime) / 1000; //ms since last sampling

if ((1000000 / SRT_MAVG_SAMPLING_RATE) / 1000 > elapsed)
if ((1000000 / SRT_MAVG_SAMPLING_RATE) / 1000 > elapsed_ms)
return; /* Last sampling too recent, skip */

if (1000000 < elapsed)
if (1000 < elapsed_ms)
{
/* No sampling in last 1 sec, initialize/reset moving average */
m_iCountMAvg = getRcvDataSize(m_iBytesCountMAvg, m_TimespanMAvg);
m_LastSamplingTime = now;

HLOGC(dlog.Debug, log << "getRcvDataSize: " << m_iCountMAvg << " " << m_iBytesCountMAvg
<< " " << m_TimespanMAvg << " ms elapsed: " << elapsed << " ms");
<< " " << m_TimespanMAvg << " ms elapsed_ms: " << elapsed_ms << " ms");
}
else if ((1000000 / SRT_MAVG_SAMPLING_RATE) / 1000 <= elapsed)
else if ((1000000 / SRT_MAVG_SAMPLING_RATE) / 1000 <= elapsed_ms)
{
/*
* Weight last average value between -1 sec and last sampling time (LST)
* and new value between last sampling time and now
* |elapsed|
* |elapsed_ms|
* +----------------------------------+-------+
* -1 LST 0(now)
*/
int instspan;
int bytescount;
int count = getRcvDataSize(bytescount, instspan);

m_iCountMAvg = (int)(((count * (1000 - elapsed)) + (count * elapsed)) / 1000);
m_iBytesCountMAvg = (int)(((bytescount * (1000 - elapsed)) + (bytescount * elapsed)) / 1000);
m_TimespanMAvg = (int)(((instspan * (1000 - elapsed)) + (instspan * elapsed)) / 1000);
m_iCountMAvg = (int)(((count * (1000 - elapsed_ms)) + (count * elapsed_ms)) / 1000);
m_iBytesCountMAvg = (int)(((bytescount * (1000 - elapsed_ms)) + (bytescount * elapsed_ms)) / 1000);
m_TimespanMAvg = (int)(((instspan * (1000 - elapsed_ms)) + (instspan * elapsed_ms)) / 1000);
m_LastSamplingTime = now;

HLOGC(dlog.Debug, log << "getRcvDataSize: " << count << " " << bytescount << " " << instspan
<< " ms elapsed: " << elapsed << " ms");
<< " ms elapsed_ms: " << elapsed_ms << " ms");
}
}
#endif /* SRT_ENABLE_RCVBUFSZ_MAVG */
Expand Down

0 comments on commit c929c61

Please sign in to comment.