Skip to main content

write_ws_prefix_into

Function write_ws_prefix_into 

Source
pub(crate) fn write_ws_prefix_into(
    buf: &mut [u8],
    primary_pts: u64,
    history: &VecDeque<(Vec<u8>, u64)>,
    red_distance: usize,
    emit_header: bool,
) -> usize
Expand description

Emit the RFC 2198 RED framing prefix into buf in-place and return its length, so the encoder can serialize the Opus packet directly after it with no scratch buffer and no per-frame allocation. The runtime counterpart of build_ws_body.

  1. Header omitted: returns 0; the caller emits the raw primary with no prefix.
  2. red_distance > 0 with usable history: writes the full RED header — tag, n_red, the primary timestamp (low 32 bits, big-endian), one 4-byte header per redundant block (F bit set, then (offset14 << 10) | len10), the 1-byte primary header (F bit clear), and the redundant block payloads oldest-first. Usable blocks are selected by history index into a fixed RED_MAX_DISTANCE array — the distance is clamped at ingest, so this allocates nothing.
  3. red_distance == 0, or no usable redundancy: the 2-byte [0x01, 0x00] framing.

buf must hold at least RED_PREFIX_MAX bytes when emit_header && red_distance > 0, and at least 2 bytes otherwise.