Chromium Code Reviews| 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 = sizeof(kEnabledPrefix) - 1; | |
| 31 const size_t kMinExperimentLength = kEnabledPrefixLength + 3; | |
| 32 | |
| 33 const double kMaxAdaptOffsetMs = 15.0; | |
| 34 | |
| 35 bool AdaptiveThresholdExperimentIsEnabled() { | |
| 36 std::string experiment_string = | |
| 37 webrtc::field_trial::FindFullName(kAdaptiveThresholdExperiment); | |
| 38 if (experiment_string.length() < kMinExperimentLength) | |
| 39 return false; | |
| 40 return experiment_string.substr(0, kEnabledPrefixLength) == kEnabledPrefix; | |
| 41 } | |
| 25 | 42 |
| 26 OveruseDetector::OveruseDetector(const OverUseDetectorOptions& options) | 43 OveruseDetector::OveruseDetector(const OverUseDetectorOptions& options) |
| 27 : options_(options), | 44 : threshold_(12.5), |
| 28 threshold_(options_.initial_threshold), | 45 k_up_(0.01), |
| 46 k_down_(0.00018), | |
| 47 experiment_set_(false), | |
| 48 in_experiment_(false), | |
|
pbos-webrtc
2015/07/06 10:01:40
Can you make a struct of experiment settings (if i
stefan-webrtc
2015/07/06 10:54:26
There are only sublcasses in tests. Don't think it
| |
| 49 options_(options), | |
| 50 last_update_ms_(-1), | |
| 29 prev_offset_(0.0), | 51 prev_offset_(0.0), |
| 30 time_over_using_(-1), | 52 time_over_using_(-1), |
| 31 overuse_counter_(0), | 53 overuse_counter_(0), |
| 32 hypothesis_(kBwNormal) {} | 54 hypothesis_(kBwNormal) { |
| 55 } | |
| 33 | 56 |
| 34 OveruseDetector::~OveruseDetector() {} | 57 OveruseDetector::~OveruseDetector() {} |
| 35 | 58 |
| 36 BandwidthUsage OveruseDetector::State() const { | 59 BandwidthUsage OveruseDetector::State() const { |
| 37 return hypothesis_; | 60 return hypothesis_; |
| 38 } | 61 } |
| 39 | 62 |
| 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, | 63 BandwidthUsage OveruseDetector::Detect(double offset, |
| 56 double ts_delta, | 64 double ts_delta, |
| 57 int num_of_deltas, | 65 int num_of_deltas, |
| 58 int64_t now_ms) { | 66 int64_t now_ms) { |
| 59 if (num_of_deltas < 2) { | 67 if (num_of_deltas < 2) { |
| 60 return kBwNormal; | 68 return kBwNormal; |
| 61 } | 69 } |
| 62 const double prev_offset = prev_offset_; | 70 const double prev_offset = prev_offset_; |
| 63 prev_offset_ = offset; | 71 prev_offset_ = offset; |
| 64 const double T = std::min(num_of_deltas, 60) * offset; | 72 const double T = std::min(num_of_deltas, 60) * offset; |
| 65 BWE_TEST_LOGGING_PLOT(1, "offset", now_ms, T); | 73 BWE_TEST_LOGGING_PLOT(1, "offset", now_ms, T); |
| 66 BWE_TEST_LOGGING_PLOT(1, "threshold", now_ms, threshold_); | 74 BWE_TEST_LOGGING_PLOT(1, "threshold", now_ms, threshold_); |
| 67 | |
| 68 if (T > threshold_) { | 75 if (T > threshold_) { |
| 69 if (time_over_using_ == -1) { | 76 if (time_over_using_ == -1) { |
| 70 // Initialize the timer. Assume that we've been | 77 // Initialize the timer. Assume that we've been |
| 71 // over-using half of the time since the previous | 78 // over-using half of the time since the previous |
| 72 // sample. | 79 // sample. |
| 73 time_over_using_ = ts_delta / 2; | 80 time_over_using_ = ts_delta / 2; |
| 74 } else { | 81 } else { |
| 75 // Increment timer | 82 // Increment timer |
| 76 time_over_using_ += ts_delta; | 83 time_over_using_ += ts_delta; |
| 77 } | 84 } |
| 78 overuse_counter_++; | 85 overuse_counter_++; |
| 79 if (time_over_using_ > kOverUsingTimeThreshold | 86 if (time_over_using_ > kOverUsingTimeThreshold |
| 80 && overuse_counter_ > 1) { | 87 && overuse_counter_ > 1) { |
| 81 if (offset >= prev_offset) { | 88 if (offset >= prev_offset) { |
| 82 time_over_using_ = 0; | 89 time_over_using_ = 0; |
| 83 overuse_counter_ = 0; | 90 overuse_counter_ = 0; |
| 84 hypothesis_ = kBwOverusing; | 91 hypothesis_ = kBwOverusing; |
| 85 } | 92 } |
| 86 } | 93 } |
| 87 } else if (T < -threshold_) { | 94 } else if (T < -threshold_) { |
| 88 time_over_using_ = -1; | 95 time_over_using_ = -1; |
| 89 overuse_counter_ = 0; | 96 overuse_counter_ = 0; |
| 90 hypothesis_ = kBwUnderusing; | 97 hypothesis_ = kBwUnderusing; |
| 91 } else { | 98 } else { |
| 92 time_over_using_ = -1; | 99 time_over_using_ = -1; |
| 93 overuse_counter_ = 0; | 100 overuse_counter_ = 0; |
| 94 hypothesis_ = kBwNormal; | 101 hypothesis_ = kBwNormal; |
| 95 } | 102 } |
| 103 | |
| 104 UpdateThreshold(T, now_ms); | |
| 105 | |
| 96 return hypothesis_; | 106 return hypothesis_; |
| 97 } | 107 } |
| 108 | |
| 109 void OveruseDetector::UpdateThreshold(double modified_offset, int64_t now_ms) { | |
| 110 if (!AdaptiveThresholdExperimentIsEnabled()) | |
|
pbos-webrtc
2015/07/06 10:01:40
Call in constructor instead, make in_experiment_ c
stefan-webrtc
2015/07/06 10:54:26
Fine with me. The reason for doing it this way was
| |
| 111 return; | |
| 112 | |
| 113 if (last_update_ms_ == -1) | |
| 114 last_update_ms_ = now_ms; | |
| 115 | |
| 116 if (fabs(modified_offset) > threshold_ + kMaxAdaptOffsetMs) { | |
| 117 // Avoid adapting the threshold to big latency spikes, caused e.g., | |
| 118 // by a sudden capacity drop. | |
| 119 last_update_ms_ = now_ms; | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 const double k = fabs(modified_offset) < threshold_ ? k_down_ : k_up_; | |
| 124 threshold_ += | |
| 125 k * (fabs(modified_offset) - threshold_) * (now_ms - last_update_ms_); | |
| 126 | |
| 127 const double kMinThreshold = 6; | |
| 128 const double kMaxThreshold = 600; | |
| 129 threshold_ = std::min(std::max(threshold_, kMinThreshold), kMaxThreshold); | |
| 130 | |
| 131 last_update_ms_ = now_ms; | |
| 132 } | |
| 133 | |
| 134 bool OveruseDetector::AdaptiveThresholdExperimentIsEnabled() { | |
| 135 InitializeExperiment(); | |
| 136 return in_experiment_; | |
| 137 } | |
| 138 | |
| 139 // Gets thresholds from the experiment name following the format | |
| 140 // "WebRTC-AdaptiveBweThreshold/Enabled-0.5,0.002/". | |
| 141 void OveruseDetector::InitializeExperiment() { | |
| 142 if (!experiment_set_) { | |
|
pbos-webrtc
2015/07/06 10:01:40
remove experiment_set_, this should not be lazy.
stefan-webrtc
2015/07/06 10:54:26
Done.
| |
| 143 experiment_set_ = true; | |
| 144 in_experiment_ = webrtc::AdaptiveThresholdExperimentIsEnabled(); | |
| 145 if (in_experiment_) { | |
| 146 double k_up = 0.0; | |
| 147 double k_down = 0.0; | |
| 148 if (ReadExperimentConstants(&k_up, &k_down)) { | |
| 149 k_up_ = k_up; | |
| 150 k_down_ = k_down; | |
| 151 } | |
| 152 } | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 bool OveruseDetector::ReadExperimentConstants(double* k_up, | |
| 157 double* k_down) const { | |
| 158 std::string experiment_string = | |
| 159 webrtc::field_trial::FindFullName(kAdaptiveThresholdExperiment); | |
| 160 std::stringstream ss(experiment_string.substr(kEnabledPrefixLength + 1)); | |
| 161 ss >> *k_up; | |
| 162 if (ss.peek() != ',') | |
| 163 return false; | |
| 164 ss.ignore(); | |
| 165 ss >> *k_down; | |
| 166 if (ss.rdbuf()->in_avail() > 0) | |
| 167 return false; | |
| 168 return true; | |
| 169 } | |
| 98 } // namespace webrtc | 170 } // namespace webrtc |
| OLD | NEW |