blob: bca4481624bd9fb692435c5e4e9cf8b653f21d11 [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"
Anton Sukhanov7940da02018-10-10 10:34:49 -070021#include "api/media_transport_interface.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080022#include "api/peerconnectioninterface.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070023#include "logging/rtc_event_log/rtc_event_log.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080024#include "media/sctp/sctptransportinternal.h"
25#include "p2p/base/dtlstransport.h"
26#include "p2p/base/p2ptransportchannel.h"
27#include "p2p/base/transportfactoryinterface.h"
28#include "pc/channel.h"
29#include "pc/dtlssrtptransport.h"
Zhi Huang365381f2018-04-13 16:44:34 -070030#include "pc/jseptransport.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080031#include "pc/rtptransport.h"
32#include "pc/srtptransport.h"
33#include "rtc_base/asyncinvoker.h"
34#include "rtc_base/constructormagic.h"
35#include "rtc_base/refcountedobject.h"
Oleh Prypin8f4bc412018-10-11 21:10:39 +000036#include "rtc_base/sslstreamadapter.h"
Artem Titove41c4332018-07-25 15:04:28 +020037#include "rtc_base/third_party/sigslot/sigslot.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080038
39namespace rtc {
40class Thread;
41class PacketTransportInternal;
42} // namespace rtc
43
44namespace webrtc {
45
Steve Antond25828a2018-08-31 13:06:05 -070046class JsepTransportController : public sigslot::has_slots<> {
Zhi Huange818b6e2018-02-22 15:26:27 -080047 public:
Zhi Huang365381f2018-04-13 16:44:34 -070048 // Used when the RtpTransport/DtlsTransport of the m= section is changed
49 // because the section is rejected or BUNDLE is enabled.
50 class Observer {
51 public:
52 virtual ~Observer() {}
53
54 // Returns true if media associated with |mid| was successfully set up to be
55 // demultiplexed on |rtp_transport|. Could return false if two bundled m=
56 // sections use the same SSRC, for example.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070057 virtual bool OnTransportChanged(
Zhi Huang365381f2018-04-13 16:44:34 -070058 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -070059 RtpTransportInternal* rtp_transport,
Zhi Huang365381f2018-04-13 16:44:34 -070060 cricket::DtlsTransportInternal* dtls_transport) = 0;
61 };
62
Zhi Huange818b6e2018-02-22 15:26:27 -080063 struct Config {
64 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
65 // upon setting a local transport description that indicates an ICE
66 // restart.
67 bool redetermine_role_on_ice_restart = true;
68 rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
69 // |crypto_options| is used to determine if created DTLS transports
70 // negotiate GCM crypto suites or not.
Oleh Prypin8f4bc412018-10-11 21:10:39 +000071 rtc::CryptoOptions crypto_options;
Zhi Huange818b6e2018-02-22 15:26:27 -080072 PeerConnectionInterface::BundlePolicy bundle_policy =
73 PeerConnectionInterface::kBundlePolicyBalanced;
74 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
75 PeerConnectionInterface::kRtcpMuxPolicyRequire;
76 bool disable_encryption = false;
77 bool enable_external_auth = false;
78 // Used to inject the ICE/DTLS transports created externally.
79 cricket::TransportFactoryInterface* external_transport_factory = nullptr;
Zhi Huang365381f2018-04-13 16:44:34 -070080 Observer* transport_observer = nullptr;
Zhi Huangb57e1692018-06-12 11:41:11 -070081 bool active_reset_srtp_params = false;
Qingsi Wang7685e862018-06-11 20:15:46 -070082 RtcEventLog* event_log = nullptr;
Anton Sukhanov7940da02018-10-10 10:34:49 -070083
84 // Optional media transport factory (experimental). If provided it will be
85 // used to create media_transport and will be used to send / receive
86 // audio and video frames instead of RTP. Note that currently
87 // media_transport co-exists with RTP / RTCP transports and uses the same
88 // underlying ICE transport.
89 MediaTransportFactory* media_transport_factory = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -080090 };
91
92 // The ICE related events are signaled on the |signaling_thread|.
93 // All the transport related methods are called on the |network_thread|.
94 JsepTransportController(rtc::Thread* signaling_thread,
95 rtc::Thread* network_thread,
96 cricket::PortAllocator* port_allocator,
Zach Steine20867f2018-08-02 13:20:15 -070097 AsyncResolverFactory* async_resolver_factory,
Zhi Huange818b6e2018-02-22 15:26:27 -080098 Config config);
99 virtual ~JsepTransportController();
100
101 // The main method to be called; applies a description at the transport
102 // level, creating/destroying transport objects as needed and updating their
103 // properties. This includes RTP, DTLS, and ICE (but not SCTP). At least not
104 // yet? May make sense to in the future.
105 RTCError SetLocalDescription(SdpType type,
106 const cricket::SessionDescription* description);
107
108 RTCError SetRemoteDescription(SdpType type,
109 const cricket::SessionDescription* description);
110
111 // Get transports to be used for the provided |mid|. If bundling is enabled,
112 // calling GetRtpTransport for multiple MIDs may yield the same object.
113 RtpTransportInternal* GetRtpTransport(const std::string& mid) const;
114 cricket::DtlsTransportInternal* GetDtlsTransport(
115 const std::string& mid) const;
116 cricket::DtlsTransportInternal* GetRtcpDtlsTransport(
117 const std::string& mid) const;
118
Anton Sukhanov7940da02018-10-10 10:34:49 -0700119 MediaTransportInterface* GetMediaTransport(const std::string& mid) const;
120
Zhi Huange818b6e2018-02-22 15:26:27 -0800121 /*********************
122 * ICE-related methods
123 ********************/
124 // This method is public to allow PeerConnection to update it from
125 // SetConfiguration.
126 void SetIceConfig(const cricket::IceConfig& config);
127 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
128 // set, offers should generate new ufrags/passwords until an ICE restart
129 // occurs.
130 void SetNeedsIceRestartFlag();
131 // Returns true if the ICE restart flag above was set, and no ICE restart has
132 // occurred yet for this transport (by applying a local description with
133 // changed ufrag/password). If the transport has been deleted as a result of
134 // bundling, returns false.
135 bool NeedsIceRestart(const std::string& mid) const;
136 // Start gathering candidates for any new transports, or transports doing an
137 // ICE restart.
138 void MaybeStartGathering();
139 RTCError AddRemoteCandidates(
140 const std::string& mid,
141 const std::vector<cricket::Candidate>& candidates);
142 RTCError RemoveRemoteCandidates(
143 const std::vector<cricket::Candidate>& candidates);
144
145 /**********************
146 * DTLS-related methods
147 *********************/
148 // Specifies the identity to use in this session.
149 // Can only be called once.
150 bool SetLocalCertificate(
151 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
152 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
153 const std::string& mid) const;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800154 // Caller owns returned certificate chain. This method mainly exists for
155 // stats reporting.
156 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800157 const std::string& mid) const;
158 // Get negotiated role, if one has been negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200159 absl::optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800160
161 // TODO(deadbeef): GetStats isn't const because all the way down to
162 // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
163 // const. Fix this.
164 bool GetStats(const std::string& mid, cricket::TransportStats* stats);
Zhi Huange818b6e2018-02-22 15:26:27 -0800165
Zhi Huange830e682018-03-30 10:48:35 -0700166 bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; }
Zhi Huang365381f2018-04-13 16:44:34 -0700167
Zhi Huangb57e1692018-06-12 11:41:11 -0700168 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
169
Zhi Huange818b6e2018-02-22 15:26:27 -0800170 // All of these signals are fired on the signaling thread.
171
172 // If any transport failed => failed,
173 // Else if all completed => completed,
174 // Else if all connected => connected,
175 // Else => connecting
176 sigslot::signal1<cricket::IceConnectionState> SignalIceConnectionState;
177
178 // If all transports done gathering => complete,
179 // Else if any are gathering => gathering,
180 // Else => new
181 sigslot::signal1<cricket::IceGatheringState> SignalIceGatheringState;
182
183 // (mid, candidates)
184 sigslot::signal2<const std::string&, const std::vector<cricket::Candidate>&>
185 SignalIceCandidatesGathered;
186
187 sigslot::signal1<const std::vector<cricket::Candidate>&>
188 SignalIceCandidatesRemoved;
189
190 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
191
Zhi Huange818b6e2018-02-22 15:26:27 -0800192 private:
Zhi Huange818b6e2018-02-22 15:26:27 -0800193 RTCError ApplyDescription_n(bool local,
194 SdpType type,
195 const cricket::SessionDescription* description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700196 RTCError ValidateAndMaybeUpdateBundleGroup(
197 bool local,
198 SdpType type,
199 const cricket::SessionDescription* description);
Zhi Huange830e682018-03-30 10:48:35 -0700200 RTCError ValidateContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800201
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700202 void HandleRejectedContent(const cricket::ContentInfo& content_info,
Zhi Huangd2248f82018-04-10 14:41:03 -0700203 const cricket::SessionDescription* description);
Zhi Huang365381f2018-04-13 16:44:34 -0700204 bool HandleBundledContent(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800205
Zhi Huang365381f2018-04-13 16:44:34 -0700206 bool SetTransportForMid(const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700207 cricket::JsepTransport* jsep_transport);
208 void RemoveTransportForMid(const std::string& mid);
Zhi Huangd2248f82018-04-10 14:41:03 -0700209
Zhi Huange818b6e2018-02-22 15:26:27 -0800210 cricket::JsepTransportDescription CreateJsepTransportDescription(
211 cricket::ContentInfo content_info,
212 cricket::TransportInfo transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700213 const std::vector<int>& encrypted_extension_ids,
214 int rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800215
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200216 absl::optional<std::string> bundled_mid() const {
217 absl::optional<std::string> bundled_mid;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700218 if (bundle_group_ && bundle_group_->FirstContentName()) {
219 bundled_mid = *(bundle_group_->FirstContentName());
Zhi Huange818b6e2018-02-22 15:26:27 -0800220 }
221 return bundled_mid;
222 }
223
224 bool IsBundled(const std::string& mid) const {
225 return bundle_group_ && bundle_group_->HasContentName(mid);
226 }
227
228 bool ShouldUpdateBundleGroup(SdpType type,
229 const cricket::SessionDescription* description);
230
231 std::vector<int> MergeEncryptedHeaderExtensionIdsForBundle(
232 const cricket::SessionDescription* description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800233 std::vector<int> GetEncryptedHeaderExtensionIds(
234 const cricket::ContentInfo& content_info);
235
Zhi Huange830e682018-03-30 10:48:35 -0700236 int GetRtpAbsSendTimeHeaderExtensionId(
237 const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800238
Zhi Huange830e682018-03-30 10:48:35 -0700239 // This method takes the BUNDLE group into account. If the JsepTransport is
240 // destroyed because of BUNDLE, it would return the transport which other
241 // transports are bundled on (In current implementation, it is the first
242 // content in the BUNDLE group).
Zhi Huang365381f2018-04-13 16:44:34 -0700243 const cricket::JsepTransport* GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700244 const std::string& mid) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700245 cricket::JsepTransport* GetJsepTransportForMid(const std::string& mid);
Zhi Huange830e682018-03-30 10:48:35 -0700246
247 // Get the JsepTransport without considering the BUNDLE group. Return nullptr
248 // if the JsepTransport is destroyed.
Zhi Huang365381f2018-04-13 16:44:34 -0700249 const cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700250 const std::string& transport_name) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700251 cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700252 const std::string& transport_name);
253
Anton Sukhanov7940da02018-10-10 10:34:49 -0700254 // Creates jsep transport. Noop if transport is already created.
255 // Transport is created either during SetLocalDescription (|local| == true) or
256 // during SetRemoteDescription (|local| == false). Passing |local| helps to
257 // differentiate initiator (caller) from answerer (callee).
258 RTCError MaybeCreateJsepTransport(bool local,
259 const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800260 void MaybeDestroyJsepTransport(const std::string& mid);
261 void DestroyAllJsepTransports_n();
262
263 void SetIceRole_n(cricket::IceRole ice_role);
264
265 cricket::IceRole DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -0700266 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -0800267 const cricket::TransportInfo& transport_info,
268 SdpType type,
269 bool local);
270
271 std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
272 const std::string& transport_name,
273 bool rtcp);
274
275 std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
276 const std::string& transport_name,
277 rtc::PacketTransportInternal* rtp_packet_transport,
278 rtc::PacketTransportInternal* rtcp_packet_transport);
279 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
280 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700281 cricket::DtlsTransportInternal* rtp_dtls_transport,
282 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800283 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
284 const std::string& transport_name,
285 cricket::DtlsTransportInternal* rtp_dtls_transport,
286 cricket::DtlsTransportInternal* rtcp_dtls_transport);
287
288 // Collect all the DtlsTransports, including RTP and RTCP, from the
289 // JsepTransports. JsepTransportController can iterate all the DtlsTransports
290 // and update the aggregate states.
291 std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
292
293 // Handlers for signals from Transport.
294 void OnTransportWritableState_n(rtc::PacketTransportInternal* transport);
295 void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport);
296 void OnTransportGatheringState_n(cricket::IceTransportInternal* transport);
297 void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
298 const cricket::Candidate& candidate);
Zhi Huange818b6e2018-02-22 15:26:27 -0800299 void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
300 const cricket::Candidates& candidates);
301 void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport);
302 void OnTransportStateChanged_n(cricket::IceTransportInternal* transport);
303
304 void UpdateAggregateStates_n();
305
306 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
307
308 rtc::Thread* const signaling_thread_ = nullptr;
309 rtc::Thread* const network_thread_ = nullptr;
310 cricket::PortAllocator* const port_allocator_ = nullptr;
Zach Steine20867f2018-08-02 13:20:15 -0700311 AsyncResolverFactory* const async_resolver_factory_ = nullptr;
Zhi Huange818b6e2018-02-22 15:26:27 -0800312
Zhi Huang365381f2018-04-13 16:44:34 -0700313 std::map<std::string, std::unique_ptr<cricket::JsepTransport>>
Zhi Huange830e682018-03-30 10:48:35 -0700314 jsep_transports_by_name_;
Zhi Huangd2248f82018-04-10 14:41:03 -0700315 // This keeps track of the mapping between media section
Zhi Huang365381f2018-04-13 16:44:34 -0700316 // (BaseChannel/SctpTransport) and the JsepTransport underneath.
317 std::map<std::string, cricket::JsepTransport*> mid_to_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800318
Zhi Huange830e682018-03-30 10:48:35 -0700319 // Aggregate state for Transports.
Zhi Huange818b6e2018-02-22 15:26:27 -0800320 cricket::IceConnectionState ice_connection_state_ =
321 cricket::kIceConnectionConnecting;
322 cricket::IceGatheringState ice_gathering_state_ = cricket::kIceGatheringNew;
323
324 Config config_;
325 const cricket::SessionDescription* local_desc_ = nullptr;
326 const cricket::SessionDescription* remote_desc_ = nullptr;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200327 absl::optional<bool> initial_offerer_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800328
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200329 absl::optional<cricket::ContentGroup> bundle_group_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800330
331 cricket::IceConfig ice_config_;
332 cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
333 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
334 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
335 rtc::AsyncInvoker invoker_;
336
Zhi Huange818b6e2018-02-22 15:26:27 -0800337 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransportController);
338};
339
340} // namespace webrtc
341
342#endif // PC_JSEPTRANSPORTCONTROLLER_H_