blob: 64d1a4861e934a59c2e33f8f88477f765fbe0962 [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),
Sergey Ulanove2b15012016-11-22 16:08:30 -080053 last_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -070054 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010055 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020056 last_rtt_(0),
57 num_pause_events_(0),
58 clock_(Clock::GetRealTimeClock()),
philipel5ef2bc12017-02-21 07:28:31 -080059 last_bwe_log_time_(0),
60 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020061 total_requested_min_bitrate_(0),
Sebastian Jansson448f4d52018-04-04 14:52:07 +020062 total_requested_max_bitrate_(0),
Sebastian Jansson35fa2802018-10-01 09:16:12 +020063 allocated_without_feedback_(0),
Sebastian Jansson29b204e2018-03-21 12:45:27 +010064 has_packet_feedback_(false),
Ying Wanga646d302018-03-02 17:04:11 +010065 bitrate_allocation_strategy_(nullptr),
66 transmission_max_bitrate_multiplier_(
67 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070068 sequenced_checker_.Detach();
69}
mflodman48a4beb2016-07-01 13:03:59 +020070
71BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070072 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
73 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020074}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000075
Niels Möller74e5f802018-04-25 14:03:46 +020076// static
Ying Wanga646d302018-03-02 17:04:11 +010077uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
78 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
79 "WebRTC-TransmissionMaxBitrateMultiplier")
80 .c_str(),
81 nullptr, 10);
82 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010083 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
84 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010085 return static_cast<uint8_t>(multiplier);
86 }
87 return kTransmissionMaxBitrateMultiplier;
88}
89
perkj71ee44c2016-06-15 00:47:53 -070090void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
91 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080092 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070093 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070094 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020095 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070096 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020097 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010098 last_fraction_loss_ = fraction_loss;
99 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -0700100 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700101
mflodman48a4beb2016-07-01 13:03:59 +0200102 // Periodically log the incoming BWE.
103 int64_t now = clock_->TimeInMilliseconds();
104 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100105 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200106 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800107 }
mflodman48a4beb2016-07-01 13:03:59 +0200108
109 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
110
111 for (auto& config : bitrate_observer_configs_) {
112 uint32_t allocated_bitrate = allocation[config.observer];
113 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200114 BitrateAllocationUpdate{allocated_bitrate, last_fraction_loss_,
115 last_rtt_, last_bwe_period_ms_});
mflodman48a4beb2016-07-01 13:03:59 +0200116
117 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
118 if (target_bitrate_bps > 0)
119 ++num_pause_events_;
120 // The protection bitrate is an estimate based on the ratio between media
121 // and protection used before this observer was muted.
122 uint32_t predicted_protection_bps =
123 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100124 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
125 << " with configured min bitrate "
126 << config.min_bitrate_bps << " and current estimate of "
127 << target_bitrate_bps << " and protection bitrate "
128 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200129 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
130 if (target_bitrate_bps > 0)
131 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100132 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
133 << ", configured min bitrate " << config.min_bitrate_bps
134 << ", current allocation " << allocated_bitrate
135 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200136 }
137
138 // Only update the media ratio if the observer got an allocation.
139 if (allocated_bitrate > 0)
140 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
141 config.allocated_bitrate_bps = allocated_bitrate;
142 }
philipel5ef2bc12017-02-21 07:28:31 -0800143 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100144}
145
perkj57c21f92016-06-17 07:27:16 -0700146void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200147 MediaStreamAllocationConfig config) {
perkj26091b12016-09-01 01:17:40 -0700148 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200149 RTC_DCHECK_GT(config.bitrate_priority, 0);
150 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700151 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000152
mflodman101f2502016-06-09 17:21:19 +0200153 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700154 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200155 it->min_bitrate_bps = config.min_bitrate_bps;
156 it->max_bitrate_bps = config.max_bitrate_bps;
157 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
158 it->enforce_min_bitrate = config.enforce_min_bitrate;
159 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000160 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800161 bitrate_observer_configs_.push_back(ObserverConfig(
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200162 observer, config.min_bitrate_bps, config.max_bitrate_bps,
163 config.pad_up_bitrate_bps, config.enforce_min_bitrate, config.track_id,
164 config.bitrate_priority, config.has_packet_feedback));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000165 }
Stefan Holmere5904162015-03-26 11:11:06 +0100166
mflodman101f2502016-06-09 17:21:19 +0200167 ObserverAllocation allocation;
168 if (last_bitrate_bps_ > 0) {
169 // Calculate a new allocation and update all observers.
170 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200171 for (auto& config : bitrate_observer_configs_) {
172 uint32_t allocated_bitrate = allocation[config.observer];
173 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200174 BitrateAllocationUpdate{allocated_bitrate, last_fraction_loss_,
175 last_rtt_, last_bwe_period_ms_});
mflodman48a4beb2016-07-01 13:03:59 +0200176 config.allocated_bitrate_bps = allocated_bitrate;
177 if (allocated_bitrate > 0)
178 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
179 }
perkjfea93092016-05-14 00:58:48 -0700180 } else {
181 // Currently, an encoder is not allowed to produce frames.
182 // But we still have to return the initial config bitrate + let the
183 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200184 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200185 observer->OnBitrateUpdated(BitrateAllocationUpdate{
186 0, last_fraction_loss_, last_rtt_, last_bwe_period_ms_});
Stefan Holmere5904162015-03-26 11:11:06 +0100187 }
perkj71ee44c2016-06-15 00:47:53 -0700188 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000189}
190
perkj71ee44c2016-06-15 00:47:53 -0700191void BitrateAllocator::UpdateAllocationLimits() {
192 uint32_t total_requested_padding_bitrate = 0;
193 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200194 uint32_t total_requested_max_bitrate = 0;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100195 bool has_packet_feedback = false;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200196 uint32_t allocated_without_feedback = 0;
perkj26091b12016-09-01 01:17:40 -0700197 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800198 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700199 if (config.enforce_min_bitrate) {
200 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800201 } else if (config.allocated_bitrate_bps == 0) {
202 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100203 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700204 }
philipel5ef2bc12017-02-21 07:28:31 -0800205 total_requested_padding_bitrate += stream_padding;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200206 total_requested_max_bitrate += config.max_bitrate_bps;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100207 if (config.allocated_bitrate_bps > 0 && config.has_packet_feedback)
208 has_packet_feedback = true;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200209 // TODO(srte): Remove field trial check.
210 if (!config.has_packet_feedback &&
211 field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC"))
212 allocated_without_feedback += config.allocated_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000213 }
perkj71ee44c2016-06-15 00:47:53 -0700214
philipel5ef2bc12017-02-21 07:28:31 -0800215 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100216 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200217 total_requested_max_bitrate == total_requested_max_bitrate_ &&
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200218 allocated_without_feedback == allocated_without_feedback_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100219 has_packet_feedback == has_packet_feedback_) {
philipel5ef2bc12017-02-21 07:28:31 -0800220 return;
221 }
222
223 total_requested_min_bitrate_ = total_requested_min_bitrate;
224 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200225 total_requested_max_bitrate_ = total_requested_max_bitrate;
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200226 allocated_without_feedback_ = allocated_without_feedback;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100227 has_packet_feedback_ = has_packet_feedback;
philipel5ef2bc12017-02-21 07:28:31 -0800228
Mirko Bonadei675513b2017-11-09 11:09:25 +0100229 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
230 << total_requested_min_bitrate
231 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200232 << total_requested_padding_bitrate
233 << "bps, total_requested_max_bitrate: "
234 << total_requested_max_bitrate << "bps";
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100235 limit_observer_->OnAllocationLimitsChanged(
236 total_requested_min_bitrate, total_requested_padding_bitrate,
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200237 total_requested_max_bitrate, allocated_without_feedback,
238 has_packet_feedback);
perkj71ee44c2016-06-15 00:47:53 -0700239}
240
241void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700242 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200243
perkj26091b12016-09-01 01:17:40 -0700244 auto it = FindObserverConfig(observer);
245 if (it != bitrate_observer_configs_.end()) {
246 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700247 }
perkj26091b12016-09-01 01:17:40 -0700248
perkj71ee44c2016-06-15 00:47:53 -0700249 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000250}
251
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200252int BitrateAllocator::GetStartBitrate(
253 BitrateAllocatorObserver* observer) const {
perkj26091b12016-09-01 01:17:40 -0700254 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200255 const auto& it = FindObserverConfig(observer);
256 if (it == bitrate_observer_configs_.end()) {
257 // This observer hasn't been added yet, just give it its fair share.
258 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700259 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200260 } else if (it->allocated_bitrate_bps == -1) {
261 // This observer hasn't received an allocation yet, so do the same.
262 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700263 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200264 } else {
265 // This observer already has an allocation.
266 return it->allocated_bitrate_bps;
267 }
perkj57c21f92016-06-17 07:27:16 -0700268}
269
Alex Narest78609d52017-10-20 10:37:47 +0200270void BitrateAllocator::SetBitrateAllocationStrategy(
271 std::unique_ptr<rtc::BitrateAllocationStrategy>
272 bitrate_allocation_strategy) {
273 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
274 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
275}
276
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200277BitrateAllocator::ObserverConfigs::const_iterator
278BitrateAllocator::FindObserverConfig(
279 const BitrateAllocatorObserver* observer) const {
280 for (auto it = bitrate_observer_configs_.begin();
281 it != bitrate_observer_configs_.end(); ++it) {
282 if (it->observer == observer)
283 return it;
284 }
285 return bitrate_observer_configs_.end();
286}
287
mflodman48a4beb2016-07-01 13:03:59 +0200288BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700289BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700290 for (auto it = bitrate_observer_configs_.begin();
291 it != bitrate_observer_configs_.end(); ++it) {
292 if (it->observer == observer)
293 return it;
294 }
295 return bitrate_observer_configs_.end();
296}
297
perkjfea93092016-05-14 00:58:48 -0700298BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200299 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700300 if (bitrate_observer_configs_.empty())
301 return ObserverAllocation();
302
Alex Narest78609d52017-10-20 10:37:47 +0200303 if (bitrate_allocation_strategy_ != nullptr) {
304 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
305 track_configs(bitrate_observer_configs_.size());
306 int i = 0;
307 for (const auto& c : bitrate_observer_configs_) {
308 track_configs[i++] = &c;
309 }
310 std::vector<uint32_t> track_allocations =
311 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
312 // The strategy should return allocation for all tracks.
313 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
314 ObserverAllocation allocation;
315 auto track_allocations_it = track_allocations.begin();
316 for (const auto& observer_config : bitrate_observer_configs_) {
317 allocation[observer_config.observer] = *track_allocations_it++;
318 }
319 return allocation;
320 }
321
perkjfea93092016-05-14 00:58:48 -0700322 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700323 return ZeroRateAllocation();
324
325 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200326 uint32_t sum_max_bitrates = 0;
327 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700328 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200329 sum_max_bitrates += observer_config.max_bitrate_bps;
330 }
331
332 // Not enough for all observers to get an allocation, allocate according to:
333 // enforced min bitrate -> allocated bitrate previous round -> restart paused
334 // streams.
335 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700336 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700337
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800338 // All observers will get their min bitrate plus a share of the rest. This
339 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200340 if (bitrate <= sum_max_bitrates)
341 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700342
Ying Wanga646d302018-03-02 17:04:11 +0100343 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200344 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000345}
346
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200347BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation()
348 const {
mflodman2ebe5b12016-05-13 01:43:51 -0700349 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700350 for (const auto& observer_config : bitrate_observer_configs_)
351 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700352 return allocation;
353}
354
mflodman2ebe5b12016-05-13 01:43:51 -0700355BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200356 uint32_t bitrate) const {
mflodman2ebe5b12016-05-13 01:43:51 -0700357 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200358 // Start by allocating bitrate to observers enforcing a min bitrate, hence
359 // remaining_bitrate might turn negative.
360 int64_t remaining_bitrate = bitrate;
361 for (const auto& observer_config : bitrate_observer_configs_) {
362 int32_t allocated_bitrate = 0;
363 if (observer_config.enforce_min_bitrate)
364 allocated_bitrate = observer_config.min_bitrate_bps;
365
366 allocation[observer_config.observer] = allocated_bitrate;
367 remaining_bitrate -= allocated_bitrate;
368 }
369
370 // Allocate bitrate to all previously active streams.
371 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700372 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200373 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100374 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200375 continue;
376
srte1eb051c2017-11-29 11:23:59 +0100377 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200378 if (remaining_bitrate >= required_bitrate) {
379 allocation[observer_config.observer] = required_bitrate;
380 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200381 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000382 }
383 }
mflodman101f2502016-06-09 17:21:19 +0200384
385 // Allocate bitrate to previously paused streams.
386 if (remaining_bitrate > 0) {
387 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100388 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200389 continue;
390
391 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100392 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200393 if (remaining_bitrate >= required_bitrate) {
394 allocation[observer_config.observer] = required_bitrate;
395 remaining_bitrate -= required_bitrate;
396 }
397 }
398 }
399
400 // Split a possible remainder evenly on all streams with an allocation.
401 if (remaining_bitrate > 0)
402 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
403
404 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100405 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000406}
mflodman101f2502016-06-09 17:21:19 +0200407
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800408// Allocates the bitrate based on the bitrate priority of each observer. This
409// bitrate priority defines the priority for bitrate to be allocated to that
410// observer in relation to other observers. For example with two observers, if
411// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
412// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
413// allocated twice the bitrate as observer 1 above the each observer's
414// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200415BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
416 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200417 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200418 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800419 ObserverAllocation observers_capacities;
420 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200421 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800422 observers_capacities[observer_config.observer] =
423 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
424 }
mflodman101f2502016-06-09 17:21:19 +0200425
426 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800427 // From the remaining bitrate, allocate a proportional amount to each observer
428 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200429 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800430 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200431
432 return allocation;
433}
434
435BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700436 uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200437 uint32_t sum_max_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200438 ObserverAllocation allocation;
439
440 for (const auto& observer_config : bitrate_observer_configs_) {
441 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
442 bitrate -= observer_config.max_bitrate_bps;
443 }
Ying Wanga646d302018-03-02 17:04:11 +0100444 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200445 &allocation);
446 return allocation;
447}
448
srte1eb051c2017-11-29 11:23:59 +0100449uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200450 // Return the configured minimum bitrate for newly added observers, to avoid
451 // requiring an extra high bitrate for the observer to get an allocated
452 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100453 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200454}
455
srte1eb051c2017-11-29 11:23:59 +0100456uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
457 uint32_t min_bitrate = min_bitrate_bps;
458 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200459 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
460 kMinToggleBitrateBps);
461 }
mflodman48a4beb2016-07-01 13:03:59 +0200462 // Account for protection bitrate used by this observer in the previous
463 // allocation.
464 // Note: the ratio will only be updated when the stream is active, meaning a
465 // paused stream won't get any ratio updates. This might lead to waiting a bit
466 // longer than necessary if the network condition improves, but this is to
467 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100468 if (media_ratio > 0.0 && media_ratio < 1.0)
469 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200470
mflodman101f2502016-06-09 17:21:19 +0200471 return min_bitrate;
472}
473
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200474void BitrateAllocator::DistributeBitrateEvenly(
475 uint32_t bitrate,
476 bool include_zero_allocations,
477 int max_multiplier,
478 ObserverAllocation* allocation) const {
mflodman101f2502016-06-09 17:21:19 +0200479 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
480
481 ObserverSortingMap list_max_bitrates;
482 for (const auto& observer_config : bitrate_observer_configs_) {
483 if (include_zero_allocations ||
484 allocation->at(observer_config.observer) != 0) {
485 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
486 observer_config.max_bitrate_bps, &observer_config));
487 }
488 }
489 auto it = list_max_bitrates.begin();
490 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800491 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200492 uint32_t extra_allocation =
493 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
494 uint32_t total_allocation =
495 extra_allocation + allocation->at(it->second->observer);
496 bitrate -= extra_allocation;
497 if (total_allocation > max_multiplier * it->first) {
498 // There is more than we can fit for this observer, carry over to the
499 // remaining observers.
500 bitrate += total_allocation - max_multiplier * it->first;
501 total_allocation = max_multiplier * it->first;
502 }
503 // Finally, update the allocation for this observer.
504 allocation->at(it->second->observer) = total_allocation;
505 it = list_max_bitrates.erase(it);
506 }
507}
508
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200509bool BitrateAllocator::EnoughBitrateForAllObservers(
510 uint32_t bitrate,
511 uint32_t sum_min_bitrates) const {
mflodman101f2502016-06-09 17:21:19 +0200512 if (bitrate < sum_min_bitrates)
513 return false;
514
perkj26091b12016-09-01 01:17:40 -0700515 uint32_t extra_bitrate_per_observer =
516 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200517 static_cast<uint32_t>(bitrate_observer_configs_.size());
518 for (const auto& observer_config : bitrate_observer_configs_) {
519 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100520 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200521 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800522 }
mflodman101f2502016-06-09 17:21:19 +0200523 }
524 return true;
525}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800526
527void BitrateAllocator::DistributeBitrateRelatively(
528 uint32_t remaining_bitrate,
529 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200530 ObserverAllocation* allocation) const {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800531 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
532 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
533
534 struct PriorityRateObserverConfig {
535 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
536 uint32_t capacity_bps,
537 double bitrate_priority)
538 : allocation_key(allocation_key),
539 capacity_bps(capacity_bps),
540 bitrate_priority(bitrate_priority) {}
541
542 BitrateAllocatorObserver* allocation_key;
543 // The amount of bitrate bps that can be allocated to this observer.
544 uint32_t capacity_bps;
545 double bitrate_priority;
546
547 // We want to sort by which observers will be allocated their full capacity
548 // first. By dividing each observer's capacity by its bitrate priority we
549 // are "normalizing" the capacity of an observer by the rate it will be
550 // filled. This is because the amount allocated is based upon bitrate
551 // priority. We allocate twice as much bitrate to an observer with twice the
552 // bitrate priority of another.
553 bool operator<(const PriorityRateObserverConfig& other) const {
554 return capacity_bps / bitrate_priority <
555 other.capacity_bps / other.bitrate_priority;
556 }
557 };
558
559 double bitrate_priority_sum = 0;
560 std::vector<PriorityRateObserverConfig> priority_rate_observers;
561 for (const auto& observer_config : bitrate_observer_configs_) {
562 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
563 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
564 observer_config.bitrate_priority);
565 bitrate_priority_sum += observer_config.bitrate_priority;
566 }
567
568 // Iterate in the order observers can be allocated their full capacity.
569 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
570 size_t i;
571 for (i = 0; i < priority_rate_observers.size(); ++i) {
572 const auto& priority_rate_observer = priority_rate_observers[i];
573 // We allocate the full capacity to an observer only if its relative
574 // portion from the remaining bitrate is sufficient to allocate its full
575 // capacity. This means we aren't greedily allocating the full capacity, but
576 // that it is only done when there is also enough bitrate to allocate the
577 // proportional amounts to all other observers.
578 double observer_share =
579 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
580 double allocation_bps = observer_share * remaining_bitrate;
581 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
582 if (!enough_bitrate)
583 break;
584 allocation->at(priority_rate_observer.allocation_key) +=
585 priority_rate_observer.capacity_bps;
586 remaining_bitrate -= priority_rate_observer.capacity_bps;
587 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
588 }
589
590 // From the remaining bitrate, allocate the proportional amounts to the
591 // observers that aren't allocated their max capacity.
592 for (; i < priority_rate_observers.size(); ++i) {
593 const auto& priority_rate_observer = priority_rate_observers[i];
594 double fraction_allocated =
595 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
596 allocation->at(priority_rate_observer.allocation_key) +=
597 fraction_allocated * remaining_bitrate;
598 }
599}
600
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000601} // namespace webrtc