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 | |
| 11 | #ifndef WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |
| 12 | #define WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |
| 13 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 14 | #include "webrtc/modules/interface/module.h" |
| 15 | #include "webrtc/system_wrappers/interface/constructor_magic.h" |
| 16 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 17 | #include "webrtc/test/testsupport/gtest_prod_util.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | class Clock; |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 22 | class CpuOveruseObserver; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 23 | class CriticalSectionWrapper; |
| 24 | class VCMExpFilter; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 25 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 26 | // Limits on standard deviation for under/overuse. |
asapersson@webrtc.org | a0a6df3 | 2014-02-20 17:37:37 +0000 | [diff] [blame] | 27 | #ifdef WEBRTC_ANDROID |
| 28 | const float kOveruseStdDevMs = 32.0f; |
| 29 | const float kNormalUseStdDevMs = 27.0f; |
| 30 | #elif WEBRTC_LINUX |
| 31 | const float kOveruseStdDevMs = 20.0f; |
| 32 | const float kNormalUseStdDevMs = 14.0f; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 33 | #elif WEBRTC_MAC |
asapersson@webrtc.org | a0a6df3 | 2014-02-20 17:37:37 +0000 | [diff] [blame] | 34 | const float kOveruseStdDevMs = 27.0f; |
| 35 | const float kNormalUseStdDevMs = 21.0f; |
| 36 | #elif WEBRTC_WIN |
| 37 | const float kOveruseStdDevMs = 20.0f; |
asapersson@webrtc.org | 8f690bc | 2014-02-13 14:43:18 +0000 | [diff] [blame] | 38 | const float kNormalUseStdDevMs = 14.0f; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 39 | #else |
asapersson@webrtc.org | a0a6df3 | 2014-02-20 17:37:37 +0000 | [diff] [blame] | 40 | const float kOveruseStdDevMs = 30.0f; |
| 41 | const float kNormalUseStdDevMs = 20.0f; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | // TODO(pbos): Move this somewhere appropriate. |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 45 | class Statistics { |
| 46 | public: |
| 47 | Statistics(); |
| 48 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 49 | void AddSample(float sample_ms); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 50 | void Reset(); |
| 51 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 52 | float Mean() const; |
| 53 | float StdDev() const; |
| 54 | uint64_t Count() const; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 57 | float InitialMean() const; |
| 58 | float InitialVariance() const; |
| 59 | |
| 60 | float sum_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 61 | uint64_t count_; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 62 | scoped_ptr<VCMExpFilter> filtered_samples_; |
| 63 | scoped_ptr<VCMExpFilter> filtered_variance_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Use to detect system overuse based on jitter in incoming frames. |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 67 | class OveruseFrameDetector : public Module { |
| 68 | public: |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 69 | explicit OveruseFrameDetector(Clock* clock, |
| 70 | float normaluse_stddev_ms, |
| 71 | float overuse_stddev_ms); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 72 | ~OveruseFrameDetector(); |
| 73 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 74 | // Registers an observer receiving overuse and underuse callbacks. Set |
| 75 | // 'observer' to NULL to disable callbacks. |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 76 | void SetObserver(CpuOveruseObserver* observer); |
| 77 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 78 | // Called for each captured frame. |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 79 | void FrameCaptured(int width, int height); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 80 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 81 | // Called when the processing of a captured frame is started. |
| 82 | void FrameProcessingStarted(); |
| 83 | |
| 84 | // Called for each encoded frame. |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 85 | void FrameEncoded(int encode_time_ms); |
| 86 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 87 | // Accessors. |
| 88 | // The last estimated jitter based on the incoming captured frames. |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 89 | int last_capture_jitter_ms() const; |
| 90 | |
| 91 | // Running average of reported encode time (FrameEncoded()). |
| 92 | // Only used for stats. |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 93 | int AvgEncodeTimeMs() const; |
| 94 | |
| 95 | // The average encode time divided by the average time difference between |
| 96 | // incoming captured frames. |
| 97 | // This variable is currently only used for statistics. |
| 98 | int EncodeUsagePercent() const; |
| 99 | |
| 100 | // The current time delay between an incoming captured frame (FrameCaptured()) |
| 101 | // until the frame is being processed (FrameProcessingStarted()). |
| 102 | // (Note: if a new frame is received before an old frame has been processed, |
| 103 | // the old frame is skipped). |
| 104 | // The delay is returned as the delay in ms per second. |
| 105 | // This variable is currently only used for statistics. |
| 106 | int AvgCaptureQueueDelayMsPerS() const; |
| 107 | int CaptureQueueDelayMsPerS() const; |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 108 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 109 | // Implements Module. |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 110 | virtual int32_t TimeUntilNextProcess() OVERRIDE; |
| 111 | virtual int32_t Process() OVERRIDE; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 114 | FRIEND_TEST_ALL_PREFIXES(OveruseFrameDetectorTest, TriggerOveruse); |
| 115 | FRIEND_TEST_ALL_PREFIXES(OveruseFrameDetectorTest, OveruseAndRecover); |
| 116 | FRIEND_TEST_ALL_PREFIXES(OveruseFrameDetectorTest, DoubleOveruseAndRecover); |
| 117 | FRIEND_TEST_ALL_PREFIXES( |
| 118 | OveruseFrameDetectorTest, TriggerNormalUsageWithMinProcessCount); |
| 119 | FRIEND_TEST_ALL_PREFIXES( |
| 120 | OveruseFrameDetectorTest, ConstantOveruseGivesNoNormalUsage); |
| 121 | FRIEND_TEST_ALL_PREFIXES(OveruseFrameDetectorTest, LastCaptureJitter); |
| 122 | |
| 123 | void set_min_process_count_before_reporting(int64_t count) { |
| 124 | min_process_count_before_reporting_ = count; |
| 125 | } |
| 126 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 127 | class EncodeTimeAvg; |
| 128 | class EncodeUsage; |
| 129 | class CaptureQueueDelay; |
| 130 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 131 | bool IsOverusing(); |
| 132 | bool IsUnderusing(int64_t time_now); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 133 | |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 134 | bool DetectFrameTimeout(int64_t now) const; |
| 135 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 136 | // Protecting all members. |
| 137 | scoped_ptr<CriticalSectionWrapper> crit_; |
| 138 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 139 | // Limits on standard deviation for under/overuse. |
| 140 | const float normaluse_stddev_ms_; |
| 141 | const float overuse_stddev_ms_; |
| 142 | |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 143 | int64_t min_process_count_before_reporting_; |
| 144 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 145 | // Observer getting overuse reports. |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 146 | CpuOveruseObserver* observer_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 147 | |
| 148 | Clock* clock_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 149 | int64_t next_process_time_; |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 150 | int64_t num_process_times_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 151 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 152 | Statistics capture_deltas_; |
| 153 | int64_t last_capture_time_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 154 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 155 | int64_t last_overuse_time_; |
| 156 | int checks_above_threshold_; |
| 157 | |
| 158 | int64_t last_rampup_time_; |
| 159 | bool in_quick_rampup_; |
| 160 | int current_rampup_delay_ms_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 161 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 162 | // Number of pixels of last captured frame. |
| 163 | int num_pixels_; |
| 164 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 165 | int last_capture_jitter_ms_; |
| 166 | |
| 167 | int64_t last_encode_sample_ms_; |
| 168 | scoped_ptr<EncodeTimeAvg> encode_time_; |
| 169 | scoped_ptr<EncodeUsage> encode_usage_; |
| 170 | |
| 171 | scoped_ptr<CaptureQueueDelay> capture_queue_delay_; |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 172 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 173 | DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); |
| 174 | }; |
| 175 | |
| 176 | } // namespace webrtc |
| 177 | |
| 178 | #endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |