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 | |
henrike@webrtc.org | 88fbb2d | 2014-05-21 21:18:46 +0000 | [diff] [blame] | 14 | #include "webrtc/base/constructormagic.h" |
tommi@webrtc.org | 18e7585 | 2015-01-29 12:34:40 +0000 | [diff] [blame^] | 15 | #include "webrtc/base/criticalsection.h" |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 16 | #include "webrtc/base/exp_filter.h" |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 17 | #include "webrtc/base/thread_annotations.h" |
tommi@webrtc.org | 18e7585 | 2015-01-29 12:34:40 +0000 | [diff] [blame^] | 18 | #include "webrtc/base/thread_checker.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/interface/module.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 21 | #include "webrtc/video_engine/include/vie_base.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | class Clock; |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 26 | class CpuOveruseObserver; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 27 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 28 | // TODO(pbos): Move this somewhere appropriate. |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 29 | class Statistics { |
| 30 | public: |
| 31 | Statistics(); |
| 32 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 33 | void AddSample(float sample_ms); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 34 | void Reset(); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 35 | void SetOptions(const CpuOveruseOptions& options); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 36 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 37 | float Mean() const; |
| 38 | float StdDev() const; |
| 39 | uint64_t Count() const; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 40 | |
| 41 | private: |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 42 | float InitialMean() const; |
| 43 | float InitialVariance() const; |
| 44 | |
| 45 | float sum_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 46 | uint64_t count_; |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 47 | CpuOveruseOptions options_; |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 48 | scoped_ptr<rtc::ExpFilter> filtered_samples_; |
| 49 | scoped_ptr<rtc::ExpFilter> filtered_variance_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // Use to detect system overuse based on jitter in incoming frames. |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 53 | class OveruseFrameDetector : public Module { |
| 54 | public: |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 55 | explicit OveruseFrameDetector(Clock* clock); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 56 | ~OveruseFrameDetector(); |
| 57 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 58 | // Registers an observer receiving overuse and underuse callbacks. Set |
| 59 | // 'observer' to NULL to disable callbacks. |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 60 | void SetObserver(CpuOveruseObserver* observer); |
| 61 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 62 | // Sets options for overuse detection. |
| 63 | void SetOptions(const CpuOveruseOptions& options); |
| 64 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 65 | // Called for each captured frame. |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 66 | void FrameCaptured(int width, int height, int64_t capture_time_ms); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 67 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 68 | // Called when the processing of a captured frame is started. |
| 69 | void FrameProcessingStarted(); |
| 70 | |
| 71 | // Called for each encoded frame. |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 72 | void FrameEncoded(int encode_time_ms); |
| 73 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 74 | // Called for each sent frame. |
| 75 | void FrameSent(int64_t capture_time_ms); |
| 76 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 77 | // Accessors. |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 78 | |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 79 | // Returns CpuOveruseMetrics where |
| 80 | // capture_jitter_ms: The estimated jitter based on incoming captured frames. |
| 81 | // avg_encode_time_ms: Running average of reported encode time |
| 82 | // (FrameEncoded()). Only used for stats. |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 83 | // TODO(asapersson): Rename metric. |
| 84 | // encode_usage_percent: The average processing time of a frame on the |
| 85 | // send-side divided by the average time difference |
| 86 | // between incoming captured frames. |
asapersson@webrtc.org | ab6bf4f | 2014-05-27 07:43:15 +0000 | [diff] [blame] | 87 | // capture_queue_delay_ms_per_s: The current time delay between an incoming |
| 88 | // captured frame (FrameCaptured()) until the |
| 89 | // frame is being processed |
| 90 | // (FrameProcessingStarted()). (Note: if a new |
| 91 | // frame is received before an old frame has |
| 92 | // been processed, the old frame is skipped). |
| 93 | // The delay is expressed in ms delay per sec. |
| 94 | // Only used for stats. |
| 95 | void GetCpuOveruseMetrics(CpuOveruseMetrics* metrics) const; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 96 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 97 | // Only public for testing. |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 98 | int CaptureQueueDelayMsPerS() const; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 99 | int LastProcessingTimeMs() const; |
| 100 | int FramesInQueue() const; |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 101 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 102 | // Implements Module. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 103 | virtual int64_t TimeUntilNextProcess() OVERRIDE; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 104 | virtual int32_t Process() OVERRIDE; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 105 | |
| 106 | private: |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 107 | class EncodeTimeAvg; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 108 | class SendProcessingUsage; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 109 | class CaptureQueueDelay; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 110 | class FrameQueue; |
| 111 | |
tommi@webrtc.org | 18e7585 | 2015-01-29 12:34:40 +0000 | [diff] [blame^] | 112 | // TODO(asapersson): This method is only used on one thread, so it shouldn't |
| 113 | // need a guard. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 114 | void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 115 | |
tommi@webrtc.org | 18e7585 | 2015-01-29 12:34:40 +0000 | [diff] [blame^] | 116 | // TODO(asapersson): This method is always called on the processing thread. |
| 117 | // If locking is required, consider doing that locking inside the |
| 118 | // implementation and reduce scope as much as possible. We should also |
| 119 | // see if we can avoid calling out to other methods while holding the lock. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 120 | bool IsOverusing() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 121 | bool IsUnderusing(int64_t time_now) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 122 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 123 | bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 124 | bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 125 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 126 | void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 127 | |
tommi@webrtc.org | 18e7585 | 2015-01-29 12:34:40 +0000 | [diff] [blame^] | 128 | // Protecting all members except const and those that are only accessed on the |
| 129 | // processing thread. |
| 130 | // TODO(asapersson): See if we can reduce locking. As is, video frame |
| 131 | // processing contends with reading stats and the processing thread. |
| 132 | mutable rtc::CriticalSection crit_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 133 | |
| 134 | // Observer getting overuse reports. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 135 | CpuOveruseObserver* observer_ GUARDED_BY(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 136 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 137 | CpuOveruseOptions options_ GUARDED_BY(crit_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 138 | |
tommi@webrtc.org | a907e01 | 2015-01-28 17:33:12 +0000 | [diff] [blame] | 139 | Clock* const clock_; |
tommi@webrtc.org | 18e7585 | 2015-01-29 12:34:40 +0000 | [diff] [blame^] | 140 | int64_t next_process_time_; // Only accessed on the processing thread. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 141 | int64_t num_process_times_ GUARDED_BY(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 142 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 143 | Statistics capture_deltas_ GUARDED_BY(crit_); |
| 144 | int64_t last_capture_time_ GUARDED_BY(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 145 | |
tommi@webrtc.org | 18e7585 | 2015-01-29 12:34:40 +0000 | [diff] [blame^] | 146 | // These six members are only accessed on the processing thread. |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 147 | int64_t last_overuse_time_; |
| 148 | int checks_above_threshold_; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 149 | int num_overuse_detections_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 150 | |
| 151 | int64_t last_rampup_time_; |
| 152 | bool in_quick_rampup_; |
| 153 | int current_rampup_delay_ms_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 154 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 155 | // Number of pixels of last captured frame. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 156 | int num_pixels_ GUARDED_BY(crit_); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 157 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 158 | int64_t last_encode_sample_ms_ GUARDED_BY(crit_); |
tommi@webrtc.org | 18e7585 | 2015-01-29 12:34:40 +0000 | [diff] [blame^] | 159 | // TODO(asapersson): Can these be regular members (avoid separate heap |
| 160 | // allocs)? |
| 161 | const scoped_ptr<EncodeTimeAvg> encode_time_ GUARDED_BY(crit_); |
| 162 | const scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_); |
| 163 | const scoped_ptr<FrameQueue> frame_queue_ GUARDED_BY(crit_); |
| 164 | |
| 165 | // TODO(asapersson): This variable is only used on one thread, so it shouldn't |
| 166 | // need a guard. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 167 | int64_t last_sample_time_ms_ GUARDED_BY(crit_); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 168 | |
tommi@webrtc.org | 18e7585 | 2015-01-29 12:34:40 +0000 | [diff] [blame^] | 169 | const scoped_ptr<CaptureQueueDelay> capture_queue_delay_ GUARDED_BY(crit_); |
| 170 | |
| 171 | rtc::ThreadChecker processing_thread_; |
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_ |