blob: 487dd47ad8e87c6de21b3c93c9db5966cf55d1b1 [file] [log] [blame]
stefan@webrtc.org792f1a12015-03-04 12:24:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020012#include "call/bitrate_allocator.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000013
14#include <algorithm>
Seth Hampsonfe73d6a2017-11-14 10:49:06 -080015#include <cmath>
Alex Narest78609d52017-10-20 10:37:47 +020016#include <memory>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000017#include <utility>
18
Sebastian Jansson6736df12018-11-21 19:18:39 +010019#include "api/units/data_rate.h"
20#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
23#include "system_wrappers/include/clock.h"
Ying Wanga646d302018-03-02 17:04:11 +010024#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000026
27namespace webrtc {
28
Rasmus Brandt681de202019-02-04 15:09:34 +010029namespace {
30
Stefan Holmere5904162015-03-26 11:11:06 +010031// Allow packets to be transmitted in up to 2 times max video bitrate if the
32// bandwidth estimate allows it.
Ying Wanga646d302018-03-02 17:04:11 +010033const uint8_t kTransmissionMaxBitrateMultiplier = 2;
Stefan Holmere5904162015-03-26 11:11:06 +010034const int kDefaultBitrateBps = 300000;
35
mflodman101f2502016-06-09 17:21:19 +020036// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
37const double kToggleFactor = 0.1;
38const uint32_t kMinToggleBitrateBps = 20000;
39
mflodman48a4beb2016-07-01 13:03:59 +020040const int64_t kBweLogIntervalMs = 5000;
41
mflodman48a4beb2016-07-01 13:03:59 +020042double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
kwibergaf476c72016-11-28 15:21:39 -080043 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 13:03:59 +020044 if (protection_bitrate == 0)
45 return 1.0;
46
47 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
48 return media_bitrate / static_cast<double>(allocated_bitrate);
49}
Rasmus Brandt681de202019-02-04 15:09:34 +010050
mflodman48a4beb2016-07-01 13:03:59 +020051} // namespace
52
Sebastian Janssonda6806c2019-03-04 17:05:12 +010053BitrateAllocator::BitrateAllocator(Clock* clock, LimitObserver* limit_observer)
perkj71ee44c2016-06-15 00:47:53 -070054 : limit_observer_(limit_observer),
Sebastian Jansson89c94b92018-11-20 17:16:36 +010055 last_target_bps_(0),
56 last_link_capacity_bps_(0),
perkjfea93092016-05-14 00:58:48 -070057 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010058 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020059 last_rtt_(0),
Sebastian Jansson13e59032018-11-21 19:13:07 +010060 last_bwe_period_ms_(1000),
mflodman48a4beb2016-07-01 13:03:59 +020061 num_pause_events_(0),
Sebastian Janssonda6806c2019-03-04 17:05:12 +010062 clock_(clock),
philipel5ef2bc12017-02-21 07:28:31 -080063 last_bwe_log_time_(0),
64 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020065 total_requested_min_bitrate_(0),
Sebastian Jansson448f4d52018-04-04 14:52:07 +020066 total_requested_max_bitrate_(0),
Ying Wanga646d302018-03-02 17:04:11 +010067 transmission_max_bitrate_multiplier_(
Jonas Olsson0182a032019-07-09 12:31:20 +020068 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070069 sequenced_checker_.Detach();
70}
mflodman48a4beb2016-07-01 13:03:59 +020071
72BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070073 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
74 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020075}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000076
Sebastian Jansson2701bc92018-12-11 15:02:47 +010077void BitrateAllocator::UpdateStartRate(uint32_t start_rate_bps) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020078 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson2701bc92018-12-11 15:02:47 +010079 last_non_zero_bitrate_bps_ = start_rate_bps;
80}
81
Niels Möller74e5f802018-04-25 14:03:46 +020082// static
Ying Wanga646d302018-03-02 17:04:11 +010083uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
84 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
85 "WebRTC-TransmissionMaxBitrateMultiplier")
86 .c_str(),
87 nullptr, 10);
88 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010089 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
90 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010091 return static_cast<uint8_t>(multiplier);
92 }
93 return kTransmissionMaxBitrateMultiplier;
94}
95
perkj71ee44c2016-06-15 00:47:53 -070096void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
Sebastian Jansson89c94b92018-11-20 17:16:36 +010097 uint32_t link_capacity_bps,
perkj71ee44c2016-06-15 00:47:53 -070098 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080099 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700100 int64_t bwe_period_ms) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200101 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100102 last_target_bps_ = target_bitrate_bps;
103 last_link_capacity_bps_ = link_capacity_bps;
perkjfea93092016-05-14 00:58:48 -0700104 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +0200105 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +0100106 last_fraction_loss_ = fraction_loss;
107 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700108 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700109
mflodman48a4beb2016-07-01 13:03:59 +0200110 // Periodically log the incoming BWE.
111 int64_t now = clock_->TimeInMilliseconds();
112 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100113 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200114 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800115 }
mflodman48a4beb2016-07-01 13:03:59 +0200116
117 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100118 ObserverAllocation bandwidth_allocation = AllocateBitrates(link_capacity_bps);
mflodman48a4beb2016-07-01 13:03:59 +0200119
120 for (auto& config : bitrate_observer_configs_) {
121 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100122 uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100123 BitrateAllocationUpdate update;
124 update.target_bitrate = DataRate::bps(allocated_bitrate);
125 update.link_capacity = DataRate::bps(allocated_bandwidth);
126 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
127 update.round_trip_time = TimeDelta::ms(last_rtt_);
128 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
129 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200130
131 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
132 if (target_bitrate_bps > 0)
133 ++num_pause_events_;
134 // The protection bitrate is an estimate based on the ratio between media
135 // and protection used before this observer was muted.
136 uint32_t predicted_protection_bps =
137 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100138 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
139 << " with configured min bitrate "
140 << config.min_bitrate_bps << " and current estimate of "
141 << target_bitrate_bps << " and protection bitrate "
142 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200143 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
144 if (target_bitrate_bps > 0)
145 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100146 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
147 << ", configured min bitrate " << config.min_bitrate_bps
148 << ", current allocation " << allocated_bitrate
149 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200150 }
151
152 // Only update the media ratio if the observer got an allocation.
153 if (allocated_bitrate > 0)
154 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
155 config.allocated_bitrate_bps = allocated_bitrate;
156 }
philipel5ef2bc12017-02-21 07:28:31 -0800157 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100158}
159
perkj57c21f92016-06-17 07:27:16 -0700160void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200161 MediaStreamAllocationConfig config) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200162 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200163 RTC_DCHECK_GT(config.bitrate_priority, 0);
164 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700165 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000166
mflodman101f2502016-06-09 17:21:19 +0200167 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700168 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200169 it->min_bitrate_bps = config.min_bitrate_bps;
170 it->max_bitrate_bps = config.max_bitrate_bps;
171 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
172 it->enforce_min_bitrate = config.enforce_min_bitrate;
173 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000174 } else {
Jonas Olsson0182a032019-07-09 12:31:20 +0200175 bitrate_observer_configs_.push_back(
176 ObserverConfig(observer, config.min_bitrate_bps, config.max_bitrate_bps,
177 config.pad_up_bitrate_bps, config.priority_bitrate_bps,
178 config.enforce_min_bitrate, config.bitrate_priority));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000179 }
Stefan Holmere5904162015-03-26 11:11:06 +0100180
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100181 if (last_target_bps_ > 0) {
mflodman101f2502016-06-09 17:21:19 +0200182 // Calculate a new allocation and update all observers.
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100183
184 ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
185 ObserverAllocation bandwidth_allocation =
186 AllocateBitrates(last_link_capacity_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200187 for (auto& config : bitrate_observer_configs_) {
188 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100189 uint32_t bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100190 BitrateAllocationUpdate update;
191 update.target_bitrate = DataRate::bps(allocated_bitrate);
192 update.link_capacity = DataRate::bps(bandwidth);
193 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
194 update.round_trip_time = TimeDelta::ms(last_rtt_);
195 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
196 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200197 config.allocated_bitrate_bps = allocated_bitrate;
198 if (allocated_bitrate > 0)
199 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
200 }
perkjfea93092016-05-14 00:58:48 -0700201 } else {
202 // Currently, an encoder is not allowed to produce frames.
203 // But we still have to return the initial config bitrate + let the
204 // observer know that it can not produce frames.
Sebastian Jansson13e59032018-11-21 19:13:07 +0100205
206 BitrateAllocationUpdate update;
207 update.target_bitrate = DataRate::Zero();
208 update.link_capacity = DataRate::Zero();
209 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
210 update.round_trip_time = TimeDelta::ms(last_rtt_);
211 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
212 observer->OnBitrateUpdated(update);
Stefan Holmere5904162015-03-26 11:11:06 +0100213 }
perkj71ee44c2016-06-15 00:47:53 -0700214 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000215}
216
perkj71ee44c2016-06-15 00:47:53 -0700217void BitrateAllocator::UpdateAllocationLimits() {
218 uint32_t total_requested_padding_bitrate = 0;
219 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200220 uint32_t total_requested_max_bitrate = 0;
perkj26091b12016-09-01 01:17:40 -0700221 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800222 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700223 if (config.enforce_min_bitrate) {
224 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800225 } else if (config.allocated_bitrate_bps == 0) {
226 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100227 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700228 }
philipel5ef2bc12017-02-21 07:28:31 -0800229 total_requested_padding_bitrate += stream_padding;
Rasmus Brandt681de202019-02-04 15:09:34 +0100230 total_requested_max_bitrate += config.max_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000231 }
perkj71ee44c2016-06-15 00:47:53 -0700232
philipel5ef2bc12017-02-21 07:28:31 -0800233 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100234 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100235 total_requested_max_bitrate == total_requested_max_bitrate_) {
philipel5ef2bc12017-02-21 07:28:31 -0800236 return;
237 }
238
239 total_requested_min_bitrate_ = total_requested_min_bitrate;
240 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200241 total_requested_max_bitrate_ = total_requested_max_bitrate;
philipel5ef2bc12017-02-21 07:28:31 -0800242
Mirko Bonadei675513b2017-11-09 11:09:25 +0100243 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
244 << total_requested_min_bitrate
245 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200246 << total_requested_padding_bitrate
247 << "bps, total_requested_max_bitrate: "
248 << total_requested_max_bitrate << "bps";
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100249 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
250 total_requested_padding_bitrate,
251 total_requested_max_bitrate);
perkj71ee44c2016-06-15 00:47:53 -0700252}
253
254void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200255 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200256
perkj26091b12016-09-01 01:17:40 -0700257 auto it = FindObserverConfig(observer);
258 if (it != bitrate_observer_configs_.end()) {
259 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700260 }
perkj26091b12016-09-01 01:17:40 -0700261
perkj71ee44c2016-06-15 00:47:53 -0700262 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000263}
264
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200265int BitrateAllocator::GetStartBitrate(
266 BitrateAllocatorObserver* observer) const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200267 RTC_DCHECK_RUN_ON(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200268 const auto& it = FindObserverConfig(observer);
269 if (it == bitrate_observer_configs_.end()) {
270 // This observer hasn't been added yet, just give it its fair share.
271 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700272 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200273 } else if (it->allocated_bitrate_bps == -1) {
274 // This observer hasn't received an allocation yet, so do the same.
275 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700276 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200277 } else {
278 // This observer already has an allocation.
279 return it->allocated_bitrate_bps;
280 }
perkj57c21f92016-06-17 07:27:16 -0700281}
282
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200283BitrateAllocator::ObserverConfigs::const_iterator
284BitrateAllocator::FindObserverConfig(
285 const BitrateAllocatorObserver* observer) const {
286 for (auto it = bitrate_observer_configs_.begin();
287 it != bitrate_observer_configs_.end(); ++it) {
288 if (it->observer == observer)
289 return it;
290 }
291 return bitrate_observer_configs_.end();
292}
293
mflodman48a4beb2016-07-01 13:03:59 +0200294BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700295BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700296 for (auto it = bitrate_observer_configs_.begin();
297 it != bitrate_observer_configs_.end(); ++it) {
298 if (it->observer == observer)
299 return it;
300 }
301 return bitrate_observer_configs_.end();
302}
303
perkjfea93092016-05-14 00:58:48 -0700304BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200305 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700306 if (bitrate_observer_configs_.empty())
307 return ObserverAllocation();
308
perkjfea93092016-05-14 00:58:48 -0700309 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700310 return ZeroRateAllocation();
311
312 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200313 uint32_t sum_max_bitrates = 0;
314 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700315 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200316 sum_max_bitrates += observer_config.max_bitrate_bps;
317 }
318
319 // Not enough for all observers to get an allocation, allocate according to:
320 // enforced min bitrate -> allocated bitrate previous round -> restart paused
321 // streams.
322 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700323 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700324
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800325 // All observers will get their min bitrate plus a share of the rest. This
326 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200327 if (bitrate <= sum_max_bitrates)
328 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700329
Ying Wanga646d302018-03-02 17:04:11 +0100330 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200331 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000332}
333
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200334BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
335 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700336 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700337 for (const auto& observer_config : bitrate_observer_configs_)
338 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700339 return allocation;
340}
341
mflodman2ebe5b12016-05-13 01:43:51 -0700342BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200343 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700344 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200345 // Start by allocating bitrate to observers enforcing a min bitrate, hence
346 // remaining_bitrate might turn negative.
347 int64_t remaining_bitrate = bitrate;
348 for (const auto& observer_config : bitrate_observer_configs_) {
349 int32_t allocated_bitrate = 0;
350 if (observer_config.enforce_min_bitrate)
351 allocated_bitrate = observer_config.min_bitrate_bps;
352
353 allocation[observer_config.observer] = allocated_bitrate;
354 remaining_bitrate -= allocated_bitrate;
355 }
356
357 // Allocate bitrate to all previously active streams.
358 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700359 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200360 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100361 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200362 continue;
363
srte1eb051c2017-11-29 11:23:59 +0100364 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200365 if (remaining_bitrate >= required_bitrate) {
366 allocation[observer_config.observer] = required_bitrate;
367 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200368 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000369 }
370 }
mflodman101f2502016-06-09 17:21:19 +0200371
372 // Allocate bitrate to previously paused streams.
373 if (remaining_bitrate > 0) {
374 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100375 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200376 continue;
377
378 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100379 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200380 if (remaining_bitrate >= required_bitrate) {
381 allocation[observer_config.observer] = required_bitrate;
382 remaining_bitrate -= required_bitrate;
383 }
384 }
385 }
386
387 // Split a possible remainder evenly on all streams with an allocation.
388 if (remaining_bitrate > 0)
389 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
390
391 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100392 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000393}
mflodman101f2502016-06-09 17:21:19 +0200394
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800395// Allocates the bitrate based on the bitrate priority of each observer. This
396// bitrate priority defines the priority for bitrate to be allocated to that
397// observer in relation to other observers. For example with two observers, if
398// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
399// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
400// allocated twice the bitrate as observer 1 above the each observer's
401// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200402BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
403 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200404 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200405 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800406 ObserverAllocation observers_capacities;
407 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200408 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800409 observers_capacities[observer_config.observer] =
410 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
411 }
mflodman101f2502016-06-09 17:21:19 +0200412
413 bitrate -= sum_min_bitrates;
Sebastian Jansson464a5572019-02-12 13:32:32 +0100414
415 // TODO(srte): Implement fair sharing between prioritized streams, currently
416 // they are treated on a first come first serve basis.
417 for (const auto& observer_config : bitrate_observer_configs_) {
418 int64_t priority_margin = observer_config.priority_bitrate_bps -
419 allocation[observer_config.observer];
420 if (priority_margin > 0 && bitrate > 0) {
421 int64_t extra_bitrate = std::min<int64_t>(priority_margin, bitrate);
422 allocation[observer_config.observer] +=
423 rtc::dchecked_cast<int>(extra_bitrate);
424 observers_capacities[observer_config.observer] -= extra_bitrate;
425 bitrate -= extra_bitrate;
426 }
427 }
428
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800429 // From the remaining bitrate, allocate a proportional amount to each observer
430 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200431 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800432 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200433
434 return allocation;
435}
436
437BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700438 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200439 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200440 ObserverAllocation allocation;
441
442 for (const auto& observer_config : bitrate_observer_configs_) {
443 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
444 bitrate -= observer_config.max_bitrate_bps;
445 }
Ying Wanga646d302018-03-02 17:04:11 +0100446 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200447 &allocation);
448 return allocation;
449}
450
srte1eb051c2017-11-29 11:23:59 +0100451uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200452 // Return the configured minimum bitrate for newly added observers, to avoid
453 // requiring an extra high bitrate for the observer to get an allocated
454 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100455 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200456}
457
srte1eb051c2017-11-29 11:23:59 +0100458uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
459 uint32_t min_bitrate = min_bitrate_bps;
460 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200461 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
462 kMinToggleBitrateBps);
463 }
mflodman48a4beb2016-07-01 13:03:59 +0200464 // Account for protection bitrate used by this observer in the previous
465 // allocation.
466 // Note: the ratio will only be updated when the stream is active, meaning a
467 // paused stream won't get any ratio updates. This might lead to waiting a bit
468 // longer than necessary if the network condition improves, but this is to
469 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100470 if (media_ratio > 0.0 && media_ratio < 1.0)
471 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200472
mflodman101f2502016-06-09 17:21:19 +0200473 return min_bitrate;
474}
475
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200476void BitrateAllocator::DistributeBitrateEvenly(
477 uint32_t bitrate,
478 bool include_zero_allocations,
479 int max_multiplier,
480 ObserverAllocation* allocation) const {
mflodman101f2502016-06-09 17:21:19 +0200481 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
482
483 ObserverSortingMap list_max_bitrates;
484 for (const auto& observer_config : bitrate_observer_configs_) {
485 if (include_zero_allocations ||
486 allocation->at(observer_config.observer) != 0) {
487 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
488 observer_config.max_bitrate_bps, &observer_config));
489 }
490 }
491 auto it = list_max_bitrates.begin();
492 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800493 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200494 uint32_t extra_allocation =
495 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
496 uint32_t total_allocation =
497 extra_allocation + allocation->at(it->second->observer);
498 bitrate -= extra_allocation;
499 if (total_allocation > max_multiplier * it->first) {
500 // There is more than we can fit for this observer, carry over to the
501 // remaining observers.
502 bitrate += total_allocation - max_multiplier * it->first;
503 total_allocation = max_multiplier * it->first;
504 }
505 // Finally, update the allocation for this observer.
506 allocation->at(it->second->observer) = total_allocation;
507 it = list_max_bitrates.erase(it);
508 }
509}
510
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200511bool BitrateAllocator::EnoughBitrateForAllObservers(
512 uint32_t bitrate,
513 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200514 if (bitrate < sum_min_bitrates)
515 return false;
516
perkj26091b12016-09-01 01:17:40 -0700517 uint32_t extra_bitrate_per_observer =
518 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200519 static_cast<uint32_t>(bitrate_observer_configs_.size());
520 for (const auto& observer_config : bitrate_observer_configs_) {
521 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100522 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200523 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800524 }
mflodman101f2502016-06-09 17:21:19 +0200525 }
526 return true;
527}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800528
529void BitrateAllocator::DistributeBitrateRelatively(
530 uint32_t remaining_bitrate,
531 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200532 ObserverAllocation* allocation) const {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800533 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
534 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
535
536 struct PriorityRateObserverConfig {
537 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
538 uint32_t capacity_bps,
539 double bitrate_priority)
540 : allocation_key(allocation_key),
541 capacity_bps(capacity_bps),
542 bitrate_priority(bitrate_priority) {}
543
544 BitrateAllocatorObserver* allocation_key;
545 // The amount of bitrate bps that can be allocated to this observer.
546 uint32_t capacity_bps;
547 double bitrate_priority;
548
549 // We want to sort by which observers will be allocated their full capacity
550 // first. By dividing each observer's capacity by its bitrate priority we
551 // are "normalizing" the capacity of an observer by the rate it will be
552 // filled. This is because the amount allocated is based upon bitrate
553 // priority. We allocate twice as much bitrate to an observer with twice the
554 // bitrate priority of another.
555 bool operator<(const PriorityRateObserverConfig& other) const {
556 return capacity_bps / bitrate_priority <
557 other.capacity_bps / other.bitrate_priority;
558 }
559 };
560
561 double bitrate_priority_sum = 0;
562 std::vector<PriorityRateObserverConfig> priority_rate_observers;
563 for (const auto& observer_config : bitrate_observer_configs_) {
564 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
565 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
566 observer_config.bitrate_priority);
567 bitrate_priority_sum += observer_config.bitrate_priority;
568 }
569
570 // Iterate in the order observers can be allocated their full capacity.
571 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
572 size_t i;
573 for (i = 0; i < priority_rate_observers.size(); ++i) {
574 const auto& priority_rate_observer = priority_rate_observers[i];
575 // We allocate the full capacity to an observer only if its relative
576 // portion from the remaining bitrate is sufficient to allocate its full
577 // capacity. This means we aren't greedily allocating the full capacity, but
578 // that it is only done when there is also enough bitrate to allocate the
579 // proportional amounts to all other observers.
580 double observer_share =
581 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
582 double allocation_bps = observer_share * remaining_bitrate;
583 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
584 if (!enough_bitrate)
585 break;
586 allocation->at(priority_rate_observer.allocation_key) +=
587 priority_rate_observer.capacity_bps;
588 remaining_bitrate -= priority_rate_observer.capacity_bps;
589 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
590 }
591
592 // From the remaining bitrate, allocate the proportional amounts to the
593 // observers that aren't allocated their max capacity.
594 for (; i < priority_rate_observers.size(); ++i) {
595 const auto& priority_rate_observer = priority_rate_observers[i];
596 double fraction_allocated =
597 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
598 allocation->at(priority_rate_observer.allocation_key) +=
599 fraction_allocated * remaining_bitrate;
600 }
601}
602
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000603} // namespace webrtc