rtp_sender_egress.h (7122B)
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 <cstddef> 15 #include <cstdint> 16 #include <memory> 17 #include <optional> 18 #include <utility> 19 #include <vector> 20 21 #include "api/array_view.h" 22 #include "api/call/transport.h" 23 #include "api/environment/environment.h" 24 #include "api/rtp_packet_sender.h" 25 #include "api/task_queue/pending_task_safety_flag.h" 26 #include "api/task_queue/task_queue_base.h" 27 #include "api/transport/network_types.h" 28 #include "api/units/timestamp.h" 29 #include "modules/include/module_fec_types.h" 30 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 31 #include "modules/rtp_rtcp/source/packet_sequencer.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_rtcp_interface.h" 35 #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h" 36 #include "modules/rtp_rtcp/source/video_fec_generator.h" 37 #include "rtc_base/bitrate_tracker.h" 38 #include "rtc_base/task_utils/repeating_task.h" 39 #include "rtc_base/thread_annotations.h" 40 41 namespace webrtc { 42 43 class RtpSenderEgress { 44 public: 45 // Helper class that redirects packets directly to the send part of this class 46 // without passing through an actual paced sender. 47 class NonPacedPacketSender : public RtpPacketSender { 48 public: 49 NonPacedPacketSender(TaskQueueBase& worker_queue, 50 RtpSenderEgress* sender, 51 PacketSequencer* sequencer); 52 virtual ~NonPacedPacketSender(); 53 54 void EnqueuePackets( 55 std::vector<std::unique_ptr<RtpPacketToSend>> packets) override; 56 // Since we don't pace packets, there's no pending packets to remove. 57 void RemovePacketsForSsrc(uint32_t /* ssrc */) override {} 58 59 private: 60 void PrepareForSend(RtpPacketToSend* packet); 61 TaskQueueBase& worker_queue_; 62 uint16_t transport_sequence_number_; 63 RtpSenderEgress* const sender_; 64 PacketSequencer* sequencer_; 65 ScopedTaskSafety task_safety_; 66 }; 67 68 RtpSenderEgress(const Environment& env, 69 const RtpRtcpInterface::Configuration& config, 70 RtpPacketHistory* packet_history); 71 ~RtpSenderEgress(); 72 73 void SendPacket(std::unique_ptr<RtpPacketToSend> packet, 74 const PacedPacketInfo& pacing_info); 75 void OnBatchComplete(); 76 uint32_t Ssrc() const { return ssrc_; } 77 std::optional<uint32_t> RtxSsrc() const { return rtx_ssrc_; } 78 std::optional<uint32_t> FlexFecSsrc() const { return flexfec_ssrc_; } 79 80 RtpSendRates GetSendRates(Timestamp now) const; 81 void GetDataCounters(StreamDataCounters* rtp_stats, 82 StreamDataCounters* rtx_stats) const; 83 84 void ForceIncludeSendPacketsInAllocation(bool part_of_allocation); 85 86 bool MediaHasBeenSent() const; 87 void SetMediaHasBeenSent(bool media_sent); 88 void SetTimestampOffset(uint32_t timestamp); 89 90 // For each sequence number in `sequence_number`, recall the last RTP packet 91 // which bore it - its timestamp and whether it was the first and/or last 92 // packet in that frame. If all of the given sequence numbers could be 93 // recalled, return a vector with all of them (in corresponding order). 94 // If any could not be recalled, return an empty vector. 95 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos( 96 ArrayView<const uint16_t> sequence_numbers) const; 97 98 void SetFecProtectionParameters(const FecProtectionParams& delta_params, 99 const FecProtectionParams& key_params); 100 std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets(); 101 102 // Clears pending status for these sequence numbers in the packet history. 103 void OnAbortedRetransmissions(ArrayView<const uint16_t> sequence_numbers); 104 105 private: 106 struct Packet { 107 std::unique_ptr<RtpPacketToSend> rtp_packet; 108 PacedPacketInfo info; 109 Timestamp now; 110 }; 111 void CompleteSendPacket(const Packet& compound_packet, bool last_in_batch); 112 bool HasCorrectSsrc(const RtpPacketToSend& packet) const; 113 114 // Sends packet on to `transport_`, leaving the RTP module. 115 bool SendPacketToNetwork(const RtpPacketToSend& packet, 116 const PacketOptions& options, 117 const PacedPacketInfo& pacing_info); 118 void UpdateRtpStats(Timestamp now, 119 uint32_t packet_ssrc, 120 RtpPacketMediaType packet_type, 121 RtpPacketCounter counter, 122 size_t packet_size); 123 124 // Called on a timer, once a second, on the worker_queue_. 125 void PeriodicUpdate(); 126 127 const Environment env_; 128 const bool enable_send_packet_batching_; 129 TaskQueueBase* const worker_queue_; 130 const uint32_t ssrc_; 131 const std::optional<uint32_t> rtx_ssrc_; 132 const std::optional<uint32_t> flexfec_ssrc_; 133 const bool populate_network2_timestamp_; 134 RtpPacketHistory* const packet_history_ RTC_GUARDED_BY(worker_queue_); 135 Transport* const transport_; 136 const bool is_audio_; 137 const bool need_rtp_packet_infos_; 138 VideoFecGenerator* const fec_generator_ RTC_GUARDED_BY(worker_queue_); 139 std::optional<uint16_t> last_sent_seq_ RTC_GUARDED_BY(worker_queue_); 140 std::optional<uint16_t> last_sent_rtx_seq_ RTC_GUARDED_BY(worker_queue_); 141 142 SendPacketObserver* const send_packet_observer_; 143 StreamDataCountersCallback* const rtp_stats_callback_; 144 BitrateStatisticsObserver* const bitrate_callback_; 145 146 bool media_has_been_sent_ RTC_GUARDED_BY(worker_queue_); 147 bool force_part_of_allocation_ RTC_GUARDED_BY(worker_queue_); 148 uint32_t timestamp_offset_ RTC_GUARDED_BY(worker_queue_); 149 150 // These counters are only used if `rtp_stats_callback_` is null. 151 StreamDataCounters rtp_stats_ RTC_GUARDED_BY(worker_queue_); 152 StreamDataCounters rtx_rtp_stats_ RTC_GUARDED_BY(worker_queue_); 153 154 // One element per value in RtpPacketMediaType, with index matching value. 155 std::vector<BitrateTracker> send_rates_ RTC_GUARDED_BY(worker_queue_); 156 std::optional<std::pair<FecProtectionParams, FecProtectionParams>> 157 pending_fec_params_ RTC_GUARDED_BY(worker_queue_); 158 159 // Maps sent packets' sequence numbers to a tuple consisting of: 160 // 1. The timestamp, without the randomizing offset mandated by the RFC. 161 // 2. Whether the packet was the first in its frame. 162 // 3. Whether the packet was the last in its frame. 163 const std::unique_ptr<RtpSequenceNumberMap> rtp_sequence_number_map_ 164 RTC_GUARDED_BY(worker_queue_); 165 RepeatingTaskHandle update_task_ RTC_GUARDED_BY(worker_queue_); 166 std::vector<Packet> packets_to_send_ RTC_GUARDED_BY(worker_queue_); 167 ScopedTaskSafety task_safety_; 168 const bool use_ntp_time_for_absolute_send_time_; 169 }; 170 171 } // namespace webrtc 172 173 #endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_EGRESS_H_