blob: 4a95103374bfb0fc570a4df516e48edcd456956d [file] [log] [blame]
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +00001/*
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.orgd4412fe2013-07-31 16:42:21 +000013#include <assert.h>
pbos@webrtc.orga9575702013-08-30 17:16:32 +000014#include <math.h>
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000015
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000016#include <algorithm>
17#include <list>
asapersson@webrtc.org734a5322014-06-10 06:35:22 +000018#include <map>
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000019
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000020#include "webrtc/modules/video_coding/utility/include/exp_filter.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000021#include "webrtc/system_wrappers/interface/clock.h"
22#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +000023#include "webrtc/system_wrappers/interface/logging.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000024
25namespace webrtc {
26
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000027// TODO(mflodman) Test different values for all of these to trigger correctly,
28// avoid fluctuations etc.
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000029namespace {
pbos@webrtc.orga9575702013-08-30 17:16:32 +000030const int64_t kProcessIntervalMs = 5000;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000031
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000032// Weight factor to apply to the standard deviation.
33const float kWeightFactor = 0.997f;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000034// Weight factor to apply to the average.
35const float kWeightFactorMean = 0.98f;
36
pbos@webrtc.orga9575702013-08-30 17:16:32 +000037// Delay between consecutive rampups. (Used for quick recovery.)
38const int kQuickRampUpDelayMs = 10 * 1000;
39// Delay between rampup attempts. Initially uses standard, scales up to max.
40const int kStandardRampUpDelayMs = 30 * 1000;
41const int kMaxRampUpDelayMs = 120 * 1000;
42// Expontential back-off factor, to prevent annoying up-down behaviour.
43const double kRampUpBackoffFactor = 2.0;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000044
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +000045// The initial average encode time (set to a fairly small value).
46const float kInitialAvgEncodeTimeMs = 5.0f;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000047
48// The maximum exponent to use in VCMExpFilter.
49const float kSampleDiffMs = 33.0f;
50const float kMaxExp = 7.0f;
51
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000052} // namespace
53
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000054Statistics::Statistics() :
55 sum_(0.0),
56 count_(0),
57 filtered_samples_(new VCMExpFilter(kWeightFactorMean)),
58 filtered_variance_(new VCMExpFilter(kWeightFactor)) {
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000059 Reset();
60}
61
62void Statistics::SetOptions(const CpuOveruseOptions& options) {
63 options_ = options;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000064}
65
66void Statistics::Reset() {
67 sum_ = 0.0;
68 count_ = 0;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000069 filtered_variance_->Reset(kWeightFactor);
70 filtered_variance_->Apply(1.0f, InitialVariance());
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000071}
72
73void Statistics::AddSample(float sample_ms) {
74 sum_ += sample_ms;
75 ++count_;
76
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000077 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) {
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000078 // Initialize filtered samples.
79 filtered_samples_->Reset(kWeightFactorMean);
80 filtered_samples_->Apply(1.0f, InitialMean());
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000081 return;
82 }
83
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000084 float exp = sample_ms / kSampleDiffMs;
85 exp = std::min(exp, kMaxExp);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000086 filtered_samples_->Apply(exp, sample_ms);
87 filtered_variance_->Apply(exp, (sample_ms - filtered_samples_->Value()) *
88 (sample_ms - filtered_samples_->Value()));
89}
90
91float Statistics::InitialMean() const {
92 if (count_ == 0)
93 return 0.0;
94 return sum_ / count_;
95}
96
97float Statistics::InitialVariance() const {
98 // Start in between the underuse and overuse threshold.
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000099 float average_stddev = (options_.low_capture_jitter_threshold_ms +
100 options_.high_capture_jitter_threshold_ms) / 2.0f;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000101 return average_stddev * average_stddev;
102}
103
104float Statistics::Mean() const { return filtered_samples_->Value(); }
105
106float Statistics::StdDev() const {
107 return sqrt(std::max(filtered_variance_->Value(), 0.0f));
108}
109
110uint64_t Statistics::Count() const { return count_; }
111
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000112
113// Class for calculating the average encode time.
114class 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.
139class OveruseFrameDetector::EncodeUsage {
140 public:
141 EncodeUsage()
142 : kWeightFactorFrameDiff(0.998f),
143 kWeightFactorEncodeTime(0.995f),
asapersson@webrtc.orge41dbee2014-05-13 13:45:13 +0000144 kInitialSampleDiffMs(40.0f),
145 kMaxSampleDiffMs(45.0f),
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000146 count_(0),
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000147 filtered_encode_time_ms_(new VCMExpFilter(kWeightFactorEncodeTime)),
148 filtered_frame_diff_ms_(new VCMExpFilter(kWeightFactorFrameDiff)) {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000149 Reset();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000150 }
151 ~EncodeUsage() {}
152
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000153 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.org9e5b0342013-12-04 13:47:44 +0000165 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.orgce12f1f2014-03-24 21:59:16 +0000172 ++count_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000173 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.orgce12f1f2014-03-24 21:59:16 +0000179 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) {
180 return static_cast<int>(InitialUsageInPercent() + 0.5f);
181 }
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000182 float frame_diff_ms = std::max(filtered_frame_diff_ms_->Value(), 1.0f);
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000183 frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000184 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.orgce12f1f2014-03-24 21:59:16 +0000189 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.org9e5b0342013-12-04 13:47:44 +0000199 private:
200 const float kWeightFactorFrameDiff;
201 const float kWeightFactorEncodeTime;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000202 const float kInitialSampleDiffMs;
203 const float kMaxSampleDiffMs;
204 uint64_t count_;
205 CpuOveruseOptions options_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000206 scoped_ptr<VCMExpFilter> filtered_encode_time_ms_;
207 scoped_ptr<VCMExpFilter> filtered_frame_diff_ms_;
208};
209
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000210// Class for calculating the relative standard deviation of encode times.
211class 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.org9e5b0342013-12-04 13:47:44 +0000315// Class for calculating the capture queue delay change.
316class 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.org8a8c3ef2014-03-20 13:15:01 +0000372OveruseFrameDetector::OveruseFrameDetector(Clock* clock)
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000373 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000374 observer_(NULL),
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000375 clock_(clock),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000376 next_process_time_(clock_->TimeInMilliseconds()),
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000377 num_process_times_(0),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000378 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.orge2af6222013-09-23 20:05:39 +0000383 current_rampup_delay_ms_(kStandardRampUpDelayMs),
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000384 num_pixels_(0),
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000385 last_encode_sample_ms_(0),
386 encode_time_(new EncodeTimeAvg()),
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000387 encode_rsd_(new EncodeTimeRsd(clock)),
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000388 encode_usage_(new EncodeUsage()),
389 capture_queue_delay_(new CaptureQueueDelay()) {
390}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000391
392OveruseFrameDetector::~OveruseFrameDetector() {
393}
394
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000395void OveruseFrameDetector::SetObserver(CpuOveruseObserver* observer) {
396 CriticalSectionScoped cs(crit_.get());
397 observer_ = observer;
398}
399
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000400void 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.orgce12f1f2014-03-24 21:59:16 +0000408 encode_usage_->SetOptions(options);
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000409 encode_rsd_->SetOptions(options);
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000410 ResetAll(num_pixels_);
411}
412
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000413int OveruseFrameDetector::CaptureQueueDelayMsPerS() const {
414 CriticalSectionScoped cs(crit_.get());
415 return capture_queue_delay_->delay_ms();
416}
417
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000418void 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.org734a5322014-06-10 06:35:22 +0000423 metrics->encode_rsd = encode_rsd_->Value();
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000424 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.org9e5b0342013-12-04 13:47:44 +0000429int32_t OveruseFrameDetector::TimeUntilNextProcess() {
430 CriticalSectionScoped cs(crit_.get());
431 return next_process_time_ - clock_->TimeInMilliseconds();
432}
433
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000434bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const {
435 if (num_pixels != num_pixels_) {
436 return true;
437 }
438 return false;
439}
440
441bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000442 if (last_capture_time_ == 0) {
443 return false;
444 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000445 return (now - last_capture_time_) > options_.frame_timeout_interval_ms;
446}
447
448void OveruseFrameDetector::ResetAll(int num_pixels) {
449 num_pixels_ = num_pixels;
450 capture_deltas_.Reset();
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000451 encode_usage_->Reset();
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000452 encode_rsd_->Reset();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000453 capture_queue_delay_->ClearFrames();
454 last_capture_time_ = 0;
455 num_process_times_ = 0;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000456}
457
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000458void OveruseFrameDetector::FrameCaptured(int width, int height) {
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000459 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000460
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000461 int64_t now = clock_->TimeInMilliseconds();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000462 if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) {
463 ResetAll(width * height);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000464 }
465
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000466 if (last_capture_time_ != 0) {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000467 capture_deltas_.AddSample(now - last_capture_time_);
468 encode_usage_->AddSample(now - last_capture_time_);
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000469 }
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000470 last_capture_time_ = now;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000471
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000472 capture_queue_delay_->FrameCaptured(now);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000473}
474
475void OveruseFrameDetector::FrameProcessingStarted() {
476 CriticalSectionScoped cs(crit_.get());
477 capture_queue_delay_->FrameProcessingStarted(clock_->TimeInMilliseconds());
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000478}
479
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000480void OveruseFrameDetector::FrameEncoded(int encode_time_ms) {
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000481 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000482 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.org734a5322014-06-10 06:35:22 +0000487 encode_rsd_->AddEncodeSample(encode_time_ms);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000488 }
489 last_encode_sample_ms_ = time;
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000490}
491
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000492int32_t OveruseFrameDetector::Process() {
493 CriticalSectionScoped cs(crit_.get());
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000494
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000495 int64_t now = clock_->TimeInMilliseconds();
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000496
497 // Used to protect against Process() being called too often.
498 if (now < next_process_time_)
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000499 return 0;
500
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000501 int64_t diff_ms = now - next_process_time_ + kProcessIntervalMs;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000502 next_process_time_ = now + kProcessIntervalMs;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000503 ++num_process_times_;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000504
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000505 encode_rsd_->Process(now);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000506 capture_queue_delay_->CalculateDelayChange(diff_ms);
507
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000508 if (num_process_times_ <= options_.min_process_count) {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000509 return 0;
510 }
511
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000512 if (IsOverusing()) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000513 // 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.orgd4412fe2013-07-31 16:42:21 +0000535 } else if (IsUnderusing(now)) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000536 last_rampup_time_ = now;
537 in_quick_rampup_ = true;
538
539 if (observer_ != NULL)
540 observer_->NormalUsage();
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000541 }
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000542
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000543 int rampup_delay =
544 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000545 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.orge2af6222013-09-23 20:05:39 +0000550 return 0;
551}
552
553bool OveruseFrameDetector::IsOverusing() {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000554 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.org8a8c3ef2014-03-20 13:15:01 +0000563 if (overusing) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000564 ++checks_above_threshold_;
565 } else {
566 checks_above_threshold_ = 0;
567 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000568 return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000569}
570
571bool OveruseFrameDetector::IsUnderusing(int64_t time_now) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000572 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
573 if (time_now < last_rampup_time_ + delay)
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000574 return false;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000575
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000576 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.org8a8c3ef2014-03-20 13:15:01 +0000584 return underusing;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000585}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000586} // namespace webrtc