blob: b07783c894a07f19ec3ed7bcd847c006e9034d91 [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"
Qingsi Wang25ec8882019-11-15 12:33:05 -080022#include "api/ice_transport_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/peer_connection_interface.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020024#include "api/rtc_event_log/rtc_event_log.h"
Niels Möller65f17ca2019-09-12 13:59:36 +020025#include "api/transport/media/media_transport_config.h"
26#include "api/transport/media/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "media/sctp/sctp_transport_internal.h"
28#include "p2p/base/dtls_transport.h"
Qingsi Wang25ec8882019-11-15 12:33:05 -080029#include "p2p/base/dtls_transport_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "p2p/base/p2p_transport_channel.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080031#include "pc/channel.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "pc/dtls_srtp_transport.h"
33#include "pc/dtls_transport.h"
34#include "pc/jsep_transport.h"
35#include "pc/rtp_transport.h"
36#include "pc/srtp_transport.h"
37#include "rtc_base/async_invoker.h"
38#include "rtc_base/constructor_magic.h"
39#include "rtc_base/ref_counted_object.h"
Artem Titove41c4332018-07-25 15:04:28 +020040#include "rtc_base/third_party/sigslot/sigslot.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080041
42namespace rtc {
43class Thread;
44class PacketTransportInternal;
45} // namespace rtc
46
47namespace webrtc {
48
Steve Antond25828a2018-08-31 13:06:05 -070049class JsepTransportController : public sigslot::has_slots<> {
Zhi Huange818b6e2018-02-22 15:26:27 -080050 public:
Zhi Huang365381f2018-04-13 16:44:34 -070051 // Used when the RtpTransport/DtlsTransport of the m= section is changed
52 // because the section is rejected or BUNDLE is enabled.
53 class Observer {
54 public:
55 virtual ~Observer() {}
56
57 // Returns true if media associated with |mid| was successfully set up to be
58 // demultiplexed on |rtp_transport|. Could return false if two bundled m=
59 // sections use the same SSRC, for example.
Bjorn A Mellemb689af42019-08-21 10:44:59 -070060 //
61 // If a data channel transport must be negotiated, |data_channel_transport|
62 // and |negotiation_state| indicate negotiation status. If
63 // |data_channel_transport| is null, the data channel transport should not
64 // be used. Otherwise, the value is a pointer to the transport to be used
65 // for data channels on |mid|, if any.
66 //
67 // The observer should not send data on |data_channel_transport| until
68 // |negotiation_state| is provisional or final. It should not delete
69 // |data_channel_transport| or any fallback transport until
70 // |negotiation_state| is final.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070071 virtual bool OnTransportChanged(
Zhi Huang365381f2018-04-13 16:44:34 -070072 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070073 RtpTransportInternal* rtp_transport,
Harald Alvestrandc85328f2019-02-28 07:51:00 +010074 rtc::scoped_refptr<DtlsTransport> dtls_transport,
Bjorn A Mellemb689af42019-08-21 10:44:59 -070075 MediaTransportInterface* media_transport,
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -070076 DataChannelTransportInterface* data_channel_transport) = 0;
Zhi Huang365381f2018-04-13 16:44:34 -070077 };
78
Zhi Huange818b6e2018-02-22 15:26:27 -080079 struct Config {
80 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
81 // upon setting a local transport description that indicates an ICE
82 // restart.
83 bool redetermine_role_on_ice_restart = true;
84 rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
85 // |crypto_options| is used to determine if created DTLS transports
86 // negotiate GCM crypto suites or not.
Benjamin Wrighta54daf12018-10-11 15:33:17 -070087 webrtc::CryptoOptions crypto_options;
Zhi Huange818b6e2018-02-22 15:26:27 -080088 PeerConnectionInterface::BundlePolicy bundle_policy =
89 PeerConnectionInterface::kBundlePolicyBalanced;
90 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
91 PeerConnectionInterface::kRtcpMuxPolicyRequire;
92 bool disable_encryption = false;
93 bool enable_external_auth = false;
94 // Used to inject the ICE/DTLS transports created externally.
Qingsi Wang25ec8882019-11-15 12:33:05 -080095 webrtc::IceTransportFactory* ice_transport_factory = nullptr;
96 cricket::DtlsTransportFactory* dtls_transport_factory = nullptr;
Zhi Huang365381f2018-04-13 16:44:34 -070097 Observer* transport_observer = nullptr;
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +020098 // Must be provided and valid for the lifetime of the
99 // JsepTransportController instance.
100 std::function<void(const rtc::CopyOnWriteBuffer& packet,
101 int64_t packet_time_us)>
102 rtcp_handler;
Zhi Huangb57e1692018-06-12 11:41:11 -0700103 bool active_reset_srtp_params = false;
Qingsi Wang7685e862018-06-11 20:15:46 -0700104 RtcEventLog* event_log = nullptr;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700105
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700106 // Factory for SCTP transports.
107 cricket::SctpTransportInternalFactory* sctp_factory = nullptr;
108
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800109 // Whether media transport is used for media.
110 bool use_media_transport_for_media = false;
111
112 // Whether media transport is used for data channels.
113 bool use_media_transport_for_data_channels = false;
114
Niels Möllerabea6e52019-03-08 14:49:32 +0100115 // Whether an RtpMediaTransport should be created as default, when no
116 // MediaTransportFactory is provided.
117 bool use_rtp_media_transport = false;
118
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700119 // Use encrypted datagram transport to send packets.
120 bool use_datagram_transport = false;
121
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700122 // Use datagram transport's implementation of data channels instead of SCTP.
123 bool use_datagram_transport_for_data_channels = false;
124
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700125 // Whether |use_datagram_transport_for_data_channels| applies to outgoing
126 // calls. If true, |use_datagram_transport_for_data_channels| applies only
127 // to incoming calls.
128 bool use_datagram_transport_for_data_channels_receive_only = false;
129
Anton Sukhanov7940da02018-10-10 10:34:49 -0700130 // Optional media transport factory (experimental). If provided it will be
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800131 // used to create media_transport (as long as either
132 // |use_media_transport_for_media| or
133 // |use_media_transport_for_data_channels| is set to true). However, whether
134 // it will be used to send / receive audio and video frames instead of RTP
135 // is determined by |use_media_transport_for_media|. Note that currently
136 // media_transport co-exists with RTP / RTCP transports and may use the same
Anton Sukhanov7940da02018-10-10 10:34:49 -0700137 // underlying ICE transport.
138 MediaTransportFactory* media_transport_factory = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800139 };
140
141 // The ICE related events are signaled on the |signaling_thread|.
142 // All the transport related methods are called on the |network_thread|.
143 JsepTransportController(rtc::Thread* signaling_thread,
144 rtc::Thread* network_thread,
145 cricket::PortAllocator* port_allocator,
Zach Steine20867f2018-08-02 13:20:15 -0700146 AsyncResolverFactory* async_resolver_factory,
Zhi Huange818b6e2018-02-22 15:26:27 -0800147 Config config);
148 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
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700172 MediaTransportConfig GetMediaTransportConfig(const std::string& mid) const;
173
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700174 DataChannelTransportInterface* GetDataChannelTransport(
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700175 const std::string& mid) const;
176
177 // TODO(sukhanov): Deprecate, return only config.
178 MediaTransportInterface* GetMediaTransport(const std::string& mid) const {
179 return GetMediaTransportConfig(mid).media_transport;
180 }
181
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800182 MediaTransportState GetMediaTransportState(const std::string& mid) const;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700183
Zhi Huange818b6e2018-02-22 15:26:27 -0800184 /*********************
185 * ICE-related methods
186 ********************/
187 // This method is public to allow PeerConnection to update it from
188 // SetConfiguration.
189 void SetIceConfig(const cricket::IceConfig& config);
190 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
191 // set, offers should generate new ufrags/passwords until an ICE restart
192 // occurs.
193 void SetNeedsIceRestartFlag();
194 // Returns true if the ICE restart flag above was set, and no ICE restart has
195 // occurred yet for this transport (by applying a local description with
196 // changed ufrag/password). If the transport has been deleted as a result of
197 // bundling, returns false.
198 bool NeedsIceRestart(const std::string& mid) const;
199 // Start gathering candidates for any new transports, or transports doing an
200 // ICE restart.
201 void MaybeStartGathering();
202 RTCError AddRemoteCandidates(
203 const std::string& mid,
204 const std::vector<cricket::Candidate>& candidates);
205 RTCError RemoveRemoteCandidates(
206 const std::vector<cricket::Candidate>& candidates);
207
208 /**********************
209 * DTLS-related methods
210 *********************/
211 // Specifies the identity to use in this session.
212 // Can only be called once.
213 bool SetLocalCertificate(
214 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
215 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
216 const std::string& mid) const;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800217 // Caller owns returned certificate chain. This method mainly exists for
218 // stats reporting.
219 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800220 const std::string& mid) const;
221 // Get negotiated role, if one has been negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200222 absl::optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800223
224 // TODO(deadbeef): GetStats isn't const because all the way down to
225 // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
226 // const. Fix this.
227 bool GetStats(const std::string& mid, cricket::TransportStats* stats);
Zhi Huange818b6e2018-02-22 15:26:27 -0800228
Zhi Huange830e682018-03-30 10:48:35 -0700229 bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; }
Zhi Huang365381f2018-04-13 16:44:34 -0700230
Zhi Huangb57e1692018-06-12 11:41:11 -0700231 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
232
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700233 // Allows to overwrite the settings from config. You may set or reset the
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800234 // media transport configuration on the jsep transport controller, as long as
235 // you did not call 'GetMediaTransport' or 'MaybeCreateJsepTransport'. Once
236 // Jsep transport is created, you can't change this setting.
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700237 void SetMediaTransportSettings(
238 bool use_media_transport_for_media,
239 bool use_media_transport_for_data_channels,
240 bool use_datagram_transport,
241 bool use_datagram_transport_for_data_channels,
242 bool use_datagram_transport_for_data_channels_receive_only);
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700243
Eldar Rello5ab79e62019-10-09 18:29:44 +0300244 // TODO(elrello): For now the rollback only removes mid to transport mapping
245 // and deletes unused transport, but doesn't consider anything more complex.
246 void RollbackTransportForMid(const std::string& mid);
247
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800248 // If media transport is present enabled and supported,
249 // when this method is called, it creates a media transport and generates its
250 // offer. The new offer is then returned, and the created media transport will
251 // subsequently be used.
252 absl::optional<cricket::SessionDescription::MediaTransportSetting>
253 GenerateOrGetLastMediaTransportOffer();
254
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -0700255 // Gets the transport parameters for the transport identified by |mid|.
256 // If |mid| is bundled, returns the parameters for the bundled transport.
257 // If the transport for |mid| has not been created yet, it may be allocated in
258 // order to generate transport parameters.
259 absl::optional<cricket::OpaqueTransportParameters> GetTransportParameters(
260 const std::string& mid);
261
Zhi Huange818b6e2018-02-22 15:26:27 -0800262 // All of these signals are fired on the signaling thread.
263
264 // If any transport failed => failed,
265 // Else if all completed => completed,
266 // Else if all connected => connected,
267 // Else => connecting
Alex Loiko9289eda2018-11-23 16:18:59 +0000268 sigslot::signal1<cricket::IceConnectionState> SignalIceConnectionState;
Zhi Huange818b6e2018-02-22 15:26:27 -0800269
Jonas Olsson635474e2018-10-18 15:58:17 +0200270 sigslot::signal1<PeerConnectionInterface::PeerConnectionState>
271 SignalConnectionState;
Alex Loiko9289eda2018-11-23 16:18:59 +0000272 sigslot::signal1<PeerConnectionInterface::IceConnectionState>
273 SignalStandardizedIceConnectionState;
Jonas Olsson635474e2018-10-18 15:58:17 +0200274
Zhi Huange818b6e2018-02-22 15:26:27 -0800275 // If all transports done gathering => complete,
276 // Else if any are gathering => gathering,
277 // Else => new
278 sigslot::signal1<cricket::IceGatheringState> SignalIceGatheringState;
279
280 // (mid, candidates)
281 sigslot::signal2<const std::string&, const std::vector<cricket::Candidate>&>
282 SignalIceCandidatesGathered;
283
Eldar Relloda13ea22019-06-01 12:23:43 +0300284 sigslot::signal1<const cricket::IceCandidateErrorEvent&>
285 SignalIceCandidateError;
286
Zhi Huange818b6e2018-02-22 15:26:27 -0800287 sigslot::signal1<const std::vector<cricket::Candidate>&>
288 SignalIceCandidatesRemoved;
289
Alex Drake00c7ecf2019-08-06 10:54:47 -0700290 sigslot::signal1<const cricket::CandidatePairChangeEvent&>
291 SignalIceCandidatePairChanged;
292
Zhi Huange818b6e2018-02-22 15:26:27 -0800293 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
294
Zhi Huange818b6e2018-02-22 15:26:27 -0800295 private:
Zhi Huange818b6e2018-02-22 15:26:27 -0800296 RTCError ApplyDescription_n(bool local,
297 SdpType type,
298 const cricket::SessionDescription* description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700299 RTCError ValidateAndMaybeUpdateBundleGroup(
300 bool local,
301 SdpType type,
302 const cricket::SessionDescription* description);
Zhi Huange830e682018-03-30 10:48:35 -0700303 RTCError ValidateContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800304
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700305 void HandleRejectedContent(const cricket::ContentInfo& content_info,
Zhi Huangd2248f82018-04-10 14:41:03 -0700306 const cricket::SessionDescription* description);
Zhi Huang365381f2018-04-13 16:44:34 -0700307 bool HandleBundledContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800308
Zhi Huang365381f2018-04-13 16:44:34 -0700309 bool SetTransportForMid(const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700310 cricket::JsepTransport* jsep_transport);
311 void RemoveTransportForMid(const std::string& mid);
Zhi Huangd2248f82018-04-10 14:41:03 -0700312
Zhi Huange818b6e2018-02-22 15:26:27 -0800313 cricket::JsepTransportDescription CreateJsepTransportDescription(
Harald Alvestrand1716d392019-06-03 20:35:45 +0200314 const cricket::ContentInfo& content_info,
315 const cricket::TransportInfo& transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700316 const std::vector<int>& encrypted_extension_ids,
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -0700317 int rtp_abs_sendtime_extn_id,
318 absl::optional<std::string> media_alt_protocol,
319 absl::optional<std::string> data_alt_protocol);
Zhi Huange818b6e2018-02-22 15:26:27 -0800320
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200321 absl::optional<std::string> bundled_mid() const {
322 absl::optional<std::string> bundled_mid;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700323 if (bundle_group_ && bundle_group_->FirstContentName()) {
324 bundled_mid = *(bundle_group_->FirstContentName());
Zhi Huange818b6e2018-02-22 15:26:27 -0800325 }
326 return bundled_mid;
327 }
328
329 bool IsBundled(const std::string& mid) const {
330 return bundle_group_ && bundle_group_->HasContentName(mid);
331 }
332
333 bool ShouldUpdateBundleGroup(SdpType type,
334 const cricket::SessionDescription* description);
335
336 std::vector<int> MergeEncryptedHeaderExtensionIdsForBundle(
337 const cricket::SessionDescription* description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800338 std::vector<int> GetEncryptedHeaderExtensionIds(
339 const cricket::ContentInfo& content_info);
340
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -0700341 // Extracts the alt-protocol settings that apply to the bundle group.
342 RTCError GetAltProtocolsForBundle(
343 const cricket::SessionDescription* description,
344 absl::optional<std::string>* media_alt_protocol,
345 absl::optional<std::string>* data_alt_protocol);
346
Zhi Huange830e682018-03-30 10:48:35 -0700347 int GetRtpAbsSendTimeHeaderExtensionId(
348 const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800349
Zhi Huange830e682018-03-30 10:48:35 -0700350 // This method takes the BUNDLE group into account. If the JsepTransport is
351 // destroyed because of BUNDLE, it would return the transport which other
352 // transports are bundled on (In current implementation, it is the first
353 // content in the BUNDLE group).
Zhi Huang365381f2018-04-13 16:44:34 -0700354 const cricket::JsepTransport* GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700355 const std::string& mid) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700356 cricket::JsepTransport* GetJsepTransportForMid(const std::string& mid);
Zhi Huange830e682018-03-30 10:48:35 -0700357
358 // Get the JsepTransport without considering the BUNDLE group. Return nullptr
359 // if the JsepTransport is destroyed.
Zhi Huang365381f2018-04-13 16:44:34 -0700360 const cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700361 const std::string& transport_name) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700362 cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700363 const std::string& transport_name);
364
Anton Sukhanov7940da02018-10-10 10:34:49 -0700365 // Creates jsep transport. Noop if transport is already created.
366 // Transport is created either during SetLocalDescription (|local| == true) or
367 // during SetRemoteDescription (|local| == false). Passing |local| helps to
368 // differentiate initiator (caller) from answerer (callee).
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -0800369 RTCError MaybeCreateJsepTransport(
370 bool local,
371 const cricket::ContentInfo& content_info,
372 const cricket::SessionDescription& description);
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800373
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800374 // Creates media transport if config wants to use it, and a=x-mt line is
375 // present for the current media transport. Returned MediaTransportInterface
376 // is not connected, and must be connected to ICE. You must call
377 // |GenerateOrGetLastMediaTransportOffer| on the caller before calling
378 // MaybeCreateMediaTransport.
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800379 std::unique_ptr<webrtc::MediaTransportInterface> MaybeCreateMediaTransport(
380 const cricket::ContentInfo& content_info,
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -0800381 const cricket::SessionDescription& description,
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800382 bool local);
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700383
384 // Creates datagram transport if config wants to use it, and a=x-mt line is
385 // present for the current media transport. Returned
386 // DatagramTransportInterface is not connected, and must be connected to ICE.
387 // You must call |GenerateOrGetLastMediaTransportOffer| on the caller before
388 // calling MaybeCreateDatagramTransport.
389 std::unique_ptr<webrtc::DatagramTransportInterface>
390 MaybeCreateDatagramTransport(const cricket::ContentInfo& content_info,
391 const cricket::SessionDescription& description,
392 bool local);
393
Zhi Huange818b6e2018-02-22 15:26:27 -0800394 void MaybeDestroyJsepTransport(const std::string& mid);
395 void DestroyAllJsepTransports_n();
396
397 void SetIceRole_n(cricket::IceRole ice_role);
398
399 cricket::IceRole DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -0700400 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -0800401 const cricket::TransportInfo& transport_info,
402 SdpType type,
403 bool local);
404
405 std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
Anton Sukhanovac6c0962019-07-10 15:44:56 -0700406 const cricket::ContentInfo& content_info,
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700407 cricket::IceTransportInternal* ice,
Anton Sukhanov292ce4e2019-06-03 13:00:24 -0700408 DatagramTransportInterface* datagram_transport);
Qingsi Wang25ec8882019-11-15 12:33:05 -0800409 rtc::scoped_refptr<webrtc::IceTransportInterface> CreateIceTransport(
410 const std::string& transport_name,
Zhi Huange818b6e2018-02-22 15:26:27 -0800411 bool rtcp);
412
413 std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
414 const std::string& transport_name,
415 rtc::PacketTransportInternal* rtp_packet_transport,
416 rtc::PacketTransportInternal* rtcp_packet_transport);
417 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
418 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700419 cricket::DtlsTransportInternal* rtp_dtls_transport,
420 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800421 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
422 const std::string& transport_name,
423 cricket::DtlsTransportInternal* rtp_dtls_transport,
424 cricket::DtlsTransportInternal* rtcp_dtls_transport);
425
426 // Collect all the DtlsTransports, including RTP and RTCP, from the
427 // JsepTransports. JsepTransportController can iterate all the DtlsTransports
428 // and update the aggregate states.
429 std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
430
431 // Handlers for signals from Transport.
432 void OnTransportWritableState_n(rtc::PacketTransportInternal* transport);
433 void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport);
434 void OnTransportGatheringState_n(cricket::IceTransportInternal* transport);
435 void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
436 const cricket::Candidate& candidate);
Eldar Relloda13ea22019-06-01 12:23:43 +0300437 void OnTransportCandidateError_n(
438 cricket::IceTransportInternal* transport,
439 const cricket::IceCandidateErrorEvent& event);
Zhi Huange818b6e2018-02-22 15:26:27 -0800440 void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
441 const cricket::Candidates& candidates);
442 void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport);
443 void OnTransportStateChanged_n(cricket::IceTransportInternal* transport);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800444 void OnMediaTransportStateChanged_n();
Alex Drake00c7ecf2019-08-06 10:54:47 -0700445 void OnTransportCandidatePairChanged_n(
446 const cricket::CandidatePairChangeEvent& event);
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700447 void OnDataChannelTransportNegotiated_n(
448 cricket::JsepTransport* transport,
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700449 DataChannelTransportInterface* data_channel_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800450
451 void UpdateAggregateStates_n();
452
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +0200453 void OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer* packet,
454 int64_t packet_time_us);
455
Zhi Huange818b6e2018-02-22 15:26:27 -0800456 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
457
458 rtc::Thread* const signaling_thread_ = nullptr;
459 rtc::Thread* const network_thread_ = nullptr;
460 cricket::PortAllocator* const port_allocator_ = nullptr;
Zach Steine20867f2018-08-02 13:20:15 -0700461 AsyncResolverFactory* const async_resolver_factory_ = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800462
Zhi Huang365381f2018-04-13 16:44:34 -0700463 std::map<std::string, std::unique_ptr<cricket::JsepTransport>>
Zhi Huange830e682018-03-30 10:48:35 -0700464 jsep_transports_by_name_;
Zhi Huangd2248f82018-04-10 14:41:03 -0700465 // This keeps track of the mapping between media section
Zhi Huang365381f2018-04-13 16:44:34 -0700466 // (BaseChannel/SctpTransport) and the JsepTransport underneath.
467 std::map<std::string, cricket::JsepTransport*> mid_to_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800468
Jonas Olsson635474e2018-10-18 15:58:17 +0200469 // Aggregate states for Transports.
Alex Loiko9289eda2018-11-23 16:18:59 +0000470 // standardized_ice_connection_state_ is intended to replace
471 // ice_connection_state, see bugs.webrtc.org/9308
472 cricket::IceConnectionState ice_connection_state_ =
473 cricket::kIceConnectionConnecting;
474 PeerConnectionInterface::IceConnectionState
475 standardized_ice_connection_state_ =
476 PeerConnectionInterface::kIceConnectionNew;
Jonas Olsson635474e2018-10-18 15:58:17 +0200477 PeerConnectionInterface::PeerConnectionState combined_connection_state_ =
478 PeerConnectionInterface::PeerConnectionState::kNew;
Zhi Huange818b6e2018-02-22 15:26:27 -0800479 cricket::IceGatheringState ice_gathering_state_ = cricket::kIceGatheringNew;
480
481 Config config_;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800482
483 // Early on in the call we don't know if media transport is going to be used,
484 // but we need to get the server-supported parameters to add to an SDP.
485 // This server media transport will be promoted to the used media transport
486 // after the local description is set, and the ownership will be transferred
487 // to the actual JsepTransport.
488 // This "offer" media transport is not created if it's done on the party that
489 // provides answer. This offer media transport is only created once at the
490 // beginning of the connection, and never again.
491 std::unique_ptr<MediaTransportInterface> offer_media_transport_ = nullptr;
492
493 // Contains the offer of the |offer_media_transport_|, in case if it needs to
494 // be repeated.
495 absl::optional<cricket::SessionDescription::MediaTransportSetting>
496 media_transport_offer_settings_;
497
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700498 // Early on in the call we don't know if datagram transport is going to be
499 // used, but we need to get the server-supported parameters to add to an SDP.
500 // This server datagram transport will be promoted to the used datagram
501 // transport after the local description is set, and the ownership will be
502 // transferred to the actual JsepTransport. This "offer" datagram transport is
503 // not created if it's done on the party that provides answer. This offer
504 // datagram transport is only created once at the beginning of the connection,
505 // and never again.
506 std::unique_ptr<DatagramTransportInterface> offer_datagram_transport_ =
507 nullptr;
508
509 // Contains the offer of the |offer_datagram_transport_|, in case if it needs
510 // to be repeated.
511 absl::optional<cricket::SessionDescription::MediaTransportSetting>
512 datagram_transport_offer_settings_;
513
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800514 // When the new offer is regenerated (due to upgrade), we don't want to
515 // re-create media transport. New streams might be created; but media
516 // transport stays the same. This flag prevents re-creation of the transport
517 // on the offerer.
518 // The first media transport is created in jsep transport controller as the
519 // |offer_media_transport_|, and then the ownership is moved to the
520 // appropriate JsepTransport, at which point |offer_media_transport_| is
521 // zeroed out. On the callee (answerer), the first media transport is not even
522 // assigned to |offer_media_transport_|. Both offerer and answerer can
523 // recreate the Offer (e.g. after adding streams in Plan B), and so we want to
524 // prevent recreation of the media transport when that happens.
525 bool media_transport_created_once_ = false;
526
Zhi Huange818b6e2018-02-22 15:26:27 -0800527 const cricket::SessionDescription* local_desc_ = nullptr;
528 const cricket::SessionDescription* remote_desc_ = nullptr;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200529 absl::optional<bool> initial_offerer_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800530
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200531 absl::optional<cricket::ContentGroup> bundle_group_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800532
533 cricket::IceConfig ice_config_;
534 cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
535 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
536 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
537 rtc::AsyncInvoker invoker_;
538
Zhi Huange818b6e2018-02-22 15:26:27 -0800539 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransportController);
540};
541
542} // namespace webrtc
543
Steve Anton10542f22019-01-11 09:11:00 -0800544#endif // PC_JSEP_TRANSPORT_CONTROLLER_H_