Skip to main content
The Client struct is the main entry point for connecting to RTSP servers. It operates in two primary modes:
  • Play mode — read RTP/RTCP media streams from a server.
  • Record mode — publish RTP media streams to a server.
Both modes share the same struct. You configure fields before calling Start() or one of its shorthand helpers, then register packet callbacks and enter the active state.

Client fields

All fields are optional unless noted. Zero values produce the documented defaults.

Connection target

FieldTypeDescription
SchemestringURL scheme: "rtsp" or "rtsps". Set automatically by StartRecording.
HoststringHost and port of the server (e.g. "localhost:8554"). Set automatically by StartRecording.

RTSP parameters

FieldTypeDefaultDescription
ReadTimeouttime.Duration10sTimeout for read operations.
WriteTimeouttime.Duration10sTimeout for write operations.
TLSConfig*tls.ConfignilTLS configuration for RTSPS connections.
TunnelTunnelTunnelNoneTunneling method: TunnelNone, TunnelHTTP, or TunnelWebSocket.
Protocol*Protocolnil (auto)Transport protocol. nil means automatic: try UDP first, fall back to TCP.
AnyPortEnableboolfalseAccept server UDP ports that differ from the announced ones.
InitialUDPReadTimeouttime.Duration3sHow long to wait for the first UDP packet before switching to TCP.
UDPReadBufferSizeintOS defaultUDP receive buffer size. Increase to reduce packet loss on high-bitrate streams.
WriteQueueSizeint256Outbound packet queue depth. Must be a power of two.
MaxPacketSizeint1472Maximum RTP/RTCP packet size (must be ≤ IPv4/UDP MTU of 1472 bytes).
UserAgentstring"gortsplib"Value sent in the User-Agent header.
DisableRTCPSenderReportsboolfalseSuppress automatic RTCP sender report generation.
RequestBackChannelsboolfalseExplicitly request ONVIF back channels from the server.
UDPSourcePortRange[2]uint16[10000, 65535]Source port range for outbound UDP packets.

Callbacks

FieldSignatureDescription
OnRequestfunc(*base.Request)Called when sending a request to the server.
OnResponsefunc(*base.Response)Called when receiving a response from the server.
OnTransportSwitchfunc(err error)Called when the library switches transport (e.g. UDP → TCP).
OnPacketsLostfunc(lost uint64)Called when the client detects lost RTP packets.
OnDecodeErrorfunc(err error)Called on non-fatal decode errors.

Key methods

MethodDescription
Start() errorConnect to the server. Must be called before Describe, SetupAll, or Announce.
Close()Close all resources and wait for cleanup to finish.
Wait() errorBlock until a fatal error occurs or Close() is called. Returns the error.
Describe(u) (*description.Session, ...)Send DESCRIBE and retrieve the session description.
SetupAll(baseURL, medias) errorSend SETUP for every media in the list.
Play(range) errorEnter play state and start receiving packets.
Pause() errorPause playback or recording without disconnecting.
Record() errorRe-enter record state after a pause.
StartRecording(url, desc) errorShorthand: Start + Announce + SetupAll + Record in one call.
WritePacketRTP(media, pkt) errorSend an RTP packet (record mode).
OnPacketRTPAny(func)Register a callback for all incoming RTP packets (play mode).
OnPacketRTCPAny(func)Register a callback for all incoming RTCP packets (play mode).
PacketPTS(media, pkt)Return the presentation timestamp of an RTP packet.
PacketNTP(media, pkt)Return the absolute NTP timestamp of an RTP packet, if available.

What’s next

Playing streams

Read RTP media from an RTSP server with callbacks and pause/resume.

Recording streams

Publish RTP media to an RTSP server using StartRecording.

Transport options

Choose between UDP, TCP, and UDP-multicast transports.

Security

RTSPS, SRTP, credentials, and self-signed certificates.