Fix end condition in ast_rtp_lookup_mime_multiple2.
[asterisk/asterisk.git] / codecs / codec_dahdi.c
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 <markster@digium.com>
9  * Kevin P. Fleming <kpfleming@digium.com>
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         <support_level>core</support_level>
31         <depend>dahdi</depend>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include <fcntl.h>
39 #include <netinet/in.h>
40 #include <sys/ioctl.h>
41 #include <sys/mman.h>
42 #include <sys/poll.h>
43 #include <dahdi/user.h>
44
45 #include "asterisk/lock.h"
46 #include "asterisk/translate.h"
47 #include "asterisk/config.h"
48 #include "asterisk/module.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/linkedlists.h"
53 #include "asterisk/ulaw.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 #ifndef DAHDI_FORMAT_MAX_AUDIO
62 #define DAHDI_FORMAT_G723_1    (1 << 0)
63 #define DAHDI_FORMAT_GSM       (1 << 1)
64 #define DAHDI_FORMAT_ULAW      (1 << 2)
65 #define DAHDI_FORMAT_ALAW      (1 << 3)
66 #define DAHDI_FORMAT_G726      (1 << 4)
67 #define DAHDI_FORMAT_ADPCM     (1 << 5)
68 #define DAHDI_FORMAT_SLINEAR   (1 << 6)
69 #define DAHDI_FORMAT_LPC10     (1 << 7)
70 #define DAHDI_FORMAT_G729A     (1 << 8)
71 #define DAHDI_FORMAT_SPEEX     (1 << 9)
72 #define DAHDI_FORMAT_ILBC      (1 << 10)
73 #endif
74
75 static struct channel_usage {
76         int total;
77         int encoders;
78         int decoders;
79 } channels;
80
81 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
82
83 static struct ast_cli_entry cli[] = {
84         AST_CLI_DEFINE(handle_cli_transcoder_show, "Display DAHDI transcoder utilization.")
85 };
86
87 struct format_map {
88         unsigned int map[32][32];
89 };
90
91 static struct format_map global_format_map = { { { 0 } } };
92
93 struct translator {
94         struct ast_translator t;
95         AST_LIST_ENTRY(translator) entry;
96 };
97
98 static AST_LIST_HEAD_STATIC(translators, translator);
99
100 struct codec_dahdi_pvt {
101         int fd;
102         struct dahdi_transcoder_formats fmts;
103         unsigned int softslin:1;
104         unsigned int fake:2;
105         uint16_t required_samples;
106         uint16_t samples_in_buffer;
107         uint16_t samples_written_to_hardware;
108         uint8_t ulaw_buffer[1024];
109 };
110
111 /* Only used by a decoder */
112 static int ulawtolin(struct ast_trans_pvt *pvt, int samples)
113 {
114         struct codec_dahdi_pvt *dahdip = pvt->pvt;
115         int i = samples;
116         uint8_t *src = &dahdip->ulaw_buffer[0];
117         int16_t *dst = pvt->outbuf.i16 + pvt->datalen;
118
119         /* convert and copy in outbuf */
120         while (i--) {
121                 *dst++ = AST_MULAW(*src++);
122         }
123
124         return 0;
125 }
126
127 /* Only used by an encoder. */
128 static int lintoulaw(struct ast_trans_pvt *pvt, struct ast_frame *f)
129 {
130         struct codec_dahdi_pvt *dahdip = pvt->pvt;
131         int i = f->samples;
132         uint8_t *dst = &dahdip->ulaw_buffer[dahdip->samples_in_buffer];
133         int16_t *src = f->data.ptr;
134
135         if (dahdip->samples_in_buffer + i > sizeof(dahdip->ulaw_buffer)) {
136                 ast_log(LOG_ERROR, "Out of buffer space!\n");
137                 return -i;
138         }
139
140         while (i--) {
141                 *dst++ = AST_LIN2MU(*src++);
142         }
143
144         dahdip->samples_in_buffer += f->samples;
145         return 0;
146 }
147
148 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
149 {
150         struct channel_usage copy;
151
152         switch (cmd) {
153         case CLI_INIT:
154                 e->command = "transcoder show";
155                 e->usage =
156                         "Usage: transcoder show\n"
157                         "       Displays channel utilization of DAHDI transcoder(s).\n";
158                 return NULL;
159         case CLI_GENERATE:
160                 return NULL;
161         }
162
163         if (a->argc != 2)
164                 return CLI_SHOWUSAGE;
165
166         copy = channels;
167
168         if (copy.total == 0)
169                 ast_cli(a->fd, "No DAHDI transcoders found.\n");
170         else
171                 ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
172
173         return CLI_SUCCESS;
174 }
175
176 static void dahdi_write_frame(struct codec_dahdi_pvt *dahdip, const uint8_t *buffer, const ssize_t count)
177 {
178         int res;
179         if (!count) return;
180         res = write(dahdip->fd, buffer, count);
181         if (-1 == res) {
182                 ast_log(LOG_ERROR, "Failed to write to transcoder: %s\n", strerror(errno));
183         }
184         if (count != res) {
185                 ast_log(LOG_ERROR, "Requested write of %zd bytes, but only wrote %d bytes.\n", count, res);
186         }
187 }
188
189 static int dahdi_encoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
190 {
191         struct codec_dahdi_pvt *dahdip = pvt->pvt;
192
193         if (!f->subclass.format.id) {
194                 /* We're just faking a return for calculation purposes. */
195                 dahdip->fake = 2;
196                 pvt->samples = f->samples;
197                 return 0;
198         }
199
200         /* Buffer up the packets and send them to the hardware if we
201          * have enough samples set up. */
202         if (dahdip->softslin) {
203                 if (lintoulaw(pvt, f)) {
204                          return -1;
205                 }
206         } else {
207                 /* NOTE:  If softslin support is not needed, and the sample
208                  * size is equal to the required sample size, we wouldn't
209                  * need this copy operation.  But at the time this was
210                  * written, only softslin is supported. */
211                 if (dahdip->samples_in_buffer + f->samples > sizeof(dahdip->ulaw_buffer)) {
212                         ast_log(LOG_ERROR, "Out of buffer space.\n");
213                         return -1;
214                 }
215                 memcpy(&dahdip->ulaw_buffer[dahdip->samples_in_buffer], f->data.ptr, f->samples);
216                 dahdip->samples_in_buffer += f->samples;
217         }
218
219         while (dahdip->samples_in_buffer >= dahdip->required_samples) {
220                 dahdi_write_frame(dahdip, dahdip->ulaw_buffer, dahdip->required_samples);
221                 dahdip->samples_written_to_hardware += dahdip->required_samples;
222                 dahdip->samples_in_buffer -= dahdip->required_samples;
223                 if (dahdip->samples_in_buffer) {
224                         /* Shift any remaining bytes down. */
225                         memmove(dahdip->ulaw_buffer, &dahdip->ulaw_buffer[dahdip->required_samples],
226                                 dahdip->samples_in_buffer);
227                 }
228         }
229         pvt->samples += f->samples;
230         pvt->datalen = 0;
231         return -1;
232 }
233
234 static void dahdi_wait_for_packet(int fd)
235 {
236         struct pollfd p = {0};
237         p.fd = fd;
238         p.events = POLLIN;
239         poll(&p, 1, 10);
240 }
241
242 static struct ast_frame *dahdi_encoder_frameout(struct ast_trans_pvt *pvt)
243 {
244         struct codec_dahdi_pvt *dahdip = pvt->pvt;
245         int res;
246
247         if (2 == dahdip->fake) {
248                 dahdip->fake = 1;
249                 pvt->f.frametype = AST_FRAME_VOICE;
250                 ast_format_clear(&pvt->f.subclass.format);
251                 pvt->f.samples = dahdip->required_samples;
252                 pvt->f.data.ptr = NULL;
253                 pvt->f.offset = 0;
254                 pvt->f.datalen = 0;
255                 pvt->f.mallocd = 0;
256                 pvt->samples = 0;
257
258                 return ast_frisolate(&pvt->f);
259
260         } else if (1 == dahdip->fake) {
261                 dahdip->fake = 0;
262                 return NULL;
263         }
264
265         if (dahdip->samples_written_to_hardware >= dahdip->required_samples) {
266                 dahdi_wait_for_packet(dahdip->fd);
267         }
268
269         res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
270         if (-1 == res) {
271                 if (EWOULDBLOCK == errno) {
272                         /* Nothing waiting... */
273                         return NULL;
274                 } else {
275                         ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
276                         return NULL;
277                 }
278         } else {
279                 pvt->f.datalen = res;
280                 pvt->f.frametype = AST_FRAME_VOICE;
281                 ast_format_copy(&pvt->f.subclass.format, &pvt->t->dst_format);
282                 pvt->f.mallocd = 0;
283                 pvt->f.offset = AST_FRIENDLY_OFFSET;
284                 pvt->f.src = pvt->t->name;
285                 pvt->f.data.ptr = pvt->outbuf.c;
286                 pvt->f.samples = ast_codec_get_samples(&pvt->f);
287
288                 dahdip->samples_written_to_hardware =
289                   (dahdip->samples_written_to_hardware >= pvt->f.samples) ?
290                      dahdip->samples_written_to_hardware - pvt->f.samples : 0;
291
292                 pvt->samples = 0;
293                 pvt->datalen = 0;
294                 return ast_frisolate(&pvt->f);
295         }
296
297         /* Shouldn't get here... */
298         return NULL;
299 }
300
301 static int dahdi_decoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
302 {
303         struct codec_dahdi_pvt *dahdip = pvt->pvt;
304
305         if (!f->subclass.format.id) {
306                 /* We're just faking a return for calculation purposes. */
307                 dahdip->fake = 2;
308                 pvt->samples = f->samples;
309                 return 0;
310         }
311
312         if (!f->datalen) {
313                 if (f->samples != dahdip->required_samples) {
314                         ast_log(LOG_ERROR, "%d != %d %d\n", f->samples, dahdip->required_samples, f->datalen);
315                 }
316         }
317         dahdi_write_frame(dahdip, f->data.ptr, f->datalen);
318         dahdip->samples_written_to_hardware += f->samples;
319         pvt->samples += f->samples;
320         pvt->datalen = 0;
321         return -1;
322 }
323
324 static struct ast_frame *dahdi_decoder_frameout(struct ast_trans_pvt *pvt)
325 {
326         int res;
327         struct codec_dahdi_pvt *dahdip = pvt->pvt;
328
329         if (2 == dahdip->fake) {
330                 dahdip->fake = 1;
331                 pvt->f.frametype = AST_FRAME_VOICE;
332                 ast_format_clear(&pvt->f.subclass.format);
333                 pvt->f.samples = dahdip->required_samples;
334                 pvt->f.data.ptr = NULL;
335                 pvt->f.offset = 0;
336                 pvt->f.datalen = 0;
337                 pvt->f.mallocd = 0;
338                 pvt->samples = 0;
339                 return ast_frisolate(&pvt->f);
340         } else if (1 == dahdip->fake) {
341                 pvt->samples = 0;
342                 dahdip->fake = 0;
343                 return NULL;
344         }
345
346         if (dahdip->samples_written_to_hardware >= ULAW_SAMPLES) {
347                 dahdi_wait_for_packet(dahdip->fd);
348         }
349
350         /* Let's check to see if there is a new frame for us.... */
351         if (dahdip->softslin) {
352                 res = read(dahdip->fd, dahdip->ulaw_buffer, sizeof(dahdip->ulaw_buffer));
353         } else {
354                 res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
355         }
356
357         if (-1 == res) {
358                 if (EWOULDBLOCK == errno) {
359                         /* Nothing waiting... */
360                         return NULL;
361                 } else {
362                         ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
363                         return NULL;
364                 }
365         } else {
366                 if (dahdip->softslin) {
367                         ulawtolin(pvt, res);
368                         pvt->f.datalen = res * 2;
369                 } else {
370                         pvt->f.datalen = res;
371                 }
372                 pvt->datalen = 0;
373                 pvt->f.frametype = AST_FRAME_VOICE;
374                 ast_format_copy(&pvt->f.subclass.format, &pvt->t->dst_format);
375                 pvt->f.mallocd = 0;
376                 pvt->f.offset = AST_FRIENDLY_OFFSET;
377                 pvt->f.src = pvt->t->name;
378                 pvt->f.data.ptr = pvt->outbuf.c;
379                 pvt->f.samples = res;
380                 pvt->samples = 0;
381                 dahdip->samples_written_to_hardware =
382                         (dahdip->samples_written_to_hardware >= res) ?
383                                 dahdip->samples_written_to_hardware - res : 0;
384
385                 return ast_frisolate(&pvt->f);
386         }
387
388         /* Shouldn't get here... */
389         return NULL;
390 }
391
392
393 static void dahdi_destroy(struct ast_trans_pvt *pvt)
394 {
395         struct codec_dahdi_pvt *dahdip = pvt->pvt;
396
397         switch (ast_format_id_from_old_bitfield(dahdip->fmts.dstfmt)) {
398         case AST_FORMAT_G729A:
399         case AST_FORMAT_G723_1:
400                 ast_atomic_fetchadd_int(&channels.encoders, -1);
401                 break;
402         default:
403                 ast_atomic_fetchadd_int(&channels.decoders, -1);
404                 break;
405         }
406
407         close(dahdip->fd);
408 }
409
410 static int dahdi_translate(struct ast_trans_pvt *pvt, struct ast_format *dst_format, struct ast_format *src_format)
411 {
412         /* Request translation through zap if possible */
413         int fd;
414         struct codec_dahdi_pvt *dahdip = pvt->pvt;
415         int flags;
416         int tried_once = 0;
417         const char *dev_filename = "/dev/dahdi/transcode";
418
419         if ((fd = open(dev_filename, O_RDWR)) < 0) {
420                 ast_log(LOG_ERROR, "Failed to open %s: %s\n", dev_filename, strerror(errno));
421                 return -1;
422         }
423
424         dahdip->fmts.srcfmt = ast_format_to_old_bitfield(src_format);
425         dahdip->fmts.dstfmt = ast_format_to_old_bitfield(dst_format);
426
427         ast_debug(1, "Opening transcoder channel from %s to %s.\n", ast_getformatname(src_format), ast_getformatname(dst_format));
428
429 retry:
430         if (ioctl(fd, DAHDI_TC_ALLOCATE, &dahdip->fmts)) {
431                 if ((ENODEV == errno) && !tried_once) {
432                         /* We requested to translate to/from an unsupported
433                          * format.  Most likely this is because signed linear
434                          * was not supported by any hardware devices even
435                          * though this module always registers signed linear
436                          * support. In this case we'll retry, requesting
437                          * support for ULAW instead of signed linear and then
438                          * we'll just convert from ulaw to signed linear in
439                          * software. */
440                         if (AST_FORMAT_SLINEAR == ast_format_id_from_old_bitfield(dahdip->fmts.srcfmt)) {
441                                 ast_debug(1, "Using soft_slin support on source\n");
442                                 dahdip->softslin = 1;
443                                 dahdip->fmts.srcfmt = ast_format_id_to_old_bitfield(AST_FORMAT_ULAW);
444                         } else if (AST_FORMAT_SLINEAR == ast_format_id_from_old_bitfield(dahdip->fmts.dstfmt)) {
445                                 ast_debug(1, "Using soft_slin support on destination\n");
446                                 dahdip->softslin = 1;
447                                 dahdip->fmts.dstfmt = ast_format_id_to_old_bitfield(AST_FORMAT_ULAW);
448                         }
449                         tried_once = 1;
450                         goto retry;
451                 }
452                 ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
453                 close(fd);
454
455                 return -1;
456         }
457
458         flags = fcntl(fd, F_GETFL);
459         if (flags > - 1) {
460                 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
461                         ast_log(LOG_WARNING, "Could not set non-block mode!\n");
462         }
463
464         dahdip->fd = fd;
465
466         dahdip->required_samples = ((dahdip->fmts.dstfmt|dahdip->fmts.srcfmt) & (ast_format_id_to_old_bitfield(AST_FORMAT_G723_1))) ? G723_SAMPLES : G729_SAMPLES;
467
468         switch (ast_format_id_from_old_bitfield(dahdip->fmts.dstfmt)) {
469         case AST_FORMAT_G729A:
470                 ast_atomic_fetchadd_int(&channels.encoders, +1);
471                 break;
472         case AST_FORMAT_G723_1:
473                 ast_atomic_fetchadd_int(&channels.encoders, +1);
474                 break;
475         default:
476                 ast_atomic_fetchadd_int(&channels.decoders, +1);
477                 break;
478         }
479
480         return 0;
481 }
482
483 static int dahdi_new(struct ast_trans_pvt *pvt)
484 {
485         return dahdi_translate(pvt,
486                 &pvt->t->dst_format,
487                 &pvt->t->src_format);
488 }
489
490 static struct ast_frame *fakesrc_sample(void)
491 {
492         /* Don't bother really trying to test hardware ones. */
493         static struct ast_frame f = {
494                 .frametype = AST_FRAME_VOICE,
495                 .samples = 160,
496                 .src = __PRETTY_FUNCTION__
497         };
498
499         return &f;
500 }
501
502 static int is_encoder(struct translator *zt)
503 {
504         if ((zt->t.src_format.id == AST_FORMAT_ULAW) ||
505                 (zt->t.src_format.id == AST_FORMAT_ALAW) ||
506                 (zt->t.src_format.id == AST_FORMAT_SLINEAR)) {
507                 return 1;
508         } else {
509                 return 0;
510         }
511 }
512
513 static int register_translator(int dst, int src)
514 {
515         struct translator *zt;
516         int res;
517         struct ast_format dst_format;
518         struct ast_format src_format;
519
520         ast_format_from_old_bitfield(&dst_format, (1 << dst));
521         ast_format_from_old_bitfield(&src_format, (1 << src));
522
523         if (!(zt = ast_calloc(1, sizeof(*zt)))) {
524                 return -1;
525         }
526
527         snprintf((char *) (zt->t.name), sizeof(zt->t.name), "zap%sto%s",
528                  ast_getformatname(&src_format), ast_getformatname(&dst_format));
529         ast_format_copy(&zt->t.src_format, &src_format);
530         ast_format_copy(&zt->t.dst_format, &dst_format);
531         zt->t.buf_size = BUFFER_SIZE;
532         if (is_encoder(zt)) {
533                 zt->t.framein = dahdi_encoder_framein;
534                 zt->t.frameout = dahdi_encoder_frameout;
535         } else {
536                 zt->t.framein = dahdi_decoder_framein;
537                 zt->t.frameout = dahdi_decoder_frameout;
538         }
539         zt->t.destroy = dahdi_destroy;
540         zt->t.buffer_samples = 0;
541         zt->t.newpvt = dahdi_new;
542         zt->t.sample = fakesrc_sample;
543         zt->t.native_plc = 0;
544
545         zt->t.desc_size = sizeof(struct codec_dahdi_pvt);
546         if ((res = ast_register_translator(&zt->t))) {
547                 ast_free(zt);
548                 return -1;
549         }
550
551         AST_LIST_LOCK(&translators);
552         AST_LIST_INSERT_HEAD(&translators, zt, entry);
553         AST_LIST_UNLOCK(&translators);
554
555         global_format_map.map[dst][src] = 1;
556
557         return res;
558 }
559
560 static void drop_translator(int dst, int src)
561 {
562         struct translator *cur;
563
564         AST_LIST_LOCK(&translators);
565         AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
566                 if (cur->t.src_format.id != ast_format_id_from_old_bitfield((1 << src)))
567                         continue;
568
569                 if (cur->t.dst_format.id != ast_format_id_from_old_bitfield((1 << dst)))
570                         continue;
571
572                 AST_LIST_REMOVE_CURRENT(entry);
573                 ast_unregister_translator(&cur->t);
574                 ast_free(cur);
575                 global_format_map.map[dst][src] = 0;
576                 break;
577         }
578         AST_LIST_TRAVERSE_SAFE_END;
579         AST_LIST_UNLOCK(&translators);
580 }
581
582 static void unregister_translators(void)
583 {
584         struct translator *cur;
585
586         AST_LIST_LOCK(&translators);
587         while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
588                 ast_unregister_translator(&cur->t);
589                 ast_free(cur);
590         }
591         AST_LIST_UNLOCK(&translators);
592 }
593
594 static void build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
595 {
596         unsigned int src, dst;
597
598         for (src = 0; src < 32; src++) {
599                 for (dst = 0; dst < 32; dst++) {
600                         if (!(srcfmts & (1 << src)))
601                                 continue;
602
603                         if (!(dstfmts & (1 << dst)))
604                                 continue;
605
606                         if (global_format_map.map[dst][src])
607                                 continue;
608
609                         if (!register_translator(dst, src))
610                                 map->map[dst][src] = 1;
611                 }
612         }
613 }
614
615 static int find_transcoders(void)
616 {
617         struct dahdi_transcoder_info info = { 0, };
618         struct format_map map = { { { 0 } } };
619         int fd;
620         unsigned int x, y;
621
622         if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
623                 ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
624                 return 0;
625         }
626
627         for (info.tcnum = 0; !ioctl(fd, DAHDI_TC_GETINFO, &info); info.tcnum++) {
628                 ast_verb(2, "Found transcoder '%s'.\n", info.name);
629
630                 /* Complex codecs need to support signed linear.  If the
631                  * hardware transcoder does not natively support signed linear
632                  * format, we will emulate it in software directly in this
633                  * module. Also, do not allow direct ulaw/alaw to complex
634                  * codec translation, since that will prevent the generic PLC
635                  * functions from working. */
636                 if (info.dstfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
637                         info.dstfmts |= DAHDI_FORMAT_SLINEAR;
638                         info.dstfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
639                 }
640                 if (info.srcfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
641                         info.srcfmts |= DAHDI_FORMAT_SLINEAR;
642                         info.srcfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
643                 }
644
645                 build_translators(&map, info.dstfmts, info.srcfmts);
646                 ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
647
648         }
649
650         close(fd);
651
652         if (!info.tcnum) {
653                 ast_verb(2, "No hardware transcoders found.\n");
654         }
655
656         for (x = 0; x < 32; x++) {
657                 for (y = 0; y < 32; y++) {
658                         if (!map.map[x][y] && global_format_map.map[x][y])
659                                 drop_translator(x, y);
660                 }
661         }
662
663         return 0;
664 }
665
666 static int reload(void)
667 {
668         return AST_MODULE_LOAD_SUCCESS;
669 }
670
671 static int unload_module(void)
672 {
673         ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
674         unregister_translators();
675
676         return 0;
677 }
678
679 static int load_module(void)
680 {
681         ast_ulaw_init();
682         find_transcoders();
683         ast_cli_register_multiple(cli, ARRAY_LEN(cli));
684         return AST_MODULE_LOAD_SUCCESS;
685 }
686
687 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic DAHDI Transcoder Codec Translator",
688                 .load = load_module,
689                 .unload = unload_module,
690                 .reload = reload,
691                );