Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop transports on closing PeerConnection #40

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/ex_webrtc/dtls_transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ defmodule ExWebRTC.DTLSTransport do
GenServer.cast(dtls_transport, {:send_rtcp, data})
end

@doc false
@spec stop(dtls_transport()) :: :ok
def stop(dtls_transport) do
GenServer.stop(dtls_transport)
end

@impl true
def init([ice_transport, ice_pid, owner]) do
{pkey, cert} = ExDTLS.generate_key_cert()
Expand Down Expand Up @@ -215,6 +221,11 @@ defmodule ExWebRTC.DTLSTransport do
{:noreply, state}
end

@impl true
def terminate(reason, _state) do
Logger.debug("Stopping DTLSTransport with reason: #{inspect(reason)}")
end

defp handle_ice_data({:data, <<f, _rest::binary>> = data}, state) when f in 20..64 do
# TODO: handle {:connection_closed, _}
case ExDTLS.handle_data(state.dtls, data) do
Expand Down
3 changes: 3 additions & 0 deletions lib/ex_webrtc/ice_transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule ExWebRTC.ICETransport do
@callback restart(pid()) :: :ok
@callback send_data(pid(), binary()) :: :ok
@callback set_remote_credentials(pid(), ufrag :: binary(), pwd :: binary()) :: :ok
@callback stop(pid()) :: :ok
end

defmodule ExWebRTC.DefaultICETransport do
Expand Down Expand Up @@ -41,4 +42,6 @@ defmodule ExWebRTC.DefaultICETransport do
defdelegate send_data(pid, data), to: ICEAgent
@impl true
defdelegate set_remote_credentials(pid, ufrag, pwd), to: ICEAgent
@impl true
defdelegate stop(pid), to: ICEAgent
end
9 changes: 8 additions & 1 deletion lib/ex_webrtc/peer_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ defmodule ExWebRTC.PeerConnection do
end

@impl true
def handle_info({:ex_ice, _from, {:gathering_state_changed, new_gathering_state}}, state) do
def handle_info({:ex_ice, _from, {:gathering_state_change, new_gathering_state}}, state) do
state = %{state | ice_gathering_state: new_gathering_state}
{:noreply, state}
end
Expand Down Expand Up @@ -482,6 +482,13 @@ defmodule ExWebRTC.PeerConnection do
{:noreply, state}
end

@impl true
def terminate(reason, state) do
Logger.debug("Closing peer connection with reason: #{inspect(reason)}")
:ok = DTLSTransport.stop(state.dtls_transport)
:ok = state.ice_transport.stop(state.ice_pid)
end

defp apply_local_description(%SessionDescription{type: type}, _state)
when type in [:rollback, :pranswer],
do: {:error, :not_implemented}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule ExWebRTC.MixProject do
defp deps do
[
{:ex_sdp, "~> 0.14.0"},
{:ex_ice, "~> 0.3.0"},
{:ex_ice, "~> 0.4.0"},
{:ex_dtls, "~> 0.15.0"},
{:ex_libsrtp, "~> 0.7.1"},
{:ex_rtp, "~> 0.2.0"},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"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"},
"ex_dtls": {:hex, :ex_dtls, "0.15.0", "71a11b0379f4ebeb781548ead0e72194e650cfb6d6c435015c8b36e9e5574f62", [:mix], [{:unifex, "~> 1.0", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "3ff1d9ef6c4b0cc800105979c1c58dc67cada91c593beeae56e05094fdbce865"},
"ex_ice": {:hex, :ex_ice, "0.3.0", "1d3139c6f51b88ba1cd9f943bc70da73370c1e035de4edb0ebeb98d107b36ced", [:mix], [{:ex_stun, "~> 0.1.0", [hex: :ex_stun, repo: "hexpm", optional: false]}], "hexpm", "e7af619d33eda64889ccd285c85e7d03fc20b6b5fa9ee54d98daabdf2e6a99a8"},
"ex_ice": {:hex, :ex_ice, "0.4.0", "fdce80373e8bc519be99825b970f7388694bcbe47ad1be99a57bd3b00223a22b", [:mix], [{:ex_stun, "~> 0.1.0", [hex: :ex_stun, repo: "hexpm", optional: false]}], "hexpm", "447a3ba75ebf482b0c07768d942a312e40f047f29d34160b220dd56a59647aa2"},
"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"},
"ex_rtcp": {:hex, :ex_rtcp, "0.1.0", "2e02d23fc6ccc7e00aed13358ffdbcb23e34d3a7f35c66cadaa54447383ecae4", [:mix], [], "hexpm", "1c9a7e636f3950fbcefedce31f3e4ca60b84ea80ad519789f9d215167c60cb2b"},
"ex_rtp": {:hex, :ex_rtp, "0.2.0", "b5a7eb093a2177dd8faa5c2cf82cbd3722bed8676dbff81c3ae3e30a32a8e407", [:mix], [], "hexpm", "a48080edeca99d90aca5a2a8129bc9d46548f1055d957d775d751fb390f9988c"},
Expand Down
8 changes: 8 additions & 0 deletions test/ex_webrtc/dtls_transport_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ defmodule ExWebRTC.DTLSTransportTest do
@impl true
def set_remote_credentials(ice_pid, _ufrag, _pwd), do: ice_pid

@impl true
def stop(ice_pid), do: GenServer.stop(ice_pid)

def send_dtls(ice_pid, data), do: GenServer.cast(ice_pid, {:send_dtls, data})

@impl true
Expand Down Expand Up @@ -187,4 +190,9 @@ defmodule ExWebRTC.DTLSTransportTest do
:ok
end
end

test "stop/1", %{dtls: dtls} do
assert :ok == DTLSTransport.stop(dtls)
assert false == Process.alive?(dtls)
end
end
14 changes: 14 additions & 0 deletions test/ex_webrtc/peer_connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,18 @@ defmodule ExWebRTC.PeerConnectionTest do
:ok = PeerConnection.close(pc1)
:ok = PeerConnection.close(pc2)
end

test "close/1" do
{:ok, pc} = PeerConnection.start()
{:links, links} = Process.info(pc, :links)
assert :ok == PeerConnection.close(pc)
assert false == Process.alive?(pc)
Enum.each(links, fn link -> assert false == Process.alive?(link) end)

{:ok, pc} = PeerConnection.start()
{:links, links} = Process.info(pc, :links)
assert true == Process.exit(pc, :shutdown)
assert false == Process.alive?(pc)
Enum.each(links, fn link -> assert false == Process.alive?(link) end)
end
end
Loading