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 * 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) {
222 const gchar *id = ges_asset_get_id (GES_ASSET (project));
224 if (id && gst_uri_is_valid (id)) {
225 ges_project_set_uri (project, ges_asset_get_id (GES_ASSET (project)));
226 GST_INFO_OBJECT (project, "Using asset ID %s as URI.", priv->uri);
228 EmitLoadedInIdle *data = g_slice_new (EmitLoadedInIdle);
230 GST_INFO_OBJECT (project, "%s, Loading an empty timeline %s"
231 " as no URI set yet", GST_OBJECT_NAME (timeline),
232 ges_asset_get_id (GES_ASSET (project)));
234 data->timeline = gst_object_ref (timeline);
235 data->project = gst_object_ref (project);
237 /* Make sure the signal is emitted after the functions ends */
238 ges_idle_add ((GSourceFunc) _emit_loaded_in_idle, data, NULL);
243 if (priv->formatter_asset == NULL)
244 priv->formatter_asset = _find_formatter_asset_for_id (priv->uri);
246 if (priv->formatter_asset == NULL) {
247 lerr = g_error_new (GES_ERROR, 0, "Could not find a suitable formatter");
251 formatter = GES_FORMATTER (ges_asset_extract (priv->formatter_asset, &lerr));
253 GST_WARNING_OBJECT (project, "Could not create the formatter: %s",
259 ges_project_add_formatter (GES_PROJECT (project), formatter);
260 ges_formatter_load_from_uri (formatter, timeline, priv->uri, &lerr);
262 GST_WARNING_OBJECT (project, "Could not load the timeline,"
263 " returning: %s", lerr->message);
271 g_propagate_error (error, lerr);
276 _uri_missing_accumulator (GSignalInvocationHint * ihint, GValue * return_accu,
277 const GValue * handler_return, gpointer data)
279 const gchar *ret = g_value_get_string (handler_return);
282 if (!gst_uri_is_valid (ret)) {
283 GST_INFO ("The uri %s was not valid, can not work with it!", ret);
287 g_value_set_string (return_accu, ret);
294 /* GESAsset vmethod implementation */
295 static GESExtractable *
296 ges_project_extract (GESAsset * project, GError ** error)
298 GESTimeline *timeline = g_object_new (GES_TYPE_TIMELINE, NULL);
300 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
301 if (_load_project (GES_PROJECT (project), timeline, error))
302 return GES_EXTRACTABLE (timeline);
304 gst_object_unref (timeline);
309 _add_media_new_paths_recursing (const gchar * value)
312 GFileEnumerator *fenum;
313 GFile *file = g_file_new_for_uri (value);
315 if (!(fenum = g_file_enumerate_children (file,
316 "standard::*", G_FILE_QUERY_INFO_NONE, NULL, NULL))) {
317 GST_INFO ("%s is not a folder", value);
322 GST_INFO ("Adding folder: %s", value);
323 g_ptr_array_add (new_paths, g_strdup (value));
324 info = g_file_enumerator_next_file (fenum, NULL, NULL);
325 while (info != NULL) {
326 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
327 GFile *f = g_file_enumerator_get_child (fenum, info);
328 gchar *uri = g_file_get_uri (f);
330 _add_media_new_paths_recursing (uri);
331 gst_object_unref (f);
334 g_object_unref (info);
335 info = g_file_enumerator_next_file (fenum, NULL, NULL);
339 gst_object_unref (file);
341 gst_object_unref (fenum);
345 ges_add_missing_uri_relocation_uri (const gchar * uri, gboolean recurse)
347 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
349 if (new_paths == NULL)
350 new_paths = g_ptr_array_new_with_free_func (g_free);
353 _add_media_new_paths_recursing (uri);
355 g_ptr_array_add (new_paths, g_strdup (uri));
361 ges_missing_uri_default (GESProject * self, GError * error,
362 GESAsset * wrong_asset)
365 const gchar *old_uri = ges_asset_get_id (wrong_asset);
366 gchar *new_id = NULL;
368 if (ges_asset_request_id_update (wrong_asset, &new_id, error) && new_id) {
369 GST_INFO_OBJECT (self, "Returned guessed new ID: %s", new_id);
374 if (new_paths == NULL)
377 if (tried_uris == NULL)
378 tried_uris = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
380 for (i = 0; i < new_paths->len; i++) {
381 gchar *basename, *res;
383 basename = g_path_get_basename (old_uri);
384 res = g_build_filename (new_paths->pdata[i], basename, NULL);
387 if (g_strcmp0 (old_uri, res) == 0) {
388 g_hash_table_add (tried_uris, res);
389 } else if (g_hash_table_lookup (tried_uris, res)) {
390 GST_DEBUG_OBJECT (self, "File already tried: %s", res);
393 g_hash_table_add (tried_uris, g_strdup (res));
394 GST_DEBUG_OBJECT (self, "Trying: %s\n", res);
403 ges_uri_asset_try_update_id (GError * error, GESAsset * wrong_asset)
405 return ges_missing_uri_default (NULL, error, wrong_asset);
409 ges_uri_assets_validate_uri (const gchar * nid)
412 g_hash_table_remove (tried_uris, nid);
415 /* GObject vmethod implementation */
417 _dispose (GObject * object)
419 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
422 g_hash_table_unref (priv->assets);
423 if (priv->loading_assets)
424 g_hash_table_unref (priv->loading_assets);
425 if (priv->loaded_with_error)
426 g_hash_table_unref (priv->loaded_with_error);
427 if (priv->formatter_asset)
428 gst_object_unref (priv->formatter_asset);
430 while (priv->formatters)
431 ges_project_remove_formatter (GES_PROJECT (object), priv->formatters->data);
433 G_OBJECT_CLASS (ges_project_parent_class)->dispose (object);
437 _finalize (GObject * object)
439 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
443 g_list_free_full (priv->encoding_profiles, g_object_unref);
445 G_OBJECT_CLASS (ges_project_parent_class)->finalize (object);
449 _get_property (GESProject * project, guint property_id,
450 GValue * value, GParamSpec * pspec)
452 GESProjectPrivate *priv = project->priv;
454 switch (property_id) {
456 g_value_set_string (value, priv->uri);
459 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
464 _set_property (GESProject * project, guint property_id,
465 const GValue * value, GParamSpec * pspec)
467 switch (property_id) {
469 project->priv->uri = g_value_dup_string (value);
472 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
477 ges_project_class_init (GESProjectClass * klass)
479 GObjectClass *object_class = G_OBJECT_CLASS (klass);
481 klass->asset_added = NULL;
482 klass->missing_uri = ges_missing_uri_default;
483 klass->loading_error = NULL;
484 klass->asset_removed = NULL;
485 object_class->get_property = (GObjectGetPropertyFunc) _get_property;
486 object_class->set_property = (GObjectSetPropertyFunc) _set_property;
491 * The location of the project to use.
493 _properties[PROP_URI] = g_param_spec_string ("uri", "URI",
494 "uri of the project", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
496 g_object_class_install_properties (object_class, PROP_LAST, _properties);
499 * GESProject::asset-added:
500 * @project: the #GESProject
501 * @asset: The #GESAsset that has been added to @project
503 _signals[ASSET_ADDED_SIGNAL] =
504 g_signal_new ("asset-added", G_TYPE_FROM_CLASS (klass),
505 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_added),
506 NULL, NULL, NULL, G_TYPE_NONE, 1, GES_TYPE_ASSET);
509 * GESProject::asset-loading:
510 * @project: the #GESProject
511 * @asset: The #GESAsset that started loading
515 _signals[ASSET_LOADING_SIGNAL] =
516 g_signal_new ("asset-loading", G_TYPE_FROM_CLASS (klass),
517 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_loading),
518 NULL, NULL, NULL, G_TYPE_NONE, 1, GES_TYPE_ASSET);
521 * GESProject::asset-removed:
522 * @project: the #GESProject
523 * @asset: The #GESAsset that has been removed from @project
525 _signals[ASSET_REMOVED_SIGNAL] =
526 g_signal_new ("asset-removed", G_TYPE_FROM_CLASS (klass),
527 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_removed),
528 NULL, NULL, NULL, G_TYPE_NONE, 1, GES_TYPE_ASSET);
531 * GESProject::loading:
532 * @project: the #GESProject that is starting to load a timeline
533 * @timeline: The #GESTimeline that started loading
537 _signals[LOADING_SIGNAL] =
538 g_signal_new ("loading", G_TYPE_FROM_CLASS (klass),
539 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loading),
540 NULL, NULL, NULL, G_TYPE_NONE, 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, NULL, G_TYPE_NONE, 1, GES_TYPE_TIMELINE);
553 * GESProject::missing-uri:
554 * @project: the #GESProject reporting that a file has moved
555 * @error: The error that happened
556 * @wrong_asset: The asset with the wrong ID, you should us it and its content
557 * only to find out what the new location is.
561 * source_moved_cb (GESProject *project, GError *error, GESAsset *asset_with_error)
563 * return g_strdup ("file:///the/new/uri.ogg");
567 * main (int argc, gchar ** argv)
569 * GESTimeline *timeline;
570 * GESProject *project = ges_project_new ("file:///some/uri.xges");
572 * g_signal_connect (project, "missing-uri", source_moved_cb, NULL);
573 * timeline = ges_asset_extract (GES_ASSET (project));
577 * Returns: (transfer full) (allow-none): The new URI of @wrong_asset
579 _signals[MISSING_URI_SIGNAL] =
580 g_signal_new ("missing-uri", G_TYPE_FROM_CLASS (klass),
581 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, missing_uri),
582 _uri_missing_accumulator, NULL, NULL,
583 G_TYPE_STRING, 2, G_TYPE_ERROR, GES_TYPE_ASSET);
586 * GESProject::error-loading-asset:
587 * @project: the #GESProject on which a problem happend when creted a #GESAsset
588 * @error: The #GError defining the error that occured, might be %NULL
589 * @id: The @id of the asset that failed loading
590 * @extractable_type: The @extractable_type of the asset that
593 * Informs you that a #GESAsset could not be created. In case of
594 * missing GStreamer plugins, the error will be set to #GST_CORE_ERROR
595 * #GST_CORE_ERROR_MISSING_PLUGIN
597 _signals[ERROR_LOADING_ASSET] =
598 g_signal_new ("error-loading-asset", G_TYPE_FROM_CLASS (klass),
599 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, loading_error),
601 G_TYPE_NONE, 3, G_TYPE_ERROR, G_TYPE_STRING, G_TYPE_GTYPE);
604 * GESProject::error-loading:
605 * @project: the #GESProject on which a problem happend when creted a #GESAsset
606 * @timeline: The timeline that failed loading
607 * @error: The #GError defining the error that occured
611 _signals[ERROR_LOADING] =
612 g_signal_new ("error-loading", G_TYPE_FROM_CLASS (klass),
613 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
614 G_TYPE_NONE, 2, GES_TYPE_TIMELINE, G_TYPE_ERROR);
616 object_class->dispose = _dispose;
617 object_class->finalize = _finalize;
619 GES_ASSET_CLASS (klass)->extract = ges_project_extract;
623 ges_project_init (GESProject * project)
625 GESProjectPrivate *priv = project->priv =
626 ges_project_get_instance_private (project);
629 priv->formatters = NULL;
630 priv->formatter_asset = NULL;
631 priv->encoding_profiles = NULL;
632 priv->assets = g_hash_table_new_full (g_str_hash, g_str_equal,
633 g_free, gst_object_unref);
634 priv->loading_assets = g_hash_table_new_full (g_str_hash, g_str_equal,
635 g_free, gst_object_unref);
636 priv->loaded_with_error = g_hash_table_new_full (g_str_hash, g_str_equal,
641 ges_project_internal_extractable_type_id (GType extractable_type,
644 return g_strdup_printf ("%s:%s", _extractable_type_name (extractable_type),
649 ges_project_internal_asset_id (GESAsset * asset)
652 ges_project_internal_extractable_type_id (ges_asset_get_extractable_type
653 (asset), ges_asset_get_id (asset));
657 _send_error_loading_asset (GESProject * project, GESAsset * asset,
660 gchar *internal_id = ges_project_internal_asset_id (asset);
661 const gchar *id = ges_asset_get_id (asset);
663 GST_DEBUG_OBJECT (project, "Sending error loading asset for %s", id);
664 g_hash_table_remove (project->priv->loading_assets, internal_id);
665 g_hash_table_add (project->priv->loaded_with_error, internal_id);
666 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, error,
667 id, ges_asset_get_extractable_type (asset));
671 ges_project_try_updating_id (GESProject * project, GESAsset * asset,
674 gchar *new_id = NULL;
678 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
679 g_return_val_if_fail (GES_IS_ASSET (asset), NULL);
680 g_return_val_if_fail (error, NULL);
682 id = ges_asset_get_id (asset);
683 GST_DEBUG_OBJECT (project, "Try to proxy %s", id);
684 if (ges_asset_request_id_update (asset, &new_id, error) == FALSE) {
685 GST_DEBUG_OBJECT (project, "Type: %s can not be proxied for id: %s "
686 "and error: %s", g_type_name (G_OBJECT_TYPE (asset)), id,
688 _send_error_loading_asset (project, asset, error);
693 /* Always send the MISSING_URI signal if requesting new ID is possible
694 * so that subclasses of GESProject are aware of the missing-uri */
695 g_signal_emit (project, _signals[MISSING_URI_SIGNAL], 0, error, asset,
699 GST_DEBUG_OBJECT (project, "new id found: %s", new_id);
700 if (!ges_asset_try_proxy (asset, new_id)) {
705 GST_DEBUG_OBJECT (project, "No new id found for %s", id);
708 internal_id = ges_project_internal_asset_id (asset);
709 g_hash_table_remove (project->priv->loading_assets, internal_id);
710 g_free (internal_id);
713 _send_error_loading_asset (project, asset, error);
720 new_asset_cb (GESAsset * source, GAsyncResult * res, GESProject * project)
722 GError *error = NULL;
723 gchar *possible_id = NULL;
724 GESAsset *asset = ges_asset_request_finish (res, &error);
727 possible_id = ges_project_try_updating_id (project, source, error);
728 g_clear_error (&error);
730 if (possible_id == NULL)
733 ges_project_create_asset (project, possible_id,
734 ges_asset_get_extractable_type (source));
736 g_free (possible_id);
740 ges_asset_set_proxy (NULL, asset);
741 ges_project_add_asset (project, asset);
743 gst_object_unref (asset);
747 * ges_project_set_loaded:
748 * @project: The #GESProject from which to emit the "project-loaded" signal
750 * Emits the "loaded" signal. This method should be called by sublasses when
751 * the project is fully loaded.
753 * Returns: %TRUE if the signale could be emitted %FALSE otherwize
756 ges_project_set_loaded (GESProject * project, GESFormatter * formatter,
760 GST_ERROR_OBJECT (project, "Emit project error-loading %s", error->message);
761 g_signal_emit (project, _signals[ERROR_LOADING], 0, formatter->timeline,
765 GST_INFO_OBJECT (project, "Emit project loaded");
766 if (GST_STATE (formatter->timeline) < GST_STATE_PAUSED) {
767 timeline_fill_gaps (formatter->timeline);
769 ges_timeline_commit (formatter->timeline);
772 g_signal_emit (project, _signals[LOADED_SIGNAL], 0, formatter->timeline);
774 /* We are now done with that formatter */
775 ges_project_remove_formatter (project, formatter);
780 ges_project_add_loading_asset (GESProject * project, GType extractable_type,
785 if ((asset = ges_asset_cache_lookup (extractable_type, id))) {
786 if (g_hash_table_insert (project->priv->loading_assets,
787 ges_project_internal_asset_id (asset), gst_object_ref (asset)))
788 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
792 /**************************************
794 * API Implementation *
796 **************************************/
799 * ges_project_create_asset:
800 * @project: A #GESProject
801 * @id: (allow-none): The id of the asset to create and add to @project
802 * @extractable_type: The #GType of the asset to create
804 * Create and add a #GESAsset to @project. You should connect to the
805 * "asset-added" signal to get the asset when it finally gets added to
808 * Returns: %TRUE if the asset started to be added %FALSE it was already
812 ges_project_create_asset (GESProject * project, const gchar * id,
813 GType extractable_type)
816 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
817 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
821 id = g_type_name (extractable_type);
822 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
824 if (g_hash_table_lookup (project->priv->assets, internal_id) ||
825 g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
826 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
828 g_free (internal_id);
831 g_free (internal_id);
833 /* TODO Add a GCancellable somewhere in our API */
834 ges_asset_request_async (extractable_type, id, NULL,
835 (GAsyncReadyCallback) new_asset_cb, project);
836 ges_project_add_loading_asset (project, extractable_type, id);
842 * ges_project_create_asset_sync:
843 * @project: A #GESProject
844 * @id: (allow-none): The id of the asset to create and add to @project
845 * @extractable_type: The #GType of the asset to create
846 * @error: A #GError to be set in case of error
848 * Create and add a #GESAsset to @project. You should connect to the
849 * "asset-added" signal to get the asset when it finally gets added to
852 * Returns: (transfer full) (nullable): The newly created #GESAsset or %NULL.
855 ges_project_create_asset_sync (GESProject * project, const gchar * id,
856 GType extractable_type, GError ** error)
859 gchar *possible_id = NULL, *internal_id;
860 gboolean retry = TRUE;
862 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
863 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
867 id = g_type_name (extractable_type);
869 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
870 if ((asset = g_hash_table_lookup (project->priv->assets, internal_id))) {
871 g_free (internal_id);
874 } else if (g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
875 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
876 g_free (internal_id);
880 g_free (internal_id);
882 /* TODO Add a GCancellable somewhere in our API */
885 if (g_type_is_a (extractable_type, GES_TYPE_URI_CLIP)) {
886 asset = GES_ASSET (ges_uri_clip_asset_request_sync (id, error));
888 asset = ges_asset_request (extractable_type, id, error);
894 ges_project_internal_extractable_type_id (extractable_type, id);
895 if ((!g_hash_table_lookup (project->priv->assets, internal_id)))
896 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
897 g_free (internal_id);
900 g_free (possible_id);
901 ges_uri_assets_validate_uri (id);
908 tmpasset = ges_asset_cache_lookup (extractable_type, id);
909 possible_id = ges_project_try_updating_id (project, tmpasset, *error);
911 if (possible_id == NULL) {
912 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, tmpasset);
913 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, *error, id,
919 g_clear_error (error);
925 if (!ges_asset_get_proxy_target (asset))
926 ges_asset_set_proxy (NULL, asset);
928 ges_project_add_asset (project, asset);
934 * ges_project_add_asset:
935 * @project: A #GESProject
936 * @asset: (transfer none): A #GESAsset to add to @project
938 * Adds a #GESAsset to @project, the project will keep a reference on
941 * Returns: %TRUE if the asset could be added %FALSE it was already
945 ges_project_add_asset (GESProject * project, GESAsset * asset)
948 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
950 internal_id = ges_project_internal_asset_id (asset);
951 if (g_hash_table_lookup (project->priv->assets, internal_id)) {
952 g_free (internal_id);
956 g_hash_table_insert (project->priv->assets, internal_id,
957 gst_object_ref (asset));
958 g_hash_table_remove (project->priv->loading_assets, internal_id);
959 GST_DEBUG_OBJECT (project, "Asset added: %s", ges_asset_get_id (asset));
960 g_signal_emit (project, _signals[ASSET_ADDED_SIGNAL], 0, asset);
966 * ges_project_remove_asset:
967 * @project: A #GESProject
968 * @asset: (transfer none): A #GESAsset to remove from @project
970 * remove a @asset to from @project.
972 * Returns: %TRUE if the asset could be removed %FALSE otherwise
975 ges_project_remove_asset (GESProject * project, GESAsset * asset)
980 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
982 internal_id = ges_project_internal_asset_id (asset);
983 ret = g_hash_table_remove (project->priv->assets, internal_id);
984 g_free (internal_id);
985 g_signal_emit (project, _signals[ASSET_REMOVED_SIGNAL], 0, asset);
991 * ges_project_get_asset:
992 * @project: A #GESProject
993 * @id: The id of the asset to retrieve
994 * @extractable_type: The extractable_type of the asset
995 * to retrieve from @object
997 * Returns: (transfer full) (allow-none): The #GESAsset with
998 * @id or %NULL if no asset with @id as an ID
1001 ges_project_get_asset (GESProject * project, const gchar * id,
1002 GType extractable_type)
1007 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1008 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
1011 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
1012 asset = g_hash_table_lookup (project->priv->assets, internal_id);
1013 g_free (internal_id);
1016 return gst_object_ref (asset);
1022 * ges_project_list_assets:
1023 * @project: A #GESProject
1024 * @filter: Type of assets to list, `GES_TYPE_EXTRACTABLE` will list
1027 * List all @asset contained in @project filtering per extractable_type
1028 * as defined by @filter. It copies the asset and thus will not be updated
1031 * Returns: (transfer full) (element-type GESAsset): The list of
1032 * #GESAsset the object contains
1035 ges_project_list_assets (GESProject * project, GType filter)
1038 GHashTableIter iter;
1039 gpointer key, value;
1041 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1042 g_return_val_if_fail (filter == G_TYPE_NONE
1043 || g_type_is_a (filter, GES_TYPE_EXTRACTABLE), NULL);
1045 g_hash_table_iter_init (&iter, project->priv->assets);
1046 while (g_hash_table_iter_next (&iter, &key, &value)) {
1047 if (g_type_is_a (ges_asset_get_extractable_type (GES_ASSET (value)),
1049 ret = g_list_append (ret, gst_object_ref (value));
1057 * @project: A #GESProject to save
1058 * @timeline: The #GESTimeline to save, it must have been extracted from @project
1059 * @uri: The uri where to save @project and @timeline
1060 * @formatter_asset: (allow-none): The formatter asset to use or %NULL. If %NULL,
1061 * will try to save in the same format as the one from which the timeline as been loaded
1062 * or default to the best formatter as defined in #ges_find_formatter_for_uri
1063 * @overwrite: %TRUE to overwrite file if it exists
1064 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1066 * Save the timeline of @project to @uri. You should make sure that @timeline
1067 * is one of the timelines that have been extracted from @project
1068 * (using ges_asset_extract (@project);)
1070 * Returns: %TRUE if the project could be save, %FALSE otherwize
1073 ges_project_save (GESProject * project, GESTimeline * timeline,
1074 const gchar * uri, GESAsset * formatter_asset, gboolean overwrite,
1078 gboolean ret = TRUE;
1079 GESFormatter *formatter = NULL;
1081 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1082 g_return_val_if_fail (formatter_asset == NULL ||
1083 g_type_is_a (ges_asset_get_extractable_type (formatter_asset),
1084 GES_TYPE_FORMATTER), FALSE);
1085 g_return_val_if_fail ((error == NULL || *error == NULL), FALSE);
1087 tl_asset = ges_extractable_get_asset (GES_EXTRACTABLE (timeline));
1088 if (tl_asset == NULL && project->priv->uri == NULL) {
1089 GESAsset *asset = ges_asset_cache_lookup (GES_TYPE_PROJECT, uri);
1092 GST_WARNING_OBJECT (project, "Trying to save project to %s but we already"
1093 "have %" GST_PTR_FORMAT " for that uri, can not save", uri, asset);
1097 GST_DEBUG_OBJECT (project, "Timeline %" GST_PTR_FORMAT " has no asset"
1098 " we have no uri set, so setting ourself as asset", timeline);
1100 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1101 } else if (tl_asset != GES_ASSET (project)) {
1102 GST_WARNING_OBJECT (project, "Timeline %" GST_PTR_FORMAT
1103 " not created by this project can not save", timeline);
1109 if (formatter_asset == NULL) {
1110 formatter_asset = gst_object_ref (ges_find_formatter_for_uri (uri));
1113 formatter = GES_FORMATTER (ges_asset_extract (formatter_asset, error));
1114 if (formatter == NULL) {
1115 GST_WARNING_OBJECT (project, "Could not create the formatter %p %s: %s",
1116 formatter_asset, ges_asset_get_id (formatter_asset),
1117 (error && *error) ? (*error)->message : "Unknown Error");
1123 ges_project_add_formatter (project, formatter);
1124 ret = ges_formatter_save_to_uri (formatter, timeline, uri, overwrite, error);
1125 if (ret && project->priv->uri == NULL)
1126 ges_project_set_uri (project, uri);
1129 if (formatter_asset)
1130 gst_object_unref (formatter_asset);
1131 ges_project_remove_formatter (project, formatter);
1138 * @uri: (allow-none): The uri to be set after creating the project.
1140 * Creates a new #GESProject and sets its uri to @uri if provided. Note that
1141 * if @uri is not valid or %NULL, the uri of the project will then be set
1142 * the first time you save the project. If you then save the project to
1143 * other locations, it will never be updated again and the first valid URI is
1144 * the URI it will keep refering to.
1146 * Returns: A newly created #GESProject
1149 ges_project_new (const gchar * uri)
1151 gchar *id = (gchar *) uri;
1152 GESProject *project;
1155 id = g_strdup_printf ("project-%i", nb_projects++);
1157 project = GES_PROJECT (ges_asset_request (GES_TYPE_TIMELINE, id, NULL));
1160 ges_project_set_uri (project, uri);
1170 * @project: A #GESProject that has an @uri set already
1171 * @timeline: A blank timeline to load @project into
1172 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1174 * Loads @project into @timeline
1176 * Returns: %TRUE if the project could be loaded %FALSE otherwize.
1179 ges_project_load (GESProject * project, GESTimeline * timeline, GError ** error)
1181 g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1182 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1183 g_return_val_if_fail (ges_project_get_uri (project), FALSE);
1184 g_return_val_if_fail (timeline->tracks == NULL, FALSE);
1186 if (!_load_project (project, timeline, error))
1189 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1195 * ges_project_get_uri:
1196 * @project: A #GESProject
1198 * Retrieve the uri that is currently set on @project
1200 * Returns: (transfer full) (nullable): a newly allocated string representing uri.
1203 ges_project_get_uri (GESProject * project)
1205 GESProjectPrivate *priv;
1207 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1209 priv = project->priv;
1211 return g_strdup (priv->uri);
1216 * ges_project_add_encoding_profile:
1217 * @project: A #GESProject
1218 * @profile: A #GstEncodingProfile to add to the project. If a profile with
1219 * the same name already exists, it will be replaced
1221 * Adds @profile to the project. It lets you save in what format
1222 * the project has been renders and keep a reference to those formats.
1223 * Also, those formats will be saves to the project file when possible.
1225 * Returns: %TRUE if @profile could be added, %FALSE otherwize
1228 ges_project_add_encoding_profile (GESProject * project,
1229 GstEncodingProfile * profile)
1232 GESProjectPrivate *priv;
1234 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1235 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1237 priv = project->priv;
1238 for (tmp = priv->encoding_profiles; tmp; tmp = tmp->next) {
1239 GstEncodingProfile *tmpprofile = GST_ENCODING_PROFILE (tmp->data);
1241 if (g_strcmp0 (gst_encoding_profile_get_name (tmpprofile),
1242 gst_encoding_profile_get_name (profile)) == 0) {
1243 GST_INFO_OBJECT (project, "Already have profile: %s, replacing it",
1244 gst_encoding_profile_get_name (profile));
1246 gst_object_unref (tmp->data);
1247 tmp->data = gst_object_ref (profile);
1252 priv->encoding_profiles = g_list_prepend (priv->encoding_profiles,
1253 gst_object_ref (profile));
1259 * ges_project_list_encoding_profiles:
1260 * @project: A #GESProject
1262 * Lists the encoding profile that have been set to @project. The first one
1263 * is the latest added.
1265 * Returns: (transfer none) (element-type GstPbutils.EncodingProfile) (allow-none): The
1266 * list of #GstEncodingProfile used in @project
1269 ges_project_list_encoding_profiles (GESProject * project)
1271 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1273 return project->priv->encoding_profiles;
1277 * ges_project_get_loading_assets:
1278 * @project: A #GESProject
1280 * Get the assets that are being loaded
1282 * Returns: (transfer full) (element-type GES.Asset): A set of loading asset
1283 * that will be added to @project. Note that those Asset are *not* loaded yet,
1284 * and thus can not be used
1287 ges_project_get_loading_assets (GESProject * project)
1289 GHashTableIter iter;
1290 gpointer key, value;
1294 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1296 g_hash_table_iter_init (&iter, project->priv->loading_assets);
1297 while (g_hash_table_iter_next (&iter, &key, &value))
1298 ret = g_list_prepend (ret, gst_object_ref (value));