7 #include "packager/mpd/base/bandwidth_estimator.h"
12 #include "packager/base/logging.h"
14 const int BandwidthEstimator::kUseAllBlocks = 0;
17 : num_blocks_for_estimation_(num_blocks),
18 harmonic_mean_denominator_(0.0),
19 num_blocks_added_(0) {}
20 BandwidthEstimator::~BandwidthEstimator() {}
22 void BandwidthEstimator::AddBlock(uint64_t size,
double duration) {
23 DCHECK_GT(duration, 0.0);
26 if (num_blocks_for_estimation_ < 0 &&
27 static_cast<int>(history_.size()) >= -1 * num_blocks_for_estimation_) {
33 const int kBitsInByte = 8;
34 const double bits_per_second_reciprocal = duration / (kBitsInByte * size);
35 harmonic_mean_denominator_ += bits_per_second_reciprocal;
36 if (num_blocks_for_estimation_ == kUseAllBlocks) {
37 DCHECK_EQ(history_.size(), 0u);
42 history_.push_back(bits_per_second_reciprocal);
43 if (num_blocks_for_estimation_ > 0 &&
44 static_cast<int>(history_.size()) > num_blocks_for_estimation_) {
45 harmonic_mean_denominator_ -= history_.front();
49 DCHECK_NE(num_blocks_for_estimation_, kUseAllBlocks);
50 DCHECK_LE(static_cast<int>(history_.size()), abs(num_blocks_for_estimation_));
51 DCHECK_EQ(num_blocks_added_, 0u);
55 uint64_t BandwidthEstimator::Estimate()
const {
56 if (harmonic_mean_denominator_ == 0.0)
59 const uint64_t num_blocks = num_blocks_for_estimation_ == kUseAllBlocks
62 return static_cast<uint64_t
>(ceil(num_blocks / harmonic_mean_denominator_));
BandwidthEstimator(int num_blocks)