| OLD | NEW |
| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "jingle/glue/utils.h" | 14 #include "jingle/glue/utils.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "remoting/protocol/channel_socket_adapter.h" | 16 #include "remoting/protocol/channel_socket_adapter.h" |
| 17 #include "remoting/protocol/port_allocator_factory.h" | 17 #include "remoting/protocol/port_allocator_factory.h" |
| 18 #include "remoting/protocol/transport_context.h" | 18 #include "remoting/protocol/transport_context.h" |
| 19 #include "third_party/webrtc/base/network.h" | 19 #include "third_party/webrtc/base/network.h" |
| 20 #include "third_party/webrtc/p2p/base/constants.h" | 20 #include "third_party/webrtc/p2p/base/constants.h" |
| 21 #include "third_party/webrtc/p2p/base/p2ptransportchannel.h" | 21 #include "third_party/webrtc/p2p/base/p2ptransportchannel.h" |
| 22 #include "third_party/webrtc/p2p/base/port.h" | 22 #include "third_party/webrtc/p2p/base/port.h" |
| 23 #include "third_party/webrtc/p2p/client/httpportallocator.h" | 23 #include "third_party/webrtc/p2p/client/httpportallocator.h" |
| 24 | 24 |
| 25 namespace remoting { | 25 namespace remoting { |
| 26 namespace protocol { | 26 namespace protocol { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // Try connecting ICE twice with timeout of 15 seconds for each attempt. | |
| 31 const int kMaxReconnectAttempts = 2; | |
| 32 const int kReconnectDelaySeconds = 15; | |
| 33 | |
| 34 // Utility function to map a cricket::Candidate string type to a | 30 // Utility function to map a cricket::Candidate string type to a |
| 35 // TransportRoute::RouteType enum value. | 31 // TransportRoute::RouteType enum value. |
| 36 TransportRoute::RouteType CandidateTypeToTransportRouteType( | 32 TransportRoute::RouteType CandidateTypeToTransportRouteType( |
| 37 const std::string& candidate_type) { | 33 const std::string& candidate_type) { |
| 38 if (candidate_type == "local") { | 34 if (candidate_type == "local") { |
| 39 return TransportRoute::DIRECT; | 35 return TransportRoute::DIRECT; |
| 40 } else if (candidate_type == "stun" || candidate_type == "prflx") { | 36 } else if (candidate_type == "stun" || candidate_type == "prflx") { |
| 41 return TransportRoute::STUN; | 37 return TransportRoute::STUN; |
| 42 } else if (candidate_type == "relay") { | 38 } else if (candidate_type == "relay") { |
| 43 return TransportRoute::RELAY; | 39 return TransportRoute::RELAY; |
| 44 } else { | 40 } else { |
| 45 LOG(FATAL) << "Unknown candidate type: " << candidate_type; | 41 LOG(FATAL) << "Unknown candidate type: " << candidate_type; |
| 46 return TransportRoute::DIRECT; | 42 return TransportRoute::DIRECT; |
| 47 } | 43 } |
| 48 } | 44 } |
| 49 | 45 |
| 50 } // namespace | 46 } // namespace |
| 51 | 47 |
| 52 IceTransportChannel::IceTransportChannel( | 48 IceTransportChannel::IceTransportChannel( |
| 53 scoped_refptr<TransportContext> transport_context) | 49 scoped_refptr<TransportContext> transport_context) |
| 54 : transport_context_(transport_context), | 50 : transport_context_(transport_context), |
| 55 ice_username_fragment_( | 51 ice_username_fragment_( |
| 56 rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH)), | 52 rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH)), |
| 57 connect_attempts_left_(kMaxReconnectAttempts), | 53 connect_attempts_left_( |
| 54 transport_context->network_settings().ice_reconnect_attempts), |
| 58 weak_factory_(this) { | 55 weak_factory_(this) { |
| 59 DCHECK(!ice_username_fragment_.empty()); | 56 DCHECK(!ice_username_fragment_.empty()); |
| 60 } | 57 } |
| 61 | 58 |
| 62 IceTransportChannel::~IceTransportChannel() { | 59 IceTransportChannel::~IceTransportChannel() { |
| 63 DCHECK(delegate_); | 60 DCHECK(delegate_); |
| 64 | 61 |
| 65 delegate_->OnChannelDeleted(this); | 62 delegate_->OnChannelDeleted(this); |
| 66 | 63 |
| 67 auto task_runner = base::ThreadTaskRunnerHandle::Get(); | 64 auto task_runner = base::ThreadTaskRunnerHandle::Get(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 } | 116 } |
| 120 | 117 |
| 121 while (!pending_candidates_.empty()) { | 118 while (!pending_candidates_.empty()) { |
| 122 channel_->AddRemoteCandidate(pending_candidates_.front()); | 119 channel_->AddRemoteCandidate(pending_candidates_.front()); |
| 123 pending_candidates_.pop_front(); | 120 pending_candidates_.pop_front(); |
| 124 } | 121 } |
| 125 | 122 |
| 126 --connect_attempts_left_; | 123 --connect_attempts_left_; |
| 127 | 124 |
| 128 // Start reconnection timer. | 125 // Start reconnection timer. |
| 129 reconnect_timer_.Start( | 126 reconnect_timer_.Start(FROM_HERE, |
| 130 FROM_HERE, base::TimeDelta::FromSeconds(kReconnectDelaySeconds), | 127 transport_context_->network_settings().ice_timeout, |
| 131 this, &IceTransportChannel::TryReconnect); | 128 this, &IceTransportChannel::TryReconnect); |
| 132 | 129 |
| 133 base::ThreadTaskRunnerHandle::Get()->PostTask( | 130 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 134 FROM_HERE, base::Bind(&IceTransportChannel::NotifyConnected, | 131 FROM_HERE, base::Bind(&IceTransportChannel::NotifyConnected, |
| 135 weak_factory_.GetWeakPtr())); | 132 weak_factory_.GetWeakPtr())); |
| 136 } | 133 } |
| 137 | 134 |
| 138 void IceTransportChannel::NotifyConnected() { | 135 void IceTransportChannel::NotifyConnected() { |
| 139 // Create P2PDatagramSocket adapter for the P2PTransportChannel. | 136 // Create P2PDatagramSocket adapter for the P2PTransportChannel. |
| 140 scoped_ptr<TransportChannelSocketAdapter> socket( | 137 scoped_ptr<TransportChannelSocketAdapter> socket( |
| 141 new TransportChannelSocketAdapter(channel_.get())); | 138 new TransportChannelSocketAdapter(channel_.get())); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 const cricket::Candidate& candidate) { | 192 const cricket::Candidate& candidate) { |
| 196 // Ignore notifications if the channel is not writable. | 193 // Ignore notifications if the channel is not writable. |
| 197 if (channel_->writable()) | 194 if (channel_->writable()) |
| 198 NotifyRouteChanged(); | 195 NotifyRouteChanged(); |
| 199 } | 196 } |
| 200 | 197 |
| 201 void IceTransportChannel::OnWritableState(cricket::TransportChannel* channel) { | 198 void IceTransportChannel::OnWritableState(cricket::TransportChannel* channel) { |
| 202 DCHECK_EQ(channel, static_cast<cricket::TransportChannel*>(channel_.get())); | 199 DCHECK_EQ(channel, static_cast<cricket::TransportChannel*>(channel_.get())); |
| 203 | 200 |
| 204 if (channel->writable()) { | 201 if (channel->writable()) { |
| 205 connect_attempts_left_ = kMaxReconnectAttempts; | 202 connect_attempts_left_ = |
| 203 transport_context_->network_settings().ice_reconnect_attempts; |
| 206 reconnect_timer_.Stop(); | 204 reconnect_timer_.Stop(); |
| 207 | 205 |
| 208 // Route change notifications are ignored when the |channel_| is not | 206 // Route change notifications are ignored when the |channel_| is not |
| 209 // writable. Notify the event handler about the current route once the | 207 // writable. Notify the event handler about the current route once the |
| 210 // channel is writable. | 208 // channel is writable. |
| 211 NotifyRouteChanged(); | 209 NotifyRouteChanged(); |
| 212 } else { | 210 } else { |
| 213 reconnect_timer_.Reset(); | 211 reconnect_timer_.Reset(); |
| 214 TryReconnect(); | 212 TryReconnect(); |
| 215 } | 213 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 266 |
| 269 // Restart ICE by resetting ICE password. | 267 // Restart ICE by resetting ICE password. |
| 270 std::string ice_password = rtc::CreateRandomString(cricket::ICE_PWD_LENGTH); | 268 std::string ice_password = rtc::CreateRandomString(cricket::ICE_PWD_LENGTH); |
| 271 delegate_->OnChannelIceCredentials(this, ice_username_fragment_, | 269 delegate_->OnChannelIceCredentials(this, ice_username_fragment_, |
| 272 ice_password); | 270 ice_password); |
| 273 channel_->SetIceCredentials(ice_username_fragment_, ice_password); | 271 channel_->SetIceCredentials(ice_username_fragment_, ice_password); |
| 274 } | 272 } |
| 275 | 273 |
| 276 } // namespace protocol | 274 } // namespace protocol |
| 277 } // namespace remoting | 275 } // namespace remoting |
| OLD | NEW |