Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(568)

Side by Side Diff: webrtc/p2p/base/transportcontroller.cc

Issue 2553043004: Revert of Refactoring that removes P2PTransport and DtlsTransport classes. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/p2p/base/transportcontroller.h ('k') | webrtc/p2p/base/transportcontroller_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/p2p/base/transportcontroller.h" 11 #include "webrtc/p2p/base/transportcontroller.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 #include <memory> 14 #include <memory>
15 15
16 #include "webrtc/base/bind.h" 16 #include "webrtc/base/bind.h"
17 #include "webrtc/base/checks.h" 17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/thread.h" 18 #include "webrtc/base/thread.h"
19 #include "webrtc/p2p/base/dtlstransport.h"
20 #include "webrtc/p2p/base/p2ptransport.h"
19 #include "webrtc/p2p/base/port.h" 21 #include "webrtc/p2p/base/port.h"
20 22
23 #ifdef HAVE_QUIC
24 #include "webrtc/p2p/quic/quictransport.h"
25 #endif // HAVE_QUIC
26
21 namespace cricket { 27 namespace cricket {
22 28
23 enum { 29 enum {
24 MSG_ICECONNECTIONSTATE, 30 MSG_ICECONNECTIONSTATE,
25 MSG_RECEIVING, 31 MSG_RECEIVING,
26 MSG_ICEGATHERINGSTATE, 32 MSG_ICEGATHERINGSTATE,
27 MSG_CANDIDATESGATHERED, 33 MSG_CANDIDATESGATHERED,
28 }; 34 };
29 35
30 struct CandidatesData : public rtc::MessageData { 36 struct CandidatesData : public rtc::MessageData {
(...skipping 16 matching lines...) Expand all
47 53
48 TransportController::TransportController(rtc::Thread* signaling_thread, 54 TransportController::TransportController(rtc::Thread* signaling_thread,
49 rtc::Thread* network_thread, 55 rtc::Thread* network_thread,
50 PortAllocator* port_allocator) 56 PortAllocator* port_allocator)
51 : TransportController(signaling_thread, 57 : TransportController(signaling_thread,
52 network_thread, 58 network_thread,
53 port_allocator, 59 port_allocator,
54 true) {} 60 true) {}
55 61
56 TransportController::~TransportController() { 62 TransportController::~TransportController() {
57 // Channel destructors may try to send packets, so this needs to happen on
58 // the network thread.
59 network_thread_->Invoke<void>( 63 network_thread_->Invoke<void>(
60 RTC_FROM_HERE, 64 RTC_FROM_HERE,
61 rtc::Bind(&TransportController::DestroyAllChannels_n, this)); 65 rtc::Bind(&TransportController::DestroyAllTransports_n, this));
66 signaling_thread_->Clear(this);
62 } 67 }
63 68
64 bool TransportController::SetSslMaxProtocolVersion( 69 bool TransportController::SetSslMaxProtocolVersion(
65 rtc::SSLProtocolVersion version) { 70 rtc::SSLProtocolVersion version) {
66 return network_thread_->Invoke<bool>( 71 return network_thread_->Invoke<bool>(
67 RTC_FROM_HERE, rtc::Bind(&TransportController::SetSslMaxProtocolVersion_n, 72 RTC_FROM_HERE, rtc::Bind(&TransportController::SetSslMaxProtocolVersion_n,
68 this, version)); 73 this, version));
69 } 74 }
70 75
71 void TransportController::SetIceConfig(const IceConfig& config) { 76 void TransportController::SetIceConfig(const IceConfig& config) {
72 network_thread_->Invoke<void>( 77 network_thread_->Invoke<void>(
73 RTC_FROM_HERE, 78 RTC_FROM_HERE,
74 rtc::Bind(&TransportController::SetIceConfig_n, this, config)); 79 rtc::Bind(&TransportController::SetIceConfig_n, this, config));
75 } 80 }
76 81
77 void TransportController::SetIceRole(IceRole ice_role) { 82 void TransportController::SetIceRole(IceRole ice_role) {
78 network_thread_->Invoke<void>( 83 network_thread_->Invoke<void>(
79 RTC_FROM_HERE, 84 RTC_FROM_HERE,
80 rtc::Bind(&TransportController::SetIceRole_n, this, ice_role)); 85 rtc::Bind(&TransportController::SetIceRole_n, this, ice_role));
81 } 86 }
82 87
83 bool TransportController::GetSslRole(const std::string& transport_name, 88 bool TransportController::GetSslRole(const std::string& transport_name,
84 rtc::SSLRole* role) const { 89 rtc::SSLRole* role) {
85 return network_thread_->Invoke<bool>( 90 return network_thread_->Invoke<bool>(
86 RTC_FROM_HERE, rtc::Bind(&TransportController::GetSslRole_n, this, 91 RTC_FROM_HERE, rtc::Bind(&TransportController::GetSslRole_n, this,
87 transport_name, role)); 92 transport_name, role));
88 } 93 }
89 94
90 bool TransportController::SetLocalCertificate( 95 bool TransportController::SetLocalCertificate(
91 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { 96 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
92 return network_thread_->Invoke<bool>( 97 return network_thread_->Invoke<bool>(
93 RTC_FROM_HERE, rtc::Bind(&TransportController::SetLocalCertificate_n, 98 RTC_FROM_HERE, rtc::Bind(&TransportController::SetLocalCertificate_n,
94 this, certificate)); 99 this, certificate));
95 } 100 }
96 101
97 bool TransportController::GetLocalCertificate( 102 bool TransportController::GetLocalCertificate(
98 const std::string& transport_name, 103 const std::string& transport_name,
99 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const { 104 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
100 return network_thread_->Invoke<bool>( 105 return network_thread_->Invoke<bool>(
101 RTC_FROM_HERE, rtc::Bind(&TransportController::GetLocalCertificate_n, 106 RTC_FROM_HERE, rtc::Bind(&TransportController::GetLocalCertificate_n,
102 this, transport_name, certificate)); 107 this, transport_name, certificate));
103 } 108 }
104 109
105 std::unique_ptr<rtc::SSLCertificate> 110 std::unique_ptr<rtc::SSLCertificate>
106 TransportController::GetRemoteSSLCertificate( 111 TransportController::GetRemoteSSLCertificate(
107 const std::string& transport_name) const { 112 const std::string& transport_name) {
108 return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertificate>>( 113 return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertificate>>(
109 RTC_FROM_HERE, rtc::Bind(&TransportController::GetRemoteSSLCertificate_n, 114 RTC_FROM_HERE, rtc::Bind(&TransportController::GetRemoteSSLCertificate_n,
110 this, transport_name)); 115 this, transport_name));
111 } 116 }
112 117
113 bool TransportController::SetLocalTransportDescription( 118 bool TransportController::SetLocalTransportDescription(
114 const std::string& transport_name, 119 const std::string& transport_name,
115 const TransportDescription& tdesc, 120 const TransportDescription& tdesc,
116 ContentAction action, 121 ContentAction action,
117 std::string* err) { 122 std::string* err) {
(...skipping 29 matching lines...) Expand all
147 } 152 }
148 153
149 bool TransportController::RemoveRemoteCandidates(const Candidates& candidates, 154 bool TransportController::RemoveRemoteCandidates(const Candidates& candidates,
150 std::string* err) { 155 std::string* err) {
151 return network_thread_->Invoke<bool>( 156 return network_thread_->Invoke<bool>(
152 RTC_FROM_HERE, rtc::Bind(&TransportController::RemoveRemoteCandidates_n, 157 RTC_FROM_HERE, rtc::Bind(&TransportController::RemoveRemoteCandidates_n,
153 this, candidates, err)); 158 this, candidates, err));
154 } 159 }
155 160
156 bool TransportController::ReadyForRemoteCandidates( 161 bool TransportController::ReadyForRemoteCandidates(
157 const std::string& transport_name) const { 162 const std::string& transport_name) {
158 return network_thread_->Invoke<bool>( 163 return network_thread_->Invoke<bool>(
159 RTC_FROM_HERE, rtc::Bind(&TransportController::ReadyForRemoteCandidates_n, 164 RTC_FROM_HERE, rtc::Bind(&TransportController::ReadyForRemoteCandidates_n,
160 this, transport_name)); 165 this, transport_name));
161 } 166 }
162 167
163 bool TransportController::GetStats(const std::string& transport_name, 168 bool TransportController::GetStats(const std::string& transport_name,
164 TransportStats* stats) { 169 TransportStats* stats) {
165 return network_thread_->Invoke<bool>( 170 return network_thread_->Invoke<bool>(
166 RTC_FROM_HERE, 171 RTC_FROM_HERE,
167 rtc::Bind(&TransportController::GetStats_n, this, transport_name, stats)); 172 rtc::Bind(&TransportController::GetStats_n, this, transport_name, stats));
168 } 173 }
169 174
170 void TransportController::SetMetricsObserver(
171 webrtc::MetricsObserverInterface* metrics_observer) {
172 return network_thread_->Invoke<void>(
173 RTC_FROM_HERE, rtc::Bind(&TransportController::SetMetricsObserver_n, this,
174 metrics_observer));
175 }
176
177 TransportChannel* TransportController::CreateTransportChannel_n( 175 TransportChannel* TransportController::CreateTransportChannel_n(
178 const std::string& transport_name, 176 const std::string& transport_name,
179 int component) { 177 int component) {
180 RTC_DCHECK(network_thread_->IsCurrent()); 178 RTC_DCHECK(network_thread_->IsCurrent());
181 179
182 RefCountedChannel* existing_channel = GetChannel_n(transport_name, component); 180 auto it = FindChannel_n(transport_name, component);
183 if (existing_channel) { 181 if (it != channels_.end()) {
184 // Channel already exists; increment reference count and return. 182 // Channel already exists; increment reference count and return.
185 existing_channel->AddRef(); 183 it->AddRef();
186 return existing_channel->dtls(); 184 return it->get();
187 } 185 }
188 186
189 // Need to create a new channel. 187 // Need to create a new channel.
190 JsepTransport* transport = GetOrCreateJsepTransport_n(transport_name); 188 Transport* transport = GetOrCreateTransport_n(transport_name);
191 189 TransportChannelImpl* channel = transport->CreateChannel(component);
192 // Create DTLS channel wrapping ICE channel, and configure it. 190 channel->SetMetricsObserver(metrics_observer_);
193 TransportChannelImpl* ice = 191 channel->SignalWritableState.connect(
194 CreateIceTransportChannel_n(transport_name, component);
195 // TODO(deadbeef): To support QUIC, would need to create a
196 // QuicTransportChannel here. What is "dtls" in this file would then become
197 // "dtls or quic".
198 TransportChannelImpl* dtls =
199 CreateDtlsTransportChannel_n(transport_name, component, ice);
200 dtls->SetMetricsObserver(metrics_observer_);
201 dtls->SetIceRole(ice_role_);
202 dtls->SetIceTiebreaker(ice_tiebreaker_);
203 dtls->SetIceConfig(ice_config_);
204 if (certificate_) {
205 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
206 RTC_DCHECK(set_cert_success);
207 }
208
209 // Connect to signals offered by the channels. Currently, the DTLS channel
210 // forwards signals from the ICE channel, so we only need to connect to the
211 // DTLS channel. In the future this won't be the case.
212 dtls->SignalWritableState.connect(
213 this, &TransportController::OnChannelWritableState_n); 192 this, &TransportController::OnChannelWritableState_n);
214 dtls->SignalReceivingState.connect( 193 channel->SignalReceivingState.connect(
215 this, &TransportController::OnChannelReceivingState_n); 194 this, &TransportController::OnChannelReceivingState_n);
216 dtls->SignalGatheringState.connect( 195 channel->SignalGatheringState.connect(
217 this, &TransportController::OnChannelGatheringState_n); 196 this, &TransportController::OnChannelGatheringState_n);
218 dtls->SignalCandidateGathered.connect( 197 channel->SignalCandidateGathered.connect(
219 this, &TransportController::OnChannelCandidateGathered_n); 198 this, &TransportController::OnChannelCandidateGathered_n);
220 dtls->SignalCandidatesRemoved.connect( 199 channel->SignalCandidatesRemoved.connect(
221 this, &TransportController::OnChannelCandidatesRemoved_n); 200 this, &TransportController::OnChannelCandidatesRemoved_n);
222 dtls->SignalRoleConflict.connect( 201 channel->SignalRoleConflict.connect(
223 this, &TransportController::OnChannelRoleConflict_n); 202 this, &TransportController::OnChannelRoleConflict_n);
224 dtls->SignalStateChanged.connect( 203 channel->SignalStateChanged.connect(
225 this, &TransportController::OnChannelStateChanged_n); 204 this, &TransportController::OnChannelStateChanged_n);
226 dtls->SignalDtlsHandshakeError.connect( 205 channel->SignalDtlsHandshakeError.connect(
227 this, &TransportController::OnDtlsHandshakeError); 206 this, &TransportController::OnDtlsHandshakeError);
228 channels_.insert(channels_.end(), RefCountedChannel(dtls, ice))->AddRef(); 207 channels_.insert(channels_.end(), RefCountedChannel(channel))->AddRef();
229 bool channel_added = transport->AddChannel(dtls, component);
230 RTC_DCHECK(channel_added);
231 // Adding a channel could cause aggregate state to change. 208 // Adding a channel could cause aggregate state to change.
232 UpdateAggregateStates_n(); 209 UpdateAggregateStates_n();
233 return dtls; 210 return channel;
234 } 211 }
235 212
236 void TransportController::DestroyTransportChannel_n( 213 void TransportController::DestroyTransportChannel_n(
237 const std::string& transport_name, 214 const std::string& transport_name,
238 int component) { 215 int component) {
239 RTC_DCHECK(network_thread_->IsCurrent()); 216 RTC_DCHECK(network_thread_->IsCurrent());
240 217
241 auto it = GetChannelIterator_n(transport_name, component); 218 auto it = FindChannel_n(transport_name, component);
242 if (it == channels_.end()) { 219 if (it == channels_.end()) {
243 LOG(LS_WARNING) << "Attempting to delete " << transport_name 220 LOG(LS_WARNING) << "Attempting to delete " << transport_name
244 << " TransportChannel " << component 221 << " TransportChannel " << component
245 << ", which doesn't exist."; 222 << ", which doesn't exist.";
246 return; 223 return;
247 } 224 }
225
248 it->DecRef(); 226 it->DecRef();
249 if (it->ref() > 0) { 227 if (it->ref() > 0) {
250 return; 228 return;
251 } 229 }
230
252 channels_.erase(it); 231 channels_.erase(it);
253 232 Transport* transport = GetTransport_n(transport_name);
254 JsepTransport* t = GetJsepTransport_n(transport_name); 233 transport->DestroyChannel(component);
255 bool channel_removed = t->RemoveChannel(component);
256 RTC_DCHECK(channel_removed);
257 // Just as we create a Transport when its first channel is created, 234 // Just as we create a Transport when its first channel is created,
258 // we delete it when its last channel is deleted. 235 // we delete it when its last channel is deleted.
259 if (!t->HasChannels()) { 236 if (!transport->HasChannels()) {
260 transports_.erase(transport_name); 237 DestroyTransport_n(transport_name);
261 } 238 }
262
263 // Removing a channel could cause aggregate state to change. 239 // Removing a channel could cause aggregate state to change.
264 UpdateAggregateStates_n(); 240 UpdateAggregateStates_n();
265 } 241 }
266 242
267 std::vector<std::string> TransportController::transport_names_for_testing() { 243 const rtc::scoped_refptr<rtc::RTCCertificate>&
268 std::vector<std::string> ret; 244 TransportController::certificate_for_testing() {
269 for (const auto& kv : transports_) { 245 return certificate_;
270 ret.push_back(kv.first);
271 }
272 return ret;
273 } 246 }
274 247
275 std::vector<TransportChannelImpl*> TransportController::channels_for_testing() { 248 Transport* TransportController::CreateTransport_n(
276 std::vector<TransportChannelImpl*> ret; 249 const std::string& transport_name) {
277 for (RefCountedChannel& channel : channels_) { 250 RTC_DCHECK(network_thread_->IsCurrent());
278 ret.push_back(channel.dtls()); 251
252 #ifdef HAVE_QUIC
253 if (quic_) {
254 return new QuicTransport(transport_name, port_allocator(), certificate_);
279 } 255 }
280 return ret; 256 #endif // HAVE_QUIC
257 Transport* transport = new DtlsTransport<P2PTransport>(
258 transport_name, port_allocator(), certificate_);
259 return transport;
281 } 260 }
282 261
283 TransportChannelImpl* TransportController::get_channel_for_testing( 262 Transport* TransportController::GetTransport_n(
284 const std::string& transport_name, 263 const std::string& transport_name) {
285 int component) { 264 RTC_DCHECK(network_thread_->IsCurrent());
286 RefCountedChannel* ch = GetChannel_n(transport_name, component);
287 return ch ? ch->dtls() : nullptr;
288 }
289 265
290 TransportChannelImpl* TransportController::CreateIceTransportChannel_n( 266 auto iter = transports_.find(transport_name);
291 const std::string& transport_name, 267 return (iter != transports_.end()) ? iter->second : nullptr;
292 int component) {
293 return new P2PTransportChannel(transport_name, component, port_allocator_);
294 }
295
296 TransportChannelImpl* TransportController::CreateDtlsTransportChannel_n(
297 const std::string&,
298 int,
299 TransportChannelImpl* ice) {
300 DtlsTransportChannelWrapper* dtls = new DtlsTransportChannelWrapper(ice);
301 dtls->SetSslMaxProtocolVersion(ssl_max_version_);
302 return dtls;
303 } 268 }
304 269
305 void TransportController::OnMessage(rtc::Message* pmsg) { 270 void TransportController::OnMessage(rtc::Message* pmsg) {
306 RTC_DCHECK(signaling_thread_->IsCurrent()); 271 RTC_DCHECK(signaling_thread_->IsCurrent());
307 272
308 switch (pmsg->message_id) { 273 switch (pmsg->message_id) {
309 case MSG_ICECONNECTIONSTATE: { 274 case MSG_ICECONNECTIONSTATE: {
310 rtc::TypedMessageData<IceConnectionState>* data = 275 rtc::TypedMessageData<IceConnectionState>* data =
311 static_cast<rtc::TypedMessageData<IceConnectionState>*>(pmsg->pdata); 276 static_cast<rtc::TypedMessageData<IceConnectionState>*>(pmsg->pdata);
312 SignalConnectionState(data->data()); 277 SignalConnectionState(data->data());
(...skipping 19 matching lines...) Expand all
332 SignalCandidatesGathered(data->transport_name, data->candidates); 297 SignalCandidatesGathered(data->transport_name, data->candidates);
333 delete data; 298 delete data;
334 break; 299 break;
335 } 300 }
336 default: 301 default:
337 ASSERT(false); 302 ASSERT(false);
338 } 303 }
339 } 304 }
340 305
341 std::vector<TransportController::RefCountedChannel>::iterator 306 std::vector<TransportController::RefCountedChannel>::iterator
342 TransportController::GetChannelIterator_n(const std::string& transport_name, 307 TransportController::FindChannel_n(const std::string& transport_name,
343 int component) { 308 int component) {
344 RTC_DCHECK(network_thread_->IsCurrent());
345 return std::find_if( 309 return std::find_if(
346 channels_.begin(), channels_.end(), 310 channels_.begin(), channels_.end(),
347 [transport_name, component](const RefCountedChannel& channel) { 311 [transport_name, component](const RefCountedChannel& channel) {
348 return channel.dtls()->transport_name() == transport_name && 312 return channel->transport_name() == transport_name &&
349 channel.dtls()->component() == component; 313 channel->component() == component;
350 }); 314 });
351 } 315 }
352 316
353 std::vector<TransportController::RefCountedChannel>::const_iterator 317 Transport* TransportController::GetOrCreateTransport_n(
354 TransportController::GetChannelIterator_n(const std::string& transport_name,
355 int component) const {
356 RTC_DCHECK(network_thread_->IsCurrent());
357 return std::find_if(
358 channels_.begin(), channels_.end(),
359 [transport_name, component](const RefCountedChannel& channel) {
360 return channel.dtls()->transport_name() == transport_name &&
361 channel.dtls()->component() == component;
362 });
363 }
364
365 const JsepTransport* TransportController::GetJsepTransport_n(
366 const std::string& transport_name) const {
367 RTC_DCHECK(network_thread_->IsCurrent());
368 auto it = transports_.find(transport_name);
369 return (it == transports_.end()) ? nullptr : it->second.get();
370 }
371
372 JsepTransport* TransportController::GetJsepTransport_n(
373 const std::string& transport_name) {
374 RTC_DCHECK(network_thread_->IsCurrent());
375 auto it = transports_.find(transport_name);
376 return (it == transports_.end()) ? nullptr : it->second.get();
377 }
378
379 const TransportController::RefCountedChannel* TransportController::GetChannel_n(
380 const std::string& transport_name,
381 int component) const {
382 RTC_DCHECK(network_thread_->IsCurrent());
383 auto it = GetChannelIterator_n(transport_name, component);
384 return (it == channels_.end()) ? nullptr : &(*it);
385 }
386
387 TransportController::RefCountedChannel* TransportController::GetChannel_n(
388 const std::string& transport_name,
389 int component) {
390 RTC_DCHECK(network_thread_->IsCurrent());
391 auto it = GetChannelIterator_n(transport_name, component);
392 return (it == channels_.end()) ? nullptr : &(*it);
393 }
394
395 JsepTransport* TransportController::GetOrCreateJsepTransport_n(
396 const std::string& transport_name) { 318 const std::string& transport_name) {
397 RTC_DCHECK(network_thread_->IsCurrent()); 319 RTC_DCHECK(network_thread_->IsCurrent());
398 320
399 JsepTransport* transport = GetJsepTransport_n(transport_name); 321 Transport* transport = GetTransport_n(transport_name);
400 if (transport) { 322 if (transport) {
401 return transport; 323 return transport;
402 } 324 }
403 325
404 transport = new JsepTransport(transport_name, certificate_); 326 transport = CreateTransport_n(transport_name);
405 transports_[transport_name] = std::unique_ptr<JsepTransport>(transport); 327 // The stuff below happens outside of CreateTransport_w so that unit tests
328 // can override CreateTransport_w to return a different type of transport.
329 transport->SetSslMaxProtocolVersion(ssl_max_version_);
330 transport->SetIceConfig(ice_config_);
331 transport->SetIceRole(ice_role_);
332 transport->SetIceTiebreaker(ice_tiebreaker_);
333 if (certificate_) {
334 transport->SetLocalCertificate(certificate_);
335 }
336 transports_[transport_name] = transport;
337
406 return transport; 338 return transport;
407 } 339 }
408 340
409 void TransportController::DestroyAllChannels_n() { 341 void TransportController::DestroyTransport_n(
342 const std::string& transport_name) {
410 RTC_DCHECK(network_thread_->IsCurrent()); 343 RTC_DCHECK(network_thread_->IsCurrent());
344
345 auto iter = transports_.find(transport_name);
346 if (iter != transports_.end()) {
347 delete iter->second;
348 transports_.erase(transport_name);
349 }
350 }
351
352 void TransportController::DestroyAllTransports_n() {
353 RTC_DCHECK(network_thread_->IsCurrent());
354
355 for (const auto& kv : transports_) {
356 delete kv.second;
357 }
411 transports_.clear(); 358 transports_.clear();
412 channels_.clear();
413 } 359 }
414 360
415 bool TransportController::SetSslMaxProtocolVersion_n( 361 bool TransportController::SetSslMaxProtocolVersion_n(
416 rtc::SSLProtocolVersion version) { 362 rtc::SSLProtocolVersion version) {
417 RTC_DCHECK(network_thread_->IsCurrent()); 363 RTC_DCHECK(network_thread_->IsCurrent());
418 364
419 // Max SSL version can only be set before transports are created. 365 // Max SSL version can only be set before transports are created.
420 if (!transports_.empty()) { 366 if (!transports_.empty()) {
421 return false; 367 return false;
422 } 368 }
423 369
424 ssl_max_version_ = version; 370 ssl_max_version_ = version;
425 return true; 371 return true;
426 } 372 }
427 373
428 void TransportController::SetIceConfig_n(const IceConfig& config) { 374 void TransportController::SetIceConfig_n(const IceConfig& config) {
429 RTC_DCHECK(network_thread_->IsCurrent()); 375 RTC_DCHECK(network_thread_->IsCurrent());
430
431 ice_config_ = config; 376 ice_config_ = config;
432 for (auto& channel : channels_) { 377 for (const auto& kv : transports_) {
433 channel.dtls()->SetIceConfig(ice_config_); 378 kv.second->SetIceConfig(ice_config_);
434 } 379 }
435 } 380 }
436 381
437 void TransportController::SetIceRole_n(IceRole ice_role) { 382 void TransportController::SetIceRole_n(IceRole ice_role) {
438 RTC_DCHECK(network_thread_->IsCurrent()); 383 RTC_DCHECK(network_thread_->IsCurrent());
439
440 ice_role_ = ice_role; 384 ice_role_ = ice_role;
441 for (auto& channel : channels_) { 385 for (const auto& kv : transports_) {
442 channel.dtls()->SetIceRole(ice_role_); 386 kv.second->SetIceRole(ice_role_);
443 } 387 }
444 } 388 }
445 389
446 bool TransportController::GetSslRole_n(const std::string& transport_name, 390 bool TransportController::GetSslRole_n(const std::string& transport_name,
447 rtc::SSLRole* role) const { 391 rtc::SSLRole* role) {
448 RTC_DCHECK(network_thread_->IsCurrent()); 392 RTC_DCHECK(network_thread_->IsCurrent());
449 393
450 const JsepTransport* t = GetJsepTransport_n(transport_name); 394 Transport* t = GetTransport_n(transport_name);
451 if (!t) { 395 if (!t) {
452 return false; 396 return false;
453 } 397 }
454 t->GetSslRole(role); 398
455 return true; 399 return t->GetSslRole(role);
456 } 400 }
457 401
458 bool TransportController::SetLocalCertificate_n( 402 bool TransportController::SetLocalCertificate_n(
459 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { 403 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
460 RTC_DCHECK(network_thread_->IsCurrent()); 404 RTC_DCHECK(network_thread_->IsCurrent());
461 405
462 // Can't change a certificate, or set a null certificate. 406 if (certificate_) {
463 if (certificate_ || !certificate) { 407 return false;
408 }
409 if (!certificate) {
464 return false; 410 return false;
465 } 411 }
466 certificate_ = certificate; 412 certificate_ = certificate;
467 413
468 // Set certificate both for Transport, which verifies it matches the 414 for (const auto& kv : transports_) {
469 // fingerprint in SDP...
470 for (auto& kv : transports_) {
471 kv.second->SetLocalCertificate(certificate_); 415 kv.second->SetLocalCertificate(certificate_);
472 } 416 }
473 // ... and for the DTLS channel, which needs it for the DTLS handshake.
474 for (auto& channel : channels_) {
475 bool set_cert_success = channel.dtls()->SetLocalCertificate(certificate);
476 RTC_DCHECK(set_cert_success);
477 }
478 return true; 417 return true;
479 } 418 }
480 419
481 bool TransportController::GetLocalCertificate_n( 420 bool TransportController::GetLocalCertificate_n(
482 const std::string& transport_name, 421 const std::string& transport_name,
483 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const { 422 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
484 RTC_DCHECK(network_thread_->IsCurrent()); 423 RTC_DCHECK(network_thread_->IsCurrent());
485 424
486 const JsepTransport* t = GetJsepTransport_n(transport_name); 425 Transport* t = GetTransport_n(transport_name);
487 if (!t) { 426 if (!t) {
488 return false; 427 return false;
489 } 428 }
429
490 return t->GetLocalCertificate(certificate); 430 return t->GetLocalCertificate(certificate);
491 } 431 }
492 432
493 std::unique_ptr<rtc::SSLCertificate> 433 std::unique_ptr<rtc::SSLCertificate>
494 TransportController::GetRemoteSSLCertificate_n( 434 TransportController::GetRemoteSSLCertificate_n(
495 const std::string& transport_name) const { 435 const std::string& transport_name) {
496 RTC_DCHECK(network_thread_->IsCurrent()); 436 RTC_DCHECK(network_thread_->IsCurrent());
497 437
498 // Get the certificate from the RTP channel's DTLS handshake. Should be 438 Transport* t = GetTransport_n(transport_name);
499 // identical to the RTCP channel's, since they were given the same remote 439 if (!t) {
500 // fingerprint.
501 const RefCountedChannel* ch = GetChannel_n(transport_name, 1);
502 if (!ch) {
503 return nullptr; 440 return nullptr;
504 } 441 }
505 return ch->dtls()->GetRemoteSSLCertificate(); 442
443 return t->GetRemoteSSLCertificate();
506 } 444 }
507 445
508 bool TransportController::SetLocalTransportDescription_n( 446 bool TransportController::SetLocalTransportDescription_n(
509 const std::string& transport_name, 447 const std::string& transport_name,
510 const TransportDescription& tdesc, 448 const TransportDescription& tdesc,
511 ContentAction action, 449 ContentAction action,
512 std::string* err) { 450 std::string* err) {
513 RTC_DCHECK(network_thread_->IsCurrent()); 451 RTC_DCHECK(network_thread_->IsCurrent());
514 452
515 JsepTransport* transport = GetJsepTransport_n(transport_name); 453 Transport* transport = GetTransport_n(transport_name);
516 if (!transport) { 454 if (!transport) {
517 // If we didn't find a transport, that's not an error; 455 // If we didn't find a transport, that's not an error;
518 // it could have been deleted as a result of bundling. 456 // it could have been deleted as a result of bundling.
519 // TODO(deadbeef): Make callers smarter so they won't attempt to set a 457 // TODO(deadbeef): Make callers smarter so they won't attempt to set a
520 // description on a deleted transport. 458 // description on a deleted transport.
521 return true; 459 return true;
522 } 460 }
523 461
524 // Older versions of Chrome expect the ICE role to be re-determined when an 462 // Older versions of Chrome expect the ICE role to be re-determined when an
525 // ICE restart occurs, and also don't perform conflict resolution correctly, 463 // ICE restart occurs, and also don't perform conflict resolution correctly,
(...skipping 15 matching lines...) Expand all
541 return transport->SetLocalTransportDescription(tdesc, action, err); 479 return transport->SetLocalTransportDescription(tdesc, action, err);
542 } 480 }
543 481
544 bool TransportController::SetRemoteTransportDescription_n( 482 bool TransportController::SetRemoteTransportDescription_n(
545 const std::string& transport_name, 483 const std::string& transport_name,
546 const TransportDescription& tdesc, 484 const TransportDescription& tdesc,
547 ContentAction action, 485 ContentAction action,
548 std::string* err) { 486 std::string* err) {
549 RTC_DCHECK(network_thread_->IsCurrent()); 487 RTC_DCHECK(network_thread_->IsCurrent());
550 488
551 // If our role is ICEROLE_CONTROLLED and the remote endpoint supports only 489 Transport* transport = GetTransport_n(transport_name);
552 // ice_lite, this local endpoint should take the CONTROLLING role.
553 // TODO(deadbeef): This is a session-level attribute, so it really shouldn't
554 // be in a TransportDescription in the first place...
555 if (ice_role_ == ICEROLE_CONTROLLED && tdesc.ice_mode == ICEMODE_LITE) {
556 SetIceRole_n(ICEROLE_CONTROLLING);
557 }
558
559 JsepTransport* transport = GetJsepTransport_n(transport_name);
560 if (!transport) { 490 if (!transport) {
561 // If we didn't find a transport, that's not an error; 491 // If we didn't find a transport, that's not an error;
562 // it could have been deleted as a result of bundling. 492 // it could have been deleted as a result of bundling.
563 // TODO(deadbeef): Make callers smarter so they won't attempt to set a 493 // TODO(deadbeef): Make callers smarter so they won't attempt to set a
564 // description on a deleted transport. 494 // description on a deleted transport.
565 return true; 495 return true;
566 } 496 }
567 497
568 LOG(LS_INFO) << "Set remote transport description on " << transport_name; 498 LOG(LS_INFO) << "Set remote transport description on " << transport_name;
569 return transport->SetRemoteTransportDescription(tdesc, action, err); 499 return transport->SetRemoteTransportDescription(tdesc, action, err);
570 } 500 }
571 501
572 void TransportController::MaybeStartGathering_n() { 502 void TransportController::MaybeStartGathering_n() {
573 for (auto& channel : channels_) { 503 for (const auto& kv : transports_) {
574 channel.dtls()->MaybeStartGathering(); 504 kv.second->MaybeStartGathering();
575 } 505 }
576 } 506 }
577 507
578 bool TransportController::AddRemoteCandidates_n( 508 bool TransportController::AddRemoteCandidates_n(
579 const std::string& transport_name, 509 const std::string& transport_name,
580 const Candidates& candidates, 510 const Candidates& candidates,
581 std::string* err) { 511 std::string* err) {
582 RTC_DCHECK(network_thread_->IsCurrent()); 512 RTC_DCHECK(network_thread_->IsCurrent());
583 513
584 // Verify each candidate before passing down to the transport layer. 514 Transport* transport = GetTransport_n(transport_name);
585 if (!VerifyCandidates(candidates, err)) {
586 return false;
587 }
588
589 JsepTransport* transport = GetJsepTransport_n(transport_name);
590 if (!transport) { 515 if (!transport) {
591 // If we didn't find a transport, that's not an error; 516 // If we didn't find a transport, that's not an error;
592 // it could have been deleted as a result of bundling. 517 // it could have been deleted as a result of bundling.
593 return true; 518 return true;
594 } 519 }
595 520
596 for (const Candidate& candidate : candidates) { 521 return transport->AddRemoteCandidates(candidates, err);
597 RefCountedChannel* channel =
598 GetChannel_n(transport_name, candidate.component());
599 if (!channel) {
600 *err = "Candidate has an unknown component: " + candidate.ToString() +
601 " for content: " + transport_name;
602 return false;
603 }
604 channel->dtls()->AddRemoteCandidate(candidate);
605 }
606 return true;
607 } 522 }
608 523
609 bool TransportController::RemoveRemoteCandidates_n(const Candidates& candidates, 524 bool TransportController::RemoveRemoteCandidates_n(const Candidates& candidates,
610 std::string* err) { 525 std::string* err) {
611 RTC_DCHECK(network_thread_->IsCurrent()); 526 RTC_DCHECK(network_thread_->IsCurrent());
612
613 // Verify each candidate before passing down to the transport layer.
614 if (!VerifyCandidates(candidates, err)) {
615 return false;
616 }
617
618 std::map<std::string, Candidates> candidates_by_transport_name; 527 std::map<std::string, Candidates> candidates_by_transport_name;
619 for (const Candidate& cand : candidates) { 528 for (const Candidate& cand : candidates) {
620 RTC_DCHECK(!cand.transport_name().empty()); 529 RTC_DCHECK(!cand.transport_name().empty());
621 candidates_by_transport_name[cand.transport_name()].push_back(cand); 530 candidates_by_transport_name[cand.transport_name()].push_back(cand);
622 } 531 }
623 532
624 bool result = true; 533 bool result = true;
625 for (const auto& kv : candidates_by_transport_name) { 534 for (auto kv : candidates_by_transport_name) {
626 const std::string& transport_name = kv.first; 535 Transport* transport = GetTransport_n(kv.first);
627 const Candidates& candidates = kv.second;
628 JsepTransport* transport = GetJsepTransport_n(transport_name);
629 if (!transport) { 536 if (!transport) {
630 // If we didn't find a transport, that's not an error; 537 // If we didn't find a transport, that's not an error;
631 // it could have been deleted as a result of bundling. 538 // it could have been deleted as a result of bundling.
632 continue; 539 continue;
633 } 540 }
634 for (const Candidate& candidate : candidates) { 541 result &= transport->RemoveRemoteCandidates(kv.second, err);
635 RefCountedChannel* channel =
636 GetChannel_n(transport_name, candidate.component());
637 if (channel) {
638 channel->dtls()->RemoveRemoteCandidate(candidate);
639 }
640 }
641 } 542 }
642 return result; 543 return result;
643 } 544 }
644 545
645 bool TransportController::ReadyForRemoteCandidates_n( 546 bool TransportController::ReadyForRemoteCandidates_n(
646 const std::string& transport_name) const { 547 const std::string& transport_name) {
647 RTC_DCHECK(network_thread_->IsCurrent()); 548 RTC_DCHECK(network_thread_->IsCurrent());
648 549
649 const JsepTransport* transport = GetJsepTransport_n(transport_name); 550 Transport* transport = GetTransport_n(transport_name);
650 if (!transport) { 551 if (!transport) {
651 return false; 552 return false;
652 } 553 }
653 return transport->ready_for_remote_candidates(); 554 return transport->ready_for_remote_candidates();
654 } 555 }
655 556
656 bool TransportController::GetStats_n(const std::string& transport_name, 557 bool TransportController::GetStats_n(const std::string& transport_name,
657 TransportStats* stats) { 558 TransportStats* stats) {
658 RTC_DCHECK(network_thread_->IsCurrent()); 559 RTC_DCHECK(network_thread_->IsCurrent());
659 560
660 JsepTransport* transport = GetJsepTransport_n(transport_name); 561 Transport* transport = GetTransport_n(transport_name);
661 if (!transport) { 562 if (!transport) {
662 return false; 563 return false;
663 } 564 }
664 return transport->GetStats(stats); 565 return transport->GetStats(stats);
665 } 566 }
666 567
667 void TransportController::SetMetricsObserver_n(
668 webrtc::MetricsObserverInterface* metrics_observer) {
669 RTC_DCHECK(network_thread_->IsCurrent());
670 metrics_observer_ = metrics_observer;
671 for (auto& channel : channels_) {
672 channel.dtls()->SetMetricsObserver(metrics_observer);
673 }
674 }
675
676 void TransportController::OnChannelWritableState_n( 568 void TransportController::OnChannelWritableState_n(
677 rtc::PacketTransportInterface* transport) { 569 rtc::PacketTransportInterface* transport) {
678 RTC_DCHECK(network_thread_->IsCurrent()); 570 RTC_DCHECK(network_thread_->IsCurrent());
679 LOG(LS_INFO) << " TransportChannel " << transport->debug_name() 571 LOG(LS_INFO) << " TransportChannel " << transport->debug_name()
680 << " writability changed to " << transport->writable() << "."; 572 << " writability changed to " << transport->writable() << ".";
681 UpdateAggregateStates_n(); 573 UpdateAggregateStates_n();
682 } 574 }
683 575
684 void TransportController::OnChannelReceivingState_n( 576 void TransportController::OnChannelReceivingState_n(
685 rtc::PacketTransportInterface* transport) { 577 rtc::PacketTransportInterface* transport) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
755 647
756 IceConnectionState new_connection_state = kIceConnectionConnecting; 648 IceConnectionState new_connection_state = kIceConnectionConnecting;
757 IceGatheringState new_gathering_state = kIceGatheringNew; 649 IceGatheringState new_gathering_state = kIceGatheringNew;
758 bool any_receiving = false; 650 bool any_receiving = false;
759 bool any_failed = false; 651 bool any_failed = false;
760 bool all_connected = !channels_.empty(); 652 bool all_connected = !channels_.empty();
761 bool all_completed = !channels_.empty(); 653 bool all_completed = !channels_.empty();
762 bool any_gathering = false; 654 bool any_gathering = false;
763 bool all_done_gathering = !channels_.empty(); 655 bool all_done_gathering = !channels_.empty();
764 for (const auto& channel : channels_) { 656 for (const auto& channel : channels_) {
765 any_receiving = any_receiving || channel.dtls()->receiving(); 657 any_receiving = any_receiving || channel->receiving();
766 any_failed = 658 any_failed = any_failed ||
767 any_failed || 659 channel->GetState() == TransportChannelState::STATE_FAILED;
768 channel.dtls()->GetState() == TransportChannelState::STATE_FAILED; 660 all_connected = all_connected && channel->writable();
769 all_connected = all_connected && channel.dtls()->writable();
770 all_completed = 661 all_completed =
771 all_completed && channel.dtls()->writable() && 662 all_completed && channel->writable() &&
772 channel.dtls()->GetState() == TransportChannelState::STATE_COMPLETED && 663 channel->GetState() == TransportChannelState::STATE_COMPLETED &&
773 channel.dtls()->GetIceRole() == ICEROLE_CONTROLLING && 664 channel->GetIceRole() == ICEROLE_CONTROLLING &&
774 channel.dtls()->gathering_state() == kIceGatheringComplete; 665 channel->gathering_state() == kIceGatheringComplete;
775 any_gathering = 666 any_gathering =
776 any_gathering || channel.dtls()->gathering_state() != kIceGatheringNew; 667 any_gathering || channel->gathering_state() != kIceGatheringNew;
777 all_done_gathering = 668 all_done_gathering = all_done_gathering &&
778 all_done_gathering && 669 channel->gathering_state() == kIceGatheringComplete;
779 channel.dtls()->gathering_state() == kIceGatheringComplete;
780 } 670 }
781 671
782 if (any_failed) { 672 if (any_failed) {
783 new_connection_state = kIceConnectionFailed; 673 new_connection_state = kIceConnectionFailed;
784 } else if (all_completed) { 674 } else if (all_completed) {
785 new_connection_state = kIceConnectionCompleted; 675 new_connection_state = kIceConnectionCompleted;
786 } else if (all_connected) { 676 } else if (all_connected) {
787 new_connection_state = kIceConnectionConnected; 677 new_connection_state = kIceConnectionConnected;
788 } 678 }
789 if (connection_state_ != new_connection_state) { 679 if (connection_state_ != new_connection_state) {
(...skipping 19 matching lines...) Expand all
809 signaling_thread_->Post( 699 signaling_thread_->Post(
810 RTC_FROM_HERE, this, MSG_ICEGATHERINGSTATE, 700 RTC_FROM_HERE, this, MSG_ICEGATHERINGSTATE,
811 new rtc::TypedMessageData<IceGatheringState>(new_gathering_state)); 701 new rtc::TypedMessageData<IceGatheringState>(new_gathering_state));
812 } 702 }
813 } 703 }
814 704
815 void TransportController::OnDtlsHandshakeError(rtc::SSLHandshakeError error) { 705 void TransportController::OnDtlsHandshakeError(rtc::SSLHandshakeError error) {
816 SignalDtlsHandshakeError(error); 706 SignalDtlsHandshakeError(error);
817 } 707 }
818 708
709 void TransportController::SetMetricsObserver(
710 webrtc::MetricsObserverInterface* metrics_observer) {
711 metrics_observer_ = metrics_observer;
712 for (auto channel : channels_) {
713 channel->SetMetricsObserver(metrics_observer);
714 }
715 }
716
819 } // namespace cricket 717 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/p2p/base/transportcontroller.h ('k') | webrtc/p2p/base/transportcontroller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698