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