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