blob: 278c58108d4ddc949fec827d896ae37af8a7f1b9 [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),
Ying Wanga646d302018-03-02 17:04:11 +010062 bitrate_allocation_strategy_(nullptr),
63 transmission_max_bitrate_multiplier_(
64 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070065 sequenced_checker_.Detach();
66}
mflodman48a4beb2016-07-01 13:03:59 +020067
68BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070069 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
70 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020071}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000072
Ying Wanga646d302018-03-02 17:04:11 +010073uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
74 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
75 "WebRTC-TransmissionMaxBitrateMultiplier")
76 .c_str(),
77 nullptr, 10);
78 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010079 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
80 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010081 return static_cast<uint8_t>(multiplier);
82 }
83 return kTransmissionMaxBitrateMultiplier;
84}
85
perkj71ee44c2016-06-15 00:47:53 -070086void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
87 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080088 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070089 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070090 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020091 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070092 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020093 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010094 last_fraction_loss_ = fraction_loss;
95 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -070096 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -070097
mflodman48a4beb2016-07-01 13:03:59 +020098 // Periodically log the incoming BWE.
99 int64_t now = clock_->TimeInMilliseconds();
100 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100101 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200102 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800103 }
mflodman48a4beb2016-07-01 13:03:59 +0200104
105 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
106
107 for (auto& config : bitrate_observer_configs_) {
108 uint32_t allocated_bitrate = allocation[config.observer];
109 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800110 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700111 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200112
113 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
114 if (target_bitrate_bps > 0)
115 ++num_pause_events_;
116 // The protection bitrate is an estimate based on the ratio between media
117 // and protection used before this observer was muted.
118 uint32_t predicted_protection_bps =
119 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100120 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
121 << " with configured min bitrate "
122 << config.min_bitrate_bps << " and current estimate of "
123 << target_bitrate_bps << " and protection bitrate "
124 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200125 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
126 if (target_bitrate_bps > 0)
127 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100128 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
129 << ", configured min bitrate " << config.min_bitrate_bps
130 << ", current allocation " << allocated_bitrate
131 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200132 }
133
134 // Only update the media ratio if the observer got an allocation.
135 if (allocated_bitrate > 0)
136 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
137 config.allocated_bitrate_bps = allocated_bitrate;
138 }
philipel5ef2bc12017-02-21 07:28:31 -0800139 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100140}
141
perkj57c21f92016-06-17 07:27:16 -0700142void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
143 uint32_t min_bitrate_bps,
144 uint32_t max_bitrate_bps,
145 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200146 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800147 std::string track_id,
148 double bitrate_priority) {
perkj26091b12016-09-01 01:17:40 -0700149 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800150 RTC_DCHECK_GT(bitrate_priority, 0);
151 RTC_DCHECK(std::isnormal(bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700152 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000153
mflodman101f2502016-06-09 17:21:19 +0200154 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700155 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700156 it->min_bitrate_bps = min_bitrate_bps;
157 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700158 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200159 it->enforce_min_bitrate = enforce_min_bitrate;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800160 it->bitrate_priority = bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000161 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800162 bitrate_observer_configs_.push_back(ObserverConfig(
163 observer, min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps,
164 enforce_min_bitrate, track_id, bitrate_priority));
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(
minyue78b4d562016-11-30 04:47:39 -0800174 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700175 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_);
minyue78b4d562016-11-30 04:47:39 -0800185 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700186 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() {
perkj26091b12016-09-01 01:17:40 -0700192 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700193 uint32_t total_requested_padding_bitrate = 0;
194 uint32_t total_requested_min_bitrate = 0;
philipelf69e7682018-02-28 13:06:28 +0100195 uint32_t total_requested_bitrate = 0;
perkj71ee44c2016-06-15 00:47:53 -0700196
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;
philipelf69e7682018-02-28 13:06:28 +0100206 total_requested_bitrate += config.max_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000207 }
perkj71ee44c2016-06-15 00:47:53 -0700208
philipel5ef2bc12017-02-21 07:28:31 -0800209 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
210 total_requested_min_bitrate == total_requested_min_bitrate_) {
211 return;
212 }
213
214 total_requested_min_bitrate_ = total_requested_min_bitrate;
215 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
216
Mirko Bonadei675513b2017-11-09 11:09:25 +0100217 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
218 << total_requested_min_bitrate
219 << "bps, total_requested_padding_bitrate: "
220 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700221 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
philipelf69e7682018-02-28 13:06:28 +0100222 total_requested_padding_bitrate,
223 total_requested_bitrate);
perkj71ee44c2016-06-15 00:47:53 -0700224}
225
226void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700227 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200228
perkj26091b12016-09-01 01:17:40 -0700229 auto it = FindObserverConfig(observer);
230 if (it != bitrate_observer_configs_.end()) {
231 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700232 }
perkj26091b12016-09-01 01:17:40 -0700233
perkj71ee44c2016-06-15 00:47:53 -0700234 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000235}
236
perkj57c21f92016-06-17 07:27:16 -0700237int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700238 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200239 const auto& it = FindObserverConfig(observer);
240 if (it == bitrate_observer_configs_.end()) {
241 // This observer hasn't been added yet, just give it its fair share.
242 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700243 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200244 } else if (it->allocated_bitrate_bps == -1) {
245 // This observer hasn't received an allocation yet, so do the same.
246 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700247 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200248 } else {
249 // This observer already has an allocation.
250 return it->allocated_bitrate_bps;
251 }
perkj57c21f92016-06-17 07:27:16 -0700252}
253
Alex Narest78609d52017-10-20 10:37:47 +0200254void BitrateAllocator::SetBitrateAllocationStrategy(
255 std::unique_ptr<rtc::BitrateAllocationStrategy>
256 bitrate_allocation_strategy) {
257 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
258 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
259}
260
mflodman48a4beb2016-07-01 13:03:59 +0200261BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700262BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
263 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700264 for (auto it = bitrate_observer_configs_.begin();
265 it != bitrate_observer_configs_.end(); ++it) {
266 if (it->observer == observer)
267 return it;
268 }
269 return bitrate_observer_configs_.end();
270}
271
perkjfea93092016-05-14 00:58:48 -0700272BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
273 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700274 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700275 if (bitrate_observer_configs_.empty())
276 return ObserverAllocation();
277
Alex Narest78609d52017-10-20 10:37:47 +0200278 if (bitrate_allocation_strategy_ != nullptr) {
279 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
280 track_configs(bitrate_observer_configs_.size());
281 int i = 0;
282 for (const auto& c : bitrate_observer_configs_) {
283 track_configs[i++] = &c;
284 }
285 std::vector<uint32_t> track_allocations =
286 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
287 // The strategy should return allocation for all tracks.
288 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
289 ObserverAllocation allocation;
290 auto track_allocations_it = track_allocations.begin();
291 for (const auto& observer_config : bitrate_observer_configs_) {
292 allocation[observer_config.observer] = *track_allocations_it++;
293 }
294 return allocation;
295 }
296
perkjfea93092016-05-14 00:58:48 -0700297 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700298 return ZeroRateAllocation();
299
300 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200301 uint32_t sum_max_bitrates = 0;
302 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700303 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200304 sum_max_bitrates += observer_config.max_bitrate_bps;
305 }
306
307 // Not enough for all observers to get an allocation, allocate according to:
308 // enforced min bitrate -> allocated bitrate previous round -> restart paused
309 // streams.
310 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700311 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700312
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800313 // All observers will get their min bitrate plus a share of the rest. This
314 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200315 if (bitrate <= sum_max_bitrates)
316 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700317
Ying Wanga646d302018-03-02 17:04:11 +0100318 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200319 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000320}
321
mflodman2ebe5b12016-05-13 01:43:51 -0700322BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700323 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700324 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700325 for (const auto& observer_config : bitrate_observer_configs_)
326 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700327 return allocation;
328}
329
mflodman2ebe5b12016-05-13 01:43:51 -0700330BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100331 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700332 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700333 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200334 // Start by allocating bitrate to observers enforcing a min bitrate, hence
335 // remaining_bitrate might turn negative.
336 int64_t remaining_bitrate = bitrate;
337 for (const auto& observer_config : bitrate_observer_configs_) {
338 int32_t allocated_bitrate = 0;
339 if (observer_config.enforce_min_bitrate)
340 allocated_bitrate = observer_config.min_bitrate_bps;
341
342 allocation[observer_config.observer] = allocated_bitrate;
343 remaining_bitrate -= allocated_bitrate;
344 }
345
346 // Allocate bitrate to all previously active streams.
347 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700348 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200349 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100350 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200351 continue;
352
srte1eb051c2017-11-29 11:23:59 +0100353 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200354 if (remaining_bitrate >= required_bitrate) {
355 allocation[observer_config.observer] = required_bitrate;
356 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200357 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000358 }
359 }
mflodman101f2502016-06-09 17:21:19 +0200360
361 // Allocate bitrate to previously paused streams.
362 if (remaining_bitrate > 0) {
363 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100364 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200365 continue;
366
367 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100368 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200369 if (remaining_bitrate >= required_bitrate) {
370 allocation[observer_config.observer] = required_bitrate;
371 remaining_bitrate -= required_bitrate;
372 }
373 }
374 }
375
376 // Split a possible remainder evenly on all streams with an allocation.
377 if (remaining_bitrate > 0)
378 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
379
380 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100381 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000382}
mflodman101f2502016-06-09 17:21:19 +0200383
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800384// Allocates the bitrate based on the bitrate priority of each observer. This
385// bitrate priority defines the priority for bitrate to be allocated to that
386// observer in relation to other observers. For example with two observers, if
387// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
388// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
389// allocated twice the bitrate as observer 1 above the each observer's
390// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200391BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
392 uint32_t bitrate,
393 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700394 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200395 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800396 ObserverAllocation observers_capacities;
397 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200398 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800399 observers_capacities[observer_config.observer] =
400 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
401 }
mflodman101f2502016-06-09 17:21:19 +0200402
403 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800404 // From the remaining bitrate, allocate a proportional amount to each observer
405 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200406 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800407 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200408
409 return allocation;
410}
411
412BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700413 uint32_t bitrate,
414 uint32_t sum_max_bitrates) {
415 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200416 ObserverAllocation allocation;
417
418 for (const auto& observer_config : bitrate_observer_configs_) {
419 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
420 bitrate -= observer_config.max_bitrate_bps;
421 }
Ying Wanga646d302018-03-02 17:04:11 +0100422 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200423 &allocation);
424 return allocation;
425}
426
srte1eb051c2017-11-29 11:23:59 +0100427uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200428 // Return the configured minimum bitrate for newly added observers, to avoid
429 // requiring an extra high bitrate for the observer to get an allocated
430 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100431 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200432}
433
srte1eb051c2017-11-29 11:23:59 +0100434uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
435 uint32_t min_bitrate = min_bitrate_bps;
436 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200437 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
438 kMinToggleBitrateBps);
439 }
mflodman48a4beb2016-07-01 13:03:59 +0200440 // Account for protection bitrate used by this observer in the previous
441 // allocation.
442 // Note: the ratio will only be updated when the stream is active, meaning a
443 // paused stream won't get any ratio updates. This might lead to waiting a bit
444 // longer than necessary if the network condition improves, but this is to
445 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100446 if (media_ratio > 0.0 && media_ratio < 1.0)
447 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200448
mflodman101f2502016-06-09 17:21:19 +0200449 return min_bitrate;
450}
451
452void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
453 bool include_zero_allocations,
454 int max_multiplier,
455 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700456 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200457 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
458
459 ObserverSortingMap list_max_bitrates;
460 for (const auto& observer_config : bitrate_observer_configs_) {
461 if (include_zero_allocations ||
462 allocation->at(observer_config.observer) != 0) {
463 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
464 observer_config.max_bitrate_bps, &observer_config));
465 }
466 }
467 auto it = list_max_bitrates.begin();
468 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800469 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200470 uint32_t extra_allocation =
471 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
472 uint32_t total_allocation =
473 extra_allocation + allocation->at(it->second->observer);
474 bitrate -= extra_allocation;
475 if (total_allocation > max_multiplier * it->first) {
476 // There is more than we can fit for this observer, carry over to the
477 // remaining observers.
478 bitrate += total_allocation - max_multiplier * it->first;
479 total_allocation = max_multiplier * it->first;
480 }
481 // Finally, update the allocation for this observer.
482 allocation->at(it->second->observer) = total_allocation;
483 it = list_max_bitrates.erase(it);
484 }
485}
486
487bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
488 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700489 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200490 if (bitrate < sum_min_bitrates)
491 return false;
492
perkj26091b12016-09-01 01:17:40 -0700493 uint32_t extra_bitrate_per_observer =
494 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200495 static_cast<uint32_t>(bitrate_observer_configs_.size());
496 for (const auto& observer_config : bitrate_observer_configs_) {
497 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100498 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200499 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800500 }
mflodman101f2502016-06-09 17:21:19 +0200501 }
502 return true;
503}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800504
505void BitrateAllocator::DistributeBitrateRelatively(
506 uint32_t remaining_bitrate,
507 const ObserverAllocation& observers_capacities,
508 ObserverAllocation* allocation) {
509 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
510 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
511 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
512
513 struct PriorityRateObserverConfig {
514 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
515 uint32_t capacity_bps,
516 double bitrate_priority)
517 : allocation_key(allocation_key),
518 capacity_bps(capacity_bps),
519 bitrate_priority(bitrate_priority) {}
520
521 BitrateAllocatorObserver* allocation_key;
522 // The amount of bitrate bps that can be allocated to this observer.
523 uint32_t capacity_bps;
524 double bitrate_priority;
525
526 // We want to sort by which observers will be allocated their full capacity
527 // first. By dividing each observer's capacity by its bitrate priority we
528 // are "normalizing" the capacity of an observer by the rate it will be
529 // filled. This is because the amount allocated is based upon bitrate
530 // priority. We allocate twice as much bitrate to an observer with twice the
531 // bitrate priority of another.
532 bool operator<(const PriorityRateObserverConfig& other) const {
533 return capacity_bps / bitrate_priority <
534 other.capacity_bps / other.bitrate_priority;
535 }
536 };
537
538 double bitrate_priority_sum = 0;
539 std::vector<PriorityRateObserverConfig> priority_rate_observers;
540 for (const auto& observer_config : bitrate_observer_configs_) {
541 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
542 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
543 observer_config.bitrate_priority);
544 bitrate_priority_sum += observer_config.bitrate_priority;
545 }
546
547 // Iterate in the order observers can be allocated their full capacity.
548 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
549 size_t i;
550 for (i = 0; i < priority_rate_observers.size(); ++i) {
551 const auto& priority_rate_observer = priority_rate_observers[i];
552 // We allocate the full capacity to an observer only if its relative
553 // portion from the remaining bitrate is sufficient to allocate its full
554 // capacity. This means we aren't greedily allocating the full capacity, but
555 // that it is only done when there is also enough bitrate to allocate the
556 // proportional amounts to all other observers.
557 double observer_share =
558 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
559 double allocation_bps = observer_share * remaining_bitrate;
560 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
561 if (!enough_bitrate)
562 break;
563 allocation->at(priority_rate_observer.allocation_key) +=
564 priority_rate_observer.capacity_bps;
565 remaining_bitrate -= priority_rate_observer.capacity_bps;
566 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
567 }
568
569 // From the remaining bitrate, allocate the proportional amounts to the
570 // observers that aren't allocated their max capacity.
571 for (; i < priority_rate_observers.size(); ++i) {
572 const auto& priority_rate_observer = priority_rate_observers[i];
573 double fraction_allocated =
574 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
575 allocation->at(priority_rate_observer.allocation_key) +=
576 fraction_allocated * remaining_bitrate;
577 }
578}
579
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000580} // namespace webrtc