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

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

Issue 2716643002: Add framerate to VideoSinkWants and ability to signal on overuse (Closed)
Patch Set: windows warning Created 3 years, 9 months 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
OLDNEW
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
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
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 rtc::Optional<std::pair<int, int>> overuse_periods;
kthelgason 2017/03/14 10:01:20 Why does this variable exist? Seems to me that the
sprang_webrtc 2017/03/14 14:15:03 This was a bit of a rush job. The complexity here
237 if (!toggling_interval.empty()) {
238 std::pair<int, int> vals;
239 if (sscanf(toggling_interval.c_str(), "%d-%d", &vals.first, &vals.second) ==
240 2) {
241 if (vals.first > 0 && vals.second > 0) {
242 overuse_periods.emplace(vals);
243 } else {
244 LOG(LS_WARNING) << "Invalid (non-positive) overuse / normal periods: "
245 << vals.first << " / " << vals.second;
246 }
247 } else {
248 LOG(LS_WARNING) << "Malformed toggling interval: " << toggling_interval;
249 }
250 }
251
252 if (overuse_periods) {
253 instance.reset(new OverdoseInjector(options, overuse_periods->first,
kthelgason 2017/03/14 10:01:20 This condidional is unnecessary. `instance.reset`
sprang_webrtc 2017/03/14 14:15:03 Acknowledged.
254 overuse_periods->second));
255 } else {
256 instance.reset(new SendProcessingUsage(options));
257 }
258
259 return instance;
260 }
261
174 class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask { 262 class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask {
175 public: 263 public:
176 explicit CheckOveruseTask(OveruseFrameDetector* overuse_detector) 264 explicit CheckOveruseTask(OveruseFrameDetector* overuse_detector)
177 : overuse_detector_(overuse_detector) { 265 : overuse_detector_(overuse_detector) {
178 rtc::TaskQueue::Current()->PostDelayedTask( 266 rtc::TaskQueue::Current()->PostDelayedTask(
179 std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs); 267 std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs);
180 } 268 }
181 269
182 void Stop() { 270 void Stop() {
183 RTC_CHECK(task_checker_.CalledSequentially()); 271 RTC_CHECK(task_checker_.CalledSequentially());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 // TODO(nisse): Use rtc::Optional 303 // TODO(nisse): Use rtc::Optional
216 last_capture_time_us_(-1), 304 last_capture_time_us_(-1),
217 last_processed_capture_time_us_(-1), 305 last_processed_capture_time_us_(-1),
218 num_pixels_(0), 306 num_pixels_(0),
219 last_overuse_time_ms_(-1), 307 last_overuse_time_ms_(-1),
220 checks_above_threshold_(0), 308 checks_above_threshold_(0),
221 num_overuse_detections_(0), 309 num_overuse_detections_(0),
222 last_rampup_time_ms_(-1), 310 last_rampup_time_ms_(-1),
223 in_quick_rampup_(false), 311 in_quick_rampup_(false),
224 current_rampup_delay_ms_(kStandardRampUpDelayMs), 312 current_rampup_delay_ms_(kStandardRampUpDelayMs),
225 usage_(new SendProcessingUsage(options)) { 313 usage_(CreateSendProcessingUsage(options)) {
226 task_checker_.Detach(); 314 task_checker_.Detach();
227 } 315 }
228 316
229 OveruseFrameDetector::~OveruseFrameDetector() { 317 OveruseFrameDetector::~OveruseFrameDetector() {
230 RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called."; 318 RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called.";
231 } 319 }
232 320
233 void OveruseFrameDetector::StartCheckForOveruse() { 321 void OveruseFrameDetector::StartCheckForOveruse() {
234 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); 322 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
235 RTC_DCHECK(!check_overuse_task_); 323 RTC_DCHECK(!check_overuse_task_);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 } 401 }
314 // TODO(pbos): Handle the case/log errors when not finding the corresponding 402 // TODO(pbos): Handle the case/log errors when not finding the corresponding
315 // frame (either very slow encoding or incorrect wrong timestamps returned 403 // frame (either very slow encoding or incorrect wrong timestamps returned
316 // from the encoder). 404 // from the encoder).
317 // This is currently the case for all frames on ChromeOS, so logging them 405 // This is currently the case for all frames on ChromeOS, so logging them
318 // would be spammy, and triggering overuse would be wrong. 406 // would be spammy, and triggering overuse would be wrong.
319 // https://crbug.com/350106 407 // https://crbug.com/350106
320 while (!frame_timing_.empty()) { 408 while (!frame_timing_.empty()) {
321 FrameTiming timing = frame_timing_.front(); 409 FrameTiming timing = frame_timing_.front();
322 if (time_sent_in_us - timing.capture_us < 410 if (time_sent_in_us - timing.capture_us <
323 kEncodingTimeMeasureWindowMs * rtc::kNumMicrosecsPerMillisec) 411 kEncodingTimeMeasureWindowMs * rtc::kNumMicrosecsPerMillisec) {
324 break; 412 break;
413 }
325 if (timing.last_send_us != -1) { 414 if (timing.last_send_us != -1) {
326 int encode_duration_us = 415 int encode_duration_us =
327 static_cast<int>(timing.last_send_us - timing.capture_us); 416 static_cast<int>(timing.last_send_us - timing.capture_us);
328 if (encoder_timing_) { 417 if (encoder_timing_) {
329 // TODO(nisse): Update encoder_timing_ to also use us units. 418 // TODO(nisse): Update encoder_timing_ to also use us units.
330 encoder_timing_->OnEncodeTiming(timing.capture_time_us / 419 encoder_timing_->OnEncodeTiming(timing.capture_time_us /
331 rtc::kNumMicrosecsPerMillisec, 420 rtc::kNumMicrosecsPerMillisec,
332 encode_duration_us / 421 encode_duration_us /
333 rtc::kNumMicrosecsPerMillisec); 422 rtc::kNumMicrosecsPerMillisec);
334 } 423 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; 478 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
390 479
391 LOG(LS_VERBOSE) << " Frame stats: " 480 LOG(LS_VERBOSE) << " Frame stats: "
392 << " encode usage " << metrics_->encode_usage_percent 481 << " encode usage " << metrics_->encode_usage_percent
393 << " overuse detections " << num_overuse_detections_ 482 << " overuse detections " << num_overuse_detections_
394 << " rampup delay " << rampup_delay; 483 << " rampup delay " << rampup_delay;
395 } 484 }
396 485
397 bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) { 486 bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) {
398 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); 487 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
488
399 if (metrics.encode_usage_percent >= 489 if (metrics.encode_usage_percent >=
400 options_.high_encode_usage_threshold_percent) { 490 options_.high_encode_usage_threshold_percent) {
401 ++checks_above_threshold_; 491 ++checks_above_threshold_;
402 } else { 492 } else {
403 checks_above_threshold_ = 0; 493 checks_above_threshold_ = 0;
404 } 494 }
405 return checks_above_threshold_ >= options_.high_threshold_consecutive_count; 495 return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
406 } 496 }
407 497
408 bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics, 498 bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics,
409 int64_t time_now) { 499 int64_t time_now) {
410 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); 500 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
411 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; 501 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
412 if (time_now < last_rampup_time_ms_ + delay) 502 if (time_now < last_rampup_time_ms_ + delay)
413 return false; 503 return false;
414 504
415 return metrics.encode_usage_percent < 505 return metrics.encode_usage_percent <
416 options_.low_encode_usage_threshold_percent; 506 options_.low_encode_usage_threshold_percent;
417 } 507 }
418 } // namespace webrtc 508 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698