encoder

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: 7 Imported by: 0

Documentation

Overview

Package encoder provides video and audio encoder interfaces using libwebrtc.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEncoderClosed    = errors.New("encoder is closed")
	ErrInvalidFrame     = errors.New("invalid frame")
	ErrEncodeFailed     = errors.New("encode failed")
	ErrUnsupportedCodec = errors.New("unsupported codec")
	ErrInvalidConfig    = errors.New("invalid encoder configuration")
	ErrBufferTooSmall   = errors.New("destination buffer too small")
)

Common errors

View Source
var (
	// ErrUnsupportedPixelFormat reports video input that does not use the
	// currently supported encoder pixel layout.
	ErrUnsupportedPixelFormat = errors.New("unsupported pixel format")
	// ErrInvalidFrameLayout reports malformed plane/stride layout in a raw
	// video frame.
	ErrInvalidFrameLayout = errors.New("invalid video frame layout")
)

Functions

func ValidateI420Frame

func ValidateI420Frame(src *frame.VideoFrame) error

ValidateI420Frame ensures a raw video frame is safe to pass into the current libwebrtc-backed encoders, which accept I420 planes.

Types

type AudioEncoder

type AudioEncoder interface {
	// EncodeInto encodes audio samples into the destination buffer.
	// Returns the number of bytes written.
	// Caller must provide a buffer of at least MaxEncodedSize() bytes.
	EncodeInto(src *frame.AudioFrame, dst []byte) (int, error)

	// MaxEncodedSize returns the maximum possible encoded size for a single
	// audio frame. Use this to allocate destination buffers.
	MaxEncodedSize() int

	// SetBitrate updates the target bitrate in bits per second.
	SetBitrate(bps uint32) error

	// Codec returns the codec type of this encoder.
	Codec() codec.Type

	// Close releases all encoder resources.
	Close() error
}

AudioEncoder encodes raw audio samples to compressed bitstream. All operations are allocation-free - caller provides buffers. Encoder instances are stateful; serialize calls per instance and use separate encoders for parallel pipelines.

func NewOpusEncoder

func NewOpusEncoder(cfg codec.OpusConfig) (AudioEncoder, error)

NewOpusEncoder creates a new Opus audio encoder.

type AudioEncoderAdvanced

type AudioEncoderAdvanced interface {
	AudioEncoder

	// SetComplexity sets encoding complexity (0-10, higher = better quality).
	SetComplexity(c int) error

	// SetFEC enables or disables forward error correction.
	SetFEC(enabled bool) error

	// SetDTX enables or disables discontinuous transmission.
	SetDTX(enabled bool) error

	// SetPacketLossPercentage hints expected packet loss for FEC tuning.
	SetPacketLossPercentage(percent int) error

	// SetBandwidth sets the audio bandwidth constraint.
	SetBandwidth(bw codec.OpusBandwidth) error
}

AudioEncoderAdvanced extends AudioEncoder with advanced runtime controls. Use type assertion to check if an encoder supports these features.

type DependencyDescriptorProvider

type DependencyDescriptorProvider interface {
	LastDependencyDescriptor() []byte
}

DependencyDescriptorProvider exposes the serialized dependency descriptor payload for the most recently encoded frame when the encoder supports it. The returned slice is only valid until the next EncodeInto call.

type EncodeResult

type EncodeResult struct {
	// N is the number of bytes written to the destination buffer.
	N int
	// IsKeyframe indicates if the encoded frame is a keyframe.
	IsKeyframe bool
}

EncodeResult contains the result of an encode operation.

type EncoderStats

type EncoderStats struct {
	FramesEncoded   uint64 // Total frames encoded
	BytesEncoded    uint64 // Total bytes produced
	KeyframesForced uint32 // Keyframes due to RequestKeyFrame
	AvgBitrate      uint32 // Average bitrate in bps
	AvgFrameSize    uint32 // Average encoded frame size
	AvgEncodeTimeUs uint32 // Average encode time in microseconds
}

EncoderStats contains encoder runtime statistics.

type LayerInfo

type LayerInfo struct {
	SpatialID  int     // Spatial layer ID (0 = lowest resolution)
	TemporalID int     // Temporal layer ID (0 = lowest framerate)
	Width      int     // Layer resolution width
	Height     int     // Layer resolution height
	Bitrate    uint32  // Current bitrate
	FPS        float64 // FPS is the current output frame rate for the layer.
	Active     bool    // Active reports whether the layer is currently enabled.
	IsKeyframe bool    // For encoded result
}

LayerInfo describes an SVC/simulcast layer.

type VideoEncoder

type VideoEncoder interface {
	// EncodeInto encodes a video frame into the destination buffer.
	// Returns the number of bytes written and whether it's a keyframe.
	// Caller must provide a buffer of at least MaxEncodedSize() bytes.
	EncodeInto(src *frame.VideoFrame, dst []byte, forceKeyframe bool) (EncodeResult, error)

	// MaxEncodedSize returns the maximum possible encoded size for the
	// configured resolution. Use this to allocate destination buffers.
	MaxEncodedSize() int

	// SetBitrate updates the target bitrate in bits per second.
	SetBitrate(bps uint32) error

	// SetFramerate updates the target framerate.
	SetFramerate(fps float64) error

	// RequestKeyFrame requests the next frame to be a keyframe.
	RequestKeyFrame()

	// Codec returns the codec type of this encoder.
	Codec() codec.Type

	// Close releases all encoder resources.
	Close() error
}

VideoEncoder encodes raw video frames to compressed bitstream. All operations are allocation-free - caller provides buffers. Encoder instances are stateful; serialize calls per instance and use separate encoders for parallel pipelines.

func NewAV1Encoder

func NewAV1Encoder(cfg codec.AV1Config) (VideoEncoder, error)

NewAV1Encoder creates a new AV1 video encoder.

func NewH264Encoder

func NewH264Encoder(cfg codec.H264Config) (VideoEncoder, error)

NewH264Encoder creates a new H.264 encoder.

func NewVP8Encoder

func NewVP8Encoder(cfg codec.VP8Config) (VideoEncoder, error)

NewVP8Encoder creates a new VP8 video encoder.

func NewVP9Encoder

func NewVP9Encoder(cfg codec.VP9Config) (VideoEncoder, error)

NewVP9Encoder creates a new VP9 video encoder.

type VideoEncoderAdvanced

type VideoEncoderAdvanced interface {
	VideoEncoder

	// SetQuality sets the quality level for CQ mode (0-63, lower = better).
	SetQuality(q int) error

	// SetKeyInterval sets the keyframe interval in frames.
	SetKeyInterval(frames int) error

	// SetRateControl changes the rate control mode at runtime.
	SetRateControl(mode codec.RateControlMode) error

	// Stats returns current encoder statistics.
	Stats() EncoderStats
}

VideoEncoderAdvanced extends VideoEncoder with advanced runtime controls. Use type assertion to check if an encoder supports these features.

type VideoEncoderSVC

type VideoEncoderSVC interface {
	VideoEncoder

	// SetSVCMode changes the SVC mode at runtime.
	SetSVCMode(mode codec.SVCMode) error

	// SetLayerBitrate sets bitrate for a specific spatial layer.
	SetLayerBitrate(spatialLayer int, bitrate uint32) error

	// SetLayerActive enables or disables a specific layer.
	SetLayerActive(spatialLayer int, active bool) error

	// SetTemporalLayerBitrate sets bitrate allocation for temporal layer.
	SetTemporalLayerBitrate(temporalLayer int, bitrate uint32) error

	// GetActiveLayerCount returns number of currently active layers.
	GetActiveLayerCount() (spatial, temporal int)

	// RequestLayerKeyFrame requests keyframe for specific spatial layer.
	RequestLayerKeyFrame(spatialLayer int)
}

VideoEncoderSVC extends VideoEncoder with SVC/simulcast controls. Use type assertion to check if an encoder supports these features.

Jump to

Keyboard shortcuts

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