blob: c7aad1da8799c332a735691da215d876b5c99762 [file] [log] [blame]
minyuecaa9cb22016-09-13 13:34:15 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/audio_network_adaptor/controller_manager.h"
minyuebc77ed72016-09-22 00:45:16 -070012
13#include <cmath>
Mirko Bonadeie45c6882019-02-16 09:59:29 +010014#include <string>
minyuecaa9cb22016-09-13 13:34:15 -070015#include <utility>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#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 Bonadei92ea95e2017-09-15 06:47:31 +020022#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 Janssoncd2a92f2019-10-31 13:53:53 +010025#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/time_utils.h"
minyuecaa9cb22016-09-13 13:34:15 -070027
minyue-webrtc7ed35f42017-06-13 11:49:29 +020028#if WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -070029RTC_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 Bonadei92ea95e2017-09-15 06:47:31 +020033#include "modules/audio_coding/audio_network_adaptor/config.pb.h"
minyuea1d9ad02016-10-02 14:53:37 -070034#endif
35RTC_POP_IGNORING_WUNDEF()
36#endif
37
minyuecaa9cb22016-09-13 13:34:15 -070038namespace webrtc {
39
minyuea1d9ad02016-10-02 14:53:37 -070040namespace {
41
minyue-webrtc7ed35f42017-06-13 11:49:29 +020042#if WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -070043
elad.alon6d7900d2017-03-24 04:12:56 -070044std::unique_ptr<FecControllerPlrBased> CreateFecControllerPlrBased(
minyuea1d9ad02016-10-02 14:53:37 -070045 const audio_network_adaptor::config::FecController& config,
michaelt92aef172017-04-18 00:11:48 -070046 bool initial_fec_enabled) {
minyuea1d9ad02016-10-02 14:53:37 -070047 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.alon6d7900d2017-03-24 04:12:56 -070063 return std::unique_ptr<FecControllerPlrBased>(
64 new FecControllerPlrBased(FecControllerPlrBased::Config(
65 initial_fec_enabled,
elad.alon326263a2017-03-29 03:16:58 -070066 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()),
michaelt92aef172017-04-18 00:11:48 -070074 config.time_constant_ms())));
minyuea1d9ad02016-10-02 14:53:37 -070075}
76
77std::unique_ptr<FrameLengthController> CreateFrameLengthController(
78 const audio_network_adaptor::config::FrameLengthController& config,
79 rtc::ArrayView<const int> encoder_frame_lengths_ms,
michaelt6f08d7d2017-02-22 07:35:05 -080080 int initial_frame_length_ms,
81 int min_encoder_bitrate_bps) {
minyuea1d9ad02016-10-02 14:53:37 -070082 RTC_CHECK(config.has_fl_increasing_packet_loss_fraction());
83 RTC_CHECK(config.has_fl_decreasing_packet_loss_fraction());
minyuea1d9ad02016-10-02 14:53:37 -070084
michaelta55f0212017-02-02 07:47:19 -080085 std::map<FrameLengthController::Config::FrameLengthChange, int>
Ying Wang0e1a5582019-08-16 16:45:30 +020086 fl_changing_bandwidths_bps;
michaelta55f0212017-02-02 07:47:19 -080087
Ying Wang0e1a5582019-08-16 16:45:30 +020088 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()) {
michaelta55f0212017-02-02 07:47:19 -0800125 fl_changing_bandwidths_bps.insert(std::make_pair(
126 FrameLengthController::Config::FrameLengthChange(60, 120),
127 config.fl_60ms_to_120ms_bandwidth_bps()));
Ying Wang0e1a5582019-08-16 16:45:30 +0200128 }
129
130 if (config.has_fl_120ms_to_60ms_bandwidth_bps()) {
michaelta55f0212017-02-02 07:47:19 -0800131 fl_changing_bandwidths_bps.insert(std::make_pair(
132 FrameLengthController::Config::FrameLengthChange(120, 60),
133 config.fl_120ms_to_60ms_bandwidth_bps()));
134 }
135
ivoc7e9c6142017-09-28 01:11:16 -0700136 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
minyuea1d9ad02016-10-02 14:53:37 -0700145 FrameLengthController::Config ctor_config(
Minyue Li8319e7f2019-01-02 16:57:02 +0100146 std::set<int>(), initial_frame_length_ms, min_encoder_bitrate_bps,
minyuea1d9ad02016-10-02 14:53:37 -0700147 config.fl_increasing_packet_loss_fraction(),
ivoc7e9c6142017-09-28 01:11:16 -0700148 config.fl_decreasing_packet_loss_fraction(), fl_increase_overhead_offset,
149 fl_decrease_overhead_offset, std::move(fl_changing_bandwidths_bps));
minyuea1d9ad02016-10-02 14:53:37 -0700150
151 for (auto frame_length : encoder_frame_lengths_ms)
Minyue Li8319e7f2019-01-02 16:57:02 +0100152 ctor_config.encoder_frame_lengths_ms.insert(frame_length);
minyuea1d9ad02016-10-02 14:53:37 -0700153
154 return std::unique_ptr<FrameLengthController>(
155 new FrameLengthController(ctor_config));
156}
157
158std::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
171std::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
182using audio_network_adaptor::BitrateController;
183std::unique_ptr<BitrateController> CreateBitrateController(
ivoc7e9c6142017-09-28 01:11:16 -0700184 const audio_network_adaptor::config::BitrateController& bitrate_config,
minyuea1d9ad02016-10-02 14:53:37 -0700185 int initial_bitrate_bps,
186 int initial_frame_length_ms) {
ivoc7e9c6142017-09-28 01:11:16 -0700187 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)));
minyuea1d9ad02016-10-02 14:53:37 -0700199}
minyue-webrtc7ed35f42017-06-13 11:49:29 +0200200#endif // WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -0700201
202} // namespace
203
minyuebc77ed72016-09-22 00:45:16 -0700204ControllerManagerImpl::Config::Config(int min_reordering_time_ms,
michaelt92aef172017-04-18 00:11:48 -0700205 float min_reordering_squared_distance)
minyuebc77ed72016-09-22 00:45:16 -0700206 : min_reordering_time_ms(min_reordering_time_ms),
michaelt92aef172017-04-18 00:11:48 -0700207 min_reordering_squared_distance(min_reordering_squared_distance) {}
minyuecaa9cb22016-09-13 13:34:15 -0700208
209ControllerManagerImpl::Config::~Config() = default;
210
minyuea1d9ad02016-10-02 14:53:37 -0700211std::unique_ptr<ControllerManager> ControllerManagerImpl::Create(
Mirko Bonadeie45c6882019-02-16 09:59:29 +0100212 const std::string& config_string,
minyuea1d9ad02016-10-02 14:53:37 -0700213 size_t num_encoder_channels,
214 rtc::ArrayView<const int> encoder_frame_lengths_ms,
michaelt6f08d7d2017-02-22 07:35:05 -0800215 int min_encoder_bitrate_bps,
minyuea1d9ad02016-10-02 14:53:37 -0700216 size_t intial_channels_to_encode,
217 int initial_frame_length_ms,
218 int initial_bitrate_bps,
219 bool initial_fec_enabled,
michaelt92aef172017-04-18 00:11:48 -0700220 bool initial_dtx_enabled) {
minyue-webrtc9c6430f2017-07-10 16:29:17 +0200221 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
227std::unique_ptr<ControllerManager> ControllerManagerImpl::Create(
Mirko Bonadeie45c6882019-02-16 09:59:29 +0100228 const std::string& config_string,
minyue-webrtc9c6430f2017-07-10 16:29:17 +0200229 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-webrtc7ed35f42017-06-13 11:49:29 +0200238#if WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -0700239 audio_network_adaptor::config::ControllerManager controller_manager_config;
minyue-webrtc9c6430f2017-07-10 16:29:17 +0200240 RTC_CHECK(controller_manager_config.ParseFromString(config_string));
241 if (debug_dump_writer)
242 debug_dump_writer->DumpControllerManagerConfig(controller_manager_config,
243 rtc::TimeMillis());
minyuea1d9ad02016-10-02 14:53:37 -0700244
245 std::vector<std::unique_ptr<Controller>> controllers;
minyue678d2ee2017-05-30 07:36:20 -0700246 std::map<const Controller*, std::pair<int, float>> scoring_points;
minyuea1d9ad02016-10-02 14:53:37 -0700247
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.alon6d7900d2017-03-24 04:12:56 -0700253 controller = CreateFecControllerPlrBased(
michaelt92aef172017-04-18 00:11:48 -0700254 controller_config.fec_controller(), initial_fec_enabled);
minyuea1d9ad02016-10-02 14:53:37 -0700255 break;
elad.alon28770482017-03-28 05:03:55 -0700256 case audio_network_adaptor::config::Controller::kFecControllerRplrBased:
Sebastian Janssoncd2a92f2019-10-31 13:53:53 +0100257 // FecControllerRplrBased has been removed and can't be used anymore.
258 RTC_NOTREACHED();
259 continue;
minyuea1d9ad02016-10-02 14:53:37 -0700260 case audio_network_adaptor::config::Controller::kFrameLengthController:
261 controller = CreateFrameLengthController(
262 controller_config.frame_length_controller(),
michaelt6f08d7d2017-02-22 07:35:05 -0800263 encoder_frame_lengths_ms, initial_frame_length_ms,
264 min_encoder_bitrate_bps);
minyuea1d9ad02016-10-02 14:53:37 -0700265 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:
ivoc7e9c6142017-09-28 01:11:16 -0700276 controller = CreateBitrateController(
277 controller_config.bitrate_controller(), initial_bitrate_bps,
278 initial_frame_length_ms);
minyuea1d9ad02016-10-02 14:53:37 -0700279 break;
280 default:
281 RTC_NOTREACHED();
282 }
283 if (controller_config.has_scoring_point()) {
minyue678d2ee2017-05-30 07:36:20 -0700284 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());
minyuea1d9ad02016-10-02 14:53:37 -0700290 }
291 controllers.push_back(std::move(controller));
292 }
293
minyue12de0772017-05-30 08:56:08 -0700294 if (scoring_points.size() == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200295 return std::unique_ptr<ControllerManagerImpl>(
296 new ControllerManagerImpl(ControllerManagerImpl::Config(0, 0),
297 std::move(controllers), scoring_points));
minyue12de0772017-05-30 08:56:08 -0700298 } 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
minyuea1d9ad02016-10-02 14:53:37 -0700308#else
309 RTC_NOTREACHED();
310 return nullptr;
minyue-webrtc7ed35f42017-06-13 11:49:29 +0200311#endif // WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -0700312}
313
minyuecaa9cb22016-09-13 13:34:15 -0700314ControllerManagerImpl::ControllerManagerImpl(const Config& config)
minyuebc77ed72016-09-22 00:45:16 -0700315 : ControllerManagerImpl(
316 config,
317 std::vector<std::unique_ptr<Controller>>(),
318 std::map<const Controller*, std::pair<int, float>>()) {}
minyuecaa9cb22016-09-13 13:34:15 -0700319
320ControllerManagerImpl::ControllerManagerImpl(
321 const Config& config,
minyue-webrtc5d689102017-08-14 14:33:32 +0200322 std::vector<std::unique_ptr<Controller>> controllers,
minyue678d2ee2017-05-30 07:36:20 -0700323 const std::map<const Controller*, std::pair<int, float>>& scoring_points)
minyuebc77ed72016-09-22 00:45:16 -0700324 : config_(config),
325 controllers_(std::move(controllers)),
Danil Chapovalovb6021232018-06-19 13:26:36 +0200326 last_reordering_time_ms_(absl::nullopt),
minyuebc77ed72016-09-22 00:45:16 -0700327 last_scoring_point_(0, 0.0) {
328 for (auto& controller : controllers_)
minyuecaa9cb22016-09-13 13:34:15 -0700329 default_sorted_controllers_.push_back(controller.get());
minyuebc77ed72016-09-22 00:45:16 -0700330 sorted_controllers_ = default_sorted_controllers_;
minyue678d2ee2017-05-30 07:36:20 -0700331 for (auto& controller_point : scoring_points) {
minyuebc77ed72016-09-22 00:45:16 -0700332 controller_scoring_points_.insert(std::make_pair(
333 controller_point.first, ScoringPoint(controller_point.second.first,
334 controller_point.second.second)));
minyuecaa9cb22016-09-13 13:34:15 -0700335 }
336}
337
338ControllerManagerImpl::~ControllerManagerImpl() = default;
339
340std::vector<Controller*> ControllerManagerImpl::GetSortedControllers(
341 const Controller::NetworkMetrics& metrics) {
minyue12de0772017-05-30 08:56:08 -0700342 if (controller_scoring_points_.size() == 0)
343 return default_sorted_controllers_;
minyuebc77ed72016-09-22 00:45:16 -0700344
345 if (!metrics.uplink_bandwidth_bps || !metrics.uplink_packet_loss_fraction)
346 return sorted_controllers_;
347
minyue12de0772017-05-30 08:56:08 -0700348 const int64_t now_ms = rtc::TimeMillis();
minyuebc77ed72016-09-22 00:45:16 -0700349 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
minyue678d2ee2017-05-30 07:36:20 -0700362 // scoring points of controllers.
minyuebc77ed72016-09-22 00:45:16 -0700363 //
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 Sundbom12ab00b2017-11-16 15:31:38 +0100388 last_reordering_time_ms_ = now_ms;
minyuebc77ed72016-09-22 00:45:16 -0700389 last_scoring_point_ = scoring_point;
390 }
391 return sorted_controllers_;
minyuecaa9cb22016-09-13 13:34:15 -0700392}
393
394std::vector<Controller*> ControllerManagerImpl::GetControllers() const {
395 return default_sorted_controllers_;
396}
397
minyuebc77ed72016-09-22 00:45:16 -0700398ControllerManagerImpl::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
404namespace {
405
406constexpr int kMinUplinkBandwidthBps = 0;
407constexpr int kMaxUplinkBandwidthBps = 120000;
408
409float 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
417float 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
425float 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
minyuecaa9cb22016-09-13 13:34:15 -0700437} // namespace webrtc