7 #include <packager/mpd/base/bandwidth_estimator.h>
13 #include <absl/log/check.h>
14 #include <absl/log/log.h>
16 #include <packager/macros/logging.h>
20 BandwidthEstimator::BandwidthEstimator() =
default;
22 BandwidthEstimator::~BandwidthEstimator() =
default;
25 if (size_in_bytes == 0 || duration == 0) {
26 LOG(WARNING) <<
"Ignore block with size=" << size_in_bytes
27 <<
", duration=" << duration;
31 const int kBitsInByte = 8;
32 const uint64_t size_in_bits = size_in_bytes * kBitsInByte;
33 total_size_in_bits_ += size_in_bits;
34 total_duration_ += duration;
36 const size_t kTargetDurationThreshold = 10;
37 if (initial_blocks_.size() < kTargetDurationThreshold) {
38 initial_blocks_.push_back({size_in_bits, duration});
42 if (target_block_duration_ == 0) {
45 target_block_duration_ = GetAverageBlockDuration();
46 for (
const Block& block : initial_blocks_) {
48 std::max(max_bitrate_, GetBitrate(block, target_block_duration_));
52 max_bitrate_ = std::max(max_bitrate_, GetBitrate({size_in_bits, duration},
53 target_block_duration_));
57 if (total_duration_ == 0)
59 return static_cast<uint64_t
>(ceil(total_size_in_bits_ / total_duration_));
63 if (max_bitrate_ != 0)
68 DCHECK(target_block_duration_ == 0);
69 const double target_block_duration = GetAverageBlockDuration();
72 uint64_t max_bitrate = 0;
73 for (
const Block& block : initial_blocks_) {
75 std::max(max_bitrate, GetBitrate(block, target_block_duration));
80 double BandwidthEstimator::GetAverageBlockDuration()
const {
81 if (initial_blocks_.empty())
84 std::accumulate(initial_blocks_.begin(), initial_blocks_.end(), 0.0,
85 [](
double duration,
const Block& block) {
86 return duration + block.duration;
88 return sum / initial_blocks_.size();
91 uint64_t BandwidthEstimator::GetBitrate(
const Block& block,
92 double target_block_duration)
const {
93 if (block.duration < 0.5 * target_block_duration) {
109 VLOG(1) <<
"Exclude short segment (duration " << block.duration
110 <<
", target_duration " << target_block_duration
111 <<
") in peak bandwidth computation.";
114 return static_cast<uint64_t
>(ceil(block.size_in_bits / block.duration));
void AddBlock(uint64_t size_in_bytes, double duration)
uint64_t Estimate() const
All the methods that are virtual are virtual for mocking.