Skip to main content

StreamIo

Struct StreamIo 

Source
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

Source

pub fn from_read<T: Read + Send + 'static>(stream: T) -> Result<Self, Error>

Source

pub fn from_read_seek<T: Read + Seek + Send + 'static>( stream: T, ) -> Result<Self, Error>

Source

pub fn from_write<T: Write + Send + 'static>(stream: T) -> Result<Self, Error>

Source

pub fn from_write_seek<T: Write + Seek + Send + 'static>( stream: T, ) -> Result<Self, Error>

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn is_writable(&self) -> bool

Returns true if this is a write (muxing) context.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for StreamIo

Source§

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

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

impl Drop for StreamIo

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for StreamIo

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

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.