gstreamer-rs/gstreamer/src/buffer_pool.rs

381 lines
11 KiB
Rust
Raw Normal View History

2020-12-15 10:53:31 +00:00
// Take a look at the license at the top of the repository in the LICENSE file.
2018-03-07 09:07:30 +00:00
use crate::AllocationParams;
use crate::Allocator;
use crate::BufferPool;
use crate::Structure;
2018-03-07 09:07:30 +00:00
2021-04-26 12:15:53 +00:00
use glib::prelude::*;
2021-04-28 12:34:56 +00:00
use glib::translate::*;
2018-03-07 09:07:30 +00:00
use std::mem;
use std::ops;
2018-04-01 08:30:03 +00:00
use std::ptr;
2018-03-07 09:07:30 +00:00
#[derive(Debug, Clone, PartialEq, Eq)]
2018-03-07 09:07:30 +00:00
pub struct BufferPoolConfig(Structure);
impl ops::Deref for BufferPoolConfig {
type Target = crate::StructureRef;
2018-03-07 09:07:30 +00:00
fn deref(&self) -> &crate::StructureRef {
2018-03-07 09:07:30 +00:00
self.0.deref()
}
}
impl ops::DerefMut for BufferPoolConfig {
fn deref_mut(&mut self) -> &mut crate::StructureRef {
2018-03-07 09:07:30 +00:00
self.0.deref_mut()
}
}
impl AsRef<crate::StructureRef> for BufferPoolConfig {
fn as_ref(&self) -> &crate::StructureRef {
2018-03-07 09:07:30 +00:00
self.0.as_ref()
}
}
impl AsMut<crate::StructureRef> for BufferPoolConfig {
fn as_mut(&mut self) -> &mut crate::StructureRef {
2018-03-07 09:07:30 +00:00
self.0.as_mut()
}
}
impl BufferPoolConfig {
pub fn add_option(&mut self, option: &str) {
unsafe {
ffi::gst_buffer_pool_config_add_option(
2018-03-07 09:07:30 +00:00
self.0.to_glib_none_mut().0,
option.to_glib_none().0,
);
}
}
pub fn has_option(&self, option: &str) -> bool {
unsafe {
from_glib(ffi::gst_buffer_pool_config_has_option(
2018-03-07 09:07:30 +00:00
self.0.to_glib_none().0,
option.to_glib_none().0,
))
}
}
#[doc(alias = "get_options")]
2021-04-11 19:39:50 +00:00
pub fn options(&self) -> Vec<String> {
2018-03-07 09:07:30 +00:00
unsafe {
let n = ffi::gst_buffer_pool_config_n_options(self.0.to_glib_none().0) as usize;
2018-03-07 09:07:30 +00:00
let mut options = Vec::with_capacity(n);
for i in 0..n {
options.push(from_glib_none(ffi::gst_buffer_pool_config_get_option(
2018-03-07 09:07:30 +00:00
self.0.to_glib_none().0,
i as u32,
)));
}
options
}
}
pub fn set_params(
2018-03-07 09:07:30 +00:00
&mut self,
caps: Option<&crate::Caps>,
2018-03-07 09:07:30 +00:00
size: u32,
min_buffers: u32,
max_buffers: u32,
) {
unsafe {
ffi::gst_buffer_pool_config_set_params(
2018-03-07 09:07:30 +00:00
self.0.to_glib_none_mut().0,
caps.to_glib_none().0,
size,
min_buffers,
max_buffers,
);
}
}
#[doc(alias = "get_params")]
2021-04-11 19:39:50 +00:00
pub fn params(&self) -> Option<(Option<crate::Caps>, u32, u32, u32)> {
2018-03-07 09:07:30 +00:00
unsafe {
let mut caps = ptr::null_mut();
let mut size = mem::MaybeUninit::uninit();
let mut min_buffers = mem::MaybeUninit::uninit();
let mut max_buffers = mem::MaybeUninit::uninit();
2018-03-07 09:07:30 +00:00
let ret: bool = from_glib(ffi::gst_buffer_pool_config_get_params(
2018-03-07 09:07:30 +00:00
self.0.to_glib_none().0,
&mut caps,
size.as_mut_ptr(),
min_buffers.as_mut_ptr(),
max_buffers.as_mut_ptr(),
2018-03-07 09:07:30 +00:00
));
if !ret {
return None;
}
Some((
from_glib_none(caps),
size.assume_init(),
min_buffers.assume_init(),
max_buffers.assume_init(),
))
2018-03-07 09:07:30 +00:00
}
}
pub fn validate_params(
2018-03-07 09:07:30 +00:00
&self,
caps: Option<&crate::Caps>,
2018-03-07 09:07:30 +00:00
size: u32,
min_buffers: u32,
max_buffers: u32,
) -> Result<(), glib::BoolError> {
2018-03-07 09:07:30 +00:00
unsafe {
glib::result_from_gboolean!(
ffi::gst_buffer_pool_config_validate_params(
self.0.to_glib_none().0,
caps.to_glib_none().0,
size,
min_buffers,
max_buffers,
),
"Parameters are not valid in this context"
)
2018-03-07 09:07:30 +00:00
}
}
#[doc(alias = "get_allocator")]
2021-04-11 19:39:50 +00:00
pub fn allocator(&self) -> Option<(Option<Allocator>, AllocationParams)> {
unsafe {
let mut allocator = ptr::null_mut();
let mut params = mem::MaybeUninit::zeroed();
let ret = from_glib(ffi::gst_buffer_pool_config_get_allocator(
self.0.to_glib_none().0,
&mut allocator,
params.as_mut_ptr(),
));
if ret {
Some((from_glib_none(allocator), params.assume_init().into()))
} else {
None
}
}
}
pub fn set_allocator(&self, allocator: Option<&Allocator>, params: Option<&AllocationParams>) {
assert!(allocator.is_some() || params.is_some());
unsafe {
ffi::gst_buffer_pool_config_set_allocator(
self.0.to_glib_none().0,
allocator.to_glib_none().0,
match params {
Some(val) => val.as_ptr(),
None => ptr::null(),
},
)
}
}
2018-03-07 09:07:30 +00:00
// TODO: options iterator
}
#[derive(Debug)]
pub struct BufferPoolAcquireParams(ffi::GstBufferPoolAcquireParams);
2018-03-07 09:07:30 +00:00
unsafe impl Send for BufferPoolAcquireParams {}
unsafe impl Sync for BufferPoolAcquireParams {}
2018-03-07 09:07:30 +00:00
impl BufferPoolAcquireParams {
pub fn with_flags(flags: crate::BufferPoolAcquireFlags) -> Self {
2020-03-22 14:18:47 +00:00
skip_assert_initialized!();
BufferPoolAcquireParams(ffi::GstBufferPoolAcquireParams {
format: ffi::GST_FORMAT_UNDEFINED,
2018-03-07 09:07:30 +00:00
start: -1,
stop: -1,
flags: flags.into_glib(),
2018-03-07 09:07:30 +00:00
_gst_reserved: [ptr::null_mut(); 4],
})
}
pub fn with_start_stop<T: crate::SpecificFormattedValue>(
2018-03-07 09:07:30 +00:00
start: T,
stop: T,
flags: crate::BufferPoolAcquireFlags,
2018-03-07 09:07:30 +00:00
) -> Self {
2020-03-22 14:18:47 +00:00
skip_assert_initialized!();
2018-03-07 09:07:30 +00:00
unsafe {
BufferPoolAcquireParams(ffi::GstBufferPoolAcquireParams {
format: start.format().into_glib(),
2018-03-07 09:07:30 +00:00
start: start.to_raw_value(),
stop: stop.to_raw_value(),
flags: flags.into_glib(),
2018-03-07 09:07:30 +00:00
_gst_reserved: [ptr::null_mut(); 4],
})
}
}
pub fn flags(&self) -> crate::BufferPoolAcquireFlags {
unsafe { from_glib(self.0.flags) }
2018-03-07 09:07:30 +00:00
}
pub fn format(&self) -> crate::Format {
unsafe { from_glib(self.0.format) }
2018-03-07 09:07:30 +00:00
}
pub fn start(&self) -> crate::GenericFormattedValue {
unsafe { crate::GenericFormattedValue::new(from_glib(self.0.format), self.0.start) }
2018-03-07 09:07:30 +00:00
}
pub fn stop(&self) -> crate::GenericFormattedValue {
unsafe { crate::GenericFormattedValue::new(from_glib(self.0.format), self.0.stop) }
2018-03-07 09:07:30 +00:00
}
}
impl PartialEq for BufferPoolAcquireParams {
fn eq(&self, other: &Self) -> bool {
2018-07-27 10:36:40 +00:00
self.format() == other.format()
&& self.start() == other.start()
2018-03-07 09:07:30 +00:00
&& self.stop() == other.stop()
}
}
impl Eq for BufferPoolAcquireParams {}
#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::GstBufferPoolAcquireParams> for BufferPoolAcquireParams {
type Storage = &'a Self;
fn to_glib_none(
&'a self,
) -> glib::translate::Stash<'a, *const ffi::GstBufferPoolAcquireParams, Self> {
glib::translate::Stash(&self.0, self)
}
}
#[doc(hidden)]
impl<'a> ToGlibPtrMut<'a, *mut ffi::GstBufferPoolAcquireParams> for BufferPoolAcquireParams {
type Storage = &'a mut Self;
fn to_glib_none_mut(
&'a mut self,
) -> glib::translate::StashMut<'a, *mut ffi::GstBufferPoolAcquireParams, Self> {
glib::translate::StashMut(&mut self.0, self)
}
}
impl BufferPool {
pub fn new() -> BufferPool {
assert_initialized_main_thread!();
let (major, minor, _, _) = crate::version();
if (major, minor) > (1, 12) {
unsafe { from_glib_full(ffi::gst_buffer_pool_new()) }
} else {
// Work-around for 1.14 switching from transfer-floating to transfer-full
unsafe { from_glib_none(ffi::gst_buffer_pool_new()) }
}
}
}
impl Default for BufferPool {
fn default() -> Self {
Self::new()
}
}
pub trait BufferPoolExtManual: 'static {
#[doc(alias = "get_config")]
2021-04-11 19:39:50 +00:00
fn config(&self) -> BufferPoolConfig;
2018-03-07 09:07:30 +00:00
fn set_config(&self, config: BufferPoolConfig) -> Result<(), glib::error::BoolError>;
fn is_flushing(&self) -> bool;
fn acquire_buffer(
2018-03-07 09:07:30 +00:00
&self,
params: Option<&BufferPoolAcquireParams>,
) -> Result<crate::Buffer, crate::FlowError>;
fn release_buffer(&self, buffer: crate::Buffer);
2018-03-07 09:07:30 +00:00
}
impl<O: IsA<BufferPool>> BufferPoolExtManual for O {
2021-04-11 19:39:50 +00:00
fn config(&self) -> BufferPoolConfig {
2018-03-07 09:07:30 +00:00
unsafe {
let ptr = ffi::gst_buffer_pool_get_config(self.as_ref().to_glib_none().0);
2018-03-07 09:07:30 +00:00
BufferPoolConfig(from_glib_full(ptr))
}
}
fn set_config(&self, config: BufferPoolConfig) -> Result<(), glib::error::BoolError> {
unsafe {
glib::result_from_gboolean!(
ffi::gst_buffer_pool_set_config(
self.as_ref().to_glib_none().0,
config.0.into_ptr()
),
2018-03-07 09:07:30 +00:00
"Failed to set config",
)
}
}
fn is_flushing(&self) -> bool {
unsafe {
let stash = self.as_ref().to_glib_none();
let ptr: *mut ffi::GstBufferPool = stash.0;
2018-03-07 09:07:30 +00:00
from_glib((*ptr).flushing)
}
}
fn acquire_buffer(
2018-03-07 09:07:30 +00:00
&self,
params: Option<&BufferPoolAcquireParams>,
) -> Result<crate::Buffer, crate::FlowError> {
let params_ptr = params.to_glib_none().0 as *mut _;
2018-03-07 09:07:30 +00:00
unsafe {
let mut buffer = ptr::null_mut();
2021-04-28 12:34:56 +00:00
crate::FlowSuccess::try_from_glib(ffi::gst_buffer_pool_acquire_buffer(
self.as_ref().to_glib_none().0,
2018-03-07 09:07:30 +00:00
&mut buffer,
params_ptr,
2021-04-28 12:34:56 +00:00
))
.map(|_| from_glib_full(buffer))
2018-03-07 09:07:30 +00:00
}
}
fn release_buffer(&self, buffer: crate::Buffer) {
2018-03-07 09:07:30 +00:00
unsafe {
ffi::gst_buffer_pool_release_buffer(self.as_ref().to_glib_none().0, buffer.into_ptr());
2018-03-07 09:07:30 +00:00
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::*;
2018-03-07 09:07:30 +00:00
#[test]
fn test_pool() {
crate::init().unwrap();
2018-03-07 09:07:30 +00:00
let pool = crate::BufferPool::new();
2021-04-11 19:39:50 +00:00
let mut config = pool.config();
config.set_params(Some(&crate::Caps::new_simple("foo/bar", &[])), 1024, 0, 2);
2018-03-07 09:07:30 +00:00
pool.set_config(config).unwrap();
pool.set_active(true).unwrap();
let params =
crate::BufferPoolAcquireParams::with_flags(crate::BufferPoolAcquireFlags::DONTWAIT);
2018-03-07 09:07:30 +00:00
let _buf1 = pool.acquire_buffer(Some(&params)).unwrap();
let buf2 = pool.acquire_buffer(Some(&params)).unwrap();
2018-03-07 09:07:30 +00:00
assert!(pool.acquire_buffer(Some(&params)).is_err());
2018-03-07 09:07:30 +00:00
drop(buf2);
let _buf2 = pool.acquire_buffer(Some(&params)).unwrap();
2018-03-07 09:07:30 +00:00
pool.set_active(false).unwrap();
}
}