blob: b0c82d9ec1715088ddf686712d0cb1653d9967af [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
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +000020#include "webrtc/base/checks.h"
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000021#include "webrtc/base/exp_filter.h"
Peter Boström415d2cd2015-10-26 11:35:17 +010022#include "webrtc/base/logging.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000023#include "webrtc/system_wrappers/interface/clock.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000024
25namespace webrtc {
26
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000027namespace {
pbos@webrtc.orga9575702013-08-30 17:16:32 +000028const int64_t kProcessIntervalMs = 5000;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000029
pbos@webrtc.orga9575702013-08-30 17:16:32 +000030// Delay between consecutive rampups. (Used for quick recovery.)
31const int kQuickRampUpDelayMs = 10 * 1000;
32// Delay between rampup attempts. Initially uses standard, scales up to max.
asapersson@webrtc.org23a4d852014-08-13 14:33:49 +000033const int kStandardRampUpDelayMs = 40 * 1000;
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +000034const int kMaxRampUpDelayMs = 240 * 1000;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000035// Expontential back-off factor, to prevent annoying up-down behaviour.
36const double kRampUpBackoffFactor = 2.0;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000037
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +000038// Max number of overuses detected before always applying the rampup delay.
asapersson@webrtc.org23a4d852014-08-13 14:33:49 +000039const int kMaxOverusesBeforeApplyRampupDelay = 4;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000040
41// The maximum exponent to use in VCMExpFilter.
42const float kSampleDiffMs = 33.0f;
43const float kMaxExp = 7.0f;
44
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000045} // namespace
46
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000047// Class for calculating the average encode time.
48class OveruseFrameDetector::EncodeTimeAvg {
49 public:
50 EncodeTimeAvg()
51 : kWeightFactor(0.5f),
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +000052 kInitialAvgEncodeTimeMs(5.0f),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000053 filtered_encode_time_ms_(new rtc::ExpFilter(kWeightFactor)) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000054 filtered_encode_time_ms_->Apply(1.0f, kInitialAvgEncodeTimeMs);
55 }
56 ~EncodeTimeAvg() {}
57
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000058 void AddSample(float encode_time_ms, int64_t diff_last_sample_ms) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000059 float exp = diff_last_sample_ms / kSampleDiffMs;
60 exp = std::min(exp, kMaxExp);
61 filtered_encode_time_ms_->Apply(exp, encode_time_ms);
62 }
63
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +000064 int Value() const {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000065 return static_cast<int>(filtered_encode_time_ms_->filtered() + 0.5);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000066 }
67
68 private:
69 const float kWeightFactor;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +000070 const float kInitialAvgEncodeTimeMs;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000071 rtc::scoped_ptr<rtc::ExpFilter> filtered_encode_time_ms_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000072};
73
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000074// Class for calculating the processing usage on the send-side (the average
75// processing time of a frame divided by the average time difference between
76// captured frames).
77class OveruseFrameDetector::SendProcessingUsage {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000078 public:
Peter Boström4b91bd02015-06-26 06:58:16 +020079 explicit SendProcessingUsage(const CpuOveruseOptions& options)
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000080 : kWeightFactorFrameDiff(0.998f),
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000081 kWeightFactorProcessing(0.995f),
asapersson@webrtc.orge41dbee2014-05-13 13:45:13 +000082 kInitialSampleDiffMs(40.0f),
83 kMaxSampleDiffMs(45.0f),
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +000084 count_(0),
Peter Boström4b91bd02015-06-26 06:58:16 +020085 options_(options),
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000086 filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000087 filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +000088 Reset();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000089 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000090 ~SendProcessingUsage() {}
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000091
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +000092 void Reset() {
93 count_ = 0;
94 filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff);
95 filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000096 filtered_processing_ms_->Reset(kWeightFactorProcessing);
97 filtered_processing_ms_->Apply(1.0f, InitialProcessingMs());
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +000098 }
99
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000100 void AddCaptureSample(float sample_ms) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000101 float exp = sample_ms / kSampleDiffMs;
102 exp = std::min(exp, kMaxExp);
103 filtered_frame_diff_ms_->Apply(exp, sample_ms);
104 }
105
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000106 void AddSample(float processing_ms, int64_t diff_last_sample_ms) {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000107 ++count_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000108 float exp = diff_last_sample_ms / kSampleDiffMs;
109 exp = std::min(exp, kMaxExp);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000110 filtered_processing_ms_->Apply(exp, processing_ms);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000111 }
112
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000113 int Value() const {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000114 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) {
115 return static_cast<int>(InitialUsageInPercent() + 0.5f);
116 }
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000117 float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f);
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000118 frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000119 float encode_usage_percent =
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000120 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000121 return static_cast<int>(encode_usage_percent + 0.5);
122 }
123
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000124 private:
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000125 float InitialUsageInPercent() const {
126 // Start in between the underuse and overuse threshold.
127 return (options_.low_encode_usage_threshold_percent +
128 options_.high_encode_usage_threshold_percent) / 2.0f;
129 }
130
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000131 float InitialProcessingMs() const {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000132 return InitialUsageInPercent() * kInitialSampleDiffMs / 100;
133 }
134
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000135 const float kWeightFactorFrameDiff;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000136 const float kWeightFactorProcessing;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000137 const float kInitialSampleDiffMs;
138 const float kMaxSampleDiffMs;
139 uint64_t count_;
Peter Boström4b91bd02015-06-26 06:58:16 +0200140 const CpuOveruseOptions options_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000141 rtc::scoped_ptr<rtc::ExpFilter> filtered_processing_ms_;
142 rtc::scoped_ptr<rtc::ExpFilter> filtered_frame_diff_ms_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000143};
144
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000145// Class for calculating the processing time of frames.
146class OveruseFrameDetector::FrameQueue {
147 public:
148 FrameQueue() : last_processing_time_ms_(-1) {}
149 ~FrameQueue() {}
150
151 // Called when a frame is captured.
152 // Starts the measuring of the processing time of the frame.
153 void Start(int64_t capture_time, int64_t now) {
154 const size_t kMaxSize = 90; // Allows for processing time of 1.5s at 60fps.
155 if (frame_times_.size() > kMaxSize) {
156 LOG(LS_WARNING) << "Max size reached, removed oldest frame.";
157 frame_times_.erase(frame_times_.begin());
158 }
159 if (frame_times_.find(capture_time) != frame_times_.end()) {
160 // Frame should not exist.
161 assert(false);
162 return;
163 }
164 frame_times_[capture_time] = now;
165 }
166
167 // Called when the processing of a frame has finished.
168 // Returns the processing time of the frame.
169 int End(int64_t capture_time, int64_t now) {
170 std::map<int64_t, int64_t>::iterator it = frame_times_.find(capture_time);
171 if (it == frame_times_.end()) {
172 return -1;
173 }
174 // Remove any old frames up to current.
175 // Old frames have been skipped by the capture process thread.
176 // TODO(asapersson): Consider measuring time from first frame in list.
177 last_processing_time_ms_ = now - (*it).second;
178 frame_times_.erase(frame_times_.begin(), ++it);
179 return last_processing_time_ms_;
180 }
181
182 void Reset() { frame_times_.clear(); }
Peter Boströmae37abb2015-06-18 19:00:34 +0200183 int NumFrames() const { return static_cast<int>(frame_times_.size()); }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000184 int last_processing_time_ms() const { return last_processing_time_ms_; }
185
186 private:
187 // Captured frames mapped by the capture time.
188 std::map<int64_t, int64_t> frame_times_;
189 int last_processing_time_ms_;
190};
191
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000192
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000193OveruseFrameDetector::OveruseFrameDetector(
194 Clock* clock,
Peter Boström4b91bd02015-06-26 06:58:16 +0200195 const CpuOveruseOptions& options,
196 CpuOveruseObserver* observer,
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000197 CpuOveruseMetricsObserver* metrics_observer)
Peter Boström4b91bd02015-06-26 06:58:16 +0200198 : options_(options),
199 observer_(observer),
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000200 metrics_observer_(metrics_observer),
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000201 clock_(clock),
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000202 num_process_times_(0),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000203 last_capture_time_(0),
asapersson74d85e12015-09-24 00:53:32 -0700204 num_pixels_(0),
205 next_process_time_(clock_->TimeInMilliseconds()),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000206 last_overuse_time_(0),
207 checks_above_threshold_(0),
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000208 num_overuse_detections_(0),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000209 last_rampup_time_(0),
210 in_quick_rampup_(false),
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000211 current_rampup_delay_ms_(kStandardRampUpDelayMs),
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000212 last_encode_sample_ms_(0),
asapersson74d85e12015-09-24 00:53:32 -0700213 last_sample_time_ms_(0),
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000214 encode_time_(new EncodeTimeAvg()),
Peter Boström4b91bd02015-06-26 06:58:16 +0200215 usage_(new SendProcessingUsage(options)),
asapersson74d85e12015-09-24 00:53:32 -0700216 frame_queue_(new FrameQueue()) {
henrikg91d6ede2015-09-17 00:24:34 -0700217 RTC_DCHECK(metrics_observer != nullptr);
Peter Boström4b91bd02015-06-26 06:58:16 +0200218 // Make sure stats are initially up-to-date. This simplifies unit testing
219 // since we don't have to trigger an update using one of the methods which
220 // would also alter the overuse state.
221 UpdateCpuOveruseMetrics();
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000222 processing_thread_.DetachFromThread();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000223}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000224
225OveruseFrameDetector::~OveruseFrameDetector() {
226}
227
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000228int OveruseFrameDetector::LastProcessingTimeMs() const {
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000229 rtc::CritScope cs(&crit_);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000230 return frame_queue_->last_processing_time_ms();
231}
232
233int OveruseFrameDetector::FramesInQueue() const {
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000234 rtc::CritScope cs(&crit_);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000235 return frame_queue_->NumFrames();
236}
237
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000238void OveruseFrameDetector::UpdateCpuOveruseMetrics() {
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000239 metrics_.avg_encode_time_ms = encode_time_->Value();
240 metrics_.encode_usage_percent = usage_->Value();
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000241
242 metrics_observer_->CpuOveruseMetricsUpdated(metrics_);
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000243}
244
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000245int64_t OveruseFrameDetector::TimeUntilNextProcess() {
henrikg91d6ede2015-09-17 00:24:34 -0700246 RTC_DCHECK(processing_thread_.CalledOnValidThread());
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000247 return next_process_time_ - clock_->TimeInMilliseconds();
248}
249
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000250bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const {
251 if (num_pixels != num_pixels_) {
252 return true;
253 }
254 return false;
255}
256
257bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const {
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000258 if (last_capture_time_ == 0) {
259 return false;
260 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000261 return (now - last_capture_time_) > options_.frame_timeout_interval_ms;
262}
263
264void OveruseFrameDetector::ResetAll(int num_pixels) {
265 num_pixels_ = num_pixels;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000266 usage_->Reset();
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000267 frame_queue_->Reset();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000268 last_capture_time_ = 0;
269 num_process_times_ = 0;
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000270 UpdateCpuOveruseMetrics();
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000271}
272
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000273void OveruseFrameDetector::FrameCaptured(int width,
274 int height,
275 int64_t capture_time_ms) {
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000276 rtc::CritScope cs(&crit_);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000277
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000278 int64_t now = clock_->TimeInMilliseconds();
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000279 if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) {
280 ResetAll(width * height);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000281 }
282
Åsa Persson746210f2015-09-08 10:52:42 +0200283 if (last_capture_time_ != 0)
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000284 usage_->AddCaptureSample(now - last_capture_time_);
Åsa Persson746210f2015-09-08 10:52:42 +0200285
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000286 last_capture_time_ = now;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000287
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000288 if (options_.enable_extended_processing_usage) {
289 frame_queue_->Start(capture_time_ms, now);
290 }
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000291}
292
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000293void OveruseFrameDetector::FrameEncoded(int encode_time_ms) {
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000294 rtc::CritScope cs(&crit_);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000295 int64_t now = clock_->TimeInMilliseconds();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000296 if (last_encode_sample_ms_ != 0) {
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000297 int64_t diff_ms = now - last_encode_sample_ms_;
298 encode_time_->AddSample(encode_time_ms, diff_ms);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000299 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000300 last_encode_sample_ms_ = now;
301
302 if (!options_.enable_extended_processing_usage) {
303 AddProcessingTime(encode_time_ms);
304 }
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000305 UpdateCpuOveruseMetrics();
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000306}
307
308void OveruseFrameDetector::FrameSent(int64_t capture_time_ms) {
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000309 rtc::CritScope cs(&crit_);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000310 if (!options_.enable_extended_processing_usage) {
311 return;
312 }
313 int delay_ms = frame_queue_->End(capture_time_ms,
314 clock_->TimeInMilliseconds());
315 if (delay_ms > 0) {
316 AddProcessingTime(delay_ms);
317 }
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000318 UpdateCpuOveruseMetrics();
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000319}
320
321void OveruseFrameDetector::AddProcessingTime(int elapsed_ms) {
322 int64_t now = clock_->TimeInMilliseconds();
323 if (last_sample_time_ms_ != 0) {
324 int64_t diff_ms = now - last_sample_time_ms_;
325 usage_->AddSample(elapsed_ms, diff_ms);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000326 }
327 last_sample_time_ms_ = now;
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000328}
329
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000330int32_t OveruseFrameDetector::Process() {
henrikg91d6ede2015-09-17 00:24:34 -0700331 RTC_DCHECK(processing_thread_.CalledOnValidThread());
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000332
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000333 int64_t now = clock_->TimeInMilliseconds();
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000334
335 // Used to protect against Process() being called too often.
336 if (now < next_process_time_)
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000337 return 0;
338
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000339 next_process_time_ = now + kProcessIntervalMs;
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000340
asapersson74d85e12015-09-24 00:53:32 -0700341 CpuOveruseMetrics current_metrics;
342 {
343 rtc::CritScope cs(&crit_);
344 ++num_process_times_;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000345
asapersson74d85e12015-09-24 00:53:32 -0700346 current_metrics = metrics_;
347 if (num_process_times_ <= options_.min_process_count)
348 return 0;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000349 }
350
asapersson74d85e12015-09-24 00:53:32 -0700351 if (IsOverusing(current_metrics)) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000352 // If the last thing we did was going up, and now have to back down, we need
353 // to check if this peak was short. If so we should back off to avoid going
354 // back and forth between this load, the system doesn't seem to handle it.
355 bool check_for_backoff = last_rampup_time_ > last_overuse_time_;
356 if (check_for_backoff) {
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000357 if (now - last_rampup_time_ < kStandardRampUpDelayMs ||
358 num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000359 // Going up was not ok for very long, back off.
360 current_rampup_delay_ms_ *= kRampUpBackoffFactor;
361 if (current_rampup_delay_ms_ > kMaxRampUpDelayMs)
362 current_rampup_delay_ms_ = kMaxRampUpDelayMs;
363 } else {
364 // Not currently backing off, reset rampup delay.
365 current_rampup_delay_ms_ = kStandardRampUpDelayMs;
366 }
367 }
368
369 last_overuse_time_ = now;
370 in_quick_rampup_ = false;
371 checks_above_threshold_ = 0;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000372 ++num_overuse_detections_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000373
374 if (observer_ != NULL)
375 observer_->OveruseDetected();
asapersson74d85e12015-09-24 00:53:32 -0700376 } else if (IsUnderusing(current_metrics, now)) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000377 last_rampup_time_ = now;
378 in_quick_rampup_ = true;
379
380 if (observer_ != NULL)
381 observer_->NormalUsage();
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000382 }
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000383
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000384 int rampup_delay =
385 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
asapersson74d85e12015-09-24 00:53:32 -0700386
387 LOG(LS_VERBOSE) << " Frame stats: "
388 << " encode usage " << current_metrics.encode_usage_percent
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000389 << " overuse detections " << num_overuse_detections_
390 << " rampup delay " << rampup_delay;
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000391
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000392 return 0;
393}
394
asapersson74d85e12015-09-24 00:53:32 -0700395bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000396 bool overusing = false;
asapersson74d85e12015-09-24 00:53:32 -0700397 if (options_.enable_encode_usage_method) {
398 overusing = metrics.encode_usage_percent >=
399 options_.high_encode_usage_threshold_percent;
400 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000401 if (overusing) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000402 ++checks_above_threshold_;
403 } else {
404 checks_above_threshold_ = 0;
405 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000406 return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000407}
408
asapersson74d85e12015-09-24 00:53:32 -0700409bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics,
410 int64_t time_now) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000411 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
412 if (time_now < last_rampup_time_ + delay)
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000413 return false;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000414
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000415 bool underusing = false;
asapersson74d85e12015-09-24 00:53:32 -0700416 if (options_.enable_encode_usage_method) {
417 underusing = metrics.encode_usage_percent <
418 options_.low_encode_usage_threshold_percent;
419 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000420 return underusing;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000421}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000422} // namespace webrtc