00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00029
00030 #include <fcntl.h>
00031 #include <netinet/in.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036
00037 #include "asterisk/lock.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/config.h"
00041 #include "asterisk/options.h"
00042 #include "asterisk/translate.h"
00043 #include "asterisk/channel.h"
00044 #include "asterisk/ulaw.h"
00045 #include "asterisk/utils.h"
00046
00047 #define BUFFER_SAMPLES 8096
00048
00049
00050
00051 #include "slin_ulaw_ex.h"
00052 #include "ulaw_slin_ex.h"
00053
00054
00055 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00056 {
00057 int i = f->samples;
00058 unsigned char *src = f->data;
00059 int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
00060
00061 pvt->samples += i;
00062 pvt->datalen += i * 2;
00063
00064
00065 while (i--)
00066 *dst++ = AST_MULAW(*src++);
00067
00068 return 0;
00069 }
00070
00071
00072 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00073 {
00074 int i = f->samples;
00075 char *dst = pvt->outbuf + pvt->samples;
00076 int16_t *src = f->data;
00077
00078 pvt->samples += i;
00079 pvt->datalen += i;
00080
00081 while (i--)
00082 *dst++ = AST_LIN2MU(*src++);
00083
00084 return 0;
00085 }
00086
00087
00088 static struct ast_frame *ulawtolin_sample(void)
00089 {
00090 static struct ast_frame f;
00091 f.frametype = AST_FRAME_VOICE;
00092 f.subclass = AST_FORMAT_ULAW;
00093 f.datalen = sizeof(ulaw_slin_ex);
00094 f.samples = sizeof(ulaw_slin_ex);
00095 f.mallocd = 0;
00096 f.offset = 0;
00097 f.src = __PRETTY_FUNCTION__;
00098 f.data = ulaw_slin_ex;
00099 return &f;
00100 }
00101
00102
00103
00104
00105
00106 static struct ast_frame *lintoulaw_sample(void)
00107 {
00108 static struct ast_frame f;
00109 f.frametype = AST_FRAME_VOICE;
00110 f.subclass = AST_FORMAT_SLINEAR;
00111 f.datalen = sizeof(slin_ulaw_ex);
00112
00113 f.samples = sizeof(slin_ulaw_ex) / 2;
00114 f.mallocd = 0;
00115 f.offset = 0;
00116 f.src = __PRETTY_FUNCTION__;
00117 f.data = slin_ulaw_ex;
00118 return &f;
00119 }
00120
00121
00122
00123
00124
00125 static struct ast_translator ulawtolin = {
00126 .name = "ulawtolin",
00127 .srcfmt = AST_FORMAT_ULAW,
00128 .dstfmt = AST_FORMAT_SLINEAR,
00129 .framein = ulawtolin_framein,
00130 .sample = ulawtolin_sample,
00131 .buffer_samples = BUFFER_SAMPLES,
00132 .buf_size = BUFFER_SAMPLES * 2,
00133 .plc_samples = 160,
00134 };
00135
00136
00137
00138
00139
00140 static struct ast_translator lintoulaw = {
00141 .name = "lintoulaw",
00142 .srcfmt = AST_FORMAT_SLINEAR,
00143 .dstfmt = AST_FORMAT_ULAW,
00144 .framein = lintoulaw_framein,
00145 .sample = lintoulaw_sample,
00146 .buf_size = BUFFER_SAMPLES,
00147 .buffer_samples = BUFFER_SAMPLES,
00148 };
00149
00150 static void parse_config(void)
00151 {
00152 struct ast_variable *var;
00153 struct ast_config *cfg = ast_config_load("codecs.conf");
00154 if (!cfg)
00155 return;
00156 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00157 if (!strcasecmp(var->name, "genericplc")) {
00158 ulawtolin.useplc = ast_true(var->value) ? 1 : 0;
00159 if (option_verbose > 2)
00160 ast_verbose(VERBOSE_PREFIX_3 "codec_ulaw: %susing generic PLC\n", ulawtolin.useplc ? "" : "not ");
00161 }
00162 }
00163 ast_config_destroy(cfg);
00164 }
00165
00166 static int reload(void)
00167 {
00168 parse_config();
00169
00170 return 0;
00171 }
00172
00173 static int unload_module(void)
00174 {
00175 int res;
00176
00177 res = ast_unregister_translator(&lintoulaw);
00178 res |= ast_unregister_translator(&ulawtolin);
00179
00180 return res;
00181 }
00182
00183 static int load_module(void)
00184 {
00185 int res;
00186
00187 parse_config();
00188 res = ast_register_translator(&ulawtolin);
00189 if (!res)
00190 res = ast_register_translator(&lintoulaw);
00191 else
00192 ast_unregister_translator(&ulawtolin);
00193
00194 return res;
00195 }
00196
00197 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00198 .load = load_module,
00199 .unload = unload_module,
00200 .reload = reload,
00201 );