2015-03-04 12:24:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2015-11-12 21:02:42 -08:00
|
|
|
#include "webrtc/call/bitrate_allocator.h"
|
2015-03-04 12:24:26 +00:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
#include "webrtc/base/checks.h"
|
2015-03-04 12:24:26 +00:00
|
|
|
#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2015-03-26 11:11:06 +01:00
|
|
|
// Allow packets to be transmitted in up to 2 times max video bitrate if the
|
|
|
|
|
// bandwidth estimate allows it.
|
|
|
|
|
const int kTransmissionMaxBitrateMultiplier = 2;
|
|
|
|
|
const int kDefaultBitrateBps = 300000;
|
|
|
|
|
|
2015-03-04 12:24:26 +00:00
|
|
|
BitrateAllocator::BitrateAllocator()
|
2016-05-13 01:43:51 -07:00
|
|
|
: bitrate_observer_configs_(),
|
2015-03-26 11:11:06 +01:00
|
|
|
enforce_min_bitrate_(true),
|
|
|
|
|
last_bitrate_bps_(kDefaultBitrateBps),
|
|
|
|
|
last_fraction_loss_(0),
|
2015-11-05 04:25:49 -08:00
|
|
|
last_rtt_(0) {}
|
2015-03-04 12:24:26 +00:00
|
|
|
|
2015-11-05 04:25:49 -08:00
|
|
|
uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate,
|
|
|
|
|
uint8_t fraction_loss,
|
|
|
|
|
int64_t rtt) {
|
2016-01-20 02:32:54 -08:00
|
|
|
rtc::CritScope lock(&crit_sect_);
|
2015-03-26 11:11:06 +01:00
|
|
|
last_bitrate_bps_ = bitrate;
|
|
|
|
|
last_fraction_loss_ = fraction_loss;
|
|
|
|
|
last_rtt_ = rtt;
|
2016-05-13 01:43:51 -07:00
|
|
|
|
2015-11-05 04:25:49 -08:00
|
|
|
uint32_t allocated_bitrate_bps = 0;
|
2016-05-13 01:43:51 -07:00
|
|
|
ObserverAllocation allocation = AllocateBitrates();
|
2015-11-05 04:25:49 -08:00
|
|
|
for (const auto& kv : allocation) {
|
2016-03-11 15:44:32 +01:00
|
|
|
kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_);
|
2015-11-05 04:25:49 -08:00
|
|
|
allocated_bitrate_bps += kv.second;
|
|
|
|
|
}
|
|
|
|
|
return allocated_bitrate_bps;
|
2015-03-26 11:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-11 15:44:32 +01:00
|
|
|
int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
|
|
|
|
|
uint32_t min_bitrate_bps,
|
2016-05-13 01:43:51 -07:00
|
|
|
uint32_t max_bitrate_bps,
|
|
|
|
|
bool enforce_min_bitrate) {
|
2016-01-20 02:32:54 -08:00
|
|
|
rtc::CritScope lock(&crit_sect_);
|
2016-05-13 01:43:51 -07:00
|
|
|
// TODO(mflodman): Enforce this per observer.
|
|
|
|
|
EnforceMinBitrate(enforce_min_bitrate);
|
2015-03-04 12:24:26 +00:00
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
auto it = FindObserverConfig(observer);
|
2015-03-04 12:24:26 +00:00
|
|
|
|
2015-03-26 11:11:06 +01:00
|
|
|
// Allow the max bitrate to be exceeded for FEC and retransmissions.
|
|
|
|
|
// TODO(holmer): We have to get rid of this hack as it makes it difficult to
|
|
|
|
|
// properly allocate bitrate. The allocator should instead distribute any
|
|
|
|
|
// extra bitrate after all streams have maxed out.
|
|
|
|
|
max_bitrate_bps *= kTransmissionMaxBitrateMultiplier;
|
2016-05-13 01:43:51 -07:00
|
|
|
if (it != bitrate_observer_configs_.end()) {
|
2015-03-04 12:24:26 +00:00
|
|
|
// Update current configuration.
|
2016-05-13 01:43:51 -07:00
|
|
|
it->min_bitrate_bps = min_bitrate_bps;
|
|
|
|
|
it->max_bitrate_bps = max_bitrate_bps;
|
2015-03-04 12:24:26 +00:00
|
|
|
} else {
|
|
|
|
|
// Add new settings.
|
2016-05-13 01:43:51 -07:00
|
|
|
bitrate_observer_configs_.push_back(ObserverConfig(
|
|
|
|
|
observer, min_bitrate_bps, max_bitrate_bps, enforce_min_bitrate));
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
2015-03-26 11:11:06 +01:00
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
ObserverAllocation allocation = AllocateBitrates();
|
2015-09-15 15:08:03 +02:00
|
|
|
int new_observer_bitrate_bps = 0;
|
2015-03-26 11:11:06 +01:00
|
|
|
for (auto& kv : allocation) {
|
2016-03-11 15:44:32 +01:00
|
|
|
kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_);
|
2015-03-26 11:11:06 +01:00
|
|
|
if (kv.first == observer)
|
2015-09-15 15:08:03 +02:00
|
|
|
new_observer_bitrate_bps = kv.second;
|
2015-03-26 11:11:06 +01:00
|
|
|
}
|
2015-09-15 15:08:03 +02:00
|
|
|
return new_observer_bitrate_bps;
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-11 15:44:32 +01:00
|
|
|
void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
|
2016-01-20 02:32:54 -08:00
|
|
|
rtc::CritScope lock(&crit_sect_);
|
2016-05-13 01:43:51 -07:00
|
|
|
auto it = FindObserverConfig(observer);
|
|
|
|
|
if (it != bitrate_observer_configs_.end()) {
|
|
|
|
|
bitrate_observer_configs_.erase(it);
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) {
|
|
|
|
|
enforce_min_bitrate_ = enforce_min_bitrate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BitrateAllocator::ObserverConfigList::iterator
|
|
|
|
|
BitrateAllocator::FindObserverConfig(
|
2016-03-11 15:44:32 +01:00
|
|
|
const BitrateAllocatorObserver* observer) {
|
2016-05-13 01:43:51 -07:00
|
|
|
for (auto it = bitrate_observer_configs_.begin();
|
|
|
|
|
it != bitrate_observer_configs_.end(); ++it) {
|
|
|
|
|
if (it->observer == observer)
|
2015-03-04 12:24:26 +00:00
|
|
|
return it;
|
|
|
|
|
}
|
2016-05-13 01:43:51 -07:00
|
|
|
return bitrate_observer_configs_.end();
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates() {
|
|
|
|
|
if (bitrate_observer_configs_.empty())
|
|
|
|
|
return ObserverAllocation();
|
|
|
|
|
|
|
|
|
|
if (last_bitrate_bps_ == 0)
|
|
|
|
|
return ZeroRateAllocation();
|
|
|
|
|
|
|
|
|
|
uint32_t sum_min_bitrates = 0;
|
|
|
|
|
for (const auto& observer_config : bitrate_observer_configs_)
|
|
|
|
|
sum_min_bitrates += observer_config.min_bitrate_bps;
|
|
|
|
|
if (last_bitrate_bps_ <= sum_min_bitrates)
|
|
|
|
|
return LowRateAllocation(last_bitrate_bps_);
|
|
|
|
|
|
|
|
|
|
return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates);
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
|
2015-03-26 11:11:06 +01:00
|
|
|
uint32_t bitrate,
|
|
|
|
|
uint32_t sum_min_bitrates) {
|
2016-05-13 01:43:51 -07:00
|
|
|
uint32_t num_remaining_observers =
|
|
|
|
|
static_cast<uint32_t>(bitrate_observer_configs_.size());
|
|
|
|
|
RTC_DCHECK_GT(num_remaining_observers, 0u);
|
|
|
|
|
|
2015-03-04 12:24:26 +00:00
|
|
|
uint32_t bitrate_per_observer =
|
2016-05-13 01:43:51 -07:00
|
|
|
(bitrate - sum_min_bitrates) / num_remaining_observers;
|
2015-03-04 12:24:26 +00:00
|
|
|
// Use map to sort list based on max bitrate.
|
|
|
|
|
ObserverSortingMap list_max_bitrates;
|
2016-05-13 01:43:51 -07:00
|
|
|
for (const auto& config : bitrate_observer_configs_) {
|
|
|
|
|
list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
|
|
|
|
|
config.max_bitrate_bps, &config));
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
2016-05-13 01:43:51 -07:00
|
|
|
|
|
|
|
|
ObserverAllocation allocation;
|
2015-03-04 12:24:26 +00:00
|
|
|
ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
|
|
|
|
|
while (max_it != list_max_bitrates.end()) {
|
2016-05-13 01:43:51 -07:00
|
|
|
num_remaining_observers--;
|
2015-03-04 12:24:26 +00:00
|
|
|
uint32_t observer_allowance =
|
2016-05-13 01:43:51 -07:00
|
|
|
max_it->second->min_bitrate_bps + bitrate_per_observer;
|
2015-03-04 12:24:26 +00:00
|
|
|
if (max_it->first < observer_allowance) {
|
|
|
|
|
// We have more than enough for this observer.
|
|
|
|
|
// Carry the remainder forward.
|
|
|
|
|
uint32_t remainder = observer_allowance - max_it->first;
|
2016-05-13 01:43:51 -07:00
|
|
|
if (num_remaining_observers != 0)
|
|
|
|
|
bitrate_per_observer += remainder / num_remaining_observers;
|
|
|
|
|
allocation[max_it->second->observer] = max_it->first;
|
2015-03-04 12:24:26 +00:00
|
|
|
} else {
|
2016-05-13 01:43:51 -07:00
|
|
|
allocation[max_it->second->observer] = observer_allowance;
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
list_max_bitrates.erase(max_it);
|
|
|
|
|
// Prepare next iteration.
|
|
|
|
|
max_it = list_max_bitrates.begin();
|
|
|
|
|
}
|
2015-03-26 11:11:06 +01:00
|
|
|
return allocation;
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
|
|
|
|
|
ObserverAllocation allocation;
|
2016-05-11 06:01:13 -07:00
|
|
|
// Zero bitrate to all observers.
|
2016-05-13 01:43:51 -07:00
|
|
|
for (const auto& observer_config : bitrate_observer_configs_)
|
|
|
|
|
allocation[observer_config.observer] = 0;
|
2016-05-11 06:01:13 -07:00
|
|
|
return allocation;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
|
2015-03-26 11:11:06 +01:00
|
|
|
uint32_t bitrate) {
|
2016-05-13 01:43:51 -07:00
|
|
|
ObserverAllocation allocation;
|
2015-03-04 12:24:26 +00:00
|
|
|
if (enforce_min_bitrate_) {
|
|
|
|
|
// Min bitrate to all observers.
|
2016-05-13 01:43:51 -07:00
|
|
|
for (const auto& observer_config : bitrate_observer_configs_)
|
|
|
|
|
allocation[observer_config.observer] = observer_config.min_bitrate_bps;
|
2015-03-04 12:24:26 +00:00
|
|
|
} else {
|
2016-05-13 01:43:51 -07:00
|
|
|
// Allocate up to |min_bitrate_bps| to one observer at a time, until
|
2015-03-04 12:24:26 +00:00
|
|
|
// |bitrate| is depleted.
|
|
|
|
|
uint32_t remainder = bitrate;
|
2016-05-13 01:43:51 -07:00
|
|
|
for (const auto& observer_config : bitrate_observer_configs_) {
|
2015-03-26 11:11:06 +01:00
|
|
|
uint32_t allocated_bitrate =
|
2016-05-13 01:43:51 -07:00
|
|
|
std::min(remainder, observer_config.min_bitrate_bps);
|
|
|
|
|
allocation[observer_config.observer] = allocated_bitrate;
|
2015-03-26 11:11:06 +01:00
|
|
|
remainder -= allocated_bitrate;
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-26 11:11:06 +01:00
|
|
|
return allocation;
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
} // namespace webrtc
|