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_DEPRECATED_DEPRECATED_RTP_SENDER_EGRESS_H_
12 #define MODULES_RTP_RTCP_SOURCE_DEPRECATED_DEPRECATED_RTP_SENDER_EGRESS_H_
13 
14 #include <map>
15 #include <memory>
16 #include <vector>
17 
18 #include "absl/types/optional.h"
19 #include "api/call/transport.h"
20 #include "api/rtc_event_log/rtc_event_log.h"
21 #include "api/units/data_rate.h"
22 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
23 #include "modules/rtp_rtcp/source/rtp_packet_history.h"
24 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
25 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
26 #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
27 #include "rtc_base/rate_statistics.h"
28 #include "rtc_base/synchronization/mutex.h"
29 #include "rtc_base/thread_annotations.h"
30 
31 namespace webrtc {
32 
33 class DEPRECATED_RtpSenderEgress {
34  public:
35   // Helper class that redirects packets directly to the send part of this class
36   // without passing through an actual paced sender.
37   class NonPacedPacketSender : public RtpPacketSender {
38    public:
39     explicit NonPacedPacketSender(DEPRECATED_RtpSenderEgress* sender);
40     virtual ~NonPacedPacketSender();
41 
42     void EnqueuePackets(
43         std::vector<std::unique_ptr<RtpPacketToSend>> packets) override;
44 
45    private:
46     uint16_t transport_sequence_number_;
47     DEPRECATED_RtpSenderEgress* const sender_;
48   };
49 
50   DEPRECATED_RtpSenderEgress(const RtpRtcpInterface::Configuration& config,
51                              RtpPacketHistory* packet_history);
52   ~DEPRECATED_RtpSenderEgress() = default;
53 
54   void SendPacket(RtpPacketToSend* packet, const PacedPacketInfo& pacing_info)
55       RTC_LOCKS_EXCLUDED(lock_);
Ssrc()56   uint32_t Ssrc() const { return ssrc_; }
RtxSsrc()57   absl::optional<uint32_t> RtxSsrc() const { return rtx_ssrc_; }
FlexFecSsrc()58   absl::optional<uint32_t> FlexFecSsrc() const { return flexfec_ssrc_; }
59 
60   void ProcessBitrateAndNotifyObservers() RTC_LOCKS_EXCLUDED(lock_);
61   RtpSendRates GetSendRates() const RTC_LOCKS_EXCLUDED(lock_);
62   void GetDataCounters(StreamDataCounters* rtp_stats,
63                        StreamDataCounters* rtx_stats) const
64       RTC_LOCKS_EXCLUDED(lock_);
65 
66   void ForceIncludeSendPacketsInAllocation(bool part_of_allocation)
67       RTC_LOCKS_EXCLUDED(lock_);
68   bool MediaHasBeenSent() const RTC_LOCKS_EXCLUDED(lock_);
69   void SetMediaHasBeenSent(bool media_sent) RTC_LOCKS_EXCLUDED(lock_);
70   void SetTimestampOffset(uint32_t timestamp) RTC_LOCKS_EXCLUDED(lock_);
71 
72   // For each sequence number in |sequence_number|, recall the last RTP packet
73   // which bore it - its timestamp and whether it was the first and/or last
74   // packet in that frame. If all of the given sequence numbers could be
75   // recalled, return a vector with all of them (in corresponding order).
76   // If any could not be recalled, return an empty vector.
77   std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
78       rtc::ArrayView<const uint16_t> sequence_numbers) const
79       RTC_LOCKS_EXCLUDED(lock_);
80 
81  private:
82   // Maps capture time in milliseconds to send-side delay in milliseconds.
83   // Send-side delay is the difference between transmission time and capture
84   // time.
85   typedef std::map<int64_t, int> SendDelayMap;
86 
87   RtpSendRates GetSendRatesLocked() const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
88   bool HasCorrectSsrc(const RtpPacketToSend& packet) const;
89   void AddPacketToTransportFeedback(uint16_t packet_id,
90                                     const RtpPacketToSend& packet,
91                                     const PacedPacketInfo& pacing_info);
92   void UpdateDelayStatistics(int64_t capture_time_ms,
93                              int64_t now_ms,
94                              uint32_t ssrc);
95   void RecomputeMaxSendDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
96   void UpdateOnSendPacket(int packet_id,
97                           int64_t capture_time_ms,
98                           uint32_t ssrc);
99   // Sends packet on to |transport_|, leaving the RTP module.
100   bool SendPacketToNetwork(const RtpPacketToSend& packet,
101                            const PacketOptions& options,
102                            const PacedPacketInfo& pacing_info);
103   void UpdateRtpStats(const RtpPacketToSend& packet)
104       RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
105 
106   const uint32_t ssrc_;
107   const absl::optional<uint32_t> rtx_ssrc_;
108   const absl::optional<uint32_t> flexfec_ssrc_;
109   const bool populate_network2_timestamp_;
110   const bool send_side_bwe_with_overhead_;
111   Clock* const clock_;
112   RtpPacketHistory* const packet_history_;
113   Transport* const transport_;
114   RtcEventLog* const event_log_;
115   const bool is_audio_;
116   const bool need_rtp_packet_infos_;
117 
118   TransportFeedbackObserver* const transport_feedback_observer_;
119   SendSideDelayObserver* const send_side_delay_observer_;
120   SendPacketObserver* const send_packet_observer_;
121   StreamDataCountersCallback* const rtp_stats_callback_;
122   BitrateStatisticsObserver* const bitrate_callback_;
123 
124   mutable Mutex lock_;
125   bool media_has_been_sent_ RTC_GUARDED_BY(lock_);
126   bool force_part_of_allocation_ RTC_GUARDED_BY(lock_);
127   uint32_t timestamp_offset_ RTC_GUARDED_BY(lock_);
128 
129   SendDelayMap send_delays_ RTC_GUARDED_BY(lock_);
130   SendDelayMap::const_iterator max_delay_it_ RTC_GUARDED_BY(lock_);
131   // The sum of delays over a kSendSideDelayWindowMs sliding window.
132   int64_t sum_delays_ms_ RTC_GUARDED_BY(lock_);
133   uint64_t total_packet_send_delay_ms_ RTC_GUARDED_BY(lock_);
134   StreamDataCounters rtp_stats_ RTC_GUARDED_BY(lock_);
135   StreamDataCounters rtx_rtp_stats_ RTC_GUARDED_BY(lock_);
136   // One element per value in RtpPacketMediaType, with index matching value.
137   std::vector<RateStatistics> send_rates_ RTC_GUARDED_BY(lock_);
138 
139   // Maps sent packets' sequence numbers to a tuple consisting of:
140   // 1. The timestamp, without the randomizing offset mandated by the RFC.
141   // 2. Whether the packet was the first in its frame.
142   // 3. Whether the packet was the last in its frame.
143   const std::unique_ptr<RtpSequenceNumberMap> rtp_sequence_number_map_
144       RTC_GUARDED_BY(lock_);
145 };
146 
147 }  // namespace webrtc
148 
149 #endif  // MODULES_RTP_RTCP_SOURCE_DEPRECATED_DEPRECATED_RTP_SENDER_EGRESS_H_
150