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

Defines

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

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
}
 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.
int ast_codec_builtin_init (void)
 Initialize built-in codecs within the core.
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.
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.
struct ast_codecast_codec_get_by_id (int id)
 Retrieve a codec given the unique identifier.
int ast_codec_get_max (void)
 Retrieve the current maximum identifier for codec iteration.
int ast_codec_init (void)
 Initialize codec support within the core.
const char * ast_codec_media_type2str (enum ast_media_type type)
 Conversion function to take a media type and turn it into a string.
unsigned int ast_codec_samples_count (struct ast_frame *frame)
 Get the number of samples contained within a frame.


Detailed Description

Codec API.

Author:
Joshua Colp <jcolp@digium.com>

Definition in file codec.h.


Define Documentation

#define ast_codec_register ( codec   )     __ast_codec_register(codec, ast_module_info->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:
codec to register
Return values:
0 success
-1 failure

Definition at line 119 of file codec.h.

Referenced by AST_TEST_DEFINE(), and load_module().


Enumeration Type Documentation

Types of media.

Enumerator:
AST_MEDIA_TYPE_UNKNOWN 
AST_MEDIA_TYPE_AUDIO 
AST_MEDIA_TYPE_VIDEO 
AST_MEDIA_TYPE_IMAGE 
AST_MEDIA_TYPE_TEXT 

Definition at line 30 of file codec.h.


Function Documentation

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:
codec to register
mod the module this codec is provided by
Return values:
0 success
-1 failure

Definition at line 262 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(), ast_codec::description, 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.

00263 {
00264    SCOPED_AO2WRLOCK(lock, codecs);
00265    struct ast_codec *codec_new;
00266 
00267    /* Some types have specific requirements */
00268    if (codec->type == AST_MEDIA_TYPE_UNKNOWN) {
00269       ast_log(LOG_ERROR, "A media type must be specified for codec '%s'\n", codec->name);
00270       return -1;
00271    } else if (codec->type == AST_MEDIA_TYPE_AUDIO) {
00272       if (!codec->sample_rate) {
00273          ast_log(LOG_ERROR, "A sample rate must be specified for codec '%s' of type '%s'\n",
00274             codec->name, ast_codec_media_type2str(codec->type));
00275          return -1;
00276       }
00277    }
00278 
00279    codec_new = ao2_find(codecs, codec, OBJ_SEARCH_OBJECT | OBJ_NOLOCK);
00280    if (codec_new) {
00281       ast_log(LOG_ERROR, "A codec with name '%s' of type '%s' and sample rate '%u' is already registered\n",
00282          codec->name, ast_codec_media_type2str(codec->type), codec->sample_rate);
00283       ao2_ref(codec_new, -1);
00284       return -1;
00285    }
00286 
00287    codec_new = ao2_t_alloc_options(sizeof(*codec_new), codec_dtor,
00288       AO2_ALLOC_OPT_LOCK_NOLOCK, S_OR(codec->description, ""));
00289    if (!codec_new) {
00290       ast_log(LOG_ERROR, "Could not allocate a codec with name '%s' of type '%s' and sample rate '%u'\n",
00291          codec->name, ast_codec_media_type2str(codec->type), codec->sample_rate);
00292       return -1;
00293    }
00294    *codec_new = *codec;
00295    codec_new->id = codec_id++;
00296 
00297    ao2_link_flags(codecs, codec_new, OBJ_NOLOCK);
00298 
00299    /* Once registered a codec can not be unregistered, and the module must persist until shutdown */
00300    ast_module_shutdown_ref(mod);
00301 
00302    ast_verb(2, "Registered '%s' codec '%s' at sample rate '%u' with id '%u'\n",
00303       ast_codec_media_type2str(codec->type), codec->name, codec->sample_rate, codec_new->id);
00304 
00305    ao2_ref(codec_new, -1);
00306 
00307    return 0;
00308 }

int ast_codec_builtin_init ( void   ) 

Initialize built-in codecs within the core.

Return values:
0 success
-1 failure

Definition at line 800 of file codec_builtin.c.

References CODEC_REGISTER_AND_CACHE, and CODEC_REGISTER_AND_CACHE_NAMED.

Referenced by main().

00801 {
00802    int res = 0;
00803 
00804    res |= CODEC_REGISTER_AND_CACHE(g723);
00805    res |= CODEC_REGISTER_AND_CACHE(ulaw);
00806    res |= CODEC_REGISTER_AND_CACHE(alaw);
00807    res |= CODEC_REGISTER_AND_CACHE(gsm);
00808    res |= CODEC_REGISTER_AND_CACHE(g726rfc3551);
00809    res |= CODEC_REGISTER_AND_CACHE(g726aal2);
00810    res |= CODEC_REGISTER_AND_CACHE(adpcm);
00811    res |= CODEC_REGISTER_AND_CACHE(slin8);
00812    res |= CODEC_REGISTER_AND_CACHE_NAMED("slin12", slin12);
00813    res |= CODEC_REGISTER_AND_CACHE_NAMED("slin16", slin16);
00814    res |= CODEC_REGISTER_AND_CACHE_NAMED("slin24", slin24);
00815    res |= CODEC_REGISTER_AND_CACHE_NAMED("slin32", slin32);
00816    res |= CODEC_REGISTER_AND_CACHE_NAMED("slin44", slin44);
00817    res |= CODEC_REGISTER_AND_CACHE_NAMED("slin48", slin48);
00818    res |= CODEC_REGISTER_AND_CACHE_NAMED("slin96", slin96);
00819    res |= CODEC_REGISTER_AND_CACHE_NAMED("slin192", slin192);
00820    res |= CODEC_REGISTER_AND_CACHE(lpc10);
00821    res |= CODEC_REGISTER_AND_CACHE(g729a);
00822    res |= CODEC_REGISTER_AND_CACHE(speex8);
00823    res |= CODEC_REGISTER_AND_CACHE_NAMED("speex16", speex16);
00824    res |= CODEC_REGISTER_AND_CACHE_NAMED("speex32", speex32);
00825    res |= CODEC_REGISTER_AND_CACHE(ilbc);
00826    res |= CODEC_REGISTER_AND_CACHE(g722);
00827    res |= CODEC_REGISTER_AND_CACHE(siren7);
00828    res |= CODEC_REGISTER_AND_CACHE(siren14);
00829    res |= CODEC_REGISTER_AND_CACHE(testlaw);
00830    res |= CODEC_REGISTER_AND_CACHE(g719);
00831    res |= CODEC_REGISTER_AND_CACHE(opus);
00832    res |= CODEC_REGISTER_AND_CACHE(jpeg);
00833    res |= CODEC_REGISTER_AND_CACHE(png);
00834    res |= CODEC_REGISTER_AND_CACHE(h261);
00835    res |= CODEC_REGISTER_AND_CACHE(h263);
00836    res |= CODEC_REGISTER_AND_CACHE(h263p);
00837    res |= CODEC_REGISTER_AND_CACHE(h264);
00838    res |= CODEC_REGISTER_AND_CACHE(mpeg4);
00839    res |= CODEC_REGISTER_AND_CACHE(vp8);
00840    res |= CODEC_REGISTER_AND_CACHE(t140red);
00841    res |= CODEC_REGISTER_AND_CACHE(t140);
00842    res |= CODEC_REGISTER_AND_CACHE(none);
00843 
00844    return res;
00845 }

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:
codec The codec itself
samples The number of samples
Return values:
length of media (in milliseconds)

Definition at line 371 of file codec.c.

References ast_codec::get_length.

Referenced by ast_format_determine_length().

00372 {
00373    if (!codec->get_length) {
00374       return 0;
00375    }
00376 
00377    return codec->get_length(samples);
00378 }

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

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

Parameters:
name The name of the codec
type The type of the codec
sample_rate Optional sample rate, may not be applicable for some types
Return values:
non-NULL success
NULL failure
Note:
The returned codec is reference counted and ao2_ref or ao2_cleanup must be used to release the reference.

Definition at line 310 of file codec.c.

References ao2_find, ast_codec::name, and OBJ_SEARCH_OBJECT.

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

00311 {
00312    struct ast_codec codec = {
00313       .name = name,
00314       .type = type,
00315       .sample_rate = sample_rate,
00316    };
00317 
00318    return ao2_find(codecs, &codec, OBJ_SEARCH_OBJECT);
00319 }

struct ast_codec* ast_codec_get_by_id ( int  id  )  [read]

Retrieve a codec given the unique identifier.

Parameters:
id The unique identifier
Return values:
non-NULL success
NULL failure
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 321 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(), data_add_codec(), handle_show_translation_path(), handle_show_translation_table(), and index2codec().

00322 {
00323    return ao2_callback(codecs, 0, codec_id_cmp, &id);
00324 }

int ast_codec_get_max ( void   ) 

Retrieve the current maximum identifier for codec iteration.

Returns:
Maximum codec identifier

Definition at line 326 of file codec.c.

Referenced by ast_format_cap_append_by_type(), and AST_TEST_DEFINE().

00327 {
00328    return codec_id;
00329 }

int ast_codec_init ( void   ) 

Initialize codec support within the core.

Return values:
0 success
-1 failure

Definition at line 240 of file codec.c.

References AO2_ALLOC_OPT_LOCK_RWLOCK, ao2_container_alloc_options, ARRAY_LEN, ast_cli_register_multiple(), ast_register_cleanup(), CODEC_BUCKETS, codec_cmp(), codec_hash(), and codec_shutdown().

Referenced by main().

00241 {
00242    codecs = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, CODEC_BUCKETS, codec_hash, codec_cmp);
00243    if (!codecs) {
00244       return -1;
00245    }
00246 
00247    ast_cli_register_multiple(codec_cli, ARRAY_LEN(codec_cli));
00248    ast_register_cleanup(codec_shutdown);
00249 
00250    return 0;
00251 }

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:
type The media type
Return values:
string representation of the media type

Definition at line 331 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(), ast_rtp_engine_load_format(), ast_rtp_read(), and show_codecs().

00332 {
00333    switch (type) {
00334    case AST_MEDIA_TYPE_AUDIO:
00335       return "audio";
00336    case AST_MEDIA_TYPE_VIDEO:
00337       return "video";
00338    case AST_MEDIA_TYPE_IMAGE:
00339       return "image";
00340    case AST_MEDIA_TYPE_TEXT:
00341       return "text";
00342    default:
00343       return "<unknown>";
00344    }
00345 }

unsigned int ast_codec_samples_count ( struct ast_frame frame  ) 

Get the number of samples contained within a frame.

Parameters:
frame The frame itself
Return values:
number of samples in the frame

Definition at line 347 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_read(), dahdi_encoder_frameout(), isAnsweringMachine(), moh_generate(), schedule_delivery(), socket_process_helper(), and socket_process_meta().

00348 {
00349    struct ast_codec *codec;
00350    unsigned int samples = 0;
00351 
00352    if ((frame->frametype != AST_FRAME_VOICE) &&
00353       (frame->frametype != AST_FRAME_VIDEO) &&
00354       (frame->frametype != AST_FRAME_IMAGE)) {
00355       return 0;
00356    }
00357 
00358    codec = ast_format_get_codec(frame->subclass.format);
00359 
00360    if (codec->samples_count) {
00361       samples = codec->samples_count(frame);
00362    } else {
00363       ast_log(LOG_WARNING, "Unable to calculate samples for codec %s\n",
00364          ast_format_get_name(frame->subclass.format));
00365    }
00366 
00367    ao2_ref(codec, -1);
00368    return samples;
00369 }


Generated on Thu Apr 16 06:30:14 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6