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"
52 #include "asterisk/ulaw.h"
54 #define BUFFER_SIZE 8000
56 #define G723_SAMPLES 240
57 #define G729_SAMPLES 160
59 static unsigned int global_useplc = 0;
61 static struct channel_usage {
67 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
69 static struct ast_cli_entry cli[] = {
70 AST_CLI_DEFINE(handle_cli_transcoder_show, "Display DAHDI transcoder utilization.")
74 unsigned int map[32][32];
77 static struct format_map global_format_map = { { { 0 } } };
80 struct ast_translator t;
81 AST_LIST_ENTRY(translator) entry;
84 static AST_LIST_HEAD_STATIC(translators, translator);
86 struct codec_dahdi_pvt {
88 struct dahdi_transcoder_formats fmts;
89 unsigned int softslin:1;
91 uint16_t required_samples;
92 uint16_t samples_in_buffer;
93 uint8_t ulaw_buffer[1024];
96 /* Only used by a decoder */
97 static int ulawtolin(struct ast_trans_pvt *pvt)
99 struct codec_dahdi_pvt *dahdip = pvt->pvt;
100 int i = dahdip->required_samples;
101 uint8_t *src = &dahdip->ulaw_buffer[0];
102 int16_t *dst = pvt->outbuf.i16 + pvt->datalen;
104 /* convert and copy in outbuf */
106 *dst++ = AST_MULAW(*src++);
112 /* Only used by an encoder. */
113 static int lintoulaw(struct ast_trans_pvt *pvt, struct ast_frame *f)
115 struct codec_dahdi_pvt *dahdip = pvt->pvt;
117 uint8_t *dst = &dahdip->ulaw_buffer[dahdip->samples_in_buffer];
118 int16_t *src = f->data.ptr;
120 if (dahdip->samples_in_buffer + i > sizeof(dahdip->ulaw_buffer)) {
121 ast_log(LOG_ERROR, "Out of buffer space!\n");
126 *dst++ = AST_LIN2MU(*src++);
129 dahdip->samples_in_buffer += f->samples;
133 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
135 struct channel_usage copy;
139 e->command = "transcoder show";
141 "Usage: transcoder show\n"
142 " Displays channel utilization of DAHDI transcoder(s).\n";
149 return CLI_SHOWUSAGE;
154 ast_cli(a->fd, "No DAHDI transcoders found.\n");
156 ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
161 static void dahdi_write_frame(struct codec_dahdi_pvt *dahdip, const uint8_t *buffer, const ssize_t count)
164 struct pollfd p = {0};
166 res = write(dahdip->fd, buffer, count);
167 if (option_verbose > 10) {
169 ast_log(LOG_ERROR, "Failed to write to transcoder: %s\n", strerror(errno));
172 ast_log(LOG_ERROR, "Requested write of %zd bytes, but only wrote %d bytes.\n", count, res);
177 res = poll(&p, 1, 50);
180 static int dahdi_encoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
182 struct codec_dahdi_pvt *dahdip = pvt->pvt;
185 /* We're just faking a return for calculation purposes. */
187 pvt->samples = f->samples;
191 /* Buffer up the packets and send them to the hardware if we
192 * have enough samples set up. */
193 if (dahdip->softslin) {
194 if (lintoulaw(pvt, f)) {
198 /* NOTE: If softslin support is not needed, and the sample
199 * size is equal to the required sample size, we wouldn't
200 * need this copy operation. But at the time this was
201 * written, only softslin is supported. */
202 if (dahdip->samples_in_buffer + f->samples > sizeof(dahdip->ulaw_buffer)) {
203 ast_log(LOG_ERROR, "Out of buffer space.\n");
206 memcpy(&dahdip->ulaw_buffer[dahdip->samples_in_buffer], f->data.ptr, f->samples);
207 dahdip->samples_in_buffer += f->samples;
210 while (dahdip->samples_in_buffer > dahdip->required_samples) {
211 dahdi_write_frame(dahdip, dahdip->ulaw_buffer, dahdip->required_samples);
212 dahdip->samples_in_buffer -= dahdip->required_samples;
213 if (dahdip->samples_in_buffer) {
214 /* Shift any remaining bytes down. */
215 memmove(dahdip->ulaw_buffer, &dahdip->ulaw_buffer[dahdip->required_samples],
216 dahdip->samples_in_buffer);
219 pvt->samples += f->samples;
224 static struct ast_frame *dahdi_encoder_frameout(struct ast_trans_pvt *pvt)
226 struct codec_dahdi_pvt *dahdip = pvt->pvt;
229 if (2 == dahdip->fake) {
231 pvt->f.frametype = AST_FRAME_VOICE;
233 pvt->f.samples = dahdip->required_samples;
234 pvt->f.data.ptr = NULL;
240 return ast_frisolate(&pvt->f);
242 } else if (1 == dahdip->fake) {
247 res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
249 if (EWOULDBLOCK == errno) {
250 /* Nothing waiting... */
253 ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
257 pvt->f.datalen = res;
258 pvt->f.samples = dahdip->required_samples;
259 pvt->f.frametype = AST_FRAME_VOICE;
260 pvt->f.subclass = 1 << (pvt->t->dstfmt);
262 pvt->f.offset = AST_FRIENDLY_OFFSET;
263 pvt->f.src = pvt->t->name;
264 pvt->f.data.ptr = pvt->outbuf.c;
268 return ast_frisolate(&pvt->f);
271 /* Shouldn't get here... */
275 static int dahdi_decoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
277 struct codec_dahdi_pvt *dahdip = pvt->pvt;
280 /* We're just faking a return for calculation purposes. */
282 pvt->samples = f->samples;
287 if (f->samples != dahdip->required_samples) {
288 ast_log(LOG_ERROR, "%d != %d %d\n", f->samples, dahdip->required_samples, f->datalen);
291 dahdi_write_frame(dahdip, f->data.ptr, f->datalen);
292 pvt->samples += f->samples;
297 static struct ast_frame *dahdi_decoder_frameout(struct ast_trans_pvt *pvt)
300 struct codec_dahdi_pvt *dahdip = pvt->pvt;
302 if (2 == dahdip->fake) {
304 pvt->f.frametype = AST_FRAME_VOICE;
306 pvt->f.samples = dahdip->required_samples;
307 pvt->f.data.ptr = NULL;
312 return ast_frisolate(&pvt->f);
313 } else if (1 == dahdip->fake) {
319 /* Let's check to see if there is a new frame for us.... */
320 if (dahdip->softslin) {
321 res = read(dahdip->fd, dahdip->ulaw_buffer, sizeof(dahdip->ulaw_buffer));
323 res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
327 if (EWOULDBLOCK == errno) {
328 /* Nothing waiting... */
331 ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
335 if (dahdip->softslin) {
337 pvt->f.datalen = res * 2;
339 pvt->f.datalen = res;
342 pvt->f.frametype = AST_FRAME_VOICE;
343 pvt->f.subclass = 1 << (pvt->t->dstfmt);
345 pvt->f.offset = AST_FRIENDLY_OFFSET;
346 pvt->f.src = pvt->t->name;
347 pvt->f.data.ptr = pvt->outbuf.c;
348 pvt->f.samples = dahdip->required_samples;
351 return ast_frisolate(&pvt->f);
354 /* Shouldn't get here... */
359 static void dahdi_destroy(struct ast_trans_pvt *pvt)
361 struct codec_dahdi_pvt *dahdip = pvt->pvt;
363 switch (dahdip->fmts.dstfmt) {
364 case AST_FORMAT_G729A:
365 case AST_FORMAT_G723_1:
366 ast_atomic_fetchadd_int(&channels.encoders, -1);
369 ast_atomic_fetchadd_int(&channels.decoders, -1);
376 static int dahdi_translate(struct ast_trans_pvt *pvt, int dest, int source)
378 /* Request translation through zap if possible */
380 struct codec_dahdi_pvt *dahdip = pvt->pvt;
383 const char *dev_filename = "/dev/dahdi/transcode";
385 if ((fd = open(dev_filename, O_RDWR)) < 0) {
386 ast_log(LOG_ERROR, "Failed to open %s: %s\n", dev_filename, strerror(errno));
390 dahdip->fmts.srcfmt = (1 << source);
391 dahdip->fmts.dstfmt = (1 << dest);
393 ast_debug(1, "Opening transcoder channel from %d to %d.\n", source, dest);
396 if (ioctl(fd, DAHDI_TC_ALLOCATE, &dahdip->fmts)) {
397 if ((ENODEV == errno) && !tried_once) {
398 /* We requested to translate to/from an unsupported
399 * format. Most likely this is because signed linear
400 * was not supported by any hardware devices even
401 * though this module always registers signed linear
402 * support. In this case we'll retry, requesting
403 * support for ULAW instead of signed linear and then
404 * we'll just convert from ulaw to signed linear in
406 if (AST_FORMAT_SLINEAR == dahdip->fmts.srcfmt) {
407 ast_debug(1, "Using soft_slin support on source\n");
408 dahdip->softslin = 1;
409 dahdip->fmts.srcfmt = AST_FORMAT_ULAW;
410 } else if (AST_FORMAT_SLINEAR == dahdip->fmts.dstfmt) {
411 ast_debug(1, "Using soft_slin support on destination\n");
412 dahdip->softslin = 1;
413 dahdip->fmts.dstfmt = AST_FORMAT_ULAW;
418 ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
424 flags = fcntl(fd, F_GETFL);
426 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
427 ast_log(LOG_WARNING, "Could not set non-block mode!\n");
432 dahdip->required_samples = ((dahdip->fmts.dstfmt|dahdip->fmts.srcfmt)&AST_FORMAT_G723_1) ? G723_SAMPLES : G729_SAMPLES;
434 switch (dahdip->fmts.dstfmt) {
435 case AST_FORMAT_G729A:
436 ast_atomic_fetchadd_int(&channels.encoders, +1);
438 case AST_FORMAT_G723_1:
439 ast_atomic_fetchadd_int(&channels.encoders, +1);
442 ast_atomic_fetchadd_int(&channels.decoders, +1);
449 static int dahdi_new(struct ast_trans_pvt *pvt)
451 return dahdi_translate(pvt, pvt->t->dstfmt, pvt->t->srcfmt);
454 static struct ast_frame *fakesrc_sample(void)
456 /* Don't bother really trying to test hardware ones. */
457 static struct ast_frame f = {
458 .frametype = AST_FRAME_VOICE,
460 .src = __PRETTY_FUNCTION__
466 static int is_encoder(struct translator *zt)
468 if (zt->t.srcfmt&(AST_FORMAT_ULAW|AST_FORMAT_ALAW|AST_FORMAT_SLINEAR)) {
475 static int register_translator(int dst, int src)
477 struct translator *zt;
480 if (!(zt = ast_calloc(1, sizeof(*zt)))) {
484 snprintf((char *) (zt->t.name), sizeof(zt->t.name), "zap%sto%s",
485 ast_getformatname((1 << src)), ast_getformatname((1 << dst)));
486 zt->t.srcfmt = (1 << src);
487 zt->t.dstfmt = (1 << dst);
488 zt->t.buf_size = BUFFER_SIZE;
489 if (is_encoder(zt)) {
490 zt->t.framein = dahdi_encoder_framein;
491 zt->t.frameout = dahdi_encoder_frameout;
493 zt->t.buffer_samples = 0;
496 zt->t.framein = dahdi_decoder_framein;
497 zt->t.frameout = dahdi_decoder_frameout;
499 if (AST_FORMAT_G723_1 == zt->t.srcfmt) {
500 zt->t.plc_samples = G723_SAMPLES;
502 zt->t.plc_samples = G729_SAMPLES;
504 zt->t.buffer_samples = zt->t.plc_samples * 8;
507 zt->t.destroy = dahdi_destroy;
508 zt->t.buffer_samples = 0;
509 zt->t.newpvt = dahdi_new;
510 zt->t.sample = fakesrc_sample;
512 zt->t.useplc = global_useplc;
515 zt->t.native_plc = 0;
517 zt->t.desc_size = sizeof(struct codec_dahdi_pvt);
518 if ((res = ast_register_translator(&zt->t))) {
523 AST_LIST_LOCK(&translators);
524 AST_LIST_INSERT_HEAD(&translators, zt, entry);
525 AST_LIST_UNLOCK(&translators);
527 global_format_map.map[dst][src] = 1;
532 static void drop_translator(int dst, int src)
534 struct translator *cur;
536 AST_LIST_LOCK(&translators);
537 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
538 if (cur->t.srcfmt != src)
541 if (cur->t.dstfmt != dst)
544 AST_LIST_REMOVE_CURRENT(entry);
545 ast_unregister_translator(&cur->t);
547 global_format_map.map[dst][src] = 0;
550 AST_LIST_TRAVERSE_SAFE_END;
551 AST_LIST_UNLOCK(&translators);
554 static void unregister_translators(void)
556 struct translator *cur;
558 AST_LIST_LOCK(&translators);
559 while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
560 ast_unregister_translator(&cur->t);
563 AST_LIST_UNLOCK(&translators);
566 static int parse_config(int reload)
568 struct ast_variable *var;
569 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
570 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
572 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
575 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
576 if (!strcasecmp(var->name, "genericplc")) {
577 global_useplc = ast_true(var->value);
578 ast_verb(3, "codec_dahdi: %susing generic PLC\n",
579 global_useplc ? "" : "not ");
583 ast_config_destroy(cfg);
587 static void build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
589 unsigned int src, dst;
591 for (src = 0; src < 32; src++) {
592 for (dst = 0; dst < 32; dst++) {
593 if (!(srcfmts & (1 << src)))
596 if (!(dstfmts & (1 << dst)))
599 if (global_format_map.map[dst][src])
602 if (!register_translator(dst, src))
603 map->map[dst][src] = 1;
608 static int find_transcoders(void)
610 struct dahdi_transcoder_info info = { 0, };
611 struct format_map map = { { { 0 } } };
615 if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
616 ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
620 for (info.tcnum = 0; !(res = ioctl(fd, DAHDI_TC_GETINFO, &info)); info.tcnum++) {
621 if (option_verbose > 1)
622 ast_verbose(VERBOSE_PREFIX_2 "Found transcoder '%s'.\n", info.name);
624 /* Complex codecs need to support signed linear. If the
625 * hardware transcoder does not natively support signed linear
626 * format, we will emulate it in software directly in this
627 * module. Also, do not allow direct ulaw/alaw to complex
628 * codec translation, since that will prevent the generic PLC
629 * functions from working. */
630 if (info.dstfmts & (AST_FORMAT_ULAW | AST_FORMAT_ALAW)) {
631 info.dstfmts |= AST_FORMAT_SLINEAR;
632 info.dstfmts &= ~(AST_FORMAT_ULAW | AST_FORMAT_ALAW);
634 if (info.srcfmts & (AST_FORMAT_ULAW | AST_FORMAT_ALAW)) {
635 info.srcfmts |= AST_FORMAT_SLINEAR;
636 info.srcfmts &= ~(AST_FORMAT_ULAW | AST_FORMAT_ALAW);
639 build_translators(&map, info.dstfmts, info.srcfmts);
640 ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
646 if (!info.tcnum && (option_verbose > 1))
647 ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
649 for (x = 0; x < 32; x++) {
650 for (y = 0; y < 32; y++) {
651 if (!map.map[x][y] && global_format_map.map[x][y])
652 drop_translator(x, y);
659 static int reload(void)
661 struct translator *cur;
664 return AST_MODULE_LOAD_DECLINE;
666 AST_LIST_LOCK(&translators);
667 AST_LIST_TRAVERSE(&translators, cur, entry)
668 cur->t.useplc = global_useplc;
669 AST_LIST_UNLOCK(&translators);
671 return AST_MODULE_LOAD_SUCCESS;
674 static int unload_module(void)
676 ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
677 unregister_translators();
682 static int load_module(void)
686 return AST_MODULE_LOAD_DECLINE;
688 ast_cli_register_multiple(cli, ARRAY_LEN(cli));
689 return AST_MODULE_LOAD_SUCCESS;
692 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic DAHDI Transcoder Codec Translator",
694 .unload = unload_module,