gstreamer_editing_services/auto/
asset.rs1use crate::{Extractable, MetaContainer, ffi};
7use glib::{
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
10 translate::*,
11};
12use std::{boxed::Box as Box_, pin::Pin};
13
14glib::wrapper! {
15 #[doc(alias = "GESAsset")]
16 pub struct Asset(Object<ffi::GESAsset, ffi::GESAssetClass>) @implements MetaContainer;
17
18 match fn {
19 type_ => || ffi::ges_asset_get_type(),
20 }
21}
22
23impl Asset {
24 pub const NONE: Option<&'static Asset> = None;
25
26 #[doc(alias = "ges_asset_needs_reload")]
27 #[doc(alias = "needs_reload")]
28 pub fn needs_reload_with_type(extractable_type: glib::types::Type, id: Option<&str>) -> bool {
29 assert_initialized_main_thread!();
30 unsafe {
31 from_glib(ffi::ges_asset_needs_reload(
32 extractable_type.into_glib(),
33 id.to_glib_none().0,
34 ))
35 }
36 }
37
38 #[doc(alias = "ges_asset_request")]
39 #[doc(alias = "request")]
40 pub fn request_with_type(
41 extractable_type: glib::types::Type,
42 id: Option<&str>,
43 ) -> Result<Option<Asset>, glib::Error> {
44 assert_initialized_main_thread!();
45 unsafe {
46 let mut error = std::ptr::null_mut();
47 let ret = ffi::ges_asset_request(
48 extractable_type.into_glib(),
49 id.to_glib_none().0,
50 &mut error,
51 );
52 if error.is_null() {
53 Ok(from_glib_full(ret))
54 } else {
55 Err(from_glib_full(error))
56 }
57 }
58 }
59
60 #[doc(alias = "ges_asset_request_async")]
61 #[doc(alias = "request_async")]
62 pub fn request_async_with_type<P: FnOnce(Result<Asset, glib::Error>) + 'static>(
63 extractable_type: glib::types::Type,
64 id: Option<&str>,
65 cancellable: Option<&impl IsA<gio::Cancellable>>,
66 callback: P,
67 ) {
68 assert_initialized_main_thread!();
69
70 let main_context = glib::MainContext::ref_thread_default();
71 let is_main_context_owner = main_context.is_owner();
72 let has_acquired_main_context = (!is_main_context_owner)
73 .then(|| main_context.acquire().ok())
74 .flatten();
75 assert!(
76 is_main_context_owner || has_acquired_main_context.is_some(),
77 "Async operations only allowed if the thread is owning the MainContext"
78 );
79
80 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
81 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
82 unsafe extern "C" fn request_async_with_type_trampoline<
83 P: FnOnce(Result<Asset, glib::Error>) + 'static,
84 >(
85 _source_object: *mut glib::gobject_ffi::GObject,
86 res: *mut gio::ffi::GAsyncResult,
87 user_data: glib::ffi::gpointer,
88 ) {
89 unsafe {
90 let mut error = std::ptr::null_mut();
91 let ret = ffi::ges_asset_request_finish(res, &mut error);
92 let result = if error.is_null() {
93 Ok(from_glib_full(ret))
94 } else {
95 Err(from_glib_full(error))
96 };
97 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
98 Box_::from_raw(user_data as *mut _);
99 let callback: P = callback.into_inner();
100 callback(result);
101 }
102 }
103 let callback = request_async_with_type_trampoline::<P>;
104 unsafe {
105 ffi::ges_asset_request_async(
106 extractable_type.into_glib(),
107 id.to_glib_none().0,
108 cancellable.map(|p| p.as_ref()).to_glib_none().0,
109 Some(callback),
110 Box_::into_raw(user_data) as *mut _,
111 );
112 }
113 }
114
115 pub fn request_async_with_type_future(
116 extractable_type: glib::types::Type,
117 id: Option<&str>,
118 ) -> Pin<Box_<dyn std::future::Future<Output = Result<Asset, glib::Error>> + 'static>> {
119 skip_assert_initialized!();
120 let id = id.map(ToOwned::to_owned);
121 Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
122 Self::request_async_with_type(
123 extractable_type,
124 id.as_ref().map(::std::borrow::Borrow::borrow),
125 Some(cancellable),
126 move |res| {
127 send.resolve(res);
128 },
129 );
130 }))
131 }
132}
133
134unsafe impl Send for Asset {}
135unsafe impl Sync for Asset {}
136
137pub trait AssetExt: IsA<Asset> + 'static {
138 #[doc(alias = "ges_asset_extract")]
139 fn extract(&self) -> Result<Extractable, glib::Error> {
140 unsafe {
141 let mut error = std::ptr::null_mut();
142 let ret = ffi::ges_asset_extract(self.as_ref().to_glib_none().0, &mut error);
143 if error.is_null() {
144 Ok(from_glib_none(ret))
145 } else {
146 Err(from_glib_full(error))
147 }
148 }
149 }
150
151 #[doc(alias = "ges_asset_get_error")]
152 #[doc(alias = "get_error")]
153 fn error(&self) -> Option<glib::Error> {
154 unsafe { from_glib_none(ffi::ges_asset_get_error(self.as_ref().to_glib_none().0)) }
155 }
156
157 #[doc(alias = "ges_asset_get_extractable_type")]
158 #[doc(alias = "get_extractable_type")]
159 #[doc(alias = "extractable-type")]
160 fn extractable_type(&self) -> glib::types::Type {
161 unsafe {
162 from_glib(ffi::ges_asset_get_extractable_type(
163 self.as_ref().to_glib_none().0,
164 ))
165 }
166 }
167
168 #[doc(alias = "ges_asset_get_id")]
169 #[doc(alias = "get_id")]
170 fn id(&self) -> glib::GString {
171 unsafe { from_glib_none(ffi::ges_asset_get_id(self.as_ref().to_glib_none().0)) }
172 }
173
174 #[doc(alias = "ges_asset_get_proxy")]
175 #[doc(alias = "get_proxy")]
176 #[must_use]
177 fn proxy(&self) -> Option<Asset> {
178 unsafe { from_glib_none(ffi::ges_asset_get_proxy(self.as_ref().to_glib_none().0)) }
179 }
180
181 #[doc(alias = "ges_asset_get_proxy_target")]
182 #[doc(alias = "get_proxy_target")]
183 #[doc(alias = "proxy-target")]
184 #[must_use]
185 fn proxy_target(&self) -> Option<Asset> {
186 unsafe {
187 from_glib_none(ffi::ges_asset_get_proxy_target(
188 self.as_ref().to_glib_none().0,
189 ))
190 }
191 }
192
193 #[doc(alias = "ges_asset_list_proxies")]
194 fn list_proxies(&self) -> Vec<Asset> {
195 unsafe {
196 FromGlibPtrContainer::from_glib_none(ffi::ges_asset_list_proxies(
197 self.as_ref().to_glib_none().0,
198 ))
199 }
200 }
201
202 #[doc(alias = "ges_asset_set_proxy")]
203 #[doc(alias = "proxy")]
204 fn set_proxy(&self, proxy: Option<&impl IsA<Asset>>) -> Result<(), glib::error::BoolError> {
205 unsafe {
206 glib::result_from_gboolean!(
207 ffi::ges_asset_set_proxy(
208 self.as_ref().to_glib_none().0,
209 proxy.map(|p| p.as_ref()).to_glib_none().0
210 ),
211 "Failed to set proxy"
212 )
213 }
214 }
215
216 #[doc(alias = "ges_asset_unproxy")]
217 fn unproxy(&self, proxy: &impl IsA<Asset>) -> Result<(), glib::error::BoolError> {
218 unsafe {
219 glib::result_from_gboolean!(
220 ffi::ges_asset_unproxy(
221 self.as_ref().to_glib_none().0,
222 proxy.as_ref().to_glib_none().0
223 ),
224 "Failed to unproxy asset"
225 )
226 }
227 }
228
229 #[doc(alias = "proxy")]
230 fn connect_proxy_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
231 unsafe extern "C" fn notify_proxy_trampoline<
232 P: IsA<Asset>,
233 F: Fn(&P) + Send + Sync + 'static,
234 >(
235 this: *mut ffi::GESAsset,
236 _param_spec: glib::ffi::gpointer,
237 f: glib::ffi::gpointer,
238 ) {
239 unsafe {
240 let f: &F = &*(f as *const F);
241 f(Asset::from_glib_borrow(this).unsafe_cast_ref())
242 }
243 }
244 unsafe {
245 let f: Box_<F> = Box_::new(f);
246 connect_raw(
247 self.as_ptr() as *mut _,
248 c"notify::proxy".as_ptr(),
249 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
250 notify_proxy_trampoline::<Self, F> as *const (),
251 )),
252 Box_::into_raw(f),
253 )
254 }
255 }
256
257 #[doc(alias = "proxy-target")]
258 fn connect_proxy_target_notify<F: Fn(&Self) + Send + Sync + 'static>(
259 &self,
260 f: F,
261 ) -> SignalHandlerId {
262 unsafe extern "C" fn notify_proxy_target_trampoline<
263 P: IsA<Asset>,
264 F: Fn(&P) + Send + Sync + 'static,
265 >(
266 this: *mut ffi::GESAsset,
267 _param_spec: glib::ffi::gpointer,
268 f: glib::ffi::gpointer,
269 ) {
270 unsafe {
271 let f: &F = &*(f as *const F);
272 f(Asset::from_glib_borrow(this).unsafe_cast_ref())
273 }
274 }
275 unsafe {
276 let f: Box_<F> = Box_::new(f);
277 connect_raw(
278 self.as_ptr() as *mut _,
279 c"notify::proxy-target".as_ptr(),
280 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
281 notify_proxy_target_trampoline::<Self, F> as *const (),
282 )),
283 Box_::into_raw(f),
284 )
285 }
286 }
287}
288
289impl<O: IsA<Asset>> AssetExt for O {}