blob: 4c0e61c6ad4f20b3734cffab2c1bca087a09304b [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"
22#include "modules/audio_coding/audio_network_adaptor/fec_controller_rplr_based.h"
23#include "modules/audio_coding/audio_network_adaptor/frame_length_controller.h"
24#include "modules/audio_coding/audio_network_adaptor/util/threshold_curve.h"
25#include "rtc_base/ignore_wundef.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
elad.alon28770482017-03-28 05:03:55 -070077std::unique_ptr<FecControllerRplrBased> CreateFecControllerRplrBased(
78 const audio_network_adaptor::config::FecControllerRplrBased& config,
79 bool initial_fec_enabled) {
80 RTC_CHECK(config.has_fec_enabling_threshold());
81 RTC_CHECK(config.has_fec_disabling_threshold());
82
83 auto& fec_enabling_threshold = config.fec_enabling_threshold();
84 RTC_CHECK(fec_enabling_threshold.has_low_bandwidth_bps());
85 RTC_CHECK(fec_enabling_threshold.has_low_bandwidth_recoverable_packet_loss());
86 RTC_CHECK(fec_enabling_threshold.has_high_bandwidth_bps());
87 RTC_CHECK(
88 fec_enabling_threshold.has_high_bandwidth_recoverable_packet_loss());
89
90 auto& fec_disabling_threshold = config.fec_disabling_threshold();
91 RTC_CHECK(fec_disabling_threshold.has_low_bandwidth_bps());
92 RTC_CHECK(
93 fec_disabling_threshold.has_low_bandwidth_recoverable_packet_loss());
94 RTC_CHECK(fec_disabling_threshold.has_high_bandwidth_bps());
95 RTC_CHECK(
96 fec_disabling_threshold.has_high_bandwidth_recoverable_packet_loss());
97
98 return std::unique_ptr<FecControllerRplrBased>(
99 new FecControllerRplrBased(FecControllerRplrBased::Config(
100 initial_fec_enabled,
elad.alon326263a2017-03-29 03:16:58 -0700101 ThresholdCurve(
elad.alon28770482017-03-28 05:03:55 -0700102 fec_enabling_threshold.low_bandwidth_bps(),
103 fec_enabling_threshold.low_bandwidth_recoverable_packet_loss(),
104 fec_enabling_threshold.high_bandwidth_bps(),
105 fec_enabling_threshold.high_bandwidth_recoverable_packet_loss()),
elad.alon326263a2017-03-29 03:16:58 -0700106 ThresholdCurve(
elad.alon28770482017-03-28 05:03:55 -0700107 fec_disabling_threshold.low_bandwidth_bps(),
108 fec_disabling_threshold.low_bandwidth_recoverable_packet_loss(),
109 fec_disabling_threshold.high_bandwidth_bps(),
110 fec_disabling_threshold
111 .high_bandwidth_recoverable_packet_loss()))));
112}
113
minyuea1d9ad02016-10-02 14:53:37 -0700114std::unique_ptr<FrameLengthController> CreateFrameLengthController(
115 const audio_network_adaptor::config::FrameLengthController& config,
116 rtc::ArrayView<const int> encoder_frame_lengths_ms,
michaelt6f08d7d2017-02-22 07:35:05 -0800117 int initial_frame_length_ms,
118 int min_encoder_bitrate_bps) {
minyuea1d9ad02016-10-02 14:53:37 -0700119 RTC_CHECK(config.has_fl_increasing_packet_loss_fraction());
120 RTC_CHECK(config.has_fl_decreasing_packet_loss_fraction());
121 RTC_CHECK(config.has_fl_20ms_to_60ms_bandwidth_bps());
122 RTC_CHECK(config.has_fl_60ms_to_20ms_bandwidth_bps());
123
michaelta55f0212017-02-02 07:47:19 -0800124 std::map<FrameLengthController::Config::FrameLengthChange, int>
125 fl_changing_bandwidths_bps = {
126 {FrameLengthController::Config::FrameLengthChange(20, 60),
127 config.fl_20ms_to_60ms_bandwidth_bps()},
128 {FrameLengthController::Config::FrameLengthChange(60, 20),
129 config.fl_60ms_to_20ms_bandwidth_bps()}};
130
131 if (config.has_fl_60ms_to_120ms_bandwidth_bps() &&
132 config.has_fl_120ms_to_60ms_bandwidth_bps()) {
133 fl_changing_bandwidths_bps.insert(std::make_pair(
134 FrameLengthController::Config::FrameLengthChange(60, 120),
135 config.fl_60ms_to_120ms_bandwidth_bps()));
136 fl_changing_bandwidths_bps.insert(std::make_pair(
137 FrameLengthController::Config::FrameLengthChange(120, 60),
138 config.fl_120ms_to_60ms_bandwidth_bps()));
139 }
140
ivoc7e9c6142017-09-28 01:11:16 -0700141 int fl_increase_overhead_offset = 0;
142 if (config.has_fl_increase_overhead_offset()) {
143 fl_increase_overhead_offset = config.fl_increase_overhead_offset();
144 }
145 int fl_decrease_overhead_offset = 0;
146 if (config.has_fl_decrease_overhead_offset()) {
147 fl_decrease_overhead_offset = config.fl_decrease_overhead_offset();
148 }
149
minyuea1d9ad02016-10-02 14:53:37 -0700150 FrameLengthController::Config ctor_config(
Minyue Li8319e7f2019-01-02 16:57:02 +0100151 std::set<int>(), initial_frame_length_ms, min_encoder_bitrate_bps,
minyuea1d9ad02016-10-02 14:53:37 -0700152 config.fl_increasing_packet_loss_fraction(),
ivoc7e9c6142017-09-28 01:11:16 -0700153 config.fl_decreasing_packet_loss_fraction(), fl_increase_overhead_offset,
154 fl_decrease_overhead_offset, std::move(fl_changing_bandwidths_bps));
minyuea1d9ad02016-10-02 14:53:37 -0700155
156 for (auto frame_length : encoder_frame_lengths_ms)
Minyue Li8319e7f2019-01-02 16:57:02 +0100157 ctor_config.encoder_frame_lengths_ms.insert(frame_length);
minyuea1d9ad02016-10-02 14:53:37 -0700158
159 return std::unique_ptr<FrameLengthController>(
160 new FrameLengthController(ctor_config));
161}
162
163std::unique_ptr<ChannelController> CreateChannelController(
164 const audio_network_adaptor::config::ChannelController& config,
165 size_t num_encoder_channels,
166 size_t intial_channels_to_encode) {
167 RTC_CHECK(config.has_channel_1_to_2_bandwidth_bps());
168 RTC_CHECK(config.has_channel_2_to_1_bandwidth_bps());
169
170 return std::unique_ptr<ChannelController>(new ChannelController(
171 ChannelController::Config(num_encoder_channels, intial_channels_to_encode,
172 config.channel_1_to_2_bandwidth_bps(),
173 config.channel_2_to_1_bandwidth_bps())));
174}
175
176std::unique_ptr<DtxController> CreateDtxController(
177 const audio_network_adaptor::config::DtxController& dtx_config,
178 bool initial_dtx_enabled) {
179 RTC_CHECK(dtx_config.has_dtx_enabling_bandwidth_bps());
180 RTC_CHECK(dtx_config.has_dtx_disabling_bandwidth_bps());
181
182 return std::unique_ptr<DtxController>(new DtxController(DtxController::Config(
183 initial_dtx_enabled, dtx_config.dtx_enabling_bandwidth_bps(),
184 dtx_config.dtx_disabling_bandwidth_bps())));
185}
186
187using audio_network_adaptor::BitrateController;
188std::unique_ptr<BitrateController> CreateBitrateController(
ivoc7e9c6142017-09-28 01:11:16 -0700189 const audio_network_adaptor::config::BitrateController& bitrate_config,
minyuea1d9ad02016-10-02 14:53:37 -0700190 int initial_bitrate_bps,
191 int initial_frame_length_ms) {
ivoc7e9c6142017-09-28 01:11:16 -0700192 int fl_increase_overhead_offset = 0;
193 if (bitrate_config.has_fl_increase_overhead_offset()) {
194 fl_increase_overhead_offset = bitrate_config.fl_increase_overhead_offset();
195 }
196 int fl_decrease_overhead_offset = 0;
197 if (bitrate_config.has_fl_decrease_overhead_offset()) {
198 fl_decrease_overhead_offset = bitrate_config.fl_decrease_overhead_offset();
199 }
200 return std::unique_ptr<BitrateController>(
201 new BitrateController(BitrateController::Config(
202 initial_bitrate_bps, initial_frame_length_ms,
203 fl_increase_overhead_offset, fl_decrease_overhead_offset)));
minyuea1d9ad02016-10-02 14:53:37 -0700204}
minyue-webrtc7ed35f42017-06-13 11:49:29 +0200205#endif // WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -0700206
207} // namespace
208
minyuebc77ed72016-09-22 00:45:16 -0700209ControllerManagerImpl::Config::Config(int min_reordering_time_ms,
michaelt92aef172017-04-18 00:11:48 -0700210 float min_reordering_squared_distance)
minyuebc77ed72016-09-22 00:45:16 -0700211 : min_reordering_time_ms(min_reordering_time_ms),
michaelt92aef172017-04-18 00:11:48 -0700212 min_reordering_squared_distance(min_reordering_squared_distance) {}
minyuecaa9cb22016-09-13 13:34:15 -0700213
214ControllerManagerImpl::Config::~Config() = default;
215
minyuea1d9ad02016-10-02 14:53:37 -0700216std::unique_ptr<ControllerManager> ControllerManagerImpl::Create(
Mirko Bonadeie45c6882019-02-16 09:59:29 +0100217 const std::string& config_string,
minyuea1d9ad02016-10-02 14:53:37 -0700218 size_t num_encoder_channels,
219 rtc::ArrayView<const int> encoder_frame_lengths_ms,
michaelt6f08d7d2017-02-22 07:35:05 -0800220 int min_encoder_bitrate_bps,
minyuea1d9ad02016-10-02 14:53:37 -0700221 size_t intial_channels_to_encode,
222 int initial_frame_length_ms,
223 int initial_bitrate_bps,
224 bool initial_fec_enabled,
michaelt92aef172017-04-18 00:11:48 -0700225 bool initial_dtx_enabled) {
minyue-webrtc9c6430f2017-07-10 16:29:17 +0200226 return Create(config_string, num_encoder_channels, encoder_frame_lengths_ms,
227 min_encoder_bitrate_bps, intial_channels_to_encode,
228 initial_frame_length_ms, initial_bitrate_bps,
229 initial_fec_enabled, initial_dtx_enabled, nullptr);
230}
231
232std::unique_ptr<ControllerManager> ControllerManagerImpl::Create(
Mirko Bonadeie45c6882019-02-16 09:59:29 +0100233 const std::string& config_string,
minyue-webrtc9c6430f2017-07-10 16:29:17 +0200234 size_t num_encoder_channels,
235 rtc::ArrayView<const int> encoder_frame_lengths_ms,
236 int min_encoder_bitrate_bps,
237 size_t intial_channels_to_encode,
238 int initial_frame_length_ms,
239 int initial_bitrate_bps,
240 bool initial_fec_enabled,
241 bool initial_dtx_enabled,
242 DebugDumpWriter* debug_dump_writer) {
minyue-webrtc7ed35f42017-06-13 11:49:29 +0200243#if WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -0700244 audio_network_adaptor::config::ControllerManager controller_manager_config;
minyue-webrtc9c6430f2017-07-10 16:29:17 +0200245 RTC_CHECK(controller_manager_config.ParseFromString(config_string));
246 if (debug_dump_writer)
247 debug_dump_writer->DumpControllerManagerConfig(controller_manager_config,
248 rtc::TimeMillis());
minyuea1d9ad02016-10-02 14:53:37 -0700249
250 std::vector<std::unique_ptr<Controller>> controllers;
minyue678d2ee2017-05-30 07:36:20 -0700251 std::map<const Controller*, std::pair<int, float>> scoring_points;
minyuea1d9ad02016-10-02 14:53:37 -0700252
253 for (int i = 0; i < controller_manager_config.controllers_size(); ++i) {
254 auto& controller_config = controller_manager_config.controllers(i);
255 std::unique_ptr<Controller> controller;
256 switch (controller_config.controller_case()) {
257 case audio_network_adaptor::config::Controller::kFecController:
elad.alon6d7900d2017-03-24 04:12:56 -0700258 controller = CreateFecControllerPlrBased(
michaelt92aef172017-04-18 00:11:48 -0700259 controller_config.fec_controller(), initial_fec_enabled);
minyuea1d9ad02016-10-02 14:53:37 -0700260 break;
elad.alon28770482017-03-28 05:03:55 -0700261 case audio_network_adaptor::config::Controller::kFecControllerRplrBased:
262 controller = CreateFecControllerRplrBased(
263 controller_config.fec_controller_rplr_based(), initial_fec_enabled);
264 break;
minyuea1d9ad02016-10-02 14:53:37 -0700265 case audio_network_adaptor::config::Controller::kFrameLengthController:
266 controller = CreateFrameLengthController(
267 controller_config.frame_length_controller(),
michaelt6f08d7d2017-02-22 07:35:05 -0800268 encoder_frame_lengths_ms, initial_frame_length_ms,
269 min_encoder_bitrate_bps);
minyuea1d9ad02016-10-02 14:53:37 -0700270 break;
271 case audio_network_adaptor::config::Controller::kChannelController:
272 controller = CreateChannelController(
273 controller_config.channel_controller(), num_encoder_channels,
274 intial_channels_to_encode);
275 break;
276 case audio_network_adaptor::config::Controller::kDtxController:
277 controller = CreateDtxController(controller_config.dtx_controller(),
278 initial_dtx_enabled);
279 break;
280 case audio_network_adaptor::config::Controller::kBitrateController:
ivoc7e9c6142017-09-28 01:11:16 -0700281 controller = CreateBitrateController(
282 controller_config.bitrate_controller(), initial_bitrate_bps,
283 initial_frame_length_ms);
minyuea1d9ad02016-10-02 14:53:37 -0700284 break;
285 default:
286 RTC_NOTREACHED();
287 }
288 if (controller_config.has_scoring_point()) {
minyue678d2ee2017-05-30 07:36:20 -0700289 auto& scoring_point = controller_config.scoring_point();
290 RTC_CHECK(scoring_point.has_uplink_bandwidth_bps());
291 RTC_CHECK(scoring_point.has_uplink_packet_loss_fraction());
292 scoring_points[controller.get()] = std::make_pair<int, float>(
293 scoring_point.uplink_bandwidth_bps(),
294 scoring_point.uplink_packet_loss_fraction());
minyuea1d9ad02016-10-02 14:53:37 -0700295 }
296 controllers.push_back(std::move(controller));
297 }
298
minyue12de0772017-05-30 08:56:08 -0700299 if (scoring_points.size() == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200300 return std::unique_ptr<ControllerManagerImpl>(
301 new ControllerManagerImpl(ControllerManagerImpl::Config(0, 0),
302 std::move(controllers), scoring_points));
minyue12de0772017-05-30 08:56:08 -0700303 } else {
304 RTC_CHECK(controller_manager_config.has_min_reordering_time_ms());
305 RTC_CHECK(controller_manager_config.has_min_reordering_squared_distance());
306 return std::unique_ptr<ControllerManagerImpl>(new ControllerManagerImpl(
307 ControllerManagerImpl::Config(
308 controller_manager_config.min_reordering_time_ms(),
309 controller_manager_config.min_reordering_squared_distance()),
310 std::move(controllers), scoring_points));
311 }
312
minyuea1d9ad02016-10-02 14:53:37 -0700313#else
314 RTC_NOTREACHED();
315 return nullptr;
minyue-webrtc7ed35f42017-06-13 11:49:29 +0200316#endif // WEBRTC_ENABLE_PROTOBUF
minyuea1d9ad02016-10-02 14:53:37 -0700317}
318
minyuecaa9cb22016-09-13 13:34:15 -0700319ControllerManagerImpl::ControllerManagerImpl(const Config& config)
minyuebc77ed72016-09-22 00:45:16 -0700320 : ControllerManagerImpl(
321 config,
322 std::vector<std::unique_ptr<Controller>>(),
323 std::map<const Controller*, std::pair<int, float>>()) {}
minyuecaa9cb22016-09-13 13:34:15 -0700324
325ControllerManagerImpl::ControllerManagerImpl(
326 const Config& config,
minyue-webrtc5d689102017-08-14 14:33:32 +0200327 std::vector<std::unique_ptr<Controller>> controllers,
minyue678d2ee2017-05-30 07:36:20 -0700328 const std::map<const Controller*, std::pair<int, float>>& scoring_points)
minyuebc77ed72016-09-22 00:45:16 -0700329 : config_(config),
330 controllers_(std::move(controllers)),
Danil Chapovalovb6021232018-06-19 13:26:36 +0200331 last_reordering_time_ms_(absl::nullopt),
minyuebc77ed72016-09-22 00:45:16 -0700332 last_scoring_point_(0, 0.0) {
333 for (auto& controller : controllers_)
minyuecaa9cb22016-09-13 13:34:15 -0700334 default_sorted_controllers_.push_back(controller.get());
minyuebc77ed72016-09-22 00:45:16 -0700335 sorted_controllers_ = default_sorted_controllers_;
minyue678d2ee2017-05-30 07:36:20 -0700336 for (auto& controller_point : scoring_points) {
minyuebc77ed72016-09-22 00:45:16 -0700337 controller_scoring_points_.insert(std::make_pair(
338 controller_point.first, ScoringPoint(controller_point.second.first,
339 controller_point.second.second)));
minyuecaa9cb22016-09-13 13:34:15 -0700340 }
341}
342
343ControllerManagerImpl::~ControllerManagerImpl() = default;
344
345std::vector<Controller*> ControllerManagerImpl::GetSortedControllers(
346 const Controller::NetworkMetrics& metrics) {
minyue12de0772017-05-30 08:56:08 -0700347 if (controller_scoring_points_.size() == 0)
348 return default_sorted_controllers_;
minyuebc77ed72016-09-22 00:45:16 -0700349
350 if (!metrics.uplink_bandwidth_bps || !metrics.uplink_packet_loss_fraction)
351 return sorted_controllers_;
352
minyue12de0772017-05-30 08:56:08 -0700353 const int64_t now_ms = rtc::TimeMillis();
minyuebc77ed72016-09-22 00:45:16 -0700354 if (last_reordering_time_ms_ &&
355 now_ms - *last_reordering_time_ms_ < config_.min_reordering_time_ms)
356 return sorted_controllers_;
357
358 ScoringPoint scoring_point(*metrics.uplink_bandwidth_bps,
359 *metrics.uplink_packet_loss_fraction);
360
361 if (last_reordering_time_ms_ &&
362 last_scoring_point_.SquaredDistanceTo(scoring_point) <
363 config_.min_reordering_squared_distance)
364 return sorted_controllers_;
365
366 // Sort controllers according to the distances of |scoring_point| to the
minyue678d2ee2017-05-30 07:36:20 -0700367 // scoring points of controllers.
minyuebc77ed72016-09-22 00:45:16 -0700368 //
369 // A controller that does not associate with any scoring point
370 // are treated as if
371 // 1) they are less important than any controller that has a scoring point,
372 // 2) they are equally important to any controller that has no scoring point,
373 // and their relative order will follow |default_sorted_controllers_|.
374 std::vector<Controller*> sorted_controllers(default_sorted_controllers_);
375 std::stable_sort(
376 sorted_controllers.begin(), sorted_controllers.end(),
377 [this, &scoring_point](const Controller* lhs, const Controller* rhs) {
378 auto lhs_scoring_point = controller_scoring_points_.find(lhs);
379 auto rhs_scoring_point = controller_scoring_points_.find(rhs);
380
381 if (lhs_scoring_point == controller_scoring_points_.end())
382 return false;
383
384 if (rhs_scoring_point == controller_scoring_points_.end())
385 return true;
386
387 return lhs_scoring_point->second.SquaredDistanceTo(scoring_point) <
388 rhs_scoring_point->second.SquaredDistanceTo(scoring_point);
389 });
390
391 if (sorted_controllers_ != sorted_controllers) {
392 sorted_controllers_ = sorted_controllers;
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100393 last_reordering_time_ms_ = now_ms;
minyuebc77ed72016-09-22 00:45:16 -0700394 last_scoring_point_ = scoring_point;
395 }
396 return sorted_controllers_;
minyuecaa9cb22016-09-13 13:34:15 -0700397}
398
399std::vector<Controller*> ControllerManagerImpl::GetControllers() const {
400 return default_sorted_controllers_;
401}
402
minyuebc77ed72016-09-22 00:45:16 -0700403ControllerManagerImpl::ScoringPoint::ScoringPoint(
404 int uplink_bandwidth_bps,
405 float uplink_packet_loss_fraction)
406 : uplink_bandwidth_bps(uplink_bandwidth_bps),
407 uplink_packet_loss_fraction(uplink_packet_loss_fraction) {}
408
409namespace {
410
411constexpr int kMinUplinkBandwidthBps = 0;
412constexpr int kMaxUplinkBandwidthBps = 120000;
413
414float NormalizeUplinkBandwidth(int uplink_bandwidth_bps) {
415 uplink_bandwidth_bps =
416 std::min(kMaxUplinkBandwidthBps,
417 std::max(kMinUplinkBandwidthBps, uplink_bandwidth_bps));
418 return static_cast<float>(uplink_bandwidth_bps - kMinUplinkBandwidthBps) /
419 (kMaxUplinkBandwidthBps - kMinUplinkBandwidthBps);
420}
421
422float NormalizePacketLossFraction(float uplink_packet_loss_fraction) {
423 // |uplink_packet_loss_fraction| is seldom larger than 0.3, so we scale it up
424 // by 3.3333f.
425 return std::min(uplink_packet_loss_fraction * 3.3333f, 1.0f);
426}
427
428} // namespace
429
430float ControllerManagerImpl::ScoringPoint::SquaredDistanceTo(
431 const ScoringPoint& scoring_point) const {
432 float diff_normalized_bitrate_bps =
433 NormalizeUplinkBandwidth(scoring_point.uplink_bandwidth_bps) -
434 NormalizeUplinkBandwidth(uplink_bandwidth_bps);
435 float diff_normalized_packet_loss =
436 NormalizePacketLossFraction(scoring_point.uplink_packet_loss_fraction) -
437 NormalizePacketLossFraction(uplink_packet_loss_fraction);
438 return std::pow(diff_normalized_bitrate_bps, 2) +
439 std::pow(diff_normalized_packet_loss, 2);
440}
441
minyuecaa9cb22016-09-13 13:34:15 -0700442} // namespace webrtc