blob: c7049ba2111bf7b9bebb9091627c23e6aa59b4a6 [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
Niels Möller74e5f802018-04-25 14:03:46 +020075// static
Ying Wanga646d302018-03-02 17:04:11 +010076uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
77 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
78 "WebRTC-TransmissionMaxBitrateMultiplier")
79 .c_str(),
80 nullptr, 10);
81 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010082 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
83 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010084 return static_cast<uint8_t>(multiplier);
85 }
86 return kTransmissionMaxBitrateMultiplier;
87}
88
perkj71ee44c2016-06-15 00:47:53 -070089void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
90 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080091 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070092 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070093 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020094 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070095 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020096 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010097 last_fraction_loss_ = fraction_loss;
98 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -070099 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -0700100
mflodman48a4beb2016-07-01 13:03:59 +0200101 // Periodically log the incoming BWE.
102 int64_t now = clock_->TimeInMilliseconds();
103 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100104 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200105 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800106 }
mflodman48a4beb2016-07-01 13:03:59 +0200107
108 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
109
110 for (auto& config : bitrate_observer_configs_) {
111 uint32_t allocated_bitrate = allocation[config.observer];
112 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
Yves Gerey665174f2018-06-19 15:03:05 +0200113 allocated_bitrate, last_fraction_loss_, last_rtt_, 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,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200145 MediaStreamAllocationConfig config) {
perkj26091b12016-09-01 01:17:40 -0700146 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200147 RTC_DCHECK_GT(config.bitrate_priority, 0);
148 RTC_DCHECK(std::isnormal(config.bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700149 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000150
mflodman101f2502016-06-09 17:21:19 +0200151 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700152 if (it != bitrate_observer_configs_.end()) {
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200153 it->min_bitrate_bps = config.min_bitrate_bps;
154 it->max_bitrate_bps = config.max_bitrate_bps;
155 it->pad_up_bitrate_bps = config.pad_up_bitrate_bps;
156 it->enforce_min_bitrate = config.enforce_min_bitrate;
157 it->bitrate_priority = config.bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000158 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800159 bitrate_observer_configs_.push_back(ObserverConfig(
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200160 observer, config.min_bitrate_bps, config.max_bitrate_bps,
161 config.pad_up_bitrate_bps, config.enforce_min_bitrate, config.track_id,
162 config.bitrate_priority, config.has_packet_feedback));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000163 }
Stefan Holmere5904162015-03-26 11:11:06 +0100164
mflodman101f2502016-06-09 17:21:19 +0200165 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 13:03:59 +0200169 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 04:47:39 -0800172 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700173 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200174 config.allocated_bitrate_bps = allocated_bitrate;
175 if (allocated_bitrate > 0)
176 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
177 }
perkjfea93092016-05-14 00:58:48 -0700178 } 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 17:21:19 +0200182 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800183 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700184 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100185 }
perkj71ee44c2016-06-15 00:47:53 -0700186 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000187}
188
perkj71ee44c2016-06-15 00:47:53 -0700189void BitrateAllocator::UpdateAllocationLimits() {
190 uint32_t total_requested_padding_bitrate = 0;
191 uint32_t total_requested_min_bitrate = 0;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200192 uint32_t total_requested_max_bitrate = 0;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100193 bool has_packet_feedback = false;
perkj26091b12016-09-01 01:17:40 -0700194 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800195 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700196 if (config.enforce_min_bitrate) {
197 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800198 } else if (config.allocated_bitrate_bps == 0) {
199 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100200 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700201 }
philipel5ef2bc12017-02-21 07:28:31 -0800202 total_requested_padding_bitrate += stream_padding;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200203 total_requested_max_bitrate += config.max_bitrate_bps;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100204 if (config.allocated_bitrate_bps > 0 && config.has_packet_feedback)
205 has_packet_feedback = true;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000206 }
perkj71ee44c2016-06-15 00:47:53 -0700207
philipel5ef2bc12017-02-21 07:28:31 -0800208 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100209 total_requested_min_bitrate == total_requested_min_bitrate_ &&
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200210 total_requested_max_bitrate == total_requested_max_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100211 has_packet_feedback == has_packet_feedback_) {
philipel5ef2bc12017-02-21 07:28:31 -0800212 return;
213 }
214
215 total_requested_min_bitrate_ = total_requested_min_bitrate;
216 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200217 total_requested_max_bitrate_ = total_requested_max_bitrate;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100218 has_packet_feedback_ = has_packet_feedback;
philipel5ef2bc12017-02-21 07:28:31 -0800219
Mirko Bonadei675513b2017-11-09 11:09:25 +0100220 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
221 << total_requested_min_bitrate
222 << "bps, total_requested_padding_bitrate: "
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200223 << total_requested_padding_bitrate
224 << "bps, total_requested_max_bitrate: "
225 << total_requested_max_bitrate << "bps";
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100226 limit_observer_->OnAllocationLimitsChanged(
227 total_requested_min_bitrate, total_requested_padding_bitrate,
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200228 total_requested_max_bitrate, has_packet_feedback);
perkj71ee44c2016-06-15 00:47:53 -0700229}
230
231void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700232 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200233
perkj26091b12016-09-01 01:17:40 -0700234 auto it = FindObserverConfig(observer);
235 if (it != bitrate_observer_configs_.end()) {
236 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700237 }
perkj26091b12016-09-01 01:17:40 -0700238
perkj71ee44c2016-06-15 00:47:53 -0700239 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000240}
241
perkj57c21f92016-06-17 07:27:16 -0700242int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700243 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200244 const auto& it = FindObserverConfig(observer);
245 if (it == bitrate_observer_configs_.end()) {
246 // This observer hasn't been added yet, just give it its fair share.
247 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700248 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200249 } else if (it->allocated_bitrate_bps == -1) {
250 // This observer hasn't received an allocation yet, so do the same.
251 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700252 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200253 } else {
254 // This observer already has an allocation.
255 return it->allocated_bitrate_bps;
256 }
perkj57c21f92016-06-17 07:27:16 -0700257}
258
Alex Narest78609d52017-10-20 10:37:47 +0200259void BitrateAllocator::SetBitrateAllocationStrategy(
260 std::unique_ptr<rtc::BitrateAllocationStrategy>
261 bitrate_allocation_strategy) {
262 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
263 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
264}
265
mflodman48a4beb2016-07-01 13:03:59 +0200266BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700267BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700268 for (auto it = bitrate_observer_configs_.begin();
269 it != bitrate_observer_configs_.end(); ++it) {
270 if (it->observer == observer)
271 return it;
272 }
273 return bitrate_observer_configs_.end();
274}
275
perkjfea93092016-05-14 00:58:48 -0700276BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
277 uint32_t bitrate) {
mflodman2ebe5b12016-05-13 01:43:51 -0700278 if (bitrate_observer_configs_.empty())
279 return ObserverAllocation();
280
Alex Narest78609d52017-10-20 10:37:47 +0200281 if (bitrate_allocation_strategy_ != nullptr) {
282 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
283 track_configs(bitrate_observer_configs_.size());
284 int i = 0;
285 for (const auto& c : bitrate_observer_configs_) {
286 track_configs[i++] = &c;
287 }
288 std::vector<uint32_t> track_allocations =
289 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
290 // The strategy should return allocation for all tracks.
291 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
292 ObserverAllocation allocation;
293 auto track_allocations_it = track_allocations.begin();
294 for (const auto& observer_config : bitrate_observer_configs_) {
295 allocation[observer_config.observer] = *track_allocations_it++;
296 }
297 return allocation;
298 }
299
perkjfea93092016-05-14 00:58:48 -0700300 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700301 return ZeroRateAllocation();
302
303 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200304 uint32_t sum_max_bitrates = 0;
305 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700306 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200307 sum_max_bitrates += observer_config.max_bitrate_bps;
308 }
309
310 // Not enough for all observers to get an allocation, allocate according to:
311 // enforced min bitrate -> allocated bitrate previous round -> restart paused
312 // streams.
313 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700314 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700315
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800316 // All observers will get their min bitrate plus a share of the rest. This
317 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200318 if (bitrate <= sum_max_bitrates)
319 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700320
Ying Wanga646d302018-03-02 17:04:11 +0100321 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200322 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000323}
324
mflodman2ebe5b12016-05-13 01:43:51 -0700325BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
326 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700327 for (const auto& observer_config : bitrate_observer_configs_)
328 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700329 return allocation;
330}
331
mflodman2ebe5b12016-05-13 01:43:51 -0700332BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100333 uint32_t bitrate) {
mflodman2ebe5b12016-05-13 01:43:51 -0700334 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200335 // Start by allocating bitrate to observers enforcing a min bitrate, hence
336 // remaining_bitrate might turn negative.
337 int64_t remaining_bitrate = bitrate;
338 for (const auto& observer_config : bitrate_observer_configs_) {
339 int32_t allocated_bitrate = 0;
340 if (observer_config.enforce_min_bitrate)
341 allocated_bitrate = observer_config.min_bitrate_bps;
342
343 allocation[observer_config.observer] = allocated_bitrate;
344 remaining_bitrate -= allocated_bitrate;
345 }
346
347 // Allocate bitrate to all previously active streams.
348 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700349 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200350 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100351 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200352 continue;
353
srte1eb051c2017-11-29 11:23:59 +0100354 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200355 if (remaining_bitrate >= required_bitrate) {
356 allocation[observer_config.observer] = required_bitrate;
357 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200358 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000359 }
360 }
mflodman101f2502016-06-09 17:21:19 +0200361
362 // Allocate bitrate to previously paused streams.
363 if (remaining_bitrate > 0) {
364 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100365 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200366 continue;
367
368 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100369 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200370 if (remaining_bitrate >= required_bitrate) {
371 allocation[observer_config.observer] = required_bitrate;
372 remaining_bitrate -= required_bitrate;
373 }
374 }
375 }
376
377 // Split a possible remainder evenly on all streams with an allocation.
378 if (remaining_bitrate > 0)
379 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
380
381 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100382 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000383}
mflodman101f2502016-06-09 17:21:19 +0200384
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800385// Allocates the bitrate based on the bitrate priority of each observer. This
386// bitrate priority defines the priority for bitrate to be allocated to that
387// observer in relation to other observers. For example with two observers, if
388// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
389// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
390// allocated twice the bitrate as observer 1 above the each observer's
391// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200392BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
393 uint32_t bitrate,
394 uint32_t sum_min_bitrates) {
mflodman101f2502016-06-09 17:21:19 +0200395 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800396 ObserverAllocation observers_capacities;
397 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200398 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800399 observers_capacities[observer_config.observer] =
400 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
401 }
mflodman101f2502016-06-09 17:21:19 +0200402
403 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800404 // From the remaining bitrate, allocate a proportional amount to each observer
405 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200406 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800407 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200408
409 return allocation;
410}
411
412BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700413 uint32_t bitrate,
414 uint32_t sum_max_bitrates) {
mflodman101f2502016-06-09 17:21:19 +0200415 ObserverAllocation allocation;
416
417 for (const auto& observer_config : bitrate_observer_configs_) {
418 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
419 bitrate -= observer_config.max_bitrate_bps;
420 }
Ying Wanga646d302018-03-02 17:04:11 +0100421 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200422 &allocation);
423 return allocation;
424}
425
srte1eb051c2017-11-29 11:23:59 +0100426uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200427 // Return the configured minimum bitrate for newly added observers, to avoid
428 // requiring an extra high bitrate for the observer to get an allocated
429 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100430 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200431}
432
srte1eb051c2017-11-29 11:23:59 +0100433uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
434 uint32_t min_bitrate = min_bitrate_bps;
435 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200436 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
437 kMinToggleBitrateBps);
438 }
mflodman48a4beb2016-07-01 13:03:59 +0200439 // Account for protection bitrate used by this observer in the previous
440 // allocation.
441 // Note: the ratio will only be updated when the stream is active, meaning a
442 // paused stream won't get any ratio updates. This might lead to waiting a bit
443 // longer than necessary if the network condition improves, but this is to
444 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100445 if (media_ratio > 0.0 && media_ratio < 1.0)
446 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200447
mflodman101f2502016-06-09 17:21:19 +0200448 return min_bitrate;
449}
450
451void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
452 bool include_zero_allocations,
453 int max_multiplier,
454 ObserverAllocation* allocation) {
455 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 15:21:39 -0800467 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200468 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) {
487 if (bitrate < sum_min_bitrates)
488 return false;
489
perkj26091b12016-09-01 01:17:40 -0700490 uint32_t extra_bitrate_per_observer =
491 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200492 static_cast<uint32_t>(bitrate_observer_configs_.size());
493 for (const auto& observer_config : bitrate_observer_configs_) {
494 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100495 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200496 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800497 }
mflodman101f2502016-06-09 17:21:19 +0200498 }
499 return true;
500}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800501
502void BitrateAllocator::DistributeBitrateRelatively(
503 uint32_t remaining_bitrate,
504 const ObserverAllocation& observers_capacities,
505 ObserverAllocation* allocation) {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800506 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
507 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
508
509 struct PriorityRateObserverConfig {
510 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
511 uint32_t capacity_bps,
512 double bitrate_priority)
513 : allocation_key(allocation_key),
514 capacity_bps(capacity_bps),
515 bitrate_priority(bitrate_priority) {}
516
517 BitrateAllocatorObserver* allocation_key;
518 // The amount of bitrate bps that can be allocated to this observer.
519 uint32_t capacity_bps;
520 double bitrate_priority;
521
522 // We want to sort by which observers will be allocated their full capacity
523 // first. By dividing each observer's capacity by its bitrate priority we
524 // are "normalizing" the capacity of an observer by the rate it will be
525 // filled. This is because the amount allocated is based upon bitrate
526 // priority. We allocate twice as much bitrate to an observer with twice the
527 // bitrate priority of another.
528 bool operator<(const PriorityRateObserverConfig& other) const {
529 return capacity_bps / bitrate_priority <
530 other.capacity_bps / other.bitrate_priority;
531 }
532 };
533
534 double bitrate_priority_sum = 0;
535 std::vector<PriorityRateObserverConfig> priority_rate_observers;
536 for (const auto& observer_config : bitrate_observer_configs_) {
537 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
538 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
539 observer_config.bitrate_priority);
540 bitrate_priority_sum += observer_config.bitrate_priority;
541 }
542
543 // Iterate in the order observers can be allocated their full capacity.
544 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
545 size_t i;
546 for (i = 0; i < priority_rate_observers.size(); ++i) {
547 const auto& priority_rate_observer = priority_rate_observers[i];
548 // We allocate the full capacity to an observer only if its relative
549 // portion from the remaining bitrate is sufficient to allocate its full
550 // capacity. This means we aren't greedily allocating the full capacity, but
551 // that it is only done when there is also enough bitrate to allocate the
552 // proportional amounts to all other observers.
553 double observer_share =
554 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
555 double allocation_bps = observer_share * remaining_bitrate;
556 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
557 if (!enough_bitrate)
558 break;
559 allocation->at(priority_rate_observer.allocation_key) +=
560 priority_rate_observer.capacity_bps;
561 remaining_bitrate -= priority_rate_observer.capacity_bps;
562 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
563 }
564
565 // From the remaining bitrate, allocate the proportional amounts to the
566 // observers that aren't allocated their max capacity.
567 for (; i < priority_rate_observers.size(); ++i) {
568 const auto& priority_rate_observer = priority_rate_observers[i];
569 double fraction_allocated =
570 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
571 allocation->at(priority_rate_observer.allocation_key) +=
572 fraction_allocated * remaining_bitrate;
573 }
574}
575
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000576} // namespace webrtc