blob: 842c3cc51bfa5a7e9d8bce4f3cbd7688f39aeca2 [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),
Sebastian Jansson35fa2802018-10-01 09:16:12 +020067 allocated_without_feedback_(0),
Sebastian Jansson29b204e2018-03-21 12:45:27 +010068 has_packet_feedback_(false),
Ying Wanga646d302018-03-02 17:04:11 +010069 bitrate_allocation_strategy_(nullptr),
70 transmission_max_bitrate_multiplier_(
71 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070072 sequenced_checker_.Detach();
73}
mflodman48a4beb2016-07-01 13:03:59 +020074
75BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070076 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
77 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020078}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000079
Niels Möller74e5f802018-04-25 14:03:46 +020080// static
Ying Wanga646d302018-03-02 17:04:11 +010081uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
82 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
83 "WebRTC-TransmissionMaxBitrateMultiplier")
84 .c_str(),
85 nullptr, 10);
86 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010087 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
88 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010089 return static_cast<uint8_t>(multiplier);
90 }
91 return kTransmissionMaxBitrateMultiplier;
92}
93
perkj71ee44c2016-06-15 00:47:53 -070094void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
Sebastian Jansson89c94b92018-11-20 17:16:36 +010095 uint32_t link_capacity_bps,
perkj71ee44c2016-06-15 00:47:53 -070096 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080097 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070098 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070099 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100100 last_target_bps_ = target_bitrate_bps;
101 last_link_capacity_bps_ = link_capacity_bps;
perkjfea93092016-05-14 00:58:48 -0700102 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +0200103 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +0100104 last_fraction_loss_ = fraction_loss;
105 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700106 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700107
mflodman48a4beb2016-07-01 13:03:59 +0200108 // Periodically log the incoming BWE.
109 int64_t now = clock_->TimeInMilliseconds();
110 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100111 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200112 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800113 }
mflodman48a4beb2016-07-01 13:03:59 +0200114
115 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100116 ObserverAllocation bandwidth_allocation = AllocateBitrates(link_capacity_bps);
mflodman48a4beb2016-07-01 13:03:59 +0200117
118 for (auto& config : bitrate_observer_configs_) {
119 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100120 uint32_t allocated_bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100121 BitrateAllocationUpdate update;
122 update.target_bitrate = DataRate::bps(allocated_bitrate);
123 update.link_capacity = DataRate::bps(allocated_bandwidth);
124 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
125 update.round_trip_time = TimeDelta::ms(last_rtt_);
126 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
127 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200128
129 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
130 if (target_bitrate_bps > 0)
131 ++num_pause_events_;
132 // The protection bitrate is an estimate based on the ratio between media
133 // and protection used before this observer was muted.
134 uint32_t predicted_protection_bps =
135 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100136 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
137 << " with configured min bitrate "
138 << config.min_bitrate_bps << " and current estimate of "
139 << target_bitrate_bps << " and protection bitrate "
140 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200141 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
142 if (target_bitrate_bps > 0)
143 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100144 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
145 << ", configured min bitrate " << config.min_bitrate_bps
146 << ", current allocation " << allocated_bitrate
147 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200148 }
149
150 // Only update the media ratio if the observer got an allocation.
151 if (allocated_bitrate > 0)
152 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
153 config.allocated_bitrate_bps = allocated_bitrate;
154 }
philipel5ef2bc12017-02-21 07:28:31 -0800155 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100156}
157
perkj57c21f92016-06-17 07:27:16 -0700158void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200159 MediaStreamAllocationConfig config) {
perkj26091b12016-09-01 01:17:40 -0700160 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200161 RTC_DCHECK_GT(config.bitrate_priority, 0);
162 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700163 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000164
mflodman101f2502016-06-09 17:21:19 +0200165 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700166 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200167 it->min_bitrate_bps = config.min_bitrate_bps;
168 it->max_bitrate_bps = config.max_bitrate_bps;
169 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
170 it->enforce_min_bitrate = config.enforce_min_bitrate;
171 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000172 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800173 bitrate_observer_configs_.push_back(ObserverConfig(
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200174 observer, config.min_bitrate_bps, config.max_bitrate_bps,
175 config.pad_up_bitrate_bps, config.enforce_min_bitrate, config.track_id,
176 config.bitrate_priority, config.has_packet_feedback));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000177 }
Stefan Holmere5904162015-03-26 11:11:06 +0100178
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100179 if (last_target_bps_ > 0) {
mflodman101f2502016-06-09 17:21:19 +0200180 // Calculate a new allocation and update all observers.
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100181
182 ObserverAllocation allocation = AllocateBitrates(last_target_bps_);
183 ObserverAllocation bandwidth_allocation =
184 AllocateBitrates(last_link_capacity_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200185 for (auto& config : bitrate_observer_configs_) {
186 uint32_t allocated_bitrate = allocation[config.observer];
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100187 uint32_t bandwidth = bandwidth_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100188 BitrateAllocationUpdate update;
189 update.target_bitrate = DataRate::bps(allocated_bitrate);
190 update.link_capacity = DataRate::bps(bandwidth);
191 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
192 update.round_trip_time = TimeDelta::ms(last_rtt_);
193 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
194 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200195 config.allocated_bitrate_bps = allocated_bitrate;
196 if (allocated_bitrate > 0)
197 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
198 }
perkjfea93092016-05-14 00:58:48 -0700199 } else {
200 // Currently, an encoder is not allowed to produce frames.
201 // But we still have to return the initial config bitrate + let the
202 // observer know that it can not produce frames.
Sebastian Jansson13e59032018-11-21 19:13:07 +0100203
204 BitrateAllocationUpdate update;
205 update.target_bitrate = DataRate::Zero();
206 update.link_capacity = DataRate::Zero();
207 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
208 update.round_trip_time = TimeDelta::ms(last_rtt_);
209 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
210 observer->OnBitrateUpdated(update);
Stefan Holmere5904162015-03-26 11:11:06 +0100211 }
perkj71ee44c2016-06-15 00:47:53 -0700212 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000213}
214
perkj71ee44c2016-06-15 00:47:53 -0700215void BitrateAllocator::UpdateAllocationLimits() {
216 uint32_t total_requested_padding_bitrate = 0;
217 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200218 uint32_t total_requested_max_bitrate = 0;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100219 bool has_packet_feedback = false;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200220 uint32_t allocated_without_feedback = 0;
perkj26091b12016-09-01 01:17:40 -0700221 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800222 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700223 if (config.enforce_min_bitrate) {
224 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800225 } else if (config.allocated_bitrate_bps == 0) {
226 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100227 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700228 }
philipel5ef2bc12017-02-21 07:28:31 -0800229 total_requested_padding_bitrate += stream_padding;
Erik Språngd1d7b232018-12-06 17:31:25 +0100230 uint32_t max_bitrate_bps = config.max_bitrate_bps;
231 if (config.media_ratio > 0) {
232 max_bitrate_bps =
233 static_cast<uint32_t>(max_bitrate_bps / config.media_ratio);
234 }
235 total_requested_max_bitrate += max_bitrate_bps;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100236 if (config.allocated_bitrate_bps > 0 && config.has_packet_feedback)
237 has_packet_feedback = true;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200238 // TODO(srte): Remove field trial check.
239 if (!config.has_packet_feedback &&
240 field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC"))
241 allocated_without_feedback += config.allocated_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000242 }
perkj71ee44c2016-06-15 00:47:53 -0700243
philipel5ef2bc12017-02-21 07:28:31 -0800244 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100245 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200246 total_requested_max_bitrate == total_requested_max_bitrate_ &&
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200247 allocated_without_feedback == allocated_without_feedback_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100248 has_packet_feedback == has_packet_feedback_) {
philipel5ef2bc12017-02-21 07:28:31 -0800249 return;
250 }
251
252 total_requested_min_bitrate_ = total_requested_min_bitrate;
253 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200254 total_requested_max_bitrate_ = total_requested_max_bitrate;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200255 allocated_without_feedback_ = allocated_without_feedback;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100256 has_packet_feedback_ = has_packet_feedback;
philipel5ef2bc12017-02-21 07:28:31 -0800257
Mirko Bonadei675513b2017-11-09 11:09:25 +0100258 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
259 << total_requested_min_bitrate
260 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200261 << total_requested_padding_bitrate
262 << "bps, total_requested_max_bitrate: "
263 << total_requested_max_bitrate << "bps";
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100264 limit_observer_->OnAllocationLimitsChanged(
265 total_requested_min_bitrate, total_requested_padding_bitrate,
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200266 total_requested_max_bitrate, allocated_without_feedback,
267 has_packet_feedback);
perkj71ee44c2016-06-15 00:47:53 -0700268}
269
270void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700271 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200272
perkj26091b12016-09-01 01:17:40 -0700273 auto it = FindObserverConfig(observer);
274 if (it != bitrate_observer_configs_.end()) {
275 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700276 }
perkj26091b12016-09-01 01:17:40 -0700277
perkj71ee44c2016-06-15 00:47:53 -0700278 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000279}
280
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200281int BitrateAllocator::GetStartBitrate(
282 BitrateAllocatorObserver* observer) const {
perkj26091b12016-09-01 01:17:40 -0700283 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200284 const auto& it = FindObserverConfig(observer);
285 if (it == bitrate_observer_configs_.end()) {
286 // This observer hasn't been added yet, just give it its fair share.
287 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700288 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200289 } else if (it->allocated_bitrate_bps == -1) {
290 // This observer hasn't received an allocation yet, so do the same.
291 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700292 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200293 } else {
294 // This observer already has an allocation.
295 return it->allocated_bitrate_bps;
296 }
perkj57c21f92016-06-17 07:27:16 -0700297}
298
Alex Narest78609d52017-10-20 10:37:47 +0200299void BitrateAllocator::SetBitrateAllocationStrategy(
300 std::unique_ptr<rtc::BitrateAllocationStrategy>
301 bitrate_allocation_strategy) {
302 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
303 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
304}
305
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200306BitrateAllocator::ObserverConfigs::const_iterator
307BitrateAllocator::FindObserverConfig(
308 const BitrateAllocatorObserver* observer) const {
309 for (auto it = bitrate_observer_configs_.begin();
310 it != bitrate_observer_configs_.end(); ++it) {
311 if (it->observer == observer)
312 return it;
313 }
314 return bitrate_observer_configs_.end();
315}
316
mflodman48a4beb2016-07-01 13:03:59 +0200317BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700318BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700319 for (auto it = bitrate_observer_configs_.begin();
320 it != bitrate_observer_configs_.end(); ++it) {
321 if (it->observer == observer)
322 return it;
323 }
324 return bitrate_observer_configs_.end();
325}
326
perkjfea93092016-05-14 00:58:48 -0700327BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200328 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700329 if (bitrate_observer_configs_.empty())
330 return ObserverAllocation();
331
Alex Narest78609d52017-10-20 10:37:47 +0200332 if (bitrate_allocation_strategy_ != nullptr) {
Sebastian Janssone2754c92018-10-24 16:02:26 +0200333 // Note: This intentionally causes slicing, we only copy the fields in
334 // ObserverConfig that are inherited from TrackConfig.
335 std::vector<rtc::BitrateAllocationStrategy::TrackConfig> track_configs(
336 bitrate_observer_configs_.begin(), bitrate_observer_configs_.end());
337
Alex Narest78609d52017-10-20 10:37:47 +0200338 std::vector<uint32_t> track_allocations =
Sebastian Janssone2754c92018-10-24 16:02:26 +0200339 bitrate_allocation_strategy_->AllocateBitrates(
340 bitrate, std::move(track_configs));
Alex Narest78609d52017-10-20 10:37:47 +0200341 // The strategy should return allocation for all tracks.
342 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
343 ObserverAllocation allocation;
344 auto track_allocations_it = track_allocations.begin();
345 for (const auto& observer_config : bitrate_observer_configs_) {
346 allocation[observer_config.observer] = *track_allocations_it++;
347 }
348 return allocation;
349 }
350
perkjfea93092016-05-14 00:58:48 -0700351 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700352 return ZeroRateAllocation();
353
354 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200355 uint32_t sum_max_bitrates = 0;
356 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700357 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200358 sum_max_bitrates += observer_config.max_bitrate_bps;
359 }
360
361 // Not enough for all observers to get an allocation, allocate according to:
362 // enforced min bitrate -> allocated bitrate previous round -> restart paused
363 // streams.
364 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700365 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700366
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800367 // All observers will get their min bitrate plus a share of the rest. This
368 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200369 if (bitrate <= sum_max_bitrates)
370 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700371
Ying Wanga646d302018-03-02 17:04:11 +0100372 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200373 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000374}
375
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200376BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
377 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700378 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700379 for (const auto& observer_config : bitrate_observer_configs_)
380 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700381 return allocation;
382}
383
mflodman2ebe5b12016-05-13 01:43:51 -0700384BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200385 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700386 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200387 // Start by allocating bitrate to observers enforcing a min bitrate, hence
388 // remaining_bitrate might turn negative.
389 int64_t remaining_bitrate = bitrate;
390 for (const auto& observer_config : bitrate_observer_configs_) {
391 int32_t allocated_bitrate = 0;
392 if (observer_config.enforce_min_bitrate)
393 allocated_bitrate = observer_config.min_bitrate_bps;
394
395 allocation[observer_config.observer] = allocated_bitrate;
396 remaining_bitrate -= allocated_bitrate;
397 }
398
399 // Allocate bitrate to all previously active streams.
400 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700401 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200402 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100403 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200404 continue;
405
srte1eb051c2017-11-29 11:23:59 +0100406 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200407 if (remaining_bitrate >= required_bitrate) {
408 allocation[observer_config.observer] = required_bitrate;
409 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200410 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000411 }
412 }
mflodman101f2502016-06-09 17:21:19 +0200413
414 // Allocate bitrate to previously paused streams.
415 if (remaining_bitrate > 0) {
416 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100417 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200418 continue;
419
420 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100421 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200422 if (remaining_bitrate >= required_bitrate) {
423 allocation[observer_config.observer] = required_bitrate;
424 remaining_bitrate -= required_bitrate;
425 }
426 }
427 }
428
429 // Split a possible remainder evenly on all streams with an allocation.
430 if (remaining_bitrate > 0)
431 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
432
433 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100434 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000435}
mflodman101f2502016-06-09 17:21:19 +0200436
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800437// Allocates the bitrate based on the bitrate priority of each observer. This
438// bitrate priority defines the priority for bitrate to be allocated to that
439// observer in relation to other observers. For example with two observers, if
440// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
441// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
442// allocated twice the bitrate as observer 1 above the each observer's
443// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200444BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
445 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200446 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200447 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800448 ObserverAllocation observers_capacities;
449 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200450 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800451 observers_capacities[observer_config.observer] =
452 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
453 }
mflodman101f2502016-06-09 17:21:19 +0200454
455 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800456 // From the remaining bitrate, allocate a proportional amount to each observer
457 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200458 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800459 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200460
461 return allocation;
462}
463
464BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700465 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200466 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200467 ObserverAllocation allocation;
468
469 for (const auto& observer_config : bitrate_observer_configs_) {
470 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
471 bitrate -= observer_config.max_bitrate_bps;
472 }
Ying Wanga646d302018-03-02 17:04:11 +0100473 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200474 &allocation);
475 return allocation;
476}
477
srte1eb051c2017-11-29 11:23:59 +0100478uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200479 // Return the configured minimum bitrate for newly added observers, to avoid
480 // requiring an extra high bitrate for the observer to get an allocated
481 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100482 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200483}
484
srte1eb051c2017-11-29 11:23:59 +0100485uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
486 uint32_t min_bitrate = min_bitrate_bps;
487 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200488 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
489 kMinToggleBitrateBps);
490 }
mflodman48a4beb2016-07-01 13:03:59 +0200491 // Account for protection bitrate used by this observer in the previous
492 // allocation.
493 // Note: the ratio will only be updated when the stream is active, meaning a
494 // paused stream won't get any ratio updates. This might lead to waiting a bit
495 // longer than necessary if the network condition improves, but this is to
496 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100497 if (media_ratio > 0.0 && media_ratio < 1.0)
498 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200499
mflodman101f2502016-06-09 17:21:19 +0200500 return min_bitrate;
501}
502
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200503void BitrateAllocator::DistributeBitrateEvenly(
504 uint32_t bitrate,
505 bool include_zero_allocations,
506 int max_multiplier,
507 ObserverAllocation* allocation) const {
mflodman101f2502016-06-09 17:21:19 +0200508 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
509
510 ObserverSortingMap list_max_bitrates;
511 for (const auto& observer_config : bitrate_observer_configs_) {
512 if (include_zero_allocations ||
513 allocation->at(observer_config.observer) != 0) {
514 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
515 observer_config.max_bitrate_bps, &observer_config));
516 }
517 }
518 auto it = list_max_bitrates.begin();
519 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800520 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200521 uint32_t extra_allocation =
522 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
523 uint32_t total_allocation =
524 extra_allocation + allocation->at(it->second->observer);
525 bitrate -= extra_allocation;
526 if (total_allocation > max_multiplier * it->first) {
527 // There is more than we can fit for this observer, carry over to the
528 // remaining observers.
529 bitrate += total_allocation - max_multiplier * it->first;
530 total_allocation = max_multiplier * it->first;
531 }
532 // Finally, update the allocation for this observer.
533 allocation->at(it->second->observer) = total_allocation;
534 it = list_max_bitrates.erase(it);
535 }
536}
537
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200538bool BitrateAllocator::EnoughBitrateForAllObservers(
539 uint32_t bitrate,
540 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200541 if (bitrate < sum_min_bitrates)
542 return false;
543
perkj26091b12016-09-01 01:17:40 -0700544 uint32_t extra_bitrate_per_observer =
545 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200546 static_cast<uint32_t>(bitrate_observer_configs_.size());
547 for (const auto& observer_config : bitrate_observer_configs_) {
548 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100549 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200550 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800551 }
mflodman101f2502016-06-09 17:21:19 +0200552 }
553 return true;
554}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800555
556void BitrateAllocator::DistributeBitrateRelatively(
557 uint32_t remaining_bitrate,
558 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200559 ObserverAllocation* allocation) const {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800560 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
561 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
562
563 struct PriorityRateObserverConfig {
564 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
565 uint32_t capacity_bps,
566 double bitrate_priority)
567 : allocation_key(allocation_key),
568 capacity_bps(capacity_bps),
569 bitrate_priority(bitrate_priority) {}
570
571 BitrateAllocatorObserver* allocation_key;
572 // The amount of bitrate bps that can be allocated to this observer.
573 uint32_t capacity_bps;
574 double bitrate_priority;
575
576 // We want to sort by which observers will be allocated their full capacity
577 // first. By dividing each observer's capacity by its bitrate priority we
578 // are "normalizing" the capacity of an observer by the rate it will be
579 // filled. This is because the amount allocated is based upon bitrate
580 // priority. We allocate twice as much bitrate to an observer with twice the
581 // bitrate priority of another.
582 bool operator<(const PriorityRateObserverConfig& other) const {
583 return capacity_bps / bitrate_priority <
584 other.capacity_bps / other.bitrate_priority;
585 }
586 };
587
588 double bitrate_priority_sum = 0;
589 std::vector<PriorityRateObserverConfig> priority_rate_observers;
590 for (const auto& observer_config : bitrate_observer_configs_) {
591 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
592 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
593 observer_config.bitrate_priority);
594 bitrate_priority_sum += observer_config.bitrate_priority;
595 }
596
597 // Iterate in the order observers can be allocated their full capacity.
598 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
599 size_t i;
600 for (i = 0; i < priority_rate_observers.size(); ++i) {
601 const auto& priority_rate_observer = priority_rate_observers[i];
602 // We allocate the full capacity to an observer only if its relative
603 // portion from the remaining bitrate is sufficient to allocate its full
604 // capacity. This means we aren't greedily allocating the full capacity, but
605 // that it is only done when there is also enough bitrate to allocate the
606 // proportional amounts to all other observers.
607 double observer_share =
608 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
609 double allocation_bps = observer_share * remaining_bitrate;
610 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
611 if (!enough_bitrate)
612 break;
613 allocation->at(priority_rate_observer.allocation_key) +=
614 priority_rate_observer.capacity_bps;
615 remaining_bitrate -= priority_rate_observer.capacity_bps;
616 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
617 }
618
619 // From the remaining bitrate, allocate the proportional amounts to the
620 // observers that aren't allocated their max capacity.
621 for (; i < priority_rate_observers.size(); ++i) {
622 const auto& priority_rate_observer = priority_rate_observers[i];
623 double fraction_allocated =
624 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
625 allocation->at(priority_rate_observer.allocation_key) +=
626 fraction_allocated * remaining_bitrate;
627 }
628}
629
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000630} // namespace webrtc