blob: c1247746328434de0edabc21f120f8d7910b7012 [file] [log] [blame]
stefan@webrtc.org792f1a12015-03-04 12:24:261/*
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 04:47:3112#include "call/bitrate_allocator.h"
stefan@webrtc.org792f1a12015-03-04 12:24:2613
14#include <algorithm>
Seth Hampsonfe73d6a2017-11-14 18:49:0615#include <cmath>
Alex Narest78609d52017-10-20 08:37:4716#include <memory>
stefan@webrtc.org792f1a12015-03-04 12:24:2617#include <utility>
18
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "modules/bitrate_controller/include/bitrate_controller.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
22#include "system_wrappers/include/clock.h"
Ying Wanga646d302018-03-02 16:04:1123#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3124#include "system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:2625
26namespace webrtc {
27
Stefan Holmere5904162015-03-26 10:11:0628// Allow packets to be transmitted in up to 2 times max video bitrate if the
29// bandwidth estimate allows it.
Ying Wanga646d302018-03-02 16:04:1130const uint8_t kTransmissionMaxBitrateMultiplier = 2;
Stefan Holmere5904162015-03-26 10:11:0631const int kDefaultBitrateBps = 300000;
32
mflodman101f2502016-06-09 15:21:1933// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
34const double kToggleFactor = 0.1;
35const uint32_t kMinToggleBitrateBps = 20000;
36
mflodman48a4beb2016-07-01 11:03:5937const int64_t kBweLogIntervalMs = 5000;
38
39namespace {
40
41double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
kwibergaf476c72016-11-28 23:21:3942 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 11:03:5943 if (protection_bitrate == 0)
44 return 1.0;
45
46 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
47 return media_bitrate / static_cast<double>(allocated_bitrate);
48}
49} // namespace
50
perkj71ee44c2016-06-15 07:47:5351BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
52 : limit_observer_(limit_observer),
Sergey Ulanove2b15012016-11-23 00:08:3053 last_bitrate_bps_(0),
perkjfea93092016-05-14 07:58:4854 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 10:11:0655 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 11:03:5956 last_rtt_(0),
57 num_pause_events_(0),
58 clock_(Clock::GetRealTimeClock()),
philipel5ef2bc12017-02-21 15:28:3159 last_bwe_log_time_(0),
60 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 08:37:4761 total_requested_min_bitrate_(0),
Ying Wanga646d302018-03-02 16:04:1162 bitrate_allocation_strategy_(nullptr),
63 transmission_max_bitrate_multiplier_(
64 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 08:17:4065 sequenced_checker_.Detach();
66}
mflodman48a4beb2016-07-01 11:03:5967
68BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-10 05:40:2569 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
70 num_pause_events_);
mflodman48a4beb2016-07-01 11:03:5971}
stefan@webrtc.org792f1a12015-03-04 12:24:2672
Ying Wanga646d302018-03-02 16:04:1173uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
74 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
75 "WebRTC-TransmissionMaxBitrateMultiplier")
76 .c_str(),
77 nullptr, 10);
78 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
79 return static_cast<uint8_t>(multiplier);
80 }
81 return kTransmissionMaxBitrateMultiplier;
82}
83
perkj71ee44c2016-06-15 07:47:5384void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
85 uint8_t fraction_loss,
minyue78b4d562016-11-30 12:47:3986 int64_t rtt,
minyue93e45222017-05-18 21:32:4187 int64_t bwe_period_ms) {
perkj26091b12016-09-01 08:17:4088 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 15:21:1989 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 07:58:4890 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 15:21:1991 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 10:11:0692 last_fraction_loss_ = fraction_loss;
93 last_rtt_ = rtt;
minyue93e45222017-05-18 21:32:4194 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 08:43:5195
mflodman48a4beb2016-07-01 11:03:5996 // Periodically log the incoming BWE.
97 int64_t now = clock_->TimeInMilliseconds();
98 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 10:09:2599 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 11:03:59100 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 12:25:49101 }
mflodman48a4beb2016-07-01 11:03:59102
103 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
104
105 for (auto& config : bitrate_observer_configs_) {
106 uint32_t allocated_bitrate = allocation[config.observer];
107 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 12:47:39108 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 21:32:41109 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 11:03:59110
111 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
112 if (target_bitrate_bps > 0)
113 ++num_pause_events_;
114 // The protection bitrate is an estimate based on the ratio between media
115 // and protection used before this observer was muted.
116 uint32_t predicted_protection_bps =
117 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 10:09:25118 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
119 << " with configured min bitrate "
120 << config.min_bitrate_bps << " and current estimate of "
121 << target_bitrate_bps << " and protection bitrate "
122 << predicted_protection_bps;
mflodman48a4beb2016-07-01 11:03:59123 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
124 if (target_bitrate_bps > 0)
125 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 10:09:25126 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
127 << ", configured min bitrate " << config.min_bitrate_bps
128 << ", current allocation " << allocated_bitrate
129 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 11:03:59130 }
131
132 // Only update the media ratio if the observer got an allocation.
133 if (allocated_bitrate > 0)
134 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
135 config.allocated_bitrate_bps = allocated_bitrate;
136 }
philipel5ef2bc12017-02-21 15:28:31137 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 10:11:06138}
139
perkj57c21f92016-06-17 14:27:16140void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
141 uint32_t min_bitrate_bps,
142 uint32_t max_bitrate_bps,
143 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 12:56:18144 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 18:49:06145 std::string track_id,
146 double bitrate_priority) {
perkj26091b12016-09-01 08:17:40147 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 18:49:06148 RTC_DCHECK_GT(bitrate_priority, 0);
149 RTC_DCHECK(std::isnormal(bitrate_priority));
mflodman2ebe5b12016-05-13 08:43:51150 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26151
mflodman101f2502016-06-09 15:21:19152 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 08:43:51153 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 08:43:51154 it->min_bitrate_bps = min_bitrate_bps;
155 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 07:47:53156 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 15:21:19157 it->enforce_min_bitrate = enforce_min_bitrate;
Seth Hampsonfe73d6a2017-11-14 18:49:06158 it->bitrate_priority = bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26159 } else {
Seth Hampsonfe73d6a2017-11-14 18:49:06160 bitrate_observer_configs_.push_back(ObserverConfig(
161 observer, min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps,
162 enforce_min_bitrate, track_id, bitrate_priority));
stefan@webrtc.org792f1a12015-03-04 12:24:26163 }
Stefan Holmere5904162015-03-26 10:11:06164
mflodman101f2502016-06-09 15:21:19165 ObserverAllocation allocation;
166 if (last_bitrate_bps_ > 0) {
167 // Calculate a new allocation and update all observers.
168 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 11:03:59169 for (auto& config : bitrate_observer_configs_) {
170 uint32_t allocated_bitrate = allocation[config.observer];
171 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 12:47:39172 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 21:32:41173 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 11:03:59174 config.allocated_bitrate_bps = allocated_bitrate;
175 if (allocated_bitrate > 0)
176 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
177 }
perkjfea93092016-05-14 07:58:48178 } else {
179 // Currently, an encoder is not allowed to produce frames.
180 // But we still have to return the initial config bitrate + let the
181 // observer know that it can not produce frames.
mflodman101f2502016-06-09 15:21:19182 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 12:47:39183 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 21:32:41184 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 10:11:06185 }
perkj71ee44c2016-06-15 07:47:53186 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26187}
188
perkj71ee44c2016-06-15 07:47:53189void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 08:17:40190 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 07:47:53191 uint32_t total_requested_padding_bitrate = 0;
192 uint32_t total_requested_min_bitrate = 0;
philipelf69e7682018-02-28 12:06:28193 uint32_t total_requested_bitrate = 0;
perkj71ee44c2016-06-15 07:47:53194
perkj26091b12016-09-01 08:17:40195 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 15:28:31196 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 08:17:40197 if (config.enforce_min_bitrate) {
198 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 15:28:31199 } else if (config.allocated_bitrate_bps == 0) {
200 stream_padding =
srte1eb051c2017-11-29 10:23:59201 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 07:47:53202 }
philipel5ef2bc12017-02-21 15:28:31203 total_requested_padding_bitrate += stream_padding;
philipelf69e7682018-02-28 12:06:28204 total_requested_bitrate += config.max_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26205 }
perkj71ee44c2016-06-15 07:47:53206
philipel5ef2bc12017-02-21 15:28:31207 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
208 total_requested_min_bitrate == total_requested_min_bitrate_) {
209 return;
210 }
211
212 total_requested_min_bitrate_ = total_requested_min_bitrate;
213 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
214
Mirko Bonadei675513b2017-11-09 10:09:25215 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
216 << total_requested_min_bitrate
217 << "bps, total_requested_padding_bitrate: "
218 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 07:47:53219 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
philipelf69e7682018-02-28 12:06:28220 total_requested_padding_bitrate,
221 total_requested_bitrate);
perkj71ee44c2016-06-15 07:47:53222}
223
224void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 08:17:40225 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 08:37:47226
perkj26091b12016-09-01 08:17:40227 auto it = FindObserverConfig(observer);
228 if (it != bitrate_observer_configs_.end()) {
229 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 07:47:53230 }
perkj26091b12016-09-01 08:17:40231
perkj71ee44c2016-06-15 07:47:53232 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26233}
234
perkj57c21f92016-06-17 14:27:16235int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 08:17:40236 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 11:03:59237 const auto& it = FindObserverConfig(observer);
238 if (it == bitrate_observer_configs_.end()) {
239 // This observer hasn't been added yet, just give it its fair share.
240 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 08:17:40241 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 11:03:59242 } else if (it->allocated_bitrate_bps == -1) {
243 // This observer hasn't received an allocation yet, so do the same.
244 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 08:17:40245 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 11:03:59246 } else {
247 // This observer already has an allocation.
248 return it->allocated_bitrate_bps;
249 }
perkj57c21f92016-06-17 14:27:16250}
251
Alex Narest78609d52017-10-20 08:37:47252void BitrateAllocator::SetBitrateAllocationStrategy(
253 std::unique_ptr<rtc::BitrateAllocationStrategy>
254 bitrate_allocation_strategy) {
255 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
256 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
257}
258
mflodman48a4beb2016-07-01 11:03:59259BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 08:17:40260BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
261 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 08:43:51262 for (auto it = bitrate_observer_configs_.begin();
263 it != bitrate_observer_configs_.end(); ++it) {
264 if (it->observer == observer)
265 return it;
266 }
267 return bitrate_observer_configs_.end();
268}
269
perkjfea93092016-05-14 07:58:48270BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
271 uint32_t bitrate) {
perkj26091b12016-09-01 08:17:40272 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 08:43:51273 if (bitrate_observer_configs_.empty())
274 return ObserverAllocation();
275
Alex Narest78609d52017-10-20 08:37:47276 if (bitrate_allocation_strategy_ != nullptr) {
277 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
278 track_configs(bitrate_observer_configs_.size());
279 int i = 0;
280 for (const auto& c : bitrate_observer_configs_) {
281 track_configs[i++] = &c;
282 }
283 std::vector<uint32_t> track_allocations =
284 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
285 // The strategy should return allocation for all tracks.
286 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
287 ObserverAllocation allocation;
288 auto track_allocations_it = track_allocations.begin();
289 for (const auto& observer_config : bitrate_observer_configs_) {
290 allocation[observer_config.observer] = *track_allocations_it++;
291 }
292 return allocation;
293 }
294
perkjfea93092016-05-14 07:58:48295 if (bitrate == 0)
mflodman2ebe5b12016-05-13 08:43:51296 return ZeroRateAllocation();
297
298 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 15:21:19299 uint32_t sum_max_bitrates = 0;
300 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 08:43:51301 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 15:21:19302 sum_max_bitrates += observer_config.max_bitrate_bps;
303 }
304
305 // Not enough for all observers to get an allocation, allocate according to:
306 // enforced min bitrate -> allocated bitrate previous round -> restart paused
307 // streams.
308 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 07:58:48309 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 08:43:51310
Seth Hampsonfe73d6a2017-11-14 18:49:06311 // All observers will get their min bitrate plus a share of the rest. This
312 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 15:21:19313 if (bitrate <= sum_max_bitrates)
314 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 08:43:51315
Ying Wanga646d302018-03-02 16:04:11316 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 15:21:19317 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26318}
319
mflodman2ebe5b12016-05-13 08:43:51320BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 08:17:40321 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 08:43:51322 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 08:43:51323 for (const auto& observer_config : bitrate_observer_configs_)
324 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 13:01:13325 return allocation;
326}
327
mflodman2ebe5b12016-05-13 08:43:51328BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 10:11:06329 uint32_t bitrate) {
perkj26091b12016-09-01 08:17:40330 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 08:43:51331 ObserverAllocation allocation;
mflodman101f2502016-06-09 15:21:19332 // Start by allocating bitrate to observers enforcing a min bitrate, hence
333 // remaining_bitrate might turn negative.
334 int64_t remaining_bitrate = bitrate;
335 for (const auto& observer_config : bitrate_observer_configs_) {
336 int32_t allocated_bitrate = 0;
337 if (observer_config.enforce_min_bitrate)
338 allocated_bitrate = observer_config.min_bitrate_bps;
339
340 allocation[observer_config.observer] = allocated_bitrate;
341 remaining_bitrate -= allocated_bitrate;
342 }
343
344 // Allocate bitrate to all previously active streams.
345 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 08:43:51346 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 15:21:19347 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 10:23:59348 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 15:21:19349 continue;
350
srte1eb051c2017-11-29 10:23:59351 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 11:03:59352 if (remaining_bitrate >= required_bitrate) {
353 allocation[observer_config.observer] = required_bitrate;
354 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 15:21:19355 }
stefan@webrtc.org792f1a12015-03-04 12:24:26356 }
357 }
mflodman101f2502016-06-09 15:21:19358
359 // Allocate bitrate to previously paused streams.
360 if (remaining_bitrate > 0) {
361 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 10:23:59362 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 15:21:19363 continue;
364
365 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 10:23:59366 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 15:21:19367 if (remaining_bitrate >= required_bitrate) {
368 allocation[observer_config.observer] = required_bitrate;
369 remaining_bitrate -= required_bitrate;
370 }
371 }
372 }
373
374 // Split a possible remainder evenly on all streams with an allocation.
375 if (remaining_bitrate > 0)
376 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
377
378 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 10:11:06379 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26380}
mflodman101f2502016-06-09 15:21:19381
Seth Hampsonfe73d6a2017-11-14 18:49:06382// Allocates the bitrate based on the bitrate priority of each observer. This
383// bitrate priority defines the priority for bitrate to be allocated to that
384// observer in relation to other observers. For example with two observers, if
385// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
386// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
387// allocated twice the bitrate as observer 1 above the each observer's
388// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 15:21:19389BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
390 uint32_t bitrate,
391 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 08:17:40392 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 15:21:19393 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 18:49:06394 ObserverAllocation observers_capacities;
395 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 15:21:19396 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 18:49:06397 observers_capacities[observer_config.observer] =
398 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
399 }
mflodman101f2502016-06-09 15:21:19400
401 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 18:49:06402 // From the remaining bitrate, allocate a proportional amount to each observer
403 // above the min bitrate already allocated.
mflodman101f2502016-06-09 15:21:19404 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 18:49:06405 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 15:21:19406
407 return allocation;
408}
409
410BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 08:17:40411 uint32_t bitrate,
412 uint32_t sum_max_bitrates) {
413 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 15:21:19414 ObserverAllocation allocation;
415
416 for (const auto& observer_config : bitrate_observer_configs_) {
417 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
418 bitrate -= observer_config.max_bitrate_bps;
419 }
Ying Wanga646d302018-03-02 16:04:11420 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 15:21:19421 &allocation);
422 return allocation;
423}
424
srte1eb051c2017-11-29 10:23:59425uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 15:21:19426 // Return the configured minimum bitrate for newly added observers, to avoid
427 // requiring an extra high bitrate for the observer to get an allocated
428 // bitrate.
srte1eb051c2017-11-29 10:23:59429 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 15:21:19430}
431
srte1eb051c2017-11-29 10:23:59432uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
433 uint32_t min_bitrate = min_bitrate_bps;
434 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 15:21:19435 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
436 kMinToggleBitrateBps);
437 }
mflodman48a4beb2016-07-01 11:03:59438 // Account for protection bitrate used by this observer in the previous
439 // allocation.
440 // Note: the ratio will only be updated when the stream is active, meaning a
441 // paused stream won't get any ratio updates. This might lead to waiting a bit
442 // longer than necessary if the network condition improves, but this is to
443 // avoid too much toggling.
srte1eb051c2017-11-29 10:23:59444 if (media_ratio > 0.0 && media_ratio < 1.0)
445 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 11:03:59446
mflodman101f2502016-06-09 15:21:19447 return min_bitrate;
448}
449
450void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
451 bool include_zero_allocations,
452 int max_multiplier,
453 ObserverAllocation* allocation) {
perkj26091b12016-09-01 08:17:40454 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 15:21:19455 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
456
457 ObserverSortingMap list_max_bitrates;
458 for (const auto& observer_config : bitrate_observer_configs_) {
459 if (include_zero_allocations ||
460 allocation->at(observer_config.observer) != 0) {
461 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
462 observer_config.max_bitrate_bps, &observer_config));
463 }
464 }
465 auto it = list_max_bitrates.begin();
466 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 23:21:39467 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 15:21:19468 uint32_t extra_allocation =
469 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
470 uint32_t total_allocation =
471 extra_allocation + allocation->at(it->second->observer);
472 bitrate -= extra_allocation;
473 if (total_allocation > max_multiplier * it->first) {
474 // There is more than we can fit for this observer, carry over to the
475 // remaining observers.
476 bitrate += total_allocation - max_multiplier * it->first;
477 total_allocation = max_multiplier * it->first;
478 }
479 // Finally, update the allocation for this observer.
480 allocation->at(it->second->observer) = total_allocation;
481 it = list_max_bitrates.erase(it);
482 }
483}
484
485bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
486 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 08:17:40487 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 15:21:19488 if (bitrate < sum_min_bitrates)
489 return false;
490
perkj26091b12016-09-01 08:17:40491 uint32_t extra_bitrate_per_observer =
492 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 15:21:19493 static_cast<uint32_t>(bitrate_observer_configs_.size());
494 for (const auto& observer_config : bitrate_observer_configs_) {
495 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 10:23:59496 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 15:21:19497 return false;
philipel5ef2bc12017-02-21 15:28:31498 }
mflodman101f2502016-06-09 15:21:19499 }
500 return true;
501}
Seth Hampsonfe73d6a2017-11-14 18:49:06502
503void BitrateAllocator::DistributeBitrateRelatively(
504 uint32_t remaining_bitrate,
505 const ObserverAllocation& observers_capacities,
506 ObserverAllocation* allocation) {
507 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
508 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
509 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
510
511 struct PriorityRateObserverConfig {
512 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
513 uint32_t capacity_bps,
514 double bitrate_priority)
515 : allocation_key(allocation_key),
516 capacity_bps(capacity_bps),
517 bitrate_priority(bitrate_priority) {}
518
519 BitrateAllocatorObserver* allocation_key;
520 // The amount of bitrate bps that can be allocated to this observer.
521 uint32_t capacity_bps;
522 double bitrate_priority;
523
524 // We want to sort by which observers will be allocated their full capacity
525 // first. By dividing each observer's capacity by its bitrate priority we
526 // are "normalizing" the capacity of an observer by the rate it will be
527 // filled. This is because the amount allocated is based upon bitrate
528 // priority. We allocate twice as much bitrate to an observer with twice the
529 // bitrate priority of another.
530 bool operator<(const PriorityRateObserverConfig& other) const {
531 return capacity_bps / bitrate_priority <
532 other.capacity_bps / other.bitrate_priority;
533 }
534 };
535
536 double bitrate_priority_sum = 0;
537 std::vector<PriorityRateObserverConfig> priority_rate_observers;
538 for (const auto& observer_config : bitrate_observer_configs_) {
539 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
540 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
541 observer_config.bitrate_priority);
542 bitrate_priority_sum += observer_config.bitrate_priority;
543 }
544
545 // Iterate in the order observers can be allocated their full capacity.
546 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
547 size_t i;
548 for (i = 0; i < priority_rate_observers.size(); ++i) {
549 const auto& priority_rate_observer = priority_rate_observers[i];
550 // We allocate the full capacity to an observer only if its relative
551 // portion from the remaining bitrate is sufficient to allocate its full
552 // capacity. This means we aren't greedily allocating the full capacity, but
553 // that it is only done when there is also enough bitrate to allocate the
554 // proportional amounts to all other observers.
555 double observer_share =
556 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
557 double allocation_bps = observer_share * remaining_bitrate;
558 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
559 if (!enough_bitrate)
560 break;
561 allocation->at(priority_rate_observer.allocation_key) +=
562 priority_rate_observer.capacity_bps;
563 remaining_bitrate -= priority_rate_observer.capacity_bps;
564 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
565 }
566
567 // From the remaining bitrate, allocate the proportional amounts to the
568 // observers that aren't allocated their max capacity.
569 for (; i < priority_rate_observers.size(); ++i) {
570 const auto& priority_rate_observer = priority_rate_observers[i];
571 double fraction_allocated =
572 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
573 allocation->at(priority_rate_observer.allocation_key) +=
574 fraction_allocated * remaining_bitrate;
575 }
576}
577
stefan@webrtc.org792f1a12015-03-04 12:24:26578} // namespace webrtc