Skip to content

Commit

Permalink
Change url to urls in ice_servers
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Apr 21, 2024
1 parent 2d762ca commit f7c28fc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion example/peer.exs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ defmodule Peer do
{_, _, _, _} -> true
{_, _, _, _, _, _, _, _} -> false
end,
ice_servers: [%{url: "stun:stun.l.google.com:19302"}]
ice_servers: [%{urls: "stun:stun.l.google.com:19302"}]
)

{:ok, ufrag, passwd} = ICEAgent.get_local_credentials(pid)
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_ice/ice_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule ExICE.ICEAgent do
ip_filter: (:inet.ip_address() -> boolean),
ice_servers: [
%{
:url => String.t(),
:urls => [String.t()] | String.t(),
optional(:username) => String.t(),
optional(:credential) => String.t()
}
Expand Down
25 changes: 16 additions & 9 deletions lib/ex_ice/priv/ice_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1711,16 +1711,23 @@ defmodule ExICE.Priv.ICEAgent do
end

defp parse_ice_servers(ice_servers) do
# Parse and flatten URLs
ice_servers
|> Enum.map(fn ice_server ->
case ExSTUN.URI.parse(ice_server.url) do
{:ok, url} ->
%{ice_server | url: url}

:error ->
Logger.warning("Couldn't parse URL: #{inspect(ice_server.url)}. Ignoring.")
nil
end
|> Enum.flat_map(fn ice_server ->
ice_server.urls
|> List.wrap()
|> Enum.map(fn url ->
case ExSTUN.URI.parse(url) do
{:ok, url} ->
ice_server
|> Map.delete(:urls)
|> Map.put(:url, url)

:error ->
Logger.warning("Couldn't parse URL: #{inspect(ice_server.url)}. Ignoring.")
nil
end
end)
end)
|> Enum.reject(&(&1 == nil))
|> Enum.split_with(fn ice_server -> ice_server.url.scheme in [:stun, :stuns] end)
Expand Down
6 changes: 3 additions & 3 deletions test/integration/p2p_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule ExICE.Integration.P2PTest do
@tag :p2p
@tag :tmp_dir
test "P2P connection", %{tmp_dir: tmp_dir} do
ice_servers = [%{url: "stun:stun.l.google.com:19302"}]
ice_servers = [%{urls: "stun:stun.l.google.com:19302"}]

ip_filter = fn
{_, _, _, _, _, _, _, _} -> true
Expand Down Expand Up @@ -84,7 +84,7 @@ defmodule ExICE.Integration.P2PTest do
@tag :tmp_dir
@tag :role_conflict
test "P2P connection with role conflict", %{tmp_dir: tmp_dir} do
ice_servers = [%{url: "stun:stun.l.google.com:19302"}]
ice_servers = [%{urls: "stun:stun.l.google.com:19302"}]
# ice_servers = []

ip_filter = fn
Expand Down Expand Up @@ -131,7 +131,7 @@ defmodule ExICE.Integration.P2PTest do

ice_servers = [
%{
url: "turn:127.0.0.1:3478?transport=udp",
urls: "turn:127.0.0.1:3478?transport=udp",
username: "testusername",
credential: "testpassword"
}
Expand Down
8 changes: 4 additions & 4 deletions test/priv/ice_agent_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ defmodule ExICE.Priv.ICEAgentTest do
role: :controlling,
transport_module: Transport.Mock,
if_discovery_module: IfDiscovery.Mock,
ice_servers: [%{url: "stun:#{@stun_ip_str}:#{@stun_port}"}]
ice_servers: [%{urls: "stun:#{@stun_ip_str}:#{@stun_port}"}]
)
|> ICEAgent.set_remote_credentials("someufrag", "somepwd")
|> ICEAgent.gather_candidates()
Expand Down Expand Up @@ -703,7 +703,7 @@ defmodule ExICE.Priv.ICEAgentTest do
if_discovery_module: IfDiscovery.Mock,
ice_servers: [
%{
url: "turn:#{@turn_ip_str}:#{@turn_port}?transport=udp",
urls: "turn:#{@turn_ip_str}:#{@turn_port}?transport=udp",
username: @turn_username,
credential: @turn_password
}
Expand Down Expand Up @@ -861,7 +861,7 @@ defmodule ExICE.Priv.ICEAgentTest do
role: :controlling,
transport_module: Transport.Mock,
if_discovery_module: IfDiscovery.Mock,
ice_servers: [%{url: "stun:192.168.0.3:19302"}],
ice_servers: [%{urls: "stun:192.168.0.3:19302"}],
ice_transport_policy: :relay
)
|> ICEAgent.set_remote_credentials("someufrag", "somepwd")
Expand Down Expand Up @@ -946,7 +946,7 @@ defmodule ExICE.Priv.ICEAgentTest do
transport_module: Transport.Mock,
ice_servers: [
%{
url: "turn:#{@turn_ip_str}:#{@turn_port}?transport=udp",
urls: "turn:#{@turn_ip_str}:#{@turn_port}?transport=udp",
username: @turn_username,
credential: @turn_password
}
Expand Down

0 comments on commit f7c28fc

Please sign in to comment.