OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
| 11 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h
" |
| 12 |
| 13 #include <cmath> |
11 #include <utility> | 14 #include <utility> |
12 | 15 |
13 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h
" | 16 #include "webrtc/system_wrappers/include/clock.h" |
14 | 17 |
15 namespace webrtc { | 18 namespace webrtc { |
16 | 19 |
17 ControllerManagerImpl::Config::Config() = default; | 20 ControllerManagerImpl::Config::Config(int min_reordering_time_ms, |
| 21 float min_reordering_squared_distance, |
| 22 const Clock* clock) |
| 23 : min_reordering_time_ms(min_reordering_time_ms), |
| 24 min_reordering_squared_distance(min_reordering_squared_distance), |
| 25 clock(clock) {} |
18 | 26 |
19 ControllerManagerImpl::Config::~Config() = default; | 27 ControllerManagerImpl::Config::~Config() = default; |
20 | 28 |
21 ControllerManagerImpl::ControllerManagerImpl(const Config& config) | 29 ControllerManagerImpl::ControllerManagerImpl(const Config& config) |
22 : config_(config) {} | 30 : ControllerManagerImpl( |
| 31 config, |
| 32 std::vector<std::unique_ptr<Controller>>(), |
| 33 std::map<const Controller*, std::pair<int, float>>()) {} |
23 | 34 |
24 ControllerManagerImpl::ControllerManagerImpl( | 35 ControllerManagerImpl::ControllerManagerImpl( |
25 const Config& config, | 36 const Config& config, |
26 std::vector<std::unique_ptr<Controller>> controllers) | 37 std::vector<std::unique_ptr<Controller>>&& controllers, |
27 : config_(config), controllers_(std::move(controllers)) { | 38 const std::map<const Controller*, std::pair<int, float>>& |
28 for (auto& controller : controllers_) { | 39 chracteristic_points) |
| 40 : config_(config), |
| 41 controllers_(std::move(controllers)), |
| 42 last_reordering_time_ms_(rtc::Optional<int64_t>()), |
| 43 last_scoring_point_(0, 0.0) { |
| 44 for (auto& controller : controllers_) |
29 default_sorted_controllers_.push_back(controller.get()); | 45 default_sorted_controllers_.push_back(controller.get()); |
| 46 sorted_controllers_ = default_sorted_controllers_; |
| 47 for (auto& controller_point : chracteristic_points) { |
| 48 controller_scoring_points_.insert(std::make_pair( |
| 49 controller_point.first, ScoringPoint(controller_point.second.first, |
| 50 controller_point.second.second))); |
30 } | 51 } |
31 } | 52 } |
32 | 53 |
33 ControllerManagerImpl::~ControllerManagerImpl() = default; | 54 ControllerManagerImpl::~ControllerManagerImpl() = default; |
34 | 55 |
35 std::vector<Controller*> ControllerManagerImpl::GetSortedControllers( | 56 std::vector<Controller*> ControllerManagerImpl::GetSortedControllers( |
36 const Controller::NetworkMetrics& metrics) { | 57 const Controller::NetworkMetrics& metrics) { |
37 // TODO(minyue): Reorder controllers according to their significance. | 58 int64_t now_ms = config_.clock->TimeInMilliseconds(); |
38 return default_sorted_controllers_; | 59 |
| 60 if (!metrics.uplink_bandwidth_bps || !metrics.uplink_packet_loss_fraction) |
| 61 return sorted_controllers_; |
| 62 |
| 63 if (last_reordering_time_ms_ && |
| 64 now_ms - *last_reordering_time_ms_ < config_.min_reordering_time_ms) |
| 65 return sorted_controllers_; |
| 66 |
| 67 ScoringPoint scoring_point(*metrics.uplink_bandwidth_bps, |
| 68 *metrics.uplink_packet_loss_fraction); |
| 69 |
| 70 if (last_reordering_time_ms_ && |
| 71 last_scoring_point_.SquaredDistanceTo(scoring_point) < |
| 72 config_.min_reordering_squared_distance) |
| 73 return sorted_controllers_; |
| 74 |
| 75 // Sort controllers according to the distances of |scoring_point| to the |
| 76 // characteristic scoring points of controllers. |
| 77 // |
| 78 // A controller that does not associate with any scoring point |
| 79 // are treated as if |
| 80 // 1) they are less important than any controller that has a scoring point, |
| 81 // 2) they are equally important to any controller that has no scoring point, |
| 82 // and their relative order will follow |default_sorted_controllers_|. |
| 83 std::vector<Controller*> sorted_controllers(default_sorted_controllers_); |
| 84 std::stable_sort( |
| 85 sorted_controllers.begin(), sorted_controllers.end(), |
| 86 [this, &scoring_point](const Controller* lhs, const Controller* rhs) { |
| 87 auto lhs_scoring_point = controller_scoring_points_.find(lhs); |
| 88 auto rhs_scoring_point = controller_scoring_points_.find(rhs); |
| 89 |
| 90 if (lhs_scoring_point == controller_scoring_points_.end()) |
| 91 return false; |
| 92 |
| 93 if (rhs_scoring_point == controller_scoring_points_.end()) |
| 94 return true; |
| 95 |
| 96 return lhs_scoring_point->second.SquaredDistanceTo(scoring_point) < |
| 97 rhs_scoring_point->second.SquaredDistanceTo(scoring_point); |
| 98 }); |
| 99 |
| 100 if (sorted_controllers_ != sorted_controllers) { |
| 101 sorted_controllers_ = sorted_controllers; |
| 102 last_reordering_time_ms_ = rtc::Optional<int64_t>(now_ms); |
| 103 last_scoring_point_ = scoring_point; |
| 104 } |
| 105 return sorted_controllers_; |
39 } | 106 } |
40 | 107 |
41 std::vector<Controller*> ControllerManagerImpl::GetControllers() const { | 108 std::vector<Controller*> ControllerManagerImpl::GetControllers() const { |
42 return default_sorted_controllers_; | 109 return default_sorted_controllers_; |
43 } | 110 } |
44 | 111 |
| 112 ControllerManagerImpl::ScoringPoint::ScoringPoint( |
| 113 int uplink_bandwidth_bps, |
| 114 float uplink_packet_loss_fraction) |
| 115 : uplink_bandwidth_bps(uplink_bandwidth_bps), |
| 116 uplink_packet_loss_fraction(uplink_packet_loss_fraction) {} |
| 117 |
| 118 namespace { |
| 119 |
| 120 constexpr int kMinUplinkBandwidthBps = 0; |
| 121 constexpr int kMaxUplinkBandwidthBps = 120000; |
| 122 |
| 123 float NormalizeUplinkBandwidth(int uplink_bandwidth_bps) { |
| 124 uplink_bandwidth_bps = |
| 125 std::min(kMaxUplinkBandwidthBps, |
| 126 std::max(kMinUplinkBandwidthBps, uplink_bandwidth_bps)); |
| 127 return static_cast<float>(uplink_bandwidth_bps - kMinUplinkBandwidthBps) / |
| 128 (kMaxUplinkBandwidthBps - kMinUplinkBandwidthBps); |
| 129 } |
| 130 |
| 131 float NormalizePacketLossFraction(float uplink_packet_loss_fraction) { |
| 132 // |uplink_packet_loss_fraction| is seldom larger than 0.3, so we scale it up |
| 133 // by 3.3333f. |
| 134 return std::min(uplink_packet_loss_fraction * 3.3333f, 1.0f); |
| 135 } |
| 136 |
| 137 } // namespace |
| 138 |
| 139 float ControllerManagerImpl::ScoringPoint::SquaredDistanceTo( |
| 140 const ScoringPoint& scoring_point) const { |
| 141 float diff_normalized_bitrate_bps = |
| 142 NormalizeUplinkBandwidth(scoring_point.uplink_bandwidth_bps) - |
| 143 NormalizeUplinkBandwidth(uplink_bandwidth_bps); |
| 144 float diff_normalized_packet_loss = |
| 145 NormalizePacketLossFraction(scoring_point.uplink_packet_loss_fraction) - |
| 146 NormalizePacketLossFraction(uplink_packet_loss_fraction); |
| 147 return std::pow(diff_normalized_bitrate_bps, 2) + |
| 148 std::pow(diff_normalized_packet_loss, 2); |
| 149 } |
| 150 |
45 } // namespace webrtc | 151 } // namespace webrtc |
OLD | NEW |