blob: d575592f1edc794abcd3189b74bc809f359b92cc [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
14#include <map>
15#include <memory>
16#include <string>
17#include <utility>
18#include <vector>
19
20#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "api/crypto/crypto_options.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/peer_connection_interface.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020023#include "api/rtc_event_log/rtc_event_log.h"
Niels Möller65f17ca2019-09-12 13:59:36 +020024#include "api/transport/media/media_transport_config.h"
25#include "api/transport/media/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "media/sctp/sctp_transport_internal.h"
27#include "p2p/base/dtls_transport.h"
28#include "p2p/base/p2p_transport_channel.h"
29#include "p2p/base/transport_factory_interface.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080030#include "pc/channel.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "pc/dtls_srtp_transport.h"
32#include "pc/dtls_transport.h"
33#include "pc/jsep_transport.h"
34#include "pc/rtp_transport.h"
35#include "pc/srtp_transport.h"
36#include "rtc_base/async_invoker.h"
37#include "rtc_base/constructor_magic.h"
38#include "rtc_base/ref_counted_object.h"
Artem Titove41c4332018-07-25 15:04:28 +020039#include "rtc_base/third_party/sigslot/sigslot.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080040
41namespace rtc {
42class Thread;
43class PacketTransportInternal;
44} // namespace rtc
45
46namespace webrtc {
47
Steve Antond25828a2018-08-31 13:06:05 -070048class JsepTransportController : public sigslot::has_slots<> {
Zhi Huange818b6e2018-02-22 15:26:27 -080049 public:
Qingsi Wang437077d2019-09-10 17:52:26 +000050 // State of negotiation for a transport.
51 enum class NegotiationState {
52 // Transport is in its initial state, not negotiated at all.
53 kInitial = 0,
54
55 // Transport is negotiated, but not finalized.
56 kProvisional = 1,
57
58 // Negotiation has completed for this transport.
59 kFinal = 2,
60 };
61
Zhi Huang365381f2018-04-13 16:44:34 -070062 // Used when the RtpTransport/DtlsTransport of the m= section is changed
63 // because the section is rejected or BUNDLE is enabled.
64 class Observer {
65 public:
66 virtual ~Observer() {}
67
68 // Returns true if media associated with |mid| was successfully set up to be
69 // demultiplexed on |rtp_transport|. Could return false if two bundled m=
70 // sections use the same SSRC, for example.
Bjorn A Mellemb689af42019-08-21 10:44:59 -070071 //
72 // If a data channel transport must be negotiated, |data_channel_transport|
73 // and |negotiation_state| indicate negotiation status. If
74 // |data_channel_transport| is null, the data channel transport should not
75 // be used. Otherwise, the value is a pointer to the transport to be used
76 // for data channels on |mid|, if any.
77 //
78 // The observer should not send data on |data_channel_transport| until
79 // |negotiation_state| is provisional or final. It should not delete
80 // |data_channel_transport| or any fallback transport until
81 // |negotiation_state| is final.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070082 virtual bool OnTransportChanged(
Zhi Huang365381f2018-04-13 16:44:34 -070083 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070084 RtpTransportInternal* rtp_transport,
Harald Alvestrandc85328f2019-02-28 07:51:00 +010085 rtc::scoped_refptr<DtlsTransport> dtls_transport,
Bjorn A Mellemb689af42019-08-21 10:44:59 -070086 MediaTransportInterface* media_transport,
Qingsi Wang437077d2019-09-10 17:52:26 +000087 DataChannelTransportInterface* data_channel_transport,
88 NegotiationState negotiation_state) = 0;
Zhi Huang365381f2018-04-13 16:44:34 -070089 };
90
Zhi Huange818b6e2018-02-22 15:26:27 -080091 struct Config {
92 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
93 // upon setting a local transport description that indicates an ICE
94 // restart.
95 bool redetermine_role_on_ice_restart = true;
96 rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
97 // |crypto_options| is used to determine if created DTLS transports
98 // negotiate GCM crypto suites or not.
Benjamin Wrighta54daf12018-10-11 15:33:17 -070099 webrtc::CryptoOptions crypto_options;
Zhi Huange818b6e2018-02-22 15:26:27 -0800100 PeerConnectionInterface::BundlePolicy bundle_policy =
101 PeerConnectionInterface::kBundlePolicyBalanced;
102 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
103 PeerConnectionInterface::kRtcpMuxPolicyRequire;
104 bool disable_encryption = false;
105 bool enable_external_auth = false;
106 // Used to inject the ICE/DTLS transports created externally.
107 cricket::TransportFactoryInterface* external_transport_factory = nullptr;
Zhi Huang365381f2018-04-13 16:44:34 -0700108 Observer* transport_observer = nullptr;
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +0200109 // Must be provided and valid for the lifetime of the
110 // JsepTransportController instance.
111 std::function<void(const rtc::CopyOnWriteBuffer& packet,
112 int64_t packet_time_us)>
113 rtcp_handler;
Zhi Huangb57e1692018-06-12 11:41:11 -0700114 bool active_reset_srtp_params = false;
Qingsi Wang7685e862018-06-11 20:15:46 -0700115 RtcEventLog* event_log = nullptr;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700116
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800117 // Whether media transport is used for media.
118 bool use_media_transport_for_media = false;
119
120 // Whether media transport is used for data channels.
121 bool use_media_transport_for_data_channels = false;
122
Niels Möllerabea6e52019-03-08 14:49:32 +0100123 // Whether an RtpMediaTransport should be created as default, when no
124 // MediaTransportFactory is provided.
125 bool use_rtp_media_transport = false;
126
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700127 // Use encrypted datagram transport to send packets.
128 bool use_datagram_transport = false;
129
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700130 // Use datagram transport's implementation of data channels instead of SCTP.
131 bool use_datagram_transport_for_data_channels = false;
132
Anton Sukhanov7940da02018-10-10 10:34:49 -0700133 // Optional media transport factory (experimental). If provided it will be
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800134 // used to create media_transport (as long as either
135 // |use_media_transport_for_media| or
136 // |use_media_transport_for_data_channels| is set to true). However, whether
137 // it will be used to send / receive audio and video frames instead of RTP
138 // is determined by |use_media_transport_for_media|. Note that currently
139 // media_transport co-exists with RTP / RTCP transports and may use the same
Anton Sukhanov7940da02018-10-10 10:34:49 -0700140 // underlying ICE transport.
141 MediaTransportFactory* media_transport_factory = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800142 };
143
144 // The ICE related events are signaled on the |signaling_thread|.
145 // All the transport related methods are called on the |network_thread|.
146 JsepTransportController(rtc::Thread* signaling_thread,
147 rtc::Thread* network_thread,
148 cricket::PortAllocator* port_allocator,
Zach Steine20867f2018-08-02 13:20:15 -0700149 AsyncResolverFactory* async_resolver_factory,
Zhi Huange818b6e2018-02-22 15:26:27 -0800150 Config config);
151 virtual ~JsepTransportController();
152
153 // The main method to be called; applies a description at the transport
154 // level, creating/destroying transport objects as needed and updating their
155 // properties. This includes RTP, DTLS, and ICE (but not SCTP). At least not
156 // yet? May make sense to in the future.
157 RTCError SetLocalDescription(SdpType type,
158 const cricket::SessionDescription* description);
159
160 RTCError SetRemoteDescription(SdpType type,
161 const cricket::SessionDescription* description);
162
163 // Get transports to be used for the provided |mid|. If bundling is enabled,
164 // calling GetRtpTransport for multiple MIDs may yield the same object.
165 RtpTransportInternal* GetRtpTransport(const std::string& mid) const;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100166 cricket::DtlsTransportInternal* GetDtlsTransport(const std::string& mid);
167 const cricket::DtlsTransportInternal* GetRtcpDtlsTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -0800168 const std::string& mid) const;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100169 // Gets the externally sharable version of the DtlsTransport.
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100170 rtc::scoped_refptr<webrtc::DtlsTransport> LookupDtlsTransportByMid(
Harald Alvestrandad88c882018-11-28 16:47:46 +0100171 const std::string& mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800172
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700173 MediaTransportConfig GetMediaTransportConfig(const std::string& mid) const;
174
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700175 DataChannelTransportInterface* GetDataChannelTransport(
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700176 const std::string& mid) const;
177
178 // TODO(sukhanov): Deprecate, return only config.
179 MediaTransportInterface* GetMediaTransport(const std::string& mid) const {
180 return GetMediaTransportConfig(mid).media_transport;
181 }
182
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800183 MediaTransportState GetMediaTransportState(const std::string& mid) const;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700184
Zhi Huange818b6e2018-02-22 15:26:27 -0800185 /*********************
186 * ICE-related methods
187 ********************/
188 // This method is public to allow PeerConnection to update it from
189 // SetConfiguration.
190 void SetIceConfig(const cricket::IceConfig& config);
191 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
192 // set, offers should generate new ufrags/passwords until an ICE restart
193 // occurs.
194 void SetNeedsIceRestartFlag();
195 // Returns true if the ICE restart flag above was set, and no ICE restart has
196 // occurred yet for this transport (by applying a local description with
197 // changed ufrag/password). If the transport has been deleted as a result of
198 // bundling, returns false.
199 bool NeedsIceRestart(const std::string& mid) const;
200 // Start gathering candidates for any new transports, or transports doing an
201 // ICE restart.
202 void MaybeStartGathering();
203 RTCError AddRemoteCandidates(
204 const std::string& mid,
205 const std::vector<cricket::Candidate>& candidates);
206 RTCError RemoveRemoteCandidates(
207 const std::vector<cricket::Candidate>& candidates);
208
209 /**********************
210 * DTLS-related methods
211 *********************/
212 // Specifies the identity to use in this session.
213 // Can only be called once.
214 bool SetLocalCertificate(
215 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
216 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
217 const std::string& mid) const;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800218 // Caller owns returned certificate chain. This method mainly exists for
219 // stats reporting.
220 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800221 const std::string& mid) const;
222 // Get negotiated role, if one has been negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200223 absl::optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800224
225 // TODO(deadbeef): GetStats isn't const because all the way down to
226 // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
227 // const. Fix this.
228 bool GetStats(const std::string& mid, cricket::TransportStats* stats);
Zhi Huange818b6e2018-02-22 15:26:27 -0800229
Zhi Huange830e682018-03-30 10:48:35 -0700230 bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; }
Zhi Huang365381f2018-04-13 16:44:34 -0700231
Zhi Huangb57e1692018-06-12 11:41:11 -0700232 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
233
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700234 // Allows to overwrite the settings from config. You may set or reset the
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800235 // media transport configuration on the jsep transport controller, as long as
236 // you did not call 'GetMediaTransport' or 'MaybeCreateJsepTransport'. Once
237 // Jsep transport is created, you can't change this setting.
238 void SetMediaTransportSettings(bool use_media_transport_for_media,
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700239 bool use_media_transport_for_data_channels,
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700240 bool use_datagram_transport,
241 bool use_datagram_transport_for_data_channels);
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700242
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800243 // If media transport is present enabled and supported,
244 // when this method is called, it creates a media transport and generates its
245 // offer. The new offer is then returned, and the created media transport will
246 // subsequently be used.
247 absl::optional<cricket::SessionDescription::MediaTransportSetting>
248 GenerateOrGetLastMediaTransportOffer();
249
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -0700250 // Gets the transport parameters for the transport identified by |mid|.
251 // If |mid| is bundled, returns the parameters for the bundled transport.
252 // If the transport for |mid| has not been created yet, it may be allocated in
253 // order to generate transport parameters.
254 absl::optional<cricket::OpaqueTransportParameters> GetTransportParameters(
255 const std::string& mid);
256
Zhi Huange818b6e2018-02-22 15:26:27 -0800257 // All of these signals are fired on the signaling thread.
258
259 // If any transport failed => failed,
260 // Else if all completed => completed,
261 // Else if all connected => connected,
262 // Else => connecting
Alex Loiko9289eda2018-11-23 16:18:59 +0000263 sigslot::signal1<cricket::IceConnectionState> SignalIceConnectionState;
Zhi Huange818b6e2018-02-22 15:26:27 -0800264
Jonas Olsson635474e2018-10-18 15:58:17 +0200265 sigslot::signal1<PeerConnectionInterface::PeerConnectionState>
266 SignalConnectionState;
Alex Loiko9289eda2018-11-23 16:18:59 +0000267 sigslot::signal1<PeerConnectionInterface::IceConnectionState>
268 SignalStandardizedIceConnectionState;
Jonas Olsson635474e2018-10-18 15:58:17 +0200269
Zhi Huange818b6e2018-02-22 15:26:27 -0800270 // If all transports done gathering => complete,
271 // Else if any are gathering => gathering,
272 // Else => new
273 sigslot::signal1<cricket::IceGatheringState> SignalIceGatheringState;
274
275 // (mid, candidates)
276 sigslot::signal2<const std::string&, const std::vector<cricket::Candidate>&>
277 SignalIceCandidatesGathered;
278
Eldar Relloda13ea22019-06-01 12:23:43 +0300279 sigslot::signal1<const cricket::IceCandidateErrorEvent&>
280 SignalIceCandidateError;
281
Zhi Huange818b6e2018-02-22 15:26:27 -0800282 sigslot::signal1<const std::vector<cricket::Candidate>&>
283 SignalIceCandidatesRemoved;
284
Alex Drake00c7ecf2019-08-06 10:54:47 -0700285 sigslot::signal1<const cricket::CandidatePairChangeEvent&>
286 SignalIceCandidatePairChanged;
287
Zhi Huange818b6e2018-02-22 15:26:27 -0800288 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
289
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700290 // TODO(mellem): Delete this signal once PeerConnection no longer
291 // uses it to determine data channel state.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800292 sigslot::signal<> SignalMediaTransportStateChanged;
293
Zhi Huange818b6e2018-02-22 15:26:27 -0800294 private:
Zhi Huange818b6e2018-02-22 15:26:27 -0800295 RTCError ApplyDescription_n(bool local,
296 SdpType type,
297 const cricket::SessionDescription* description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700298 RTCError ValidateAndMaybeUpdateBundleGroup(
299 bool local,
300 SdpType type,
301 const cricket::SessionDescription* description);
Zhi Huange830e682018-03-30 10:48:35 -0700302 RTCError ValidateContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800303
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700304 void HandleRejectedContent(const cricket::ContentInfo& content_info,
Zhi Huangd2248f82018-04-10 14:41:03 -0700305 const cricket::SessionDescription* description);
Zhi Huang365381f2018-04-13 16:44:34 -0700306 bool HandleBundledContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800307
Zhi Huang365381f2018-04-13 16:44:34 -0700308 bool SetTransportForMid(const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700309 cricket::JsepTransport* jsep_transport);
310 void RemoveTransportForMid(const std::string& mid);
Zhi Huangd2248f82018-04-10 14:41:03 -0700311
Zhi Huange818b6e2018-02-22 15:26:27 -0800312 cricket::JsepTransportDescription CreateJsepTransportDescription(
Harald Alvestrand1716d392019-06-03 20:35:45 +0200313 const cricket::ContentInfo& content_info,
314 const cricket::TransportInfo& transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700315 const std::vector<int>& encrypted_extension_ids,
316 int rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800317
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200318 absl::optional<std::string> bundled_mid() const {
319 absl::optional<std::string> bundled_mid;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700320 if (bundle_group_ && bundle_group_->FirstContentName()) {
321 bundled_mid = *(bundle_group_->FirstContentName());
Zhi Huange818b6e2018-02-22 15:26:27 -0800322 }
323 return bundled_mid;
324 }
325
326 bool IsBundled(const std::string& mid) const {
327 return bundle_group_ && bundle_group_->HasContentName(mid);
328 }
329
330 bool ShouldUpdateBundleGroup(SdpType type,
331 const cricket::SessionDescription* description);
332
333 std::vector<int> MergeEncryptedHeaderExtensionIdsForBundle(
334 const cricket::SessionDescription* description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800335 std::vector<int> GetEncryptedHeaderExtensionIds(
336 const cricket::ContentInfo& content_info);
337
Zhi Huange830e682018-03-30 10:48:35 -0700338 int GetRtpAbsSendTimeHeaderExtensionId(
339 const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800340
Zhi Huange830e682018-03-30 10:48:35 -0700341 // This method takes the BUNDLE group into account. If the JsepTransport is
342 // destroyed because of BUNDLE, it would return the transport which other
343 // transports are bundled on (In current implementation, it is the first
344 // content in the BUNDLE group).
Zhi Huang365381f2018-04-13 16:44:34 -0700345 const cricket::JsepTransport* GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700346 const std::string& mid) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700347 cricket::JsepTransport* GetJsepTransportForMid(const std::string& mid);
Zhi Huange830e682018-03-30 10:48:35 -0700348
349 // Get the JsepTransport without considering the BUNDLE group. Return nullptr
350 // if the JsepTransport is destroyed.
Zhi Huang365381f2018-04-13 16:44:34 -0700351 const cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700352 const std::string& transport_name) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700353 cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700354 const std::string& transport_name);
355
Anton Sukhanov7940da02018-10-10 10:34:49 -0700356 // Creates jsep transport. Noop if transport is already created.
357 // Transport is created either during SetLocalDescription (|local| == true) or
358 // during SetRemoteDescription (|local| == false). Passing |local| helps to
359 // differentiate initiator (caller) from answerer (callee).
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -0800360 RTCError MaybeCreateJsepTransport(
361 bool local,
362 const cricket::ContentInfo& content_info,
363 const cricket::SessionDescription& description);
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800364
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800365 // Creates media transport if config wants to use it, and a=x-mt line is
366 // present for the current media transport. Returned MediaTransportInterface
367 // is not connected, and must be connected to ICE. You must call
368 // |GenerateOrGetLastMediaTransportOffer| on the caller before calling
369 // MaybeCreateMediaTransport.
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800370 std::unique_ptr<webrtc::MediaTransportInterface> MaybeCreateMediaTransport(
371 const cricket::ContentInfo& content_info,
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -0800372 const cricket::SessionDescription& description,
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800373 bool local);
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700374
375 // Creates datagram transport if config wants to use it, and a=x-mt line is
376 // present for the current media transport. Returned
377 // DatagramTransportInterface is not connected, and must be connected to ICE.
378 // You must call |GenerateOrGetLastMediaTransportOffer| on the caller before
379 // calling MaybeCreateDatagramTransport.
380 std::unique_ptr<webrtc::DatagramTransportInterface>
381 MaybeCreateDatagramTransport(const cricket::ContentInfo& content_info,
382 const cricket::SessionDescription& description,
383 bool local);
384
Zhi Huange818b6e2018-02-22 15:26:27 -0800385 void MaybeDestroyJsepTransport(const std::string& mid);
386 void DestroyAllJsepTransports_n();
387
388 void SetIceRole_n(cricket::IceRole ice_role);
389
390 cricket::IceRole DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -0700391 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -0800392 const cricket::TransportInfo& transport_info,
393 SdpType type,
394 bool local);
395
396 std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
Anton Sukhanovac6c0962019-07-10 15:44:56 -0700397 const cricket::ContentInfo& content_info,
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700398 cricket::IceTransportInternal* ice,
Anton Sukhanov292ce4e2019-06-03 13:00:24 -0700399 DatagramTransportInterface* datagram_transport);
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800400 std::unique_ptr<cricket::IceTransportInternal> CreateIceTransport(
401 const std::string transport_name,
Zhi Huange818b6e2018-02-22 15:26:27 -0800402 bool rtcp);
403
404 std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
405 const std::string& transport_name,
406 rtc::PacketTransportInternal* rtp_packet_transport,
407 rtc::PacketTransportInternal* rtcp_packet_transport);
408 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
409 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700410 cricket::DtlsTransportInternal* rtp_dtls_transport,
411 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800412 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
413 const std::string& transport_name,
414 cricket::DtlsTransportInternal* rtp_dtls_transport,
415 cricket::DtlsTransportInternal* rtcp_dtls_transport);
416
417 // Collect all the DtlsTransports, including RTP and RTCP, from the
418 // JsepTransports. JsepTransportController can iterate all the DtlsTransports
419 // and update the aggregate states.
420 std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
421
422 // Handlers for signals from Transport.
423 void OnTransportWritableState_n(rtc::PacketTransportInternal* transport);
424 void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport);
425 void OnTransportGatheringState_n(cricket::IceTransportInternal* transport);
426 void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
427 const cricket::Candidate& candidate);
Eldar Relloda13ea22019-06-01 12:23:43 +0300428 void OnTransportCandidateError_n(
429 cricket::IceTransportInternal* transport,
430 const cricket::IceCandidateErrorEvent& event);
Zhi Huange818b6e2018-02-22 15:26:27 -0800431 void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
432 const cricket::Candidates& candidates);
433 void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport);
434 void OnTransportStateChanged_n(cricket::IceTransportInternal* transport);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800435 void OnMediaTransportStateChanged_n();
Alex Drake00c7ecf2019-08-06 10:54:47 -0700436 void OnTransportCandidatePairChanged_n(
437 const cricket::CandidatePairChangeEvent& event);
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700438 void OnDataChannelTransportNegotiated_n(
439 cricket::JsepTransport* transport,
Qingsi Wang437077d2019-09-10 17:52:26 +0000440 DataChannelTransportInterface* data_channel_transport,
441 bool provisional);
Zhi Huange818b6e2018-02-22 15:26:27 -0800442
443 void UpdateAggregateStates_n();
444
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +0200445 void OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer* packet,
446 int64_t packet_time_us);
447
Zhi Huange818b6e2018-02-22 15:26:27 -0800448 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
449
450 rtc::Thread* const signaling_thread_ = nullptr;
451 rtc::Thread* const network_thread_ = nullptr;
452 cricket::PortAllocator* const port_allocator_ = nullptr;
Zach Steine20867f2018-08-02 13:20:15 -0700453 AsyncResolverFactory* const async_resolver_factory_ = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800454
Zhi Huang365381f2018-04-13 16:44:34 -0700455 std::map<std::string, std::unique_ptr<cricket::JsepTransport>>
Zhi Huange830e682018-03-30 10:48:35 -0700456 jsep_transports_by_name_;
Zhi Huangd2248f82018-04-10 14:41:03 -0700457 // This keeps track of the mapping between media section
Zhi Huang365381f2018-04-13 16:44:34 -0700458 // (BaseChannel/SctpTransport) and the JsepTransport underneath.
459 std::map<std::string, cricket::JsepTransport*> mid_to_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800460
Jonas Olsson635474e2018-10-18 15:58:17 +0200461 // Aggregate states for Transports.
Alex Loiko9289eda2018-11-23 16:18:59 +0000462 // standardized_ice_connection_state_ is intended to replace
463 // ice_connection_state, see bugs.webrtc.org/9308
464 cricket::IceConnectionState ice_connection_state_ =
465 cricket::kIceConnectionConnecting;
466 PeerConnectionInterface::IceConnectionState
467 standardized_ice_connection_state_ =
468 PeerConnectionInterface::kIceConnectionNew;
Jonas Olsson635474e2018-10-18 15:58:17 +0200469 PeerConnectionInterface::PeerConnectionState combined_connection_state_ =
470 PeerConnectionInterface::PeerConnectionState::kNew;
Zhi Huange818b6e2018-02-22 15:26:27 -0800471 cricket::IceGatheringState ice_gathering_state_ = cricket::kIceGatheringNew;
472
473 Config config_;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800474
475 // Early on in the call we don't know if media transport is going to be used,
476 // but we need to get the server-supported parameters to add to an SDP.
477 // This server media transport will be promoted to the used media transport
478 // after the local description is set, and the ownership will be transferred
479 // to the actual JsepTransport.
480 // This "offer" media transport is not created if it's done on the party that
481 // provides answer. This offer media transport is only created once at the
482 // beginning of the connection, and never again.
483 std::unique_ptr<MediaTransportInterface> offer_media_transport_ = nullptr;
484
485 // Contains the offer of the |offer_media_transport_|, in case if it needs to
486 // be repeated.
487 absl::optional<cricket::SessionDescription::MediaTransportSetting>
488 media_transport_offer_settings_;
489
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700490 // Early on in the call we don't know if datagram transport is going to be
491 // used, but we need to get the server-supported parameters to add to an SDP.
492 // This server datagram transport will be promoted to the used datagram
493 // transport after the local description is set, and the ownership will be
494 // transferred to the actual JsepTransport. This "offer" datagram transport is
495 // not created if it's done on the party that provides answer. This offer
496 // datagram transport is only created once at the beginning of the connection,
497 // and never again.
498 std::unique_ptr<DatagramTransportInterface> offer_datagram_transport_ =
499 nullptr;
500
501 // Contains the offer of the |offer_datagram_transport_|, in case if it needs
502 // to be repeated.
503 absl::optional<cricket::SessionDescription::MediaTransportSetting>
504 datagram_transport_offer_settings_;
505
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800506 // When the new offer is regenerated (due to upgrade), we don't want to
507 // re-create media transport. New streams might be created; but media
508 // transport stays the same. This flag prevents re-creation of the transport
509 // on the offerer.
510 // The first media transport is created in jsep transport controller as the
511 // |offer_media_transport_|, and then the ownership is moved to the
512 // appropriate JsepTransport, at which point |offer_media_transport_| is
513 // zeroed out. On the callee (answerer), the first media transport is not even
514 // assigned to |offer_media_transport_|. Both offerer and answerer can
515 // recreate the Offer (e.g. after adding streams in Plan B), and so we want to
516 // prevent recreation of the media transport when that happens.
517 bool media_transport_created_once_ = false;
518
Zhi Huange818b6e2018-02-22 15:26:27 -0800519 const cricket::SessionDescription* local_desc_ = nullptr;
520 const cricket::SessionDescription* remote_desc_ = nullptr;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200521 absl::optional<bool> initial_offerer_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800522
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200523 absl::optional<cricket::ContentGroup> bundle_group_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800524
525 cricket::IceConfig ice_config_;
526 cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
527 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
528 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
529 rtc::AsyncInvoker invoker_;
530
Zhi Huange818b6e2018-02-22 15:26:27 -0800531 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransportController);
532};
533
534} // namespace webrtc
535
Steve Anton10542f22019-01-11 09:11:00 -0800536#endif // PC_JSEP_TRANSPORT_CONTROLLER_H_