blob: 06b74ba4e484a91d3177c2a4da21a8d45e3cef31 [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 "modules/bitrate_controller/include/bitrate_controller.h"
22#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
perkj71ee44c2016-06-15 00:47:53 -070054BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
55 : limit_observer_(limit_observer),
Sebastian Jansson89c94b92018-11-20 17:16:36 +010056 last_target_bps_(0),
57 last_link_capacity_bps_(0),
perkjfea93092016-05-14 00:58:48 -070058 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010059 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020060 last_rtt_(0),
Sebastian Jansson13e59032018-11-21 19:13:07 +010061 last_bwe_period_ms_(1000),
mflodman48a4beb2016-07-01 13:03:59 +020062 num_pause_events_(0),
63 clock_(Clock::GetRealTimeClock()),
philipel5ef2bc12017-02-21 07:28:31 -080064 last_bwe_log_time_(0),
65 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020066 total_requested_min_bitrate_(0),
Sebastian Jansson448f4d52018-04-04 14:52:07 +020067 total_requested_max_bitrate_(0),
Ying Wanga646d302018-03-02 17:04:11 +010068 bitrate_allocation_strategy_(nullptr),
69 transmission_max_bitrate_multiplier_(
70 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) {
80 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
81 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,
Sebastian Jansson89c94b92018-11-20 17:16:36 +010099 uint32_t link_capacity_bps,
perkj71ee44c2016-06-15 00:47:53 -0700100 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800101 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700102 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -0700103 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100104 last_target_bps_ = target_bitrate_bps;
105 last_link_capacity_bps_ = link_capacity_bps;
perkjfea93092016-05-14 00:58:48 -0700106 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +0200107 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +0100108 last_fraction_loss_ = fraction_loss;
109 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700110 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700111
mflodman48a4beb2016-07-01 13:03:59 +0200112 // Periodically log the incoming BWE.
113 int64_t now = clock_->TimeInMilliseconds();
114 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200116 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800117 }
mflodman48a4beb2016-07-01 13:03:59 +0200118
119 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100120 ObserverAllocation bandwidth_allocation = AllocateBitrates(link_capacity_bps);
mflodman48a4beb2016-07-01 13:03:59 +0200121
122 for (auto& config : bitrate_observer_configs_) {
123 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100124 uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100125 BitrateAllocationUpdate update;
126 update.target_bitrate = DataRate::bps(allocated_bitrate);
127 update.link_capacity = DataRate::bps(allocated_bandwidth);
128 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
129 update.round_trip_time = TimeDelta::ms(last_rtt_);
130 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
131 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200132
133 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
134 if (target_bitrate_bps > 0)
135 ++num_pause_events_;
136 // The protection bitrate is an estimate based on the ratio between media
137 // and protection used before this observer was muted.
138 uint32_t predicted_protection_bps =
139 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100140 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
141 << " with configured min bitrate "
142 << config.min_bitrate_bps << " and current estimate of "
143 << target_bitrate_bps << " and protection bitrate "
144 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200145 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
146 if (target_bitrate_bps > 0)
147 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100148 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
149 << ", configured min bitrate " << config.min_bitrate_bps
150 << ", current allocation " << allocated_bitrate
151 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200152 }
153
154 // Only update the media ratio if the observer got an allocation.
155 if (allocated_bitrate > 0)
156 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
157 config.allocated_bitrate_bps = allocated_bitrate;
158 }
philipel5ef2bc12017-02-21 07:28:31 -0800159 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100160}
161
perkj57c21f92016-06-17 07:27:16 -0700162void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200163 MediaStreamAllocationConfig config) {
perkj26091b12016-09-01 01:17:40 -0700164 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200165 RTC_DCHECK_GT(config.bitrate_priority, 0);
166 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700167 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000168
mflodman101f2502016-06-09 17:21:19 +0200169 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700170 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200171 it->min_bitrate_bps = config.min_bitrate_bps;
172 it->max_bitrate_bps = config.max_bitrate_bps;
173 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
174 it->enforce_min_bitrate = config.enforce_min_bitrate;
175 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000176 } else {
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100177 bitrate_observer_configs_.push_back(
178 ObserverConfig(observer, config.min_bitrate_bps, config.max_bitrate_bps,
179 config.pad_up_bitrate_bps, config.enforce_min_bitrate,
180 config.track_id, config.bitrate_priority));
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 =
188 AllocateBitrates(last_link_capacity_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200189 for (auto& config : bitrate_observer_configs_) {
190 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100191 uint32_t bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100192 BitrateAllocationUpdate update;
193 update.target_bitrate = DataRate::bps(allocated_bitrate);
194 update.link_capacity = DataRate::bps(bandwidth);
195 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
196 update.round_trip_time = TimeDelta::ms(last_rtt_);
197 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
198 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200199 config.allocated_bitrate_bps = allocated_bitrate;
200 if (allocated_bitrate > 0)
201 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
202 }
perkjfea93092016-05-14 00:58:48 -0700203 } else {
204 // Currently, an encoder is not allowed to produce frames.
205 // But we still have to return the initial config bitrate + let the
206 // observer know that it can not produce frames.
Sebastian Jansson13e59032018-11-21 19:13:07 +0100207
208 BitrateAllocationUpdate update;
209 update.target_bitrate = DataRate::Zero();
210 update.link_capacity = DataRate::Zero();
211 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
212 update.round_trip_time = TimeDelta::ms(last_rtt_);
213 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
214 observer->OnBitrateUpdated(update);
Stefan Holmere5904162015-03-26 11:11:06 +0100215 }
perkj71ee44c2016-06-15 00:47:53 -0700216 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000217}
218
perkj71ee44c2016-06-15 00:47:53 -0700219void BitrateAllocator::UpdateAllocationLimits() {
220 uint32_t total_requested_padding_bitrate = 0;
221 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200222 uint32_t total_requested_max_bitrate = 0;
perkj26091b12016-09-01 01:17:40 -0700223 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800224 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700225 if (config.enforce_min_bitrate) {
226 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800227 } else if (config.allocated_bitrate_bps == 0) {
228 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100229 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700230 }
philipel5ef2bc12017-02-21 07:28:31 -0800231 total_requested_padding_bitrate += stream_padding;
Rasmus Brandt681de202019-02-04 15:09:34 +0100232 total_requested_max_bitrate += config.max_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000233 }
perkj71ee44c2016-06-15 00:47:53 -0700234
philipel5ef2bc12017-02-21 07:28:31 -0800235 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100236 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100237 total_requested_max_bitrate == total_requested_max_bitrate_) {
philipel5ef2bc12017-02-21 07:28:31 -0800238 return;
239 }
240
241 total_requested_min_bitrate_ = total_requested_min_bitrate;
242 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200243 total_requested_max_bitrate_ = total_requested_max_bitrate;
philipel5ef2bc12017-02-21 07:28:31 -0800244
Mirko Bonadei675513b2017-11-09 11:09:25 +0100245 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
246 << total_requested_min_bitrate
247 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200248 << total_requested_padding_bitrate
249 << "bps, total_requested_max_bitrate: "
250 << total_requested_max_bitrate << "bps";
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100251 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
252 total_requested_padding_bitrate,
253 total_requested_max_bitrate);
perkj71ee44c2016-06-15 00:47:53 -0700254}
255
256void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700257 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200258
perkj26091b12016-09-01 01:17:40 -0700259 auto it = FindObserverConfig(observer);
260 if (it != bitrate_observer_configs_.end()) {
261 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700262 }
perkj26091b12016-09-01 01:17:40 -0700263
perkj71ee44c2016-06-15 00:47:53 -0700264 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000265}
266
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200267int BitrateAllocator::GetStartBitrate(
268 BitrateAllocatorObserver* observer) const {
perkj26091b12016-09-01 01:17:40 -0700269 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200270 const auto& it = FindObserverConfig(observer);
271 if (it == bitrate_observer_configs_.end()) {
272 // This observer hasn't been added yet, just give it its fair share.
273 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700274 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200275 } else if (it->allocated_bitrate_bps == -1) {
276 // This observer hasn't received an allocation yet, so do the same.
277 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700278 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200279 } else {
280 // This observer already has an allocation.
281 return it->allocated_bitrate_bps;
282 }
perkj57c21f92016-06-17 07:27:16 -0700283}
284
Alex Narest78609d52017-10-20 10:37:47 +0200285void BitrateAllocator::SetBitrateAllocationStrategy(
286 std::unique_ptr<rtc::BitrateAllocationStrategy>
287 bitrate_allocation_strategy) {
288 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
289 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
290}
291
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200292BitrateAllocator::ObserverConfigs::const_iterator
293BitrateAllocator::FindObserverConfig(
294 const BitrateAllocatorObserver* observer) const {
295 for (auto it = bitrate_observer_configs_.begin();
296 it != bitrate_observer_configs_.end(); ++it) {
297 if (it->observer == observer)
298 return it;
299 }
300 return bitrate_observer_configs_.end();
301}
302
mflodman48a4beb2016-07-01 13:03:59 +0200303BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700304BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700305 for (auto it = bitrate_observer_configs_.begin();
306 it != bitrate_observer_configs_.end(); ++it) {
307 if (it->observer == observer)
308 return it;
309 }
310 return bitrate_observer_configs_.end();
311}
312
perkjfea93092016-05-14 00:58:48 -0700313BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200314 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700315 if (bitrate_observer_configs_.empty())
316 return ObserverAllocation();
317
Alex Narest78609d52017-10-20 10:37:47 +0200318 if (bitrate_allocation_strategy_ != nullptr) {
Sebastian Janssone2754c92018-10-24 16:02:26 +0200319 // Note: This intentionally causes slicing, we only copy the fields in
320 // ObserverConfig that are inherited from TrackConfig.
321 std::vector<rtc::BitrateAllocationStrategy::TrackConfig> track_configs(
322 bitrate_observer_configs_.begin(), bitrate_observer_configs_.end());
323
Alex Narest78609d52017-10-20 10:37:47 +0200324 std::vector<uint32_t> track_allocations =
Sebastian Janssone2754c92018-10-24 16:02:26 +0200325 bitrate_allocation_strategy_->AllocateBitrates(
326 bitrate, std::move(track_configs));
Alex Narest78609d52017-10-20 10:37:47 +0200327 // The strategy should return allocation for all tracks.
328 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
329 ObserverAllocation allocation;
330 auto track_allocations_it = track_allocations.begin();
331 for (const auto& observer_config : bitrate_observer_configs_) {
332 allocation[observer_config.observer] = *track_allocations_it++;
333 }
334 return allocation;
335 }
336
perkjfea93092016-05-14 00:58:48 -0700337 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700338 return ZeroRateAllocation();
339
340 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200341 uint32_t sum_max_bitrates = 0;
342 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700343 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200344 sum_max_bitrates += observer_config.max_bitrate_bps;
345 }
346
347 // Not enough for all observers to get an allocation, allocate according to:
348 // enforced min bitrate -> allocated bitrate previous round -> restart paused
349 // streams.
350 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700351 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700352
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800353 // All observers will get their min bitrate plus a share of the rest. This
354 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200355 if (bitrate <= sum_max_bitrates)
356 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700357
Ying Wanga646d302018-03-02 17:04:11 +0100358 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200359 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000360}
361
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200362BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
363 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700364 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700365 for (const auto& observer_config : bitrate_observer_configs_)
366 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700367 return allocation;
368}
369
mflodman2ebe5b12016-05-13 01:43:51 -0700370BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200371 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700372 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200373 // Start by allocating bitrate to observers enforcing a min bitrate, hence
374 // remaining_bitrate might turn negative.
375 int64_t remaining_bitrate = bitrate;
376 for (const auto& observer_config : bitrate_observer_configs_) {
377 int32_t allocated_bitrate = 0;
378 if (observer_config.enforce_min_bitrate)
379 allocated_bitrate = observer_config.min_bitrate_bps;
380
381 allocation[observer_config.observer] = allocated_bitrate;
382 remaining_bitrate -= allocated_bitrate;
383 }
384
385 // Allocate bitrate to all previously active streams.
386 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700387 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200388 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100389 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200390 continue;
391
srte1eb051c2017-11-29 11:23:59 +0100392 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200393 if (remaining_bitrate >= required_bitrate) {
394 allocation[observer_config.observer] = required_bitrate;
395 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200396 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000397 }
398 }
mflodman101f2502016-06-09 17:21:19 +0200399
400 // Allocate bitrate to previously paused streams.
401 if (remaining_bitrate > 0) {
402 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100403 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200404 continue;
405
406 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100407 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200408 if (remaining_bitrate >= required_bitrate) {
409 allocation[observer_config.observer] = required_bitrate;
410 remaining_bitrate -= required_bitrate;
411 }
412 }
413 }
414
415 // Split a possible remainder evenly on all streams with an allocation.
416 if (remaining_bitrate > 0)
417 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
418
419 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100420 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000421}
mflodman101f2502016-06-09 17:21:19 +0200422
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800423// Allocates the bitrate based on the bitrate priority of each observer. This
424// bitrate priority defines the priority for bitrate to be allocated to that
425// observer in relation to other observers. For example with two observers, if
426// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
427// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
428// allocated twice the bitrate as observer 1 above the each observer's
429// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200430BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
431 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200432 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200433 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800434 ObserverAllocation observers_capacities;
435 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200436 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800437 observers_capacities[observer_config.observer] =
438 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
439 }
mflodman101f2502016-06-09 17:21:19 +0200440
441 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800442 // From the remaining bitrate, allocate a proportional amount to each observer
443 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200444 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800445 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200446
447 return allocation;
448}
449
450BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700451 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200452 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200453 ObserverAllocation allocation;
454
455 for (const auto& observer_config : bitrate_observer_configs_) {
456 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
457 bitrate -= observer_config.max_bitrate_bps;
458 }
Ying Wanga646d302018-03-02 17:04:11 +0100459 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200460 &allocation);
461 return allocation;
462}
463
srte1eb051c2017-11-29 11:23:59 +0100464uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200465 // Return the configured minimum bitrate for newly added observers, to avoid
466 // requiring an extra high bitrate for the observer to get an allocated
467 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100468 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200469}
470
srte1eb051c2017-11-29 11:23:59 +0100471uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
472 uint32_t min_bitrate = min_bitrate_bps;
473 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200474 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
475 kMinToggleBitrateBps);
476 }
mflodman48a4beb2016-07-01 13:03:59 +0200477 // Account for protection bitrate used by this observer in the previous
478 // allocation.
479 // Note: the ratio will only be updated when the stream is active, meaning a
480 // paused stream won't get any ratio updates. This might lead to waiting a bit
481 // longer than necessary if the network condition improves, but this is to
482 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100483 if (media_ratio > 0.0 && media_ratio < 1.0)
484 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200485
mflodman101f2502016-06-09 17:21:19 +0200486 return min_bitrate;
487}
488
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200489void BitrateAllocator::DistributeBitrateEvenly(
490 uint32_t bitrate,
491 bool include_zero_allocations,
492 int max_multiplier,
493 ObserverAllocation* allocation) const {
mflodman101f2502016-06-09 17:21:19 +0200494 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
495
496 ObserverSortingMap list_max_bitrates;
497 for (const auto& observer_config : bitrate_observer_configs_) {
498 if (include_zero_allocations ||
499 allocation->at(observer_config.observer) != 0) {
500 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
501 observer_config.max_bitrate_bps, &observer_config));
502 }
503 }
504 auto it = list_max_bitrates.begin();
505 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800506 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200507 uint32_t extra_allocation =
508 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
509 uint32_t total_allocation =
510 extra_allocation + allocation->at(it->second->observer);
511 bitrate -= extra_allocation;
512 if (total_allocation > max_multiplier * it->first) {
513 // There is more than we can fit for this observer, carry over to the
514 // remaining observers.
515 bitrate += total_allocation - max_multiplier * it->first;
516 total_allocation = max_multiplier * it->first;
517 }
518 // Finally, update the allocation for this observer.
519 allocation->at(it->second->observer) = total_allocation;
520 it = list_max_bitrates.erase(it);
521 }
522}
523
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200524bool BitrateAllocator::EnoughBitrateForAllObservers(
525 uint32_t bitrate,
526 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200527 if (bitrate < sum_min_bitrates)
528 return false;
529
perkj26091b12016-09-01 01:17:40 -0700530 uint32_t extra_bitrate_per_observer =
531 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200532 static_cast<uint32_t>(bitrate_observer_configs_.size());
533 for (const auto& observer_config : bitrate_observer_configs_) {
534 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100535 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200536 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800537 }
mflodman101f2502016-06-09 17:21:19 +0200538 }
539 return true;
540}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800541
542void BitrateAllocator::DistributeBitrateRelatively(
543 uint32_t remaining_bitrate,
544 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200545 ObserverAllocation* allocation) const {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800546 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
547 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
548
549 struct PriorityRateObserverConfig {
550 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
551 uint32_t capacity_bps,
552 double bitrate_priority)
553 : allocation_key(allocation_key),
554 capacity_bps(capacity_bps),
555 bitrate_priority(bitrate_priority) {}
556
557 BitrateAllocatorObserver* allocation_key;
558 // The amount of bitrate bps that can be allocated to this observer.
559 uint32_t capacity_bps;
560 double bitrate_priority;
561
562 // We want to sort by which observers will be allocated their full capacity
563 // first. By dividing each observer's capacity by its bitrate priority we
564 // are "normalizing" the capacity of an observer by the rate it will be
565 // filled. This is because the amount allocated is based upon bitrate
566 // priority. We allocate twice as much bitrate to an observer with twice the
567 // bitrate priority of another.
568 bool operator<(const PriorityRateObserverConfig& other) const {
569 return capacity_bps / bitrate_priority <
570 other.capacity_bps / other.bitrate_priority;
571 }
572 };
573
574 double bitrate_priority_sum = 0;
575 std::vector<PriorityRateObserverConfig> priority_rate_observers;
576 for (const auto& observer_config : bitrate_observer_configs_) {
577 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
578 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
579 observer_config.bitrate_priority);
580 bitrate_priority_sum += observer_config.bitrate_priority;
581 }
582
583 // Iterate in the order observers can be allocated their full capacity.
584 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
585 size_t i;
586 for (i = 0; i < priority_rate_observers.size(); ++i) {
587 const auto& priority_rate_observer = priority_rate_observers[i];
588 // We allocate the full capacity to an observer only if its relative
589 // portion from the remaining bitrate is sufficient to allocate its full
590 // capacity. This means we aren't greedily allocating the full capacity, but
591 // that it is only done when there is also enough bitrate to allocate the
592 // proportional amounts to all other observers.
593 double observer_share =
594 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
595 double allocation_bps = observer_share * remaining_bitrate;
596 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
597 if (!enough_bitrate)
598 break;
599 allocation->at(priority_rate_observer.allocation_key) +=
600 priority_rate_observer.capacity_bps;
601 remaining_bitrate -= priority_rate_observer.capacity_bps;
602 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
603 }
604
605 // From the remaining bitrate, allocate the proportional amounts to the
606 // observers that aren't allocated their max capacity.
607 for (; i < priority_rate_observers.size(); ++i) {
608 const auto& priority_rate_observer = priority_rate_observers[i];
609 double fraction_allocated =
610 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
611 allocation->at(priority_rate_observer.allocation_key) +=
612 fraction_allocated * remaining_bitrate;
613 }
614}
615
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000616} // namespace webrtc