Struct h264_reader::push::NalAccumulator

source ·
pub struct NalAccumulator<H: AccumulatedNalHandler> { /* private fields */ }
Expand description

NAL accumulator for push parsers.

This is meant to be used by parsers for a specific format: Annex B, AVC, MPEG-TS, RTP, etc. Accumulates NALs in an internal buffer and delegates to an AccumulatedNalHandler.

use h264_reader::nal::{Nal, RefNal, UnitType};
use h264_reader::push::{NalAccumulator, NalFragmentHandler, NalInterest};
let mut calls = Vec::new();
let mut acc = NalAccumulator::new(|nal: RefNal<'_>| {
    let nal_unit_type = nal.header().unwrap().nal_unit_type();
    calls.push((nal_unit_type, nal.is_complete()));
    match nal_unit_type {
        UnitType::SeqParameterSet => NalInterest::Buffer,
        _ => NalInterest::Ignore,
    }
});

// Push a SeqParameterSet in two calls (the latter with two byte slices).
acc.nal_fragment(&[&b"\x67\x64\x00\x0A\xAC\x72\x84\x44\x26\x84\x00\x00\x03"[..]], false);
acc.nal_fragment(&[&b"\x00"[..], &b"\x04\x00\x00\x03\x00\xCA\x3C\x48\x96\x11\x80"[..]], true);

// Push a PicParameterSet in two calls.
acc.nal_fragment(&[&b"\x68"[..]], false);
acc.nal_fragment(&[&b"\xE8\x43\x8F\x13\x21\x30"[..]], true);

assert_eq!(calls, &[
    (UnitType::SeqParameterSet, false),
    (UnitType::SeqParameterSet, true),
    (UnitType::PicParameterSet, false),
    // no second call on the PicParameterSet because the handler returned Ignore.
]);

Non-trivial handlers may need to own state that can be accessed outside the handler:

use h264_reader::nal::{Nal, RefNal, UnitType};
use h264_reader::push::{AccumulatedNalHandler, NalAccumulator, NalFragmentHandler, NalInterest};
struct MyHandler(Vec<UnitType>);
impl AccumulatedNalHandler for MyHandler {
    fn nal(&mut self, nal: RefNal<'_>) -> NalInterest {
        self.0.push(nal.header().unwrap().nal_unit_type());
        NalInterest::Ignore
    }
}
let mut acc = NalAccumulator::new(MyHandler(Vec::new()));
acc.nal_fragment(&[&b"\x67\x64\x00\x0A\xAC\x72\x84\x44\x26\x84\x00\x00\x03"[..]], false);
acc.nal_fragment(&[&b"\x00"[..], &b"\x04\x00\x00\x03\x00\xCA\x3C\x48\x96\x11\x80"[..]], true);
acc.nal_fragment(&[&b"\x68"[..]], false);
acc.nal_fragment(&[&b"\xE8\x43\x8F\x13\x21\x30"[..]], true);
assert_eq!(acc.handler().0, &[
    UnitType::SeqParameterSet,
    UnitType::PicParameterSet,
]);

Implementations§

source§

impl<H: AccumulatedNalHandler> NalAccumulator<H>

source

pub fn new(nal_handler: H) -> Self

Creates a new accumulator which delegates to the given nal_handler on every push. nal_handler always sees the NAL from the beginning.

source

pub fn handler(&self) -> &H

Gets a reference to the handler.

source

pub fn handler_mut(&mut self) -> &mut H

Gets a mutable reference to the handler.

source

pub fn into_handler(self) -> H

Unwraps this NalAccumulator<h>, returning the inner handler.

Trait Implementations§

source§

impl<H: AccumulatedNalHandler + Debug> Debug for NalAccumulator<H>

source§

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

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

impl<H: AccumulatedNalHandler> NalFragmentHandler for NalAccumulator<H>

source§

fn nal_fragment(&mut self, bufs: &[&[u8]], end: bool)

Calls nal_handler with accumulated NAL unless any of the following are true:

  • a previous call on the same NAL returned NalInterest::Ignore.
  • the NAL is totally empty.
  • bufs is empty and end is false.

Auto Trait Implementations§

§

impl<H> Freeze for NalAccumulator<H>
where H: Freeze,

§

impl<H> RefUnwindSafe for NalAccumulator<H>
where H: RefUnwindSafe,

§

impl<H> Send for NalAccumulator<H>
where H: Send,

§

impl<H> Sync for NalAccumulator<H>
where H: Sync,

§

impl<H> Unpin for NalAccumulator<H>
where H: Unpin,

§

impl<H> UnwindSafe for NalAccumulator<H>
where H: UnwindSafe,

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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.