blob: 5f3fccc868822a1eda564345d7e2a5c7ce7b0d2e [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"
deadbeefcbecd352015-09-23 11:50:27 -070020#include "webrtc/base/sigslot.h"
21#include "webrtc/base/sslstreamadapter.h"
22#include "webrtc/p2p/base/candidate.h"
23#include "webrtc/p2p/base/transport.h"
24
25namespace rtc {
26class Thread;
27}
Honghai Zhangd93f50c2016-10-05 11:47:22 -070028namespace webrtc {
29class MetricsObserverInterface;
30}
deadbeefcbecd352015-09-23 11:50:27 -070031
32namespace cricket {
33
34class TransportController : public sigslot::has_slots<>,
35 public rtc::MessageHandler {
36 public:
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070037 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
38 // upon setting a local transport description that indicates an ICE restart.
39 // For the constructor that doesn't take this parameter, it defaults to true.
40 TransportController(rtc::Thread* signaling_thread,
41 rtc::Thread* network_thread,
42 PortAllocator* port_allocator,
43 bool redetermine_role_on_ice_restart);
44
deadbeefcbecd352015-09-23 11:50:27 -070045 TransportController(rtc::Thread* signaling_thread,
Danil Chapovalov7f216b72016-05-12 09:20:31 +020046 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -070047 PortAllocator* port_allocator);
48
49 virtual ~TransportController();
50
51 rtc::Thread* signaling_thread() const { return signaling_thread_; }
Danil Chapovalov7f216b72016-05-12 09:20:31 +020052 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070053
54 PortAllocator* port_allocator() const { return port_allocator_; }
55
56 // Can only be set before transports are created.
57 // TODO(deadbeef): Make this an argument to the constructor once BaseSession
58 // and WebRtcSession are combined
59 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version);
60
honghaiz1f429e32015-09-28 07:57:34 -070061 void SetIceConfig(const IceConfig& config);
deadbeefcbecd352015-09-23 11:50:27 -070062 void SetIceRole(IceRole ice_role);
63
Taylor Brandstetterf475d362016-01-08 15:35:57 -080064 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role);
deadbeefcbecd352015-09-23 11:50:27 -070065
66 // Specifies the identity to use in this session.
67 // Can only be called once.
68 bool SetLocalCertificate(
69 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
70 bool GetLocalCertificate(
71 const std::string& transport_name,
72 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
73 // Caller owns returned certificate
jbauch555604a2016-04-26 03:13:22 -070074 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -070075 const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -070076 bool SetLocalTransportDescription(const std::string& transport_name,
77 const TransportDescription& tdesc,
78 ContentAction action,
79 std::string* err);
80 bool SetRemoteTransportDescription(const std::string& transport_name,
81 const TransportDescription& tdesc,
82 ContentAction action,
83 std::string* err);
84 // Start gathering candidates for any new transports, or transports doing an
85 // ICE restart.
86 void MaybeStartGathering();
87 bool AddRemoteCandidates(const std::string& transport_name,
88 const Candidates& candidates,
89 std::string* err);
Honghai Zhang7fb69db2016-03-14 11:59:18 -070090 bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err);
deadbeefcbecd352015-09-23 11:50:27 -070091 bool ReadyForRemoteCandidates(const std::string& transport_name);
92 bool GetStats(const std::string& transport_name, TransportStats* stats);
93
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070094 // Creates a channel if it doesn't exist. Otherwise, increments a reference
95 // count and returns an existing channel.
Danil Chapovalov7f216b72016-05-12 09:20:31 +020096 virtual TransportChannel* CreateTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -070097 const std::string& transport_name,
98 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070099
100 // Decrements a channel's reference count, and destroys the channel if
101 // nothing is referencing it.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200102 virtual void DestroyTransportChannel_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700103 int component);
104
mikescarlette7748672016-04-29 20:20:54 -0700105 void use_quic() { quic_ = true; }
106 bool quic() const { return quic_; }
107
deadbeefcbecd352015-09-23 11:50:27 -0700108 // All of these signals are fired on the signalling thread.
109
110 // If any transport failed => failed,
111 // Else if all completed => completed,
112 // Else if all connected => connected,
113 // Else => connecting
114 sigslot::signal1<IceConnectionState> SignalConnectionState;
115
116 // Receiving if any transport is receiving
117 sigslot::signal1<bool> SignalReceiving;
118
119 // If all transports done gathering => complete,
120 // Else if any are gathering => gathering,
121 // Else => new
122 sigslot::signal1<IceGatheringState> SignalGatheringState;
123
124 // (transport_name, candidates)
125 sigslot::signal2<const std::string&, const Candidates&>
126 SignalCandidatesGathered;
127
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700128 sigslot::signal1<const Candidates&> SignalCandidatesRemoved;
129
deadbeefcbecd352015-09-23 11:50:27 -0700130 // for unit test
131 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
132
zhihuangd82eee02016-08-26 11:25:05 -0700133 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
134
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700135 void SetMetricsObserver(webrtc::MetricsObserverInterface* metrics_observer);
136
deadbeefcbecd352015-09-23 11:50:27 -0700137 protected:
138 // Protected and virtual so we can override it in unit tests.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200139 virtual Transport* CreateTransport_n(const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700140
141 // For unit tests
142 const std::map<std::string, Transport*>& transports() { return transports_; }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200143 Transport* GetTransport_n(const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700144
145 private:
146 void OnMessage(rtc::Message* pmsg) override;
147
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700148 // It's the Transport that's currently responsible for creating/destroying
149 // channels, but the TransportController keeps track of how many external
150 // objects (BaseChannels) reference each channel.
151 struct RefCountedChannel {
152 RefCountedChannel() : impl_(nullptr), ref_(0) {}
153 explicit RefCountedChannel(TransportChannelImpl* impl)
154 : impl_(impl), ref_(0) {}
155
156 void AddRef() { ++ref_; }
157 void DecRef() {
158 ASSERT(ref_ > 0);
159 --ref_;
160 }
161 int ref() const { return ref_; }
162
163 TransportChannelImpl* get() const { return impl_; }
164 TransportChannelImpl* operator->() const { return impl_; }
165
166 private:
167 TransportChannelImpl* impl_;
168 int ref_;
169 };
170
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200171 std::vector<RefCountedChannel>::iterator FindChannel_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700172 const std::string& transport_name,
173 int component);
174
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200175 Transport* GetOrCreateTransport_n(const std::string& transport_name);
176 void DestroyTransport_n(const std::string& transport_name);
177 void DestroyAllTransports_n();
deadbeefcbecd352015-09-23 11:50:27 -0700178
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200179 bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version);
180 void SetIceConfig_n(const IceConfig& config);
181 void SetIceRole_n(IceRole ice_role);
182 bool GetSslRole_n(const std::string& transport_name, rtc::SSLRole* role);
183 bool SetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700184 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200185 bool GetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700186 const std::string& transport_name,
187 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200188 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n(
kwibergb4d01c42016-04-06 05:15:06 -0700189 const std::string& transport_name);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200190 bool SetLocalTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700191 const TransportDescription& tdesc,
192 ContentAction action,
193 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200194 bool SetRemoteTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700195 const TransportDescription& tdesc,
196 ContentAction action,
197 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200198 void MaybeStartGathering_n();
199 bool AddRemoteCandidates_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700200 const Candidates& candidates,
201 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200202 bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err);
203 bool ReadyForRemoteCandidates_n(const std::string& transport_name);
204 bool GetStats_n(const std::string& transport_name, TransportStats* stats);
deadbeefcbecd352015-09-23 11:50:27 -0700205
206 // Handlers for signals from Transport.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200207 void OnChannelWritableState_n(TransportChannel* channel);
208 void OnChannelReceivingState_n(TransportChannel* channel);
209 void OnChannelGatheringState_n(TransportChannelImpl* channel);
210 void OnChannelCandidateGathered_n(TransportChannelImpl* channel,
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700211 const Candidate& candidate);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700212 void OnChannelCandidatesRemoved(const Candidates& candidates);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200213 void OnChannelCandidatesRemoved_n(TransportChannelImpl* channel,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700214 const Candidates& candidates);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200215 void OnChannelRoleConflict_n(TransportChannelImpl* channel);
Honghai Zhang1590c392016-05-24 13:15:02 -0700216 void OnChannelStateChanged_n(TransportChannelImpl* channel);
deadbeefcbecd352015-09-23 11:50:27 -0700217
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200218 void UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700219
zhihuangd82eee02016-08-26 11:25:05 -0700220 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
221
deadbeefcbecd352015-09-23 11:50:27 -0700222 rtc::Thread* const signaling_thread_ = nullptr;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200223 rtc::Thread* const network_thread_ = nullptr;
deadbeefcbecd352015-09-23 11:50:27 -0700224 typedef std::map<std::string, Transport*> TransportMap;
225 TransportMap transports_;
226
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700227 std::vector<RefCountedChannel> channels_;
228
deadbeefcbecd352015-09-23 11:50:27 -0700229 PortAllocator* const port_allocator_ = nullptr;
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800230 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefcbecd352015-09-23 11:50:27 -0700231
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700232 // Aggregate state for TransportChannelImpls.
deadbeefcbecd352015-09-23 11:50:27 -0700233 IceConnectionState connection_state_ = kIceConnectionConnecting;
234 bool receiving_ = false;
235 IceGatheringState gathering_state_ = kIceGatheringNew;
236
237 // TODO(deadbeef): Move the fields below down to the transports themselves
honghaiz1f429e32015-09-28 07:57:34 -0700238 IceConfig ice_config_;
deadbeefcbecd352015-09-23 11:50:27 -0700239 IceRole ice_role_ = ICEROLE_CONTROLLING;
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700240 bool redetermine_role_on_ice_restart_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200241 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
deadbeefcbecd352015-09-23 11:50:27 -0700242 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700243 rtc::AsyncInvoker invoker_;
mikescarlette7748672016-04-29 20:20:54 -0700244 // True if QUIC is used instead of DTLS.
245 bool quic_ = false;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700246
247 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
deadbeefcbecd352015-09-23 11:50:27 -0700248};
249
250} // namespace cricket
251
252#endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_