| Index: webrtc/rtc_base/bitrateallocationstrategy.cc
|
| diff --git a/webrtc/rtc_base/bitrateallocationstrategy.cc b/webrtc/rtc_base/bitrateallocationstrategy.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ac15a2fb5ec9e4c98b515c11cbbdf27b1fa9c3e5
|
| --- /dev/null
|
| +++ b/webrtc/rtc_base/bitrateallocationstrategy.cc
|
| @@ -0,0 +1,112 @@
|
| +/*
|
| + * Copyright 2017 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.
|
| + */
|
| +
|
| +#include "webrtc/rtc_base/bitrateallocationstrategy.h"
|
| +#include <algorithm>
|
| +#include <utility>
|
| +
|
| +namespace rtc {
|
| +
|
| +BitrateAllocationStrategy::TrackAllocations
|
| +BitrateAllocationStrategy::SetAllBitratesToMinimum(
|
| + const TrackConfigs& track_configs) {
|
| + TrackAllocations track_allocations;
|
| + for (const auto& track_config : track_configs) {
|
| + track_allocations[track_config.second.track_id] =
|
| + track_config.second.min_bitrate_bps;
|
| + }
|
| + return track_allocations;
|
| +}
|
| +
|
| +BitrateAllocationStrategy::TrackAllocations
|
| +BitrateAllocationStrategy::DistributeBitratesEvenly(
|
| + const TrackConfigs& track_configs,
|
| + uint32_t available_bitrate) {
|
| + TrackAllocations track_allocations = SetAllBitratesToMinimum(track_configs);
|
| + uint32_t sum_min_bitrates = 0;
|
| + uint32_t sum_max_bitrates = 0;
|
| + for (const auto& track_config : track_configs) {
|
| + sum_min_bitrates += track_config.second.min_bitrate_bps;
|
| + sum_max_bitrates += track_config.second.max_bitrate_bps;
|
| + }
|
| + if (sum_min_bitrates >= available_bitrate) {
|
| + return track_allocations;
|
| + } else if (available_bitrate >= sum_max_bitrates) {
|
| + for (const auto& track_config : track_configs) {
|
| + track_allocations[track_config.second.track_id] =
|
| + track_config.second.max_bitrate_bps;
|
| + }
|
| + return track_allocations;
|
| + } else {
|
| + std::multimap<uint32_t, TrackConfig> max_bitrate_sorted_configs;
|
| + for (const auto& track_config : track_configs) {
|
| + max_bitrate_sorted_configs.insert(std::pair<uint32_t, const TrackConfig&>(
|
| + track_config.second.max_bitrate_bps, track_config.second));
|
| + }
|
| + uint32_t total_available_increase = (available_bitrate - sum_min_bitrates);
|
| + int processed_configs = 0;
|
| + for (const auto& track_config_pair : max_bitrate_sorted_configs) {
|
| + TrackConfig track_config = track_config_pair.second;
|
| + uint32_t available_increase =
|
| + total_available_increase /
|
| + (static_cast<uint32_t>(track_configs.size() - processed_configs));
|
| + uint32_t consumed_increase =
|
| + std::min(track_config.max_bitrate_bps, available_increase);
|
| + track_allocations[track_config.track_id] += consumed_increase;
|
| + total_available_increase -= consumed_increase;
|
| + ++processed_configs;
|
| + }
|
| + return track_allocations;
|
| + }
|
| +}
|
| +
|
| +AudioPriorityBitrateAllocationStrategy::AudioPriorityBitrateAllocationStrategy(
|
| + std::string audio_track_id,
|
| + uint32_t sufficient_audio_bitrate)
|
| + : audio_track_id_(audio_track_id),
|
| + sufficient_audio_bitrate_(sufficient_audio_bitrate) {}
|
| +
|
| +BitrateAllocationStrategy::TrackAllocations
|
| +AudioPriorityBitrateAllocationStrategy::AllocateBitrates(
|
| + uint32_t available_bitrate,
|
| + const TrackConfigs& track_configs) {
|
| + auto audio_track_config_pair = track_configs.find(audio_track_id_);
|
| + const TrackConfig& audio_track_config = audio_track_config_pair->second;
|
| + if (audio_track_config_pair == track_configs.end()) {
|
| + return DistributeBitratesEvenly(track_configs, available_bitrate);
|
| + } else {
|
| + RTC_DCHECK(sufficient_audio_bitrate_ >= audio_track_config.min_bitrate_bps);
|
| + uint32_t sum_min_bitrates = 0;
|
| + for (const auto& track_config : track_configs) {
|
| + sum_min_bitrates += track_config.second.min_bitrate_bps;
|
| + }
|
| + if (available_bitrate <= sum_min_bitrates) {
|
| + return SetAllBitratesToMinimum(track_configs);
|
| + } else {
|
| + if (available_bitrate <= sum_min_bitrates + sufficient_audio_bitrate_ -
|
| + audio_track_config.min_bitrate_bps) {
|
| + TrackAllocations track_allocations =
|
| + SetAllBitratesToMinimum(track_configs);
|
| + track_allocations[audio_track_id_] +=
|
| + available_bitrate - sum_min_bitrates;
|
| + return track_allocations;
|
| + } else {
|
| + TrackConfig sufficient_audio_config(audio_track_config);
|
| + sufficient_audio_config.min_bitrate_bps = sufficient_audio_bitrate_;
|
| + TrackConfigs sufficient_track_configs(track_configs);
|
| + sufficient_track_configs[audio_track_id_] = sufficient_audio_config;
|
| + return DistributeBitratesEvenly(sufficient_track_configs,
|
| + available_bitrate);
|
| + }
|
| + }
|
| + }
|
| +}
|
| +
|
| +} // namespace rtc
|
|
|