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 | #ifndef VIDEO_OVERUSE_FRAME_DETECTOR_H_ |
| 12 | #define VIDEO_OVERUSE_FRAME_DETECTOR_H_ |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 13 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 14 | #include <list> |
kwiberg | 27f982b | 2016-03-01 11:52:33 -0800 | [diff] [blame] | 15 | #include <memory> |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 16 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 18 | #include "api/video/video_stream_encoder_observer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/video_coding/utility/quality_scaler.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/constructor_magic.h" |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 21 | #include "rtc_base/numerics/exp_filter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/sequenced_task_checker.h" |
| 23 | #include "rtc_base/task_queue.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 24 | #include "rtc_base/task_utils/repeating_task.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/thread_annotations.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 29 | class VideoFrame; |
Peter Boström | 300eeb6 | 2015-05-12 16:51:11 +0200 | [diff] [blame] | 30 | |
Peter Boström | 300eeb6 | 2015-05-12 16:51:11 +0200 | [diff] [blame] | 31 | struct CpuOveruseOptions { |
torbjorng | 448468d | 2016-02-10 08:11:57 -0800 | [diff] [blame] | 32 | CpuOveruseOptions(); |
Peter Boström | 300eeb6 | 2015-05-12 16:51:11 +0200 | [diff] [blame] | 33 | |
Peter Boström | 300eeb6 | 2015-05-12 16:51:11 +0200 | [diff] [blame] | 34 | int low_encode_usage_threshold_percent; // Threshold for triggering underuse. |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 35 | int high_encode_usage_threshold_percent; // Threshold for triggering overuse. |
Peter Boström | 300eeb6 | 2015-05-12 16:51:11 +0200 | [diff] [blame] | 36 | // General settings. |
| 37 | int frame_timeout_interval_ms; // The maximum allowed interval between two |
| 38 | // frames before resetting estimations. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 39 | int min_frame_samples; // The minimum number of frames required. |
Peter Boström | 300eeb6 | 2015-05-12 16:51:11 +0200 | [diff] [blame] | 40 | int min_process_count; // The number of initial process times required before |
| 41 | // triggering an overuse/underuse. |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 42 | int high_threshold_consecutive_count; // The number of consecutive checks |
| 43 | // above the high threshold before |
| 44 | // triggering an overuse. |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 45 | // New estimator enabled if this is set non-zero. |
| 46 | int filter_time_ms; // Time constant for averaging |
Peter Boström | 300eeb6 | 2015-05-12 16:51:11 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Åsa Persson | 746210f | 2015-09-08 10:52:42 +0200 | [diff] [blame] | 49 | // Use to detect system overuse based on the send-side processing time of |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 50 | // incoming frames. All methods must be called on a single task queue but it can |
| 51 | // be created and destroyed on an arbitrary thread. |
| 52 | // OveruseFrameDetector::StartCheckForOveruse must be called to periodically |
| 53 | // check for overuse. |
| 54 | class OveruseFrameDetector { |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 55 | public: |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 56 | explicit OveruseFrameDetector(CpuOveruseMetricsObserver* metrics_observer); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 57 | virtual ~OveruseFrameDetector(); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 58 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 59 | // Start to periodically check for overuse. |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame^] | 60 | void StartCheckForOveruse(rtc::TaskQueue* task_queue, |
| 61 | const CpuOveruseOptions& options, |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 62 | AdaptationObserverInterface* overuse_observer); |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 63 | |
| 64 | // StopCheckForOveruse must be called before destruction if |
| 65 | // StartCheckForOveruse has been called. |
| 66 | void StopCheckForOveruse(); |
| 67 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 68 | // Defines the current maximum framerate targeted by the capturer. This is |
| 69 | // used to make sure the encode usage percent doesn't drop unduly if the |
| 70 | // capturer has quiet periods (for instance caused by screen capturers with |
| 71 | // variable capture rate depending on content updates), otherwise we might |
| 72 | // experience adaptation toggling. |
| 73 | virtual void OnTargetFramerateUpdated(int framerate_fps); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 74 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 75 | // Called for each captured frame. |
| 76 | void FrameCaptured(const VideoFrame& frame, int64_t time_when_first_seen_us); |
| 77 | |
| 78 | // Called for each sent frame. |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 79 | void FrameSent(uint32_t timestamp, |
| 80 | int64_t time_sent_in_us, |
| 81 | int64_t capture_time_us, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 82 | absl::optional<int> encode_duration_us); |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 83 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame] | 84 | // Interface for cpu load estimation. Intended for internal use only. |
| 85 | class ProcessingUsage { |
| 86 | public: |
| 87 | virtual void Reset() = 0; |
| 88 | virtual void SetMaxSampleDiffMs(float diff_ms) = 0; |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 89 | virtual void FrameCaptured(const VideoFrame& frame, |
| 90 | int64_t time_when_first_seen_us, |
| 91 | int64_t last_capture_time_us) = 0; |
| 92 | // Returns encode_time in us, if there's a new measurement. |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 93 | virtual absl::optional<int> FrameSent( |
Niels Möller | 83dbeac | 2017-12-14 16:39:44 +0100 | [diff] [blame] | 94 | // These two argument used by old estimator. |
| 95 | uint32_t timestamp, |
| 96 | int64_t time_sent_in_us, |
| 97 | // And these two by the new estimator. |
| 98 | int64_t capture_time_us, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 99 | absl::optional<int> encode_duration_us) = 0; |
Niels Möller | e08cf3a | 2017-12-07 15:23:58 +0100 | [diff] [blame] | 100 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame] | 101 | virtual int Value() = 0; |
| 102 | virtual ~ProcessingUsage() = default; |
| 103 | }; |
| 104 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 105 | protected: |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame] | 106 | // Protected for test purposes. |
| 107 | void CheckForOveruse(AdaptationObserverInterface* overuse_observer); |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 108 | void SetOptions(const CpuOveruseOptions& options); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 109 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 110 | CpuOveruseOptions options_; |
| 111 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 112 | private: |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 113 | void EncodedFrameTimeMeasured(int encode_duration_ms); |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 114 | bool IsOverusing(int encode_usage_percent); |
| 115 | bool IsUnderusing(int encode_usage_percent, int64_t time_now); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 116 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 117 | bool FrameTimeoutDetected(int64_t now) const; |
| 118 | bool FrameSizeChanged(int num_pixels) const; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 119 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 120 | void ResetAll(int num_pixels); |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 121 | |
Niels Möller | 904f869 | 2017-12-07 11:22:39 +0100 | [diff] [blame] | 122 | static std::unique_ptr<ProcessingUsage> CreateProcessingUsage( |
Niels Möller | 6b642f7 | 2017-12-08 14:11:14 +0100 | [diff] [blame] | 123 | const CpuOveruseOptions& options); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 124 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 125 | rtc::SequencedTaskChecker task_checker_; |
| 126 | // Owned by the task queue from where StartCheckForOveruse is called. |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 127 | RepeatingTaskHandle check_overuse_task_ RTC_GUARDED_BY(task_checker_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 128 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 129 | // Stats metrics. |
| 130 | CpuOveruseMetricsObserver* const metrics_observer_; |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 131 | absl::optional<int> encode_usage_percent_ RTC_GUARDED_BY(task_checker_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 132 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 133 | int64_t num_process_times_ RTC_GUARDED_BY(task_checker_); |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 134 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 135 | int64_t last_capture_time_us_ RTC_GUARDED_BY(task_checker_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 136 | |
asapersson | 74d85e1 | 2015-09-24 00:53:32 -0700 | [diff] [blame] | 137 | // Number of pixels of last captured frame. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 138 | int num_pixels_ RTC_GUARDED_BY(task_checker_); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 139 | int max_framerate_ RTC_GUARDED_BY(task_checker_); |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 140 | int64_t last_overuse_time_ms_ RTC_GUARDED_BY(task_checker_); |
| 141 | int checks_above_threshold_ RTC_GUARDED_BY(task_checker_); |
| 142 | int num_overuse_detections_ RTC_GUARDED_BY(task_checker_); |
| 143 | int64_t last_rampup_time_ms_ RTC_GUARDED_BY(task_checker_); |
| 144 | bool in_quick_rampup_ RTC_GUARDED_BY(task_checker_); |
| 145 | int current_rampup_delay_ms_ RTC_GUARDED_BY(task_checker_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 146 | |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 147 | std::unique_ptr<ProcessingUsage> usage_ RTC_PT_GUARDED_BY(task_checker_); |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 148 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 149 | RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | } // namespace webrtc |
| 153 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 154 | #endif // VIDEO_OVERUSE_FRAME_DETECTOR_H_ |