blob: b379f7f0868ec189e30760f8d8b143f8afadbfcb [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>
15#include <utility>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/bitrate_controller/include/bitrate_controller.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
20#include "system_wrappers/include/clock.h"
21#include "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,
minyue93e45222017-05-18 14:32:41 -070071 int64_t bwe_period_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;
minyue93e45222017-05-18 14:32:41 -070078 last_bwe_period_ms_ = bwe_period_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_,
minyue93e45222017-05-18 14:32:41 -070093 last_bwe_period_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,
Alex Narestb3944f02017-10-13 14:56:18 +0200127 bool enforce_min_bitrate,
128 std::string track_id) {
perkj26091b12016-09-01 01:17:40 -0700129 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700130 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000131
mflodman101f2502016-06-09 17:21:19 +0200132 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700133 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700134 it->min_bitrate_bps = min_bitrate_bps;
135 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700136 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200137 it->enforce_min_bitrate = enforce_min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000138 } else {
perkj71ee44c2016-06-15 00:47:53 -0700139 bitrate_observer_configs_.push_back(
140 ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200141 pad_up_bitrate_bps, enforce_min_bitrate, track_id));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000142 }
Stefan Holmere5904162015-03-26 11:11:06 +0100143
mflodman101f2502016-06-09 17:21:19 +0200144 ObserverAllocation allocation;
145 if (last_bitrate_bps_ > 0) {
146 // Calculate a new allocation and update all observers.
147 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200148 for (auto& config : bitrate_observer_configs_) {
149 uint32_t allocated_bitrate = allocation[config.observer];
150 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800151 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700152 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200153 config.allocated_bitrate_bps = allocated_bitrate;
154 if (allocated_bitrate > 0)
155 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
156 }
perkjfea93092016-05-14 00:58:48 -0700157 } else {
158 // Currently, an encoder is not allowed to produce frames.
159 // But we still have to return the initial config bitrate + let the
160 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200161 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800162 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700163 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100164 }
perkj71ee44c2016-06-15 00:47:53 -0700165 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000166}
167
perkj71ee44c2016-06-15 00:47:53 -0700168void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700169 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700170 uint32_t total_requested_padding_bitrate = 0;
171 uint32_t total_requested_min_bitrate = 0;
172
perkj26091b12016-09-01 01:17:40 -0700173 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800174 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700175 if (config.enforce_min_bitrate) {
176 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800177 } else if (config.allocated_bitrate_bps == 0) {
178 stream_padding =
179 std::max(MinBitrateWithHysteresis(config), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700180 }
philipel5ef2bc12017-02-21 07:28:31 -0800181 total_requested_padding_bitrate += stream_padding;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000182 }
perkj71ee44c2016-06-15 00:47:53 -0700183
philipel5ef2bc12017-02-21 07:28:31 -0800184 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
185 total_requested_min_bitrate == total_requested_min_bitrate_) {
186 return;
187 }
188
189 total_requested_min_bitrate_ = total_requested_min_bitrate;
190 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
191
perkj9b522f82016-07-07 00:36:28 -0700192 LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
193 << total_requested_min_bitrate
194 << "bps, total_requested_padding_bitrate: "
195 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700196 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
197 total_requested_padding_bitrate);
198}
199
200void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700201 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
202 auto it = FindObserverConfig(observer);
203 if (it != bitrate_observer_configs_.end()) {
204 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700205 }
perkj26091b12016-09-01 01:17:40 -0700206
perkj71ee44c2016-06-15 00:47:53 -0700207 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000208}
209
perkj57c21f92016-06-17 07:27:16 -0700210int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700211 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200212 const auto& it = FindObserverConfig(observer);
213 if (it == bitrate_observer_configs_.end()) {
214 // This observer hasn't been added yet, just give it its fair share.
215 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700216 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200217 } else if (it->allocated_bitrate_bps == -1) {
218 // This observer hasn't received an allocation yet, so do the same.
219 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700220 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200221 } else {
222 // This observer already has an allocation.
223 return it->allocated_bitrate_bps;
224 }
perkj57c21f92016-06-17 07:27:16 -0700225}
226
mflodman48a4beb2016-07-01 13:03:59 +0200227BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700228BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
229 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700230 for (auto it = bitrate_observer_configs_.begin();
231 it != bitrate_observer_configs_.end(); ++it) {
232 if (it->observer == observer)
233 return it;
234 }
235 return bitrate_observer_configs_.end();
236}
237
perkjfea93092016-05-14 00:58:48 -0700238BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
239 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700240 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700241 if (bitrate_observer_configs_.empty())
242 return ObserverAllocation();
243
perkjfea93092016-05-14 00:58:48 -0700244 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700245 return ZeroRateAllocation();
246
247 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200248 uint32_t sum_max_bitrates = 0;
249 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700250 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200251 sum_max_bitrates += observer_config.max_bitrate_bps;
252 }
253
254 // Not enough for all observers to get an allocation, allocate according to:
255 // enforced min bitrate -> allocated bitrate previous round -> restart paused
256 // streams.
257 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700258 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700259
mflodman101f2502016-06-09 17:21:19 +0200260 // All observers will get their min bitrate plus an even share of the rest.
261 if (bitrate <= sum_max_bitrates)
262 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700263
mflodman101f2502016-06-09 17:21:19 +0200264 // All observers will get up to kTransmissionMaxBitrateMultiplier x max.
265 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000266}
267
mflodman2ebe5b12016-05-13 01:43:51 -0700268BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700269 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700270 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700271 for (const auto& observer_config : bitrate_observer_configs_)
272 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700273 return allocation;
274}
275
mflodman2ebe5b12016-05-13 01:43:51 -0700276BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100277 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700278 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700279 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200280 // Start by allocating bitrate to observers enforcing a min bitrate, hence
281 // remaining_bitrate might turn negative.
282 int64_t remaining_bitrate = bitrate;
283 for (const auto& observer_config : bitrate_observer_configs_) {
284 int32_t allocated_bitrate = 0;
285 if (observer_config.enforce_min_bitrate)
286 allocated_bitrate = observer_config.min_bitrate_bps;
287
288 allocation[observer_config.observer] = allocated_bitrate;
289 remaining_bitrate -= allocated_bitrate;
290 }
291
292 // Allocate bitrate to all previously active streams.
293 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700294 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200295 if (observer_config.enforce_min_bitrate ||
296 LastAllocatedBitrate(observer_config) == 0)
297 continue;
298
mflodman48a4beb2016-07-01 13:03:59 +0200299 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
300 if (remaining_bitrate >= required_bitrate) {
301 allocation[observer_config.observer] = required_bitrate;
302 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200303 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000304 }
305 }
mflodman101f2502016-06-09 17:21:19 +0200306
307 // Allocate bitrate to previously paused streams.
308 if (remaining_bitrate > 0) {
309 for (const auto& observer_config : bitrate_observer_configs_) {
310 if (LastAllocatedBitrate(observer_config) != 0)
311 continue;
312
313 // Add a hysteresis to avoid toggling.
314 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
315 if (remaining_bitrate >= required_bitrate) {
316 allocation[observer_config.observer] = required_bitrate;
317 remaining_bitrate -= required_bitrate;
318 }
319 }
320 }
321
322 // Split a possible remainder evenly on all streams with an allocation.
323 if (remaining_bitrate > 0)
324 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
325
326 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100327 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000328}
mflodman101f2502016-06-09 17:21:19 +0200329
330BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
331 uint32_t bitrate,
332 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700333 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200334 ObserverAllocation allocation;
335 for (const auto& observer_config : bitrate_observer_configs_)
336 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
337
338 bitrate -= sum_min_bitrates;
339 if (bitrate > 0)
340 DistributeBitrateEvenly(bitrate, true, 1, &allocation);
341
342 return allocation;
343}
344
345BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700346 uint32_t bitrate,
347 uint32_t sum_max_bitrates) {
348 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200349 ObserverAllocation allocation;
350
351 for (const auto& observer_config : bitrate_observer_configs_) {
352 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
353 bitrate -= observer_config.max_bitrate_bps;
354 }
355 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier,
356 &allocation);
357 return allocation;
358}
359
360uint32_t BitrateAllocator::LastAllocatedBitrate(
361 const ObserverConfig& observer_config) {
mflodman101f2502016-06-09 17:21:19 +0200362 // Return the configured minimum bitrate for newly added observers, to avoid
363 // requiring an extra high bitrate for the observer to get an allocated
364 // bitrate.
perkj26091b12016-09-01 01:17:40 -0700365 return observer_config.allocated_bitrate_bps == -1
366 ? observer_config.min_bitrate_bps
367 : observer_config.allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200368}
369
370uint32_t BitrateAllocator::MinBitrateWithHysteresis(
371 const ObserverConfig& observer_config) {
372 uint32_t min_bitrate = observer_config.min_bitrate_bps;
373 if (LastAllocatedBitrate(observer_config) == 0) {
374 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
375 kMinToggleBitrateBps);
376 }
mflodman48a4beb2016-07-01 13:03:59 +0200377 // Account for protection bitrate used by this observer in the previous
378 // allocation.
379 // Note: the ratio will only be updated when the stream is active, meaning a
380 // paused stream won't get any ratio updates. This might lead to waiting a bit
381 // longer than necessary if the network condition improves, but this is to
382 // avoid too much toggling.
383 if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0)
384 min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio);
385
mflodman101f2502016-06-09 17:21:19 +0200386 return min_bitrate;
387}
388
389void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
390 bool include_zero_allocations,
391 int max_multiplier,
392 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700393 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200394 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
395
396 ObserverSortingMap list_max_bitrates;
397 for (const auto& observer_config : bitrate_observer_configs_) {
398 if (include_zero_allocations ||
399 allocation->at(observer_config.observer) != 0) {
400 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
401 observer_config.max_bitrate_bps, &observer_config));
402 }
403 }
404 auto it = list_max_bitrates.begin();
405 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800406 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200407 uint32_t extra_allocation =
408 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
409 uint32_t total_allocation =
410 extra_allocation + allocation->at(it->second->observer);
411 bitrate -= extra_allocation;
412 if (total_allocation > max_multiplier * it->first) {
413 // There is more than we can fit for this observer, carry over to the
414 // remaining observers.
415 bitrate += total_allocation - max_multiplier * it->first;
416 total_allocation = max_multiplier * it->first;
417 }
418 // Finally, update the allocation for this observer.
419 allocation->at(it->second->observer) = total_allocation;
420 it = list_max_bitrates.erase(it);
421 }
422}
423
424bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
425 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700426 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200427 if (bitrate < sum_min_bitrates)
428 return false;
429
perkj26091b12016-09-01 01:17:40 -0700430 uint32_t extra_bitrate_per_observer =
431 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200432 static_cast<uint32_t>(bitrate_observer_configs_.size());
433 for (const auto& observer_config : bitrate_observer_configs_) {
434 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
philipel5ef2bc12017-02-21 07:28:31 -0800435 MinBitrateWithHysteresis(observer_config)) {
mflodman101f2502016-06-09 17:21:19 +0200436 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800437 }
mflodman101f2502016-06-09 17:21:19 +0200438 }
439 return true;
440}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000441} // namespace webrtc