Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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" |
Tomas Gunnarsson | 473bbd8 | 2020-06-27 17:44:55 +0200 | [diff] [blame] | 24 | #include "api/task_queue/task_queue_base.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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" |
Tomas Gunnarsson | fae0562 | 2020-06-03 08:54:39 +0200 | [diff] [blame] | 34 | #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 35 | #include "modules/rtp_rtcp/source/rtp_sender.h" |
| 36 | #include "modules/rtp_rtcp/source/rtp_sender_egress.h" |
| 37 | #include "rtc_base/critical_section.h" |
| 38 | #include "rtc_base/gtest_prod_util.h" |
| 39 | #include "rtc_base/synchronization/sequence_checker.h" |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 40 | #include "rtc_base/task_utils/pending_task_safety_flag.h" |
| 41 | #include "rtc_base/task_utils/repeating_task.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 42 | |
| 43 | namespace webrtc { |
| 44 | |
| 45 | class Clock; |
| 46 | struct PacedPacketInfo; |
| 47 | struct RTPVideoHeader; |
| 48 | |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 49 | class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface, |
| 50 | public Module, |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 51 | public RTCPReceiver::ModuleRtpRtcp { |
| 52 | public: |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 53 | explicit ModuleRtpRtcpImpl2( |
| 54 | const RtpRtcpInterface::Configuration& configuration); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 55 | ~ModuleRtpRtcpImpl2() override; |
| 56 | |
Tomas Gunnarsson | fae0562 | 2020-06-03 08:54:39 +0200 | [diff] [blame] | 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. |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 61 | static std::unique_ptr<ModuleRtpRtcpImpl2> Create( |
| 62 | const Configuration& configuration); |
Tomas Gunnarsson | fae0562 | 2020-06-03 08:54:39 +0200 | [diff] [blame] | 63 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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 | |
| 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 OnPacketsAcknowledged( |
| 152 | rtc::ArrayView<const uint16_t> sequence_numbers) override; |
| 153 | |
| 154 | std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding( |
| 155 | size_t target_size_bytes) override; |
| 156 | |
| 157 | std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos( |
| 158 | rtc::ArrayView<const uint16_t> sequence_numbers) const override; |
| 159 | |
| 160 | size_t ExpectedPerPacketOverhead() const override; |
| 161 | |
| 162 | // RTCP part. |
| 163 | |
| 164 | // Get RTCP status. |
| 165 | RtcpMode RTCP() const override; |
| 166 | |
| 167 | // Configure RTCP status i.e on/off. |
| 168 | void SetRTCPStatus(RtcpMode method) override; |
| 169 | |
| 170 | // Set RTCP CName. |
| 171 | int32_t SetCNAME(const char* c_name) override; |
| 172 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 180 | // Get RoundTripTime. |
| 181 | int32_t RTT(uint32_t remote_ssrc, |
| 182 | int64_t* rtt, |
| 183 | int64_t* avg_rtt, |
| 184 | int64_t* min_rtt, |
| 185 | int64_t* max_rtt) const override; |
| 186 | |
| 187 | int64_t ExpectedRetransmissionTimeMs() const override; |
| 188 | |
| 189 | // Force a send of an RTCP packet. |
| 190 | // Normal SR and RR are triggered via the process function. |
| 191 | int32_t SendRTCP(RTCPPacketType rtcpPacketType) override; |
| 192 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 193 | void GetSendStreamDataCounters( |
| 194 | StreamDataCounters* rtp_counters, |
| 195 | StreamDataCounters* rtx_counters) const override; |
| 196 | |
| 197 | // Get received RTCP report, report block. |
| 198 | int32_t RemoteRTCPStat( |
| 199 | std::vector<RTCPReportBlock>* receive_blocks) const override; |
| 200 | // A snapshot of the most recent Report Block with additional data of |
| 201 | // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats. |
| 202 | // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(), |
| 203 | // which is the SSRC of the corresponding outbound RTP stream, is unique. |
| 204 | std::vector<ReportBlockData> GetLatestReportBlockData() const override; |
| 205 | |
| 206 | // (REMB) Receiver Estimated Max Bitrate. |
| 207 | void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override; |
| 208 | void UnsetRemb() override; |
| 209 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 210 | void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override; |
| 211 | |
| 212 | size_t MaxRtpPacketSize() const override; |
| 213 | |
| 214 | void SetMaxRtpPacketSize(size_t max_packet_size) override; |
| 215 | |
| 216 | // (NACK) Negative acknowledgment part. |
| 217 | |
| 218 | // Send a Negative acknowledgment packet. |
| 219 | // TODO(philipel): Deprecate SendNACK and use SendNack instead. |
| 220 | int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override; |
| 221 | |
| 222 | void SendNack(const std::vector<uint16_t>& sequence_numbers) override; |
| 223 | |
| 224 | // Store the sent packets, needed to answer to a negative acknowledgment |
| 225 | // requests. |
| 226 | void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override; |
| 227 | |
| 228 | bool StorePackets() const override; |
| 229 | |
| 230 | void SendCombinedRtcpPacket( |
| 231 | std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override; |
| 232 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 233 | // (XR) Receiver reference time report. |
| 234 | void SetRtcpXrRrtrStatus(bool enable) override; |
| 235 | |
| 236 | bool RtcpXrRrtrStatus() const override; |
| 237 | |
| 238 | // Video part. |
| 239 | int32_t SendLossNotification(uint16_t last_decoded_seq_num, |
| 240 | uint16_t last_received_seq_num, |
| 241 | bool decodability_flag, |
| 242 | bool buffering_allowed) override; |
| 243 | |
| 244 | bool LastReceivedNTP(uint32_t* NTPsecs, |
| 245 | uint32_t* NTPfrac, |
| 246 | uint32_t* remote_sr) const; |
| 247 | |
| 248 | void BitrateSent(uint32_t* total_rate, |
| 249 | uint32_t* video_rate, |
| 250 | uint32_t* fec_rate, |
| 251 | uint32_t* nackRate) const override; |
| 252 | |
| 253 | RtpSendRates GetSendRates() const override; |
| 254 | |
| 255 | void OnReceivedNack( |
| 256 | const std::vector<uint16_t>& nack_sequence_numbers) override; |
| 257 | void OnReceivedRtcpReportBlocks( |
| 258 | const ReportBlockList& report_blocks) override; |
| 259 | void OnRequestSendReport() override; |
| 260 | |
| 261 | void SetVideoBitrateAllocation( |
| 262 | const VideoBitrateAllocation& bitrate) override; |
| 263 | |
| 264 | RTPSender* RtpSender() override; |
| 265 | const RTPSender* RtpSender() const override; |
| 266 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 267 | private: |
| 268 | FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt); |
| 269 | FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly); |
| 270 | |
Erik Språng | a1888ae | 2020-07-02 12:02:36 +0000 | [diff] [blame^] | 271 | struct RtpSenderContext { |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 272 | explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 273 | // Storage of packets, for retransmissions and padding, if applicable. |
| 274 | RtpPacketHistory packet_history; |
| 275 | // Handles final time timestamping/stats/etc and handover to Transport. |
| 276 | RtpSenderEgress packet_sender; |
| 277 | // If no paced sender configured, this class will be used to pass packets |
| 278 | // from |packet_generator_| to |packet_sender_|. |
| 279 | RtpSenderEgress::NonPacedPacketSender non_paced_sender; |
| 280 | // Handles creation of RTP packets to be sent. |
| 281 | RTPSender packet_generator; |
| 282 | }; |
| 283 | |
| 284 | void set_rtt_ms(int64_t rtt_ms); |
| 285 | int64_t rtt_ms() const; |
| 286 | |
| 287 | bool TimeToSendFullNackList(int64_t now) const; |
| 288 | |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 289 | // Called on a timer, once a second, on the worker_queue_, to update the RTT, |
| 290 | // check if we need to send RTCP report, send TMMBR updates and fire events. |
| 291 | void PeriodicUpdate(); |
| 292 | |
Tomas Gunnarsson | 473bbd8 | 2020-06-27 17:44:55 +0200 | [diff] [blame] | 293 | TaskQueueBase* const worker_queue_; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 294 | SequenceChecker process_thread_checker_; |
| 295 | |
| 296 | std::unique_ptr<RtpSenderContext> rtp_sender_; |
| 297 | |
| 298 | RTCPSender rtcp_sender_; |
| 299 | RTCPReceiver rtcp_receiver_; |
| 300 | |
| 301 | Clock* const clock_; |
| 302 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 303 | int64_t last_rtt_process_time_; |
| 304 | int64_t next_process_time_; |
| 305 | uint16_t packet_overhead_; |
| 306 | |
| 307 | // Send side |
| 308 | int64_t nack_last_time_sent_full_ms_; |
| 309 | uint16_t nack_last_seq_number_sent_; |
| 310 | |
| 311 | RemoteBitrateEstimator* const remote_bitrate_; |
| 312 | |
| 313 | RtcpRttStats* const rtt_stats_; |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 314 | RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 315 | |
| 316 | // The processed RTT from RtcpRttStats. |
| 317 | rtc::CriticalSection critical_section_rtt_; |
| 318 | int64_t rtt_ms_; |
| 319 | }; |
| 320 | |
| 321 | } // namespace webrtc |
| 322 | |
| 323 | #endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_ |