pub struct AsyncAudioDecoder { /* private fields */ }Expand description
Async wrapper around AudioDecoder.
open and decode_frame both execute on a spawn_blocking thread so the
Tokio executor is never blocked by FFmpeg I/O or decoding work.
Multiple concurrent callers share the inner decoder through Arc<Mutex<...>>.
§Examples
use ff_decode::AsyncAudioDecoder;
use futures::StreamExt;
let mut decoder = AsyncAudioDecoder::open("audio.mp3").await?;
while let Some(frame) = decoder.decode_frame().await? {
println!("audio frame with {} samples", frame.samples());
}Implementations§
Source§impl AsyncAudioDecoder
impl AsyncAudioDecoder
Sourcepub fn builder(path: impl AsRef<Path>) -> AsyncAudioDecoderBuilder
pub fn builder(path: impl AsRef<Path>) -> AsyncAudioDecoderBuilder
Returns a builder for configuring the async audio decoder.
Use this when you need to control the output sample format, sample rate,
or channel count. For zero-configuration decoding, prefer
AsyncAudioDecoder::open.
§Examples
use ff_decode::AsyncAudioDecoder;
use ff_format::SampleFormat;
let decoder = AsyncAudioDecoder::builder("audio.mp3")
.output_format(SampleFormat::F32)
.output_sample_rate(48_000)
.build()
.await?;Sourcepub async fn open(
path: impl AsRef<Path> + Send + 'static,
) -> Result<Self, DecodeError>
pub async fn open( path: impl AsRef<Path> + Send + 'static, ) -> Result<Self, DecodeError>
Opens the audio file asynchronously.
File I/O and codec initialisation are performed on a spawn_blocking
thread so the async executor is not blocked.
§Errors
Returns DecodeError if the file is missing, contains no audio
stream, or uses an unsupported codec.
Sourcepub async fn decode_frame(&mut self) -> Result<Option<AudioFrame>, DecodeError>
pub async fn decode_frame(&mut self) -> Result<Option<AudioFrame>, DecodeError>
Decodes the next audio frame.
The blocking FFmpeg call is offloaded to a spawn_blocking thread so
the Tokio executor is never blocked.
Returns Ok(None) at end of stream.
§Errors
Returns DecodeError on codec or I/O errors.
Sourcepub fn into_stream(
self,
) -> impl Stream<Item = Result<AudioFrame, DecodeError>> + Send
pub fn into_stream( self, ) -> impl Stream<Item = Result<AudioFrame, DecodeError>> + Send
Converts this decoder into a Stream of audio frames.
Decoding is offloaded to a spawn_blocking thread on each poll via
Self::decode_frame. The stream is Send and can be used with
tokio::spawn.
The stream ends when the file is exhausted (Ok(None) from
decode_frame). Errors are yielded as Err items; the stream
terminates after the first error.