@@ -86,6 +86,7 @@ defmodule ExICE.Priv.ICEAgent do
86
86
sockets: [ ] ,
87
87
local_cands: % { } ,
88
88
remote_cands: % { } ,
89
+ local_preferences: % { } ,
89
90
stun_servers: [ ] ,
90
91
turn_servers: [ ] ,
91
92
resolved_turn_servers: [ ] ,
@@ -305,7 +306,11 @@ defmodule ExICE.Priv.ICEAgent do
305
306
ice_agent = change_gathering_state ( ice_agent , :gathering )
306
307
307
308
{ :ok , sockets } = Gatherer . open_sockets ( ice_agent . gatherer )
308
- host_cands = Gatherer . gather_host_candidates ( ice_agent . gatherer , sockets )
309
+
310
+ { local_preferences , host_cands } =
311
+ Gatherer . gather_host_candidates ( ice_agent . gatherer , ice_agent . local_preferences , sockets )
312
+
313
+ ice_agent = % __MODULE__ { ice_agent | local_preferences: local_preferences }
309
314
310
315
ice_agent =
311
316
Enum . reduce ( host_cands , ice_agent , fn host_cand , ice_agent ->
@@ -1074,14 +1079,26 @@ defmodule ExICE.Priv.ICEAgent do
1074
1079
{ client . turn_ip , client . turn_port } | ice_agent . resolved_turn_servers
1075
1080
]
1076
1081
1077
- ice_agent = % { ice_agent | resolved_turn_servers: resolved_turn_servers }
1082
+ # Use sock_addr for calculating priority.
1083
+ # In other case, we might duplicate priority.
1084
+ { :ok , { sock_addr , _sock_port } } = ice_agent . transport_module . sockname ( tr . socket )
1085
+
1086
+ { local_preferences , priority } =
1087
+ Candidate . priority ( ice_agent . local_preferences , sock_addr , :relay )
1088
+
1089
+ ice_agent = % {
1090
+ ice_agent
1091
+ | resolved_turn_servers: resolved_turn_servers ,
1092
+ local_preferences: local_preferences
1093
+ }
1078
1094
1079
1095
relay_cand =
1080
1096
Candidate.Relay . new (
1081
1097
address: alloc_ip ,
1082
1098
port: alloc_port ,
1083
1099
base_address: alloc_ip ,
1084
1100
base_port: alloc_port ,
1101
+ priority: priority ,
1085
1102
transport_module: ice_agent . transport_module ,
1086
1103
socket: tr . socket ,
1087
1104
client: client
@@ -1741,12 +1758,18 @@ defmodule ExICE.Priv.ICEAgent do
1741
1758
nil ->
1742
1759
{ :ok , { base_addr , base_port } } = ice_agent . transport_module . sockname ( tr . socket )
1743
1760
1761
+ { local_preferences , priority } =
1762
+ Candidate . priority ( ice_agent . local_preferences , base_addr , :srflx )
1763
+
1764
+ ice_agent = % __MODULE__ { ice_agent | local_preferences: local_preferences }
1765
+
1744
1766
cand =
1745
1767
Candidate.Srflx . new (
1746
1768
address: xor_addr ,
1747
1769
port: xor_port ,
1748
1770
base_address: base_addr ,
1749
1771
base_port: base_port ,
1772
+ priority: priority ,
1750
1773
transport_module: ice_agent . transport_module ,
1751
1774
socket: tr . socket
1752
1775
)
@@ -2058,12 +2081,16 @@ defmodule ExICE.Priv.ICEAgent do
2058
2081
# TODO calculate correct prio and foundation
2059
2082
local_cand = Map . fetch! ( ice_agent . local_cands , conn_check_pair . local_cand_id )
2060
2083
2084
+ priority =
2085
+ Candidate . priority! ( ice_agent . local_preferences , local_cand . base . base_address , :prflx )
2086
+
2061
2087
cand =
2062
2088
Candidate.Prflx . new (
2063
2089
address: xor_addr . address ,
2064
2090
port: xor_addr . port ,
2065
2091
base_address: local_cand . base . base_address ,
2066
2092
base_port: local_cand . base . base_port ,
2093
+ priority: priority ,
2067
2094
transport_module: ice_agent . transport_module ,
2068
2095
socket: local_cand . base . socket
2069
2096
)
@@ -2808,7 +2835,19 @@ defmodule ExICE.Priv.ICEAgent do
2808
2835
# priority sent to the other side has to be
2809
2836
# computed with the candidate type preference of
2810
2837
# peer-reflexive; refer to sec 7.1.1
2811
- priority = Candidate . priority ( local_candidate . base . base_address , :prflx )
2838
+ priority =
2839
+ if local_candidate . base . type == :relay do
2840
+ { :ok , { sock_addr , _sock_port } } =
2841
+ ice_agent . transport_module . sockname ( local_candidate . base . socket )
2842
+
2843
+ Candidate . priority! ( ice_agent . local_preferences , sock_addr , :prflx )
2844
+ else
2845
+ Candidate . priority! (
2846
+ ice_agent . local_preferences ,
2847
+ local_candidate . base . base_address ,
2848
+ :prflx
2849
+ )
2850
+ end
2812
2851
2813
2852
attrs = [
2814
2853
% Username { value: "#{ ice_agent . remote_ufrag } :#{ ice_agent . local_ufrag } " } ,
0 commit comments