Skip to main content

TsResync

Struct TsResync 

Source
pub struct TsResync { /* private fields */ }
Expand description

Stateful TS byte-stream resynchroniser (ISO/IEC 13818-1 §2.4.3.2).

Recovers 188-byte MPEG-TS packet alignment from an arbitrary byte stream that may start mid-packet or contain junk, and detects 204-byte Reed-Solomon-coded packets (stripping the 16 parity bytes).

Feed raw bytes with feed; each call returns a Vec of aligned 188-byte TS packets. Bytes are buffered across calls so that packet boundaries may span call boundaries freely.

Lock is declared after LOCK_CONFIRMATIONS consecutive sync bytes are found at the candidate stride (188 or 204). On sync loss the resynchroniser re-scans from the byte after the lost position.

§Stats

stats returns cumulative counters (packets emitted, resyncs, dropped bytes).

Implementations§

Source§

impl TsResync

Source

pub fn new() -> Self

Create a new resynchroniser with an empty internal buffer.

Source

pub fn feed(&mut self, data: &[u8]) -> Vec<[u8; 188]>

Feed data and emit every newly-aligned 188-byte TS packet.

For a 204-byte stream the 16 Reed-Solomon parity bytes are stripped; only the 188-byte TS payload is returned. Bytes that cannot yet form a complete packet (or fall before lock) are buffered for the next call.

Drain take_losses. Every span discarded after lock is appended to an internal list (about 32-40 bytes each) that only take_losses empties; a caller that feeds forever and never drains it grows memory linearly with the number of re-locks on a lossy input.

Source

pub fn feed_into(&mut self, data: &[u8], emitted: &mut Vec<[u8; 188]>) -> usize

feed appending to a caller-owned out (not cleared), so a steady-state caller that reuses one Vec allocates nothing per call (audit r01-O4). The output capacity is reserved once from the buffered byte count instead of growing by doubling.

Returns the number of packets appended.

Source

pub fn stride(&self) -> Option<PacketStride>

Detected packet stride, or None before the stream has locked.

Source

pub fn take_losses(&mut self) -> Vec<ResyncLoss>

Drain the sync losses recorded since the last call (see ResyncLoss).

This is the only thing that empties the internal loss list, which is unbounded by design (dropping a loss would hide data loss): call it regularly — e.g. after every feed — or the list grows with every re-lock on a lossy input. If you do not need the losses, still call it and discard the result.

Source

pub fn finish(&mut self)

Declare end of input: close a sync loss that never re-locked and record a truncated trailing packet, both retrievable with take_losses. The buffer is discarded.

Source

pub fn stats(&self) -> ResyncStats

Accumulated statistics.

Trait Implementations§

Source§

impl Debug for TsResync

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TsResync

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.