Update for glib::Object::new() API changes

This commit is contained in:
Sebastian Dröge 2022-10-07 20:39:11 +03:00
parent dcd53bd16e
commit 3cd902513d
13 changed files with 30 additions and 89 deletions

View file

@ -422,7 +422,7 @@ mod cairo_compositor {
impl CairoCompositor { impl CairoCompositor {
// Creates a new instance of our compositor with the given name. // Creates a new instance of our compositor with the given name.
pub fn new(name: Option<&str>) -> Self { pub fn new(name: Option<&str>) -> Self {
glib::Object::new(&[("name", &name)]).expect("Failed to create cairo compositor") glib::Object::new(&[("name", &name)])
} }
} }

View file

@ -52,7 +52,7 @@ fn create_receiver_pipeline(
let pipeline = gst::Pipeline::new(None); let pipeline = gst::Pipeline::new(None);
let src = gst::ElementFactory::make("appsrc", None).map_err(|_| MissingElement("appsrc"))?; let src = gst::ElementFactory::make("appsrc", None).map_err(|_| MissingElement("appsrc"))?;
let filter = video_filter::FdMemoryFadeInVideoFilter::new()?.upcast::<gst::Element>(); let filter = video_filter::FdMemoryFadeInVideoFilter::default().upcast::<gst::Element>();
let convert = gst::ElementFactory::make("videoconvert", None) let convert = gst::ElementFactory::make("videoconvert", None)
.map_err(|_| MissingElement("videoconvert"))?; .map_err(|_| MissingElement("videoconvert"))?;
let queue = gst::ElementFactory::make("queue", None).map_err(|_| MissingElement("queue"))?; let queue = gst::ElementFactory::make("queue", None).map_err(|_| MissingElement("queue"))?;
@ -376,15 +376,13 @@ fn main() {
// The purpose of this custom video filter is to demonstrate how // The purpose of this custom video filter is to demonstrate how
// the file descriptor of a FdMemory can be accessed. // the file descriptor of a FdMemory can be accessed.
mod video_filter { mod video_filter {
use anyhow::Error;
glib::wrapper! { glib::wrapper! {
pub struct FdMemoryFadeInVideoFilter(ObjectSubclass<imp::FdMemoryFadeInVideoFilter>) @extends gst_video::VideoFilter, gst_base::BaseTransform, gst::Element, gst::Object; pub struct FdMemoryFadeInVideoFilter(ObjectSubclass<imp::FdMemoryFadeInVideoFilter>) @extends gst_video::VideoFilter, gst_base::BaseTransform, gst::Element, gst::Object;
} }
impl FdMemoryFadeInVideoFilter { impl Default for FdMemoryFadeInVideoFilter {
pub fn new() -> Result<Self, Error> { fn default() -> Self {
Ok(glib::Object::builder().build()?) glib::Object::builder().build()
} }
} }
mod imp { mod imp {

View file

@ -52,7 +52,7 @@ mod mirror {
impl GLMirrorFilter { impl GLMirrorFilter {
pub fn new(name: Option<&str>) -> Self { pub fn new(name: Option<&str>) -> Self {
glib::Object::new(&[("name", &name)]).expect("Failed to create GL Mirror Filter Object") glib::Object::new(&[("name", &name)])
} }
} }

View file

@ -153,7 +153,7 @@ mod media_factory {
impl Default for Factory { impl Default for Factory {
// Creates a new instance of our factory // Creates a new instance of our factory
fn default() -> Factory { fn default() -> Factory {
glib::Object::new(&[]).expect("Failed to create factory") glib::Object::new(&[])
} }
} }
} }
@ -265,7 +265,7 @@ mod server {
impl Default for Server { impl Default for Server {
// Creates a new instance of our factory // Creates a new instance of our factory
fn default() -> Server { fn default() -> Server {
glib::Object::new(&[]).expect("Failed to create server") glib::Object::new(&[])
} }
} }
} }
@ -313,7 +313,7 @@ mod client {
impl Default for Client { impl Default for Client {
// Creates a new instance of our factory // Creates a new instance of our factory
fn default() -> Client { fn default() -> Client {
glib::Object::new(&[]).expect("Failed to create client") glib::Object::new(&[])
} }
} }
} }
@ -361,7 +361,7 @@ mod mount_points {
impl Default for MountPoints { impl Default for MountPoints {
// Creates a new instance of our factory // Creates a new instance of our factory
fn default() -> Self { fn default() -> Self {
glib::Object::new(&[]).expect("Failed to create mount points") glib::Object::new(&[])
} }
} }
} }

View file

@ -223,7 +223,7 @@ mod fir_filter {
impl FirFilter { impl FirFilter {
// Creates a new instance of our filter with the given name // Creates a new instance of our filter with the given name
pub fn new(name: Option<&str>) -> FirFilter { pub fn new(name: Option<&str>) -> FirFilter {
glib::Object::new(&[("name", &name)]).expect("Failed to create fir filter") glib::Object::new(&[("name", &name)])
} }
// Sets the coefficients by getting access to the private // Sets the coefficients by getting access to the private

View file

@ -15,32 +15,11 @@ pub trait ChildProxyExtManual: 'static {
#[doc(alias = "get_child_property")] #[doc(alias = "get_child_property")]
#[doc(alias = "gst_child_proxy_get")] #[doc(alias = "gst_child_proxy_get")]
fn child_property_value(&self, name: &str) -> glib::Value; fn child_property_value(&self, name: &str) -> glib::Value;
#[doc(alias = "get_child_property")]
#[doc(alias = "gst_child_proxy_get")]
fn try_child_property<V: for<'b> glib::value::FromValue<'b> + 'static>(
&self,
name: &str,
) -> Result<V, glib::BoolError>;
#[doc(alias = "get_child_property")]
#[doc(alias = "gst_child_proxy_get")]
fn try_child_property_value(&self, name: &str) -> Result<glib::Value, glib::BoolError>;
#[doc(alias = "gst_child_proxy_set")] #[doc(alias = "gst_child_proxy_set")]
fn set_child_property<V: glib::ToValue>(&self, name: &str, value: V); fn set_child_property<V: glib::ToValue>(&self, name: &str, value: V);
#[doc(alias = "gst_child_proxy_set_property")] #[doc(alias = "gst_child_proxy_set_property")]
fn set_child_property_from_value(&self, name: &str, value: &glib::Value); fn set_child_property_from_value(&self, name: &str, value: &glib::Value);
#[doc(alias = "gst_child_proxy_set")]
fn try_set_child_property<V: glib::ToValue>(
&self,
name: &str,
value: V,
) -> Result<(), glib::BoolError>;
#[doc(alias = "gst_child_proxy_set_property")]
fn try_set_child_property_from_value(
&self,
name: &str,
value: &glib::Value,
) -> Result<(), glib::BoolError>;
} }
impl<O: IsA<ChildProxy>> ChildProxyExtManual for O { impl<O: IsA<ChildProxy>> ChildProxyExtManual for O {
@ -62,54 +41,27 @@ impl<O: IsA<ChildProxy>> ChildProxyExtManual for O {
} }
} }
#[track_caller]
fn child_property<V: for<'b> glib::value::FromValue<'b> + 'static>(&self, name: &str) -> V { fn child_property<V: for<'b> glib::value::FromValue<'b> + 'static>(&self, name: &str) -> V {
let (child, pspec) = self.lookup(name).unwrap(); let (child, pspec) = self.lookup(name).unwrap();
child.property(pspec.name()) child.property(pspec.name())
} }
#[track_caller]
fn child_property_value(&self, name: &str) -> glib::Value { fn child_property_value(&self, name: &str) -> glib::Value {
let (child, pspec) = self.lookup(name).unwrap(); let (child, pspec) = self.lookup(name).unwrap();
child.property_value(pspec.name()) child.property_value(pspec.name())
} }
fn try_child_property<V: for<'b> glib::value::FromValue<'b> + 'static>( #[track_caller]
&self,
name: &str,
) -> Result<V, glib::BoolError> {
let (child, pspec) = self.lookup(name)?;
child.try_property(pspec.name())
}
fn try_child_property_value(&self, name: &str) -> Result<glib::Value, glib::BoolError> {
let (child, pspec) = self.lookup(name)?;
child.try_property_value(pspec.name())
}
fn set_child_property<V: glib::ToValue>(&self, name: &str, value: V) { fn set_child_property<V: glib::ToValue>(&self, name: &str, value: V) {
let (child, pspec) = self.lookup(name).unwrap(); let (child, pspec) = self.lookup(name).unwrap();
child.set_property(pspec.name(), value) child.set_property(pspec.name(), value)
} }
#[track_caller]
fn set_child_property_from_value(&self, name: &str, value: &glib::Value) { fn set_child_property_from_value(&self, name: &str, value: &glib::Value) {
let (child, pspec) = self.lookup(name).unwrap(); let (child, pspec) = self.lookup(name).unwrap();
child.set_property_from_value(pspec.name(), value) child.set_property_from_value(pspec.name(), value)
} }
fn try_set_child_property<V: glib::ToValue>(
&self,
name: &str,
value: V,
) -> Result<(), glib::BoolError> {
let (child, pspec) = self.lookup(name)?;
child.try_set_property(pspec.name(), value)
}
fn try_set_child_property_from_value(
&self,
name: &str,
value: &glib::Value,
) -> Result<(), glib::BoolError> {
let (child, pspec) = self.lookup(name)?;
child.try_set_property_from_value(pspec.name(), value)
}
} }

View file

@ -4,18 +4,16 @@ use crate::value::GstValueExt;
use glib::prelude::*; use glib::prelude::*;
pub trait GObjectExtManualGst: 'static { pub trait GObjectExtManualGst: 'static {
#[doc(alias = "gst_util_set_object_arg")]
fn try_set_property_from_str(&self, name: &str, value: &str) -> Result<(), glib::BoolError>;
#[doc(alias = "gst_util_set_object_arg")] #[doc(alias = "gst_util_set_object_arg")]
fn set_property_from_str(&self, name: &str, value: &str); fn set_property_from_str(&self, name: &str, value: &str);
} }
impl<O: IsA<glib::Object>> GObjectExtManualGst for O { impl<O: IsA<glib::Object>> GObjectExtManualGst for O {
fn try_set_property_from_str(&self, name: &str, value: &str) -> Result<(), glib::BoolError> { #[track_caller]
let pspec = self.find_property(name).ok_or_else(|| { fn set_property_from_str(&self, name: &str, value: &str) {
glib::bool_error!("property '{}' of type '{}' not found", name, self.type_()) let pspec = self.find_property(name).unwrap_or_else(|| {
})?; panic!("property '{}' of type '{}' not found", name, self.type_());
});
let value = { let value = {
if pspec.value_type() == crate::Structure::static_type() && value == "NULL" { if pspec.value_type() == crate::Structure::static_type() && value == "NULL" {
@ -23,20 +21,16 @@ impl<O: IsA<glib::Object>> GObjectExtManualGst for O {
} else { } else {
#[cfg(feature = "v1_20")] #[cfg(feature = "v1_20")]
{ {
glib::Value::deserialize_with_pspec(value, &pspec)? glib::Value::deserialize_with_pspec(value, &pspec).unwrap()
} }
#[cfg(not(feature = "v1_20"))] #[cfg(not(feature = "v1_20"))]
{ {
glib::Value::deserialize(value, pspec.value_type())? glib::Value::deserialize(value, pspec.value_type()).unwrap()
} }
} }
}; };
self.try_set_property_from_value(name, &value) self.set_property_from_value(name, &value)
}
fn set_property_from_str(&self, name: &str, value: &str) {
self.try_set_property_from_str(name, value).unwrap()
} }
} }

View file

@ -1661,8 +1661,7 @@ impl<T: IsA<Pad> + IsA<glib::Object> + glib::object::IsClass> PadBuilder<T> {
pub fn new(name: Option<&str>, direction: crate::PadDirection) -> Self { pub fn new(name: Option<&str>, direction: crate::PadDirection) -> Self {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
let pad = glib::Object::new::<T>(&[("name", &name), ("direction", &direction)]) let pad = glib::Object::new::<T>(&[("name", &name), ("direction", &direction)]);
.expect("Failed to create pad");
// Ghost pads are a bit special // Ghost pads are a bit special
if let Some(pad) = pad.dynamic_cast_ref::<crate::GhostPad>() { if let Some(pad) = pad.dynamic_cast_ref::<crate::GhostPad>() {
@ -1713,7 +1712,6 @@ impl<T: IsA<Pad> + IsA<glib::Object> + glib::object::IsClass> PadBuilder<T> {
("template", templ), ("template", templ),
], ],
) )
.expect("Failed to create pad")
.downcast::<T>() .downcast::<T>()
.unwrap(); .unwrap();

View file

@ -554,7 +554,7 @@ mod tests {
impl Default for TestBufferPool { impl Default for TestBufferPool {
fn default() -> Self { fn default() -> Self {
glib::Object::new(&[]).unwrap() glib::Object::new(&[])
} }
} }

View file

@ -782,7 +782,7 @@ mod tests {
impl TestElement { impl TestElement {
pub fn new(name: Option<&str>) -> Self { pub fn new(name: Option<&str>) -> Self {
glib::Object::new(&[("name", &name)]).unwrap() glib::Object::new(&[("name", &name)])
} }
} }

View file

@ -131,7 +131,7 @@ mod tests {
impl TestPad { impl TestPad {
pub fn new(name: &str, direction: PadDirection) -> Self { pub fn new(name: &str, direction: PadDirection) -> Self {
glib::Object::new(&[("name", &name), ("direction", &direction)]).unwrap() glib::Object::new(&[("name", &name), ("direction", &direction)])
} }
} }

View file

@ -313,7 +313,7 @@ mod tests {
impl Default for TestPool { impl Default for TestPool {
fn default() -> Self { fn default() -> Self {
glib::Object::new(&[]).unwrap() glib::Object::new(&[])
} }
} }

View file

@ -56,7 +56,7 @@ fn send_seek_event(pipeline: &Element, rate: f64) -> bool {
}; };
// If we have not done so, obtain the sink through which we will send the seek events // If we have not done so, obtain the sink through which we will send the seek events
if let Ok(Some(video_sink)) = pipeline.try_property::<Option<Element>>("video-sink") { if let Some(video_sink) = pipeline.property::<Option<Element>>("video-sink") {
println!("Current rate: {}\r", rate); println!("Current rate: {}\r", rate);
// Send the event // Send the event
video_sink.send_event(seek_event) video_sink.send_event(seek_event)
@ -171,8 +171,7 @@ USAGE: Choose one of the following options, then press enter:
} }
} }
Command::NextFrame => { Command::NextFrame => {
if let Ok(Some(video_sink)) = pipeline.try_property::<Option<Element>>("video-sink") if let Some(video_sink) = pipeline.property::<Option<Element>>("video-sink") {
{
// Send the event // Send the event
let step = Step::new(gst::format::Buffers(1), rate.abs(), true, false); let step = Step::new(gst::format::Buffers(1), rate.abs(), true, false);
video_sink.send_event(step); video_sink.send_event(step);