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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 pending_encoder_reconfiguration_(false), | 246 pending_encoder_reconfiguration_(false), |
247 encoder_start_bitrate_bps_(0), | 247 encoder_start_bitrate_bps_(0), |
248 max_data_payload_length_(0), | 248 max_data_payload_length_(0), |
249 last_observed_bitrate_bps_(0), | 249 last_observed_bitrate_bps_(0), |
250 encoder_paused_and_dropped_frame_(false), | 250 encoder_paused_and_dropped_frame_(false), |
251 has_received_sli_(false), | 251 has_received_sli_(false), |
252 picture_id_sli_(0), | 252 picture_id_sli_(0), |
253 has_received_rpsi_(false), | 253 has_received_rpsi_(false), |
254 picture_id_rpsi_(0), | 254 picture_id_rpsi_(0), |
255 clock_(Clock::GetRealTimeClock()), | 255 clock_(Clock::GetRealTimeClock()), |
256 degradation_preference_( | |
257 VideoSendStream::DegradationPreference::kBalanced), | |
258 cpu_restricted_counter_(0), | |
259 last_frame_width_(0), | 256 last_frame_width_(0), |
260 last_frame_height_(0), | 257 last_frame_height_(0), |
261 last_captured_timestamp_(0), | 258 last_captured_timestamp_(0), |
262 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 259 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
263 clock_->TimeInMilliseconds()), | 260 clock_->TimeInMilliseconds()), |
264 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 261 last_frame_log_ms_(clock_->TimeInMilliseconds()), |
265 captured_frame_count_(0), | 262 captured_frame_count_(0), |
266 dropped_frame_count_(0), | 263 dropped_frame_count_(0), |
267 encoder_queue_("EncoderQueue") { | 264 encoder_queue_("EncoderQueue") { |
268 encoder_queue_.PostTask([this] { | 265 encoder_queue_.PostTask([this] { |
(...skipping 12 matching lines...) Expand all Loading... |
281 | 278 |
282 void ViEEncoder::Stop() { | 279 void ViEEncoder::Stop() { |
283 RTC_DCHECK_RUN_ON(&thread_checker_); | 280 RTC_DCHECK_RUN_ON(&thread_checker_); |
284 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); | 281 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); |
285 encoder_queue_.PostTask([this] { | 282 encoder_queue_.PostTask([this] { |
286 RTC_DCHECK_RUN_ON(&encoder_queue_); | 283 RTC_DCHECK_RUN_ON(&encoder_queue_); |
287 overuse_detector_.StopCheckForOveruse(); | 284 overuse_detector_.StopCheckForOveruse(); |
288 rate_allocator_.reset(); | 285 rate_allocator_.reset(); |
289 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 286 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
290 false); | 287 false); |
| 288 quality_scaler_ = nullptr; |
291 shutdown_event_.Set(); | 289 shutdown_event_.Set(); |
292 }); | 290 }); |
293 | 291 |
294 shutdown_event_.Wait(rtc::Event::kForever); | 292 shutdown_event_.Wait(rtc::Event::kForever); |
295 } | 293 } |
296 | 294 |
297 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 295 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { |
298 RTC_DCHECK_RUN_ON(&thread_checker_); | 296 RTC_DCHECK_RUN_ON(&thread_checker_); |
299 RTC_DCHECK(!module_process_thread_); | 297 RTC_DCHECK(!module_process_thread_); |
300 module_process_thread_ = module_process_thread; | 298 module_process_thread_ = module_process_thread; |
301 module_process_thread_->RegisterModule(&video_sender_); | 299 module_process_thread_->RegisterModule(&video_sender_); |
302 module_process_thread_checker_.DetachFromThread(); | 300 module_process_thread_checker_.DetachFromThread(); |
303 } | 301 } |
304 | 302 |
305 void ViEEncoder::DeRegisterProcessThread() { | 303 void ViEEncoder::DeRegisterProcessThread() { |
306 RTC_DCHECK_RUN_ON(&thread_checker_); | 304 RTC_DCHECK_RUN_ON(&thread_checker_); |
307 module_process_thread_->DeRegisterModule(&video_sender_); | 305 module_process_thread_->DeRegisterModule(&video_sender_); |
308 } | 306 } |
309 | 307 |
310 void ViEEncoder::SetSource( | 308 void ViEEncoder::SetSource( |
311 rtc::VideoSourceInterface<VideoFrame>* source, | 309 rtc::VideoSourceInterface<VideoFrame>* source, |
312 const VideoSendStream::DegradationPreference& degradation_preference) { | 310 const VideoSendStream::DegradationPreference& degradation_preference) { |
313 RTC_DCHECK_RUN_ON(&thread_checker_); | 311 RTC_DCHECK_RUN_ON(&thread_checker_); |
314 source_proxy_->SetSource(source, degradation_preference); | 312 source_proxy_->SetSource(source, degradation_preference); |
315 encoder_queue_.PostTask([this, degradation_preference] { | 313 encoder_queue_.PostTask([this, degradation_preference] { |
316 RTC_DCHECK_RUN_ON(&encoder_queue_); | 314 RTC_DCHECK_RUN_ON(&encoder_queue_); |
317 degradation_preference_ = degradation_preference; | 315 scaling_enabled_ = |
318 // Set the stats for if we are currently CPU restricted. We are CPU | 316 (degradation_preference != |
319 // restricted depending on degradation preference and | 317 VideoSendStream::DegradationPreference::kMaintainResolution); |
320 // if the overusedetector has currently detected overuse which is counted in | 318 stats_proxy_->SetResolutionRestrictionStats( |
321 // |cpu_restricted_counter_| | 319 scaling_enabled_ && scale_counter_[kQuality] > 0, |
322 // We do this on the encoder task queue to avoid a race with the stats set | 320 scaling_enabled_ && scale_counter_[kCpu] > 0); |
323 // in ViEEncoder::NormalUsage and ViEEncoder::OveruseDetected. | |
324 stats_proxy_->SetCpuRestrictedResolution( | |
325 degradation_preference_ != | |
326 VideoSendStream::DegradationPreference::kMaintainResolution && | |
327 cpu_restricted_counter_ != 0); | |
328 }); | 321 }); |
329 } | 322 } |
330 | 323 |
331 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { | 324 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
332 source_proxy_->SetWantsRotationApplied(rotation_applied); | 325 source_proxy_->SetWantsRotationApplied(rotation_applied); |
333 encoder_queue_.PostTask([this, sink] { | 326 encoder_queue_.PostTask([this, sink] { |
334 RTC_DCHECK_RUN_ON(&encoder_queue_); | 327 RTC_DCHECK_RUN_ON(&encoder_queue_); |
335 sink_ = sink; | 328 sink_ = sink; |
336 }); | 329 }); |
337 } | 330 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 if (framerate == 0) | 398 if (framerate == 0) |
406 framerate = codec.maxFramerate; | 399 framerate = codec.maxFramerate; |
407 stats_proxy_->OnEncoderReconfigured( | 400 stats_proxy_->OnEncoderReconfigured( |
408 encoder_config_, rate_allocator_->GetPreferredBitrateBps(framerate)); | 401 encoder_config_, rate_allocator_->GetPreferredBitrateBps(framerate)); |
409 } | 402 } |
410 | 403 |
411 pending_encoder_reconfiguration_ = false; | 404 pending_encoder_reconfiguration_ = false; |
412 | 405 |
413 sink_->OnEncoderConfigurationChanged( | 406 sink_->OnEncoderConfigurationChanged( |
414 std::move(streams), encoder_config_.min_transmit_bitrate_bps); | 407 std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
| 408 |
| 409 const auto scaling_settings = settings_.encoder->GetScalingSettings(); |
| 410 if (scaling_settings.enabled && scaling_enabled_) { |
| 411 if (scaling_settings.thresholds) { |
| 412 quality_scaler_.reset( |
| 413 new QualityScaler(this, *(scaling_settings.thresholds))); |
| 414 } else { |
| 415 quality_scaler_.reset(new QualityScaler(this, codec_type_)); |
| 416 } |
| 417 } else { |
| 418 quality_scaler_.reset(nullptr); |
| 419 } |
415 } | 420 } |
416 | 421 |
417 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 422 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
418 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 423 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
419 VideoFrame incoming_frame = video_frame; | 424 VideoFrame incoming_frame = video_frame; |
420 | 425 |
421 // Local time in webrtc time base. | 426 // Local time in webrtc time base. |
422 int64_t current_time = clock_->TimeInMilliseconds(); | 427 int64_t current_time = clock_->TimeInMilliseconds(); |
423 incoming_frame.set_render_time_ms(current_time); | 428 incoming_frame.set_render_time_ms(current_time); |
424 | 429 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 // End trace event on first frame after encoder resumes, if frame was dropped. | 487 // End trace event on first frame after encoder resumes, if frame was dropped. |
483 if (encoder_paused_and_dropped_frame_) { | 488 if (encoder_paused_and_dropped_frame_) { |
484 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this); | 489 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this); |
485 } | 490 } |
486 encoder_paused_and_dropped_frame_ = false; | 491 encoder_paused_and_dropped_frame_ = false; |
487 } | 492 } |
488 | 493 |
489 void ViEEncoder::EncodeVideoFrame(const VideoFrame& video_frame, | 494 void ViEEncoder::EncodeVideoFrame(const VideoFrame& video_frame, |
490 int64_t time_when_posted_in_ms) { | 495 int64_t time_when_posted_in_ms) { |
491 RTC_DCHECK_RUN_ON(&encoder_queue_); | 496 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 497 |
492 if (pre_encode_callback_) | 498 if (pre_encode_callback_) |
493 pre_encode_callback_->OnFrame(video_frame); | 499 pre_encode_callback_->OnFrame(video_frame); |
494 | 500 |
495 if (!last_frame_info_ || video_frame.width() != last_frame_info_->width || | 501 if (!last_frame_info_ || video_frame.width() != last_frame_info_->width || |
496 video_frame.height() != last_frame_info_->height || | 502 video_frame.height() != last_frame_info_->height || |
497 video_frame.rotation() != last_frame_info_->rotation || | 503 video_frame.rotation() != last_frame_info_->rotation || |
498 video_frame.is_texture() != last_frame_info_->is_texture) { | 504 video_frame.is_texture() != last_frame_info_->is_texture) { |
499 pending_encoder_reconfiguration_ = true; | 505 pending_encoder_reconfiguration_ = true; |
500 last_frame_info_ = rtc::Optional<VideoFrameInfo>( | 506 last_frame_info_ = rtc::Optional<VideoFrameInfo>( |
501 VideoFrameInfo(video_frame.width(), video_frame.height(), | 507 VideoFrameInfo(video_frame.width(), video_frame.height(), |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 // running in parallel on different threads. | 565 // running in parallel on different threads. |
560 if (stats_proxy_) { | 566 if (stats_proxy_) { |
561 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); | 567 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); |
562 } | 568 } |
563 | 569 |
564 EncodedImageCallback::Result result = | 570 EncodedImageCallback::Result result = |
565 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); | 571 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); |
566 | 572 |
567 int64_t time_sent = clock_->TimeInMilliseconds(); | 573 int64_t time_sent = clock_->TimeInMilliseconds(); |
568 uint32_t timestamp = encoded_image._timeStamp; | 574 uint32_t timestamp = encoded_image._timeStamp; |
569 | 575 const int qp = encoded_image.qp_; |
570 encoder_queue_.PostTask([this, timestamp, time_sent] { | 576 encoder_queue_.PostTask([this, timestamp, time_sent, qp] { |
571 RTC_DCHECK_RUN_ON(&encoder_queue_); | 577 RTC_DCHECK_RUN_ON(&encoder_queue_); |
572 overuse_detector_.FrameSent(timestamp, time_sent); | 578 overuse_detector_.FrameSent(timestamp, time_sent); |
| 579 if (quality_scaler_) |
| 580 quality_scaler_->ReportQP(qp); |
573 }); | 581 }); |
574 | 582 |
575 return result; | 583 return result; |
576 } | 584 } |
577 | 585 |
578 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { | 586 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { |
579 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); | 587 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); |
580 if (stats_proxy_) | 588 if (stats_proxy_) |
581 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); | 589 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); |
582 } | 590 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 bool video_suspension_changed = video_is_suspended != EncoderPaused(); | 647 bool video_suspension_changed = video_is_suspended != EncoderPaused(); |
640 last_observed_bitrate_bps_ = bitrate_bps; | 648 last_observed_bitrate_bps_ = bitrate_bps; |
641 | 649 |
642 if (stats_proxy_ && video_suspension_changed) { | 650 if (stats_proxy_ && video_suspension_changed) { |
643 LOG(LS_INFO) << "Video suspend state changed to: " | 651 LOG(LS_INFO) << "Video suspend state changed to: " |
644 << (video_is_suspended ? "suspended" : "not suspended"); | 652 << (video_is_suspended ? "suspended" : "not suspended"); |
645 stats_proxy_->OnSuspendChange(video_is_suspended); | 653 stats_proxy_->OnSuspendChange(video_is_suspended); |
646 } | 654 } |
647 } | 655 } |
648 | 656 |
649 void ViEEncoder::OveruseDetected() { | 657 void ViEEncoder::ScaleDown(ScaleReason reason) { |
650 RTC_DCHECK_RUN_ON(&encoder_queue_); | 658 RTC_DCHECK_RUN_ON(&encoder_queue_); |
651 if (degradation_preference_ == | 659 if (!scaling_enabled_) |
652 VideoSendStream::DegradationPreference::kMaintainResolution || | |
653 cpu_restricted_counter_ >= kMaxCpuDowngrades) { | |
654 return; | 660 return; |
655 } | |
656 LOG(LS_INFO) << "CPU overuse detected. Requesting lower resolution."; | |
657 // Request lower resolution if the current resolution is lower than last time | 661 // Request lower resolution if the current resolution is lower than last time |
658 // we asked for the resolution to be lowered. | 662 // we asked for the resolution to be lowered. |
659 // Update stats accordingly. | |
660 int current_pixel_count = last_frame_height_ * last_frame_width_; | 663 int current_pixel_count = last_frame_height_ * last_frame_width_; |
661 if (!max_pixel_count_ || current_pixel_count < *max_pixel_count_) { | 664 if (max_pixel_count_ && current_pixel_count >= *max_pixel_count_) |
662 max_pixel_count_ = rtc::Optional<int>(current_pixel_count); | 665 return; |
663 max_pixel_count_step_up_ = rtc::Optional<int>(); | 666 switch (reason) { |
664 stats_proxy_->OnCpuRestrictedResolutionChanged(true); | 667 case kQuality: |
665 ++cpu_restricted_counter_; | 668 if (scale_counter_[reason] >= kMaxQualityDowngrades) |
666 source_proxy_->RequestResolutionLowerThan(current_pixel_count); | 669 return; |
| 670 stats_proxy_->OnQualityRestrictedResolutionChanged(true); |
| 671 break; |
| 672 case kCpu: |
| 673 if (scale_counter_[reason] >= kMaxCpuDowngrades) |
| 674 return; |
| 675 // Update stats accordingly. |
| 676 stats_proxy_->OnCpuRestrictedResolutionChanged(true); |
| 677 break; |
| 678 } |
| 679 max_pixel_count_ = rtc::Optional<int>(current_pixel_count); |
| 680 max_pixel_count_step_up_ = rtc::Optional<int>(); |
| 681 ++scale_counter_[reason]; |
| 682 source_proxy_->RequestResolutionLowerThan(current_pixel_count); |
| 683 LOG(LS_INFO) << "Scaling down resolution."; |
| 684 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
| 685 LOG(LS_INFO) << "Scaled " << scale_counter_[i] |
| 686 << " times for reason: " << (i ? "quality" : "cpu"); |
667 } | 687 } |
668 } | 688 } |
669 | 689 |
670 void ViEEncoder::NormalUsage() { | 690 void ViEEncoder::ScaleUp(ScaleReason reason) { |
671 RTC_DCHECK_RUN_ON(&encoder_queue_); | 691 RTC_DCHECK_RUN_ON(&encoder_queue_); |
672 if (degradation_preference_ == | 692 if (scale_counter_[reason] == 0 || !scaling_enabled_) |
673 VideoSendStream::DegradationPreference::kMaintainResolution || | |
674 cpu_restricted_counter_ == 0) { | |
675 return; | 693 return; |
| 694 // Only scale if resolution is higher than last time |
| 695 // we requested higher resolution. |
| 696 int current_pixel_count = last_frame_height_ * last_frame_width_; |
| 697 if (current_pixel_count <= max_pixel_count_step_up_.value_or(0)) |
| 698 return; |
| 699 switch (reason) { |
| 700 case kQuality: |
| 701 stats_proxy_->OnQualityRestrictedResolutionChanged( |
| 702 scale_counter_[reason] > 1); |
| 703 break; |
| 704 case kCpu: |
| 705 // Update stats accordingly. |
| 706 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counter_[reason] > |
| 707 1); |
| 708 break; |
676 } | 709 } |
677 | 710 max_pixel_count_ = rtc::Optional<int>(); |
678 LOG(LS_INFO) << "CPU underuse detected. Requesting higher resolution."; | 711 max_pixel_count_step_up_ = rtc::Optional<int>(current_pixel_count); |
679 int current_pixel_count = last_frame_height_ * last_frame_width_; | 712 --scale_counter_[reason]; |
680 // Request higher resolution if we are CPU restricted and the the current | 713 source_proxy_->RequestHigherResolutionThan(current_pixel_count); |
681 // resolution is higher than last time we requested higher resolution. | 714 LOG(LS_INFO) << "Scaling up resolution."; |
682 // Update stats accordingly. | 715 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
683 if (!max_pixel_count_step_up_ || | 716 LOG(LS_INFO) << "Scaled " << scale_counter_[i] |
684 current_pixel_count > *max_pixel_count_step_up_) { | 717 << " times for reason: " << (i ? "quality" : "cpu"); |
685 max_pixel_count_ = rtc::Optional<int>(); | |
686 max_pixel_count_step_up_ = rtc::Optional<int>(current_pixel_count); | |
687 --cpu_restricted_counter_; | |
688 stats_proxy_->OnCpuRestrictedResolutionChanged(cpu_restricted_counter_ > 0); | |
689 source_proxy_->RequestHigherResolutionThan(current_pixel_count); | |
690 } | 718 } |
691 } | 719 } |
692 | 720 |
693 } // namespace webrtc | 721 } // namespace webrtc |
OLD | NEW |