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 | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 15 | #include "webrtc/base/criticalsection.h" |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 16 | #include "webrtc/base/scoped_ptr.h" |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 17 | #include "webrtc/base/exp_filter.h" |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 18 | #include "webrtc/base/thread_annotations.h" |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 19 | #include "webrtc/base/thread_checker.h" |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/interface/module.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_; |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 48 | rtc::scoped_ptr<rtc::ExpFilter> filtered_samples_; |
| 49 | rtc::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: |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 55 | OveruseFrameDetector(Clock* clock, |
| 56 | CpuOveruseMetricsObserver* metrics_observer); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 57 | ~OveruseFrameDetector(); |
| 58 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 59 | // Registers an observer receiving overuse and underuse callbacks. Set |
| 60 | // 'observer' to NULL to disable callbacks. |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 61 | void SetObserver(CpuOveruseObserver* observer); |
| 62 | |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 63 | // Sets options for overuse detection. |
| 64 | void SetOptions(const CpuOveruseOptions& options); |
| 65 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame] | 66 | // Called for each captured frame. |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 67 | void FrameCaptured(int width, int height, int64_t capture_time_ms); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 68 | |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 69 | // Called when the processing of a captured frame is started. |
| 70 | void FrameProcessingStarted(); |
| 71 | |
| 72 | // Called for each encoded frame. |
asapersson@webrtc.org | c7ff8f9 | 2013-11-26 11:12:33 +0000 | [diff] [blame] | 73 | void FrameEncoded(int encode_time_ms); |
| 74 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 75 | // Called for each sent frame. |
| 76 | void FrameSent(int64_t capture_time_ms); |
| 77 | |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 78 | // Only public for testing. |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 79 | int CaptureQueueDelayMsPerS() const; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 80 | int LastProcessingTimeMs() const; |
| 81 | int FramesInQueue() const; |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 82 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 83 | // Implements Module. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame^] | 84 | int64_t TimeUntilNextProcess() override; |
| 85 | int32_t Process() override; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 86 | |
| 87 | private: |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 88 | class EncodeTimeAvg; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 89 | class SendProcessingUsage; |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 90 | class CaptureQueueDelay; |
asapersson@webrtc.org | 9aed002 | 2014-10-16 06:57:12 +0000 | [diff] [blame] | 91 | class FrameQueue; |
| 92 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 93 | void UpdateCpuOveruseMetrics() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 94 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 95 | // TODO(asapersson): This method is only used on one thread, so it shouldn't |
| 96 | // need a guard. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 97 | void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
asapersson@webrtc.org | 9e5b034 | 2013-12-04 13:47:44 +0000 | [diff] [blame] | 98 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 99 | // TODO(asapersson): This method is always called on the processing thread. |
| 100 | // If locking is required, consider doing that locking inside the |
| 101 | // implementation and reduce scope as much as possible. We should also |
| 102 | // 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] | 103 | bool IsOverusing() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 104 | bool IsUnderusing(int64_t time_now) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 105 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 106 | bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 107 | bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 108 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 109 | void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
asapersson@webrtc.org | b60346e | 2014-02-17 19:02:15 +0000 | [diff] [blame] | 110 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 111 | // Protecting all members except const and those that are only accessed on the |
| 112 | // processing thread. |
| 113 | // TODO(asapersson): See if we can reduce locking. As is, video frame |
| 114 | // processing contends with reading stats and the processing thread. |
| 115 | mutable rtc::CriticalSection crit_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 116 | |
| 117 | // Observer getting overuse reports. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 118 | CpuOveruseObserver* observer_ GUARDED_BY(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 119 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 120 | CpuOveruseOptions options_ GUARDED_BY(crit_); |
asapersson@webrtc.org | 8a8c3ef | 2014-03-20 13:15:01 +0000 | [diff] [blame] | 121 | |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 122 | // Stats metrics. |
| 123 | CpuOveruseMetricsObserver* const metrics_observer_; |
| 124 | CpuOveruseMetrics metrics_ GUARDED_BY(crit_); |
| 125 | |
tommi@webrtc.org | a907e01 | 2015-01-28 17:33:12 +0000 | [diff] [blame] | 126 | Clock* const clock_; |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 127 | int64_t next_process_time_; // Only accessed on the processing thread. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 128 | int64_t num_process_times_ GUARDED_BY(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 129 | |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 130 | Statistics capture_deltas_ GUARDED_BY(crit_); |
| 131 | int64_t last_capture_time_ GUARDED_BY(crit_); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 132 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 133 | // These six members are only accessed on the processing thread. |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 134 | int64_t last_overuse_time_; |
| 135 | int checks_above_threshold_; |
asapersson@webrtc.org | d980307 | 2014-06-16 14:27:19 +0000 | [diff] [blame] | 136 | int num_overuse_detections_; |
pbos@webrtc.org | a957570 | 2013-08-30 17:16:32 +0000 | [diff] [blame] | 137 | |
| 138 | int64_t last_rampup_time_; |
| 139 | bool in_quick_rampup_; |
| 140 | int current_rampup_delay_ms_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 141 | |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 142 | // Number of pixels of last captured frame. |
asapersson@webrtc.org | cd621a8 | 2014-11-11 09:40:19 +0000 | [diff] [blame] | 143 | int num_pixels_ GUARDED_BY(crit_); |
asapersson@webrtc.org | e2af622 | 2013-09-23 20:05:39 +0000 | [diff] [blame] | 144 | |
asapersson@webrtc.org | 4414939 | 2015-02-04 08:34:47 +0000 | [diff] [blame] | 145 | int64_t last_encode_sample_ms_; // Only accessed by one thread. |
| 146 | |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 147 | // TODO(asapersson): Can these be regular members (avoid separate heap |
| 148 | // allocs)? |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 149 | const rtc::scoped_ptr<EncodeTimeAvg> encode_time_ GUARDED_BY(crit_); |
| 150 | const rtc::scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_); |
| 151 | const rtc::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 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 155 | const rtc::scoped_ptr<CaptureQueueDelay> capture_queue_delay_ |
| 156 | GUARDED_BY(crit_); |
tommi@webrtc.org | 7a57f8f | 2015-02-08 18:27:46 +0000 | [diff] [blame] | 157 | |
| 158 | rtc::ThreadChecker processing_thread_; |
asapersson@webrtc.org | b24d335 | 2013-11-20 13:51:40 +0000 | [diff] [blame] | 159 | |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 160 | DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); |
| 161 | }; |
| 162 | |
| 163 | } // namespace webrtc |
| 164 | |
| 165 | #endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |