blob: d3cb2275b0bc431bff69f843a09b4648ae322fed [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/bitrate_controller/include/bitrate_controller.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
22#include "system_wrappers/include/clock.h"
Ying Wanga646d302018-03-02 17:04:11 +010023#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000025
26namespace webrtc {
27
Stefan Holmere5904162015-03-26 11:11:06 +010028// Allow packets to be transmitted in up to 2 times max video bitrate if the
29// bandwidth estimate allows it.
Ying Wanga646d302018-03-02 17:04:11 +010030const uint8_t kTransmissionMaxBitrateMultiplier = 2;
Stefan Holmere5904162015-03-26 11:11:06 +010031const int kDefaultBitrateBps = 300000;
32
mflodman101f2502016-06-09 17:21:19 +020033// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
34const double kToggleFactor = 0.1;
35const uint32_t kMinToggleBitrateBps = 20000;
36
mflodman48a4beb2016-07-01 13:03:59 +020037const int64_t kBweLogIntervalMs = 5000;
38
39namespace {
40
41double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
kwibergaf476c72016-11-28 15:21:39 -080042 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 13:03:59 +020043 if (protection_bitrate == 0)
44 return 1.0;
45
46 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
47 return media_bitrate / static_cast<double>(allocated_bitrate);
48}
49} // namespace
50
perkj71ee44c2016-06-15 00:47:53 -070051BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
52 : limit_observer_(limit_observer),
Sebastian Jansson89c94b92018-11-20 17:16:36 +010053 last_target_bps_(0),
54 last_link_capacity_bps_(0),
perkjfea93092016-05-14 00:58:48 -070055 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010056 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020057 last_rtt_(0),
Sebastian Jansson13e59032018-11-21 19:13:07 +010058 last_bwe_period_ms_(1000),
mflodman48a4beb2016-07-01 13:03:59 +020059 num_pause_events_(0),
60 clock_(Clock::GetRealTimeClock()),
philipel5ef2bc12017-02-21 07:28:31 -080061 last_bwe_log_time_(0),
62 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020063 total_requested_min_bitrate_(0),
Sebastian Jansson448f4d52018-04-04 14:52:07 +020064 total_requested_max_bitrate_(0),
Sebastian Jansson35fa2802018-10-01 09:16:12 +020065 allocated_without_feedback_(0),
Sebastian Jansson29b204e2018-03-21 12:45:27 +010066 has_packet_feedback_(false),
Ying Wanga646d302018-03-02 17:04:11 +010067 bitrate_allocation_strategy_(nullptr),
68 transmission_max_bitrate_multiplier_(
69 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070070 sequenced_checker_.Detach();
71}
mflodman48a4beb2016-07-01 13:03:59 +020072
73BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070074 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
75 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020076}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000077
Niels Möller74e5f802018-04-25 14:03:46 +020078// static
Ying Wanga646d302018-03-02 17:04:11 +010079uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
80 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
81 "WebRTC-TransmissionMaxBitrateMultiplier")
82 .c_str(),
83 nullptr, 10);
84 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010085 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
86 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010087 return static_cast<uint8_t>(multiplier);
88 }
89 return kTransmissionMaxBitrateMultiplier;
90}
91
perkj71ee44c2016-06-15 00:47:53 -070092void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
Sebastian Jansson89c94b92018-11-20 17:16:36 +010093 uint32_t link_capacity_bps,
perkj71ee44c2016-06-15 00:47:53 -070094 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080095 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070096 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070097 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +010098 last_target_bps_ = target_bitrate_bps;
99 last_link_capacity_bps_ = link_capacity_bps;
perkjfea93092016-05-14 00:58:48 -0700100 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +0200101 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +0100102 last_fraction_loss_ = fraction_loss;
103 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700104 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700105
mflodman48a4beb2016-07-01 13:03:59 +0200106 // Periodically log the incoming BWE.
107 int64_t now = clock_->TimeInMilliseconds();
108 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100109 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200110 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800111 }
mflodman48a4beb2016-07-01 13:03:59 +0200112
113 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100114 ObserverAllocation bandwidth_allocation = AllocateBitrates(link_capacity_bps);
mflodman48a4beb2016-07-01 13:03:59 +0200115
116 for (auto& config : bitrate_observer_configs_) {
117 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100118 uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100119 BitrateAllocationUpdate update;
120 update.target_bitrate = DataRate::bps(allocated_bitrate);
121 update.link_capacity = DataRate::bps(allocated_bandwidth);
122 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
123 update.round_trip_time = TimeDelta::ms(last_rtt_);
124 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
125 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200126
127 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
128 if (target_bitrate_bps > 0)
129 ++num_pause_events_;
130 // The protection bitrate is an estimate based on the ratio between media
131 // and protection used before this observer was muted.
132 uint32_t predicted_protection_bps =
133 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100134 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
135 << " with configured min bitrate "
136 << config.min_bitrate_bps << " and current estimate of "
137 << target_bitrate_bps << " and protection bitrate "
138 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200139 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
140 if (target_bitrate_bps > 0)
141 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100142 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
143 << ", configured min bitrate " << config.min_bitrate_bps
144 << ", current allocation " << allocated_bitrate
145 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200146 }
147
148 // Only update the media ratio if the observer got an allocation.
149 if (allocated_bitrate > 0)
150 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
151 config.allocated_bitrate_bps = allocated_bitrate;
152 }
philipel5ef2bc12017-02-21 07:28:31 -0800153 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100154}
155
perkj57c21f92016-06-17 07:27:16 -0700156void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200157 MediaStreamAllocationConfig config) {
perkj26091b12016-09-01 01:17:40 -0700158 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200159 RTC_DCHECK_GT(config.bitrate_priority, 0);
160 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700161 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000162
mflodman101f2502016-06-09 17:21:19 +0200163 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700164 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200165 it->min_bitrate_bps = config.min_bitrate_bps;
166 it->max_bitrate_bps = config.max_bitrate_bps;
167 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
168 it->enforce_min_bitrate = config.enforce_min_bitrate;
169 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000170 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800171 bitrate_observer_configs_.push_back(ObserverConfig(
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200172 observer, config.min_bitrate_bps, config.max_bitrate_bps,
173 config.pad_up_bitrate_bps, config.enforce_min_bitrate, config.track_id,
174 config.bitrate_priority, config.has_packet_feedback));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000175 }
Stefan Holmere5904162015-03-26 11:11:06 +0100176
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100177 if (last_target_bps_ > 0) {
mflodman101f2502016-06-09 17:21:19 +0200178 // Calculate a new allocation and update all observers.
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100179
180 ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
181 ObserverAllocation bandwidth_allocation =
182 AllocateBitrates(last_link_capacity_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200183 for (auto& config : bitrate_observer_configs_) {
184 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100185 uint32_t bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100186 BitrateAllocationUpdate update;
187 update.target_bitrate = DataRate::bps(allocated_bitrate);
188 update.link_capacity = DataRate::bps(bandwidth);
189 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
190 update.round_trip_time = TimeDelta::ms(last_rtt_);
191 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
192 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200193 config.allocated_bitrate_bps = allocated_bitrate;
194 if (allocated_bitrate > 0)
195 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
196 }
perkjfea93092016-05-14 00:58:48 -0700197 } else {
198 // Currently, an encoder is not allowed to produce frames.
199 // But we still have to return the initial config bitrate + let the
200 // observer know that it can not produce frames.
Sebastian Jansson13e59032018-11-21 19:13:07 +0100201
202 BitrateAllocationUpdate update;
203 update.target_bitrate = DataRate::Zero();
204 update.link_capacity = DataRate::Zero();
205 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
206 update.round_trip_time = TimeDelta::ms(last_rtt_);
207 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
208 observer->OnBitrateUpdated(update);
Stefan Holmere5904162015-03-26 11:11:06 +0100209 }
perkj71ee44c2016-06-15 00:47:53 -0700210 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000211}
212
perkj71ee44c2016-06-15 00:47:53 -0700213void BitrateAllocator::UpdateAllocationLimits() {
214 uint32_t total_requested_padding_bitrate = 0;
215 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200216 uint32_t total_requested_max_bitrate = 0;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100217 bool has_packet_feedback = false;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200218 uint32_t allocated_without_feedback = 0;
perkj26091b12016-09-01 01:17:40 -0700219 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800220 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700221 if (config.enforce_min_bitrate) {
222 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800223 } else if (config.allocated_bitrate_bps == 0) {
224 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100225 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700226 }
philipel5ef2bc12017-02-21 07:28:31 -0800227 total_requested_padding_bitrate += stream_padding;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200228 total_requested_max_bitrate += config.max_bitrate_bps;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100229 if (config.allocated_bitrate_bps > 0 && config.has_packet_feedback)
230 has_packet_feedback = true;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200231 // TODO(srte): Remove field trial check.
232 if (!config.has_packet_feedback &&
233 field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC"))
234 allocated_without_feedback += config.allocated_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000235 }
perkj71ee44c2016-06-15 00:47:53 -0700236
philipel5ef2bc12017-02-21 07:28:31 -0800237 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100238 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200239 total_requested_max_bitrate == total_requested_max_bitrate_ &&
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200240 allocated_without_feedback == allocated_without_feedback_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100241 has_packet_feedback == has_packet_feedback_) {
philipel5ef2bc12017-02-21 07:28:31 -0800242 return;
243 }
244
245 total_requested_min_bitrate_ = total_requested_min_bitrate;
246 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200247 total_requested_max_bitrate_ = total_requested_max_bitrate;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200248 allocated_without_feedback_ = allocated_without_feedback;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100249 has_packet_feedback_ = has_packet_feedback;
philipel5ef2bc12017-02-21 07:28:31 -0800250
Mirko Bonadei675513b2017-11-09 11:09:25 +0100251 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
252 << total_requested_min_bitrate
253 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200254 << total_requested_padding_bitrate
255 << "bps, total_requested_max_bitrate: "
256 << total_requested_max_bitrate << "bps";
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100257 limit_observer_->OnAllocationLimitsChanged(
258 total_requested_min_bitrate, total_requested_padding_bitrate,
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200259 total_requested_max_bitrate, allocated_without_feedback,
260 has_packet_feedback);
perkj71ee44c2016-06-15 00:47:53 -0700261}
262
263void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700264 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200265
perkj26091b12016-09-01 01:17:40 -0700266 auto it = FindObserverConfig(observer);
267 if (it != bitrate_observer_configs_.end()) {
268 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700269 }
perkj26091b12016-09-01 01:17:40 -0700270
perkj71ee44c2016-06-15 00:47:53 -0700271 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000272}
273
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200274int BitrateAllocator::GetStartBitrate(
275 BitrateAllocatorObserver* observer) const {
perkj26091b12016-09-01 01:17:40 -0700276 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200277 const auto& it = FindObserverConfig(observer);
278 if (it == bitrate_observer_configs_.end()) {
279 // This observer hasn't been added yet, just give it its fair share.
280 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700281 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200282 } else if (it->allocated_bitrate_bps == -1) {
283 // This observer hasn't received an allocation yet, so do the same.
284 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700285 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200286 } else {
287 // This observer already has an allocation.
288 return it->allocated_bitrate_bps;
289 }
perkj57c21f92016-06-17 07:27:16 -0700290}
291
Alex Narest78609d52017-10-20 10:37:47 +0200292void BitrateAllocator::SetBitrateAllocationStrategy(
293 std::unique_ptr<rtc::BitrateAllocationStrategy>
294 bitrate_allocation_strategy) {
295 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
296 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
297}
298
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200299BitrateAllocator::ObserverConfigs::const_iterator
300BitrateAllocator::FindObserverConfig(
301 const BitrateAllocatorObserver* observer) const {
302 for (auto it = bitrate_observer_configs_.begin();
303 it != bitrate_observer_configs_.end(); ++it) {
304 if (it->observer == observer)
305 return it;
306 }
307 return bitrate_observer_configs_.end();
308}
309
mflodman48a4beb2016-07-01 13:03:59 +0200310BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700311BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700312 for (auto it = bitrate_observer_configs_.begin();
313 it != bitrate_observer_configs_.end(); ++it) {
314 if (it->observer == observer)
315 return it;
316 }
317 return bitrate_observer_configs_.end();
318}
319
perkjfea93092016-05-14 00:58:48 -0700320BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200321 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700322 if (bitrate_observer_configs_.empty())
323 return ObserverAllocation();
324
Alex Narest78609d52017-10-20 10:37:47 +0200325 if (bitrate_allocation_strategy_ != nullptr) {
Sebastian Janssone2754c92018-10-24 16:02:26 +0200326 // Note: This intentionally causes slicing, we only copy the fields in
327 // ObserverConfig that are inherited from TrackConfig.
328 std::vector<rtc::BitrateAllocationStrategy::TrackConfig> track_configs(
329 bitrate_observer_configs_.begin(), bitrate_observer_configs_.end());
330
Alex Narest78609d52017-10-20 10:37:47 +0200331 std::vector<uint32_t> track_allocations =
Sebastian Janssone2754c92018-10-24 16:02:26 +0200332 bitrate_allocation_strategy_->AllocateBitrates(
333 bitrate, std::move(track_configs));
Alex Narest78609d52017-10-20 10:37:47 +0200334 // The strategy should return allocation for all tracks.
335 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
336 ObserverAllocation allocation;
337 auto track_allocations_it = track_allocations.begin();
338 for (const auto& observer_config : bitrate_observer_configs_) {
339 allocation[observer_config.observer] = *track_allocations_it++;
340 }
341 return allocation;
342 }
343
perkjfea93092016-05-14 00:58:48 -0700344 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700345 return ZeroRateAllocation();
346
347 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200348 uint32_t sum_max_bitrates = 0;
349 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700350 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200351 sum_max_bitrates += observer_config.max_bitrate_bps;
352 }
353
354 // Not enough for all observers to get an allocation, allocate according to:
355 // enforced min bitrate -> allocated bitrate previous round -> restart paused
356 // streams.
357 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700358 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700359
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800360 // All observers will get their min bitrate plus a share of the rest. This
361 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200362 if (bitrate <= sum_max_bitrates)
363 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700364
Ying Wanga646d302018-03-02 17:04:11 +0100365 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200366 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000367}
368
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200369BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
370 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700371 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700372 for (const auto& observer_config : bitrate_observer_configs_)
373 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700374 return allocation;
375}
376
mflodman2ebe5b12016-05-13 01:43:51 -0700377BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200378 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700379 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200380 // Start by allocating bitrate to observers enforcing a min bitrate, hence
381 // remaining_bitrate might turn negative.
382 int64_t remaining_bitrate = bitrate;
383 for (const auto& observer_config : bitrate_observer_configs_) {
384 int32_t allocated_bitrate = 0;
385 if (observer_config.enforce_min_bitrate)
386 allocated_bitrate = observer_config.min_bitrate_bps;
387
388 allocation[observer_config.observer] = allocated_bitrate;
389 remaining_bitrate -= allocated_bitrate;
390 }
391
392 // Allocate bitrate to all previously active streams.
393 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700394 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200395 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100396 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200397 continue;
398
srte1eb051c2017-11-29 11:23:59 +0100399 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200400 if (remaining_bitrate >= required_bitrate) {
401 allocation[observer_config.observer] = required_bitrate;
402 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200403 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000404 }
405 }
mflodman101f2502016-06-09 17:21:19 +0200406
407 // Allocate bitrate to previously paused streams.
408 if (remaining_bitrate > 0) {
409 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100410 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200411 continue;
412
413 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100414 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200415 if (remaining_bitrate >= required_bitrate) {
416 allocation[observer_config.observer] = required_bitrate;
417 remaining_bitrate -= required_bitrate;
418 }
419 }
420 }
421
422 // Split a possible remainder evenly on all streams with an allocation.
423 if (remaining_bitrate > 0)
424 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
425
426 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100427 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000428}
mflodman101f2502016-06-09 17:21:19 +0200429
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800430// Allocates the bitrate based on the bitrate priority of each observer. This
431// bitrate priority defines the priority for bitrate to be allocated to that
432// observer in relation to other observers. For example with two observers, if
433// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
434// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
435// allocated twice the bitrate as observer 1 above the each observer's
436// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200437BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
438 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200439 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200440 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800441 ObserverAllocation observers_capacities;
442 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200443 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800444 observers_capacities[observer_config.observer] =
445 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
446 }
mflodman101f2502016-06-09 17:21:19 +0200447
448 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800449 // From the remaining bitrate, allocate a proportional amount to each observer
450 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200451 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800452 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200453
454 return allocation;
455}
456
457BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700458 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200459 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200460 ObserverAllocation allocation;
461
462 for (const auto& observer_config : bitrate_observer_configs_) {
463 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
464 bitrate -= observer_config.max_bitrate_bps;
465 }
Ying Wanga646d302018-03-02 17:04:11 +0100466 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200467 &allocation);
468 return allocation;
469}
470
srte1eb051c2017-11-29 11:23:59 +0100471uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200472 // Return the configured minimum bitrate for newly added observers, to avoid
473 // requiring an extra high bitrate for the observer to get an allocated
474 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100475 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200476}
477
srte1eb051c2017-11-29 11:23:59 +0100478uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
479 uint32_t min_bitrate = min_bitrate_bps;
480 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200481 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
482 kMinToggleBitrateBps);
483 }
mflodman48a4beb2016-07-01 13:03:59 +0200484 // Account for protection bitrate used by this observer in the previous
485 // allocation.
486 // Note: the ratio will only be updated when the stream is active, meaning a
487 // paused stream won't get any ratio updates. This might lead to waiting a bit
488 // longer than necessary if the network condition improves, but this is to
489 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100490 if (media_ratio > 0.0 && media_ratio < 1.0)
491 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200492
mflodman101f2502016-06-09 17:21:19 +0200493 return min_bitrate;
494}
495
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200496void BitrateAllocator::DistributeBitrateEvenly(
497 uint32_t bitrate,
498 bool include_zero_allocations,
499 int max_multiplier,
500 ObserverAllocation* allocation) const {
mflodman101f2502016-06-09 17:21:19 +0200501 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
502
503 ObserverSortingMap list_max_bitrates;
504 for (const auto& observer_config : bitrate_observer_configs_) {
505 if (include_zero_allocations ||
506 allocation->at(observer_config.observer) != 0) {
507 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
508 observer_config.max_bitrate_bps, &observer_config));
509 }
510 }
511 auto it = list_max_bitrates.begin();
512 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800513 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200514 uint32_t extra_allocation =
515 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
516 uint32_t total_allocation =
517 extra_allocation + allocation->at(it->second->observer);
518 bitrate -= extra_allocation;
519 if (total_allocation > max_multiplier * it->first) {
520 // There is more than we can fit for this observer, carry over to the
521 // remaining observers.
522 bitrate += total_allocation - max_multiplier * it->first;
523 total_allocation = max_multiplier * it->first;
524 }
525 // Finally, update the allocation for this observer.
526 allocation->at(it->second->observer) = total_allocation;
527 it = list_max_bitrates.erase(it);
528 }
529}
530
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200531bool BitrateAllocator::EnoughBitrateForAllObservers(
532 uint32_t bitrate,
533 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200534 if (bitrate < sum_min_bitrates)
535 return false;
536
perkj26091b12016-09-01 01:17:40 -0700537 uint32_t extra_bitrate_per_observer =
538 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200539 static_cast<uint32_t>(bitrate_observer_configs_.size());
540 for (const auto& observer_config : bitrate_observer_configs_) {
541 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100542 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200543 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800544 }
mflodman101f2502016-06-09 17:21:19 +0200545 }
546 return true;
547}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800548
549void BitrateAllocator::DistributeBitrateRelatively(
550 uint32_t remaining_bitrate,
551 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200552 ObserverAllocation* allocation) const {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800553 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
554 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
555
556 struct PriorityRateObserverConfig {
557 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
558 uint32_t capacity_bps,
559 double bitrate_priority)
560 : allocation_key(allocation_key),
561 capacity_bps(capacity_bps),
562 bitrate_priority(bitrate_priority) {}
563
564 BitrateAllocatorObserver* allocation_key;
565 // The amount of bitrate bps that can be allocated to this observer.
566 uint32_t capacity_bps;
567 double bitrate_priority;
568
569 // We want to sort by which observers will be allocated their full capacity
570 // first. By dividing each observer's capacity by its bitrate priority we
571 // are "normalizing" the capacity of an observer by the rate it will be
572 // filled. This is because the amount allocated is based upon bitrate
573 // priority. We allocate twice as much bitrate to an observer with twice the
574 // bitrate priority of another.
575 bool operator<(const PriorityRateObserverConfig& other) const {
576 return capacity_bps / bitrate_priority <
577 other.capacity_bps / other.bitrate_priority;
578 }
579 };
580
581 double bitrate_priority_sum = 0;
582 std::vector<PriorityRateObserverConfig> priority_rate_observers;
583 for (const auto& observer_config : bitrate_observer_configs_) {
584 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
585 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
586 observer_config.bitrate_priority);
587 bitrate_priority_sum += observer_config.bitrate_priority;
588 }
589
590 // Iterate in the order observers can be allocated their full capacity.
591 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
592 size_t i;
593 for (i = 0; i < priority_rate_observers.size(); ++i) {
594 const auto& priority_rate_observer = priority_rate_observers[i];
595 // We allocate the full capacity to an observer only if its relative
596 // portion from the remaining bitrate is sufficient to allocate its full
597 // capacity. This means we aren't greedily allocating the full capacity, but
598 // that it is only done when there is also enough bitrate to allocate the
599 // proportional amounts to all other observers.
600 double observer_share =
601 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
602 double allocation_bps = observer_share * remaining_bitrate;
603 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
604 if (!enough_bitrate)
605 break;
606 allocation->at(priority_rate_observer.allocation_key) +=
607 priority_rate_observer.capacity_bps;
608 remaining_bitrate -= priority_rate_observer.capacity_bps;
609 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
610 }
611
612 // From the remaining bitrate, allocate the proportional amounts to the
613 // observers that aren't allocated their max capacity.
614 for (; i < priority_rate_observers.size(); ++i) {
615 const auto& priority_rate_observer = priority_rate_observers[i];
616 double fraction_allocated =
617 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
618 allocation->at(priority_rate_observer.allocation_key) +=
619 fraction_allocated * remaining_bitrate;
620 }
621}
622
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000623} // namespace webrtc