support for subclassing buffer pool

This commit is contained in:
Christian Meissl 2022-02-01 10:12:05 +01:00
parent 0173b73170
commit 033dac2270
3 changed files with 727 additions and 21 deletions

View file

@ -4,51 +4,106 @@ use crate::AllocationParams;
use crate::Allocator;
use crate::BufferPool;
use crate::Structure;
use crate::StructureRef;
use glib::prelude::*;
use glib::translate::*;
use std::mem;
use std::ops;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ptr;
#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct BufferPoolConfig(Structure);
impl ops::Deref for BufferPoolConfig {
impl Deref for BufferPoolConfig {
type Target = BufferPoolConfigRef;
fn deref(&self) -> &BufferPoolConfigRef {
unsafe { &*(self.0.as_ptr() as *const StructureRef as *const BufferPoolConfigRef) }
}
}
impl DerefMut for BufferPoolConfig {
fn deref_mut(&mut self) -> &mut BufferPoolConfigRef {
unsafe { &mut *(self.0.as_ptr() as *mut StructureRef as *mut BufferPoolConfigRef) }
}
}
impl AsRef<BufferPoolConfigRef> for BufferPoolConfig {
fn as_ref(&self) -> &BufferPoolConfigRef {
self.deref()
}
}
impl AsMut<BufferPoolConfigRef> for BufferPoolConfig {
fn as_mut(&mut self) -> &mut BufferPoolConfigRef {
self.deref_mut()
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct BufferPoolConfigRef(StructureRef);
impl BufferPoolConfigRef {
pub unsafe fn from_glib_borrow<'a>(ptr: *const ffi::GstStructure) -> &'a BufferPoolConfigRef {
assert!(!ptr.is_null());
&*(ptr as *mut StructureRef as *mut BufferPoolConfigRef)
}
pub unsafe fn from_glib_borrow_mut<'a>(
ptr: *mut ffi::GstStructure,
) -> &'a mut BufferPoolConfigRef {
assert!(!ptr.is_null());
&mut *(ptr as *mut StructureRef as *mut BufferPoolConfigRef)
}
pub unsafe fn as_ptr(&self) -> *const ffi::GstStructure {
self as *const Self as *const ffi::GstStructure
}
pub unsafe fn as_mut_ptr(&self) -> *mut ffi::GstStructure {
self as *const Self as *mut ffi::GstStructure
}
}
impl ops::Deref for BufferPoolConfigRef {
type Target = crate::StructureRef;
fn deref(&self) -> &crate::StructureRef {
self.0.deref()
&self.0
}
}
impl ops::DerefMut for BufferPoolConfig {
impl ops::DerefMut for BufferPoolConfigRef {
fn deref_mut(&mut self) -> &mut crate::StructureRef {
self.0.deref_mut()
&mut self.0
}
}
impl AsRef<crate::StructureRef> for BufferPoolConfig {
impl AsRef<crate::StructureRef> for BufferPoolConfigRef {
fn as_ref(&self) -> &crate::StructureRef {
self.0.as_ref()
&self.0
}
}
impl AsMut<crate::StructureRef> for BufferPoolConfig {
impl AsMut<crate::StructureRef> for BufferPoolConfigRef {
fn as_mut(&mut self) -> &mut crate::StructureRef {
self.0.as_mut()
&mut self.0
}
}
impl BufferPoolConfig {
impl BufferPoolConfigRef {
#[doc(alias = "gst_buffer_pool_config_add_option")]
pub fn add_option(&mut self, option: &str) {
unsafe {
ffi::gst_buffer_pool_config_add_option(
self.0.to_glib_none_mut().0,
option.to_glib_none().0,
);
ffi::gst_buffer_pool_config_add_option(self.0.as_mut_ptr(), option.to_glib_none().0);
}
}
@ -56,7 +111,7 @@ impl BufferPoolConfig {
pub fn has_option(&self, option: &str) -> bool {
unsafe {
from_glib(ffi::gst_buffer_pool_config_has_option(
self.0.to_glib_none().0,
self.0.as_mut_ptr(),
option.to_glib_none().0,
))
}
@ -66,12 +121,12 @@ impl BufferPoolConfig {
#[doc(alias = "gst_buffer_pool_config_n_options")]
pub fn options(&self) -> Vec<String> {
unsafe {
let n = ffi::gst_buffer_pool_config_n_options(self.0.to_glib_none().0) as usize;
let n = ffi::gst_buffer_pool_config_n_options(self.0.as_mut_ptr()) as usize;
let mut options = Vec::with_capacity(n);
for i in 0..n {
options.push(from_glib_none(ffi::gst_buffer_pool_config_get_option(
self.0.to_glib_none().0,
self.0.as_mut_ptr(),
i as u32,
)));
}
@ -90,7 +145,7 @@ impl BufferPoolConfig {
) {
unsafe {
ffi::gst_buffer_pool_config_set_params(
self.0.to_glib_none_mut().0,
self.0.as_mut_ptr(),
caps.to_glib_none().0,
size,
min_buffers,
@ -109,7 +164,7 @@ impl BufferPoolConfig {
let mut max_buffers = mem::MaybeUninit::uninit();
let ret: bool = from_glib(ffi::gst_buffer_pool_config_get_params(
self.0.to_glib_none().0,
self.0.as_mut_ptr(),
&mut caps,
size.as_mut_ptr(),
min_buffers.as_mut_ptr(),
@ -139,7 +194,7 @@ impl BufferPoolConfig {
unsafe {
glib::result_from_gboolean!(
ffi::gst_buffer_pool_config_validate_params(
self.0.to_glib_none().0,
self.0.as_mut_ptr(),
caps.to_glib_none().0,
size,
min_buffers,
@ -157,7 +212,7 @@ impl BufferPoolConfig {
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,
self.0.as_mut_ptr(),
&mut allocator,
params.as_mut_ptr(),
));
@ -174,7 +229,7 @@ impl BufferPoolConfig {
assert!(allocator.is_some() || params.is_some());
unsafe {
ffi::gst_buffer_pool_config_set_allocator(
self.0.to_glib_none().0,
self.0.as_mut_ptr(),
allocator.to_glib_none().0,
match params {
Some(val) => val.as_ptr(),
@ -271,6 +326,13 @@ impl<'a> ToGlibPtrMut<'a, *mut ffi::GstBufferPoolAcquireParams> for BufferPoolAc
}
}
#[doc(hidden)]
impl FromGlibPtrNone<*mut ffi::GstBufferPoolAcquireParams> for BufferPoolAcquireParams {
unsafe fn from_glib_none(ptr: *mut ffi::GstBufferPoolAcquireParams) -> Self {
Self(*ptr)
}
}
impl BufferPool {
#[doc(alias = "gst_buffer_pool_new")]
pub fn new() -> BufferPool {

View file

@ -0,0 +1,642 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use super::prelude::*;
use glib::{
subclass::{prelude::*, InitializingObject},
translate::*,
Cast, StaticType,
};
use libc::c_char;
use crate::{BufferPool, BufferPoolAcquireParams, BufferPoolConfigRef};
pub trait BufferPoolImpl: BufferPoolImplExt + GstObjectImpl + Send + Sync {
fn acquire_buffer(
&self,
buffer_pool: &Self::Type,
params: Option<&BufferPoolAcquireParams>,
) -> Result<crate::Buffer, crate::FlowError> {
self.parent_acquire_buffer(buffer_pool, params)
}
fn alloc_buffer(
&self,
buffer_pool: &Self::Type,
params: Option<&BufferPoolAcquireParams>,
) -> Result<crate::Buffer, crate::FlowError> {
self.parent_alloc_buffer(buffer_pool, params)
}
fn flush_start(&self, buffer_pool: &Self::Type) {
self.parent_flush_start(buffer_pool)
}
fn flush_stop(&self, buffer_pool: &Self::Type) {
self.parent_flush_stop(buffer_pool)
}
fn free_buffer(&self, buffer_pool: &Self::Type, buffer: crate::Buffer) {
self.parent_free_buffer(buffer_pool, buffer)
}
fn release_buffer(&self, buffer_pool: &Self::Type, buffer: crate::Buffer) {
self.parent_release_buffer(buffer_pool, buffer)
}
fn reset_buffer(&self, buffer_pool: &Self::Type, buffer: &mut crate::BufferRef) {
self.parent_reset_buffer(buffer_pool, buffer)
}
fn start(&self, buffer_pool: &Self::Type) -> bool {
self.parent_start(buffer_pool)
}
fn stop(&self, buffer_pool: &Self::Type) -> bool {
self.parent_stop(buffer_pool)
}
fn options() -> &'static [&'static str] {
&[]
}
fn set_config(&self, buffer_pool: &Self::Type, config: &mut BufferPoolConfigRef) -> bool {
self.parent_set_config(buffer_pool, config)
}
}
pub trait BufferPoolImplExt: ObjectSubclass {
fn parent_acquire_buffer(
&self,
buffer_pool: &Self::Type,
params: Option<&BufferPoolAcquireParams>,
) -> Result<crate::Buffer, crate::FlowError>;
fn parent_alloc_buffer(
&self,
buffer_pool: &Self::Type,
params: Option<&BufferPoolAcquireParams>,
) -> Result<crate::Buffer, crate::FlowError>;
fn parent_free_buffer(&self, buffer_pool: &Self::Type, buffer: crate::Buffer);
fn parent_release_buffer(&self, buffer_pool: &Self::Type, buffer: crate::Buffer);
fn parent_reset_buffer(&self, buffer_pool: &Self::Type, buffer: &mut crate::BufferRef);
fn parent_start(&self, buffer_pool: &Self::Type) -> bool;
fn parent_stop(&self, buffer_pool: &Self::Type) -> bool;
fn parent_set_config(&self, buffer_pool: &Self::Type, config: &mut BufferPoolConfigRef)
-> bool;
fn parent_flush_start(&self, _buffer_pool: &Self::Type);
fn parent_flush_stop(&self, _buffer_pool: &Self::Type);
}
impl<T: BufferPoolImpl> BufferPoolImplExt for T {
fn parent_acquire_buffer(
&self,
buffer_pool: &Self::Type,
params: Option<&BufferPoolAcquireParams>,
) -> Result<crate::Buffer, crate::FlowError> {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).acquire_buffer {
let params_ptr = mut_override(params.to_glib_none().0);
let mut buffer = std::ptr::null_mut();
let result = f(
buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0,
&mut buffer,
params_ptr,
);
crate::FlowSuccess::try_from_glib(result).map(|_| from_glib_full(buffer))
} else {
Err(crate::FlowError::NotSupported)
}
}
}
fn parent_alloc_buffer(
&self,
buffer_pool: &Self::Type,
params: Option<&BufferPoolAcquireParams>,
) -> Result<crate::Buffer, crate::FlowError> {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).alloc_buffer {
let params_ptr = mut_override(params.to_glib_none().0);
let mut buffer = std::ptr::null_mut();
let result = f(
buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0,
&mut buffer,
params_ptr,
);
crate::FlowSuccess::try_from_glib(result).map(|_| from_glib_full(buffer))
} else {
Err(crate::FlowError::NotSupported)
}
}
}
fn parent_free_buffer(&self, buffer_pool: &Self::Type, buffer: crate::Buffer) {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).free_buffer {
f(
buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0,
buffer.into_ptr(),
)
}
}
}
fn parent_release_buffer(&self, buffer_pool: &Self::Type, buffer: crate::Buffer) {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).release_buffer {
f(
buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0,
buffer.into_ptr(),
)
}
}
}
fn parent_reset_buffer(&self, buffer_pool: &Self::Type, buffer: &mut crate::BufferRef) {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).reset_buffer {
f(
buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0,
buffer.as_mut_ptr(),
)
}
}
}
fn parent_start(&self, buffer_pool: &Self::Type) -> bool {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).start {
let result = f(buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0);
from_glib(result)
} else {
true
}
}
}
fn parent_stop(&self, buffer_pool: &Self::Type) -> bool {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).stop {
let result = f(buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0);
from_glib(result)
} else {
true
}
}
}
fn parent_set_config(
&self,
buffer_pool: &Self::Type,
config: &mut BufferPoolConfigRef,
) -> bool {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).set_config {
let result = f(
buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0,
(*config).as_mut_ptr(),
);
from_glib(result)
} else {
false
}
}
}
fn parent_flush_start(&self, buffer_pool: &Self::Type) {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).flush_start {
f(buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0)
}
}
}
fn parent_flush_stop(&self, buffer_pool: &Self::Type) {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GstBufferPoolClass;
if let Some(f) = (*parent_class).flush_stop {
f(buffer_pool
.unsafe_cast_ref::<crate::BufferPool>()
.to_glib_none()
.0)
}
}
}
}
// Send+Sync wrapper around a NULL-terminated C string array
struct CStrV(*mut *const libc::c_char);
unsafe impl Send for CStrV {}
unsafe impl Sync for CStrV {}
impl Drop for CStrV {
fn drop(&mut self) {
unsafe { glib::ffi::g_strfreev(self.0 as *mut _) };
}
}
unsafe impl<T: BufferPoolImpl> IsSubclassable<T> for BufferPool {
fn class_init(klass: &mut glib::Class<Self>) {
Self::parent_class_init::<T>(klass);
let klass = klass.as_mut();
klass.acquire_buffer = Some(buffer_pool_acquire_buffer::<T>);
klass.alloc_buffer = Some(buffer_pool_alloc_buffer::<T>);
klass.release_buffer = Some(buffer_pool_release_buffer::<T>);
klass.reset_buffer = Some(buffer_pool_reset_buffer::<T>);
klass.start = Some(buffer_pool_start::<T>);
klass.stop = Some(buffer_pool_stop::<T>);
klass.get_options = Some(buffer_pool_get_options::<T>);
klass.set_config = Some(buffer_pool_set_config::<T>);
klass.flush_start = Some(buffer_pool_flush_start::<T>);
klass.flush_stop = Some(buffer_pool_flush_stop::<T>);
klass.free_buffer = Some(buffer_pool_free_buffer::<T>);
}
fn instance_init(instance: &mut InitializingObject<T>) {
Self::parent_instance_init(instance);
// Store the pool options in the instance data
// for later retrieval in buffer_pool_get_options
let options = T::options();
let options = options.to_glib_full();
instance.set_instance_data(T::type_(), CStrV(options));
}
}
unsafe extern "C" fn buffer_pool_acquire_buffer<T: BufferPoolImpl>(
ptr: *mut ffi::GstBufferPool,
buffer: *mut *mut ffi::GstBuffer,
params: *mut ffi::GstBufferPoolAcquireParams,
) -> ffi::GstFlowReturn {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
let params: Option<BufferPoolAcquireParams> = from_glib_none(params);
match imp.acquire_buffer(wrap.unsafe_cast_ref(), params.as_ref()) {
Ok(b) => {
*buffer = b.into_ptr();
ffi::GST_FLOW_OK
}
Err(err) => err.into_glib(),
}
}
unsafe extern "C" fn buffer_pool_alloc_buffer<T: BufferPoolImpl>(
ptr: *mut ffi::GstBufferPool,
buffer: *mut *mut ffi::GstBuffer,
params: *mut ffi::GstBufferPoolAcquireParams,
) -> ffi::GstFlowReturn {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
let params: Option<BufferPoolAcquireParams> = from_glib_none(params);
match imp.alloc_buffer(wrap.unsafe_cast_ref(), params.as_ref()) {
Ok(b) => {
*buffer = b.into_ptr();
ffi::GST_FLOW_OK
}
Err(err) => err.into_glib(),
}
}
unsafe extern "C" fn buffer_pool_flush_start<T: BufferPoolImpl>(ptr: *mut ffi::GstBufferPool) {
// the GstBufferPool implementation calls this
// in finalize where the ref_count will already
// be zero and we are actually destroyed
// see: https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1645
if (*(ptr as *const glib::gobject_ffi::GObject)).ref_count == 0 {
// flush_start is a no-op in GstBufferPool
return;
}
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
imp.flush_start(wrap.unsafe_cast_ref());
}
unsafe extern "C" fn buffer_pool_flush_stop<T: BufferPoolImpl>(ptr: *mut ffi::GstBufferPool) {
// the GstBufferPool implementation calls this
// in finalize where the ref_count will already
// be zero and we are actually destroyed
// see: https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1645
if (*(ptr as *const glib::gobject_ffi::GObject)).ref_count == 0 {
// flush_stop is a no-op in GstBufferPool
return;
}
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
imp.flush_stop(wrap.unsafe_cast_ref());
}
unsafe extern "C" fn buffer_pool_free_buffer<T: BufferPoolImpl>(
ptr: *mut ffi::GstBufferPool,
buffer: *mut ffi::GstBuffer,
) {
// the GstBufferPool implementation calls this
// in finalize where the ref_count will already
// be zero and we are actually destroyed
// see: https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1645
if (*(ptr as *const glib::gobject_ffi::GObject)).ref_count == 0 {
// As a workaround we call free_buffer directly on the
// GstBufferPool to prevent leaking the buffer
// This will NOT call free_buffer on a subclass.
let pool_class =
glib::Class::<crate::BufferPool>::from_type(crate::BufferPool::static_type()).unwrap();
let pool_class = pool_class.as_ref();
if let Some(f) = pool_class.free_buffer {
f(ptr, buffer)
}
return;
}
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
imp.free_buffer(wrap.unsafe_cast_ref(), from_glib_full(buffer));
}
unsafe extern "C" fn buffer_pool_release_buffer<T: BufferPoolImpl>(
ptr: *mut ffi::GstBufferPool,
buffer: *mut ffi::GstBuffer,
) {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
imp.release_buffer(wrap.unsafe_cast_ref(), from_glib_full(buffer));
}
unsafe extern "C" fn buffer_pool_reset_buffer<T: BufferPoolImpl>(
ptr: *mut ffi::GstBufferPool,
buffer: *mut ffi::GstBuffer,
) {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
imp.reset_buffer(
wrap.unsafe_cast_ref(),
crate::BufferRef::from_mut_ptr(buffer),
);
}
unsafe extern "C" fn buffer_pool_start<T: BufferPoolImpl>(
ptr: *mut ffi::GstBufferPool,
) -> glib::ffi::gboolean {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
imp.start(wrap.unsafe_cast_ref()).into_glib()
}
unsafe extern "C" fn buffer_pool_stop<T: BufferPoolImpl>(
ptr: *mut ffi::GstBufferPool,
) -> glib::ffi::gboolean {
// the GstBufferPool implementation calls this
// in finalize where the ref_count will already
// be zero and we are actually destroyed
// see: https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1645
if (*(ptr as *const glib::gobject_ffi::GObject)).ref_count == 0 {
// As a workaround we call stop directly on the GstBufferPool
// This is needed because the default implementation clears
// the pool in stop.
let pool_class =
glib::Class::<crate::BufferPool>::from_type(crate::BufferPool::static_type()).unwrap();
let pool_class = pool_class.as_ref();
let result = if let Some(f) = pool_class.stop {
f(ptr)
} else {
true.into_glib()
};
return result;
}
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
imp.stop(wrap.unsafe_cast_ref()).into_glib()
}
unsafe extern "C" fn buffer_pool_get_options<T: BufferPoolImpl>(
ptr: *mut ffi::GstBufferPool,
) -> *mut *const c_char {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
T::instance_data::<CStrV>(imp, T::type_())
.unwrap_or(&CStrV(std::ptr::null_mut()))
.0
}
unsafe extern "C" fn buffer_pool_set_config<T: BufferPoolImpl>(
ptr: *mut ffi::GstBufferPool,
config: *mut ffi::GstStructure,
) -> glib::ffi::gboolean {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let wrap: Borrowed<BufferPool> = from_glib_borrow(ptr);
imp.set_config(
wrap.unsafe_cast_ref(),
BufferPoolConfigRef::from_glib_borrow_mut(config),
)
.into_glib()
}
#[cfg(test)]
mod tests {
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use super::*;
use crate::prelude::*;
pub mod imp {
use super::*;
#[derive(Default)]
pub struct TestBufferPool;
impl ObjectImpl for TestBufferPool {}
impl GstObjectImpl for TestBufferPool {}
impl BufferPoolImpl for TestBufferPool {
fn options() -> &'static [&'static str] {
&["TEST_OPTION"]
}
fn set_config(
&self,
buffer_pool: &Self::Type,
config: &mut BufferPoolConfigRef,
) -> bool {
let (caps, size, min_buffers, max_buffers) = config.params().unwrap();
config.set_params(caps.as_ref(), size * 2, min_buffers, max_buffers);
self.parent_set_config(buffer_pool, config)
}
}
#[glib::object_subclass]
impl ObjectSubclass for TestBufferPool {
const NAME: &'static str = "TestBufferPool";
type Type = super::TestBufferPool;
type ParentType = BufferPool;
type Interfaces = ();
}
}
glib::wrapper! {
pub struct TestBufferPool(ObjectSubclass<imp::TestBufferPool>) @extends BufferPool, crate::Object;
}
impl Default for TestBufferPool {
fn default() -> Self {
glib::Object::new(&[]).unwrap()
}
}
#[test]
fn test_pool_options() {
crate::init().unwrap();
let pool = TestBufferPool::default();
assert_eq!(pool.options(), vec!["TEST_OPTION"]);
}
#[test]
fn test_pool_acquire() {
crate::init().unwrap();
let pool = TestBufferPool::default();
let mut config = pool.config();
config.set_params(None, 1024, 1, 1);
pool.set_config(config).expect("failed to set pool config");
pool.set_active(true).expect("failed to activate pool");
let buffer = pool
.acquire_buffer(None)
.expect("failed to acquire buffer from pool");
assert_eq!(buffer.size(), 2048);
}
#[test]
fn test_pool_free_on_finalize() {
crate::init().unwrap();
let pool = TestBufferPool::default();
let mut config = pool.config();
config.set_params(None, 1024, 1, 1);
pool.set_config(config).expect("failed to set pool config");
pool.set_active(true).expect("failed to activate pool");
let mut buffer = pool
.acquire_buffer(None)
.expect("failed to acquire buffer from pool");
let finalized = Arc::new(AtomicBool::new(false));
unsafe {
ffi::gst_mini_object_weak_ref(
buffer.make_mut().upcast_mut().as_mut_ptr(),
Some(buffer_finalized),
Arc::into_raw(finalized.clone()) as *mut _,
)
};
// return the buffer to the pool
std::mem::drop(buffer);
// drop should finalize the buffer pool which frees all allocated buffers
std::mem::drop(pool);
assert!(finalized.load(Ordering::SeqCst));
}
#[test]
fn test_pool_free_on_deactivate() {
crate::init().unwrap();
let pool = TestBufferPool::default();
let mut config = pool.config();
config.set_params(None, 1024, 1, 1);
pool.set_config(config).expect("failed to set pool config");
pool.set_active(true).expect("failed to activate pool");
let mut buffer = pool
.acquire_buffer(None)
.expect("failed to acquire buffer from pool");
let finalized = Arc::new(AtomicBool::new(false));
unsafe {
ffi::gst_mini_object_weak_ref(
buffer.make_mut().upcast_mut().as_mut_ptr(),
Some(buffer_finalized),
Arc::into_raw(finalized.clone()) as *mut _,
)
};
// return the buffer to the pool
std::mem::drop(buffer);
// de-activating a poll should free all buffers
pool.set_active(false).expect("failed to de-activate pool");
assert!(finalized.load(Ordering::SeqCst));
}
unsafe extern "C" fn buffer_finalized(
data: *mut libc::c_void,
_mini_object: *mut ffi::GstMiniObject,
) {
let finalized = &*(data as *const AtomicBool);
finalized.store(true, Ordering::SeqCst);
}
}

View file

@ -16,6 +16,7 @@ mod plugin;
mod plugin;
mod bin;
mod buffer_pool;
mod child_proxy;
mod element;
mod ghost_pad;
@ -46,6 +47,7 @@ pub mod prelude {
pub use glib::subclass::prelude::*;
pub use super::bin::{BinImpl, BinImplExt};
pub use super::buffer_pool::{BufferPoolImpl, BufferPoolImplExt};
pub use super::child_proxy::{ChildProxyImpl, ChildProxyImplExt};
pub use super::clock::{ClockImpl, ClockImplExt};
pub use super::device::{DeviceImpl, DeviceImplExt};