1 /* GStreamer Editing Services
3 * Copyright (C) <2012> Thibault Saunier <thibault.saunier@collabora.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 * @short_description: A GESAsset that is used to manage projects
25 * The #GESProject is used to control a set of #GESAsset and is a
26 * #GESAsset with `GES_TYPE_TIMELINE` as @extractable_type itself. That
27 * means that you can extract #GESTimeline from a project as followed:
30 * GESProject *project;
31 * GESTimeline *timeline;
33 * project = ges_project_new ("file:///path/to/a/valid/project/uri");
35 * // Here you can connect to the various signal to get more infos about
36 * // what is happening and recover from errors if possible
39 * timeline = ges_asset_extract (GES_ASSET (project));
42 * The #GESProject class offers a higher level API to handle #GESAsset-s.
43 * It lets you request new asset, and it informs you about new assets through
44 * a set of signals. Also it handles problem such as missing files/missing
45 * #GstElement and lets you try to recover from those.
49 * In order to add a subproject, the only thing to do is to add the subproject
50 * with to the main project:
53 * ges_project_add_asset (project, GES_ASSET (subproject));
55 * then the subproject will be serialized in the project files. To use
56 * the subproject in a timeline, you should use a #GESUriClip with the
57 * same subproject URI.
59 * When loading a project with subproject, subprojects URIs will be temporary
60 * writable local files. If you want to edit the subproject timeline,
61 * you should retrieve the subproject from the parent project asset list and
62 * extract the timeline with ges_asset_extract() and save it at
63 * the same temporary location.
70 #include "ges-internal.h"
72 static GPtrArray *new_paths = NULL;
73 static GHashTable *tried_uris = NULL;
75 struct _GESProjectPrivate
78 /* Set of asset ID being loaded */
79 GHashTable *loading_assets;
80 GHashTable *loaded_with_error;
81 GESAsset *formatter_asset;
87 GList *encoding_profiles;
90 typedef struct EmitLoadedInIdle
93 GESTimeline *timeline;
103 ASSET_REMOVED_SIGNAL,
105 ASSET_LOADING_SIGNAL,
109 G_DEFINE_TYPE_WITH_PRIVATE (GESProject, ges_project, GES_TYPE_ASSET);
111 static guint _signals[LAST_SIGNAL] = { 0 };
113 static guint nb_projects = 0;
115 /* Find the type that implemented the GESExtractable interface */
116 static inline const gchar *
117 _extractable_type_name (GType type)
120 if (g_type_is_a (g_type_parent (type), GES_TYPE_EXTRACTABLE))
121 type = g_type_parent (type);
123 return g_type_name (type);
134 static GParamSpec *_properties[LAST_SIGNAL] = { 0 };
137 _emit_loaded_in_idle (EmitLoadedInIdle * data)
139 g_signal_emit (data->project, _signals[LOADED_SIGNAL], 0, data->timeline);
141 gst_object_unref (data->project);
142 gst_object_unref (data->timeline);
143 g_slice_free (EmitLoadedInIdle, data);
149 * ges_project_add_formatter:
150 * @project: The project to add a formatter to
151 * @formatter: A formatter used by @project
153 * Adds a formatter as used to load @project
158 ges_project_add_formatter (GESProject * project, GESFormatter * formatter)
160 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
162 ges_formatter_set_project (formatter, project);
163 priv->formatters = g_list_append (priv->formatters, formatter);
165 gst_object_ref_sink (formatter);
169 ges_project_remove_formatter (GESProject * project, GESFormatter * formatter)
172 GESProjectPrivate *priv = GES_PROJECT (project)->priv;
174 for (tmp = priv->formatters; tmp; tmp = tmp->next) {
175 if (tmp->data == formatter) {
176 gst_object_unref (formatter);
177 priv->formatters = g_list_delete_link (priv->formatters, tmp);
185 ges_project_set_uri (GESProject * project, const gchar * uri)
187 GESProjectPrivate *priv;
189 g_return_if_fail (GES_IS_PROJECT (project));
191 priv = project->priv;
193 GST_WARNING_OBJECT (project, "Trying to rest URI, this is prohibited");
199 GST_LOG_OBJECT (project, "Uri should not be NULL");
203 priv->uri = g_strdup (uri);
205 /* We use that URI as ID */
206 ges_asset_set_id (GES_ASSET (project), uri);
212 _load_project (GESProject * project, GESTimeline * timeline, GError ** error)
215 GESProjectPrivate *priv;
216 GESFormatter *formatter;
218 priv = GES_PROJECT (project)->priv;
220 g_signal_emit (project, _signals[LOADING_SIGNAL], 0, timeline);
221 if (priv->uri == NULL) {
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, g_cclosure_marshal_generic, 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, g_cclosure_marshal_generic, 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, g_cclosure_marshal_generic, 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, g_cclosure_marshal_generic, G_TYPE_NONE,
541 1, GES_TYPE_TIMELINE);
544 * GESProject::loaded:
545 * @project: the #GESProject that is done loading a timeline.
546 * @timeline: The #GESTimeline that completed loading
548 _signals[LOADED_SIGNAL] =
549 g_signal_new ("loaded", G_TYPE_FROM_CLASS (klass),
550 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESProjectClass, loaded),
551 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
552 1, GES_TYPE_TIMELINE);
555 * GESProject::missing-uri:
556 * @project: the #GESProject reporting that a file has moved
557 * @error: The error that happened
558 * @wrong_asset: The asset with the wrong ID, you should us it and its content
559 * only to find out what the new location is.
563 * source_moved_cb (GESProject *project, GError *error, GESAsset *asset_with_error)
565 * return g_strdup ("file:///the/new/uri.ogg");
569 * main (int argc, gchar ** argv)
571 * GESTimeline *timeline;
572 * GESProject *project = ges_project_new ("file:///some/uri.xges");
574 * g_signal_connect (project, "missing-uri", source_moved_cb, NULL);
575 * timeline = ges_asset_extract (GES_ASSET (project));
579 * Returns: (transfer full) (allow-none): The new URI of @wrong_asset
581 _signals[MISSING_URI_SIGNAL] =
582 g_signal_new ("missing-uri", G_TYPE_FROM_CLASS (klass),
583 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, missing_uri),
584 _uri_missing_accumulator, NULL, g_cclosure_marshal_generic,
585 G_TYPE_STRING, 2, G_TYPE_ERROR, GES_TYPE_ASSET);
588 * GESProject::error-loading-asset:
589 * @project: the #GESProject on which a problem happend when creted a #GESAsset
590 * @error: The #GError defining the error that occured, might be %NULL
591 * @id: The @id of the asset that failed loading
592 * @extractable_type: The @extractable_type of the asset that
595 * Informs you that a #GESAsset could not be created. In case of
596 * missing GStreamer plugins, the error will be set to #GST_CORE_ERROR
597 * #GST_CORE_ERROR_MISSING_PLUGIN
599 _signals[ERROR_LOADING_ASSET] =
600 g_signal_new ("error-loading-asset", G_TYPE_FROM_CLASS (klass),
601 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GESProjectClass, loading_error),
602 NULL, NULL, g_cclosure_marshal_generic,
603 G_TYPE_NONE, 3, G_TYPE_ERROR, G_TYPE_STRING, G_TYPE_GTYPE);
606 * GESProject::error-loading:
607 * @project: the #GESProject on which a problem happend when creted a #GESAsset
608 * @timeline: The timeline that failed loading
609 * @error: The #GError defining the error that occured
613 _signals[ERROR_LOADING] =
614 g_signal_new ("error-loading", G_TYPE_FROM_CLASS (klass),
615 G_SIGNAL_RUN_LAST, 0,
616 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GES_TYPE_TIMELINE,
619 object_class->dispose = _dispose;
620 object_class->finalize = _finalize;
622 GES_ASSET_CLASS (klass)->extract = ges_project_extract;
626 ges_project_init (GESProject * project)
628 GESProjectPrivate *priv = project->priv =
629 ges_project_get_instance_private (project);
632 priv->formatters = NULL;
633 priv->formatter_asset = NULL;
634 priv->encoding_profiles = NULL;
635 priv->assets = g_hash_table_new_full (g_str_hash, g_str_equal,
636 g_free, gst_object_unref);
637 priv->loading_assets = g_hash_table_new_full (g_str_hash, g_str_equal,
638 g_free, gst_object_unref);
639 priv->loaded_with_error = g_hash_table_new_full (g_str_hash, g_str_equal,
644 ges_project_internal_extractable_type_id (GType extractable_type,
647 return g_strdup_printf ("%s:%s", _extractable_type_name (extractable_type),
652 ges_project_internal_asset_id (GESAsset * asset)
655 ges_project_internal_extractable_type_id (ges_asset_get_extractable_type
656 (asset), ges_asset_get_id (asset));
660 _send_error_loading_asset (GESProject * project, GESAsset * asset,
663 gchar *internal_id = ges_project_internal_asset_id (asset);
664 const gchar *id = ges_asset_get_id (asset);
666 GST_DEBUG_OBJECT (project, "Sending error loading asset for %s", id);
667 g_hash_table_remove (project->priv->loading_assets, internal_id);
668 g_hash_table_add (project->priv->loaded_with_error, internal_id);
669 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, error,
670 id, ges_asset_get_extractable_type (asset));
674 ges_project_try_updating_id (GESProject * project, GESAsset * asset,
677 gchar *new_id = NULL;
681 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
682 g_return_val_if_fail (GES_IS_ASSET (asset), NULL);
683 g_return_val_if_fail (error, NULL);
685 id = ges_asset_get_id (asset);
686 GST_DEBUG_OBJECT (project, "Try to proxy %s", id);
687 if (ges_asset_request_id_update (asset, &new_id, error) == FALSE) {
688 GST_DEBUG_OBJECT (project, "Type: %s can not be proxied for id: %s "
689 "and error: %s", g_type_name (G_OBJECT_TYPE (asset)), id,
691 _send_error_loading_asset (project, asset, error);
696 /* Always send the MISSING_URI signal if requesting new ID is possible
697 * so that subclasses of GESProject are aware of the missing-uri */
698 g_signal_emit (project, _signals[MISSING_URI_SIGNAL], 0, error, asset,
702 GST_DEBUG_OBJECT (project, "new id found: %s", new_id);
703 if (!ges_asset_try_proxy (asset, new_id)) {
708 GST_DEBUG_OBJECT (project, "No new id found for %s", id);
711 internal_id = ges_project_internal_asset_id (asset);
712 g_hash_table_remove (project->priv->loading_assets, internal_id);
713 g_free (internal_id);
716 _send_error_loading_asset (project, asset, error);
723 new_asset_cb (GESAsset * source, GAsyncResult * res, GESProject * project)
725 GError *error = NULL;
726 gchar *possible_id = NULL;
727 GESAsset *asset = ges_asset_request_finish (res, &error);
730 possible_id = ges_project_try_updating_id (project, source, error);
731 g_clear_error (&error);
733 if (possible_id == NULL)
736 ges_project_create_asset (project, possible_id,
737 ges_asset_get_extractable_type (source));
739 g_free (possible_id);
743 ges_asset_set_proxy (NULL, asset);
744 ges_project_add_asset (project, asset);
746 gst_object_unref (asset);
750 * ges_project_set_loaded:
751 * @project: The #GESProject from which to emit the "project-loaded" signal
753 * Emits the "loaded" signal. This method should be called by sublasses when
754 * the project is fully loaded.
756 * Returns: %TRUE if the signale could be emitted %FALSE otherwize
759 ges_project_set_loaded (GESProject * project, GESFormatter * formatter,
763 GST_ERROR_OBJECT (project, "Emit project error-loading %s", error->message);
764 g_signal_emit (project, _signals[ERROR_LOADING], 0, formatter->timeline,
768 GST_INFO_OBJECT (project, "Emit project loaded");
769 if (GST_STATE (formatter->timeline) < GST_STATE_PAUSED) {
770 timeline_fill_gaps (formatter->timeline);
772 ges_timeline_commit (formatter->timeline);
775 g_signal_emit (project, _signals[LOADED_SIGNAL], 0, formatter->timeline);
777 /* We are now done with that formatter */
778 ges_project_remove_formatter (project, formatter);
783 ges_project_add_loading_asset (GESProject * project, GType extractable_type,
788 if ((asset = ges_asset_cache_lookup (extractable_type, id))) {
789 if (g_hash_table_insert (project->priv->loading_assets,
790 ges_project_internal_asset_id (asset), gst_object_ref (asset)))
791 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
795 /**************************************
797 * API Implementation *
799 **************************************/
802 * ges_project_create_asset:
803 * @project: A #GESProject
804 * @id: (allow-none): The id of the asset to create and add to @project
805 * @extractable_type: The #GType of the asset to create
807 * Create and add a #GESAsset to @project. You should connect to the
808 * "asset-added" signal to get the asset when it finally gets added to
811 * Returns: %TRUE if the asset started to be added %FALSE it was already
815 ges_project_create_asset (GESProject * project, const gchar * id,
816 GType extractable_type)
819 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
820 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
824 id = g_type_name (extractable_type);
825 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
827 if (g_hash_table_lookup (project->priv->assets, internal_id) ||
828 g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
829 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
831 g_free (internal_id);
834 g_free (internal_id);
836 /* TODO Add a GCancellable somewhere in our API */
837 ges_asset_request_async (extractable_type, id, NULL,
838 (GAsyncReadyCallback) new_asset_cb, project);
839 ges_project_add_loading_asset (project, extractable_type, id);
845 * ges_project_create_asset_sync:
846 * @project: A #GESProject
847 * @id: (allow-none): The id of the asset to create and add to @project
848 * @extractable_type: The #GType of the asset to create
849 * @error: A #GError to be set in case of error
851 * Create and add a #GESAsset to @project. You should connect to the
852 * "asset-added" signal to get the asset when it finally gets added to
855 * Returns: (transfer full) (nullable): The newly created #GESAsset or %NULL.
858 ges_project_create_asset_sync (GESProject * project, const gchar * id,
859 GType extractable_type, GError ** error)
862 gchar *possible_id = NULL, *internal_id;
863 gboolean retry = TRUE;
865 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
866 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
870 id = g_type_name (extractable_type);
872 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
873 if ((asset = g_hash_table_lookup (project->priv->assets, internal_id))) {
874 g_free (internal_id);
877 } else if (g_hash_table_lookup (project->priv->loading_assets, internal_id) ||
878 g_hash_table_lookup (project->priv->loaded_with_error, internal_id)) {
879 g_free (internal_id);
884 /* TODO Add a GCancellable somewhere in our API */
887 if (g_type_is_a (extractable_type, GES_TYPE_URI_CLIP)) {
888 asset = GES_ASSET (ges_uri_clip_asset_request_sync (id, error));
890 asset = ges_asset_request (extractable_type, id, error);
896 ges_project_internal_extractable_type_id (extractable_type, id);
897 if ((!g_hash_table_lookup (project->priv->assets, internal_id)))
898 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, asset);
899 g_free (internal_id);
902 g_free (possible_id);
903 ges_uri_assets_validate_uri (id);
910 tmpasset = ges_asset_cache_lookup (extractable_type, id);
911 possible_id = ges_project_try_updating_id (project, tmpasset, *error);
913 if (possible_id == NULL) {
914 g_signal_emit (project, _signals[ASSET_LOADING_SIGNAL], 0, tmpasset);
915 g_signal_emit (project, _signals[ERROR_LOADING_ASSET], 0, *error, id,
921 g_clear_error (error);
927 if (!ges_asset_get_proxy_target (asset))
928 ges_asset_set_proxy (NULL, asset);
930 ges_project_add_asset (project, asset);
936 * ges_project_add_asset:
937 * @project: A #GESProject
938 * @asset: (transfer none): A #GESAsset to add to @project
940 * Adds a #GESAsset to @project, the project will keep a reference on
943 * Returns: %TRUE if the asset could be added %FALSE it was already
947 ges_project_add_asset (GESProject * project, GESAsset * asset)
950 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
952 internal_id = ges_project_internal_asset_id (asset);
953 if (g_hash_table_lookup (project->priv->assets, internal_id)) {
954 g_free (internal_id);
958 g_hash_table_insert (project->priv->assets, internal_id,
959 gst_object_ref (asset));
960 g_hash_table_remove (project->priv->loading_assets, internal_id);
961 GST_DEBUG_OBJECT (project, "Asset added: %s", ges_asset_get_id (asset));
962 g_signal_emit (project, _signals[ASSET_ADDED_SIGNAL], 0, asset);
968 * ges_project_remove_asset:
969 * @project: A #GESProject
970 * @asset: (transfer none): A #GESAsset to remove from @project
972 * remove a @asset to from @project.
974 * Returns: %TRUE if the asset could be removed %FALSE otherwise
977 ges_project_remove_asset (GESProject * project, GESAsset * asset)
982 g_return_val_if_fail (GES_IS_PROJECT (project), FALSE);
984 internal_id = ges_project_internal_asset_id (asset);
985 ret = g_hash_table_remove (project->priv->assets, internal_id);
986 g_free (internal_id);
987 g_signal_emit (project, _signals[ASSET_REMOVED_SIGNAL], 0, asset);
993 * ges_project_get_asset:
994 * @project: A #GESProject
995 * @id: The id of the asset to retrieve
996 * @extractable_type: The extractable_type of the asset
997 * to retrieve from @object
999 * Returns: (transfer full) (allow-none): The #GESAsset with
1000 * @id or %NULL if no asset with @id as an ID
1003 ges_project_get_asset (GESProject * project, const gchar * id,
1004 GType extractable_type)
1009 g_return_val_if_fail (GES_IS_PROJECT (project), NULL);
1010 g_return_val_if_fail (g_type_is_a (extractable_type, GES_TYPE_EXTRACTABLE),
1013 internal_id = ges_project_internal_extractable_type_id (extractable_type, id);
1014 asset = g_hash_table_lookup (project->priv->assets, internal_id);
1015 g_free (internal_id);
1018 return gst_object_ref (asset);
1024 * ges_project_list_assets:
1025 * @project: A #GESProject
1026 * @filter: Type of assets to list, `GES_TYPE_EXTRACTABLE` will list
1029 * List all @asset contained in @project filtering per extractable_type
1030 * as defined by @filter. It copies the asset and thus will not be updated
1033 * Returns: (transfer full) (element-type GESAsset): The list of
1034 * #GESAsset the object contains
1037 ges_project_list_assets (GESProject * project, GType filter)
1040 GHashTableIter iter;
1041 gpointer key, value;
1043 g_return_val_if_fail (GES_IS_PROJECT (project), 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));