# `Caracola.SDP`
[🔗](https://github.com/netkubes/caracola/blob/v0.1.0-rc.1/lib/caracola/sdp.ex#L1)

RFC 4566 SDP parser and generator. Port of NkSIP's `nksip_sdp`.

Lines must appear in the order mandated by the RFC. As in NkSIP, `b=`
lines found among the `a=` lines are accepted (FreeSWITCH sends them).

    iex> {:ok, sdp} = Caracola.SDP.parse("v=0\r\no=- 1 2 IN IP4 h\r\ns=-\r\nt=0 0\r\nm=audio 4000 RTP/AVP 0\r\na=sendrecv\r\n")
    iex> [media] = sdp.medias
    iex> {media.media, media.port, media.fmt, media.attributes}
    {"audio", 4000, ["0"], [{"sendrecv", []}]}

# `address`

```elixir
@type address() :: {type :: String.t(), addr_type :: String.t(), addr :: String.t()}
```

A connection address: network type, address type and address.

# `attribute`

```elixir
@type attribute() :: {name :: String.t(), values :: [String.t()]}
```

An attribute: its name and values.

# `media_spec`

```elixir
@type media_spec() ::
  {media :: String.t(), port :: :inet.port_number(),
   [{:rtpmap, non_neg_integer(), String.t()} | String.t() | atom()]}
```

A media description to build a session with `Caracola.SDP.new/2`: media type, port and codecs or attributes.

# `media_status`

```elixir
@type media_status() :: :inactive | :recvonly | :sendonly | :sendrecv
```

The direction of a media stream.

# `t`

```elixir
@type t() :: %Caracola.SDP{
  address: address(),
  attributes: [attribute()],
  bandwidth: [String.t()],
  connect: address() | nil,
  email: String.t() | nil,
  id: non_neg_integer(),
  info: String.t() | nil,
  key: String.t() | nil,
  medias: [Caracola.SDP.Media.t()],
  phone: String.t() | nil,
  sdp_vsn: String.t(),
  session: String.t(),
  time: [time()],
  uri: String.t() | nil,
  user: String.t(),
  vsn: non_neg_integer(),
  zone: String.t() | nil
}
```

A session description (RFC 8866).

# `time`

```elixir
@type time() ::
  {start :: non_neg_integer(), stop :: non_neg_integer(), reps :: [String.t()]}
```

A time description: start, stop and repeats.

# `encode`

```elixir
@spec encode(t()) :: binary()
```

Serializes an SDP, using CRLF line endings.

# `from_message`

```elixir
@spec from_message(Caracola.Message.t()) :: t() | nil
```

Returns the SDP in the body of a message, if its Content-Type is
`application/sdp`.

# `increment`

```elixir
@spec increment(t()) :: t()
```

Increments the SDP version.

# `new`

```elixir
@spec new(String.t(), [media_spec()]) :: t()
```

Builds a basic SDP for `host` with one `m=` section per media spec.

Each spec is `{media, port, attributes}`, where an attribute is either
`{:rtpmap, payload, data}` (which also adds the payload to the format list)
or a raw attribute like `"ptime:20"` or `:sendrecv`. The protocol is
`RTP/AVP`.

    iex> sdp = Caracola.SDP.new("10.0.0.1", [{"audio", 4000, [{:rtpmap, 0, "PCMU/8000"}, :sendrecv]}])
    iex> {sdp.connect, hd(sdp.medias).fmt, hd(sdp.medias).attributes}
    {{"IN", "IP4", "10.0.0.1"}, ["0"], [{"rtpmap", ["0", "PCMU/8000"]}, {"sendrecv", []}]}

# `newer?`

```elixir
@spec newer?(t() | nil, t() | nil) :: boolean()
```

Returns `true` if `sdp2` has a higher version than `sdp1`. Returns `false`
if any of them is `nil`.

# `parse`

```elixir
@spec parse(binary()) :: {:ok, t()} | :error
```

Parses an SDP body. Both CRLF and LF line endings are accepted.

# `to_iodata`

```elixir
@spec to_iodata(t()) :: iodata()
```

Serializes an SDP as iodata.

# `update`

```elixir
@spec update(t(), media_status()) :: t()
```

Sets the direction of all medias to `status`, replacing any previous one,
and increments the version.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
