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