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.

Import path: github.com/bluenviron/gortsplib/v5/pkg/description

Session

A Session is the top-level description of an RTSP stream. It maps directly to an SDP session description and holds one or more Media entries.
type Session struct {
    BaseURL      *base.URL
    Title        string
    Multicast    bool
    KeyMgmtMikey *mikey.Message
    FECGroups    []SessionFECGroup
    Medias       []*Media
}

Fields

BaseURL
*base.URL
Base URL of the stream. Populated when the session is received from a server; read-only.
Title
string
Optional human-readable title of the stream. Maps to the SDP s= field.
Multicast
bool
When true, the SDP connection address is set to a multicast address (224.1.0.0).
KeyMgmtMikey
*mikey.Message
Optional MIKEY key-management attribute (a=key-mgmt). Used for SRTP streams.
FECGroups
[]SessionFECGroup
Forward Error Correction groups as defined in RFC 5109. Each group is a slice of media IDs.
Medias
[]*Media
required
The list of media streams in this session. At least one entry is required for a valid SDP.

Methods

FindFormat

func (d *Session) FindFormat(forma any) *Media
Searches all medias in the session for a format of the given type. When found, it stores the format in forma (a pointer to the target format variable) and returns the owning *Media. Returns nil if no matching format is found.
var forma *format.H264
media := desc.FindFormat(&forma)
if media != nil {
    // forma is now populated
}

Marshal

func (d Session) Marshal() ([]byte, error)
Encodes the session as an SDP byte slice.

Unmarshal

func (d *Session) Unmarshal(ssd *sdp.SessionDescription) error
Decodes the session from a parsed SDP session description.

Media

A Media represents a single media stream (e.g., one video track or one audio track) within a session.
type Media struct {
    Type          MediaType
    ID            string
    IsBackChannel bool
    Profile       headers.TransportProfile
    KeyMgmtMikey  *mikey.Message
    Control       string
    Formats       []format.Format
}

Fields

Type
MediaType
required
The type of this media stream: MediaTypeVideo, MediaTypeAudio, or MediaTypeApplication.
ID
string
Optional media identifier (a=mid). Must be alphanumeric. Used when multiple media sections are bundled (RFC 5888).
IsBackChannel
bool
When true, the media is a back channel (e.g., two-way audio on an IP camera). Serialized as a=sendonly in SDP.
Profile
headers.TransportProfile
The RTP profile: TransportProfileAVP (default) or TransportProfileSAVP (for SRTP).
KeyMgmtMikey
*mikey.Message
Optional per-media MIKEY key-management attribute.
Control
string
The a=control attribute value. Used to build the per-media RTSP URL for SETUP requests.
Formats
[]format.Format
required
The RTP payload formats carried by this media stream. At least one format is required.

Methods

FindFormat

func (m Media) FindFormat(forma any) bool
Searches the media’s Formats slice for a format whose concrete type matches the element type of forma. On success, sets *forma and returns true.

URL

func (m Media) URL(contentBase *base.URL) (*base.URL, error)
Returns the absolute RTSP URL for this media, resolved against contentBase (the Content-Base header from the server). Returns an error when contentBase is nil.

Marshal

func (m Media) Marshal() (*psdp.MediaDescription, error)
Encodes the media as a pion SDP MediaDescription.

Unmarshal

func (m *Media) Unmarshal(md *psdp.MediaDescription) error
Decodes the media from a pion SDP MediaDescription.

MediaType

MediaType is a string type used to classify a media stream.
ConstantValueDescription
MediaTypeVideo"video"Video stream
MediaTypeAudio"audio"Audio stream
MediaTypeApplication"application"Application data stream (e.g., metadata, KLV)

SessionFECGroup

type SessionFECGroup []string
A SessionFECGroup is a slice of media IDs that form a Forward Error Correction group (RFC 5109). Groups are serialized as a=group:FEC <id1> <id2> ... in SDP.

Code example

Building a session description for a recording client that sends H.264 video:
import (
    "github.com/bluenviron/gortsplib/v5/pkg/description"
    "github.com/bluenviron/gortsplib/v5/pkg/format"
)

desc := &description.Session{
    Medias: []*description.Media{
        {
            Type: description.MediaTypeVideo,
            Formats: []format.Format{
                &format.H264{
                    PayloadTyp:        96,
                    PacketizationMode: 1,
                },
            },
        },
    },
}
Searching for a specific format across all medias:
var h264Fmt *format.H264
media := desc.FindFormat(&h264Fmt)
if media == nil {
    // no H264 track found
}

Build docs developers (and LLMs) love