blob: e3c1187fb4bd8f8d4fb3f10f590ab83805571c3f [file] [log] [blame]
Zhi Huange818b6e2018-02-22 15:26:27 -08001/*
2 * Copyright 2017 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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_JSEP_TRANSPORT_CONTROLLER_H_
12#define PC_JSEP_TRANSPORT_CONTROLLER_H_
Zhi Huange818b6e2018-02-22 15:26:27 -080013
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000014#include <stdint.h>
15
16#include <functional>
Zhi Huange818b6e2018-02-22 15:26:27 -080017#include <map>
18#include <memory>
19#include <string>
20#include <utility>
21#include <vector>
22
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000023#include "absl/types/optional.h"
Harald Alvestrand0ccfbd22021-04-08 07:25:04 +000024#include "api/async_dns_resolver.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080025#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "api/crypto/crypto_options.h"
Qingsi Wang25ec8882019-11-15 12:33:05 -080027#include "api/ice_transport_factory.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000028#include "api/ice_transport_interface.h"
29#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "api/peer_connection_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000031#include "api/rtc_error.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020032#include "api/rtc_event_log/rtc_event_log.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000033#include "api/scoped_refptr.h"
34#include "api/transport/data_channel_transport_interface.h"
35#include "api/transport/sctp_transport_factory_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "media/sctp/sctp_transport_internal.h"
37#include "p2p/base/dtls_transport.h"
Qingsi Wang25ec8882019-11-15 12:33:05 -080038#include "p2p/base/dtls_transport_factory.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000039#include "p2p/base/dtls_transport_internal.h"
40#include "p2p/base/ice_transport_internal.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "p2p/base/p2p_transport_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000042#include "p2p/base/packet_transport_internal.h"
43#include "p2p/base/port.h"
44#include "p2p/base/port_allocator.h"
45#include "p2p/base/transport_description.h"
46#include "p2p/base/transport_info.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080047#include "pc/channel.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "pc/dtls_srtp_transport.h"
49#include "pc/dtls_transport.h"
50#include "pc/jsep_transport.h"
51#include "pc/rtp_transport.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000052#include "pc/rtp_transport_internal.h"
53#include "pc/sctp_transport.h"
54#include "pc/session_description.h"
Steve Anton10542f22019-01-11 09:11:00 -080055#include "pc/srtp_transport.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000056#include "pc/transport_stats.h"
Mirko Bonadei3d259352020-10-23 12:04:40 +020057#include "rtc_base/callback_list.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000058#include "rtc_base/constructor_magic.h"
59#include "rtc_base/copy_on_write_buffer.h"
60#include "rtc_base/helpers.h"
61#include "rtc_base/ref_counted_object.h"
62#include "rtc_base/rtc_certificate.h"
63#include "rtc_base/ssl_certificate.h"
64#include "rtc_base/ssl_stream_adapter.h"
Artem Titove41c4332018-07-25 15:04:28 +020065#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000066#include "rtc_base/thread.h"
67#include "rtc_base/thread_annotations.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080068
69namespace rtc {
70class Thread;
71class PacketTransportInternal;
72} // namespace rtc
73
74namespace webrtc {
75
Steve Antond25828a2018-08-31 13:06:05 -070076class JsepTransportController : public sigslot::has_slots<> {
Zhi Huange818b6e2018-02-22 15:26:27 -080077 public:
Zhi Huang365381f2018-04-13 16:44:34 -070078 // Used when the RtpTransport/DtlsTransport of the m= section is changed
79 // because the section is rejected or BUNDLE is enabled.
80 class Observer {
81 public:
82 virtual ~Observer() {}
83
84 // Returns true if media associated with |mid| was successfully set up to be
85 // demultiplexed on |rtp_transport|. Could return false if two bundled m=
86 // sections use the same SSRC, for example.
Bjorn A Mellemb689af42019-08-21 10:44:59 -070087 //
88 // If a data channel transport must be negotiated, |data_channel_transport|
89 // and |negotiation_state| indicate negotiation status. If
90 // |data_channel_transport| is null, the data channel transport should not
91 // be used. Otherwise, the value is a pointer to the transport to be used
92 // for data channels on |mid|, if any.
93 //
94 // The observer should not send data on |data_channel_transport| until
95 // |negotiation_state| is provisional or final. It should not delete
96 // |data_channel_transport| or any fallback transport until
97 // |negotiation_state| is final.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070098 virtual bool OnTransportChanged(
Zhi Huang365381f2018-04-13 16:44:34 -070099 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700100 RtpTransportInternal* rtp_transport,
Harald Alvestrandc85328f2019-02-28 07:51:00 +0100101 rtc::scoped_refptr<DtlsTransport> dtls_transport,
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700102 DataChannelTransportInterface* data_channel_transport) = 0;
Zhi Huang365381f2018-04-13 16:44:34 -0700103 };
104
Zhi Huange818b6e2018-02-22 15:26:27 -0800105 struct Config {
106 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
107 // upon setting a local transport description that indicates an ICE
108 // restart.
109 bool redetermine_role_on_ice_restart = true;
110 rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
111 // |crypto_options| is used to determine if created DTLS transports
112 // negotiate GCM crypto suites or not.
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700113 webrtc::CryptoOptions crypto_options;
Zhi Huange818b6e2018-02-22 15:26:27 -0800114 PeerConnectionInterface::BundlePolicy bundle_policy =
115 PeerConnectionInterface::kBundlePolicyBalanced;
116 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
117 PeerConnectionInterface::kRtcpMuxPolicyRequire;
118 bool disable_encryption = false;
119 bool enable_external_auth = false;
120 // Used to inject the ICE/DTLS transports created externally.
Qingsi Wang25ec8882019-11-15 12:33:05 -0800121 webrtc::IceTransportFactory* ice_transport_factory = nullptr;
122 cricket::DtlsTransportFactory* dtls_transport_factory = nullptr;
Zhi Huang365381f2018-04-13 16:44:34 -0700123 Observer* transport_observer = nullptr;
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +0200124 // Must be provided and valid for the lifetime of the
125 // JsepTransportController instance.
126 std::function<void(const rtc::CopyOnWriteBuffer& packet,
127 int64_t packet_time_us)>
128 rtcp_handler;
Harald Alvestrand133c0522021-01-29 14:27:12 +0000129 // Initial value for whether DtlsTransport reset causes a reset
130 // of SRTP parameters.
Zhi Huangb57e1692018-06-12 11:41:11 -0700131 bool active_reset_srtp_params = false;
Qingsi Wang7685e862018-06-11 20:15:46 -0700132 RtcEventLog* event_log = nullptr;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700133
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700134 // Factory for SCTP transports.
Per Kjellander2bca0082020-08-28 09:15:15 +0200135 SctpTransportFactoryInterface* sctp_factory = nullptr;
Lahiru Ginnaliya Gamathige70f9e242021-01-27 23:32:46 -0800136 std::function<void(const rtc::SSLHandshakeError)> on_dtls_handshake_error_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800137 };
138
Tommic3257d02021-02-10 17:40:08 +0000139 // The ICE related events are fired on the |network_thread|.
140 // All the transport related methods are called on the |network_thread|
141 // and destruction of the JsepTransportController must occur on the
142 // |network_thread|.
Harald Alvestrand0ccfbd22021-04-08 07:25:04 +0000143 JsepTransportController(
144 rtc::Thread* network_thread,
145 cricket::PortAllocator* port_allocator,
146 AsyncDnsResolverFactoryInterface* async_dns_resolver_factory,
147 Config config);
Zhi Huange818b6e2018-02-22 15:26:27 -0800148 virtual ~JsepTransportController();
149
150 // The main method to be called; applies a description at the transport
151 // level, creating/destroying transport objects as needed and updating their
152 // properties. This includes RTP, DTLS, and ICE (but not SCTP). At least not
153 // yet? May make sense to in the future.
154 RTCError SetLocalDescription(SdpType type,
155 const cricket::SessionDescription* description);
156
157 RTCError SetRemoteDescription(SdpType type,
158 const cricket::SessionDescription* description);
159
160 // Get transports to be used for the provided |mid|. If bundling is enabled,
161 // calling GetRtpTransport for multiple MIDs may yield the same object.
162 RtpTransportInternal* GetRtpTransport(const std::string& mid) const;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100163 cricket::DtlsTransportInternal* GetDtlsTransport(const std::string& mid);
164 const cricket::DtlsTransportInternal* GetRtcpDtlsTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -0800165 const std::string& mid) const;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100166 // Gets the externally sharable version of the DtlsTransport.
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100167 rtc::scoped_refptr<webrtc::DtlsTransport> LookupDtlsTransportByMid(
Harald Alvestrandad88c882018-11-28 16:47:46 +0100168 const std::string& mid);
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700169 rtc::scoped_refptr<SctpTransport> GetSctpTransport(
170 const std::string& mid) const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800171
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700172 DataChannelTransportInterface* GetDataChannelTransport(
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700173 const std::string& mid) const;
174
Zhi Huange818b6e2018-02-22 15:26:27 -0800175 /*********************
176 * ICE-related methods
177 ********************/
178 // This method is public to allow PeerConnection to update it from
179 // SetConfiguration.
180 void SetIceConfig(const cricket::IceConfig& config);
181 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
182 // set, offers should generate new ufrags/passwords until an ICE restart
183 // occurs.
184 void SetNeedsIceRestartFlag();
185 // Returns true if the ICE restart flag above was set, and no ICE restart has
186 // occurred yet for this transport (by applying a local description with
187 // changed ufrag/password). If the transport has been deleted as a result of
188 // bundling, returns false.
189 bool NeedsIceRestart(const std::string& mid) const;
190 // Start gathering candidates for any new transports, or transports doing an
191 // ICE restart.
192 void MaybeStartGathering();
193 RTCError AddRemoteCandidates(
194 const std::string& mid,
195 const std::vector<cricket::Candidate>& candidates);
196 RTCError RemoveRemoteCandidates(
197 const std::vector<cricket::Candidate>& candidates);
198
199 /**********************
200 * DTLS-related methods
201 *********************/
202 // Specifies the identity to use in this session.
203 // Can only be called once.
204 bool SetLocalCertificate(
205 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
206 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
207 const std::string& mid) const;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800208 // Caller owns returned certificate chain. This method mainly exists for
209 // stats reporting.
210 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800211 const std::string& mid) const;
212 // Get negotiated role, if one has been negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200213 absl::optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800214
215 // TODO(deadbeef): GetStats isn't const because all the way down to
216 // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
217 // const. Fix this.
218 bool GetStats(const std::string& mid, cricket::TransportStats* stats);
Zhi Huange818b6e2018-02-22 15:26:27 -0800219
Zhi Huange830e682018-03-30 10:48:35 -0700220 bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; }
Zhi Huang365381f2018-04-13 16:44:34 -0700221
Zhi Huangb57e1692018-06-12 11:41:11 -0700222 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
223
Eldar Rellod85ea752020-02-19 20:41:07 +0200224 // For now the rollback only removes mid to transport mappings
Eldar Rello353a7182019-11-25 18:49:44 +0200225 // and deletes unused transports, but doesn't consider anything more complex.
Eldar Rellod85ea752020-02-19 20:41:07 +0200226 void RollbackTransports();
Eldar Rello5ab79e62019-10-09 18:29:44 +0300227
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800228 // F: void(const std::string&, const std::vector<cricket::Candidate>&)
229 template <typename F>
230 void SubscribeIceCandidateGathered(F&& callback) {
Tommic3257d02021-02-10 17:40:08 +0000231 RTC_DCHECK_RUN_ON(network_thread_);
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800232 signal_ice_candidates_gathered_.AddReceiver(std::forward<F>(callback));
233 }
234
235 // F: void(cricket::IceConnectionState)
236 template <typename F>
237 void SubscribeIceConnectionState(F&& callback) {
Tommic3257d02021-02-10 17:40:08 +0000238 RTC_DCHECK_RUN_ON(network_thread_);
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800239 signal_ice_connection_state_.AddReceiver(std::forward<F>(callback));
240 }
241
242 // F: void(PeerConnectionInterface::PeerConnectionState)
243 template <typename F>
244 void SubscribeConnectionState(F&& callback) {
Tommic3257d02021-02-10 17:40:08 +0000245 RTC_DCHECK_RUN_ON(network_thread_);
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800246 signal_connection_state_.AddReceiver(std::forward<F>(callback));
247 }
248
249 // F: void(PeerConnectionInterface::IceConnectionState)
250 template <typename F>
251 void SubscribeStandardizedIceConnectionState(F&& callback) {
Tommic3257d02021-02-10 17:40:08 +0000252 RTC_DCHECK_RUN_ON(network_thread_);
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800253 signal_standardized_ice_connection_state_.AddReceiver(
254 std::forward<F>(callback));
255 }
256
257 // F: void(cricket::IceGatheringState)
258 template <typename F>
259 void SubscribeIceGatheringState(F&& callback) {
Tommic3257d02021-02-10 17:40:08 +0000260 RTC_DCHECK_RUN_ON(network_thread_);
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800261 signal_ice_gathering_state_.AddReceiver(std::forward<F>(callback));
262 }
263
264 // F: void(const cricket::IceCandidateErrorEvent&)
265 template <typename F>
266 void SubscribeIceCandidateError(F&& callback) {
Tommic3257d02021-02-10 17:40:08 +0000267 RTC_DCHECK_RUN_ON(network_thread_);
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800268 signal_ice_candidate_error_.AddReceiver(std::forward<F>(callback));
269 }
270
271 // F: void(const std::vector<cricket::Candidate>&)
272 template <typename F>
273 void SubscribeIceCandidatesRemoved(F&& callback) {
Tommic3257d02021-02-10 17:40:08 +0000274 RTC_DCHECK_RUN_ON(network_thread_);
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800275 signal_ice_candidates_removed_.AddReceiver(std::forward<F>(callback));
276 }
277
278 // F: void(const cricket::CandidatePairChangeEvent&)
279 template <typename F>
280 void SubscribeIceCandidatePairChanged(F&& callback) {
Tommic3257d02021-02-10 17:40:08 +0000281 RTC_DCHECK_RUN_ON(network_thread_);
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800282 signal_ice_candidate_pair_changed_.AddReceiver(std::forward<F>(callback));
283 }
284
285 private:
Tommic3257d02021-02-10 17:40:08 +0000286 // All of these callbacks are fired on the network thread.
Zhi Huange818b6e2018-02-22 15:26:27 -0800287
288 // If any transport failed => failed,
289 // Else if all completed => completed,
290 // Else if all connected => connected,
291 // Else => connecting
Tommic3257d02021-02-10 17:40:08 +0000292 CallbackList<cricket::IceConnectionState> signal_ice_connection_state_
293 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800294
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800295 CallbackList<PeerConnectionInterface::PeerConnectionState>
Tommic3257d02021-02-10 17:40:08 +0000296 signal_connection_state_ RTC_GUARDED_BY(network_thread_);
Lahiru Ginnaliya Gamathigee99c68d2020-09-30 14:33:45 -0700297
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800298 CallbackList<PeerConnectionInterface::IceConnectionState>
Tommic3257d02021-02-10 17:40:08 +0000299 signal_standardized_ice_connection_state_ RTC_GUARDED_BY(network_thread_);
Jonas Olsson635474e2018-10-18 15:58:17 +0200300
Zhi Huange818b6e2018-02-22 15:26:27 -0800301 // If all transports done gathering => complete,
302 // Else if any are gathering => gathering,
303 // Else => new
Tommic3257d02021-02-10 17:40:08 +0000304 CallbackList<cricket::IceGatheringState> signal_ice_gathering_state_
305 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800306
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800307 // [mid, candidates]
308 CallbackList<const std::string&, const std::vector<cricket::Candidate>&>
Tommic3257d02021-02-10 17:40:08 +0000309 signal_ice_candidates_gathered_ RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800310
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800311 CallbackList<const cricket::IceCandidateErrorEvent&>
Tommic3257d02021-02-10 17:40:08 +0000312 signal_ice_candidate_error_ RTC_GUARDED_BY(network_thread_);
Eldar Relloda13ea22019-06-01 12:23:43 +0300313
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800314 CallbackList<const std::vector<cricket::Candidate>&>
Tommic3257d02021-02-10 17:40:08 +0000315 signal_ice_candidates_removed_ RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800316
Lahiru Ginnaliya Gamathige5eb527c2021-01-18 23:32:22 -0800317 CallbackList<const cricket::CandidatePairChangeEvent&>
Tommic3257d02021-02-10 17:40:08 +0000318 signal_ice_candidate_pair_changed_ RTC_GUARDED_BY(network_thread_);
Alex Drake00c7ecf2019-08-06 10:54:47 -0700319
Zhi Huange818b6e2018-02-22 15:26:27 -0800320 RTCError ApplyDescription_n(bool local,
321 SdpType type,
Niels Möller5cf0ef02021-01-18 14:00:36 +0100322 const cricket::SessionDescription* description)
323 RTC_RUN_ON(network_thread_);
Henrik Boströmf8187e02021-04-26 21:04:26 +0200324 RTCError ValidateAndMaybeUpdateBundleGroups(
Zhi Huangd2248f82018-04-10 14:41:03 -0700325 bool local,
326 SdpType type,
327 const cricket::SessionDescription* description);
Zhi Huange830e682018-03-30 10:48:35 -0700328 RTCError ValidateContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800329
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700330 void HandleRejectedContent(const cricket::ContentInfo& content_info,
Henrik Boströmf8187e02021-04-26 21:04:26 +0200331 std::map<std::string, cricket::ContentGroup*>&
332 established_bundle_groups_by_mid)
Niels Möller5cf0ef02021-01-18 14:00:36 +0100333 RTC_RUN_ON(network_thread_);
Henrik Boströmf8187e02021-04-26 21:04:26 +0200334 bool HandleBundledContent(const cricket::ContentInfo& content_info,
335 const cricket::ContentGroup& bundle_group)
Niels Möller5cf0ef02021-01-18 14:00:36 +0100336 RTC_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800337
Zhi Huang365381f2018-04-13 16:44:34 -0700338 bool SetTransportForMid(const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700339 cricket::JsepTransport* jsep_transport);
340 void RemoveTransportForMid(const std::string& mid);
Zhi Huangd2248f82018-04-10 14:41:03 -0700341
Zhi Huange818b6e2018-02-22 15:26:27 -0800342 cricket::JsepTransportDescription CreateJsepTransportDescription(
Harald Alvestrand1716d392019-06-03 20:35:45 +0200343 const cricket::ContentInfo& content_info,
344 const cricket::TransportInfo& transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700345 const std::vector<int>& encrypted_extension_ids,
Niels Möllerdc80aaf2020-06-18 10:10:17 +0200346 int rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800347
Zhi Huange818b6e2018-02-22 15:26:27 -0800348 bool ShouldUpdateBundleGroup(SdpType type,
349 const cricket::SessionDescription* description);
350
Henrik Boströmf8187e02021-04-26 21:04:26 +0200351 std::map<const cricket::ContentGroup*, std::vector<int>>
352 MergeEncryptedHeaderExtensionIdsForBundles(
353 const std::map<std::string, cricket::ContentGroup*>& bundle_groups_by_mid,
Zhi Huange818b6e2018-02-22 15:26:27 -0800354 const cricket::SessionDescription* description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800355 std::vector<int> GetEncryptedHeaderExtensionIds(
356 const cricket::ContentInfo& content_info);
357
Zhi Huange830e682018-03-30 10:48:35 -0700358 int GetRtpAbsSendTimeHeaderExtensionId(
359 const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800360
Zhi Huange830e682018-03-30 10:48:35 -0700361 // This method takes the BUNDLE group into account. If the JsepTransport is
362 // destroyed because of BUNDLE, it would return the transport which other
363 // transports are bundled on (In current implementation, it is the first
364 // content in the BUNDLE group).
Zhi Huang365381f2018-04-13 16:44:34 -0700365 const cricket::JsepTransport* GetJsepTransportForMid(
Tomas Gunnarsson92eebef2021-02-10 13:05:44 +0100366 const std::string& mid) const RTC_RUN_ON(network_thread_);
367 cricket::JsepTransport* GetJsepTransportForMid(const std::string& mid)
368 RTC_RUN_ON(network_thread_);
Zhi Huange830e682018-03-30 10:48:35 -0700369
370 // Get the JsepTransport without considering the BUNDLE group. Return nullptr
371 // if the JsepTransport is destroyed.
Zhi Huang365381f2018-04-13 16:44:34 -0700372 const cricket::JsepTransport* GetJsepTransportByName(
Tomas Gunnarsson20f74562021-02-04 10:22:50 +0100373 const std::string& transport_name) const RTC_RUN_ON(network_thread_);
Zhi Huang365381f2018-04-13 16:44:34 -0700374 cricket::JsepTransport* GetJsepTransportByName(
Tomas Gunnarsson20f74562021-02-04 10:22:50 +0100375 const std::string& transport_name) RTC_RUN_ON(network_thread_);
Zhi Huange830e682018-03-30 10:48:35 -0700376
Anton Sukhanov7940da02018-10-10 10:34:49 -0700377 // Creates jsep transport. Noop if transport is already created.
378 // Transport is created either during SetLocalDescription (|local| == true) or
379 // during SetRemoteDescription (|local| == false). Passing |local| helps to
380 // differentiate initiator (caller) from answerer (callee).
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -0800381 RTCError MaybeCreateJsepTransport(
382 bool local,
383 const cricket::ContentInfo& content_info,
Niels Möller5cf0ef02021-01-18 14:00:36 +0100384 const cricket::SessionDescription& description)
385 RTC_RUN_ON(network_thread_);
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800386
Niels Möller5cf0ef02021-01-18 14:00:36 +0100387 void MaybeDestroyJsepTransport(const std::string& mid)
388 RTC_RUN_ON(network_thread_);
389 void DestroyAllJsepTransports_n() RTC_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800390
Niels Möller5cf0ef02021-01-18 14:00:36 +0100391 void SetIceRole_n(cricket::IceRole ice_role) RTC_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800392
393 cricket::IceRole DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -0700394 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -0800395 const cricket::TransportInfo& transport_info,
396 SdpType type,
397 bool local);
398
399 std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
Anton Sukhanovac6c0962019-07-10 15:44:56 -0700400 const cricket::ContentInfo& content_info,
Niels Möller2a707032020-06-16 16:39:13 +0200401 cricket::IceTransportInternal* ice);
Qingsi Wang25ec8882019-11-15 12:33:05 -0800402 rtc::scoped_refptr<webrtc::IceTransportInterface> CreateIceTransport(
403 const std::string& transport_name,
Zhi Huange818b6e2018-02-22 15:26:27 -0800404 bool rtcp);
405
406 std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
407 const std::string& transport_name,
408 rtc::PacketTransportInternal* rtp_packet_transport,
409 rtc::PacketTransportInternal* rtcp_packet_transport);
410 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
411 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700412 cricket::DtlsTransportInternal* rtp_dtls_transport,
413 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800414 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
415 const std::string& transport_name,
416 cricket::DtlsTransportInternal* rtp_dtls_transport,
417 cricket::DtlsTransportInternal* rtcp_dtls_transport);
418
419 // Collect all the DtlsTransports, including RTP and RTCP, from the
420 // JsepTransports. JsepTransportController can iterate all the DtlsTransports
421 // and update the aggregate states.
422 std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
423
424 // Handlers for signals from Transport.
Niels Möller5cf0ef02021-01-18 14:00:36 +0100425 void OnTransportWritableState_n(rtc::PacketTransportInternal* transport)
426 RTC_RUN_ON(network_thread_);
427 void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport)
428 RTC_RUN_ON(network_thread_);
429 void OnTransportGatheringState_n(cricket::IceTransportInternal* transport)
430 RTC_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800431 void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
Niels Möller5cf0ef02021-01-18 14:00:36 +0100432 const cricket::Candidate& candidate)
433 RTC_RUN_ON(network_thread_);
434 void OnTransportCandidateError_n(cricket::IceTransportInternal* transport,
435 const cricket::IceCandidateErrorEvent& event)
436 RTC_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800437 void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
Niels Möller5cf0ef02021-01-18 14:00:36 +0100438 const cricket::Candidates& candidates)
439 RTC_RUN_ON(network_thread_);
440 void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport)
441 RTC_RUN_ON(network_thread_);
442 void OnTransportStateChanged_n(cricket::IceTransportInternal* transport)
443 RTC_RUN_ON(network_thread_);
Alex Drake00c7ecf2019-08-06 10:54:47 -0700444 void OnTransportCandidatePairChanged_n(
Niels Möller5cf0ef02021-01-18 14:00:36 +0100445 const cricket::CandidatePairChangeEvent& event)
446 RTC_RUN_ON(network_thread_);
447 void UpdateAggregateStates_n() RTC_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800448
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +0200449 void OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer* packet,
Niels Möller5cf0ef02021-01-18 14:00:36 +0100450 int64_t packet_time_us)
451 RTC_RUN_ON(network_thread_);
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +0200452
Zhi Huange818b6e2018-02-22 15:26:27 -0800453 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
454
Zhi Huange818b6e2018-02-22 15:26:27 -0800455 rtc::Thread* const network_thread_ = nullptr;
456 cricket::PortAllocator* const port_allocator_ = nullptr;
Harald Alvestrand0ccfbd22021-04-08 07:25:04 +0000457 AsyncDnsResolverFactoryInterface* const async_dns_resolver_factory_ = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800458
Zhi Huang365381f2018-04-13 16:44:34 -0700459 std::map<std::string, std::unique_ptr<cricket::JsepTransport>>
Tomas Gunnarsson20f74562021-02-04 10:22:50 +0100460 jsep_transports_by_name_ RTC_GUARDED_BY(network_thread_);
Zhi Huangd2248f82018-04-10 14:41:03 -0700461 // This keeps track of the mapping between media section
Zhi Huang365381f2018-04-13 16:44:34 -0700462 // (BaseChannel/SctpTransport) and the JsepTransport underneath.
Tomas Gunnarsson92eebef2021-02-10 13:05:44 +0100463 std::map<std::string, cricket::JsepTransport*> mid_to_transport_
464 RTC_GUARDED_BY(network_thread_);
Eldar Rellod85ea752020-02-19 20:41:07 +0200465 // Keep track of mids that have been mapped to transports. Used for rollback.
466 std::vector<std::string> pending_mids_ RTC_GUARDED_BY(network_thread_);
Jonas Olsson635474e2018-10-18 15:58:17 +0200467 // Aggregate states for Transports.
Alex Loiko9289eda2018-11-23 16:18:59 +0000468 // standardized_ice_connection_state_ is intended to replace
469 // ice_connection_state, see bugs.webrtc.org/9308
470 cricket::IceConnectionState ice_connection_state_ =
471 cricket::kIceConnectionConnecting;
472 PeerConnectionInterface::IceConnectionState
473 standardized_ice_connection_state_ =
474 PeerConnectionInterface::kIceConnectionNew;
Jonas Olsson635474e2018-10-18 15:58:17 +0200475 PeerConnectionInterface::PeerConnectionState combined_connection_state_ =
476 PeerConnectionInterface::PeerConnectionState::kNew;
Zhi Huange818b6e2018-02-22 15:26:27 -0800477 cricket::IceGatheringState ice_gathering_state_ = cricket::kIceGatheringNew;
478
Harald Alvestrand133c0522021-01-29 14:27:12 +0000479 const Config config_;
480 bool active_reset_srtp_params_ RTC_GUARDED_BY(network_thread_);
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800481
Zhi Huange818b6e2018-02-22 15:26:27 -0800482 const cricket::SessionDescription* local_desc_ = nullptr;
483 const cricket::SessionDescription* remote_desc_ = nullptr;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200484 absl::optional<bool> initial_offerer_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800485
Henrik Boströmf8187e02021-04-26 21:04:26 +0200486 // Use unique_ptr<> to get a stable address.
487 std::vector<std::unique_ptr<cricket::ContentGroup>> bundle_groups_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800488
489 cricket::IceConfig ice_config_;
490 cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
491 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
492 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800493
Zhi Huange818b6e2018-02-22 15:26:27 -0800494 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransportController);
495};
496
497} // namespace webrtc
498
Steve Anton10542f22019-01-11 09:11:00 -0800499#endif // PC_JSEP_TRANSPORT_CONTROLLER_H_