mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [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 | |
| 11 | #ifndef WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |
| 12 | #define WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |
| 13 | |
henrike@webrtc.org | 88fbb2d | 2014-05-21 21:18:46 | [diff] [blame] | 14 | #include "webrtc/base/constructormagic.h" |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 | [diff] [blame] | 15 | #include "webrtc/base/criticalsection.h" |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 | [diff] [blame] | 16 | #include "webrtc/base/scoped_ptr.h" |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 | [diff] [blame] | 17 | #include "webrtc/base/exp_filter.h" |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 | [diff] [blame] | 18 | #include "webrtc/base/thread_annotations.h" |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 | [diff] [blame] | 19 | #include "webrtc/base/thread_checker.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 20 | #include "webrtc/modules/interface/module.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | class Clock; |
Peter Boström | 300eeb6 | 2015-05-12 14:51:11 | [diff] [blame] | 25 | |
| 26 | // CpuOveruseObserver is called when a system overuse is detected and |
| 27 | // VideoEngine cannot keep up the encoding frequency. |
| 28 | class CpuOveruseObserver { |
| 29 | public: |
| 30 | // Called as soon as an overuse is detected. |
| 31 | virtual void OveruseDetected() = 0; |
| 32 | // Called periodically when the system is not overused any longer. |
| 33 | virtual void NormalUsage() = 0; |
| 34 | |
| 35 | protected: |
| 36 | virtual ~CpuOveruseObserver() {} |
| 37 | }; |
| 38 | |
| 39 | struct CpuOveruseOptions { |
| 40 | CpuOveruseOptions() |
Åsa Persson | 746210f | 2015-09-08 08:52:42 | [diff] [blame^] | 41 | : enable_encode_usage_method(true), |
Peter Boström | 300eeb6 | 2015-05-12 14:51:11 | [diff] [blame] | 42 | low_encode_usage_threshold_percent(55), |
| 43 | high_encode_usage_threshold_percent(85), |
Peter Boström | 300eeb6 | 2015-05-12 14:51:11 | [diff] [blame] | 44 | enable_extended_processing_usage(true), |
| 45 | frame_timeout_interval_ms(1500), |
| 46 | min_frame_samples(120), |
| 47 | min_process_count(3), |
| 48 | high_threshold_consecutive_count(2) {} |
| 49 | |
Peter Boström | 300eeb6 | 2015-05-12 14:51:11 | [diff] [blame] | 50 | // Method based on encode time of frames. |
| 51 | bool enable_encode_usage_method; |
| 52 | int low_encode_usage_threshold_percent; // Threshold for triggering underuse. |
| 53 | int high_encode_usage_threshold_percent; // Threshold for triggering overuse. |
Peter Boström | 300eeb6 | 2015-05-12 14:51:11 | [diff] [blame] | 54 | bool enable_extended_processing_usage; // Include a larger time span (in |
| 55 | // addition to encode time) for |
| 56 | // measuring the processing time of a |
| 57 | // frame. |
| 58 | // General settings. |
| 59 | int frame_timeout_interval_ms; // The maximum allowed interval between two |
| 60 | // frames before resetting estimations. |
| 61 | int min_frame_samples; // The minimum number of frames required. |
| 62 | int min_process_count; // The number of initial process times required before |
| 63 | // triggering an overuse/underuse. |
| 64 | int high_threshold_consecutive_count; // The number of consecutive checks |
| 65 | // above the high threshold before |
| 66 | // triggering an overuse. |
Peter Boström | 300eeb6 | 2015-05-12 14:51:11 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct CpuOveruseMetrics { |
| 70 | CpuOveruseMetrics() |
Åsa Persson | 746210f | 2015-09-08 08:52:42 | [diff] [blame^] | 71 | : avg_encode_time_ms(-1), |
Asa Persson | cddb367 | 2015-07-16 06:08:03 | [diff] [blame] | 72 | encode_usage_percent(-1) {} |
Peter Boström | 300eeb6 | 2015-05-12 14:51:11 | [diff] [blame] | 73 | |
Peter Boström | 300eeb6 | 2015-05-12 14:51:11 | [diff] [blame] | 74 | int avg_encode_time_ms; // The average encode time in ms. |
| 75 | int encode_usage_percent; // The average encode time divided by the average |
| 76 | // time difference between incoming captured frames. |
Peter Boström | 300eeb6 | 2015-05-12 14:51:11 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | class CpuOveruseMetricsObserver { |
| 80 | public: |
| 81 | virtual ~CpuOveruseMetricsObserver() {} |
| 82 | virtual void CpuOveruseMetricsUpdated(const CpuOveruseMetrics& metrics) = 0; |
| 83 | }; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 84 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 | [diff] [blame] | 85 | |
Åsa Persson | 746210f | 2015-09-08 08:52:42 | [diff] [blame^] | 86 | // Use to detect system overuse based on the send-side processing time of |
| 87 | // incoming frames. |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 88 | class OveruseFrameDetector : public Module { |
| 89 | public: |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 | [diff] [blame] | 90 | OveruseFrameDetector(Clock* clock, |
Peter Boström | 4b91bd0 | 2015-06-26 04:58:16 | [diff] [blame] | 91 | const CpuOveruseOptions& options, |
| 92 | CpuOveruseObserver* overuse_observer, |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 | [diff] [blame] | 93 | CpuOveruseMetricsObserver* metrics_observer); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 94 | ~OveruseFrameDetector(); |
| 95 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 | [diff] [blame] | 96 | // Called for each captured frame. |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 | [diff] [blame] | 97 | void FrameCaptured(int width, int height, int64_t capture_time_ms); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 98 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 | [diff] [blame] | 99 | // Called for each encoded frame. |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 | [diff] [blame] | 100 | void FrameEncoded(int encode_time_ms); |
| 101 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 | [diff] [blame] | 102 | // Called for each sent frame. |
| 103 | void FrameSent(int64_t capture_time_ms); |
| 104 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 | [diff] [blame] | 105 | // Only public for testing. |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 | [diff] [blame] | 106 | int LastProcessingTimeMs() const; |
| 107 | int FramesInQueue() const; |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 | [diff] [blame] | 108 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 109 | // Implements Module. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 110 | int64_t TimeUntilNextProcess() override; |
| 111 | int32_t Process() override; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 112 | |
| 113 | private: |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 | [diff] [blame] | 114 | class EncodeTimeAvg; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 | [diff] [blame] | 115 | class SendProcessingUsage; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 | [diff] [blame] | 116 | class FrameQueue; |
| 117 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 | [diff] [blame] | 118 | void UpdateCpuOveruseMetrics() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 119 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 | [diff] [blame] | 120 | // TODO(asapersson): This method is only used on one thread, so it shouldn't |
| 121 | // need a guard. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 | [diff] [blame] | 122 | void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 | [diff] [blame] | 123 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 | [diff] [blame] | 124 | // TODO(asapersson): This method is always called on the processing thread. |
| 125 | // If locking is required, consider doing that locking inside the |
| 126 | // implementation and reduce scope as much as possible. We should also |
| 127 | // see if we can avoid calling out to other methods while holding the lock. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 | [diff] [blame] | 128 | bool IsOverusing() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 129 | bool IsUnderusing(int64_t time_now) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 130 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 | [diff] [blame] | 131 | bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 132 | bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 | [diff] [blame] | 133 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 | [diff] [blame] | 134 | void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 | [diff] [blame] | 135 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 | [diff] [blame] | 136 | // Protecting all members except const and those that are only accessed on the |
| 137 | // processing thread. |
| 138 | // TODO(asapersson): See if we can reduce locking. As is, video frame |
| 139 | // processing contends with reading stats and the processing thread. |
| 140 | mutable rtc::CriticalSection crit_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 141 | |
Peter Boström | 4b91bd0 | 2015-06-26 04:58:16 | [diff] [blame] | 142 | const CpuOveruseOptions options_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 143 | |
Peter Boström | 4b91bd0 | 2015-06-26 04:58:16 | [diff] [blame] | 144 | // Observer getting overuse reports. |
| 145 | CpuOveruseObserver* const observer_; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 | [diff] [blame] | 146 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 | [diff] [blame] | 147 | // Stats metrics. |
| 148 | CpuOveruseMetricsObserver* const metrics_observer_; |
| 149 | CpuOveruseMetrics metrics_ GUARDED_BY(crit_); |
| 150 | |
tommi@webrtc.org | a907e01 | 2015-01-28 17:33:12 | [diff] [blame] | 151 | Clock* const clock_; |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 | [diff] [blame] | 152 | int64_t next_process_time_; // Only accessed on the processing thread. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 | [diff] [blame] | 153 | int64_t num_process_times_ GUARDED_BY(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 154 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 | [diff] [blame] | 155 | int64_t last_capture_time_ GUARDED_BY(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 156 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 | [diff] [blame] | 157 | // These six members are only accessed on the processing thread. |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 | [diff] [blame] | 158 | int64_t last_overuse_time_; |
| 159 | int checks_above_threshold_; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 | [diff] [blame] | 160 | int num_overuse_detections_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 | [diff] [blame] | 161 | |
| 162 | int64_t last_rampup_time_; |
| 163 | bool in_quick_rampup_; |
| 164 | int current_rampup_delay_ms_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 165 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 | [diff] [blame] | 166 | // Number of pixels of last captured frame. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 | [diff] [blame] | 167 | int num_pixels_ GUARDED_BY(crit_); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 | [diff] [blame] | 168 | |
asapersson@webrtc.org | 4414939 | 2015-02-04 08:34:47 | [diff] [blame] | 169 | int64_t last_encode_sample_ms_; // Only accessed by one thread. |
| 170 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 | [diff] [blame] | 171 | // TODO(asapersson): Can these be regular members (avoid separate heap |
| 172 | // allocs)? |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 | [diff] [blame] | 173 | const rtc::scoped_ptr<EncodeTimeAvg> encode_time_ GUARDED_BY(crit_); |
| 174 | const rtc::scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_); |
| 175 | const rtc::scoped_ptr<FrameQueue> frame_queue_ GUARDED_BY(crit_); |
asapersson@webrtc.org | 4414939 | 2015-02-04 08:34:47 | [diff] [blame] | 176 | |
| 177 | int64_t last_sample_time_ms_; // Only accessed by one thread. |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 | [diff] [blame] | 178 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 | [diff] [blame] | 179 | rtc::ThreadChecker processing_thread_; |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 | [diff] [blame] | 180 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 | [diff] [blame] | 181 | DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); |
| 182 | }; |
| 183 | |
| 184 | } // namespace webrtc |
| 185 | |
| 186 | #endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |