[RUST] how to thread fetch element instance

Sebastian Dröge sebastian at centricular.com
Fri Apr 9 10:41:33 UTC 2021


On Thu, 2021-04-08 at 21:54 -0500, kakakong wrote:
> fn sink_event(&self, element: &Self::Type, event: gst::Event) -> bool
> {
>     match event.view() {
>         let state = self.state.lock().unwrap();
>         EventView::StreamStart(_) => {
>             state.inspect_timer
>                 .schedule_repeating(chrono::Duration::seconds(1),
> move ||
> unsafe {
>                     // how to get self and element instance
>                 })
>                 .ignore();
>         }
>         _ => (),
>     }
> }
> 
> i need get element state and element instance's real-time data.
> i attempt to use raw pointer, but it not work;

In the code above you have `element` which is the element instance, and
`self` which should be the private/implementation state of it.

If you want to pass that to the closure, you could do something like

    let element_clone = element.clone();
   
state.inspect_timer.schedule_repeating(chrono::Duration::seconds(1),
move || {
        let imp = YourElementType::from_instance(&element_clone); //
This is the same as `self` outside the closure
        [...]
    })
    .ignore();


Note that this keeps the element alive until the closure is dropped.

See https://gtk-rs.org/gtk-rs/glib/subclass/types/trait.ObjectSubclassExt.html#tymethod.from_instance
 for details.

There are also various examples of this pattern
in https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs

-- 
Sebastian Dröge, Centricular Ltd · https://www.centricular.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/gstreamer-devel/attachments/20210409/431f5f19/attachment.htm>


More information about the gstreamer-devel mailing list