red

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0 modules, 0 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 red implements RFC 2198 redundancy for RTP payloads.

A RED payload carries a primary packet and may include older packets as redundant blocks. Parse and Build provide stateless helpers; Decoder and Encoder reuse block/output storage and maintain packet history for stream processing. Payload-type arguments are 7-bit RTP payload type values.

Index

Constants

View Source
const MaxDepth = 5

MaxDepth is the maximum number of redundant blocks supported per packet. Values above this are rejected by Parse and clamped by Build.

Variables

This section is empty.

Functions

func Build

func Build(primary []byte, primaryTimestamp uint32, history []Frame, depth, frameSamples int, primaryPayloadType byte) ([]byte, int)

Build calls BuildAppend without a reusable destination. It allocates output storage for nonempty results; use BuildAppend to reuse a caller-supplied buffer.

func BuildAppend

func BuildAppend(dst []byte, primary []byte, primaryTimestamp uint32, history []Frame, depth, frameSamples int, primaryPayloadType byte) (out []byte, redundantBytes int)

BuildAppend writes a RED payload to dst[:0], returning output backed by dst when its capacity is sufficient and the total redundant payload bytes (not header bytes). history must be ordered newest-first, as returned by AppendHistory; depth is clamped to MaxDepth. primaryPayloadType must be in 0..127. frameSamples is the RTP timestamp increment per Opus frame; eligible history entries must be an exact multiple of that interval, have a 14-bit timestamp offset, and have a nonempty payload that fits the 10-bit block- length field. A nil or undersized dst may allocate. An empty primary produces an empty result. A nonpositive depth copies primary without a RED header; nonpositive frameSamples emits only a primary header and payload, with no redundant blocks. With sufficient capacity, non-overlapping inputs require no allocation. Input payloads that overlap output bytes are snapshotted before writing.

func FindRecovery

func FindRecovery(blocks []Block, lostAgo, frameSamples int, currentTimestamp, missingTimestamp uint32) []byte

FindRecovery returns the redundant payload for the missing timestamp, if it is present in blocks. lostAgo is the number of frames between the current and missing packets; frameSamples is the RTP timestamp increment per frame. RTP timestamp subtraction uses uint32 wraparound. The returned slice aliases the matching Block.Payload; it returns nil when the offset exceeds RFC 2198's 14-bit field, the timestamp difference does not match, or no block covers it.

Types

type Block

type Block struct {
	// PayloadType is the RTP payload type for this block (7-bit, 0–127).
	PayloadType byte

	// TimestampOffset is primary_timestamp − block_timestamp.
	// It is always positive and at most 0x3FFF (14 bits).
	TimestampOffset int

	// Payload aliases the buffer passed to Parse or ParseInto. It remains valid
	// until that buffer is modified; copy it before reusing the input storage.
	Payload []byte
}

Block is a single redundant entry parsed from a RED payload. The primary block is not represented as a Block; it is returned separately by Parse.

func Parse

func Parse(buf []byte, primaryPayloadType byte) (primary []byte, blocks []Block, err error)

Parse calls ParseInto without a reusable block slice. It allocates block storage when the packet contains redundant blocks; ParseInto can reuse a caller-supplied slice for allocation-free parsing.

func ParseInto

func ParseInto(buf []byte, primaryPayloadType byte, dst []Block) (primary []byte, blocks []Block, err error)

ParseInto parses a RED payload into dst[:0] and returns the primary payload and redundant blocks in wire order. primaryPayloadType must be in 0..127, and every block header must carry that payload type. It reuses dst when its capacity is sufficient; the returned blocks slice aliases dst's backing array. The primary and block payloads alias buf; copy them if they must outlive or remain independent of the input. It returns an error for empty or truncated input, invalid blocks, unexpected payload types, a missing primary payload, or more than MaxDepth blocks.

type Decoder

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

Decoder parses RFC 2198 RED packets, reusing an internal block slice. After a parse has grown that slice to the needed capacity, later parses with no more blocks allocate no block storage. It is the stateful counterpart to Parse and is not safe for concurrent use.

func NewDecoder

func NewDecoder(primaryPayloadType byte) *Decoder

NewDecoder returns a Decoder for packets whose primary and redundant blocks carry primaryPayloadType.

func (*Decoder) Parse

func (d *Decoder) Parse(buf []byte) (primary []byte, blocks []Block, err error)

Parse decodes one RED payload into the primary Opus payload and redundant blocks (oldest-first). The primary and Block payloads alias buf; the blocks slice aliases the Decoder's reused storage and is overwritten by a later Parse call. Copy any data that must remain independent. Parsing reuses storage after it has enough capacity for the packet's blocks.

type Encoder

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

Encoder builds RFC 2198 RED packets and owns its frame history and reusable output buffer. With sufficient capacity, Encode reuses those buffers without allocating when primary does not overlap its output. Aliased primary input may require a snapshot. It is the stateful counterpart to Build with managed history and is not safe for concurrent use.

func NewEncoder

func NewEncoder(primaryPayloadType byte, frameSamples, depth int) *Encoder

NewEncoder returns an Encoder that emits up to depth redundant blocks per packet, each an exact multiple of positive frameSamples RTP ticks older than the primary, using the 7-bit primaryPayloadType for every block. depth above MaxDepth is clamped; nonpositive depth emits no redundant blocks.

func (*Encoder) Encode

func (e *Encoder) Encode(primary []byte, timestamp uint32) (payload []byte, redundantBytes int)

Encode builds the RED packet carrying primary at the given RTP timestamp plus the eligible recent frames, then records primary in the history for later packets. The returned payload aliases the Encoder's reused buffer and is valid only until the next Encode call; redundantBytes is the redundant payload total. primary is copied into the history, so it never escapes and the caller may reuse its buffer immediately.

func (*Encoder) Reset

func (e *Encoder) Reset()

Reset clears the redundant-frame history, for example after an RTP timestamp discontinuity. The payload type and timing stay configured; a packet returned by an earlier Encode remains valid until the next Encode reuses the output buffer.

type Frame

type Frame struct {
	// Timestamp is the RTP timestamp of this frame.
	Timestamp uint32

	// Payload is the encoded Opus payload for this frame. BuildAppend reads it
	// without copying it into history; AppendHistory stores its own copy.
	Payload []byte
}

Frame is a single send-side history entry used when building RED packets.

func AppendHistory

func AppendHistory(history []Frame, payload []byte, timestamp uint32, maxDepth int) []Frame

AppendHistory copies payload into the front of history, trims the result to maxDepth entries, and returns the updated slice. When history is full, it reuses the evicted payload buffer if its capacity is sufficient; otherwise it allocates a new buffer. A Frame.Payload remains valid only while that frame is retained. Empty payloads and nonpositive maxDepth leave history unchanged.

Jump to

Keyboard shortcuts

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