Skip to main content

AsyncAudioEncoder

Struct AsyncAudioEncoder 

Source
pub struct AsyncAudioEncoder { /* private fields */ }
Expand description

Async wrapper around AudioEncoder.

Frames are queued into a bounded channel (capacity 8) and encoded by a dedicated worker thread. When the channel is full, push suspends the caller, providing natural back-pressure.

§Construction

Use AudioEncoder::create to configure the encoder, then call AsyncAudioEncoder::from_builder:

use ff_encode::{AsyncAudioEncoder, AudioEncoder, AudioCodec};

let mut encoder = AsyncAudioEncoder::from_builder(
    AudioEncoder::create("output.m4a")
        .audio(48000, 2)
        .audio_codec(AudioCodec::Aac),
)?;

encoder.push(frame).await?;
encoder.finish().await?;

§Back-pressure

The internal channel holds at most 8 frames. Once that buffer is full, push yields until the worker drains a slot. This prevents unbounded memory growth when the encoder cannot keep up with the incoming frame rate.

Implementations§

Source§

impl AsyncAudioEncoder

Source

pub fn from_builder(builder: AudioEncoderBuilder) -> Result<Self, EncodeError>

Builds an async encoder from a configured builder.

Consumes the builder, validates the configuration, opens the output file, and starts the worker thread. The worker runs the synchronous FFmpeg encode loop in the background.

§Errors

Returns EncodeError if the builder configuration is invalid or the output file cannot be created.

Source

pub async fn push(&mut self, frame: AudioFrame) -> Result<(), EncodeError>

Queues an audio frame for encoding.

If the internal channel (capacity 8) is full, this method suspends the caller until the worker drains a slot.

§Errors

Returns EncodeError::WorkerPanicked if the worker thread has exited unexpectedly.

Source

pub async fn finish(self) -> Result<(), EncodeError>

Signals end-of-stream, flushes remaining frames, and writes the file trailer.

Drops the channel sender (signalling EOF to the worker), then waits for the worker thread to finish without blocking the async executor. Any error from the worker is propagated back to the caller.

§Errors

Returns EncodeError if encoding fails during flush or if the worker thread panicked.

Trait Implementations§

Source§

impl Debug for AsyncAudioEncoder

Source§

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

Formats the value using the given formatter. Read more

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.