| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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 #include "webrtc/video/video_send_stream.h" | 10 #include "webrtc/video/video_send_stream.h" |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <sstream> | 13 #include <sstream> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <utility> | 15 #include <utility> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "webrtc/common_types.h" | 18 #include "webrtc/common_types.h" |
| 19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/file.h" | 20 #include "webrtc/base/file.h" |
| 21 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
| 22 #include "webrtc/base/trace_event.h" | 22 #include "webrtc/base/trace_event.h" |
| 23 #include "webrtc/base/weak_ptr.h" |
| 23 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 24 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
| 24 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 25 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
| 25 #include "webrtc/modules/pacing/packet_router.h" | 26 #include "webrtc/modules/pacing/packet_router.h" |
| 26 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 27 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 27 #include "webrtc/modules/utility/include/process_thread.h" | 28 #include "webrtc/modules/utility/include/process_thread.h" |
| 28 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" | 29 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" |
| 29 #include "webrtc/video/call_stats.h" | 30 #include "webrtc/video/call_stats.h" |
| 30 #include "webrtc/video/vie_remb.h" | 31 #include "webrtc/video/vie_remb.h" |
| 31 #include "webrtc/video_send_stream.h" | 32 #include "webrtc/video_send_stream.h" |
| 32 | 33 |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 uint32_t encoder_target_rate_bps_; | 333 uint32_t encoder_target_rate_bps_; |
| 333 | 334 |
| 334 ViEEncoder* const vie_encoder_; | 335 ViEEncoder* const vie_encoder_; |
| 335 EncoderStateFeedback encoder_feedback_; | 336 EncoderStateFeedback encoder_feedback_; |
| 336 ProtectionBitrateCalculator protection_bitrate_calculator_; | 337 ProtectionBitrateCalculator protection_bitrate_calculator_; |
| 337 | 338 |
| 338 const std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_; | 339 const std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_; |
| 339 // RtpRtcp modules, declared here as they use other members on construction. | 340 // RtpRtcp modules, declared here as they use other members on construction. |
| 340 const std::vector<RtpRtcp*> rtp_rtcp_modules_; | 341 const std::vector<RtpRtcp*> rtp_rtcp_modules_; |
| 341 PayloadRouter payload_router_; | 342 PayloadRouter payload_router_; |
| 343 |
| 344 // |weak_ptr_| to our self. This is used since we can not call |
| 345 // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy |
| 346 // an existing WeakPtr. |
| 347 rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_; |
| 348 // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are |
| 349 // invalidated before any other members are destroyed. |
| 350 rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_; |
| 342 }; | 351 }; |
| 343 | 352 |
| 344 // TODO(tommi): See if there's a more elegant way to create a task that creates | 353 // TODO(tommi): See if there's a more elegant way to create a task that creates |
| 345 // an object on the correct task queue. | 354 // an object on the correct task queue. |
| 346 class VideoSendStream::ConstructionTask : public rtc::QueuedTask { | 355 class VideoSendStream::ConstructionTask : public rtc::QueuedTask { |
| 347 public: | 356 public: |
| 348 ConstructionTask(std::unique_ptr<VideoSendStreamImpl>* send_stream, | 357 ConstructionTask(std::unique_ptr<VideoSendStreamImpl>* send_stream, |
| 349 rtc::Event* done_event, | 358 rtc::Event* done_event, |
| 350 SendStatisticsProxy* stats_proxy, | 359 SendStatisticsProxy* stats_proxy, |
| 351 ViEEncoder* vie_encoder, | 360 ViEEncoder* vie_encoder, |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 std::unique_ptr<VideoSendStreamImpl> send_stream_; | 433 std::unique_ptr<VideoSendStreamImpl> send_stream_; |
| 425 rtc::Event* done_event_; | 434 rtc::Event* done_event_; |
| 426 }; | 435 }; |
| 427 | 436 |
| 428 // CheckEncoderActivityTask is used for tracking when the encoder last produced | 437 // CheckEncoderActivityTask is used for tracking when the encoder last produced |
| 429 // and encoded video frame. If the encoder has not produced anything the last | 438 // and encoded video frame. If the encoder has not produced anything the last |
| 430 // kEncoderTimeOutMs we also want to stop sending padding. | 439 // kEncoderTimeOutMs we also want to stop sending padding. |
| 431 class VideoSendStreamImpl::CheckEncoderActivityTask : public rtc::QueuedTask { | 440 class VideoSendStreamImpl::CheckEncoderActivityTask : public rtc::QueuedTask { |
| 432 public: | 441 public: |
| 433 static const int kEncoderTimeOutMs = 2000; | 442 static const int kEncoderTimeOutMs = 2000; |
| 434 explicit CheckEncoderActivityTask(VideoSendStreamImpl* send_stream) | 443 explicit CheckEncoderActivityTask( |
| 435 : activity_(0), send_stream_(send_stream), timed_out_(false) {} | 444 const rtc::WeakPtr<VideoSendStreamImpl>& send_stream) |
| 445 : activity_(0), send_stream_(std::move(send_stream)), timed_out_(false) {} |
| 436 | 446 |
| 437 void Stop() { | 447 void Stop() { |
| 438 RTC_CHECK(task_checker_.CalledSequentially()); | 448 RTC_CHECK(task_checker_.CalledSequentially()); |
| 439 send_stream_ = nullptr; | 449 send_stream_.reset(); |
| 440 } | 450 } |
| 441 | 451 |
| 442 void UpdateEncoderActivity() { | 452 void UpdateEncoderActivity() { |
| 443 // UpdateEncoderActivity is called from VideoSendStreamImpl::Encoded on | 453 // UpdateEncoderActivity is called from VideoSendStreamImpl::Encoded on |
| 444 // whatever thread the real encoder implementation run on. In the case of | 454 // whatever thread the real encoder implementation run on. In the case of |
| 445 // hardware encoders, there might be several encoders | 455 // hardware encoders, there might be several encoders |
| 446 // running in parallel on different threads. | 456 // running in parallel on different threads. |
| 447 rtc::AtomicOps::ReleaseStore(&activity_, 1); | 457 rtc::AtomicOps::ReleaseStore(&activity_, 1); |
| 448 } | 458 } |
| 449 | 459 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 465 | 475 |
| 466 rtc::TaskQueue::Current()->PostDelayedTask( | 476 rtc::TaskQueue::Current()->PostDelayedTask( |
| 467 std::unique_ptr<rtc::QueuedTask>(this), kEncoderTimeOutMs); | 477 std::unique_ptr<rtc::QueuedTask>(this), kEncoderTimeOutMs); |
| 468 // Return false to prevent this task from being deleted. Ownership has been | 478 // Return false to prevent this task from being deleted. Ownership has been |
| 469 // transferred to the task queue when PostDelayedTask was called. | 479 // transferred to the task queue when PostDelayedTask was called. |
| 470 return false; | 480 return false; |
| 471 } | 481 } |
| 472 volatile int activity_; | 482 volatile int activity_; |
| 473 | 483 |
| 474 rtc::SequencedTaskChecker task_checker_; | 484 rtc::SequencedTaskChecker task_checker_; |
| 475 VideoSendStreamImpl* send_stream_; | 485 rtc::WeakPtr<VideoSendStreamImpl> send_stream_; |
| 476 bool timed_out_; | 486 bool timed_out_; |
| 477 }; | 487 }; |
| 478 | 488 |
| 479 class VideoSendStreamImpl::EncoderReconfiguredTask : public rtc::QueuedTask { | 489 class VideoSendStreamImpl::EncoderReconfiguredTask : public rtc::QueuedTask { |
| 480 public: | 490 public: |
| 481 EncoderReconfiguredTask(VideoSendStreamImpl* send_stream, | 491 EncoderReconfiguredTask(const rtc::WeakPtr<VideoSendStreamImpl>& send_stream, |
| 482 std::vector<VideoStream> streams, | 492 std::vector<VideoStream> streams, |
| 483 int min_transmit_bitrate_bps) | 493 int min_transmit_bitrate_bps) |
| 484 : send_stream_(send_stream), | 494 : send_stream_(std::move(send_stream)), |
| 485 streams_(std::move(streams)), | 495 streams_(std::move(streams)), |
| 486 min_transmit_bitrate_bps_(min_transmit_bitrate_bps) {} | 496 min_transmit_bitrate_bps_(min_transmit_bitrate_bps) {} |
| 487 | 497 |
| 488 private: | 498 private: |
| 489 bool Run() override { | 499 bool Run() override { |
| 490 send_stream_->OnEncoderConfigurationChanged(std::move(streams_), | 500 if (send_stream_) |
| 491 min_transmit_bitrate_bps_); | 501 send_stream_->OnEncoderConfigurationChanged(std::move(streams_), |
| 502 min_transmit_bitrate_bps_); |
| 492 return true; | 503 return true; |
| 493 } | 504 } |
| 494 | 505 |
| 495 VideoSendStreamImpl* send_stream_; | 506 rtc::WeakPtr<VideoSendStreamImpl> send_stream_; |
| 496 std::vector<VideoStream> streams_; | 507 std::vector<VideoStream> streams_; |
| 497 int min_transmit_bitrate_bps_; | 508 int min_transmit_bitrate_bps_; |
| 498 }; | 509 }; |
| 499 | 510 |
| 500 VideoSendStream::VideoSendStream( | 511 VideoSendStream::VideoSendStream( |
| 501 int num_cpu_cores, | 512 int num_cpu_cores, |
| 502 ProcessThread* module_process_thread, | 513 ProcessThread* module_process_thread, |
| 503 rtc::TaskQueue* worker_queue, | 514 rtc::TaskQueue* worker_queue, |
| 504 CallStats* call_stats, | 515 CallStats* call_stats, |
| 505 CongestionController* congestion_controller, | 516 CongestionController* congestion_controller, |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 congestion_controller_->GetTransportFeedbackObserver(), | 679 congestion_controller_->GetTransportFeedbackObserver(), |
| 669 call_stats_->rtcp_rtt_stats(), | 680 call_stats_->rtcp_rtt_stats(), |
| 670 congestion_controller_->pacer(), | 681 congestion_controller_->pacer(), |
| 671 congestion_controller_->packet_router(), | 682 congestion_controller_->packet_router(), |
| 672 stats_proxy_, | 683 stats_proxy_, |
| 673 send_delay_stats, | 684 send_delay_stats, |
| 674 event_log, | 685 event_log, |
| 675 congestion_controller_->GetRetransmissionRateLimiter(), | 686 congestion_controller_->GetRetransmissionRateLimiter(), |
| 676 config_->rtp.ssrcs.size())), | 687 config_->rtp.ssrcs.size())), |
| 677 payload_router_(rtp_rtcp_modules_, | 688 payload_router_(rtp_rtcp_modules_, |
| 678 config_->encoder_settings.payload_type) { | 689 config_->encoder_settings.payload_type), |
| 690 weak_ptr_factory_(this) { |
| 679 RTC_DCHECK_RUN_ON(worker_queue_); | 691 RTC_DCHECK_RUN_ON(worker_queue_); |
| 680 LOG(LS_INFO) << "VideoSendStreamInternal: " << config_->ToString(); | 692 LOG(LS_INFO) << "VideoSendStreamInternal: " << config_->ToString(); |
| 693 weak_ptr_ = weak_ptr_factory_.GetWeakPtr(); |
| 681 module_process_thread_checker_.DetachFromThread(); | 694 module_process_thread_checker_.DetachFromThread(); |
| 682 | 695 |
| 683 RTC_DCHECK(!config_->rtp.ssrcs.empty()); | 696 RTC_DCHECK(!config_->rtp.ssrcs.empty()); |
| 684 RTC_DCHECK(call_stats_); | 697 RTC_DCHECK(call_stats_); |
| 685 RTC_DCHECK(congestion_controller_); | 698 RTC_DCHECK(congestion_controller_); |
| 686 RTC_DCHECK(remb_); | 699 RTC_DCHECK(remb_); |
| 687 | 700 |
| 688 // RTP/RTCP initialization. | 701 // RTP/RTCP initialization. |
| 689 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 702 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 690 congestion_controller_->packet_router()->AddRtpModule(rtp_rtcp); | 703 congestion_controller_->packet_router()->AddRtpModule(rtp_rtcp); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 payload_router_.set_active(true); | 794 payload_router_.set_active(true); |
| 782 | 795 |
| 783 bitrate_allocator_->AddObserver( | 796 bitrate_allocator_->AddObserver( |
| 784 this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, | 797 this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, |
| 785 max_padding_bitrate_, !config_->suspend_below_min_bitrate); | 798 max_padding_bitrate_, !config_->suspend_below_min_bitrate); |
| 786 | 799 |
| 787 // Start monitoring encoder activity. | 800 // Start monitoring encoder activity. |
| 788 { | 801 { |
| 789 rtc::CritScope lock(&encoder_activity_crit_sect_); | 802 rtc::CritScope lock(&encoder_activity_crit_sect_); |
| 790 RTC_DCHECK(!check_encoder_activity_task_); | 803 RTC_DCHECK(!check_encoder_activity_task_); |
| 791 check_encoder_activity_task_ = new CheckEncoderActivityTask(this); | 804 check_encoder_activity_task_ = new CheckEncoderActivityTask(weak_ptr_); |
| 792 worker_queue_->PostDelayedTask( | 805 worker_queue_->PostDelayedTask( |
| 793 std::unique_ptr<rtc::QueuedTask>(check_encoder_activity_task_), | 806 std::unique_ptr<rtc::QueuedTask>(check_encoder_activity_task_), |
| 794 CheckEncoderActivityTask::kEncoderTimeOutMs); | 807 CheckEncoderActivityTask::kEncoderTimeOutMs); |
| 795 } | 808 } |
| 796 | 809 |
| 797 vie_encoder_->SendKeyFrame(); | 810 vie_encoder_->SendKeyFrame(); |
| 798 } | 811 } |
| 799 | 812 |
| 800 void VideoSendStreamImpl::Stop() { | 813 void VideoSendStreamImpl::Stop() { |
| 801 RTC_DCHECK_RUN_ON(worker_queue_); | 814 RTC_DCHECK_RUN_ON(worker_queue_); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 830 LOG(LS_INFO) << "SignalEncoderActive, Encoder is active."; | 843 LOG(LS_INFO) << "SignalEncoderActive, Encoder is active."; |
| 831 bitrate_allocator_->AddObserver( | 844 bitrate_allocator_->AddObserver( |
| 832 this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, | 845 this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, |
| 833 max_padding_bitrate_, !config_->suspend_below_min_bitrate); | 846 max_padding_bitrate_, !config_->suspend_below_min_bitrate); |
| 834 } | 847 } |
| 835 | 848 |
| 836 void VideoSendStreamImpl::OnEncoderConfigurationChanged( | 849 void VideoSendStreamImpl::OnEncoderConfigurationChanged( |
| 837 std::vector<VideoStream> streams, | 850 std::vector<VideoStream> streams, |
| 838 int min_transmit_bitrate_bps) { | 851 int min_transmit_bitrate_bps) { |
| 839 if (!worker_queue_->IsCurrent()) { | 852 if (!worker_queue_->IsCurrent()) { |
| 840 // TODO(perkj): Using |this| in post is safe for now since destruction of | |
| 841 // VideoSendStreamImpl is synchronized in | |
| 842 // VideoSendStream::StopPermanentlyAndGetRtpStates. But we should really | |
| 843 // use some kind of weak_ptr to guarantee that VideoSendStreamImpl is still | |
| 844 // alive when this task runs. | |
| 845 worker_queue_->PostTask( | 853 worker_queue_->PostTask( |
| 846 std::unique_ptr<rtc::QueuedTask>(new EncoderReconfiguredTask( | 854 std::unique_ptr<rtc::QueuedTask>(new EncoderReconfiguredTask( |
| 847 this, std::move(streams), min_transmit_bitrate_bps))); | 855 weak_ptr_, std::move(streams), min_transmit_bitrate_bps))); |
| 848 return; | 856 return; |
| 849 } | 857 } |
| 850 RTC_DCHECK_GE(config_->rtp.ssrcs.size(), streams.size()); | 858 RTC_DCHECK_GE(config_->rtp.ssrcs.size(), streams.size()); |
| 851 TRACE_EVENT0("webrtc", "VideoSendStream::OnEncoderConfigurationChanged"); | 859 TRACE_EVENT0("webrtc", "VideoSendStream::OnEncoderConfigurationChanged"); |
| 852 RTC_DCHECK_GE(config_->rtp.ssrcs.size(), streams.size()); | 860 RTC_DCHECK_GE(config_->rtp.ssrcs.size(), streams.size()); |
| 853 RTC_DCHECK_RUN_ON(worker_queue_); | 861 RTC_DCHECK_RUN_ON(worker_queue_); |
| 854 | 862 |
| 855 const int kEncoderMinBitrateBps = 30000; | 863 const int kEncoderMinBitrateBps = 30000; |
| 856 encoder_min_bitrate_bps_ = | 864 encoder_min_bitrate_bps_ = |
| 857 std::max(streams[0].min_bitrate_bps, kEncoderMinBitrateBps); | 865 std::max(streams[0].min_bitrate_bps, kEncoderMinBitrateBps); |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1108 &module_nack_rate); | 1116 &module_nack_rate); |
| 1109 *sent_video_rate_bps += module_video_rate; | 1117 *sent_video_rate_bps += module_video_rate; |
| 1110 *sent_nack_rate_bps += module_nack_rate; | 1118 *sent_nack_rate_bps += module_nack_rate; |
| 1111 *sent_fec_rate_bps += module_fec_rate; | 1119 *sent_fec_rate_bps += module_fec_rate; |
| 1112 } | 1120 } |
| 1113 return 0; | 1121 return 0; |
| 1114 } | 1122 } |
| 1115 | 1123 |
| 1116 } // namespace internal | 1124 } // namespace internal |
| 1117 } // namespace webrtc | 1125 } // namespace webrtc |
| OLD | NEW |