rtp_rtcp_interface.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (c) 2020 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_INTERFACE_H_
  11. #define MODULES_RTP_RTCP_SOURCE_RTP_RTCP_INTERFACE_H_
  12. #include <memory>
  13. #include <string>
  14. #include <vector>
  15. #include "absl/types/optional.h"
  16. #include "api/frame_transformer_interface.h"
  17. #include "api/scoped_refptr.h"
  18. #include "api/transport/webrtc_key_value_config.h"
  19. #include "api/video/video_bitrate_allocation.h"
  20. #include "modules/rtp_rtcp/include/receive_statistics.h"
  21. #include "modules/rtp_rtcp/include/report_block_data.h"
  22. #include "modules/rtp_rtcp/include/rtp_packet_sender.h"
  23. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  24. #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
  25. #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
  26. #include "modules/rtp_rtcp/source/video_fec_generator.h"
  27. #include "rtc_base/constructor_magic.h"
  28. namespace webrtc {
  29. // Forward declarations.
  30. class FrameEncryptorInterface;
  31. class RateLimiter;
  32. class RemoteBitrateEstimator;
  33. class RtcEventLog;
  34. class RTPSender;
  35. class Transport;
  36. class VideoBitrateAllocationObserver;
  37. class RtpRtcpInterface : public RtcpFeedbackSenderInterface {
  38. public:
  39. struct Configuration {
  40. Configuration() = default;
  41. Configuration(Configuration&& rhs) = default;
  42. // True for a audio version of the RTP/RTCP module object false will create
  43. // a video version.
  44. bool audio = false;
  45. bool receiver_only = false;
  46. // The clock to use to read time. If nullptr then system clock will be used.
  47. Clock* clock = nullptr;
  48. ReceiveStatisticsProvider* receive_statistics = nullptr;
  49. // Transport object that will be called when packets are ready to be sent
  50. // out on the network.
  51. Transport* outgoing_transport = nullptr;
  52. // Called when the receiver requests an intra frame.
  53. RtcpIntraFrameObserver* intra_frame_callback = nullptr;
  54. // Called when the receiver sends a loss notification.
  55. RtcpLossNotificationObserver* rtcp_loss_notification_observer = nullptr;
  56. // Called when we receive a changed estimate from the receiver of out
  57. // stream.
  58. RtcpBandwidthObserver* bandwidth_callback = nullptr;
  59. NetworkStateEstimateObserver* network_state_estimate_observer = nullptr;
  60. TransportFeedbackObserver* transport_feedback_callback = nullptr;
  61. VideoBitrateAllocationObserver* bitrate_allocation_observer = nullptr;
  62. RtcpRttStats* rtt_stats = nullptr;
  63. RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer = nullptr;
  64. // Called on receipt of RTCP report block from remote side.
  65. // TODO(bugs.webrtc.org/10678): Remove RtcpStatisticsCallback in
  66. // favor of ReportBlockDataObserver.
  67. // TODO(bugs.webrtc.org/10679): Consider whether we want to use
  68. // only getters or only callbacks. If we decide on getters, the
  69. // ReportBlockDataObserver should also be removed in favor of
  70. // GetLatestReportBlockData().
  71. RtcpStatisticsCallback* rtcp_statistics_callback = nullptr;
  72. RtcpCnameCallback* rtcp_cname_callback = nullptr;
  73. ReportBlockDataObserver* report_block_data_observer = nullptr;
  74. // Estimates the bandwidth available for a set of streams from the same
  75. // client.
  76. RemoteBitrateEstimator* remote_bitrate_estimator = nullptr;
  77. // Spread any bursts of packets into smaller bursts to minimize packet loss.
  78. RtpPacketSender* paced_sender = nullptr;
  79. // Generates FEC packets.
  80. // TODO(sprang): Wire up to RtpSenderEgress.
  81. VideoFecGenerator* fec_generator = nullptr;
  82. BitrateStatisticsObserver* send_bitrate_observer = nullptr;
  83. SendSideDelayObserver* send_side_delay_observer = nullptr;
  84. RtcEventLog* event_log = nullptr;
  85. SendPacketObserver* send_packet_observer = nullptr;
  86. RateLimiter* retransmission_rate_limiter = nullptr;
  87. StreamDataCountersCallback* rtp_stats_callback = nullptr;
  88. int rtcp_report_interval_ms = 0;
  89. // Update network2 instead of pacer_exit field of video timing extension.
  90. bool populate_network2_timestamp = false;
  91. rtc::scoped_refptr<FrameTransformerInterface> frame_transformer;
  92. // E2EE Custom Video Frame Encryption
  93. FrameEncryptorInterface* frame_encryptor = nullptr;
  94. // Require all outgoing frames to be encrypted with a FrameEncryptor.
  95. bool require_frame_encryption = false;
  96. // Corresponds to extmap-allow-mixed in SDP negotiation.
  97. bool extmap_allow_mixed = false;
  98. // If true, the RTP sender will always annotate outgoing packets with
  99. // MID and RID header extensions, if provided and negotiated.
  100. // If false, the RTP sender will stop sending MID and RID header extensions,
  101. // when it knows that the receiver is ready to demux based on SSRC. This is
  102. // done by RTCP RR acking.
  103. bool always_send_mid_and_rid = false;
  104. // If set, field trials are read from |field_trials|, otherwise
  105. // defaults to webrtc::FieldTrialBasedConfig.
  106. const WebRtcKeyValueConfig* field_trials = nullptr;
  107. // SSRCs for media and retransmission, respectively.
  108. // FlexFec SSRC is fetched from |flexfec_sender|.
  109. uint32_t local_media_ssrc = 0;
  110. absl::optional<uint32_t> rtx_send_ssrc;
  111. bool need_rtp_packet_infos = false;
  112. // If true, the RTP packet history will select RTX packets based on
  113. // heuristics such as send time, retransmission count etc, in order to
  114. // make padding potentially more useful.
  115. // If false, the last packet will always be picked. This may reduce CPU
  116. // overhead.
  117. bool enable_rtx_padding_prioritization = true;
  118. private:
  119. RTC_DISALLOW_COPY_AND_ASSIGN(Configuration);
  120. };
  121. // **************************************************************************
  122. // Receiver functions
  123. // **************************************************************************
  124. virtual void IncomingRtcpPacket(const uint8_t* incoming_packet,
  125. size_t incoming_packet_length) = 0;
  126. virtual void SetRemoteSSRC(uint32_t ssrc) = 0;
  127. // **************************************************************************
  128. // Sender
  129. // **************************************************************************
  130. // Sets the maximum size of an RTP packet, including RTP headers.
  131. virtual void SetMaxRtpPacketSize(size_t size) = 0;
  132. // Returns max RTP packet size. Takes into account RTP headers and
  133. // FEC/ULP/RED overhead (when FEC is enabled).
  134. virtual size_t MaxRtpPacketSize() const = 0;
  135. virtual void RegisterSendPayloadFrequency(int payload_type,
  136. int payload_frequency) = 0;
  137. // Unregisters a send payload.
  138. // |payload_type| - payload type of codec
  139. // Returns -1 on failure else 0.
  140. virtual int32_t DeRegisterSendPayload(int8_t payload_type) = 0;
  141. virtual void SetExtmapAllowMixed(bool extmap_allow_mixed) = 0;
  142. // Register extension by uri, triggers CHECK on falure.
  143. virtual void RegisterRtpHeaderExtension(absl::string_view uri, int id) = 0;
  144. virtual int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) = 0;
  145. virtual void DeregisterSendRtpHeaderExtension(absl::string_view uri) = 0;
  146. // Returns true if RTP module is send media, and any of the extensions
  147. // required for bandwidth estimation is registered.
  148. virtual bool SupportsPadding() const = 0;
  149. // Same as SupportsPadding(), but additionally requires that
  150. // SetRtxSendStatus() has been called with the kRtxRedundantPayloads option
  151. // enabled.
  152. virtual bool SupportsRtxPayloadPadding() const = 0;
  153. // Returns start timestamp.
  154. virtual uint32_t StartTimestamp() const = 0;
  155. // Sets start timestamp. Start timestamp is set to a random value if this
  156. // function is never called.
  157. virtual void SetStartTimestamp(uint32_t timestamp) = 0;
  158. // Returns SequenceNumber.
  159. virtual uint16_t SequenceNumber() const = 0;
  160. // Sets SequenceNumber, default is a random number.
  161. virtual void SetSequenceNumber(uint16_t seq) = 0;
  162. virtual void SetRtpState(const RtpState& rtp_state) = 0;
  163. virtual void SetRtxState(const RtpState& rtp_state) = 0;
  164. virtual RtpState GetRtpState() const = 0;
  165. virtual RtpState GetRtxState() const = 0;
  166. // Returns SSRC.
  167. virtual uint32_t SSRC() const = 0;
  168. // Sets the value for sending in the RID (and Repaired) RTP header extension.
  169. // RIDs are used to identify an RTP stream if SSRCs are not negotiated.
  170. // If the RID and Repaired RID extensions are not registered, the RID will
  171. // not be sent.
  172. virtual void SetRid(const std::string& rid) = 0;
  173. // Sets the value for sending in the MID RTP header extension.
  174. // The MID RTP header extension should be registered for this to do anything.
  175. // Once set, this value can not be changed or removed.
  176. virtual void SetMid(const std::string& mid) = 0;
  177. // Sets CSRC.
  178. // |csrcs| - vector of CSRCs
  179. virtual void SetCsrcs(const std::vector<uint32_t>& csrcs) = 0;
  180. // Turns on/off sending RTX (RFC 4588). The modes can be set as a combination
  181. // of values of the enumerator RtxMode.
  182. virtual void SetRtxSendStatus(int modes) = 0;
  183. // Returns status of sending RTX (RFC 4588). The returned value can be
  184. // a combination of values of the enumerator RtxMode.
  185. virtual int RtxSendStatus() const = 0;
  186. // Returns the SSRC used for RTX if set, otherwise a nullopt.
  187. virtual absl::optional<uint32_t> RtxSsrc() const = 0;
  188. // Sets the payload type to use when sending RTX packets. Note that this
  189. // doesn't enable RTX, only the payload type is set.
  190. virtual void SetRtxSendPayloadType(int payload_type,
  191. int associated_payload_type) = 0;
  192. // Returns the FlexFEC SSRC, if there is one.
  193. virtual absl::optional<uint32_t> FlexfecSsrc() const = 0;
  194. // Sets sending status. Sends kRtcpByeCode when going from true to false.
  195. // Returns -1 on failure else 0.
  196. virtual int32_t SetSendingStatus(bool sending) = 0;
  197. // Returns current sending status.
  198. virtual bool Sending() const = 0;
  199. // Starts/Stops media packets. On by default.
  200. virtual void SetSendingMediaStatus(bool sending) = 0;
  201. // Returns current media sending status.
  202. virtual bool SendingMedia() const = 0;
  203. // Returns whether audio is configured (i.e. Configuration::audio = true).
  204. virtual bool IsAudioConfigured() const = 0;
  205. // Indicate that the packets sent by this module should be counted towards the
  206. // bitrate estimate since the stream participates in the bitrate allocation.
  207. virtual void SetAsPartOfAllocation(bool part_of_allocation) = 0;
  208. // Returns bitrate sent (post-pacing) per packet type.
  209. virtual RtpSendRates GetSendRates() const = 0;
  210. virtual RTPSender* RtpSender() = 0;
  211. virtual const RTPSender* RtpSender() const = 0;
  212. // Record that a frame is about to be sent. Returns true on success, and false
  213. // if the module isn't ready to send.
  214. virtual bool OnSendingRtpFrame(uint32_t timestamp,
  215. int64_t capture_time_ms,
  216. int payload_type,
  217. bool force_sender_report) = 0;
  218. // Try to send the provided packet. Returns true iff packet matches any of
  219. // the SSRCs for this module (media/rtx/fec etc) and was forwarded to the
  220. // transport.
  221. virtual bool TrySendPacket(RtpPacketToSend* packet,
  222. const PacedPacketInfo& pacing_info) = 0;
  223. // Update the FEC protection parameters to use for delta- and key-frames.
  224. // Only used when deferred FEC is active.
  225. virtual void SetFecProtectionParams(
  226. const FecProtectionParams& delta_params,
  227. const FecProtectionParams& key_params) = 0;
  228. // If deferred FEC generation is enabled, this method should be called after
  229. // calling TrySendPacket(). Any generated FEC packets will be removed and
  230. // returned from the FEC generator.
  231. virtual std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() = 0;
  232. virtual void OnPacketsAcknowledged(
  233. rtc::ArrayView<const uint16_t> sequence_numbers) = 0;
  234. virtual std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
  235. size_t target_size_bytes) = 0;
  236. virtual std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
  237. rtc::ArrayView<const uint16_t> sequence_numbers) const = 0;
  238. // Returns an expected per packet overhead representing the main RTP header,
  239. // any CSRCs, and the registered header extensions that are expected on all
  240. // packets (i.e. disregarding things like abs capture time which is only
  241. // populated on a subset of packets, but counting MID/RID type extensions
  242. // when we expect to send them).
  243. virtual size_t ExpectedPerPacketOverhead() const = 0;
  244. // **************************************************************************
  245. // RTCP
  246. // **************************************************************************
  247. // Returns RTCP status.
  248. virtual RtcpMode RTCP() const = 0;
  249. // Sets RTCP status i.e on(compound or non-compound)/off.
  250. // |method| - RTCP method to use.
  251. virtual void SetRTCPStatus(RtcpMode method) = 0;
  252. // Sets RTCP CName (i.e unique identifier).
  253. // Returns -1 on failure else 0.
  254. virtual int32_t SetCNAME(const char* cname) = 0;
  255. // Returns remote NTP.
  256. // Returns -1 on failure else 0.
  257. virtual int32_t RemoteNTP(uint32_t* received_ntp_secs,
  258. uint32_t* received_ntp_frac,
  259. uint32_t* rtcp_arrival_time_secs,
  260. uint32_t* rtcp_arrival_time_frac,
  261. uint32_t* rtcp_timestamp) const = 0;
  262. // Returns current RTT (round-trip time) estimate.
  263. // Returns -1 on failure else 0.
  264. virtual int32_t RTT(uint32_t remote_ssrc,
  265. int64_t* rtt,
  266. int64_t* avg_rtt,
  267. int64_t* min_rtt,
  268. int64_t* max_rtt) const = 0;
  269. // Returns the estimated RTT, with fallback to a default value.
  270. virtual int64_t ExpectedRetransmissionTimeMs() const = 0;
  271. // Forces a send of a RTCP packet. Periodic SR and RR are triggered via the
  272. // process function.
  273. // Returns -1 on failure else 0.
  274. virtual int32_t SendRTCP(RTCPPacketType rtcp_packet_type) = 0;
  275. // Returns send statistics for the RTP and RTX stream.
  276. virtual void GetSendStreamDataCounters(
  277. StreamDataCounters* rtp_counters,
  278. StreamDataCounters* rtx_counters) const = 0;
  279. // Returns received RTCP report block.
  280. // Returns -1 on failure else 0.
  281. // TODO(https://crbug.com/webrtc/10678): Remove this in favor of
  282. // GetLatestReportBlockData().
  283. virtual int32_t RemoteRTCPStat(
  284. std::vector<RTCPReportBlock>* receive_blocks) const = 0;
  285. // A snapshot of Report Blocks with additional data of interest to statistics.
  286. // Within this list, the sender-source SSRC pair is unique and per-pair the
  287. // ReportBlockData represents the latest Report Block that was received for
  288. // that pair.
  289. virtual std::vector<ReportBlockData> GetLatestReportBlockData() const = 0;
  290. // (XR) Sets Receiver Reference Time Report (RTTR) status.
  291. virtual void SetRtcpXrRrtrStatus(bool enable) = 0;
  292. // Returns current Receiver Reference Time Report (RTTR) status.
  293. virtual bool RtcpXrRrtrStatus() const = 0;
  294. // (REMB) Receiver Estimated Max Bitrate.
  295. // Schedules sending REMB on next and following sender/receiver reports.
  296. void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override = 0;
  297. // Stops sending REMB on next and following sender/receiver reports.
  298. void UnsetRemb() override = 0;
  299. // (NACK)
  300. // Sends a Negative acknowledgement packet.
  301. // Returns -1 on failure else 0.
  302. // TODO(philipel): Deprecate this and start using SendNack instead, mostly
  303. // because we want a function that actually send NACK for the specified
  304. // packets.
  305. virtual int32_t SendNACK(const uint16_t* nack_list, uint16_t size) = 0;
  306. // Sends NACK for the packets specified.
  307. // Note: This assumes the caller keeps track of timing and doesn't rely on
  308. // the RTP module to do this.
  309. virtual void SendNack(const std::vector<uint16_t>& sequence_numbers) = 0;
  310. // Store the sent packets, needed to answer to a Negative acknowledgment
  311. // requests.
  312. virtual void SetStorePacketsStatus(bool enable, uint16_t numberToStore) = 0;
  313. // Returns true if the module is configured to store packets.
  314. virtual bool StorePackets() const = 0;
  315. virtual void SetVideoBitrateAllocation(
  316. const VideoBitrateAllocation& bitrate) = 0;
  317. // **************************************************************************
  318. // Video
  319. // **************************************************************************
  320. // Requests new key frame.
  321. // using PLI, https://tools.ietf.org/html/rfc4585#section-6.3.1.1
  322. void SendPictureLossIndication() { SendRTCP(kRtcpPli); }
  323. // using FIR, https://tools.ietf.org/html/rfc5104#section-4.3.1.2
  324. void SendFullIntraRequest() { SendRTCP(kRtcpFir); }
  325. // Sends a LossNotification RTCP message.
  326. // Returns -1 on failure else 0.
  327. virtual int32_t SendLossNotification(uint16_t last_decoded_seq_num,
  328. uint16_t last_received_seq_num,
  329. bool decodability_flag,
  330. bool buffering_allowed) = 0;
  331. };
  332. } // namespace webrtc
  333. #endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_INTERFACE_H_