2 * Asterisk -- An open source telephony toolkit.
4 * Zaptel 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 Zaptel transcoding
30 <depend>zaptel_transcode</depend>
31 <depend>zaptel</depend>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41 #include <netinet/in.h>
44 #include <sys/ioctl.h>
47 #include <zaptel/zaptel.h>
49 #include "asterisk/lock.h"
50 #include "asterisk/translate.h"
51 #include "asterisk/config.h"
52 #include "asterisk/options.h"
53 #include "asterisk/module.h"
54 #include "asterisk/logger.h"
55 #include "asterisk/channel.h"
56 #include "asterisk/utils.h"
57 #include "asterisk/linkedlists.h"
59 #define BUFFER_SAMPLES 8000
61 static unsigned int global_useplc = 0;
64 unsigned int map[32][32];
67 static struct format_map global_format_map = { { { 0 } } };
70 struct ast_translator t;
71 AST_LIST_ENTRY(translator) entry;
74 static AST_LIST_HEAD_STATIC(translators, translator);
79 #ifdef DEBUG_TRANSCODE
83 struct zt_transcode_header *hdr;
87 static int zap_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
89 struct pvt *ztp = pvt->pvt;
90 struct zt_transcode_header *hdr = ztp->hdr;
93 /* Fake a return frame for calculation purposes */
95 pvt->samples = f->samples;
100 /* Copy at front of buffer */
103 if (hdr->srclen + f->datalen > sizeof(hdr->srcdata)) {
104 ast_log(LOG_WARNING, "Out of space for codec translation!\n");
108 if (hdr->srclen + f->datalen + hdr->srcoffset > sizeof(hdr->srcdata)) {
110 memmove(hdr->srcdata, hdr->srcdata + hdr->srcoffset, hdr->srclen);
114 memcpy(hdr->srcdata + hdr->srcoffset + hdr->srclen, f->data, f->datalen);
115 hdr->srclen += f->datalen;
116 pvt->samples += f->samples;
121 static struct ast_frame *zap_frameout(struct ast_trans_pvt *pvt)
123 struct pvt *ztp = pvt->pvt;
124 struct zt_transcode_header *hdr = ztp->hdr;
127 if (ztp->fake == 2) {
129 ztp->f.frametype = AST_FRAME_VOICE;
131 ztp->f.samples = 160;
137 } else if (ztp->fake == 1) {
141 #ifdef DEBUG_TRANSCODE
142 ztp->totalms += hdr->dstsamples;
143 if ((ztp->totalms - ztp->lasttotalms) > 8000) {
144 printf("Whee %p, %d (%d to %d)\n", ztp, hdr->dstlen, ztp->lasttotalms, ztp->totalms);
145 ztp->lasttotalms = ztp->totalms;
148 ztp->f.frametype = AST_FRAME_VOICE;
149 ztp->f.subclass = hdr->dstfmt;
150 ztp->f.samples = hdr->dstsamples;
151 ztp->f.data = hdr->dstdata + hdr->dstoffset;
152 ztp->f.offset = hdr->dstoffset;
153 ztp->f.datalen = hdr->dstlen;
155 pvt->samples -= ztp->f.samples;
160 hdr->dstoffset = AST_FRIENDLY_OFFSET;
161 x = ZT_TCOP_TRANSCODE;
162 if (ioctl(ztp->fd, ZT_TRANSCODE_OP, &x))
163 ast_log(LOG_WARNING, "Failed to transcode: %s\n", strerror(errno));
172 static void zap_destroy(struct ast_trans_pvt *pvt)
174 struct pvt *ztp = pvt->pvt;
176 munmap(ztp->hdr, sizeof(*ztp->hdr));
180 static int zap_translate(struct ast_trans_pvt *pvt, int dest, int source)
182 /* Request translation through zap if possible */
184 unsigned int x = ZT_TCOP_ALLOCATE;
185 struct pvt *ztp = pvt->pvt;
186 struct zt_transcode_header *hdr;
189 if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0)
191 flags = fcntl(fd, F_GETFL);
193 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
194 ast_log(LOG_WARNING, "Could not set non-block mode!\n");
198 if ((hdr = mmap(NULL, sizeof(*hdr), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
199 ast_log(LOG_ERROR, "Memory Map failed for transcoding (%s)\n", strerror(errno));
205 if (hdr->magic != ZT_TRANSCODE_MAGIC) {
206 ast_log(LOG_ERROR, "Transcoder header (%08x) wasn't magic. Abandoning\n", hdr->magic);
207 munmap(hdr, sizeof(*hdr));
213 hdr->srcfmt = (1 << source);
214 hdr->dstfmt = (1 << dest);
215 if (ioctl(fd, ZT_TRANSCODE_OP, &x)) {
216 ast_log(LOG_ERROR, "Unable to attach transcoder: %s\n", strerror(errno));
217 munmap(hdr, sizeof(*hdr));
230 static int zap_new(struct ast_trans_pvt *pvt)
232 return zap_translate(pvt, pvt->t->dstfmt, pvt->t->srcfmt);
235 static struct ast_frame *fakesrc_sample(void)
237 /* Don't bother really trying to test hardware ones. */
238 static struct ast_frame f = {
239 .frametype = AST_FRAME_VOICE,
241 .src = __PRETTY_FUNCTION__
247 static int register_translator(int dst, int src)
249 struct translator *zt;
252 if (!(zt = ast_calloc(1, sizeof(*zt))))
255 snprintf((char *) (zt->t.name), sizeof(zt->t.name), "zap%sto%s",
256 ast_getformatname((1 << src)), ast_getformatname((1 << dst)));
257 zt->t.srcfmt = (1 << src);
258 zt->t.dstfmt = (1 << dst);
259 zt->t.newpvt = zap_new;
260 zt->t.framein = zap_framein;
261 zt->t.frameout = zap_frameout;
262 zt->t.destroy = zap_destroy;
263 zt->t.sample = fakesrc_sample;
264 zt->t.useplc = global_useplc;
265 zt->t.buf_size = BUFFER_SAMPLES * 2;
266 zt->t.desc_size = sizeof(struct pvt);
267 if ((res = ast_register_translator(&zt->t))) {
272 AST_LIST_LOCK(&translators);
273 AST_LIST_INSERT_HEAD(&translators, zt, entry);
274 AST_LIST_UNLOCK(&translators);
276 global_format_map.map[dst][src] = 1;
281 static void drop_translator(int dst, int src)
283 struct translator *cur;
285 AST_LIST_LOCK(&translators);
286 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
287 if (cur->t.srcfmt != src)
290 if (cur->t.dstfmt != dst)
293 AST_LIST_REMOVE_CURRENT(&translators, entry);
294 ast_unregister_translator(&cur->t);
296 global_format_map.map[dst][src] = 0;
299 AST_LIST_TRAVERSE_SAFE_END;
300 AST_LIST_UNLOCK(&translators);
303 static void unregister_translators(void)
305 struct translator *cur;
307 AST_LIST_LOCK(&translators);
308 while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
309 ast_unregister_translator(&cur->t);
312 AST_LIST_UNLOCK(&translators);
315 static void parse_config(void)
317 struct ast_variable *var;
318 struct ast_config *cfg = ast_config_load("codecs.conf");
323 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
324 if (!strcasecmp(var->name, "genericplc")) {
325 global_useplc = ast_true(var->value);
326 if (option_verbose > 2)
327 ast_verbose(VERBOSE_PREFIX_3 "codec_zap: %susing generic PLC\n",
328 global_useplc ? "" : "not ");
332 ast_config_destroy(cfg);
335 static void build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
337 unsigned int src, dst;
339 for (src = 0; src < 32; src++) {
340 for (dst = 0; dst < 32; dst++) {
341 if (!(srcfmts & (1 << src)))
344 if (!(dstfmts & (1 << dst)))
347 if (global_format_map.map[dst][src])
350 if (!register_translator(dst, src))
351 map->map[dst][src] = 1;
356 static int find_transcoders(void)
358 struct zt_transcode_info info = { 0, };
359 struct format_map map = { { { 0 } } };
363 info.op = ZT_TCOP_GETINFO;
364 if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0) {
365 ast_log(LOG_NOTICE, "No Zaptel transcoder support!\n");
368 for (info.tcnum = 0; !(res = ioctl(fd, ZT_TRANSCODE_OP, &info)); info.tcnum++) {
369 if (option_verbose > 1)
370 ast_verbose(VERBOSE_PREFIX_2 "Found transcoder '%s'.\n", info.name);
371 build_translators(&map, info.dstfmts, info.srcfmts);
375 if (!info.tcnum && (option_verbose > 1))
376 ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
378 for (x = 0; x < 32; x++) {
379 for (y = 0; y < 32; y++) {
380 if (!map.map[x][y] && global_format_map.map[x][y])
381 drop_translator(x, y);
388 static int reload(void)
390 struct translator *cur;
395 AST_LIST_LOCK(&translators);
396 AST_LIST_TRAVERSE(&translators, cur, entry)
397 cur->t.useplc = global_useplc;
398 AST_LIST_UNLOCK(&translators);
403 static int unload_module(void)
405 unregister_translators();
410 static int load_module(void)
418 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic Zaptel Transcoder Codec Translator",
420 .unload = unload_module,