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
URL scheme. Must be
"rtsp" or "rtsps" (TLS).Remote host and port (e.g.
"192.168.1.10:554").RTSP parameters
Timeout for read operations.
Timeout for write operations.
TLS configuration used when connecting to RTSPS servers.
Tunneling method. One of
TunnelNone, TunnelHTTP, or TunnelWebSocket.Transport protocol (
ProtocolUDP, ProtocolUDPMulticast, or ProtocolTCP). When nil the client first tries UDP, then falls back to TCP automatically.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.
When reading with UDP, if no packet is received within this timeout the client switches to TCP.
Size of the UDP read buffer. Increase to reduce packet loss on high-bandwidth streams.
Size of the outbound packet queue. Must be a power of two.
Maximum size of outbound RTP/RTCP packets in bytes. Must be less than the IPv4/UDP MTU (1472 bytes).
Value sent in the
User-Agent RTSP header.Disable automatic generation and sending of RTCP sender reports.
Explicitly request back channels from the server (ONVIF extension).
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.
Function used to open UDP listeners.
Function used to resolve IP addresses from hostnames.
Callbacks
Called each time the client sends a request to the server. Prototype:
func(*base.Request).Called each time the client receives a response from the server. Prototype:
func(*base.Response).Called each time the client receives a request from the server (e.g. server-initiated OPTIONS). Prototype:
func(*base.Request).Called each time the client sends a response to the server. Prototype:
func(*base.Response).Called when the transport protocol is switched (e.g. UDP to TCP fallback). Prototype:
func(err error). Defaults to logging the error.Called when the client detects lost RTP packets. Prototype:
func(lost uint64). Defaults to logging the count.Called when a non-fatal decode error occurs. Prototype:
func(err error). Defaults to logging the error.Methods
Lifecycle
Lifecycle
Start
Scheme and Host fields. Must be called before any RTSP method calls. Starts the internal event loop in a goroutine.Non-nil if initialization fails (e.g. invalid
WriteQueueSize, invalid MaxPacketSize).StartRecording
address as a URL, calls Start(), Announce(), SetupAll(), and Record() in sequence. The client is closed automatically on any error.Full RTSP URL string of the server to publish to.
Session description (SDP) of the media to be published.
Non-nil if any step in the connection/publish sequence fails.
Close
Wait
Close() is called or when a fatal error occurs (e.g. network timeout).The close error, or
nil if closed cleanly via Close().Stream negotiation
Stream negotiation
Describe
The RTSP URL to describe.
Parsed session description containing media tracks and their formats.
The raw RTSP response.
Non-nil on failure.
Announce
Setup() and Record() when publishing.The RTSP URL to announce to.
Session description of the media to publish.
The raw RTSP response.
Non-nil on failure.
SetupAll
Setup() for every media in medias. Uses port 0 for both RTP and RTCP (auto-assigned). Stops and returns on the first error.The base URL returned from
Describe() (available as desc.BaseURL).Slice of media descriptions to set up.
Non-nil if any SETUP request fails.
Setup
rtpPort and rtcpPort are only used with UDP transport; pass 0 for both to let the OS choose ports.The base URL of the stream (from
Describe()).The specific media track to set up.
Local UDP port for RTP.
0 means auto-assign. Must be even.Local UDP port for RTCP.
0 means auto-assign. Must equal rtpPort + 1.The raw RTSP response.
Non-nil on failure.
Options
The URL to send the OPTIONS request to.
The raw RTSP response containing a
Public header with supported methods.Non-nil on failure.
Stream control
Stream control
Play
Setup(). Starts receiving packets.Playback range. Pass
nil to start from the beginning (NPT 0).The raw RTSP response.
Non-nil on failure.
Record
Announce() and Setup(). Starts sending packets.The raw RTSP response.
Non-nil on failure.
Pause
Play() or Record(). Suspends the stream without tearing it down.The raw RTSP response.
Non-nil on failure.
Packet I/O
Packet I/O
WritePacketRTP
Record().The media track to send on.
The RTP packet to send.
Non-nil on write failure.
WritePacketRTPWithNTP
WritePacketRTP but accepts an explicit NTP timestamp. This NTP value is used in periodic RTCP sender reports.The media track to send on.
The RTP packet to send.
Absolute NTP timestamp of the packet.
Non-nil on write failure.
WritePacketRTCP
The media track to send on.
The RTCP packet to send.
Non-nil on write failure.
OnPacketRTP
OnPacketRTPFunc has signature func(*rtp.Packet). Must be called after Setup() and before Play().The media track to listen on.
The specific codec format to receive packets for.
Callback invoked for each received RTP packet:
func(*rtp.Packet).OnPacketRTPAny
OnPacketRTPAnyFunc has signature func(*description.Media, format.Format, *rtp.Packet).Callback invoked for each received RTP packet with media and format context.
OnPacketRTCP
OnPacketRTCPFunc has signature func(rtcp.Packet).The media track to listen on.
Callback invoked for each received RTCP packet.
OnPacketRTCPAny
OnPacketRTCPAnyFunc has signature func(*description.Media, rtcp.Packet).Callback invoked for each received RTCP packet with media context.
Timestamps
Timestamps
PacketPTS
rtptime.GlobalDecoder.The media track the packet belongs to.
The inbound RTP packet.
The PTS in media clock units.
true if the PTS could be computed, false if not enough information is available yet.PacketNTP
The media track the packet belongs to.
The inbound RTP packet.
The absolute NTP timestamp.
true if the NTP could be derived from RTCP sender reports, false otherwise.Introspection
Introspection
Transport
ClientTransport contains:Conn ConnTransport— connection transport, withTunnelfield (TunnelNone,TunnelHTTP,TunnelWebSocket).Session *SessionTransport— session transport (non-nil afterSetup()), withProtocol(ProtocolUDP,ProtocolUDPMulticast,ProtocolTCP) andProfilefields.
Stats
*ClientStats struct contains:| Field | Type | Description |
|---|---|---|
Conn.InboundBytes | uint64 | Total bytes received on the TCP connection |
Conn.OutboundBytes | uint64 | Total bytes sent on the TCP connection |
Session.InboundBytes | uint64 | Total inbound media bytes |
Session.InboundRTPPackets | uint64 | RTP packets received and processed |
Session.InboundRTPPacketsLost | uint64 | Lost inbound RTP packets |
Session.InboundRTPPacketsInError | uint64 | Inbound RTP packets that could not be processed |
Session.InboundRTPPacketsJitter | float64 | Mean jitter of inbound RTP packets |
Session.InboundRTCPPackets | uint64 | RTCP packets received and processed |
Session.OutboundBytes | uint64 | Total outbound media bytes |
Session.OutboundRTPPackets | uint64 | RTP packets sent |
Session.OutboundRTPPacketsReportedLost | uint64 | RTP packets reported lost by the remote |
Session.OutboundRTCPPackets | uint64 | RTCP packets sent |
Session.Medias | map[*description.Media]SessionStatsMedia | Per-media statistics |
Callback type aliases
| Type | Signature | Used by |
|---|---|---|
ClientOnRequestFunc | func(*base.Request) | OnRequest, OnServerRequest |
ClientOnResponseFunc | func(*base.Response) | OnResponse, OnServerResponse |
ClientOnTransportSwitchFunc | func(err error) | OnTransportSwitch |
ClientOnPacketsLostFunc | func(lost uint64) | OnPacketsLost |
ClientOnDecodeErrorFunc | func(err error) | OnDecodeError |
OnPacketRTPFunc | func(*rtp.Packet) | OnPacketRTP |
OnPacketRTPAnyFunc | func(*description.Media, format.Format, *rtp.Packet) | OnPacketRTPAny |
OnPacketRTCPFunc | func(rtcp.Packet) | OnPacketRTCP |
OnPacketRTCPAnyFunc | func(*description.Media, rtcp.Packet) | OnPacketRTCPAny |