rtp_sender_video.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2012 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 MODULES_RTP_RTCP_SOURCE_RTP_SENDER_VIDEO_H_
  11. #define MODULES_RTP_RTCP_SOURCE_RTP_SENDER_VIDEO_H_
  12. #include <map>
  13. #include <memory>
  14. #include <vector>
  15. #include "absl/strings/string_view.h"
  16. #include "absl/types/optional.h"
  17. #include "api/array_view.h"
  18. #include "api/frame_transformer_interface.h"
  19. #include "api/scoped_refptr.h"
  20. #include "api/task_queue/task_queue_base.h"
  21. #include "api/transport/rtp/dependency_descriptor.h"
  22. #include "api/video/video_codec_type.h"
  23. #include "api/video/video_frame_type.h"
  24. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  25. #include "modules/rtp_rtcp/source/absolute_capture_time_sender.h"
  26. #include "modules/rtp_rtcp/source/active_decode_targets_helper.h"
  27. #include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
  28. #include "modules/rtp_rtcp/source/rtp_sender.h"
  29. #include "modules/rtp_rtcp/source/rtp_sender_video_frame_transformer_delegate.h"
  30. #include "modules/rtp_rtcp/source/rtp_video_header.h"
  31. #include "modules/rtp_rtcp/source/video_fec_generator.h"
  32. #include "rtc_base/one_time_event.h"
  33. #include "rtc_base/race_checker.h"
  34. #include "rtc_base/rate_statistics.h"
  35. #include "rtc_base/synchronization/mutex.h"
  36. #include "rtc_base/synchronization/sequence_checker.h"
  37. #include "rtc_base/thread_annotations.h"
  38. namespace webrtc {
  39. class FrameEncryptorInterface;
  40. class RtpPacketizer;
  41. class RtpPacketToSend;
  42. // kConditionallyRetransmitHigherLayers allows retransmission of video frames
  43. // in higher layers if either the last frame in that layer was too far back in
  44. // time, or if we estimate that a new frame will be available in a lower layer
  45. // in a shorter time than it would take to request and receive a retransmission.
  46. enum RetransmissionMode : uint8_t {
  47. kRetransmitOff = 0x0,
  48. kRetransmitBaseLayer = 0x2,
  49. kRetransmitHigherLayers = 0x4,
  50. kRetransmitAllLayers = 0x6,
  51. kConditionallyRetransmitHigherLayers = 0x8
  52. };
  53. class RTPSenderVideo {
  54. public:
  55. static constexpr int64_t kTLRateWindowSizeMs = 2500;
  56. struct Config {
  57. Config() = default;
  58. Config(const Config&) = delete;
  59. Config(Config&&) = default;
  60. // All members of this struct, with the exception of |field_trials|, are
  61. // expected to outlive the RTPSenderVideo object they are passed to.
  62. Clock* clock = nullptr;
  63. RTPSender* rtp_sender = nullptr;
  64. FlexfecSender* flexfec_sender = nullptr;
  65. VideoFecGenerator* fec_generator = nullptr;
  66. // Some FEC data is duplicated here in preparation of moving FEC to
  67. // the egress stage.
  68. absl::optional<VideoFecGenerator::FecType> fec_type;
  69. size_t fec_overhead_bytes = 0; // Per packet max FEC overhead.
  70. FrameEncryptorInterface* frame_encryptor = nullptr;
  71. bool require_frame_encryption = false;
  72. bool enable_retransmit_all_layers = false;
  73. absl::optional<int> red_payload_type;
  74. const WebRtcKeyValueConfig* field_trials = nullptr;
  75. rtc::scoped_refptr<FrameTransformerInterface> frame_transformer;
  76. TaskQueueBase* send_transport_queue = nullptr;
  77. };
  78. explicit RTPSenderVideo(const Config& config);
  79. virtual ~RTPSenderVideo();
  80. // expected_retransmission_time_ms.has_value() -> retransmission allowed.
  81. // Calls to this method is assumed to be externally serialized.
  82. // |estimated_capture_clock_offset_ms| is an estimated clock offset between
  83. // this sender and the original capturer, for this video packet. See
  84. // http://www.webrtc.org/experiments/rtp-hdrext/abs-capture-time for more
  85. // details. If the sender and the capture has the same clock, it is supposed
  86. // to be zero valued, which is given as the default.
  87. bool SendVideo(int payload_type,
  88. absl::optional<VideoCodecType> codec_type,
  89. uint32_t rtp_timestamp,
  90. int64_t capture_time_ms,
  91. rtc::ArrayView<const uint8_t> payload,
  92. RTPVideoHeader video_header,
  93. absl::optional<int64_t> expected_retransmission_time_ms,
  94. absl::optional<int64_t> estimated_capture_clock_offset_ms = 0);
  95. bool SendEncodedImage(
  96. int payload_type,
  97. absl::optional<VideoCodecType> codec_type,
  98. uint32_t rtp_timestamp,
  99. const EncodedImage& encoded_image,
  100. RTPVideoHeader video_header,
  101. absl::optional<int64_t> expected_retransmission_time_ms);
  102. // Configures video structures produced by encoder to send using the
  103. // dependency descriptor rtp header extension. Next call to SendVideo should
  104. // have video_header.frame_type == kVideoFrameKey.
  105. // All calls to SendVideo after this call must use video_header compatible
  106. // with the video_structure.
  107. void SetVideoStructure(const FrameDependencyStructure* video_structure);
  108. void SetVideoStructureUnderLock(
  109. const FrameDependencyStructure* video_structure);
  110. uint32_t VideoBitrateSent() const;
  111. // Returns the current packetization overhead rate, in bps. Note that this is
  112. // the payload overhead, eg the VP8 payload headers, not the RTP headers
  113. // or extension/
  114. uint32_t PacketizationOverheadBps() const;
  115. protected:
  116. static uint8_t GetTemporalId(const RTPVideoHeader& header);
  117. bool AllowRetransmission(uint8_t temporal_id,
  118. int32_t retransmission_settings,
  119. int64_t expected_retransmission_time_ms);
  120. private:
  121. struct TemporalLayerStats {
  122. TemporalLayerStats()
  123. : frame_rate_fp1000s(kTLRateWindowSizeMs, 1000 * 1000),
  124. last_frame_time_ms(0) {}
  125. // Frame rate, in frames per 1000 seconds. This essentially turns the fps
  126. // value into a fixed point value with three decimals. Improves precision at
  127. // low frame rates.
  128. RateStatistics frame_rate_fp1000s;
  129. int64_t last_frame_time_ms;
  130. };
  131. void AddRtpHeaderExtensions(
  132. const RTPVideoHeader& video_header,
  133. const absl::optional<AbsoluteCaptureTime>& absolute_capture_time,
  134. bool first_packet,
  135. bool last_packet,
  136. RtpPacketToSend* packet) const
  137. RTC_EXCLUSIVE_LOCKS_REQUIRED(send_checker_);
  138. size_t FecPacketOverhead() const RTC_EXCLUSIVE_LOCKS_REQUIRED(send_checker_);
  139. void LogAndSendToNetwork(
  140. std::vector<std::unique_ptr<RtpPacketToSend>> packets,
  141. size_t unpacketized_payload_size);
  142. bool red_enabled() const { return red_payload_type_.has_value(); }
  143. bool UpdateConditionalRetransmit(uint8_t temporal_id,
  144. int64_t expected_retransmission_time_ms)
  145. RTC_EXCLUSIVE_LOCKS_REQUIRED(stats_mutex_);
  146. void MaybeUpdateCurrentPlayoutDelay(const RTPVideoHeader& header)
  147. RTC_EXCLUSIVE_LOCKS_REQUIRED(send_checker_);
  148. RTPSender* const rtp_sender_;
  149. Clock* const clock_;
  150. const int32_t retransmission_settings_;
  151. // These members should only be accessed from within SendVideo() to avoid
  152. // potential race conditions.
  153. rtc::RaceChecker send_checker_;
  154. VideoRotation last_rotation_ RTC_GUARDED_BY(send_checker_);
  155. absl::optional<ColorSpace> last_color_space_ RTC_GUARDED_BY(send_checker_);
  156. bool transmit_color_space_next_frame_ RTC_GUARDED_BY(send_checker_);
  157. std::unique_ptr<FrameDependencyStructure> video_structure_
  158. RTC_GUARDED_BY(send_checker_);
  159. // Current target playout delay.
  160. VideoPlayoutDelay current_playout_delay_ RTC_GUARDED_BY(send_checker_);
  161. // Flag indicating if we need to propagate |current_playout_delay_| in order
  162. // to guarantee it gets delivered.
  163. bool playout_delay_pending_;
  164. // Set by the field trial WebRTC-ForceSendPlayoutDelay to override the playout
  165. // delay of outgoing video frames.
  166. const absl::optional<VideoPlayoutDelay> forced_playout_delay_;
  167. // Should never be held when calling out of this class.
  168. Mutex mutex_;
  169. const absl::optional<int> red_payload_type_;
  170. VideoFecGenerator* const fec_generator_;
  171. absl::optional<VideoFecGenerator::FecType> fec_type_;
  172. const size_t fec_overhead_bytes_; // Per packet max FEC overhead.
  173. mutable Mutex stats_mutex_;
  174. // Bitrate used for video payload and RTP headers.
  175. RateStatistics video_bitrate_ RTC_GUARDED_BY(stats_mutex_);
  176. RateStatistics packetization_overhead_bitrate_ RTC_GUARDED_BY(stats_mutex_);
  177. std::map<int, TemporalLayerStats> frame_stats_by_temporal_layer_
  178. RTC_GUARDED_BY(stats_mutex_);
  179. OneTimeEvent first_frame_sent_;
  180. // E2EE Custom Video Frame Encryptor (optional)
  181. FrameEncryptorInterface* const frame_encryptor_ = nullptr;
  182. // If set to true will require all outgoing frames to pass through an
  183. // initialized frame_encryptor_ before being sent out of the network.
  184. // Otherwise these payloads will be dropped.
  185. const bool require_frame_encryption_;
  186. // Set to true if the generic descriptor should be authenticated.
  187. const bool generic_descriptor_auth_experiment_;
  188. AbsoluteCaptureTimeSender absolute_capture_time_sender_;
  189. // Tracks updates to the active decode targets and decides when active decode
  190. // targets bitmask should be attached to the dependency descriptor.
  191. ActiveDecodeTargetsHelper active_decode_targets_tracker_;
  192. const rtc::scoped_refptr<RTPSenderVideoFrameTransformerDelegate>
  193. frame_transformer_delegate_;
  194. const bool include_capture_clock_offset_;
  195. };
  196. } // namespace webrtc
  197. #endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_VIDEO_H_