2018-09-29 08:13:57 +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 std::fmt;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::ops;
|
|
|
|
|
|
|
|
use miniobject::MiniObject;
|
|
|
|
use BufferRef;
|
|
|
|
|
|
|
|
use ffi;
|
|
|
|
use glib;
|
|
|
|
use glib::translate::{from_glib, FromGlib};
|
|
|
|
use glib_ffi;
|
|
|
|
|
|
|
|
pub unsafe trait MetaAPI: Sized {
|
|
|
|
type GstType;
|
|
|
|
|
|
|
|
fn get_meta_api() -> glib::Type;
|
|
|
|
|
2018-10-11 08:30:54 +00:00
|
|
|
unsafe fn from_ptr(buffer: &BufferRef, ptr: *const Self::GstType) -> MetaRef<Self> {
|
2018-09-29 08:13:57 +00:00
|
|
|
assert!(!ptr.is_null());
|
|
|
|
|
|
|
|
let meta_api = Self::get_meta_api();
|
|
|
|
if meta_api != glib::Type::Invalid {
|
|
|
|
assert_eq!(
|
|
|
|
meta_api,
|
|
|
|
from_glib((*(*(ptr as *const ffi::GstMeta)).info).api)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
MetaRef {
|
2018-10-11 08:31:28 +00:00
|
|
|
meta: &*(ptr as *const Self),
|
2018-09-29 08:13:57 +00:00
|
|
|
buffer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 08:30:54 +00:00
|
|
|
unsafe fn from_mut_ptr<T>(
|
|
|
|
buffer: &mut BufferRef,
|
2018-09-29 08:13:57 +00:00
|
|
|
ptr: *mut Self::GstType,
|
2018-10-11 08:30:54 +00:00
|
|
|
) -> MetaRefMut<Self, T> {
|
2018-09-29 08:13:57 +00:00
|
|
|
assert!(!ptr.is_null());
|
|
|
|
|
|
|
|
let meta_api = Self::get_meta_api();
|
|
|
|
if meta_api != glib::Type::Invalid {
|
|
|
|
assert_eq!(
|
|
|
|
meta_api,
|
|
|
|
from_glib((*(*(ptr as *const ffi::GstMeta)).info).api)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
MetaRefMut {
|
2018-10-11 08:31:28 +00:00
|
|
|
meta: &mut *(ptr as *mut Self),
|
2018-09-29 08:13:57 +00:00
|
|
|
buffer,
|
|
|
|
mode: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MetaRef<'a, T: MetaAPI + 'a> {
|
|
|
|
meta: &'a T,
|
2018-09-29 22:17:12 +00:00
|
|
|
buffer: &'a BufferRef,
|
2018-09-29 08:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Standalone {}
|
|
|
|
pub enum Iterated {}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MetaRefMut<'a, T: MetaAPI + 'a, U> {
|
|
|
|
meta: &'a mut T,
|
2018-09-29 22:17:12 +00:00
|
|
|
buffer: &'a mut BufferRef,
|
2018-09-29 08:13:57 +00:00
|
|
|
mode: PhantomData<U>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MetaAPI> ops::Deref for MetaRef<'a, T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
self.meta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MetaAPI> AsRef<MetaRef<'a, T>> for MetaRef<'a, T> {
|
|
|
|
fn as_ref(&self) -> &MetaRef<'a, T> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MetaAPI, U> ops::Deref for MetaRefMut<'a, T, U> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
self.meta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MetaAPI, U> ops::DerefMut for MetaRefMut<'a, T, U> {
|
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
self.meta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MetaAPI, U> AsRef<MetaRef<'a, T>> for MetaRefMut<'a, T, U> {
|
|
|
|
fn as_ref(&self) -> &MetaRef<'a, T> {
|
2018-10-11 08:31:28 +00:00
|
|
|
unsafe { &*(self as *const MetaRefMut<'a, T, U> as *const MetaRef<'a, T>) }
|
2018-09-29 08:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MetaAPI> MetaRef<'a, T> {
|
|
|
|
pub fn get_api(&self) -> glib::Type {
|
|
|
|
unsafe {
|
|
|
|
let meta = self.meta as *const _ as *const ffi::GstMeta;
|
|
|
|
let info = (*meta).info;
|
|
|
|
glib::Type::from_glib((*info).api)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ptr(&self) -> *const T::GstType {
|
|
|
|
self.meta as *const _ as *const <T as MetaAPI>::GstType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 22:17:12 +00:00
|
|
|
impl<'a> MetaRef<'a, Meta> {
|
2018-09-29 08:13:57 +00:00
|
|
|
pub fn downcast_ref<T: MetaAPI>(&self) -> Option<&MetaRef<'a, T>> {
|
|
|
|
let target_type = T::get_meta_api();
|
|
|
|
let type_ = self.get_api();
|
|
|
|
|
|
|
|
if type_ == glib::Type::Invalid || target_type == type_ {
|
2018-10-11 08:31:28 +00:00
|
|
|
Some(unsafe { &*(self as *const MetaRef<'a, Meta> as *const MetaRef<'a, T>) })
|
2018-09-29 08:13:57 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MetaAPI, U> MetaRefMut<'a, T, U> {
|
|
|
|
pub fn get_api(&self) -> glib::Type {
|
|
|
|
unsafe {
|
|
|
|
let meta = self.meta as *const _ as *const ffi::GstMeta;
|
|
|
|
let info = (*meta).info;
|
|
|
|
glib::Type::from_glib((*info).api)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ptr(&self) -> *const T::GstType {
|
|
|
|
self.meta as *const _ as *const <T as MetaAPI>::GstType
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_mut_ptr(&mut self) -> *mut T::GstType {
|
|
|
|
self.meta as *mut _ as *mut <T as MetaAPI>::GstType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MetaAPI> MetaRefMut<'a, T, Standalone> {
|
|
|
|
pub fn remove(mut self) {
|
|
|
|
unsafe {
|
2018-09-29 22:17:12 +00:00
|
|
|
let res = ffi::gst_buffer_remove_meta(
|
|
|
|
self.buffer.as_mut_ptr(),
|
|
|
|
self.as_mut_ptr() as *mut ffi::GstMeta,
|
|
|
|
);
|
2018-09-29 08:13:57 +00:00
|
|
|
assert_ne!(res, glib_ffi::GFALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 22:17:12 +00:00
|
|
|
impl<'a, U> MetaRefMut<'a, Meta, U> {
|
2018-09-29 08:13:57 +00:00
|
|
|
pub fn downcast_ref<T: MetaAPI>(&mut self) -> Option<&MetaRefMut<'a, T, U>> {
|
|
|
|
let target_type = T::get_meta_api();
|
|
|
|
let type_ = self.get_api();
|
|
|
|
|
|
|
|
if type_ == glib::Type::Invalid || target_type == type_ {
|
2018-10-11 08:31:28 +00:00
|
|
|
Some(unsafe { &*(self as *mut MetaRefMut<'a, Meta, U> as *const MetaRefMut<'a, T, U>) })
|
2018-09-29 08:13:57 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
2018-09-29 22:17:12 +00:00
|
|
|
pub struct Meta(ffi::GstMeta);
|
2018-09-29 08:13:57 +00:00
|
|
|
|
2018-09-29 22:17:12 +00:00
|
|
|
impl Meta {
|
2018-09-29 08:13:57 +00:00
|
|
|
fn get_api(&self) -> glib::Type {
|
|
|
|
unsafe { glib::Type::from_glib((*self.0.info).api) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 22:17:12 +00:00
|
|
|
unsafe impl MetaAPI for Meta {
|
2018-09-29 08:13:57 +00:00
|
|
|
type GstType = ffi::GstMeta;
|
|
|
|
|
|
|
|
fn get_meta_api() -> glib::Type {
|
|
|
|
glib::Type::Invalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 22:17:12 +00:00
|
|
|
impl fmt::Debug for Meta {
|
2018-09-29 08:13:57 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("Meta")
|
|
|
|
.field("api", &self.get_api())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
2018-09-29 22:17:12 +00:00
|
|
|
pub struct ParentBufferMeta(ffi::GstParentBufferMeta);
|
2018-09-29 08:13:57 +00:00
|
|
|
|
2018-09-29 22:17:12 +00:00
|
|
|
impl ParentBufferMeta {
|
|
|
|
pub fn add<'a>(
|
|
|
|
buffer: &'a mut BufferRef,
|
|
|
|
parent: &BufferRef,
|
|
|
|
) -> MetaRefMut<'a, Self, Standalone> {
|
2018-09-29 08:13:57 +00:00
|
|
|
unsafe {
|
|
|
|
let meta =
|
|
|
|
ffi::gst_buffer_add_parent_buffer_meta(buffer.as_mut_ptr(), parent.as_mut_ptr());
|
|
|
|
|
2018-09-29 22:17:12 +00:00
|
|
|
Self::from_mut_ptr(buffer, meta)
|
2018-09-29 08:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_parent(&self) -> &BufferRef {
|
|
|
|
unsafe { BufferRef::from_ptr(self.0.buffer) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 22:17:12 +00:00
|
|
|
unsafe impl MetaAPI for ParentBufferMeta {
|
2018-09-29 08:13:57 +00:00
|
|
|
type GstType = ffi::GstParentBufferMeta;
|
|
|
|
|
|
|
|
fn get_meta_api() -> glib::Type {
|
|
|
|
unsafe { from_glib(ffi::gst_parent_buffer_meta_api_get_type()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 22:17:12 +00:00
|
|
|
impl fmt::Debug for ParentBufferMeta {
|
2018-09-29 08:13:57 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("ParentBufferMeta")
|
|
|
|
.field("parent", &self.get_parent())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_get_iterate_meta() {
|
2018-09-29 09:19:47 +00:00
|
|
|
::init().unwrap();
|
2018-09-29 08:13:57 +00:00
|
|
|
|
|
|
|
let mut buffer = ::Buffer::new();
|
|
|
|
let parent = ::Buffer::new();
|
|
|
|
{
|
|
|
|
let meta = ParentBufferMeta::add(buffer.get_mut().unwrap(), &*parent);
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(meta.get_parent().as_ptr(), parent.as_ptr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let metas = buffer.iter_meta::<Meta>().collect::<Vec<_>>();
|
|
|
|
assert_eq!(metas.len(), 1);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let metas = buffer
|
|
|
|
.get_mut()
|
|
|
|
.unwrap()
|
|
|
|
.iter_meta_mut::<Meta>()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(metas.len(), 1);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let metas = buffer.iter_meta::<ParentBufferMeta>().collect::<Vec<_>>();
|
|
|
|
assert_eq!(metas.len(), 1);
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(metas[0].get_parent().as_ptr(), parent.as_ptr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let metas = buffer
|
|
|
|
.get_mut()
|
|
|
|
.unwrap()
|
|
|
|
.iter_meta_mut::<ParentBufferMeta>()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(metas.len(), 1);
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(metas[0].get_parent().as_ptr(), parent.as_ptr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let meta = buffer
|
|
|
|
.get_mut()
|
|
|
|
.unwrap()
|
|
|
|
.get_meta_mut::<ParentBufferMeta>()
|
|
|
|
.unwrap();
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(meta.get_parent().as_ptr(), parent.as_ptr());
|
|
|
|
}
|
|
|
|
meta.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let metas = buffer.iter_meta::<Meta>().collect::<Vec<_>>();
|
|
|
|
assert_eq!(metas.len(), 0);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let metas = buffer
|
|
|
|
.get_mut()
|
|
|
|
.unwrap()
|
|
|
|
.iter_meta_mut::<Meta>()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(metas.len(), 0);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let metas = buffer.iter_meta::<ParentBufferMeta>().collect::<Vec<_>>();
|
|
|
|
assert_eq!(metas.len(), 0);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let metas = buffer
|
|
|
|
.get_mut()
|
|
|
|
.unwrap()
|
|
|
|
.iter_meta_mut::<ParentBufferMeta>()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(metas.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(buffer.get_meta::<ParentBufferMeta>().is_none());
|
|
|
|
}
|
|
|
|
}
|