| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #include "rtc_base/bitrateallocationstrategy.h" |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 12 | |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 13 | #include <algorithm> |
| Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <cstddef> |
| 15 | #include <cstdint> |
| Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 16 | #include <map> |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 19 | #include "rtc_base/numerics/safe_minmax.h" |
| 20 | #include "system_wrappers/include/field_trial.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | AudioPriorityConfig::AudioPriorityConfig() |
| 24 | : min_rate("min"), max_rate("max"), target_rate("target") { |
| 25 | std::string trial_string; |
| 26 | // TODO(bugs.webrtc.org/9889): Remove this when Chromium build has been fixed. |
| 27 | #if !defined(WEBRTC_CHROMIUM_BUILD) |
| 28 | trial_string = field_trial::FindFullName("WebRTC-Bwe-AudioPriority"); |
| 29 | #endif |
| 30 | ParseFieldTrial({&min_rate, &max_rate, &target_rate}, trial_string); |
| 31 | } |
| 32 | AudioPriorityConfig::AudioPriorityConfig(const AudioPriorityConfig&) = default; |
| 33 | AudioPriorityConfig::~AudioPriorityConfig() = default; |
| 34 | |
| 35 | } // namespace webrtc |
| 36 | |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 37 | namespace rtc { |
| 38 | |
| Alex Narest | e0b2ff5 | 2017-11-16 20:03:46 +0100 | [diff] [blame] | 39 | // The purpose of this is to allow video streams to use extra bandwidth for FEC. |
| 40 | // TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in |
| 41 | // video send stream. Similar logic is implemented in BitrateAllocator. |
| 42 | |
| 43 | const int kTransmissionMaxBitrateMultiplier = 2; |
| 44 | |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 45 | std::vector<uint32_t> BitrateAllocationStrategy::SetAllBitratesToMinimum( |
| 46 | const ArrayView<const TrackConfig*> track_configs) { |
| 47 | std::vector<uint32_t> track_allocations; |
| 48 | for (const auto* track_config : track_configs) { |
| 49 | track_allocations.push_back(track_config->min_bitrate_bps); |
| 50 | } |
| 51 | return track_allocations; |
| 52 | } |
| 53 | |
| 54 | std::vector<uint32_t> BitrateAllocationStrategy::DistributeBitratesEvenly( |
| 55 | const ArrayView<const TrackConfig*> track_configs, |
| 56 | uint32_t available_bitrate) { |
| 57 | std::vector<uint32_t> track_allocations = |
| 58 | SetAllBitratesToMinimum(track_configs); |
| 59 | uint32_t sum_min_bitrates = 0; |
| 60 | uint32_t sum_max_bitrates = 0; |
| 61 | for (const auto* track_config : track_configs) { |
| 62 | sum_min_bitrates += track_config->min_bitrate_bps; |
| 63 | sum_max_bitrates += track_config->max_bitrate_bps; |
| 64 | } |
| 65 | if (sum_min_bitrates >= available_bitrate) { |
| 66 | return track_allocations; |
| 67 | } else if (available_bitrate >= sum_max_bitrates) { |
| 68 | auto track_allocations_it = track_allocations.begin(); |
| 69 | for (const auto* track_config : track_configs) { |
| 70 | *track_allocations_it++ = track_config->max_bitrate_bps; |
| 71 | } |
| 72 | return track_allocations; |
| 73 | } else { |
| 74 | // If sum_min_bitrates < available_bitrate < sum_max_bitrates allocate |
| 75 | // bitrates evenly up to max_bitrate_bps starting from the track with the |
| 76 | // lowest max_bitrate_bps. Remainder of available bitrate split evenly among |
| 77 | // remaining tracks. |
| 78 | std::multimap<uint32_t, size_t> max_bitrate_sorted_configs; |
| 79 | for (const TrackConfig** track_configs_it = track_configs.begin(); |
| 80 | track_configs_it != track_configs.end(); ++track_configs_it) { |
| 81 | max_bitrate_sorted_configs.insert( |
| 82 | std::make_pair((*track_configs_it)->max_bitrate_bps, |
| 83 | track_configs_it - track_configs.begin())); |
| 84 | } |
| 85 | uint32_t total_available_increase = available_bitrate - sum_min_bitrates; |
| 86 | int processed_configs = 0; |
| 87 | for (const auto& track_config_pair : max_bitrate_sorted_configs) { |
| 88 | uint32_t available_increase = |
| 89 | total_available_increase / |
| 90 | (static_cast<uint32_t>(track_configs.size() - processed_configs)); |
| 91 | uint32_t consumed_increase = |
| 92 | std::min(track_configs[track_config_pair.second]->max_bitrate_bps - |
| 93 | track_configs[track_config_pair.second]->min_bitrate_bps, |
| 94 | available_increase); |
| 95 | track_allocations[track_config_pair.second] += consumed_increase; |
| 96 | total_available_increase -= consumed_increase; |
| 97 | ++processed_configs; |
| 98 | } |
| 99 | return track_allocations; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | AudioPriorityBitrateAllocationStrategy::AudioPriorityBitrateAllocationStrategy( |
| 104 | std::string audio_track_id, |
| 105 | uint32_t sufficient_audio_bitrate) |
| 106 | : audio_track_id_(audio_track_id), |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 107 | sufficient_audio_bitrate_(sufficient_audio_bitrate) { |
| 108 | if (config_.target_rate) { |
| 109 | sufficient_audio_bitrate_ = config_.target_rate->bps(); |
| 110 | } |
| 111 | } |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 112 | |
| 113 | std::vector<uint32_t> AudioPriorityBitrateAllocationStrategy::AllocateBitrates( |
| 114 | uint32_t available_bitrate, |
| 115 | const ArrayView<const TrackConfig*> track_configs) { |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 116 | absl::optional<TrackConfig> audio_track_config; |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 117 | size_t audio_config_index = 0; |
| 118 | uint32_t sum_min_bitrates = 0; |
| Alex Narest | e0b2ff5 | 2017-11-16 20:03:46 +0100 | [diff] [blame] | 119 | uint32_t sum_max_bitrates = 0; |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 120 | |
| 121 | for (const auto*& track_config : track_configs) { |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 122 | if (track_config->track_id == audio_track_id_) { |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 123 | audio_config_index = &track_config - &track_configs[0]; |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 124 | audio_track_config = *track_config; |
| 125 | if (config_.min_rate) |
| 126 | audio_track_config->min_bitrate_bps = config_.min_rate->bps(); |
| 127 | if (config_.max_rate) |
| 128 | audio_track_config->max_bitrate_bps = config_.max_rate->bps(); |
| 129 | sum_min_bitrates += audio_track_config->min_bitrate_bps; |
| 130 | sum_max_bitrates += audio_track_config->max_bitrate_bps; |
| 131 | } else { |
| 132 | sum_min_bitrates += track_config->min_bitrate_bps; |
| 133 | sum_max_bitrates += track_config->max_bitrate_bps; |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 134 | } |
| 135 | } |
| Alex Narest | e0b2ff5 | 2017-11-16 20:03:46 +0100 | [diff] [blame] | 136 | if (sum_max_bitrates < available_bitrate) { |
| 137 | // Allow non audio streams to go above max upto |
| 138 | // kTransmissionMaxBitrateMultiplier * max_bitrate_bps |
| 139 | size_t track_configs_size = track_configs.size(); |
| 140 | std::vector<TrackConfig> increased_track_configs(track_configs_size); |
| 141 | std::vector<const TrackConfig*> increased_track_configs_ptr( |
| 142 | track_configs_size); |
| 143 | for (unsigned long i = 0; i < track_configs_size; i++) { |
| 144 | increased_track_configs[i] = (*track_configs[i]); |
| 145 | increased_track_configs_ptr[i] = &increased_track_configs[i]; |
| 146 | if (track_configs[i]->track_id != audio_track_id_) { |
| 147 | increased_track_configs[i].max_bitrate_bps = |
| 148 | track_configs[i]->max_bitrate_bps * |
| 149 | kTransmissionMaxBitrateMultiplier; |
| 150 | } |
| 151 | } |
| 152 | return DistributeBitratesEvenly(increased_track_configs_ptr, |
| 153 | available_bitrate); |
| 154 | } |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 155 | if (!audio_track_config) { |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 156 | return DistributeBitratesEvenly(track_configs, available_bitrate); |
| 157 | } |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 158 | auto safe_sufficient_audio_bitrate = rtc::SafeClamp( |
| 159 | sufficient_audio_bitrate_, audio_track_config->min_bitrate_bps, |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 160 | audio_track_config->max_bitrate_bps); |
| 161 | if (available_bitrate <= sum_min_bitrates) { |
| 162 | return SetAllBitratesToMinimum(track_configs); |
| 163 | } else { |
| 164 | if (available_bitrate <= sum_min_bitrates + safe_sufficient_audio_bitrate - |
| 165 | audio_track_config->min_bitrate_bps) { |
| 166 | std::vector<uint32_t> track_allocations = |
| 167 | SetAllBitratesToMinimum(track_configs); |
| 168 | track_allocations[audio_config_index] += |
| 169 | available_bitrate - sum_min_bitrates; |
| 170 | return track_allocations; |
| 171 | } else { |
| 172 | // Setting audio track minimum to safe_sufficient_audio_bitrate will |
| 173 | // allow using DistributeBitratesEvenly to allocate at least sufficient |
| 174 | // bitrate for audio and the rest evenly. |
| 175 | TrackConfig sufficient_track_config(*track_configs[audio_config_index]); |
| 176 | sufficient_track_config.min_bitrate_bps = safe_sufficient_audio_bitrate; |
| 177 | track_configs[audio_config_index] = &sufficient_track_config; |
| 178 | std::vector<uint32_t> track_allocations = |
| 179 | DistributeBitratesEvenly(track_configs, available_bitrate); |
| 180 | return track_allocations; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | } // namespace rtc |