Skip to main content
ServerSession represents a server-side RTSP session. A session is created automatically by the server when a client sends a SETUP or ANNOUNCE request. It tracks the session state, the negotiated transport, setupped media tracks, and optional user data. ServerConn represents a single TCP connection to a client. One connection can be associated with at most one session, but a session (when using UDP transport) can be associated with multiple connections.

ServerSessionState

The state of a session progresses through the following values:

ServerSession methods

State

Returns the current state of the session. Thread-safe.
ServerSessionState
One of the ServerSessionState* constants.

Stream

Returns the ServerStream associated with this session (set during SETUP for reading sessions). Returns nil for publishing sessions or before SETUP.

Path

Returns the RTSP path extracted from the SETUP or ANNOUNCE request URL.

Query

Returns the query string extracted from the SETUP or ANNOUNCE request URL.

AnnouncedDescription

Returns the session description provided by a publishing client in the ANNOUNCE request. Returns nil for reading sessions.

Medias

Returns the list of media tracks that have been set up via SETUP, in the order they were set up.

Transport

Returns the negotiated transport details. Non-nil only after SETUP has been called at least once.SessionTransport fields:

Conns

Returns all ServerConn instances currently associated with the session.

SetUserData / UserData

Attach and retrieve arbitrary application data associated with the session. Not thread-safe — should be called only from handler callbacks.

Close

Closes the session. Causes all associated connections to be closed and OnSessionClose to be called. Safe to call from any goroutine.

OnPacketRTP

Registers a callback for inbound RTP packets on a specific media format. Must be called after SETUP. OnPacketRTPFunc has signature func(*rtp.Packet).
*description.Media
required
The media track to listen on.
format.Format
required
The codec format to receive packets for.
OnPacketRTPFunc
required
Callback invoked for each received RTP packet.

OnPacketRTPAny

Registers a callback for inbound RTP packets on all set-up media tracks and formats. OnPacketRTPAnyFunc has signature func(*description.Media, format.Format, *rtp.Packet).
OnPacketRTPAnyFunc
required
Callback invoked with media and format context for each received RTP packet.

OnPacketRTCP

Registers a callback for inbound RTCP packets on a specific media track. 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

Registers a callback for inbound RTCP packets on all set-up media tracks. OnPacketRTCPAnyFunc has signature func(*description.Media, rtcp.Packet).
OnPacketRTCPAnyFunc
required
Callback invoked with media context for each received RTCP packet.

WritePacketRTP

Writes an RTP packet directly to this session. Use ServerStream.WritePacketRTP instead when you want to fan out to all readers.
*description.Media
required
The media track to write to.
*rtp.Packet
required
The RTP packet to send.
error
Non-nil on write failure.

WritePacketRTPWithNTP

Like WritePacketRTP but accepts an explicit NTP timestamp for RTCP sender reports.
*description.Media
required
The media track to write to.
*rtp.Packet
required
The RTP packet to send.
time.Time
required
Absolute NTP timestamp of the packet.
error
Non-nil on write failure.

WritePacketRTCP

Writes an RTCP packet directly to this session.
*description.Media
required
The media track to write to.
rtcp.Packet
required
The RTCP packet to send.
error
Non-nil on write failure.

PacketPTS

Returns the presentation timestamp (PTS) of an inbound RTP packet for recording sessions. Decodes the RTP timestamp and synchronizes it with other tracks.
*description.Media
required
The media track the packet belongs to.
*rtp.Packet
required
The inbound RTP packet.
int64
PTS in media clock units.
bool
true if the PTS could be determined.

PacketNTP

Returns the absolute NTP timestamp of an inbound RTP packet, derived from RTCP sender reports sent by the publisher.
*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.

Stats

Returns a snapshot of session statistics.SessionStats fields:

ServerConn methods

ServerConn is a server-side TCP connection. It is passed to handler callbacks and provides the following public API.

NetConn

Returns the underlying net.Conn. The remote address (RemoteAddr()) gives the client’s IP and port.

Transport

Returns connection transport details. The ConnTransport struct has a single field:

Session

Returns the ServerSession currently associated with this connection, or nil if no session has been established yet.

SetUserData / UserData

Attach and retrieve arbitrary application data associated with the connection.

VerifyCredentials

Verifies the credentials provided in the Authorization header of req against expectedUser and expectedPass. Supports the authentication methods configured in Server.AuthMethods (Basic and/or Digest).Call this inside handler callbacks (e.g. OnDescribe, OnAnnounce) to enforce access control. Returns false and the server will automatically send a 401 Unauthorized with a WWW-Authenticate challenge on the first attempt. Returns false and closes the connection if wrong credentials are provided after a challenge.
*base.Request
required
The incoming RTSP request.
string
required
Expected username. Must not be empty.
string
required
Expected password.
bool
true if credentials are correct, false otherwise.

Stats

Returns a snapshot of connection statistics.ConnStats fields:

Close

Closes the connection. If a session is associated, it is notified and may be closed if no other connections remain.