blob: 7929cf014024ac7f4a0204bf5ace161a525c7321 [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;
johand89ab142016-10-25 10:50:32 -070031class PacketTransportInterface;
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.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200116 virtual TransportChannel* CreateTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700117 const std::string& transport_name,
118 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700119
120 // Decrements a channel's reference count, and destroys the channel if
121 // nothing is referencing it.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200122 virtual void DestroyTransportChannel_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700123 int component);
124
mikescarlette7748672016-04-29 20:20:54 -0700125 void use_quic() { quic_ = true; }
126 bool quic() const { return quic_; }
127
deadbeef49f34fd2016-12-06 16:22:06 -0800128 // TODO(deadbeef): Remove all for_testing methods!
129 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing()
130 const {
131 return certificate_;
132 }
133 std::vector<std::string> transport_names_for_testing();
134 std::vector<TransportChannelImpl*> channels_for_testing();
135 TransportChannelImpl* get_channel_for_testing(
136 const std::string& transport_name,
137 int component);
138
deadbeefcbecd352015-09-23 11:50:27 -0700139 // All of these signals are fired on the signalling thread.
140
141 // If any transport failed => failed,
142 // Else if all completed => completed,
143 // Else if all connected => connected,
144 // Else => connecting
145 sigslot::signal1<IceConnectionState> SignalConnectionState;
146
147 // Receiving if any transport is receiving
148 sigslot::signal1<bool> SignalReceiving;
149
150 // If all transports done gathering => complete,
151 // Else if any are gathering => gathering,
152 // Else => new
153 sigslot::signal1<IceGatheringState> SignalGatheringState;
154
155 // (transport_name, candidates)
156 sigslot::signal2<const std::string&, const Candidates&>
157 SignalCandidatesGathered;
158
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700159 sigslot::signal1<const Candidates&> SignalCandidatesRemoved;
160
zhihuangd82eee02016-08-26 11:25:05 -0700161 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
162
deadbeefcbecd352015-09-23 11:50:27 -0700163 protected:
deadbeef49f34fd2016-12-06 16:22:06 -0800164 // TODO(deadbeef): Get rid of these virtual methods. Used by
165 // FakeTransportController currently, but FakeTransportController shouldn't
166 // even be functioning by subclassing TransportController.
zhihuangd06adf62017-01-12 15:58:31 -0800167 virtual IceTransportInternal* CreateIceTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800168 const std::string& transport_name,
169 int component);
170 virtual TransportChannelImpl* CreateDtlsTransportChannel_n(
171 const std::string& transport_name,
172 int component,
zhihuangd06adf62017-01-12 15:58:31 -0800173 IceTransportInternal* ice);
deadbeefcbecd352015-09-23 11:50:27 -0700174
175 private:
176 void OnMessage(rtc::Message* pmsg) override;
177
deadbeef62802a12016-12-13 16:38:36 -0800178 class ChannelPair;
179 typedef rtc::RefCountedObject<ChannelPair> RefCountedChannel;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700180
deadbeef49f34fd2016-12-06 16:22:06 -0800181 // Helper functions to get a channel or transport, or iterator to it (in case
182 // it needs to be erased).
deadbeef62802a12016-12-13 16:38:36 -0800183 std::vector<RefCountedChannel*>::iterator GetChannelIterator_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700184 const std::string& transport_name,
185 int component);
deadbeef62802a12016-12-13 16:38:36 -0800186 std::vector<RefCountedChannel*>::const_iterator GetChannelIterator_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800187 const std::string& transport_name,
188 int component) const;
deadbeefd1a38b52016-12-10 13:15:33 -0800189 const JsepTransport* GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800190 const std::string& transport_name) const;
deadbeefd1a38b52016-12-10 13:15:33 -0800191 JsepTransport* GetJsepTransport(const std::string& transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800192 const RefCountedChannel* GetChannel_n(const std::string& transport_name,
193 int component) const;
194 RefCountedChannel* GetChannel_n(const std::string& transport_name,
195 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700196
deadbeefd1a38b52016-12-10 13:15:33 -0800197 JsepTransport* GetOrCreateJsepTransport(const std::string& transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800198 void DestroyAllChannels_n();
deadbeefcbecd352015-09-23 11:50:27 -0700199
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200200 bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version);
201 void SetIceConfig_n(const IceConfig& config);
202 void SetIceRole_n(IceRole ice_role);
deadbeef49f34fd2016-12-06 16:22:06 -0800203 bool GetSslRole_n(const std::string& transport_name,
204 rtc::SSLRole* role) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200205 bool SetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700206 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200207 bool GetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700208 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800209 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200210 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800211 const std::string& transport_name) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200212 bool SetLocalTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700213 const TransportDescription& tdesc,
214 ContentAction action,
215 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200216 bool SetRemoteTransportDescription_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 void MaybeStartGathering_n();
221 bool AddRemoteCandidates_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700222 const Candidates& candidates,
223 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200224 bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err);
deadbeef49f34fd2016-12-06 16:22:06 -0800225 bool ReadyForRemoteCandidates_n(const std::string& transport_name) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200226 bool GetStats_n(const std::string& transport_name, TransportStats* stats);
deadbeef49f34fd2016-12-06 16:22:06 -0800227 void SetMetricsObserver_n(webrtc::MetricsObserverInterface* metrics_observer);
deadbeefcbecd352015-09-23 11:50:27 -0700228
229 // Handlers for signals from Transport.
johand89ab142016-10-25 10:50:32 -0700230 void OnChannelWritableState_n(rtc::PacketTransportInterface* transport);
johan15ca8f62016-11-01 01:47:41 -0700231 void OnChannelReceivingState_n(rtc::PacketTransportInterface* transport);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200232 void OnChannelGatheringState_n(TransportChannelImpl* channel);
233 void OnChannelCandidateGathered_n(TransportChannelImpl* channel,
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700234 const Candidate& candidate);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700235 void OnChannelCandidatesRemoved(const Candidates& candidates);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200236 void OnChannelCandidatesRemoved_n(TransportChannelImpl* channel,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700237 const Candidates& candidates);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200238 void OnChannelRoleConflict_n(TransportChannelImpl* channel);
Honghai Zhang1590c392016-05-24 13:15:02 -0700239 void OnChannelStateChanged_n(TransportChannelImpl* channel);
deadbeefcbecd352015-09-23 11:50:27 -0700240
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200241 void UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700242
zhihuangd82eee02016-08-26 11:25:05 -0700243 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
244
deadbeefcbecd352015-09-23 11:50:27 -0700245 rtc::Thread* const signaling_thread_ = nullptr;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200246 rtc::Thread* const network_thread_ = nullptr;
deadbeef57fd7262016-12-06 15:28:55 -0800247 PortAllocator* const port_allocator_ = nullptr;
deadbeef49f34fd2016-12-06 16:22:06 -0800248
249 std::map<std::string, std::unique_ptr<JsepTransport>> transports_;
deadbeef62802a12016-12-13 16:38:36 -0800250 std::vector<RefCountedChannel*> channels_;
deadbeef57fd7262016-12-06 15:28:55 -0800251
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700252 // Aggregate state for TransportChannelImpls.
deadbeefcbecd352015-09-23 11:50:27 -0700253 IceConnectionState connection_state_ = kIceConnectionConnecting;
254 bool receiving_ = false;
255 IceGatheringState gathering_state_ = kIceGatheringNew;
256
honghaiz1f429e32015-09-28 07:57:34 -0700257 IceConfig ice_config_;
deadbeefcbecd352015-09-23 11:50:27 -0700258 IceRole ice_role_ = ICEROLE_CONTROLLING;
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700259 bool redetermine_role_on_ice_restart_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200260 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
deadbeef49f34fd2016-12-06 16:22:06 -0800261 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefcbecd352015-09-23 11:50:27 -0700262 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700263 rtc::AsyncInvoker invoker_;
mikescarlette7748672016-04-29 20:20:54 -0700264 // True if QUIC is used instead of DTLS.
265 bool quic_ = false;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700266
267 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
deadbeef62802a12016-12-13 16:38:36 -0800268
269 RTC_DISALLOW_COPY_AND_ASSIGN(TransportController);
deadbeefcbecd352015-09-23 11:50:27 -0700270};
271
272} // namespace cricket
273
274#endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_