1 // Copyright (c) 2012 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.
5 #include "content/renderer/p2p/port_allocator.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "content/public/common/content_switches.h"
13 P2PPortAllocator::Config::Config() {}
15 P2PPortAllocator::Config::~Config() {
18 P2PPortAllocator::Config::RelayServerConfig::RelayServerConfig()
22 P2PPortAllocator::Config::RelayServerConfig::~RelayServerConfig() {
25 P2PPortAllocator::P2PPortAllocator(
26 P2PSocketDispatcher
* socket_dispatcher
,
27 rtc::NetworkManager
* network_manager
,
28 rtc::PacketSocketFactory
* socket_factory
,
31 : cricket::BasicPortAllocator(network_manager
, socket_factory
),
32 socket_dispatcher_(socket_dispatcher
),
37 if (!config_
.enable_multiple_routes
)
38 flags
|= cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION
;
39 if (!config_
.enable_nonproxied_udp
) {
40 flags
|= cricket::PORTALLOCATOR_DISABLE_UDP
|
41 cricket::PORTALLOCATOR_DISABLE_STUN
|
42 cricket::PORTALLOCATOR_DISABLE_UDP_RELAY
;
45 set_allow_tcp_listen(false);
46 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
47 bool enable_webrtc_stun_origin
=
48 cmd_line
->HasSwitch(switches::kEnableWebRtcStunOrigin
);
49 if (enable_webrtc_stun_origin
) {
50 set_origin(origin
.spec());
54 P2PPortAllocator::~P2PPortAllocator() {
57 cricket::PortAllocatorSession
* P2PPortAllocator::CreateSessionInternal(
58 const std::string
& content_name
,
60 const std::string
& ice_username_fragment
,
61 const std::string
& ice_password
) {
62 return new P2PPortAllocatorSession(
63 this, content_name
, component
, ice_username_fragment
, ice_password
);
66 P2PPortAllocatorSession::P2PPortAllocatorSession(
67 P2PPortAllocator
* allocator
,
68 const std::string
& content_name
,
70 const std::string
& ice_username_fragment
,
71 const std::string
& ice_password
)
72 : cricket::BasicPortAllocatorSession(allocator
,
75 ice_username_fragment
,
77 allocator_(allocator
) {
80 P2PPortAllocatorSession::~P2PPortAllocatorSession() {
83 void P2PPortAllocatorSession::GetPortConfigurations() {
84 const P2PPortAllocator::Config
& config
= allocator_
->config_
;
85 cricket::PortConfiguration
* port_config
= new cricket::PortConfiguration(
86 config
.stun_servers
, std::string(), std::string());
88 for (size_t i
= 0; i
< config
.relays
.size(); ++i
) {
89 cricket::RelayCredentials
credentials(config
.relays
[i
].username
,
90 config
.relays
[i
].password
);
91 cricket::RelayServerConfig
relay_server(cricket::RELAY_TURN
);
92 cricket::ProtocolType protocol
;
93 if (!cricket::StringToProto(config
.relays
[i
].transport_type
.c_str(),
95 DLOG(WARNING
) << "Ignoring TURN server "
96 << config
.relays
[i
].server_address
<< ". "
97 << "Reason= Incorrect "
98 << config
.relays
[i
].transport_type
99 << " transport parameter.";
103 relay_server
.ports
.push_back(cricket::ProtocolAddress(
104 rtc::SocketAddress(config
.relays
[i
].server_address
,
105 config
.relays
[i
].port
),
107 config
.relays
[i
].secure
));
108 relay_server
.credentials
= credentials
;
109 port_config
->AddRelay(relay_server
);
111 ConfigReady(port_config
);
114 } // namespace content