OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/protocol/transport_context.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "remoting/protocol/port_allocator_factory.h" |
| 12 #include "third_party/webrtc/p2p/client/httpportallocator.h" |
| 13 |
| 14 namespace remoting { |
| 15 namespace protocol { |
| 16 |
| 17 // Get fresh STUN/Relay configuration every hour. |
| 18 static const int kJingleInfoUpdatePeriodSeconds = 3600; |
| 19 |
| 20 TransportContext::TransportContext( |
| 21 SignalStrategy* signal_strategy, |
| 22 scoped_ptr<PortAllocatorFactory> port_allocator_factory, |
| 23 const NetworkSettings& network_settings, |
| 24 TransportRole role) |
| 25 : signal_strategy_(signal_strategy), |
| 26 port_allocator_factory_(port_allocator_factory.Pass()), |
| 27 network_settings_(network_settings), |
| 28 role_(role) {} |
| 29 |
| 30 TransportContext::~TransportContext() {} |
| 31 |
| 32 void TransportContext::Prepare() { |
| 33 EnsureFreshJingleInfo(); |
| 34 } |
| 35 |
| 36 void TransportContext::CreatePortAllocator( |
| 37 const CreatePortAllocatorCallback& callback) { |
| 38 EnsureFreshJingleInfo(); |
| 39 |
| 40 // If there is a pending |jingle_info_request_| delay starting the new |
| 41 // transport until the request is finished. |
| 42 if (jingle_info_request_) { |
| 43 pending_port_allocator_requests_.push_back(callback); |
| 44 } else { |
| 45 callback.Run(CreatePortAllocatorInternal()); |
| 46 } |
| 47 } |
| 48 |
| 49 void TransportContext::EnsureFreshJingleInfo() { |
| 50 // Check if request is already pending. |
| 51 if (jingle_info_request_) |
| 52 return; |
| 53 |
| 54 // Don't need to make jingleinfo request if both STUN and Relay are disabled. |
| 55 if ((network_settings_.flags & (NetworkSettings::NAT_TRAVERSAL_STUN | |
| 56 NetworkSettings::NAT_TRAVERSAL_RELAY)) == 0) { |
| 57 return; |
| 58 } |
| 59 |
| 60 if (last_jingle_info_update_time_.is_null() || |
| 61 base::TimeTicks::Now() - last_jingle_info_update_time_ > |
| 62 base::TimeDelta::FromSeconds(kJingleInfoUpdatePeriodSeconds)) { |
| 63 jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); |
| 64 jingle_info_request_->Send(base::Bind( |
| 65 &TransportContext::OnJingleInfo, base::Unretained(this))); |
| 66 } |
| 67 } |
| 68 |
| 69 void TransportContext::OnJingleInfo( |
| 70 const std::string& relay_token, |
| 71 const std::vector<std::string>& relay_hosts, |
| 72 const std::vector<rtc::SocketAddress>& stun_hosts) { |
| 73 relay_token_ = relay_token; |
| 74 relay_hosts_ = relay_hosts; |
| 75 stun_hosts_ = stun_hosts; |
| 76 |
| 77 jingle_info_request_.reset(); |
| 78 if ((!relay_token.empty() && !relay_hosts.empty()) || !stun_hosts.empty()) |
| 79 last_jingle_info_update_time_ = base::TimeTicks::Now(); |
| 80 |
| 81 while (!pending_port_allocator_requests_.empty()) { |
| 82 pending_port_allocator_requests_.begin()->Run( |
| 83 CreatePortAllocatorInternal()); |
| 84 pending_port_allocator_requests_.pop_front(); |
| 85 } |
| 86 } |
| 87 |
| 88 scoped_ptr<cricket::PortAllocator> |
| 89 TransportContext::CreatePortAllocatorInternal() { |
| 90 scoped_ptr<cricket::HttpPortAllocatorBase> result = |
| 91 port_allocator_factory_->CreatePortAllocator(); |
| 92 |
| 93 if (!relay_token_.empty() && !relay_hosts_.empty()) { |
| 94 result->SetRelayHosts(relay_hosts_); |
| 95 result->SetRelayToken(relay_token_); |
| 96 } |
| 97 if (!stun_hosts_.empty()) |
| 98 result->SetStunHosts(stun_hosts_); |
| 99 |
| 100 // We always use PseudoTcp to provide a reliable channel. It provides poor |
| 101 // performance when combined with TCP-based transport, so we have to disable |
| 102 // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username |
| 103 // fragment is shared between all candidates. |
| 104 int flags = cricket::PORTALLOCATOR_DISABLE_TCP | |
| 105 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | |
| 106 cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 107 |
| 108 if (!(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_STUN)) |
| 109 flags |= cricket::PORTALLOCATOR_DISABLE_STUN; |
| 110 |
| 111 if (!(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_RELAY)) |
| 112 flags |= cricket::PORTALLOCATOR_DISABLE_RELAY; |
| 113 |
| 114 result->set_flags(flags); |
| 115 result->SetPortRange(network_settings_.port_range.min_port, |
| 116 network_settings_.port_range.max_port); |
| 117 |
| 118 return result.Pass(); |
| 119 } |
| 120 |
| 121 } // namespace protocol |
| 122 } // namespace remoting |
OLD | NEW |