Shaka Packager SDK
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 <algorithm>
10 #include <cmath>
11 
12 #include "packager/base/logging.h"
13 
14 namespace shaka {
15 
16 BandwidthEstimator::BandwidthEstimator() = default;
17 BandwidthEstimator::~BandwidthEstimator() = default;
18 
19 void BandwidthEstimator::AddBlock(uint64_t size_in_bytes, double duration) {
20  if (size_in_bytes == 0 || duration == 0) {
21  LOG(WARNING) << "Ignore block with size=" << size_in_bytes
22  << ", duration=" << duration;
23  return;
24  }
25 
26  const int kBitsInByte = 8;
27  const uint64_t size_in_bits = size_in_bytes * kBitsInByte;
28  total_size_in_bits_ += size_in_bits;
29 
30  total_duration_ += duration;
31 
32  const uint64_t bitrate = static_cast<uint64_t>(ceil(size_in_bits / duration));
33  max_bitrate_ = std::max(bitrate, max_bitrate_);
34 }
35 
36 uint64_t BandwidthEstimator::Estimate() const {
37  if (total_duration_ == 0)
38  return 0;
39  return static_cast<uint64_t>(ceil(total_size_in_bits_ / total_duration_));
40 }
41 
42 uint64_t BandwidthEstimator::Max() const {
43  return max_bitrate_;
44 }
45 
46 } // namespace shaka
All the methods that are virtual are virtual for mocking.
void AddBlock(uint64_t size_in_bytes, double duration)