diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index 2e72b5d66..08b82d4ec 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -240,6 +240,12 @@ impl BufferRef { } } + pub fn copy_deep(&self) -> Buffer { + unsafe { + from_glib_full(ffi::gst_buffer_copy_deep(self.as_ptr())) + } + } + pub fn get_size(&self) -> usize { unsafe { ffi::gst_buffer_get_size(self.as_mut_ptr()) } } diff --git a/gstreamer/src/bufferlist.rs b/gstreamer/src/bufferlist.rs new file mode 100644 index 000000000..d3a2c00e8 --- /dev/null +++ b/gstreamer/src/bufferlist.rs @@ -0,0 +1,157 @@ +// Copyright (C) 2017 Sebastian Dröge +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use ffi; +use glib; +use glib::StaticType; +use glib::translate::{from_glib, from_glib_none, from_glib_full}; + +use miniobject::*; +use Buffer; +use BufferRef; + +pub type BufferList = GstRc; +pub struct BufferListRef(ffi::GstBufferList); + +unsafe impl MiniObject for BufferListRef { + type GstType = ffi::GstBufferList; +} + +impl GstRc { + pub fn new() -> Self { + assert_initialized_main_thread!(); + unsafe { from_glib_full(ffi::gst_buffer_list_new()) } + } + + pub fn new_sized(size: usize) -> Self { + assert_initialized_main_thread!(); + unsafe { from_glib_full(ffi::gst_buffer_list_new_sized(size as u32)) } + } +} + +impl BufferListRef { + pub fn insert(&mut self, idx: i32, buffer: Buffer) { + unsafe { + ffi::gst_buffer_list_insert(self.as_mut_ptr(), idx, buffer.into_ptr()); + } + } + + pub fn add(&mut self, buffer: Buffer) { + self.insert(-1, buffer); + } + + pub fn copy_deep(&self) -> BufferList { + unsafe { + from_glib_full(ffi::gst_buffer_list_copy_deep(self.as_ptr())) + } + } + + pub fn remove(&mut self, idx: u32, len: u32) { + unsafe { + ffi::gst_buffer_list_remove(self.as_mut_ptr(), idx, len) + } + } + + pub fn get(&self, idx: u32) -> Option<&BufferRef> { + unsafe { + let ptr = ffi::gst_buffer_list_get(self.as_mut_ptr(), idx); + if ptr.is_null() { + None + } else { + Some(BufferRef::from_ptr(ptr)) + } + } + } + + pub fn len(&self) -> usize { + unsafe { + ffi::gst_buffer_list_length(self.as_mut_ptr()) as usize + } + } + + pub fn iter<'a>(&'a self) -> Iter<'a> { + Iter::new(self) + } +} + +impl ToOwned for BufferListRef { + type Owned = GstRc; + + fn to_owned(&self) -> GstRc { + unsafe { from_glib_none(self.as_ptr()) } + } +} + +impl StaticType for BufferListRef { + fn static_type() -> glib::Type { + unsafe { + from_glib(ffi::gst_buffer_list_get_type()) + } + } +} + +unsafe impl Sync for BufferListRef {} +unsafe impl Send for BufferListRef {} + +pub struct Iter<'a> { + list: &'a BufferListRef, + idx: u32, + size: u32, +} + +impl<'a> Iter<'a> { + fn new(list: &'a BufferListRef) -> Iter<'a> { + Iter { + list: list, + idx: 0, + size: list.len() as u32, + } + } +} + +impl<'a> Iterator for Iter<'a> +{ + type Item = &'a BufferRef; + + fn next(&mut self) -> Option { + if self.idx >= self.size { + return None; + } + + let item = self.list.get(self.idx); + self.idx += 1; + + item + } + + fn size_hint(&self) -> (usize, Option) { + if self.idx == self.size { + return (0, Some(0)); + } + + let remaining = (self.size - self.idx) as usize; + + (remaining, Some(remaining)) + } +} + +impl<'a> DoubleEndedIterator for Iter<'a> +{ + fn next_back(&mut self) -> Option { + if self.idx == self.size { + return None; + } + + self.size -= 1; + self.list.get(self.size) + } +} + +impl<'a> ExactSizeIterator for Iter<'a> +{ +} diff --git a/gstreamer/src/lib.rs b/gstreamer/src/lib.rs index 30f17ab4c..549fb9077 100644 --- a/gstreamer/src/lib.rs +++ b/gstreamer/src/lib.rs @@ -60,6 +60,8 @@ pub mod buffer; pub use buffer::{Buffer, BufferRef, ReadBufferMap, ReadWriteBufferMap, ReadMappedBuffer, ReadWriteMappedBuffer}; pub mod sample; pub use sample::{Sample, SampleRef}; +pub mod bufferlist; +pub use bufferlist::{BufferList, BufferListRef}; mod element; mod bin;