blob: c3c7919f506044eb31c6ed27028f41d682c69d1c [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"
22#include "media/sctp/sctptransportinternal.h"
23#include "p2p/base/dtlstransport.h"
24#include "p2p/base/p2ptransportchannel.h"
25#include "p2p/base/transportfactoryinterface.h"
26#include "pc/channel.h"
27#include "pc/dtlssrtptransport.h"
Zhi Huang365381f2018-04-13 16:44:34 -070028#include "pc/jseptransport.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080029#include "pc/rtptransport.h"
30#include "pc/srtptransport.h"
31#include "rtc_base/asyncinvoker.h"
32#include "rtc_base/constructormagic.h"
33#include "rtc_base/refcountedobject.h"
34#include "rtc_base/sigslot.h"
35#include "rtc_base/sslstreamadapter.h"
36
37namespace rtc {
38class Thread;
39class PacketTransportInternal;
40} // namespace rtc
41
42namespace webrtc {
43
44class JsepTransportController : public sigslot::has_slots<>,
45 public rtc::MessageHandler {
46 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.
56 virtual bool OnRtpTransportChanged(const std::string& mid,
57 RtpTransportInternal* rtp_transport) = 0;
58
59 virtual void OnDtlsTransportChanged(
60 const std::string& mid,
61 cricket::DtlsTransportInternal* dtls_transport) = 0;
62 };
63
Zhi Huange818b6e2018-02-22 15:26:27 -080064 struct Config {
65 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
66 // upon setting a local transport description that indicates an ICE
67 // restart.
68 bool redetermine_role_on_ice_restart = true;
69 rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
70 // |crypto_options| is used to determine if created DTLS transports
71 // negotiate GCM crypto suites or not.
72 rtc::CryptoOptions crypto_options;
73 PeerConnectionInterface::BundlePolicy bundle_policy =
74 PeerConnectionInterface::kBundlePolicyBalanced;
75 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
76 PeerConnectionInterface::kRtcpMuxPolicyRequire;
77 bool disable_encryption = false;
78 bool enable_external_auth = false;
79 // Used to inject the ICE/DTLS transports created externally.
80 cricket::TransportFactoryInterface* external_transport_factory = nullptr;
Zhi Huang365381f2018-04-13 16:44:34 -070081 Observer* transport_observer = 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,
89 Config config);
90 virtual ~JsepTransportController();
91
92 // The main method to be called; applies a description at the transport
93 // level, creating/destroying transport objects as needed and updating their
94 // properties. This includes RTP, DTLS, and ICE (but not SCTP). At least not
95 // yet? May make sense to in the future.
96 RTCError SetLocalDescription(SdpType type,
97 const cricket::SessionDescription* description);
98
99 RTCError SetRemoteDescription(SdpType type,
100 const cricket::SessionDescription* description);
101
102 // Get transports to be used for the provided |mid|. If bundling is enabled,
103 // calling GetRtpTransport for multiple MIDs may yield the same object.
104 RtpTransportInternal* GetRtpTransport(const std::string& mid) const;
105 cricket::DtlsTransportInternal* GetDtlsTransport(
106 const std::string& mid) const;
107 cricket::DtlsTransportInternal* GetRtcpDtlsTransport(
108 const std::string& mid) const;
109
110 /*********************
111 * ICE-related methods
112 ********************/
113 // This method is public to allow PeerConnection to update it from
114 // SetConfiguration.
115 void SetIceConfig(const cricket::IceConfig& config);
116 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
117 // set, offers should generate new ufrags/passwords until an ICE restart
118 // occurs.
119 void SetNeedsIceRestartFlag();
120 // Returns true if the ICE restart flag above was set, and no ICE restart has
121 // occurred yet for this transport (by applying a local description with
122 // changed ufrag/password). If the transport has been deleted as a result of
123 // bundling, returns false.
124 bool NeedsIceRestart(const std::string& mid) const;
125 // Start gathering candidates for any new transports, or transports doing an
126 // ICE restart.
127 void MaybeStartGathering();
128 RTCError AddRemoteCandidates(
129 const std::string& mid,
130 const std::vector<cricket::Candidate>& candidates);
131 RTCError RemoveRemoteCandidates(
132 const std::vector<cricket::Candidate>& candidates);
133
134 /**********************
135 * DTLS-related methods
136 *********************/
137 // Specifies the identity to use in this session.
138 // Can only be called once.
139 bool SetLocalCertificate(
140 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
141 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
142 const std::string& mid) const;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800143 // Caller owns returned certificate chain. This method mainly exists for
144 // stats reporting.
145 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800146 const std::string& mid) const;
147 // Get negotiated role, if one has been negotiated.
148 rtc::Optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
149
150 // TODO(deadbeef): GetStats isn't const because all the way down to
151 // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
152 // const. Fix this.
153 bool GetStats(const std::string& mid, cricket::TransportStats* stats);
154 void SetMetricsObserver(webrtc::MetricsObserverInterface* metrics_observer);
155
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 Huange818b6e2018-02-22 15:26:27 -0800158 // All of these signals are fired on the signaling thread.
159
160 // If any transport failed => failed,
161 // Else if all completed => completed,
162 // Else if all connected => connected,
163 // Else => connecting
164 sigslot::signal1<cricket::IceConnectionState> SignalIceConnectionState;
165
166 // If all transports done gathering => complete,
167 // Else if any are gathering => gathering,
168 // Else => new
169 sigslot::signal1<cricket::IceGatheringState> SignalIceGatheringState;
170
171 // (mid, candidates)
172 sigslot::signal2<const std::string&, const std::vector<cricket::Candidate>&>
173 SignalIceCandidatesGathered;
174
175 sigslot::signal1<const std::vector<cricket::Candidate>&>
176 SignalIceCandidatesRemoved;
177
178 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
179
Zhi Huange818b6e2018-02-22 15:26:27 -0800180 private:
181 void OnMessage(rtc::Message* pmsg) override;
182
183 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
Zhi Huang365381f2018-04-13 16:44:34 -0700192 bool 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,
197 cricket::JsepTransport* jsep_transport,
Zhi Huangd2248f82018-04-10 14:41:03 -0700198 cricket::MediaProtocolType protocol_type);
Zhi Huang365381f2018-04-13 16:44:34 -0700199 bool RemoveTransportForMid(const std::string& mid,
Zhi Huangd2248f82018-04-10 14:41:03 -0700200 cricket::MediaProtocolType protocol_type);
201
Zhi Huange818b6e2018-02-22 15:26:27 -0800202 cricket::JsepTransportDescription CreateJsepTransportDescription(
203 cricket::ContentInfo content_info,
204 cricket::TransportInfo transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700205 const std::vector<int>& encrypted_extension_ids,
206 int rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800207
208 rtc::Optional<std::string> bundled_mid() const {
209 rtc::Optional<std::string> bundled_mid;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700210 if (bundle_group_ && bundle_group_->FirstContentName()) {
211 bundled_mid = *(bundle_group_->FirstContentName());
Zhi Huange818b6e2018-02-22 15:26:27 -0800212 }
213 return bundled_mid;
214 }
215
216 bool IsBundled(const std::string& mid) const {
217 return bundle_group_ && bundle_group_->HasContentName(mid);
218 }
219
220 bool ShouldUpdateBundleGroup(SdpType type,
221 const cricket::SessionDescription* description);
222
223 std::vector<int> MergeEncryptedHeaderExtensionIdsForBundle(
224 const cricket::SessionDescription* description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800225 std::vector<int> GetEncryptedHeaderExtensionIds(
226 const cricket::ContentInfo& content_info);
227
Zhi Huange830e682018-03-30 10:48:35 -0700228 int GetRtpAbsSendTimeHeaderExtensionId(
229 const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800230
Zhi Huange830e682018-03-30 10:48:35 -0700231 // This method takes the BUNDLE group into account. If the JsepTransport is
232 // destroyed because of BUNDLE, it would return the transport which other
233 // transports are bundled on (In current implementation, it is the first
234 // content in the BUNDLE group).
Zhi Huang365381f2018-04-13 16:44:34 -0700235 const cricket::JsepTransport* GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700236 const std::string& mid) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700237 cricket::JsepTransport* GetJsepTransportForMid(const std::string& mid);
Zhi Huange830e682018-03-30 10:48:35 -0700238
239 // Get the JsepTransport without considering the BUNDLE group. Return nullptr
240 // if the JsepTransport is destroyed.
Zhi Huang365381f2018-04-13 16:44:34 -0700241 const cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700242 const std::string& transport_name) const;
Zhi Huang365381f2018-04-13 16:44:34 -0700243 cricket::JsepTransport* GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700244 const std::string& transport_name);
245
Zhi Huangd2248f82018-04-10 14:41:03 -0700246 RTCError MaybeCreateJsepTransport(const cricket::ContentInfo& content_info);
Zhi Huange818b6e2018-02-22 15:26:27 -0800247 void MaybeDestroyJsepTransport(const std::string& mid);
248 void DestroyAllJsepTransports_n();
249
250 void SetIceRole_n(cricket::IceRole ice_role);
251
252 cricket::IceRole DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -0700253 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -0800254 const cricket::TransportInfo& transport_info,
255 SdpType type,
256 bool local);
257
258 std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
259 const std::string& transport_name,
260 bool rtcp);
261
262 std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
263 const std::string& transport_name,
264 rtc::PacketTransportInternal* rtp_packet_transport,
265 rtc::PacketTransportInternal* rtcp_packet_transport);
266 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
267 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700268 cricket::DtlsTransportInternal* rtp_dtls_transport,
269 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800270 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
271 const std::string& transport_name,
272 cricket::DtlsTransportInternal* rtp_dtls_transport,
273 cricket::DtlsTransportInternal* rtcp_dtls_transport);
274
275 // Collect all the DtlsTransports, including RTP and RTCP, from the
276 // JsepTransports. JsepTransportController can iterate all the DtlsTransports
277 // and update the aggregate states.
278 std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
279
280 // Handlers for signals from Transport.
281 void OnTransportWritableState_n(rtc::PacketTransportInternal* transport);
282 void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport);
283 void OnTransportGatheringState_n(cricket::IceTransportInternal* transport);
284 void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
285 const cricket::Candidate& candidate);
286 void OnTransportCandidatesRemoved(const cricket::Candidates& candidates);
287 void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
288 const cricket::Candidates& candidates);
289 void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport);
290 void OnTransportStateChanged_n(cricket::IceTransportInternal* transport);
291
292 void UpdateAggregateStates_n();
293
294 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
295
296 rtc::Thread* const signaling_thread_ = nullptr;
297 rtc::Thread* const network_thread_ = nullptr;
298 cricket::PortAllocator* const port_allocator_ = nullptr;
299
Zhi Huang365381f2018-04-13 16:44:34 -0700300 std::map<std::string, std::unique_ptr<cricket::JsepTransport>>
Zhi Huange830e682018-03-30 10:48:35 -0700301 jsep_transports_by_name_;
Zhi Huangd2248f82018-04-10 14:41:03 -0700302 // This keeps track of the mapping between media section
Zhi Huang365381f2018-04-13 16:44:34 -0700303 // (BaseChannel/SctpTransport) and the JsepTransport underneath.
304 std::map<std::string, cricket::JsepTransport*> mid_to_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800305
Zhi Huange830e682018-03-30 10:48:35 -0700306 // Aggregate state for Transports.
Zhi Huange818b6e2018-02-22 15:26:27 -0800307 cricket::IceConnectionState ice_connection_state_ =
308 cricket::kIceConnectionConnecting;
309 cricket::IceGatheringState ice_gathering_state_ = cricket::kIceGatheringNew;
310
311 Config config_;
312 const cricket::SessionDescription* local_desc_ = nullptr;
313 const cricket::SessionDescription* remote_desc_ = nullptr;
314 rtc::Optional<bool> initial_offerer_;
315
316 rtc::Optional<cricket::ContentGroup> bundle_group_;
317
318 cricket::IceConfig ice_config_;
319 cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
320 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
321 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
322 rtc::AsyncInvoker invoker_;
323
324 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
325
326 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransportController);
327};
328
329} // namespace webrtc
330
331#endif // PC_JSEPTRANSPORTCONTROLLER_H_