From c0754f895b2eed0c36978eb16118711490c33885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=C5=9Aled=C5=BA?= Date: Thu, 1 Feb 2024 11:16:06 +0100 Subject: [PATCH] Add `ICECandidate.to_json/1` and `ICECandidate.from_json/1` (#62) --- examples/echo/example.exs | 19 ++----------------- examples/save_to_file/example.exs | 18 ++---------------- examples/send_from_file/example.exs | 19 ++----------------- lib/ex_webrtc/ice_candidate.ex | 25 +++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 50 deletions(-) diff --git a/examples/echo/example.exs b/examples/echo/example.exs index ceb24c7b..dedf913c 100644 --- a/examples/echo/example.exs +++ b/examples/echo/example.exs @@ -127,16 +127,8 @@ defmodule Peer do defp handle_ws_message(%{"type" => "ice", "data" => data}, state) do Logger.info("Received remote ICE candidate: #{inspect(data)}") - - candidate = %ICECandidate{ - candidate: data["candidate"], - sdp_mid: data["sdpMid"], - sdp_m_line_index: data["sdpMLineIndex"], - username_fragment: data["usernameFragment"] - } - + candidate = ICECandidate.from_json(data) :ok = PeerConnection.add_ice_candidate(state.peer_connection, candidate) - state end @@ -146,14 +138,7 @@ defmodule Peer do end defp handle_webrtc_message({:ice_candidate, candidate}, state) do - candidate = %{ - "candidate" => candidate.candidate, - "sdpMid" => candidate.sdp_mid, - "sdpMLineIndex" => candidate.sdp_m_line_index, - "usernameFragment" => candidate.username_fragment - } - - msg = %{"type" => "ice", "data" => candidate} + msg = %{"type" => "ice", "data" => ICECandidate.to_json(candidate)) :gun.ws_send(state.conn, state.stream, {:text, Jason.encode!(msg)}) state end diff --git a/examples/save_to_file/example.exs b/examples/save_to_file/example.exs index 92e2b1ae..78bcd973 100644 --- a/examples/save_to_file/example.exs +++ b/examples/save_to_file/example.exs @@ -122,14 +122,7 @@ defmodule Peer do defp handle_ws_message(%{"type" => "ice", "data" => data}, state) do Logger.info("Received remote ICE candidate: #{inspect(data)}") - - candidate = %ICECandidate{ - candidate: data["candidate"], - sdp_mid: data["sdpMid"], - sdp_m_line_index: data["sdpMLineIndex"], - username_fragment: data["usernameFragment"] - } - + candidate = ICECandidate.from_json(data) :ok = PeerConnection.add_ice_candidate(state.peer_connection, candidate) end @@ -146,14 +139,7 @@ defmodule Peer do end defp handle_webrtc_message({:ice_candidate, candidate}, state) do - candidate = %{ - "candidate" => candidate.candidate, - "sdpMid" => candidate.sdp_mid, - "sdpMLineIndex" => candidate.sdp_m_line_index, - "usernameFragment" => candidate.username_fragment - } - - msg = %{"type" => "ice", "data" => candidate} + msg = %{"type" => "ice", "data" => ICECandidate.to_json(candidate)} :gun.ws_send(state.conn, state.stream, {:text, Jason.encode!(msg)}) state end diff --git a/examples/send_from_file/example.exs b/examples/send_from_file/example.exs index da124bdf..db62e117 100644 --- a/examples/send_from_file/example.exs +++ b/examples/send_from_file/example.exs @@ -219,16 +219,8 @@ defmodule Peer do defp handle_ws_message(%{"type" => "ice", "data" => data}, state) do Logger.info("Received remote ICE candidate: #{inspect(data)}") - - candidate = %ICECandidate{ - candidate: data["candidate"], - sdp_mid: data["sdpMid"], - sdp_m_line_index: data["sdpMLineIndex"], - username_fragment: data["usernameFragment"] - } - + candidate = ICECandidate.from_json(data) :ok = PeerConnection.add_ice_candidate(state.peer_connection, candidate) - state end @@ -238,14 +230,7 @@ defmodule Peer do end defp handle_webrtc_message({:ice_candidate, candidate}, state) do - candidate = %{ - "candidate" => candidate.candidate, - "sdpMid" => candidate.sdp_mid, - "sdpMLineIndex" => candidate.sdp_m_line_index, - "usernameFragment" => candidate.username_fragment - } - - msg = %{"type" => "ice", "data" => candidate} + msg = %{"type" => "ice", "data" => ICECandidate.to_json(candidate)} :gun.ws_send(state.conn, state.stream, {:text, Jason.encode!(msg)}) state end diff --git a/lib/ex_webrtc/ice_candidate.ex b/lib/ex_webrtc/ice_candidate.ex index 47c2facd..068ba118 100644 --- a/lib/ex_webrtc/ice_candidate.ex +++ b/lib/ex_webrtc/ice_candidate.ex @@ -11,4 +11,29 @@ defmodule ExWebRTC.ICECandidate do } defstruct [:candidate, :username_fragment, :sdp_mid, :sdp_m_line_index] + + @spec to_json(t()) :: %{String.t() => String.t()} + def to_json(%__MODULE__{} = c) do + %{ + "candidate" => c.candidate, + "sdpMid" => c.sdp_mid, + "sdpMLineIndex" => c.sdp_m_line_index, + "usernameFragment" => c.username_fragment + } + end + + @spec from_json(%{String.t() => String.t()}) :: t() + def from_json(%{ + "candidate" => c, + "sdpMid" => mid, + "sdpMLineIndex" => mline_idx, + "usernameFragment" => ufrag + }) do + %__MODULE__{ + candidate: c, + sdp_mid: mid, + sdp_m_line_index: mline_idx, + username_fragment: ufrag + } + end end