Apply Chromium C++ style to BitRateStats.

BUG=

Review URL: https://webrtc-codereview.appspot.com/1325006

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3870 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
solenberg@webrtc.org 2013-04-18 12:25:32 +00:00
parent 65f995a3df
commit d26457fdeb
3 changed files with 90 additions and 115 deletions

View File

@ -8,71 +8,56 @@
* be found in the AUTHORS file in the root of the source tree. * 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 { namespace webrtc {
const float kBitrateAverageWindow = 500.0f; const float kBitrateAverageWindowMs = 500.0f;
BitRateStats::BitRateStats() BitRateStats::BitRateStats()
:_dataSamples(), _accumulatedBytes(0) : data_samples_(),
{ accumulated_bytes_(0) {
} }
BitRateStats::~BitRateStats() BitRateStats::~BitRateStats() {
{ Init();
while (_dataSamples.size() > 0) }
{
delete _dataSamples.front(); void BitRateStats::Init() {
_dataSamples.pop_front(); accumulated_bytes_ = 0;
while (data_samples_.size() > 0) {
delete data_samples_.front();
data_samples_.pop_front();
} }
} }
void BitRateStats::Init() void BitRateStats::Update(uint32_t packet_size_bytes, int64_t now_ms) {
{
_accumulatedBytes = 0;
while (_dataSamples.size() > 0)
{
delete _dataSamples.front();
_dataSamples.pop_front();
}
}
void BitRateStats::Update(uint32_t packetSizeBytes, int64_t nowMs)
{
// Find an empty slot for storing the new sample and at the same time // Find an empty slot for storing the new sample and at the same time
// accumulate the history. // accumulate the history.
_dataSamples.push_back(new DataTimeSizeTuple(packetSizeBytes, nowMs)); data_samples_.push_back(new DataTimeSizeTuple(packet_size_bytes, now_ms));
_accumulatedBytes += packetSizeBytes; accumulated_bytes_ += packet_size_bytes;
EraseOld(nowMs); EraseOld(now_ms);
} }
void BitRateStats::EraseOld(int64_t nowMs) void BitRateStats::EraseOld(int64_t now_ms) {
{ while (data_samples_.size() > 0) {
while (_dataSamples.size() > 0) if (now_ms - data_samples_.front()->time_complete_ms >
{ kBitrateAverageWindowMs) {
if (nowMs - _dataSamples.front()->_timeCompleteMs >
kBitrateAverageWindow)
{
// Delete old sample // Delete old sample
_accumulatedBytes -= _dataSamples.front()->_sizeBytes; accumulated_bytes_ -= data_samples_.front()->size_bytes;
delete _dataSamples.front(); delete data_samples_.front();
_dataSamples.pop_front(); data_samples_.pop_front();
} } else {
else
{
break; break;
} }
} }
} }
uint32_t BitRateStats::BitRate(int64_t nowMs) uint32_t BitRateStats::BitRate(int64_t now_ms) {
{
// Calculate the average bit rate the past BITRATE_AVERAGE_WINDOW ms. // Calculate the average bit rate the past BITRATE_AVERAGE_WINDOW ms.
// Removes any old samples from the list. // Removes any old samples from the list.
EraseOld(nowMs); EraseOld(now_ms);
return static_cast<uint32_t>(_accumulatedBytes * 8.0f * 1000.0f / return static_cast<uint32_t>(accumulated_bytes_ * 8.0f * 1000.0f /
kBitrateAverageWindow + 0.5f); kBitrateAverageWindowMs + 0.5f);
} }
} // namespace webrtc } // namespace webrtc

View File

@ -17,34 +17,31 @@
namespace webrtc { namespace webrtc {
class BitRateStats class BitRateStats {
{
public: public:
BitRateStats(); BitRateStats();
~BitRateStats(); ~BitRateStats();
void Init(); void Init();
void Update(uint32_t packetSizeBytes, int64_t nowMs); void Update(uint32_t packet_size_bytes, int64_t now_ms);
uint32_t BitRate(int64_t nowMs); uint32_t BitRate(int64_t now_ms);
private: private:
struct DataTimeSizeTuple struct DataTimeSizeTuple {
{ DataTimeSizeTuple(uint32_t size_bytes_in, int64_t time_complete_ms_in)
DataTimeSizeTuple(uint32_t sizeBytes, int64_t timeCompleteMs) : size_bytes(size_bytes_in),
: time_complete_ms(time_complete_ms_in) {
_sizeBytes(sizeBytes), }
_timeCompleteMs(timeCompleteMs) {}
uint32_t _sizeBytes; uint32_t size_bytes;
int64_t _timeCompleteMs; int64_t time_complete_ms;
}; };
void EraseOld(int64_t nowMs); void EraseOld(int64_t now_ms);
std::list<DataTimeSizeTuple*> _dataSamples; std::list<DataTimeSizeTuple*> data_samples_;
uint32_t _accumulatedBytes; uint32_t accumulated_bytes_;
}; };
} // namespace webrtc } // namespace webrtc
#endif // WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_BITRATE_ESTIMATOR_H_ #endif // WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_BITRATE_ESTIMATOR_H_

View File

@ -8,51 +8,44 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
/*
* This file includes unit tests for the bitrate estimator.
*/
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "typedefs.h" #include "webrtc/modules/remote_bitrate_estimator/bitrate_estimator.h"
#include "bitrate_estimator.h"
namespace { namespace {
using webrtc::BitRateStats; using webrtc::BitRateStats;
class BitRateStatsTest : public ::testing::Test class BitRateStatsTest : public ::testing::Test {
{
protected: protected:
BitRateStatsTest() {}; BitRateStatsTest() {};
BitRateStats bitRate; BitRateStats stats_;
}; };
TEST_F(BitRateStatsTest, TestStrictMode) TEST_F(BitRateStatsTest, TestStrictMode) {
{ int64_t now_ms = 0;
int64_t nowMs = 0;
// Should be initialized to 0. // Should be initialized to 0.
EXPECT_EQ(0u, bitRate.BitRate(nowMs)); EXPECT_EQ(0u, stats_.BitRate(now_ms));
bitRate.Update(1500, nowMs); stats_.Update(1500, now_ms);
// Expecting 24 kbps given a 500 ms window with one 1500 bytes packet. // Expecting 24 kbps given a 500 ms window with one 1500 bytes packet.
EXPECT_EQ(24000u, bitRate.BitRate(nowMs)); EXPECT_EQ(24000u, stats_.BitRate(now_ms));
bitRate.Init(); stats_.Init();
// Expecting 0 after init. // Expecting 0 after init.
EXPECT_EQ(0u, bitRate.BitRate(nowMs)); EXPECT_EQ(0u, stats_.BitRate(now_ms));
for (int i = 0; i < 100000; ++i) for (int i = 0; i < 100000; ++i) {
{ if (now_ms % 10 == 0) {
if (nowMs % 10 == 0) stats_.Update(1500, now_ms);
bitRate.Update(1500, nowMs); }
// Approximately 1200 kbps expected. Not exact since when packets // Approximately 1200 kbps expected. Not exact since when packets
// are removed we will jump 10 ms to the next packet. // are removed we will jump 10 ms to the next packet.
if (nowMs > 0 && nowMs % 500 == 0) if (now_ms > 0 && now_ms % 500 == 0) {
EXPECT_NEAR(1200000u, bitRate.BitRate(nowMs), 24000u); EXPECT_NEAR(1200000u, stats_.BitRate(now_ms), 24000u);
nowMs += 1;
} }
nowMs += 500; now_ms += 1;
}
now_ms += 500;
// The window is 2 seconds. If nothing has been received for that time // The window is 2 seconds. If nothing has been received for that time
// the estimate should be 0. // the estimate should be 0.
EXPECT_EQ(0u, bitRate.BitRate(nowMs)); EXPECT_EQ(0u, stats_.BitRate(now_ms));
}
} }
} // namespace