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/checks.h" | |
| 19 #include "webrtc/base/common.h" | |
| 17 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" | 20 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" |
| 18 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" | 21 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
| 19 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" | 22 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" |
| 23 #include "webrtc/system_wrappers/interface/field_trial.h" | |
| 20 #include "webrtc/system_wrappers/interface/trace.h" | 24 #include "webrtc/system_wrappers/interface/trace.h" |
| 21 | 25 |
| 22 namespace webrtc { | 26 namespace webrtc { |
| 23 | 27 |
| 24 enum { kOverUsingTimeThreshold = 100 }; | 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 const double kOverUsingTimeThreshold = 10; | |
| 35 | |
| 36 bool AdaptiveThresholdExperimentIsEnabled() { | |
| 37 std::string experiment_string = | |
| 38 webrtc::field_trial::FindFullName(kAdaptiveThresholdExperiment); | |
| 39 if (experiment_string.length() < kMinExperimentLength) | |
| 40 return false; | |
| 41 return experiment_string.substr(0, kEnabledPrefixLength) == kEnabledPrefix; | |
| 42 } | |
| 25 | 43 |
| 26 OveruseDetector::OveruseDetector(const OverUseDetectorOptions& options) | 44 OveruseDetector::OveruseDetector(const OverUseDetectorOptions& options) |
| 27 : options_(options), | 45 : in_experiment_(AdaptiveThresholdExperimentIsEnabled()), |
| 28 threshold_(options_.initial_threshold), | 46 k_up_(0.01), |
| 47 k_down_(0.00018), | |
| 48 overusing_time_threshold_(100), | |
| 49 options_(options), | |
| 50 threshold_(12.5), | |
| 51 last_update_ms_(-1), | |
| 29 prev_offset_(0.0), | 52 prev_offset_(0.0), |
| 30 time_over_using_(-1), | 53 time_over_using_(-1), |
| 31 overuse_counter_(0), | 54 overuse_counter_(0), |
| 32 hypothesis_(kBwNormal) {} | 55 hypothesis_(kBwNormal) { |
| 56 if (in_experiment_) | |
| 57 InitializeExperiment(); | |
| 58 } | |
| 33 | 59 |
| 34 OveruseDetector::~OveruseDetector() {} | 60 OveruseDetector::~OveruseDetector() {} |
| 35 | 61 |
| 36 BandwidthUsage OveruseDetector::State() const { | 62 BandwidthUsage OveruseDetector::State() const { |
| 37 return hypothesis_; | 63 return hypothesis_; |
| 38 } | 64 } |
| 39 | 65 |
| 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, | 66 BandwidthUsage OveruseDetector::Detect(double offset, |
| 56 double ts_delta, | 67 double ts_delta, |
| 57 int num_of_deltas, | 68 int num_of_deltas, |
| 58 int64_t now_ms) { | 69 int64_t now_ms) { |
| 59 if (num_of_deltas < 2) { | 70 if (num_of_deltas < 2) { |
| 60 return kBwNormal; | 71 return kBwNormal; |
| 61 } | 72 } |
| 62 const double prev_offset = prev_offset_; | 73 const double prev_offset = prev_offset_; |
| 63 prev_offset_ = offset; | 74 prev_offset_ = offset; |
| 64 const double T = std::min(num_of_deltas, 60) * offset; | 75 const double T = std::min(num_of_deltas, 60) * offset; |
| 65 BWE_TEST_LOGGING_PLOT(1, "offset", now_ms, T); | 76 BWE_TEST_LOGGING_PLOT(1, "offset", now_ms, T); |
| 66 BWE_TEST_LOGGING_PLOT(1, "threshold", now_ms, threshold_); | 77 BWE_TEST_LOGGING_PLOT(1, "threshold", now_ms, threshold_); |
| 67 | |
| 68 if (T > threshold_) { | 78 if (T > threshold_) { |
| 69 if (time_over_using_ == -1) { | 79 if (time_over_using_ == -1) { |
| 70 // Initialize the timer. Assume that we've been | 80 // Initialize the timer. Assume that we've been |
| 71 // over-using half of the time since the previous | 81 // over-using half of the time since the previous |
| 72 // sample. | 82 // sample. |
| 73 time_over_using_ = ts_delta / 2; | 83 time_over_using_ = ts_delta / 2; |
| 74 } else { | 84 } else { |
| 75 // Increment timer | 85 // Increment timer |
| 76 time_over_using_ += ts_delta; | 86 time_over_using_ += ts_delta; |
| 77 } | 87 } |
| 78 overuse_counter_++; | 88 overuse_counter_++; |
| 79 if (time_over_using_ > kOverUsingTimeThreshold | 89 if (time_over_using_ > kOverUsingTimeThreshold |
| 80 && overuse_counter_ > 1) { | 90 && overuse_counter_ > 1) { |
| 81 if (offset >= prev_offset) { | 91 if (offset >= prev_offset) { |
| 82 time_over_using_ = 0; | 92 time_over_using_ = 0; |
| 83 overuse_counter_ = 0; | 93 overuse_counter_ = 0; |
| 84 hypothesis_ = kBwOverusing; | 94 hypothesis_ = kBwOverusing; |
| 85 } | 95 } |
| 86 } | 96 } |
| 87 } else if (T < -threshold_) { | 97 } else if (T < -threshold_) { |
| 88 time_over_using_ = -1; | 98 time_over_using_ = -1; |
| 89 overuse_counter_ = 0; | 99 overuse_counter_ = 0; |
| 90 hypothesis_ = kBwUnderusing; | 100 hypothesis_ = kBwUnderusing; |
| 91 } else { | 101 } else { |
| 92 time_over_using_ = -1; | 102 time_over_using_ = -1; |
| 93 overuse_counter_ = 0; | 103 overuse_counter_ = 0; |
| 94 hypothesis_ = kBwNormal; | 104 hypothesis_ = kBwNormal; |
| 95 } | 105 } |
| 106 | |
| 107 UpdateThreshold(T, now_ms); | |
| 108 | |
| 96 return hypothesis_; | 109 return hypothesis_; |
| 97 } | 110 } |
| 111 | |
| 112 void OveruseDetector::UpdateThreshold(double modified_offset, int64_t now_ms) { | |
| 113 if (!in_experiment_) | |
|
stefan-webrtc
2015/07/07 08:31:33
I don't want to remove the in_experiment_ flag bec
| |
| 114 return; | |
| 115 | |
| 116 if (last_update_ms_ == -1) | |
| 117 last_update_ms_ = now_ms; | |
| 118 | |
| 119 if (fabs(modified_offset) > threshold_ + kMaxAdaptOffsetMs) { | |
| 120 // Avoid adapting the threshold to big latency spikes, caused e.g., | |
| 121 // by a sudden capacity drop. | |
| 122 last_update_ms_ = now_ms; | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 const double k = fabs(modified_offset) < threshold_ ? k_down_ : k_up_; | |
| 127 threshold_ += | |
| 128 k * (fabs(modified_offset) - threshold_) * (now_ms - last_update_ms_); | |
| 129 | |
| 130 const double kMinThreshold = 6; | |
| 131 const double kMaxThreshold = 600; | |
| 132 threshold_ = std::min(std::max(threshold_, kMinThreshold), kMaxThreshold); | |
| 133 | |
| 134 last_update_ms_ = now_ms; | |
| 135 } | |
| 136 | |
| 137 // Gets thresholds from the experiment name following the format | |
| 138 // "WebRTC-AdaptiveBweThreshold/Enabled-0.5,0.002/". | |
| 139 void OveruseDetector::InitializeExperiment() { | |
| 140 double k_up = 0.0; | |
| 141 double k_down = 0.0; | |
| 142 overusing_time_threshold_ = kOverUsingTimeThreshold; | |
| 143 if (ReadExperimentConstants(&k_up, &k_down)) { | |
| 144 k_up_ = k_up; | |
| 145 k_down_ = k_down; | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 bool OveruseDetector::ReadExperimentConstants(double* k_up, | |
| 150 double* k_down) const { | |
| 151 std::string experiment_string = | |
| 152 webrtc::field_trial::FindFullName(kAdaptiveThresholdExperiment); | |
| 153 std::stringstream ss(experiment_string.substr(kEnabledPrefixLength + 1)); | |
| 154 return sscanf(experiment_string.substr(kEnabledPrefixLength + 1).c_str(), | |
| 155 "%lf,%lf", k_up, k_down) == 2; | |
| 156 } | |
| 98 } // namespace webrtc | 157 } // namespace webrtc |
| OLD | NEW |