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::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() uint32_t BitRateStats::BitRate(int64_t now_ms) {
{ // Calculate the average bit rate the past BITRATE_AVERAGE_WINDOW ms.
_accumulatedBytes = 0; // Removes any old samples from the list.
while (_dataSamples.size() > 0) EraseOld(now_ms);
{ return static_cast<uint32_t>(accumulated_bytes_ * 8.0f * 1000.0f /
delete _dataSamples.front(); kBitrateAverageWindowMs + 0.5f);
_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
// 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<uint32_t>(_accumulatedBytes * 8.0f * 1000.0f /
kBitrateAverageWindow + 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 stats_;
BitRateStats bitRate;
}; };
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, stats_.BitRate(now_ms));
EXPECT_EQ(0u, bitRate.BitRate(nowMs)); stats_.Update(1500, now_ms);
bitRate.Update(1500, nowMs); // 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, stats_.BitRate(now_ms));
EXPECT_EQ(24000u, bitRate.BitRate(nowMs)); stats_.Init();
bitRate.Init(); // Expecting 0 after init.
// Expecting 0 after init. EXPECT_EQ(0u, stats_.BitRate(now_ms));
EXPECT_EQ(0u, bitRate.BitRate(nowMs)); for (int i = 0; i < 100000; ++i) {
for (int i = 0; i < 100000; ++i) if (now_ms % 10 == 0) {
{ stats_.Update(1500, now_ms);
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;
} }
nowMs += 500; // Approximately 1200 kbps expected. Not exact since when packets
// The window is 2 seconds. If nothing has been received for that time // are removed we will jump 10 ms to the next packet.
// the estimate should be 0. if (now_ms > 0 && now_ms % 500 == 0) {
EXPECT_EQ(0u, bitRate.BitRate(nowMs)); 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