Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:44 2009

## Asterisk developer's documentation

# codec_ulaw.c File Reference

codec_ulaw.c - translate between signed linear and ulaw 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/ulaw.h"`
`#include "slin_ulaw_ex.h"`
`#include "ulaw_slin_ex.h"`

Include dependency graph for codec_ulaw.c:

Go to the source code of this file.

## Data Structures

struct | ulaw_decoder_pvt
struct | ulaw_encoder_pvt

## Defines

#define | BUFFER_SIZE 8096

## Functions

| AST_MUTEX_DEFINE_STATIC (localuser_lock)
char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
static int | lintoulaw_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
static struct ast_frame * | lintoulaw_frameout (struct ast_translator_pvt *pvt)
static struct ast_translator_pvt * | lintoulaw_new (void)
static struct ast_frame * | lintoulaw_sample (void)
int | load_module (void)
| Initialize the module.
static void | parse_config (void)
int | reload (void)
| Reload stuff.
static void | ulaw_destroy (struct ast_translator_pvt *pvt)
static int | ulawtolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
static struct ast_frame * | ulawtolin_frameout (struct ast_translator_pvt *pvt)
static struct ast_translator_pvt * | ulawtolin_new (void)
static struct ast_frame * | ulawtolin_sample (void)
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static struct ast_translator | lintoulaw
static int | localusecnt = 0
static char * | tdesc = "Mu-law Coder/Decoder"
static struct ast_translator | ulawtolin
static int | useplc = 0

## Detailed Description

codec_ulaw.c - translate between signed linear and ulaw

Definition in file codec_ulaw.c.

## Define Documentation

#define BUFFER_SIZE 8096

Definition at line 46 of file codec_ulaw.c.

## Function Documentation

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 428 of file codec_ulaw.c.

    00429 {
    00430   return tdesc;
    00431 }

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 442 of file codec_ulaw.c.

References ASTERISK_GPL_KEY.

    00443 {
    00444   return ASTERISK_GPL_KEY;
    00445 }

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

Definition at line 229 of file codec_ulaw.c.

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

    00230 {
    00231   struct ulaw_encoder_pvt *tmp = (struct ulaw_encoder_pvt *) pvt;
    00232   int x;
    00233   short *s;
    00234   if (tmp->tail + f->datalen/2 >= sizeof(tmp->outbuf))
    00235     {
    00236       ast_log (LOG_WARNING, "Out of buffer space\n");
    00237       return -1;
    00238     }
    00239   s = f->data;
    00240   for (x=0;x<f->datalen/2;x++)
    00241    tmp->outbuf[x+tmp->tail] = AST_LIN2MU(s[x]);
    00242   tmp->tail += f->datalen/2;
    00243   return 0;
    00244 }

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

Definition at line 259 of file codec_ulaw.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.

    00260 {
    00261   struct ulaw_encoder_pvt *tmp = (struct ulaw_encoder_pvt *) pvt;
    00262
    00263   if (tmp->tail) {
    00264      tmp->f.frametype = AST_FRAME_VOICE;
    00265      tmp->f.subclass = AST_FORMAT_ULAW;
    00266      tmp->f.samples = tmp->tail;
    00267      tmp->f.mallocd = 0;
    00268      tmp->f.offset = AST_FRIENDLY_OFFSET;
    00269      tmp->f.src = __PRETTY_FUNCTION__;
    00270      tmp->f.data = tmp->outbuf;
    00271      tmp->f.datalen = tmp->tail;
    00272      tmp->tail = 0;
    00273      return &tmp->f;
    00274    } else return NULL;
    00275 }

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

Definition at line 124 of file codec_ulaw.c.

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

    00125 {
    00126   struct ulaw_encoder_pvt *tmp;
    00127   tmp = malloc (sizeof (struct ulaw_encoder_pvt));
    00128   if (tmp)
    00129     {
    00130      memset(tmp, 0, sizeof(*tmp));
    00131       localusecnt++;
    00132       ast_update_use_count ();
    00133       tmp->tail = 0;
    00134     }
    00135   return (struct ast_translator_pvt *) tmp;
    00136 }

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

Definition at line 302 of file codec_ulaw.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.

    00303 {
    00304   static struct ast_frame f;
    00305   f.frametype = AST_FRAME_VOICE;
    00306   f.subclass = AST_FORMAT_SLINEAR;
    00307   f.datalen = sizeof (slin_ulaw_ex);
    00308   /* Assume 8000 Hz */
    00309   f.samples = sizeof (slin_ulaw_ex) / 2;
    00310   f.mallocd = 0;
    00311   f.offset = 0;
    00312   f.src = __PRETTY_FUNCTION__;
    00313   f.data = slin_ulaw_ex;
    00314   return &f;
    00315 }

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 411 of file codec_ulaw.c.

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

    00412 {
    00413   int res;
    00414   parse_config();
    00415   res = ast_register_translator (&ulawtolin);
    00416   if (!res)
    00417     res = ast_register_translator (&lintoulaw);
    00418   else
    00419     ast_unregister_translator (&ulawtolin);
    00420   return res;
    00421 }

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

Definition at line 369 of file codec_ulaw.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.

    00370 {
    00371   struct ast_config *cfg;
    00372   struct ast_variable *var;
    00373   if ((cfg = ast_config_load("codecs.conf"))) {
    00374     if ((var = ast_variable_browse(cfg, "plc"))) {
    00375       while (var) {
    00376    if (!strcasecmp(var->name, "genericplc")) {
    00377      useplc = ast_true(var->value) ? 1 : 0;
    00378      if (option_verbose > 2)
    00379        ast_verbose(VERBOSE_PREFIX_3 "codec_ulaw: %susing generic PLC\n", useplc ? "" : "not ");
    00380    }
    00381    var = var->next;
    00382       }
    00383     }
    00384     ast_config_destroy(cfg);
    00385   }
    00386 }

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 389 of file codec_ulaw.c.

References parse_config().

    00390 {
    00391   parse_config();
    00392   return 0;
    00393 }

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

Definition at line 329 of file codec_ulaw.c.

References ast_update_use_count(), and free.

    00330 {
    00331   free (pvt);
    00332   localusecnt--;
    00333   ast_update_use_count ();
    00334 }

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

Definition at line 151 of file codec_ulaw.c.

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

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

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

Definition at line 198 of file codec_ulaw.c.

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

    00199 {
    00200   struct ulaw_decoder_pvt *tmp = (struct ulaw_decoder_pvt *) pvt;
    00201
    00202   if (!tmp->tail)
    00203     return NULL;
    00204
    00205   tmp->f.frametype = AST_FRAME_VOICE;
    00206   tmp->f.subclass = AST_FORMAT_SLINEAR;
    00207   tmp->f.datalen = tmp->tail *2;
    00208   tmp->f.samples = tmp->tail;
    00209   tmp->f.mallocd = 0;
    00210   tmp->f.offset = AST_FRIENDLY_OFFSET;
    00211   tmp->f.src = __PRETTY_FUNCTION__;
    00212   tmp->f.data = tmp->outbuf;
    00213   tmp->tail = 0;
    00214   return &tmp->f;
    00215 }

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

Definition at line 97 of file codec_ulaw.c.

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

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

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

Definition at line 283 of file codec_ulaw.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.

    00284 {
    00285   static struct ast_frame f;
    00286   f.frametype = AST_FRAME_VOICE;
    00287   f.subclass = AST_FORMAT_ULAW;
    00288   f.datalen = sizeof (ulaw_slin_ex);
    00289   f.samples = sizeof(ulaw_slin_ex);
    00290   f.mallocd = 0;
    00291   f.offset = 0;
    00292   f.src = __PRETTY_FUNCTION__;
    00293   f.data = ulaw_slin_ex;
    00294   return &f;
    00295 }

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 397 of file codec_ulaw.c.

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

    00398 {
    00399   int res;
    00400   ast_mutex_lock (&localuser_lock);
    00401   res = ast_unregister_translator (&lintoulaw);
    00402   if (!res)
    00403     res = ast_unregister_translator (&ulawtolin);
    00404   if (localusecnt)
    00405     res = -1;
    00406   ast_mutex_unlock (&localuser_lock);
    00407   return res;
    00408 }

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 434 of file codec_ulaw.c.

References STANDARD_USECOUNT.

    00435 {
    00436   int res;
    00437   STANDARD_USECOUNT (res);
    00438   return res;
    00439 }

## Variable Documentation

struct ast_translator lintoulaw`[static]`

Definition at line 356 of file codec_ulaw.c.

int localusecnt = 0`[static]`

Definition at line 49 of file codec_ulaw.c.

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

Definition at line 51 of file codec_ulaw.c.

struct ast_translator ulawtolin`[static]`

Definition at line 340 of file codec_ulaw.c.

int useplc = 0`[static]`

Definition at line 53 of file codec_ulaw.c.