blob: 34b06b1984e34fbf179b3684166c20642843ea99 [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
mflodman0e7e2592015-11-12 21:02:42 -080012#include "webrtc/call/bitrate_allocator.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000013
14#include <algorithm>
15#include <utility>
16
mflodman2ebe5b12016-05-13 01:43:51 -070017#include "webrtc/base/checks.h"
mflodman48a4beb2016-07-01 13:03:59 +020018#include "webrtc/base/logging.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000019#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
mflodman48a4beb2016-07-01 13:03:59 +020020#include "webrtc/system_wrappers/include/clock.h"
21#include "webrtc/system_wrappers/include/metrics.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000022
23namespace webrtc {
24
Stefan Holmere5904162015-03-26 11:11:06 +010025// Allow packets to be transmitted in up to 2 times max video bitrate if the
26// bandwidth estimate allows it.
27const int kTransmissionMaxBitrateMultiplier = 2;
28const int kDefaultBitrateBps = 300000;
29
mflodman101f2502016-06-09 17:21:19 +020030// Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
31const double kToggleFactor = 0.1;
32const uint32_t kMinToggleBitrateBps = 20000;
33
mflodman48a4beb2016-07-01 13:03:59 +020034const int64_t kBweLogIntervalMs = 5000;
35
36namespace {
37
38double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
39 RTC_DCHECK_GT(allocated_bitrate, 0u);
40 if (protection_bitrate == 0)
41 return 1.0;
42
43 uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
44 return media_bitrate / static_cast<double>(allocated_bitrate);
45}
46} // namespace
47
perkj71ee44c2016-06-15 00:47:53 -070048BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
49 : limit_observer_(limit_observer),
50 bitrate_observer_configs_(),
Stefan Holmere5904162015-03-26 11:11:06 +010051 last_bitrate_bps_(kDefaultBitrateBps),
perkjfea93092016-05-14 00:58:48 -070052 last_non_zero_bitrate_bps_(kDefaultBitrateBps),
Stefan Holmere5904162015-03-26 11:11:06 +010053 last_fraction_loss_(0),
mflodman48a4beb2016-07-01 13:03:59 +020054 last_rtt_(0),
55 num_pause_events_(0),
56 clock_(Clock::GetRealTimeClock()),
perkj8eb37a32016-08-16 02:40:55 -070057 last_bwe_log_time_(0) {}
mflodman48a4beb2016-07-01 13:03:59 +020058
59BitrateAllocator::~BitrateAllocator() {
60 RTC_LOGGED_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
61 num_pause_events_);
62}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000063
perkj71ee44c2016-06-15 00:47:53 -070064void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps,
65 uint8_t fraction_loss,
66 int64_t rtt) {
perkj8eb37a32016-08-16 02:40:55 -070067 rtc::CritScope lock(&crit_sect_);
mflodman101f2502016-06-09 17:21:19 +020068 last_bitrate_bps_ = target_bitrate_bps;
perkjfea93092016-05-14 00:58:48 -070069 last_non_zero_bitrate_bps_ =
mflodman101f2502016-06-09 17:21:19 +020070 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_;
Stefan Holmere5904162015-03-26 11:11:06 +010071 last_fraction_loss_ = fraction_loss;
72 last_rtt_ = rtt;
mflodman2ebe5b12016-05-13 01:43:51 -070073
mflodman48a4beb2016-07-01 13:03:59 +020074 // Periodically log the incoming BWE.
75 int64_t now = clock_->TimeInMilliseconds();
76 if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
77 LOG(LS_INFO) << "Current BWE " << target_bitrate_bps;
78 last_bwe_log_time_ = now;
sprang2f48d942015-11-05 04:25:49 -080079 }
mflodman48a4beb2016-07-01 13:03:59 +020080
81 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps);
82
83 for (auto& config : bitrate_observer_configs_) {
84 uint32_t allocated_bitrate = allocation[config.observer];
85 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
86 allocated_bitrate, last_fraction_loss_, last_rtt_);
87
88 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
89 if (target_bitrate_bps > 0)
90 ++num_pause_events_;
91 // The protection bitrate is an estimate based on the ratio between media
92 // and protection used before this observer was muted.
93 uint32_t predicted_protection_bps =
94 (1.0 - config.media_ratio) * config.min_bitrate_bps;
95 LOG(LS_INFO) << "Pausing observer " << config.observer
96 << " with configured min bitrate " << config.min_bitrate_bps
97 << " and current estimate of " << target_bitrate_bps
98 << " and protection bitrate " << predicted_protection_bps;
99 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
100 if (target_bitrate_bps > 0)
101 ++num_pause_events_;
102 LOG(LS_INFO) << "Resuming observer " << config.observer
103 << ", configured min bitrate " << config.min_bitrate_bps
104 << ", current allocation " << allocated_bitrate
105 << " and protection bitrate " << protection_bitrate;
106 }
107
108 // Only update the media ratio if the observer got an allocation.
109 if (allocated_bitrate > 0)
110 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
111 config.allocated_bitrate_bps = allocated_bitrate;
112 }
Stefan Holmere5904162015-03-26 11:11:06 +0100113}
114
perkj57c21f92016-06-17 07:27:16 -0700115void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
116 uint32_t min_bitrate_bps,
117 uint32_t max_bitrate_bps,
118 uint32_t pad_up_bitrate_bps,
119 bool enforce_min_bitrate) {
perkj8eb37a32016-08-16 02:40:55 -0700120 rtc::CritScope lock(&crit_sect_);
mflodman2ebe5b12016-05-13 01:43:51 -0700121 auto it = FindObserverConfig(observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000122
mflodman101f2502016-06-09 17:21:19 +0200123 // Update settings if the observer already exists, create a new one otherwise.
mflodman2ebe5b12016-05-13 01:43:51 -0700124 if (it != bitrate_observer_configs_.end()) {
mflodman2ebe5b12016-05-13 01:43:51 -0700125 it->min_bitrate_bps = min_bitrate_bps;
126 it->max_bitrate_bps = max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700127 it->pad_up_bitrate_bps = pad_up_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200128 it->enforce_min_bitrate = enforce_min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000129 } else {
perkj71ee44c2016-06-15 00:47:53 -0700130 bitrate_observer_configs_.push_back(
131 ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps,
132 pad_up_bitrate_bps, enforce_min_bitrate));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000133 }
Stefan Holmere5904162015-03-26 11:11:06 +0100134
mflodman101f2502016-06-09 17:21:19 +0200135 ObserverAllocation allocation;
136 if (last_bitrate_bps_ > 0) {
137 // Calculate a new allocation and update all observers.
138 allocation = AllocateBitrates(last_bitrate_bps_);
mflodman48a4beb2016-07-01 13:03:59 +0200139 for (auto& config : bitrate_observer_configs_) {
140 uint32_t allocated_bitrate = allocation[config.observer];
141 uint32_t protection_bitrate = config.observer->OnBitrateUpdated(
142 allocated_bitrate, last_fraction_loss_, last_rtt_);
143 config.allocated_bitrate_bps = allocated_bitrate;
144 if (allocated_bitrate > 0)
145 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
146 }
perkjfea93092016-05-14 00:58:48 -0700147 } else {
148 // Currently, an encoder is not allowed to produce frames.
149 // But we still have to return the initial config bitrate + let the
150 // observer know that it can not produce frames.
mflodman101f2502016-06-09 17:21:19 +0200151 allocation = AllocateBitrates(last_non_zero_bitrate_bps_);
perkjfea93092016-05-14 00:58:48 -0700152 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_);
Stefan Holmere5904162015-03-26 11:11:06 +0100153 }
perkj71ee44c2016-06-15 00:47:53 -0700154 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000155}
156
perkj71ee44c2016-06-15 00:47:53 -0700157void BitrateAllocator::UpdateAllocationLimits() {
158 uint32_t total_requested_padding_bitrate = 0;
159 uint32_t total_requested_min_bitrate = 0;
160
perkj8eb37a32016-08-16 02:40:55 -0700161 {
162 rtc::CritScope lock(&crit_sect_);
163 for (const auto& config : bitrate_observer_configs_) {
164 if (config.enforce_min_bitrate) {
165 total_requested_min_bitrate += config.min_bitrate_bps;
166 }
167 total_requested_padding_bitrate += config.pad_up_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700168 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000169 }
perkj71ee44c2016-06-15 00:47:53 -0700170
perkj9b522f82016-07-07 00:36:28 -0700171 LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
172 << total_requested_min_bitrate
173 << "bps, total_requested_padding_bitrate: "
174 << total_requested_padding_bitrate << "bps";
perkj71ee44c2016-06-15 00:47:53 -0700175 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate,
176 total_requested_padding_bitrate);
177}
178
179void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
perkj8eb37a32016-08-16 02:40:55 -0700180 {
181 rtc::CritScope lock(&crit_sect_);
182 auto it = FindObserverConfig(observer);
183 if (it != bitrate_observer_configs_.end()) {
184 bitrate_observer_configs_.erase(it);
185 }
perkj71ee44c2016-06-15 00:47:53 -0700186 }
187 UpdateAllocationLimits();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000188}
189
perkj57c21f92016-06-17 07:27:16 -0700190int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) {
perkj8eb37a32016-08-16 02:40:55 -0700191 rtc::CritScope lock(&crit_sect_);
mflodman48a4beb2016-07-01 13:03:59 +0200192 const auto& it = FindObserverConfig(observer);
193 if (it == bitrate_observer_configs_.end()) {
194 // This observer hasn't been added yet, just give it its fair share.
195 return last_non_zero_bitrate_bps_ /
perkj8eb37a32016-08-16 02:40:55 -0700196 static_cast<int>((bitrate_observer_configs_.size() + 1));
mflodman48a4beb2016-07-01 13:03:59 +0200197 } else if (it->allocated_bitrate_bps == -1) {
198 // This observer hasn't received an allocation yet, so do the same.
199 return last_non_zero_bitrate_bps_ /
perkj8eb37a32016-08-16 02:40:55 -0700200 static_cast<int>(bitrate_observer_configs_.size());
mflodman48a4beb2016-07-01 13:03:59 +0200201 } else {
202 // This observer already has an allocation.
203 return it->allocated_bitrate_bps;
204 }
perkj57c21f92016-06-17 07:27:16 -0700205}
206
mflodman48a4beb2016-07-01 13:03:59 +0200207BitrateAllocator::ObserverConfigs::iterator
perkj8eb37a32016-08-16 02:40:55 -0700208BitrateAllocator::FindObserverConfig(
209 const BitrateAllocatorObserver* observer) {
mflodman2ebe5b12016-05-13 01:43:51 -0700210 for (auto it = bitrate_observer_configs_.begin();
211 it != bitrate_observer_configs_.end(); ++it) {
212 if (it->observer == observer)
213 return it;
214 }
215 return bitrate_observer_configs_.end();
216}
217
perkjfea93092016-05-14 00:58:48 -0700218BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates(
219 uint32_t bitrate) {
mflodman2ebe5b12016-05-13 01:43:51 -0700220 if (bitrate_observer_configs_.empty())
221 return ObserverAllocation();
222
perkjfea93092016-05-14 00:58:48 -0700223 if (bitrate == 0)
mflodman2ebe5b12016-05-13 01:43:51 -0700224 return ZeroRateAllocation();
225
226 uint32_t sum_min_bitrates = 0;
mflodman101f2502016-06-09 17:21:19 +0200227 uint32_t sum_max_bitrates = 0;
228 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman2ebe5b12016-05-13 01:43:51 -0700229 sum_min_bitrates += observer_config.min_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200230 sum_max_bitrates += observer_config.max_bitrate_bps;
231 }
232
233 // Not enough for all observers to get an allocation, allocate according to:
234 // enforced min bitrate -> allocated bitrate previous round -> restart paused
235 // streams.
236 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates))
perkjfea93092016-05-14 00:58:48 -0700237 return LowRateAllocation(bitrate);
mflodman2ebe5b12016-05-13 01:43:51 -0700238
mflodman101f2502016-06-09 17:21:19 +0200239 // All observers will get their min bitrate plus an even share of the rest.
240 if (bitrate <= sum_max_bitrates)
241 return NormalRateAllocation(bitrate, sum_min_bitrates);
mflodman2ebe5b12016-05-13 01:43:51 -0700242
mflodman101f2502016-06-09 17:21:19 +0200243 // All observers will get up to kTransmissionMaxBitrateMultiplier x max.
244 return MaxRateAllocation(bitrate, sum_max_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000245}
246
mflodman2ebe5b12016-05-13 01:43:51 -0700247BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
248 ObserverAllocation allocation;
mflodman2ebe5b12016-05-13 01:43:51 -0700249 for (const auto& observer_config : bitrate_observer_configs_)
250 allocation[observer_config.observer] = 0;
perkjec81bcd2016-05-11 06:01:13 -0700251 return allocation;
252}
253
mflodman2ebe5b12016-05-13 01:43:51 -0700254BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
Stefan Holmere5904162015-03-26 11:11:06 +0100255 uint32_t bitrate) {
mflodman2ebe5b12016-05-13 01:43:51 -0700256 ObserverAllocation allocation;
perkj8eb37a32016-08-16 02:40:55 -0700257
mflodman101f2502016-06-09 17:21:19 +0200258 // Start by allocating bitrate to observers enforcing a min bitrate, hence
259 // remaining_bitrate might turn negative.
260 int64_t remaining_bitrate = bitrate;
261 for (const auto& observer_config : bitrate_observer_configs_) {
262 int32_t allocated_bitrate = 0;
263 if (observer_config.enforce_min_bitrate)
264 allocated_bitrate = observer_config.min_bitrate_bps;
265
266 allocation[observer_config.observer] = allocated_bitrate;
267 remaining_bitrate -= allocated_bitrate;
268 }
269
270 // Allocate bitrate to all previously active streams.
271 if (remaining_bitrate > 0) {
mflodman2ebe5b12016-05-13 01:43:51 -0700272 for (const auto& observer_config : bitrate_observer_configs_) {
mflodman101f2502016-06-09 17:21:19 +0200273 if (observer_config.enforce_min_bitrate ||
274 LastAllocatedBitrate(observer_config) == 0)
275 continue;
276
mflodman48a4beb2016-07-01 13:03:59 +0200277 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
278 if (remaining_bitrate >= required_bitrate) {
279 allocation[observer_config.observer] = required_bitrate;
280 remaining_bitrate -= required_bitrate;
mflodman101f2502016-06-09 17:21:19 +0200281 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000282 }
283 }
mflodman101f2502016-06-09 17:21:19 +0200284
285 // Allocate bitrate to previously paused streams.
286 if (remaining_bitrate > 0) {
287 for (const auto& observer_config : bitrate_observer_configs_) {
288 if (LastAllocatedBitrate(observer_config) != 0)
289 continue;
290
291 // Add a hysteresis to avoid toggling.
292 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config);
293 if (remaining_bitrate >= required_bitrate) {
294 allocation[observer_config.observer] = required_bitrate;
295 remaining_bitrate -= required_bitrate;
296 }
297 }
298 }
299
300 // Split a possible remainder evenly on all streams with an allocation.
301 if (remaining_bitrate > 0)
302 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation);
303
304 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size());
Stefan Holmere5904162015-03-26 11:11:06 +0100305 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000306}
mflodman101f2502016-06-09 17:21:19 +0200307
308BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
309 uint32_t bitrate,
310 uint32_t sum_min_bitrates) {
perkj8eb37a32016-08-16 02:40:55 -0700311
mflodman101f2502016-06-09 17:21:19 +0200312 ObserverAllocation allocation;
313 for (const auto& observer_config : bitrate_observer_configs_)
314 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
315
316 bitrate -= sum_min_bitrates;
317 if (bitrate > 0)
318 DistributeBitrateEvenly(bitrate, true, 1, &allocation);
319
320 return allocation;
321}
322
323BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation(
perkj8eb37a32016-08-16 02:40:55 -0700324 uint32_t bitrate, uint32_t sum_max_bitrates) {
mflodman101f2502016-06-09 17:21:19 +0200325 ObserverAllocation allocation;
326
327 for (const auto& observer_config : bitrate_observer_configs_) {
328 allocation[observer_config.observer] = observer_config.max_bitrate_bps;
329 bitrate -= observer_config.max_bitrate_bps;
330 }
331 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier,
332 &allocation);
333 return allocation;
334}
335
336uint32_t BitrateAllocator::LastAllocatedBitrate(
337 const ObserverConfig& observer_config) {
perkj8eb37a32016-08-16 02:40:55 -0700338
mflodman101f2502016-06-09 17:21:19 +0200339 // Return the configured minimum bitrate for newly added observers, to avoid
340 // requiring an extra high bitrate for the observer to get an allocated
341 // bitrate.
perkj8eb37a32016-08-16 02:40:55 -0700342 return observer_config.allocated_bitrate_bps == -1 ?
343 observer_config.min_bitrate_bps : observer_config.allocated_bitrate_bps;
mflodman101f2502016-06-09 17:21:19 +0200344}
345
346uint32_t BitrateAllocator::MinBitrateWithHysteresis(
347 const ObserverConfig& observer_config) {
348 uint32_t min_bitrate = observer_config.min_bitrate_bps;
349 if (LastAllocatedBitrate(observer_config) == 0) {
350 min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
351 kMinToggleBitrateBps);
352 }
mflodman48a4beb2016-07-01 13:03:59 +0200353 // Account for protection bitrate used by this observer in the previous
354 // allocation.
355 // Note: the ratio will only be updated when the stream is active, meaning a
356 // paused stream won't get any ratio updates. This might lead to waiting a bit
357 // longer than necessary if the network condition improves, but this is to
358 // avoid too much toggling.
359 if (observer_config.media_ratio > 0.0 && observer_config.media_ratio < 1.0)
360 min_bitrate += min_bitrate * (1.0 - observer_config.media_ratio);
361
mflodman101f2502016-06-09 17:21:19 +0200362 return min_bitrate;
363}
364
365void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate,
366 bool include_zero_allocations,
367 int max_multiplier,
368 ObserverAllocation* allocation) {
369 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size());
370
371 ObserverSortingMap list_max_bitrates;
372 for (const auto& observer_config : bitrate_observer_configs_) {
373 if (include_zero_allocations ||
374 allocation->at(observer_config.observer) != 0) {
375 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
376 observer_config.max_bitrate_bps, &observer_config));
377 }
378 }
379 auto it = list_max_bitrates.begin();
380 while (it != list_max_bitrates.end()) {
381 RTC_DCHECK_GT(bitrate, 0u);
382 uint32_t extra_allocation =
383 bitrate / static_cast<uint32_t>(list_max_bitrates.size());
384 uint32_t total_allocation =
385 extra_allocation + allocation->at(it->second->observer);
386 bitrate -= extra_allocation;
387 if (total_allocation > max_multiplier * it->first) {
388 // There is more than we can fit for this observer, carry over to the
389 // remaining observers.
390 bitrate += total_allocation - max_multiplier * it->first;
391 total_allocation = max_multiplier * it->first;
392 }
393 // Finally, update the allocation for this observer.
394 allocation->at(it->second->observer) = total_allocation;
395 it = list_max_bitrates.erase(it);
396 }
397}
398
399bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate,
400 uint32_t sum_min_bitrates) {
401 if (bitrate < sum_min_bitrates)
402 return false;
403
perkj8eb37a32016-08-16 02:40:55 -0700404 uint32_t extra_bitrate_per_observer = (bitrate - sum_min_bitrates) /
mflodman101f2502016-06-09 17:21:19 +0200405 static_cast<uint32_t>(bitrate_observer_configs_.size());
406 for (const auto& observer_config : bitrate_observer_configs_) {
407 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer <
408 MinBitrateWithHysteresis(observer_config))
409 return false;
410 }
411 return true;
412}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000413} // namespace webrtc