Skip to content

Commit b559388

Browse files
authored
Stop transports on closing PeerConnection (#40)
1 parent af29961 commit b559388

File tree

7 files changed

+46
-3
lines changed

7 files changed

+46
-3
lines changed

lib/ex_webrtc/dtls_transport.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ defmodule ExWebRTC.DTLSTransport do
7272
GenServer.cast(dtls_transport, {:send_rtcp, data})
7373
end
7474

75+
@doc false
76+
@spec stop(dtls_transport()) :: :ok
77+
def stop(dtls_transport) do
78+
GenServer.stop(dtls_transport)
79+
end
80+
7581
@impl true
7682
def init([ice_transport, ice_pid, owner]) do
7783
{pkey, cert} = ExDTLS.generate_key_cert()
@@ -215,6 +221,11 @@ defmodule ExWebRTC.DTLSTransport do
215221
{:noreply, state}
216222
end
217223

224+
@impl true
225+
def terminate(reason, _state) do
226+
Logger.debug("Stopping DTLSTransport with reason: #{inspect(reason)}")
227+
end
228+
218229
defp handle_ice_data({:data, <<f, _rest::binary>> = data}, state) when f in 20..64 do
219230
# TODO: handle {:connection_closed, _}
220231
case ExDTLS.handle_data(state.dtls, data) do

lib/ex_webrtc/ice_transport.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule ExWebRTC.ICETransport do
1414
@callback restart(pid()) :: :ok
1515
@callback send_data(pid(), binary()) :: :ok
1616
@callback set_remote_credentials(pid(), ufrag :: binary(), pwd :: binary()) :: :ok
17+
@callback stop(pid()) :: :ok
1718
end
1819

1920
defmodule ExWebRTC.DefaultICETransport do
@@ -41,4 +42,6 @@ defmodule ExWebRTC.DefaultICETransport do
4142
defdelegate send_data(pid, data), to: ICEAgent
4243
@impl true
4344
defdelegate set_remote_credentials(pid, ufrag, pwd), to: ICEAgent
45+
@impl true
46+
defdelegate stop(pid), to: ICEAgent
4447
end

lib/ex_webrtc/peer_connection.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ defmodule ExWebRTC.PeerConnection do
431431
end
432432

433433
@impl true
434-
def handle_info({:ex_ice, _from, {:gathering_state_changed, new_gathering_state}}, state) do
434+
def handle_info({:ex_ice, _from, {:gathering_state_change, new_gathering_state}}, state) do
435435
state = %{state | ice_gathering_state: new_gathering_state}
436436
{:noreply, state}
437437
end
@@ -482,6 +482,13 @@ defmodule ExWebRTC.PeerConnection do
482482
{:noreply, state}
483483
end
484484

485+
@impl true
486+
def terminate(reason, state) do
487+
Logger.debug("Closing peer connection with reason: #{inspect(reason)}")
488+
:ok = DTLSTransport.stop(state.dtls_transport)
489+
:ok = state.ice_transport.stop(state.ice_pid)
490+
end
491+
485492
defp apply_local_description(%SessionDescription{type: type}, _state)
486493
when type in [:rollback, :pranswer],
487494
do: {:error, :not_implemented}

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ defmodule ExWebRTC.MixProject do
4646
defp deps do
4747
[
4848
{:ex_sdp, "~> 0.14.0"},
49-
{:ex_ice, "~> 0.3.0"},
49+
{:ex_ice, "~> 0.4.0"},
5050
{:ex_dtls, "~> 0.15.0"},
5151
{:ex_libsrtp, "~> 0.7.1"},
5252
{:ex_rtp, "~> 0.2.0"},

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
1212
"ex_doc": {:hex, :ex_doc, "0.30.9", "d691453495c47434c0f2052b08dd91cc32bc4e1a218f86884563448ee2502dd2", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "d7aaaf21e95dc5cddabf89063327e96867d00013963eadf2c6ad135506a8bc10"},
1313
"ex_dtls": {:hex, :ex_dtls, "0.15.0", "71a11b0379f4ebeb781548ead0e72194e650cfb6d6c435015c8b36e9e5574f62", [:mix], [{:unifex, "~> 1.0", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "3ff1d9ef6c4b0cc800105979c1c58dc67cada91c593beeae56e05094fdbce865"},
14-
"ex_ice": {:hex, :ex_ice, "0.3.0", "1d3139c6f51b88ba1cd9f943bc70da73370c1e035de4edb0ebeb98d107b36ced", [:mix], [{:ex_stun, "~> 0.1.0", [hex: :ex_stun, repo: "hexpm", optional: false]}], "hexpm", "e7af619d33eda64889ccd285c85e7d03fc20b6b5fa9ee54d98daabdf2e6a99a8"},
14+
"ex_ice": {:hex, :ex_ice, "0.4.0", "fdce80373e8bc519be99825b970f7388694bcbe47ad1be99a57bd3b00223a22b", [:mix], [{:ex_stun, "~> 0.1.0", [hex: :ex_stun, repo: "hexpm", optional: false]}], "hexpm", "447a3ba75ebf482b0c07768d942a312e40f047f29d34160b220dd56a59647aa2"},
1515
"ex_libsrtp": {:hex, :ex_libsrtp, "0.7.2", "211bd89c08026943ce71f3e2c0231795b99cee748808ed3ae7b97cd8d2450b6b", [:mix], [{:bunch, "~> 1.6", [hex: :bunch, repo: "hexpm", optional: false]}, {:bundlex, "~> 1.3", [hex: :bundlex, repo: "hexpm", optional: false]}, {:membrane_precompiled_dependency_provider, "~> 0.1.0", [hex: :membrane_precompiled_dependency_provider, repo: "hexpm", optional: false]}, {:unifex, "~> 1.1", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "2e20645d0d739a4ecdcf8d4810a0c198120c8a2f617f2b75b2e2e704d59f492a"},
1616
"ex_rtcp": {:hex, :ex_rtcp, "0.1.0", "2e02d23fc6ccc7e00aed13358ffdbcb23e34d3a7f35c66cadaa54447383ecae4", [:mix], [], "hexpm", "1c9a7e636f3950fbcefedce31f3e4ca60b84ea80ad519789f9d215167c60cb2b"},
1717
"ex_rtp": {:hex, :ex_rtp, "0.2.0", "b5a7eb093a2177dd8faa5c2cf82cbd3722bed8676dbff81c3ae3e30a32a8e407", [:mix], [], "hexpm", "a48080edeca99d90aca5a2a8129bc9d46548f1055d957d775d751fb390f9988c"},

test/ex_webrtc/dtls_transport_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ defmodule ExWebRTC.DTLSTransportTest do
4141
@impl true
4242
def set_remote_credentials(ice_pid, _ufrag, _pwd), do: ice_pid
4343

44+
@impl true
45+
def stop(ice_pid), do: GenServer.stop(ice_pid)
46+
4447
def send_dtls(ice_pid, data), do: GenServer.cast(ice_pid, {:send_dtls, data})
4548

4649
@impl true
@@ -187,4 +190,9 @@ defmodule ExWebRTC.DTLSTransportTest do
187190
:ok
188191
end
189192
end
193+
194+
test "stop/1", %{dtls: dtls} do
195+
assert :ok == DTLSTransport.stop(dtls)
196+
assert false == Process.alive?(dtls)
197+
end
190198
end

test/ex_webrtc/peer_connection_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,4 +527,18 @@ defmodule ExWebRTC.PeerConnectionTest do
527527
:ok = PeerConnection.close(pc1)
528528
:ok = PeerConnection.close(pc2)
529529
end
530+
531+
test "close/1" do
532+
{:ok, pc} = PeerConnection.start()
533+
{:links, links} = Process.info(pc, :links)
534+
assert :ok == PeerConnection.close(pc)
535+
assert false == Process.alive?(pc)
536+
Enum.each(links, fn link -> assert false == Process.alive?(link) end)
537+
538+
{:ok, pc} = PeerConnection.start()
539+
{:links, links} = Process.info(pc, :links)
540+
assert true == Process.exit(pc, :shutdown)
541+
assert false == Process.alive?(pc)
542+
Enum.each(links, fn link -> assert false == Process.alive?(link) end)
543+
end
530544
end

0 commit comments

Comments
 (0)