Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use explicit lifetime instead of 'static for custom IO context #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

grabango-k
Copy link

I'd like to decode incoming HTTP buffers, which are not necessarily contiguous but do support the bytes::Buf protocol, using a AVIOContextCustom.

However, the read/send/seek callbacks associated with the context require that all data associated with them be Send + 'static, which limits the ability to parse data out of buffers without needing to copy.

For example, I would like to be able to create the following custom context:

/// Creates a [`AVIOContextCustom`] from an encoded video file.
/// This is streaming compatible and does not support seeking.
fn create_io_context<'a>(mut video_data: impl Buf + Send + 'a) -> AVIOContextCustom<'a> {
    let video_data_len = video_data.remaining();
    let buffer = AVMem::new(16384);
    let mut read_pos = 0;
    AVIOContextCustom::alloc_context(
        buffer,
        false,
        vec![],
        Some(Box::new(move |_, dest| {
            let end_pos = video_data_len.min(read_pos + dest.len());
            if end_pos <= read_pos {
                return ffi::AVERROR_EOF;
            }
            let read_len = end_pos - read_pos;
            let chunk = video_data.chunk();
            let bytes_read = read_len.min(chunk.len());
            dest[0..bytes_read].copy_from_slice(&chunk[..bytes_read]);
            video_data.advance(bytes_read);
            read_pos += bytes_read;
            bytes_read as i32
        })),
        None,
        None,
    )
}

This changes the API allow an explicit lifetime for data in the context, rather than requiring 'static. This would, of course, break backwards compatibility, but it has been extremely useful for my own work at least, avoiding an extra copy into a coalesced Bytes object or similar.

@ldm0 ldm0 self-requested a review September 26, 2023 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant