blob: 7d9e5cb65141d675beb9371a5cce716e1d40f2f7 [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
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020019#include "absl/algorithm/container.h"
Sebastian Jansson6736df12018-11-21 19:18:39 +010020#include "api/units/data_rate.h"
21#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
23#include "rtc_base/logging.h"
Sebastian Jansson40de3cc2019-09-19 14:54:43 +020024#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "system_wrappers/include/clock.h"
Ying Wanga646d302018-03-02 17:04:11 +010026#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000028
29namespace webrtc {
30
Rasmus Brandt681de202019-02-04 15:09:34 +010031namespace {
Sebastian Jansson538ca572019-09-24 19:47:26 +020032using bitrate_allocator_impl::AllocatableTrack;
Rasmus Brandt681de202019-02-04 15:09:34 +010033
Stefan Holmere5904162015-03-26 11:11:06 +010034// Allow packets to be transmitted in up to 2 times max video bitrate if the
35// bandwidth estimate allows it.
Ying Wanga646d302018-03-02 17:04:11 +010036const uint8_t kTransmissionMaxBitrateMultiplier = 2;
Stefan Holmere5904162015-03-26 11:11:06 +010037const int kDefaultBitrateBps = 300000;
38
mflodman101f2502016-06-09 17:21:19 +020039// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
40const double kToggleFactor = 0.1;
41const uint32_t kMinToggleBitrateBps = 20000;
42
mflodman48a4beb2016-07-01 13:03:59 +020043const int64_t kBweLogIntervalMs = 5000;
44
mflodman48a4beb2016-07-01 13:03:59 +020045double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
kwibergaf476c72016-11-28 15:21:39 -080046 RTC_DCHECK_GT(allocated_bitrate, 0);
mflodman48a4beb2016-07-01 13:03:59 +020047 if (protection_bitrate == 0)
48 return 1.0;
49
50 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
51 return media_bitrate / static_cast<double>(allocated_bitrate);
52}
Rasmus Brandt681de202019-02-04 15:09:34 +010053
Sebastian Jansson538ca572019-09-24 19:47:26 +020054bool EnoughBitrateForAllObservers(
55 const std::vector<AllocatableTrack>& allocatable_tracks,
56 uint32_t bitrate,
57 uint32_t sum_min_bitrates) {
58 if (bitrate < sum_min_bitrates)
59 return false;
60
61 uint32_t extra_bitrate_per_observer =
62 (bitrate - sum_min_bitrates) /
63 static_cast<uint32_t>(allocatable_tracks.size());
64 for (const auto& observer_config : allocatable_tracks) {
65 if (observer_config.config.min_bitrate_bps + extra_bitrate_per_observer <
66 observer_config.MinBitrateWithHysteresis()) {
67 return false;
68 }
69 }
70 return true;
71}
72
73// Splits |bitrate| evenly to observers already in |allocation|.
74// |include_zero_allocations| decides if zero allocations should be part of
75// the distribution or not. The allowed max bitrate is |max_multiplier| x
76// observer max bitrate.
77void DistributeBitrateEvenly(
78 const std::vector<AllocatableTrack>& allocatable_tracks,
79 uint32_t bitrate,
80 bool include_zero_allocations,
81 int max_multiplier,
82 std::map<BitrateAllocatorObserver*, int>* allocation) {
83 RTC_DCHECK_EQ(allocation->size(), allocatable_tracks.size());
84
85 std::multimap<uint32_t, const AllocatableTrack*> list_max_bitrates;
86 for (const auto& observer_config : allocatable_tracks) {
87 if (include_zero_allocations ||
88 allocation->at(observer_config.observer) != 0) {
89 list_max_bitrates.insert(
90 {observer_config.config.max_bitrate_bps, &observer_config});
91 }
92 }
93 auto it = list_max_bitrates.begin();
94 while (it != list_max_bitrates.end()) {
95 RTC_DCHECK_GT(bitrate, 0);
96 uint32_t extra_allocation =
97 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
98 uint32_t total_allocation =
99 extra_allocation + allocation->at(it->second->observer);
100 bitrate -= extra_allocation;
101 if (total_allocation > max_multiplier * it->first) {
102 // There is more than we can fit for this observer, carry over to the
103 // remaining observers.
104 bitrate += total_allocation - max_multiplier * it->first;
105 total_allocation = max_multiplier * it->first;
106 }
107 // Finally, update the allocation for this observer.
108 allocation->at(it->second->observer) = total_allocation;
109 it = list_max_bitrates.erase(it);
110 }
111}
112
113// From the available |bitrate|, each observer will be allocated a
114// proportional amount based upon its bitrate priority. If that amount is
115// more than the observer's capacity, it will be allocated its capacity, and
116// the excess bitrate is still allocated proportionally to other observers.
117// Allocating the proportional amount means an observer with twice the
118// bitrate_priority of another will be allocated twice the bitrate.
119void DistributeBitrateRelatively(
120 const std::vector<AllocatableTrack>& allocatable_tracks,
121 uint32_t remaining_bitrate,
122 const std::map<BitrateAllocatorObserver*, int>& observers_capacities,
123 std::map<BitrateAllocatorObserver*, int>* allocation) {
124 RTC_DCHECK_EQ(allocation->size(), allocatable_tracks.size());
125 RTC_DCHECK_EQ(observers_capacities.size(), allocatable_tracks.size());
126
127 struct PriorityRateObserverConfig {
128 BitrateAllocatorObserver* allocation_key;
129 // The amount of bitrate bps that can be allocated to this observer.
130 int capacity_bps;
131 double bitrate_priority;
132 };
133
134 double bitrate_priority_sum = 0;
135 std::vector<PriorityRateObserverConfig> priority_rate_observers;
136 for (const auto& observer_config : allocatable_tracks) {
137 priority_rate_observers.push_back(PriorityRateObserverConfig{
138 observer_config.observer,
139 observers_capacities.at(observer_config.observer),
140 observer_config.config.bitrate_priority});
141 bitrate_priority_sum += observer_config.config.bitrate_priority;
142 }
143
144 // Iterate in the order observers can be allocated their full capacity.
145
146 // We want to sort by which observers will be allocated their full capacity
147 // first. By dividing each observer's capacity by its bitrate priority we
148 // are "normalizing" the capacity of an observer by the rate it will be
149 // filled. This is because the amount allocated is based upon bitrate
150 // priority. We allocate twice as much bitrate to an observer with twice the
151 // bitrate priority of another.
152 absl::c_sort(priority_rate_observers, [](const auto& a, const auto& b) {
153 return a.capacity_bps / a.bitrate_priority <
154 b.capacity_bps / b.bitrate_priority;
155 });
156 size_t i;
157 for (i = 0; i < priority_rate_observers.size(); ++i) {
158 const auto& priority_rate_observer = priority_rate_observers[i];
159 // We allocate the full capacity to an observer only if its relative
160 // portion from the remaining bitrate is sufficient to allocate its full
161 // capacity. This means we aren't greedily allocating the full capacity, but
162 // that it is only done when there is also enough bitrate to allocate the
163 // proportional amounts to all other observers.
164 double observer_share =
165 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
166 double allocation_bps = observer_share * remaining_bitrate;
167 bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
168 if (!enough_bitrate)
169 break;
170 allocation->at(priority_rate_observer.allocation_key) +=
171 priority_rate_observer.capacity_bps;
172 remaining_bitrate -= priority_rate_observer.capacity_bps;
173 bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
174 }
175
176 // From the remaining bitrate, allocate the proportional amounts to the
177 // observers that aren't allocated their max capacity.
178 for (; i < priority_rate_observers.size(); ++i) {
179 const auto& priority_rate_observer = priority_rate_observers[i];
180 double fraction_allocated =
181 priority_rate_observer.bitrate_priority / bitrate_priority_sum;
182 allocation->at(priority_rate_observer.allocation_key) +=
183 fraction_allocated * remaining_bitrate;
184 }
185}
186
187// Allocates bitrate to observers when there isn't enough to allocate the
188// minimum to all observers.
189std::map<BitrateAllocatorObserver*, int> LowRateAllocation(
190 const std::vector<AllocatableTrack>& allocatable_tracks,
191 uint32_t bitrate) {
192 std::map<BitrateAllocatorObserver*, int> allocation;
193 // Start by allocating bitrate to observers enforcing a min bitrate, hence
194 // remaining_bitrate might turn negative.
195 int64_t remaining_bitrate = bitrate;
196 for (const auto& observer_config : allocatable_tracks) {
197 int32_t allocated_bitrate = 0;
198 if (observer_config.config.enforce_min_bitrate)
199 allocated_bitrate = observer_config.config.min_bitrate_bps;
200
201 allocation[observer_config.observer] = allocated_bitrate;
202 remaining_bitrate -= allocated_bitrate;
203 }
204
205 // Allocate bitrate to all previously active streams.
206 if (remaining_bitrate > 0) {
207 for (const auto& observer_config : allocatable_tracks) {
208 if (observer_config.config.enforce_min_bitrate ||
209 observer_config.LastAllocatedBitrate() == 0)
210 continue;
211
212 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
213 if (remaining_bitrate >= required_bitrate) {
214 allocation[observer_config.observer] = required_bitrate;
215 remaining_bitrate -= required_bitrate;
216 }
217 }
218 }
219
220 // Allocate bitrate to previously paused streams.
221 if (remaining_bitrate > 0) {
222 for (const auto& observer_config : allocatable_tracks) {
223 if (observer_config.LastAllocatedBitrate() != 0)
224 continue;
225
226 // Add a hysteresis to avoid toggling.
227 uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
228 if (remaining_bitrate >= required_bitrate) {
229 allocation[observer_config.observer] = required_bitrate;
230 remaining_bitrate -= required_bitrate;
231 }
232 }
233 }
234
235 // Split a possible remainder evenly on all streams with an allocation.
236 if (remaining_bitrate > 0)
237 DistributeBitrateEvenly(allocatable_tracks, remaining_bitrate, false, 1,
238 &allocation);
239
240 RTC_DCHECK_EQ(allocation.size(), allocatable_tracks.size());
241 return allocation;
242}
243
244// Allocates bitrate to all observers when the available bandwidth is enough
245// to allocate the minimum to all observers but not enough to allocate the
246// max bitrate of each observer.
247
248// Allocates the bitrate based on the bitrate priority of each observer. This
249// bitrate priority defines the priority for bitrate to be allocated to that
250// observer in relation to other observers. For example with two observers, if
251// observer 1 had a bitrate_priority = 1.0, and observer 2 has a
252// bitrate_priority = 2.0, the expected behavior is that observer 2 will be
253// allocated twice the bitrate as observer 1 above the each observer's
254// min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
255std::map<BitrateAllocatorObserver*, int> NormalRateAllocation(
256 const std::vector<AllocatableTrack>& allocatable_tracks,
257 uint32_t bitrate,
258 uint32_t sum_min_bitrates) {
259 std::map<BitrateAllocatorObserver*, int> allocation;
260 std::map<BitrateAllocatorObserver*, int> observers_capacities;
261 for (const auto& observer_config : allocatable_tracks) {
262 allocation[observer_config.observer] =
263 observer_config.config.min_bitrate_bps;
264 observers_capacities[observer_config.observer] =
265 observer_config.config.max_bitrate_bps -
266 observer_config.config.min_bitrate_bps;
267 }
268
269 bitrate -= sum_min_bitrates;
270
271 // TODO(srte): Implement fair sharing between prioritized streams, currently
272 // they are treated on a first come first serve basis.
273 for (const auto& observer_config : allocatable_tracks) {
274 int64_t priority_margin = observer_config.config.priority_bitrate_bps -
275 allocation[observer_config.observer];
276 if (priority_margin > 0 && bitrate > 0) {
277 int64_t extra_bitrate = std::min<int64_t>(priority_margin, bitrate);
278 allocation[observer_config.observer] +=
279 rtc::dchecked_cast<int>(extra_bitrate);
280 observers_capacities[observer_config.observer] -= extra_bitrate;
281 bitrate -= extra_bitrate;
282 }
283 }
284
285 // From the remaining bitrate, allocate a proportional amount to each observer
286 // above the min bitrate already allocated.
287 if (bitrate > 0)
288 DistributeBitrateRelatively(allocatable_tracks, bitrate,
289 observers_capacities, &allocation);
290
291 return allocation;
292}
293
294// Allocates bitrate to observers when there is enough available bandwidth
295// for all observers to be allocated their max bitrate.
296std::map<BitrateAllocatorObserver*, int> MaxRateAllocation(
297 const std::vector<AllocatableTrack>& allocatable_tracks,
298 uint32_t bitrate,
299 uint32_t sum_max_bitrates) {
300 std::map<BitrateAllocatorObserver*, int> allocation;
301
302 for (const auto& observer_config : allocatable_tracks) {
303 allocation[observer_config.observer] =
304 observer_config.config.max_bitrate_bps;
305 bitrate -= observer_config.config.max_bitrate_bps;
306 }
307 DistributeBitrateEvenly(allocatable_tracks, bitrate, true,
308 kTransmissionMaxBitrateMultiplier, &allocation);
309 return allocation;
310}
311
312// Allocates zero bitrate to all observers.
313std::map<BitrateAllocatorObserver*, int> ZeroRateAllocation(
314 const std::vector<AllocatableTrack>& allocatable_tracks) {
315 std::map<BitrateAllocatorObserver*, int> allocation;
316 for (const auto& observer_config : allocatable_tracks)
317 allocation[observer_config.observer] = 0;
318 return allocation;
319}
320
321std::map<BitrateAllocatorObserver*, int> AllocateBitrates(
322 const std::vector<AllocatableTrack>& allocatable_tracks,
323 uint32_t bitrate) {
324 if (allocatable_tracks.empty())
325 return std::map<BitrateAllocatorObserver*, int>();
326
327 if (bitrate == 0)
328 return ZeroRateAllocation(allocatable_tracks);
329
330 uint32_t sum_min_bitrates = 0;
331 uint32_t sum_max_bitrates = 0;
332 for (const auto& observer_config : allocatable_tracks) {
333 sum_min_bitrates += observer_config.config.min_bitrate_bps;
334 sum_max_bitrates += observer_config.config.max_bitrate_bps;
335 }
336
337 // Not enough for all observers to get an allocation, allocate according to:
338 // enforced min bitrate -> allocated bitrate previous round -> restart paused
339 // streams.
340 if (!EnoughBitrateForAllObservers(allocatable_tracks, bitrate,
341 sum_min_bitrates))
342 return LowRateAllocation(allocatable_tracks, bitrate);
343
344 // All observers will get their min bitrate plus a share of the rest. This
345 // share is allocated to each observer based on its bitrate_priority.
346 if (bitrate <= sum_max_bitrates)
347 return NormalRateAllocation(allocatable_tracks, bitrate, sum_min_bitrates);
348
349 // All observers will get up to transmission_max_bitrate_multiplier_ x max.
350 return MaxRateAllocation(allocatable_tracks, bitrate, sum_max_bitrates);
351}
352
mflodman48a4beb2016-07-01 13:03:59 +0200353} // namespace
354
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200355BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
perkj71ee44c2016-06-15 00:47:53 -0700356 : limit_observer_(limit_observer),
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100357 last_target_bps_(0),
Florent Castelli4e615d52019-08-22 16:09:06 +0200358 last_stable_target_bps_(0),
perkjfea93092016-05-14 00:58:48 -0700359 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +0100360 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +0200361 last_rtt_(0),
Sebastian Jansson13e59032018-11-21 19:13:07 +0100362 last_bwe_period_ms_(1000),
mflodman48a4beb2016-07-01 13:03:59 +0200363 num_pause_events_(0),
Sebastian Jansson538ca572019-09-24 19:47:26 +0200364 last_bwe_log_time_(0) {
perkj26091b12016-09-01 01:17:40 -0700365 sequenced_checker_.Detach();
366}
mflodman48a4beb2016-07-01 13:03:59 +0200367
368BitrateAllocator::~BitrateAllocator() {
asapersson1d02d3e2016-09-09 22:40:25 -0700369 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
370 num_pause_events_);
mflodman48a4beb2016-07-01 13:03:59 +0200371}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000372
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100373void BitrateAllocator::UpdateStartRate(uint32_t start_rate_bps) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200374 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100375 last_non_zero_bitrate_bps_ = start_rate_bps;
376}
377
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200378void BitrateAllocator::OnNetworkEstimateChanged(TargetTransferRate msg) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200379 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200380 last_target_bps_ = msg.target_rate.bps();
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200381 last_stable_target_bps_ = msg.stable_target_rate.bps();
perkjfea93092016-05-14 00:58:48 -0700382 last_non_zero_bitrate_bps_ =
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200383 last_target_bps_ > 0 ? last_target_bps_ : last_non_zero_bitrate_bps_;
384
385 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
386 last_fraction_loss_ =
387 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
388 last_rtt_ = msg.network_estimate.round_trip_time.ms();
389 last_bwe_period_ms_ = msg.network_estimate.bwe_period.ms();
mflodman2ebe5b12016-05-13 01:43:51 -0700390
mflodman48a4beb2016-07-01 13:03:59 +0200391 // Periodically log the incoming BWE.
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200392 int64_t now = msg.at_time.ms();
mflodman48a4beb2016-07-01 13:03:59 +0200393 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200394 RTC_LOG(LS_INFO) << "Current BWE " << last_target_bps_;
mflodman48a4beb2016-07-01 13:03:59 +0200395 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -0800396 }
mflodman48a4beb2016-07-01 13:03:59 +0200397
Sebastian Jansson538ca572019-09-24 19:47:26 +0200398 auto allocation = AllocateBitrates(allocatable_tracks_, last_target_bps_);
399 auto stable_bitrate_allocation =
400 AllocateBitrates(allocatable_tracks_, last_stable_target_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200401
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200402 for (auto& config : allocatable_tracks_) {
mflodman48a4beb2016-07-01 13:03:59 +0200403 uint32_t allocated_bitrate = allocation[config.observer];
Florent Castelli4e615d52019-08-22 16:09:06 +0200404 uint32_t allocated_stable_target_rate =
405 stable_bitrate_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100406 BitrateAllocationUpdate update;
407 update.target_bitrate = DataRate::bps(allocated_bitrate);
Florent Castelli4e615d52019-08-22 16:09:06 +0200408 update.stable_target_bitrate = DataRate::bps(allocated_stable_target_rate);
Sebastian Jansson13e59032018-11-21 19:13:07 +0100409 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
410 update.round_trip_time = TimeDelta::ms(last_rtt_);
411 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
412 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200413
414 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200415 if (last_target_bps_ > 0)
mflodman48a4beb2016-07-01 13:03:59 +0200416 ++num_pause_events_;
417 // The protection bitrate is an estimate based on the ratio between media
418 // and protection used before this observer was muted.
419 uint32_t predicted_protection_bps =
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200420 (1.0 - config.media_ratio) * config.config.min_bitrate_bps;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100421 RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
422 << " with configured min bitrate "
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200423 << config.config.min_bitrate_bps
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200424 << " and current estimate of " << last_target_bps_
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200425 << " and protection bitrate "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100426 << predicted_protection_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200427 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200428 if (last_target_bps_ > 0)
mflodman48a4beb2016-07-01 13:03:59 +0200429 ++num_pause_events_;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100430 RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200431 << ", configured min bitrate "
432 << config.config.min_bitrate_bps
Mirko Bonadei675513b2017-11-09 11:09:25 +0100433 << ", current allocation " << allocated_bitrate
434 << " and protection bitrate " << protection_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200435 }
436
437 // Only update the media ratio if the observer got an allocation.
438 if (allocated_bitrate > 0)
439 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
440 config.allocated_bitrate_bps = allocated_bitrate;
441 }
philipel5ef2bc12017-02-21 07:28:31 -0800442 UpdateAllocationLimits();
Stefan Holmere5904162015-03-26 11:11:06 +0100443}
444
perkj57c21f92016-06-17 07:27:16 -0700445void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200446 MediaStreamAllocationConfig config) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200447 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200448 RTC_DCHECK_GT(config.bitrate_priority, 0);
449 RTC_DCHECK(std::isnormal(config.bitrate_priority));
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200450 auto it = absl::c_find_if(
451 allocatable_tracks_,
452 [observer](const auto& config) { return config.observer == observer; });
mflodman101f2502016-06-09 17:21:19 +0200453 // Update settings if the observer already exists, create a new one otherwise.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200454 if (it != allocatable_tracks_.end()) {
455 it->config = config;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000456 } else {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200457 allocatable_tracks_.push_back(AllocatableTrack(observer, config));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000458 }
Stefan Holmere5904162015-03-26 11:11:06 +0100459
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100460 if (last_target_bps_ > 0) {
mflodman101f2502016-06-09 17:21:19 +0200461 // Calculate a new allocation and update all observers.
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100462
Sebastian Jansson538ca572019-09-24 19:47:26 +0200463 auto allocation = AllocateBitrates(allocatable_tracks_, last_target_bps_);
464 auto stable_bitrate_allocation =
465 AllocateBitrates(allocatable_tracks_, last_stable_target_bps_);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200466 for (auto& config : allocatable_tracks_) {
mflodman48a4beb2016-07-01 13:03:59 +0200467 uint32_t allocated_bitrate = allocation[config.observer];
Florent Castelli4e615d52019-08-22 16:09:06 +0200468 uint32_t allocated_stable_bitrate =
469 stable_bitrate_allocation[config.observer];
Sebastian Jansson13e59032018-11-21 19:13:07 +0100470 BitrateAllocationUpdate update;
471 update.target_bitrate = DataRate::bps(allocated_bitrate);
Florent Castelli4e615d52019-08-22 16:09:06 +0200472 update.stable_target_bitrate = DataRate::bps(allocated_stable_bitrate);
Sebastian Jansson13e59032018-11-21 19:13:07 +0100473 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
474 update.round_trip_time = TimeDelta::ms(last_rtt_);
475 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
476 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
mflodman48a4beb2016-07-01 13:03:59 +0200477 config.allocated_bitrate_bps = allocated_bitrate;
478 if (allocated_bitrate > 0)
479 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
480 }
perkjfea93092016-05-14 00:58:48 -0700481 } else {
482 // Currently, an encoder is not allowed to produce frames.
483 // But we still have to return the initial config bitrate + let the
484 // observer know that it can not produce frames.
Sebastian Jansson13e59032018-11-21 19:13:07 +0100485
486 BitrateAllocationUpdate update;
487 update.target_bitrate = DataRate::Zero();
Florent Castelli4e615d52019-08-22 16:09:06 +0200488 update.stable_target_bitrate = DataRate::Zero();
Sebastian Jansson13e59032018-11-21 19:13:07 +0100489 update.packet_loss_ratio = last_fraction_loss_ / 256.0;
490 update.round_trip_time = TimeDelta::ms(last_rtt_);
491 update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
492 observer->OnBitrateUpdated(update);
Stefan Holmere5904162015-03-26 11:11:06 +0100493 }
perkj71ee44c2016-06-15 00:47:53 -0700494 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000495}
496
perkj71ee44c2016-06-15 00:47:53 -0700497void BitrateAllocator::UpdateAllocationLimits() {
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200498 BitrateAllocationLimits limits;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200499 for (const auto& config : allocatable_tracks_) {
500 uint32_t stream_padding = config.config.pad_up_bitrate_bps;
501 if (config.config.enforce_min_bitrate) {
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200502 limits.min_allocatable_rate +=
503 DataRate::bps(config.config.min_bitrate_bps);
philipel5ef2bc12017-02-21 07:28:31 -0800504 } else if (config.allocated_bitrate_bps == 0) {
505 stream_padding =
srte1eb051c2017-11-29 11:23:59 +0100506 std::max(config.MinBitrateWithHysteresis(), stream_padding);
perkj71ee44c2016-06-15 00:47:53 -0700507 }
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200508 limits.max_padding_rate += DataRate::bps(stream_padding);
509 limits.max_allocatable_rate += DataRate::bps(config.config.max_bitrate_bps);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000510 }
perkj71ee44c2016-06-15 00:47:53 -0700511
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200512 if (limits.min_allocatable_rate == current_limits_.min_allocatable_rate &&
513 limits.max_allocatable_rate == current_limits_.max_allocatable_rate &&
514 limits.max_padding_rate == current_limits_.max_padding_rate) {
philipel5ef2bc12017-02-21 07:28:31 -0800515 return;
516 }
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200517 current_limits_ = limits;
philipel5ef2bc12017-02-21 07:28:31 -0800518
Mirko Bonadei675513b2017-11-09 11:09:25 +0100519 RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200520 << ToString(limits.min_allocatable_rate)
521 << ", total_requested_padding_bitrate: "
522 << ToString(limits.max_padding_rate)
523 << ", total_requested_max_bitrate: "
524 << ToString(limits.max_allocatable_rate);
525
526 limit_observer_->OnAllocationLimitsChanged(limits);
perkj71ee44c2016-06-15 00:47:53 -0700527}
528
529void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200530 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200531 for (auto it = allocatable_tracks_.begin(); it != allocatable_tracks_.end();
532 ++it) {
533 if (it->observer == observer) {
534 allocatable_tracks_.erase(it);
535 break;
536 }
perkj71ee44c2016-06-15 00:47:53 -0700537 }
perkj26091b12016-09-01 01:17:40 -0700538
perkj71ee44c2016-06-15 00:47:53 -0700539 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000540}
541
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200542int BitrateAllocator::GetStartBitrate(
543 BitrateAllocatorObserver* observer) const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200544 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200545 auto it = absl::c_find_if(
546 allocatable_tracks_,
547 [observer](const auto& config) { return config.observer == observer; });
548 if (it == allocatable_tracks_.end()) {
mflodman48a4beb2016-07-01 13:03:59 +0200549 // This observer hasn't been added yet, just give it its fair share.
550 return last_non_zero_bitrate_bps_ /
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200551 static_cast<int>((allocatable_tracks_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200552 } else if (it->allocated_bitrate_bps == -1) {
553 // This observer hasn't received an allocation yet, so do the same.
554 return last_non_zero_bitrate_bps_ /
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200555 static_cast<int>(allocatable_tracks_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200556 } else {
557 // This observer already has an allocation.
558 return it->allocated_bitrate_bps;
559 }
perkj57c21f92016-06-17 07:27:16 -0700560}
561
Sebastian Jansson538ca572019-09-24 19:47:26 +0200562uint32_t bitrate_allocator_impl::AllocatableTrack::LastAllocatedBitrate()
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200563 const {
mflodman101f2502016-06-09 17:21:19 +0200564 // Return the configured minimum bitrate for newly added observers, to avoid
565 // requiring an extra high bitrate for the observer to get an allocated
566 // bitrate.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200567 return allocated_bitrate_bps == -1 ? config.min_bitrate_bps
568 : allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200569}
570
Sebastian Jansson538ca572019-09-24 19:47:26 +0200571uint32_t bitrate_allocator_impl::AllocatableTrack::MinBitrateWithHysteresis()
572 const {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200573 uint32_t min_bitrate = config.min_bitrate_bps;
srte1eb051c2017-11-29 11:23:59 +0100574 if (LastAllocatedBitrate() == 0) {
mflodman101f2502016-06-09 17:21:19 +0200575 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
576 kMinToggleBitrateBps);
577 }
mflodman48a4beb2016-07-01 13:03:59 +0200578 // Account for protection bitrate used by this observer in the previous
579 // allocation.
580 // Note: the ratio will only be updated when the stream is active, meaning a
581 // paused stream won't get any ratio updates. This might lead to waiting a bit
582 // longer than necessary if the network condition improves, but this is to
583 // avoid too much toggling.
srte1eb051c2017-11-29 11:23:59 +0100584 if (media_ratio > 0.0 && media_ratio < 1.0)
585 min_bitrate += min_bitrate * (1.0 - media_ratio);
mflodman48a4beb2016-07-01 13:03:59 +0200586
mflodman101f2502016-06-09 17:21:19 +0200587 return min_bitrate;
588}
589
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000590} // namespace webrtc