Skip to content

Commit 2a2e501

Browse files
authored
Merge pull request #6 from membraneframework/ice-restart
Add ICE restart
2 parents 622f91f + 6dafed4 commit 2a2e501

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

c_src/ex_libnice/native.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,20 @@ UNIFEX_TERM set_remote_candidate(UnifexEnv *env, State *state,
256256
return set_remote_candidate_result_ok(env, state);
257257
}
258258

259+
UNIFEX_TERM restart(UnifexEnv *env, State *state) {
260+
if(nice_agent_restart(state->agent)) {
261+
return restart_result_ok(env, state);
262+
}
263+
return restart_result_error_failed_to_restart(env);
264+
}
265+
266+
UNIFEX_TERM restart_stream(UnifexEnv *env, State *state, unsigned int stream_id) {
267+
if(nice_agent_restart_stream(state->agent, stream_id)) {
268+
return restart_stream_result_ok(env, state);
269+
}
270+
return restart_stream_result_error_failed_to_restart(env);
271+
}
272+
259273
UNIFEX_TERM send_payload(UnifexEnv *env, State *state, unsigned int stream_id,
260274
unsigned int component_id, UnifexPayload *payload) {
261275
if(nice_agent_send(state->agent, stream_id, component_id, payload->size, (char *)payload->data) < 0) {

c_src/ex_libnice/native.spec.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ spec set_remote_candidate(state, candidate :: string, stream_id :: unsigned, com
3636
| {:error :: label, :failed_to_parse_sdp_string :: label}
3737
| {:error :: label, :failed_to_set :: label}
3838

39+
spec restart(state) :: {:ok :: label, state}
40+
| {:error :: label, :failed_to_restart :: label}
41+
42+
spec restart_stream(state, stream_id :: unsigned) :: {:ok :: label, state}
43+
| {:error :: label, :failed_to_restart :: label}
44+
3945
spec send_payload(state, stream_id :: unsigned, component_id :: unsigned, data :: payload) :: {:ok :: label, state}
4046
| {:error :: label, :failed_to_send :: label}
4147

lib/ex_libnice.ex

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,22 @@ defmodule ExLibnice do
161161
GenServer.call(pid, {:set_remote_candidate, candidate, stream_id, component_id})
162162
end
163163

164+
@doc """
165+
Restarts all streams.
166+
"""
167+
@spec restart(pid :: pid()) :: :ok | {:error, :failed_to_restart}
168+
def restart(pid) do
169+
GenServer.call(pid, :restart)
170+
end
171+
172+
@doc """
173+
Restarts stream with id `stream_id`.
174+
"""
175+
@spec restart_stream(pid :: pid(), stream_id :: integer()) :: :ok | {:error, :failed_to_restart}
176+
def restart_stream(pid, stream_id) do
177+
GenServer.call(pid, {:restart_stream, stream_id})
178+
end
179+
164180
@doc """
165181
Sends payload on component with id `component_id` in stream with id `stream_id`. Payload has to
166182
be in a binary format.
@@ -317,6 +333,32 @@ defmodule ExLibnice do
317333
end
318334
end
319335

336+
@impl true
337+
def handle_call(:restart, _from, %{cnode: cnode} = state) do
338+
case Unifex.CNode.call(cnode, :restart) do
339+
:ok ->
340+
Logger.debug("ICE restarted")
341+
{:reply, :ok, state}
342+
343+
{:error, cause} ->
344+
Logger.warn("Couldn't restart ICE")
345+
{:reply, {:error, cause}, state}
346+
end
347+
end
348+
349+
@impl true
350+
def handle_call({:restart_stream, stream_id}, _from, %{cnode: cnode} = state) do
351+
case Unifex.CNode.call(cnode, :restart_stream, [stream_id]) do
352+
:ok ->
353+
Logger.debug("Stream #{inspect(stream_id)} restarted")
354+
{:reply, :ok, state}
355+
356+
{:error, cause} ->
357+
Logger.warn("Couldn't restart stream #{inspect(stream_id)}")
358+
{:reply, {:error, cause}, state}
359+
end
360+
end
361+
320362
@impl true
321363
def handle_call(
322364
{:send_payload, stream_id, component_id, payload},

test/ex_libnice_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ defmodule ExLibniceTest do
7474
{:ok, stream_id} = ExLibnice.add_stream(pid, 1)
7575
:ok = ExLibnice.gather_candidates(pid, stream_id)
7676
assert_receive {:new_candidate_full, _candidate}
77-
assert_receive({:candidate_gathering_done, ^stream_id}, 2000)
77+
assert_receive({:candidate_gathering_done, ^stream_id}, 5000)
7878
{:error, :invalid_stream_or_allocation} = ExLibnice.gather_candidates(pid, 2000)
7979
end
8080

0 commit comments

Comments
 (0)