Wed Jan 8 2020 09:50:10

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"

Go to the source code of this file.

Data Structures

struct  gsm_translator_pvt
 

Macros

#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. More...
 
static int lintogsm_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 store samples into working buffer for later decode More...
 
static struct ast_framelintogsm_frameout (struct ast_trans_pvt *pvt)
 encode and produce a frame More...
 
static int load_module (void)
 
static int reload (void)
 standard module glue More...
 
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 = "ac1f6a56484a8820659555499174e588" , .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.

Macro Definition Documentation

#define BUFFER_SAMPLES   8000

Definition at line 51 of file codec_gsm.c.

Referenced by gsmtolin_framein(), and lintogsm_framein().

#define GSM_FRAME_LEN   33

Definition at line 53 of file codec_gsm.c.

Referenced by gsmtolin_framein(), and lintogsm_frameout().

#define GSM_SAMPLES   160

Definition at line 52 of file codec_gsm.c.

Referenced by gsm_sample(), gsmtolin_framein(), and lintogsm_frameout().

#define MSGSM_FRAME_LEN   65

Definition at line 54 of file codec_gsm.c.

Referenced by gsmtolin_framein().

Function Documentation

static void __reg_module ( void  )
static

Definition at line 231 of file codec_gsm.c.

static void __unreg_module ( void  )
static

Definition at line 231 of file codec_gsm.c.

static void gsm_destroy_stuff ( struct ast_trans_pvt pvt)
static

Definition at line 163 of file codec_gsm.c.

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

164 {
165  struct gsm_translator_pvt *tmp = pvt->pvt;
166  if (tmp->gsm)
167  gsm_destroy(tmp->gsm);
168 }
void * pvt
Definition: translate.h:141
static int gsm_new ( struct ast_trans_pvt pvt)
static

Definition at line 65 of file codec_gsm.c.

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

66 {
67  struct gsm_translator_pvt *tmp = pvt->pvt;
68 
69  return (tmp->gsm = gsm_create()) ? 0 : -1;
70 }
void * pvt
Definition: translate.h:141
static int gsmtolin_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
)
static

decode and store in outbuf.

Definition at line 73 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.

74 {
75  struct gsm_translator_pvt *tmp = pvt->pvt;
76  int x;
77  int16_t *dst = pvt->outbuf.i16;
78  /* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
79  int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
81 
82  for (x=0; x < f->datalen; x += flen) {
83  unsigned char data[2 * GSM_FRAME_LEN];
84  unsigned char *src;
85  int len;
86  if (flen == MSGSM_FRAME_LEN) {
87  len = 2*GSM_SAMPLES;
88  src = data;
89  /* Translate MSGSM format to Real GSM format before feeding in */
90  /* XXX what's the point here! we should just work
91  * on the full format.
92  */
93  conv65(f->data.ptr + x, data);
94  } else {
95  len = GSM_SAMPLES;
96  src = f->data.ptr + x;
97  }
98  /* XXX maybe we don't need to check */
99  if (pvt->samples + len > BUFFER_SAMPLES) {
100  ast_log(LOG_WARNING, "Out of buffer space\n");
101  return -1;
102  }
103  if (gsm_decode(tmp->gsm, src, dst + pvt->samples)) {
104  ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
105  return -1;
106  }
107  pvt->samples += GSM_SAMPLES;
108  pvt->datalen += 2 * GSM_SAMPLES;
109  if (flen == MSGSM_FRAME_LEN) {
110  if (gsm_decode(tmp->gsm, data + GSM_FRAME_LEN, dst + pvt->samples)) {
111  ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
112  return -1;
113  }
114  pvt->samples += GSM_SAMPLES;
115  pvt->datalen += 2 * GSM_SAMPLES;
116  }
117  }
118  return 0;
119 }
int datalen
actual space used in outbuf
Definition: translate.h:140
union ast_trans_pvt::@213 outbuf
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
#define GSM_FRAME_LEN
Definition: codec_gsm.c:53
void * pvt
Definition: translate.h:141
int16_t * i16
Definition: translate.h:145
#define MSGSM_FRAME_LEN
Definition: codec_gsm.c:54
int datalen
Definition: frame.h:148
static void conv65(wav_byte *c, gsm_byte *d)
Definition: msgsm.h:455
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define GSM_SAMPLES
Definition: codec_gsm.c:52
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define BUFFER_SAMPLES
Definition: codec_gsm.c:51
union ast_frame::@172 data
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 122 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_trans_pvt::samples, and ast_frame::samples.

123 {
124  struct gsm_translator_pvt *tmp = pvt->pvt;
125 
126  /* XXX We should look at how old the rest of our stream is, and if it
127  is too old, then we should overwrite it entirely, otherwise we can
128  get artifacts of earlier talk that do not belong */
129  if (pvt->samples + f->samples > BUFFER_SAMPLES) {
130  ast_log(LOG_WARNING, "Out of buffer space\n");
131  return -1;
132  }
133  memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
134  pvt->samples += f->samples;
135  return 0;
136 }
int16_t buf[BUFFER_SAMPLES]
Definition: codec_gsm.c:62
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
void * pvt
Definition: translate.h:141
int datalen
Definition: frame.h:148
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define BUFFER_SAMPLES
Definition: codec_gsm.c:51
union ast_frame::@172 data
int samples
Definition: frame.h:150
static struct ast_frame* lintogsm_frameout ( struct ast_trans_pvt pvt)
static

encode and produce a frame

Definition at line 139 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.

140 {
141  struct gsm_translator_pvt *tmp = pvt->pvt;
142  int datalen = 0;
143  int samples = 0;
144 
145  /* We can't work on anything less than a frame in size */
146  if (pvt->samples < GSM_SAMPLES)
147  return NULL;
148  while (pvt->samples >= GSM_SAMPLES) {
149  /* Encode a frame of data */
150  gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c + datalen);
151  datalen += GSM_FRAME_LEN;
152  samples += GSM_SAMPLES;
153  pvt->samples -= GSM_SAMPLES;
154  }
155 
156  /* Move the data at the end of the buffer to the front */
157  if (pvt->samples)
158  memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
159 
160  return ast_trans_frameout(pvt, datalen, samples);
161 }
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
Definition: translate.c:235
union ast_trans_pvt::@213 outbuf
int16_t buf[BUFFER_SAMPLES]
Definition: codec_gsm.c:62
#define GSM_FRAME_LEN
Definition: codec_gsm.c:53
void * pvt
Definition: translate.h:141
#define GSM_SAMPLES
Definition: codec_gsm.c:52
unsigned char gsm_byte
Definition: msgsm.h:6
static int load_module ( void  )
static

Definition at line 213 of file codec_gsm.c.

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

214 {
215  int res;
216 
218  if (!res)
220  else
222  if (res)
225 }
static struct ast_translator lintogsm
Definition: codec_gsm.c:183
static struct ast_translator gsmtolin
Definition: codec_gsm.c:170
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:170
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:942
static int reload ( void  )
static

standard module glue

Definition at line 197 of file codec_gsm.c.

References AST_MODULE_LOAD_SUCCESS.

198 {
200 }
static int unload_module ( void  )
static

Definition at line 202 of file codec_gsm.c.

References ast_unregister_translator().

203 {
204  int res;
205 
207  if (!res)
209 
210  return res;
211 }
static struct ast_translator lintogsm
Definition: codec_gsm.c:183
static struct ast_translator gsmtolin
Definition: codec_gsm.c:170
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:942

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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .reload = reload, }
static

Definition at line 231 of file codec_gsm.c.

Definition at line 231 of file codec_gsm.c.

struct ast_translator gsmtolin
static

Definition at line 170 of file codec_gsm.c.

struct ast_translator lintogsm
static

Definition at line 183 of file codec_gsm.c.