video_send_stream_impl.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2018 The WebRTC project authors. All Rights Reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VIDEO_VIDEO_SEND_STREAM_IMPL_H_
  11. #define VIDEO_VIDEO_SEND_STREAM_IMPL_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <atomic>
  15. #include <map>
  16. #include <memory>
  17. #include <vector>
  18. #include "absl/types/optional.h"
  19. #include "api/fec_controller.h"
  20. #include "api/rtc_event_log/rtc_event_log.h"
  21. #include "api/video/encoded_image.h"
  22. #include "api/video/video_bitrate_allocation.h"
  23. #include "api/video/video_bitrate_allocator.h"
  24. #include "api/video/video_stream_encoder_interface.h"
  25. #include "api/video_codecs/video_encoder.h"
  26. #include "api/video_codecs/video_encoder_config.h"
  27. #include "call/bitrate_allocator.h"
  28. #include "call/rtp_config.h"
  29. #include "call/rtp_transport_controller_send_interface.h"
  30. #include "call/rtp_video_sender_interface.h"
  31. #include "modules/include/module_common_types.h"
  32. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  33. #include "modules/utility/include/process_thread.h"
  34. #include "modules/video_coding/include/video_codec_interface.h"
  35. #include "rtc_base/experiments/field_trial_parser.h"
  36. #include "rtc_base/synchronization/mutex.h"
  37. #include "rtc_base/task_queue.h"
  38. #include "rtc_base/task_utils/repeating_task.h"
  39. #include "rtc_base/thread_annotations.h"
  40. #include "rtc_base/weak_ptr.h"
  41. #include "video/encoder_rtcp_feedback.h"
  42. #include "video/send_delay_stats.h"
  43. #include "video/send_statistics_proxy.h"
  44. #include "video/video_send_stream.h"
  45. namespace webrtc {
  46. namespace internal {
  47. // Pacing buffer config; overridden by ALR config if provided.
  48. struct PacingConfig {
  49. PacingConfig();
  50. PacingConfig(const PacingConfig&);
  51. PacingConfig& operator=(const PacingConfig&) = default;
  52. ~PacingConfig();
  53. FieldTrialParameter<double> pacing_factor;
  54. FieldTrialParameter<TimeDelta> max_pacing_delay;
  55. };
  56. // VideoSendStreamImpl implements internal::VideoSendStream.
  57. // It is created and destroyed on |worker_queue|. The intent is to decrease the
  58. // need for locking and to ensure methods are called in sequence.
  59. // Public methods except |DeliverRtcp| must be called on |worker_queue|.
  60. // DeliverRtcp is called on the libjingle worker thread or a network thread.
  61. // An encoder may deliver frames through the EncodedImageCallback on an
  62. // arbitrary thread.
  63. class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver,
  64. public VideoStreamEncoderInterface::EncoderSink,
  65. public VideoBitrateAllocationObserver {
  66. public:
  67. VideoSendStreamImpl(
  68. Clock* clock,
  69. SendStatisticsProxy* stats_proxy,
  70. rtc::TaskQueue* worker_queue,
  71. RtcpRttStats* call_stats,
  72. RtpTransportControllerSendInterface* transport,
  73. BitrateAllocatorInterface* bitrate_allocator,
  74. SendDelayStats* send_delay_stats,
  75. VideoStreamEncoderInterface* video_stream_encoder,
  76. RtcEventLog* event_log,
  77. const VideoSendStream::Config* config,
  78. int initial_encoder_max_bitrate,
  79. double initial_encoder_bitrate_priority,
  80. std::map<uint32_t, RtpState> suspended_ssrcs,
  81. std::map<uint32_t, RtpPayloadState> suspended_payload_states,
  82. VideoEncoderConfig::ContentType content_type,
  83. std::unique_ptr<FecController> fec_controller);
  84. ~VideoSendStreamImpl() override;
  85. // RegisterProcessThread register |module_process_thread| with those objects
  86. // that use it. Registration has to happen on the thread were
  87. // |module_process_thread| was created (libjingle's worker thread).
  88. // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue,
  89. // maybe |worker_queue|.
  90. void RegisterProcessThread(ProcessThread* module_process_thread);
  91. void DeRegisterProcessThread();
  92. void DeliverRtcp(const uint8_t* packet, size_t length);
  93. void UpdateActiveSimulcastLayers(const std::vector<bool> active_layers);
  94. void Start();
  95. void Stop();
  96. // TODO(holmer): Move these to RtpTransportControllerSend.
  97. std::map<uint32_t, RtpState> GetRtpStates() const;
  98. std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const;
  99. absl::optional<float> configured_pacing_factor_;
  100. private:
  101. // Implements BitrateAllocatorObserver.
  102. uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
  103. void OnEncoderConfigurationChanged(
  104. std::vector<VideoStream> streams,
  105. bool is_svc,
  106. VideoEncoderConfig::ContentType content_type,
  107. int min_transmit_bitrate_bps) override;
  108. // Implements EncodedImageCallback. The implementation routes encoded frames
  109. // to the |payload_router_| and |config.pre_encode_callback| if set.
  110. // Called on an arbitrary encoder callback thread.
  111. EncodedImageCallback::Result OnEncodedImage(
  112. const EncodedImage& encoded_image,
  113. const CodecSpecificInfo* codec_specific_info) override;
  114. // Implements EncodedImageCallback.
  115. void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
  116. // Implements VideoBitrateAllocationObserver.
  117. void OnBitrateAllocationUpdated(
  118. const VideoBitrateAllocation& allocation) override;
  119. // Starts monitoring and sends a keyframe.
  120. void StartupVideoSendStream();
  121. // Removes the bitrate observer, stops monitoring and notifies the video
  122. // encoder of the bitrate update.
  123. void StopVideoSendStream() RTC_RUN_ON(worker_queue_);
  124. void ConfigureProtection();
  125. void ConfigureSsrcs();
  126. void SignalEncoderTimedOut();
  127. void SignalEncoderActive();
  128. MediaStreamAllocationConfig GetAllocationConfig() const
  129. RTC_RUN_ON(worker_queue_);
  130. Clock* const clock_;
  131. const bool has_alr_probing_;
  132. const PacingConfig pacing_config_;
  133. SendStatisticsProxy* const stats_proxy_;
  134. const VideoSendStream::Config* const config_;
  135. rtc::TaskQueue* const worker_queue_;
  136. RepeatingTaskHandle check_encoder_activity_task_
  137. RTC_GUARDED_BY(worker_queue_);
  138. std::atomic_bool activity_;
  139. bool timed_out_ RTC_GUARDED_BY(worker_queue_);
  140. RtpTransportControllerSendInterface* const transport_;
  141. BitrateAllocatorInterface* const bitrate_allocator_;
  142. Mutex ivf_writers_mutex_;
  143. bool disable_padding_;
  144. int max_padding_bitrate_;
  145. int encoder_min_bitrate_bps_;
  146. uint32_t encoder_max_bitrate_bps_;
  147. uint32_t encoder_target_rate_bps_;
  148. double encoder_bitrate_priority_;
  149. bool has_packet_feedback_;
  150. VideoStreamEncoderInterface* const video_stream_encoder_;
  151. EncoderRtcpFeedback encoder_feedback_;
  152. RtcpBandwidthObserver* const bandwidth_observer_;
  153. RtpVideoSenderInterface* const rtp_video_sender_;
  154. // |weak_ptr_| to our self. This is used since we can not call
  155. // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy
  156. // an existing WeakPtr.
  157. rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_;
  158. // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are
  159. // invalidated before any other members are destroyed.
  160. rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_;
  161. // Context for the most recent and last sent video bitrate allocation. Used to
  162. // throttle sending of similar bitrate allocations.
  163. struct VbaSendContext {
  164. VideoBitrateAllocation last_sent_allocation;
  165. absl::optional<VideoBitrateAllocation> throttled_allocation;
  166. int64_t last_send_time_ms;
  167. };
  168. absl::optional<VbaSendContext> video_bitrate_allocation_context_
  169. RTC_GUARDED_BY(worker_queue_);
  170. };
  171. } // namespace internal
  172. } // namespace webrtc
  173. #endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_