OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_ | 11 #ifndef WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_ |
12 #define WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_ | 12 #define WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_ |
13 | 13 |
14 #include <list> | |
15 | |
14 #include "webrtc/base/constructormagic.h" | 16 #include "webrtc/base/constructormagic.h" |
15 #include "webrtc/base/criticalsection.h" | 17 #include "webrtc/base/criticalsection.h" |
18 #include "webrtc/base/optional.h" | |
16 #include "webrtc/base/scoped_ptr.h" | 19 #include "webrtc/base/scoped_ptr.h" |
17 #include "webrtc/base/exp_filter.h" | 20 #include "webrtc/base/exp_filter.h" |
åsapersson
2016/02/02 15:19:10
nit: order
| |
18 #include "webrtc/base/thread_annotations.h" | 21 #include "webrtc/base/thread_annotations.h" |
19 #include "webrtc/base/thread_checker.h" | 22 #include "webrtc/base/thread_checker.h" |
20 #include "webrtc/modules/include/module.h" | 23 #include "webrtc/modules/include/module.h" |
21 | 24 |
22 namespace webrtc { | 25 namespace webrtc { |
23 | 26 |
24 class Clock; | 27 class Clock; |
28 class EncodedFrameObserver; | |
29 class VideoFrame; | |
25 | 30 |
26 // CpuOveruseObserver is called when a system overuse is detected and | 31 // CpuOveruseObserver is called when a system overuse is detected and |
27 // VideoEngine cannot keep up the encoding frequency. | 32 // VideoEngine cannot keep up the encoding frequency. |
28 class CpuOveruseObserver { | 33 class CpuOveruseObserver { |
29 public: | 34 public: |
30 // Called as soon as an overuse is detected. | 35 // Called as soon as an overuse is detected. |
31 virtual void OveruseDetected() = 0; | 36 virtual void OveruseDetected() = 0; |
32 // Called periodically when the system is not overused any longer. | 37 // Called periodically when the system is not overused any longer. |
33 virtual void NormalUsage() = 0; | 38 virtual void NormalUsage() = 0; |
34 | 39 |
(...skipping 26 matching lines...) Expand all Loading... | |
61 struct CpuOveruseMetrics { | 66 struct CpuOveruseMetrics { |
62 CpuOveruseMetrics() : encode_usage_percent(-1) {} | 67 CpuOveruseMetrics() : encode_usage_percent(-1) {} |
63 | 68 |
64 int encode_usage_percent; // Average encode time divided by the average time | 69 int encode_usage_percent; // Average encode time divided by the average time |
65 // difference between incoming captured frames. | 70 // difference between incoming captured frames. |
66 }; | 71 }; |
67 | 72 |
68 class CpuOveruseMetricsObserver { | 73 class CpuOveruseMetricsObserver { |
69 public: | 74 public: |
70 virtual ~CpuOveruseMetricsObserver() {} | 75 virtual ~CpuOveruseMetricsObserver() {} |
71 virtual void CpuOveruseMetricsUpdated(const CpuOveruseMetrics& metrics) = 0; | 76 virtual void OnEncodedFrameTimeMeasured(int encode_duration_ms, |
77 const CpuOveruseMetrics& metrics) = 0; | |
72 }; | 78 }; |
73 | 79 |
74 | |
75 // Use to detect system overuse based on the send-side processing time of | 80 // Use to detect system overuse based on the send-side processing time of |
76 // incoming frames. | 81 // incoming frames. |
77 class OveruseFrameDetector : public Module { | 82 class OveruseFrameDetector : public Module { |
78 public: | 83 public: |
79 OveruseFrameDetector(Clock* clock, | 84 OveruseFrameDetector(Clock* clock, |
80 const CpuOveruseOptions& options, | 85 const CpuOveruseOptions& options, |
81 CpuOveruseObserver* overuse_observer, | 86 CpuOveruseObserver* overuse_observer, |
87 EncodedFrameObserver* encoder_timing_, | |
82 CpuOveruseMetricsObserver* metrics_observer); | 88 CpuOveruseMetricsObserver* metrics_observer); |
83 ~OveruseFrameDetector(); | 89 ~OveruseFrameDetector(); |
84 | 90 |
85 // Called for each captured frame. | 91 // Called for each captured frame. |
86 void FrameCaptured(int width, int height, int64_t capture_time_ms); | 92 void FrameCaptured(const VideoFrame& frame); |
87 | 93 |
88 // Called for each sent frame. | 94 // Called for each sent frame. |
89 void FrameSent(int64_t capture_time_ms); | 95 void FrameSent(uint32_t timestamp); |
90 | |
91 // Only public for testing. | |
92 int LastProcessingTimeMs() const; | |
93 int FramesInQueue() const; | |
94 | 96 |
95 // Implements Module. | 97 // Implements Module. |
96 int64_t TimeUntilNextProcess() override; | 98 int64_t TimeUntilNextProcess() override; |
97 int32_t Process() override; | 99 int32_t Process() override; |
98 | 100 |
99 private: | 101 private: |
100 class SendProcessingUsage; | 102 class SendProcessingUsage; |
101 class FrameQueue; | 103 struct FrameTiming { |
104 FrameTiming(int64_t capture_ntp_ms, uint32_t timestamp, int64_t now) | |
105 : capture_ntp_ms(capture_ntp_ms), | |
106 timestamp(timestamp), | |
107 capture_ms(now), | |
108 last_send_ms(-1) {} | |
109 int64_t capture_ntp_ms; | |
110 uint32_t timestamp; | |
111 int64_t capture_ms; | |
112 int64_t last_send_ms; | |
113 }; | |
102 | 114 |
103 void UpdateCpuOveruseMetrics() EXCLUSIVE_LOCKS_REQUIRED(crit_); | 115 void EncodedFrameTimeMeasured(int encode_duration_ms) |
104 | 116 EXCLUSIVE_LOCKS_REQUIRED(crit_); |
105 // TODO(asapersson): This method is only used on one thread, so it shouldn't | |
106 // need a guard. | |
107 void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
108 | 117 |
109 // Only called on the processing thread. | 118 // Only called on the processing thread. |
110 bool IsOverusing(const CpuOveruseMetrics& metrics); | 119 bool IsOverusing(const CpuOveruseMetrics& metrics); |
111 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now); | 120 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now); |
112 | 121 |
113 bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_); | 122 bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
114 bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_); | 123 bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
115 | 124 |
116 void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_); | 125 void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
117 | 126 |
118 // Protecting all members except const and those that are only accessed on the | 127 // Protecting all members except const and those that are only accessed on the |
119 // processing thread. | 128 // processing thread. |
120 // TODO(asapersson): See if we can reduce locking. As is, video frame | 129 // TODO(asapersson): See if we can reduce locking. As is, video frame |
121 // processing contends with reading stats and the processing thread. | 130 // processing contends with reading stats and the processing thread. |
122 rtc::CriticalSection crit_; | 131 rtc::CriticalSection crit_; |
123 | 132 |
124 const CpuOveruseOptions options_; | 133 const CpuOveruseOptions options_; |
125 | 134 |
126 // Observer getting overuse reports. | 135 // Observer getting overuse reports. |
127 CpuOveruseObserver* const observer_; | 136 CpuOveruseObserver* const observer_; |
137 EncodedFrameObserver* const encoder_timing_; | |
128 | 138 |
129 // Stats metrics. | 139 // Stats metrics. |
130 CpuOveruseMetricsObserver* const metrics_observer_; | 140 CpuOveruseMetricsObserver* const metrics_observer_; |
131 CpuOveruseMetrics metrics_ GUARDED_BY(crit_); | 141 rtc::Optional<CpuOveruseMetrics> metrics_ GUARDED_BY(crit_); |
132 | 142 |
133 Clock* const clock_; | 143 Clock* const clock_; |
134 int64_t num_process_times_ GUARDED_BY(crit_); | 144 int64_t num_process_times_ GUARDED_BY(crit_); |
135 | 145 |
136 int64_t last_capture_time_ GUARDED_BY(crit_); | 146 int64_t last_capture_ms_ GUARDED_BY(crit_); |
147 int64_t last_processed_capture_ms_ GUARDED_BY(crit_); | |
137 | 148 |
138 // Number of pixels of last captured frame. | 149 // Number of pixels of last captured frame. |
139 int num_pixels_ GUARDED_BY(crit_); | 150 int num_pixels_ GUARDED_BY(crit_); |
140 | 151 |
141 // These seven members are only accessed on the processing thread. | 152 // These seven members are only accessed on the processing thread. |
142 int64_t next_process_time_; | 153 int64_t next_process_time_ms_; |
143 int64_t last_overuse_time_; | 154 int64_t last_overuse_time_ms_; |
144 int checks_above_threshold_; | 155 int checks_above_threshold_; |
145 int num_overuse_detections_; | 156 int num_overuse_detections_; |
146 int64_t last_rampup_time_; | 157 int64_t last_rampup_time_; |
147 bool in_quick_rampup_; | 158 bool in_quick_rampup_; |
148 int current_rampup_delay_ms_; | 159 int current_rampup_delay_ms_; |
149 | 160 |
150 int64_t last_sample_time_ms_; // Only accessed by one thread. | |
151 | |
152 // TODO(asapersson): Can these be regular members (avoid separate heap | 161 // TODO(asapersson): Can these be regular members (avoid separate heap |
153 // allocs)? | 162 // allocs)? |
154 const rtc::scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_); | 163 const rtc::scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_); |
155 const rtc::scoped_ptr<FrameQueue> frame_queue_ GUARDED_BY(crit_); | 164 std::list<FrameTiming> frame_timing_ GUARDED_BY(crit_); |
156 | 165 |
157 rtc::ThreadChecker processing_thread_; | 166 rtc::ThreadChecker processing_thread_; |
158 | 167 |
159 RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); | 168 RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); |
160 }; | 169 }; |
161 | 170 |
162 } // namespace webrtc | 171 } // namespace webrtc |
163 | 172 |
164 #endif // WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_ | 173 #endif // WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_ |
OLD | NEW |