You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
7.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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/critical_section.h"
  36. #include "rtc_base/experiments/field_trial_parser.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/call_stats.h"
  42. #include "video/encoder_rtcp_feedback.h"
  43. #include "video/send_delay_stats.h"
  44. #include "video/send_statistics_proxy.h"
  45. #include "video/video_send_stream.h"
  46. namespace webrtc {
  47. namespace internal {
  48. // Pacing buffer config; overridden by ALR config if provided.
  49. struct PacingConfig {
  50. PacingConfig();
  51. PacingConfig(const PacingConfig&);
  52. PacingConfig& operator=(const PacingConfig&) = default;
  53. ~PacingConfig();
  54. FieldTrialParameter<double> pacing_factor;
  55. FieldTrialParameter<TimeDelta> max_pacing_delay;
  56. };
  57. // VideoSendStreamImpl implements internal::VideoSendStream.
  58. // It is created and destroyed on |worker_queue|. The intent is to decrease the
  59. // need for locking and to ensure methods are called in sequence.
  60. // Public methods except |DeliverRtcp| must be called on |worker_queue|.
  61. // DeliverRtcp is called on the libjingle worker thread or a network thread.
  62. // An encoder may deliver frames through the EncodedImageCallback on an
  63. // arbitrary thread.
  64. class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver,
  65. public VideoStreamEncoderInterface::EncoderSink,
  66. public VideoBitrateAllocationObserver {
  67. public:
  68. VideoSendStreamImpl(
  69. Clock* clock,
  70. SendStatisticsProxy* stats_proxy,
  71. rtc::TaskQueue* worker_queue,
  72. CallStats* call_stats,
  73. RtpTransportControllerSendInterface* transport,
  74. BitrateAllocatorInterface* bitrate_allocator,
  75. SendDelayStats* send_delay_stats,
  76. VideoStreamEncoderInterface* video_stream_encoder,
  77. RtcEventLog* event_log,
  78. const VideoSendStream::Config* config,
  79. int initial_encoder_max_bitrate,
  80. double initial_encoder_bitrate_priority,
  81. std::map<uint32_t, RtpState> suspended_ssrcs,
  82. std::map<uint32_t, RtpPayloadState> suspended_payload_states,
  83. VideoEncoderConfig::ContentType content_type,
  84. std::unique_ptr<FecController> fec_controller,
  85. MediaTransportInterface* media_transport);
  86. ~VideoSendStreamImpl() override;
  87. // RegisterProcessThread register |module_process_thread| with those objects
  88. // that use it. Registration has to happen on the thread were
  89. // |module_process_thread| was created (libjingle's worker thread).
  90. // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue,
  91. // maybe |worker_queue|.
  92. void RegisterProcessThread(ProcessThread* module_process_thread);
  93. void DeRegisterProcessThread();
  94. void DeliverRtcp(const uint8_t* packet, size_t length);
  95. void UpdateActiveSimulcastLayers(const std::vector<bool> active_layers);
  96. void Start();
  97. void Stop();
  98. // TODO(holmer): Move these to RtpTransportControllerSend.
  99. std::map<uint32_t, RtpState> GetRtpStates() const;
  100. std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const;
  101. absl::optional<float> configured_pacing_factor_;
  102. private:
  103. // Implements BitrateAllocatorObserver.
  104. uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
  105. void OnEncoderConfigurationChanged(
  106. std::vector<VideoStream> streams,
  107. VideoEncoderConfig::ContentType content_type,
  108. int min_transmit_bitrate_bps) override;
  109. // Implements EncodedImageCallback. The implementation routes encoded frames
  110. // to the |payload_router_| and |config.pre_encode_callback| if set.
  111. // Called on an arbitrary encoder callback thread.
  112. EncodedImageCallback::Result OnEncodedImage(
  113. const EncodedImage& encoded_image,
  114. const CodecSpecificInfo* codec_specific_info,
  115. const RTPFragmentationHeader* fragmentation) 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. CallStats* const call_stats_;
  141. RtpTransportControllerSendInterface* const transport_;
  142. BitrateAllocatorInterface* const bitrate_allocator_;
  143. rtc::CriticalSection ivf_writers_crit_;
  144. bool disable_padding_;
  145. int max_padding_bitrate_;
  146. int encoder_min_bitrate_bps_;
  147. uint32_t encoder_max_bitrate_bps_;
  148. uint32_t encoder_target_rate_bps_;
  149. double encoder_bitrate_priority_;
  150. bool has_packet_feedback_;
  151. VideoStreamEncoderInterface* const video_stream_encoder_;
  152. EncoderRtcpFeedback encoder_feedback_;
  153. RtcpBandwidthObserver* const bandwidth_observer_;
  154. RtpVideoSenderInterface* const rtp_video_sender_;
  155. // |weak_ptr_| to our self. This is used since we can not call
  156. // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy
  157. // an existing WeakPtr.
  158. rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_;
  159. // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are
  160. // invalidated before any other members are destroyed.
  161. rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_;
  162. // Context for the most recent and last sent video bitrate allocation. Used to
  163. // throttle sending of similar bitrate allocations.
  164. struct VbaSendContext {
  165. VideoBitrateAllocation last_sent_allocation;
  166. absl::optional<VideoBitrateAllocation> throttled_allocation;
  167. int64_t last_send_time_ms;
  168. };
  169. absl::optional<VbaSendContext> video_bitrate_allocation_context_
  170. RTC_GUARDED_BY(worker_queue_);
  171. MediaTransportInterface* const media_transport_;
  172. rtc::CriticalSection media_transport_id_lock_;
  173. int64_t media_transport_frame_id_ RTC_GUARDED_BY(media_transport_id_lock_) =
  174. 0;
  175. };
  176. } // namespace internal
  177. } // namespace webrtc
  178. #endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_