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.
This commit is contained in:
Thibault Saunier 2019-07-03 20:15:23 -04:00
parent c5c451fc1c
commit 7caa424aaf
3 changed files with 60 additions and 3 deletions

View file

@ -680,3 +680,56 @@ _find_formatter_asset_for_id (const gchar * id)
return asset; 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 ();
}

View file

@ -175,6 +175,9 @@ gboolean ges_formatter_save_to_uri (GESFormatter * formatter,
GES_API GES_API
GESAsset *ges_formatter_get_default (void); GESAsset *ges_formatter_get_default (void);
GES_API
GESAsset *ges_find_formatter_for_uri (const gchar *uri);
G_END_DECLS G_END_DECLS
#endif /* _GES_FORMATTER */ #endif /* _GES_FORMATTER */

View file

@ -1058,7 +1058,7 @@ ges_project_list_assets (GESProject * project, GType filter)
* @uri: The uri where to save @project and @timeline * @uri: The uri where to save @project and @timeline
* @formatter_asset: (allow-none): The formatter asset to use or %NULL. If %NULL, * @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 * 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 * @overwrite: %TRUE to overwrite file if it exists
* @error: (out) (allow-none): An error to be set in case something wrong happens or %NULL * @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; goto out;
} }
if (formatter_asset == NULL) if (formatter_asset == NULL) {
formatter_asset = gst_object_ref (ges_formatter_get_default ()); formatter_asset = gst_object_ref (ges_find_formatter_for_uri (uri));
}
formatter = GES_FORMATTER (ges_asset_extract (formatter_asset, error)); formatter = GES_FORMATTER (ges_asset_extract (formatter_asset, error));
if (formatter == NULL) { if (formatter == NULL) {