| 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( |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 46 | const std::vector<BitrateAllocationStrategy::TrackConfig>& track_configs) { |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 47 | std::vector<uint32_t> track_allocations; |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 48 | for (const auto& track_config : track_configs) { |
| 49 | track_allocations.push_back(track_config.min_bitrate_bps); |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 50 | } |
| 51 | return track_allocations; |
| 52 | } |
| 53 | |
| 54 | std::vector<uint32_t> BitrateAllocationStrategy::DistributeBitratesEvenly( |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 55 | const std::vector<BitrateAllocationStrategy::TrackConfig>& track_configs, |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 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; |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 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; |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 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(); |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 69 | for (const auto& track_config : track_configs) { |
| 70 | *track_allocations_it++ = track_config.max_bitrate_bps; |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 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; |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 79 | for (const auto& track_config : track_configs) { |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 80 | max_bitrate_sorted_configs.insert( |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 81 | std::make_pair(track_config.max_bitrate_bps, |
| 82 | &track_config - &track_configs.front())); |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 83 | } |
| 84 | uint32_t total_available_increase = available_bitrate - sum_min_bitrates; |
| 85 | int processed_configs = 0; |
| 86 | for (const auto& track_config_pair : max_bitrate_sorted_configs) { |
| 87 | uint32_t available_increase = |
| 88 | total_available_increase / |
| 89 | (static_cast<uint32_t>(track_configs.size() - processed_configs)); |
| 90 | uint32_t consumed_increase = |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 91 | std::min(track_configs[track_config_pair.second].max_bitrate_bps - |
| 92 | track_configs[track_config_pair.second].min_bitrate_bps, |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 93 | available_increase); |
| 94 | track_allocations[track_config_pair.second] += consumed_increase; |
| 95 | total_available_increase -= consumed_increase; |
| 96 | ++processed_configs; |
| 97 | } |
| 98 | return track_allocations; |
| 99 | } |
| 100 | } |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 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, |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 113 | std::vector<BitrateAllocationStrategy::TrackConfig> track_configs) { |
| 114 | TrackConfig* audio_track_config = nullptr; |
| 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 | |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 119 | for (auto& track_config : track_configs) { |
| 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 | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 122 | audio_track_config = &track_config; |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 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(); |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 127 | } |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 128 | sum_min_bitrates += track_config.min_bitrate_bps; |
| 129 | sum_max_bitrates += track_config.max_bitrate_bps; |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 130 | } |
| Alex Narest | e0b2ff5 | 2017-11-16 20:03:46 +0100 | [diff] [blame] | 131 | if (sum_max_bitrates < available_bitrate) { |
| 132 | // Allow non audio streams to go above max upto |
| 133 | // kTransmissionMaxBitrateMultiplier * max_bitrate_bps |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 134 | for (auto& track_config : track_configs) { |
| 135 | if (&track_config != audio_track_config) |
| 136 | track_config.max_bitrate_bps *= kTransmissionMaxBitrateMultiplier; |
| Alex Narest | e0b2ff5 | 2017-11-16 20:03:46 +0100 | [diff] [blame] | 137 | } |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 138 | return DistributeBitratesEvenly(track_configs, available_bitrate); |
| Alex Narest | e0b2ff5 | 2017-11-16 20:03:46 +0100 | [diff] [blame] | 139 | } |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 140 | if (!audio_track_config) { |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 141 | return DistributeBitratesEvenly(track_configs, available_bitrate); |
| 142 | } |
| Sebastian Jansson | d28efe5 | 2018-10-18 12:26:46 +0200 | [diff] [blame] | 143 | auto safe_sufficient_audio_bitrate = rtc::SafeClamp( |
| 144 | sufficient_audio_bitrate_, audio_track_config->min_bitrate_bps, |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 145 | audio_track_config->max_bitrate_bps); |
| 146 | if (available_bitrate <= sum_min_bitrates) { |
| 147 | return SetAllBitratesToMinimum(track_configs); |
| 148 | } else { |
| 149 | if (available_bitrate <= sum_min_bitrates + safe_sufficient_audio_bitrate - |
| 150 | audio_track_config->min_bitrate_bps) { |
| 151 | std::vector<uint32_t> track_allocations = |
| 152 | SetAllBitratesToMinimum(track_configs); |
| 153 | track_allocations[audio_config_index] += |
| 154 | available_bitrate - sum_min_bitrates; |
| 155 | return track_allocations; |
| 156 | } else { |
| 157 | // Setting audio track minimum to safe_sufficient_audio_bitrate will |
| 158 | // allow using DistributeBitratesEvenly to allocate at least sufficient |
| 159 | // bitrate for audio and the rest evenly. |
| Sebastian Jansson | e2754c9 | 2018-10-24 16:02:26 +0200 | [diff] [blame^] | 160 | audio_track_config->min_bitrate_bps = safe_sufficient_audio_bitrate; |
| Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 161 | std::vector<uint32_t> track_allocations = |
| 162 | DistributeBitratesEvenly(track_configs, available_bitrate); |
| 163 | return track_allocations; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | } // namespace rtc |