Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Side by Side Diff: webrtc/video/overuse_frame_detector.cc

Issue 1507903005: Revert of Merge webrtc/video_engine/ into webrtc/video/ (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Resolved merge conflict Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/video/overuse_frame_detector.h ('k') | webrtc/video/overuse_frame_detector_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "webrtc/video/overuse_frame_detector.h"
12
13 #include <assert.h>
14 #include <math.h>
15
16 #include <algorithm>
17 #include <list>
18 #include <map>
19
20 #include "webrtc/base/checks.h"
21 #include "webrtc/base/exp_filter.h"
22 #include "webrtc/base/logging.h"
23 #include "webrtc/system_wrappers/include/clock.h"
24
25 namespace webrtc {
26
27 namespace {
28 const int64_t kProcessIntervalMs = 5000;
29
30 // Delay between consecutive rampups. (Used for quick recovery.)
31 const int kQuickRampUpDelayMs = 10 * 1000;
32 // Delay between rampup attempts. Initially uses standard, scales up to max.
33 const int kStandardRampUpDelayMs = 40 * 1000;
34 const int kMaxRampUpDelayMs = 240 * 1000;
35 // Expontential back-off factor, to prevent annoying up-down behaviour.
36 const double kRampUpBackoffFactor = 2.0;
37
38 // Max number of overuses detected before always applying the rampup delay.
39 const int kMaxOverusesBeforeApplyRampupDelay = 4;
40
41 // The maximum exponent to use in VCMExpFilter.
42 const float kSampleDiffMs = 33.0f;
43 const float kMaxExp = 7.0f;
44
45 } // namespace
46
47 // Class for calculating the processing usage on the send-side (the average
48 // processing time of a frame divided by the average time difference between
49 // captured frames).
50 class OveruseFrameDetector::SendProcessingUsage {
51 public:
52 explicit SendProcessingUsage(const CpuOveruseOptions& options)
53 : kWeightFactorFrameDiff(0.998f),
54 kWeightFactorProcessing(0.995f),
55 kInitialSampleDiffMs(40.0f),
56 kMaxSampleDiffMs(45.0f),
57 count_(0),
58 options_(options),
59 filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)),
60 filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) {
61 Reset();
62 }
63 ~SendProcessingUsage() {}
64
65 void Reset() {
66 count_ = 0;
67 filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff);
68 filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs);
69 filtered_processing_ms_->Reset(kWeightFactorProcessing);
70 filtered_processing_ms_->Apply(1.0f, InitialProcessingMs());
71 }
72
73 void AddCaptureSample(float sample_ms) {
74 float exp = sample_ms / kSampleDiffMs;
75 exp = std::min(exp, kMaxExp);
76 filtered_frame_diff_ms_->Apply(exp, sample_ms);
77 }
78
79 void AddSample(float processing_ms, int64_t diff_last_sample_ms) {
80 ++count_;
81 float exp = diff_last_sample_ms / kSampleDiffMs;
82 exp = std::min(exp, kMaxExp);
83 filtered_processing_ms_->Apply(exp, processing_ms);
84 }
85
86 int Value() const {
87 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) {
88 return static_cast<int>(InitialUsageInPercent() + 0.5f);
89 }
90 float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f);
91 frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs);
92 float encode_usage_percent =
93 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms;
94 return static_cast<int>(encode_usage_percent + 0.5);
95 }
96
97 private:
98 float InitialUsageInPercent() const {
99 // Start in between the underuse and overuse threshold.
100 return (options_.low_encode_usage_threshold_percent +
101 options_.high_encode_usage_threshold_percent) / 2.0f;
102 }
103
104 float InitialProcessingMs() const {
105 return InitialUsageInPercent() * kInitialSampleDiffMs / 100;
106 }
107
108 const float kWeightFactorFrameDiff;
109 const float kWeightFactorProcessing;
110 const float kInitialSampleDiffMs;
111 const float kMaxSampleDiffMs;
112 uint64_t count_;
113 const CpuOveruseOptions options_;
114 rtc::scoped_ptr<rtc::ExpFilter> filtered_processing_ms_;
115 rtc::scoped_ptr<rtc::ExpFilter> filtered_frame_diff_ms_;
116 };
117
118 // Class for calculating the processing time of frames.
119 class OveruseFrameDetector::FrameQueue {
120 public:
121 FrameQueue() : last_processing_time_ms_(-1) {}
122 ~FrameQueue() {}
123
124 // Called when a frame is captured.
125 // Starts the measuring of the processing time of the frame.
126 void Start(int64_t capture_time, int64_t now) {
127 const size_t kMaxSize = 90; // Allows for processing time of 1.5s at 60fps.
128 if (frame_times_.size() > kMaxSize) {
129 LOG(LS_WARNING) << "Max size reached, removed oldest frame.";
130 frame_times_.erase(frame_times_.begin());
131 }
132 if (frame_times_.find(capture_time) != frame_times_.end()) {
133 // Frame should not exist.
134 assert(false);
135 return;
136 }
137 frame_times_[capture_time] = now;
138 }
139
140 // Called when the processing of a frame has finished.
141 // Returns the processing time of the frame.
142 int End(int64_t capture_time, int64_t now) {
143 std::map<int64_t, int64_t>::iterator it = frame_times_.find(capture_time);
144 if (it == frame_times_.end()) {
145 return -1;
146 }
147 // Remove any old frames up to current.
148 // Old frames have been skipped by the capture process thread.
149 // TODO(asapersson): Consider measuring time from first frame in list.
150 last_processing_time_ms_ = now - (*it).second;
151 frame_times_.erase(frame_times_.begin(), ++it);
152 return last_processing_time_ms_;
153 }
154
155 void Reset() { frame_times_.clear(); }
156 int NumFrames() const { return static_cast<int>(frame_times_.size()); }
157 int last_processing_time_ms() const { return last_processing_time_ms_; }
158
159 private:
160 // Captured frames mapped by the capture time.
161 std::map<int64_t, int64_t> frame_times_;
162 int last_processing_time_ms_;
163 };
164
165
166 OveruseFrameDetector::OveruseFrameDetector(
167 Clock* clock,
168 const CpuOveruseOptions& options,
169 CpuOveruseObserver* observer,
170 CpuOveruseMetricsObserver* metrics_observer)
171 : options_(options),
172 observer_(observer),
173 metrics_observer_(metrics_observer),
174 clock_(clock),
175 num_process_times_(0),
176 last_capture_time_(0),
177 num_pixels_(0),
178 next_process_time_(clock_->TimeInMilliseconds()),
179 last_overuse_time_(0),
180 checks_above_threshold_(0),
181 num_overuse_detections_(0),
182 last_rampup_time_(0),
183 in_quick_rampup_(false),
184 current_rampup_delay_ms_(kStandardRampUpDelayMs),
185 last_sample_time_ms_(0),
186 usage_(new SendProcessingUsage(options)),
187 frame_queue_(new FrameQueue()) {
188 RTC_DCHECK(metrics_observer != nullptr);
189 // Make sure stats are initially up-to-date. This simplifies unit testing
190 // since we don't have to trigger an update using one of the methods which
191 // would also alter the overuse state.
192 UpdateCpuOveruseMetrics();
193 processing_thread_.DetachFromThread();
194 }
195
196 OveruseFrameDetector::~OveruseFrameDetector() {
197 }
198
199 int OveruseFrameDetector::LastProcessingTimeMs() const {
200 rtc::CritScope cs(&crit_);
201 return frame_queue_->last_processing_time_ms();
202 }
203
204 int OveruseFrameDetector::FramesInQueue() const {
205 rtc::CritScope cs(&crit_);
206 return frame_queue_->NumFrames();
207 }
208
209 void OveruseFrameDetector::UpdateCpuOveruseMetrics() {
210 metrics_.encode_usage_percent = usage_->Value();
211
212 metrics_observer_->CpuOveruseMetricsUpdated(metrics_);
213 }
214
215 int64_t OveruseFrameDetector::TimeUntilNextProcess() {
216 RTC_DCHECK(processing_thread_.CalledOnValidThread());
217 return next_process_time_ - clock_->TimeInMilliseconds();
218 }
219
220 bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const {
221 if (num_pixels != num_pixels_) {
222 return true;
223 }
224 return false;
225 }
226
227 bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const {
228 if (last_capture_time_ == 0) {
229 return false;
230 }
231 return (now - last_capture_time_) > options_.frame_timeout_interval_ms;
232 }
233
234 void OveruseFrameDetector::ResetAll(int num_pixels) {
235 num_pixels_ = num_pixels;
236 usage_->Reset();
237 frame_queue_->Reset();
238 last_capture_time_ = 0;
239 num_process_times_ = 0;
240 UpdateCpuOveruseMetrics();
241 }
242
243 void OveruseFrameDetector::FrameCaptured(int width,
244 int height,
245 int64_t capture_time_ms) {
246 rtc::CritScope cs(&crit_);
247
248 int64_t now = clock_->TimeInMilliseconds();
249 if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) {
250 ResetAll(width * height);
251 }
252
253 if (last_capture_time_ != 0)
254 usage_->AddCaptureSample(now - last_capture_time_);
255
256 last_capture_time_ = now;
257
258 if (options_.enable_extended_processing_usage) {
259 frame_queue_->Start(capture_time_ms, now);
260 }
261 }
262
263 void OveruseFrameDetector::FrameEncoded(int encode_time_ms) {
264 if (options_.enable_extended_processing_usage)
265 return;
266
267 rtc::CritScope cs(&crit_);
268 AddProcessingTime(encode_time_ms);
269 }
270
271 void OveruseFrameDetector::FrameSent(int64_t capture_time_ms) {
272 if (!options_.enable_extended_processing_usage)
273 return;
274
275 rtc::CritScope cs(&crit_);
276 int delay_ms = frame_queue_->End(capture_time_ms,
277 clock_->TimeInMilliseconds());
278 if (delay_ms > 0) {
279 AddProcessingTime(delay_ms);
280 }
281 }
282
283 void OveruseFrameDetector::AddProcessingTime(int elapsed_ms) {
284 int64_t now = clock_->TimeInMilliseconds();
285 if (last_sample_time_ms_ != 0) {
286 int64_t diff_ms = now - last_sample_time_ms_;
287 usage_->AddSample(elapsed_ms, diff_ms);
288 }
289 last_sample_time_ms_ = now;
290 UpdateCpuOveruseMetrics();
291 }
292
293 int32_t OveruseFrameDetector::Process() {
294 RTC_DCHECK(processing_thread_.CalledOnValidThread());
295
296 int64_t now = clock_->TimeInMilliseconds();
297
298 // Used to protect against Process() being called too often.
299 if (now < next_process_time_)
300 return 0;
301
302 next_process_time_ = now + kProcessIntervalMs;
303
304 CpuOveruseMetrics current_metrics;
305 {
306 rtc::CritScope cs(&crit_);
307 ++num_process_times_;
308
309 current_metrics = metrics_;
310 if (num_process_times_ <= options_.min_process_count)
311 return 0;
312 }
313
314 if (IsOverusing(current_metrics)) {
315 // If the last thing we did was going up, and now have to back down, we need
316 // to check if this peak was short. If so we should back off to avoid going
317 // back and forth between this load, the system doesn't seem to handle it.
318 bool check_for_backoff = last_rampup_time_ > last_overuse_time_;
319 if (check_for_backoff) {
320 if (now - last_rampup_time_ < kStandardRampUpDelayMs ||
321 num_overuse_detections_ > kMaxOverusesBeforeApplyRampupDelay) {
322 // Going up was not ok for very long, back off.
323 current_rampup_delay_ms_ *= kRampUpBackoffFactor;
324 if (current_rampup_delay_ms_ > kMaxRampUpDelayMs)
325 current_rampup_delay_ms_ = kMaxRampUpDelayMs;
326 } else {
327 // Not currently backing off, reset rampup delay.
328 current_rampup_delay_ms_ = kStandardRampUpDelayMs;
329 }
330 }
331
332 last_overuse_time_ = now;
333 in_quick_rampup_ = false;
334 checks_above_threshold_ = 0;
335 ++num_overuse_detections_;
336
337 if (observer_ != NULL)
338 observer_->OveruseDetected();
339 } else if (IsUnderusing(current_metrics, now)) {
340 last_rampup_time_ = now;
341 in_quick_rampup_ = true;
342
343 if (observer_ != NULL)
344 observer_->NormalUsage();
345 }
346
347 int rampup_delay =
348 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
349
350 LOG(LS_VERBOSE) << " Frame stats: "
351 << " encode usage " << current_metrics.encode_usage_percent
352 << " overuse detections " << num_overuse_detections_
353 << " rampup delay " << rampup_delay;
354
355 return 0;
356 }
357
358 bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) {
359 bool overusing = false;
360 if (options_.enable_encode_usage_method) {
361 overusing = metrics.encode_usage_percent >=
362 options_.high_encode_usage_threshold_percent;
363 }
364 if (overusing) {
365 ++checks_above_threshold_;
366 } else {
367 checks_above_threshold_ = 0;
368 }
369 return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
370 }
371
372 bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics,
373 int64_t time_now) {
374 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
375 if (time_now < last_rampup_time_ + delay)
376 return false;
377
378 bool underusing = false;
379 if (options_.enable_encode_usage_method) {
380 underusing = metrics.encode_usage_percent <
381 options_.low_encode_usage_threshold_percent;
382 }
383 return underusing;
384 }
385 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/video/overuse_frame_detector.h ('k') | webrtc/video/overuse_frame_detector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698