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 void RemoteRTCPSenderInfo(uint32_t* packet_count, 203 uint32_t* octet_count, 204 int64_t* ntp_timestamp_ms, 205 int64_t* remote_ntp_timestamp_ms) const override; 206 207 // Get received RTCP report, report block. 208 int32_t RemoteRTCPStat( 209 std::vector<RTCPReportBlock>* receive_blocks) const override; 210 // A snapshot of the most recent Report Block with additional data of 211 // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats. 212 // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(), 213 // which is the SSRC of the corresponding outbound RTP stream, is unique. 214 std::vector<ReportBlockData> GetLatestReportBlockData() const override; 215 absl::optional<SenderReportStats> GetSenderReportStats() 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 bool StorePackets() const override; 240 241 void SendCombinedRtcpPacket( 242 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override; 243 244 // (XR) Receiver reference time report. 245 void SetRtcpXrRrtrStatus(bool enable) override; 246 247 bool RtcpXrRrtrStatus() const override; 248 249 // Video part. 250 int32_t SendLossNotification(uint16_t last_decoded_seq_num, 251 uint16_t last_received_seq_num, 252 bool decodability_flag, 253 bool buffering_allowed) override; 254 255 void BitrateSent(uint32_t* total_rate, 256 uint32_t* video_rate, 257 uint32_t* fec_rate, 258 uint32_t* nackRate) const override; 259 260 RtpSendRates GetSendRates() const override; 261 262 void OnReceivedNack( 263 const std::vector<uint16_t>& nack_sequence_numbers) override; 264 void OnReceivedRtcpReportBlocks( 265 const ReportBlockList& report_blocks) override; 266 void OnRequestSendReport() override; 267 268 void SetVideoBitrateAllocation( 269 const VideoBitrateAllocation& bitrate) override; 270 271 RTPSender* RtpSender() override; 272 const RTPSender* RtpSender() const override; 273 274 private: 275 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt); 276 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly); 277 278 struct RtpSenderContext : public SequenceNumberAssigner { 279 explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config); 280 void AssignSequenceNumber(RtpPacketToSend* packet) override; 281 // Storage of packets, for retransmissions and padding, if applicable. 282 RtpPacketHistory packet_history; 283 // Handles final time timestamping/stats/etc and handover to Transport. 284 RtpSenderEgress packet_sender; 285 // If no paced sender configured, this class will be used to pass packets 286 // from |packet_generator_| to |packet_sender_|. 287 RtpSenderEgress::NonPacedPacketSender non_paced_sender; 288 // Handles creation of RTP packets to be sent. 289 RTPSender packet_generator; 290 }; 291 292 void set_rtt_ms(int64_t rtt_ms); 293 int64_t rtt_ms() const; 294 295 bool TimeToSendFullNackList(int64_t now) const; 296 297 // Called on a timer, once a second, on the worker_queue_, to update the RTT, 298 // check if we need to send RTCP report, send TMMBR updates and fire events. 299 void PeriodicUpdate(); 300 301 TaskQueueBase* const worker_queue_; 302 SequenceChecker process_thread_checker_; 303 304 std::unique_ptr<RtpSenderContext> rtp_sender_; 305 306 RTCPSender rtcp_sender_; 307 RTCPReceiver rtcp_receiver_; 308 309 Clock* const clock_; 310 311 int64_t last_rtt_process_time_; 312 int64_t next_process_time_; 313 uint16_t packet_overhead_; 314 315 // Send side 316 int64_t nack_last_time_sent_full_ms_; 317 uint16_t nack_last_seq_number_sent_; 318 319 RemoteBitrateEstimator* const remote_bitrate_; 320 321 RtcpRttStats* const rtt_stats_; 322 RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_); 323 324 // The processed RTT from RtcpRttStats. 325 mutable Mutex mutex_rtt_; 326 int64_t rtt_ms_; 327 }; 328 329 } // namespace webrtc 330 331 #endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_ 332