2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2017-07-28 17:04:15 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use std::{fmt, ptr};
|
2017-07-28 17:04:15 +00:00
|
|
|
|
2022-05-06 19:41:15 +00:00
|
|
|
use glib::translate::{from_glib_full, from_glib_none, mut_override, IntoGlibPtr, ToGlibPtr};
|
2017-07-28 17:04:15 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use crate::{
|
|
|
|
format::FormattedValueIntrinsic, Buffer, BufferList, BufferListRef, BufferRef, Caps, CapsRef,
|
|
|
|
FormattedSegment, Segment, Structure, StructureRef,
|
|
|
|
};
|
2020-11-21 13:46:48 +00:00
|
|
|
|
2020-12-20 15:09:22 +00:00
|
|
|
mini_object_wrapper!(Sample, SampleRef, ffi::GstSample, || {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_sample_get_type()
|
2018-09-28 15:11:46 +00:00
|
|
|
});
|
2017-07-28 17:04:15 +00:00
|
|
|
|
2019-10-22 20:32:39 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2021-12-03 23:17:29 +00:00
|
|
|
#[must_use = "The builder must be built to be used"]
|
2019-10-22 20:32:39 +00:00
|
|
|
pub struct SampleBuilder<'a> {
|
|
|
|
buffer: Option<&'a Buffer>,
|
|
|
|
buffer_list: Option<&'a BufferList>,
|
|
|
|
caps: Option<&'a Caps>,
|
|
|
|
segment: Option<&'a Segment>,
|
|
|
|
info: Option<Structure>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SampleBuilder<'a> {
|
|
|
|
pub fn buffer(self, buffer: &'a Buffer) -> Self {
|
|
|
|
Self {
|
|
|
|
buffer: Some(buffer),
|
|
|
|
buffer_list: None,
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn buffer_list(self, buffer_list: &'a BufferList) -> Self {
|
|
|
|
Self {
|
|
|
|
buffer: None,
|
|
|
|
buffer_list: Some(buffer_list),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn caps(self, caps: &'a Caps) -> Self {
|
|
|
|
Self {
|
|
|
|
caps: Some(caps),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:27:16 +00:00
|
|
|
pub fn segment<F: FormattedValueIntrinsic>(self, segment: &'a FormattedSegment<F>) -> Self {
|
2019-10-22 20:32:39 +00:00
|
|
|
Self {
|
|
|
|
segment: Some(segment.upcast_ref()),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn info(self, info: Structure) -> Self {
|
|
|
|
Self {
|
|
|
|
info: Some(info),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 23:17:29 +00:00
|
|
|
#[must_use = "Building the sample without using it has no effect"]
|
2019-10-22 20:32:39 +00:00
|
|
|
pub fn build(self) -> Sample {
|
2017-07-28 17:04:15 +00:00
|
|
|
unsafe {
|
2022-05-06 19:41:15 +00:00
|
|
|
let info = self
|
|
|
|
.info
|
|
|
|
.map(|i| i.into_glib_ptr())
|
|
|
|
.unwrap_or(ptr::null_mut());
|
2017-07-28 17:04:15 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let sample: Sample = from_glib_full(ffi::gst_sample_new(
|
2019-10-22 20:32:39 +00:00
|
|
|
self.buffer.to_glib_none().0,
|
|
|
|
self.caps.to_glib_none().0,
|
|
|
|
self.segment.to_glib_none().0,
|
2017-07-31 11:16:42 +00:00
|
|
|
mut_override(info),
|
2019-10-22 20:32:39 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if let Some(buffer_list) = self.buffer_list {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_sample_set_buffer_list(
|
2019-10-22 20:32:39 +00:00
|
|
|
sample.to_glib_none().0,
|
|
|
|
buffer_list.to_glib_none().0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sample
|
2017-07-28 17:04:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-22 20:32:39 +00:00
|
|
|
}
|
2017-12-30 10:02:48 +00:00
|
|
|
|
2019-10-22 20:32:39 +00:00
|
|
|
impl Sample {
|
2020-06-25 16:22:25 +00:00
|
|
|
pub fn builder<'a>() -> SampleBuilder<'a> {
|
2022-12-25 10:47:02 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2019-10-22 20:32:39 +00:00
|
|
|
SampleBuilder {
|
|
|
|
buffer: None,
|
|
|
|
buffer_list: None,
|
|
|
|
caps: None,
|
|
|
|
segment: None,
|
|
|
|
info: None,
|
2017-12-30 10:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 17:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SampleRef {
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_buffer")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_get_buffer")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn buffer(&self) -> Option<&BufferRef> {
|
2019-04-23 16:53:10 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let ptr = ffi::gst_sample_get_buffer(self.as_mut_ptr());
|
2019-04-23 16:53:10 +00:00
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(BufferRef::from_ptr(ptr))
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 17:04:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_buffer_owned")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn buffer_owned(&self) -> Option<Buffer> {
|
|
|
|
unsafe { self.buffer().map(|buffer| from_glib_none(buffer.as_ptr())) }
|
2019-05-23 11:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_buffer_list")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_get_buffer_list")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn buffer_list(&self) -> Option<&BufferListRef> {
|
2019-04-23 16:53:10 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let ptr = ffi::gst_sample_get_buffer_list(self.as_mut_ptr());
|
2019-04-23 16:53:10 +00:00
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(BufferListRef::from_ptr(ptr))
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 07:15:20 +00:00
|
|
|
}
|
2017-07-28 17:04:15 +00:00
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_buffer_list_owned")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn buffer_list_owned(&self) -> Option<BufferList> {
|
|
|
|
unsafe { self.buffer_list().map(|list| from_glib_none(list.as_ptr())) }
|
2019-05-23 11:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_caps")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_get_caps")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn caps(&self) -> Option<&CapsRef> {
|
2019-04-23 16:53:10 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let ptr = ffi::gst_sample_get_caps(self.as_mut_ptr());
|
2019-04-23 16:53:10 +00:00
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(CapsRef::from_ptr(ptr))
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 17:04:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_caps_owned")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn caps_owned(&self) -> Option<Caps> {
|
|
|
|
unsafe { self.caps().map(|caps| from_glib_none(caps.as_ptr())) }
|
2019-05-23 11:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_segment")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_get_segment")]
|
2021-07-12 11:22:09 +00:00
|
|
|
pub fn segment(&self) -> Option<&Segment> {
|
|
|
|
unsafe {
|
|
|
|
let ptr = ffi::gst_sample_get_segment(self.as_mut_ptr());
|
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
2023-01-03 09:10:08 +00:00
|
|
|
Some(crate::Segment::from_glib_ptr_borrow(ptr))
|
2021-07-12 11:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 17:04:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_info")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_get_info")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn info(&self) -> Option<&StructureRef> {
|
2017-07-28 17:04:15 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let ptr = ffi::gst_sample_get_info(self.as_mut_ptr());
|
2017-07-28 17:04:15 +00:00
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(StructureRef::from_glib_borrow(ptr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 16:53:10 +00:00
|
|
|
|
2023-05-04 05:55:48 +00:00
|
|
|
#[cfg(feature = "v1_16")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_set_buffer")]
|
2019-05-23 11:28:09 +00:00
|
|
|
pub fn set_buffer(&mut self, buffer: Option<&Buffer>) {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { ffi::gst_sample_set_buffer(self.as_mut_ptr(), buffer.to_glib_none().0) }
|
2019-04-23 16:53:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 05:55:48 +00:00
|
|
|
#[cfg(feature = "v1_16")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_set_buffer_list")]
|
2019-05-23 11:28:09 +00:00
|
|
|
pub fn set_buffer_list(&mut self, buffer_list: Option<&BufferList>) {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { ffi::gst_sample_set_buffer_list(self.as_mut_ptr(), buffer_list.to_glib_none().0) }
|
2019-04-23 16:53:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 05:55:48 +00:00
|
|
|
#[cfg(feature = "v1_16")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_set_caps")]
|
2019-05-23 11:28:09 +00:00
|
|
|
pub fn set_caps(&mut self, caps: Option<&Caps>) {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { ffi::gst_sample_set_caps(self.as_mut_ptr(), caps.to_glib_none().0) }
|
2019-04-23 16:53:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 05:55:48 +00:00
|
|
|
#[cfg(feature = "v1_16")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_set_segment")]
|
2019-05-22 20:27:13 +00:00
|
|
|
pub fn set_segment(&mut self, segment: Option<&Segment>) {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { ffi::gst_sample_set_segment(self.as_mut_ptr(), segment.to_glib_none().0) }
|
2019-04-23 16:53:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 05:55:48 +00:00
|
|
|
#[cfg(feature = "v1_16")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_sample_set_info")]
|
2019-04-23 16:53:10 +00:00
|
|
|
pub fn set_info(&mut self, info: Option<Structure>) {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_sample_set_info(
|
2019-04-23 16:53:10 +00:00
|
|
|
self.as_mut_ptr(),
|
2022-05-06 19:41:15 +00:00
|
|
|
info.map(|i| i.into_glib_ptr()).unwrap_or(ptr::null_mut()),
|
2019-04-23 16:53:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 17:04:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 21:12:46 +00:00
|
|
|
impl fmt::Debug for Sample {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
SampleRef::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-30 10:02:33 +00:00
|
|
|
impl fmt::Debug for SampleRef {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("Sample")
|
2021-04-11 19:39:50 +00:00
|
|
|
.field("buffer", &self.buffer())
|
|
|
|
.field("caps", &self.caps())
|
|
|
|
.field("segment", &self.segment())
|
|
|
|
.field("info", &self.info())
|
2017-12-30 10:02:33 +00:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 20:21:51 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn test_sample_new_with_info() {
|
2023-01-03 18:58:25 +00:00
|
|
|
use crate::{Sample, Structure};
|
2018-06-11 20:21:51 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-06-11 20:21:51 +00:00
|
|
|
|
|
|
|
let info = Structure::builder("sample.info")
|
2021-10-24 16:11:30 +00:00
|
|
|
.field("f3", 123i32)
|
2018-06-11 20:21:51 +00:00
|
|
|
.build();
|
2020-06-25 16:22:25 +00:00
|
|
|
let sample = Sample::builder().info(info).build();
|
2018-06-11 20:21:51 +00:00
|
|
|
|
2021-04-11 19:39:50 +00:00
|
|
|
assert!(sample.info().is_some());
|
2018-06-11 20:21:51 +00:00
|
|
|
}
|
|
|
|
}
|