blob: ac9519f323e4bac1383b7fc47fad876f50b946d4 [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;
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +000041const int kMaxRampUpDelayMs = 240 * 1000;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000042// 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
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000129 int Value() const {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000130 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
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000178 int Value() 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.org2881ab12014-06-12 08:46:46 +0000189 private:
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000190 float InitialUsageInPercent() const {
191 // Start in between the underuse and overuse threshold.
192 return (options_.low_encode_usage_threshold_percent +
193 options_.high_encode_usage_threshold_percent) / 2.0f;
194 }
195
196 float InitialEncodeTimeMs() const {
197 return InitialUsageInPercent() * kInitialSampleDiffMs / 100;
198 }
199
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000200 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
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000298 private:
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000299 float InitialValue() const {
300 // Start in between the underuse and overuse threshold.
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000301 return std::max(((options_.low_encode_time_rsd_threshold +
302 options_.high_encode_time_rsd_threshold) / 2.0f), 0.0f);
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000303 }
304
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000305 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
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000361 int Value() const {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000362 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);
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000422 metrics->avg_encode_time_ms = encode_time_->Value();
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000423 metrics->encode_rsd = encode_rsd_->Value();
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000424 metrics->encode_usage_percent = encode_usage_->Value();
425 metrics->capture_queue_delay_ms_per_s = capture_queue_delay_->Value();
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000426}
427
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000428int32_t OveruseFrameDetector::TimeUntilNextProcess() {
429 CriticalSectionScoped cs(crit_.get());
430 return next_process_time_ - clock_->TimeInMilliseconds();
431}
432
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000433bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const {
434 if (num_pixels != num_pixels_) {
435 return true;
436 }
437 return false;
438}
439
440bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000441 if (last_capture_time_ == 0) {
442 return false;
443 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000444 return (now - last_capture_time_) > options_.frame_timeout_interval_ms;
445}
446
447void OveruseFrameDetector::ResetAll(int num_pixels) {
448 num_pixels_ = num_pixels;
449 capture_deltas_.Reset();
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000450 encode_usage_->Reset();
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000451 encode_rsd_->Reset();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000452 capture_queue_delay_->ClearFrames();
453 last_capture_time_ = 0;
454 num_process_times_ = 0;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000455}
456
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000457void OveruseFrameDetector::FrameCaptured(int width, int height) {
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000458 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000459
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000460 int64_t now = clock_->TimeInMilliseconds();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000461 if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) {
462 ResetAll(width * height);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000463 }
464
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000465 if (last_capture_time_ != 0) {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000466 capture_deltas_.AddSample(now - last_capture_time_);
467 encode_usage_->AddSample(now - last_capture_time_);
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000468 }
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000469 last_capture_time_ = now;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000470
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000471 capture_queue_delay_->FrameCaptured(now);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000472}
473
474void OveruseFrameDetector::FrameProcessingStarted() {
475 CriticalSectionScoped cs(crit_.get());
476 capture_queue_delay_->FrameProcessingStarted(clock_->TimeInMilliseconds());
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000477}
478
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000479void OveruseFrameDetector::FrameEncoded(int encode_time_ms) {
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000480 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000481 int64_t time = clock_->TimeInMilliseconds();
482 if (last_encode_sample_ms_ != 0) {
483 int64_t diff_ms = time - last_encode_sample_ms_;
484 encode_time_->AddEncodeSample(encode_time_ms, diff_ms);
485 encode_usage_->AddEncodeSample(encode_time_ms, diff_ms);
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000486 encode_rsd_->AddEncodeSample(encode_time_ms);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000487 }
488 last_encode_sample_ms_ = time;
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000489}
490
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000491int32_t OveruseFrameDetector::Process() {
492 CriticalSectionScoped cs(crit_.get());
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000493
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000494 int64_t now = clock_->TimeInMilliseconds();
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000495
496 // Used to protect against Process() being called too often.
497 if (now < next_process_time_)
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000498 return 0;
499
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000500 int64_t diff_ms = now - next_process_time_ + kProcessIntervalMs;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000501 next_process_time_ = now + kProcessIntervalMs;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000502 ++num_process_times_;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000503
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000504 encode_rsd_->Process(now);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000505 capture_queue_delay_->CalculateDelayChange(diff_ms);
506
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000507 if (num_process_times_ <= options_.min_process_count) {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000508 return 0;
509 }
510
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000511 if (IsOverusing()) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000512 // If the last thing we did was going up, and now have to back down, we need
513 // to check if this peak was short. If so we should back off to avoid going
514 // back and forth between this load, the system doesn't seem to handle it.
515 bool check_for_backoff = last_rampup_time_ > last_overuse_time_;
516 if (check_for_backoff) {
517 if (now - last_rampup_time_ < kStandardRampUpDelayMs) {
518 // Going up was not ok for very long, back off.
519 current_rampup_delay_ms_ *= kRampUpBackoffFactor;
520 if (current_rampup_delay_ms_ > kMaxRampUpDelayMs)
521 current_rampup_delay_ms_ = kMaxRampUpDelayMs;
522 } else {
523 // Not currently backing off, reset rampup delay.
524 current_rampup_delay_ms_ = kStandardRampUpDelayMs;
525 }
526 }
527
528 last_overuse_time_ = now;
529 in_quick_rampup_ = false;
530 checks_above_threshold_ = 0;
531
532 if (observer_ != NULL)
533 observer_->OveruseDetected();
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000534 } else if (IsUnderusing(now)) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000535 last_rampup_time_ = now;
536 in_quick_rampup_ = true;
537
538 if (observer_ != NULL)
539 observer_->NormalUsage();
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000540 }
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000541
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000542 int rampup_delay =
543 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000544 LOG(LS_INFO) << " Frame stats: capture avg: " << capture_deltas_.Mean()
545 << " capture stddev " << capture_deltas_.StdDev()
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000546 << " encode usage " << encode_usage_->Value()
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000547 << " encode rsd " << encode_rsd_->Value()
548 << " rampup delay " << rampup_delay;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000549 return 0;
550}
551
552bool OveruseFrameDetector::IsOverusing() {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000553 bool overusing = false;
554 if (options_.enable_capture_jitter_method) {
555 overusing = capture_deltas_.StdDev() >=
556 options_.high_capture_jitter_threshold_ms;
557 } else if (options_.enable_encode_usage_method) {
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000558 bool encode_usage_overuse =
559 encode_usage_->Value() >= options_.high_encode_usage_threshold_percent;
560 bool encode_rsd_overuse = false;
561 if (options_.high_encode_time_rsd_threshold > 0) {
562 encode_rsd_overuse =
563 (encode_rsd_->Value() >= options_.high_encode_time_rsd_threshold);
564 }
565 overusing = encode_usage_overuse || encode_rsd_overuse;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000566 }
567
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000568 if (overusing) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000569 ++checks_above_threshold_;
570 } else {
571 checks_above_threshold_ = 0;
572 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000573 return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000574}
575
576bool OveruseFrameDetector::IsUnderusing(int64_t time_now) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000577 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
578 if (time_now < last_rampup_time_ + delay)
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000579 return false;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000580
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000581 bool underusing = false;
582 if (options_.enable_capture_jitter_method) {
583 underusing = capture_deltas_.StdDev() <
584 options_.low_capture_jitter_threshold_ms;
585 } else if (options_.enable_encode_usage_method) {
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000586 bool encode_usage_underuse =
587 encode_usage_->Value() < options_.low_encode_usage_threshold_percent;
588 bool encode_rsd_underuse = true;
589 if (options_.low_encode_time_rsd_threshold > 0) {
590 encode_rsd_underuse =
591 (encode_rsd_->Value() < options_.low_encode_time_rsd_threshold);
592 }
593 underusing = encode_usage_underuse && encode_rsd_underuse;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000594 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000595 return underusing;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000596}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000597} // namespace webrtc