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    /// message);
8    ///  }
9    ///
10    ///  gst_object_unref (mfs);
11    /// }
12    ///
13    /// // The request:
14    /// ges_uri_clip_asset_new (uri, (GAsyncReadyCallback) filesource_asset_loaded_cb, user_data);
15    /// ]|
16    /// ## `uri`
17    /// The URI of the file for which to create a [`UriClipAsset`][crate::UriClipAsset]
18    /// ## `cancellable`
19    /// optional `GCancellable` object, [`None`] to ignore.
20    /// ## `callback`
21    /// a `GAsyncReadyCallback` to call when the initialization is finished
22    #[doc(alias = "ges_uri_clip_asset_new")]
23    #[allow(clippy::new_ret_no_self)]
24    pub fn new<P: FnOnce(Result<UriClipAsset, glib::Error>) + 'static>(
25        uri: &str,
26        cancellable: Option<&impl IsA<gio::Cancellable>>,
27        callback: P,
28    ) {
29        assert_initialized_main_thread!();
30
31        let main_context = glib::MainContext::ref_thread_default();
32        let is_main_context_owner = main_context.is_owner();
33        let has_acquired_main_context = (!is_main_context_owner)
34            .then(|| main_context.acquire().ok())
35            .flatten();
36        assert!(
37            is_main_context_owner || has_acquired_main_context.is_some(),
38            "Async operations only allowed if the thread is owning the MainContext"
39        );
40
41        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
42            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
43        unsafe extern "C" fn new_trampoline<
44            P: FnOnce(Result<UriClipAsset, glib::Error>) + 'static,
45        >(
46            _source_object: *mut glib::gobject_ffi::GObject,
47            res: *mut gio::ffi::GAsyncResult,
48            user_data: glib::ffi::gpointer,
49        ) {
50            unsafe {
51                let mut error = std::ptr::null_mut();
52                let ret = {
53                    #[cfg(feature = "v1_16")]
54                    {
55                        ffi::ges_uri_clip_asset_finish(res, &mut error)
56                    }
57                    #[cfg(not(feature = "v1_16"))]
58                    {
59                        ffi::ges_asset_request_finish(res, &mut error) as *mut ffi::GESUriClipAsset
60                    }
61                };
62                let result = if error.is_null() {
63                    Ok(from_glib_full(ret))
64                } else {
65                    Err(from_glib_full(error))
66                };
67                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
68                    Box_::from_raw(user_data as *mut _);
69                let callback: P = callback.into_inner();
70                callback(result);
71            }
72        }
73        let callback = new_trampoline::<P>;
74        unsafe {
75            ffi::ges_uri_clip_asset_new(
76                uri.to_glib_none().0,
77                cancellable.map(|p| p.as_ref()).to_glib_none().0,
78                Some(callback),
79                Box_::into_raw(user_data) as *mut _,
80            );
81        }
82    }
83
84    pub fn new_future(
85        uri: &str,
86    ) -> Pin<Box_<dyn std::future::Future<Output = Result<UriClipAsset, glib::Error>> + 'static>>
87    {
88        skip_assert_initialized!();
89        let uri = String::from(uri);
90        Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
91            Self::new(&uri, Some(cancellable), move |res| {
92                send.resolve(res);
93            });
94        }))
95    }
96}