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>
|
|
|
|
|
|
|
|
|
|
#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-01-20 02:32:54 -08:00
|
|
|
: bitrate_observers_(),
|
2015-11-05 04:25:49 -08:00
|
|
|
bitrate_observers_modified_(false),
|
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;
|
2015-11-05 04:25:49 -08:00
|
|
|
uint32_t allocated_bitrate_bps = 0;
|
2015-03-26 11:11:06 +01:00
|
|
|
ObserverBitrateMap 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BitrateAllocator::ObserverBitrateMap BitrateAllocator::AllocateBitrates() {
|
2015-03-04 12:24:26 +00:00
|
|
|
if (bitrate_observers_.empty())
|
2015-03-26 11:11:06 +01:00
|
|
|
return ObserverBitrateMap();
|
2015-03-04 12:24:26 +00:00
|
|
|
|
|
|
|
|
uint32_t sum_min_bitrates = 0;
|
2015-03-26 11:11:06 +01:00
|
|
|
for (const auto& observer : bitrate_observers_)
|
2015-09-15 15:08:03 +02:00
|
|
|
sum_min_bitrates += observer.second.min_bitrate;
|
2016-05-09 04:57:11 -07:00
|
|
|
if (last_bitrate_bps_ <= sum_min_bitrates)
|
2015-03-26 11:11:06 +01:00
|
|
|
return LowRateAllocation(last_bitrate_bps_);
|
2015-03-04 12:24:26 +00:00
|
|
|
else
|
2015-03-26 11:11:06 +01:00
|
|
|
return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates);
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-11 15:44:32 +01:00
|
|
|
int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
|
|
|
|
|
uint32_t min_bitrate_bps,
|
|
|
|
|
uint32_t max_bitrate_bps) {
|
2016-01-20 02:32:54 -08:00
|
|
|
rtc::CritScope lock(&crit_sect_);
|
2015-03-04 12:24:26 +00:00
|
|
|
|
|
|
|
|
BitrateObserverConfList::iterator it =
|
|
|
|
|
FindObserverConfigurationPair(observer);
|
|
|
|
|
|
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;
|
2015-03-04 12:24:26 +00:00
|
|
|
if (it != bitrate_observers_.end()) {
|
|
|
|
|
// Update current configuration.
|
2015-09-15 15:08:03 +02:00
|
|
|
it->second.min_bitrate = min_bitrate_bps;
|
|
|
|
|
it->second.max_bitrate = max_bitrate_bps;
|
2015-03-04 12:24:26 +00:00
|
|
|
} else {
|
|
|
|
|
// Add new settings.
|
|
|
|
|
bitrate_observers_.push_back(BitrateObserverConfiguration(
|
2015-09-15 15:08:03 +02:00
|
|
|
observer, BitrateConfiguration(min_bitrate_bps, max_bitrate_bps)));
|
2015-03-04 12:24:26 +00:00
|
|
|
bitrate_observers_modified_ = true;
|
|
|
|
|
}
|
2015-03-26 11:11:06 +01:00
|
|
|
|
|
|
|
|
ObserverBitrateMap 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_);
|
2015-03-04 12:24:26 +00:00
|
|
|
BitrateObserverConfList::iterator it =
|
|
|
|
|
FindObserverConfigurationPair(observer);
|
|
|
|
|
if (it != bitrate_observers_.end()) {
|
|
|
|
|
bitrate_observers_.erase(it);
|
|
|
|
|
bitrate_observers_modified_ = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 04:57:11 -07:00
|
|
|
void BitrateAllocator::GetMinMaxBitrateSumBps(int* min_bitrate_sum_bps,
|
|
|
|
|
int* max_bitrate_sum_bps) const {
|
|
|
|
|
*min_bitrate_sum_bps = 0;
|
|
|
|
|
*max_bitrate_sum_bps = 0;
|
|
|
|
|
|
|
|
|
|
rtc::CritScope lock(&crit_sect_);
|
|
|
|
|
for (const auto& observer : bitrate_observers_) {
|
|
|
|
|
*min_bitrate_sum_bps += observer.second.min_bitrate;
|
|
|
|
|
*max_bitrate_sum_bps += observer.second.max_bitrate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-04 12:24:26 +00:00
|
|
|
BitrateAllocator::BitrateObserverConfList::iterator
|
|
|
|
|
BitrateAllocator::FindObserverConfigurationPair(
|
2016-03-11 15:44:32 +01:00
|
|
|
const BitrateAllocatorObserver* observer) {
|
2015-03-26 11:11:06 +01:00
|
|
|
for (auto it = bitrate_observers_.begin(); it != bitrate_observers_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
if (it->first == observer)
|
2015-03-04 12:24:26 +00:00
|
|
|
return it;
|
|
|
|
|
}
|
|
|
|
|
return bitrate_observers_.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) {
|
2016-01-20 02:32:54 -08:00
|
|
|
rtc::CritScope lock(&crit_sect_);
|
2015-03-04 12:24:26 +00:00
|
|
|
enforce_min_bitrate_ = enforce_min_bitrate;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 11:11:06 +01:00
|
|
|
BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation(
|
|
|
|
|
uint32_t bitrate,
|
|
|
|
|
uint32_t sum_min_bitrates) {
|
2015-11-12 21:02:42 -08:00
|
|
|
uint32_t number_of_observers =
|
|
|
|
|
static_cast<uint32_t>(bitrate_observers_.size());
|
2015-03-04 12:24:26 +00:00
|
|
|
uint32_t bitrate_per_observer =
|
|
|
|
|
(bitrate - sum_min_bitrates) / number_of_observers;
|
|
|
|
|
// Use map to sort list based on max bitrate.
|
|
|
|
|
ObserverSortingMap list_max_bitrates;
|
2015-03-26 11:11:06 +01:00
|
|
|
for (const auto& observer : bitrate_observers_) {
|
|
|
|
|
list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration>(
|
2015-09-15 15:08:03 +02:00
|
|
|
observer.second.max_bitrate,
|
|
|
|
|
ObserverConfiguration(observer.first, observer.second.min_bitrate)));
|
2015-03-04 12:24:26 +00:00
|
|
|
}
|
2015-03-26 11:11:06 +01:00
|
|
|
ObserverBitrateMap allocation;
|
2015-03-04 12:24:26 +00:00
|
|
|
ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
|
|
|
|
|
while (max_it != list_max_bitrates.end()) {
|
|
|
|
|
number_of_observers--;
|
|
|
|
|
uint32_t observer_allowance =
|
2015-09-15 15:08:03 +02:00
|
|
|
max_it->second.min_bitrate + 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;
|
|
|
|
|
if (number_of_observers != 0) {
|
|
|
|
|
bitrate_per_observer += remainder / number_of_observers;
|
|
|
|
|
}
|
2015-09-15 15:08:03 +02:00
|
|
|
allocation[max_it->second.observer] = max_it->first;
|
2015-03-04 12:24:26 +00:00
|
|
|
} else {
|
2015-09-15 15:08:03 +02: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
|
|
|
}
|
|
|
|
|
|
2015-03-26 11:11:06 +01:00
|
|
|
BitrateAllocator::ObserverBitrateMap BitrateAllocator::LowRateAllocation(
|
|
|
|
|
uint32_t bitrate) {
|
|
|
|
|
ObserverBitrateMap allocation;
|
2015-03-04 12:24:26 +00:00
|
|
|
if (enforce_min_bitrate_) {
|
|
|
|
|
// Min bitrate to all observers.
|
2015-03-26 11:11:06 +01:00
|
|
|
for (const auto& observer : bitrate_observers_)
|
2015-09-15 15:08:03 +02:00
|
|
|
allocation[observer.first] = observer.second.min_bitrate;
|
2015-03-04 12:24:26 +00:00
|
|
|
} else {
|
2015-09-15 15:08:03 +02:00
|
|
|
// Allocate up to |min_bitrate| to one observer at a time, until
|
2015-03-04 12:24:26 +00:00
|
|
|
// |bitrate| is depleted.
|
|
|
|
|
uint32_t remainder = bitrate;
|
2015-03-26 11:11:06 +01:00
|
|
|
for (const auto& observer : bitrate_observers_) {
|
|
|
|
|
uint32_t allocated_bitrate =
|
2015-09-15 15:08:03 +02:00
|
|
|
std::min(remainder, observer.second.min_bitrate);
|
2015-03-26 11:11:06 +01:00
|
|
|
allocation[observer.first] = allocated_bitrate;
|
|
|
|
|
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
|