Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/bluenviron/gortsplib/llms.txt

Use this file to discover all available pages before exploring further.

Client is a RTSP client that can read streams from servers (playing) or publish streams to servers (recording). It manages connections, transport negotiation, authentication, and packet I/O.

Struct fields

Target

Scheme
string
URL scheme. Must be "rtsp" or "rtsps" (TLS).
Host
string
Remote host and port (e.g. "192.168.1.10:554").

RTSP parameters

ReadTimeout
time.Duration
default:"10s"
Timeout for read operations.
WriteTimeout
time.Duration
default:"10s"
Timeout for write operations.
TLSConfig
*tls.Config
default:"nil"
TLS configuration used when connecting to RTSPS servers.
Tunnel
Tunnel
default:"TunnelNone"
Tunneling method. One of TunnelNone, TunnelHTTP, or TunnelWebSocket.
Protocol
*Protocol
default:"nil"
Transport protocol (ProtocolUDP, ProtocolUDPMulticast, or ProtocolTCP). When nil the client first tries UDP, then falls back to TCP automatically.
AnyPortEnable
bool
default:"false"
Enable communication with servers that do not provide UDP server ports or that use different server ports than announced. This can be a security risk.
InitialUDPReadTimeout
time.Duration
default:"3s"
When reading with UDP, if no packet is received within this timeout the client switches to TCP.
UDPReadBufferSize
int
default:"OS default"
Size of the UDP read buffer. Increase to reduce packet loss on high-bandwidth streams.
WriteQueueSize
int
default:"256"
Size of the outbound packet queue. Must be a power of two.
MaxPacketSize
int
default:"1472"
Maximum size of outbound RTP/RTCP packets in bytes. Must be less than the IPv4/UDP MTU (1472 bytes).
UserAgent
string
default:"\"gortsplib\""
Value sent in the User-Agent RTSP header.
DisableRTCPSenderReports
bool
default:"false"
Disable automatic generation and sending of RTCP sender reports.
RequestBackChannels
bool
default:"false"
Explicitly request back channels from the server (ONVIF extension).
UDPSourcePortRange
[2]uint16
default:"[10000, 65535]"
Range of local source ports used for outbound UDP packets.

System functions

DialContext
func(ctx context.Context, network, address string) (net.Conn, error)
default:"(&net.Dialer{}).DialContext"
Function used to open TCP connections. Override for custom networking.
ListenPacket
func(network, address string) (net.PacketConn, error)
default:"net.ListenPacket"
Function used to open UDP listeners.
ResolveIPAddr
func(network, address string) (*net.IPAddr, error)
default:"net.ResolveIPAddr"
Function used to resolve IP addresses from hostnames.

Callbacks

OnRequest
ClientOnRequestFunc
Called each time the client sends a request to the server. Prototype: func(*base.Request).
OnResponse
ClientOnResponseFunc
Called each time the client receives a response from the server. Prototype: func(*base.Response).
OnServerRequest
ClientOnRequestFunc
Called each time the client receives a request from the server (e.g. server-initiated OPTIONS). Prototype: func(*base.Request).
OnServerResponse
ClientOnResponseFunc
Called each time the client sends a response to the server. Prototype: func(*base.Response).
OnTransportSwitch
ClientOnTransportSwitchFunc
Called when the transport protocol is switched (e.g. UDP to TCP fallback). Prototype: func(err error). Defaults to logging the error.
OnPacketsLost
ClientOnPacketsLostFunc
Called when the client detects lost RTP packets. Prototype: func(lost uint64). Defaults to logging the count.
OnDecodeError
ClientOnDecodeErrorFunc
Called when a non-fatal decode error occurs. Prototype: func(err error). Defaults to logging the error.

Methods

Start

func (c *Client) Start() error
Initializes the connection to the server using the Scheme and Host fields. Must be called before any RTSP method calls. Starts the internal event loop in a goroutine.
error
error
Non-nil if initialization fails (e.g. invalid WriteQueueSize, invalid MaxPacketSize).

StartRecording

func (c *Client) StartRecording(address string, desc *description.Session) error
Convenience method that parses address as a URL, calls Start(), Announce(), SetupAll(), and Record() in sequence. The client is closed automatically on any error.
address
string
required
Full RTSP URL string of the server to publish to.
desc
*description.Session
required
Session description (SDP) of the media to be published.
error
error
Non-nil if any step in the connection/publish sequence fails.

Close

func (c *Client) Close()
Closes all client resources (connections, UDP listeners, goroutines) and waits for them to exit. Sends a TEARDOWN request before closing. Safe to call from any goroutine.

Wait

func (c *Client) Wait() error
Blocks until all client resources have exited. This occurs either when Close() is called or when a fatal error occurs (e.g. network timeout).
error
error
The close error, or nil if closed cleanly via Close().

Describe

func (c *Client) Describe(u *base.URL) (*description.Session, *base.Response, error)
Sends a DESCRIBE request to the server and returns the parsed session description. Handles redirects automatically.
u
*base.URL
required
The RTSP URL to describe.
*description.Session
*description.Session
Parsed session description containing media tracks and their formats.
*base.Response
*base.Response
The raw RTSP response.
error
error
Non-nil on failure.

Announce

func (c *Client) Announce(u *base.URL, desc *description.Session) (*base.Response, error)
Sends an ANNOUNCE request to the server with the given session description. Call before Setup() and Record() when publishing.
u
*base.URL
required
The RTSP URL to announce to.
desc
*description.Session
required
Session description of the media to publish.
*base.Response
*base.Response
The raw RTSP response.
error
error
Non-nil on failure.

SetupAll

func (c *Client) SetupAll(baseURL *base.URL, medias []*description.Media) error
Calls Setup() for every media in medias. Uses port 0 for both RTP and RTCP (auto-assigned). Stops and returns on the first error.
baseURL
*base.URL
required
The base URL returned from Describe() (available as desc.BaseURL).
medias
[]*description.Media
required
Slice of media descriptions to set up.
error
error
Non-nil if any SETUP request fails.

Setup

func (c *Client) Setup(
    baseURL *base.URL,
    media *description.Media,
    rtpPort int,
    rtcpPort int,
) (*base.Response, error)
Sends a SETUP request for a single media track. rtpPort and rtcpPort are only used with UDP transport; pass 0 for both to let the OS choose ports.
baseURL
*base.URL
required
The base URL of the stream (from Describe()).
media
*description.Media
required
The specific media track to set up.
rtpPort
int
Local UDP port for RTP. 0 means auto-assign. Must be even.
rtcpPort
int
Local UDP port for RTCP. 0 means auto-assign. Must equal rtpPort + 1.
*base.Response
*base.Response
The raw RTSP response.
error
error
Non-nil on failure.

Options

func (c *Client) Options(u *base.URL) (*base.Response, error)
Sends an OPTIONS request. Normally called automatically; can be used to probe server capabilities.
u
*base.URL
required
The URL to send the OPTIONS request to.
*base.Response
*base.Response
The raw RTSP response containing a Public header with supported methods.
error
error
Non-nil on failure.

Play

func (c *Client) Play(ra *headers.Range) (*base.Response, error)
Sends a PLAY request. Must be called after Setup(). Starts receiving packets.
ra
*headers.Range
Playback range. Pass nil to start from the beginning (NPT 0).
*base.Response
*base.Response
The raw RTSP response.
error
error
Non-nil on failure.

Record

func (c *Client) Record() (*base.Response, error)
Sends a RECORD request. Must be called after Announce() and Setup(). Starts sending packets.
*base.Response
*base.Response
The raw RTSP response.
error
error
Non-nil on failure.

Pause

func (c *Client) Pause() (*base.Response, error)
Sends a PAUSE request. Can only be called after Play() or Record(). Suspends the stream without tearing it down.
*base.Response
*base.Response
The raw RTSP response.
error
error
Non-nil on failure.

WritePacketRTP

func (c *Client) WritePacketRTP(medi *description.Media, pkt *rtp.Packet) error
Sends an RTP packet to the server. Uses the current wall clock as the NTP timestamp. Only valid in the recording state after Record().
medi
*description.Media
required
The media track to send on.
pkt
*rtp.Packet
required
The RTP packet to send.
error
error
Non-nil on write failure.

WritePacketRTPWithNTP

func (c *Client) WritePacketRTPWithNTP(medi *description.Media, pkt *rtp.Packet, ntp time.Time) error
Like WritePacketRTP but accepts an explicit NTP timestamp. This NTP value is used in periodic RTCP sender reports.
medi
*description.Media
required
The media track to send on.
pkt
*rtp.Packet
required
The RTP packet to send.
ntp
time.Time
required
Absolute NTP timestamp of the packet.
error
error
Non-nil on write failure.

WritePacketRTCP

func (c *Client) WritePacketRTCP(medi *description.Media, pkt rtcp.Packet) error
Sends an RTCP packet to the server.
medi
*description.Media
required
The media track to send on.
pkt
rtcp.Packet
required
The RTCP packet to send.
error
error
Non-nil on write failure.

OnPacketRTP

func (c *Client) OnPacketRTP(medi *description.Media, forma format.Format, cb OnPacketRTPFunc)
Registers a callback for incoming RTP packets on a specific media format. OnPacketRTPFunc has signature func(*rtp.Packet). Must be called after Setup() and before Play().
medi
*description.Media
required
The media track to listen on.
forma
format.Format
required
The specific codec format to receive packets for.
cb
OnPacketRTPFunc
required
Callback invoked for each received RTP packet: func(*rtp.Packet).

OnPacketRTPAny

func (c *Client) OnPacketRTPAny(cb OnPacketRTPAnyFunc)
Registers a callback for incoming RTP packets on all set-up media tracks and formats. OnPacketRTPAnyFunc has signature func(*description.Media, format.Format, *rtp.Packet).
cb
OnPacketRTPAnyFunc
required
Callback invoked for each received RTP packet with media and format context.

OnPacketRTCP

func (c *Client) OnPacketRTCP(medi *description.Media, cb OnPacketRTCPFunc)
Registers a callback for incoming RTCP packets on a specific media track. OnPacketRTCPFunc has signature func(rtcp.Packet).
medi
*description.Media
required
The media track to listen on.
cb
OnPacketRTCPFunc
required
Callback invoked for each received RTCP packet.

OnPacketRTCPAny

func (c *Client) OnPacketRTCPAny(cb OnPacketRTCPAnyFunc)
Registers a callback for incoming RTCP packets on all set-up media tracks. OnPacketRTCPAnyFunc has signature func(*description.Media, rtcp.Packet).
cb
OnPacketRTCPAnyFunc
required
Callback invoked for each received RTCP packet with media context.

PacketPTS

func (c *Client) PacketPTS(medi *description.Media, pkt *rtp.Packet) (int64, bool)
Returns the presentation timestamp (PTS) of an inbound RTP packet. The PTS is computed by decoding the RTP timestamp and synchronizing it with other tracks via rtptime.GlobalDecoder.
medi
*description.Media
required
The media track the packet belongs to.
pkt
*rtp.Packet
required
The inbound RTP packet.
int64
int64
The PTS in media clock units.
bool
bool
true if the PTS could be computed, false if not enough information is available yet.

PacketNTP

func (c *Client) PacketNTP(medi *description.Media, pkt *rtp.Packet) (time.Time, bool)
Returns the absolute NTP timestamp of an inbound RTP packet, derived from RTCP sender reports.
medi
*description.Media
required
The media track the packet belongs to.
pkt
*rtp.Packet
required
The inbound RTP packet.
time.Time
time.Time
The absolute NTP timestamp.
bool
bool
true if the NTP could be derived from RTCP sender reports, false otherwise.

Transport

func (c *Client) Transport() *ClientTransport
Returns details about the active transport. The returned ClientTransport contains:
  • Conn ConnTransport — connection transport, with Tunnel field (TunnelNone, TunnelHTTP, TunnelWebSocket).
  • Session *SessionTransport — session transport (non-nil after Setup()), with Protocol (ProtocolUDP, ProtocolUDPMulticast, ProtocolTCP) and Profile fields.

Stats

func (c *Client) Stats() *ClientStats
Returns a snapshot of client statistics. The returned *ClientStats struct contains:
FieldTypeDescription
Conn.InboundBytesuint64Total bytes received on the TCP connection
Conn.OutboundBytesuint64Total bytes sent on the TCP connection
Session.InboundBytesuint64Total inbound media bytes
Session.InboundRTPPacketsuint64RTP packets received and processed
Session.InboundRTPPacketsLostuint64Lost inbound RTP packets
Session.InboundRTPPacketsInErroruint64Inbound RTP packets that could not be processed
Session.InboundRTPPacketsJitterfloat64Mean jitter of inbound RTP packets
Session.InboundRTCPPacketsuint64RTCP packets received and processed
Session.OutboundBytesuint64Total outbound media bytes
Session.OutboundRTPPacketsuint64RTP packets sent
Session.OutboundRTPPacketsReportedLostuint64RTP packets reported lost by the remote
Session.OutboundRTCPPacketsuint64RTCP packets sent
Session.Mediasmap[*description.Media]SessionStatsMediaPer-media statistics

Callback type aliases

TypeSignatureUsed by
ClientOnRequestFuncfunc(*base.Request)OnRequest, OnServerRequest
ClientOnResponseFuncfunc(*base.Response)OnResponse, OnServerResponse
ClientOnTransportSwitchFuncfunc(err error)OnTransportSwitch
ClientOnPacketsLostFuncfunc(lost uint64)OnPacketsLost
ClientOnDecodeErrorFuncfunc(err error)OnDecodeError
OnPacketRTPFuncfunc(*rtp.Packet)OnPacketRTP
OnPacketRTPAnyFuncfunc(*description.Media, format.Format, *rtp.Packet)OnPacketRTPAny
OnPacketRTCPFuncfunc(rtcp.Packet)OnPacketRTCP
OnPacketRTCPAnyFuncfunc(*description.Media, rtcp.Packet)OnPacketRTCPAny

Build docs developers (and LLMs) love