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" |
Markus Handell | 885d538 | 2021-06-21 18:57:36 +0200 | [diff] [blame] | 26 | #include "api/units/time_delta.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 27 | #include "api/video/video_bitrate_allocation.h" |
| 28 | #include "modules/include/module_fec_types.h" |
| 29 | #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 30 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" // RTCPPacketType |
| 31 | #include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h" |
| 32 | #include "modules/rtp_rtcp/source/rtcp_receiver.h" |
| 33 | #include "modules/rtp_rtcp/source/rtcp_sender.h" |
| 34 | #include "modules/rtp_rtcp/source/rtp_packet_history.h" |
| 35 | #include "modules/rtp_rtcp/source/rtp_packet_to_send.h" |
| 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" |
Markus Handell | 885d538 | 2021-06-21 18:57:36 +0200 | [diff] [blame] | 43 | #include "rtc_base/task_utils/to_queued_task.h" |
| 44 | #include "rtc_base/thread_annotations.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 45 | |
| 46 | namespace webrtc { |
| 47 | |
| 48 | class Clock; |
| 49 | struct PacedPacketInfo; |
| 50 | struct RTPVideoHeader; |
| 51 | |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 52 | class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface, |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 53 | public RTCPReceiver::ModuleRtpRtcp { |
| 54 | public: |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 55 | explicit ModuleRtpRtcpImpl2( |
| 56 | const RtpRtcpInterface::Configuration& configuration); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 57 | ~ModuleRtpRtcpImpl2() override; |
| 58 | |
Niels Moller | 2accc7d | 2021-01-12 15:54:16 +0000 | [diff] [blame] | 59 | // This method is provided to easy with migrating away from the |
| 60 | // RtpRtcp::Create factory method. Since this is an internal implementation |
| 61 | // detail though, creating an instance of ModuleRtpRtcpImpl2 directly should |
| 62 | // be fine. |
| 63 | static std::unique_ptr<ModuleRtpRtcpImpl2> Create( |
| 64 | const Configuration& configuration); |
| 65 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 66 | // Receiver part. |
| 67 | |
| 68 | // Called when we receive an RTCP packet. |
| 69 | void IncomingRtcpPacket(const uint8_t* incoming_packet, |
| 70 | size_t incoming_packet_length) override; |
| 71 | |
| 72 | void SetRemoteSSRC(uint32_t ssrc) override; |
| 73 | |
Tommi | 08be9ba | 2021-06-15 23:01:57 +0200 | [diff] [blame] | 74 | void SetLocalSsrc(uint32_t local_ssrc) override; |
| 75 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 76 | // Sender part. |
| 77 | void RegisterSendPayloadFrequency(int payload_type, |
| 78 | int payload_frequency) override; |
| 79 | |
| 80 | int32_t DeRegisterSendPayload(int8_t payload_type) override; |
| 81 | |
| 82 | void SetExtmapAllowMixed(bool extmap_allow_mixed) override; |
| 83 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 84 | void RegisterRtpHeaderExtension(absl::string_view uri, int id) override; |
| 85 | int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override; |
| 86 | void DeregisterSendRtpHeaderExtension(absl::string_view uri) override; |
| 87 | |
| 88 | bool SupportsPadding() const override; |
| 89 | bool SupportsRtxPayloadPadding() const override; |
| 90 | |
| 91 | // Get start timestamp. |
| 92 | uint32_t StartTimestamp() const override; |
| 93 | |
| 94 | // Configure start timestamp, default is a random number. |
| 95 | void SetStartTimestamp(uint32_t timestamp) override; |
| 96 | |
| 97 | uint16_t SequenceNumber() const override; |
| 98 | |
| 99 | // Set SequenceNumber, default is a random number. |
| 100 | void SetSequenceNumber(uint16_t seq) override; |
| 101 | |
| 102 | void SetRtpState(const RtpState& rtp_state) override; |
| 103 | void SetRtxState(const RtpState& rtp_state) override; |
| 104 | RtpState GetRtpState() const override; |
| 105 | RtpState GetRtxState() const override; |
| 106 | |
| 107 | uint32_t SSRC() const override { return rtcp_sender_.SSRC(); } |
| 108 | |
Tommi | 08be9ba | 2021-06-15 23:01:57 +0200 | [diff] [blame] | 109 | // Semantically identical to `SSRC()` but must be called on the packet |
| 110 | // delivery thread/tq and returns the ssrc that maps to |
| 111 | // RtpRtcpInterface::Configuration::local_media_ssrc. |
| 112 | uint32_t local_media_ssrc() const; |
| 113 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 114 | void SetRid(const std::string& rid) override; |
| 115 | |
| 116 | void SetMid(const std::string& mid) override; |
| 117 | |
| 118 | void SetCsrcs(const std::vector<uint32_t>& csrcs) override; |
| 119 | |
| 120 | RTCPSender::FeedbackState GetFeedbackState(); |
| 121 | |
| 122 | void SetRtxSendStatus(int mode) override; |
| 123 | int RtxSendStatus() const override; |
| 124 | absl::optional<uint32_t> RtxSsrc() const override; |
| 125 | |
| 126 | void SetRtxSendPayloadType(int payload_type, |
| 127 | int associated_payload_type) override; |
| 128 | |
| 129 | absl::optional<uint32_t> FlexfecSsrc() const override; |
| 130 | |
| 131 | // Sends kRtcpByeCode when going from true to false. |
| 132 | int32_t SetSendingStatus(bool sending) override; |
| 133 | |
| 134 | bool Sending() const override; |
| 135 | |
| 136 | // Drops or relays media packets. |
| 137 | void SetSendingMediaStatus(bool sending) override; |
| 138 | |
| 139 | bool SendingMedia() const override; |
| 140 | |
| 141 | bool IsAudioConfigured() const override; |
| 142 | |
| 143 | void SetAsPartOfAllocation(bool part_of_allocation) override; |
| 144 | |
| 145 | bool OnSendingRtpFrame(uint32_t timestamp, |
| 146 | int64_t capture_time_ms, |
| 147 | int payload_type, |
| 148 | bool force_sender_report) override; |
| 149 | |
| 150 | bool TrySendPacket(RtpPacketToSend* packet, |
| 151 | const PacedPacketInfo& pacing_info) override; |
| 152 | |
Erik Språng | 1d50cb6 | 2020-07-02 17:41:32 +0200 | [diff] [blame] | 153 | void SetFecProtectionParams(const FecProtectionParams& delta_params, |
| 154 | const FecProtectionParams& key_params) override; |
| 155 | |
| 156 | std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override; |
| 157 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 158 | void OnPacketsAcknowledged( |
| 159 | rtc::ArrayView<const uint16_t> sequence_numbers) override; |
| 160 | |
| 161 | std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding( |
| 162 | size_t target_size_bytes) override; |
| 163 | |
| 164 | std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos( |
| 165 | rtc::ArrayView<const uint16_t> sequence_numbers) const override; |
| 166 | |
| 167 | size_t ExpectedPerPacketOverhead() const override; |
| 168 | |
| 169 | // RTCP part. |
| 170 | |
| 171 | // Get RTCP status. |
| 172 | RtcpMode RTCP() const override; |
| 173 | |
| 174 | // Configure RTCP status i.e on/off. |
| 175 | void SetRTCPStatus(RtcpMode method) override; |
| 176 | |
| 177 | // Set RTCP CName. |
| 178 | int32_t SetCNAME(const char* c_name) override; |
| 179 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 180 | // Get remote NTP. |
| 181 | int32_t RemoteNTP(uint32_t* received_ntp_secs, |
| 182 | uint32_t* received_ntp_frac, |
| 183 | uint32_t* rtcp_arrival_time_secs, |
| 184 | uint32_t* rtcp_arrival_time_frac, |
| 185 | uint32_t* rtcp_timestamp) const override; |
| 186 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 187 | // Get RoundTripTime. |
| 188 | int32_t RTT(uint32_t remote_ssrc, |
| 189 | int64_t* rtt, |
| 190 | int64_t* avg_rtt, |
| 191 | int64_t* min_rtt, |
| 192 | int64_t* max_rtt) const override; |
| 193 | |
| 194 | int64_t ExpectedRetransmissionTimeMs() const override; |
| 195 | |
| 196 | // Force a send of an RTCP packet. |
Markus Handell | 885d538 | 2021-06-21 18:57:36 +0200 | [diff] [blame] | 197 | // Normal SR and RR are triggered via the task queue that's current when this |
| 198 | // object is created. |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 199 | int32_t SendRTCP(RTCPPacketType rtcpPacketType) override; |
| 200 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 201 | void GetSendStreamDataCounters( |
| 202 | StreamDataCounters* rtp_counters, |
| 203 | StreamDataCounters* rtx_counters) const override; |
| 204 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 205 | // A snapshot of the most recent Report Block with additional data of |
| 206 | // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats. |
| 207 | // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(), |
| 208 | // which is the SSRC of the corresponding outbound RTP stream, is unique. |
| 209 | std::vector<ReportBlockData> GetLatestReportBlockData() const override; |
Alessio Bazzica | bc1c93d | 2021-03-12 17:45:26 +0100 | [diff] [blame] | 210 | absl::optional<SenderReportStats> GetSenderReportStats() const override; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 211 | |
| 212 | // (REMB) Receiver Estimated Max Bitrate. |
| 213 | void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override; |
| 214 | void UnsetRemb() override; |
| 215 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 216 | void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override; |
| 217 | |
| 218 | size_t MaxRtpPacketSize() const override; |
| 219 | |
| 220 | void SetMaxRtpPacketSize(size_t max_packet_size) override; |
| 221 | |
| 222 | // (NACK) Negative acknowledgment part. |
| 223 | |
| 224 | // Send a Negative acknowledgment packet. |
| 225 | // TODO(philipel): Deprecate SendNACK and use SendNack instead. |
| 226 | int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override; |
| 227 | |
| 228 | void SendNack(const std::vector<uint16_t>& sequence_numbers) override; |
| 229 | |
| 230 | // Store the sent packets, needed to answer to a negative acknowledgment |
| 231 | // requests. |
| 232 | void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override; |
| 233 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 234 | void SendCombinedRtcpPacket( |
| 235 | std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override; |
| 236 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 237 | // Video part. |
| 238 | int32_t SendLossNotification(uint16_t last_decoded_seq_num, |
| 239 | uint16_t last_received_seq_num, |
| 240 | bool decodability_flag, |
| 241 | bool buffering_allowed) override; |
| 242 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 243 | RtpSendRates GetSendRates() const override; |
| 244 | |
| 245 | void OnReceivedNack( |
| 246 | const std::vector<uint16_t>& nack_sequence_numbers) override; |
| 247 | void OnReceivedRtcpReportBlocks( |
| 248 | const ReportBlockList& report_blocks) override; |
| 249 | void OnRequestSendReport() override; |
| 250 | |
| 251 | void SetVideoBitrateAllocation( |
| 252 | const VideoBitrateAllocation& bitrate) override; |
| 253 | |
| 254 | RTPSender* RtpSender() override; |
| 255 | const RTPSender* RtpSender() const override; |
| 256 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 257 | private: |
| 258 | FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt); |
| 259 | FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly); |
| 260 | |
Erik Språng | 1d50cb6 | 2020-07-02 17:41:32 +0200 | [diff] [blame] | 261 | struct RtpSenderContext : public SequenceNumberAssigner { |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 262 | explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config); |
Erik Språng | 1d50cb6 | 2020-07-02 17:41:32 +0200 | [diff] [blame] | 263 | void AssignSequenceNumber(RtpPacketToSend* packet) override; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 264 | // Storage of packets, for retransmissions and padding, if applicable. |
| 265 | RtpPacketHistory packet_history; |
| 266 | // Handles final time timestamping/stats/etc and handover to Transport. |
| 267 | RtpSenderEgress packet_sender; |
| 268 | // If no paced sender configured, this class will be used to pass packets |
| 269 | // from |packet_generator_| to |packet_sender_|. |
| 270 | RtpSenderEgress::NonPacedPacketSender non_paced_sender; |
| 271 | // Handles creation of RTP packets to be sent. |
| 272 | RTPSender packet_generator; |
| 273 | }; |
| 274 | |
| 275 | void set_rtt_ms(int64_t rtt_ms); |
| 276 | int64_t rtt_ms() const; |
| 277 | |
| 278 | bool TimeToSendFullNackList(int64_t now) const; |
| 279 | |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 280 | // Called on a timer, once a second, on the worker_queue_, to update the RTT, |
| 281 | // check if we need to send RTCP report, send TMMBR updates and fire events. |
| 282 | void PeriodicUpdate(); |
| 283 | |
Niels Möller | af6ea0c | 2020-11-20 12:21:21 +0100 | [diff] [blame] | 284 | // Returns true if the module is configured to store packets. |
| 285 | bool StorePackets() const; |
| 286 | |
Markus Handell | 885d538 | 2021-06-21 18:57:36 +0200 | [diff] [blame] | 287 | // Used from RtcpSenderMediator to maybe send rtcp. |
| 288 | void MaybeSendRtcp() RTC_RUN_ON(worker_queue_); |
| 289 | |
| 290 | // Called when |rtcp_sender_| informs of the next RTCP instant. The method may |
| 291 | // be called on various sequences, and is called under a RTCPSenderLock. |
| 292 | void ScheduleRtcpSendEvaluation(TimeDelta duration); |
| 293 | |
| 294 | // Helper method combating too early delayed calls from task queues. |
| 295 | // TODO(bugs.webrtc.org/12889): Consider removing this function when the issue |
| 296 | // is resolved. |
| 297 | void MaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time) |
| 298 | RTC_RUN_ON(worker_queue_); |
| 299 | |
| 300 | // Schedules a call to MaybeSendRtcpAtOrAfterTimestamp delayed by |duration|. |
| 301 | void ScheduleMaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time, |
| 302 | TimeDelta duration); |
| 303 | |
Tomas Gunnarsson | 473bbd8 | 2020-06-27 17:44:55 +0200 | [diff] [blame] | 304 | TaskQueueBase* const worker_queue_; |
Tommi | 08be9ba | 2021-06-15 23:01:57 +0200 | [diff] [blame] | 305 | RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 306 | |
| 307 | std::unique_ptr<RtpSenderContext> rtp_sender_; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 308 | RTCPSender rtcp_sender_; |
| 309 | RTCPReceiver rtcp_receiver_; |
| 310 | |
| 311 | Clock* const clock_; |
| 312 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 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_; |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 322 | RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 323 | |
| 324 | // The processed RTT from RtcpRttStats. |
Markus Handell | f7303e6 | 2020-07-09 01:34:42 +0200 | [diff] [blame] | 325 | mutable Mutex mutex_rtt_; |
Niels Möller | cd98213 | 2020-11-26 16:19:56 +0100 | [diff] [blame] | 326 | int64_t rtt_ms_ RTC_GUARDED_BY(mutex_rtt_); |
Markus Handell | 885d538 | 2021-06-21 18:57:36 +0200 | [diff] [blame] | 327 | |
| 328 | RTC_NO_UNIQUE_ADDRESS ScopedTaskSafety task_safety_; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | } // namespace webrtc |
| 332 | |
| 333 | #endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_ |