codec_dahdi.c 18 KB

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