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