View Source ExRTP.Packet (ex_rtp v0.4.0)

RTP packet encoding and decoding functionalities.

Examples

  iex> alias ExRTP.Packet
  iex> alias ExRTP.Packet.Extension.AudioLevel
  iex> extension = AudioLevel.new(true, 120) |> AudioLevel.to_raw(5)
  iex> payload = <<3, 5, 5, 0>>
  iex> encoded =
  ...>   payload
  ...>   |> Packet.new(
  ...>     payload_type: 120,
  ...>     sequence_number: 50_000,
  ...>     timestamp: 1_000_000,
  ...>     ssrc: 500_000
  ...>   )
  ...>   |> Packet.add_extension(extension)
  ...>   |> Packet.encode()
  iex> {:ok, %Packet{payload: ^payload}} = Packet.decode(encoded)

Summary

Types

t()

Struct representing an RTP packet.

Functions

Adds RTP header extension (RFC 8285, multiple extensions per packet) to this packet.

Decodes binary into an RTP packet.

Encodes an RTP packet and returns resulting binary.

Fetches extension with specified id from this packet.

Removes all RTP header extension with specified ID, if there's any.

Removes RTP header extensions form this packet.

Sets RTP header extension (RFC 3550, one extension per packet) for this packet.

Types

@type t() :: %ExRTP.Packet{
  csrc: [uint32()],
  extension: boolean(),
  extension_profile: uint16() | nil,
  extensions: [ExRTP.Packet.Extension.t()] | binary() | nil,
  marker: boolean(),
  padding: boolean(),
  padding_size: uint8(),
  payload: binary(),
  payload_type: uint7(),
  sequence_number: uint16(),
  ssrc: uint32(),
  timestamp: uint32(),
  version: 0..3
}

Struct representing an RTP packet.

Use new/2 to create a new RTP packet. RTP header extensions and packet padding should not be modified directly, but with the use of functions in this module.

@type uint7() :: 0..127
@type uint8() :: 0..255
@type uint16() :: 0..65535
@type uint32() :: 0..4_294_967_295

Functions

Link to this function

add_extension(packet, extension)

View Source
@spec add_extension(t(), ExRTP.Packet.Extension.t()) :: t()

Adds RTP header extension (RFC 8285, multiple extensions per packet) to this packet.

If you want to use the traditional RTP extension mechanism (one extension per packet), see set_extension/3.

Note that:

  • if this packet already has the traditional extension (RFC 3550, one extension per packet) set, it will be removed,
  • if the packet contained the RFC 8285 extensions, this function will append the new extension without removing previous extensions.

This function automatically decides whether to use "one byte" or "two byte" extension mechanism (see RFC 8285, sec. 4) based on the extension data size and its id:

  • if sizes of all of the extension are in range 1..16, and the IDs are in range 1..14, "one byte" mechanism is used,
  • if there is an extension with size in range 17..255 or equal to 0, or with ID in range 15..255, "two byte" mechanism is used,
  • otherwise, the extension is invalid and this function will raise.
@spec decode(binary()) :: {:ok, t()} | {:error, :not_enough_data}

Decodes binary into an RTP packet.

If RTP header extension is not used, extensions and extension_profile fields in the returned struct will be nil. Otherwise, extensions will be:

  • a list, if the general mechanism for RTP header extension from RFC 8285 is used,
  • a binary, if this is a traditional header extension as defined in RFC 3550

If the binary is too short to be valid, this function will return error with :not_enough_data.

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

Encodes an RTP packet and returns resulting binary.

Link to this function

fetch_extension(packet, id)

View Source
@spec fetch_extension(t(), non_neg_integer()) ::
  {:ok, ExRTP.Packet.Extension.t()} | :error

Fetches extension with specified id from this packet.

If no extension with id is found or RFC 8285 extension mechanism is not used, this function will return :error.

Link to this function

new(payload, fields \\ [])

View Source
@spec new(binary(),
  payload_type: uint7(),
  sequence_number: uint16(),
  timestamp: uint32(),
  ssrc: uint32(),
  csrc: [uint32()],
  marker: boolean(),
  padding: uint8()
) :: t()

Creates new ExRTP.Packet.t/0 struct.

The fields parameter corresponds to the RTP packet header fields. Refer to RFC 3550 for in-depth explanation.

If not passed, payload_type, sequence_number, timestamp and ssrc will be set to 0, as they are often expected to be set later during the processing of the packet.

The fields keyword list may also include:

  • csrc (by default [], must be shorter than 16),
  • marker (by default false),
  • padding (by default no padding is applied)
Link to this function

remove_extension(packet, id)

View Source
@spec remove_extension(t(), non_neg_integer()) :: t()

Removes all RTP header extension with specified ID, if there's any.

This function works only works for RFC 8285 extensions, for the RFC 3550 extension use remove_extensions/1 instead.

Link to this function

remove_extensions(packet)

View Source
@spec remove_extensions(t()) :: t()

Removes RTP header extensions form this packet.

Link to this function

set_extension(packet, profile, extension)

View Source
@spec set_extension(t(), uint16(), binary()) :: t()

Sets RTP header extension (RFC 3550, one extension per packet) for this packet.

If you want to use the general extension mechanism from RFC 8285 (multiple extensions per packet), see add_extension/2.

Note that:

  • this function will overwrite any extensions set beforehand,
  • extension must be no longer than 262_140 bytes and its length must be a multiple of 4 bytes,
  • profile value must not be 0xBEDE or 0x1000.