Asterisk - The Open Source Telephony Project  21.4.1
Macros | Functions | Variables
format_cache.c File Reference

Media Format Cache API. More...

#include "asterisk.h"
#include "asterisk/logger.h"
#include "asterisk/format.h"
#include "asterisk/format_cache.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h"

Go to the source code of this file.

Macros

#define CACHE_BUCKETS   53
 Number of buckets to use for the media format cache (should be prime for performance reasons)
 

Functions

struct ast_format__ast_format_cache_get (const char *name, const char *tag, const char *file, int line, const char *func)
 
struct ast_formatast_format_cache_get_by_codec (const struct ast_codec *codec)
 Retrieve a format from the cache by its codec. More...
 
struct ast_formatast_format_cache_get_slin_by_rate (unsigned int rate)
 Retrieve the best signed linear format given a sample rate. More...
 
int ast_format_cache_init (void)
 Initialize format cache support within the core. More...
 
int ast_format_cache_is_slinear (struct ast_format *format)
 Determines if a format is one of the cached slin formats. More...
 
int ast_format_cache_set (struct ast_format *format)
 Set a named format cache entry. More...
 
static void format_cache_shutdown (void)
 Function called when the process is shutting down.
 
static int format_cmp_cb (void *obj, void *arg, int flags)
 
static int format_hash_cb (const void *obj, int flags)
 
static void set_cached_format (const char *name, struct ast_format *format)
 

Variables

struct ast_formatast_format_adpcm
 Built-in cached adpcm format.
 
struct ast_formatast_format_alaw
 Built-in cached alaw format.
 
struct ast_formatast_format_codec2
 Built-in cached codec2 format. More...
 
struct ast_formatast_format_g719
 Built-in cached g719 format.
 
struct ast_formatast_format_g722
 Built-in cached g722 format.
 
struct ast_formatast_format_g723
 Built-in cached g723.1 format.
 
struct ast_formatast_format_g726
 Built-in cached g726 format.
 
struct ast_formatast_format_g726_aal2
 Built-in cached g726-aal2 format. More...
 
struct ast_formatast_format_g729
 Built-in cached g729 format.
 
struct ast_formatast_format_gsm
 Built-in cached gsm format.
 
struct ast_formatast_format_h261
 Built-in cached h261 format.
 
struct ast_formatast_format_h263
 Built-in cached h263 format.
 
struct ast_formatast_format_h263p
 Built-in cached h263 plus format.
 
struct ast_formatast_format_h264
 Built-in cached h264 format.
 
struct ast_formatast_format_h265
 Built-in cached h265 format.
 
struct ast_formatast_format_ilbc
 Built-in cached ilbc format.
 
struct ast_formatast_format_jpeg
 Built-in cached jpeg format.
 
struct ast_formatast_format_lpc10
 Built-in cached ilbc format.
 
struct ast_formatast_format_mp4
 Built-in cached mp4 format.
 
struct ast_formatast_format_none
 Built-in "null" format.
 
struct ast_formatast_format_opus
 Built-in cached opus format.
 
struct ast_formatast_format_png
 Built-in cached png format.
 
struct ast_formatast_format_silk12
 
struct ast_formatast_format_silk16
 
struct ast_formatast_format_silk24
 
struct ast_formatast_format_silk8
 Built-in "silk" format. More...
 
struct ast_formatast_format_siren14
 Built-in cached siren14 format.
 
struct ast_formatast_format_siren7
 Built-in cached siren7 format.
 
struct ast_formatast_format_slin
 Built-in cached signed linear 8kHz format.
 
struct ast_formatast_format_slin12
 Built-in cached signed linear 12kHz format.
 
struct ast_formatast_format_slin16
 Built-in cached signed linear 16kHz format.
 
struct ast_formatast_format_slin192
 Built-in cached signed linear 192kHz format.
 
struct ast_formatast_format_slin24
 Built-in cached signed linear 24kHz format.
 
struct ast_formatast_format_slin32
 Built-in cached signed linear 32kHz format.
 
struct ast_formatast_format_slin44
 Built-in cached signed linear 44kHz format.
 
struct ast_formatast_format_slin48
 Built-in cached signed linear 48kHz format.
 
struct ast_formatast_format_slin96
 Built-in cached signed linear 96kHz format.
 
struct ast_formatast_format_speex
 Built-in cached speex format.
 
struct ast_formatast_format_speex16
 Built-in cached speex at 16kHz format.
 
struct ast_formatast_format_speex32
 Built-in cached speex at 32kHz format.
 
struct ast_formatast_format_t140
 Built-in cached t140 format.
 
struct ast_formatast_format_t140_red
 Built-in cached t140 red format.
 
struct ast_formatast_format_t38
 Built-in cached T.38 format.
 
struct ast_formatast_format_ulaw
 Built-in cached ulaw format.
 
struct ast_formatast_format_vp8
 Built-in cached vp8 format.
 
struct ast_formatast_format_vp9
 Built-in cached vp9 format.
 
static struct ao2_containerformats
 Cached formats.
 

Detailed Description

Media Format Cache API.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file format_cache.c.

Function Documentation

struct ast_format* ast_format_cache_get_by_codec ( const struct ast_codec codec)

Retrieve a format from the cache by its codec.

Parameters
codecThe codec to search by
Return values
non-NULLif found
NULLif not found
Note
The returned format has its reference count incremented. It must be dropped using ao2_ref or ao2_cleanup.

Definition at line 551 of file format_cache.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_ref, and ast_format_get_codec().

Referenced by ast_format_cap_append_by_type().

552 {
553  struct ast_format *format;
554  struct ao2_iterator it;
555 
556  for (it = ao2_iterator_init(formats, 0);
557  (format = ao2_iterator_next(&it));
558  ao2_ref(format, -1)) {
559  struct ast_codec *candidate = ast_format_get_codec(format);
560  if (codec == candidate) {
561  ao2_cleanup(candidate);
563  return format;
564  }
565  ao2_cleanup(candidate);
566  }
567 
569  return NULL;
570 }
struct ast_codec * ast_format_get_codec(const struct ast_format *format)
Get the codec associated with a format.
Definition: format.c:324
Definition of a media format.
Definition: format.c:43
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Definition: file.c:69
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
Represents a media codec within Asterisk.
Definition: codec.h:42
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
struct ast_format* ast_format_cache_get_slin_by_rate ( unsigned int  rate)

Retrieve the best signed linear format given a sample rate.

Parameters
rateThe sample rate

This is a convenience function that returns one of the global ast_format_slinxxx formats.

Returns
pointer to the signed linear format
Note
The returned format has NOT had its reference count incremented.

Definition at line 512 of file format_cache.c.

References ast_format_slin, ast_format_slin12, ast_format_slin16, ast_format_slin192, ast_format_slin24, ast_format_slin32, ast_format_slin44, ast_format_slin48, and ast_format_slin96.

Referenced by ast_channel_make_compatible_helper(), snoop_determine_format(), softmix_bridge_unsuspend(), and softmix_mixing_loop().

513 {
514  if (rate >= 192000) {
515  return ast_format_slin192;
516  } else if (rate >= 96000) {
517  return ast_format_slin96;
518  } else if (rate >= 48000) {
519  return ast_format_slin48;
520  } else if (rate >= 44100) {
521  return ast_format_slin44;
522  } else if (rate >= 32000) {
523  return ast_format_slin32;
524  } else if (rate >= 24000) {
525  return ast_format_slin24;
526  } else if (rate >= 16000) {
527  return ast_format_slin16;
528  } else if (rate >= 12000) {
529  return ast_format_slin12;
530  }
531  return ast_format_slin;
532 }
struct ast_format * ast_format_slin192
Built-in cached signed linear 192kHz format.
Definition: format_cache.c:81
struct ast_format * ast_format_slin96
Built-in cached signed linear 96kHz format.
Definition: format_cache.c:76
struct ast_format * ast_format_slin32
Built-in cached signed linear 32kHz format.
Definition: format_cache.c:61
struct ast_format * ast_format_slin24
Built-in cached signed linear 24kHz format.
Definition: format_cache.c:56
struct ast_format * ast_format_slin48
Built-in cached signed linear 48kHz format.
Definition: format_cache.c:71
struct ast_format * ast_format_slin16
Built-in cached signed linear 16kHz format.
Definition: format_cache.c:51
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
struct ast_format * ast_format_slin44
Built-in cached signed linear 44kHz format.
Definition: format_cache.c:66
struct ast_format * ast_format_slin12
Built-in cached signed linear 12kHz format.
Definition: format_cache.c:46
int ast_format_cache_init ( void  )

Initialize format cache support within the core.

Return values
0success
-1failure

Definition at line 364 of file format_cache.c.

References AO2_ALLOC_OPT_LOCK_RWLOCK, ao2_container_alloc_hash, ast_register_cleanup(), CACHE_BUCKETS, and format_cache_shutdown().

365 {
367  format_hash_cb, NULL, format_cmp_cb);
368  if (!formats) {
369  return -1;
370  }
371 
373 
374  return 0;
375 }
static void format_cache_shutdown(void)
Function called when the process is shutting down.
Definition: format_cache.c:311
#define CACHE_BUCKETS
Number of buckets to use for the media format cache (should be prime for performance reasons) ...
Definition: format_cache.c:257
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#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
Definition: file.c:69
int ast_format_cache_is_slinear ( struct ast_format format)

Determines if a format is one of the cached slin formats.

Parameters
formatThe format to check
Return values
0if the format is not an SLIN format
1if the format is an SLIN format

Definition at line 534 of file format_cache.c.

References ast_format_cmp(), and AST_FORMAT_CMP_EQUAL.

Referenced by __ast_read(), ast_channel_make_compatible_helper(), ast_dsp_call_progress(), ast_dsp_process(), ast_frame_adjust_volume(), ast_frame_adjust_volume_float(), ast_slinfactory_init_with_format(), and ast_write_stream().

535 {
545  return 1;
546  }
547 
548  return 0;
549 }
struct ast_format * ast_format_slin192
Built-in cached signed linear 192kHz format.
Definition: format_cache.c:81
struct ast_format * ast_format_slin96
Built-in cached signed linear 96kHz format.
Definition: format_cache.c:76
struct ast_format * ast_format_slin32
Built-in cached signed linear 32kHz format.
Definition: format_cache.c:61
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
struct ast_format * ast_format_slin24
Built-in cached signed linear 24kHz format.
Definition: format_cache.c:56
struct ast_format * ast_format_slin48
Built-in cached signed linear 48kHz format.
Definition: format_cache.c:71
struct ast_format * ast_format_slin16
Built-in cached signed linear 16kHz format.
Definition: format_cache.c:51
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
struct ast_format * ast_format_slin44
Built-in cached signed linear 44kHz format.
Definition: format_cache.c:66
struct ast_format * ast_format_slin12
Built-in cached signed linear 12kHz format.
Definition: format_cache.c:46
int ast_format_cache_set ( struct ast_format format)

Set a named format cache entry.

Parameters
formatA pointer to the format to cache
Return values
0success
-1failure

Definition at line 474 of file format_cache.c.

References ao2_link_flags, ao2_unlink_flags, ast_format_get_name(), lock, OBJ_NOLOCK, OBJ_SEARCH_KEY, and SCOPED_AO2WRLOCK.

475 {
477  struct ast_format *old_format;
478 
479  ast_assert(format != NULL);
480 
481  if (ast_strlen_zero(ast_format_get_name(format))) {
482  return -1;
483  }
484 
485  old_format = ao2_find(formats, ast_format_get_name(format), OBJ_SEARCH_KEY | OBJ_NOLOCK);
486  if (old_format) {
487  ao2_unlink_flags(formats, old_format, OBJ_NOLOCK);
488  }
490 
491  set_cached_format(ast_format_get_name(format), format);
492 
493  ast_verb(5, "%s cached format with name '%s'\n",
494  old_format ? "Updated" : "Created",
495  ast_format_get_name(format));
496 
497  ao2_cleanup(old_format);
498 
499  return 0;
500 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
Definition of a media format.
Definition: format.c:43
#define ao2_link_flags(container, obj, flags)
Add an object to a container.
Definition: astobj2.h:1554
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define SCOPED_AO2WRLOCK(varname, obj)
scoped lock specialization for ao2 write locks.
Definition: lock.h:614
ast_mutex_t lock
Definition: file.c:69
#define ao2_unlink_flags(container, obj, flags)
Remove an object from a container.
Definition: astobj2.h:1600

Variable Documentation

struct ast_format* ast_format_codec2

Built-in cached codec2 format.

Built-in cached Codec 2 format.

Definition at line 226 of file format_cache.c.

struct ast_format* ast_format_g726_aal2
struct ast_format* ast_format_silk8

Built-in "silk" format.

Built-in SILK format.

Definition at line 251 of file format_cache.c.