xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl2.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_
12 #define MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <memory>
18 #include <set>
19 #include <string>
20 #include <vector>
21 
22 #include "absl/strings/string_view.h"
23 #include "absl/types/optional.h"
24 #include "api/rtp_headers.h"
25 #include "api/sequence_checker.h"
26 #include "api/task_queue/pending_task_safety_flag.h"
27 #include "api/task_queue/task_queue_base.h"
28 #include "api/units/time_delta.h"
29 #include "api/video/video_bitrate_allocation.h"
30 #include "modules/include/module_fec_types.h"
31 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"  // RTCPPacketType
32 #include "modules/rtp_rtcp/source/packet_sequencer.h"
33 #include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h"
34 #include "modules/rtp_rtcp/source/rtcp_receiver.h"
35 #include "modules/rtp_rtcp/source/rtcp_sender.h"
36 #include "modules/rtp_rtcp/source/rtp_packet_history.h"
37 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
38 #include "modules/rtp_rtcp/source/rtp_sender.h"
39 #include "modules/rtp_rtcp/source/rtp_sender_egress.h"
40 #include "rtc_base/gtest_prod_util.h"
41 #include "rtc_base/synchronization/mutex.h"
42 #include "rtc_base/system/no_unique_address.h"
43 #include "rtc_base/task_utils/repeating_task.h"
44 #include "rtc_base/thread_annotations.h"
45 
46 namespace webrtc {
47 
48 class Clock;
49 struct PacedPacketInfo;
50 struct RTPVideoHeader;
51 
52 class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface,
53                                  public RTCPReceiver::ModuleRtpRtcp {
54  public:
55   explicit ModuleRtpRtcpImpl2(
56       const RtpRtcpInterface::Configuration& configuration);
57   ~ModuleRtpRtcpImpl2() override;
58 
59   // This method is provided to easy with migrating away from the
60   // RtpRtcp::Create factory method. Since this is an internal implementation
61   // detail though, creating an instance of ModuleRtpRtcpImpl2 directly should
62   // be fine.
63   static std::unique_ptr<ModuleRtpRtcpImpl2> Create(
64       const Configuration& configuration);
65 
66   // Receiver part.
67 
68   // Called when we receive an RTCP packet.
69   void IncomingRtcpPacket(const uint8_t* incoming_packet,
70                           size_t incoming_packet_length) override;
71 
72   void SetRemoteSSRC(uint32_t ssrc) override;
73 
74   void SetLocalSsrc(uint32_t local_ssrc) override;
75 
76   // Sender part.
77   void RegisterSendPayloadFrequency(int payload_type,
78                                     int payload_frequency) override;
79 
80   int32_t DeRegisterSendPayload(int8_t payload_type) override;
81 
82   void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
83 
84   void RegisterRtpHeaderExtension(absl::string_view uri, int id) override;
85   void DeregisterSendRtpHeaderExtension(absl::string_view uri) override;
86 
87   bool SupportsPadding() const override;
88   bool SupportsRtxPayloadPadding() const override;
89 
90   // Get start timestamp.
91   uint32_t StartTimestamp() const override;
92 
93   // Configure start timestamp, default is a random number.
94   void SetStartTimestamp(uint32_t timestamp) override;
95 
96   uint16_t SequenceNumber() const override;
97 
98   // Set SequenceNumber, default is a random number.
99   void SetSequenceNumber(uint16_t seq) override;
100 
101   void SetRtpState(const RtpState& rtp_state) override;
102   void SetRtxState(const RtpState& rtp_state) override;
103   RtpState GetRtpState() const override;
104   RtpState GetRtxState() const override;
105 
106   void SetNonSenderRttMeasurement(bool enabled) override;
107 
SSRC()108   uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
109 
110   // Semantically identical to `SSRC()` but must be called on the packet
111   // delivery thread/tq and returns the ssrc that maps to
112   // RtpRtcpInterface::Configuration::local_media_ssrc.
113   uint32_t local_media_ssrc() const;
114 
115   void SetMid(absl::string_view mid) override;
116 
117   void SetCsrcs(const std::vector<uint32_t>& csrcs) override;
118 
119   RTCPSender::FeedbackState GetFeedbackState();
120 
121   void SetRtxSendStatus(int mode) override;
122   int RtxSendStatus() const override;
123   absl::optional<uint32_t> RtxSsrc() const override;
124 
125   void SetRtxSendPayloadType(int payload_type,
126                              int associated_payload_type) override;
127 
128   absl::optional<uint32_t> FlexfecSsrc() const override;
129 
130   // Sends kRtcpByeCode when going from true to false.
131   int32_t SetSendingStatus(bool sending) override;
132 
133   bool Sending() const override;
134 
135   // Drops or relays media packets.
136   void SetSendingMediaStatus(bool sending) override;
137 
138   bool SendingMedia() const override;
139 
140   bool IsAudioConfigured() const override;
141 
142   void SetAsPartOfAllocation(bool part_of_allocation) override;
143 
144   bool OnSendingRtpFrame(uint32_t timestamp,
145                          int64_t capture_time_ms,
146                          int payload_type,
147                          bool force_sender_report) override;
148 
149   bool TrySendPacket(RtpPacketToSend* packet,
150                      const PacedPacketInfo& pacing_info) override;
151 
152   void SetFecProtectionParams(const FecProtectionParams& delta_params,
153                               const FecProtectionParams& key_params) override;
154 
155   std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override;
156 
157   void OnAbortedRetransmissions(
158       rtc::ArrayView<const uint16_t> sequence_numbers) override;
159 
160   void OnPacketsAcknowledged(
161       rtc::ArrayView<const uint16_t> sequence_numbers) override;
162 
163   std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
164       size_t target_size_bytes) override;
165 
166   std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
167       rtc::ArrayView<const uint16_t> sequence_numbers) const override;
168 
169   size_t ExpectedPerPacketOverhead() const override;
170 
171   void OnPacketSendingThreadSwitched() override;
172 
173   // RTCP part.
174 
175   // Get RTCP status.
176   RtcpMode RTCP() const override;
177 
178   // Configure RTCP status i.e on/off.
179   void SetRTCPStatus(RtcpMode method) override;
180 
181   // Set RTCP CName.
182   int32_t SetCNAME(absl::string_view c_name) override;
183 
184   // Get remote NTP.
185   int32_t RemoteNTP(uint32_t* received_ntp_secs,
186                     uint32_t* received_ntp_frac,
187                     uint32_t* rtcp_arrival_time_secs,
188                     uint32_t* rtcp_arrival_time_frac,
189                     uint32_t* rtcp_timestamp) const override;
190 
191   // Get RoundTripTime.
192   int32_t RTT(uint32_t remote_ssrc,
193               int64_t* rtt,
194               int64_t* avg_rtt,
195               int64_t* min_rtt,
196               int64_t* max_rtt) const override;
197 
198   int64_t ExpectedRetransmissionTimeMs() const override;
199 
200   // Force a send of an RTCP packet.
201   // Normal SR and RR are triggered via the task queue that's current when this
202   // object is created.
203   int32_t SendRTCP(RTCPPacketType rtcpPacketType) override;
204 
205   void GetSendStreamDataCounters(
206       StreamDataCounters* rtp_counters,
207       StreamDataCounters* rtx_counters) const override;
208 
209   // A snapshot of the most recent Report Block with additional data of
210   // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
211   // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(),
212   // which is the SSRC of the corresponding outbound RTP stream, is unique.
213   std::vector<ReportBlockData> GetLatestReportBlockData() const override;
214   absl::optional<SenderReportStats> GetSenderReportStats() const override;
215   absl::optional<NonSenderRttStats> GetNonSenderRttStats() const override;
216 
217   // (REMB) Receiver Estimated Max Bitrate.
218   void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
219   void UnsetRemb() override;
220 
221   void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
222 
223   size_t MaxRtpPacketSize() const override;
224 
225   void SetMaxRtpPacketSize(size_t max_packet_size) override;
226 
227   // (NACK) Negative acknowledgment part.
228 
229   // Send a Negative acknowledgment packet.
230   // TODO(philipel): Deprecate SendNACK and use SendNack instead.
231   int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
232 
233   void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
234 
235   // Store the sent packets, needed to answer to a negative acknowledgment
236   // requests.
237   void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
238 
239   void SendCombinedRtcpPacket(
240       std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
241 
242   // Video part.
243   int32_t SendLossNotification(uint16_t last_decoded_seq_num,
244                                uint16_t last_received_seq_num,
245                                bool decodability_flag,
246                                bool buffering_allowed) override;
247 
248   RtpSendRates GetSendRates() const override;
249 
250   void OnReceivedNack(
251       const std::vector<uint16_t>& nack_sequence_numbers) override;
252   void OnReceivedRtcpReportBlocks(
253       const ReportBlockList& report_blocks) override;
254   void OnRequestSendReport() override;
255 
256   void SetVideoBitrateAllocation(
257       const VideoBitrateAllocation& bitrate) override;
258 
259   RTPSender* RtpSender() override;
260   const RTPSender* RtpSender() const override;
261 
262  private:
263   FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt);
264   FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly);
265 
266   struct RtpSenderContext {
267     explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config);
268     // Storage of packets, for retransmissions and padding, if applicable.
269     RtpPacketHistory packet_history;
270     SequenceChecker sequencing_checker;
271     // Handles sequence number assignment and padding timestamp generation.
272     PacketSequencer sequencer RTC_GUARDED_BY(sequencing_checker);
273     // Handles final time timestamping/stats/etc and handover to Transport.
274     RtpSenderEgress packet_sender;
275     // If no paced sender configured, this class will be used to pass packets
276     // from `packet_generator_` to `packet_sender_`.
277     RtpSenderEgress::NonPacedPacketSender non_paced_sender;
278     // Handles creation of RTP packets to be sent.
279     RTPSender packet_generator;
280   };
281 
282   void set_rtt_ms(int64_t rtt_ms);
283   int64_t rtt_ms() const;
284 
285   bool TimeToSendFullNackList(int64_t now) const;
286 
287   // Called on a timer, once a second, on the worker_queue_, to update the RTT,
288   // check if we need to send RTCP report, send TMMBR updates and fire events.
289   void PeriodicUpdate();
290 
291   // Returns true if the module is configured to store packets.
292   bool StorePackets() const;
293 
294   // Used from RtcpSenderMediator to maybe send rtcp.
295   void MaybeSendRtcp() RTC_RUN_ON(worker_queue_);
296 
297   // Called when `rtcp_sender_` informs of the next RTCP instant. The method may
298   // be called on various sequences, and is called under a RTCPSenderLock.
299   void ScheduleRtcpSendEvaluation(TimeDelta duration);
300 
301   // Helper method combating too early delayed calls from task queues.
302   // TODO(bugs.webrtc.org/12889): Consider removing this function when the issue
303   // is resolved.
304   void MaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time)
305       RTC_RUN_ON(worker_queue_);
306 
307   // Schedules a call to MaybeSendRtcpAtOrAfterTimestamp delayed by `duration`.
308   void ScheduleMaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time,
309                                                TimeDelta duration);
310 
311   TaskQueueBase* const worker_queue_;
312   RTC_NO_UNIQUE_ADDRESS SequenceChecker rtcp_thread_checker_;
313 
314   std::unique_ptr<RtpSenderContext> rtp_sender_;
315   RTCPSender rtcp_sender_;
316   RTCPReceiver rtcp_receiver_;
317 
318   Clock* const clock_;
319 
320   uint16_t packet_overhead_;
321 
322   // Send side
323   int64_t nack_last_time_sent_full_ms_;
324   uint16_t nack_last_seq_number_sent_;
325 
326   RtcpRttStats* const rtt_stats_;
327   RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_);
328 
329   // The processed RTT from RtcpRttStats.
330   mutable Mutex mutex_rtt_;
331   int64_t rtt_ms_ RTC_GUARDED_BY(mutex_rtt_);
332 
333   RTC_NO_UNIQUE_ADDRESS ScopedTaskSafety task_safety_;
334 };
335 
336 }  // namespace webrtc
337 
338 #endif  // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_
339