blob: 091e70e993f240870154022a90f2d88ffa1beae6 [file] [log] [blame]
deadbeefcbecd352015-09-23 11:50:27 -07001/*
2 * Copyright 2015 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 WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_
12#define WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_
13
14#include <map>
jbauch555604a2016-04-26 03:13:22 -070015#include <memory>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <string>
17#include <vector>
18
Honghai Zhang7fb69db2016-03-14 11:59:18 -070019#include "webrtc/base/asyncinvoker.h"
deadbeef62802a12016-12-13 16:38:36 -080020#include "webrtc/base/constructormagic.h"
21#include "webrtc/base/refcountedobject.h"
deadbeefcbecd352015-09-23 11:50:27 -070022#include "webrtc/base/sigslot.h"
23#include "webrtc/base/sslstreamadapter.h"
24#include "webrtc/p2p/base/candidate.h"
deadbeef49f34fd2016-12-06 16:22:06 -080025#include "webrtc/p2p/base/dtlstransportchannel.h"
26#include "webrtc/p2p/base/jseptransport.h"
27#include "webrtc/p2p/base/p2ptransportchannel.h"
deadbeefcbecd352015-09-23 11:50:27 -070028
29namespace rtc {
30class Thread;
deadbeef5bd5ca32017-02-10 11:31:50 -080031class PacketTransportInternal;
deadbeefcbecd352015-09-23 11:50:27 -070032}
Honghai Zhangd93f50c2016-10-05 11:47:22 -070033namespace webrtc {
34class MetricsObserverInterface;
35}
deadbeefcbecd352015-09-23 11:50:27 -070036
37namespace cricket {
38
39class TransportController : public sigslot::has_slots<>,
40 public rtc::MessageHandler {
41 public:
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070042 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
43 // upon setting a local transport description that indicates an ICE restart.
44 // For the constructor that doesn't take this parameter, it defaults to true.
45 TransportController(rtc::Thread* signaling_thread,
46 rtc::Thread* network_thread,
47 PortAllocator* port_allocator,
48 bool redetermine_role_on_ice_restart);
49
deadbeefcbecd352015-09-23 11:50:27 -070050 TransportController(rtc::Thread* signaling_thread,
Danil Chapovalov7f216b72016-05-12 09:20:31 +020051 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -070052 PortAllocator* port_allocator);
53
54 virtual ~TransportController();
55
56 rtc::Thread* signaling_thread() const { return signaling_thread_; }
Danil Chapovalov7f216b72016-05-12 09:20:31 +020057 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070058
59 PortAllocator* port_allocator() const { return port_allocator_; }
60
61 // Can only be set before transports are created.
62 // TODO(deadbeef): Make this an argument to the constructor once BaseSession
63 // and WebRtcSession are combined
64 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version);
65
honghaiz1f429e32015-09-28 07:57:34 -070066 void SetIceConfig(const IceConfig& config);
deadbeefcbecd352015-09-23 11:50:27 -070067 void SetIceRole(IceRole ice_role);
68
deadbeefd1a38b52016-12-10 13:15:33 -080069 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
70 // set, offers should generate new ufrags/passwords until an ICE restart
71 // occurs.
72 void SetNeedsIceRestartFlag();
73 // Returns true if the ICE restart flag above was set, and no ICE restart has
74 // occurred yet for this transport (by applying a local description with
75 // changed ufrag/password). If the transport has been deleted as a result of
76 // bundling, returns false.
77 bool NeedsIceRestart(const std::string& transport_name) const;
78
deadbeef49f34fd2016-12-06 16:22:06 -080079 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role) const;
deadbeefcbecd352015-09-23 11:50:27 -070080
81 // Specifies the identity to use in this session.
82 // Can only be called once.
83 bool SetLocalCertificate(
84 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
85 bool GetLocalCertificate(
86 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -080087 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const;
88 // Caller owns returned certificate. This method mainly exists for stats
89 // reporting.
jbauch555604a2016-04-26 03:13:22 -070090 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
deadbeef49f34fd2016-12-06 16:22:06 -080091 const std::string& transport_name) const;
deadbeefcbecd352015-09-23 11:50:27 -070092 bool SetLocalTransportDescription(const std::string& transport_name,
93 const TransportDescription& tdesc,
94 ContentAction action,
95 std::string* err);
96 bool SetRemoteTransportDescription(const std::string& transport_name,
97 const TransportDescription& tdesc,
98 ContentAction action,
99 std::string* err);
100 // Start gathering candidates for any new transports, or transports doing an
101 // ICE restart.
102 void MaybeStartGathering();
103 bool AddRemoteCandidates(const std::string& transport_name,
104 const Candidates& candidates,
105 std::string* err);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700106 bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err);
deadbeef49f34fd2016-12-06 16:22:06 -0800107 bool ReadyForRemoteCandidates(const std::string& transport_name) const;
108 // TODO(deadbeef): GetStats isn't const because all the way down to
109 // OpenSSLStreamAdapter,
110 // GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not const. Fix this.
deadbeefcbecd352015-09-23 11:50:27 -0700111 bool GetStats(const std::string& transport_name, TransportStats* stats);
deadbeef49f34fd2016-12-06 16:22:06 -0800112 void SetMetricsObserver(webrtc::MetricsObserverInterface* metrics_observer);
deadbeefcbecd352015-09-23 11:50:27 -0700113
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700114 // Creates a channel if it doesn't exist. Otherwise, increments a reference
115 // count and returns an existing channel.
zhihuangb2cdd932017-01-19 16:54:25 -0800116 DtlsTransportInternal* CreateDtlsTransport(const std::string& transport_name,
117 int component);
118 virtual DtlsTransportInternal* CreateDtlsTransport_n(
deadbeefcbecd352015-09-23 11:50:27 -0700119 const std::string& transport_name,
120 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700121
122 // Decrements a channel's reference count, and destroys the channel if
123 // nothing is referencing it.
zhihuangb2cdd932017-01-19 16:54:25 -0800124 virtual void DestroyDtlsTransport(const std::string& transport_name,
125 int component);
126 virtual void DestroyDtlsTransport_n(const std::string& transport_name,
127 int component);
deadbeefcbecd352015-09-23 11:50:27 -0700128
mikescarlette7748672016-04-29 20:20:54 -0700129 void use_quic() { quic_ = true; }
130 bool quic() const { return quic_; }
131
deadbeef49f34fd2016-12-06 16:22:06 -0800132 // TODO(deadbeef): Remove all for_testing methods!
133 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing()
134 const {
135 return certificate_;
136 }
137 std::vector<std::string> transport_names_for_testing();
zhihuangb2cdd932017-01-19 16:54:25 -0800138 std::vector<DtlsTransportInternal*> channels_for_testing();
139 DtlsTransportInternal* get_channel_for_testing(
deadbeef49f34fd2016-12-06 16:22:06 -0800140 const std::string& transport_name,
141 int component);
142
deadbeefcbecd352015-09-23 11:50:27 -0700143 // All of these signals are fired on the signalling thread.
144
145 // If any transport failed => failed,
146 // Else if all completed => completed,
147 // Else if all connected => connected,
148 // Else => connecting
149 sigslot::signal1<IceConnectionState> SignalConnectionState;
150
151 // Receiving if any transport is receiving
152 sigslot::signal1<bool> SignalReceiving;
153
154 // If all transports done gathering => complete,
155 // Else if any are gathering => gathering,
156 // Else => new
157 sigslot::signal1<IceGatheringState> SignalGatheringState;
158
159 // (transport_name, candidates)
160 sigslot::signal2<const std::string&, const Candidates&>
161 SignalCandidatesGathered;
162
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700163 sigslot::signal1<const Candidates&> SignalCandidatesRemoved;
164
zhihuangd82eee02016-08-26 11:25:05 -0700165 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
166
deadbeefcbecd352015-09-23 11:50:27 -0700167 protected:
deadbeef49f34fd2016-12-06 16:22:06 -0800168 // TODO(deadbeef): Get rid of these virtual methods. Used by
169 // FakeTransportController currently, but FakeTransportController shouldn't
170 // even be functioning by subclassing TransportController.
zhihuangd06adf62017-01-12 15:58:31 -0800171 virtual IceTransportInternal* CreateIceTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800172 const std::string& transport_name,
173 int component);
zhihuangb2cdd932017-01-19 16:54:25 -0800174 virtual DtlsTransportInternal* CreateDtlsTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800175 const std::string& transport_name,
176 int component,
zhihuangd06adf62017-01-12 15:58:31 -0800177 IceTransportInternal* ice);
deadbeefcbecd352015-09-23 11:50:27 -0700178
179 private:
180 void OnMessage(rtc::Message* pmsg) override;
181
deadbeef62802a12016-12-13 16:38:36 -0800182 class ChannelPair;
183 typedef rtc::RefCountedObject<ChannelPair> RefCountedChannel;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700184
deadbeef49f34fd2016-12-06 16:22:06 -0800185 // Helper functions to get a channel or transport, or iterator to it (in case
186 // it needs to be erased).
deadbeef62802a12016-12-13 16:38:36 -0800187 std::vector<RefCountedChannel*>::iterator GetChannelIterator_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700188 const std::string& transport_name,
189 int component);
deadbeef62802a12016-12-13 16:38:36 -0800190 std::vector<RefCountedChannel*>::const_iterator GetChannelIterator_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800191 const std::string& transport_name,
192 int component) const;
deadbeefd1a38b52016-12-10 13:15:33 -0800193 const JsepTransport* GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800194 const std::string& transport_name) const;
deadbeefd1a38b52016-12-10 13:15:33 -0800195 JsepTransport* GetJsepTransport(const std::string& transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800196 const RefCountedChannel* GetChannel_n(const std::string& transport_name,
197 int component) const;
198 RefCountedChannel* GetChannel_n(const std::string& transport_name,
199 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700200
deadbeefd1a38b52016-12-10 13:15:33 -0800201 JsepTransport* GetOrCreateJsepTransport(const std::string& transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800202 void DestroyAllChannels_n();
deadbeefcbecd352015-09-23 11:50:27 -0700203
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200204 bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version);
205 void SetIceConfig_n(const IceConfig& config);
206 void SetIceRole_n(IceRole ice_role);
deadbeef49f34fd2016-12-06 16:22:06 -0800207 bool GetSslRole_n(const std::string& transport_name,
208 rtc::SSLRole* role) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200209 bool SetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700210 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200211 bool GetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700212 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800213 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200214 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800215 const std::string& transport_name) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200216 bool SetLocalTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700217 const TransportDescription& tdesc,
218 ContentAction action,
219 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200220 bool SetRemoteTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700221 const TransportDescription& tdesc,
222 ContentAction action,
223 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200224 void MaybeStartGathering_n();
225 bool AddRemoteCandidates_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700226 const Candidates& candidates,
227 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200228 bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err);
deadbeef49f34fd2016-12-06 16:22:06 -0800229 bool ReadyForRemoteCandidates_n(const std::string& transport_name) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200230 bool GetStats_n(const std::string& transport_name, TransportStats* stats);
deadbeef49f34fd2016-12-06 16:22:06 -0800231 void SetMetricsObserver_n(webrtc::MetricsObserverInterface* metrics_observer);
deadbeefcbecd352015-09-23 11:50:27 -0700232
233 // Handlers for signals from Transport.
deadbeef5bd5ca32017-02-10 11:31:50 -0800234 void OnChannelWritableState_n(rtc::PacketTransportInternal* transport);
235 void OnChannelReceivingState_n(rtc::PacketTransportInternal* transport);
zhihuangb2cdd932017-01-19 16:54:25 -0800236 void OnChannelGatheringState_n(IceTransportInternal* channel);
237 void OnChannelCandidateGathered_n(IceTransportInternal* channel,
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700238 const Candidate& candidate);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700239 void OnChannelCandidatesRemoved(const Candidates& candidates);
zhihuangb2cdd932017-01-19 16:54:25 -0800240 void OnChannelCandidatesRemoved_n(IceTransportInternal* channel,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700241 const Candidates& candidates);
zhihuangb2cdd932017-01-19 16:54:25 -0800242 void OnChannelRoleConflict_n(IceTransportInternal* channel);
243 void OnChannelStateChanged_n(IceTransportInternal* channel);
deadbeefcbecd352015-09-23 11:50:27 -0700244
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200245 void UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700246
zhihuangd82eee02016-08-26 11:25:05 -0700247 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
248
deadbeefcbecd352015-09-23 11:50:27 -0700249 rtc::Thread* const signaling_thread_ = nullptr;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200250 rtc::Thread* const network_thread_ = nullptr;
deadbeef57fd7262016-12-06 15:28:55 -0800251 PortAllocator* const port_allocator_ = nullptr;
deadbeef49f34fd2016-12-06 16:22:06 -0800252
253 std::map<std::string, std::unique_ptr<JsepTransport>> transports_;
deadbeef62802a12016-12-13 16:38:36 -0800254 std::vector<RefCountedChannel*> channels_;
deadbeef57fd7262016-12-06 15:28:55 -0800255
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700256 // Aggregate state for TransportChannelImpls.
deadbeefcbecd352015-09-23 11:50:27 -0700257 IceConnectionState connection_state_ = kIceConnectionConnecting;
258 bool receiving_ = false;
259 IceGatheringState gathering_state_ = kIceGatheringNew;
260
honghaiz1f429e32015-09-28 07:57:34 -0700261 IceConfig ice_config_;
deadbeefcbecd352015-09-23 11:50:27 -0700262 IceRole ice_role_ = ICEROLE_CONTROLLING;
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700263 bool redetermine_role_on_ice_restart_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200264 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
deadbeef49f34fd2016-12-06 16:22:06 -0800265 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefcbecd352015-09-23 11:50:27 -0700266 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700267 rtc::AsyncInvoker invoker_;
mikescarlette7748672016-04-29 20:20:54 -0700268 // True if QUIC is used instead of DTLS.
269 bool quic_ = false;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700270
271 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
deadbeef62802a12016-12-13 16:38:36 -0800272
273 RTC_DISALLOW_COPY_AND_ASSIGN(TransportController);
deadbeefcbecd352015-09-23 11:50:27 -0700274};
275
276} // namespace cricket
277
278#endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_