From 2dd32ca7cf1795eecbbb0b43c5ce704193ffae20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Fri, 3 Nov 2023 12:13:50 +0100 Subject: [PATCH] Simplify start of DTLS handshake --- lib/ex_webrtc/dtls_transport.ex | 22 +++++----------------- lib/ex_webrtc/peer_connection.ex | 5 +++++ 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/ex_webrtc/dtls_transport.ex b/lib/ex_webrtc/dtls_transport.ex index 546b9392..726357b7 100644 --- a/lib/ex_webrtc/dtls_transport.ex +++ b/lib/ex_webrtc/dtls_transport.ex @@ -46,7 +46,7 @@ defmodule ExWebRTC.DTLSTransport do %__MODULE__{dtls | client: client} end - def start(%{ice_state: ice_state} = dtls, :active) do + def start(dtls, :active) do {:ok, client} = ExDTLS.start_link( client_mode: true, @@ -55,22 +55,15 @@ defmodule ExWebRTC.DTLSTransport do cert: dtls.cert ) - dtls = %__MODULE__{dtls | client: client} - - case ice_state do - state when state in [:active, :connected] -> - start_handshake(dtls) - dtls - - _other -> - %__MODULE__{dtls | should_start: true} - end + # we assume that ICE in not in connected state yet + %__MODULE__{dtls | client: client, should_start: true} end def update_ice_state(dtls, :connected) do dtls = if dtls.should_start do - start_handshake(dtls) + {:ok, packets} = ExDTLS.do_handshake(dtls.client) + :ok = ICEAgent.send_data(dtls.ice_agent, packets) %__MODULE__{dtls | should_start: false} else dtls @@ -132,9 +125,4 @@ defmodule ExWebRTC.DTLSTransport do dtls end end - - defp start_handshake(dtls) do - {:ok, packets} = ExDTLS.do_handshake(dtls.client) - :ok = ICEAgent.send_data(dtls.ice_agent, packets) - end end diff --git a/lib/ex_webrtc/peer_connection.ex b/lib/ex_webrtc/peer_connection.ex index ec2a2528..60fb1d55 100644 --- a/lib/ex_webrtc/peer_connection.ex +++ b/lib/ex_webrtc/peer_connection.ex @@ -283,6 +283,11 @@ defmodule ExWebRTC.PeerConnection do end end + @impl true + def handle_call({:add_ice_candidate, _}, _from, %{current_remote_desc: nil} = state) do + {:reply, {:error, :no_remote_description}, state} + end + @impl true def handle_call({:add_ice_candidate, candidate}, _from, state) do with "candidate:" <> attr <- candidate.candidate do