blob: 9755f4c9d7f24d3bbd4e87830d0869230e92f9f9 [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"
28#include "pc/jseptransport2.h"
29#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:
47 struct Config {
48 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
49 // upon setting a local transport description that indicates an ICE
50 // restart.
51 bool redetermine_role_on_ice_restart = true;
52 rtc::SSLProtocolVersion ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
53 // |crypto_options| is used to determine if created DTLS transports
54 // negotiate GCM crypto suites or not.
55 rtc::CryptoOptions crypto_options;
56 PeerConnectionInterface::BundlePolicy bundle_policy =
57 PeerConnectionInterface::kBundlePolicyBalanced;
58 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy =
59 PeerConnectionInterface::kRtcpMuxPolicyRequire;
60 bool disable_encryption = false;
61 bool enable_external_auth = false;
62 // Used to inject the ICE/DTLS transports created externally.
63 cricket::TransportFactoryInterface* external_transport_factory = nullptr;
64 };
65
66 // The ICE related events are signaled on the |signaling_thread|.
67 // All the transport related methods are called on the |network_thread|.
68 JsepTransportController(rtc::Thread* signaling_thread,
69 rtc::Thread* network_thread,
70 cricket::PortAllocator* port_allocator,
71 Config config);
72 virtual ~JsepTransportController();
73
74 // The main method to be called; applies a description at the transport
75 // level, creating/destroying transport objects as needed and updating their
76 // properties. This includes RTP, DTLS, and ICE (but not SCTP). At least not
77 // yet? May make sense to in the future.
78 RTCError SetLocalDescription(SdpType type,
79 const cricket::SessionDescription* description);
80
81 RTCError SetRemoteDescription(SdpType type,
82 const cricket::SessionDescription* description);
83
84 // Get transports to be used for the provided |mid|. If bundling is enabled,
85 // calling GetRtpTransport for multiple MIDs may yield the same object.
86 RtpTransportInternal* GetRtpTransport(const std::string& mid) const;
87 cricket::DtlsTransportInternal* GetDtlsTransport(
88 const std::string& mid) const;
89 cricket::DtlsTransportInternal* GetRtcpDtlsTransport(
90 const std::string& mid) const;
91
92 /*********************
93 * ICE-related methods
94 ********************/
95 // This method is public to allow PeerConnection to update it from
96 // SetConfiguration.
97 void SetIceConfig(const cricket::IceConfig& config);
98 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
99 // set, offers should generate new ufrags/passwords until an ICE restart
100 // occurs.
101 void SetNeedsIceRestartFlag();
102 // Returns true if the ICE restart flag above was set, and no ICE restart has
103 // occurred yet for this transport (by applying a local description with
104 // changed ufrag/password). If the transport has been deleted as a result of
105 // bundling, returns false.
106 bool NeedsIceRestart(const std::string& mid) const;
107 // Start gathering candidates for any new transports, or transports doing an
108 // ICE restart.
109 void MaybeStartGathering();
110 RTCError AddRemoteCandidates(
111 const std::string& mid,
112 const std::vector<cricket::Candidate>& candidates);
113 RTCError RemoveRemoteCandidates(
114 const std::vector<cricket::Candidate>& candidates);
115
116 /**********************
117 * DTLS-related methods
118 *********************/
119 // Specifies the identity to use in this session.
120 // Can only be called once.
121 bool SetLocalCertificate(
122 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
123 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate(
124 const std::string& mid) const;
125 // Caller owns returned certificate. This method mainly exists for stats
126 // reporting.
127 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
128 const std::string& mid) const;
129 // Get negotiated role, if one has been negotiated.
130 rtc::Optional<rtc::SSLRole> GetDtlsRole(const std::string& mid) const;
131
132 // TODO(deadbeef): GetStats isn't const because all the way down to
133 // OpenSSLStreamAdapter, GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not
134 // const. Fix this.
135 bool GetStats(const std::string& mid, cricket::TransportStats* stats);
136 void SetMetricsObserver(webrtc::MetricsObserverInterface* metrics_observer);
137
138 // All of these signals are fired on the signaling thread.
139
140 // If any transport failed => failed,
141 // Else if all completed => completed,
142 // Else if all connected => connected,
143 // Else => connecting
144 sigslot::signal1<cricket::IceConnectionState> SignalIceConnectionState;
145
146 // If all transports done gathering => complete,
147 // Else if any are gathering => gathering,
148 // Else => new
149 sigslot::signal1<cricket::IceGatheringState> SignalIceGatheringState;
150
151 // (mid, candidates)
152 sigslot::signal2<const std::string&, const std::vector<cricket::Candidate>&>
153 SignalIceCandidatesGathered;
154
155 sigslot::signal1<const std::vector<cricket::Candidate>&>
156 SignalIceCandidatesRemoved;
157
158 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
159
160 // This will be fired when BUNDLE is enabled, the PeerConnection will handle
161 // the signal and set the RtpTransport for the BaseChannel.
162 // The first argument is the MID and the second is the new RtpTransport.
163 // Before firing this signal, the previous RtpTransport must no longer be
164 // referenced.
165 sigslot::signal2<const std::string&, RtpTransportInternal*>
166 SignalRtpTransportChanged;
167
168 // SCTP version of the signal above. PeerConnection will set a new
169 // DtlsTransport for the SctpTransport.
170 sigslot::signal2<const std::string&, cricket::DtlsTransportInternal*>
171 SignalDtlsTransportChanged;
172
173 private:
174 void OnMessage(rtc::Message* pmsg) override;
175
176 RTCError ApplyDescription_n(bool local,
177 SdpType type,
178 const cricket::SessionDescription* description);
179
180 void HandleRejectedContent(const cricket::ContentInfo& content_info);
181 void HandleBundledContent(const cricket::ContentInfo& content_info);
182
183 cricket::JsepTransportDescription CreateJsepTransportDescription(
184 cricket::ContentInfo content_info,
185 cricket::TransportInfo transport_info,
186 const std::vector<int>& encrypted_extension_ids);
187
188 rtc::Optional<std::string> bundled_mid() const {
189 rtc::Optional<std::string> bundled_mid;
190 if (bundle_group_) {
191 bundled_mid = std::move(*(bundle_group_->FirstContentName()));
192 }
193 return bundled_mid;
194 }
195
196 bool IsBundled(const std::string& mid) const {
197 return bundle_group_ && bundle_group_->HasContentName(mid);
198 }
199
200 bool ShouldUpdateBundleGroup(SdpType type,
201 const cricket::SessionDescription* description);
202
203 std::vector<int> MergeEncryptedHeaderExtensionIdsForBundle(
204 const cricket::SessionDescription* description);
205
206 std::vector<int> GetEncryptedHeaderExtensionIds(
207 const cricket::ContentInfo& content_info);
208
209 const cricket::JsepTransport2* GetJsepTransport(const std::string& mid) const;
210 cricket::JsepTransport2* GetJsepTransport(const std::string& mid);
211
212 void MaybeCreateJsepTransport(const std::string& mid,
213 const cricket::ContentInfo& content_info);
214 void MaybeDestroyJsepTransport(const std::string& mid);
215 void DestroyAllJsepTransports_n();
216
217 void SetIceRole_n(cricket::IceRole ice_role);
218
219 cricket::IceRole DetermineIceRole(
220 cricket::JsepTransport2* jsep_transport,
221 const cricket::TransportInfo& transport_info,
222 SdpType type,
223 bool local);
224
225 std::unique_ptr<cricket::DtlsTransportInternal> CreateDtlsTransport(
226 const std::string& transport_name,
227 bool rtcp);
228
229 std::unique_ptr<webrtc::RtpTransport> CreateUnencryptedRtpTransport(
230 const std::string& transport_name,
231 rtc::PacketTransportInternal* rtp_packet_transport,
232 rtc::PacketTransportInternal* rtcp_packet_transport);
233 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
234 const std::string& transport_name,
235 rtc::PacketTransportInternal* rtp_packet_transport,
236 rtc::PacketTransportInternal* rtcp_packet_transport);
237 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
238 const std::string& transport_name,
239 cricket::DtlsTransportInternal* rtp_dtls_transport,
240 cricket::DtlsTransportInternal* rtcp_dtls_transport);
241
242 // Collect all the DtlsTransports, including RTP and RTCP, from the
243 // JsepTransports. JsepTransportController can iterate all the DtlsTransports
244 // and update the aggregate states.
245 std::vector<cricket::DtlsTransportInternal*> GetDtlsTransports();
246
247 // Handlers for signals from Transport.
248 void OnTransportWritableState_n(rtc::PacketTransportInternal* transport);
249 void OnTransportReceivingState_n(rtc::PacketTransportInternal* transport);
250 void OnTransportGatheringState_n(cricket::IceTransportInternal* transport);
251 void OnTransportCandidateGathered_n(cricket::IceTransportInternal* transport,
252 const cricket::Candidate& candidate);
253 void OnTransportCandidatesRemoved(const cricket::Candidates& candidates);
254 void OnTransportCandidatesRemoved_n(cricket::IceTransportInternal* transport,
255 const cricket::Candidates& candidates);
256 void OnTransportRoleConflict_n(cricket::IceTransportInternal* transport);
257 void OnTransportStateChanged_n(cricket::IceTransportInternal* transport);
258
259 void UpdateAggregateStates_n();
260
261 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
262
263 rtc::Thread* const signaling_thread_ = nullptr;
264 rtc::Thread* const network_thread_ = nullptr;
265 cricket::PortAllocator* const port_allocator_ = nullptr;
266
267 std::map<std::string, std::unique_ptr<cricket::JsepTransport2>>
268 jsep_transports_by_mid_;
269
270 // Aggregate state for TransportChannelImpls.
271 cricket::IceConnectionState ice_connection_state_ =
272 cricket::kIceConnectionConnecting;
273 cricket::IceGatheringState ice_gathering_state_ = cricket::kIceGatheringNew;
274
275 Config config_;
276 const cricket::SessionDescription* local_desc_ = nullptr;
277 const cricket::SessionDescription* remote_desc_ = nullptr;
278 rtc::Optional<bool> initial_offerer_;
279
280 rtc::Optional<cricket::ContentGroup> bundle_group_;
281
282 cricket::IceConfig ice_config_;
283 cricket::IceRole ice_role_ = cricket::ICEROLE_CONTROLLING;
284 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
285 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
286 rtc::AsyncInvoker invoker_;
287
288 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
289
290 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransportController);
291};
292
293} // namespace webrtc
294
295#endif // PC_JSEPTRANSPORTCONTROLLER_H_