Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
codec.c File Reference

Codecs API. More...

#include "asterisk.h"
#include "asterisk/logger.h"
#include "asterisk/codec.h"
#include "asterisk/format.h"
#include "asterisk/frame.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h"
#include "asterisk/module.h"
#include "asterisk/cli.h"
Include dependency graph for codec.c:

Go to the source code of this file.

Data Structures

struct  internal_ast_codec
 

Macros

#define CODEC_BUCKETS   53
 Number of buckets to use for codecs (should be prime for performance reasons) More...
 

Functions

int __ast_codec_register (struct ast_codec *codec, struct ast_module *mod)
 This function is used to register a codec with the Asterisk core. Registering allows it to be passed through in frames and configured in channel drivers. More...
 
int __ast_codec_register_with_format (struct ast_codec *codec, const char *format_name, struct ast_module *mod)
 
unsigned int ast_codec_determine_length (const struct ast_codec *codec, unsigned int samples)
 Get the length of media (in milliseconds) given a number of samples. More...
 
struct ast_codecast_codec_get (const char *name, enum ast_media_type type, unsigned int sample_rate)
 Retrieve a codec given a name, type, and sample rate. More...
 
struct ast_codecast_codec_get_by_id (int id)
 Retrieve a codec given the unique identifier. More...
 
int ast_codec_get_max (void)
 Retrieve the current maximum identifier for codec iteration. More...
 
int ast_codec_init (void)
 Initialize codec support within the core. More...
 
const char * ast_codec_media_type2str (enum ast_media_type type)
 Conversion function to take a media type and turn it into a string. More...
 
unsigned int ast_codec_samples_count (struct ast_frame *frame)
 Get the number of samples contained within a frame. More...
 
enum ast_media_type ast_media_type_from_str (const char *media_type_str)
 Conversion function to take a media string and convert it to a media type. More...
 
static int codec_cmp (void *obj, void *arg, int flags)
 
static void codec_dtor (void *obj)
 
static int codec_id_cmp (void *obj, void *arg, int flags)
 Callback function for getting a codec based on unique identifier. More...
 
static void codec_shutdown (void)
 Function called when the process is shutting down. More...
 
static char * show_codec (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * show_codecs (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 

Variables

static struct ast_cli_entry codec_cli []
 
static int codec_id = 1
 Current identifier value for newly registered codec. More...
 
static struct ao2_containercodecs
 Registered codecs. More...
 

Detailed Description

Codecs API.

Author
Joshua Colp jcolp[email protected]@dig[email protected]ium.c[email protected]om

Definition in file codec.c.

Macro Definition Documentation

◆ CODEC_BUCKETS

#define CODEC_BUCKETS   53

Number of buckets to use for codecs (should be prime for performance reasons)

Definition at line 42 of file codec.c.

Referenced by ast_codec_init().

Function Documentation

◆ __ast_codec_register()

int __ast_codec_register ( struct ast_codec codec,
struct ast_module mod 
)

This function is used to register a codec with the Asterisk core. Registering allows it to be passed through in frames and configured in channel drivers.

Parameters
codecto register
modthe module this codec is provided by
Return values
0success
-1failure

Definition at line 272 of file codec.c.

References __ast_codec_register_with_format(), and NULL.

273 {
274  return __ast_codec_register_with_format(codec, NULL, mod);
275 }
#define NULL
Definition: resample.c:96
int __ast_codec_register_with_format(struct ast_codec *codec, const char *format_name, struct ast_module *mod)
Definition: codec.c:277

◆ __ast_codec_register_with_format()

int __ast_codec_register_with_format ( struct ast_codec codec,
const char *  format_name,
struct ast_module mod 
)

Definition at line 277 of file codec.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_find, ao2_link_flags, ao2_ref, ao2_t_alloc_options, ast_codec_media_type2str(), ast_log, AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_UNKNOWN, ast_module_shutdown_ref, ast_verb, codec_dtor(), codec_id, ast_codec::description, internal_ast_codec::external, internal_ast_codec::format_name, ast_codec::id, lock, LOG_ERROR, ast_codec::name, OBJ_NOLOCK, OBJ_SEARCH_OBJECT, S_OR, ast_codec::sample_rate, SCOPED_AO2WRLOCK, and ast_codec::type.

Referenced by __ast_codec_register().

278 {
280  struct internal_ast_codec *codec_new;
281 
282  /* Some types have specific requirements */
283  if (codec->type == AST_MEDIA_TYPE_UNKNOWN) {
284  ast_log(LOG_ERROR, "A media type must be specified for codec '%s'\n", codec->name);
285  return -1;
286  } else if (codec->type == AST_MEDIA_TYPE_AUDIO) {
287  if (!codec->sample_rate) {
288  ast_log(LOG_ERROR, "A sample rate must be specified for codec '%s' of type '%s'\n",
289  codec->name, ast_codec_media_type2str(codec->type));
290  return -1;
291  }
292  }
293 
294  codec_new = ao2_find(codecs, codec, OBJ_SEARCH_OBJECT | OBJ_NOLOCK);
295  if (codec_new) {
296  ast_log(LOG_ERROR, "A codec with name '%s' of type '%s' and sample rate '%u' is already registered\n",
297  codec->name, ast_codec_media_type2str(codec->type), codec->sample_rate);
298  ao2_ref(codec_new, -1);
299  return -1;
300  }
301 
302  codec_new = ao2_t_alloc_options(sizeof(*codec_new), codec_dtor,
304  if (!codec_new) {
305  ast_log(LOG_ERROR, "Could not allocate a codec with name '%s' of type '%s' and sample rate '%u'\n",
306  codec->name, ast_codec_media_type2str(codec->type), codec->sample_rate);
307  return -1;
308  }
309  codec_new->external = *codec;
310  codec_new->format_name = format_name;
311  codec_new->external.id = codec_id++;
312 
313  ao2_link_flags(codecs, codec_new, OBJ_NOLOCK);
314 
315  /* Once registered a codec can not be unregistered, and the module must persist until shutdown */
317 
318  ast_verb(2, "Registered '%s' codec '%s' at sample rate '%u' with id '%u'\n",
319  ast_codec_media_type2str(codec->type), codec->name, codec->sample_rate, codec_new->external.id);
320 
321  ao2_ref(codec_new, -1);
322 
323  return 0;
324 }
const char * name
Name for this codec.
Definition: codec.h:46
const char * ast_codec_media_type2str(enum ast_media_type type)
Conversion function to take a media type and turn it into a string.
Definition: codec.c:347
unsigned int id
Internal unique identifier for this codec, set at registration time (starts at 1) ...
Definition: codec.h:44
struct ast_codec external
Public codec structure. Must remain first.
Definition: codec.c:61
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define ao2_link_flags(container, obj, flags)
Definition: astobj2.h:1572
#define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg)
Allocate and initialize an object.
Definition: astobj2.h:404
#define ast_verb(level,...)
Definition: logger.h:463
static void codec_dtor(void *obj)
Definition: codec.c:263
#define SCOPED_AO2WRLOCK(varname, obj)
scoped lock specialization for ao2 write locks.
Definition: lock.h:612
#define ast_log
Definition: astobj2.c:42
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
ast_mutex_t lock
Definition: app_meetme.c:1091
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define LOG_ERROR
Definition: logger.h:285
const char * description
Brief description.
Definition: codec.h:48
#define ast_module_shutdown_ref(mod)
Prevent unload of the module before shutdown.
Definition: module.h:464
static int codec_id
Current identifier value for newly registered codec.
Definition: codec.c:45
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
const char * format_name
A format name for a default sane format using this codec.
Definition: codec.c:63
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition: codec.h:52
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
enum ast_media_type type
Type of media this codec contains.
Definition: codec.h:50

◆ ast_codec_determine_length()

unsigned int ast_codec_determine_length ( const struct ast_codec codec,
unsigned int  samples 
)

Get the length of media (in milliseconds) given a number of samples.

Parameters
codecThe codec itself
samplesThe number of samples
Return values
lengthof media (in milliseconds)

Definition at line 407 of file codec.c.

References ast_codec::get_length.

Referenced by ast_format_determine_length().

408 {
409  if (!codec->get_length) {
410  return 0;
411  }
412 
413  return codec->get_length(samples);
414 }
int(* get_length)(unsigned int samples)
Retrieve the length of media from number of samples.
Definition: codec.h:76

◆ ast_codec_get()

struct ast_codec* ast_codec_get ( const char *  name,
enum ast_media_type  type,
unsigned int  sample_rate 
)

Retrieve a codec given a name, type, and sample rate.

Parameters
nameThe name of the codec
typeThe type of the codec
sample_rateOptional sample rate, may not be applicable for some types
Return values
non-NULLsuccess
NULLfailure
Note
The returned codec is reference counted and ao2_ref or ao2_cleanup must be used to release the reference.

Definition at line 326 of file codec.c.

References ao2_find, ast_codec::name, name, OBJ_SEARCH_OBJECT, ast_codec::sample_rate, and type.

Referenced by __ast_register_translator(), AST_TEST_DEFINE(), handle_show_translation_path(), and newpvt().

327 {
328  struct ast_codec codec = {
329  .name = name,
330  .type = type,
331  .sample_rate = sample_rate,
332  };
333 
334  return ao2_find(codecs, &codec, OBJ_SEARCH_OBJECT);
335 }
static const char type[]
Definition: chan_ooh323.c:109
const char * name
Name for this codec.
Definition: codec.h:46
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
static const char name[]
Definition: cdr_mysql.c:74
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition: codec.h:52
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
Represents a media codec within Asterisk.
Definition: codec.h:42

◆ ast_codec_get_by_id()

struct ast_codec* ast_codec_get_by_id ( int  id)

Retrieve a codec given the unique identifier.

Parameters
idThe unique identifier
Return values
non-NULLsuccess
NULLfailure
Note
Identifiers start at 1 so if iterating don't start at 0.
The returned codec is reference counted and ao2_ref or ao2_cleanup must be used to release the reference.

Definition at line 337 of file codec.c.

References ao2_callback, and codec_id_cmp().

Referenced by ast_format_cap_append_by_type(), AST_TEST_DEFINE(), complete_trans_path_choice(), handle_show_translation_path(), handle_show_translation_table(), and index2codec().

338 {
339  return ao2_callback(codecs, 0, codec_id_cmp, &id);
340 }
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
static int codec_id_cmp(void *obj, void *arg, int flags)
Callback function for getting a codec based on unique identifier.
Definition: codec.c:189
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48

◆ ast_codec_get_max()

int ast_codec_get_max ( void  )

Retrieve the current maximum identifier for codec iteration.

Returns
Maximum codec identifier

Definition at line 342 of file codec.c.

References codec_id.

Referenced by ast_format_cap_append_by_type(), and AST_TEST_DEFINE().

343 {
344  return codec_id;
345 }
static int codec_id
Current identifier value for newly registered codec.
Definition: codec.c:45

◆ ast_codec_init()

int ast_codec_init ( void  )

Initialize codec support within the core.

Return values
0success
-1failure

Definition at line 249 of file codec.c.

References AO2_ALLOC_OPT_LOCK_RWLOCK, ao2_container_alloc_hash, ARRAY_LEN, ast_cli_register_multiple, ast_register_cleanup(), CODEC_BUCKETS, codec_cmp(), codec_shutdown(), and NULL.

Referenced by asterisk_daemon().

250 {
252  ast_codec_hash_fn, NULL, codec_cmp);
253  if (!codecs) {
254  return -1;
255  }
256 
259 
260  return 0;
261 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static struct ast_cli_entry codec_cli[]
Definition: codec.c:236
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
#define NULL
Definition: resample.c:96
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
static void codec_shutdown(void)
Function called when the process is shutting down.
Definition: codec.c:242
static int codec_cmp(void *obj, void *arg, int flags)
Definition: codec.c:78
#define CODEC_BUCKETS
Number of buckets to use for codecs (should be prime for performance reasons)
Definition: codec.c:42

◆ ast_codec_media_type2str()

const char* ast_codec_media_type2str ( enum ast_media_type  type)

Conversion function to take a media type and turn it into a string.

Parameters
typeThe media type
Return values
stringrepresentation of the media type

Definition at line 347 of file codec.c.

References AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_IMAGE, AST_MEDIA_TYPE_TEXT, and AST_MEDIA_TYPE_VIDEO.

Referenced by __ast_codec_register_with_format(), add_msid_to_stream(), add_sdp_streams(), ast_rtp_engine_load_format(), ast_rtp_interpret(), ast_rtp_read(), ast_sip_create_joint_call_cap(), ast_sip_session_media_state_add(), ast_stream_create_resolved(), ast_stream_to_str(), ast_stream_topology_append_stream(), ast_stream_topology_create_from_format_cap(), ast_stream_topology_set_stream(), AST_TEST_DEFINE(), chan_pjsip_write_stream(), check_stream_positions(), create_outgoing_sdp_stream(), handle_incoming_sdp(), handle_negotiated_sdp_session_media(), handle_showchan(), is_media_state_valid(), log_caps(), negotiate_incoming_sdp_stream(), rtp_check_timeout(), sdp_requires_deferral(), set_caps(), set_incoming_call_offer_cap(), show_codecs(), stream_echo_exec(), stream_echo_write_error(), and validate_stream().

348 {
349  switch (type) {
351  return "audio";
353  return "video";
355  return "image";
356  case AST_MEDIA_TYPE_TEXT:
357  return "text";
358  default:
359  return "<unknown>";
360  }
361 }
static const char type[]
Definition: chan_ooh323.c:109

◆ ast_codec_samples_count()

unsigned int ast_codec_samples_count ( struct ast_frame frame)

Get the number of samples contained within a frame.

Parameters
frameThe frame itself
Return values
numberof samples in the frame

Definition at line 378 of file codec.c.

References ao2_ref, ast_format_get_codec(), ast_format_get_name(), AST_FRAME_IMAGE, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_log, ast_frame_subclass::format, ast_frame::frametype, LOG_WARNING, ast_codec::samples_count, and ast_frame::subclass.

Referenced by ast_rtp_interpret(), dahdi_encoder_frameout(), isAnsweringMachine(), moh_generate(), ogg_speex_read(), schedule_delivery(), socket_process_helper(), and socket_process_meta().

379 {
380  struct ast_codec *codec;
381  unsigned int samples = 0;
382 
383  if ((frame->frametype != AST_FRAME_VOICE) &&
384  (frame->frametype != AST_FRAME_VIDEO) &&
385  (frame->frametype != AST_FRAME_IMAGE)) {
386  return 0;
387  }
388 
389  codec = ast_format_get_codec(frame->subclass.format);
390 
391  if (codec->samples_count) {
392  samples = codec->samples_count(frame);
393  if ((int) samples < 0) {
394  ast_log(LOG_WARNING, "Codec %s returned invalid number of samples.\n",
396  samples = 0;
397  }
398  } else {
399  ast_log(LOG_WARNING, "Unable to calculate samples for codec %s\n",
401  }
402 
403  ao2_ref(codec, -1);
404  return samples;
405 }
struct ast_codec * ast_format_get_codec(const struct ast_format *format)
Get the codec associated with a format.
Definition: format.c:324
#define LOG_WARNING
Definition: logger.h:274
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
struct ast_frame_subclass subclass
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
int(* samples_count)(struct ast_frame *frame)
Retrieve the number of samples in a frame.
Definition: codec.h:68
enum ast_frame_type frametype
struct ast_format * format
Represents a media codec within Asterisk.
Definition: codec.h:42

◆ ast_media_type_from_str()

enum ast_media_type ast_media_type_from_str ( const char *  media_type_str)

Conversion function to take a media string and convert it to a media type.

Parameters
media_type_strThe media type string
Return values
Theast_media_type that corresponds to the string
Since
15.0.0

Definition at line 363 of file codec.c.

References AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_IMAGE, AST_MEDIA_TYPE_TEXT, AST_MEDIA_TYPE_UNKNOWN, and AST_MEDIA_TYPE_VIDEO.

Referenced by handle_incoming_sdp(), sdp_requires_deferral(), and stream_echo_exec().

364 {
365  if (!strcasecmp(media_type_str, "audio")) {
366  return AST_MEDIA_TYPE_AUDIO;
367  } else if (!strcasecmp(media_type_str, "video")) {
368  return AST_MEDIA_TYPE_VIDEO;
369  } else if (!strcasecmp(media_type_str, "image")) {
370  return AST_MEDIA_TYPE_IMAGE;
371  } else if (!strcasecmp(media_type_str, "text")) {
372  return AST_MEDIA_TYPE_TEXT;
373  } else {
374  return AST_MEDIA_TYPE_UNKNOWN;
375  }
376 }

◆ codec_cmp()

static int codec_cmp ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 78 of file codec.c.

References ast_assert, AST_MEDIA_TYPE_UNKNOWN, CMP_MATCH, ast_codec::name, OBJ_SEARCH_KEY, OBJ_SEARCH_MASK, OBJ_SEARCH_OBJECT, OBJ_SEARCH_PARTIAL_KEY, ast_codec::sample_rate, and ast_codec::type.

Referenced by ast_codec_init().

79 {
80  const struct ast_codec *left = obj;
81  const struct ast_codec *right = arg;
82  const char *right_key = arg;
83  int cmp;
84 
85  switch (flags & OBJ_SEARCH_MASK) {
86  case OBJ_SEARCH_OBJECT:
87  right_key = right->name;
88  cmp = strcmp(left->name, right_key);
89 
90  if (right->type != AST_MEDIA_TYPE_UNKNOWN) {
91  cmp |= (right->type != left->type);
92  }
93 
94  /* BUGBUG: this will allow a match on a codec by name only.
95  * This is particularly useful when executed by the CLI; if
96  * that is not needed in translate.c, this can be removed.
97  */
98  if (right->sample_rate) {
99  cmp |= (right->sample_rate != left->sample_rate);
100  }
101  break;
102  case OBJ_SEARCH_KEY:
103  cmp = strcmp(left->name, right_key);
104  break;
106  cmp = strncmp(left->name, right_key, strlen(right_key));
107  break;
108  default:
109  ast_assert(0);
110  cmp = 0;
111  break;
112  }
113  if (cmp) {
114  return 0;
115  }
116 
117  return CMP_MATCH;
118 }
const char * name
Name for this codec.
Definition: codec.h:46
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
#define ast_assert(a)
Definition: utils.h:695
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1120
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition: codec.h:52
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
enum ast_media_type type
Type of media this codec contains.
Definition: codec.h:50
Search option field mask.
Definition: astobj2.h:1076
Represents a media codec within Asterisk.
Definition: codec.h:42

◆ codec_dtor()

static void codec_dtor ( void *  obj)
static

Definition at line 263 of file codec.c.

References ast_module_unref, and ast_codec::mod.

Referenced by __ast_codec_register_with_format().

264 {
265  struct ast_codec *codec;
266 
267  codec = obj;
268 
269  ast_module_unref(codec->mod);
270 }
struct ast_module * mod
The module that registered this codec.
Definition: codec.h:82
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:469
Represents a media codec within Asterisk.
Definition: codec.h:42

◆ codec_id_cmp()

static int codec_id_cmp ( void *  obj,
void *  arg,
int  flags 
)
static

Callback function for getting a codec based on unique identifier.

Definition at line 189 of file codec.c.

References CMP_MATCH, CMP_STOP, and ast_codec::id.

Referenced by ast_codec_get_by_id(), and show_codec().

190 {
191  struct ast_codec *codec = obj;
192  int *id = arg;
193 
194  return (codec->id == *id) ? CMP_MATCH | CMP_STOP : 0;
195 }
unsigned int id
Internal unique identifier for this codec, set at registration time (starts at 1) ...
Definition: codec.h:44
Represents a media codec within Asterisk.
Definition: codec.h:42

◆ codec_shutdown()

static void codec_shutdown ( void  )
static

Function called when the process is shutting down.

Definition at line 242 of file codec.c.

References ao2_cleanup, ARRAY_LEN, ast_cli_unregister_multiple(), and NULL.

Referenced by ast_codec_init().

243 {
246  codecs = NULL;
247 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static struct ast_cli_entry codec_cli[]
Definition: codec.c:236
#define NULL
Definition: resample.c:96
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ show_codec()

static char* show_codec ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 197 of file codec.c.

References ao2_callback, ao2_ref, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, codec_id_cmp(), ast_cli_entry::command, ast_codec::description, internal_ast_codec::external, ast_cli_args::fd, internal_ast_codec::format_name, ast_codec::id, NULL, S_OR, and ast_cli_entry::usage.

198 {
199  int type_punned_codec;
200  struct internal_ast_codec *codec;
201 
202  switch (cmd) {
203  case CLI_INIT:
204  e->command = "core show codec";
205  e->usage =
206  "Usage: core show codec <number>\n"
207  " Displays codec mapping\n";
208  return NULL;
209  case CLI_GENERATE:
210  return NULL;
211  }
212 
213  if (a->argc != 4) {
214  return CLI_SHOWUSAGE;
215  }
216 
217  if (sscanf(a->argv[3], "%30d", &type_punned_codec) != 1) {
218  return CLI_SHOWUSAGE;
219  }
220 
221  codec = ao2_callback(codecs, 0, codec_id_cmp, &type_punned_codec);
222  if (!codec) {
223  ast_cli(a->fd, "Codec %d not found\n", type_punned_codec);
224  return CLI_SUCCESS;
225  }
226 
227  ast_cli(a->fd, "%11u %s (%s)\n", (unsigned int) codec->external.id, codec->external.description,
228  S_OR(codec->format_name, "no format"));
229 
230  ao2_ref(codec, -1);
231 
232  return CLI_SUCCESS;
233 }
unsigned int id
Internal unique identifier for this codec, set at registration time (starts at 1) ...
Definition: codec.h:44
const int argc
Definition: cli.h:160
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
struct ast_codec external
Public codec structure. Must remain first.
Definition: codec.c:61
Definition: cli.h:152
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
static int codec_id_cmp(void *obj, void *arg, int flags)
Callback function for getting a codec based on unique identifier.
Definition: codec.c:189
const int fd
Definition: cli.h:159
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
#define ao2_ref(o, delta)
Definition: astobj2.h:464
const char *const * argv
Definition: cli.h:161
const char * description
Brief description.
Definition: codec.h:48
#define CLI_SHOWUSAGE
Definition: cli.h:45
char * command
Definition: cli.h:186
const char * format_name
A format name for a default sane format using this codec.
Definition: codec.c:63
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79

◆ show_codecs()

static char* show_codecs ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 120 of file codec.c.

References ao2_iterator_destroy(), AO2_ITERATOR_DONTLOCK, ao2_iterator_init(), ao2_iterator_next, ao2_rdlock, ao2_ref, ao2_unlock, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_codec_media_type2str(), AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_IMAGE, AST_MEDIA_TYPE_TEXT, AST_MEDIA_TYPE_VIDEO, ast_opt_dont_warn, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_codec::description, internal_ast_codec::external, ast_cli_args::fd, internal_ast_codec::format_name, ast_codec::id, ast_codec::name, NULL, S_OR, ast_codec::type, and ast_cli_entry::usage.

121 {
122  struct ao2_iterator i;
123  struct internal_ast_codec *codec;
124 
125  switch (cmd) {
126  case CLI_INIT:
127  e->command = "core show codecs [audio|video|image|text]";
128  e->usage =
129  "Usage: core show codecs [audio|video|image|text]\n"
130  " Displays codec mapping\n";
131  return NULL;
132  case CLI_GENERATE:
133  return NULL;
134  }
135 
136  if ((a->argc < 3) || (a->argc > 4)) {
137  return CLI_SHOWUSAGE;
138  }
139 
140  if (!ast_opt_dont_warn) {
141  ast_cli(a->fd, "Disclaimer: this command is for informational purposes only.\n"
142  "\tIt does not indicate anything about your configuration.\n");
143  }
144 
145  ast_cli(a->fd, "%8s %-5s %-12s %-16s %s\n","ID","TYPE","NAME","FORMAT","DESCRIPTION");
146  ast_cli(a->fd, "------------------------------------------------------------------------------------------------\n");
147 
150 
151  for (; (codec = ao2_iterator_next(&i)); ao2_ref(codec, -1)) {
152  if (a->argc == 4) {
153  if (!strcasecmp(a->argv[3], "audio")) {
154  if (codec->external.type != AST_MEDIA_TYPE_AUDIO) {
155  continue;
156  }
157  } else if (!strcasecmp(a->argv[3], "video")) {
158  if (codec->external.type != AST_MEDIA_TYPE_VIDEO) {
159  continue;
160  }
161  } else if (!strcasecmp(a->argv[3], "image")) {
162  if (codec->external.type != AST_MEDIA_TYPE_IMAGE) {
163  continue;
164  }
165  } else if (!strcasecmp(a->argv[3], "text")) {
166  if (codec->external.type != AST_MEDIA_TYPE_TEXT) {
167  continue;
168  }
169  } else {
170  continue;
171  }
172  }
173 
174  ast_cli(a->fd, "%8u %-5s %-12s %-16s (%s)\n",
175  codec->external.id,
177  codec->external.name,
178  S_OR(codec->format_name, "no cached format"),
179  codec->external.description);
180  }
181 
184 
185  return CLI_SUCCESS;
186 }
const char * name
Name for this codec.
Definition: codec.h:46
const char * ast_codec_media_type2str(enum ast_media_type type)
Conversion function to take a media type and turn it into a string.
Definition: codec.c:347
unsigned int id
Internal unique identifier for this codec, set at registration time (starts at 1) ...
Definition: codec.h:44
const int argc
Definition: cli.h:160
struct ast_codec external
Public codec structure. Must remain first.
Definition: codec.c:61
Definition: cli.h:152
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
#define ast_opt_dont_warn
Definition: options.h:125
const int fd
Definition: cli.h:159
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
#define ao2_ref(o, delta)
Definition: astobj2.h:464
const char *const * argv
Definition: cli.h:161
const char * description
Brief description.
Definition: codec.h:48
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define ao2_rdlock(a)
Definition: astobj2.h:719
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
char * command
Definition: cli.h:186
const char * format_name
A format name for a default sane format using this codec.
Definition: codec.c:63
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
Assume that the ao2_container is already locked.
Definition: astobj2.h:1872
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
enum ast_media_type type
Type of media this codec contains.
Definition: codec.h:50
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.

Variable Documentation

◆ codec_cli

struct ast_cli_entry codec_cli[]
static
Initial value:
= {
{ .handler = show_codecs , .summary = "Displays a list of registered codecs" ,},
{ .handler = show_codec , .summary = "Shows a specific codec" ,},
}
static char * show_codecs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: codec.c:120
static char * show_codec(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: codec.c:197

Definition at line 236 of file codec.c.

◆ codec_id

int codec_id = 1
static

Current identifier value for newly registered codec.

Definition at line 45 of file codec.c.

Referenced by __ast_codec_register_with_format(), and ast_codec_get_max().

◆ codecs

struct ao2_container* codecs
static

Registered codecs.

Definition at line 48 of file codec.c.

Referenced by action_originate(), ast_rtp_read(), codec_prefs_to_str(), handle_capabilities_res_message(), and process_sdp().