xref: /aosp_15_r20/external/webrtc/modules/pacing/packet_router.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2015 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_PACING_PACKET_ROUTER_H_
12 #define MODULES_PACING_PACKET_ROUTER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <list>
18 #include <memory>
19 #include <unordered_map>
20 #include <utility>
21 #include <vector>
22 
23 #include "api/transport/network_types.h"
24 #include "modules/pacing/pacing_controller.h"
25 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
26 #include "modules/rtp_rtcp/source/rtcp_packet.h"
27 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
28 #include "rtc_base/synchronization/mutex.h"
29 #include "rtc_base/thread_annotations.h"
30 
31 namespace webrtc {
32 
33 class RtpRtcpInterface;
34 
35 // PacketRouter keeps track of rtp send modules to support the pacer.
36 // In addition, it handles feedback messages, which are sent on a send
37 // module if possible (sender report), otherwise on receive module
38 // (receiver report). For the latter case, we also keep track of the
39 // receive modules.
40 class PacketRouter : public PacingController::PacketSender {
41  public:
42   PacketRouter();
43   explicit PacketRouter(uint16_t start_transport_seq);
44   ~PacketRouter() override;
45 
46   PacketRouter(const PacketRouter&) = delete;
47   PacketRouter& operator=(const PacketRouter&) = delete;
48 
49   void AddSendRtpModule(RtpRtcpInterface* rtp_module, bool remb_candidate);
50   void RemoveSendRtpModule(RtpRtcpInterface* rtp_module);
51 
52   void AddReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender,
53                            bool remb_candidate);
54   void RemoveReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender);
55 
56   void SendPacket(std::unique_ptr<RtpPacketToSend> packet,
57                   const PacedPacketInfo& cluster_info) override;
58   std::vector<std::unique_ptr<RtpPacketToSend>> FetchFec() override;
59   std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
60       DataSize size) override;
61   void OnAbortedRetransmissions(
62       uint32_t ssrc,
63       rtc::ArrayView<const uint16_t> sequence_numbers) override;
64   absl::optional<uint32_t> GetRtxSsrcForMedia(uint32_t ssrc) const override;
65 
66   uint16_t CurrentTransportSequenceNumber() const;
67 
68   // Send REMB feedback.
69   void SendRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs);
70 
71   // Sends `packets` in one or more IP packets.
72   void SendCombinedRtcpPacket(
73       std::vector<std::unique_ptr<rtcp::RtcpPacket>> packets);
74 
75  private:
76   void AddRembModuleCandidate(RtcpFeedbackSenderInterface* candidate_module,
77                               bool media_sender)
78       RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
79   void MaybeRemoveRembModuleCandidate(
80       RtcpFeedbackSenderInterface* candidate_module,
81       bool media_sender) RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
82   void UnsetActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
83   void DetermineActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
84   void AddSendRtpModuleToMap(RtpRtcpInterface* rtp_module, uint32_t ssrc)
85       RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
86   void RemoveSendRtpModuleFromMap(uint32_t ssrc)
87       RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
88 
89   mutable Mutex modules_mutex_;
90   // Ssrc to RtpRtcpInterface module;
91   std::unordered_map<uint32_t, RtpRtcpInterface*> send_modules_map_
92       RTC_GUARDED_BY(modules_mutex_);
93   std::list<RtpRtcpInterface*> send_modules_list_
94       RTC_GUARDED_BY(modules_mutex_);
95   // The last module used to send media.
96   RtpRtcpInterface* last_send_module_ RTC_GUARDED_BY(modules_mutex_);
97   // Rtcp modules of the rtp receivers.
98   std::vector<RtcpFeedbackSenderInterface*> rtcp_feedback_senders_
99       RTC_GUARDED_BY(modules_mutex_);
100 
101   // Candidates for the REMB module can be RTP sender/receiver modules, with
102   // the sender modules taking precedence.
103   std::vector<RtcpFeedbackSenderInterface*> sender_remb_candidates_
104       RTC_GUARDED_BY(modules_mutex_);
105   std::vector<RtcpFeedbackSenderInterface*> receiver_remb_candidates_
106       RTC_GUARDED_BY(modules_mutex_);
107   RtcpFeedbackSenderInterface* active_remb_module_
108       RTC_GUARDED_BY(modules_mutex_);
109 
110   uint64_t transport_seq_ RTC_GUARDED_BY(modules_mutex_);
111 
112   // TODO(bugs.webrtc.org/10809): Replace lock with a sequence checker once the
113   // process thread is gone.
114   std::vector<std::unique_ptr<RtpPacketToSend>> pending_fec_packets_
115       RTC_GUARDED_BY(modules_mutex_);
116 };
117 }  // namespace webrtc
118 #endif  // MODULES_PACING_PACKET_ROUTER_H_
119