diff --git a/srtcore/buffer_tools.cpp b/srtcore/buffer_tools.cpp index 194c49c09..68bb26ed1 100644 --- a/srtcore/buffer_tools.cpp +++ b/srtcore/buffer_tools.cpp @@ -159,8 +159,8 @@ CSndRateEstimator::CSndRateEstimator(const time_point& tsNow) : m_tsFirstSampleTime(tsNow) , m_iCurSampleIdx(0) , m_iRateBps(0) - , m_Samples(NUM_PERIODS) , m_iFirstSampleIdx(0) + , m_Samples(NUM_PERIODS) { } @@ -275,11 +275,9 @@ int CSndRateEstimator::incSampleIdx(int val, int inc) const } CMovingRateEstimator::CMovingRateEstimator() - : m_tsFirstSampleTime(sync::steady_clock::now()) - , m_iFirstSampleIdx(0) - , m_iCurSampleIdx(0) - , m_iRateBps(0) - , m_Samples(NUM_PERIODS) + : CSndRateEstimator(sync::steady_clock::now()) + , m_MovingSamples(NUM_PERIODS) + , lastSlotTimestamp(sync::steady_clock::now()) { resetRate(0, NUM_PERIODS); } @@ -291,8 +289,8 @@ void CMovingRateEstimator::addSample(int pkts, double bytes) if (iSampleDeltaIdx == 0) { - m_Samples[m_iCurSampleIdx].m_iBytesCount += bytes; - m_Samples[m_iCurSampleIdx].m_iPktsCount += pkts; + m_MovingSamples[m_iCurSampleIdx].m_iBytesCount += bytes; + m_MovingSamples[m_iCurSampleIdx].m_iPktsCount += pkts; } else { @@ -305,9 +303,9 @@ void CMovingRateEstimator::addSample(int pkts, double bytes) resetRate(0, loopbackDiff); } - m_iCurSampleIdx = ((m_iCurSampleIdx + iSampleDeltaIdx) % NUM_PERIODS); - m_Samples[m_iCurSampleIdx].m_iBytesCount = bytes; - m_Samples[m_iCurSampleIdx].m_iPktsCount = pkts; + m_iCurSampleIdx = ((m_iCurSampleIdx + iSampleDeltaIdx) % NUM_PERIODS); + m_MovingSamples[m_iCurSampleIdx].m_iBytesCount = bytes; + m_MovingSamples[m_iCurSampleIdx].m_iPktsCount = pkts; lastSlotTimestamp = now; } @@ -318,7 +316,7 @@ void CMovingRateEstimator::addSample(int pkts, double bytes) void CMovingRateEstimator::resetRate(int from, int to) { for (int i = max(0, from); i < min(int(NUM_PERIODS), to); i++) - m_Samples[i].reset(); + m_MovingSamples[i].reset(); } void CMovingRateEstimator::computeAverageValue() @@ -329,7 +327,7 @@ void CMovingRateEstimator::computeAverageValue() m_iRateBps = 0; for (int i = 0; i < NUM_PERIODS; i++) - m_iRateBps += (m_Samples[i].m_iBytesCount + (CPacket::HDR_SIZE * m_Samples[i].m_iPktsCount)); + m_iRateBps += (m_MovingSamples[i].m_iBytesCount + (CPacket::HDR_SIZE * m_MovingSamples[i].m_iPktsCount)); if (isFirstPeriod) m_iRateBps = m_iRateBps * 1000 / startDelta; diff --git a/srtcore/buffer_tools.h b/srtcore/buffer_tools.h index 0be738da6..f18e92c93 100644 --- a/srtcore/buffer_tools.h +++ b/srtcore/buffer_tools.h @@ -190,17 +190,16 @@ class CSndRateEstimator bool empty() const { return m_iPktsCount == 0; } }; - srt::FixedArray m_Samples; // Table of stored data - private: - static const int NUM_PERIODS = 10; - static const int SAMPLE_DURATION_MS = 100; // 100 ms - int m_iFirstSampleIdx; //< Index of the first sample. + static const int NUM_PERIODS = 10; + static const int SAMPLE_DURATION_MS = 100; // 100 ms + int m_iFirstSampleIdx; //< Index of the first sample. + srt::FixedArray m_Samples; // Table of stored data int incSampleIdx(int val, int inc = 1) const; }; -class CMovingRateEstimator +class CMovingRateEstimator : CSndRateEstimator { typedef sync::steady_clock::time_point time_point; @@ -221,52 +220,10 @@ class CMovingRateEstimator private: // We would like responsiveness (accuracy) of rate estimation higher than 100 ms // (ideally around 50 ms) for network adaptive algorithms. - const int NUM_PERIODS = 100; // To get 1s of values - const int SAMPLE_DURATION_MS = 10; // 10 ms - time_point m_tsFirstSampleTime; //< Start time of the first sample. - time_point lastSlotTimestamp; // Used to compute the delta between 2 calls - int m_iFirstSampleIdx; //< Index of the first sample. - int m_iCurSampleIdx; //< Index of the current sample being collected. - int m_iRateBps; //< Rate in Bytes/sec. - - struct Sample - { - int m_iPktsCount; // number of payload packets - int m_iBytesCount; // number of payload bytes - - void reset() - { - m_iPktsCount = 0; - m_iBytesCount = 0; - } - - Sample() - : m_iPktsCount(0) - , m_iBytesCount(0) - { - } - - Sample(int iPkts, int iBytes) - : m_iPktsCount(iPkts) - , m_iBytesCount(iBytes) - { - } - - Sample operator+(const Sample& other) - { - return Sample(m_iPktsCount + other.m_iPktsCount, m_iBytesCount + other.m_iBytesCount); - } - - Sample& operator+=(const Sample& other) - { - *this = *this + other; - return *this; - } - - bool empty() const { return m_iPktsCount == 0; } - }; - - srt::FixedArray m_Samples; // Table of stored data + const int NUM_PERIODS = 100; // To get 1s of values + const int SAMPLE_DURATION_MS = 10; // 10 ms + srt::FixedArray m_MovingSamples; // Table of stored data + time_point lastSlotTimestamp; //< Start time of the first sample. /// This method will compute the average value based on all table's measures and the period /// (NUM_PERIODS*SAMPLE_DURATION_MS) diff --git a/srtcore/stats.h b/srtcore/stats.h index 16043fc35..4b67378fd 100644 --- a/srtcore/stats.h +++ b/srtcore/stats.h @@ -145,7 +145,7 @@ struct Sender Metric recvdAck; // The number of ACK packets received by the sender. Metric recvdNak; // The number of ACK packets received by the sender. - CMovingRateEstimator mobileRateEstimator; // The average Mbps over last second + CMovingRateEstimator mavgRateEstimator; // The average Mbps over last second void reset() { @@ -171,11 +171,11 @@ struct Sender sentFilterExtra.resetTrace(); } - void updateRate(int pkts, double bytes) { mobileRateEstimator.addSample(pkts, bytes); } + void updateRate(int pkts, double bytes) { mavgRateEstimator.addSample(pkts, bytes); } - void resetRate() { mobileRateEstimator.resetRate(); } + void resetRate() { mavgRateEstimator.resetRate(); } - int getAverageValue() { return mobileRateEstimator.getRate(); } + int getAverageValue() { return mavgRateEstimator.getRate(); } }; /// Receiver-side statistics. @@ -196,7 +196,7 @@ struct Receiver Metric sentAck; // The number of ACK packets sent by the receiver. Metric sentNak; // The number of NACK packets sent by the receiver. - CMovingRateEstimator mobileRateEstimator; // The average Mbps over last second + CMovingRateEstimator mavgRateEstimator; // The average Mbps over last second void reset() { @@ -230,11 +230,11 @@ struct Receiver sentNak.resetTrace(); } - void updateRate(int pkts, double bytes) { mobileRateEstimator.addSample(pkts, bytes); } + void updateRate(int pkts, double bytes) { mavgRateEstimator.addSample(pkts, bytes); } - void resetRate() { mobileRateEstimator.resetRate(); } + void resetRate() { mavgRateEstimator.resetRate(); } - int getAverageValue() { return mobileRateEstimator.getRate(); } + int getAverageValue() { return mavgRateEstimator.getRate(); } }; } // namespace stats