blob: 172f9401f1e2d7f48e65d1932b2e98fe1a3fc551 [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
Stefan Holmere5904162015-03-26 11:11:06 +010030// Allow packets to be transmitted in up to 2 times max video bitrate if the
31// bandwidth estimate allows it.
Ying Wanga646d302018-03-02 17:04:11 +010032const uint8_t kTransmissionMaxBitrateMultiplier = 2;
Stefan Holmere5904162015-03-26 11:11:06 +010033const int kDefaultBitrateBps = 300000;
34
mflodman101f2502016-06-09 17:21:19 +020035// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
36const double kToggleFactor = 0.1;
37const uint32_t kMinToggleBitrateBps = 20000;
38
mflodman48a4beb2016-07-01 13:03:59 +020039const int64_t kBweLogIntervalMs = 5000;
40
41namespace {
42
43double 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}
51} // namespace
52
perkj71ee44c2016-06-15 00:47:53 -070053BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
54 : limit_observer_(limit_observer),
Sebastian Jansson89c94b92018-11-20 17:16:36 +010055 last_target_bps_(0),
56 last_link_capacity_bps_(0),
perkjfea93092016-05-14 00:58:48 -070057 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010058 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020059 last_rtt_(0),
Sebastian Jansson13e59032018-11-21 19:13:07 +010060 last_bwe_period_ms_(1000),
mflodman48a4beb2016-07-01 13:03:59 +020061 num_pause_events_(0),
62 clock_(Clock::GetRealTimeClock()),
philipel5ef2bc12017-02-21 07:28:31 -080063 last_bwe_log_time_(0),
64 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020065 total_requested_min_bitrate_(0),
Sebastian Jansson448f4d52018-04-04 14:52:07 +020066 total_requested_max_bitrate_(0),
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
Sebastian Jansson2701bc92018-12-11 15:02:47 +010078void BitrateAllocator::UpdateStartRate(uint32_t start_rate_bps) {
79 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
80 last_non_zero_bitrate_bps_ = start_rate_bps;
81}
82
Niels Möller74e5f802018-04-25 14:03:46 +020083// static
Ying Wanga646d302018-03-02 17:04:11 +010084uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
85 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
86 "WebRTC-TransmissionMaxBitrateMultiplier")
87 .c_str(),
88 nullptr, 10);
89 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010090 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
91 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010092 return static_cast<uint8_t>(multiplier);
93 }
94 return kTransmissionMaxBitrateMultiplier;
95}
96
perkj71ee44c2016-06-15 00:47:53 -070097void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
Sebastian Jansson89c94b92018-11-20 17:16:36 +010098 uint32_t link_capacity_bps,
perkj71ee44c2016-06-15 00:47:53 -070099 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800100 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700101 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -0700102 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100103 last_target_bps_ = target_bitrate_bps;
104 last_link_capacity_bps_ = link_capacity_bps;
perkjfea93092016-05-14 00:58:48 -0700105 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +0200106 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +0100107 last_fraction_loss_ = fraction_loss;
108 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700109 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700110
mflodman48a4beb2016-07-01 13:03:59 +0200111 // Periodically log the incoming BWE.
112 int64_t now = clock_->TimeInMilliseconds();
113 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200115 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800116 }
mflodman48a4beb2016-07-01 13:03:59 +0200117
118 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100119 ObserverAllocation bandwidth_allocation = AllocateBitrates(link_capacity_bps);
mflodman48a4beb2016-07-01 13:03:59 +0200120
121 for (auto& config : bitrate_observer_configs_) {
122 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100123 uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100124 BitrateAllocationUpdate update;
125 update.target_bitrate = DataRate::bps(allocated_bitrate);
126 update.link_capacity = DataRate::bps(allocated_bandwidth);
127 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
128 update.round_trip_time = TimeDelta::ms(last_rtt_);
129 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
130 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200131
132 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
133 if (target_bitrate_bps > 0)
134 ++num_pause_events_;
135 // The protection bitrate is an estimate based on the ratio between media
136 // and protection used before this observer was muted.
137 uint32_t predicted_protection_bps =
138 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100139 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
140 << " with configured min bitrate "
141 << config.min_bitrate_bps << " and current estimate of "
142 << target_bitrate_bps << " and protection bitrate "
143 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200144 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
145 if (target_bitrate_bps > 0)
146 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100147 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
148 << ", configured min bitrate " << config.min_bitrate_bps
149 << ", current allocation " << allocated_bitrate
150 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200151 }
152
153 // Only update the media ratio if the observer got an allocation.
154 if (allocated_bitrate > 0)
155 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
156 config.allocated_bitrate_bps = allocated_bitrate;
157 }
philipel5ef2bc12017-02-21 07:28:31 -0800158 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100159}
160
perkj57c21f92016-06-17 07:27:16 -0700161void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200162 MediaStreamAllocationConfig config) {
perkj26091b12016-09-01 01:17:40 -0700163 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200164 RTC_DCHECK_GT(config.bitrate_priority, 0);
165 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700166 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000167
mflodman101f2502016-06-09 17:21:19 +0200168 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700169 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200170 it->min_bitrate_bps = config.min_bitrate_bps;
171 it->max_bitrate_bps = config.max_bitrate_bps;
172 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
173 it->enforce_min_bitrate = config.enforce_min_bitrate;
174 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000175 } else {
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100176 bitrate_observer_configs_.push_back(
177 ObserverConfig(observer, config.min_bitrate_bps, config.max_bitrate_bps,
178 config.pad_up_bitrate_bps, config.enforce_min_bitrate,
179 config.track_id, config.bitrate_priority));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000180 }
Stefan Holmere5904162015-03-26 11:11:06 +0100181
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100182 if (last_target_bps_ > 0) {
mflodman101f2502016-06-09 17:21:19 +0200183 // Calculate a new allocation and update all observers.
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100184
185 ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
186 ObserverAllocation bandwidth_allocation =
187 AllocateBitrates(last_link_capacity_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200188 for (auto& config : bitrate_observer_configs_) {
189 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100190 uint32_t bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100191 BitrateAllocationUpdate update;
192 update.target_bitrate = DataRate::bps(allocated_bitrate);
193 update.link_capacity = DataRate::bps(bandwidth);
194 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
195 update.round_trip_time = TimeDelta::ms(last_rtt_);
196 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
197 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200198 config.allocated_bitrate_bps = allocated_bitrate;
199 if (allocated_bitrate > 0)
200 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
201 }
perkjfea93092016-05-14 00:58:48 -0700202 } else {
203 // Currently, an encoder is not allowed to produce frames.
204 // But we still have to return the initial config bitrate + let the
205 // observer know that it can not produce frames.
Sebastian Jansson13e59032018-11-21 19:13:07 +0100206
207 BitrateAllocationUpdate update;
208 update.target_bitrate = DataRate::Zero();
209 update.link_capacity = DataRate::Zero();
210 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
211 update.round_trip_time = TimeDelta::ms(last_rtt_);
212 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
213 observer->OnBitrateUpdated(update);
Stefan Holmere5904162015-03-26 11:11:06 +0100214 }
perkj71ee44c2016-06-15 00:47:53 -0700215 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000216}
217
perkj71ee44c2016-06-15 00:47:53 -0700218void BitrateAllocator::UpdateAllocationLimits() {
219 uint32_t total_requested_padding_bitrate = 0;
220 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200221 uint32_t total_requested_max_bitrate = 0;
perkj26091b12016-09-01 01:17:40 -0700222 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800223 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700224 if (config.enforce_min_bitrate) {
225 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800226 } else if (config.allocated_bitrate_bps == 0) {
227 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100228 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700229 }
philipel5ef2bc12017-02-21 07:28:31 -0800230 total_requested_padding_bitrate += stream_padding;
Erik Språngd1d7b232018-12-06 17:31:25 +0100231 uint32_t max_bitrate_bps = config.max_bitrate_bps;
Erik Språng00672b12018-12-11 15:29:29 +0100232 if (config.media_ratio < 1.0) {
233 // Account for protection overhead (eg FEC). Assumption is that overhead
234 // is never more than 100%. Don't adjust based exact value as that might
235 // trigger too frequent calls to OnAllocationLimitsChanged().
236 max_bitrate_bps *= 2;
Erik Språngd1d7b232018-12-06 17:31:25 +0100237 }
238 total_requested_max_bitrate += max_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000239 }
perkj71ee44c2016-06-15 00:47:53 -0700240
philipel5ef2bc12017-02-21 07:28:31 -0800241 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100242 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100243 total_requested_max_bitrate == total_requested_max_bitrate_) {
philipel5ef2bc12017-02-21 07:28:31 -0800244 return;
245 }
246
247 total_requested_min_bitrate_ = total_requested_min_bitrate;
248 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200249 total_requested_max_bitrate_ = total_requested_max_bitrate;
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 Jansson79f0d4d2019-01-23 09:41:43 +0100257 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
258 total_requested_padding_bitrate,
259 total_requested_max_bitrate);
perkj71ee44c2016-06-15 00:47:53 -0700260}
261
262void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700263 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200264
perkj26091b12016-09-01 01:17:40 -0700265 auto it = FindObserverConfig(observer);
266 if (it != bitrate_observer_configs_.end()) {
267 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700268 }
perkj26091b12016-09-01 01:17:40 -0700269
perkj71ee44c2016-06-15 00:47:53 -0700270 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000271}
272
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200273int BitrateAllocator::GetStartBitrate(
274 BitrateAllocatorObserver* observer) const {
perkj26091b12016-09-01 01:17:40 -0700275 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200276 const auto& it = FindObserverConfig(observer);
277 if (it == bitrate_observer_configs_.end()) {
278 // This observer hasn't been added yet, just give it its fair share.
279 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700280 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200281 } else if (it->allocated_bitrate_bps == -1) {
282 // This observer hasn't received an allocation yet, so do the same.
283 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700284 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200285 } else {
286 // This observer already has an allocation.
287 return it->allocated_bitrate_bps;
288 }
perkj57c21f92016-06-17 07:27:16 -0700289}
290
Alex Narest78609d52017-10-20 10:37:47 +0200291void BitrateAllocator::SetBitrateAllocationStrategy(
292 std::unique_ptr<rtc::BitrateAllocationStrategy>
293 bitrate_allocation_strategy) {
294 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
295 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
296}
297
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200298BitrateAllocator::ObserverConfigs::const_iterator
299BitrateAllocator::FindObserverConfig(
300 const BitrateAllocatorObserver* observer) const {
301 for (auto it = bitrate_observer_configs_.begin();
302 it != bitrate_observer_configs_.end(); ++it) {
303 if (it->observer == observer)
304 return it;
305 }
306 return bitrate_observer_configs_.end();
307}
308
mflodman48a4beb2016-07-01 13:03:59 +0200309BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700310BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700311 for (auto it = bitrate_observer_configs_.begin();
312 it != bitrate_observer_configs_.end(); ++it) {
313 if (it->observer == observer)
314 return it;
315 }
316 return bitrate_observer_configs_.end();
317}
318
perkjfea93092016-05-14 00:58:48 -0700319BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200320 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700321 if (bitrate_observer_configs_.empty())
322 return ObserverAllocation();
323
Alex Narest78609d52017-10-20 10:37:47 +0200324 if (bitrate_allocation_strategy_ != nullptr) {
Sebastian Janssone2754c92018-10-24 16:02:26 +0200325 // Note: This intentionally causes slicing, we only copy the fields in
326 // ObserverConfig that are inherited from TrackConfig.
327 std::vector<rtc::BitrateAllocationStrategy::TrackConfig> track_configs(
328 bitrate_observer_configs_.begin(), bitrate_observer_configs_.end());
329
Alex Narest78609d52017-10-20 10:37:47 +0200330 std::vector<uint32_t> track_allocations =
Sebastian Janssone2754c92018-10-24 16:02:26 +0200331 bitrate_allocation_strategy_->AllocateBitrates(
332 bitrate, std::move(track_configs));
Alex Narest78609d52017-10-20 10:37:47 +0200333 // The strategy should return allocation for all tracks.
334 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
335 ObserverAllocation allocation;
336 auto track_allocations_it = track_allocations.begin();
337 for (const auto& observer_config : bitrate_observer_configs_) {
338 allocation[observer_config.observer] = *track_allocations_it++;
339 }
340 return allocation;
341 }
342
perkjfea93092016-05-14 00:58:48 -0700343 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700344 return ZeroRateAllocation();
345
346 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200347 uint32_t sum_max_bitrates = 0;
348 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700349 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200350 sum_max_bitrates += observer_config.max_bitrate_bps;
351 }
352
353 // Not enough for all observers to get an allocation, allocate according to:
354 // enforced min bitrate -> allocated bitrate previous round -> restart paused
355 // streams.
356 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700357 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700358
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800359 // All observers will get their min bitrate plus a share of the rest. This
360 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200361 if (bitrate <= sum_max_bitrates)
362 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700363
Ying Wanga646d302018-03-02 17:04:11 +0100364 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200365 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000366}
367
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200368BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
369 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700370 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700371 for (const auto& observer_config : bitrate_observer_configs_)
372 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700373 return allocation;
374}
375
mflodman2ebe5b12016-05-13 01:43:51 -0700376BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200377 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700378 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200379 // Start by allocating bitrate to observers enforcing a min bitrate, hence
380 // remaining_bitrate might turn negative.
381 int64_t remaining_bitrate = bitrate;
382 for (const auto& observer_config : bitrate_observer_configs_) {
383 int32_t allocated_bitrate = 0;
384 if (observer_config.enforce_min_bitrate)
385 allocated_bitrate = observer_config.min_bitrate_bps;
386
387 allocation[observer_config.observer] = allocated_bitrate;
388 remaining_bitrate -= allocated_bitrate;
389 }
390
391 // Allocate bitrate to all previously active streams.
392 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700393 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200394 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100395 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200396 continue;
397
srte1eb051c2017-11-29 11:23:59 +0100398 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200399 if (remaining_bitrate >= required_bitrate) {
400 allocation[observer_config.observer] = required_bitrate;
401 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200402 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000403 }
404 }
mflodman101f2502016-06-09 17:21:19 +0200405
406 // Allocate bitrate to previously paused streams.
407 if (remaining_bitrate > 0) {
408 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100409 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200410 continue;
411
412 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100413 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200414 if (remaining_bitrate >= required_bitrate) {
415 allocation[observer_config.observer] = required_bitrate;
416 remaining_bitrate -= required_bitrate;
417 }
418 }
419 }
420
421 // Split a possible remainder evenly on all streams with an allocation.
422 if (remaining_bitrate > 0)
423 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
424
425 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100426 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000427}
mflodman101f2502016-06-09 17:21:19 +0200428
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800429// Allocates the bitrate based on the bitrate priority of each observer. This
430// bitrate priority defines the priority for bitrate to be allocated to that
431// observer in relation to other observers. For example with two observers, if
432// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
433// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
434// allocated twice the bitrate as observer 1 above the each observer's
435// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200436BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
437 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200438 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200439 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800440 ObserverAllocation observers_capacities;
441 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200442 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800443 observers_capacities[observer_config.observer] =
444 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
445 }
mflodman101f2502016-06-09 17:21:19 +0200446
447 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800448 // From the remaining bitrate, allocate a proportional amount to each observer
449 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200450 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800451 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200452
453 return allocation;
454}
455
456BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700457 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200458 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200459 ObserverAllocation allocation;
460
461 for (const auto& observer_config : bitrate_observer_configs_) {
462 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
463 bitrate -= observer_config.max_bitrate_bps;
464 }
Ying Wanga646d302018-03-02 17:04:11 +0100465 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200466 &allocation);
467 return allocation;
468}
469
srte1eb051c2017-11-29 11:23:59 +0100470uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200471 // Return the configured minimum bitrate for newly added observers, to avoid
472 // requiring an extra high bitrate for the observer to get an allocated
473 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100474 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200475}
476
srte1eb051c2017-11-29 11:23:59 +0100477uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
478 uint32_t min_bitrate = min_bitrate_bps;
479 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200480 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
481 kMinToggleBitrateBps);
482 }
mflodman48a4beb2016-07-01 13:03:59 +0200483 // Account for protection bitrate used by this observer in the previous
484 // allocation.
485 // Note: the ratio will only be updated when the stream is active, meaning a
486 // paused stream won't get any ratio updates. This might lead to waiting a bit
487 // longer than necessary if the network condition improves, but this is to
488 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100489 if (media_ratio > 0.0 && media_ratio < 1.0)
490 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200491
mflodman101f2502016-06-09 17:21:19 +0200492 return min_bitrate;
493}
494
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200495void BitrateAllocator::DistributeBitrateEvenly(
496 uint32_t bitrate,
497 bool include_zero_allocations,
498 int max_multiplier,
499 ObserverAllocation* allocation) const {
mflodman101f2502016-06-09 17:21:19 +0200500 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
501
502 ObserverSortingMap list_max_bitrates;
503 for (const auto& observer_config : bitrate_observer_configs_) {
504 if (include_zero_allocations ||
505 allocation->at(observer_config.observer) != 0) {
506 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
507 observer_config.max_bitrate_bps, &observer_config));
508 }
509 }
510 auto it = list_max_bitrates.begin();
511 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800512 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200513 uint32_t extra_allocation =
514 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
515 uint32_t total_allocation =
516 extra_allocation + allocation->at(it->second->observer);
517 bitrate -= extra_allocation;
518 if (total_allocation > max_multiplier * it->first) {
519 // There is more than we can fit for this observer, carry over to the
520 // remaining observers.
521 bitrate += total_allocation - max_multiplier * it->first;
522 total_allocation = max_multiplier * it->first;
523 }
524 // Finally, update the allocation for this observer.
525 allocation->at(it->second->observer) = total_allocation;
526 it = list_max_bitrates.erase(it);
527 }
528}
529
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200530bool BitrateAllocator::EnoughBitrateForAllObservers(
531 uint32_t bitrate,
532 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200533 if (bitrate < sum_min_bitrates)
534 return false;
535
perkj26091b12016-09-01 01:17:40 -0700536 uint32_t extra_bitrate_per_observer =
537 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200538 static_cast<uint32_t>(bitrate_observer_configs_.size());
539 for (const auto& observer_config : bitrate_observer_configs_) {
540 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100541 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200542 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800543 }
mflodman101f2502016-06-09 17:21:19 +0200544 }
545 return true;
546}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800547
548void BitrateAllocator::DistributeBitrateRelatively(
549 uint32_t remaining_bitrate,
550 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200551 ObserverAllocation* allocation) const {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800552 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
553 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
554
555 struct PriorityRateObserverConfig {
556 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
557 uint32_t capacity_bps,
558 double bitrate_priority)
559 : allocation_key(allocation_key),
560 capacity_bps(capacity_bps),
561 bitrate_priority(bitrate_priority) {}
562
563 BitrateAllocatorObserver* allocation_key;
564 // The amount of bitrate bps that can be allocated to this observer.
565 uint32_t capacity_bps;
566 double bitrate_priority;
567
568 // We want to sort by which observers will be allocated their full capacity
569 // first. By dividing each observer's capacity by its bitrate priority we
570 // are "normalizing" the capacity of an observer by the rate it will be
571 // filled. This is because the amount allocated is based upon bitrate
572 // priority. We allocate twice as much bitrate to an observer with twice the
573 // bitrate priority of another.
574 bool operator<(const PriorityRateObserverConfig& other) const {
575 return capacity_bps / bitrate_priority <
576 other.capacity_bps / other.bitrate_priority;
577 }
578 };
579
580 double bitrate_priority_sum = 0;
581 std::vector<PriorityRateObserverConfig> priority_rate_observers;
582 for (const auto& observer_config : bitrate_observer_configs_) {
583 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
584 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
585 observer_config.bitrate_priority);
586 bitrate_priority_sum += observer_config.bitrate_priority;
587 }
588
589 // Iterate in the order observers can be allocated their full capacity.
590 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
591 size_t i;
592 for (i = 0; i < priority_rate_observers.size(); ++i) {
593 const auto& priority_rate_observer = priority_rate_observers[i];
594 // We allocate the full capacity to an observer only if its relative
595 // portion from the remaining bitrate is sufficient to allocate its full
596 // capacity. This means we aren't greedily allocating the full capacity, but
597 // that it is only done when there is also enough bitrate to allocate the
598 // proportional amounts to all other observers.
599 double observer_share =
600 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
601 double allocation_bps = observer_share * remaining_bitrate;
602 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
603 if (!enough_bitrate)
604 break;
605 allocation->at(priority_rate_observer.allocation_key) +=
606 priority_rate_observer.capacity_bps;
607 remaining_bitrate -= priority_rate_observer.capacity_bps;
608 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
609 }
610
611 // From the remaining bitrate, allocate the proportional amounts to the
612 // observers that aren't allocated their max capacity.
613 for (; i < priority_rate_observers.size(); ++i) {
614 const auto& priority_rate_observer = priority_rate_observers[i];
615 double fraction_allocated =
616 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
617 allocation->at(priority_rate_observer.allocation_key) +=
618 fraction_allocated * remaining_bitrate;
619 }
620}
621
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000622} // namespace webrtc