blob: 753fd7ef0c9fda39e8e1cfd81d7d7c41e12cca0b [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>
Seth Hampsonfe73d6a2017-11-14 10:49:06 -080015#include <cmath>
Alex Narest78609d52017-10-20 10:37:47 +020016#include <memory>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000017#include <utility>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#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 17:04:11 +010023#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000025
26namespace webrtc {
27
Stefan Holmere5904162015-03-26 11:11:06 +010028// Allow packets to be transmitted in up to 2 times max video bitrate if the
29// bandwidth estimate allows it.
Ying Wanga646d302018-03-02 17:04:11 +010030const uint8_t kTransmissionMaxBitrateMultiplier = 2;
Stefan Holmere5904162015-03-26 11:11:06 +010031const int kDefaultBitrateBps = 300000;
32
mflodman101f2502016-06-09 17:21:19 +020033// 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 13:03:59 +020037const int64_t kBweLogIntervalMs = 5000;
38
39namespace {
40
41double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
kwibergaf476c72016-11-28 15:21:39 -080042 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 13:03:59 +020043 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 00:47:53 -070051BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
52 : limit_observer_(limit_observer),
Sergey Ulanove2b15012016-11-22 16:08:30 -080053 last_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -070054 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010055 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020056 last_rtt_(0),
57 num_pause_events_(0),
58 clock_(Clock::GetRealTimeClock()),
philipel5ef2bc12017-02-21 07:28:31 -080059 last_bwe_log_time_(0),
60 total_requested_padding_bitrate_(0),
Alex Narest78609d52017-10-20 10:37:47 +020061 total_requested_min_bitrate_(0),
Sebastian Jansson448f4d52018-04-04 14:52:07 +020062 total_requested_max_bitrate_(0),
Sebastian Jansson29b204e2018-03-21 12:45:27 +010063 has_packet_feedback_(false),
Ying Wanga646d302018-03-02 17:04:11 +010064 bitrate_allocation_strategy_(nullptr),
65 transmission_max_bitrate_multiplier_(
66 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070067 sequenced_checker_.Detach();
68}
mflodman48a4beb2016-07-01 13:03:59 +020069
70BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070071 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
72 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020073}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000074
Ying Wanga646d302018-03-02 17:04:11 +010075uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
76 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
77 "WebRTC-TransmissionMaxBitrateMultiplier")
78 .c_str(),
79 nullptr, 10);
80 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010081 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
82 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010083 return static_cast<uint8_t>(multiplier);
84 }
85 return kTransmissionMaxBitrateMultiplier;
86}
87
perkj71ee44c2016-06-15 00:47:53 -070088void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
89 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080090 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070091 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070092 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020093 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070094 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020095 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010096 last_fraction_loss_ = fraction_loss;
97 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -070098 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -070099
mflodman48a4beb2016-07-01 13:03:59 +0200100 // Periodically log the incoming BWE.
101 int64_t now = clock_->TimeInMilliseconds();
102 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100103 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200104 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800105 }
mflodman48a4beb2016-07-01 13:03:59 +0200106
107 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
108
109 for (auto& config : bitrate_observer_configs_) {
110 uint32_t allocated_bitrate = allocation[config.observer];
111 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800112 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700113 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200114
115 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
116 if (target_bitrate_bps > 0)
117 ++num_pause_events_;
118 // The protection bitrate is an estimate based on the ratio between media
119 // and protection used before this observer was muted.
120 uint32_t predicted_protection_bps =
121 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100122 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
123 << " with configured min bitrate "
124 << config.min_bitrate_bps << " and current estimate of "
125 << target_bitrate_bps << " and protection bitrate "
126 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200127 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
128 if (target_bitrate_bps > 0)
129 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100130 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
131 << ", configured min bitrate " << config.min_bitrate_bps
132 << ", current allocation " << allocated_bitrate
133 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200134 }
135
136 // Only update the media ratio if the observer got an allocation.
137 if (allocated_bitrate > 0)
138 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
139 config.allocated_bitrate_bps = allocated_bitrate;
140 }
philipel5ef2bc12017-02-21 07:28:31 -0800141 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100142}
143
perkj57c21f92016-06-17 07:27:16 -0700144void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
145 uint32_t min_bitrate_bps,
146 uint32_t max_bitrate_bps,
147 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200148 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800149 std::string track_id,
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100150 double bitrate_priority,
151 bool has_packet_feedback) {
perkj26091b12016-09-01 01:17:40 -0700152 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800153 RTC_DCHECK_GT(bitrate_priority, 0);
154 RTC_DCHECK(std::isnormal(bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700155 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000156
mflodman101f2502016-06-09 17:21:19 +0200157 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700158 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700159 it->min_bitrate_bps = min_bitrate_bps;
160 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700161 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200162 it->enforce_min_bitrate = enforce_min_bitrate;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800163 it->bitrate_priority = bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000164 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800165 bitrate_observer_configs_.push_back(ObserverConfig(
166 observer, min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps,
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100167 enforce_min_bitrate, track_id, bitrate_priority, has_packet_feedback));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000168 }
Stefan Holmere5904162015-03-26 11:11:06 +0100169
mflodman101f2502016-06-09 17:21:19 +0200170 ObserverAllocation allocation;
171 if (last_bitrate_bps_ > 0) {
172 // Calculate a new allocation and update all observers.
173 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200174 for (auto& config : bitrate_observer_configs_) {
175 uint32_t allocated_bitrate = allocation[config.observer];
176 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800177 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700178 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200179 config.allocated_bitrate_bps = allocated_bitrate;
180 if (allocated_bitrate > 0)
181 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
182 }
perkjfea93092016-05-14 00:58:48 -0700183 } else {
184 // Currently, an encoder is not allowed to produce frames.
185 // But we still have to return the initial config bitrate + let the
186 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200187 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800188 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700189 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100190 }
perkj71ee44c2016-06-15 00:47:53 -0700191 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000192}
193
perkj71ee44c2016-06-15 00:47:53 -0700194void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700195 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700196 uint32_t total_requested_padding_bitrate = 0;
197 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200198 uint32_t total_requested_max_bitrate = 0;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100199 bool has_packet_feedback = false;
perkj26091b12016-09-01 01:17:40 -0700200 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800201 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700202 if (config.enforce_min_bitrate) {
203 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800204 } else if (config.allocated_bitrate_bps == 0) {
205 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100206 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700207 }
philipel5ef2bc12017-02-21 07:28:31 -0800208 total_requested_padding_bitrate += stream_padding;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200209 total_requested_max_bitrate += config.max_bitrate_bps;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100210 if (config.allocated_bitrate_bps > 0 && config.has_packet_feedback)
211 has_packet_feedback = true;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000212 }
perkj71ee44c2016-06-15 00:47:53 -0700213
philipel5ef2bc12017-02-21 07:28:31 -0800214 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100215 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200216 total_requested_max_bitrate == total_requested_max_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100217 has_packet_feedback == has_packet_feedback_) {
philipel5ef2bc12017-02-21 07:28:31 -0800218 return;
219 }
220
221 total_requested_min_bitrate_ = total_requested_min_bitrate;
222 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200223 total_requested_max_bitrate_ = total_requested_max_bitrate;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100224 has_packet_feedback_ = has_packet_feedback;
philipel5ef2bc12017-02-21 07:28:31 -0800225
Mirko Bonadei675513b2017-11-09 11:09:25 +0100226 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
227 << total_requested_min_bitrate
228 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200229 << total_requested_padding_bitrate
230 << "bps, total_requested_max_bitrate: "
231 << total_requested_max_bitrate << "bps";
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100232 limit_observer_->OnAllocationLimitsChanged(
233 total_requested_min_bitrate, total_requested_padding_bitrate,
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200234 total_requested_max_bitrate, has_packet_feedback);
perkj71ee44c2016-06-15 00:47:53 -0700235}
236
237void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700238 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200239
perkj26091b12016-09-01 01:17:40 -0700240 auto it = FindObserverConfig(observer);
241 if (it != bitrate_observer_configs_.end()) {
242 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700243 }
perkj26091b12016-09-01 01:17:40 -0700244
perkj71ee44c2016-06-15 00:47:53 -0700245 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000246}
247
perkj57c21f92016-06-17 07:27:16 -0700248int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700249 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200250 const auto& it = FindObserverConfig(observer);
251 if (it == bitrate_observer_configs_.end()) {
252 // This observer hasn't been added yet, just give it its fair share.
253 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700254 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200255 } else if (it->allocated_bitrate_bps == -1) {
256 // This observer hasn't received an allocation yet, so do the same.
257 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700258 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200259 } else {
260 // This observer already has an allocation.
261 return it->allocated_bitrate_bps;
262 }
perkj57c21f92016-06-17 07:27:16 -0700263}
264
Alex Narest78609d52017-10-20 10:37:47 +0200265void BitrateAllocator::SetBitrateAllocationStrategy(
266 std::unique_ptr<rtc::BitrateAllocationStrategy>
267 bitrate_allocation_strategy) {
268 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
269 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
270}
271
mflodman48a4beb2016-07-01 13:03:59 +0200272BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700273BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
274 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700275 for (auto it = bitrate_observer_configs_.begin();
276 it != bitrate_observer_configs_.end(); ++it) {
277 if (it->observer == observer)
278 return it;
279 }
280 return bitrate_observer_configs_.end();
281}
282
perkjfea93092016-05-14 00:58:48 -0700283BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
284 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700285 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700286 if (bitrate_observer_configs_.empty())
287 return ObserverAllocation();
288
Alex Narest78609d52017-10-20 10:37:47 +0200289 if (bitrate_allocation_strategy_ != nullptr) {
290 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
291 track_configs(bitrate_observer_configs_.size());
292 int i = 0;
293 for (const auto& c : bitrate_observer_configs_) {
294 track_configs[i++] = &c;
295 }
296 std::vector<uint32_t> track_allocations =
297 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
298 // The strategy should return allocation for all tracks.
299 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
300 ObserverAllocation allocation;
301 auto track_allocations_it = track_allocations.begin();
302 for (const auto& observer_config : bitrate_observer_configs_) {
303 allocation[observer_config.observer] = *track_allocations_it++;
304 }
305 return allocation;
306 }
307
perkjfea93092016-05-14 00:58:48 -0700308 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700309 return ZeroRateAllocation();
310
311 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200312 uint32_t sum_max_bitrates = 0;
313 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700314 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200315 sum_max_bitrates += observer_config.max_bitrate_bps;
316 }
317
318 // Not enough for all observers to get an allocation, allocate according to:
319 // enforced min bitrate -> allocated bitrate previous round -> restart paused
320 // streams.
321 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700322 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700323
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800324 // All observers will get their min bitrate plus a share of the rest. This
325 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200326 if (bitrate <= sum_max_bitrates)
327 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700328
Ying Wanga646d302018-03-02 17:04:11 +0100329 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200330 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000331}
332
mflodman2ebe5b12016-05-13 01:43:51 -0700333BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700334 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700335 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700336 for (const auto& observer_config : bitrate_observer_configs_)
337 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700338 return allocation;
339}
340
mflodman2ebe5b12016-05-13 01:43:51 -0700341BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100342 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700343 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700344 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200345 // Start by allocating bitrate to observers enforcing a min bitrate, hence
346 // remaining_bitrate might turn negative.
347 int64_t remaining_bitrate = bitrate;
348 for (const auto& observer_config : bitrate_observer_configs_) {
349 int32_t allocated_bitrate = 0;
350 if (observer_config.enforce_min_bitrate)
351 allocated_bitrate = observer_config.min_bitrate_bps;
352
353 allocation[observer_config.observer] = allocated_bitrate;
354 remaining_bitrate -= allocated_bitrate;
355 }
356
357 // Allocate bitrate to all previously active streams.
358 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700359 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200360 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100361 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200362 continue;
363
srte1eb051c2017-11-29 11:23:59 +0100364 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200365 if (remaining_bitrate >= required_bitrate) {
366 allocation[observer_config.observer] = required_bitrate;
367 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200368 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000369 }
370 }
mflodman101f2502016-06-09 17:21:19 +0200371
372 // Allocate bitrate to previously paused streams.
373 if (remaining_bitrate > 0) {
374 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100375 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200376 continue;
377
378 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100379 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200380 if (remaining_bitrate >= required_bitrate) {
381 allocation[observer_config.observer] = required_bitrate;
382 remaining_bitrate -= required_bitrate;
383 }
384 }
385 }
386
387 // Split a possible remainder evenly on all streams with an allocation.
388 if (remaining_bitrate > 0)
389 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
390
391 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100392 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000393}
mflodman101f2502016-06-09 17:21:19 +0200394
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800395// Allocates the bitrate based on the bitrate priority of each observer. This
396// bitrate priority defines the priority for bitrate to be allocated to that
397// observer in relation to other observers. For example with two observers, if
398// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
399// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
400// allocated twice the bitrate as observer 1 above the each observer's
401// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200402BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
403 uint32_t bitrate,
404 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700405 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200406 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800407 ObserverAllocation observers_capacities;
408 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200409 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800410 observers_capacities[observer_config.observer] =
411 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
412 }
mflodman101f2502016-06-09 17:21:19 +0200413
414 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800415 // From the remaining bitrate, allocate a proportional amount to each observer
416 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200417 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800418 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200419
420 return allocation;
421}
422
423BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700424 uint32_t bitrate,
425 uint32_t sum_max_bitrates) {
426 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200427 ObserverAllocation allocation;
428
429 for (const auto& observer_config : bitrate_observer_configs_) {
430 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
431 bitrate -= observer_config.max_bitrate_bps;
432 }
Ying Wanga646d302018-03-02 17:04:11 +0100433 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200434 &allocation);
435 return allocation;
436}
437
srte1eb051c2017-11-29 11:23:59 +0100438uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200439 // Return the configured minimum bitrate for newly added observers, to avoid
440 // requiring an extra high bitrate for the observer to get an allocated
441 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100442 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200443}
444
srte1eb051c2017-11-29 11:23:59 +0100445uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
446 uint32_t min_bitrate = min_bitrate_bps;
447 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200448 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
449 kMinToggleBitrateBps);
450 }
mflodman48a4beb2016-07-01 13:03:59 +0200451 // Account for protection bitrate used by this observer in the previous
452 // allocation.
453 // Note: the ratio will only be updated when the stream is active, meaning a
454 // paused stream won't get any ratio updates. This might lead to waiting a bit
455 // longer than necessary if the network condition improves, but this is to
456 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100457 if (media_ratio > 0.0 && media_ratio < 1.0)
458 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200459
mflodman101f2502016-06-09 17:21:19 +0200460 return min_bitrate;
461}
462
463void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
464 bool include_zero_allocations,
465 int max_multiplier,
466 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700467 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200468 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
469
470 ObserverSortingMap list_max_bitrates;
471 for (const auto& observer_config : bitrate_observer_configs_) {
472 if (include_zero_allocations ||
473 allocation->at(observer_config.observer) != 0) {
474 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
475 observer_config.max_bitrate_bps, &observer_config));
476 }
477 }
478 auto it = list_max_bitrates.begin();
479 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800480 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200481 uint32_t extra_allocation =
482 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
483 uint32_t total_allocation =
484 extra_allocation + allocation->at(it->second->observer);
485 bitrate -= extra_allocation;
486 if (total_allocation > max_multiplier * it->first) {
487 // There is more than we can fit for this observer, carry over to the
488 // remaining observers.
489 bitrate += total_allocation - max_multiplier * it->first;
490 total_allocation = max_multiplier * it->first;
491 }
492 // Finally, update the allocation for this observer.
493 allocation->at(it->second->observer) = total_allocation;
494 it = list_max_bitrates.erase(it);
495 }
496}
497
498bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
499 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700500 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200501 if (bitrate < sum_min_bitrates)
502 return false;
503
perkj26091b12016-09-01 01:17:40 -0700504 uint32_t extra_bitrate_per_observer =
505 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200506 static_cast<uint32_t>(bitrate_observer_configs_.size());
507 for (const auto& observer_config : bitrate_observer_configs_) {
508 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100509 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200510 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800511 }
mflodman101f2502016-06-09 17:21:19 +0200512 }
513 return true;
514}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800515
516void BitrateAllocator::DistributeBitrateRelatively(
517 uint32_t remaining_bitrate,
518 const ObserverAllocation& observers_capacities,
519 ObserverAllocation* allocation) {
520 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
521 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
522 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
523
524 struct PriorityRateObserverConfig {
525 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
526 uint32_t capacity_bps,
527 double bitrate_priority)
528 : allocation_key(allocation_key),
529 capacity_bps(capacity_bps),
530 bitrate_priority(bitrate_priority) {}
531
532 BitrateAllocatorObserver* allocation_key;
533 // The amount of bitrate bps that can be allocated to this observer.
534 uint32_t capacity_bps;
535 double bitrate_priority;
536
537 // We want to sort by which observers will be allocated their full capacity
538 // first. By dividing each observer's capacity by its bitrate priority we
539 // are "normalizing" the capacity of an observer by the rate it will be
540 // filled. This is because the amount allocated is based upon bitrate
541 // priority. We allocate twice as much bitrate to an observer with twice the
542 // bitrate priority of another.
543 bool operator<(const PriorityRateObserverConfig& other) const {
544 return capacity_bps / bitrate_priority <
545 other.capacity_bps / other.bitrate_priority;
546 }
547 };
548
549 double bitrate_priority_sum = 0;
550 std::vector<PriorityRateObserverConfig> priority_rate_observers;
551 for (const auto& observer_config : bitrate_observer_configs_) {
552 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
553 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
554 observer_config.bitrate_priority);
555 bitrate_priority_sum += observer_config.bitrate_priority;
556 }
557
558 // Iterate in the order observers can be allocated their full capacity.
559 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
560 size_t i;
561 for (i = 0; i < priority_rate_observers.size(); ++i) {
562 const auto& priority_rate_observer = priority_rate_observers[i];
563 // We allocate the full capacity to an observer only if its relative
564 // portion from the remaining bitrate is sufficient to allocate its full
565 // capacity. This means we aren't greedily allocating the full capacity, but
566 // that it is only done when there is also enough bitrate to allocate the
567 // proportional amounts to all other observers.
568 double observer_share =
569 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
570 double allocation_bps = observer_share * remaining_bitrate;
571 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
572 if (!enough_bitrate)
573 break;
574 allocation->at(priority_rate_observer.allocation_key) +=
575 priority_rate_observer.capacity_bps;
576 remaining_bitrate -= priority_rate_observer.capacity_bps;
577 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
578 }
579
580 // From the remaining bitrate, allocate the proportional amounts to the
581 // observers that aren't allocated their max capacity.
582 for (; i < priority_rate_observers.size(); ++i) {
583 const auto& priority_rate_observer = priority_rate_observers[i];
584 double fraction_allocated =
585 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
586 allocation->at(priority_rate_observer.allocation_key) +=
587 fraction_allocated * remaining_bitrate;
588 }
589}
590
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000591} // namespace webrtc