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;
103 ASSET_REMOVED_SIGNAL,
105 ASSET_LOADING_SIGNAL,
109 G_DEFINE_TYPE_WITH_PRIVATE (GESProject, ges_project, GES_TYPE_ASSET);
111 static guint _signals[LAST_SIGNAL] = { 0 };
113 static guint nb_projects = 0;
115 /* Find the type that implemented the GESExtractable interface */
116 static inline const gchar *
117 _extractable_type_name (GType type)
120 if (g_type_is_a (g_type_parent (type), GES_TYPE_EXTRACTABLE))
121 type = g_type_parent (type);
123 return g_type_name (type);
134 static GParamSpec *_properties[LAST_SIGNAL] = { 0 };
137 _emit_loaded_in_idle (EmitLoadedInIdle * data)
139 g_signal_emit (data->project, _signals[LOADED_SIGNAL], 0, data->timeline);
141 gst_object_unref (data->project);
142 gst_object_unref (data->timeline);
143 g_slice_free (EmitLoadedInIdle, data);
149 * ges_project_add_formatter:
150 * @project: The project to add a formatter to
151 * @formatter: A formatter used by @project
153 * Adds a formatter as used to load @project
158 ges_project_add_formatter (GESProject * project, GESFormatter * formatter)
160 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
162 ges_formatter_set_project (formatter, project);
163 priv->formatters = g_list_append (priv->formatters, formatter);
165 gst_object_ref_sink (formatter);
169 ges_project_remove_formatter (GESProject * project, GESFormatter * formatter)
172 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
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);
185 ges_project_set_uri (GESProject * project, const gchar * uri)
187 GESProjectPrivate *priv;
189 g_return_if_fail (GES_IS_PROJECT (project));
191 priv = project->priv;
193 GST_WARNING_OBJECT (project, "Trying to rest URI, this is prohibited");
199 GST_LOG_OBJECT (project, "Uri should not be NULL");
203 priv->uri = g_strdup (uri);
205 /* We use that URI as ID */
206 ges_asset_set_id (GES_ASSET (project), uri);
212 _load_project (GESProject * project, GESTimeline * timeline, GError ** error)
215 GESProjectPrivate *priv;
216 GESFormatter *formatter;
218 priv = GES_PROJECT (project)->priv;
220 g_signal_emit (project, _signals[LOADING_SIGNAL], 0, timeline);
221 if (priv->uri == NULL) {
223 if (gst_uri_is_valid (ges_asset_get_id (GES_ASSET (project)))) {
224 ges_project_set_uri (project, ges_asset_get_id (GES_ASSET (project)));
225 GST_INFO_OBJECT (project, "Using asset ID %s as URI.", priv->uri);
227 EmitLoadedInIdle *data = g_slice_new (EmitLoadedInIdle);
229 GST_INFO_OBJECT (project, "%s, Loading an empty timeline %s"
230 " as no URI set yet", GST_OBJECT_NAME (timeline),
231 ges_asset_get_id (GES_ASSET (project)));
233 data->timeline = gst_object_ref (timeline);
234 data->project = gst_object_ref (project);
236 /* Make sure the signal is emitted after the functions ends */
237 ges_idle_add ((GSourceFunc) _emit_loaded_in_idle, data, NULL);
242 if (priv->formatter_asset == NULL)
243 priv->formatter_asset = _find_formatter_asset_for_id (priv->uri);
245 if (priv->formatter_asset == NULL) {
246 lerr = g_error_new (GES_ERROR, 0, "Could not find a suitable formatter");
250 formatter = GES_FORMATTER (ges_asset_extract (priv->formatter_asset, &lerr));
252 GST_WARNING_OBJECT (project, "Could not create the formatter: %s",
258 ges_project_add_formatter (GES_PROJECT (project), formatter);
259 ges_formatter_load_from_uri (formatter, timeline, priv->uri, &lerr);
261 GST_WARNING_OBJECT (project, "Could not load the timeline,"
262 " returning: %s", lerr->message);
270 g_propagate_error (error, lerr);
275 _uri_missing_accumulator (GSignalInvocationHint * ihint, GValue * return_accu,
276 const GValue * handler_return, gpointer data)
278 const gchar *ret = g_value_get_string (handler_return);
281 if (!gst_uri_is_valid (ret)) {
282 GST_INFO ("The uri %s was not valid, can not work with it!", ret);
286 g_value_set_string (return_accu, ret);
293 /* GESAsset vmethod implementation */
294 static GESExtractable *
295 ges_project_extract (GESAsset * project, GError ** error)
297 GESTimeline *timeline = g_object_new (GES_TYPE_TIMELINE, NULL);
299 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
300 if (_load_project (GES_PROJECT (project), timeline, error))
301 return GES_EXTRACTABLE (timeline);
303 gst_object_unref (timeline);
308 _add_media_new_paths_recursing (const gchar * value)
311 GFileEnumerator *fenum;
312 GFile *file = g_file_new_for_uri (value);
314 if (!(fenum = g_file_enumerate_children (file,
315 "standard::*", G_FILE_QUERY_INFO_NONE, NULL, NULL))) {
316 GST_INFO ("%s is not a folder", value);
321 GST_INFO ("Adding folder: %s", value);
322 g_ptr_array_add (new_paths, g_strdup (value));
323 info = g_file_enumerator_next_file (fenum, NULL, NULL);
324 while (info != NULL) {
325 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
326 GFile *f = g_file_enumerator_get_child (fenum, info);
327 gchar *uri = g_file_get_uri (f);
329 _add_media_new_paths_recursing (uri);
330 gst_object_unref (f);
333 g_object_unref (info);
334 info = g_file_enumerator_next_file (fenum, NULL, NULL);
338 gst_object_unref (file);
340 gst_object_unref (fenum);
344 ges_add_missing_uri_relocation_uri (const gchar * uri, gboolean recurse)
346 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
348 if (new_paths == NULL)
349 new_paths = g_ptr_array_new_with_free_func (g_free);
352 _add_media_new_paths_recursing (uri);
354 g_ptr_array_add (new_paths, g_strdup (uri));
360 ges_missing_uri_default (GESProject * self, GError * error,
361 GESAsset * wrong_asset)
364 const gchar *old_uri = ges_asset_get_id (wrong_asset);
365 gchar *new_id = NULL;
367 if (ges_asset_request_id_update (wrong_asset, &new_id, error) && new_id) {
368 GST_INFO_OBJECT (self, "Returned guessed new ID: %s", new_id);
373 if (new_paths == NULL)
376 if (tried_uris == NULL)
377 tried_uris = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
379 for (i = 0; i < new_paths->len; i++) {
380 gchar *basename, *res;
382 basename = g_path_get_basename (old_uri);
383 res = g_build_filename (new_paths->pdata[i], basename, NULL);
386 if (g_strcmp0 (old_uri, res) == 0) {
387 g_hash_table_add (tried_uris, res);
388 } else if (g_hash_table_lookup (tried_uris, res)) {
389 GST_DEBUG_OBJECT (self, "File already tried: %s", res);
392 g_hash_table_add (tried_uris, g_strdup (res));
393 GST_DEBUG_OBJECT (self, "Trying: %s\n", res);
402 ges_uri_asset_try_update_id (GError * error, GESAsset * wrong_asset)
404 return ges_missing_uri_default (NULL, error, wrong_asset);
408 ges_uri_assets_validate_uri (const gchar * nid)
411 g_hash_table_remove (tried_uris, nid);
414 /* GObject vmethod implementation */
416 _dispose (GObject * object)
418 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
421 g_hash_table_unref (priv->assets);
422 if (priv->loading_assets)
423 g_hash_table_unref (priv->loading_assets);
424 if (priv->loaded_with_error)
425 g_hash_table_unref (priv->loaded_with_error);
426 if (priv->formatter_asset)
427 gst_object_unref (priv->formatter_asset);
429 while (priv->formatters)
430 ges_project_remove_formatter (GES_PROJECT (object), priv->formatters->data);
432 G_OBJECT_CLASS (ges_project_parent_class)->dispose (object);
436 _finalize (GObject * object)
438 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
442 g_list_free_full (priv->encoding_profiles, g_object_unref);
444 G_OBJECT_CLASS (ges_project_parent_class)->finalize (object);
448 _get_property (GESProject * project, guint property_id,
449 GValue * value, GParamSpec * pspec)
451 GESProjectPrivate *priv = project->priv;
453 switch (property_id) {
455 g_value_set_string (value, priv->uri);
458 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
463 _set_property (GESProject * project, guint property_id,
464 const GValue * value, GParamSpec * pspec)
466 switch (property_id) {
468 project->priv->uri = g_value_dup_string (value);
471 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
476 ges_project_class_init (GESProjectClass * klass)
478 GObjectClass *object_class = G_OBJECT_CLASS (klass);
480 klass->asset_added = NULL;
481 klass->missing_uri = ges_missing_uri_default;
482 klass->loading_error = NULL;
483 klass->asset_removed = NULL;
484 object_class->get_property = (GObjectGetPropertyFunc) _get_property;
485 object_class->set_property = (GObjectSetPropertyFunc) _set_property;
490 * The location of the project to use.
492 _properties[PROP_URI] = g_param_spec_string ("uri", "URI",
493 "uri of the project", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
495 g_object_class_install_properties (object_class, PROP_LAST, _properties);
498 * GESProject::asset-added:
499 * @project: the #GESProject
500 * @asset: The #GESAsset that has been added to @project
502 _signals[ASSET_ADDED_SIGNAL] =
503 g_signal_new ("asset-added", G_TYPE_FROM_CLASS (klass),
504 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_added),
505 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
508 * GESProject::asset-loading:
509 * @project: the #GESProject
510 * @asset: The #GESAsset that started loading
514 _signals[ASSET_LOADING_SIGNAL] =
515 g_signal_new ("asset-loading", G_TYPE_FROM_CLASS (klass),
516 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_loading),
517 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
520 * GESProject::asset-removed:
521 * @project: the #GESProject
522 * @asset: The #GESAsset that has been removed from @project
524 _signals[ASSET_REMOVED_SIGNAL] =
525 g_signal_new ("asset-removed", G_TYPE_FROM_CLASS (klass),
526 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_removed),
527 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
530 * GESProject::loading:
531 * @project: the #GESProject that is starting to load a timeline
532 * @timeline: The #GESTimeline that started loading
536 _signals[LOADING_SIGNAL] =
537 g_signal_new ("loading", G_TYPE_FROM_CLASS (klass),
538 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loading),
539 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
540 1, GES_TYPE_TIMELINE);
543 * GESProject::loaded:
544 * @project: the #GESProject that is done loading a timeline.
545 * @timeline: The #GESTimeline that completed loading
547 _signals[LOADED_SIGNAL] =
548 g_signal_new ("loaded", G_TYPE_FROM_CLASS (klass),
549 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loaded),
550 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
551 1, GES_TYPE_TIMELINE);
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.
562 * source_moved_cb (GESProject *project, GError *error, GESAsset *asset_with_error)
564 * return g_strdup ("file:///the/new/uri.ogg");
568 * main (int argc, gchar ** argv)
570 * GESTimeline *timeline;
571 * GESProject *project = ges_project_new ("file:///some/uri.xges");
573 * g_signal_connect (project, "missing-uri", source_moved_cb, NULL);
574 * timeline = ges_asset_extract (GES_ASSET (project));
578 * Returns: (transfer full) (allow-none): The new URI of @wrong_asset
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, g_cclosure_marshal_generic,
584 G_TYPE_STRING, 2, G_TYPE_ERROR, GES_TYPE_ASSET);
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
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
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, g_cclosure_marshal_generic,
602 G_TYPE_NONE, 3, G_TYPE_ERROR, G_TYPE_STRING, G_TYPE_GTYPE);
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
612 _signals[ERROR_LOADING] =
613 g_signal_new ("error-loading", G_TYPE_FROM_CLASS (klass),
614 G_SIGNAL_RUN_LAST, 0,
615 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GES_TYPE_TIMELINE,
618 object_class->dispose = _dispose;
619 object_class->finalize = _finalize;
621 GES_ASSET_CLASS (klass)->extract = ges_project_extract;
625 ges_project_init (GESProject * project)
627 GESProjectPrivate *priv = project->priv =
628 ges_project_get_instance_private (project);
631 priv->formatters = NULL;
632 priv->formatter_asset = NULL;
633 priv->encoding_profiles = NULL;
634 priv->assets = g_hash_table_new_full (g_str_hash, g_str_equal,
635 g_free, gst_object_unref);
636 priv->loading_assets = g_hash_table_new_full (g_str_hash, g_str_equal,
637 g_free, gst_object_unref);
638 priv->loaded_with_error = g_hash_table_new_full (g_str_hash, g_str_equal,
643 ges_project_internal_extractable_type_id (GType extractable_type,
646 return g_strdup_printf ("%s:%s", _extractable_type_name (extractable_type),
651 ges_project_internal_asset_id (GESAsset * asset)
654 ges_project_internal_extractable_type_id (ges_asset_get_extractable_type
655 (asset), ges_asset_get_id (asset));
659 _send_error_loading_asset (GESProject * project, GESAsset * asset,
662 gchar *internal_id = ges_project_internal_asset_id (asset);
663 const gchar *id = ges_asset_get_id (asset);
665 GST_DEBUG_OBJECT (project, "Sending error loading asset for %s", id);
666 g_hash_table_remove (project->priv->loading_assets, internal_id);
667 g_hash_table_add (project->priv->loaded_with_error, internal_id);
668 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, error,
669 id, ges_asset_get_extractable_type (asset));
673 ges_project_try_updating_id (GESProject * project, GESAsset * asset,
676 gchar *new_id = NULL;
680 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
681 g_return_val_if_fail (GES_IS_ASSET (asset), NULL);
682 g_return_val_if_fail (error, NULL);
684 id = ges_asset_get_id (asset);
685 GST_DEBUG_OBJECT (project, "Try to proxy %s", id);
686 if (ges_asset_request_id_update (asset, &new_id, error) == FALSE) {
687 GST_DEBUG_OBJECT (project, "Type: %s can not be proxied for id: %s "
688 "and error: %s", g_type_name (G_OBJECT_TYPE (asset)), id,
690 _send_error_loading_asset (project, asset, error);
695 /* Always send the MISSING_URI signal if requesting new ID is possible
696 * so that subclasses of GESProject are aware of the missing-uri */
697 g_signal_emit (project, _signals[MISSING_URI_SIGNAL], 0, error, asset,
701 GST_DEBUG_OBJECT (project, "new id found: %s", new_id);
702 if (!ges_asset_try_proxy (asset, new_id)) {
707 GST_DEBUG_OBJECT (project, "No new id found for %s", id);
710 internal_id = ges_project_internal_asset_id (asset);
711 g_hash_table_remove (project->priv->loading_assets, internal_id);
712 g_free (internal_id);
715 _send_error_loading_asset (project, asset, error);
722 new_asset_cb (GESAsset * source, GAsyncResult * res, GESProject * project)
724 GError *error = NULL;
725 gchar *possible_id = NULL;
726 GESAsset *asset = ges_asset_request_finish (res, &error);
729 possible_id = ges_project_try_updating_id (project, source, error);
730 g_clear_error (&error);
732 if (possible_id == NULL)
735 ges_project_create_asset (project, possible_id,
736 ges_asset_get_extractable_type (source));
738 g_free (possible_id);
742 ges_asset_set_proxy (NULL, asset);
743 ges_project_add_asset (project, asset);
745 gst_object_unref (asset);
749 * ges_project_set_loaded:
750 * @project: The #GESProject from which to emit the "project-loaded" signal
752 * Emits the "loaded" signal. This method should be called by sublasses when
753 * the project is fully loaded.
755 * Returns: %TRUE if the signale could be emitted %FALSE otherwize
758 ges_project_set_loaded (GESProject * project, GESFormatter * formatter,
762 GST_ERROR_OBJECT (project, "Emit project error-loading %s", error->message);
763 g_signal_emit (project, _signals[ERROR_LOADING], 0, formatter->timeline,
767 GST_INFO_OBJECT (project, "Emit project loaded");
768 if (GST_STATE (formatter->timeline) < GST_STATE_PAUSED) {
769 timeline_fill_gaps (formatter->timeline);
771 ges_timeline_commit (formatter->timeline);
774 g_signal_emit (project, _signals[LOADED_SIGNAL], 0, formatter->timeline);
776 /* We are now done with that formatter */
777 ges_project_remove_formatter (project, formatter);
782 ges_project_add_loading_asset (GESProject * project, GType extractable_type,
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);
794 /**************************************
796 * API Implementation *
798 **************************************/
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
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
810 * Returns: %TRUE if the asset started to be added %FALSE it was already
814 ges_project_create_asset (GESProject * project, const gchar * id,
815 GType extractable_type)
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),
823 id = g_type_name (extractable_type);
824 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
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)) {
830 g_free (internal_id);
833 g_free (internal_id);
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);
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
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
854 * Returns: (transfer full) (nullable): The newly created #GESAsset or %NULL.
857 ges_project_create_asset_sync (GESProject * project, const gchar * id,
858 GType extractable_type, GError ** error)
861 gchar *possible_id = NULL, *internal_id;
862 gboolean retry = TRUE;
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),
869 id = g_type_name (extractable_type);
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);
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);
883 /* TODO Add a GCancellable somewhere in our API */
886 if (g_type_is_a (extractable_type, GES_TYPE_URI_CLIP)) {
887 asset = GES_ASSET (ges_uri_clip_asset_request_sync (id, error));
889 asset = ges_asset_request (extractable_type, id, error);
895 ges_project_internal_extractable_type_id (extractable_type, id);
896 if ((!g_hash_table_lookup (project->priv->assets, internal_id)))
897 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
898 g_free (internal_id);
901 g_free (possible_id);
902 ges_uri_assets_validate_uri (id);
909 tmpasset = ges_asset_cache_lookup (extractable_type, id);
910 possible_id = ges_project_try_updating_id (project, tmpasset, *error);
912 if (possible_id == NULL) {
913 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, tmpasset);
914 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, *error, id,
920 g_clear_error (error);
926 if (!ges_asset_get_proxy_target (asset))
927 ges_asset_set_proxy (NULL, asset);
929 ges_project_add_asset (project, asset);
935 * ges_project_add_asset:
936 * @project: A #GESProject
937 * @asset: (transfer none): A #GESAsset to add to @project
939 * Adds a #GESAsset to @project, the project will keep a reference on
942 * Returns: %TRUE if the asset could be added %FALSE it was already
946 ges_project_add_asset (GESProject * project, GESAsset * asset)
949 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
951 internal_id = ges_project_internal_asset_id (asset);
952 if (g_hash_table_lookup (project->priv->assets, internal_id)) {
953 g_free (internal_id);
957 g_hash_table_insert (project->priv->assets, internal_id,
958 gst_object_ref (asset));
959 g_hash_table_remove (project->priv->loading_assets, internal_id);
960 GST_DEBUG_OBJECT (project, "Asset added: %s", ges_asset_get_id (asset));
961 g_signal_emit (project, _signals[ASSET_ADDED_SIGNAL], 0, asset);
967 * ges_project_remove_asset:
968 * @project: A #GESProject
969 * @asset: (transfer none): A #GESAsset to remove from @project
971 * remove a @asset to from @project.
973 * Returns: %TRUE if the asset could be removed %FALSE otherwise
976 ges_project_remove_asset (GESProject * project, GESAsset * asset)
981 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
983 internal_id = ges_project_internal_asset_id (asset);
984 ret = g_hash_table_remove (project->priv->assets, internal_id);
985 g_free (internal_id);
986 g_signal_emit (project, _signals[ASSET_REMOVED_SIGNAL], 0, asset);
992 * ges_project_get_asset:
993 * @project: A #GESProject
994 * @id: The id of the asset to retrieve
995 * @extractable_type: The extractable_type of the asset
996 * to retrieve from @object
998 * Returns: (transfer full) (allow-none): The #GESAsset with
999 * @id or %NULL if no asset with @id as an ID
1002 ges_project_get_asset (GESProject * project, const gchar * id,
1003 GType extractable_type)
1008 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1009 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
1012 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
1013 asset = g_hash_table_lookup (project->priv->assets, internal_id);
1014 g_free (internal_id);
1017 return gst_object_ref (asset);
1023 * ges_project_list_assets:
1024 * @project: A #GESProject
1025 * @filter: Type of assets to list, `GES_TYPE_EXTRACTABLE` will list
1028 * List all @asset contained in @project filtering per extractable_type
1029 * as defined by @filter. It copies the asset and thus will not be updated
1032 * Returns: (transfer full) (element-type GESAsset): The list of
1033 * #GESAsset the object contains
1036 ges_project_list_assets (GESProject * project, GType filter)
1039 GHashTableIter iter;
1040 gpointer key, value;
1042 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1044 g_hash_table_iter_init (&iter, project->priv->assets);
1045 while (g_hash_table_iter_next (&iter, &key, &value)) {
1046 if (g_type_is_a (ges_asset_get_extractable_type (GES_ASSET (value)),
1048 ret = g_list_append (ret, gst_object_ref (value));
1056 * @project: A #GESProject to save
1057 * @timeline: The #GESTimeline to save, it must have been extracted from @project
1058 * @uri: The uri where to save @project and @timeline
1059 * @formatter_asset: (allow-none): The formatter asset to use or %NULL. If %NULL,
1060 * will try to save in the same format as the one from which the timeline as been loaded
1061 * or default to the best formatter as defined in #ges_find_formatter_for_uri
1062 * @overwrite: %TRUE to overwrite file if it exists
1063 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1065 * Save the timeline of @project to @uri. You should make sure that @timeline
1066 * is one of the timelines that have been extracted from @project
1067 * (using ges_asset_extract (@project);)
1069 * Returns: %TRUE if the project could be save, %FALSE otherwize
1072 ges_project_save (GESProject * project, GESTimeline * timeline,
1073 const gchar * uri, GESAsset * formatter_asset, gboolean overwrite,
1077 gboolean ret = TRUE;
1078 GESFormatter *formatter = NULL;
1080 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1081 g_return_val_if_fail (formatter_asset == NULL ||
1082 g_type_is_a (ges_asset_get_extractable_type (formatter_asset),
1083 GES_TYPE_FORMATTER), FALSE);
1084 g_return_val_if_fail ((error == NULL || *error == NULL), FALSE);
1086 tl_asset = ges_extractable_get_asset (GES_EXTRACTABLE (timeline));
1087 if (tl_asset == NULL && project->priv->uri == NULL) {
1088 GESAsset *asset = ges_asset_cache_lookup (GES_TYPE_PROJECT, uri);
1091 GST_WARNING_OBJECT (project, "Trying to save project to %s but we already"
1092 "have %" GST_PTR_FORMAT " for that uri, can not save", uri, asset);
1096 GST_DEBUG_OBJECT (project, "Timeline %" GST_PTR_FORMAT " has no asset"
1097 " we have no uri set, so setting ourself as asset", timeline);
1099 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1100 } else if (tl_asset != GES_ASSET (project)) {
1101 GST_WARNING_OBJECT (project, "Timeline %" GST_PTR_FORMAT
1102 " not created by this project can not save", timeline);
1108 if (formatter_asset == NULL) {
1109 formatter_asset = gst_object_ref (ges_find_formatter_for_uri (uri));
1112 formatter = GES_FORMATTER (ges_asset_extract (formatter_asset, error));
1113 if (formatter == NULL) {
1114 GST_WARNING_OBJECT (project, "Could not create the formatter %p %s: %s",
1115 formatter_asset, ges_asset_get_id (formatter_asset),
1116 (error && *error) ? (*error)->message : "Unknown Error");
1122 ges_project_add_formatter (project, formatter);
1123 ret = ges_formatter_save_to_uri (formatter, timeline, uri, overwrite, error);
1124 if (ret && project->priv->uri == NULL)
1125 ges_project_set_uri (project, uri);
1128 if (formatter_asset)
1129 gst_object_unref (formatter_asset);
1130 ges_project_remove_formatter (project, formatter);
1137 * @uri: (allow-none): The uri to be set after creating the project.
1139 * Creates a new #GESProject and sets its uri to @uri if provided. Note that
1140 * if @uri is not valid or %NULL, the uri of the project will then be set
1141 * the first time you save the project. If you then save the project to
1142 * other locations, it will never be updated again and the first valid URI is
1143 * the URI it will keep refering to.
1145 * Returns: A newly created #GESProject
1148 ges_project_new (const gchar * uri)
1150 gchar *id = (gchar *) uri;
1151 GESProject *project;
1154 id = g_strdup_printf ("project-%i", nb_projects++);
1156 project = GES_PROJECT (ges_asset_request (GES_TYPE_TIMELINE, id, NULL));
1159 ges_project_set_uri (project, uri);
1169 * @project: A #GESProject that has an @uri set already
1170 * @timeline: A blank timeline to load @project into
1171 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1173 * Loads @project into @timeline
1175 * Returns: %TRUE if the project could be loaded %FALSE otherwize.
1178 ges_project_load (GESProject * project, GESTimeline * timeline, GError ** error)
1180 g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1181 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1182 g_return_val_if_fail (ges_project_get_uri (project), FALSE);
1183 g_return_val_if_fail (timeline->tracks == NULL, FALSE);
1185 if (!_load_project (project, timeline, error))
1188 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1194 * ges_project_get_uri:
1195 * @project: A #GESProject
1197 * Retrieve the uri that is currently set on @project
1199 * Returns: (transfer full) (nullable): a newly allocated string representing uri.
1202 ges_project_get_uri (GESProject * project)
1204 GESProjectPrivate *priv;
1206 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1208 priv = project->priv;
1210 return g_strdup (priv->uri);
1215 * ges_project_add_encoding_profile:
1216 * @project: A #GESProject
1217 * @profile: A #GstEncodingProfile to add to the project. If a profile with
1218 * the same name already exists, it will be replaced
1220 * Adds @profile to the project. It lets you save in what format
1221 * the project has been renders and keep a reference to those formats.
1222 * Also, those formats will be saves to the project file when possible.
1224 * Returns: %TRUE if @profile could be added, %FALSE otherwize
1227 ges_project_add_encoding_profile (GESProject * project,
1228 GstEncodingProfile * profile)
1231 GESProjectPrivate *priv;
1233 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1234 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1236 priv = project->priv;
1237 for (tmp = priv->encoding_profiles; tmp; tmp = tmp->next) {
1238 GstEncodingProfile *tmpprofile = GST_ENCODING_PROFILE (tmp->data);
1240 if (g_strcmp0 (gst_encoding_profile_get_name (tmpprofile),
1241 gst_encoding_profile_get_name (profile)) == 0) {
1242 GST_INFO_OBJECT (project, "Already have profile: %s, replacing it",
1243 gst_encoding_profile_get_name (profile));
1245 gst_object_unref (tmp->data);
1246 tmp->data = gst_object_ref (profile);
1251 priv->encoding_profiles = g_list_prepend (priv->encoding_profiles,
1252 gst_object_ref (profile));
1258 * ges_project_list_encoding_profiles:
1259 * @project: A #GESProject
1261 * Lists the encoding profile that have been set to @project. The first one
1262 * is the latest added.
1264 * Returns: (transfer none) (element-type GstPbutils.EncodingProfile) (allow-none): The
1265 * list of #GstEncodingProfile used in @project
1268 ges_project_list_encoding_profiles (GESProject * project)
1270 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1272 return project->priv->encoding_profiles;
1276 * ges_project_get_loading_assets:
1277 * @project: A #GESProject
1279 * Get the assets that are being loaded
1281 * Returns: (transfer full) (element-type GES.Asset): A set of loading asset
1282 * that will be added to @project. Note that those Asset are *not* loaded yet,
1283 * and thus can not be used
1286 ges_project_get_loading_assets (GESProject * project)
1288 GHashTableIter iter;
1289 gpointer key, value;
1293 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1295 g_hash_table_iter_init (&iter, project->priv->loading_assets);
1296 while (g_hash_table_iter_next (&iter, &key, &value))
1297 ret = g_list_prepend (ret, gst_object_ref (value));