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> | 14 #include <list> |
15 #include <memory> | 15 #include <memory> |
16 | 16 |
17 #include "webrtc/base/constructormagic.h" | 17 #include "webrtc/base/constructormagic.h" |
18 #include "webrtc/base/criticalsection.h" | |
19 #include "webrtc/base/optional.h" | 18 #include "webrtc/base/optional.h" |
20 #include "webrtc/base/exp_filter.h" | 19 #include "webrtc/base/exp_filter.h" |
20 #include "webrtc/base/sequenced_task_checker.h" | |
21 #include "webrtc/base/task_queue.h" | |
21 #include "webrtc/base/thread_annotations.h" | 22 #include "webrtc/base/thread_annotations.h" |
22 #include "webrtc/base/thread_checker.h" | |
23 #include "webrtc/modules/include/module.h" | |
24 | 23 |
25 namespace webrtc { | 24 namespace webrtc { |
26 | 25 |
27 class Clock; | 26 class Clock; |
28 class EncodedFrameObserver; | 27 class EncodedFrameObserver; |
29 class VideoFrame; | 28 class VideoFrame; |
30 | 29 |
31 // CpuOveruseObserver is called when a system overuse is detected and | 30 // CpuOveruseObserver is called when a system overuse is detected and |
32 // VideoEngine cannot keep up the encoding frequency. | 31 // VideoEngine cannot keep up the encoding frequency. |
33 class CpuOveruseObserver { | 32 class CpuOveruseObserver { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 }; | 64 }; |
66 | 65 |
67 class CpuOveruseMetricsObserver { | 66 class CpuOveruseMetricsObserver { |
68 public: | 67 public: |
69 virtual ~CpuOveruseMetricsObserver() {} | 68 virtual ~CpuOveruseMetricsObserver() {} |
70 virtual void OnEncodedFrameTimeMeasured(int encode_duration_ms, | 69 virtual void OnEncodedFrameTimeMeasured(int encode_duration_ms, |
71 const CpuOveruseMetrics& metrics) = 0; | 70 const CpuOveruseMetrics& metrics) = 0; |
72 }; | 71 }; |
73 | 72 |
74 // Use to detect system overuse based on the send-side processing time of | 73 // Use to detect system overuse based on the send-side processing time of |
75 // incoming frames. | 74 // incoming frames. All methods must be called on a single task queue but it can |
76 class OveruseFrameDetector : public Module { | 75 // be created and destroyed on an arbitrary thread. |
76 // OverUseFrameDetector::StartCheckForOverUse must be called to periodically | |
åsapersson
2016/08/24 08:59:05
nit: OverUse->Overuse
perkj_webrtc
2016/09/01 10:03:30
Done.
| |
77 // check for overuse. | |
78 class OveruseFrameDetector { | |
77 public: | 79 public: |
78 OveruseFrameDetector(Clock* clock, | 80 OveruseFrameDetector(Clock* clock, |
79 const CpuOveruseOptions& options, | 81 const CpuOveruseOptions& options, |
80 CpuOveruseObserver* overuse_observer, | 82 CpuOveruseObserver* overuse_observer, |
81 EncodedFrameObserver* encoder_timing_, | 83 EncodedFrameObserver* encoder_timing_, |
82 CpuOveruseMetricsObserver* metrics_observer); | 84 CpuOveruseMetricsObserver* metrics_observer); |
83 ~OveruseFrameDetector(); | 85 ~OveruseFrameDetector(); |
84 | 86 |
87 // Start to periodically check for overuse. | |
88 void StartCheckForOveruse(); | |
89 | |
90 // StopCheckForOverUse must be called before destruction if | |
åsapersson
2016/08/24 08:59:05
nit: OverUse->Overuse
perkj_webrtc
2016/09/01 10:03:30
Done.
| |
91 // StartCheckForOveruse has been called. | |
92 void StopCheckForOveruse(); | |
93 | |
85 // Called for each captured frame. | 94 // Called for each captured frame. |
86 void FrameCaptured(const VideoFrame& frame); | 95 void FrameCaptured(const VideoFrame& frame, int64_t time_when_first_seen); |
åsapersson
2016/08/24 08:59:05
time_when_first_seen_ms
perkj_webrtc
2016/09/01 10:03:30
Done.
| |
87 | 96 |
88 // Called for each sent frame. | 97 // Called for each sent frame. |
89 void FrameSent(uint32_t timestamp); | 98 void FrameSent(uint32_t timestamp, int64_t time_sent_in_ms); |
90 | 99 |
91 // Implements Module. | 100 protected: |
92 int64_t TimeUntilNextProcess() override; | 101 void CheckForOveruse(); // Protected for test purposes. |
93 void Process() override; | |
94 | 102 |
95 private: | 103 private: |
96 class SendProcessingUsage; | 104 class SendProcessingUsage; |
105 class CheckOveruseTask; | |
97 struct FrameTiming { | 106 struct FrameTiming { |
98 FrameTiming(int64_t capture_ntp_ms, uint32_t timestamp, int64_t now) | 107 FrameTiming(int64_t capture_ntp_ms, uint32_t timestamp, int64_t now) |
99 : capture_ntp_ms(capture_ntp_ms), | 108 : capture_ntp_ms(capture_ntp_ms), |
100 timestamp(timestamp), | 109 timestamp(timestamp), |
101 capture_ms(now), | 110 capture_ms(now), |
102 last_send_ms(-1) {} | 111 last_send_ms(-1) {} |
103 int64_t capture_ntp_ms; | 112 int64_t capture_ntp_ms; |
104 uint32_t timestamp; | 113 uint32_t timestamp; |
105 int64_t capture_ms; | 114 int64_t capture_ms; |
106 int64_t last_send_ms; | 115 int64_t last_send_ms; |
107 }; | 116 }; |
108 | 117 |
109 void EncodedFrameTimeMeasured(int encode_duration_ms) | 118 void EncodedFrameTimeMeasured(int encode_duration_ms); |
110 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
111 | |
112 // Only called on the processing thread. | |
113 bool IsOverusing(const CpuOveruseMetrics& metrics); | 119 bool IsOverusing(const CpuOveruseMetrics& metrics); |
114 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now); | 120 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now); |
115 | 121 |
116 bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_); | 122 bool FrameTimeoutDetected(int64_t now) const; |
117 bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_); | 123 bool FrameSizeChanged(int num_pixels) const; |
118 | 124 |
119 void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_); | 125 void ResetAll(int num_pixels); |
120 | 126 |
121 // Protecting all members except const and those that are only accessed on the | 127 rtc::SequencedTaskChecker task_checker_; |
122 // processing thread. | 128 // Owned by the task queue from where StartCheckForOveruse is called. |
123 // TODO(asapersson): See if we can reduce locking. As is, video frame | 129 CheckOveruseTask* check_over_use_task_; |
åsapersson
2016/08/24 08:59:05
maybe check_overuse_task_
perkj_webrtc
2016/09/01 10:03:29
Done.
| |
124 // processing contends with reading stats and the processing thread. | |
125 rtc::CriticalSection crit_; | |
126 | 130 |
127 const CpuOveruseOptions options_; | 131 const CpuOveruseOptions options_; |
128 | 132 |
129 // Observer getting overuse reports. | 133 // Observer getting overuse reports. |
130 CpuOveruseObserver* const observer_; | 134 CpuOveruseObserver* const observer_; |
131 EncodedFrameObserver* const encoder_timing_; | 135 EncodedFrameObserver* const encoder_timing_; |
132 | 136 |
133 // Stats metrics. | 137 // Stats metrics. |
134 CpuOveruseMetricsObserver* const metrics_observer_; | 138 CpuOveruseMetricsObserver* const metrics_observer_; |
135 rtc::Optional<CpuOveruseMetrics> metrics_ GUARDED_BY(crit_); | 139 rtc::Optional<CpuOveruseMetrics> metrics_ GUARDED_BY(task_checker_); |
140 Clock* const clock_; | |
136 | 141 |
137 Clock* const clock_; | 142 int64_t num_process_times_ GUARDED_BY(task_checker_); |
138 int64_t num_process_times_ GUARDED_BY(crit_); | |
139 | 143 |
140 int64_t last_capture_time_ms_ GUARDED_BY(crit_); | 144 int64_t last_capture_time_ms_ GUARDED_BY(task_checker_); |
141 int64_t last_processed_capture_time_ms_ GUARDED_BY(crit_); | 145 int64_t last_processed_capture_time_ms_ GUARDED_BY(task_checker_); |
142 | 146 |
143 // Number of pixels of last captured frame. | 147 // Number of pixels of last captured frame. |
144 int num_pixels_ GUARDED_BY(crit_); | 148 int num_pixels_ GUARDED_BY(task_checker_); |
145 | 149 int64_t last_overuse_time_ms_ GUARDED_BY(task_checker_); |
146 // These seven members are only accessed on the processing thread. | 150 int checks_above_threshold_ GUARDED_BY(task_checker_); |
147 int64_t next_process_time_ms_; | 151 int num_overuse_detections_ GUARDED_BY(task_checker_); |
148 int64_t last_overuse_time_ms_; | 152 int64_t last_rampup_time_ms_ GUARDED_BY(task_checker_); |
149 int checks_above_threshold_; | 153 bool in_quick_rampup_ GUARDED_BY(task_checker_); |
150 int num_overuse_detections_; | 154 int current_rampup_delay_ms_ GUARDED_BY(task_checker_); |
151 int64_t last_rampup_time_ms_; | |
152 bool in_quick_rampup_; | |
153 int current_rampup_delay_ms_; | |
154 | 155 |
155 // TODO(asapersson): Can these be regular members (avoid separate heap | 156 // TODO(asapersson): Can these be regular members (avoid separate heap |
156 // allocs)? | 157 // allocs)? |
157 const std::unique_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_); | 158 const std::unique_ptr<SendProcessingUsage> usage_ GUARDED_BY(task_checker_); |
158 std::list<FrameTiming> frame_timing_ GUARDED_BY(crit_); | 159 std::list<FrameTiming> frame_timing_ GUARDED_BY(task_checker_); |
159 | |
160 rtc::ThreadChecker processing_thread_; | |
161 | 160 |
162 RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); | 161 RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); |
163 }; | 162 }; |
164 | 163 |
165 } // namespace webrtc | 164 } // namespace webrtc |
166 | 165 |
167 #endif // WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_ | 166 #endif // WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_ |
OLD | NEW |