rtp_video_sender.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2015 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 CALL_RTP_VIDEO_SENDER_H_
  11. #define CALL_RTP_VIDEO_SENDER_H_
  12. #include <map>
  13. #include <memory>
  14. #include <unordered_set>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/array_view.h"
  18. #include "api/call/transport.h"
  19. #include "api/fec_controller.h"
  20. #include "api/fec_controller_override.h"
  21. #include "api/rtc_event_log/rtc_event_log.h"
  22. #include "api/transport/field_trial_based_config.h"
  23. #include "api/video_codecs/video_encoder.h"
  24. #include "call/rtp_config.h"
  25. #include "call/rtp_payload_params.h"
  26. #include "call/rtp_transport_controller_send_interface.h"
  27. #include "call/rtp_video_sender_interface.h"
  28. #include "modules/rtp_rtcp/include/flexfec_sender.h"
  29. #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h"
  30. #include "modules/rtp_rtcp/source/rtp_sender.h"
  31. #include "modules/rtp_rtcp/source/rtp_sender_video.h"
  32. #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
  33. #include "modules/rtp_rtcp/source/rtp_video_header.h"
  34. #include "modules/utility/include/process_thread.h"
  35. #include "rtc_base/constructor_magic.h"
  36. #include "rtc_base/rate_limiter.h"
  37. #include "rtc_base/synchronization/mutex.h"
  38. #include "rtc_base/thread_annotations.h"
  39. #include "rtc_base/thread_checker.h"
  40. namespace webrtc {
  41. class FrameEncryptorInterface;
  42. class RtpTransportControllerSendInterface;
  43. namespace webrtc_internal_rtp_video_sender {
  44. // RTP state for a single simulcast stream. Internal to the implementation of
  45. // RtpVideoSender.
  46. struct RtpStreamSender {
  47. RtpStreamSender(std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp,
  48. std::unique_ptr<RTPSenderVideo> sender_video,
  49. std::unique_ptr<VideoFecGenerator> fec_generator);
  50. ~RtpStreamSender();
  51. RtpStreamSender(RtpStreamSender&&) = default;
  52. RtpStreamSender& operator=(RtpStreamSender&&) = default;
  53. // Note: Needs pointer stability.
  54. std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp;
  55. std::unique_ptr<RTPSenderVideo> sender_video;
  56. std::unique_ptr<VideoFecGenerator> fec_generator;
  57. };
  58. } // namespace webrtc_internal_rtp_video_sender
  59. // RtpVideoSender routes outgoing data to the correct sending RTP module, based
  60. // on the simulcast layer in RTPVideoHeader.
  61. class RtpVideoSender : public RtpVideoSenderInterface,
  62. public VCMProtectionCallback,
  63. public StreamFeedbackObserver {
  64. public:
  65. // Rtp modules are assumed to be sorted in simulcast index order.
  66. RtpVideoSender(
  67. Clock* clock,
  68. std::map<uint32_t, RtpState> suspended_ssrcs,
  69. const std::map<uint32_t, RtpPayloadState>& states,
  70. const RtpConfig& rtp_config,
  71. int rtcp_report_interval_ms,
  72. Transport* send_transport,
  73. const RtpSenderObservers& observers,
  74. RtpTransportControllerSendInterface* transport,
  75. RtcEventLog* event_log,
  76. RateLimiter* retransmission_limiter, // move inside RtpTransport
  77. std::unique_ptr<FecController> fec_controller,
  78. FrameEncryptorInterface* frame_encryptor,
  79. const CryptoOptions& crypto_options, // move inside RtpTransport
  80. rtc::scoped_refptr<FrameTransformerInterface> frame_transformer);
  81. ~RtpVideoSender() override;
  82. // RegisterProcessThread register |module_process_thread| with those objects
  83. // that use it. Registration has to happen on the thread were
  84. // |module_process_thread| was created (libjingle's worker thread).
  85. // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue,
  86. // maybe |worker_queue|.
  87. void RegisterProcessThread(ProcessThread* module_process_thread)
  88. RTC_LOCKS_EXCLUDED(mutex_) override;
  89. void DeRegisterProcessThread() RTC_LOCKS_EXCLUDED(mutex_) override;
  90. // RtpVideoSender will only route packets if being active, all packets will be
  91. // dropped otherwise.
  92. void SetActive(bool active) RTC_LOCKS_EXCLUDED(mutex_) override;
  93. // Sets the sending status of the rtp modules and appropriately sets the
  94. // payload router to active if any rtp modules are active.
  95. void SetActiveModules(const std::vector<bool> active_modules)
  96. RTC_LOCKS_EXCLUDED(mutex_) override;
  97. bool IsActive() RTC_LOCKS_EXCLUDED(mutex_) override;
  98. void OnNetworkAvailability(bool network_available)
  99. RTC_LOCKS_EXCLUDED(mutex_) override;
  100. std::map<uint32_t, RtpState> GetRtpStates() const
  101. RTC_LOCKS_EXCLUDED(mutex_) override;
  102. std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const
  103. RTC_LOCKS_EXCLUDED(mutex_) override;
  104. void DeliverRtcp(const uint8_t* packet, size_t length)
  105. RTC_LOCKS_EXCLUDED(mutex_) override;
  106. // Implements webrtc::VCMProtectionCallback.
  107. int ProtectionRequest(const FecProtectionParams* delta_params,
  108. const FecProtectionParams* key_params,
  109. uint32_t* sent_video_rate_bps,
  110. uint32_t* sent_nack_rate_bps,
  111. uint32_t* sent_fec_rate_bps)
  112. RTC_LOCKS_EXCLUDED(mutex_) override;
  113. // Implements FecControllerOverride.
  114. void SetFecAllowed(bool fec_allowed) RTC_LOCKS_EXCLUDED(mutex_) override;
  115. // Implements EncodedImageCallback.
  116. // Returns 0 if the packet was routed / sent, -1 otherwise.
  117. EncodedImageCallback::Result OnEncodedImage(
  118. const EncodedImage& encoded_image,
  119. const CodecSpecificInfo* codec_specific_info)
  120. RTC_LOCKS_EXCLUDED(mutex_) override;
  121. void OnBitrateAllocationUpdated(const VideoBitrateAllocation& bitrate)
  122. RTC_LOCKS_EXCLUDED(mutex_) override;
  123. void OnTransportOverheadChanged(size_t transport_overhead_bytes_per_packet)
  124. RTC_LOCKS_EXCLUDED(mutex_) override;
  125. void OnBitrateUpdated(BitrateAllocationUpdate update, int framerate)
  126. RTC_LOCKS_EXCLUDED(mutex_) override;
  127. uint32_t GetPayloadBitrateBps() const RTC_LOCKS_EXCLUDED(mutex_) override;
  128. uint32_t GetProtectionBitrateBps() const RTC_LOCKS_EXCLUDED(mutex_) override;
  129. void SetEncodingData(size_t width, size_t height, size_t num_temporal_layers)
  130. RTC_LOCKS_EXCLUDED(mutex_) override;
  131. std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
  132. uint32_t ssrc,
  133. rtc::ArrayView<const uint16_t> sequence_numbers) const
  134. RTC_LOCKS_EXCLUDED(mutex_) override;
  135. // From StreamFeedbackObserver.
  136. void OnPacketFeedbackVector(
  137. std::vector<StreamPacketInfo> packet_feedback_vector)
  138. RTC_LOCKS_EXCLUDED(mutex_) override;
  139. private:
  140. bool IsActiveLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  141. void SetActiveModulesLocked(const std::vector<bool> active_modules)
  142. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  143. void UpdateModuleSendingState() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  144. void ConfigureProtection();
  145. void ConfigureSsrcs();
  146. void ConfigureRids();
  147. bool NackEnabled() const;
  148. uint32_t GetPacketizationOverheadRate() const;
  149. const FieldTrialBasedConfig field_trials_;
  150. const bool send_side_bwe_with_overhead_;
  151. const bool account_for_packetization_overhead_;
  152. const bool use_early_loss_detection_;
  153. const bool has_packet_feedback_;
  154. const bool use_deferred_fec_;
  155. // TODO(holmer): Remove mutex_ once RtpVideoSender runs on the
  156. // transport task queue.
  157. mutable Mutex mutex_;
  158. bool active_ RTC_GUARDED_BY(mutex_);
  159. ProcessThread* module_process_thread_;
  160. rtc::ThreadChecker module_process_thread_checker_;
  161. std::map<uint32_t, RtpState> suspended_ssrcs_;
  162. const std::unique_ptr<FecController> fec_controller_;
  163. bool fec_allowed_ RTC_GUARDED_BY(mutex_);
  164. // Rtp modules are assumed to be sorted in simulcast index order.
  165. const std::vector<webrtc_internal_rtp_video_sender::RtpStreamSender>
  166. rtp_streams_;
  167. const RtpConfig rtp_config_;
  168. const absl::optional<VideoCodecType> codec_type_;
  169. RtpTransportControllerSendInterface* const transport_;
  170. // When using the generic descriptor we want all simulcast streams to share
  171. // one frame id space (so that the SFU can switch stream without having to
  172. // rewrite the frame id), therefore |shared_frame_id| has to live in a place
  173. // where we are aware of all the different streams.
  174. int64_t shared_frame_id_ = 0;
  175. std::vector<RtpPayloadParams> params_ RTC_GUARDED_BY(mutex_);
  176. size_t transport_overhead_bytes_per_packet_ RTC_GUARDED_BY(mutex_);
  177. uint32_t protection_bitrate_bps_;
  178. uint32_t encoder_target_rate_bps_;
  179. std::vector<bool> loss_mask_vector_ RTC_GUARDED_BY(mutex_);
  180. std::vector<FrameCounts> frame_counts_ RTC_GUARDED_BY(mutex_);
  181. FrameCountObserver* const frame_count_observer_;
  182. // Effectively const map from SSRC to RtpRtcp, for all media SSRCs.
  183. // This map is set at construction time and never changed, but it's
  184. // non-trivial to make it properly const.
  185. std::map<uint32_t, RtpRtcpInterface*> ssrc_to_rtp_module_;
  186. RTC_DISALLOW_COPY_AND_ASSIGN(RtpVideoSender);
  187. };
  188. } // namespace webrtc
  189. #endif // CALL_RTP_VIDEO_SENDER_H_