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