stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | * |
| 10 | */ |
| 11 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 12 | #include "call/bitrate_allocator.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 13 | |
| 14 | #include <algorithm> |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 15 | #include <cmath> |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 16 | #include <memory> |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/bitrate_controller/include/bitrate_controller.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/logging.h" |
| 22 | #include "system_wrappers/include/clock.h" |
Ying Wang | a646d30 | 2018-03-02 17:04:11 +0100 | [diff] [blame] | 23 | #include "system_wrappers/include/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "system_wrappers/include/metrics.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 28 | // Allow packets to be transmitted in up to 2 times max video bitrate if the |
| 29 | // bandwidth estimate allows it. |
Ying Wang | a646d30 | 2018-03-02 17:04:11 +0100 | [diff] [blame] | 30 | const uint8_t kTransmissionMaxBitrateMultiplier = 2; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 31 | const int kDefaultBitrateBps = 300000; |
| 32 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 33 | // Require a bitrate increase of max(10%, 20kbps) to resume paused streams. |
| 34 | const double kToggleFactor = 0.1; |
| 35 | const uint32_t kMinToggleBitrateBps = 20000; |
| 36 | |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 37 | const int64_t kBweLogIntervalMs = 5000; |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 42 | RTC_DCHECK_GT(allocated_bitrate, 0); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 43 | if (protection_bitrate == 0) |
| 44 | return 1.0; |
| 45 | |
| 46 | uint32_t media_bitrate = allocated_bitrate - protection_bitrate; |
| 47 | return media_bitrate / static_cast<double>(allocated_bitrate); |
| 48 | } |
| 49 | } // namespace |
| 50 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 51 | BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer) |
| 52 | : limit_observer_(limit_observer), |
Sergey Ulanov | e2b1501 | 2016-11-22 16:08:30 -0800 | [diff] [blame] | 53 | last_bitrate_bps_(0), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 54 | last_non_zero_bitrate_bps_(kDefaultBitrateBps), |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 55 | last_fraction_loss_(0), |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 56 | last_rtt_(0), |
| 57 | num_pause_events_(0), |
| 58 | clock_(Clock::GetRealTimeClock()), |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame] | 59 | last_bwe_log_time_(0), |
| 60 | total_requested_padding_bitrate_(0), |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 61 | total_requested_min_bitrate_(0), |
Sebastian Jansson | 448f4d5 | 2018-04-04 14:52:07 +0200 | [diff] [blame] | 62 | total_requested_max_bitrate_(0), |
Sebastian Jansson | 35fa280 | 2018-10-01 09:16:12 +0200 | [diff] [blame] | 63 | allocated_without_feedback_(0), |
Sebastian Jansson | 29b204e | 2018-03-21 12:45:27 +0100 | [diff] [blame] | 64 | has_packet_feedback_(false), |
Ying Wang | a646d30 | 2018-03-02 17:04:11 +0100 | [diff] [blame] | 65 | bitrate_allocation_strategy_(nullptr), |
| 66 | transmission_max_bitrate_multiplier_( |
| 67 | GetTransmissionMaxBitrateMultiplier()) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 68 | sequenced_checker_.Detach(); |
| 69 | } |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 70 | |
| 71 | BitrateAllocator::~BitrateAllocator() { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 72 | RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents", |
| 73 | num_pause_events_); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 74 | } |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 75 | |
Niels Möller | 74e5f80 | 2018-04-25 14:03:46 +0200 | [diff] [blame] | 76 | // static |
Ying Wang | a646d30 | 2018-03-02 17:04:11 +0100 | [diff] [blame] | 77 | uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() { |
| 78 | uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName( |
| 79 | "WebRTC-TransmissionMaxBitrateMultiplier") |
| 80 | .c_str(), |
| 81 | nullptr, 10); |
| 82 | if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) { |
Ying Wang | 012b7e7 | 2018-03-05 15:44:23 +0100 | [diff] [blame] | 83 | RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to " |
| 84 | << multiplier; |
Ying Wang | a646d30 | 2018-03-02 17:04:11 +0100 | [diff] [blame] | 85 | return static_cast<uint8_t>(multiplier); |
| 86 | } |
| 87 | return kTransmissionMaxBitrateMultiplier; |
| 88 | } |
| 89 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 90 | void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, |
| 91 | uint8_t fraction_loss, |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 92 | int64_t rtt, |
minyue | 93e4522 | 2017-05-18 14:32:41 -0700 | [diff] [blame] | 93 | int64_t bwe_period_ms) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 94 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 95 | last_bitrate_bps_ = target_bitrate_bps; |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 96 | last_non_zero_bitrate_bps_ = |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 97 | target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 98 | last_fraction_loss_ = fraction_loss; |
| 99 | last_rtt_ = rtt; |
minyue | 93e4522 | 2017-05-18 14:32:41 -0700 | [diff] [blame] | 100 | last_bwe_period_ms_ = bwe_period_ms; |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 101 | |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 102 | // Periodically log the incoming BWE. |
| 103 | int64_t now = clock_->TimeInMilliseconds(); |
| 104 | if (now > last_bwe_log_time_ + kBweLogIntervalMs) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 105 | RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps; |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 106 | last_bwe_log_time_ = now; |
sprang | 2f48d94 | 2015-11-05 04:25:49 -0800 | [diff] [blame] | 107 | } |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 108 | |
| 109 | ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps); |
| 110 | |
| 111 | for (auto& config : bitrate_observer_configs_) { |
| 112 | uint32_t allocated_bitrate = allocation[config.observer]; |
| 113 | uint32_t protection_bitrate = config.observer->OnBitrateUpdated( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | allocated_bitrate, last_fraction_loss_, last_rtt_, last_bwe_period_ms_); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 115 | |
| 116 | if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) { |
| 117 | if (target_bitrate_bps > 0) |
| 118 | ++num_pause_events_; |
| 119 | // The protection bitrate is an estimate based on the ratio between media |
| 120 | // and protection used before this observer was muted. |
| 121 | uint32_t predicted_protection_bps = |
| 122 | (1.0 - config.media_ratio) * config.min_bitrate_bps; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 123 | RTC_LOG(LS_INFO) << "Pausing observer " << config.observer |
| 124 | << " with configured min bitrate " |
| 125 | << config.min_bitrate_bps << " and current estimate of " |
| 126 | << target_bitrate_bps << " and protection bitrate " |
| 127 | << predicted_protection_bps; |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 128 | } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) { |
| 129 | if (target_bitrate_bps > 0) |
| 130 | ++num_pause_events_; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 131 | RTC_LOG(LS_INFO) << "Resuming observer " << config.observer |
| 132 | << ", configured min bitrate " << config.min_bitrate_bps |
| 133 | << ", current allocation " << allocated_bitrate |
| 134 | << " and protection bitrate " << protection_bitrate; |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Only update the media ratio if the observer got an allocation. |
| 138 | if (allocated_bitrate > 0) |
| 139 | config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); |
| 140 | config.allocated_bitrate_bps = allocated_bitrate; |
| 141 | } |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame] | 142 | UpdateAllocationLimits(); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 143 | } |
| 144 | |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 145 | void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, |
Sebastian Jansson | 24ad720 | 2018-04-19 08:25:12 +0200 | [diff] [blame] | 146 | MediaStreamAllocationConfig config) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 147 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
Sebastian Jansson | 24ad720 | 2018-04-19 08:25:12 +0200 | [diff] [blame] | 148 | RTC_DCHECK_GT(config.bitrate_priority, 0); |
| 149 | RTC_DCHECK(std::isnormal(config.bitrate_priority)); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 150 | auto it = FindObserverConfig(observer); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 151 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 152 | // Update settings if the observer already exists, create a new one otherwise. |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 153 | if (it != bitrate_observer_configs_.end()) { |
Sebastian Jansson | 24ad720 | 2018-04-19 08:25:12 +0200 | [diff] [blame] | 154 | it->min_bitrate_bps = config.min_bitrate_bps; |
| 155 | it->max_bitrate_bps = config.max_bitrate_bps; |
| 156 | it->pad_up_bitrate_bps = config.pad_up_bitrate_bps; |
| 157 | it->enforce_min_bitrate = config.enforce_min_bitrate; |
| 158 | it->bitrate_priority = config.bitrate_priority; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 159 | } else { |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 160 | bitrate_observer_configs_.push_back(ObserverConfig( |
Sebastian Jansson | 24ad720 | 2018-04-19 08:25:12 +0200 | [diff] [blame] | 161 | observer, config.min_bitrate_bps, config.max_bitrate_bps, |
| 162 | config.pad_up_bitrate_bps, config.enforce_min_bitrate, config.track_id, |
| 163 | config.bitrate_priority, config.has_packet_feedback)); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 164 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 165 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 166 | ObserverAllocation allocation; |
| 167 | if (last_bitrate_bps_ > 0) { |
| 168 | // Calculate a new allocation and update all observers. |
| 169 | allocation = AllocateBitrates(last_bitrate_bps_); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 170 | for (auto& config : bitrate_observer_configs_) { |
| 171 | uint32_t allocated_bitrate = allocation[config.observer]; |
| 172 | uint32_t protection_bitrate = config.observer->OnBitrateUpdated( |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 173 | allocated_bitrate, last_fraction_loss_, last_rtt_, |
minyue | 93e4522 | 2017-05-18 14:32:41 -0700 | [diff] [blame] | 174 | last_bwe_period_ms_); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 175 | config.allocated_bitrate_bps = allocated_bitrate; |
| 176 | if (allocated_bitrate > 0) |
| 177 | config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); |
| 178 | } |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 179 | } else { |
| 180 | // Currently, an encoder is not allowed to produce frames. |
| 181 | // But we still have to return the initial config bitrate + let the |
| 182 | // observer know that it can not produce frames. |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 183 | allocation = AllocateBitrates(last_non_zero_bitrate_bps_); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 184 | observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_, |
minyue | 93e4522 | 2017-05-18 14:32:41 -0700 | [diff] [blame] | 185 | last_bwe_period_ms_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 186 | } |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 187 | UpdateAllocationLimits(); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 188 | } |
| 189 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 190 | void BitrateAllocator::UpdateAllocationLimits() { |
| 191 | uint32_t total_requested_padding_bitrate = 0; |
| 192 | uint32_t total_requested_min_bitrate = 0; |
Sebastian Jansson | 448f4d5 | 2018-04-04 14:52:07 +0200 | [diff] [blame] | 193 | uint32_t total_requested_max_bitrate = 0; |
Sebastian Jansson | 29b204e | 2018-03-21 12:45:27 +0100 | [diff] [blame] | 194 | bool has_packet_feedback = false; |
Sebastian Jansson | 35fa280 | 2018-10-01 09:16:12 +0200 | [diff] [blame] | 195 | uint32_t allocated_without_feedback = 0; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 196 | for (const auto& config : bitrate_observer_configs_) { |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame] | 197 | uint32_t stream_padding = config.pad_up_bitrate_bps; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 198 | if (config.enforce_min_bitrate) { |
| 199 | total_requested_min_bitrate += config.min_bitrate_bps; |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame] | 200 | } else if (config.allocated_bitrate_bps == 0) { |
| 201 | stream_padding = |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 202 | std::max(config.MinBitrateWithHysteresis(), stream_padding); |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 203 | } |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame] | 204 | total_requested_padding_bitrate += stream_padding; |
Sebastian Jansson | 448f4d5 | 2018-04-04 14:52:07 +0200 | [diff] [blame] | 205 | total_requested_max_bitrate += config.max_bitrate_bps; |
Sebastian Jansson | 29b204e | 2018-03-21 12:45:27 +0100 | [diff] [blame] | 206 | if (config.allocated_bitrate_bps > 0 && config.has_packet_feedback) |
| 207 | has_packet_feedback = true; |
Sebastian Jansson | 35fa280 | 2018-10-01 09:16:12 +0200 | [diff] [blame] | 208 | // TODO(srte): Remove field trial check. |
| 209 | if (!config.has_packet_feedback && |
| 210 | field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC")) |
| 211 | allocated_without_feedback += config.allocated_bitrate_bps; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 212 | } |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 213 | |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame] | 214 | if (total_requested_padding_bitrate == total_requested_padding_bitrate_ && |
Sebastian Jansson | 29b204e | 2018-03-21 12:45:27 +0100 | [diff] [blame] | 215 | total_requested_min_bitrate == total_requested_min_bitrate_ && |
Sebastian Jansson | 448f4d5 | 2018-04-04 14:52:07 +0200 | [diff] [blame] | 216 | total_requested_max_bitrate == total_requested_max_bitrate_ && |
Sebastian Jansson | 35fa280 | 2018-10-01 09:16:12 +0200 | [diff] [blame] | 217 | allocated_without_feedback == allocated_without_feedback_ && |
Sebastian Jansson | 29b204e | 2018-03-21 12:45:27 +0100 | [diff] [blame] | 218 | has_packet_feedback == has_packet_feedback_) { |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
| 222 | total_requested_min_bitrate_ = total_requested_min_bitrate; |
| 223 | total_requested_padding_bitrate_ = total_requested_padding_bitrate; |
Sebastian Jansson | 448f4d5 | 2018-04-04 14:52:07 +0200 | [diff] [blame] | 224 | total_requested_max_bitrate_ = total_requested_max_bitrate; |
Sebastian Jansson | 35fa280 | 2018-10-01 09:16:12 +0200 | [diff] [blame] | 225 | allocated_without_feedback_ = allocated_without_feedback; |
Sebastian Jansson | 29b204e | 2018-03-21 12:45:27 +0100 | [diff] [blame] | 226 | has_packet_feedback_ = has_packet_feedback; |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame] | 227 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 228 | RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: " |
| 229 | << total_requested_min_bitrate |
| 230 | << "bps, total_requested_padding_bitrate: " |
Sebastian Jansson | 448f4d5 | 2018-04-04 14:52:07 +0200 | [diff] [blame] | 231 | << total_requested_padding_bitrate |
| 232 | << "bps, total_requested_max_bitrate: " |
| 233 | << total_requested_max_bitrate << "bps"; |
Sebastian Jansson | 29b204e | 2018-03-21 12:45:27 +0100 | [diff] [blame] | 234 | limit_observer_->OnAllocationLimitsChanged( |
| 235 | total_requested_min_bitrate, total_requested_padding_bitrate, |
Sebastian Jansson | 35fa280 | 2018-10-01 09:16:12 +0200 | [diff] [blame] | 236 | total_requested_max_bitrate, allocated_without_feedback, |
| 237 | has_packet_feedback); |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 241 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 242 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 243 | auto it = FindObserverConfig(observer); |
| 244 | if (it != bitrate_observer_configs_.end()) { |
| 245 | bitrate_observer_configs_.erase(it); |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 246 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 247 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 248 | UpdateAllocationLimits(); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 251 | int BitrateAllocator::GetStartBitrate( |
| 252 | BitrateAllocatorObserver* observer) const { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 253 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 254 | const auto& it = FindObserverConfig(observer); |
| 255 | if (it == bitrate_observer_configs_.end()) { |
| 256 | // This observer hasn't been added yet, just give it its fair share. |
| 257 | return last_non_zero_bitrate_bps_ / |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 258 | static_cast<int>((bitrate_observer_configs_.size() + 1)); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 259 | } else if (it->allocated_bitrate_bps == -1) { |
| 260 | // This observer hasn't received an allocation yet, so do the same. |
| 261 | return last_non_zero_bitrate_bps_ / |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 262 | static_cast<int>(bitrate_observer_configs_.size()); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 263 | } else { |
| 264 | // This observer already has an allocation. |
| 265 | return it->allocated_bitrate_bps; |
| 266 | } |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 269 | void BitrateAllocator::SetBitrateAllocationStrategy( |
| 270 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 271 | bitrate_allocation_strategy) { |
| 272 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 273 | bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy); |
| 274 | } |
| 275 | |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 276 | BitrateAllocator::ObserverConfigs::const_iterator |
| 277 | BitrateAllocator::FindObserverConfig( |
| 278 | const BitrateAllocatorObserver* observer) const { |
| 279 | for (auto it = bitrate_observer_configs_.begin(); |
| 280 | it != bitrate_observer_configs_.end(); ++it) { |
| 281 | if (it->observer == observer) |
| 282 | return it; |
| 283 | } |
| 284 | return bitrate_observer_configs_.end(); |
| 285 | } |
| 286 | |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 287 | BitrateAllocator::ObserverConfigs::iterator |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 288 | BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 289 | for (auto it = bitrate_observer_configs_.begin(); |
| 290 | it != bitrate_observer_configs_.end(); ++it) { |
| 291 | if (it->observer == observer) |
| 292 | return it; |
| 293 | } |
| 294 | return bitrate_observer_configs_.end(); |
| 295 | } |
| 296 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 297 | BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates( |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 298 | uint32_t bitrate) const { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 299 | if (bitrate_observer_configs_.empty()) |
| 300 | return ObserverAllocation(); |
| 301 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 302 | if (bitrate_allocation_strategy_ != nullptr) { |
| 303 | std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*> |
| 304 | track_configs(bitrate_observer_configs_.size()); |
| 305 | int i = 0; |
| 306 | for (const auto& c : bitrate_observer_configs_) { |
| 307 | track_configs[i++] = &c; |
| 308 | } |
| 309 | std::vector<uint32_t> track_allocations = |
| 310 | bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs); |
| 311 | // The strategy should return allocation for all tracks. |
| 312 | RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size()); |
| 313 | ObserverAllocation allocation; |
| 314 | auto track_allocations_it = track_allocations.begin(); |
| 315 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 316 | allocation[observer_config.observer] = *track_allocations_it++; |
| 317 | } |
| 318 | return allocation; |
| 319 | } |
| 320 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 321 | if (bitrate == 0) |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 322 | return ZeroRateAllocation(); |
| 323 | |
| 324 | uint32_t sum_min_bitrates = 0; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 325 | uint32_t sum_max_bitrates = 0; |
| 326 | for (const auto& observer_config : bitrate_observer_configs_) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 327 | sum_min_bitrates += observer_config.min_bitrate_bps; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 328 | sum_max_bitrates += observer_config.max_bitrate_bps; |
| 329 | } |
| 330 | |
| 331 | // Not enough for all observers to get an allocation, allocate according to: |
| 332 | // enforced min bitrate -> allocated bitrate previous round -> restart paused |
| 333 | // streams. |
| 334 | if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates)) |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 335 | return LowRateAllocation(bitrate); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 336 | |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 337 | // All observers will get their min bitrate plus a share of the rest. This |
| 338 | // share is allocated to each observer based on its bitrate_priority. |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 339 | if (bitrate <= sum_max_bitrates) |
| 340 | return NormalRateAllocation(bitrate, sum_min_bitrates); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 341 | |
Ying Wang | a646d30 | 2018-03-02 17:04:11 +0100 | [diff] [blame] | 342 | // All observers will get up to transmission_max_bitrate_multiplier_ x max. |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 343 | return MaxRateAllocation(bitrate, sum_max_bitrates); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 346 | BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() |
| 347 | const { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 348 | ObserverAllocation allocation; |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 349 | for (const auto& observer_config : bitrate_observer_configs_) |
| 350 | allocation[observer_config.observer] = 0; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 351 | return allocation; |
| 352 | } |
| 353 | |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 354 | BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation( |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 355 | uint32_t bitrate) const { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 356 | ObserverAllocation allocation; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 357 | // Start by allocating bitrate to observers enforcing a min bitrate, hence |
| 358 | // remaining_bitrate might turn negative. |
| 359 | int64_t remaining_bitrate = bitrate; |
| 360 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 361 | int32_t allocated_bitrate = 0; |
| 362 | if (observer_config.enforce_min_bitrate) |
| 363 | allocated_bitrate = observer_config.min_bitrate_bps; |
| 364 | |
| 365 | allocation[observer_config.observer] = allocated_bitrate; |
| 366 | remaining_bitrate -= allocated_bitrate; |
| 367 | } |
| 368 | |
| 369 | // Allocate bitrate to all previously active streams. |
| 370 | if (remaining_bitrate > 0) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 371 | for (const auto& observer_config : bitrate_observer_configs_) { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 372 | if (observer_config.enforce_min_bitrate || |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 373 | observer_config.LastAllocatedBitrate() == 0) |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 374 | continue; |
| 375 | |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 376 | uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis(); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 377 | if (remaining_bitrate >= required_bitrate) { |
| 378 | allocation[observer_config.observer] = required_bitrate; |
| 379 | remaining_bitrate -= required_bitrate; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 380 | } |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 383 | |
| 384 | // Allocate bitrate to previously paused streams. |
| 385 | if (remaining_bitrate > 0) { |
| 386 | for (const auto& observer_config : bitrate_observer_configs_) { |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 387 | if (observer_config.LastAllocatedBitrate() != 0) |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 388 | continue; |
| 389 | |
| 390 | // Add a hysteresis to avoid toggling. |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 391 | uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis(); |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 392 | if (remaining_bitrate >= required_bitrate) { |
| 393 | allocation[observer_config.observer] = required_bitrate; |
| 394 | remaining_bitrate -= required_bitrate; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Split a possible remainder evenly on all streams with an allocation. |
| 400 | if (remaining_bitrate > 0) |
| 401 | DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation); |
| 402 | |
| 403 | RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size()); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 404 | return allocation; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 405 | } |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 406 | |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 407 | // Allocates the bitrate based on the bitrate priority of each observer. This |
| 408 | // bitrate priority defines the priority for bitrate to be allocated to that |
| 409 | // observer in relation to other observers. For example with two observers, if |
| 410 | // observer 1 had a bitrate_priority = 1.0, and observer 2 has a |
| 411 | // bitrate_priority = 2.0, the expected behavior is that observer 2 will be |
| 412 | // allocated twice the bitrate as observer 1 above the each observer's |
| 413 | // min_bitrate_bps values, until one of the observers hits its max_bitrate_bps. |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 414 | BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation( |
| 415 | uint32_t bitrate, |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 416 | uint32_t sum_min_bitrates) const { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 417 | ObserverAllocation allocation; |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 418 | ObserverAllocation observers_capacities; |
| 419 | for (const auto& observer_config : bitrate_observer_configs_) { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 420 | allocation[observer_config.observer] = observer_config.min_bitrate_bps; |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 421 | observers_capacities[observer_config.observer] = |
| 422 | observer_config.max_bitrate_bps - observer_config.min_bitrate_bps; |
| 423 | } |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 424 | |
| 425 | bitrate -= sum_min_bitrates; |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 426 | // From the remaining bitrate, allocate a proportional amount to each observer |
| 427 | // above the min bitrate already allocated. |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 428 | if (bitrate > 0) |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 429 | DistributeBitrateRelatively(bitrate, observers_capacities, &allocation); |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 430 | |
| 431 | return allocation; |
| 432 | } |
| 433 | |
| 434 | BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation( |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 435 | uint32_t bitrate, |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 436 | uint32_t sum_max_bitrates) const { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 437 | ObserverAllocation allocation; |
| 438 | |
| 439 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 440 | allocation[observer_config.observer] = observer_config.max_bitrate_bps; |
| 441 | bitrate -= observer_config.max_bitrate_bps; |
| 442 | } |
Ying Wang | a646d30 | 2018-03-02 17:04:11 +0100 | [diff] [blame] | 443 | DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_, |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 444 | &allocation); |
| 445 | return allocation; |
| 446 | } |
| 447 | |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 448 | uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 449 | // Return the configured minimum bitrate for newly added observers, to avoid |
| 450 | // requiring an extra high bitrate for the observer to get an allocated |
| 451 | // bitrate. |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 452 | return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 453 | } |
| 454 | |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 455 | uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const { |
| 456 | uint32_t min_bitrate = min_bitrate_bps; |
| 457 | if (LastAllocatedBitrate() == 0) { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 458 | min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate), |
| 459 | kMinToggleBitrateBps); |
| 460 | } |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 461 | // Account for protection bitrate used by this observer in the previous |
| 462 | // allocation. |
| 463 | // Note: the ratio will only be updated when the stream is active, meaning a |
| 464 | // paused stream won't get any ratio updates. This might lead to waiting a bit |
| 465 | // longer than necessary if the network condition improves, but this is to |
| 466 | // avoid too much toggling. |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 467 | if (media_ratio > 0.0 && media_ratio < 1.0) |
| 468 | min_bitrate += min_bitrate * (1.0 - media_ratio); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 469 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 470 | return min_bitrate; |
| 471 | } |
| 472 | |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 473 | void BitrateAllocator::DistributeBitrateEvenly( |
| 474 | uint32_t bitrate, |
| 475 | bool include_zero_allocations, |
| 476 | int max_multiplier, |
| 477 | ObserverAllocation* allocation) const { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 478 | RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size()); |
| 479 | |
| 480 | ObserverSortingMap list_max_bitrates; |
| 481 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 482 | if (include_zero_allocations || |
| 483 | allocation->at(observer_config.observer) != 0) { |
| 484 | list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>( |
| 485 | observer_config.max_bitrate_bps, &observer_config)); |
| 486 | } |
| 487 | } |
| 488 | auto it = list_max_bitrates.begin(); |
| 489 | while (it != list_max_bitrates.end()) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 490 | RTC_DCHECK_GT(bitrate, 0); |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 491 | uint32_t extra_allocation = |
| 492 | bitrate / static_cast<uint32_t>(list_max_bitrates.size()); |
| 493 | uint32_t total_allocation = |
| 494 | extra_allocation + allocation->at(it->second->observer); |
| 495 | bitrate -= extra_allocation; |
| 496 | if (total_allocation > max_multiplier * it->first) { |
| 497 | // There is more than we can fit for this observer, carry over to the |
| 498 | // remaining observers. |
| 499 | bitrate += total_allocation - max_multiplier * it->first; |
| 500 | total_allocation = max_multiplier * it->first; |
| 501 | } |
| 502 | // Finally, update the allocation for this observer. |
| 503 | allocation->at(it->second->observer) = total_allocation; |
| 504 | it = list_max_bitrates.erase(it); |
| 505 | } |
| 506 | } |
| 507 | |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 508 | bool BitrateAllocator::EnoughBitrateForAllObservers( |
| 509 | uint32_t bitrate, |
| 510 | uint32_t sum_min_bitrates) const { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 511 | if (bitrate < sum_min_bitrates) |
| 512 | return false; |
| 513 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 514 | uint32_t extra_bitrate_per_observer = |
| 515 | (bitrate - sum_min_bitrates) / |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 516 | static_cast<uint32_t>(bitrate_observer_configs_.size()); |
| 517 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 518 | if (observer_config.min_bitrate_bps + extra_bitrate_per_observer < |
srte | 1eb051c | 2017-11-29 11:23:59 +0100 | [diff] [blame] | 519 | observer_config.MinBitrateWithHysteresis()) { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 520 | return false; |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame] | 521 | } |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 522 | } |
| 523 | return true; |
| 524 | } |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 525 | |
| 526 | void BitrateAllocator::DistributeBitrateRelatively( |
| 527 | uint32_t remaining_bitrate, |
| 528 | const ObserverAllocation& observers_capacities, |
Sebastian Jansson | 44a262a | 2018-10-24 16:07:20 +0200 | [diff] [blame^] | 529 | ObserverAllocation* allocation) const { |
Seth Hampson | fe73d6a | 2017-11-14 10:49:06 -0800 | [diff] [blame] | 530 | RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size()); |
| 531 | RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size()); |
| 532 | |
| 533 | struct PriorityRateObserverConfig { |
| 534 | PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key, |
| 535 | uint32_t capacity_bps, |
| 536 | double bitrate_priority) |
| 537 | : allocation_key(allocation_key), |
| 538 | capacity_bps(capacity_bps), |
| 539 | bitrate_priority(bitrate_priority) {} |
| 540 | |
| 541 | BitrateAllocatorObserver* allocation_key; |
| 542 | // The amount of bitrate bps that can be allocated to this observer. |
| 543 | uint32_t capacity_bps; |
| 544 | double bitrate_priority; |
| 545 | |
| 546 | // We want to sort by which observers will be allocated their full capacity |
| 547 | // first. By dividing each observer's capacity by its bitrate priority we |
| 548 | // are "normalizing" the capacity of an observer by the rate it will be |
| 549 | // filled. This is because the amount allocated is based upon bitrate |
| 550 | // priority. We allocate twice as much bitrate to an observer with twice the |
| 551 | // bitrate priority of another. |
| 552 | bool operator<(const PriorityRateObserverConfig& other) const { |
| 553 | return capacity_bps / bitrate_priority < |
| 554 | other.capacity_bps / other.bitrate_priority; |
| 555 | } |
| 556 | }; |
| 557 | |
| 558 | double bitrate_priority_sum = 0; |
| 559 | std::vector<PriorityRateObserverConfig> priority_rate_observers; |
| 560 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 561 | uint32_t capacity_bps = observers_capacities.at(observer_config.observer); |
| 562 | priority_rate_observers.emplace_back(observer_config.observer, capacity_bps, |
| 563 | observer_config.bitrate_priority); |
| 564 | bitrate_priority_sum += observer_config.bitrate_priority; |
| 565 | } |
| 566 | |
| 567 | // Iterate in the order observers can be allocated their full capacity. |
| 568 | std::sort(priority_rate_observers.begin(), priority_rate_observers.end()); |
| 569 | size_t i; |
| 570 | for (i = 0; i < priority_rate_observers.size(); ++i) { |
| 571 | const auto& priority_rate_observer = priority_rate_observers[i]; |
| 572 | // We allocate the full capacity to an observer only if its relative |
| 573 | // portion from the remaining bitrate is sufficient to allocate its full |
| 574 | // capacity. This means we aren't greedily allocating the full capacity, but |
| 575 | // that it is only done when there is also enough bitrate to allocate the |
| 576 | // proportional amounts to all other observers. |
| 577 | double observer_share = |
| 578 | priority_rate_observer.bitrate_priority / bitrate_priority_sum; |
| 579 | double allocation_bps = observer_share * remaining_bitrate; |
| 580 | bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps; |
| 581 | if (!enough_bitrate) |
| 582 | break; |
| 583 | allocation->at(priority_rate_observer.allocation_key) += |
| 584 | priority_rate_observer.capacity_bps; |
| 585 | remaining_bitrate -= priority_rate_observer.capacity_bps; |
| 586 | bitrate_priority_sum -= priority_rate_observer.bitrate_priority; |
| 587 | } |
| 588 | |
| 589 | // From the remaining bitrate, allocate the proportional amounts to the |
| 590 | // observers that aren't allocated their max capacity. |
| 591 | for (; i < priority_rate_observers.size(); ++i) { |
| 592 | const auto& priority_rate_observer = priority_rate_observers[i]; |
| 593 | double fraction_allocated = |
| 594 | priority_rate_observer.bitrate_priority / bitrate_priority_sum; |
| 595 | allocation->at(priority_rate_observer.allocation_key) += |
| 596 | fraction_allocated * remaining_bitrate; |
| 597 | } |
| 598 | } |
| 599 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 600 | } // namespace webrtc |