blob: 4a3a00e10f0e2831e4eefcf6bd53d229086b0d84 [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#include "webrtc/p2p/base/transportcontroller.h"
12
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
deadbeefcbecd352015-09-23 11:50:27 -070016#include "webrtc/base/bind.h"
17#include "webrtc/base/checks.h"
18#include "webrtc/base/thread.h"
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070019#include "webrtc/p2p/base/port.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
48 // DtlsTransportChannelWrapper and P2PTransportChannelWrapper,
49 // once TransportChannelImpl is removed.
50 ChannelPair(TransportChannelImpl* dtls, TransportChannelImpl* ice)
51 : 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.
56 const TransportChannelImpl* dtls() const { return dtls_.get(); }
57 TransportChannelImpl* dtls() { return dtls_.get(); }
58 const TransportChannelImpl* ice() const { return ice_.get(); }
59 TransportChannelImpl* ice() { return ice_.get(); }
60
61 private:
62 std::unique_ptr<TransportChannelImpl> ice_;
63 std::unique_ptr<TransportChannelImpl> dtls_;
64
65 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelPair);
deadbeefcbecd352015-09-23 11:50:27 -070066};
67
68TransportController::TransportController(rtc::Thread* signaling_thread,
Danil Chapovalov7f216b72016-05-12 09:20:31 +020069 rtc::Thread* network_thread,
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070070 PortAllocator* port_allocator,
71 bool redetermine_role_on_ice_restart)
deadbeefcbecd352015-09-23 11:50:27 -070072 : signaling_thread_(signaling_thread),
Danil Chapovalov7f216b72016-05-12 09:20:31 +020073 network_thread_(network_thread),
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070074 port_allocator_(port_allocator),
75 redetermine_role_on_ice_restart_(redetermine_role_on_ice_restart) {}
76
77TransportController::TransportController(rtc::Thread* signaling_thread,
78 rtc::Thread* network_thread,
79 PortAllocator* port_allocator)
80 : TransportController(signaling_thread,
81 network_thread,
82 port_allocator,
83 true) {}
deadbeefcbecd352015-09-23 11:50:27 -070084
85TransportController::~TransportController() {
deadbeef49f34fd2016-12-06 16:22:06 -080086 // Channel destructors may try to send packets, so this needs to happen on
87 // the network thread.
Danil Chapovalov7f216b72016-05-12 09:20:31 +020088 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070089 RTC_FROM_HERE,
deadbeef49f34fd2016-12-06 16:22:06 -080090 rtc::Bind(&TransportController::DestroyAllChannels_n, this));
deadbeefcbecd352015-09-23 11:50:27 -070091}
92
93bool TransportController::SetSslMaxProtocolVersion(
94 rtc::SSLProtocolVersion version) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070095 return network_thread_->Invoke<bool>(
96 RTC_FROM_HERE, rtc::Bind(&TransportController::SetSslMaxProtocolVersion_n,
97 this, version));
deadbeefcbecd352015-09-23 11:50:27 -070098}
99
honghaiz1f429e32015-09-28 07:57:34 -0700100void TransportController::SetIceConfig(const IceConfig& config) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200101 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700102 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200103 rtc::Bind(&TransportController::SetIceConfig_n, this, config));
deadbeefcbecd352015-09-23 11:50:27 -0700104}
105
106void TransportController::SetIceRole(IceRole ice_role) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200107 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700108 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200109 rtc::Bind(&TransportController::SetIceRole_n, this, ice_role));
deadbeefcbecd352015-09-23 11:50:27 -0700110}
111
deadbeefd1a38b52016-12-10 13:15:33 -0800112void TransportController::SetNeedsIceRestartFlag() {
113 for (auto& kv : transports_) {
114 kv.second->SetNeedsIceRestartFlag();
115 }
116}
117
118bool TransportController::NeedsIceRestart(
119 const std::string& transport_name) const {
120 const JsepTransport* transport = GetJsepTransport(transport_name);
121 if (!transport) {
122 return false;
123 }
124 return transport->NeedsIceRestart();
125}
126
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800127bool TransportController::GetSslRole(const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800128 rtc::SSLRole* role) const {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700129 return network_thread_->Invoke<bool>(
130 RTC_FROM_HERE, rtc::Bind(&TransportController::GetSslRole_n, this,
131 transport_name, role));
deadbeefcbecd352015-09-23 11:50:27 -0700132}
133
134bool TransportController::SetLocalCertificate(
135 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700136 return network_thread_->Invoke<bool>(
137 RTC_FROM_HERE, rtc::Bind(&TransportController::SetLocalCertificate_n,
138 this, certificate));
deadbeefcbecd352015-09-23 11:50:27 -0700139}
140
141bool TransportController::GetLocalCertificate(
142 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800143 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const {
hbosdf6075a2016-12-19 04:58:02 -0800144 if (network_thread_->IsCurrent()) {
145 return GetLocalCertificate_n(transport_name, certificate);
146 }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200147 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700148 RTC_FROM_HERE, rtc::Bind(&TransportController::GetLocalCertificate_n,
149 this, transport_name, certificate));
deadbeefcbecd352015-09-23 11:50:27 -0700150}
151
jbauch555604a2016-04-26 03:13:22 -0700152std::unique_ptr<rtc::SSLCertificate>
kwibergb4d01c42016-04-06 05:15:06 -0700153TransportController::GetRemoteSSLCertificate(
deadbeef49f34fd2016-12-06 16:22:06 -0800154 const std::string& transport_name) const {
hbosdf6075a2016-12-19 04:58:02 -0800155 if (network_thread_->IsCurrent()) {
156 return GetRemoteSSLCertificate_n(transport_name);
157 }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200158 return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertificate>>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700159 RTC_FROM_HERE, rtc::Bind(&TransportController::GetRemoteSSLCertificate_n,
160 this, transport_name));
deadbeefcbecd352015-09-23 11:50:27 -0700161}
162
163bool TransportController::SetLocalTransportDescription(
164 const std::string& transport_name,
165 const TransportDescription& tdesc,
166 ContentAction action,
167 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200168 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700169 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200170 rtc::Bind(&TransportController::SetLocalTransportDescription_n, this,
deadbeefcbecd352015-09-23 11:50:27 -0700171 transport_name, tdesc, action, err));
172}
173
174bool TransportController::SetRemoteTransportDescription(
175 const std::string& transport_name,
176 const TransportDescription& tdesc,
177 ContentAction action,
178 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200179 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700180 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200181 rtc::Bind(&TransportController::SetRemoteTransportDescription_n, this,
deadbeefcbecd352015-09-23 11:50:27 -0700182 transport_name, tdesc, action, err));
183}
184
185void TransportController::MaybeStartGathering() {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200186 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700187 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200188 rtc::Bind(&TransportController::MaybeStartGathering_n, this));
deadbeefcbecd352015-09-23 11:50:27 -0700189}
190
191bool TransportController::AddRemoteCandidates(const std::string& transport_name,
192 const Candidates& candidates,
193 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200194 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700195 RTC_FROM_HERE, rtc::Bind(&TransportController::AddRemoteCandidates_n,
196 this, transport_name, candidates, err));
deadbeefcbecd352015-09-23 11:50:27 -0700197}
198
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700199bool TransportController::RemoveRemoteCandidates(const Candidates& candidates,
200 std::string* err) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700201 return network_thread_->Invoke<bool>(
202 RTC_FROM_HERE, rtc::Bind(&TransportController::RemoveRemoteCandidates_n,
203 this, candidates, err));
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700204}
205
deadbeefcbecd352015-09-23 11:50:27 -0700206bool TransportController::ReadyForRemoteCandidates(
deadbeef49f34fd2016-12-06 16:22:06 -0800207 const std::string& transport_name) const {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700208 return network_thread_->Invoke<bool>(
209 RTC_FROM_HERE, rtc::Bind(&TransportController::ReadyForRemoteCandidates_n,
210 this, transport_name));
deadbeefcbecd352015-09-23 11:50:27 -0700211}
212
213bool TransportController::GetStats(const std::string& transport_name,
214 TransportStats* stats) {
hbosdf6075a2016-12-19 04:58:02 -0800215 if (network_thread_->IsCurrent()) {
216 return GetStats_n(transport_name, stats);
217 }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200218 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700219 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200220 rtc::Bind(&TransportController::GetStats_n, this, transport_name, stats));
deadbeefcbecd352015-09-23 11:50:27 -0700221}
222
deadbeef49f34fd2016-12-06 16:22:06 -0800223void TransportController::SetMetricsObserver(
224 webrtc::MetricsObserverInterface* metrics_observer) {
225 return network_thread_->Invoke<void>(
226 RTC_FROM_HERE, rtc::Bind(&TransportController::SetMetricsObserver_n, this,
227 metrics_observer));
228}
229
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200230TransportChannel* TransportController::CreateTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700231 const std::string& transport_name,
232 int component) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200233 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700234
deadbeef49f34fd2016-12-06 16:22:06 -0800235 RefCountedChannel* existing_channel = GetChannel_n(transport_name, component);
236 if (existing_channel) {
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700237 // Channel already exists; increment reference count and return.
deadbeef49f34fd2016-12-06 16:22:06 -0800238 existing_channel->AddRef();
239 return existing_channel->dtls();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700240 }
241
242 // Need to create a new channel.
deadbeefd1a38b52016-12-10 13:15:33 -0800243 JsepTransport* transport = GetOrCreateJsepTransport(transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800244
245 // Create DTLS channel wrapping ICE channel, and configure it.
246 TransportChannelImpl* ice =
247 CreateIceTransportChannel_n(transport_name, component);
248 // TODO(deadbeef): To support QUIC, would need to create a
249 // QuicTransportChannel here. What is "dtls" in this file would then become
250 // "dtls or quic".
251 TransportChannelImpl* dtls =
252 CreateDtlsTransportChannel_n(transport_name, component, ice);
253 dtls->SetMetricsObserver(metrics_observer_);
254 dtls->SetIceRole(ice_role_);
255 dtls->SetIceTiebreaker(ice_tiebreaker_);
256 dtls->SetIceConfig(ice_config_);
257 if (certificate_) {
258 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
259 RTC_DCHECK(set_cert_success);
260 }
261
262 // Connect to signals offered by the channels. Currently, the DTLS channel
263 // forwards signals from the ICE channel, so we only need to connect to the
264 // DTLS channel. In the future this won't be the case.
265 dtls->SignalWritableState.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200266 this, &TransportController::OnChannelWritableState_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800267 dtls->SignalReceivingState.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200268 this, &TransportController::OnChannelReceivingState_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800269 dtls->SignalGatheringState.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200270 this, &TransportController::OnChannelGatheringState_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800271 dtls->SignalCandidateGathered.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200272 this, &TransportController::OnChannelCandidateGathered_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800273 dtls->SignalCandidatesRemoved.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200274 this, &TransportController::OnChannelCandidatesRemoved_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800275 dtls->SignalRoleConflict.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200276 this, &TransportController::OnChannelRoleConflict_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800277 dtls->SignalStateChanged.connect(
Honghai Zhang1590c392016-05-24 13:15:02 -0700278 this, &TransportController::OnChannelStateChanged_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800279 dtls->SignalDtlsHandshakeError.connect(
zhihuangd82eee02016-08-26 11:25:05 -0700280 this, &TransportController::OnDtlsHandshakeError);
deadbeef62802a12016-12-13 16:38:36 -0800281 RefCountedChannel* new_pair = new RefCountedChannel(dtls, ice);
282 new_pair->AddRef();
283 channels_.insert(channels_.end(), new_pair);
deadbeef49f34fd2016-12-06 16:22:06 -0800284 bool channel_added = transport->AddChannel(dtls, component);
285 RTC_DCHECK(channel_added);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700286 // Adding a channel could cause aggregate state to change.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200287 UpdateAggregateStates_n();
deadbeef49f34fd2016-12-06 16:22:06 -0800288 return dtls;
deadbeefcbecd352015-09-23 11:50:27 -0700289}
290
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200291void TransportController::DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700292 const std::string& transport_name,
293 int component) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200294 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700295
deadbeef49f34fd2016-12-06 16:22:06 -0800296 auto it = GetChannelIterator_n(transport_name, component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700297 if (it == channels_.end()) {
298 LOG(LS_WARNING) << "Attempting to delete " << transport_name
299 << " TransportChannel " << component
300 << ", which doesn't exist.";
deadbeefcbecd352015-09-23 11:50:27 -0700301 return;
302 }
deadbeef62802a12016-12-13 16:38:36 -0800303 if ((*it)->Release() > 0) {
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700304 return;
305 }
deadbeef57fd7262016-12-06 15:28:55 -0800306 channels_.erase(it);
deadbeef49f34fd2016-12-06 16:22:06 -0800307
deadbeefd1a38b52016-12-10 13:15:33 -0800308 JsepTransport* t = GetJsepTransport(transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800309 bool channel_removed = t->RemoveChannel(component);
310 RTC_DCHECK(channel_removed);
deadbeefcbecd352015-09-23 11:50:27 -0700311 // Just as we create a Transport when its first channel is created,
312 // we delete it when its last channel is deleted.
deadbeef49f34fd2016-12-06 16:22:06 -0800313 if (!t->HasChannels()) {
314 transports_.erase(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700315 }
deadbeef49f34fd2016-12-06 16:22:06 -0800316
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700317 // Removing a channel could cause aggregate state to change.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200318 UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700319}
320
deadbeef49f34fd2016-12-06 16:22:06 -0800321std::vector<std::string> TransportController::transport_names_for_testing() {
322 std::vector<std::string> ret;
323 for (const auto& kv : transports_) {
324 ret.push_back(kv.first);
mikescarlette7748672016-04-29 20:20:54 -0700325 }
deadbeef49f34fd2016-12-06 16:22:06 -0800326 return ret;
deadbeefcbecd352015-09-23 11:50:27 -0700327}
328
deadbeef49f34fd2016-12-06 16:22:06 -0800329std::vector<TransportChannelImpl*> TransportController::channels_for_testing() {
330 std::vector<TransportChannelImpl*> ret;
deadbeef62802a12016-12-13 16:38:36 -0800331 for (RefCountedChannel* channel : channels_) {
332 ret.push_back(channel->dtls());
deadbeef49f34fd2016-12-06 16:22:06 -0800333 }
334 return ret;
335}
deadbeefcbecd352015-09-23 11:50:27 -0700336
deadbeef49f34fd2016-12-06 16:22:06 -0800337TransportChannelImpl* TransportController::get_channel_for_testing(
338 const std::string& transport_name,
339 int component) {
340 RefCountedChannel* ch = GetChannel_n(transport_name, component);
341 return ch ? ch->dtls() : nullptr;
342}
343
344TransportChannelImpl* TransportController::CreateIceTransportChannel_n(
345 const std::string& transport_name,
346 int component) {
347 return new P2PTransportChannel(transport_name, component, port_allocator_);
348}
349
350TransportChannelImpl* TransportController::CreateDtlsTransportChannel_n(
351 const std::string&,
352 int,
353 TransportChannelImpl* ice) {
354 DtlsTransportChannelWrapper* dtls = new DtlsTransportChannelWrapper(ice);
355 dtls->SetSslMaxProtocolVersion(ssl_max_version_);
356 return dtls;
deadbeefcbecd352015-09-23 11:50:27 -0700357}
358
359void TransportController::OnMessage(rtc::Message* pmsg) {
360 RTC_DCHECK(signaling_thread_->IsCurrent());
361
362 switch (pmsg->message_id) {
363 case MSG_ICECONNECTIONSTATE: {
364 rtc::TypedMessageData<IceConnectionState>* data =
365 static_cast<rtc::TypedMessageData<IceConnectionState>*>(pmsg->pdata);
366 SignalConnectionState(data->data());
367 delete data;
368 break;
369 }
370 case MSG_RECEIVING: {
371 rtc::TypedMessageData<bool>* data =
372 static_cast<rtc::TypedMessageData<bool>*>(pmsg->pdata);
373 SignalReceiving(data->data());
374 delete data;
375 break;
376 }
377 case MSG_ICEGATHERINGSTATE: {
378 rtc::TypedMessageData<IceGatheringState>* data =
379 static_cast<rtc::TypedMessageData<IceGatheringState>*>(pmsg->pdata);
380 SignalGatheringState(data->data());
381 delete data;
382 break;
383 }
384 case MSG_CANDIDATESGATHERED: {
385 CandidatesData* data = static_cast<CandidatesData*>(pmsg->pdata);
386 SignalCandidatesGathered(data->transport_name, data->candidates);
387 delete data;
388 break;
389 }
390 default:
391 ASSERT(false);
392 }
393}
394
deadbeef62802a12016-12-13 16:38:36 -0800395std::vector<TransportController::RefCountedChannel*>::iterator
deadbeef49f34fd2016-12-06 16:22:06 -0800396TransportController::GetChannelIterator_n(const std::string& transport_name,
397 int component) {
398 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef62802a12016-12-13 16:38:36 -0800399 return std::find_if(channels_.begin(), channels_.end(),
400 [transport_name, component](RefCountedChannel* channel) {
401 return channel->dtls()->transport_name() ==
402 transport_name &&
403 channel->dtls()->component() == component;
404 });
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700405}
406
deadbeef62802a12016-12-13 16:38:36 -0800407std::vector<TransportController::RefCountedChannel*>::const_iterator
deadbeef49f34fd2016-12-06 16:22:06 -0800408TransportController::GetChannelIterator_n(const std::string& transport_name,
409 int component) const {
410 RTC_DCHECK(network_thread_->IsCurrent());
411 return std::find_if(
412 channels_.begin(), channels_.end(),
deadbeef62802a12016-12-13 16:38:36 -0800413 [transport_name, component](const RefCountedChannel* channel) {
414 return channel->dtls()->transport_name() == transport_name &&
415 channel->dtls()->component() == component;
deadbeef49f34fd2016-12-06 16:22:06 -0800416 });
417}
418
deadbeefd1a38b52016-12-10 13:15:33 -0800419const JsepTransport* TransportController::GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800420 const std::string& transport_name) const {
deadbeef49f34fd2016-12-06 16:22:06 -0800421 auto it = transports_.find(transport_name);
422 return (it == transports_.end()) ? nullptr : it->second.get();
423}
424
deadbeefd1a38b52016-12-10 13:15:33 -0800425JsepTransport* TransportController::GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800426 const std::string& transport_name) {
deadbeef49f34fd2016-12-06 16:22:06 -0800427 auto it = transports_.find(transport_name);
428 return (it == transports_.end()) ? nullptr : it->second.get();
429}
430
431const TransportController::RefCountedChannel* TransportController::GetChannel_n(
432 const std::string& transport_name,
433 int component) const {
434 RTC_DCHECK(network_thread_->IsCurrent());
435 auto it = GetChannelIterator_n(transport_name, component);
deadbeef62802a12016-12-13 16:38:36 -0800436 return (it == channels_.end()) ? nullptr : *it;
deadbeef49f34fd2016-12-06 16:22:06 -0800437}
438
439TransportController::RefCountedChannel* TransportController::GetChannel_n(
440 const std::string& transport_name,
441 int component) {
442 RTC_DCHECK(network_thread_->IsCurrent());
443 auto it = GetChannelIterator_n(transport_name, component);
deadbeef62802a12016-12-13 16:38:36 -0800444 return (it == channels_.end()) ? nullptr : *it;
deadbeef49f34fd2016-12-06 16:22:06 -0800445}
446
deadbeefd1a38b52016-12-10 13:15:33 -0800447JsepTransport* TransportController::GetOrCreateJsepTransport(
deadbeefcbecd352015-09-23 11:50:27 -0700448 const std::string& transport_name) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200449 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700450
deadbeefd1a38b52016-12-10 13:15:33 -0800451 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700452 if (transport) {
453 return transport;
454 }
455
deadbeef49f34fd2016-12-06 16:22:06 -0800456 transport = new JsepTransport(transport_name, certificate_);
457 transports_[transport_name] = std::unique_ptr<JsepTransport>(transport);
deadbeefcbecd352015-09-23 11:50:27 -0700458 return transport;
459}
460
deadbeef49f34fd2016-12-06 16:22:06 -0800461void TransportController::DestroyAllChannels_n() {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200462 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700463 transports_.clear();
deadbeef62802a12016-12-13 16:38:36 -0800464 for (RefCountedChannel* channel : channels_) {
465 // Even though these objects are normally ref-counted, if
466 // TransportController is deleted while they still have references, just
467 // remove all references.
468 while (channel->Release() > 0) {
469 }
470 }
deadbeef49f34fd2016-12-06 16:22:06 -0800471 channels_.clear();
deadbeefcbecd352015-09-23 11:50:27 -0700472}
473
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200474bool TransportController::SetSslMaxProtocolVersion_n(
deadbeefcbecd352015-09-23 11:50:27 -0700475 rtc::SSLProtocolVersion version) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200476 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700477
478 // Max SSL version can only be set before transports are created.
479 if (!transports_.empty()) {
480 return false;
481 }
482
483 ssl_max_version_ = version;
484 return true;
485}
486
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200487void TransportController::SetIceConfig_n(const IceConfig& config) {
488 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800489
honghaiz1f429e32015-09-28 07:57:34 -0700490 ice_config_ = config;
deadbeef49f34fd2016-12-06 16:22:06 -0800491 for (auto& channel : channels_) {
deadbeef62802a12016-12-13 16:38:36 -0800492 channel->dtls()->SetIceConfig(ice_config_);
deadbeefcbecd352015-09-23 11:50:27 -0700493 }
494}
495
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200496void TransportController::SetIceRole_n(IceRole ice_role) {
497 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800498
deadbeefcbecd352015-09-23 11:50:27 -0700499 ice_role_ = ice_role;
deadbeef49f34fd2016-12-06 16:22:06 -0800500 for (auto& channel : channels_) {
deadbeef62802a12016-12-13 16:38:36 -0800501 channel->dtls()->SetIceRole(ice_role_);
deadbeefcbecd352015-09-23 11:50:27 -0700502 }
503}
504
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200505bool TransportController::GetSslRole_n(const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800506 rtc::SSLRole* role) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200507 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700508
deadbeefd1a38b52016-12-10 13:15:33 -0800509 const JsepTransport* t = GetJsepTransport(transport_name);
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800510 if (!t) {
deadbeefcbecd352015-09-23 11:50:27 -0700511 return false;
512 }
deadbeef49f34fd2016-12-06 16:22:06 -0800513 t->GetSslRole(role);
514 return true;
deadbeefcbecd352015-09-23 11:50:27 -0700515}
516
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200517bool TransportController::SetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700518 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200519 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700520
deadbeef49f34fd2016-12-06 16:22:06 -0800521 // Can't change a certificate, or set a null certificate.
522 if (certificate_ || !certificate) {
deadbeefcbecd352015-09-23 11:50:27 -0700523 return false;
524 }
525 certificate_ = certificate;
526
deadbeef49f34fd2016-12-06 16:22:06 -0800527 // Set certificate both for Transport, which verifies it matches the
528 // fingerprint in SDP...
529 for (auto& kv : transports_) {
deadbeefcbecd352015-09-23 11:50:27 -0700530 kv.second->SetLocalCertificate(certificate_);
531 }
deadbeef49f34fd2016-12-06 16:22:06 -0800532 // ... and for the DTLS channel, which needs it for the DTLS handshake.
533 for (auto& channel : channels_) {
deadbeef62802a12016-12-13 16:38:36 -0800534 bool set_cert_success = channel->dtls()->SetLocalCertificate(certificate);
deadbeef49f34fd2016-12-06 16:22:06 -0800535 RTC_DCHECK(set_cert_success);
536 }
deadbeefcbecd352015-09-23 11:50:27 -0700537 return true;
538}
539
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200540bool TransportController::GetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700541 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800542 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200543 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700544
deadbeefd1a38b52016-12-10 13:15:33 -0800545 const JsepTransport* t = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700546 if (!t) {
547 return false;
548 }
deadbeefcbecd352015-09-23 11:50:27 -0700549 return t->GetLocalCertificate(certificate);
550}
551
jbauch555604a2016-04-26 03:13:22 -0700552std::unique_ptr<rtc::SSLCertificate>
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200553TransportController::GetRemoteSSLCertificate_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800554 const std::string& transport_name) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200555 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700556
deadbeef49f34fd2016-12-06 16:22:06 -0800557 // Get the certificate from the RTP channel's DTLS handshake. Should be
558 // identical to the RTCP channel's, since they were given the same remote
559 // fingerprint.
560 const RefCountedChannel* ch = GetChannel_n(transport_name, 1);
561 if (!ch) {
kwibergb4d01c42016-04-06 05:15:06 -0700562 return nullptr;
deadbeefcbecd352015-09-23 11:50:27 -0700563 }
deadbeef49f34fd2016-12-06 16:22:06 -0800564 return ch->dtls()->GetRemoteSSLCertificate();
deadbeefcbecd352015-09-23 11:50:27 -0700565}
566
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200567bool TransportController::SetLocalTransportDescription_n(
deadbeefcbecd352015-09-23 11:50:27 -0700568 const std::string& transport_name,
569 const TransportDescription& tdesc,
570 ContentAction action,
571 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200572 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700573
deadbeefd1a38b52016-12-10 13:15:33 -0800574 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700575 if (!transport) {
576 // If we didn't find a transport, that's not an error;
577 // it could have been deleted as a result of bundling.
578 // TODO(deadbeef): Make callers smarter so they won't attempt to set a
579 // description on a deleted transport.
580 return true;
581 }
582
deadbeef91042f82016-07-15 17:48:13 -0700583 // Older versions of Chrome expect the ICE role to be re-determined when an
584 // ICE restart occurs, and also don't perform conflict resolution correctly,
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700585 // so for now we can't safely stop doing this, unless the application opts in
586 // by setting |redetermine_role_on_ice_restart_| to false.
deadbeef91042f82016-07-15 17:48:13 -0700587 // See: https://bugs.chromium.org/p/chromium/issues/detail?id=628676
588 // TODO(deadbeef): Remove this when these old versions of Chrome reach a low
589 // enough population.
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700590 if (redetermine_role_on_ice_restart_ && transport->local_description() &&
deadbeef91042f82016-07-15 17:48:13 -0700591 IceCredentialsChanged(transport->local_description()->ice_ufrag,
592 transport->local_description()->ice_pwd,
593 tdesc.ice_ufrag, tdesc.ice_pwd)) {
594 IceRole new_ice_role =
595 (action == CA_OFFER) ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED;
596 SetIceRole(new_ice_role);
597 }
598
Honghai Zhang9ecb0852016-09-15 16:41:04 -0700599 LOG(LS_INFO) << "Set local transport description on " << transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700600 return transport->SetLocalTransportDescription(tdesc, action, err);
601}
602
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200603bool TransportController::SetRemoteTransportDescription_n(
deadbeefcbecd352015-09-23 11:50:27 -0700604 const std::string& transport_name,
605 const TransportDescription& tdesc,
606 ContentAction action,
607 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200608 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700609
deadbeef49f34fd2016-12-06 16:22:06 -0800610 // If our role is ICEROLE_CONTROLLED and the remote endpoint supports only
611 // ice_lite, this local endpoint should take the CONTROLLING role.
612 // TODO(deadbeef): This is a session-level attribute, so it really shouldn't
613 // be in a TransportDescription in the first place...
614 if (ice_role_ == ICEROLE_CONTROLLED && tdesc.ice_mode == ICEMODE_LITE) {
615 SetIceRole_n(ICEROLE_CONTROLLING);
616 }
617
deadbeefd1a38b52016-12-10 13:15:33 -0800618 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700619 if (!transport) {
620 // If we didn't find a transport, that's not an error;
621 // it could have been deleted as a result of bundling.
622 // TODO(deadbeef): Make callers smarter so they won't attempt to set a
623 // description on a deleted transport.
624 return true;
625 }
626
Honghai Zhang9ecb0852016-09-15 16:41:04 -0700627 LOG(LS_INFO) << "Set remote transport description on " << transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700628 return transport->SetRemoteTransportDescription(tdesc, action, err);
629}
630
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200631void TransportController::MaybeStartGathering_n() {
deadbeef49f34fd2016-12-06 16:22:06 -0800632 for (auto& channel : channels_) {
deadbeef62802a12016-12-13 16:38:36 -0800633 channel->dtls()->MaybeStartGathering();
deadbeefcbecd352015-09-23 11:50:27 -0700634 }
635}
636
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200637bool TransportController::AddRemoteCandidates_n(
deadbeefcbecd352015-09-23 11:50:27 -0700638 const std::string& transport_name,
639 const Candidates& candidates,
640 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200641 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700642
deadbeef49f34fd2016-12-06 16:22:06 -0800643 // Verify each candidate before passing down to the transport layer.
644 if (!VerifyCandidates(candidates, err)) {
645 return false;
646 }
647
deadbeefd1a38b52016-12-10 13:15:33 -0800648 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700649 if (!transport) {
650 // If we didn't find a transport, that's not an error;
651 // it could have been deleted as a result of bundling.
652 return true;
653 }
654
deadbeef49f34fd2016-12-06 16:22:06 -0800655 for (const Candidate& candidate : candidates) {
656 RefCountedChannel* channel =
657 GetChannel_n(transport_name, candidate.component());
658 if (!channel) {
659 *err = "Candidate has an unknown component: " + candidate.ToString() +
660 " for content: " + transport_name;
661 return false;
662 }
663 channel->dtls()->AddRemoteCandidate(candidate);
664 }
665 return true;
deadbeefcbecd352015-09-23 11:50:27 -0700666}
667
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200668bool TransportController::RemoveRemoteCandidates_n(const Candidates& candidates,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700669 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200670 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800671
672 // Verify each candidate before passing down to the transport layer.
673 if (!VerifyCandidates(candidates, err)) {
674 return false;
675 }
676
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700677 std::map<std::string, Candidates> candidates_by_transport_name;
678 for (const Candidate& cand : candidates) {
679 RTC_DCHECK(!cand.transport_name().empty());
680 candidates_by_transport_name[cand.transport_name()].push_back(cand);
681 }
682
683 bool result = true;
deadbeef49f34fd2016-12-06 16:22:06 -0800684 for (const auto& kv : candidates_by_transport_name) {
685 const std::string& transport_name = kv.first;
686 const Candidates& candidates = kv.second;
deadbeefd1a38b52016-12-10 13:15:33 -0800687 JsepTransport* transport = GetJsepTransport(transport_name);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700688 if (!transport) {
689 // If we didn't find a transport, that's not an error;
690 // it could have been deleted as a result of bundling.
691 continue;
692 }
deadbeef49f34fd2016-12-06 16:22:06 -0800693 for (const Candidate& candidate : candidates) {
694 RefCountedChannel* channel =
695 GetChannel_n(transport_name, candidate.component());
696 if (channel) {
697 channel->dtls()->RemoveRemoteCandidate(candidate);
698 }
699 }
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700700 }
701 return result;
702}
703
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200704bool TransportController::ReadyForRemoteCandidates_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800705 const std::string& transport_name) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200706 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700707
deadbeefd1a38b52016-12-10 13:15:33 -0800708 const JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700709 if (!transport) {
710 return false;
711 }
712 return transport->ready_for_remote_candidates();
713}
714
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200715bool TransportController::GetStats_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700716 TransportStats* stats) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200717 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700718
deadbeefd1a38b52016-12-10 13:15:33 -0800719 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700720 if (!transport) {
721 return false;
722 }
723 return transport->GetStats(stats);
724}
725
deadbeef49f34fd2016-12-06 16:22:06 -0800726void TransportController::SetMetricsObserver_n(
727 webrtc::MetricsObserverInterface* metrics_observer) {
728 RTC_DCHECK(network_thread_->IsCurrent());
729 metrics_observer_ = metrics_observer;
730 for (auto& channel : channels_) {
deadbeef62802a12016-12-13 16:38:36 -0800731 channel->dtls()->SetMetricsObserver(metrics_observer);
deadbeef49f34fd2016-12-06 16:22:06 -0800732 }
733}
734
johand89ab142016-10-25 10:50:32 -0700735void TransportController::OnChannelWritableState_n(
736 rtc::PacketTransportInterface* transport) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200737 RTC_DCHECK(network_thread_->IsCurrent());
johand89ab142016-10-25 10:50:32 -0700738 LOG(LS_INFO) << " TransportChannel " << transport->debug_name()
739 << " writability changed to " << transport->writable() << ".";
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200740 UpdateAggregateStates_n();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700741}
742
johan15ca8f62016-11-01 01:47:41 -0700743void TransportController::OnChannelReceivingState_n(
744 rtc::PacketTransportInterface* transport) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200745 RTC_DCHECK(network_thread_->IsCurrent());
746 UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700747}
748
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200749void TransportController::OnChannelGatheringState_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700750 TransportChannelImpl* channel) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200751 RTC_DCHECK(network_thread_->IsCurrent());
752 UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700753}
754
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200755void TransportController::OnChannelCandidateGathered_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700756 TransportChannelImpl* channel,
757 const Candidate& candidate) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200758 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700759
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700760 // We should never signal peer-reflexive candidates.
761 if (candidate.type() == PRFLX_PORT_TYPE) {
762 RTC_DCHECK(false);
763 return;
764 }
765 std::vector<Candidate> candidates;
766 candidates.push_back(candidate);
767 CandidatesData* data =
768 new CandidatesData(channel->transport_name(), candidates);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700769 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_CANDIDATESGATHERED, data);
deadbeefcbecd352015-09-23 11:50:27 -0700770}
771
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200772void TransportController::OnChannelCandidatesRemoved_n(
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700773 TransportChannelImpl* channel,
774 const Candidates& candidates) {
775 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700776 RTC_FROM_HERE, signaling_thread_,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700777 rtc::Bind(&TransportController::OnChannelCandidatesRemoved, this,
778 candidates));
779}
780
781void TransportController::OnChannelCandidatesRemoved(
782 const Candidates& candidates) {
783 RTC_DCHECK(signaling_thread_->IsCurrent());
784 SignalCandidatesRemoved(candidates);
785}
786
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200787void TransportController::OnChannelRoleConflict_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700788 TransportChannelImpl* channel) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200789 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef1c206102016-05-27 13:34:37 -0700790 // Note: since the role conflict is handled entirely on the network thread,
791 // we don't need to worry about role conflicts occurring on two ports at once.
792 // The first one encountered should immediately reverse the role.
deadbeefcbecd352015-09-23 11:50:27 -0700793 IceRole reversed_role = (ice_role_ == ICEROLE_CONTROLLING)
794 ? ICEROLE_CONTROLLED
795 : ICEROLE_CONTROLLING;
deadbeef1c206102016-05-27 13:34:37 -0700796 LOG(LS_INFO) << "Got role conflict; switching to "
797 << (reversed_role == ICEROLE_CONTROLLING ? "controlling"
798 : "controlled")
799 << " role.";
800 SetIceRole_n(reversed_role);
deadbeefcbecd352015-09-23 11:50:27 -0700801}
802
Honghai Zhang1590c392016-05-24 13:15:02 -0700803void TransportController::OnChannelStateChanged_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700804 TransportChannelImpl* channel) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200805 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700806 LOG(LS_INFO) << channel->transport_name() << " TransportChannel "
807 << channel->component()
Honghai Zhang1590c392016-05-24 13:15:02 -0700808 << " state changed. Check if state is complete.";
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200809 UpdateAggregateStates_n();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700810}
811
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200812void TransportController::UpdateAggregateStates_n() {
813 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700814
815 IceConnectionState new_connection_state = kIceConnectionConnecting;
816 IceGatheringState new_gathering_state = kIceGatheringNew;
817 bool any_receiving = false;
818 bool any_failed = false;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700819 bool all_connected = !channels_.empty();
820 bool all_completed = !channels_.empty();
deadbeefcbecd352015-09-23 11:50:27 -0700821 bool any_gathering = false;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700822 bool all_done_gathering = !channels_.empty();
823 for (const auto& channel : channels_) {
deadbeef62802a12016-12-13 16:38:36 -0800824 any_receiving = any_receiving || channel->dtls()->receiving();
deadbeef49f34fd2016-12-06 16:22:06 -0800825 any_failed =
826 any_failed ||
deadbeef62802a12016-12-13 16:38:36 -0800827 channel->dtls()->GetState() == TransportChannelState::STATE_FAILED;
828 all_connected = all_connected && channel->dtls()->writable();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700829 all_completed =
deadbeef62802a12016-12-13 16:38:36 -0800830 all_completed && channel->dtls()->writable() &&
831 channel->dtls()->GetState() == TransportChannelState::STATE_COMPLETED &&
832 channel->dtls()->GetIceRole() == ICEROLE_CONTROLLING &&
833 channel->dtls()->gathering_state() == kIceGatheringComplete;
deadbeefcbecd352015-09-23 11:50:27 -0700834 any_gathering =
deadbeef62802a12016-12-13 16:38:36 -0800835 any_gathering || channel->dtls()->gathering_state() != kIceGatheringNew;
deadbeef49f34fd2016-12-06 16:22:06 -0800836 all_done_gathering =
837 all_done_gathering &&
deadbeef62802a12016-12-13 16:38:36 -0800838 channel->dtls()->gathering_state() == kIceGatheringComplete;
deadbeefcbecd352015-09-23 11:50:27 -0700839 }
840
841 if (any_failed) {
842 new_connection_state = kIceConnectionFailed;
843 } else if (all_completed) {
844 new_connection_state = kIceConnectionCompleted;
845 } else if (all_connected) {
846 new_connection_state = kIceConnectionConnected;
847 }
848 if (connection_state_ != new_connection_state) {
849 connection_state_ = new_connection_state;
850 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700851 RTC_FROM_HERE, this, MSG_ICECONNECTIONSTATE,
deadbeefcbecd352015-09-23 11:50:27 -0700852 new rtc::TypedMessageData<IceConnectionState>(new_connection_state));
853 }
854
855 if (receiving_ != any_receiving) {
856 receiving_ = any_receiving;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700857 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_RECEIVING,
deadbeefcbecd352015-09-23 11:50:27 -0700858 new rtc::TypedMessageData<bool>(any_receiving));
859 }
860
861 if (all_done_gathering) {
862 new_gathering_state = kIceGatheringComplete;
863 } else if (any_gathering) {
864 new_gathering_state = kIceGatheringGathering;
865 }
866 if (gathering_state_ != new_gathering_state) {
867 gathering_state_ = new_gathering_state;
868 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700869 RTC_FROM_HERE, this, MSG_ICEGATHERINGSTATE,
deadbeefcbecd352015-09-23 11:50:27 -0700870 new rtc::TypedMessageData<IceGatheringState>(new_gathering_state));
871 }
872}
873
zhihuangd82eee02016-08-26 11:25:05 -0700874void TransportController::OnDtlsHandshakeError(rtc::SSLHandshakeError error) {
875 SignalDtlsHandshakeError(error);
876}
877
deadbeefcbecd352015-09-23 11:50:27 -0700878} // namespace cricket