• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 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 PC_COMPOSITE_RTP_TRANSPORT_H_
12 #define PC_COMPOSITE_RTP_TRANSPORT_H_
13 
14 #include <memory>
15 #include <set>
16 #include <string>
17 #include <vector>
18 
19 #include "call/rtp_demuxer.h"
20 #include "call/rtp_packet_sink_interface.h"
21 #include "pc/rtp_transport_internal.h"
22 #include "pc/session_description.h"
23 #include "rtc_base/async_packet_socket.h"
24 #include "rtc_base/copy_on_write_buffer.h"
25 
26 namespace webrtc {
27 
28 // Composite RTP transport capable of receiving from multiple sub-transports.
29 //
30 // CompositeRtpTransport is receive-only until the caller explicitly chooses
31 // which transport will be used to send and calls |SetSendTransport|.  This
32 // choice must be made as part of the SDP negotiation process, based on receipt
33 // of a provisional answer.  |CompositeRtpTransport| does not become writable or
34 // ready to send until |SetSendTransport| is called.
35 //
36 // When a full answer is received, the user should replace the composite
37 // transport with the single, chosen RTP transport, then delete the composite
38 // and all non-chosen transports.
39 class CompositeRtpTransport : public RtpTransportInternal {
40  public:
41   // Constructs a composite out of the given |transports|.  |transports| must
42   // not be empty.  All |transports| must outlive the composite.
43   explicit CompositeRtpTransport(std::vector<RtpTransportInternal*> transports);
44 
45   // Sets which transport will be used for sending packets.  Once called,
46   // |IsReadyToSend|, |IsWritable|, and the associated signals will reflect the
47   // state of |send_tranpsort|.
48   void SetSendTransport(RtpTransportInternal* send_transport);
49 
50   // Removes |transport| from the composite.  No-op if |transport| is null or
51   // not found in the composite.  Removing a transport disconnects all signals
52   // and RTP demux sinks from that transport.  The send transport may not be
53   // removed.
54   void RemoveTransport(RtpTransportInternal* transport);
55 
56   // All transports within a composite must have the same name.
57   const std::string& transport_name() const override;
58 
59   int SetRtpOption(rtc::Socket::Option opt, int value) override;
60   int SetRtcpOption(rtc::Socket::Option opt, int value) override;
61 
62   // All transports within a composite must either enable or disable RTCP mux.
63   bool rtcp_mux_enabled() const override;
64 
65   // Enables or disables RTCP mux for all component transports.
66   void SetRtcpMuxEnabled(bool enabled) override;
67 
68   // The composite is ready to send if |send_transport_| is set and ready to
69   // send.
70   bool IsReadyToSend() const override;
71 
72   // The composite is writable if |send_transport_| is set and writable.
73   bool IsWritable(bool rtcp) const override;
74 
75   // Sends an RTP packet.  May only be called after |send_transport_| is set.
76   bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
77                      const rtc::PacketOptions& options,
78                      int flags) override;
79 
80   // Sends an RTCP packet.  May only be called after |send_transport_| is set.
81   bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
82                       const rtc::PacketOptions& options,
83                       int flags) override;
84 
85   // Updates the mapping of RTP header extensions for all component transports.
86   void UpdateRtpHeaderExtensionMap(
87       const cricket::RtpHeaderExtensions& header_extensions) override;
88 
89   // SRTP is only active for a composite if it is active for all component
90   // transports.
91   bool IsSrtpActive() const override;
92 
93   // Registers an RTP demux sink with all component transports.
94   bool RegisterRtpDemuxerSink(const RtpDemuxerCriteria& criteria,
95                               RtpPacketSinkInterface* sink) override;
96   bool UnregisterRtpDemuxerSink(RtpPacketSinkInterface* sink) override;
97 
98  private:
99   // Receive-side signals.
100   void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> route);
101   void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer* packet,
102                             int64_t packet_time_us);
103 
104   // Send-side signals.
105   void OnWritableState(bool writable);
106   void OnReadyToSend(bool ready_to_send);
107   void OnSentPacket(const rtc::SentPacket& packet);
108 
109   std::vector<RtpTransportInternal*> transports_;
110   RtpTransportInternal* send_transport_ = nullptr;
111 
112   // Record of registered RTP demuxer sinks.  Used to unregister sinks when a
113   // transport is removed.
114   std::set<RtpPacketSinkInterface*> rtp_demuxer_sinks_;
115 };
116 
117 }  // namespace webrtc
118 
119 #endif  // PC_COMPOSITE_RTP_TRANSPORT_H_
120