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

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

Issue 2789823002: Reland of Add framerate to VideoSinkWants and ability to signal on overuse (Closed)
Patch Set: Rebase fix Created 3 years, 8 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
« no previous file with comments | « webrtc/video/overuse_frame_detector.h ('k') | webrtc/video/send_statistics_proxy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 normal_period_ms,
183 int64_t overuse_period_ms,
184 int64_t underuse_period_ms)
185 : OveruseFrameDetector::SendProcessingUsage(options),
186 normal_period_ms_(normal_period_ms),
187 overuse_period_ms_(overuse_period_ms),
188 underuse_period_ms_(underuse_period_ms),
189 state_(State::kNormal),
190 last_toggling_ms_(-1) {
191 RTC_DCHECK_GT(overuse_period_ms, 0);
192 RTC_DCHECK_GT(normal_period_ms, 0);
193 LOG(LS_INFO) << "Simulating overuse with intervals " << normal_period_ms
194 << "ms normal mode, " << overuse_period_ms
195 << "ms overuse mode.";
196 }
197
198 ~OverdoseInjector() override {}
199
200 int Value() override {
201 int64_t now_ms = rtc::TimeMillis();
202 if (last_toggling_ms_ == -1) {
203 last_toggling_ms_ = now_ms;
204 } else {
205 switch (state_) {
206 case State::kNormal:
207 if (now_ms > last_toggling_ms_ + normal_period_ms_) {
208 state_ = State::kOveruse;
209 last_toggling_ms_ = now_ms;
210 LOG(LS_INFO) << "Simulating CPU overuse.";
211 }
212 break;
213 case State::kOveruse:
214 if (now_ms > last_toggling_ms_ + overuse_period_ms_) {
215 state_ = State::kUnderuse;
216 last_toggling_ms_ = now_ms;
217 LOG(LS_INFO) << "Simulating CPU underuse.";
218 }
219 break;
220 case State::kUnderuse:
221 if (now_ms > last_toggling_ms_ + underuse_period_ms_) {
222 state_ = State::kNormal;
223 last_toggling_ms_ = now_ms;
224 LOG(LS_INFO) << "Actual CPU overuse measurements in effect.";
225 }
226 break;
227 }
228 }
229
230 rtc::Optional<int> overried_usage_value;
231 switch (state_) {
232 case State::kNormal:
233 break;
234 case State::kOveruse:
235 overried_usage_value.emplace(250);
236 break;
237 case State::kUnderuse:
238 overried_usage_value.emplace(5);
239 break;
240 }
241
242 return overried_usage_value.value_or(SendProcessingUsage::Value());
243 }
244
245 private:
246 const int64_t normal_period_ms_;
247 const int64_t overuse_period_ms_;
248 const int64_t underuse_period_ms_;
249 enum class State { kNormal, kOveruse, kUnderuse } state_;
250 int64_t last_toggling_ms_;
251 };
252
253 std::unique_ptr<OveruseFrameDetector::SendProcessingUsage>
254 OveruseFrameDetector::CreateSendProcessingUsage(
255 const CpuOveruseOptions& options) {
256 std::unique_ptr<SendProcessingUsage> instance;
257 std::string toggling_interval =
258 field_trial::FindFullName("WebRTC-ForceSimulatedOveruseIntervalMs");
259 if (!toggling_interval.empty()) {
260 int normal_period_ms = 0;
261 int overuse_period_ms = 0;
262 int underuse_period_ms = 0;
263 if (sscanf(toggling_interval.c_str(), "%d-%d-%d", &normal_period_ms,
264 &overuse_period_ms, &underuse_period_ms) == 3) {
265 if (normal_period_ms > 0 && overuse_period_ms > 0 &&
266 underuse_period_ms > 0) {
267 instance.reset(new OverdoseInjector(
268 options, normal_period_ms, overuse_period_ms, underuse_period_ms));
269 } else {
270 LOG(LS_WARNING)
271 << "Invalid (non-positive) normal/overuse/underuse periods: "
272 << normal_period_ms << " / " << overuse_period_ms << " / "
273 << underuse_period_ms;
274 }
275 } else {
276 LOG(LS_WARNING) << "Malformed toggling interval: " << toggling_interval;
277 }
278 }
279
280 if (!instance) {
281 // No valid overuse simulation parameters set, use normal usage class.
282 instance.reset(new SendProcessingUsage(options));
283 }
284
285 return instance;
286 }
287
174 class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask { 288 class OveruseFrameDetector::CheckOveruseTask : public rtc::QueuedTask {
175 public: 289 public:
176 explicit CheckOveruseTask(OveruseFrameDetector* overuse_detector) 290 explicit CheckOveruseTask(OveruseFrameDetector* overuse_detector)
177 : overuse_detector_(overuse_detector) { 291 : overuse_detector_(overuse_detector) {
178 rtc::TaskQueue::Current()->PostDelayedTask( 292 rtc::TaskQueue::Current()->PostDelayedTask(
179 std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs); 293 std::unique_ptr<rtc::QueuedTask>(this), kTimeToFirstCheckForOveruseMs);
180 } 294 }
181 295
182 void Stop() { 296 void Stop() {
183 RTC_CHECK(task_checker_.CalledSequentially()); 297 RTC_CHECK(task_checker_.CalledSequentially());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 // TODO(nisse): Use rtc::Optional 329 // TODO(nisse): Use rtc::Optional
216 last_capture_time_us_(-1), 330 last_capture_time_us_(-1),
217 last_processed_capture_time_us_(-1), 331 last_processed_capture_time_us_(-1),
218 num_pixels_(0), 332 num_pixels_(0),
219 last_overuse_time_ms_(-1), 333 last_overuse_time_ms_(-1),
220 checks_above_threshold_(0), 334 checks_above_threshold_(0),
221 num_overuse_detections_(0), 335 num_overuse_detections_(0),
222 last_rampup_time_ms_(-1), 336 last_rampup_time_ms_(-1),
223 in_quick_rampup_(false), 337 in_quick_rampup_(false),
224 current_rampup_delay_ms_(kStandardRampUpDelayMs), 338 current_rampup_delay_ms_(kStandardRampUpDelayMs),
225 usage_(new SendProcessingUsage(options)) { 339 usage_(CreateSendProcessingUsage(options)) {
226 task_checker_.Detach(); 340 task_checker_.Detach();
227 } 341 }
228 342
229 OveruseFrameDetector::~OveruseFrameDetector() { 343 OveruseFrameDetector::~OveruseFrameDetector() {
230 RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called."; 344 RTC_DCHECK(!check_overuse_task_) << "StopCheckForOverUse must be called.";
231 } 345 }
232 346
233 void OveruseFrameDetector::StartCheckForOveruse() { 347 void OveruseFrameDetector::StartCheckForOveruse() {
234 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); 348 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
235 RTC_DCHECK(!check_overuse_task_); 349 RTC_DCHECK(!check_overuse_task_);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 } 427 }
314 // TODO(pbos): Handle the case/log errors when not finding the corresponding 428 // TODO(pbos): Handle the case/log errors when not finding the corresponding
315 // frame (either very slow encoding or incorrect wrong timestamps returned 429 // frame (either very slow encoding or incorrect wrong timestamps returned
316 // from the encoder). 430 // from the encoder).
317 // This is currently the case for all frames on ChromeOS, so logging them 431 // This is currently the case for all frames on ChromeOS, so logging them
318 // would be spammy, and triggering overuse would be wrong. 432 // would be spammy, and triggering overuse would be wrong.
319 // https://crbug.com/350106 433 // https://crbug.com/350106
320 while (!frame_timing_.empty()) { 434 while (!frame_timing_.empty()) {
321 FrameTiming timing = frame_timing_.front(); 435 FrameTiming timing = frame_timing_.front();
322 if (time_sent_in_us - timing.capture_us < 436 if (time_sent_in_us - timing.capture_us <
323 kEncodingTimeMeasureWindowMs * rtc::kNumMicrosecsPerMillisec) 437 kEncodingTimeMeasureWindowMs * rtc::kNumMicrosecsPerMillisec) {
324 break; 438 break;
439 }
325 if (timing.last_send_us != -1) { 440 if (timing.last_send_us != -1) {
326 int encode_duration_us = 441 int encode_duration_us =
327 static_cast<int>(timing.last_send_us - timing.capture_us); 442 static_cast<int>(timing.last_send_us - timing.capture_us);
328 if (encoder_timing_) { 443 if (encoder_timing_) {
329 // TODO(nisse): Update encoder_timing_ to also use us units. 444 // TODO(nisse): Update encoder_timing_ to also use us units.
330 encoder_timing_->OnEncodeTiming(timing.capture_time_us / 445 encoder_timing_->OnEncodeTiming(timing.capture_time_us /
331 rtc::kNumMicrosecsPerMillisec, 446 rtc::kNumMicrosecsPerMillisec,
332 encode_duration_us / 447 encode_duration_us /
333 rtc::kNumMicrosecsPerMillisec); 448 rtc::kNumMicrosecsPerMillisec);
334 } 449 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; 504 in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
390 505
391 LOG(LS_VERBOSE) << " Frame stats: " 506 LOG(LS_VERBOSE) << " Frame stats: "
392 << " encode usage " << metrics_->encode_usage_percent 507 << " encode usage " << metrics_->encode_usage_percent
393 << " overuse detections " << num_overuse_detections_ 508 << " overuse detections " << num_overuse_detections_
394 << " rampup delay " << rampup_delay; 509 << " rampup delay " << rampup_delay;
395 } 510 }
396 511
397 bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) { 512 bool OveruseFrameDetector::IsOverusing(const CpuOveruseMetrics& metrics) {
398 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); 513 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
514
399 if (metrics.encode_usage_percent >= 515 if (metrics.encode_usage_percent >=
400 options_.high_encode_usage_threshold_percent) { 516 options_.high_encode_usage_threshold_percent) {
401 ++checks_above_threshold_; 517 ++checks_above_threshold_;
402 } else { 518 } else {
403 checks_above_threshold_ = 0; 519 checks_above_threshold_ = 0;
404 } 520 }
405 return checks_above_threshold_ >= options_.high_threshold_consecutive_count; 521 return checks_above_threshold_ >= options_.high_threshold_consecutive_count;
406 } 522 }
407 523
408 bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics, 524 bool OveruseFrameDetector::IsUnderusing(const CpuOveruseMetrics& metrics,
409 int64_t time_now) { 525 int64_t time_now) {
410 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); 526 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
411 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; 527 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_;
412 if (time_now < last_rampup_time_ms_ + delay) 528 if (time_now < last_rampup_time_ms_ + delay)
413 return false; 529 return false;
414 530
415 return metrics.encode_usage_percent < 531 return metrics.encode_usage_percent <
416 options_.low_encode_usage_threshold_percent; 532 options_.low_encode_usage_threshold_percent;
417 } 533 }
418 } // namespace webrtc 534 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/video/overuse_frame_detector.h ('k') | webrtc/video/send_statistics_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698