codec

package
v0.0.0-...-392cdac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package codec defines codec types and configurations for libgowebrtc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanonicalH264FMTP

func CanonicalH264FMTP(profile H264Profile, packetizationMode int) string

CanonicalH264FMTP constructs a canonical H264 fmtp line.

func CanonicalVP9FMTP

func CanonicalVP9FMTP(profile VP9Profile) string

CanonicalVP9FMTP constructs a canonical VP9 fmtp line.

func CanonicalizeFMTP

func CanonicalizeFMTP(line string) string

CanonicalizeFMTP renders an SDP fmtp line in stable key order.

func CanonicalizeFMTPMap

func CanonicalizeFMTPMap(values map[string]string) string

CanonicalizeFMTPMap renders an SDP fmtp map in stable key order.

func CanonicalizeH264FMTP

func CanonicalizeH264FMTP(line string) string

CanonicalizeH264FMTP returns the canonical ordering used across the repo.

func CanonicalizeVP9FMTP

func CanonicalizeVP9FMTP(line string) string

CanonicalizeVP9FMTP returns the stable VP9 fmtp representation.

func H264FMTPMatches

func H264FMTPMatches(a, b string) bool

H264FMTPMatches compares H264 fmtp lines by profile-level-id and packetization-mode.

func H264PacketizationModeFromFMTP

func H264PacketizationModeFromFMTP(line string) int

H264PacketizationModeFromFMTP returns the H264 packetization-mode.

func ParseFMTP

func ParseFMTP(line string) map[string]string

ParseFMTP parses an SDP fmtp line into a canonical key/value map. Keys are lower-cased and surrounding whitespace is removed.

func VP9FMTPMatches

func VP9FMTPMatches(a, b string) bool

VP9FMTPMatches compares VP9 fmtp lines by profile-id.

Types

type AV1Config

type AV1Config struct {
	// Required
	Width  int
	Height int // Height is the target frame height in pixels.

	// Bitrate control
	Bitrate     uint32          // Target bitrate in bps
	MaxBitrate  uint32          // Max bitrate for VBR
	RateControl RateControlMode // RateControl selects CBR, VBR, or CQ mode.

	// Quality
	FPS         float64    // Target framerate
	KeyInterval int        // Keyframe interval in frames
	Profile     AV1Profile // AV1 profile
	CQ          int        // Quality level for CQ mode (0-63)
	Speed       int        // Encoding speed (0-10, higher = faster)

	// Features
	Threads       int  // Encoding threads
	TileColumns   int  // Tile columns (log2)
	TileRows      int  // Tile rows (log2)
	FrameParallel bool // Enable frame parallel features
	LowDelay      bool // Low latency mode
	PreferHW      bool // Prefer hardware encoder
	ScreenContent bool // Optimize for screen content

	// SVC/Simulcast (AV1 has excellent native SVC support)
	SVC *SVCConfig // SVC configuration (nil = disabled, use SVCPreset* helpers)
}

AV1Config contains AV1 encoder configuration.

func DefaultAV1Config

func DefaultAV1Config(width, height int) AV1Config

DefaultAV1Config returns a convenience preset for AV1.

type AV1Profile

type AV1Profile int

AV1Profile represents AV1 profiles.

const (
	AV1ProfileMain         AV1Profile = 0 // 8/10-bit 4:2:0
	AV1ProfileHigh         AV1Profile = 1 // 8/10-bit 4:4:4
	AV1ProfileProfessional AV1Profile = 2 // 8/10/12-bit, all subsampling
)

AV1Profile values identify the standard AV1 bitstream profiles.

type H264Config

type H264Config struct {
	// Required
	Width  int
	Height int // Height is the target frame height in pixels.

	// Bitrate control
	Bitrate     uint32          // Target bitrate in bps (0 = auto based on resolution)
	MaxBitrate  uint32          // Max bitrate for VBR mode (0 = 1.5x Bitrate)
	MinBitrate  uint32          // Min bitrate for VBR mode (0 = 0.5x Bitrate)
	RateControl RateControlMode // RateControl selects CBR, VBR, or CQ mode.

	// Quality
	FPS         float64     // Target framerate (0 = 30)
	KeyInterval int         // Keyframe interval in frames (0 = 2 seconds worth)
	Profile     H264Profile // H.264 profile (empty = ConstrainedBaseline)
	Level       string      // H.264 level (empty = auto)
	CRF         int         // Quality for CQ mode (0-51, lower = better, 0 = lossless)

	// Performance
	Threads     int  // Encoding threads (0 = auto)
	LowDelay    bool // Optimize for low latency
	ZeroLatency bool // Ultra low latency mode (disables B-frames, lookahead)
	PreferHW    bool // Prefer hardware encoder if available

	// Simulcast (H.264 doesn't support true SVC, only simulcast)
	Simulcast *SVCConfig // Simulcast configuration (nil = disabled)
}

H264Config contains H.264 encoder configuration.

func DefaultH264Config

func DefaultH264Config(width, height int) H264Config

DefaultH264Config returns a convenience preset for H.264.

func (H264Config) FPSOrDefault

func (c H264Config) FPSOrDefault() float64

FPSOrDefault returns FPS or default value.

type H264FMTP

type H264FMTP struct {
	LevelAsymmetryAllowed string // LevelAsymmetryAllowed mirrors the SDP level-asymmetry-allowed flag.
	PacketizationMode     int    // PacketizationMode is the SDP packetization-mode value.
	ProfileLevelID        string // ProfileLevelID is the canonical six-byte profile-level-id value.
}

H264FMTP captures the H264 fmtp parameters that influence compatibility.

func ParseH264FMTP

func ParseH264FMTP(line string) H264FMTP

ParseH264FMTP parses H264-specific fmtp parameters.

type H264Profile

type H264Profile string

H264Profile represents H.264 profile levels.

const (
	H264ProfileBaseline        H264Profile = "42001f" // Baseline Level 3.1
	H264ProfileConstrainedBase H264Profile = "42e01f" // Constrained Baseline Level 3.1
	H264ProfileMain            H264Profile = "4d001f" // Main Level 3.1
	H264ProfileHigh            H264Profile = "64001f" // High Level 3.1
	H264ProfileHigh10          H264Profile = "6e001f" // High 10 Level 3.1
)

H264Profile values identify canonical H.264 profile-level-id prefixes.

func H264ProfileFromFMTP

func H264ProfileFromFMTP(line string) (H264Profile, bool)

H264ProfileFromFMTP returns the H264 profile-level-id prefix when present.

type OpusApplication

type OpusApplication int

OpusApplication specifies the Opus encoder application type.

const (
	OpusApplicationVoIP     OpusApplication = 2048 // Voice over IP (speech)
	OpusApplicationAudio    OpusApplication = 2049 // Audio (music, mixed content)
	OpusApplicationLowDelay OpusApplication = 2051 // Low delay audio
)

OpusApplication values select Opus tuning for speech, music, or low delay.

type OpusBandwidth

type OpusBandwidth int

OpusBandwidth specifies the audio bandwidth.

const (
	OpusBandwidthAuto      OpusBandwidth = -1000 // Auto-detect
	OpusBandwidthNarrow    OpusBandwidth = 1101  // 4kHz (narrowband)
	OpusBandwidthMedium    OpusBandwidth = 1102  // 6kHz (medium band)
	OpusBandwidthWide      OpusBandwidth = 1103  // 8kHz (wideband)
	OpusBandwidthSuperWide OpusBandwidth = 1104  // 12kHz (super wideband)
	OpusBandwidthFull      OpusBandwidth = 1105  // 20kHz (fullband)
)

OpusBandwidth values select the Opus encoder audio bandwidth hint.

type OpusConfig

type OpusConfig struct {
	// Audio format
	SampleRate int // 8000, 12000, 16000, 24000, or 48000
	Channels   int // 1 (mono) or 2 (stereo)

	// Bitrate
	Bitrate     uint32 // Target bitrate in bps (6000-510000)
	VBR         bool   // Variable bitrate (default: true)
	Constrained bool   // Constrained VBR (useful for streaming)

	// Quality
	Application OpusApplication // VoIP, Audio, or LowDelay
	Bandwidth   OpusBandwidth   // Audio bandwidth
	Complexity  int             // Encoding complexity (0-10, higher = better quality)
	FrameSize   float64         // Frame size in ms: 2.5, 5, 10, 20, 40, 60

	// Features
	FEC        bool // Forward Error Correction
	DTX        bool // Discontinuous transmission (silence suppression)
	InBandFEC  bool // In-band FEC for packet loss recovery
	PacketLoss int  // Expected packet loss percentage (for FEC tuning)
}

OpusConfig contains Opus encoder configuration.

func DefaultOpusConfig

func DefaultOpusConfig() OpusConfig

DefaultOpusConfig returns sensible defaults for Opus.

type RateControlMode

type RateControlMode int

RateControlMode specifies the encoder rate control strategy.

const (
	RateControlCBR RateControlMode = iota // Constant bitrate
	RateControlVBR                        // Variable bitrate
	RateControlCQ                         // Constant quality (CRF/CQ mode)
)

RateControlMode values control encoder bitrate behavior.

type SVCConfig

type SVCConfig struct {
	Mode   SVCMode          // SVC/simulcast mode
	Layers []SVCLayerConfig // Per-layer configuration. Simulcast helpers require one explicit entry per spatial layer.
}

SVCConfig configures scalable video coding.

func SVCPresetChrome

func SVCPresetChrome() *SVCConfig

SVCPresetChrome returns Chrome's default SVC for VP9/AV1. L3T3_KEY is Chrome's preferred mode for WebRTC.

func SVCPresetFirefox

func SVCPresetFirefox() *SVCConfig

SVCPresetFirefox returns Firefox's typical SVC configuration. Firefox tends to use L2T3 with inter-layer prediction.

func SVCPresetLowLatency

func SVCPresetLowLatency() *SVCConfig

SVCPresetLowLatency returns optimal SVC for low-latency use cases. Uses L1T2 temporal layers for bandwidth adaptation without spatial complexity.

func SVCPresetNone

func SVCPresetNone() *SVCConfig

SVCPresetNone returns no SVC configuration.

func SVCPresetSFU

func SVCPresetSFU() *SVCConfig

SVCPresetSFU returns optimal SVC for SFU/media server usage. Uses L3T3_KEY (K-SVC) - best for selective forwarding without transcoding.

func SVCPresetSFULite

func SVCPresetSFULite() *SVCConfig

SVCPresetSFULite returns lighter SVC for SFU with 2 spatial layers. Uses L2T3_KEY - good balance of adaptability and complexity.

func SVCPresetScreenShare

func SVCPresetScreenShare() *SVCConfig

SVCPresetScreenShare returns optimal SVC for screen sharing (temporal only). Uses L1T3 for smooth motion at varying framerates.

func SVCPresetSimulcast

func SVCPresetSimulcast() *SVCConfig

SVCPresetSimulcast returns classic simulcast (3 independent streams). Best compatibility, highest bandwidth usage.

func SVCPresetSimulcastLite

func SVCPresetSimulcastLite() *SVCConfig

SVCPresetSimulcastLite returns 2-stream simulcast. Good for mobile or bandwidth-constrained scenarios.

type SVCLayerConfig

type SVCLayerConfig struct {
	RID        string  // RID identifies the simulcast RTP stream. Leave empty for non-simulcast SVC-only layers.
	Width      int     // Resolution width in pixels.
	Height     int     // Resolution height in pixels.
	Bitrate    uint32  // Target bitrate for this layer
	MaxBitrate uint32  // Max bitrate for this layer
	FPS        float64 // Framerate for this layer (0 = same as base)
	Active     bool    // Whether this layer is active
}

SVCLayerConfig configures a single SVC/simulcast layer.

type SVCMode

type SVCMode int

SVCMode specifies the scalable video coding mode. L = spatial Layers, T = Temporal layers, S = Simulcast, K = Key-frame dependent

const (
	SVCModeNone SVCMode = iota // No SVC

	// Standard SVC (inter-layer prediction)
	SVCModeL1T1 // 1 spatial, 1 temporal layer (no SVC)
	SVCModeL1T2 // 1 spatial, 2 temporal layers
	SVCModeL1T3 // 1 spatial, 3 temporal layers
	SVCModeL2T1 // 2 spatial, 1 temporal layer
	SVCModeL2T2 // 2 spatial, 2 temporal layers
	SVCModeL2T3 // 2 spatial, 3 temporal layers
	SVCModeL3T1 // 3 spatial, 1 temporal layer
	SVCModeL3T2 // 3 spatial, 2 temporal layers
	SVCModeL3T3 // 3 spatial, 3 temporal layers (full SVC)

	// K-SVC (Key-frame dependent, no inter-layer prediction - better for SFU)
	SVCModeL1T2_KEY // 1 spatial, 2 temporal (key-frame dependent)
	SVCModeL1T3_KEY // 1 spatial, 3 temporal (key-frame dependent)
	SVCModeL2T1_KEY // 2 spatial, 1 temporal (key-frame dependent)
	SVCModeL2T2_KEY // 2 spatial, 2 temporal (key-frame dependent)
	SVCModeL2T3_KEY // 2 spatial, 3 temporal (key-frame dependent)
	SVCModeL3T1_KEY // 3 spatial, 1 temporal (key-frame dependent)
	SVCModeL3T2_KEY // 3 spatial, 2 temporal (key-frame dependent)
	SVCModeL3T3_KEY // 3 spatial, 3 temporal (key-frame dependent, best for SFU)

	// Simulcast (separate independent encoders)
	SVCModeS2T1 // 2 simulcast streams, 1 temporal
	SVCModeS2T3 // 2 simulcast streams, 3 temporal
	SVCModeS3T1 // 3 simulcast streams, 1 temporal
	SVCModeS3T3 // 3 simulcast streams, 3 temporal (full simulcast)
)

SVCMode values describe supported scalability and simulcast layouts.

func (SVCMode) IsKeyFrameDependent

func (m SVCMode) IsKeyFrameDependent() bool

IsKeyFrameDependent returns true if this is K-SVC mode (no inter-layer prediction).

func (SVCMode) IsSimulcast

func (m SVCMode) IsSimulcast() bool

IsSimulcast returns true if this is a simulcast mode (separate encoders).

func (SVCMode) SpatialLayers

func (m SVCMode) SpatialLayers() int

SpatialLayers returns number of spatial layers.

func (SVCMode) String

func (m SVCMode) String() string

String returns string representation of SVC mode.

func (SVCMode) TemporalLayers

func (m SVCMode) TemporalLayers() int

TemporalLayers returns number of temporal layers.

type Type

type Type int

Type represents a video or audio codec type.

const (
	// Video codecs
	H264 Type = iota
	VP8
	VP9
	AV1

	// Audio codecs
	Opus
	PCMU
	PCMA
)

Type values identify the codecs supported by libgowebrtc.

func ParseMimeType

func ParseMimeType(mimeType string) (Type, bool)

ParseMimeType converts a MIME type string into a codec type. The parser is case-insensitive and accepts values with or without the "audio/" or "video/" prefix.

func (Type) ClockRate

func (t Type) ClockRate() uint32

ClockRate returns the RTP clock rate for the codec.

func (Type) IsAudio

func (t Type) IsAudio() bool

IsAudio returns true if this is an audio codec.

func (Type) IsVideo

func (t Type) IsVideo() bool

IsVideo returns true if this is a video codec.

func (Type) MimeType

func (t Type) MimeType() string

MimeType returns the MIME type for the codec.

func (Type) String

func (t Type) String() string

String returns the string representation of the codec type.

type VP8Config

type VP8Config struct {
	// Required
	Width  int
	Height int // Height is the target frame height in pixels.

	// Bitrate control
	Bitrate     uint32          // Target bitrate in bps
	MaxBitrate  uint32          // Max bitrate for VBR
	RateControl RateControlMode // RateControl selects CBR, VBR, or CQ mode.

	// Quality
	FPS         float64 // Target framerate
	KeyInterval int     // Keyframe interval in frames
	CQ          int     // Quality level for CQ mode (0-63, lower = better)
	Deadline    int     // Encoding deadline: 0=best, 1=good, 2=realtime

	// Performance
	Threads        int  // Encoding threads
	LowDelay       bool // Low latency mode
	PreferHW       bool // Prefer hardware encoder
	ErrorResilient bool // Enable error resilience features
}

VP8Config contains VP8 encoder configuration.

func DefaultVP8Config

func DefaultVP8Config(width, height int) VP8Config

DefaultVP8Config returns a convenience preset for VP8.

type VP9Config

type VP9Config struct {
	// Required
	Width  int
	Height int // Height is the target frame height in pixels.

	// Bitrate control
	Bitrate     uint32          // Target bitrate in bps
	MaxBitrate  uint32          // Max bitrate for VBR
	RateControl RateControlMode // RateControl selects CBR, VBR, or CQ mode.

	// Quality
	FPS         float64    // Target framerate
	KeyInterval int        // Keyframe interval in frames
	Profile     VP9Profile // VP9 profile (0-3)
	CQ          int        // Quality level for CQ mode (0-63)
	Speed       int        // Encoding speed (0-9, higher = faster)

	// Features
	Threads       int  // Encoding threads
	TileColumns   int  // Tile columns (log2)
	TileRows      int  // Tile rows (log2)
	FrameParallel bool // Enable frame parallel decoding
	LowDelay      bool // Low latency mode
	PreferHW      bool // Prefer hardware encoder

	// SVC/Simulcast (VP9 has native SVC support)
	SVC *SVCConfig // SVC configuration (nil = disabled, use SVCPreset* helpers)
}

VP9Config contains VP9 encoder configuration.

func DefaultVP9Config

func DefaultVP9Config(width, height int) VP9Config

DefaultVP9Config returns a convenience preset for VP9.

type VP9Profile

type VP9Profile int

VP9Profile represents VP9 profiles.

const (
	VP9Profile0 VP9Profile = 0 // 8-bit 4:2:0
	VP9Profile1 VP9Profile = 1 // 8-bit 4:2:2/4:4:4
	VP9Profile2 VP9Profile = 2 // 10/12-bit 4:2:0
	VP9Profile3 VP9Profile = 3 // 10/12-bit 4:2:2/4:4:4
)

VP9Profile values identify the standard VP9 bitstream profiles.

func VP9ProfileIDFromFMTP

func VP9ProfileIDFromFMTP(line string) VP9Profile

VP9ProfileIDFromFMTP returns the VP9 profile-id, defaulting to profile 0.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL