mflodman@webrtc.org | f8f91d6 | 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 | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 13 | #include <assert.h> |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 14 | #include <math.h> |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 15 | |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <list> |
| 18 | |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/video_coding/utility/include/exp_filter.h" |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/interface/clock.h" |
| 21 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 22 | #include "webrtc/system_wrappers/interface/trace.h" |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 26 | // TODO(mflodman) Test different values for all of these to trigger correctly, |
| 27 | // avoid fluctuations etc. |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 28 | namespace { |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 29 | const int64_t kProcessIntervalMs = 5000; |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 30 | |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 31 | // Weight factor to apply to the standard deviation. |
| 32 | const float kWeightFactor = 0.997f; |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 33 | // Weight factor to apply to the average. |
| 34 | const float kWeightFactorMean = 0.98f; |
| 35 | |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 36 | // Delay between consecutive rampups. (Used for quick recovery.) |
| 37 | const int kQuickRampUpDelayMs = 10 * 1000; |
| 38 | // Delay between rampup attempts. Initially uses standard, scales up to max. |
| 39 | const int kStandardRampUpDelayMs = 30 * 1000; |
| 40 | const int kMaxRampUpDelayMs = 120 * 1000; |
| 41 | // Expontential back-off factor, to prevent annoying up-down behaviour. |
| 42 | const double kRampUpBackoffFactor = 2.0; |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 43 | |
asapersson@webrtc.org | dd4f866 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 44 | // The initial average encode time (set to a fairly small value). |
| 45 | const float kInitialAvgEncodeTimeMs = 5.0f; |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 46 | |
| 47 | // The maximum exponent to use in VCMExpFilter. |
| 48 | const float kSampleDiffMs = 33.0f; |
| 49 | const float kMaxExp = 7.0f; |
| 50 | |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 51 | } // namespace |
| 52 | |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 53 | Statistics::Statistics() : |
| 54 | sum_(0.0), |
| 55 | count_(0), |
| 56 | filtered_samples_(new VCMExpFilter(kWeightFactorMean)), |
| 57 | filtered_variance_(new VCMExpFilter(kWeightFactor)) { |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 58 | Reset(); |
| 59 | } |
| 60 | |
| 61 | void Statistics::SetOptions(const CpuOveruseOptions& options) { |
| 62 | options_ = options; |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void Statistics::Reset() { |
| 66 | sum_ = 0.0; |
| 67 | count_ = 0; |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 68 | filtered_variance_->Reset(kWeightFactor); |
| 69 | filtered_variance_->Apply(1.0f, InitialVariance()); |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void Statistics::AddSample(float sample_ms) { |
| 73 | sum_ += sample_ms; |
| 74 | ++count_; |
| 75 | |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 76 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 77 | // Initialize filtered samples. |
| 78 | filtered_samples_->Reset(kWeightFactorMean); |
| 79 | filtered_samples_->Apply(1.0f, InitialMean()); |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 83 | float exp = sample_ms / kSampleDiffMs; |
| 84 | exp = std::min(exp, kMaxExp); |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 85 | filtered_samples_->Apply(exp, sample_ms); |
| 86 | filtered_variance_->Apply(exp, (sample_ms - filtered_samples_->Value()) * |
| 87 | (sample_ms - filtered_samples_->Value())); |
| 88 | } |
| 89 | |
| 90 | float Statistics::InitialMean() const { |
| 91 | if (count_ == 0) |
| 92 | return 0.0; |
| 93 | return sum_ / count_; |
| 94 | } |
| 95 | |
| 96 | float Statistics::InitialVariance() const { |
| 97 | // Start in between the underuse and overuse threshold. |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 98 | float average_stddev = (options_.low_capture_jitter_threshold_ms + |
| 99 | options_.high_capture_jitter_threshold_ms) / 2.0f; |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 100 | return average_stddev * average_stddev; |
| 101 | } |
| 102 | |
| 103 | float Statistics::Mean() const { return filtered_samples_->Value(); } |
| 104 | |
| 105 | float Statistics::StdDev() const { |
| 106 | return sqrt(std::max(filtered_variance_->Value(), 0.0f)); |
| 107 | } |
| 108 | |
| 109 | uint64_t Statistics::Count() const { return count_; } |
| 110 | |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 111 | |
| 112 | // Class for calculating the average encode time. |
| 113 | class OveruseFrameDetector::EncodeTimeAvg { |
| 114 | public: |
| 115 | EncodeTimeAvg() |
| 116 | : kWeightFactor(0.5f), |
| 117 | filtered_encode_time_ms_(new VCMExpFilter(kWeightFactor)) { |
| 118 | filtered_encode_time_ms_->Apply(1.0f, kInitialAvgEncodeTimeMs); |
| 119 | } |
| 120 | ~EncodeTimeAvg() {} |
| 121 | |
| 122 | void AddEncodeSample(float encode_time_ms, int64_t diff_last_sample_ms) { |
| 123 | float exp = diff_last_sample_ms / kSampleDiffMs; |
| 124 | exp = std::min(exp, kMaxExp); |
| 125 | filtered_encode_time_ms_->Apply(exp, encode_time_ms); |
| 126 | } |
| 127 | |
| 128 | int filtered_encode_time_ms() const { |
| 129 | return static_cast<int>(filtered_encode_time_ms_->Value() + 0.5); |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | const float kWeightFactor; |
| 134 | scoped_ptr<VCMExpFilter> filtered_encode_time_ms_; |
| 135 | }; |
| 136 | |
| 137 | // Class for calculating the encode usage. |
| 138 | class OveruseFrameDetector::EncodeUsage { |
| 139 | public: |
| 140 | EncodeUsage() |
| 141 | : kWeightFactorFrameDiff(0.998f), |
| 142 | kWeightFactorEncodeTime(0.995f), |
| 143 | filtered_encode_time_ms_(new VCMExpFilter(kWeightFactorEncodeTime)), |
| 144 | filtered_frame_diff_ms_(new VCMExpFilter(kWeightFactorFrameDiff)) { |
| 145 | filtered_encode_time_ms_->Apply(1.0f, kInitialAvgEncodeTimeMs); |
| 146 | filtered_frame_diff_ms_->Apply(1.0f, kSampleDiffMs); |
| 147 | } |
| 148 | ~EncodeUsage() {} |
| 149 | |
| 150 | void AddSample(float sample_ms) { |
| 151 | float exp = sample_ms / kSampleDiffMs; |
| 152 | exp = std::min(exp, kMaxExp); |
| 153 | filtered_frame_diff_ms_->Apply(exp, sample_ms); |
| 154 | } |
| 155 | |
| 156 | void AddEncodeSample(float encode_time_ms, int64_t diff_last_sample_ms) { |
| 157 | float exp = diff_last_sample_ms / kSampleDiffMs; |
| 158 | exp = std::min(exp, kMaxExp); |
| 159 | filtered_encode_time_ms_->Apply(exp, encode_time_ms); |
| 160 | } |
| 161 | |
| 162 | int UsageInPercent() const { |
| 163 | float frame_diff_ms = std::max(filtered_frame_diff_ms_->Value(), 1.0f); |
| 164 | float encode_usage_percent = |
| 165 | 100.0f * filtered_encode_time_ms_->Value() / frame_diff_ms; |
| 166 | return static_cast<int>(encode_usage_percent + 0.5); |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | const float kWeightFactorFrameDiff; |
| 171 | const float kWeightFactorEncodeTime; |
| 172 | scoped_ptr<VCMExpFilter> filtered_encode_time_ms_; |
| 173 | scoped_ptr<VCMExpFilter> filtered_frame_diff_ms_; |
| 174 | }; |
| 175 | |
| 176 | // Class for calculating the capture queue delay change. |
| 177 | class OveruseFrameDetector::CaptureQueueDelay { |
| 178 | public: |
| 179 | CaptureQueueDelay() |
| 180 | : kWeightFactor(0.5f), |
| 181 | delay_ms_(0), |
| 182 | filtered_delay_ms_per_s_(new VCMExpFilter(kWeightFactor)) { |
| 183 | filtered_delay_ms_per_s_->Apply(1.0f, 0.0f); |
| 184 | } |
| 185 | ~CaptureQueueDelay() {} |
| 186 | |
| 187 | void FrameCaptured(int64_t now) { |
| 188 | const size_t kMaxSize = 200; |
| 189 | if (frames_.size() > kMaxSize) { |
| 190 | frames_.pop_front(); |
| 191 | } |
| 192 | frames_.push_back(now); |
| 193 | } |
| 194 | |
| 195 | void FrameProcessingStarted(int64_t now) { |
| 196 | if (frames_.empty()) { |
| 197 | return; |
| 198 | } |
| 199 | delay_ms_ = now - frames_.front(); |
| 200 | frames_.pop_front(); |
| 201 | } |
| 202 | |
| 203 | void CalculateDelayChange(int64_t diff_last_sample_ms) { |
| 204 | if (diff_last_sample_ms <= 0) { |
| 205 | return; |
| 206 | } |
| 207 | float exp = static_cast<float>(diff_last_sample_ms) / kProcessIntervalMs; |
| 208 | exp = std::min(exp, kMaxExp); |
| 209 | filtered_delay_ms_per_s_->Apply(exp, |
| 210 | delay_ms_ * 1000.0f / diff_last_sample_ms); |
| 211 | ClearFrames(); |
| 212 | } |
| 213 | |
| 214 | void ClearFrames() { |
| 215 | frames_.clear(); |
| 216 | } |
| 217 | |
| 218 | int delay_ms() const { |
| 219 | return delay_ms_; |
| 220 | } |
| 221 | |
| 222 | int filtered_delay_ms_per_s() const { |
| 223 | return static_cast<int>(filtered_delay_ms_per_s_->Value() + 0.5); |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | const float kWeightFactor; |
| 228 | std::list<int64_t> frames_; |
| 229 | int delay_ms_; |
| 230 | scoped_ptr<VCMExpFilter> filtered_delay_ms_per_s_; |
| 231 | }; |
| 232 | |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 233 | OveruseFrameDetector::OveruseFrameDetector(Clock* clock) |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 234 | : crit_(CriticalSectionWrapper::CreateCriticalSection()), |
mflodman@webrtc.org | bf76ae2 | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 235 | observer_(NULL), |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 236 | clock_(clock), |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 237 | next_process_time_(clock_->TimeInMilliseconds()), |
asapersson@webrtc.org | 9950796 | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 238 | num_process_times_(0), |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 239 | last_capture_time_(0), |
| 240 | last_overuse_time_(0), |
| 241 | checks_above_threshold_(0), |
| 242 | last_rampup_time_(0), |
| 243 | in_quick_rampup_(false), |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 244 | current_rampup_delay_ms_(kStandardRampUpDelayMs), |
asapersson@webrtc.org | 4747585 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 245 | num_pixels_(0), |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 246 | last_encode_sample_ms_(0), |
| 247 | encode_time_(new EncodeTimeAvg()), |
| 248 | encode_usage_(new EncodeUsage()), |
| 249 | capture_queue_delay_(new CaptureQueueDelay()) { |
| 250 | } |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 251 | |
| 252 | OveruseFrameDetector::~OveruseFrameDetector() { |
| 253 | } |
| 254 | |
mflodman@webrtc.org | bf76ae2 | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 255 | void OveruseFrameDetector::SetObserver(CpuOveruseObserver* observer) { |
| 256 | CriticalSectionScoped cs(crit_.get()); |
| 257 | observer_ = observer; |
| 258 | } |
| 259 | |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 260 | void OveruseFrameDetector::SetOptions(const CpuOveruseOptions& options) { |
| 261 | assert(options.min_frame_samples > 0); |
| 262 | CriticalSectionScoped cs(crit_.get()); |
| 263 | if (options_.Equals(options)) { |
| 264 | return; |
| 265 | } |
| 266 | options_ = options; |
| 267 | capture_deltas_.SetOptions(options); |
| 268 | ResetAll(num_pixels_); |
| 269 | } |
| 270 | |
| 271 | int OveruseFrameDetector::CaptureJitterMs() const { |
| 272 | CriticalSectionScoped cs(crit_.get()); |
| 273 | return static_cast<int>(capture_deltas_.StdDev() + 0.5); |
| 274 | } |
| 275 | |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 276 | int OveruseFrameDetector::AvgEncodeTimeMs() const { |
| 277 | CriticalSectionScoped cs(crit_.get()); |
| 278 | return encode_time_->filtered_encode_time_ms(); |
| 279 | } |
| 280 | |
| 281 | int OveruseFrameDetector::EncodeUsagePercent() const { |
| 282 | CriticalSectionScoped cs(crit_.get()); |
| 283 | return encode_usage_->UsageInPercent(); |
| 284 | } |
| 285 | |
| 286 | int OveruseFrameDetector::AvgCaptureQueueDelayMsPerS() const { |
| 287 | CriticalSectionScoped cs(crit_.get()); |
| 288 | return capture_queue_delay_->filtered_delay_ms_per_s(); |
| 289 | } |
| 290 | |
| 291 | int OveruseFrameDetector::CaptureQueueDelayMsPerS() const { |
| 292 | CriticalSectionScoped cs(crit_.get()); |
| 293 | return capture_queue_delay_->delay_ms(); |
| 294 | } |
| 295 | |
| 296 | int32_t OveruseFrameDetector::TimeUntilNextProcess() { |
| 297 | CriticalSectionScoped cs(crit_.get()); |
| 298 | return next_process_time_ - clock_->TimeInMilliseconds(); |
| 299 | } |
| 300 | |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 301 | bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const { |
| 302 | if (num_pixels != num_pixels_) { |
| 303 | return true; |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const { |
asapersson@webrtc.org | 9950796 | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 309 | if (last_capture_time_ == 0) { |
| 310 | return false; |
| 311 | } |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 312 | return (now - last_capture_time_) > options_.frame_timeout_interval_ms; |
| 313 | } |
| 314 | |
| 315 | void OveruseFrameDetector::ResetAll(int num_pixels) { |
| 316 | num_pixels_ = num_pixels; |
| 317 | capture_deltas_.Reset(); |
| 318 | capture_queue_delay_->ClearFrames(); |
| 319 | last_capture_time_ = 0; |
| 320 | num_process_times_ = 0; |
asapersson@webrtc.org | 9950796 | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 321 | } |
| 322 | |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 323 | void OveruseFrameDetector::FrameCaptured(int width, int height) { |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 324 | CriticalSectionScoped cs(crit_.get()); |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 325 | |
asapersson@webrtc.org | 9950796 | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 326 | int64_t now = clock_->TimeInMilliseconds(); |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 327 | if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) { |
| 328 | ResetAll(width * height); |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 329 | } |
| 330 | |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 331 | if (last_capture_time_ != 0) { |
asapersson@webrtc.org | 9950796 | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 332 | capture_deltas_.AddSample(now - last_capture_time_); |
| 333 | encode_usage_->AddSample(now - last_capture_time_); |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 334 | } |
asapersson@webrtc.org | 9950796 | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 335 | last_capture_time_ = now; |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 336 | |
asapersson@webrtc.org | 9950796 | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 337 | capture_queue_delay_->FrameCaptured(now); |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void OveruseFrameDetector::FrameProcessingStarted() { |
| 341 | CriticalSectionScoped cs(crit_.get()); |
| 342 | capture_queue_delay_->FrameProcessingStarted(clock_->TimeInMilliseconds()); |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 343 | } |
| 344 | |
asapersson@webrtc.org | dd4f866 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 345 | void OveruseFrameDetector::FrameEncoded(int encode_time_ms) { |
asapersson@webrtc.org | 4747585 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 346 | CriticalSectionScoped cs(crit_.get()); |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 347 | int64_t time = clock_->TimeInMilliseconds(); |
| 348 | if (last_encode_sample_ms_ != 0) { |
| 349 | int64_t diff_ms = time - last_encode_sample_ms_; |
| 350 | encode_time_->AddEncodeSample(encode_time_ms, diff_ms); |
| 351 | encode_usage_->AddEncodeSample(encode_time_ms, diff_ms); |
| 352 | } |
| 353 | last_encode_sample_ms_ = time; |
asapersson@webrtc.org | dd4f866 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 354 | } |
| 355 | |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 356 | int32_t OveruseFrameDetector::Process() { |
| 357 | CriticalSectionScoped cs(crit_.get()); |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 358 | |
mflodman@webrtc.org | bf76ae2 | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 359 | int64_t now = clock_->TimeInMilliseconds(); |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 360 | |
| 361 | // Used to protect against Process() being called too often. |
| 362 | if (now < next_process_time_) |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 363 | return 0; |
| 364 | |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 365 | int64_t diff_ms = now - next_process_time_ + kProcessIntervalMs; |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 366 | next_process_time_ = now + kProcessIntervalMs; |
asapersson@webrtc.org | 9950796 | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 367 | ++num_process_times_; |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 368 | |
asapersson@webrtc.org | f1630b1 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 369 | capture_queue_delay_->CalculateDelayChange(diff_ms); |
| 370 | |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 371 | if (num_process_times_ <= options_.min_process_count) { |
asapersson@webrtc.org | 9950796 | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 375 | if (IsOverusing()) { |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 376 | // If the last thing we did was going up, and now have to back down, we need |
| 377 | // to check if this peak was short. If so we should back off to avoid going |
| 378 | // back and forth between this load, the system doesn't seem to handle it. |
| 379 | bool check_for_backoff = last_rampup_time_ > last_overuse_time_; |
| 380 | if (check_for_backoff) { |
| 381 | if (now - last_rampup_time_ < kStandardRampUpDelayMs) { |
| 382 | // Going up was not ok for very long, back off. |
| 383 | current_rampup_delay_ms_ *= kRampUpBackoffFactor; |
| 384 | if (current_rampup_delay_ms_ > kMaxRampUpDelayMs) |
| 385 | current_rampup_delay_ms_ = kMaxRampUpDelayMs; |
| 386 | } else { |
| 387 | // Not currently backing off, reset rampup delay. |
| 388 | current_rampup_delay_ms_ = kStandardRampUpDelayMs; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | last_overuse_time_ = now; |
| 393 | in_quick_rampup_ = false; |
| 394 | checks_above_threshold_ = 0; |
| 395 | |
| 396 | if (observer_ != NULL) |
| 397 | observer_->OveruseDetected(); |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 398 | } else if (IsUnderusing(now)) { |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 399 | last_rampup_time_ = now; |
| 400 | in_quick_rampup_ = true; |
| 401 | |
| 402 | if (observer_ != NULL) |
| 403 | observer_->NormalUsage(); |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 404 | } |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 405 | |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 406 | WEBRTC_TRACE( |
| 407 | webrtc::kTraceInfo, |
| 408 | webrtc::kTraceVideo, |
| 409 | -1, |
| 410 | "Capture input stats: avg: %.2fms, std_dev: %.2fms (rampup delay: " |
| 411 | "%dms, overuse: >=%.2fms, " |
| 412 | "underuse: <%.2fms)", |
| 413 | capture_deltas_.Mean(), |
| 414 | capture_deltas_.StdDev(), |
| 415 | in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_, |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 416 | options_.high_capture_jitter_threshold_ms, |
| 417 | options_.low_capture_jitter_threshold_ms); |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 418 | |
asapersson@webrtc.org | ae14504 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | bool OveruseFrameDetector::IsOverusing() { |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 423 | bool overusing = options_.enable_capture_jitter_method && |
| 424 | (capture_deltas_.StdDev() >= options_.high_capture_jitter_threshold_ms); |
| 425 | if (overusing) { |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 426 | ++checks_above_threshold_; |
| 427 | } else { |
| 428 | checks_above_threshold_ = 0; |
| 429 | } |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 430 | return checks_above_threshold_ >= options_.high_threshold_consecutive_count; |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | bool OveruseFrameDetector::IsUnderusing(int64_t time_now) { |
pbos@webrtc.org | ce9de71 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 434 | int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
| 435 | if (time_now < last_rampup_time_ + delay) |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 436 | return false; |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 437 | |
asapersson@webrtc.org | 9da327c | 2014-03-20 13:15:01 +0000 | [diff] [blame^] | 438 | bool underusing = options_.enable_capture_jitter_method && |
| 439 | (capture_deltas_.StdDev() < options_.low_capture_jitter_threshold_ms); |
| 440 | return underusing; |
mflodman@webrtc.org | cb9a72b | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 441 | } |
mflodman@webrtc.org | f8f91d6 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 442 | } // namespace webrtc |