• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
26 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
27 #include "modules/rtp_rtcp/source/rtcp_packet.h"
28 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
29 #include "rtc_base/constructor_magic.h"
30 #include "rtc_base/synchronization/mutex.h"
31 #include "rtc_base/thread_annotations.h"
32 
33 namespace webrtc {
34 
35 class RtpRtcpInterface;
36 
37 // PacketRouter keeps track of rtp send modules to support the pacer.
38 // In addition, it handles feedback messages, which are sent on a send
39 // module if possible (sender report), otherwise on receive module
40 // (receiver report). For the latter case, we also keep track of the
41 // receive modules.
42 class PacketRouter : public RemoteBitrateObserver,
43                      public TransportFeedbackSenderInterface,
44                      public PacingController::PacketSender {
45  public:
46   PacketRouter();
47   explicit PacketRouter(uint16_t start_transport_seq);
48   ~PacketRouter() override;
49 
50   void AddSendRtpModule(RtpRtcpInterface* rtp_module, bool remb_candidate);
51   void RemoveSendRtpModule(RtpRtcpInterface* rtp_module);
52 
53   void AddReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender,
54                            bool remb_candidate);
55   void RemoveReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender);
56 
57   void SendPacket(std::unique_ptr<RtpPacketToSend> packet,
58                   const PacedPacketInfo& cluster_info) override;
59   std::vector<std::unique_ptr<RtpPacketToSend>> FetchFec() override;
60   std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
61       DataSize size) override;
62 
63   uint16_t CurrentTransportSequenceNumber() const;
64 
65   // Called every time there is a new bitrate estimate for a receive channel
66   // group. This call will trigger a new RTCP REMB packet if the bitrate
67   // estimate has decreased or if no RTCP REMB packet has been sent for
68   // a certain time interval.
69   // Implements RtpReceiveBitrateUpdate.
70   void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
71                                uint32_t bitrate_bps) override;
72 
73   // Ensures remote party notified of the receive bitrate limit no larger than
74   // |bitrate_bps|.
75   void SetMaxDesiredReceiveBitrate(int64_t bitrate_bps);
76 
77   // Send REMB feedback.
78   bool SendRemb(int64_t bitrate_bps, const std::vector<uint32_t>& ssrcs);
79 
80   // Sends |packets| in one or more IP packets.
81   bool SendCombinedRtcpPacket(
82       std::vector<std::unique_ptr<rtcp::RtcpPacket>> packets) override;
83 
84  private:
85   void AddRembModuleCandidate(RtcpFeedbackSenderInterface* candidate_module,
86                               bool media_sender)
87       RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
88   void MaybeRemoveRembModuleCandidate(
89       RtcpFeedbackSenderInterface* candidate_module,
90       bool media_sender) RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
91   void UnsetActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
92   void DetermineActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
93   void AddSendRtpModuleToMap(RtpRtcpInterface* rtp_module, uint32_t ssrc)
94       RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
95   void RemoveSendRtpModuleFromMap(uint32_t ssrc)
96       RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
97 
98   mutable Mutex modules_mutex_;
99   // Ssrc to RtpRtcpInterface module;
100   std::unordered_map<uint32_t, RtpRtcpInterface*> send_modules_map_
101       RTC_GUARDED_BY(modules_mutex_);
102   std::list<RtpRtcpInterface*> send_modules_list_
103       RTC_GUARDED_BY(modules_mutex_);
104   // The last module used to send media.
105   RtpRtcpInterface* last_send_module_ RTC_GUARDED_BY(modules_mutex_);
106   // Rtcp modules of the rtp receivers.
107   std::vector<RtcpFeedbackSenderInterface*> rtcp_feedback_senders_
108       RTC_GUARDED_BY(modules_mutex_);
109 
110   // TODO(eladalon): remb_mutex_ only ever held from one function, and it's not
111   // clear if that function can actually be called from more than one thread.
112   Mutex remb_mutex_;
113   // The last time a REMB was sent.
114   int64_t last_remb_time_ms_ RTC_GUARDED_BY(remb_mutex_);
115   int64_t last_send_bitrate_bps_ RTC_GUARDED_BY(remb_mutex_);
116   // The last bitrate update.
117   int64_t bitrate_bps_ RTC_GUARDED_BY(remb_mutex_);
118   int64_t max_bitrate_bps_ RTC_GUARDED_BY(remb_mutex_);
119 
120   // Candidates for the REMB module can be RTP sender/receiver modules, with
121   // the sender modules taking precedence.
122   std::vector<RtcpFeedbackSenderInterface*> sender_remb_candidates_
123       RTC_GUARDED_BY(modules_mutex_);
124   std::vector<RtcpFeedbackSenderInterface*> receiver_remb_candidates_
125       RTC_GUARDED_BY(modules_mutex_);
126   RtcpFeedbackSenderInterface* active_remb_module_
127       RTC_GUARDED_BY(modules_mutex_);
128 
129   uint64_t transport_seq_ RTC_GUARDED_BY(modules_mutex_);
130 
131   // TODO(bugs.webrtc.org/10809): Replace lock with a sequence checker once the
132   // process thread is gone.
133   std::vector<std::unique_ptr<RtpPacketToSend>> pending_fec_packets_
134       RTC_GUARDED_BY(modules_mutex_);
135 
136   RTC_DISALLOW_COPY_AND_ASSIGN(PacketRouter);
137 };
138 }  // namespace webrtc
139 #endif  // MODULES_PACING_PACKET_ROUTER_H_
140