pub struct UnboundObjectPool<T: Send> { /* private fields */ }Expand description
A growable pool of reusable Ts — lets a frame-producing element
(see crate::elements::SwDecoder, crate::elements::Scaler) hand
out the same buffers over and over instead of allocating a fresh one
(and freeing the old one) every single frame. Reusing an
already-allocated ffmpeg_next::frame::Video also lets ffmpeg’s own
avcodec_receive_frame/sws_scale skip their internal buffer
allocation when the reused frame already matches — not just savings
on this crate’s side.
Has no capacity wait or “pool exhausted” result:
UnboundObjectPool::get pops a previously-returned item if one’s
available, or calls init to build a fresh one on the spot otherwise.
The pool therefore grows to whatever depth turns out to be needed (e.g.
however many frames a downstream Queue lets pile up at once). As with
any caller-provided closure, a panic inside init still propagates.
Deliberately a private implementation detail owned by whichever
element produces the frames (a struct field, initialized once in that
element’s own constructor) — not something the Pipeline holds or
passes around. Nothing outside that one element needs to know a pool
is involved at all; sharing the frames themselves downstream still
goes through the ordinary Arc<UnboundObjectPoolRef<T>> in
crate::buffer::MediaBuffer::Video.
Implementations§
Source§impl<T: Send + Sync> UnboundObjectPool<T>
impl<T: Send + Sync> UnboundObjectPool<T>
Sourcepub fn new(
size: usize,
init: impl Fn() -> T + Send + Sync + 'static,
release: impl Fn(&mut T) + Send + Sync + 'static,
) -> UnboundObjectPool<T>
pub fn new( size: usize, init: impl Fn() -> T + Send + Sync + 'static, release: impl Fn(&mut T) + Send + Sync + 'static, ) -> UnboundObjectPool<T>
Pre-fills with size items built via init (0 is fine — the
pool still grows on demand, it just starts out empty and pays for
the first size-ish get() calls up front instead of amortized
over the stream). release runs on an item right before it goes
back into the pool (e.g. to reset state) — a no-op closure is
fine if there’s nothing to reset, which is the common case for a
video frame: the next consume overwrites every pixel (and every
piece of metadata it cares about, like pts) before anyone
downstream sees it again.
Sourcepub fn get(&self) -> UnboundObjectPoolRef<T>
pub fn get(&self) -> UnboundObjectPoolRef<T>
Never waits for a pooled item and has no exhaustion error — see the
type docs. If the pool is empty, this calls the supplied init
closure directly.