use crate::sys as ffi;
use crate::camera::Camera;
use crate::error::{Error, Result};
use crate::frame_buffer::FrameBuffer;
use crate::stream::Stream;
pub struct FrameBufferAllocator {
ptr: *mut ffi::lc_frame_buffer_allocator_t,
}
unsafe impl Send for FrameBufferAllocator {}
impl FrameBufferAllocator {
pub fn new(camera: &Camera) -> Self {
let ptr = unsafe { ffi::lc_frame_buffer_allocator_create(camera.ptr) };
Self { ptr }
}
pub fn allocate(&self, stream: &Stream) -> Result<usize> {
let ret = unsafe { ffi::lc_frame_buffer_allocator_allocate(self.ptr, stream.ptr) };
if ret < 0 {
return Err(Error::AllocateFailed);
}
Ok(ret as usize)
}
pub fn free(&self, stream: &Stream) -> Result<()> {
let ret = unsafe { ffi::lc_frame_buffer_allocator_free(self.ptr, stream.ptr) };
if ret < 0 {
return Err(Error::FreeFailed);
}
Ok(())
}
pub fn buffers_count(&self, stream: &Stream) -> usize {
unsafe { ffi::lc_frame_buffer_allocator_buffers_count(self.ptr, stream.ptr) }
}
pub fn get_buffer(&self, stream: &Stream, index: usize) -> Result<FrameBuffer> {
let fb_ptr =
unsafe { ffi::lc_frame_buffer_allocator_get_buffer(self.ptr, stream.ptr, index) };
if fb_ptr.is_null() {
return Err(Error::IndexOutOfRange);
}
Ok(FrameBuffer::from_raw(fb_ptr))
}
}
impl Drop for FrameBufferAllocator {
fn drop(&mut self) {
unsafe { ffi::lc_frame_buffer_allocator_destroy(self.ptr) };
}
}