1 /* GStreamer Editing Services
3 * Copyright (C) <2012> Thibault Saunier <thibault.saunier@collabora.com>
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.
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.
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.
23 * @short_description: A GESAsset that is used to manage projects
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:
30 * GESProject *project;
31 * GESTimeline *timeline;
33 * project = ges_project_new ("file:///path/to/a/valid/project/uri");
35 * // Here you can connect to the various signal to get more infos about
36 * // what is happening and recover from errors if possible
39 * timeline = ges_asset_extract (GES_ASSET (project));
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.
49 * In order to add a subproject, the only thing to do is to add the subproject
50 * with to the main project:
53 * ges_project_add_asset (project, GES_ASSET (subproject));
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.
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.
70 #include "ges-internal.h"
72 static GPtrArray *new_paths = NULL;
73 static GHashTable *tried_uris = NULL;
75 struct _GESProjectPrivate
78 /* Set of asset ID being loaded */
79 GHashTable *loading_assets;
80 GHashTable *loaded_with_error;
81 GESAsset *formatter_asset;
87 GList *encoding_profiles;
90 typedef struct EmitLoadedInIdle
93 GESTimeline *timeline;
102 ASSET_REMOVED_SIGNAL,
104 ASSET_LOADING_SIGNAL,
108 G_DEFINE_TYPE_WITH_PRIVATE (GESProject, ges_project, GES_TYPE_ASSET);
110 static guint _signals[LAST_SIGNAL] = { 0 };
112 static guint nb_projects = 0;
114 /* Find the type that implemented the GESExtractable interface */
115 static inline const gchar *
116 _extractable_type_name (GType type)
119 if (g_type_is_a (g_type_parent (type), GES_TYPE_EXTRACTABLE))
120 type = g_type_parent (type);
122 return g_type_name (type);
133 static GParamSpec *_properties[LAST_SIGNAL] = { 0 };
136 _emit_loaded_in_idle (EmitLoadedInIdle * data)
138 g_signal_emit (data->project, _signals[LOADED_SIGNAL], 0, data->timeline);
140 gst_object_unref (data->project);
141 gst_object_unref (data->timeline);
142 g_slice_free (EmitLoadedInIdle, data);
148 ges_project_add_formatter (GESProject * project, GESFormatter * formatter)
150 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
152 ges_formatter_set_project (formatter, project);
153 priv->formatters = g_list_append (priv->formatters, formatter);
155 gst_object_ref_sink (formatter);
159 ges_project_remove_formatter (GESProject * project, GESFormatter * formatter)
162 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
164 for (tmp = priv->formatters; tmp; tmp = tmp->next) {
165 if (tmp->data == formatter) {
166 gst_object_unref (formatter);
167 priv->formatters = g_list_delete_link (priv->formatters, tmp);
175 ges_project_set_uri (GESProject * project, const gchar * uri)
177 GESProjectPrivate *priv;
179 g_return_if_fail (GES_IS_PROJECT (project));
181 priv = project->priv;
183 GST_WARNING_OBJECT (project, "Trying to rest URI, this is prohibited");
189 GST_LOG_OBJECT (project, "Uri should not be NULL");
193 priv->uri = g_strdup (uri);
195 /* We use that URI as ID */
196 ges_asset_set_id (GES_ASSET (project), uri);
202 _load_project (GESProject * project, GESTimeline * timeline, GError ** error)
205 GESProjectPrivate *priv;
206 GESFormatter *formatter;
208 priv = GES_PROJECT (project)->priv;
210 g_signal_emit (project, _signals[LOADING_SIGNAL], 0, timeline);
211 if (priv->uri == NULL) {
213 if (gst_uri_is_valid (ges_asset_get_id (GES_ASSET (project)))) {
214 ges_project_set_uri (project, ges_asset_get_id (GES_ASSET (project)));
215 GST_INFO_OBJECT (project, "Using asset ID %s as URI.", priv->uri);
217 EmitLoadedInIdle *data = g_slice_new (EmitLoadedInIdle);
219 GST_INFO_OBJECT (project, "%s, Loading an empty timeline %s"
220 " as no URI set yet", GST_OBJECT_NAME (timeline),
221 ges_asset_get_id (GES_ASSET (project)));
223 data->timeline = gst_object_ref (timeline);
224 data->project = gst_object_ref (project);
226 /* Make sure the signal is emitted after the functions ends */
227 ges_idle_add ((GSourceFunc) _emit_loaded_in_idle, data, NULL);
232 if (priv->formatter_asset == NULL)
233 priv->formatter_asset = _find_formatter_asset_for_id (priv->uri);
235 if (priv->formatter_asset == NULL) {
236 lerr = g_error_new (GES_ERROR, 0, "Could not find a suitable formatter");
240 formatter = GES_FORMATTER (ges_asset_extract (priv->formatter_asset, &lerr));
242 GST_WARNING_OBJECT (project, "Could not create the formatter: %s",
248 ges_project_add_formatter (GES_PROJECT (project), formatter);
249 ges_formatter_load_from_uri (formatter, timeline, priv->uri, &lerr);
251 GST_WARNING_OBJECT (project, "Could not load the timeline,"
252 " returning: %s", lerr->message);
260 g_propagate_error (error, lerr);
265 _uri_missing_accumulator (GSignalInvocationHint * ihint, GValue * return_accu,
266 const GValue * handler_return, gpointer data)
268 const gchar *ret = g_value_get_string (handler_return);
271 if (!gst_uri_is_valid (ret)) {
272 GST_INFO ("The uri %s was not valid, can not work with it!", ret);
276 g_value_set_string (return_accu, ret);
283 /* GESAsset vmethod implementation */
284 static GESExtractable *
285 ges_project_extract (GESAsset * project, GError ** error)
287 GESTimeline *timeline = g_object_new (GES_TYPE_TIMELINE, NULL);
289 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
290 if (_load_project (GES_PROJECT (project), timeline, error))
291 return GES_EXTRACTABLE (timeline);
293 gst_object_unref (timeline);
298 _add_media_new_paths_recursing (const gchar * value)
301 GFileEnumerator *fenum;
302 GFile *file = g_file_new_for_uri (value);
304 if (!(fenum = g_file_enumerate_children (file,
305 "standard::*", G_FILE_QUERY_INFO_NONE, NULL, NULL))) {
306 GST_INFO ("%s is not a folder", value);
311 GST_INFO ("Adding folder: %s", value);
312 g_ptr_array_add (new_paths, g_strdup (value));
313 info = g_file_enumerator_next_file (fenum, NULL, NULL);
314 while (info != NULL) {
315 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
316 GFile *f = g_file_enumerator_get_child (fenum, info);
317 gchar *uri = g_file_get_uri (f);
319 _add_media_new_paths_recursing (uri);
320 gst_object_unref (f);
323 g_object_unref (info);
324 info = g_file_enumerator_next_file (fenum, NULL, NULL);
328 gst_object_unref (file);
330 gst_object_unref (fenum);
334 ges_add_missing_uri_relocation_uri (const gchar * uri, gboolean recurse)
336 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
338 if (new_paths == NULL)
339 new_paths = g_ptr_array_new_with_free_func (g_free);
342 _add_media_new_paths_recursing (uri);
344 g_ptr_array_add (new_paths, g_strdup (uri));
350 ges_missing_uri_default (GESProject * self, GError * error,
351 GESAsset * wrong_asset)
354 const gchar *old_uri = ges_asset_get_id (wrong_asset);
355 gchar *new_id = NULL;
357 if (ges_asset_request_id_update (wrong_asset, &new_id, error) && new_id) {
358 GST_INFO_OBJECT (self, "Returned guessed new ID: %s", new_id);
363 if (new_paths == NULL)
366 if (tried_uris == NULL)
367 tried_uris = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
369 for (i = 0; i < new_paths->len; i++) {
370 gchar *basename, *res;
372 basename = g_path_get_basename (old_uri);
373 res = g_build_filename (new_paths->pdata[i], basename, NULL);
376 if (g_strcmp0 (old_uri, res) == 0) {
377 g_hash_table_add (tried_uris, res);
378 } else if (g_hash_table_lookup (tried_uris, res)) {
379 GST_DEBUG_OBJECT (self, "File already tried: %s", res);
382 g_hash_table_add (tried_uris, g_strdup (res));
383 GST_DEBUG_OBJECT (self, "Trying: %s\n", res);
392 ges_uri_asset_try_update_id (GError * error, GESAsset * wrong_asset)
394 return ges_missing_uri_default (NULL, error, wrong_asset);
398 ges_uri_assets_validate_uri (const gchar * nid)
401 g_hash_table_remove (tried_uris, nid);
404 /* GObject vmethod implementation */
406 _dispose (GObject * object)
408 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
411 g_hash_table_unref (priv->assets);
412 if (priv->loading_assets)
413 g_hash_table_unref (priv->loading_assets);
414 if (priv->loaded_with_error)
415 g_hash_table_unref (priv->loaded_with_error);
416 if (priv->formatter_asset)
417 gst_object_unref (priv->formatter_asset);
419 while (priv->formatters)
420 ges_project_remove_formatter (GES_PROJECT (object), priv->formatters->data);
422 G_OBJECT_CLASS (ges_project_parent_class)->dispose (object);
426 _finalize (GObject * object)
428 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
432 g_list_free_full (priv->encoding_profiles, g_object_unref);
434 G_OBJECT_CLASS (ges_project_parent_class)->finalize (object);
438 _get_property (GESProject * project, guint property_id,
439 GValue * value, GParamSpec * pspec)
441 GESProjectPrivate *priv = project->priv;
443 switch (property_id) {
445 g_value_set_string (value, priv->uri);
448 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
453 _set_property (GESProject * project, guint property_id,
454 const GValue * value, GParamSpec * pspec)
456 switch (property_id) {
458 project->priv->uri = g_value_dup_string (value);
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
466 ges_project_class_init (GESProjectClass * klass)
468 GObjectClass *object_class = G_OBJECT_CLASS (klass);
470 klass->asset_added = NULL;
471 klass->missing_uri = ges_missing_uri_default;
472 klass->loading_error = NULL;
473 klass->asset_removed = NULL;
474 object_class->get_property = (GObjectGetPropertyFunc) _get_property;
475 object_class->set_property = (GObjectSetPropertyFunc) _set_property;
480 * The location of the project to use.
482 _properties[PROP_URI] = g_param_spec_string ("uri", "URI",
483 "uri of the project", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
485 g_object_class_install_properties (object_class, PROP_LAST, _properties);
488 * GESProject::asset-added:
489 * @project: the #GESProject
490 * @asset: The #GESAsset that has been added to @project
492 _signals[ASSET_ADDED_SIGNAL] =
493 g_signal_new ("asset-added", G_TYPE_FROM_CLASS (klass),
494 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_added),
495 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
498 * GESProject::asset-loading:
499 * @project: the #GESProject
500 * @asset: The #GESAsset that started loading
504 _signals[ASSET_LOADING_SIGNAL] =
505 g_signal_new ("asset-loading", G_TYPE_FROM_CLASS (klass),
506 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_loading),
507 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
510 * GESProject::asset-removed:
511 * @project: the #GESProject
512 * @asset: The #GESAsset that has been removed from @project
514 _signals[ASSET_REMOVED_SIGNAL] =
515 g_signal_new ("asset-removed", G_TYPE_FROM_CLASS (klass),
516 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_removed),
517 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
520 * GESProject::loading:
521 * @project: the #GESProject that is starting to load a timeline
522 * @timeline: The #GESTimeline that started loading
526 _signals[LOADING_SIGNAL] =
527 g_signal_new ("loading", G_TYPE_FROM_CLASS (klass),
528 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loading),
529 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
530 1, GES_TYPE_TIMELINE);
533 * GESProject::loaded:
534 * @project: the #GESProject that is done loading a timeline.
535 * @timeline: The #GESTimeline that completed loading
537 _signals[LOADED_SIGNAL] =
538 g_signal_new ("loaded", G_TYPE_FROM_CLASS (klass),
539 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loaded),
540 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
541 1, GES_TYPE_TIMELINE);
544 * GESProject::missing-uri:
545 * @project: the #GESProject reporting that a file has moved
546 * @error: The error that happened
547 * @wrong_asset: The asset with the wrong ID, you should us it and its content
548 * only to find out what the new location is.
552 * source_moved_cb (GESProject *project, GError *error, GESAsset *asset_with_error)
554 * return g_strdup ("file:///the/new/uri.ogg");
558 * main (int argc, gchar ** argv)
560 * GESTimeline *timeline;
561 * GESProject *project = ges_project_new ("file:///some/uri.xges");
563 * g_signal_connect (project, "missing-uri", source_moved_cb, NULL);
564 * timeline = ges_asset_extract (GES_ASSET (project));
568 * Returns: (transfer full) (allow-none): The new URI of @wrong_asset
570 _signals[MISSING_URI_SIGNAL] =
571 g_signal_new ("missing-uri", G_TYPE_FROM_CLASS (klass),
572 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, missing_uri),
573 _uri_missing_accumulator, NULL, g_cclosure_marshal_generic,
574 G_TYPE_STRING, 2, G_TYPE_ERROR, GES_TYPE_ASSET);
577 * GESProject::error-loading-asset:
578 * @project: the #GESProject on which a problem happend when creted a #GESAsset
579 * @error: The #GError defining the error that occured, might be %NULL
580 * @id: The @id of the asset that failed loading
581 * @extractable_type: The @extractable_type of the asset that
584 * Informs you that a #GESAsset could not be created. In case of
585 * missing GStreamer plugins, the error will be set to #GST_CORE_ERROR
586 * #GST_CORE_ERROR_MISSING_PLUGIN
588 _signals[ERROR_LOADING_ASSET] =
589 g_signal_new ("error-loading-asset", G_TYPE_FROM_CLASS (klass),
590 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, loading_error),
591 NULL, NULL, g_cclosure_marshal_generic,
592 G_TYPE_NONE, 3, G_TYPE_ERROR, G_TYPE_STRING, G_TYPE_GTYPE);
594 object_class->dispose = _dispose;
595 object_class->finalize = _finalize;
597 GES_ASSET_CLASS (klass)->extract = ges_project_extract;
601 ges_project_init (GESProject * project)
603 GESProjectPrivate *priv = project->priv =
604 ges_project_get_instance_private (project);
607 priv->formatters = NULL;
608 priv->formatter_asset = NULL;
609 priv->encoding_profiles = NULL;
610 priv->assets = g_hash_table_new_full (g_str_hash, g_str_equal,
611 g_free, gst_object_unref);
612 priv->loading_assets = g_hash_table_new_full (g_str_hash, g_str_equal,
613 g_free, gst_object_unref);
614 priv->loaded_with_error = g_hash_table_new_full (g_str_hash, g_str_equal,
619 ges_project_internal_extractable_type_id (GType extractable_type,
622 return g_strdup_printf ("%s:%s", _extractable_type_name (extractable_type),
627 ges_project_internal_asset_id (GESAsset * asset)
630 ges_project_internal_extractable_type_id (ges_asset_get_extractable_type
631 (asset), ges_asset_get_id (asset));
635 _send_error_loading_asset (GESProject * project, GESAsset * asset,
638 gchar *internal_id = ges_project_internal_asset_id (asset);
639 const gchar *id = ges_asset_get_id (asset);
641 GST_DEBUG_OBJECT (project, "Sending error loading asset for %s", id);
642 g_hash_table_remove (project->priv->loading_assets, internal_id);
643 g_hash_table_add (project->priv->loaded_with_error, internal_id);
644 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, error,
645 id, ges_asset_get_extractable_type (asset));
649 ges_project_try_updating_id (GESProject * project, GESAsset * asset,
652 gchar *new_id = NULL;
656 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
657 g_return_val_if_fail (GES_IS_ASSET (asset), NULL);
658 g_return_val_if_fail (error, NULL);
660 id = ges_asset_get_id (asset);
661 GST_DEBUG_OBJECT (project, "Try to proxy %s", id);
662 if (ges_asset_request_id_update (asset, &new_id, error) == FALSE) {
663 GST_DEBUG_OBJECT (project, "Type: %s can not be proxied for id: %s "
664 "and error: %s", g_type_name (G_OBJECT_TYPE (asset)), id,
666 _send_error_loading_asset (project, asset, error);
671 /* Always send the MISSING_URI signal if requesting new ID is possible
672 * so that subclasses of GESProject are aware of the missing-uri */
673 g_signal_emit (project, _signals[MISSING_URI_SIGNAL], 0, error, asset,
677 GST_DEBUG_OBJECT (project, "new id found: %s", new_id);
678 if (!ges_asset_try_proxy (asset, new_id)) {
683 GST_DEBUG_OBJECT (project, "No new id found for %s", id);
686 internal_id = ges_project_internal_asset_id (asset);
687 g_hash_table_remove (project->priv->loading_assets, internal_id);
688 g_free (internal_id);
691 _send_error_loading_asset (project, asset, error);
698 new_asset_cb (GESAsset * source, GAsyncResult * res, GESProject * project)
700 GError *error = NULL;
701 gchar *possible_id = NULL;
702 GESAsset *asset = ges_asset_request_finish (res, &error);
705 possible_id = ges_project_try_updating_id (project, source, error);
706 g_clear_error (&error);
708 if (possible_id == NULL)
711 ges_project_create_asset (project, possible_id,
712 ges_asset_get_extractable_type (source));
714 g_free (possible_id);
718 ges_asset_set_proxy (NULL, asset);
719 ges_project_add_asset (project, asset);
721 gst_object_unref (asset);
725 * ges_project_set_loaded:
726 * @project: The #GESProject from which to emit the "project-loaded" signal
728 * Emits the "loaded" signal. This method should be called by sublasses when
729 * the project is fully loaded.
731 * Returns: %TRUE if the signale could be emitted %FALSE otherwize
734 ges_project_set_loaded (GESProject * project, GESFormatter * formatter)
736 GST_INFO_OBJECT (project, "Emit project loaded");
737 if (GST_STATE (formatter->timeline) < GST_STATE_PAUSED) {
738 timeline_fill_gaps (formatter->timeline);
740 ges_timeline_commit (formatter->timeline);
743 g_signal_emit (project, _signals[LOADED_SIGNAL], 0, formatter->timeline);
745 /* We are now done with that formatter */
746 ges_project_remove_formatter (project, formatter);
751 ges_project_add_loading_asset (GESProject * project, GType extractable_type,
756 if ((asset = ges_asset_cache_lookup (extractable_type, id))) {
757 if (g_hash_table_insert (project->priv->loading_assets,
758 ges_project_internal_asset_id (asset), gst_object_ref (asset)))
759 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
763 /**************************************
765 * API Implementation *
767 **************************************/
770 * ges_project_create_asset:
771 * @project: A #GESProject
772 * @id: (allow-none): The id of the asset to create and add to @project
773 * @extractable_type: The #GType of the asset to create
775 * Create and add a #GESAsset to @project. You should connect to the
776 * "asset-added" signal to get the asset when it finally gets added to
779 * Returns: %TRUE if the asset started to be added %FALSE it was already
783 ges_project_create_asset (GESProject * project, const gchar * id,
784 GType extractable_type)
787 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
788 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
792 id = g_type_name (extractable_type);
793 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
795 if (g_hash_table_lookup (project->priv->assets, internal_id) ||
796 g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
797 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
799 g_free (internal_id);
802 g_free (internal_id);
804 /* TODO Add a GCancellable somewhere in our API */
805 ges_asset_request_async (extractable_type, id, NULL,
806 (GAsyncReadyCallback) new_asset_cb, project);
807 ges_project_add_loading_asset (project, extractable_type, id);
813 * ges_project_create_asset_sync:
814 * @project: A #GESProject
815 * @id: (allow-none): The id of the asset to create and add to @project
816 * @extractable_type: The #GType of the asset to create
817 * @error: A #GError to be set in case of error
819 * Create and add a #GESAsset to @project. You should connect to the
820 * "asset-added" signal to get the asset when it finally gets added to
823 * Returns: (transfer full) (nullable): The newly created #GESAsset or %NULL.
826 ges_project_create_asset_sync (GESProject * project, const gchar * id,
827 GType extractable_type, GError ** error)
830 gchar *possible_id = NULL, *internal_id;
831 gboolean retry = TRUE;
833 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
834 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
838 id = g_type_name (extractable_type);
840 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
841 if ((asset = g_hash_table_lookup (project->priv->assets, internal_id))) {
842 g_free (internal_id);
845 } else if (g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
846 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
847 g_free (internal_id);
852 /* TODO Add a GCancellable somewhere in our API */
855 if (g_type_is_a (extractable_type, GES_TYPE_URI_CLIP)) {
856 asset = GES_ASSET (ges_uri_clip_asset_request_sync (id, error));
858 asset = ges_asset_request (extractable_type, id, error);
864 ges_project_internal_extractable_type_id (extractable_type, id);
865 if ((!g_hash_table_lookup (project->priv->assets, internal_id)))
866 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
867 g_free (internal_id);
870 g_free (possible_id);
871 ges_uri_assets_validate_uri (id);
878 tmpasset = ges_asset_cache_lookup (extractable_type, id);
879 possible_id = ges_project_try_updating_id (project, tmpasset, *error);
881 if (possible_id == NULL) {
882 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, tmpasset);
883 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, *error, id,
889 g_clear_error (error);
895 if (!ges_asset_get_proxy_target (asset))
896 ges_asset_set_proxy (NULL, asset);
898 ges_project_add_asset (project, asset);
904 * ges_project_add_asset:
905 * @project: A #GESProject
906 * @asset: (transfer none): A #GESAsset to add to @project
908 * Adds a #GESAsset to @project, the project will keep a reference on
911 * Returns: %TRUE if the asset could be added %FALSE it was already
915 ges_project_add_asset (GESProject * project, GESAsset * asset)
918 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
920 internal_id = ges_project_internal_asset_id (asset);
921 if (g_hash_table_lookup (project->priv->assets, internal_id)) {
922 g_free (internal_id);
926 g_hash_table_insert (project->priv->assets, internal_id,
927 gst_object_ref (asset));
928 g_hash_table_remove (project->priv->loading_assets, internal_id);
929 GST_DEBUG_OBJECT (project, "Asset added: %s", ges_asset_get_id (asset));
930 g_signal_emit (project, _signals[ASSET_ADDED_SIGNAL], 0, asset);
936 * ges_project_remove_asset:
937 * @project: A #GESProject
938 * @asset: (transfer none): A #GESAsset to remove from @project
940 * remove a @asset to from @project.
942 * Returns: %TRUE if the asset could be removed %FALSE otherwise
945 ges_project_remove_asset (GESProject * project, GESAsset * asset)
950 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
952 internal_id = ges_project_internal_asset_id (asset);
953 ret = g_hash_table_remove (project->priv->assets, internal_id);
954 g_free (internal_id);
955 g_signal_emit (project, _signals[ASSET_REMOVED_SIGNAL], 0, asset);
961 * ges_project_get_asset:
962 * @project: A #GESProject
963 * @id: The id of the asset to retrieve
964 * @extractable_type: The extractable_type of the asset
965 * to retrieve from @object
967 * Returns: (transfer full) (allow-none): The #GESAsset with
968 * @id or %NULL if no asset with @id as an ID
971 ges_project_get_asset (GESProject * project, const gchar * id,
972 GType extractable_type)
977 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
978 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
981 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
982 asset = g_hash_table_lookup (project->priv->assets, internal_id);
983 g_free (internal_id);
986 return gst_object_ref (asset);
992 * ges_project_list_assets:
993 * @project: A #GESProject
994 * @filter: Type of assets to list, `GES_TYPE_EXTRACTABLE` will list
997 * List all @asset contained in @project filtering per extractable_type
998 * as defined by @filter. It copies the asset and thus will not be updated
1001 * Returns: (transfer full) (element-type GESAsset): The list of
1002 * #GESAsset the object contains
1005 ges_project_list_assets (GESProject * project, GType filter)
1008 GHashTableIter iter;
1009 gpointer key, value;
1011 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1013 g_hash_table_iter_init (&iter, project->priv->assets);
1014 while (g_hash_table_iter_next (&iter, &key, &value)) {
1015 if (g_type_is_a (ges_asset_get_extractable_type (GES_ASSET (value)),
1017 ret = g_list_append (ret, gst_object_ref (value));
1025 * @project: A #GESProject to save
1026 * @timeline: The #GESTimeline to save, it must have been extracted from @project
1027 * @uri: The uri where to save @project and @timeline
1028 * @formatter_asset: (allow-none): The formatter asset to use or %NULL. If %NULL,
1029 * will try to save in the same format as the one from which the timeline as been loaded
1030 * or default to the formatter with highest rank
1031 * @overwrite: %TRUE to overwrite file if it exists
1032 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1034 * Save the timeline of @project to @uri. You should make sure that @timeline
1035 * is one of the timelines that have been extracted from @project
1036 * (using ges_asset_extract (@project);)
1038 * Returns: %TRUE if the project could be save, %FALSE otherwize
1041 ges_project_save (GESProject * project, GESTimeline * timeline,
1042 const gchar * uri, GESAsset * formatter_asset, gboolean overwrite,
1046 gboolean ret = TRUE;
1047 GESFormatter *formatter = NULL;
1049 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1050 g_return_val_if_fail (formatter_asset == NULL ||
1051 g_type_is_a (ges_asset_get_extractable_type (formatter_asset),
1052 GES_TYPE_FORMATTER), FALSE);
1053 g_return_val_if_fail ((error == NULL || *error == NULL), FALSE);
1055 tl_asset = ges_extractable_get_asset (GES_EXTRACTABLE (timeline));
1056 if (tl_asset == NULL && project->priv->uri == NULL) {
1057 GESAsset *asset = ges_asset_cache_lookup (GES_TYPE_PROJECT, uri);
1060 GST_WARNING_OBJECT (project, "Trying to save project to %s but we already"
1061 "have %" GST_PTR_FORMAT " for that uri, can not save", uri, asset);
1065 GST_DEBUG_OBJECT (project, "Timeline %" GST_PTR_FORMAT " has no asset"
1066 " we have no uri set, so setting ourself as asset", timeline);
1068 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1069 } else if (tl_asset != GES_ASSET (project)) {
1070 GST_WARNING_OBJECT (project, "Timeline %" GST_PTR_FORMAT
1071 " not created by this project can not save", timeline);
1077 if (formatter_asset == NULL)
1078 formatter_asset = gst_object_ref (ges_formatter_get_default ());
1080 formatter = GES_FORMATTER (ges_asset_extract (formatter_asset, error));
1081 if (formatter == NULL) {
1082 GST_WARNING_OBJECT (project, "Could not create the formatter %p %s: %s",
1083 formatter_asset, ges_asset_get_id (formatter_asset),
1084 (error && *error) ? (*error)->message : "Unknown Error");
1090 ges_project_add_formatter (project, formatter);
1091 ret = ges_formatter_save_to_uri (formatter, timeline, uri, overwrite, error);
1092 if (ret && project->priv->uri == NULL)
1093 ges_project_set_uri (project, uri);
1096 if (formatter_asset)
1097 gst_object_unref (formatter_asset);
1098 ges_project_remove_formatter (project, formatter);
1105 * @uri: (allow-none): The uri to be set after creating the project.
1107 * Creates a new #GESProject and sets its uri to @uri if provided. Note that
1108 * if @uri is not valid or %NULL, the uri of the project will then be set
1109 * the first time you save the project. If you then save the project to
1110 * other locations, it will never be updated again and the first valid URI is
1111 * the URI it will keep refering to.
1113 * Returns: A newly created #GESProject
1116 ges_project_new (const gchar * uri)
1118 gchar *id = (gchar *) uri;
1119 GESProject *project;
1122 id = g_strdup_printf ("project-%i", nb_projects++);
1124 project = GES_PROJECT (ges_asset_request (GES_TYPE_TIMELINE, id, NULL));
1127 ges_project_set_uri (project, uri);
1137 * @project: A #GESProject that has an @uri set already
1138 * @timeline: A blank timeline to load @project into
1139 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1141 * Loads @project into @timeline
1143 * Returns: %TRUE if the project could be loaded %FALSE otherwize.
1146 ges_project_load (GESProject * project, GESTimeline * timeline, GError ** error)
1148 g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1149 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1150 g_return_val_if_fail (ges_project_get_uri (project), FALSE);
1151 g_return_val_if_fail (timeline->tracks == NULL, FALSE);
1153 if (!_load_project (project, timeline, error))
1156 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1162 * ges_project_get_uri:
1163 * @project: A #GESProject
1165 * Retrieve the uri that is currently set on @project
1167 * Returns: (transfer full) (nullable): a newly allocated string representing uri.
1170 ges_project_get_uri (GESProject * project)
1172 GESProjectPrivate *priv;
1174 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1176 priv = project->priv;
1178 return g_strdup (priv->uri);
1183 * ges_project_add_encoding_profile:
1184 * @project: A #GESProject
1185 * @profile: A #GstEncodingProfile to add to the project. If a profile with
1186 * the same name already exists, it will be replaced
1188 * Adds @profile to the project. It lets you save in what format
1189 * the project has been renders and keep a reference to those formats.
1190 * Also, those formats will be saves to the project file when possible.
1192 * Returns: %TRUE if @profile could be added, %FALSE otherwize
1195 ges_project_add_encoding_profile (GESProject * project,
1196 GstEncodingProfile * profile)
1199 GESProjectPrivate *priv;
1201 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1202 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1204 priv = project->priv;
1205 for (tmp = priv->encoding_profiles; tmp; tmp = tmp->next) {
1206 GstEncodingProfile *tmpprofile = GST_ENCODING_PROFILE (tmp->data);
1208 if (g_strcmp0 (gst_encoding_profile_get_name (tmpprofile),
1209 gst_encoding_profile_get_name (profile)) == 0) {
1210 GST_INFO_OBJECT (project, "Already have profile: %s, replacing it",
1211 gst_encoding_profile_get_name (profile));
1213 gst_object_unref (tmp->data);
1214 tmp->data = gst_object_ref (profile);
1219 priv->encoding_profiles = g_list_prepend (priv->encoding_profiles,
1220 gst_object_ref (profile));
1226 * ges_project_list_encoding_profiles:
1227 * @project: A #GESProject
1229 * Lists the encoding profile that have been set to @project. The first one
1230 * is the latest added.
1232 * Returns: (transfer none) (element-type GstPbutils.EncodingProfile) (allow-none): The
1233 * list of #GstEncodingProfile used in @project
1236 ges_project_list_encoding_profiles (GESProject * project)
1238 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1240 return project->priv->encoding_profiles;
1244 * ges_project_get_loading_assets:
1245 * @project: A #GESProject
1247 * Get the assets that are being loaded
1249 * Returns: (transfer full) (element-type GES.Asset): A set of loading asset
1250 * that will be added to @project. Note that those Asset are *not* loaded yet,
1251 * and thus can not be used
1254 ges_project_get_loading_assets (GESProject * project)
1256 GHashTableIter iter;
1257 gpointer key, value;
1261 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1263 g_hash_table_iter_init (&iter, project->priv->loading_assets);
1264 while (g_hash_table_iter_next (&iter, &key, &value))
1265 ret = g_list_prepend (ret, gst_object_ref (value));