Skip to content

Commit

Permalink
Add ICECandidate.to_json/1 and ICECandidate.from_json/1 (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 authored Feb 1, 2024
1 parent bf45f20 commit c0754f8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 50 deletions.
19 changes: 2 additions & 17 deletions examples/echo/example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
18 changes: 2 additions & 16 deletions examples/save_to_file/example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
19 changes: 2 additions & 17 deletions examples/send_from_file/example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions lib/ex_webrtc/ice_candidate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c0754f8

Please sign in to comment.