1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use ges_sys;
use glib;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::GString;
use glib_sys;
use gst_pbutils;
use libc;
use std::boxed::Box as Box_;
use std::mem::transmute;
use std::ptr;
use Asset;
use Timeline;

glib_wrapper! {
    /// The `Project` is used to control a set of `Asset` and is a
    /// `Asset` with `GES_TYPE_TIMELINE` as `extractable_type` itself. That
    /// means that you can extract `Timeline` from a project as followed:
    ///
    ///
    /// ```text
    ///  GESProject *project;
    ///  GESTimeline *timeline;
    ///
    ///  project = ges_project_new ("file:///path/to/a/valid/project/uri");
    ///
    ///  // Here you can connect to the various signal to get more infos about
    ///  // what is happening and recover from errors if possible
    ///  ...
    ///
    ///  timeline = ges_asset_extract (GES_ASSET (project));
    /// ```
    ///
    /// The `Project` class offers a higher level API to handle `Asset`-s.
    /// It lets you request new asset, and it informs you about new assets through
    /// a set of signals. Also it handles problem such as missing files/missing
    /// `gst::Element` and lets you try to recover from those.
    ///
    /// # Implements
    ///
    /// [`ProjectExt`](trait.ProjectExt.html), [`AssetExt`](trait.AssetExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html)
    pub struct Project(Object<ges_sys::GESProject, ges_sys::GESProjectClass, ProjectClass>) @extends Asset;

    match fn {
        get_type => || ges_sys::ges_project_get_type(),
    }
}

impl Project {
    /// Creates a new `Project` and sets its uri to `uri` if provided. Note that
    /// if `uri` is not valid or `None`, the uri of the project will then be set
    /// the first time you save the project. If you then save the project to
    /// other locations, it will never be updated again and the first valid URI is
    /// the URI it will keep refering to.
    /// ## `uri`
    /// The uri to be set after creating the project.
    ///
    /// # Returns
    ///
    /// A newly created `Project`
    pub fn new(uri: Option<&str>) -> Project {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ges_sys::ges_project_new(uri.to_glib_none().0)) }
    }
}

pub const NONE_PROJECT: Option<&Project> = None;

/// Trait containing all `Project` methods.
///
/// # Implementors
///
/// [`Project`](struct.Project.html)
pub trait ProjectExt: 'static {
    /// Adds a `Asset` to `self`, the project will keep a reference on
    /// `asset`.
    /// ## `asset`
    /// A `Asset` to add to `self`
    ///
    /// # Returns
    ///
    /// `true` if the asset could be added `false` it was already
    /// in the project
    fn add_asset<P: IsA<Asset>>(&self, asset: &P) -> bool;

    /// Adds `profile` to the project. It lets you save in what format
    /// the project has been renders and keep a reference to those formats.
    /// Also, those formats will be saves to the project file when possible.
    /// ## `profile`
    /// A `gst_pbutils::EncodingProfile` to add to the project. If a profile with
    /// the same name already exists, it will be replaced
    ///
    /// # Returns
    ///
    /// `true` if `profile` could be added, `false` otherwize
    fn add_encoding_profile<P: IsA<gst_pbutils::EncodingProfile>>(
        &self,
        profile: &P,
    ) -> Result<(), glib::error::BoolError>;

    /// Create and add a `Asset` to `self`. You should connect to the
    /// "asset-added" signal to get the asset when it finally gets added to
    /// `self`
    /// ## `id`
    /// The id of the asset to create and add to `self`
    /// ## `extractable_type`
    /// The `glib::Type` of the asset to create
    ///
    /// # Returns
    ///
    /// `true` if the asset started to be added `false` it was already
    /// in the project
    fn create_asset(&self, id: Option<&str>, extractable_type: glib::types::Type) -> bool;

    /// Create and add a `Asset` to `self`. You should connect to the
    /// "asset-added" signal to get the asset when it finally gets added to
    /// `self`
    /// ## `id`
    /// The id of the asset to create and add to `self`
    /// ## `extractable_type`
    /// The `glib::Type` of the asset to create
    ///
    /// # Returns
    ///
    /// The newly created `Asset` or `None`.
    fn create_asset_sync(
        &self,
        id: Option<&str>,
        extractable_type: glib::types::Type,
    ) -> Result<Option<Asset>, glib::Error>;

    /// ## `id`
    /// The id of the asset to retrieve
    /// ## `extractable_type`
    /// The extractable_type of the asset
    /// to retrieve from `object`
    ///
    /// # Returns
    ///
    /// The `Asset` with
    /// `id` or `None` if no asset with `id` as an ID
    fn get_asset(&self, id: &str, extractable_type: glib::types::Type) -> Option<Asset>;

    /// Get the assets that are being loaded
    ///
    /// # Returns
    ///
    /// A set of loading asset
    /// that will be added to `self`. Note that those Asset are *not* loaded yet,
    /// and thus can not be used
    fn get_loading_assets(&self) -> Vec<Asset>;

    /// Retrieve the uri that is currently set on `self`
    ///
    /// # Returns
    ///
    /// a newly allocated string representing uri.
    fn get_uri(&self) -> Option<GString>;

    /// List all `asset` contained in `self` filtering per extractable_type
    /// as defined by `filter`. It copies the asset and thus will not be updated
    /// in time.
    /// ## `filter`
    /// Type of assets to list, `GES_TYPE_EXTRACTABLE` will list
    /// all assets
    ///
    /// # Returns
    ///
    /// The list of
    /// `Asset` the object contains
    fn list_assets(&self, filter: glib::types::Type) -> Vec<Asset>;

    /// Lists the encoding profile that have been set to `self`. The first one
    /// is the latest added.
    ///
    /// # Returns
    ///
    /// The
    /// list of `gst_pbutils::EncodingProfile` used in `self`
    fn list_encoding_profiles(&self) -> Vec<gst_pbutils::EncodingProfile>;

    /// Loads `self` into `timeline`
    /// ## `timeline`
    /// A blank timeline to load `self` into
    ///
    /// # Returns
    ///
    /// `true` if the project could be loaded `false` otherwize.
    fn load<P: IsA<Timeline>>(&self, timeline: &P) -> Result<(), glib::Error>;

    /// remove a `asset` to from `self`.
    /// ## `asset`
    /// A `Asset` to remove from `self`
    ///
    /// # Returns
    ///
    /// `true` if the asset could be removed `false` otherwise
    fn remove_asset<P: IsA<Asset>>(&self, asset: &P) -> Result<(), glib::error::BoolError>;

    /// Save the timeline of `self` to `uri`. You should make sure that `timeline`
    /// is one of the timelines that have been extracted from `self`
    /// (using ges_asset_extract (`self`);)
    /// ## `timeline`
    /// The `Timeline` to save, it must have been extracted from `self`
    /// ## `uri`
    /// The uri where to save `self` and `timeline`
    /// ## `formatter_asset`
    /// The formatter asset to use or `None`. If `None`,
    /// will try to save in the same format as the one from which the timeline as been loaded
    /// or default to the formatter with highest rank
    /// ## `overwrite`
    /// `true` to overwrite file if it exists
    ///
    /// # Returns
    ///
    /// `true` if the project could be save, `false` otherwize
    fn save<P: IsA<Timeline>, Q: IsA<Asset>>(
        &self,
        timeline: &P,
        uri: &str,
        formatter_asset: Option<&Q>,
        overwrite: bool,
    ) -> Result<(), glib::Error>;

    /// ## `asset`
    /// The `Asset` that has been added to `project`
    fn connect_asset_added<F: Fn(&Self, &Asset) + 'static>(&self, f: F) -> SignalHandlerId;

    /// ## `asset`
    /// The `Asset` that started loading
    fn connect_asset_loading<F: Fn(&Self, &Asset) + 'static>(&self, f: F) -> SignalHandlerId;

    /// ## `asset`
    /// The `Asset` that has been removed from `project`
    fn connect_asset_removed<F: Fn(&Self, &Asset) + 'static>(&self, f: F) -> SignalHandlerId;

    /// Informs you that a `Asset` could not be created. In case of
    /// missing GStreamer plugins, the error will be set to `GST_CORE_ERROR`
    /// `gst::CoreError::MissingPlugin`
    /// ## `error`
    /// The `glib::Error` defining the error that occured, might be `None`
    /// ## `id`
    /// The `id` of the asset that failed loading
    /// ## `extractable_type`
    /// The `extractable_type` of the asset that
    /// failed loading
    fn connect_error_loading_asset<F: Fn(&Self, &glib::Error, &str, glib::types::Type) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// ## `timeline`
    /// The `Timeline` that complete loading
    fn connect_loaded<F: Fn(&Self, &Timeline) + 'static>(&self, f: F) -> SignalHandlerId;

    ///
    /// ```text
    /// static gchar
    /// source_moved_cb (GESProject *project, GError *error, GESAsset *asset_with_error)
    /// {
    ///   return g_strdup ("file:///the/new/uri.ogg");
    /// }
    ///
    /// static int
    /// main (int argc, gchar ** argv)
    /// {
    ///   GESTimeline *timeline;
    ///   GESProject *project = ges_project_new ("file:///some/uri.xges");
    ///
    ///   g_signal_connect (project, "missing-uri", source_moved_cb, NULL);
    ///   timeline = ges_asset_extract (GES_ASSET (project));
    /// }
    /// ```
    /// ## `error`
    /// The error that happened
    /// ## `wrong_asset`
    /// The asset with the wrong ID, you should us it and its content
    /// only to find out what the new location is.
    ///
    /// # Returns
    ///
    /// The new URI of `wrong_asset`
    fn connect_missing_uri<F: Fn(&Self, &glib::Error, &Asset) -> Option<GString> + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<Project>> ProjectExt for O {
    fn add_asset<P: IsA<Asset>>(&self, asset: &P) -> bool {
        unsafe {
            from_glib(ges_sys::ges_project_add_asset(
                self.as_ref().to_glib_none().0,
                asset.as_ref().to_glib_none().0,
            ))
        }
    }

    fn add_encoding_profile<P: IsA<gst_pbutils::EncodingProfile>>(
        &self,
        profile: &P,
    ) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib_result_from_gboolean!(
                ges_sys::ges_project_add_encoding_profile(
                    self.as_ref().to_glib_none().0,
                    profile.as_ref().to_glib_none().0
                ),
                "Failed to add profile"
            )
        }
    }

    fn create_asset(&self, id: Option<&str>, extractable_type: glib::types::Type) -> bool {
        unsafe {
            from_glib(ges_sys::ges_project_create_asset(
                self.as_ref().to_glib_none().0,
                id.to_glib_none().0,
                extractable_type.to_glib(),
            ))
        }
    }

    fn create_asset_sync(
        &self,
        id: Option<&str>,
        extractable_type: glib::types::Type,
    ) -> Result<Option<Asset>, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ges_sys::ges_project_create_asset_sync(
                self.as_ref().to_glib_none().0,
                id.to_glib_none().0,
                extractable_type.to_glib(),
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn get_asset(&self, id: &str, extractable_type: glib::types::Type) -> Option<Asset> {
        unsafe {
            from_glib_full(ges_sys::ges_project_get_asset(
                self.as_ref().to_glib_none().0,
                id.to_glib_none().0,
                extractable_type.to_glib(),
            ))
        }
    }

    fn get_loading_assets(&self) -> Vec<Asset> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ges_sys::ges_project_get_loading_assets(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_uri(&self) -> Option<GString> {
        unsafe { from_glib_full(ges_sys::ges_project_get_uri(self.as_ref().to_glib_none().0)) }
    }

    fn list_assets(&self, filter: glib::types::Type) -> Vec<Asset> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ges_sys::ges_project_list_assets(
                self.as_ref().to_glib_none().0,
                filter.to_glib(),
            ))
        }
    }

    fn list_encoding_profiles(&self) -> Vec<gst_pbutils::EncodingProfile> {
        unsafe {
            FromGlibPtrContainer::from_glib_none(ges_sys::ges_project_list_encoding_profiles(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn load<P: IsA<Timeline>>(&self, timeline: &P) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = ges_sys::ges_project_load(
                self.as_ref().to_glib_none().0,
                timeline.as_ref().to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn remove_asset<P: IsA<Asset>>(&self, asset: &P) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib_result_from_gboolean!(
                ges_sys::ges_project_remove_asset(
                    self.as_ref().to_glib_none().0,
                    asset.as_ref().to_glib_none().0
                ),
                "Failed to remove asset"
            )
        }
    }

    fn save<P: IsA<Timeline>, Q: IsA<Asset>>(
        &self,
        timeline: &P,
        uri: &str,
        formatter_asset: Option<&Q>,
        overwrite: bool,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = ges_sys::ges_project_save(
                self.as_ref().to_glib_none().0,
                timeline.as_ref().to_glib_none().0,
                uri.to_glib_none().0,
                formatter_asset.map(|p| p.as_ref()).to_glib_none().0,
                overwrite.to_glib(),
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn connect_asset_added<F: Fn(&Self, &Asset) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn asset_added_trampoline<P, F: Fn(&P, &Asset) + 'static>(
            this: *mut ges_sys::GESProject,
            asset: *mut ges_sys::GESAsset,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Project>,
        {
            let f: &F = &*(f as *const F);
            f(
                &Project::from_glib_borrow(this).unsafe_cast(),
                &from_glib_borrow(asset),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"asset-added\0".as_ptr() as *const _,
                Some(transmute(asset_added_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_asset_loading<F: Fn(&Self, &Asset) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn asset_loading_trampoline<P, F: Fn(&P, &Asset) + 'static>(
            this: *mut ges_sys::GESProject,
            asset: *mut ges_sys::GESAsset,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Project>,
        {
            let f: &F = &*(f as *const F);
            f(
                &Project::from_glib_borrow(this).unsafe_cast(),
                &from_glib_borrow(asset),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"asset-loading\0".as_ptr() as *const _,
                Some(transmute(asset_loading_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_asset_removed<F: Fn(&Self, &Asset) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn asset_removed_trampoline<P, F: Fn(&P, &Asset) + 'static>(
            this: *mut ges_sys::GESProject,
            asset: *mut ges_sys::GESAsset,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Project>,
        {
            let f: &F = &*(f as *const F);
            f(
                &Project::from_glib_borrow(this).unsafe_cast(),
                &from_glib_borrow(asset),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"asset-removed\0".as_ptr() as *const _,
                Some(transmute(asset_removed_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_error_loading_asset<
        F: Fn(&Self, &glib::Error, &str, glib::types::Type) + 'static,
    >(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn error_loading_asset_trampoline<
            P,
            F: Fn(&P, &glib::Error, &str, glib::types::Type) + 'static,
        >(
            this: *mut ges_sys::GESProject,
            error: *mut glib_sys::GError,
            id: *mut libc::c_char,
            extractable_type: glib_sys::GType,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Project>,
        {
            let f: &F = &*(f as *const F);
            f(
                &Project::from_glib_borrow(this).unsafe_cast(),
                &from_glib_borrow(error),
                &GString::from_glib_borrow(id),
                from_glib(extractable_type),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"error-loading-asset\0".as_ptr() as *const _,
                Some(transmute(
                    error_loading_asset_trampoline::<Self, F> as usize,
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_loaded<F: Fn(&Self, &Timeline) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn loaded_trampoline<P, F: Fn(&P, &Timeline) + 'static>(
            this: *mut ges_sys::GESProject,
            timeline: *mut ges_sys::GESTimeline,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Project>,
        {
            let f: &F = &*(f as *const F);
            f(
                &Project::from_glib_borrow(this).unsafe_cast(),
                &from_glib_borrow(timeline),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"loaded\0".as_ptr() as *const _,
                Some(transmute(loaded_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_missing_uri<F: Fn(&Self, &glib::Error, &Asset) -> Option<GString> + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn missing_uri_trampoline<
            P,
            F: Fn(&P, &glib::Error, &Asset) -> Option<GString> + 'static,
        >(
            this: *mut ges_sys::GESProject,
            error: *mut glib_sys::GError,
            wrong_asset: *mut ges_sys::GESAsset,
            f: glib_sys::gpointer,
        ) -> *mut libc::c_char
        where
            P: IsA<Project>,
        {
            let f: &F = &*(f as *const F);
            f(
                &Project::from_glib_borrow(this).unsafe_cast(),
                &from_glib_borrow(error),
                &from_glib_borrow(wrong_asset),
            )
            .to_glib_full()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"missing-uri\0".as_ptr() as *const _,
                Some(transmute(missing_uri_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }
}