Skip to main content
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
required
The server instance this stream belongs to. Must be set before calling Initialize().
*description.Session
required
The session description (SDP) that describes the media contained in this stream. Must be set before calling Initialize().
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:

Methods

Initialize

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
Non-nil if the server is not present or not yet initialized, or if SSRC/SRTP setup fails.

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

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.
*description.Media
required
The media track the packet belongs to. Must be one of the medias in Desc.
*rtp.Packet
required
The RTP packet to distribute.
error
ErrServerStreamClosed if the stream has been closed; otherwise a write error.

WritePacketRTPWithNTP

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.
*description.Media
required
The media track the packet belongs to.
*rtp.Packet
required
The RTP packet to distribute.
time.Time
required
Absolute NTP timestamp of the packet.
error
ErrServerStreamClosed if the stream has been closed; otherwise a write error.

WritePacketRTCP

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

Stats

Returns a snapshot of stream statistics.ServerStreamStats fields:ServerStreamStatsMedia fields:ServerStreamStatsFormat fields:

Usage example

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).