gstreamer-rs/gstreamer/src/buffer_pool.rs

352 lines
9.6 KiB
Rust
Raw Normal View History

2018-03-07 09:07:30 +00:00
// Copyright (C) 2018 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use BufferPool;
use Structure;
use glib;
use glib::translate::{from_glib, from_glib_full, from_glib_none, ToGlib, ToGlibPtr, ToGlibPtrMut};
2018-04-25 08:10:06 +00:00
use glib::IsA;
2018-03-07 09:07:30 +00:00
2019-03-19 07:58:20 +00:00
use gst_sys;
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, PartialEq, Eq)]
pub struct BufferPoolConfig(Structure);
impl ops::Deref for BufferPoolConfig {
type Target = ::StructureRef;
fn deref(&self) -> &::StructureRef {
self.0.deref()
}
}
impl ops::DerefMut for BufferPoolConfig {
fn deref_mut(&mut self) -> &mut ::StructureRef {
self.0.deref_mut()
}
}
impl AsRef<::StructureRef> for BufferPoolConfig {
fn as_ref(&self) -> &::StructureRef {
self.0.as_ref()
}
}
impl AsMut<::StructureRef> for BufferPoolConfig {
fn as_mut(&mut self) -> &mut ::StructureRef {
self.0.as_mut()
}
}
impl BufferPoolConfig {
pub fn add_option(&mut self, option: &str) {
unsafe {
2019-03-19 07:58:20 +00:00
gst_sys::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 {
2019-03-19 07:58:20 +00:00
from_glib(gst_sys::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,
))
}
}
pub fn get_options(&self) -> Vec<String> {
unsafe {
2019-03-19 07:58:20 +00:00
let n = gst_sys::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 {
2019-03-19 07:58:20 +00:00
options.push(from_glib_none(gst_sys::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<'a, T: Into<Option<&'a ::Caps>>>(
&mut self,
caps: T,
size: u32,
min_buffers: u32,
max_buffers: u32,
) {
let caps = caps.into();
unsafe {
2019-03-19 07:58:20 +00:00
gst_sys::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,
);
}
}
pub fn get_params(&self) -> Option<(Option<::Caps>, u32, u32, u32)> {
unsafe {
let mut caps = ptr::null_mut();
let mut size = mem::uninitialized();
let mut min_buffers = mem::uninitialized();
let mut max_buffers = mem::uninitialized();
2019-03-19 07:58:20 +00:00
let ret: bool = from_glib(gst_sys::gst_buffer_pool_config_get_params(
2018-03-07 09:07:30 +00:00
self.0.to_glib_none().0,
&mut caps,
&mut size,
&mut min_buffers,
&mut max_buffers,
));
if !ret {
return None;
}
Some((from_glib_none(caps), size, min_buffers, max_buffers))
}
}
pub fn validate_params<'a, T: Into<Option<&'a ::Caps>>>(
&self,
caps: T,
size: u32,
min_buffers: u32,
max_buffers: u32,
) -> Result<(), glib::BoolError> {
2018-03-07 09:07:30 +00:00
let caps = caps.into();
unsafe {
glib_result_from_gboolean!(
2019-03-19 07:58:20 +00:00
gst_sys::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
}
}
// TODO: get_allocator / set_allocator
// TODO: options iterator
}
#[derive(Debug)]
2019-03-19 07:58:20 +00:00
pub struct BufferPoolAcquireParams(gst_sys::GstBufferPoolAcquireParams);
2018-03-07 09:07:30 +00:00
impl BufferPoolAcquireParams {
pub fn with_flags(flags: ::BufferPoolAcquireFlags) -> Self {
2019-03-19 07:58:20 +00:00
BufferPoolAcquireParams(gst_sys::GstBufferPoolAcquireParams {
format: gst_sys::GST_FORMAT_UNDEFINED,
2018-03-07 09:07:30 +00:00
start: -1,
stop: -1,
flags: flags.to_glib(),
_gst_reserved: [ptr::null_mut(); 4],
})
}
pub fn with_start_stop<T: ::SpecificFormattedValue>(
start: T,
stop: T,
flags: ::BufferPoolAcquireFlags,
) -> Self {
unsafe {
2019-03-19 07:58:20 +00:00
BufferPoolAcquireParams(gst_sys::GstBufferPoolAcquireParams {
2018-03-07 09:07:30 +00:00
format: start.get_format().to_glib(),
start: start.to_raw_value(),
stop: stop.to_raw_value(),
flags: flags.to_glib(),
_gst_reserved: [ptr::null_mut(); 4],
})
}
}
pub fn flags(&self) -> ::BufferPoolAcquireFlags {
from_glib(self.0.flags)
}
pub fn format(&self) -> ::Format {
from_glib(self.0.format)
}
pub fn start(&self) -> ::GenericFormattedValue {
::GenericFormattedValue::new(from_glib(self.0.format), self.0.start)
}
pub fn stop(&self) -> ::GenericFormattedValue {
::GenericFormattedValue::new(from_glib(self.0.format), self.0.stop)
}
}
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 gst_sys::GstBufferPoolAcquireParams> for BufferPoolAcquireParams {
type Storage = &'a Self;
fn to_glib_none(
&'a self,
) -> glib::translate::Stash<'a, *const gst_sys::GstBufferPoolAcquireParams, Self> {
glib::translate::Stash(&self.0, self)
}
}
#[doc(hidden)]
impl<'a> ToGlibPtrMut<'a, *mut gst_sys::GstBufferPoolAcquireParams> for BufferPoolAcquireParams {
type Storage = &'a mut Self;
fn to_glib_none_mut(
&'a mut self,
) -> glib::translate::StashMut<'a, *mut gst_sys::GstBufferPoolAcquireParams, Self> {
glib::translate::StashMut(&mut self.0, self)
}
}
impl BufferPool {
pub fn new() -> BufferPool {
assert_initialized_main_thread!();
let (major, minor, _, _) = ::version();
if (major, minor) > (1, 12) {
2019-03-19 07:58:20 +00:00
unsafe { from_glib_full(gst_sys::gst_buffer_pool_new()) }
} else {
// Work-around for 1.14 switching from transfer-floating to transfer-full
2019-03-19 07:58:20 +00:00
unsafe { from_glib_none(gst_sys::gst_buffer_pool_new()) }
}
}
}
impl Default for BufferPool {
fn default() -> Self {
Self::new()
}
}
pub trait BufferPoolExtManual: 'static {
2018-03-07 09:07:30 +00:00
fn get_config(&self) -> BufferPoolConfig;
fn set_config(&self, config: BufferPoolConfig) -> Result<(), glib::error::BoolError>;
fn is_flushing(&self) -> bool;
fn acquire_buffer<'a, P: Into<Option<&'a BufferPoolAcquireParams>>>(
&self,
params: P,
) -> Result<::Buffer, ::FlowError>;
2018-03-07 09:07:30 +00:00
fn release_buffer(&self, buffer: ::Buffer);
}
impl<O: IsA<BufferPool>> BufferPoolExtManual for O {
fn get_config(&self) -> BufferPoolConfig {
unsafe {
2019-03-19 07:58:20 +00:00
let ptr = gst_sys::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!(
2019-03-19 07:58:20 +00:00
gst_sys::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();
2019-03-19 07:58:20 +00:00
let ptr: *mut gst_sys::GstBufferPool = stash.0;
2018-03-07 09:07:30 +00:00
from_glib((*ptr).flushing)
}
}
fn acquire_buffer<'a, P: Into<Option<&'a BufferPoolAcquireParams>>>(
&self,
params: P,
) -> Result<::Buffer, ::FlowError> {
let params_ptr = params.into().to_glib_none().0 as *mut _;
2018-03-07 09:07:30 +00:00
unsafe {
let mut buffer = ptr::null_mut();
2019-03-19 07:58:20 +00:00
let ret: ::FlowReturn = from_glib(gst_sys::gst_buffer_pool_acquire_buffer(
self.as_ref().to_glib_none().0,
2018-03-07 09:07:30 +00:00
&mut buffer,
params_ptr,
));
ret.into_result_value(|| from_glib_full(buffer))
2018-03-07 09:07:30 +00:00
}
}
fn release_buffer(&self, buffer: ::Buffer) {
unsafe {
2019-03-19 07:58:20 +00:00
gst_sys::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 prelude::*;
#[test]
fn test_pool() {
::init().unwrap();
let pool = ::BufferPool::new();
let mut config = pool.get_config();
config.set_params(Some(&::Caps::new_simple("foo/bar", &[])), 1024, 0, 2);
pool.set_config(config).unwrap();
pool.set_active(true).unwrap();
let params = ::BufferPoolAcquireParams::with_flags(::BufferPoolAcquireFlags::DONTWAIT);
let _buf1 = pool.acquire_buffer(&params).unwrap();
let buf2 = pool.acquire_buffer(&params).unwrap();
assert!(pool.acquire_buffer(&params).is_err());
drop(buf2);
let _buf2 = pool.acquire_buffer(&params).unwrap();
pool.set_active(false).unwrap();
}
}