DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
bandwidth_estimator.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/mpd/base/bandwidth_estimator.h"
8 
9 #include <cmath>
10 #include <cstdlib>
11 
12 #include "packager/base/logging.h"
13 
14 const int BandwidthEstimator::kUseAllBlocks = 0;
15 
17  : num_blocks_for_estimation_(num_blocks),
18  harmonic_mean_denominator_(0.0),
19  num_blocks_added_(0) {}
20 BandwidthEstimator::~BandwidthEstimator() {}
21 
22 void BandwidthEstimator::AddBlock(uint64_t size, double duration) {
23  DCHECK_GT(duration, 0.0);
24  DCHECK_GT(size, 0u);
25 
26  if (num_blocks_for_estimation_ < 0 &&
27  static_cast<int>(history_.size()) >= -1 * num_blocks_for_estimation_) {
28  // Short circuiting the case where |num_blocks_for_estimation_| number of
29  // blocks have been added already.
30  return;
31  }
32 
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);
38  ++num_blocks_added_;
39  return;
40  }
41 
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();
46  history_.pop_front();
47  }
48 
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);
52  return;
53 }
54 
55 uint64_t BandwidthEstimator::Estimate() const {
56  if (harmonic_mean_denominator_ == 0.0)
57  return 0;
58 
59  const uint64_t num_blocks = num_blocks_for_estimation_ == kUseAllBlocks
60  ? num_blocks_added_
61  : history_.size();
62  return static_cast<uint64_t>(ceil(num_blocks / harmonic_mean_denominator_));
63 }
BandwidthEstimator(int num_blocks)