#include "asterisk.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include "asterisk/lock.h"
#include "asterisk/translate.h"
#include "asterisk/config.h"
#include "asterisk/options.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "../formats/msgsm.h"
#include "slin_gsm_ex.h"
#include "gsm_slin_ex.h"
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 | |
AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT,"GSM Coder/Decoder",.load=load_module,.unload=unload_module,.reload=reload,) | |
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 struct ast_frame * | gsmtolin_sample (void) |
static int | lintogsm_framein (struct ast_trans_pvt *pvt, struct ast_frame *f) |
store samples into working buffer for later decode | |
static struct ast_frame * | lintogsm_frameout (struct ast_trans_pvt *pvt) |
encode and produce a frame | |
static struct ast_frame * | lintogsm_sample (void) |
static int | load_module (void) |
static void | parse_config (void) |
static int | reload (void) |
standard module glue | |
static int | unload_module (void) |
Variables | |
static struct ast_translator | gsmtolin |
static struct ast_translator | lintogsm |
Definition in file codec_gsm.c.
#define BUFFER_SAMPLES 8000 |
Definition at line 65 of file codec_gsm.c.
#define GSM_FRAME_LEN 33 |
Definition at line 67 of file codec_gsm.c.
Referenced by gsmtolin_framein(), and lintogsm_frameout().
#define GSM_SAMPLES 160 |
Definition at line 66 of file codec_gsm.c.
Referenced by gsm_read(), gsm_seek(), gsm_tell(), gsmtolin_framein(), gsmtolin_sample(), lintogsm_frameout(), and wav_read().
#define MSGSM_FRAME_LEN 65 |
AST_MODULE_INFO | ( | ASTERISK_GPL_KEY | , | |
AST_MODFLAG_DEFAULT | , | |||
"GSM Coder/Decoder" | , | |||
. | load = load_module , |
|||
. | unload = unload_module , |
|||
. | reload = reload | |||
) |
static void gsm_destroy_stuff | ( | struct ast_trans_pvt * | pvt | ) | [static] |
Definition at line 203 of file codec_gsm.c.
References gsm_translator_pvt::gsm, and ast_trans_pvt::pvt.
00204 { 00205 struct gsm_translator_pvt *tmp = pvt->pvt; 00206 if (tmp->gsm) 00207 gsm_destroy(tmp->gsm); 00208 }
static int gsm_new | ( | struct ast_trans_pvt * | pvt | ) | [static] |
Definition at line 75 of file codec_gsm.c.
References gsm_translator_pvt::gsm, and ast_trans_pvt::pvt.
00076 { 00077 struct gsm_translator_pvt *tmp = pvt->pvt; 00078 00079 return (tmp->gsm = gsm_create()) ? 0 : -1; 00080 }
static int gsmtolin_framein | ( | struct ast_trans_pvt * | pvt, | |
struct ast_frame * | f | |||
) | [static] |
decode and store in outbuf.
Definition at line 113 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, len, LOG_WARNING, MSGSM_FRAME_LEN, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.
00114 { 00115 struct gsm_translator_pvt *tmp = pvt->pvt; 00116 int x; 00117 int16_t *dst = (int16_t *)pvt->outbuf; 00118 /* guess format from frame len. 65 for MSGSM, 33 for regular GSM */ 00119 int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ? 00120 MSGSM_FRAME_LEN : GSM_FRAME_LEN; 00121 00122 for (x=0; x < f->datalen; x += flen) { 00123 unsigned char data[2 * GSM_FRAME_LEN]; 00124 unsigned char *src; 00125 int len; 00126 if (flen == MSGSM_FRAME_LEN) { 00127 len = 2*GSM_SAMPLES; 00128 src = data; 00129 /* Translate MSGSM format to Real GSM format before feeding in */ 00130 /* XXX what's the point here! we should just work 00131 * on the full format. 00132 */ 00133 conv65(f->data + x, data); 00134 } else { 00135 len = GSM_SAMPLES; 00136 src = f->data + x; 00137 } 00138 /* XXX maybe we don't need to check */ 00139 if (pvt->samples + len > BUFFER_SAMPLES) { 00140 ast_log(LOG_WARNING, "Out of buffer space\n"); 00141 return -1; 00142 } 00143 if (gsm_decode(tmp->gsm, src, dst + pvt->samples)) { 00144 ast_log(LOG_WARNING, "Invalid GSM data (1)\n"); 00145 return -1; 00146 } 00147 pvt->samples += GSM_SAMPLES; 00148 pvt->datalen += 2 * GSM_SAMPLES; 00149 if (flen == MSGSM_FRAME_LEN) { 00150 if (gsm_decode(tmp->gsm, data + GSM_FRAME_LEN, dst + pvt->samples)) { 00151 ast_log(LOG_WARNING, "Invalid GSM data (2)\n"); 00152 return -1; 00153 } 00154 pvt->samples += GSM_SAMPLES; 00155 pvt->datalen += 2 * GSM_SAMPLES; 00156 } 00157 } 00158 return 0; 00159 }
static struct ast_frame* gsmtolin_sample | ( | void | ) | [static, read] |
Definition at line 97 of file codec_gsm.c.
References AST_FORMAT_GSM, AST_FRAME_VOICE, ast_frame::data, ast_frame::datalen, ast_frame::frametype, GSM_SAMPLES, gsm_slin_ex, ast_frame::mallocd, ast_frame::offset, ast_frame::samples, ast_frame::src, and ast_frame::subclass.
00098 { 00099 static struct ast_frame f; 00100 f.frametype = AST_FRAME_VOICE; 00101 f.subclass = AST_FORMAT_GSM; 00102 f.datalen = sizeof(gsm_slin_ex); 00103 /* All frames are 20 ms long */ 00104 f.samples = GSM_SAMPLES; 00105 f.mallocd = 0; 00106 f.offset = 0; 00107 f.src = __PRETTY_FUNCTION__; 00108 f.data = gsm_slin_ex; 00109 return &f; 00110 }
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 162 of file codec_gsm.c.
References ast_log(), gsm_translator_pvt::buf, BUFFER_SAMPLES, ast_frame::data, ast_frame::datalen, LOG_WARNING, ast_trans_pvt::pvt, ast_frame::samples, and ast_trans_pvt::samples.
00163 { 00164 struct gsm_translator_pvt *tmp = pvt->pvt; 00165 00166 /* XXX We should look at how old the rest of our stream is, and if it 00167 is too old, then we should overwrite it entirely, otherwise we can 00168 get artifacts of earlier talk that do not belong */ 00169 if (pvt->samples + f->samples > BUFFER_SAMPLES) { 00170 ast_log(LOG_WARNING, "Out of buffer space\n"); 00171 return -1; 00172 } 00173 memcpy(tmp->buf + pvt->samples, f->data, f->datalen); 00174 pvt->samples += f->samples; 00175 return 0; 00176 }
static struct ast_frame* lintogsm_frameout | ( | struct ast_trans_pvt * | pvt | ) | [static, read] |
encode and produce a frame
Definition at line 179 of file codec_gsm.c.
References ast_trans_frameout(), gsm_translator_pvt::buf, gsm_translator_pvt::gsm, GSM_FRAME_LEN, GSM_SAMPLES, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.
00180 { 00181 struct gsm_translator_pvt *tmp = pvt->pvt; 00182 int datalen = 0; 00183 int samples = 0; 00184 00185 /* We can't work on anything less than a frame in size */ 00186 if (pvt->samples < GSM_SAMPLES) 00187 return NULL; 00188 while (pvt->samples >= GSM_SAMPLES) { 00189 /* Encode a frame of data */ 00190 gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf + datalen); 00191 datalen += GSM_FRAME_LEN; 00192 samples += GSM_SAMPLES; 00193 pvt->samples -= GSM_SAMPLES; 00194 } 00195 00196 /* Move the data at the end of the buffer to the front */ 00197 if (pvt->samples) 00198 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2); 00199 00200 return ast_trans_frameout(pvt, datalen, samples); 00201 }
static struct ast_frame* lintogsm_sample | ( | void | ) | [static, read] |
Definition at line 82 of file codec_gsm.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_gsm_ex, ast_frame::src, and ast_frame::subclass.
00083 { 00084 static struct ast_frame f; 00085 f.frametype = AST_FRAME_VOICE; 00086 f.subclass = AST_FORMAT_SLINEAR; 00087 f.datalen = sizeof(slin_gsm_ex); 00088 /* Assume 8000 Hz */ 00089 f.samples = sizeof(slin_gsm_ex)/2; 00090 f.mallocd = 0; 00091 f.offset = 0; 00092 f.src = __PRETTY_FUNCTION__; 00093 f.data = slin_gsm_ex; 00094 return &f; 00095 }
static int load_module | ( | void | ) | [static] |
Definition at line 272 of file codec_gsm.c.
References ast_register_translator, ast_unregister_translator(), and parse_config().
00273 { 00274 int res; 00275 00276 parse_config(); 00277 res = ast_register_translator(&gsmtolin); 00278 if (!res) 00279 res=ast_register_translator(&lintogsm); 00280 else 00281 ast_unregister_translator(&gsmtolin); 00282 00283 return res; 00284 }
static void parse_config | ( | void | ) | [static] |
Definition at line 238 of file codec_gsm.c.
References ast_config_destroy(), ast_config_load(), ast_true(), ast_variable_browse(), ast_verbose(), ast_variable::name, ast_variable::next, option_verbose, ast_translator::useplc, ast_variable::value, var, and VERBOSE_PREFIX_3.
00239 { 00240 struct ast_variable *var; 00241 struct ast_config *cfg = ast_config_load("codecs.conf"); 00242 if (!cfg) 00243 return; 00244 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) { 00245 if (!strcasecmp(var->name, "genericplc")) { 00246 gsmtolin.useplc = ast_true(var->value) ? 1 : 0; 00247 if (option_verbose > 2) 00248 ast_verbose(VERBOSE_PREFIX_3 "codec_gsm: %susing generic PLC\n", gsmtolin.useplc ? "" : "not "); 00249 } 00250 } 00251 ast_config_destroy(cfg); 00252 }
static int reload | ( | void | ) | [static] |
standard module glue
Definition at line 255 of file codec_gsm.c.
References parse_config().
00256 { 00257 parse_config(); 00258 return 0; 00259 }
static int unload_module | ( | void | ) | [static] |
Definition at line 261 of file codec_gsm.c.
References ast_unregister_translator().
00262 { 00263 int res; 00264 00265 res = ast_unregister_translator(&lintogsm); 00266 if (!res) 00267 res = ast_unregister_translator(&gsmtolin); 00268 00269 return res; 00270 }
struct ast_translator gsmtolin [static] |
Definition at line 210 of file codec_gsm.c.
struct ast_translator lintogsm [static] |
Definition at line 224 of file codec_gsm.c.