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 | |
mflodman | 0e7e259 | 2015-11-12 21:02:42 -0800 | [diff] [blame] | 12 | #include "webrtc/call/bitrate_allocator.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 13 | |
| 14 | #include <algorithm> |
| 15 | #include <utility> |
| 16 | |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 18 | #include "webrtc/base/logging.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/include/clock.h" |
| 21 | #include "webrtc/system_wrappers/include/metrics.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 25 | // Allow packets to be transmitted in up to 2 times max video bitrate if the |
| 26 | // bandwidth estimate allows it. |
| 27 | const int kTransmissionMaxBitrateMultiplier = 2; |
| 28 | const int kDefaultBitrateBps = 300000; |
| 29 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 30 | // Require a bitrate increase of max(10%, 20kbps) to resume paused streams. |
| 31 | const double kToggleFactor = 0.1; |
| 32 | const uint32_t kMinToggleBitrateBps = 20000; |
| 33 | |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 34 | const int64_t kBweLogIntervalMs = 5000; |
| 35 | |
| 36 | namespace { |
| 37 | |
| 38 | double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) { |
| 39 | RTC_DCHECK_GT(allocated_bitrate, 0u); |
| 40 | if (protection_bitrate == 0) |
| 41 | return 1.0; |
| 42 | |
| 43 | uint32_t media_bitrate = allocated_bitrate - protection_bitrate; |
| 44 | return media_bitrate / static_cast<double>(allocated_bitrate); |
| 45 | } |
| 46 | } // namespace |
| 47 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 48 | BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer) |
| 49 | : limit_observer_(limit_observer), |
| 50 | bitrate_observer_configs_(), |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 51 | last_bitrate_bps_(kDefaultBitrateBps), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 52 | last_non_zero_bitrate_bps_(kDefaultBitrateBps), |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 53 | last_fraction_loss_(0), |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 54 | last_rtt_(0), |
| 55 | num_pause_events_(0), |
| 56 | clock_(Clock::GetRealTimeClock()), |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 57 | last_bwe_log_time_(0) {} |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 58 | |
| 59 | BitrateAllocator::~BitrateAllocator() { |
| 60 | RTC_LOGGED_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents", |
| 61 | num_pause_events_); |
| 62 | } |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 63 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 64 | void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, |
| 65 | uint8_t fraction_loss, |
| 66 | int64_t rtt) { |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 67 | rtc::CritScope lock(&crit_sect_); |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 68 | last_bitrate_bps_ = target_bitrate_bps; |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 69 | last_non_zero_bitrate_bps_ = |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 70 | target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 71 | last_fraction_loss_ = fraction_loss; |
| 72 | last_rtt_ = rtt; |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 73 | |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 74 | // Periodically log the incoming BWE. |
| 75 | int64_t now = clock_->TimeInMilliseconds(); |
| 76 | if (now > last_bwe_log_time_ + kBweLogIntervalMs) { |
| 77 | LOG(LS_INFO) << "Current BWE " << target_bitrate_bps; |
| 78 | last_bwe_log_time_ = now; |
sprang | 2f48d94 | 2015-11-05 04:25:49 -0800 | [diff] [blame] | 79 | } |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 80 | |
| 81 | ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps); |
| 82 | |
| 83 | for (auto& config : bitrate_observer_configs_) { |
| 84 | uint32_t allocated_bitrate = allocation[config.observer]; |
| 85 | uint32_t protection_bitrate = config.observer->OnBitrateUpdated( |
| 86 | allocated_bitrate, last_fraction_loss_, last_rtt_); |
| 87 | |
| 88 | if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) { |
| 89 | if (target_bitrate_bps > 0) |
| 90 | ++num_pause_events_; |
| 91 | // The protection bitrate is an estimate based on the ratio between media |
| 92 | // and protection used before this observer was muted. |
| 93 | uint32_t predicted_protection_bps = |
| 94 | (1.0 - config.media_ratio) * config.min_bitrate_bps; |
| 95 | LOG(LS_INFO) << "Pausing observer " << config.observer |
| 96 | << " with configured min bitrate " << config.min_bitrate_bps |
| 97 | << " and current estimate of " << target_bitrate_bps |
| 98 | << " and protection bitrate " << predicted_protection_bps; |
| 99 | } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) { |
| 100 | if (target_bitrate_bps > 0) |
| 101 | ++num_pause_events_; |
| 102 | LOG(LS_INFO) << "Resuming observer " << config.observer |
| 103 | << ", configured min bitrate " << config.min_bitrate_bps |
| 104 | << ", current allocation " << allocated_bitrate |
| 105 | << " and protection bitrate " << protection_bitrate; |
| 106 | } |
| 107 | |
| 108 | // Only update the media ratio if the observer got an allocation. |
| 109 | if (allocated_bitrate > 0) |
| 110 | config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); |
| 111 | config.allocated_bitrate_bps = allocated_bitrate; |
| 112 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 113 | } |
| 114 | |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 115 | void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, |
| 116 | uint32_t min_bitrate_bps, |
| 117 | uint32_t max_bitrate_bps, |
| 118 | uint32_t pad_up_bitrate_bps, |
| 119 | bool enforce_min_bitrate) { |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 120 | rtc::CritScope lock(&crit_sect_); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 121 | auto it = FindObserverConfig(observer); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 122 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 123 | // Update settings if the observer already exists, create a new one otherwise. |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 124 | if (it != bitrate_observer_configs_.end()) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 125 | it->min_bitrate_bps = min_bitrate_bps; |
| 126 | it->max_bitrate_bps = max_bitrate_bps; |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 127 | it->pad_up_bitrate_bps = pad_up_bitrate_bps; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 128 | it->enforce_min_bitrate = enforce_min_bitrate; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 129 | } else { |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 130 | bitrate_observer_configs_.push_back( |
| 131 | ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps, |
| 132 | pad_up_bitrate_bps, enforce_min_bitrate)); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 133 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 134 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 135 | ObserverAllocation allocation; |
| 136 | if (last_bitrate_bps_ > 0) { |
| 137 | // Calculate a new allocation and update all observers. |
| 138 | allocation = AllocateBitrates(last_bitrate_bps_); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 139 | for (auto& config : bitrate_observer_configs_) { |
| 140 | uint32_t allocated_bitrate = allocation[config.observer]; |
| 141 | uint32_t protection_bitrate = config.observer->OnBitrateUpdated( |
| 142 | allocated_bitrate, last_fraction_loss_, last_rtt_); |
| 143 | config.allocated_bitrate_bps = allocated_bitrate; |
| 144 | if (allocated_bitrate > 0) |
| 145 | config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); |
| 146 | } |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 147 | } else { |
| 148 | // Currently, an encoder is not allowed to produce frames. |
| 149 | // But we still have to return the initial config bitrate + let the |
| 150 | // observer know that it can not produce frames. |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 151 | allocation = AllocateBitrates(last_non_zero_bitrate_bps_); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 152 | observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 153 | } |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 154 | UpdateAllocationLimits(); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 155 | } |
| 156 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 157 | void BitrateAllocator::UpdateAllocationLimits() { |
| 158 | uint32_t total_requested_padding_bitrate = 0; |
| 159 | uint32_t total_requested_min_bitrate = 0; |
| 160 | |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 161 | { |
| 162 | rtc::CritScope lock(&crit_sect_); |
| 163 | for (const auto& config : bitrate_observer_configs_) { |
| 164 | if (config.enforce_min_bitrate) { |
| 165 | total_requested_min_bitrate += config.min_bitrate_bps; |
| 166 | } |
| 167 | total_requested_padding_bitrate += config.pad_up_bitrate_bps; |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 168 | } |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 169 | } |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 170 | |
perkj | 9b522f8 | 2016-07-07 00:36:28 -0700 | [diff] [blame] | 171 | LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: " |
| 172 | << total_requested_min_bitrate |
| 173 | << "bps, total_requested_padding_bitrate: " |
| 174 | << total_requested_padding_bitrate << "bps"; |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 175 | limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate, |
| 176 | total_requested_padding_bitrate); |
| 177 | } |
| 178 | |
| 179 | void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 180 | { |
| 181 | rtc::CritScope lock(&crit_sect_); |
| 182 | auto it = FindObserverConfig(observer); |
| 183 | if (it != bitrate_observer_configs_.end()) { |
| 184 | bitrate_observer_configs_.erase(it); |
| 185 | } |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 186 | } |
| 187 | UpdateAllocationLimits(); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 188 | } |
| 189 | |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 190 | int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) { |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 191 | rtc::CritScope lock(&crit_sect_); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 192 | const auto& it = FindObserverConfig(observer); |
| 193 | if (it == bitrate_observer_configs_.end()) { |
| 194 | // This observer hasn't been added yet, just give it its fair share. |
| 195 | return last_non_zero_bitrate_bps_ / |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 196 | static_cast<int>((bitrate_observer_configs_.size() + 1)); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 197 | } else if (it->allocated_bitrate_bps == -1) { |
| 198 | // This observer hasn't received an allocation yet, so do the same. |
| 199 | return last_non_zero_bitrate_bps_ / |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 200 | static_cast<int>(bitrate_observer_configs_.size()); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 201 | } else { |
| 202 | // This observer already has an allocation. |
| 203 | return it->allocated_bitrate_bps; |
| 204 | } |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 205 | } |
| 206 | |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 207 | BitrateAllocator::ObserverConfigs::iterator |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 208 | BitrateAllocator::FindObserverConfig( |
| 209 | const BitrateAllocatorObserver* observer) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 210 | for (auto it = bitrate_observer_configs_.begin(); |
| 211 | it != bitrate_observer_configs_.end(); ++it) { |
| 212 | if (it->observer == observer) |
| 213 | return it; |
| 214 | } |
| 215 | return bitrate_observer_configs_.end(); |
| 216 | } |
| 217 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 218 | BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates( |
| 219 | uint32_t bitrate) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 220 | if (bitrate_observer_configs_.empty()) |
| 221 | return ObserverAllocation(); |
| 222 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 223 | if (bitrate == 0) |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 224 | return ZeroRateAllocation(); |
| 225 | |
| 226 | uint32_t sum_min_bitrates = 0; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 227 | uint32_t sum_max_bitrates = 0; |
| 228 | for (const auto& observer_config : bitrate_observer_configs_) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 229 | sum_min_bitrates += observer_config.min_bitrate_bps; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 230 | sum_max_bitrates += observer_config.max_bitrate_bps; |
| 231 | } |
| 232 | |
| 233 | // Not enough for all observers to get an allocation, allocate according to: |
| 234 | // enforced min bitrate -> allocated bitrate previous round -> restart paused |
| 235 | // streams. |
| 236 | if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates)) |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 237 | return LowRateAllocation(bitrate); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 238 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 239 | // All observers will get their min bitrate plus an even share of the rest. |
| 240 | if (bitrate <= sum_max_bitrates) |
| 241 | return NormalRateAllocation(bitrate, sum_min_bitrates); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 242 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 243 | // All observers will get up to kTransmissionMaxBitrateMultiplier x max. |
| 244 | return MaxRateAllocation(bitrate, sum_max_bitrates); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 245 | } |
| 246 | |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 247 | BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() { |
| 248 | ObserverAllocation allocation; |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 249 | for (const auto& observer_config : bitrate_observer_configs_) |
| 250 | allocation[observer_config.observer] = 0; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 251 | return allocation; |
| 252 | } |
| 253 | |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 254 | BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation( |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 255 | uint32_t bitrate) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 256 | ObserverAllocation allocation; |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 257 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 258 | // Start by allocating bitrate to observers enforcing a min bitrate, hence |
| 259 | // remaining_bitrate might turn negative. |
| 260 | int64_t remaining_bitrate = bitrate; |
| 261 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 262 | int32_t allocated_bitrate = 0; |
| 263 | if (observer_config.enforce_min_bitrate) |
| 264 | allocated_bitrate = observer_config.min_bitrate_bps; |
| 265 | |
| 266 | allocation[observer_config.observer] = allocated_bitrate; |
| 267 | remaining_bitrate -= allocated_bitrate; |
| 268 | } |
| 269 | |
| 270 | // Allocate bitrate to all previously active streams. |
| 271 | if (remaining_bitrate > 0) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 272 | for (const auto& observer_config : bitrate_observer_configs_) { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 273 | if (observer_config.enforce_min_bitrate || |
| 274 | LastAllocatedBitrate(observer_config) == 0) |
| 275 | continue; |
| 276 | |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 277 | uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config); |
| 278 | if (remaining_bitrate >= required_bitrate) { |
| 279 | allocation[observer_config.observer] = required_bitrate; |
| 280 | remaining_bitrate -= required_bitrate; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 281 | } |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 284 | |
| 285 | // Allocate bitrate to previously paused streams. |
| 286 | if (remaining_bitrate > 0) { |
| 287 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 288 | if (LastAllocatedBitrate(observer_config) != 0) |
| 289 | continue; |
| 290 | |
| 291 | // Add a hysteresis to avoid toggling. |
| 292 | uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config); |
| 293 | if (remaining_bitrate >= required_bitrate) { |
| 294 | allocation[observer_config.observer] = required_bitrate; |
| 295 | remaining_bitrate -= required_bitrate; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // Split a possible remainder evenly on all streams with an allocation. |
| 301 | if (remaining_bitrate > 0) |
| 302 | DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation); |
| 303 | |
| 304 | RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size()); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 305 | return allocation; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 306 | } |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 307 | |
| 308 | BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation( |
| 309 | uint32_t bitrate, |
| 310 | uint32_t sum_min_bitrates) { |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 311 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 312 | ObserverAllocation allocation; |
| 313 | for (const auto& observer_config : bitrate_observer_configs_) |
| 314 | allocation[observer_config.observer] = observer_config.min_bitrate_bps; |
| 315 | |
| 316 | bitrate -= sum_min_bitrates; |
| 317 | if (bitrate > 0) |
| 318 | DistributeBitrateEvenly(bitrate, true, 1, &allocation); |
| 319 | |
| 320 | return allocation; |
| 321 | } |
| 322 | |
| 323 | BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation( |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 324 | uint32_t bitrate, uint32_t sum_max_bitrates) { |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 325 | ObserverAllocation allocation; |
| 326 | |
| 327 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 328 | allocation[observer_config.observer] = observer_config.max_bitrate_bps; |
| 329 | bitrate -= observer_config.max_bitrate_bps; |
| 330 | } |
| 331 | DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier, |
| 332 | &allocation); |
| 333 | return allocation; |
| 334 | } |
| 335 | |
| 336 | uint32_t BitrateAllocator::LastAllocatedBitrate( |
| 337 | const ObserverConfig& observer_config) { |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 338 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 339 | // Return the configured minimum bitrate for newly added observers, to avoid |
| 340 | // requiring an extra high bitrate for the observer to get an allocated |
| 341 | // bitrate. |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 342 | return observer_config.allocated_bitrate_bps == -1 ? |
| 343 | observer_config.min_bitrate_bps : observer_config.allocated_bitrate_bps; |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | uint32_t BitrateAllocator::MinBitrateWithHysteresis( |
| 347 | const ObserverConfig& observer_config) { |
| 348 | uint32_t min_bitrate = observer_config.min_bitrate_bps; |
| 349 | if (LastAllocatedBitrate(observer_config) == 0) { |
| 350 | min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate), |
| 351 | kMinToggleBitrateBps); |
| 352 | } |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 353 | // Account for protection bitrate used by this observer in the previous |
| 354 | // allocation. |
| 355 | // Note: the ratio will only be updated when the stream is active, meaning a |
| 356 | // paused stream won't get any ratio updates. This might lead to waiting a bit |
| 357 | // longer than necessary if the network condition improves, but this is to |
| 358 | // avoid too much toggling. |
| 359 | if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0) |
| 360 | min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio); |
| 361 | |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 362 | return min_bitrate; |
| 363 | } |
| 364 | |
| 365 | void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate, |
| 366 | bool include_zero_allocations, |
| 367 | int max_multiplier, |
| 368 | ObserverAllocation* allocation) { |
| 369 | RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size()); |
| 370 | |
| 371 | ObserverSortingMap list_max_bitrates; |
| 372 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 373 | if (include_zero_allocations || |
| 374 | allocation->at(observer_config.observer) != 0) { |
| 375 | list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>( |
| 376 | observer_config.max_bitrate_bps, &observer_config)); |
| 377 | } |
| 378 | } |
| 379 | auto it = list_max_bitrates.begin(); |
| 380 | while (it != list_max_bitrates.end()) { |
| 381 | RTC_DCHECK_GT(bitrate, 0u); |
| 382 | uint32_t extra_allocation = |
| 383 | bitrate / static_cast<uint32_t>(list_max_bitrates.size()); |
| 384 | uint32_t total_allocation = |
| 385 | extra_allocation + allocation->at(it->second->observer); |
| 386 | bitrate -= extra_allocation; |
| 387 | if (total_allocation > max_multiplier * it->first) { |
| 388 | // There is more than we can fit for this observer, carry over to the |
| 389 | // remaining observers. |
| 390 | bitrate += total_allocation - max_multiplier * it->first; |
| 391 | total_allocation = max_multiplier * it->first; |
| 392 | } |
| 393 | // Finally, update the allocation for this observer. |
| 394 | allocation->at(it->second->observer) = total_allocation; |
| 395 | it = list_max_bitrates.erase(it); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate, |
| 400 | uint32_t sum_min_bitrates) { |
| 401 | if (bitrate < sum_min_bitrates) |
| 402 | return false; |
| 403 | |
perkj | 8eb37a3 | 2016-08-16 02:40:55 -0700 | [diff] [blame] | 404 | uint32_t extra_bitrate_per_observer = (bitrate - sum_min_bitrates) / |
mflodman | 101f250 | 2016-06-09 17:21:19 +0200 | [diff] [blame] | 405 | static_cast<uint32_t>(bitrate_observer_configs_.size()); |
| 406 | for (const auto& observer_config : bitrate_observer_configs_) { |
| 407 | if (observer_config.min_bitrate_bps + extra_bitrate_per_observer < |
| 408 | MinBitrateWithHysteresis(observer_config)) |
| 409 | return false; |
| 410 | } |
| 411 | return true; |
| 412 | } |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 413 | } // namespace webrtc |