blob: 97a2bb69ecdb118a3e8ca3dd72a291387f17c539 [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),
Mirko Bonadeie95b57c2019-07-12 14:55:42 +000067 bitrate_allocation_strategy_(nullptr),
Ying Wanga646d302018-03-02 17:04:11 +010068 transmission_max_bitrate_multiplier_(
Mirko Bonadeie95b57c2019-07-12 14:55:42 +000069 GetTransmissionMaxBitrateMultiplier()),
70 ignore_injected_strategy_(
71 field_trial::IsEnabled("WebRTC-IgnoreInjectedAllocationStrategy")) {
perkj26091b12016-09-01 01:17:40 -070072 sequenced_checker_.Detach();
73}
mflodman48a4beb2016-07-01 13:03:59 +020074
75BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070076 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
77 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020078}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000079
Sebastian Jansson2701bc92018-12-11 15:02:47 +010080void BitrateAllocator::UpdateStartRate(uint32_t start_rate_bps) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020081 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson2701bc92018-12-11 15:02:47 +010082 last_non_zero_bitrate_bps_ = start_rate_bps;
83}
84
Niels Möller74e5f802018-04-25 14:03:46 +020085// static
Ying Wanga646d302018-03-02 17:04:11 +010086uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
87 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
88 "WebRTC-TransmissionMaxBitrateMultiplier")
89 .c_str(),
90 nullptr, 10);
91 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010092 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
93 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010094 return static_cast<uint8_t>(multiplier);
95 }
96 return kTransmissionMaxBitrateMultiplier;
97}
98
perkj71ee44c2016-06-15 00:47:53 -070099void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100100 uint32_t link_capacity_bps,
perkj71ee44c2016-06-15 00:47:53 -0700101 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800102 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700103 int64_t bwe_period_ms) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200104 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100105 last_target_bps_ = target_bitrate_bps;
106 last_link_capacity_bps_ = link_capacity_bps;
perkjfea93092016-05-14 00:58:48 -0700107 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +0200108 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +0100109 last_fraction_loss_ = fraction_loss;
110 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700111 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700112
mflodman48a4beb2016-07-01 13:03:59 +0200113 // Periodically log the incoming BWE.
114 int64_t now = clock_->TimeInMilliseconds();
115 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200117 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800118 }
mflodman48a4beb2016-07-01 13:03:59 +0200119
120 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100121 ObserverAllocation bandwidth_allocation = AllocateBitrates(link_capacity_bps);
mflodman48a4beb2016-07-01 13:03:59 +0200122
123 for (auto& config : bitrate_observer_configs_) {
124 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100125 uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100126 BitrateAllocationUpdate update;
127 update.target_bitrate = DataRate::bps(allocated_bitrate);
128 update.link_capacity = DataRate::bps(allocated_bandwidth);
129 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
130 update.round_trip_time = TimeDelta::ms(last_rtt_);
131 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
132 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200133
134 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
135 if (target_bitrate_bps > 0)
136 ++num_pause_events_;
137 // The protection bitrate is an estimate based on the ratio between media
138 // and protection used before this observer was muted.
139 uint32_t predicted_protection_bps =
140 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100141 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
142 << " with configured min bitrate "
143 << config.min_bitrate_bps << " and current estimate of "
144 << target_bitrate_bps << " and protection bitrate "
145 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200146 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
147 if (target_bitrate_bps > 0)
148 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
150 << ", configured min bitrate " << config.min_bitrate_bps
151 << ", current allocation " << allocated_bitrate
152 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200153 }
154
155 // Only update the media ratio if the observer got an allocation.
156 if (allocated_bitrate > 0)
157 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
158 config.allocated_bitrate_bps = allocated_bitrate;
159 }
philipel5ef2bc12017-02-21 07:28:31 -0800160 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100161}
162
perkj57c21f92016-06-17 07:27:16 -0700163void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200164 MediaStreamAllocationConfig config) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200165 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200166 RTC_DCHECK_GT(config.bitrate_priority, 0);
167 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700168 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000169
mflodman101f2502016-06-09 17:21:19 +0200170 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700171 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200172 it->min_bitrate_bps = config.min_bitrate_bps;
173 it->max_bitrate_bps = config.max_bitrate_bps;
174 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
175 it->enforce_min_bitrate = config.enforce_min_bitrate;
176 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000177 } else {
Mirko Bonadeie95b57c2019-07-12 14:55:42 +0000178 bitrate_observer_configs_.push_back(ObserverConfig(
179 observer, config.min_bitrate_bps, config.max_bitrate_bps,
180 config.pad_up_bitrate_bps, config.priority_bitrate_bps,
181 config.enforce_min_bitrate, config.track_id, config.bitrate_priority));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000182 }
Stefan Holmere5904162015-03-26 11:11:06 +0100183
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100184 if (last_target_bps_ > 0) {
mflodman101f2502016-06-09 17:21:19 +0200185 // Calculate a new allocation and update all observers.
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100186
187 ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
188 ObserverAllocation bandwidth_allocation =
189 AllocateBitrates(last_link_capacity_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200190 for (auto& config : bitrate_observer_configs_) {
191 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100192 uint32_t bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100193 BitrateAllocationUpdate update;
194 update.target_bitrate = DataRate::bps(allocated_bitrate);
195 update.link_capacity = DataRate::bps(bandwidth);
196 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
197 update.round_trip_time = TimeDelta::ms(last_rtt_);
198 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
199 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200200 config.allocated_bitrate_bps = allocated_bitrate;
201 if (allocated_bitrate > 0)
202 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
203 }
perkjfea93092016-05-14 00:58:48 -0700204 } else {
205 // Currently, an encoder is not allowed to produce frames.
206 // But we still have to return the initial config bitrate + let the
207 // observer know that it can not produce frames.
Sebastian Jansson13e59032018-11-21 19:13:07 +0100208
209 BitrateAllocationUpdate update;
210 update.target_bitrate = DataRate::Zero();
211 update.link_capacity = DataRate::Zero();
212 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
213 update.round_trip_time = TimeDelta::ms(last_rtt_);
214 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
215 observer->OnBitrateUpdated(update);
Stefan Holmere5904162015-03-26 11:11:06 +0100216 }
perkj71ee44c2016-06-15 00:47:53 -0700217 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000218}
219
perkj71ee44c2016-06-15 00:47:53 -0700220void BitrateAllocator::UpdateAllocationLimits() {
221 uint32_t total_requested_padding_bitrate = 0;
222 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200223 uint32_t total_requested_max_bitrate = 0;
perkj26091b12016-09-01 01:17:40 -0700224 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800225 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700226 if (config.enforce_min_bitrate) {
227 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800228 } else if (config.allocated_bitrate_bps == 0) {
229 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100230 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700231 }
philipel5ef2bc12017-02-21 07:28:31 -0800232 total_requested_padding_bitrate += stream_padding;
Rasmus Brandt681de202019-02-04 15:09:34 +0100233 total_requested_max_bitrate += config.max_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000234 }
perkj71ee44c2016-06-15 00:47:53 -0700235
philipel5ef2bc12017-02-21 07:28:31 -0800236 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100237 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100238 total_requested_max_bitrate == total_requested_max_bitrate_) {
philipel5ef2bc12017-02-21 07:28:31 -0800239 return;
240 }
241
242 total_requested_min_bitrate_ = total_requested_min_bitrate;
243 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200244 total_requested_max_bitrate_ = total_requested_max_bitrate;
philipel5ef2bc12017-02-21 07:28:31 -0800245
Mirko Bonadei675513b2017-11-09 11:09:25 +0100246 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
247 << total_requested_min_bitrate
248 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200249 << total_requested_padding_bitrate
250 << "bps, total_requested_max_bitrate: "
251 << total_requested_max_bitrate << "bps";
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100252 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
253 total_requested_padding_bitrate,
254 total_requested_max_bitrate);
perkj71ee44c2016-06-15 00:47:53 -0700255}
256
257void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200258 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200259
perkj26091b12016-09-01 01:17:40 -0700260 auto it = FindObserverConfig(observer);
261 if (it != bitrate_observer_configs_.end()) {
262 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700263 }
perkj26091b12016-09-01 01:17:40 -0700264
perkj71ee44c2016-06-15 00:47:53 -0700265 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000266}
267
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200268int BitrateAllocator::GetStartBitrate(
269 BitrateAllocatorObserver* observer) const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200270 RTC_DCHECK_RUN_ON(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200271 const auto& it = FindObserverConfig(observer);
272 if (it == bitrate_observer_configs_.end()) {
273 // This observer hasn't been added yet, just give it its fair share.
274 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700275 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200276 } else if (it->allocated_bitrate_bps == -1) {
277 // This observer hasn't received an allocation yet, so do the same.
278 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700279 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200280 } else {
281 // This observer already has an allocation.
282 return it->allocated_bitrate_bps;
283 }
perkj57c21f92016-06-17 07:27:16 -0700284}
285
Mirko Bonadeie95b57c2019-07-12 14:55:42 +0000286void BitrateAllocator::SetBitrateAllocationStrategy(
287 std::unique_ptr<rtc::BitrateAllocationStrategy>
288 bitrate_allocation_strategy) {
289 RTC_DCHECK_RUN_ON(&sequenced_checker_);
290 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
291}
292
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200293BitrateAllocator::ObserverConfigs::const_iterator
294BitrateAllocator::FindObserverConfig(
295 const BitrateAllocatorObserver* observer) const {
296 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
mflodman48a4beb2016-07-01 13:03:59 +0200304BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700305BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700306 for (auto it = bitrate_observer_configs_.begin();
307 it != bitrate_observer_configs_.end(); ++it) {
308 if (it->observer == observer)
309 return it;
310 }
311 return bitrate_observer_configs_.end();
312}
313
perkjfea93092016-05-14 00:58:48 -0700314BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200315 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700316 if (bitrate_observer_configs_.empty())
317 return ObserverAllocation();
318
Mirko Bonadeie95b57c2019-07-12 14:55:42 +0000319 if (!ignore_injected_strategy_ && bitrate_allocation_strategy_ != nullptr) {
320 // Note: This intentionally causes slicing, we only copy the fields in
321 // ObserverConfig that are inherited from TrackConfig.
322 std::vector<rtc::BitrateAllocationStrategy::TrackConfig> track_configs(
323 bitrate_observer_configs_.begin(), bitrate_observer_configs_.end());
324
325 std::vector<uint32_t> track_allocations =
326 bitrate_allocation_strategy_->AllocateBitrates(
327 bitrate, std::move(track_configs));
328 // The strategy should return allocation for all tracks.
329 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
330 ObserverAllocation allocation;
331 auto track_allocations_it = track_allocations.begin();
332 for (const auto& observer_config : bitrate_observer_configs_) {
333 allocation[observer_config.observer] = *track_allocations_it++;
334 }
335 return allocation;
336 }
337
perkjfea93092016-05-14 00:58:48 -0700338 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700339 return ZeroRateAllocation();
340
341 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200342 uint32_t sum_max_bitrates = 0;
343 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700344 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200345 sum_max_bitrates += observer_config.max_bitrate_bps;
346 }
347
348 // Not enough for all observers to get an allocation, allocate according to:
349 // enforced min bitrate -> allocated bitrate previous round -> restart paused
350 // streams.
351 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700352 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700353
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800354 // All observers will get their min bitrate plus a share of the rest. This
355 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200356 if (bitrate <= sum_max_bitrates)
357 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700358
Ying Wanga646d302018-03-02 17:04:11 +0100359 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200360 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000361}
362
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200363BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
364 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700365 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700366 for (const auto& observer_config : bitrate_observer_configs_)
367 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700368 return allocation;
369}
370
mflodman2ebe5b12016-05-13 01:43:51 -0700371BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200372 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700373 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200374 // Start by allocating bitrate to observers enforcing a min bitrate, hence
375 // remaining_bitrate might turn negative.
376 int64_t remaining_bitrate = bitrate;
377 for (const auto& observer_config : bitrate_observer_configs_) {
378 int32_t allocated_bitrate = 0;
379 if (observer_config.enforce_min_bitrate)
380 allocated_bitrate = observer_config.min_bitrate_bps;
381
382 allocation[observer_config.observer] = allocated_bitrate;
383 remaining_bitrate -= allocated_bitrate;
384 }
385
386 // Allocate bitrate to all previously active streams.
387 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700388 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200389 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100390 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200391 continue;
392
srte1eb051c2017-11-29 11:23:59 +0100393 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200394 if (remaining_bitrate >= required_bitrate) {
395 allocation[observer_config.observer] = required_bitrate;
396 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200397 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000398 }
399 }
mflodman101f2502016-06-09 17:21:19 +0200400
401 // Allocate bitrate to previously paused streams.
402 if (remaining_bitrate > 0) {
403 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100404 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200405 continue;
406
407 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100408 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200409 if (remaining_bitrate >= required_bitrate) {
410 allocation[observer_config.observer] = required_bitrate;
411 remaining_bitrate -= required_bitrate;
412 }
413 }
414 }
415
416 // Split a possible remainder evenly on all streams with an allocation.
417 if (remaining_bitrate > 0)
418 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
419
420 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100421 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000422}
mflodman101f2502016-06-09 17:21:19 +0200423
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800424// Allocates the bitrate based on the bitrate priority of each observer. This
425// bitrate priority defines the priority for bitrate to be allocated to that
426// observer in relation to other observers. For example with two observers, if
427// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
428// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
429// allocated twice the bitrate as observer 1 above the each observer's
430// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200431BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
432 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200433 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200434 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800435 ObserverAllocation observers_capacities;
436 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200437 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800438 observers_capacities[observer_config.observer] =
439 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
440 }
mflodman101f2502016-06-09 17:21:19 +0200441
442 bitrate -= sum_min_bitrates;
Sebastian Jansson464a5572019-02-12 13:32:32 +0100443
444 // TODO(srte): Implement fair sharing between prioritized streams, currently
445 // they are treated on a first come first serve basis.
446 for (const auto& observer_config : bitrate_observer_configs_) {
447 int64_t priority_margin = observer_config.priority_bitrate_bps -
448 allocation[observer_config.observer];
449 if (priority_margin > 0 && bitrate > 0) {
450 int64_t extra_bitrate = std::min<int64_t>(priority_margin, bitrate);
451 allocation[observer_config.observer] +=
452 rtc::dchecked_cast<int>(extra_bitrate);
453 observers_capacities[observer_config.observer] -= extra_bitrate;
454 bitrate -= extra_bitrate;
455 }
456 }
457
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800458 // From the remaining bitrate, allocate a proportional amount to each observer
459 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200460 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800461 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200462
463 return allocation;
464}
465
466BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700467 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200468 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200469 ObserverAllocation allocation;
470
471 for (const auto& observer_config : bitrate_observer_configs_) {
472 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
473 bitrate -= observer_config.max_bitrate_bps;
474 }
Ying Wanga646d302018-03-02 17:04:11 +0100475 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200476 &allocation);
477 return allocation;
478}
479
srte1eb051c2017-11-29 11:23:59 +0100480uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200481 // Return the configured minimum bitrate for newly added observers, to avoid
482 // requiring an extra high bitrate for the observer to get an allocated
483 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100484 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200485}
486
srte1eb051c2017-11-29 11:23:59 +0100487uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
488 uint32_t min_bitrate = min_bitrate_bps;
489 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200490 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
491 kMinToggleBitrateBps);
492 }
mflodman48a4beb2016-07-01 13:03:59 +0200493 // Account for protection bitrate used by this observer in the previous
494 // allocation.
495 // Note: the ratio will only be updated when the stream is active, meaning a
496 // paused stream won't get any ratio updates. This might lead to waiting a bit
497 // longer than necessary if the network condition improves, but this is to
498 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100499 if (media_ratio > 0.0 && media_ratio < 1.0)
500 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200501
mflodman101f2502016-06-09 17:21:19 +0200502 return min_bitrate;
503}
504
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200505void BitrateAllocator::DistributeBitrateEvenly(
506 uint32_t bitrate,
507 bool include_zero_allocations,
508 int max_multiplier,
509 ObserverAllocation* allocation) const {
mflodman101f2502016-06-09 17:21:19 +0200510 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
511
512 ObserverSortingMap list_max_bitrates;
513 for (const auto& observer_config : bitrate_observer_configs_) {
514 if (include_zero_allocations ||
515 allocation->at(observer_config.observer) != 0) {
516 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
517 observer_config.max_bitrate_bps, &observer_config));
518 }
519 }
520 auto it = list_max_bitrates.begin();
521 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800522 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200523 uint32_t extra_allocation =
524 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
525 uint32_t total_allocation =
526 extra_allocation + allocation->at(it->second->observer);
527 bitrate -= extra_allocation;
528 if (total_allocation > max_multiplier * it->first) {
529 // There is more than we can fit for this observer, carry over to the
530 // remaining observers.
531 bitrate += total_allocation - max_multiplier * it->first;
532 total_allocation = max_multiplier * it->first;
533 }
534 // Finally, update the allocation for this observer.
535 allocation->at(it->second->observer) = total_allocation;
536 it = list_max_bitrates.erase(it);
537 }
538}
539
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200540bool BitrateAllocator::EnoughBitrateForAllObservers(
541 uint32_t bitrate,
542 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200543 if (bitrate < sum_min_bitrates)
544 return false;
545
perkj26091b12016-09-01 01:17:40 -0700546 uint32_t extra_bitrate_per_observer =
547 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200548 static_cast<uint32_t>(bitrate_observer_configs_.size());
549 for (const auto& observer_config : bitrate_observer_configs_) {
550 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100551 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200552 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800553 }
mflodman101f2502016-06-09 17:21:19 +0200554 }
555 return true;
556}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800557
558void BitrateAllocator::DistributeBitrateRelatively(
559 uint32_t remaining_bitrate,
560 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200561 ObserverAllocation* allocation) const {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800562 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
563 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
564
565 struct PriorityRateObserverConfig {
566 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
567 uint32_t capacity_bps,
568 double bitrate_priority)
569 : allocation_key(allocation_key),
570 capacity_bps(capacity_bps),
571 bitrate_priority(bitrate_priority) {}
572
573 BitrateAllocatorObserver* allocation_key;
574 // The amount of bitrate bps that can be allocated to this observer.
575 uint32_t capacity_bps;
576 double bitrate_priority;
577
578 // We want to sort by which observers will be allocated their full capacity
579 // first. By dividing each observer's capacity by its bitrate priority we
580 // are "normalizing" the capacity of an observer by the rate it will be
581 // filled. This is because the amount allocated is based upon bitrate
582 // priority. We allocate twice as much bitrate to an observer with twice the
583 // bitrate priority of another.
584 bool operator<(const PriorityRateObserverConfig& other) const {
585 return capacity_bps / bitrate_priority <
586 other.capacity_bps / other.bitrate_priority;
587 }
588 };
589
590 double bitrate_priority_sum = 0;
591 std::vector<PriorityRateObserverConfig> priority_rate_observers;
592 for (const auto& observer_config : bitrate_observer_configs_) {
593 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
594 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
595 observer_config.bitrate_priority);
596 bitrate_priority_sum += observer_config.bitrate_priority;
597 }
598
599 // Iterate in the order observers can be allocated their full capacity.
600 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
601 size_t i;
602 for (i = 0; i < priority_rate_observers.size(); ++i) {
603 const auto& priority_rate_observer = priority_rate_observers[i];
604 // We allocate the full capacity to an observer only if its relative
605 // portion from the remaining bitrate is sufficient to allocate its full
606 // capacity. This means we aren't greedily allocating the full capacity, but
607 // that it is only done when there is also enough bitrate to allocate the
608 // proportional amounts to all other observers.
609 double observer_share =
610 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
611 double allocation_bps = observer_share * remaining_bitrate;
612 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
613 if (!enough_bitrate)
614 break;
615 allocation->at(priority_rate_observer.allocation_key) +=
616 priority_rate_observer.capacity_bps;
617 remaining_bitrate -= priority_rate_observer.capacity_bps;
618 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
619 }
620
621 // From the remaining bitrate, allocate the proportional amounts to the
622 // observers that aren't allocated their max capacity.
623 for (; i < priority_rate_observers.size(); ++i) {
624 const auto& priority_rate_observer = priority_rate_observers[i];
625 double fraction_allocated =
626 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
627 allocation->at(priority_rate_observer.allocation_key) +=
628 fraction_allocated * remaining_bitrate;
629 }
630}
631
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000632} // namespace webrtc