track

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

Documentation

Overview

Package track provides Pion-compatible TrackLocal implementations backed by libwebrtc.

Index

Constants

View Source
const DependencyDescriptorRTPHeaderExtensionURI = "https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension"

DependencyDescriptorRTPHeaderExtensionURI is the RTP header extension URI used for AV1/VP9 dependency descriptors.

Variables

View Source
var (
	// ErrTrackClosed reports operations on a closed track.
	ErrTrackClosed = errors.New("track is closed")
	// ErrNotBound reports writes attempted before the track is bound.
	ErrNotBound = errors.New("track not bound")
	// ErrAlreadyBound reports a second bind attempt on an already bound track.
	ErrAlreadyBound = errors.New("track already bound")
	// ErrEncodeFailed reports an encoding failure.
	ErrEncodeFailed = errors.New("encode failed")
	// ErrInvalidConfig reports invalid track configuration.
	ErrInvalidConfig = errors.New("invalid config")
	// ErrNilVideoFrame reports a nil video frame input.
	ErrNilVideoFrame = errors.New("video frame is nil")
	// ErrNilAudioFrame reports a nil audio frame input.
	ErrNilAudioFrame = errors.New("audio frame is nil")
	// ErrNilRTPPacket reports a nil RTP packet input.
	ErrNilRTPPacket = errors.New("rtp packet is nil")
)

Errors

Functions

func ScaleI420Frame

func ScaleI420Frame(src, dst *frame.VideoFrame, scaleFactor float64)

ScaleI420Frame scales an I420 video frame by the given factor using box filter (area averaging). A scale factor of 2.0 means the output is half the size of the input. The dst frame must already be allocated with the correct dimensions.

func ScaleI420FrameFast

func ScaleI420FrameFast(src, dst *frame.VideoFrame, scaleFactor float64)

ScaleI420FrameFast scales an I420 frame using nearest neighbor (fast but lower quality). Use this when performance is critical.

Types

type AudioTrack

type AudioTrack struct {
	// contains filtered or unexported fields
}

AudioTrack implements webrtc.TrackLocal using libwebrtc Opus encoder.

func NewAudioTrack

func NewAudioTrack(cfg AudioTrackConfig) (*AudioTrack, error)

NewAudioTrack creates a new audio track backed by libwebrtc Opus encoder. MTU must be set to a positive value by the caller.

func (*AudioTrack) Bind

Bind is called by Pion when the track is added to a PeerConnection.

func (*AudioTrack) Close

func (t *AudioTrack) Close() error

Close releases all resources.

func (*AudioTrack) ID

func (t *AudioTrack) ID() string

ID returns the track ID.

func (*AudioTrack) Kind

func (t *AudioTrack) Kind() webrtc.RTPCodecType

Kind returns webrtc.RTPCodecTypeAudio.

func (*AudioTrack) RID

func (t *AudioTrack) RID() string

RID returns the RTP stream ID (empty for non-simulcast).

func (*AudioTrack) RTPContext

func (t *AudioTrack) RTPContext() (RTPPacketContext, bool)

RTPContext returns the current negotiated RTP context for this audio track.

func (*AudioTrack) SetBitrate

func (t *AudioTrack) SetBitrate(bps uint32) error

SetBitrate adjusts encoder bitrate.

func (*AudioTrack) SetOnRTPPacket

func (t *AudioTrack) SetOnRTPPacket(observer RTPPacketObserver)

SetOnRTPPacket configures a send-side RTP observer for this audio track.

func (*AudioTrack) SetRTPPacketMutator

func (t *AudioTrack) SetRTPPacketMutator(mutator RTPPacketMutator)

SetRTPPacketMutator configures a send-side RTP mutator for this audio track.

func (*AudioTrack) StreamID

func (t *AudioTrack) StreamID() string

StreamID returns the stream ID.

func (*AudioTrack) Unbind

func (t *AudioTrack) Unbind(ctx webrtc.TrackLocalContext) error

Unbind is called when the track is removed from the PeerConnection.

func (*AudioTrack) WriteEncodedData

func (t *AudioTrack) WriteEncodedData(data []byte, timestamp uint32) error

WriteEncodedData writes pre-encoded Opus data as RTP packets.

func (*AudioTrack) WriteFrame

func (t *AudioTrack) WriteFrame(f *frame.AudioFrame) error

WriteFrame encodes an audio frame and writes RTP packets.

type AudioTrackConfig

type AudioTrackConfig struct {
	ID         string // ID is the stable media track identifier.
	StreamID   string // StreamID is the MediaStream identifier exposed to the remote peer.
	SampleRate int    // SampleRate is the source PCM sample rate in Hz.
	Channels   int    // Channels is the number of PCM channels in source frames.
	Bitrate    uint32 // Bitrate is the initial Opus target bitrate in bps.
	MTU        uint16 // MTU is the RTP packet size budget used during packetization and must be set explicitly.

	// Optional codec preferences used during TrackLocal binding.
	CodecPreferences []webrtc.RTPCodecParameters
}

AudioTrackConfig configures an audio track.

type BandwidthEstimate

type BandwidthEstimate struct {
	TimestampUs      int64   // TimestampUs is when the estimate was produced, in microseconds.
	TargetBitrateBps int64   // TargetBitrateBps is libwebrtc's current target send bitrate.
	AvailableSendBps int64   // AvailableSendBps is the estimated available uplink bitrate.
	PacingRateBps    int64   // PacingRateBps is the packet pacing rate suggested by the estimator.
	LossRate         float64 // LossRate is the recent packet loss fraction in the range [0,1].
}

BandwidthEstimate contains bandwidth estimation data from the network.

type BandwidthEstimateSource

type BandwidthEstimateSource func() *BandwidthEstimate

BandwidthEstimateSource is a function that returns the current BWE.

type Parameters

type Parameters struct {
	// Active enables or pauses output. Set it explicitly on each call because
	// SetParameters applies this struct as an absolute state, not a partial patch.
	Active                bool
	MaxBitrate            uint32  // MaxBitrate caps the active encoding bitrate in bps.
	MaxFramerate          float64 // MaxFramerate caps the active encoding frame rate.
	ScaleResolutionDownBy float64 // ScaleResolutionDownBy reduces output resolution by this factor.
	ScalabilityMode       string  // SVC mode (e.g., "L3T3_KEY", "L1T2")
	Priority              string  // "very-low", "low", "medium", "high"
}

Parameters mirrors browser RTCRtpEncodingParameters for manual control.

type RTPPacketContext

type RTPPacketContext struct {
	TrackID          string
	StreamID         string
	RID              string
	SSRC             webrtc.SSRC
	PayloadType      webrtc.PayloadType
	CodecParameters  webrtc.RTPCodecParameters
	HeaderExtensions []webrtc.RTPHeaderExtensionParameter
	PacketIndex      int
	PacketCount      int
}

RTPPacketContext describes the negotiated RTP state for an outbound packet.

Experimental: this surface is intended for send-side validation and custom RTP header extension work.

func (RTPPacketContext) HeaderExtensionID

func (c RTPPacketContext) HeaderExtensionID(uri string) (uint8, bool)

HeaderExtensionID returns the negotiated ID for the given RTP header extension URI.

type RTPPacketMutator

type RTPPacketMutator func(pkt *rtp.Packet, ctx RTPPacketContext) error

RTPPacketMutator can inspect or modify a packet before it is written.

Experimental: mutators should return quickly and avoid long blocking work.

type RTPPacketObserver

type RTPPacketObserver func(pkt *rtp.Packet, ctx RTPPacketContext)

RTPPacketObserver receives a clone of the final RTP packet about to be sent.

Experimental: observers should return quickly and avoid long blocking work.

type VideoTrack

type VideoTrack struct {
	// contains filtered or unexported fields
}

VideoTrack implements webrtc.TrackLocal using libwebrtc encoder. Call WriteFrame to encode raw video and send RTP packets.

func NewVideoTrack

func NewVideoTrack(cfg VideoTrackConfig) (*VideoTrack, error)

NewVideoTrack creates a new video track backed by libwebrtc encoder. Auto adaptation stays disabled unless the caller opts in explicitly. MTU must be set to a positive value by the caller.

func (*VideoTrack) Bind

Bind is called by Pion when the track is added to a PeerConnection.

func (*VideoTrack) Close

func (t *VideoTrack) Close() error

Close releases all resources.

func (*VideoTrack) HandleRTCPFeedback

func (t *VideoTrack) HandleRTCPFeedback(feedbackType int, ssrc uint32)

HandleRTCPFeedback handles RTCP feedback for browser-like behavior. feedbackType: 0=PLI, 1=FIR, 2=NACK

func (*VideoTrack) ID

func (t *VideoTrack) ID() string

ID returns the track ID.

func (*VideoTrack) Kind

func (t *VideoTrack) Kind() webrtc.RTPCodecType

Kind returns webrtc.RTPCodecTypeVideo.

func (*VideoTrack) RID

func (t *VideoTrack) RID() string

RID returns the RTP stream ID (empty for non-simulcast).

func (*VideoTrack) RTPContext

func (t *VideoTrack) RTPContext() (RTPPacketContext, bool)

RTPContext returns the current negotiated RTP context for this video track.

func (*VideoTrack) RequestKeyFrame

func (t *VideoTrack) RequestKeyFrame()

RequestKeyFrame requests the encoder to generate a keyframe.

func (*VideoTrack) SetBWESource

func (t *VideoTrack) SetBWESource(source BandwidthEstimateSource)

SetBWESource sets the bandwidth estimation source for auto adaptation. This is typically wired up when the track is added to a PeerConnection.

func (*VideoTrack) SetBitrate

func (t *VideoTrack) SetBitrate(bps uint32) error

SetBitrate adjusts encoder bitrate.

func (*VideoTrack) SetFramerate

func (t *VideoTrack) SetFramerate(fps float64) error

SetFramerate adjusts encoder framerate.

func (*VideoTrack) SetOnRTPPacket

func (t *VideoTrack) SetOnRTPPacket(observer RTPPacketObserver)

SetOnRTPPacket configures a send-side RTP observer for this video track.

func (*VideoTrack) SetParameters

func (t *VideoTrack) SetParameters(params Parameters) error

SetParameters allows manual control like browser RTCRtpSender.setParameters(). It applies Parameters as an absolute state, so callers should always set Active explicitly instead of relying on the zero value.

func (*VideoTrack) SetRTPPacketMutator

func (t *VideoTrack) SetRTPPacketMutator(mutator RTPPacketMutator)

SetRTPPacketMutator configures a send-side RTP mutator for this video track.

func (*VideoTrack) StreamID

func (t *VideoTrack) StreamID() string

StreamID returns the stream ID.

func (*VideoTrack) Unbind

func (t *VideoTrack) Unbind(ctx webrtc.TrackLocalContext) error

Unbind is called when the track is removed from the PeerConnection.

func (*VideoTrack) WriteEncodedData

func (t *VideoTrack) WriteEncodedData(data []byte, timestamp uint32, isKeyframe bool) error

WriteEncodedData writes pre-encoded data as RTP packets. Useful when you already have encoded H.264/VP8/etc data.

func (*VideoTrack) WriteFrame

func (t *VideoTrack) WriteFrame(f *frame.VideoFrame, forceKeyframe bool) error

WriteFrame encodes a video frame and writes RTP packets to the bound peer connection.

func (*VideoTrack) WriteRTP

func (t *VideoTrack) WriteRTP(pkt *rtp.Packet) error

WriteRTP writes an already-formed RTP packet.

type VideoTrackConfig

type VideoTrackConfig struct {
	ID       string     // ID is the stable media track identifier.
	StreamID string     // StreamID is the MediaStream identifier exposed to the remote peer.
	RID      string     // RID is the RTP stream identifier used for simulcast encodings.
	Codec    codec.Type // Codec is the preferred libgowebrtc video codec.
	Width    int        // Width is the source frame width in pixels.
	Height   int        // Height is the source frame height in pixels.
	Bitrate  uint32     // Bitrate is the initial encoder target bitrate in bps.
	FPS      float64    // FPS is the initial encoder target frame rate.
	MTU      uint16     // MTU is the RTP packetization budget in bytes and must be set explicitly.

	// Auto adaptation is opt-in so helpers can choose browser-like defaults
	// explicitly instead of the base constructor guessing from zero values.
	AutoKeyframe   bool // PLI/FIR → RequestKeyFrame()
	AutoBitrate    bool // BWE → adjust bitrate
	AutoFramerate  bool // BWE → adjust framerate
	AutoResolution bool // BWE → scale resolution

	// Constraints (like browser MediaTrackConstraints)
	MinBitrate   uint32  // Floor for bitrate adaptation
	MaxBitrate   uint32  // Ceiling for bitrate adaptation
	MinFramerate float64 // Floor for framerate
	MaxFramerate float64 // Ceiling for framerate
	MinWidth     int     // Don't scale below this
	MinHeight    int     // Don't scale below this

	// Optional codec preferences used during TrackLocal binding.
	// When provided, Bind selects the best negotiated codec from this list.
	CodecPreferences []webrtc.RTPCodecParameters

	// SVC configures scalable or simulcast encoder output when supported.
	SVC *codec.SVCConfig
}

VideoTrackConfig configures a video track.

Jump to

Keyboard shortcuts

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