Skip to main content
The server dispatches incoming RTSP messages to your handler struct by checking which ServerHandler* interfaces it implements. Each interface defines a single method. Implement only the interfaces relevant to your use case.

Handler interfaces

Connection lifecycle

Called when a new TCP connection is accepted by the server.
Called when a connection is closed, whether by the client, the server, or an error.

Session lifecycle

Called when a new RTSP session is created. Sessions are created lazily on the first SETUP or ANNOUNCE request.
Called when a session is closed. Use this to tear down any resources associated with the session — for example, closing a ServerStream when the publisher disconnects.

Request/response logging

Called for every raw RTSP request received on a connection, before the server processes it. Useful for logging or inspection.
Called for every RTSP response sent by the server. Useful for logging or inspection.

RTSP methods

Called when a client sends a DESCRIBE request. Readers call this to fetch the stream description (SDP) before calling SETUP.Return the *ServerStream the client should be offered, or nil with StatusNotFound if no stream is available.
Called when a publisher sends an ANNOUNCE request. This is where you create a ServerStream for the incoming publication.
Called for every SETUP request. Both readers and publishers send SETUP. Use ctx.Session.State() to distinguish them:
  • ServerSessionStatePreRecord — the session is setting up a track to publish.
  • ServerSessionStateInitial or ServerSessionStatePrePlay — the session is setting up a track to read.
Return the *ServerStream for readers. For publishers, return nil.
Called when a reader sends a PLAY request, after all tracks are set up. The server begins delivering packets to the reader.
Called when a publisher sends a RECORD request. Register packet callbacks here using ctx.Session.OnPacketRTPAny or ctx.Session.OnPacketRTP.
Called when a reader or publisher sends a PAUSE request. Packet delivery is suspended until the next PLAY or RECORD.
Called for GET_PARAMETER and SET_PARAMETER requests. These are often used by clients as keepalives.

Error and diagnostic callbacks

Called when the server detects lost RTP packets from a publisher, based on RTCP receiver reports.
Called when a non-fatal decode error occurs while processing incoming RTP packets. The session continues.
Called when a ServerStream fails to deliver a packet to a particular reader session. The reader is disconnected automatically; this callback is informational.

ServerStream

ServerStream holds the stream description and distributes packets to all readers. You create one in OnAnnounce (for live relay) or before starting the server (for file serving).
Call stream.Close() when the publisher disconnects. This immediately disconnects all active readers.

Session states

ServerSession.State() returns the current state of a session. The most common use is distinguishing a publisher from a reader inside OnSetup.

Full example: relay publisher to readers

This example accepts a single publisher and forwards all incoming RTP packets to every connected reader.
server/main.go

Example: serve H264 from disk

This example creates a ServerStream before accepting connections and streams a MPEG-TS file to all readers.
server-play-format-h264-from-disk/main.go
When serving a pre-built stream (not relay), hold the mutex locked during setup so no client can send a DESCRIBE before the stream is ready. Release it once Initialize() succeeds.