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