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

Side by Side Diff: remoting/protocol/ice_transport_channel.cc

Issue 1521883006: Add TransportContext class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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 | « remoting/protocol/ice_transport_channel.h ('k') | remoting/protocol/ice_transport_factory.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/protocol/ice_transport_channel.h" 5 #include "remoting/protocol/ice_transport_channel.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
13 #include "jingle/glue/utils.h" 13 #include "jingle/glue/utils.h"
14 #include "net/base/net_errors.h" 14 #include "net/base/net_errors.h"
15 #include "remoting/protocol/channel_socket_adapter.h" 15 #include "remoting/protocol/channel_socket_adapter.h"
16 #include "remoting/protocol/transport_context.h"
16 #include "third_party/webrtc/base/network.h" 17 #include "third_party/webrtc/base/network.h"
17 #include "third_party/webrtc/p2p/base/constants.h" 18 #include "third_party/webrtc/p2p/base/constants.h"
18 #include "third_party/webrtc/p2p/base/p2ptransportchannel.h" 19 #include "third_party/webrtc/p2p/base/p2ptransportchannel.h"
19 #include "third_party/webrtc/p2p/base/port.h" 20 #include "third_party/webrtc/p2p/base/port.h"
20 #include "third_party/webrtc/p2p/client/httpportallocator.h" 21 #include "third_party/webrtc/p2p/client/httpportallocator.h"
21 22
22 namespace remoting { 23 namespace remoting {
23 namespace protocol { 24 namespace protocol {
24 25
25 namespace { 26 namespace {
(...skipping 13 matching lines...) Expand all
39 } else if (candidate_type == "relay") { 40 } else if (candidate_type == "relay") {
40 return TransportRoute::RELAY; 41 return TransportRoute::RELAY;
41 } else { 42 } else {
42 LOG(FATAL) << "Unknown candidate type: " << candidate_type; 43 LOG(FATAL) << "Unknown candidate type: " << candidate_type;
43 return TransportRoute::DIRECT; 44 return TransportRoute::DIRECT;
44 } 45 }
45 } 46 }
46 47
47 } // namespace 48 } // namespace
48 49
49 IceTransportChannel::IceTransportChannel(cricket::PortAllocator* port_allocator, 50 IceTransportChannel::IceTransportChannel(
50 const NetworkSettings& network_settings, 51 scoped_refptr<TransportContext> transport_context)
51 TransportRole role) 52 : transport_context_(transport_context),
52 : port_allocator_(port_allocator),
53 network_settings_(network_settings),
54 role_(role),
55 delegate_(nullptr),
56 ice_username_fragment_( 53 ice_username_fragment_(
57 rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH)), 54 rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH)),
58 can_start_(false),
59 connect_attempts_left_(kMaxReconnectAttempts), 55 connect_attempts_left_(kMaxReconnectAttempts),
60 weak_factory_(this) { 56 weak_factory_(this) {
61 DCHECK(!ice_username_fragment_.empty()); 57 DCHECK(!ice_username_fragment_.empty());
62 } 58 }
63 59
64 IceTransportChannel::~IceTransportChannel() { 60 IceTransportChannel::~IceTransportChannel() {
65 DCHECK(delegate_); 61 DCHECK(delegate_);
66 62
67 delegate_->OnTransportDeleted(this); 63 delegate_->OnTransportDeleted(this);
68 64
69 if (channel_.get()) { 65 auto task_runner = base::ThreadTaskRunnerHandle::Get();
70 base::ThreadTaskRunnerHandle::Get()->DeleteSoon( 66 if (channel_)
71 FROM_HERE, channel_.release()); 67 task_runner->DeleteSoon(FROM_HERE, channel_.release());
72 } 68 if (port_allocator_)
73 } 69 task_runner->DeleteSoon(FROM_HERE, port_allocator_.release());
74
75 void IceTransportChannel::OnCanStart() {
76 DCHECK(thread_checker_.CalledOnValidThread());
77
78 DCHECK(!can_start_);
79 can_start_ = true;
80
81 // If Connect() has been called then start connection.
82 if (!callback_.is_null())
83 DoStart();
84
85 // Pass pending ICE credentials and candidates to the channel.
86 if (!remote_ice_username_fragment_.empty()) {
87 channel_->SetRemoteIceCredentials(remote_ice_username_fragment_,
88 remote_ice_password_);
89 }
90
91 while (!pending_candidates_.empty()) {
92 channel_->AddRemoteCandidate(pending_candidates_.front());
93 pending_candidates_.pop_front();
94 }
95 } 70 }
96 71
97 void IceTransportChannel::Connect(const std::string& name, 72 void IceTransportChannel::Connect(const std::string& name,
98 Delegate* delegate, 73 Delegate* delegate,
99 const ConnectedCallback& callback) { 74 const ConnectedCallback& callback) {
100 DCHECK(thread_checker_.CalledOnValidThread()); 75 DCHECK(thread_checker_.CalledOnValidThread());
101 DCHECK(!name.empty()); 76 DCHECK(!name.empty());
102 DCHECK(delegate); 77 DCHECK(delegate);
103 DCHECK(!callback.is_null()); 78 DCHECK(!callback.is_null());
104 79
105 DCHECK(name_.empty()); 80 DCHECK(name_.empty());
106 name_ = name; 81 name_ = name;
107 delegate_ = delegate; 82 delegate_ = delegate;
108 callback_ = callback; 83 callback_ = callback;
109 84
110 if (can_start_) 85 transport_context_->CreatePortAllocator(
111 DoStart(); 86 base::Bind(&IceTransportChannel::OnPortAllocatorCreated,
87 weak_factory_.GetWeakPtr()));
112 } 88 }
113 89
114 void IceTransportChannel::DoStart() { 90 void IceTransportChannel::OnPortAllocatorCreated(
91 scoped_ptr<cricket::PortAllocator> port_allocator){
115 DCHECK(!channel_.get()); 92 DCHECK(!channel_.get());
116 93
94 port_allocator_ = port_allocator.Pass();
95
117 // Create P2PTransportChannel, attach signal handlers and connect it. 96 // Create P2PTransportChannel, attach signal handlers and connect it.
118 // TODO(sergeyu): Specify correct component ID for the channel. 97 // TODO(sergeyu): Specify correct component ID for the channel.
119 channel_.reset(new cricket::P2PTransportChannel( 98 channel_.reset(new cricket::P2PTransportChannel(
120 std::string(), 0, nullptr, port_allocator_)); 99 std::string(), 0, nullptr, port_allocator_.get()));
121 std::string ice_password = rtc::CreateRandomString(cricket::ICE_PWD_LENGTH); 100 std::string ice_password = rtc::CreateRandomString(cricket::ICE_PWD_LENGTH);
122 channel_->SetIceProtocolType(cricket::ICEPROTO_RFC5245); 101 channel_->SetIceProtocolType(cricket::ICEPROTO_RFC5245);
123 channel_->SetIceRole((role_ == TransportRole::CLIENT) 102 channel_->SetIceRole((transport_context_->role() == TransportRole::CLIENT)
124 ? cricket::ICEROLE_CONTROLLING 103 ? cricket::ICEROLE_CONTROLLING
125 : cricket::ICEROLE_CONTROLLED); 104 : cricket::ICEROLE_CONTROLLED);
126 delegate_->OnTransportIceCredentials(this, ice_username_fragment_, 105 delegate_->OnTransportIceCredentials(this, ice_username_fragment_,
127 ice_password); 106 ice_password);
128 channel_->SetIceCredentials(ice_username_fragment_, ice_password); 107 channel_->SetIceCredentials(ice_username_fragment_, ice_password);
129 channel_->SignalCandidateGathered.connect( 108 channel_->SignalCandidateGathered.connect(
130 this, &IceTransportChannel::OnCandidateGathered); 109 this, &IceTransportChannel::OnCandidateGathered);
131 channel_->SignalRouteChange.connect( 110 channel_->SignalRouteChange.connect(
132 this, &IceTransportChannel::OnRouteChange); 111 this, &IceTransportChannel::OnRouteChange);
133 channel_->SignalReceivingState.connect( 112 channel_->SignalReceivingState.connect(
134 this, &IceTransportChannel::OnReceivingState); 113 this, &IceTransportChannel::OnReceivingState);
135 channel_->SignalWritableState.connect( 114 channel_->SignalWritableState.connect(
136 this, &IceTransportChannel::OnWritableState); 115 this, &IceTransportChannel::OnWritableState);
137 channel_->set_incoming_only( 116 channel_->set_incoming_only(!(transport_context_->network_settings().flags &
138 !(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_OUTGOING)); 117 NetworkSettings::NAT_TRAVERSAL_OUTGOING));
139 118
140 channel_->Connect(); 119 channel_->Connect();
141 channel_->MaybeStartGathering(); 120 channel_->MaybeStartGathering();
142 121
122 // Pass pending ICE credentials and candidates to the channel.
123 if (!remote_ice_username_fragment_.empty()) {
124 channel_->SetRemoteIceCredentials(remote_ice_username_fragment_,
125 remote_ice_password_);
126 }
127
128 while (!pending_candidates_.empty()) {
129 channel_->AddRemoteCandidate(pending_candidates_.front());
130 pending_candidates_.pop_front();
131 }
132
143 --connect_attempts_left_; 133 --connect_attempts_left_;
144 134
145 // Start reconnection timer. 135 // Start reconnection timer.
146 reconnect_timer_.Start( 136 reconnect_timer_.Start(
147 FROM_HERE, base::TimeDelta::FromSeconds(kReconnectDelaySeconds), 137 FROM_HERE, base::TimeDelta::FromSeconds(kReconnectDelaySeconds),
148 this, &IceTransportChannel::TryReconnect); 138 this, &IceTransportChannel::TryReconnect);
149 } 139 }
150 140
151 void IceTransportChannel::NotifyConnected() { 141 void IceTransportChannel::NotifyConnected() {
152 // Create P2PDatagramSocket adapter for the P2PTransportChannel. 142 // Create P2PDatagramSocket adapter for the P2PTransportChannel.
153 scoped_ptr<TransportChannelSocketAdapter> socket( 143 scoped_ptr<TransportChannelSocketAdapter> socket(
154 new TransportChannelSocketAdapter(channel_.get())); 144 new TransportChannelSocketAdapter(channel_.get()));
155 socket->SetOnDestroyedCallback(base::Bind( 145 socket->SetOnDestroyedCallback(base::Bind(
156 &IceTransportChannel::OnChannelDestroyed, base::Unretained(this))); 146 &IceTransportChannel::OnChannelDestroyed, base::Unretained(this)));
157 base::ResetAndReturn(&callback_).Run(socket.Pass()); 147 base::ResetAndReturn(&callback_).Run(socket.Pass());
158 } 148 }
159 149
160 void IceTransportChannel::SetRemoteCredentials(const std::string& ufrag, 150 void IceTransportChannel::SetRemoteCredentials(const std::string& ufrag,
161 const std::string& password) { 151 const std::string& password) {
162 DCHECK(thread_checker_.CalledOnValidThread()); 152 DCHECK(thread_checker_.CalledOnValidThread());
163 153
164 remote_ice_username_fragment_ = ufrag; 154 remote_ice_username_fragment_ = ufrag;
165 remote_ice_password_ = password; 155 remote_ice_password_ = password;
166 156
167 if (channel_) 157 if (channel_)
168 channel_->SetRemoteIceCredentials(ufrag, password); 158 channel_->SetRemoteIceCredentials(ufrag, password);
169 } 159 }
170 160
171 void IceTransportChannel::AddRemoteCandidate( 161 void IceTransportChannel::AddRemoteCandidate(
172 const cricket::Candidate& candidate) { 162 const cricket::Candidate& candidate) {
173 DCHECK(thread_checker_.CalledOnValidThread()); 163 DCHECK(thread_checker_.CalledOnValidThread());
174 164
175 // To enforce the no-relay setting, it's not enough to not produce relay 165 // To enforce the no-relay setting, it's not enough to not produce relay
176 // candidates. It's also necessary to discard remote relay candidates. 166 // candidates. It's also necessary to discard remote relay candidates.
177 bool relay_allowed = (network_settings_.flags & 167 bool relay_allowed = (transport_context_->network_settings().flags &
178 NetworkSettings::NAT_TRAVERSAL_RELAY) != 0; 168 NetworkSettings::NAT_TRAVERSAL_RELAY) != 0;
179 if (!relay_allowed && candidate.type() == cricket::RELAY_PORT_TYPE) 169 if (!relay_allowed && candidate.type() == cricket::RELAY_PORT_TYPE)
180 return; 170 return;
181 171
182 if (channel_) { 172 if (channel_) {
183 channel_->AddRemoteCandidate(candidate); 173 channel_->AddRemoteCandidate(candidate);
184 } else { 174 } else {
185 pending_candidates_.push_back(candidate); 175 pending_candidates_.push_back(candidate);
186 } 176 }
187 } 177 }
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 278
289 // Restart ICE by resetting ICE password. 279 // Restart ICE by resetting ICE password.
290 std::string ice_password = rtc::CreateRandomString(cricket::ICE_PWD_LENGTH); 280 std::string ice_password = rtc::CreateRandomString(cricket::ICE_PWD_LENGTH);
291 delegate_->OnTransportIceCredentials(this, ice_username_fragment_, 281 delegate_->OnTransportIceCredentials(this, ice_username_fragment_,
292 ice_password); 282 ice_password);
293 channel_->SetIceCredentials(ice_username_fragment_, ice_password); 283 channel_->SetIceCredentials(ice_username_fragment_, ice_password);
294 } 284 }
295 285
296 } // namespace protocol 286 } // namespace protocol
297 } // namespace remoting 287 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/ice_transport_channel.h ('k') | remoting/protocol/ice_transport_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698