blob: 004262e262836548aa4d3905f4101d4b631435de [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.
29const int kTransmissionMaxBitrateMultiplier = 2;
30const int kDefaultBitrateBps = 300000;
31
mflodman101f2502016-06-09 17:21:19 +020032// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
33const double kToggleFactor = 0.1;
34const uint32_t kMinToggleBitrateBps = 20000;
35
mflodman48a4beb2016-07-01 13:03:59 +020036const int64_t kBweLogIntervalMs = 5000;
37
38namespace {
39
40double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
kwibergaf476c72016-11-28 15:21:39 -080041 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 13:03:59 +020042 if (protection_bitrate == 0)
43 return 1.0;
44
45 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
46 return media_bitrate / static_cast<double>(allocated_bitrate);
47}
48} // namespace
49
perkj71ee44c2016-06-15 00:47:53 -070050BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
51 : limit_observer_(limit_observer),
Sergey Ulanove2b15012016-11-22 16:08:30 -080052 last_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -070053 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010054 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020055 last_rtt_(0),
56 num_pause_events_(0),
57 clock_(Clock::GetRealTimeClock()),
philipel5ef2bc12017-02-21 07:28:31 -080058 last_bwe_log_time_(0),
59 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020060 total_requested_min_bitrate_(0),
61 bitrate_allocation_strategy_(nullptr) {
perkj26091b12016-09-01 01:17:40 -070062 sequenced_checker_.Detach();
63}
mflodman48a4beb2016-07-01 13:03:59 +020064
65BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070066 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
67 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020068}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000069
perkj71ee44c2016-06-15 00:47:53 -070070void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
71 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080072 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070073 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070074 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020075 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070076 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020077 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010078 last_fraction_loss_ = fraction_loss;
79 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -070080 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -070081
mflodman48a4beb2016-07-01 13:03:59 +020082 // Periodically log the incoming BWE.
83 int64_t now = clock_->TimeInMilliseconds();
84 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010085 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +020086 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -080087 }
mflodman48a4beb2016-07-01 13:03:59 +020088
89 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
90
91 for (auto& config : bitrate_observer_configs_) {
92 uint32_t allocated_bitrate = allocation[config.observer];
93 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -080094 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -070095 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +020096
97 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
98 if (target_bitrate_bps > 0)
99 ++num_pause_events_;
100 // The protection bitrate is an estimate based on the ratio between media
101 // and protection used before this observer was muted.
102 uint32_t predicted_protection_bps =
103 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100104 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
105 << " with configured min bitrate "
106 << config.min_bitrate_bps << " and current estimate of "
107 << target_bitrate_bps << " and protection bitrate "
108 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200109 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
110 if (target_bitrate_bps > 0)
111 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100112 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
113 << ", configured min bitrate " << config.min_bitrate_bps
114 << ", current allocation " << allocated_bitrate
115 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200116 }
117
118 // Only update the media ratio if the observer got an allocation.
119 if (allocated_bitrate > 0)
120 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
121 config.allocated_bitrate_bps = allocated_bitrate;
122 }
philipel5ef2bc12017-02-21 07:28:31 -0800123 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100124}
125
perkj57c21f92016-06-17 07:27:16 -0700126void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
127 uint32_t min_bitrate_bps,
128 uint32_t max_bitrate_bps,
129 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200130 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800131 std::string track_id,
132 double bitrate_priority) {
perkj26091b12016-09-01 01:17:40 -0700133 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800134 RTC_DCHECK_GT(bitrate_priority, 0);
135 RTC_DCHECK(std::isnormal(bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700136 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000137
mflodman101f2502016-06-09 17:21:19 +0200138 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700139 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700140 it->min_bitrate_bps = min_bitrate_bps;
141 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700142 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200143 it->enforce_min_bitrate = enforce_min_bitrate;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800144 it->bitrate_priority = bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000145 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800146 bitrate_observer_configs_.push_back(ObserverConfig(
147 observer, min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps,
148 enforce_min_bitrate, track_id, bitrate_priority));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000149 }
Stefan Holmere5904162015-03-26 11:11:06 +0100150
mflodman101f2502016-06-09 17:21:19 +0200151 ObserverAllocation allocation;
152 if (last_bitrate_bps_ > 0) {
153 // Calculate a new allocation and update all observers.
154 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200155 for (auto& config : bitrate_observer_configs_) {
156 uint32_t allocated_bitrate = allocation[config.observer];
157 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800158 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700159 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200160 config.allocated_bitrate_bps = allocated_bitrate;
161 if (allocated_bitrate > 0)
162 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
163 }
perkjfea93092016-05-14 00:58:48 -0700164 } else {
165 // Currently, an encoder is not allowed to produce frames.
166 // But we still have to return the initial config bitrate + let the
167 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200168 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800169 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700170 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100171 }
perkj71ee44c2016-06-15 00:47:53 -0700172 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000173}
174
perkj71ee44c2016-06-15 00:47:53 -0700175void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700176 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700177 uint32_t total_requested_padding_bitrate = 0;
178 uint32_t total_requested_min_bitrate = 0;
179
perkj26091b12016-09-01 01:17:40 -0700180 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800181 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700182 if (config.enforce_min_bitrate) {
183 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800184 } else if (config.allocated_bitrate_bps == 0) {
185 stream_padding =
186 std::max(MinBitrateWithHysteresis(config), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700187 }
philipel5ef2bc12017-02-21 07:28:31 -0800188 total_requested_padding_bitrate += stream_padding;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000189 }
perkj71ee44c2016-06-15 00:47:53 -0700190
philipel5ef2bc12017-02-21 07:28:31 -0800191 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
192 total_requested_min_bitrate == total_requested_min_bitrate_) {
193 return;
194 }
195
196 total_requested_min_bitrate_ = total_requested_min_bitrate;
197 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
198
Mirko Bonadei675513b2017-11-09 11:09:25 +0100199 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
200 << total_requested_min_bitrate
201 << "bps, total_requested_padding_bitrate: "
202 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700203 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
204 total_requested_padding_bitrate);
205}
206
207void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700208 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200209
perkj26091b12016-09-01 01:17:40 -0700210 auto it = FindObserverConfig(observer);
211 if (it != bitrate_observer_configs_.end()) {
212 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700213 }
perkj26091b12016-09-01 01:17:40 -0700214
perkj71ee44c2016-06-15 00:47:53 -0700215 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000216}
217
perkj57c21f92016-06-17 07:27:16 -0700218int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700219 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200220 const auto& it = FindObserverConfig(observer);
221 if (it == bitrate_observer_configs_.end()) {
222 // This observer hasn't been added yet, just give it its fair share.
223 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700224 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200225 } else if (it->allocated_bitrate_bps == -1) {
226 // This observer hasn't received an allocation yet, so do the same.
227 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700228 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200229 } else {
230 // This observer already has an allocation.
231 return it->allocated_bitrate_bps;
232 }
perkj57c21f92016-06-17 07:27:16 -0700233}
234
Alex Narest78609d52017-10-20 10:37:47 +0200235void BitrateAllocator::SetBitrateAllocationStrategy(
236 std::unique_ptr<rtc::BitrateAllocationStrategy>
237 bitrate_allocation_strategy) {
238 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
239 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
240}
241
mflodman48a4beb2016-07-01 13:03:59 +0200242BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700243BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
244 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700245 for (auto it = bitrate_observer_configs_.begin();
246 it != bitrate_observer_configs_.end(); ++it) {
247 if (it->observer == observer)
248 return it;
249 }
250 return bitrate_observer_configs_.end();
251}
252
perkjfea93092016-05-14 00:58:48 -0700253BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
254 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700255 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700256 if (bitrate_observer_configs_.empty())
257 return ObserverAllocation();
258
Alex Narest78609d52017-10-20 10:37:47 +0200259 if (bitrate_allocation_strategy_ != nullptr) {
260 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
261 track_configs(bitrate_observer_configs_.size());
262 int i = 0;
263 for (const auto& c : bitrate_observer_configs_) {
264 track_configs[i++] = &c;
265 }
266 std::vector<uint32_t> track_allocations =
267 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
268 // The strategy should return allocation for all tracks.
269 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
270 ObserverAllocation allocation;
271 auto track_allocations_it = track_allocations.begin();
272 for (const auto& observer_config : bitrate_observer_configs_) {
273 allocation[observer_config.observer] = *track_allocations_it++;
274 }
275 return allocation;
276 }
277
perkjfea93092016-05-14 00:58:48 -0700278 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700279 return ZeroRateAllocation();
280
281 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200282 uint32_t sum_max_bitrates = 0;
283 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700284 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200285 sum_max_bitrates += observer_config.max_bitrate_bps;
286 }
287
288 // Not enough for all observers to get an allocation, allocate according to:
289 // enforced min bitrate -> allocated bitrate previous round -> restart paused
290 // streams.
291 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700292 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700293
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800294 // All observers will get their min bitrate plus a share of the rest. This
295 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200296 if (bitrate <= sum_max_bitrates)
297 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700298
mflodman101f2502016-06-09 17:21:19 +0200299 // All observers will get up to kTransmissionMaxBitrateMultiplier x max.
300 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000301}
302
mflodman2ebe5b12016-05-13 01:43:51 -0700303BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700304 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700305 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700306 for (const auto& observer_config : bitrate_observer_configs_)
307 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700308 return allocation;
309}
310
mflodman2ebe5b12016-05-13 01:43:51 -0700311BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100312 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700313 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700314 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200315 // Start by allocating bitrate to observers enforcing a min bitrate, hence
316 // remaining_bitrate might turn negative.
317 int64_t remaining_bitrate = bitrate;
318 for (const auto& observer_config : bitrate_observer_configs_) {
319 int32_t allocated_bitrate = 0;
320 if (observer_config.enforce_min_bitrate)
321 allocated_bitrate = observer_config.min_bitrate_bps;
322
323 allocation[observer_config.observer] = allocated_bitrate;
324 remaining_bitrate -= allocated_bitrate;
325 }
326
327 // Allocate bitrate to all previously active streams.
328 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700329 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200330 if (observer_config.enforce_min_bitrate ||
331 LastAllocatedBitrate(observer_config) == 0)
332 continue;
333
mflodman48a4beb2016-07-01 13:03:59 +0200334 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
335 if (remaining_bitrate >= required_bitrate) {
336 allocation[observer_config.observer] = required_bitrate;
337 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200338 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000339 }
340 }
mflodman101f2502016-06-09 17:21:19 +0200341
342 // Allocate bitrate to previously paused streams.
343 if (remaining_bitrate > 0) {
344 for (const auto& observer_config : bitrate_observer_configs_) {
345 if (LastAllocatedBitrate(observer_config) != 0)
346 continue;
347
348 // Add a hysteresis to avoid toggling.
349 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
350 if (remaining_bitrate >= required_bitrate) {
351 allocation[observer_config.observer] = required_bitrate;
352 remaining_bitrate -= required_bitrate;
353 }
354 }
355 }
356
357 // Split a possible remainder evenly on all streams with an allocation.
358 if (remaining_bitrate > 0)
359 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
360
361 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100362 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000363}
mflodman101f2502016-06-09 17:21:19 +0200364
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800365// Allocates the bitrate based on the bitrate priority of each observer. This
366// bitrate priority defines the priority for bitrate to be allocated to that
367// observer in relation to other observers. For example with two observers, if
368// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
369// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
370// allocated twice the bitrate as observer 1 above the each observer's
371// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200372BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
373 uint32_t bitrate,
374 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700375 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200376 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800377 ObserverAllocation observers_capacities;
378 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200379 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800380 observers_capacities[observer_config.observer] =
381 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
382 }
mflodman101f2502016-06-09 17:21:19 +0200383
384 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800385 // From the remaining bitrate, allocate a proportional amount to each observer
386 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200387 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800388 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200389
390 return allocation;
391}
392
393BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700394 uint32_t bitrate,
395 uint32_t sum_max_bitrates) {
396 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200397 ObserverAllocation allocation;
398
399 for (const auto& observer_config : bitrate_observer_configs_) {
400 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
401 bitrate -= observer_config.max_bitrate_bps;
402 }
403 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier,
404 &allocation);
405 return allocation;
406}
407
408uint32_t BitrateAllocator::LastAllocatedBitrate(
409 const ObserverConfig& observer_config) {
mflodman101f2502016-06-09 17:21:19 +0200410 // Return the configured minimum bitrate for newly added observers, to avoid
411 // requiring an extra high bitrate for the observer to get an allocated
412 // bitrate.
perkj26091b12016-09-01 01:17:40 -0700413 return observer_config.allocated_bitrate_bps == -1
414 ? observer_config.min_bitrate_bps
415 : observer_config.allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200416}
417
418uint32_t BitrateAllocator::MinBitrateWithHysteresis(
419 const ObserverConfig& observer_config) {
420 uint32_t min_bitrate = observer_config.min_bitrate_bps;
421 if (LastAllocatedBitrate(observer_config) == 0) {
422 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
423 kMinToggleBitrateBps);
424 }
mflodman48a4beb2016-07-01 13:03:59 +0200425 // Account for protection bitrate used by this observer in the previous
426 // allocation.
427 // Note: the ratio will only be updated when the stream is active, meaning a
428 // paused stream won't get any ratio updates. This might lead to waiting a bit
429 // longer than necessary if the network condition improves, but this is to
430 // avoid too much toggling.
431 if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0)
432 min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio);
433
mflodman101f2502016-06-09 17:21:19 +0200434 return min_bitrate;
435}
436
437void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
438 bool include_zero_allocations,
439 int max_multiplier,
440 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700441 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200442 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
443
444 ObserverSortingMap list_max_bitrates;
445 for (const auto& observer_config : bitrate_observer_configs_) {
446 if (include_zero_allocations ||
447 allocation->at(observer_config.observer) != 0) {
448 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
449 observer_config.max_bitrate_bps, &observer_config));
450 }
451 }
452 auto it = list_max_bitrates.begin();
453 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800454 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200455 uint32_t extra_allocation =
456 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
457 uint32_t total_allocation =
458 extra_allocation + allocation->at(it->second->observer);
459 bitrate -= extra_allocation;
460 if (total_allocation > max_multiplier * it->first) {
461 // There is more than we can fit for this observer, carry over to the
462 // remaining observers.
463 bitrate += total_allocation - max_multiplier * it->first;
464 total_allocation = max_multiplier * it->first;
465 }
466 // Finally, update the allocation for this observer.
467 allocation->at(it->second->observer) = total_allocation;
468 it = list_max_bitrates.erase(it);
469 }
470}
471
472bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
473 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700474 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200475 if (bitrate < sum_min_bitrates)
476 return false;
477
perkj26091b12016-09-01 01:17:40 -0700478 uint32_t extra_bitrate_per_observer =
479 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200480 static_cast<uint32_t>(bitrate_observer_configs_.size());
481 for (const auto& observer_config : bitrate_observer_configs_) {
482 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
philipel5ef2bc12017-02-21 07:28:31 -0800483 MinBitrateWithHysteresis(observer_config)) {
mflodman101f2502016-06-09 17:21:19 +0200484 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800485 }
mflodman101f2502016-06-09 17:21:19 +0200486 }
487 return true;
488}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800489
490void BitrateAllocator::DistributeBitrateRelatively(
491 uint32_t remaining_bitrate,
492 const ObserverAllocation& observers_capacities,
493 ObserverAllocation* allocation) {
494 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
495 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
496 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
497
498 struct PriorityRateObserverConfig {
499 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
500 uint32_t capacity_bps,
501 double bitrate_priority)
502 : allocation_key(allocation_key),
503 capacity_bps(capacity_bps),
504 bitrate_priority(bitrate_priority) {}
505
506 BitrateAllocatorObserver* allocation_key;
507 // The amount of bitrate bps that can be allocated to this observer.
508 uint32_t capacity_bps;
509 double bitrate_priority;
510
511 // We want to sort by which observers will be allocated their full capacity
512 // first. By dividing each observer's capacity by its bitrate priority we
513 // are "normalizing" the capacity of an observer by the rate it will be
514 // filled. This is because the amount allocated is based upon bitrate
515 // priority. We allocate twice as much bitrate to an observer with twice the
516 // bitrate priority of another.
517 bool operator<(const PriorityRateObserverConfig& other) const {
518 return capacity_bps / bitrate_priority <
519 other.capacity_bps / other.bitrate_priority;
520 }
521 };
522
523 double bitrate_priority_sum = 0;
524 std::vector<PriorityRateObserverConfig> priority_rate_observers;
525 for (const auto& observer_config : bitrate_observer_configs_) {
526 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
527 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
528 observer_config.bitrate_priority);
529 bitrate_priority_sum += observer_config.bitrate_priority;
530 }
531
532 // Iterate in the order observers can be allocated their full capacity.
533 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
534 size_t i;
535 for (i = 0; i < priority_rate_observers.size(); ++i) {
536 const auto& priority_rate_observer = priority_rate_observers[i];
537 // We allocate the full capacity to an observer only if its relative
538 // portion from the remaining bitrate is sufficient to allocate its full
539 // capacity. This means we aren't greedily allocating the full capacity, but
540 // that it is only done when there is also enough bitrate to allocate the
541 // proportional amounts to all other observers.
542 double observer_share =
543 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
544 double allocation_bps = observer_share * remaining_bitrate;
545 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
546 if (!enough_bitrate)
547 break;
548 allocation->at(priority_rate_observer.allocation_key) +=
549 priority_rate_observer.capacity_bps;
550 remaining_bitrate -= priority_rate_observer.capacity_bps;
551 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
552 }
553
554 // From the remaining bitrate, allocate the proportional amounts to the
555 // observers that aren't allocated their max capacity.
556 for (; i < priority_rate_observers.size(); ++i) {
557 const auto& priority_rate_observer = priority_rate_observers[i];
558 double fraction_allocated =
559 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
560 allocation->at(priority_rate_observer.allocation_key) +=
561 fraction_allocated * remaining_bitrate;
562 }
563}
564
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000565} // namespace webrtc