diff --git a/webrtc/modules/remote_bitrate_estimator/bitrate_estimator.cc b/webrtc/modules/remote_bitrate_estimator/bitrate_estimator.cc index 7301e9726..316297f03 100644 --- a/webrtc/modules/remote_bitrate_estimator/bitrate_estimator.cc +++ b/webrtc/modules/remote_bitrate_estimator/bitrate_estimator.cc @@ -8,71 +8,56 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "bitrate_estimator.h" +#include "webrtc/modules/remote_bitrate_estimator/bitrate_estimator.h" namespace webrtc { -const float kBitrateAverageWindow = 500.0f; +const float kBitrateAverageWindowMs = 500.0f; BitRateStats::BitRateStats() - :_dataSamples(), _accumulatedBytes(0) -{ + : data_samples_(), + accumulated_bytes_(0) { } -BitRateStats::~BitRateStats() -{ - while (_dataSamples.size() > 0) - { - delete _dataSamples.front(); - _dataSamples.pop_front(); +BitRateStats::~BitRateStats() { + Init(); +} + +void BitRateStats::Init() { + accumulated_bytes_ = 0; + while (data_samples_.size() > 0) { + delete data_samples_.front(); + data_samples_.pop_front(); + } +} + +void BitRateStats::Update(uint32_t packet_size_bytes, int64_t now_ms) { + // Find an empty slot for storing the new sample and at the same time + // accumulate the history. + data_samples_.push_back(new DataTimeSizeTuple(packet_size_bytes, now_ms)); + accumulated_bytes_ += packet_size_bytes; + EraseOld(now_ms); +} + +void BitRateStats::EraseOld(int64_t now_ms) { + while (data_samples_.size() > 0) { + if (now_ms - data_samples_.front()->time_complete_ms > + kBitrateAverageWindowMs) { + // Delete old sample + accumulated_bytes_ -= data_samples_.front()->size_bytes; + delete data_samples_.front(); + data_samples_.pop_front(); + } else { + break; } + } } -void BitRateStats::Init() -{ - _accumulatedBytes = 0; - while (_dataSamples.size() > 0) - { - delete _dataSamples.front(); - _dataSamples.pop_front(); - } +uint32_t BitRateStats::BitRate(int64_t now_ms) { + // Calculate the average bit rate the past BITRATE_AVERAGE_WINDOW ms. + // Removes any old samples from the list. + EraseOld(now_ms); + return static_cast(accumulated_bytes_ * 8.0f * 1000.0f / + kBitrateAverageWindowMs + 0.5f); } - -void BitRateStats::Update(uint32_t packetSizeBytes, int64_t nowMs) -{ - // Find an empty slot for storing the new sample and at the same time - // accumulate the history. - _dataSamples.push_back(new DataTimeSizeTuple(packetSizeBytes, nowMs)); - _accumulatedBytes += packetSizeBytes; - EraseOld(nowMs); -} - -void BitRateStats::EraseOld(int64_t nowMs) -{ - while (_dataSamples.size() > 0) - { - if (nowMs - _dataSamples.front()->_timeCompleteMs > - kBitrateAverageWindow) - { - // Delete old sample - _accumulatedBytes -= _dataSamples.front()->_sizeBytes; - delete _dataSamples.front(); - _dataSamples.pop_front(); - } - else - { - break; - } - } -} - -uint32_t BitRateStats::BitRate(int64_t nowMs) -{ - // Calculate the average bit rate the past BITRATE_AVERAGE_WINDOW ms. - // Removes any old samples from the list. - EraseOld(nowMs); - return static_cast(_accumulatedBytes * 8.0f * 1000.0f / - kBitrateAverageWindow + 0.5f); -} - } // namespace webrtc diff --git a/webrtc/modules/remote_bitrate_estimator/bitrate_estimator.h b/webrtc/modules/remote_bitrate_estimator/bitrate_estimator.h index a2a0efcf2..6e58a7942 100644 --- a/webrtc/modules/remote_bitrate_estimator/bitrate_estimator.h +++ b/webrtc/modules/remote_bitrate_estimator/bitrate_estimator.h @@ -17,34 +17,31 @@ namespace webrtc { -class BitRateStats -{ -public: - BitRateStats(); - ~BitRateStats(); +class BitRateStats { + public: + BitRateStats(); + ~BitRateStats(); - void Init(); - void Update(uint32_t packetSizeBytes, int64_t nowMs); - uint32_t BitRate(int64_t nowMs); + void Init(); + void Update(uint32_t packet_size_bytes, int64_t now_ms); + uint32_t BitRate(int64_t now_ms); -private: - struct DataTimeSizeTuple - { - DataTimeSizeTuple(uint32_t sizeBytes, int64_t timeCompleteMs) - : - _sizeBytes(sizeBytes), - _timeCompleteMs(timeCompleteMs) {} + private: + struct DataTimeSizeTuple { + DataTimeSizeTuple(uint32_t size_bytes_in, int64_t time_complete_ms_in) + : size_bytes(size_bytes_in), + time_complete_ms(time_complete_ms_in) { + } - uint32_t _sizeBytes; - int64_t _timeCompleteMs; - }; + uint32_t size_bytes; + int64_t time_complete_ms; + }; - void EraseOld(int64_t nowMs); + void EraseOld(int64_t now_ms); - std::list _dataSamples; - uint32_t _accumulatedBytes; + std::list data_samples_; + uint32_t accumulated_bytes_; }; - } // namespace webrtc #endif // WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_BITRATE_ESTIMATOR_H_ diff --git a/webrtc/modules/remote_bitrate_estimator/bitrate_estimator_unittest.cc b/webrtc/modules/remote_bitrate_estimator/bitrate_estimator_unittest.cc index ddc9e5998..2dcc603fd 100644 --- a/webrtc/modules/remote_bitrate_estimator/bitrate_estimator_unittest.cc +++ b/webrtc/modules/remote_bitrate_estimator/bitrate_estimator_unittest.cc @@ -8,51 +8,44 @@ * be found in the AUTHORS file in the root of the source tree. */ -/* - * This file includes unit tests for the bitrate estimator. - */ - #include -#include "typedefs.h" -#include "bitrate_estimator.h" +#include "webrtc/modules/remote_bitrate_estimator/bitrate_estimator.h" namespace { using webrtc::BitRateStats; -class BitRateStatsTest : public ::testing::Test -{ -protected: - BitRateStatsTest() {}; - BitRateStats bitRate; +class BitRateStatsTest : public ::testing::Test { + protected: + BitRateStatsTest() {}; + BitRateStats stats_; }; -TEST_F(BitRateStatsTest, TestStrictMode) -{ - int64_t nowMs = 0; - // Should be initialized to 0. - EXPECT_EQ(0u, bitRate.BitRate(nowMs)); - bitRate.Update(1500, nowMs); - // Expecting 24 kbps given a 500 ms window with one 1500 bytes packet. - EXPECT_EQ(24000u, bitRate.BitRate(nowMs)); - bitRate.Init(); - // Expecting 0 after init. - EXPECT_EQ(0u, bitRate.BitRate(nowMs)); - for (int i = 0; i < 100000; ++i) - { - if (nowMs % 10 == 0) - bitRate.Update(1500, nowMs); - // Approximately 1200 kbps expected. Not exact since when packets - // are removed we will jump 10 ms to the next packet. - if (nowMs > 0 && nowMs % 500 == 0) - EXPECT_NEAR(1200000u, bitRate.BitRate(nowMs), 24000u); - nowMs += 1; +TEST_F(BitRateStatsTest, TestStrictMode) { + int64_t now_ms = 0; + // Should be initialized to 0. + EXPECT_EQ(0u, stats_.BitRate(now_ms)); + stats_.Update(1500, now_ms); + // Expecting 24 kbps given a 500 ms window with one 1500 bytes packet. + EXPECT_EQ(24000u, stats_.BitRate(now_ms)); + stats_.Init(); + // Expecting 0 after init. + EXPECT_EQ(0u, stats_.BitRate(now_ms)); + for (int i = 0; i < 100000; ++i) { + if (now_ms % 10 == 0) { + stats_.Update(1500, now_ms); } - nowMs += 500; - // The window is 2 seconds. If nothing has been received for that time - // the estimate should be 0. - EXPECT_EQ(0u, bitRate.BitRate(nowMs)); -} - + // Approximately 1200 kbps expected. Not exact since when packets + // are removed we will jump 10 ms to the next packet. + if (now_ms > 0 && now_ms % 500 == 0) { + EXPECT_NEAR(1200000u, stats_.BitRate(now_ms), 24000u); + } + now_ms += 1; + } + now_ms += 500; + // The window is 2 seconds. If nothing has been received for that time + // the estimate should be 0. + EXPECT_EQ(0u, stats_.BitRate(now_ms)); } +} // namespace