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