From bf0a8b68db529e67017ee7c51ba51a20d2815cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Dzi=C4=99giel?= Date: Thu, 27 Jun 2024 20:57:53 +0200 Subject: [PATCH] clapper: Add media item creation with cached file Allow creating item with a previously downloaded cache file. When set, file at location will be tried to be loaded first. If this fails, URI will be used as normal. --- src/lib/clapper/clapper-media-item.c | 48 ++++++++++++++++++++++++++-- src/lib/clapper/clapper-media-item.h | 3 ++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/lib/clapper/clapper-media-item.c b/src/lib/clapper/clapper-media-item.c index 16c149f60..7f5f87ea6 100644 --- a/src/lib/clapper/clapper-media-item.c +++ b/src/lib/clapper/clapper-media-item.c @@ -62,6 +62,7 @@ enum PROP_ID, PROP_URI, PROP_SUBURI, + PROP_CACHE_LOCATION, PROP_TITLE, PROP_CONTAINER_FORMAT, PROP_DURATION, @@ -143,6 +144,35 @@ clapper_media_item_new_from_file (GFile *file) return item; } +/** + * clapper_media_item_new_cached: + * @uri: a media URI + * @location: (type filename) (nullable): a path to downloaded file + * + * Same as [ctor@Clapper.MediaItem.new], but allows to provide + * a location of a cache file where particular media at @uri + * is supposed to be found. + * + * File at @location existence will be checked upon starting playback + * of created item. If cache file is not found, media item @uri will be + * used as fallback. In this case when [property@Clapper.Player:download-enabled] + * is set to %TRUE, item will be downloaded and cached again if possible. + * + * Returns: (transfer full): a new #ClapperMediaItem. + * + * Since: 0.8 + */ +ClapperMediaItem * +clapper_media_item_new_cached (const gchar *uri, const gchar *location) +{ + ClapperMediaItem *item = clapper_media_item_new (uri); + + if (G_LIKELY (item != NULL) && location) + clapper_media_item_set_cache_location (item, location); + + return item; +} + /** * clapper_media_item_get_id: * @item: a #ClapperMediaItem @@ -452,8 +482,8 @@ clapper_media_item_update_from_discoverer_info (ClapperMediaItem *self, GstDisco gst_object_unref (player); } -/* XXX: Must be set from player thread */ -inline void +/* XXX: Must be set from player thread or upon construction */ +void clapper_media_item_set_cache_location (ClapperMediaItem *self, const gchar *location) { g_free (self->cache_uri); @@ -560,6 +590,9 @@ clapper_media_item_set_property (GObject *object, guint prop_id, case PROP_SUBURI: clapper_media_item_set_suburi (self, g_value_get_string (value)); break; + case PROP_CACHE_LOCATION: + clapper_media_item_set_cache_location (self, g_value_get_string (value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -640,6 +673,17 @@ clapper_media_item_class_init (ClapperMediaItemClass *klass) NULL, NULL, NULL, G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); + /** + * ClapperMediaItem:cache-location: + * + * Media downloaded cache file location. + * + * Since: 0.8 + */ + param_specs[PROP_CACHE_LOCATION] = g_param_spec_string ("cache-location", + NULL, NULL, NULL, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); + /** * ClapperMediaItem:title: * diff --git a/src/lib/clapper/clapper-media-item.h b/src/lib/clapper/clapper-media-item.h index 631d2f2c2..12744df0e 100644 --- a/src/lib/clapper/clapper-media-item.h +++ b/src/lib/clapper/clapper-media-item.h @@ -45,6 +45,9 @@ ClapperMediaItem * clapper_media_item_new (const gchar *uri); CLAPPER_API ClapperMediaItem * clapper_media_item_new_from_file (GFile *file); +CLAPPER_API +ClapperMediaItem * clapper_media_item_new_cached (const gchar *uri, const gchar *location); + CLAPPER_API guint clapper_media_item_get_id (ClapperMediaItem *item);