From fb56bbda6e6f53d1ca5e5b52dc0e3ca0a2b6a1ab Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Mon, 17 Jan 2022 18:33:13 +0100 Subject: [PATCH] gstreamer: Split parsers for owned and borrowed views When we have a view like `Caps<&EventRef>`, we can return a `CapsRef` borrowing from the wrapped `EventRef`. This way, the `CapsRef` we return can outlive the view itself. This is in contrast to a `Caps` view which owns the `Event`. Here, the `CapsRef` we return cannot outlive the view. gstreamer-rs 0.18 consolidated code and treated everything like the latter case. Fix this by duplicating the accessors for each case. Fixes: https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/issues/367 --- gstreamer/src/event.rs | 99 ++++++++++++++++++++++++++++------ gstreamer/src/query.rs | 118 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 195 insertions(+), 22 deletions(-) diff --git a/gstreamer/src/event.rs b/gstreamer/src/event.rs index d2a565c88..68c48c59b 100644 --- a/gstreamer/src/event.rs +++ b/gstreamer/src/event.rs @@ -470,20 +470,33 @@ impl StreamStart { assert_initialized_main_thread!(); StreamStartBuilder::new(stream_id) } -} -impl StreamStart { #[doc(alias = "get_stream_id")] #[doc(alias = "gst_event_parse_stream_start")] pub fn stream_id(&self) -> &str { unsafe { let mut stream_id = ptr::null(); - ffi::gst_event_parse_stream_start(self.0.as_ptr(), &mut stream_id); + ffi::gst_event_parse_stream_start(AsPtr::as_ptr(&self.0), &mut stream_id); CStr::from_ptr(stream_id).to_str().unwrap() } } +} +impl<'a> StreamStart<&'a EventRef> { + #[doc(alias = "get_stream_id")] + #[doc(alias = "gst_event_parse_stream_start")] + pub fn stream_id(&self) -> &'a str { + unsafe { + let mut stream_id = ptr::null(); + + ffi::gst_event_parse_stream_start(AsPtr::as_ptr(&self.0), &mut stream_id); + CStr::from_ptr(stream_id).to_str().unwrap() + } + } +} + +impl StreamStart { #[doc(alias = "get_stream_flags")] #[doc(alias = "gst_event_parse_stream_flags")] pub fn stream_flags(&self) -> crate::StreamFlags { @@ -539,16 +552,33 @@ impl Caps { assert_initialized_main_thread!(); CapsBuilder::new(caps) } -} -impl Caps { #[doc(alias = "get_caps")] #[doc(alias = "gst_event_parse_caps")] pub fn caps(&self) -> &crate::CapsRef { unsafe { let mut caps = ptr::null_mut(); - ffi::gst_event_parse_caps(self.0.as_ptr(), &mut caps); + ffi::gst_event_parse_caps(AsPtr::as_ptr(&self.0), &mut caps); + crate::CapsRef::from_ptr(caps) + } + } + + #[doc(alias = "get_caps_owned")] + #[doc(alias = "gst_event_parse_caps")] + pub fn caps_owned(&self) -> crate::Caps { + unsafe { from_glib_none(self.caps().as_ptr()) } + } +} + +impl<'a> Caps<&'a EventRef> { + #[doc(alias = "get_caps")] + #[doc(alias = "gst_event_parse_caps")] + pub fn caps(&self) -> &'a crate::CapsRef { + unsafe { + let mut caps = ptr::null_mut(); + + ffi::gst_event_parse_caps(AsPtr::as_ptr(&self.0), &mut caps); crate::CapsRef::from_ptr(caps) } } @@ -575,16 +605,27 @@ impl Segment { assert_initialized_main_thread!(); SegmentBuilder::new(segment.as_ref()) } -} -impl Segment { #[doc(alias = "get_segment")] #[doc(alias = "gst_event_parse_segment")] pub fn segment(&self) -> &crate::Segment { unsafe { let mut segment = ptr::null(); - ffi::gst_event_parse_segment(self.0.as_ptr(), &mut segment); + ffi::gst_event_parse_segment(AsPtr::as_ptr(&self.0), &mut segment); + &*(segment as *mut ffi::GstSegment as *mut crate::Segment) + } + } +} + +impl<'a> Segment<&'a EventRef> { + #[doc(alias = "get_segment")] + #[doc(alias = "gst_event_parse_segment")] + pub fn segment(&self) -> &'a crate::Segment { + unsafe { + let mut segment = ptr::null(); + + ffi::gst_event_parse_segment(AsPtr::as_ptr(&self.0), &mut segment); &*(segment as *mut ffi::GstSegment as *mut crate::Segment) } } @@ -643,16 +684,33 @@ impl Tag { assert_initialized_main_thread!(); TagBuilder::new(tags) } -} -impl Tag { #[doc(alias = "get_tag")] #[doc(alias = "gst_event_parse_tag")] pub fn tag(&self) -> &crate::TagListRef { unsafe { let mut tags = ptr::null_mut(); - ffi::gst_event_parse_tag(self.0.as_ptr(), &mut tags); + ffi::gst_event_parse_tag(AsPtr::as_ptr(&self.0), &mut tags); + crate::TagListRef::from_ptr(tags) + } + } + + #[doc(alias = "get_tag_owned")] + #[doc(alias = "gst_event_parse_tag")] + pub fn tag_owned(&self) -> crate::TagList { + unsafe { from_glib_none(self.tag().as_ptr()) } + } +} + +impl<'a> Tag<&'a EventRef> { + #[doc(alias = "get_tag")] + #[doc(alias = "gst_event_parse_tag")] + pub fn tag(&self) -> &'a crate::TagListRef { + unsafe { + let mut tags = ptr::null_mut(); + + ffi::gst_event_parse_tag(AsPtr::as_ptr(&self.0), &mut tags); crate::TagListRef::from_ptr(tags) } } @@ -1287,15 +1345,26 @@ impl TocSelect { assert_initialized_main_thread!(); TocSelectBuilder::new(uid) } -} -impl TocSelect { #[doc(alias = "get_uid")] pub fn uid(&self) -> &str { unsafe { let mut uid = ptr::null_mut(); - ffi::gst_event_parse_toc_select(self.0.as_ptr(), &mut uid); + ffi::gst_event_parse_toc_select(AsPtr::as_ptr(&self.0), &mut uid); + + CStr::from_ptr(uid).to_str().unwrap() + } + } +} + +impl<'a> TocSelect<&'a EventRef> { + #[doc(alias = "get_uid")] + pub fn uid(&self) -> &'a str { + unsafe { + let mut uid = ptr::null_mut(); + + ffi::gst_event_parse_toc_select(AsPtr::as_ptr(&self.0), &mut uid); CStr::from_ptr(uid).to_str().unwrap() } diff --git a/gstreamer/src/query.rs b/gstreamer/src/query.rs index 12b975134..097ca8926 100644 --- a/gstreamer/src/query.rs +++ b/gstreamer/src/query.rs @@ -1318,15 +1318,13 @@ impl AcceptCaps { ))) } } -} -impl AcceptCaps { #[doc(alias = "get_caps")] #[doc(alias = "gst_query_parse_accept_caps")] pub fn caps(&self) -> &crate::CapsRef { unsafe { let mut caps = ptr::null_mut(); - ffi::gst_query_parse_accept_caps(self.0.as_ptr(), &mut caps); + ffi::gst_query_parse_accept_caps(AsPtr::as_ptr(&self.0), &mut caps); crate::CapsRef::from_ptr(caps) } } @@ -1336,7 +1334,45 @@ impl AcceptCaps { pub fn caps_owned(&self) -> crate::Caps { unsafe { from_glib_none(self.caps().as_ptr()) } } +} +impl<'a> AcceptCaps<&'a QueryRef> { + #[doc(alias = "get_caps")] + #[doc(alias = "gst_query_parse_accept_caps")] + pub fn caps(&self) -> &'a crate::CapsRef { + unsafe { + let mut caps = ptr::null_mut(); + ffi::gst_query_parse_accept_caps(AsPtr::as_ptr(&self.0), &mut caps); + crate::CapsRef::from_ptr(caps) + } + } + + #[doc(alias = "get_caps_owned")] + #[doc(alias = "gst_query_parse_accept_caps")] + pub fn caps_owned(&self) -> crate::Caps { + unsafe { from_glib_none(self.caps().as_ptr()) } + } +} + +impl<'a> AcceptCaps<&'a mut QueryRef> { + #[doc(alias = "get_caps")] + #[doc(alias = "gst_query_parse_accept_caps")] + pub fn caps(&self) -> &'a crate::CapsRef { + unsafe { + let mut caps = ptr::null_mut(); + ffi::gst_query_parse_accept_caps(AsPtr::as_ptr(&self.0), &mut caps); + crate::CapsRef::from_ptr(caps) + } + } + + #[doc(alias = "get_caps_owned")] + #[doc(alias = "gst_query_parse_accept_caps")] + pub fn caps_owned(&self) -> crate::Caps { + unsafe { from_glib_none(self.caps().as_ptr()) } + } +} + +impl AcceptCaps { #[doc(alias = "get_result")] #[doc(alias = "gst_query_parse_accept_caps_result")] pub fn result(&self) -> bool { @@ -1447,15 +1483,13 @@ impl Context { ))) } } -} -impl Context { #[doc(alias = "get_context")] #[doc(alias = "gst_query_parse_context")] pub fn context(&self) -> Option<&crate::ContextRef> { unsafe { let mut context = ptr::null_mut(); - ffi::gst_query_parse_context(self.0.as_ptr(), &mut context); + ffi::gst_query_parse_context(AsPtr::as_ptr(&self.0), &mut context); if context.is_null() { None } else { @@ -1478,7 +1512,77 @@ impl Context { pub fn context_type(&self) -> &str { unsafe { let mut context_type = ptr::null(); - ffi::gst_query_parse_context_type(self.0.as_ptr(), &mut context_type); + ffi::gst_query_parse_context_type(AsPtr::as_ptr(&self.0), &mut context_type); + CStr::from_ptr(context_type).to_str().unwrap() + } + } +} + +impl<'a> Context<&'a QueryRef> { + #[doc(alias = "get_context")] + #[doc(alias = "gst_query_parse_context")] + pub fn context(&self) -> Option<&'a crate::ContextRef> { + unsafe { + let mut context = ptr::null_mut(); + ffi::gst_query_parse_context(AsPtr::as_ptr(&self.0), &mut context); + if context.is_null() { + None + } else { + Some(crate::ContextRef::from_ptr(context)) + } + } + } + + #[doc(alias = "get_context_owned")] + #[doc(alias = "gst_query_parse_context")] + pub fn context_owned(&self) -> Option { + unsafe { + self.context() + .map(|context| from_glib_none(context.as_ptr())) + } + } + + #[doc(alias = "get_context_type")] + #[doc(alias = "gst_query_parse_context_type")] + pub fn context_type(&self) -> &'a str { + unsafe { + let mut context_type = ptr::null(); + ffi::gst_query_parse_context_type(AsPtr::as_ptr(&self.0), &mut context_type); + CStr::from_ptr(context_type).to_str().unwrap() + } + } +} + +impl<'a> Context<&'a mut QueryRef> { + #[doc(alias = "get_context")] + #[doc(alias = "gst_query_parse_context")] + pub fn context(&self) -> Option<&'a crate::ContextRef> { + unsafe { + let mut context = ptr::null_mut(); + ffi::gst_query_parse_context(AsPtr::as_ptr(&self.0), &mut context); + if context.is_null() { + None + } else { + Some(crate::ContextRef::from_ptr(context)) + } + } + } + + #[doc(alias = "get_context_owned")] + #[doc(alias = "gst_query_parse_context")] + pub fn context_owned(&self) -> Option { + unsafe { + self.context() + .map(|context| from_glib_none(context.as_ptr())) + } + } + + #[doc(alias = "get_context_type")] + #[doc(alias = "gst_query_parse_context_type")] + pub fn context_type(&self) -> &'a str { + unsafe { + let mut context_type = ptr::null(); + ffi::gst_query_parse_context_type(AsPtr::as_ptr(&self.0), &mut context_type); CStr::from_ptr(context_type).to_str().unwrap() } }