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.
52 #include "ges-internal.h"
54 static GPtrArray *new_paths = NULL;
55 static GHashTable *tried_uris = NULL;
57 struct _GESProjectPrivate
60 /* Set of asset ID being loaded */
61 GHashTable *loading_assets;
62 GHashTable *loaded_with_error;
63 GESAsset *formatter_asset;
69 GList *encoding_profiles;
72 typedef struct EmitLoadedInIdle
75 GESTimeline *timeline;
90 G_DEFINE_TYPE_WITH_PRIVATE (GESProject, ges_project, GES_TYPE_ASSET);
92 static guint _signals[LAST_SIGNAL] = { 0 };
94 static guint nb_projects = 0;
96 /* Find the type that implemented the GESExtractable interface */
97 static inline const gchar *
98 _extractable_type_name (GType type)
101 if (g_type_is_a (g_type_parent (type), GES_TYPE_EXTRACTABLE))
102 type = g_type_parent (type);
104 return g_type_name (type);
115 static GParamSpec *_properties[LAST_SIGNAL] = { 0 };
118 _emit_loaded_in_idle (EmitLoadedInIdle * data)
120 g_signal_emit (data->project, _signals[LOADED_SIGNAL], 0, data->timeline);
122 gst_object_unref (data->project);
123 gst_object_unref (data->timeline);
124 g_slice_free (EmitLoadedInIdle, data);
130 ges_project_add_formatter (GESProject * project, GESFormatter * formatter)
132 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
134 ges_formatter_set_project (formatter, project);
135 priv->formatters = g_list_append (priv->formatters, formatter);
137 gst_object_ref_sink (formatter);
141 ges_project_remove_formatter (GESProject * project, GESFormatter * formatter)
144 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
146 for (tmp = priv->formatters; tmp; tmp = tmp->next) {
147 if (tmp->data == formatter) {
148 gst_object_unref (formatter);
149 priv->formatters = g_list_delete_link (priv->formatters, tmp);
157 ges_project_set_uri (GESProject * project, const gchar * uri)
159 GESProjectPrivate *priv;
161 g_return_if_fail (GES_IS_PROJECT (project));
163 priv = project->priv;
165 GST_WARNING_OBJECT (project, "Trying to rest URI, this is prohibited");
171 GST_LOG_OBJECT (project, "Uri should not be NULL");
175 priv->uri = g_strdup (uri);
177 /* We use that URI as ID */
178 ges_asset_set_id (GES_ASSET (project), uri);
184 _load_project (GESProject * project, GESTimeline * timeline, GError ** error)
187 GESProjectPrivate *priv;
188 GESFormatter *formatter;
190 priv = GES_PROJECT (project)->priv;
192 g_signal_emit (project, _signals[LOADING_SIGNAL], 0, timeline);
193 if (priv->uri == NULL) {
194 EmitLoadedInIdle *data = g_slice_new (EmitLoadedInIdle);
196 GST_LOG_OBJECT (project, "%s, Loading an empty timeline %s"
197 " as no URI set yet", GST_OBJECT_NAME (timeline),
198 ges_asset_get_id (GES_ASSET (project)));
200 data->timeline = gst_object_ref (timeline);
201 data->project = gst_object_ref (project);
203 /* Make sure the signal is emitted after the functions ends */
204 g_idle_add ((GSourceFunc) _emit_loaded_in_idle, data);
208 if (priv->formatter_asset == NULL)
209 priv->formatter_asset = _find_formatter_asset_for_id (priv->uri);
211 if (priv->formatter_asset == NULL) {
212 lerr = g_error_new (GES_ERROR, 0, "Could not find a suitable formatter");
216 formatter = GES_FORMATTER (ges_asset_extract (priv->formatter_asset, &lerr));
218 GST_WARNING_OBJECT (project, "Could not create the formatter: %s",
224 ges_project_add_formatter (GES_PROJECT (project), formatter);
225 ges_formatter_load_from_uri (formatter, timeline, priv->uri, &lerr);
227 GST_WARNING_OBJECT (project, "Could not load the timeline,"
228 " returning: %s", lerr->message);
236 g_propagate_error (error, lerr);
241 _uri_missing_accumulator (GSignalInvocationHint * ihint, GValue * return_accu,
242 const GValue * handler_return, gpointer data)
244 const gchar *ret = g_value_get_string (handler_return);
247 if (!gst_uri_is_valid (ret)) {
248 GST_INFO ("The uri %s was not valid, can not work with it!", ret);
252 g_value_set_string (return_accu, ret);
259 /* GESAsset vmethod implementation */
260 static GESExtractable *
261 ges_project_extract (GESAsset * project, GError ** error)
263 GESTimeline *timeline = g_object_new (GES_TYPE_TIMELINE, NULL);
265 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
266 if (_load_project (GES_PROJECT (project), timeline, error))
267 return GES_EXTRACTABLE (timeline);
269 gst_object_unref (timeline);
274 _add_media_new_paths_recursing (const gchar * value)
277 GFileEnumerator *fenum;
278 GFile *file = g_file_new_for_uri (value);
280 if (!(fenum = g_file_enumerate_children (file,
281 "standard::*", G_FILE_QUERY_INFO_NONE, NULL, NULL))) {
282 GST_INFO ("%s is not a folder", value);
287 GST_INFO ("Adding folder: %s", value);
288 g_ptr_array_add (new_paths, g_strdup (value));
289 info = g_file_enumerator_next_file (fenum, NULL, NULL);
290 while (info != NULL) {
291 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
292 GFile *f = g_file_enumerator_get_child (fenum, info);
293 gchar *uri = g_file_get_uri (f);
295 _add_media_new_paths_recursing (uri);
296 gst_object_unref (f);
299 g_object_unref (info);
300 info = g_file_enumerator_next_file (fenum, NULL, NULL);
304 gst_object_unref (file);
306 gst_object_unref (fenum);
310 ges_add_missing_uri_relocation_uri (const gchar * uri, gboolean recurse)
312 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
314 if (new_paths == NULL)
315 new_paths = g_ptr_array_new_with_free_func (g_free);
318 _add_media_new_paths_recursing (uri);
320 g_ptr_array_add (new_paths, g_strdup (uri));
326 ges_missing_uri_default (GESProject * self, GError * error,
327 GESAsset * wrong_asset)
330 const gchar *old_uri = ges_asset_get_id (wrong_asset);
331 gchar *new_id = NULL;
333 if (ges_asset_request_id_update (wrong_asset, &new_id, error) && new_id) {
334 GST_INFO_OBJECT (self, "Returned guessed new ID: %s", new_id);
339 if (new_paths == NULL)
342 if (tried_uris == NULL)
343 tried_uris = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
345 for (i = 0; i < new_paths->len; i++) {
346 gchar *basename, *res;
348 basename = g_path_get_basename (old_uri);
349 res = g_build_filename (new_paths->pdata[i], basename, NULL);
352 if (g_strcmp0 (old_uri, res) == 0) {
353 g_hash_table_add (tried_uris, res);
354 } else if (g_hash_table_lookup (tried_uris, res)) {
355 GST_DEBUG_OBJECT (self, "File already tried: %s", res);
358 g_hash_table_add (tried_uris, g_strdup (res));
359 GST_DEBUG_OBJECT (self, "Trying: %s\n", res);
368 ges_uri_asset_try_update_id (GError * error, GESAsset * wrong_asset)
370 return ges_missing_uri_default (NULL, error, wrong_asset);
374 ges_uri_assets_validate_uri (const gchar * nid)
377 g_hash_table_remove (tried_uris, nid);
380 /* GObject vmethod implementation */
382 _dispose (GObject * object)
384 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
387 g_hash_table_unref (priv->assets);
388 if (priv->loading_assets)
389 g_hash_table_unref (priv->loading_assets);
390 if (priv->loaded_with_error)
391 g_hash_table_unref (priv->loaded_with_error);
392 if (priv->formatter_asset)
393 gst_object_unref (priv->formatter_asset);
395 while (priv->formatters)
396 ges_project_remove_formatter (GES_PROJECT (object), priv->formatters->data);
398 G_OBJECT_CLASS (ges_project_parent_class)->dispose (object);
402 _finalize (GObject * object)
404 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
408 g_list_free_full (priv->encoding_profiles, g_object_unref);
410 G_OBJECT_CLASS (ges_project_parent_class)->finalize (object);
414 _get_property (GESProject * project, guint property_id,
415 GValue * value, GParamSpec * pspec)
417 GESProjectPrivate *priv = project->priv;
419 switch (property_id) {
421 g_value_set_string (value, priv->uri);
424 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
429 _set_property (GESProject * project, guint property_id,
430 const GValue * value, GParamSpec * pspec)
432 switch (property_id) {
434 project->priv->uri = g_value_dup_string (value);
437 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
442 ges_project_class_init (GESProjectClass * klass)
444 GObjectClass *object_class = G_OBJECT_CLASS (klass);
446 klass->asset_added = NULL;
447 klass->missing_uri = ges_missing_uri_default;
448 klass->loading_error = NULL;
449 klass->asset_removed = NULL;
450 object_class->get_property = (GObjectGetPropertyFunc) _get_property;
451 object_class->set_property = (GObjectSetPropertyFunc) _set_property;
456 * The location of the project to use.
458 _properties[PROP_URI] = g_param_spec_string ("uri", "URI",
459 "uri of the project", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
461 g_object_class_install_properties (object_class, PROP_LAST, _properties);
464 * GESProject::asset-added:
465 * @project: the #GESProject
466 * @asset: The #GESAsset that has been added to @project
468 _signals[ASSET_ADDED_SIGNAL] =
469 g_signal_new ("asset-added", G_TYPE_FROM_CLASS (klass),
470 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_added),
471 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
474 * GESProject::asset-loading:
475 * @project: the #GESProject
476 * @asset: The #GESAsset that started loading
480 _signals[ASSET_LOADING_SIGNAL] =
481 g_signal_new ("asset-loading", G_TYPE_FROM_CLASS (klass),
482 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_loading),
483 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
486 * GESProject::asset-removed:
487 * @project: the #GESProject
488 * @asset: The #GESAsset that has been removed from @project
490 _signals[ASSET_REMOVED_SIGNAL] =
491 g_signal_new ("asset-removed", G_TYPE_FROM_CLASS (klass),
492 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_removed),
493 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
496 * GESProject::loading:
497 * @project: the #GESProject that is starting to load a timeline
498 * @timeline: The #GESTimeline that started loading
502 _signals[LOADING_SIGNAL] =
503 g_signal_new ("loading", G_TYPE_FROM_CLASS (klass),
504 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loading),
505 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
506 1, GES_TYPE_TIMELINE);
509 * GESProject::loaded:
510 * @project: the #GESProject that is done loading a timeline.
511 * @timeline: The #GESTimeline that completed loading
513 _signals[LOADED_SIGNAL] =
514 g_signal_new ("loaded", G_TYPE_FROM_CLASS (klass),
515 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loaded),
516 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
517 1, GES_TYPE_TIMELINE);
520 * GESProject::missing-uri:
521 * @project: the #GESProject reporting that a file has moved
522 * @error: The error that happened
523 * @wrong_asset: The asset with the wrong ID, you should us it and its content
524 * only to find out what the new location is.
528 * source_moved_cb (GESProject *project, GError *error, GESAsset *asset_with_error)
530 * return g_strdup ("file:///the/new/uri.ogg");
534 * main (int argc, gchar ** argv)
536 * GESTimeline *timeline;
537 * GESProject *project = ges_project_new ("file:///some/uri.xges");
539 * g_signal_connect (project, "missing-uri", source_moved_cb, NULL);
540 * timeline = ges_asset_extract (GES_ASSET (project));
544 * Returns: (transfer full) (allow-none): The new URI of @wrong_asset
546 _signals[MISSING_URI_SIGNAL] =
547 g_signal_new ("missing-uri", G_TYPE_FROM_CLASS (klass),
548 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, missing_uri),
549 _uri_missing_accumulator, NULL, g_cclosure_marshal_generic,
550 G_TYPE_STRING, 2, G_TYPE_ERROR, GES_TYPE_ASSET);
553 * GESProject::error-loading-asset:
554 * @project: the #GESProject on which a problem happend when creted a #GESAsset
555 * @error: The #GError defining the error that occured, might be %NULL
556 * @id: The @id of the asset that failed loading
557 * @extractable_type: The @extractable_type of the asset that
560 * Informs you that a #GESAsset could not be created. In case of
561 * missing GStreamer plugins, the error will be set to #GST_CORE_ERROR
562 * #GST_CORE_ERROR_MISSING_PLUGIN
564 _signals[ERROR_LOADING_ASSET] =
565 g_signal_new ("error-loading-asset", G_TYPE_FROM_CLASS (klass),
566 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, loading_error),
567 NULL, NULL, g_cclosure_marshal_generic,
568 G_TYPE_NONE, 3, G_TYPE_ERROR, G_TYPE_STRING, G_TYPE_GTYPE);
570 object_class->dispose = _dispose;
571 object_class->finalize = _finalize;
573 GES_ASSET_CLASS (klass)->extract = ges_project_extract;
577 ges_project_init (GESProject * project)
579 GESProjectPrivate *priv = project->priv =
580 ges_project_get_instance_private (project);
583 priv->formatters = NULL;
584 priv->formatter_asset = NULL;
585 priv->encoding_profiles = NULL;
586 priv->assets = g_hash_table_new_full (g_str_hash, g_str_equal,
587 g_free, gst_object_unref);
588 priv->loading_assets = g_hash_table_new_full (g_str_hash, g_str_equal,
589 g_free, gst_object_unref);
590 priv->loaded_with_error = g_hash_table_new_full (g_str_hash, g_str_equal,
595 ges_project_internal_extractable_type_id (GType extractable_type,
598 return g_strdup_printf ("%s:%s", _extractable_type_name (extractable_type),
603 ges_project_internal_asset_id (GESAsset * asset)
606 ges_project_internal_extractable_type_id (ges_asset_get_extractable_type
607 (asset), ges_asset_get_id (asset));
611 _send_error_loading_asset (GESProject * project, GESAsset * asset,
614 gchar *internal_id = ges_project_internal_asset_id (asset);
615 const gchar *id = ges_asset_get_id (asset);
617 GST_DEBUG_OBJECT (project, "Sending error loading asset for %s", id);
618 g_hash_table_remove (project->priv->loading_assets, internal_id);
619 g_hash_table_add (project->priv->loaded_with_error, internal_id);
620 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, error,
621 id, ges_asset_get_extractable_type (asset));
625 ges_project_try_updating_id (GESProject * project, GESAsset * asset,
628 gchar *new_id = NULL;
632 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
633 g_return_val_if_fail (GES_IS_ASSET (asset), NULL);
634 g_return_val_if_fail (error, NULL);
636 id = ges_asset_get_id (asset);
637 GST_DEBUG_OBJECT (project, "Try to proxy %s", id);
638 if (ges_asset_request_id_update (asset, &new_id, error) == FALSE) {
639 GST_DEBUG_OBJECT (project, "Type: %s can not be proxied for id: %s "
640 "and error: %s", g_type_name (G_OBJECT_TYPE (asset)), id,
642 _send_error_loading_asset (project, asset, error);
647 /* Always send the MISSING_URI signal if requesting new ID is possible
648 * so that subclasses of GESProject are aware of the missing-uri */
649 g_signal_emit (project, _signals[MISSING_URI_SIGNAL], 0, error, asset,
653 GST_DEBUG_OBJECT (project, "new id found: %s", new_id);
654 if (!ges_asset_try_proxy (asset, new_id)) {
659 GST_DEBUG_OBJECT (project, "No new id found for %s", id);
662 internal_id = ges_project_internal_asset_id (asset);
663 g_hash_table_remove (project->priv->loading_assets, internal_id);
664 g_free (internal_id);
667 _send_error_loading_asset (project, asset, error);
674 new_asset_cb (GESAsset * source, GAsyncResult * res, GESProject * project)
676 GError *error = NULL;
677 gchar *possible_id = NULL;
678 GESAsset *asset = ges_asset_request_finish (res, &error);
681 possible_id = ges_project_try_updating_id (project, source, error);
682 g_clear_error (&error);
684 if (possible_id == NULL)
687 ges_project_create_asset (project, possible_id,
688 ges_asset_get_extractable_type (source));
690 g_free (possible_id);
694 ges_asset_set_proxy (NULL, asset);
695 ges_project_add_asset (project, asset);
697 gst_object_unref (asset);
701 * ges_project_set_loaded:
702 * @project: The #GESProject from which to emit the "project-loaded" signal
704 * Emits the "loaded" signal. This method should be called by sublasses when
705 * the project is fully loaded.
707 * Returns: %TRUE if the signale could be emitted %FALSE otherwize
710 ges_project_set_loaded (GESProject * project, GESFormatter * formatter)
712 GST_INFO_OBJECT (project, "Emit project loaded");
713 if (GST_STATE (formatter->timeline) < GST_STATE_PAUSED) {
714 timeline_fill_gaps (formatter->timeline);
716 ges_timeline_commit (formatter->timeline);
719 g_signal_emit (project, _signals[LOADED_SIGNAL], 0, formatter->timeline);
721 /* We are now done with that formatter */
722 ges_project_remove_formatter (project, formatter);
727 ges_project_add_loading_asset (GESProject * project, GType extractable_type,
732 if ((asset = ges_asset_cache_lookup (extractable_type, id))) {
733 if (g_hash_table_insert (project->priv->loading_assets,
734 ges_project_internal_asset_id (asset), gst_object_ref (asset)))
735 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
739 /**************************************
741 * API Implementation *
743 **************************************/
746 * ges_project_create_asset:
747 * @project: A #GESProject
748 * @id: (allow-none): The id of the asset to create and add to @project
749 * @extractable_type: The #GType of the asset to create
751 * Create and add a #GESAsset to @project. You should connect to the
752 * "asset-added" signal to get the asset when it finally gets added to
755 * Returns: %TRUE if the asset started to be added %FALSE it was already
759 ges_project_create_asset (GESProject * project, const gchar * id,
760 GType extractable_type)
763 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
764 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
768 id = g_type_name (extractable_type);
769 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
771 if (g_hash_table_lookup (project->priv->assets, internal_id) ||
772 g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
773 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
775 g_free (internal_id);
778 g_free (internal_id);
780 /* TODO Add a GCancellable somewhere in our API */
781 ges_asset_request_async (extractable_type, id, NULL,
782 (GAsyncReadyCallback) new_asset_cb, project);
783 ges_project_add_loading_asset (project, extractable_type, id);
789 * ges_project_create_asset_sync:
790 * @project: A #GESProject
791 * @id: (allow-none): The id of the asset to create and add to @project
792 * @extractable_type: The #GType of the asset to create
793 * @error: A #GError to be set in case of error
795 * Create and add a #GESAsset to @project. You should connect to the
796 * "asset-added" signal to get the asset when it finally gets added to
799 * Returns: (transfer full) (nullable): The newly created #GESAsset or %NULL.
802 ges_project_create_asset_sync (GESProject * project, const gchar * id,
803 GType extractable_type, GError ** error)
806 gchar *possible_id = NULL, *internal_id;
807 gboolean retry = TRUE;
809 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
810 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
814 id = g_type_name (extractable_type);
816 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
817 if ((asset = g_hash_table_lookup (project->priv->assets, internal_id))) {
818 g_free (internal_id);
821 } else if (g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
822 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
823 g_free (internal_id);
828 /* TODO Add a GCancellable somewhere in our API */
831 if (g_type_is_a (extractable_type, GES_TYPE_URI_CLIP)) {
832 asset = GES_ASSET (ges_uri_clip_asset_request_sync (id, error));
834 asset = ges_asset_request (extractable_type, id, error);
840 ges_project_internal_extractable_type_id (extractable_type, id);
841 if ((!g_hash_table_lookup (project->priv->assets, internal_id)))
842 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
843 g_free (internal_id);
846 g_free (possible_id);
847 ges_uri_assets_validate_uri (id);
854 tmpasset = ges_asset_cache_lookup (extractable_type, id);
855 possible_id = ges_project_try_updating_id (project, tmpasset, *error);
857 if (possible_id == NULL) {
858 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, tmpasset);
859 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, *error, id,
865 g_clear_error (error);
871 if (!ges_asset_get_proxy_target (asset))
872 ges_asset_set_proxy (NULL, asset);
874 ges_project_add_asset (project, asset);
880 * ges_project_add_asset:
881 * @project: A #GESProject
882 * @asset: (transfer none): A #GESAsset to add to @project
884 * Adds a #GESAsset to @project, the project will keep a reference on
887 * Returns: %TRUE if the asset could be added %FALSE it was already
891 ges_project_add_asset (GESProject * project, GESAsset * asset)
894 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
896 internal_id = ges_project_internal_asset_id (asset);
897 if (g_hash_table_lookup (project->priv->assets, internal_id)) {
898 g_free (internal_id);
902 g_hash_table_insert (project->priv->assets, internal_id,
903 gst_object_ref (asset));
904 g_hash_table_remove (project->priv->loading_assets, internal_id);
905 GST_DEBUG_OBJECT (project, "Asset added: %s", ges_asset_get_id (asset));
906 g_signal_emit (project, _signals[ASSET_ADDED_SIGNAL], 0, asset);
912 * ges_project_remove_asset:
913 * @project: A #GESProject
914 * @asset: (transfer none): A #GESAsset to remove from @project
916 * remove a @asset to from @project.
918 * Returns: %TRUE if the asset could be removed %FALSE otherwise
921 ges_project_remove_asset (GESProject * project, GESAsset * asset)
926 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
928 internal_id = ges_project_internal_asset_id (asset);
929 ret = g_hash_table_remove (project->priv->assets, internal_id);
930 g_free (internal_id);
931 g_signal_emit (project, _signals[ASSET_REMOVED_SIGNAL], 0, asset);
937 * ges_project_get_asset:
938 * @project: A #GESProject
939 * @id: The id of the asset to retrieve
940 * @extractable_type: The extractable_type of the asset
941 * to retrieve from @object
943 * Returns: (transfer full) (allow-none): The #GESAsset with
944 * @id or %NULL if no asset with @id as an ID
947 ges_project_get_asset (GESProject * project, const gchar * id,
948 GType extractable_type)
953 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
954 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
957 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
958 asset = g_hash_table_lookup (project->priv->assets, internal_id);
959 g_free (internal_id);
962 return gst_object_ref (asset);
968 * ges_project_list_assets:
969 * @project: A #GESProject
970 * @filter: Type of assets to list, `GES_TYPE_EXTRACTABLE` will list
973 * List all @asset contained in @project filtering per extractable_type
974 * as defined by @filter. It copies the asset and thus will not be updated
977 * Returns: (transfer full) (element-type GESAsset): The list of
978 * #GESAsset the object contains
981 ges_project_list_assets (GESProject * project, GType filter)
987 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
989 g_hash_table_iter_init (&iter, project->priv->assets);
990 while (g_hash_table_iter_next (&iter, &key, &value)) {
991 if (g_type_is_a (ges_asset_get_extractable_type (GES_ASSET (value)),
993 ret = g_list_append (ret, gst_object_ref (value));
1001 * @project: A #GESProject to save
1002 * @timeline: The #GESTimeline to save, it must have been extracted from @project
1003 * @uri: The uri where to save @project and @timeline
1004 * @formatter_asset: (allow-none): The formatter asset to use or %NULL. If %NULL,
1005 * will try to save in the same format as the one from which the timeline as been loaded
1006 * or default to the formatter with highest rank
1007 * @overwrite: %TRUE to overwrite file if it exists
1008 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1010 * Save the timeline of @project to @uri. You should make sure that @timeline
1011 * is one of the timelines that have been extracted from @project
1012 * (using ges_asset_extract (@project);)
1014 * Returns: %TRUE if the project could be save, %FALSE otherwize
1017 ges_project_save (GESProject * project, GESTimeline * timeline,
1018 const gchar * uri, GESAsset * formatter_asset, gboolean overwrite,
1022 gboolean ret = TRUE;
1023 GESFormatter *formatter = NULL;
1025 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1026 g_return_val_if_fail (formatter_asset == NULL ||
1027 g_type_is_a (ges_asset_get_extractable_type (formatter_asset),
1028 GES_TYPE_FORMATTER), FALSE);
1029 g_return_val_if_fail ((error == NULL || *error == NULL), FALSE);
1031 tl_asset = ges_extractable_get_asset (GES_EXTRACTABLE (timeline));
1032 if (tl_asset == NULL && project->priv->uri == NULL) {
1033 GESAsset *asset = ges_asset_cache_lookup (GES_TYPE_PROJECT, uri);
1036 GST_WARNING_OBJECT (project, "Trying to save project to %s but we already"
1037 "have %" GST_PTR_FORMAT " for that uri, can not save", uri, asset);
1041 GST_DEBUG_OBJECT (project, "Timeline %" GST_PTR_FORMAT " has no asset"
1042 " we have no uri set, so setting ourself as asset", timeline);
1044 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1045 } else if (tl_asset != GES_ASSET (project)) {
1046 GST_WARNING_OBJECT (project, "Timeline %" GST_PTR_FORMAT
1047 " not created by this project can not save", timeline);
1053 if (formatter_asset == NULL)
1054 formatter_asset = gst_object_ref (ges_formatter_get_default ());
1056 formatter = GES_FORMATTER (ges_asset_extract (formatter_asset, error));
1057 if (formatter == NULL) {
1058 GST_WARNING_OBJECT (project, "Could not create the formatter %p %s: %s",
1059 formatter_asset, ges_asset_get_id (formatter_asset),
1060 (error && *error) ? (*error)->message : "Unknown Error");
1066 ges_project_add_formatter (project, formatter);
1067 ret = ges_formatter_save_to_uri (formatter, timeline, uri, overwrite, error);
1068 if (ret && project->priv->uri == NULL)
1069 ges_project_set_uri (project, uri);
1072 if (formatter_asset)
1073 gst_object_unref (formatter_asset);
1074 ges_project_remove_formatter (project, formatter);
1081 * @uri: (allow-none): The uri to be set after creating the project.
1083 * Creates a new #GESProject and sets its uri to @uri if provided. Note that
1084 * if @uri is not valid or %NULL, the uri of the project will then be set
1085 * the first time you save the project. If you then save the project to
1086 * other locations, it will never be updated again and the first valid URI is
1087 * the URI it will keep refering to.
1089 * Returns: A newly created #GESProject
1092 ges_project_new (const gchar * uri)
1094 gchar *id = (gchar *) uri;
1095 GESProject *project;
1098 id = g_strdup_printf ("project-%i", nb_projects++);
1100 project = GES_PROJECT (ges_asset_request (GES_TYPE_TIMELINE, id, NULL));
1103 ges_project_set_uri (project, uri);
1113 * @project: A #GESProject that has an @uri set already
1114 * @timeline: A blank timeline to load @project into
1115 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1117 * Loads @project into @timeline
1119 * Returns: %TRUE if the project could be loaded %FALSE otherwize.
1122 ges_project_load (GESProject * project, GESTimeline * timeline, GError ** error)
1124 g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1125 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1126 g_return_val_if_fail (ges_project_get_uri (project), FALSE);
1127 g_return_val_if_fail (timeline->tracks == NULL, FALSE);
1129 if (!_load_project (project, timeline, error))
1132 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1138 * ges_project_get_uri:
1139 * @project: A #GESProject
1141 * Retrieve the uri that is currently set on @project
1143 * Returns: (transfer full) (nullable): a newly allocated string representing uri.
1146 ges_project_get_uri (GESProject * project)
1148 GESProjectPrivate *priv;
1150 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1152 priv = project->priv;
1154 return g_strdup (priv->uri);
1159 * ges_project_add_encoding_profile:
1160 * @project: A #GESProject
1161 * @profile: A #GstEncodingProfile to add to the project. If a profile with
1162 * the same name already exists, it will be replaced
1164 * Adds @profile to the project. It lets you save in what format
1165 * the project has been renders and keep a reference to those formats.
1166 * Also, those formats will be saves to the project file when possible.
1168 * Returns: %TRUE if @profile could be added, %FALSE otherwize
1171 ges_project_add_encoding_profile (GESProject * project,
1172 GstEncodingProfile * profile)
1175 GESProjectPrivate *priv;
1177 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1178 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1180 priv = project->priv;
1181 for (tmp = priv->encoding_profiles; tmp; tmp = tmp->next) {
1182 GstEncodingProfile *tmpprofile = GST_ENCODING_PROFILE (tmp->data);
1184 if (g_strcmp0 (gst_encoding_profile_get_name (tmpprofile),
1185 gst_encoding_profile_get_name (profile)) == 0) {
1186 GST_INFO_OBJECT (project, "Already have profile: %s, replacing it",
1187 gst_encoding_profile_get_name (profile));
1189 gst_object_unref (tmp->data);
1190 tmp->data = gst_object_ref (profile);
1195 priv->encoding_profiles = g_list_prepend (priv->encoding_profiles,
1196 gst_object_ref (profile));
1202 * ges_project_list_encoding_profiles:
1203 * @project: A #GESProject
1205 * Lists the encoding profile that have been set to @project. The first one
1206 * is the latest added.
1208 * Returns: (transfer none) (element-type GstPbutils.EncodingProfile) (allow-none): The
1209 * list of #GstEncodingProfile used in @project
1212 ges_project_list_encoding_profiles (GESProject * project)
1214 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1216 return project->priv->encoding_profiles;
1220 * ges_project_get_loading_assets:
1221 * @project: A #GESProject
1223 * Get the assets that are being loaded
1225 * Returns: (transfer full) (element-type GES.Asset): A set of loading asset
1226 * that will be added to @project. Note that those Asset are *not* loaded yet,
1227 * and thus can not be used
1230 ges_project_get_loading_assets (GESProject * project)
1232 GHashTableIter iter;
1233 gpointer key, value;
1237 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1239 g_hash_table_iter_init (&iter, project->priv->loading_assets);
1240 while (g_hash_table_iter_next (&iter, &key, &value))
1241 ret = g_list_prepend (ret, gst_object_ref (value));