blob: e186380ee66ed7f33b8269d44e29d054f211f90a [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>
Alex Narest54d1da12017-10-17 19:49:15 +020015#include <memory>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000016#include <utility>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/bitrate_controller/include/bitrate_controller.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
21#include "system_wrappers/include/clock.h"
22#include "system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000023
24namespace webrtc {
25
Stefan Holmere5904162015-03-26 11:11:06 +010026// Allow packets to be transmitted in up to 2 times max video bitrate if the
27// bandwidth estimate allows it.
28const int kTransmissionMaxBitrateMultiplier = 2;
29const int kDefaultBitrateBps = 300000;
30
mflodman101f2502016-06-09 17:21:19 +020031// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
32const double kToggleFactor = 0.1;
33const uint32_t kMinToggleBitrateBps = 20000;
34
mflodman48a4beb2016-07-01 13:03:59 +020035const int64_t kBweLogIntervalMs = 5000;
36
37namespace {
38
39double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
kwibergaf476c72016-11-28 15:21:39 -080040 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 13:03:59 +020041 if (protection_bitrate == 0)
42 return 1.0;
43
44 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
45 return media_bitrate / static_cast<double>(allocated_bitrate);
46}
47} // namespace
48
perkj71ee44c2016-06-15 00:47:53 -070049BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
50 : limit_observer_(limit_observer),
51 bitrate_observer_configs_(),
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 Narest54d1da12017-10-17 19:49:15 +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) {
85 LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
86 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;
104 LOG(LS_INFO) << "Pausing observer " << config.observer
105 << " with configured min bitrate " << config.min_bitrate_bps
106 << " and current estimate of " << target_bitrate_bps
107 << " and protection bitrate " << predicted_protection_bps;
108 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
109 if (target_bitrate_bps > 0)
110 ++num_pause_events_;
111 LOG(LS_INFO) << "Resuming observer " << config.observer
112 << ", configured min bitrate " << config.min_bitrate_bps
113 << ", current allocation " << allocated_bitrate
114 << " and protection bitrate " << protection_bitrate;
115 }
116
117 // Only update the media ratio if the observer got an allocation.
118 if (allocated_bitrate > 0)
119 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
120 config.allocated_bitrate_bps = allocated_bitrate;
121 }
philipel5ef2bc12017-02-21 07:28:31 -0800122 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100123}
124
perkj57c21f92016-06-17 07:27:16 -0700125void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
126 uint32_t min_bitrate_bps,
127 uint32_t max_bitrate_bps,
128 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200129 bool enforce_min_bitrate,
130 std::string track_id) {
perkj26091b12016-09-01 01:17:40 -0700131 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700132 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000133
mflodman101f2502016-06-09 17:21:19 +0200134 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700135 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700136 it->min_bitrate_bps = min_bitrate_bps;
137 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700138 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200139 it->enforce_min_bitrate = enforce_min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000140 } else {
perkj71ee44c2016-06-15 00:47:53 -0700141 bitrate_observer_configs_.push_back(
142 ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200143 pad_up_bitrate_bps, enforce_min_bitrate, track_id));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000144 }
Stefan Holmere5904162015-03-26 11:11:06 +0100145
mflodman101f2502016-06-09 17:21:19 +0200146 ObserverAllocation allocation;
147 if (last_bitrate_bps_ > 0) {
148 // Calculate a new allocation and update all observers.
149 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200150 for (auto& config : bitrate_observer_configs_) {
151 uint32_t allocated_bitrate = allocation[config.observer];
152 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800153 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700154 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200155 config.allocated_bitrate_bps = allocated_bitrate;
156 if (allocated_bitrate > 0)
157 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
158 }
perkjfea93092016-05-14 00:58:48 -0700159 } else {
160 // Currently, an encoder is not allowed to produce frames.
161 // But we still have to return the initial config bitrate + let the
162 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200163 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800164 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700165 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100166 }
perkj71ee44c2016-06-15 00:47:53 -0700167 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000168}
169
perkj71ee44c2016-06-15 00:47:53 -0700170void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700171 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700172 uint32_t total_requested_padding_bitrate = 0;
173 uint32_t total_requested_min_bitrate = 0;
174
perkj26091b12016-09-01 01:17:40 -0700175 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800176 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700177 if (config.enforce_min_bitrate) {
178 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800179 } else if (config.allocated_bitrate_bps == 0) {
180 stream_padding =
181 std::max(MinBitrateWithHysteresis(config), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700182 }
philipel5ef2bc12017-02-21 07:28:31 -0800183 total_requested_padding_bitrate += stream_padding;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000184 }
perkj71ee44c2016-06-15 00:47:53 -0700185
philipel5ef2bc12017-02-21 07:28:31 -0800186 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
187 total_requested_min_bitrate == total_requested_min_bitrate_) {
188 return;
189 }
190
191 total_requested_min_bitrate_ = total_requested_min_bitrate;
192 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
193
perkj9b522f82016-07-07 00:36:28 -0700194 LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
195 << total_requested_min_bitrate
196 << "bps, total_requested_padding_bitrate: "
197 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700198 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
199 total_requested_padding_bitrate);
200}
201
202void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700203 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest54d1da12017-10-17 19:49:15 +0200204
perkj26091b12016-09-01 01:17:40 -0700205 auto it = FindObserverConfig(observer);
206 if (it != bitrate_observer_configs_.end()) {
207 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700208 }
perkj26091b12016-09-01 01:17:40 -0700209
perkj71ee44c2016-06-15 00:47:53 -0700210 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000211}
212
perkj57c21f92016-06-17 07:27:16 -0700213int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700214 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200215 const auto& it = FindObserverConfig(observer);
216 if (it == bitrate_observer_configs_.end()) {
217 // This observer hasn't been added yet, just give it its fair share.
218 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700219 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200220 } else if (it->allocated_bitrate_bps == -1) {
221 // This observer hasn't received an allocation yet, so do the same.
222 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700223 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200224 } else {
225 // This observer already has an allocation.
226 return it->allocated_bitrate_bps;
227 }
perkj57c21f92016-06-17 07:27:16 -0700228}
229
Alex Narest54d1da12017-10-17 19:49:15 +0200230void BitrateAllocator::SetBitrateAllocationStrategy(
231 std::unique_ptr<rtc::BitrateAllocationStrategy>
232 bitrate_allocation_strategy) {
233 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
234 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
235}
236
mflodman48a4beb2016-07-01 13:03:59 +0200237BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700238BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
239 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700240 for (auto it = bitrate_observer_configs_.begin();
241 it != bitrate_observer_configs_.end(); ++it) {
242 if (it->observer == observer)
243 return it;
244 }
245 return bitrate_observer_configs_.end();
246}
247
perkjfea93092016-05-14 00:58:48 -0700248BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
249 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700250 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700251 if (bitrate_observer_configs_.empty())
252 return ObserverAllocation();
253
Alex Narest54d1da12017-10-17 19:49:15 +0200254 if (bitrate_allocation_strategy_ != nullptr) {
255 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
256 track_configs(bitrate_observer_configs_.size());
257 int i = 0;
258 for (const auto& c : bitrate_observer_configs_) {
259 track_configs[i++] = &c;
260 }
261 std::vector<uint32_t> track_allocations =
262 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
263 // The strategy should return allocation for all tracks.
264 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
265 ObserverAllocation allocation;
266 auto track_allocations_it = track_allocations.begin();
267 for (const auto& observer_config : bitrate_observer_configs_) {
268 allocation[observer_config.observer] = *track_allocations_it++;
269 }
270 return allocation;
271 }
272
perkjfea93092016-05-14 00:58:48 -0700273 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700274 return ZeroRateAllocation();
275
276 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200277 uint32_t sum_max_bitrates = 0;
278 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700279 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200280 sum_max_bitrates += observer_config.max_bitrate_bps;
281 }
282
283 // Not enough for all observers to get an allocation, allocate according to:
284 // enforced min bitrate -> allocated bitrate previous round -> restart paused
285 // streams.
286 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700287 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700288
mflodman101f2502016-06-09 17:21:19 +0200289 // All observers will get their min bitrate plus an even share of the rest.
290 if (bitrate <= sum_max_bitrates)
291 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700292
mflodman101f2502016-06-09 17:21:19 +0200293 // All observers will get up to kTransmissionMaxBitrateMultiplier x max.
294 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000295}
296
mflodman2ebe5b12016-05-13 01:43:51 -0700297BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700298 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700299 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700300 for (const auto& observer_config : bitrate_observer_configs_)
301 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700302 return allocation;
303}
304
mflodman2ebe5b12016-05-13 01:43:51 -0700305BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100306 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700307 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700308 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200309 // Start by allocating bitrate to observers enforcing a min bitrate, hence
310 // remaining_bitrate might turn negative.
311 int64_t remaining_bitrate = bitrate;
312 for (const auto& observer_config : bitrate_observer_configs_) {
313 int32_t allocated_bitrate = 0;
314 if (observer_config.enforce_min_bitrate)
315 allocated_bitrate = observer_config.min_bitrate_bps;
316
317 allocation[observer_config.observer] = allocated_bitrate;
318 remaining_bitrate -= allocated_bitrate;
319 }
320
321 // Allocate bitrate to all previously active streams.
322 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700323 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200324 if (observer_config.enforce_min_bitrate ||
325 LastAllocatedBitrate(observer_config) == 0)
326 continue;
327
mflodman48a4beb2016-07-01 13:03:59 +0200328 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
329 if (remaining_bitrate >= required_bitrate) {
330 allocation[observer_config.observer] = required_bitrate;
331 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200332 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000333 }
334 }
mflodman101f2502016-06-09 17:21:19 +0200335
336 // Allocate bitrate to previously paused streams.
337 if (remaining_bitrate > 0) {
338 for (const auto& observer_config : bitrate_observer_configs_) {
339 if (LastAllocatedBitrate(observer_config) != 0)
340 continue;
341
342 // Add a hysteresis to avoid toggling.
343 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
344 if (remaining_bitrate >= required_bitrate) {
345 allocation[observer_config.observer] = required_bitrate;
346 remaining_bitrate -= required_bitrate;
347 }
348 }
349 }
350
351 // Split a possible remainder evenly on all streams with an allocation.
352 if (remaining_bitrate > 0)
353 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
354
355 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100356 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000357}
mflodman101f2502016-06-09 17:21:19 +0200358
359BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
360 uint32_t bitrate,
361 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700362 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200363 ObserverAllocation allocation;
364 for (const auto& observer_config : bitrate_observer_configs_)
365 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
366
367 bitrate -= sum_min_bitrates;
368 if (bitrate > 0)
369 DistributeBitrateEvenly(bitrate, true, 1, &allocation);
370
371 return allocation;
372}
373
374BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700375 uint32_t bitrate,
376 uint32_t sum_max_bitrates) {
377 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200378 ObserverAllocation allocation;
379
380 for (const auto& observer_config : bitrate_observer_configs_) {
381 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
382 bitrate -= observer_config.max_bitrate_bps;
383 }
384 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier,
385 &allocation);
386 return allocation;
387}
388
389uint32_t BitrateAllocator::LastAllocatedBitrate(
390 const ObserverConfig& observer_config) {
mflodman101f2502016-06-09 17:21:19 +0200391 // Return the configured minimum bitrate for newly added observers, to avoid
392 // requiring an extra high bitrate for the observer to get an allocated
393 // bitrate.
perkj26091b12016-09-01 01:17:40 -0700394 return observer_config.allocated_bitrate_bps == -1
395 ? observer_config.min_bitrate_bps
396 : observer_config.allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200397}
398
399uint32_t BitrateAllocator::MinBitrateWithHysteresis(
400 const ObserverConfig& observer_config) {
401 uint32_t min_bitrate = observer_config.min_bitrate_bps;
402 if (LastAllocatedBitrate(observer_config) == 0) {
403 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
404 kMinToggleBitrateBps);
405 }
mflodman48a4beb2016-07-01 13:03:59 +0200406 // Account for protection bitrate used by this observer in the previous
407 // allocation.
408 // Note: the ratio will only be updated when the stream is active, meaning a
409 // paused stream won't get any ratio updates. This might lead to waiting a bit
410 // longer than necessary if the network condition improves, but this is to
411 // avoid too much toggling.
412 if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0)
413 min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio);
414
mflodman101f2502016-06-09 17:21:19 +0200415 return min_bitrate;
416}
417
418void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
419 bool include_zero_allocations,
420 int max_multiplier,
421 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700422 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200423 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
424
425 ObserverSortingMap list_max_bitrates;
426 for (const auto& observer_config : bitrate_observer_configs_) {
427 if (include_zero_allocations ||
428 allocation->at(observer_config.observer) != 0) {
429 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
430 observer_config.max_bitrate_bps, &observer_config));
431 }
432 }
433 auto it = list_max_bitrates.begin();
434 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800435 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200436 uint32_t extra_allocation =
437 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
438 uint32_t total_allocation =
439 extra_allocation + allocation->at(it->second->observer);
440 bitrate -= extra_allocation;
441 if (total_allocation > max_multiplier * it->first) {
442 // There is more than we can fit for this observer, carry over to the
443 // remaining observers.
444 bitrate += total_allocation - max_multiplier * it->first;
445 total_allocation = max_multiplier * it->first;
446 }
447 // Finally, update the allocation for this observer.
448 allocation->at(it->second->observer) = total_allocation;
449 it = list_max_bitrates.erase(it);
450 }
451}
452
453bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
454 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700455 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200456 if (bitrate < sum_min_bitrates)
457 return false;
458
perkj26091b12016-09-01 01:17:40 -0700459 uint32_t extra_bitrate_per_observer =
460 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200461 static_cast<uint32_t>(bitrate_observer_configs_.size());
462 for (const auto& observer_config : bitrate_observer_configs_) {
463 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
philipel5ef2bc12017-02-21 07:28:31 -0800464 MinBitrateWithHysteresis(observer_config)) {
mflodman101f2502016-06-09 17:21:19 +0200465 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800466 }
mflodman101f2502016-06-09 17:21:19 +0200467 }
468 return true;
469}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000470} // namespace webrtc