blob: 645ee3c41ca1ff8f0bd7fe8a8fbd0e8a31147400 [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_(),
Stefan Holmere5904162015-03-26 11:11:06 +010051 last_bitrate_bps_(kDefaultBitrateBps),
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,
68 int64_t rtt) {
perkj26091b12016-09-01 01:17:40 -070069 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020070 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070071 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020072 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010073 last_fraction_loss_ = fraction_loss;
74 last_rtt_ = rtt;
mflodman2ebe5b12016-05-13 01:43:51 -070075
mflodman48a4beb2016-07-01 13:03:59 +020076 // Periodically log the incoming BWE.
77 int64_t now = clock_->TimeInMilliseconds();
78 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
79 LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
80 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -080081 }
mflodman48a4beb2016-07-01 13:03:59 +020082
83 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
84
85 for (auto& config : bitrate_observer_configs_) {
86 uint32_t allocated_bitrate = allocation[config.observer];
87 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
88 allocated_bitrate, last_fraction_loss_, last_rtt_);
89
90 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
91 if (target_bitrate_bps > 0)
92 ++num_pause_events_;
93 // The protection bitrate is an estimate based on the ratio between media
94 // and protection used before this observer was muted.
95 uint32_t predicted_protection_bps =
96 (1.0 - config.media_ratio) * config.min_bitrate_bps;
97 LOG(LS_INFO) << "Pausing observer " << config.observer
98 << " with configured min bitrate " << config.min_bitrate_bps
99 << " and current estimate of " << target_bitrate_bps
100 << " and protection bitrate " << predicted_protection_bps;
101 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
102 if (target_bitrate_bps > 0)
103 ++num_pause_events_;
104 LOG(LS_INFO) << "Resuming observer " << config.observer
105 << ", configured min bitrate " << config.min_bitrate_bps
106 << ", current allocation " << allocated_bitrate
107 << " and protection bitrate " << protection_bitrate;
108 }
109
110 // Only update the media ratio if the observer got an allocation.
111 if (allocated_bitrate > 0)
112 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
113 config.allocated_bitrate_bps = allocated_bitrate;
114 }
Stefan Holmere5904162015-03-26 11:11:06 +0100115}
116
perkj57c21f92016-06-17 07:27:16 -0700117void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
118 uint32_t min_bitrate_bps,
119 uint32_t max_bitrate_bps,
120 uint32_t pad_up_bitrate_bps,
121 bool enforce_min_bitrate) {
perkj26091b12016-09-01 01:17:40 -0700122 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700123 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000124
mflodman101f2502016-06-09 17:21:19 +0200125 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700126 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700127 it->min_bitrate_bps = min_bitrate_bps;
128 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700129 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200130 it->enforce_min_bitrate = enforce_min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000131 } else {
perkj71ee44c2016-06-15 00:47:53 -0700132 bitrate_observer_configs_.push_back(
133 ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps,
134 pad_up_bitrate_bps, enforce_min_bitrate));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000135 }
Stefan Holmere5904162015-03-26 11:11:06 +0100136
mflodman101f2502016-06-09 17:21:19 +0200137 ObserverAllocation allocation;
138 if (last_bitrate_bps_ > 0) {
139 // Calculate a new allocation and update all observers.
140 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200141 for (auto& config : bitrate_observer_configs_) {
142 uint32_t allocated_bitrate = allocation[config.observer];
143 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
144 allocated_bitrate, last_fraction_loss_, last_rtt_);
145 config.allocated_bitrate_bps = allocated_bitrate;
146 if (allocated_bitrate > 0)
147 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
148 }
perkjfea93092016-05-14 00:58:48 -0700149 } else {
150 // Currently, an encoder is not allowed to produce frames.
151 // But we still have to return the initial config bitrate + let the
152 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200153 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
perkjfea93092016-05-14 00:58:48 -0700154 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_);
Stefan Holmere5904162015-03-26 11:11:06 +0100155 }
perkj71ee44c2016-06-15 00:47:53 -0700156 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000157}
158
perkj71ee44c2016-06-15 00:47:53 -0700159void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700160 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700161 uint32_t total_requested_padding_bitrate = 0;
162 uint32_t total_requested_min_bitrate = 0;
163
perkj26091b12016-09-01 01:17:40 -0700164 for (const auto& config : bitrate_observer_configs_) {
165 if (config.enforce_min_bitrate) {
166 total_requested_min_bitrate += config.min_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700167 }
perkj26091b12016-09-01 01:17:40 -0700168 total_requested_padding_bitrate += config.pad_up_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000169 }
perkj71ee44c2016-06-15 00:47:53 -0700170
perkj9b522f82016-07-07 00:36:28 -0700171 LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
172 << total_requested_min_bitrate
173 << "bps, total_requested_padding_bitrate: "
174 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700175 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
176 total_requested_padding_bitrate);
177}
178
179void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700180 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
181 auto it = FindObserverConfig(observer);
182 if (it != bitrate_observer_configs_.end()) {
183 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700184 }
perkj26091b12016-09-01 01:17:40 -0700185
perkj71ee44c2016-06-15 00:47:53 -0700186 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000187}
188
perkj57c21f92016-06-17 07:27:16 -0700189int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700190 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200191 const auto& it = FindObserverConfig(observer);
192 if (it == bitrate_observer_configs_.end()) {
193 // This observer hasn't been added yet, just give it its fair share.
194 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700195 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200196 } else if (it->allocated_bitrate_bps == -1) {
197 // This observer hasn't received an allocation yet, so do the same.
198 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700199 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200200 } else {
201 // This observer already has an allocation.
202 return it->allocated_bitrate_bps;
203 }
perkj57c21f92016-06-17 07:27:16 -0700204}
205
mflodman48a4beb2016-07-01 13:03:59 +0200206BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700207BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
208 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700209 for (auto it = bitrate_observer_configs_.begin();
210 it != bitrate_observer_configs_.end(); ++it) {
211 if (it->observer == observer)
212 return it;
213 }
214 return bitrate_observer_configs_.end();
215}
216
perkjfea93092016-05-14 00:58:48 -0700217BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
218 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700219 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700220 if (bitrate_observer_configs_.empty())
221 return ObserverAllocation();
222
perkjfea93092016-05-14 00:58:48 -0700223 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700224 return ZeroRateAllocation();
225
226 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200227 uint32_t sum_max_bitrates = 0;
228 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700229 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200230 sum_max_bitrates += observer_config.max_bitrate_bps;
231 }
232
233 // Not enough for all observers to get an allocation, allocate according to:
234 // enforced min bitrate -> allocated bitrate previous round -> restart paused
235 // streams.
236 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700237 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700238
mflodman101f2502016-06-09 17:21:19 +0200239 // All observers will get their min bitrate plus an even share of the rest.
240 if (bitrate <= sum_max_bitrates)
241 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700242
mflodman101f2502016-06-09 17:21:19 +0200243 // All observers will get up to kTransmissionMaxBitrateMultiplier x max.
244 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000245}
246
mflodman2ebe5b12016-05-13 01:43:51 -0700247BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700248 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700249 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700250 for (const auto& observer_config : bitrate_observer_configs_)
251 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700252 return allocation;
253}
254
mflodman2ebe5b12016-05-13 01:43:51 -0700255BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100256 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700257 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700258 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200259 // Start by allocating bitrate to observers enforcing a min bitrate, hence
260 // remaining_bitrate might turn negative.
261 int64_t remaining_bitrate = bitrate;
262 for (const auto& observer_config : bitrate_observer_configs_) {
263 int32_t allocated_bitrate = 0;
264 if (observer_config.enforce_min_bitrate)
265 allocated_bitrate = observer_config.min_bitrate_bps;
266
267 allocation[observer_config.observer] = allocated_bitrate;
268 remaining_bitrate -= allocated_bitrate;
269 }
270
271 // Allocate bitrate to all previously active streams.
272 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700273 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200274 if (observer_config.enforce_min_bitrate ||
275 LastAllocatedBitrate(observer_config) == 0)
276 continue;
277
mflodman48a4beb2016-07-01 13:03:59 +0200278 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
279 if (remaining_bitrate >= required_bitrate) {
280 allocation[observer_config.observer] = required_bitrate;
281 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200282 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000283 }
284 }
mflodman101f2502016-06-09 17:21:19 +0200285
286 // Allocate bitrate to previously paused streams.
287 if (remaining_bitrate > 0) {
288 for (const auto& observer_config : bitrate_observer_configs_) {
289 if (LastAllocatedBitrate(observer_config) != 0)
290 continue;
291
292 // Add a hysteresis to avoid toggling.
293 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
294 if (remaining_bitrate >= required_bitrate) {
295 allocation[observer_config.observer] = required_bitrate;
296 remaining_bitrate -= required_bitrate;
297 }
298 }
299 }
300
301 // Split a possible remainder evenly on all streams with an allocation.
302 if (remaining_bitrate > 0)
303 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
304
305 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100306 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000307}
mflodman101f2502016-06-09 17:21:19 +0200308
309BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
310 uint32_t bitrate,
311 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700312 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200313 ObserverAllocation allocation;
314 for (const auto& observer_config : bitrate_observer_configs_)
315 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
316
317 bitrate -= sum_min_bitrates;
318 if (bitrate > 0)
319 DistributeBitrateEvenly(bitrate, true, 1, &allocation);
320
321 return allocation;
322}
323
324BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700325 uint32_t bitrate,
326 uint32_t sum_max_bitrates) {
327 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200328 ObserverAllocation allocation;
329
330 for (const auto& observer_config : bitrate_observer_configs_) {
331 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
332 bitrate -= observer_config.max_bitrate_bps;
333 }
334 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier,
335 &allocation);
336 return allocation;
337}
338
339uint32_t BitrateAllocator::LastAllocatedBitrate(
340 const ObserverConfig& observer_config) {
mflodman101f2502016-06-09 17:21:19 +0200341 // Return the configured minimum bitrate for newly added observers, to avoid
342 // requiring an extra high bitrate for the observer to get an allocated
343 // bitrate.
perkj26091b12016-09-01 01:17:40 -0700344 return observer_config.allocated_bitrate_bps == -1
345 ? observer_config.min_bitrate_bps
346 : observer_config.allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200347}
348
349uint32_t BitrateAllocator::MinBitrateWithHysteresis(
350 const ObserverConfig& observer_config) {
351 uint32_t min_bitrate = observer_config.min_bitrate_bps;
352 if (LastAllocatedBitrate(observer_config) == 0) {
353 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
354 kMinToggleBitrateBps);
355 }
mflodman48a4beb2016-07-01 13:03:59 +0200356 // Account for protection bitrate used by this observer in the previous
357 // allocation.
358 // Note: the ratio will only be updated when the stream is active, meaning a
359 // paused stream won't get any ratio updates. This might lead to waiting a bit
360 // longer than necessary if the network condition improves, but this is to
361 // avoid too much toggling.
362 if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0)
363 min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio);
364
mflodman101f2502016-06-09 17:21:19 +0200365 return min_bitrate;
366}
367
368void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
369 bool include_zero_allocations,
370 int max_multiplier,
371 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700372 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200373 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
374
375 ObserverSortingMap list_max_bitrates;
376 for (const auto& observer_config : bitrate_observer_configs_) {
377 if (include_zero_allocations ||
378 allocation->at(observer_config.observer) != 0) {
379 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
380 observer_config.max_bitrate_bps, &observer_config));
381 }
382 }
383 auto it = list_max_bitrates.begin();
384 while (it != list_max_bitrates.end()) {
385 RTC_DCHECK_GT(bitrate, 0u);
386 uint32_t extra_allocation =
387 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
388 uint32_t total_allocation =
389 extra_allocation + allocation->at(it->second->observer);
390 bitrate -= extra_allocation;
391 if (total_allocation > max_multiplier * it->first) {
392 // There is more than we can fit for this observer, carry over to the
393 // remaining observers.
394 bitrate += total_allocation - max_multiplier * it->first;
395 total_allocation = max_multiplier * it->first;
396 }
397 // Finally, update the allocation for this observer.
398 allocation->at(it->second->observer) = total_allocation;
399 it = list_max_bitrates.erase(it);
400 }
401}
402
403bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
404 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700405 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200406 if (bitrate < sum_min_bitrates)
407 return false;
408
perkj26091b12016-09-01 01:17:40 -0700409 uint32_t extra_bitrate_per_observer =
410 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200411 static_cast<uint32_t>(bitrate_observer_configs_.size());
412 for (const auto& observer_config : bitrate_observer_configs_) {
413 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
414 MinBitrateWithHysteresis(observer_config))
415 return false;
416 }
417 return true;
418}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000419} // namespace webrtc