Debian Codegraph @

codecs/codec_dahdi.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * DAHDI native transcoding support
5 *
6 * Copyright (C) 1999 - 2008, Digium, Inc.
7 *
8 * Mark Spencer <[email protected]>
9 * Kevin P. Fleming <[email protected]>
10 *
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.
16 *
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.
20 */
21
22 /*! \file
23 *
24 * \brief Translate between various formats natively through DAHDI transcoding
25 *
26 * \ingroup codecs
27 */
28
29 /*** MODULEINFO
30 <depend>dahdi</depend>
31 <support_level>core</support_level>
32 ***/
33
34 #include "asterisk.h"
35 #include <stdbool.h>
36
37 #include <poll.h>
38 #include <fcntl.h>
39 #include <netinet/in.h>
40 #include <sys/ioctl.h>
41 #include <sys/mman.h>
42 #include <dahdi/user.h>
43
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"
53 #include "asterisk/format_compatibility.h"
54
55 #define BUFFER_SIZE 8000
56
57 #define G723_SAMPLES 240
58 #define G729_SAMPLES 160
59 #define ULAW_SAMPLES 160
60
61 /* Defines from DAHDI. */
62 #ifndef DAHDI_FORMAT_MAX_AUDIO
63 /*! G.723.1 compression */
64 #define DAHDI_FORMAT_G723_1 (1 << 0)
65 /*! GSM compression */
66 #define DAHDI_FORMAT_GSM (1 << 1)
67 /*! Raw mu-law data (G.711) */
68 #define DAHDI_FORMAT_ULAW (1 << 2)
69 /*! Raw A-law data (G.711) */
70 #define DAHDI_FORMAT_ALAW (1 << 3)
71 /*! ADPCM (G.726, 32kbps) */
72 #define DAHDI_FORMAT_G726 (1 << 4)
73 /*! ADPCM (IMA) */
74 #define DAHDI_FORMAT_ADPCM (1 << 5)
75 /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
76 #define DAHDI_FORMAT_SLINEAR (1 << 6)
77 /*! LPC10, 180 samples/frame */
78 #define DAHDI_FORMAT_LPC10 (1 << 7)
79 /*! G.729A audio */
80 #define DAHDI_FORMAT_G729A (1 << 8)
81 /*! SpeeX Free Compression */
82 #define DAHDI_FORMAT_SPEEX (1 << 9)
83 /*! iLBC Free Compression */
84 #define DAHDI_FORMAT_ILBC (1 << 10)
85 #endif
86
87 static struct channel_usage {
88 int total;
89 int encoders;
90 int decoders;
91 } channels;
92
93 #if defined(NOT_NEEDED)
94 /*!
95 * \internal
96 * \brief Convert DAHDI format bitfield to old Asterisk format bitfield.
97 * \since 13.0.0
98 *
99 * \param dahdi Bitfield from DAHDI to convert.
100 *
101 * \note They should be the same values but they don't have to be.
102 *
103 * \return Old Asterisk bitfield equivalent.
104 */
105 static uint64_t bitfield_dahdi2ast(unsigned dahdi)
106 {
107 uint64_t ast;
108
109 switch (dahdi) {
110 case DAHDI_FORMAT_G723_1:
111 ast = AST_FORMAT_G723;
112 break;
113 case DAHDI_FORMAT_GSM:
114 ast = AST_FORMAT_GSM;
115 break;
116 case DAHDI_FORMAT_ULAW:
117 ast = AST_FORMAT_ULAW;
118 break;
119 case DAHDI_FORMAT_ALAW:
120 ast = AST_FORMAT_ALAW;
121 break;
122 case DAHDI_FORMAT_G726:
123 ast = AST_FORMAT_G726_AAL2;
124 break;
125 case DAHDI_FORMAT_ADPCM:
126 ast = AST_FORMAT_ADPCM;
127 break;
128 case DAHDI_FORMAT_SLINEAR:
129 ast = AST_FORMAT_SLIN;
130 break;
131 case DAHDI_FORMAT_LPC10:
132 ast = AST_FORMAT_LPC10;
133 break;
134 case DAHDI_FORMAT_G729A:
135 ast = AST_FORMAT_G729;
136 break;
137 case DAHDI_FORMAT_SPEEX:
138 ast = AST_FORMAT_SPEEX;
139 break;
140 case DAHDI_FORMAT_ILBC:
141 ast = AST_FORMAT_ILBC;
142 break;
143 default:
144 ast = 0;
145 break;
146 }
147
148 return ast;
149 }
150 #endif /* defined(NOT_NEEDED) */
151
152 /*!
153 * \internal
154 * \brief Get the ast_codec by DAHDI format.
155 * \since 13.0.0
156 *
157 * \param dahdi_fmt DAHDI specific codec identifier.
158 *
159 * \return Specified codec if exists otherwise NULL.
160 */
161 static const struct ast_codec *get_dahdi_codec(uint32_t dahdi_fmt)
162 {
163 const struct ast_codec *codec;
164
165 static const struct ast_codec dahdi_g723_1 = {
166 .name = "g723",
167 .type = AST_MEDIA_TYPE_AUDIO,
168 .sample_rate = 8000,
169 };
170 static const struct ast_codec dahdi_gsm = {
171 .name = "gsm",
172 .type = AST_MEDIA_TYPE_AUDIO,
173 .sample_rate = 8000,
174 };
175 static const struct ast_codec dahdi_ulaw = {
176 .name = "ulaw",
177 .type = AST_MEDIA_TYPE_AUDIO,
178 .sample_rate = 8000,
179 };
180 static const struct ast_codec dahdi_alaw = {
181 .name = "alaw",
182 .type = AST_MEDIA_TYPE_AUDIO,
183 .sample_rate = 8000,
184 };
185 static const struct ast_codec dahdi_g726 = {
186 .name = "g726",
187 .type = AST_MEDIA_TYPE_AUDIO,
188 .sample_rate = 8000,
189 };
190 static const struct ast_codec dahdi_adpcm = {
191 .name = "adpcm",
192 .type = AST_MEDIA_TYPE_AUDIO,
193 .sample_rate = 8000,
194 };
195 static const struct ast_codec dahdi_slinear = {
196 .name = "slin",
197 .type = AST_MEDIA_TYPE_AUDIO,
198 .sample_rate = 8000,
199 };
200 static const struct ast_codec dahdi_lpc10 = {
201 .name = "lpc10",
202 .type = AST_MEDIA_TYPE_AUDIO,
203 .sample_rate = 8000,
204 };
205 static const struct ast_codec dahdi_g729a = {
206 .name = "g729",
207 .type = AST_MEDIA_TYPE_AUDIO,
208 .sample_rate = 8000,
209 };
210 static const struct ast_codec dahdi_speex = {
211 .name = "speex",
212 .type = AST_MEDIA_TYPE_AUDIO,
213 .sample_rate = 8000,
214 };
215 static const struct ast_codec dahdi_ilbc = {
216 .name = "ilbc",
217 .type = AST_MEDIA_TYPE_AUDIO,
218 .sample_rate = 8000,
219 };
220
221 switch (dahdi_fmt) {
222 case DAHDI_FORMAT_G723_1:
223 codec = &dahdi_g723_1;
224 break;
225 case DAHDI_FORMAT_GSM:
226 codec = &dahdi_gsm;
227 break;
228 case DAHDI_FORMAT_ULAW:
229 codec = &dahdi_ulaw;
230 break;
231 case DAHDI_FORMAT_ALAW:
232 codec = &dahdi_alaw;
233 break;
234 case DAHDI_FORMAT_G726:
235 codec = &dahdi_g726;
236 break;
237 case DAHDI_FORMAT_ADPCM:
238 codec = &dahdi_adpcm;
239 break;
240 case DAHDI_FORMAT_SLINEAR:
241 codec = &dahdi_slinear;
242 break;
243 case DAHDI_FORMAT_LPC10:
244 codec = &dahdi_lpc10;
245 break;
246 case DAHDI_FORMAT_G729A:
247 codec = &dahdi_g729a;
248 break;
249 case DAHDI_FORMAT_SPEEX:
250 codec = &dahdi_speex;
251 break;
252 case DAHDI_FORMAT_ILBC:
253 codec = &dahdi_ilbc;
254 break;
255 default:
256 codec = NULL;
257 break;
258 }
259
260 return codec;
261 }
262
263 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
264
265 static struct ast_cli_entry cli[] = {
266 AST_CLI_DEFINE(handle_cli_transcoder_show, "Display DAHDI transcoder utilization.")
267 };
268
269 struct translator {
270 struct ast_translator t;
271 uint32_t src_dahdi_fmt;
272 uint32_t dst_dahdi_fmt;
273 AST_LIST_ENTRY(translator) entry;
274 };
275
276 #ifndef container_of
277 #define container_of(ptr, type, member) \
278 ((type *)((char *)(ptr) - offsetof(type, member)))
279 #endif
280
281 static AST_LIST_HEAD_STATIC(translators, translator);
282
283 struct codec_dahdi_pvt {
284 int fd;
285 struct dahdi_transcoder_formats fmts;
286 unsigned int softslin:1;
287 unsigned int fake:2;
288 uint16_t required_samples;
289 uint16_t samples_in_buffer;
290 uint16_t samples_written_to_hardware;
291 uint8_t ulaw_buffer[1024];
292 };
293
294 /* Only used by a decoder */
295 static int ulawtolin(struct ast_trans_pvt *pvt, int samples)
296 {
297 struct codec_dahdi_pvt *dahdip = pvt->pvt;
298 int i = samples;
299 uint8_t *src = &dahdip->ulaw_buffer[0];
300 int16_t *dst = pvt->outbuf.i16 + pvt->datalen;
301
302 /* convert and copy in outbuf */
303 while (i--) {
304 *dst++ = AST_MULAW(*src++);
305 }
306
307 return 0;
308 }
309
310 /* Only used by an encoder. */
311 static int lintoulaw(struct ast_trans_pvt *pvt, struct ast_frame *f)
312 {
313 struct codec_dahdi_pvt *dahdip = pvt->pvt;
314 int i = f->samples;
315 uint8_t *dst = &dahdip->ulaw_buffer[dahdip->samples_in_buffer];
316 int16_t *src = f->data.ptr;
317
318 if (dahdip->samples_in_buffer + i > sizeof(dahdip->ulaw_buffer)) {
319 ast_log(LOG_ERROR, "Out of buffer space!\n");
320 return -i;
321 }
322
323 while (i--) {
324 *dst++ = AST_LIN2MU(*src++);
325 }
326
327 dahdip->samples_in_buffer += f->samples;
328 return 0;
329 }
330
331 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
332 {
333 struct channel_usage copy;
334
335 switch (cmd) {
336 case CLI_INIT:
337 e->command = "transcoder show";
338 e->usage =
339 "Usage: transcoder show\n"
340 " Displays channel utilization of DAHDI transcoder(s).\n";
341 return NULL;
342 case CLI_GENERATE:
343 return NULL;
344 }
345
346 if (a->argc != 2)
347 return CLI_SHOWUSAGE;
348
349 copy = channels;
350
351 if (copy.total == 0)
352 ast_cli(a->fd, "No DAHDI transcoders found.\n");
353 else
354 ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
355
356 return CLI_SUCCESS;
357 }
358
359 static void dahdi_write_frame(struct codec_dahdi_pvt *dahdip, const uint8_t *buffer, const ssize_t count)
360 {
361 int res;
362 if (!count) return;
363 res = write(dahdip->fd, buffer, count);
364 if (-1 == res) {
365 ast_log(LOG_ERROR, "Failed to write to transcoder: %s\n", strerror(errno));
366 }
367 if (count != res) {
368 ast_log(LOG_ERROR, "Requested write of %zd bytes, but only wrote %d bytes.\n", count, res);
369 }
370 }
371
372 static int dahdi_encoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
373 {
374 struct codec_dahdi_pvt *dahdip = pvt->pvt;
375
376 if (!f->subclass.format) {
377 /* We're just faking a return for calculation purposes. */
378 dahdip->fake = 2;
379 pvt->samples = f->samples;
380 return 0;
381 }
382
383 /* Buffer up the packets and send them to the hardware if we
384 * have enough samples set up. */
385 if (dahdip->softslin) {
386 if (lintoulaw(pvt, f)) {
387 return -1;
388 }
389 } else {
390 /* NOTE: If softslin support is not needed, and the sample
391 * size is equal to the required sample size, we wouldn't
392 * need this copy operation. But at the time this was
393 * written, only softslin is supported. */
394 if (dahdip->samples_in_buffer + f->samples > sizeof(dahdip->ulaw_buffer)) {
395 ast_log(LOG_ERROR, "Out of buffer space.\n");
396 return -1;
397 }
398 memcpy(&dahdip->ulaw_buffer[dahdip->samples_in_buffer], f->data.ptr, f->samples);
399 dahdip->samples_in_buffer += f->samples;
400 }
401
402 while (dahdip->samples_in_buffer >= dahdip->required_samples) {
403 dahdi_write_frame(dahdip, dahdip->ulaw_buffer, dahdip->required_samples);
404 dahdip->samples_written_to_hardware += dahdip->required_samples;
405 dahdip->samples_in_buffer -= dahdip->required_samples;
406 if (dahdip->samples_in_buffer) {
407 /* Shift any remaining bytes down. */
408 memmove(dahdip->ulaw_buffer, &dahdip->ulaw_buffer[dahdip->required_samples],
409 dahdip->samples_in_buffer);
410 }
411 }
412 pvt->samples += f->samples;
413 pvt->datalen = 0;
414 return -1;
415 }
416
417 static void dahdi_wait_for_packet(int fd)
418 {
419 struct pollfd p = {0};
420 p.fd = fd;
421 p.events = POLLIN;
422 poll(&p, 1, 10);
423 }
424
425 static struct ast_frame *dahdi_encoder_frameout(struct ast_trans_pvt *pvt)
426 {
427 struct codec_dahdi_pvt *dahdip = pvt->pvt;
428 int res;
429
430 if (2 == dahdip->fake) {
431 struct ast_frame frm = {
432 .frametype = AST_FRAME_VOICE,
433 .samples = dahdip->required_samples,
434 .src = pvt->t->name,
435 };
436
437 dahdip->fake = 1;
438 pvt->samples = 0;
439
440 return ast_frisolate(&frm);
441 } else if (1 == dahdip->fake) {
442 dahdip->fake = 0;
443 return NULL;
444 }
445
446 if (dahdip->samples_written_to_hardware >= dahdip->required_samples) {
447 dahdi_wait_for_packet(dahdip->fd);
448 }
449
450 res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
451 if (-1 == res) {
452 if (EWOULDBLOCK == errno) {
453 /* Nothing waiting... */
454 return NULL;
455 } else {
456 ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
457 return NULL;
458 }
459 } else {
460 pvt->f.datalen = res;
461 pvt->f.samples = ast_codec_samples_count(&pvt->f);
462
463 dahdip->samples_written_to_hardware =
464 (dahdip->samples_written_to_hardware >= pvt->f.samples) ?
465 dahdip->samples_written_to_hardware - pvt->f.samples : 0;
466
467 pvt->samples = 0;
468 pvt->datalen = 0;
469 return ast_frisolate(&pvt->f);
470 }
471
472 /* Shouldn't get here... */
473 return NULL;
474 }
475
476 static int dahdi_decoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
477 {
478 struct codec_dahdi_pvt *dahdip = pvt->pvt;
479
480 if (!f->subclass.format) {
481 /* We're just faking a return for calculation purposes. */
482 dahdip->fake = 2;
483 pvt->samples = f->samples;
484 return 0;
485 }
486
487 if (!f->datalen) {
488 if (f->samples != dahdip->required_samples) {
489 ast_log(LOG_ERROR, "%d != %d %d\n", f->samples, dahdip->required_samples, f->datalen);
490 }
491 }
492 dahdi_write_frame(dahdip, f->data.ptr, f->datalen);
493 dahdip->samples_written_to_hardware += f->samples;
494 pvt->samples += f->samples;
495 pvt->datalen = 0;
496 return -1;
497 }
498
499 static struct ast_frame *dahdi_decoder_frameout(struct ast_trans_pvt *pvt)
500 {
501 int res;
502 struct codec_dahdi_pvt *dahdip = pvt->pvt;
503
504 if (2 == dahdip->fake) {
505 struct ast_frame frm = {
506 .frametype = AST_FRAME_VOICE,
507 .samples = dahdip->required_samples,
508 .src = pvt->t->name,
509 };
510
511 dahdip->fake = 1;
512 pvt->samples = 0;
513
514 return ast_frisolate(&frm);
515 } else if (1 == dahdip->fake) {
516 pvt->samples = 0;
517 dahdip->fake = 0;
518 return NULL;
519 }
520
521 if (dahdip->samples_written_to_hardware >= ULAW_SAMPLES) {
522 dahdi_wait_for_packet(dahdip->fd);
523 }
524
525 /* Let's check to see if there is a new frame for us.... */
526 if (dahdip->softslin) {
527 res = read(dahdip->fd, dahdip->ulaw_buffer, sizeof(dahdip->ulaw_buffer));
528 } else {
529 res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
530 }
531
532 if (-1 == res) {
533 if (EWOULDBLOCK == errno) {
534 /* Nothing waiting... */
535 return NULL;
536 } else {
537 ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
538 return NULL;
539 }
540 } else {
541 if (dahdip->softslin) {
542 ulawtolin(pvt, res);
543 pvt->f.datalen = res * 2;
544 } else {
545 pvt->f.datalen = res;
546 }
547 pvt->datalen = 0;
548 pvt->f.samples = res;
549 pvt->samples = 0;
550 dahdip->samples_written_to_hardware =
551 (dahdip->samples_written_to_hardware >= res) ?
552 dahdip->samples_written_to_hardware - res : 0;
553
554 return ast_frisolate(&pvt->f);
555 }
556
557 /* Shouldn't get here... */
558 return NULL;
559 }
560
561
562 static void dahdi_destroy(struct ast_trans_pvt *pvt)
563 {
564 struct codec_dahdi_pvt *dahdip = pvt->pvt;
565
566 switch (dahdip->fmts.dstfmt) {
567 case DAHDI_FORMAT_G729A:
568 case DAHDI_FORMAT_G723_1:
569 ast_atomic_fetchadd_int(&channels.encoders, -1);
570 break;
571 default:
572 ast_atomic_fetchadd_int(&channels.decoders, -1);
573 break;
574 }
575
576 close(dahdip->fd);
577 }
578
579 static struct ast_format *dahdi_format_to_cached(int format)
580 {
581 switch (format) {
582 case DAHDI_FORMAT_G723_1:
583 return ast_format_g723;
584 case DAHDI_FORMAT_GSM:
585 return ast_format_gsm;
586 case DAHDI_FORMAT_ULAW:
587 return ast_format_ulaw;
588 case DAHDI_FORMAT_ALAW:
589 return ast_format_alaw;
590 case DAHDI_FORMAT_G726:
591 return ast_format_g726;
592 case DAHDI_FORMAT_ADPCM:
593 return ast_format_adpcm;
594 case DAHDI_FORMAT_SLINEAR:
595 return ast_format_slin;
596 case DAHDI_FORMAT_LPC10:
597 return ast_format_lpc10;
598 case DAHDI_FORMAT_G729A:
599 return ast_format_g729;
600 case DAHDI_FORMAT_SPEEX:
601 return ast_format_speex;
602 case DAHDI_FORMAT_ILBC:
603 return ast_format_ilbc;
604 }
605
606 /* This will never be reached */
607 ast_assert(0);
608 return NULL;
609 }
610
611 static int dahdi_translate(struct ast_trans_pvt *pvt, uint32_t dst_dahdi_fmt, uint32_t src_dahdi_fmt)
612 {
613 /* Request translation through zap if possible */
614 int fd;
615 struct codec_dahdi_pvt *dahdip = pvt->pvt;
616 int tried_once = 0;
617 const char *dev_filename = "/dev/dahdi/transcode";
618
619 if ((fd = open(dev_filename, O_RDWR)) < 0) {
620 ast_log(LOG_ERROR, "Failed to open %s: %s\n", dev_filename, strerror(errno));
621 return -1;
622 }
623
624 dahdip->fmts.srcfmt = src_dahdi_fmt;
625 dahdip->fmts.dstfmt = dst_dahdi_fmt;
626
627 ast_assert(pvt->f.subclass.format == NULL);
628 pvt->f.subclass.format = ao2_bump(dahdi_format_to_cached(dahdip->fmts.dstfmt));
629
630 ast_debug(1, "Opening transcoder channel from %s to %s.\n", pvt->t->src_codec.name, pvt->t->dst_codec.name);
631
632 retry:
633 if (ioctl(fd, DAHDI_TC_ALLOCATE, &dahdip->fmts)) {
634 if ((ENODEV == errno) && !tried_once) {
635 /* We requested to translate to/from an unsupported
636 * format. Most likely this is because signed linear
637 * was not supported by any hardware devices even
638 * though this module always registers signed linear
639 * support. In this case we'll retry, requesting
640 * support for ULAW instead of signed linear and then
641 * we'll just convert from ulaw to signed linear in
642 * software. */
643 if (dahdip->fmts.srcfmt == DAHDI_FORMAT_SLINEAR) {
644 ast_debug(1, "Using soft_slin support on source\n");
645 dahdip->softslin = 1;
646 dahdip->fmts.srcfmt = DAHDI_FORMAT_ULAW;
647 } else if (dahdip->fmts.dstfmt == DAHDI_FORMAT_SLINEAR) {
648 ast_debug(1, "Using soft_slin support on destination\n");
649 dahdip->softslin = 1;
650 dahdip->fmts.dstfmt = DAHDI_FORMAT_ULAW;
651 }
652 tried_once = 1;
653 goto retry;
654 }
655 ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
656 close(fd);
657
658 return -1;
659 }
660
661 ast_fd_set_flags(fd, O_NONBLOCK);
662
663 dahdip->fd = fd;
664
665 dahdip->required_samples = ((dahdip->fmts.dstfmt|dahdip->fmts.srcfmt) & (DAHDI_FORMAT_G723_1)) ? G723_SAMPLES : G729_SAMPLES;
666
667 switch (dahdip->fmts.dstfmt) {
668 case DAHDI_FORMAT_G729A:
669 ast_atomic_fetchadd_int(&channels.encoders, +1);
670 break;
671 case DAHDI_FORMAT_G723_1:
672 ast_atomic_fetchadd_int(&channels.encoders, +1);
673 break;
674 default:
675 ast_atomic_fetchadd_int(&channels.decoders, +1);
676 break;
677 }
678
679 return 0;
680 }
681
682 static int dahdi_new(struct ast_trans_pvt *pvt)
683 {
684 struct translator *zt = container_of(pvt->t, struct translator, t);
685
686 return dahdi_translate(pvt, zt->dst_dahdi_fmt, zt->src_dahdi_fmt);
687 }
688
689 static struct ast_frame *fakesrc_sample(void)
690 {
691 /* Don't bother really trying to test hardware ones. */
692 static struct ast_frame f = {
693 .frametype = AST_FRAME_VOICE,
694 .samples = 160,
695 .src = __PRETTY_FUNCTION__
696 };
697
698 return &f;
699 }
700
701 static bool is_encoder(uint32_t src_dahdi_fmt)
702 {
703 return ((src_dahdi_fmt & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW | DAHDI_FORMAT_SLINEAR)) > 0);
704 }
705
706 /* Must be called with the translators list locked. */
707 static int register_translator(uint32_t dst_dahdi_fmt, uint32_t src_dahdi_fmt)
708 {
709 const struct ast_codec *dst_codec;
710 const struct ast_codec *src_codec;
711 struct translator *zt;
712 int res;
713
714 dst_codec = get_dahdi_codec(dst_dahdi_fmt);
715 src_codec = get_dahdi_codec(src_dahdi_fmt);
716 if (!dst_codec || !src_codec) {
717 return -1;
718 }
719
720 if (!(zt = ast_calloc(1, sizeof(*zt)))) {
721 return -1;
722 }
723
724 zt->src_dahdi_fmt = src_dahdi_fmt;
725 zt->dst_dahdi_fmt = dst_dahdi_fmt;
726
727 snprintf(zt->t.name, sizeof(zt->t.name), "dahdi_%s_to_%s",
728 src_codec->name, dst_codec->name);
729
730 memcpy(&zt->t.src_codec, src_codec, sizeof(*src_codec));
731 memcpy(&zt->t.dst_codec, dst_codec, sizeof(*dst_codec));
732 zt->t.buf_size = BUFFER_SIZE;
733 if (is_encoder(src_dahdi_fmt)) {
734 zt->t.framein = dahdi_encoder_framein;
735 zt->t.frameout = dahdi_encoder_frameout;
736 } else {
737 zt->t.framein = dahdi_decoder_framein;
738 zt->t.frameout = dahdi_decoder_frameout;
739 }
740 zt->t.destroy = dahdi_destroy;
741 zt->t.buffer_samples = 0;
742 zt->t.newpvt = dahdi_new;
743 zt->t.sample = fakesrc_sample;
744 zt->t.native_plc = 0;
745
746 zt->t.desc_size = sizeof(struct codec_dahdi_pvt);
747 if ((res = ast_register_translator(&zt->t))) {
748 ast_free(zt);
749 return -1;
750 }
751
752 AST_LIST_INSERT_HEAD(&translators, zt, entry);
753
754 return res;
755 }
756
757 static void unregister_translators(void)
758 {
759 struct translator *cur;
760
761 AST_LIST_LOCK(&translators);
762 while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
763 ast_unregister_translator(&cur->t);
764 ast_free(cur);
765 }
766 AST_LIST_UNLOCK(&translators);
767 }
768
769 /* Must be called with the translators list locked. */
770 static bool is_already_registered(uint32_t dstfmt, uint32_t srcfmt)
771 {
772 bool res = false;
773 const struct translator *zt;
774
775 AST_LIST_TRAVERSE(&translators, zt, entry) {
776 if ((zt->src_dahdi_fmt == srcfmt) && (zt->dst_dahdi_fmt == dstfmt)) {
777 res = true;
778 break;
779 }
780 }
781
782 return res;
783 }
784
785 static void build_translators(uint32_t dstfmts, uint32_t srcfmts)
786 {
787 uint32_t srcfmt;
788 uint32_t dstfmt;
789
790 AST_LIST_LOCK(&translators);
791
792 for (srcfmt = 1; srcfmt != 0; srcfmt <<= 1) {
793 for (dstfmt = 1; dstfmt != 0; dstfmt <<= 1) {
794 if (!(dstfmts & dstfmt) || !(srcfmts & srcfmt)) {
795 continue;
796 }
797 if (is_already_registered(dstfmt, srcfmt)) {
798 continue;
799 }
800 register_translator(dstfmt, srcfmt);
801 }
802 }
803
804 AST_LIST_UNLOCK(&translators);
805 }
806
807 static int find_transcoders(void)
808 {
809 struct dahdi_transcoder_info info = { 0, };
810 int fd;
811
812 if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
813 ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
814 return 0;
815 }
816
817 for (info.tcnum = 0; !ioctl(fd, DAHDI_TC_GETINFO, &info); info.tcnum++) {
818 ast_verb(2, "Found transcoder '%s'.\n", info.name);
819
820 /* Complex codecs need to support signed linear. If the
821 * hardware transcoder does not natively support signed linear
822 * format, we will emulate it in software directly in this
823 * module. Also, do not allow direct ulaw/alaw to complex
824 * codec translation, since that will prevent the generic PLC
825 * functions from working. */
826 if (info.dstfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
827 info.dstfmts |= DAHDI_FORMAT_SLINEAR;
828 info.dstfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
829 }
830 if (info.srcfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
831 info.srcfmts |= DAHDI_FORMAT_SLINEAR;
832 info.srcfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
833 }
834
835 build_translators(info.dstfmts, info.srcfmts);
836 ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
837 }
838
839 close(fd);
840
841 if (!info.tcnum) {
842 ast_verb(2, "No hardware transcoders found.\n");
843 }
844
845 return 0;
846 }
847
848 static int reload(void)
849 {
850 return AST_MODULE_LOAD_SUCCESS;
851 }
852
853 static int unload_module(void)
854 {
855 ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
856 unregister_translators();
857
858 return 0;
859 }
860
861 static int load_module(void)
862 {
863 find_transcoders();
864 ast_cli_register_multiple(cli, ARRAY_LEN(cli));
865 return AST_MODULE_LOAD_SUCCESS;
866 }
867
868 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic DAHDI Transcoder Codec Translator",
869 .support_level = AST_MODULE_SUPPORT_CORE,
870 .load = load_module,
871 .unload = unload_module,
872 .reload = reload,
873 );