Skip to main content
Server is a RTSP server. It accepts connections from clients, manages sessions, and dispatches events to a user-supplied handler.

Struct fields

Network configuration

string
required
The TCP address the server listens on (e.g. ":8554"). Also used for TCP transport. This field is required.
string
Address for the UDP RTP listener (e.g. ":8000"). Must be set together with UDPRTCPAddress to enable UDP transport. The RTP port must be even.
string
Address for the UDP RTCP listener (e.g. ":8001"). Must be set together with UDPRTPAddress. The RTCP port must equal UDPRTPPort + 1.
string
CIDR range of multicast IPs to assign (e.g. "224.1.0.0/16"). Must be set together with MulticastRTPPort and MulticastRTCPPort to enable UDP-multicast transport.
int
Port for multicast RTP packets. Must be even.
int
Port for multicast RTCP packets. Must equal MulticastRTPPort + 1.

Timeouts

time.Duration
default:"10s"
Timeout for read operations.
time.Duration
default:"10s"
Timeout for write operations.
time.Duration
default:"60s"
Read timeout for idle connections and sessions with no activity.

TLS

*tls.Config
TLS configuration. When set, the server accepts RTSPS connections and enforces SRTP for UDP transport.

I/O tuning

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 per session. 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).
bool
default:"false"
Disable automatic generation and sending of RTCP sender reports to readers.

Authentication

[]auth.VerifyMethod
default:"[Basic, DigestMD5]"
Allowed authentication methods. Defaults to [auth.VerifyMethodBasic, auth.VerifyMethodDigestMD5]. DigestSHA256 is omitted by default because it prevents FFmpeg from authenticating.

Handler

ServerHandler
An object that implements one or more of the ServerHandler* interfaces to handle server events. See Handler interfaces below.

System functions

func(network, address string) (net.Listener, error)
default:"net.Listen"
Function used to create the TCP listener. Override for custom networking.
func(network, address string) (net.PacketConn, error)
default:"net.ListenPacket"
Function used to create UDP listeners.

Methods

Start

Starts the server. Validates configuration, opens all network listeners (TCP, UDP RTP/RTCP, multicast), and spawns the internal event loop goroutine.
error
Non-nil if configuration is invalid or listeners cannot be opened.

StartAndWait

Starts the server and blocks until a fatal error occurs or Close() is called. Equivalent to calling Start() then Wait().
error
The terminal error, or nil if closed cleanly.

Wait

Blocks until all server resources have exited. Returns the error that caused the server to stop.
error
The terminal error, or nil if closed cleanly.

Close

Closes all server resources (listeners, connections, sessions) and waits for them to exit.

NetListener

Returns the underlying net.Listener for the TCP RTSP socket. Useful for obtaining the dynamically assigned port when RTSPAddress uses port 0.

Handler interfaces

The Handler field accepts any value that implements one or more of the following interfaces. Implement only the interfaces relevant to your use case.

Connection events

Called when a new connection is opened.Context struct ServerHandlerOnConnOpenCtx:
Called when a connection is closed.Context struct ServerHandlerOnConnCloseCtx:
Called for every inbound request and outbound response on a connection. Useful for logging or debugging.

Session events

Called when a new RTSP session is created.Context struct ServerHandlerOnSessionOpenCtx:
Called when a session is closed.Context struct ServerHandlerOnSessionCloseCtx:

RTSP method handlers

Called when a client sends a DESCRIBE request. Return the response and the ServerStream to use. The server serializes the stream description into the response body automatically.Context struct ServerHandlerOnDescribeCtx:
Called when a publisher sends an ANNOUNCE request.Context struct ServerHandlerOnAnnounceCtx:
Called when a client sends a SETUP request. Return the ServerStream to associate the session with it as a reader. Return nil for the stream when handling publishers.Context struct ServerHandlerOnSetupCtx:
Called when a reader sends a PLAY request.Context struct ServerHandlerOnPlayCtx:
Called when a publisher sends a RECORD request.Context struct ServerHandlerOnRecordCtx:
Called when a client sends a PAUSE request.Context struct ServerHandlerOnPauseCtx:
Called for GET_PARAMETER and SET_PARAMETER requests respectively. GET_PARAMETER is also used as a keepalive ping; the server responds with 200 OK automatically if this interface is not implemented.Both context structs contain Session, Conn, Request, Path, and Query fields.

Error and diagnostic handlers

Called when the server detects lost inbound RTP packets (recording sessions).Context struct ServerHandlerOnPacketsLostCtx:
Called when a non-fatal packet decode error occurs.Context struct ServerHandlerOnDecodeErrorCtx:
Called when a ServerStream fails to write packets to a reader session. Defaults to logging the error if not implemented.Context struct ServerHandlerOnStreamWriteErrorCtx: