add ExtenSpy variant of ChanSpy
[asterisk-bristuff.git] / codecs / codec_zap.c
blobee9b94a4abbcc5741e572fa4055ef234c5acaf16
1 /*
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.
22 /*! \file
24 * \brief Translate between various formats natively through Zaptel transcoding
26 * \ingroup codecs
29 /*** MODULEINFO
30 <depend>zaptel</depend>
31 ***/
33 #include "asterisk.h"
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <netinet/in.h>
41 #include <string.h>
42 #include <stdio.h>
43 #include <sys/ioctl.h>
44 #include <errno.h>
45 #include <sys/mman.h>
46 #include <zaptel/zaptel.h>
48 #include "asterisk/lock.h"
49 #include "asterisk/translate.h"
50 #include "asterisk/config.h"
51 #include "asterisk/options.h"
52 #include "asterisk/module.h"
53 #include "asterisk/logger.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/utils.h"
56 #include "asterisk/linkedlists.h"
58 #define BUFFER_SAMPLES 8000
60 static unsigned int global_useplc = 0;
62 struct format_map {
63 unsigned int map[32][32];
66 static struct format_map global_format_map = { { { 0 } } };
68 struct translator {
69 struct ast_translator t;
70 AST_LIST_ENTRY(translator) entry;
73 static AST_LIST_HEAD_STATIC(translators, translator);
75 struct pvt {
76 int fd;
77 int fake;
78 struct zt_transcode_header *hdr;
79 struct ast_frame f;
82 static int zap_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
84 struct pvt *ztp = pvt->pvt;
85 struct zt_transcode_header *hdr = ztp->hdr;
87 if (!f->subclass) {
88 /* Fake a return frame for calculation purposes */
89 ztp->fake = 2;
90 pvt->samples = f->samples;
91 return 0;
94 if (!hdr->srclen)
95 /* Copy at front of buffer */
96 hdr->srcoffset = 0;
98 if (hdr->srclen + f->datalen > sizeof(hdr->srcdata)) {
99 ast_log(LOG_WARNING, "Out of space for codec translation!\n");
100 return -1;
103 if (hdr->srclen + f->datalen + hdr->srcoffset > sizeof(hdr->srcdata)) {
104 /* Very unlikely */
105 memmove(hdr->srcdata, hdr->srcdata + hdr->srcoffset, hdr->srclen);
106 hdr->srcoffset = 0;
109 memcpy(hdr->srcdata + hdr->srcoffset, f->data, f->datalen);
110 hdr->srclen += f->datalen;
111 pvt->samples += f->samples;
113 return -1;
116 static struct ast_frame *zap_frameout(struct ast_trans_pvt *pvt)
118 struct pvt *ztp = pvt->pvt;
119 struct zt_transcode_header *hdr = ztp->hdr;
120 unsigned int x;
122 if (ztp->fake == 2) {
123 ztp->fake = 1;
124 ztp->f.frametype = AST_FRAME_VOICE;
125 ztp->f.subclass = 0;
126 ztp->f.samples = 160;
127 ztp->f.data = NULL;
128 ztp->f.offset = 0;
129 ztp->f.datalen = 0;
130 ztp->f.mallocd = 0;
131 pvt->samples = 0;
132 } else if (ztp->fake == 1) {
133 return NULL;
134 } else if (!hdr->srclen) {
135 return NULL;
136 } else {
137 hdr->dstoffset = 0;
138 hdr->dstlen = 0;
139 x = ZT_TCOP_TRANSCODE;
140 if (ioctl(ztp->fd, ZT_TRANSCODE_OP, &x))
141 ast_log(LOG_WARNING, "Failed to transcode: %s\n", strerror(errno));
142 if (!hdr->dstlen)
143 return NULL;
144 ztp->f.frametype = AST_FRAME_VOICE;
145 ztp->f.subclass = hdr->dstfmt;
146 ztp->f.samples = hdr->dstsamples;
147 ztp->f.data = hdr->dstdata + hdr->dstoffset;
148 ztp->f.offset = hdr->dstoffset;
149 ztp->f.datalen = hdr->dstlen;
150 ztp->f.mallocd = 0;
151 pvt->samples -= ztp->f.samples;
154 return &ztp->f;
157 static void zap_destroy(struct ast_trans_pvt *pvt)
159 struct pvt *ztp = pvt->pvt;
161 munmap(ztp->hdr, sizeof(*ztp->hdr));
162 close(ztp->fd);
165 static int zap_translate(struct ast_trans_pvt *pvt, int dest, int source)
167 /* Request translation through zap if possible */
168 int fd;
169 unsigned int x = ZT_TCOP_RESET;
170 struct pvt *ztp = pvt->pvt;
171 struct zt_transcode_header *hdr;
173 if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0)
174 return -1;
176 if ((hdr = mmap(NULL, sizeof(*hdr), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
177 ast_log(LOG_ERROR, "Memory Map failed for transcoding (%s)\n", strerror(errno));
178 close(fd);
180 return -1;
183 if (hdr->magic != ZT_TRANSCODE_MAGIC) {
184 ast_log(LOG_ERROR, "Transcoder header (%08x) wasn't magic. Abandoning\n", hdr->magic);
185 munmap(hdr, sizeof(*hdr));
186 close(fd);
188 return -1;
191 hdr->srcfmt = (1 << source);
192 hdr->dstfmt = (1 << dest);
193 if (ioctl(fd, ZT_TRANSCODE_OP, &x)) {
194 ast_log(LOG_ERROR, "Unable to attach transcoder: %s\n", strerror(errno));
195 munmap(hdr, sizeof(*hdr));
196 close(fd);
198 return -1;
201 ztp = pvt->pvt;
202 ztp->fd = fd;
203 ztp->hdr = hdr;
205 return 0;
208 static int zap_new(struct ast_trans_pvt *pvt)
210 return zap_translate(pvt, pvt->t->dstfmt, pvt->t->srcfmt);
213 static struct ast_frame *fakesrc_sample(void)
215 /* Don't bother really trying to test hardware ones. */
216 static struct ast_frame f = {
217 .frametype = AST_FRAME_VOICE,
218 .samples = 160,
219 .src = __PRETTY_FUNCTION__
222 return &f;
225 static int register_translator(int dst, int src, void *mod)
227 struct translator *zt;
228 int res;
230 if (!(zt = ast_calloc(1, sizeof(*zt))))
231 return -1;
233 snprintf((char *) (zt->t.name), sizeof(zt->t.name), "zap%sto%s",
234 ast_getformatname((1 << src)), ast_getformatname((1 << dst)));
235 zt->t.srcfmt = (1 << src);
236 zt->t.dstfmt = (1 << dst);
237 zt->t.newpvt = zap_new;
238 zt->t.framein = zap_framein;
239 zt->t.frameout = zap_frameout;
240 zt->t.destroy = zap_destroy;
241 zt->t.sample = fakesrc_sample;
242 zt->t.useplc = global_useplc;
243 zt->t.buf_size = BUFFER_SAMPLES * 2;
244 zt->t.desc_size = sizeof(struct pvt);
245 if ((res = ast_register_translator(&zt->t, mod))) {
246 free(zt);
247 return -1;
250 AST_LIST_LOCK(&translators);
251 AST_LIST_INSERT_HEAD(&translators, zt, entry);
252 AST_LIST_UNLOCK(&translators);
254 global_format_map.map[dst][src] = 1;
256 return res;
259 static void drop_translator(int dst, int src)
261 struct translator *cur;
263 AST_LIST_LOCK(&translators);
264 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
265 if (cur->t.srcfmt != src)
266 continue;
268 if (cur->t.dstfmt != dst)
269 continue;
271 AST_LIST_REMOVE_CURRENT(&translators, entry);
272 ast_unregister_translator(&cur->t);
273 free(cur);
274 global_format_map.map[dst][src] = 0;
275 break;
277 AST_LIST_TRAVERSE_SAFE_END;
278 AST_LIST_UNLOCK(&translators);
281 static void unregister_translators(void)
283 struct translator *cur;
285 AST_LIST_LOCK(&translators);
286 while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
287 ast_unregister_translator(&cur->t);
288 free(cur);
290 AST_LIST_UNLOCK(&translators);
293 static void parse_config(void)
295 struct ast_variable *var;
296 struct ast_config *cfg = ast_config_load("codecs.conf");
298 if (!cfg)
299 return;
301 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
302 if (!strcasecmp(var->name, "genericplc")) {
303 global_useplc = ast_true(var->value);
304 if (option_verbose > 2)
305 ast_verbose(VERBOSE_PREFIX_3 "codec_zap: %susing generic PLC\n",
306 global_useplc ? "" : "not ");
310 ast_config_destroy(cfg);
313 static void build_translators(void *mod, struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
315 unsigned int src, dst;
317 for (src = 0; src < 32; src++) {
318 for (dst = 0; dst < 32; dst++) {
319 if (!(srcfmts & (1 << src)))
320 continue;
322 if (!(dstfmts & (1 << dst)))
323 continue;
325 if (global_format_map.map[dst][src])
326 continue;
328 if (!register_translator(dst, src, mod))
329 map->map[dst][src] = 1;
334 static int find_transcoders(void *mod)
336 struct zt_transcode_info info = { 0, };
337 struct format_map map = { { { 0 } } };
338 int fd, res;
339 unsigned int x, y;
341 info.op = ZT_TCOP_GETINFO;
342 if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0) {
343 ast_log(LOG_NOTICE, "No Zaptel transcoder support!\n");
344 return 0;
346 for (info.tcnum = 0; !(res = ioctl(fd, ZT_TRANSCODE_OP, &info)); info.tcnum++) {
347 if (option_verbose > 1)
348 ast_verbose(VERBOSE_PREFIX_2 "Found transcoder '%s'.\n", info.name);
349 build_translators(mod, &map, info.dstfmts, info.srcfmts);
351 close(fd);
353 if (!info.tcnum && (option_verbose > 1))
354 ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
356 for (x = 0; x < 32; x++) {
357 for (y = 0; y < 32; y++) {
358 if (!map.map[x][y] && global_format_map.map[x][y])
359 drop_translator(x, y);
363 return 0;
366 static int reload(void *mod)
368 struct translator *cur;
370 parse_config();
371 find_transcoders(mod);
373 AST_LIST_LOCK(&translators);
374 AST_LIST_TRAVERSE(&translators, cur, entry)
375 cur->t.useplc = global_useplc;
376 AST_LIST_UNLOCK(&translators);
378 return 0;
381 static int unload_module(void *mod)
383 unregister_translators();
385 return 0;
388 static int load_module(void *mod)
390 parse_config();
391 find_transcoders(mod);
393 return 0;
396 static const char *description(void)
398 return "Generic Zaptel Transcoder Codec Translator";
401 static const char *key(void)
403 return ASTERISK_GPL_KEY;
406 STD_MOD(MOD_1, reload, NULL, NULL);