blob: 32b0d2554e6e6599af22c554329e1322f9bd3dd2 [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 processing time of frames.
216class OveruseFrameDetector::FrameQueue {
217 public:
218 FrameQueue() : last_processing_time_ms_(-1) {}
219 ~FrameQueue() {}
220
221 // Called when a frame is captured.
222 // Starts the measuring of the processing time of the frame.
223 void Start(int64_t capture_time, int64_t now) {
224 const size_t kMaxSize = 90; // Allows for processing time of 1.5s at 60fps.
225 if (frame_times_.size() > kMaxSize) {
226 LOG(LS_WARNING) << "Max size reached, removed oldest frame.";
227 frame_times_.erase(frame_times_.begin());
228 }
229 if (frame_times_.find(capture_time) != frame_times_.end()) {
230 // Frame should not exist.
231 assert(false);
232 return;
233 }
234 frame_times_[capture_time] = now;
235 }
236
237 // Called when the processing of a frame has finished.
238 // Returns the processing time of the frame.
239 int End(int64_t capture_time, int64_t now) {
240 std::map<int64_t, int64_t>::iterator it = frame_times_.find(capture_time);
241 if (it == frame_times_.end()) {
242 return -1;
243 }
244 // Remove any old frames up to current.
245 // Old frames have been skipped by the capture process thread.
246 // TODO(asapersson): Consider measuring time from first frame in list.
247 last_processing_time_ms_ = now - (*it).second;
248 frame_times_.erase(frame_times_.begin(), ++it);
249 return last_processing_time_ms_;
250 }
251
252 void Reset() { frame_times_.clear(); }
253 int NumFrames() const { return frame_times_.size(); }
254 int last_processing_time_ms() const { return last_processing_time_ms_; }
255
256 private:
257 // Captured frames mapped by the capture time.
258 std::map<int64_t, int64_t> frame_times_;
259 int last_processing_time_ms_;
260};
261
262// TODO(asapersson): Remove this class. Not used.
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000263// Class for calculating the capture queue delay change.
264class OveruseFrameDetector::CaptureQueueDelay {
265 public:
266 CaptureQueueDelay()
267 : kWeightFactor(0.5f),
268 delay_ms_(0),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000269 filtered_delay_ms_per_s_(new rtc::ExpFilter(kWeightFactor)) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000270 filtered_delay_ms_per_s_->Apply(1.0f, 0.0f);
271 }
272 ~CaptureQueueDelay() {}
273
274 void FrameCaptured(int64_t now) {
275 const size_t kMaxSize = 200;
276 if (frames_.size() > kMaxSize) {
277 frames_.pop_front();
278 }
279 frames_.push_back(now);
280 }
281
282 void FrameProcessingStarted(int64_t now) {
283 if (frames_.empty()) {
284 return;
285 }
286 delay_ms_ = now - frames_.front();
287 frames_.pop_front();
288 }
289
290 void CalculateDelayChange(int64_t diff_last_sample_ms) {
291 if (diff_last_sample_ms <= 0) {
292 return;
293 }
294 float exp = static_cast<float>(diff_last_sample_ms) / kProcessIntervalMs;
295 exp = std::min(exp, kMaxExp);
296 filtered_delay_ms_per_s_->Apply(exp,
297 delay_ms_ * 1000.0f / diff_last_sample_ms);
298 ClearFrames();
299 }
300
301 void ClearFrames() {
302 frames_.clear();
303 }
304
305 int delay_ms() const {
306 return delay_ms_;
307 }
308
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000309 int Value() const {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000310 return static_cast<int>(filtered_delay_ms_per_s_->filtered() + 0.5);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000311 }
312
313 private:
314 const float kWeightFactor;
315 std::list<int64_t> frames_;
316 int delay_ms_;
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000317 scoped_ptr<rtc::ExpFilter> filtered_delay_ms_per_s_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000318};
319
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000320OveruseFrameDetector::OveruseFrameDetector(Clock* clock)
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000321 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000322 observer_(NULL),
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000323 clock_(clock),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000324 next_process_time_(clock_->TimeInMilliseconds()),
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000325 num_process_times_(0),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000326 last_capture_time_(0),
327 last_overuse_time_(0),
328 checks_above_threshold_(0),
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000329 num_overuse_detections_(0),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000330 last_rampup_time_(0),
331 in_quick_rampup_(false),
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000332 current_rampup_delay_ms_(kStandardRampUpDelayMs),
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000333 num_pixels_(0),
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000334 last_encode_sample_ms_(0),
335 encode_time_(new EncodeTimeAvg()),
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000336 usage_(new SendProcessingUsage()),
337 frame_queue_(new FrameQueue()),
338 last_sample_time_ms_(0),
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000339 capture_queue_delay_(new CaptureQueueDelay()) {
340}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000341
342OveruseFrameDetector::~OveruseFrameDetector() {
343}
344
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000345void OveruseFrameDetector::SetObserver(CpuOveruseObserver* observer) {
346 CriticalSectionScoped cs(crit_.get());
347 observer_ = observer;
348}
349
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000350void OveruseFrameDetector::SetOptions(const CpuOveruseOptions& options) {
351 assert(options.min_frame_samples > 0);
352 CriticalSectionScoped cs(crit_.get());
353 if (options_.Equals(options)) {
354 return;
355 }
356 options_ = options;
357 capture_deltas_.SetOptions(options);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000358 usage_->SetOptions(options);
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000359 ResetAll(num_pixels_);
360}
361
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000362int OveruseFrameDetector::CaptureQueueDelayMsPerS() const {
363 CriticalSectionScoped cs(crit_.get());
364 return capture_queue_delay_->delay_ms();
365}
366
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000367int OveruseFrameDetector::LastProcessingTimeMs() const {
368 CriticalSectionScoped cs(crit_.get());
369 return frame_queue_->last_processing_time_ms();
370}
371
372int OveruseFrameDetector::FramesInQueue() const {
373 CriticalSectionScoped cs(crit_.get());
374 return frame_queue_->NumFrames();
375}
376
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000377void OveruseFrameDetector::GetCpuOveruseMetrics(
378 CpuOveruseMetrics* metrics) const {
379 CriticalSectionScoped cs(crit_.get());
380 metrics->capture_jitter_ms = static_cast<int>(capture_deltas_.StdDev() + 0.5);
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000381 metrics->avg_encode_time_ms = encode_time_->Value();
asapersson@webrtc.org7f105132014-10-29 10:05:21 +0000382 metrics->encode_rsd = 0;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000383 metrics->encode_usage_percent = usage_->Value();
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000384 metrics->capture_queue_delay_ms_per_s = capture_queue_delay_->Value();
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000385}
386
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000387int32_t OveruseFrameDetector::TimeUntilNextProcess() {
388 CriticalSectionScoped cs(crit_.get());
389 return next_process_time_ - clock_->TimeInMilliseconds();
390}
391
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000392bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const {
393 if (num_pixels != num_pixels_) {
394 return true;
395 }
396 return false;
397}
398
399bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000400 if (last_capture_time_ == 0) {
401 return false;
402 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000403 return (now - last_capture_time_) > options_.frame_timeout_interval_ms;
404}
405
406void OveruseFrameDetector::ResetAll(int num_pixels) {
407 num_pixels_ = num_pixels;
408 capture_deltas_.Reset();
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000409 usage_->Reset();
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000410 frame_queue_->Reset();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000411 capture_queue_delay_->ClearFrames();
412 last_capture_time_ = 0;
413 num_process_times_ = 0;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000414}
415
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000416void OveruseFrameDetector::FrameCaptured(int width,
417 int height,
418 int64_t capture_time_ms) {
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000419 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000420
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000421 int64_t now = clock_->TimeInMilliseconds();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000422 if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) {
423 ResetAll(width * height);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000424 }
425
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000426 if (last_capture_time_ != 0) {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000427 capture_deltas_.AddSample(now - last_capture_time_);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000428 usage_->AddCaptureSample(now - last_capture_time_);
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000429 }
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000430 last_capture_time_ = now;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000431
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000432 capture_queue_delay_->FrameCaptured(now);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000433
434 if (options_.enable_extended_processing_usage) {
435 frame_queue_->Start(capture_time_ms, now);
436 }
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000437}
438
439void OveruseFrameDetector::FrameProcessingStarted() {
440 CriticalSectionScoped cs(crit_.get());
441 capture_queue_delay_->FrameProcessingStarted(clock_->TimeInMilliseconds());
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000442}
443
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000444void OveruseFrameDetector::FrameEncoded(int encode_time_ms) {
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000445 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000446 int64_t now = clock_->TimeInMilliseconds();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000447 if (last_encode_sample_ms_ != 0) {
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000448 int64_t diff_ms = now - last_encode_sample_ms_;
449 encode_time_->AddSample(encode_time_ms, diff_ms);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000450 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000451 last_encode_sample_ms_ = now;
452
453 if (!options_.enable_extended_processing_usage) {
454 AddProcessingTime(encode_time_ms);
455 }
456}
457
458void OveruseFrameDetector::FrameSent(int64_t capture_time_ms) {
459 CriticalSectionScoped cs(crit_.get());
460 if (!options_.enable_extended_processing_usage) {
461 return;
462 }
463 int delay_ms = frame_queue_->End(capture_time_ms,
464 clock_->TimeInMilliseconds());
465 if (delay_ms > 0) {
466 AddProcessingTime(delay_ms);
467 }
468}
469
470void OveruseFrameDetector::AddProcessingTime(int elapsed_ms) {
471 int64_t now = clock_->TimeInMilliseconds();
472 if (last_sample_time_ms_ != 0) {
473 int64_t diff_ms = now - last_sample_time_ms_;
474 usage_->AddSample(elapsed_ms, diff_ms);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000475 }
476 last_sample_time_ms_ = now;
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000477}
478
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000479int32_t OveruseFrameDetector::Process() {
480 CriticalSectionScoped cs(crit_.get());
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000481
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000482 int64_t now = clock_->TimeInMilliseconds();
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000483
484 // Used to protect against Process() being called too often.
485 if (now < next_process_time_)
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000486 return 0;
487
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000488 int64_t diff_ms = now - next_process_time_ + kProcessIntervalMs;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000489 next_process_time_ = now + kProcessIntervalMs;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000490 ++num_process_times_;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000491
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000492 capture_queue_delay_->CalculateDelayChange(diff_ms);
493
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000494 if (num_process_times_ <= options_.min_process_count) {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000495 return 0;
496 }
497
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000498 if (IsOverusing()) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000499 // If the last thing we did was going up, and now have to back down, we need
500 // to check if this peak was short. If so we should back off to avoid going
501 // back and forth between this load, the system doesn't seem to handle it.
502 bool check_for_backoff = last_rampup_time_ > last_overuse_time_;
503 if (check_for_backoff) {
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000504 if (now - last_rampup_time_ < kStandardRampUpDelayMs ||
505 num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000506 // Going up was not ok for very long, back off.
507 current_rampup_delay_ms_ *= kRampUpBackoffFactor;
508 if (current_rampup_delay_ms_ > kMaxRampUpDelayMs)
509 current_rampup_delay_ms_ = kMaxRampUpDelayMs;
510 } else {
511 // Not currently backing off, reset rampup delay.
512 current_rampup_delay_ms_ = kStandardRampUpDelayMs;
513 }
514 }
515
516 last_overuse_time_ = now;
517 in_quick_rampup_ = false;
518 checks_above_threshold_ = 0;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000519 ++num_overuse_detections_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000520
521 if (observer_ != NULL)
522 observer_->OveruseDetected();
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000523 } else if (IsUnderusing(now)) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000524 last_rampup_time_ = now;
525 in_quick_rampup_ = true;
526
527 if (observer_ != NULL)
528 observer_->NormalUsage();
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000529 }
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000530
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000531 int rampup_delay =
532 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000533 LOG(LS_VERBOSE) << " Frame stats: capture avg: " << capture_deltas_.Mean()
534 << " capture stddev " << capture_deltas_.StdDev()
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000535 << " encode usage " << usage_->Value()
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000536 << " overuse detections " << num_overuse_detections_
537 << " rampup delay " << rampup_delay;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000538 return 0;
539}
540
541bool OveruseFrameDetector::IsOverusing() {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000542 bool overusing = false;
543 if (options_.enable_capture_jitter_method) {
544 overusing = capture_deltas_.StdDev() >=
545 options_.high_capture_jitter_threshold_ms;
546 } else if (options_.enable_encode_usage_method) {
asapersson@webrtc.org7f105132014-10-29 10:05:21 +0000547 overusing = usage_->Value() >= options_.high_encode_usage_threshold_percent;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000548 }
549
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000550 if (overusing) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000551 ++checks_above_threshold_;
552 } else {
553 checks_above_threshold_ = 0;
554 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000555 return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000556}
557
558bool OveruseFrameDetector::IsUnderusing(int64_t time_now) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000559 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
560 if (time_now < last_rampup_time_ + delay)
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000561 return false;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000562
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000563 bool underusing = false;
564 if (options_.enable_capture_jitter_method) {
565 underusing = capture_deltas_.StdDev() <
566 options_.low_capture_jitter_threshold_ms;
567 } else if (options_.enable_encode_usage_method) {
asapersson@webrtc.org7f105132014-10-29 10:05:21 +0000568 underusing = usage_->Value() < options_.low_encode_usage_threshold_percent;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000569 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000570 return underusing;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000571}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000572} // namespace webrtc