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>
impl<H: AccumulatedNalHandler> NalAccumulator<H>
sourcepub fn new(nal_handler: H) -> Self
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.
sourcepub fn handler_mut(&mut self) -> &mut H
pub fn handler_mut(&mut self) -> &mut H
Gets a mutable reference to the handler.
sourcepub fn into_handler(self) -> H
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>
impl<H: AccumulatedNalHandler + Debug> Debug for NalAccumulator<H>
source§impl<H: AccumulatedNalHandler> NalFragmentHandler for NalAccumulator<H>
impl<H: AccumulatedNalHandler> NalFragmentHandler for NalAccumulator<H>
source§fn nal_fragment(&mut self, bufs: &[&[u8]], end: bool)
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 andend
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more