Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete_attribute(s)/2. Move attributes API to the ExSDP module. #40

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/ex_sdp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ defmodule ExSDP do

@spec get_attributes(sdp :: t(), key :: module() | atom() | binary()) :: [Attribute.t()]
def get_attributes(sdp, key), do: Utils.get_attributes(sdp, key)

@spec delete_attribute(sdp :: t(), key :: module() | atom() | binary()) :: t()
def delete_attribute(sdp, key), do: Utils.delete_attribute(sdp, key)

@spec delete_attributes(sdp :: t(), keys :: [module() | atom() | binary()]) :: t()
def delete_attributes(sdp, keys), do: Utils.delete_attributes(sdp, keys)
end

defimpl String.Chars, for: ExSDP do
Expand Down
6 changes: 6 additions & 0 deletions lib/ex_sdp/media.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ defmodule ExSDP.Media do
@spec get_attributes(media :: t(), key :: module() | atom() | binary()) :: [Attribute.t()]
def get_attributes(media, key), do: Utils.get_attributes(media, key)

@spec delete_attribute(media :: t(), key :: module() | atom() | binary()) :: t()
def delete_attribute(media, key), do: Utils.delete_attribute(media, key)

@spec delete_attributes(media :: t(), [key :: module() | atom() | binary()]) :: t()
def delete_attributes(media, keys), do: Utils.delete_attributes(media, keys)
Copy link
Member Author

@mickel8 mickel8 Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should instead put everything in ExSDP and instead of distinguisng between ExSDP.add/get/delete_attribute and ExSDP.Media.add/get/delete_attribute, always have ExSDP.add/get/delete_attribute 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo yes, in general the distinction between ExSDP.get_attribute() and ExSDP.Media.get_attribute() is weird and prone to mistakes.


@spec parse(binary()) :: {:ok, t()} | {:error, :invalid_media_spec | :malformed_port_number}
def parse(media) do
withl conn: [type, port, proto, fmt] <- String.split(media, " ", parts: 4),
Expand Down
22 changes: 22 additions & 0 deletions lib/ex_sdp/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ defmodule ExSDP.Utils do
end)
end

@spec delete_attribute(ExSDP.t() | ExSDP.Media.t(), module() | atom() | binary()) ::
ExSDP.t() | ExSDP.Media.t()
def delete_attribute(sdp_or_mline, key) do
delete_attributes(sdp_or_mline, [key])
end

@spec delete_attributes(ExSDP.t() | ExSDP.Media.t(), [module() | atom() | binary()]) ::
ExSDP.t() | ExSDP.Media.t()
def delete_attributes(sdp_or_mline, keys) when is_list(keys) do
keys = Enum.map(keys, fn key -> Map.get(@struct_attr_keys, key, key) end)

new_attrs =
Enum.reject(sdp_or_mline.attributes, fn
%module{} -> module in keys
{k, _v} -> k in keys
# flag attributes
k -> k in keys
end)

Map.put(sdp_or_mline, :attributes, new_attrs)
end

@spec split(String.t(), String.t() | [String.t()] | :binary.cp() | Regex.t(), any) ::
{:error, :too_few_fields} | {:ok, [String.t()]}
def split(origin, delim, expected_len) do
Expand Down
44 changes: 43 additions & 1 deletion test/ex_sdp/media_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ defmodule ExSDP.MediaTest do
end

describe "Utils functions" do
test "gets attribute by atom or module" do
test "gets and deletes attribute by atom, binary or module" do
rtpmap = %RTPMapping{clock_rate: 8000, encoding: "L8", params: 1, payload_type: 96}

ssrc = %SSRC{id: 12_345, attribute: "cname", value: "HPd3XfRHXYUxzfsJ"}
Expand All @@ -279,6 +279,7 @@ defmodule ExSDP.MediaTest do
|> Media.add_attribute(fmtp)
|> Media.add_attribute(msid)
|> Media.add_attribute(extmap)
|> Media.add_attribute({"key", "value"})

assert rtpmap == Media.get_attribute(media, RTPMapping)
assert rtpmap == Media.get_attribute(media, :rtpmap)
Expand All @@ -294,6 +295,47 @@ defmodule ExSDP.MediaTest do

assert extmap == Media.get_attribute(media, Extmap)
assert extmap == Media.get_attribute(media, :extmap)

assert {"key", "value"} == Media.get_attribute(media, "key")

assert [%SSRC{}, %FMTP{}, %MSID{}, %Extmap{}, {"key", "value"}] =
Media.delete_attribute(media, RTPMapping).attributes

assert [%SSRC{}, %FMTP{}, %MSID{}, %Extmap{}, {"key", "value"}] =
Media.delete_attribute(media, :rtpmap).attributes

assert [%RTPMapping{}, %FMTP{}, %MSID{}, %Extmap{}, {"key", "value"}] =
Media.delete_attribute(media, SSRC).attributes

assert [%RTPMapping{}, %FMTP{}, %MSID{}, %Extmap{}, {"key", "value"}] =
Media.delete_attribute(media, :ssrc).attributes

assert [%RTPMapping{}, %SSRC{}, %MSID{}, %Extmap{}, {"key", "value"}] =
Media.delete_attribute(media, FMTP).attributes

assert [%RTPMapping{}, %SSRC{}, %MSID{}, %Extmap{}, {"key", "value"}] =
Media.delete_attribute(media, :fmtp).attributes

assert [%RTPMapping{}, %SSRC{}, %FMTP{}, %Extmap{}, {"key", "value"}] =
Media.delete_attribute(media, MSID).attributes

assert [%RTPMapping{}, %SSRC{}, %FMTP{}, %Extmap{}, {"key", "value"}] =
Media.delete_attribute(media, :msid).attributes

assert [%RTPMapping{}, %SSRC{}, %FMTP{}, %MSID{}, {"key", "value"}] =
Media.delete_attribute(media, Extmap).attributes

assert [%RTPMapping{}, %SSRC{}, %FMTP{}, %MSID{}, {"key", "value"}] =
Media.delete_attribute(media, :extmap).attributes

assert [%RTPMapping{}, %SSRC{}, %FMTP{}, %MSID{}, %Extmap{}] =
Media.delete_attribute(media, "key").attributes

assert [] ==
Media.delete_attributes(media, [RTPMapping, SSRC, FMTP, MSID, Extmap, "key"]).attributes

assert [] ==
Media.delete_attributes(media, [:rtpmap, :ssrc, :fmtp, :msid, :extmap, "key"]).attributes
end
end
end
17 changes: 13 additions & 4 deletions test/sdp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ defmodule ExSDPTest do
end
end

describe "get_attribute/2" do
test "gets attribute by atom, binary or module" do
describe "Utils functions" do
test "gets and deletes attribute by atom, binary or module" do
sdp =
ExSDP.new()
|> ExSDP.add_attribute({"key", "value"})
Expand All @@ -208,10 +208,19 @@ defmodule ExSDPTest do
assert nil == ExSDP.get_attribute(sdp, :non_existing_atom)
assert nil == ExSDP.get_attribute(sdp, ExSDP.Attribute.NonExistingModule)
assert nil == ExSDP.get_attribute(sdp, "non_existing_string")

assert [:recvonly, %ExSDP.Attribute.Group{}] = ExSDP.delete_attribute(sdp, "key").attributes

assert [{"key", "value"}, %ExSDP.Attribute.Group{}] =
ExSDP.delete_attribute(sdp, :recvonly).attributes

assert [{"key", "value"}, :recvonly] =
ExSDP.delete_attribute(sdp, ExSDP.Attribute.Group).attributes

assert [] =
ExSDP.delete_attributes(sdp, ["key", :recvonly, ExSDP.Attribute.Group]).attributes
end
end

describe "get_attributes/2" do
test "gets multiple attributes by module" do
sdp =
ExSDP.new()
Expand Down