From 7caa424aaf29023aa215d773e60c572e12dd1092 Mon Sep 17 00:00:00 2001 From: Thibault Saunier Date: Wed, 3 Jul 2019 20:15:23 -0400 Subject: [PATCH] formatter: Add a method to retrieve the best formatter for a givent URI Uses the file extension as hint falling back to the default formatter if none is found Make use of that function in when saving a project and not formatter is specified. --- ges/ges-formatter.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ges/ges-formatter.h | 3 +++ ges/ges-project.c | 7 ++++--- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/ges/ges-formatter.c b/ges/ges-formatter.c index 36cb6ba..4bbdb0a 100644 --- a/ges/ges-formatter.c +++ b/ges/ges-formatter.c @@ -680,3 +680,56 @@ _find_formatter_asset_for_id (const gchar * id) return asset; } + +/** + * ges_find_formatter_for_uri: + * + * Get the best formatter for @uri. It tries to find a formatter + * compatible with @uri extension, if none is found, it returns the default + * formatter asset. + * + * Returns: (transfer none): The #GESAsset for the best formatter to save to @uri + * + * Since: 1.18 + */ +GESAsset * +ges_find_formatter_for_uri (const gchar * uri) +{ + GList *formatter_assets, *tmp; + GESAsset *asset = NULL; + + gchar *extension = _get_extension (uri); + if (!extension) + return ges_formatter_get_default (); + + formatter_assets = g_list_sort (ges_list_assets (GES_TYPE_FORMATTER), + (GCompareFunc) _sort_formatters); + + for (tmp = formatter_assets; tmp; tmp = tmp->next) { + gint i; + gchar **valid_exts = + g_strsplit (ges_meta_container_get_string (GES_META_CONTAINER + (tmp->data), + GES_META_FORMATTER_EXTENSION), ",", -1); + + for (i = 0; valid_exts[i]; i++) { + if (!g_strcmp0 (extension, valid_exts[i])) { + asset = GES_ASSET (tmp->data); + break; + } + } + + g_strfreev (valid_exts); + if (asset) + break; + } + g_free (extension); + g_list_free (formatter_assets); + + if (asset) { + GST_INFO_OBJECT (asset, "Using for URI %s", uri); + return asset; + } + + return ges_formatter_get_default (); +} diff --git a/ges/ges-formatter.h b/ges/ges-formatter.h index a77f6b8..1f31bf8 100644 --- a/ges/ges-formatter.h +++ b/ges/ges-formatter.h @@ -175,6 +175,9 @@ gboolean ges_formatter_save_to_uri (GESFormatter * formatter, GES_API GESAsset *ges_formatter_get_default (void); +GES_API +GESAsset *ges_find_formatter_for_uri (const gchar *uri); + G_END_DECLS #endif /* _GES_FORMATTER */ diff --git a/ges/ges-project.c b/ges/ges-project.c index 9159af1..2903110 100644 --- a/ges/ges-project.c +++ b/ges/ges-project.c @@ -1058,7 +1058,7 @@ ges_project_list_assets (GESProject * project, GType filter) * @uri: The uri where to save @project and @timeline * @formatter_asset: (allow-none): The formatter asset to use or %NULL. If %NULL, * will try to save in the same format as the one from which the timeline as been loaded - * or default to the formatter with highest rank + * or default to the best formatter as defined in #ges_find_formatter_for_uri * @overwrite: %TRUE to overwrite file if it exists * @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL * @@ -1105,8 +1105,9 @@ ges_project_save (GESProject * project, GESTimeline * timeline, goto out; } - if (formatter_asset == NULL) - formatter_asset = gst_object_ref (ges_formatter_get_default ()); + if (formatter_asset == NULL) { + formatter_asset = gst_object_ref (ges_find_formatter_for_uri (uri)); + } formatter = GES_FORMATTER (ges_asset_extract (formatter_asset, error)); if (formatter == NULL) { -- 2.7.4