@@ -52,6 +52,9 @@ defmodule ExICE.ICEAgent do
52
52
All notifications are by default sent to a process that spawns `ExICE`.
53
53
This behavior can be overwritten using the following options.
54
54
55
+ * `role` - agent's role. If not set, it can be later choosen with `set_role/2`. Please note, that
56
+ until role is set, adding remote candidates or gathering local candidates won't possible, and calls to these
57
+ functions will be ignored. Defaults to `nil`.
55
58
* `ip_filter` - filter applied when gathering host candidates
56
59
* `ports` - ports that will be used when gathering host candidates, otherwise the ports are chosen by the OS
57
60
* `ice_servers` - list of STUN/TURN servers
@@ -64,6 +67,7 @@ defmodule ExICE.ICEAgent do
64
67
* `on_new_candidate` - where to send new candidates. Defaults to a process that spawns `ExICE`.
65
68
"""
66
69
@ type opts ( ) :: [
70
+ role: role ( ) | nil ,
67
71
ip_filter: ip_filter ( ) ,
68
72
ports: Enumerable . t ( non_neg_integer ( ) ) ,
69
73
ice_servers: [
@@ -86,9 +90,9 @@ defmodule ExICE.ICEAgent do
86
90
Process calling this function is called a `controlling process` and
87
91
has to be prepared for receiving ExICE messages described by `t:signal/0`.
88
92
"""
89
- @ spec start_link ( role ( ) , opts ( ) ) :: GenServer . on_start ( )
90
- def start_link ( role , opts \\ [ ] ) do
91
- GenServer . start_link ( __MODULE__ , opts ++ [ role: role , controlling_process: self ( ) ] )
93
+ @ spec start_link ( opts ( ) ) :: GenServer . on_start ( )
94
+ def start_link ( opts \\ [ ] ) when is_list ( opts ) do
95
+ GenServer . start_link ( __MODULE__ , opts ++ [ controlling_process: self ( ) ] )
92
96
end
93
97
94
98
@ doc """
@@ -123,6 +127,14 @@ defmodule ExICE.ICEAgent do
123
127
GenServer . call ( ice_agent , { :on_new_candidate , send_to } )
124
128
end
125
129
130
+ @ doc """
131
+ Gets agent's role.
132
+ """
133
+ @ spec get_role ( pid ( ) ) :: ExICE.Agent . t ( ) | nil
134
+ def get_role ( ice_agent ) do
135
+ GenServer . call ( ice_agent , :get_role )
136
+ end
137
+
126
138
@ doc """
127
139
Gets local credentials.
128
140
@@ -152,6 +164,20 @@ defmodule ExICE.ICEAgent do
152
164
GenServer . call ( ice_agent , :get_remote_candidates )
153
165
end
154
166
167
+ @ doc """
168
+ Sets agent's role.
169
+
170
+ In case of WebRTC, agent's role depends on who sends the first offer.
171
+ Since an agent has to be initialized at the very beginning, there is no
172
+ possibility to set its role in the constructor.
173
+
174
+ This function can only be called once. Subsequent calls will be ignored.
175
+ """
176
+ @ spec set_role ( pid ( ) , role ( ) ) :: :ok
177
+ def set_role ( ice_agent , role ) do
178
+ GenServer . cast ( ice_agent , { :set_role , role } )
179
+ end
180
+
155
181
@ doc """
156
182
Sets remote credentials.
157
183
@@ -283,6 +309,12 @@ defmodule ExICE.ICEAgent do
283
309
{ :reply , :ok , % { state | ice_agent: ice_agent } }
284
310
end
285
311
312
+ @ impl true
313
+ def handle_call ( :get_role , _from , state ) do
314
+ role = ExICE.Priv.ICEAgent . get_role ( state . ice_agent )
315
+ { :reply , role , state }
316
+ end
317
+
286
318
@ impl true
287
319
def handle_call ( :get_local_credentials , _from , state ) do
288
320
{ local_ufrag , local_pwd } = ExICE.Priv.ICEAgent . get_local_credentials ( state . ice_agent )
@@ -307,6 +339,12 @@ defmodule ExICE.ICEAgent do
307
339
{ :reply , stats , state }
308
340
end
309
341
342
+ @ impl true
343
+ def handle_cast ( { :set_role , role } , state ) do
344
+ ice_agent = ExICE.Priv.ICEAgent . set_role ( state . ice_agent , role )
345
+ { :noreply , % { state | ice_agent: ice_agent } }
346
+ end
347
+
310
348
@ impl true
311
349
def handle_cast ( { :set_remote_credentials , ufrag , pwd } , state ) do
312
350
ice_agent = ExICE.Priv.ICEAgent . set_remote_credentials ( state . ice_agent , ufrag , pwd )
0 commit comments