Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Functions
media_index.c File Reference

Sound file format and description indexer. More...

#include "asterisk.h"
#include <dirent.h>
#include <sys/stat.h>
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/format.h"
#include "asterisk/format_cap.h"
#include "asterisk/media_index.h"
#include "asterisk/file.h"

Go to the source code of this file.

Data Structures

struct  ast_media_index
 
struct  media_info
 Structure to hold information about a media file. More...
 
struct  media_variant
 Structure to hold a list of the format variations for a media file for a specific variant. More...
 
struct  read_dirs_data
 

Macros

#define INDEX_BUCKETS   157
 The number of buckets to be used for storing media filename-keyed objects.
 
#define VARIANT_BUCKETS   7
 The number of buckets to be used for storing variant-keyed objects.
 

Functions

static int add_media_cb (void *obj, void *arg, int flags)
 Add the media_info's filename to the container of filenames requested.
 
static int add_variant_cb (void *obj, void *arg, int flags)
 Add the variant to the list of variants requested.
 
static struct media_variantalloc_variant (struct ast_media_index *index, const char *filename, const char *variant_str)
 create the appropriate media_variant and any necessary structures
 
const char * ast_media_get_description (struct ast_media_index *index, const char *filename, const char *variant_str)
 Get the description for a media file. More...
 
struct ast_format_capast_media_get_format_cap (struct ast_media_index *index, const char *filename, const char *variant_str)
 Get the ast_format_cap for a media file. More...
 
struct ao2_containerast_media_get_media (struct ast_media_index *index)
 Get the a container of all media available on the system. More...
 
struct ao2_containerast_media_get_variants (struct ast_media_index *index, const char *filename)
 Get the languages in which a media file is available. More...
 
struct ast_media_indexast_media_index_create (const char *base_dir)
 Creates a new media index. More...
 
int ast_media_index_update_for_file (struct ast_media_index *index, const char *variant, const char *filename)
 Update a media index for a specific sound file. More...
 
static struct media_variantfind_variant (struct ast_media_index *index, const char *filename, const char *variant)
 
static void media_index_dtor (void *obj)
 
static struct media_infomedia_info_alloc (const char *name)
 
static int media_info_cmp (void *obj, void *arg, int flags)
 
static void media_info_destroy (void *obj)
 
static int media_info_hash (const void *obj, const int flags)
 
static struct media_variantmedia_variant_alloc (const char *variant_str)
 
static int media_variant_cmp (void *obj, void *arg, int flags)
 
static void media_variant_destroy (void *obj)
 
static int media_variant_hash (const void *obj, const int flags)
 
static int process_description_file (struct ast_media_index *index, const char *subdir, const char *variant_str, const char *filename, const char *match_filename)
 Process a media description text file. More...
 
static int process_media_file (struct ast_media_index *index, const char *variant, const char *subdir, const char *filename_stripped, const char *ext)
 Process a media file into the index.
 
static int read_dirs_cb (const char *dir_name, const char *filename, void *obj)
 
static int update_file_format_info (struct ast_media_index *index, const char *filename, const char *variant_str, struct ast_format *file_format)
 Update an index with new format/variant information.
 

Detailed Description

Sound file format and description indexer.

Definition in file media_index.c.

Function Documentation

const char* ast_media_get_description ( struct ast_media_index index,
const char *  filename,
const char *  variant 
)

Get the description for a media file.

Parameters
indexMedia index in which to query information
filenameName of the file for which to get the description
variantMedia variant for which to get the description
Return values
NULLif not found
Returns
The description requested (must be copied to be kept)

Definition at line 230 of file media_index.c.

References RAII_VAR.

Referenced by create_sound_blob().

231 {
232  RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
233  if (ast_strlen_zero(filename) || ast_strlen_zero(variant_str)) {
234  return NULL;
235  }
236 
237  variant = find_variant(index, filename, variant_str);
238  if (!variant) {
239  return NULL;
240  }
241 
242  return variant->description;
243 }
Structure to hold a list of the format variations for a media file for a specific variant...
Definition: media_index.c:46
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
struct ast_format_cap* ast_media_get_format_cap ( struct ast_media_index index,
const char *  filename,
const char *  variant 
)

Get the ast_format_cap for a media file.

Parameters
indexMedia index in which to query information
filenameName of the file for which to get the description
variantMedia variant for which to get the description
Return values
NULLif not found
Returns
a copy of the format capabilities (must be destroyed with ast_format_cap_destroy)

Definition at line 245 of file media_index.c.

References ast_format_cap_alloc, ast_format_cap_append_from_cap(), AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap::formats, and RAII_VAR.

Referenced by add_format_information_cb().

246 {
247  struct ast_format_cap *dupcap;
248  RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
249  if (ast_strlen_zero(filename) || ast_strlen_zero(variant_str)) {
250  return NULL;
251  }
252 
253  variant = find_variant(index, filename, variant_str);
254  if (!variant) {
255  return NULL;
256  }
257 
259  if (dupcap) {
260  ast_format_cap_append_from_cap(dupcap, variant->formats, AST_MEDIA_TYPE_UNKNOWN);
261  }
262  return dupcap;
263 }
Structure to hold a list of the format variations for a media file for a specific variant...
Definition: media_index.c:46
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
struct ast_format_cap::@359 formats
Vector of formats, indexed using the codec identifier.
int ast_format_cap_append_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src, enum ast_media_type type)
Append the formats of provided type in src to dst.
Definition: format_cap.c:269
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
struct ao2_container* ast_media_get_media ( struct ast_media_index index)

Get the a container of all media available on the system.

Parameters
indexMedia index in which to query information
Return values
NULLon error
Returns
an ast_str_container filled with media file name strings

Definition at line 307 of file media_index.c.

References add_media_cb(), ao2_callback, ao2_ref, ast_str_container_alloc, ast_media_index::index, INDEX_BUCKETS, ast_media_index::media_list_cache, OBJ_NODATA, and RAII_VAR.

Referenced by ast_ari_sounds_list(), handle_cli_sound_show(), and handle_cli_sounds_show().

308 {
309  RAII_VAR(struct ao2_container *, media, NULL, ao2_cleanup);
310 
311  if (!index->media_list_cache) {
313  if (!media) {
314  return NULL;
315  }
316 
317  ao2_callback(index->index, OBJ_NODATA, add_media_cb, media);
318 
319  /* Ref to the cache */
320  ao2_ref(media, +1);
321  index->media_list_cache = media;
322  }
323 
324  /* Ref to the caller */
325  ao2_ref(index->media_list_cache, +1);
326  return index->media_list_cache;
327 }
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1693
#define ast_str_container_alloc(buckets)
Allocates a hash container for bare strings.
Definition: strings.h:1365
#define INDEX_BUCKETS
The number of buckets to be used for storing media filename-keyed objects.
Definition: media_index.c:43
struct ao2_container * media_list_cache
Definition: media_index.c:150
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ao2_container * index
Definition: media_index.c:149
static int add_media_cb(void *obj, void *arg, int flags)
Add the media_info's filename to the container of filenames requested.
Definition: media_index.c:299
Generic container type.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
struct ao2_container* ast_media_get_variants ( struct ast_media_index index,
const char *  filename 
)

Get the languages in which a media file is available.

Parameters
indexMedia index in which to query information
filenameName of the file for which to get available languages
Return values
NULLon error
Returns
an ast_str_container filled with language strings

Definition at line 274 of file media_index.c.

References add_variant_cb(), ao2_callback, ao2_ref, ast_str_container_alloc, ast_media_index::index, OBJ_KEY, OBJ_NODATA, RAII_VAR, VARIANT_BUCKETS, and media_info::variants.

Referenced by create_sound_blob(), and handle_cli_sound_show().

275 {
276  RAII_VAR(struct media_info *, info, NULL, ao2_cleanup);
277  RAII_VAR(struct ao2_container *, variants, NULL, ao2_cleanup);
278  if (!filename) {
279  return NULL;
280  }
281 
283  if (!variants) {
284  return NULL;
285  }
286 
287  info = ao2_find(index->index, filename, OBJ_KEY);
288  if (!info) {
289  return NULL;
290  }
291 
292  ao2_callback(info->variants, OBJ_NODATA, add_variant_cb, variants);
293 
294  ao2_ref(variants, +1);
295  return variants;
296 }
#define OBJ_KEY
Definition: astobj2.h:1151
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1693
#define ast_str_container_alloc(buckets)
Allocates a hash container for bare strings.
Definition: strings.h:1365
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Structure to hold information about a media file.
Definition: media_index.c:99
struct ao2_container * index
Definition: media_index.c:149
#define VARIANT_BUCKETS
The number of buckets to be used for storing variant-keyed objects.
Definition: media_index.c:40
Generic container type.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
static int add_variant_cb(void *obj, void *arg, int flags)
Add the variant to the list of variants requested.
Definition: media_index.c:266
struct ast_media_index* ast_media_index_create ( const char *  base_dir)

Creates a new media index.

Parameters
base_dirBase directory for indexing
Return values
NULLon error
Anew AO2 refcounted media index

Definition at line 162 of file media_index.c.

References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, ao2_ref, ast_media_index::base_dir, ast_media_index::index, and INDEX_BUCKETS.

Referenced by ast_sounds_get_index_for_file().

163 {
164  size_t base_dir_sz = strlen(base_dir) + 1;
165  struct ast_media_index *index = ao2_alloc(sizeof(*index) + base_dir_sz, media_index_dtor);
166 
167  if (!index) {
168  return NULL;
169  }
170 
171  memcpy(index->base_dir, base_dir, base_dir_sz);
172 
174  media_info_hash, NULL, media_info_cmp);
175  if (!index->index) {
176  ao2_ref(index, -1);
177 
178  return NULL;
179  }
180 
181  return index;
182 }
#define INDEX_BUCKETS
The number of buckets to be used for storing media filename-keyed objects.
Definition: media_index.c:43
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
struct ao2_container * index
Definition: media_index.c:149
char base_dir[0]
Definition: media_index.c:151
int ast_media_index_update_for_file ( struct ast_media_index index,
const char *  variant,
const char *  filename 
)

Update a media index for a specific sound file.

Since
13.25.0
16.2.0
Parameters
indexMedia index in which to query information
variantMedia variant for which to get the description
filenameSound file name without extension
Note
If filename is NULL, this function will act as ast_media_index_update and add all sound files to the index.
Return values
non-zeroon error
Returns
zero on success

Definition at line 587 of file media_index.c.

References ast_alloca, ast_debug, ast_file_read_dirs(), ast_tvdiff_us(), ast_media_index::base_dir, and S_OR.

Referenced by update_index_cb().

589 {
590  struct timeval start;
591  struct timeval end;
592  int64_t elapsed;
593  int rc;
594  size_t dirname_len = strlen(index->base_dir) + strlen(S_OR(variant, "")) + 1;
595  struct read_dirs_data data = {
596  .search_filename = S_OR(filename, ""),
597  .search_filename_len = strlen(S_OR(filename, "")),
598  .search_variant = S_OR(variant, ""),
599  .index = index,
600  .dirname_len = dirname_len,
601  };
602  char *search_dir = ast_alloca(dirname_len + 1);
603 
604  sprintf(search_dir, "%s%s%s", index->base_dir, ast_strlen_zero(variant) ? "" : "/",
605  data.search_variant);
606 
607  gettimeofday(&start, NULL);
608  rc = ast_file_read_dirs(search_dir, read_dirs_cb, &data, -1);
609  gettimeofday(&end, NULL);
610  elapsed = ast_tvdiff_us(end, start);
611  ast_debug(1, "Media for language '%s' indexed in %8.6f seconds\n", data.search_variant, elapsed / 1E6);
612 
613  return rc;
614 }
int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth)
Recursively iterate through files and directories up to max_depth.
Definition: file.c:1274
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:80
char base_dir[0]
Definition: media_index.c:151
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
Definition: time.h:87
static int process_description_file ( struct ast_media_index index,
const char *  subdir,
const char *  variant_str,
const char *  filename,
const char *  match_filename 
)
static

Process a media description text file.

This currently processes core-sounds-*.txt and extra-sounds-*.txt, but will process others if present.

Definition at line 380 of file media_index.c.

References alloc_variant(), ao2_ref, ast_skip_blanks(), ast_str_append(), ast_str_buffer(), ast_str_create, ast_str_reset(), ast_str_set(), ast_strdup, ast_string_field_set, ast_trim_blanks(), ast_media_index::base_dir, media_variant::description, RAII_VAR, and media_variant::variant.

385 {
386  RAII_VAR(struct ast_str *, description_file_path, ast_str_create(64), ast_free);
387  RAII_VAR(struct ast_str *, cumulative_description, ast_str_create(64), ast_free);
388  char *file_id_persist = NULL;
389  int res = 0;
390  FILE *f = NULL;
391 #if defined(LOW_MEMORY)
392  char buf[256];
393 #else
394  char buf[2048];
395 #endif
396 
397  if (!description_file_path || !cumulative_description) {
398  return -1;
399  }
400 
401  if (ast_strlen_zero(subdir)) {
402  ast_str_set(&description_file_path, 0, "%s/%s/%s", index->base_dir, variant_str, filename);
403  } else {
404  ast_str_set(&description_file_path, 0, "%s/%s/%s/%s", index->base_dir, variant_str, subdir, filename);
405  }
406  f = fopen(ast_str_buffer(description_file_path), "r");
407  if (!f) {
408  ast_log(LOG_WARNING, "Could not open media description file '%s': %s\n", ast_str_buffer(description_file_path), strerror(errno));
409  return -1;
410  }
411 
412  while (!feof(f)) {
413  char *file_identifier, *description;
414  if (!fgets(buf, sizeof(buf), f)) {
415  if (ferror(f)) {
416  ast_log(LOG_ERROR, "Error reading from file %s: %s\n", ast_str_buffer(description_file_path), strerror(errno));
417  }
418  continue;
419  }
420 
421  /* Skip lines that are too long */
422  if (strlen(buf) == sizeof(buf) - 1 && buf[sizeof(buf) - 1] != '\n') {
423  ast_log(LOG_WARNING, "Line too long, skipping. It begins with: %.32s...\n", buf);
424  while (fgets(buf, sizeof(buf), f)) {
425  if (strlen(buf) != sizeof(buf) - 1 || buf[sizeof(buf) - 1] == '\n') {
426  break;
427  }
428  }
429  if (ferror(f)) {
430  ast_log(LOG_ERROR, "Error reading from file %s: %s\n", ast_str_buffer(description_file_path), strerror(errno));
431  }
432  continue;
433  }
434 
435  if (buf[0] == ';') {
436  /* ignore comments */
437  continue;
438  }
439 
440  ast_trim_blanks(buf);
441  description = buf;
442  file_identifier = strsep(&description, ":");
443  if (!description) {
444  /* no ':' means this is a continuation */
445  if (file_id_persist) {
446  ast_str_append(&cumulative_description, 0, "\n%s", file_identifier);
447  }
448  continue;
449  } else {
450  /* if there's text in cumulative_description, archive it and start anew */
451  if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
452  struct media_variant *variant;
453 
454  /*
455  * If we were only searching for a specific sound filename
456  * don't include others.
457  */
458  if (ast_strlen_zero(match_filename) || strcmp(match_filename, file_id_persist) == 0) {
459  variant = alloc_variant(index, file_id_persist, variant_str);
460  if (!variant) {
461  res = -1;
462  break;
463  }
464  ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
465  ao2_ref(variant, -1);
466  }
467 
468  ast_str_reset(cumulative_description);
469  }
470 
471  ast_free(file_id_persist);
472  file_id_persist = ast_strdup(file_identifier);
473  description = ast_skip_blanks(description);
474  ast_str_set(&cumulative_description, 0, "%s", description);
475  }
476  }
477 
478  /* handle the last one */
479  if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
480  struct media_variant *variant;
481 
482  /*
483  * If we were only searching for a specific sound filename
484  * don't include others.
485  */
486  if (ast_strlen_zero(match_filename) || strcmp(match_filename, file_id_persist) == 0) {
487  variant = alloc_variant(index, file_id_persist, variant_str);
488  if (variant) {
489  ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
490  ao2_ref(variant, -1);
491  } else {
492  res = -1;
493  }
494  }
495  }
496 
497  ast_free(file_id_persist);
498  fclose(f);
499  return res;
500 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
Structure to hold a list of the format variations for a media file for a specific variant...
Definition: media_index.c:46
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Support for dynamic strings.
Definition: strings.h:623
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:161
char * ast_trim_blanks(char *str)
Trims trailing whitespace characters from a string.
Definition: strings.h:186
static struct media_variant * alloc_variant(struct ast_media_index *index, const char *filename, const char *variant_str)
create the appropriate media_variant and any necessary structures
Definition: media_index.c:197
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:693
char base_dir[0]
Definition: media_index.c:151
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
char variant[0]
Definition: media_index.c:51
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521