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
- Variables
- type Decoder
- func (d *Decoder) AppendFrame(dst []byte, frameID uint32) ([]byte, bool)
- func (d *Decoder) AppendPartial(dst []byte, frameID uint32) (au []byte, missing int, ok bool)
- func (d *Decoder) Flush()
- func (d *Decoder) Push(plain []byte) (PushResult, error)
- func (d *Decoder) Slice(frameID uint32, slice int) []byte
- type Encoder
- type Fixed
- type FrameStats
- type ParityPolicy
- type Policy
- type PushResult
Constants ¶
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.
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 ¶
var ErrBadShard = errors.New("fec: bad or inconsistent shard")
var ErrTooLarge = errors.New("fec: slice too large")
Functions ¶
This section is empty.
Types ¶
type Decoder ¶
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 ¶
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 ¶
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.
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 (*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: per block, data shards then parity shards. 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.
type Fixed ¶
type Fixed int
Fixed is a parity percentage, rounded up so any non-zero percentage yields at least one parity shard.
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 ¶
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.