blob: 522a505594052abdf3481e020b86e46ba9c260f5 [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
Peter Boström7623ce42015-12-09 12:13:30 +010011#include "webrtc/video/overuse_frame_detector.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000012
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"
Peter Boströme4499152016-02-05 11:13:28 +010023#include "webrtc/frame_callback.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010024#include "webrtc/system_wrappers/include/clock.h"
Peter Boströme4499152016-02-05 11:13:28 +010025#include "webrtc/video_frame.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000026
torbjorng448468d2016-02-10 08:11:57 -080027#if defined(WEBRTC_MAC)
28#include <mach/mach.h>
29#endif
30
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000031namespace webrtc {
32
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000033namespace {
pbos@webrtc.orga9575702013-08-30 17:16:32 +000034const int64_t kProcessIntervalMs = 5000;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000035
pbos@webrtc.orga9575702013-08-30 17:16:32 +000036// Delay between consecutive rampups. (Used for quick recovery.)
37const int kQuickRampUpDelayMs = 10 * 1000;
38// Delay between rampup attempts. Initially uses standard, scales up to max.
asapersson@webrtc.org23a4d852014-08-13 14:33:49 +000039const int kStandardRampUpDelayMs = 40 * 1000;
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +000040const int kMaxRampUpDelayMs = 240 * 1000;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000041// Expontential back-off factor, to prevent annoying up-down behaviour.
42const double kRampUpBackoffFactor = 2.0;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000043
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +000044// Max number of overuses detected before always applying the rampup delay.
asapersson@webrtc.org23a4d852014-08-13 14:33:49 +000045const int kMaxOverusesBeforeApplyRampupDelay = 4;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000046
47// The maximum exponent to use in VCMExpFilter.
48const float kSampleDiffMs = 33.0f;
49const float kMaxExp = 7.0f;
50
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000051} // namespace
52
torbjorng448468d2016-02-10 08:11:57 -080053CpuOveruseOptions::CpuOveruseOptions()
54 : high_encode_usage_threshold_percent(85),
55 frame_timeout_interval_ms(1500),
56 min_frame_samples(120),
57 min_process_count(3),
58 high_threshold_consecutive_count(2) {
59#if defined(WEBRTC_MAC)
60 // This is proof-of-concept code for letting the physical core count affect
61 // the interval into which we attempt to scale. For now, the code is Mac OS
62 // specific, since that's the platform were we saw most problems.
63 // TODO(torbjorng): Enhance SystemInfo to return this metric.
64
65 mach_port_t mach_host = mach_host_self();
66 host_basic_info hbi = {};
67 mach_msg_type_number_t info_count = HOST_BASIC_INFO_COUNT;
68 kern_return_t kr =
69 host_info(mach_host, HOST_BASIC_INFO, reinterpret_cast<host_info_t>(&hbi),
70 &info_count);
71 mach_port_deallocate(mach_task_self(), mach_host);
72
73 int n_physical_cores;
74 if (kr != KERN_SUCCESS) {
75 // If we couldn't get # of physical CPUs, don't panic. Assume we have 1.
76 n_physical_cores = 1;
77 LOG(LS_ERROR) << "Failed to determine number of physical cores, assuming 1";
78 } else {
79 n_physical_cores = hbi.physical_cpu;
80 LOG(LS_INFO) << "Number of physical cores:" << n_physical_cores;
81 }
82
83 // Change init list default for few core systems. The assumption here is that
84 // encoding, which we measure here, takes about 1/4 of the processing of a
85 // two-way call. This is roughly true for x86 using both vp8 and vp9 without
86 // hardware encoding. Since we don't affect the incoming stream here, we only
87 // control about 1/2 of the total processing needs, but this is not taken into
88 // account.
89 if (n_physical_cores == 1)
90 high_encode_usage_threshold_percent = 20; // Roughly 1/4 of 100%.
91 else if (n_physical_cores == 2)
92 high_encode_usage_threshold_percent = 40; // Roughly 1/4 of 200%.
93
94#endif // WEBRTC_MAC
95 // Note that we make the interval 2x+epsilon wide, since libyuv scaling steps
96 // are close to that (when squared). This wide interval makes sure that
97 // scaling up or down does not jump all the way across the interval.
98 low_encode_usage_threshold_percent =
99 (high_encode_usage_threshold_percent - 1) / 2;
100}
101
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000102// Class for calculating the processing usage on the send-side (the average
103// processing time of a frame divided by the average time difference between
104// captured frames).
105class OveruseFrameDetector::SendProcessingUsage {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000106 public:
Peter Boström4b91bd02015-06-26 06:58:16 +0200107 explicit SendProcessingUsage(const CpuOveruseOptions& options)
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000108 : kWeightFactorFrameDiff(0.998f),
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000109 kWeightFactorProcessing(0.995f),
asapersson@webrtc.orge41dbee2014-05-13 13:45:13 +0000110 kInitialSampleDiffMs(40.0f),
111 kMaxSampleDiffMs(45.0f),
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000112 count_(0),
Peter Boström4b91bd02015-06-26 06:58:16 +0200113 options_(options),
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000114 filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000115 filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000116 Reset();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000117 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000118 ~SendProcessingUsage() {}
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000119
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000120 void Reset() {
121 count_ = 0;
122 filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff);
123 filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000124 filtered_processing_ms_->Reset(kWeightFactorProcessing);
125 filtered_processing_ms_->Apply(1.0f, InitialProcessingMs());
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000126 }
127
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000128 void AddCaptureSample(float sample_ms) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000129 float exp = sample_ms / kSampleDiffMs;
130 exp = std::min(exp, kMaxExp);
131 filtered_frame_diff_ms_->Apply(exp, sample_ms);
132 }
133
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000134 void AddSample(float processing_ms, int64_t diff_last_sample_ms) {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000135 ++count_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000136 float exp = diff_last_sample_ms / kSampleDiffMs;
137 exp = std::min(exp, kMaxExp);
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000138 filtered_processing_ms_->Apply(exp, processing_ms);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000139 }
140
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000141 int Value() const {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000142 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) {
143 return static_cast<int>(InitialUsageInPercent() + 0.5f);
144 }
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000145 float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f);
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000146 frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000147 float encode_usage_percent =
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000148 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000149 return static_cast<int>(encode_usage_percent + 0.5);
150 }
151
asapersson@webrtc.org2881ab12014-06-12 08:46:46 +0000152 private:
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000153 float InitialUsageInPercent() const {
154 // Start in between the underuse and overuse threshold.
155 return (options_.low_encode_usage_threshold_percent +
156 options_.high_encode_usage_threshold_percent) / 2.0f;
157 }
158
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000159 float InitialProcessingMs() const {
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000160 return InitialUsageInPercent() * kInitialSampleDiffMs / 100;
161 }
162
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000163 const float kWeightFactorFrameDiff;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000164 const float kWeightFactorProcessing;
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000165 const float kInitialSampleDiffMs;
166 const float kMaxSampleDiffMs;
167 uint64_t count_;
Peter Boström4b91bd02015-06-26 06:58:16 +0200168 const CpuOveruseOptions options_;
kwiberg27f982b2016-03-01 11:52:33 -0800169 std::unique_ptr<rtc::ExpFilter> filtered_processing_ms_;
170 std::unique_ptr<rtc::ExpFilter> filtered_frame_diff_ms_;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000171};
172
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000173OveruseFrameDetector::OveruseFrameDetector(
174 Clock* clock,
Peter Boström4b91bd02015-06-26 06:58:16 +0200175 const CpuOveruseOptions& options,
176 CpuOveruseObserver* observer,
Peter Boströme4499152016-02-05 11:13:28 +0100177 EncodedFrameObserver* encoder_timing,
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000178 CpuOveruseMetricsObserver* metrics_observer)
Peter Boström4b91bd02015-06-26 06:58:16 +0200179 : options_(options),
180 observer_(observer),
Peter Boströme4499152016-02-05 11:13:28 +0100181 encoder_timing_(encoder_timing),
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000182 metrics_observer_(metrics_observer),
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000183 clock_(clock),
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000184 num_process_times_(0),
Peter Boströme4499152016-02-05 11:13:28 +0100185 last_capture_time_ms_(-1),
186 last_processed_capture_time_ms_(-1),
asapersson74d85e12015-09-24 00:53:32 -0700187 num_pixels_(0),
Peter Boströme4499152016-02-05 11:13:28 +0100188 next_process_time_ms_(clock_->TimeInMilliseconds()),
189 last_overuse_time_ms_(-1),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000190 checks_above_threshold_(0),
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000191 num_overuse_detections_(0),
Peter Boströme4499152016-02-05 11:13:28 +0100192 last_rampup_time_ms_(-1),
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000193 in_quick_rampup_(false),
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000194 current_rampup_delay_ms_(kStandardRampUpDelayMs),
Peter Boströme4499152016-02-05 11:13:28 +0100195 usage_(new SendProcessingUsage(options)) {
henrikg91d6ede2015-09-17 00:24:34 -0700196 RTC_DCHECK(metrics_observer != nullptr);
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000197 processing_thread_.DetachFromThread();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000198}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000199
200OveruseFrameDetector::~OveruseFrameDetector() {
201}
202
Peter Boströme4499152016-02-05 11:13:28 +0100203void OveruseFrameDetector::EncodedFrameTimeMeasured(int encode_duration_ms) {
204 if (!metrics_)
205 metrics_ = rtc::Optional<CpuOveruseMetrics>(CpuOveruseMetrics());
206 metrics_->encode_usage_percent = usage_->Value();
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000207
Peter Boströme4499152016-02-05 11:13:28 +0100208 metrics_observer_->OnEncodedFrameTimeMeasured(encode_duration_ms, *metrics_);
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000209}
210
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000211int64_t OveruseFrameDetector::TimeUntilNextProcess() {
henrikg91d6ede2015-09-17 00:24:34 -0700212 RTC_DCHECK(processing_thread_.CalledOnValidThread());
Peter Boströme4499152016-02-05 11:13:28 +0100213 return next_process_time_ms_ - clock_->TimeInMilliseconds();
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000214}
215
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000216bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const {
217 if (num_pixels != num_pixels_) {
218 return true;
219 }
220 return false;
221}
222
223bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const {
Peter Boströme4499152016-02-05 11:13:28 +0100224 if (last_capture_time_ms_ == -1)
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000225 return false;
Peter Boströme4499152016-02-05 11:13:28 +0100226 return (now - last_capture_time_ms_) > options_.frame_timeout_interval_ms;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000227}
228
229void OveruseFrameDetector::ResetAll(int num_pixels) {
230 num_pixels_ = num_pixels;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000231 usage_->Reset();
Peter Boströme4499152016-02-05 11:13:28 +0100232 frame_timing_.clear();
233 last_capture_time_ms_ = -1;
234 last_processed_capture_time_ms_ = -1;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000235 num_process_times_ = 0;
Peter Boströme4499152016-02-05 11:13:28 +0100236 metrics_ = rtc::Optional<CpuOveruseMetrics>();
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000237}
238
Peter Boströme4499152016-02-05 11:13:28 +0100239void OveruseFrameDetector::FrameCaptured(const VideoFrame& frame) {
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000240 rtc::CritScope cs(&crit_);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000241
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000242 int64_t now = clock_->TimeInMilliseconds();
Peter Boströme4499152016-02-05 11:13:28 +0100243 if (FrameSizeChanged(frame.width() * frame.height()) ||
244 FrameTimeoutDetected(now)) {
245 ResetAll(frame.width() * frame.height());
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000246 }
247
Peter Boströme4499152016-02-05 11:13:28 +0100248 if (last_capture_time_ms_ != -1)
249 usage_->AddCaptureSample(now - last_capture_time_ms_);
Åsa Persson746210f2015-09-08 10:52:42 +0200250
Peter Boströme4499152016-02-05 11:13:28 +0100251 last_capture_time_ms_ = now;
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000252
Peter Boströme4499152016-02-05 11:13:28 +0100253 frame_timing_.push_back(
254 FrameTiming(frame.ntp_time_ms(), frame.timestamp(), now));
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000255}
256
Peter Boströme4499152016-02-05 11:13:28 +0100257void OveruseFrameDetector::FrameSent(uint32_t timestamp) {
asapersson1aa420b2015-12-07 03:12:22 -0800258 rtc::CritScope cs(&crit_);
Peter Boströme4499152016-02-05 11:13:28 +0100259 // Delay before reporting actual encoding time, used to have the ability to
260 // detect total encoding time when encoding more than one layer. Encoding is
261 // here assumed to finish within a second (or that we get enough long-time
262 // samples before one second to trigger an overuse even when this is not the
263 // case).
264 static const int64_t kEncodingTimeMeasureWindowMs = 1000;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000265 int64_t now = clock_->TimeInMilliseconds();
Peter Boströme4499152016-02-05 11:13:28 +0100266 for (auto& it : frame_timing_) {
267 if (it.timestamp == timestamp) {
268 it.last_send_ms = now;
269 break;
270 }
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000271 }
Peter Boströme4499152016-02-05 11:13:28 +0100272 // TODO(pbos): Handle the case/log errors when not finding the corresponding
273 // frame (either very slow encoding or incorrect wrong timestamps returned
274 // from the encoder).
275 // This is currently the case for all frames on ChromeOS, so logging them
276 // would be spammy, and triggering overuse would be wrong.
277 // https://crbug.com/350106
278 while (!frame_timing_.empty()) {
279 FrameTiming timing = frame_timing_.front();
280 if (now - timing.capture_ms < kEncodingTimeMeasureWindowMs)
281 break;
282 if (timing.last_send_ms != -1) {
283 int encode_duration_ms =
284 static_cast<int>(timing.last_send_ms - timing.capture_ms);
285 if (encoder_timing_) {
286 encoder_timing_->OnEncodeTiming(timing.capture_ntp_ms,
287 encode_duration_ms);
288 }
289 if (last_processed_capture_time_ms_ != -1) {
290 int64_t diff_ms = timing.capture_ms - last_processed_capture_time_ms_;
291 usage_->AddSample(encode_duration_ms, diff_ms);
292 }
293 last_processed_capture_time_ms_ = timing.capture_ms;
294 EncodedFrameTimeMeasured(encode_duration_ms);
295 }
296 frame_timing_.pop_front();
297 }
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000298}
299
pbosa26ac922016-02-25 04:50:01 -0800300void OveruseFrameDetector::Process() {
henrikg91d6ede2015-09-17 00:24:34 -0700301 RTC_DCHECK(processing_thread_.CalledOnValidThread());
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000302
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000303 int64_t now = clock_->TimeInMilliseconds();
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000304
305 // Used to protect against Process() being called too often.
Peter Boströme4499152016-02-05 11:13:28 +0100306 if (now < next_process_time_ms_)
pbosa26ac922016-02-25 04:50:01 -0800307 return;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000308
Peter Boströme4499152016-02-05 11:13:28 +0100309 next_process_time_ms_ = now + kProcessIntervalMs;
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000310
asapersson74d85e12015-09-24 00:53:32 -0700311 CpuOveruseMetrics current_metrics;
312 {
313 rtc::CritScope cs(&crit_);
314 ++num_process_times_;
Peter Boströme4499152016-02-05 11:13:28 +0100315 if (num_process_times_ <= options_.min_process_count || !metrics_)
pbosa26ac922016-02-25 04:50:01 -0800316 return;
Peter Boströme4499152016-02-05 11:13:28 +0100317
318 current_metrics = *metrics_;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000319 }
320
asapersson74d85e12015-09-24 00:53:32 -0700321 if (IsOverusing(current_metrics)) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000322 // If the last thing we did was going up, and now have to back down, we need
323 // to check if this peak was short. If so we should back off to avoid going
324 // back and forth between this load, the system doesn't seem to handle it.
Peter Boströme4499152016-02-05 11:13:28 +0100325 bool check_for_backoff = last_rampup_time_ms_ > last_overuse_time_ms_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000326 if (check_for_backoff) {
Peter Boströme4499152016-02-05 11:13:28 +0100327 if (now - last_rampup_time_ms_ < kStandardRampUpDelayMs ||
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000328 num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000329 // Going up was not ok for very long, back off.
330 current_rampup_delay_ms_ *= kRampUpBackoffFactor;
331 if (current_rampup_delay_ms_ > kMaxRampUpDelayMs)
332 current_rampup_delay_ms_ = kMaxRampUpDelayMs;
333 } else {
334 // Not currently backing off, reset rampup delay.
335 current_rampup_delay_ms_ = kStandardRampUpDelayMs;
336 }
337 }
338
Peter Boströme4499152016-02-05 11:13:28 +0100339 last_overuse_time_ms_ = now;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000340 in_quick_rampup_ = false;
341 checks_above_threshold_ = 0;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000342 ++num_overuse_detections_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000343
344 if (observer_ != NULL)
345 observer_->OveruseDetected();
asapersson74d85e12015-09-24 00:53:32 -0700346 } else if (IsUnderusing(current_metrics, now)) {
Peter Boströme4499152016-02-05 11:13:28 +0100347 last_rampup_time_ms_ = now;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000348 in_quick_rampup_ = true;
349
350 if (observer_ != NULL)
351 observer_->NormalUsage();
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000352 }
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000353
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000354 int rampup_delay =
355 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
asapersson74d85e12015-09-24 00:53:32 -0700356
357 LOG(LS_VERBOSE) << " Frame stats: "
358 << " encode usage " << current_metrics.encode_usage_percent
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000359 << " overuse detections " << num_overuse_detections_
360 << " rampup delay " << rampup_delay;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000361}
362
asapersson74d85e12015-09-24 00:53:32 -0700363bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) {
Peter Boström01f364e2016-01-07 16:38:25 +0100364 if (metrics.encode_usage_percent >=
365 options_.high_encode_usage_threshold_percent) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000366 ++checks_above_threshold_;
367 } else {
368 checks_above_threshold_ = 0;
369 }
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000370 return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000371}
372
asapersson74d85e12015-09-24 00:53:32 -0700373bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics,
374 int64_t time_now) {
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000375 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
Peter Boströme4499152016-02-05 11:13:28 +0100376 if (time_now < last_rampup_time_ms_ + delay)
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000377 return false;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000378
Peter Boström01f364e2016-01-07 16:38:25 +0100379 return metrics.encode_usage_percent <
380 options_.low_encode_usage_threshold_percent;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000381}
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000382} // namespace webrtc