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 #include "webrtc/video_engine/overuse_frame_detector.h" | 11 #include "webrtc/video_engine/overuse_frame_detector.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 #include <math.h> | 14 #include <math.h> |
15 | 15 |
16 #include <algorithm> | 16 #include <algorithm> |
17 #include <list> | 17 #include <list> |
18 #include <map> | 18 #include <map> |
19 | 19 |
20 #include "webrtc/base/checks.h" | 20 #include "webrtc/base/checks.h" |
21 #include "webrtc/base/exp_filter.h" | 21 #include "webrtc/base/exp_filter.h" |
22 #include "webrtc/system_wrappers/interface/clock.h" | 22 #include "webrtc/system_wrappers/interface/clock.h" |
23 #include "webrtc/system_wrappers/interface/logging.h" | 23 #include "webrtc/system_wrappers/interface/logging.h" |
24 | 24 |
25 namespace webrtc { | 25 namespace webrtc { |
26 | 26 |
27 // TODO(mflodman) Test different values for all of these to trigger correctly, | |
28 // avoid fluctuations etc. | |
29 namespace { | 27 namespace { |
30 const int64_t kProcessIntervalMs = 5000; | 28 const int64_t kProcessIntervalMs = 5000; |
31 | 29 |
32 // Weight factor to apply to the standard deviation. | |
33 const float kWeightFactor = 0.997f; | |
34 // Weight factor to apply to the average. | |
35 const float kWeightFactorMean = 0.98f; | |
36 | |
37 // Delay between consecutive rampups. (Used for quick recovery.) | 30 // Delay between consecutive rampups. (Used for quick recovery.) |
38 const int kQuickRampUpDelayMs = 10 * 1000; | 31 const int kQuickRampUpDelayMs = 10 * 1000; |
39 // Delay between rampup attempts. Initially uses standard, scales up to max. | 32 // Delay between rampup attempts. Initially uses standard, scales up to max. |
40 const int kStandardRampUpDelayMs = 40 * 1000; | 33 const int kStandardRampUpDelayMs = 40 * 1000; |
41 const int kMaxRampUpDelayMs = 240 * 1000; | 34 const int kMaxRampUpDelayMs = 240 * 1000; |
42 // Expontential back-off factor, to prevent annoying up-down behaviour. | 35 // Expontential back-off factor, to prevent annoying up-down behaviour. |
43 const double kRampUpBackoffFactor = 2.0; | 36 const double kRampUpBackoffFactor = 2.0; |
44 | 37 |
45 // Max number of overuses detected before always applying the rampup delay. | 38 // Max number of overuses detected before always applying the rampup delay. |
46 const int kMaxOverusesBeforeApplyRampupDelay = 4; | 39 const int kMaxOverusesBeforeApplyRampupDelay = 4; |
47 | 40 |
48 // The maximum exponent to use in VCMExpFilter. | 41 // The maximum exponent to use in VCMExpFilter. |
49 const float kSampleDiffMs = 33.0f; | 42 const float kSampleDiffMs = 33.0f; |
50 const float kMaxExp = 7.0f; | 43 const float kMaxExp = 7.0f; |
51 | 44 |
52 } // namespace | 45 } // namespace |
53 | 46 |
54 // TODO(asapersson): Remove this class. Not used. | |
55 Statistics::Statistics(const CpuOveruseOptions& options) | |
56 : sum_(0.0), | |
57 count_(0), | |
58 options_(options), | |
59 filtered_samples_(new rtc::ExpFilter(kWeightFactorMean)), | |
60 filtered_variance_(new rtc::ExpFilter(kWeightFactor)) { | |
61 Reset(); | |
62 } | |
63 | |
64 void Statistics::Reset() { | |
65 sum_ = 0.0; | |
66 count_ = 0; | |
67 filtered_variance_->Reset(kWeightFactor); | |
68 filtered_variance_->Apply(1.0f, InitialVariance()); | |
69 } | |
70 | |
71 void Statistics::AddSample(float sample_ms) { | |
72 sum_ += sample_ms; | |
73 ++count_; | |
74 | |
75 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { | |
76 // Initialize filtered samples. | |
77 filtered_samples_->Reset(kWeightFactorMean); | |
78 filtered_samples_->Apply(1.0f, InitialMean()); | |
79 return; | |
80 } | |
81 | |
82 float exp = sample_ms / kSampleDiffMs; | |
83 exp = std::min(exp, kMaxExp); | |
84 filtered_samples_->Apply(exp, sample_ms); | |
85 filtered_variance_->Apply(exp, (sample_ms - filtered_samples_->filtered()) * | |
86 (sample_ms - filtered_samples_->filtered())); | |
87 } | |
88 | |
89 float Statistics::InitialMean() const { | |
90 if (count_ == 0) | |
91 return 0.0; | |
92 return sum_ / count_; | |
93 } | |
94 | |
95 float Statistics::InitialVariance() const { | |
96 // Start in between the underuse and overuse threshold. | |
97 float average_stddev = (options_.low_capture_jitter_threshold_ms + | |
98 options_.high_capture_jitter_threshold_ms) / 2.0f; | |
99 return average_stddev * average_stddev; | |
100 } | |
101 | |
102 float Statistics::Mean() const { return filtered_samples_->filtered(); } | |
103 | |
104 float Statistics::StdDev() const { | |
105 return sqrt(std::max(filtered_variance_->filtered(), 0.0f)); | |
106 } | |
107 | |
108 uint64_t Statistics::Count() const { return count_; } | |
109 | |
110 | |
111 // Class for calculating the average encode time. | 47 // Class for calculating the average encode time. |
112 class OveruseFrameDetector::EncodeTimeAvg { | 48 class OveruseFrameDetector::EncodeTimeAvg { |
113 public: | 49 public: |
114 EncodeTimeAvg() | 50 EncodeTimeAvg() |
115 : kWeightFactor(0.5f), | 51 : kWeightFactor(0.5f), |
116 kInitialAvgEncodeTimeMs(5.0f), | 52 kInitialAvgEncodeTimeMs(5.0f), |
117 filtered_encode_time_ms_(new rtc::ExpFilter(kWeightFactor)) { | 53 filtered_encode_time_ms_(new rtc::ExpFilter(kWeightFactor)) { |
118 filtered_encode_time_ms_->Apply(1.0f, kInitialAvgEncodeTimeMs); | 54 filtered_encode_time_ms_->Apply(1.0f, kInitialAvgEncodeTimeMs); |
119 } | 55 } |
120 ~EncodeTimeAvg() {} | 56 ~EncodeTimeAvg() {} |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 Clock* clock, | 194 Clock* clock, |
259 const CpuOveruseOptions& options, | 195 const CpuOveruseOptions& options, |
260 CpuOveruseObserver* observer, | 196 CpuOveruseObserver* observer, |
261 CpuOveruseMetricsObserver* metrics_observer) | 197 CpuOveruseMetricsObserver* metrics_observer) |
262 : options_(options), | 198 : options_(options), |
263 observer_(observer), | 199 observer_(observer), |
264 metrics_observer_(metrics_observer), | 200 metrics_observer_(metrics_observer), |
265 clock_(clock), | 201 clock_(clock), |
266 next_process_time_(clock_->TimeInMilliseconds()), | 202 next_process_time_(clock_->TimeInMilliseconds()), |
267 num_process_times_(0), | 203 num_process_times_(0), |
268 capture_deltas_(options), | |
269 last_capture_time_(0), | 204 last_capture_time_(0), |
270 last_overuse_time_(0), | 205 last_overuse_time_(0), |
271 checks_above_threshold_(0), | 206 checks_above_threshold_(0), |
272 num_overuse_detections_(0), | 207 num_overuse_detections_(0), |
273 last_rampup_time_(0), | 208 last_rampup_time_(0), |
274 in_quick_rampup_(false), | 209 in_quick_rampup_(false), |
275 current_rampup_delay_ms_(kStandardRampUpDelayMs), | 210 current_rampup_delay_ms_(kStandardRampUpDelayMs), |
276 num_pixels_(0), | 211 num_pixels_(0), |
277 last_encode_sample_ms_(0), | 212 last_encode_sample_ms_(0), |
278 encode_time_(new EncodeTimeAvg()), | 213 encode_time_(new EncodeTimeAvg()), |
(...skipping 15 matching lines...) Expand all Loading... | |
294 rtc::CritScope cs(&crit_); | 229 rtc::CritScope cs(&crit_); |
295 return frame_queue_->last_processing_time_ms(); | 230 return frame_queue_->last_processing_time_ms(); |
296 } | 231 } |
297 | 232 |
298 int OveruseFrameDetector::FramesInQueue() const { | 233 int OveruseFrameDetector::FramesInQueue() const { |
299 rtc::CritScope cs(&crit_); | 234 rtc::CritScope cs(&crit_); |
300 return frame_queue_->NumFrames(); | 235 return frame_queue_->NumFrames(); |
301 } | 236 } |
302 | 237 |
303 void OveruseFrameDetector::UpdateCpuOveruseMetrics() { | 238 void OveruseFrameDetector::UpdateCpuOveruseMetrics() { |
304 metrics_.capture_jitter_ms = static_cast<int>(capture_deltas_.StdDev() + 0.5); | |
305 metrics_.avg_encode_time_ms = encode_time_->Value(); | 239 metrics_.avg_encode_time_ms = encode_time_->Value(); |
306 metrics_.encode_usage_percent = usage_->Value(); | 240 metrics_.encode_usage_percent = usage_->Value(); |
307 | 241 |
308 metrics_observer_->CpuOveruseMetricsUpdated(metrics_); | 242 metrics_observer_->CpuOveruseMetricsUpdated(metrics_); |
309 } | 243 } |
310 | 244 |
311 int64_t OveruseFrameDetector::TimeUntilNextProcess() { | 245 int64_t OveruseFrameDetector::TimeUntilNextProcess() { |
312 DCHECK(processing_thread_.CalledOnValidThread()); | 246 DCHECK(processing_thread_.CalledOnValidThread()); |
313 return next_process_time_ - clock_->TimeInMilliseconds(); | 247 return next_process_time_ - clock_->TimeInMilliseconds(); |
314 } | 248 } |
315 | 249 |
316 bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const { | 250 bool OveruseFrameDetector::FrameSizeChanged(int num_pixels) const { |
317 if (num_pixels != num_pixels_) { | 251 if (num_pixels != num_pixels_) { |
318 return true; | 252 return true; |
319 } | 253 } |
320 return false; | 254 return false; |
321 } | 255 } |
322 | 256 |
323 bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const { | 257 bool OveruseFrameDetector::FrameTimeoutDetected(int64_t now) const { |
324 if (last_capture_time_ == 0) { | 258 if (last_capture_time_ == 0) { |
325 return false; | 259 return false; |
326 } | 260 } |
327 return (now - last_capture_time_) > options_.frame_timeout_interval_ms; | 261 return (now - last_capture_time_) > options_.frame_timeout_interval_ms; |
328 } | 262 } |
329 | 263 |
330 void OveruseFrameDetector::ResetAll(int num_pixels) { | 264 void OveruseFrameDetector::ResetAll(int num_pixels) { |
331 num_pixels_ = num_pixels; | 265 num_pixels_ = num_pixels; |
332 capture_deltas_.Reset(); | |
333 usage_->Reset(); | 266 usage_->Reset(); |
334 frame_queue_->Reset(); | 267 frame_queue_->Reset(); |
335 last_capture_time_ = 0; | 268 last_capture_time_ = 0; |
336 num_process_times_ = 0; | 269 num_process_times_ = 0; |
337 UpdateCpuOveruseMetrics(); | 270 UpdateCpuOveruseMetrics(); |
338 } | 271 } |
339 | 272 |
340 void OveruseFrameDetector::FrameCaptured(int width, | 273 void OveruseFrameDetector::FrameCaptured(int width, |
341 int height, | 274 int height, |
342 int64_t capture_time_ms) { | 275 int64_t capture_time_ms) { |
343 rtc::CritScope cs(&crit_); | 276 rtc::CritScope cs(&crit_); |
344 | 277 |
345 int64_t now = clock_->TimeInMilliseconds(); | 278 int64_t now = clock_->TimeInMilliseconds(); |
346 if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) { | 279 if (FrameSizeChanged(width * height) || FrameTimeoutDetected(now)) { |
347 ResetAll(width * height); | 280 ResetAll(width * height); |
348 } | 281 } |
349 | 282 |
350 if (last_capture_time_ != 0) { | 283 if (last_capture_time_ != 0) { |
pbos-webrtc
2015/07/21 18:14:09
remove {}s
åsapersson
2015/07/22 05:50:24
Done.
| |
351 capture_deltas_.AddSample(now - last_capture_time_); | |
352 usage_->AddCaptureSample(now - last_capture_time_); | 284 usage_->AddCaptureSample(now - last_capture_time_); |
353 } | 285 } |
354 last_capture_time_ = now; | 286 last_capture_time_ = now; |
355 | 287 |
356 if (options_.enable_extended_processing_usage) { | 288 if (options_.enable_extended_processing_usage) { |
357 frame_queue_->Start(capture_time_ms, now); | 289 frame_queue_->Start(capture_time_ms, now); |
358 } | 290 } |
359 UpdateCpuOveruseMetrics(); | |
360 } | 291 } |
361 | 292 |
362 void OveruseFrameDetector::FrameEncoded(int encode_time_ms) { | 293 void OveruseFrameDetector::FrameEncoded(int encode_time_ms) { |
363 rtc::CritScope cs(&crit_); | 294 rtc::CritScope cs(&crit_); |
364 int64_t now = clock_->TimeInMilliseconds(); | 295 int64_t now = clock_->TimeInMilliseconds(); |
365 if (last_encode_sample_ms_ != 0) { | 296 if (last_encode_sample_ms_ != 0) { |
366 int64_t diff_ms = now - last_encode_sample_ms_; | 297 int64_t diff_ms = now - last_encode_sample_ms_; |
367 encode_time_->AddSample(encode_time_ms, diff_ms); | 298 encode_time_->AddSample(encode_time_ms, diff_ms); |
368 } | 299 } |
369 last_encode_sample_ms_ = now; | 300 last_encode_sample_ms_ = now; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
442 } else if (IsUnderusing(now)) { | 373 } else if (IsUnderusing(now)) { |
443 last_rampup_time_ = now; | 374 last_rampup_time_ = now; |
444 in_quick_rampup_ = true; | 375 in_quick_rampup_ = true; |
445 | 376 |
446 if (observer_ != NULL) | 377 if (observer_ != NULL) |
447 observer_->NormalUsage(); | 378 observer_->NormalUsage(); |
448 } | 379 } |
449 | 380 |
450 int rampup_delay = | 381 int rampup_delay = |
451 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; | 382 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
452 LOG(LS_VERBOSE) << " Frame stats: capture avg: " << capture_deltas_.Mean() | 383 LOG(LS_VERBOSE) << " Frame stats: encode usage: " << usage_->Value() |
453 << " capture stddev " << capture_deltas_.StdDev() | |
454 << " encode usage " << usage_->Value() | |
455 << " overuse detections " << num_overuse_detections_ | 384 << " overuse detections " << num_overuse_detections_ |
456 << " rampup delay " << rampup_delay; | 385 << " rampup delay " << rampup_delay; |
457 | 386 |
458 return 0; | 387 return 0; |
459 } | 388 } |
460 | 389 |
461 bool OveruseFrameDetector::IsOverusing() { | 390 bool OveruseFrameDetector::IsOverusing() { |
462 bool overusing = false; | 391 bool overusing = false; |
463 if (options_.enable_capture_jitter_method) { | 392 if (options_.enable_encode_usage_method) { |
pbos-webrtc
2015/07/21 18:14:09
remove {}s
åsapersson
2015/07/22 05:50:24
Done.
| |
464 overusing = capture_deltas_.StdDev() >= | |
465 options_.high_capture_jitter_threshold_ms; | |
466 } else if (options_.enable_encode_usage_method) { | |
467 overusing = usage_->Value() >= options_.high_encode_usage_threshold_percent; | 393 overusing = usage_->Value() >= options_.high_encode_usage_threshold_percent; |
468 } | 394 } |
469 | 395 |
470 if (overusing) { | 396 if (overusing) { |
471 ++checks_above_threshold_; | 397 ++checks_above_threshold_; |
472 } else { | 398 } else { |
473 checks_above_threshold_ = 0; | 399 checks_above_threshold_ = 0; |
474 } | 400 } |
475 return checks_above_threshold_ >= options_.high_threshold_consecutive_count; | 401 return checks_above_threshold_ >= options_.high_threshold_consecutive_count; |
476 } | 402 } |
477 | 403 |
478 bool OveruseFrameDetector::IsUnderusing(int64_t time_now) { | 404 bool OveruseFrameDetector::IsUnderusing(int64_t time_now) { |
479 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; | 405 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
480 if (time_now < last_rampup_time_ + delay) | 406 if (time_now < last_rampup_time_ + delay) |
481 return false; | 407 return false; |
482 | 408 |
483 bool underusing = false; | 409 bool underusing = false; |
484 if (options_.enable_capture_jitter_method) { | 410 if (options_.enable_encode_usage_method) { |
pbos-webrtc
2015/07/21 18:14:09
remove {}s
åsapersson
2015/07/22 05:50:24
Done.
| |
485 underusing = capture_deltas_.StdDev() < | |
486 options_.low_capture_jitter_threshold_ms; | |
487 } else if (options_.enable_encode_usage_method) { | |
488 underusing = usage_->Value() < options_.low_encode_usage_threshold_percent; | 411 underusing = usage_->Value() < options_.low_encode_usage_threshold_percent; |
489 } | 412 } |
490 return underusing; | 413 return underusing; |
491 } | 414 } |
492 } // namespace webrtc | 415 } // namespace webrtc |
OLD | NEW |