2 * Asterisk -- An open source telephony toolkit.
4 * DAHDI native transcoding support
6 * Copyright (C) 1999 - 2008, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
9 * Kevin P. Fleming <kpfleming@digium.com>
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
24 * \brief Translate between various formats natively through DAHDI transcoding
30 <depend>dahdi</depend>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include <netinet/in.h>
39 #include <sys/ioctl.h>
42 #include <dahdi/user.h>
44 #include "asterisk/lock.h"
45 #include "asterisk/translate.h"
46 #include "asterisk/config.h"
47 #include "asterisk/module.h"
48 #include "asterisk/cli.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/utils.h"
51 #include "asterisk/linkedlists.h"
53 #define BUFFER_SAMPLES 8000
55 static unsigned int global_useplc = 0;
57 static struct channel_usage {
63 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
65 static struct ast_cli_entry cli[] = {
66 AST_CLI_DEFINE(handle_cli_transcoder_show, "Display DAHDI transcoder utilization.")
70 unsigned int map[32][32];
73 static struct format_map global_format_map = { { { 0 } } };
76 struct ast_translator t;
77 AST_LIST_ENTRY(translator) entry;
80 static AST_LIST_HEAD_STATIC(translators, translator);
85 struct dahdi_transcoder_formats fmts;
89 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
91 struct channel_usage copy;
95 e->command = "transcoder show";
97 "Usage: transcoder show\n"
98 " Displays channel utilization of DAHDI transcoder(s).\n";
105 return CLI_SHOWUSAGE;
110 ast_cli(a->fd, "No DAHDI transcoders found.\n");
112 ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
117 static int dahdi_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
120 struct pvt *dahdip = pvt->pvt;
123 /* Give the frame to the hardware transcoder... */
124 res = write(dahdip->fd, f->data.ptr, f->datalen);
126 ast_log(LOG_ERROR, "Failed to write to /dev/dahdi/transcode: %s\n", strerror(errno));
128 if (f->datalen != res) {
129 ast_log(LOG_ERROR, "Requested write of %d bytes, but only wrote %d bytes.\n", f->datalen, res);
132 pvt->samples += f->samples;
134 /* Fake a return frame for calculation purposes */
136 pvt->samples = f->samples;
142 static struct ast_frame *dahdi_frameout(struct ast_trans_pvt *pvt)
144 struct pvt *dahdip = pvt->pvt;
146 if (0 == dahdip->fake) {
148 /* Let's check to see if there is a new frame for us.... */
149 res = read(dahdip->fd, pvt->outbuf.uc + pvt->datalen, pvt->t->buf_size - pvt->datalen);
151 if (EWOULDBLOCK == errno) {
152 /* Nothing waiting... */
155 ast_log(LOG_ERROR, "Failed to read from /dev/dahdi/transcode: %s\n", strerror(errno));
159 pvt->f.samples = dahdip->samples;
160 pvt->f.datalen = res;
162 pvt->f.frametype = AST_FRAME_VOICE;
163 pvt->f.subclass = 1 << (pvt->t->dstfmt);
165 pvt->f.offset = AST_FRIENDLY_OFFSET;
166 pvt->f.src = pvt->t->name;
167 pvt->f.data.ptr = pvt->outbuf.uc;
168 ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
173 } else if (2 == dahdip->fake) {
176 pvt->f.frametype = AST_FRAME_VOICE;
178 pvt->f.samples = 160;
179 pvt->f.data.ptr = NULL;
183 ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
188 } else if (1 == dahdip->fake) {
193 /* Shouldn't get here... */
197 static void dahdi_destroy(struct ast_trans_pvt *pvt)
199 struct pvt *dahdip = pvt->pvt;
201 switch (dahdip->fmts.dstfmt) {
202 case AST_FORMAT_G729A:
203 case AST_FORMAT_G723_1:
204 ast_atomic_fetchadd_int(&channels.encoders, -1);
207 ast_atomic_fetchadd_int(&channels.decoders, -1);
214 static int dahdi_translate(struct ast_trans_pvt *pvt, int dest, int source)
216 /* Request translation through zap if possible */
218 struct pvt *dahdip = pvt->pvt;
221 if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
222 ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
226 dahdip->fmts.srcfmt = (1 << source);
227 dahdip->fmts.dstfmt = (1 << dest);
229 ast_log(LOG_VERBOSE, "Opening transcoder channel from %d to %d.\n", source, dest);
231 if (ioctl(fd, DAHDI_TC_ALLOCATE, &dahdip->fmts)) {
232 ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
238 flags = fcntl(fd, F_GETFL);
240 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
241 ast_log(LOG_WARNING, "Could not set non-block mode!\n");
246 switch (dahdip->fmts.dstfmt) {
247 case AST_FORMAT_G729A:
248 dahdip->samples = 160;
250 case AST_FORMAT_G723_1:
251 dahdip->samples = 240;
252 ast_atomic_fetchadd_int(&channels.encoders, +1);
255 dahdip->samples = 160;
256 ast_atomic_fetchadd_int(&channels.decoders, +1);
263 static int dahdi_new(struct ast_trans_pvt *pvt)
265 return dahdi_translate(pvt, pvt->t->dstfmt, pvt->t->srcfmt);
268 static struct ast_frame *fakesrc_sample(void)
270 /* Don't bother really trying to test hardware ones. */
271 static struct ast_frame f = {
272 .frametype = AST_FRAME_VOICE,
274 .src = __PRETTY_FUNCTION__
280 static int register_translator(int dst, int src)
282 struct translator *dahdi;
285 if (!(dahdi = ast_calloc(1, sizeof(*dahdi))))
288 snprintf((char *) (dahdi->t.name), sizeof(dahdi->t.name), "dahdi%sto%s",
289 ast_getformatname((1 << src)), ast_getformatname((1 << dst)));
290 dahdi->t.srcfmt = (1 << src);
291 dahdi->t.dstfmt = (1 << dst);
292 dahdi->t.newpvt = dahdi_new;
293 dahdi->t.framein = dahdi_framein;
294 dahdi->t.frameout = dahdi_frameout;
295 dahdi->t.destroy = dahdi_destroy;
296 dahdi->t.sample = fakesrc_sample;
297 dahdi->t.useplc = global_useplc;
298 dahdi->t.buf_size = BUFFER_SAMPLES * 2;
299 dahdi->t.desc_size = sizeof(struct pvt);
300 if ((res = ast_register_translator(&dahdi->t))) {
305 AST_LIST_LOCK(&translators);
306 AST_LIST_INSERT_HEAD(&translators, dahdi, entry);
307 AST_LIST_UNLOCK(&translators);
309 global_format_map.map[dst][src] = 1;
314 static void drop_translator(int dst, int src)
316 struct translator *cur;
318 AST_LIST_LOCK(&translators);
319 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
320 if (cur->t.srcfmt != src)
323 if (cur->t.dstfmt != dst)
326 AST_LIST_REMOVE_CURRENT(entry);
327 ast_unregister_translator(&cur->t);
329 global_format_map.map[dst][src] = 0;
332 AST_LIST_TRAVERSE_SAFE_END;
333 AST_LIST_UNLOCK(&translators);
336 static void unregister_translators(void)
338 struct translator *current;
340 AST_LIST_LOCK(&translators);
341 while ((current = AST_LIST_REMOVE_HEAD(&translators, entry))) {
342 ast_unregister_translator(¤t->t);
345 AST_LIST_UNLOCK(&translators);
348 static int parse_config(int reload)
350 struct ast_variable *var;
351 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
352 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
356 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
359 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
360 if (!strcasecmp(var->name, "genericplc")) {
361 global_useplc = ast_true(var->value);
362 ast_verb(3, "codec_dahdi: %susing generic PLC\n",
363 global_useplc ? "" : "not ");
366 ast_config_destroy(cfg);
370 static void build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
372 unsigned int src, dst;
374 for (src = 0; src < 32; src++) {
375 for (dst = 0; dst < 32; dst++) {
376 if (!(srcfmts & (1 << src)))
379 if (!(dstfmts & (1 << dst)))
382 if (global_format_map.map[dst][src])
385 if (!register_translator(dst, src))
386 map->map[dst][src] = 1;
391 static int find_transcoders(void)
393 struct dahdi_transcoder_info info = { 0, };
394 struct format_map map = { { { 0 } } };
398 if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
399 ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
403 for (info.tcnum = 0; !(res = ioctl(fd, DAHDI_TC_GETINFO, &info)); info.tcnum++) {
404 if (option_verbose > 1)
405 ast_verbose(VERBOSE_PREFIX_2 "Found transcoder '%s'.\n", info.name);
406 build_translators(&map, info.dstfmts, info.srcfmts);
407 ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
412 if (!info.tcnum && (option_verbose > 1))
413 ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
415 for (x = 0; x < 32; x++) {
416 for (y = 0; y < 32; y++) {
417 if (!map.map[x][y] && global_format_map.map[x][y])
418 drop_translator(x, y);
425 static int reload(void)
427 struct translator *cur;
430 return AST_MODULE_LOAD_DECLINE;
432 AST_LIST_LOCK(&translators);
433 AST_LIST_TRAVERSE(&translators, cur, entry)
434 cur->t.useplc = global_useplc;
435 AST_LIST_UNLOCK(&translators);
437 return AST_MODULE_LOAD_SUCCESS;
440 static int unload_module(void)
442 ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
443 unregister_translators();
448 static int load_module(void)
451 return AST_MODULE_LOAD_DECLINE;
453 ast_cli_register_multiple(cli, ARRAY_LEN(cli));
454 return AST_MODULE_LOAD_SUCCESS;
457 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic DAHDI Transcoder Codec Translator",
459 .unload = unload_module,