OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 23 matching lines...) Expand all Loading... |
34 using DegradationPreference = VideoSendStream::DegradationPreference; | 34 using DegradationPreference = VideoSendStream::DegradationPreference; |
35 | 35 |
36 // Time interval for logging frame counts. | 36 // Time interval for logging frame counts. |
37 const int64_t kFrameLogIntervalMs = 60000; | 37 const int64_t kFrameLogIntervalMs = 60000; |
38 | 38 |
39 // We will never ask for a resolution lower than this. | 39 // We will never ask for a resolution lower than this. |
40 // TODO(kthelgason): Lower this limit when better testing | 40 // TODO(kthelgason): Lower this limit when better testing |
41 // on MediaCodec and fallback implementations are in place. | 41 // on MediaCodec and fallback implementations are in place. |
42 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 | 42 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 |
43 const int kMinPixelsPerFrame = 320 * 180; | 43 const int kMinPixelsPerFrame = 320 * 180; |
| 44 const int kMinFramerateFps = 2; |
44 | 45 |
45 // The maximum number of frames to drop at beginning of stream | 46 // The maximum number of frames to drop at beginning of stream |
46 // to try and achieve desired bitrate. | 47 // to try and achieve desired bitrate. |
47 const int kMaxInitialFramedrop = 4; | 48 const int kMaxInitialFramedrop = 4; |
48 | 49 |
49 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | 50 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle |
50 // pipelining encoders better (multiple input frames before something comes | 51 // pipelining encoders better (multiple input frames before something comes |
51 // out). This should effectively turn off CPU adaptations for systems that | 52 // out). This should effectively turn off CPU adaptations for systems that |
52 // remotely cope with the load right now. | 53 // remotely cope with the load right now. |
53 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { | 54 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 source_(nullptr) {} | 154 source_(nullptr) {} |
154 | 155 |
155 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, | 156 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
156 const DegradationPreference& degradation_preference) { | 157 const DegradationPreference& degradation_preference) { |
157 // Called on libjingle's worker thread. | 158 // Called on libjingle's worker thread. |
158 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | 159 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); |
159 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | 160 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; |
160 rtc::VideoSinkWants wants; | 161 rtc::VideoSinkWants wants; |
161 { | 162 { |
162 rtc::CritScope lock(&crit_); | 163 rtc::CritScope lock(&crit_); |
| 164 wants = sink_wants_; |
| 165 // If changing degradation preference, clear any constraints from the |
| 166 // current sink wants that will no longer apply. |
| 167 if (degradation_preference_ != degradation_preference) { |
| 168 switch (degradation_preference) { |
| 169 case DegradationPreference::kBalanced: |
| 170 wants.max_framerate_fps_.reset(); |
| 171 break; |
| 172 case DegradationPreference::kMaintainResolution: |
| 173 wants.max_pixel_count.reset(); |
| 174 wants.target_pixel_count.reset(); |
| 175 break; |
| 176 } |
| 177 } |
| 178 degradation_preference_ = degradation_preference; |
163 old_source = source_; | 179 old_source = source_; |
164 source_ = source; | 180 source_ = source; |
165 degradation_preference_ = degradation_preference; | |
166 wants = current_wants(); | |
167 } | 181 } |
168 | 182 |
169 if (old_source != source && old_source != nullptr) { | 183 if (old_source != source && old_source != nullptr) { |
170 old_source->RemoveSink(vie_encoder_); | 184 old_source->RemoveSink(vie_encoder_); |
171 } | 185 } |
172 | 186 |
173 if (!source) { | 187 if (!source) { |
174 return; | 188 return; |
175 } | 189 } |
176 | 190 |
177 source->AddOrUpdateSink(vie_encoder_, wants); | 191 source->AddOrUpdateSink(vie_encoder_, wants); |
178 } | 192 } |
179 | 193 |
180 void SetWantsRotationApplied(bool rotation_applied) { | 194 void SetWantsRotationApplied(bool rotation_applied) { |
181 rtc::CritScope lock(&crit_); | 195 rtc::CritScope lock(&crit_); |
182 sink_wants_.rotation_applied = rotation_applied; | 196 sink_wants_.rotation_applied = rotation_applied; |
183 disabled_scaling_sink_wants_.rotation_applied = rotation_applied; | 197 if (source_) |
184 if (source_) { | 198 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
185 source_->AddOrUpdateSink(vie_encoder_, current_wants()); | |
186 } | |
187 } | 199 } |
188 | 200 |
189 void RequestResolutionLowerThan(int pixel_count) { | 201 void RequestResolutionLowerThan(int pixel_count) { |
190 // Called on the encoder task queue. | 202 // Called on the encoder task queue. |
191 rtc::CritScope lock(&crit_); | 203 rtc::CritScope lock(&crit_); |
192 if (!IsResolutionScalingEnabledLocked()) { | 204 if (!IsResolutionScalingEnabledLocked()) { |
193 // This can happen since |degradation_preference_| is set on | 205 // This can happen since |degradation_preference_| is set on |
194 // libjingle's worker thread but the adaptation is done on the encoder | 206 // libjingle's worker thread but the adaptation is done on the encoder |
195 // task queue. | 207 // task queue. |
196 return; | 208 return; |
197 } | 209 } |
198 // The input video frame size will have a resolution with less than or | 210 // The input video frame size will have a resolution with less than or |
199 // equal to |max_pixel_count| depending on how the source can scale the | 211 // equal to |max_pixel_count| depending on how the source can scale the |
200 // input frame size. | 212 // input frame size. |
201 const int pixels_wanted = (pixel_count * 3) / 5; | 213 const int pixels_wanted = (pixel_count * 3) / 5; |
202 if (pixels_wanted < kMinPixelsPerFrame) | 214 if (pixels_wanted < kMinPixelsPerFrame) |
203 return; | 215 return; |
204 sink_wants_.max_pixel_count = rtc::Optional<int>(pixels_wanted); | 216 sink_wants_.max_pixel_count = rtc::Optional<int>(pixels_wanted); |
205 sink_wants_.target_pixel_count = rtc::Optional<int>(); | 217 sink_wants_.target_pixel_count = rtc::Optional<int>(); |
206 if (source_) | 218 if (source_) |
207 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 219 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
208 } | 220 } |
209 | 221 |
| 222 void RequestFramerateLowerThan(int framerate_fps) { |
| 223 // Called on the encoder task queue. |
| 224 rtc::CritScope lock(&crit_); |
| 225 if (!IsFramerateScalingEnabledLocked()) { |
| 226 // This can happen since |degradation_preference_| is set on |
| 227 // libjingle's worker thread but the adaptation is done on the encoder |
| 228 // task queue. |
| 229 return; |
| 230 } |
| 231 // The input video frame rate will be scaled down to 2/3 of input fps, |
| 232 // rounding down. |
| 233 const int framerate_wanted = |
| 234 std::max(kMinFramerateFps, (framerate_fps * 2) / 3); |
| 235 sink_wants_.max_framerate_fps_.emplace(framerate_wanted); |
| 236 if (source_) |
| 237 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
| 238 } |
| 239 |
210 void RequestHigherResolutionThan(int pixel_count) { | 240 void RequestHigherResolutionThan(int pixel_count) { |
211 rtc::CritScope lock(&crit_); | 241 rtc::CritScope lock(&crit_); |
212 if (!IsResolutionScalingEnabledLocked()) { | 242 if (!IsResolutionScalingEnabledLocked()) { |
213 // This can happen since |degradation_preference_| is set on | 243 // This can happen since |degradation_preference_| is set on |
214 // libjingle's worker thread but the adaptation is done on the encoder | 244 // libjingle's worker thread but the adaptation is done on the encoder |
215 // task queue. | 245 // task queue. |
216 return; | 246 return; |
217 } | 247 } |
218 // On step down we request at most 3/5 the pixel count of the previous | 248 |
219 // resolution, so in order to take "one step up" we request a resolution as | 249 if (pixel_count == std::numeric_limits<int>::max()) { |
220 // close as possible to 5/3 of the current resolution. The actual pixel | 250 // Remove any restrains. |
221 // count selected depends on the capabilities of the source. In order to not | 251 sink_wants_.target_pixel_count.reset(); |
222 // take a too large step up, we cap the requested pixel count to be at most | 252 sink_wants_.max_pixel_count.reset(); |
223 // four time the current number of pixels. | 253 } else { |
224 sink_wants_.target_pixel_count = rtc::Optional<int>((pixel_count * 5) / 3); | 254 // On step down we request at most 3/5 the pixel count of the previous |
225 sink_wants_.max_pixel_count = rtc::Optional<int>(pixel_count * 4); | 255 // resolution, so in order to take "one step up" we request a resolution |
| 256 // as close as possible to 5/3 of the current resolution. The actual pixel |
| 257 // count selected depends on the capabilities of the source. In order to |
| 258 // not take a too large step up, we cap the requested pixel count to be at |
| 259 // most four time the current number of pixels. |
| 260 sink_wants_.target_pixel_count = |
| 261 rtc::Optional<int>((pixel_count * 5) / 3); |
| 262 sink_wants_.max_pixel_count = rtc::Optional<int>(pixel_count * 4); |
| 263 } |
226 if (source_) | 264 if (source_) |
227 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 265 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
228 } | 266 } |
| 267 |
| 268 void RequestHigherFramerateThan(int framerate_fps) { |
| 269 // Called on the encoder task queue. |
| 270 rtc::CritScope lock(&crit_); |
| 271 if (!IsFramerateScalingEnabledLocked()) { |
| 272 // This can happen since |degradation_preference_| is set on |
| 273 // libjingle's worker thread but the adaptation is done on the encoder |
| 274 // task queue. |
| 275 return; |
| 276 } |
| 277 if (framerate_fps == std::numeric_limits<int>::max()) { |
| 278 // Remove any restrains. |
| 279 sink_wants_.max_framerate_fps_.reset(); |
| 280 } else { |
| 281 // The input video frame rate will be scaled up to the last step, with |
| 282 // rounding. |
| 283 const int framerate_wanted = (framerate_fps * 3) / 2; |
| 284 sink_wants_.max_framerate_fps_.emplace(framerate_wanted); |
| 285 } |
| 286 if (source_) |
| 287 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
| 288 } |
229 | 289 |
230 private: | 290 private: |
231 bool IsResolutionScalingEnabledLocked() const | 291 bool IsResolutionScalingEnabledLocked() const |
232 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 292 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
233 return degradation_preference_ != | 293 return degradation_preference_ != |
234 DegradationPreference::kMaintainResolution; | 294 DegradationPreference::kMaintainResolution; |
235 } | 295 } |
236 | 296 |
237 const rtc::VideoSinkWants& current_wants() const | 297 bool IsFramerateScalingEnabledLocked() const |
238 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 298 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
239 return IsResolutionScalingEnabledLocked() ? sink_wants_ | 299 return degradation_preference_ == |
240 : disabled_scaling_sink_wants_; | 300 DegradationPreference::kMaintainResolution; |
241 } | 301 } |
242 | 302 |
243 rtc::CriticalSection crit_; | 303 rtc::CriticalSection crit_; |
244 rtc::SequencedTaskChecker main_checker_; | 304 rtc::SequencedTaskChecker main_checker_; |
245 ViEEncoder* const vie_encoder_; | 305 ViEEncoder* const vie_encoder_; |
246 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); | 306 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); |
247 rtc::VideoSinkWants disabled_scaling_sink_wants_ GUARDED_BY(&crit_); | |
248 DegradationPreference degradation_preference_ GUARDED_BY(&crit_); | 307 DegradationPreference degradation_preference_ GUARDED_BY(&crit_); |
249 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 308 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); |
250 | 309 |
251 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 310 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
252 }; | 311 }; |
253 | 312 |
254 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 313 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
255 SendStatisticsProxy* stats_proxy, | 314 SendStatisticsProxy* stats_proxy, |
256 const VideoSendStream::Config::EncoderSettings& settings, | 315 const VideoSendStream::Config::EncoderSettings& settings, |
257 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 316 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
(...skipping 18 matching lines...) Expand all Loading... |
276 encoder_start_bitrate_bps_(0), | 335 encoder_start_bitrate_bps_(0), |
277 max_data_payload_length_(0), | 336 max_data_payload_length_(0), |
278 nack_enabled_(false), | 337 nack_enabled_(false), |
279 last_observed_bitrate_bps_(0), | 338 last_observed_bitrate_bps_(0), |
280 encoder_paused_and_dropped_frame_(false), | 339 encoder_paused_and_dropped_frame_(false), |
281 has_received_sli_(false), | 340 has_received_sli_(false), |
282 picture_id_sli_(0), | 341 picture_id_sli_(0), |
283 has_received_rpsi_(false), | 342 has_received_rpsi_(false), |
284 picture_id_rpsi_(0), | 343 picture_id_rpsi_(0), |
285 clock_(Clock::GetRealTimeClock()), | 344 clock_(Clock::GetRealTimeClock()), |
286 scale_counter_(kScaleReasonSize, 0), | 345 scale_counters_{ |
| 346 {VideoSendStream::DegradationPreference::kMaintainResolution, |
| 347 std::vector<int>(kScaleReasonSize, 0)}, |
| 348 {VideoSendStream::DegradationPreference::kBalanced, |
| 349 std::vector<int>(kScaleReasonSize, 0)}}, |
287 degradation_preference_(DegradationPreference::kMaintainResolution), | 350 degradation_preference_(DegradationPreference::kMaintainResolution), |
288 last_captured_timestamp_(0), | 351 last_captured_timestamp_(0), |
289 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 352 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
290 clock_->TimeInMilliseconds()), | 353 clock_->TimeInMilliseconds()), |
291 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 354 last_frame_log_ms_(clock_->TimeInMilliseconds()), |
292 captured_frame_count_(0), | 355 captured_frame_count_(0), |
293 dropped_frame_count_(0), | 356 dropped_frame_count_(0), |
294 bitrate_observer_(nullptr), | 357 bitrate_observer_(nullptr), |
295 encoder_queue_("EncoderQueue") { | 358 encoder_queue_("EncoderQueue") { |
296 RTC_DCHECK(stats_proxy); | 359 RTC_DCHECK(stats_proxy); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 if (scaling_settings.thresholds) { | 530 if (scaling_settings.thresholds) { |
468 quality_scaler_.reset( | 531 quality_scaler_.reset( |
469 new QualityScaler(this, *(scaling_settings.thresholds))); | 532 new QualityScaler(this, *(scaling_settings.thresholds))); |
470 } else { | 533 } else { |
471 quality_scaler_.reset(new QualityScaler(this, codec_type_)); | 534 quality_scaler_.reset(new QualityScaler(this, codec_type_)); |
472 } | 535 } |
473 } else { | 536 } else { |
474 quality_scaler_.reset(nullptr); | 537 quality_scaler_.reset(nullptr); |
475 initial_rampup_ = kMaxInitialFramedrop; | 538 initial_rampup_ = kMaxInitialFramedrop; |
476 } | 539 } |
| 540 const std::vector<int>& scale_counter = *GetScaleCounters(); |
477 stats_proxy_->SetResolutionRestrictionStats( | 541 stats_proxy_->SetResolutionRestrictionStats( |
478 degradation_preference_allows_scaling, scale_counter_[kCpu] > 0, | 542 degradation_preference_allows_scaling, scale_counter[kCpu] > 0, |
479 scale_counter_[kQuality]); | 543 scale_counter[kQuality]); |
480 } | 544 } |
481 | 545 |
482 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 546 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
483 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 547 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
484 VideoFrame incoming_frame = video_frame; | 548 VideoFrame incoming_frame = video_frame; |
485 | 549 |
486 // Local time in webrtc time base. | 550 // Local time in webrtc time base. |
487 int64_t current_time_us = clock_->TimeInMicroseconds(); | 551 int64_t current_time_us = clock_->TimeInMicroseconds(); |
488 int64_t current_time_ms = current_time_us / rtc::kNumMicrosecsPerMillisec; | 552 int64_t current_time_ms = current_time_us / rtc::kNumMicrosecsPerMillisec; |
489 // TODO(nisse): This always overrides the incoming timestamp. Don't | 553 // TODO(nisse): This always overrides the incoming timestamp. Don't |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 | 796 |
733 if (video_suspension_changed) { | 797 if (video_suspension_changed) { |
734 LOG(LS_INFO) << "Video suspend state changed to: " | 798 LOG(LS_INFO) << "Video suspend state changed to: " |
735 << (video_is_suspended ? "suspended" : "not suspended"); | 799 << (video_is_suspended ? "suspended" : "not suspended"); |
736 stats_proxy_->OnSuspendChange(video_is_suspended); | 800 stats_proxy_->OnSuspendChange(video_is_suspended); |
737 } | 801 } |
738 } | 802 } |
739 | 803 |
740 void ViEEncoder::AdaptDown(AdaptReason reason) { | 804 void ViEEncoder::AdaptDown(AdaptReason reason) { |
741 RTC_DCHECK_RUN_ON(&encoder_queue_); | 805 RTC_DCHECK_RUN_ON(&encoder_queue_); |
742 if (degradation_preference_ != DegradationPreference::kBalanced) | 806 AdaptationRequest adaptation_request = { |
743 return; | 807 last_frame_info_->pixel_count(), |
744 RTC_DCHECK(static_cast<bool>(last_frame_info_)); | 808 stats_proxy_->GetStats().input_frame_rate, |
745 int current_pixel_count = last_frame_info_->pixel_count(); | 809 AdaptationRequest::Mode::kAdaptDown}; |
746 if (last_adaptation_request_ && | 810 |
747 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown && | 811 if (degradation_preference_ == DegradationPreference::kMaintainResolution && |
748 current_pixel_count >= last_adaptation_request_->input_pixel_count_) { | 812 adaptation_request.framerate_fps_ <= 0) { |
749 // Don't request lower resolution if the current resolution is not lower | 813 // No input fps estimate available, can't determine how to scale down fps. |
750 // than the last time we asked for the resolution to be lowered. | |
751 return; | 814 return; |
752 } | 815 } |
753 last_adaptation_request_.emplace(AdaptationRequest{ | 816 |
754 current_pixel_count, AdaptationRequest::Mode::kAdaptDown}); | 817 bool downgrade_requested = |
| 818 last_adaptation_request_ && |
| 819 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; |
| 820 int max_downgrades; |
| 821 switch (degradation_preference_) { |
| 822 case DegradationPreference::kBalanced: |
| 823 max_downgrades = kMaxCpuResolutionDowngrades; |
| 824 if (downgrade_requested && |
| 825 adaptation_request.input_pixel_count_ >= |
| 826 last_adaptation_request_->input_pixel_count_) { |
| 827 // Don't request lower resolution if the current resolution is not |
| 828 // lower than the last time we asked for the resolution to be lowered. |
| 829 return; |
| 830 } |
| 831 break; |
| 832 case DegradationPreference::kMaintainResolution: |
| 833 max_downgrades = kMaxCpuFramerateDowngrades; |
| 834 if (downgrade_requested && |
| 835 adaptation_request.framerate_fps_ < kMinFramerateFps) { |
| 836 // Don't request lower framerate if we don't have a valid frame rate. |
| 837 // Since framerate, unlike resolution, is a measure we have to |
| 838 // estimate, and can fluctuate naturally over time, don't make the |
| 839 // same kind of limitations as for resolution, but trust the overuse |
| 840 // detector to not trigger too often. |
| 841 return; |
| 842 } |
| 843 break; |
| 844 } |
| 845 |
| 846 last_adaptation_request_.emplace(adaptation_request); |
| 847 std::vector<int>* scale_counter = GetScaleCounters(); |
755 | 848 |
756 switch (reason) { | 849 switch (reason) { |
757 case kQuality: | 850 case kQuality: |
758 stats_proxy_->OnQualityRestrictedResolutionChanged( | 851 stats_proxy_->OnQualityRestrictedResolutionChanged( |
759 scale_counter_[reason] + 1); | 852 (*scale_counter)[reason] + 1); |
760 break; | 853 break; |
761 case kCpu: | 854 case kCpu: |
762 if (scale_counter_[reason] >= kMaxCpuDowngrades) | 855 if ((*scale_counter)[reason] >= max_downgrades) |
763 return; | 856 return; |
764 // Update stats accordingly. | 857 // Update stats accordingly. |
765 stats_proxy_->OnCpuRestrictedResolutionChanged(true); | 858 stats_proxy_->OnCpuRestrictedResolutionChanged(true); |
766 break; | 859 break; |
767 } | 860 } |
768 ++scale_counter_[reason]; | 861 ++(*scale_counter)[reason]; |
769 source_proxy_->RequestResolutionLowerThan(current_pixel_count); | 862 |
770 LOG(LS_INFO) << "Scaling down resolution."; | 863 switch (degradation_preference_) { |
| 864 case DegradationPreference::kBalanced: |
| 865 source_proxy_->RequestResolutionLowerThan( |
| 866 adaptation_request.input_pixel_count_); |
| 867 LOG(LS_INFO) << "Scaling down resolution."; |
| 868 break; |
| 869 case DegradationPreference::kMaintainResolution: |
| 870 source_proxy_->RequestFramerateLowerThan( |
| 871 adaptation_request.framerate_fps_); |
| 872 LOG(LS_INFO) << "Scaling down framerate."; |
| 873 break; |
| 874 } |
| 875 |
771 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 876 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
772 LOG(LS_INFO) << "Scaled " << scale_counter_[i] | 877 LOG(LS_INFO) << "Scaled " << (*scale_counter)[i] |
773 << " times for reason: " << (i ? "cpu" : "quality"); | 878 << " times for reason: " << (i ? "cpu" : "quality"); |
774 } | 879 } |
775 } | 880 } |
776 | 881 |
777 void ViEEncoder::AdaptUp(AdaptReason reason) { | 882 void ViEEncoder::AdaptUp(AdaptReason reason) { |
778 RTC_DCHECK_RUN_ON(&encoder_queue_); | 883 RTC_DCHECK_RUN_ON(&encoder_queue_); |
779 if (scale_counter_[reason] == 0 || | 884 int* scale_counter = &((*GetScaleCounters())[reason]); |
780 degradation_preference_ != DegradationPreference::kBalanced) { | 885 if (*scale_counter == 0) |
781 return; | 886 return; |
| 887 RTC_DCHECK_GT(*scale_counter, 0); |
| 888 AdaptationRequest adaptation_request = { |
| 889 last_frame_info_->pixel_count(), |
| 890 stats_proxy_->GetStats().input_frame_rate, |
| 891 AdaptationRequest::Mode::kAdaptUp}; |
| 892 |
| 893 bool adapt_up_requested = |
| 894 last_adaptation_request_ && |
| 895 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; |
| 896 switch (degradation_preference_) { |
| 897 case DegradationPreference::kBalanced: |
| 898 if (adapt_up_requested && |
| 899 adaptation_request.input_pixel_count_ <= |
| 900 last_adaptation_request_->input_pixel_count_) { |
| 901 // Don't request higher resolution if the current resolution is not |
| 902 // higher than the last time we asked for the resolution to be higher. |
| 903 return; |
| 904 } |
| 905 break; |
| 906 case DegradationPreference::kMaintainResolution: |
| 907 // TODO(sprang): Don't request higher framerate if we are already at |
| 908 // max requested fps? |
| 909 break; |
782 } | 910 } |
783 // Only scale if resolution is higher than last time we requested higher | |
784 // resolution. | |
785 RTC_DCHECK(static_cast<bool>(last_frame_info_)); | |
786 int current_pixel_count = last_frame_info_->pixel_count(); | |
787 if (last_adaptation_request_ && | |
788 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp && | |
789 current_pixel_count <= last_adaptation_request_->input_pixel_count_) { | |
790 // Don't request higher resolution if the current resolution is not higher | |
791 // than the last time we asked for the resolution to be higher. | |
792 return; | |
793 } | |
794 last_adaptation_request_.emplace(AdaptationRequest{ | |
795 current_pixel_count, AdaptationRequest::Mode::kAdaptUp}); | |
796 | 911 |
797 switch (reason) { | 912 switch (reason) { |
798 case kQuality: | 913 case kQuality: |
799 stats_proxy_->OnQualityRestrictedResolutionChanged( | 914 stats_proxy_->OnQualityRestrictedResolutionChanged(*scale_counter - 1); |
800 scale_counter_[reason] - 1); | |
801 break; | 915 break; |
802 case kCpu: | 916 case kCpu: |
803 // Update stats accordingly. | 917 // Update stats accordingly. |
804 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counter_[reason] > | 918 stats_proxy_->OnCpuRestrictedResolutionChanged(*scale_counter > 1); |
805 1); | |
806 break; | 919 break; |
807 } | 920 } |
808 --scale_counter_[reason]; | 921 |
809 source_proxy_->RequestHigherResolutionThan(current_pixel_count); | 922 // Decrease counter of how many times we have scaled down, for this |
810 LOG(LS_INFO) << "Scaling up resolution."; | 923 // degradation preference mode and reason. |
| 924 --*scale_counter; |
| 925 |
| 926 // Get a sum of how many times have scaled down, in total, for this |
| 927 // degradation preference mode. If it is 0, remove any restraints. |
| 928 const std::vector<int>& current_scale_counters = *GetScaleCounters(); |
| 929 int scale_sum = 0; |
| 930 for (int i : current_scale_counters) |
| 931 scale_sum += i; |
| 932 |
| 933 switch (degradation_preference_) { |
| 934 case DegradationPreference::kBalanced: |
| 935 if (scale_sum == 0) { |
| 936 LOG(LS_INFO) << "Removing resolution down-scaling setting."; |
| 937 source_proxy_->RequestHigherResolutionThan( |
| 938 std::numeric_limits<int>::max()); |
| 939 } else { |
| 940 source_proxy_->RequestHigherResolutionThan( |
| 941 adaptation_request.input_pixel_count_); |
| 942 LOG(LS_INFO) << "Scaling up resolution."; |
| 943 } |
| 944 break; |
| 945 case DegradationPreference::kMaintainResolution: |
| 946 if (scale_sum == 0) { |
| 947 LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
| 948 source_proxy_->RequestHigherFramerateThan( |
| 949 std::numeric_limits<int>::max()); |
| 950 } else { |
| 951 source_proxy_->RequestHigherFramerateThan( |
| 952 adaptation_request.framerate_fps_); |
| 953 LOG(LS_INFO) << "Scaling up framerate."; |
| 954 } |
| 955 break; |
| 956 } |
| 957 |
811 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 958 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
812 LOG(LS_INFO) << "Scaled " << scale_counter_[i] | 959 LOG(LS_INFO) << "Scaled " << (*GetScaleCounters())[i] |
813 << " times for reason: " << (i ? "cpu" : "quality"); | 960 << " times for reason: " << (i ? "cpu" : "quality"); |
814 } | 961 } |
815 } | 962 } |
816 | 963 |
| 964 std::vector<int>* ViEEncoder::GetScaleCounters() { |
| 965 auto it = scale_counters_.find(degradation_preference_); |
| 966 RTC_CHECK(it != scale_counters_.end()); |
| 967 return &it->second; |
| 968 } |
| 969 |
817 } // namespace webrtc | 970 } // namespace webrtc |
OLD | NEW |