blob: e57b7d8fce52844343aedf0674827d9b99ebf949 [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"
Anton Sukhanov7940da02018-10-10 10:34:49 -070022#include "api/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/peer_connection_interface.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070024#include "logging/rtc_event_log/rtc_event_log.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "media/sctp/sctp_transport_internal.h"
26#include "p2p/base/dtls_transport.h"
27#include "p2p/base/p2p_transport_channel.h"
28#include "p2p/base/transport_factory_interface.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080029#include "pc/channel.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "pc/dtls_srtp_transport.h"
31#include "pc/dtls_transport.h"
32#include "pc/jsep_transport.h"
33#include "pc/rtp_transport.h"
34#include "pc/srtp_transport.h"
35#include "rtc_base/async_invoker.h"
36#include "rtc_base/constructor_magic.h"
37#include "rtc_base/ref_counted_object.h"
Artem Titove41c4332018-07-25 15:04:28 +020038#include "rtc_base/third_party/sigslot/sigslot.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080039
40namespace rtc {
41class Thread;
42class PacketTransportInternal;
43} // namespace rtc
44
45namespace webrtc {
46
Steve Antond25828a2018-08-31 13:06:05 -070047class JsepTransportController : public sigslot::has_slots<> {
Zhi Huange818b6e2018-02-22 15:26:27 -080048 public:
Zhi Huang365381f2018-04-13 16:44:34 -070049 // Used when the RtpTransport/DtlsTransport of the m= section is changed
50 // because the section is rejected or BUNDLE is enabled.
51 class Observer {
52 public:
53 virtual ~Observer() {}
54
55 // Returns true if media associated with |mid| was successfully set up to be
56 // demultiplexed on |rtp_transport|. Could return false if two bundled m=
57 // sections use the same SSRC, for example.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070058 virtual bool OnTransportChanged(
Zhi Huang365381f2018-04-13 16:44:34 -070059 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070060 RtpTransportInternal* rtp_transport,
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -080061 cricket::DtlsTransportInternal* dtls_transport,
62 MediaTransportInterface* media_transport) = 0;
Zhi Huang365381f2018-04-13 16:44:34 -070063 };
64
Zhi Huange818b6e2018-02-22 15:26:27 -080065 struct Config {
66 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
67 // upon setting a local transport description that indicates an ICE
68 // restart.
69 bool redetermine_role_on_ice_restart = true;
70 rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
71 // |crypto_options| is used to determine if created DTLS transports
72 // negotiate GCM crypto suites or not.
Benjamin Wrighta54daf12018-10-11 15:33:17 -070073 webrtc::CryptoOptions crypto_options;
Zhi Huange818b6e2018-02-22 15:26:27 -080074 PeerConnectionInterface::BundlePolicy bundle_policy =
75 PeerConnectionInterface::kBundlePolicyBalanced;
76 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
77 PeerConnectionInterface::kRtcpMuxPolicyRequire;
78 bool disable_encryption = false;
79 bool enable_external_auth = false;
80 // Used to inject the ICE/DTLS transports created externally.
81 cricket::TransportFactoryInterface* external_transport_factory = nullptr;
Zhi Huang365381f2018-04-13 16:44:34 -070082 Observer* transport_observer = nullptr;
Zhi Huangb57e1692018-06-12 11:41:11 -070083 bool active_reset_srtp_params = false;
Qingsi Wang7685e862018-06-11 20:15:46 -070084 RtcEventLog* event_log = nullptr;
Anton Sukhanov7940da02018-10-10 10:34:49 -070085
86 // Optional media transport factory (experimental). If provided it will be
87 // used to create media_transport and will be used to send / receive
88 // audio and video frames instead of RTP. Note that currently
89 // media_transport co-exists with RTP / RTCP transports and uses the same
90 // underlying ICE transport.
91 MediaTransportFactory* media_transport_factory = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -080092 };
93
94 // The ICE related events are signaled on the |signaling_thread|.
95 // All the transport related methods are called on the |network_thread|.
96 JsepTransportController(rtc::Thread* signaling_thread,
97 rtc::Thread* network_thread,
98 cricket::PortAllocator* port_allocator,
Zach Steine20867f2018-08-02 13:20:15 -070099 AsyncResolverFactory* async_resolver_factory,
Zhi Huange818b6e2018-02-22 15:26:27 -0800100 Config config);
101 virtual ~JsepTransportController();
102
103 // The main method to be called; applies a description at the transport
104 // level, creating/destroying transport objects as needed and updating their
105 // properties. This includes RTP, DTLS, and ICE (but not SCTP). At least not
106 // yet? May make sense to in the future.
107 RTCError SetLocalDescription(SdpType type,
108 const cricket::SessionDescription* description);
109
110 RTCError SetRemoteDescription(SdpType type,
111 const cricket::SessionDescription* description);
112
113 // Get transports to be used for the provided |mid|. If bundling is enabled,
114 // calling GetRtpTransport for multiple MIDs may yield the same object.
115 RtpTransportInternal* GetRtpTransport(const std::string& mid) const;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100116 cricket::DtlsTransportInternal* GetDtlsTransport(const std::string& mid);
117 const cricket::DtlsTransportInternal* GetRtcpDtlsTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -0800118 const std::string& mid) const;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100119 // Gets the externally sharable version of the DtlsTransport.
120 rtc::scoped_refptr<webrtc::DtlsTransportInterface> LookupDtlsTransportByMid(
121 const std::string& mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800122
Anton Sukhanov7940da02018-10-10 10:34:49 -0700123 MediaTransportInterface* GetMediaTransport(const std::string& mid) const;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800124 MediaTransportState GetMediaTransportState(const std::string& mid) const;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700125
Zhi Huange818b6e2018-02-22 15:26:27 -0800126 /*********************
127 * ICE-related methods
128 ********************/
129 // This method is public to allow PeerConnection to update it from
130 // SetConfiguration.
131 void SetIceConfig(const cricket::IceConfig& config);
132 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
133 // set, offers should generate new ufrags/passwords until an ICE restart
134 // occurs.
135 void SetNeedsIceRestartFlag();
136 // Returns true if the ICE restart flag above was set, and no ICE restart has
137 // occurred yet for this transport (by applying a local description with
138 // changed ufrag/password). If the transport has been deleted as a result of
139 // bundling, returns false.
140 bool NeedsIceRestart(const std::string& mid) const;
141 // Start gathering candidates for any new transports, or transports doing an
142 // ICE restart.
143 void MaybeStartGathering();
144 RTCError AddRemoteCandidates(
145 const std::string& mid,
146 const std::vector<cricket::Candidate>& candidates);
147 RTCError RemoveRemoteCandidates(
148 const std::vector<cricket::Candidate>& candidates);
149
150 /**********************
151 * DTLS-related methods
152 *********************/
153 // Specifies the identity to use in this session.
154 // Can only be called once.
155 bool SetLocalCertificate(
156 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
157 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
158 const std::string& mid) const;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800159 // Caller owns returned certificate chain. This method mainly exists for
160 // stats reporting.
161 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800162 const std::string& mid) const;
163 // Get negotiated role, if one has been negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200164 absl::optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800165
166 // TODO(deadbeef): GetStats isn't const because all the way down to
167 // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
168 // const. Fix this.
169 bool GetStats(const std::string& mid, cricket::TransportStats* stats);
Zhi Huange818b6e2018-02-22 15:26:27 -0800170
Zhi Huange830e682018-03-30 10:48:35 -0700171 bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; }
Zhi Huang365381f2018-04-13 16:44:34 -0700172
Zhi Huangb57e1692018-06-12 11:41:11 -0700173 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
174
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700175 // Allows to overwrite the settings from config. You may set or reset the
176 // media transport factory on the jsep transport controller, as long as you
177 // did not call 'GetMediaTransport' or 'MaybeCreateJsepTransport'. Once Jsep
178 // transport is created, you can't change this setting.
179 void SetMediaTransportFactory(MediaTransportFactory* media_transport_factory);
180
Zhi Huange818b6e2018-02-22 15:26:27 -0800181 // All of these signals are fired on the signaling thread.
182
183 // If any transport failed => failed,
184 // Else if all completed => completed,
185 // Else if all connected => connected,
186 // Else => connecting
Alex Loiko9289eda2018-11-23 16:18:59 +0000187 sigslot::signal1<cricket::IceConnectionState> SignalIceConnectionState;
Zhi Huange818b6e2018-02-22 15:26:27 -0800188
Jonas Olsson635474e2018-10-18 15:58:17 +0200189 sigslot::signal1<PeerConnectionInterface::PeerConnectionState>
190 SignalConnectionState;
Alex Loiko9289eda2018-11-23 16:18:59 +0000191 sigslot::signal1<PeerConnectionInterface::IceConnectionState>
192 SignalStandardizedIceConnectionState;
Jonas Olsson635474e2018-10-18 15:58:17 +0200193
Zhi Huange818b6e2018-02-22 15:26:27 -0800194 // If all transports done gathering => complete,
195 // Else if any are gathering => gathering,
196 // Else => new
197 sigslot::signal1<cricket::IceGatheringState> SignalIceGatheringState;
198
199 // (mid, candidates)
200 sigslot::signal2<const std::string&, const std::vector<cricket::Candidate>&>
201 SignalIceCandidatesGathered;
202
203 sigslot::signal1<const std::vector<cricket::Candidate>&>
204 SignalIceCandidatesRemoved;
205
206 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
207
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800208 sigslot::signal<> SignalMediaTransportStateChanged;
209
Zhi Huange818b6e2018-02-22 15:26:27 -0800210 private:
Zhi Huange818b6e2018-02-22 15:26:27 -0800211 RTCError ApplyDescription_n(bool local,
212 SdpType type,
213 const cricket::SessionDescription* description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700214 RTCError ValidateAndMaybeUpdateBundleGroup(
215 bool local,
216 SdpType type,
217 const cricket::SessionDescription* description);
Zhi Huange830e682018-03-30 10:48:35 -0700218 RTCError ValidateContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800219
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700220 void HandleRejectedContent(const cricket::ContentInfo& content_info,
Zhi Huangd2248f82018-04-10 14:41:03 -0700221 const cricket::SessionDescription* description);
Zhi Huang365381f2018-04-13 16:44:34 -0700222 bool HandleBundledContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800223
Zhi Huang365381f2018-04-13 16:44:34 -0700224 bool SetTransportForMid(const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700225 cricket::JsepTransport* jsep_transport);
226 void RemoveTransportForMid(const std::string& mid);
Zhi Huangd2248f82018-04-10 14:41:03 -0700227
Zhi Huange818b6e2018-02-22 15:26:27 -0800228 cricket::JsepTransportDescription CreateJsepTransportDescription(
229 cricket::ContentInfo content_info,
230 cricket::TransportInfo transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700231 const std::vector<int>& encrypted_extension_ids,
232 int rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800233
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200234 absl::optional<std::string> bundled_mid() const {
235 absl::optional<std::string> bundled_mid;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700236 if (bundle_group_ && bundle_group_->FirstContentName()) {
237 bundled_mid = *(bundle_group_->FirstContentName());
Zhi Huange818b6e2018-02-22 15:26:27 -0800238 }
239 return bundled_mid;
240 }
241
242 bool IsBundled(const std::string& mid) const {
243 return bundle_group_ && bundle_group_->HasContentName(mid);
244 }
245
246 bool ShouldUpdateBundleGroup(SdpType type,
247 const cricket::SessionDescription* description);
248
249 std::vector<int> MergeEncryptedHeaderExtensionIdsForBundle(
250 const cricket::SessionDescription* description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800251 std::vector<int> GetEncryptedHeaderExtensionIds(
252 const cricket::ContentInfo& content_info);
253
Zhi Huange830e682018-03-30 10:48:35 -0700254 int GetRtpAbsSendTimeHeaderExtensionId(
255 const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800256
Zhi Huange830e682018-03-30 10:48:35 -0700257 // This method takes the BUNDLE group into account. If the JsepTransport is
258 // destroyed because of BUNDLE, it would return the transport which other
259 // transports are bundled on (In current implementation, it is the first
260 // content in the BUNDLE group).
Zhi Huang365381f2018-04-13 16:44:34 -0700261 const cricket::JsepTransport* GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700262 const std::string& mid) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700263 cricket::JsepTransport* GetJsepTransportForMid(const std::string& mid);
Zhi Huange830e682018-03-30 10:48:35 -0700264
265 // Get the JsepTransport without considering the BUNDLE group. Return nullptr
266 // if the JsepTransport is destroyed.
Zhi Huang365381f2018-04-13 16:44:34 -0700267 const cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700268 const std::string& transport_name) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700269 cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700270 const std::string& transport_name);
271
Anton Sukhanov7940da02018-10-10 10:34:49 -0700272 // Creates jsep transport. Noop if transport is already created.
273 // Transport is created either during SetLocalDescription (|local| == true) or
274 // during SetRemoteDescription (|local| == false). Passing |local| helps to
275 // differentiate initiator (caller) from answerer (callee).
276 RTCError MaybeCreateJsepTransport(bool local,
277 const cricket::ContentInfo& content_info);
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800278
279 // Creates media transport if config wants to use it, and pre-shared key is
280 // provided in content info. It modifies the config to disable media transport
281 // if pre-shared key is not provided.
282 std::unique_ptr<webrtc::MediaTransportInterface> MaybeCreateMediaTransport(
283 const cricket::ContentInfo& content_info,
284 bool local,
285 cricket::IceTransportInternal* ice_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800286 void MaybeDestroyJsepTransport(const std::string& mid);
287 void DestroyAllJsepTransports_n();
288
289 void SetIceRole_n(cricket::IceRole ice_role);
290
291 cricket::IceRole DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -0700292 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -0800293 const cricket::TransportInfo& transport_info,
294 SdpType type,
295 bool local);
296
297 std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800298 std::unique_ptr<cricket::IceTransportInternal> ice);
299 std::unique_ptr<cricket::IceTransportInternal> CreateIceTransport(
300 const std::string transport_name,
Zhi Huange818b6e2018-02-22 15:26:27 -0800301 bool rtcp);
302
303 std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
304 const std::string& transport_name,
305 rtc::PacketTransportInternal* rtp_packet_transport,
306 rtc::PacketTransportInternal* rtcp_packet_transport);
307 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
308 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700309 cricket::DtlsTransportInternal* rtp_dtls_transport,
310 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800311 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
312 const std::string& transport_name,
313 cricket::DtlsTransportInternal* rtp_dtls_transport,
314 cricket::DtlsTransportInternal* rtcp_dtls_transport);
315
316 // Collect all the DtlsTransports, including RTP and RTCP, from the
317 // JsepTransports. JsepTransportController can iterate all the DtlsTransports
318 // and update the aggregate states.
319 std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
320
321 // Handlers for signals from Transport.
322 void OnTransportWritableState_n(rtc::PacketTransportInternal* transport);
323 void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport);
324 void OnTransportGatheringState_n(cricket::IceTransportInternal* transport);
325 void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
326 const cricket::Candidate& candidate);
Zhi Huange818b6e2018-02-22 15:26:27 -0800327 void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
328 const cricket::Candidates& candidates);
329 void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport);
330 void OnTransportStateChanged_n(cricket::IceTransportInternal* transport);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800331 void OnMediaTransportStateChanged_n();
Zhi Huange818b6e2018-02-22 15:26:27 -0800332
333 void UpdateAggregateStates_n();
334
335 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
336
337 rtc::Thread* const signaling_thread_ = nullptr;
338 rtc::Thread* const network_thread_ = nullptr;
339 cricket::PortAllocator* const port_allocator_ = nullptr;
Zach Steine20867f2018-08-02 13:20:15 -0700340 AsyncResolverFactory* const async_resolver_factory_ = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800341
Zhi Huang365381f2018-04-13 16:44:34 -0700342 std::map<std::string, std::unique_ptr<cricket::JsepTransport>>
Zhi Huange830e682018-03-30 10:48:35 -0700343 jsep_transports_by_name_;
Zhi Huangd2248f82018-04-10 14:41:03 -0700344 // This keeps track of the mapping between media section
Zhi Huang365381f2018-04-13 16:44:34 -0700345 // (BaseChannel/SctpTransport) and the JsepTransport underneath.
346 std::map<std::string, cricket::JsepTransport*> mid_to_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800347
Jonas Olsson635474e2018-10-18 15:58:17 +0200348 // Aggregate states for Transports.
Alex Loiko9289eda2018-11-23 16:18:59 +0000349 // standardized_ice_connection_state_ is intended to replace
350 // ice_connection_state, see bugs.webrtc.org/9308
351 cricket::IceConnectionState ice_connection_state_ =
352 cricket::kIceConnectionConnecting;
353 PeerConnectionInterface::IceConnectionState
354 standardized_ice_connection_state_ =
355 PeerConnectionInterface::kIceConnectionNew;
Jonas Olsson635474e2018-10-18 15:58:17 +0200356 PeerConnectionInterface::PeerConnectionState combined_connection_state_ =
357 PeerConnectionInterface::PeerConnectionState::kNew;
Zhi Huange818b6e2018-02-22 15:26:27 -0800358 cricket::IceGatheringState ice_gathering_state_ = cricket::kIceGatheringNew;
359
360 Config config_;
361 const cricket::SessionDescription* local_desc_ = nullptr;
362 const cricket::SessionDescription* remote_desc_ = nullptr;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200363 absl::optional<bool> initial_offerer_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800364
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200365 absl::optional<cricket::ContentGroup> bundle_group_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800366
367 cricket::IceConfig ice_config_;
368 cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
369 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
370 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
371 rtc::AsyncInvoker invoker_;
372
Zhi Huange818b6e2018-02-22 15:26:27 -0800373 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransportController);
374};
375
376} // namespace webrtc
377
Steve Anton10542f22019-01-11 09:11:00 -0800378#endif // PC_JSEP_TRANSPORT_CONTROLLER_H_