Skip to main content

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::{UriClipAsset, ffi};
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            unsafe {
36                let mut error = std::ptr::null_mut();
37                let ret = {
38                    #[cfg(feature = "v1_16")]
39                    {
40                        ffi::ges_uri_clip_asset_finish(res, &mut error)
41                    }
42                    #[cfg(not(feature = "v1_16"))]
43                    {
44                        ffi::ges_asset_request_finish(res, &mut error) as *mut ffi::GESUriClipAsset
45                    }
46                };
47                let result = if error.is_null() {
48                    Ok(from_glib_full(ret))
49                } else {
50                    Err(from_glib_full(error))
51                };
52                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
53                    Box_::from_raw(user_data as *mut _);
54                let callback: P = callback.into_inner();
55                callback(result);
56            }
57        }
58        let callback = new_trampoline::<P>;
59        unsafe {
60            ffi::ges_uri_clip_asset_new(
61                uri.to_glib_none().0,
62                cancellable.map(|p| p.as_ref()).to_glib_none().0,
63                Some(callback),
64                Box_::into_raw(user_data) as *mut _,
65            );
66        }
67    }
68
69    pub fn new_future(
70        uri: &str,
71    ) -> Pin<Box_<dyn std::future::Future<Output = Result<UriClipAsset, glib::Error>> + 'static>>
72    {
73        skip_assert_initialized!();
74        let uri = String::from(uri);
75        Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
76            Self::new(&uri, Some(cancellable), move |res| {
77                send.resolve(res);
78            });
79        }))
80    }
81}