minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/audio_network_adaptor/controller_manager.h" |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 12 | |
| 13 | #include <cmath> |
Mirko Bonadei | e45c688 | 2019-02-16 09:59:29 +0100 | [diff] [blame] | 14 | #include <string> |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_coding/audio_network_adaptor/bitrate_controller.h" |
| 18 | #include "modules/audio_coding/audio_network_adaptor/channel_controller.h" |
| 19 | #include "modules/audio_coding/audio_network_adaptor/debug_dump_writer.h" |
| 20 | #include "modules/audio_coding/audio_network_adaptor/dtx_controller.h" |
| 21 | #include "modules/audio_coding/audio_network_adaptor/fec_controller_plr_based.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/audio_coding/audio_network_adaptor/frame_length_controller.h" |
| 23 | #include "modules/audio_coding/audio_network_adaptor/util/threshold_curve.h" |
| 24 | #include "rtc_base/ignore_wundef.h" |
Sebastian Jansson | cd2a92f | 2019-10-31 13:53:53 +0100 | [diff] [blame^] | 25 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "rtc_base/time_utils.h" |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 27 | |
minyue-webrtc | 7ed35f4 | 2017-06-13 11:49:29 +0200 | [diff] [blame] | 28 | #if WEBRTC_ENABLE_PROTOBUF |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 29 | RTC_PUSH_IGNORING_WUNDEF() |
| 30 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
| 31 | #include "external/webrtc/webrtc/modules/audio_coding/audio_network_adaptor/config.pb.h" |
| 32 | #else |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "modules/audio_coding/audio_network_adaptor/config.pb.h" |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 34 | #endif |
| 35 | RTC_POP_IGNORING_WUNDEF() |
| 36 | #endif |
| 37 | |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 38 | namespace webrtc { |
| 39 | |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 40 | namespace { |
| 41 | |
minyue-webrtc | 7ed35f4 | 2017-06-13 11:49:29 +0200 | [diff] [blame] | 42 | #if WEBRTC_ENABLE_PROTOBUF |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 43 | |
elad.alon | 6d7900d | 2017-03-24 04:12:56 -0700 | [diff] [blame] | 44 | std::unique_ptr<FecControllerPlrBased> CreateFecControllerPlrBased( |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 45 | const audio_network_adaptor::config::FecController& config, |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 46 | bool initial_fec_enabled) { |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 47 | RTC_CHECK(config.has_fec_enabling_threshold()); |
| 48 | RTC_CHECK(config.has_fec_disabling_threshold()); |
| 49 | RTC_CHECK(config.has_time_constant_ms()); |
| 50 | |
| 51 | auto& fec_enabling_threshold = config.fec_enabling_threshold(); |
| 52 | RTC_CHECK(fec_enabling_threshold.has_low_bandwidth_bps()); |
| 53 | RTC_CHECK(fec_enabling_threshold.has_low_bandwidth_packet_loss()); |
| 54 | RTC_CHECK(fec_enabling_threshold.has_high_bandwidth_bps()); |
| 55 | RTC_CHECK(fec_enabling_threshold.has_high_bandwidth_packet_loss()); |
| 56 | |
| 57 | auto& fec_disabling_threshold = config.fec_disabling_threshold(); |
| 58 | RTC_CHECK(fec_disabling_threshold.has_low_bandwidth_bps()); |
| 59 | RTC_CHECK(fec_disabling_threshold.has_low_bandwidth_packet_loss()); |
| 60 | RTC_CHECK(fec_disabling_threshold.has_high_bandwidth_bps()); |
| 61 | RTC_CHECK(fec_disabling_threshold.has_high_bandwidth_packet_loss()); |
| 62 | |
elad.alon | 6d7900d | 2017-03-24 04:12:56 -0700 | [diff] [blame] | 63 | return std::unique_ptr<FecControllerPlrBased>( |
| 64 | new FecControllerPlrBased(FecControllerPlrBased::Config( |
| 65 | initial_fec_enabled, |
elad.alon | 326263a | 2017-03-29 03:16:58 -0700 | [diff] [blame] | 66 | ThresholdCurve(fec_enabling_threshold.low_bandwidth_bps(), |
| 67 | fec_enabling_threshold.low_bandwidth_packet_loss(), |
| 68 | fec_enabling_threshold.high_bandwidth_bps(), |
| 69 | fec_enabling_threshold.high_bandwidth_packet_loss()), |
| 70 | ThresholdCurve(fec_disabling_threshold.low_bandwidth_bps(), |
| 71 | fec_disabling_threshold.low_bandwidth_packet_loss(), |
| 72 | fec_disabling_threshold.high_bandwidth_bps(), |
| 73 | fec_disabling_threshold.high_bandwidth_packet_loss()), |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 74 | config.time_constant_ms()))); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | std::unique_ptr<FrameLengthController> CreateFrameLengthController( |
| 78 | const audio_network_adaptor::config::FrameLengthController& config, |
| 79 | rtc::ArrayView<const int> encoder_frame_lengths_ms, |
michaelt | 6f08d7d | 2017-02-22 07:35:05 -0800 | [diff] [blame] | 80 | int initial_frame_length_ms, |
| 81 | int min_encoder_bitrate_bps) { |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 82 | RTC_CHECK(config.has_fl_increasing_packet_loss_fraction()); |
| 83 | RTC_CHECK(config.has_fl_decreasing_packet_loss_fraction()); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 84 | |
michaelt | a55f021 | 2017-02-02 07:47:19 -0800 | [diff] [blame] | 85 | std::map<FrameLengthController::Config::FrameLengthChange, int> |
Ying Wang | 0e1a558 | 2019-08-16 16:45:30 +0200 | [diff] [blame] | 86 | fl_changing_bandwidths_bps; |
michaelt | a55f021 | 2017-02-02 07:47:19 -0800 | [diff] [blame] | 87 | |
Ying Wang | 0e1a558 | 2019-08-16 16:45:30 +0200 | [diff] [blame] | 88 | if (config.has_fl_20ms_to_60ms_bandwidth_bps()) { |
| 89 | fl_changing_bandwidths_bps.insert( |
| 90 | std::make_pair(FrameLengthController::Config::FrameLengthChange(20, 60), |
| 91 | config.fl_20ms_to_60ms_bandwidth_bps())); |
| 92 | } |
| 93 | |
| 94 | if (config.has_fl_60ms_to_20ms_bandwidth_bps()) { |
| 95 | fl_changing_bandwidths_bps.insert( |
| 96 | std::make_pair(FrameLengthController::Config::FrameLengthChange(60, 20), |
| 97 | config.fl_60ms_to_20ms_bandwidth_bps())); |
| 98 | } |
| 99 | |
| 100 | if (config.has_fl_20ms_to_40ms_bandwidth_bps()) { |
| 101 | fl_changing_bandwidths_bps.insert( |
| 102 | std::make_pair(FrameLengthController::Config::FrameLengthChange(20, 40), |
| 103 | config.fl_20ms_to_40ms_bandwidth_bps())); |
| 104 | } |
| 105 | |
| 106 | if (config.has_fl_40ms_to_20ms_bandwidth_bps()) { |
| 107 | fl_changing_bandwidths_bps.insert( |
| 108 | std::make_pair(FrameLengthController::Config::FrameLengthChange(40, 20), |
| 109 | config.fl_40ms_to_20ms_bandwidth_bps())); |
| 110 | } |
| 111 | |
| 112 | if (config.has_fl_40ms_to_60ms_bandwidth_bps()) { |
| 113 | fl_changing_bandwidths_bps.insert( |
| 114 | std::make_pair(FrameLengthController::Config::FrameLengthChange(40, 60), |
| 115 | config.fl_40ms_to_60ms_bandwidth_bps())); |
| 116 | } |
| 117 | |
| 118 | if (config.has_fl_60ms_to_40ms_bandwidth_bps()) { |
| 119 | fl_changing_bandwidths_bps.insert( |
| 120 | std::make_pair(FrameLengthController::Config::FrameLengthChange(60, 40), |
| 121 | config.fl_60ms_to_40ms_bandwidth_bps())); |
| 122 | } |
| 123 | |
| 124 | if (config.has_fl_60ms_to_120ms_bandwidth_bps()) { |
michaelt | a55f021 | 2017-02-02 07:47:19 -0800 | [diff] [blame] | 125 | fl_changing_bandwidths_bps.insert(std::make_pair( |
| 126 | FrameLengthController::Config::FrameLengthChange(60, 120), |
| 127 | config.fl_60ms_to_120ms_bandwidth_bps())); |
Ying Wang | 0e1a558 | 2019-08-16 16:45:30 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (config.has_fl_120ms_to_60ms_bandwidth_bps()) { |
michaelt | a55f021 | 2017-02-02 07:47:19 -0800 | [diff] [blame] | 131 | fl_changing_bandwidths_bps.insert(std::make_pair( |
| 132 | FrameLengthController::Config::FrameLengthChange(120, 60), |
| 133 | config.fl_120ms_to_60ms_bandwidth_bps())); |
| 134 | } |
| 135 | |
ivoc | 7e9c614 | 2017-09-28 01:11:16 -0700 | [diff] [blame] | 136 | int fl_increase_overhead_offset = 0; |
| 137 | if (config.has_fl_increase_overhead_offset()) { |
| 138 | fl_increase_overhead_offset = config.fl_increase_overhead_offset(); |
| 139 | } |
| 140 | int fl_decrease_overhead_offset = 0; |
| 141 | if (config.has_fl_decrease_overhead_offset()) { |
| 142 | fl_decrease_overhead_offset = config.fl_decrease_overhead_offset(); |
| 143 | } |
| 144 | |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 145 | FrameLengthController::Config ctor_config( |
Minyue Li | 8319e7f | 2019-01-02 16:57:02 +0100 | [diff] [blame] | 146 | std::set<int>(), initial_frame_length_ms, min_encoder_bitrate_bps, |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 147 | config.fl_increasing_packet_loss_fraction(), |
ivoc | 7e9c614 | 2017-09-28 01:11:16 -0700 | [diff] [blame] | 148 | config.fl_decreasing_packet_loss_fraction(), fl_increase_overhead_offset, |
| 149 | fl_decrease_overhead_offset, std::move(fl_changing_bandwidths_bps)); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 150 | |
| 151 | for (auto frame_length : encoder_frame_lengths_ms) |
Minyue Li | 8319e7f | 2019-01-02 16:57:02 +0100 | [diff] [blame] | 152 | ctor_config.encoder_frame_lengths_ms.insert(frame_length); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 153 | |
| 154 | return std::unique_ptr<FrameLengthController>( |
| 155 | new FrameLengthController(ctor_config)); |
| 156 | } |
| 157 | |
| 158 | std::unique_ptr<ChannelController> CreateChannelController( |
| 159 | const audio_network_adaptor::config::ChannelController& config, |
| 160 | size_t num_encoder_channels, |
| 161 | size_t intial_channels_to_encode) { |
| 162 | RTC_CHECK(config.has_channel_1_to_2_bandwidth_bps()); |
| 163 | RTC_CHECK(config.has_channel_2_to_1_bandwidth_bps()); |
| 164 | |
| 165 | return std::unique_ptr<ChannelController>(new ChannelController( |
| 166 | ChannelController::Config(num_encoder_channels, intial_channels_to_encode, |
| 167 | config.channel_1_to_2_bandwidth_bps(), |
| 168 | config.channel_2_to_1_bandwidth_bps()))); |
| 169 | } |
| 170 | |
| 171 | std::unique_ptr<DtxController> CreateDtxController( |
| 172 | const audio_network_adaptor::config::DtxController& dtx_config, |
| 173 | bool initial_dtx_enabled) { |
| 174 | RTC_CHECK(dtx_config.has_dtx_enabling_bandwidth_bps()); |
| 175 | RTC_CHECK(dtx_config.has_dtx_disabling_bandwidth_bps()); |
| 176 | |
| 177 | return std::unique_ptr<DtxController>(new DtxController(DtxController::Config( |
| 178 | initial_dtx_enabled, dtx_config.dtx_enabling_bandwidth_bps(), |
| 179 | dtx_config.dtx_disabling_bandwidth_bps()))); |
| 180 | } |
| 181 | |
| 182 | using audio_network_adaptor::BitrateController; |
| 183 | std::unique_ptr<BitrateController> CreateBitrateController( |
ivoc | 7e9c614 | 2017-09-28 01:11:16 -0700 | [diff] [blame] | 184 | const audio_network_adaptor::config::BitrateController& bitrate_config, |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 185 | int initial_bitrate_bps, |
| 186 | int initial_frame_length_ms) { |
ivoc | 7e9c614 | 2017-09-28 01:11:16 -0700 | [diff] [blame] | 187 | int fl_increase_overhead_offset = 0; |
| 188 | if (bitrate_config.has_fl_increase_overhead_offset()) { |
| 189 | fl_increase_overhead_offset = bitrate_config.fl_increase_overhead_offset(); |
| 190 | } |
| 191 | int fl_decrease_overhead_offset = 0; |
| 192 | if (bitrate_config.has_fl_decrease_overhead_offset()) { |
| 193 | fl_decrease_overhead_offset = bitrate_config.fl_decrease_overhead_offset(); |
| 194 | } |
| 195 | return std::unique_ptr<BitrateController>( |
| 196 | new BitrateController(BitrateController::Config( |
| 197 | initial_bitrate_bps, initial_frame_length_ms, |
| 198 | fl_increase_overhead_offset, fl_decrease_overhead_offset))); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 199 | } |
minyue-webrtc | 7ed35f4 | 2017-06-13 11:49:29 +0200 | [diff] [blame] | 200 | #endif // WEBRTC_ENABLE_PROTOBUF |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 201 | |
| 202 | } // namespace |
| 203 | |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 204 | ControllerManagerImpl::Config::Config(int min_reordering_time_ms, |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 205 | float min_reordering_squared_distance) |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 206 | : min_reordering_time_ms(min_reordering_time_ms), |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 207 | min_reordering_squared_distance(min_reordering_squared_distance) {} |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 208 | |
| 209 | ControllerManagerImpl::Config::~Config() = default; |
| 210 | |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 211 | std::unique_ptr<ControllerManager> ControllerManagerImpl::Create( |
Mirko Bonadei | e45c688 | 2019-02-16 09:59:29 +0100 | [diff] [blame] | 212 | const std::string& config_string, |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 213 | size_t num_encoder_channels, |
| 214 | rtc::ArrayView<const int> encoder_frame_lengths_ms, |
michaelt | 6f08d7d | 2017-02-22 07:35:05 -0800 | [diff] [blame] | 215 | int min_encoder_bitrate_bps, |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 216 | size_t intial_channels_to_encode, |
| 217 | int initial_frame_length_ms, |
| 218 | int initial_bitrate_bps, |
| 219 | bool initial_fec_enabled, |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 220 | bool initial_dtx_enabled) { |
minyue-webrtc | 9c6430f | 2017-07-10 16:29:17 +0200 | [diff] [blame] | 221 | return Create(config_string, num_encoder_channels, encoder_frame_lengths_ms, |
| 222 | min_encoder_bitrate_bps, intial_channels_to_encode, |
| 223 | initial_frame_length_ms, initial_bitrate_bps, |
| 224 | initial_fec_enabled, initial_dtx_enabled, nullptr); |
| 225 | } |
| 226 | |
| 227 | std::unique_ptr<ControllerManager> ControllerManagerImpl::Create( |
Mirko Bonadei | e45c688 | 2019-02-16 09:59:29 +0100 | [diff] [blame] | 228 | const std::string& config_string, |
minyue-webrtc | 9c6430f | 2017-07-10 16:29:17 +0200 | [diff] [blame] | 229 | size_t num_encoder_channels, |
| 230 | rtc::ArrayView<const int> encoder_frame_lengths_ms, |
| 231 | int min_encoder_bitrate_bps, |
| 232 | size_t intial_channels_to_encode, |
| 233 | int initial_frame_length_ms, |
| 234 | int initial_bitrate_bps, |
| 235 | bool initial_fec_enabled, |
| 236 | bool initial_dtx_enabled, |
| 237 | DebugDumpWriter* debug_dump_writer) { |
minyue-webrtc | 7ed35f4 | 2017-06-13 11:49:29 +0200 | [diff] [blame] | 238 | #if WEBRTC_ENABLE_PROTOBUF |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 239 | audio_network_adaptor::config::ControllerManager controller_manager_config; |
minyue-webrtc | 9c6430f | 2017-07-10 16:29:17 +0200 | [diff] [blame] | 240 | RTC_CHECK(controller_manager_config.ParseFromString(config_string)); |
| 241 | if (debug_dump_writer) |
| 242 | debug_dump_writer->DumpControllerManagerConfig(controller_manager_config, |
| 243 | rtc::TimeMillis()); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 244 | |
| 245 | std::vector<std::unique_ptr<Controller>> controllers; |
minyue | 678d2ee | 2017-05-30 07:36:20 -0700 | [diff] [blame] | 246 | std::map<const Controller*, std::pair<int, float>> scoring_points; |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 247 | |
| 248 | for (int i = 0; i < controller_manager_config.controllers_size(); ++i) { |
| 249 | auto& controller_config = controller_manager_config.controllers(i); |
| 250 | std::unique_ptr<Controller> controller; |
| 251 | switch (controller_config.controller_case()) { |
| 252 | case audio_network_adaptor::config::Controller::kFecController: |
elad.alon | 6d7900d | 2017-03-24 04:12:56 -0700 | [diff] [blame] | 253 | controller = CreateFecControllerPlrBased( |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 254 | controller_config.fec_controller(), initial_fec_enabled); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 255 | break; |
elad.alon | 2877048 | 2017-03-28 05:03:55 -0700 | [diff] [blame] | 256 | case audio_network_adaptor::config::Controller::kFecControllerRplrBased: |
Sebastian Jansson | cd2a92f | 2019-10-31 13:53:53 +0100 | [diff] [blame^] | 257 | // FecControllerRplrBased has been removed and can't be used anymore. |
| 258 | RTC_NOTREACHED(); |
| 259 | continue; |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 260 | case audio_network_adaptor::config::Controller::kFrameLengthController: |
| 261 | controller = CreateFrameLengthController( |
| 262 | controller_config.frame_length_controller(), |
michaelt | 6f08d7d | 2017-02-22 07:35:05 -0800 | [diff] [blame] | 263 | encoder_frame_lengths_ms, initial_frame_length_ms, |
| 264 | min_encoder_bitrate_bps); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 265 | break; |
| 266 | case audio_network_adaptor::config::Controller::kChannelController: |
| 267 | controller = CreateChannelController( |
| 268 | controller_config.channel_controller(), num_encoder_channels, |
| 269 | intial_channels_to_encode); |
| 270 | break; |
| 271 | case audio_network_adaptor::config::Controller::kDtxController: |
| 272 | controller = CreateDtxController(controller_config.dtx_controller(), |
| 273 | initial_dtx_enabled); |
| 274 | break; |
| 275 | case audio_network_adaptor::config::Controller::kBitrateController: |
ivoc | 7e9c614 | 2017-09-28 01:11:16 -0700 | [diff] [blame] | 276 | controller = CreateBitrateController( |
| 277 | controller_config.bitrate_controller(), initial_bitrate_bps, |
| 278 | initial_frame_length_ms); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 279 | break; |
| 280 | default: |
| 281 | RTC_NOTREACHED(); |
| 282 | } |
| 283 | if (controller_config.has_scoring_point()) { |
minyue | 678d2ee | 2017-05-30 07:36:20 -0700 | [diff] [blame] | 284 | auto& scoring_point = controller_config.scoring_point(); |
| 285 | RTC_CHECK(scoring_point.has_uplink_bandwidth_bps()); |
| 286 | RTC_CHECK(scoring_point.has_uplink_packet_loss_fraction()); |
| 287 | scoring_points[controller.get()] = std::make_pair<int, float>( |
| 288 | scoring_point.uplink_bandwidth_bps(), |
| 289 | scoring_point.uplink_packet_loss_fraction()); |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 290 | } |
| 291 | controllers.push_back(std::move(controller)); |
| 292 | } |
| 293 | |
minyue | 12de077 | 2017-05-30 08:56:08 -0700 | [diff] [blame] | 294 | if (scoring_points.size() == 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 295 | return std::unique_ptr<ControllerManagerImpl>( |
| 296 | new ControllerManagerImpl(ControllerManagerImpl::Config(0, 0), |
| 297 | std::move(controllers), scoring_points)); |
minyue | 12de077 | 2017-05-30 08:56:08 -0700 | [diff] [blame] | 298 | } else { |
| 299 | RTC_CHECK(controller_manager_config.has_min_reordering_time_ms()); |
| 300 | RTC_CHECK(controller_manager_config.has_min_reordering_squared_distance()); |
| 301 | return std::unique_ptr<ControllerManagerImpl>(new ControllerManagerImpl( |
| 302 | ControllerManagerImpl::Config( |
| 303 | controller_manager_config.min_reordering_time_ms(), |
| 304 | controller_manager_config.min_reordering_squared_distance()), |
| 305 | std::move(controllers), scoring_points)); |
| 306 | } |
| 307 | |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 308 | #else |
| 309 | RTC_NOTREACHED(); |
| 310 | return nullptr; |
minyue-webrtc | 7ed35f4 | 2017-06-13 11:49:29 +0200 | [diff] [blame] | 311 | #endif // WEBRTC_ENABLE_PROTOBUF |
minyue | a1d9ad0 | 2016-10-02 14:53:37 -0700 | [diff] [blame] | 312 | } |
| 313 | |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 314 | ControllerManagerImpl::ControllerManagerImpl(const Config& config) |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 315 | : ControllerManagerImpl( |
| 316 | config, |
| 317 | std::vector<std::unique_ptr<Controller>>(), |
| 318 | std::map<const Controller*, std::pair<int, float>>()) {} |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 319 | |
| 320 | ControllerManagerImpl::ControllerManagerImpl( |
| 321 | const Config& config, |
minyue-webrtc | 5d68910 | 2017-08-14 14:33:32 +0200 | [diff] [blame] | 322 | std::vector<std::unique_ptr<Controller>> controllers, |
minyue | 678d2ee | 2017-05-30 07:36:20 -0700 | [diff] [blame] | 323 | const std::map<const Controller*, std::pair<int, float>>& scoring_points) |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 324 | : config_(config), |
| 325 | controllers_(std::move(controllers)), |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 326 | last_reordering_time_ms_(absl::nullopt), |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 327 | last_scoring_point_(0, 0.0) { |
| 328 | for (auto& controller : controllers_) |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 329 | default_sorted_controllers_.push_back(controller.get()); |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 330 | sorted_controllers_ = default_sorted_controllers_; |
minyue | 678d2ee | 2017-05-30 07:36:20 -0700 | [diff] [blame] | 331 | for (auto& controller_point : scoring_points) { |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 332 | controller_scoring_points_.insert(std::make_pair( |
| 333 | controller_point.first, ScoringPoint(controller_point.second.first, |
| 334 | controller_point.second.second))); |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | ControllerManagerImpl::~ControllerManagerImpl() = default; |
| 339 | |
| 340 | std::vector<Controller*> ControllerManagerImpl::GetSortedControllers( |
| 341 | const Controller::NetworkMetrics& metrics) { |
minyue | 12de077 | 2017-05-30 08:56:08 -0700 | [diff] [blame] | 342 | if (controller_scoring_points_.size() == 0) |
| 343 | return default_sorted_controllers_; |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 344 | |
| 345 | if (!metrics.uplink_bandwidth_bps || !metrics.uplink_packet_loss_fraction) |
| 346 | return sorted_controllers_; |
| 347 | |
minyue | 12de077 | 2017-05-30 08:56:08 -0700 | [diff] [blame] | 348 | const int64_t now_ms = rtc::TimeMillis(); |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 349 | if (last_reordering_time_ms_ && |
| 350 | now_ms - *last_reordering_time_ms_ < config_.min_reordering_time_ms) |
| 351 | return sorted_controllers_; |
| 352 | |
| 353 | ScoringPoint scoring_point(*metrics.uplink_bandwidth_bps, |
| 354 | *metrics.uplink_packet_loss_fraction); |
| 355 | |
| 356 | if (last_reordering_time_ms_ && |
| 357 | last_scoring_point_.SquaredDistanceTo(scoring_point) < |
| 358 | config_.min_reordering_squared_distance) |
| 359 | return sorted_controllers_; |
| 360 | |
| 361 | // Sort controllers according to the distances of |scoring_point| to the |
minyue | 678d2ee | 2017-05-30 07:36:20 -0700 | [diff] [blame] | 362 | // scoring points of controllers. |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 363 | // |
| 364 | // A controller that does not associate with any scoring point |
| 365 | // are treated as if |
| 366 | // 1) they are less important than any controller that has a scoring point, |
| 367 | // 2) they are equally important to any controller that has no scoring point, |
| 368 | // and their relative order will follow |default_sorted_controllers_|. |
| 369 | std::vector<Controller*> sorted_controllers(default_sorted_controllers_); |
| 370 | std::stable_sort( |
| 371 | sorted_controllers.begin(), sorted_controllers.end(), |
| 372 | [this, &scoring_point](const Controller* lhs, const Controller* rhs) { |
| 373 | auto lhs_scoring_point = controller_scoring_points_.find(lhs); |
| 374 | auto rhs_scoring_point = controller_scoring_points_.find(rhs); |
| 375 | |
| 376 | if (lhs_scoring_point == controller_scoring_points_.end()) |
| 377 | return false; |
| 378 | |
| 379 | if (rhs_scoring_point == controller_scoring_points_.end()) |
| 380 | return true; |
| 381 | |
| 382 | return lhs_scoring_point->second.SquaredDistanceTo(scoring_point) < |
| 383 | rhs_scoring_point->second.SquaredDistanceTo(scoring_point); |
| 384 | }); |
| 385 | |
| 386 | if (sorted_controllers_ != sorted_controllers) { |
| 387 | sorted_controllers_ = sorted_controllers; |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 388 | last_reordering_time_ms_ = now_ms; |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 389 | last_scoring_point_ = scoring_point; |
| 390 | } |
| 391 | return sorted_controllers_; |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | std::vector<Controller*> ControllerManagerImpl::GetControllers() const { |
| 395 | return default_sorted_controllers_; |
| 396 | } |
| 397 | |
minyue | bc77ed7 | 2016-09-22 00:45:16 -0700 | [diff] [blame] | 398 | ControllerManagerImpl::ScoringPoint::ScoringPoint( |
| 399 | int uplink_bandwidth_bps, |
| 400 | float uplink_packet_loss_fraction) |
| 401 | : uplink_bandwidth_bps(uplink_bandwidth_bps), |
| 402 | uplink_packet_loss_fraction(uplink_packet_loss_fraction) {} |
| 403 | |
| 404 | namespace { |
| 405 | |
| 406 | constexpr int kMinUplinkBandwidthBps = 0; |
| 407 | constexpr int kMaxUplinkBandwidthBps = 120000; |
| 408 | |
| 409 | float NormalizeUplinkBandwidth(int uplink_bandwidth_bps) { |
| 410 | uplink_bandwidth_bps = |
| 411 | std::min(kMaxUplinkBandwidthBps, |
| 412 | std::max(kMinUplinkBandwidthBps, uplink_bandwidth_bps)); |
| 413 | return static_cast<float>(uplink_bandwidth_bps - kMinUplinkBandwidthBps) / |
| 414 | (kMaxUplinkBandwidthBps - kMinUplinkBandwidthBps); |
| 415 | } |
| 416 | |
| 417 | float NormalizePacketLossFraction(float uplink_packet_loss_fraction) { |
| 418 | // |uplink_packet_loss_fraction| is seldom larger than 0.3, so we scale it up |
| 419 | // by 3.3333f. |
| 420 | return std::min(uplink_packet_loss_fraction * 3.3333f, 1.0f); |
| 421 | } |
| 422 | |
| 423 | } // namespace |
| 424 | |
| 425 | float ControllerManagerImpl::ScoringPoint::SquaredDistanceTo( |
| 426 | const ScoringPoint& scoring_point) const { |
| 427 | float diff_normalized_bitrate_bps = |
| 428 | NormalizeUplinkBandwidth(scoring_point.uplink_bandwidth_bps) - |
| 429 | NormalizeUplinkBandwidth(uplink_bandwidth_bps); |
| 430 | float diff_normalized_packet_loss = |
| 431 | NormalizePacketLossFraction(scoring_point.uplink_packet_loss_fraction) - |
| 432 | NormalizePacketLossFraction(uplink_packet_loss_fraction); |
| 433 | return std::pow(diff_normalized_bitrate_bps, 2) + |
| 434 | std::pow(diff_normalized_packet_loss, 2); |
| 435 | } |
| 436 | |
minyue | caa9cb2 | 2016-09-13 13:34:15 -0700 | [diff] [blame] | 437 | } // namespace webrtc |