blob: d9340cf357d0e1345db635893e2fb27a34451480 [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
11#ifndef PC_JSEPTRANSPORTCONTROLLER_H_
12#define PC_JSEPTRANSPORTCONTROLLER_H_
13
14#include <map>
15#include <memory>
16#include <string>
17#include <utility>
18#include <vector>
19
20#include "api/candidate.h"
21#include "api/peerconnectioninterface.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070022#include "logging/rtc_event_log/rtc_event_log.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080023#include "media/sctp/sctptransportinternal.h"
24#include "p2p/base/dtlstransport.h"
25#include "p2p/base/p2ptransportchannel.h"
26#include "p2p/base/transportfactoryinterface.h"
27#include "pc/channel.h"
28#include "pc/dtlssrtptransport.h"
Zhi Huang365381f2018-04-13 16:44:34 -070029#include "pc/jseptransport.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080030#include "pc/rtptransport.h"
31#include "pc/srtptransport.h"
32#include "rtc_base/asyncinvoker.h"
33#include "rtc_base/constructormagic.h"
34#include "rtc_base/refcountedobject.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080035#include "rtc_base/sslstreamadapter.h"
Artem Titove41c4332018-07-25 15:04:28 +020036#include "rtc_base/third_party/sigslot/sigslot.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080037
38namespace rtc {
39class Thread;
40class PacketTransportInternal;
41} // namespace rtc
42
43namespace webrtc {
44
Steve Antond25828a2018-08-31 13:06:05 -070045class JsepTransportController : public sigslot::has_slots<> {
Zhi Huange818b6e2018-02-22 15:26:27 -080046 public:
Zhi Huang365381f2018-04-13 16:44:34 -070047 // Used when the RtpTransport/DtlsTransport of the m= section is changed
48 // because the section is rejected or BUNDLE is enabled.
49 class Observer {
50 public:
51 virtual ~Observer() {}
52
53 // Returns true if media associated with |mid| was successfully set up to be
54 // demultiplexed on |rtp_transport|. Could return false if two bundled m=
55 // sections use the same SSRC, for example.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070056 virtual bool OnTransportChanged(
Zhi Huang365381f2018-04-13 16:44:34 -070057 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070058 RtpTransportInternal* rtp_transport,
Zhi Huang365381f2018-04-13 16:44:34 -070059 cricket::DtlsTransportInternal* dtls_transport) = 0;
60 };
61
Zhi Huange818b6e2018-02-22 15:26:27 -080062 struct Config {
63 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
64 // upon setting a local transport description that indicates an ICE
65 // restart.
66 bool redetermine_role_on_ice_restart = true;
67 rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
68 // |crypto_options| is used to determine if created DTLS transports
69 // negotiate GCM crypto suites or not.
70 rtc::CryptoOptions crypto_options;
71 PeerConnectionInterface::BundlePolicy bundle_policy =
72 PeerConnectionInterface::kBundlePolicyBalanced;
73 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
74 PeerConnectionInterface::kRtcpMuxPolicyRequire;
75 bool disable_encryption = false;
76 bool enable_external_auth = false;
77 // Used to inject the ICE/DTLS transports created externally.
78 cricket::TransportFactoryInterface* external_transport_factory = nullptr;
Zhi Huang365381f2018-04-13 16:44:34 -070079 Observer* transport_observer = nullptr;
Zhi Huangb57e1692018-06-12 11:41:11 -070080 bool active_reset_srtp_params = false;
Qingsi Wang7685e862018-06-11 20:15:46 -070081 RtcEventLog* event_log = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -080082 };
83
84 // The ICE related events are signaled on the |signaling_thread|.
85 // All the transport related methods are called on the |network_thread|.
86 JsepTransportController(rtc::Thread* signaling_thread,
87 rtc::Thread* network_thread,
88 cricket::PortAllocator* port_allocator,
Zach Steine20867f2018-08-02 13:20:15 -070089 AsyncResolverFactory* async_resolver_factory,
Zhi Huange818b6e2018-02-22 15:26:27 -080090 Config config);
91 virtual ~JsepTransportController();
92
93 // The main method to be called; applies a description at the transport
94 // level, creating/destroying transport objects as needed and updating their
95 // properties. This includes RTP, DTLS, and ICE (but not SCTP). At least not
96 // yet? May make sense to in the future.
97 RTCError SetLocalDescription(SdpType type,
98 const cricket::SessionDescription* description);
99
100 RTCError SetRemoteDescription(SdpType type,
101 const cricket::SessionDescription* description);
102
103 // Get transports to be used for the provided |mid|. If bundling is enabled,
104 // calling GetRtpTransport for multiple MIDs may yield the same object.
105 RtpTransportInternal* GetRtpTransport(const std::string& mid) const;
106 cricket::DtlsTransportInternal* GetDtlsTransport(
107 const std::string& mid) const;
108 cricket::DtlsTransportInternal* GetRtcpDtlsTransport(
109 const std::string& mid) const;
110
111 /*********************
112 * ICE-related methods
113 ********************/
114 // This method is public to allow PeerConnection to update it from
115 // SetConfiguration.
116 void SetIceConfig(const cricket::IceConfig& config);
117 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
118 // set, offers should generate new ufrags/passwords until an ICE restart
119 // occurs.
120 void SetNeedsIceRestartFlag();
121 // Returns true if the ICE restart flag above was set, and no ICE restart has
122 // occurred yet for this transport (by applying a local description with
123 // changed ufrag/password). If the transport has been deleted as a result of
124 // bundling, returns false.
125 bool NeedsIceRestart(const std::string& mid) const;
126 // Start gathering candidates for any new transports, or transports doing an
127 // ICE restart.
128 void MaybeStartGathering();
129 RTCError AddRemoteCandidates(
130 const std::string& mid,
131 const std::vector<cricket::Candidate>& candidates);
132 RTCError RemoveRemoteCandidates(
133 const std::vector<cricket::Candidate>& candidates);
134
135 /**********************
136 * DTLS-related methods
137 *********************/
138 // Specifies the identity to use in this session.
139 // Can only be called once.
140 bool SetLocalCertificate(
141 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
142 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
143 const std::string& mid) const;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800144 // Caller owns returned certificate chain. This method mainly exists for
145 // stats reporting.
146 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800147 const std::string& mid) const;
148 // Get negotiated role, if one has been negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200149 absl::optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800150
151 // TODO(deadbeef): GetStats isn't const because all the way down to
152 // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
153 // const. Fix this.
154 bool GetStats(const std::string& mid, cricket::TransportStats* stats);
Zhi Huange818b6e2018-02-22 15:26:27 -0800155
Zhi Huange830e682018-03-30 10:48:35 -0700156 bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; }
Zhi Huang365381f2018-04-13 16:44:34 -0700157
Zhi Huangb57e1692018-06-12 11:41:11 -0700158 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
159
Zhi Huange818b6e2018-02-22 15:26:27 -0800160 // All of these signals are fired on the signaling thread.
161
162 // If any transport failed => failed,
163 // Else if all completed => completed,
164 // Else if all connected => connected,
165 // Else => connecting
166 sigslot::signal1<cricket::IceConnectionState> SignalIceConnectionState;
167
168 // If all transports done gathering => complete,
169 // Else if any are gathering => gathering,
170 // Else => new
171 sigslot::signal1<cricket::IceGatheringState> SignalIceGatheringState;
172
173 // (mid, candidates)
174 sigslot::signal2<const std::string&, const std::vector<cricket::Candidate>&>
175 SignalIceCandidatesGathered;
176
177 sigslot::signal1<const std::vector<cricket::Candidate>&>
178 SignalIceCandidatesRemoved;
179
180 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
181
Zhi Huange818b6e2018-02-22 15:26:27 -0800182 private:
Zhi Huange818b6e2018-02-22 15:26:27 -0800183 RTCError ApplyDescription_n(bool local,
184 SdpType type,
185 const cricket::SessionDescription* description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700186 RTCError ValidateAndMaybeUpdateBundleGroup(
187 bool local,
188 SdpType type,
189 const cricket::SessionDescription* description);
Zhi Huange830e682018-03-30 10:48:35 -0700190 RTCError ValidateContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800191
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700192 void HandleRejectedContent(const cricket::ContentInfo& content_info,
Zhi Huangd2248f82018-04-10 14:41:03 -0700193 const cricket::SessionDescription* description);
Zhi Huang365381f2018-04-13 16:44:34 -0700194 bool HandleBundledContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800195
Zhi Huang365381f2018-04-13 16:44:34 -0700196 bool SetTransportForMid(const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700197 cricket::JsepTransport* jsep_transport);
198 void RemoveTransportForMid(const std::string& mid);
Zhi Huangd2248f82018-04-10 14:41:03 -0700199
Zhi Huange818b6e2018-02-22 15:26:27 -0800200 cricket::JsepTransportDescription CreateJsepTransportDescription(
201 cricket::ContentInfo content_info,
202 cricket::TransportInfo transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700203 const std::vector<int>& encrypted_extension_ids,
204 int rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800205
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200206 absl::optional<std::string> bundled_mid() const {
207 absl::optional<std::string> bundled_mid;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700208 if (bundle_group_ && bundle_group_->FirstContentName()) {
209 bundled_mid = *(bundle_group_->FirstContentName());
Zhi Huange818b6e2018-02-22 15:26:27 -0800210 }
211 return bundled_mid;
212 }
213
214 bool IsBundled(const std::string& mid) const {
215 return bundle_group_ && bundle_group_->HasContentName(mid);
216 }
217
218 bool ShouldUpdateBundleGroup(SdpType type,
219 const cricket::SessionDescription* description);
220
221 std::vector<int> MergeEncryptedHeaderExtensionIdsForBundle(
222 const cricket::SessionDescription* description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800223 std::vector<int> GetEncryptedHeaderExtensionIds(
224 const cricket::ContentInfo& content_info);
225
Zhi Huange830e682018-03-30 10:48:35 -0700226 int GetRtpAbsSendTimeHeaderExtensionId(
227 const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800228
Zhi Huange830e682018-03-30 10:48:35 -0700229 // This method takes the BUNDLE group into account. If the JsepTransport is
230 // destroyed because of BUNDLE, it would return the transport which other
231 // transports are bundled on (In current implementation, it is the first
232 // content in the BUNDLE group).
Zhi Huang365381f2018-04-13 16:44:34 -0700233 const cricket::JsepTransport* GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700234 const std::string& mid) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700235 cricket::JsepTransport* GetJsepTransportForMid(const std::string& mid);
Zhi Huange830e682018-03-30 10:48:35 -0700236
237 // Get the JsepTransport without considering the BUNDLE group. Return nullptr
238 // if the JsepTransport is destroyed.
Zhi Huang365381f2018-04-13 16:44:34 -0700239 const cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700240 const std::string& transport_name) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700241 cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700242 const std::string& transport_name);
243
Zhi Huangd2248f82018-04-10 14:41:03 -0700244 RTCError MaybeCreateJsepTransport(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800245 void MaybeDestroyJsepTransport(const std::string& mid);
246 void DestroyAllJsepTransports_n();
247
248 void SetIceRole_n(cricket::IceRole ice_role);
249
250 cricket::IceRole DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -0700251 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -0800252 const cricket::TransportInfo& transport_info,
253 SdpType type,
254 bool local);
255
256 std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
257 const std::string& transport_name,
258 bool rtcp);
259
260 std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
261 const std::string& transport_name,
262 rtc::PacketTransportInternal* rtp_packet_transport,
263 rtc::PacketTransportInternal* rtcp_packet_transport);
264 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
265 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700266 cricket::DtlsTransportInternal* rtp_dtls_transport,
267 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800268 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
269 const std::string& transport_name,
270 cricket::DtlsTransportInternal* rtp_dtls_transport,
271 cricket::DtlsTransportInternal* rtcp_dtls_transport);
272
273 // Collect all the DtlsTransports, including RTP and RTCP, from the
274 // JsepTransports. JsepTransportController can iterate all the DtlsTransports
275 // and update the aggregate states.
276 std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
277
278 // Handlers for signals from Transport.
279 void OnTransportWritableState_n(rtc::PacketTransportInternal* transport);
280 void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport);
281 void OnTransportGatheringState_n(cricket::IceTransportInternal* transport);
282 void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
283 const cricket::Candidate& candidate);
Zhi Huange818b6e2018-02-22 15:26:27 -0800284 void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
285 const cricket::Candidates& candidates);
286 void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport);
287 void OnTransportStateChanged_n(cricket::IceTransportInternal* transport);
288
289 void UpdateAggregateStates_n();
290
291 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
292
293 rtc::Thread* const signaling_thread_ = nullptr;
294 rtc::Thread* const network_thread_ = nullptr;
295 cricket::PortAllocator* const port_allocator_ = nullptr;
Zach Steine20867f2018-08-02 13:20:15 -0700296 AsyncResolverFactory* const async_resolver_factory_ = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800297
Zhi Huang365381f2018-04-13 16:44:34 -0700298 std::map<std::string, std::unique_ptr<cricket::JsepTransport>>
Zhi Huange830e682018-03-30 10:48:35 -0700299 jsep_transports_by_name_;
Zhi Huangd2248f82018-04-10 14:41:03 -0700300 // This keeps track of the mapping between media section
Zhi Huang365381f2018-04-13 16:44:34 -0700301 // (BaseChannel/SctpTransport) and the JsepTransport underneath.
302 std::map<std::string, cricket::JsepTransport*> mid_to_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800303
Zhi Huange830e682018-03-30 10:48:35 -0700304 // Aggregate state for Transports.
Zhi Huange818b6e2018-02-22 15:26:27 -0800305 cricket::IceConnectionState ice_connection_state_ =
306 cricket::kIceConnectionConnecting;
307 cricket::IceGatheringState ice_gathering_state_ = cricket::kIceGatheringNew;
308
309 Config config_;
310 const cricket::SessionDescription* local_desc_ = nullptr;
311 const cricket::SessionDescription* remote_desc_ = nullptr;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200312 absl::optional<bool> initial_offerer_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800313
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200314 absl::optional<cricket::ContentGroup> bundle_group_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800315
316 cricket::IceConfig ice_config_;
317 cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
318 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
319 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
320 rtc::AsyncInvoker invoker_;
321
Zhi Huange818b6e2018-02-22 15:26:27 -0800322 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransportController);
323};
324
325} // namespace webrtc
326
327#endif // PC_JSEPTRANSPORTCONTROLLER_H_