From dc5e408c2d85aee2ec3ece8f303a38070390fd02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 23 Jan 2023 14:31:03 +0200 Subject: [PATCH] gstreamer: Add various convenience `From` impls for `Caps`, `BufferList`, `CapsFeatures`, `Buffer` and `VideoOverlayComposition` Part-of: --- .../src/video_overlay_composition.rs | 70 ++++++++++++++- gstreamer/src/buffer.rs | 26 ++++++ gstreamer/src/bufferlist.rs | 26 ++++++ gstreamer/src/caps.rs | 72 +++++++++++++++ gstreamer/src/caps_features.rs | 88 +++++++++++++++++++ 5 files changed, 280 insertions(+), 2 deletions(-) diff --git a/gstreamer-video/src/video_overlay_composition.rs b/gstreamer-video/src/video_overlay_composition.rs index f3eab7f7c..eb5c9346f 100644 --- a/gstreamer-video/src/video_overlay_composition.rs +++ b/gstreamer-video/src/video_overlay_composition.rs @@ -2,7 +2,7 @@ use std::{fmt, mem}; -use glib::translate::{from_glib, from_glib_full, from_glib_none, IntoGlib, ToGlibPtr}; +use glib::translate::*; gst::mini_object_wrapper!( VideoOverlayRectangle, @@ -341,6 +341,72 @@ impl<'a> IntoIterator for &'a VideoOverlayComposition { } } +impl From for VideoOverlayComposition { + fn from(value: VideoOverlayRectangle) -> Self { + skip_assert_initialized!(); + + unsafe { + Self::from_glib_full(ffi::gst_video_overlay_composition_new( + value.into_glib_ptr(), + )) + } + } +} + +impl<'a> From<&'a VideoOverlayRectangle> for VideoOverlayComposition { + fn from(value: &'a VideoOverlayRectangle) -> Self { + skip_assert_initialized!(); + + unsafe { Self::from_glib_full(ffi::gst_video_overlay_composition_new(value.as_mut_ptr())) } + } +} + +#[cfg(any(feature = "v1_20", feature = "dox"))] +impl From<[VideoOverlayRectangle; N]> for VideoOverlayComposition { + fn from(value: [VideoOverlayRectangle; N]) -> Self { + assert_initialized_main_thread!(); + + unsafe { + use std::ptr; + + let composition = + Self::from_glib_full(ffi::gst_video_overlay_composition_new(ptr::null_mut())); + + value.into_iter().for_each(|rect| { + ffi::gst_video_overlay_composition_add_rectangle( + composition.as_mut_ptr(), + rect.into_glib_ptr(), + ); + }); + + composition + } + } +} + +#[cfg(any(feature = "v1_20", feature = "dox"))] +impl<'a, const N: usize> From<[&'a VideoOverlayRectangle; N]> for VideoOverlayComposition { + fn from(value: [&'a VideoOverlayRectangle; N]) -> Self { + assert_initialized_main_thread!(); + + unsafe { + use std::ptr; + + let composition = + Self::from_glib_full(ffi::gst_video_overlay_composition_new(ptr::null_mut())); + + value.into_iter().for_each(|rect| { + ffi::gst_video_overlay_composition_add_rectangle( + composition.as_mut_ptr(), + rect.as_mut_ptr(), + ); + }); + + composition + } + } +} + #[cfg(any(feature = "v1_20", feature = "dox"))] impl std::iter::FromIterator for VideoOverlayComposition { fn from_iter>(iter: T) -> Self { @@ -355,7 +421,7 @@ impl std::iter::FromIterator for VideoOverlayComposition iter.into_iter().for_each(|rect| { ffi::gst_video_overlay_composition_add_rectangle( composition.as_mut_ptr(), - rect.as_mut_ptr(), + rect.into_glib_ptr(), ); }); diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index 836209e4e..d0e9def1e 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -998,6 +998,32 @@ impl<'a> IntoIterator for &'a BufferRef { } } +impl From for Buffer { + fn from(value: Memory) -> Self { + skip_assert_initialized!(); + + let mut buffer = Buffer::new(); + { + let buffer = buffer.get_mut().unwrap(); + buffer.append_memory(value); + } + buffer + } +} + +impl From<[Memory; N]> for Buffer { + fn from(value: [Memory; N]) -> Self { + skip_assert_initialized!(); + + let mut buffer = Buffer::new(); + { + let buffer = buffer.get_mut().unwrap(); + value.into_iter().for_each(|b| buffer.append_memory(b)); + } + buffer + } +} + impl std::iter::FromIterator for Buffer { fn from_iter>(iter: T) -> Self { skip_assert_initialized!(); diff --git a/gstreamer/src/bufferlist.rs b/gstreamer/src/bufferlist.rs index 4ea62be79..7a194c2a2 100644 --- a/gstreamer/src/bufferlist.rs +++ b/gstreamer/src/bufferlist.rs @@ -315,6 +315,32 @@ impl<'a> IntoIterator for &'a BufferListRef { } } +impl From for BufferList { + fn from(value: Buffer) -> Self { + skip_assert_initialized!(); + + let mut list = BufferList::new_sized(1); + { + let list = list.get_mut().unwrap(); + list.add(value); + } + list + } +} + +impl From<[Buffer; N]> for BufferList { + fn from(value: [Buffer; N]) -> Self { + skip_assert_initialized!(); + + let mut list = BufferList::new_sized(N); + { + let list = list.get_mut().unwrap(); + value.into_iter().for_each(|b| list.add(b)); + } + list + } +} + impl std::iter::FromIterator for BufferList { fn from_iter>(iter: T) -> Self { assert_initialized_main_thread!(); diff --git a/gstreamer/src/caps.rs b/gstreamer/src/caps.rs index d1dcc2831..f1b180d78 100644 --- a/gstreamer/src/caps.rs +++ b/gstreamer/src/caps.rs @@ -157,6 +157,78 @@ impl str::FromStr for Caps { } } +impl From for Caps { + fn from(v: Structure) -> Caps { + skip_assert_initialized!(); + let mut caps = Caps::new_empty(); + + { + let caps = caps.get_mut().unwrap(); + caps.append_structure(v); + } + + caps + } +} + +impl From<[Structure; N]> for Caps { + fn from(v: [Structure; N]) -> Caps { + skip_assert_initialized!(); + let mut caps = Caps::new_empty(); + + { + let caps = caps.get_mut().unwrap(); + v.into_iter().for_each(|s| caps.append_structure(s)); + } + + caps + } +} + +impl From<(Structure, CapsFeatures)> for Caps { + fn from(v: (Structure, CapsFeatures)) -> Caps { + skip_assert_initialized!(); + let mut caps = Caps::new_empty(); + + { + let caps = caps.get_mut().unwrap(); + caps.append_structure_full(v.0, Some(v.1)); + } + + caps + } +} + +impl From<[(Structure, CapsFeatures); N]> for Caps { + fn from(v: [(Structure, CapsFeatures); N]) -> Caps { + skip_assert_initialized!(); + let mut caps = Caps::new_empty(); + + { + let caps = caps.get_mut().unwrap(); + v.into_iter() + .for_each(|s| caps.append_structure_full(s.0, Some(s.1))); + } + + caps + } +} + +impl From<[(Structure, Option); N]> for Caps { + fn from(v: [(Structure, Option); N]) -> Caps { + skip_assert_initialized!(); + let mut caps = Caps::new_empty(); + + { + let caps = caps.get_mut().unwrap(); + v.into_iter() + .for_each(|s| caps.append_structure_full(s.0, s.1)); + } + + caps + } +} + impl std::iter::FromIterator for Caps { fn from_iter>(iter: T) -> Self { skip_assert_initialized!(); diff --git a/gstreamer/src/caps_features.rs b/gstreamer/src/caps_features.rs index 2477d9c62..546a0f11b 100644 --- a/gstreamer/src/caps_features.rs +++ b/gstreamer/src/caps_features.rs @@ -669,6 +669,94 @@ impl<'a> IntoIterator for &'a CapsFeaturesRef { } } +impl<'a> From<&'a str> for CapsFeatures { + fn from(value: &'a str) -> Self { + skip_assert_initialized!(); + let mut features = CapsFeatures::new_empty(); + + features.add(value); + + features + } +} + +impl<'a> From<&'a glib::GStr> for CapsFeatures { + fn from(value: &'a glib::GStr) -> Self { + skip_assert_initialized!(); + let mut features = CapsFeatures::new_empty(); + + features.add(value); + + features + } +} + +impl From for CapsFeatures { + fn from(value: glib::Quark) -> Self { + skip_assert_initialized!(); + let mut features = CapsFeatures::new_empty(); + + features.add_from_quark(value); + + features + } +} + +impl<'a, const N: usize> From<[&'a str; N]> for CapsFeatures { + fn from(value: [&'a str; N]) -> Self { + skip_assert_initialized!(); + let mut features = CapsFeatures::new_empty(); + + value.into_iter().for_each(|f| features.add(f)); + + features + } +} + +impl<'a, const N: usize> From<[&'a glib::GStr; N]> for CapsFeatures { + fn from(value: [&'a glib::GStr; N]) -> Self { + skip_assert_initialized!(); + let mut features = CapsFeatures::new_empty(); + + value.into_iter().for_each(|f| features.add(f)); + + features + } +} + +impl From<[String; N]> for CapsFeatures { + fn from(value: [String; N]) -> Self { + skip_assert_initialized!(); + let mut features = CapsFeatures::new_empty(); + + value.into_iter().for_each(|f| features.add(&f)); + + features + } +} + +impl From<[glib::GString; N]> for CapsFeatures { + fn from(value: [glib::GString; N]) -> Self { + skip_assert_initialized!(); + let mut features = CapsFeatures::new_empty(); + + value.into_iter().for_each(|f| features.add(&f)); + + features + } +} + +impl From<[glib::Quark; N]> for CapsFeatures { + fn from(value: [glib::Quark; N]) -> Self { + skip_assert_initialized!(); + let mut features = CapsFeatures::new_empty(); + + value.into_iter().for_each(|f| features.add_from_quark(f)); + + features + } +} + impl<'a> std::iter::FromIterator<&'a str> for CapsFeatures { fn from_iter>(iter: T) -> Self { skip_assert_initialized!();