blob: 70763f3e8d059711fb03296889f8baebc3c64e9c [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
mflodman0e7e2592015-11-12 21:02:42 -080012#include "webrtc/call/bitrate_allocator.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000013
14#include <algorithm>
15#include <utility>
16
mflodman2ebe5b12016-05-13 01:43:51 -070017#include "webrtc/base/checks.h"
mflodman48a4beb2016-07-01 13:03:59 +020018#include "webrtc/base/logging.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000019#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
mflodman48a4beb2016-07-01 13:03:59 +020020#include "webrtc/system_wrappers/include/clock.h"
21#include "webrtc/system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000022
23namespace webrtc {
24
Stefan Holmere5904162015-03-26 11:11:06 +010025// Allow packets to be transmitted in up to 2 times max video bitrate if the
26// bandwidth estimate allows it.
27const int kTransmissionMaxBitrateMultiplier = 2;
28const int kDefaultBitrateBps = 300000;
29
mflodman101f2502016-06-09 17:21:19 +020030// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
31const double kToggleFactor = 0.1;
32const uint32_t kMinToggleBitrateBps = 20000;
33
mflodman48a4beb2016-07-01 13:03:59 +020034const int64_t kBweLogIntervalMs = 5000;
35
36namespace {
37
38double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
39 RTC_DCHECK_GT(allocated_bitrate, 0u);
40 if (protection_bitrate == 0)
41 return 1.0;
42
43 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
44 return media_bitrate / static_cast<double>(allocated_bitrate);
45}
46} // namespace
47
perkj71ee44c2016-06-15 00:47:53 -070048BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
49 : limit_observer_(limit_observer),
50 bitrate_observer_configs_(),
Sergey Ulanove2b15012016-11-22 16:08:30 -080051 last_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -070052 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010053 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020054 last_rtt_(0),
55 num_pause_events_(0),
56 clock_(Clock::GetRealTimeClock()),
perkj26091b12016-09-01 01:17:40 -070057 last_bwe_log_time_(0) {
58 sequenced_checker_.Detach();
59}
mflodman48a4beb2016-07-01 13:03:59 +020060
61BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070062 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
63 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020064}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000065
perkj71ee44c2016-06-15 00:47:53 -070066void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
67 uint8_t fraction_loss,
michaelt9abbf5a2016-11-28 07:00:18 -080068 int64_t rtt,
69 int64_t probing_interval_ms) {
perkj26091b12016-09-01 01:17:40 -070070 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020071 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070072 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020073 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010074 last_fraction_loss_ = fraction_loss;
75 last_rtt_ = rtt;
michaelt9abbf5a2016-11-28 07:00:18 -080076 last_probing_interval_ms_ = probing_interval_ms;
mflodman2ebe5b12016-05-13 01:43:51 -070077
mflodman48a4beb2016-07-01 13:03:59 +020078 // Periodically log the incoming BWE.
79 int64_t now = clock_->TimeInMilliseconds();
80 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
81 LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
82 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -080083 }
mflodman48a4beb2016-07-01 13:03:59 +020084
85 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
86
87 for (auto& config : bitrate_observer_configs_) {
88 uint32_t allocated_bitrate = allocation[config.observer];
89 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
michaelt9abbf5a2016-11-28 07:00:18 -080090 allocated_bitrate, last_fraction_loss_, last_rtt_,
91 last_probing_interval_ms_);
mflodman48a4beb2016-07-01 13:03:59 +020092
93 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
94 if (target_bitrate_bps > 0)
95 ++num_pause_events_;
96 // The protection bitrate is an estimate based on the ratio between media
97 // and protection used before this observer was muted.
98 uint32_t predicted_protection_bps =
99 (1.0 - config.media_ratio) * config.min_bitrate_bps;
100 LOG(LS_INFO) << "Pausing observer " << config.observer
101 << " with configured min bitrate " << config.min_bitrate_bps
102 << " and current estimate of " << target_bitrate_bps
103 << " and protection bitrate " << predicted_protection_bps;
104 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
105 if (target_bitrate_bps > 0)
106 ++num_pause_events_;
107 LOG(LS_INFO) << "Resuming observer " << config.observer
108 << ", configured min bitrate " << config.min_bitrate_bps
109 << ", current allocation " << allocated_bitrate
110 << " and protection bitrate " << protection_bitrate;
111 }
112
113 // Only update the media ratio if the observer got an allocation.
114 if (allocated_bitrate > 0)
115 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
116 config.allocated_bitrate_bps = allocated_bitrate;
117 }
Stefan Holmere5904162015-03-26 11:11:06 +0100118}
119
perkj57c21f92016-06-17 07:27:16 -0700120void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
121 uint32_t min_bitrate_bps,
122 uint32_t max_bitrate_bps,
123 uint32_t pad_up_bitrate_bps,
124 bool enforce_min_bitrate) {
perkj26091b12016-09-01 01:17:40 -0700125 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700126 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000127
mflodman101f2502016-06-09 17:21:19 +0200128 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700129 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700130 it->min_bitrate_bps = min_bitrate_bps;
131 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700132 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200133 it->enforce_min_bitrate = enforce_min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000134 } else {
perkj71ee44c2016-06-15 00:47:53 -0700135 bitrate_observer_configs_.push_back(
136 ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps,
137 pad_up_bitrate_bps, enforce_min_bitrate));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000138 }
Stefan Holmere5904162015-03-26 11:11:06 +0100139
mflodman101f2502016-06-09 17:21:19 +0200140 ObserverAllocation allocation;
141 if (last_bitrate_bps_ > 0) {
142 // Calculate a new allocation and update all observers.
143 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200144 for (auto& config : bitrate_observer_configs_) {
145 uint32_t allocated_bitrate = allocation[config.observer];
146 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
michaelt9abbf5a2016-11-28 07:00:18 -0800147 allocated_bitrate, last_fraction_loss_, last_rtt_,
148 last_probing_interval_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200149 config.allocated_bitrate_bps = allocated_bitrate;
150 if (allocated_bitrate > 0)
151 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
152 }
perkjfea93092016-05-14 00:58:48 -0700153 } else {
154 // Currently, an encoder is not allowed to produce frames.
155 // But we still have to return the initial config bitrate + let the
156 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200157 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
michaelt9abbf5a2016-11-28 07:00:18 -0800158 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
159 last_probing_interval_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100160 }
perkj71ee44c2016-06-15 00:47:53 -0700161 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000162}
163
perkj71ee44c2016-06-15 00:47:53 -0700164void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700165 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700166 uint32_t total_requested_padding_bitrate = 0;
167 uint32_t total_requested_min_bitrate = 0;
168
perkj26091b12016-09-01 01:17:40 -0700169 for (const auto& config : bitrate_observer_configs_) {
170 if (config.enforce_min_bitrate) {
171 total_requested_min_bitrate += config.min_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700172 }
perkj26091b12016-09-01 01:17:40 -0700173 total_requested_padding_bitrate += config.pad_up_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000174 }
perkj71ee44c2016-06-15 00:47:53 -0700175
perkj9b522f82016-07-07 00:36:28 -0700176 LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
177 << total_requested_min_bitrate
178 << "bps, total_requested_padding_bitrate: "
179 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700180 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
181 total_requested_padding_bitrate);
182}
183
184void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700185 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
186 auto it = FindObserverConfig(observer);
187 if (it != bitrate_observer_configs_.end()) {
188 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700189 }
perkj26091b12016-09-01 01:17:40 -0700190
perkj71ee44c2016-06-15 00:47:53 -0700191 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000192}
193
perkj57c21f92016-06-17 07:27:16 -0700194int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700195 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200196 const auto& it = FindObserverConfig(observer);
197 if (it == bitrate_observer_configs_.end()) {
198 // This observer hasn't been added yet, just give it its fair share.
199 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700200 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200201 } else if (it->allocated_bitrate_bps == -1) {
202 // This observer hasn't received an allocation yet, so do the same.
203 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700204 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200205 } else {
206 // This observer already has an allocation.
207 return it->allocated_bitrate_bps;
208 }
perkj57c21f92016-06-17 07:27:16 -0700209}
210
mflodman48a4beb2016-07-01 13:03:59 +0200211BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700212BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
213 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700214 for (auto it = bitrate_observer_configs_.begin();
215 it != bitrate_observer_configs_.end(); ++it) {
216 if (it->observer == observer)
217 return it;
218 }
219 return bitrate_observer_configs_.end();
220}
221
perkjfea93092016-05-14 00:58:48 -0700222BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
223 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700224 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700225 if (bitrate_observer_configs_.empty())
226 return ObserverAllocation();
227
perkjfea93092016-05-14 00:58:48 -0700228 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700229 return ZeroRateAllocation();
230
231 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200232 uint32_t sum_max_bitrates = 0;
233 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700234 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200235 sum_max_bitrates += observer_config.max_bitrate_bps;
236 }
237
238 // Not enough for all observers to get an allocation, allocate according to:
239 // enforced min bitrate -> allocated bitrate previous round -> restart paused
240 // streams.
241 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700242 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700243
mflodman101f2502016-06-09 17:21:19 +0200244 // All observers will get their min bitrate plus an even share of the rest.
245 if (bitrate <= sum_max_bitrates)
246 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700247
mflodman101f2502016-06-09 17:21:19 +0200248 // All observers will get up to kTransmissionMaxBitrateMultiplier x max.
249 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000250}
251
mflodman2ebe5b12016-05-13 01:43:51 -0700252BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700253 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700254 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700255 for (const auto& observer_config : bitrate_observer_configs_)
256 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700257 return allocation;
258}
259
mflodman2ebe5b12016-05-13 01:43:51 -0700260BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100261 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700262 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700263 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200264 // Start by allocating bitrate to observers enforcing a min bitrate, hence
265 // remaining_bitrate might turn negative.
266 int64_t remaining_bitrate = bitrate;
267 for (const auto& observer_config : bitrate_observer_configs_) {
268 int32_t allocated_bitrate = 0;
269 if (observer_config.enforce_min_bitrate)
270 allocated_bitrate = observer_config.min_bitrate_bps;
271
272 allocation[observer_config.observer] = allocated_bitrate;
273 remaining_bitrate -= allocated_bitrate;
274 }
275
276 // Allocate bitrate to all previously active streams.
277 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700278 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200279 if (observer_config.enforce_min_bitrate ||
280 LastAllocatedBitrate(observer_config) == 0)
281 continue;
282
mflodman48a4beb2016-07-01 13:03:59 +0200283 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
284 if (remaining_bitrate >= required_bitrate) {
285 allocation[observer_config.observer] = required_bitrate;
286 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200287 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000288 }
289 }
mflodman101f2502016-06-09 17:21:19 +0200290
291 // Allocate bitrate to previously paused streams.
292 if (remaining_bitrate > 0) {
293 for (const auto& observer_config : bitrate_observer_configs_) {
294 if (LastAllocatedBitrate(observer_config) != 0)
295 continue;
296
297 // Add a hysteresis to avoid toggling.
298 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
299 if (remaining_bitrate >= required_bitrate) {
300 allocation[observer_config.observer] = required_bitrate;
301 remaining_bitrate -= required_bitrate;
302 }
303 }
304 }
305
306 // Split a possible remainder evenly on all streams with an allocation.
307 if (remaining_bitrate > 0)
308 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
309
310 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100311 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000312}
mflodman101f2502016-06-09 17:21:19 +0200313
314BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
315 uint32_t bitrate,
316 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700317 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200318 ObserverAllocation allocation;
319 for (const auto& observer_config : bitrate_observer_configs_)
320 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
321
322 bitrate -= sum_min_bitrates;
323 if (bitrate > 0)
324 DistributeBitrateEvenly(bitrate, true, 1, &allocation);
325
326 return allocation;
327}
328
329BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700330 uint32_t bitrate,
331 uint32_t sum_max_bitrates) {
332 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200333 ObserverAllocation allocation;
334
335 for (const auto& observer_config : bitrate_observer_configs_) {
336 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
337 bitrate -= observer_config.max_bitrate_bps;
338 }
339 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier,
340 &allocation);
341 return allocation;
342}
343
344uint32_t BitrateAllocator::LastAllocatedBitrate(
345 const ObserverConfig& observer_config) {
mflodman101f2502016-06-09 17:21:19 +0200346 // Return the configured minimum bitrate for newly added observers, to avoid
347 // requiring an extra high bitrate for the observer to get an allocated
348 // bitrate.
perkj26091b12016-09-01 01:17:40 -0700349 return observer_config.allocated_bitrate_bps == -1
350 ? observer_config.min_bitrate_bps
351 : observer_config.allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200352}
353
354uint32_t BitrateAllocator::MinBitrateWithHysteresis(
355 const ObserverConfig& observer_config) {
356 uint32_t min_bitrate = observer_config.min_bitrate_bps;
357 if (LastAllocatedBitrate(observer_config) == 0) {
358 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
359 kMinToggleBitrateBps);
360 }
mflodman48a4beb2016-07-01 13:03:59 +0200361 // Account for protection bitrate used by this observer in the previous
362 // allocation.
363 // Note: the ratio will only be updated when the stream is active, meaning a
364 // paused stream won't get any ratio updates. This might lead to waiting a bit
365 // longer than necessary if the network condition improves, but this is to
366 // avoid too much toggling.
367 if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0)
368 min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio);
369
mflodman101f2502016-06-09 17:21:19 +0200370 return min_bitrate;
371}
372
373void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
374 bool include_zero_allocations,
375 int max_multiplier,
376 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700377 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200378 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
379
380 ObserverSortingMap list_max_bitrates;
381 for (const auto& observer_config : bitrate_observer_configs_) {
382 if (include_zero_allocations ||
383 allocation->at(observer_config.observer) != 0) {
384 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
385 observer_config.max_bitrate_bps, &observer_config));
386 }
387 }
388 auto it = list_max_bitrates.begin();
389 while (it != list_max_bitrates.end()) {
390 RTC_DCHECK_GT(bitrate, 0u);
391 uint32_t extra_allocation =
392 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
393 uint32_t total_allocation =
394 extra_allocation + allocation->at(it->second->observer);
395 bitrate -= extra_allocation;
396 if (total_allocation > max_multiplier * it->first) {
397 // There is more than we can fit for this observer, carry over to the
398 // remaining observers.
399 bitrate += total_allocation - max_multiplier * it->first;
400 total_allocation = max_multiplier * it->first;
401 }
402 // Finally, update the allocation for this observer.
403 allocation->at(it->second->observer) = total_allocation;
404 it = list_max_bitrates.erase(it);
405 }
406}
407
408bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
409 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700410 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200411 if (bitrate < sum_min_bitrates)
412 return false;
413
perkj26091b12016-09-01 01:17:40 -0700414 uint32_t extra_bitrate_per_observer =
415 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200416 static_cast<uint32_t>(bitrate_observer_configs_.size());
417 for (const auto& observer_config : bitrate_observer_configs_) {
418 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
419 MinBitrateWithHysteresis(observer_config))
420 return false;
421 }
422 return true;
423}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000424} // namespace webrtc