| OLD | NEW |
| (Empty) |
| 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 | |
| 14 #include "webrtc/base/constructormagic.h" | |
| 15 #include "webrtc/base/criticalsection.h" | |
| 16 #include "webrtc/base/scoped_ptr.h" | |
| 17 #include "webrtc/base/exp_filter.h" | |
| 18 #include "webrtc/base/thread_annotations.h" | |
| 19 #include "webrtc/base/thread_checker.h" | |
| 20 #include "webrtc/modules/include/module.h" | |
| 21 | |
| 22 namespace webrtc { | |
| 23 | |
| 24 class Clock; | |
| 25 | |
| 26 // CpuOveruseObserver is called when a system overuse is detected and | |
| 27 // VideoEngine cannot keep up the encoding frequency. | |
| 28 class CpuOveruseObserver { | |
| 29 public: | |
| 30 // Called as soon as an overuse is detected. | |
| 31 virtual void OveruseDetected() = 0; | |
| 32 // Called periodically when the system is not overused any longer. | |
| 33 virtual void NormalUsage() = 0; | |
| 34 | |
| 35 protected: | |
| 36 virtual ~CpuOveruseObserver() {} | |
| 37 }; | |
| 38 | |
| 39 struct CpuOveruseOptions { | |
| 40 CpuOveruseOptions() | |
| 41 : enable_encode_usage_method(true), | |
| 42 low_encode_usage_threshold_percent(55), | |
| 43 high_encode_usage_threshold_percent(85), | |
| 44 enable_extended_processing_usage(true), | |
| 45 frame_timeout_interval_ms(1500), | |
| 46 min_frame_samples(120), | |
| 47 min_process_count(3), | |
| 48 high_threshold_consecutive_count(2) {} | |
| 49 | |
| 50 // Method based on encode time of frames. | |
| 51 bool enable_encode_usage_method; | |
| 52 int low_encode_usage_threshold_percent; // Threshold for triggering underuse. | |
| 53 int high_encode_usage_threshold_percent; // Threshold for triggering overuse. | |
| 54 bool enable_extended_processing_usage; // Include a larger time span (in | |
| 55 // addition to encode time) for | |
| 56 // measuring the processing time of a | |
| 57 // frame. | |
| 58 // General settings. | |
| 59 int frame_timeout_interval_ms; // The maximum allowed interval between two | |
| 60 // frames before resetting estimations. | |
| 61 int min_frame_samples; // The minimum number of frames required. | |
| 62 int min_process_count; // The number of initial process times required before | |
| 63 // triggering an overuse/underuse. | |
| 64 int high_threshold_consecutive_count; // The number of consecutive checks | |
| 65 // above the high threshold before | |
| 66 // triggering an overuse. | |
| 67 }; | |
| 68 | |
| 69 struct CpuOveruseMetrics { | |
| 70 CpuOveruseMetrics() : encode_usage_percent(-1) {} | |
| 71 | |
| 72 int encode_usage_percent; // Average encode time divided by the average time | |
| 73 // difference between incoming captured frames. | |
| 74 }; | |
| 75 | |
| 76 class CpuOveruseMetricsObserver { | |
| 77 public: | |
| 78 virtual ~CpuOveruseMetricsObserver() {} | |
| 79 virtual void CpuOveruseMetricsUpdated(const CpuOveruseMetrics& metrics) = 0; | |
| 80 }; | |
| 81 | |
| 82 | |
| 83 // Use to detect system overuse based on the send-side processing time of | |
| 84 // incoming frames. | |
| 85 class OveruseFrameDetector : public Module { | |
| 86 public: | |
| 87 OveruseFrameDetector(Clock* clock, | |
| 88 const CpuOveruseOptions& options, | |
| 89 CpuOveruseObserver* overuse_observer, | |
| 90 CpuOveruseMetricsObserver* metrics_observer); | |
| 91 ~OveruseFrameDetector(); | |
| 92 | |
| 93 // Called for each captured frame. | |
| 94 void FrameCaptured(int width, int height, int64_t capture_time_ms); | |
| 95 | |
| 96 // Called for each encoded frame. | |
| 97 void FrameEncoded(int encode_time_ms); | |
| 98 | |
| 99 // Called for each sent frame. | |
| 100 void FrameSent(int64_t capture_time_ms); | |
| 101 | |
| 102 // Only public for testing. | |
| 103 int LastProcessingTimeMs() const; | |
| 104 int FramesInQueue() const; | |
| 105 | |
| 106 // Implements Module. | |
| 107 int64_t TimeUntilNextProcess() override; | |
| 108 int32_t Process() override; | |
| 109 | |
| 110 private: | |
| 111 class SendProcessingUsage; | |
| 112 class FrameQueue; | |
| 113 | |
| 114 void UpdateCpuOveruseMetrics() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 115 | |
| 116 // TODO(asapersson): This method is only used on one thread, so it shouldn't | |
| 117 // need a guard. | |
| 118 void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 119 | |
| 120 // Only called on the processing thread. | |
| 121 bool IsOverusing(const CpuOveruseMetrics& metrics); | |
| 122 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now); | |
| 123 | |
| 124 bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 125 bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 126 | |
| 127 void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 128 | |
| 129 // Protecting all members except const and those that are only accessed on the | |
| 130 // processing thread. | |
| 131 // TODO(asapersson): See if we can reduce locking. As is, video frame | |
| 132 // processing contends with reading stats and the processing thread. | |
| 133 mutable rtc::CriticalSection crit_; | |
| 134 | |
| 135 const CpuOveruseOptions options_; | |
| 136 | |
| 137 // Observer getting overuse reports. | |
| 138 CpuOveruseObserver* const observer_; | |
| 139 | |
| 140 // Stats metrics. | |
| 141 CpuOveruseMetricsObserver* const metrics_observer_; | |
| 142 CpuOveruseMetrics metrics_ GUARDED_BY(crit_); | |
| 143 | |
| 144 Clock* const clock_; | |
| 145 int64_t num_process_times_ GUARDED_BY(crit_); | |
| 146 | |
| 147 int64_t last_capture_time_ GUARDED_BY(crit_); | |
| 148 | |
| 149 // Number of pixels of last captured frame. | |
| 150 int num_pixels_ GUARDED_BY(crit_); | |
| 151 | |
| 152 // These seven members are only accessed on the processing thread. | |
| 153 int64_t next_process_time_; | |
| 154 int64_t last_overuse_time_; | |
| 155 int checks_above_threshold_; | |
| 156 int num_overuse_detections_; | |
| 157 int64_t last_rampup_time_; | |
| 158 bool in_quick_rampup_; | |
| 159 int current_rampup_delay_ms_; | |
| 160 | |
| 161 int64_t last_sample_time_ms_; // Only accessed by one thread. | |
| 162 | |
| 163 // TODO(asapersson): Can these be regular members (avoid separate heap | |
| 164 // allocs)? | |
| 165 const rtc::scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_); | |
| 166 const rtc::scoped_ptr<FrameQueue> frame_queue_ GUARDED_BY(crit_); | |
| 167 | |
| 168 rtc::ThreadChecker processing_thread_; | |
| 169 | |
| 170 RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); | |
| 171 }; | |
| 172 | |
| 173 } // namespace webrtc | |
| 174 | |
| 175 #endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ | |
| OLD | NEW |