blob: fc202ce22f5895f56e844eedf29bd47a74af31c3 [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"
23#include "system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000024
25namespace webrtc {
26
Stefan Holmere5904162015-03-26 11:11:06 +010027// Allow packets to be transmitted in up to 2 times max video bitrate if the
28// bandwidth estimate allows it.
Alex Nareste0b2ff52017-11-16 20:03:46 +010029// TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in
30// video send stream. Similar logic is implemented in
31// AudioPriorityBitrateAllocationStrategy.
Stefan Holmere5904162015-03-26 11:11:06 +010032const int kTransmissionMaxBitrateMultiplier = 2;
33const int kDefaultBitrateBps = 300000;
34
mflodman101f2502016-06-09 17:21:19 +020035// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
36const double kToggleFactor = 0.1;
37const uint32_t kMinToggleBitrateBps = 20000;
38
mflodman48a4beb2016-07-01 13:03:59 +020039const int64_t kBweLogIntervalMs = 5000;
40
41namespace {
42
43double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
kwibergaf476c72016-11-28 15:21:39 -080044 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 13:03:59 +020045 if (protection_bitrate == 0)
46 return 1.0;
47
48 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
49 return media_bitrate / static_cast<double>(allocated_bitrate);
50}
51} // namespace
52
perkj71ee44c2016-06-15 00:47:53 -070053BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
54 : limit_observer_(limit_observer),
Sergey Ulanove2b15012016-11-22 16:08:30 -080055 last_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -070056 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010057 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020058 last_rtt_(0),
59 num_pause_events_(0),
60 clock_(Clock::GetRealTimeClock()),
philipel5ef2bc12017-02-21 07:28:31 -080061 last_bwe_log_time_(0),
62 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020063 total_requested_min_bitrate_(0),
64 bitrate_allocation_strategy_(nullptr) {
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
perkj71ee44c2016-06-15 00:47:53 -070073void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
74 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080075 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070076 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070077 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020078 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070079 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020080 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010081 last_fraction_loss_ = fraction_loss;
82 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -070083 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -070084
mflodman48a4beb2016-07-01 13:03:59 +020085 // Periodically log the incoming BWE.
86 int64_t now = clock_->TimeInMilliseconds();
87 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010088 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +020089 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -080090 }
mflodman48a4beb2016-07-01 13:03:59 +020091
92 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
93
94 for (auto& config : bitrate_observer_configs_) {
95 uint32_t allocated_bitrate = allocation[config.observer];
96 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -080097 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -070098 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +020099
100 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
101 if (target_bitrate_bps > 0)
102 ++num_pause_events_;
103 // The protection bitrate is an estimate based on the ratio between media
104 // and protection used before this observer was muted.
105 uint32_t predicted_protection_bps =
106 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100107 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
108 << " with configured min bitrate "
109 << config.min_bitrate_bps << " and current estimate of "
110 << target_bitrate_bps << " and protection bitrate "
111 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200112 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
113 if (target_bitrate_bps > 0)
114 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
116 << ", configured min bitrate " << config.min_bitrate_bps
117 << ", current allocation " << allocated_bitrate
118 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200119 }
120
121 // Only update the media ratio if the observer got an allocation.
122 if (allocated_bitrate > 0)
123 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
124 config.allocated_bitrate_bps = allocated_bitrate;
125 }
philipel5ef2bc12017-02-21 07:28:31 -0800126 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100127}
128
perkj57c21f92016-06-17 07:27:16 -0700129void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
130 uint32_t min_bitrate_bps,
131 uint32_t max_bitrate_bps,
132 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200133 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800134 std::string track_id,
135 double bitrate_priority) {
perkj26091b12016-09-01 01:17:40 -0700136 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800137 RTC_DCHECK_GT(bitrate_priority, 0);
138 RTC_DCHECK(std::isnormal(bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700139 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000140
mflodman101f2502016-06-09 17:21:19 +0200141 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700142 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700143 it->min_bitrate_bps = min_bitrate_bps;
144 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700145 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200146 it->enforce_min_bitrate = enforce_min_bitrate;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800147 it->bitrate_priority = bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000148 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800149 bitrate_observer_configs_.push_back(ObserverConfig(
150 observer, min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps,
151 enforce_min_bitrate, track_id, bitrate_priority));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000152 }
Stefan Holmere5904162015-03-26 11:11:06 +0100153
mflodman101f2502016-06-09 17:21:19 +0200154 ObserverAllocation allocation;
155 if (last_bitrate_bps_ > 0) {
156 // Calculate a new allocation and update all observers.
157 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200158 for (auto& config : bitrate_observer_configs_) {
159 uint32_t allocated_bitrate = allocation[config.observer];
160 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800161 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700162 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200163 config.allocated_bitrate_bps = allocated_bitrate;
164 if (allocated_bitrate > 0)
165 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
166 }
perkjfea93092016-05-14 00:58:48 -0700167 } else {
168 // Currently, an encoder is not allowed to produce frames.
169 // But we still have to return the initial config bitrate + let the
170 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200171 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800172 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700173 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100174 }
perkj71ee44c2016-06-15 00:47:53 -0700175 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000176}
177
perkj71ee44c2016-06-15 00:47:53 -0700178void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700179 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700180 uint32_t total_requested_padding_bitrate = 0;
181 uint32_t total_requested_min_bitrate = 0;
182
perkj26091b12016-09-01 01:17:40 -0700183 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800184 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700185 if (config.enforce_min_bitrate) {
186 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800187 } else if (config.allocated_bitrate_bps == 0) {
188 stream_padding =
189 std::max(MinBitrateWithHysteresis(config), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700190 }
philipel5ef2bc12017-02-21 07:28:31 -0800191 total_requested_padding_bitrate += stream_padding;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000192 }
perkj71ee44c2016-06-15 00:47:53 -0700193
philipel5ef2bc12017-02-21 07:28:31 -0800194 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
195 total_requested_min_bitrate == total_requested_min_bitrate_) {
196 return;
197 }
198
199 total_requested_min_bitrate_ = total_requested_min_bitrate;
200 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
201
Mirko Bonadei675513b2017-11-09 11:09:25 +0100202 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
203 << total_requested_min_bitrate
204 << "bps, total_requested_padding_bitrate: "
205 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700206 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
207 total_requested_padding_bitrate);
208}
209
210void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700211 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200212
perkj26091b12016-09-01 01:17:40 -0700213 auto it = FindObserverConfig(observer);
214 if (it != bitrate_observer_configs_.end()) {
215 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700216 }
perkj26091b12016-09-01 01:17:40 -0700217
perkj71ee44c2016-06-15 00:47:53 -0700218 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000219}
220
perkj57c21f92016-06-17 07:27:16 -0700221int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700222 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200223 const auto& it = FindObserverConfig(observer);
224 if (it == bitrate_observer_configs_.end()) {
225 // This observer hasn't been added yet, just give it its fair share.
226 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700227 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200228 } else if (it->allocated_bitrate_bps == -1) {
229 // This observer hasn't received an allocation yet, so do the same.
230 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700231 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200232 } else {
233 // This observer already has an allocation.
234 return it->allocated_bitrate_bps;
235 }
perkj57c21f92016-06-17 07:27:16 -0700236}
237
Alex Narest78609d52017-10-20 10:37:47 +0200238void BitrateAllocator::SetBitrateAllocationStrategy(
239 std::unique_ptr<rtc::BitrateAllocationStrategy>
240 bitrate_allocation_strategy) {
241 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
242 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
243}
244
mflodman48a4beb2016-07-01 13:03:59 +0200245BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700246BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
247 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700248 for (auto it = bitrate_observer_configs_.begin();
249 it != bitrate_observer_configs_.end(); ++it) {
250 if (it->observer == observer)
251 return it;
252 }
253 return bitrate_observer_configs_.end();
254}
255
perkjfea93092016-05-14 00:58:48 -0700256BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
257 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700258 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700259 if (bitrate_observer_configs_.empty())
260 return ObserverAllocation();
261
Alex Narest78609d52017-10-20 10:37:47 +0200262 if (bitrate_allocation_strategy_ != nullptr) {
263 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
264 track_configs(bitrate_observer_configs_.size());
265 int i = 0;
266 for (const auto& c : bitrate_observer_configs_) {
267 track_configs[i++] = &c;
268 }
269 std::vector<uint32_t> track_allocations =
270 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
271 // The strategy should return allocation for all tracks.
272 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
273 ObserverAllocation allocation;
274 auto track_allocations_it = track_allocations.begin();
275 for (const auto& observer_config : bitrate_observer_configs_) {
276 allocation[observer_config.observer] = *track_allocations_it++;
277 }
278 return allocation;
279 }
280
perkjfea93092016-05-14 00:58:48 -0700281 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700282 return ZeroRateAllocation();
283
284 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200285 uint32_t sum_max_bitrates = 0;
286 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700287 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200288 sum_max_bitrates += observer_config.max_bitrate_bps;
289 }
290
291 // Not enough for all observers to get an allocation, allocate according to:
292 // enforced min bitrate -> allocated bitrate previous round -> restart paused
293 // streams.
294 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700295 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700296
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800297 // All observers will get their min bitrate plus a share of the rest. This
298 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200299 if (bitrate <= sum_max_bitrates)
300 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700301
mflodman101f2502016-06-09 17:21:19 +0200302 // All observers will get up to kTransmissionMaxBitrateMultiplier x max.
303 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000304}
305
mflodman2ebe5b12016-05-13 01:43:51 -0700306BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700307 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700308 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700309 for (const auto& observer_config : bitrate_observer_configs_)
310 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700311 return allocation;
312}
313
mflodman2ebe5b12016-05-13 01:43:51 -0700314BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100315 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700316 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700317 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200318 // Start by allocating bitrate to observers enforcing a min bitrate, hence
319 // remaining_bitrate might turn negative.
320 int64_t remaining_bitrate = bitrate;
321 for (const auto& observer_config : bitrate_observer_configs_) {
322 int32_t allocated_bitrate = 0;
323 if (observer_config.enforce_min_bitrate)
324 allocated_bitrate = observer_config.min_bitrate_bps;
325
326 allocation[observer_config.observer] = allocated_bitrate;
327 remaining_bitrate -= allocated_bitrate;
328 }
329
330 // Allocate bitrate to all previously active streams.
331 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700332 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200333 if (observer_config.enforce_min_bitrate ||
334 LastAllocatedBitrate(observer_config) == 0)
335 continue;
336
mflodman48a4beb2016-07-01 13:03:59 +0200337 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
338 if (remaining_bitrate >= required_bitrate) {
339 allocation[observer_config.observer] = required_bitrate;
340 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200341 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000342 }
343 }
mflodman101f2502016-06-09 17:21:19 +0200344
345 // Allocate bitrate to previously paused streams.
346 if (remaining_bitrate > 0) {
347 for (const auto& observer_config : bitrate_observer_configs_) {
348 if (LastAllocatedBitrate(observer_config) != 0)
349 continue;
350
351 // Add a hysteresis to avoid toggling.
352 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
353 if (remaining_bitrate >= required_bitrate) {
354 allocation[observer_config.observer] = required_bitrate;
355 remaining_bitrate -= required_bitrate;
356 }
357 }
358 }
359
360 // Split a possible remainder evenly on all streams with an allocation.
361 if (remaining_bitrate > 0)
362 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
363
364 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100365 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000366}
mflodman101f2502016-06-09 17:21:19 +0200367
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800368// Allocates the bitrate based on the bitrate priority of each observer. This
369// bitrate priority defines the priority for bitrate to be allocated to that
370// observer in relation to other observers. For example with two observers, if
371// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
372// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
373// allocated twice the bitrate as observer 1 above the each observer's
374// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200375BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
376 uint32_t bitrate,
377 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700378 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200379 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800380 ObserverAllocation observers_capacities;
381 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200382 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800383 observers_capacities[observer_config.observer] =
384 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
385 }
mflodman101f2502016-06-09 17:21:19 +0200386
387 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800388 // From the remaining bitrate, allocate a proportional amount to each observer
389 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200390 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800391 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200392
393 return allocation;
394}
395
396BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700397 uint32_t bitrate,
398 uint32_t sum_max_bitrates) {
399 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200400 ObserverAllocation allocation;
401
402 for (const auto& observer_config : bitrate_observer_configs_) {
403 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
404 bitrate -= observer_config.max_bitrate_bps;
405 }
406 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier,
407 &allocation);
408 return allocation;
409}
410
411uint32_t BitrateAllocator::LastAllocatedBitrate(
412 const ObserverConfig& observer_config) {
mflodman101f2502016-06-09 17:21:19 +0200413 // Return the configured minimum bitrate for newly added observers, to avoid
414 // requiring an extra high bitrate for the observer to get an allocated
415 // bitrate.
perkj26091b12016-09-01 01:17:40 -0700416 return observer_config.allocated_bitrate_bps == -1
417 ? observer_config.min_bitrate_bps
418 : observer_config.allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200419}
420
421uint32_t BitrateAllocator::MinBitrateWithHysteresis(
422 const ObserverConfig& observer_config) {
423 uint32_t min_bitrate = observer_config.min_bitrate_bps;
424 if (LastAllocatedBitrate(observer_config) == 0) {
425 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
426 kMinToggleBitrateBps);
427 }
mflodman48a4beb2016-07-01 13:03:59 +0200428 // Account for protection bitrate used by this observer in the previous
429 // allocation.
430 // Note: the ratio will only be updated when the stream is active, meaning a
431 // paused stream won't get any ratio updates. This might lead to waiting a bit
432 // longer than necessary if the network condition improves, but this is to
433 // avoid too much toggling.
434 if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0)
435 min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio);
436
mflodman101f2502016-06-09 17:21:19 +0200437 return min_bitrate;
438}
439
440void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
441 bool include_zero_allocations,
442 int max_multiplier,
443 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700444 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200445 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
446
447 ObserverSortingMap list_max_bitrates;
448 for (const auto& observer_config : bitrate_observer_configs_) {
449 if (include_zero_allocations ||
450 allocation->at(observer_config.observer) != 0) {
451 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
452 observer_config.max_bitrate_bps, &observer_config));
453 }
454 }
455 auto it = list_max_bitrates.begin();
456 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800457 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200458 uint32_t extra_allocation =
459 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
460 uint32_t total_allocation =
461 extra_allocation + allocation->at(it->second->observer);
462 bitrate -= extra_allocation;
463 if (total_allocation > max_multiplier * it->first) {
464 // There is more than we can fit for this observer, carry over to the
465 // remaining observers.
466 bitrate += total_allocation - max_multiplier * it->first;
467 total_allocation = max_multiplier * it->first;
468 }
469 // Finally, update the allocation for this observer.
470 allocation->at(it->second->observer) = total_allocation;
471 it = list_max_bitrates.erase(it);
472 }
473}
474
475bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
476 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700477 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200478 if (bitrate < sum_min_bitrates)
479 return false;
480
perkj26091b12016-09-01 01:17:40 -0700481 uint32_t extra_bitrate_per_observer =
482 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200483 static_cast<uint32_t>(bitrate_observer_configs_.size());
484 for (const auto& observer_config : bitrate_observer_configs_) {
485 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
philipel5ef2bc12017-02-21 07:28:31 -0800486 MinBitrateWithHysteresis(observer_config)) {
mflodman101f2502016-06-09 17:21:19 +0200487 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800488 }
mflodman101f2502016-06-09 17:21:19 +0200489 }
490 return true;
491}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800492
493void BitrateAllocator::DistributeBitrateRelatively(
494 uint32_t remaining_bitrate,
495 const ObserverAllocation& observers_capacities,
496 ObserverAllocation* allocation) {
497 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
498 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
499 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
500
501 struct PriorityRateObserverConfig {
502 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
503 uint32_t capacity_bps,
504 double bitrate_priority)
505 : allocation_key(allocation_key),
506 capacity_bps(capacity_bps),
507 bitrate_priority(bitrate_priority) {}
508
509 BitrateAllocatorObserver* allocation_key;
510 // The amount of bitrate bps that can be allocated to this observer.
511 uint32_t capacity_bps;
512 double bitrate_priority;
513
514 // We want to sort by which observers will be allocated their full capacity
515 // first. By dividing each observer's capacity by its bitrate priority we
516 // are "normalizing" the capacity of an observer by the rate it will be
517 // filled. This is because the amount allocated is based upon bitrate
518 // priority. We allocate twice as much bitrate to an observer with twice the
519 // bitrate priority of another.
520 bool operator<(const PriorityRateObserverConfig& other) const {
521 return capacity_bps / bitrate_priority <
522 other.capacity_bps / other.bitrate_priority;
523 }
524 };
525
526 double bitrate_priority_sum = 0;
527 std::vector<PriorityRateObserverConfig> priority_rate_observers;
528 for (const auto& observer_config : bitrate_observer_configs_) {
529 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
530 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
531 observer_config.bitrate_priority);
532 bitrate_priority_sum += observer_config.bitrate_priority;
533 }
534
535 // Iterate in the order observers can be allocated their full capacity.
536 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
537 size_t i;
538 for (i = 0; i < priority_rate_observers.size(); ++i) {
539 const auto& priority_rate_observer = priority_rate_observers[i];
540 // We allocate the full capacity to an observer only if its relative
541 // portion from the remaining bitrate is sufficient to allocate its full
542 // capacity. This means we aren't greedily allocating the full capacity, but
543 // that it is only done when there is also enough bitrate to allocate the
544 // proportional amounts to all other observers.
545 double observer_share =
546 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
547 double allocation_bps = observer_share * remaining_bitrate;
548 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
549 if (!enough_bitrate)
550 break;
551 allocation->at(priority_rate_observer.allocation_key) +=
552 priority_rate_observer.capacity_bps;
553 remaining_bitrate -= priority_rate_observer.capacity_bps;
554 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
555 }
556
557 // From the remaining bitrate, allocate the proportional amounts to the
558 // observers that aren't allocated their max capacity.
559 for (; i < priority_rate_observers.size(); ++i) {
560 const auto& priority_rate_observer = priority_rate_observers[i];
561 double fraction_allocated =
562 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
563 allocation->at(priority_rate_observer.allocation_key) +=
564 fraction_allocated * remaining_bitrate;
565 }
566}
567
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000568} // namespace webrtc