blob: ed7216033b429d91625fd16a3c86d3a55cf9722f [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>
15#include <string>
16#include <vector>
17
Honghai Zhang7fb69db2016-03-14 11:59:18 -070018#include "webrtc/base/asyncinvoker.h"
deadbeefcbecd352015-09-23 11:50:27 -070019#include "webrtc/base/sigslot.h"
20#include "webrtc/base/sslstreamadapter.h"
21#include "webrtc/p2p/base/candidate.h"
22#include "webrtc/p2p/base/transport.h"
23
24namespace rtc {
25class Thread;
26}
27
28namespace cricket {
29
30class TransportController : public sigslot::has_slots<>,
31 public rtc::MessageHandler {
32 public:
33 TransportController(rtc::Thread* signaling_thread,
34 rtc::Thread* worker_thread,
35 PortAllocator* port_allocator);
36
37 virtual ~TransportController();
38
39 rtc::Thread* signaling_thread() const { return signaling_thread_; }
40 rtc::Thread* worker_thread() const { return worker_thread_; }
41
42 PortAllocator* port_allocator() const { return port_allocator_; }
43
44 // Can only be set before transports are created.
45 // TODO(deadbeef): Make this an argument to the constructor once BaseSession
46 // and WebRtcSession are combined
47 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version);
48
honghaiz1f429e32015-09-28 07:57:34 -070049 void SetIceConfig(const IceConfig& config);
deadbeefcbecd352015-09-23 11:50:27 -070050 void SetIceRole(IceRole ice_role);
51
Taylor Brandstetterf475d362016-01-08 15:35:57 -080052 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role);
deadbeefcbecd352015-09-23 11:50:27 -070053
54 // Specifies the identity to use in this session.
55 // Can only be called once.
56 bool SetLocalCertificate(
57 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
58 bool GetLocalCertificate(
59 const std::string& transport_name,
60 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
61 // Caller owns returned certificate
62 bool GetRemoteSSLCertificate(const std::string& transport_name,
63 rtc::SSLCertificate** cert);
64 bool SetLocalTransportDescription(const std::string& transport_name,
65 const TransportDescription& tdesc,
66 ContentAction action,
67 std::string* err);
68 bool SetRemoteTransportDescription(const std::string& transport_name,
69 const TransportDescription& tdesc,
70 ContentAction action,
71 std::string* err);
72 // Start gathering candidates for any new transports, or transports doing an
73 // ICE restart.
74 void MaybeStartGathering();
75 bool AddRemoteCandidates(const std::string& transport_name,
76 const Candidates& candidates,
77 std::string* err);
Honghai Zhang7fb69db2016-03-14 11:59:18 -070078 bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err);
deadbeefcbecd352015-09-23 11:50:27 -070079 bool ReadyForRemoteCandidates(const std::string& transport_name);
80 bool GetStats(const std::string& transport_name, TransportStats* stats);
81
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070082 // Creates a channel if it doesn't exist. Otherwise, increments a reference
83 // count and returns an existing channel.
deadbeefcbecd352015-09-23 11:50:27 -070084 virtual TransportChannel* CreateTransportChannel_w(
85 const std::string& transport_name,
86 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070087
88 // Decrements a channel's reference count, and destroys the channel if
89 // nothing is referencing it.
deadbeefcbecd352015-09-23 11:50:27 -070090 virtual void DestroyTransportChannel_w(const std::string& transport_name,
91 int component);
92
93 // All of these signals are fired on the signalling thread.
94
95 // If any transport failed => failed,
96 // Else if all completed => completed,
97 // Else if all connected => connected,
98 // Else => connecting
99 sigslot::signal1<IceConnectionState> SignalConnectionState;
100
101 // Receiving if any transport is receiving
102 sigslot::signal1<bool> SignalReceiving;
103
104 // If all transports done gathering => complete,
105 // Else if any are gathering => gathering,
106 // Else => new
107 sigslot::signal1<IceGatheringState> SignalGatheringState;
108
109 // (transport_name, candidates)
110 sigslot::signal2<const std::string&, const Candidates&>
111 SignalCandidatesGathered;
112
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700113 sigslot::signal1<const Candidates&> SignalCandidatesRemoved;
114
deadbeefcbecd352015-09-23 11:50:27 -0700115 // for unit test
116 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
117
118 protected:
119 // Protected and virtual so we can override it in unit tests.
120 virtual Transport* CreateTransport_w(const std::string& transport_name);
121
122 // For unit tests
123 const std::map<std::string, Transport*>& transports() { return transports_; }
124 Transport* GetTransport_w(const std::string& transport_name);
125
126 private:
127 void OnMessage(rtc::Message* pmsg) override;
128
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700129 // It's the Transport that's currently responsible for creating/destroying
130 // channels, but the TransportController keeps track of how many external
131 // objects (BaseChannels) reference each channel.
132 struct RefCountedChannel {
133 RefCountedChannel() : impl_(nullptr), ref_(0) {}
134 explicit RefCountedChannel(TransportChannelImpl* impl)
135 : impl_(impl), ref_(0) {}
136
137 void AddRef() { ++ref_; }
138 void DecRef() {
139 ASSERT(ref_ > 0);
140 --ref_;
141 }
142 int ref() const { return ref_; }
143
144 TransportChannelImpl* get() const { return impl_; }
145 TransportChannelImpl* operator->() const { return impl_; }
146
147 private:
148 TransportChannelImpl* impl_;
149 int ref_;
150 };
151
152 std::vector<RefCountedChannel>::iterator FindChannel_w(
153 const std::string& transport_name,
154 int component);
155
deadbeefcbecd352015-09-23 11:50:27 -0700156 Transport* GetOrCreateTransport_w(const std::string& transport_name);
157 void DestroyTransport_w(const std::string& transport_name);
158 void DestroyAllTransports_w();
159
160 bool SetSslMaxProtocolVersion_w(rtc::SSLProtocolVersion version);
honghaiz1f429e32015-09-28 07:57:34 -0700161 void SetIceConfig_w(const IceConfig& config);
deadbeefcbecd352015-09-23 11:50:27 -0700162 void SetIceRole_w(IceRole ice_role);
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800163 bool GetSslRole_w(const std::string& transport_name, rtc::SSLRole* role);
deadbeefcbecd352015-09-23 11:50:27 -0700164 bool SetLocalCertificate_w(
165 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
166 bool GetLocalCertificate_w(
167 const std::string& transport_name,
168 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
169 bool GetRemoteSSLCertificate_w(const std::string& transport_name,
170 rtc::SSLCertificate** cert);
171 bool SetLocalTransportDescription_w(const std::string& transport_name,
172 const TransportDescription& tdesc,
173 ContentAction action,
174 std::string* err);
175 bool SetRemoteTransportDescription_w(const std::string& transport_name,
176 const TransportDescription& tdesc,
177 ContentAction action,
178 std::string* err);
179 void MaybeStartGathering_w();
180 bool AddRemoteCandidates_w(const std::string& transport_name,
181 const Candidates& candidates,
182 std::string* err);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700183 bool RemoveRemoteCandidates_w(const Candidates& candidates, std::string* err);
deadbeefcbecd352015-09-23 11:50:27 -0700184 bool ReadyForRemoteCandidates_w(const std::string& transport_name);
185 bool GetStats_w(const std::string& transport_name, TransportStats* stats);
186
187 // Handlers for signals from Transport.
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700188 void OnChannelWritableState_w(TransportChannel* channel);
189 void OnChannelReceivingState_w(TransportChannel* channel);
190 void OnChannelGatheringState_w(TransportChannelImpl* channel);
191 void OnChannelCandidateGathered_w(TransportChannelImpl* channel,
192 const Candidate& candidate);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700193 void OnChannelCandidatesRemoved(const Candidates& candidates);
194 void OnChannelCandidatesRemoved_w(TransportChannelImpl* channel,
195 const Candidates& candidates);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700196 void OnChannelRoleConflict_w(TransportChannelImpl* channel);
197 void OnChannelConnectionRemoved_w(TransportChannelImpl* channel);
deadbeefcbecd352015-09-23 11:50:27 -0700198
199 void UpdateAggregateStates_w();
deadbeefcbecd352015-09-23 11:50:27 -0700200
201 rtc::Thread* const signaling_thread_ = nullptr;
202 rtc::Thread* const worker_thread_ = nullptr;
203 typedef std::map<std::string, Transport*> TransportMap;
204 TransportMap transports_;
205
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700206 std::vector<RefCountedChannel> channels_;
207
deadbeefcbecd352015-09-23 11:50:27 -0700208 PortAllocator* const port_allocator_ = nullptr;
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800209 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefcbecd352015-09-23 11:50:27 -0700210
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700211 // Aggregate state for TransportChannelImpls.
deadbeefcbecd352015-09-23 11:50:27 -0700212 IceConnectionState connection_state_ = kIceConnectionConnecting;
213 bool receiving_ = false;
214 IceGatheringState gathering_state_ = kIceGatheringNew;
215
216 // TODO(deadbeef): Move the fields below down to the transports themselves
honghaiz1f429e32015-09-28 07:57:34 -0700217 IceConfig ice_config_;
deadbeefcbecd352015-09-23 11:50:27 -0700218 IceRole ice_role_ = ICEROLE_CONTROLLING;
219 // Flag which will be set to true after the first role switch
220 bool ice_role_switch_ = false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200221 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
deadbeefcbecd352015-09-23 11:50:27 -0700222 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700223 rtc::AsyncInvoker invoker_;
deadbeefcbecd352015-09-23 11:50:27 -0700224};
225
226} // namespace cricket
227
228#endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_