1 /* 2 * Copyright (c) 2019 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_SENDER_EGRESS_H_ 12 #define MODULES_RTP_RTCP_SOURCE_RTP_SENDER_EGRESS_H_ 13 14 #include <map> 15 #include <memory> 16 #include <utility> 17 #include <vector> 18 19 #include "absl/types/optional.h" 20 #include "api/call/transport.h" 21 #include "api/rtc_event_log/rtc_event_log.h" 22 #include "api/sequence_checker.h" 23 #include "api/task_queue/pending_task_safety_flag.h" 24 #include "api/task_queue/task_queue_base.h" 25 #include "api/units/data_rate.h" 26 #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h" 27 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 28 #include "modules/rtp_rtcp/source/packet_sequencer.h" 29 #include "modules/rtp_rtcp/source/rtp_packet_history.h" 30 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h" 31 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h" 32 #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h" 33 #include "rtc_base/rate_statistics.h" 34 #include "rtc_base/synchronization/mutex.h" 35 #include "rtc_base/system/no_unique_address.h" 36 #include "rtc_base/task_utils/repeating_task.h" 37 #include "rtc_base/thread_annotations.h" 38 39 namespace webrtc { 40 41 class RtpSenderEgress { 42 public: 43 // Helper class that redirects packets directly to the send part of this class 44 // without passing through an actual paced sender. 45 class NonPacedPacketSender : public RtpPacketSender { 46 public: 47 NonPacedPacketSender(RtpSenderEgress* sender, PacketSequencer* sequencer); 48 virtual ~NonPacedPacketSender(); 49 50 void EnqueuePackets( 51 std::vector<std::unique_ptr<RtpPacketToSend>> packets) override; 52 53 private: 54 void PrepareForSend(RtpPacketToSend* packet); 55 uint16_t transport_sequence_number_; 56 RtpSenderEgress* const sender_; 57 PacketSequencer* sequencer_; 58 }; 59 60 RtpSenderEgress(const RtpRtcpInterface::Configuration& config, 61 RtpPacketHistory* packet_history); 62 ~RtpSenderEgress(); 63 64 void SendPacket(RtpPacketToSend* packet, const PacedPacketInfo& pacing_info) 65 RTC_LOCKS_EXCLUDED(lock_); Ssrc()66 uint32_t Ssrc() const { return ssrc_; } RtxSsrc()67 absl::optional<uint32_t> RtxSsrc() const { return rtx_ssrc_; } FlexFecSsrc()68 absl::optional<uint32_t> FlexFecSsrc() const { return flexfec_ssrc_; } 69 70 RtpSendRates GetSendRates() const RTC_LOCKS_EXCLUDED(lock_); 71 void GetDataCounters(StreamDataCounters* rtp_stats, 72 StreamDataCounters* rtx_stats) const 73 RTC_LOCKS_EXCLUDED(lock_); 74 75 void ForceIncludeSendPacketsInAllocation(bool part_of_allocation) 76 RTC_LOCKS_EXCLUDED(lock_); 77 bool MediaHasBeenSent() const RTC_LOCKS_EXCLUDED(lock_); 78 void SetMediaHasBeenSent(bool media_sent) RTC_LOCKS_EXCLUDED(lock_); 79 void SetTimestampOffset(uint32_t timestamp) RTC_LOCKS_EXCLUDED(lock_); 80 81 // For each sequence number in `sequence_number`, recall the last RTP packet 82 // which bore it - its timestamp and whether it was the first and/or last 83 // packet in that frame. If all of the given sequence numbers could be 84 // recalled, return a vector with all of them (in corresponding order). 85 // If any could not be recalled, return an empty vector. 86 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos( 87 rtc::ArrayView<const uint16_t> sequence_numbers) const 88 RTC_LOCKS_EXCLUDED(lock_); 89 90 void SetFecProtectionParameters(const FecProtectionParams& delta_params, 91 const FecProtectionParams& key_params); 92 std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets(); 93 94 // Clears pending status for these sequence numbers in the packet history. 95 void OnAbortedRetransmissions( 96 rtc::ArrayView<const uint16_t> sequence_numbers); 97 98 private: 99 // Maps capture time in milliseconds to send-side delay in milliseconds. 100 // Send-side delay is the difference between transmission time and capture 101 // time. 102 typedef std::map<int64_t, int> SendDelayMap; 103 104 RtpSendRates GetSendRatesLocked(int64_t now_ms) const 105 RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); 106 bool HasCorrectSsrc(const RtpPacketToSend& packet) const; 107 void AddPacketToTransportFeedback(uint16_t packet_id, 108 const RtpPacketToSend& packet, 109 const PacedPacketInfo& pacing_info); 110 void UpdateDelayStatistics(int64_t capture_time_ms, 111 int64_t now_ms, 112 uint32_t ssrc); 113 void RecomputeMaxSendDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); 114 void UpdateOnSendPacket(int packet_id, 115 int64_t capture_time_ms, 116 uint32_t ssrc); 117 // Sends packet on to `transport_`, leaving the RTP module. 118 bool SendPacketToNetwork(const RtpPacketToSend& packet, 119 const PacketOptions& options, 120 const PacedPacketInfo& pacing_info); 121 122 void UpdateRtpStats(int64_t now_ms, 123 uint32_t packet_ssrc, 124 RtpPacketMediaType packet_type, 125 RtpPacketCounter counter, 126 size_t packet_size); 127 #if BWE_TEST_LOGGING_COMPILE_TIME_ENABLE 128 void BweTestLoggingPlot(int64_t now_ms, uint32_t packet_ssrc); 129 #endif 130 131 // Called on a timer, once a second, on the worker_queue_. 132 void PeriodicUpdate(); 133 134 TaskQueueBase* const worker_queue_; 135 RTC_NO_UNIQUE_ADDRESS SequenceChecker pacer_checker_; 136 const uint32_t ssrc_; 137 const absl::optional<uint32_t> rtx_ssrc_; 138 const absl::optional<uint32_t> flexfec_ssrc_; 139 const bool populate_network2_timestamp_; 140 Clock* const clock_; 141 RtpPacketHistory* const packet_history_; 142 Transport* const transport_; 143 RtcEventLog* const event_log_; 144 #if BWE_TEST_LOGGING_COMPILE_TIME_ENABLE 145 const bool is_audio_; 146 #endif 147 const bool need_rtp_packet_infos_; 148 VideoFecGenerator* const fec_generator_ RTC_GUARDED_BY(pacer_checker_); 149 absl::optional<uint16_t> last_sent_seq_ RTC_GUARDED_BY(pacer_checker_); 150 absl::optional<uint16_t> last_sent_rtx_seq_ RTC_GUARDED_BY(pacer_checker_); 151 152 TransportFeedbackObserver* const transport_feedback_observer_; 153 SendSideDelayObserver* const send_side_delay_observer_; 154 SendPacketObserver* const send_packet_observer_; 155 StreamDataCountersCallback* const rtp_stats_callback_; 156 BitrateStatisticsObserver* const bitrate_callback_; 157 158 mutable Mutex lock_; 159 bool media_has_been_sent_ RTC_GUARDED_BY(pacer_checker_); 160 bool force_part_of_allocation_ RTC_GUARDED_BY(lock_); 161 uint32_t timestamp_offset_ RTC_GUARDED_BY(worker_queue_); 162 163 SendDelayMap send_delays_ RTC_GUARDED_BY(lock_); 164 SendDelayMap::const_iterator max_delay_it_ RTC_GUARDED_BY(lock_); 165 // The sum of delays over a kSendSideDelayWindowMs sliding window. 166 int64_t sum_delays_ms_ RTC_GUARDED_BY(lock_); 167 StreamDataCounters rtp_stats_ RTC_GUARDED_BY(lock_); 168 StreamDataCounters rtx_rtp_stats_ RTC_GUARDED_BY(lock_); 169 // One element per value in RtpPacketMediaType, with index matching value. 170 std::vector<RateStatistics> send_rates_ RTC_GUARDED_BY(lock_); 171 absl::optional<std::pair<FecProtectionParams, FecProtectionParams>> 172 pending_fec_params_ RTC_GUARDED_BY(lock_); 173 174 // Maps sent packets' sequence numbers to a tuple consisting of: 175 // 1. The timestamp, without the randomizing offset mandated by the RFC. 176 // 2. Whether the packet was the first in its frame. 177 // 3. Whether the packet was the last in its frame. 178 const std::unique_ptr<RtpSequenceNumberMap> rtp_sequence_number_map_ 179 RTC_GUARDED_BY(worker_queue_); 180 RepeatingTaskHandle update_task_ RTC_GUARDED_BY(worker_queue_); 181 ScopedTaskSafety task_safety_; 182 }; 183 184 } // namespace webrtc 185 186 #endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_EGRESS_H_ 187