ServerHandler* interfaces it implements. Each interface defines a single method. Implement only the interfaces relevant to your use case.
Handler interfaces
Connection lifecycle
ServerHandlerOnConnOpen
ServerHandlerOnConnOpen
Called when a new TCP connection is accepted by the server.
ServerHandlerOnConnClose
ServerHandlerOnConnClose
Called when a connection is closed, whether by the client, the server, or an error.
Session lifecycle
ServerHandlerOnSessionOpen
ServerHandlerOnSessionOpen
Called when a new RTSP session is created. Sessions are created lazily on the first SETUP or ANNOUNCE request.
ServerHandlerOnSessionClose
ServerHandlerOnSessionClose
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
ServerHandlerOnRequest
ServerHandlerOnRequest
Called for every raw RTSP request received on a connection, before the server processes it. Useful for logging or inspection.
ServerHandlerOnResponse
ServerHandlerOnResponse
Called for every RTSP response sent by the server. Useful for logging or inspection.
RTSP methods
ServerHandlerOnDescribe
ServerHandlerOnDescribe
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.ServerHandlerOnAnnounce
ServerHandlerOnAnnounce
Called when a publisher sends an
ANNOUNCE request. This is where you create a ServerStream for the incoming publication.ServerHandlerOnSetup
ServerHandlerOnSetup
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.ServerSessionStateInitialorServerSessionStatePrePlay— the session is setting up a track to read.
*ServerStream for readers. For publishers, return nil.ServerHandlerOnPlay
ServerHandlerOnPlay
Called when a reader sends a
PLAY request, after all tracks are set up. The server begins delivering packets to the reader.ServerHandlerOnRecord
ServerHandlerOnRecord
Called when a publisher sends a
RECORD request. Register packet callbacks here using ctx.Session.OnPacketRTPAny or ctx.Session.OnPacketRTP.ServerHandlerOnPause
ServerHandlerOnPause
Called when a reader or publisher sends a
PAUSE request. Packet delivery is suspended until the next PLAY or RECORD.ServerHandlerOnGetParameter / ServerHandlerOnSetParameter
ServerHandlerOnGetParameter / ServerHandlerOnSetParameter
Called for
GET_PARAMETER and SET_PARAMETER requests. These are often used by clients as keepalives.Error and diagnostic callbacks
ServerHandlerOnPacketsLost
ServerHandlerOnPacketsLost
Called when the server detects lost RTP packets from a publisher, based on RTCP receiver reports.
ServerHandlerOnDecodeError
ServerHandlerOnDecodeError
Called when a non-fatal decode error occurs while processing incoming RTP packets. The session continues.
ServerHandlerOnStreamWriteError
ServerHandlerOnStreamWriteError
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 aServerStream before accepting connections and streams a MPEG-TS file to all readers.
server-play-format-h264-from-disk/main.go