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