Skip to content

Conversation

@wgxh-cc
Copy link

@wgxh-cc wgxh-cc commented Aug 19, 2025

For asynchronous and integrated cases, the usage of AVIOContext could be awkward. Imagine that you have a stream data to sync across thread, which could be read and written continuously, then to link it to AVIOContext, we must work hard. With newly-added AVIOContextOpaque, we could do that:

let data: Arc<Mutex<Vec<u8>>> = Default::default();
let data_ = Arc::clone(&data);
thread::spawn(move || {
    data_.lock().unwrap().push(some_data)
});

let read_packet = |opaque, buf, len| {
    // just for demonstration here
    data.lock().unwrap().read_exact(buf).unwrap();

    len
};
let buffer = AVMem::new(1024);
let avio_ctx = AVIOContextOpaque::alloc_context(buffer, false, Arc::clone(&data), Some(read_packet), None, None);

@wgxh-cc
Copy link
Author

wgxh-cc commented Aug 19, 2025

Could I create a application-like example, demonstrating the API usage of rsmpeg and just enjoying?

If can, I shall put them under examples, so that one can run the pieces.

@wgxh-cc wgxh-cc changed the title Add support for custom user-managed data using AVIOContext Add support for custom user-managed data using AVIOContextOpaque Aug 19, 2025
@ldm0 ldm0 requested review from Copilot and ldm0 and removed request for ldm0 August 19, 2025 15:54
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for custom user-managed data using AVIOContextOpaque, enabling more flexible integration with asynchronous and multi-threaded scenarios where custom data types can be passed through the AVIO context. The implementation allows users to provide their own data structures (wrapped in Arc<Mutex> or similar) alongside custom read/write/seek callbacks.

Key changes:

  • Introduces AVIOContextOpaque struct with generic type support for custom data
  • Refactors existing callback infrastructure to support generic opaque data types
  • Updates AVIOContextContainer enum to include the new opaque variant

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/avformat/avio.rs Adds AVIOContextOpaque implementation with generic type support and refactors callback infrastructure
src/avformat/avformat.rs Updates AVIOContextContainer enum to include opaque variant and simplifies pointer access pattern

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

}

#[cfg(not(feature = "ffmpeg7"))]
unsafe extern "C" fn write_c<T: Send + Sync>(
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two write_c functions defined with conflicting configurations. The second function at line 124-132 should be pub to match the visibility of other functions in the module.

Suggested change
unsafe extern "C" fn write_c<T: Send + Sync>(
pub unsafe extern "C" fn write_c<T: Send + Sync>(

Copilot uses AI. Check for mistakes.
}
}

impl Drop for AVIOContextOpaque {
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Drop implementation for AVIOContextOpaque doesn't handle the opaque data cleanup. The opaque pointer passed to avio_alloc_context needs to be properly freed, but there's no mechanism to recover and drop the Box<Opaque> that was leaked in alloc_context.

Copilot uses AI. Check for mistakes.

// pub type ReadPacketCallback = Box<dyn FnMut(&mut Vec<u8>, &mut [u8]) -> i32 + Send + 'static>;
// pub type WritePacketCallback = Box<dyn FnMut(&mut Vec<u8>, &[u8]) -> i32 + Send + 'static>;
// pub type SeekCallback = Box<dyn FnMut(&mut Vec<u8>, i64, i32) -> i64 + Send + 'static>;
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented-out code. These type aliases are now defined in the opaque module and should not be left as dead comments.

Suggested change
// pub type SeekCallback = Box<dyn FnMut(&mut Vec<u8>, i64, i32) -> i64 + Send + 'static>;

Copilot uses AI. Check for mistakes.
@@ -1,5 +1,6 @@
use std::{
ffi::{c_void, CStr},
marker::PhantomData,
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PhantomData is imported but never used in the code. Remove this unused import.

Suggested change
marker::PhantomData,

Copilot uses AI. Check for mistakes.
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