Asterisk - The Open Source Telephony Project  21.4.1
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"

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)
 

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.
 
static void codec_shutdown (void)
 Function called when the process is shutting down.
 
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.
 
static struct ao2_containercodecs
 Registered codecs.
 

Detailed Description

Codecs API.

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

Definition in file codec.c.

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

References ast_codec::get_length.

Referenced by ast_format_determine_length().

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

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

Referenced by __ast_register_translator(), and newpvt().

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 }
const char * name
Name for this codec.
Definition: codec.h:46
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
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:1087
enum ast_media_type type
Type of media this codec contains.
Definition: codec.h:50
Represents a media codec within Asterisk.
Definition: codec.h:42
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.

References ao2_callback, and codec_id_cmp().

Referenced by ast_format_cap_append_by_type().

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, as described below.
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
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
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.

References codec_id.

Referenced by ast_format_cap_append_by_type().

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

Initialize codec support within the core.

Return values
0success
-1failure

Definition at line 250 of file codec.c.

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

251 {
253  ast_codec_hash_fn, NULL, codec_cmp);
254  if (!codecs) {
255  return -1;
256  }
257 
258  ast_cli_register_multiple(codec_cli, ARRAY_LEN(codec_cli));
260 
261  return 0;
262 }
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
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)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
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
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 348 of file codec.c.

Referenced by ast_rtp_engine_load_format(), ast_rtp_read(), 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(), create_outgoing_sdp_stream(), handle_showchan(), negotiate_incoming_sdp_stream(), and rtp_check_timeout().

349 {
350  switch (type) {
351  case AST_MEDIA_TYPE_AUDIO:
352  return "audio";
353  case AST_MEDIA_TYPE_VIDEO:
354  return "video";
355  case AST_MEDIA_TYPE_IMAGE:
356  return "image";
357  case AST_MEDIA_TYPE_TEXT:
358  return "text";
359  default:
360  return "<unknown>";
361  }
362 }
enum ast_media_type type
Type of media this codec contains.
Definition: codec.h:50
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.

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

Referenced by ogg_speex_read(), and schedule_delivery().

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 }
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
struct ast_frame_subclass subclass
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
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
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")) {
367  return AST_MEDIA_TYPE_AUDIO;
368  } else if (!strcasecmp(media_type_str, "video")) {
369  return AST_MEDIA_TYPE_VIDEO;
370  } else if (!strcasecmp(media_type_str, "image")) {
371  return AST_MEDIA_TYPE_IMAGE;
372  } else if (!strcasecmp(media_type_str, "text")) {
373  return AST_MEDIA_TYPE_TEXT;
374  } else {
375  return AST_MEDIA_TYPE_UNKNOWN;
376  }
377 }

Variable Documentation

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" ,},
}

Definition at line 237 of file codec.c.