Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: webrtc/video/vie_encoder.cc

Issue 2398963003: Move usage of QualityScaler to ViEEncoder. (Closed)
Patch Set: Report dropped frames to ViEEncoder Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
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
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
586 void ViEEncoder::OnDroppedFrame() {
587 encoder_queue_.PostTask([this] {
588 RTC_DCHECK_RUN_ON(&encoder_queue_);
589 if (quality_scaler_)
590 quality_scaler_->ReportDroppedFrame();
591 });
592 }
593
578 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { 594 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) {
579 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); 595 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
580 if (stats_proxy_) 596 if (stats_proxy_)
581 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); 597 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate);
582 } 598 }
583 599
584 void ViEEncoder::OnReceivedSLI(uint8_t picture_id) { 600 void ViEEncoder::OnReceivedSLI(uint8_t picture_id) {
585 if (!encoder_queue_.IsCurrent()) { 601 if (!encoder_queue_.IsCurrent()) {
586 encoder_queue_.PostTask([this, picture_id] { OnReceivedSLI(picture_id); }); 602 encoder_queue_.PostTask([this, picture_id] { OnReceivedSLI(picture_id); });
587 return; 603 return;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 bool video_suspension_changed = video_is_suspended != EncoderPaused(); 655 bool video_suspension_changed = video_is_suspended != EncoderPaused();
640 last_observed_bitrate_bps_ = bitrate_bps; 656 last_observed_bitrate_bps_ = bitrate_bps;
641 657
642 if (stats_proxy_ && video_suspension_changed) { 658 if (stats_proxy_ && video_suspension_changed) {
643 LOG(LS_INFO) << "Video suspend state changed to: " 659 LOG(LS_INFO) << "Video suspend state changed to: "
644 << (video_is_suspended ? "suspended" : "not suspended"); 660 << (video_is_suspended ? "suspended" : "not suspended");
645 stats_proxy_->OnSuspendChange(video_is_suspended); 661 stats_proxy_->OnSuspendChange(video_is_suspended);
646 } 662 }
647 } 663 }
648 664
649 void ViEEncoder::OveruseDetected() { 665 void ViEEncoder::ScaleDown(ScaleReason reason) {
650 RTC_DCHECK_RUN_ON(&encoder_queue_); 666 RTC_DCHECK_RUN_ON(&encoder_queue_);
651 if (degradation_preference_ == 667 if (!scaling_enabled_)
652 VideoSendStream::DegradationPreference::kMaintainResolution ||
653 cpu_restricted_counter_ >= kMaxCpuDowngrades) {
654 return; 668 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 669 // Request lower resolution if the current resolution is lower than last time
658 // we asked for the resolution to be lowered. 670 // we asked for the resolution to be lowered.
659 // Update stats accordingly.
660 int current_pixel_count = last_frame_height_ * last_frame_width_; 671 int current_pixel_count = last_frame_height_ * last_frame_width_;
661 if (!max_pixel_count_ || current_pixel_count < *max_pixel_count_) { 672 if (max_pixel_count_ && current_pixel_count >= *max_pixel_count_)
662 max_pixel_count_ = rtc::Optional<int>(current_pixel_count); 673 return;
663 max_pixel_count_step_up_ = rtc::Optional<int>(); 674 switch (reason) {
664 stats_proxy_->OnCpuRestrictedResolutionChanged(true); 675 case kQuality:
665 ++cpu_restricted_counter_; 676 if (scale_counter_[reason] >= kMaxQualityDowngrades)
666 source_proxy_->RequestResolutionLowerThan(current_pixel_count); 677 return;
678 stats_proxy_->OnQualityRestrictedResolutionChanged(true);
679 break;
680 case kCpu:
681 if (scale_counter_[reason] >= kMaxCpuDowngrades)
682 return;
683 // Update stats accordingly.
684 stats_proxy_->OnCpuRestrictedResolutionChanged(true);
685 break;
686 }
687 max_pixel_count_ = rtc::Optional<int>(current_pixel_count);
688 max_pixel_count_step_up_ = rtc::Optional<int>();
689 ++scale_counter_[reason];
690 source_proxy_->RequestResolutionLowerThan(current_pixel_count);
691 LOG(LS_INFO) << "Scaling down resolution.";
692 for (size_t i = 0; i < kScaleReasonSize; ++i) {
693 LOG(LS_INFO) << "Scaled " << scale_counter_[i]
694 << " times for reason: " << (i ? "quality" : "cpu");
667 } 695 }
668 } 696 }
669 697
670 void ViEEncoder::NormalUsage() { 698 void ViEEncoder::ScaleUp(ScaleReason reason) {
671 RTC_DCHECK_RUN_ON(&encoder_queue_); 699 RTC_DCHECK_RUN_ON(&encoder_queue_);
672 if (degradation_preference_ == 700 if (scale_counter_[reason] == 0 || !scaling_enabled_)
673 VideoSendStream::DegradationPreference::kMaintainResolution ||
674 cpu_restricted_counter_ == 0) {
675 return; 701 return;
702 // Only scale if resolution is higher than last time
703 // we requested higher resolution.
704 int current_pixel_count = last_frame_height_ * last_frame_width_;
705 if (current_pixel_count <= max_pixel_count_step_up_.value_or(0))
706 return;
707 switch (reason) {
708 case kQuality:
709 stats_proxy_->OnQualityRestrictedResolutionChanged(
710 scale_counter_[reason] > 1);
711 break;
712 case kCpu:
713 // Update stats accordingly.
714 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counter_[reason] >
715 1);
716 break;
676 } 717 }
677 718 max_pixel_count_ = rtc::Optional<int>();
678 LOG(LS_INFO) << "CPU underuse detected. Requesting higher resolution."; 719 max_pixel_count_step_up_ = rtc::Optional<int>(current_pixel_count);
679 int current_pixel_count = last_frame_height_ * last_frame_width_; 720 --scale_counter_[reason];
680 // Request higher resolution if we are CPU restricted and the the current 721 source_proxy_->RequestHigherResolutionThan(current_pixel_count);
681 // resolution is higher than last time we requested higher resolution. 722 LOG(LS_INFO) << "Scaling up resolution.";
682 // Update stats accordingly. 723 for (size_t i = 0; i < kScaleReasonSize; ++i) {
683 if (!max_pixel_count_step_up_ || 724 LOG(LS_INFO) << "Scaled " << scale_counter_[i]
684 current_pixel_count > *max_pixel_count_step_up_) { 725 << " 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 } 726 }
691 } 727 }
692 728
693 } // namespace webrtc 729 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698