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" |
| 24 | #include "api/video/video_bitrate_allocation.h" |
| 25 | #include "modules/include/module_fec_types.h" |
| 26 | #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
| 27 | #include "modules/rtp_rtcp/include/rtp_rtcp.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_sender.h" |
| 35 | #include "modules/rtp_rtcp/source/rtp_sender_egress.h" |
| 36 | #include "rtc_base/critical_section.h" |
| 37 | #include "rtc_base/gtest_prod_util.h" |
| 38 | #include "rtc_base/synchronization/sequence_checker.h" |
| 39 | |
| 40 | namespace webrtc { |
| 41 | |
| 42 | class Clock; |
| 43 | struct PacedPacketInfo; |
| 44 | struct RTPVideoHeader; |
| 45 | |
| 46 | class ModuleRtpRtcpImpl2 final : public RtpRtcp, |
| 47 | public RTCPReceiver::ModuleRtpRtcp { |
| 48 | public: |
| 49 | explicit ModuleRtpRtcpImpl2(const RtpRtcp::Configuration& configuration); |
| 50 | ~ModuleRtpRtcpImpl2() override; |
| 51 | |
| 52 | // Returns the number of milliseconds until the module want a worker thread to |
| 53 | // call Process. |
| 54 | int64_t TimeUntilNextProcess() override; |
| 55 | |
| 56 | // Process any pending tasks such as timeouts. |
| 57 | void Process() override; |
| 58 | |
| 59 | // Receiver part. |
| 60 | |
| 61 | // Called when we receive an RTCP packet. |
| 62 | void IncomingRtcpPacket(const uint8_t* incoming_packet, |
| 63 | size_t incoming_packet_length) override; |
| 64 | |
| 65 | void SetRemoteSSRC(uint32_t ssrc) override; |
| 66 | |
| 67 | // Sender part. |
| 68 | void RegisterSendPayloadFrequency(int payload_type, |
| 69 | int payload_frequency) override; |
| 70 | |
| 71 | int32_t DeRegisterSendPayload(int8_t payload_type) override; |
| 72 | |
| 73 | void SetExtmapAllowMixed(bool extmap_allow_mixed) override; |
| 74 | |
| 75 | // Register RTP header extension. |
| 76 | int32_t RegisterSendRtpHeaderExtension(RTPExtensionType type, |
| 77 | uint8_t id) override; |
| 78 | void RegisterRtpHeaderExtension(absl::string_view uri, int id) override; |
| 79 | int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override; |
| 80 | void DeregisterSendRtpHeaderExtension(absl::string_view uri) override; |
| 81 | |
| 82 | bool SupportsPadding() const override; |
| 83 | bool SupportsRtxPayloadPadding() const override; |
| 84 | |
| 85 | // Get start timestamp. |
| 86 | uint32_t StartTimestamp() const override; |
| 87 | |
| 88 | // Configure start timestamp, default is a random number. |
| 89 | void SetStartTimestamp(uint32_t timestamp) override; |
| 90 | |
| 91 | uint16_t SequenceNumber() const override; |
| 92 | |
| 93 | // Set SequenceNumber, default is a random number. |
| 94 | void SetSequenceNumber(uint16_t seq) override; |
| 95 | |
| 96 | void SetRtpState(const RtpState& rtp_state) override; |
| 97 | void SetRtxState(const RtpState& rtp_state) override; |
| 98 | RtpState GetRtpState() const override; |
| 99 | RtpState GetRtxState() const override; |
| 100 | |
| 101 | uint32_t SSRC() const override { return rtcp_sender_.SSRC(); } |
| 102 | |
| 103 | void SetRid(const std::string& rid) override; |
| 104 | |
| 105 | void SetMid(const std::string& mid) override; |
| 106 | |
| 107 | void SetCsrcs(const std::vector<uint32_t>& csrcs) override; |
| 108 | |
| 109 | RTCPSender::FeedbackState GetFeedbackState(); |
| 110 | |
| 111 | void SetRtxSendStatus(int mode) override; |
| 112 | int RtxSendStatus() const override; |
| 113 | absl::optional<uint32_t> RtxSsrc() const override; |
| 114 | |
| 115 | void SetRtxSendPayloadType(int payload_type, |
| 116 | int associated_payload_type) override; |
| 117 | |
| 118 | absl::optional<uint32_t> FlexfecSsrc() const override; |
| 119 | |
| 120 | // Sends kRtcpByeCode when going from true to false. |
| 121 | int32_t SetSendingStatus(bool sending) override; |
| 122 | |
| 123 | bool Sending() const override; |
| 124 | |
| 125 | // Drops or relays media packets. |
| 126 | void SetSendingMediaStatus(bool sending) override; |
| 127 | |
| 128 | bool SendingMedia() const override; |
| 129 | |
| 130 | bool IsAudioConfigured() const override; |
| 131 | |
| 132 | void SetAsPartOfAllocation(bool part_of_allocation) override; |
| 133 | |
| 134 | bool OnSendingRtpFrame(uint32_t timestamp, |
| 135 | int64_t capture_time_ms, |
| 136 | int payload_type, |
| 137 | bool force_sender_report) override; |
| 138 | |
| 139 | bool TrySendPacket(RtpPacketToSend* packet, |
| 140 | const PacedPacketInfo& pacing_info) override; |
| 141 | |
| 142 | void OnPacketsAcknowledged( |
| 143 | rtc::ArrayView<const uint16_t> sequence_numbers) override; |
| 144 | |
| 145 | std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding( |
| 146 | size_t target_size_bytes) override; |
| 147 | |
| 148 | std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos( |
| 149 | rtc::ArrayView<const uint16_t> sequence_numbers) const override; |
| 150 | |
| 151 | size_t ExpectedPerPacketOverhead() const override; |
| 152 | |
| 153 | // RTCP part. |
| 154 | |
| 155 | // Get RTCP status. |
| 156 | RtcpMode RTCP() const override; |
| 157 | |
| 158 | // Configure RTCP status i.e on/off. |
| 159 | void SetRTCPStatus(RtcpMode method) override; |
| 160 | |
| 161 | // Set RTCP CName. |
| 162 | int32_t SetCNAME(const char* c_name) override; |
| 163 | |
| 164 | // Get remote CName. |
| 165 | int32_t RemoteCNAME(uint32_t remote_ssrc, |
| 166 | char c_name[RTCP_CNAME_SIZE]) const override; |
| 167 | |
| 168 | // Get remote NTP. |
| 169 | int32_t RemoteNTP(uint32_t* received_ntp_secs, |
| 170 | uint32_t* received_ntp_frac, |
| 171 | uint32_t* rtcp_arrival_time_secs, |
| 172 | uint32_t* rtcp_arrival_time_frac, |
| 173 | uint32_t* rtcp_timestamp) const override; |
| 174 | |
| 175 | int32_t AddMixedCNAME(uint32_t ssrc, const char* c_name) override; |
| 176 | |
| 177 | int32_t RemoveMixedCNAME(uint32_t ssrc) override; |
| 178 | |
| 179 | // Get RoundTripTime. |
| 180 | int32_t RTT(uint32_t remote_ssrc, |
| 181 | int64_t* rtt, |
| 182 | int64_t* avg_rtt, |
| 183 | int64_t* min_rtt, |
| 184 | int64_t* max_rtt) const override; |
| 185 | |
| 186 | int64_t ExpectedRetransmissionTimeMs() const override; |
| 187 | |
| 188 | // Force a send of an RTCP packet. |
| 189 | // Normal SR and RR are triggered via the process function. |
| 190 | int32_t SendRTCP(RTCPPacketType rtcpPacketType) override; |
| 191 | |
| 192 | // Statistics of the amount of data sent and received. |
| 193 | int32_t DataCountersRTP(size_t* bytes_sent, |
| 194 | uint32_t* packets_sent) const override; |
| 195 | |
| 196 | void GetSendStreamDataCounters( |
| 197 | StreamDataCounters* rtp_counters, |
| 198 | StreamDataCounters* rtx_counters) const override; |
| 199 | |
| 200 | // Get received RTCP report, report block. |
| 201 | int32_t RemoteRTCPStat( |
| 202 | std::vector<RTCPReportBlock>* receive_blocks) const override; |
| 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 | |
| 209 | // (REMB) Receiver Estimated Max Bitrate. |
| 210 | void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override; |
| 211 | void UnsetRemb() override; |
| 212 | |
| 213 | // (TMMBR) Temporary Max Media Bit Rate. |
| 214 | bool TMMBR() const override; |
| 215 | |
| 216 | void SetTMMBRStatus(bool enable) override; |
| 217 | |
| 218 | void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override; |
| 219 | |
| 220 | size_t MaxRtpPacketSize() const override; |
| 221 | |
| 222 | void SetMaxRtpPacketSize(size_t max_packet_size) override; |
| 223 | |
| 224 | // (NACK) Negative acknowledgment part. |
| 225 | |
| 226 | // Send a Negative acknowledgment packet. |
| 227 | // TODO(philipel): Deprecate SendNACK and use SendNack instead. |
| 228 | int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override; |
| 229 | |
| 230 | void SendNack(const std::vector<uint16_t>& sequence_numbers) override; |
| 231 | |
| 232 | // Store the sent packets, needed to answer to a negative acknowledgment |
| 233 | // requests. |
| 234 | void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override; |
| 235 | |
| 236 | bool StorePackets() const override; |
| 237 | |
| 238 | void SendCombinedRtcpPacket( |
| 239 | std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override; |
| 240 | |
| 241 | // (APP) Application specific data. |
| 242 | int32_t SetRTCPApplicationSpecificData(uint8_t sub_type, |
| 243 | uint32_t name, |
| 244 | const uint8_t* data, |
| 245 | uint16_t length) override; |
| 246 | |
| 247 | // (XR) Receiver reference time report. |
| 248 | void SetRtcpXrRrtrStatus(bool enable) override; |
| 249 | |
| 250 | bool RtcpXrRrtrStatus() const override; |
| 251 | |
| 252 | // Video part. |
| 253 | int32_t SendLossNotification(uint16_t last_decoded_seq_num, |
| 254 | uint16_t last_received_seq_num, |
| 255 | bool decodability_flag, |
| 256 | bool buffering_allowed) override; |
| 257 | |
| 258 | bool LastReceivedNTP(uint32_t* NTPsecs, |
| 259 | uint32_t* NTPfrac, |
| 260 | uint32_t* remote_sr) const; |
| 261 | |
| 262 | void BitrateSent(uint32_t* total_rate, |
| 263 | uint32_t* video_rate, |
| 264 | uint32_t* fec_rate, |
| 265 | uint32_t* nackRate) const override; |
| 266 | |
| 267 | RtpSendRates GetSendRates() const override; |
| 268 | |
| 269 | void OnReceivedNack( |
| 270 | const std::vector<uint16_t>& nack_sequence_numbers) override; |
| 271 | void OnReceivedRtcpReportBlocks( |
| 272 | const ReportBlockList& report_blocks) override; |
| 273 | void OnRequestSendReport() override; |
| 274 | |
| 275 | void SetVideoBitrateAllocation( |
| 276 | const VideoBitrateAllocation& bitrate) override; |
| 277 | |
| 278 | RTPSender* RtpSender() override; |
| 279 | const RTPSender* RtpSender() const override; |
| 280 | |
| 281 | protected: |
| 282 | bool UpdateRTCPReceiveInformationTimers(); |
| 283 | |
| 284 | RTPSender* rtp_sender() { |
| 285 | return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr; |
| 286 | } |
| 287 | const RTPSender* rtp_sender() const { |
| 288 | return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr; |
| 289 | } |
| 290 | |
| 291 | RTCPSender* rtcp_sender() { return &rtcp_sender_; } |
| 292 | const RTCPSender* rtcp_sender() const { return &rtcp_sender_; } |
| 293 | |
| 294 | RTCPReceiver* rtcp_receiver() { return &rtcp_receiver_; } |
| 295 | const RTCPReceiver* rtcp_receiver() const { return &rtcp_receiver_; } |
| 296 | |
| 297 | Clock* clock() const { return clock_; } |
| 298 | |
| 299 | // TODO(sprang): Remove when usage is gone. |
| 300 | DataRate SendRate() const; |
| 301 | DataRate NackOverheadRate() const; |
| 302 | |
| 303 | private: |
| 304 | FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt); |
| 305 | FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly); |
| 306 | |
| 307 | struct RtpSenderContext { |
| 308 | explicit RtpSenderContext(const RtpRtcp::Configuration& config); |
| 309 | // Storage of packets, for retransmissions and padding, if applicable. |
| 310 | RtpPacketHistory packet_history; |
| 311 | // Handles final time timestamping/stats/etc and handover to Transport. |
| 312 | RtpSenderEgress packet_sender; |
| 313 | // If no paced sender configured, this class will be used to pass packets |
| 314 | // from |packet_generator_| to |packet_sender_|. |
| 315 | RtpSenderEgress::NonPacedPacketSender non_paced_sender; |
| 316 | // Handles creation of RTP packets to be sent. |
| 317 | RTPSender packet_generator; |
| 318 | }; |
| 319 | |
| 320 | void set_rtt_ms(int64_t rtt_ms); |
| 321 | int64_t rtt_ms() const; |
| 322 | |
| 323 | bool TimeToSendFullNackList(int64_t now) const; |
| 324 | |
| 325 | SequenceChecker construction_thread_checker_; |
| 326 | SequenceChecker process_thread_checker_; |
| 327 | |
| 328 | std::unique_ptr<RtpSenderContext> rtp_sender_; |
| 329 | |
| 330 | RTCPSender rtcp_sender_; |
| 331 | RTCPReceiver rtcp_receiver_; |
| 332 | |
| 333 | Clock* const clock_; |
| 334 | |
| 335 | int64_t last_bitrate_process_time_; |
| 336 | int64_t last_rtt_process_time_; |
| 337 | int64_t next_process_time_; |
| 338 | uint16_t packet_overhead_; |
| 339 | |
| 340 | // Send side |
| 341 | int64_t nack_last_time_sent_full_ms_; |
| 342 | uint16_t nack_last_seq_number_sent_; |
| 343 | |
| 344 | RemoteBitrateEstimator* const remote_bitrate_; |
| 345 | |
| 346 | RtcpRttStats* const rtt_stats_; |
| 347 | |
| 348 | // The processed RTT from RtcpRttStats. |
| 349 | rtc::CriticalSection critical_section_rtt_; |
| 350 | int64_t rtt_ms_; |
| 351 | }; |
| 352 | |
| 353 | } // namespace webrtc |
| 354 | |
| 355 | #endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_ |