gstreamer: Return some values by reference instead

Less refcounting.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1183>
This commit is contained in:
Sebastian Dröge 2023-01-04 19:48:41 +02:00
parent 32a608b76b
commit 1e793f3b65
13 changed files with 36 additions and 17 deletions

View file

@ -121,7 +121,7 @@ fn example_main() {
MessageView::StateChanged(state_changed) => MessageView::StateChanged(state_changed) =>
// We are only interested in state-changed messages from playbin // 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 && state_changed.current() == gst::State::Playing
{ {
// Generate a dot graph of the pipeline to GST_DEBUG_DUMP_DOT_DIR if defined // Generate a dot graph of the pipeline to GST_DEBUG_DUMP_DOT_DIR if defined

View file

@ -253,7 +253,7 @@ fn example_main() -> Result<(), Error> {
} }
MessageView::StateChanged(s) => { MessageView::StateChanged(s) => {
if let Some(element) = msg.src() { if let Some(element) = msg.src() {
if element == pipeline && s.current() == gst::State::Playing { if element == &pipeline && s.current() == gst::State::Playing {
eprintln!("PLAYING"); eprintln!("PLAYING");
gst::debug_bin_to_dot_file( gst::debug_bin_to_dot_file(
&pipeline, &pipeline,

View file

@ -179,7 +179,7 @@ fn example_main() -> Result<(), Error> {
} }
MessageView::StateChanged(s) => { MessageView::StateChanged(s) => {
if let Some(element) = msg.src() { if let Some(element) = msg.src() {
if element == pipeline && s.current() == gst::State::Playing { if element == &pipeline && s.current() == gst::State::Playing {
eprintln!("PLAYING"); eprintln!("PLAYING");
gst::debug_bin_to_dot_file( gst::debug_bin_to_dot_file(
&pipeline, &pipeline,

View file

@ -414,7 +414,7 @@ impl App {
let context_type = ctxt.context_type(); let context_type = ctxt.context_type();
if context_type == *gst_gl::GL_DISPLAY_CONTEXT_TYPE { if context_type == *gst_gl::GL_DISPLAY_CONTEXT_TYPE {
if let Some(el) = if let Some(el) =
msg.src().map(|s| s.downcast::<gst::Element>().unwrap()) msg.src().map(|s| s.downcast_ref::<gst::Element>().unwrap())
{ {
let context = gst::Context::new(context_type, true); let context = gst::Context::new(context_type, true);
context.set_gl_display(&gl_display); context.set_gl_display(&gl_display);
@ -423,7 +423,7 @@ impl App {
} }
if context_type == "gst.gl.app_context" { if context_type == "gst.gl.app_context" {
if let Some(el) = if let Some(el) =
msg.src().map(|s| s.downcast::<gst::Element>().unwrap()) msg.src().map(|s| s.downcast_ref::<gst::Element>().unwrap())
{ {
let mut context = gst::Context::new(context_type, true); let mut context = gst::Context::new(context_type, true);
{ {

View file

@ -166,8 +166,14 @@ impl Memory {
impl MemoryRef { impl MemoryRef {
#[doc(alias = "get_allocator")] #[doc(alias = "get_allocator")]
pub fn allocator(&self) -> Option<Allocator> { pub fn allocator(&self) -> Option<&Allocator> {
unsafe { from_glib_none(self.0.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")] #[doc(alias = "get_parent")]

View file

@ -17,8 +17,14 @@ mini_object_wrapper!(Message, MessageRef, ffi::GstMessage, || {
impl MessageRef { impl MessageRef {
#[doc(alias = "get_src")] #[doc(alias = "get_src")]
pub fn src(&self) -> Option<Object> { pub fn src(&self) -> Option<&Object> {
unsafe { from_glib_none((*self.as_ptr()).src) } 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")] #[doc(alias = "get_seqnum")]

View file

@ -239,11 +239,18 @@ impl ParamSpecArray {
} }
} }
pub fn element_spec(&self) -> Option<ParamSpec> { pub fn element_spec(&self) -> Option<&ParamSpec> {
unsafe { unsafe {
let ptr = self.as_ptr(); 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),
)
}
} }
} }

View file

@ -98,7 +98,7 @@ fn tutorial_main() {
break; break;
} }
MessageView::StateChanged(state_changed) => { 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!( println!(
"Pipeline state changed from {:?} to {:?}", "Pipeline state changed from {:?} to {:?}",
state_changed.old(), state_changed.old(),

View file

@ -128,7 +128,7 @@ fn handle_message(custom_data: &mut CustomData, msg: &gst::Message) {
MessageView::StateChanged(state_changed) => { MessageView::StateChanged(state_changed) => {
if state_changed if state_changed
.src() .src()
.map(|s| s == custom_data.playbin) .map(|s| s == &custom_data.playbin)
.unwrap_or(false) .unwrap_or(false)
{ {
let new_state = state_changed.current(); let new_state = state_changed.current();

View file

@ -360,7 +360,7 @@ mod tutorial5 {
// This is called when the pipeline changes states. We use it to // This is called when the pipeline changes states. We use it to
// keep track of the current state. // keep track of the current state.
gst::MessageView::StateChanged(state_changed) => { 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()); println!("State set to {:?}", state_changed.current());
} }
} }

View file

@ -141,7 +141,7 @@ fn tutorial_main() {
MessageView::StateChanged(state_changed) => MessageView::StateChanged(state_changed) =>
// We are only interested in state-changed messages from the pipeline // 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 new_state = state_changed.current();
let old_state = state_changed.old(); let old_state = state_changed.old();

View file

@ -155,7 +155,7 @@ fn tutorial_main() -> Result<(), Error> {
MessageView::StateChanged(state_changed) => { MessageView::StateChanged(state_changed) => {
if state_changed if state_changed
.src() .src()
.map(|s| s == playbin_clone) .map(|s| s == &playbin_clone)
.unwrap_or(false) .unwrap_or(false)
&& state_changed.current() == gst::State::Playing && state_changed.current() == gst::State::Playing
{ {

View file

@ -160,7 +160,7 @@ fn tutorial_main() -> Result<(), Error> {
MessageView::StateChanged(state_changed) => { MessageView::StateChanged(state_changed) => {
if state_changed if state_changed
.src() .src()
.map(|s| s == playbin_clone) .map(|s| s == &playbin_clone)
.unwrap_or(false) .unwrap_or(false)
&& state_changed.current() == gst::State::Playing && state_changed.current() == gst::State::Playing
{ {