2 * Asterisk -- An open source telephony toolkit.
4 * DAHDI native transcoding support
6 * Copyright (C) 1999 - 2006, 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 #ifdef DEBUG_TRANSCODE
89 struct dahdi_transcode_header *hdr;
92 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
94 struct channel_usage copy;
98 e->command = "transcoder show";
100 "Usage: transcoder show\n"
101 " Displays channel utilization of DAHDI transcoder(s).\n";
108 return CLI_SHOWUSAGE;
113 ast_cli(a->fd, "No DAHDI transcoders found.\n");
115 ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
120 static int dahdi_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
122 struct pvt *dahdip = pvt->pvt;
123 struct dahdi_transcode_header *hdr = dahdip->hdr;
126 /* Fake a return frame for calculation purposes */
128 pvt->samples = f->samples;
133 /* Copy at front of buffer */
136 if (hdr->srclen + f->datalen > sizeof(hdr->srcdata)) {
137 ast_log(LOG_WARNING, "Out of space for codec translation!\n");
141 if (hdr->srclen + f->datalen + hdr->srcoffset > sizeof(hdr->srcdata)) {
143 memmove(hdr->srcdata, hdr->srcdata + hdr->srcoffset, hdr->srclen);
147 memcpy(hdr->srcdata + hdr->srcoffset + hdr->srclen, f->data.ptr, f->datalen);
148 hdr->srclen += f->datalen;
149 pvt->samples += f->samples;
154 static struct ast_frame *dahdi_frameout(struct ast_trans_pvt *pvt)
156 struct pvt *dahdip = pvt->pvt;
157 struct dahdi_transcode_header *hdr = dahdip->hdr;
160 if (dahdip->fake == 2) {
162 pvt->f.frametype = AST_FRAME_VOICE;
164 pvt->f.samples = 160;
165 pvt->f.data.ptr = NULL;
169 ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
171 } else if (dahdip->fake == 1) {
175 #ifdef DEBUG_TRANSCODE
176 dahdip->totalms += hdr->dstsamples;
177 if ((dahdip->totalms - dahdip->lasttotalms) > 8000) {
178 printf("Whee %p, %d (%d to %d)\n", dahdip, hdr->dstlen, dahdip->lasttotalms, dahdip->totalms);
179 dahdip->lasttotalms = dahdip->totalms;
182 pvt->f.frametype = AST_FRAME_VOICE;
183 pvt->f.subclass = hdr->dstfmt;
184 pvt->f.samples = hdr->dstsamples;
185 pvt->f.data.ptr = hdr->dstdata + hdr->dstoffset;
186 pvt->f.offset = hdr->dstoffset;
187 pvt->f.datalen = hdr->dstlen;
189 ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
190 pvt->samples -= pvt->f.samples;
195 hdr->dstoffset = AST_FRIENDLY_OFFSET;
196 x = DAHDI_TCOP_TRANSCODE;
197 if (ioctl(dahdip->fd, DAHDI_TRANSCODE_OP, &x))
198 ast_log(LOG_WARNING, "Failed to transcode: %s\n", strerror(errno));
207 static void dahdi_destroy(struct ast_trans_pvt *pvt)
209 struct pvt *dahdip = pvt->pvt;
212 x = DAHDI_TCOP_RELEASE;
213 if (ioctl(dahdip->fd, DAHDI_TRANSCODE_OP, &x))
214 ast_log(LOG_WARNING, "Failed to release transcoder channel: %s\n", strerror(errno));
216 switch (dahdip->hdr->dstfmt) {
217 case AST_FORMAT_G729A:
218 case AST_FORMAT_G723_1:
219 ast_atomic_fetchadd_int(&channels.encoders, -1);
222 ast_atomic_fetchadd_int(&channels.decoders, -1);
226 munmap(dahdip->hdr, sizeof(*dahdip->hdr));
230 static int dahdi_translate(struct ast_trans_pvt *pvt, int dest, int source)
232 /* Request translation through dahdi if possible */
234 unsigned int x = DAHDI_TCOP_ALLOCATE;
235 struct pvt *dahdip = pvt->pvt;
236 struct dahdi_transcode_header *hdr;
239 if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0)
241 flags = fcntl(fd, F_GETFL);
243 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
244 ast_log(LOG_WARNING, "Could not set non-block mode!\n");
248 if ((hdr = mmap(NULL, sizeof(*hdr), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
249 ast_log(LOG_ERROR, "Memory Map failed for transcoding (%s)\n", strerror(errno));
255 if (hdr->magic != DAHDI_TRANSCODE_MAGIC) {
256 ast_log(LOG_ERROR, "Transcoder header (%08x) wasn't magic. Abandoning\n", hdr->magic);
257 munmap(hdr, sizeof(*hdr));
263 hdr->srcfmt = (1 << source);
264 hdr->dstfmt = (1 << dest);
265 if (ioctl(fd, DAHDI_TRANSCODE_OP, &x)) {
266 ast_log(LOG_ERROR, "Unable to attach transcoder: %s\n", strerror(errno));
267 munmap(hdr, sizeof(*hdr));
277 switch (hdr->dstfmt) {
278 case AST_FORMAT_G729A:
279 case AST_FORMAT_G723_1:
280 ast_atomic_fetchadd_int(&channels.encoders, +1);
283 ast_atomic_fetchadd_int(&channels.decoders, +1);
290 static int dahdi_new(struct ast_trans_pvt *pvt)
292 return dahdi_translate(pvt, pvt->t->dstfmt, pvt->t->srcfmt);
295 static struct ast_frame *fakesrc_sample(void)
297 /* Don't bother really trying to test hardware ones. */
298 static struct ast_frame f = {
299 .frametype = AST_FRAME_VOICE,
301 .src = __PRETTY_FUNCTION__
307 static int register_translator(int dst, int src)
309 struct translator *dahdi;
312 if (!(dahdi = ast_calloc(1, sizeof(*dahdi))))
315 snprintf((char *) (dahdi->t.name), sizeof(dahdi->t.name), "DAHDI%sto%s",
316 ast_getformatname((1 << src)), ast_getformatname((1 << dst)));
317 dahdi->t.srcfmt = (1 << src);
318 dahdi->t.dstfmt = (1 << dst);
319 dahdi->t.newpvt = dahdi_new;
320 dahdi->t.framein = dahdi_framein;
321 dahdi->t.frameout = dahdi_frameout;
322 dahdi->t.destroy = dahdi_destroy;
323 dahdi->t.sample = fakesrc_sample;
324 dahdi->t.useplc = global_useplc;
325 dahdi->t.buf_size = BUFFER_SAMPLES * 2;
326 dahdi->t.desc_size = sizeof(struct pvt);
327 if ((res = ast_register_translator(&dahdi->t))) {
332 AST_LIST_LOCK(&translators);
333 AST_LIST_INSERT_HEAD(&translators, dahdi, entry);
334 AST_LIST_UNLOCK(&translators);
336 global_format_map.map[dst][src] = 1;
341 static void drop_translator(int dst, int src)
343 struct translator *cur;
345 AST_LIST_LOCK(&translators);
346 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
347 if (cur->t.srcfmt != src)
350 if (cur->t.dstfmt != dst)
353 AST_LIST_REMOVE_CURRENT(entry);
354 ast_unregister_translator(&cur->t);
356 global_format_map.map[dst][src] = 0;
359 AST_LIST_TRAVERSE_SAFE_END;
360 AST_LIST_UNLOCK(&translators);
363 static void unregister_translators(void)
365 struct translator *current;
367 AST_LIST_LOCK(&translators);
368 while ((current = AST_LIST_REMOVE_HEAD(&translators, entry))) {
369 ast_unregister_translator(¤t->t);
372 AST_LIST_UNLOCK(&translators);
375 static int parse_config(int reload)
377 struct ast_variable *var;
378 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
379 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
383 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
386 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
387 if (!strcasecmp(var->name, "genericplc")) {
388 global_useplc = ast_true(var->value);
389 ast_verb(3, "codec_dahdi: %susing generic PLC\n",
390 global_useplc ? "" : "not ");
393 ast_config_destroy(cfg);
397 static void build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
399 unsigned int src, dst;
401 for (src = 0; src < 32; src++) {
402 for (dst = 0; dst < 32; dst++) {
403 if (!(srcfmts & (1 << src)))
406 if (!(dstfmts & (1 << dst)))
409 if (global_format_map.map[dst][src])
412 if (!register_translator(dst, src))
413 map->map[dst][src] = 1;
418 static int find_transcoders(void)
420 struct dahdi_transcode_info info = { 0, };
421 struct format_map map = { { { 0 } } };
425 if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
426 ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
430 info.op = DAHDI_TCOP_GETINFO;
431 for (info.tcnum = 0; !(res = ioctl(fd, DAHDI_TRANSCODE_OP, &info)); info.tcnum++) {
432 ast_verb(2, "Found transcoder '%s'.\n", info.name);
433 build_translators(&map, info.dstfmts, info.srcfmts);
434 ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
440 ast_verb(2, "No hardware transcoders found.\n");
442 for (x = 0; x < 32; x++) {
443 for (y = 0; y < 32; y++) {
444 if (!map.map[x][y] && global_format_map.map[x][y])
445 drop_translator(x, y);
452 static int reload(void)
454 struct translator *cur;
457 return AST_MODULE_LOAD_DECLINE;
459 AST_LIST_LOCK(&translators);
460 AST_LIST_TRAVERSE(&translators, cur, entry)
461 cur->t.useplc = global_useplc;
462 AST_LIST_UNLOCK(&translators);
464 return AST_MODULE_LOAD_SUCCESS;
467 static int unload_module(void)
469 ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
470 unregister_translators();
475 static int load_module(void)
478 return AST_MODULE_LOAD_DECLINE;
480 ast_cli_register_multiple(cli, ARRAY_LEN(cli));
481 return AST_MODULE_LOAD_SUCCESS;
484 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic DAHDI Transcoder Codec Translator",
486 .unload = unload_module,