• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::__anon8c24cb480111::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()432 CpuOveruseOptions::CpuOveruseOptions()
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   // This is proof-of-concept code for letting the physical core count affect
442   // the interval into which we attempt to scale. For now, the code is Mac OS
443   // specific, since that's the platform were we saw most problems.
444   // TODO(torbjorng): Enhance SystemInfo to return this metric.
445 
446   mach_port_t mach_host = mach_host_self();
447   host_basic_info hbi = {};
448   mach_msg_type_number_t info_count = HOST_BASIC_INFO_COUNT;
449   kern_return_t kr =
450       host_info(mach_host, HOST_BASIC_INFO, reinterpret_cast<host_info_t>(&hbi),
451                 &info_count);
452   mach_port_deallocate(mach_task_self(), mach_host);
453 
454   int n_physical_cores;
455   if (kr != KERN_SUCCESS) {
456     // If we couldn't get # of physical CPUs, don't panic. Assume we have 1.
457     n_physical_cores = 1;
458     RTC_LOG(LS_ERROR)
459         << "Failed to determine number of physical cores, assuming 1";
460   } else {
461     n_physical_cores = hbi.physical_cpu;
462     RTC_LOG(LS_INFO) << "Number of physical cores:" << n_physical_cores;
463   }
464 
465   // Change init list default for few core systems. The assumption here is that
466   // encoding, which we measure here, takes about 1/4 of the processing of a
467   // two-way call. This is roughly true for x86 using both vp8 and vp9 without
468   // hardware encoding. Since we don't affect the incoming stream here, we only
469   // control about 1/2 of the total processing needs, but this is not taken into
470   // account.
471   if (n_physical_cores == 1)
472     high_encode_usage_threshold_percent = 20;  // Roughly 1/4 of 100%.
473   else if (n_physical_cores == 2)
474     high_encode_usage_threshold_percent = 40;  // Roughly 1/4 of 200%.
475 #endif  // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
476 
477   // Note that we make the interval 2x+epsilon wide, since libyuv scaling steps
478   // are close to that (when squared). This wide interval makes sure that
479   // scaling up or down does not jump all the way across the interval.
480   low_encode_usage_threshold_percent =
481       (high_encode_usage_threshold_percent - 1) / 2;
482 }
483 
484 std::unique_ptr<OveruseFrameDetector::ProcessingUsage>
CreateProcessingUsage(const CpuOveruseOptions & options)485 OveruseFrameDetector::CreateProcessingUsage(const CpuOveruseOptions& options) {
486   std::unique_ptr<ProcessingUsage> instance;
487   if (options.filter_time_ms > 0) {
488     instance = std::make_unique<SendProcessingUsage2>(options);
489   } else {
490     instance = std::make_unique<SendProcessingUsage1>(options);
491   }
492   std::string toggling_interval =
493       field_trial::FindFullName("WebRTC-ForceSimulatedOveruseIntervalMs");
494   if (!toggling_interval.empty()) {
495     int normal_period_ms = 0;
496     int overuse_period_ms = 0;
497     int underuse_period_ms = 0;
498     if (sscanf(toggling_interval.c_str(), "%d-%d-%d", &normal_period_ms,
499                &overuse_period_ms, &underuse_period_ms) == 3) {
500       if (normal_period_ms > 0 && overuse_period_ms > 0 &&
501           underuse_period_ms > 0) {
502         instance = std::make_unique<OverdoseInjector>(
503             std::move(instance), normal_period_ms, overuse_period_ms,
504             underuse_period_ms);
505       } else {
506         RTC_LOG(LS_WARNING)
507             << "Invalid (non-positive) normal/overuse/underuse periods: "
508             << normal_period_ms << " / " << overuse_period_ms << " / "
509             << underuse_period_ms;
510       }
511     } else {
512       RTC_LOG(LS_WARNING) << "Malformed toggling interval: "
513                           << toggling_interval;
514     }
515   }
516   return instance;
517 }
518 
OveruseFrameDetector(CpuOveruseMetricsObserver * metrics_observer)519 OveruseFrameDetector::OveruseFrameDetector(
520     CpuOveruseMetricsObserver* metrics_observer)
521     : metrics_observer_(metrics_observer),
522       num_process_times_(0),
523       // TODO(nisse): Use absl::optional
524       last_capture_time_us_(-1),
525       num_pixels_(0),
526       max_framerate_(kDefaultFrameRate),
527       last_overuse_time_ms_(-1),
528       checks_above_threshold_(0),
529       num_overuse_detections_(0),
530       last_rampup_time_ms_(-1),
531       in_quick_rampup_(false),
532       current_rampup_delay_ms_(kStandardRampUpDelayMs) {
533   task_checker_.Detach();
534   ParseFieldTrial({&filter_time_constant_},
535                   field_trial::FindFullName("WebRTC-CpuLoadEstimator"));
536 }
537 
~OveruseFrameDetector()538 OveruseFrameDetector::~OveruseFrameDetector() {}
539 
StartCheckForOveruse(TaskQueueBase * task_queue_base,const CpuOveruseOptions & options,OveruseFrameDetectorObserverInterface * overuse_observer)540 void OveruseFrameDetector::StartCheckForOveruse(
541     TaskQueueBase* task_queue_base,
542     const CpuOveruseOptions& options,
543     OveruseFrameDetectorObserverInterface* overuse_observer) {
544   RTC_DCHECK_RUN_ON(&task_checker_);
545   RTC_DCHECK(!check_overuse_task_.Running());
546   RTC_DCHECK(overuse_observer != nullptr);
547 
548   SetOptions(options);
549   check_overuse_task_ = RepeatingTaskHandle::DelayedStart(
550       task_queue_base, TimeDelta::Millis(kTimeToFirstCheckForOveruseMs),
551       [this, overuse_observer] {
552         CheckForOveruse(overuse_observer);
553         return TimeDelta::Millis(kCheckForOveruseIntervalMs);
554       });
555 }
StopCheckForOveruse()556 void OveruseFrameDetector::StopCheckForOveruse() {
557   RTC_DCHECK_RUN_ON(&task_checker_);
558   check_overuse_task_.Stop();
559 }
560 
EncodedFrameTimeMeasured(int encode_duration_ms)561 void OveruseFrameDetector::EncodedFrameTimeMeasured(int encode_duration_ms) {
562   RTC_DCHECK_RUN_ON(&task_checker_);
563   encode_usage_percent_ = usage_->Value();
564 
565   metrics_observer_->OnEncodedFrameTimeMeasured(encode_duration_ms,
566                                                 *encode_usage_percent_);
567 }
568 
FrameSizeChanged(int num_pixels) const569 bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const {
570   RTC_DCHECK_RUN_ON(&task_checker_);
571   if (num_pixels != num_pixels_) {
572     return true;
573   }
574   return false;
575 }
576 
FrameTimeoutDetected(int64_t now_us) const577 bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now_us) const {
578   RTC_DCHECK_RUN_ON(&task_checker_);
579   if (last_capture_time_us_ == -1)
580     return false;
581   return (now_us - last_capture_time_us_) >
582          options_.frame_timeout_interval_ms * rtc::kNumMicrosecsPerMillisec;
583 }
584 
ResetAll(int num_pixels)585 void OveruseFrameDetector::ResetAll(int num_pixels) {
586   // Reset state, as a result resolution being changed. Do not however change
587   // the current frame rate back to the default.
588   RTC_DCHECK_RUN_ON(&task_checker_);
589   num_pixels_ = num_pixels;
590   usage_->Reset();
591   last_capture_time_us_ = -1;
592   num_process_times_ = 0;
593   encode_usage_percent_ = absl::nullopt;
594   OnTargetFramerateUpdated(max_framerate_);
595 }
596 
OnTargetFramerateUpdated(int framerate_fps)597 void OveruseFrameDetector::OnTargetFramerateUpdated(int framerate_fps) {
598   RTC_DCHECK_RUN_ON(&task_checker_);
599   RTC_DCHECK_GE(framerate_fps, 0);
600   max_framerate_ = std::min(kMaxFramerate, framerate_fps);
601   usage_->SetMaxSampleDiffMs((1000 / std::max(kMinFramerate, max_framerate_)) *
602                              kMaxSampleDiffMarginFactor);
603 }
604 
FrameCaptured(const VideoFrame & frame,int64_t time_when_first_seen_us)605 void OveruseFrameDetector::FrameCaptured(const VideoFrame& frame,
606                                          int64_t time_when_first_seen_us) {
607   RTC_DCHECK_RUN_ON(&task_checker_);
608 
609   if (FrameSizeChanged(frame.width() * frame.height()) ||
610       FrameTimeoutDetected(time_when_first_seen_us)) {
611     ResetAll(frame.width() * frame.height());
612   }
613 
614   usage_->FrameCaptured(frame, time_when_first_seen_us, last_capture_time_us_);
615   last_capture_time_us_ = time_when_first_seen_us;
616 }
617 
FrameSent(uint32_t timestamp,int64_t time_sent_in_us,int64_t capture_time_us,absl::optional<int> encode_duration_us)618 void OveruseFrameDetector::FrameSent(uint32_t timestamp,
619                                      int64_t time_sent_in_us,
620                                      int64_t capture_time_us,
621                                      absl::optional<int> encode_duration_us) {
622   RTC_DCHECK_RUN_ON(&task_checker_);
623   encode_duration_us = usage_->FrameSent(timestamp, time_sent_in_us,
624                                          capture_time_us, encode_duration_us);
625 
626   if (encode_duration_us) {
627     EncodedFrameTimeMeasured(*encode_duration_us /
628                              rtc::kNumMicrosecsPerMillisec);
629   }
630 }
631 
CheckForOveruse(OveruseFrameDetectorObserverInterface * observer)632 void OveruseFrameDetector::CheckForOveruse(
633     OveruseFrameDetectorObserverInterface* observer) {
634   RTC_DCHECK_RUN_ON(&task_checker_);
635   RTC_DCHECK(observer);
636   ++num_process_times_;
637   if (num_process_times_ <= options_.min_process_count ||
638       !encode_usage_percent_)
639     return;
640 
641   int64_t now_ms = rtc::TimeMillis();
642 
643   if (IsOverusing(*encode_usage_percent_)) {
644     // If the last thing we did was going up, and now have to back down, we need
645     // to check if this peak was short. If so we should back off to avoid going
646     // back and forth between this load, the system doesn't seem to handle it.
647     bool check_for_backoff = last_rampup_time_ms_ > last_overuse_time_ms_;
648     if (check_for_backoff) {
649       if (now_ms - last_rampup_time_ms_ < kStandardRampUpDelayMs ||
650           num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) {
651         // Going up was not ok for very long, back off.
652         current_rampup_delay_ms_ *= kRampUpBackoffFactor;
653         if (current_rampup_delay_ms_ > kMaxRampUpDelayMs)
654           current_rampup_delay_ms_ = kMaxRampUpDelayMs;
655       } else {
656         // Not currently backing off, reset rampup delay.
657         current_rampup_delay_ms_ = kStandardRampUpDelayMs;
658       }
659     }
660 
661     last_overuse_time_ms_ = now_ms;
662     in_quick_rampup_ = false;
663     checks_above_threshold_ = 0;
664     ++num_overuse_detections_;
665 
666     observer->AdaptDown();
667   } else if (IsUnderusing(*encode_usage_percent_, now_ms)) {
668     last_rampup_time_ms_ = now_ms;
669     in_quick_rampup_ = true;
670 
671     observer->AdaptUp();
672   }
673 
674   int rampup_delay =
675       in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
676 
677   RTC_LOG(LS_VERBOSE) << " Frame stats: "
678                          " encode usage "
679                       << *encode_usage_percent_ << " overuse detections "
680                       << num_overuse_detections_ << " rampup delay "
681                       << rampup_delay;
682 }
683 
SetOptions(const CpuOveruseOptions & options)684 void OveruseFrameDetector::SetOptions(const CpuOveruseOptions& options) {
685   RTC_DCHECK_RUN_ON(&task_checker_);
686   options_ = options;
687 
688   // Time constant config overridable by field trial.
689   if (filter_time_constant_) {
690     options_.filter_time_ms = filter_time_constant_->ms();
691   }
692   // Force reset with next frame.
693   num_pixels_ = 0;
694   usage_ = CreateProcessingUsage(options);
695 }
696 
IsOverusing(int usage_percent)697 bool OveruseFrameDetector::IsOverusing(int usage_percent) {
698   RTC_DCHECK_RUN_ON(&task_checker_);
699 
700   if (usage_percent >= options_.high_encode_usage_threshold_percent) {
701     ++checks_above_threshold_;
702   } else {
703     checks_above_threshold_ = 0;
704   }
705   return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
706 }
707 
IsUnderusing(int usage_percent,int64_t time_now)708 bool OveruseFrameDetector::IsUnderusing(int usage_percent, int64_t time_now) {
709   RTC_DCHECK_RUN_ON(&task_checker_);
710   int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
711   if (time_now < last_rampup_time_ms_ + delay)
712     return false;
713 
714   return usage_percent < options_.low_encode_usage_threshold_percent;
715 }
716 }  // namespace webrtc
717