blob: a5259f240c39bc84e6ae8ed96bc20b4b63fa17c3 [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),
66 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020067 total_requested_min_bitrate_(0),
Sebastian Jansson448f4d52018-04-04 14:52:07 +020068 total_requested_max_bitrate_(0),
Ying Wanga646d302018-03-02 17:04:11 +010069 transmission_max_bitrate_multiplier_(
Jonas Olsson0182a032019-07-09 12:31:20 +020070 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070071 sequenced_checker_.Detach();
72}
mflodman48a4beb2016-07-01 13:03:59 +020073
74BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070075 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
76 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020077}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000078
Sebastian Jansson2701bc92018-12-11 15:02:47 +010079void BitrateAllocator::UpdateStartRate(uint32_t start_rate_bps) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020080 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson2701bc92018-12-11 15:02:47 +010081 last_non_zero_bitrate_bps_ = start_rate_bps;
82}
83
Niels Möller74e5f802018-04-25 14:03:46 +020084// static
Ying Wanga646d302018-03-02 17:04:11 +010085uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
86 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
87 "WebRTC-TransmissionMaxBitrateMultiplier")
88 .c_str(),
89 nullptr, 10);
90 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010091 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
92 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010093 return static_cast<uint8_t>(multiplier);
94 }
95 return kTransmissionMaxBitrateMultiplier;
96}
97
perkj71ee44c2016-06-15 00:47:53 -070098void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
Florent Castelli4e615d52019-08-22 16:09:06 +020099 uint32_t stable_target_bitrate_bps,
100 uint32_t bandwidth_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;
Florent Castelli4e615d52019-08-22 16:09:06 +0200106 last_bandwidth_bps_ = bandwidth_bps;
107 last_stable_target_bps_ = stable_target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -0700108 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +0200109 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +0100110 last_fraction_loss_ = fraction_loss;
111 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700112 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700113
mflodman48a4beb2016-07-01 13:03:59 +0200114 // Periodically log the incoming BWE.
115 int64_t now = clock_->TimeInMilliseconds();
116 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100117 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200118 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800119 }
mflodman48a4beb2016-07-01 13:03:59 +0200120
121 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
Florent Castelli4e615d52019-08-22 16:09:06 +0200122 ObserverAllocation bandwidth_allocation = AllocateBitrates(bandwidth_bps);
123 ObserverAllocation stable_bitrate_allocation =
124 AllocateBitrates(stable_target_bitrate_bps);
mflodman48a4beb2016-07-01 13:03:59 +0200125
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200126 for (auto& config : allocatable_tracks_) {
mflodman48a4beb2016-07-01 13:03:59 +0200127 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100128 uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
Florent Castelli4e615d52019-08-22 16:09:06 +0200129 uint32_t allocated_stable_target_rate =
130 stable_bitrate_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100131 BitrateAllocationUpdate update;
132 update.target_bitrate = DataRate::bps(allocated_bitrate);
Florent Castelli4e615d52019-08-22 16:09:06 +0200133 update.stable_target_bitrate = DataRate::bps(allocated_stable_target_rate);
Sebastian Jansson13e59032018-11-21 19:13:07 +0100134 update.link_capacity = DataRate::bps(allocated_bandwidth);
135 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
136 update.round_trip_time = TimeDelta::ms(last_rtt_);
137 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
138 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200139
140 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
141 if (target_bitrate_bps > 0)
142 ++num_pause_events_;
143 // The protection bitrate is an estimate based on the ratio between media
144 // and protection used before this observer was muted.
145 uint32_t predicted_protection_bps =
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200146 (1.0 - config.media_ratio) * config.config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100147 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
148 << " with configured min bitrate "
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200149 << config.config.min_bitrate_bps
150 << " and current estimate of " << target_bitrate_bps
151 << " and protection bitrate "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200153 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
154 if (target_bitrate_bps > 0)
155 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100156 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200157 << ", configured min bitrate "
158 << config.config.min_bitrate_bps
Mirko Bonadei675513b2017-11-09 11:09:25 +0100159 << ", current allocation " << allocated_bitrate
160 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200161 }
162
163 // Only update the media ratio if the observer got an allocation.
164 if (allocated_bitrate > 0)
165 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
166 config.allocated_bitrate_bps = allocated_bitrate;
167 }
philipel5ef2bc12017-02-21 07:28:31 -0800168 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100169}
170
perkj57c21f92016-06-17 07:27:16 -0700171void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200172 MediaStreamAllocationConfig config) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200173 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200174 RTC_DCHECK_GT(config.bitrate_priority, 0);
175 RTC_DCHECK(std::isnormal(config.bitrate_priority));
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200176 auto it = absl::c_find_if(
177 allocatable_tracks_,
178 [observer](const auto& config) { return config.observer == observer; });
mflodman101f2502016-06-09 17:21:19 +0200179 // Update settings if the observer already exists, create a new one otherwise.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200180 if (it != allocatable_tracks_.end()) {
181 it->config = config;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000182 } else {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200183 allocatable_tracks_.push_back(AllocatableTrack(observer, config));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000184 }
Stefan Holmere5904162015-03-26 11:11:06 +0100185
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100186 if (last_target_bps_ > 0) {
mflodman101f2502016-06-09 17:21:19 +0200187 // Calculate a new allocation and update all observers.
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100188
189 ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
190 ObserverAllocation bandwidth_allocation =
Florent Castelli4e615d52019-08-22 16:09:06 +0200191 AllocateBitrates(last_bandwidth_bps_);
192 ObserverAllocation stable_bitrate_allocation =
193 AllocateBitrates(last_stable_target_bps_);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200194 for (auto& config : allocatable_tracks_) {
mflodman48a4beb2016-07-01 13:03:59 +0200195 uint32_t allocated_bitrate = allocation[config.observer];
Florent Castelli4e615d52019-08-22 16:09:06 +0200196 uint32_t allocated_stable_bitrate =
197 stable_bitrate_allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100198 uint32_t bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100199 BitrateAllocationUpdate update;
200 update.target_bitrate = DataRate::bps(allocated_bitrate);
Florent Castelli4e615d52019-08-22 16:09:06 +0200201 update.stable_target_bitrate = DataRate::bps(allocated_stable_bitrate);
Sebastian Jansson13e59032018-11-21 19:13:07 +0100202 update.link_capacity = DataRate::bps(bandwidth);
203 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
204 update.round_trip_time = TimeDelta::ms(last_rtt_);
205 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
206 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200207 config.allocated_bitrate_bps = allocated_bitrate;
208 if (allocated_bitrate > 0)
209 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
210 }
perkjfea93092016-05-14 00:58:48 -0700211 } else {
212 // Currently, an encoder is not allowed to produce frames.
213 // But we still have to return the initial config bitrate + let the
214 // observer know that it can not produce frames.
Sebastian Jansson13e59032018-11-21 19:13:07 +0100215
216 BitrateAllocationUpdate update;
217 update.target_bitrate = DataRate::Zero();
Florent Castelli4e615d52019-08-22 16:09:06 +0200218 update.stable_target_bitrate = DataRate::Zero();
Sebastian Jansson13e59032018-11-21 19:13:07 +0100219 update.link_capacity = DataRate::Zero();
220 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
221 update.round_trip_time = TimeDelta::ms(last_rtt_);
222 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
223 observer->OnBitrateUpdated(update);
Stefan Holmere5904162015-03-26 11:11:06 +0100224 }
perkj71ee44c2016-06-15 00:47:53 -0700225 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000226}
227
perkj71ee44c2016-06-15 00:47:53 -0700228void BitrateAllocator::UpdateAllocationLimits() {
229 uint32_t total_requested_padding_bitrate = 0;
230 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200231 uint32_t total_requested_max_bitrate = 0;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200232 for (const auto& config : allocatable_tracks_) {
233 uint32_t stream_padding = config.config.pad_up_bitrate_bps;
234 if (config.config.enforce_min_bitrate) {
235 total_requested_min_bitrate += config.config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800236 } else if (config.allocated_bitrate_bps == 0) {
237 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100238 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700239 }
philipel5ef2bc12017-02-21 07:28:31 -0800240 total_requested_padding_bitrate += stream_padding;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200241 total_requested_max_bitrate += config.config.max_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000242 }
perkj71ee44c2016-06-15 00:47:53 -0700243
philipel5ef2bc12017-02-21 07:28:31 -0800244 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100245 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100246 total_requested_max_bitrate == total_requested_max_bitrate_) {
philipel5ef2bc12017-02-21 07:28:31 -0800247 return;
248 }
249
250 total_requested_min_bitrate_ = total_requested_min_bitrate;
251 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200252 total_requested_max_bitrate_ = total_requested_max_bitrate;
philipel5ef2bc12017-02-21 07:28:31 -0800253
Mirko Bonadei675513b2017-11-09 11:09:25 +0100254 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
255 << total_requested_min_bitrate
256 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200257 << total_requested_padding_bitrate
258 << "bps, total_requested_max_bitrate: "
259 << total_requested_max_bitrate << "bps";
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100260 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
261 total_requested_padding_bitrate,
262 total_requested_max_bitrate);
perkj71ee44c2016-06-15 00:47:53 -0700263}
264
265void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200266 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200267 for (auto it = allocatable_tracks_.begin(); it != allocatable_tracks_.end();
268 ++it) {
269 if (it->observer == observer) {
270 allocatable_tracks_.erase(it);
271 break;
272 }
perkj71ee44c2016-06-15 00:47:53 -0700273 }
perkj26091b12016-09-01 01:17:40 -0700274
perkj71ee44c2016-06-15 00:47:53 -0700275 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000276}
277
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200278int BitrateAllocator::GetStartBitrate(
279 BitrateAllocatorObserver* observer) const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200280 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200281 auto it = absl::c_find_if(
282 allocatable_tracks_,
283 [observer](const auto& config) { return config.observer == observer; });
284 if (it == allocatable_tracks_.end()) {
mflodman48a4beb2016-07-01 13:03:59 +0200285 // This observer hasn't been added yet, just give it its fair share.
286 return last_non_zero_bitrate_bps_ /
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200287 static_cast<int>((allocatable_tracks_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200288 } else if (it->allocated_bitrate_bps == -1) {
289 // This observer hasn't received an allocation yet, so do the same.
290 return last_non_zero_bitrate_bps_ /
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200291 static_cast<int>(allocatable_tracks_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200292 } else {
293 // This observer already has an allocation.
294 return it->allocated_bitrate_bps;
295 }
perkj57c21f92016-06-17 07:27:16 -0700296}
297
perkjfea93092016-05-14 00:58:48 -0700298BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200299 uint32_t bitrate) const {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200300 if (allocatable_tracks_.empty())
mflodman2ebe5b12016-05-13 01:43:51 -0700301 return ObserverAllocation();
302
perkjfea93092016-05-14 00:58:48 -0700303 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700304 return ZeroRateAllocation();
305
306 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200307 uint32_t sum_max_bitrates = 0;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200308 for (const auto& observer_config : allocatable_tracks_) {
309 sum_min_bitrates += observer_config.config.min_bitrate_bps;
310 sum_max_bitrates += observer_config.config.max_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200311 }
312
313 // Not enough for all observers to get an allocation, allocate according to:
314 // enforced min bitrate -> allocated bitrate previous round -> restart paused
315 // streams.
316 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700317 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700318
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800319 // All observers will get their min bitrate plus a share of the rest. This
320 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200321 if (bitrate <= sum_max_bitrates)
322 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700323
Ying Wanga646d302018-03-02 17:04:11 +0100324 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200325 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000326}
327
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200328BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
329 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700330 ObserverAllocation allocation;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200331 for (const auto& observer_config : allocatable_tracks_)
mflodman2ebe5b12016-05-13 01:43:51 -0700332 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700333 return allocation;
334}
335
mflodman2ebe5b12016-05-13 01:43:51 -0700336BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200337 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700338 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200339 // Start by allocating bitrate to observers enforcing a min bitrate, hence
340 // remaining_bitrate might turn negative.
341 int64_t remaining_bitrate = bitrate;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200342 for (const auto& observer_config : allocatable_tracks_) {
mflodman101f2502016-06-09 17:21:19 +0200343 int32_t allocated_bitrate = 0;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200344 if (observer_config.config.enforce_min_bitrate)
345 allocated_bitrate = observer_config.config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200346
347 allocation[observer_config.observer] = allocated_bitrate;
348 remaining_bitrate -= allocated_bitrate;
349 }
350
351 // Allocate bitrate to all previously active streams.
352 if (remaining_bitrate > 0) {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200353 for (const auto& observer_config : allocatable_tracks_) {
354 if (observer_config.config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100355 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200356 continue;
357
srte1eb051c2017-11-29 11:23:59 +0100358 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200359 if (remaining_bitrate >= required_bitrate) {
360 allocation[observer_config.observer] = required_bitrate;
361 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200362 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000363 }
364 }
mflodman101f2502016-06-09 17:21:19 +0200365
366 // Allocate bitrate to previously paused streams.
367 if (remaining_bitrate > 0) {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200368 for (const auto& observer_config : allocatable_tracks_) {
srte1eb051c2017-11-29 11:23:59 +0100369 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200370 continue;
371
372 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100373 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200374 if (remaining_bitrate >= required_bitrate) {
375 allocation[observer_config.observer] = required_bitrate;
376 remaining_bitrate -= required_bitrate;
377 }
378 }
379 }
380
381 // Split a possible remainder evenly on all streams with an allocation.
382 if (remaining_bitrate > 0)
383 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
384
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200385 RTC_DCHECK_EQ(allocation.size(), allocatable_tracks_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100386 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000387}
mflodman101f2502016-06-09 17:21:19 +0200388
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800389// Allocates the bitrate based on the bitrate priority of each observer. This
390// bitrate priority defines the priority for bitrate to be allocated to that
391// observer in relation to other observers. For example with two observers, if
392// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
393// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
394// allocated twice the bitrate as observer 1 above the each observer's
395// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200396BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
397 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200398 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200399 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800400 ObserverAllocation observers_capacities;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200401 for (const auto& observer_config : allocatable_tracks_) {
402 allocation[observer_config.observer] =
403 observer_config.config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800404 observers_capacities[observer_config.observer] =
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200405 observer_config.config.max_bitrate_bps -
406 observer_config.config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800407 }
mflodman101f2502016-06-09 17:21:19 +0200408
409 bitrate -= sum_min_bitrates;
Sebastian Jansson464a5572019-02-12 13:32:32 +0100410
411 // TODO(srte): Implement fair sharing between prioritized streams, currently
412 // they are treated on a first come first serve basis.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200413 for (const auto& observer_config : allocatable_tracks_) {
414 int64_t priority_margin = observer_config.config.priority_bitrate_bps -
Sebastian Jansson464a5572019-02-12 13:32:32 +0100415 allocation[observer_config.observer];
416 if (priority_margin > 0 && bitrate > 0) {
417 int64_t extra_bitrate = std::min<int64_t>(priority_margin, bitrate);
418 allocation[observer_config.observer] +=
419 rtc::dchecked_cast<int>(extra_bitrate);
420 observers_capacities[observer_config.observer] -= extra_bitrate;
421 bitrate -= extra_bitrate;
422 }
423 }
424
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800425 // From the remaining bitrate, allocate a proportional amount to each observer
426 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200427 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800428 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200429
430 return allocation;
431}
432
433BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700434 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200435 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200436 ObserverAllocation allocation;
437
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200438 for (const auto& observer_config : allocatable_tracks_) {
439 allocation[observer_config.observer] =
440 observer_config.config.max_bitrate_bps;
441 bitrate -= observer_config.config.max_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200442 }
Ying Wanga646d302018-03-02 17:04:11 +0100443 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200444 &allocation);
445 return allocation;
446}
447
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200448uint32_t BitrateAllocator::AllocatableTrack::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200449 // Return the configured minimum bitrate for newly added observers, to avoid
450 // requiring an extra high bitrate for the observer to get an allocated
451 // bitrate.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200452 return allocated_bitrate_bps == -1 ? config.min_bitrate_bps
453 : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200454}
455
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200456uint32_t BitrateAllocator::AllocatableTrack::MinBitrateWithHysteresis() const {
457 uint32_t min_bitrate = config.min_bitrate_bps;
srte1eb051c2017-11-29 11:23:59 +0100458 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200459 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
460 kMinToggleBitrateBps);
461 }
mflodman48a4beb2016-07-01 13:03:59 +0200462 // Account for protection bitrate used by this observer in the previous
463 // allocation.
464 // Note: the ratio will only be updated when the stream is active, meaning a
465 // paused stream won't get any ratio updates. This might lead to waiting a bit
466 // longer than necessary if the network condition improves, but this is to
467 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100468 if (media_ratio > 0.0 && media_ratio < 1.0)
469 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200470
mflodman101f2502016-06-09 17:21:19 +0200471 return min_bitrate;
472}
473
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200474void BitrateAllocator::DistributeBitrateEvenly(
475 uint32_t bitrate,
476 bool include_zero_allocations,
477 int max_multiplier,
478 ObserverAllocation* allocation) const {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200479 RTC_DCHECK_EQ(allocation->size(), allocatable_tracks_.size());
mflodman101f2502016-06-09 17:21:19 +0200480
481 ObserverSortingMap list_max_bitrates;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200482 for (const auto& observer_config : allocatable_tracks_) {
mflodman101f2502016-06-09 17:21:19 +0200483 if (include_zero_allocations ||
484 allocation->at(observer_config.observer) != 0) {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200485 list_max_bitrates.insert(std::pair<uint32_t, const AllocatableTrack*>(
486 observer_config.config.max_bitrate_bps, &observer_config));
mflodman101f2502016-06-09 17:21:19 +0200487 }
488 }
489 auto it = list_max_bitrates.begin();
490 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800491 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200492 uint32_t extra_allocation =
493 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
494 uint32_t total_allocation =
495 extra_allocation + allocation->at(it->second->observer);
496 bitrate -= extra_allocation;
497 if (total_allocation > max_multiplier * it->first) {
498 // There is more than we can fit for this observer, carry over to the
499 // remaining observers.
500 bitrate += total_allocation - max_multiplier * it->first;
501 total_allocation = max_multiplier * it->first;
502 }
503 // Finally, update the allocation for this observer.
504 allocation->at(it->second->observer) = total_allocation;
505 it = list_max_bitrates.erase(it);
506 }
507}
508
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200509bool BitrateAllocator::EnoughBitrateForAllObservers(
510 uint32_t bitrate,
511 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200512 if (bitrate < sum_min_bitrates)
513 return false;
514
perkj26091b12016-09-01 01:17:40 -0700515 uint32_t extra_bitrate_per_observer =
516 (bitrate - sum_min_bitrates) /
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200517 static_cast<uint32_t>(allocatable_tracks_.size());
518 for (const auto& observer_config : allocatable_tracks_) {
519 if (observer_config.config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100520 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200521 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800522 }
mflodman101f2502016-06-09 17:21:19 +0200523 }
524 return true;
525}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800526
527void BitrateAllocator::DistributeBitrateRelatively(
528 uint32_t remaining_bitrate,
529 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200530 ObserverAllocation* allocation) const {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200531 RTC_DCHECK_EQ(allocation->size(), allocatable_tracks_.size());
532 RTC_DCHECK_EQ(observers_capacities.size(), allocatable_tracks_.size());
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800533
534 struct PriorityRateObserverConfig {
535 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
536 uint32_t capacity_bps,
537 double bitrate_priority)
538 : allocation_key(allocation_key),
539 capacity_bps(capacity_bps),
540 bitrate_priority(bitrate_priority) {}
541
542 BitrateAllocatorObserver* allocation_key;
543 // The amount of bitrate bps that can be allocated to this observer.
544 uint32_t capacity_bps;
545 double bitrate_priority;
546
547 // We want to sort by which observers will be allocated their full capacity
548 // first. By dividing each observer's capacity by its bitrate priority we
549 // are "normalizing" the capacity of an observer by the rate it will be
550 // filled. This is because the amount allocated is based upon bitrate
551 // priority. We allocate twice as much bitrate to an observer with twice the
552 // bitrate priority of another.
553 bool operator<(const PriorityRateObserverConfig& other) const {
554 return capacity_bps / bitrate_priority <
555 other.capacity_bps / other.bitrate_priority;
556 }
557 };
558
559 double bitrate_priority_sum = 0;
560 std::vector<PriorityRateObserverConfig> priority_rate_observers;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200561 for (const auto& observer_config : allocatable_tracks_) {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800562 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200563 priority_rate_observers.emplace_back(
564 observer_config.observer, capacity_bps,
565 observer_config.config.bitrate_priority);
566 bitrate_priority_sum += observer_config.config.bitrate_priority;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800567 }
568
569 // Iterate in the order observers can be allocated their full capacity.
570 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
571 size_t i;
572 for (i = 0; i < priority_rate_observers.size(); ++i) {
573 const auto& priority_rate_observer = priority_rate_observers[i];
574 // We allocate the full capacity to an observer only if its relative
575 // portion from the remaining bitrate is sufficient to allocate its full
576 // capacity. This means we aren't greedily allocating the full capacity, but
577 // that it is only done when there is also enough bitrate to allocate the
578 // proportional amounts to all other observers.
579 double observer_share =
580 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
581 double allocation_bps = observer_share * remaining_bitrate;
582 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
583 if (!enough_bitrate)
584 break;
585 allocation->at(priority_rate_observer.allocation_key) +=
586 priority_rate_observer.capacity_bps;
587 remaining_bitrate -= priority_rate_observer.capacity_bps;
588 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
589 }
590
591 // From the remaining bitrate, allocate the proportional amounts to the
592 // observers that aren't allocated their max capacity.
593 for (; i < priority_rate_observers.size(); ++i) {
594 const auto& priority_rate_observer = priority_rate_observers[i];
595 double fraction_allocated =
596 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
597 allocation->at(priority_rate_observer.allocation_key) +=
598 fraction_allocated * remaining_bitrate;
599 }
600}
601
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000602} // namespace webrtc