rtp_rtcp_impl2.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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_RTCP_IMPL2_H_
  11. #define MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include <set>
  16. #include <string>
  17. #include <vector>
  18. #include "absl/types/optional.h"
  19. #include "api/rtp_headers.h"
  20. #include "api/task_queue/task_queue_base.h"
  21. #include "api/video/video_bitrate_allocation.h"
  22. #include "modules/include/module_fec_types.h"
  23. #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
  24. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" // RTCPPacketType
  25. #include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h"
  26. #include "modules/rtp_rtcp/source/rtcp_receiver.h"
  27. #include "modules/rtp_rtcp/source/rtcp_sender.h"
  28. #include "modules/rtp_rtcp/source/rtp_packet_history.h"
  29. #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
  30. #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h"
  31. #include "modules/rtp_rtcp/source/rtp_sender.h"
  32. #include "modules/rtp_rtcp/source/rtp_sender_egress.h"
  33. #include "rtc_base/gtest_prod_util.h"
  34. #include "rtc_base/synchronization/mutex.h"
  35. #include "rtc_base/synchronization/sequence_checker.h"
  36. #include "rtc_base/system/no_unique_address.h"
  37. #include "rtc_base/task_utils/pending_task_safety_flag.h"
  38. #include "rtc_base/task_utils/repeating_task.h"
  39. namespace webrtc {
  40. class Clock;
  41. struct PacedPacketInfo;
  42. struct RTPVideoHeader;
  43. class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface,
  44. public Module,
  45. public RTCPReceiver::ModuleRtpRtcp {
  46. public:
  47. explicit ModuleRtpRtcpImpl2(
  48. const RtpRtcpInterface::Configuration& configuration);
  49. ~ModuleRtpRtcpImpl2() override;
  50. // This method is provided to easy with migrating away from the
  51. // RtpRtcp::Create factory method. Since this is an internal implementation
  52. // detail though, creating an instance of ModuleRtpRtcpImpl2 directly should
  53. // be fine.
  54. static std::unique_ptr<ModuleRtpRtcpImpl2> Create(
  55. const Configuration& configuration);
  56. // Returns the number of milliseconds until the module want a worker thread to
  57. // call Process.
  58. int64_t TimeUntilNextProcess() override;
  59. // Process any pending tasks such as timeouts.
  60. void Process() override;
  61. // Receiver part.
  62. // Called when we receive an RTCP packet.
  63. void IncomingRtcpPacket(const uint8_t* incoming_packet,
  64. size_t incoming_packet_length) override;
  65. void SetRemoteSSRC(uint32_t ssrc) override;
  66. // Sender part.
  67. void RegisterSendPayloadFrequency(int payload_type,
  68. int payload_frequency) override;
  69. int32_t DeRegisterSendPayload(int8_t payload_type) override;
  70. void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
  71. void RegisterRtpHeaderExtension(absl::string_view uri, int id) override;
  72. int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override;
  73. void DeregisterSendRtpHeaderExtension(absl::string_view uri) override;
  74. bool SupportsPadding() const override;
  75. bool SupportsRtxPayloadPadding() const override;
  76. // Get start timestamp.
  77. uint32_t StartTimestamp() const override;
  78. // Configure start timestamp, default is a random number.
  79. void SetStartTimestamp(uint32_t timestamp) override;
  80. uint16_t SequenceNumber() const override;
  81. // Set SequenceNumber, default is a random number.
  82. void SetSequenceNumber(uint16_t seq) override;
  83. void SetRtpState(const RtpState& rtp_state) override;
  84. void SetRtxState(const RtpState& rtp_state) override;
  85. RtpState GetRtpState() const override;
  86. RtpState GetRtxState() const override;
  87. uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
  88. void SetRid(const std::string& rid) override;
  89. void SetMid(const std::string& mid) override;
  90. void SetCsrcs(const std::vector<uint32_t>& csrcs) override;
  91. RTCPSender::FeedbackState GetFeedbackState();
  92. void SetRtxSendStatus(int mode) override;
  93. int RtxSendStatus() const override;
  94. absl::optional<uint32_t> RtxSsrc() const override;
  95. void SetRtxSendPayloadType(int payload_type,
  96. int associated_payload_type) override;
  97. absl::optional<uint32_t> FlexfecSsrc() const override;
  98. // Sends kRtcpByeCode when going from true to false.
  99. int32_t SetSendingStatus(bool sending) override;
  100. bool Sending() const override;
  101. // Drops or relays media packets.
  102. void SetSendingMediaStatus(bool sending) override;
  103. bool SendingMedia() const override;
  104. bool IsAudioConfigured() const override;
  105. void SetAsPartOfAllocation(bool part_of_allocation) override;
  106. bool OnSendingRtpFrame(uint32_t timestamp,
  107. int64_t capture_time_ms,
  108. int payload_type,
  109. bool force_sender_report) override;
  110. bool TrySendPacket(RtpPacketToSend* packet,
  111. const PacedPacketInfo& pacing_info) override;
  112. void SetFecProtectionParams(const FecProtectionParams& delta_params,
  113. const FecProtectionParams& key_params) override;
  114. std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override;
  115. void OnPacketsAcknowledged(
  116. rtc::ArrayView<const uint16_t> sequence_numbers) override;
  117. std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
  118. size_t target_size_bytes) override;
  119. std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
  120. rtc::ArrayView<const uint16_t> sequence_numbers) const override;
  121. size_t ExpectedPerPacketOverhead() const override;
  122. // RTCP part.
  123. // Get RTCP status.
  124. RtcpMode RTCP() const override;
  125. // Configure RTCP status i.e on/off.
  126. void SetRTCPStatus(RtcpMode method) override;
  127. // Set RTCP CName.
  128. int32_t SetCNAME(const char* c_name) override;
  129. // Get remote NTP.
  130. int32_t RemoteNTP(uint32_t* received_ntp_secs,
  131. uint32_t* received_ntp_frac,
  132. uint32_t* rtcp_arrival_time_secs,
  133. uint32_t* rtcp_arrival_time_frac,
  134. uint32_t* rtcp_timestamp) const override;
  135. // Get RoundTripTime.
  136. int32_t RTT(uint32_t remote_ssrc,
  137. int64_t* rtt,
  138. int64_t* avg_rtt,
  139. int64_t* min_rtt,
  140. int64_t* max_rtt) const override;
  141. int64_t ExpectedRetransmissionTimeMs() const override;
  142. // Force a send of an RTCP packet.
  143. // Normal SR and RR are triggered via the process function.
  144. int32_t SendRTCP(RTCPPacketType rtcpPacketType) override;
  145. void GetSendStreamDataCounters(
  146. StreamDataCounters* rtp_counters,
  147. StreamDataCounters* rtx_counters) const override;
  148. // Get received RTCP report, report block.
  149. int32_t RemoteRTCPStat(
  150. std::vector<RTCPReportBlock>* receive_blocks) const override;
  151. // A snapshot of the most recent Report Block with additional data of
  152. // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
  153. // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(),
  154. // which is the SSRC of the corresponding outbound RTP stream, is unique.
  155. std::vector<ReportBlockData> GetLatestReportBlockData() const override;
  156. // (REMB) Receiver Estimated Max Bitrate.
  157. void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
  158. void UnsetRemb() override;
  159. void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
  160. size_t MaxRtpPacketSize() const override;
  161. void SetMaxRtpPacketSize(size_t max_packet_size) override;
  162. // (NACK) Negative acknowledgment part.
  163. // Send a Negative acknowledgment packet.
  164. // TODO(philipel): Deprecate SendNACK and use SendNack instead.
  165. int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
  166. void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
  167. // Store the sent packets, needed to answer to a negative acknowledgment
  168. // requests.
  169. void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
  170. bool StorePackets() const override;
  171. void SendCombinedRtcpPacket(
  172. std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
  173. // (XR) Receiver reference time report.
  174. void SetRtcpXrRrtrStatus(bool enable) override;
  175. bool RtcpXrRrtrStatus() const override;
  176. // Video part.
  177. int32_t SendLossNotification(uint16_t last_decoded_seq_num,
  178. uint16_t last_received_seq_num,
  179. bool decodability_flag,
  180. bool buffering_allowed) override;
  181. bool LastReceivedNTP(uint32_t* NTPsecs,
  182. uint32_t* NTPfrac,
  183. uint32_t* remote_sr) const;
  184. RtpSendRates GetSendRates() const override;
  185. void OnReceivedNack(
  186. const std::vector<uint16_t>& nack_sequence_numbers) override;
  187. void OnReceivedRtcpReportBlocks(
  188. const ReportBlockList& report_blocks) override;
  189. void OnRequestSendReport() override;
  190. void SetVideoBitrateAllocation(
  191. const VideoBitrateAllocation& bitrate) override;
  192. RTPSender* RtpSender() override;
  193. const RTPSender* RtpSender() const override;
  194. private:
  195. FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt);
  196. FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly);
  197. struct RtpSenderContext : public SequenceNumberAssigner {
  198. explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config);
  199. void AssignSequenceNumber(RtpPacketToSend* packet) override;
  200. // Storage of packets, for retransmissions and padding, if applicable.
  201. RtpPacketHistory packet_history;
  202. // Handles final time timestamping/stats/etc and handover to Transport.
  203. RtpSenderEgress packet_sender;
  204. // If no paced sender configured, this class will be used to pass packets
  205. // from |packet_generator_| to |packet_sender_|.
  206. RtpSenderEgress::NonPacedPacketSender non_paced_sender;
  207. // Handles creation of RTP packets to be sent.
  208. RTPSender packet_generator;
  209. };
  210. void set_rtt_ms(int64_t rtt_ms);
  211. int64_t rtt_ms() const;
  212. bool TimeToSendFullNackList(int64_t now) const;
  213. // Called on a timer, once a second, on the worker_queue_, to update the RTT,
  214. // check if we need to send RTCP report, send TMMBR updates and fire events.
  215. void PeriodicUpdate();
  216. TaskQueueBase* const worker_queue_;
  217. RTC_NO_UNIQUE_ADDRESS SequenceChecker process_thread_checker_;
  218. std::unique_ptr<RtpSenderContext> rtp_sender_;
  219. RTCPSender rtcp_sender_;
  220. RTCPReceiver rtcp_receiver_;
  221. Clock* const clock_;
  222. int64_t last_rtt_process_time_;
  223. int64_t next_process_time_;
  224. uint16_t packet_overhead_;
  225. // Send side
  226. int64_t nack_last_time_sent_full_ms_;
  227. uint16_t nack_last_seq_number_sent_;
  228. RemoteBitrateEstimator* const remote_bitrate_;
  229. RtcpRttStats* const rtt_stats_;
  230. RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_);
  231. // The processed RTT from RtcpRttStats.
  232. mutable Mutex mutex_rtt_;
  233. int64_t rtt_ms_;
  234. };
  235. } // namespace webrtc
  236. #endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_