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

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

Issue 1350523003: TransportController refactoring. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Adding "MaybeStartGathering", so PeerConnection can explicitly control timing of gathering. Created 5 years, 3 months 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
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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 <utility> // for std::pair
12
11 #include "webrtc/p2p/base/transport.h" 13 #include "webrtc/p2p/base/transport.h"
12 14
13 #include "webrtc/p2p/base/candidate.h" 15 #include "webrtc/p2p/base/candidate.h"
14 #include "webrtc/p2p/base/constants.h" 16 #include "webrtc/p2p/base/constants.h"
15 #include "webrtc/p2p/base/port.h" 17 #include "webrtc/p2p/base/port.h"
16 #include "webrtc/p2p/base/transportchannelimpl.h" 18 #include "webrtc/p2p/base/transportchannelimpl.h"
17 #include "webrtc/base/bind.h" 19 #include "webrtc/base/bind.h"
18 #include "webrtc/base/common.h" 20 #include "webrtc/base/common.h"
19 #include "webrtc/base/logging.h" 21 #include "webrtc/base/logging.h"
20 22
21 namespace cricket { 23 namespace cricket {
22 24
23 using rtc::Bind; 25 using rtc::Bind;
24 26
25 enum {
26 MSG_ONSIGNALINGREADY = 1,
27 MSG_ONREMOTECANDIDATE,
28 MSG_READSTATE,
29 MSG_WRITESTATE,
30 MSG_REQUESTSIGNALING,
31 MSG_CANDIDATEREADY,
32 MSG_ROUTECHANGE,
33 MSG_CONNECTING,
34 MSG_CANDIDATEALLOCATIONCOMPLETE,
35 MSG_ROLECONFLICT,
36 MSG_COMPLETED,
37 MSG_FAILED,
38 MSG_RECEIVINGSTATE,
39 };
40
41 struct ChannelParams : public rtc::MessageData {
42 ChannelParams() : channel(NULL), candidate(NULL) {}
43 explicit ChannelParams(int component)
44 : component(component), channel(NULL), candidate(NULL) {}
45 explicit ChannelParams(Candidate* candidate)
46 : channel(NULL), candidate(candidate) {
47 }
48
49 ~ChannelParams() {
50 delete candidate;
51 }
52
53 std::string name;
54 int component;
55 TransportChannelImpl* channel;
56 Candidate* candidate;
57 };
58
59 static bool VerifyIceParams(const TransportDescription& desc) { 27 static bool VerifyIceParams(const TransportDescription& desc) {
60 // For legacy protocols. 28 // For legacy protocols.
61 if (desc.ice_ufrag.empty() && desc.ice_pwd.empty()) 29 if (desc.ice_ufrag.empty() && desc.ice_pwd.empty())
62 return true; 30 return true;
63 31
64 if (desc.ice_ufrag.length() < ICE_UFRAG_MIN_LENGTH || 32 if (desc.ice_ufrag.length() < ICE_UFRAG_MIN_LENGTH ||
65 desc.ice_ufrag.length() > ICE_UFRAG_MAX_LENGTH) { 33 desc.ice_ufrag.length() > ICE_UFRAG_MAX_LENGTH) {
66 return false; 34 return false;
67 } 35 }
68 if (desc.ice_pwd.length() < ICE_PWD_MIN_LENGTH || 36 if (desc.ice_pwd.length() < ICE_PWD_MIN_LENGTH ||
(...skipping 21 matching lines...) Expand all
90 // should clean this up when GICE is no longer used. 58 // should clean this up when GICE is no longer used.
91 return (old_ufrag != new_ufrag) || (old_pwd != new_pwd); 59 return (old_ufrag != new_ufrag) || (old_pwd != new_pwd);
92 } 60 }
93 61
94 static bool IceCredentialsChanged(const TransportDescription& old_desc, 62 static bool IceCredentialsChanged(const TransportDescription& old_desc,
95 const TransportDescription& new_desc) { 63 const TransportDescription& new_desc) {
96 return IceCredentialsChanged(old_desc.ice_ufrag, old_desc.ice_pwd, 64 return IceCredentialsChanged(old_desc.ice_ufrag, old_desc.ice_pwd,
97 new_desc.ice_ufrag, new_desc.ice_pwd); 65 new_desc.ice_ufrag, new_desc.ice_pwd);
98 } 66 }
99 67
100 Transport::Transport(rtc::Thread* signaling_thread, 68 Transport::Transport(const std::string& name, PortAllocator* allocator)
101 rtc::Thread* worker_thread, 69 : name_(name), allocator_(allocator) {}
102 const std::string& content_name, 70
103 PortAllocator* allocator) 71 Transport::~Transport() {
104 : signaling_thread_(signaling_thread), 72 ASSERT(channels_destroyed_);
105 worker_thread_(worker_thread),
106 content_name_(content_name),
107 allocator_(allocator),
108 destroyed_(false),
109 readable_(TRANSPORT_STATE_NONE),
110 writable_(TRANSPORT_STATE_NONE),
111 receiving_(TRANSPORT_STATE_NONE),
112 was_writable_(false),
113 connect_requested_(false),
114 ice_role_(ICEROLE_UNKNOWN),
115 tiebreaker_(0),
116 remote_ice_mode_(ICEMODE_FULL),
117 channel_receiving_timeout_(-1) {
118 } 73 }
119 74
120 Transport::~Transport() { 75 bool Transport::AllChannelsCompleted() const {
121 ASSERT(signaling_thread_->IsCurrent()); 76 // We aren't completed until at least one channel is complete, so if there
122 ASSERT(destroyed_); 77 // are no channels, we aren't complete yet.
78 if (channels_.empty()) {
79 LOG(LS_INFO) << name() << " transport is not complete"
80 << " because it has no TransportChannels";
81 return false;
82 }
83
84 // A Transport's ICE process is completed if all of its channels are writable,
85 // have finished allocating candidates, and have pruned all but one of their
86 // connections.
87 for (const auto& iter : channels_) {
88 const TransportChannelImpl* channel = iter.second.get();
89 bool complete =
90 channel->writable() &&
91 channel->GetState() == TransportChannelState::STATE_COMPLETED &&
92 channel->GetIceRole() == ICEROLE_CONTROLLING &&
93 channel->gathering_state() == kIceGatheringComplete;
94 if (!complete) {
95 LOG(LS_INFO) << name() << " transport is not complete"
96 << " because a channel is still incomplete.";
97 return false;
98 }
99 }
100
101 return true;
102 }
103
104 bool Transport::AnyChannelFailed() const {
105 for (const auto& iter : channels_) {
106 if (iter.second->GetState() == TransportChannelState::STATE_FAILED) {
107 return true;
108 }
109 }
110 return false;
123 } 111 }
124 112
125 void Transport::SetIceRole(IceRole role) { 113 void Transport::SetIceRole(IceRole role) {
126 worker_thread_->Invoke<void>(Bind(&Transport::SetIceRole_w, this, role)); 114 ice_role_ = role;
127 } 115 for (auto& iter : channels_) {
128 116 iter.second->SetIceRole(ice_role_);
129 void Transport::SetCertificate( 117 }
130 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
131 worker_thread_->Invoke<void>(Bind(&Transport::SetCertificate_w, this,
132 certificate));
133 }
134
135 bool Transport::GetCertificate(
136 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
137 // The identity is set on the worker thread, so for safety it must also be
138 // acquired on the worker thread.
139 return worker_thread_->Invoke<bool>(
140 Bind(&Transport::GetCertificate_w, this, certificate));
141 } 118 }
142 119
143 bool Transport::GetRemoteSSLCertificate(rtc::SSLCertificate** cert) { 120 bool Transport::GetRemoteSSLCertificate(rtc::SSLCertificate** cert) {
144 // Channels can be deleted on the worker thread, so for safety the remote
145 // certificate is acquired on the worker thread.
146 return worker_thread_->Invoke<bool>(
147 Bind(&Transport::GetRemoteSSLCertificate_w, this, cert));
148 }
149
150 bool Transport::GetRemoteSSLCertificate_w(rtc::SSLCertificate** cert) {
151 ASSERT(worker_thread()->IsCurrent());
152 if (channels_.empty()) 121 if (channels_.empty())
153 return false; 122 return false;
154 123
155 ChannelMap::iterator iter = channels_.begin(); 124 ChannelMap::iterator iter = channels_.begin();
156 return iter->second->GetRemoteSSLCertificate(cert); 125 return iter->second->GetRemoteSSLCertificate(cert);
157 } 126 }
158 127
159 void Transport::SetChannelReceivingTimeout(int timeout_ms) { 128 void Transport::SetChannelReceivingTimeout(int timeout_ms) {
160 worker_thread_->Invoke<void>(
161 Bind(&Transport::SetChannelReceivingTimeout_w, this, timeout_ms));
162 }
163
164 void Transport::SetChannelReceivingTimeout_w(int timeout_ms) {
165 ASSERT(worker_thread()->IsCurrent());
166 channel_receiving_timeout_ = timeout_ms; 129 channel_receiving_timeout_ = timeout_ms;
167 for (const auto& kv : channels_) { 130 for (const auto& kv : channels_) {
168 kv.second->SetReceivingTimeout(timeout_ms); 131 kv.second->SetReceivingTimeout(timeout_ms);
169 } 132 }
170 } 133 }
171 134
172 bool Transport::SetLocalTransportDescription( 135 bool Transport::SetLocalTransportDescription(
173 const TransportDescription& description, 136 const TransportDescription& description,
174 ContentAction action, 137 ContentAction action,
175 std::string* error_desc) { 138 std::string* error_desc) {
176 return worker_thread_->Invoke<bool>(Bind( 139 bool ret = true;
177 &Transport::SetLocalTransportDescription_w, this, 140
178 description, action, error_desc)); 141 if (!VerifyIceParams(description)) {
142 return BadTransportDescription("Invalid ice-ufrag or ice-pwd length",
143 error_desc);
144 }
145
146 if (local_description_ &&
147 IceCredentialsChanged(*local_description_, description)) {
148 IceRole new_ice_role =
149 (action == CA_OFFER) ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED;
150
151 // It must be called before ApplyLocalTransportDescription, which may
152 // trigger an ICE restart and depends on the new ICE role.
153 SetIceRole(new_ice_role);
154 }
155
156 local_description_.reset(new TransportDescription(description));
157
158 for (auto& iter : channels_) {
159 ret &= ApplyLocalTransportDescription(iter.second.get(), error_desc);
160 }
161 if (!ret) {
162 return false;
163 }
164
165 // If PRANSWER/ANSWER is set, we should decide transport protocol type.
166 if (action == CA_PRANSWER || action == CA_ANSWER) {
167 ret &= NegotiateTransportDescription(action, error_desc);
168 }
169 if (ret) {
170 local_description_set_ = true;
171 ConnectChannels();
172 }
173
174 return ret;
179 } 175 }
180 176
181 bool Transport::SetRemoteTransportDescription( 177 bool Transport::SetRemoteTransportDescription(
182 const TransportDescription& description, 178 const TransportDescription& description,
183 ContentAction action, 179 ContentAction action,
184 std::string* error_desc) { 180 std::string* error_desc) {
185 return worker_thread_->Invoke<bool>(Bind( 181 bool ret = true;
186 &Transport::SetRemoteTransportDescription_w, this, 182
187 description, action, error_desc)); 183 if (!VerifyIceParams(description)) {
184 return BadTransportDescription("Invalid ice-ufrag or ice-pwd length",
185 error_desc);
186 }
187
188 remote_description_.reset(new TransportDescription(description));
189 for (auto& iter : channels_) {
190 ret &= ApplyRemoteTransportDescription(iter.second.get(), error_desc);
191 }
192
193 // If PRANSWER/ANSWER is set, we should decide transport protocol type.
194 if (action == CA_PRANSWER || action == CA_ANSWER) {
195 ret = NegotiateTransportDescription(CA_OFFER, error_desc);
196 }
197 if (ret) {
198 remote_description_set_ = true;
199 }
200
201 return ret;
188 } 202 }
189 203
190 TransportChannelImpl* Transport::CreateChannel(int component) { 204 TransportChannelImpl* Transport::CreateChannel(int component) {
191 return worker_thread_->Invoke<TransportChannelImpl*>(Bind(
192 &Transport::CreateChannel_w, this, component));
193 }
194
195 TransportChannelImpl* Transport::CreateChannel_w(int component) {
196 ASSERT(worker_thread()->IsCurrent());
197 TransportChannelImpl* impl; 205 TransportChannelImpl* impl;
198 // TODO(tommi): We don't really need to grab the lock until the actual call
199 // to insert() below and presumably hold it throughout initialization of
200 // |impl| after the impl_exists check. Maybe we can factor that out to
201 // a separate function and not grab the lock in this function.
202 // Actually, we probably don't need to hold the lock while initializing
203 // |impl| since we can just do the insert when that's done.
204 rtc::CritScope cs(&crit_);
205 206
206 // Create the entry if it does not exist. 207 // Create the entry if it does not exist.
207 bool impl_exists = false; 208 bool impl_exists = false;
208 auto iterator = channels_.find(component); 209 auto iterator = channels_.find(component);
209 if (iterator == channels_.end()) { 210 if (iterator == channels_.end()) {
210 impl = CreateTransportChannel(component); 211 impl = CreateTransportChannel(component);
211 iterator = channels_.insert(std::pair<int, ChannelMapEntry>( 212 iterator = channels_.insert(std::pair<int, ChannelMapEntry>(
212 component, ChannelMapEntry(impl))).first; 213 component, ChannelMapEntry(impl))).first;
213 } else { 214 } else {
214 impl = iterator->second.get(); 215 impl = iterator->second.get();
215 impl_exists = true; 216 impl_exists = true;
216 } 217 }
217 218
218 // Increase the ref count. 219 // Increase the ref count.
219 iterator->second.AddRef(); 220 iterator->second.AddRef();
220 destroyed_ = false; 221 channels_destroyed_ = false;
221 222
222 if (impl_exists) { 223 if (impl_exists) {
223 // If this is an existing channel, we should just return it without 224 // If this is an existing channel, we should just return it without
224 // connecting to all the signal again. 225 // connecting to all the signal again.
225 return impl; 226 return impl;
226 } 227 }
227 228
228 // Push down our transport state to the new channel. 229 // Push down our transport state to the new channel.
229 impl->SetIceRole(ice_role_); 230 impl->SetIceRole(ice_role_);
230 impl->SetIceTiebreaker(tiebreaker_); 231 impl->SetIceTiebreaker(tiebreaker_);
231 impl->SetReceivingTimeout(channel_receiving_timeout_); 232 impl->SetReceivingTimeout(channel_receiving_timeout_);
232 // TODO(ronghuawu): Change CreateChannel_w to be able to return error since 233 // TODO(ronghuawu): Change CreateChannel to be able to return error since
233 // below Apply**Description_w calls can fail. 234 // below Apply**Description calls can fail.
234 if (local_description_) 235 if (local_description_)
235 ApplyLocalTransportDescription_w(impl, NULL); 236 ApplyLocalTransportDescription(impl, NULL);
236 if (remote_description_) 237 if (remote_description_)
237 ApplyRemoteTransportDescription_w(impl, NULL); 238 ApplyRemoteTransportDescription(impl, NULL);
238 if (local_description_ && remote_description_) 239 if (local_description_ && remote_description_)
239 ApplyNegotiatedTransportDescription_w(impl, NULL); 240 ApplyNegotiatedTransportDescription(impl, NULL);
240 241
241 impl->SignalReadableState.connect(this, &Transport::OnChannelReadableState); 242 impl->SignalReadableState.connect(this, &Transport::OnChannelReadableState);
242 impl->SignalWritableState.connect(this, &Transport::OnChannelWritableState); 243 impl->SignalWritableState.connect(this, &Transport::OnChannelWritableState);
243 impl->SignalReceivingState.connect(this, &Transport::OnChannelReceivingState); 244 impl->SignalReceivingState.connect(this, &Transport::OnChannelReceivingState);
244 impl->SignalRequestSignaling.connect( 245 impl->SignalGatheringState.connect(this, &Transport::OnChannelGatheringState);
245 this, &Transport::OnChannelRequestSignaling); 246 impl->SignalCandidateGathered.connect(this,
246 impl->SignalCandidateReady.connect(this, &Transport::OnChannelCandidateReady); 247 &Transport::OnChannelCandidateGathered);
247 impl->SignalRouteChange.connect(this, &Transport::OnChannelRouteChange); 248 impl->SignalRouteChange.connect(this, &Transport::OnChannelRouteChange);
248 impl->SignalCandidatesAllocationDone.connect(
249 this, &Transport::OnChannelCandidatesAllocationDone);
250 impl->SignalRoleConflict.connect(this, &Transport::OnRoleConflict); 249 impl->SignalRoleConflict.connect(this, &Transport::OnRoleConflict);
251 impl->SignalConnectionRemoved.connect( 250 impl->SignalConnectionRemoved.connect(
252 this, &Transport::OnChannelConnectionRemoved); 251 this, &Transport::OnChannelConnectionRemoved);
253 252
254 if (connect_requested_) { 253 if (connect_requested_) {
255 impl->Connect(); 254 impl->Connect();
256 if (channels_.size() == 1) { 255 if (channels_.size() == 1) {
257 // If this is the first channel, then indicate that we have started 256 // If this is the first channel, then indicate that we have started
258 // connecting. 257 // connecting.
259 signaling_thread()->Post(this, MSG_CONNECTING, NULL); 258 SignalConnecting(this);
260 } 259 }
261 } 260 }
262 return impl; 261 return impl;
263 } 262 }
264 263
265 TransportChannelImpl* Transport::GetChannel(int component) { 264 TransportChannelImpl* Transport::GetChannel(int component) {
266 // TODO(tommi,pthatcher): Since we're returning a pointer from the channels_
267 // map, shouldn't we assume that we're on the worker thread? (The pointer
268 // will be used outside of the lock).
269 // And if we're on the worker thread, which is the only thread that modifies
270 // channels_, can we skip grabbing the lock?
271 rtc::CritScope cs(&crit_);
272 ChannelMap::iterator iter = channels_.find(component); 265 ChannelMap::iterator iter = channels_.find(component);
273 return (iter != channels_.end()) ? iter->second.get() : NULL; 266 return (iter != channels_.end()) ? iter->second.get() : NULL;
274 } 267 }
275 268
276 bool Transport::HasChannels() { 269 bool Transport::HasChannels() {
277 rtc::CritScope cs(&crit_);
278 return !channels_.empty(); 270 return !channels_.empty();
279 } 271 }
280 272
281 void Transport::DestroyChannel(int component) { 273 void Transport::DestroyChannel(int component) {
282 worker_thread_->Invoke<void>(Bind(
283 &Transport::DestroyChannel_w, this, component));
284 }
285
286 void Transport::DestroyChannel_w(int component) {
287 ASSERT(worker_thread()->IsCurrent());
288
289 ChannelMap::iterator iter = channels_.find(component); 274 ChannelMap::iterator iter = channels_.find(component);
290 if (iter == channels_.end()) 275 if (iter == channels_.end())
291 return; 276 return;
292 277
293 TransportChannelImpl* impl = NULL; 278 TransportChannelImpl* impl = NULL;
294 279
295 iter->second.DecRef(); 280 iter->second.DecRef();
296 if (!iter->second.ref()) { 281 if (!iter->second.ref()) {
297 impl = iter->second.get(); 282 impl = iter->second.get();
298 rtc::CritScope cs(&crit_);
299 channels_.erase(iter); 283 channels_.erase(iter);
300 } 284 }
301 285
302 if (connect_requested_ && channels_.empty()) { 286 if (connect_requested_ && channels_.empty()) {
303 // We're no longer attempting to connect. 287 // We're no longer attempting to connect.
304 signaling_thread()->Post(this, MSG_CONNECTING, NULL); 288 SignalConnecting(this);
305 } 289 }
306 290
307 if (impl) { 291 if (impl) {
308 // Check in case the deleted channel was the only non-writable channel.
309 OnChannelWritableState(impl);
310 DestroyTransportChannel(impl); 292 DestroyTransportChannel(impl);
293 // Need to update aggregate state after destroying a channel,
294 // for example if it was the only one that wasn't yet writable.
295 UpdateReadableState();
296 UpdateWritableState();
297 UpdateReceivingState();
298 UpdateGatheringState();
299 MaybeSignalCompleted();
311 } 300 }
312 } 301 }
313 302
314 void Transport::ConnectChannels() { 303 void Transport::ConnectChannels() {
315 ASSERT(signaling_thread()->IsCurrent());
316 worker_thread_->Invoke<void>(Bind(&Transport::ConnectChannels_w, this));
317 }
318
319 void Transport::ConnectChannels_w() {
320 ASSERT(worker_thread()->IsCurrent());
321 if (connect_requested_ || channels_.empty()) 304 if (connect_requested_ || channels_.empty())
322 return; 305 return;
323 306
324 connect_requested_ = true; 307 connect_requested_ = true;
325 signaling_thread()->Post(this, MSG_CANDIDATEREADY, NULL);
326 308
327 if (!local_description_) { 309 if (!local_description_) {
328 // TOOD(mallinath) : TransportDescription(TD) shouldn't be generated here. 310 // TOOD(mallinath) : TransportDescription(TD) shouldn't be generated here.
329 // As Transport must know TD is offer or answer and cricket::Transport 311 // As Transport must know TD is offer or answer and cricket::Transport
330 // doesn't have the capability to decide it. This should be set by the 312 // doesn't have the capability to decide it. This should be set by the
331 // Session. 313 // Session.
332 // Session must generate local TD before remote candidates pushed when 314 // Session must generate local TD before remote candidates pushed when
333 // initiate request initiated by the remote. 315 // initiate request initiated by the remote.
334 LOG(LS_INFO) << "Transport::ConnectChannels_w: No local description has " 316 LOG(LS_INFO) << "Transport::ConnectChannels: No local description has "
335 << "been set. Will generate one."; 317 << "been set. Will generate one.";
336 TransportDescription desc(std::vector<std::string>(), 318 TransportDescription desc(
337 rtc::CreateRandomString(ICE_UFRAG_LENGTH), 319 std::vector<std::string>(), rtc::CreateRandomString(ICE_UFRAG_LENGTH),
338 rtc::CreateRandomString(ICE_PWD_LENGTH), 320 rtc::CreateRandomString(ICE_PWD_LENGTH), ICEMODE_FULL,
339 ICEMODE_FULL, CONNECTIONROLE_NONE, NULL, 321 CONNECTIONROLE_NONE, NULL, Candidates());
340 Candidates()); 322 SetLocalTransportDescription(desc, CA_OFFER, NULL);
341 SetLocalTransportDescription_w(desc, CA_OFFER, NULL);
342 } 323 }
343 324
344 CallChannels_w(&TransportChannelImpl::Connect); 325 CallChannels(&TransportChannelImpl::Connect);
345 if (!channels_.empty()) { 326 if (HasChannels()) {
346 signaling_thread()->Post(this, MSG_CONNECTING, NULL); 327 SignalConnecting(this);
347 } 328 }
348 } 329 }
349 330
350 void Transport::OnConnecting_s() { 331 void Transport::MaybeStartGathering() {
351 ASSERT(signaling_thread()->IsCurrent()); 332 if (connect_requested_) {
352 SignalConnecting(this); 333 CallChannels(&TransportChannelImpl::MaybeStartGathering);
334 }
353 } 335 }
354 336
355 void Transport::DestroyAllChannels() { 337 void Transport::DestroyAllChannels() {
356 ASSERT(signaling_thread()->IsCurrent());
357 worker_thread_->Invoke<void>(Bind(&Transport::DestroyAllChannels_w, this));
358 worker_thread()->Clear(this);
359 signaling_thread()->Clear(this);
360 destroyed_ = true;
361 }
362
363 void Transport::DestroyAllChannels_w() {
364 ASSERT(worker_thread()->IsCurrent());
365
366 std::vector<TransportChannelImpl*> impls; 338 std::vector<TransportChannelImpl*> impls;
367 for (auto& iter : channels_) { 339 for (auto& iter : channels_) {
368 iter.second.DecRef(); 340 iter.second.DecRef();
369 if (!iter.second.ref()) 341 if (!iter.second.ref())
370 impls.push_back(iter.second.get()); 342 impls.push_back(iter.second.get());
371 } 343 }
372 344
373 { 345 channels_.clear();
374 rtc::CritScope cs(&crit_); 346
375 channels_.clear(); 347 for (TransportChannelImpl* impl : impls) {
348 DestroyTransportChannel(impl);
376 } 349 }
377 350 channels_destroyed_ = true;
378 for (size_t i = 0; i < impls.size(); ++i)
379 DestroyTransportChannel(impls[i]);
380 } 351 }
381 352
382 void Transport::OnSignalingReady() { 353 void Transport::CallChannels(TransportChannelFunc func) {
383 ASSERT(signaling_thread()->IsCurrent());
384 if (destroyed_) return;
385
386 worker_thread()->Post(this, MSG_ONSIGNALINGREADY, NULL);
387
388 // Notify the subclass.
389 OnTransportSignalingReady();
390 }
391
392 void Transport::CallChannels_w(TransportChannelFunc func) {
393 ASSERT(worker_thread()->IsCurrent());
394 for (const auto& iter : channels_) { 354 for (const auto& iter : channels_) {
395 ((iter.second.get())->*func)(); 355 ((iter.second.get())->*func)();
396 } 356 }
397 } 357 }
398 358
399 bool Transport::VerifyCandidate(const Candidate& cand, std::string* error) { 359 bool Transport::VerifyCandidate(const Candidate& cand, std::string* error) {
400 // No address zero. 360 // No address zero.
401 if (cand.address().IsNil() || cand.address().IsAny()) { 361 if (cand.address().IsNil() || cand.address().IsAny()) {
402 *error = "candidate has address of zero"; 362 *error = "candidate has address of zero";
403 return false; 363 return false;
(...skipping 18 matching lines...) Expand all
422 *error = "candidate has port of 80 or 443 with private IP address"; 382 *error = "candidate has port of 80 or 443 with private IP address";
423 return false; 383 return false;
424 } 384 }
425 } 385 }
426 386
427 return true; 387 return true;
428 } 388 }
429 389
430 390
431 bool Transport::GetStats(TransportStats* stats) { 391 bool Transport::GetStats(TransportStats* stats) {
432 ASSERT(signaling_thread()->IsCurrent()); 392 stats->transport_name = name();
433 return worker_thread_->Invoke<bool>(Bind(
434 &Transport::GetStats_w, this, stats));
435 }
436
437 bool Transport::GetStats_w(TransportStats* stats) {
438 ASSERT(worker_thread()->IsCurrent());
439 stats->content_name = content_name();
440 stats->channel_stats.clear(); 393 stats->channel_stats.clear();
441 for (auto iter : channels_) { 394 for (auto iter : channels_) {
442 ChannelMapEntry& entry = iter.second; 395 ChannelMapEntry& entry = iter.second;
443 TransportChannelStats substats; 396 TransportChannelStats substats;
444 substats.component = entry->component(); 397 substats.component = entry->component();
445 entry->GetSrtpCipher(&substats.srtp_cipher); 398 entry->GetSrtpCipher(&substats.srtp_cipher);
446 entry->GetSslCipher(&substats.ssl_cipher); 399 entry->GetSslCipher(&substats.ssl_cipher);
447 if (!entry->GetStats(&substats.connection_infos)) { 400 if (!entry->GetStats(&substats.connection_infos)) {
448 return false; 401 return false;
449 } 402 }
450 stats->channel_stats.push_back(substats); 403 stats->channel_stats.push_back(substats);
451 } 404 }
452 return true; 405 return true;
453 } 406 }
454 407
455 bool Transport::GetSslRole(rtc::SSLRole* ssl_role) const { 408 bool Transport::AddRemoteCandidates(const std::vector<Candidate>& candidates,
456 return worker_thread_->Invoke<bool>(Bind( 409 std::string* error) {
457 &Transport::GetSslRole_w, this, ssl_role)); 410 ASSERT(!channels_destroyed_);
458 } 411 // Verify each candidate before passing down to transport layer.
412 for (const Candidate& cand : candidates) {
413 if (!VerifyCandidate(cand, error)) {
414 return false;
415 }
416 if (!HasChannel(cand.component())) {
417 *error = "Candidate has unknown component: " + cand.ToString() +
418 " for content: " + name();
419 return false;
420 }
421 }
459 422
460 bool Transport::SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) {
461 return worker_thread_->Invoke<bool>(Bind(
462 &Transport::SetSslMaxProtocolVersion_w, this, version));
463 }
464
465 void Transport::OnRemoteCandidates(const std::vector<Candidate>& candidates) {
466 for (std::vector<Candidate>::const_iterator iter = candidates.begin(); 423 for (std::vector<Candidate>::const_iterator iter = candidates.begin();
467 iter != candidates.end(); 424 iter != candidates.end();
468 ++iter) { 425 ++iter) {
469 OnRemoteCandidate(*iter); 426 TransportChannelImpl* channel = GetChannel(iter->component());
427 if (channel != NULL) {
428 channel->AddRemoteCandidate(*iter);
429 }
470 } 430 }
471 } 431 return true;
472
473 void Transport::OnRemoteCandidate(const Candidate& candidate) {
474 ASSERT(signaling_thread()->IsCurrent());
475 if (destroyed_) return;
476
477 if (!HasChannel(candidate.component())) {
478 LOG(LS_WARNING) << "Ignoring candidate for unknown component "
479 << candidate.component();
480 return;
481 }
482
483 ChannelParams* params = new ChannelParams(new Candidate(candidate));
484 worker_thread()->Post(this, MSG_ONREMOTECANDIDATE, params);
485 }
486
487 void Transport::OnRemoteCandidate_w(const Candidate& candidate) {
488 ASSERT(worker_thread()->IsCurrent());
489 ChannelMap::iterator iter = channels_.find(candidate.component());
490 // It's ok for a channel to go away while this message is in transit.
491 if (iter != channels_.end()) {
492 iter->second->OnCandidate(candidate);
493 }
494 } 432 }
495 433
496 void Transport::OnChannelReadableState(TransportChannel* channel) { 434 void Transport::OnChannelReadableState(TransportChannel* channel) {
497 ASSERT(worker_thread()->IsCurrent()); 435 UpdateReadableState();
498 signaling_thread()->Post(this, MSG_READSTATE, NULL);
499 }
500
501 void Transport::OnChannelReadableState_s() {
502 ASSERT(signaling_thread()->IsCurrent());
503 TransportState readable = GetTransportState_s(TRANSPORT_READABLE_STATE);
504 if (readable_ != readable) {
505 readable_ = readable;
506 SignalReadableState(this);
507 }
508 } 436 }
509 437
510 void Transport::OnChannelWritableState(TransportChannel* channel) { 438 void Transport::OnChannelWritableState(TransportChannel* channel) {
511 ASSERT(worker_thread()->IsCurrent()); 439 LOG(LS_INFO) << name() << " TransportChannel " << channel->component()
512 signaling_thread()->Post(this, MSG_WRITESTATE, NULL); 440 << " writability changed to " << channel->writable()
513 441 << ". Check if transport is complete.";
514 MaybeCompleted_w(); 442 UpdateWritableState();
515 } 443 MaybeSignalCompleted();
516
517 void Transport::OnChannelWritableState_s() {
518 ASSERT(signaling_thread()->IsCurrent());
519 TransportState writable = GetTransportState_s(TRANSPORT_WRITABLE_STATE);
520 if (writable_ != writable) {
521 was_writable_ = (writable_ == TRANSPORT_STATE_ALL);
522 writable_ = writable;
523 SignalWritableState(this);
524 }
525 } 444 }
526 445
527 void Transport::OnChannelReceivingState(TransportChannel* channel) { 446 void Transport::OnChannelReceivingState(TransportChannel* channel) {
528 ASSERT(worker_thread()->IsCurrent()); 447 UpdateReceivingState();
529 signaling_thread()->Post(this, MSG_RECEIVINGSTATE);
530 } 448 }
531 449
532 void Transport::OnChannelReceivingState_s() { 450 TransportState Transport::GetTransportState(TransportStateType state_type) {
533 ASSERT(signaling_thread()->IsCurrent());
534 TransportState receiving = GetTransportState_s(TRANSPORT_RECEIVING_STATE);
535 if (receiving_ != receiving) {
536 receiving_ = receiving;
537 SignalReceivingState(this);
538 }
539 }
540
541 TransportState Transport::GetTransportState_s(TransportStateType state_type) {
542 ASSERT(signaling_thread()->IsCurrent());
543
544 rtc::CritScope cs(&crit_);
545 bool any = false; 451 bool any = false;
546 bool all = !channels_.empty(); 452 bool all = !channels_.empty();
547 for (const auto iter : channels_) { 453 for (const auto iter : channels_) {
548 bool b = false; 454 bool b = false;
549 switch (state_type) { 455 switch (state_type) {
550 case TRANSPORT_READABLE_STATE: 456 case TRANSPORT_READABLE_STATE:
551 b = iter.second->readable(); 457 b = iter.second->readable();
552 break; 458 break;
553 case TRANSPORT_WRITABLE_STATE: 459 case TRANSPORT_WRITABLE_STATE:
554 b = iter.second->writable(); 460 b = iter.second->writable();
(...skipping 10 matching lines...) Expand all
565 471
566 if (all) { 472 if (all) {
567 return TRANSPORT_STATE_ALL; 473 return TRANSPORT_STATE_ALL;
568 } else if (any) { 474 } else if (any) {
569 return TRANSPORT_STATE_SOME; 475 return TRANSPORT_STATE_SOME;
570 } 476 }
571 477
572 return TRANSPORT_STATE_NONE; 478 return TRANSPORT_STATE_NONE;
573 } 479 }
574 480
575 void Transport::OnChannelRequestSignaling(TransportChannelImpl* channel) { 481 void Transport::OnChannelGatheringState(TransportChannelImpl* channel) {
576 ASSERT(worker_thread()->IsCurrent()); 482 ASSERT(channels_.find(channel->component()) != channels_.end());
577 // Resetting ICE state for the channel. 483 UpdateGatheringState();
578 ChannelMap::iterator iter = channels_.find(channel->component()); 484 if (gathering_state_ == kIceGatheringComplete) {
579 if (iter != channels_.end()) 485 // If UpdateGatheringState brought us to kIceGatheringComplete, check if
580 iter->second.set_candidates_allocated(false); 486 // our connection state is also "Completed". Otherwise, there's no point in
581 signaling_thread()->Post(this, MSG_REQUESTSIGNALING, nullptr); 487 // checking (since it would only produce log messages).
488 MaybeSignalCompleted();
489 }
582 } 490 }
583 491
584 void Transport::OnChannelRequestSignaling_s() { 492 void Transport::OnChannelCandidateGathered(TransportChannelImpl* channel,
585 ASSERT(signaling_thread()->IsCurrent()); 493 const Candidate& candidate) {
586 LOG(LS_INFO) << "Transport: " << content_name_ << ", allocating candidates";
587 SignalRequestSignaling(this);
588 }
589
590 void Transport::OnChannelCandidateReady(TransportChannelImpl* channel,
591 const Candidate& candidate) {
592 // We should never signal peer-reflexive candidates. 494 // We should never signal peer-reflexive candidates.
593 if (candidate.type() == PRFLX_PORT_TYPE) { 495 if (candidate.type() == PRFLX_PORT_TYPE) {
594 ASSERT(false); 496 ASSERT(false);
595 return; 497 return;
596 } 498 }
597 499
598 ASSERT(worker_thread()->IsCurrent());
599 rtc::CritScope cs(&crit_);
600 ready_candidates_.push_back(candidate);
601
602 // We hold any messages until the client lets us connect.
603 if (connect_requested_) {
604 signaling_thread()->Post(
605 this, MSG_CANDIDATEREADY, NULL);
606 }
607 }
608
609 void Transport::OnChannelCandidateReady_s() {
610 ASSERT(signaling_thread()->IsCurrent());
611 ASSERT(connect_requested_); 500 ASSERT(connect_requested_);
612
613 std::vector<Candidate> candidates; 501 std::vector<Candidate> candidates;
614 { 502 candidates.push_back(candidate);
615 rtc::CritScope cs(&crit_); 503 SignalCandidatesGathered(this, candidates);
616 candidates.swap(ready_candidates_);
617 }
618
619 // we do the deleting of Candidate* here to keep the new above and
620 // delete below close to each other
621 if (!candidates.empty()) {
622 SignalCandidatesReady(this, candidates);
623 }
624 } 504 }
625 505
626 void Transport::OnChannelRouteChange(TransportChannel* channel, 506 void Transport::OnChannelRouteChange(TransportChannel* channel,
627 const Candidate& remote_candidate) { 507 const Candidate& remote_candidate) {
628 ASSERT(worker_thread()->IsCurrent());
629 ChannelParams* params = new ChannelParams(new Candidate(remote_candidate));
630 params->channel = static_cast<cricket::TransportChannelImpl*>(channel);
631 signaling_thread()->Post(this, MSG_ROUTECHANGE, params);
632 }
633
634 void Transport::OnChannelRouteChange_s(const TransportChannel* channel,
635 const Candidate& remote_candidate) {
636 ASSERT(signaling_thread()->IsCurrent());
637 SignalRouteChange(this, remote_candidate.component(), remote_candidate); 508 SignalRouteChange(this, remote_candidate.component(), remote_candidate);
638 } 509 }
639 510
640 void Transport::OnChannelCandidatesAllocationDone(
641 TransportChannelImpl* channel) {
642 ASSERT(worker_thread()->IsCurrent());
643 ChannelMap::iterator iter = channels_.find(channel->component());
644 ASSERT(iter != channels_.end());
645 LOG(LS_INFO) << "Transport: " << content_name_ << ", component "
646 << channel->component() << " allocation complete";
647
648 iter->second.set_candidates_allocated(true);
649
650 // If all channels belonging to this Transport got signal, then
651 // forward this signal to upper layer.
652 // Can this signal arrive before all transport channels are created?
653 for (auto& iter : channels_) {
654 if (!iter.second.candidates_allocated())
655 return;
656 }
657 signaling_thread_->Post(this, MSG_CANDIDATEALLOCATIONCOMPLETE);
658
659 MaybeCompleted_w();
660 }
661
662 void Transport::OnChannelCandidatesAllocationDone_s() {
663 ASSERT(signaling_thread()->IsCurrent());
664 LOG(LS_INFO) << "Transport: " << content_name_ << " allocation complete";
665 SignalCandidatesAllocationDone(this);
666 }
667
668 void Transport::OnRoleConflict(TransportChannelImpl* channel) { 511 void Transport::OnRoleConflict(TransportChannelImpl* channel) {
669 signaling_thread_->Post(this, MSG_ROLECONFLICT); 512 SignalRoleConflict();
670 } 513 }
671 514
672 void Transport::OnChannelConnectionRemoved(TransportChannelImpl* channel) { 515 void Transport::OnChannelConnectionRemoved(TransportChannelImpl* channel) {
673 ASSERT(worker_thread()->IsCurrent()); 516 LOG(LS_INFO) << name() << " TransportChannel " << channel->component()
674 MaybeCompleted_w(); 517 << " connection removed. Check if transport is complete.";
518 MaybeSignalCompleted();
675 519
676 // Check if the state is now Failed. 520 // Check if the state is now Failed.
677 // Failed is only available in the Controlling ICE role. 521 // Failed is only available in the Controlling ICE role.
678 if (channel->GetIceRole() != ICEROLE_CONTROLLING) { 522 if (channel->GetIceRole() != ICEROLE_CONTROLLING) {
679 return; 523 return;
680 } 524 }
681 525
682 ChannelMap::iterator iter = channels_.find(channel->component()); 526 // Failed can only occur after candidate gathering has stopped.
683 ASSERT(iter != channels_.end()); 527 if (channel->gathering_state() != kIceGatheringComplete) {
684 // Failed can only occur after candidate allocation has stopped.
685 if (!iter->second.candidates_allocated()) {
686 return; 528 return;
687 } 529 }
688 530
689 if (channel->GetState() == TransportChannelState::STATE_FAILED) { 531 if (channel->GetState() == TransportChannelState::STATE_FAILED) {
690 // A Transport has failed if any of its channels have no remaining 532 // A Transport has failed if any of its channels have no remaining
691 // connections. 533 // connections.
692 signaling_thread_->Post(this, MSG_FAILED); 534 SignalFailed(this);
693 } 535 }
694 } 536 }
695 537
696 void Transport::MaybeCompleted_w() { 538 void Transport::MaybeSignalCompleted() {
697 ASSERT(worker_thread()->IsCurrent()); 539 if (AllChannelsCompleted()) {
540 LOG(LS_INFO) << name() << " transport is complete"
541 << " because all the channels are complete.";
542 SignalCompleted(this);
543 }
544 // TODO(deadbeef): Should we do anything if we previously were completed,
545 // but now are not (if, for example, a new remote candidate is added)?
546 }
698 547
699 // When there is no channel created yet, calling this function could fire an 548 void Transport::UpdateGatheringState() {
700 // IceConnectionCompleted event prematurely. 549 IceGatheringState new_state = kIceGatheringNew;
701 if (channels_.empty()) { 550 bool any_gathering = false;
702 return; 551 bool all_complete = !channels_.empty();
552 for (const auto& kv : channels_) {
553 any_gathering =
554 any_gathering || kv.second->gathering_state() != kIceGatheringNew;
555 all_complete =
556 all_complete && kv.second->gathering_state() == kIceGatheringComplete;
557 }
558 if (all_complete) {
559 new_state = kIceGatheringComplete;
560 } else if (any_gathering) {
561 new_state = kIceGatheringGathering;
703 } 562 }
704 563
705 // A Transport's ICE process is completed if all of its channels are writable, 564 if (gathering_state_ != new_state) {
706 // have finished allocating candidates, and have pruned all but one of their 565 gathering_state_ = new_state;
707 // connections. 566 if (gathering_state_ == kIceGatheringGathering) {
708 for (const auto& iter : channels_) { 567 LOG(LS_INFO) << "Transport: " << name_ << ", gathering candidates";
709 const TransportChannelImpl* channel = iter.second.get(); 568 } else if (gathering_state_ == kIceGatheringComplete) {
710 if (!(channel->writable() && 569 LOG(LS_INFO) << "Transport " << name() << " gathering complete.";
711 channel->GetState() == TransportChannelState::STATE_COMPLETED &&
712 channel->GetIceRole() == ICEROLE_CONTROLLING &&
713 iter.second.candidates_allocated())) {
714 return;
715 } 570 }
716 } 571 SignalGatheringState(this);
717
718 signaling_thread_->Post(this, MSG_COMPLETED);
719 }
720
721 void Transport::SetIceRole_w(IceRole role) {
722 ASSERT(worker_thread()->IsCurrent());
723 rtc::CritScope cs(&crit_);
724 ice_role_ = role;
725 for (auto& iter : channels_) {
726 iter.second->SetIceRole(ice_role_);
727 } 572 }
728 } 573 }
729 574
730 void Transport::SetRemoteIceMode_w(IceMode mode) { 575 void Transport::UpdateReceivingState() {
731 ASSERT(worker_thread()->IsCurrent()); 576 TransportState receiving = GetTransportState(TRANSPORT_RECEIVING_STATE);
732 remote_ice_mode_ = mode; 577 if (receiving_ != receiving) {
733 // Shouldn't channels be created after this method executed? 578 receiving_ = receiving;
734 for (auto& iter : channels_) { 579 SignalReceivingState(this);
735 iter.second->SetRemoteIceMode(remote_ice_mode_);
736 } 580 }
737 } 581 }
738 582
739 bool Transport::SetLocalTransportDescription_w( 583 void Transport::UpdateWritableState() {
740 const TransportDescription& desc, 584 TransportState writable = GetTransportState(TRANSPORT_WRITABLE_STATE);
741 ContentAction action, 585 LOG(LS_INFO) << name() << " transport writable state changed? " << writable_
742 std::string* error_desc) { 586 << " => " << writable;
743 ASSERT(worker_thread()->IsCurrent()); 587 if (writable_ != writable) {
744 bool ret = true; 588 was_writable_ = (writable_ == TRANSPORT_STATE_ALL);
745 589 writable_ = writable;
746 if (!VerifyIceParams(desc)) { 590 SignalWritableState(this);
747 return BadTransportDescription("Invalid ice-ufrag or ice-pwd length",
748 error_desc);
749 } 591 }
750
751 // TODO(tommi,pthatcher): I'm not sure why we need to grab this lock at this
752 // point. |local_description_| seems to always be modified on the worker
753 // thread, so we should be able to use it here without grabbing the lock.
754 // However, we _might_ need it before the call to reset() below?
755 // Raw access to |local_description_| is granted to derived transports outside
756 // of locking (see local_description() in the header file).
757 // The contract is that the derived implementations must be aware of when the
758 // description might change and do appropriate synchronization.
759 rtc::CritScope cs(&crit_);
760 if (local_description_ && IceCredentialsChanged(*local_description_, desc)) {
761 IceRole new_ice_role = (action == CA_OFFER) ? ICEROLE_CONTROLLING
762 : ICEROLE_CONTROLLED;
763
764 // It must be called before ApplyLocalTransportDescription_w, which may
765 // trigger an ICE restart and depends on the new ICE role.
766 SetIceRole_w(new_ice_role);
767 }
768
769 local_description_.reset(new TransportDescription(desc));
770
771 for (auto& iter : channels_) {
772 ret &= ApplyLocalTransportDescription_w(iter.second.get(), error_desc);
773 }
774 if (!ret)
775 return false;
776
777 // If PRANSWER/ANSWER is set, we should decide transport protocol type.
778 if (action == CA_PRANSWER || action == CA_ANSWER) {
779 ret &= NegotiateTransportDescription_w(action, error_desc);
780 }
781 return ret;
782 } 592 }
783 593
784 bool Transport::SetRemoteTransportDescription_w( 594 void Transport::UpdateReadableState() {
785 const TransportDescription& desc, 595 TransportState readable = GetTransportState(TRANSPORT_READABLE_STATE);
786 ContentAction action, 596 if (readable_ != readable) {
787 std::string* error_desc) { 597 readable_ = readable;
788 bool ret = true; 598 SignalReadableState(this);
789
790 if (!VerifyIceParams(desc)) {
791 return BadTransportDescription("Invalid ice-ufrag or ice-pwd length",
792 error_desc);
793 } 599 }
794
795 // TODO(tommi,pthatcher): See todo for local_description_ above.
796 rtc::CritScope cs(&crit_);
797 remote_description_.reset(new TransportDescription(desc));
798 for (auto& iter : channels_) {
799 ret &= ApplyRemoteTransportDescription_w(iter.second.get(), error_desc);
800 }
801
802 // If PRANSWER/ANSWER is set, we should decide transport protocol type.
803 if (action == CA_PRANSWER || action == CA_ANSWER) {
804 ret = NegotiateTransportDescription_w(CA_OFFER, error_desc);
805 }
806 return ret;
807 } 600 }
808 601
809 bool Transport::ApplyLocalTransportDescription_w(TransportChannelImpl* ch, 602 bool Transport::ApplyLocalTransportDescription(TransportChannelImpl* ch,
810 std::string* error_desc) { 603 std::string* error_desc) {
811 ASSERT(worker_thread()->IsCurrent());
812 ch->SetIceCredentials(local_description_->ice_ufrag, 604 ch->SetIceCredentials(local_description_->ice_ufrag,
813 local_description_->ice_pwd); 605 local_description_->ice_pwd);
814 return true; 606 return true;
815 } 607 }
816 608
817 bool Transport::ApplyRemoteTransportDescription_w(TransportChannelImpl* ch, 609 bool Transport::ApplyRemoteTransportDescription(TransportChannelImpl* ch,
818 std::string* error_desc) { 610 std::string* error_desc) {
819 ch->SetRemoteIceCredentials(remote_description_->ice_ufrag, 611 ch->SetRemoteIceCredentials(remote_description_->ice_ufrag,
820 remote_description_->ice_pwd); 612 remote_description_->ice_pwd);
821 return true; 613 return true;
822 } 614 }
823 615
824 bool Transport::ApplyNegotiatedTransportDescription_w( 616 bool Transport::ApplyNegotiatedTransportDescription(
825 TransportChannelImpl* channel, std::string* error_desc) { 617 TransportChannelImpl* channel,
826 ASSERT(worker_thread()->IsCurrent()); 618 std::string* error_desc) {
827 channel->SetRemoteIceMode(remote_ice_mode_); 619 channel->SetRemoteIceMode(remote_ice_mode_);
828 return true; 620 return true;
829 } 621 }
830 622
831 bool Transport::NegotiateTransportDescription_w(ContentAction local_role, 623 bool Transport::NegotiateTransportDescription(ContentAction local_role,
832 std::string* error_desc) { 624 std::string* error_desc) {
833 ASSERT(worker_thread()->IsCurrent());
834 // TODO(ekr@rtfm.com): This is ICE-specific stuff. Refactor into 625 // TODO(ekr@rtfm.com): This is ICE-specific stuff. Refactor into
835 // P2PTransport. 626 // P2PTransport.
836 627
837 // If transport is in ICEROLE_CONTROLLED and remote end point supports only 628 // If transport is in ICEROLE_CONTROLLED and remote end point supports only
838 // ice_lite, this local end point should take CONTROLLING role. 629 // ice_lite, this local end point should take CONTROLLING role.
839 if (ice_role_ == ICEROLE_CONTROLLED && 630 if (ice_role_ == ICEROLE_CONTROLLED &&
840 remote_description_->ice_mode == ICEMODE_LITE) { 631 remote_description_->ice_mode == ICEMODE_LITE) {
841 SetIceRole_w(ICEROLE_CONTROLLING); 632 SetIceRole(ICEROLE_CONTROLLING);
842 } 633 }
843 634
844 // Update remote ice_mode to all existing channels. 635 // Update remote ice_mode to all existing channels.
845 remote_ice_mode_ = remote_description_->ice_mode; 636 remote_ice_mode_ = remote_description_->ice_mode;
846 637
847 // Now that we have negotiated everything, push it downward. 638 // Now that we have negotiated everything, push it downward.
848 // Note that we cache the result so that if we have race conditions 639 // Note that we cache the result so that if we have race conditions
849 // between future SetRemote/SetLocal invocations and new channel 640 // between future SetRemote/SetLocal invocations and new channel
850 // creation, we have the negotiation state saved until a new 641 // creation, we have the negotiation state saved until a new
851 // negotiation happens. 642 // negotiation happens.
852 for (auto& iter : channels_) { 643 for (auto& iter : channels_) {
853 if (!ApplyNegotiatedTransportDescription_w(iter.second.get(), error_desc)) 644 if (!ApplyNegotiatedTransportDescription(iter.second.get(), error_desc))
854 return false; 645 return false;
855 } 646 }
856 return true; 647 return true;
857 } 648 }
858 649
859 void Transport::OnMessage(rtc::Message* msg) {
860 switch (msg->message_id) {
861 case MSG_ONSIGNALINGREADY:
862 CallChannels_w(&TransportChannelImpl::OnSignalingReady);
863 break;
864 case MSG_ONREMOTECANDIDATE: {
865 ChannelParams* params = static_cast<ChannelParams*>(msg->pdata);
866 OnRemoteCandidate_w(*params->candidate);
867 delete params;
868 }
869 break;
870 case MSG_CONNECTING:
871 OnConnecting_s();
872 break;
873 case MSG_READSTATE:
874 OnChannelReadableState_s();
875 break;
876 case MSG_WRITESTATE:
877 OnChannelWritableState_s();
878 break;
879 case MSG_RECEIVINGSTATE:
880 OnChannelReceivingState_s();
881 break;
882 case MSG_REQUESTSIGNALING:
883 OnChannelRequestSignaling_s();
884 break;
885 case MSG_CANDIDATEREADY:
886 OnChannelCandidateReady_s();
887 break;
888 case MSG_ROUTECHANGE: {
889 ChannelParams* params = static_cast<ChannelParams*>(msg->pdata);
890 OnChannelRouteChange_s(params->channel, *params->candidate);
891 delete params;
892 }
893 break;
894 case MSG_CANDIDATEALLOCATIONCOMPLETE:
895 OnChannelCandidatesAllocationDone_s();
896 break;
897 case MSG_ROLECONFLICT:
898 SignalRoleConflict();
899 break;
900 case MSG_COMPLETED:
901 SignalCompleted(this);
902 break;
903 case MSG_FAILED:
904 SignalFailed(this);
905 break;
906 }
907 }
908
909 } // namespace cricket 650 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698