From aa4c927e8d3d60e3e4fb14e204d5c7801cf63970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=C5=9Aled=C5=BA?= Date: Tue, 28 Nov 2023 10:51:29 +0100 Subject: [PATCH] Revert "Convert PeerConnection internal state into map (#24)" This reverts commit 66e9c6756f0fbadea64a1874717e91937c9a74d3. --- lib/ex_webrtc/peer_connection.ex | 41 +++++++++++++++++++------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/ex_webrtc/peer_connection.ex b/lib/ex_webrtc/peer_connection.ex index 2eeee569..2aeb7854 100644 --- a/lib/ex_webrtc/peer_connection.ex +++ b/lib/ex_webrtc/peer_connection.ex @@ -45,6 +45,26 @@ defmodule ExWebRTC.PeerConnection do """ @type connection_state() :: :closed | :failed | :disconnected | :new | :connecting | :connected + @enforce_keys [:config, :owner] + defstruct @enforce_keys ++ + [ + :current_local_desc, + :pending_local_desc, + :current_remote_desc, + :pending_remote_desc, + :ice_agent, + :dtls_transport, + demuxer: %Demuxer{}, + transceivers: [], + ice_state: nil, + dtls_state: nil, + signaling_state: :stable, + conn_state: :new, + last_offer: nil, + last_answer: nil, + peer_fingerprint: nil + ] + #### API #### @spec start_link(Configuration.options()) :: GenServer.on_start() def start_link(options \\ []) do @@ -112,24 +132,13 @@ defmodule ExWebRTC.PeerConnection do {:ok, dtls_transport} = DTLSTransport.start_link(ice_config) ice_agent = DTLSTransport.get_ice_agent(dtls_transport) - state = %{ + state = %__MODULE__{ owner: owner, config: config, - current_local_desc: nil, - pending_local_desc: nil, - current_remote_desc: nil, - pending_remote_desc: nil, ice_agent: ice_agent, dtls_transport: dtls_transport, - demuxer: %Demuxer{}, - transceivers: [], ice_state: :new, - dtls_state: :new, - signaling_state: :stable, - conn_state: :new, - last_offer: nil, - last_answer: nil, - peer_fingerprint: nil + dtls_state: :new } notify(state.owner, {:connection_state_change, :new}) @@ -340,7 +349,7 @@ defmodule ExWebRTC.PeerConnection do @impl true def handle_info({:ex_ice, _from, {:connection_state_change, new_ice_state}}, state) do - state = %{state | ice_state: new_ice_state} + state = %__MODULE__{state | ice_state: new_ice_state} next_conn_state = next_conn_state(new_ice_state, state.dtls_state) state = update_conn_state(state, next_conn_state) @@ -368,7 +377,7 @@ defmodule ExWebRTC.PeerConnection do @impl true def handle_info({:dtls_transport, _pid, {:state_change, new_dtls_state}}, state) do - state = %{state | dtls_state: new_dtls_state} + state = %__MODULE__{state | dtls_state: new_dtls_state} next_conn_state = next_conn_state(state.ice_state, new_dtls_state) state = update_conn_state(state, next_conn_state) {:noreply, state} @@ -379,7 +388,7 @@ defmodule ExWebRTC.PeerConnection do case Demuxer.demux(state.demuxer, data) do {:ok, demuxer, mid, packet} -> notify(state.owner, {:data, {mid, packet}}) - {:noreply, %{state | demuxer: demuxer}} + {:noreply, %__MODULE__{state | demuxer: demuxer}} {:error, reason} -> Logger.error("Unable to demux RTP, reason: #{inspect(reason)}")