Plug some GError leaks when loading assets
[platform/upstream/gst-editing-services.git] / ges / ges-project.c
1 /* GStreamer Editing Services
2  *
3  * Copyright (C) <2012> Thibault Saunier <thibault.saunier@collabora.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 /**
21  * SECTION: gesproject
22  * @title: GESProject
23  * @short_description: A GESAsset that is used to manage projects
24  *
25  * The #GESProject is used to control a set of #GESAsset and is a
26  * #GESAsset with #GES_TYPE_TIMELINE as @extractable_type itself. That
27  * means that you can extract #GESTimeline from a project as followed:
28  *
29  * |[
30  *  GESProject *project;
31  *  GESTimeline *timeline;
32  *
33  *  project = ges_project_new ("file:///path/to/a/valid/project/uri");
34  *
35  *  // Here you can connect to the various signal to get more infos about
36  *  // what is happening and recover from errors if possible
37  *  ...
38  *
39  *  timeline = ges_asset_extract (GES_ASSET (project));
40  * ]|
41  *
42  * The #GESProject class offers a higher level API to handle #GESAsset-s.
43  * It lets you request new asset, and it informs you about new assets through
44  * a set of signals. Also it handles problem such as missing files/missing
45  * #GstElement and lets you try to recover from those.
46  */
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include "ges.h"
52 #include "ges-internal.h"
53
54 static GPtrArray *new_paths = NULL;
55 static GHashTable *tried_uris = NULL;
56
57 /* TODO We should rely on both extractable_type and @id to identify
58  * a Asset, not only @id
59  */
60 struct _GESProjectPrivate
61 {
62   GHashTable *assets;
63   /* Set of asset ID being loaded */
64   GHashTable *loading_assets;
65   GHashTable *loaded_with_error;
66   GESAsset *formatter_asset;
67
68   GList *formatters;
69
70   gchar *uri;
71
72   GList *encoding_profiles;
73 };
74
75 typedef struct EmitLoadedInIdle
76 {
77   GESProject *project;
78   GESTimeline *timeline;
79 } EmitLoadedInIdle;
80
81 enum
82 {
83   LOADED_SIGNAL,
84   ERROR_LOADING_ASSET,
85   ASSET_ADDED_SIGNAL,
86   ASSET_REMOVED_SIGNAL,
87   MISSING_URI_SIGNAL,
88   ASSET_LOADING_SIGNAL,
89   LAST_SIGNAL
90 };
91
92 G_DEFINE_TYPE_WITH_PRIVATE (GESProject, ges_project, GES_TYPE_ASSET);
93
94 static guint _signals[LAST_SIGNAL] = { 0 };
95
96 static guint nb_projects = 0;
97
98 enum
99 {
100   PROP_0,
101   PROP_URI,
102   PROP_LAST,
103 };
104
105 static GParamSpec *_properties[LAST_SIGNAL] = { 0 };
106
107 static gboolean
108 _emit_loaded_in_idle (EmitLoadedInIdle * data)
109 {
110   g_signal_emit (data->project, _signals[LOADED_SIGNAL], 0, data->timeline);
111
112   gst_object_unref (data->project);
113   gst_object_unref (data->timeline);
114   g_slice_free (EmitLoadedInIdle, data);
115
116   return FALSE;
117 }
118
119 static void
120 ges_project_add_formatter (GESProject * project, GESFormatter * formatter)
121 {
122   GESProjectPrivate *priv = GES_PROJECT (project)->priv;
123
124   ges_formatter_set_project (formatter, project);
125   priv->formatters = g_list_append (priv->formatters, formatter);
126
127   gst_object_ref_sink (formatter);
128 }
129
130 static void
131 ges_project_remove_formatter (GESProject * project, GESFormatter * formatter)
132 {
133   GList *tmp;
134   GESProjectPrivate *priv = GES_PROJECT (project)->priv;
135
136   for (tmp = priv->formatters; tmp; tmp = tmp->next) {
137     if (tmp->data == formatter) {
138       gst_object_unref (formatter);
139       priv->formatters = g_list_delete_link (priv->formatters, tmp);
140
141       return;
142     }
143   }
144 }
145
146 static void
147 ges_project_set_uri (GESProject * project, const gchar * uri)
148 {
149   GESProjectPrivate *priv;
150
151   g_return_if_fail (GES_IS_PROJECT (project));
152
153   priv = project->priv;
154   if (priv->uri) {
155     GST_WARNING_OBJECT (project, "Trying to rest URI, this is prohibited");
156
157     return;
158   }
159
160   if (uri == NULL) {
161     GST_LOG_OBJECT (project, "Uri should not be NULL");
162     return;
163   }
164
165   priv->uri = g_strdup (uri);
166
167   /* We use that URI as ID */
168   ges_asset_set_id (GES_ASSET (project), uri);
169
170   return;
171 }
172
173 static gboolean
174 _load_project (GESProject * project, GESTimeline * timeline, GError ** error)
175 {
176   GError *lerr = NULL;
177   GESProjectPrivate *priv;
178   GESFormatter *formatter;
179
180   priv = GES_PROJECT (project)->priv;
181
182   if (priv->uri == NULL) {
183     EmitLoadedInIdle *data = g_slice_new (EmitLoadedInIdle);
184
185     GST_LOG_OBJECT (project, "%s, Loading an empty timeline %s"
186         " as no URI set yet", GST_OBJECT_NAME (timeline),
187         ges_asset_get_id (GES_ASSET (project)));
188
189     data->timeline = gst_object_ref (timeline);
190     data->project = gst_object_ref (project);
191
192     /* Make sure the signal is emitted after the functions ends */
193     g_idle_add ((GSourceFunc) _emit_loaded_in_idle, data);
194     return TRUE;
195   }
196
197   if (priv->formatter_asset == NULL)
198     priv->formatter_asset = _find_formatter_asset_for_id (priv->uri);
199
200   if (priv->formatter_asset == NULL) {
201     lerr = g_error_new (GES_ERROR, 0, "Could not find a suitable formatter");
202     goto failed;
203   }
204
205   formatter = GES_FORMATTER (ges_asset_extract (priv->formatter_asset, &lerr));
206   if (lerr) {
207     GST_WARNING_OBJECT (project, "Could not create the formatter: %s",
208         lerr->message);
209
210     goto failed;
211   }
212
213   ges_project_add_formatter (GES_PROJECT (project), formatter);
214   ges_formatter_load_from_uri (formatter, timeline, priv->uri, &lerr);
215   if (lerr) {
216     GST_WARNING_OBJECT (project, "Could not load the timeline,"
217         " returning: %s", lerr->message);
218     goto failed;
219   }
220
221   return TRUE;
222
223 failed:
224   if (lerr)
225     g_propagate_error (error, lerr);
226   return FALSE;
227 }
228
229 static gboolean
230 _uri_missing_accumulator (GSignalInvocationHint * ihint, GValue * return_accu,
231     const GValue * handler_return, gpointer data)
232 {
233   const gchar *ret = g_value_get_string (handler_return);
234
235   if (ret) {
236     if (!gst_uri_is_valid (ret)) {
237       GST_INFO ("The uri %s was not valid, can not work with it!", ret);
238       return TRUE;
239     }
240
241     g_value_set_string (return_accu, ret);
242     return FALSE;
243   }
244
245   return TRUE;
246 }
247
248 /* GESAsset vmethod implementation */
249 static GESExtractable *
250 ges_project_extract (GESAsset * project, GError ** error)
251 {
252   GESTimeline *timeline = g_object_new (GES_TYPE_TIMELINE, NULL);
253
254   ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
255   if (_load_project (GES_PROJECT (project), timeline, error))
256     return GES_EXTRACTABLE (timeline);
257
258   gst_object_unref (timeline);
259   return NULL;
260 }
261
262 static void
263 _add_media_new_paths_recursing (const gchar * value)
264 {
265   GFileInfo *info;
266   GFileEnumerator *fenum;
267   GFile *file = g_file_new_for_uri (value);
268
269   if (!(fenum = g_file_enumerate_children (file,
270               "standard::*", G_FILE_QUERY_INFO_NONE, NULL, NULL))) {
271     GST_INFO ("%s is not a folder", value);
272
273     goto done;
274   }
275
276   GST_INFO ("Adding folder: %s", value);
277   g_ptr_array_add (new_paths, g_strdup (value));
278   info = g_file_enumerator_next_file (fenum, NULL, NULL);
279   while (info != NULL) {
280     if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
281       GFile *f = g_file_enumerator_get_child (fenum, info);
282       gchar *uri = g_file_get_uri (f);
283
284       _add_media_new_paths_recursing (uri);
285       gst_object_unref (f);
286       g_free (uri);
287     }
288     g_object_unref (info);
289     info = g_file_enumerator_next_file (fenum, NULL, NULL);
290   }
291
292 done:
293   gst_object_unref (file);
294   if (fenum)
295     gst_object_unref (fenum);
296 }
297
298 gboolean
299 ges_add_missing_uri_relocation_uri (const gchar * uri, gboolean recurse)
300 {
301   g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
302
303   if (new_paths == NULL)
304     new_paths = g_ptr_array_new_with_free_func (g_free);
305
306   if (recurse)
307     _add_media_new_paths_recursing (uri);
308   else
309     g_ptr_array_add (new_paths, g_strdup (uri));
310
311   return TRUE;
312 }
313
314 static gchar *
315 ges_missing_uri_default (GESProject * self, GError * error,
316     GESAsset * wrong_asset)
317 {
318   guint i;
319   const gchar *old_uri = ges_asset_get_id (wrong_asset);
320   gchar *new_id = NULL;
321
322   if (ges_asset_request_id_update (wrong_asset, &new_id, error) && new_id) {
323     GST_INFO_OBJECT (self, "Returned guessed new ID: %s", new_id);
324
325     return new_id;
326   }
327
328   if (new_paths == NULL)
329     return NULL;
330
331   if (tried_uris == NULL)
332     tried_uris = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
333
334   for (i = 0; i < new_paths->len; i++) {
335     gchar *basename, *res;
336
337     basename = g_path_get_basename (old_uri);
338     res = g_build_filename (new_paths->pdata[i], basename, NULL);
339     g_free (basename);
340
341     if (g_strcmp0 (old_uri, res) == 0) {
342       g_hash_table_add (tried_uris, res);
343     } else if (g_hash_table_lookup (tried_uris, res)) {
344       GST_DEBUG_OBJECT (self, "File already tried: %s", res);
345       g_free (res);
346     } else {
347       g_hash_table_add (tried_uris, g_strdup (res));
348       GST_DEBUG_OBJECT (self, "Trying: %s\n", res);
349       return res;
350     }
351   }
352
353   return NULL;
354 }
355
356 gchar *
357 ges_uri_asset_try_update_id (GError * error, GESAsset * wrong_asset)
358 {
359   return ges_missing_uri_default (NULL, error, wrong_asset);
360 }
361
362 static void
363 ges_uri_assets_validate_uri (const gchar * nid)
364 {
365   if (tried_uris)
366     g_hash_table_remove (tried_uris, nid);
367 }
368
369 /* GObject vmethod implementation */
370 static void
371 _dispose (GObject * object)
372 {
373   GList *tmp;
374   GESProjectPrivate *priv = GES_PROJECT (object)->priv;
375
376   if (priv->assets)
377     g_hash_table_unref (priv->assets);
378   if (priv->loading_assets)
379     g_hash_table_unref (priv->loading_assets);
380   if (priv->loaded_with_error)
381     g_hash_table_unref (priv->loaded_with_error);
382   if (priv->formatter_asset)
383     gst_object_unref (priv->formatter_asset);
384
385   for (tmp = priv->formatters; tmp; tmp = tmp->next)
386     ges_project_remove_formatter (GES_PROJECT (object), tmp->data);;
387
388   G_OBJECT_CLASS (ges_project_parent_class)->dispose (object);
389 }
390
391 static void
392 _finalize (GObject * object)
393 {
394   GESProjectPrivate *priv = GES_PROJECT (object)->priv;
395
396   if (priv->uri)
397     g_free (priv->uri);
398   g_list_free_full (priv->encoding_profiles, g_object_unref);
399
400   G_OBJECT_CLASS (ges_project_parent_class)->finalize (object);
401 }
402
403 static void
404 _get_property (GESProject * project, guint property_id,
405     GValue * value, GParamSpec * pspec)
406 {
407   GESProjectPrivate *priv = project->priv;
408
409   switch (property_id) {
410     case PROP_URI:
411       g_value_set_string (value, priv->uri);
412       break;
413     default:
414       G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
415   }
416 }
417
418 static void
419 _set_property (GESProject * project, guint property_id,
420     const GValue * value, GParamSpec * pspec)
421 {
422   switch (property_id) {
423     case PROP_URI:
424       project->priv->uri = g_value_dup_string (value);
425       break;
426     default:
427       G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
428   }
429 }
430
431 static void
432 ges_project_class_init (GESProjectClass * klass)
433 {
434   GObjectClass *object_class = G_OBJECT_CLASS (klass);
435
436   klass->asset_added = NULL;
437   klass->missing_uri = ges_missing_uri_default;
438   klass->loading_error = NULL;
439   klass->asset_removed = NULL;
440   object_class->get_property = (GObjectGetPropertyFunc) _get_property;
441   object_class->set_property = (GObjectSetPropertyFunc) _set_property;
442
443   /**
444    * GESProject::uri:
445    *
446    * The location of the project to use.
447    */
448   _properties[PROP_URI] = g_param_spec_string ("uri", "URI",
449       "uri of the project", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
450
451   g_object_class_install_properties (object_class, PROP_LAST, _properties);
452
453   /**
454    * GESProject::asset-added:
455    * @project: the #GESProject
456    * @asset: The #GESAsset that has been added to @project
457    */
458   _signals[ASSET_ADDED_SIGNAL] =
459       g_signal_new ("asset-added", G_TYPE_FROM_CLASS (klass),
460       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_added),
461       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
462
463   /**
464    * GESProject::asset-loading:
465    * @project: the #GESProject
466    * @asset: The #GESAsset that started loading
467    *
468    * Since: 1.8
469    */
470   _signals[ASSET_LOADING_SIGNAL] =
471       g_signal_new ("asset-loading", G_TYPE_FROM_CLASS (klass),
472       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_loading),
473       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
474
475   /**
476    * GESProject::asset-removed:
477    * @project: the #GESProject
478    * @asset: The #GESAsset that has been removed from @project
479    */
480   _signals[ASSET_REMOVED_SIGNAL] =
481       g_signal_new ("asset-removed", G_TYPE_FROM_CLASS (klass),
482       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_removed),
483       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
484
485   /**
486    * GESProject::loaded:
487    * @project: the #GESProject that is done loading a project.
488    * @timeline: The #GESTimeline that complete loading
489    */
490   _signals[LOADED_SIGNAL] =
491       g_signal_new ("loaded", G_TYPE_FROM_CLASS (klass),
492       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loaded),
493       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
494       1, GES_TYPE_TIMELINE);
495
496   /**
497    * GESProject::missing-uri:
498    * @project: the #GESProject reporting that a file has moved
499    * @error: The error that happened
500    * @wrong_asset: The asset with the wrong ID, you should us it and its content
501    * only to find out what the new location is.
502    *
503    * |[
504    * static gchar
505    * source_moved_cb (GESProject *project, GError *error, GESAsset *asset_with_error)
506    * {
507    *   return g_strdup ("file:///the/new/uri.ogg");
508    * }
509    *
510    * static int
511    * main (int argc, gchar ** argv)
512    * {
513    *   GESTimeline *timeline;
514    *   GESProject *project = ges_project_new ("file:///some/uri.xges");
515    *
516    *   g_signal_connect (project, "missing-uri", source_moved_cb, NULL);
517    *   timeline = ges_asset_extract (GES_ASSET (project));
518    * }
519    * ]|
520    *
521    * Returns: (transfer full) (allow-none): The new URI of @wrong_asset
522    */
523   _signals[MISSING_URI_SIGNAL] =
524       g_signal_new ("missing-uri", G_TYPE_FROM_CLASS (klass),
525       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, missing_uri),
526       _uri_missing_accumulator, NULL, g_cclosure_marshal_generic,
527       G_TYPE_STRING, 2, G_TYPE_ERROR, GES_TYPE_ASSET);
528
529   /**
530    * GESProject::error-loading-asset:
531    * @project: the #GESProject on which a problem happend when creted a #GESAsset
532    * @error: The #GError defining the error that occured, might be %NULL
533    * @id: The @id of the asset that failed loading
534    * @extractable_type: The @extractable_type of the asset that
535    * failed loading
536    *
537    * Informs you that a #GESAsset could not be created. In case of
538    * missing GStreamer plugins, the error will be set to #GST_CORE_ERROR
539    * #GST_CORE_ERROR_MISSING_PLUGIN
540    */
541   _signals[ERROR_LOADING_ASSET] =
542       g_signal_new ("error-loading-asset", G_TYPE_FROM_CLASS (klass),
543       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, loading_error),
544       NULL, NULL, g_cclosure_marshal_generic,
545       G_TYPE_NONE, 3, G_TYPE_ERROR, G_TYPE_STRING, G_TYPE_GTYPE);
546
547   object_class->dispose = _dispose;
548   object_class->finalize = _finalize;
549
550   GES_ASSET_CLASS (klass)->extract = ges_project_extract;
551 }
552
553 static void
554 ges_project_init (GESProject * project)
555 {
556   GESProjectPrivate *priv = project->priv =
557       ges_project_get_instance_private (project);
558
559   priv->uri = NULL;
560   priv->formatters = NULL;
561   priv->formatter_asset = NULL;
562   priv->encoding_profiles = NULL;
563   priv->assets = g_hash_table_new_full (g_str_hash, g_str_equal,
564       g_free, gst_object_unref);
565   priv->loading_assets = g_hash_table_new_full (g_str_hash, g_str_equal,
566       g_free, gst_object_unref);
567   priv->loaded_with_error = g_hash_table_new_full (g_str_hash, g_str_equal,
568       g_free, NULL);
569 }
570
571 static void
572 _send_error_loading_asset (GESProject * project, GESAsset * asset,
573     GError * error)
574 {
575   const gchar *id = ges_asset_get_id (asset);
576
577   GST_DEBUG_OBJECT (project, "Sending error loading asset for %s", id);
578   g_hash_table_remove (project->priv->loading_assets, id);
579   g_hash_table_add (project->priv->loaded_with_error, g_strdup (id));
580   g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, error, id,
581       ges_asset_get_extractable_type (asset));
582 }
583
584 gchar *
585 ges_project_try_updating_id (GESProject * project, GESAsset * asset,
586     GError * error)
587 {
588   gchar *new_id = NULL;
589   const gchar *id;
590
591   g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
592   g_return_val_if_fail (GES_IS_ASSET (asset), NULL);
593   g_return_val_if_fail (error, NULL);
594
595   id = ges_asset_get_id (asset);
596   GST_DEBUG_OBJECT (project, "Try to proxy %s", id);
597   if (ges_asset_request_id_update (asset, &new_id, error) == FALSE) {
598     GST_DEBUG_OBJECT (project, "Type: %s can not be proxied for id: %s "
599         "and error: %s", g_type_name (G_OBJECT_TYPE (asset)), id,
600         error->message);
601     _send_error_loading_asset (project, asset, error);
602
603     return NULL;
604   }
605
606   /* Always send the MISSING_URI signal if requesting new ID is possible
607    * so that subclasses of GESProject are aware of the missing-uri */
608   g_signal_emit (project, _signals[MISSING_URI_SIGNAL], 0, error, asset,
609       &new_id);
610
611   if (new_id) {
612     GST_DEBUG_OBJECT (project, "new id found: %s", new_id);
613     if (!ges_asset_try_proxy (asset, new_id)) {
614       g_free (new_id);
615       new_id = NULL;
616     }
617   } else {
618     GST_DEBUG_OBJECT (project, "No new id found for %s", id);
619   }
620
621   g_hash_table_remove (project->priv->loading_assets, id);
622
623   if (new_id == NULL)
624     _send_error_loading_asset (project, asset, error);
625
626
627   return new_id;
628 }
629
630 static void
631 new_asset_cb (GESAsset * source, GAsyncResult * res, GESProject * project)
632 {
633   GError *error = NULL;
634   gchar *possible_id = NULL;
635   GESAsset *asset = ges_asset_request_finish (res, &error);
636
637   if (error) {
638     possible_id = ges_project_try_updating_id (project, source, error);
639     g_clear_error (&error);
640
641     if (possible_id == NULL)
642       return;
643
644     ges_project_create_asset (project, possible_id,
645         ges_asset_get_extractable_type (source));
646
647     g_free (possible_id);
648     return;
649   }
650
651   ges_asset_set_proxy (NULL, asset);
652   ges_project_add_asset (project, asset);
653   if (asset)
654     gst_object_unref (asset);
655 }
656
657 /**
658  * ges_project_set_loaded:
659  * @project: The #GESProject from which to emit the "project-loaded" signal
660  *
661  * Emits the "loaded" signal. This method should be called by sublasses when
662  * the project is fully loaded.
663  *
664  * Returns: %TRUE if the signale could be emitted %FALSE otherwize
665  */
666 gboolean
667 ges_project_set_loaded (GESProject * project, GESFormatter * formatter)
668 {
669   GST_INFO_OBJECT (project, "Emit project loaded");
670   if (GST_STATE (formatter->timeline) < GST_STATE_PAUSED) {
671     timeline_fill_gaps (formatter->timeline);
672   } else {
673     ges_timeline_commit (formatter->timeline);
674   }
675
676   g_signal_emit (project, _signals[LOADED_SIGNAL], 0, formatter->timeline);
677
678   /* We are now done with that formatter */
679   ges_project_remove_formatter (project, formatter);
680   return TRUE;
681 }
682
683 void
684 ges_project_add_loading_asset (GESProject * project, GType extractable_type,
685     const gchar * id)
686 {
687   GESAsset *asset;
688
689   if ((asset = ges_asset_cache_lookup (extractable_type, id))) {
690     if (g_hash_table_insert (project->priv->loading_assets, g_strdup (id),
691             gst_object_ref (asset)))
692       g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
693   }
694 }
695
696 /**************************************
697  *                                    *
698  *         API Implementation         *
699  *                                    *
700  **************************************/
701
702 /**
703  * ges_project_create_asset:
704  * @project: A #GESProject
705  * @id: (allow-none): The id of the asset to create and add to @project
706  * @extractable_type: The #GType of the asset to create
707  *
708  * Create and add a #GESAsset to @project. You should connect to the
709  * "asset-added" signal to get the asset when it finally gets added to
710  * @project
711  *
712  * Returns: %TRUE if the asset started to be added %FALSE it was already
713  * in the project
714  */
715 gboolean
716 ges_project_create_asset (GESProject * project, const gchar * id,
717     GType extractable_type)
718 {
719   g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
720   g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
721       FALSE);
722
723   if (id == NULL)
724     id = g_type_name (extractable_type);
725
726   if (g_hash_table_lookup (project->priv->assets, id) ||
727       g_hash_table_lookup (project->priv->loading_assets, id) ||
728       g_hash_table_lookup (project->priv->loaded_with_error, id))
729     return FALSE;
730
731   /* TODO Add a GCancellable somewhere in our API */
732   ges_asset_request_async (extractable_type, id, NULL,
733       (GAsyncReadyCallback) new_asset_cb, project);
734   ges_project_add_loading_asset (project, extractable_type, id);
735
736   return TRUE;
737 }
738
739 /**
740  * ges_project_create_asset_sync:
741  * @project: A #GESProject
742  * @id: (allow-none): The id of the asset to create and add to @project
743  * @extractable_type: The #GType of the asset to create
744  * @error: A #GError to be set in case of error
745  *
746  * Create and add a #GESAsset to @project. You should connect to the
747  * "asset-added" signal to get the asset when it finally gets added to
748  * @project
749  *
750  * Returns: (transfer full) (nullable): The newly created #GESAsset or %NULL.
751  */
752 GESAsset *
753 ges_project_create_asset_sync (GESProject * project, const gchar * id,
754     GType extractable_type, GError ** error)
755 {
756   GESAsset *asset;
757   gchar *possible_id = NULL;
758   gboolean retry = TRUE;
759
760   g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
761   g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
762       FALSE);
763
764   if (id == NULL)
765     id = g_type_name (extractable_type);
766
767   if ((asset = g_hash_table_lookup (project->priv->assets, id)))
768     return asset;
769   else if (g_hash_table_lookup (project->priv->loading_assets, id) ||
770       g_hash_table_lookup (project->priv->loaded_with_error, id))
771     return NULL;
772
773   /* TODO Add a GCancellable somewhere in our API */
774   while (retry) {
775
776     if (g_type_is_a (extractable_type, GES_TYPE_URI_CLIP)) {
777       asset = GES_ASSET (ges_uri_clip_asset_request_sync (id, error));
778     } else {
779       asset = ges_asset_request (extractable_type, id, error);
780     }
781
782     if (asset) {
783       retry = FALSE;
784
785       if ((!g_hash_table_lookup (project->priv->assets,
786                   ges_asset_get_id (asset))))
787         g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
788
789       if (possible_id) {
790         g_free (possible_id);
791         ges_uri_assets_validate_uri (id);
792       }
793
794       break;
795     } else {
796       GESAsset *tmpasset;
797
798       tmpasset = ges_asset_cache_lookup (extractable_type, id);
799       possible_id = ges_project_try_updating_id (project, tmpasset, *error);
800
801       if (possible_id == NULL) {
802         g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, tmpasset);
803         g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, *error, id,
804             extractable_type);
805         return NULL;
806       }
807
808
809       g_clear_error (error);
810
811       id = possible_id;
812     }
813   }
814
815   if (!ges_asset_get_proxy_target (asset))
816     ges_asset_set_proxy (NULL, asset);
817
818   ges_project_add_asset (project, asset);
819
820   return asset;
821 }
822
823 /**
824  * ges_project_add_asset:
825  * @project: A #GESProject
826  * @asset: (transfer none): A #GESAsset to add to @project
827  *
828  * Adds a #Asset to @project, the project will keep a reference on
829  * @asset.
830  *
831  * Returns: %TRUE if the asset could be added %FALSE it was already
832  * in the project
833  */
834 gboolean
835 ges_project_add_asset (GESProject * project, GESAsset * asset)
836 {
837   g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
838
839   if (g_hash_table_lookup (project->priv->assets, ges_asset_get_id (asset)))
840     return TRUE;
841
842   g_hash_table_insert (project->priv->assets,
843       g_strdup (ges_asset_get_id (asset)), gst_object_ref (asset));
844
845   g_hash_table_remove (project->priv->loading_assets, ges_asset_get_id (asset));
846   GST_DEBUG_OBJECT (project, "Asset added: %s", ges_asset_get_id (asset));
847   g_signal_emit (project, _signals[ASSET_ADDED_SIGNAL], 0, asset);
848
849   return TRUE;
850 }
851
852 /**
853  * ges_project_remove_asset:
854  * @project: A #GESProject
855  * @asset: (transfer none): A #GESAsset to remove from @project
856  *
857  * remove a @asset to from @project.
858  *
859  * Returns: %TRUE if the asset could be removed %FALSE otherwise
860  */
861 gboolean
862 ges_project_remove_asset (GESProject * project, GESAsset * asset)
863 {
864   gboolean ret;
865
866   g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
867
868   ret = g_hash_table_remove (project->priv->assets, ges_asset_get_id (asset));
869   g_signal_emit (project, _signals[ASSET_REMOVED_SIGNAL], 0, asset);
870
871   return ret;
872 }
873
874 /**
875  * ges_project_get_asset:
876  * @project: A #GESProject
877  * @id: The id of the asset to retrieve
878  * @extractable_type: The extractable_type of the asset
879  * to retrieve from @object
880  *
881  * Returns: (transfer full) (allow-none): The #GESAsset with
882  * @id or %NULL if no asset with @id as an ID
883  */
884 GESAsset *
885 ges_project_get_asset (GESProject * project, const gchar * id,
886     GType extractable_type)
887 {
888   GESAsset *asset;
889
890   g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
891   g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
892       NULL);
893
894   asset = g_hash_table_lookup (project->priv->assets, id);
895
896   if (asset)
897     return gst_object_ref (asset);
898
899   return NULL;
900 }
901
902 /**
903  * ges_project_list_assets:
904  * @project: A #GESProject
905  * @filter: Type of assets to list, #GES_TYPE_EXTRACTABLE will list
906  * all assets
907  *
908  * List all @asset contained in @project filtering per extractable_type
909  * as defined by @filter. It copies the asset and thus will not be updated
910  * in time.
911  *
912  * Returns: (transfer full) (element-type GESAsset): The list of
913  * #GESAsset the object contains
914  */
915 GList *
916 ges_project_list_assets (GESProject * project, GType filter)
917 {
918   GList *ret = NULL;
919   GHashTableIter iter;
920   gpointer key, value;
921
922   g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
923
924   g_hash_table_iter_init (&iter, project->priv->assets);
925   while (g_hash_table_iter_next (&iter, &key, &value)) {
926     if (g_type_is_a (ges_asset_get_extractable_type (GES_ASSET (value)),
927             filter))
928       ret = g_list_append (ret, gst_object_ref (value));
929   }
930
931   return ret;
932 }
933
934 /**
935  * ges_project_save:
936  * @project: A #GESProject to save
937  * @timeline: The #GESTimeline to save, it must have been extracted from @project
938  * @uri: The uri where to save @project and @timeline
939  * @formatter_asset: (allow-none): The formatter asset to use or %NULL. If %NULL,
940  * will try to save in the same format as the one from which the timeline as been loaded
941  * or default to the formatter with highest rank
942  * @overwrite: %TRUE to overwrite file if it exists
943  * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
944  *
945  * Save the timeline of @project to @uri. You should make sure that @timeline
946  * is one of the timelines that have been extracted from @project
947  * (using ges_asset_extract (@project);)
948  *
949  * Returns: %TRUE if the project could be save, %FALSE otherwize
950  */
951 gboolean
952 ges_project_save (GESProject * project, GESTimeline * timeline,
953     const gchar * uri, GESAsset * formatter_asset, gboolean overwrite,
954     GError ** error)
955 {
956   GESAsset *tl_asset;
957   gboolean ret = TRUE;
958   GESFormatter *formatter = NULL;
959
960   g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
961   g_return_val_if_fail (formatter_asset == NULL ||
962       g_type_is_a (ges_asset_get_extractable_type (formatter_asset),
963           GES_TYPE_FORMATTER), FALSE);
964   g_return_val_if_fail ((error == NULL || *error == NULL), FALSE);
965
966   tl_asset = ges_extractable_get_asset (GES_EXTRACTABLE (timeline));
967   if (tl_asset == NULL && project->priv->uri == NULL) {
968     GESAsset *asset = ges_asset_cache_lookup (GES_TYPE_PROJECT, uri);
969
970     if (asset) {
971       GST_WARNING_OBJECT (project, "Trying to save project to %s but we already"
972           "have %" GST_PTR_FORMAT " for that uri, can not save", uri, asset);
973       goto out;
974     }
975
976     GST_DEBUG_OBJECT (project, "Timeline %" GST_PTR_FORMAT " has no asset"
977         " we have no uri set, so setting ourself as asset", timeline);
978
979     ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
980   } else if (tl_asset != GES_ASSET (project)) {
981     GST_WARNING_OBJECT (project, "Timeline %" GST_PTR_FORMAT
982         " not created by this project can not save", timeline);
983
984     ret = FALSE;
985     goto out;
986   }
987
988   if (formatter_asset == NULL)
989     formatter_asset = gst_object_ref (ges_formatter_get_default ());
990
991   formatter = GES_FORMATTER (ges_asset_extract (formatter_asset, error));
992   if (formatter == NULL) {
993     GST_WARNING_OBJECT (project, "Could not create the formatter %p %s: %s",
994         formatter_asset, ges_asset_get_id (formatter_asset),
995         (error && *error) ? (*error)->message : "Unknown Error");
996
997     ret = FALSE;
998     goto out;
999   }
1000
1001   ges_project_add_formatter (project, formatter);
1002   ret = ges_formatter_save_to_uri (formatter, timeline, uri, overwrite, error);
1003   if (ret && project->priv->uri == NULL)
1004     ges_project_set_uri (project, uri);
1005
1006 out:
1007   if (formatter_asset)
1008     gst_object_unref (formatter_asset);
1009   ges_project_remove_formatter (project, formatter);
1010
1011   return ret;
1012 }
1013
1014 /**
1015  * ges_project_new:
1016  * @uri: (allow-none): The uri to be set after creating the project.
1017  *
1018  * Creates a new #GESProject and sets its uri to @uri if provided. Note that
1019  * if @uri is not valid or %NULL, the uri of the project will then be set
1020  * the first time you save the project. If you then save the project to
1021  * other locations, it will never be updated again and the first valid URI is
1022  * the URI it will keep refering to.
1023  *
1024  * Returns: A newly created #GESProject
1025  */
1026 GESProject *
1027 ges_project_new (const gchar * uri)
1028 {
1029   gchar *id = (gchar *) uri;
1030   GESProject *project;
1031
1032   if (uri == NULL)
1033     id = g_strdup_printf ("project-%i", nb_projects++);
1034
1035   project = GES_PROJECT (ges_asset_request (GES_TYPE_TIMELINE, id, NULL));
1036
1037   if (project && uri)
1038     ges_project_set_uri (project, uri);
1039
1040   if (uri == NULL)
1041     g_free (id);
1042
1043   return project;
1044 }
1045
1046 /**
1047  * ges_project_load:
1048  * @project: A #GESProject that has an @uri set already
1049  * @timeline: A blank timeline to load @project into
1050  * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1051  *
1052  * Loads @project into @timeline
1053  *
1054  * Returns: %TRUE if the project could be loaded %FALSE otherwize.
1055  */
1056 gboolean
1057 ges_project_load (GESProject * project, GESTimeline * timeline, GError ** error)
1058 {
1059   g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1060   g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1061   g_return_val_if_fail (ges_project_get_uri (project), FALSE);
1062   g_return_val_if_fail (timeline->tracks == NULL, FALSE);
1063
1064   if (!_load_project (project, timeline, error))
1065     return FALSE;
1066
1067   ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1068
1069   return TRUE;
1070 }
1071
1072 /**
1073  * ges_project_get_uri:
1074  * @project: A #GESProject
1075  *
1076  * Retrieve the uri that is currently set on @project
1077  *
1078  * Returns: (transfer full) (nullable): a newly allocated string representing uri.
1079  */
1080 gchar *
1081 ges_project_get_uri (GESProject * project)
1082 {
1083   GESProjectPrivate *priv;
1084
1085   g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1086
1087   priv = project->priv;
1088   if (priv->uri)
1089     return g_strdup (priv->uri);
1090   return NULL;
1091 }
1092
1093 /**
1094  * ges_project_add_encoding_profile:
1095  * @project: A #GESProject
1096  * @profile: A #GstEncodingProfile to add to the project. If a profile with
1097  * the same name already exists, it will be replaced
1098  *
1099  * Adds @profile to the project. It lets you save in what format
1100  * the project has been renders and keep a reference to those formats.
1101  * Also, those formats will be saves to the project file when possible.
1102  *
1103  * Returns: %TRUE if @profile could be added, %FALSE otherwize
1104  */
1105 gboolean
1106 ges_project_add_encoding_profile (GESProject * project,
1107     GstEncodingProfile * profile)
1108 {
1109   GList *tmp;
1110   GESProjectPrivate *priv;
1111
1112   g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1113   g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1114
1115   priv = project->priv;
1116   for (tmp = priv->encoding_profiles; tmp; tmp = tmp->next) {
1117     GstEncodingProfile *tmpprofile = GST_ENCODING_PROFILE (tmp->data);
1118
1119     if (g_strcmp0 (gst_encoding_profile_get_name (tmpprofile),
1120             gst_encoding_profile_get_name (profile)) == 0) {
1121       GST_INFO_OBJECT (project, "Already have profile: %s, replacing it",
1122           gst_encoding_profile_get_name (profile));
1123
1124       gst_object_unref (tmp->data);
1125       tmp->data = gst_object_ref (profile);
1126       return TRUE;
1127     }
1128   }
1129
1130   priv->encoding_profiles = g_list_prepend (priv->encoding_profiles,
1131       gst_object_ref (profile));
1132
1133   return TRUE;
1134 }
1135
1136 /**
1137  * ges_project_list_encoding_profiles:
1138  * @project: A #GESProject
1139  *
1140  * Lists the encoding profile that have been set to @project. The first one
1141  * is the latest added.
1142  *
1143  * Returns: (transfer none) (element-type GstPbutils.EncodingProfile) (allow-none): The
1144  * list of #GstEncodingProfile used in @project
1145  */
1146 const GList *
1147 ges_project_list_encoding_profiles (GESProject * project)
1148 {
1149   g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1150
1151   return project->priv->encoding_profiles;
1152 }
1153
1154 /**
1155  * ges_project_get_loading_assets:
1156  * @project: A #GESProject
1157  *
1158  * Get the assets that are being loaded
1159  *
1160  * Returns: (transfer full) (element-type GES.Asset): A set of loading asset
1161  * that will be added to @project. Note that those Asset are *not* loaded yet,
1162  * and thus can not be used
1163  */
1164 GList *
1165 ges_project_get_loading_assets (GESProject * project)
1166 {
1167   GHashTableIter iter;
1168   gpointer key, value;
1169
1170   GList *ret = NULL;
1171
1172   g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1173
1174   g_hash_table_iter_init (&iter, project->priv->loading_assets);
1175   while (g_hash_table_iter_next (&iter, &key, &value))
1176     ret = g_list_prepend (ret, gst_object_ref (value));
1177
1178   return ret;
1179 }