blob: 04676897d6c78fac3f157491d9f263d41cea07f7 [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 Jansson29b204e2018-03-21 12:45:27 +010062 has_packet_feedback_(false),
Ying Wanga646d302018-03-02 17:04:11 +010063 bitrate_allocation_strategy_(nullptr),
64 transmission_max_bitrate_multiplier_(
65 GetTransmissionMaxBitrateMultiplier()) {
perkj26091b12016-09-01 01:17:40 -070066 sequenced_checker_.Detach();
67}
mflodman48a4beb2016-07-01 13:03:59 +020068
69BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -070070 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
71 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +020072}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000073
Ying Wanga646d302018-03-02 17:04:11 +010074uint8_t BitrateAllocator::GetTransmissionMaxBitrateMultiplier() {
75 uint64_t multiplier = strtoul(webrtc::field_trial::FindFullName(
76 "WebRTC-TransmissionMaxBitrateMultiplier")
77 .c_str(),
78 nullptr, 10);
79 if (multiplier > 0 && multiplier <= kTransmissionMaxBitrateMultiplier) {
Ying Wang012b7e72018-03-05 15:44:23 +010080 RTC_LOG(LS_INFO) << "TransmissionMaxBitrateMultiplier is set to "
81 << multiplier;
Ying Wanga646d302018-03-02 17:04:11 +010082 return static_cast<uint8_t>(multiplier);
83 }
84 return kTransmissionMaxBitrateMultiplier;
85}
86
perkj71ee44c2016-06-15 00:47:53 -070087void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
88 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080089 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070090 int64_t bwe_period_ms) {
perkj26091b12016-09-01 01:17:40 -070091 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +020092 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070093 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020094 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010095 last_fraction_loss_ = fraction_loss;
96 last_rtt_ = rtt;
minyue93e45222017-05-18 14:32:41 -070097 last_bwe_period_ms_ = bwe_period_ms;
mflodman2ebe5b12016-05-13 01:43:51 -070098
mflodman48a4beb2016-07-01 13:03:59 +020099 // Periodically log the incoming BWE.
100 int64_t now = clock_->TimeInMilliseconds();
101 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100102 RTC_LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200103 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800104 }
mflodman48a4beb2016-07-01 13:03:59 +0200105
106 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
107
108 for (auto& config : bitrate_observer_configs_) {
109 uint32_t allocated_bitrate = allocation[config.observer];
110 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800111 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700112 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200113
114 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
115 if (target_bitrate_bps > 0)
116 ++num_pause_events_;
117 // The protection bitrate is an estimate based on the ratio between media
118 // and protection used before this observer was muted.
119 uint32_t predicted_protection_bps =
120 (1.0 - config.media_ratio) * config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100121 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
122 << " with configured min bitrate "
123 << config.min_bitrate_bps << " and current estimate of "
124 << target_bitrate_bps << " and protection bitrate "
125 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200126 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
127 if (target_bitrate_bps > 0)
128 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100129 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
130 << ", configured min bitrate " << config.min_bitrate_bps
131 << ", current allocation " << allocated_bitrate
132 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200133 }
134
135 // Only update the media ratio if the observer got an allocation.
136 if (allocated_bitrate > 0)
137 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
138 config.allocated_bitrate_bps = allocated_bitrate;
139 }
philipel5ef2bc12017-02-21 07:28:31 -0800140 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100141}
142
perkj57c21f92016-06-17 07:27:16 -0700143void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
144 uint32_t min_bitrate_bps,
145 uint32_t max_bitrate_bps,
146 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200147 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800148 std::string track_id,
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100149 double bitrate_priority,
150 bool has_packet_feedback) {
perkj26091b12016-09-01 01:17:40 -0700151 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800152 RTC_DCHECK_GT(bitrate_priority, 0);
153 RTC_DCHECK(std::isnormal(bitrate_priority));
mflodman2ebe5b12016-05-13 01:43:51 -0700154 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000155
mflodman101f2502016-06-09 17:21:19 +0200156 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700157 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700158 it->min_bitrate_bps = min_bitrate_bps;
159 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700160 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200161 it->enforce_min_bitrate = enforce_min_bitrate;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800162 it->bitrate_priority = bitrate_priority;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000163 } else {
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800164 bitrate_observer_configs_.push_back(ObserverConfig(
165 observer, min_bitrate_bps, max_bitrate_bps, pad_up_bitrate_bps,
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100166 enforce_min_bitrate, track_id, bitrate_priority, has_packet_feedback));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000167 }
Stefan Holmere5904162015-03-26 11:11:06 +0100168
mflodman101f2502016-06-09 17:21:19 +0200169 ObserverAllocation allocation;
170 if (last_bitrate_bps_ > 0) {
171 // Calculate a new allocation and update all observers.
172 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200173 for (auto& config : bitrate_observer_configs_) {
174 uint32_t allocated_bitrate = allocation[config.observer];
175 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
minyue78b4d562016-11-30 04:47:39 -0800176 allocated_bitrate, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700177 last_bwe_period_ms_);
mflodman48a4beb2016-07-01 13:03:59 +0200178 config.allocated_bitrate_bps = allocated_bitrate;
179 if (allocated_bitrate > 0)
180 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
181 }
perkjfea93092016-05-14 00:58:48 -0700182 } else {
183 // Currently, an encoder is not allowed to produce frames.
184 // But we still have to return the initial config bitrate + let the
185 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200186 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
minyue78b4d562016-11-30 04:47:39 -0800187 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_,
minyue93e45222017-05-18 14:32:41 -0700188 last_bwe_period_ms_);
Stefan Holmere5904162015-03-26 11:11:06 +0100189 }
perkj71ee44c2016-06-15 00:47:53 -0700190 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000191}
192
perkj71ee44c2016-06-15 00:47:53 -0700193void BitrateAllocator::UpdateAllocationLimits() {
perkj26091b12016-09-01 01:17:40 -0700194 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700195 uint32_t total_requested_padding_bitrate = 0;
196 uint32_t total_requested_min_bitrate = 0;
philipelf69e7682018-02-28 13:06:28 +0100197 uint32_t total_requested_bitrate = 0;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100198 bool has_packet_feedback = false;
perkj26091b12016-09-01 01:17:40 -0700199 for (const auto& config : bitrate_observer_configs_) {
philipel5ef2bc12017-02-21 07:28:31 -0800200 uint32_t stream_padding = config.pad_up_bitrate_bps;
perkj26091b12016-09-01 01:17:40 -0700201 if (config.enforce_min_bitrate) {
202 total_requested_min_bitrate += config.min_bitrate_bps;
philipel5ef2bc12017-02-21 07:28:31 -0800203 } else if (config.allocated_bitrate_bps == 0) {
204 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100205 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700206 }
philipel5ef2bc12017-02-21 07:28:31 -0800207 total_requested_padding_bitrate += stream_padding;
philipelf69e7682018-02-28 13:06:28 +0100208 total_requested_bitrate += config.max_bitrate_bps;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100209 if (config.allocated_bitrate_bps > 0 && config.has_packet_feedback)
210 has_packet_feedback = true;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000211 }
perkj71ee44c2016-06-15 00:47:53 -0700212
philipel5ef2bc12017-02-21 07:28:31 -0800213 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ &&
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100214 total_requested_min_bitrate == total_requested_min_bitrate_ &&
215 has_packet_feedback == has_packet_feedback_) {
philipel5ef2bc12017-02-21 07:28:31 -0800216 return;
217 }
218
219 total_requested_min_bitrate_ = total_requested_min_bitrate;
220 total_requested_padding_bitrate_ = total_requested_padding_bitrate;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100221 has_packet_feedback_ = has_packet_feedback;
philipel5ef2bc12017-02-21 07:28:31 -0800222
Mirko Bonadei675513b2017-11-09 11:09:25 +0100223 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
224 << total_requested_min_bitrate
225 << "bps, total_requested_padding_bitrate: "
226 << total_requested_padding_bitrate << "bps";
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100227 limit_observer_->OnAllocationLimitsChanged(
228 total_requested_min_bitrate, total_requested_padding_bitrate,
229 total_requested_bitrate, has_packet_feedback);
perkj71ee44c2016-06-15 00:47:53 -0700230}
231
232void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700233 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200234
perkj26091b12016-09-01 01:17:40 -0700235 auto it = FindObserverConfig(observer);
236 if (it != bitrate_observer_configs_.end()) {
237 bitrate_observer_configs_.erase(it);
perkj71ee44c2016-06-15 00:47:53 -0700238 }
perkj26091b12016-09-01 01:17:40 -0700239
perkj71ee44c2016-06-15 00:47:53 -0700240 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000241}
242
perkj57c21f92016-06-17 07:27:16 -0700243int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj26091b12016-09-01 01:17:40 -0700244 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200245 const auto& it = FindObserverConfig(observer);
246 if (it == bitrate_observer_configs_.end()) {
247 // This observer hasn't been added yet, just give it its fair share.
248 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700249 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200250 } else if (it->allocated_bitrate_bps == -1) {
251 // This observer hasn't received an allocation yet, so do the same.
252 return last_non_zero_bitrate_bps_ /
perkj26091b12016-09-01 01:17:40 -0700253 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200254 } else {
255 // This observer already has an allocation.
256 return it->allocated_bitrate_bps;
257 }
perkj57c21f92016-06-17 07:27:16 -0700258}
259
Alex Narest78609d52017-10-20 10:37:47 +0200260void BitrateAllocator::SetBitrateAllocationStrategy(
261 std::unique_ptr<rtc::BitrateAllocationStrategy>
262 bitrate_allocation_strategy) {
263 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
264 bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
265}
266
mflodman48a4beb2016-07-01 13:03:59 +0200267BitrateAllocator::ObserverConfigs::iterator
perkj26091b12016-09-01 01:17:40 -0700268BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
269 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700270 for (auto it = bitrate_observer_configs_.begin();
271 it != bitrate_observer_configs_.end(); ++it) {
272 if (it->observer == observer)
273 return it;
274 }
275 return bitrate_observer_configs_.end();
276}
277
perkjfea93092016-05-14 00:58:48 -0700278BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
279 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700280 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700281 if (bitrate_observer_configs_.empty())
282 return ObserverAllocation();
283
Alex Narest78609d52017-10-20 10:37:47 +0200284 if (bitrate_allocation_strategy_ != nullptr) {
285 std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
286 track_configs(bitrate_observer_configs_.size());
287 int i = 0;
288 for (const auto& c : bitrate_observer_configs_) {
289 track_configs[i++] = &c;
290 }
291 std::vector<uint32_t> track_allocations =
292 bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
293 // The strategy should return allocation for all tracks.
294 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
295 ObserverAllocation allocation;
296 auto track_allocations_it = track_allocations.begin();
297 for (const auto& observer_config : bitrate_observer_configs_) {
298 allocation[observer_config.observer] = *track_allocations_it++;
299 }
300 return allocation;
301 }
302
perkjfea93092016-05-14 00:58:48 -0700303 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700304 return ZeroRateAllocation();
305
306 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200307 uint32_t sum_max_bitrates = 0;
308 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700309 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200310 sum_max_bitrates += observer_config.max_bitrate_bps;
311 }
312
313 // Not enough for all observers to get an allocation, allocate according to:
314 // enforced min bitrate -> allocated bitrate previous round -> restart paused
315 // streams.
316 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700317 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700318
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800319 // All observers will get their min bitrate plus a share of the rest. This
320 // share is allocated to each observer based on its bitrate_priority.
mflodman101f2502016-06-09 17:21:19 +0200321 if (bitrate <= sum_max_bitrates)
322 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700323
Ying Wanga646d302018-03-02 17:04:11 +0100324 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
mflodman101f2502016-06-09 17:21:19 +0200325 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000326}
327
mflodman2ebe5b12016-05-13 01:43:51 -0700328BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
perkj26091b12016-09-01 01:17:40 -0700329 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700330 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700331 for (const auto& observer_config : bitrate_observer_configs_)
332 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700333 return allocation;
334}
335
mflodman2ebe5b12016-05-13 01:43:51 -0700336BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100337 uint32_t bitrate) {
perkj26091b12016-09-01 01:17:40 -0700338 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700339 ObserverAllocation allocation;
mflodman101f2502016-06-09 17:21:19 +0200340 // Start by allocating bitrate to observers enforcing a min bitrate, hence
341 // remaining_bitrate might turn negative.
342 int64_t remaining_bitrate = bitrate;
343 for (const auto& observer_config : bitrate_observer_configs_) {
344 int32_t allocated_bitrate = 0;
345 if (observer_config.enforce_min_bitrate)
346 allocated_bitrate = observer_config.min_bitrate_bps;
347
348 allocation[observer_config.observer] = allocated_bitrate;
349 remaining_bitrate -= allocated_bitrate;
350 }
351
352 // Allocate bitrate to all previously active streams.
353 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700354 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200355 if (observer_config.enforce_min_bitrate ||
srte1eb051c2017-11-29 11:23:59 +0100356 observer_config.LastAllocatedBitrate() == 0)
mflodman101f2502016-06-09 17:21:19 +0200357 continue;
358
srte1eb051c2017-11-29 11:23:59 +0100359 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman48a4beb2016-07-01 13:03:59 +0200360 if (remaining_bitrate >= required_bitrate) {
361 allocation[observer_config.observer] = required_bitrate;
362 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200363 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000364 }
365 }
mflodman101f2502016-06-09 17:21:19 +0200366
367 // Allocate bitrate to previously paused streams.
368 if (remaining_bitrate > 0) {
369 for (const auto& observer_config : bitrate_observer_configs_) {
srte1eb051c2017-11-29 11:23:59 +0100370 if (observer_config.LastAllocatedBitrate() != 0)
mflodman101f2502016-06-09 17:21:19 +0200371 continue;
372
373 // Add a hysteresis to avoid toggling.
srte1eb051c2017-11-29 11:23:59 +0100374 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
mflodman101f2502016-06-09 17:21:19 +0200375 if (remaining_bitrate >= required_bitrate) {
376 allocation[observer_config.observer] = required_bitrate;
377 remaining_bitrate -= required_bitrate;
378 }
379 }
380 }
381
382 // Split a possible remainder evenly on all streams with an allocation.
383 if (remaining_bitrate > 0)
384 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
385
386 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100387 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000388}
mflodman101f2502016-06-09 17:21:19 +0200389
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800390// Allocates the bitrate based on the bitrate priority of each observer. This
391// bitrate priority defines the priority for bitrate to be allocated to that
392// observer in relation to other observers. For example with two observers, if
393// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
394// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
395// allocated twice the bitrate as observer 1 above the each observer's
396// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
mflodman101f2502016-06-09 17:21:19 +0200397BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
398 uint32_t bitrate,
399 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700400 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200401 ObserverAllocation allocation;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800402 ObserverAllocation observers_capacities;
403 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200404 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800405 observers_capacities[observer_config.observer] =
406 observer_config.max_bitrate_bps - observer_config.min_bitrate_bps;
407 }
mflodman101f2502016-06-09 17:21:19 +0200408
409 bitrate -= sum_min_bitrates;
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800410 // From the remaining bitrate, allocate a proportional amount to each observer
411 // above the min bitrate already allocated.
mflodman101f2502016-06-09 17:21:19 +0200412 if (bitrate > 0)
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800413 DistributeBitrateRelatively(bitrate, observers_capacities, &allocation);
mflodman101f2502016-06-09 17:21:19 +0200414
415 return allocation;
416}
417
418BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj26091b12016-09-01 01:17:40 -0700419 uint32_t bitrate,
420 uint32_t sum_max_bitrates) {
421 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200422 ObserverAllocation allocation;
423
424 for (const auto& observer_config : bitrate_observer_configs_) {
425 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
426 bitrate -= observer_config.max_bitrate_bps;
427 }
Ying Wanga646d302018-03-02 17:04:11 +0100428 DistributeBitrateEvenly(bitrate, true, transmission_max_bitrate_multiplier_,
mflodman101f2502016-06-09 17:21:19 +0200429 &allocation);
430 return allocation;
431}
432
srte1eb051c2017-11-29 11:23:59 +0100433uint32_t BitrateAllocator::ObserverConfig::LastAllocatedBitrate() const {
mflodman101f2502016-06-09 17:21:19 +0200434 // Return the configured minimum bitrate for newly added observers, to avoid
435 // requiring an extra high bitrate for the observer to get an allocated
436 // bitrate.
srte1eb051c2017-11-29 11:23:59 +0100437 return allocated_bitrate_bps == -1 ? min_bitrate_bps : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200438}
439
srte1eb051c2017-11-29 11:23:59 +0100440uint32_t BitrateAllocator::ObserverConfig::MinBitrateWithHysteresis() const {
441 uint32_t min_bitrate = min_bitrate_bps;
442 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200443 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
444 kMinToggleBitrateBps);
445 }
mflodman48a4beb2016-07-01 13:03:59 +0200446 // Account for protection bitrate used by this observer in the previous
447 // allocation.
448 // Note: the ratio will only be updated when the stream is active, meaning a
449 // paused stream won't get any ratio updates. This might lead to waiting a bit
450 // longer than necessary if the network condition improves, but this is to
451 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100452 if (media_ratio > 0.0 && media_ratio < 1.0)
453 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200454
mflodman101f2502016-06-09 17:21:19 +0200455 return min_bitrate;
456}
457
458void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
459 bool include_zero_allocations,
460 int max_multiplier,
461 ObserverAllocation* allocation) {
perkj26091b12016-09-01 01:17:40 -0700462 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200463 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
464
465 ObserverSortingMap list_max_bitrates;
466 for (const auto& observer_config : bitrate_observer_configs_) {
467 if (include_zero_allocations ||
468 allocation->at(observer_config.observer) != 0) {
469 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
470 observer_config.max_bitrate_bps, &observer_config));
471 }
472 }
473 auto it = list_max_bitrates.begin();
474 while (it != list_max_bitrates.end()) {
kwibergaf476c72016-11-28 15:21:39 -0800475 RTC_DCHECK_GT(bitrate, 0);
mflodman101f2502016-06-09 17:21:19 +0200476 uint32_t extra_allocation =
477 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
478 uint32_t total_allocation =
479 extra_allocation + allocation->at(it->second->observer);
480 bitrate -= extra_allocation;
481 if (total_allocation > max_multiplier * it->first) {
482 // There is more than we can fit for this observer, carry over to the
483 // remaining observers.
484 bitrate += total_allocation - max_multiplier * it->first;
485 total_allocation = max_multiplier * it->first;
486 }
487 // Finally, update the allocation for this observer.
488 allocation->at(it->second->observer) = total_allocation;
489 it = list_max_bitrates.erase(it);
490 }
491}
492
493bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
494 uint32_t sum_min_bitrates) {
perkj26091b12016-09-01 01:17:40 -0700495 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200496 if (bitrate < sum_min_bitrates)
497 return false;
498
perkj26091b12016-09-01 01:17:40 -0700499 uint32_t extra_bitrate_per_observer =
500 (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200501 static_cast<uint32_t>(bitrate_observer_configs_.size());
502 for (const auto& observer_config : bitrate_observer_configs_) {
503 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
srte1eb051c2017-11-29 11:23:59 +0100504 observer_config.MinBitrateWithHysteresis()) {
mflodman101f2502016-06-09 17:21:19 +0200505 return false;
philipel5ef2bc12017-02-21 07:28:31 -0800506 }
mflodman101f2502016-06-09 17:21:19 +0200507 }
508 return true;
509}
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800510
511void BitrateAllocator::DistributeBitrateRelatively(
512 uint32_t remaining_bitrate,
513 const ObserverAllocation& observers_capacities,
514 ObserverAllocation* allocation) {
515 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
516 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
517 RTC_DCHECK_EQ(observers_capacities.size(), bitrate_observer_configs_.size());
518
519 struct PriorityRateObserverConfig {
520 PriorityRateObserverConfig(BitrateAllocatorObserver* allocation_key,
521 uint32_t capacity_bps,
522 double bitrate_priority)
523 : allocation_key(allocation_key),
524 capacity_bps(capacity_bps),
525 bitrate_priority(bitrate_priority) {}
526
527 BitrateAllocatorObserver* allocation_key;
528 // The amount of bitrate bps that can be allocated to this observer.
529 uint32_t capacity_bps;
530 double bitrate_priority;
531
532 // We want to sort by which observers will be allocated their full capacity
533 // first. By dividing each observer's capacity by its bitrate priority we
534 // are "normalizing" the capacity of an observer by the rate it will be
535 // filled. This is because the amount allocated is based upon bitrate
536 // priority. We allocate twice as much bitrate to an observer with twice the
537 // bitrate priority of another.
538 bool operator<(const PriorityRateObserverConfig& other) const {
539 return capacity_bps / bitrate_priority <
540 other.capacity_bps / other.bitrate_priority;
541 }
542 };
543
544 double bitrate_priority_sum = 0;
545 std::vector<PriorityRateObserverConfig> priority_rate_observers;
546 for (const auto& observer_config : bitrate_observer_configs_) {
547 uint32_t capacity_bps = observers_capacities.at(observer_config.observer);
548 priority_rate_observers.emplace_back(observer_config.observer, capacity_bps,
549 observer_config.bitrate_priority);
550 bitrate_priority_sum += observer_config.bitrate_priority;
551 }
552
553 // Iterate in the order observers can be allocated their full capacity.
554 std::sort(priority_rate_observers.begin(), priority_rate_observers.end());
555 size_t i;
556 for (i = 0; i < priority_rate_observers.size(); ++i) {
557 const auto& priority_rate_observer = priority_rate_observers[i];
558 // We allocate the full capacity to an observer only if its relative
559 // portion from the remaining bitrate is sufficient to allocate its full
560 // capacity. This means we aren't greedily allocating the full capacity, but
561 // that it is only done when there is also enough bitrate to allocate the
562 // proportional amounts to all other observers.
563 double observer_share =
564 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
565 double allocation_bps = observer_share * remaining_bitrate;
566 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
567 if (!enough_bitrate)
568 break;
569 allocation->at(priority_rate_observer.allocation_key) +=
570 priority_rate_observer.capacity_bps;
571 remaining_bitrate -= priority_rate_observer.capacity_bps;
572 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
573 }
574
575 // From the remaining bitrate, allocate the proportional amounts to the
576 // observers that aren't allocated their max capacity.
577 for (; i < priority_rate_observers.size(); ++i) {
578 const auto& priority_rate_observer = priority_rate_observers[i];
579 double fraction_allocated =
580 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
581 allocation->at(priority_rate_observer.allocation_key) +=
582 fraction_allocated * remaining_bitrate;
583 }
584}
585
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000586} // namespace webrtc