From b30d53d8ce5c8b0c97eae69de49a57fa1703085d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Dzi=C4=99giel?= Date: Sun, 18 May 2025 14:21:19 +0200 Subject: [PATCH] clapper: Add ability to set harvest expiration date --- src/lib/clapper/clapper-harvest.c | 61 +++++++++++++++++++++++++++++++ src/lib/clapper/clapper-harvest.h | 6 +++ 2 files changed, 67 insertions(+) diff --git a/src/lib/clapper/clapper-harvest.c b/src/lib/clapper/clapper-harvest.c index 4c0d603a..0dc70e81 100644 --- a/src/lib/clapper/clapper-harvest.c +++ b/src/lib/clapper/clapper-harvest.c @@ -50,6 +50,8 @@ struct _ClapperHarvest guint16 n_chapters; guint16 n_tracks; + + gint64 exp_epoch; }; #define parent_class clapper_harvest_parent_class @@ -413,6 +415,65 @@ clapper_harvest_headers_set_value (ClapperHarvest *self, const gchar *key, const gst_structure_set_value (self->headers, key, value); } +/** + * clapper_harvest_set_expiration_date_utc: + * @harvest: a #ClapperHarvest + * @date_utc: a #GDateTime in UTC time + * + * Set date in UTC time until harvested content is expected + * to stay alive. + * + * This is used for harvest caching, so next time user requests to + * play the same URI, recently harvested data can be reused without + * the need to run [vfunc@Clapper.Extractable.extract] again. + * + * Since: 0.10 + */ +void +clapper_harvest_set_expiration_date_utc (ClapperHarvest *self, GDateTime *date_utc) +{ + g_return_if_fail (CLAPPER_IS_HARVEST (self)); + g_return_if_fail (date_utc != NULL); + + self->exp_epoch = g_date_time_to_unix (date_utc); + GST_DEBUG_OBJECT (self, "Expiration epoch: %" G_GINT64_FORMAT, self->exp_epoch); +} + +/** + * clapper_harvest_set_expiration_seconds: + * @harvest: a #ClapperHarvest + * @seconds: time in seconds until expiration + * + * Set amount of seconds for how long harvested content is + * expected to stay alive. + * + * Alternative function to [method@Clapper.Harvest.set_expiration_date_utc], + * but takes time as number in seconds from now. + * + * It is safe to pass zero or negative number to this function in + * case when calculating time manually and it already expired. + * + * Since: 0.10 + */ +void +clapper_harvest_set_expiration_seconds (ClapperHarvest *self, gdouble seconds) +{ + GDateTime *date, *date_epoch; + + g_return_if_fail (CLAPPER_IS_HARVEST (self)); + + GST_DEBUG_OBJECT (self, "Set expiration seconds: %.3lfs", seconds); + + date = g_date_time_new_now_utc (); + date_epoch = g_date_time_add_seconds (date, seconds); + g_date_time_unref (date); + + self->exp_epoch = g_date_time_to_unix (date_epoch); + g_date_time_unref (date_epoch); + + GST_DEBUG_OBJECT (self, "Expiration epoch: %" G_GINT64_FORMAT, self->exp_epoch); +} + static void clapper_harvest_init (ClapperHarvest *self) { diff --git a/src/lib/clapper/clapper-harvest.h b/src/lib/clapper/clapper-harvest.h index 54cd0ad3..aceb2dce 100644 --- a/src/lib/clapper/clapper-harvest.h +++ b/src/lib/clapper/clapper-harvest.h @@ -61,4 +61,10 @@ void clapper_harvest_headers_set (ClapperHarvest *harvest, const gchar *key, ... CLAPPER_API void clapper_harvest_headers_set_value (ClapperHarvest *harvest, const gchar *key, const GValue *value); +CLAPPER_API +void clapper_harvest_set_expiration_date_utc (ClapperHarvest *harvest, GDateTime *date_utc); + +CLAPPER_API +void clapper_harvest_set_expiration_seconds (ClapperHarvest *harvest, gdouble seconds); + G_END_DECLS