blob: 12ea94fcd447b82e718699950ed7c7b31aca6bac [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "p2p/base/transportcontroller.h"
deadbeefcbecd352015-09-23 11:50:27 -070012
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070013#include <algorithm>
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "p2p/base/port.h"
17#include "rtc_base/bind.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/thread.h"
deadbeefcbecd352015-09-23 11:50:27 -070020
deadbeef62802a12016-12-13 16:38:36 -080021namespace {
deadbeefcbecd352015-09-23 11:50:27 -070022
23enum {
24 MSG_ICECONNECTIONSTATE,
25 MSG_RECEIVING,
26 MSG_ICEGATHERINGSTATE,
27 MSG_CANDIDATESGATHERED,
28};
29
30struct CandidatesData : public rtc::MessageData {
31 CandidatesData(const std::string& transport_name,
deadbeef62802a12016-12-13 16:38:36 -080032 const cricket::Candidates& candidates)
deadbeefcbecd352015-09-23 11:50:27 -070033 : transport_name(transport_name), candidates(candidates) {}
34
35 std::string transport_name;
deadbeef62802a12016-12-13 16:38:36 -080036 cricket::Candidates candidates;
37};
38
39} // namespace {
40
41namespace cricket {
42
43// This class groups the DTLS and ICE channels, and helps keep track of
44// how many external objects (BaseChannels) reference each channel.
45class TransportController::ChannelPair {
46 public:
47 // TODO(deadbeef): Change the types of |dtls| and |ice| to
zhihuangb2cdd932017-01-19 16:54:25 -080048 // DtlsTransport and P2PTransportChannelWrapper, once TransportChannelImpl is
49 // removed.
50 ChannelPair(DtlsTransportInternal* dtls, IceTransportInternal* ice)
deadbeef62802a12016-12-13 16:38:36 -080051 : ice_(ice), dtls_(dtls) {}
52
53 // Currently, all ICE-related calls still go through this DTLS channel. But
54 // that will change once we get rid of TransportChannelImpl, and the DTLS
55 // channel interface no longer includes ICE-specific methods.
zhihuangb2cdd932017-01-19 16:54:25 -080056 const DtlsTransportInternal* dtls() const { return dtls_.get(); }
57 DtlsTransportInternal* dtls() { return dtls_.get(); }
zhihuangd06adf62017-01-12 15:58:31 -080058 const IceTransportInternal* ice() const { return ice_.get(); }
59 IceTransportInternal* ice() { return ice_.get(); }
deadbeef62802a12016-12-13 16:38:36 -080060
61 private:
zhihuangd06adf62017-01-12 15:58:31 -080062 std::unique_ptr<IceTransportInternal> ice_;
zhihuangb2cdd932017-01-19 16:54:25 -080063 std::unique_ptr<DtlsTransportInternal> dtls_;
deadbeef62802a12016-12-13 16:38:36 -080064
65 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelPair);
deadbeefcbecd352015-09-23 11:50:27 -070066};
67
deadbeef7914b8c2017-04-21 03:23:33 -070068TransportController::TransportController(
69 rtc::Thread* signaling_thread,
70 rtc::Thread* network_thread,
71 PortAllocator* port_allocator,
72 bool redetermine_role_on_ice_restart,
73 const rtc::CryptoOptions& crypto_options)
deadbeefcbecd352015-09-23 11:50:27 -070074 : signaling_thread_(signaling_thread),
Danil Chapovalov7f216b72016-05-12 09:20:31 +020075 network_thread_(network_thread),
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070076 port_allocator_(port_allocator),
deadbeef7914b8c2017-04-21 03:23:33 -070077 redetermine_role_on_ice_restart_(redetermine_role_on_ice_restart),
78 crypto_options_(crypto_options) {}
deadbeefcbecd352015-09-23 11:50:27 -070079
80TransportController::~TransportController() {
deadbeef49f34fd2016-12-06 16:22:06 -080081 // Channel destructors may try to send packets, so this needs to happen on
82 // the network thread.
Danil Chapovalov7f216b72016-05-12 09:20:31 +020083 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070084 RTC_FROM_HERE,
deadbeef49f34fd2016-12-06 16:22:06 -080085 rtc::Bind(&TransportController::DestroyAllChannels_n, this));
deadbeefcbecd352015-09-23 11:50:27 -070086}
87
88bool TransportController::SetSslMaxProtocolVersion(
89 rtc::SSLProtocolVersion version) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070090 return network_thread_->Invoke<bool>(
91 RTC_FROM_HERE, rtc::Bind(&TransportController::SetSslMaxProtocolVersion_n,
92 this, version));
deadbeefcbecd352015-09-23 11:50:27 -070093}
94
honghaiz1f429e32015-09-28 07:57:34 -070095void TransportController::SetIceConfig(const IceConfig& config) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +020096 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070097 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +020098 rtc::Bind(&TransportController::SetIceConfig_n, this, config));
deadbeefcbecd352015-09-23 11:50:27 -070099}
100
101void TransportController::SetIceRole(IceRole ice_role) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200102 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700103 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200104 rtc::Bind(&TransportController::SetIceRole_n, this, ice_role));
deadbeefcbecd352015-09-23 11:50:27 -0700105}
106
deadbeefd1a38b52016-12-10 13:15:33 -0800107void TransportController::SetNeedsIceRestartFlag() {
108 for (auto& kv : transports_) {
109 kv.second->SetNeedsIceRestartFlag();
110 }
111}
112
113bool TransportController::NeedsIceRestart(
114 const std::string& transport_name) const {
115 const JsepTransport* transport = GetJsepTransport(transport_name);
116 if (!transport) {
117 return false;
118 }
119 return transport->NeedsIceRestart();
120}
121
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800122bool TransportController::GetSslRole(const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800123 rtc::SSLRole* role) const {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700124 return network_thread_->Invoke<bool>(
125 RTC_FROM_HERE, rtc::Bind(&TransportController::GetSslRole_n, this,
126 transport_name, role));
deadbeefcbecd352015-09-23 11:50:27 -0700127}
128
129bool TransportController::SetLocalCertificate(
130 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700131 return network_thread_->Invoke<bool>(
132 RTC_FROM_HERE, rtc::Bind(&TransportController::SetLocalCertificate_n,
133 this, certificate));
deadbeefcbecd352015-09-23 11:50:27 -0700134}
135
136bool TransportController::GetLocalCertificate(
137 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800138 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const {
hbosdf6075a2016-12-19 04:58:02 -0800139 if (network_thread_->IsCurrent()) {
140 return GetLocalCertificate_n(transport_name, certificate);
141 }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200142 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700143 RTC_FROM_HERE, rtc::Bind(&TransportController::GetLocalCertificate_n,
144 this, transport_name, certificate));
deadbeefcbecd352015-09-23 11:50:27 -0700145}
146
jbauch555604a2016-04-26 03:13:22 -0700147std::unique_ptr<rtc::SSLCertificate>
kwibergb4d01c42016-04-06 05:15:06 -0700148TransportController::GetRemoteSSLCertificate(
deadbeef49f34fd2016-12-06 16:22:06 -0800149 const std::string& transport_name) const {
hbosdf6075a2016-12-19 04:58:02 -0800150 if (network_thread_->IsCurrent()) {
151 return GetRemoteSSLCertificate_n(transport_name);
152 }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200153 return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertificate>>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700154 RTC_FROM_HERE, rtc::Bind(&TransportController::GetRemoteSSLCertificate_n,
155 this, transport_name));
deadbeefcbecd352015-09-23 11:50:27 -0700156}
157
158bool TransportController::SetLocalTransportDescription(
159 const std::string& transport_name,
160 const TransportDescription& tdesc,
161 ContentAction action,
162 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200163 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700164 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200165 rtc::Bind(&TransportController::SetLocalTransportDescription_n, this,
deadbeefcbecd352015-09-23 11:50:27 -0700166 transport_name, tdesc, action, err));
167}
168
169bool TransportController::SetRemoteTransportDescription(
170 const std::string& transport_name,
171 const TransportDescription& tdesc,
172 ContentAction action,
173 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200174 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700175 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200176 rtc::Bind(&TransportController::SetRemoteTransportDescription_n, this,
deadbeefcbecd352015-09-23 11:50:27 -0700177 transport_name, tdesc, action, err));
178}
179
180void TransportController::MaybeStartGathering() {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200181 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700182 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200183 rtc::Bind(&TransportController::MaybeStartGathering_n, this));
deadbeefcbecd352015-09-23 11:50:27 -0700184}
185
186bool TransportController::AddRemoteCandidates(const std::string& transport_name,
187 const Candidates& candidates,
188 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200189 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700190 RTC_FROM_HERE, rtc::Bind(&TransportController::AddRemoteCandidates_n,
191 this, transport_name, candidates, err));
deadbeefcbecd352015-09-23 11:50:27 -0700192}
193
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700194bool TransportController::RemoveRemoteCandidates(const Candidates& candidates,
195 std::string* err) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700196 return network_thread_->Invoke<bool>(
197 RTC_FROM_HERE, rtc::Bind(&TransportController::RemoveRemoteCandidates_n,
198 this, candidates, err));
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700199}
200
deadbeefcbecd352015-09-23 11:50:27 -0700201bool TransportController::ReadyForRemoteCandidates(
deadbeef49f34fd2016-12-06 16:22:06 -0800202 const std::string& transport_name) const {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700203 return network_thread_->Invoke<bool>(
204 RTC_FROM_HERE, rtc::Bind(&TransportController::ReadyForRemoteCandidates_n,
205 this, transport_name));
deadbeefcbecd352015-09-23 11:50:27 -0700206}
207
208bool TransportController::GetStats(const std::string& transport_name,
209 TransportStats* stats) {
hbosdf6075a2016-12-19 04:58:02 -0800210 if (network_thread_->IsCurrent()) {
211 return GetStats_n(transport_name, stats);
212 }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200213 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700214 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200215 rtc::Bind(&TransportController::GetStats_n, this, transport_name, stats));
deadbeefcbecd352015-09-23 11:50:27 -0700216}
217
deadbeef49f34fd2016-12-06 16:22:06 -0800218void TransportController::SetMetricsObserver(
219 webrtc::MetricsObserverInterface* metrics_observer) {
220 return network_thread_->Invoke<void>(
221 RTC_FROM_HERE, rtc::Bind(&TransportController::SetMetricsObserver_n, this,
222 metrics_observer));
223}
224
zhihuangb2cdd932017-01-19 16:54:25 -0800225DtlsTransportInternal* TransportController::CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -0800226 const std::string& transport_name,
227 int component) {
zhihuangb2cdd932017-01-19 16:54:25 -0800228 return network_thread_->Invoke<DtlsTransportInternal*>(
229 RTC_FROM_HERE, rtc::Bind(&TransportController::CreateDtlsTransport_n,
zhihuangf5b251b2017-01-12 19:37:48 -0800230 this, transport_name, component));
231}
232
zhihuangb2cdd932017-01-19 16:54:25 -0800233DtlsTransportInternal* TransportController::CreateDtlsTransport_n(
deadbeefcbecd352015-09-23 11:50:27 -0700234 const std::string& transport_name,
235 int component) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200236 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700237
deadbeef49f34fd2016-12-06 16:22:06 -0800238 RefCountedChannel* existing_channel = GetChannel_n(transport_name, component);
239 if (existing_channel) {
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700240 // Channel already exists; increment reference count and return.
deadbeef49f34fd2016-12-06 16:22:06 -0800241 existing_channel->AddRef();
242 return existing_channel->dtls();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700243 }
244
245 // Need to create a new channel.
deadbeefd1a38b52016-12-10 13:15:33 -0800246 JsepTransport* transport = GetOrCreateJsepTransport(transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800247
248 // Create DTLS channel wrapping ICE channel, and configure it.
zhihuangd06adf62017-01-12 15:58:31 -0800249 IceTransportInternal* ice =
deadbeef49f34fd2016-12-06 16:22:06 -0800250 CreateIceTransportChannel_n(transport_name, component);
251 // TODO(deadbeef): To support QUIC, would need to create a
252 // QuicTransportChannel here. What is "dtls" in this file would then become
253 // "dtls or quic".
zhihuangb2cdd932017-01-19 16:54:25 -0800254 DtlsTransportInternal* dtls =
deadbeef49f34fd2016-12-06 16:22:06 -0800255 CreateDtlsTransportChannel_n(transport_name, component, ice);
zhihuangb2cdd932017-01-19 16:54:25 -0800256 dtls->ice_transport()->SetMetricsObserver(metrics_observer_);
257 dtls->ice_transport()->SetIceRole(ice_role_);
258 dtls->ice_transport()->SetIceTiebreaker(ice_tiebreaker_);
259 dtls->ice_transport()->SetIceConfig(ice_config_);
zhihuangb19012e2017-09-19 13:47:59 -0700260 if (certificate_) {
261 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
262 RTC_DCHECK(set_cert_success);
263 }
deadbeef49f34fd2016-12-06 16:22:06 -0800264
265 // Connect to signals offered by the channels. Currently, the DTLS channel
266 // forwards signals from the ICE channel, so we only need to connect to the
267 // DTLS channel. In the future this won't be the case.
268 dtls->SignalWritableState.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200269 this, &TransportController::OnChannelWritableState_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800270 dtls->SignalReceivingState.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200271 this, &TransportController::OnChannelReceivingState_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800272 dtls->SignalDtlsHandshakeError.connect(
zhihuangd82eee02016-08-26 11:25:05 -0700273 this, &TransportController::OnDtlsHandshakeError);
zhihuangb2cdd932017-01-19 16:54:25 -0800274 dtls->ice_transport()->SignalGatheringState.connect(
275 this, &TransportController::OnChannelGatheringState_n);
276 dtls->ice_transport()->SignalCandidateGathered.connect(
277 this, &TransportController::OnChannelCandidateGathered_n);
278 dtls->ice_transport()->SignalCandidatesRemoved.connect(
279 this, &TransportController::OnChannelCandidatesRemoved_n);
280 dtls->ice_transport()->SignalRoleConflict.connect(
281 this, &TransportController::OnChannelRoleConflict_n);
282 dtls->ice_transport()->SignalStateChanged.connect(
283 this, &TransportController::OnChannelStateChanged_n);
deadbeef62802a12016-12-13 16:38:36 -0800284 RefCountedChannel* new_pair = new RefCountedChannel(dtls, ice);
285 new_pair->AddRef();
286 channels_.insert(channels_.end(), new_pair);
deadbeef49f34fd2016-12-06 16:22:06 -0800287 bool channel_added = transport->AddChannel(dtls, component);
288 RTC_DCHECK(channel_added);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700289 // Adding a channel could cause aggregate state to change.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200290 UpdateAggregateStates_n();
deadbeef49f34fd2016-12-06 16:22:06 -0800291 return dtls;
deadbeefcbecd352015-09-23 11:50:27 -0700292}
293
zhihuangb2cdd932017-01-19 16:54:25 -0800294void TransportController::DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -0800295 const std::string& transport_name,
296 int component) {
297 network_thread_->Invoke<void>(
zhihuangb2cdd932017-01-19 16:54:25 -0800298 RTC_FROM_HERE, rtc::Bind(&TransportController::DestroyDtlsTransport_n,
deadbeefbad5dad2017-01-17 18:32:35 -0800299 this, transport_name, component));
300}
301
zhihuangb2cdd932017-01-19 16:54:25 -0800302void TransportController::DestroyDtlsTransport_n(
deadbeefcbecd352015-09-23 11:50:27 -0700303 const std::string& transport_name,
304 int component) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200305 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800306 auto it = GetChannelIterator_n(transport_name, component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700307 if (it == channels_.end()) {
308 LOG(LS_WARNING) << "Attempting to delete " << transport_name
309 << " TransportChannel " << component
310 << ", which doesn't exist.";
deadbeefcbecd352015-09-23 11:50:27 -0700311 return;
312 }
deadbeef62802a12016-12-13 16:38:36 -0800313 if ((*it)->Release() > 0) {
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700314 return;
315 }
deadbeef57fd7262016-12-06 15:28:55 -0800316 channels_.erase(it);
deadbeef49f34fd2016-12-06 16:22:06 -0800317
deadbeefd1a38b52016-12-10 13:15:33 -0800318 JsepTransport* t = GetJsepTransport(transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800319 bool channel_removed = t->RemoveChannel(component);
320 RTC_DCHECK(channel_removed);
deadbeefcbecd352015-09-23 11:50:27 -0700321 // Just as we create a Transport when its first channel is created,
322 // we delete it when its last channel is deleted.
deadbeef49f34fd2016-12-06 16:22:06 -0800323 if (!t->HasChannels()) {
324 transports_.erase(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700325 }
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700326 // Removing a channel could cause aggregate state to change.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200327 UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700328}
329
deadbeef49f34fd2016-12-06 16:22:06 -0800330std::vector<std::string> TransportController::transport_names_for_testing() {
331 std::vector<std::string> ret;
332 for (const auto& kv : transports_) {
333 ret.push_back(kv.first);
mikescarlette7748672016-04-29 20:20:54 -0700334 }
deadbeef49f34fd2016-12-06 16:22:06 -0800335 return ret;
deadbeefcbecd352015-09-23 11:50:27 -0700336}
337
zhihuangb2cdd932017-01-19 16:54:25 -0800338std::vector<DtlsTransportInternal*>
339TransportController::channels_for_testing() {
340 std::vector<DtlsTransportInternal*> ret;
deadbeef62802a12016-12-13 16:38:36 -0800341 for (RefCountedChannel* channel : channels_) {
342 ret.push_back(channel->dtls());
deadbeef49f34fd2016-12-06 16:22:06 -0800343 }
344 return ret;
345}
deadbeefcbecd352015-09-23 11:50:27 -0700346
zhihuangb2cdd932017-01-19 16:54:25 -0800347DtlsTransportInternal* TransportController::get_channel_for_testing(
deadbeef49f34fd2016-12-06 16:22:06 -0800348 const std::string& transport_name,
349 int component) {
350 RefCountedChannel* ch = GetChannel_n(transport_name, component);
351 return ch ? ch->dtls() : nullptr;
352}
353
zhihuangd06adf62017-01-12 15:58:31 -0800354IceTransportInternal* TransportController::CreateIceTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800355 const std::string& transport_name,
356 int component) {
357 return new P2PTransportChannel(transport_name, component, port_allocator_);
358}
359
zhihuangb2cdd932017-01-19 16:54:25 -0800360DtlsTransportInternal* TransportController::CreateDtlsTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800361 const std::string&,
362 int,
zhihuangd06adf62017-01-12 15:58:31 -0800363 IceTransportInternal* ice) {
deadbeef7914b8c2017-04-21 03:23:33 -0700364 DtlsTransport* dtls = new DtlsTransport(ice, crypto_options_);
deadbeef49f34fd2016-12-06 16:22:06 -0800365 dtls->SetSslMaxProtocolVersion(ssl_max_version_);
366 return dtls;
deadbeefcbecd352015-09-23 11:50:27 -0700367}
368
369void TransportController::OnMessage(rtc::Message* pmsg) {
370 RTC_DCHECK(signaling_thread_->IsCurrent());
371
372 switch (pmsg->message_id) {
373 case MSG_ICECONNECTIONSTATE: {
374 rtc::TypedMessageData<IceConnectionState>* data =
375 static_cast<rtc::TypedMessageData<IceConnectionState>*>(pmsg->pdata);
376 SignalConnectionState(data->data());
377 delete data;
378 break;
379 }
380 case MSG_RECEIVING: {
381 rtc::TypedMessageData<bool>* data =
382 static_cast<rtc::TypedMessageData<bool>*>(pmsg->pdata);
383 SignalReceiving(data->data());
384 delete data;
385 break;
386 }
387 case MSG_ICEGATHERINGSTATE: {
388 rtc::TypedMessageData<IceGatheringState>* data =
389 static_cast<rtc::TypedMessageData<IceGatheringState>*>(pmsg->pdata);
390 SignalGatheringState(data->data());
391 delete data;
392 break;
393 }
394 case MSG_CANDIDATESGATHERED: {
395 CandidatesData* data = static_cast<CandidatesData*>(pmsg->pdata);
396 SignalCandidatesGathered(data->transport_name, data->candidates);
397 delete data;
398 break;
399 }
400 default:
nissec80e7412017-01-11 05:56:46 -0800401 RTC_NOTREACHED();
deadbeefcbecd352015-09-23 11:50:27 -0700402 }
403}
404
deadbeef62802a12016-12-13 16:38:36 -0800405std::vector<TransportController::RefCountedChannel*>::iterator
deadbeef49f34fd2016-12-06 16:22:06 -0800406TransportController::GetChannelIterator_n(const std::string& transport_name,
407 int component) {
408 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef62802a12016-12-13 16:38:36 -0800409 return std::find_if(channels_.begin(), channels_.end(),
410 [transport_name, component](RefCountedChannel* channel) {
411 return channel->dtls()->transport_name() ==
412 transport_name &&
413 channel->dtls()->component() == component;
414 });
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700415}
416
deadbeef62802a12016-12-13 16:38:36 -0800417std::vector<TransportController::RefCountedChannel*>::const_iterator
deadbeef49f34fd2016-12-06 16:22:06 -0800418TransportController::GetChannelIterator_n(const std::string& transport_name,
419 int component) const {
420 RTC_DCHECK(network_thread_->IsCurrent());
421 return std::find_if(
422 channels_.begin(), channels_.end(),
deadbeef62802a12016-12-13 16:38:36 -0800423 [transport_name, component](const RefCountedChannel* channel) {
424 return channel->dtls()->transport_name() == transport_name &&
425 channel->dtls()->component() == component;
deadbeef49f34fd2016-12-06 16:22:06 -0800426 });
427}
428
deadbeefd1a38b52016-12-10 13:15:33 -0800429const JsepTransport* TransportController::GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800430 const std::string& transport_name) const {
deadbeef49f34fd2016-12-06 16:22:06 -0800431 auto it = transports_.find(transport_name);
432 return (it == transports_.end()) ? nullptr : it->second.get();
433}
434
deadbeefd1a38b52016-12-10 13:15:33 -0800435JsepTransport* TransportController::GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800436 const std::string& transport_name) {
deadbeef49f34fd2016-12-06 16:22:06 -0800437 auto it = transports_.find(transport_name);
438 return (it == transports_.end()) ? nullptr : it->second.get();
439}
440
441const TransportController::RefCountedChannel* TransportController::GetChannel_n(
442 const std::string& transport_name,
443 int component) const {
444 RTC_DCHECK(network_thread_->IsCurrent());
445 auto it = GetChannelIterator_n(transport_name, component);
deadbeef62802a12016-12-13 16:38:36 -0800446 return (it == channels_.end()) ? nullptr : *it;
deadbeef49f34fd2016-12-06 16:22:06 -0800447}
448
449TransportController::RefCountedChannel* TransportController::GetChannel_n(
450 const std::string& transport_name,
451 int component) {
452 RTC_DCHECK(network_thread_->IsCurrent());
453 auto it = GetChannelIterator_n(transport_name, component);
deadbeef62802a12016-12-13 16:38:36 -0800454 return (it == channels_.end()) ? nullptr : *it;
deadbeef49f34fd2016-12-06 16:22:06 -0800455}
456
deadbeefd1a38b52016-12-10 13:15:33 -0800457JsepTransport* TransportController::GetOrCreateJsepTransport(
deadbeefcbecd352015-09-23 11:50:27 -0700458 const std::string& transport_name) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200459 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700460
deadbeefd1a38b52016-12-10 13:15:33 -0800461 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700462 if (transport) {
463 return transport;
464 }
465
deadbeef49f34fd2016-12-06 16:22:06 -0800466 transport = new JsepTransport(transport_name, certificate_);
467 transports_[transport_name] = std::unique_ptr<JsepTransport>(transport);
deadbeefcbecd352015-09-23 11:50:27 -0700468 return transport;
469}
470
deadbeef49f34fd2016-12-06 16:22:06 -0800471void TransportController::DestroyAllChannels_n() {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200472 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700473 transports_.clear();
deadbeef62802a12016-12-13 16:38:36 -0800474 for (RefCountedChannel* channel : channels_) {
475 // Even though these objects are normally ref-counted, if
476 // TransportController is deleted while they still have references, just
477 // remove all references.
478 while (channel->Release() > 0) {
479 }
480 }
deadbeef49f34fd2016-12-06 16:22:06 -0800481 channels_.clear();
deadbeefcbecd352015-09-23 11:50:27 -0700482}
483
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200484bool TransportController::SetSslMaxProtocolVersion_n(
deadbeefcbecd352015-09-23 11:50:27 -0700485 rtc::SSLProtocolVersion version) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200486 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700487
488 // Max SSL version can only be set before transports are created.
489 if (!transports_.empty()) {
490 return false;
491 }
492
493 ssl_max_version_ = version;
494 return true;
495}
496
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200497void TransportController::SetIceConfig_n(const IceConfig& config) {
498 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800499
honghaiz1f429e32015-09-28 07:57:34 -0700500 ice_config_ = config;
deadbeef49f34fd2016-12-06 16:22:06 -0800501 for (auto& channel : channels_) {
zhihuangb2cdd932017-01-19 16:54:25 -0800502 channel->dtls()->ice_transport()->SetIceConfig(ice_config_);
deadbeefcbecd352015-09-23 11:50:27 -0700503 }
504}
505
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200506void TransportController::SetIceRole_n(IceRole ice_role) {
507 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800508
deadbeefcbecd352015-09-23 11:50:27 -0700509 ice_role_ = ice_role;
deadbeef49f34fd2016-12-06 16:22:06 -0800510 for (auto& channel : channels_) {
zhihuangb2cdd932017-01-19 16:54:25 -0800511 channel->dtls()->ice_transport()->SetIceRole(ice_role_);
deadbeefcbecd352015-09-23 11:50:27 -0700512 }
513}
514
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200515bool TransportController::GetSslRole_n(const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800516 rtc::SSLRole* role) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200517 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700518
deadbeefd1a38b52016-12-10 13:15:33 -0800519 const JsepTransport* t = GetJsepTransport(transport_name);
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800520 if (!t) {
deadbeefcbecd352015-09-23 11:50:27 -0700521 return false;
522 }
deadbeefd8cfa1a2017-03-27 10:33:26 -0700523 rtc::Optional<rtc::SSLRole> current_role = t->GetSslRole();
524 if (!current_role) {
525 return false;
526 }
527 *role = *current_role;
deadbeef49f34fd2016-12-06 16:22:06 -0800528 return true;
deadbeefcbecd352015-09-23 11:50:27 -0700529}
530
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200531bool TransportController::SetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700532 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200533 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700534
deadbeef49f34fd2016-12-06 16:22:06 -0800535 // Can't change a certificate, or set a null certificate.
536 if (certificate_ || !certificate) {
deadbeefcbecd352015-09-23 11:50:27 -0700537 return false;
538 }
539 certificate_ = certificate;
540
deadbeef8662f942017-01-20 21:20:51 -0800541 // Set certificate for JsepTransport, which verifies it matches the
zhihuangb19012e2017-09-19 13:47:59 -0700542 // fingerprint in SDP, and DTLS transport.
543 // Fallback from DTLS to SDES is not supported.
deadbeef49f34fd2016-12-06 16:22:06 -0800544 for (auto& kv : transports_) {
deadbeefcbecd352015-09-23 11:50:27 -0700545 kv.second->SetLocalCertificate(certificate_);
546 }
zhihuangb19012e2017-09-19 13:47:59 -0700547 for (auto& channel : channels_) {
548 bool set_cert_success = channel->dtls()->SetLocalCertificate(certificate_);
549 RTC_DCHECK(set_cert_success);
550 }
deadbeefcbecd352015-09-23 11:50:27 -0700551 return true;
552}
553
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200554bool TransportController::GetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700555 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800556 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200557 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700558
deadbeefd1a38b52016-12-10 13:15:33 -0800559 const JsepTransport* t = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700560 if (!t) {
561 return false;
562 }
deadbeefcbecd352015-09-23 11:50:27 -0700563 return t->GetLocalCertificate(certificate);
564}
565
jbauch555604a2016-04-26 03:13:22 -0700566std::unique_ptr<rtc::SSLCertificate>
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200567TransportController::GetRemoteSSLCertificate_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800568 const std::string& transport_name) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200569 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700570
deadbeef49f34fd2016-12-06 16:22:06 -0800571 // Get the certificate from the RTP channel's DTLS handshake. Should be
572 // identical to the RTCP channel's, since they were given the same remote
573 // fingerprint.
574 const RefCountedChannel* ch = GetChannel_n(transport_name, 1);
575 if (!ch) {
kwibergb4d01c42016-04-06 05:15:06 -0700576 return nullptr;
deadbeefcbecd352015-09-23 11:50:27 -0700577 }
deadbeef49f34fd2016-12-06 16:22:06 -0800578 return ch->dtls()->GetRemoteSSLCertificate();
deadbeefcbecd352015-09-23 11:50:27 -0700579}
580
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200581bool TransportController::SetLocalTransportDescription_n(
deadbeefcbecd352015-09-23 11:50:27 -0700582 const std::string& transport_name,
583 const TransportDescription& tdesc,
584 ContentAction action,
585 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200586 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700587
deadbeefd1a38b52016-12-10 13:15:33 -0800588 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700589 if (!transport) {
590 // If we didn't find a transport, that's not an error;
591 // it could have been deleted as a result of bundling.
592 // TODO(deadbeef): Make callers smarter so they won't attempt to set a
593 // description on a deleted transport.
594 return true;
595 }
596
deadbeef91042f82016-07-15 17:48:13 -0700597 // Older versions of Chrome expect the ICE role to be re-determined when an
598 // ICE restart occurs, and also don't perform conflict resolution correctly,
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700599 // so for now we can't safely stop doing this, unless the application opts in
600 // by setting |redetermine_role_on_ice_restart_| to false.
deadbeef91042f82016-07-15 17:48:13 -0700601 // See: https://bugs.chromium.org/p/chromium/issues/detail?id=628676
602 // TODO(deadbeef): Remove this when these old versions of Chrome reach a low
603 // enough population.
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700604 if (redetermine_role_on_ice_restart_ && transport->local_description() &&
deadbeef91042f82016-07-15 17:48:13 -0700605 IceCredentialsChanged(transport->local_description()->ice_ufrag,
606 transport->local_description()->ice_pwd,
deadbeef897d08e2017-04-20 00:57:25 -0700607 tdesc.ice_ufrag, tdesc.ice_pwd) &&
608 // Don't change the ICE role if the remote endpoint is ICE lite; we
609 // should always be controlling in that case.
610 (!transport->remote_description() ||
611 transport->remote_description()->ice_mode != ICEMODE_LITE)) {
deadbeef91042f82016-07-15 17:48:13 -0700612 IceRole new_ice_role =
613 (action == CA_OFFER) ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED;
614 SetIceRole(new_ice_role);
615 }
616
Honghai Zhang9ecb0852016-09-15 16:41:04 -0700617 LOG(LS_INFO) << "Set local transport description on " << transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700618 return transport->SetLocalTransportDescription(tdesc, action, err);
619}
620
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200621bool TransportController::SetRemoteTransportDescription_n(
deadbeefcbecd352015-09-23 11:50:27 -0700622 const std::string& transport_name,
623 const TransportDescription& tdesc,
624 ContentAction action,
625 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200626 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700627
deadbeef49f34fd2016-12-06 16:22:06 -0800628 // If our role is ICEROLE_CONTROLLED and the remote endpoint supports only
629 // ice_lite, this local endpoint should take the CONTROLLING role.
630 // TODO(deadbeef): This is a session-level attribute, so it really shouldn't
631 // be in a TransportDescription in the first place...
632 if (ice_role_ == ICEROLE_CONTROLLED && tdesc.ice_mode == ICEMODE_LITE) {
633 SetIceRole_n(ICEROLE_CONTROLLING);
634 }
635
deadbeefd1a38b52016-12-10 13:15:33 -0800636 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700637 if (!transport) {
638 // If we didn't find a transport, that's not an error;
639 // it could have been deleted as a result of bundling.
640 // TODO(deadbeef): Make callers smarter so they won't attempt to set a
641 // description on a deleted transport.
642 return true;
643 }
644
Honghai Zhang9ecb0852016-09-15 16:41:04 -0700645 LOG(LS_INFO) << "Set remote transport description on " << transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700646 return transport->SetRemoteTransportDescription(tdesc, action, err);
647}
648
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200649void TransportController::MaybeStartGathering_n() {
deadbeef49f34fd2016-12-06 16:22:06 -0800650 for (auto& channel : channels_) {
zhihuangb2cdd932017-01-19 16:54:25 -0800651 channel->dtls()->ice_transport()->MaybeStartGathering();
deadbeefcbecd352015-09-23 11:50:27 -0700652 }
653}
654
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200655bool TransportController::AddRemoteCandidates_n(
deadbeefcbecd352015-09-23 11:50:27 -0700656 const std::string& transport_name,
657 const Candidates& candidates,
658 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200659 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700660
deadbeef49f34fd2016-12-06 16:22:06 -0800661 // Verify each candidate before passing down to the transport layer.
662 if (!VerifyCandidates(candidates, err)) {
663 return false;
664 }
665
deadbeefd1a38b52016-12-10 13:15:33 -0800666 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700667 if (!transport) {
668 // If we didn't find a transport, that's not an error;
669 // it could have been deleted as a result of bundling.
670 return true;
671 }
672
deadbeef49f34fd2016-12-06 16:22:06 -0800673 for (const Candidate& candidate : candidates) {
674 RefCountedChannel* channel =
675 GetChannel_n(transport_name, candidate.component());
676 if (!channel) {
677 *err = "Candidate has an unknown component: " + candidate.ToString() +
678 " for content: " + transport_name;
679 return false;
680 }
zhihuangb2cdd932017-01-19 16:54:25 -0800681 channel->dtls()->ice_transport()->AddRemoteCandidate(candidate);
deadbeef49f34fd2016-12-06 16:22:06 -0800682 }
683 return true;
deadbeefcbecd352015-09-23 11:50:27 -0700684}
685
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200686bool TransportController::RemoveRemoteCandidates_n(const Candidates& candidates,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700687 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200688 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800689
690 // Verify each candidate before passing down to the transport layer.
691 if (!VerifyCandidates(candidates, err)) {
692 return false;
693 }
694
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700695 std::map<std::string, Candidates> candidates_by_transport_name;
696 for (const Candidate& cand : candidates) {
697 RTC_DCHECK(!cand.transport_name().empty());
698 candidates_by_transport_name[cand.transport_name()].push_back(cand);
699 }
700
701 bool result = true;
deadbeef49f34fd2016-12-06 16:22:06 -0800702 for (const auto& kv : candidates_by_transport_name) {
703 const std::string& transport_name = kv.first;
704 const Candidates& candidates = kv.second;
deadbeefd1a38b52016-12-10 13:15:33 -0800705 JsepTransport* transport = GetJsepTransport(transport_name);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700706 if (!transport) {
707 // If we didn't find a transport, that's not an error;
708 // it could have been deleted as a result of bundling.
709 continue;
710 }
deadbeef49f34fd2016-12-06 16:22:06 -0800711 for (const Candidate& candidate : candidates) {
712 RefCountedChannel* channel =
713 GetChannel_n(transport_name, candidate.component());
714 if (channel) {
zhihuangb2cdd932017-01-19 16:54:25 -0800715 channel->dtls()->ice_transport()->RemoveRemoteCandidate(candidate);
deadbeef49f34fd2016-12-06 16:22:06 -0800716 }
717 }
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700718 }
719 return result;
720}
721
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200722bool TransportController::ReadyForRemoteCandidates_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800723 const std::string& transport_name) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200724 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700725
deadbeefd1a38b52016-12-10 13:15:33 -0800726 const JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700727 if (!transport) {
728 return false;
729 }
730 return transport->ready_for_remote_candidates();
731}
732
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200733bool TransportController::GetStats_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700734 TransportStats* stats) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200735 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700736
deadbeefd1a38b52016-12-10 13:15:33 -0800737 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700738 if (!transport) {
739 return false;
740 }
741 return transport->GetStats(stats);
742}
743
deadbeef49f34fd2016-12-06 16:22:06 -0800744void TransportController::SetMetricsObserver_n(
745 webrtc::MetricsObserverInterface* metrics_observer) {
746 RTC_DCHECK(network_thread_->IsCurrent());
747 metrics_observer_ = metrics_observer;
748 for (auto& channel : channels_) {
zhihuangb2cdd932017-01-19 16:54:25 -0800749 channel->dtls()->ice_transport()->SetMetricsObserver(metrics_observer);
deadbeef49f34fd2016-12-06 16:22:06 -0800750 }
751}
752
johand89ab142016-10-25 10:50:32 -0700753void TransportController::OnChannelWritableState_n(
deadbeef5bd5ca32017-02-10 11:31:50 -0800754 rtc::PacketTransportInternal* transport) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200755 RTC_DCHECK(network_thread_->IsCurrent());
johand89ab142016-10-25 10:50:32 -0700756 LOG(LS_INFO) << " TransportChannel " << transport->debug_name()
757 << " writability changed to " << transport->writable() << ".";
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200758 UpdateAggregateStates_n();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700759}
760
johan15ca8f62016-11-01 01:47:41 -0700761void TransportController::OnChannelReceivingState_n(
deadbeef5bd5ca32017-02-10 11:31:50 -0800762 rtc::PacketTransportInternal* transport) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200763 RTC_DCHECK(network_thread_->IsCurrent());
764 UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700765}
766
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200767void TransportController::OnChannelGatheringState_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800768 IceTransportInternal* channel) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200769 RTC_DCHECK(network_thread_->IsCurrent());
770 UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700771}
772
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200773void TransportController::OnChannelCandidateGathered_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800774 IceTransportInternal* channel,
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700775 const Candidate& candidate) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200776 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700777
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700778 // We should never signal peer-reflexive candidates.
779 if (candidate.type() == PRFLX_PORT_TYPE) {
nisseeb4ca4e2017-01-12 02:24:27 -0800780 RTC_NOTREACHED();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700781 return;
782 }
783 std::vector<Candidate> candidates;
784 candidates.push_back(candidate);
785 CandidatesData* data =
786 new CandidatesData(channel->transport_name(), candidates);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700787 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_CANDIDATESGATHERED, data);
deadbeefcbecd352015-09-23 11:50:27 -0700788}
789
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200790void TransportController::OnChannelCandidatesRemoved_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800791 IceTransportInternal* channel,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700792 const Candidates& candidates) {
793 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700794 RTC_FROM_HERE, signaling_thread_,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700795 rtc::Bind(&TransportController::OnChannelCandidatesRemoved, this,
796 candidates));
797}
798
799void TransportController::OnChannelCandidatesRemoved(
800 const Candidates& candidates) {
801 RTC_DCHECK(signaling_thread_->IsCurrent());
802 SignalCandidatesRemoved(candidates);
803}
804
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200805void TransportController::OnChannelRoleConflict_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800806 IceTransportInternal* channel) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200807 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef1c206102016-05-27 13:34:37 -0700808 // Note: since the role conflict is handled entirely on the network thread,
809 // we don't need to worry about role conflicts occurring on two ports at once.
810 // The first one encountered should immediately reverse the role.
deadbeefcbecd352015-09-23 11:50:27 -0700811 IceRole reversed_role = (ice_role_ == ICEROLE_CONTROLLING)
812 ? ICEROLE_CONTROLLED
813 : ICEROLE_CONTROLLING;
deadbeef1c206102016-05-27 13:34:37 -0700814 LOG(LS_INFO) << "Got role conflict; switching to "
815 << (reversed_role == ICEROLE_CONTROLLING ? "controlling"
816 : "controlled")
817 << " role.";
818 SetIceRole_n(reversed_role);
deadbeefcbecd352015-09-23 11:50:27 -0700819}
820
Honghai Zhang1590c392016-05-24 13:15:02 -0700821void TransportController::OnChannelStateChanged_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800822 IceTransportInternal* channel) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200823 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700824 LOG(LS_INFO) << channel->transport_name() << " TransportChannel "
825 << channel->component()
Honghai Zhang1590c392016-05-24 13:15:02 -0700826 << " state changed. Check if state is complete.";
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200827 UpdateAggregateStates_n();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700828}
829
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200830void TransportController::UpdateAggregateStates_n() {
831 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700832
833 IceConnectionState new_connection_state = kIceConnectionConnecting;
834 IceGatheringState new_gathering_state = kIceGatheringNew;
835 bool any_receiving = false;
836 bool any_failed = false;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700837 bool all_connected = !channels_.empty();
838 bool all_completed = !channels_.empty();
deadbeefcbecd352015-09-23 11:50:27 -0700839 bool any_gathering = false;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700840 bool all_done_gathering = !channels_.empty();
841 for (const auto& channel : channels_) {
deadbeef62802a12016-12-13 16:38:36 -0800842 any_receiving = any_receiving || channel->dtls()->receiving();
zhihuangd06adf62017-01-12 15:58:31 -0800843 any_failed = any_failed ||
zhihuangb2cdd932017-01-19 16:54:25 -0800844 channel->dtls()->ice_transport()->GetState() ==
845 IceTransportState::STATE_FAILED;
deadbeef62802a12016-12-13 16:38:36 -0800846 all_connected = all_connected && channel->dtls()->writable();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700847 all_completed =
deadbeef62802a12016-12-13 16:38:36 -0800848 all_completed && channel->dtls()->writable() &&
zhihuangb2cdd932017-01-19 16:54:25 -0800849 channel->dtls()->ice_transport()->GetState() ==
850 IceTransportState::STATE_COMPLETED &&
851 channel->dtls()->ice_transport()->GetIceRole() == ICEROLE_CONTROLLING &&
852 channel->dtls()->ice_transport()->gathering_state() ==
853 kIceGatheringComplete;
deadbeefcbecd352015-09-23 11:50:27 -0700854 any_gathering =
zhihuangb2cdd932017-01-19 16:54:25 -0800855 any_gathering ||
856 channel->dtls()->ice_transport()->gathering_state() != kIceGatheringNew;
857 all_done_gathering = all_done_gathering &&
858 channel->dtls()->ice_transport()->gathering_state() ==
859 kIceGatheringComplete;
deadbeefcbecd352015-09-23 11:50:27 -0700860 }
deadbeefcbecd352015-09-23 11:50:27 -0700861 if (any_failed) {
862 new_connection_state = kIceConnectionFailed;
863 } else if (all_completed) {
864 new_connection_state = kIceConnectionCompleted;
865 } else if (all_connected) {
866 new_connection_state = kIceConnectionConnected;
867 }
868 if (connection_state_ != new_connection_state) {
869 connection_state_ = new_connection_state;
870 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700871 RTC_FROM_HERE, this, MSG_ICECONNECTIONSTATE,
deadbeefcbecd352015-09-23 11:50:27 -0700872 new rtc::TypedMessageData<IceConnectionState>(new_connection_state));
873 }
874
875 if (receiving_ != any_receiving) {
876 receiving_ = any_receiving;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700877 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_RECEIVING,
deadbeefcbecd352015-09-23 11:50:27 -0700878 new rtc::TypedMessageData<bool>(any_receiving));
879 }
880
881 if (all_done_gathering) {
882 new_gathering_state = kIceGatheringComplete;
883 } else if (any_gathering) {
884 new_gathering_state = kIceGatheringGathering;
885 }
886 if (gathering_state_ != new_gathering_state) {
887 gathering_state_ = new_gathering_state;
888 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700889 RTC_FROM_HERE, this, MSG_ICEGATHERINGSTATE,
deadbeefcbecd352015-09-23 11:50:27 -0700890 new rtc::TypedMessageData<IceGatheringState>(new_gathering_state));
891 }
892}
893
zhihuangd82eee02016-08-26 11:25:05 -0700894void TransportController::OnDtlsHandshakeError(rtc::SSLHandshakeError error) {
895 SignalDtlsHandshakeError(error);
896}
897
deadbeefcbecd352015-09-23 11:50:27 -0700898} // namespace cricket