| 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 | 
| 11 #include "webrtc/video/vie_encoder.h" | 11 #include "webrtc/video/vie_encoder.h" | 
| 12 | 12 | 
| 13 #include <algorithm> | 13 #include <algorithm> | 
| 14 #include <limits> | 14 #include <limits> | 
| 15 #include <utility> | 15 #include <utility> | 
| 16 | 16 | 
| 17 #include "webrtc/modules/video_coding/include/video_codec_initializer.h" | 17 #include "webrtc/modules/video_coding/include/video_codec_initializer.h" | 
| 18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" | 
| 19 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" | 
| 20 #include "webrtc/base/trace_event.h" | 20 #include "webrtc/base/trace_event.h" | 
| 21 #include "webrtc/base/timeutils.h" | 21 #include "webrtc/base/timeutils.h" | 
|  | 22 #include "webrtc/common_video/include/video_bitrate_allocator.h" | 
| 22 #include "webrtc/modules/pacing/paced_sender.h" | 23 #include "webrtc/modules/pacing/paced_sender.h" | 
| 23 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" | 24 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" | 
| 24 #include "webrtc/modules/video_coding/include/video_coding.h" | 25 #include "webrtc/modules/video_coding/include/video_coding.h" | 
| 25 #include "webrtc/modules/video_coding/include/video_coding_defines.h" | 26 #include "webrtc/modules/video_coding/include/video_coding_defines.h" | 
| 26 #include "webrtc/video/overuse_frame_detector.h" | 27 #include "webrtc/video/overuse_frame_detector.h" | 
| 27 #include "webrtc/video/send_statistics_proxy.h" | 28 #include "webrtc/video/send_statistics_proxy.h" | 
| 28 #include "webrtc/video_frame.h" | 29 #include "webrtc/video_frame.h" | 
| 29 |  | 
| 30 namespace webrtc { | 30 namespace webrtc { | 
| 31 | 31 | 
| 32 namespace { | 32 namespace { | 
| 33 // Time interval for logging frame counts. | 33 // Time interval for logging frame counts. | 
| 34 const int64_t kFrameLogIntervalMs = 60000; | 34 const int64_t kFrameLogIntervalMs = 60000; | 
| 35 // We will never ask for a resolution lower than this. | 35 // We will never ask for a resolution lower than this. | 
| 36 const int kMinPixelsPerFrame = 120 * 90; | 36 const int kMinPixelsPerFrame = 120 * 90; | 
| 37 | 37 | 
| 38 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | 38 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | 
| 39 // pipelining encoders better (multiple input frames before something comes | 39 // pipelining encoders better (multiple input frames before something comes | 
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 263       picture_id_rpsi_(0), | 263       picture_id_rpsi_(0), | 
| 264       clock_(Clock::GetRealTimeClock()), | 264       clock_(Clock::GetRealTimeClock()), | 
| 265       last_frame_width_(0), | 265       last_frame_width_(0), | 
| 266       last_frame_height_(0), | 266       last_frame_height_(0), | 
| 267       last_captured_timestamp_(0), | 267       last_captured_timestamp_(0), | 
| 268       delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 268       delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 
| 269                              clock_->TimeInMilliseconds()), | 269                              clock_->TimeInMilliseconds()), | 
| 270       last_frame_log_ms_(clock_->TimeInMilliseconds()), | 270       last_frame_log_ms_(clock_->TimeInMilliseconds()), | 
| 271       captured_frame_count_(0), | 271       captured_frame_count_(0), | 
| 272       dropped_frame_count_(0), | 272       dropped_frame_count_(0), | 
|  | 273       bitrate_observer_(nullptr), | 
| 273       encoder_queue_("EncoderQueue") { | 274       encoder_queue_("EncoderQueue") { | 
| 274   encoder_queue_.PostTask([this] { | 275   encoder_queue_.PostTask([this] { | 
| 275     RTC_DCHECK_RUN_ON(&encoder_queue_); | 276     RTC_DCHECK_RUN_ON(&encoder_queue_); | 
| 276     overuse_detector_.StartCheckForOveruse(); | 277     overuse_detector_.StartCheckForOveruse(); | 
| 277     video_sender_.RegisterExternalEncoder( | 278     video_sender_.RegisterExternalEncoder( | 
| 278         settings_.encoder, settings_.payload_type, settings_.internal_source); | 279         settings_.encoder, settings_.payload_type, settings_.internal_source); | 
| 279   }); | 280   }); | 
| 280 } | 281 } | 
| 281 | 282 | 
| 282 ViEEncoder::~ViEEncoder() { | 283 ViEEncoder::~ViEEncoder() { | 
| 283   RTC_DCHECK_RUN_ON(&thread_checker_); | 284   RTC_DCHECK_RUN_ON(&thread_checker_); | 
| 284   RTC_DCHECK(shutdown_event_.Wait(0)) | 285   RTC_DCHECK(shutdown_event_.Wait(0)) | 
| 285       << "Must call ::Stop() before destruction."; | 286       << "Must call ::Stop() before destruction."; | 
| 286 } | 287 } | 
| 287 | 288 | 
| 288 void ViEEncoder::Stop() { | 289 void ViEEncoder::Stop() { | 
| 289   RTC_DCHECK_RUN_ON(&thread_checker_); | 290   RTC_DCHECK_RUN_ON(&thread_checker_); | 
| 290   source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); | 291   source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); | 
| 291   encoder_queue_.PostTask([this] { | 292   encoder_queue_.PostTask([this] { | 
| 292     RTC_DCHECK_RUN_ON(&encoder_queue_); | 293     RTC_DCHECK_RUN_ON(&encoder_queue_); | 
| 293     overuse_detector_.StopCheckForOveruse(); | 294     overuse_detector_.StopCheckForOveruse(); | 
| 294     rate_allocator_.reset(); | 295     rate_allocator_.reset(); | 
|  | 296     bitrate_observer_ = nullptr; | 
| 295     video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 297     video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 
| 296                                           false); | 298                                           false); | 
| 297     quality_scaler_ = nullptr; | 299     quality_scaler_ = nullptr; | 
| 298     shutdown_event_.Set(); | 300     shutdown_event_.Set(); | 
| 299   }); | 301   }); | 
| 300 | 302 | 
| 301   shutdown_event_.Wait(rtc::Event::kForever); | 303   shutdown_event_.Wait(rtc::Event::kForever); | 
| 302 } | 304 } | 
| 303 | 305 | 
| 304 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 306 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 
| 305   RTC_DCHECK_RUN_ON(&thread_checker_); | 307   RTC_DCHECK_RUN_ON(&thread_checker_); | 
| 306   RTC_DCHECK(!module_process_thread_); | 308   RTC_DCHECK(!module_process_thread_); | 
| 307   module_process_thread_ = module_process_thread; | 309   module_process_thread_ = module_process_thread; | 
| 308   module_process_thread_->RegisterModule(&video_sender_); | 310   module_process_thread_->RegisterModule(&video_sender_); | 
| 309   module_process_thread_checker_.DetachFromThread(); | 311   module_process_thread_checker_.DetachFromThread(); | 
| 310 } | 312 } | 
| 311 | 313 | 
| 312 void ViEEncoder::DeRegisterProcessThread() { | 314 void ViEEncoder::DeRegisterProcessThread() { | 
| 313   RTC_DCHECK_RUN_ON(&thread_checker_); | 315   RTC_DCHECK_RUN_ON(&thread_checker_); | 
| 314   module_process_thread_->DeRegisterModule(&video_sender_); | 316   module_process_thread_->DeRegisterModule(&video_sender_); | 
| 315 } | 317 } | 
| 316 | 318 | 
|  | 319 void ViEEncoder::SetBitrateObserver( | 
|  | 320     VideoBitrateAllocationObserver* bitrate_observer) { | 
|  | 321   RTC_DCHECK_RUN_ON(&thread_checker_); | 
|  | 322   encoder_queue_.PostTask([this, bitrate_observer] { | 
|  | 323     RTC_DCHECK_RUN_ON(&encoder_queue_); | 
|  | 324     RTC_DCHECK(!bitrate_observer_); | 
|  | 325     bitrate_observer_ = bitrate_observer; | 
|  | 326   }); | 
|  | 327 } | 
|  | 328 | 
| 317 void ViEEncoder::SetSource( | 329 void ViEEncoder::SetSource( | 
| 318     rtc::VideoSourceInterface<VideoFrame>* source, | 330     rtc::VideoSourceInterface<VideoFrame>* source, | 
| 319     const VideoSendStream::DegradationPreference& degradation_preference) { | 331     const VideoSendStream::DegradationPreference& degradation_preference) { | 
| 320   RTC_DCHECK_RUN_ON(&thread_checker_); | 332   RTC_DCHECK_RUN_ON(&thread_checker_); | 
| 321   source_proxy_->SetSource(source, degradation_preference); | 333   source_proxy_->SetSource(source, degradation_preference); | 
| 322   encoder_queue_.PostTask([this, degradation_preference] { | 334   encoder_queue_.PostTask([this, degradation_preference] { | 
| 323     RTC_DCHECK_RUN_ON(&encoder_queue_); | 335     RTC_DCHECK_RUN_ON(&encoder_queue_); | 
| 324     scaling_enabled_ = | 336     scaling_enabled_ = | 
| 325         (degradation_preference != | 337         (degradation_preference != | 
| 326          VideoSendStream::DegradationPreference::kMaintainResolution); | 338          VideoSendStream::DegradationPreference::kMaintainResolution); | 
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 398   codec.expect_encode_from_texture = last_frame_info_->is_texture; | 410   codec.expect_encode_from_texture = last_frame_info_->is_texture; | 
| 399 | 411 | 
| 400   bool success = video_sender_.RegisterSendCodec( | 412   bool success = video_sender_.RegisterSendCodec( | 
| 401                      &codec, number_of_cores_, | 413                      &codec, number_of_cores_, | 
| 402                      static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK; | 414                      static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK; | 
| 403   if (!success) { | 415   if (!success) { | 
| 404     LOG(LS_ERROR) << "Failed to configure encoder."; | 416     LOG(LS_ERROR) << "Failed to configure encoder."; | 
| 405     RTC_DCHECK(success); | 417     RTC_DCHECK(success); | 
| 406   } | 418   } | 
| 407 | 419 | 
| 408   video_sender_.UpdateChannelParemeters(rate_allocator_.get()); | 420   video_sender_.UpdateChannelParemeters(rate_allocator_.get(), | 
|  | 421                                         bitrate_observer_); | 
|  | 422 | 
| 409   if (stats_proxy_) { | 423   if (stats_proxy_) { | 
| 410     int framerate = stats_proxy_->GetSendFrameRate(); | 424     int framerate = stats_proxy_->GetSendFrameRate(); | 
| 411     if (framerate == 0) | 425     if (framerate == 0) | 
| 412       framerate = codec.maxFramerate; | 426       framerate = codec.maxFramerate; | 
| 413     stats_proxy_->OnEncoderReconfigured( | 427     stats_proxy_->OnEncoderReconfigured( | 
| 414         encoder_config_, rate_allocator_->GetPreferredBitrateBps(framerate)); | 428         encoder_config_, rate_allocator_->GetPreferredBitrateBps(framerate)); | 
| 415   } | 429   } | 
| 416 | 430 | 
| 417   pending_encoder_reconfiguration_ = false; | 431   pending_encoder_reconfiguration_ = false; | 
| 418 | 432 | 
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 653     return; | 667     return; | 
| 654   } | 668   } | 
| 655   RTC_DCHECK_RUN_ON(&encoder_queue_); | 669   RTC_DCHECK_RUN_ON(&encoder_queue_); | 
| 656   RTC_DCHECK(sink_) << "sink_ must be set before the encoder is active."; | 670   RTC_DCHECK(sink_) << "sink_ must be set before the encoder is active."; | 
| 657 | 671 | 
| 658   LOG(LS_VERBOSE) << "OnBitrateUpdated, bitrate " << bitrate_bps | 672   LOG(LS_VERBOSE) << "OnBitrateUpdated, bitrate " << bitrate_bps | 
| 659                   << " packet loss " << static_cast<int>(fraction_lost) | 673                   << " packet loss " << static_cast<int>(fraction_lost) | 
| 660                   << " rtt " << round_trip_time_ms; | 674                   << " rtt " << round_trip_time_ms; | 
| 661 | 675 | 
| 662   video_sender_.SetChannelParameters(bitrate_bps, fraction_lost, | 676   video_sender_.SetChannelParameters(bitrate_bps, fraction_lost, | 
| 663                                      round_trip_time_ms, rate_allocator_.get()); | 677                                      round_trip_time_ms, rate_allocator_.get(), | 
|  | 678                                      bitrate_observer_); | 
| 664 | 679 | 
| 665   encoder_start_bitrate_bps_ = | 680   encoder_start_bitrate_bps_ = | 
| 666       bitrate_bps != 0 ? bitrate_bps : encoder_start_bitrate_bps_; | 681       bitrate_bps != 0 ? bitrate_bps : encoder_start_bitrate_bps_; | 
| 667   bool video_is_suspended = bitrate_bps == 0; | 682   bool video_is_suspended = bitrate_bps == 0; | 
| 668   bool video_suspension_changed = video_is_suspended != EncoderPaused(); | 683   bool video_suspension_changed = video_is_suspended != EncoderPaused(); | 
| 669   last_observed_bitrate_bps_ = bitrate_bps; | 684   last_observed_bitrate_bps_ = bitrate_bps; | 
| 670 | 685 | 
| 671   if (stats_proxy_ && video_suspension_changed) { | 686   if (stats_proxy_ && video_suspension_changed) { | 
| 672     LOG(LS_INFO) << "Video suspend state changed to: " | 687     LOG(LS_INFO) << "Video suspend state changed to: " | 
| 673                  << (video_is_suspended ? "suspended" : "not suspended"); | 688                  << (video_is_suspended ? "suspended" : "not suspended"); | 
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 731   --scale_counter_[reason]; | 746   --scale_counter_[reason]; | 
| 732   source_proxy_->RequestHigherResolutionThan(current_pixel_count); | 747   source_proxy_->RequestHigherResolutionThan(current_pixel_count); | 
| 733   LOG(LS_INFO) << "Scaling up resolution."; | 748   LOG(LS_INFO) << "Scaling up resolution."; | 
| 734   for (size_t i = 0; i < kScaleReasonSize; ++i) { | 749   for (size_t i = 0; i < kScaleReasonSize; ++i) { | 
| 735     LOG(LS_INFO) << "Scaled " << scale_counter_[i] | 750     LOG(LS_INFO) << "Scaled " << scale_counter_[i] | 
| 736                  << " times for reason: " << (i ? "quality" : "cpu"); | 751                  << " times for reason: " << (i ? "quality" : "cpu"); | 
| 737   } | 752   } | 
| 738 } | 753 } | 
| 739 | 754 | 
| 740 }  // namespace webrtc | 755 }  // namespace webrtc | 
| OLD | NEW | 
|---|