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 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/video_coding/utility/include/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. |
| 40 | const int kStandardRampUpDelayMs = 30 * 1000; |
| 41 | const int kMaxRampUpDelayMs = 120 * 1000; |
| 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 | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 45 | // The initial average encode time (set to a fairly small value). |
| 46 | const float kInitialAvgEncodeTimeMs = 5.0f; |
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 | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 54 | Statistics::Statistics() : |
| 55 | sum_(0.0), |
| 56 | count_(0), |
| 57 | filtered_samples_(new VCMExpFilter(kWeightFactorMean)), |
| 58 | filtered_variance_(new VCMExpFilter(kWeightFactor)) { |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 59 | Reset(); |
| 60 | } |
| 61 | |
| 62 | void Statistics::SetOptions(const CpuOveruseOptions& options) { |
| 63 | options_ = options; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void Statistics::Reset() { |
| 67 | sum_ = 0.0; |
| 68 | count_ = 0; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 69 | filtered_variance_->Reset(kWeightFactor); |
| 70 | filtered_variance_->Apply(1.0f, InitialVariance()); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void Statistics::AddSample(float sample_ms) { |
| 74 | sum_ += sample_ms; |
| 75 | ++count_; |
| 76 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 77 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 78 | // Initialize filtered samples. |
| 79 | filtered_samples_->Reset(kWeightFactorMean); |
| 80 | filtered_samples_->Apply(1.0f, InitialMean()); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 84 | float exp = sample_ms / kSampleDiffMs; |
| 85 | exp = std::min(exp, kMaxExp); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 86 | filtered_samples_->Apply(exp, sample_ms); |
| 87 | filtered_variance_->Apply(exp, (sample_ms - filtered_samples_->Value()) * |
| 88 | (sample_ms - filtered_samples_->Value())); |
| 89 | } |
| 90 | |
| 91 | float Statistics::InitialMean() const { |
| 92 | if (count_ == 0) |
| 93 | return 0.0; |
| 94 | return sum_ / count_; |
| 95 | } |
| 96 | |
| 97 | float Statistics::InitialVariance() const { |
| 98 | // Start in between the underuse and overuse threshold. |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 99 | float average_stddev = (options_.low_capture_jitter_threshold_ms + |
| 100 | options_.high_capture_jitter_threshold_ms) / 2.0f; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 101 | return average_stddev * average_stddev; |
| 102 | } |
| 103 | |
| 104 | float Statistics::Mean() const { return filtered_samples_->Value(); } |
| 105 | |
| 106 | float Statistics::StdDev() const { |
| 107 | return sqrt(std::max(filtered_variance_->Value(), 0.0f)); |
| 108 | } |
| 109 | |
| 110 | uint64_t Statistics::Count() const { return count_; } |
| 111 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 112 | |
| 113 | // Class for calculating the average encode time. |
| 114 | class OveruseFrameDetector::EncodeTimeAvg { |
| 115 | public: |
| 116 | EncodeTimeAvg() |
| 117 | : kWeightFactor(0.5f), |
| 118 | filtered_encode_time_ms_(new VCMExpFilter(kWeightFactor)) { |
| 119 | filtered_encode_time_ms_->Apply(1.0f, kInitialAvgEncodeTimeMs); |
| 120 | } |
| 121 | ~EncodeTimeAvg() {} |
| 122 | |
| 123 | void AddEncodeSample(float encode_time_ms, int64_t diff_last_sample_ms) { |
| 124 | float exp = diff_last_sample_ms / kSampleDiffMs; |
| 125 | exp = std::min(exp, kMaxExp); |
| 126 | filtered_encode_time_ms_->Apply(exp, encode_time_ms); |
| 127 | } |
| 128 | |
| 129 | int filtered_encode_time_ms() const { |
| 130 | return static_cast<int>(filtered_encode_time_ms_->Value() + 0.5); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | const float kWeightFactor; |
| 135 | scoped_ptr<VCMExpFilter> filtered_encode_time_ms_; |
| 136 | }; |
| 137 | |
| 138 | // Class for calculating the encode usage. |
| 139 | class OveruseFrameDetector::EncodeUsage { |
| 140 | public: |
| 141 | EncodeUsage() |
| 142 | : kWeightFactorFrameDiff(0.998f), |
| 143 | kWeightFactorEncodeTime(0.995f), |
asapersson@webrtc.org | e41dbee | 2014-05-13 13:45:13 +0000 | [diff] [blame] | 144 | kInitialSampleDiffMs(40.0f), |
| 145 | kMaxSampleDiffMs(45.0f), |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 146 | count_(0), |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 147 | filtered_encode_time_ms_(new VCMExpFilter(kWeightFactorEncodeTime)), |
| 148 | filtered_frame_diff_ms_(new VCMExpFilter(kWeightFactorFrameDiff)) { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 149 | Reset(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 150 | } |
| 151 | ~EncodeUsage() {} |
| 152 | |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 153 | void SetOptions(const CpuOveruseOptions& options) { |
| 154 | options_ = options; |
| 155 | } |
| 156 | |
| 157 | void Reset() { |
| 158 | count_ = 0; |
| 159 | filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff); |
| 160 | filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs); |
| 161 | filtered_encode_time_ms_->Reset(kWeightFactorEncodeTime); |
| 162 | filtered_encode_time_ms_->Apply(1.0f, InitialEncodeTimeMs()); |
| 163 | } |
| 164 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 165 | void AddSample(float sample_ms) { |
| 166 | float exp = sample_ms / kSampleDiffMs; |
| 167 | exp = std::min(exp, kMaxExp); |
| 168 | filtered_frame_diff_ms_->Apply(exp, sample_ms); |
| 169 | } |
| 170 | |
| 171 | void AddEncodeSample(float encode_time_ms, int64_t diff_last_sample_ms) { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 172 | ++count_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 173 | float exp = diff_last_sample_ms / kSampleDiffMs; |
| 174 | exp = std::min(exp, kMaxExp); |
| 175 | filtered_encode_time_ms_->Apply(exp, encode_time_ms); |
| 176 | } |
| 177 | |
| 178 | int UsageInPercent() const { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 179 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
| 180 | return static_cast<int>(InitialUsageInPercent() + 0.5f); |
| 181 | } |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 182 | float frame_diff_ms = std::max(filtered_frame_diff_ms_->Value(), 1.0f); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 183 | frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 184 | float encode_usage_percent = |
| 185 | 100.0f * filtered_encode_time_ms_->Value() / frame_diff_ms; |
| 186 | return static_cast<int>(encode_usage_percent + 0.5); |
| 187 | } |
| 188 | |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 189 | float InitialUsageInPercent() const { |
| 190 | // Start in between the underuse and overuse threshold. |
| 191 | return (options_.low_encode_usage_threshold_percent + |
| 192 | options_.high_encode_usage_threshold_percent) / 2.0f; |
| 193 | } |
| 194 | |
| 195 | float InitialEncodeTimeMs() const { |
| 196 | return InitialUsageInPercent() * kInitialSampleDiffMs / 100; |
| 197 | } |
| 198 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 199 | private: |
| 200 | const float kWeightFactorFrameDiff; |
| 201 | const float kWeightFactorEncodeTime; |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 202 | const float kInitialSampleDiffMs; |
| 203 | const float kMaxSampleDiffMs; |
| 204 | uint64_t count_; |
| 205 | CpuOveruseOptions options_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 206 | scoped_ptr<VCMExpFilter> filtered_encode_time_ms_; |
| 207 | scoped_ptr<VCMExpFilter> filtered_frame_diff_ms_; |
| 208 | }; |
| 209 | |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 210 | // Class for calculating the relative standard deviation of encode times. |
| 211 | class OveruseFrameDetector::EncodeTimeRsd { |
| 212 | public: |
| 213 | EncodeTimeRsd(Clock* clock) |
| 214 | : kWeightFactor(0.6f), |
| 215 | count_(0), |
| 216 | filtered_rsd_(new VCMExpFilter(kWeightFactor)), |
| 217 | hist_samples_(0), |
| 218 | hist_sum_(0.0f), |
| 219 | last_process_time_ms_(clock->TimeInMilliseconds()) { |
| 220 | Reset(); |
| 221 | } |
| 222 | ~EncodeTimeRsd() {} |
| 223 | |
| 224 | void SetOptions(const CpuOveruseOptions& options) { |
| 225 | options_ = options; |
| 226 | } |
| 227 | |
| 228 | void Reset() { |
| 229 | count_ = 0; |
| 230 | filtered_rsd_->Reset(kWeightFactor); |
| 231 | filtered_rsd_->Apply(1.0f, InitialValue()); |
| 232 | hist_.clear(); |
| 233 | hist_samples_ = 0; |
| 234 | hist_sum_ = 0.0f; |
| 235 | } |
| 236 | |
| 237 | void AddEncodeSample(float encode_time_ms) { |
| 238 | int bin = static_cast<int>(encode_time_ms + 0.5f); |
| 239 | if (bin <= 0) { |
| 240 | // The frame was probably not encoded, skip possible dropped frame. |
| 241 | return; |
| 242 | } |
| 243 | ++count_; |
| 244 | ++hist_[bin]; |
| 245 | ++hist_samples_; |
| 246 | hist_sum_ += bin; |
| 247 | } |
| 248 | |
| 249 | void Process(int64_t now) { |
| 250 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
| 251 | // Have not received min number of frames since last reset. |
| 252 | return; |
| 253 | } |
| 254 | const int kMinHistSamples = 20; |
| 255 | if (hist_samples_ < kMinHistSamples) { |
| 256 | return; |
| 257 | } |
| 258 | const int64_t kMinDiffSinceLastProcessMs = 1000; |
| 259 | int64_t diff_last_process_ms = now - last_process_time_ms_; |
| 260 | if (now - last_process_time_ms_ <= kMinDiffSinceLastProcessMs) { |
| 261 | return; |
| 262 | } |
| 263 | last_process_time_ms_ = now; |
| 264 | |
| 265 | // Calculate variance (using samples above the mean). |
| 266 | // Checks for a larger encode time of some frames while there is a small |
| 267 | // increase in the average time. |
| 268 | int mean = hist_sum_ / hist_samples_; |
| 269 | float variance = 0.0f; |
| 270 | int total_count = 0; |
| 271 | for (std::map<int,int>::iterator it = hist_.begin(); |
| 272 | it != hist_.end(); ++it) { |
| 273 | int time = it->first; |
| 274 | int count = it->second; |
| 275 | if (time > mean) { |
| 276 | total_count += count; |
| 277 | for (int i = 0; i < count; ++i) { |
| 278 | variance += ((time - mean) * (time - mean)); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | variance /= std::max(total_count, 1); |
| 283 | float cov = sqrt(variance) / mean; |
| 284 | |
| 285 | hist_.clear(); |
| 286 | hist_samples_ = 0; |
| 287 | hist_sum_ = 0.0f; |
| 288 | |
| 289 | float exp = static_cast<float>(diff_last_process_ms) / kProcessIntervalMs; |
| 290 | exp = std::min(exp, kMaxExp); |
| 291 | filtered_rsd_->Apply(exp, 100.0f * cov); |
| 292 | } |
| 293 | |
| 294 | int Value() const { |
| 295 | return static_cast<int>(filtered_rsd_->Value() + 0.5); |
| 296 | } |
| 297 | |
| 298 | float InitialValue() const { |
| 299 | // Start in between the underuse and overuse threshold. |
| 300 | return (options_.low_encode_time_rsd_threshold + |
| 301 | options_.high_encode_time_rsd_threshold) / 2.0f; |
| 302 | } |
| 303 | |
| 304 | private: |
| 305 | const float kWeightFactor; |
| 306 | uint32_t count_; // Number of encode samples since last reset. |
| 307 | CpuOveruseOptions options_; |
| 308 | scoped_ptr<VCMExpFilter> filtered_rsd_; |
| 309 | int hist_samples_; |
| 310 | float hist_sum_; |
| 311 | std::map<int,int> hist_; // Histogram of encode time of frames. |
| 312 | int64_t last_process_time_ms_; |
| 313 | }; |
| 314 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 315 | // Class for calculating the capture queue delay change. |
| 316 | class OveruseFrameDetector::CaptureQueueDelay { |
| 317 | public: |
| 318 | CaptureQueueDelay() |
| 319 | : kWeightFactor(0.5f), |
| 320 | delay_ms_(0), |
| 321 | filtered_delay_ms_per_s_(new VCMExpFilter(kWeightFactor)) { |
| 322 | filtered_delay_ms_per_s_->Apply(1.0f, 0.0f); |
| 323 | } |
| 324 | ~CaptureQueueDelay() {} |
| 325 | |
| 326 | void FrameCaptured(int64_t now) { |
| 327 | const size_t kMaxSize = 200; |
| 328 | if (frames_.size() > kMaxSize) { |
| 329 | frames_.pop_front(); |
| 330 | } |
| 331 | frames_.push_back(now); |
| 332 | } |
| 333 | |
| 334 | void FrameProcessingStarted(int64_t now) { |
| 335 | if (frames_.empty()) { |
| 336 | return; |
| 337 | } |
| 338 | delay_ms_ = now - frames_.front(); |
| 339 | frames_.pop_front(); |
| 340 | } |
| 341 | |
| 342 | void CalculateDelayChange(int64_t diff_last_sample_ms) { |
| 343 | if (diff_last_sample_ms <= 0) { |
| 344 | return; |
| 345 | } |
| 346 | float exp = static_cast<float>(diff_last_sample_ms) / kProcessIntervalMs; |
| 347 | exp = std::min(exp, kMaxExp); |
| 348 | filtered_delay_ms_per_s_->Apply(exp, |
| 349 | delay_ms_ * 1000.0f / diff_last_sample_ms); |
| 350 | ClearFrames(); |
| 351 | } |
| 352 | |
| 353 | void ClearFrames() { |
| 354 | frames_.clear(); |
| 355 | } |
| 356 | |
| 357 | int delay_ms() const { |
| 358 | return delay_ms_; |
| 359 | } |
| 360 | |
| 361 | int filtered_delay_ms_per_s() const { |
| 362 | return static_cast<int>(filtered_delay_ms_per_s_->Value() + 0.5); |
| 363 | } |
| 364 | |
| 365 | private: |
| 366 | const float kWeightFactor; |
| 367 | std::list<int64_t> frames_; |
| 368 | int delay_ms_; |
| 369 | scoped_ptr<VCMExpFilter> filtered_delay_ms_per_s_; |
| 370 | }; |
| 371 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 372 | OveruseFrameDetector::OveruseFrameDetector(Clock* clock) |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 373 | : crit_(CriticalSectionWrapper::CreateCriticalSection()), |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 374 | observer_(NULL), |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 375 | clock_(clock), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 376 | next_process_time_(clock_->TimeInMilliseconds()), |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 377 | num_process_times_(0), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 378 | last_capture_time_(0), |
| 379 | last_overuse_time_(0), |
| 380 | checks_above_threshold_(0), |
| 381 | last_rampup_time_(0), |
| 382 | in_quick_rampup_(false), |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 383 | current_rampup_delay_ms_(kStandardRampUpDelayMs), |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 384 | num_pixels_(0), |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 385 | last_encode_sample_ms_(0), |
| 386 | encode_time_(new EncodeTimeAvg()), |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 387 | encode_rsd_(new EncodeTimeRsd(clock)), |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 388 | encode_usage_(new EncodeUsage()), |
| 389 | capture_queue_delay_(new CaptureQueueDelay()) { |
| 390 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 391 | |
| 392 | OveruseFrameDetector::~OveruseFrameDetector() { |
| 393 | } |
| 394 | |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 395 | void OveruseFrameDetector::SetObserver(CpuOveruseObserver* observer) { |
| 396 | CriticalSectionScoped cs(crit_.get()); |
| 397 | observer_ = observer; |
| 398 | } |
| 399 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 400 | void OveruseFrameDetector::SetOptions(const CpuOveruseOptions& options) { |
| 401 | assert(options.min_frame_samples > 0); |
| 402 | CriticalSectionScoped cs(crit_.get()); |
| 403 | if (options_.Equals(options)) { |
| 404 | return; |
| 405 | } |
| 406 | options_ = options; |
| 407 | capture_deltas_.SetOptions(options); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 408 | encode_usage_->SetOptions(options); |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 409 | encode_rsd_->SetOptions(options); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 410 | ResetAll(num_pixels_); |
| 411 | } |
| 412 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 413 | int OveruseFrameDetector::CaptureQueueDelayMsPerS() const { |
| 414 | CriticalSectionScoped cs(crit_.get()); |
| 415 | return capture_queue_delay_->delay_ms(); |
| 416 | } |
| 417 | |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 418 | void OveruseFrameDetector::GetCpuOveruseMetrics( |
| 419 | CpuOveruseMetrics* metrics) const { |
| 420 | CriticalSectionScoped cs(crit_.get()); |
| 421 | metrics->capture_jitter_ms = static_cast<int>(capture_deltas_.StdDev() + 0.5); |
| 422 | metrics->avg_encode_time_ms = encode_time_->filtered_encode_time_ms(); |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 423 | metrics->encode_rsd = encode_rsd_->Value(); |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 424 | metrics->encode_usage_percent = encode_usage_->UsageInPercent(); |
| 425 | metrics->capture_queue_delay_ms_per_s = |
| 426 | capture_queue_delay_->filtered_delay_ms_per_s(); |
| 427 | } |
| 428 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 429 | int32_t OveruseFrameDetector::TimeUntilNextProcess() { |
| 430 | CriticalSectionScoped cs(crit_.get()); |
| 431 | return next_process_time_ - clock_->TimeInMilliseconds(); |
| 432 | } |
| 433 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 434 | bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const { |
| 435 | if (num_pixels != num_pixels_) { |
| 436 | return true; |
| 437 | } |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const { |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 442 | if (last_capture_time_ == 0) { |
| 443 | return false; |
| 444 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 445 | return (now - last_capture_time_) > options_.frame_timeout_interval_ms; |
| 446 | } |
| 447 | |
| 448 | void OveruseFrameDetector::ResetAll(int num_pixels) { |
| 449 | num_pixels_ = num_pixels; |
| 450 | capture_deltas_.Reset(); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 451 | encode_usage_->Reset(); |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 452 | encode_rsd_->Reset(); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 453 | capture_queue_delay_->ClearFrames(); |
| 454 | last_capture_time_ = 0; |
| 455 | num_process_times_ = 0; |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 456 | } |
| 457 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 458 | void OveruseFrameDetector::FrameCaptured(int width, int height) { |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 459 | CriticalSectionScoped cs(crit_.get()); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 460 | |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 461 | int64_t now = clock_->TimeInMilliseconds(); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 462 | if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) { |
| 463 | ResetAll(width * height); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 464 | } |
| 465 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 466 | if (last_capture_time_ != 0) { |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 467 | capture_deltas_.AddSample(now - last_capture_time_); |
| 468 | encode_usage_->AddSample(now - last_capture_time_); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 469 | } |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 470 | last_capture_time_ = now; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 471 | |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 472 | capture_queue_delay_->FrameCaptured(now); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | void OveruseFrameDetector::FrameProcessingStarted() { |
| 476 | CriticalSectionScoped cs(crit_.get()); |
| 477 | capture_queue_delay_->FrameProcessingStarted(clock_->TimeInMilliseconds()); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 478 | } |
| 479 | |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 480 | void OveruseFrameDetector::FrameEncoded(int encode_time_ms) { |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 481 | CriticalSectionScoped cs(crit_.get()); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 482 | int64_t time = clock_->TimeInMilliseconds(); |
| 483 | if (last_encode_sample_ms_ != 0) { |
| 484 | int64_t diff_ms = time - last_encode_sample_ms_; |
| 485 | encode_time_->AddEncodeSample(encode_time_ms, diff_ms); |
| 486 | encode_usage_->AddEncodeSample(encode_time_ms, diff_ms); |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 487 | encode_rsd_->AddEncodeSample(encode_time_ms); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 488 | } |
| 489 | last_encode_sample_ms_ = time; |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 490 | } |
| 491 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 492 | int32_t OveruseFrameDetector::Process() { |
| 493 | CriticalSectionScoped cs(crit_.get()); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 494 | |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 495 | int64_t now = clock_->TimeInMilliseconds(); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 496 | |
| 497 | // Used to protect against Process() being called too often. |
| 498 | if (now < next_process_time_) |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 499 | return 0; |
| 500 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 501 | int64_t diff_ms = now - next_process_time_ + kProcessIntervalMs; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 502 | next_process_time_ = now + kProcessIntervalMs; |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 503 | ++num_process_times_; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 504 | |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 505 | encode_rsd_->Process(now); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 506 | capture_queue_delay_->CalculateDelayChange(diff_ms); |
| 507 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 508 | if (num_process_times_ <= options_.min_process_count) { |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 512 | if (IsOverusing()) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 513 | // If the last thing we did was going up, and now have to back down, we need |
| 514 | // to check if this peak was short. If so we should back off to avoid going |
| 515 | // back and forth between this load, the system doesn't seem to handle it. |
| 516 | bool check_for_backoff = last_rampup_time_ > last_overuse_time_; |
| 517 | if (check_for_backoff) { |
| 518 | if (now - last_rampup_time_ < kStandardRampUpDelayMs) { |
| 519 | // Going up was not ok for very long, back off. |
| 520 | current_rampup_delay_ms_ *= kRampUpBackoffFactor; |
| 521 | if (current_rampup_delay_ms_ > kMaxRampUpDelayMs) |
| 522 | current_rampup_delay_ms_ = kMaxRampUpDelayMs; |
| 523 | } else { |
| 524 | // Not currently backing off, reset rampup delay. |
| 525 | current_rampup_delay_ms_ = kStandardRampUpDelayMs; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | last_overuse_time_ = now; |
| 530 | in_quick_rampup_ = false; |
| 531 | checks_above_threshold_ = 0; |
| 532 | |
| 533 | if (observer_ != NULL) |
| 534 | observer_->OveruseDetected(); |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 535 | } else if (IsUnderusing(now)) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 536 | last_rampup_time_ = now; |
| 537 | in_quick_rampup_ = true; |
| 538 | |
| 539 | if (observer_ != NULL) |
| 540 | observer_->NormalUsage(); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 541 | } |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 542 | |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 543 | int rampup_delay = |
| 544 | in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 545 | LOG(LS_INFO) << " Frame stats: capture avg: " << capture_deltas_.Mean() |
| 546 | << " capture stddev " << capture_deltas_.StdDev() |
| 547 | << " encode usage " << encode_usage_->UsageInPercent() |
| 548 | << " encode rsd " << encode_rsd_->Value() |
| 549 | << " rampup delay " << rampup_delay; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | bool OveruseFrameDetector::IsOverusing() { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 554 | bool overusing = false; |
| 555 | if (options_.enable_capture_jitter_method) { |
| 556 | overusing = capture_deltas_.StdDev() >= |
| 557 | options_.high_capture_jitter_threshold_ms; |
| 558 | } else if (options_.enable_encode_usage_method) { |
| 559 | overusing = encode_usage_->UsageInPercent() >= |
| 560 | options_.high_encode_usage_threshold_percent; |
| 561 | } |
| 562 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 563 | if (overusing) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 564 | ++checks_above_threshold_; |
| 565 | } else { |
| 566 | checks_above_threshold_ = 0; |
| 567 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 568 | return checks_above_threshold_ >= options_.high_threshold_consecutive_count; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | bool OveruseFrameDetector::IsUnderusing(int64_t time_now) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 572 | int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
| 573 | if (time_now < last_rampup_time_ + delay) |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 574 | return false; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 575 | |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 576 | bool underusing = false; |
| 577 | if (options_.enable_capture_jitter_method) { |
| 578 | underusing = capture_deltas_.StdDev() < |
| 579 | options_.low_capture_jitter_threshold_ms; |
| 580 | } else if (options_.enable_encode_usage_method) { |
| 581 | underusing = encode_usage_->UsageInPercent() < |
| 582 | options_.low_encode_usage_threshold_percent; |
| 583 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 584 | return underusing; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 585 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 586 | } // namespace webrtc |