rtp_rtcp_impl.h 12 KB

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