From 1e793f3b654e62af8da623cfe9eb6bdf78870faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 4 Jan 2023 19:48:41 +0200 Subject: [PATCH] gstreamer: Return some values by reference instead Less refcounting. Part-of: --- examples/src/bin/playbin.rs | 2 +- examples/src/bin/rtpfecclient.rs | 2 +- examples/src/bin/rtpfecserver.rs | 2 +- examples/src/glupload.rs | 4 ++-- gstreamer/src/memory.rs | 10 ++++++++-- gstreamer/src/message.rs | 10 ++++++++-- gstreamer/src/param_spec.rs | 11 +++++++++-- tutorials/src/bin/basic-tutorial-3.rs | 2 +- tutorials/src/bin/basic-tutorial-4.rs | 2 +- tutorials/src/bin/basic-tutorial-5.rs | 2 +- tutorials/src/bin/basic-tutorial-6.rs | 2 +- tutorials/src/bin/playback-tutorial-1.rs | 2 +- tutorials/src/bin/playback-tutorial-2.rs | 2 +- 13 files changed, 36 insertions(+), 17 deletions(-) diff --git a/examples/src/bin/playbin.rs b/examples/src/bin/playbin.rs index 34e9e2645..92d2452bd 100644 --- a/examples/src/bin/playbin.rs +++ b/examples/src/bin/playbin.rs @@ -121,7 +121,7 @@ fn example_main() { MessageView::StateChanged(state_changed) => // We are only interested in state-changed messages from playbin { - if state_changed.src().map(|s| s == playbin).unwrap_or(false) + if state_changed.src().map(|s| s == &playbin).unwrap_or(false) && state_changed.current() == gst::State::Playing { // Generate a dot graph of the pipeline to GST_DEBUG_DUMP_DOT_DIR if defined diff --git a/examples/src/bin/rtpfecclient.rs b/examples/src/bin/rtpfecclient.rs index b8aa9ae78..4182eb497 100644 --- a/examples/src/bin/rtpfecclient.rs +++ b/examples/src/bin/rtpfecclient.rs @@ -253,7 +253,7 @@ fn example_main() -> Result<(), Error> { } MessageView::StateChanged(s) => { if let Some(element) = msg.src() { - if element == pipeline && s.current() == gst::State::Playing { + if element == &pipeline && s.current() == gst::State::Playing { eprintln!("PLAYING"); gst::debug_bin_to_dot_file( &pipeline, diff --git a/examples/src/bin/rtpfecserver.rs b/examples/src/bin/rtpfecserver.rs index 2fd963a1b..b2d5f3b5a 100644 --- a/examples/src/bin/rtpfecserver.rs +++ b/examples/src/bin/rtpfecserver.rs @@ -179,7 +179,7 @@ fn example_main() -> Result<(), Error> { } MessageView::StateChanged(s) => { if let Some(element) = msg.src() { - if element == pipeline && s.current() == gst::State::Playing { + if element == &pipeline && s.current() == gst::State::Playing { eprintln!("PLAYING"); gst::debug_bin_to_dot_file( &pipeline, diff --git a/examples/src/glupload.rs b/examples/src/glupload.rs index c75f51746..5dd37c534 100644 --- a/examples/src/glupload.rs +++ b/examples/src/glupload.rs @@ -414,7 +414,7 @@ impl App { let context_type = ctxt.context_type(); if context_type == *gst_gl::GL_DISPLAY_CONTEXT_TYPE { if let Some(el) = - msg.src().map(|s| s.downcast::().unwrap()) + msg.src().map(|s| s.downcast_ref::().unwrap()) { let context = gst::Context::new(context_type, true); context.set_gl_display(&gl_display); @@ -423,7 +423,7 @@ impl App { } if context_type == "gst.gl.app_context" { if let Some(el) = - msg.src().map(|s| s.downcast::().unwrap()) + msg.src().map(|s| s.downcast_ref::().unwrap()) { let mut context = gst::Context::new(context_type, true); { diff --git a/gstreamer/src/memory.rs b/gstreamer/src/memory.rs index 376297155..6640f8ed7 100644 --- a/gstreamer/src/memory.rs +++ b/gstreamer/src/memory.rs @@ -166,8 +166,14 @@ impl Memory { impl MemoryRef { #[doc(alias = "get_allocator")] - pub fn allocator(&self) -> Option { - unsafe { from_glib_none(self.0.allocator) } + pub fn allocator(&self) -> Option<&Allocator> { + unsafe { + if self.0.allocator.is_null() { + None + } else { + Some(&*(&self.0.allocator as *const *mut ffi::GstAllocator as *const Allocator)) + } + } } #[doc(alias = "get_parent")] diff --git a/gstreamer/src/message.rs b/gstreamer/src/message.rs index 8a8763c98..5606aa30c 100644 --- a/gstreamer/src/message.rs +++ b/gstreamer/src/message.rs @@ -17,8 +17,14 @@ mini_object_wrapper!(Message, MessageRef, ffi::GstMessage, || { impl MessageRef { #[doc(alias = "get_src")] - pub fn src(&self) -> Option { - unsafe { from_glib_none((*self.as_ptr()).src) } + pub fn src(&self) -> Option<&Object> { + unsafe { + if (*self.as_ptr()).src.is_null() { + None + } else { + Some(&*(&(*self.as_ptr()).src as *const *mut ffi::GstObject as *const Object)) + } + } } #[doc(alias = "get_seqnum")] diff --git a/gstreamer/src/param_spec.rs b/gstreamer/src/param_spec.rs index 00b1394dc..88d0c36cc 100644 --- a/gstreamer/src/param_spec.rs +++ b/gstreamer/src/param_spec.rs @@ -239,11 +239,18 @@ impl ParamSpecArray { } } - pub fn element_spec(&self) -> Option { + pub fn element_spec(&self) -> Option<&ParamSpec> { unsafe { let ptr = self.as_ptr(); - from_glib_none((*ptr).element_spec) + if (*ptr).element_spec.is_null() { + None + } else { + Some( + &*(&(*ptr).element_spec as *const *mut glib::gobject_ffi::GParamSpec + as *const glib::ParamSpec), + ) + } } } diff --git a/tutorials/src/bin/basic-tutorial-3.rs b/tutorials/src/bin/basic-tutorial-3.rs index 858d62390..c073be8e1 100644 --- a/tutorials/src/bin/basic-tutorial-3.rs +++ b/tutorials/src/bin/basic-tutorial-3.rs @@ -98,7 +98,7 @@ fn tutorial_main() { break; } MessageView::StateChanged(state_changed) => { - if state_changed.src().map(|s| s == pipeline).unwrap_or(false) { + if state_changed.src().map(|s| s == &pipeline).unwrap_or(false) { println!( "Pipeline state changed from {:?} to {:?}", state_changed.old(), diff --git a/tutorials/src/bin/basic-tutorial-4.rs b/tutorials/src/bin/basic-tutorial-4.rs index 4e9077333..dd1f56c64 100644 --- a/tutorials/src/bin/basic-tutorial-4.rs +++ b/tutorials/src/bin/basic-tutorial-4.rs @@ -128,7 +128,7 @@ fn handle_message(custom_data: &mut CustomData, msg: &gst::Message) { MessageView::StateChanged(state_changed) => { if state_changed .src() - .map(|s| s == custom_data.playbin) + .map(|s| s == &custom_data.playbin) .unwrap_or(false) { let new_state = state_changed.current(); diff --git a/tutorials/src/bin/basic-tutorial-5.rs b/tutorials/src/bin/basic-tutorial-5.rs index 336e0c145..c2b95b78b 100644 --- a/tutorials/src/bin/basic-tutorial-5.rs +++ b/tutorials/src/bin/basic-tutorial-5.rs @@ -360,7 +360,7 @@ mod tutorial5 { // This is called when the pipeline changes states. We use it to // keep track of the current state. gst::MessageView::StateChanged(state_changed) => { - if state_changed.src().map(|s| s == pipeline).unwrap_or(false) { + if state_changed.src().map(|s| s == &pipeline).unwrap_or(false) { println!("State set to {:?}", state_changed.current()); } } diff --git a/tutorials/src/bin/basic-tutorial-6.rs b/tutorials/src/bin/basic-tutorial-6.rs index 2d6b7e46f..3ffffb4b6 100644 --- a/tutorials/src/bin/basic-tutorial-6.rs +++ b/tutorials/src/bin/basic-tutorial-6.rs @@ -141,7 +141,7 @@ fn tutorial_main() { MessageView::StateChanged(state_changed) => // We are only interested in state-changed messages from the pipeline { - if state_changed.src().map(|s| s == pipeline).unwrap_or(false) { + if state_changed.src().map(|s| s == &pipeline).unwrap_or(false) { let new_state = state_changed.current(); let old_state = state_changed.old(); diff --git a/tutorials/src/bin/playback-tutorial-1.rs b/tutorials/src/bin/playback-tutorial-1.rs index 468e8c14b..f728fac64 100644 --- a/tutorials/src/bin/playback-tutorial-1.rs +++ b/tutorials/src/bin/playback-tutorial-1.rs @@ -155,7 +155,7 @@ fn tutorial_main() -> Result<(), Error> { MessageView::StateChanged(state_changed) => { if state_changed .src() - .map(|s| s == playbin_clone) + .map(|s| s == &playbin_clone) .unwrap_or(false) && state_changed.current() == gst::State::Playing { diff --git a/tutorials/src/bin/playback-tutorial-2.rs b/tutorials/src/bin/playback-tutorial-2.rs index ba4fc2939..99030a8b9 100644 --- a/tutorials/src/bin/playback-tutorial-2.rs +++ b/tutorials/src/bin/playback-tutorial-2.rs @@ -160,7 +160,7 @@ fn tutorial_main() -> Result<(), Error> { MessageView::StateChanged(state_changed) => { if state_changed .src() - .map(|s| s == playbin_clone) + .map(|s| s == &playbin_clone) .unwrap_or(false) && state_changed.current() == gst::State::Playing {