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 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 13 | #include <math.h> |
Piotr Tworek | 5e4833c | 2017-12-12 12:09:31 +0100 | [diff] [blame] | 14 | #include <stdio.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" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
| 24 | #include "rtc_base/logging.h" |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 25 | #include "rtc_base/numerics/exp_filter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/timeutils.h" |
| 27 | #include "system_wrappers/include/field_trial.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 28 | |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 29 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 30 | #include <mach/mach.h> |
pbos | a102507 | 2016-05-14 03:04:19 -0700 | [diff] [blame] | 31 | #endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 32 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 33 | namespace webrtc { |
| 34 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 35 | namespace { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 36 | const int64_t kCheckForOveruseIntervalMs = 5000; |
| 37 | const int64_t kTimeToFirstCheckForOveruseMs = 100; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 38 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 39 | // Delay between consecutive rampups. (Used for quick recovery.) |
| 40 | const int kQuickRampUpDelayMs = 10 * 1000; |
| 41 | // Delay between rampup attempts. Initially uses standard, scales up to max. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 42 | const int kStandardRampUpDelayMs = 40 * 1000; |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 43 | const int kMaxRampUpDelayMs = 240 * 1000; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 44 | // Expontential back-off factor, to prevent annoying up-down behaviour. |
| 45 | const double kRampUpBackoffFactor = 2.0; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 46 | |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 47 | // Max number of overuses detected before always applying the rampup delay. |
asapersson@webrtc.org | 23a4d85 | 2014-08-13 14:33:49 +0000 | [diff] [blame] | 48 | const int kMaxOverusesBeforeApplyRampupDelay = 4; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 49 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 50 | // The maximum exponent to use in VCMExpFilter. |
| 51 | const float kMaxExp = 7.0f; |
| 52 | // Default value used before first reconfiguration. |
| 53 | const int kDefaultFrameRate = 30; |
| 54 | // Default sample diff, default frame rate. |
| 55 | const float kDefaultSampleDiffMs = 1000.0f / kDefaultFrameRate; |
| 56 | // A factor applied to the sample diff on OnTargetFramerateUpdated to determine |
| 57 | // a max limit for the sample diff. For instance, with a framerate of 30fps, |
| 58 | // the sample diff is capped to (1000 / 30) * 1.35 = 45ms. This prevents |
| 59 | // triggering too soon if there are individual very large outliers. |
| 60 | const float kMaxSampleDiffMarginFactor = 1.35f; |
| 61 | // Minimum framerate allowed for usage calculation. This prevents crazy long |
| 62 | // encode times from being accepted if the frame rate happens to be low. |
| 63 | const int kMinFramerate = 7; |
| 64 | const int kMaxFramerate = 30; |
| 65 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 66 | const auto kScaleReasonCpu = AdaptationObserverInterface::AdaptReason::kCpu; |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 67 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 68 | // Class for calculating the processing usage on the send-side (the average |
| 69 | // processing time of a frame divided by the average time difference between |
| 70 | // captured frames). |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 71 | class SendProcessingUsage1 : public OveruseFrameDetector::ProcessingUsage { |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 72 | public: |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 73 | explicit SendProcessingUsage1(const CpuOveruseOptions& options) |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 74 | : kWeightFactorFrameDiff(0.998f), |
| 75 | kWeightFactorProcessing(0.995f), |
| 76 | kInitialSampleDiffMs(40.0f), |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 77 | options_(options), |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 78 | count_(0), |
| 79 | last_processed_capture_time_us_(-1), |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 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 | } |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 85 | virtual ~SendProcessingUsage1() {} |
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 | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 88 | frame_timing_.clear(); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 89 | count_ = 0; |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 90 | last_processed_capture_time_us_ = -1; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 91 | max_sample_diff_ms_ = kDefaultSampleDiffMs * kMaxSampleDiffMarginFactor; |
| 92 | filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff); |
| 93 | filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs); |
| 94 | filtered_processing_ms_->Reset(kWeightFactorProcessing); |
| 95 | filtered_processing_ms_->Apply(1.0f, InitialProcessingMs()); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame] | 98 | void SetMaxSampleDiffMs(float diff_ms) override { |
| 99 | max_sample_diff_ms_ = diff_ms; |
| 100 | } |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 101 | |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 102 | void FrameCaptured(const VideoFrame& frame, |
| 103 | int64_t time_when_first_seen_us, |
| 104 | int64_t last_capture_time_us) override { |
| 105 | if (last_capture_time_us != -1) |
| 106 | AddCaptureSample(1e-3 * (time_when_first_seen_us - last_capture_time_us)); |
| 107 | |
| 108 | frame_timing_.push_back(FrameTiming(frame.timestamp_us(), frame.timestamp(), |
| 109 | time_when_first_seen_us)); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 112 | absl::optional<int> FrameSent( |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 113 | uint32_t timestamp, |
| 114 | int64_t time_sent_in_us, |
| 115 | int64_t /* capture_time_us */, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 116 | absl::optional<int> /* encode_duration_us */) override { |
| 117 | absl::optional<int> encode_duration_us; |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 118 | // Delay before reporting actual encoding time, used to have the ability to |
| 119 | // detect total encoding time when encoding more than one layer. Encoding is |
| 120 | // here assumed to finish within a second (or that we get enough long-time |
| 121 | // samples before one second to trigger an overuse even when this is not the |
| 122 | // case). |
| 123 | static const int64_t kEncodingTimeMeasureWindowMs = 1000; |
| 124 | for (auto& it : frame_timing_) { |
| 125 | if (it.timestamp == timestamp) { |
| 126 | it.last_send_us = time_sent_in_us; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | // TODO(pbos): Handle the case/log errors when not finding the corresponding |
| 131 | // frame (either very slow encoding or incorrect wrong timestamps returned |
| 132 | // from the encoder). |
| 133 | // This is currently the case for all frames on ChromeOS, so logging them |
| 134 | // would be spammy, and triggering overuse would be wrong. |
| 135 | // https://crbug.com/350106 |
| 136 | while (!frame_timing_.empty()) { |
| 137 | FrameTiming timing = frame_timing_.front(); |
| 138 | if (time_sent_in_us - timing.capture_us < |
| 139 | kEncodingTimeMeasureWindowMs * rtc::kNumMicrosecsPerMillisec) { |
| 140 | break; |
| 141 | } |
| 142 | if (timing.last_send_us != -1) { |
| 143 | encode_duration_us.emplace( |
| 144 | static_cast<int>(timing.last_send_us - timing.capture_us)); |
Niels Möller | 6b642f7 | 2017-12-08 14:11:14 +0100 | [diff] [blame] | 145 | |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 146 | if (last_processed_capture_time_us_ != -1) { |
| 147 | int64_t diff_us = timing.capture_us - last_processed_capture_time_us_; |
| 148 | AddSample(1e-3 * (*encode_duration_us), 1e-3 * diff_us); |
| 149 | } |
| 150 | last_processed_capture_time_us_ = timing.capture_us; |
| 151 | } |
| 152 | frame_timing_.pop_front(); |
| 153 | } |
| 154 | return encode_duration_us; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame] | 157 | int Value() override { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 158 | if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
| 159 | return static_cast<int>(InitialUsageInPercent() + 0.5f); |
asapersson@webrtc.org | ce12f1f | 2014-03-24 21:59:16 +0000 | [diff] [blame] | 160 | } |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 161 | float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f); |
| 162 | frame_diff_ms = std::min(frame_diff_ms, max_sample_diff_ms_); |
| 163 | float encode_usage_percent = |
| 164 | 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms; |
| 165 | return static_cast<int>(encode_usage_percent + 0.5); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 166 | } |
| 167 | |
asapersson@webrtc.org | 2881ab1 | 2014-06-12 08:46:46 +0000 | [diff] [blame] | 168 | private: |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 169 | struct FrameTiming { |
| 170 | FrameTiming(int64_t capture_time_us, uint32_t timestamp, int64_t now) |
| 171 | : capture_time_us(capture_time_us), |
| 172 | timestamp(timestamp), |
| 173 | capture_us(now), |
| 174 | last_send_us(-1) {} |
| 175 | int64_t capture_time_us; |
| 176 | uint32_t timestamp; |
| 177 | int64_t capture_us; |
| 178 | int64_t last_send_us; |
| 179 | }; |
| 180 | |
| 181 | void AddCaptureSample(float sample_ms) { |
| 182 | float exp = sample_ms / kDefaultSampleDiffMs; |
| 183 | exp = std::min(exp, kMaxExp); |
| 184 | filtered_frame_diff_ms_->Apply(exp, sample_ms); |
| 185 | } |
| 186 | |
| 187 | void AddSample(float processing_ms, int64_t diff_last_sample_ms) { |
| 188 | ++count_; |
| 189 | float exp = diff_last_sample_ms / kDefaultSampleDiffMs; |
| 190 | exp = std::min(exp, kMaxExp); |
| 191 | filtered_processing_ms_->Apply(exp, processing_ms); |
| 192 | } |
| 193 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 194 | float InitialUsageInPercent() const { |
| 195 | // Start in between the underuse and overuse threshold. |
| 196 | return (options_.low_encode_usage_threshold_percent + |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 197 | options_.high_encode_usage_threshold_percent) / |
| 198 | 2.0f; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | float InitialProcessingMs() const { |
| 202 | return InitialUsageInPercent() * kInitialSampleDiffMs / 100; |
| 203 | } |
| 204 | |
| 205 | const float kWeightFactorFrameDiff; |
| 206 | const float kWeightFactorProcessing; |
| 207 | const float kInitialSampleDiffMs; |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 208 | |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 209 | const CpuOveruseOptions options_; |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 210 | std::list<FrameTiming> frame_timing_; |
| 211 | uint64_t count_; |
| 212 | int64_t last_processed_capture_time_us_; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 213 | float max_sample_diff_ms_; |
| 214 | std::unique_ptr<rtc::ExpFilter> filtered_processing_ms_; |
| 215 | std::unique_ptr<rtc::ExpFilter> filtered_frame_diff_ms_; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 218 | // New cpu load estimator. |
| 219 | // TODO(bugs.webrtc.org/8504): For some period of time, we need to |
| 220 | // switch between the two versions of the estimator for experiments. |
| 221 | // When problems are sorted out, the old estimator should be deleted. |
| 222 | class SendProcessingUsage2 : public OveruseFrameDetector::ProcessingUsage { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 223 | public: |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 224 | explicit SendProcessingUsage2(const CpuOveruseOptions& options) |
| 225 | : options_(options) { |
| 226 | Reset(); |
| 227 | } |
| 228 | virtual ~SendProcessingUsage2() = default; |
| 229 | |
| 230 | void Reset() override { |
| 231 | prev_time_us_ = -1; |
| 232 | // Start in between the underuse and overuse threshold. |
| 233 | load_estimate_ = (options_.low_encode_usage_threshold_percent + |
| 234 | options_.high_encode_usage_threshold_percent) / |
| 235 | 200.0; |
| 236 | } |
| 237 | |
| 238 | void SetMaxSampleDiffMs(float /* diff_ms */) override {} |
| 239 | |
| 240 | void FrameCaptured(const VideoFrame& frame, |
| 241 | int64_t time_when_first_seen_us, |
| 242 | int64_t last_capture_time_us) override {} |
| 243 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 244 | absl::optional<int> FrameSent( |
Niels Möller | f5033ad | 2018-08-14 17:00:46 +0200 | [diff] [blame] | 245 | uint32_t /* timestamp */, |
| 246 | int64_t /* time_sent_in_us */, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 247 | int64_t capture_time_us, |
| 248 | absl::optional<int> encode_duration_us) override { |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 249 | if (encode_duration_us) { |
Niels Möller | f5033ad | 2018-08-14 17:00:46 +0200 | [diff] [blame] | 250 | int duration_per_frame_us = |
| 251 | DurationPerInputFrame(capture_time_us, *encode_duration_us); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 252 | if (prev_time_us_ != -1) { |
Niels Möller | 58d2a5e | 2018-08-07 16:37:18 +0200 | [diff] [blame] | 253 | if (capture_time_us < prev_time_us_) { |
| 254 | // The weighting in AddSample assumes that samples are processed with |
| 255 | // non-decreasing measurement timestamps. We could implement |
| 256 | // appropriate weights for samples arriving late, but since it is a |
| 257 | // rare case, keep things simple, by just pushing those measurements a |
| 258 | // bit forward in time. |
| 259 | capture_time_us = prev_time_us_; |
| 260 | } |
Niels Möller | f5033ad | 2018-08-14 17:00:46 +0200 | [diff] [blame] | 261 | AddSample(1e-6 * duration_per_frame_us, |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 262 | 1e-6 * (capture_time_us - prev_time_us_)); |
| 263 | } |
| 264 | } |
| 265 | prev_time_us_ = capture_time_us; |
| 266 | |
| 267 | return encode_duration_us; |
| 268 | } |
| 269 | |
| 270 | private: |
| 271 | void AddSample(double encode_time, double diff_time) { |
| 272 | RTC_CHECK_GE(diff_time, 0.0); |
| 273 | |
| 274 | // Use the filter update |
| 275 | // |
| 276 | // load <-- x/d (1-exp (-d/T)) + exp (-d/T) load |
| 277 | // |
| 278 | // where we must take care for small d, using the proper limit |
| 279 | // (1 - exp(-d/tau)) / d = 1/tau - d/2tau^2 + O(d^2) |
| 280 | double tau = (1e-3 * options_.filter_time_ms); |
| 281 | double e = diff_time / tau; |
| 282 | double c; |
| 283 | if (e < 0.0001) { |
| 284 | c = (1 - e / 2) / tau; |
| 285 | } else { |
| 286 | c = -expm1(-e) / diff_time; |
| 287 | } |
| 288 | load_estimate_ = c * encode_time + exp(-e) * load_estimate_; |
| 289 | } |
| 290 | |
Niels Möller | f5033ad | 2018-08-14 17:00:46 +0200 | [diff] [blame] | 291 | int64_t DurationPerInputFrame(int64_t capture_time_us, |
| 292 | int64_t encode_time_us) { |
| 293 | // Discard data on old frames; limit 2 seconds. |
| 294 | static constexpr int64_t kMaxAge = 2 * rtc::kNumMicrosecsPerSec; |
| 295 | for (auto it = max_encode_time_per_input_frame_.begin(); |
| 296 | it != max_encode_time_per_input_frame_.end() && |
| 297 | it->first < capture_time_us - kMaxAge;) { |
| 298 | it = max_encode_time_per_input_frame_.erase(it); |
| 299 | } |
| 300 | |
| 301 | std::map<int64_t, int>::iterator it; |
| 302 | bool inserted; |
| 303 | std::tie(it, inserted) = max_encode_time_per_input_frame_.emplace( |
| 304 | capture_time_us, encode_time_us); |
| 305 | if (inserted) { |
| 306 | // First encoded frame for this input frame. |
| 307 | return encode_time_us; |
| 308 | } |
| 309 | if (encode_time_us <= it->second) { |
| 310 | // Shorter encode time than previous frame (unlikely). Count it as being |
| 311 | // done in parallel. |
| 312 | return 0; |
| 313 | } |
| 314 | // Record new maximum encode time, and return increase from previous max. |
| 315 | int increase = encode_time_us - it->second; |
| 316 | it->second = encode_time_us; |
| 317 | return increase; |
| 318 | } |
| 319 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 320 | int Value() override { |
| 321 | return static_cast<int>(100.0 * load_estimate_ + 0.5); |
| 322 | } |
| 323 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 324 | const CpuOveruseOptions options_; |
Niels Möller | f5033ad | 2018-08-14 17:00:46 +0200 | [diff] [blame] | 325 | // Indexed by the capture timestamp, used as frame id. |
| 326 | std::map<int64_t, int> max_encode_time_per_input_frame_; |
| 327 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 328 | int64_t prev_time_us_ = -1; |
| 329 | double load_estimate_; |
| 330 | }; |
| 331 | |
| 332 | // Class used for manual testing of overuse, enabled via field trial flag. |
| 333 | class OverdoseInjector : public OveruseFrameDetector::ProcessingUsage { |
| 334 | public: |
| 335 | OverdoseInjector(std::unique_ptr<OveruseFrameDetector::ProcessingUsage> usage, |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 336 | int64_t normal_period_ms, |
| 337 | int64_t overuse_period_ms, |
| 338 | int64_t underuse_period_ms) |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 339 | : usage_(std::move(usage)), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 340 | normal_period_ms_(normal_period_ms), |
| 341 | overuse_period_ms_(overuse_period_ms), |
| 342 | underuse_period_ms_(underuse_period_ms), |
| 343 | state_(State::kNormal), |
| 344 | last_toggling_ms_(-1) { |
| 345 | RTC_DCHECK_GT(overuse_period_ms, 0); |
| 346 | RTC_DCHECK_GT(normal_period_ms, 0); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 347 | RTC_LOG(LS_INFO) << "Simulating overuse with intervals " << normal_period_ms |
| 348 | << "ms normal mode, " << overuse_period_ms |
| 349 | << "ms overuse mode."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | ~OverdoseInjector() override {} |
| 353 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 354 | void Reset() override { usage_->Reset(); } |
| 355 | |
| 356 | void SetMaxSampleDiffMs(float diff_ms) override { |
| 357 | usage_->SetMaxSampleDiffMs(diff_ms); |
| 358 | } |
| 359 | |
| 360 | void FrameCaptured(const VideoFrame& frame, |
| 361 | int64_t time_when_first_seen_us, |
| 362 | int64_t last_capture_time_us) override { |
| 363 | usage_->FrameCaptured(frame, time_when_first_seen_us, last_capture_time_us); |
| 364 | } |
| 365 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 366 | absl::optional<int> FrameSent( |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 367 | // These two argument used by old estimator. |
| 368 | uint32_t timestamp, |
| 369 | int64_t time_sent_in_us, |
| 370 | // And these two by the new estimator. |
| 371 | int64_t capture_time_us, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 372 | absl::optional<int> encode_duration_us) override { |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 373 | return usage_->FrameSent(timestamp, time_sent_in_us, capture_time_us, |
| 374 | encode_duration_us); |
| 375 | } |
| 376 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 377 | int Value() override { |
| 378 | int64_t now_ms = rtc::TimeMillis(); |
| 379 | if (last_toggling_ms_ == -1) { |
| 380 | last_toggling_ms_ = now_ms; |
| 381 | } else { |
| 382 | switch (state_) { |
| 383 | case State::kNormal: |
| 384 | if (now_ms > last_toggling_ms_ + normal_period_ms_) { |
| 385 | state_ = State::kOveruse; |
| 386 | last_toggling_ms_ = now_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 387 | RTC_LOG(LS_INFO) << "Simulating CPU overuse."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 388 | } |
| 389 | break; |
| 390 | case State::kOveruse: |
| 391 | if (now_ms > last_toggling_ms_ + overuse_period_ms_) { |
| 392 | state_ = State::kUnderuse; |
| 393 | last_toggling_ms_ = now_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 394 | RTC_LOG(LS_INFO) << "Simulating CPU underuse."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 395 | } |
| 396 | break; |
| 397 | case State::kUnderuse: |
| 398 | if (now_ms > last_toggling_ms_ + underuse_period_ms_) { |
| 399 | state_ = State::kNormal; |
| 400 | last_toggling_ms_ = now_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 401 | RTC_LOG(LS_INFO) << "Actual CPU overuse measurements in effect."; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 402 | } |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 407 | absl::optional<int> overried_usage_value; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 408 | switch (state_) { |
| 409 | case State::kNormal: |
| 410 | break; |
| 411 | case State::kOveruse: |
| 412 | overried_usage_value.emplace(250); |
| 413 | break; |
| 414 | case State::kUnderuse: |
| 415 | overried_usage_value.emplace(5); |
| 416 | break; |
| 417 | } |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 418 | |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 419 | return overried_usage_value.value_or(usage_->Value()); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | private: |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 423 | const std::unique_ptr<OveruseFrameDetector::ProcessingUsage> usage_; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 424 | const int64_t normal_period_ms_; |
| 425 | const int64_t overuse_period_ms_; |
| 426 | const int64_t underuse_period_ms_; |
| 427 | enum class State { kNormal, kOveruse, kUnderuse } state_; |
| 428 | int64_t last_toggling_ms_; |
| 429 | }; |
| 430 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame] | 431 | } // namespace |
| 432 | |
| 433 | CpuOveruseOptions::CpuOveruseOptions() |
| 434 | : high_encode_usage_threshold_percent(85), |
| 435 | frame_timeout_interval_ms(1500), |
| 436 | min_frame_samples(120), |
| 437 | min_process_count(3), |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 438 | high_threshold_consecutive_count(2), |
| 439 | // Disabled by default. |
| 440 | filter_time_ms(0) { |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame] | 441 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 442 | // This is proof-of-concept code for letting the physical core count affect |
| 443 | // the interval into which we attempt to scale. For now, the code is Mac OS |
| 444 | // specific, since that's the platform were we saw most problems. |
| 445 | // TODO(torbjorng): Enhance SystemInfo to return this metric. |
| 446 | |
| 447 | mach_port_t mach_host = mach_host_self(); |
| 448 | host_basic_info hbi = {}; |
| 449 | mach_msg_type_number_t info_count = HOST_BASIC_INFO_COUNT; |
| 450 | kern_return_t kr = |
| 451 | host_info(mach_host, HOST_BASIC_INFO, reinterpret_cast<host_info_t>(&hbi), |
| 452 | &info_count); |
| 453 | mach_port_deallocate(mach_task_self(), mach_host); |
| 454 | |
| 455 | int n_physical_cores; |
| 456 | if (kr != KERN_SUCCESS) { |
| 457 | // If we couldn't get # of physical CPUs, don't panic. Assume we have 1. |
| 458 | n_physical_cores = 1; |
| 459 | RTC_LOG(LS_ERROR) |
| 460 | << "Failed to determine number of physical cores, assuming 1"; |
| 461 | } else { |
| 462 | n_physical_cores = hbi.physical_cpu; |
| 463 | RTC_LOG(LS_INFO) << "Number of physical cores:" << n_physical_cores; |
| 464 | } |
| 465 | |
| 466 | // Change init list default for few core systems. The assumption here is that |
| 467 | // encoding, which we measure here, takes about 1/4 of the processing of a |
| 468 | // two-way call. This is roughly true for x86 using both vp8 and vp9 without |
| 469 | // hardware encoding. Since we don't affect the incoming stream here, we only |
| 470 | // control about 1/2 of the total processing needs, but this is not taken into |
| 471 | // account. |
| 472 | if (n_physical_cores == 1) |
| 473 | high_encode_usage_threshold_percent = 20; // Roughly 1/4 of 100%. |
| 474 | else if (n_physical_cores == 2) |
| 475 | high_encode_usage_threshold_percent = 40; // Roughly 1/4 of 200%. |
| 476 | #endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 477 | |
| 478 | // Note that we make the interval 2x+epsilon wide, since libyuv scaling steps |
| 479 | // are close to that (when squared). This wide interval makes sure that |
| 480 | // scaling up or down does not jump all the way across the interval. |
| 481 | low_encode_usage_threshold_percent = |
| 482 | (high_encode_usage_threshold_percent - 1) / 2; |
| 483 | } |
| 484 | |
| 485 | std::unique_ptr<OveruseFrameDetector::ProcessingUsage> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 486 | OveruseFrameDetector::CreateProcessingUsage(const CpuOveruseOptions& options) { |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame] | 487 | std::unique_ptr<ProcessingUsage> instance; |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 488 | if (options.filter_time_ms > 0) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 489 | instance = absl::make_unique<SendProcessingUsage2>(options); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 490 | } else { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 491 | instance = absl::make_unique<SendProcessingUsage1>(options); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 492 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 493 | std::string toggling_interval = |
| 494 | field_trial::FindFullName("WebRTC-ForceSimulatedOveruseIntervalMs"); |
| 495 | if (!toggling_interval.empty()) { |
| 496 | int normal_period_ms = 0; |
| 497 | int overuse_period_ms = 0; |
| 498 | int underuse_period_ms = 0; |
| 499 | if (sscanf(toggling_interval.c_str(), "%d-%d-%d", &normal_period_ms, |
| 500 | &overuse_period_ms, &underuse_period_ms) == 3) { |
| 501 | if (normal_period_ms > 0 && overuse_period_ms > 0 && |
| 502 | underuse_period_ms > 0) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 503 | instance = absl::make_unique<OverdoseInjector>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 504 | std::move(instance), normal_period_ms, overuse_period_ms, |
| 505 | underuse_period_ms); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 506 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 507 | RTC_LOG(LS_WARNING) |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 508 | << "Invalid (non-positive) normal/overuse/underuse periods: " |
| 509 | << normal_period_ms << " / " << overuse_period_ms << " / " |
| 510 | << underuse_period_ms; |
| 511 | } |
| 512 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 513 | RTC_LOG(LS_WARNING) << "Malformed toggling interval: " |
| 514 | << toggling_interval; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 517 | return instance; |
| 518 | } |
| 519 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 520 | class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask { |
| 521 | public: |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 522 | CheckOveruseTask(OveruseFrameDetector* overuse_detector, |
| 523 | AdaptationObserverInterface* observer) |
| 524 | : overuse_detector_(overuse_detector), observer_(observer) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 525 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 526 | std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs); |
| 527 | } |
| 528 | |
| 529 | void Stop() { |
| 530 | RTC_CHECK(task_checker_.CalledSequentially()); |
| 531 | overuse_detector_ = nullptr; |
| 532 | } |
| 533 | |
| 534 | private: |
| 535 | bool Run() override { |
| 536 | RTC_CHECK(task_checker_.CalledSequentially()); |
| 537 | if (!overuse_detector_) |
| 538 | return true; // This will make the task queue delete this task. |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 539 | overuse_detector_->CheckForOveruse(observer_); |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 540 | |
| 541 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 542 | std::unique_ptr<rtc::QueuedTask>(this), kCheckForOveruseIntervalMs); |
| 543 | // Return false to prevent this task from being deleted. Ownership has been |
| 544 | // transferred to the task queue when PostDelayedTask was called. |
| 545 | return false; |
| 546 | } |
| 547 | rtc::SequencedTaskChecker task_checker_; |
| 548 | OveruseFrameDetector* overuse_detector_; |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 549 | // Observer getting overuse reports. |
| 550 | AdaptationObserverInterface* observer_; |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 551 | }; |
| 552 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 553 | OveruseFrameDetector::OveruseFrameDetector( |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 554 | CpuOveruseMetricsObserver* metrics_observer) |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 555 | : check_overuse_task_(nullptr), |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 556 | metrics_observer_(metrics_observer), |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 557 | num_process_times_(0), |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 558 | // TODO(nisse): Use absl::optional |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 559 | last_capture_time_us_(-1), |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 560 | num_pixels_(0), |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 561 | max_framerate_(kDefaultFrameRate), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 562 | last_overuse_time_ms_(-1), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 563 | checks_above_threshold_(0), |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 564 | num_overuse_detections_(0), |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 565 | last_rampup_time_ms_(-1), |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 566 | in_quick_rampup_(false), |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 567 | current_rampup_delay_ms_(kStandardRampUpDelayMs) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 568 | task_checker_.Detach(); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 569 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 570 | |
| 571 | OveruseFrameDetector::~OveruseFrameDetector() { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 572 | RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called."; |
| 573 | } |
| 574 | |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 575 | void OveruseFrameDetector::StartCheckForOveruse( |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 576 | const CpuOveruseOptions& options, |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 577 | AdaptationObserverInterface* overuse_observer) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 578 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 579 | RTC_DCHECK(!check_overuse_task_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 580 | RTC_DCHECK(overuse_observer != nullptr); |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 581 | |
| 582 | SetOptions(options); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 583 | check_overuse_task_ = new CheckOveruseTask(this, overuse_observer); |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 584 | } |
| 585 | void OveruseFrameDetector::StopCheckForOveruse() { |
| 586 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 587 | if (check_overuse_task_) { |
| 588 | check_overuse_task_->Stop(); |
| 589 | check_overuse_task_ = nullptr; |
| 590 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 593 | void OveruseFrameDetector::EncodedFrameTimeMeasured(int encode_duration_ms) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 594 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 595 | encode_usage_percent_ = usage_->Value(); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 596 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 597 | metrics_observer_->OnEncodedFrameTimeMeasured(encode_duration_ms, |
| 598 | *encode_usage_percent_); |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 599 | } |
| 600 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 601 | bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 602 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 603 | if (num_pixels != num_pixels_) { |
| 604 | return true; |
| 605 | } |
| 606 | return false; |
| 607 | } |
| 608 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 609 | bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now_us) const { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 610 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 611 | if (last_capture_time_us_ == -1) |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 612 | return false; |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 613 | return (now_us - last_capture_time_us_) > |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 614 | options_.frame_timeout_interval_ms * rtc::kNumMicrosecsPerMillisec; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 617 | void OveruseFrameDetector::ResetAll(int num_pixels) { |
| 618 | // Reset state, as a result resolution being changed. Do not however change |
| 619 | // the current frame rate back to the default. |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 620 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 621 | num_pixels_ = num_pixels; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 622 | usage_->Reset(); |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 623 | last_capture_time_us_ = -1; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 624 | num_process_times_ = 0; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 625 | encode_usage_percent_ = absl::nullopt; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 626 | OnTargetFramerateUpdated(max_framerate_); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 629 | void OveruseFrameDetector::OnTargetFramerateUpdated(int framerate_fps) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 630 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 631 | RTC_DCHECK_GE(framerate_fps, 0); |
| 632 | max_framerate_ = std::min(kMaxFramerate, framerate_fps); |
| 633 | usage_->SetMaxSampleDiffMs((1000 / std::max(kMinFramerate, max_framerate_)) * |
| 634 | kMaxSampleDiffMarginFactor); |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 637 | void OveruseFrameDetector::FrameCaptured(const VideoFrame& frame, |
| 638 | int64_t time_when_first_seen_us) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 639 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | eee7ced | 2017-12-01 11:25:01 +0100 | [diff] [blame] | 640 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 641 | if (FrameSizeChanged(frame.width() * frame.height()) || |
| 642 | FrameTimeoutDetected(time_when_first_seen_us)) { |
| 643 | ResetAll(frame.width() * frame.height()); |
| 644 | } |
| 645 | |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 646 | usage_->FrameCaptured(frame, time_when_first_seen_us, last_capture_time_us_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 647 | last_capture_time_us_ = time_when_first_seen_us; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void OveruseFrameDetector::FrameSent(uint32_t timestamp, |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 651 | int64_t time_sent_in_us, |
| 652 | int64_t capture_time_us, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 653 | absl::optional<int> encode_duration_us) { |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 654 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 655 | encode_duration_us = usage_->FrameSent(timestamp, time_sent_in_us, |
| 656 | capture_time_us, encode_duration_us); |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 657 | |
| 658 | if (encode_duration_us) { |
| 659 | EncodedFrameTimeMeasured(*encode_duration_us / |
| 660 | rtc::kNumMicrosecsPerMillisec); |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 661 | } |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 664 | void OveruseFrameDetector::CheckForOveruse( |
| 665 | AdaptationObserverInterface* observer) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 666 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 667 | RTC_DCHECK(observer); |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 668 | ++num_process_times_; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 669 | if (num_process_times_ <= options_.min_process_count || |
| 670 | !encode_usage_percent_) |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 671 | return; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 672 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 673 | int64_t now_ms = rtc::TimeMillis(); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 674 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 675 | if (IsOverusing(*encode_usage_percent_)) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 676 | // If the last thing we did was going up, and now have to back down, we need |
| 677 | // to check if this peak was short. If so we should back off to avoid going |
| 678 | // 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] | 679 | 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] | 680 | if (check_for_backoff) { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 681 | if (now_ms - last_rampup_time_ms_ < kStandardRampUpDelayMs || |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 682 | num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 683 | // Going up was not ok for very long, back off. |
| 684 | current_rampup_delay_ms_ *= kRampUpBackoffFactor; |
| 685 | if (current_rampup_delay_ms_ > kMaxRampUpDelayMs) |
| 686 | current_rampup_delay_ms_ = kMaxRampUpDelayMs; |
| 687 | } else { |
| 688 | // Not currently backing off, reset rampup delay. |
| 689 | current_rampup_delay_ms_ = kStandardRampUpDelayMs; |
| 690 | } |
| 691 | } |
| 692 | |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 693 | last_overuse_time_ms_ = now_ms; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 694 | in_quick_rampup_ = false; |
| 695 | checks_above_threshold_ = 0; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 696 | ++num_overuse_detections_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 697 | |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 698 | observer->AdaptDown(kScaleReasonCpu); |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 699 | } else if (IsUnderusing(*encode_usage_percent_, now_ms)) { |
nisse | e0e3bdf | 2017-01-18 02:16:20 -0800 | [diff] [blame] | 700 | last_rampup_time_ms_ = now_ms; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 701 | in_quick_rampup_ = true; |
| 702 | |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 703 | observer->AdaptUp(kScaleReasonCpu); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 704 | } |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 705 | |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 706 | int rampup_delay = |
| 707 | in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 708 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 709 | RTC_LOG(LS_VERBOSE) << " Frame stats: " |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 710 | << " encode usage " << *encode_usage_percent_ |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 711 | << " overuse detections " << num_overuse_detections_ |
| 712 | << " rampup delay " << rampup_delay; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 715 | void OveruseFrameDetector::SetOptions(const CpuOveruseOptions& options) { |
| 716 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 717 | options_ = options; |
| 718 | // Force reset with next frame. |
| 719 | num_pixels_ = 0; |
| 720 | usage_ = CreateProcessingUsage(options); |
| 721 | } |
| 722 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 723 | bool OveruseFrameDetector::IsOverusing(int usage_percent) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 724 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 725 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 726 | if (usage_percent >= options_.high_encode_usage_threshold_percent) { |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 727 | ++checks_above_threshold_; |
| 728 | } else { |
| 729 | checks_above_threshold_ = 0; |
| 730 | } |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 731 | return checks_above_threshold_ >= options_.high_threshold_consecutive_count; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 734 | bool OveruseFrameDetector::IsUnderusing(int usage_percent, int64_t time_now) { |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 735 | RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 736 | int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 737 | if (time_now < last_rampup_time_ms_ + delay) |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 738 | return false; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 739 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 740 | return usage_percent < options_.low_encode_usage_threshold_percent; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 741 | } |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 742 | } // namespace webrtc |