blob: bcaeed539fea5a96369ebaf317e1c3d23755a499 [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:
Zhi Huang365381f2018-04-13 16:44:34 -070050 // Used when the RtpTransport/DtlsTransport of the m= section is changed
51 // because the section is rejected or BUNDLE is enabled.
52 class Observer {
53 public:
54 virtual ~Observer() {}
55
56 // Returns true if media associated with |mid| was successfully set up to be
57 // demultiplexed on |rtp_transport|. Could return false if two bundled m=
58 // sections use the same SSRC, for example.
Bjorn A Mellemb689af42019-08-21 10:44:59 -070059 //
60 // If a data channel transport must be negotiated, |data_channel_transport|
61 // and |negotiation_state| indicate negotiation status. If
62 // |data_channel_transport| is null, the data channel transport should not
63 // be used. Otherwise, the value is a pointer to the transport to be used
64 // for data channels on |mid|, if any.
65 //
66 // The observer should not send data on |data_channel_transport| until
67 // |negotiation_state| is provisional or final. It should not delete
68 // |data_channel_transport| or any fallback transport until
69 // |negotiation_state| is final.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070070 virtual bool OnTransportChanged(
Zhi Huang365381f2018-04-13 16:44:34 -070071 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070072 RtpTransportInternal* rtp_transport,
Harald Alvestrandc85328f2019-02-28 07:51:00 +010073 rtc::scoped_refptr<DtlsTransport> dtls_transport,
Bjorn A Mellemb689af42019-08-21 10:44:59 -070074 MediaTransportInterface* media_transport,
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -070075 DataChannelTransportInterface* data_channel_transport) = 0;
Zhi Huang365381f2018-04-13 16:44:34 -070076 };
77
Zhi Huange818b6e2018-02-22 15:26:27 -080078 struct Config {
79 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
80 // upon setting a local transport description that indicates an ICE
81 // restart.
82 bool redetermine_role_on_ice_restart = true;
83 rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
84 // |crypto_options| is used to determine if created DTLS transports
85 // negotiate GCM crypto suites or not.
Benjamin Wrighta54daf12018-10-11 15:33:17 -070086 webrtc::CryptoOptions crypto_options;
Zhi Huange818b6e2018-02-22 15:26:27 -080087 PeerConnectionInterface::BundlePolicy bundle_policy =
88 PeerConnectionInterface::kBundlePolicyBalanced;
89 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
90 PeerConnectionInterface::kRtcpMuxPolicyRequire;
91 bool disable_encryption = false;
92 bool enable_external_auth = false;
93 // Used to inject the ICE/DTLS transports created externally.
94 cricket::TransportFactoryInterface* external_transport_factory = nullptr;
Zhi Huang365381f2018-04-13 16:44:34 -070095 Observer* transport_observer = nullptr;
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +020096 // Must be provided and valid for the lifetime of the
97 // JsepTransportController instance.
98 std::function<void(const rtc::CopyOnWriteBuffer& packet,
99 int64_t packet_time_us)>
100 rtcp_handler;
Zhi Huangb57e1692018-06-12 11:41:11 -0700101 bool active_reset_srtp_params = false;
Qingsi Wang7685e862018-06-11 20:15:46 -0700102 RtcEventLog* event_log = nullptr;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700103
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700104 // Factory for SCTP transports.
105 cricket::SctpTransportInternalFactory* sctp_factory = nullptr;
106
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800107 // Whether media transport is used for media.
108 bool use_media_transport_for_media = false;
109
110 // Whether media transport is used for data channels.
111 bool use_media_transport_for_data_channels = false;
112
Niels Möllerabea6e52019-03-08 14:49:32 +0100113 // Whether an RtpMediaTransport should be created as default, when no
114 // MediaTransportFactory is provided.
115 bool use_rtp_media_transport = false;
116
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700117 // Use encrypted datagram transport to send packets.
118 bool use_datagram_transport = false;
119
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700120 // Use datagram transport's implementation of data channels instead of SCTP.
121 bool use_datagram_transport_for_data_channels = false;
122
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700123 // Whether |use_datagram_transport_for_data_channels| applies to outgoing
124 // calls. If true, |use_datagram_transport_for_data_channels| applies only
125 // to incoming calls.
126 bool use_datagram_transport_for_data_channels_receive_only = false;
127
Anton Sukhanov7940da02018-10-10 10:34:49 -0700128 // Optional media transport factory (experimental). If provided it will be
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800129 // used to create media_transport (as long as either
130 // |use_media_transport_for_media| or
131 // |use_media_transport_for_data_channels| is set to true). However, whether
132 // it will be used to send / receive audio and video frames instead of RTP
133 // is determined by |use_media_transport_for_media|. Note that currently
134 // media_transport co-exists with RTP / RTCP transports and may use the same
Anton Sukhanov7940da02018-10-10 10:34:49 -0700135 // underlying ICE transport.
136 MediaTransportFactory* media_transport_factory = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800137 };
138
139 // The ICE related events are signaled on the |signaling_thread|.
140 // All the transport related methods are called on the |network_thread|.
141 JsepTransportController(rtc::Thread* signaling_thread,
142 rtc::Thread* network_thread,
143 cricket::PortAllocator* port_allocator,
Zach Steine20867f2018-08-02 13:20:15 -0700144 AsyncResolverFactory* async_resolver_factory,
Zhi Huange818b6e2018-02-22 15:26:27 -0800145 Config config);
146 virtual ~JsepTransportController();
147
148 // The main method to be called; applies a description at the transport
149 // level, creating/destroying transport objects as needed and updating their
150 // properties. This includes RTP, DTLS, and ICE (but not SCTP). At least not
151 // yet? May make sense to in the future.
152 RTCError SetLocalDescription(SdpType type,
153 const cricket::SessionDescription* description);
154
155 RTCError SetRemoteDescription(SdpType type,
156 const cricket::SessionDescription* description);
157
158 // Get transports to be used for the provided |mid|. If bundling is enabled,
159 // calling GetRtpTransport for multiple MIDs may yield the same object.
160 RtpTransportInternal* GetRtpTransport(const std::string& mid) const;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100161 cricket::DtlsTransportInternal* GetDtlsTransport(const std::string& mid);
162 const cricket::DtlsTransportInternal* GetRtcpDtlsTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -0800163 const std::string& mid) const;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100164 // Gets the externally sharable version of the DtlsTransport.
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100165 rtc::scoped_refptr<webrtc::DtlsTransport> LookupDtlsTransportByMid(
Harald Alvestrandad88c882018-11-28 16:47:46 +0100166 const std::string& mid);
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700167 rtc::scoped_refptr<SctpTransport> GetSctpTransport(
168 const std::string& mid) const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800169
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700170 MediaTransportConfig GetMediaTransportConfig(const std::string& mid) const;
171
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
175 // TODO(sukhanov): Deprecate, return only config.
176 MediaTransportInterface* GetMediaTransport(const std::string& mid) const {
177 return GetMediaTransportConfig(mid).media_transport;
178 }
179
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800180 MediaTransportState GetMediaTransportState(const std::string& mid) const;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700181
Zhi Huange818b6e2018-02-22 15:26:27 -0800182 /*********************
183 * ICE-related methods
184 ********************/
185 // This method is public to allow PeerConnection to update it from
186 // SetConfiguration.
187 void SetIceConfig(const cricket::IceConfig& config);
188 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
189 // set, offers should generate new ufrags/passwords until an ICE restart
190 // occurs.
191 void SetNeedsIceRestartFlag();
192 // Returns true if the ICE restart flag above was set, and no ICE restart has
193 // occurred yet for this transport (by applying a local description with
194 // changed ufrag/password). If the transport has been deleted as a result of
195 // bundling, returns false.
196 bool NeedsIceRestart(const std::string& mid) const;
197 // Start gathering candidates for any new transports, or transports doing an
198 // ICE restart.
199 void MaybeStartGathering();
200 RTCError AddRemoteCandidates(
201 const std::string& mid,
202 const std::vector<cricket::Candidate>& candidates);
203 RTCError RemoveRemoteCandidates(
204 const std::vector<cricket::Candidate>& candidates);
205
206 /**********************
207 * DTLS-related methods
208 *********************/
209 // Specifies the identity to use in this session.
210 // Can only be called once.
211 bool SetLocalCertificate(
212 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
213 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
214 const std::string& mid) const;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800215 // Caller owns returned certificate chain. This method mainly exists for
216 // stats reporting.
217 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800218 const std::string& mid) const;
219 // Get negotiated role, if one has been negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200220 absl::optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800221
222 // TODO(deadbeef): GetStats isn't const because all the way down to
223 // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
224 // const. Fix this.
225 bool GetStats(const std::string& mid, cricket::TransportStats* stats);
Zhi Huange818b6e2018-02-22 15:26:27 -0800226
Zhi Huange830e682018-03-30 10:48:35 -0700227 bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; }
Zhi Huang365381f2018-04-13 16:44:34 -0700228
Zhi Huangb57e1692018-06-12 11:41:11 -0700229 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
230
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700231 // Allows to overwrite the settings from config. You may set or reset the
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800232 // media transport configuration on the jsep transport controller, as long as
233 // you did not call 'GetMediaTransport' or 'MaybeCreateJsepTransport'. Once
234 // Jsep transport is created, you can't change this setting.
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700235 void SetMediaTransportSettings(
236 bool use_media_transport_for_media,
237 bool use_media_transport_for_data_channels,
238 bool use_datagram_transport,
239 bool use_datagram_transport_for_data_channels,
240 bool use_datagram_transport_for_data_channels_receive_only);
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700241
Eldar Rello5ab79e62019-10-09 18:29:44 +0300242 // TODO(elrello): For now the rollback only removes mid to transport mapping
243 // and deletes unused transport, but doesn't consider anything more complex.
244 void RollbackTransportForMid(const std::string& mid);
245
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800246 // If media transport is present enabled and supported,
247 // when this method is called, it creates a media transport and generates its
248 // offer. The new offer is then returned, and the created media transport will
249 // subsequently be used.
250 absl::optional<cricket::SessionDescription::MediaTransportSetting>
251 GenerateOrGetLastMediaTransportOffer();
252
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -0700253 // Gets the transport parameters for the transport identified by |mid|.
254 // If |mid| is bundled, returns the parameters for the bundled transport.
255 // If the transport for |mid| has not been created yet, it may be allocated in
256 // order to generate transport parameters.
257 absl::optional<cricket::OpaqueTransportParameters> GetTransportParameters(
258 const std::string& mid);
259
Zhi Huange818b6e2018-02-22 15:26:27 -0800260 // All of these signals are fired on the signaling thread.
261
262 // If any transport failed => failed,
263 // Else if all completed => completed,
264 // Else if all connected => connected,
265 // Else => connecting
Alex Loiko9289eda2018-11-23 16:18:59 +0000266 sigslot::signal1<cricket::IceConnectionState> SignalIceConnectionState;
Zhi Huange818b6e2018-02-22 15:26:27 -0800267
Jonas Olsson635474e2018-10-18 15:58:17 +0200268 sigslot::signal1<PeerConnectionInterface::PeerConnectionState>
269 SignalConnectionState;
Alex Loiko9289eda2018-11-23 16:18:59 +0000270 sigslot::signal1<PeerConnectionInterface::IceConnectionState>
271 SignalStandardizedIceConnectionState;
Jonas Olsson635474e2018-10-18 15:58:17 +0200272
Zhi Huange818b6e2018-02-22 15:26:27 -0800273 // If all transports done gathering => complete,
274 // Else if any are gathering => gathering,
275 // Else => new
276 sigslot::signal1<cricket::IceGatheringState> SignalIceGatheringState;
277
278 // (mid, candidates)
279 sigslot::signal2<const std::string&, const std::vector<cricket::Candidate>&>
280 SignalIceCandidatesGathered;
281
Eldar Relloda13ea22019-06-01 12:23:43 +0300282 sigslot::signal1<const cricket::IceCandidateErrorEvent&>
283 SignalIceCandidateError;
284
Zhi Huange818b6e2018-02-22 15:26:27 -0800285 sigslot::signal1<const std::vector<cricket::Candidate>&>
286 SignalIceCandidatesRemoved;
287
Alex Drake00c7ecf2019-08-06 10:54:47 -0700288 sigslot::signal1<const cricket::CandidatePairChangeEvent&>
289 SignalIceCandidatePairChanged;
290
Zhi Huange818b6e2018-02-22 15:26:27 -0800291 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
292
Zhi Huange818b6e2018-02-22 15:26:27 -0800293 private:
Zhi Huange818b6e2018-02-22 15:26:27 -0800294 RTCError ApplyDescription_n(bool local,
295 SdpType type,
296 const cricket::SessionDescription* description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700297 RTCError ValidateAndMaybeUpdateBundleGroup(
298 bool local,
299 SdpType type,
300 const cricket::SessionDescription* description);
Zhi Huange830e682018-03-30 10:48:35 -0700301 RTCError ValidateContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800302
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700303 void HandleRejectedContent(const cricket::ContentInfo& content_info,
Zhi Huangd2248f82018-04-10 14:41:03 -0700304 const cricket::SessionDescription* description);
Zhi Huang365381f2018-04-13 16:44:34 -0700305 bool HandleBundledContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800306
Zhi Huang365381f2018-04-13 16:44:34 -0700307 bool SetTransportForMid(const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700308 cricket::JsepTransport* jsep_transport);
309 void RemoveTransportForMid(const std::string& mid);
Zhi Huangd2248f82018-04-10 14:41:03 -0700310
Zhi Huange818b6e2018-02-22 15:26:27 -0800311 cricket::JsepTransportDescription CreateJsepTransportDescription(
Harald Alvestrand1716d392019-06-03 20:35:45 +0200312 const cricket::ContentInfo& content_info,
313 const cricket::TransportInfo& transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700314 const std::vector<int>& encrypted_extension_ids,
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -0700315 int rtp_abs_sendtime_extn_id,
316 absl::optional<std::string> media_alt_protocol,
317 absl::optional<std::string> data_alt_protocol);
Zhi Huange818b6e2018-02-22 15:26:27 -0800318
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200319 absl::optional<std::string> bundled_mid() const {
320 absl::optional<std::string> bundled_mid;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700321 if (bundle_group_ && bundle_group_->FirstContentName()) {
322 bundled_mid = *(bundle_group_->FirstContentName());
Zhi Huange818b6e2018-02-22 15:26:27 -0800323 }
324 return bundled_mid;
325 }
326
327 bool IsBundled(const std::string& mid) const {
328 return bundle_group_ && bundle_group_->HasContentName(mid);
329 }
330
331 bool ShouldUpdateBundleGroup(SdpType type,
332 const cricket::SessionDescription* description);
333
334 std::vector<int> MergeEncryptedHeaderExtensionIdsForBundle(
335 const cricket::SessionDescription* description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800336 std::vector<int> GetEncryptedHeaderExtensionIds(
337 const cricket::ContentInfo& content_info);
338
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -0700339 // Extracts the alt-protocol settings that apply to the bundle group.
340 RTCError GetAltProtocolsForBundle(
341 const cricket::SessionDescription* description,
342 absl::optional<std::string>* media_alt_protocol,
343 absl::optional<std::string>* data_alt_protocol);
344
Zhi Huange830e682018-03-30 10:48:35 -0700345 int GetRtpAbsSendTimeHeaderExtensionId(
346 const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800347
Zhi Huange830e682018-03-30 10:48:35 -0700348 // This method takes the BUNDLE group into account. If the JsepTransport is
349 // destroyed because of BUNDLE, it would return the transport which other
350 // transports are bundled on (In current implementation, it is the first
351 // content in the BUNDLE group).
Zhi Huang365381f2018-04-13 16:44:34 -0700352 const cricket::JsepTransport* GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700353 const std::string& mid) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700354 cricket::JsepTransport* GetJsepTransportForMid(const std::string& mid);
Zhi Huange830e682018-03-30 10:48:35 -0700355
356 // Get the JsepTransport without considering the BUNDLE group. Return nullptr
357 // if the JsepTransport is destroyed.
Zhi Huang365381f2018-04-13 16:44:34 -0700358 const cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700359 const std::string& transport_name) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700360 cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700361 const std::string& transport_name);
362
Anton Sukhanov7940da02018-10-10 10:34:49 -0700363 // Creates jsep transport. Noop if transport is already created.
364 // Transport is created either during SetLocalDescription (|local| == true) or
365 // during SetRemoteDescription (|local| == false). Passing |local| helps to
366 // differentiate initiator (caller) from answerer (callee).
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -0800367 RTCError MaybeCreateJsepTransport(
368 bool local,
369 const cricket::ContentInfo& content_info,
370 const cricket::SessionDescription& description);
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800371
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800372 // Creates media transport if config wants to use it, and a=x-mt line is
373 // present for the current media transport. Returned MediaTransportInterface
374 // is not connected, and must be connected to ICE. You must call
375 // |GenerateOrGetLastMediaTransportOffer| on the caller before calling
376 // MaybeCreateMediaTransport.
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800377 std::unique_ptr<webrtc::MediaTransportInterface> MaybeCreateMediaTransport(
378 const cricket::ContentInfo& content_info,
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -0800379 const cricket::SessionDescription& description,
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800380 bool local);
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700381
382 // Creates datagram transport if config wants to use it, and a=x-mt line is
383 // present for the current media transport. Returned
384 // DatagramTransportInterface is not connected, and must be connected to ICE.
385 // You must call |GenerateOrGetLastMediaTransportOffer| on the caller before
386 // calling MaybeCreateDatagramTransport.
387 std::unique_ptr<webrtc::DatagramTransportInterface>
388 MaybeCreateDatagramTransport(const cricket::ContentInfo& content_info,
389 const cricket::SessionDescription& description,
390 bool local);
391
Zhi Huange818b6e2018-02-22 15:26:27 -0800392 void MaybeDestroyJsepTransport(const std::string& mid);
393 void DestroyAllJsepTransports_n();
394
395 void SetIceRole_n(cricket::IceRole ice_role);
396
397 cricket::IceRole DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -0700398 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -0800399 const cricket::TransportInfo& transport_info,
400 SdpType type,
401 bool local);
402
403 std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
Anton Sukhanovac6c0962019-07-10 15:44:56 -0700404 const cricket::ContentInfo& content_info,
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700405 cricket::IceTransportInternal* ice,
Anton Sukhanov292ce4e2019-06-03 13:00:24 -0700406 DatagramTransportInterface* datagram_transport);
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800407 std::unique_ptr<cricket::IceTransportInternal> CreateIceTransport(
408 const std::string transport_name,
Zhi Huange818b6e2018-02-22 15:26:27 -0800409 bool rtcp);
410
411 std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
412 const std::string& transport_name,
413 rtc::PacketTransportInternal* rtp_packet_transport,
414 rtc::PacketTransportInternal* rtcp_packet_transport);
415 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
416 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700417 cricket::DtlsTransportInternal* rtp_dtls_transport,
418 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800419 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
420 const std::string& transport_name,
421 cricket::DtlsTransportInternal* rtp_dtls_transport,
422 cricket::DtlsTransportInternal* rtcp_dtls_transport);
423
424 // Collect all the DtlsTransports, including RTP and RTCP, from the
425 // JsepTransports. JsepTransportController can iterate all the DtlsTransports
426 // and update the aggregate states.
427 std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
428
429 // Handlers for signals from Transport.
430 void OnTransportWritableState_n(rtc::PacketTransportInternal* transport);
431 void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport);
432 void OnTransportGatheringState_n(cricket::IceTransportInternal* transport);
433 void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
434 const cricket::Candidate& candidate);
Eldar Relloda13ea22019-06-01 12:23:43 +0300435 void OnTransportCandidateError_n(
436 cricket::IceTransportInternal* transport,
437 const cricket::IceCandidateErrorEvent& event);
Zhi Huange818b6e2018-02-22 15:26:27 -0800438 void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
439 const cricket::Candidates& candidates);
440 void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport);
441 void OnTransportStateChanged_n(cricket::IceTransportInternal* transport);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800442 void OnMediaTransportStateChanged_n();
Alex Drake00c7ecf2019-08-06 10:54:47 -0700443 void OnTransportCandidatePairChanged_n(
444 const cricket::CandidatePairChangeEvent& event);
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700445 void OnDataChannelTransportNegotiated_n(
446 cricket::JsepTransport* transport,
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700447 DataChannelTransportInterface* data_channel_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800448
449 void UpdateAggregateStates_n();
450
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +0200451 void OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer* packet,
452 int64_t packet_time_us);
453
Zhi Huange818b6e2018-02-22 15:26:27 -0800454 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
455
456 rtc::Thread* const signaling_thread_ = nullptr;
457 rtc::Thread* const network_thread_ = nullptr;
458 cricket::PortAllocator* const port_allocator_ = nullptr;
Zach Steine20867f2018-08-02 13:20:15 -0700459 AsyncResolverFactory* const async_resolver_factory_ = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800460
Zhi Huang365381f2018-04-13 16:44:34 -0700461 std::map<std::string, std::unique_ptr<cricket::JsepTransport>>
Zhi Huange830e682018-03-30 10:48:35 -0700462 jsep_transports_by_name_;
Zhi Huangd2248f82018-04-10 14:41:03 -0700463 // This keeps track of the mapping between media section
Zhi Huang365381f2018-04-13 16:44:34 -0700464 // (BaseChannel/SctpTransport) and the JsepTransport underneath.
465 std::map<std::string, cricket::JsepTransport*> mid_to_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800466
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
479 Config config_;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800480
481 // Early on in the call we don't know if media transport is going to be used,
482 // but we need to get the server-supported parameters to add to an SDP.
483 // This server media transport will be promoted to the used media transport
484 // after the local description is set, and the ownership will be transferred
485 // to the actual JsepTransport.
486 // This "offer" media transport is not created if it's done on the party that
487 // provides answer. This offer media transport is only created once at the
488 // beginning of the connection, and never again.
489 std::unique_ptr<MediaTransportInterface> offer_media_transport_ = nullptr;
490
491 // Contains the offer of the |offer_media_transport_|, in case if it needs to
492 // be repeated.
493 absl::optional<cricket::SessionDescription::MediaTransportSetting>
494 media_transport_offer_settings_;
495
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700496 // Early on in the call we don't know if datagram transport is going to be
497 // used, but we need to get the server-supported parameters to add to an SDP.
498 // This server datagram transport will be promoted to the used datagram
499 // transport after the local description is set, and the ownership will be
500 // transferred to the actual JsepTransport. This "offer" datagram transport is
501 // not created if it's done on the party that provides answer. This offer
502 // datagram transport is only created once at the beginning of the connection,
503 // and never again.
504 std::unique_ptr<DatagramTransportInterface> offer_datagram_transport_ =
505 nullptr;
506
507 // Contains the offer of the |offer_datagram_transport_|, in case if it needs
508 // to be repeated.
509 absl::optional<cricket::SessionDescription::MediaTransportSetting>
510 datagram_transport_offer_settings_;
511
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800512 // When the new offer is regenerated (due to upgrade), we don't want to
513 // re-create media transport. New streams might be created; but media
514 // transport stays the same. This flag prevents re-creation of the transport
515 // on the offerer.
516 // The first media transport is created in jsep transport controller as the
517 // |offer_media_transport_|, and then the ownership is moved to the
518 // appropriate JsepTransport, at which point |offer_media_transport_| is
519 // zeroed out. On the callee (answerer), the first media transport is not even
520 // assigned to |offer_media_transport_|. Both offerer and answerer can
521 // recreate the Offer (e.g. after adding streams in Plan B), and so we want to
522 // prevent recreation of the media transport when that happens.
523 bool media_transport_created_once_ = false;
524
Zhi Huange818b6e2018-02-22 15:26:27 -0800525 const cricket::SessionDescription* local_desc_ = nullptr;
526 const cricket::SessionDescription* remote_desc_ = nullptr;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200527 absl::optional<bool> initial_offerer_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800528
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200529 absl::optional<cricket::ContentGroup> bundle_group_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800530
531 cricket::IceConfig ice_config_;
532 cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
533 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
534 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
535 rtc::AsyncInvoker invoker_;
536
Zhi Huange818b6e2018-02-22 15:26:27 -0800537 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransportController);
538};
539
540} // namespace webrtc
541
Steve Anton10542f22019-01-11 09:11:00 -0800542#endif // PC_JSEP_TRANSPORT_CONTROLLER_H_