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:
parent
65f995a3df
commit
d26457fdeb
@ -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::Init()
|
||||
{
|
||||
_accumulatedBytes = 0;
|
||||
while (_dataSamples.size() > 0)
|
||||
{
|
||||
delete _dataSamples.front();
|
||||
_dataSamples.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void BitRateStats::Update(uint32_t packetSizeBytes, int64_t nowMs)
|
||||
{
|
||||
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.
|
||||
_dataSamples.push_back(new DataTimeSizeTuple(packetSizeBytes, nowMs));
|
||||
_accumulatedBytes += packetSizeBytes;
|
||||
EraseOld(nowMs);
|
||||
data_samples_.push_back(new DataTimeSizeTuple(packet_size_bytes, now_ms));
|
||||
accumulated_bytes_ += packet_size_bytes;
|
||||
EraseOld(now_ms);
|
||||
}
|
||||
|
||||
void BitRateStats::EraseOld(int64_t nowMs)
|
||||
{
|
||||
while (_dataSamples.size() > 0)
|
||||
{
|
||||
if (nowMs - _dataSamples.front()->_timeCompleteMs >
|
||||
kBitrateAverageWindow)
|
||||
{
|
||||
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
|
||||
_accumulatedBytes -= _dataSamples.front()->_sizeBytes;
|
||||
delete _dataSamples.front();
|
||||
_dataSamples.pop_front();
|
||||
}
|
||||
else
|
||||
{
|
||||
accumulated_bytes_ -= data_samples_.front()->size_bytes;
|
||||
delete data_samples_.front();
|
||||
data_samples_.pop_front();
|
||||
} else {
|
||||
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.
|
||||
// Removes any old samples from the list.
|
||||
EraseOld(nowMs);
|
||||
return static_cast<uint32_t>(_accumulatedBytes * 8.0f * 1000.0f /
|
||||
kBitrateAverageWindow + 0.5f);
|
||||
EraseOld(now_ms);
|
||||
return static_cast<uint32_t>(accumulated_bytes_ * 8.0f * 1000.0f /
|
||||
kBitrateAverageWindowMs + 0.5f);
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
@ -17,34 +17,31 @@
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
class BitRateStats
|
||||
{
|
||||
public:
|
||||
class BitRateStats {
|
||||
public:
|
||||
BitRateStats();
|
||||
~BitRateStats();
|
||||
|
||||
void Init();
|
||||
void Update(uint32_t packetSizeBytes, int64_t nowMs);
|
||||
uint32_t BitRate(int64_t nowMs);
|
||||
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<DataTimeSizeTuple*> _dataSamples;
|
||||
uint32_t _accumulatedBytes;
|
||||
std::list<DataTimeSizeTuple*> data_samples_;
|
||||
uint32_t accumulated_bytes_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_BITRATE_ESTIMATOR_H_
|
||||
|
@ -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 <gtest/gtest.h>
|
||||
|
||||
#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:
|
||||
class BitRateStatsTest : public ::testing::Test {
|
||||
protected:
|
||||
BitRateStatsTest() {};
|
||||
BitRateStats bitRate;
|
||||
BitRateStats stats_;
|
||||
};
|
||||
|
||||
TEST_F(BitRateStatsTest, TestStrictMode)
|
||||
{
|
||||
int64_t nowMs = 0;
|
||||
TEST_F(BitRateStatsTest, TestStrictMode) {
|
||||
int64_t now_ms = 0;
|
||||
// Should be initialized to 0.
|
||||
EXPECT_EQ(0u, bitRate.BitRate(nowMs));
|
||||
bitRate.Update(1500, nowMs);
|
||||
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, bitRate.BitRate(nowMs));
|
||||
bitRate.Init();
|
||||
EXPECT_EQ(24000u, stats_.BitRate(now_ms));
|
||||
stats_.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);
|
||||
EXPECT_EQ(0u, stats_.BitRate(now_ms));
|
||||
for (int i = 0; i < 100000; ++i) {
|
||||
if (now_ms % 10 == 0) {
|
||||
stats_.Update(1500, now_ms);
|
||||
}
|
||||
// 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;
|
||||
if (now_ms > 0 && now_ms % 500 == 0) {
|
||||
EXPECT_NEAR(1200000u, stats_.BitRate(now_ms), 24000u);
|
||||
}
|
||||
nowMs += 500;
|
||||
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, bitRate.BitRate(nowMs));
|
||||
}
|
||||
|
||||
EXPECT_EQ(0u, stats_.BitRate(now_ms));
|
||||
}
|
||||
} // namespace
|
||||
|
Loading…
x
Reference in New Issue
Block a user