Support multiple URLs in PeerConnectionInterface::IceServer

This adds support for multiple URLs in a IceServer configuration as
defined in http://w3c.github.io/webrtc-pc/#idl-def-RTCIceServer.

BUG=2096
R=pthatcher@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/57489004

Cr-Commit-Position: refs/heads/master@{#9320}
This commit is contained in:
Joachim Bauch
2015-05-28 23:06:30 +02:00
parent 45b229cc89
commit 7c4e7458b5
3 changed files with 138 additions and 88 deletions

View File

@@ -166,7 +166,8 @@ typedef webrtc::PortAllocatorFactoryInterface::StunConfiguration
typedef webrtc::PortAllocatorFactoryInterface::TurnConfiguration
TurnConfiguration;
bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration,
bool ParseIceServerUrl(const PeerConnectionInterface::IceServer& server,
const std::string& url,
std::vector<StunConfiguration>* stun_config,
std::vector<TurnConfiguration>* turn_config) {
// draft-nandakumar-rtcweb-stun-uri-01
@@ -183,15 +184,9 @@ bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration,
// transport-ext = 1*unreserved
// turn-host = IP-literal / IPv4address / reg-name
// turn-port = *DIGIT
for (size_t i = 0; i < configuration.size(); ++i) {
webrtc::PeerConnectionInterface::IceServer server = configuration[i];
if (server.uri.empty()) {
LOG(WARNING) << "Empty uri.";
continue;
}
std::vector<std::string> tokens;
std::string turn_transport_type = kUdpTransportType;
rtc::tokenize(server.uri, '?', &tokens);
rtc::tokenize(url, '?', &tokens);
std::string uri_without_transport = tokens[0];
// Let's look into transport= param, if it exists.
if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present.
@@ -202,7 +197,7 @@ bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration,
// letters.
if (tokens[1] != kUdpTransportType && tokens[1] != kTcpTransportType) {
LOG(LS_WARNING) << "Transport param should always be udp or tcp.";
continue;
return true;
}
turn_transport_type = tokens[1];
}
@@ -215,7 +210,7 @@ bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration,
&hoststring)) {
LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: "
<< uri_without_transport;
continue;
return true;
}
ASSERT(!hoststring.empty());
@@ -224,10 +219,11 @@ bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration,
tokens.clear();
rtc::tokenize(hoststring, '@', &tokens);
ASSERT(!tokens.empty());
std::string username(server.username);
// TODO(pthatcher): What's the right thing to do if tokens.size() is >2?
// E.g. a string like "foo@bar@bat".
if (tokens.size() >= kTurnHostTokensNum) {
server.username = rtc::s_url_decode(tokens[0]);
username.assign(rtc::s_url_decode(tokens[0]));
hoststring = tokens[1];
} else {
hoststring = tokens[0];
@@ -242,12 +238,12 @@ bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration,
std::string address;
if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
LOG(WARNING) << "Invalid Hostname format: " << uri_without_transport;
continue;
return true;
}
if (port <= 0 || port > 0xffff) {
LOG(WARNING) << "Invalid port: " << port;
continue;
return true;
}
switch (service_type) {
@@ -257,12 +253,12 @@ bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration,
break;
case TURN:
case TURNS: {
if (server.username.empty()) {
if (username.empty()) {
// Turn url example from the spec |url:"turn:user@turn.example.org"|.
std::vector<std::string> turn_tokens;
rtc::tokenize(address, '@', &turn_tokens);
if (turn_tokens.size() == kTurnHostTokensNum) {
server.username = rtc::s_url_decode(turn_tokens[0]);
username.assign(rtc::s_url_decode(turn_tokens[0]));
address = turn_tokens[1];
}
}
@@ -270,7 +266,7 @@ bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration,
bool secure = (service_type == TURNS);
turn_config->push_back(TurnConfiguration(address, port,
server.username,
username,
server.password,
turn_transport_type,
secure));
@@ -278,9 +274,30 @@ bool ParseIceServers(const PeerConnectionInterface::IceServers& configuration,
}
case INVALID:
default:
LOG(WARNING) << "Configuration not supported: " << server.uri;
LOG(WARNING) << "Configuration not supported: " << url;
return false;
}
return true;
}
bool ParseIceServers(const PeerConnectionInterface::IceServers& servers,
std::vector<StunConfiguration>* stun_config,
std::vector<TurnConfiguration>* turn_config) {
for (const webrtc::PeerConnectionInterface::IceServer& server : servers) {
if (!server.urls.empty()) {
for (const std::string& url : server.urls) {
if (!ParseIceServerUrl(server, url, stun_config, turn_config)) {
return false;
}
}
} else if (!server.uri.empty()) {
// Fallback to old .uri if new .urls isn't present.
if (!ParseIceServerUrl(server, server.uri, stun_config, turn_config)) {
return false;
}
} else {
LOG(WARNING) << "Empty uri.";
}
}
return true;
}

View File

@@ -198,6 +198,37 @@ TEST_F(PeerConnectionFactoryTest, CreatePCUsingIceServers) {
VerifyTurnConfigurations(turn_configs);
}
// This test verifies creation of PeerConnection with valid STUN and TURN
// configuration. Also verifies the list of URL's parsed correctly as expected.
TEST_F(PeerConnectionFactoryTest, CreatePCUsingIceServersUrls) {
PeerConnectionInterface::RTCConfiguration config;
webrtc::PeerConnectionInterface::IceServer ice_server;
ice_server.urls.push_back(kStunIceServer);
ice_server.urls.push_back(kTurnIceServer);
ice_server.urls.push_back(kTurnIceServerWithTransport);
ice_server.password = kTurnPassword;
config.servers.push_back(ice_server);
rtc::scoped_refptr<PeerConnectionInterface> pc(
factory_->CreatePeerConnection(config, NULL,
allocator_factory_.get(),
new FakeIdentityService(),
&observer_));
EXPECT_TRUE(pc.get() != NULL);
StunConfigurations stun_configs;
webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1(
"stun.l.google.com", 19302);
stun_configs.push_back(stun1);
VerifyStunConfigurations(stun_configs);
TurnConfigurations turn_configs;
webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
"test.com", 1234, "test@hello.com", kTurnPassword, "udp", false);
turn_configs.push_back(turn1);
webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2(
"hello.com", kDefaultStunPort, "test", kTurnPassword, "tcp", false);
turn_configs.push_back(turn2);
VerifyTurnConfigurations(turn_configs);
}
// This test verifies creation of PeerConnection with valid STUN and TURN
// configuration. Also verifies the URL's parsed correctly as expected.
// This version doesn't use RTCConfiguration.

View File

@@ -175,7 +175,9 @@ class PeerConnectionInterface : public rtc::RefCountInterface {
};
struct IceServer {
// TODO(jbauch): Remove uri when all code using it has switched to urls.
std::string uri;
std::vector<std::string> urls;
std::string username;
std::string password;
};