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 #include <numeric>
12 
13 #include "packager/base/logging.h"
14 
15 namespace shaka {
16 
17 BandwidthEstimator::BandwidthEstimator() = default;
18 
19 BandwidthEstimator::~BandwidthEstimator() = default;
20 
21 void BandwidthEstimator::AddBlock(uint64_t size_in_bytes, double duration) {
22  if (size_in_bytes == 0 || duration == 0) {
23  LOG(WARNING) << "Ignore block with size=" << size_in_bytes
24  << ", duration=" << duration;
25  return;
26  }
27 
28  const int kBitsInByte = 8;
29  const uint64_t size_in_bits = size_in_bytes * kBitsInByte;
30  total_size_in_bits_ += size_in_bits;
31  total_duration_ += duration;
32 
33  const size_t kTargetDurationThreshold = 10;
34  if (initial_blocks_.size() < kTargetDurationThreshold) {
35  initial_blocks_.push_back({size_in_bits, duration});
36  return;
37  }
38 
39  if (target_block_duration_ == 0) {
40  // Use the average duration as the target block duration. It will be used
41  // to filter small blocks from bandwidth calculation.
42  target_block_duration_ = GetAverageBlockDuration();
43  for (const Block& block : initial_blocks_) {
44  max_bitrate_ =
45  std::max(max_bitrate_, GetBitrate(block, target_block_duration_));
46  }
47  return;
48  }
49  max_bitrate_ = std::max(max_bitrate_, GetBitrate({size_in_bits, duration},
50  target_block_duration_));
51 }
52 
53 uint64_t BandwidthEstimator::Estimate() const {
54  if (total_duration_ == 0)
55  return 0;
56  return static_cast<uint64_t>(ceil(total_size_in_bits_ / total_duration_));
57 }
58 
59 uint64_t BandwidthEstimator::Max() const {
60  if (max_bitrate_ != 0)
61  return max_bitrate_;
62 
63  // We don't have the |target_block_duration_| yet. Calculate a target
64  // duration from the current available blocks.
65  DCHECK(target_block_duration_ == 0);
66  const double target_block_duration = GetAverageBlockDuration();
67 
68  // Calculate maximum bitrate with the target duration calculated above.
69  uint64_t max_bitrate = 0;
70  for (const Block& block : initial_blocks_) {
71  max_bitrate =
72  std::max(max_bitrate, GetBitrate(block, target_block_duration));
73  }
74  return max_bitrate;
75 }
76 
77 double BandwidthEstimator::GetAverageBlockDuration() const {
78  if (initial_blocks_.empty())
79  return 0.0;
80  const double sum =
81  std::accumulate(initial_blocks_.begin(), initial_blocks_.end(), 0.0,
82  [](double duration, const Block& block) {
83  return duration + block.duration;
84  });
85  return sum / initial_blocks_.size();
86 }
87 
88 uint64_t BandwidthEstimator::GetBitrate(const Block& block,
89  double target_block_duration) const {
90  if (block.duration < 0.5 * target_block_duration) {
91  // https://tools.ietf.org/html/rfc8216#section-4.1
92  // The peak segment bit rate of a Media Playlist is the largest bit rate of
93  // any continuous set of segments whose total duration is between 0.5
94  // and 1.5 times the target duration.
95  // Only the short segments are excluded here as our media playlist generator
96  // sets the target duration in the playlist to the largest segment duration.
97  // So although the segment duration could be 1.5 times the user provided
98  // segment duration, it will never be larger than the actual target
99  // duration.
100  //
101  // We also apply the same exclusion to the bandwidth computation for DASH as
102  // the bitrate for the short segment is not a good signal for peak
103  // bandwidth.
104  // See https://github.com/google/shaka-packager/issues/498 for details.
105  VLOG(1) << "Exclude short segment (duration " << block.duration
106  << ", target_duration " << target_block_duration
107  << ") in peak bandwidth computation.";
108  return 0.0;
109  }
110  return static_cast<uint64_t>(ceil(block.size_in_bits / block.duration));
111 }
112 
113 } // namespace shaka
shaka::BandwidthEstimator::Max
uint64_t Max() const
Definition: bandwidth_estimator.cc:59
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::BandwidthEstimator::Estimate
uint64_t Estimate() const
Definition: bandwidth_estimator.cc:53
shaka::BandwidthEstimator::AddBlock
void AddBlock(uint64_t size_in_bytes, double duration)
Definition: bandwidth_estimator.cc:21