mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/overuse_frame_detector.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 12 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 13 | #include <assert.h> |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 14 | #include <math.h> |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 15 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <list> |
asapersson@webrtc.org | 734a532 | 2014-06-10 06:35:22 +0000 | [diff] [blame] | 18 | #include <map> |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <utility> |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 21 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/video/video_frame.h" |
| 23 | #include "common_video/include/frame_callback.h" |
| 24 | #include "rtc_base/checks.h" |
| 25 | #include "rtc_base/logging.h" |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 26 | #include "rtc_base/numerics/exp_filter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/timeutils.h" |
| 28 | #include "system_wrappers/include/field_trial.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 29 | |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 30 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 31 | #include <mach/mach.h> |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 32 | #endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 33 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 34 | namespace webrtc { |
| 35 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 36 | namespace { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 37 | const int64_t kCheckForOveruseIntervalMs = 5000; |
| 38 | const int64_t kTimeToFirstCheckForOveruseMs = 100; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 39 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 40 | // Delay between consecutive rampups. (Used for quick recovery.) |
| 41 | const int kQuickRampUpDelayMs = 10 * 1000; |
| 42 | // Delay between rampup attempts. Initially uses standard, scales up to max. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 43 | const int kStandardRampUpDelayMs = 40 * 1000; |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 44 | const int kMaxRampUpDelayMs = 240 * 1000; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 45 | // Expontential back-off factor, to prevent annoying up-down behaviour. |
| 46 | const double kRampUpBackoffFactor = 2.0; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 47 | |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 48 | // Max number of overuses detected before always applying the rampup delay. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 49 | const int kMaxOverusesBeforeApplyRampupDelay = 4; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 50 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 51 | // The maximum exponent to use in VCMExpFilter. |
| 52 | const float kMaxExp = 7.0f; |
| 53 | // Default value used before first reconfiguration. |
| 54 | const int kDefaultFrameRate = 30; |
| 55 | // Default sample diff, default frame rate. |
| 56 | const float kDefaultSampleDiffMs = 1000.0f / kDefaultFrameRate; |
| 57 | // A factor applied to the sample diff on OnTargetFramerateUpdated to determine |
| 58 | // a max limit for the sample diff. For instance, with a framerate of 30fps, |
| 59 | // the sample diff is capped to (1000 / 30) * 1.35 = 45ms. This prevents |
| 60 | // triggering too soon if there are individual very large outliers. |
| 61 | const float kMaxSampleDiffMarginFactor = 1.35f; |
| 62 | // Minimum framerate allowed for usage calculation. This prevents crazy long |
| 63 | // encode times from being accepted if the frame rate happens to be low. |
| 64 | const int kMinFramerate = 7; |
| 65 | const int kMaxFramerate = 30; |
| 66 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 67 | const auto kScaleReasonCpu = AdaptationObserverInterface::AdaptReason::kCpu; |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 68 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 69 | // Class for calculating the processing usage on the send-side (the average |
| 70 | // processing time of a frame divided by the average time difference between |
| 71 | // captured frames). |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 72 | class SendProcessingUsage : public OveruseFrameDetector::ProcessingUsage { |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 73 | public: |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 74 | explicit SendProcessingUsage(const CpuOveruseOptions& options) |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 75 | : kWeightFactorFrameDiff(0.998f), |
| 76 | kWeightFactorProcessing(0.995f), |
| 77 | kInitialSampleDiffMs(40.0f), |
| 78 | count_(0), |
| 79 | options_(options), |
| 80 | max_sample_diff_ms_(kDefaultSampleDiffMs * kMaxSampleDiffMarginFactor), |
| 81 | filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)), |
| 82 | filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) { |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 83 | Reset(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 84 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 85 | virtual ~SendProcessingUsage() {} |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 86 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 87 | void Reset() override { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 88 | count_ = 0; |
| 89 | max_sample_diff_ms_ = kDefaultSampleDiffMs * kMaxSampleDiffMarginFactor; |
| 90 | filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff); |
| 91 | filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs); |
| 92 | filtered_processing_ms_->Reset(kWeightFactorProcessing); |
| 93 | filtered_processing_ms_->Apply(1.0f, InitialProcessingMs()); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 96 | void SetMaxSampleDiffMs(float diff_ms) override { |
| 97 | max_sample_diff_ms_ = diff_ms; |
| 98 | } |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 99 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 100 | void AddCaptureSample(float sample_ms) override { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 101 | float exp = sample_ms / kDefaultSampleDiffMs; |
| 102 | exp = std::min(exp, kMaxExp); |
| 103 | filtered_frame_diff_ms_->Apply(exp, sample_ms); |
| 104 | } |
| 105 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 106 | void AddSample(float processing_ms, int64_t diff_last_sample_ms) override { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 107 | ++count_; |
| 108 | float exp = diff_last_sample_ms / kDefaultSampleDiffMs; |
| 109 | exp = std::min(exp, kMaxExp); |
| 110 | filtered_processing_ms_->Apply(exp, processing_ms); |
| 111 | } |
| 112 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 113 | int Value() override { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 114 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
| 115 | return static_cast<int>(InitialUsageInPercent() + 0.5f); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 116 | } |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 117 | float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f); |
| 118 | frame_diff_ms = std::min(frame_diff_ms, max_sample_diff_ms_); |
| 119 | float encode_usage_percent = |
| 120 | 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms; |
| 121 | return static_cast<int>(encode_usage_percent + 0.5); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 122 | } |
| 123 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 124 | private: |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 125 | 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 | |
| 131 | float InitialProcessingMs() const { |
| 132 | return InitialUsageInPercent() * kInitialSampleDiffMs / 100; |
| 133 | } |
| 134 | |
| 135 | const float kWeightFactorFrameDiff; |
| 136 | const float kWeightFactorProcessing; |
| 137 | const float kInitialSampleDiffMs; |
| 138 | uint64_t count_; |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 139 | const CpuOveruseOptions options_; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 140 | float max_sample_diff_ms_; |
| 141 | std::unique_ptr<rtc::ExpFilter> filtered_processing_ms_; |
| 142 | std::unique_ptr<rtc::ExpFilter> filtered_frame_diff_ms_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 145 | // Class used for manual testing of overuse, enabled via field trial flag. |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 146 | class OverdoseInjector : public SendProcessingUsage { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 147 | public: |
| 148 | OverdoseInjector(const CpuOveruseOptions& options, |
| 149 | int64_t normal_period_ms, |
| 150 | int64_t overuse_period_ms, |
| 151 | int64_t underuse_period_ms) |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 152 | : SendProcessingUsage(options), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 153 | normal_period_ms_(normal_period_ms), |
| 154 | overuse_period_ms_(overuse_period_ms), |
| 155 | underuse_period_ms_(underuse_period_ms), |
| 156 | state_(State::kNormal), |
| 157 | last_toggling_ms_(-1) { |
| 158 | RTC_DCHECK_GT(overuse_period_ms, 0); |
| 159 | RTC_DCHECK_GT(normal_period_ms, 0); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 160 | RTC_LOG(LS_INFO) << "Simulating overuse with intervals " << normal_period_ms |
| 161 | << "ms normal mode, " << overuse_period_ms |
| 162 | << "ms overuse mode."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | ~OverdoseInjector() override {} |
| 166 | |
| 167 | int Value() override { |
| 168 | int64_t now_ms = rtc::TimeMillis(); |
| 169 | if (last_toggling_ms_ == -1) { |
| 170 | last_toggling_ms_ = now_ms; |
| 171 | } else { |
| 172 | switch (state_) { |
| 173 | case State::kNormal: |
| 174 | if (now_ms > last_toggling_ms_ + normal_period_ms_) { |
| 175 | state_ = State::kOveruse; |
| 176 | last_toggling_ms_ = now_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 177 | RTC_LOG(LS_INFO) << "Simulating CPU overuse."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 178 | } |
| 179 | break; |
| 180 | case State::kOveruse: |
| 181 | if (now_ms > last_toggling_ms_ + overuse_period_ms_) { |
| 182 | state_ = State::kUnderuse; |
| 183 | last_toggling_ms_ = now_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 184 | RTC_LOG(LS_INFO) << "Simulating CPU underuse."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 185 | } |
| 186 | break; |
| 187 | case State::kUnderuse: |
| 188 | if (now_ms > last_toggling_ms_ + underuse_period_ms_) { |
| 189 | state_ = State::kNormal; |
| 190 | last_toggling_ms_ = now_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 191 | RTC_LOG(LS_INFO) << "Actual CPU overuse measurements in effect."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 192 | } |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | rtc::Optional<int> overried_usage_value; |
| 198 | switch (state_) { |
| 199 | case State::kNormal: |
| 200 | break; |
| 201 | case State::kOveruse: |
| 202 | overried_usage_value.emplace(250); |
| 203 | break; |
| 204 | case State::kUnderuse: |
| 205 | overried_usage_value.emplace(5); |
| 206 | break; |
| 207 | } |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 208 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 209 | return overried_usage_value.value_or(SendProcessingUsage::Value()); |
| 210 | } |
| 211 | |
| 212 | private: |
| 213 | const int64_t normal_period_ms_; |
| 214 | const int64_t overuse_period_ms_; |
| 215 | const int64_t underuse_period_ms_; |
| 216 | enum class State { kNormal, kOveruse, kUnderuse } state_; |
| 217 | int64_t last_toggling_ms_; |
| 218 | }; |
| 219 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 220 | } // namespace |
| 221 | |
| 222 | CpuOveruseOptions::CpuOveruseOptions() |
| 223 | : high_encode_usage_threshold_percent(85), |
| 224 | frame_timeout_interval_ms(1500), |
| 225 | min_frame_samples(120), |
| 226 | min_process_count(3), |
| 227 | high_threshold_consecutive_count(2) { |
| 228 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 229 | // This is proof-of-concept code for letting the physical core count affect |
| 230 | // the interval into which we attempt to scale. For now, the code is Mac OS |
| 231 | // specific, since that's the platform were we saw most problems. |
| 232 | // TODO(torbjorng): Enhance SystemInfo to return this metric. |
| 233 | |
| 234 | mach_port_t mach_host = mach_host_self(); |
| 235 | host_basic_info hbi = {}; |
| 236 | mach_msg_type_number_t info_count = HOST_BASIC_INFO_COUNT; |
| 237 | kern_return_t kr = |
| 238 | host_info(mach_host, HOST_BASIC_INFO, reinterpret_cast<host_info_t>(&hbi), |
| 239 | &info_count); |
| 240 | mach_port_deallocate(mach_task_self(), mach_host); |
| 241 | |
| 242 | int n_physical_cores; |
| 243 | if (kr != KERN_SUCCESS) { |
| 244 | // If we couldn't get # of physical CPUs, don't panic. Assume we have 1. |
| 245 | n_physical_cores = 1; |
| 246 | RTC_LOG(LS_ERROR) |
| 247 | << "Failed to determine number of physical cores, assuming 1"; |
| 248 | } else { |
| 249 | n_physical_cores = hbi.physical_cpu; |
| 250 | RTC_LOG(LS_INFO) << "Number of physical cores:" << n_physical_cores; |
| 251 | } |
| 252 | |
| 253 | // Change init list default for few core systems. The assumption here is that |
| 254 | // encoding, which we measure here, takes about 1/4 of the processing of a |
| 255 | // two-way call. This is roughly true for x86 using both vp8 and vp9 without |
| 256 | // hardware encoding. Since we don't affect the incoming stream here, we only |
| 257 | // control about 1/2 of the total processing needs, but this is not taken into |
| 258 | // account. |
| 259 | if (n_physical_cores == 1) |
| 260 | high_encode_usage_threshold_percent = 20; // Roughly 1/4 of 100%. |
| 261 | else if (n_physical_cores == 2) |
| 262 | high_encode_usage_threshold_percent = 40; // Roughly 1/4 of 200%. |
| 263 | #endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 264 | |
| 265 | // Note that we make the interval 2x+epsilon wide, since libyuv scaling steps |
| 266 | // are close to that (when squared). This wide interval makes sure that |
| 267 | // scaling up or down does not jump all the way across the interval. |
| 268 | low_encode_usage_threshold_percent = |
| 269 | (high_encode_usage_threshold_percent - 1) / 2; |
| 270 | } |
| 271 | |
| 272 | std::unique_ptr<OveruseFrameDetector::ProcessingUsage> |
| 273 | OveruseFrameDetector::CreateProcessingUsage(const CpuOveruseOptions& options) { |
| 274 | std::unique_ptr<ProcessingUsage> instance; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 275 | std::string toggling_interval = |
| 276 | field_trial::FindFullName("WebRTC-ForceSimulatedOveruseIntervalMs"); |
| 277 | if (!toggling_interval.empty()) { |
| 278 | int normal_period_ms = 0; |
| 279 | int overuse_period_ms = 0; |
| 280 | int underuse_period_ms = 0; |
| 281 | if (sscanf(toggling_interval.c_str(), "%d-%d-%d", &normal_period_ms, |
| 282 | &overuse_period_ms, &underuse_period_ms) == 3) { |
| 283 | if (normal_period_ms > 0 && overuse_period_ms > 0 && |
| 284 | underuse_period_ms > 0) { |
| 285 | instance.reset(new OverdoseInjector( |
| 286 | options, normal_period_ms, overuse_period_ms, underuse_period_ms)); |
| 287 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 288 | RTC_LOG(LS_WARNING) |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 289 | << "Invalid (non-positive) normal/overuse/underuse periods: " |
| 290 | << normal_period_ms << " / " << overuse_period_ms << " / " |
| 291 | << underuse_period_ms; |
| 292 | } |
| 293 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 294 | RTC_LOG(LS_WARNING) << "Malformed toggling interval: " |
| 295 | << toggling_interval; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | if (!instance) { |
| 300 | // No valid overuse simulation parameters set, use normal usage class. |
| 301 | instance.reset(new SendProcessingUsage(options)); |
| 302 | } |
| 303 | |
| 304 | return instance; |
| 305 | } |
| 306 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 307 | class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask { |
| 308 | public: |
| 309 | explicit CheckOveruseTask(OveruseFrameDetector* overuse_detector) |
| 310 | : overuse_detector_(overuse_detector) { |
| 311 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 312 | std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs); |
| 313 | } |
| 314 | |
| 315 | void Stop() { |
| 316 | RTC_CHECK(task_checker_.CalledSequentially()); |
| 317 | overuse_detector_ = nullptr; |
| 318 | } |
| 319 | |
| 320 | private: |
| 321 | bool Run() override { |
| 322 | RTC_CHECK(task_checker_.CalledSequentially()); |
| 323 | if (!overuse_detector_) |
| 324 | return true; // This will make the task queue delete this task. |
| 325 | overuse_detector_->CheckForOveruse(); |
| 326 | |
| 327 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 328 | std::unique_ptr<rtc::QueuedTask>(this), kCheckForOveruseIntervalMs); |
| 329 | // Return false to prevent this task from being deleted. Ownership has been |
| 330 | // transferred to the task queue when PostDelayedTask was called. |
| 331 | return false; |
| 332 | } |
| 333 | rtc::SequencedTaskChecker task_checker_; |
| 334 | OveruseFrameDetector* overuse_detector_; |
| 335 | }; |
| 336 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 337 | OveruseFrameDetector::OveruseFrameDetector( |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 338 | const CpuOveruseOptions& options, |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 339 | AdaptationObserverInterface* observer, |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 340 | EncodedFrameObserver* encoder_timing, |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 341 | CpuOveruseMetricsObserver* metrics_observer) |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 342 | : check_overuse_task_(nullptr), |
| 343 | options_(options), |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 344 | observer_(observer), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 345 | encoder_timing_(encoder_timing), |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 346 | metrics_observer_(metrics_observer), |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 347 | num_process_times_(0), |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 348 | // TODO(nisse): Use rtc::Optional |
| 349 | last_capture_time_us_(-1), |
| 350 | last_processed_capture_time_us_(-1), |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 351 | num_pixels_(0), |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 352 | max_framerate_(kDefaultFrameRate), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 353 | last_overuse_time_ms_(-1), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 354 | checks_above_threshold_(0), |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 355 | num_overuse_detections_(0), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 356 | last_rampup_time_ms_(-1), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 357 | in_quick_rampup_(false), |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 358 | current_rampup_delay_ms_(kStandardRampUpDelayMs), |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame^] | 359 | usage_(CreateProcessingUsage(options)) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 360 | task_checker_.Detach(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 361 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 362 | |
| 363 | OveruseFrameDetector::~OveruseFrameDetector() { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 364 | RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called."; |
| 365 | } |
| 366 | |
| 367 | void OveruseFrameDetector::StartCheckForOveruse() { |
| 368 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 369 | RTC_DCHECK(!check_overuse_task_); |
| 370 | check_overuse_task_ = new CheckOveruseTask(this); |
| 371 | } |
| 372 | void OveruseFrameDetector::StopCheckForOveruse() { |
| 373 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 374 | check_overuse_task_->Stop(); |
| 375 | check_overuse_task_ = nullptr; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 378 | void OveruseFrameDetector::EncodedFrameTimeMeasured(int encode_duration_ms) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 379 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 380 | if (!metrics_) |
| 381 | metrics_ = rtc::Optional<CpuOveruseMetrics>(CpuOveruseMetrics()); |
| 382 | metrics_->encode_usage_percent = usage_->Value(); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 383 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 384 | metrics_observer_->OnEncodedFrameTimeMeasured(encode_duration_ms, *metrics_); |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 385 | } |
| 386 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 387 | bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 388 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 389 | if (num_pixels != num_pixels_) { |
| 390 | return true; |
| 391 | } |
| 392 | return false; |
| 393 | } |
| 394 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 395 | bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now_us) const { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 396 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 397 | if (last_capture_time_us_ == -1) |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 398 | return false; |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 399 | return (now_us - last_capture_time_us_) > |
| 400 | options_.frame_timeout_interval_ms * rtc::kNumMicrosecsPerMillisec; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 403 | void OveruseFrameDetector::ResetAll(int num_pixels) { |
| 404 | // Reset state, as a result resolution being changed. Do not however change |
| 405 | // the current frame rate back to the default. |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 406 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 407 | num_pixels_ = num_pixels; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 408 | usage_->Reset(); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 409 | frame_timing_.clear(); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 410 | last_capture_time_us_ = -1; |
| 411 | last_processed_capture_time_us_ = -1; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 412 | num_process_times_ = 0; |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 413 | metrics_ = rtc::Optional<CpuOveruseMetrics>(); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 414 | OnTargetFramerateUpdated(max_framerate_); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 417 | void OveruseFrameDetector::OnTargetFramerateUpdated(int framerate_fps) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 418 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 419 | RTC_DCHECK_GE(framerate_fps, 0); |
| 420 | max_framerate_ = std::min(kMaxFramerate, framerate_fps); |
| 421 | usage_->SetMaxSampleDiffMs((1000 / std::max(kMinFramerate, max_framerate_)) * |
| 422 | kMaxSampleDiffMarginFactor); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 425 | void OveruseFrameDetector::FrameCaptured(const VideoFrame& frame, |
| 426 | int64_t time_when_first_seen_us) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 427 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame] | 428 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 429 | if (FrameSizeChanged(frame.width() * frame.height()) || |
| 430 | FrameTimeoutDetected(time_when_first_seen_us)) { |
| 431 | ResetAll(frame.width() * frame.height()); |
| 432 | } |
| 433 | |
| 434 | if (last_capture_time_us_ != -1) |
| 435 | usage_->AddCaptureSample( |
| 436 | 1e-3 * (time_when_first_seen_us - last_capture_time_us_)); |
| 437 | |
| 438 | last_capture_time_us_ = time_when_first_seen_us; |
| 439 | |
| 440 | frame_timing_.push_back(FrameTiming(frame.timestamp_us(), frame.timestamp(), |
| 441 | time_when_first_seen_us)); |
| 442 | } |
| 443 | |
| 444 | void OveruseFrameDetector::FrameSent(uint32_t timestamp, |
| 445 | int64_t time_sent_in_us) { |
| 446 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 447 | // Delay before reporting actual encoding time, used to have the ability to |
| 448 | // detect total encoding time when encoding more than one layer. Encoding is |
| 449 | // here assumed to finish within a second (or that we get enough long-time |
| 450 | // samples before one second to trigger an overuse even when this is not the |
| 451 | // case). |
| 452 | static const int64_t kEncodingTimeMeasureWindowMs = 1000; |
| 453 | for (auto& it : frame_timing_) { |
| 454 | if (it.timestamp == timestamp) { |
| 455 | it.last_send_us = time_sent_in_us; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | // TODO(pbos): Handle the case/log errors when not finding the corresponding |
| 460 | // frame (either very slow encoding or incorrect wrong timestamps returned |
| 461 | // from the encoder). |
| 462 | // This is currently the case for all frames on ChromeOS, so logging them |
| 463 | // would be spammy, and triggering overuse would be wrong. |
| 464 | // https://crbug.com/350106 |
| 465 | while (!frame_timing_.empty()) { |
| 466 | FrameTiming timing = frame_timing_.front(); |
| 467 | if (time_sent_in_us - timing.capture_us < |
| 468 | kEncodingTimeMeasureWindowMs * rtc::kNumMicrosecsPerMillisec) { |
| 469 | break; |
| 470 | } |
| 471 | if (timing.last_send_us != -1) { |
| 472 | int encode_duration_us = |
| 473 | static_cast<int>(timing.last_send_us - timing.capture_us); |
| 474 | if (encoder_timing_) { |
| 475 | // TODO(nisse): Update encoder_timing_ to also use us units. |
| 476 | encoder_timing_->OnEncodeTiming(timing.capture_time_us / |
| 477 | rtc::kNumMicrosecsPerMillisec, |
| 478 | encode_duration_us / |
| 479 | rtc::kNumMicrosecsPerMillisec); |
| 480 | } |
| 481 | if (last_processed_capture_time_us_ != -1) { |
| 482 | int64_t diff_us = timing.capture_us - last_processed_capture_time_us_; |
| 483 | usage_->AddSample(1e-3 * encode_duration_us, 1e-3 * diff_us); |
| 484 | } |
| 485 | last_processed_capture_time_us_ = timing.capture_us; |
| 486 | EncodedFrameTimeMeasured(encode_duration_us / |
| 487 | rtc::kNumMicrosecsPerMillisec); |
| 488 | } |
| 489 | frame_timing_.pop_front(); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 490 | } |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 491 | } |
| 492 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 493 | void OveruseFrameDetector::CheckForOveruse() { |
| 494 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 495 | ++num_process_times_; |
| 496 | if (num_process_times_ <= options_.min_process_count || !metrics_) |
| 497 | return; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 498 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 499 | int64_t now_ms = rtc::TimeMillis(); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 500 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 501 | if (IsOverusing(*metrics_)) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 502 | // If the last thing we did was going up, and now have to back down, we need |
| 503 | // to check if this peak was short. If so we should back off to avoid going |
| 504 | // back and forth between this load, the system doesn't seem to handle it. |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 505 | bool check_for_backoff = last_rampup_time_ms_ > last_overuse_time_ms_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 506 | if (check_for_backoff) { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 507 | if (now_ms - last_rampup_time_ms_ < kStandardRampUpDelayMs || |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 508 | num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 509 | // Going up was not ok for very long, back off. |
| 510 | current_rampup_delay_ms_ *= kRampUpBackoffFactor; |
| 511 | if (current_rampup_delay_ms_ > kMaxRampUpDelayMs) |
| 512 | current_rampup_delay_ms_ = kMaxRampUpDelayMs; |
| 513 | } else { |
| 514 | // Not currently backing off, reset rampup delay. |
| 515 | current_rampup_delay_ms_ = kStandardRampUpDelayMs; |
| 516 | } |
| 517 | } |
| 518 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 519 | last_overuse_time_ms_ = now_ms; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 520 | in_quick_rampup_ = false; |
| 521 | checks_above_threshold_ = 0; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 522 | ++num_overuse_detections_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 523 | |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 524 | if (observer_) |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 525 | observer_->AdaptDown(kScaleReasonCpu); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 526 | } else if (IsUnderusing(*metrics_, now_ms)) { |
| 527 | last_rampup_time_ms_ = now_ms; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 528 | in_quick_rampup_ = true; |
| 529 | |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 530 | if (observer_) |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 531 | observer_->AdaptUp(kScaleReasonCpu); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 532 | } |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 533 | |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 534 | int rampup_delay = |
| 535 | in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 536 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 537 | RTC_LOG(LS_VERBOSE) << " Frame stats: " |
| 538 | << " encode usage " << metrics_->encode_usage_percent |
| 539 | << " overuse detections " << num_overuse_detections_ |
| 540 | << " rampup delay " << rampup_delay; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 541 | } |
| 542 | |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 543 | bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 544 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 545 | |
Peter Boström | 01f364e | 2016-01-07 16:38:25 +0100 | [diff] [blame] | 546 | if (metrics.encode_usage_percent >= |
| 547 | options_.high_encode_usage_threshold_percent) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 548 | ++checks_above_threshold_; |
| 549 | } else { |
| 550 | checks_above_threshold_ = 0; |
| 551 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 552 | return checks_above_threshold_ >= options_.high_threshold_consecutive_count; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 553 | } |
| 554 | |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 555 | bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics, |
| 556 | int64_t time_now) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 557 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 558 | int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 559 | if (time_now < last_rampup_time_ms_ + delay) |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 560 | return false; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 561 | |
Peter Boström | 01f364e | 2016-01-07 16:38:25 +0100 | [diff] [blame] | 562 | return metrics.encode_usage_percent < |
| 563 | options_.low_encode_usage_threshold_percent; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 564 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 565 | } // namespace webrtc |