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