mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/video_engine/overuse_frame_detector.h" |
| 12 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 13 | #include <assert.h> |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 14 | #include <math.h> |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 15 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <list> |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 18 | #include <map> |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 19 | |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 20 | #include "webrtc/base/exp_filter.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/interface/clock.h" |
| 22 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 23 | #include "webrtc/system_wrappers/interface/logging.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 27 | // TODO(mflodman) Test different values for all of these to trigger correctly, |
| 28 | // avoid fluctuations etc. |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 29 | namespace { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 30 | const int64_t kProcessIntervalMs = 5000; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 31 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 32 | // Weight factor to apply to the standard deviation. |
| 33 | const float kWeightFactor = 0.997f; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 34 | // Weight factor to apply to the average. |
| 35 | const float kWeightFactorMean = 0.98f; |
| 36 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 37 | // Delay between consecutive rampups. (Used for quick recovery.) |
| 38 | const int kQuickRampUpDelayMs = 10 * 1000; |
| 39 | // Delay between rampup attempts. Initially uses standard, scales up to max. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 40 | const int kStandardRampUpDelayMs = 40 * 1000; |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 41 | const int kMaxRampUpDelayMs = 240 * 1000; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 42 | // Expontential back-off factor, to prevent annoying up-down behaviour. |
| 43 | const double kRampUpBackoffFactor = 2.0; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 44 | |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 45 | // Max number of overuses detected before always applying the rampup delay. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 46 | const int kMaxOverusesBeforeApplyRampupDelay = 4; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 47 | |
| 48 | // The maximum exponent to use in VCMExpFilter. |
| 49 | const float kSampleDiffMs = 33.0f; |
| 50 | const float kMaxExp = 7.0f; |
| 51 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 52 | } // namespace |
| 53 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 54 | // TODO(asapersson): Remove this class. Not used. |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 55 | Statistics::Statistics() : |
| 56 | sum_(0.0), |
| 57 | count_(0), |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 58 | filtered_samples_(new rtc::ExpFilter(kWeightFactorMean)), |
| 59 | filtered_variance_(new rtc::ExpFilter(kWeightFactor)) { |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 60 | Reset(); |
| 61 | } |
| 62 | |
| 63 | void Statistics::SetOptions(const CpuOveruseOptions& options) { |
| 64 | options_ = options; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void Statistics::Reset() { |
| 68 | sum_ = 0.0; |
| 69 | count_ = 0; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 70 | filtered_variance_->Reset(kWeightFactor); |
| 71 | filtered_variance_->Apply(1.0f, InitialVariance()); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void Statistics::AddSample(float sample_ms) { |
| 75 | sum_ += sample_ms; |
| 76 | ++count_; |
| 77 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 78 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 79 | // Initialize filtered samples. |
| 80 | filtered_samples_->Reset(kWeightFactorMean); |
| 81 | filtered_samples_->Apply(1.0f, InitialMean()); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 85 | float exp = sample_ms / kSampleDiffMs; |
| 86 | exp = std::min(exp, kMaxExp); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 87 | filtered_samples_->Apply(exp, sample_ms); |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 88 | filtered_variance_->Apply(exp, (sample_ms - filtered_samples_->filtered()) * |
| 89 | (sample_ms - filtered_samples_->filtered())); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | float Statistics::InitialMean() const { |
| 93 | if (count_ == 0) |
| 94 | return 0.0; |
| 95 | return sum_ / count_; |
| 96 | } |
| 97 | |
| 98 | float Statistics::InitialVariance() const { |
| 99 | // Start in between the underuse and overuse threshold. |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 100 | float average_stddev = (options_.low_capture_jitter_threshold_ms + |
| 101 | options_.high_capture_jitter_threshold_ms) / 2.0f; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 102 | return average_stddev * average_stddev; |
| 103 | } |
| 104 | |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 105 | float Statistics::Mean() const { return filtered_samples_->filtered(); } |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 106 | |
| 107 | float Statistics::StdDev() const { |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 108 | return sqrt(std::max(filtered_variance_->filtered(), 0.0f)); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | uint64_t Statistics::Count() const { return count_; } |
| 112 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 113 | |
| 114 | // Class for calculating the average encode time. |
| 115 | class OveruseFrameDetector::EncodeTimeAvg { |
| 116 | public: |
| 117 | EncodeTimeAvg() |
| 118 | : kWeightFactor(0.5f), |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 119 | kInitialAvgEncodeTimeMs(5.0f), |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 120 | filtered_encode_time_ms_(new rtc::ExpFilter(kWeightFactor)) { |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 121 | filtered_encode_time_ms_->Apply(1.0f, kInitialAvgEncodeTimeMs); |
| 122 | } |
| 123 | ~EncodeTimeAvg() {} |
| 124 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 125 | void AddSample(float encode_time_ms, int64_t diff_last_sample_ms) { |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 126 | float exp = diff_last_sample_ms / kSampleDiffMs; |
| 127 | exp = std::min(exp, kMaxExp); |
| 128 | filtered_encode_time_ms_->Apply(exp, encode_time_ms); |
| 129 | } |
| 130 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 131 | int Value() const { |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 132 | return static_cast<int>(filtered_encode_time_ms_->filtered() + 0.5); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private: |
| 136 | const float kWeightFactor; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 137 | const float kInitialAvgEncodeTimeMs; |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 138 | scoped_ptr<rtc::ExpFilter> filtered_encode_time_ms_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 141 | // Class for calculating the processing usage on the send-side (the average |
| 142 | // processing time of a frame divided by the average time difference between |
| 143 | // captured frames). |
| 144 | class OveruseFrameDetector::SendProcessingUsage { |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 145 | public: |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 146 | SendProcessingUsage() |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 147 | : kWeightFactorFrameDiff(0.998f), |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 148 | kWeightFactorProcessing(0.995f), |
asapersson@webrtc.org | e41dbee | 2014-05-13 13:45:13 +0000 | [diff] [blame] | 149 | kInitialSampleDiffMs(40.0f), |
| 150 | kMaxSampleDiffMs(45.0f), |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 151 | count_(0), |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 152 | filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)), |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 153 | filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 154 | Reset(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 155 | } |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 156 | ~SendProcessingUsage() {} |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 157 | |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 158 | void SetOptions(const CpuOveruseOptions& options) { |
| 159 | options_ = options; |
| 160 | } |
| 161 | |
| 162 | void Reset() { |
| 163 | count_ = 0; |
| 164 | filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff); |
| 165 | filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 166 | filtered_processing_ms_->Reset(kWeightFactorProcessing); |
| 167 | filtered_processing_ms_->Apply(1.0f, InitialProcessingMs()); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 168 | } |
| 169 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 170 | void AddCaptureSample(float sample_ms) { |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 171 | float exp = sample_ms / kSampleDiffMs; |
| 172 | exp = std::min(exp, kMaxExp); |
| 173 | filtered_frame_diff_ms_->Apply(exp, sample_ms); |
| 174 | } |
| 175 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 176 | void AddSample(float processing_ms, int64_t diff_last_sample_ms) { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 177 | ++count_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 178 | float exp = diff_last_sample_ms / kSampleDiffMs; |
| 179 | exp = std::min(exp, kMaxExp); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 180 | filtered_processing_ms_->Apply(exp, processing_ms); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 181 | } |
| 182 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 183 | int Value() const { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 184 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
| 185 | return static_cast<int>(InitialUsageInPercent() + 0.5f); |
| 186 | } |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 187 | float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 188 | frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 189 | float encode_usage_percent = |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 190 | 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 191 | return static_cast<int>(encode_usage_percent + 0.5); |
| 192 | } |
| 193 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 194 | private: |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 195 | float InitialUsageInPercent() const { |
| 196 | // Start in between the underuse and overuse threshold. |
| 197 | return (options_.low_encode_usage_threshold_percent + |
| 198 | options_.high_encode_usage_threshold_percent) / 2.0f; |
| 199 | } |
| 200 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 201 | float InitialProcessingMs() const { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 202 | return InitialUsageInPercent() * kInitialSampleDiffMs / 100; |
| 203 | } |
| 204 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 205 | const float kWeightFactorFrameDiff; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 206 | const float kWeightFactorProcessing; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 207 | const float kInitialSampleDiffMs; |
| 208 | const float kMaxSampleDiffMs; |
| 209 | uint64_t count_; |
| 210 | CpuOveruseOptions options_; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 211 | scoped_ptr<rtc::ExpFilter> filtered_processing_ms_; |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 212 | scoped_ptr<rtc::ExpFilter> filtered_frame_diff_ms_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 215 | // Class for calculating the relative standard deviation of the processing time |
| 216 | // of frame on the send-side. |
| 217 | // Currently only used for testing. |
| 218 | class OveruseFrameDetector::SendProcessingRsd { |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 219 | public: |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 220 | SendProcessingRsd(Clock* clock) |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 221 | : kWeightFactor(0.6f), |
| 222 | count_(0), |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 223 | filtered_rsd_(new rtc::ExpFilter(kWeightFactor)), |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 224 | hist_samples_(0), |
| 225 | hist_sum_(0.0f), |
| 226 | last_process_time_ms_(clock->TimeInMilliseconds()) { |
| 227 | Reset(); |
| 228 | } |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 229 | ~SendProcessingRsd() {} |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 230 | |
| 231 | void SetOptions(const CpuOveruseOptions& options) { |
| 232 | options_ = options; |
| 233 | } |
| 234 | |
| 235 | void Reset() { |
| 236 | count_ = 0; |
| 237 | filtered_rsd_->Reset(kWeightFactor); |
| 238 | filtered_rsd_->Apply(1.0f, InitialValue()); |
| 239 | hist_.clear(); |
| 240 | hist_samples_ = 0; |
| 241 | hist_sum_ = 0.0f; |
| 242 | } |
| 243 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 244 | void AddSample(float processing_ms) { |
| 245 | int bin = static_cast<int>(processing_ms + 0.5f); |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 246 | if (bin <= 0) { |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | ++count_; |
| 250 | ++hist_[bin]; |
| 251 | ++hist_samples_; |
| 252 | hist_sum_ += bin; |
| 253 | } |
| 254 | |
| 255 | void Process(int64_t now) { |
| 256 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
| 257 | // Have not received min number of frames since last reset. |
| 258 | return; |
| 259 | } |
| 260 | const int kMinHistSamples = 20; |
| 261 | if (hist_samples_ < kMinHistSamples) { |
| 262 | return; |
| 263 | } |
| 264 | const int64_t kMinDiffSinceLastProcessMs = 1000; |
| 265 | int64_t diff_last_process_ms = now - last_process_time_ms_; |
| 266 | if (now - last_process_time_ms_ <= kMinDiffSinceLastProcessMs) { |
| 267 | return; |
| 268 | } |
| 269 | last_process_time_ms_ = now; |
| 270 | |
| 271 | // Calculate variance (using samples above the mean). |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 272 | // Checks for a larger processing time of some frames while there is a small |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 273 | // increase in the average time. |
| 274 | int mean = hist_sum_ / hist_samples_; |
| 275 | float variance = 0.0f; |
| 276 | int total_count = 0; |
| 277 | for (std::map<int,int>::iterator it = hist_.begin(); |
| 278 | it != hist_.end(); ++it) { |
| 279 | int time = it->first; |
| 280 | int count = it->second; |
| 281 | if (time > mean) { |
| 282 | total_count += count; |
| 283 | for (int i = 0; i < count; ++i) { |
| 284 | variance += ((time - mean) * (time - mean)); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | variance /= std::max(total_count, 1); |
| 289 | float cov = sqrt(variance) / mean; |
| 290 | |
| 291 | hist_.clear(); |
| 292 | hist_samples_ = 0; |
| 293 | hist_sum_ = 0.0f; |
| 294 | |
| 295 | float exp = static_cast<float>(diff_last_process_ms) / kProcessIntervalMs; |
| 296 | exp = std::min(exp, kMaxExp); |
| 297 | filtered_rsd_->Apply(exp, 100.0f * cov); |
| 298 | } |
| 299 | |
| 300 | int Value() const { |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 301 | return static_cast<int>(filtered_rsd_->filtered() + 0.5); |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 302 | } |
| 303 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 304 | private: |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 305 | float InitialValue() const { |
| 306 | // Start in between the underuse and overuse threshold. |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 307 | return std::max(((options_.low_encode_time_rsd_threshold + |
| 308 | options_.high_encode_time_rsd_threshold) / 2.0f), 0.0f); |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 309 | } |
| 310 | |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 311 | const float kWeightFactor; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 312 | uint32_t count_; // Number of samples since last reset. |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 313 | CpuOveruseOptions options_; |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 314 | scoped_ptr<rtc::ExpFilter> filtered_rsd_; |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 315 | int hist_samples_; |
| 316 | float hist_sum_; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 317 | std::map<int, int> hist_; // Histogram of time spent on processing frames. |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 318 | int64_t last_process_time_ms_; |
| 319 | }; |
| 320 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 321 | // Class for calculating the processing time of frames. |
| 322 | class OveruseFrameDetector::FrameQueue { |
| 323 | public: |
| 324 | FrameQueue() : last_processing_time_ms_(-1) {} |
| 325 | ~FrameQueue() {} |
| 326 | |
| 327 | // Called when a frame is captured. |
| 328 | // Starts the measuring of the processing time of the frame. |
| 329 | void Start(int64_t capture_time, int64_t now) { |
| 330 | const size_t kMaxSize = 90; // Allows for processing time of 1.5s at 60fps. |
| 331 | if (frame_times_.size() > kMaxSize) { |
| 332 | LOG(LS_WARNING) << "Max size reached, removed oldest frame."; |
| 333 | frame_times_.erase(frame_times_.begin()); |
| 334 | } |
| 335 | if (frame_times_.find(capture_time) != frame_times_.end()) { |
| 336 | // Frame should not exist. |
| 337 | assert(false); |
| 338 | return; |
| 339 | } |
| 340 | frame_times_[capture_time] = now; |
| 341 | } |
| 342 | |
| 343 | // Called when the processing of a frame has finished. |
| 344 | // Returns the processing time of the frame. |
| 345 | int End(int64_t capture_time, int64_t now) { |
| 346 | std::map<int64_t, int64_t>::iterator it = frame_times_.find(capture_time); |
| 347 | if (it == frame_times_.end()) { |
| 348 | return -1; |
| 349 | } |
| 350 | // Remove any old frames up to current. |
| 351 | // Old frames have been skipped by the capture process thread. |
| 352 | // TODO(asapersson): Consider measuring time from first frame in list. |
| 353 | last_processing_time_ms_ = now - (*it).second; |
| 354 | frame_times_.erase(frame_times_.begin(), ++it); |
| 355 | return last_processing_time_ms_; |
| 356 | } |
| 357 | |
| 358 | void Reset() { frame_times_.clear(); } |
| 359 | int NumFrames() const { return frame_times_.size(); } |
| 360 | int last_processing_time_ms() const { return last_processing_time_ms_; } |
| 361 | |
| 362 | private: |
| 363 | // Captured frames mapped by the capture time. |
| 364 | std::map<int64_t, int64_t> frame_times_; |
| 365 | int last_processing_time_ms_; |
| 366 | }; |
| 367 | |
| 368 | // TODO(asapersson): Remove this class. Not used. |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 369 | // Class for calculating the capture queue delay change. |
| 370 | class OveruseFrameDetector::CaptureQueueDelay { |
| 371 | public: |
| 372 | CaptureQueueDelay() |
| 373 | : kWeightFactor(0.5f), |
| 374 | delay_ms_(0), |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 375 | filtered_delay_ms_per_s_(new rtc::ExpFilter(kWeightFactor)) { |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 376 | filtered_delay_ms_per_s_->Apply(1.0f, 0.0f); |
| 377 | } |
| 378 | ~CaptureQueueDelay() {} |
| 379 | |
| 380 | void FrameCaptured(int64_t now) { |
| 381 | const size_t kMaxSize = 200; |
| 382 | if (frames_.size() > kMaxSize) { |
| 383 | frames_.pop_front(); |
| 384 | } |
| 385 | frames_.push_back(now); |
| 386 | } |
| 387 | |
| 388 | void FrameProcessingStarted(int64_t now) { |
| 389 | if (frames_.empty()) { |
| 390 | return; |
| 391 | } |
| 392 | delay_ms_ = now - frames_.front(); |
| 393 | frames_.pop_front(); |
| 394 | } |
| 395 | |
| 396 | void CalculateDelayChange(int64_t diff_last_sample_ms) { |
| 397 | if (diff_last_sample_ms <= 0) { |
| 398 | return; |
| 399 | } |
| 400 | float exp = static_cast<float>(diff_last_sample_ms) / kProcessIntervalMs; |
| 401 | exp = std::min(exp, kMaxExp); |
| 402 | filtered_delay_ms_per_s_->Apply(exp, |
| 403 | delay_ms_ * 1000.0f / diff_last_sample_ms); |
| 404 | ClearFrames(); |
| 405 | } |
| 406 | |
| 407 | void ClearFrames() { |
| 408 | frames_.clear(); |
| 409 | } |
| 410 | |
| 411 | int delay_ms() const { |
| 412 | return delay_ms_; |
| 413 | } |
| 414 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 415 | int Value() const { |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 416 | return static_cast<int>(filtered_delay_ms_per_s_->filtered() + 0.5); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | private: |
| 420 | const float kWeightFactor; |
| 421 | std::list<int64_t> frames_; |
| 422 | int delay_ms_; |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 423 | scoped_ptr<rtc::ExpFilter> filtered_delay_ms_per_s_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 424 | }; |
| 425 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 426 | OveruseFrameDetector::OveruseFrameDetector(Clock* clock) |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 427 | : crit_(CriticalSectionWrapper::CreateCriticalSection()), |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 428 | observer_(NULL), |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 429 | clock_(clock), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 430 | next_process_time_(clock_->TimeInMilliseconds()), |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 431 | num_process_times_(0), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 432 | last_capture_time_(0), |
| 433 | last_overuse_time_(0), |
| 434 | checks_above_threshold_(0), |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 435 | num_overuse_detections_(0), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 436 | last_rampup_time_(0), |
| 437 | in_quick_rampup_(false), |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 438 | current_rampup_delay_ms_(kStandardRampUpDelayMs), |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 439 | num_pixels_(0), |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 440 | last_encode_sample_ms_(0), |
| 441 | encode_time_(new EncodeTimeAvg()), |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 442 | rsd_(new SendProcessingRsd(clock)), |
| 443 | usage_(new SendProcessingUsage()), |
| 444 | frame_queue_(new FrameQueue()), |
| 445 | last_sample_time_ms_(0), |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 446 | capture_queue_delay_(new CaptureQueueDelay()) { |
| 447 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 448 | |
| 449 | OveruseFrameDetector::~OveruseFrameDetector() { |
| 450 | } |
| 451 | |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 452 | void OveruseFrameDetector::SetObserver(CpuOveruseObserver* observer) { |
| 453 | CriticalSectionScoped cs(crit_.get()); |
| 454 | observer_ = observer; |
| 455 | } |
| 456 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 457 | void OveruseFrameDetector::SetOptions(const CpuOveruseOptions& options) { |
| 458 | assert(options.min_frame_samples > 0); |
| 459 | CriticalSectionScoped cs(crit_.get()); |
| 460 | if (options_.Equals(options)) { |
| 461 | return; |
| 462 | } |
| 463 | options_ = options; |
| 464 | capture_deltas_.SetOptions(options); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 465 | usage_->SetOptions(options); |
| 466 | rsd_->SetOptions(options); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 467 | ResetAll(num_pixels_); |
| 468 | } |
| 469 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 470 | int OveruseFrameDetector::CaptureQueueDelayMsPerS() const { |
| 471 | CriticalSectionScoped cs(crit_.get()); |
| 472 | return capture_queue_delay_->delay_ms(); |
| 473 | } |
| 474 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 475 | int OveruseFrameDetector::LastProcessingTimeMs() const { |
| 476 | CriticalSectionScoped cs(crit_.get()); |
| 477 | return frame_queue_->last_processing_time_ms(); |
| 478 | } |
| 479 | |
| 480 | int OveruseFrameDetector::FramesInQueue() const { |
| 481 | CriticalSectionScoped cs(crit_.get()); |
| 482 | return frame_queue_->NumFrames(); |
| 483 | } |
| 484 | |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 485 | void OveruseFrameDetector::GetCpuOveruseMetrics( |
| 486 | CpuOveruseMetrics* metrics) const { |
| 487 | CriticalSectionScoped cs(crit_.get()); |
| 488 | metrics->capture_jitter_ms = static_cast<int>(capture_deltas_.StdDev() + 0.5); |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 489 | metrics->avg_encode_time_ms = encode_time_->Value(); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 490 | metrics->encode_rsd = rsd_->Value(); |
| 491 | metrics->encode_usage_percent = usage_->Value(); |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 492 | metrics->capture_queue_delay_ms_per_s = capture_queue_delay_->Value(); |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 493 | } |
| 494 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 495 | int32_t OveruseFrameDetector::TimeUntilNextProcess() { |
| 496 | CriticalSectionScoped cs(crit_.get()); |
| 497 | return next_process_time_ - clock_->TimeInMilliseconds(); |
| 498 | } |
| 499 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 500 | bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const { |
| 501 | if (num_pixels != num_pixels_) { |
| 502 | return true; |
| 503 | } |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const { |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 508 | if (last_capture_time_ == 0) { |
| 509 | return false; |
| 510 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 511 | return (now - last_capture_time_) > options_.frame_timeout_interval_ms; |
| 512 | } |
| 513 | |
| 514 | void OveruseFrameDetector::ResetAll(int num_pixels) { |
| 515 | num_pixels_ = num_pixels; |
| 516 | capture_deltas_.Reset(); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 517 | usage_->Reset(); |
| 518 | rsd_->Reset(); |
| 519 | frame_queue_->Reset(); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 520 | capture_queue_delay_->ClearFrames(); |
| 521 | last_capture_time_ = 0; |
| 522 | num_process_times_ = 0; |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 523 | } |
| 524 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 525 | void OveruseFrameDetector::FrameCaptured(int width, |
| 526 | int height, |
| 527 | int64_t capture_time_ms) { |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 528 | CriticalSectionScoped cs(crit_.get()); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 529 | |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 530 | int64_t now = clock_->TimeInMilliseconds(); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 531 | if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) { |
| 532 | ResetAll(width * height); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 533 | } |
| 534 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 535 | if (last_capture_time_ != 0) { |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 536 | capture_deltas_.AddSample(now - last_capture_time_); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 537 | usage_->AddCaptureSample(now - last_capture_time_); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 538 | } |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 539 | last_capture_time_ = now; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 540 | |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 541 | capture_queue_delay_->FrameCaptured(now); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 542 | |
| 543 | if (options_.enable_extended_processing_usage) { |
| 544 | frame_queue_->Start(capture_time_ms, now); |
| 545 | } |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void OveruseFrameDetector::FrameProcessingStarted() { |
| 549 | CriticalSectionScoped cs(crit_.get()); |
| 550 | capture_queue_delay_->FrameProcessingStarted(clock_->TimeInMilliseconds()); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 551 | } |
| 552 | |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 553 | void OveruseFrameDetector::FrameEncoded(int encode_time_ms) { |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 554 | CriticalSectionScoped cs(crit_.get()); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 555 | int64_t now = clock_->TimeInMilliseconds(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 556 | if (last_encode_sample_ms_ != 0) { |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 557 | int64_t diff_ms = now - last_encode_sample_ms_; |
| 558 | encode_time_->AddSample(encode_time_ms, diff_ms); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 559 | } |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 560 | last_encode_sample_ms_ = now; |
| 561 | |
| 562 | if (!options_.enable_extended_processing_usage) { |
| 563 | AddProcessingTime(encode_time_ms); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | void OveruseFrameDetector::FrameSent(int64_t capture_time_ms) { |
| 568 | CriticalSectionScoped cs(crit_.get()); |
| 569 | if (!options_.enable_extended_processing_usage) { |
| 570 | return; |
| 571 | } |
| 572 | int delay_ms = frame_queue_->End(capture_time_ms, |
| 573 | clock_->TimeInMilliseconds()); |
| 574 | if (delay_ms > 0) { |
| 575 | AddProcessingTime(delay_ms); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void OveruseFrameDetector::AddProcessingTime(int elapsed_ms) { |
| 580 | int64_t now = clock_->TimeInMilliseconds(); |
| 581 | if (last_sample_time_ms_ != 0) { |
| 582 | int64_t diff_ms = now - last_sample_time_ms_; |
| 583 | usage_->AddSample(elapsed_ms, diff_ms); |
| 584 | rsd_->AddSample(elapsed_ms); |
| 585 | } |
| 586 | last_sample_time_ms_ = now; |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 587 | } |
| 588 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 589 | int32_t OveruseFrameDetector::Process() { |
| 590 | CriticalSectionScoped cs(crit_.get()); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 591 | |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 592 | int64_t now = clock_->TimeInMilliseconds(); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 593 | |
| 594 | // Used to protect against Process() being called too often. |
| 595 | if (now < next_process_time_) |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 596 | return 0; |
| 597 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 598 | int64_t diff_ms = now - next_process_time_ + kProcessIntervalMs; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 599 | next_process_time_ = now + kProcessIntervalMs; |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 600 | ++num_process_times_; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 601 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 602 | rsd_->Process(now); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 603 | capture_queue_delay_->CalculateDelayChange(diff_ms); |
| 604 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 605 | if (num_process_times_ <= options_.min_process_count) { |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 609 | if (IsOverusing()) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 610 | // If the last thing we did was going up, and now have to back down, we need |
| 611 | // to check if this peak was short. If so we should back off to avoid going |
| 612 | // back and forth between this load, the system doesn't seem to handle it. |
| 613 | bool check_for_backoff = last_rampup_time_ > last_overuse_time_; |
| 614 | if (check_for_backoff) { |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 615 | if (now - last_rampup_time_ < kStandardRampUpDelayMs || |
| 616 | num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 617 | // Going up was not ok for very long, back off. |
| 618 | current_rampup_delay_ms_ *= kRampUpBackoffFactor; |
| 619 | if (current_rampup_delay_ms_ > kMaxRampUpDelayMs) |
| 620 | current_rampup_delay_ms_ = kMaxRampUpDelayMs; |
| 621 | } else { |
| 622 | // Not currently backing off, reset rampup delay. |
| 623 | current_rampup_delay_ms_ = kStandardRampUpDelayMs; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | last_overuse_time_ = now; |
| 628 | in_quick_rampup_ = false; |
| 629 | checks_above_threshold_ = 0; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 630 | ++num_overuse_detections_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 631 | |
| 632 | if (observer_ != NULL) |
| 633 | observer_->OveruseDetected(); |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 634 | } else if (IsUnderusing(now)) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 635 | last_rampup_time_ = now; |
| 636 | in_quick_rampup_ = true; |
| 637 | |
| 638 | if (observer_ != NULL) |
| 639 | observer_->NormalUsage(); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 640 | } |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 641 | |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 642 | int rampup_delay = |
| 643 | in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 644 | LOG(LS_VERBOSE) << " Frame stats: capture avg: " << capture_deltas_.Mean() |
| 645 | << " capture stddev " << capture_deltas_.StdDev() |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 646 | << " encode usage " << usage_->Value() |
| 647 | << " encode rsd " << rsd_->Value() |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 648 | << " overuse detections " << num_overuse_detections_ |
| 649 | << " rampup delay " << rampup_delay; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | bool OveruseFrameDetector::IsOverusing() { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 654 | bool overusing = false; |
| 655 | if (options_.enable_capture_jitter_method) { |
| 656 | overusing = capture_deltas_.StdDev() >= |
| 657 | options_.high_capture_jitter_threshold_ms; |
| 658 | } else if (options_.enable_encode_usage_method) { |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 659 | bool usage_overuse = |
| 660 | usage_->Value() >= options_.high_encode_usage_threshold_percent; |
| 661 | bool rsd_overuse = false; |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 662 | if (options_.high_encode_time_rsd_threshold > 0) { |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 663 | rsd_overuse = (rsd_->Value() >= options_.high_encode_time_rsd_threshold); |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 664 | } |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 665 | overusing = usage_overuse || rsd_overuse; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 666 | } |
| 667 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 668 | if (overusing) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 669 | ++checks_above_threshold_; |
| 670 | } else { |
| 671 | checks_above_threshold_ = 0; |
| 672 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 673 | return checks_above_threshold_ >= options_.high_threshold_consecutive_count; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | bool OveruseFrameDetector::IsUnderusing(int64_t time_now) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 677 | int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
| 678 | if (time_now < last_rampup_time_ + delay) |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 679 | return false; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 680 | |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 681 | bool underusing = false; |
| 682 | if (options_.enable_capture_jitter_method) { |
| 683 | underusing = capture_deltas_.StdDev() < |
| 684 | options_.low_capture_jitter_threshold_ms; |
| 685 | } else if (options_.enable_encode_usage_method) { |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 686 | bool usage_underuse = |
| 687 | usage_->Value() < options_.low_encode_usage_threshold_percent; |
| 688 | bool rsd_underuse = true; |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 689 | if (options_.low_encode_time_rsd_threshold > 0) { |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 690 | rsd_underuse = (rsd_->Value() < options_.low_encode_time_rsd_threshold); |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 691 | } |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame^] | 692 | underusing = usage_underuse && rsd_underuse; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 693 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 694 | return underusing; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 695 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 696 | } // namespace webrtc |