Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:42 2009

## Asterisk developer's documentation

# codec_alaw.c File Reference

codec_alaw.c - translate between signed linear and alaw More...

`#include <fcntl.h>`
`#include <netinet/in.h>`
`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <unistd.h>`
`#include "asterisk.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/module.h"`
`#include "asterisk/config.h"`
`#include "asterisk/options.h"`
`#include "asterisk/translate.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/alaw.h"`
`#include "slin_ulaw_ex.h"`
`#include "ulaw_slin_ex.h"`

Include dependency graph for codec_alaw.c:

Go to the source code of this file.

## Data Structures

struct | alaw_decoder_pvt
| Private workspace for translating alaw signals to signed linear. More...
struct | alaw_encoder_pvt
| Private workspace for translating signed linear signals to alaw. More...

## Defines

#define | BUFFER_SIZE 8096

## Functions

static void | alaw_destroy (struct ast_translator_pvt *pvt)
| alaw_Destroy Destroys a private workspace.
static int | alawtolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
| alawToLin_FrameIn Fill an input buffer with packed 4-bit alaw values if there is room left.
static struct ast_frame * | alawtolin_frameout (struct ast_translator_pvt *pvt)
| alawToLin_FrameOut Convert 4-bit alaw encoded signals to 16-bit signed linear.
static struct ast_translator_pvt * | alawtolin_new (void)
| alawToLin_New Create a new instance of alaw_decoder_pvt.
static struct ast_frame * | alawtolin_sample (void)
| alawToLin_Sample
| AST_MUTEX_DEFINE_STATIC (localuser_lock)
char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
static int | lintoalaw_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
| LinToalaw_FrameIn Fill an input buffer with 16-bit signed linear PCM values.
static struct ast_frame * | lintoalaw_frameout (struct ast_translator_pvt *pvt)
| LinToalaw_FrameOut Convert a buffer of raw 16-bit signed linear PCM to a buffer of 4-bit alaw packed two to a byte (Big Endian).
static struct ast_translator_pvt * | lintoalaw_new (void)
| LinToalaw_New Create a new instance of alaw_encoder_pvt.
static struct ast_frame * | lintoalaw_sample (void)
| LinToalaw_Sample.
int | load_module (void)
| Initialize the module.
static void | parse_config (void)
int | reload (void)
| Reload stuff.
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static struct ast_translator | alawtolin
| The complete translator for alawToLin.
static struct ast_translator | lintoalaw
| The complete translator for LinToalaw.
static int | localusecnt = 0
static char * | tdesc = "A-law Coder/Decoder"
static int | useplc = 0

## Detailed Description

codec_alaw.c - translate between signed linear and alaw

Definition in file codec_alaw.c.

## Define Documentation

#define BUFFER_SIZE 8096

Definition at line 46 of file codec_alaw.c.

## Function Documentation

static void alaw_destroy | ( | struct ast_translator_pvt * | pvt | ) | `[static]`

alaw_Destroy Destroys a private workspace.

Results: It's gone!

Side effects: None.

Definition at line 319 of file codec_alaw.c.

References ast_update_use_count(), and free.

    00320 {
    00321   free (pvt);
    00322   localusecnt--;
    00323   ast_update_use_count ();
    00324 }

static int alawtolin_framein | ( | struct ast_translator_pvt * | pvt,
|  | struct ast_frame * | f |
| ) |  |  | `[static]`

alawToLin_FrameIn Fill an input buffer with packed 4-bit alaw values if there is room left.

Results: Foo

Side effects: tmp->tail is the number of packed values in the buffer.

Definition at line 147 of file codec_alaw.c.

References AST_ALAW, ast_log(), ast_frame::data, ast_frame::datalen, LOG_WARNING, alaw_decoder_pvt::outbuf, alaw_decoder_pvt::plc, plc_fillin(), plc_rx(), and alaw_decoder_pvt::tail.

    00148 {
    00149   struct alaw_decoder_pvt *tmp = (struct alaw_decoder_pvt *) pvt;
    00150   int x;
    00151   unsigned char *b;
    00152
    00153   if(f->datalen == 0) { /* perform PLC with nominal framesize of 20ms/160 samples */
    00154         if((tmp->tail + 160)  * 2 > sizeof(tmp->outbuf)) {
    00155             ast_log(LOG_WARNING, "Out of buffer space\n");
    00156             return -1;
    00157         }
    00158         if(useplc) {
    00159        plc_fillin(&tmp->plc, tmp->outbuf+tmp->tail, 160);
    00160        tmp->tail += 160;
    00161    }
    00162         return 0;
    00163   }
    00164
    00165   if ((tmp->tail + f->datalen) * 2 > sizeof(tmp->outbuf)) {
    00166    ast_log(LOG_WARNING, "Out of buffer space\n");
    00167    return -1;
    00168   }
    00169
    00170   /* Reset ssindex and signal to frame's specified values */
    00171   b = f->data;
    00172   for (x=0;x<f->datalen;x++)
    00173    tmp->outbuf[tmp->tail + x] = AST_ALAW(b[x]);
    00174
    00175   if(useplc) plc_rx(&tmp->plc, tmp->outbuf+tmp->tail, f->datalen);
    00176
    00177   tmp->tail += f->datalen;
    00178   return 0;
    00179 }

static struct ast_frame* alawtolin_frameout | ( | struct ast_translator_pvt * | pvt | ) | `[static, read]`

alawToLin_FrameOut Convert 4-bit alaw encoded signals to 16-bit signed linear.

Results: Converted signals are placed in tmp->f.data, tmp->f.datalen and tmp->f.samples are calculated.

Side effects: None.

Definition at line 193 of file codec_alaw.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_frame::data, ast_frame::datalen, alaw_decoder_pvt::f, ast_frame::frametype, ast_frame::mallocd, ast_frame::offset, alaw_decoder_pvt::outbuf, ast_frame::samples, ast_frame::src, ast_frame::subclass, and alaw_decoder_pvt::tail.

    00194 {
    00195   struct alaw_decoder_pvt *tmp = (struct alaw_decoder_pvt *) pvt;
    00196
    00197   if (!tmp->tail)
    00198     return NULL;
    00199
    00200   tmp->f.frametype = AST_FRAME_VOICE;
    00201   tmp->f.subclass = AST_FORMAT_SLINEAR;
    00202   tmp->f.datalen = tmp->tail *2;
    00203   tmp->f.samples = tmp->tail;
    00204   tmp->f.mallocd = 0;
    00205   tmp->f.offset = AST_FRIENDLY_OFFSET;
    00206   tmp->f.src = __PRETTY_FUNCTION__;
    00207   tmp->f.data = tmp->outbuf;
    00208   tmp->tail = 0;
    00209   return &tmp->f;
    00210 }

static struct ast_translator_pvt* alawtolin_new | ( | void |  | ) | `[static, read]`

alawToLin_New Create a new instance of alaw_decoder_pvt.

Results: Returns a pointer to the new instance.

Side effects: None.

Definition at line 94 of file codec_alaw.c.

References ast_update_use_count(), malloc, alaw_decoder_pvt::plc, plc_init(), and alaw_decoder_pvt::tail.

    00095 {
    00096   struct alaw_decoder_pvt *tmp;
    00097   tmp = malloc (sizeof (struct alaw_decoder_pvt));
    00098   if (tmp)
    00099     {
    00100      memset(tmp, 0, sizeof(*tmp));
    00101       tmp->tail = 0;
    00102       plc_init(&tmp->plc);
    00103       localusecnt++;
    00104       ast_update_use_count ();
    00105     }
    00106   return (struct ast_translator_pvt *) tmp;
    00107 }

static struct ast_frame* alawtolin_sample | ( | void |  | ) | `[static, read]`

alawToLin_Sample

Definition at line 275 of file codec_alaw.c.

References AST_FORMAT_ALAW, AST_FRAME_VOICE, ast_frame::data, ast_frame::datalen, ast_frame::frametype, ast_frame::mallocd, ast_frame::offset, ast_frame::samples, ast_frame::src, ast_frame::subclass, and ulaw_slin_ex.

    00276 {
    00277   static struct ast_frame f;
    00278   f.frametype = AST_FRAME_VOICE;
    00279   f.subclass = AST_FORMAT_ALAW;
    00280   f.datalen = sizeof (ulaw_slin_ex);
    00281   f.samples = sizeof(ulaw_slin_ex);
    00282   f.mallocd = 0;
    00283   f.offset = 0;
    00284   f.src = __PRETTY_FUNCTION__;
    00285   f.data = ulaw_slin_ex;
    00286   return &f;
    00287 }

AST_MUTEX_DEFINE_STATIC | ( | localuser_lock |  | ) |

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 413 of file codec_alaw.c.

    00414 {
    00415   return tdesc;
    00416 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 425 of file codec_alaw.c.

References ASTERISK_GPL_KEY.

    00426 {
    00427   return ASTERISK_GPL_KEY;
    00428 }

static int lintoalaw_framein | ( | struct ast_translator_pvt * | pvt,
|  | struct ast_frame * | f |
| ) |  |  | `[static]`

LinToalaw_FrameIn Fill an input buffer with 16-bit signed linear PCM values.

Results: None.

Side effects: tmp->tail is number of signal values in the input buffer.

Definition at line 223 of file codec_alaw.c.

References AST_LIN2A, ast_log(), ast_frame::data, ast_frame::datalen, LOG_WARNING, alaw_encoder_pvt::outbuf, s, and alaw_encoder_pvt::tail.

    00224 {
    00225   struct alaw_encoder_pvt *tmp = (struct alaw_encoder_pvt *) pvt;
    00226   int x;
    00227   short *s;
    00228   if (tmp->tail + f->datalen/2 >= sizeof(tmp->outbuf))
    00229     {
    00230       ast_log (LOG_WARNING, "Out of buffer space\n");
    00231       return -1;
    00232     }
    00233   s = f->data;
    00234   for (x=0;x<f->datalen/2;x++)
    00235    tmp->outbuf[x+tmp->tail] = AST_LIN2A(s[x]);
    00236   tmp->tail += f->datalen/2;
    00237   return 0;
    00238 }

static struct ast_frame* lintoalaw_frameout | ( | struct ast_translator_pvt * | pvt | ) | `[static, read]`

LinToalaw_FrameOut Convert a buffer of raw 16-bit signed linear PCM to a buffer of 4-bit alaw packed two to a byte (Big Endian).

Results: Foo

Side effects: Leftover inbuf data gets packed, tail gets updated.

Definition at line 252 of file codec_alaw.c.

References AST_FORMAT_ALAW, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_frame::data, ast_frame::datalen, alaw_encoder_pvt::f, ast_frame::frametype, ast_frame::mallocd, ast_frame::offset, alaw_encoder_pvt::outbuf, ast_frame::samples, ast_frame::src, ast_frame::subclass, and alaw_encoder_pvt::tail.

    00253 {
    00254   struct alaw_encoder_pvt *tmp = (struct alaw_encoder_pvt *) pvt;
    00255
    00256   if (tmp->tail) {
    00257      tmp->f.frametype = AST_FRAME_VOICE;
    00258      tmp->f.subclass = AST_FORMAT_ALAW;
    00259      tmp->f.samples = tmp->tail;
    00260      tmp->f.mallocd = 0;
    00261      tmp->f.offset = AST_FRIENDLY_OFFSET;
    00262      tmp->f.src = __PRETTY_FUNCTION__;
    00263      tmp->f.data = tmp->outbuf;
    00264      tmp->f.datalen = tmp->tail;
    00265      tmp->tail = 0;
    00266      return &tmp->f;
    00267    } else return NULL;
    00268 }

static struct ast_translator_pvt* lintoalaw_new | ( | void |  | ) | `[static, read]`

LinToalaw_New Create a new instance of alaw_encoder_pvt.

Results: Returns a pointer to the new instance.

Side effects: None.

Definition at line 120 of file codec_alaw.c.

References ast_update_use_count(), malloc, and alaw_encoder_pvt::tail.

    00121 {
    00122   struct alaw_encoder_pvt *tmp;
    00123   tmp = malloc (sizeof (struct alaw_encoder_pvt));
    00124   if (tmp)
    00125     {
    00126      memset(tmp, 0, sizeof(*tmp));
    00127       localusecnt++;
    00128       ast_update_use_count ();
    00129       tmp->tail = 0;
    00130     }
    00131   return (struct ast_translator_pvt *) tmp;
    00132 }

static struct ast_frame* lintoalaw_sample | ( | void |  | ) | `[static, read]`

LinToalaw_Sample.

Definition at line 293 of file codec_alaw.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frame::data, ast_frame::datalen, ast_frame::frametype, ast_frame::mallocd, ast_frame::offset, ast_frame::samples, slin_ulaw_ex, ast_frame::src, and ast_frame::subclass.

    00294 {
    00295   static struct ast_frame f;
    00296   f.frametype = AST_FRAME_VOICE;
    00297   f.subclass = AST_FORMAT_SLINEAR;
    00298   f.datalen = sizeof (slin_ulaw_ex);
    00299   /* Assume 8000 Hz */
    00300   f.samples = sizeof (slin_ulaw_ex) / 2;
    00301   f.mallocd = 0;
    00302   f.offset = 0;
    00303   f.src = __PRETTY_FUNCTION__;
    00304   f.data = slin_ulaw_ex;
    00305   return &f;
    00306 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 397 of file codec_alaw.c.

References ast_register_translator(), ast_unregister_translator(), and parse_config().

    00398 {
    00399   int res;
    00400   parse_config();
    00401   res = ast_register_translator (&alawtolin);
    00402   if (!res)
    00403     res = ast_register_translator (&lintoalaw);
    00404   else
    00405     ast_unregister_translator (&alawtolin);
    00406   return res;
    00407 }

static void parse_config | ( | void |  | ) | `[static]`

Definition at line 358 of file codec_alaw.c.

References ast_config_destroy(), ast_config_load(), ast_true(), ast_variable_browse(), ast_verbose(), cfg, ast_variable::name, ast_variable::next, option_verbose, ast_variable::value, var, and VERBOSE_PREFIX_3.

    00359 {
    00360   struct ast_config *cfg;
    00361   struct ast_variable *var;
    00362
    00363   if ((cfg = ast_config_load("codecs.conf"))) {
    00364     if ((var = ast_variable_browse(cfg, "plc"))) {
    00365       while (var) {
    00366        if (!strcasecmp(var->name, "genericplc")) {
    00367          useplc = ast_true(var->value) ? 1 : 0;
    00368          if (option_verbose > 2)
    00369            ast_verbose(VERBOSE_PREFIX_3 "codec_alaw: %susing generic PLC\n", useplc ? "" : "not ");
    00370        }
    00371        var = var->next;
    00372       }
    00373     }
    00374     ast_config_destroy(cfg);
    00375   }
    00376 }

int reload | ( | void |  | ) |

Reload stuff.

This function is where any reload routines take place. Re-read config files, change signalling, whatever is appropriate on a reload.

Returns:
  The return value is not used.

Definition at line 378 of file codec_alaw.c.

References parse_config().

    00379 {
    00380   parse_config();
    00381   return 0;
    00382 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 384 of file codec_alaw.c.

References ast_mutex_lock(), ast_mutex_unlock(), and ast_unregister_translator().

    00385 {
    00386   int res;
    00387   ast_mutex_lock (&localuser_lock);
    00388   res = ast_unregister_translator (&lintoalaw);
    00389   if (!res)
    00390     res = ast_unregister_translator (&alawtolin);
    00391   if (localusecnt)
    00392     res = -1;
    00393   ast_mutex_unlock (&localuser_lock);
    00394   return res;
    00395 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 418 of file codec_alaw.c.

References STANDARD_USECOUNT.

    00419 {
    00420   int res;
    00421   STANDARD_USECOUNT (res);
    00422   return res;
    00423 }

## Variable Documentation

struct ast_translator alawtolin`[static]`

The complete translator for alawToLin.

Definition at line 330 of file codec_alaw.c.

struct ast_translator lintoalaw`[static]`

The complete translator for LinToalaw.

Definition at line 346 of file codec_alaw.c.

int localusecnt = 0`[static]`

Definition at line 49 of file codec_alaw.c.

char* tdesc = "A-law Coder/Decoder"`[static]`

Definition at line 51 of file codec_alaw.c.

int useplc = 0`[static]`

Definition at line 53 of file codec_alaw.c.