xref: /aosp_15_r20/external/webrtc/pc/jsep_transport_controller.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2017 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef PC_JSEP_TRANSPORT_CONTROLLER_H_
12*d9f75844SAndroid Build Coastguard Worker #define PC_JSEP_TRANSPORT_CONTROLLER_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #include <functional>
17*d9f75844SAndroid Build Coastguard Worker #include <map>
18*d9f75844SAndroid Build Coastguard Worker #include <memory>
19*d9f75844SAndroid Build Coastguard Worker #include <string>
20*d9f75844SAndroid Build Coastguard Worker #include <type_traits>
21*d9f75844SAndroid Build Coastguard Worker #include <utility>
22*d9f75844SAndroid Build Coastguard Worker #include <vector>
23*d9f75844SAndroid Build Coastguard Worker 
24*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
25*d9f75844SAndroid Build Coastguard Worker #include "api/async_dns_resolver.h"
26*d9f75844SAndroid Build Coastguard Worker #include "api/candidate.h"
27*d9f75844SAndroid Build Coastguard Worker #include "api/crypto/crypto_options.h"
28*d9f75844SAndroid Build Coastguard Worker #include "api/ice_transport_factory.h"
29*d9f75844SAndroid Build Coastguard Worker #include "api/ice_transport_interface.h"
30*d9f75844SAndroid Build Coastguard Worker #include "api/jsep.h"
31*d9f75844SAndroid Build Coastguard Worker #include "api/peer_connection_interface.h"
32*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_error.h"
33*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_event_log/rtc_event_log.h"
34*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
35*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
36*d9f75844SAndroid Build Coastguard Worker #include "api/transport/data_channel_transport_interface.h"
37*d9f75844SAndroid Build Coastguard Worker #include "api/transport/sctp_transport_factory_interface.h"
38*d9f75844SAndroid Build Coastguard Worker #include "media/sctp/sctp_transport_internal.h"
39*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/dtls_transport.h"
40*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/dtls_transport_factory.h"
41*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/dtls_transport_internal.h"
42*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/ice_transport_internal.h"
43*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/p2p_transport_channel.h"
44*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/packet_transport_internal.h"
45*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/port.h"
46*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/port_allocator.h"
47*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/transport_description.h"
48*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/transport_info.h"
49*d9f75844SAndroid Build Coastguard Worker #include "pc/dtls_srtp_transport.h"
50*d9f75844SAndroid Build Coastguard Worker #include "pc/dtls_transport.h"
51*d9f75844SAndroid Build Coastguard Worker #include "pc/jsep_transport.h"
52*d9f75844SAndroid Build Coastguard Worker #include "pc/jsep_transport_collection.h"
53*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_transport.h"
54*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_transport_internal.h"
55*d9f75844SAndroid Build Coastguard Worker #include "pc/sctp_transport.h"
56*d9f75844SAndroid Build Coastguard Worker #include "pc/session_description.h"
57*d9f75844SAndroid Build Coastguard Worker #include "pc/srtp_transport.h"
58*d9f75844SAndroid Build Coastguard Worker #include "pc/transport_stats.h"
59*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/callback_list.h"
60*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
61*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/copy_on_write_buffer.h"
62*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/helpers.h"
63*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/rtc_certificate.h"
64*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ssl_certificate.h"
65*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ssl_stream_adapter.h"
66*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/third_party/sigslot/sigslot.h"
67*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread.h"
68*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread_annotations.h"
69*d9f75844SAndroid Build Coastguard Worker 
70*d9f75844SAndroid Build Coastguard Worker namespace rtc {
71*d9f75844SAndroid Build Coastguard Worker class Thread;
72*d9f75844SAndroid Build Coastguard Worker class PacketTransportInternal;
73*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
76*d9f75844SAndroid Build Coastguard Worker 
77*d9f75844SAndroid Build Coastguard Worker class JsepTransportController : public sigslot::has_slots<> {
78*d9f75844SAndroid Build Coastguard Worker  public:
79*d9f75844SAndroid Build Coastguard Worker   // Used when the RtpTransport/DtlsTransport of the m= section is changed
80*d9f75844SAndroid Build Coastguard Worker   // because the section is rejected or BUNDLE is enabled.
81*d9f75844SAndroid Build Coastguard Worker   class Observer {
82*d9f75844SAndroid Build Coastguard Worker    public:
~Observer()83*d9f75844SAndroid Build Coastguard Worker     virtual ~Observer() {}
84*d9f75844SAndroid Build Coastguard Worker 
85*d9f75844SAndroid Build Coastguard Worker     // Returns true if media associated with `mid` was successfully set up to be
86*d9f75844SAndroid Build Coastguard Worker     // demultiplexed on `rtp_transport`. Could return false if two bundled m=
87*d9f75844SAndroid Build Coastguard Worker     // sections use the same SSRC, for example.
88*d9f75844SAndroid Build Coastguard Worker     //
89*d9f75844SAndroid Build Coastguard Worker     // If a data channel transport must be negotiated, `data_channel_transport`
90*d9f75844SAndroid Build Coastguard Worker     // and `negotiation_state` indicate negotiation status.  If
91*d9f75844SAndroid Build Coastguard Worker     // `data_channel_transport` is null, the data channel transport should not
92*d9f75844SAndroid Build Coastguard Worker     // be used.  Otherwise, the value is a pointer to the transport to be used
93*d9f75844SAndroid Build Coastguard Worker     // for data channels on `mid`, if any.
94*d9f75844SAndroid Build Coastguard Worker     //
95*d9f75844SAndroid Build Coastguard Worker     // The observer should not send data on `data_channel_transport` until
96*d9f75844SAndroid Build Coastguard Worker     // `negotiation_state` is provisional or final.  It should not delete
97*d9f75844SAndroid Build Coastguard Worker     // `data_channel_transport` or any fallback transport until
98*d9f75844SAndroid Build Coastguard Worker     // `negotiation_state` is final.
99*d9f75844SAndroid Build Coastguard Worker     virtual bool OnTransportChanged(
100*d9f75844SAndroid Build Coastguard Worker         const std::string& mid,
101*d9f75844SAndroid Build Coastguard Worker         RtpTransportInternal* rtp_transport,
102*d9f75844SAndroid Build Coastguard Worker         rtc::scoped_refptr<DtlsTransport> dtls_transport,
103*d9f75844SAndroid Build Coastguard Worker         DataChannelTransportInterface* data_channel_transport) = 0;
104*d9f75844SAndroid Build Coastguard Worker   };
105*d9f75844SAndroid Build Coastguard Worker 
106*d9f75844SAndroid Build Coastguard Worker   struct Config {
107*d9f75844SAndroid Build Coastguard Worker     // If `redetermine_role_on_ice_restart` is true, ICE role is redetermined
108*d9f75844SAndroid Build Coastguard Worker     // upon setting a local transport description that indicates an ICE
109*d9f75844SAndroid Build Coastguard Worker     // restart.
110*d9f75844SAndroid Build Coastguard Worker     bool redetermine_role_on_ice_restart = true;
111*d9f75844SAndroid Build Coastguard Worker     rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
112*d9f75844SAndroid Build Coastguard Worker     // `crypto_options` is used to determine if created DTLS transports
113*d9f75844SAndroid Build Coastguard Worker     // negotiate GCM crypto suites or not.
114*d9f75844SAndroid Build Coastguard Worker     webrtc::CryptoOptions crypto_options;
115*d9f75844SAndroid Build Coastguard Worker     PeerConnectionInterface::BundlePolicy bundle_policy =
116*d9f75844SAndroid Build Coastguard Worker         PeerConnectionInterface::kBundlePolicyBalanced;
117*d9f75844SAndroid Build Coastguard Worker     PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
118*d9f75844SAndroid Build Coastguard Worker         PeerConnectionInterface::kRtcpMuxPolicyRequire;
119*d9f75844SAndroid Build Coastguard Worker     bool disable_encryption = false;
120*d9f75844SAndroid Build Coastguard Worker     bool enable_external_auth = false;
121*d9f75844SAndroid Build Coastguard Worker     // Used to inject the ICE/DTLS transports created externally.
122*d9f75844SAndroid Build Coastguard Worker     webrtc::IceTransportFactory* ice_transport_factory = nullptr;
123*d9f75844SAndroid Build Coastguard Worker     cricket::DtlsTransportFactory* dtls_transport_factory = nullptr;
124*d9f75844SAndroid Build Coastguard Worker     Observer* transport_observer = nullptr;
125*d9f75844SAndroid Build Coastguard Worker     // Must be provided and valid for the lifetime of the
126*d9f75844SAndroid Build Coastguard Worker     // JsepTransportController instance.
127*d9f75844SAndroid Build Coastguard Worker     std::function<void(const rtc::CopyOnWriteBuffer& packet,
128*d9f75844SAndroid Build Coastguard Worker                        int64_t packet_time_us)>
129*d9f75844SAndroid Build Coastguard Worker         rtcp_handler;
130*d9f75844SAndroid Build Coastguard Worker     // Initial value for whether DtlsTransport reset causes a reset
131*d9f75844SAndroid Build Coastguard Worker     // of SRTP parameters.
132*d9f75844SAndroid Build Coastguard Worker     bool active_reset_srtp_params = false;
133*d9f75844SAndroid Build Coastguard Worker     RtcEventLog* event_log = nullptr;
134*d9f75844SAndroid Build Coastguard Worker 
135*d9f75844SAndroid Build Coastguard Worker     // Factory for SCTP transports.
136*d9f75844SAndroid Build Coastguard Worker     SctpTransportFactoryInterface* sctp_factory = nullptr;
137*d9f75844SAndroid Build Coastguard Worker     std::function<void(rtc::SSLHandshakeError)> on_dtls_handshake_error_;
138*d9f75844SAndroid Build Coastguard Worker 
139*d9f75844SAndroid Build Coastguard Worker     // Field trials.
140*d9f75844SAndroid Build Coastguard Worker     const webrtc::FieldTrialsView* field_trials;
141*d9f75844SAndroid Build Coastguard Worker   };
142*d9f75844SAndroid Build Coastguard Worker 
143*d9f75844SAndroid Build Coastguard Worker   // The ICE related events are fired on the `network_thread`.
144*d9f75844SAndroid Build Coastguard Worker   // All the transport related methods are called on the `network_thread`
145*d9f75844SAndroid Build Coastguard Worker   // and destruction of the JsepTransportController must occur on the
146*d9f75844SAndroid Build Coastguard Worker   // `network_thread`.
147*d9f75844SAndroid Build Coastguard Worker   JsepTransportController(
148*d9f75844SAndroid Build Coastguard Worker       rtc::Thread* network_thread,
149*d9f75844SAndroid Build Coastguard Worker       cricket::PortAllocator* port_allocator,
150*d9f75844SAndroid Build Coastguard Worker       AsyncDnsResolverFactoryInterface* async_dns_resolver_factory,
151*d9f75844SAndroid Build Coastguard Worker       Config config);
152*d9f75844SAndroid Build Coastguard Worker   virtual ~JsepTransportController();
153*d9f75844SAndroid Build Coastguard Worker 
154*d9f75844SAndroid Build Coastguard Worker   JsepTransportController(const JsepTransportController&) = delete;
155*d9f75844SAndroid Build Coastguard Worker   JsepTransportController& operator=(const JsepTransportController&) = delete;
156*d9f75844SAndroid Build Coastguard Worker 
157*d9f75844SAndroid Build Coastguard Worker   // The main method to be called; applies a description at the transport
158*d9f75844SAndroid Build Coastguard Worker   // level, creating/destroying transport objects as needed and updating their
159*d9f75844SAndroid Build Coastguard Worker   // properties. This includes RTP, DTLS, and ICE (but not SCTP). At least not
160*d9f75844SAndroid Build Coastguard Worker   // yet? May make sense to in the future.
161*d9f75844SAndroid Build Coastguard Worker   RTCError SetLocalDescription(SdpType type,
162*d9f75844SAndroid Build Coastguard Worker                                const cricket::SessionDescription* description);
163*d9f75844SAndroid Build Coastguard Worker 
164*d9f75844SAndroid Build Coastguard Worker   RTCError SetRemoteDescription(SdpType type,
165*d9f75844SAndroid Build Coastguard Worker                                 const cricket::SessionDescription* description);
166*d9f75844SAndroid Build Coastguard Worker 
167*d9f75844SAndroid Build Coastguard Worker   // Get transports to be used for the provided `mid`. If bundling is enabled,
168*d9f75844SAndroid Build Coastguard Worker   // calling GetRtpTransport for multiple MIDs may yield the same object.
169*d9f75844SAndroid Build Coastguard Worker   RtpTransportInternal* GetRtpTransport(absl::string_view mid) const;
170*d9f75844SAndroid Build Coastguard Worker   cricket::DtlsTransportInternal* GetDtlsTransport(const std::string& mid);
171*d9f75844SAndroid Build Coastguard Worker   const cricket::DtlsTransportInternal* GetRtcpDtlsTransport(
172*d9f75844SAndroid Build Coastguard Worker       const std::string& mid) const;
173*d9f75844SAndroid Build Coastguard Worker   // Gets the externally sharable version of the DtlsTransport.
174*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<webrtc::DtlsTransport> LookupDtlsTransportByMid(
175*d9f75844SAndroid Build Coastguard Worker       const std::string& mid);
176*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<SctpTransport> GetSctpTransport(
177*d9f75844SAndroid Build Coastguard Worker       const std::string& mid) const;
178*d9f75844SAndroid Build Coastguard Worker 
179*d9f75844SAndroid Build Coastguard Worker   DataChannelTransportInterface* GetDataChannelTransport(
180*d9f75844SAndroid Build Coastguard Worker       const std::string& mid) const;
181*d9f75844SAndroid Build Coastguard Worker 
182*d9f75844SAndroid Build Coastguard Worker   /*********************
183*d9f75844SAndroid Build Coastguard Worker    * ICE-related methods
184*d9f75844SAndroid Build Coastguard Worker    ********************/
185*d9f75844SAndroid Build Coastguard Worker   // This method is public to allow PeerConnection to update it from
186*d9f75844SAndroid Build Coastguard Worker   // SetConfiguration.
187*d9f75844SAndroid Build Coastguard Worker   void SetIceConfig(const cricket::IceConfig& config);
188*d9f75844SAndroid Build Coastguard Worker   // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
189*d9f75844SAndroid Build Coastguard Worker   // set, offers should generate new ufrags/passwords until an ICE restart
190*d9f75844SAndroid Build Coastguard Worker   // occurs.
191*d9f75844SAndroid Build Coastguard Worker   void SetNeedsIceRestartFlag();
192*d9f75844SAndroid Build Coastguard Worker   // Returns true if the ICE restart flag above was set, and no ICE restart has
193*d9f75844SAndroid Build Coastguard Worker   // occurred yet for this transport (by applying a local description with
194*d9f75844SAndroid Build Coastguard Worker   // changed ufrag/password). If the transport has been deleted as a result of
195*d9f75844SAndroid Build Coastguard Worker   // bundling, returns false.
196*d9f75844SAndroid Build Coastguard Worker   bool NeedsIceRestart(const std::string& mid) const;
197*d9f75844SAndroid Build Coastguard Worker   // Start gathering candidates for any new transports, or transports doing an
198*d9f75844SAndroid Build Coastguard Worker   // ICE restart.
199*d9f75844SAndroid Build Coastguard Worker   void MaybeStartGathering();
200*d9f75844SAndroid Build Coastguard Worker   RTCError AddRemoteCandidates(
201*d9f75844SAndroid Build Coastguard Worker       const std::string& mid,
202*d9f75844SAndroid Build Coastguard Worker       const std::vector<cricket::Candidate>& candidates);
203*d9f75844SAndroid Build Coastguard Worker   RTCError RemoveRemoteCandidates(
204*d9f75844SAndroid Build Coastguard Worker       const std::vector<cricket::Candidate>& candidates);
205*d9f75844SAndroid Build Coastguard Worker 
206*d9f75844SAndroid Build Coastguard Worker   /**********************
207*d9f75844SAndroid Build Coastguard Worker    * DTLS-related methods
208*d9f75844SAndroid Build Coastguard Worker    *********************/
209*d9f75844SAndroid Build Coastguard Worker   // Specifies the identity to use in this session.
210*d9f75844SAndroid Build Coastguard Worker   // Can only be called once.
211*d9f75844SAndroid Build Coastguard Worker   bool SetLocalCertificate(
212*d9f75844SAndroid Build Coastguard Worker       const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
213*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
214*d9f75844SAndroid Build Coastguard Worker       const std::string& mid) const;
215*d9f75844SAndroid Build Coastguard Worker   // Caller owns returned certificate chain. This method mainly exists for
216*d9f75844SAndroid Build Coastguard Worker   // stats reporting.
217*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
218*d9f75844SAndroid Build Coastguard Worker       const std::string& mid) const;
219*d9f75844SAndroid Build Coastguard Worker   // Get negotiated role, if one has been negotiated.
220*d9f75844SAndroid Build Coastguard Worker   absl::optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
221*d9f75844SAndroid Build Coastguard Worker 
222*d9f75844SAndroid Build Coastguard Worker   // TODO(deadbeef): GetStats isn't const because all the way down to
223*d9f75844SAndroid Build Coastguard Worker   // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
224*d9f75844SAndroid Build Coastguard Worker   // const. Fix this.
225*d9f75844SAndroid Build Coastguard Worker   bool GetStats(const std::string& mid, cricket::TransportStats* stats);
226*d9f75844SAndroid Build Coastguard Worker 
initial_offerer()227*d9f75844SAndroid Build Coastguard Worker   bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; }
228*d9f75844SAndroid Build Coastguard Worker 
229*d9f75844SAndroid Build Coastguard Worker   void SetActiveResetSrtpParams(bool active_reset_srtp_params);
230*d9f75844SAndroid Build Coastguard Worker 
231*d9f75844SAndroid Build Coastguard Worker   RTCError RollbackTransports();
232*d9f75844SAndroid Build Coastguard Worker 
233*d9f75844SAndroid Build Coastguard Worker   // F: void(const std::string&, const std::vector<cricket::Candidate>&)
234*d9f75844SAndroid Build Coastguard Worker   template <typename F>
SubscribeIceCandidateGathered(F && callback)235*d9f75844SAndroid Build Coastguard Worker   void SubscribeIceCandidateGathered(F&& callback) {
236*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(network_thread_);
237*d9f75844SAndroid Build Coastguard Worker     signal_ice_candidates_gathered_.AddReceiver(std::forward<F>(callback));
238*d9f75844SAndroid Build Coastguard Worker   }
239*d9f75844SAndroid Build Coastguard Worker 
240*d9f75844SAndroid Build Coastguard Worker   // F: void(cricket::IceConnectionState)
241*d9f75844SAndroid Build Coastguard Worker   template <typename F>
SubscribeIceConnectionState(F && callback)242*d9f75844SAndroid Build Coastguard Worker   void SubscribeIceConnectionState(F&& callback) {
243*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(network_thread_);
244*d9f75844SAndroid Build Coastguard Worker     signal_ice_connection_state_.AddReceiver(std::forward<F>(callback));
245*d9f75844SAndroid Build Coastguard Worker   }
246*d9f75844SAndroid Build Coastguard Worker 
247*d9f75844SAndroid Build Coastguard Worker   // F: void(PeerConnectionInterface::PeerConnectionState)
248*d9f75844SAndroid Build Coastguard Worker   template <typename F>
SubscribeConnectionState(F && callback)249*d9f75844SAndroid Build Coastguard Worker   void SubscribeConnectionState(F&& callback) {
250*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(network_thread_);
251*d9f75844SAndroid Build Coastguard Worker     signal_connection_state_.AddReceiver(std::forward<F>(callback));
252*d9f75844SAndroid Build Coastguard Worker   }
253*d9f75844SAndroid Build Coastguard Worker 
254*d9f75844SAndroid Build Coastguard Worker   // F: void(PeerConnectionInterface::IceConnectionState)
255*d9f75844SAndroid Build Coastguard Worker   template <typename F>
SubscribeStandardizedIceConnectionState(F && callback)256*d9f75844SAndroid Build Coastguard Worker   void SubscribeStandardizedIceConnectionState(F&& callback) {
257*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(network_thread_);
258*d9f75844SAndroid Build Coastguard Worker     signal_standardized_ice_connection_state_.AddReceiver(
259*d9f75844SAndroid Build Coastguard Worker         std::forward<F>(callback));
260*d9f75844SAndroid Build Coastguard Worker   }
261*d9f75844SAndroid Build Coastguard Worker 
262*d9f75844SAndroid Build Coastguard Worker   // F: void(cricket::IceGatheringState)
263*d9f75844SAndroid Build Coastguard Worker   template <typename F>
SubscribeIceGatheringState(F && callback)264*d9f75844SAndroid Build Coastguard Worker   void SubscribeIceGatheringState(F&& callback) {
265*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(network_thread_);
266*d9f75844SAndroid Build Coastguard Worker     signal_ice_gathering_state_.AddReceiver(std::forward<F>(callback));
267*d9f75844SAndroid Build Coastguard Worker   }
268*d9f75844SAndroid Build Coastguard Worker 
269*d9f75844SAndroid Build Coastguard Worker   // F: void(const cricket::IceCandidateErrorEvent&)
270*d9f75844SAndroid Build Coastguard Worker   template <typename F>
SubscribeIceCandidateError(F && callback)271*d9f75844SAndroid Build Coastguard Worker   void SubscribeIceCandidateError(F&& callback) {
272*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(network_thread_);
273*d9f75844SAndroid Build Coastguard Worker     signal_ice_candidate_error_.AddReceiver(std::forward<F>(callback));
274*d9f75844SAndroid Build Coastguard Worker   }
275*d9f75844SAndroid Build Coastguard Worker 
276*d9f75844SAndroid Build Coastguard Worker   // F: void(const std::vector<cricket::Candidate>&)
277*d9f75844SAndroid Build Coastguard Worker   template <typename F>
SubscribeIceCandidatesRemoved(F && callback)278*d9f75844SAndroid Build Coastguard Worker   void SubscribeIceCandidatesRemoved(F&& callback) {
279*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(network_thread_);
280*d9f75844SAndroid Build Coastguard Worker     signal_ice_candidates_removed_.AddReceiver(std::forward<F>(callback));
281*d9f75844SAndroid Build Coastguard Worker   }
282*d9f75844SAndroid Build Coastguard Worker 
283*d9f75844SAndroid Build Coastguard Worker   // F: void(const cricket::CandidatePairChangeEvent&)
284*d9f75844SAndroid Build Coastguard Worker   template <typename F>
SubscribeIceCandidatePairChanged(F && callback)285*d9f75844SAndroid Build Coastguard Worker   void SubscribeIceCandidatePairChanged(F&& callback) {
286*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(network_thread_);
287*d9f75844SAndroid Build Coastguard Worker     signal_ice_candidate_pair_changed_.AddReceiver(std::forward<F>(callback));
288*d9f75844SAndroid Build Coastguard Worker   }
289*d9f75844SAndroid Build Coastguard Worker 
290*d9f75844SAndroid Build Coastguard Worker  private:
291*d9f75844SAndroid Build Coastguard Worker   // All of these callbacks are fired on the network thread.
292*d9f75844SAndroid Build Coastguard Worker 
293*d9f75844SAndroid Build Coastguard Worker   // If any transport failed => failed,
294*d9f75844SAndroid Build Coastguard Worker   // Else if all completed => completed,
295*d9f75844SAndroid Build Coastguard Worker   // Else if all connected => connected,
296*d9f75844SAndroid Build Coastguard Worker   // Else => connecting
297*d9f75844SAndroid Build Coastguard Worker   CallbackList<cricket::IceConnectionState> signal_ice_connection_state_
298*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(network_thread_);
299*d9f75844SAndroid Build Coastguard Worker 
300*d9f75844SAndroid Build Coastguard Worker   CallbackList<PeerConnectionInterface::PeerConnectionState>
301*d9f75844SAndroid Build Coastguard Worker       signal_connection_state_ RTC_GUARDED_BY(network_thread_);
302*d9f75844SAndroid Build Coastguard Worker 
303*d9f75844SAndroid Build Coastguard Worker   CallbackList<PeerConnectionInterface::IceConnectionState>
304*d9f75844SAndroid Build Coastguard Worker       signal_standardized_ice_connection_state_ RTC_GUARDED_BY(network_thread_);
305*d9f75844SAndroid Build Coastguard Worker 
306*d9f75844SAndroid Build Coastguard Worker   // If all transports done gathering => complete,
307*d9f75844SAndroid Build Coastguard Worker   // Else if any are gathering => gathering,
308*d9f75844SAndroid Build Coastguard Worker   // Else => new
309*d9f75844SAndroid Build Coastguard Worker   CallbackList<cricket::IceGatheringState> signal_ice_gathering_state_
310*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(network_thread_);
311*d9f75844SAndroid Build Coastguard Worker 
312*d9f75844SAndroid Build Coastguard Worker   // [mid, candidates]
313*d9f75844SAndroid Build Coastguard Worker   CallbackList<const std::string&, const std::vector<cricket::Candidate>&>
314*d9f75844SAndroid Build Coastguard Worker       signal_ice_candidates_gathered_ RTC_GUARDED_BY(network_thread_);
315*d9f75844SAndroid Build Coastguard Worker 
316*d9f75844SAndroid Build Coastguard Worker   CallbackList<const cricket::IceCandidateErrorEvent&>
317*d9f75844SAndroid Build Coastguard Worker       signal_ice_candidate_error_ RTC_GUARDED_BY(network_thread_);
318*d9f75844SAndroid Build Coastguard Worker 
319*d9f75844SAndroid Build Coastguard Worker   CallbackList<const std::vector<cricket::Candidate>&>
320*d9f75844SAndroid Build Coastguard Worker       signal_ice_candidates_removed_ RTC_GUARDED_BY(network_thread_);
321*d9f75844SAndroid Build Coastguard Worker 
322*d9f75844SAndroid Build Coastguard Worker   CallbackList<const cricket::CandidatePairChangeEvent&>
323*d9f75844SAndroid Build Coastguard Worker       signal_ice_candidate_pair_changed_ RTC_GUARDED_BY(network_thread_);
324*d9f75844SAndroid Build Coastguard Worker 
325*d9f75844SAndroid Build Coastguard Worker   RTCError ApplyDescription_n(bool local,
326*d9f75844SAndroid Build Coastguard Worker                               SdpType type,
327*d9f75844SAndroid Build Coastguard Worker                               const cricket::SessionDescription* description)
328*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
329*d9f75844SAndroid Build Coastguard Worker   RTCError ValidateAndMaybeUpdateBundleGroups(
330*d9f75844SAndroid Build Coastguard Worker       bool local,
331*d9f75844SAndroid Build Coastguard Worker       SdpType type,
332*d9f75844SAndroid Build Coastguard Worker       const cricket::SessionDescription* description);
333*d9f75844SAndroid Build Coastguard Worker   RTCError ValidateContent(const cricket::ContentInfo& content_info);
334*d9f75844SAndroid Build Coastguard Worker 
335*d9f75844SAndroid Build Coastguard Worker   void HandleRejectedContent(const cricket::ContentInfo& content_info)
336*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
337*d9f75844SAndroid Build Coastguard Worker   bool HandleBundledContent(const cricket::ContentInfo& content_info,
338*d9f75844SAndroid Build Coastguard Worker                             const cricket::ContentGroup& bundle_group)
339*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
340*d9f75844SAndroid Build Coastguard Worker 
341*d9f75844SAndroid Build Coastguard Worker   cricket::JsepTransportDescription CreateJsepTransportDescription(
342*d9f75844SAndroid Build Coastguard Worker       const cricket::ContentInfo& content_info,
343*d9f75844SAndroid Build Coastguard Worker       const cricket::TransportInfo& transport_info,
344*d9f75844SAndroid Build Coastguard Worker       const std::vector<int>& encrypted_extension_ids,
345*d9f75844SAndroid Build Coastguard Worker       int rtp_abs_sendtime_extn_id);
346*d9f75844SAndroid Build Coastguard Worker 
347*d9f75844SAndroid Build Coastguard Worker   std::map<const cricket::ContentGroup*, std::vector<int>>
348*d9f75844SAndroid Build Coastguard Worker   MergeEncryptedHeaderExtensionIdsForBundles(
349*d9f75844SAndroid Build Coastguard Worker       const cricket::SessionDescription* description);
350*d9f75844SAndroid Build Coastguard Worker   std::vector<int> GetEncryptedHeaderExtensionIds(
351*d9f75844SAndroid Build Coastguard Worker       const cricket::ContentInfo& content_info);
352*d9f75844SAndroid Build Coastguard Worker 
353*d9f75844SAndroid Build Coastguard Worker   int GetRtpAbsSendTimeHeaderExtensionId(
354*d9f75844SAndroid Build Coastguard Worker       const cricket::ContentInfo& content_info);
355*d9f75844SAndroid Build Coastguard Worker 
356*d9f75844SAndroid Build Coastguard Worker   // This method takes the BUNDLE group into account. If the JsepTransport is
357*d9f75844SAndroid Build Coastguard Worker   // destroyed because of BUNDLE, it would return the transport which other
358*d9f75844SAndroid Build Coastguard Worker   // transports are bundled on (In current implementation, it is the first
359*d9f75844SAndroid Build Coastguard Worker   // content in the BUNDLE group).
360*d9f75844SAndroid Build Coastguard Worker   const cricket::JsepTransport* GetJsepTransportForMid(
361*d9f75844SAndroid Build Coastguard Worker       const std::string& mid) const RTC_RUN_ON(network_thread_);
362*d9f75844SAndroid Build Coastguard Worker   cricket::JsepTransport* GetJsepTransportForMid(const std::string& mid)
363*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
364*d9f75844SAndroid Build Coastguard Worker   const cricket::JsepTransport* GetJsepTransportForMid(
365*d9f75844SAndroid Build Coastguard Worker       absl::string_view mid) const RTC_RUN_ON(network_thread_);
366*d9f75844SAndroid Build Coastguard Worker   cricket::JsepTransport* GetJsepTransportForMid(absl::string_view mid)
367*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
368*d9f75844SAndroid Build Coastguard Worker 
369*d9f75844SAndroid Build Coastguard Worker   // Get the JsepTransport without considering the BUNDLE group. Return nullptr
370*d9f75844SAndroid Build Coastguard Worker   // if the JsepTransport is destroyed.
371*d9f75844SAndroid Build Coastguard Worker   const cricket::JsepTransport* GetJsepTransportByName(
372*d9f75844SAndroid Build Coastguard Worker       const std::string& transport_name) const RTC_RUN_ON(network_thread_);
373*d9f75844SAndroid Build Coastguard Worker   cricket::JsepTransport* GetJsepTransportByName(
374*d9f75844SAndroid Build Coastguard Worker       const std::string& transport_name) RTC_RUN_ON(network_thread_);
375*d9f75844SAndroid Build Coastguard Worker 
376*d9f75844SAndroid Build Coastguard Worker   // Creates jsep transport. Noop if transport is already created.
377*d9f75844SAndroid Build Coastguard Worker   // Transport is created either during SetLocalDescription (`local` == true) or
378*d9f75844SAndroid Build Coastguard Worker   // during SetRemoteDescription (`local` == false). Passing `local` helps to
379*d9f75844SAndroid Build Coastguard Worker   // differentiate initiator (caller) from answerer (callee).
380*d9f75844SAndroid Build Coastguard Worker   RTCError MaybeCreateJsepTransport(
381*d9f75844SAndroid Build Coastguard Worker       bool local,
382*d9f75844SAndroid Build Coastguard Worker       const cricket::ContentInfo& content_info,
383*d9f75844SAndroid Build Coastguard Worker       const cricket::SessionDescription& description)
384*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
385*d9f75844SAndroid Build Coastguard Worker 
386*d9f75844SAndroid Build Coastguard Worker   void DestroyAllJsepTransports_n() RTC_RUN_ON(network_thread_);
387*d9f75844SAndroid Build Coastguard Worker 
388*d9f75844SAndroid Build Coastguard Worker   void SetIceRole_n(cricket::IceRole ice_role) RTC_RUN_ON(network_thread_);
389*d9f75844SAndroid Build Coastguard Worker 
390*d9f75844SAndroid Build Coastguard Worker   cricket::IceRole DetermineIceRole(
391*d9f75844SAndroid Build Coastguard Worker       cricket::JsepTransport* jsep_transport,
392*d9f75844SAndroid Build Coastguard Worker       const cricket::TransportInfo& transport_info,
393*d9f75844SAndroid Build Coastguard Worker       SdpType type,
394*d9f75844SAndroid Build Coastguard Worker       bool local);
395*d9f75844SAndroid Build Coastguard Worker 
396*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
397*d9f75844SAndroid Build Coastguard Worker       const cricket::ContentInfo& content_info,
398*d9f75844SAndroid Build Coastguard Worker       cricket::IceTransportInternal* ice);
399*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<webrtc::IceTransportInterface> CreateIceTransport(
400*d9f75844SAndroid Build Coastguard Worker       const std::string& transport_name,
401*d9f75844SAndroid Build Coastguard Worker       bool rtcp);
402*d9f75844SAndroid Build Coastguard Worker 
403*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
404*d9f75844SAndroid Build Coastguard Worker       const std::string& transport_name,
405*d9f75844SAndroid Build Coastguard Worker       rtc::PacketTransportInternal* rtp_packet_transport,
406*d9f75844SAndroid Build Coastguard Worker       rtc::PacketTransportInternal* rtcp_packet_transport);
407*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
408*d9f75844SAndroid Build Coastguard Worker       const std::string& transport_name,
409*d9f75844SAndroid Build Coastguard Worker       cricket::DtlsTransportInternal* rtp_dtls_transport,
410*d9f75844SAndroid Build Coastguard Worker       cricket::DtlsTransportInternal* rtcp_dtls_transport);
411*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
412*d9f75844SAndroid Build Coastguard Worker       const std::string& transport_name,
413*d9f75844SAndroid Build Coastguard Worker       cricket::DtlsTransportInternal* rtp_dtls_transport,
414*d9f75844SAndroid Build Coastguard Worker       cricket::DtlsTransportInternal* rtcp_dtls_transport);
415*d9f75844SAndroid Build Coastguard Worker 
416*d9f75844SAndroid Build Coastguard Worker   // Collect all the DtlsTransports, including RTP and RTCP, from the
417*d9f75844SAndroid Build Coastguard Worker   // JsepTransports, including those not mapped to a MID because they are being
418*d9f75844SAndroid Build Coastguard Worker   // kept alive in case of rollback.
419*d9f75844SAndroid Build Coastguard Worker   std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
420*d9f75844SAndroid Build Coastguard Worker   // Same as the above, but doesn't include rollback transports.
421*d9f75844SAndroid Build Coastguard Worker   // JsepTransportController can iterate all the DtlsTransports and update the
422*d9f75844SAndroid Build Coastguard Worker   // aggregate states.
423*d9f75844SAndroid Build Coastguard Worker   std::vector<cricket::DtlsTransportInternal*> GetActiveDtlsTransports();
424*d9f75844SAndroid Build Coastguard Worker 
425*d9f75844SAndroid Build Coastguard Worker   // Handlers for signals from Transport.
426*d9f75844SAndroid Build Coastguard Worker   void OnTransportWritableState_n(rtc::PacketTransportInternal* transport)
427*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
428*d9f75844SAndroid Build Coastguard Worker   void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport)
429*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
430*d9f75844SAndroid Build Coastguard Worker   void OnTransportGatheringState_n(cricket::IceTransportInternal* transport)
431*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
432*d9f75844SAndroid Build Coastguard Worker   void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
433*d9f75844SAndroid Build Coastguard Worker                                       const cricket::Candidate& candidate)
434*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
435*d9f75844SAndroid Build Coastguard Worker   void OnTransportCandidateError_n(cricket::IceTransportInternal* transport,
436*d9f75844SAndroid Build Coastguard Worker                                    const cricket::IceCandidateErrorEvent& event)
437*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
438*d9f75844SAndroid Build Coastguard Worker   void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
439*d9f75844SAndroid Build Coastguard Worker                                       const cricket::Candidates& candidates)
440*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
441*d9f75844SAndroid Build Coastguard Worker   void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport)
442*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
443*d9f75844SAndroid Build Coastguard Worker   void OnTransportStateChanged_n(cricket::IceTransportInternal* transport)
444*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
445*d9f75844SAndroid Build Coastguard Worker   void OnTransportCandidatePairChanged_n(
446*d9f75844SAndroid Build Coastguard Worker       const cricket::CandidatePairChangeEvent& event)
447*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
448*d9f75844SAndroid Build Coastguard Worker   void UpdateAggregateStates_n() RTC_RUN_ON(network_thread_);
449*d9f75844SAndroid Build Coastguard Worker 
450*d9f75844SAndroid Build Coastguard Worker   void OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer* packet,
451*d9f75844SAndroid Build Coastguard Worker                               int64_t packet_time_us)
452*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(network_thread_);
453*d9f75844SAndroid Build Coastguard Worker 
454*d9f75844SAndroid Build Coastguard Worker   void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
455*d9f75844SAndroid Build Coastguard Worker 
456*d9f75844SAndroid Build Coastguard Worker   bool OnTransportChanged(const std::string& mid,
457*d9f75844SAndroid Build Coastguard Worker                           cricket::JsepTransport* transport);
458*d9f75844SAndroid Build Coastguard Worker 
459*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* const network_thread_ = nullptr;
460*d9f75844SAndroid Build Coastguard Worker   cricket::PortAllocator* const port_allocator_ = nullptr;
461*d9f75844SAndroid Build Coastguard Worker   AsyncDnsResolverFactoryInterface* const async_dns_resolver_factory_ = nullptr;
462*d9f75844SAndroid Build Coastguard Worker 
463*d9f75844SAndroid Build Coastguard Worker   JsepTransportCollection transports_ RTC_GUARDED_BY(network_thread_);
464*d9f75844SAndroid Build Coastguard Worker   // Aggregate states for Transports.
465*d9f75844SAndroid Build Coastguard Worker   // standardized_ice_connection_state_ is intended to replace
466*d9f75844SAndroid Build Coastguard Worker   // ice_connection_state, see bugs.webrtc.org/9308
467*d9f75844SAndroid Build Coastguard Worker   cricket::IceConnectionState ice_connection_state_ =
468*d9f75844SAndroid Build Coastguard Worker       cricket::kIceConnectionConnecting;
469*d9f75844SAndroid Build Coastguard Worker   PeerConnectionInterface::IceConnectionState
470*d9f75844SAndroid Build Coastguard Worker       standardized_ice_connection_state_ =
471*d9f75844SAndroid Build Coastguard Worker           PeerConnectionInterface::kIceConnectionNew;
472*d9f75844SAndroid Build Coastguard Worker   PeerConnectionInterface::PeerConnectionState combined_connection_state_ =
473*d9f75844SAndroid Build Coastguard Worker       PeerConnectionInterface::PeerConnectionState::kNew;
474*d9f75844SAndroid Build Coastguard Worker   cricket::IceGatheringState ice_gathering_state_ = cricket::kIceGatheringNew;
475*d9f75844SAndroid Build Coastguard Worker 
476*d9f75844SAndroid Build Coastguard Worker   const Config config_;
477*d9f75844SAndroid Build Coastguard Worker   bool active_reset_srtp_params_ RTC_GUARDED_BY(network_thread_);
478*d9f75844SAndroid Build Coastguard Worker 
479*d9f75844SAndroid Build Coastguard Worker   const cricket::SessionDescription* local_desc_ = nullptr;
480*d9f75844SAndroid Build Coastguard Worker   const cricket::SessionDescription* remote_desc_ = nullptr;
481*d9f75844SAndroid Build Coastguard Worker   absl::optional<bool> initial_offerer_;
482*d9f75844SAndroid Build Coastguard Worker 
483*d9f75844SAndroid Build Coastguard Worker   cricket::IceConfig ice_config_;
484*d9f75844SAndroid Build Coastguard Worker   cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
485*d9f75844SAndroid Build Coastguard Worker   uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
486*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
487*d9f75844SAndroid Build Coastguard Worker 
488*d9f75844SAndroid Build Coastguard Worker   BundleManager bundles_;
489*d9f75844SAndroid Build Coastguard Worker };
490*d9f75844SAndroid Build Coastguard Worker 
491*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
492*d9f75844SAndroid Build Coastguard Worker 
493*d9f75844SAndroid Build Coastguard Worker #endif  // PC_JSEP_TRANSPORT_CONTROLLER_H_
494