RTSP (RTSP v0.1.0)

View Source

Simplify connecting to RTSP servers.

Usage

To start an RTSP session, you can use the start_link/1 function with the required options:

{:ok, session} = RTSP.start_link(stream_uri: "rtsp://localhost:554/stream", allowed_media_types: [:video])
{:ok, tracks} = RTSP.connect(session)
:ok = RTSP.play(session)

The calling process will receive messages with the received media samples.

Message Types

The calling process will receive messages in the following format:

  • {pid_or_name, :discontinuity} - Indicates a discontinuity in the stream.
  • {pid_or_name, control_path, sample} - Contains the media sample received from the stream. control_path is the RTSP control path for the track, and sample is the media sample data.
  • {pid_or_name, :session_closed} - Indicates that the RTSP session has been closed.

A sample is a tuple in the format {payload, rtp_timestamp, wallclock_timestamp, key_frame?}:

  • payload - The media payload data (a whole access unit in case of video).
  • rtp_timestamp - The RTP timestamp of the sample as nano second starting from 0.
  • wallclock_timestamp - The wall clock timestamp when the sample was received.
  • key_frame? - A boolean indicating whether the sample is a key frame (valid for video streams.)

TCP and UDP

Currently only TCP transport is supported. UDP transport will be added in the future.

Summary

Types

Represents a track in the RTSP session.

Functions

Returns a specification to start this module under a supervisor.

Connects the rtsp server./home/ghilas/p/Evercam/ex_nvr/rtsp/lib/rtsp/source.ex

Sends a play request and starts receiving media streams.

Starts a new RTSP client session.

Types

session_opts()

@type session_opts() :: [
  stream_uri: String.t(),
  allowed_media_types: [:video | :audio | :application],
  timeout: pos_integer() | :infinity,
  keep_alive_interval: pos_integer(),
  parent_pid: pid(),
  name: atom() | nil,
  onvif_replay: boolean(),
  start_date: DateTime.t() | nil,
  end_date: DateTime.t() | nil
]

track()

@type track() :: %{
  control_path: String.t(),
  type: :video | :audio | :application,
  fmtp: ExSDP.Attribute.FMTP.t() | nil,
  rtpmap: ExSDP.Attribute.RTPMapping.t() | nil
}

Represents a track in the RTSP session.

Each track corresponds to a media stream and contains the following fields:

  • control_path - The RTSP control path for the track, can be used as an id.
  • type - The type of the media stream, which can be :video, :audio, or :application.
  • fmtp - The FMTP attribute for the track, which contains format-specific parameters.
  • rtpmap - The RTP mapping attribute for the track, which contains information about the payload type and encoding.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

connect(pid, timeout \\ 5000)

@spec connect(pid(), timeout()) :: {:ok, [track()]} | {:error, reason :: any()}

Connects the rtsp server./home/ghilas/p/Evercam/ex_nvr/rtsp/lib/rtsp/source.ex

It returns the set up tracks in case of success.

play(pid, timeout \\ 5000)

@spec play(pid(), timeout()) :: :ok | {:error, reason :: any()}

Sends a play request and starts receiving media streams.

start_link(opts)

@spec start_link(session_opts()) :: GenServer.on_start()

Starts a new RTSP client session.

The following options can be provided:

  • :stream_uri - The RTSP stream URI to connect to (required).
  • :allowed_media_types - A list of allowed media types, defaults to: [:video, :audio].
  • :timeout - The timeout for RTSP operations (default: 5 seconds).
  • :keep_alive_interval - The interval for sending keep-alive messages (default: 30 seconds).
  • :parent_pid - The parent process that will receive messages, defaults to the calling process.
  • :name - The name of the GenServer process, if provided it'll be used as the first element in the sent messages.

Onvif Replay

To decode onvif replay extension packets, the following options can be provided

  • :onvif_replay - Whether to enable ONVIF replay extension (default: false).
  • :start_date - The start date for the session.
  • :end_date - The end date for the session.