blob: 2692c006671ed36d3971caa6aebbd149d3485b15 [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 Narest78609d52017-10-20 10:37:47 +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 Narest78609d52017-10-20 10:37:47 +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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010085 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +020086 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;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100104 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
105 << " with configured min bitrate "
106 << config.min_bitrate_bps << " and current estimate of "
107 << target_bitrate_bps << " and protection bitrate "
108 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200109 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
110 if (target_bitrate_bps > 0)
111 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100112 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
113 << ", configured min bitrate " << config.min_bitrate_bps
114 << ", current allocation " << allocated_bitrate
115 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200116 }
117
118 // Only update the media ratio if the observer got an allocation.
119 if (allocated_bitrate > 0)
120 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
121 config.allocated_bitrate_bps = allocated_bitrate;
122 }
philipel5ef2bc12017-02-21 07:28:31 -0800123 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100124}
125
perkj57c21f92016-06-17 07:27:16 -0700126void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
127 uint32_t min_bitrate_bps,
128 uint32_t max_bitrate_bps,
129 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200130 bool enforce_min_bitrate,
131 std::string track_id) {
perkj26091b12016-09-01 01:17:40 -0700132 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700133 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000134
mflodman101f2502016-06-09 17:21:19 +0200135 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700136 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700137 it->min_bitrate_bps = min_bitrate_bps;
138 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700139 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200140 it->enforce_min_bitrate = enforce_min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000141 } else {
perkj71ee44c2016-06-15 00:47:53 -0700142 bitrate_observer_configs_.push_back(
143 ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200144 pad_up_bitrate_bps, enforce_min_bitrate, track_id));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000145 }
Stefan Holmere5904162015-03-26 11:11:06 +0100146
mflodman101f2502016-06-09 17:21:19 +0200147 ObserverAllocation allocation;
148 if (last_bitrate_bps_ > 0) {
149 // Calculate a new allocation and update all observers.
150 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200151 for (auto& config : bitrate_observer_configs_) {
152 uint32_t allocated_bitrate = allocation[config.observer];
153 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800154 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700155 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200156 config.allocated_bitrate_bps = allocated_bitrate;
157 if (allocated_bitrate > 0)
158 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
159 }
perkjfea93092016-05-14 00:58:48 -0700160 } else {
161 // Currently, an encoder is not allowed to produce frames.
162 // But we still have to return the initial config bitrate + let the
163 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200164 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800165 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700166 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100167 }
perkj71ee44c2016-06-15 00:47:53 -0700168 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000169}
170
perkj71ee44c2016-06-15 00:47:53 -0700171void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700172 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700173 uint32_t total_requested_padding_bitrate = 0;
174 uint32_t total_requested_min_bitrate = 0;
175
perkj26091b12016-09-01 01:17:40 -0700176 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800177 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700178 if (config.enforce_min_bitrate) {
179 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800180 } else if (config.allocated_bitrate_bps == 0) {
181 stream_padding =
182 std::max(MinBitrateWithHysteresis(config), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700183 }
philipel5ef2bc12017-02-21 07:28:31 -0800184 total_requested_padding_bitrate += stream_padding;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000185 }
perkj71ee44c2016-06-15 00:47:53 -0700186
philipel5ef2bc12017-02-21 07:28:31 -0800187 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
188 total_requested_min_bitrate == total_requested_min_bitrate_) {
189 return;
190 }
191
192 total_requested_min_bitrate_ = total_requested_min_bitrate;
193 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
194
Mirko Bonadei675513b2017-11-09 11:09:25 +0100195 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
196 << total_requested_min_bitrate
197 << "bps, total_requested_padding_bitrate: "
198 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700199 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
200 total_requested_padding_bitrate);
201}
202
203void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700204 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200205
perkj26091b12016-09-01 01:17:40 -0700206 auto it = FindObserverConfig(observer);
207 if (it != bitrate_observer_configs_.end()) {
208 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700209 }
perkj26091b12016-09-01 01:17:40 -0700210
perkj71ee44c2016-06-15 00:47:53 -0700211 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000212}
213
perkj57c21f92016-06-17 07:27:16 -0700214int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700215 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200216 const auto& it = FindObserverConfig(observer);
217 if (it == bitrate_observer_configs_.end()) {
218 // This observer hasn't been added yet, just give it its fair share.
219 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700220 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200221 } else if (it->allocated_bitrate_bps == -1) {
222 // This observer hasn't received an allocation yet, so do the same.
223 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700224 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200225 } else {
226 // This observer already has an allocation.
227 return it->allocated_bitrate_bps;
228 }
perkj57c21f92016-06-17 07:27:16 -0700229}
230
Alex Narest78609d52017-10-20 10:37:47 +0200231void BitrateAllocator::SetBitrateAllocationStrategy(
232 std::unique_ptr<rtc::BitrateAllocationStrategy>
233 bitrate_allocation_strategy) {
234 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
235 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
236}
237
mflodman48a4beb2016-07-01 13:03:59 +0200238BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700239BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
240 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700241 for (auto it = bitrate_observer_configs_.begin();
242 it != bitrate_observer_configs_.end(); ++it) {
243 if (it->observer == observer)
244 return it;
245 }
246 return bitrate_observer_configs_.end();
247}
248
perkjfea93092016-05-14 00:58:48 -0700249BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
250 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700251 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700252 if (bitrate_observer_configs_.empty())
253 return ObserverAllocation();
254
Alex Narest78609d52017-10-20 10:37:47 +0200255 if (bitrate_allocation_strategy_ != nullptr) {
256 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
257 track_configs(bitrate_observer_configs_.size());
258 int i = 0;
259 for (const auto& c : bitrate_observer_configs_) {
260 track_configs[i++] = &c;
261 }
262 std::vector<uint32_t> track_allocations =
263 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
264 // The strategy should return allocation for all tracks.
265 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
266 ObserverAllocation allocation;
267 auto track_allocations_it = track_allocations.begin();
268 for (const auto& observer_config : bitrate_observer_configs_) {
269 allocation[observer_config.observer] = *track_allocations_it++;
270 }
271 return allocation;
272 }
273
perkjfea93092016-05-14 00:58:48 -0700274 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700275 return ZeroRateAllocation();
276
277 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200278 uint32_t sum_max_bitrates = 0;
279 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700280 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200281 sum_max_bitrates += observer_config.max_bitrate_bps;
282 }
283
284 // Not enough for all observers to get an allocation, allocate according to:
285 // enforced min bitrate -> allocated bitrate previous round -> restart paused
286 // streams.
287 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700288 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700289
mflodman101f2502016-06-09 17:21:19 +0200290 // All observers will get their min bitrate plus an even share of the rest.
291 if (bitrate <= sum_max_bitrates)
292 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700293
mflodman101f2502016-06-09 17:21:19 +0200294 // All observers will get up to kTransmissionMaxBitrateMultiplier x max.
295 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000296}
297
mflodman2ebe5b12016-05-13 01:43:51 -0700298BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700299 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700300 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700301 for (const auto& observer_config : bitrate_observer_configs_)
302 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700303 return allocation;
304}
305
mflodman2ebe5b12016-05-13 01:43:51 -0700306BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100307 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700308 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700309 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200310 // Start by allocating bitrate to observers enforcing a min bitrate, hence
311 // remaining_bitrate might turn negative.
312 int64_t remaining_bitrate = bitrate;
313 for (const auto& observer_config : bitrate_observer_configs_) {
314 int32_t allocated_bitrate = 0;
315 if (observer_config.enforce_min_bitrate)
316 allocated_bitrate = observer_config.min_bitrate_bps;
317
318 allocation[observer_config.observer] = allocated_bitrate;
319 remaining_bitrate -= allocated_bitrate;
320 }
321
322 // Allocate bitrate to all previously active streams.
323 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700324 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200325 if (observer_config.enforce_min_bitrate ||
326 LastAllocatedBitrate(observer_config) == 0)
327 continue;
328
mflodman48a4beb2016-07-01 13:03:59 +0200329 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
330 if (remaining_bitrate >= required_bitrate) {
331 allocation[observer_config.observer] = required_bitrate;
332 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200333 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000334 }
335 }
mflodman101f2502016-06-09 17:21:19 +0200336
337 // Allocate bitrate to previously paused streams.
338 if (remaining_bitrate > 0) {
339 for (const auto& observer_config : bitrate_observer_configs_) {
340 if (LastAllocatedBitrate(observer_config) != 0)
341 continue;
342
343 // Add a hysteresis to avoid toggling.
344 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
345 if (remaining_bitrate >= required_bitrate) {
346 allocation[observer_config.observer] = required_bitrate;
347 remaining_bitrate -= required_bitrate;
348 }
349 }
350 }
351
352 // Split a possible remainder evenly on all streams with an allocation.
353 if (remaining_bitrate > 0)
354 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
355
356 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100357 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000358}
mflodman101f2502016-06-09 17:21:19 +0200359
360BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
361 uint32_t bitrate,
362 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700363 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200364 ObserverAllocation allocation;
365 for (const auto& observer_config : bitrate_observer_configs_)
366 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
367
368 bitrate -= sum_min_bitrates;
369 if (bitrate > 0)
370 DistributeBitrateEvenly(bitrate, true, 1, &allocation);
371
372 return allocation;
373}
374
375BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700376 uint32_t bitrate,
377 uint32_t sum_max_bitrates) {
378 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200379 ObserverAllocation allocation;
380
381 for (const auto& observer_config : bitrate_observer_configs_) {
382 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
383 bitrate -= observer_config.max_bitrate_bps;
384 }
385 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier,
386 &allocation);
387 return allocation;
388}
389
390uint32_t BitrateAllocator::LastAllocatedBitrate(
391 const ObserverConfig& observer_config) {
mflodman101f2502016-06-09 17:21:19 +0200392 // Return the configured minimum bitrate for newly added observers, to avoid
393 // requiring an extra high bitrate for the observer to get an allocated
394 // bitrate.
perkj26091b12016-09-01 01:17:40 -0700395 return observer_config.allocated_bitrate_bps == -1
396 ? observer_config.min_bitrate_bps
397 : observer_config.allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200398}
399
400uint32_t BitrateAllocator::MinBitrateWithHysteresis(
401 const ObserverConfig& observer_config) {
402 uint32_t min_bitrate = observer_config.min_bitrate_bps;
403 if (LastAllocatedBitrate(observer_config) == 0) {
404 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
405 kMinToggleBitrateBps);
406 }
mflodman48a4beb2016-07-01 13:03:59 +0200407 // Account for protection bitrate used by this observer in the previous
408 // allocation.
409 // Note: the ratio will only be updated when the stream is active, meaning a
410 // paused stream won't get any ratio updates. This might lead to waiting a bit
411 // longer than necessary if the network condition improves, but this is to
412 // avoid too much toggling.
413 if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0)
414 min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio);
415
mflodman101f2502016-06-09 17:21:19 +0200416 return min_bitrate;
417}
418
419void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
420 bool include_zero_allocations,
421 int max_multiplier,
422 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700423 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200424 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
425
426 ObserverSortingMap list_max_bitrates;
427 for (const auto& observer_config : bitrate_observer_configs_) {
428 if (include_zero_allocations ||
429 allocation->at(observer_config.observer) != 0) {
430 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
431 observer_config.max_bitrate_bps, &observer_config));
432 }
433 }
434 auto it = list_max_bitrates.begin();
435 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800436 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200437 uint32_t extra_allocation =
438 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
439 uint32_t total_allocation =
440 extra_allocation + allocation->at(it->second->observer);
441 bitrate -= extra_allocation;
442 if (total_allocation > max_multiplier * it->first) {
443 // There is more than we can fit for this observer, carry over to the
444 // remaining observers.
445 bitrate += total_allocation - max_multiplier * it->first;
446 total_allocation = max_multiplier * it->first;
447 }
448 // Finally, update the allocation for this observer.
449 allocation->at(it->second->observer) = total_allocation;
450 it = list_max_bitrates.erase(it);
451 }
452}
453
454bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
455 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700456 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200457 if (bitrate < sum_min_bitrates)
458 return false;
459
perkj26091b12016-09-01 01:17:40 -0700460 uint32_t extra_bitrate_per_observer =
461 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200462 static_cast<uint32_t>(bitrate_observer_configs_.size());
463 for (const auto& observer_config : bitrate_observer_configs_) {
464 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
philipel5ef2bc12017-02-21 07:28:31 -0800465 MinBitrateWithHysteresis(observer_config)) {
mflodman101f2502016-06-09 17:21:19 +0200466 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800467 }
mflodman101f2502016-06-09 17:21:19 +0200468 }
469 return true;
470}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000471} // namespace webrtc