From 9e1622976af90a729da903f0c07ff5bc187063a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Dzi=C4=99giel?= Date: Sun, 23 Nov 2025 12:44:40 +0100 Subject: [PATCH] clapper: Make media item "cache-location" a readable property --- src/lib/clapper/clapper-enums.h | 2 + src/lib/clapper/clapper-media-item.c | 80 +++++++++++++++++++++++---- src/lib/clapper/clapper-media-item.h | 3 + src/lib/clapper/clapper-playbin-bus.c | 2 + src/lib/clapper/clapper-player.c | 3 + 5 files changed, 79 insertions(+), 11 deletions(-) diff --git a/src/lib/clapper/clapper-enums.h b/src/lib/clapper/clapper-enums.h index 58ea281f..16236db6 100644 --- a/src/lib/clapper/clapper-enums.h +++ b/src/lib/clapper/clapper-enums.h @@ -173,6 +173,7 @@ typedef enum * @CLAPPER_REACTABLE_ITEM_UPDATED_DURATION: Media item duration was updated. * @CLAPPER_REACTABLE_ITEM_UPDATED_TIMELINE: Media item timeline was updated. * @CLAPPER_REACTABLE_ITEM_UPDATED_TAGS: Media item tags were updated. + * @CLAPPER_REACTABLE_ITEM_UPDATED_CACHE_LOCATION: Media item cache location was updated. * * Flags informing which properties were updated within [class@Clapper.MediaItem]. * @@ -184,6 +185,7 @@ typedef enum CLAPPER_REACTABLE_ITEM_UPDATED_DURATION = 1 << 1, CLAPPER_REACTABLE_ITEM_UPDATED_TIMELINE = 1 << 2, CLAPPER_REACTABLE_ITEM_UPDATED_TAGS = 1 << 3, + CLAPPER_REACTABLE_ITEM_UPDATED_CACHE_LOCATION = 1 << 4, } ClapperReactableItemUpdatedFlags; G_END_DECLS diff --git a/src/lib/clapper/clapper-media-item.c b/src/lib/clapper/clapper-media-item.c index 1bd24c7d..c4fc41ea 100644 --- a/src/lib/clapper/clapper-media-item.c +++ b/src/lib/clapper/clapper-media-item.c @@ -278,6 +278,31 @@ clapper_media_item_get_suburi (ClapperMediaItem *self) return suburi; } +/** + * clapper_media_item_get_cache_location: + * @item: a #ClapperMediaItem + * + * Get downloaded cache file location of #ClapperMediaItem. + * + * Returns: (transfer full) (type filename) (nullable): a cache file location of #ClapperMediaItem. + * + * Since: 0.10 + */ +gchar * +clapper_media_item_get_cache_location (ClapperMediaItem *self) +{ + gchar *cache_location = NULL; + + g_return_val_if_fail (CLAPPER_IS_MEDIA_ITEM (self), NULL); + + GST_OBJECT_LOCK (self); + if (self->cache_uri) + cache_location = g_filename_from_uri (self->cache_uri, NULL, NULL); + GST_OBJECT_UNLOCK (self); + + return cache_location; +} + /** * clapper_media_item_get_title: * @item: a #ClapperMediaItem @@ -770,16 +795,42 @@ clapper_media_item_update_from_parsed_playlist (ClapperMediaItem *self, GListSto void clapper_media_item_set_cache_location (ClapperMediaItem *self, const gchar *location) { - if (!self->cache_uri && !location) - return; // No change (called during construction) + gboolean changed; - g_clear_pointer (&self->cache_uri, g_free); + GST_OBJECT_LOCK (self); - if (location) - self->cache_uri = g_filename_to_uri (location, NULL, NULL); + /* Skip if both are NULL (no change - called during construction) */ + if ((changed = (self->cache_uri || location))) { + g_clear_pointer (&self->cache_uri, g_free); - GST_DEBUG_OBJECT (self, "Set cache URI: \"%s\"", - GST_STR_NULL (self->cache_uri)); + if (location) + self->cache_uri = g_filename_to_uri (location, NULL, NULL); + + GST_DEBUG_OBJECT (self, "Set cache URI: \"%s\"", + GST_STR_NULL (self->cache_uri)); + } + + GST_OBJECT_UNLOCK (self); + + if (changed) { + ClapperPlayer *player; + + if ((player = clapper_player_get_from_ancestor (GST_OBJECT_CAST (self)))) { + ClapperFeaturesManager *features_manager; + + clapper_app_bus_post_prop_notify (player->app_bus, + GST_OBJECT_CAST (self), param_specs[PROP_CACHE_LOCATION]); + + if (player->reactables_manager) { + clapper_reactables_manager_trigger_item_updated (player->reactables_manager, self, + CLAPPER_REACTABLE_ITEM_UPDATED_CACHE_LOCATION); + } + if ((features_manager = clapper_player_get_features_manager (player))) + clapper_features_manager_trigger_item_updated (features_manager, self); + + gst_object_unref (player); + } + } } /* XXX: Can only be read from player thread. @@ -798,9 +849,6 @@ clapper_media_item_get_playback_uri (ClapperMediaItem *self) if (exists) return self->cache_uri; - - /* Do not test file existence next time */ - clapper_media_item_set_cache_location (self, NULL); } if (self->redirect_uri) @@ -914,6 +962,9 @@ clapper_media_item_get_property (GObject *object, guint prop_id, case PROP_SUBURI: g_value_take_string (value, clapper_media_item_get_suburi (self)); break; + case PROP_CACHE_LOCATION: + g_value_take_string (value, clapper_media_item_get_cache_location (self)); + break; case PROP_TAGS: g_value_take_boxed (value, clapper_media_item_get_tags (self)); break; @@ -980,11 +1031,18 @@ clapper_media_item_class_init (ClapperMediaItemClass *klass) * * Media downloaded cache file location. * + * This can be either set for newly created media items or + * it will be updated after download is completed if + * [property@Clapper.Player:download-enabled] is set. + * + * NOTE: This property was added in 0.8 as write at construct only. + * It can also be read only since Clapper 0.10. + * * 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); + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); /** * ClapperMediaItem:tags: diff --git a/src/lib/clapper/clapper-media-item.h b/src/lib/clapper/clapper-media-item.h index 8c8733a7..f2daf488 100644 --- a/src/lib/clapper/clapper-media-item.h +++ b/src/lib/clapper/clapper-media-item.h @@ -60,6 +60,9 @@ void clapper_media_item_set_suburi (ClapperMediaItem *item, const gchar *suburi) CLAPPER_API gchar * clapper_media_item_get_suburi (ClapperMediaItem *item); +CLAPPER_API +gchar * clapper_media_item_get_cache_location (ClapperMediaItem *item); + CLAPPER_API gchar * clapper_media_item_get_title (ClapperMediaItem *item); diff --git a/src/lib/clapper/clapper-playbin-bus.c b/src/lib/clapper/clapper-playbin-bus.c index 9e5476e5..1ee4aa08 100644 --- a/src/lib/clapper/clapper-playbin-bus.c +++ b/src/lib/clapper/clapper-playbin-bus.c @@ -957,6 +957,8 @@ _handle_element_msg (GstMessage *msg, ClapperPlayer *player) location = gst_structure_get_string (structure, "location"); signal_id = g_signal_lookup ("download-complete", CLAPPER_TYPE_PLAYER); + /* Set cache location before "download-complete" signal emit, + * so it can also be read directly from item */ GST_INFO_OBJECT (player, "Download of %" GST_PTR_FORMAT " complete: %s", downloaded_item, location); clapper_media_item_set_cache_location (downloaded_item, location); diff --git a/src/lib/clapper/clapper-player.c b/src/lib/clapper/clapper-player.c index 89602e7d..7fbf8a21 100644 --- a/src/lib/clapper/clapper-player.c +++ b/src/lib/clapper/clapper-player.c @@ -3012,6 +3012,9 @@ clapper_player_class_init (ClapperPlayerClass *klass) * be only emitted when progressive download buffering is enabled by * setting [property@Clapper.Player:download-enabled] property to %TRUE. * + * Download cache file location can also be read directly from @item + * through its [property@Clapper.MediaItem:cache-location] property. + * * Since: 0.8 */ signals[SIGNAL_DOWNLOAD_COMPLETE] = g_signal_new ("download-complete",