red

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: BSD-3-Clause Imports: 1 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 RTP Payload for Redundant Audio Data.

RED wraps one or more Opus payloads in a single RTP packet. The primary block carries the most recent frame; zero or more redundant blocks carry earlier frames whose original RTP packets may have been lost. A receiver that detects a gap in the sequence can recover missing frames directly from the redundant blocks, avoiding PLC or FEC decode overhead.

Wire format (RFC 2198 §2):

+-------+-------+-------+-------+
|F|  PT |     timestamp offset  |
+-------+-------+-------+-------+
|             length            |
+-------+-------+-------+-------+
     ... repeated for each redundant block ...
+-------+-------+-------+-------+
|0|  PT |   (primary block)     |
+-------+-------+-------+-------+
     redundant payloads in order, then primary payload

The F bit (0x80) is set for every header except the last (primary) one. Timestamp offsets are unsigned, measured backwards from the primary timestamp (i.e. primary_ts − redundant_ts). Block lengths are 10-bit values fitting in two bytes together with the lower 2 bits of the offset.

Encoder and Decoder are the high-level, allocation-free API; they own their buffers and the redundant-frame history so callers do not manage them. Parse, Build, AppendHistory and FindRecovery remain as the underlying stateless primitives.

Typical send path:

enc := red.NewEncoder(opusPT, frameSamples, depth)
for each frame:
    payload, _ := enc.Encode(primary, timestamp)
    send(payload) // valid until the next Encode

Typical receive path:

dec := red.NewDecoder(opusPT)
primary, blocks, err := dec.Parse(pkt.Payload)
for lostAgo := missing; lostAgo >= 1; lostAgo-- {
    if b := red.FindRecovery(blocks, lostAgo, frameSamples, pkt.Timestamp, missingTS); b != nil {
        // decode b as Opus
    }
}

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 is BuildAppend with a freshly allocated buffer. See BuildAppend for the allocation-free variant that reuses 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 constructs an RFC 2198 RED payload into dst[:0], returning the encoded payload and the total number of redundant payload bytes included. Reusing a dst buffer across calls makes building allocation-free; passing a nil dst allocates a fresh buffer.

The payload contains the primary Opus payload and up to depth redundant copies drawn from history, which must be ordered newest-first (as returned by AppendHistory). frameSamples is the number of RTP timestamp ticks per Opus frame (960 for 20 ms at 48 kHz). Only history entries whose timestamp difference from primaryTimestamp is an exact multiple of frameSamples and fits in 14 bits are eligible as redundant blocks.

Special cases:

  • If primary is empty, the result is empty with 0 redundant bytes.
  • If depth ≤ 0, the result is a raw copy of primary (no RED envelope).
  • If frameSamples ≤ 0, primary is wrapped in a minimal RED envelope with no redundant blocks (one primary header + payload).

func FindRecovery

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

FindRecovery searches blocks for a redundant entry whose timestamp matches the given missingTimestamp. lostAgo is the number of frames back the missing packet is relative to the packet that carried blocks (e.g. 1 means it is the immediately preceding frame). frameSamples is the RTP timestamp increment per frame.

The expected timestamp offset is lostAgo*frameSamples. If currentTimestamp − missingTimestamp does not equal that value, or no block carries a matching offset, FindRecovery returns nil.

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 is a sub-slice of the buffer passed to Parse; it is valid until
	// that buffer is modified. Callers that need to retain it beyond the next
	// Parse call must copy it.
	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 is ParseInto with a freshly allocated block slice. See ParseInto for the allocation-free variant that reuses a caller-supplied slice.

func ParseInto

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

ParseInto decodes an RFC 2198 RED payload, appending the redundant blocks into dst[:0] and returning the primary Opus payload alongside them. Reusing a dst slice with capacity >= the block count (at most MaxDepth) across packets makes parsing allocation-free; passing a nil dst allocates a fresh slice.

blocks are ordered oldest-first (the order they appear on the wire). primaryPayloadType is the expected RTP payload type for both the primary and all redundant blocks (e.g. 111 for Opus in WebRTC). ParseInto returns one of the package sentinel errors for any condition defined in RFC 2198:

  • empty input
  • truncated header or payload region
  • a redundant block with zero timestamp offset or zero length
  • more than MaxDepth redundant blocks
  • a payload type that does not match primaryPayloadType

ParseInto does not copy payload bytes; the returned primary slice and Block Payload fields reference the input buf directly.

type Decoder

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

Decoder parses a stream of RFC 2198 RED packets, reusing an internal block slice so that steady-state parsing allocates nothing. It is the stateful, allocation-free counterpart to Parse. A Decoder 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 the redundant blocks (oldest-first). The returned primary and Block payloads alias buf, and the blocks slice aliases the Decoder's reused buffer; all are valid only until the next Parse call or until buf is modified. It does not allocate once warm.

type Encoder

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

Encoder builds a stream of RFC 2198 RED packets, owning both the redundant frame history and the output buffer so that steady-state building allocates nothing and the caller manages neither. It is the stateful counterpart to Build plus a managed history. An Encoder 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 frameSamples RTP ticks older than the primary — using primaryPayloadType for every block. depth is clamped to MaxDepth.

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, e.g. after an RTP discontinuity. The payload type and timing stay configured.

type Frame

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

	// Payload is the encoded Opus payload for this frame.
	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 inserts payload as the newest (front) Frame of history, trims to at most maxDepth entries, and returns the updated slice — store it as your new history. The input payload is copied so the caller may reuse its buffer.

To stay allocation-free once the window is full, AppendHistory shifts the slice in place and recycles the payload buffer of the entry it evicts. A Frame's Payload is therefore only valid while that Frame remains within the maxDepth window; do not retain it after it falls out. If payload is empty or maxDepth <= 0, history is returned unchanged.

Jump to

Keyboard shortcuts

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