RTSP (RTSP v0.4.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:

  • {:rtsp, pid_or_name, :discontinuity} - Indicates a discontinuity in the stream.
  • {:rtsp, pid_or_name, {control_path, sample_or_samples}} - 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.
  • {:rtsp, pid_or_name, :session_closed} - Indicates that the RTSP session has been closed.

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

  • 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.
  • key_frame? - A boolean indicating whether the sample is a key frame (valid for video streams.)
  • wallclock_timestamp - The wall clock timestamp when the sample was received.

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.

Closes the RTSP session and stops receiving media streams.

Types

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(Keyword.t()) :: GenServer.on_start()

Starts a new RTSP client session.

The following options can be provided:

  • :stream_uri (String.t/0) - Required. The RTSP stream URI to connect to.

  • :allowed_media_types - The type of media streams to request from the rtsp server. The default value is [:audio, :video].

  • :transport - The transport protocol to use for the RTSP session.

    This can be either:

    • :tcp - for TCP transport.
    • {:udp, min_port, max_port} - for UDP transport, where min_port and max_port are the port range for RTP and RTCP streams.

    The default value is :tcp.

  • :timeout (timeout/0) - The timeout for RTSP operations. The default value is 5000.

  • :keep_alive_interval (timeout/0) - The interval for sending keep-alive messages. The default value is 30000.

  • :reorder_queue_size (pos_integer/0) - Set number of packets to buffer for handling of reordered packets.

    Due to the implementation, this size should be an exponent of 2.

    The default value is 64.

  • :receiver (pid/0) - The process that will receive media streams messages.

  • :onvif_replay - The stream uri is an onvif replay.

    • :start_date (struct of type DateTime) - Required. The start date for the onvif replay session.

    • :end_date (struct of type DateTime) - The end date for the onvif replay session. The default value is nil.

stop(pid)

@spec stop(pid()) :: :ok

Closes the RTSP session and stops receiving media streams.