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