Use IntoGlibPtr trait instead of to_glib_full() where appropriate to reduce unnecessary refcounting/copying

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1183>
This commit is contained in:
Sebastian Dröge 2023-01-04 18:11:27 +02:00
parent 5e852fa0ff
commit 8d30bcbf4b
26 changed files with 73 additions and 70 deletions

View file

@ -61,8 +61,8 @@ fn configure_encodebin(encodebin: &gst::Element) {
&gst::Caps::builder("video/x-matroska").build(),
)
.name("container")
.add_profile(&(video_profile))
.add_profile(&(audio_profile))
.add_profile(video_profile)
.add_profile(audio_profile)
.build();
// Finally, apply the EncodingProfile onto our encodebin element.

View file

@ -58,8 +58,8 @@ fn configure_pipeline(pipeline: &ges::Pipeline, output_name: &str) {
let container_profile =
gst_pbutils::EncodingContainerProfile::builder(&gst::Caps::builder("video/webm").build())
.name("container")
.add_profile(&video_profile)
.add_profile(&audio_profile)
.add_profile(video_profile)
.add_profile(audio_profile)
.build();
// Apply the EncodingProfile to the pipeline, and set it to render mode

View file

@ -736,7 +736,7 @@ unsafe extern "C" fn audio_decoder_getcaps<T: AudioDecoderImpl>(
.as_ref(),
)
})
.to_glib_full()
.into_glib_ptr()
}
unsafe extern "C" fn audio_decoder_sink_event<T: AudioDecoderImpl>(

View file

@ -669,7 +669,7 @@ unsafe extern "C" fn audio_encoder_getcaps<T: AudioEncoderImpl>(
.as_ref(),
)
})
.to_glib_full()
.into_glib_ptr()
}
unsafe extern "C" fn audio_encoder_sink_event<T: AudioEncoderImpl>(

View file

@ -941,7 +941,7 @@ unsafe extern "C" fn aggregator_create_new_pad<T: AggregatorImpl>(
.as_ref(),
)
})
.to_glib_full()
.into_glib_ptr()
}
unsafe extern "C" fn aggregator_update_src_caps<T: AggregatorImpl>(
@ -1074,5 +1074,6 @@ unsafe extern "C" fn aggregator_peek_next_sample<T: AggregatorImpl>(
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
gst::panic_to_error!(imp, None, { imp.peek_next_sample(&from_glib_borrow(pad)) }).to_glib_full()
gst::panic_to_error!(imp, None, { imp.peek_next_sample(&from_glib_borrow(pad)) })
.into_glib_ptr()
}

View file

@ -33,11 +33,11 @@ impl NetAddressMeta {
unsafe { from_glib_none(self.0.addr) }
}
pub fn set_addr<T: IsA<gio::SocketAddress>>(&mut self, addr: &T) {
pub fn set_addr(&mut self, addr: impl IsA<gio::SocketAddress>) {
#![allow(clippy::cast_ptr_alignment)]
unsafe {
glib::gobject_ffi::g_object_unref(self.0.addr as *mut _);
self.0.addr = addr.as_ref().to_glib_full();
self.0.addr = addr.upcast().into_glib_ptr();
}
}
}

View file

@ -154,7 +154,7 @@ impl<O: IsA<EncodingProfile>> EncodingProfileBuilderCommon for O {
// Split the trait as only the getter is public
trait EncodingProfileHasRestrictionSetter {
fn set_restriction(&self, restriction: Option<&gst::Caps>);
fn set_restriction(&self, restriction: Option<gst::Caps>);
}
pub trait EncodingProfileHasRestrictionGetter {
@ -167,12 +167,12 @@ macro_rules! declare_encoding_profile_has_restriction(
($name:ident) => {
impl EncodingProfileHasRestrictionSetter for $name {
// checker-ignore-item
fn set_restriction(&self, restriction: Option<&gst::Caps>) {
fn set_restriction(&self, restriction: Option<gst::Caps>) {
let profile: &EncodingProfile = glib::object::Cast::upcast_ref(self);
unsafe {
let restriction = match restriction {
Some(restriction) => restriction.to_glib_full(),
Some(restriction) => restriction.into_glib_ptr(),
None => gst::ffi::gst_caps_new_any(),
};
@ -305,11 +305,11 @@ impl EncodingContainerProfile {
}
// checker-ignore-item
fn add_profile<P: IsA<EncodingProfile>>(&self, profile: &P) {
fn add_profile(&self, profile: impl IsA<EncodingProfile>) {
unsafe {
let res = ffi::gst_encoding_container_profile_add_profile(
self.to_glib_none().0,
profile.as_ref().to_glib_full(),
profile.upcast().into_glib_ptr(),
);
// Can't possibly fail unless we pass random pointers
assert_ne!(res, glib::ffi::GFALSE);
@ -588,7 +588,7 @@ impl<'a> EncodingContainerProfileBuilder<'a> {
);
for profile in self.profiles {
container_profile.add_profile(&profile);
container_profile.add_profile(profile);
}
set_common_fields(&container_profile, self.base);
@ -597,8 +597,8 @@ impl<'a> EncodingContainerProfileBuilder<'a> {
}
#[doc(alias = "gst_encoding_container_profile_add_profile")]
pub fn add_profile<P: IsA<EncodingProfile>>(mut self, profile: &P) -> Self {
self.profiles.push(profile.as_ref().clone());
pub fn add_profile(mut self, profile: impl IsA<EncodingProfile>) -> Self {
self.profiles.push(profile.upcast());
self
}
}
@ -666,7 +666,7 @@ mod tests {
let restriction = gst_audio::AudioCapsBuilder::new()
.format(gst_audio::AudioFormat::S32be)
.build();
audio_profile.set_restriction(Some(&restriction));
audio_profile.set_restriction(Some(restriction.clone()));
assert_eq!(audio_profile.restriction().unwrap(), restriction);
}
@ -714,7 +714,7 @@ mod tests {
let restriction = gst_video::VideoCapsBuilder::new()
.format(gst_video::VideoFormat::Nv12)
.build();
video_profile.set_restriction(Some(&restriction));
video_profile.set_restriction(Some(restriction.clone()));
assert_eq!(video_profile.restriction().unwrap(), restriction);
}
@ -743,8 +743,8 @@ mod tests {
.presence(PRESENCE)
.allow_dynamic_output(ALLOW_DYNAMIC_OUTPUT)
.enabled(ENABLED)
.add_profile(&audio_profile)
.add_profile(&video_profile)
.add_profile(audio_profile.clone())
.add_profile(video_profile.clone())
.build();
assert_eq!(profile.name().unwrap(), CONTAINER_PROFILE_NAME);

View file

@ -205,7 +205,7 @@ unsafe extern "C" fn rtp_base_payload_get_caps<T: RTPBasePayloadImpl>(
.as_ref(),
)
})
.to_glib_full()
.into_glib_ptr()
}
unsafe extern "C" fn rtp_base_payload_set_caps<T: RTPBasePayloadImpl>(

View file

@ -6,13 +6,13 @@ use crate::RTSPMedia;
pub trait RTSPMediaExtManual: 'static {
#[doc(alias = "gst_rtsp_media_take_pipeline")]
fn take_pipeline<P: IsA<gst::Pipeline>>(&self, pipeline: &P);
fn take_pipeline(&self, pipeline: impl IsA<gst::Pipeline>);
}
impl<O: IsA<RTSPMedia>> RTSPMediaExtManual for O {
fn take_pipeline<P: IsA<gst::Pipeline>>(&self, pipeline: &P) {
fn take_pipeline(&self, pipeline: impl IsA<gst::Pipeline>) {
unsafe {
let pipeline = pipeline.as_ref().to_glib_full();
let pipeline = pipeline.upcast().into_glib_ptr();
// See https://gitlab.freedesktop.org/gstreamer/gst-rtsp-server/merge_requests/109
glib::gobject_ffi::g_object_force_floating(pipeline as *mut _);
ffi::gst_rtsp_media_take_pipeline(self.as_ref().to_glib_none().0, pipeline);

View file

@ -839,7 +839,7 @@ unsafe extern "C" fn client_make_path_from_uri<T: RTSPClientImpl>(
let imp = instance.imp();
imp.make_path_from_uri(&from_glib_borrow(url))
.to_glib_full()
.into_glib_ptr()
}
unsafe extern "C" fn client_closed<T: RTSPClientImpl>(ptr: *mut ffi::GstRTSPClient) {
@ -1000,7 +1000,7 @@ unsafe extern "C" fn client_check_requirements<T: RTSPClientImpl>(
let imp = instance.imp();
imp.check_requirements(&from_glib_borrow(ctx), Vec::from_glib_none(arr).as_slice())
.to_glib_full()
.into_glib_ptr()
}
unsafe extern "C" fn client_pre_options_request<T: RTSPClientImpl>(

View file

@ -553,7 +553,7 @@ unsafe extern "C" fn media_create_rtpbin<T: RTSPMediaImpl>(
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let res: *mut gst::ffi::GstElement = imp.create_rtpbin().to_glib_full();
let res: *mut gst::ffi::GstElement = imp.create_rtpbin().into_glib_ptr();
if !res.is_null() {
glib::gobject_ffi::g_object_force_floating(res as *mut _);

View file

@ -203,7 +203,7 @@ unsafe extern "C" fn factory_gen_key<T: RTSPMediaFactoryImpl>(
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
imp.gen_key(&from_glib_borrow(url)).to_glib_full()
imp.gen_key(&from_glib_borrow(url)).into_glib_ptr()
}
unsafe extern "C" fn factory_create_element<T: RTSPMediaFactoryImpl>(
@ -213,7 +213,7 @@ unsafe extern "C" fn factory_create_element<T: RTSPMediaFactoryImpl>(
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let element = imp.create_element(&from_glib_borrow(url)).to_glib_full();
let element = imp.create_element(&from_glib_borrow(url)).into_glib_ptr();
glib::gobject_ffi::g_object_force_floating(element as *mut _);
element
}
@ -225,7 +225,7 @@ unsafe extern "C" fn factory_construct<T: RTSPMediaFactoryImpl>(
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
imp.construct(&from_glib_borrow(url)).to_glib_full()
imp.construct(&from_glib_borrow(url)).into_glib_ptr()
}
unsafe extern "C" fn factory_create_pipeline<T: RTSPMediaFactoryImpl>(
@ -240,8 +240,9 @@ unsafe extern "C" fn factory_create_pipeline<T: RTSPMediaFactoryImpl>(
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
let pipeline: *mut gst::ffi::GstPipeline =
imp.create_pipeline(&from_glib_borrow(media)).to_glib_full();
let pipeline: *mut gst::ffi::GstPipeline = imp
.create_pipeline(&from_glib_borrow(media))
.into_glib_ptr();
// FIXME We somehow need to ensure the pipeline actually stays alive...
glib::gobject_ffi::g_object_set_qdata_full(

View file

@ -49,5 +49,5 @@ unsafe extern "C" fn mount_points_make_path<T: RTSPMountPointsImpl>(
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
imp.make_path(&from_glib_borrow(url)).to_glib_full()
imp.make_path(&from_glib_borrow(url)).into_glib_ptr()
}

View file

@ -64,7 +64,7 @@ unsafe extern "C" fn server_create_client<T: RTSPServerImpl>(
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
imp.create_client().to_glib_full()
imp.create_client().into_glib_ptr()
}
unsafe extern "C" fn server_client_connected<T: RTSPServerImpl>(

View file

@ -806,7 +806,7 @@ unsafe extern "C" fn video_decoder_getcaps<T: VideoDecoderImpl>(
.as_ref(),
)
})
.to_glib_full()
.into_glib_ptr()
}
unsafe extern "C" fn video_decoder_sink_event<T: VideoDecoderImpl>(

View file

@ -661,7 +661,7 @@ unsafe extern "C" fn video_encoder_getcaps<T: VideoEncoderImpl>(
.as_ref(),
)
})
.to_glib_full()
.into_glib_ptr()
}
unsafe extern "C" fn video_encoder_sink_event<T: VideoEncoderImpl>(

View file

@ -1,10 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use std::mem;
use glib::{
translate::{from_glib, from_glib_full, IntoGlib, ToGlibPtr},
ToSendValue,
};
use glib::{translate::*, ToSendValue};
use gst::EventType;
#[cfg(any(feature = "v1_22", feature = "dox"))]
@ -426,7 +423,7 @@ nav_event_builder!(
modifier_state: s.modifier_state,
},
};
gst::ffi::gst_event_new_navigation(event.structure().to_glib_full())
gst::ffi::gst_event_new_navigation(event.structure().into_glib_ptr())
}
);
@ -487,7 +484,7 @@ nav_event_builder!(
modifier_state: s.modifier_state,
},
};
gst::ffi::gst_event_new_navigation(event.structure().to_glib_full())
gst::ffi::gst_event_new_navigation(event.structure().into_glib_ptr())
}
);
@ -532,7 +529,7 @@ impl<'a> CommandEventBuilder<'a> {
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_22")))]
modifier_state: s.modifier_state,
};
gst::ffi::gst_event_new_navigation(event.structure().to_glib_full())
gst::ffi::gst_event_new_navigation(event.structure().into_glib_ptr())
});
}
@ -573,7 +570,7 @@ nav_event_builder!(
modifier_state: s.modifier_state,
},
};
gst::ffi::gst_event_new_navigation(event.structure().to_glib_full())
gst::ffi::gst_event_new_navigation(event.structure().into_glib_ptr())
}
);
@ -599,7 +596,7 @@ nav_event_builder!(
modifier_state: s.modifier_state,
},
};
gst::ffi::gst_event_new_navigation(event.structure().to_glib_full())
gst::ffi::gst_event_new_navigation(event.structure().into_glib_ptr())
}
);

View file

@ -325,13 +325,13 @@ macro_rules! generic_impl {
unsafe { from_glib_none(self.inner.config.latest_daily_jam) }
}
pub fn set_latest_daily_jam(&mut self, latest_daily_jam: Option<&glib::DateTime>) {
pub fn set_latest_daily_jam(&mut self, latest_daily_jam: Option<glib::DateTime>) {
unsafe {
if !self.inner.config.latest_daily_jam.is_null() {
glib::ffi::g_date_time_unref(self.inner.config.latest_daily_jam);
}
self.inner.config.latest_daily_jam = latest_daily_jam.to_glib_full()
self.inner.config.latest_daily_jam = latest_daily_jam.into_glib_ptr();
}
}
}

View file

@ -1,21 +1,24 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use glib::{translate::*, IsA};
use glib::{translate::*, Cast, IsA};
use crate::Allocator;
impl Allocator {
#[doc(alias = "gst_allocator_register")]
pub fn register(name: &str, allocator: &impl IsA<Allocator>) {
pub fn register(name: &str, allocator: impl IsA<Allocator>) {
skip_assert_initialized!();
unsafe {
// See https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3364
if crate::version() < (1, 20, 5, 0) {
ffi::gst_allocator_register(name.to_glib_full(), allocator.as_ref().to_glib_full());
ffi::gst_allocator_register(
name.to_glib_full(),
allocator.upcast().into_glib_ptr(),
);
} else {
ffi::gst_allocator_register(
name.to_glib_none().0,
allocator.as_ref().to_glib_full(),
allocator.upcast().into_glib_ptr(),
);
}
}

View file

@ -10,7 +10,6 @@
// Re-exported for the subclass gst_plugin_define! macro
pub use ffi;
pub use glib;
use glib::translate::{from_glib, from_glib_full};
pub use paste;
#[doc(hidden)]
@ -256,6 +255,8 @@ use std::ptr;
#[doc(alias = "gst_init_check")]
pub fn init() -> Result<(), glib::Error> {
unsafe {
use glib::translate::*;
let mut error = ptr::null_mut();
if from_glib(ffi::gst_init_check(
ptr::null_mut(),

View file

@ -8,7 +8,7 @@ use std::{
ptr, slice,
};
use glib::translate::{from_glib, from_glib_full, from_glib_none, IntoGlibPtr, ToGlibPtr};
use glib::translate::*;
use crate::{AllocationParams, Allocator, MemoryFlags};

View file

@ -104,20 +104,23 @@ pub struct StreamCollectionBuilder(StreamCollection);
impl StreamCollectionBuilder {
#[doc(alias = "gst_stream_collection_add_stream")]
pub fn stream(self, stream: &Stream) -> Self {
pub fn stream(self, stream: Stream) -> Self {
unsafe {
ffi::gst_stream_collection_add_stream((self.0).to_glib_none().0, stream.to_glib_full());
ffi::gst_stream_collection_add_stream(
(self.0).to_glib_none().0,
stream.into_glib_ptr(),
);
}
self
}
pub fn streams(self, streams: &[impl AsRef<Stream>]) -> Self {
for stream in streams {
pub fn streams(self, streams: impl IntoIterator<Item = Stream>) -> Self {
for stream in streams.into_iter() {
unsafe {
ffi::gst_stream_collection_add_stream(
(self.0).to_glib_none().0,
stream.as_ref().to_glib_full(),
stream.into_glib_ptr(),
);
}
}

View file

@ -167,7 +167,7 @@ mod tests {
const TEST_ALLOCATOR_NAME: &str = "TestAllocator";
let allocator = TestAllocator::default();
Allocator::register(TEST_ALLOCATOR_NAME, &allocator);
Allocator::register(TEST_ALLOCATOR_NAME, allocator);
let allocator = Allocator::find(Some(TEST_ALLOCATOR_NAME));

View file

@ -132,7 +132,7 @@ unsafe extern "C" fn child_proxy_get_child_by_name<T: ChildProxyImpl>(
let imp = instance.imp();
imp.child_by_name(&glib::GString::from_glib_borrow(name))
.to_glib_full()
.into_glib_ptr()
}
unsafe extern "C" fn child_proxy_get_child_by_index<T: ChildProxyImpl>(
@ -142,7 +142,7 @@ unsafe extern "C" fn child_proxy_get_child_by_index<T: ChildProxyImpl>(
let instance = &*(child_proxy as *mut T::Instance);
let imp = instance.imp();
imp.child_by_index(index).to_glib_full()
imp.child_by_index(index).into_glib_ptr()
}
unsafe extern "C" fn child_proxy_get_children_count<T: ChildProxyImpl>(

View file

@ -97,13 +97,10 @@ unsafe extern "C" fn device_create_element<T: DeviceImpl>(
Ok(element) => {
// The reference we're going to return, the initial reference is going to
// be dropped here now
let element_ptr = element.to_glib_full();
drop(element);
let element = element.into_glib_ptr();
// See https://gitlab.freedesktop.org/gstreamer/gstreamer/issues/444
glib::gobject_ffi::g_object_force_floating(
element_ptr as *mut glib::gobject_ffi::GObject,
);
element_ptr
glib::gobject_ffi::g_object_force_floating(element as *mut glib::gobject_ffi::GObject);
element
}
Err(err) => {
err.log_with_imp(imp);

View file

@ -538,7 +538,7 @@ unsafe extern "C" fn element_provide_clock<T: ElementImpl>(
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
panic_to_error!(imp, None, { imp.provide_clock() }).to_glib_full()
panic_to_error!(imp, None, { imp.provide_clock() }).into_glib_ptr()
}
unsafe extern "C" fn element_post_message<T: ElementImpl>(