| 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 15 matching lines...) Expand all Loading... |
| 26 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" | 26 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" |
| 27 #include "webrtc/modules/video_coding/include/video_codec_initializer.h" | 27 #include "webrtc/modules/video_coding/include/video_codec_initializer.h" |
| 28 #include "webrtc/modules/video_coding/include/video_coding.h" | 28 #include "webrtc/modules/video_coding/include/video_coding.h" |
| 29 #include "webrtc/modules/video_coding/include/video_coding_defines.h" | 29 #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
| 30 #include "webrtc/video/overuse_frame_detector.h" | 30 #include "webrtc/video/overuse_frame_detector.h" |
| 31 #include "webrtc/video/send_statistics_proxy.h" | 31 #include "webrtc/video/send_statistics_proxy.h" |
| 32 #include "webrtc/video_frame.h" | 32 #include "webrtc/video_frame.h" |
| 33 namespace webrtc { | 33 namespace webrtc { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 using DegradationPreference = VideoSendStream::DegradationPreference; |
| 36 | 37 |
| 37 // Time interval for logging frame counts. | 38 // Time interval for logging frame counts. |
| 38 const int64_t kFrameLogIntervalMs = 60000; | 39 const int64_t kFrameLogIntervalMs = 60000; |
| 39 | 40 |
| 40 // We will never ask for a resolution lower than this. | 41 // We will never ask for a resolution lower than this. |
| 41 // TODO(kthelgason): Lower this limit when better testing | 42 // TODO(kthelgason): Lower this limit when better testing |
| 42 // on MediaCodec and fallback implementations are in place. | 43 // on MediaCodec and fallback implementations are in place. |
| 43 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 | 44 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 |
| 44 const int kMinPixelsPerFrame = 320 * 180; | 45 const int kMinPixelsPerFrame = 320 * 180; |
| 45 const int kMinFramerateFps = 2; | 46 const int kMinFramerateFps = 2; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 }; | 145 }; |
| 145 | 146 |
| 146 // VideoSourceProxy is responsible ensuring thread safety between calls to | 147 // VideoSourceProxy is responsible ensuring thread safety between calls to |
| 147 // ViEEncoder::SetSource that will happen on libjingle's worker thread when a | 148 // ViEEncoder::SetSource that will happen on libjingle's worker thread when a |
| 148 // video capturer is connected to the encoder and the encoder task queue | 149 // video capturer is connected to the encoder and the encoder task queue |
| 149 // (encoder_queue_) where the encoder reports its VideoSinkWants. | 150 // (encoder_queue_) where the encoder reports its VideoSinkWants. |
| 150 class ViEEncoder::VideoSourceProxy { | 151 class ViEEncoder::VideoSourceProxy { |
| 151 public: | 152 public: |
| 152 explicit VideoSourceProxy(ViEEncoder* vie_encoder) | 153 explicit VideoSourceProxy(ViEEncoder* vie_encoder) |
| 153 : vie_encoder_(vie_encoder), | 154 : vie_encoder_(vie_encoder), |
| 154 degradation_preference_( | 155 degradation_preference_(DegradationPreference::kDegradationDisabled), |
| 155 VideoSendStream::DegradationPreference::kDegradationDisabled), | |
| 156 source_(nullptr) {} | 156 source_(nullptr) {} |
| 157 | 157 |
| 158 void SetSource( | 158 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
| 159 rtc::VideoSourceInterface<VideoFrame>* source, | 159 const DegradationPreference& degradation_preference) { |
| 160 const VideoSendStream::DegradationPreference& degradation_preference) { | |
| 161 // Called on libjingle's worker thread. | 160 // Called on libjingle's worker thread. |
| 162 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | 161 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); |
| 163 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | 162 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; |
| 164 rtc::VideoSinkWants wants; | 163 rtc::VideoSinkWants wants; |
| 165 { | 164 { |
| 166 rtc::CritScope lock(&crit_); | 165 rtc::CritScope lock(&crit_); |
| 167 degradation_preference_ = degradation_preference; | 166 degradation_preference_ = degradation_preference; |
| 168 old_source = source_; | 167 old_source = source_; |
| 169 source_ = source; | 168 source_ = source; |
| 170 wants = GetActiveSinkWants(); | 169 wants = GetActiveSinkWants(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 186 sink_wants_.rotation_applied = rotation_applied; | 185 sink_wants_.rotation_applied = rotation_applied; |
| 187 if (source_) | 186 if (source_) |
| 188 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 187 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
| 189 } | 188 } |
| 190 | 189 |
| 191 rtc::VideoSinkWants GetActiveSinkWants() EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 190 rtc::VideoSinkWants GetActiveSinkWants() EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
| 192 rtc::VideoSinkWants wants = sink_wants_; | 191 rtc::VideoSinkWants wants = sink_wants_; |
| 193 // Clear any constraints from the current sink wants that don't apply to | 192 // Clear any constraints from the current sink wants that don't apply to |
| 194 // the used degradation_preference. | 193 // the used degradation_preference. |
| 195 switch (degradation_preference_) { | 194 switch (degradation_preference_) { |
| 196 case VideoSendStream::DegradationPreference::kBalanced: | 195 case DegradationPreference::kBalanced: |
| 197 FALLTHROUGH(); | 196 FALLTHROUGH(); |
| 198 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 197 case DegradationPreference::kMaintainFramerate: |
| 199 wants.max_framerate_fps = std::numeric_limits<int>::max(); | 198 wants.max_framerate_fps = std::numeric_limits<int>::max(); |
| 200 break; | 199 break; |
| 201 case VideoSendStream::DegradationPreference::kMaintainResolution: | 200 case DegradationPreference::kMaintainResolution: |
| 202 wants.max_pixel_count = std::numeric_limits<int>::max(); | 201 wants.max_pixel_count = std::numeric_limits<int>::max(); |
| 203 wants.target_pixel_count.reset(); | 202 wants.target_pixel_count.reset(); |
| 204 break; | 203 break; |
| 205 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 204 case DegradationPreference::kDegradationDisabled: |
| 206 wants.max_pixel_count = std::numeric_limits<int>::max(); | 205 wants.max_pixel_count = std::numeric_limits<int>::max(); |
| 207 wants.target_pixel_count.reset(); | 206 wants.target_pixel_count.reset(); |
| 208 wants.max_framerate_fps = std::numeric_limits<int>::max(); | 207 wants.max_framerate_fps = std::numeric_limits<int>::max(); |
| 209 } | 208 } |
| 210 return wants; | 209 return wants; |
| 211 } | 210 } |
| 212 | 211 |
| 213 void RequestResolutionLowerThan(int pixel_count) { | 212 void RequestResolutionLowerThan(int pixel_count) { |
| 214 // Called on the encoder task queue. | 213 // Called on the encoder task queue. |
| 215 rtc::CritScope lock(&crit_); | 214 rtc::CritScope lock(&crit_); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 sink_wants_.max_framerate_fps = framerate_wanted; | 295 sink_wants_.max_framerate_fps = framerate_wanted; |
| 297 } | 296 } |
| 298 if (source_) | 297 if (source_) |
| 299 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 298 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); |
| 300 } | 299 } |
| 301 | 300 |
| 302 private: | 301 private: |
| 303 bool IsResolutionScalingEnabledLocked() const | 302 bool IsResolutionScalingEnabledLocked() const |
| 304 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 303 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
| 305 return degradation_preference_ == | 304 return degradation_preference_ == |
| 306 VideoSendStream::DegradationPreference::kMaintainFramerate || | 305 DegradationPreference::kMaintainFramerate || |
| 307 degradation_preference_ == | 306 degradation_preference_ == DegradationPreference::kBalanced; |
| 308 VideoSendStream::DegradationPreference::kBalanced; | |
| 309 } | 307 } |
| 310 | 308 |
| 311 bool IsFramerateScalingEnabledLocked() const | 309 bool IsFramerateScalingEnabledLocked() const |
| 312 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 310 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
| 313 // TODO(sprang): Also accept kBalanced here? | 311 // TODO(sprang): Also accept kBalanced here? |
| 314 return degradation_preference_ == | 312 return degradation_preference_ == |
| 315 VideoSendStream::DegradationPreference::kMaintainResolution; | 313 DegradationPreference::kMaintainResolution; |
| 316 } | 314 } |
| 317 | 315 |
| 318 rtc::CriticalSection crit_; | 316 rtc::CriticalSection crit_; |
| 319 rtc::SequencedTaskChecker main_checker_; | 317 rtc::SequencedTaskChecker main_checker_; |
| 320 ViEEncoder* const vie_encoder_; | 318 ViEEncoder* const vie_encoder_; |
| 321 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); | 319 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); |
| 322 VideoSendStream::DegradationPreference degradation_preference_ | 320 DegradationPreference degradation_preference_ GUARDED_BY(&crit_); |
| 323 GUARDED_BY(&crit_); | |
| 324 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 321 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); |
| 325 | 322 |
| 326 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 323 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
| 327 }; | 324 }; |
| 328 | 325 |
| 329 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 326 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
| 330 SendStatisticsProxy* stats_proxy, | 327 SendStatisticsProxy* stats_proxy, |
| 331 const VideoSendStream::Config::EncoderSettings& settings, | 328 const VideoSendStream::Config::EncoderSettings& settings, |
| 332 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 329 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
| 333 EncodedFrameObserver* encoder_timing) | 330 EncodedFrameObserver* encoder_timing) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 347 stats_proxy_(stats_proxy), | 344 stats_proxy_(stats_proxy), |
| 348 pre_encode_callback_(pre_encode_callback), | 345 pre_encode_callback_(pre_encode_callback), |
| 349 module_process_thread_(nullptr), | 346 module_process_thread_(nullptr), |
| 350 pending_encoder_reconfiguration_(false), | 347 pending_encoder_reconfiguration_(false), |
| 351 encoder_start_bitrate_bps_(0), | 348 encoder_start_bitrate_bps_(0), |
| 352 max_data_payload_length_(0), | 349 max_data_payload_length_(0), |
| 353 nack_enabled_(false), | 350 nack_enabled_(false), |
| 354 last_observed_bitrate_bps_(0), | 351 last_observed_bitrate_bps_(0), |
| 355 encoder_paused_and_dropped_frame_(false), | 352 encoder_paused_and_dropped_frame_(false), |
| 356 clock_(Clock::GetRealTimeClock()), | 353 clock_(Clock::GetRealTimeClock()), |
| 357 degradation_preference_( | 354 degradation_preference_(DegradationPreference::kDegradationDisabled), |
| 358 VideoSendStream::DegradationPreference::kDegradationDisabled), | |
| 359 last_captured_timestamp_(0), | 355 last_captured_timestamp_(0), |
| 360 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 356 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
| 361 clock_->TimeInMilliseconds()), | 357 clock_->TimeInMilliseconds()), |
| 362 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 358 last_frame_log_ms_(clock_->TimeInMilliseconds()), |
| 363 captured_frame_count_(0), | 359 captured_frame_count_(0), |
| 364 dropped_frame_count_(0), | 360 dropped_frame_count_(0), |
| 365 bitrate_observer_(nullptr), | 361 bitrate_observer_(nullptr), |
| 366 encoder_queue_("EncoderQueue") { | 362 encoder_queue_("EncoderQueue") { |
| 367 RTC_DCHECK(stats_proxy); | 363 RTC_DCHECK(stats_proxy); |
| 368 encoder_queue_.PostTask([this] { | 364 encoder_queue_.PostTask([this] { |
| 369 RTC_DCHECK_RUN_ON(&encoder_queue_); | 365 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 370 overuse_detector_.StartCheckForOveruse(); | 366 overuse_detector_.StartCheckForOveruse(); |
| 371 video_sender_.RegisterExternalEncoder( | 367 video_sender_.RegisterExternalEncoder( |
| 372 settings_.encoder, settings_.payload_type, settings_.internal_source); | 368 settings_.encoder, settings_.payload_type, settings_.internal_source); |
| 373 }); | 369 }); |
| 374 } | 370 } |
| 375 | 371 |
| 376 ViEEncoder::~ViEEncoder() { | 372 ViEEncoder::~ViEEncoder() { |
| 377 RTC_DCHECK_RUN_ON(&thread_checker_); | 373 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 378 RTC_DCHECK(shutdown_event_.Wait(0)) | 374 RTC_DCHECK(shutdown_event_.Wait(0)) |
| 379 << "Must call ::Stop() before destruction."; | 375 << "Must call ::Stop() before destruction."; |
| 380 } | 376 } |
| 381 | 377 |
| 382 void ViEEncoder::Stop() { | 378 void ViEEncoder::Stop() { |
| 383 RTC_DCHECK_RUN_ON(&thread_checker_); | 379 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 384 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); | 380 source_proxy_->SetSource(nullptr, DegradationPreference()); |
| 385 encoder_queue_.PostTask([this] { | 381 encoder_queue_.PostTask([this] { |
| 386 RTC_DCHECK_RUN_ON(&encoder_queue_); | 382 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 387 overuse_detector_.StopCheckForOveruse(); | 383 overuse_detector_.StopCheckForOveruse(); |
| 388 rate_allocator_.reset(); | 384 rate_allocator_.reset(); |
| 389 bitrate_observer_ = nullptr; | 385 bitrate_observer_ = nullptr; |
| 390 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 386 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
| 391 false); | 387 false); |
| 392 quality_scaler_ = nullptr; | 388 quality_scaler_ = nullptr; |
| 393 shutdown_event_.Set(); | 389 shutdown_event_.Set(); |
| 394 }); | 390 }); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 414 RTC_DCHECK_RUN_ON(&thread_checker_); | 410 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 415 encoder_queue_.PostTask([this, bitrate_observer] { | 411 encoder_queue_.PostTask([this, bitrate_observer] { |
| 416 RTC_DCHECK_RUN_ON(&encoder_queue_); | 412 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 417 RTC_DCHECK(!bitrate_observer_); | 413 RTC_DCHECK(!bitrate_observer_); |
| 418 bitrate_observer_ = bitrate_observer; | 414 bitrate_observer_ = bitrate_observer; |
| 419 }); | 415 }); |
| 420 } | 416 } |
| 421 | 417 |
| 422 void ViEEncoder::SetSource( | 418 void ViEEncoder::SetSource( |
| 423 rtc::VideoSourceInterface<VideoFrame>* source, | 419 rtc::VideoSourceInterface<VideoFrame>* source, |
| 424 const VideoSendStream::VideoSendStream::DegradationPreference& | 420 const VideoSendStream::DegradationPreference& degradation_preference) { |
| 425 degradation_preference) { | |
| 426 RTC_DCHECK_RUN_ON(&thread_checker_); | 421 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 427 source_proxy_->SetSource(source, degradation_preference); | 422 source_proxy_->SetSource(source, degradation_preference); |
| 428 encoder_queue_.PostTask([this, degradation_preference] { | 423 encoder_queue_.PostTask([this, degradation_preference] { |
| 429 RTC_DCHECK_RUN_ON(&encoder_queue_); | 424 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 430 if (degradation_preference_ != degradation_preference) { | 425 if (degradation_preference_ != degradation_preference) { |
| 431 // Reset adaptation state, so that we're not tricked into thinking there's | 426 // Reset adaptation state, so that we're not tricked into thinking there's |
| 432 // an already pending request of the same type. | 427 // an already pending request of the same type. |
| 433 last_adaptation_request_.reset(); | 428 last_adaptation_request_.reset(); |
| 434 } | 429 } |
| 435 degradation_preference_ = degradation_preference; | 430 degradation_preference_ = degradation_preference; |
| 436 bool allow_scaling = | 431 bool allow_scaling = |
| 437 degradation_preference_ == | 432 degradation_preference_ == DegradationPreference::kMaintainFramerate || |
| 438 VideoSendStream::DegradationPreference::kMaintainFramerate || | 433 degradation_preference_ == DegradationPreference::kBalanced; |
| 439 degradation_preference_ == | |
| 440 VideoSendStream::DegradationPreference::kBalanced; | |
| 441 initial_rampup_ = allow_scaling ? 0 : kMaxInitialFramedrop; | 434 initial_rampup_ = allow_scaling ? 0 : kMaxInitialFramedrop; |
| 442 ConfigureQualityScaler(); | 435 ConfigureQualityScaler(); |
| 443 }); | 436 }); |
| 444 } | 437 } |
| 445 | 438 |
| 446 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { | 439 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
| 447 source_proxy_->SetWantsRotationApplied(rotation_applied); | 440 source_proxy_->SetWantsRotationApplied(rotation_applied); |
| 448 encoder_queue_.PostTask([this, sink] { | 441 encoder_queue_.PostTask([this, sink] { |
| 449 RTC_DCHECK_RUN_ON(&encoder_queue_); | 442 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 450 sink_ = sink; | 443 sink_ = sink; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 sink_->OnEncoderConfigurationChanged( | 527 sink_->OnEncoderConfigurationChanged( |
| 535 std::move(streams), encoder_config_.min_transmit_bitrate_bps); | 528 std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
| 536 | 529 |
| 537 ConfigureQualityScaler(); | 530 ConfigureQualityScaler(); |
| 538 } | 531 } |
| 539 | 532 |
| 540 void ViEEncoder::ConfigureQualityScaler() { | 533 void ViEEncoder::ConfigureQualityScaler() { |
| 541 RTC_DCHECK_RUN_ON(&encoder_queue_); | 534 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 542 const auto scaling_settings = settings_.encoder->GetScalingSettings(); | 535 const auto scaling_settings = settings_.encoder->GetScalingSettings(); |
| 543 const bool degradation_preference_allows_scaling = | 536 const bool degradation_preference_allows_scaling = |
| 544 degradation_preference_ == | 537 degradation_preference_ == DegradationPreference::kMaintainFramerate || |
| 545 VideoSendStream::DegradationPreference::kMaintainFramerate || | 538 degradation_preference_ == DegradationPreference::kBalanced; |
| 546 degradation_preference_ == | |
| 547 VideoSendStream::DegradationPreference::kBalanced; | |
| 548 const bool quality_scaling_allowed = | 539 const bool quality_scaling_allowed = |
| 549 degradation_preference_allows_scaling && scaling_settings.enabled; | 540 degradation_preference_allows_scaling && scaling_settings.enabled; |
| 550 | 541 |
| 551 const std::vector<int>& scale_counters = GetScaleCounters(); | 542 const std::vector<int>& scale_counters = GetScaleCounters(); |
| 552 stats_proxy_->SetCpuScalingStats( | 543 stats_proxy_->SetCpuScalingStats( |
| 553 degradation_preference_allows_scaling ? scale_counters[kCpu] > 0 : false); | 544 degradation_preference_allows_scaling ? scale_counters[kCpu] > 0 : false); |
| 554 stats_proxy_->SetQualityScalingStats( | 545 stats_proxy_->SetQualityScalingStats( |
| 555 quality_scaling_allowed ? scale_counters[kQuality] : -1); | 546 quality_scaling_allowed ? scale_counters[kQuality] : -1); |
| 556 | 547 |
| 557 if (quality_scaling_allowed) { | 548 if (quality_scaling_allowed) { |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 AdaptationRequest adaptation_request = { | 791 AdaptationRequest adaptation_request = { |
| 801 last_frame_info_->pixel_count(), | 792 last_frame_info_->pixel_count(), |
| 802 stats_proxy_->GetStats().input_frame_rate, | 793 stats_proxy_->GetStats().input_frame_rate, |
| 803 AdaptationRequest::Mode::kAdaptDown}; | 794 AdaptationRequest::Mode::kAdaptDown}; |
| 804 bool downgrade_requested = | 795 bool downgrade_requested = |
| 805 last_adaptation_request_ && | 796 last_adaptation_request_ && |
| 806 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; | 797 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; |
| 807 | 798 |
| 808 int max_downgrades = 0; | 799 int max_downgrades = 0; |
| 809 switch (degradation_preference_) { | 800 switch (degradation_preference_) { |
| 810 case VideoSendStream::DegradationPreference::kBalanced: | 801 case DegradationPreference::kBalanced: |
| 811 FALLTHROUGH(); | 802 FALLTHROUGH(); |
| 812 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 803 case DegradationPreference::kMaintainFramerate: |
| 813 max_downgrades = kMaxCpuResolutionDowngrades; | 804 max_downgrades = kMaxCpuResolutionDowngrades; |
| 814 if (downgrade_requested && | 805 if (downgrade_requested && |
| 815 adaptation_request.input_pixel_count_ >= | 806 adaptation_request.input_pixel_count_ >= |
| 816 last_adaptation_request_->input_pixel_count_) { | 807 last_adaptation_request_->input_pixel_count_) { |
| 817 // Don't request lower resolution if the current resolution is not | 808 // Don't request lower resolution if the current resolution is not |
| 818 // lower than the last time we asked for the resolution to be lowered. | 809 // lower than the last time we asked for the resolution to be lowered. |
| 819 return; | 810 return; |
| 820 } | 811 } |
| 821 break; | 812 break; |
| 822 case VideoSendStream::DegradationPreference::kMaintainResolution: | 813 case DegradationPreference::kMaintainResolution: |
| 823 max_downgrades = kMaxCpuFramerateDowngrades; | 814 max_downgrades = kMaxCpuFramerateDowngrades; |
| 824 if (adaptation_request.framerate_fps_ <= 0 || | 815 if (adaptation_request.framerate_fps_ <= 0 || |
| 825 (downgrade_requested && | 816 (downgrade_requested && |
| 826 adaptation_request.framerate_fps_ < kMinFramerateFps)) { | 817 adaptation_request.framerate_fps_ < kMinFramerateFps)) { |
| 827 // If no input fps estimate available, can't determine how to scale down | 818 // If no input fps estimate available, can't determine how to scale down |
| 828 // framerate. Otherwise, don't request lower framerate if we don't have | 819 // framerate. Otherwise, don't request lower framerate if we don't have |
| 829 // a valid frame rate. Since framerate, unlike resolution, is a measure | 820 // a valid frame rate. Since framerate, unlike resolution, is a measure |
| 830 // we have to estimate, and can fluctuate naturally over time, don't | 821 // we have to estimate, and can fluctuate naturally over time, don't |
| 831 // make the same kind of limitations as for resolution, but trust the | 822 // make the same kind of limitations as for resolution, but trust the |
| 832 // overuse detector to not trigger too often. | 823 // overuse detector to not trigger too often. |
| 833 return; | 824 return; |
| 834 } | 825 } |
| 835 break; | 826 break; |
| 836 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 827 case DegradationPreference::kDegradationDisabled: |
| 837 return; | 828 return; |
| 838 } | 829 } |
| 839 | 830 |
| 840 last_adaptation_request_.emplace(adaptation_request); | 831 last_adaptation_request_.emplace(adaptation_request); |
| 841 const std::vector<int>& scale_counter = GetScaleCounters(); | 832 const std::vector<int>& scale_counter = GetScaleCounters(); |
| 842 | 833 |
| 843 switch (reason) { | 834 switch (reason) { |
| 844 case kQuality: | 835 case kQuality: |
| 845 stats_proxy_->OnQualityRestrictedResolutionChanged(scale_counter[reason] + | 836 stats_proxy_->OnQualityRestrictedResolutionChanged(scale_counter[reason] + |
| 846 1); | 837 1); |
| 847 break; | 838 break; |
| 848 case kCpu: | 839 case kCpu: |
| 849 if (scale_counter[reason] >= max_downgrades) | 840 if (scale_counter[reason] >= max_downgrades) |
| 850 return; | 841 return; |
| 851 // Update stats accordingly. | 842 // Update stats accordingly. |
| 852 stats_proxy_->OnCpuRestrictedResolutionChanged(true); | 843 stats_proxy_->OnCpuRestrictedResolutionChanged(true); |
| 853 break; | 844 break; |
| 854 } | 845 } |
| 855 | 846 |
| 856 IncrementScaleCounter(reason, 1); | 847 IncrementScaleCounter(reason, 1); |
| 857 | 848 |
| 858 switch (degradation_preference_) { | 849 switch (degradation_preference_) { |
| 859 case VideoSendStream::DegradationPreference::kBalanced: | 850 case DegradationPreference::kBalanced: |
| 860 FALLTHROUGH(); | 851 FALLTHROUGH(); |
| 861 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 852 case DegradationPreference::kMaintainFramerate: |
| 862 source_proxy_->RequestResolutionLowerThan( | 853 source_proxy_->RequestResolutionLowerThan( |
| 863 adaptation_request.input_pixel_count_); | 854 adaptation_request.input_pixel_count_); |
| 864 LOG(LS_INFO) << "Scaling down resolution."; | 855 LOG(LS_INFO) << "Scaling down resolution."; |
| 865 break; | 856 break; |
| 866 case VideoSendStream::DegradationPreference::kMaintainResolution: | 857 case DegradationPreference::kMaintainResolution: |
| 867 source_proxy_->RequestFramerateLowerThan( | 858 source_proxy_->RequestFramerateLowerThan( |
| 868 adaptation_request.framerate_fps_); | 859 adaptation_request.framerate_fps_); |
| 869 LOG(LS_INFO) << "Scaling down framerate."; | 860 LOG(LS_INFO) << "Scaling down framerate."; |
| 870 break; | 861 break; |
| 871 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 862 case DegradationPreference::kDegradationDisabled: |
| 872 RTC_NOTREACHED(); | 863 RTC_NOTREACHED(); |
| 873 } | 864 } |
| 874 | 865 |
| 875 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 866 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
| 876 LOG(LS_INFO) << "Scaled " << GetScaleCounters()[i] | 867 LOG(LS_INFO) << "Scaled " << GetScaleCounters()[i] |
| 877 << " times for reason: " << (i ? "cpu" : "quality"); | 868 << " times for reason: " << (i ? "cpu" : "quality"); |
| 878 } | 869 } |
| 879 } | 870 } |
| 880 | 871 |
| 881 void ViEEncoder::AdaptUp(AdaptReason reason) { | 872 void ViEEncoder::AdaptUp(AdaptReason reason) { |
| 882 RTC_DCHECK_RUN_ON(&encoder_queue_); | 873 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 883 int scale_counter = GetScaleCounters()[reason]; | 874 int scale_counter = GetScaleCounters()[reason]; |
| 884 if (scale_counter == 0) | 875 if (scale_counter == 0) |
| 885 return; | 876 return; |
| 886 RTC_DCHECK_GT(scale_counter, 0); | 877 RTC_DCHECK_GT(scale_counter, 0); |
| 887 AdaptationRequest adaptation_request = { | 878 AdaptationRequest adaptation_request = { |
| 888 last_frame_info_->pixel_count(), | 879 last_frame_info_->pixel_count(), |
| 889 stats_proxy_->GetStats().input_frame_rate, | 880 stats_proxy_->GetStats().input_frame_rate, |
| 890 AdaptationRequest::Mode::kAdaptUp}; | 881 AdaptationRequest::Mode::kAdaptUp}; |
| 891 | 882 |
| 892 bool adapt_up_requested = | 883 bool adapt_up_requested = |
| 893 last_adaptation_request_ && | 884 last_adaptation_request_ && |
| 894 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; | 885 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; |
| 895 switch (degradation_preference_) { | 886 switch (degradation_preference_) { |
| 896 case VideoSendStream::DegradationPreference::kBalanced: | 887 case DegradationPreference::kBalanced: |
| 897 FALLTHROUGH(); | 888 FALLTHROUGH(); |
| 898 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 889 case DegradationPreference::kMaintainFramerate: |
| 899 if (adapt_up_requested && | 890 if (adapt_up_requested && |
| 900 adaptation_request.input_pixel_count_ <= | 891 adaptation_request.input_pixel_count_ <= |
| 901 last_adaptation_request_->input_pixel_count_) { | 892 last_adaptation_request_->input_pixel_count_) { |
| 902 // Don't request higher resolution if the current resolution is not | 893 // Don't request higher resolution if the current resolution is not |
| 903 // higher than the last time we asked for the resolution to be higher. | 894 // higher than the last time we asked for the resolution to be higher. |
| 904 return; | 895 return; |
| 905 } | 896 } |
| 906 break; | 897 break; |
| 907 case VideoSendStream::DegradationPreference::kMaintainResolution: | 898 case DegradationPreference::kMaintainResolution: |
| 908 // TODO(sprang): Don't request higher framerate if we are already at | 899 // TODO(sprang): Don't request higher framerate if we are already at |
| 909 // max requested fps? | 900 // max requested fps? |
| 910 break; | 901 break; |
| 911 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 902 case DegradationPreference::kDegradationDisabled: |
| 912 return; | 903 return; |
| 913 } | 904 } |
| 914 | 905 |
| 915 last_adaptation_request_.emplace(adaptation_request); | 906 last_adaptation_request_.emplace(adaptation_request); |
| 916 | 907 |
| 917 switch (reason) { | 908 switch (reason) { |
| 918 case kQuality: | 909 case kQuality: |
| 919 stats_proxy_->OnQualityRestrictedResolutionChanged(scale_counter - 1); | 910 stats_proxy_->OnQualityRestrictedResolutionChanged(scale_counter - 1); |
| 920 break; | 911 break; |
| 921 case kCpu: | 912 case kCpu: |
| 922 // Update stats accordingly. | 913 // Update stats accordingly. |
| 923 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counter > 1); | 914 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counter > 1); |
| 924 break; | 915 break; |
| 925 } | 916 } |
| 926 | 917 |
| 927 // Decrease counter of how many times we have scaled down, for this | 918 // Decrease counter of how many times we have scaled down, for this |
| 928 // degradation preference mode and reason. | 919 // degradation preference mode and reason. |
| 929 IncrementScaleCounter(reason, -1); | 920 IncrementScaleCounter(reason, -1); |
| 930 | 921 |
| 931 // Get a sum of how many times have scaled down, in total, for this | 922 // Get a sum of how many times have scaled down, in total, for this |
| 932 // degradation preference mode. If it is 0, remove any restraints. | 923 // degradation preference mode. If it is 0, remove any restraints. |
| 933 const std::vector<int>& current_scale_counters = GetScaleCounters(); | 924 const std::vector<int>& current_scale_counters = GetScaleCounters(); |
| 934 const int scale_sum = std::accumulate(current_scale_counters.begin(), | 925 const int scale_sum = std::accumulate(current_scale_counters.begin(), |
| 935 current_scale_counters.end(), 0); | 926 current_scale_counters.end(), 0); |
| 936 switch (degradation_preference_) { | 927 switch (degradation_preference_) { |
| 937 case VideoSendStream::DegradationPreference::kBalanced: | 928 case DegradationPreference::kBalanced: |
| 938 FALLTHROUGH(); | 929 FALLTHROUGH(); |
| 939 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 930 case DegradationPreference::kMaintainFramerate: |
| 940 if (scale_sum == 0) { | 931 if (scale_sum == 0) { |
| 941 LOG(LS_INFO) << "Removing resolution down-scaling setting."; | 932 LOG(LS_INFO) << "Removing resolution down-scaling setting."; |
| 942 source_proxy_->RequestHigherResolutionThan( | 933 source_proxy_->RequestHigherResolutionThan( |
| 943 std::numeric_limits<int>::max()); | 934 std::numeric_limits<int>::max()); |
| 944 } else { | 935 } else { |
| 945 source_proxy_->RequestHigherResolutionThan( | 936 source_proxy_->RequestHigherResolutionThan( |
| 946 adaptation_request.input_pixel_count_); | 937 adaptation_request.input_pixel_count_); |
| 947 LOG(LS_INFO) << "Scaling up resolution."; | 938 LOG(LS_INFO) << "Scaling up resolution."; |
| 948 } | 939 } |
| 949 break; | 940 break; |
| 950 case VideoSendStream::DegradationPreference::kMaintainResolution: | 941 case DegradationPreference::kMaintainResolution: |
| 951 if (scale_sum == 0) { | 942 if (scale_sum == 0) { |
| 952 LOG(LS_INFO) << "Removing framerate down-scaling setting."; | 943 LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
| 953 source_proxy_->RequestHigherFramerateThan( | 944 source_proxy_->RequestHigherFramerateThan( |
| 954 std::numeric_limits<int>::max()); | 945 std::numeric_limits<int>::max()); |
| 955 } else { | 946 } else { |
| 956 source_proxy_->RequestHigherFramerateThan( | 947 source_proxy_->RequestHigherFramerateThan( |
| 957 adaptation_request.framerate_fps_); | 948 adaptation_request.framerate_fps_); |
| 958 LOG(LS_INFO) << "Scaling up framerate."; | 949 LOG(LS_INFO) << "Scaling up framerate."; |
| 959 } | 950 } |
| 960 break; | 951 break; |
| 961 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 952 case DegradationPreference::kDegradationDisabled: |
| 962 RTC_NOTREACHED(); | 953 RTC_NOTREACHED(); |
| 963 } | 954 } |
| 964 | 955 |
| 965 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 956 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
| 966 LOG(LS_INFO) << "Scaled " << current_scale_counters[i] | 957 LOG(LS_INFO) << "Scaled " << current_scale_counters[i] |
| 967 << " times for reason: " << (i ? "cpu" : "quality"); | 958 << " times for reason: " << (i ? "cpu" : "quality"); |
| 968 } | 959 } |
| 969 } | 960 } |
| 970 | 961 |
| 971 const std::vector<int>& ViEEncoder::GetScaleCounters() { | 962 const std::vector<int>& ViEEncoder::GetScaleCounters() { |
| 972 auto it = scale_counters_.find(degradation_preference_); | 963 auto it = scale_counters_.find(degradation_preference_); |
| 973 if (it == scale_counters_.end()) { | 964 if (it == scale_counters_.end()) { |
| 974 scale_counters_[degradation_preference_].resize(kScaleReasonSize); | 965 scale_counters_[degradation_preference_].resize(kScaleReasonSize); |
| 975 return scale_counters_[degradation_preference_]; | 966 return scale_counters_[degradation_preference_]; |
| 976 } | 967 } |
| 977 return it->second; | 968 return it->second; |
| 978 } | 969 } |
| 979 | 970 |
| 980 void ViEEncoder::IncrementScaleCounter(int reason, int delta) { | 971 void ViEEncoder::IncrementScaleCounter(int reason, int delta) { |
| 981 // Get the counters and validate. This may also lazily initialize the state. | 972 // Get the counters and validate. This may also lazily initialize the state. |
| 982 const std::vector<int>& counter = GetScaleCounters(); | 973 const std::vector<int>& counter = GetScaleCounters(); |
| 983 if (delta < 0) { | 974 if (delta < 0) { |
| 984 RTC_DCHECK_GE(counter[reason], delta); | 975 RTC_DCHECK_GE(counter[reason], delta); |
| 985 } | 976 } |
| 986 scale_counters_[degradation_preference_][reason] += delta; | 977 scale_counters_[degradation_preference_][reason] += delta; |
| 987 } | 978 } |
| 988 | 979 |
| 989 } // namespace webrtc | 980 } // namespace webrtc |
| OLD | NEW |