@@ -52,6 +52,9 @@ defmodule ExICE.ICEAgent do
5252 All notifications are by default sent to a process that spawns `ExICE`.
5353 This behavior can be overwritten using the following options.
5454
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`.
5558 * `ip_filter` - filter applied when gathering host candidates
5659 * `ports` - ports that will be used when gathering host candidates, otherwise the ports are chosen by the OS
5760 * `ice_servers` - list of STUN/TURN servers
@@ -64,6 +67,7 @@ defmodule ExICE.ICEAgent do
6467 * `on_new_candidate` - where to send new candidates. Defaults to a process that spawns `ExICE`.
6568 """
6669 @ type opts ( ) :: [
70+ role: role ( ) | nil ,
6771 ip_filter: ip_filter ( ) ,
6872 ports: Enumerable . t ( non_neg_integer ( ) ) ,
6973 ice_servers: [
@@ -86,9 +90,9 @@ defmodule ExICE.ICEAgent do
8690 Process calling this function is called a `controlling process` and
8791 has to be prepared for receiving ExICE messages described by `t:signal/0`.
8892 """
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 ( ) ] )
9296 end
9397
9498 @ doc """
@@ -123,6 +127,14 @@ defmodule ExICE.ICEAgent do
123127 GenServer . call ( ice_agent , { :on_new_candidate , send_to } )
124128 end
125129
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+
126138 @ doc """
127139 Gets local credentials.
128140
@@ -152,6 +164,20 @@ defmodule ExICE.ICEAgent do
152164 GenServer . call ( ice_agent , :get_remote_candidates )
153165 end
154166
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+
155181 @ doc """
156182 Sets remote credentials.
157183
@@ -283,6 +309,12 @@ defmodule ExICE.ICEAgent do
283309 { :reply , :ok , % { state | ice_agent: ice_agent } }
284310 end
285311
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+
286318 @ impl true
287319 def handle_call ( :get_local_credentials , _from , state ) do
288320 { local_ufrag , local_pwd } = ExICE.Priv.ICEAgent . get_local_credentials ( state . ice_agent )
@@ -307,6 +339,12 @@ defmodule ExICE.ICEAgent do
307339 { :reply , stats , state }
308340 end
309341
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+
310348 @ impl true
311349 def handle_cast ( { :set_remote_credentials , ufrag , pwd } , state ) do
312350 ice_agent = ExICE.Priv.ICEAgent . set_remote_credentials ( state . ice_agent , ufrag , pwd )
0 commit comments