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