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" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 24 | #include "api/sequence_checker.h" |
Tomas Gunnarsson | 473bbd8 | 2020-06-27 17:44:55 +0200 | [diff] [blame] | 25 | #include "api/task_queue/task_queue_base.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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" |
Tomas Gunnarsson | fae0562 | 2020-06-03 08:54:39 +0200 | [diff] [blame] | 35 | #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 36 | #include "modules/rtp_rtcp/source/rtp_sender.h" |
| 37 | #include "modules/rtp_rtcp/source/rtp_sender_egress.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 38 | #include "rtc_base/gtest_prod_util.h" |
Markus Handell | f7303e6 | 2020-07-09 01:34:42 +0200 | [diff] [blame] | 39 | #include "rtc_base/synchronization/mutex.h" |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 40 | #include "rtc_base/system/no_unique_address.h" |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 41 | #include "rtc_base/task_utils/pending_task_safety_flag.h" |
| 42 | #include "rtc_base/task_utils/repeating_task.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 43 | |
| 44 | namespace webrtc { |
| 45 | |
| 46 | class Clock; |
| 47 | struct PacedPacketInfo; |
| 48 | struct RTPVideoHeader; |
| 49 | |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 50 | class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface, |
| 51 | public Module, |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 52 | public RTCPReceiver::ModuleRtpRtcp { |
| 53 | public: |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 54 | explicit ModuleRtpRtcpImpl2( |
| 55 | const RtpRtcpInterface::Configuration& configuration); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 56 | ~ModuleRtpRtcpImpl2() override; |
| 57 | |
Niels Moller | 2accc7d | 2021-01-12 15:54:16 +0000 | [diff] [blame] | 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 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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 | |
Tommi | 08be9ba | 2021-06-15 23:01:57 +0200 | [diff] [blame^] | 80 | void SetLocalSsrc(uint32_t local_ssrc) override; |
| 81 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 82 | // Sender part. |
| 83 | void RegisterSendPayloadFrequency(int payload_type, |
| 84 | int payload_frequency) override; |
| 85 | |
| 86 | int32_t DeRegisterSendPayload(int8_t payload_type) override; |
| 87 | |
| 88 | void SetExtmapAllowMixed(bool extmap_allow_mixed) override; |
| 89 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 90 | void RegisterRtpHeaderExtension(absl::string_view uri, int id) override; |
| 91 | int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override; |
| 92 | void DeregisterSendRtpHeaderExtension(absl::string_view uri) override; |
| 93 | |
| 94 | bool SupportsPadding() const override; |
| 95 | bool SupportsRtxPayloadPadding() const override; |
| 96 | |
| 97 | // Get start timestamp. |
| 98 | uint32_t StartTimestamp() const override; |
| 99 | |
| 100 | // Configure start timestamp, default is a random number. |
| 101 | void SetStartTimestamp(uint32_t timestamp) override; |
| 102 | |
| 103 | uint16_t SequenceNumber() const override; |
| 104 | |
| 105 | // Set SequenceNumber, default is a random number. |
| 106 | void SetSequenceNumber(uint16_t seq) override; |
| 107 | |
| 108 | void SetRtpState(const RtpState& rtp_state) override; |
| 109 | void SetRtxState(const RtpState& rtp_state) override; |
| 110 | RtpState GetRtpState() const override; |
| 111 | RtpState GetRtxState() const override; |
| 112 | |
| 113 | uint32_t SSRC() const override { return rtcp_sender_.SSRC(); } |
| 114 | |
Tommi | 08be9ba | 2021-06-15 23:01:57 +0200 | [diff] [blame^] | 115 | // Semantically identical to `SSRC()` but must be called on the packet |
| 116 | // delivery thread/tq and returns the ssrc that maps to |
| 117 | // RtpRtcpInterface::Configuration::local_media_ssrc. |
| 118 | uint32_t local_media_ssrc() const; |
| 119 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 120 | void SetRid(const std::string& rid) override; |
| 121 | |
| 122 | void SetMid(const std::string& mid) override; |
| 123 | |
| 124 | void SetCsrcs(const std::vector<uint32_t>& csrcs) override; |
| 125 | |
| 126 | RTCPSender::FeedbackState GetFeedbackState(); |
| 127 | |
| 128 | void SetRtxSendStatus(int mode) override; |
| 129 | int RtxSendStatus() const override; |
| 130 | absl::optional<uint32_t> RtxSsrc() const override; |
| 131 | |
| 132 | void SetRtxSendPayloadType(int payload_type, |
| 133 | int associated_payload_type) override; |
| 134 | |
| 135 | absl::optional<uint32_t> FlexfecSsrc() const override; |
| 136 | |
| 137 | // Sends kRtcpByeCode when going from true to false. |
| 138 | int32_t SetSendingStatus(bool sending) override; |
| 139 | |
| 140 | bool Sending() const override; |
| 141 | |
| 142 | // Drops or relays media packets. |
| 143 | void SetSendingMediaStatus(bool sending) override; |
| 144 | |
| 145 | bool SendingMedia() const override; |
| 146 | |
| 147 | bool IsAudioConfigured() const override; |
| 148 | |
| 149 | void SetAsPartOfAllocation(bool part_of_allocation) override; |
| 150 | |
| 151 | bool OnSendingRtpFrame(uint32_t timestamp, |
| 152 | int64_t capture_time_ms, |
| 153 | int payload_type, |
| 154 | bool force_sender_report) override; |
| 155 | |
| 156 | bool TrySendPacket(RtpPacketToSend* packet, |
| 157 | const PacedPacketInfo& pacing_info) override; |
| 158 | |
Erik Språng | 1d50cb6 | 2020-07-02 17:41:32 +0200 | [diff] [blame] | 159 | void SetFecProtectionParams(const FecProtectionParams& delta_params, |
| 160 | const FecProtectionParams& key_params) override; |
| 161 | |
| 162 | std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override; |
| 163 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 164 | void OnPacketsAcknowledged( |
| 165 | rtc::ArrayView<const uint16_t> sequence_numbers) override; |
| 166 | |
| 167 | std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding( |
| 168 | size_t target_size_bytes) override; |
| 169 | |
| 170 | std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos( |
| 171 | rtc::ArrayView<const uint16_t> sequence_numbers) const override; |
| 172 | |
| 173 | size_t ExpectedPerPacketOverhead() const override; |
| 174 | |
| 175 | // RTCP part. |
| 176 | |
| 177 | // Get RTCP status. |
| 178 | RtcpMode RTCP() const override; |
| 179 | |
| 180 | // Configure RTCP status i.e on/off. |
| 181 | void SetRTCPStatus(RtcpMode method) override; |
| 182 | |
| 183 | // Set RTCP CName. |
| 184 | int32_t SetCNAME(const char* c_name) override; |
| 185 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 186 | // Get remote NTP. |
| 187 | int32_t RemoteNTP(uint32_t* received_ntp_secs, |
| 188 | uint32_t* received_ntp_frac, |
| 189 | uint32_t* rtcp_arrival_time_secs, |
| 190 | uint32_t* rtcp_arrival_time_frac, |
| 191 | uint32_t* rtcp_timestamp) const override; |
| 192 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 193 | // Get RoundTripTime. |
| 194 | int32_t RTT(uint32_t remote_ssrc, |
| 195 | int64_t* rtt, |
| 196 | int64_t* avg_rtt, |
| 197 | int64_t* min_rtt, |
| 198 | int64_t* max_rtt) const override; |
| 199 | |
| 200 | int64_t ExpectedRetransmissionTimeMs() const override; |
| 201 | |
| 202 | // Force a send of an RTCP packet. |
| 203 | // Normal SR and RR are triggered via the process function. |
| 204 | int32_t SendRTCP(RTCPPacketType rtcpPacketType) override; |
| 205 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 206 | void GetSendStreamDataCounters( |
| 207 | StreamDataCounters* rtp_counters, |
| 208 | StreamDataCounters* rtx_counters) const override; |
| 209 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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; |
Alessio Bazzica | bc1c93d | 2021-03-12 17:45:26 +0100 | [diff] [blame] | 215 | absl::optional<SenderReportStats> GetSenderReportStats() const override; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 239 | void SendCombinedRtcpPacket( |
| 240 | std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override; |
| 241 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 242 | // Video part. |
| 243 | int32_t SendLossNotification(uint16_t last_decoded_seq_num, |
| 244 | uint16_t last_received_seq_num, |
| 245 | bool decodability_flag, |
| 246 | bool buffering_allowed) override; |
| 247 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 248 | RtpSendRates GetSendRates() const override; |
| 249 | |
| 250 | void OnReceivedNack( |
| 251 | const std::vector<uint16_t>& nack_sequence_numbers) override; |
| 252 | void OnReceivedRtcpReportBlocks( |
| 253 | const ReportBlockList& report_blocks) override; |
| 254 | void OnRequestSendReport() override; |
| 255 | |
| 256 | void SetVideoBitrateAllocation( |
| 257 | const VideoBitrateAllocation& bitrate) override; |
| 258 | |
| 259 | RTPSender* RtpSender() override; |
| 260 | const RTPSender* RtpSender() const override; |
| 261 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 262 | private: |
| 263 | FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt); |
| 264 | FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly); |
| 265 | |
Erik Språng | 1d50cb6 | 2020-07-02 17:41:32 +0200 | [diff] [blame] | 266 | struct RtpSenderContext : public SequenceNumberAssigner { |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 267 | explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config); |
Erik Språng | 1d50cb6 | 2020-07-02 17:41:32 +0200 | [diff] [blame] | 268 | void AssignSequenceNumber(RtpPacketToSend* packet) override; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 269 | // Storage of packets, for retransmissions and padding, if applicable. |
| 270 | RtpPacketHistory packet_history; |
| 271 | // Handles final time timestamping/stats/etc and handover to Transport. |
| 272 | RtpSenderEgress packet_sender; |
| 273 | // If no paced sender configured, this class will be used to pass packets |
| 274 | // from |packet_generator_| to |packet_sender_|. |
| 275 | RtpSenderEgress::NonPacedPacketSender non_paced_sender; |
| 276 | // Handles creation of RTP packets to be sent. |
| 277 | RTPSender packet_generator; |
| 278 | }; |
| 279 | |
| 280 | void set_rtt_ms(int64_t rtt_ms); |
| 281 | int64_t rtt_ms() const; |
| 282 | |
| 283 | bool TimeToSendFullNackList(int64_t now) const; |
| 284 | |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 285 | // Called on a timer, once a second, on the worker_queue_, to update the RTT, |
| 286 | // check if we need to send RTCP report, send TMMBR updates and fire events. |
| 287 | void PeriodicUpdate(); |
| 288 | |
Niels Möller | af6ea0c | 2020-11-20 12:21:21 +0100 | [diff] [blame] | 289 | // Returns true if the module is configured to store packets. |
| 290 | bool StorePackets() const; |
| 291 | |
Tomas Gunnarsson | 473bbd8 | 2020-06-27 17:44:55 +0200 | [diff] [blame] | 292 | TaskQueueBase* const worker_queue_; |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 293 | RTC_NO_UNIQUE_ADDRESS SequenceChecker process_thread_checker_; |
Tommi | 08be9ba | 2021-06-15 23:01:57 +0200 | [diff] [blame^] | 294 | RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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. |
Markus Handell | f7303e6 | 2020-07-09 01:34:42 +0200 | [diff] [blame] | 317 | mutable Mutex mutex_rtt_; |
Niels Möller | cd98213 | 2020-11-26 16:19:56 +0100 | [diff] [blame] | 318 | int64_t rtt_ms_ RTC_GUARDED_BY(mutex_rtt_); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | } // namespace webrtc |
| 322 | |
| 323 | #endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_ |