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/overuse_frame_detector.h" | 11 #include "webrtc/video/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 #include <string> |
| 20 #include <utility> |
19 | 21 |
20 #include "webrtc/api/video/video_frame.h" | 22 #include "webrtc/api/video/video_frame.h" |
21 #include "webrtc/base/checks.h" | 23 #include "webrtc/base/checks.h" |
22 #include "webrtc/base/logging.h" | 24 #include "webrtc/base/logging.h" |
23 #include "webrtc/base/numerics/exp_filter.h" | 25 #include "webrtc/base/numerics/exp_filter.h" |
24 #include "webrtc/common_video/include/frame_callback.h" | 26 #include "webrtc/common_video/include/frame_callback.h" |
| 27 #include "webrtc/system_wrappers/include/field_trial.h" |
25 | 28 |
26 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) | 29 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
27 #include <mach/mach.h> | 30 #include <mach/mach.h> |
28 #endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) | 31 #endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
29 | 32 |
30 namespace webrtc { | 33 namespace webrtc { |
31 | 34 |
32 namespace { | 35 namespace { |
33 const int64_t kCheckForOveruseIntervalMs = 5000; | 36 const int64_t kCheckForOveruseIntervalMs = 5000; |
34 const int64_t kTimeToFirstCheckForOveruseMs = 100; | 37 const int64_t kTimeToFirstCheckForOveruseMs = 100; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 : kWeightFactorFrameDiff(0.998f), | 112 : kWeightFactorFrameDiff(0.998f), |
110 kWeightFactorProcessing(0.995f), | 113 kWeightFactorProcessing(0.995f), |
111 kInitialSampleDiffMs(40.0f), | 114 kInitialSampleDiffMs(40.0f), |
112 kMaxSampleDiffMs(45.0f), | 115 kMaxSampleDiffMs(45.0f), |
113 count_(0), | 116 count_(0), |
114 options_(options), | 117 options_(options), |
115 filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)), | 118 filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)), |
116 filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) { | 119 filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) { |
117 Reset(); | 120 Reset(); |
118 } | 121 } |
119 ~SendProcessingUsage() {} | 122 virtual ~SendProcessingUsage() {} |
120 | 123 |
121 void Reset() { | 124 void Reset() { |
122 count_ = 0; | 125 count_ = 0; |
123 filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff); | 126 filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff); |
124 filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs); | 127 filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs); |
125 filtered_processing_ms_->Reset(kWeightFactorProcessing); | 128 filtered_processing_ms_->Reset(kWeightFactorProcessing); |
126 filtered_processing_ms_->Apply(1.0f, InitialProcessingMs()); | 129 filtered_processing_ms_->Apply(1.0f, InitialProcessingMs()); |
127 } | 130 } |
128 | 131 |
129 void AddCaptureSample(float sample_ms) { | 132 void AddCaptureSample(float sample_ms) { |
130 float exp = sample_ms / kSampleDiffMs; | 133 float exp = sample_ms / kSampleDiffMs; |
131 exp = std::min(exp, kMaxExp); | 134 exp = std::min(exp, kMaxExp); |
132 filtered_frame_diff_ms_->Apply(exp, sample_ms); | 135 filtered_frame_diff_ms_->Apply(exp, sample_ms); |
133 } | 136 } |
134 | 137 |
135 void AddSample(float processing_ms, int64_t diff_last_sample_ms) { | 138 void AddSample(float processing_ms, int64_t diff_last_sample_ms) { |
136 ++count_; | 139 ++count_; |
137 float exp = diff_last_sample_ms / kSampleDiffMs; | 140 float exp = diff_last_sample_ms / kSampleDiffMs; |
138 exp = std::min(exp, kMaxExp); | 141 exp = std::min(exp, kMaxExp); |
139 filtered_processing_ms_->Apply(exp, processing_ms); | 142 filtered_processing_ms_->Apply(exp, processing_ms); |
140 } | 143 } |
141 | 144 |
142 int Value() const { | 145 virtual int Value() { |
143 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { | 146 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
144 return static_cast<int>(InitialUsageInPercent() + 0.5f); | 147 return static_cast<int>(InitialUsageInPercent() + 0.5f); |
145 } | 148 } |
146 float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f); | 149 float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f); |
147 frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs); | 150 frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs); |
148 float encode_usage_percent = | 151 float encode_usage_percent = |
149 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms; | 152 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms; |
150 return static_cast<int>(encode_usage_percent + 0.5); | 153 return static_cast<int>(encode_usage_percent + 0.5); |
151 } | 154 } |
152 | 155 |
(...skipping 11 matching lines...) Expand all Loading... |
164 const float kWeightFactorFrameDiff; | 167 const float kWeightFactorFrameDiff; |
165 const float kWeightFactorProcessing; | 168 const float kWeightFactorProcessing; |
166 const float kInitialSampleDiffMs; | 169 const float kInitialSampleDiffMs; |
167 const float kMaxSampleDiffMs; | 170 const float kMaxSampleDiffMs; |
168 uint64_t count_; | 171 uint64_t count_; |
169 const CpuOveruseOptions options_; | 172 const CpuOveruseOptions options_; |
170 std::unique_ptr<rtc::ExpFilter> filtered_processing_ms_; | 173 std::unique_ptr<rtc::ExpFilter> filtered_processing_ms_; |
171 std::unique_ptr<rtc::ExpFilter> filtered_frame_diff_ms_; | 174 std::unique_ptr<rtc::ExpFilter> filtered_frame_diff_ms_; |
172 }; | 175 }; |
173 | 176 |
| 177 // Class used for manual testing of overuse, enabled via field trial flag. |
| 178 class OveruseFrameDetector::OverdoseInjector |
| 179 : public OveruseFrameDetector::SendProcessingUsage { |
| 180 public: |
| 181 OverdoseInjector(const CpuOveruseOptions& options, |
| 182 int64_t overuse_period_ms, |
| 183 int64_t normal_period_ms) |
| 184 : OveruseFrameDetector::SendProcessingUsage(options), |
| 185 overuse_period_ms_(overuse_period_ms), |
| 186 normal_period_ms_(normal_period_ms), |
| 187 is_overusing_(false), |
| 188 last_toggling_ms_(-1) { |
| 189 RTC_DCHECK_GT(overuse_period_ms, 0); |
| 190 RTC_DCHECK_GT(normal_period_ms, 0); |
| 191 LOG(LS_INFO) << "Simulating overuse with intervals " << normal_period_ms |
| 192 << "ms normal mode, " << overuse_period_ms |
| 193 << "ms overuse mode."; |
| 194 } |
| 195 |
| 196 ~OverdoseInjector() override {} |
| 197 |
| 198 int Value() override { |
| 199 int64_t now_ms = rtc::TimeMillis(); |
| 200 if (last_toggling_ms_ == -1) { |
| 201 last_toggling_ms_ = now_ms; |
| 202 } else { |
| 203 int64_t toggle_time_ms = |
| 204 last_toggling_ms_ + |
| 205 (is_overusing_ ? overuse_period_ms_ : normal_period_ms_); |
| 206 if (now_ms > toggle_time_ms) { |
| 207 is_overusing_ = !is_overusing_; |
| 208 last_toggling_ms_ = now_ms; |
| 209 if (is_overusing_) { |
| 210 LOG(LS_INFO) << "Simulating CPU overuse."; |
| 211 } else { |
| 212 LOG(LS_INFO) << "Disabling CPU overuse simulation."; |
| 213 } |
| 214 } |
| 215 } |
| 216 |
| 217 if (is_overusing_) |
| 218 return 250; // 250% should be enough for anyone. |
| 219 |
| 220 return SendProcessingUsage::Value(); |
| 221 } |
| 222 |
| 223 private: |
| 224 const int64_t overuse_period_ms_; |
| 225 const int64_t normal_period_ms_; |
| 226 bool is_overusing_; |
| 227 int64_t last_toggling_ms_; |
| 228 }; |
| 229 |
| 230 std::unique_ptr<OveruseFrameDetector::SendProcessingUsage> |
| 231 OveruseFrameDetector::CreateSendProcessingUsage( |
| 232 const CpuOveruseOptions& options) { |
| 233 std::unique_ptr<SendProcessingUsage> instance; |
| 234 std::string toggling_interval = |
| 235 field_trial::FindFullName("WebRTC-ForceSimulatedOveruseIntervalMs"); |
| 236 if (!toggling_interval.empty()) { |
| 237 int overuse_period_ms = 0; |
| 238 int normal_period_ms = 0; |
| 239 if (sscanf(toggling_interval.c_str(), "%d-%d", &overuse_period_ms, |
| 240 &normal_period_ms) == 2) { |
| 241 if (overuse_period_ms > 0 && normal_period_ms > 0) { |
| 242 instance.reset( |
| 243 new OverdoseInjector(options, overuse_period_ms, normal_period_ms)); |
| 244 } else { |
| 245 LOG(LS_WARNING) << "Invalid (non-positive) overuse / normal periods: " |
| 246 << overuse_period_ms << " / " << normal_period_ms; |
| 247 } |
| 248 } else { |
| 249 LOG(LS_WARNING) << "Malformed toggling interval: " << toggling_interval; |
| 250 } |
| 251 } |
| 252 |
| 253 if (!instance) { |
| 254 // No valid overuse simulation parameters set, use normal usage class. |
| 255 instance.reset(new SendProcessingUsage(options)); |
| 256 } |
| 257 |
| 258 return instance; |
| 259 } |
| 260 |
174 class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask { | 261 class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask { |
175 public: | 262 public: |
176 explicit CheckOveruseTask(OveruseFrameDetector* overuse_detector) | 263 explicit CheckOveruseTask(OveruseFrameDetector* overuse_detector) |
177 : overuse_detector_(overuse_detector) { | 264 : overuse_detector_(overuse_detector) { |
178 rtc::TaskQueue::Current()->PostDelayedTask( | 265 rtc::TaskQueue::Current()->PostDelayedTask( |
179 std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs); | 266 std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs); |
180 } | 267 } |
181 | 268 |
182 void Stop() { | 269 void Stop() { |
183 RTC_CHECK(task_checker_.CalledSequentially()); | 270 RTC_CHECK(task_checker_.CalledSequentially()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 // TODO(nisse): Use rtc::Optional | 302 // TODO(nisse): Use rtc::Optional |
216 last_capture_time_us_(-1), | 303 last_capture_time_us_(-1), |
217 last_processed_capture_time_us_(-1), | 304 last_processed_capture_time_us_(-1), |
218 num_pixels_(0), | 305 num_pixels_(0), |
219 last_overuse_time_ms_(-1), | 306 last_overuse_time_ms_(-1), |
220 checks_above_threshold_(0), | 307 checks_above_threshold_(0), |
221 num_overuse_detections_(0), | 308 num_overuse_detections_(0), |
222 last_rampup_time_ms_(-1), | 309 last_rampup_time_ms_(-1), |
223 in_quick_rampup_(false), | 310 in_quick_rampup_(false), |
224 current_rampup_delay_ms_(kStandardRampUpDelayMs), | 311 current_rampup_delay_ms_(kStandardRampUpDelayMs), |
225 usage_(new SendProcessingUsage(options)) { | 312 usage_(CreateSendProcessingUsage(options)) { |
226 task_checker_.Detach(); | 313 task_checker_.Detach(); |
227 } | 314 } |
228 | 315 |
229 OveruseFrameDetector::~OveruseFrameDetector() { | 316 OveruseFrameDetector::~OveruseFrameDetector() { |
230 RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called."; | 317 RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called."; |
231 } | 318 } |
232 | 319 |
233 void OveruseFrameDetector::StartCheckForOveruse() { | 320 void OveruseFrameDetector::StartCheckForOveruse() { |
234 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); | 321 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
235 RTC_DCHECK(!check_overuse_task_); | 322 RTC_DCHECK(!check_overuse_task_); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 } | 400 } |
314 // TODO(pbos): Handle the case/log errors when not finding the corresponding | 401 // TODO(pbos): Handle the case/log errors when not finding the corresponding |
315 // frame (either very slow encoding or incorrect wrong timestamps returned | 402 // frame (either very slow encoding or incorrect wrong timestamps returned |
316 // from the encoder). | 403 // from the encoder). |
317 // This is currently the case for all frames on ChromeOS, so logging them | 404 // This is currently the case for all frames on ChromeOS, so logging them |
318 // would be spammy, and triggering overuse would be wrong. | 405 // would be spammy, and triggering overuse would be wrong. |
319 // https://crbug.com/350106 | 406 // https://crbug.com/350106 |
320 while (!frame_timing_.empty()) { | 407 while (!frame_timing_.empty()) { |
321 FrameTiming timing = frame_timing_.front(); | 408 FrameTiming timing = frame_timing_.front(); |
322 if (time_sent_in_us - timing.capture_us < | 409 if (time_sent_in_us - timing.capture_us < |
323 kEncodingTimeMeasureWindowMs * rtc::kNumMicrosecsPerMillisec) | 410 kEncodingTimeMeasureWindowMs * rtc::kNumMicrosecsPerMillisec) { |
324 break; | 411 break; |
| 412 } |
325 if (timing.last_send_us != -1) { | 413 if (timing.last_send_us != -1) { |
326 int encode_duration_us = | 414 int encode_duration_us = |
327 static_cast<int>(timing.last_send_us - timing.capture_us); | 415 static_cast<int>(timing.last_send_us - timing.capture_us); |
328 if (encoder_timing_) { | 416 if (encoder_timing_) { |
329 // TODO(nisse): Update encoder_timing_ to also use us units. | 417 // TODO(nisse): Update encoder_timing_ to also use us units. |
330 encoder_timing_->OnEncodeTiming(timing.capture_time_us / | 418 encoder_timing_->OnEncodeTiming(timing.capture_time_us / |
331 rtc::kNumMicrosecsPerMillisec, | 419 rtc::kNumMicrosecsPerMillisec, |
332 encode_duration_us / | 420 encode_duration_us / |
333 rtc::kNumMicrosecsPerMillisec); | 421 rtc::kNumMicrosecsPerMillisec); |
334 } | 422 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; | 477 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
390 | 478 |
391 LOG(LS_VERBOSE) << " Frame stats: " | 479 LOG(LS_VERBOSE) << " Frame stats: " |
392 << " encode usage " << metrics_->encode_usage_percent | 480 << " encode usage " << metrics_->encode_usage_percent |
393 << " overuse detections " << num_overuse_detections_ | 481 << " overuse detections " << num_overuse_detections_ |
394 << " rampup delay " << rampup_delay; | 482 << " rampup delay " << rampup_delay; |
395 } | 483 } |
396 | 484 |
397 bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) { | 485 bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) { |
398 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); | 486 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
| 487 |
399 if (metrics.encode_usage_percent >= | 488 if (metrics.encode_usage_percent >= |
400 options_.high_encode_usage_threshold_percent) { | 489 options_.high_encode_usage_threshold_percent) { |
401 ++checks_above_threshold_; | 490 ++checks_above_threshold_; |
402 } else { | 491 } else { |
403 checks_above_threshold_ = 0; | 492 checks_above_threshold_ = 0; |
404 } | 493 } |
405 return checks_above_threshold_ >= options_.high_threshold_consecutive_count; | 494 return checks_above_threshold_ >= options_.high_threshold_consecutive_count; |
406 } | 495 } |
407 | 496 |
408 bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics, | 497 bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics, |
409 int64_t time_now) { | 498 int64_t time_now) { |
410 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); | 499 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
411 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; | 500 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
412 if (time_now < last_rampup_time_ms_ + delay) | 501 if (time_now < last_rampup_time_ms_ + delay) |
413 return false; | 502 return false; |
414 | 503 |
415 return metrics.encode_usage_percent < | 504 return metrics.encode_usage_percent < |
416 options_.low_encode_usage_threshold_percent; | 505 options_.low_encode_usage_threshold_percent; |
417 } | 506 } |
418 } // namespace webrtc | 507 } // namespace webrtc |
OLD | NEW |