1 /* GStreamer Editing Services
3 * Copyright (C) <2012> Thibault Saunier <thibault.saunier@collabora.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 * @short_description: A GESAsset that is used to manage projects
25 * The #GESProject is used to control a set of #GESAsset and is a
26 * #GESAsset with `GES_TYPE_TIMELINE` as @extractable_type itself. That
27 * means that you can extract #GESTimeline from a project as followed:
30 * GESProject *project;
31 * GESTimeline *timeline;
33 * project = ges_project_new ("file:///path/to/a/valid/project/uri");
35 * // Here you can connect to the various signal to get more infos about
36 * // what is happening and recover from errors if possible
39 * timeline = ges_asset_extract (GES_ASSET (project));
42 * The #GESProject class offers a higher level API to handle #GESAsset-s.
43 * It lets you request new asset, and it informs you about new assets through
44 * a set of signals. Also it handles problem such as missing files/missing
45 * #GstElement and lets you try to recover from those.
49 * In order to add a subproject, the only thing to do is to add the subproject
50 * with to the main project:
53 * ges_project_add_asset (project, GES_ASSET (subproject));
55 * then the subproject will be serialized in the project files. To use
56 * the subproject in a timeline, you should use a #GESUriClip with the
57 * same subproject URI.
59 * When loading a project with subproject, subprojects URIs will be temporary
60 * writable local files. If you want to edit the subproject timeline,
61 * you should retrieve the subproject from the parent project asset list and
62 * extract the timeline with ges_asset_extract() and save it at
63 * the same temporary location.
70 #include "ges-internal.h"
72 static GPtrArray *new_paths = NULL;
73 static GHashTable *tried_uris = NULL;
75 struct _GESProjectPrivate
78 /* Set of asset ID being loaded */
79 GHashTable *loading_assets;
80 GHashTable *loaded_with_error;
81 GESAsset *formatter_asset;
87 GList *encoding_profiles;
90 typedef struct EmitLoadedInIdle
93 GESTimeline *timeline;
102 ASSET_REMOVED_SIGNAL,
104 ASSET_LOADING_SIGNAL,
108 G_DEFINE_TYPE_WITH_PRIVATE (GESProject, ges_project, GES_TYPE_ASSET);
110 static guint _signals[LAST_SIGNAL] = { 0 };
112 static guint nb_projects = 0;
114 /* Find the type that implemented the GESExtractable interface */
115 static inline const gchar *
116 _extractable_type_name (GType type)
119 if (g_type_is_a (g_type_parent (type), GES_TYPE_EXTRACTABLE))
120 type = g_type_parent (type);
122 return g_type_name (type);
133 static GParamSpec *_properties[LAST_SIGNAL] = { 0 };
136 _emit_loaded_in_idle (EmitLoadedInIdle * data)
138 g_signal_emit (data->project, _signals[LOADED_SIGNAL], 0, data->timeline);
140 gst_object_unref (data->project);
141 gst_object_unref (data->timeline);
142 g_slice_free (EmitLoadedInIdle, data);
148 ges_project_add_formatter (GESProject * project, GESFormatter * formatter)
150 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
152 ges_formatter_set_project (formatter, project);
153 priv->formatters = g_list_append (priv->formatters, formatter);
155 gst_object_ref_sink (formatter);
159 ges_project_remove_formatter (GESProject * project, GESFormatter * formatter)
162 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
164 for (tmp = priv->formatters; tmp; tmp = tmp->next) {
165 if (tmp->data == formatter) {
166 gst_object_unref (formatter);
167 priv->formatters = g_list_delete_link (priv->formatters, tmp);
175 ges_project_set_uri (GESProject * project, const gchar * uri)
177 GESProjectPrivate *priv;
179 g_return_if_fail (GES_IS_PROJECT (project));
181 priv = project->priv;
183 GST_WARNING_OBJECT (project, "Trying to rest URI, this is prohibited");
189 GST_LOG_OBJECT (project, "Uri should not be NULL");
193 priv->uri = g_strdup (uri);
195 /* We use that URI as ID */
196 ges_asset_set_id (GES_ASSET (project), uri);
202 _load_project (GESProject * project, GESTimeline * timeline, GError ** error)
205 GESProjectPrivate *priv;
206 GESFormatter *formatter;
208 priv = GES_PROJECT (project)->priv;
210 g_signal_emit (project, _signals[LOADING_SIGNAL], 0, timeline);
211 if (priv->uri == NULL) {
212 EmitLoadedInIdle *data = g_slice_new (EmitLoadedInIdle);
214 GST_LOG_OBJECT (project, "%s, Loading an empty timeline %s"
215 " as no URI set yet", GST_OBJECT_NAME (timeline),
216 ges_asset_get_id (GES_ASSET (project)));
218 data->timeline = gst_object_ref (timeline);
219 data->project = gst_object_ref (project);
221 /* Make sure the signal is emitted after the functions ends */
222 g_idle_add ((GSourceFunc) _emit_loaded_in_idle, data);
226 if (priv->formatter_asset == NULL)
227 priv->formatter_asset = _find_formatter_asset_for_id (priv->uri);
229 if (priv->formatter_asset == NULL) {
230 lerr = g_error_new (GES_ERROR, 0, "Could not find a suitable formatter");
234 formatter = GES_FORMATTER (ges_asset_extract (priv->formatter_asset, &lerr));
236 GST_WARNING_OBJECT (project, "Could not create the formatter: %s",
242 ges_project_add_formatter (GES_PROJECT (project), formatter);
243 ges_formatter_load_from_uri (formatter, timeline, priv->uri, &lerr);
245 GST_WARNING_OBJECT (project, "Could not load the timeline,"
246 " returning: %s", lerr->message);
254 g_propagate_error (error, lerr);
259 _uri_missing_accumulator (GSignalInvocationHint * ihint, GValue * return_accu,
260 const GValue * handler_return, gpointer data)
262 const gchar *ret = g_value_get_string (handler_return);
265 if (!gst_uri_is_valid (ret)) {
266 GST_INFO ("The uri %s was not valid, can not work with it!", ret);
270 g_value_set_string (return_accu, ret);
277 /* GESAsset vmethod implementation */
278 static GESExtractable *
279 ges_project_extract (GESAsset * project, GError ** error)
281 GESTimeline *timeline = g_object_new (GES_TYPE_TIMELINE, NULL);
283 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
284 if (_load_project (GES_PROJECT (project), timeline, error))
285 return GES_EXTRACTABLE (timeline);
287 gst_object_unref (timeline);
292 _add_media_new_paths_recursing (const gchar * value)
295 GFileEnumerator *fenum;
296 GFile *file = g_file_new_for_uri (value);
298 if (!(fenum = g_file_enumerate_children (file,
299 "standard::*", G_FILE_QUERY_INFO_NONE, NULL, NULL))) {
300 GST_INFO ("%s is not a folder", value);
305 GST_INFO ("Adding folder: %s", value);
306 g_ptr_array_add (new_paths, g_strdup (value));
307 info = g_file_enumerator_next_file (fenum, NULL, NULL);
308 while (info != NULL) {
309 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
310 GFile *f = g_file_enumerator_get_child (fenum, info);
311 gchar *uri = g_file_get_uri (f);
313 _add_media_new_paths_recursing (uri);
314 gst_object_unref (f);
317 g_object_unref (info);
318 info = g_file_enumerator_next_file (fenum, NULL, NULL);
322 gst_object_unref (file);
324 gst_object_unref (fenum);
328 ges_add_missing_uri_relocation_uri (const gchar * uri, gboolean recurse)
330 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
332 if (new_paths == NULL)
333 new_paths = g_ptr_array_new_with_free_func (g_free);
336 _add_media_new_paths_recursing (uri);
338 g_ptr_array_add (new_paths, g_strdup (uri));
344 ges_missing_uri_default (GESProject * self, GError * error,
345 GESAsset * wrong_asset)
348 const gchar *old_uri = ges_asset_get_id (wrong_asset);
349 gchar *new_id = NULL;
351 if (ges_asset_request_id_update (wrong_asset, &new_id, error) && new_id) {
352 GST_INFO_OBJECT (self, "Returned guessed new ID: %s", new_id);
357 if (new_paths == NULL)
360 if (tried_uris == NULL)
361 tried_uris = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
363 for (i = 0; i < new_paths->len; i++) {
364 gchar *basename, *res;
366 basename = g_path_get_basename (old_uri);
367 res = g_build_filename (new_paths->pdata[i], basename, NULL);
370 if (g_strcmp0 (old_uri, res) == 0) {
371 g_hash_table_add (tried_uris, res);
372 } else if (g_hash_table_lookup (tried_uris, res)) {
373 GST_DEBUG_OBJECT (self, "File already tried: %s", res);
376 g_hash_table_add (tried_uris, g_strdup (res));
377 GST_DEBUG_OBJECT (self, "Trying: %s\n", res);
386 ges_uri_asset_try_update_id (GError * error, GESAsset * wrong_asset)
388 return ges_missing_uri_default (NULL, error, wrong_asset);
392 ges_uri_assets_validate_uri (const gchar * nid)
395 g_hash_table_remove (tried_uris, nid);
398 /* GObject vmethod implementation */
400 _dispose (GObject * object)
402 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
405 g_hash_table_unref (priv->assets);
406 if (priv->loading_assets)
407 g_hash_table_unref (priv->loading_assets);
408 if (priv->loaded_with_error)
409 g_hash_table_unref (priv->loaded_with_error);
410 if (priv->formatter_asset)
411 gst_object_unref (priv->formatter_asset);
413 while (priv->formatters)
414 ges_project_remove_formatter (GES_PROJECT (object), priv->formatters->data);
416 G_OBJECT_CLASS (ges_project_parent_class)->dispose (object);
420 _finalize (GObject * object)
422 GESProjectPrivate *priv = GES_PROJECT (object)->priv;
426 g_list_free_full (priv->encoding_profiles, g_object_unref);
428 G_OBJECT_CLASS (ges_project_parent_class)->finalize (object);
432 _get_property (GESProject * project, guint property_id,
433 GValue * value, GParamSpec * pspec)
435 GESProjectPrivate *priv = project->priv;
437 switch (property_id) {
439 g_value_set_string (value, priv->uri);
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
447 _set_property (GESProject * project, guint property_id,
448 const GValue * value, GParamSpec * pspec)
450 switch (property_id) {
452 project->priv->uri = g_value_dup_string (value);
455 G_OBJECT_WARN_INVALID_PROPERTY_ID (project, property_id, pspec);
460 ges_project_class_init (GESProjectClass * klass)
462 GObjectClass *object_class = G_OBJECT_CLASS (klass);
464 klass->asset_added = NULL;
465 klass->missing_uri = ges_missing_uri_default;
466 klass->loading_error = NULL;
467 klass->asset_removed = NULL;
468 object_class->get_property = (GObjectGetPropertyFunc) _get_property;
469 object_class->set_property = (GObjectSetPropertyFunc) _set_property;
474 * The location of the project to use.
476 _properties[PROP_URI] = g_param_spec_string ("uri", "URI",
477 "uri of the project", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
479 g_object_class_install_properties (object_class, PROP_LAST, _properties);
482 * GESProject::asset-added:
483 * @project: the #GESProject
484 * @asset: The #GESAsset that has been added to @project
486 _signals[ASSET_ADDED_SIGNAL] =
487 g_signal_new ("asset-added", G_TYPE_FROM_CLASS (klass),
488 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_added),
489 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
492 * GESProject::asset-loading:
493 * @project: the #GESProject
494 * @asset: The #GESAsset that started loading
498 _signals[ASSET_LOADING_SIGNAL] =
499 g_signal_new ("asset-loading", G_TYPE_FROM_CLASS (klass),
500 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_loading),
501 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
504 * GESProject::asset-removed:
505 * @project: the #GESProject
506 * @asset: The #GESAsset that has been removed from @project
508 _signals[ASSET_REMOVED_SIGNAL] =
509 g_signal_new ("asset-removed", G_TYPE_FROM_CLASS (klass),
510 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, asset_removed),
511 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GES_TYPE_ASSET);
514 * GESProject::loading:
515 * @project: the #GESProject that is starting to load a timeline
516 * @timeline: The #GESTimeline that started loading
520 _signals[LOADING_SIGNAL] =
521 g_signal_new ("loading", G_TYPE_FROM_CLASS (klass),
522 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loading),
523 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
524 1, GES_TYPE_TIMELINE);
527 * GESProject::loaded:
528 * @project: the #GESProject that is done loading a timeline.
529 * @timeline: The #GESTimeline that completed loading
531 _signals[LOADED_SIGNAL] =
532 g_signal_new ("loaded", G_TYPE_FROM_CLASS (klass),
533 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loaded),
534 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
535 1, GES_TYPE_TIMELINE);
538 * GESProject::missing-uri:
539 * @project: the #GESProject reporting that a file has moved
540 * @error: The error that happened
541 * @wrong_asset: The asset with the wrong ID, you should us it and its content
542 * only to find out what the new location is.
546 * source_moved_cb (GESProject *project, GError *error, GESAsset *asset_with_error)
548 * return g_strdup ("file:///the/new/uri.ogg");
552 * main (int argc, gchar ** argv)
554 * GESTimeline *timeline;
555 * GESProject *project = ges_project_new ("file:///some/uri.xges");
557 * g_signal_connect (project, "missing-uri", source_moved_cb, NULL);
558 * timeline = ges_asset_extract (GES_ASSET (project));
562 * Returns: (transfer full) (allow-none): The new URI of @wrong_asset
564 _signals[MISSING_URI_SIGNAL] =
565 g_signal_new ("missing-uri", G_TYPE_FROM_CLASS (klass),
566 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, missing_uri),
567 _uri_missing_accumulator, NULL, g_cclosure_marshal_generic,
568 G_TYPE_STRING, 2, G_TYPE_ERROR, GES_TYPE_ASSET);
571 * GESProject::error-loading-asset:
572 * @project: the #GESProject on which a problem happend when creted a #GESAsset
573 * @error: The #GError defining the error that occured, might be %NULL
574 * @id: The @id of the asset that failed loading
575 * @extractable_type: The @extractable_type of the asset that
578 * Informs you that a #GESAsset could not be created. In case of
579 * missing GStreamer plugins, the error will be set to #GST_CORE_ERROR
580 * #GST_CORE_ERROR_MISSING_PLUGIN
582 _signals[ERROR_LOADING_ASSET] =
583 g_signal_new ("error-loading-asset", G_TYPE_FROM_CLASS (klass),
584 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, loading_error),
585 NULL, NULL, g_cclosure_marshal_generic,
586 G_TYPE_NONE, 3, G_TYPE_ERROR, G_TYPE_STRING, G_TYPE_GTYPE);
588 object_class->dispose = _dispose;
589 object_class->finalize = _finalize;
591 GES_ASSET_CLASS (klass)->extract = ges_project_extract;
595 ges_project_init (GESProject * project)
597 GESProjectPrivate *priv = project->priv =
598 ges_project_get_instance_private (project);
601 priv->formatters = NULL;
602 priv->formatter_asset = NULL;
603 priv->encoding_profiles = NULL;
604 priv->assets = g_hash_table_new_full (g_str_hash, g_str_equal,
605 g_free, gst_object_unref);
606 priv->loading_assets = g_hash_table_new_full (g_str_hash, g_str_equal,
607 g_free, gst_object_unref);
608 priv->loaded_with_error = g_hash_table_new_full (g_str_hash, g_str_equal,
613 ges_project_internal_extractable_type_id (GType extractable_type,
616 return g_strdup_printf ("%s:%s", _extractable_type_name (extractable_type),
621 ges_project_internal_asset_id (GESAsset * asset)
624 ges_project_internal_extractable_type_id (ges_asset_get_extractable_type
625 (asset), ges_asset_get_id (asset));
629 _send_error_loading_asset (GESProject * project, GESAsset * asset,
632 gchar *internal_id = ges_project_internal_asset_id (asset);
633 const gchar *id = ges_asset_get_id (asset);
635 GST_DEBUG_OBJECT (project, "Sending error loading asset for %s", id);
636 g_hash_table_remove (project->priv->loading_assets, internal_id);
637 g_hash_table_add (project->priv->loaded_with_error, internal_id);
638 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, error,
639 id, ges_asset_get_extractable_type (asset));
643 ges_project_try_updating_id (GESProject * project, GESAsset * asset,
646 gchar *new_id = NULL;
650 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
651 g_return_val_if_fail (GES_IS_ASSET (asset), NULL);
652 g_return_val_if_fail (error, NULL);
654 id = ges_asset_get_id (asset);
655 GST_DEBUG_OBJECT (project, "Try to proxy %s", id);
656 if (ges_asset_request_id_update (asset, &new_id, error) == FALSE) {
657 GST_DEBUG_OBJECT (project, "Type: %s can not be proxied for id: %s "
658 "and error: %s", g_type_name (G_OBJECT_TYPE (asset)), id,
660 _send_error_loading_asset (project, asset, error);
665 /* Always send the MISSING_URI signal if requesting new ID is possible
666 * so that subclasses of GESProject are aware of the missing-uri */
667 g_signal_emit (project, _signals[MISSING_URI_SIGNAL], 0, error, asset,
671 GST_DEBUG_OBJECT (project, "new id found: %s", new_id);
672 if (!ges_asset_try_proxy (asset, new_id)) {
677 GST_DEBUG_OBJECT (project, "No new id found for %s", id);
680 internal_id = ges_project_internal_asset_id (asset);
681 g_hash_table_remove (project->priv->loading_assets, internal_id);
682 g_free (internal_id);
685 _send_error_loading_asset (project, asset, error);
692 new_asset_cb (GESAsset * source, GAsyncResult * res, GESProject * project)
694 GError *error = NULL;
695 gchar *possible_id = NULL;
696 GESAsset *asset = ges_asset_request_finish (res, &error);
699 possible_id = ges_project_try_updating_id (project, source, error);
700 g_clear_error (&error);
702 if (possible_id == NULL)
705 ges_project_create_asset (project, possible_id,
706 ges_asset_get_extractable_type (source));
708 g_free (possible_id);
712 ges_asset_set_proxy (NULL, asset);
713 ges_project_add_asset (project, asset);
715 gst_object_unref (asset);
719 * ges_project_set_loaded:
720 * @project: The #GESProject from which to emit the "project-loaded" signal
722 * Emits the "loaded" signal. This method should be called by sublasses when
723 * the project is fully loaded.
725 * Returns: %TRUE if the signale could be emitted %FALSE otherwize
728 ges_project_set_loaded (GESProject * project, GESFormatter * formatter)
730 GST_INFO_OBJECT (project, "Emit project loaded");
731 if (GST_STATE (formatter->timeline) < GST_STATE_PAUSED) {
732 timeline_fill_gaps (formatter->timeline);
734 ges_timeline_commit (formatter->timeline);
737 g_signal_emit (project, _signals[LOADED_SIGNAL], 0, formatter->timeline);
739 /* We are now done with that formatter */
740 ges_project_remove_formatter (project, formatter);
745 ges_project_add_loading_asset (GESProject * project, GType extractable_type,
750 if ((asset = ges_asset_cache_lookup (extractable_type, id))) {
751 if (g_hash_table_insert (project->priv->loading_assets,
752 ges_project_internal_asset_id (asset), gst_object_ref (asset)))
753 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
757 /**************************************
759 * API Implementation *
761 **************************************/
764 * ges_project_create_asset:
765 * @project: A #GESProject
766 * @id: (allow-none): The id of the asset to create and add to @project
767 * @extractable_type: The #GType of the asset to create
769 * Create and add a #GESAsset to @project. You should connect to the
770 * "asset-added" signal to get the asset when it finally gets added to
773 * Returns: %TRUE if the asset started to be added %FALSE it was already
777 ges_project_create_asset (GESProject * project, const gchar * id,
778 GType extractable_type)
781 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
782 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
786 id = g_type_name (extractable_type);
787 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
789 if (g_hash_table_lookup (project->priv->assets, internal_id) ||
790 g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
791 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
793 g_free (internal_id);
796 g_free (internal_id);
798 /* TODO Add a GCancellable somewhere in our API */
799 ges_asset_request_async (extractable_type, id, NULL,
800 (GAsyncReadyCallback) new_asset_cb, project);
801 ges_project_add_loading_asset (project, extractable_type, id);
807 * ges_project_create_asset_sync:
808 * @project: A #GESProject
809 * @id: (allow-none): The id of the asset to create and add to @project
810 * @extractable_type: The #GType of the asset to create
811 * @error: A #GError to be set in case of error
813 * Create and add a #GESAsset to @project. You should connect to the
814 * "asset-added" signal to get the asset when it finally gets added to
817 * Returns: (transfer full) (nullable): The newly created #GESAsset or %NULL.
820 ges_project_create_asset_sync (GESProject * project, const gchar * id,
821 GType extractable_type, GError ** error)
824 gchar *possible_id = NULL, *internal_id;
825 gboolean retry = TRUE;
827 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
828 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
832 id = g_type_name (extractable_type);
834 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
835 if ((asset = g_hash_table_lookup (project->priv->assets, internal_id))) {
836 g_free (internal_id);
839 } else if (g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
840 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
841 g_free (internal_id);
846 /* TODO Add a GCancellable somewhere in our API */
849 if (g_type_is_a (extractable_type, GES_TYPE_URI_CLIP)) {
850 asset = GES_ASSET (ges_uri_clip_asset_request_sync (id, error));
852 asset = ges_asset_request (extractable_type, id, error);
858 ges_project_internal_extractable_type_id (extractable_type, id);
859 if ((!g_hash_table_lookup (project->priv->assets, internal_id)))
860 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
861 g_free (internal_id);
864 g_free (possible_id);
865 ges_uri_assets_validate_uri (id);
872 tmpasset = ges_asset_cache_lookup (extractable_type, id);
873 possible_id = ges_project_try_updating_id (project, tmpasset, *error);
875 if (possible_id == NULL) {
876 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, tmpasset);
877 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, *error, id,
883 g_clear_error (error);
889 if (!ges_asset_get_proxy_target (asset))
890 ges_asset_set_proxy (NULL, asset);
892 ges_project_add_asset (project, asset);
898 * ges_project_add_asset:
899 * @project: A #GESProject
900 * @asset: (transfer none): A #GESAsset to add to @project
902 * Adds a #GESAsset to @project, the project will keep a reference on
905 * Returns: %TRUE if the asset could be added %FALSE it was already
909 ges_project_add_asset (GESProject * project, GESAsset * asset)
912 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
914 internal_id = ges_project_internal_asset_id (asset);
915 if (g_hash_table_lookup (project->priv->assets, internal_id)) {
916 g_free (internal_id);
920 g_hash_table_insert (project->priv->assets, internal_id,
921 gst_object_ref (asset));
922 g_hash_table_remove (project->priv->loading_assets, internal_id);
923 GST_DEBUG_OBJECT (project, "Asset added: %s", ges_asset_get_id (asset));
924 g_signal_emit (project, _signals[ASSET_ADDED_SIGNAL], 0, asset);
930 * ges_project_remove_asset:
931 * @project: A #GESProject
932 * @asset: (transfer none): A #GESAsset to remove from @project
934 * remove a @asset to from @project.
936 * Returns: %TRUE if the asset could be removed %FALSE otherwise
939 ges_project_remove_asset (GESProject * project, GESAsset * asset)
944 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
946 internal_id = ges_project_internal_asset_id (asset);
947 ret = g_hash_table_remove (project->priv->assets, internal_id);
948 g_free (internal_id);
949 g_signal_emit (project, _signals[ASSET_REMOVED_SIGNAL], 0, asset);
955 * ges_project_get_asset:
956 * @project: A #GESProject
957 * @id: The id of the asset to retrieve
958 * @extractable_type: The extractable_type of the asset
959 * to retrieve from @object
961 * Returns: (transfer full) (allow-none): The #GESAsset with
962 * @id or %NULL if no asset with @id as an ID
965 ges_project_get_asset (GESProject * project, const gchar * id,
966 GType extractable_type)
971 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
972 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
975 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
976 asset = g_hash_table_lookup (project->priv->assets, internal_id);
977 g_free (internal_id);
980 return gst_object_ref (asset);
986 * ges_project_list_assets:
987 * @project: A #GESProject
988 * @filter: Type of assets to list, `GES_TYPE_EXTRACTABLE` will list
991 * List all @asset contained in @project filtering per extractable_type
992 * as defined by @filter. It copies the asset and thus will not be updated
995 * Returns: (transfer full) (element-type GESAsset): The list of
996 * #GESAsset the object contains
999 ges_project_list_assets (GESProject * project, GType filter)
1002 GHashTableIter iter;
1003 gpointer key, value;
1005 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1007 g_hash_table_iter_init (&iter, project->priv->assets);
1008 while (g_hash_table_iter_next (&iter, &key, &value)) {
1009 if (g_type_is_a (ges_asset_get_extractable_type (GES_ASSET (value)),
1011 ret = g_list_append (ret, gst_object_ref (value));
1019 * @project: A #GESProject to save
1020 * @timeline: The #GESTimeline to save, it must have been extracted from @project
1021 * @uri: The uri where to save @project and @timeline
1022 * @formatter_asset: (allow-none): The formatter asset to use or %NULL. If %NULL,
1023 * will try to save in the same format as the one from which the timeline as been loaded
1024 * or default to the formatter with highest rank
1025 * @overwrite: %TRUE to overwrite file if it exists
1026 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1028 * Save the timeline of @project to @uri. You should make sure that @timeline
1029 * is one of the timelines that have been extracted from @project
1030 * (using ges_asset_extract (@project);)
1032 * Returns: %TRUE if the project could be save, %FALSE otherwize
1035 ges_project_save (GESProject * project, GESTimeline * timeline,
1036 const gchar * uri, GESAsset * formatter_asset, gboolean overwrite,
1040 gboolean ret = TRUE;
1041 GESFormatter *formatter = NULL;
1043 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1044 g_return_val_if_fail (formatter_asset == NULL ||
1045 g_type_is_a (ges_asset_get_extractable_type (formatter_asset),
1046 GES_TYPE_FORMATTER), FALSE);
1047 g_return_val_if_fail ((error == NULL || *error == NULL), FALSE);
1049 tl_asset = ges_extractable_get_asset (GES_EXTRACTABLE (timeline));
1050 if (tl_asset == NULL && project->priv->uri == NULL) {
1051 GESAsset *asset = ges_asset_cache_lookup (GES_TYPE_PROJECT, uri);
1054 GST_WARNING_OBJECT (project, "Trying to save project to %s but we already"
1055 "have %" GST_PTR_FORMAT " for that uri, can not save", uri, asset);
1059 GST_DEBUG_OBJECT (project, "Timeline %" GST_PTR_FORMAT " has no asset"
1060 " we have no uri set, so setting ourself as asset", timeline);
1062 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1063 } else if (tl_asset != GES_ASSET (project)) {
1064 GST_WARNING_OBJECT (project, "Timeline %" GST_PTR_FORMAT
1065 " not created by this project can not save", timeline);
1071 if (formatter_asset == NULL)
1072 formatter_asset = gst_object_ref (ges_formatter_get_default ());
1074 formatter = GES_FORMATTER (ges_asset_extract (formatter_asset, error));
1075 if (formatter == NULL) {
1076 GST_WARNING_OBJECT (project, "Could not create the formatter %p %s: %s",
1077 formatter_asset, ges_asset_get_id (formatter_asset),
1078 (error && *error) ? (*error)->message : "Unknown Error");
1084 ges_project_add_formatter (project, formatter);
1085 ret = ges_formatter_save_to_uri (formatter, timeline, uri, overwrite, error);
1086 if (ret && project->priv->uri == NULL)
1087 ges_project_set_uri (project, uri);
1090 if (formatter_asset)
1091 gst_object_unref (formatter_asset);
1092 ges_project_remove_formatter (project, formatter);
1099 * @uri: (allow-none): The uri to be set after creating the project.
1101 * Creates a new #GESProject and sets its uri to @uri if provided. Note that
1102 * if @uri is not valid or %NULL, the uri of the project will then be set
1103 * the first time you save the project. If you then save the project to
1104 * other locations, it will never be updated again and the first valid URI is
1105 * the URI it will keep refering to.
1107 * Returns: A newly created #GESProject
1110 ges_project_new (const gchar * uri)
1112 gchar *id = (gchar *) uri;
1113 GESProject *project;
1116 id = g_strdup_printf ("project-%i", nb_projects++);
1118 project = GES_PROJECT (ges_asset_request (GES_TYPE_TIMELINE, id, NULL));
1121 ges_project_set_uri (project, uri);
1131 * @project: A #GESProject that has an @uri set already
1132 * @timeline: A blank timeline to load @project into
1133 * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL
1135 * Loads @project into @timeline
1137 * Returns: %TRUE if the project could be loaded %FALSE otherwize.
1140 ges_project_load (GESProject * project, GESTimeline * timeline, GError ** error)
1142 g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
1143 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1144 g_return_val_if_fail (ges_project_get_uri (project), FALSE);
1145 g_return_val_if_fail (timeline->tracks == NULL, FALSE);
1147 if (!_load_project (project, timeline, error))
1150 ges_extractable_set_asset (GES_EXTRACTABLE (timeline), GES_ASSET (project));
1156 * ges_project_get_uri:
1157 * @project: A #GESProject
1159 * Retrieve the uri that is currently set on @project
1161 * Returns: (transfer full) (nullable): a newly allocated string representing uri.
1164 ges_project_get_uri (GESProject * project)
1166 GESProjectPrivate *priv;
1168 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1170 priv = project->priv;
1172 return g_strdup (priv->uri);
1177 * ges_project_add_encoding_profile:
1178 * @project: A #GESProject
1179 * @profile: A #GstEncodingProfile to add to the project. If a profile with
1180 * the same name already exists, it will be replaced
1182 * Adds @profile to the project. It lets you save in what format
1183 * the project has been renders and keep a reference to those formats.
1184 * Also, those formats will be saves to the project file when possible.
1186 * Returns: %TRUE if @profile could be added, %FALSE otherwize
1189 ges_project_add_encoding_profile (GESProject * project,
1190 GstEncodingProfile * profile)
1193 GESProjectPrivate *priv;
1195 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1196 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1198 priv = project->priv;
1199 for (tmp = priv->encoding_profiles; tmp; tmp = tmp->next) {
1200 GstEncodingProfile *tmpprofile = GST_ENCODING_PROFILE (tmp->data);
1202 if (g_strcmp0 (gst_encoding_profile_get_name (tmpprofile),
1203 gst_encoding_profile_get_name (profile)) == 0) {
1204 GST_INFO_OBJECT (project, "Already have profile: %s, replacing it",
1205 gst_encoding_profile_get_name (profile));
1207 gst_object_unref (tmp->data);
1208 tmp->data = gst_object_ref (profile);
1213 priv->encoding_profiles = g_list_prepend (priv->encoding_profiles,
1214 gst_object_ref (profile));
1220 * ges_project_list_encoding_profiles:
1221 * @project: A #GESProject
1223 * Lists the encoding profile that have been set to @project. The first one
1224 * is the latest added.
1226 * Returns: (transfer none) (element-type GstPbutils.EncodingProfile) (allow-none): The
1227 * list of #GstEncodingProfile used in @project
1230 ges_project_list_encoding_profiles (GESProject * project)
1232 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
1234 return project->priv->encoding_profiles;
1238 * ges_project_get_loading_assets:
1239 * @project: A #GESProject
1241 * Get the assets that are being loaded
1243 * Returns: (transfer full) (element-type GES.Asset): A set of loading asset
1244 * that will be added to @project. Note that those Asset are *not* loaded yet,
1245 * and thus can not be used
1248 ges_project_get_loading_assets (GESProject * project)
1250 GHashTableIter iter;
1251 gpointer key, value;
1255 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1257 g_hash_table_iter_init (&iter, project->priv->loading_assets);
1258 while (g_hash_table_iter_next (&iter, &key, &value))
1259 ret = g_list_prepend (ret, gst_object_ref (value));