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
string
URL scheme. Must be
"rtsp" or "rtsps" (TLS).string
Remote host and port (e.g.
"192.168.1.10:554").RTSP parameters
time.Duration
default:"10s"
Timeout for read operations.
time.Duration
default:"10s"
Timeout for write operations.
*tls.Config
default:"nil"
TLS configuration used when connecting to RTSPS servers.
Tunnel
default:"TunnelNone"
Tunneling method. One of
TunnelNone, TunnelHTTP, or TunnelWebSocket.*Protocol
default:"nil"
Transport protocol (
ProtocolUDP, ProtocolUDPMulticast, or ProtocolTCP). When nil the client first tries UDP, then falls back to TCP automatically.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.
time.Duration
default:"3s"
When reading with UDP, if no packet is received within this timeout the client switches to TCP.
int
default:"OS default"
Size of the UDP read buffer. Increase to reduce packet loss on high-bandwidth streams.
int
default:"256"
Size of the outbound packet queue. Must be a power of two.
int
default:"1472"
Maximum size of outbound RTP/RTCP packets in bytes. Must be less than the IPv4/UDP MTU (1472 bytes).
string
default:"\"gortsplib\""
Value sent in the
User-Agent RTSP header.bool
default:"false"
Disable automatic generation and sending of RTCP sender reports.
bool
default:"false"
Explicitly request back channels from the server (ONVIF extension).
[2]uint16
default:"[10000, 65535]"
Range of local source ports used for outbound UDP packets.
System functions
func(ctx context.Context, network, address string) (net.Conn, error)
default:"(&net.Dialer{}).DialContext"
Function used to open TCP connections. Override for custom networking.
func(network, address string) (net.PacketConn, error)
default:"net.ListenPacket"
Function used to open UDP listeners.
func(network, address string) (*net.IPAddr, error)
default:"net.ResolveIPAddr"
Function used to resolve IP addresses from hostnames.
Callbacks
ClientOnRequestFunc
Called each time the client sends a request to the server. Prototype:
func(*base.Request).ClientOnResponseFunc
Called each time the client receives a response from the server. Prototype:
func(*base.Response).ClientOnRequestFunc
Called each time the client receives a request from the server (e.g. server-initiated OPTIONS). Prototype:
func(*base.Request).ClientOnResponseFunc
Called each time the client sends a response to the server. Prototype:
func(*base.Response).ClientOnTransportSwitchFunc
Called when the transport protocol is switched (e.g. UDP to TCP fallback). Prototype:
func(err error). Defaults to logging the error.ClientOnPacketsLostFunc
Called when the client detects lost RTP packets. Prototype:
func(lost uint64). Defaults to logging the count.ClientOnDecodeErrorFunc
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.error
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.string
required
Full RTSP URL string of the server to publish to.
*description.Session
required
Session description (SDP) of the media to be published.
error
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).error
The close error, or
nil if closed cleanly via Close().Stream negotiation
Stream negotiation
Describe
*base.URL
required
The RTSP URL to describe.
*description.Session
Parsed session description containing media tracks and their formats.
*base.Response
The raw RTSP response.
error
Non-nil on failure.
Announce
Setup() and Record() when publishing.*base.URL
required
The RTSP URL to announce to.
*description.Session
required
Session description of the media to publish.
*base.Response
The raw RTSP response.
error
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.*base.URL
required
The base URL returned from
Describe() (available as desc.BaseURL).[]*description.Media
required
Slice of media descriptions to set up.
error
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.*base.URL
required
The base URL of the stream (from
Describe()).*description.Media
required
The specific media track to set up.
int
Local UDP port for RTP.
0 means auto-assign. Must be even.int
Local UDP port for RTCP.
0 means auto-assign. Must equal rtpPort + 1.*base.Response
The raw RTSP response.
error
Non-nil on failure.
Options
*base.URL
required
The URL to send the OPTIONS request to.
*base.Response
The raw RTSP response containing a
Public header with supported methods.error
Non-nil on failure.
Stream control
Stream control
Play
Setup(). Starts receiving packets.*headers.Range
Playback range. Pass
nil to start from the beginning (NPT 0).*base.Response
The raw RTSP response.
error
Non-nil on failure.
Record
Announce() and Setup(). Starts sending packets.*base.Response
The raw RTSP response.
error
Non-nil on failure.
Pause
Play() or Record(). Suspends the stream without tearing it down.*base.Response
The raw RTSP response.
error
Non-nil on failure.
Packet I/O
Packet I/O
WritePacketRTP
Record().*description.Media
required
The media track to send on.
*rtp.Packet
required
The RTP packet to send.
error
Non-nil on write failure.
WritePacketRTPWithNTP
WritePacketRTP but accepts an explicit NTP timestamp. This NTP value is used in periodic RTCP sender reports.*description.Media
required
The media track to send on.
*rtp.Packet
required
The RTP packet to send.
time.Time
required
Absolute NTP timestamp of the packet.
error
Non-nil on write failure.
WritePacketRTCP
*description.Media
required
The media track to send on.
rtcp.Packet
required
The RTCP packet to send.
error
Non-nil on write failure.
OnPacketRTP
OnPacketRTPFunc has signature func(*rtp.Packet). Must be called after Setup() and before Play().*description.Media
required
The media track to listen on.
format.Format
required
The specific codec format to receive packets for.
OnPacketRTPFunc
required
Callback invoked for each received RTP packet:
func(*rtp.Packet).OnPacketRTPAny
OnPacketRTPAnyFunc has signature func(*description.Media, format.Format, *rtp.Packet).OnPacketRTPAnyFunc
required
Callback invoked for each received RTP packet with media and format context.
OnPacketRTCP
OnPacketRTCPFunc has signature func(rtcp.Packet).*description.Media
required
The media track to listen on.
OnPacketRTCPFunc
required
Callback invoked for each received RTCP packet.
OnPacketRTCPAny
OnPacketRTCPAnyFunc has signature func(*description.Media, rtcp.Packet).OnPacketRTCPAnyFunc
required
Callback invoked for each received RTCP packet with media context.
Timestamps
Timestamps
PacketPTS
rtptime.GlobalDecoder.*description.Media
required
The media track the packet belongs to.
*rtp.Packet
required
The inbound RTP packet.
int64
The PTS in media clock units.
bool
true if the PTS could be computed, false if not enough information is available yet.PacketNTP
*description.Media
required
The media track the packet belongs to.
*rtp.Packet
required
The inbound RTP packet.
time.Time
The absolute NTP timestamp.
bool
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: