Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.cc

Issue 2349113002: Adding reordering logic in audio network adaptor. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "
14
15 namespace webrtc { 16 namespace webrtc {
16 17
17 ControllerManagerImpl::Config::Config() = default; 18 ControllerManagerImpl::Config::Config(int min_reordering_time_ms,
19 float min_reordering_squared_distance,
20 Clock* clock)
21 : min_reordering_time_ms(min_reordering_time_ms),
22 min_reordering_squared_distance(min_reordering_squared_distance),
23 clock(clock) {}
18 24
19 ControllerManagerImpl::Config::~Config() = default; 25 ControllerManagerImpl::Config::~Config() = default;
20 26
21 ControllerManagerImpl::ControllerManagerImpl(const Config& config) 27 ControllerManagerImpl::ControllerManagerImpl(const Config& config)
22 : config_(config) {} 28 : ControllerManagerImpl(
29 config,
30 std::map<const Controller*, std::pair<int, float>>(),
31 std::vector<std::unique_ptr<Controller>>()) {}
23 32
24 ControllerManagerImpl::ControllerManagerImpl( 33 ControllerManagerImpl::ControllerManagerImpl(
25 const Config& config, 34 const Config& config,
35 const std::map<const Controller*, std::pair<int, float>>&
36 chracteristic_points,
26 std::vector<std::unique_ptr<Controller>> controllers) 37 std::vector<std::unique_ptr<Controller>> controllers)
27 : config_(config), controllers_(std::move(controllers)) { 38 : config_(config),
28 for (auto& controller : controllers_) { 39 controllers_(std::move(controllers)),
40 last_reordering_time_ms_(rtc::Optional<int64_t>()),
41 last_scoring_point_(0, 0.0) {
42 for (auto& controller : controllers_)
29 default_sorted_controllers_.push_back(controller.get()); 43 default_sorted_controllers_.push_back(controller.get());
44 sorted_controllers_ = default_sorted_controllers_;
45 for (auto& controller_point : chracteristic_points) {
46 controller_scoring_points_.insert(std::make_pair(
47 controller_point.first, ScoringPoint(controller_point.second.first,
48 controller_point.second.second)));
30 } 49 }
31 } 50 }
32 51
33 ControllerManagerImpl::~ControllerManagerImpl() = default; 52 ControllerManagerImpl::~ControllerManagerImpl() = default;
34 53
35 std::vector<Controller*> ControllerManagerImpl::GetSortedControllers( 54 std::vector<Controller*> ControllerManagerImpl::GetSortedControllers(
36 const Controller::NetworkMetrics& metrics) { 55 const Controller::NetworkMetrics& metrics) {
37 // TODO(minyue): Reorder controllers according to their significance. 56 int64_t now_ms = config_.clock->TimeInMilliseconds();
38 return default_sorted_controllers_; 57
58 if (!metrics.uplink_bandwidth_bps || !metrics.uplink_packet_loss_fraction)
59 return sorted_controllers_;
60
61 if (last_reordering_time_ms_ &&
62 now_ms - *last_reordering_time_ms_ < config_.min_reordering_time_ms)
63 return sorted_controllers_;
64
65 ScoringPoint scoring_point(*metrics.uplink_bandwidth_bps,
66 *metrics.uplink_packet_loss_fraction);
67
68 if (last_reordering_time_ms_ &&
michaelt 2016/09/19 08:22:45 should work even without "last_reordering_time_ms_
minyue-webrtc 2016/09/19 10:06:57 If last_reordering_time_ms_ is none, it will try t
michaelt 2016/09/22 06:57:16 Acknowledged.
69 last_scoring_point_.SquaredDistanceTo(scoring_point) <
70 config_.min_reordering_squared_distance)
71 return sorted_controllers_;
72
73 // Sort controllers according to the distances of |scoring_point| to the
74 // characteristic scoring points of controllers.
75 //
76 // A controller that does not associate with any scoring point
77 // are treated as if
78 // 1) they are less important than any controller that has a scoring point,
79 // 2) they are equally important to any controller that has no scoring point,
80 // and their relative order will follow |default_sorted_controllers_|.
81 std::vector<Controller*> sorted_controllers(default_sorted_controllers_);
82 std::stable_sort(
83 sorted_controllers.begin(), sorted_controllers.end(),
84 [this, &scoring_point](const Controller* lhs, const Controller* rhs) {
85 auto lhs_scoring_point = controller_scoring_points_.find(lhs);
86 auto rhs_scoring_point = controller_scoring_points_.find(rhs);
87
88 if (lhs_scoring_point != controller_scoring_points_.end() &&
hlundin-webrtc 2016/09/20 08:44:00 You can swap the order of these two "end-cases":
minyue-webrtc 2016/09/21 07:42:58 Done.
89 rhs_scoring_point == controller_scoring_points_.end())
90 return true;
91
92 if (lhs_scoring_point == controller_scoring_points_.end())
93 return false;
94
95 return lhs_scoring_point->second.SquaredDistanceTo(scoring_point) <
96 rhs_scoring_point->second.SquaredDistanceTo(scoring_point);
97 });
98
99 if (sorted_controllers_ != sorted_controllers) {
100 sorted_controllers_ = sorted_controllers;
101 last_reordering_time_ms_ = rtc::Optional<int64_t>(now_ms);
102 last_scoring_point_ = scoring_point;
103 }
104 return sorted_controllers_;
39 } 105 }
40 106
41 std::vector<Controller*> ControllerManagerImpl::GetControllers() const { 107 std::vector<Controller*> ControllerManagerImpl::GetControllers() const {
42 return default_sorted_controllers_; 108 return default_sorted_controllers_;
43 } 109 }
44 110
111 ControllerManagerImpl::ScoringPoint::ScoringPoint(
112 int uplink_bandwidth_bps,
113 float uplink_packet_loss_fraction)
114 : uplink_bandwidth_bps(uplink_bandwidth_bps),
115 uplink_packet_loss_fraction(uplink_packet_loss_fraction) {}
116
117 namespace {
118
119 constexpr int kMinUplinkBandwidthBps = 0;
120 constexpr int kMaxUplinkBandwidthBps = 120000;
michaelt 2016/09/19 08:22:45 should we inject this by the config. Or if not we
minyue-webrtc 2016/09/19 10:06:57 I think the definition of distance can be hard cod
121
122 float NormalizeUplinkBandwidth(int uplink_Bandwidth_bps) {
hlundin-webrtc 2016/09/20 08:44:00 Small 'b' in the parameter name.
minyue-webrtc 2016/09/21 07:42:58 oh, yes, my bad.
123 uplink_Bandwidth_bps =
124 std::min(kMaxUplinkBandwidthBps,
125 std::max(kMinUplinkBandwidthBps, uplink_Bandwidth_bps));
126 return static_cast<float>(uplink_Bandwidth_bps - kMinUplinkBandwidthBps) /
127 (kMaxUplinkBandwidthBps - kMinUplinkBandwidthBps);
128 }
129
130 float NormalizePacketLossFraction(float uplink_packet_loss_fraction) {
131 // |uplink_packet_loss_fraction| is seldom smaller than 0.3, so we scale it up
hlundin-webrtc 2016/09/20 08:44:00 "seldom smaller" -> "seldom larger"?
minyue-webrtc 2016/09/21 07:42:58 oh, my bad
132 // by 3.3333f.
133 return std::min(uplink_packet_loss_fraction * 3.3333f, 1.0f);
134 }
135
136 } // namespace
137
138 float ControllerManagerImpl::ScoringPoint::SquaredDistanceTo(
139 const ScoringPoint& scoring_point) const {
140 float diff_normalized_bitrate_bps =
141 NormalizeUplinkBandwidth(scoring_point.uplink_bandwidth_bps) -
142 NormalizeUplinkBandwidth(uplink_bandwidth_bps);
143 float diff_normalized_packet_loss =
144 NormalizePacketLossFraction(scoring_point.uplink_packet_loss_fraction) -
145 NormalizePacketLossFraction(uplink_packet_loss_fraction);
146 return std::pow(diff_normalized_bitrate_bps, 2) +
147 std::pow(diff_normalized_packet_loss, 2);
148 }
149
45 } // namespace webrtc 150 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698