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