blob: 80255eac69b24ee61c78f6371d402a0c29b5d927 [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>
minyuecaa9cb22016-09-13 13:34:15 -070014#include <utility>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_coding/audio_network_adaptor/bitrate_controller.h"
17#include "modules/audio_coding/audio_network_adaptor/channel_controller.h"
18#include "modules/audio_coding/audio_network_adaptor/debug_dump_writer.h"
19#include "modules/audio_coding/audio_network_adaptor/dtx_controller.h"
20#include "modules/audio_coding/audio_network_adaptor/fec_controller_plr_based.h"
21#include "modules/audio_coding/audio_network_adaptor/fec_controller_rplr_based.h"
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"
25#include "rtc_base/timeutils.h"
minyuecaa9cb22016-09-13 13:34:15 -070026
minyue-webrtc7ed35f42017-06-13 11:49:29 +020027#if WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -070028RTC_PUSH_IGNORING_WUNDEF()
29#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
30#include "external/webrtc/webrtc/modules/audio_coding/audio_network_adaptor/config.pb.h"
31#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_coding/audio_network_adaptor/config.pb.h"
minyuea1d9ad02016-10-02 14:53:37 -070033#endif
34RTC_POP_IGNORING_WUNDEF()
35#endif
36
minyuecaa9cb22016-09-13 13:34:15 -070037namespace webrtc {
38
minyuea1d9ad02016-10-02 14:53:37 -070039namespace {
40
minyue-webrtc7ed35f42017-06-13 11:49:29 +020041#if WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -070042
elad.alon6d7900d2017-03-24 04:12:56 -070043std::unique_ptr<FecControllerPlrBased> CreateFecControllerPlrBased(
minyuea1d9ad02016-10-02 14:53:37 -070044 const audio_network_adaptor::config::FecController& config,
michaelt92aef172017-04-18 00:11:48 -070045 bool initial_fec_enabled) {
minyuea1d9ad02016-10-02 14:53:37 -070046 RTC_CHECK(config.has_fec_enabling_threshold());
47 RTC_CHECK(config.has_fec_disabling_threshold());
48 RTC_CHECK(config.has_time_constant_ms());
49
50 auto& fec_enabling_threshold = config.fec_enabling_threshold();
51 RTC_CHECK(fec_enabling_threshold.has_low_bandwidth_bps());
52 RTC_CHECK(fec_enabling_threshold.has_low_bandwidth_packet_loss());
53 RTC_CHECK(fec_enabling_threshold.has_high_bandwidth_bps());
54 RTC_CHECK(fec_enabling_threshold.has_high_bandwidth_packet_loss());
55
56 auto& fec_disabling_threshold = config.fec_disabling_threshold();
57 RTC_CHECK(fec_disabling_threshold.has_low_bandwidth_bps());
58 RTC_CHECK(fec_disabling_threshold.has_low_bandwidth_packet_loss());
59 RTC_CHECK(fec_disabling_threshold.has_high_bandwidth_bps());
60 RTC_CHECK(fec_disabling_threshold.has_high_bandwidth_packet_loss());
61
elad.alon6d7900d2017-03-24 04:12:56 -070062 return std::unique_ptr<FecControllerPlrBased>(
63 new FecControllerPlrBased(FecControllerPlrBased::Config(
64 initial_fec_enabled,
elad.alon326263a2017-03-29 03:16:58 -070065 ThresholdCurve(fec_enabling_threshold.low_bandwidth_bps(),
66 fec_enabling_threshold.low_bandwidth_packet_loss(),
67 fec_enabling_threshold.high_bandwidth_bps(),
68 fec_enabling_threshold.high_bandwidth_packet_loss()),
69 ThresholdCurve(fec_disabling_threshold.low_bandwidth_bps(),
70 fec_disabling_threshold.low_bandwidth_packet_loss(),
71 fec_disabling_threshold.high_bandwidth_bps(),
72 fec_disabling_threshold.high_bandwidth_packet_loss()),
michaelt92aef172017-04-18 00:11:48 -070073 config.time_constant_ms())));
minyuea1d9ad02016-10-02 14:53:37 -070074}
75
elad.alon28770482017-03-28 05:03:55 -070076std::unique_ptr<FecControllerRplrBased> CreateFecControllerRplrBased(
77 const audio_network_adaptor::config::FecControllerRplrBased& config,
78 bool initial_fec_enabled) {
79 RTC_CHECK(config.has_fec_enabling_threshold());
80 RTC_CHECK(config.has_fec_disabling_threshold());
81
82 auto& fec_enabling_threshold = config.fec_enabling_threshold();
83 RTC_CHECK(fec_enabling_threshold.has_low_bandwidth_bps());
84 RTC_CHECK(fec_enabling_threshold.has_low_bandwidth_recoverable_packet_loss());
85 RTC_CHECK(fec_enabling_threshold.has_high_bandwidth_bps());
86 RTC_CHECK(
87 fec_enabling_threshold.has_high_bandwidth_recoverable_packet_loss());
88
89 auto& fec_disabling_threshold = config.fec_disabling_threshold();
90 RTC_CHECK(fec_disabling_threshold.has_low_bandwidth_bps());
91 RTC_CHECK(
92 fec_disabling_threshold.has_low_bandwidth_recoverable_packet_loss());
93 RTC_CHECK(fec_disabling_threshold.has_high_bandwidth_bps());
94 RTC_CHECK(
95 fec_disabling_threshold.has_high_bandwidth_recoverable_packet_loss());
96
97 return std::unique_ptr<FecControllerRplrBased>(
98 new FecControllerRplrBased(FecControllerRplrBased::Config(
99 initial_fec_enabled,
elad.alon326263a2017-03-29 03:16:58 -0700100 ThresholdCurve(
elad.alon28770482017-03-28 05:03:55 -0700101 fec_enabling_threshold.low_bandwidth_bps(),
102 fec_enabling_threshold.low_bandwidth_recoverable_packet_loss(),
103 fec_enabling_threshold.high_bandwidth_bps(),
104 fec_enabling_threshold.high_bandwidth_recoverable_packet_loss()),
elad.alon326263a2017-03-29 03:16:58 -0700105 ThresholdCurve(
elad.alon28770482017-03-28 05:03:55 -0700106 fec_disabling_threshold.low_bandwidth_bps(),
107 fec_disabling_threshold.low_bandwidth_recoverable_packet_loss(),
108 fec_disabling_threshold.high_bandwidth_bps(),
109 fec_disabling_threshold
110 .high_bandwidth_recoverable_packet_loss()))));
111}
112
minyuea1d9ad02016-10-02 14:53:37 -0700113std::unique_ptr<FrameLengthController> CreateFrameLengthController(
114 const audio_network_adaptor::config::FrameLengthController& config,
115 rtc::ArrayView<const int> encoder_frame_lengths_ms,
michaelt6f08d7d2017-02-22 07:35:05 -0800116 int initial_frame_length_ms,
117 int min_encoder_bitrate_bps) {
minyuea1d9ad02016-10-02 14:53:37 -0700118 RTC_CHECK(config.has_fl_increasing_packet_loss_fraction());
119 RTC_CHECK(config.has_fl_decreasing_packet_loss_fraction());
120 RTC_CHECK(config.has_fl_20ms_to_60ms_bandwidth_bps());
121 RTC_CHECK(config.has_fl_60ms_to_20ms_bandwidth_bps());
122
michaelta55f0212017-02-02 07:47:19 -0800123 std::map<FrameLengthController::Config::FrameLengthChange, int>
124 fl_changing_bandwidths_bps = {
125 {FrameLengthController::Config::FrameLengthChange(20, 60),
126 config.fl_20ms_to_60ms_bandwidth_bps()},
127 {FrameLengthController::Config::FrameLengthChange(60, 20),
128 config.fl_60ms_to_20ms_bandwidth_bps()}};
129
130 if (config.has_fl_60ms_to_120ms_bandwidth_bps() &&
131 config.has_fl_120ms_to_60ms_bandwidth_bps()) {
132 fl_changing_bandwidths_bps.insert(std::make_pair(
133 FrameLengthController::Config::FrameLengthChange(60, 120),
134 config.fl_60ms_to_120ms_bandwidth_bps()));
135 fl_changing_bandwidths_bps.insert(std::make_pair(
136 FrameLengthController::Config::FrameLengthChange(120, 60),
137 config.fl_120ms_to_60ms_bandwidth_bps()));
138 }
139
ivoc7e9c6142017-09-28 01:11:16 -0700140 int fl_increase_overhead_offset = 0;
141 if (config.has_fl_increase_overhead_offset()) {
142 fl_increase_overhead_offset = config.fl_increase_overhead_offset();
143 }
144 int fl_decrease_overhead_offset = 0;
145 if (config.has_fl_decrease_overhead_offset()) {
146 fl_decrease_overhead_offset = config.fl_decrease_overhead_offset();
147 }
148
minyuea1d9ad02016-10-02 14:53:37 -0700149 FrameLengthController::Config ctor_config(
michaelt6f08d7d2017-02-22 07:35:05 -0800150 std::vector<int>(), initial_frame_length_ms, min_encoder_bitrate_bps,
minyuea1d9ad02016-10-02 14:53:37 -0700151 config.fl_increasing_packet_loss_fraction(),
ivoc7e9c6142017-09-28 01:11:16 -0700152 config.fl_decreasing_packet_loss_fraction(), fl_increase_overhead_offset,
153 fl_decrease_overhead_offset, std::move(fl_changing_bandwidths_bps));
minyuea1d9ad02016-10-02 14:53:37 -0700154
155 for (auto frame_length : encoder_frame_lengths_ms)
156 ctor_config.encoder_frame_lengths_ms.push_back(frame_length);
157
158 return std::unique_ptr<FrameLengthController>(
159 new FrameLengthController(ctor_config));
160}
161
162std::unique_ptr<ChannelController> CreateChannelController(
163 const audio_network_adaptor::config::ChannelController& config,
164 size_t num_encoder_channels,
165 size_t intial_channels_to_encode) {
166 RTC_CHECK(config.has_channel_1_to_2_bandwidth_bps());
167 RTC_CHECK(config.has_channel_2_to_1_bandwidth_bps());
168
169 return std::unique_ptr<ChannelController>(new ChannelController(
170 ChannelController::Config(num_encoder_channels, intial_channels_to_encode,
171 config.channel_1_to_2_bandwidth_bps(),
172 config.channel_2_to_1_bandwidth_bps())));
173}
174
175std::unique_ptr<DtxController> CreateDtxController(
176 const audio_network_adaptor::config::DtxController& dtx_config,
177 bool initial_dtx_enabled) {
178 RTC_CHECK(dtx_config.has_dtx_enabling_bandwidth_bps());
179 RTC_CHECK(dtx_config.has_dtx_disabling_bandwidth_bps());
180
181 return std::unique_ptr<DtxController>(new DtxController(DtxController::Config(
182 initial_dtx_enabled, dtx_config.dtx_enabling_bandwidth_bps(),
183 dtx_config.dtx_disabling_bandwidth_bps())));
184}
185
186using audio_network_adaptor::BitrateController;
187std::unique_ptr<BitrateController> CreateBitrateController(
ivoc7e9c6142017-09-28 01:11:16 -0700188 const audio_network_adaptor::config::BitrateController& bitrate_config,
minyuea1d9ad02016-10-02 14:53:37 -0700189 int initial_bitrate_bps,
190 int initial_frame_length_ms) {
ivoc7e9c6142017-09-28 01:11:16 -0700191 int fl_increase_overhead_offset = 0;
192 if (bitrate_config.has_fl_increase_overhead_offset()) {
193 fl_increase_overhead_offset = bitrate_config.fl_increase_overhead_offset();
194 }
195 int fl_decrease_overhead_offset = 0;
196 if (bitrate_config.has_fl_decrease_overhead_offset()) {
197 fl_decrease_overhead_offset = bitrate_config.fl_decrease_overhead_offset();
198 }
199 return std::unique_ptr<BitrateController>(
200 new BitrateController(BitrateController::Config(
201 initial_bitrate_bps, initial_frame_length_ms,
202 fl_increase_overhead_offset, fl_decrease_overhead_offset)));
minyuea1d9ad02016-10-02 14:53:37 -0700203}
minyue-webrtc7ed35f42017-06-13 11:49:29 +0200204#endif // WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -0700205
206} // namespace
207
minyuebc77ed72016-09-22 00:45:16 -0700208ControllerManagerImpl::Config::Config(int min_reordering_time_ms,
michaelt92aef172017-04-18 00:11:48 -0700209 float min_reordering_squared_distance)
minyuebc77ed72016-09-22 00:45:16 -0700210 : min_reordering_time_ms(min_reordering_time_ms),
michaelt92aef172017-04-18 00:11:48 -0700211 min_reordering_squared_distance(min_reordering_squared_distance) {}
minyuecaa9cb22016-09-13 13:34:15 -0700212
213ControllerManagerImpl::Config::~Config() = default;
214
minyuea1d9ad02016-10-02 14:53:37 -0700215std::unique_ptr<ControllerManager> ControllerManagerImpl::Create(
mbonadei7c2c8432017-04-07 00:59:12 -0700216 const ProtoString& config_string,
minyuea1d9ad02016-10-02 14:53:37 -0700217 size_t num_encoder_channels,
218 rtc::ArrayView<const int> encoder_frame_lengths_ms,
michaelt6f08d7d2017-02-22 07:35:05 -0800219 int min_encoder_bitrate_bps,
minyuea1d9ad02016-10-02 14:53:37 -0700220 size_t intial_channels_to_encode,
221 int initial_frame_length_ms,
222 int initial_bitrate_bps,
223 bool initial_fec_enabled,
michaelt92aef172017-04-18 00:11:48 -0700224 bool initial_dtx_enabled) {
minyue-webrtc9c6430f2017-07-10 16:29:17 +0200225 return Create(config_string, num_encoder_channels, encoder_frame_lengths_ms,
226 min_encoder_bitrate_bps, intial_channels_to_encode,
227 initial_frame_length_ms, initial_bitrate_bps,
228 initial_fec_enabled, initial_dtx_enabled, nullptr);
229}
230
231std::unique_ptr<ControllerManager> ControllerManagerImpl::Create(
232 const ProtoString& config_string,
233 size_t num_encoder_channels,
234 rtc::ArrayView<const int> encoder_frame_lengths_ms,
235 int min_encoder_bitrate_bps,
236 size_t intial_channels_to_encode,
237 int initial_frame_length_ms,
238 int initial_bitrate_bps,
239 bool initial_fec_enabled,
240 bool initial_dtx_enabled,
241 DebugDumpWriter* debug_dump_writer) {
minyue-webrtc7ed35f42017-06-13 11:49:29 +0200242#if WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -0700243 audio_network_adaptor::config::ControllerManager controller_manager_config;
minyue-webrtc9c6430f2017-07-10 16:29:17 +0200244 RTC_CHECK(controller_manager_config.ParseFromString(config_string));
245 if (debug_dump_writer)
246 debug_dump_writer->DumpControllerManagerConfig(controller_manager_config,
247 rtc::TimeMillis());
minyuea1d9ad02016-10-02 14:53:37 -0700248
249 std::vector<std::unique_ptr<Controller>> controllers;
minyue678d2ee2017-05-30 07:36:20 -0700250 std::map<const Controller*, std::pair<int, float>> scoring_points;
minyuea1d9ad02016-10-02 14:53:37 -0700251
252 for (int i = 0; i < controller_manager_config.controllers_size(); ++i) {
253 auto& controller_config = controller_manager_config.controllers(i);
254 std::unique_ptr<Controller> controller;
255 switch (controller_config.controller_case()) {
256 case audio_network_adaptor::config::Controller::kFecController:
elad.alon6d7900d2017-03-24 04:12:56 -0700257 controller = CreateFecControllerPlrBased(
michaelt92aef172017-04-18 00:11:48 -0700258 controller_config.fec_controller(), initial_fec_enabled);
minyuea1d9ad02016-10-02 14:53:37 -0700259 break;
elad.alon28770482017-03-28 05:03:55 -0700260 case audio_network_adaptor::config::Controller::kFecControllerRplrBased:
261 controller = CreateFecControllerRplrBased(
262 controller_config.fec_controller_rplr_based(), initial_fec_enabled);
263 break;
minyuea1d9ad02016-10-02 14:53:37 -0700264 case audio_network_adaptor::config::Controller::kFrameLengthController:
265 controller = CreateFrameLengthController(
266 controller_config.frame_length_controller(),
michaelt6f08d7d2017-02-22 07:35:05 -0800267 encoder_frame_lengths_ms, initial_frame_length_ms,
268 min_encoder_bitrate_bps);
minyuea1d9ad02016-10-02 14:53:37 -0700269 break;
270 case audio_network_adaptor::config::Controller::kChannelController:
271 controller = CreateChannelController(
272 controller_config.channel_controller(), num_encoder_channels,
273 intial_channels_to_encode);
274 break;
275 case audio_network_adaptor::config::Controller::kDtxController:
276 controller = CreateDtxController(controller_config.dtx_controller(),
277 initial_dtx_enabled);
278 break;
279 case audio_network_adaptor::config::Controller::kBitrateController:
ivoc7e9c6142017-09-28 01:11:16 -0700280 controller = CreateBitrateController(
281 controller_config.bitrate_controller(), initial_bitrate_bps,
282 initial_frame_length_ms);
minyuea1d9ad02016-10-02 14:53:37 -0700283 break;
284 default:
285 RTC_NOTREACHED();
286 }
287 if (controller_config.has_scoring_point()) {
minyue678d2ee2017-05-30 07:36:20 -0700288 auto& scoring_point = controller_config.scoring_point();
289 RTC_CHECK(scoring_point.has_uplink_bandwidth_bps());
290 RTC_CHECK(scoring_point.has_uplink_packet_loss_fraction());
291 scoring_points[controller.get()] = std::make_pair<int, float>(
292 scoring_point.uplink_bandwidth_bps(),
293 scoring_point.uplink_packet_loss_fraction());
minyuea1d9ad02016-10-02 14:53:37 -0700294 }
295 controllers.push_back(std::move(controller));
296 }
297
minyue12de0772017-05-30 08:56:08 -0700298 if (scoring_points.size() == 0) {
299 return std::unique_ptr<ControllerManagerImpl>(new ControllerManagerImpl(
300 ControllerManagerImpl::Config(0, 0), std::move(controllers),
301 scoring_points));
302 } else {
303 RTC_CHECK(controller_manager_config.has_min_reordering_time_ms());
304 RTC_CHECK(controller_manager_config.has_min_reordering_squared_distance());
305 return std::unique_ptr<ControllerManagerImpl>(new ControllerManagerImpl(
306 ControllerManagerImpl::Config(
307 controller_manager_config.min_reordering_time_ms(),
308 controller_manager_config.min_reordering_squared_distance()),
309 std::move(controllers), scoring_points));
310 }
311
minyuea1d9ad02016-10-02 14:53:37 -0700312#else
313 RTC_NOTREACHED();
314 return nullptr;
minyue-webrtc7ed35f42017-06-13 11:49:29 +0200315#endif // WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -0700316}
317
minyuecaa9cb22016-09-13 13:34:15 -0700318ControllerManagerImpl::ControllerManagerImpl(const Config& config)
minyuebc77ed72016-09-22 00:45:16 -0700319 : ControllerManagerImpl(
320 config,
321 std::vector<std::unique_ptr<Controller>>(),
322 std::map<const Controller*, std::pair<int, float>>()) {}
minyuecaa9cb22016-09-13 13:34:15 -0700323
324ControllerManagerImpl::ControllerManagerImpl(
325 const Config& config,
minyue-webrtc5d689102017-08-14 14:33:32 +0200326 std::vector<std::unique_ptr<Controller>> controllers,
minyue678d2ee2017-05-30 07:36:20 -0700327 const std::map<const Controller*, std::pair<int, float>>& scoring_points)
minyuebc77ed72016-09-22 00:45:16 -0700328 : config_(config),
329 controllers_(std::move(controllers)),
Danil Chapovalovb6021232018-06-19 13:26:36 +0200330 last_reordering_time_ms_(absl::nullopt),
minyuebc77ed72016-09-22 00:45:16 -0700331 last_scoring_point_(0, 0.0) {
332 for (auto& controller : controllers_)
minyuecaa9cb22016-09-13 13:34:15 -0700333 default_sorted_controllers_.push_back(controller.get());
minyuebc77ed72016-09-22 00:45:16 -0700334 sorted_controllers_ = default_sorted_controllers_;
minyue678d2ee2017-05-30 07:36:20 -0700335 for (auto& controller_point : scoring_points) {
minyuebc77ed72016-09-22 00:45:16 -0700336 controller_scoring_points_.insert(std::make_pair(
337 controller_point.first, ScoringPoint(controller_point.second.first,
338 controller_point.second.second)));
minyuecaa9cb22016-09-13 13:34:15 -0700339 }
340}
341
342ControllerManagerImpl::~ControllerManagerImpl() = default;
343
344std::vector<Controller*> ControllerManagerImpl::GetSortedControllers(
345 const Controller::NetworkMetrics& metrics) {
minyue12de0772017-05-30 08:56:08 -0700346 if (controller_scoring_points_.size() == 0)
347 return default_sorted_controllers_;
minyuebc77ed72016-09-22 00:45:16 -0700348
349 if (!metrics.uplink_bandwidth_bps || !metrics.uplink_packet_loss_fraction)
350 return sorted_controllers_;
351
minyue12de0772017-05-30 08:56:08 -0700352 const int64_t now_ms = rtc::TimeMillis();
minyuebc77ed72016-09-22 00:45:16 -0700353 if (last_reordering_time_ms_ &&
354 now_ms - *last_reordering_time_ms_ < config_.min_reordering_time_ms)
355 return sorted_controllers_;
356
357 ScoringPoint scoring_point(*metrics.uplink_bandwidth_bps,
358 *metrics.uplink_packet_loss_fraction);
359
360 if (last_reordering_time_ms_ &&
361 last_scoring_point_.SquaredDistanceTo(scoring_point) <
362 config_.min_reordering_squared_distance)
363 return sorted_controllers_;
364
365 // Sort controllers according to the distances of |scoring_point| to the
minyue678d2ee2017-05-30 07:36:20 -0700366 // scoring points of controllers.
minyuebc77ed72016-09-22 00:45:16 -0700367 //
368 // A controller that does not associate with any scoring point
369 // are treated as if
370 // 1) they are less important than any controller that has a scoring point,
371 // 2) they are equally important to any controller that has no scoring point,
372 // and their relative order will follow |default_sorted_controllers_|.
373 std::vector<Controller*> sorted_controllers(default_sorted_controllers_);
374 std::stable_sort(
375 sorted_controllers.begin(), sorted_controllers.end(),
376 [this, &scoring_point](const Controller* lhs, const Controller* rhs) {
377 auto lhs_scoring_point = controller_scoring_points_.find(lhs);
378 auto rhs_scoring_point = controller_scoring_points_.find(rhs);
379
380 if (lhs_scoring_point == controller_scoring_points_.end())
381 return false;
382
383 if (rhs_scoring_point == controller_scoring_points_.end())
384 return true;
385
386 return lhs_scoring_point->second.SquaredDistanceTo(scoring_point) <
387 rhs_scoring_point->second.SquaredDistanceTo(scoring_point);
388 });
389
390 if (sorted_controllers_ != sorted_controllers) {
391 sorted_controllers_ = sorted_controllers;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100392 last_reordering_time_ms_ = now_ms;
minyuebc77ed72016-09-22 00:45:16 -0700393 last_scoring_point_ = scoring_point;
394 }
395 return sorted_controllers_;
minyuecaa9cb22016-09-13 13:34:15 -0700396}
397
398std::vector<Controller*> ControllerManagerImpl::GetControllers() const {
399 return default_sorted_controllers_;
400}
401
minyuebc77ed72016-09-22 00:45:16 -0700402ControllerManagerImpl::ScoringPoint::ScoringPoint(
403 int uplink_bandwidth_bps,
404 float uplink_packet_loss_fraction)
405 : uplink_bandwidth_bps(uplink_bandwidth_bps),
406 uplink_packet_loss_fraction(uplink_packet_loss_fraction) {}
407
408namespace {
409
410constexpr int kMinUplinkBandwidthBps = 0;
411constexpr int kMaxUplinkBandwidthBps = 120000;
412
413float NormalizeUplinkBandwidth(int uplink_bandwidth_bps) {
414 uplink_bandwidth_bps =
415 std::min(kMaxUplinkBandwidthBps,
416 std::max(kMinUplinkBandwidthBps, uplink_bandwidth_bps));
417 return static_cast<float>(uplink_bandwidth_bps - kMinUplinkBandwidthBps) /
418 (kMaxUplinkBandwidthBps - kMinUplinkBandwidthBps);
419}
420
421float NormalizePacketLossFraction(float uplink_packet_loss_fraction) {
422 // |uplink_packet_loss_fraction| is seldom larger than 0.3, so we scale it up
423 // by 3.3333f.
424 return std::min(uplink_packet_loss_fraction * 3.3333f, 1.0f);
425}
426
427} // namespace
428
429float ControllerManagerImpl::ScoringPoint::SquaredDistanceTo(
430 const ScoringPoint& scoring_point) const {
431 float diff_normalized_bitrate_bps =
432 NormalizeUplinkBandwidth(scoring_point.uplink_bandwidth_bps) -
433 NormalizeUplinkBandwidth(uplink_bandwidth_bps);
434 float diff_normalized_packet_loss =
435 NormalizePacketLossFraction(scoring_point.uplink_packet_loss_fraction) -
436 NormalizePacketLossFraction(uplink_packet_loss_fraction);
437 return std::pow(diff_normalized_bitrate_bps, 2) +
438 std::pow(diff_normalized_packet_loss, 2);
439}
440
minyuecaa9cb22016-09-13 13:34:15 -0700441} // namespace webrtc