blob: c13848fb9737e03552c9a1bcf97123b129817cdc [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 Jansson4d461ba2019-09-17 20:53:26 +020019#include "absl/algorithm/container.h"
Sebastian Jansson6736df12018-11-21 19:18:39 +010020#include "api/units/data_rate.h"
21#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
23#include "rtc_base/logging.h"
24#include "system_wrappers/include/clock.h"
Ying Wanga646d302018-03-02 17:04:11 +010025#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000027
28namespace webrtc {
29
Rasmus Brandt681de202019-02-04 15:09:34 +010030namespace {
31
Stefan Holmere5904162015-03-26 11:11:06 +010032// Allow packets to be transmitted in up to 2 times max video bitrate if the
33// bandwidth estimate allows it.
Ying Wanga646d302018-03-02 17:04:11 +010034const uint8_t kTransmissionMaxBitrateMultiplier = 2;
Stefan Holmere5904162015-03-26 11:11:06 +010035const int kDefaultBitrateBps = 300000;
36
mflodman101f2502016-06-09 17:21:19 +020037// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
38const double kToggleFactor = 0.1;
39const uint32_t kMinToggleBitrateBps = 20000;
40
mflodman48a4beb2016-07-01 13:03:59 +020041const int64_t kBweLogIntervalMs = 5000;
42
mflodman48a4beb2016-07-01 13:03:59 +020043double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
kwibergaf476c72016-11-28 15:21:39 -080044 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 13:03:59 +020045 if (protection_bitrate == 0)
46 return 1.0;
47
48 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
49 return media_bitrate / static_cast<double>(allocated_bitrate);
50}
Rasmus Brandt681de202019-02-04 15:09:34 +010051
mflodman48a4beb2016-07-01 13:03:59 +020052} // namespace
53
Sebastian Janssonda6806c2019-03-04 17:05:12 +010054BitrateAllocator::BitrateAllocator(Clock* clock, LimitObserver* limit_observer)
perkj71ee44c2016-06-15 00:47:53 -070055 : limit_observer_(limit_observer),
Sebastian Jansson89c94b92018-11-20 17:16:36 +010056 last_target_bps_(0),
Florent Castelli4e615d52019-08-22 16:09:06 +020057 last_stable_target_bps_(0),
58 last_bandwidth_bps_(0),
perkjfea93092016-05-14 00:58:48 -070059 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010060 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020061 last_rtt_(0),
Sebastian Jansson13e59032018-11-21 19:13:07 +010062 last_bwe_period_ms_(1000),
mflodman48a4beb2016-07-01 13:03:59 +020063 num_pause_events_(0),
Sebastian Janssonda6806c2019-03-04 17:05:12 +010064 clock_(clock),
philipel5ef2bc12017-02-21 07:28:31 -080065 last_bwe_log_time_(0),
Ying Wanga646d302018-03-02 17:04:11 +010066 transmission_max_bitrate_multiplier_(
Jonas Olsson0182a032019-07-09 12:31:20 +020067 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070068 sequenced_checker_.Detach();
69}
mflodman48a4beb2016-07-01 13:03:59 +020070
71BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070072 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
73 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020074}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000075
Sebastian Jansson2701bc92018-12-11 15:02:47 +010076void BitrateAllocator::UpdateStartRate(uint32_t start_rate_bps) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020077 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson2701bc92018-12-11 15:02:47 +010078 last_non_zero_bitrate_bps_ = start_rate_bps;
79}
80
Niels Möller74e5f802018-04-25 14:03:46 +020081// static
Ying Wanga646d302018-03-02 17:04:11 +010082uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
83 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
84 "WebRTC-TransmissionMaxBitrateMultiplier")
85 .c_str(),
86 nullptr, 10);
87 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010088 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
89 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010090 return static_cast<uint8_t>(multiplier);
91 }
92 return kTransmissionMaxBitrateMultiplier;
93}
94
perkj71ee44c2016-06-15 00:47:53 -070095void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
Florent Castelli4e615d52019-08-22 16:09:06 +020096 uint32_t stable_target_bitrate_bps,
97 uint32_t bandwidth_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;
Florent Castelli4e615d52019-08-22 16:09:06 +0200103 last_bandwidth_bps_ = bandwidth_bps;
104 last_stable_target_bps_ = stable_target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -0700105 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +0200106 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +0100107 last_fraction_loss_ = fraction_loss;
108 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700109 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700110
mflodman48a4beb2016-07-01 13:03:59 +0200111 // Periodically log the incoming BWE.
112 int64_t now = clock_->TimeInMilliseconds();
113 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200115 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800116 }
mflodman48a4beb2016-07-01 13:03:59 +0200117
118 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
Florent Castelli4e615d52019-08-22 16:09:06 +0200119 ObserverAllocation bandwidth_allocation = AllocateBitrates(bandwidth_bps);
120 ObserverAllocation stable_bitrate_allocation =
121 AllocateBitrates(stable_target_bitrate_bps);
mflodman48a4beb2016-07-01 13:03:59 +0200122
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200123 for (auto& config : allocatable_tracks_) {
mflodman48a4beb2016-07-01 13:03:59 +0200124 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100125 uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
Florent Castelli4e615d52019-08-22 16:09:06 +0200126 uint32_t allocated_stable_target_rate =
127 stable_bitrate_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100128 BitrateAllocationUpdate update;
129 update.target_bitrate = DataRate::bps(allocated_bitrate);
Florent Castelli4e615d52019-08-22 16:09:06 +0200130 update.stable_target_bitrate = DataRate::bps(allocated_stable_target_rate);
Sebastian Jansson13e59032018-11-21 19:13:07 +0100131 update.link_capacity = DataRate::bps(allocated_bandwidth);
132 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
133 update.round_trip_time = TimeDelta::ms(last_rtt_);
134 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
135 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200136
137 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
138 if (target_bitrate_bps > 0)
139 ++num_pause_events_;
140 // The protection bitrate is an estimate based on the ratio between media
141 // and protection used before this observer was muted.
142 uint32_t predicted_protection_bps =
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200143 (1.0 - config.media_ratio) * config.config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100144 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
145 << " with configured min bitrate "
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200146 << config.config.min_bitrate_bps
147 << " and current estimate of " << target_bitrate_bps
148 << " and protection bitrate "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200150 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
151 if (target_bitrate_bps > 0)
152 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100153 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200154 << ", configured min bitrate "
155 << config.config.min_bitrate_bps
Mirko Bonadei675513b2017-11-09 11:09:25 +0100156 << ", current allocation " << allocated_bitrate
157 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200158 }
159
160 // Only update the media ratio if the observer got an allocation.
161 if (allocated_bitrate > 0)
162 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
163 config.allocated_bitrate_bps = allocated_bitrate;
164 }
philipel5ef2bc12017-02-21 07:28:31 -0800165 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100166}
167
perkj57c21f92016-06-17 07:27:16 -0700168void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200169 MediaStreamAllocationConfig config) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200170 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200171 RTC_DCHECK_GT(config.bitrate_priority, 0);
172 RTC_DCHECK(std::isnormal(config.bitrate_priority));
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200173 auto it = absl::c_find_if(
174 allocatable_tracks_,
175 [observer](const auto& config) { return config.observer == observer; });
mflodman101f2502016-06-09 17:21:19 +0200176 // Update settings if the observer already exists, create a new one otherwise.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200177 if (it != allocatable_tracks_.end()) {
178 it->config = config;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000179 } else {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200180 allocatable_tracks_.push_back(AllocatableTrack(observer, config));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000181 }
Stefan Holmere5904162015-03-26 11:11:06 +0100182
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100183 if (last_target_bps_ > 0) {
mflodman101f2502016-06-09 17:21:19 +0200184 // Calculate a new allocation and update all observers.
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100185
186 ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
187 ObserverAllocation bandwidth_allocation =
Florent Castelli4e615d52019-08-22 16:09:06 +0200188 AllocateBitrates(last_bandwidth_bps_);
189 ObserverAllocation stable_bitrate_allocation =
190 AllocateBitrates(last_stable_target_bps_);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200191 for (auto& config : allocatable_tracks_) {
mflodman48a4beb2016-07-01 13:03:59 +0200192 uint32_t allocated_bitrate = allocation[config.observer];
Florent Castelli4e615d52019-08-22 16:09:06 +0200193 uint32_t allocated_stable_bitrate =
194 stable_bitrate_allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100195 uint32_t bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100196 BitrateAllocationUpdate update;
197 update.target_bitrate = DataRate::bps(allocated_bitrate);
Florent Castelli4e615d52019-08-22 16:09:06 +0200198 update.stable_target_bitrate = DataRate::bps(allocated_stable_bitrate);
Sebastian Jansson13e59032018-11-21 19:13:07 +0100199 update.link_capacity = DataRate::bps(bandwidth);
200 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
201 update.round_trip_time = TimeDelta::ms(last_rtt_);
202 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
203 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200204 config.allocated_bitrate_bps = allocated_bitrate;
205 if (allocated_bitrate > 0)
206 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
207 }
perkjfea93092016-05-14 00:58:48 -0700208 } else {
209 // Currently, an encoder is not allowed to produce frames.
210 // But we still have to return the initial config bitrate + let the
211 // observer know that it can not produce frames.
Sebastian Jansson13e59032018-11-21 19:13:07 +0100212
213 BitrateAllocationUpdate update;
214 update.target_bitrate = DataRate::Zero();
Florent Castelli4e615d52019-08-22 16:09:06 +0200215 update.stable_target_bitrate = DataRate::Zero();
Sebastian Jansson13e59032018-11-21 19:13:07 +0100216 update.link_capacity = DataRate::Zero();
217 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
218 update.round_trip_time = TimeDelta::ms(last_rtt_);
219 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
220 observer->OnBitrateUpdated(update);
Stefan Holmere5904162015-03-26 11:11:06 +0100221 }
perkj71ee44c2016-06-15 00:47:53 -0700222 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000223}
224
perkj71ee44c2016-06-15 00:47:53 -0700225void BitrateAllocator::UpdateAllocationLimits() {
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200226 BitrateAllocationLimits limits;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200227 for (const auto& config : allocatable_tracks_) {
228 uint32_t stream_padding = config.config.pad_up_bitrate_bps;
229 if (config.config.enforce_min_bitrate) {
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200230 limits.min_allocatable_rate +=
231 DataRate::bps(config.config.min_bitrate_bps);
philipel5ef2bc12017-02-21 07:28:31 -0800232 } else if (config.allocated_bitrate_bps == 0) {
233 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100234 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700235 }
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200236 limits.max_padding_rate += DataRate::bps(stream_padding);
237 limits.max_allocatable_rate += DataRate::bps(config.config.max_bitrate_bps);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000238 }
perkj71ee44c2016-06-15 00:47:53 -0700239
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200240 if (limits.min_allocatable_rate == current_limits_.min_allocatable_rate &&
241 limits.max_allocatable_rate == current_limits_.max_allocatable_rate &&
242 limits.max_padding_rate == current_limits_.max_padding_rate) {
philipel5ef2bc12017-02-21 07:28:31 -0800243 return;
244 }
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200245 current_limits_ = limits;
philipel5ef2bc12017-02-21 07:28:31 -0800246
Mirko Bonadei675513b2017-11-09 11:09:25 +0100247 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200248 << ToString(limits.min_allocatable_rate)
249 << ", total_requested_padding_bitrate: "
250 << ToString(limits.max_padding_rate)
251 << ", total_requested_max_bitrate: "
252 << ToString(limits.max_allocatable_rate);
253
254 limit_observer_->OnAllocationLimitsChanged(limits);
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_);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200259 for (auto it = allocatable_tracks_.begin(); it != allocatable_tracks_.end();
260 ++it) {
261 if (it->observer == observer) {
262 allocatable_tracks_.erase(it);
263 break;
264 }
perkj71ee44c2016-06-15 00:47:53 -0700265 }
perkj26091b12016-09-01 01:17:40 -0700266
perkj71ee44c2016-06-15 00:47:53 -0700267 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000268}
269
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200270int BitrateAllocator::GetStartBitrate(
271 BitrateAllocatorObserver* observer) const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200272 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200273 auto it = absl::c_find_if(
274 allocatable_tracks_,
275 [observer](const auto& config) { return config.observer == observer; });
276 if (it == allocatable_tracks_.end()) {
mflodman48a4beb2016-07-01 13:03:59 +0200277 // This observer hasn't been added yet, just give it its fair share.
278 return last_non_zero_bitrate_bps_ /
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200279 static_cast<int>((allocatable_tracks_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200280 } else if (it->allocated_bitrate_bps == -1) {
281 // This observer hasn't received an allocation yet, so do the same.
282 return last_non_zero_bitrate_bps_ /
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200283 static_cast<int>(allocatable_tracks_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200284 } else {
285 // This observer already has an allocation.
286 return it->allocated_bitrate_bps;
287 }
perkj57c21f92016-06-17 07:27:16 -0700288}
289
perkjfea93092016-05-14 00:58:48 -0700290BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200291 uint32_t bitrate) const {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200292 if (allocatable_tracks_.empty())
mflodman2ebe5b12016-05-13 01:43:51 -0700293 return ObserverAllocation();
294
perkjfea93092016-05-14 00:58:48 -0700295 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700296 return ZeroRateAllocation();
297
298 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200299 uint32_t sum_max_bitrates = 0;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200300 for (const auto& observer_config : allocatable_tracks_) {
301 sum_min_bitrates += observer_config.config.min_bitrate_bps;
302 sum_max_bitrates += observer_config.config.max_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200303 }
304
305 // Not enough for all observers to get an allocation, allocate according to:
306 // enforced min bitrate -> allocated bitrate previous round -> restart paused
307 // streams.
308 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700309 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700310
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800311 // All observers will get their min bitrate plus a share of the rest. This
312 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200313 if (bitrate <= sum_max_bitrates)
314 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700315
Ying Wanga646d302018-03-02 17:04:11 +0100316 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200317 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000318}
319
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200320BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
321 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700322 ObserverAllocation allocation;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200323 for (const auto& observer_config : allocatable_tracks_)
mflodman2ebe5b12016-05-13 01:43:51 -0700324 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700325 return allocation;
326}
327
mflodman2ebe5b12016-05-13 01:43:51 -0700328BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200329 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700330 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200331 // Start by allocating bitrate to observers enforcing a min bitrate, hence
332 // remaining_bitrate might turn negative.
333 int64_t remaining_bitrate = bitrate;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200334 for (const auto& observer_config : allocatable_tracks_) {
mflodman101f2502016-06-09 17:21:19 +0200335 int32_t allocated_bitrate = 0;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200336 if (observer_config.config.enforce_min_bitrate)
337 allocated_bitrate = observer_config.config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200338
339 allocation[observer_config.observer] = allocated_bitrate;
340 remaining_bitrate -= allocated_bitrate;
341 }
342
343 // Allocate bitrate to all previously active streams.
344 if (remaining_bitrate > 0) {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200345 for (const auto& observer_config : allocatable_tracks_) {
346 if (observer_config.config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100347 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200348 continue;
349
srte1eb051c2017-11-29 11:23:59 +0100350 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200351 if (remaining_bitrate >= required_bitrate) {
352 allocation[observer_config.observer] = required_bitrate;
353 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200354 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000355 }
356 }
mflodman101f2502016-06-09 17:21:19 +0200357
358 // Allocate bitrate to previously paused streams.
359 if (remaining_bitrate > 0) {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200360 for (const auto& observer_config : allocatable_tracks_) {
srte1eb051c2017-11-29 11:23:59 +0100361 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200362 continue;
363
364 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100365 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200366 if (remaining_bitrate >= required_bitrate) {
367 allocation[observer_config.observer] = required_bitrate;
368 remaining_bitrate -= required_bitrate;
369 }
370 }
371 }
372
373 // Split a possible remainder evenly on all streams with an allocation.
374 if (remaining_bitrate > 0)
375 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
376
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200377 RTC_DCHECK_EQ(allocation.size(), allocatable_tracks_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100378 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000379}
mflodman101f2502016-06-09 17:21:19 +0200380
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800381// Allocates the bitrate based on the bitrate priority of each observer. This
382// bitrate priority defines the priority for bitrate to be allocated to that
383// observer in relation to other observers. For example with two observers, if
384// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
385// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
386// allocated twice the bitrate as observer 1 above the each observer's
387// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200388BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
389 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200390 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200391 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800392 ObserverAllocation observers_capacities;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200393 for (const auto& observer_config : allocatable_tracks_) {
394 allocation[observer_config.observer] =
395 observer_config.config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800396 observers_capacities[observer_config.observer] =
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200397 observer_config.config.max_bitrate_bps -
398 observer_config.config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800399 }
mflodman101f2502016-06-09 17:21:19 +0200400
401 bitrate -= sum_min_bitrates;
Sebastian Jansson464a5572019-02-12 13:32:32 +0100402
403 // TODO(srte): Implement fair sharing between prioritized streams, currently
404 // they are treated on a first come first serve basis.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200405 for (const auto& observer_config : allocatable_tracks_) {
406 int64_t priority_margin = observer_config.config.priority_bitrate_bps -
Sebastian Jansson464a5572019-02-12 13:32:32 +0100407 allocation[observer_config.observer];
408 if (priority_margin > 0 && bitrate > 0) {
409 int64_t extra_bitrate = std::min<int64_t>(priority_margin, bitrate);
410 allocation[observer_config.observer] +=
411 rtc::dchecked_cast<int>(extra_bitrate);
412 observers_capacities[observer_config.observer] -= extra_bitrate;
413 bitrate -= extra_bitrate;
414 }
415 }
416
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800417 // From the remaining bitrate, allocate a proportional amount to each observer
418 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200419 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800420 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200421
422 return allocation;
423}
424
425BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700426 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200427 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200428 ObserverAllocation allocation;
429
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200430 for (const auto& observer_config : allocatable_tracks_) {
431 allocation[observer_config.observer] =
432 observer_config.config.max_bitrate_bps;
433 bitrate -= observer_config.config.max_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200434 }
Ying Wanga646d302018-03-02 17:04:11 +0100435 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200436 &allocation);
437 return allocation;
438}
439
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200440uint32_t BitrateAllocator::AllocatableTrack::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200441 // Return the configured minimum bitrate for newly added observers, to avoid
442 // requiring an extra high bitrate for the observer to get an allocated
443 // bitrate.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200444 return allocated_bitrate_bps == -1 ? config.min_bitrate_bps
445 : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200446}
447
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200448uint32_t BitrateAllocator::AllocatableTrack::MinBitrateWithHysteresis() const {
449 uint32_t min_bitrate = config.min_bitrate_bps;
srte1eb051c2017-11-29 11:23:59 +0100450 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200451 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
452 kMinToggleBitrateBps);
453 }
mflodman48a4beb2016-07-01 13:03:59 +0200454 // Account for protection bitrate used by this observer in the previous
455 // allocation.
456 // Note: the ratio will only be updated when the stream is active, meaning a
457 // paused stream won't get any ratio updates. This might lead to waiting a bit
458 // longer than necessary if the network condition improves, but this is to
459 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100460 if (media_ratio > 0.0 && media_ratio < 1.0)
461 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200462
mflodman101f2502016-06-09 17:21:19 +0200463 return min_bitrate;
464}
465
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200466void BitrateAllocator::DistributeBitrateEvenly(
467 uint32_t bitrate,
468 bool include_zero_allocations,
469 int max_multiplier,
470 ObserverAllocation* allocation) const {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200471 RTC_DCHECK_EQ(allocation->size(), allocatable_tracks_.size());
mflodman101f2502016-06-09 17:21:19 +0200472
473 ObserverSortingMap list_max_bitrates;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200474 for (const auto& observer_config : allocatable_tracks_) {
mflodman101f2502016-06-09 17:21:19 +0200475 if (include_zero_allocations ||
476 allocation->at(observer_config.observer) != 0) {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200477 list_max_bitrates.insert(std::pair<uint32_t, const AllocatableTrack*>(
478 observer_config.config.max_bitrate_bps, &observer_config));
mflodman101f2502016-06-09 17:21:19 +0200479 }
480 }
481 auto it = list_max_bitrates.begin();
482 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800483 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200484 uint32_t extra_allocation =
485 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
486 uint32_t total_allocation =
487 extra_allocation + allocation->at(it->second->observer);
488 bitrate -= extra_allocation;
489 if (total_allocation > max_multiplier * it->first) {
490 // There is more than we can fit for this observer, carry over to the
491 // remaining observers.
492 bitrate += total_allocation - max_multiplier * it->first;
493 total_allocation = max_multiplier * it->first;
494 }
495 // Finally, update the allocation for this observer.
496 allocation->at(it->second->observer) = total_allocation;
497 it = list_max_bitrates.erase(it);
498 }
499}
500
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200501bool BitrateAllocator::EnoughBitrateForAllObservers(
502 uint32_t bitrate,
503 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200504 if (bitrate < sum_min_bitrates)
505 return false;
506
perkj26091b12016-09-01 01:17:40 -0700507 uint32_t extra_bitrate_per_observer =
508 (bitrate - sum_min_bitrates) /
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200509 static_cast<uint32_t>(allocatable_tracks_.size());
510 for (const auto& observer_config : allocatable_tracks_) {
511 if (observer_config.config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100512 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200513 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800514 }
mflodman101f2502016-06-09 17:21:19 +0200515 }
516 return true;
517}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800518
519void BitrateAllocator::DistributeBitrateRelatively(
520 uint32_t remaining_bitrate,
521 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200522 ObserverAllocation* allocation) const {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200523 RTC_DCHECK_EQ(allocation->size(), allocatable_tracks_.size());
524 RTC_DCHECK_EQ(observers_capacities.size(), allocatable_tracks_.size());
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800525
526 struct PriorityRateObserverConfig {
527 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
528 uint32_t capacity_bps,
529 double bitrate_priority)
530 : allocation_key(allocation_key),
531 capacity_bps(capacity_bps),
532 bitrate_priority(bitrate_priority) {}
533
534 BitrateAllocatorObserver* allocation_key;
535 // The amount of bitrate bps that can be allocated to this observer.
536 uint32_t capacity_bps;
537 double bitrate_priority;
538
539 // We want to sort by which observers will be allocated their full capacity
540 // first. By dividing each observer's capacity by its bitrate priority we
541 // are "normalizing" the capacity of an observer by the rate it will be
542 // filled. This is because the amount allocated is based upon bitrate
543 // priority. We allocate twice as much bitrate to an observer with twice the
544 // bitrate priority of another.
545 bool operator<(const PriorityRateObserverConfig& other) const {
546 return capacity_bps / bitrate_priority <
547 other.capacity_bps / other.bitrate_priority;
548 }
549 };
550
551 double bitrate_priority_sum = 0;
552 std::vector<PriorityRateObserverConfig> priority_rate_observers;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200553 for (const auto& observer_config : allocatable_tracks_) {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800554 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200555 priority_rate_observers.emplace_back(
556 observer_config.observer, capacity_bps,
557 observer_config.config.bitrate_priority);
558 bitrate_priority_sum += observer_config.config.bitrate_priority;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800559 }
560
561 // Iterate in the order observers can be allocated their full capacity.
562 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
563 size_t i;
564 for (i = 0; i < priority_rate_observers.size(); ++i) {
565 const auto& priority_rate_observer = priority_rate_observers[i];
566 // We allocate the full capacity to an observer only if its relative
567 // portion from the remaining bitrate is sufficient to allocate its full
568 // capacity. This means we aren't greedily allocating the full capacity, but
569 // that it is only done when there is also enough bitrate to allocate the
570 // proportional amounts to all other observers.
571 double observer_share =
572 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
573 double allocation_bps = observer_share * remaining_bitrate;
574 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
575 if (!enough_bitrate)
576 break;
577 allocation->at(priority_rate_observer.allocation_key) +=
578 priority_rate_observer.capacity_bps;
579 remaining_bitrate -= priority_rate_observer.capacity_bps;
580 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
581 }
582
583 // From the remaining bitrate, allocate the proportional amounts to the
584 // observers that aren't allocated their max capacity.
585 for (; i < priority_rate_observers.size(); ++i) {
586 const auto& priority_rate_observer = priority_rate_observers[i];
587 double fraction_allocated =
588 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
589 allocation->at(priority_rate_observer.allocation_key) +=
590 fraction_allocated * remaining_bitrate;
591 }
592}
593
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000594} // namespace webrtc