Skip to content

Commit

Permalink
Handle ExDTLS errors (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 authored Jan 17, 2024
1 parent 9842026 commit afa1851
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
13 changes: 6 additions & 7 deletions examples/echo/example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ defmodule Peer do
ICECandidate,
PeerConnection,
MediaStreamTrack,
SessionDescription,
RTPTransceiver
SessionDescription
}

@ice_servers [
%{urls: "stun:stun.l.google.com:19302"}
]

def start_link() do
GenServer.start_link(__MODULE__, nil)
def start() do
GenServer.start(__MODULE__, nil)
end

@impl true
Expand Down Expand Up @@ -189,12 +188,12 @@ defmodule Peer do
end
end

{:ok, pid} = Peer.start_link()
{:ok, pid} = Peer.start()
ref = Process.monitor(pid)

receive do
{:DOWN, ^ref, _, _, _} ->
Logger.info("Peer process closed. Exiting")
{:DOWN, ^ref, _, _, reason} ->
Logger.info("Peer process closed, reason: #{inspect(reason)}. Exiting")

other ->
Logger.warning("Unexpected msg. Exiting. Msg: #{inspect(other)}")
Expand Down
14 changes: 7 additions & 7 deletions examples/save_to_file/example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ defmodule Peer do
%{urls: "stun:stun.l.google.com:19302"}
]

def start_link() do
GenServer.start_link(__MODULE__, nil)
def start() do
GenServer.start(__MODULE__, nil)
end

@impl true
Expand Down Expand Up @@ -135,9 +135,9 @@ defmodule Peer do

defp handle_ws_message(%{"type" => "peer_left"}, state) do
# in real scenario you should probably close the PeerConnection explicitly
Ogg.Writer.close(state.ogg_writer)
IVF.Writer.close(state.ivf_writer)
Logger.info("Remote peer left. Closing files and exiting.")
if state.ogg_writer, do: Ogg.Writer.close(state.ogg_writer)
if state.ivf_writer, do: IVF.Writer.close(state.ivf_writer)
exit(:normal)
end

Expand Down Expand Up @@ -217,12 +217,12 @@ defmodule Peer do
end
end

{:ok, pid} = Peer.start_link()
{:ok, pid} = Peer.start()
ref = Process.monitor(pid)

receive do
{:DOWN, ^ref, _, _, _} ->
Logger.info("Peer process closed. Exiting")
{:DOWN, ^ref, _, _, reason} ->
Logger.info("Peer process closed, reason: #{inspect(reason)}. Exiting")

other ->
Logger.warning("Unexpected msg. Exiting. Msg: #{inspect(other)}")
Expand Down
4 changes: 2 additions & 2 deletions examples/save_to_file/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<main>
<h1>Elixir WebRTC Save to File Example</h1>
</main>
<button id="button" >Start</button>
<button id="button">Start</button>
<video id="videoPlayer" autoplay muted></video>
<script src="example.js"></script>
</body>

</html>
</html>
10 changes: 5 additions & 5 deletions examples/send_from_file/example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ defmodule Peer do
%{urls: "stun:stun.l.google.com:19302"}
]

def start_link() do
GenServer.start_link(__MODULE__, nil)
def start() do
GenServer.start(__MODULE__, nil)
end

@impl true
Expand Down Expand Up @@ -269,12 +269,12 @@ defmodule Peer do
end
end

{:ok, pid} = Peer.start_link()
{:ok, pid} = Peer.start()
ref = Process.monitor(pid)

receive do
{:DOWN, ^ref, _, _, _} ->
Logger.info("Peer process closed. Exiting")
{:DOWN, ^ref, _, _, reason} ->
Logger.info("Peer process closed, reason: #{inspect(reason)}. Exiting")

other ->
Logger.warning("Unexpected msg. Exiting. Msg: #{inspect(other)}")
Expand Down
33 changes: 22 additions & 11 deletions lib/ex_webrtc/dtls_transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@ defmodule ExWebRTC.DTLSTransport do

@impl true
def handle_info({:ex_ice, _from, {:data, _data} = msg}, state) do
state = handle_ice_data(msg, state)
{:noreply, state}
case handle_ice_data(msg, state) do
{:ok, state} -> {:noreply, state}
# we use shutdown to avoid logging an error
{:error, reason} -> {:stop, {:shutdown, reason}, state}
end
end

@impl true
Expand All @@ -224,12 +227,12 @@ defmodule ExWebRTC.DTLSTransport do
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
{:handshake_packets, packets, timeout} when state.ice_connected ->
:ok = state.ice_transport.send_data(state.ice_pid, packets)
Process.send_after(self(), :dtls_timeout, timeout)
update_dtls_state(state, :connecting)
state = update_dtls_state(state, :connecting)
{:ok, state}

{:handshake_packets, packets, timeout} ->
Logger.debug("""
Expand All @@ -239,7 +242,8 @@ defmodule ExWebRTC.DTLSTransport do

Process.send_after(self(), :dtls_timeout, timeout)
state = %{state | buffered_packets: packets}
update_dtls_state(state, :connecting)
state = update_dtls_state(state, :connecting)
{:ok, state}

{:handshake_finished, lkm, rkm, profile, packets} ->
Logger.debug("DTLS handshake finished")
Expand All @@ -253,19 +257,26 @@ defmodule ExWebRTC.DTLSTransport do

if peer_fingerprint == state.peer_fingerprint do
:ok = setup_srtp(state, lkm, rkm, profile)
update_dtls_state(state, :connected)
state = update_dtls_state(state, :connected)
{:ok, state}
else
Logger.debug("Non-matching peer cert fingerprint.")
update_dtls_state(state, :failed)
state = update_dtls_state(state, :failed)
{:ok, state}
end

{:handshake_finished, lkm, rkm, profile} ->
Logger.debug("DTLS handshake finished")
:ok = setup_srtp(state, lkm, rkm, profile)
update_dtls_state(state, :connected)
state = update_dtls_state(state, :connected)
{:ok, state}

:handshake_want_read ->
state
{:ok, state}

{:error, reason} = error ->
Logger.debug("DTLS error: #{reason}")
error
end
end

Expand All @@ -285,15 +296,15 @@ defmodule ExWebRTC.DTLSTransport do
Logger.error("Failed to decrypt SRTP/SRTCP, reason: #{inspect(reason)}")
end

state
{:ok, state}
end

defp handle_ice_data({:data, _data}, state) do
Logger.warning(
"Received RTP/RTCP packets, but DTLS handshake hasn't been finished yet. Ignoring."
)

state
{:ok, state}
end

defp setup_srtp(state, local_keying_material, remote_keying_material, profile) do
Expand Down

0 comments on commit afa1851

Please sign in to comment.