blob: 9603613209cd4e3cb79d33f646c4393928323683 [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
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000020#include "webrtc/base/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.
asapersson@webrtc.org23a4d852014-08-13 14:33:49 +000040const int kStandardRampUpDelayMs = 40 * 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.orgd9803072014-06-16 14:27:19 +000045// Max number of overuses detected before always applying the rampup delay.
asapersson@webrtc.org23a4d852014-08-13 14:33:49 +000046const int kMaxOverusesBeforeApplyRampupDelay = 4;
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.org9aed0022014-10-16 06:57:12 +000054// TODO(asapersson): Remove this class. Not used.
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000055Statistics::Statistics() :
56 sum_(0.0),
57 count_(0),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000058 filtered_samples_(new rtc::ExpFilter(kWeightFactorMean)),
59 filtered_variance_(new rtc::ExpFilter(kWeightFactor)) {
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000060 Reset();
61}
62
63void Statistics::SetOptions(const CpuOveruseOptions& options) {
64 options_ = options;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000065}
66
67void Statistics::Reset() {
68 sum_ = 0.0;
69 count_ = 0;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000070 filtered_variance_->Reset(kWeightFactor);
71 filtered_variance_->Apply(1.0f, InitialVariance());
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000072}
73
74void Statistics::AddSample(float sample_ms) {
75 sum_ += sample_ms;
76 ++count_;
77
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000078 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) {
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000079 // Initialize filtered samples.
80 filtered_samples_->Reset(kWeightFactorMean);
81 filtered_samples_->Apply(1.0f, InitialMean());
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000082 return;
83 }
84
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000085 float exp = sample_ms / kSampleDiffMs;
86 exp = std::min(exp, kMaxExp);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000087 filtered_samples_->Apply(exp, sample_ms);
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000088 filtered_variance_->Apply(exp, (sample_ms - filtered_samples_->filtered()) *
89 (sample_ms - filtered_samples_->filtered()));
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000090}
91
92float Statistics::InitialMean() const {
93 if (count_ == 0)
94 return 0.0;
95 return sum_ / count_;
96}
97
98float Statistics::InitialVariance() const {
99 // Start in between the underuse and overuse threshold.
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000100 float average_stddev = (options_.low_capture_jitter_threshold_ms +
101 options_.high_capture_jitter_threshold_ms) / 2.0f;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000102 return average_stddev * average_stddev;
103}
104
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000105float Statistics::Mean() const { return filtered_samples_->filtered(); }
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000106
107float Statistics::StdDev() const {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000108 return sqrt(std::max(filtered_variance_->filtered(), 0.0f));
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000109}
110
111uint64_t Statistics::Count() const { return count_; }
112
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000113
114// Class for calculating the average encode time.
115class OveruseFrameDetector::EncodeTimeAvg {
116 public:
117 EncodeTimeAvg()
118 : kWeightFactor(0.5f),
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000119 kInitialAvgEncodeTimeMs(5.0f),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000120 filtered_encode_time_ms_(new rtc::ExpFilter(kWeightFactor)) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000121 filtered_encode_time_ms_->Apply(1.0f, kInitialAvgEncodeTimeMs);
122 }
123 ~EncodeTimeAvg() {}
124
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000125 void AddSample(float encode_time_ms, int64_t diff_last_sample_ms) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000126 float exp = diff_last_sample_ms / kSampleDiffMs;
127 exp = std::min(exp, kMaxExp);
128 filtered_encode_time_ms_->Apply(exp, encode_time_ms);
129 }
130
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000131 int Value() const {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000132 return static_cast<int>(filtered_encode_time_ms_->filtered() + 0.5);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000133 }
134
135 private:
136 const float kWeightFactor;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000137 const float kInitialAvgEncodeTimeMs;
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000138 scoped_ptr<rtc::ExpFilter> filtered_encode_time_ms_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000139};
140
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000141// Class for calculating the processing usage on the send-side (the average
142// processing time of a frame divided by the average time difference between
143// captured frames).
144class OveruseFrameDetector::SendProcessingUsage {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000145 public:
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000146 SendProcessingUsage()
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000147 : kWeightFactorFrameDiff(0.998f),
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000148 kWeightFactorProcessing(0.995f),
asapersson@webrtc.orge41dbee2014-05-13 13:45:13 +0000149 kInitialSampleDiffMs(40.0f),
150 kMaxSampleDiffMs(45.0f),
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000151 count_(0),
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000152 filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000153 filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000154 Reset();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000155 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000156 ~SendProcessingUsage() {}
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000157
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000158 void SetOptions(const CpuOveruseOptions& options) {
159 options_ = options;
160 }
161
162 void Reset() {
163 count_ = 0;
164 filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff);
165 filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000166 filtered_processing_ms_->Reset(kWeightFactorProcessing);
167 filtered_processing_ms_->Apply(1.0f, InitialProcessingMs());
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000168 }
169
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000170 void AddCaptureSample(float sample_ms) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000171 float exp = sample_ms / kSampleDiffMs;
172 exp = std::min(exp, kMaxExp);
173 filtered_frame_diff_ms_->Apply(exp, sample_ms);
174 }
175
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000176 void AddSample(float processing_ms, int64_t diff_last_sample_ms) {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000177 ++count_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000178 float exp = diff_last_sample_ms / kSampleDiffMs;
179 exp = std::min(exp, kMaxExp);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000180 filtered_processing_ms_->Apply(exp, processing_ms);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000181 }
182
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000183 int Value() const {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000184 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) {
185 return static_cast<int>(InitialUsageInPercent() + 0.5f);
186 }
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000187 float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f);
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000188 frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000189 float encode_usage_percent =
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000190 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000191 return static_cast<int>(encode_usage_percent + 0.5);
192 }
193
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000194 private:
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000195 float InitialUsageInPercent() const {
196 // Start in between the underuse and overuse threshold.
197 return (options_.low_encode_usage_threshold_percent +
198 options_.high_encode_usage_threshold_percent) / 2.0f;
199 }
200
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000201 float InitialProcessingMs() const {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000202 return InitialUsageInPercent() * kInitialSampleDiffMs / 100;
203 }
204
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000205 const float kWeightFactorFrameDiff;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000206 const float kWeightFactorProcessing;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000207 const float kInitialSampleDiffMs;
208 const float kMaxSampleDiffMs;
209 uint64_t count_;
210 CpuOveruseOptions options_;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000211 scoped_ptr<rtc::ExpFilter> filtered_processing_ms_;
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000212 scoped_ptr<rtc::ExpFilter> filtered_frame_diff_ms_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000213};
214
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000215// Class for calculating the relative standard deviation of the processing time
216// of frame on the send-side.
217// Currently only used for testing.
218class OveruseFrameDetector::SendProcessingRsd {
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000219 public:
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000220 SendProcessingRsd(Clock* clock)
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000221 : kWeightFactor(0.6f),
222 count_(0),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000223 filtered_rsd_(new rtc::ExpFilter(kWeightFactor)),
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000224 hist_samples_(0),
225 hist_sum_(0.0f),
226 last_process_time_ms_(clock->TimeInMilliseconds()) {
227 Reset();
228 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000229 ~SendProcessingRsd() {}
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000230
231 void SetOptions(const CpuOveruseOptions& options) {
232 options_ = options;
233 }
234
235 void Reset() {
236 count_ = 0;
237 filtered_rsd_->Reset(kWeightFactor);
238 filtered_rsd_->Apply(1.0f, InitialValue());
239 hist_.clear();
240 hist_samples_ = 0;
241 hist_sum_ = 0.0f;
242 }
243
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000244 void AddSample(float processing_ms) {
245 int bin = static_cast<int>(processing_ms + 0.5f);
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000246 if (bin <= 0) {
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000247 return;
248 }
249 ++count_;
250 ++hist_[bin];
251 ++hist_samples_;
252 hist_sum_ += bin;
253 }
254
255 void Process(int64_t now) {
256 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) {
257 // Have not received min number of frames since last reset.
258 return;
259 }
260 const int kMinHistSamples = 20;
261 if (hist_samples_ < kMinHistSamples) {
262 return;
263 }
264 const int64_t kMinDiffSinceLastProcessMs = 1000;
265 int64_t diff_last_process_ms = now - last_process_time_ms_;
266 if (now - last_process_time_ms_ <= kMinDiffSinceLastProcessMs) {
267 return;
268 }
269 last_process_time_ms_ = now;
270
271 // Calculate variance (using samples above the mean).
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000272 // Checks for a larger processing time of some frames while there is a small
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000273 // increase in the average time.
274 int mean = hist_sum_ / hist_samples_;
275 float variance = 0.0f;
276 int total_count = 0;
277 for (std::map<int,int>::iterator it = hist_.begin();
278 it != hist_.end(); ++it) {
279 int time = it->first;
280 int count = it->second;
281 if (time > mean) {
282 total_count += count;
283 for (int i = 0; i < count; ++i) {
284 variance += ((time - mean) * (time - mean));
285 }
286 }
287 }
288 variance /= std::max(total_count, 1);
289 float cov = sqrt(variance) / mean;
290
291 hist_.clear();
292 hist_samples_ = 0;
293 hist_sum_ = 0.0f;
294
295 float exp = static_cast<float>(diff_last_process_ms) / kProcessIntervalMs;
296 exp = std::min(exp, kMaxExp);
297 filtered_rsd_->Apply(exp, 100.0f * cov);
298 }
299
300 int Value() const {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000301 return static_cast<int>(filtered_rsd_->filtered() + 0.5);
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000302 }
303
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000304 private:
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000305 float InitialValue() const {
306 // Start in between the underuse and overuse threshold.
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000307 return std::max(((options_.low_encode_time_rsd_threshold +
308 options_.high_encode_time_rsd_threshold) / 2.0f), 0.0f);
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000309 }
310
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000311 const float kWeightFactor;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000312 uint32_t count_; // Number of samples since last reset.
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000313 CpuOveruseOptions options_;
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000314 scoped_ptr<rtc::ExpFilter> filtered_rsd_;
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000315 int hist_samples_;
316 float hist_sum_;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000317 std::map<int, int> hist_; // Histogram of time spent on processing frames.
asapersson@webrtc.org734a5322014-06-10 06:35:22 +0000318 int64_t last_process_time_ms_;
319};
320
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000321// Class for calculating the processing time of frames.
322class OveruseFrameDetector::FrameQueue {
323 public:
324 FrameQueue() : last_processing_time_ms_(-1) {}
325 ~FrameQueue() {}
326
327 // Called when a frame is captured.
328 // Starts the measuring of the processing time of the frame.
329 void Start(int64_t capture_time, int64_t now) {
330 const size_t kMaxSize = 90; // Allows for processing time of 1.5s at 60fps.
331 if (frame_times_.size() > kMaxSize) {
332 LOG(LS_WARNING) << "Max size reached, removed oldest frame.";
333 frame_times_.erase(frame_times_.begin());
334 }
335 if (frame_times_.find(capture_time) != frame_times_.end()) {
336 // Frame should not exist.
337 assert(false);
338 return;
339 }
340 frame_times_[capture_time] = now;
341 }
342
343 // Called when the processing of a frame has finished.
344 // Returns the processing time of the frame.
345 int End(int64_t capture_time, int64_t now) {
346 std::map<int64_t, int64_t>::iterator it = frame_times_.find(capture_time);
347 if (it == frame_times_.end()) {
348 return -1;
349 }
350 // Remove any old frames up to current.
351 // Old frames have been skipped by the capture process thread.
352 // TODO(asapersson): Consider measuring time from first frame in list.
353 last_processing_time_ms_ = now - (*it).second;
354 frame_times_.erase(frame_times_.begin(), ++it);
355 return last_processing_time_ms_;
356 }
357
358 void Reset() { frame_times_.clear(); }
359 int NumFrames() const { return frame_times_.size(); }
360 int last_processing_time_ms() const { return last_processing_time_ms_; }
361
362 private:
363 // Captured frames mapped by the capture time.
364 std::map<int64_t, int64_t> frame_times_;
365 int last_processing_time_ms_;
366};
367
368// TODO(asapersson): Remove this class. Not used.
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000369// Class for calculating the capture queue delay change.
370class OveruseFrameDetector::CaptureQueueDelay {
371 public:
372 CaptureQueueDelay()
373 : kWeightFactor(0.5f),
374 delay_ms_(0),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000375 filtered_delay_ms_per_s_(new rtc::ExpFilter(kWeightFactor)) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000376 filtered_delay_ms_per_s_->Apply(1.0f, 0.0f);
377 }
378 ~CaptureQueueDelay() {}
379
380 void FrameCaptured(int64_t now) {
381 const size_t kMaxSize = 200;
382 if (frames_.size() > kMaxSize) {
383 frames_.pop_front();
384 }
385 frames_.push_back(now);
386 }
387
388 void FrameProcessingStarted(int64_t now) {
389 if (frames_.empty()) {
390 return;
391 }
392 delay_ms_ = now - frames_.front();
393 frames_.pop_front();
394 }
395
396 void CalculateDelayChange(int64_t diff_last_sample_ms) {
397 if (diff_last_sample_ms <= 0) {
398 return;
399 }
400 float exp = static_cast<float>(diff_last_sample_ms) / kProcessIntervalMs;
401 exp = std::min(exp, kMaxExp);
402 filtered_delay_ms_per_s_->Apply(exp,
403 delay_ms_ * 1000.0f / diff_last_sample_ms);
404 ClearFrames();
405 }
406
407 void ClearFrames() {
408 frames_.clear();
409 }
410
411 int delay_ms() const {
412 return delay_ms_;
413 }
414
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000415 int Value() const {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000416 return static_cast<int>(filtered_delay_ms_per_s_->filtered() + 0.5);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000417 }
418
419 private:
420 const float kWeightFactor;
421 std::list<int64_t> frames_;
422 int delay_ms_;
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000423 scoped_ptr<rtc::ExpFilter> filtered_delay_ms_per_s_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000424};
425
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000426OveruseFrameDetector::OveruseFrameDetector(Clock* clock)
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000427 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000428 observer_(NULL),
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000429 clock_(clock),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000430 next_process_time_(clock_->TimeInMilliseconds()),
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000431 num_process_times_(0),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000432 last_capture_time_(0),
433 last_overuse_time_(0),
434 checks_above_threshold_(0),
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000435 num_overuse_detections_(0),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000436 last_rampup_time_(0),
437 in_quick_rampup_(false),
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000438 current_rampup_delay_ms_(kStandardRampUpDelayMs),
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000439 num_pixels_(0),
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000440 last_encode_sample_ms_(0),
441 encode_time_(new EncodeTimeAvg()),
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000442 rsd_(new SendProcessingRsd(clock)),
443 usage_(new SendProcessingUsage()),
444 frame_queue_(new FrameQueue()),
445 last_sample_time_ms_(0),
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000446 capture_queue_delay_(new CaptureQueueDelay()) {
447}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000448
449OveruseFrameDetector::~OveruseFrameDetector() {
450}
451
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000452void OveruseFrameDetector::SetObserver(CpuOveruseObserver* observer) {
453 CriticalSectionScoped cs(crit_.get());
454 observer_ = observer;
455}
456
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000457void OveruseFrameDetector::SetOptions(const CpuOveruseOptions& options) {
458 assert(options.min_frame_samples > 0);
459 CriticalSectionScoped cs(crit_.get());
460 if (options_.Equals(options)) {
461 return;
462 }
463 options_ = options;
464 capture_deltas_.SetOptions(options);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000465 usage_->SetOptions(options);
466 rsd_->SetOptions(options);
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000467 ResetAll(num_pixels_);
468}
469
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000470int OveruseFrameDetector::CaptureQueueDelayMsPerS() const {
471 CriticalSectionScoped cs(crit_.get());
472 return capture_queue_delay_->delay_ms();
473}
474
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000475int OveruseFrameDetector::LastProcessingTimeMs() const {
476 CriticalSectionScoped cs(crit_.get());
477 return frame_queue_->last_processing_time_ms();
478}
479
480int OveruseFrameDetector::FramesInQueue() const {
481 CriticalSectionScoped cs(crit_.get());
482 return frame_queue_->NumFrames();
483}
484
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000485void OveruseFrameDetector::GetCpuOveruseMetrics(
486 CpuOveruseMetrics* metrics) const {
487 CriticalSectionScoped cs(crit_.get());
488 metrics->capture_jitter_ms = static_cast<int>(capture_deltas_.StdDev() + 0.5);
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000489 metrics->avg_encode_time_ms = encode_time_->Value();
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000490 metrics->encode_rsd = rsd_->Value();
491 metrics->encode_usage_percent = usage_->Value();
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000492 metrics->capture_queue_delay_ms_per_s = capture_queue_delay_->Value();
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000493}
494
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000495int32_t OveruseFrameDetector::TimeUntilNextProcess() {
496 CriticalSectionScoped cs(crit_.get());
497 return next_process_time_ - clock_->TimeInMilliseconds();
498}
499
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000500bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const {
501 if (num_pixels != num_pixels_) {
502 return true;
503 }
504 return false;
505}
506
507bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000508 if (last_capture_time_ == 0) {
509 return false;
510 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000511 return (now - last_capture_time_) > options_.frame_timeout_interval_ms;
512}
513
514void OveruseFrameDetector::ResetAll(int num_pixels) {
515 num_pixels_ = num_pixels;
516 capture_deltas_.Reset();
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000517 usage_->Reset();
518 rsd_->Reset();
519 frame_queue_->Reset();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000520 capture_queue_delay_->ClearFrames();
521 last_capture_time_ = 0;
522 num_process_times_ = 0;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000523}
524
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000525void OveruseFrameDetector::FrameCaptured(int width,
526 int height,
527 int64_t capture_time_ms) {
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000528 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000529
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000530 int64_t now = clock_->TimeInMilliseconds();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000531 if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) {
532 ResetAll(width * height);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000533 }
534
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000535 if (last_capture_time_ != 0) {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000536 capture_deltas_.AddSample(now - last_capture_time_);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000537 usage_->AddCaptureSample(now - last_capture_time_);
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000538 }
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000539 last_capture_time_ = now;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000540
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000541 capture_queue_delay_->FrameCaptured(now);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000542
543 if (options_.enable_extended_processing_usage) {
544 frame_queue_->Start(capture_time_ms, now);
545 }
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000546}
547
548void OveruseFrameDetector::FrameProcessingStarted() {
549 CriticalSectionScoped cs(crit_.get());
550 capture_queue_delay_->FrameProcessingStarted(clock_->TimeInMilliseconds());
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000551}
552
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000553void OveruseFrameDetector::FrameEncoded(int encode_time_ms) {
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000554 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000555 int64_t now = clock_->TimeInMilliseconds();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000556 if (last_encode_sample_ms_ != 0) {
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000557 int64_t diff_ms = now - last_encode_sample_ms_;
558 encode_time_->AddSample(encode_time_ms, diff_ms);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000559 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000560 last_encode_sample_ms_ = now;
561
562 if (!options_.enable_extended_processing_usage) {
563 AddProcessingTime(encode_time_ms);
564 }
565}
566
567void OveruseFrameDetector::FrameSent(int64_t capture_time_ms) {
568 CriticalSectionScoped cs(crit_.get());
569 if (!options_.enable_extended_processing_usage) {
570 return;
571 }
572 int delay_ms = frame_queue_->End(capture_time_ms,
573 clock_->TimeInMilliseconds());
574 if (delay_ms > 0) {
575 AddProcessingTime(delay_ms);
576 }
577}
578
579void OveruseFrameDetector::AddProcessingTime(int elapsed_ms) {
580 int64_t now = clock_->TimeInMilliseconds();
581 if (last_sample_time_ms_ != 0) {
582 int64_t diff_ms = now - last_sample_time_ms_;
583 usage_->AddSample(elapsed_ms, diff_ms);
584 rsd_->AddSample(elapsed_ms);
585 }
586 last_sample_time_ms_ = now;
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000587}
588
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000589int32_t OveruseFrameDetector::Process() {
590 CriticalSectionScoped cs(crit_.get());
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000591
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000592 int64_t now = clock_->TimeInMilliseconds();
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000593
594 // Used to protect against Process() being called too often.
595 if (now < next_process_time_)
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000596 return 0;
597
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000598 int64_t diff_ms = now - next_process_time_ + kProcessIntervalMs;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000599 next_process_time_ = now + kProcessIntervalMs;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000600 ++num_process_times_;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000601
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000602 rsd_->Process(now);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000603 capture_queue_delay_->CalculateDelayChange(diff_ms);
604
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000605 if (num_process_times_ <= options_.min_process_count) {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000606 return 0;
607 }
608
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000609 if (IsOverusing()) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000610 // If the last thing we did was going up, and now have to back down, we need
611 // to check if this peak was short. If so we should back off to avoid going
612 // back and forth between this load, the system doesn't seem to handle it.
613 bool check_for_backoff = last_rampup_time_ > last_overuse_time_;
614 if (check_for_backoff) {
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000615 if (now - last_rampup_time_ < kStandardRampUpDelayMs ||
616 num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000617 // Going up was not ok for very long, back off.
618 current_rampup_delay_ms_ *= kRampUpBackoffFactor;
619 if (current_rampup_delay_ms_ > kMaxRampUpDelayMs)
620 current_rampup_delay_ms_ = kMaxRampUpDelayMs;
621 } else {
622 // Not currently backing off, reset rampup delay.
623 current_rampup_delay_ms_ = kStandardRampUpDelayMs;
624 }
625 }
626
627 last_overuse_time_ = now;
628 in_quick_rampup_ = false;
629 checks_above_threshold_ = 0;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000630 ++num_overuse_detections_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000631
632 if (observer_ != NULL)
633 observer_->OveruseDetected();
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000634 } else if (IsUnderusing(now)) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000635 last_rampup_time_ = now;
636 in_quick_rampup_ = true;
637
638 if (observer_ != NULL)
639 observer_->NormalUsage();
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000640 }
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000641
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000642 int rampup_delay =
643 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000644 LOG(LS_VERBOSE) << " Frame stats: capture avg: " << capture_deltas_.Mean()
645 << " capture stddev " << capture_deltas_.StdDev()
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000646 << " encode usage " << usage_->Value()
647 << " encode rsd " << rsd_->Value()
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000648 << " overuse detections " << num_overuse_detections_
649 << " rampup delay " << rampup_delay;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000650 return 0;
651}
652
653bool OveruseFrameDetector::IsOverusing() {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000654 bool overusing = false;
655 if (options_.enable_capture_jitter_method) {
656 overusing = capture_deltas_.StdDev() >=
657 options_.high_capture_jitter_threshold_ms;
658 } else if (options_.enable_encode_usage_method) {
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000659 bool usage_overuse =
660 usage_->Value() >= options_.high_encode_usage_threshold_percent;
661 bool rsd_overuse = false;
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000662 if (options_.high_encode_time_rsd_threshold > 0) {
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000663 rsd_overuse = (rsd_->Value() >= options_.high_encode_time_rsd_threshold);
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000664 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000665 overusing = usage_overuse || rsd_overuse;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000666 }
667
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000668 if (overusing) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000669 ++checks_above_threshold_;
670 } else {
671 checks_above_threshold_ = 0;
672 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000673 return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000674}
675
676bool OveruseFrameDetector::IsUnderusing(int64_t time_now) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000677 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
678 if (time_now < last_rampup_time_ + delay)
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000679 return false;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000680
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000681 bool underusing = false;
682 if (options_.enable_capture_jitter_method) {
683 underusing = capture_deltas_.StdDev() <
684 options_.low_capture_jitter_threshold_ms;
685 } else if (options_.enable_encode_usage_method) {
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000686 bool usage_underuse =
687 usage_->Value() < options_.low_encode_usage_threshold_percent;
688 bool rsd_underuse = true;
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000689 if (options_.low_encode_time_rsd_threshold > 0) {
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000690 rsd_underuse = (rsd_->Value() < options_.low_encode_time_rsd_threshold);
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000691 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000692 underusing = usage_underuse && rsd_underuse;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000693 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000694 return underusing;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000695}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000696} // namespace webrtc