diff --git a/webrtc/p2p/stunprober/stunprober.cc b/webrtc/p2p/stunprober/stunprober.cc index 938bb21e25..29939be95f 100644 --- a/webrtc/p2p/stunprober/stunprober.cc +++ b/webrtc/p2p/stunprober/stunprober.cc @@ -74,28 +74,11 @@ void StunProber::Requester::SendStunRequest() { auto addr = server_ips_[num_request_sent_]; request.server_addr = addr.ipaddr(); - int rv = 0; - - // Only bind to the interface at the first request. - if (num_request_sent_ == 0) { - rtc::IPAddress local_addr; - rv = prober_->GetLocalAddress(&local_addr); - if (rv != 0) { - prober_->End(GENERIC_FAILURE, rv); - return; - } - rv = socket_->Bind(rtc::SocketAddress(local_addr, 0)); - if (rv < 0) { - prober_->End(GENERIC_FAILURE, rv); - return; - } - } - // The write must succeed immediately. Otherwise, the calculating of the STUN // request timing could become too complicated. Callback is ignored by passing // empty AsyncCallback. - rv = socket_->SendTo(addr, const_cast(request_packet->Data()), - request_packet->Length(), AsyncCallback()); + int rv = socket_->SendTo(addr, const_cast(request_packet->Data()), + request_packet->Length(), AsyncCallback()); if (rv < 0) { prober_->End(WRITE_FAILED, rv); return; @@ -279,7 +262,12 @@ void StunProber::OnServerResolved(int index, int result) { return; } - MaybeScheduleStunRequests(); + socket_factory_->Prepare(GetTotalClientSockets(), GetTotalServerSockets(), + [this](int result) { + if (result == 0) { + this->MaybeScheduleStunRequests(); + } + }); } int StunProber::GetLocalAddress(rtc::IPAddress* addr) { @@ -440,6 +428,8 @@ bool StunProber::GetStats(StunProber::Stats* prob_stats) { stats.num_request_sent = num_sent; stats.num_response_received = num_received; stats.target_request_interval_ns = interval_ms_ * 1000; + stats.symmetric_nat = + stats.srflx_addrs.size() > static_cast(GetTotalServerSockets()); if (num_sent) { stats.success_percent = static_cast(100 * num_received / num_sent); diff --git a/webrtc/p2p/stunprober/stunprober.h b/webrtc/p2p/stunprober/stunprober.h index f53be3a480..30cfdb57a0 100644 --- a/webrtc/p2p/stunprober/stunprober.h +++ b/webrtc/p2p/stunprober/stunprober.h @@ -55,7 +55,6 @@ class SocketInterface { FAILED = -2, }; SocketInterface() {} - virtual int GetLocalAddress(rtc::SocketAddress* local_address) = 0; virtual void Close() = 0; virtual ~SocketInterface() {} @@ -70,6 +69,8 @@ class ClientSocketInterface : public SocketInterface { // getsockname will only return 0.0.0.0. virtual int Connect(const rtc::SocketAddress& addr) = 0; + virtual int GetLocalAddress(rtc::SocketAddress* local_address) = 0; + private: DISALLOW_COPY_AND_ASSIGN(ClientSocketInterface); }; @@ -77,7 +78,6 @@ class ClientSocketInterface : public SocketInterface { class ServerSocketInterface : public SocketInterface { public: ServerSocketInterface() {} - virtual int Bind(const rtc::SocketAddress& addr) = 0; virtual int SendTo(const rtc::SocketAddress& addr, char* buf, @@ -99,6 +99,14 @@ class ServerSocketInterface : public SocketInterface { class SocketFactoryInterface { public: SocketFactoryInterface() {} + // To provide a chance to prepare the sockets that we need. This is + // implemented for chrome renderer process as the socket needs to be ready to + // use in browser process. + virtual void Prepare(size_t total_client_socket, + size_t total_server_socket, + AsyncCallback callback) { + callback(0); + } virtual ClientSocketInterface* CreateClientSocket() = 0; virtual ServerSocketInterface* CreateServerSocket( size_t send_buffer_size, @@ -134,6 +142,7 @@ class StunProber { int num_request_sent = 0; int num_response_received = 0; bool behind_nat = false; + bool symmetric_nat = false; int average_rtt_ms = -1; int success_percent = 0; int target_request_interval_ns = 0; @@ -148,8 +157,6 @@ class StunProber { // If the srflx_addrs has more than 1 element, the NAT is symmetric. std::set srflx_addrs; - - bool symmetric_nat() { return srflx_addrs.size() > 1; } }; // StunProber is not thread safe. It's task_runner's responsibility to ensure @@ -265,6 +272,12 @@ class StunProber { return num_request_sent_ >= requests_per_ip_ * all_servers_ips_.size(); } + int GetTotalClientSockets() { return 1; } + int GetTotalServerSockets() { + return (shared_socket_mode_ ? 1 : all_servers_ips_.size()) * + requests_per_ip_; + } + bool SendNextRequest(); // Will be invoked in 1ms intervals and schedule the next request from the diff --git a/webrtc/p2p/stunprober/stunprober_dependencies.h b/webrtc/p2p/stunprober/stunprober_dependencies.h index 813a2f931c..4e64df71ba 100644 --- a/webrtc/p2p/stunprober/stunprober_dependencies.h +++ b/webrtc/p2p/stunprober/stunprober_dependencies.h @@ -35,10 +35,6 @@ class Socket : public ClientSocketInterface, return MapResult(socket_->Connect(addr)); } - int Bind(const rtc::SocketAddress& addr) override { - return MapResult(socket_->Bind(addr)); - } - int SendTo(const rtc::SocketAddress& addr, char* buf, size_t buf_len,