1use std::{
4 marker::PhantomData,
5 mem, ops,
6 ops::{Deref, DerefMut},
7 ptr,
8};
9
10use glib::{prelude::*, translate::*};
11
12use crate::{ffi, AllocationParams, Allocator, BufferPool, Structure, StructureRef};
13
14#[derive(Debug, Clone, PartialEq, Eq)]
15#[repr(transparent)]
16pub struct BufferPoolConfig(Structure);
17
18impl Deref for BufferPoolConfig {
19 type Target = BufferPoolConfigRef;
20
21 #[inline]
22 fn deref(&self) -> &BufferPoolConfigRef {
23 unsafe { &*(self.0.as_ptr() as *const StructureRef as *const BufferPoolConfigRef) }
24 }
25}
26
27impl DerefMut for BufferPoolConfig {
28 #[inline]
29 fn deref_mut(&mut self) -> &mut BufferPoolConfigRef {
30 unsafe { &mut *(self.0.as_ptr() as *mut StructureRef as *mut BufferPoolConfigRef) }
31 }
32}
33
34impl AsRef<BufferPoolConfigRef> for BufferPoolConfig {
35 #[inline]
36 fn as_ref(&self) -> &BufferPoolConfigRef {
37 self.deref()
38 }
39}
40
41impl AsMut<BufferPoolConfigRef> for BufferPoolConfig {
42 #[inline]
43 fn as_mut(&mut self) -> &mut BufferPoolConfigRef {
44 self.deref_mut()
45 }
46}
47
48#[derive(Debug)]
49#[repr(transparent)]
50pub struct BufferPoolConfigRef(StructureRef);
51
52impl BufferPoolConfigRef {
53 #[inline]
54 pub unsafe fn from_glib_borrow<'a>(ptr: *const ffi::GstStructure) -> &'a BufferPoolConfigRef {
55 debug_assert!(!ptr.is_null());
56
57 &*(ptr as *mut StructureRef as *mut BufferPoolConfigRef)
58 }
59
60 #[inline]
61 pub unsafe fn from_glib_borrow_mut<'a>(
62 ptr: *mut ffi::GstStructure,
63 ) -> &'a mut BufferPoolConfigRef {
64 debug_assert!(!ptr.is_null());
65
66 &mut *(ptr as *mut StructureRef as *mut BufferPoolConfigRef)
67 }
68
69 #[inline]
70 pub fn as_ptr(&self) -> *const ffi::GstStructure {
71 self as *const Self as *const ffi::GstStructure
72 }
73
74 #[inline]
75 pub fn as_mut_ptr(&self) -> *mut ffi::GstStructure {
76 self as *const Self as *mut ffi::GstStructure
77 }
78}
79
80impl ops::Deref for BufferPoolConfigRef {
81 type Target = crate::StructureRef;
82
83 #[inline]
84 fn deref(&self) -> &crate::StructureRef {
85 &self.0
86 }
87}
88
89impl ops::DerefMut for BufferPoolConfigRef {
90 #[inline]
91 fn deref_mut(&mut self) -> &mut crate::StructureRef {
92 &mut self.0
93 }
94}
95
96impl AsRef<crate::StructureRef> for BufferPoolConfigRef {
97 #[inline]
98 fn as_ref(&self) -> &crate::StructureRef {
99 &self.0
100 }
101}
102
103impl AsMut<crate::StructureRef> for BufferPoolConfigRef {
104 #[inline]
105 fn as_mut(&mut self) -> &mut crate::StructureRef {
106 &mut self.0
107 }
108}
109
110impl BufferPoolConfigRef {
111 #[doc(alias = "gst_buffer_pool_config_add_option")]
112 pub fn add_option(&mut self, option: &str) {
113 unsafe {
114 ffi::gst_buffer_pool_config_add_option(self.0.as_mut_ptr(), option.to_glib_none().0);
115 }
116 }
117
118 #[doc(alias = "gst_buffer_pool_config_has_option")]
119 pub fn has_option(&self, option: &str) -> bool {
120 unsafe {
121 from_glib(ffi::gst_buffer_pool_config_has_option(
122 self.0.as_mut_ptr(),
123 option.to_glib_none().0,
124 ))
125 }
126 }
127
128 #[doc(alias = "get_options")]
129 #[doc(alias = "gst_buffer_pool_config_n_options")]
130 #[doc(alias = "gst_buffer_pool_config_get_option")]
131 pub fn options(&self) -> Vec<String> {
132 unsafe {
133 let n = ffi::gst_buffer_pool_config_n_options(self.0.as_mut_ptr()) as usize;
134 let mut options = Vec::with_capacity(n);
135
136 for i in 0..n {
137 options.push(from_glib_none(ffi::gst_buffer_pool_config_get_option(
138 self.0.as_mut_ptr(),
139 i as u32,
140 )));
141 }
142
143 options
144 }
145 }
146
147 #[doc(alias = "gst_buffer_pool_config_set_params")]
148 pub fn set_params(
149 &mut self,
150 caps: Option<&crate::Caps>,
151 size: u32,
152 min_buffers: u32,
153 max_buffers: u32,
154 ) {
155 unsafe {
156 ffi::gst_buffer_pool_config_set_params(
157 self.0.as_mut_ptr(),
158 caps.to_glib_none().0,
159 size,
160 min_buffers,
161 max_buffers,
162 );
163 }
164 }
165
166 #[doc(alias = "get_params")]
167 #[doc(alias = "gst_buffer_pool_config_get_params")]
168 pub fn params(&self) -> Option<(Option<crate::Caps>, u32, u32, u32)> {
169 unsafe {
170 let mut caps = ptr::null_mut();
171 let mut size = mem::MaybeUninit::uninit();
172 let mut min_buffers = mem::MaybeUninit::uninit();
173 let mut max_buffers = mem::MaybeUninit::uninit();
174
175 let ret: bool = from_glib(ffi::gst_buffer_pool_config_get_params(
176 self.0.as_mut_ptr(),
177 &mut caps,
178 size.as_mut_ptr(),
179 min_buffers.as_mut_ptr(),
180 max_buffers.as_mut_ptr(),
181 ));
182 if !ret {
183 return None;
184 }
185
186 Some((
187 from_glib_none(caps),
188 size.assume_init(),
189 min_buffers.assume_init(),
190 max_buffers.assume_init(),
191 ))
192 }
193 }
194
195 #[doc(alias = "gst_buffer_pool_config_validate_params")]
196 pub fn validate_params(
197 &self,
198 caps: Option<&crate::Caps>,
199 size: u32,
200 min_buffers: u32,
201 max_buffers: u32,
202 ) -> Result<(), glib::BoolError> {
203 unsafe {
204 glib::result_from_gboolean!(
205 ffi::gst_buffer_pool_config_validate_params(
206 self.0.as_mut_ptr(),
207 caps.to_glib_none().0,
208 size,
209 min_buffers,
210 max_buffers,
211 ),
212 "Parameters are not valid in this context"
213 )
214 }
215 }
216
217 #[doc(alias = "get_allocator")]
218 #[doc(alias = "gst_buffer_pool_config_get_allocator")]
219 pub fn allocator(&self) -> Option<(Option<Allocator>, AllocationParams)> {
220 unsafe {
221 let mut allocator = ptr::null_mut();
222 let mut params = mem::MaybeUninit::uninit();
223 let ret = from_glib(ffi::gst_buffer_pool_config_get_allocator(
224 self.0.as_mut_ptr(),
225 &mut allocator,
226 params.as_mut_ptr(),
227 ));
228 if ret {
229 Some((from_glib_none(allocator), params.assume_init().into()))
230 } else {
231 None
232 }
233 }
234 }
235
236 #[doc(alias = "gst_buffer_pool_config_set_allocator")]
237 pub fn set_allocator(&self, allocator: Option<&Allocator>, params: Option<&AllocationParams>) {
238 assert!(allocator.is_some() || params.is_some());
239 unsafe {
240 ffi::gst_buffer_pool_config_set_allocator(
241 self.0.as_mut_ptr(),
242 allocator.to_glib_none().0,
243 match params {
244 Some(val) => val.as_ptr(),
245 None => ptr::null(),
246 },
247 )
248 }
249 }
250 }
252
253#[derive(Debug)]
254#[doc(alias = "GstBufferPoolAcquireParams")]
255pub struct BufferPoolAcquireParams(ffi::GstBufferPoolAcquireParams);
256
257unsafe impl Send for BufferPoolAcquireParams {}
258unsafe impl Sync for BufferPoolAcquireParams {}
259
260impl BufferPoolAcquireParams {
261 pub fn with_flags(flags: crate::BufferPoolAcquireFlags) -> Self {
262 skip_assert_initialized!();
263 BufferPoolAcquireParams(ffi::GstBufferPoolAcquireParams {
264 format: ffi::GST_FORMAT_UNDEFINED,
265 start: -1,
266 stop: -1,
267 flags: flags.into_glib(),
268 _gst_reserved: [ptr::null_mut(); 4],
269 })
270 }
271
272 pub fn with_start_stop<T: crate::format::SpecificFormattedValue>(
273 start: T,
274 stop: T,
275 flags: crate::BufferPoolAcquireFlags,
276 ) -> Self {
277 skip_assert_initialized!();
278 unsafe {
279 BufferPoolAcquireParams(ffi::GstBufferPoolAcquireParams {
280 format: start.format().into_glib(),
281 start: start.into_raw_value(),
282 stop: stop.into_raw_value(),
283 flags: flags.into_glib(),
284 _gst_reserved: [ptr::null_mut(); 4],
285 })
286 }
287 }
288
289 pub fn flags(&self) -> crate::BufferPoolAcquireFlags {
290 unsafe { from_glib(self.0.flags) }
291 }
292
293 pub fn format(&self) -> crate::Format {
294 unsafe { from_glib(self.0.format) }
295 }
296
297 pub fn start(&self) -> crate::GenericFormattedValue {
298 unsafe { crate::GenericFormattedValue::new(from_glib(self.0.format), self.0.start) }
299 }
300
301 pub fn stop(&self) -> crate::GenericFormattedValue {
302 unsafe { crate::GenericFormattedValue::new(from_glib(self.0.format), self.0.stop) }
303 }
304}
305
306impl PartialEq for BufferPoolAcquireParams {
307 fn eq(&self, other: &Self) -> bool {
308 self.format() == other.format()
309 && self.start() == other.start()
310 && self.stop() == other.stop()
311 }
312}
313
314impl Eq for BufferPoolAcquireParams {}
315
316#[doc(hidden)]
317impl<'a> ToGlibPtr<'a, *const ffi::GstBufferPoolAcquireParams> for BufferPoolAcquireParams {
318 type Storage = PhantomData<&'a Self>;
319
320 #[inline]
321 fn to_glib_none(
322 &'a self,
323 ) -> glib::translate::Stash<'a, *const ffi::GstBufferPoolAcquireParams, Self> {
324 glib::translate::Stash(&self.0, PhantomData)
325 }
326}
327
328#[doc(hidden)]
329impl<'a> ToGlibPtrMut<'a, *mut ffi::GstBufferPoolAcquireParams> for BufferPoolAcquireParams {
330 type Storage = PhantomData<&'a mut Self>;
331
332 #[inline]
333 fn to_glib_none_mut(
334 &'a mut self,
335 ) -> glib::translate::StashMut<'a, *mut ffi::GstBufferPoolAcquireParams, Self> {
336 glib::translate::StashMut(&mut self.0, PhantomData)
337 }
338}
339
340#[doc(hidden)]
341impl FromGlibPtrNone<*mut ffi::GstBufferPoolAcquireParams> for BufferPoolAcquireParams {
342 #[inline]
343 unsafe fn from_glib_none(ptr: *mut ffi::GstBufferPoolAcquireParams) -> Self {
344 Self(*ptr)
345 }
346}
347
348mod sealed {
349 pub trait Sealed {}
350 impl<T: super::IsA<super::BufferPool>> Sealed for T {}
351}
352
353pub trait BufferPoolExtManual: sealed::Sealed + IsA<BufferPool> + 'static {
354 #[doc(alias = "get_config")]
361 #[doc(alias = "gst_buffer_pool_get_config")]
362 fn config(&self) -> BufferPoolConfig {
363 unsafe {
364 let ptr = ffi::gst_buffer_pool_get_config(self.as_ref().to_glib_none().0);
365 BufferPoolConfig(from_glib_full(ptr))
366 }
367 }
368
369 #[doc(alias = "gst_buffer_pool_set_config")]
392 fn set_config(&self, config: BufferPoolConfig) -> Result<(), glib::error::BoolError> {
393 unsafe {
394 glib::result_from_gboolean!(
395 ffi::gst_buffer_pool_set_config(
396 self.as_ref().to_glib_none().0,
397 config.0.into_glib_ptr()
398 ),
399 "Failed to set config",
400 )
401 }
402 }
403
404 fn is_flushing(&self) -> bool {
405 unsafe {
406 let stash = self.as_ref().to_glib_none();
407 let ptr: *mut ffi::GstBufferPool = stash.0;
408
409 from_glib((*ptr).flushing)
410 }
411 }
412
413 #[doc(alias = "gst_buffer_pool_acquire_buffer")]
430 fn acquire_buffer(
431 &self,
432 params: Option<&BufferPoolAcquireParams>,
433 ) -> Result<crate::Buffer, crate::FlowError> {
434 let params_ptr = params.to_glib_none().0 as *mut _;
435
436 unsafe {
437 let mut buffer = ptr::null_mut();
438 crate::FlowSuccess::try_from_glib(ffi::gst_buffer_pool_acquire_buffer(
439 self.as_ref().to_glib_none().0,
440 &mut buffer,
441 params_ptr,
442 ))
443 .map(|_| from_glib_full(buffer))
444 }
445 }
446}
447
448impl<O: IsA<BufferPool>> BufferPoolExtManual for O {}
449
450#[cfg(test)]
451mod tests {
452 use super::*;
453 use crate::prelude::*;
454
455 #[test]
456 fn pool_with_params() {
457 crate::init().unwrap();
458
459 let pool = crate::BufferPool::new();
460 let mut config = pool.config();
461 config.set_params(Some(&crate::Caps::builder("foo/bar").build()), 1024, 0, 2);
462 pool.set_config(config).unwrap();
463
464 pool.set_active(true).unwrap();
465
466 let params =
467 crate::BufferPoolAcquireParams::with_flags(crate::BufferPoolAcquireFlags::DONTWAIT);
468
469 let _buf1 = pool.acquire_buffer(Some(¶ms)).unwrap();
470 let buf2 = pool.acquire_buffer(Some(¶ms)).unwrap();
471
472 assert!(pool.acquire_buffer(Some(¶ms)).is_err());
473
474 drop(buf2);
475 let _buf2 = pool.acquire_buffer(Some(¶ms)).unwrap();
476
477 pool.set_active(false).unwrap();
478 }
479
480 #[test]
481 fn pool_no_params() {
482 crate::init().unwrap();
483
484 let pool = crate::BufferPool::new();
485 let mut config = pool.config();
486 config.set_params(None, 1024, 0, 2);
487 pool.set_config(config).unwrap();
488
489 pool.set_active(true).unwrap();
490 let _buf1 = pool.acquire_buffer(None).unwrap();
491 pool.set_active(false).unwrap();
492 }
493}