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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 // Delay between rampup attempts. Initially uses standard, scales up to max. | 42 // Delay between rampup attempts. Initially uses standard, scales up to max. |
43 const int kStandardRampUpDelayMs = 40 * 1000; | 43 const int kStandardRampUpDelayMs = 40 * 1000; |
44 const int kMaxRampUpDelayMs = 240 * 1000; | 44 const int kMaxRampUpDelayMs = 240 * 1000; |
45 // Expontential back-off factor, to prevent annoying up-down behaviour. | 45 // Expontential back-off factor, to prevent annoying up-down behaviour. |
46 const double kRampUpBackoffFactor = 2.0; | 46 const double kRampUpBackoffFactor = 2.0; |
47 | 47 |
48 // Max number of overuses detected before always applying the rampup delay. | 48 // Max number of overuses detected before always applying the rampup delay. |
49 const int kMaxOverusesBeforeApplyRampupDelay = 4; | 49 const int kMaxOverusesBeforeApplyRampupDelay = 4; |
50 | 50 |
51 // The maximum exponent to use in VCMExpFilter. | 51 // The maximum exponent to use in VCMExpFilter. |
52 const float kSampleDiffMs = 33.0f; | |
53 const float kMaxExp = 7.0f; | 52 const float kMaxExp = 7.0f; |
53 // Default value used before first reconfiguration. | |
54 const int kDefaultFrameRate = 30; | |
55 // Default sample diff, default frame rate. | |
56 const float kDefaultSampleDiffMs = 1000.0f / kDefaultFrameRate; | |
57 // A factor applied to the sample diff on OnTargetFramerateUpdated to determine | |
58 // a max limit for the sample diff. For instance, with a framerate of 30fps, | |
59 // the sample diff is capped to (1000 / 30) * 1.35 = 45ms. This prevents | |
60 // triggering too soon if there are individual very large outliers. | |
61 const float kMaxSampleDiffMarginFactor = 1.35f; | |
62 // Minimum framerate allows for user calculation. This prevents crazy long | |
63 // encode times from being accepted if the frame rate happens to be low. | |
64 const int kMinFramerate = 7; | |
54 | 65 |
55 const auto kScaleReasonCpu = AdaptationObserverInterface::AdaptReason::kCpu; | 66 const auto kScaleReasonCpu = AdaptationObserverInterface::AdaptReason::kCpu; |
56 } // namespace | 67 } // namespace |
57 | 68 |
58 CpuOveruseOptions::CpuOveruseOptions() | 69 CpuOveruseOptions::CpuOveruseOptions() |
59 : high_encode_usage_threshold_percent(85), | 70 : high_encode_usage_threshold_percent(85), |
60 frame_timeout_interval_ms(1500), | 71 frame_timeout_interval_ms(1500), |
61 min_frame_samples(120), | 72 min_frame_samples(120), |
62 min_process_count(3), | 73 min_process_count(3), |
63 high_threshold_consecutive_count(2) { | 74 high_threshold_consecutive_count(2) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 | 117 |
107 // Class for calculating the processing usage on the send-side (the average | 118 // Class for calculating the processing usage on the send-side (the average |
108 // processing time of a frame divided by the average time difference between | 119 // processing time of a frame divided by the average time difference between |
109 // captured frames). | 120 // captured frames). |
110 class OveruseFrameDetector::SendProcessingUsage { | 121 class OveruseFrameDetector::SendProcessingUsage { |
111 public: | 122 public: |
112 explicit SendProcessingUsage(const CpuOveruseOptions& options) | 123 explicit SendProcessingUsage(const CpuOveruseOptions& options) |
113 : kWeightFactorFrameDiff(0.998f), | 124 : kWeightFactorFrameDiff(0.998f), |
114 kWeightFactorProcessing(0.995f), | 125 kWeightFactorProcessing(0.995f), |
115 kInitialSampleDiffMs(40.0f), | 126 kInitialSampleDiffMs(40.0f), |
116 kMaxSampleDiffMs(45.0f), | |
117 count_(0), | 127 count_(0), |
118 options_(options), | 128 options_(options), |
129 max_sample_diff_ms_(kDefaultSampleDiffMs * kMaxSampleDiffMarginFactor), | |
119 filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)), | 130 filtered_processing_ms_(new rtc::ExpFilter(kWeightFactorProcessing)), |
120 filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) { | 131 filtered_frame_diff_ms_(new rtc::ExpFilter(kWeightFactorFrameDiff)) { |
121 Reset(); | 132 Reset(); |
122 } | 133 } |
123 virtual ~SendProcessingUsage() {} | 134 virtual ~SendProcessingUsage() {} |
124 | 135 |
125 void Reset() { | 136 void Reset() { |
126 count_ = 0; | 137 count_ = 0; |
138 max_sample_diff_ms_ = kDefaultSampleDiffMs * kMaxSampleDiffMarginFactor; | |
127 filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff); | 139 filtered_frame_diff_ms_->Reset(kWeightFactorFrameDiff); |
128 filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs); | 140 filtered_frame_diff_ms_->Apply(1.0f, kInitialSampleDiffMs); |
129 filtered_processing_ms_->Reset(kWeightFactorProcessing); | 141 filtered_processing_ms_->Reset(kWeightFactorProcessing); |
130 filtered_processing_ms_->Apply(1.0f, InitialProcessingMs()); | 142 filtered_processing_ms_->Apply(1.0f, InitialProcessingMs()); |
131 } | 143 } |
132 | 144 |
145 void SetMaxSampleDiffMs(float diff_ms) { max_sample_diff_ms_ = diff_ms; } | |
146 | |
133 void AddCaptureSample(float sample_ms) { | 147 void AddCaptureSample(float sample_ms) { |
134 float exp = sample_ms / kSampleDiffMs; | 148 float exp = sample_ms / kDefaultSampleDiffMs; |
135 exp = std::min(exp, kMaxExp); | 149 exp = std::min(exp, kMaxExp); |
136 filtered_frame_diff_ms_->Apply(exp, sample_ms); | 150 filtered_frame_diff_ms_->Apply(exp, sample_ms); |
137 } | 151 } |
138 | 152 |
139 void AddSample(float processing_ms, int64_t diff_last_sample_ms) { | 153 void AddSample(float processing_ms, int64_t diff_last_sample_ms) { |
140 ++count_; | 154 ++count_; |
141 float exp = diff_last_sample_ms / kSampleDiffMs; | 155 float exp = diff_last_sample_ms / kDefaultSampleDiffMs; |
142 exp = std::min(exp, kMaxExp); | 156 exp = std::min(exp, kMaxExp); |
143 filtered_processing_ms_->Apply(exp, processing_ms); | 157 filtered_processing_ms_->Apply(exp, processing_ms); |
144 } | 158 } |
145 | 159 |
146 virtual int Value() { | 160 virtual int Value() { |
147 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { | 161 if (count_ < static_cast<uint32_t>(options_.min_frame_samples)) { |
148 return static_cast<int>(InitialUsageInPercent() + 0.5f); | 162 return static_cast<int>(InitialUsageInPercent() + 0.5f); |
149 } | 163 } |
150 float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f); | 164 float frame_diff_ms = std::max(filtered_frame_diff_ms_->filtered(), 1.0f); |
151 frame_diff_ms = std::min(frame_diff_ms, kMaxSampleDiffMs); | 165 frame_diff_ms = std::min(frame_diff_ms, max_sample_diff_ms_); |
152 float encode_usage_percent = | 166 float encode_usage_percent = |
153 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms; | 167 100.0f * filtered_processing_ms_->filtered() / frame_diff_ms; |
154 return static_cast<int>(encode_usage_percent + 0.5); | 168 return static_cast<int>(encode_usage_percent + 0.5); |
155 } | 169 } |
156 | 170 |
157 private: | 171 private: |
158 float InitialUsageInPercent() const { | 172 float InitialUsageInPercent() const { |
159 // Start in between the underuse and overuse threshold. | 173 // Start in between the underuse and overuse threshold. |
160 return (options_.low_encode_usage_threshold_percent + | 174 return (options_.low_encode_usage_threshold_percent + |
161 options_.high_encode_usage_threshold_percent) / 2.0f; | 175 options_.high_encode_usage_threshold_percent) / 2.0f; |
162 } | 176 } |
163 | 177 |
164 float InitialProcessingMs() const { | 178 float InitialProcessingMs() const { |
165 return InitialUsageInPercent() * kInitialSampleDiffMs / 100; | 179 return InitialUsageInPercent() * kInitialSampleDiffMs / 100; |
166 } | 180 } |
167 | 181 |
168 const float kWeightFactorFrameDiff; | 182 const float kWeightFactorFrameDiff; |
169 const float kWeightFactorProcessing; | 183 const float kWeightFactorProcessing; |
170 const float kInitialSampleDiffMs; | 184 const float kInitialSampleDiffMs; |
171 const float kMaxSampleDiffMs; | |
172 uint64_t count_; | 185 uint64_t count_; |
173 const CpuOveruseOptions options_; | 186 const CpuOveruseOptions options_; |
187 float max_sample_diff_ms_; | |
174 std::unique_ptr<rtc::ExpFilter> filtered_processing_ms_; | 188 std::unique_ptr<rtc::ExpFilter> filtered_processing_ms_; |
175 std::unique_ptr<rtc::ExpFilter> filtered_frame_diff_ms_; | 189 std::unique_ptr<rtc::ExpFilter> filtered_frame_diff_ms_; |
176 }; | 190 }; |
177 | 191 |
178 // Class used for manual testing of overuse, enabled via field trial flag. | 192 // Class used for manual testing of overuse, enabled via field trial flag. |
179 class OveruseFrameDetector::OverdoseInjector | 193 class OveruseFrameDetector::OverdoseInjector |
180 : public OveruseFrameDetector::SendProcessingUsage { | 194 : public OveruseFrameDetector::SendProcessingUsage { |
181 public: | 195 public: |
182 OverdoseInjector(const CpuOveruseOptions& options, | 196 OverdoseInjector(const CpuOveruseOptions& options, |
183 int64_t normal_period_ms, | 197 int64_t normal_period_ms, |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 : check_overuse_task_(nullptr), | 338 : check_overuse_task_(nullptr), |
325 options_(options), | 339 options_(options), |
326 observer_(observer), | 340 observer_(observer), |
327 encoder_timing_(encoder_timing), | 341 encoder_timing_(encoder_timing), |
328 metrics_observer_(metrics_observer), | 342 metrics_observer_(metrics_observer), |
329 num_process_times_(0), | 343 num_process_times_(0), |
330 // TODO(nisse): Use rtc::Optional | 344 // TODO(nisse): Use rtc::Optional |
331 last_capture_time_us_(-1), | 345 last_capture_time_us_(-1), |
332 last_processed_capture_time_us_(-1), | 346 last_processed_capture_time_us_(-1), |
333 num_pixels_(0), | 347 num_pixels_(0), |
348 framerate_(kDefaultFrameRate), | |
334 last_overuse_time_ms_(-1), | 349 last_overuse_time_ms_(-1), |
335 checks_above_threshold_(0), | 350 checks_above_threshold_(0), |
336 num_overuse_detections_(0), | 351 num_overuse_detections_(0), |
337 last_rampup_time_ms_(-1), | 352 last_rampup_time_ms_(-1), |
338 in_quick_rampup_(false), | 353 in_quick_rampup_(false), |
339 current_rampup_delay_ms_(kStandardRampUpDelayMs), | 354 current_rampup_delay_ms_(kStandardRampUpDelayMs), |
340 usage_(CreateSendProcessingUsage(options)) { | 355 usage_(CreateSendProcessingUsage(options)) { |
341 task_checker_.Detach(); | 356 task_checker_.Detach(); |
342 } | 357 } |
343 | 358 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
383 | 398 |
384 void OveruseFrameDetector::ResetAll(int num_pixels) { | 399 void OveruseFrameDetector::ResetAll(int num_pixels) { |
385 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); | 400 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
386 num_pixels_ = num_pixels; | 401 num_pixels_ = num_pixels; |
387 usage_->Reset(); | 402 usage_->Reset(); |
388 frame_timing_.clear(); | 403 frame_timing_.clear(); |
389 last_capture_time_us_ = -1; | 404 last_capture_time_us_ = -1; |
390 last_processed_capture_time_us_ = -1; | 405 last_processed_capture_time_us_ = -1; |
391 num_process_times_ = 0; | 406 num_process_times_ = 0; |
392 metrics_ = rtc::Optional<CpuOveruseMetrics>(); | 407 metrics_ = rtc::Optional<CpuOveruseMetrics>(); |
408 OnTargetFramerateUpdated(framerate_); | |
kthelgason
2017/06/12 11:56:47
is this supposed to be the last framerate and not
sprang_webrtc
2017/06/14 08:39:16
The state is reset if the resolution is changed, i
| |
409 } | |
410 | |
411 void OveruseFrameDetector::OnTargetFramerateUpdated(int framerate_fps) { | |
412 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); | |
413 RTC_DCHECK(framerate_fps > 0); | |
414 framerate_ = framerate_fps; | |
åsapersson
2017/06/13 12:15:09
Add a max limit for framerate_ to 30?
sprang_webrtc
2017/06/14 08:39:16
Do we really want to? I think we at least support
åsapersson
2017/06/14 10:50:26
What if max is 60fps and input 30fps. Wouldn't max
sprang_webrtc
2017/06/14 13:41:28
Yes, if the user configures a max framerate of 60f
| |
415 usage_->SetMaxSampleDiffMs((1000 / std::max(kMinFramerate, framerate_fps)) * | |
416 kMaxSampleDiffMarginFactor); | |
393 } | 417 } |
394 | 418 |
395 void OveruseFrameDetector::FrameCaptured(const VideoFrame& frame, | 419 void OveruseFrameDetector::FrameCaptured(const VideoFrame& frame, |
396 int64_t time_when_first_seen_us) { | 420 int64_t time_when_first_seen_us) { |
397 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); | 421 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
398 | 422 |
399 if (FrameSizeChanged(frame.width() * frame.height()) || | 423 if (FrameSizeChanged(frame.width() * frame.height()) || |
400 FrameTimeoutDetected(time_when_first_seen_us)) { | 424 FrameTimeoutDetected(time_when_first_seen_us)) { |
401 ResetAll(frame.width() * frame.height()); | 425 ResetAll(frame.width() * frame.height()); |
402 } | 426 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
526 int64_t time_now) { | 550 int64_t time_now) { |
527 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); | 551 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_); |
528 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; | 552 int delay = in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; |
529 if (time_now < last_rampup_time_ms_ + delay) | 553 if (time_now < last_rampup_time_ms_ + delay) |
530 return false; | 554 return false; |
531 | 555 |
532 return metrics.encode_usage_percent < | 556 return metrics.encode_usage_percent < |
533 options_.low_encode_usage_threshold_percent; | 557 options_.low_encode_usage_threshold_percent; |
534 } | 558 } |
535 } // namespace webrtc | 559 } // namespace webrtc |
OLD | NEW |