Asterisk - The Open Source Telephony Project GIT-master-7e7a603
Data Structures | Macros | Enumerations | Functions
codec.h File Reference

Codec API. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_codec
 Represents a media codec within Asterisk. More...
 

Macros

#define ast_codec_register(codec)   __ast_codec_register(codec, AST_MODULE_SELF)
 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...
 

Enumerations

enum  ast_media_type {
  AST_MEDIA_TYPE_UNKNOWN = 0 , AST_MEDIA_TYPE_AUDIO , AST_MEDIA_TYPE_VIDEO , AST_MEDIA_TYPE_IMAGE ,
  AST_MEDIA_TYPE_TEXT , AST_MEDIA_TYPE_END
}
 Types of media. 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_builtin_init (void)
 Initialize built-in codecs within the core. More...
 
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...
 

Detailed Description

Codec API.

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

Definition in file codec.h.

Macro Definition Documentation

◆ ast_codec_register

#define ast_codec_register (   codec)    __ast_codec_register(codec, AST_MODULE_SELF)

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
Return values
0success
-1failure

Definition at line 124 of file codec.h.

Enumeration Type Documentation

◆ ast_media_type

Types of media.

Enumerator
AST_MEDIA_TYPE_UNKNOWN 
AST_MEDIA_TYPE_AUDIO 
AST_MEDIA_TYPE_VIDEO 
AST_MEDIA_TYPE_IMAGE 
AST_MEDIA_TYPE_TEXT 
AST_MEDIA_TYPE_END 

Definition at line 30 of file codec.h.

30 {
37};
@ AST_MEDIA_TYPE_AUDIO
Definition: codec.h:32
@ AST_MEDIA_TYPE_UNKNOWN
Definition: codec.h:31
@ AST_MEDIA_TYPE_VIDEO
Definition: codec.h:33
@ AST_MEDIA_TYPE_END
Definition: codec.h:36
@ AST_MEDIA_TYPE_IMAGE
Definition: codec.h:34
@ AST_MEDIA_TYPE_TEXT
Definition: codec.h:35

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 273 of file codec.c.

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

References __ast_codec_register_with_format(), ast_codec::mod, and NULL.

◆ ast_codec_builtin_init()

int ast_codec_builtin_init ( void  )

Initialize built-in codecs within the core.

Return values
0success
-1failure

Definition at line 956 of file codec_builtin.c.

957{
958 int res = 0;
959
969 res |= CODEC_REGISTER_AND_CACHE_NAMED("slin12", slin12);
970 res |= CODEC_REGISTER_AND_CACHE_NAMED("slin16", slin16);
971 res |= CODEC_REGISTER_AND_CACHE_NAMED("slin24", slin24);
972 res |= CODEC_REGISTER_AND_CACHE_NAMED("slin32", slin32);
973 res |= CODEC_REGISTER_AND_CACHE_NAMED("slin44", slin44);
974 res |= CODEC_REGISTER_AND_CACHE_NAMED("slin48", slin48);
975 res |= CODEC_REGISTER_AND_CACHE_NAMED("slin96", slin96);
976 res |= CODEC_REGISTER_AND_CACHE_NAMED("slin192", slin192);
980 res |= CODEC_REGISTER_AND_CACHE_NAMED("speex16", speex16);
981 res |= CODEC_REGISTER_AND_CACHE_NAMED("speex32", speex32);
1002 res |= CODEC_REGISTER_AND_CACHE_NAMED("silk8", silk8);
1003 res |= CODEC_REGISTER_AND_CACHE_NAMED("silk12", silk12);
1004 res |= CODEC_REGISTER_AND_CACHE_NAMED("silk16", silk16);
1005 res |= CODEC_REGISTER_AND_CACHE_NAMED("silk24", silk24);
1006
1007 return res;
1008}
static struct ast_codec g722
static struct ast_codec codec2
static struct ast_codec jpeg
static struct ast_codec t38
static struct ast_codec siren7
static struct ast_codec silk16
static struct ast_codec h263p
static struct ast_codec h263
static struct ast_codec ulaw
static struct ast_codec silk24
static struct ast_codec slin16
static struct ast_codec siren14
static struct ast_codec adpcm
static struct ast_codec silk8
static struct ast_codec g723
Definition: codec_builtin.c:97
static struct ast_codec g729a
static struct ast_codec slin32
static struct ast_codec h265
static struct ast_codec lpc10
static struct ast_codec vp8
static struct ast_codec slin192
static struct ast_codec g726rfc3551
static struct ast_codec none
static struct ast_codec g719
static struct ast_codec t140
static struct ast_codec ilbc
static struct ast_codec t140red
static struct ast_codec mpeg4
static struct ast_codec slin48
static struct ast_codec speex8
static struct ast_codec g726aal2
static struct ast_codec png
static struct ast_codec slin24
#define CODEC_REGISTER_AND_CACHE(codec)
static struct ast_codec speex32
static struct ast_codec speex16
static struct ast_codec slin8
static struct ast_codec alaw
static struct ast_codec h261
static struct ast_codec slin12
static struct ast_codec vp9
static struct ast_codec opus
static struct ast_codec h264
static struct ast_codec silk12
static struct ast_codec slin96
#define CODEC_REGISTER_AND_CACHE_NAMED(fmt_name, codec)
static struct ast_codec slin44

References adpcm, alaw, codec2, CODEC_REGISTER_AND_CACHE, CODEC_REGISTER_AND_CACHE_NAMED, g719, g722, g723, g726aal2, g726rfc3551, g729a, h261, h263, h263p, h264, h265, ilbc, jpeg, lpc10, mpeg4, none, opus, png, silk12, silk16, silk24, silk8, siren14, siren7, slin12, slin16, slin192, slin24, slin32, slin44, slin48, slin8, slin96, speex16, speex32, speex8, t140, t140red, t38, ulaw, vp8, and vp9.

Referenced by asterisk_daemon().

◆ 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 408 of file codec.c.

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

References ast_codec::get_length.

Referenced by ast_format_determine_length().

◆ 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 327 of file codec.c.

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

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

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

◆ 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 338 of file codec.c.

339{
340 return ao2_callback(codecs, 0, codec_id_cmp, &id);
341}
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
static int codec_id_cmp(void *obj, void *arg, int flags)
Callback function for getting a codec based on unique identifier.
Definition: codec.c:190

References ao2_callback, codec_id_cmp(), and codecs.

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().

◆ 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 343 of file codec.c.

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

References codec_id.

Referenced by ast_format_cap_append_by_type(), and AST_TEST_DEFINE().

◆ ast_codec_init()

int ast_codec_init ( void  )

Initialize codec support within the core.

Return values
0success
-1failure

Definition at line 250 of file codec.c.

251{
253 ast_codec_hash_fn, NULL, codec_cmp);
254 if (!codecs) {
255 return -1;
256 }
257
260
261 return 0;
262}
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
@ AO2_ALLOC_OPT_LOCK_RWLOCK
Definition: astobj2.h:365
#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
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static void codec_shutdown(void)
Function called when the process is shutting down.
Definition: codec.c:243
#define CODEC_BUCKETS
Number of buckets to use for codecs (should be prime for performance reasons)
Definition: codec.c:42
static int codec_cmp(void *obj, void *arg, int flags)
Definition: codec.c:78
static struct ast_cli_entry codec_cli[]
Definition: codec.c:237
#define ARRAY_LEN(a)
Definition: utils.h:666

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

Referenced by asterisk_daemon().

◆ ast_codec_media_type2str()

const char * ast_codec_media_type2str ( enum ast_media_type  type)

◆ 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 379 of file codec.c.

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

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().

◆ 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 364 of file codec.c.

365{
366 if (!strcasecmp(media_type_str, "audio")) {
368 } else if (!strcasecmp(media_type_str, "video")) {
370 } else if (!strcasecmp(media_type_str, "image")) {
372 } else if (!strcasecmp(media_type_str, "text")) {
373 return AST_MEDIA_TYPE_TEXT;
374 } else {
376 }
377}

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().