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