From 0fd3ac73a83a2805217b5e931b967b301626a149 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 8 Jan 2025 08:50:11 -0400 Subject: [PATCH] MT#61822 supporting removing entries from FS cache Change-Id: Ie358778ce5d8d6872bc4964367cf0ec22296934f --- daemon/cli.c | 27 +++++++++++++++++++++++++++ daemon/media_player.c | 25 +++++++++++++++++++++++++ include/media_player.h | 2 ++ utils/rtpengine-ctl | 2 ++ 4 files changed, 56 insertions(+) diff --git a/daemon/cli.c b/daemon/cli.c index 2f0d489a9..a3c86f201 100644 --- a/daemon/cli.c +++ b/daemon/cli.c @@ -122,6 +122,8 @@ static void cli_incoming_media_evict_file(str *instr, struct cli_writer *cw, con static void cli_incoming_media_evict_files(str *instr, struct cli_writer *cw, const cli_handler_t *); static void cli_incoming_media_evict_db(str *instr, struct cli_writer *cw, const cli_handler_t *); static void cli_incoming_media_evict_dbs(str *instr, struct cli_writer *cw, const cli_handler_t *); +static void cli_incoming_media_evict_cache(str *instr, struct cli_writer *cw, const cli_handler_t *); +static void cli_incoming_media_evict_caches(str *instr, struct cli_writer *cw, const cli_handler_t *); #endif @@ -213,6 +215,8 @@ static const cli_handler_t cli_media_evict_handlers[] = { { "files", cli_incoming_media_evict_files, NULL }, { "db", cli_incoming_media_evict_db, NULL }, { "dbs", cli_incoming_media_evict_dbs, NULL }, + { "cache", cli_incoming_media_evict_cache, NULL }, + { "caches", cli_incoming_media_evict_caches, NULL }, { NULL, }, }; static const cli_handler_t cli_media_handlers[] = { @@ -1910,4 +1914,27 @@ static void cli_incoming_media_list_caches(str *instr, struct cli_writer *cw, co cw->cw_printf(cw, "%llu\n", (unsigned long long) GPOINTER_TO_UINT(id)); } } + +static void cli_incoming_media_evict_cache(str *instr, struct cli_writer *cw, const cli_handler_t *handler) { + if (instr->len == 0) { + cw->cw_printf(cw, "More parameters required.\n"); + return ; + } + + unsigned long long id = str_to_ui(instr, 0); + if (id == 0 || id == ULLONG_MAX) + cw->cw_printf(cw, "Invalid ID '" STR_FORMAT "'\n", STR_FMT(instr)); + else { + bool ok = media_player_evict_cache(id); + if (ok) + cw->cw_printf(cw, "Success\n"); + else + cw->cw_printf(cw, "Failed to evict '" STR_FORMAT "'\n", STR_FMT(instr)); + } +} + +static void cli_incoming_media_evict_caches(str *instr, struct cli_writer *cw, const cli_handler_t *handler) { + unsigned int num = media_player_evict_caches(); + cw->cw_printf(cw, "%u DB cache entries evicted\n", num); +} #endif diff --git a/daemon/media_player.c b/daemon/media_player.c index 8d7f0b400..ee162522f 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -2379,6 +2379,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(DIR, closedir) typedef union { GQueue *q; + unsigned int *u; } iterate_db_cache_arg __attribute__((__transparent_union__)); static void media_player_iterate_db_cache(void (*callback)(unsigned long long, iterate_db_cache_arg), @@ -2414,3 +2415,27 @@ GQueue media_player_list_caches(void) { media_player_iterate_db_cache(media_player_add_to_queue, &ret); return ret; } + +bool media_player_evict_cache(unsigned long long id) { +#ifdef WITH_TRANSCODING + g_autoptr(char) fn = media_player_make_cache_entry_name(id); + int ret = unlink(fn); + if (ret == 0) + return true; + if (errno == ENOENT) // ignore these + return false; + ilog(LOG_WARN, "Failed to unlink media cache file '%s': %s", fn, strerror(errno)); +#endif + return false; +} + +static void media_player_evict_caches_all(unsigned long long id, unsigned int *u) { + if (media_player_evict_cache(id)) + (*u)++; +} + +unsigned int media_player_evict_caches(void) { + unsigned int ret = 0; + media_player_iterate_db_cache(media_player_evict_caches_all, &ret); + return ret; +} diff --git a/include/media_player.h b/include/media_player.h index ab6a68ddb..a0990ae23 100644 --- a/include/media_player.h +++ b/include/media_player.h @@ -164,6 +164,8 @@ unsigned int media_player_evict_db_medias(void); str_q media_player_list_files(void); GQueue media_player_list_dbs(void); GQueue media_player_list_caches(void); +bool media_player_evict_cache(unsigned long long); +unsigned int media_player_evict_caches(void); struct send_timer *send_timer_new(struct packet_stream *); void send_timer_push(struct send_timer *, struct codec_packet *); diff --git a/utils/rtpengine-ctl b/utils/rtpengine-ctl index 92c48a088..2e665e168 100755 --- a/utils/rtpengine-ctl +++ b/utils/rtpengine-ctl @@ -178,6 +178,8 @@ sub showusage { print " files : remove all media files from memory\n"; print " db : remove one database media entry from memory\n"; print " dbs : remove all database media entries from memory\n"; + print " cache : remove one database media entry from file cache\n"; + print " caches : remove all database media entries from file cache\n"; print "\n"; print "\n"; print " Return Value:\n";