| 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 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 int max_measurements) | 21 int max_measurements) |
| 22 : buffer_(new int[max_measurements]), | 22 : buffer_(new int[max_measurements]), |
| 23 max_measurements_(max_measurements), | 23 max_measurements_(max_measurements), |
| 24 fraction_(fraction), | 24 fraction_(fraction), |
| 25 low_threshold_(low_threshold), | 25 low_threshold_(low_threshold), |
| 26 high_threshold_(high_threshold), | 26 high_threshold_(high_threshold), |
| 27 until_full_(max_measurements), | 27 until_full_(max_measurements), |
| 28 next_index_(0), | 28 next_index_(0), |
| 29 sum_(0), | 29 sum_(0), |
| 30 count_low_(0), | 30 count_low_(0), |
| 31 count_high_(0) { | 31 count_high_(0), |
| 32 num_high_states_(0), |
| 33 num_certain_states_(0) { |
| 32 RTC_CHECK_GT(fraction, 0.5f); | 34 RTC_CHECK_GT(fraction, 0.5f); |
| 33 RTC_CHECK_GT(max_measurements, 1); | 35 RTC_CHECK_GT(max_measurements, 1); |
| 34 RTC_CHECK_LT(low_threshold, high_threshold); | 36 RTC_CHECK_LT(low_threshold, high_threshold); |
| 35 } | 37 } |
| 36 | 38 |
| 37 void QualityThreshold::AddMeasurement(int measurement) { | 39 void QualityThreshold::AddMeasurement(int measurement) { |
| 38 int prev_val = until_full_ > 0 ? 0 : buffer_[next_index_]; | 40 int prev_val = until_full_ > 0 ? 0 : buffer_[next_index_]; |
| 39 buffer_[next_index_] = measurement; | 41 buffer_[next_index_] = measurement; |
| 40 next_index_ = (next_index_ + 1) % max_measurements_; | 42 next_index_ = (next_index_ + 1) % max_measurements_; |
| 41 | 43 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 57 | 59 |
| 58 float sufficient_majority = fraction_ * max_measurements_; | 60 float sufficient_majority = fraction_ * max_measurements_; |
| 59 if (count_high_ >= sufficient_majority) { | 61 if (count_high_ >= sufficient_majority) { |
| 60 is_high_ = rtc::Optional<bool>(true); | 62 is_high_ = rtc::Optional<bool>(true); |
| 61 } else if (count_low_ >= sufficient_majority) { | 63 } else if (count_low_ >= sufficient_majority) { |
| 62 is_high_ = rtc::Optional<bool>(false); | 64 is_high_ = rtc::Optional<bool>(false); |
| 63 } | 65 } |
| 64 | 66 |
| 65 if (until_full_ > 0) | 67 if (until_full_ > 0) |
| 66 --until_full_; | 68 --until_full_; |
| 69 |
| 70 if (is_high_) { |
| 71 if (*is_high_) |
| 72 ++num_high_states_; |
| 73 ++num_certain_states_; |
| 74 } |
| 67 } | 75 } |
| 68 | 76 |
| 69 rtc::Optional<bool> QualityThreshold::IsHigh() const { | 77 rtc::Optional<bool> QualityThreshold::IsHigh() const { |
| 70 return is_high_; | 78 return is_high_; |
| 71 } | 79 } |
| 72 | 80 |
| 73 rtc::Optional<double> QualityThreshold::CalculateVariance() const { | 81 rtc::Optional<double> QualityThreshold::CalculateVariance() const { |
| 74 if (until_full_ > 0) { | 82 if (until_full_ > 0) { |
| 75 return rtc::Optional<double>(); | 83 return rtc::Optional<double>(); |
| 76 } | 84 } |
| 77 | 85 |
| 78 double variance = 0; | 86 double variance = 0; |
| 79 double mean = static_cast<double>(sum_) / max_measurements_; | 87 double mean = static_cast<double>(sum_) / max_measurements_; |
| 80 for (int i = 0; i < max_measurements_; ++i) { | 88 for (int i = 0; i < max_measurements_; ++i) { |
| 81 variance += (buffer_[i] - mean) * (buffer_[i] - mean); | 89 variance += (buffer_[i] - mean) * (buffer_[i] - mean); |
| 82 } | 90 } |
| 83 return rtc::Optional<double>(variance / (max_measurements_ - 1)); | 91 return rtc::Optional<double>(variance / (max_measurements_ - 1)); |
| 84 } | 92 } |
| 85 | 93 |
| 94 rtc::Optional<double> QualityThreshold::FractionHigh( |
| 95 int min_required_samples) const { |
| 96 RTC_DCHECK_GT(min_required_samples, 0); |
| 97 if (num_certain_states_ < min_required_samples) |
| 98 return rtc::Optional<double>(); |
| 99 |
| 100 return rtc::Optional<double>(static_cast<double>(num_high_states_) / |
| 101 num_certain_states_); |
| 102 } |
| 103 |
| 86 } // namespace webrtc | 104 } // namespace webrtc |
| OLD | NEW |