Fri Nov 12 11:58:40 2010

Asterisk developer's documentation


codec_gsm.c File Reference

Translate between signed linear and Global System for Mobile Communications (GSM). More...

#include "asterisk.h"
#include "asterisk/translate.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "gsm.h"
#include "../formats/msgsm.h"
#include "asterisk/slin.h"
#include "ex_gsm.h"
Include dependency graph for codec_gsm.c:

Go to the source code of this file.

Data Structures

struct  gsm_translator_pvt

Defines

#define BUFFER_SAMPLES   8000
#define GSM_FRAME_LEN   33
#define GSM_SAMPLES   160
#define MSGSM_FRAME_LEN   65

Functions

static void __reg_module (void)
static void __unreg_module (void)
static void gsm_destroy_stuff (struct ast_trans_pvt *pvt)
static int gsm_new (struct ast_trans_pvt *pvt)
static int gsmtolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 decode and store in outbuf.
static int lintogsm_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 store samples into working buffer for later decode
static struct ast_framelintogsm_frameout (struct ast_trans_pvt *pvt)
 encode and produce a frame
static int load_module (void)
static int parse_config (int reload)
static int reload (void)
 standard module glue
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "GSM Coder/Decoder" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "0901e4e500243c855563a2d78b0c03e4" , .load = load_module, .unload = unload_module, .reload = reload, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_translator gsmtolin
static struct ast_translator lintogsm

Detailed Description

Translate between signed linear and Global System for Mobile Communications (GSM).

Definition in file codec_gsm.c.


Define Documentation

#define BUFFER_SAMPLES   8000

Definition at line 50 of file codec_gsm.c.

#define GSM_FRAME_LEN   33

Definition at line 52 of file codec_gsm.c.

Referenced by gsmtolin_framein(), and lintogsm_frameout().

#define GSM_SAMPLES   160
#define MSGSM_FRAME_LEN   65

Definition at line 53 of file codec_gsm.c.

Referenced by gsmtolin_framein().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 254 of file codec_gsm.c.

static void __unreg_module ( void   )  [static]

Definition at line 254 of file codec_gsm.c.

static void gsm_destroy_stuff ( struct ast_trans_pvt pvt  )  [static]

Definition at line 162 of file codec_gsm.c.

References gsm_translator_pvt::gsm, and ast_trans_pvt::pvt.

00163 {
00164    struct gsm_translator_pvt *tmp = pvt->pvt;
00165    if (tmp->gsm)
00166       gsm_destroy(tmp->gsm);
00167 }

static int gsm_new ( struct ast_trans_pvt pvt  )  [static]

Definition at line 64 of file codec_gsm.c.

References gsm_translator_pvt::gsm, and ast_trans_pvt::pvt.

00065 {
00066    struct gsm_translator_pvt *tmp = pvt->pvt;
00067    
00068    return (tmp->gsm = gsm_create()) ? 0 : -1;
00069 }

static int gsmtolin_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
) [static]

decode and store in outbuf.

Definition at line 72 of file codec_gsm.c.

References ast_log(), BUFFER_SAMPLES, conv65(), ast_frame::data, ast_trans_pvt::datalen, ast_frame::datalen, gsm_translator_pvt::gsm, GSM_FRAME_LEN, GSM_SAMPLES, ast_trans_pvt::i16, len(), LOG_WARNING, MSGSM_FRAME_LEN, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

00073 {
00074    struct gsm_translator_pvt *tmp = pvt->pvt;
00075    int x;
00076    int16_t *dst = pvt->outbuf.i16;
00077    /* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
00078    int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
00079       MSGSM_FRAME_LEN : GSM_FRAME_LEN;
00080 
00081    for (x=0; x < f->datalen; x += flen) {
00082       unsigned char data[2 * GSM_FRAME_LEN];
00083       unsigned char *src;
00084       int len;
00085       if (flen == MSGSM_FRAME_LEN) {
00086          len = 2*GSM_SAMPLES;
00087          src = data;
00088          /* Translate MSGSM format to Real GSM format before feeding in */
00089          /* XXX what's the point here! we should just work
00090           * on the full format.
00091           */
00092          conv65(f->data.ptr + x, data);
00093       } else {
00094          len = GSM_SAMPLES;
00095          src = f->data.ptr + x;
00096       }
00097       /* XXX maybe we don't need to check */
00098       if (pvt->samples + len > BUFFER_SAMPLES) {   
00099          ast_log(LOG_WARNING, "Out of buffer space\n");
00100          return -1;
00101       }
00102       if (gsm_decode(tmp->gsm, src, dst + pvt->samples)) {
00103          ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
00104          return -1;
00105       }
00106       pvt->samples += GSM_SAMPLES;
00107       pvt->datalen += 2 * GSM_SAMPLES;
00108       if (flen == MSGSM_FRAME_LEN) {
00109          if (gsm_decode(tmp->gsm, data + GSM_FRAME_LEN, dst + pvt->samples)) {
00110             ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
00111             return -1;
00112          }
00113          pvt->samples += GSM_SAMPLES;
00114          pvt->datalen += 2 * GSM_SAMPLES;
00115       }
00116    }
00117    return 0;
00118 }

static int lintogsm_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
) [static]

store samples into working buffer for later decode

Definition at line 121 of file codec_gsm.c.

References ast_log(), gsm_translator_pvt::buf, BUFFER_SAMPLES, ast_frame::data, ast_frame::datalen, LOG_WARNING, ast_frame::ptr, ast_trans_pvt::pvt, ast_frame::samples, and ast_trans_pvt::samples.

00122 {
00123    struct gsm_translator_pvt *tmp = pvt->pvt;
00124 
00125    /* XXX We should look at how old the rest of our stream is, and if it
00126       is too old, then we should overwrite it entirely, otherwise we can
00127       get artifacts of earlier talk that do not belong */
00128    if (pvt->samples + f->samples > BUFFER_SAMPLES) {
00129       ast_log(LOG_WARNING, "Out of buffer space\n");
00130       return -1;
00131    }
00132    memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
00133    pvt->samples += f->samples;
00134    return 0;
00135 }

static struct ast_frame* lintogsm_frameout ( struct ast_trans_pvt pvt  )  [static, read]

encode and produce a frame

Definition at line 138 of file codec_gsm.c.

References ast_trans_frameout(), gsm_translator_pvt::buf, ast_trans_pvt::c, gsm_translator_pvt::gsm, GSM_FRAME_LEN, GSM_SAMPLES, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

00139 {
00140    struct gsm_translator_pvt *tmp = pvt->pvt;
00141    int datalen = 0;
00142    int samples = 0;
00143 
00144    /* We can't work on anything less than a frame in size */
00145    if (pvt->samples < GSM_SAMPLES)
00146       return NULL;
00147    while (pvt->samples >= GSM_SAMPLES) {
00148       /* Encode a frame of data */
00149       gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c + datalen);
00150       datalen += GSM_FRAME_LEN;
00151       samples += GSM_SAMPLES;
00152       pvt->samples -= GSM_SAMPLES;
00153    }
00154 
00155    /* Move the data at the end of the buffer to the front */
00156    if (pvt->samples)
00157       memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
00158 
00159    return ast_trans_frameout(pvt, datalen, samples);
00160 }

static int load_module ( void   )  [static]

Definition at line 234 of file codec_gsm.c.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_register_translator, ast_unregister_translator(), and parse_config().

00235 {
00236    int res;
00237 
00238    if (parse_config(0))
00239       return AST_MODULE_LOAD_DECLINE;
00240    res = ast_register_translator(&gsmtolin);
00241    if (!res) 
00242       res=ast_register_translator(&lintogsm);
00243    else
00244       ast_unregister_translator(&gsmtolin);
00245    if (res) 
00246       return AST_MODULE_LOAD_FAILURE;
00247    return AST_MODULE_LOAD_SUCCESS;
00248 }

static int parse_config ( int  reload  )  [static]

Definition at line 197 of file codec_gsm.c.

References ast_config_destroy(), ast_config_load, ast_true(), ast_variable_browse(), ast_verb, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, ast_variable::name, ast_variable::next, ast_translator::useplc, ast_variable::value, and var.

Referenced by load_module(), and reload().

00198 {
00199    struct ast_variable *var;
00200    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00201    struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
00202    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
00203       return 0;
00204    for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00205           if (!strcasecmp(var->name, "genericplc")) {
00206              gsmtolin.useplc = ast_true(var->value) ? 1 : 0;
00207             ast_verb(3, "codec_gsm: %susing generic PLC\n", gsmtolin.useplc ? "" : "not ");
00208           }
00209    }
00210    ast_config_destroy(cfg);
00211    return 0;
00212 }

static int reload ( void   )  [static]

standard module glue

Definition at line 215 of file codec_gsm.c.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and parse_config().

00216 {
00217    if (parse_config(1)) {
00218       return AST_MODULE_LOAD_DECLINE;
00219    }
00220    return AST_MODULE_LOAD_SUCCESS;
00221 }

static int unload_module ( void   )  [static]

Definition at line 223 of file codec_gsm.c.

References ast_unregister_translator().

00224 {
00225    int res;
00226 
00227    res = ast_unregister_translator(&lintogsm);
00228    if (!res)
00229       res = ast_unregister_translator(&gsmtolin);
00230 
00231    return res;
00232 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "GSM Coder/Decoder" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "0901e4e500243c855563a2d78b0c03e4" , .load = load_module, .unload = unload_module, .reload = reload, } [static]

Definition at line 254 of file codec_gsm.c.

Definition at line 254 of file codec_gsm.c.

struct ast_translator gsmtolin [static]

Definition at line 169 of file codec_gsm.c.

struct ast_translator lintogsm [static]

Definition at line 183 of file codec_gsm.c.


Generated by  doxygen 1.6.2