Asterisk - The Open Source Telephony Project
18.5.0
|
An in-memory media cache. More...
#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/config.h"
#include "asterisk/bucket.h"
#include "asterisk/astdb.h"
#include "asterisk/cli.h"
#include "asterisk/file.h"
#include "asterisk/media_cache.h"
Go to the source code of this file.
Macros | |
#define | AO2_BUCKETS 61 |
#define | AST_DB_FAMILY "MediaCache" |
#define | AST_DB_FAMILY_LEN 12 |
#define | FORMAT_ROW "%-40s\n\t%-40s\n" |
#define | FORMAT_ROW "\t%20s: %-40.40s\n" |
Functions | |
int | ast_media_cache_create_or_update (const char *uri, const char *file_path, struct ast_variable *metadata) |
Create/update a cached media item. More... | |
int | ast_media_cache_delete (const char *uri) |
Remove an item from the media cache. More... | |
int | ast_media_cache_exists (const char *uri) |
Check if an item exists in the cache. More... | |
int | ast_media_cache_init (void) |
Initialize the media cache. More... | |
int | ast_media_cache_retrieve (const char *uri, const char *preferred_file_name, char *file_path, size_t len) |
Retrieve an item from the cache. More... | |
int | ast_media_cache_retrieve_metadata (const char *uri, const char *key, char *value, size_t len) |
Retrieve metadata from an item in the cache. More... | |
static void | bucket_file_update_path (struct ast_bucket_file *bucket_file, const char *preferred_file_name) |
static char * | cli_complete_uri (const char *word) |
static char * | media_cache_handle_create_item (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | media_cache_handle_delete_item (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | media_cache_handle_refresh_item (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | media_cache_handle_show_all (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static char * | media_cache_handle_show_item (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static void | media_cache_item_del_from_astdb (struct ast_bucket_file *bucket_file) |
static int | media_cache_item_populate_from_astdb (const char *uri, const char *hash) |
static void | media_cache_item_sync_to_astdb (struct ast_bucket_file *bucket_file) |
static void | media_cache_populate_from_astdb (void) |
static int | media_cache_prnt_summary (void *obj, void *arg, int flags) |
static void | media_cache_remove_from_astdb (const char *uri, const char *hash) |
static void | media_cache_shutdown (void) |
static int | metadata_sync_to_astdb (void *obj, void *arg, int flags) |
static void | normalize_content_type_header (char *content_type) |
Variables | |
static struct ast_cli_entry | cli_media_cache [] |
static struct ao2_container * | media_cache |
An in-memory media cache.
Definition in file media_cache.c.
#define AO2_BUCKETS 61 |
Number of buckets in the ao2 container holding our media items
Definition at line 48 of file media_cache.c.
Referenced by ast_media_cache_init().
#define AST_DB_FAMILY "MediaCache" |
The name of the AstDB family holding items in the cache.
Definition at line 42 of file media_cache.c.
Referenced by media_cache_item_del_from_astdb(), media_cache_item_sync_to_astdb(), media_cache_populate_from_astdb(), and media_cache_remove_from_astdb().
#define AST_DB_FAMILY_LEN 12 |
Length of 'MediaCache' + 2 '/' characters
Definition at line 45 of file media_cache.c.
Referenced by media_cache_populate_from_astdb(), and media_cache_remove_from_astdb().
#define FORMAT_ROW "%-40s\n\t%-40s\n" |
Referenced by media_cache_handle_show_item(), and media_cache_prnt_summary().
#define FORMAT_ROW "\t%20s: %-40.40s\n" |
int ast_media_cache_create_or_update | ( | const char * | uri, |
const char * | file_path, | ||
struct ast_variable * | metadata | ||
) |
Create/update a cached media item.
uri | The unique URI for the media item to store in the cache |
file_path | Full path to the media file to be cached |
metadata | Metadata to store with the cached item |
0 | The item was cached |
-1 | An error occurred when creating/updating the item |
Example Usage:
While ast_media_cache_retrieve is used to retrieve media from some AstBucket Bucket File API provider, this method allows for overwriting what is provided by a backend with some local media. This is useful for reconstructing or otherwise associating local media with a remote URI, deferring updating of the media from the backend to some later retrieval.
Definition at line 285 of file media_cache.c.
References ao2_find, ao2_link_flags, ao2_ref, ao2_unlink_flags, ast_bucket_file_alloc(), ast_bucket_file_clone(), ast_bucket_file_create(), ast_bucket_file_metadata_set(), ast_log, ast_strdupa, ast_strlen_zero, ast_bucket_file::created, ext, LOG_WARNING, media_cache_item_sync_to_astdb(), ast_bucket_file::modified, ast_variable::name, ast_variable::next, OBJ_NOLOCK, OBJ_SEARCH_KEY, ast_bucket_file::path, SCOPED_AO2LOCK, tmp(), and ast_variable::value.
Referenced by AST_TEST_DEFINE(), and media_cache_handle_create_item().
int ast_media_cache_delete | ( | const char * | uri | ) |
Remove an item from the media cache.
uri | The unique URI for the media item to store in the cache |
0 | success |
-1 | error |
Example Usage:
This removes an item completely from the media cache. Any files local on disk associated with the item are deleted as well.
Definition at line 364 of file media_cache.c.
References ao2_find, ao2_ref, ast_bucket_file_delete(), ast_strlen_zero, media_cache_item_del_from_astdb(), OBJ_SEARCH_KEY, and OBJ_UNLINK.
Referenced by media_cache_handle_delete_item().
int ast_media_cache_exists | ( | const char * | uri | ) |
Check if an item exists in the cache.
uri | The unique URI for the media item |
0 | uri does not exist in cache |
1 | uri does exist in cache |
Definition at line 53 of file media_cache.c.
References ao2_find, ao2_ref, ast_bucket_file_retrieve(), ast_strlen_zero, and OBJ_SEARCH_KEY.
Referenced by AST_TEST_DEFINE().
int ast_media_cache_init | ( | void | ) |
Initialize the media cache.
0 | success |
-1 | error |
Definition at line 688 of file media_cache.c.
References AO2_ALLOC_OPT_LOCK_MUTEX, AO2_BUCKETS, ao2_container_alloc_hash, ARRAY_LEN, ast_cli_register_multiple, ast_register_cleanup(), ast_sorcery_object_id_compare(), ast_sorcery_object_id_hash(), media_cache_populate_from_astdb(), media_cache_shutdown(), and NULL.
Referenced by asterisk_daemon().
int ast_media_cache_retrieve | ( | const char * | uri, |
const char * | preferred_file_name, | ||
char * | file_path, | ||
size_t | len | ||
) |
Retrieve an item from the cache.
uri | The unique URI for the media item |
preferred_file_name | The preferred name for the file storing the media once it is retrieved. Can be NULL. |
file_path | Buffer to store the full path to the media in the cache |
len | The length of the buffer pointed to by file_path |
0 | The item was retrieved successfully |
-1 | The item could not be retrieved |
Example Usage:
Retrieving an item will cause the AstBucket Bucket File API Bucket backend associated with the URI scheme in uri
to be queried. If the Bucket backend does not require an update, the cached information is used to find the file associated with uri
, and file_path
is populated with the location of the media file associated with uri
.
If the item is not in the cache, the item will be retrieved using the AstBucket Bucket File API backend. When this occurs, if preferred_file_name
is given, it will be used as the destination file for the retrieval. When retrieval of the media from the backend is complete, file_path
is then populated as before.
Definition at line 197 of file media_cache.c.
References ao2_find, ao2_link_flags, ao2_ref, ao2_unlink_flags, ast_bucket_file_delete(), ast_bucket_file_is_stale(), ast_bucket_file_retrieve(), ast_copy_string(), ast_debug, ast_file_is_readable(), ast_strlen_zero, bucket_file_update_path(), ext, media_cache_item_sync_to_astdb(), OBJ_NOLOCK, OBJ_SEARCH_KEY, ast_bucket_file::path, and SCOPED_AO2LOCK.
Referenced by AST_TEST_DEFINE(), fileexists_test(), and media_cache_handle_refresh_item().
int ast_media_cache_retrieve_metadata | ( | const char * | uri, |
const char * | key, | ||
char * | value, | ||
size_t | len | ||
) |
Retrieve metadata from an item in the cache.
uri | The unique URI for the media item |
key | The key of the metadata to retrieve |
value | Buffer to store the value in |
len | The length of the buffer pointed to by value |
0 | The metadata was retrieved successfully |
-1 | The metadata could not be retrieved |
Example Usage:
Definition at line 258 of file media_cache.c.
References ao2_find, ao2_ref, ast_copy_string(), ast_strlen_zero, ast_bucket_file::metadata, OBJ_SEARCH_KEY, and ast_bucket_metadata::value.
Referenced by AST_TEST_DEFINE().
|
static |
Definition at line 150 of file media_cache.c.
References ao2_ref, ast_bucket_file_metadata_get(), ast_bucket_file_metadata_set(), ast_copy_string(), ast_free, ast_get_extension_for_mime_type(), ast_get_format_for_file_ext(), ast_sorcery_object_get_id(), ast_strdup, ast_strlen_zero, ext, normalize_content_type_header(), ast_bucket_file::path, PATH_MAX, and ast_bucket_metadata::value.
Referenced by ast_media_cache_retrieve().
|
static |
Definition at line 518 of file media_cache.c.
References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_cli_completion_add(), ast_sorcery_object_get_id(), ast_strdup, and NULL.
Referenced by media_cache_handle_delete_item(), media_cache_handle_refresh_item(), and media_cache_handle_show_item().
|
static |
Definition at line 639 of file media_cache.c.
References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_media_cache_create_or_update(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, NULL, and ast_cli_entry::usage.
|
static |
Definition at line 580 of file media_cache.c.
References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_media_cache_delete(), cli_complete_uri(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, NULL, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.
|
static |
Definition at line 609 of file media_cache.c.
References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_media_cache_retrieve(), cli_complete_uri(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, NULL, PATH_MAX, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.
|
static |
Definition at line 490 of file media_cache.c.
References ao2_callback, ast_cli_args::argc, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, media_cache_prnt_summary(), NULL, OBJ_MULTIPLE, OBJ_NODATA, and ast_cli_entry::usage.
|
static |
Definition at line 536 of file media_cache.c.
References ao2_find, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_sorcery_object_get_id(), cli_complete_uri(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, FORMAT_ROW, ast_bucket_file::metadata, ast_bucket_metadata::name, NULL, OBJ_SEARCH_KEY, ast_bucket_file::path, ast_cli_args::pos, ast_cli_entry::usage, ast_bucket_metadata::value, and ast_cli_args::word.
|
static |
Definition at line 114 of file media_cache.c.
References ast_db_del(), ast_db_deltree(), AST_DB_FAMILY, ast_db_get_allocated(), ast_free, ast_sorcery_object_get_id(), and NULL.
Referenced by ast_media_cache_delete().
|
static |
Definition at line 406 of file media_cache.c.
References ao2_link, ao2_ref, ast_bucket_file_alloc(), ast_bucket_file_metadata_set(), ast_db_freetree(), ast_db_gettree(), ast_log, ast_strlen_zero, ast_db_entry::data, ast_db_entry::key, LOG_WARNING, ast_db_entry::next, and NULL.
Referenced by media_cache_populate_from_astdb().
|
static |
Definition at line 96 of file media_cache.c.
References ast_bucket_file_metadata_callback(), AST_DB_FAMILY, ast_db_put(), ast_sha1_hash(), ast_sorcery_object_get_id(), metadata_sync_to_astdb(), and ast_bucket_file::path.
Referenced by ast_media_cache_create_or_update(), and ast_media_cache_retrieve().
|
static |
Definition at line 460 of file media_cache.c.
References AST_DB_FAMILY, AST_DB_FAMILY_LEN, ast_db_freetree(), ast_db_gettree(), ast_db_entry::data, ast_db_entry::key, media_cache_item_populate_from_astdb(), media_cache_remove_from_astdb(), ast_db_entry::next, and NULL.
Referenced by ast_media_cache_init().
|
static |
Definition at line 478 of file media_cache.c.
References a, ast_cli(), ast_sorcery_object_get_id(), CMP_MATCH, ast_cli_args::fd, FORMAT_ROW, and ast_bucket_file::path.
Referenced by media_cache_handle_show_all().
|
static |
Definition at line 392 of file media_cache.c.
References ast_db_del(), ast_db_deltree(), AST_DB_FAMILY, AST_DB_FAMILY_LEN, and NULL.
Referenced by media_cache_populate_from_astdb().
|
static |
Definition at line 680 of file media_cache.c.
References ao2_cleanup, ARRAY_LEN, ast_cli_unregister_multiple(), and NULL.
Referenced by ast_media_cache_init().
|
static |
Definition at line 81 of file media_cache.c.
References ast_db_put(), ast_bucket_metadata::name, and ast_bucket_metadata::value.
Referenced by media_cache_item_sync_to_astdb().
|
static |
Definition at line 133 of file media_cache.c.
Referenced by bucket_file_update_path().
|
static |
Definition at line 668 of file media_cache.c.
|
static |
Our one and only container holding media items
Definition at line 51 of file media_cache.c.