larksuite / rsmpeg

A Rust crate that exposes FFmpeg's power as much as possible.
https://docs.rs/rsmpeg/latest/rsmpeg/
MIT License
677 stars 41 forks source link

Getting data in multithread / async context #164

Closed vnghia closed 8 months ago

vnghia commented 8 months ago

Although I've add some getter for the data, I ended up using this where Sender/Receiver is from a channel crate of your choice.

fn make_output_io_context(buffer_size: usize, tx: Sender<Vec<u8>>) -> AVIOContextContainer {
    AVIOContextContainer::Custom(AVIOContextCustom::alloc_context(
        AVMem::new(buffer_size),
        true,
        vec![],
        None,
        Some(Box::new(move |_, data| match tx.send(data.to_vec()) {
            Ok(()) => data.len() as i32,
            Err(_) => ffi::AVERROR_EXTERNAL,
        })),
        None,
    ))
}

With this and something like std::thread::spawn(|| transcode(tx)) // no join here!!!, we can pull the data as soon as it is available. Which is a lot better than waiting for processing ends.

Do you think we should add this example to somewhere in the doc @ldm0 ?

ldm0 commented 8 months ago

Adding code examples in doc increases maintenance burden since the api changes rapidly. Currently I prefer adding tests which do specific job. :-P

vnghia commented 8 months ago

Got it. Thank you!