7 #include "packager/mpd/base/bandwidth_estimator.h" 13 #include "packager/base/logging.h" 17 BandwidthEstimator::BandwidthEstimator() =
default;
19 BandwidthEstimator::~BandwidthEstimator() =
default;
22 if (size_in_bytes == 0 || duration == 0) {
23 LOG(WARNING) <<
"Ignore block with size=" << size_in_bytes
24 <<
", duration=" << duration;
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;
33 const size_t kTargetDurationThreshold = 10;
34 if (initial_blocks_.size() < kTargetDurationThreshold) {
35 initial_blocks_.push_back({size_in_bits, duration});
39 if (target_block_duration_ == 0) {
42 target_block_duration_ = GetAverageBlockDuration();
43 for (
const Block& block : initial_blocks_) {
45 std::max(max_bitrate_, GetBitrate(block, target_block_duration_));
49 max_bitrate_ = std::max(max_bitrate_, GetBitrate({size_in_bits, duration},
50 target_block_duration_));
54 if (total_duration_ == 0)
56 return static_cast<uint64_t
>(ceil(total_size_in_bits_ / total_duration_));
60 if (max_bitrate_ != 0)
65 DCHECK(target_block_duration_ == 0);
66 const double target_block_duration = GetAverageBlockDuration();
69 uint64_t max_bitrate = 0;
70 for (
const Block& block : initial_blocks_) {
72 std::max(max_bitrate, GetBitrate(block, target_block_duration));
77 double BandwidthEstimator::GetAverageBlockDuration()
const {
78 if (initial_blocks_.empty())
81 std::accumulate(initial_blocks_.begin(), initial_blocks_.end(), 0.0,
82 [](
double duration,
const Block& block) {
83 return duration + block.duration;
85 return sum / initial_blocks_.size();
88 uint64_t BandwidthEstimator::GetBitrate(
const Block& block,
89 double target_block_duration)
const {
90 if (block.duration < 0.5 * target_block_duration) {
105 VLOG(1) <<
"Exclude short segment (duration " << block.duration
106 <<
", target_duration " << target_block_duration
107 <<
") in peak bandwidth computation.";
110 return static_cast<uint64_t
>(ceil(block.size_in_bits / block.duration));
All the methods that are virtual are virtual for mocking.
void AddBlock(uint64_t size_in_bytes, double duration)
uint64_t Estimate() const