pub struct StreamIo { /* private fields */ }Expand description
An FFmpeg AVIOContext backed by a Rust Read/Write/Seek stream,
for custom I/O via format::input_from_stream / format::output_to_stream.
StreamIo owns both the AVIOContext and the boxed stream; dropping it
frees both. The stream must be Send + 'static (the callbacks may run on
whatever thread is driving the context), but not Sync: callbacks never
run concurrently.
A context is unidirectional: StreamIo::from_read /
StreamIo::from_read_seek create read (demuxing) contexts,
StreamIo::from_write / StreamIo::from_write_seek write (muxing)
contexts; a mismatch is rejected with EINVAL.
I/O is buffered internally (32 KiB by default; tune it with the
*_with_capacity constructors). The stream must be blocking: FFmpeg has
no retry layer for custom I/O, so the first WouldBlock/TimedOut error
poisons the context. Interrupted is retried internally like FFmpeg’s own
protocol layer retries EINTR. When the owning format context’s
AVIOInterruptCB (installed by input_from_stream_with_interrupt) is
present, the callbacks poll it at the top of every attempt and return
AVERROR_EXIT once it reports an abort. That poll is what lets a cancel
abort even a stream that keeps returning data, and
lets a LEVEL-triggered cancel (a token the stream keeps answering
Interrupted for until the caller re-arms it) abort promptly instead of
spinning. Ok(0) from read is reported as EOF.
Dropping a writable StreamIo flushes buffered data and the stream itself, discarding
errors (like std::io::BufWriter). That keep-alive is shared with any
codec::Parameters/Context derived from a stream, so the drop
and its flush run on whichever thread releases the last of those owners.
For well-formed output you must still call write_trailer first. Use
StreamIo::into_inner to get the stream back.
Implementations§
Source§impl StreamIo
impl StreamIo
pub fn from_read<T: Read + Send + 'static>(stream: T) -> Result<Self, Error>
pub fn from_read_seek<T: Read + Seek + Send + 'static>( stream: T, ) -> Result<Self, Error>
pub fn from_write<T: Write + Send + 'static>(stream: T) -> Result<Self, Error>
pub fn from_write_seek<T: Write + Seek + Send + 'static>( stream: T, ) -> Result<Self, Error>
Sourcepub fn from_read_with_capacity<T: Read + Send + 'static>(
stream: T,
capacity: usize,
) -> Result<Self, Error>
pub fn from_read_with_capacity<T: Read + Send + 'static>( stream: T, capacity: usize, ) -> Result<Self, Error>
Like StreamIo::from_read, with an explicit buffer size in bytes.
Fails with EINVAL if capacity is zero or exceeds c_int::MAX.
Sourcepub fn from_read_seek_with_capacity<T: Read + Seek + Send + 'static>(
stream: T,
capacity: usize,
) -> Result<Self, Error>
pub fn from_read_seek_with_capacity<T: Read + Seek + Send + 'static>( stream: T, capacity: usize, ) -> Result<Self, Error>
Like StreamIo::from_read_seek, with an explicit buffer size in bytes.
Fails with EINVAL if capacity is zero or exceeds c_int::MAX.
Sourcepub fn from_write_with_capacity<T: Write + Send + 'static>(
stream: T,
capacity: usize,
) -> Result<Self, Error>
pub fn from_write_with_capacity<T: Write + Send + 'static>( stream: T, capacity: usize, ) -> Result<Self, Error>
Like StreamIo::from_write, with an explicit buffer size in bytes.
Fails with EINVAL if capacity is zero or exceeds c_int::MAX.
Sourcepub fn from_write_seek_with_capacity<T: Write + Seek + Send + 'static>(
stream: T,
capacity: usize,
) -> Result<Self, Error>
pub fn from_write_seek_with_capacity<T: Write + Seek + Send + 'static>( stream: T, capacity: usize, ) -> Result<Self, Error>
Like StreamIo::from_write_seek, with an explicit buffer size in bytes.
Fails with EINVAL if capacity is zero or exceeds c_int::MAX.
Sourcepub fn is_writable(&self) -> bool
pub fn is_writable(&self) -> bool
Returns true if this is a write (muxing) context.
Sourcepub fn into_inner<T: 'static>(self) -> Result<T, Self>
pub fn into_inner<T: 'static>(self) -> Result<T, Self>
Consumes the StreamIo and returns the wrapped stream, flushing data
still buffered in the AVIOContext first. The stream’s own
Write::flush is not called, so the caller can flush and observe
errors. Fails (returning self) unless T is the exact type the
StreamIo was constructed with.
Sourcepub fn as_mut_ptr(&mut self) -> *mut AVIOContext
pub fn as_mut_ptr(&mut self) -> *mut AVIOContext
Returns a mutable raw pointer to the underlying AVIOContext.
§Safety
The returned pointer is owned by self. Do not free it or mutate its
buffer/opaque fields directly. It must not outlive self.