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