blob: 0fb1bf0c880b22a85df9fcae506af733f11c1454 [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),
58 num_pause_events_(0),
59 clock_(Clock::GetRealTimeClock()),
philipel5ef2bc12017-02-21 07:28:31 -080060 last_bwe_log_time_(0),
61 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020062 total_requested_min_bitrate_(0),
Sebastian Jansson448f4d52018-04-04 14:52:07 +020063 total_requested_max_bitrate_(0),
Sebastian Jansson35fa2802018-10-01 09:16:12 +020064 allocated_without_feedback_(0),
Sebastian Jansson29b204e2018-03-21 12:45:27 +010065 has_packet_feedback_(false),
Ying Wanga646d302018-03-02 17:04:11 +010066 bitrate_allocation_strategy_(nullptr),
67 transmission_max_bitrate_multiplier_(
68 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070069 sequenced_checker_.Detach();
70}
mflodman48a4beb2016-07-01 13:03:59 +020071
72BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070073 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
74 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020075}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000076
Niels Möller74e5f802018-04-25 14:03:46 +020077// static
Ying Wanga646d302018-03-02 17:04:11 +010078uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
79 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
80 "WebRTC-TransmissionMaxBitrateMultiplier")
81 .c_str(),
82 nullptr, 10);
83 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010084 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
85 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010086 return static_cast<uint8_t>(multiplier);
87 }
88 return kTransmissionMaxBitrateMultiplier;
89}
90
perkj71ee44c2016-06-15 00:47:53 -070091void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
Sebastian Jansson89c94b92018-11-20 17:16:36 +010092 uint32_t link_capacity_bps,
perkj71ee44c2016-06-15 00:47:53 -070093 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080094 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070095 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070096 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +010097 last_target_bps_ = target_bitrate_bps;
98 last_link_capacity_bps_ = link_capacity_bps;
perkjfea93092016-05-14 00:58:48 -070099 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +0200100 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +0100101 last_fraction_loss_ = fraction_loss;
102 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700103 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700104
mflodman48a4beb2016-07-01 13:03:59 +0200105 // Periodically log the incoming BWE.
106 int64_t now = clock_->TimeInMilliseconds();
107 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100108 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200109 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800110 }
mflodman48a4beb2016-07-01 13:03:59 +0200111
112 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100113 ObserverAllocation bandwidth_allocation = AllocateBitrates(link_capacity_bps);
mflodman48a4beb2016-07-01 13:03:59 +0200114
115 for (auto& config : bitrate_observer_configs_) {
116 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100117 uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
118 uint32_t protection_bitrate =
119 config.observer->OnBitrateUpdated(BitrateAllocationUpdate{
120 allocated_bitrate, allocated_bandwidth, last_fraction_loss_,
121 last_rtt_, last_bwe_period_ms_});
mflodman48a4beb2016-07-01 13:03:59 +0200122
123 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
124 if (target_bitrate_bps > 0)
125 ++num_pause_events_;
126 // The protection bitrate is an estimate based on the ratio between media
127 // and protection used before this observer was muted.
128 uint32_t predicted_protection_bps =
129 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100130 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
131 << " with configured min bitrate "
132 << config.min_bitrate_bps << " and current estimate of "
133 << target_bitrate_bps << " and protection bitrate "
134 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200135 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
136 if (target_bitrate_bps > 0)
137 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100138 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
139 << ", configured min bitrate " << config.min_bitrate_bps
140 << ", current allocation " << allocated_bitrate
141 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200142 }
143
144 // Only update the media ratio if the observer got an allocation.
145 if (allocated_bitrate > 0)
146 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
147 config.allocated_bitrate_bps = allocated_bitrate;
148 }
philipel5ef2bc12017-02-21 07:28:31 -0800149 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100150}
151
perkj57c21f92016-06-17 07:27:16 -0700152void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200153 MediaStreamAllocationConfig config) {
perkj26091b12016-09-01 01:17:40 -0700154 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200155 RTC_DCHECK_GT(config.bitrate_priority, 0);
156 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700157 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000158
mflodman101f2502016-06-09 17:21:19 +0200159 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700160 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200161 it->min_bitrate_bps = config.min_bitrate_bps;
162 it->max_bitrate_bps = config.max_bitrate_bps;
163 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
164 it->enforce_min_bitrate = config.enforce_min_bitrate;
165 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000166 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800167 bitrate_observer_configs_.push_back(ObserverConfig(
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200168 observer, config.min_bitrate_bps, config.max_bitrate_bps,
169 config.pad_up_bitrate_bps, config.enforce_min_bitrate, config.track_id,
170 config.bitrate_priority, config.has_packet_feedback));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000171 }
Stefan Holmere5904162015-03-26 11:11:06 +0100172
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100173 if (last_target_bps_ > 0) {
mflodman101f2502016-06-09 17:21:19 +0200174 // Calculate a new allocation and update all observers.
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100175
176 ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
177 ObserverAllocation bandwidth_allocation =
178 AllocateBitrates(last_link_capacity_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200179 for (auto& config : bitrate_observer_configs_) {
180 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100181 uint32_t bandwidth = bandwidth_allocation[config.observer];
182 uint32_t protection_bitrate =
183 config.observer->OnBitrateUpdated(BitrateAllocationUpdate{
184 allocated_bitrate, bandwidth, last_fraction_loss_, last_rtt_,
185 last_bwe_period_ms_});
mflodman48a4beb2016-07-01 13:03:59 +0200186 config.allocated_bitrate_bps = allocated_bitrate;
187 if (allocated_bitrate > 0)
188 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
189 }
perkjfea93092016-05-14 00:58:48 -0700190 } else {
191 // Currently, an encoder is not allowed to produce frames.
192 // But we still have to return the initial config bitrate + let the
193 // observer know that it can not produce frames.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200194 observer->OnBitrateUpdated(BitrateAllocationUpdate{
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100195 0, 0, last_fraction_loss_, last_rtt_, last_bwe_period_ms_});
Stefan Holmere5904162015-03-26 11:11:06 +0100196 }
perkj71ee44c2016-06-15 00:47:53 -0700197 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000198}
199
perkj71ee44c2016-06-15 00:47:53 -0700200void BitrateAllocator::UpdateAllocationLimits() {
201 uint32_t total_requested_padding_bitrate = 0;
202 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200203 uint32_t total_requested_max_bitrate = 0;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100204 bool has_packet_feedback = false;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200205 uint32_t allocated_without_feedback = 0;
perkj26091b12016-09-01 01:17:40 -0700206 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800207 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700208 if (config.enforce_min_bitrate) {
209 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800210 } else if (config.allocated_bitrate_bps == 0) {
211 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100212 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700213 }
philipel5ef2bc12017-02-21 07:28:31 -0800214 total_requested_padding_bitrate += stream_padding;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200215 total_requested_max_bitrate += config.max_bitrate_bps;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100216 if (config.allocated_bitrate_bps > 0 && config.has_packet_feedback)
217 has_packet_feedback = true;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200218 // TODO(srte): Remove field trial check.
219 if (!config.has_packet_feedback &&
220 field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC"))
221 allocated_without_feedback += config.allocated_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000222 }
perkj71ee44c2016-06-15 00:47:53 -0700223
philipel5ef2bc12017-02-21 07:28:31 -0800224 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100225 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200226 total_requested_max_bitrate == total_requested_max_bitrate_ &&
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200227 allocated_without_feedback == allocated_without_feedback_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100228 has_packet_feedback == has_packet_feedback_) {
philipel5ef2bc12017-02-21 07:28:31 -0800229 return;
230 }
231
232 total_requested_min_bitrate_ = total_requested_min_bitrate;
233 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200234 total_requested_max_bitrate_ = total_requested_max_bitrate;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200235 allocated_without_feedback_ = allocated_without_feedback;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100236 has_packet_feedback_ = has_packet_feedback;
philipel5ef2bc12017-02-21 07:28:31 -0800237
Mirko Bonadei675513b2017-11-09 11:09:25 +0100238 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
239 << total_requested_min_bitrate
240 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200241 << total_requested_padding_bitrate
242 << "bps, total_requested_max_bitrate: "
243 << total_requested_max_bitrate << "bps";
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100244 limit_observer_->OnAllocationLimitsChanged(
245 total_requested_min_bitrate, total_requested_padding_bitrate,
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200246 total_requested_max_bitrate, allocated_without_feedback,
247 has_packet_feedback);
perkj71ee44c2016-06-15 00:47:53 -0700248}
249
250void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700251 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200252
perkj26091b12016-09-01 01:17:40 -0700253 auto it = FindObserverConfig(observer);
254 if (it != bitrate_observer_configs_.end()) {
255 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700256 }
perkj26091b12016-09-01 01:17:40 -0700257
perkj71ee44c2016-06-15 00:47:53 -0700258 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000259}
260
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200261int BitrateAllocator::GetStartBitrate(
262 BitrateAllocatorObserver* observer) const {
perkj26091b12016-09-01 01:17:40 -0700263 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200264 const auto& it = FindObserverConfig(observer);
265 if (it == bitrate_observer_configs_.end()) {
266 // This observer hasn't been added yet, just give it its fair share.
267 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700268 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200269 } else if (it->allocated_bitrate_bps == -1) {
270 // This observer hasn't received an allocation yet, so do the same.
271 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700272 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200273 } else {
274 // This observer already has an allocation.
275 return it->allocated_bitrate_bps;
276 }
perkj57c21f92016-06-17 07:27:16 -0700277}
278
Alex Narest78609d52017-10-20 10:37:47 +0200279void BitrateAllocator::SetBitrateAllocationStrategy(
280 std::unique_ptr<rtc::BitrateAllocationStrategy>
281 bitrate_allocation_strategy) {
282 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
283 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
284}
285
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200286BitrateAllocator::ObserverConfigs::const_iterator
287BitrateAllocator::FindObserverConfig(
288 const BitrateAllocatorObserver* observer) const {
289 for (auto it = bitrate_observer_configs_.begin();
290 it != bitrate_observer_configs_.end(); ++it) {
291 if (it->observer == observer)
292 return it;
293 }
294 return bitrate_observer_configs_.end();
295}
296
mflodman48a4beb2016-07-01 13:03:59 +0200297BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700298BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700299 for (auto it = bitrate_observer_configs_.begin();
300 it != bitrate_observer_configs_.end(); ++it) {
301 if (it->observer == observer)
302 return it;
303 }
304 return bitrate_observer_configs_.end();
305}
306
perkjfea93092016-05-14 00:58:48 -0700307BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200308 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700309 if (bitrate_observer_configs_.empty())
310 return ObserverAllocation();
311
Alex Narest78609d52017-10-20 10:37:47 +0200312 if (bitrate_allocation_strategy_ != nullptr) {
Sebastian Janssone2754c92018-10-24 16:02:26 +0200313 // Note: This intentionally causes slicing, we only copy the fields in
314 // ObserverConfig that are inherited from TrackConfig.
315 std::vector<rtc::BitrateAllocationStrategy::TrackConfig> track_configs(
316 bitrate_observer_configs_.begin(), bitrate_observer_configs_.end());
317
Alex Narest78609d52017-10-20 10:37:47 +0200318 std::vector<uint32_t> track_allocations =
Sebastian Janssone2754c92018-10-24 16:02:26 +0200319 bitrate_allocation_strategy_->AllocateBitrates(
320 bitrate, std::move(track_configs));
Alex Narest78609d52017-10-20 10:37:47 +0200321 // The strategy should return allocation for all tracks.
322 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
323 ObserverAllocation allocation;
324 auto track_allocations_it = track_allocations.begin();
325 for (const auto& observer_config : bitrate_observer_configs_) {
326 allocation[observer_config.observer] = *track_allocations_it++;
327 }
328 return allocation;
329 }
330
perkjfea93092016-05-14 00:58:48 -0700331 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700332 return ZeroRateAllocation();
333
334 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200335 uint32_t sum_max_bitrates = 0;
336 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700337 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200338 sum_max_bitrates += observer_config.max_bitrate_bps;
339 }
340
341 // Not enough for all observers to get an allocation, allocate according to:
342 // enforced min bitrate -> allocated bitrate previous round -> restart paused
343 // streams.
344 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700345 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700346
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800347 // All observers will get their min bitrate plus a share of the rest. This
348 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200349 if (bitrate <= sum_max_bitrates)
350 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700351
Ying Wanga646d302018-03-02 17:04:11 +0100352 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200353 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000354}
355
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200356BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
357 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700358 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700359 for (const auto& observer_config : bitrate_observer_configs_)
360 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700361 return allocation;
362}
363
mflodman2ebe5b12016-05-13 01:43:51 -0700364BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200365 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700366 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200367 // Start by allocating bitrate to observers enforcing a min bitrate, hence
368 // remaining_bitrate might turn negative.
369 int64_t remaining_bitrate = bitrate;
370 for (const auto& observer_config : bitrate_observer_configs_) {
371 int32_t allocated_bitrate = 0;
372 if (observer_config.enforce_min_bitrate)
373 allocated_bitrate = observer_config.min_bitrate_bps;
374
375 allocation[observer_config.observer] = allocated_bitrate;
376 remaining_bitrate -= allocated_bitrate;
377 }
378
379 // Allocate bitrate to all previously active streams.
380 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700381 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200382 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100383 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200384 continue;
385
srte1eb051c2017-11-29 11:23:59 +0100386 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200387 if (remaining_bitrate >= required_bitrate) {
388 allocation[observer_config.observer] = required_bitrate;
389 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200390 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000391 }
392 }
mflodman101f2502016-06-09 17:21:19 +0200393
394 // Allocate bitrate to previously paused streams.
395 if (remaining_bitrate > 0) {
396 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100397 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200398 continue;
399
400 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100401 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200402 if (remaining_bitrate >= required_bitrate) {
403 allocation[observer_config.observer] = required_bitrate;
404 remaining_bitrate -= required_bitrate;
405 }
406 }
407 }
408
409 // Split a possible remainder evenly on all streams with an allocation.
410 if (remaining_bitrate > 0)
411 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
412
413 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100414 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000415}
mflodman101f2502016-06-09 17:21:19 +0200416
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800417// Allocates the bitrate based on the bitrate priority of each observer. This
418// bitrate priority defines the priority for bitrate to be allocated to that
419// observer in relation to other observers. For example with two observers, if
420// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
421// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
422// allocated twice the bitrate as observer 1 above the each observer's
423// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200424BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
425 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200426 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200427 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800428 ObserverAllocation observers_capacities;
429 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200430 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800431 observers_capacities[observer_config.observer] =
432 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
433 }
mflodman101f2502016-06-09 17:21:19 +0200434
435 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800436 // From the remaining bitrate, allocate a proportional amount to each observer
437 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200438 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800439 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200440
441 return allocation;
442}
443
444BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700445 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200446 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200447 ObserverAllocation allocation;
448
449 for (const auto& observer_config : bitrate_observer_configs_) {
450 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
451 bitrate -= observer_config.max_bitrate_bps;
452 }
Ying Wanga646d302018-03-02 17:04:11 +0100453 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200454 &allocation);
455 return allocation;
456}
457
srte1eb051c2017-11-29 11:23:59 +0100458uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200459 // Return the configured minimum bitrate for newly added observers, to avoid
460 // requiring an extra high bitrate for the observer to get an allocated
461 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100462 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200463}
464
srte1eb051c2017-11-29 11:23:59 +0100465uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
466 uint32_t min_bitrate = min_bitrate_bps;
467 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200468 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
469 kMinToggleBitrateBps);
470 }
mflodman48a4beb2016-07-01 13:03:59 +0200471 // Account for protection bitrate used by this observer in the previous
472 // allocation.
473 // Note: the ratio will only be updated when the stream is active, meaning a
474 // paused stream won't get any ratio updates. This might lead to waiting a bit
475 // longer than necessary if the network condition improves, but this is to
476 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100477 if (media_ratio > 0.0 && media_ratio < 1.0)
478 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200479
mflodman101f2502016-06-09 17:21:19 +0200480 return min_bitrate;
481}
482
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200483void BitrateAllocator::DistributeBitrateEvenly(
484 uint32_t bitrate,
485 bool include_zero_allocations,
486 int max_multiplier,
487 ObserverAllocation* allocation) const {
mflodman101f2502016-06-09 17:21:19 +0200488 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
489
490 ObserverSortingMap list_max_bitrates;
491 for (const auto& observer_config : bitrate_observer_configs_) {
492 if (include_zero_allocations ||
493 allocation->at(observer_config.observer) != 0) {
494 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
495 observer_config.max_bitrate_bps, &observer_config));
496 }
497 }
498 auto it = list_max_bitrates.begin();
499 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800500 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200501 uint32_t extra_allocation =
502 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
503 uint32_t total_allocation =
504 extra_allocation + allocation->at(it->second->observer);
505 bitrate -= extra_allocation;
506 if (total_allocation > max_multiplier * it->first) {
507 // There is more than we can fit for this observer, carry over to the
508 // remaining observers.
509 bitrate += total_allocation - max_multiplier * it->first;
510 total_allocation = max_multiplier * it->first;
511 }
512 // Finally, update the allocation for this observer.
513 allocation->at(it->second->observer) = total_allocation;
514 it = list_max_bitrates.erase(it);
515 }
516}
517
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200518bool BitrateAllocator::EnoughBitrateForAllObservers(
519 uint32_t bitrate,
520 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200521 if (bitrate < sum_min_bitrates)
522 return false;
523
perkj26091b12016-09-01 01:17:40 -0700524 uint32_t extra_bitrate_per_observer =
525 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200526 static_cast<uint32_t>(bitrate_observer_configs_.size());
527 for (const auto& observer_config : bitrate_observer_configs_) {
528 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100529 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200530 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800531 }
mflodman101f2502016-06-09 17:21:19 +0200532 }
533 return true;
534}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800535
536void BitrateAllocator::DistributeBitrateRelatively(
537 uint32_t remaining_bitrate,
538 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200539 ObserverAllocation* allocation) const {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800540 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
541 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
542
543 struct PriorityRateObserverConfig {
544 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
545 uint32_t capacity_bps,
546 double bitrate_priority)
547 : allocation_key(allocation_key),
548 capacity_bps(capacity_bps),
549 bitrate_priority(bitrate_priority) {}
550
551 BitrateAllocatorObserver* allocation_key;
552 // The amount of bitrate bps that can be allocated to this observer.
553 uint32_t capacity_bps;
554 double bitrate_priority;
555
556 // We want to sort by which observers will be allocated their full capacity
557 // first. By dividing each observer's capacity by its bitrate priority we
558 // are "normalizing" the capacity of an observer by the rate it will be
559 // filled. This is because the amount allocated is based upon bitrate
560 // priority. We allocate twice as much bitrate to an observer with twice the
561 // bitrate priority of another.
562 bool operator<(const PriorityRateObserverConfig& other) const {
563 return capacity_bps / bitrate_priority <
564 other.capacity_bps / other.bitrate_priority;
565 }
566 };
567
568 double bitrate_priority_sum = 0;
569 std::vector<PriorityRateObserverConfig> priority_rate_observers;
570 for (const auto& observer_config : bitrate_observer_configs_) {
571 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
572 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
573 observer_config.bitrate_priority);
574 bitrate_priority_sum += observer_config.bitrate_priority;
575 }
576
577 // Iterate in the order observers can be allocated their full capacity.
578 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
579 size_t i;
580 for (i = 0; i < priority_rate_observers.size(); ++i) {
581 const auto& priority_rate_observer = priority_rate_observers[i];
582 // We allocate the full capacity to an observer only if its relative
583 // portion from the remaining bitrate is sufficient to allocate its full
584 // capacity. This means we aren't greedily allocating the full capacity, but
585 // that it is only done when there is also enough bitrate to allocate the
586 // proportional amounts to all other observers.
587 double observer_share =
588 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
589 double allocation_bps = observer_share * remaining_bitrate;
590 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
591 if (!enough_bitrate)
592 break;
593 allocation->at(priority_rate_observer.allocation_key) +=
594 priority_rate_observer.capacity_bps;
595 remaining_bitrate -= priority_rate_observer.capacity_bps;
596 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
597 }
598
599 // From the remaining bitrate, allocate the proportional amounts to the
600 // observers that aren't allocated their max capacity.
601 for (; i < priority_rate_observers.size(); ++i) {
602 const auto& priority_rate_observer = priority_rate_observers[i];
603 double fraction_allocated =
604 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
605 allocation->at(priority_rate_observer.allocation_key) +=
606 fraction_allocated * remaining_bitrate;
607 }
608}
609
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000610} // namespace webrtc