gstreamer_editing_services/
uri_clip_asset.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2use crate::{ffi, UriClipAsset};
3use glib::{prelude::*, translate::*};
4use std::{boxed::Box as Box_, pin::Pin};
5
6impl UriClipAsset {
7    #[doc(alias = "ges_uri_clip_asset_new")]
8    #[allow(clippy::new_ret_no_self)]
9    pub fn new<P: FnOnce(Result<UriClipAsset, glib::Error>) + 'static>(
10        uri: &str,
11        cancellable: Option<&impl IsA<gio::Cancellable>>,
12        callback: P,
13    ) {
14        assert_initialized_main_thread!();
15
16        let main_context = glib::MainContext::ref_thread_default();
17        let is_main_context_owner = main_context.is_owner();
18        let has_acquired_main_context = (!is_main_context_owner)
19            .then(|| main_context.acquire().ok())
20            .flatten();
21        assert!(
22            is_main_context_owner || has_acquired_main_context.is_some(),
23            "Async operations only allowed if the thread is owning the MainContext"
24        );
25
26        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
27            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
28        unsafe extern "C" fn new_trampoline<
29            P: FnOnce(Result<UriClipAsset, glib::Error>) + 'static,
30        >(
31            _source_object: *mut glib::gobject_ffi::GObject,
32            res: *mut gio::ffi::GAsyncResult,
33            user_data: glib::ffi::gpointer,
34        ) {
35            let mut error = std::ptr::null_mut();
36            let ret = {
37                #[cfg(feature = "v1_16")]
38                {
39                    ffi::ges_uri_clip_asset_finish(res, &mut error)
40                }
41                #[cfg(not(feature = "v1_16"))]
42                {
43                    ffi::ges_asset_request_finish(res, &mut error) as *mut ffi::GESUriClipAsset
44                }
45            };
46            let result = if error.is_null() {
47                Ok(from_glib_full(ret))
48            } else {
49                Err(from_glib_full(error))
50            };
51            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
52                Box_::from_raw(user_data as *mut _);
53            let callback: P = callback.into_inner();
54            callback(result);
55        }
56        let callback = new_trampoline::<P>;
57        unsafe {
58            ffi::ges_uri_clip_asset_new(
59                uri.to_glib_none().0,
60                cancellable.map(|p| p.as_ref()).to_glib_none().0,
61                Some(callback),
62                Box_::into_raw(user_data) as *mut _,
63            );
64        }
65    }
66
67    pub fn new_future(
68        uri: &str,
69    ) -> Pin<Box_<dyn std::future::Future<Output = Result<UriClipAsset, glib::Error>> + 'static>>
70    {
71        skip_assert_initialized!();
72        let uri = String::from(uri);
73        Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
74            Self::new(&uri, Some(cancellable), move |res| {
75                send.resolve(res);
76            });
77        }))
78    }
79}