blob: c3568df34c65a4595ccea2eb154b493cdeb73e57 [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) {
kwibergaf476c72016-11-28 15:21:39 -080039 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 13:03:59 +020040 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()),
philipel5ef2bc12017-02-21 07:28:31 -080057 last_bwe_log_time_(0),
58 total_requested_padding_bitrate_(0),
59 total_requested_min_bitrate_(0) {
perkj26091b12016-09-01 01:17:40 -070060 sequenced_checker_.Detach();
61}
mflodman48a4beb2016-07-01 13:03:59 +020062
63BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070064 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
65 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020066}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000067
perkj71ee44c2016-06-15 00:47:53 -070068void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
69 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080070 int64_t rtt,
71 int64_t probing_interval_ms) {
perkj26091b12016-09-01 01:17:40 -070072 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020073 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070074 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020075 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010076 last_fraction_loss_ = fraction_loss;
77 last_rtt_ = rtt;
minyue78b4d562016-11-30 04:47:39 -080078 last_probing_interval_ms_ = probing_interval_ms;
mflodman2ebe5b12016-05-13 01:43:51 -070079
mflodman48a4beb2016-07-01 13:03:59 +020080 // Periodically log the incoming BWE.
81 int64_t now = clock_->TimeInMilliseconds();
82 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
83 LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
84 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -080085 }
mflodman48a4beb2016-07-01 13:03:59 +020086
87 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
88
89 for (auto& config : bitrate_observer_configs_) {
90 uint32_t allocated_bitrate = allocation[config.observer];
91 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -080092 allocated_bitrate, last_fraction_loss_, last_rtt_,
93 last_probing_interval_ms_);
mflodman48a4beb2016-07-01 13:03:59 +020094
95 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
96 if (target_bitrate_bps > 0)
97 ++num_pause_events_;
98 // The protection bitrate is an estimate based on the ratio between media
99 // and protection used before this observer was muted.
100 uint32_t predicted_protection_bps =
101 (1.0 - config.media_ratio) * config.min_bitrate_bps;
102 LOG(LS_INFO) << "Pausing observer " << config.observer
103 << " with configured min bitrate " << config.min_bitrate_bps
104 << " and current estimate of " << target_bitrate_bps
105 << " and protection bitrate " << predicted_protection_bps;
106 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
107 if (target_bitrate_bps > 0)
108 ++num_pause_events_;
109 LOG(LS_INFO) << "Resuming observer " << config.observer
110 << ", configured min bitrate " << config.min_bitrate_bps
111 << ", current allocation " << allocated_bitrate
112 << " and protection bitrate " << protection_bitrate;
113 }
114
115 // Only update the media ratio if the observer got an allocation.
116 if (allocated_bitrate > 0)
117 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
118 config.allocated_bitrate_bps = allocated_bitrate;
119 }
philipel5ef2bc12017-02-21 07:28:31 -0800120 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100121}
122
perkj57c21f92016-06-17 07:27:16 -0700123void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
124 uint32_t min_bitrate_bps,
125 uint32_t max_bitrate_bps,
126 uint32_t pad_up_bitrate_bps,
127 bool enforce_min_bitrate) {
perkj26091b12016-09-01 01:17:40 -0700128 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700129 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000130
mflodman101f2502016-06-09 17:21:19 +0200131 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700132 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700133 it->min_bitrate_bps = min_bitrate_bps;
134 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700135 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200136 it->enforce_min_bitrate = enforce_min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000137 } else {
perkj71ee44c2016-06-15 00:47:53 -0700138 bitrate_observer_configs_.push_back(
139 ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps,
140 pad_up_bitrate_bps, enforce_min_bitrate));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000141 }
Stefan Holmere5904162015-03-26 11:11:06 +0100142
mflodman101f2502016-06-09 17:21:19 +0200143 ObserverAllocation allocation;
144 if (last_bitrate_bps_ > 0) {
145 // Calculate a new allocation and update all observers.
146 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200147 for (auto& config : bitrate_observer_configs_) {
148 uint32_t allocated_bitrate = allocation[config.observer];
149 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800150 allocated_bitrate, last_fraction_loss_, last_rtt_,
151 last_probing_interval_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200152 config.allocated_bitrate_bps = allocated_bitrate;
153 if (allocated_bitrate > 0)
154 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
155 }
perkjfea93092016-05-14 00:58:48 -0700156 } else {
157 // Currently, an encoder is not allowed to produce frames.
158 // But we still have to return the initial config bitrate + let the
159 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200160 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800161 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
162 last_probing_interval_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100163 }
perkj71ee44c2016-06-15 00:47:53 -0700164 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000165}
166
perkj71ee44c2016-06-15 00:47:53 -0700167void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700168 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700169 uint32_t total_requested_padding_bitrate = 0;
170 uint32_t total_requested_min_bitrate = 0;
171
perkj26091b12016-09-01 01:17:40 -0700172 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800173 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700174 if (config.enforce_min_bitrate) {
175 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800176 } else if (config.allocated_bitrate_bps == 0) {
177 stream_padding =
178 std::max(MinBitrateWithHysteresis(config), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700179 }
philipel5ef2bc12017-02-21 07:28:31 -0800180 total_requested_padding_bitrate += stream_padding;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000181 }
perkj71ee44c2016-06-15 00:47:53 -0700182
philipel5ef2bc12017-02-21 07:28:31 -0800183 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
184 total_requested_min_bitrate == total_requested_min_bitrate_) {
185 return;
186 }
187
188 total_requested_min_bitrate_ = total_requested_min_bitrate;
189 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
190
perkj9b522f82016-07-07 00:36:28 -0700191 LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
192 << total_requested_min_bitrate
193 << "bps, total_requested_padding_bitrate: "
194 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700195 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
196 total_requested_padding_bitrate);
197}
198
199void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700200 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
201 auto it = FindObserverConfig(observer);
202 if (it != bitrate_observer_configs_.end()) {
203 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700204 }
perkj26091b12016-09-01 01:17:40 -0700205
perkj71ee44c2016-06-15 00:47:53 -0700206 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000207}
208
perkj57c21f92016-06-17 07:27:16 -0700209int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700210 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200211 const auto& it = FindObserverConfig(observer);
212 if (it == bitrate_observer_configs_.end()) {
213 // This observer hasn't been added yet, just give it its fair share.
214 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700215 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200216 } else if (it->allocated_bitrate_bps == -1) {
217 // This observer hasn't received an allocation yet, so do the same.
218 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700219 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200220 } else {
221 // This observer already has an allocation.
222 return it->allocated_bitrate_bps;
223 }
perkj57c21f92016-06-17 07:27:16 -0700224}
225
mflodman48a4beb2016-07-01 13:03:59 +0200226BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700227BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
228 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700229 for (auto it = bitrate_observer_configs_.begin();
230 it != bitrate_observer_configs_.end(); ++it) {
231 if (it->observer == observer)
232 return it;
233 }
234 return bitrate_observer_configs_.end();
235}
236
perkjfea93092016-05-14 00:58:48 -0700237BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
238 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700239 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700240 if (bitrate_observer_configs_.empty())
241 return ObserverAllocation();
242
perkjfea93092016-05-14 00:58:48 -0700243 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700244 return ZeroRateAllocation();
245
246 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200247 uint32_t sum_max_bitrates = 0;
248 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700249 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200250 sum_max_bitrates += observer_config.max_bitrate_bps;
251 }
252
253 // Not enough for all observers to get an allocation, allocate according to:
254 // enforced min bitrate -> allocated bitrate previous round -> restart paused
255 // streams.
256 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700257 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700258
mflodman101f2502016-06-09 17:21:19 +0200259 // All observers will get their min bitrate plus an even share of the rest.
260 if (bitrate <= sum_max_bitrates)
261 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700262
mflodman101f2502016-06-09 17:21:19 +0200263 // All observers will get up to kTransmissionMaxBitrateMultiplier x max.
264 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000265}
266
mflodman2ebe5b12016-05-13 01:43:51 -0700267BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700268 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700269 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700270 for (const auto& observer_config : bitrate_observer_configs_)
271 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700272 return allocation;
273}
274
mflodman2ebe5b12016-05-13 01:43:51 -0700275BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100276 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700277 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700278 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200279 // Start by allocating bitrate to observers enforcing a min bitrate, hence
280 // remaining_bitrate might turn negative.
281 int64_t remaining_bitrate = bitrate;
282 for (const auto& observer_config : bitrate_observer_configs_) {
283 int32_t allocated_bitrate = 0;
284 if (observer_config.enforce_min_bitrate)
285 allocated_bitrate = observer_config.min_bitrate_bps;
286
287 allocation[observer_config.observer] = allocated_bitrate;
288 remaining_bitrate -= allocated_bitrate;
289 }
290
291 // Allocate bitrate to all previously active streams.
292 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700293 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200294 if (observer_config.enforce_min_bitrate ||
295 LastAllocatedBitrate(observer_config) == 0)
296 continue;
297
mflodman48a4beb2016-07-01 13:03:59 +0200298 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;
mflodman101f2502016-06-09 17:21:19 +0200302 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000303 }
304 }
mflodman101f2502016-06-09 17:21:19 +0200305
306 // Allocate bitrate to previously paused streams.
307 if (remaining_bitrate > 0) {
308 for (const auto& observer_config : bitrate_observer_configs_) {
309 if (LastAllocatedBitrate(observer_config) != 0)
310 continue;
311
312 // Add a hysteresis to avoid toggling.
313 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
314 if (remaining_bitrate >= required_bitrate) {
315 allocation[observer_config.observer] = required_bitrate;
316 remaining_bitrate -= required_bitrate;
317 }
318 }
319 }
320
321 // Split a possible remainder evenly on all streams with an allocation.
322 if (remaining_bitrate > 0)
323 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
324
325 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100326 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000327}
mflodman101f2502016-06-09 17:21:19 +0200328
329BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
330 uint32_t bitrate,
331 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700332 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200333 ObserverAllocation allocation;
334 for (const auto& observer_config : bitrate_observer_configs_)
335 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
336
337 bitrate -= sum_min_bitrates;
338 if (bitrate > 0)
339 DistributeBitrateEvenly(bitrate, true, 1, &allocation);
340
341 return allocation;
342}
343
344BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700345 uint32_t bitrate,
346 uint32_t sum_max_bitrates) {
347 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200348 ObserverAllocation allocation;
349
350 for (const auto& observer_config : bitrate_observer_configs_) {
351 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
352 bitrate -= observer_config.max_bitrate_bps;
353 }
354 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier,
355 &allocation);
356 return allocation;
357}
358
359uint32_t BitrateAllocator::LastAllocatedBitrate(
360 const ObserverConfig& observer_config) {
mflodman101f2502016-06-09 17:21:19 +0200361 // Return the configured minimum bitrate for newly added observers, to avoid
362 // requiring an extra high bitrate for the observer to get an allocated
363 // bitrate.
perkj26091b12016-09-01 01:17:40 -0700364 return observer_config.allocated_bitrate_bps == -1
365 ? observer_config.min_bitrate_bps
366 : observer_config.allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200367}
368
369uint32_t BitrateAllocator::MinBitrateWithHysteresis(
370 const ObserverConfig& observer_config) {
371 uint32_t min_bitrate = observer_config.min_bitrate_bps;
372 if (LastAllocatedBitrate(observer_config) == 0) {
373 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
374 kMinToggleBitrateBps);
375 }
mflodman48a4beb2016-07-01 13:03:59 +0200376 // Account for protection bitrate used by this observer in the previous
377 // allocation.
378 // Note: the ratio will only be updated when the stream is active, meaning a
379 // paused stream won't get any ratio updates. This might lead to waiting a bit
380 // longer than necessary if the network condition improves, but this is to
381 // avoid too much toggling.
382 if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0)
383 min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio);
384
mflodman101f2502016-06-09 17:21:19 +0200385 return min_bitrate;
386}
387
388void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
389 bool include_zero_allocations,
390 int max_multiplier,
391 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700392 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200393 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
394
395 ObserverSortingMap list_max_bitrates;
396 for (const auto& observer_config : bitrate_observer_configs_) {
397 if (include_zero_allocations ||
398 allocation->at(observer_config.observer) != 0) {
399 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
400 observer_config.max_bitrate_bps, &observer_config));
401 }
402 }
403 auto it = list_max_bitrates.begin();
404 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800405 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200406 uint32_t extra_allocation =
407 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
408 uint32_t total_allocation =
409 extra_allocation + allocation->at(it->second->observer);
410 bitrate -= extra_allocation;
411 if (total_allocation > max_multiplier * it->first) {
412 // There is more than we can fit for this observer, carry over to the
413 // remaining observers.
414 bitrate += total_allocation - max_multiplier * it->first;
415 total_allocation = max_multiplier * it->first;
416 }
417 // Finally, update the allocation for this observer.
418 allocation->at(it->second->observer) = total_allocation;
419 it = list_max_bitrates.erase(it);
420 }
421}
422
423bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
424 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700425 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200426 if (bitrate < sum_min_bitrates)
427 return false;
428
perkj26091b12016-09-01 01:17:40 -0700429 uint32_t extra_bitrate_per_observer =
430 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200431 static_cast<uint32_t>(bitrate_observer_configs_.size());
432 for (const auto& observer_config : bitrate_observer_configs_) {
433 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
philipel5ef2bc12017-02-21 07:28:31 -0800434 MinBitrateWithHysteresis(observer_config)) {
mflodman101f2502016-06-09 17:21:19 +0200435 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800436 }
mflodman101f2502016-06-09 17:21:19 +0200437 }
438 return true;
439}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000440} // namespace webrtc