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