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