Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:42 2009

## Asterisk developer's documentation

# codec_a_mu.c File Reference

codec_a_mu.c - translate between alaw and ulaw directly 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/translate.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/alaw.h"`
`#include "asterisk/ulaw.h"`
`#include "ulaw_slin_ex.h"`

Include dependency graph for codec_a_mu.c:

Go to the source code of this file.

## Data Structures

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

## Defines

#define | BUFFER_SIZE 8096

## Functions

static void | alaw_destroy (struct ast_translator_pvt *pvt)
static int | alawtoulaw_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
static struct ast_frame * | alawtoulaw_frameout (struct ast_translator_pvt *pvt)
static struct ast_translator_pvt * | alawtoulaw_new (void)
static struct ast_frame * | alawtoulaw_sample (void)
| AST_MUTEX_DEFINE_STATIC (localuser_lock)
char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
static int | ulawtoalaw_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
static struct ast_frame * | ulawtoalaw_frameout (struct ast_translator_pvt *pvt)
static struct ast_translator_pvt * | ulawtoalaw_new (void)
static struct ast_frame * | ulawtoalaw_sample (void)
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static unsigned char | a2mu [256]
static struct ast_translator | alawtoulaw
static int | localusecnt = 0
static unsigned char | mu2a [256]
static char * | tdesc = "A-law and Mulaw direct Coder/Decoder"
static struct ast_translator | ulawtoalaw

## Detailed Description

codec_a_mu.c - translate between alaw and ulaw directly

Definition in file codec_a_mu.c.

## Define Documentation

#define BUFFER_SIZE 8096

Definition at line 45 of file codec_a_mu.c.

## Function Documentation

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

Definition at line 251 of file codec_a_mu.c.

References ast_update_use_count(), and free.

    00252 {
    00253   free (pvt);
    00254   localusecnt--;
    00255   ast_update_use_count ();
    00256 }

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

Definition at line 114 of file codec_a_mu.c.

References ast_log(), ast_frame::data, ast_frame::datalen, LOG_WARNING, ulaw_encoder_pvt::outbuf, and ulaw_encoder_pvt::tail.

    00115 {
    00116   struct ulaw_encoder_pvt *tmp = (struct ulaw_encoder_pvt *) pvt;
    00117   int x;
    00118   unsigned char *b;
    00119
    00120   if ((tmp->tail + f->datalen)> sizeof(tmp->outbuf)) {
    00121    ast_log(LOG_WARNING, "Out of buffer space\n");
    00122    return -1;
    00123   }
    00124
    00125   /* Reset ssindex and signal to frame's specified values */
    00126   b = f->data;
    00127   for (x=0;x<f->datalen;x++)
    00128    tmp->outbuf[tmp->tail + x] = a2mu[b[x]];
    00129
    00130   tmp->tail += f->datalen;
    00131   return 0;
    00132 }

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

Definition at line 135 of file codec_a_mu.c.

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

    00136 {
    00137   struct ulaw_encoder_pvt *tmp = (struct ulaw_encoder_pvt *) pvt;
    00138
    00139   if (!tmp->tail)
    00140     return NULL;
    00141
    00142   tmp->f.frametype = AST_FRAME_VOICE;
    00143   tmp->f.subclass = AST_FORMAT_ULAW;
    00144   tmp->f.datalen = tmp->tail;
    00145   tmp->f.samples = tmp->tail;
    00146   tmp->f.mallocd = 0;
    00147   tmp->f.offset = AST_FRIENDLY_OFFSET;
    00148   tmp->f.src = __PRETTY_FUNCTION__;
    00149   tmp->f.data = tmp->outbuf;
    00150   tmp->tail = 0;
    00151   return &tmp->f;
    00152 }

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

Definition at line 84 of file codec_a_mu.c.

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

    00085 {
    00086   struct ulaw_encoder_pvt *tmp;
    00087   tmp = malloc (sizeof (struct ulaw_encoder_pvt));
    00088   if (tmp)
    00089     {
    00090      memset(tmp, 0, sizeof(*tmp));
    00091       tmp->tail = 0;
    00092       localusecnt++;
    00093       ast_update_use_count ();
    00094     }
    00095   return (struct ast_translator_pvt *) tmp;
    00096 }

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

Definition at line 209 of file codec_a_mu.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.

    00210 {
    00211   static struct ast_frame f;
    00212   f.frametype = AST_FRAME_VOICE;
    00213   f.subclass = AST_FORMAT_ALAW;
    00214   f.datalen = sizeof (ulaw_slin_ex);
    00215   f.samples = sizeof(ulaw_slin_ex);
    00216   f.mallocd = 0;
    00217   f.offset = 0;
    00218   f.src = __PRETTY_FUNCTION__;
    00219   f.data = ulaw_slin_ex;
    00220   return &f;
    00221 }

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 326 of file codec_a_mu.c.

    00327 {
    00328   return tdesc;
    00329 }

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 340 of file codec_a_mu.c.

References ASTERISK_GPL_KEY.

    00341 {
    00342   return ASTERISK_GPL_KEY;
    00343 }

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 305 of file codec_a_mu.c.

References AST_ALAW, AST_LIN2A, AST_LIN2MU, AST_MULAW, ast_register_translator(), and ast_unregister_translator().

    00306 {
    00307   int res;
    00308   int x;
    00309   for (x=0;x<256;x++) {
    00310    mu2a[x] = AST_LIN2A(AST_MULAW(x));
    00311    a2mu[x] = AST_LIN2MU(AST_ALAW(x));
    00312   }
    00313   res = ast_register_translator (&alawtoulaw);
    00314   if (!res)
    00315     res = ast_register_translator (&ulawtoalaw);
    00316   else
    00317     ast_unregister_translator (&alawtoulaw);
    00318   return res;
    00319 }

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

Definition at line 155 of file codec_a_mu.c.

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

    00156 {
    00157   struct alaw_encoder_pvt *tmp = (struct alaw_encoder_pvt *) pvt;
    00158   int x;
    00159   unsigned char *s;
    00160   if (tmp->tail + f->datalen >= sizeof(tmp->outbuf))
    00161     {
    00162       ast_log (LOG_WARNING, "Out of buffer space\n");
    00163       return -1;
    00164     }
    00165   s = f->data;
    00166   for (x=0;x<f->datalen;x++)
    00167    tmp->outbuf[x+tmp->tail] = mu2a[s[x]];
    00168   tmp->tail += f->datalen;
    00169   return 0;
    00170 }

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

Definition at line 185 of file codec_a_mu.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.

    00186 {
    00187   struct alaw_encoder_pvt *tmp = (struct alaw_encoder_pvt *) pvt;
    00188
    00189   if (tmp->tail) {
    00190      tmp->f.frametype = AST_FRAME_VOICE;
    00191      tmp->f.subclass = AST_FORMAT_ALAW;
    00192      tmp->f.samples = tmp->tail;
    00193      tmp->f.mallocd = 0;
    00194      tmp->f.offset = AST_FRIENDLY_OFFSET;
    00195      tmp->f.src = __PRETTY_FUNCTION__;
    00196      tmp->f.data = tmp->outbuf;
    00197      tmp->f.datalen = tmp->tail;
    00198      tmp->tail = 0;
    00199      return &tmp->f;
    00200    } else return NULL;
    00201 }

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

Definition at line 99 of file codec_a_mu.c.

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

    00100 {
    00101   struct alaw_encoder_pvt *tmp;
    00102   tmp = malloc (sizeof (struct alaw_encoder_pvt));
    00103   if (tmp)
    00104     {
    00105      memset(tmp, 0, sizeof(*tmp));
    00106       localusecnt++;
    00107       ast_update_use_count ();
    00108       tmp->tail = 0;
    00109     }
    00110   return (struct ast_translator_pvt *) tmp;
    00111 }

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

Definition at line 224 of file codec_a_mu.c.

References AST_FORMAT_ULAW, 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.

    00225 {
    00226   static struct ast_frame f;
    00227   f.frametype = AST_FRAME_VOICE;
    00228   f.subclass = AST_FORMAT_ULAW;
    00229   f.datalen = sizeof (ulaw_slin_ex);
    00230   f.samples = sizeof(ulaw_slin_ex);
    00231   f.mallocd = 0;
    00232   f.offset = 0;
    00233   f.src = __PRETTY_FUNCTION__;
    00234   f.data = ulaw_slin_ex;
    00235   return &f;
    00236 }

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 291 of file codec_a_mu.c.

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

    00292 {
    00293   int res;
    00294   ast_mutex_lock (&localuser_lock);
    00295   res = ast_unregister_translator (&ulawtoalaw);
    00296   if (!res)
    00297     res = ast_unregister_translator (&alawtoulaw);
    00298   if (localusecnt)
    00299     res = -1;
    00300   ast_mutex_unlock (&localuser_lock);
    00301   return res;
    00302 }

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 332 of file codec_a_mu.c.

References STANDARD_USECOUNT.

    00333 {
    00334   int res;
    00335   STANDARD_USECOUNT (res);
    00336   return res;
    00337 }

## Variable Documentation

unsigned char a2mu[256]`[static]`

Definition at line 53 of file codec_a_mu.c.

struct ast_translator alawtoulaw`[static]`

Definition at line 262 of file codec_a_mu.c.

int localusecnt = 0`[static]`

Definition at line 48 of file codec_a_mu.c.

unsigned char mu2a[256]`[static]`

Definition at line 52 of file codec_a_mu.c.

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

Definition at line 50 of file codec_a_mu.c.

struct ast_translator ulawtoalaw`[static]`

Definition at line 278 of file codec_a_mu.c.