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" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | class Clock; |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 21 | class CpuOveruseObserver; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 22 | class CriticalSectionWrapper; |
| 23 | class VCMExpFilter; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 24 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 25 | // Limits on standard deviation for under/overuse. |
| 26 | #ifdef WEBRTC_LINUX |
| 27 | const float kOveruseStdDevMs = 15.0f; |
| 28 | const float kNormalUseStdDevMs = 7.0f; |
| 29 | #elif WEBRTC_MAC |
| 30 | const float kOveruseStdDevMs = 22.0f; |
| 31 | const float kNormalUseStdDevMs = 12.0f; |
| 32 | #else |
| 33 | const float kOveruseStdDevMs = 17.0f; |
| 34 | const float kNormalUseStdDevMs = 10.0f; |
| 35 | #endif |
| 36 | |
| 37 | // TODO(pbos): Move this somewhere appropriate. |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 38 | class Statistics { |
| 39 | public: |
| 40 | Statistics(); |
| 41 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 42 | void AddSample(float sample_ms); |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 43 | void Reset(); |
| 44 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 45 | float Mean() const; |
| 46 | float StdDev() const; |
| 47 | uint64_t Count() const; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 48 | |
| 49 | private: |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 50 | float InitialMean() const; |
| 51 | float InitialVariance() const; |
| 52 | |
| 53 | float sum_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 54 | uint64_t count_; |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 55 | scoped_ptr<VCMExpFilter> filtered_samples_; |
| 56 | scoped_ptr<VCMExpFilter> filtered_variance_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // Use to detect system overuse based on jitter in incoming frames. |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 60 | class OveruseFrameDetector : public Module { |
| 61 | public: |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 62 | explicit OveruseFrameDetector(Clock* clock, |
| 63 | float normaluse_stddev_ms, |
| 64 | float overuse_stddev_ms); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 65 | ~OveruseFrameDetector(); |
| 66 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 67 | // Registers an observer receiving overuse and underuse callbacks. Set |
| 68 | // 'observer' to NULL to disable callbacks. |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 69 | void SetObserver(CpuOveruseObserver* observer); |
| 70 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 71 | // Called for each captured frame. |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 72 | void FrameCaptured(int width, int height); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 73 | |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 74 | void FrameEncoded(int encode_time_ms); |
| 75 | |
| 76 | int last_capture_jitter_ms() const; |
| 77 | |
| 78 | // Running average of reported encode time (FrameEncoded()). |
| 79 | // Only used for stats. |
| 80 | int avg_encode_time_ms() const; |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 81 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 82 | // Implements Module. |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 83 | virtual int32_t TimeUntilNextProcess() OVERRIDE; |
| 84 | virtual int32_t Process() OVERRIDE; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 85 | |
| 86 | private: |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 87 | bool IsOverusing(); |
| 88 | bool IsUnderusing(int64_t time_now); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 89 | |
| 90 | // Protecting all members. |
| 91 | scoped_ptr<CriticalSectionWrapper> crit_; |
| 92 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 93 | // Limits on standard deviation for under/overuse. |
| 94 | const float normaluse_stddev_ms_; |
| 95 | const float overuse_stddev_ms_; |
| 96 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 97 | // Observer getting overuse reports. |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 98 | CpuOveruseObserver* observer_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 99 | |
| 100 | Clock* clock_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 101 | int64_t next_process_time_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 102 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 103 | Statistics capture_deltas_; |
| 104 | int64_t last_capture_time_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 105 | |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 106 | int64_t last_overuse_time_; |
| 107 | int checks_above_threshold_; |
| 108 | |
| 109 | int64_t last_rampup_time_; |
| 110 | bool in_quick_rampup_; |
| 111 | int current_rampup_delay_ms_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 112 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 113 | // Number of pixels of last captured frame. |
| 114 | int num_pixels_; |
| 115 | |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 116 | float avg_encode_time_ms_; |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 117 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 118 | DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); |
| 119 | }; |
| 120 | |
| 121 | } // namespace webrtc |
| 122 | |
| 123 | #endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |