blob: 534f117186531e5a72b4f9ac12483c5869878714 [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
Zhi Huangb5261582017-09-29 10:51:43 -070011#ifndef PC_TRANSPORTCONTROLLER_H_
12#define PC_TRANSPORTCONTROLLER_H_
deadbeefcbecd352015-09-23 11:50:27 -070013
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
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "p2p/base/dtlstransport.h"
21#include "p2p/base/jseptransport.h"
22#include "p2p/base/p2ptransportchannel.h"
23#include "rtc_base/asyncinvoker.h"
24#include "rtc_base/constructormagic.h"
25#include "rtc_base/refcountedobject.h"
26#include "rtc_base/sigslot.h"
27#include "rtc_base/sslstreamadapter.h"
deadbeefcbecd352015-09-23 11:50:27 -070028
29namespace rtc {
30class Thread;
deadbeef5bd5ca32017-02-10 11:31:50 -080031class PacketTransportInternal;
Zhi Huangb5261582017-09-29 10:51:43 -070032} // namespace rtc
33
Honghai Zhangd93f50c2016-10-05 11:47:22 -070034namespace webrtc {
35class MetricsObserverInterface;
Zhi Huangb5261582017-09-29 10:51:43 -070036} // namespace webrtc
deadbeefcbecd352015-09-23 11:50:27 -070037
38namespace cricket {
39
40class TransportController : public sigslot::has_slots<>,
41 public rtc::MessageHandler {
42 public:
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070043 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
44 // upon setting a local transport description that indicates an ICE restart.
45 // For the constructor that doesn't take this parameter, it defaults to true.
deadbeef7914b8c2017-04-21 03:23:33 -070046 //
47 // |crypto_options| is used to determine if created DTLS transports negotiate
48 // GCM crypto suites or not.
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070049 TransportController(rtc::Thread* signaling_thread,
50 rtc::Thread* network_thread,
51 PortAllocator* port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -070052 bool redetermine_role_on_ice_restart,
53 const rtc::CryptoOptions& crypto_options);
deadbeefcbecd352015-09-23 11:50:27 -070054
55 virtual ~TransportController();
56
57 rtc::Thread* signaling_thread() const { return signaling_thread_; }
Danil Chapovalov7f216b72016-05-12 09:20:31 +020058 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070059
60 PortAllocator* port_allocator() const { return port_allocator_; }
61
62 // Can only be set before transports are created.
63 // TODO(deadbeef): Make this an argument to the constructor once BaseSession
64 // and WebRtcSession are combined
65 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version);
66
honghaiz1f429e32015-09-28 07:57:34 -070067 void SetIceConfig(const IceConfig& config);
deadbeefcbecd352015-09-23 11:50:27 -070068 void SetIceRole(IceRole ice_role);
69
deadbeefd1a38b52016-12-10 13:15:33 -080070 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
71 // set, offers should generate new ufrags/passwords until an ICE restart
72 // occurs.
73 void SetNeedsIceRestartFlag();
74 // Returns true if the ICE restart flag above was set, and no ICE restart has
75 // occurred yet for this transport (by applying a local description with
76 // changed ufrag/password). If the transport has been deleted as a result of
77 // bundling, returns false.
78 bool NeedsIceRestart(const std::string& transport_name) const;
79
deadbeef49f34fd2016-12-06 16:22:06 -080080 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role) const;
deadbeefcbecd352015-09-23 11:50:27 -070081
82 // Specifies the identity to use in this session.
83 // Can only be called once.
84 bool SetLocalCertificate(
85 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
86 bool GetLocalCertificate(
87 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -080088 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const;
89 // Caller owns returned certificate. This method mainly exists for stats
90 // reporting.
jbauch555604a2016-04-26 03:13:22 -070091 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
deadbeef49f34fd2016-12-06 16:22:06 -080092 const std::string& transport_name) const;
deadbeefcbecd352015-09-23 11:50:27 -070093 bool SetLocalTransportDescription(const std::string& transport_name,
94 const TransportDescription& tdesc,
95 ContentAction action,
96 std::string* err);
97 bool SetRemoteTransportDescription(const std::string& transport_name,
98 const TransportDescription& tdesc,
99 ContentAction action,
100 std::string* err);
101 // Start gathering candidates for any new transports, or transports doing an
102 // ICE restart.
103 void MaybeStartGathering();
104 bool AddRemoteCandidates(const std::string& transport_name,
105 const Candidates& candidates,
106 std::string* err);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700107 bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err);
deadbeef49f34fd2016-12-06 16:22:06 -0800108 bool ReadyForRemoteCandidates(const std::string& transport_name) const;
109 // TODO(deadbeef): GetStats isn't const because all the way down to
110 // OpenSSLStreamAdapter,
111 // GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not const. Fix this.
deadbeefcbecd352015-09-23 11:50:27 -0700112 bool GetStats(const std::string& transport_name, TransportStats* stats);
deadbeef49f34fd2016-12-06 16:22:06 -0800113 void SetMetricsObserver(webrtc::MetricsObserverInterface* metrics_observer);
deadbeefcbecd352015-09-23 11:50:27 -0700114
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700115 // Creates a channel if it doesn't exist. Otherwise, increments a reference
116 // count and returns an existing channel.
zhihuangb2cdd932017-01-19 16:54:25 -0800117 DtlsTransportInternal* CreateDtlsTransport(const std::string& transport_name,
118 int component);
119 virtual DtlsTransportInternal* CreateDtlsTransport_n(
deadbeefcbecd352015-09-23 11:50:27 -0700120 const std::string& transport_name,
121 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700122
123 // Decrements a channel's reference count, and destroys the channel if
124 // nothing is referencing it.
zhihuangb2cdd932017-01-19 16:54:25 -0800125 virtual void DestroyDtlsTransport(const std::string& transport_name,
126 int component);
127 virtual void DestroyDtlsTransport_n(const std::string& transport_name,
128 int component);
deadbeefcbecd352015-09-23 11:50:27 -0700129
deadbeef49f34fd2016-12-06 16:22:06 -0800130 // TODO(deadbeef): Remove all for_testing methods!
131 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing()
132 const {
133 return certificate_;
134 }
135 std::vector<std::string> transport_names_for_testing();
zhihuangb2cdd932017-01-19 16:54:25 -0800136 std::vector<DtlsTransportInternal*> channels_for_testing();
137 DtlsTransportInternal* get_channel_for_testing(
deadbeef49f34fd2016-12-06 16:22:06 -0800138 const std::string& transport_name,
139 int component);
140
deadbeefcbecd352015-09-23 11:50:27 -0700141 // All of these signals are fired on the signalling thread.
142
143 // If any transport failed => failed,
144 // Else if all completed => completed,
145 // Else if all connected => connected,
146 // Else => connecting
147 sigslot::signal1<IceConnectionState> SignalConnectionState;
148
149 // Receiving if any transport is receiving
150 sigslot::signal1<bool> SignalReceiving;
151
152 // If all transports done gathering => complete,
153 // Else if any are gathering => gathering,
154 // Else => new
155 sigslot::signal1<IceGatheringState> SignalGatheringState;
156
157 // (transport_name, candidates)
158 sigslot::signal2<const std::string&, const Candidates&>
159 SignalCandidatesGathered;
160
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700161 sigslot::signal1<const Candidates&> SignalCandidatesRemoved;
162
zhihuangd82eee02016-08-26 11:25:05 -0700163 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
164
deadbeefcbecd352015-09-23 11:50:27 -0700165 protected:
deadbeef49f34fd2016-12-06 16:22:06 -0800166 // TODO(deadbeef): Get rid of these virtual methods. Used by
167 // FakeTransportController currently, but FakeTransportController shouldn't
168 // even be functioning by subclassing TransportController.
zhihuangd06adf62017-01-12 15:58:31 -0800169 virtual IceTransportInternal* CreateIceTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800170 const std::string& transport_name,
171 int component);
zhihuangb2cdd932017-01-19 16:54:25 -0800172 virtual DtlsTransportInternal* CreateDtlsTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800173 const std::string& transport_name,
174 int component,
zhihuangd06adf62017-01-12 15:58:31 -0800175 IceTransportInternal* ice);
deadbeefcbecd352015-09-23 11:50:27 -0700176
177 private:
178 void OnMessage(rtc::Message* pmsg) override;
179
deadbeef62802a12016-12-13 16:38:36 -0800180 class ChannelPair;
181 typedef rtc::RefCountedObject<ChannelPair> RefCountedChannel;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700182
deadbeef49f34fd2016-12-06 16:22:06 -0800183 // Helper functions to get a channel or transport, or iterator to it (in case
184 // it needs to be erased).
deadbeef62802a12016-12-13 16:38:36 -0800185 std::vector<RefCountedChannel*>::iterator GetChannelIterator_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700186 const std::string& transport_name,
187 int component);
deadbeef62802a12016-12-13 16:38:36 -0800188 std::vector<RefCountedChannel*>::const_iterator GetChannelIterator_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800189 const std::string& transport_name,
190 int component) const;
deadbeefd1a38b52016-12-10 13:15:33 -0800191 const JsepTransport* GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800192 const std::string& transport_name) const;
deadbeefd1a38b52016-12-10 13:15:33 -0800193 JsepTransport* GetJsepTransport(const std::string& transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800194 const RefCountedChannel* GetChannel_n(const std::string& transport_name,
195 int component) const;
196 RefCountedChannel* GetChannel_n(const std::string& transport_name,
197 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700198
deadbeefd1a38b52016-12-10 13:15:33 -0800199 JsepTransport* GetOrCreateJsepTransport(const std::string& transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800200 void DestroyAllChannels_n();
deadbeefcbecd352015-09-23 11:50:27 -0700201
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200202 bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version);
203 void SetIceConfig_n(const IceConfig& config);
204 void SetIceRole_n(IceRole ice_role);
deadbeef49f34fd2016-12-06 16:22:06 -0800205 bool GetSslRole_n(const std::string& transport_name,
206 rtc::SSLRole* role) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200207 bool SetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700208 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200209 bool GetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700210 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800211 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200212 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800213 const std::string& transport_name) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200214 bool SetLocalTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700215 const TransportDescription& tdesc,
216 ContentAction action,
217 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200218 bool SetRemoteTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700219 const TransportDescription& tdesc,
220 ContentAction action,
221 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200222 void MaybeStartGathering_n();
223 bool AddRemoteCandidates_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700224 const Candidates& candidates,
225 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200226 bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err);
deadbeef49f34fd2016-12-06 16:22:06 -0800227 bool ReadyForRemoteCandidates_n(const std::string& transport_name) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200228 bool GetStats_n(const std::string& transport_name, TransportStats* stats);
deadbeef49f34fd2016-12-06 16:22:06 -0800229 void SetMetricsObserver_n(webrtc::MetricsObserverInterface* metrics_observer);
deadbeefcbecd352015-09-23 11:50:27 -0700230
231 // Handlers for signals from Transport.
deadbeef5bd5ca32017-02-10 11:31:50 -0800232 void OnChannelWritableState_n(rtc::PacketTransportInternal* transport);
233 void OnChannelReceivingState_n(rtc::PacketTransportInternal* transport);
zhihuangb2cdd932017-01-19 16:54:25 -0800234 void OnChannelGatheringState_n(IceTransportInternal* channel);
235 void OnChannelCandidateGathered_n(IceTransportInternal* channel,
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700236 const Candidate& candidate);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700237 void OnChannelCandidatesRemoved(const Candidates& candidates);
zhihuangb2cdd932017-01-19 16:54:25 -0800238 void OnChannelCandidatesRemoved_n(IceTransportInternal* channel,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700239 const Candidates& candidates);
zhihuangb2cdd932017-01-19 16:54:25 -0800240 void OnChannelRoleConflict_n(IceTransportInternal* channel);
241 void OnChannelStateChanged_n(IceTransportInternal* channel);
deadbeefcbecd352015-09-23 11:50:27 -0700242
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200243 void UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700244
zhihuangd82eee02016-08-26 11:25:05 -0700245 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
246
deadbeefcbecd352015-09-23 11:50:27 -0700247 rtc::Thread* const signaling_thread_ = nullptr;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200248 rtc::Thread* const network_thread_ = nullptr;
deadbeef57fd7262016-12-06 15:28:55 -0800249 PortAllocator* const port_allocator_ = nullptr;
deadbeef49f34fd2016-12-06 16:22:06 -0800250
251 std::map<std::string, std::unique_ptr<JsepTransport>> transports_;
deadbeef62802a12016-12-13 16:38:36 -0800252 std::vector<RefCountedChannel*> channels_;
deadbeef57fd7262016-12-06 15:28:55 -0800253
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700254 // Aggregate state for TransportChannelImpls.
deadbeefcbecd352015-09-23 11:50:27 -0700255 IceConnectionState connection_state_ = kIceConnectionConnecting;
256 bool receiving_ = false;
257 IceGatheringState gathering_state_ = kIceGatheringNew;
258
honghaiz1f429e32015-09-28 07:57:34 -0700259 IceConfig ice_config_;
deadbeefcbecd352015-09-23 11:50:27 -0700260 IceRole ice_role_ = ICEROLE_CONTROLLING;
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700261 bool redetermine_role_on_ice_restart_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200262 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
deadbeef7914b8c2017-04-21 03:23:33 -0700263 rtc::CryptoOptions crypto_options_;
deadbeef49f34fd2016-12-06 16:22:06 -0800264 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefcbecd352015-09-23 11:50:27 -0700265 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700266 rtc::AsyncInvoker invoker_;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700267
268 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
deadbeef62802a12016-12-13 16:38:36 -0800269
270 RTC_DISALLOW_COPY_AND_ASSIGN(TransportController);
deadbeefcbecd352015-09-23 11:50:27 -0700271};
272
273} // namespace cricket
274
Zhi Huangb5261582017-09-29 10:51:43 -0700275#endif // PC_TRANSPORTCONTROLLER_H_