blob: da3bab3fe44f7fe26ece30cfb6129f2a7c2a7f37 [file] [log] [blame]
deadbeefcbecd352015-09-23 18:50:271/*
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 10:13:2215#include <memory>
deadbeefcbecd352015-09-23 18:50:2716#include <string>
17#include <vector>
18
Honghai Zhang7fb69db2016-03-14 18:59:1819#include "webrtc/base/asyncinvoker.h"
deadbeefcbecd352015-09-23 18:50:2720#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:
34 TransportController(rtc::Thread* signaling_thread,
Danil Chapovalov7f216b72016-05-12 07:20:3135 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 18:50:2736 PortAllocator* port_allocator);
37
38 virtual ~TransportController();
39
40 rtc::Thread* signaling_thread() const { return signaling_thread_; }
Danil Chapovalov7f216b72016-05-12 07:20:3141 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 18:50:2742
43 PortAllocator* port_allocator() const { return port_allocator_; }
44
45 // Can only be set before transports are created.
46 // TODO(deadbeef): Make this an argument to the constructor once BaseSession
47 // and WebRtcSession are combined
48 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version);
49
honghaiz1f429e32015-09-28 14:57:3450 void SetIceConfig(const IceConfig& config);
deadbeefcbecd352015-09-23 18:50:2751 void SetIceRole(IceRole ice_role);
52
Taylor Brandstetterf475d3652016-01-08 23:35:5753 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role);
deadbeefcbecd352015-09-23 18:50:2754
55 // Specifies the identity to use in this session.
56 // Can only be called once.
57 bool SetLocalCertificate(
58 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
59 bool GetLocalCertificate(
60 const std::string& transport_name,
61 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
62 // Caller owns returned certificate
jbauch555604a2016-04-26 10:13:2263 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 12:15:0664 const std::string& transport_name);
deadbeefcbecd352015-09-23 18:50:2765 bool SetLocalTransportDescription(const std::string& transport_name,
66 const TransportDescription& tdesc,
67 ContentAction action,
68 std::string* err);
69 bool SetRemoteTransportDescription(const std::string& transport_name,
70 const TransportDescription& tdesc,
71 ContentAction action,
72 std::string* err);
73 // Start gathering candidates for any new transports, or transports doing an
74 // ICE restart.
75 void MaybeStartGathering();
76 bool AddRemoteCandidates(const std::string& transport_name,
77 const Candidates& candidates,
78 std::string* err);
Honghai Zhang7fb69db2016-03-14 18:59:1879 bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err);
deadbeefcbecd352015-09-23 18:50:2780 bool ReadyForRemoteCandidates(const std::string& transport_name);
81 bool GetStats(const std::string& transport_name, TransportStats* stats);
82
Taylor Brandstetterc4d3a5d2015-09-30 17:32:5983 // Creates a channel if it doesn't exist. Otherwise, increments a reference
84 // count and returns an existing channel.
Danil Chapovalov7f216b72016-05-12 07:20:3185 virtual TransportChannel* CreateTransportChannel_n(
deadbeefcbecd352015-09-23 18:50:2786 const std::string& transport_name,
87 int component);
Taylor Brandstetterc4d3a5d2015-09-30 17:32:5988
89 // Decrements a channel's reference count, and destroys the channel if
90 // nothing is referencing it.
Danil Chapovalov7f216b72016-05-12 07:20:3191 virtual void DestroyTransportChannel_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 18:50:2792 int component);
93
mikescarlette7748672016-04-30 03:20:5494 void use_quic() { quic_ = true; }
95 bool quic() const { return quic_; }
96
deadbeefcbecd352015-09-23 18:50:2797 // All of these signals are fired on the signalling thread.
98
99 // If any transport failed => failed,
100 // Else if all completed => completed,
101 // Else if all connected => connected,
102 // Else => connecting
103 sigslot::signal1<IceConnectionState> SignalConnectionState;
104
105 // Receiving if any transport is receiving
106 sigslot::signal1<bool> SignalReceiving;
107
108 // If all transports done gathering => complete,
109 // Else if any are gathering => gathering,
110 // Else => new
111 sigslot::signal1<IceGatheringState> SignalGatheringState;
112
113 // (transport_name, candidates)
114 sigslot::signal2<const std::string&, const Candidates&>
115 SignalCandidatesGathered;
116
Honghai Zhang7fb69db2016-03-14 18:59:18117 sigslot::signal1<const Candidates&> SignalCandidatesRemoved;
118
deadbeefcbecd352015-09-23 18:50:27119 // for unit test
120 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
121
122 protected:
123 // Protected and virtual so we can override it in unit tests.
Danil Chapovalov7f216b72016-05-12 07:20:31124 virtual Transport* CreateTransport_n(const std::string& transport_name);
deadbeefcbecd352015-09-23 18:50:27125
126 // For unit tests
127 const std::map<std::string, Transport*>& transports() { return transports_; }
Danil Chapovalov7f216b72016-05-12 07:20:31128 Transport* GetTransport_n(const std::string& transport_name);
deadbeefcbecd352015-09-23 18:50:27129
130 private:
131 void OnMessage(rtc::Message* pmsg) override;
132
Taylor Brandstetterc4d3a5d2015-09-30 17:32:59133 // It's the Transport that's currently responsible for creating/destroying
134 // channels, but the TransportController keeps track of how many external
135 // objects (BaseChannels) reference each channel.
136 struct RefCountedChannel {
137 RefCountedChannel() : impl_(nullptr), ref_(0) {}
138 explicit RefCountedChannel(TransportChannelImpl* impl)
139 : impl_(impl), ref_(0) {}
140
141 void AddRef() { ++ref_; }
142 void DecRef() {
143 ASSERT(ref_ > 0);
144 --ref_;
145 }
146 int ref() const { return ref_; }
147
148 TransportChannelImpl* get() const { return impl_; }
149 TransportChannelImpl* operator->() const { return impl_; }
150
151 private:
152 TransportChannelImpl* impl_;
153 int ref_;
154 };
155
Danil Chapovalov7f216b72016-05-12 07:20:31156 std::vector<RefCountedChannel>::iterator FindChannel_n(
Taylor Brandstetterc4d3a5d2015-09-30 17:32:59157 const std::string& transport_name,
158 int component);
159
Danil Chapovalov7f216b72016-05-12 07:20:31160 Transport* GetOrCreateTransport_n(const std::string& transport_name);
161 void DestroyTransport_n(const std::string& transport_name);
162 void DestroyAllTransports_n();
deadbeefcbecd352015-09-23 18:50:27163
Danil Chapovalov7f216b72016-05-12 07:20:31164 bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version);
165 void SetIceConfig_n(const IceConfig& config);
166 void SetIceRole_n(IceRole ice_role);
167 bool GetSslRole_n(const std::string& transport_name, rtc::SSLRole* role);
168 bool SetLocalCertificate_n(
deadbeefcbecd352015-09-23 18:50:27169 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
Danil Chapovalov7f216b72016-05-12 07:20:31170 bool GetLocalCertificate_n(
deadbeefcbecd352015-09-23 18:50:27171 const std::string& transport_name,
172 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
Danil Chapovalov7f216b72016-05-12 07:20:31173 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n(
kwibergb4d01c42016-04-06 12:15:06174 const std::string& transport_name);
Danil Chapovalov7f216b72016-05-12 07:20:31175 bool SetLocalTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 18:50:27176 const TransportDescription& tdesc,
177 ContentAction action,
178 std::string* err);
Danil Chapovalov7f216b72016-05-12 07:20:31179 bool SetRemoteTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 18:50:27180 const TransportDescription& tdesc,
181 ContentAction action,
182 std::string* err);
Danil Chapovalov7f216b72016-05-12 07:20:31183 void MaybeStartGathering_n();
184 bool AddRemoteCandidates_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 18:50:27185 const Candidates& candidates,
186 std::string* err);
Danil Chapovalov7f216b72016-05-12 07:20:31187 bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err);
188 bool ReadyForRemoteCandidates_n(const std::string& transport_name);
189 bool GetStats_n(const std::string& transport_name, TransportStats* stats);
deadbeefcbecd352015-09-23 18:50:27190
191 // Handlers for signals from Transport.
Danil Chapovalov7f216b72016-05-12 07:20:31192 void OnChannelWritableState_n(TransportChannel* channel);
193 void OnChannelReceivingState_n(TransportChannel* channel);
194 void OnChannelGatheringState_n(TransportChannelImpl* channel);
195 void OnChannelCandidateGathered_n(TransportChannelImpl* channel,
Taylor Brandstetterc4d3a5d2015-09-30 17:32:59196 const Candidate& candidate);
Honghai Zhang7fb69db2016-03-14 18:59:18197 void OnChannelCandidatesRemoved(const Candidates& candidates);
Danil Chapovalov7f216b72016-05-12 07:20:31198 void OnChannelCandidatesRemoved_n(TransportChannelImpl* channel,
Honghai Zhang7fb69db2016-03-14 18:59:18199 const Candidates& candidates);
Danil Chapovalov7f216b72016-05-12 07:20:31200 void OnChannelRoleConflict_n(TransportChannelImpl* channel);
Honghai Zhang1590c392016-05-24 20:15:02201 void OnChannelStateChanged_n(TransportChannelImpl* channel);
deadbeefcbecd352015-09-23 18:50:27202
Danil Chapovalov7f216b72016-05-12 07:20:31203 void UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 18:50:27204
205 rtc::Thread* const signaling_thread_ = nullptr;
Danil Chapovalov7f216b72016-05-12 07:20:31206 rtc::Thread* const network_thread_ = nullptr;
deadbeefcbecd352015-09-23 18:50:27207 typedef std::map<std::string, Transport*> TransportMap;
208 TransportMap transports_;
209
Taylor Brandstetterc4d3a5d2015-09-30 17:32:59210 std::vector<RefCountedChannel> channels_;
211
deadbeefcbecd352015-09-23 18:50:27212 PortAllocator* const port_allocator_ = nullptr;
Guo-wei Shieha7446d22016-01-11 23:27:03213 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefcbecd352015-09-23 18:50:27214
Taylor Brandstetterc4d3a5d2015-09-30 17:32:59215 // Aggregate state for TransportChannelImpls.
deadbeefcbecd352015-09-23 18:50:27216 IceConnectionState connection_state_ = kIceConnectionConnecting;
217 bool receiving_ = false;
218 IceGatheringState gathering_state_ = kIceGatheringNew;
219
220 // TODO(deadbeef): Move the fields below down to the transports themselves
honghaiz1f429e32015-09-28 14:57:34221 IceConfig ice_config_;
deadbeefcbecd352015-09-23 18:50:27222 IceRole ice_role_ = ICEROLE_CONTROLLING;
Peter Boström0c4e06b2015-10-07 10:23:21223 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
deadbeefcbecd352015-09-23 18:50:27224 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
Honghai Zhang7fb69db2016-03-14 18:59:18225 rtc::AsyncInvoker invoker_;
mikescarlette7748672016-04-30 03:20:54226 // True if QUIC is used instead of DTLS.
227 bool quic_ = false;
deadbeefcbecd352015-09-23 18:50:27228};
229
230} // namespace cricket
231
232#endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_