Skip to content

Commit

Permalink
clapper: Add media item creation with cached file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Rafostar committed Jun 27, 2024
1 parent af4ae2c commit bf0a8b6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/lib/clapper/clapper-media-item.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ enum
PROP_ID,
PROP_URI,
PROP_SUBURI,
PROP_CACHE_LOCATION,
PROP_TITLE,
PROP_CONTAINER_FORMAT,
PROP_DURATION,
Expand Down Expand Up @@ -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 [[email protected]], 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 [[email protected]: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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
*
Expand Down
3 changes: 3 additions & 0 deletions src/lib/clapper/clapper-media-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit bf0a8b6

Please sign in to comment.