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/format

Format interface

Every RTP payload format implements the Format interface:
type Format interface {
    // ClockRate returns the RTP clock rate in Hz.
    ClockRate() int

    // PayloadType returns the RTP payload type number.
    PayloadType() uint8

    // RTPMap returns the rtpmap SDP attribute value (e.g. "H264/90000").
    RTPMap() string

    // FMTP returns the fmtp SDP attribute as a key-value map.
    FMTP() map[string]string

    // PTSEqualsDTS reports whether PTS equals DTS for the given RTP packet.
    // Used to determine whether a packet carries a random-access point.
    PTSEqualsDTS(*rtp.Packet) bool

    // Codec returns a human-readable codec name.
    // Deprecated: arbitrary. Will be removed in the next version.
    Codec() string
}

Method descriptions

ClockRate
int
The RTP clock rate in Hz used for timestamp calculations. For example, 90000 for video formats and 48000 for Opus audio.
PayloadType
uint8
The RTP payload type number (0–127). Dynamic payload types are in the range 96–127. Some formats use static payload type numbers (e.g., PCMU is 0, PCMA is 8).
RTPMap
string
The a=rtpmap SDP attribute value. Returns an empty string for formats with static payload types where rtpmap is optional.
FMTP
map[string]string
The a=fmtp attribute as a parsed key-value map. Returns an empty map when no format parameters are needed.
PTSEqualsDTS
bool
Reports whether the presentation timestamp equals the decode timestamp for the given packet. Returns true on random-access points (e.g., H.264 IDR frames).

Format implementations

StructCodecStatic payload typeDynamic (96–127)Encoder pkgDecoder pkg
H264H.264 / AVC35 (allowed)Yesrtph264rtph264
H265H.265 / HEVCYesrtph265rtph265
AV1AV1Yesrtpav1rtpav1
VP9VP9Yesrtpvp9rtpvp9
VP8VP8Yesrtpvp8rtpvp8
MPEG4VideoMPEG-4 Part 2 videoYes
MPEG1VideoMPEG-1/2 video32Nortpmpeg1videortpmpeg1video
MJPEGMotion JPEG26Nortpmjpegrtpmjpeg
OpusOpus / MultiOpusYesrtpsimpleaudiortpsimpleaudio
MPEG4AudioMPEG-4 Audio (Generic)Yesrtpmpeg4audiortpmpeg4audio
MPEG4AudioLATMMPEG-4 Audio LATMYes
VorbisVorbisYes
AC3AC-3Yesrtpac3rtpac3
G711G.711 PCMU/PCMA0, 8Yesrtpsimpleaudiortpsimpleaudio
G722G.7229Nortpsimpleaudiortpsimpleaudio
G726G.726 (16/24/32/40 kbps)Yes
LPCMLinear PCM (L8/L16/L24)10, 11Yesrtplpcmrtplpcm
MPEG1AudioMPEG-1/2 audio14Nortpmpeg1audiortpmpeg1audio
SpeexSpeexYes
KLVSMPTE 336M KLVYesrtpklvrtpklv
MPEGTSMPEG-TS33Nortpmpegtsrtpmpegts
GenericUnknown / fallbackanyany

Encoder and decoder pattern

Formats that carry complex framing (e.g., H.264 NAL unit packetization) provide CreateEncoder() and CreateDecoder() factory methods. These return codec-specific types from sub-packages under pkg/format/rtp<codec>.

Encoding

encode, err := forma.CreateEncoder()
if err != nil { ... }

// Encode an access unit ([][]byte for H.264) into RTP packets
pkts, err := enc.Encode(accessUnit)
if err != nil { ... }

for _, pkt := range pkts {
    // send pkt over the network
}

Decoding

dec, err := forma.CreateDecoder()
if err != nil { ... }

// Decode a single RTP packet into an access unit
au, pts, err := dec.Decode(pkt)
if err != nil { ... }
// use au ([][]byte for H.264)

H264 struct

H264 is the RTP format for the H.264 codec (RFC 6184).
type H264 struct {
    PayloadTyp        uint8
    SPS               []byte
    PPS               []byte
    PacketizationMode int
}
PayloadTyp
uint8
required
RTP payload type number. Typically 96 for dynamic assignment. The field name is PayloadTyp (not PayloadType) to avoid a collision with the interface method.
SPS
[]byte
H.264 Sequence Parameter Set NAL unit (without Annex B start code). Serialized into the sprop-parameter-sets fmtp attribute.
PPS
[]byte
H.264 Picture Parameter Set NAL unit (without Annex B start code). Serialized alongside SPS in sprop-parameter-sets.
PacketizationMode
int
H.264 packetization mode (0 = single NAL unit mode, 1 = non-interleaved mode). Serialized as the packetization-mode fmtp attribute. Defaults to 0.

H264 methods

MethodSignatureDescription
CreateEncoder() (*rtph264.Encoder, error)Returns an H.264 RTP encoder initialized with the format’s payload type and packetization mode.
CreateDecoder() (*rtph264.Decoder, error)Returns an H.264 RTP decoder.
SafeSetParams(sps []byte, pps []byte)Thread-safe setter for SPS and PPS parameters.
SafeParams() ([]byte, []byte)Thread-safe getter for SPS and PPS parameters.

Example with H264

import (
    "github.com/bluenviron/gortsplib/v5/pkg/format"
    "github.com/bluenviron/gortsplib/v5/pkg/format/rtph264"
)

// Declare the format
forma := &format.H264{
    PayloadTyp:        96,
    PacketizationMode: 1,
}

// Encoding
enc, err := forma.CreateEncoder()
if err != nil { ... }

// accessUnit is a [][]byte of H.264 NAL units
pkts, err := enc.Encode(accessUnit)
if err != nil { ... }

// Decoding
dec, err := forma.CreateDecoder()
if err != nil { ... }

au, pts, err := dec.Decode(pkt)
if err != nil { ... }

Codec-specific encoder/decoder packages

Each sub-package under pkg/format/rtp<codec> exposes Encoder and Decoder structs with codec-specific options:
Sub-packageCodec
pkg/format/rtph264H.264
pkg/format/rtph265H.265
pkg/format/rtpav1AV1
pkg/format/rtpvp8VP8
pkg/format/rtpvp9VP9
pkg/format/rtpmpeg4audioMPEG-4 Audio
pkg/format/rtpac3AC-3
pkg/format/rtplpcmLinear PCM
pkg/format/rtpklvKLV
pkg/format/rtpmjpegMotion JPEG
pkg/format/rtpmpeg1audioMPEG-1 Audio
pkg/format/rtpmpeg1videoMPEG-1 Video
pkg/format/rtpmpegtsMPEG-TS
pkg/format/rtpsimpleaudioG.711, G.722, Opus
The rtpfragmented sub-package provides a generic fragmented-unit encoder/decoder used internally by several codec packages.

Build docs developers (and LLMs) love