blob: b5d9ac7d40455d53e19441e73a19a421520eced2 [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}
28
29namespace cricket {
30
31class TransportController : public sigslot::has_slots<>,
32 public rtc::MessageHandler {
33 public:
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070034 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
35 // upon setting a local transport description that indicates an ICE restart.
36 // For the constructor that doesn't take this parameter, it defaults to true.
37 TransportController(rtc::Thread* signaling_thread,
38 rtc::Thread* network_thread,
39 PortAllocator* port_allocator,
40 bool redetermine_role_on_ice_restart);
41
deadbeefcbecd352015-09-23 11:50:27 -070042 TransportController(rtc::Thread* signaling_thread,
Danil Chapovalov7f216b72016-05-12 09:20:31 +020043 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -070044 PortAllocator* port_allocator);
45
46 virtual ~TransportController();
47
48 rtc::Thread* signaling_thread() const { return signaling_thread_; }
Danil Chapovalov7f216b72016-05-12 09:20:31 +020049 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070050
51 PortAllocator* port_allocator() const { return port_allocator_; }
52
53 // Can only be set before transports are created.
54 // TODO(deadbeef): Make this an argument to the constructor once BaseSession
55 // and WebRtcSession are combined
56 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version);
57
honghaiz1f429e32015-09-28 07:57:34 -070058 void SetIceConfig(const IceConfig& config);
deadbeefcbecd352015-09-23 11:50:27 -070059 void SetIceRole(IceRole ice_role);
60
Taylor Brandstetterf475d362016-01-08 15:35:57 -080061 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role);
deadbeefcbecd352015-09-23 11:50:27 -070062
63 // Specifies the identity to use in this session.
64 // Can only be called once.
65 bool SetLocalCertificate(
66 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
67 bool GetLocalCertificate(
68 const std::string& transport_name,
69 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
70 // Caller owns returned certificate
jbauch555604a2016-04-26 03:13:22 -070071 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -070072 const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -070073 bool SetLocalTransportDescription(const std::string& transport_name,
74 const TransportDescription& tdesc,
75 ContentAction action,
76 std::string* err);
77 bool SetRemoteTransportDescription(const std::string& transport_name,
78 const TransportDescription& tdesc,
79 ContentAction action,
80 std::string* err);
81 // Start gathering candidates for any new transports, or transports doing an
82 // ICE restart.
83 void MaybeStartGathering();
84 bool AddRemoteCandidates(const std::string& transport_name,
85 const Candidates& candidates,
86 std::string* err);
Honghai Zhang7fb69db2016-03-14 11:59:18 -070087 bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err);
deadbeefcbecd352015-09-23 11:50:27 -070088 bool ReadyForRemoteCandidates(const std::string& transport_name);
89 bool GetStats(const std::string& transport_name, TransportStats* stats);
90
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070091 // Creates a channel if it doesn't exist. Otherwise, increments a reference
92 // count and returns an existing channel.
Danil Chapovalov7f216b72016-05-12 09:20:31 +020093 virtual TransportChannel* CreateTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -070094 const std::string& transport_name,
95 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070096
97 // Decrements a channel's reference count, and destroys the channel if
98 // nothing is referencing it.
Danil Chapovalov7f216b72016-05-12 09:20:31 +020099 virtual void DestroyTransportChannel_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700100 int component);
101
mikescarlette7748672016-04-29 20:20:54 -0700102 void use_quic() { quic_ = true; }
103 bool quic() const { return quic_; }
104
deadbeefcbecd352015-09-23 11:50:27 -0700105 // All of these signals are fired on the signalling thread.
106
107 // If any transport failed => failed,
108 // Else if all completed => completed,
109 // Else if all connected => connected,
110 // Else => connecting
111 sigslot::signal1<IceConnectionState> SignalConnectionState;
112
113 // Receiving if any transport is receiving
114 sigslot::signal1<bool> SignalReceiving;
115
116 // If all transports done gathering => complete,
117 // Else if any are gathering => gathering,
118 // Else => new
119 sigslot::signal1<IceGatheringState> SignalGatheringState;
120
121 // (transport_name, candidates)
122 sigslot::signal2<const std::string&, const Candidates&>
123 SignalCandidatesGathered;
124
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700125 sigslot::signal1<const Candidates&> SignalCandidatesRemoved;
126
deadbeefcbecd352015-09-23 11:50:27 -0700127 // for unit test
128 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
129
zhihuangd82eee02016-08-26 11:25:05 -0700130 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
131
deadbeefcbecd352015-09-23 11:50:27 -0700132 protected:
133 // Protected and virtual so we can override it in unit tests.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200134 virtual Transport* CreateTransport_n(const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700135
136 // For unit tests
137 const std::map<std::string, Transport*>& transports() { return transports_; }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200138 Transport* GetTransport_n(const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700139
140 private:
141 void OnMessage(rtc::Message* pmsg) override;
142
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700143 // It's the Transport that's currently responsible for creating/destroying
144 // channels, but the TransportController keeps track of how many external
145 // objects (BaseChannels) reference each channel.
146 struct RefCountedChannel {
147 RefCountedChannel() : impl_(nullptr), ref_(0) {}
148 explicit RefCountedChannel(TransportChannelImpl* impl)
149 : impl_(impl), ref_(0) {}
150
151 void AddRef() { ++ref_; }
152 void DecRef() {
153 ASSERT(ref_ > 0);
154 --ref_;
155 }
156 int ref() const { return ref_; }
157
158 TransportChannelImpl* get() const { return impl_; }
159 TransportChannelImpl* operator->() const { return impl_; }
160
161 private:
162 TransportChannelImpl* impl_;
163 int ref_;
164 };
165
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200166 std::vector<RefCountedChannel>::iterator FindChannel_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700167 const std::string& transport_name,
168 int component);
169
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200170 Transport* GetOrCreateTransport_n(const std::string& transport_name);
171 void DestroyTransport_n(const std::string& transport_name);
172 void DestroyAllTransports_n();
deadbeefcbecd352015-09-23 11:50:27 -0700173
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200174 bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version);
175 void SetIceConfig_n(const IceConfig& config);
176 void SetIceRole_n(IceRole ice_role);
177 bool GetSslRole_n(const std::string& transport_name, rtc::SSLRole* role);
178 bool SetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700179 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200180 bool GetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700181 const std::string& transport_name,
182 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200183 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n(
kwibergb4d01c42016-04-06 05:15:06 -0700184 const std::string& transport_name);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200185 bool SetLocalTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700186 const TransportDescription& tdesc,
187 ContentAction action,
188 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200189 bool SetRemoteTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700190 const TransportDescription& tdesc,
191 ContentAction action,
192 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200193 void MaybeStartGathering_n();
194 bool AddRemoteCandidates_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700195 const Candidates& candidates,
196 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200197 bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err);
198 bool ReadyForRemoteCandidates_n(const std::string& transport_name);
199 bool GetStats_n(const std::string& transport_name, TransportStats* stats);
deadbeefcbecd352015-09-23 11:50:27 -0700200
201 // Handlers for signals from Transport.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200202 void OnChannelWritableState_n(TransportChannel* channel);
203 void OnChannelReceivingState_n(TransportChannel* channel);
204 void OnChannelGatheringState_n(TransportChannelImpl* channel);
205 void OnChannelCandidateGathered_n(TransportChannelImpl* channel,
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700206 const Candidate& candidate);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700207 void OnChannelCandidatesRemoved(const Candidates& candidates);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200208 void OnChannelCandidatesRemoved_n(TransportChannelImpl* channel,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700209 const Candidates& candidates);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200210 void OnChannelRoleConflict_n(TransportChannelImpl* channel);
Honghai Zhang1590c392016-05-24 13:15:02 -0700211 void OnChannelStateChanged_n(TransportChannelImpl* channel);
deadbeefcbecd352015-09-23 11:50:27 -0700212
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200213 void UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700214
zhihuangd82eee02016-08-26 11:25:05 -0700215 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
216
deadbeefcbecd352015-09-23 11:50:27 -0700217 rtc::Thread* const signaling_thread_ = nullptr;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200218 rtc::Thread* const network_thread_ = nullptr;
deadbeefcbecd352015-09-23 11:50:27 -0700219 typedef std::map<std::string, Transport*> TransportMap;
220 TransportMap transports_;
221
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700222 std::vector<RefCountedChannel> channels_;
223
deadbeefcbecd352015-09-23 11:50:27 -0700224 PortAllocator* const port_allocator_ = nullptr;
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800225 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefcbecd352015-09-23 11:50:27 -0700226
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700227 // Aggregate state for TransportChannelImpls.
deadbeefcbecd352015-09-23 11:50:27 -0700228 IceConnectionState connection_state_ = kIceConnectionConnecting;
229 bool receiving_ = false;
230 IceGatheringState gathering_state_ = kIceGatheringNew;
231
232 // TODO(deadbeef): Move the fields below down to the transports themselves
honghaiz1f429e32015-09-28 07:57:34 -0700233 IceConfig ice_config_;
deadbeefcbecd352015-09-23 11:50:27 -0700234 IceRole ice_role_ = ICEROLE_CONTROLLING;
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700235 bool redetermine_role_on_ice_restart_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200236 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
deadbeefcbecd352015-09-23 11:50:27 -0700237 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700238 rtc::AsyncInvoker invoker_;
mikescarlette7748672016-04-29 20:20:54 -0700239 // True if QUIC is used instead of DTLS.
240 bool quic_ = false;
deadbeefcbecd352015-09-23 11:50:27 -0700241};
242
243} // namespace cricket
244
245#endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_