-
Notifications
You must be signed in to change notification settings - Fork 9
/
ssrc.ex
40 lines (33 loc) · 1.25 KB
/
ssrc.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
defmodule ExSDP.Attribute.SSRC do
@moduledoc """
This module represents ssrc (RFC 5576).
"""
alias ExSDP.Utils
@enforce_keys [:id, :attribute]
defstruct @enforce_keys ++ [:value]
@type t :: %__MODULE__{id: non_neg_integer(), attribute: binary(), value: binary() | nil}
@typedoc """
Key that can be used for searching this attribute using `ExSDP.Media.get_attribute/2`.
"""
@type attr_key :: :ssrc
@spec parse(binary()) :: {:ok, t()} | {:error, :invalid_ssrc}
def parse(ssrc) do
with [id, attribute] <- String.split(ssrc, " ", parts: 2),
{:ok, id} <- Utils.parse_numeric_string(id) do
case String.split(attribute, ":") do
[attribute, value] -> {:ok, %__MODULE__{id: id, attribute: attribute, value: value}}
[attribute] -> {:ok, %__MODULE__{id: id, attribute: attribute}}
_invalid_ssrc -> {:error, :invalid_ssrc}
end
else
_invalid_ssrc -> {:error, :invalid_ssrc}
end
end
end
defimpl String.Chars, for: ExSDP.Attribute.SSRC do
alias ExSDP.Attribute.SSRC
@impl true
def to_string(%SSRC{id: id, attribute: attribute, value: nil}), do: "ssrc:#{id} #{attribute}"
def to_string(%SSRC{id: id, attribute: attribute, value: value}),
do: "ssrc:#{id} #{attribute}:#{value}"
end