Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/bluenviron/gortsplib/llms.txt

Use this file to discover all available pages before exploring further.

ServerStream represents a media stream on the server side. It stores the stream description, manages SSRC allocation, distributes packets to all active reader sessions, and optionally manages multicast listeners. A ServerStream is created by the application and shared between the publisher session and all reader sessions. The publisher writes packets into the stream via WritePacketRTP / WritePacketRTCP, and the stream fans the packets out to every active reader.

Struct fields

Server
*Server
required
The server instance this stream belongs to. Must be set before calling Initialize().
Desc
*description.Session
required
The session description (SDP) that describes the media contained in this stream. Must be set before calling Initialize().
MulticastParams
map[*description.Media]StreamMediaMulticastParams
Optional per-media multicast configuration. When set, the specified IP and ports are used instead of the server’s auto-assigned multicast addresses.StreamMediaMulticastParams fields:
FieldTypeDescription
IPnet.IPMulticast group IP
RTPPortintMulticast RTP port
RTCPPortintMulticast RTCP port

Methods

Initialize

func (st *ServerStream) Initialize() error
Initializes the stream. Allocates local SSRCs for each media format, sets up SRTP contexts when the parent server uses TLS, and prepares internal data structures. Must be called after setting Server and Desc, and after the server has been started with Server.Start().
error
error
Non-nil if the server is not present or not yet initialized, or if SSRC/SRTP setup fails.

Close

func (st *ServerStream) Close()
Closes the stream. All reader sessions currently subscribed to this stream are closed immediately. After Close() is called, subsequent WritePacketRTP / WritePacketRTCP calls return ErrServerStreamClosed.

WritePacketRTP

func (st *ServerStream) WritePacketRTP(medi *description.Media, pkt *rtp.Packet) error
Writes an RTP packet to all active readers of the stream. Uses the current wall clock as the NTP timestamp for RTCP sender reports. Thread-safe.
medi
*description.Media
required
The media track the packet belongs to. Must be one of the medias in Desc.
pkt
*rtp.Packet
required
The RTP packet to distribute.
error
error
ErrServerStreamClosed if the stream has been closed; otherwise a write error.

WritePacketRTPWithNTP

func (st *ServerStream) WritePacketRTPWithNTP(
    medi *description.Media,
    pkt *rtp.Packet,
    ntp time.Time,
) error
Like WritePacketRTP but accepts an explicit NTP timestamp. The NTP value is included in periodic RTCP sender reports sent to readers. Use this when you have an accurate external clock source.
medi
*description.Media
required
The media track the packet belongs to.
pkt
*rtp.Packet
required
The RTP packet to distribute.
ntp
time.Time
required
Absolute NTP timestamp of the packet.
error
error
ErrServerStreamClosed if the stream has been closed; otherwise a write error.

WritePacketRTCP

func (st *ServerStream) WritePacketRTCP(medi *description.Media, pkt rtcp.Packet) error
Writes an RTCP packet to all active readers of the stream. Thread-safe.
medi
*description.Media
required
The media track the packet belongs to.
pkt
rtcp.Packet
required
The RTCP packet to distribute.
error
error
ErrServerStreamClosed if the stream has been closed; otherwise a write error.

Stats

func (st *ServerStream) Stats() *ServerStreamStats
Returns a snapshot of stream statistics.ServerStreamStats fields:
FieldTypeDescription
OutboundBytesuint64Total bytes written to readers
OutboundRTPPacketsuint64Total RTP packets distributed
OutboundRTCPPacketsuint64Total RTCP packets distributed
Mediasmap[*description.Media]ServerStreamStatsMediaPer-media statistics
ServerStreamStatsMedia fields:
FieldTypeDescription
OutboundBytesuint64Bytes written for this media
OutboundRTCPPacketsuint64RTCP packets distributed for this media
Formatsmap[format.Format]ServerStreamStatsFormatPer-format statistics
ServerStreamStatsFormat fields:
FieldTypeDescription
OutboundRTPPacketsuint64RTP packets distributed for this format
LocalSSRCuint32Locally assigned SSRC for this format

Usage example

// Create stream after server is started
stream := &gortsplib.ServerStream{
    Server: server,
    Desc:   sessionDesc, // *description.Session from SDP
}
err := stream.Initialize()
if err != nil {
    panic(err)
}
defer stream.Close()

// In OnDescribe and OnSetup handlers, return this stream.
// The server will add reader sessions to it automatically.

// Write packets from a publisher goroutine:
err = stream.WritePacketRTP(media, rtpPacket)
if err != nil {
    log.Println(err)
}
The ServerStream is not associated with any particular publisher connection. It is the application’s responsibility to create the stream when a publisher arrives (e.g. in OnAnnounce) and close it when the publisher leaves (e.g. in OnSessionClose).

Build docs developers (and LLMs) love