Skip to content

Commit

Permalink
Add bitrate fmtp parsing for G7221
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Malaev committed Mar 12, 2024
1 parent ea367eb commit b8d1ac2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/ex_sdp/attribute/fmtp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ defmodule ExSDP.Attribute.FMTP do
:dtmf_tones,
# RED
:redundant_payloads,
# G7221
:bitrate,
unknown: []
]

Expand Down Expand Up @@ -115,6 +117,8 @@ defmodule ExSDP.Attribute.FMTP do
dtmf_tones: String.t() | nil,
# RED
redundant_payloads: [RTPMapping.payload_type_t()] | nil,
# G7221
bitrate: non_neg_integer() | nil,
# params that are currently not supported
unknown: [String.t()]
}
Expand Down Expand Up @@ -326,6 +330,10 @@ defmodule ExSDP.Attribute.FMTP do
do: {rest, %{fmtp | repair_window: value}}
end

defp parse_param(["bitrate=" <> value | rest], fmtp) do
with {:ok, value} <- Utils.parse_numeric_string(value), do: {rest, %{fmtp | bitrate: value}}
end

defp parse_param([head | rest] = params, fmtp) do
# this is for non-key-value parameters as `key=value` format is not mandatory
cond do
Expand Down Expand Up @@ -430,7 +438,9 @@ defimpl String.Chars, for: ExSDP.Attribute.FMTP do
# Telephone Events
Serializer.maybe_serialize("dtmf-tones", fmtp.dtmf_tones),
# RED
Serializer.maybe_serialize_list(fmtp.redundant_payloads, "/")
Serializer.maybe_serialize_list(fmtp.redundant_payloads, "/"),
# G7221
Serializer.maybe_serialize("bitrate", fmtp.bitrate)
]
|> Enum.filter(fn param -> param != "" end)
|> Enum.join(";")
Expand Down
22 changes: 22 additions & 0 deletions test/ex_sdp/attribute/fmtp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ defmodule ExSDP.Attribute.FMTPTest do
assert {:error, :invalid_pt} == FMTP.parse(fmtp)
end

test "parses proper fmtp with G7221 bitrate parameter" do
fmtp = "98 bitrate=48000"

expected = %FMTP{
pt: 98,
bitrate: 48_000
}

assert {:ok, expected} == FMTP.parse(fmtp)
end

test "parses proper fmtp with simple DTMF tones parameter" do
fmtp = "100 0-15"

Expand Down Expand Up @@ -255,5 +266,16 @@ defmodule ExSDP.Attribute.FMTPTest do

assert "#{fmtp}" == expected
end

test "serializes FMTP with bitrate parameter for G7221" do
fmtp = %FMTP{
pt: 98,
bitrate: 48_000
}

expected = "fmtp:98 bitrate=48000"

assert "#{fmtp}" == expected
end
end
end

0 comments on commit b8d1ac2

Please sign in to comment.