Skip to content

Commit

Permalink
Ensure ICE credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Oct 24, 2023
1 parent ec1c9f7 commit 0af46c1
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 53 deletions.
12 changes: 3 additions & 9 deletions lib/ex_webrtc/peer_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,11 @@ defmodule ExWebRTC.PeerConnection do

defp apply_remote_description(_type, sdp, state) do
# TODO apply steps listed in RFC 8829 5.10
media = hd(sdp.media)

with :ok <- SDPUtils.ensure_mid(sdp),
:ok <- SDPUtils.ensure_bundle(sdp),
{:ice_ufrag, {:ice_ufrag, ufrag}} <-
{:ice_ufrag, ExSDP.Media.get_attribute(media, :ice_ufrag)},
{:ice_pwd, {:ice_pwd, pwd}} <- {:ice_pwd, ExSDP.Media.get_attribute(media, :ice_pwd)},
{:ok, {ice_ufrag, ice_pwd}} <- SDPUtils.get_ice_credentials(sdp),
{:ok, new_transceivers} <- update_transceivers(state.transceivers, sdp) do
:ok = ICEAgent.set_remote_credentials(state.ice_agent, ufrag, pwd)
:ok = ICEAgent.set_remote_credentials(state.ice_agent, ice_ufrag, ice_pwd)
:ok = ICEAgent.gather_candidates(state.ice_agent)

new_remote_tracks =
Expand All @@ -306,9 +302,7 @@ defmodule ExWebRTC.PeerConnection do

{:ok, %{state | current_remote_desc: sdp, transceivers: new_transceivers}}
else
{:ice_ufrag, nil} -> {:error, :missing_ice_ufrag}
{:ice_pwd, nil} -> {:error, :missing_ice_pwd}
other -> other
error -> error
end
end

Expand Down
87 changes: 87 additions & 0 deletions lib/ex_webrtc/sdp_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,91 @@ defmodule ExWebRTC.SDPUtils do
{:error, :multiple_bundle_groups}
end
end

@spec get_ice_credentials(ExSDP.t()) ::
{:ok, {binary(), binary()}}
| {:error,
:missing_ice_pwd
| :missing_ice_ufrag
| :missing_ice_credentials
| :conflicting_ice_credentials}
def get_ice_credentials(sdp) do
session_creds = do_get_ice_credentials(sdp)
mline_creds = Enum.map(sdp.media, fn mline -> do_get_ice_credentials(mline) end)

case {session_creds, mline_creds} do
{{nil, nil}, []} ->

Check warning on line 72 in lib/ex_webrtc/sdp_utils.ex

View check run for this annotation

Codecov / codecov/patch

lib/ex_webrtc/sdp_utils.ex#L72

Added line #L72 was not covered by tests
{:error, :missing_ice_credentials}

{session_creds, []} ->

Check warning on line 75 in lib/ex_webrtc/sdp_utils.ex

View check run for this annotation

Codecov / codecov/patch

lib/ex_webrtc/sdp_utils.ex#L75

Added line #L75 was not covered by tests
{:ok, session_creds}

{{nil, nil}, mline_creds} ->
with :ok <- ensure_ice_credentials_present(mline_creds),
:ok <- ensure_ice_credentials_unique(mline_creds) do
{:ok, List.first(mline_creds)}
end

{{s_ufrag, s_pwd} = session_creds, mline_creds} ->
creds =
Enum.map([session_creds | mline_creds], fn
{nil, nil} -> session_creds
{nil, pwd} -> {s_ufrag, pwd}
{ufrag, nil} -> {ufrag, s_pwd}
other -> other
end)

case ensure_ice_credentials_unique(creds) do
:ok -> {:ok, List.first(creds)}
error -> error

Check warning on line 95 in lib/ex_webrtc/sdp_utils.ex

View check run for this annotation

Codecov / codecov/patch

lib/ex_webrtc/sdp_utils.ex#L95

Added line #L95 was not covered by tests
end
end
end

defp do_get_ice_credentials(sdp_or_mline) do
get_attr =
case sdp_or_mline do
%ExSDP{} -> &ExSDP.get_attribute/2
%ExSDP.Media{} -> &ExSDP.Media.get_attribute/2
end

ice_ufrag =
case get_attr.(sdp_or_mline, :ice_ufrag) do
{:ice_ufrag, ice_ufrag} -> ice_ufrag
nil -> nil
end

ice_pwd =
case get_attr.(sdp_or_mline, :ice_pwd) do
{:ice_pwd, ice_pwd} -> ice_pwd
nil -> nil
end

{ice_ufrag, ice_pwd}
end

defp ensure_ice_credentials_present(creds) do
creds
|> Enum.find(fn {ice_ufrag, ice_pwd} -> ice_ufrag == nil or ice_pwd == nil end)
|> case do
{nil, nil} ->
{:error, :missing_ice_credentials}

{nil, _} ->
{:error, :missing_ice_ufrag}

{_, nil} ->
{:error, :missing_ice_pwd}

nil ->
:ok
end
end

defp ensure_ice_credentials_unique(creds) do
case Enum.uniq(creds) do
[_] -> :ok
_ -> {:error, :conflicting_ice_credentials}
end
end
end
3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ defmodule ExWebRTC.MixProject do

defp deps do
[
# {:ex_sdp, "~> 0.11"},
{:ex_sdp, github: "membraneframework/ex_sdp", branch: "get-attr"},
{:ex_sdp, "~> 0.13"},
{:ex_ice, "~> 0.1"},
{:ex_dtls, "~> 0.13"},

Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"ex_doc": {:hex, :ex_doc, "0.30.6", "5f8b54854b240a2b55c9734c4b1d0dd7bdd41f71a095d42a70445c03cf05a281", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bd48f2ddacf4e482c727f9293d9498e0881597eae6ddc3d9562bd7923375109f"},
"ex_dtls": {:hex, :ex_dtls, "0.13.0", "4d7631eefc19a8820d4f79883f379ff2ad642976bda55493d4ec4e5d10d6c078", [:mix], [{:unifex, "~> 1.0", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "3ece30967006ec12a4088e60514cb08847814fba8b8a21aca3862e5d1fd4a6bc"},
"ex_ice": {:hex, :ex_ice, "0.1.0", "2653c884872d8769cf9fc655c74002a63ed6c21be1b3c2badfa42bdc74de2355", [:mix], [{:ex_stun, "~> 0.1.0", [hex: :ex_stun, repo: "hexpm", optional: false]}], "hexpm", "e2539a321f87f31997ba974d532d00511e5828f2f113b550b1ef6aa799dd2ffe"},
"ex_sdp": {:git, "https://github.com/membraneframework/ex_sdp.git", "2d8dc9e2c964ed2d883a21d88547e9c5aaf0df1a", [branch: "get-attr"]},
"ex_sdp": {:hex, :ex_sdp, "0.13.0", "b464cf5f6b70433159be243115857599f82b07234ee022997868c85ae1f225f7", [:mix], [{:bunch, "~> 1.3", [hex: :bunch, repo: "hexpm", optional: false]}, {:uuid, "~> 1.1", [hex: :uuid, repo: "hexpm", optional: false]}], "hexpm", "975ca4d274240c51ee85909bc0618bd4dd940e69f7d8c8f0d701f1524eaffaad"},
"ex_stun": {:hex, :ex_stun, "0.1.0", "252474bf4c8519fbf4bc0fbfc6a1b846a634b1478c65dbbfb4b6ab4e33c2a95a", [:mix], [], "hexpm", "629fc8be45b624a92522f81d85ba001877b1f0745889a2419bdb678790d7480c"},
"excoveralls": {:hex, :excoveralls, "0.17.1", "83fa7906ef23aa7fc8ad7ee469c357a63b1b3d55dd701ff5b9ce1f72442b2874", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "95bc6fda953e84c60f14da4a198880336205464e75383ec0f570180567985ae0"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
Expand Down
163 changes: 122 additions & 41 deletions test/peer_connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ defmodule ExWebRTC.PeerConnectionTest do
a=ssrc:184440407 msid:- 1259ea70-c6b7-445a-9c20-49cec7433ccb
"""

@audio_mline ExSDP.Media.new("audio", 9, "UDP/TLS/RTP/SAVPF", [108])
|> ExSDP.Media.add_attributes(mid: "0", ice_ufrag: "someufrag", ice_pwd: "somepwd")

@video_mline ExSDP.Media.new("video", 9, "UDP/TLS/RTP/SAVPF", [96])
|> ExSDP.Media.add_attributes(mid: "1", ice_ufrag: "someufrag", ice_pwd: "somepwd")

test "track notification" do
{:ok, pc} = PeerConnection.start_link()

Expand All @@ -96,46 +102,121 @@ defmodule ExWebRTC.PeerConnectionTest do
refute_receive {:ex_webrtc, ^pc, {:track, %MediaStreamTrack{}}}
end

test "set_remote_description/2" do
{:ok, pc} = PeerConnection.start_link()

raw_sdp = ExSDP.new()

audio_mline =
ExSDP.Media.new("audio", 9, "UDP/TLS/RTP/SAVPF", [108])
|> ExSDP.Media.add_attributes(mid: "0", ice_ufrag: "someufrag", ice_pwd: "somepwd")

video_mline =
ExSDP.Media.new("video", 9, "UDP/TLS/RTP/SAVPF", [96])
|> ExSDP.Media.add_attributes(mid: "1", ice_ufrag: "someufrag", ice_pwd: "somepwd")

sdp = ExSDP.add_media(raw_sdp, audio_mline) |> to_string()
offer = %SessionDescription{type: :offer, sdp: sdp}
assert {:error, :missing_bundle_group} = PeerConnection.set_remote_description(pc, offer)

mline = ExSDP.Media.add_attribute(audio_mline, {:mid, "1"})
sdp = ExSDP.add_media(raw_sdp, mline) |> to_string()
offer = %SessionDescription{type: :offer, sdp: sdp}
assert {:error, :duplicated_mid} = PeerConnection.set_remote_description(pc, offer)

mline = ExSDP.Media.new("audio", 9, "UDP/TLS/RTP/SAVPF", [96])
sdp = ExSDP.add_media(raw_sdp, mline) |> to_string()
offer = %SessionDescription{type: :offer, sdp: sdp}
assert {:error, :missing_mid} = PeerConnection.set_remote_description(pc, offer)

sdp =
raw_sdp
|> ExSDP.add_attribute(%ExSDP.Attribute.Group{semantics: "BUNDLE", mids: [0]})
|> ExSDP.add_media(audio_mline)

offer = %SessionDescription{type: :offer, sdp: to_string(sdp)}
assert :ok == PeerConnection.set_remote_description(pc, offer)

sdp = ExSDP.add_media(sdp, video_mline) |> to_string()

offer = %SessionDescription{type: :offer, sdp: sdp}

assert {:error, :non_exhaustive_bundle_group} ==
PeerConnection.set_remote_description(pc, offer)
describe "set_remote_description/2" do
test "MID" do
{:ok, pc} = PeerConnection.start_link()

raw_sdp = ExSDP.new()

mline = ExSDP.Media.add_attribute(@audio_mline, {:mid, "1"})
sdp = ExSDP.add_media(raw_sdp, mline) |> to_string()
offer = %SessionDescription{type: :offer, sdp: sdp}
assert {:error, :duplicated_mid} = PeerConnection.set_remote_description(pc, offer)

mline = ExSDP.Media.new("audio", 9, "UDP/TLS/RTP/SAVPF", [96])
sdp = ExSDP.add_media(raw_sdp, mline) |> to_string()
offer = %SessionDescription{type: :offer, sdp: sdp}
assert {:error, :missing_mid} = PeerConnection.set_remote_description(pc, offer)
end

test "BUNDLE group" do
{:ok, pc} = PeerConnection.start_link()

sdp = ExSDP.add_media(ExSDP.new(), [@audio_mline, @video_mline])

[
{nil, {:error, :missing_bundle_group}},
{%ExSDP.Attribute.Group{semantics: "BUNDLE", mids: [0]},
{:error, :non_exhaustive_bundle_group}},
{%ExSDP.Attribute.Group{semantics: "BUNDLE", mids: [0, 1]}, :ok}
]
|> Enum.each(fn {bundle_group, expected_result} ->
sdp = ExSDP.add_attribute(sdp, bundle_group) |> to_string()
offer = %SessionDescription{type: :offer, sdp: sdp}
assert expected_result == PeerConnection.set_remote_description(pc, offer)
end)
end

test "ICE credentials" do
{:ok, pc} = PeerConnection.start_link()

raw_sdp = ExSDP.new()

[
{{nil, nil}, {"someufrag", "somepwd"}, {"someufrag", "somepwd"}, :ok},
{{"someufrag", "somepwd"}, {"someufrag", "somepwd"}, {nil, nil}, :ok},
{{"someufrag", "somepwd"}, {nil, nil}, {nil, nil}, :ok},
{{"someufrag", "somepwd"}, {"someufrag", nil}, {nil, "somepwd"}, :ok},
{{nil, nil}, {"someufrag", "somepwd"}, {nil, nil}, {:error, :missing_ice_credentials}},
{{nil, nil}, {"someufrag", "somepwd"}, {"someufrag", nil}, {:error, :missing_ice_pwd}},
{{nil, nil}, {"someufrag", "somepwd"}, {nil, "somepwd"}, {:error, :missing_ice_ufrag}},
{{nil, nil}, {nil, nil}, {nil, nil}, {:error, :missing_ice_credentials}},
{{nil, nil}, {"someufrag", "somepwd"}, {"someufrag", "someotherpwd"},
{:error, :conflicting_ice_credentials}}
]
|> Enum.each(fn {{s_ufrag, s_pwd}, {a_ufrag, a_pwd}, {v_ufrag, v_pwd}, expected_result} ->
audio_mline =
ExSDP.Media.new("audio", 9, "UDP/TLS/RTP/SAVPF", [108])
|> ExSDP.Media.add_attributes(mid: "0")

video_mline =
ExSDP.Media.new("video", 9, "UDP/TLS/RTP/SAVPF", [96])
|> ExSDP.Media.add_attributes(mid: "1")

audio_mline =
if a_ufrag do
ExSDP.Media.add_attribute(audio_mline, {:ice_ufrag, a_ufrag})
else
audio_mline
end

audio_mline =
if a_pwd do
ExSDP.Media.add_attribute(audio_mline, {:ice_pwd, a_pwd})
else
audio_mline
end

video_mline =
if v_ufrag do
ExSDP.Media.add_attribute(video_mline, {:ice_ufrag, v_ufrag})
else
video_mline
end

video_mline =
if v_pwd do
ExSDP.Media.add_attribute(video_mline, {:ice_pwd, v_pwd})
else
video_mline
end

sdp =
ExSDP.add_attribute(raw_sdp, %ExSDP.Attribute.Group{semantics: "BUNDLE", mids: [0, 1]})

sdp =
if s_ufrag do
ExSDP.add_attribute(sdp, {:ice_ufrag, s_ufrag})
else
sdp
end

sdp =
if s_pwd do
ExSDP.add_attribute(sdp, {:ice_pwd, s_pwd})
else
sdp
end

sdp =
sdp
|> ExSDP.add_media([audio_mline, video_mline])
|> to_string()

offer = %SessionDescription{type: :offer, sdp: sdp}

assert expected_result == PeerConnection.set_remote_description(pc, offer)
end)
end
end
end

0 comments on commit 0af46c1

Please sign in to comment.