blob: a300f6d67bca8cd5f783fbf401177696e8812970 [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 Jansson29b204e2018-03-21 12:45:27 +010063 has_packet_feedback_(false),
Ying Wanga646d302018-03-02 17:04:11 +010064 bitrate_allocation_strategy_(nullptr),
65 transmission_max_bitrate_multiplier_(
66 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070067 sequenced_checker_.Detach();
68}
mflodman48a4beb2016-07-01 13:03:59 +020069
70BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070071 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
72 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020073}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000074
Niels Möller74e5f802018-04-25 14:03:46 +020075// static
Ying Wanga646d302018-03-02 17:04:11 +010076uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
77 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
78 "WebRTC-TransmissionMaxBitrateMultiplier")
79 .c_str(),
80 nullptr, 10);
81 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010082 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
83 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010084 return static_cast<uint8_t>(multiplier);
85 }
86 return kTransmissionMaxBitrateMultiplier;
87}
88
perkj71ee44c2016-06-15 00:47:53 -070089void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
90 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080091 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070092 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070093 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020094 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070095 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020096 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010097 last_fraction_loss_ = fraction_loss;
98 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -070099 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700100
mflodman48a4beb2016-07-01 13:03:59 +0200101 // Periodically log the incoming BWE.
102 int64_t now = clock_->TimeInMilliseconds();
103 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100104 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200105 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800106 }
mflodman48a4beb2016-07-01 13:03:59 +0200107
108 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
109
110 for (auto& config : bitrate_observer_configs_) {
111 uint32_t allocated_bitrate = allocation[config.observer];
112 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800113 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700114 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200115
116 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
117 if (target_bitrate_bps > 0)
118 ++num_pause_events_;
119 // The protection bitrate is an estimate based on the ratio between media
120 // and protection used before this observer was muted.
121 uint32_t predicted_protection_bps =
122 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
124 << " with configured min bitrate "
125 << config.min_bitrate_bps << " and current estimate of "
126 << target_bitrate_bps << " and protection bitrate "
127 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200128 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
129 if (target_bitrate_bps > 0)
130 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100131 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
132 << ", configured min bitrate " << config.min_bitrate_bps
133 << ", current allocation " << allocated_bitrate
134 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200135 }
136
137 // Only update the media ratio if the observer got an allocation.
138 if (allocated_bitrate > 0)
139 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
140 config.allocated_bitrate_bps = allocated_bitrate;
141 }
philipel5ef2bc12017-02-21 07:28:31 -0800142 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100143}
144
perkj57c21f92016-06-17 07:27:16 -0700145void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200146 MediaStreamAllocationConfig config) {
perkj26091b12016-09-01 01:17:40 -0700147 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200148 RTC_DCHECK_GT(config.bitrate_priority, 0);
149 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700150 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000151
mflodman101f2502016-06-09 17:21:19 +0200152 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700153 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200154 it->min_bitrate_bps = config.min_bitrate_bps;
155 it->max_bitrate_bps = config.max_bitrate_bps;
156 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
157 it->enforce_min_bitrate = config.enforce_min_bitrate;
158 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000159 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800160 bitrate_observer_configs_.push_back(ObserverConfig(
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200161 observer, config.min_bitrate_bps, config.max_bitrate_bps,
162 config.pad_up_bitrate_bps, config.enforce_min_bitrate, config.track_id,
163 config.bitrate_priority, config.has_packet_feedback));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000164 }
Stefan Holmere5904162015-03-26 11:11:06 +0100165
mflodman101f2502016-06-09 17:21:19 +0200166 ObserverAllocation allocation;
167 if (last_bitrate_bps_ > 0) {
168 // Calculate a new allocation and update all observers.
169 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200170 for (auto& config : bitrate_observer_configs_) {
171 uint32_t allocated_bitrate = allocation[config.observer];
172 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800173 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700174 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200175 config.allocated_bitrate_bps = allocated_bitrate;
176 if (allocated_bitrate > 0)
177 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
178 }
perkjfea93092016-05-14 00:58:48 -0700179 } else {
180 // Currently, an encoder is not allowed to produce frames.
181 // But we still have to return the initial config bitrate + let the
182 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200183 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800184 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700185 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100186 }
perkj71ee44c2016-06-15 00:47:53 -0700187 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000188}
189
perkj71ee44c2016-06-15 00:47:53 -0700190void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700191 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700192 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;
perkj26091b12016-09-01 01:17:40 -0700196 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800197 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700198 if (config.enforce_min_bitrate) {
199 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800200 } else if (config.allocated_bitrate_bps == 0) {
201 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100202 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700203 }
philipel5ef2bc12017-02-21 07:28:31 -0800204 total_requested_padding_bitrate += stream_padding;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200205 total_requested_max_bitrate += config.max_bitrate_bps;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100206 if (config.allocated_bitrate_bps > 0 && config.has_packet_feedback)
207 has_packet_feedback = true;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000208 }
perkj71ee44c2016-06-15 00:47:53 -0700209
philipel5ef2bc12017-02-21 07:28:31 -0800210 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100211 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200212 total_requested_max_bitrate == total_requested_max_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100213 has_packet_feedback == has_packet_feedback_) {
philipel5ef2bc12017-02-21 07:28:31 -0800214 return;
215 }
216
217 total_requested_min_bitrate_ = total_requested_min_bitrate;
218 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200219 total_requested_max_bitrate_ = total_requested_max_bitrate;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100220 has_packet_feedback_ = has_packet_feedback;
philipel5ef2bc12017-02-21 07:28:31 -0800221
Mirko Bonadei675513b2017-11-09 11:09:25 +0100222 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
223 << total_requested_min_bitrate
224 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200225 << total_requested_padding_bitrate
226 << "bps, total_requested_max_bitrate: "
227 << total_requested_max_bitrate << "bps";
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100228 limit_observer_->OnAllocationLimitsChanged(
229 total_requested_min_bitrate, total_requested_padding_bitrate,
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200230 total_requested_max_bitrate, has_packet_feedback);
perkj71ee44c2016-06-15 00:47:53 -0700231}
232
233void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700234 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200235
perkj26091b12016-09-01 01:17:40 -0700236 auto it = FindObserverConfig(observer);
237 if (it != bitrate_observer_configs_.end()) {
238 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700239 }
perkj26091b12016-09-01 01:17:40 -0700240
perkj71ee44c2016-06-15 00:47:53 -0700241 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000242}
243
perkj57c21f92016-06-17 07:27:16 -0700244int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700245 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200246 const auto& it = FindObserverConfig(observer);
247 if (it == bitrate_observer_configs_.end()) {
248 // This observer hasn't been added yet, just give it its fair share.
249 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700250 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200251 } else if (it->allocated_bitrate_bps == -1) {
252 // This observer hasn't received an allocation yet, so do the same.
253 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700254 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200255 } else {
256 // This observer already has an allocation.
257 return it->allocated_bitrate_bps;
258 }
perkj57c21f92016-06-17 07:27:16 -0700259}
260
Alex Narest78609d52017-10-20 10:37:47 +0200261void BitrateAllocator::SetBitrateAllocationStrategy(
262 std::unique_ptr<rtc::BitrateAllocationStrategy>
263 bitrate_allocation_strategy) {
264 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
265 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
266}
267
mflodman48a4beb2016-07-01 13:03:59 +0200268BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700269BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
270 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700271 for (auto it = bitrate_observer_configs_.begin();
272 it != bitrate_observer_configs_.end(); ++it) {
273 if (it->observer == observer)
274 return it;
275 }
276 return bitrate_observer_configs_.end();
277}
278
perkjfea93092016-05-14 00:58:48 -0700279BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
280 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700281 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700282 if (bitrate_observer_configs_.empty())
283 return ObserverAllocation();
284
Alex Narest78609d52017-10-20 10:37:47 +0200285 if (bitrate_allocation_strategy_ != nullptr) {
286 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
287 track_configs(bitrate_observer_configs_.size());
288 int i = 0;
289 for (const auto& c : bitrate_observer_configs_) {
290 track_configs[i++] = &c;
291 }
292 std::vector<uint32_t> track_allocations =
293 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
294 // The strategy should return allocation for all tracks.
295 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
296 ObserverAllocation allocation;
297 auto track_allocations_it = track_allocations.begin();
298 for (const auto& observer_config : bitrate_observer_configs_) {
299 allocation[observer_config.observer] = *track_allocations_it++;
300 }
301 return allocation;
302 }
303
perkjfea93092016-05-14 00:58:48 -0700304 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700305 return ZeroRateAllocation();
306
307 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200308 uint32_t sum_max_bitrates = 0;
309 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700310 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200311 sum_max_bitrates += observer_config.max_bitrate_bps;
312 }
313
314 // Not enough for all observers to get an allocation, allocate according to:
315 // enforced min bitrate -> allocated bitrate previous round -> restart paused
316 // streams.
317 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700318 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700319
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800320 // All observers will get their min bitrate plus a share of the rest. This
321 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200322 if (bitrate <= sum_max_bitrates)
323 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700324
Ying Wanga646d302018-03-02 17:04:11 +0100325 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200326 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000327}
328
mflodman2ebe5b12016-05-13 01:43:51 -0700329BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700330 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700331 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700332 for (const auto& observer_config : bitrate_observer_configs_)
333 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700334 return allocation;
335}
336
mflodman2ebe5b12016-05-13 01:43:51 -0700337BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100338 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700339 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700340 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200341 // Start by allocating bitrate to observers enforcing a min bitrate, hence
342 // remaining_bitrate might turn negative.
343 int64_t remaining_bitrate = bitrate;
344 for (const auto& observer_config : bitrate_observer_configs_) {
345 int32_t allocated_bitrate = 0;
346 if (observer_config.enforce_min_bitrate)
347 allocated_bitrate = observer_config.min_bitrate_bps;
348
349 allocation[observer_config.observer] = allocated_bitrate;
350 remaining_bitrate -= allocated_bitrate;
351 }
352
353 // Allocate bitrate to all previously active streams.
354 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700355 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200356 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100357 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200358 continue;
359
srte1eb051c2017-11-29 11:23:59 +0100360 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200361 if (remaining_bitrate >= required_bitrate) {
362 allocation[observer_config.observer] = required_bitrate;
363 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200364 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000365 }
366 }
mflodman101f2502016-06-09 17:21:19 +0200367
368 // Allocate bitrate to previously paused streams.
369 if (remaining_bitrate > 0) {
370 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100371 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200372 continue;
373
374 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100375 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200376 if (remaining_bitrate >= required_bitrate) {
377 allocation[observer_config.observer] = required_bitrate;
378 remaining_bitrate -= required_bitrate;
379 }
380 }
381 }
382
383 // Split a possible remainder evenly on all streams with an allocation.
384 if (remaining_bitrate > 0)
385 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
386
387 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100388 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000389}
mflodman101f2502016-06-09 17:21:19 +0200390
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800391// Allocates the bitrate based on the bitrate priority of each observer. This
392// bitrate priority defines the priority for bitrate to be allocated to that
393// observer in relation to other observers. For example with two observers, if
394// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
395// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
396// allocated twice the bitrate as observer 1 above the each observer's
397// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200398BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
399 uint32_t bitrate,
400 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700401 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200402 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800403 ObserverAllocation observers_capacities;
404 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200405 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800406 observers_capacities[observer_config.observer] =
407 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
408 }
mflodman101f2502016-06-09 17:21:19 +0200409
410 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800411 // From the remaining bitrate, allocate a proportional amount to each observer
412 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200413 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800414 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200415
416 return allocation;
417}
418
419BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700420 uint32_t bitrate,
421 uint32_t sum_max_bitrates) {
422 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200423 ObserverAllocation allocation;
424
425 for (const auto& observer_config : bitrate_observer_configs_) {
426 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
427 bitrate -= observer_config.max_bitrate_bps;
428 }
Ying Wanga646d302018-03-02 17:04:11 +0100429 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200430 &allocation);
431 return allocation;
432}
433
srte1eb051c2017-11-29 11:23:59 +0100434uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200435 // Return the configured minimum bitrate for newly added observers, to avoid
436 // requiring an extra high bitrate for the observer to get an allocated
437 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100438 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200439}
440
srte1eb051c2017-11-29 11:23:59 +0100441uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
442 uint32_t min_bitrate = min_bitrate_bps;
443 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200444 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
445 kMinToggleBitrateBps);
446 }
mflodman48a4beb2016-07-01 13:03:59 +0200447 // Account for protection bitrate used by this observer in the previous
448 // allocation.
449 // Note: the ratio will only be updated when the stream is active, meaning a
450 // paused stream won't get any ratio updates. This might lead to waiting a bit
451 // longer than necessary if the network condition improves, but this is to
452 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100453 if (media_ratio > 0.0 && media_ratio < 1.0)
454 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200455
mflodman101f2502016-06-09 17:21:19 +0200456 return min_bitrate;
457}
458
459void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
460 bool include_zero_allocations,
461 int max_multiplier,
462 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700463 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200464 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
465
466 ObserverSortingMap list_max_bitrates;
467 for (const auto& observer_config : bitrate_observer_configs_) {
468 if (include_zero_allocations ||
469 allocation->at(observer_config.observer) != 0) {
470 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
471 observer_config.max_bitrate_bps, &observer_config));
472 }
473 }
474 auto it = list_max_bitrates.begin();
475 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800476 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200477 uint32_t extra_allocation =
478 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
479 uint32_t total_allocation =
480 extra_allocation + allocation->at(it->second->observer);
481 bitrate -= extra_allocation;
482 if (total_allocation > max_multiplier * it->first) {
483 // There is more than we can fit for this observer, carry over to the
484 // remaining observers.
485 bitrate += total_allocation - max_multiplier * it->first;
486 total_allocation = max_multiplier * it->first;
487 }
488 // Finally, update the allocation for this observer.
489 allocation->at(it->second->observer) = total_allocation;
490 it = list_max_bitrates.erase(it);
491 }
492}
493
494bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
495 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700496 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200497 if (bitrate < sum_min_bitrates)
498 return false;
499
perkj26091b12016-09-01 01:17:40 -0700500 uint32_t extra_bitrate_per_observer =
501 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200502 static_cast<uint32_t>(bitrate_observer_configs_.size());
503 for (const auto& observer_config : bitrate_observer_configs_) {
504 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100505 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200506 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800507 }
mflodman101f2502016-06-09 17:21:19 +0200508 }
509 return true;
510}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800511
512void BitrateAllocator::DistributeBitrateRelatively(
513 uint32_t remaining_bitrate,
514 const ObserverAllocation& observers_capacities,
515 ObserverAllocation* allocation) {
516 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
517 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
518 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
519
520 struct PriorityRateObserverConfig {
521 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
522 uint32_t capacity_bps,
523 double bitrate_priority)
524 : allocation_key(allocation_key),
525 capacity_bps(capacity_bps),
526 bitrate_priority(bitrate_priority) {}
527
528 BitrateAllocatorObserver* allocation_key;
529 // The amount of bitrate bps that can be allocated to this observer.
530 uint32_t capacity_bps;
531 double bitrate_priority;
532
533 // We want to sort by which observers will be allocated their full capacity
534 // first. By dividing each observer's capacity by its bitrate priority we
535 // are "normalizing" the capacity of an observer by the rate it will be
536 // filled. This is because the amount allocated is based upon bitrate
537 // priority. We allocate twice as much bitrate to an observer with twice the
538 // bitrate priority of another.
539 bool operator<(const PriorityRateObserverConfig& other) const {
540 return capacity_bps / bitrate_priority <
541 other.capacity_bps / other.bitrate_priority;
542 }
543 };
544
545 double bitrate_priority_sum = 0;
546 std::vector<PriorityRateObserverConfig> priority_rate_observers;
547 for (const auto& observer_config : bitrate_observer_configs_) {
548 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
549 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
550 observer_config.bitrate_priority);
551 bitrate_priority_sum += observer_config.bitrate_priority;
552 }
553
554 // Iterate in the order observers can be allocated their full capacity.
555 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
556 size_t i;
557 for (i = 0; i < priority_rate_observers.size(); ++i) {
558 const auto& priority_rate_observer = priority_rate_observers[i];
559 // We allocate the full capacity to an observer only if its relative
560 // portion from the remaining bitrate is sufficient to allocate its full
561 // capacity. This means we aren't greedily allocating the full capacity, but
562 // that it is only done when there is also enough bitrate to allocate the
563 // proportional amounts to all other observers.
564 double observer_share =
565 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
566 double allocation_bps = observer_share * remaining_bitrate;
567 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
568 if (!enough_bitrate)
569 break;
570 allocation->at(priority_rate_observer.allocation_key) +=
571 priority_rate_observer.capacity_bps;
572 remaining_bitrate -= priority_rate_observer.capacity_bps;
573 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
574 }
575
576 // From the remaining bitrate, allocate the proportional amounts to the
577 // observers that aren't allocated their max capacity.
578 for (; i < priority_rate_observers.size(); ++i) {
579 const auto& priority_rate_observer = priority_rate_observers[i];
580 double fraction_allocated =
581 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
582 allocation->at(priority_rate_observer.allocation_key) +=
583 fraction_allocated * remaining_bitrate;
584 }
585}
586
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000587} // namespace webrtc