OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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/remote_bitrate_estimator/overuse_detector.h" | 11 #include "webrtc/modules/remote_bitrate_estimator/overuse_detector.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <sstream> | |
14 #include <math.h> | 15 #include <math.h> |
15 #include <stdlib.h> | 16 #include <stdlib.h> |
16 | 17 |
18 #include "webrtc/base/common.h" | |
17 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" | 19 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" |
18 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" | 20 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
19 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" | 21 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" |
22 #include "webrtc/system_wrappers/interface/field_trial.h" | |
20 #include "webrtc/system_wrappers/interface/trace.h" | 23 #include "webrtc/system_wrappers/interface/trace.h" |
21 | 24 |
22 namespace webrtc { | 25 namespace webrtc { |
23 | 26 |
24 enum { kOverUsingTimeThreshold = 100 }; | 27 enum { kOverUsingTimeThreshold = 10 }; |
28 const char* kAdaptiveThresholdExperiment = "WebRTC-AdaptiveBweThreshold"; | |
29 const char* kEnabledPrefix = "Enabled"; | |
30 const size_t kEnabledPrefixLength = 7; | |
31 const size_t kMinExperimentLength = | |
mflodman
2015/07/03 08:29:55
Can you comment this.
stefan-webrtc
2015/07/03 13:45:11
I changed it to be sizeof(kEnabledPrefix) - 1 inst
| |
32 kEnabledPrefixLength + 3; | |
33 | |
34 const double kMaxAdaptOffsetMs = 15.0; | |
25 | 35 |
26 OveruseDetector::OveruseDetector(const OverUseDetectorOptions& options) | 36 OveruseDetector::OveruseDetector(const OverUseDetectorOptions& options) |
27 : options_(options), | 37 : options_(options), |
28 threshold_(options_.initial_threshold), | 38 threshold_(12.5), |
mflodman
2015/07/03 08:29:56
Why 12.5 instead of 25?
Gaetano Carlucci
2015/07/03 10:54:28
This aims to speed up the convergence of the adapt
stefan-webrtc
2015/07/03 13:45:10
I think this makes a very little difference in pra
| |
39 last_update_ms_(-1), | |
29 prev_offset_(0.0), | 40 prev_offset_(0.0), |
30 time_over_using_(-1), | 41 time_over_using_(-1), |
31 overuse_counter_(0), | 42 overuse_counter_(0), |
32 hypothesis_(kBwNormal) {} | 43 hypothesis_(kBwNormal), |
44 k_up_(0.01), | |
45 k_down_(0.00018) { | |
46 double k_up = 0.0; | |
47 double k_down = 0.0; | |
48 if (GetExperimentThresholds(&k_up, &k_down)) { | |
49 k_up_ = k_up; | |
50 k_down_ = k_down; | |
51 } | |
52 } | |
33 | 53 |
34 OveruseDetector::~OveruseDetector() {} | 54 OveruseDetector::~OveruseDetector() {} |
35 | 55 |
36 BandwidthUsage OveruseDetector::State() const { | 56 BandwidthUsage OveruseDetector::State() const { |
37 return hypothesis_; | 57 return hypothesis_; |
38 } | 58 } |
39 | 59 |
40 | |
41 void OveruseDetector::SetRateControlRegion(RateControlRegion region) { | |
42 switch (region) { | |
43 case kRcMaxUnknown: { | |
44 threshold_ = options_.initial_threshold; | |
45 break; | |
46 } | |
47 case kRcAboveMax: | |
48 case kRcNearMax: { | |
49 threshold_ = options_.initial_threshold / 2; | |
50 break; | |
51 } | |
52 } | |
53 } | |
54 | |
55 BandwidthUsage OveruseDetector::Detect(double offset, | 60 BandwidthUsage OveruseDetector::Detect(double offset, |
56 double ts_delta, | 61 double ts_delta, |
57 int num_of_deltas, | 62 int num_of_deltas, |
58 int64_t now_ms) { | 63 int64_t now_ms) { |
59 if (num_of_deltas < 2) { | 64 if (num_of_deltas < 2) { |
60 return kBwNormal; | 65 return kBwNormal; |
61 } | 66 } |
62 const double prev_offset = prev_offset_; | 67 const double prev_offset = prev_offset_; |
63 prev_offset_ = offset; | 68 prev_offset_ = offset; |
64 const double T = std::min(num_of_deltas, 60) * offset; | 69 const double T = std::min(num_of_deltas, 60) * offset; |
65 BWE_TEST_LOGGING_PLOT(1, "offset", now_ms, T); | 70 BWE_TEST_LOGGING_PLOT(1, "offset", now_ms, T); |
66 BWE_TEST_LOGGING_PLOT(1, "threshold", now_ms, threshold_); | 71 BWE_TEST_LOGGING_PLOT(1, "threshold", now_ms, threshold_); |
67 | |
68 if (T > threshold_) { | 72 if (T > threshold_) { |
69 if (time_over_using_ == -1) { | 73 if (time_over_using_ == -1) { |
70 // Initialize the timer. Assume that we've been | 74 // Initialize the timer. Assume that we've been |
71 // over-using half of the time since the previous | 75 // over-using half of the time since the previous |
72 // sample. | 76 // sample. |
73 time_over_using_ = ts_delta / 2; | 77 time_over_using_ = ts_delta / 2; |
74 } else { | 78 } else { |
75 // Increment timer | 79 // Increment timer |
76 time_over_using_ += ts_delta; | 80 time_over_using_ += ts_delta; |
77 } | 81 } |
78 overuse_counter_++; | 82 overuse_counter_++; |
79 if (time_over_using_ > kOverUsingTimeThreshold | 83 if (time_over_using_ > kOverUsingTimeThreshold |
80 && overuse_counter_ > 1) { | 84 && overuse_counter_ > 1) { |
81 if (offset >= prev_offset) { | 85 if (offset >= prev_offset) { |
82 time_over_using_ = 0; | 86 time_over_using_ = 0; |
83 overuse_counter_ = 0; | 87 overuse_counter_ = 0; |
84 hypothesis_ = kBwOverusing; | 88 hypothesis_ = kBwOverusing; |
85 } | 89 } |
86 } | 90 } |
87 } else if (T < -threshold_) { | 91 } else if (T < -threshold_) { |
88 time_over_using_ = -1; | 92 time_over_using_ = -1; |
89 overuse_counter_ = 0; | 93 overuse_counter_ = 0; |
90 hypothesis_ = kBwUnderusing; | 94 hypothesis_ = kBwUnderusing; |
91 } else { | 95 } else { |
92 time_over_using_ = -1; | 96 time_over_using_ = -1; |
93 overuse_counter_ = 0; | 97 overuse_counter_ = 0; |
94 hypothesis_ = kBwNormal; | 98 hypothesis_ = kBwNormal; |
95 } | 99 } |
100 | |
101 UpdateThreshold(T, now_ms); | |
102 | |
96 return hypothesis_; | 103 return hypothesis_; |
97 } | 104 } |
105 | |
106 void OveruseDetector::UpdateThreshold(double modified_offset, int64_t now_ms) { | |
mflodman
2015/07/03 08:29:56
LG for the parts I know about this algorithm, but
| |
107 if (!AdaptiveThresholdExperimentIsEnabled()) | |
108 return; | |
109 | |
110 if (last_update_ms_ == -1) | |
111 last_update_ms_ = now_ms; | |
112 | |
113 if (fabs(modified_offset) > threshold_ + kMaxAdaptOffsetMs) { | |
114 // Avoid adapting the threshold to big latency spikes, caused e.g., | |
115 // by a sudden capacity drop. | |
116 last_update_ms_ = now_ms; | |
117 return; | |
118 } | |
119 | |
120 const double k = fabs(modified_offset) < threshold_ ? k_down_ : k_up_; | |
121 threshold_ += | |
122 k * (fabs(modified_offset) - threshold_) * (now_ms - last_update_ms_); | |
123 | |
124 const double kMinThreshold = 6; | |
125 const double kMaxThreshold = 600; | |
126 threshold_ = std::min(std::max(threshold_, kMinThreshold), kMaxThreshold); | |
127 | |
128 last_update_ms_ = now_ms; | |
129 } | |
130 | |
131 bool OveruseDetector::AdaptiveThresholdExperimentIsEnabled() const { | |
132 std::string experiment_string = | |
133 webrtc::field_trial::FindFullName(kAdaptiveThresholdExperiment); | |
134 if (experiment_string.length() < kMinExperimentLength) | |
135 return false; | |
136 return experiment_string.substr(0, kEnabledPrefixLength) == kEnabledPrefix; | |
137 } | |
138 | |
139 // Gets thresholds from the experiment name following the format | |
140 // "WebRTC-AdaptiveBweThreshold/Enabled-0.5,0.002/". | |
141 bool OveruseDetector::GetExperimentThresholds(double* k_up, | |
142 double* k_down) const { | |
143 if (!AdaptiveThresholdExperimentIsEnabled()) | |
144 return false; | |
145 std::string experiment_string = | |
146 webrtc::field_trial::FindFullName(kAdaptiveThresholdExperiment); | |
147 std::stringstream ss(experiment_string.substr( | |
148 kEnabledPrefixLength + 1)); | |
149 ss >> *k_up; | |
150 if (ss.peek() != ',') | |
151 return false; | |
152 ss.ignore(); | |
153 ss >> *k_down; | |
154 if (ss.rdbuf()->in_avail() > 0) | |
155 return false; | |
156 return true; | |
157 } | |
98 } // namespace webrtc | 158 } // namespace webrtc |
OLD | NEW |