pub struct CMBlockBuffer(/* private fields */);Expand description
Block buffer containing contiguous media data
CMBlockBuffer is a Core Media type that represents a block of data,
commonly used for audio samples or compressed video data. The data is
managed by Core Media and released when the buffer is dropped.
Unlike CVPixelBuffer or IOSurface, CMBlockBuffer does not require
locking for data access - the data pointer is valid as long as the buffer
is retained.
§Examples
use screencapturekit::cm::CMBlockBuffer;
fn process_block_buffer(buffer: &CMBlockBuffer) {
// Check if there's any data
if buffer.is_empty() {
return;
}
println!("Buffer has {} bytes", buffer.data_length());
// Get a pointer to the data
if let Some((ptr, length)) = buffer.data_pointer(0) {
println!("Got {} bytes at offset 0", length);
}
// Or copy data to a Vec
if let Some(data) = buffer.copy_data_bytes(0, buffer.data_length()) {
println!("Copied {} bytes", data.len());
}
}Implementations§
Source§impl CMBlockBuffer
impl CMBlockBuffer
Sourcepub fn create(data: &[u8]) -> Option<Self>
pub fn create(data: &[u8]) -> Option<Self>
Create a new CMBlockBuffer with the given data
§Arguments
data- The data to copy into the block buffer
§Returns
Some(CMBlockBuffer) if successful, None if creation failed.
§Examples
use screencapturekit::cm::CMBlockBuffer;
let data = vec![1u8, 2, 3, 4, 5];
let buffer = CMBlockBuffer::create(&data).expect("Failed to create buffer");
assert_eq!(buffer.data_length(), 5);Sourcepub fn create_empty() -> Option<Self>
pub fn create_empty() -> Option<Self>
Sourcepub fn from_raw(ptr: *mut c_void) -> Option<Self>
pub fn from_raw(ptr: *mut c_void) -> Option<Self>
Create from a raw pointer, returning None if null
Sourcepub unsafe fn from_ptr(ptr: *mut c_void) -> Self
pub unsafe fn from_ptr(ptr: *mut c_void) -> Self
§Safety
The caller must ensure the pointer is a valid CMBlockBuffer pointer.
Sourcepub fn data_length(&self) -> usize
pub fn data_length(&self) -> usize
Get the total data length of the buffer in bytes
§Examples
use screencapturekit::cm::CMBlockBuffer;
fn check_size(buffer: &CMBlockBuffer) {
let size = buffer.data_length();
println!("Buffer contains {} bytes", size);
}Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the buffer is empty (contains no data)
§Examples
use screencapturekit::cm::CMBlockBuffer;
fn process(buffer: &CMBlockBuffer) {
if buffer.is_empty() {
println!("No data to process");
return;
}
// Process data...
}Sourcepub fn is_range_contiguous(&self, offset: usize, length: usize) -> bool
pub fn is_range_contiguous(&self, offset: usize, length: usize) -> bool
Sourcepub fn data_pointer(&self, offset: usize) -> Option<(*const u8, usize)>
pub fn data_pointer(&self, offset: usize) -> Option<(*const u8, usize)>
Get a pointer to the data at the specified offset
Returns a tuple of (data pointer, length available at that offset) if successful.
The pointer is valid as long as this CMBlockBuffer is retained.
§Arguments
offset- Byte offset into the buffer
§Returns
Some((pointer, length_at_offset)) if the data pointer was obtained successfully,
None if the operation failed.
§Examples
use screencapturekit::cm::CMBlockBuffer;
fn read_data(buffer: &CMBlockBuffer) {
if let Some((ptr, length)) = buffer.data_pointer(0) {
// SAFETY: ptr is valid for `length` bytes while buffer is alive
let slice = unsafe { std::slice::from_raw_parts(ptr, length) };
println!("First byte: {:02x}", slice[0]);
}
}Sourcepub unsafe fn data_pointer_mut(&self, offset: usize) -> Option<(*mut u8, usize)>
pub unsafe fn data_pointer_mut(&self, offset: usize) -> Option<(*mut u8, usize)>
Get a mutable pointer to the data at the specified offset
§Safety
The caller must ensure that modifying the data is safe and that no other references to this data exist.
Sourcepub fn copy_data_bytes(&self, offset: usize, length: usize) -> Option<Vec<u8>>
pub fn copy_data_bytes(&self, offset: usize, length: usize) -> Option<Vec<u8>>
Copy data bytes from the buffer into a new Vec<u8>
This is the safest way to access buffer data as it copies the bytes into owned memory.
§Arguments
offset- Starting offset in the bufferlength- Number of bytes to copy
§Returns
Some(Vec<u8>) containing the copied data, or None if the copy failed.
§Examples
use screencapturekit::cm::CMBlockBuffer;
fn extract_data(buffer: &CMBlockBuffer) -> Option<Vec<u8>> {
// Copy all data from the buffer
buffer.copy_data_bytes(0, buffer.data_length())
}Sourcepub fn copy_data_bytes_into(
&self,
offset: usize,
destination: &mut [u8],
) -> Result<(), i32>
pub fn copy_data_bytes_into( &self, offset: usize, destination: &mut [u8], ) -> Result<(), i32>
Copy data bytes from the buffer into an existing slice
§Arguments
offset- Starting offset in the bufferdestination- Mutable slice to copy data into
§Errors
Returns a Core Media error code if the copy fails.
§Examples
use screencapturekit::cm::CMBlockBuffer;
fn read_header(buffer: &CMBlockBuffer) -> Result<[u8; 4], i32> {
let mut header = [0u8; 4];
buffer.copy_data_bytes_into(0, &mut header)?;
Ok(header)
}Sourcepub fn as_slice(&self) -> Option<&[u8]>
pub fn as_slice(&self) -> Option<&[u8]>
Get a slice view of the data if the entire buffer is contiguous
This is a zero-copy way to access the data, but only works if the buffer’s data is stored contiguously in memory.
§Returns
Some(&[u8]) if the buffer is contiguous, None otherwise.
§Examples
use screencapturekit::cm::CMBlockBuffer;
fn process_contiguous(buffer: &CMBlockBuffer) {
if let Some(data) = buffer.as_slice() {
println!("Processing {} contiguous bytes", data.len());
} else {
// Fall back to copying
if let Some(data) = buffer.copy_data_bytes(0, buffer.data_length()) {
println!("Processing {} copied bytes", data.len());
}
}
}Sourcepub fn cursor(&self) -> Option<Cursor<Vec<u8>>>
pub fn cursor(&self) -> Option<Cursor<Vec<u8>>>
Access buffer with a standard std::io::Cursor
Returns a cursor over a copy of the buffer data. The cursor implements
Read and Seek traits for convenient sequential data access.
Note: This copies the data because CMBlockBuffer may not be contiguous.
For zero-copy access to contiguous buffers, use as_slice().
§Returns
Some(Cursor) if data could be copied, None if the copy failed.
§Examples
use std::io::{Read, Seek, SeekFrom};
use screencapturekit::cm::CMBlockBuffer;
fn read_data(buffer: &CMBlockBuffer) {
if let Some(mut cursor) = buffer.cursor() {
// Read first 4 bytes
let mut header = [0u8; 4];
cursor.read_exact(&mut header).unwrap();
// Seek to a position
cursor.seek(SeekFrom::Start(100)).unwrap();
// Read more data
let mut buf = [0u8; 16];
cursor.read_exact(&mut buf).unwrap();
}
}Sourcepub fn cursor_ref(&self) -> Option<Cursor<&[u8]>>
pub fn cursor_ref(&self) -> Option<Cursor<&[u8]>>
Access contiguous buffer with a zero-copy std::io::Cursor
Returns a cursor over the buffer data without copying, but only works if the buffer is contiguous in memory.
§Returns
Some(Cursor) if the buffer is contiguous, None otherwise.
§Examples
use std::io::{Read, Seek, SeekFrom};
use screencapturekit::cm::CMBlockBuffer;
fn read_contiguous(buffer: &CMBlockBuffer) {
// Try zero-copy first
if let Some(mut cursor) = buffer.cursor_ref() {
let mut header = [0u8; 4];
cursor.read_exact(&mut header).unwrap();
} else {
// Fall back to copying cursor
if let Some(mut cursor) = buffer.cursor() {
let mut header = [0u8; 4];
cursor.read_exact(&mut header).unwrap();
}
}
}