jitterbuffer

package
v1.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 6 Imported by: 5 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jitterbuffer implements a buffer for RTP packets designed to help counteract non-deterministic sources of latency

Index

Constants

View Source
const (
	// StartBuffering is emitted when the buffer receives its first packet.
	StartBuffering Event = "startBuffering"
	// BeginPlayback is emitted when the buffer has satisfied its buffer length.
	BeginPlayback = "playing"
	// BufferUnderflow is emitted when the buffer does not have enough packets to Pop.
	BufferUnderflow = "underflow"
	// BufferOverflow is emitted when the buffer has exceeded its limit.
	BufferOverflow = "overflow"
)

Variables

View Source
var (
	// ErrBufferUnderrun is returned when the buffer has no items.
	ErrBufferUnderrun = errors.New("invalid Peek: Empty jitter buffer")
	// ErrPopWhileBuffering is returned if a jitter buffer is not in a playback state.
	ErrPopWhileBuffering = errors.New("attempt to pop while buffering")
)
View Source
var (
	// ErrInvalidOperation may be returned if a Pop or Find operation is performed on an playback buffer.
	ErrInvalidOperation = errors.New("attempt to find or pop on an empty list")
	// ErrNotFound will be returned if the packet cannot be found in the playblack buffer.
	ErrNotFound = errors.New("packet not found")
)

Functions

This section is empty.

Types

type Event

type Event string

Event represents all events a JitterBuffer can emit.

type EventListener

type EventListener func(event Event, jb *JitterBuffer)

EventListener will be called when the corresponding Event occurs.

type InterceptorFactory added in v0.1.30

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

InterceptorFactory is a interceptor.Factory for a GeneratorInterceptor.

func NewInterceptor added in v0.1.30

func NewInterceptor(opts ...ReceiverInterceptorOption) (*InterceptorFactory, error)

NewInterceptor returns a new InterceptorFactory.

func (*InterceptorFactory) NewInterceptor added in v0.1.30

func (g *InterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error)

NewInterceptor constructs a new ReceiverInterceptor.

type JitterBuffer

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

A JitterBuffer will accept Pushed packets, put them in sequence number order, and allows removing in either sequence number order or via a provided timestamp.

A JitterBuffer is not safe for concurrent use; callers sharing one across goroutines must synchronize access themselves.

func New

func New(opts ...Option) *JitterBuffer

New will initialize a jitter buffer and its associated statistics.

func (*JitterBuffer) Clear added in v0.1.30

func (jb *JitterBuffer) Clear(resetState bool)

Clear will empty the buffer and optionally reset the state. Configuration such as the minimum packet count is kept.

func (*JitterBuffer) Listen

func (jb *JitterBuffer) Listen(event Event, cb EventListener)

Listen will register an event listener The jitter buffer may emit events correspnding, interested listerns should look at Event for available events.

func (*JitterBuffer) Peek

func (jb *JitterBuffer) Peek(playoutHead bool) (*rtp.Packet, error)

Peek at the packet which is either:

At the playout head when we are emitting, and the playoutHead flag is true

or else

At the last sequence received

func (*JitterBuffer) PeekAtSequence added in v0.1.27

func (jb *JitterBuffer) PeekAtSequence(sq uint16) (*rtp.Packet, error)

PeekAtSequence will return an RTP packet from the jitter buffer at the specified Sequence without removing it from the buffer. Like RemoveAtSequence, it finds a packet still queued for playout even if newer packets pushed it out of the window.

func (*JitterBuffer) PlayoutHead added in v0.1.28

func (jb *JitterBuffer) PlayoutHead() uint16

PlayoutHead returns the SequenceNumber that will be attempted to Pop next.

func (*JitterBuffer) Pop

func (jb *JitterBuffer) Pop() (*rtp.Packet, error)

Pop an RTP packet from the jitter buffer at the current playout head.

func (*JitterBuffer) PopAtSequence added in v0.1.27

func (jb *JitterBuffer) PopAtSequence(sq uint16) (*rtp.Packet, error)

PopAtSequence will pop an RTP packet from the jitter buffer at the specified Sequence.

func (*JitterBuffer) PopAtTimestamp

func (jb *JitterBuffer) PopAtTimestamp(ts uint32) (*rtp.Packet, error)

PopAtTimestamp pops an RTP packet from the jitter buffer with the provided timestamp Call this method repeatedly to drain the buffer at the timestamp.

func (*JitterBuffer) Push

func (jb *JitterBuffer) Push(packet *rtp.Packet)

Push an RTP packet into the jitter buffer. Unless WithCopy is set this does not clone the data, so if the memory is expected to be reused, the caller should pass a copy of the packet they wish to buffer.

func (*JitterBuffer) RemoveAtSequence

func (jb *JitterBuffer) RemoveAtSequence(sq uint16) (*rtp.Packet, error)

RemoveAtSequence removes the packet with the specified Sequence from the buffer and returns it, regardless of playout state. Playout is consumed through sq whether or not it was buffered: packets still queued at or before sq are discarded, and playout resumes with the packets after sq. A packet still queued for playout is returned even if newer packets have pushed it out of the sequence window.

func (*JitterBuffer) SetPlayoutHead added in v0.1.28

func (jb *JitterBuffer) SetPlayoutHead(playoutHead uint16)

SetPlayoutHead allows you to manually specify the packet you wish to pop next If you have encountered a packet that hasn't resolved you can skip it.

type Option

type Option func(jb *JitterBuffer)

Option will Override JitterBuffer's defaults.

func DisableCopy

func DisableCopy() Option

DisableCopy restores the default of buffering the caller's packets without copying, undoing an earlier WithCopy. Callers must not reuse a pushed packet's memory while the buffer may still return it.

func WithCopy

func WithCopy() Option

WithCopy makes Push copy each packet's header and payload into pooled buffers, so callers may reuse their packet memory once Push returns. Packets returned by the buffer are independent copies. Packets with payloads larger than 1460 bytes are dropped in this mode.

func WithMinimumPacketCount added in v0.1.27

func WithMinimumPacketCount(count uint16) Option

WithMinimumPacketCount will set the required number of packets to be received before any attempt to pop a packet can succeed. The default is 50. A count larger than the buffer size (see WithSize) is lowered to the buffer size.

func WithSize

func WithSize(size uint16) Option

WithSize sets how many sequence numbers the buffer can hold. It is rounded up to a power of two and capped at 32768 (half the sequence number space). Zero keeps the default of 1024. The minimum packet count is lowered to the resulting size if it is larger.

type ReceiverInterceptor added in v0.1.30

type ReceiverInterceptor struct {
	interceptor.NoOp
	// contains filtered or unexported fields
}

ReceiverInterceptor places a JitterBuffer in the chain to smooth packet arrival and allow for network jitter

The Interceptor is designed to fit in a RemoteStream
pipeline and buffer incoming packets for a short period (currently
defaulting to 50 packets) before emitting packets to be consumed by the
next step in the pipeline.

The caller must ensure they are prepared to handle an
ErrPopWhileBuffering in the case that insufficient packets have been
received by the jitter buffer. The caller should retry the operation
at some point later as the buffer may have been filled in the interim.

The caller should also be aware that an ErrBufferUnderrun may be
returned in the case that the initial buffering was sufficient and
playback began but the caller is consuming packets (or they are not
arriving) quickly enough.

func (*ReceiverInterceptor) BindRemoteStream added in v0.1.30

BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method will be called once per rtp packet.

func (*ReceiverInterceptor) Close added in v0.1.30

func (i *ReceiverInterceptor) Close() error

Close closes the interceptor.

func (*ReceiverInterceptor) UnbindRemoteStream added in v0.1.30

func (i *ReceiverInterceptor) UnbindRemoteStream(info *interceptor.StreamInfo)

UnbindRemoteStream is called when the Stream is removed. It can be used to clean up any data related to that track.

type ReceiverInterceptorOption added in v0.1.30

type ReceiverInterceptorOption func(d *ReceiverInterceptor) error

ReceiverInterceptorOption can be used to configure ReceiverInterceptor.

func Log added in v0.1.30

Log sets a logger for the interceptor.

func WithBufferOptions

func WithBufferOptions(opts ...Option) ReceiverInterceptorOption

WithBufferOptions configures JitterBuffers created for each remote stream.

func WithLoggerFactory added in v0.1.43

func WithLoggerFactory(loggerFactory logging.LoggerFactory) ReceiverInterceptorOption

WithLoggerFactory sets a logger factory for the interceptor.

type RingBuffer

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

RingBuffer is a classic Ring Buffer.

func NewRingBuffer

func NewRingBuffer(size uint16) *RingBuffer

func (*RingBuffer) At

func (r *RingBuffer) At(sequenceNumber uint16) *rtpbuffer.RetainablePacket

At returns the queued packet with sequenceNumber without removing it, or nil. The queue is normally consecutive sequence numbers, so the slot is computed directly; SetPlayoutHead can break that, so a mismatch falls back to a scan.

func (*RingBuffer) Clear

func (r *RingBuffer) Clear()

func (*RingBuffer) Empty

func (r *RingBuffer) Empty() bool

func (*RingBuffer) Full

func (r *RingBuffer) Full() bool

func (*RingBuffer) Length

func (r *RingBuffer) Length() uint16

func (*RingBuffer) Peek

func (*RingBuffer) Pop

func (*RingBuffer) PopAt

func (r *RingBuffer) PopAt(sequenceNumber uint16) *rtpbuffer.RetainablePacket

func (*RingBuffer) PopAtTimestamp

func (r *RingBuffer) PopAtTimestamp(timestamp uint32) *rtpbuffer.RetainablePacket

func (*RingBuffer) Push

func (r *RingBuffer) Push(rPacket *rtpbuffer.RetainablePacket) bool

type State

type State uint16

State tracks a JitterBuffer as either Buffering or Emitting.

const (
	// Buffering is the state when the jitter buffer has not started emitting yet,
	// or has hit an underflow and needs to re-buffer packets.
	Buffering State = iota
	//  Emitting is the state when the jitter buffer is operating nominally
	Emitting
)

func (State) String

func (jbs State) String() string

type Stats

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

Stats Track interesting statistics for the life of this JitterBuffer outOfOrderCount will provide the number of times a packet was Pushed

without its predecessor being present

underflowCount will provide the count of attempts to Pop an empty buffer overflowCount will track the number of times the jitter buffer exceeds its limit.

Jump to

Keyboard shortcuts

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