fec

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 6 Imported by: ? 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 fec protects video slices with Reed-Solomon parity sized from the receiver's measured loss, and rebuilds them on the receiving side.

Index

Constants

View Source
const MaxDataPerBlock = 128

MaxDataPerBlock caps data shards per Reed-Solomon block, leaving room in the 255-shard code for parity up to 100% of data.

View Source
const ReassemblyWindow = 8

ReassemblyWindow is how many frames stay open for late shards. When a frame newer than id+ReassemblyWindow arrives, frame id is finalized.

Variables

View Source
var ErrBadShard = errors.New("fec: bad or inconsistent shard")
View Source
var ErrTooLarge = errors.New("fec: slice too large")

Functions

func BurstFloor added in v0.2.0

func BurstFloor(data, blocks int, keyframe bool) int

BurstFloor is the parity a block carries whatever the loss estimate, for the bursts a clean path still has: a NIC, switch or socket queue that overflows drops consecutive datagrams, which an estimate of independent loss does not foresee, and on a clean LAN the estimate is back to zero a sixth of a second after each loss. A lost keyframe costs a round trip and the largest frame again; a lost P frame a reference invalidation and a few frames skipped. One block with k parity rebuilds any k lost, so a P frame carries enough for a six-datagram burst from 64 data shards and four from 32; smaller ones stay bare. A block of a multi-block slice, interleaved on the wire, takes a third of a six-datagram burst.

Types

type Decoder

type Decoder struct {
	Late    uint64
	Invalid uint64
	// contains filtered or unexported fields
}

Decoder rebuilds slices and frames from shard datagrams, using Reed-Solomon reconstruction when data shards are missing. Single-block slices are stored contiguously, so Slice needs no copy. Not safe for concurrent use.

func NewDecoder

func NewDecoder(shardSize int, onFinal func(FrameStats)) *Decoder

NewDecoder returns a Decoder for shards of shardSize bytes. onFinal, if set, is called for each frame as it leaves the window.

func (*Decoder) AppendFrame

func (d *Decoder) AppendFrame(dst []byte, frameID uint32) ([]byte, bool)

AppendFrame appends every slice of a complete frame to dst. It reports false, leaving dst unchanged, if the frame is not complete.

func (*Decoder) AppendPartial

func (d *Decoder) AppendPartial(dst []byte, frameID uint32) (au []byte, missing int, ok bool)

AppendPartial appends what is known of frame frameID to dst: ready slices whole, and in other slices each seen block's data shards, with zeros for the shards that neither arrived nor were recovered. Blocks and slices with no shard seen are left out, so offsets shift: only a stream of self-describing shard-sized chunks (PyroWave) can use it. missing counts the zeroed shards. ok is false once the frame has left the window.

func (*Decoder) Flush

func (d *Decoder) Flush()

Flush finalizes every open frame.

func (*Decoder) Push

func (d *Decoder) Push(plain []byte) (PushResult, error)

Push consumes one plaintext video datagram (header and shard).

func (*Decoder) Slice

func (d *Decoder) Slice(frameID uint32, slice int) []byte

Slice returns the bytes of a ready slice, or nil. The bytes stay valid until the frame leaves the window.

type Encoder

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

Encoder turns video slices into shard datagrams. Each datagram buffer has headroom bytes before the plaintext (video header and shard) and tailroom bytes of spare capacity after it, so the caller can seal it in place. Buffers are reused, so after warm-up the encoder itself does not allocate. Not safe for concurrent use.

func NewEncoder

func NewEncoder(shardSize, headroom, tailroom int) *Encoder

func (*Encoder) EncodeFrame

func (e *Encoder) EncodeFrame(info wire.VideoInfo, frame []byte, policy ParityPolicy, emit func(pkt []byte) error) (int, error)

EncodeFrame splits frame evenly into info.SliceCount slices and encodes each. It returns the number of datagrams emitted.

func (*Encoder) EncodeSlice

func (e *Encoder) EncodeSlice(info wire.VideoInfo, slice int, data []byte, policy ParityPolicy, emit func(pkt []byte) error) (int, error)

EncodeSlice emits every datagram of one slice. A slice of several blocks is sent interleaved, shard i of each block in turn, so a burst of n lost datagrams costs each block about n/blocks shards instead of one block all of them. emit receives the full buffer, headroom included; it is reused after emit returns. Slices of one frame may be encoded as the codec produces them.

func (*Encoder) ShardSize

func (e *Encoder) ShardSize() int

type Fixed

type Fixed int

Fixed is a parity percentage, rounded up so any non-zero percentage yields at least one parity shard.

func (Fixed) Parity

func (f Fixed) Parity(data int) int

type FrameStats

type FrameStats struct {
	FrameID         uint32
	Keyframe        bool
	Complete        bool
	RecvShards      int
	SentShards      int
	Slices          int
	SlicesRecovered int
	SlicesLost      int
}

FrameStats is reported once per frame when it leaves the window. SentShards counts only blocks that delivered at least one shard.

type ParityPolicy

type ParityPolicy interface {
	Parity(data int) int
}

ParityPolicy chooses the parity shard count for a block of data shards.

type Policy

type Policy struct {
	// Target is the acceptable probability that a block is unrecoverable.
	Target float64
	// MinLoss is a floor on the loss estimate, to keep some parity on a
	// clean path.
	MinLoss float64
	// MaxRatio caps parity as a fraction of data shards.
	MaxRatio float64
	// Window is roughly how many shards the loss estimate averages over
	// while loss is falling. Rising loss is tracked four times faster, and
	// the first loss on a clean path (estimate under cleanLoss) sets the
	// estimate to its report at once, so the next frames carry parity
	// before a second loss.
	Window float64
	// contains filtered or unexported fields
}

Policy sizes parity from a smoothed loss estimate: the smallest k such that, with independent loss at that rate, a block of data+k shards loses more than k with probability below Target. No loss gives no parity; about 1% loss gives 10 to 20%; 5% gives about 40% on small blocks.

Observe and SetLoss may run concurrently with Parity.

func NewPolicy

func NewPolicy() *Policy

NewPolicy returns a Policy with Target 1e-3, no loss floor, parity capped at 100% of data, and a 2000-shard averaging window.

func (*Policy) Loss

func (p *Policy) Loss() float64

func (*Policy) Observe

func (p *Policy) Observe(recv, sent int)

Observe folds a receiver report of recv out of sent shards into the loss estimate.

func (*Policy) Parity

func (p *Policy) Parity(data int) int

func (*Policy) SetLoss

func (p *Policy) SetLoss(l float64)

type PushResult

type PushResult struct {
	Info          wire.VideoInfo
	SliceReady    bool
	SliceIndex    int
	Recovered     bool
	FrameComplete bool
	// FrameBytes is the frame's total slice bytes, set when FrameComplete.
	FrameBytes int
}

PushResult says what one shard completed.

Jump to

Keyboard shortcuts

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