codec_dahdi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 (option_verbose > 10) {
  151. if (-1 == res) {
  152. ast_log(LOG_ERROR, "Failed to write to transcoder: %s\n", strerror(errno));
  153. }
  154. if (count != res) {
  155. ast_log(LOG_ERROR, "Requested write of %zd bytes, but only wrote %d bytes.\n", count, res);
  156. }
  157. }
  158. }
  159. static int dahdi_encoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  160. {
  161. struct codec_dahdi_pvt *dahdip = pvt->pvt;
  162. if (!f->subclass.format.id) {
  163. /* We're just faking a return for calculation purposes. */
  164. dahdip->fake = 2;
  165. pvt->samples = f->samples;
  166. return 0;
  167. }
  168. /* Buffer up the packets and send them to the hardware if we
  169. * have enough samples set up. */
  170. if (dahdip->softslin) {
  171. if (lintoulaw(pvt, f)) {
  172. return -1;
  173. }
  174. } else {
  175. /* NOTE: If softslin support is not needed, and the sample
  176. * size is equal to the required sample size, we wouldn't
  177. * need this copy operation. But at the time this was
  178. * written, only softslin is supported. */
  179. if (dahdip->samples_in_buffer + f->samples > sizeof(dahdip->ulaw_buffer)) {
  180. ast_log(LOG_ERROR, "Out of buffer space.\n");
  181. return -1;
  182. }
  183. memcpy(&dahdip->ulaw_buffer[dahdip->samples_in_buffer], f->data.ptr, f->samples);
  184. dahdip->samples_in_buffer += f->samples;
  185. }
  186. while (dahdip->samples_in_buffer >= dahdip->required_samples) {
  187. dahdi_write_frame(dahdip, dahdip->ulaw_buffer, dahdip->required_samples);
  188. dahdip->samples_written_to_hardware += dahdip->required_samples;
  189. dahdip->samples_in_buffer -= dahdip->required_samples;
  190. if (dahdip->samples_in_buffer) {
  191. /* Shift any remaining bytes down. */
  192. memmove(dahdip->ulaw_buffer, &dahdip->ulaw_buffer[dahdip->required_samples],
  193. dahdip->samples_in_buffer);
  194. }
  195. }
  196. pvt->samples += f->samples;
  197. pvt->datalen = 0;
  198. return -1;
  199. }
  200. static void dahdi_wait_for_packet(int fd)
  201. {
  202. struct pollfd p = {0};
  203. p.fd = fd;
  204. p.events = POLLIN;
  205. poll(&p, 1, 10);
  206. }
  207. static struct ast_frame *dahdi_encoder_frameout(struct ast_trans_pvt *pvt)
  208. {
  209. struct codec_dahdi_pvt *dahdip = pvt->pvt;
  210. int res;
  211. if (2 == dahdip->fake) {
  212. dahdip->fake = 1;
  213. pvt->f.frametype = AST_FRAME_VOICE;
  214. ast_format_clear(&pvt->f.subclass.format);
  215. pvt->f.samples = dahdip->required_samples;
  216. pvt->f.data.ptr = NULL;
  217. pvt->f.offset = 0;
  218. pvt->f.datalen = 0;
  219. pvt->f.mallocd = 0;
  220. pvt->samples = 0;
  221. return ast_frisolate(&pvt->f);
  222. } else if (1 == dahdip->fake) {
  223. dahdip->fake = 0;
  224. return NULL;
  225. }
  226. if (dahdip->samples_written_to_hardware >= dahdip->required_samples) {
  227. dahdi_wait_for_packet(dahdip->fd);
  228. }
  229. res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
  230. if (-1 == res) {
  231. if (EWOULDBLOCK == errno) {
  232. /* Nothing waiting... */
  233. return NULL;
  234. } else {
  235. ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
  236. return NULL;
  237. }
  238. } else {
  239. pvt->f.datalen = res;
  240. pvt->f.frametype = AST_FRAME_VOICE;
  241. ast_format_copy(&pvt->f.subclass.format, &pvt->t->dst_format);
  242. pvt->f.mallocd = 0;
  243. pvt->f.offset = AST_FRIENDLY_OFFSET;
  244. pvt->f.src = pvt->t->name;
  245. pvt->f.data.ptr = pvt->outbuf.c;
  246. pvt->f.samples = ast_codec_get_samples(&pvt->f);
  247. dahdip->samples_written_to_hardware =
  248. (dahdip->samples_written_to_hardware >= pvt->f.samples) ?
  249. dahdip->samples_written_to_hardware - pvt->f.samples : 0;
  250. pvt->samples = 0;
  251. pvt->datalen = 0;
  252. return ast_frisolate(&pvt->f);
  253. }
  254. /* Shouldn't get here... */
  255. return NULL;
  256. }
  257. static int dahdi_decoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  258. {
  259. struct codec_dahdi_pvt *dahdip = pvt->pvt;
  260. if (!f->subclass.format.id) {
  261. /* We're just faking a return for calculation purposes. */
  262. dahdip->fake = 2;
  263. pvt->samples = f->samples;
  264. return 0;
  265. }
  266. if (!f->datalen) {
  267. if (f->samples != dahdip->required_samples) {
  268. ast_log(LOG_ERROR, "%d != %d %d\n", f->samples, dahdip->required_samples, f->datalen);
  269. }
  270. }
  271. dahdi_write_frame(dahdip, f->data.ptr, f->datalen);
  272. dahdip->samples_written_to_hardware += f->samples;
  273. pvt->samples += f->samples;
  274. pvt->datalen = 0;
  275. return -1;
  276. }
  277. static struct ast_frame *dahdi_decoder_frameout(struct ast_trans_pvt *pvt)
  278. {
  279. int res;
  280. struct codec_dahdi_pvt *dahdip = pvt->pvt;
  281. if (2 == dahdip->fake) {
  282. dahdip->fake = 1;
  283. pvt->f.frametype = AST_FRAME_VOICE;
  284. ast_format_clear(&pvt->f.subclass.format);
  285. pvt->f.samples = dahdip->required_samples;
  286. pvt->f.data.ptr = NULL;
  287. pvt->f.offset = 0;
  288. pvt->f.datalen = 0;
  289. pvt->f.mallocd = 0;
  290. pvt->samples = 0;
  291. return ast_frisolate(&pvt->f);
  292. } else if (1 == dahdip->fake) {
  293. pvt->samples = 0;
  294. dahdip->fake = 0;
  295. return NULL;
  296. }
  297. if (dahdip->samples_written_to_hardware >= ULAW_SAMPLES) {
  298. dahdi_wait_for_packet(dahdip->fd);
  299. }
  300. /* Let's check to see if there is a new frame for us.... */
  301. if (dahdip->softslin) {
  302. res = read(dahdip->fd, dahdip->ulaw_buffer, sizeof(dahdip->ulaw_buffer));
  303. } else {
  304. res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
  305. }
  306. if (-1 == res) {
  307. if (EWOULDBLOCK == errno) {
  308. /* Nothing waiting... */
  309. return NULL;
  310. } else {
  311. ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
  312. return NULL;
  313. }
  314. } else {
  315. if (dahdip->softslin) {
  316. ulawtolin(pvt, res);
  317. pvt->f.datalen = res * 2;
  318. } else {
  319. pvt->f.datalen = res;
  320. }
  321. pvt->datalen = 0;
  322. pvt->f.frametype = AST_FRAME_VOICE;
  323. ast_format_copy(&pvt->f.subclass.format, &pvt->t->dst_format);
  324. pvt->f.mallocd = 0;
  325. pvt->f.offset = AST_FRIENDLY_OFFSET;
  326. pvt->f.src = pvt->t->name;
  327. pvt->f.data.ptr = pvt->outbuf.c;
  328. pvt->f.samples = res;
  329. pvt->samples = 0;
  330. dahdip->samples_written_to_hardware =
  331. (dahdip->samples_written_to_hardware >= res) ?
  332. dahdip->samples_written_to_hardware - res : 0;
  333. return ast_frisolate(&pvt->f);
  334. }
  335. /* Shouldn't get here... */
  336. return NULL;
  337. }
  338. static void dahdi_destroy(struct ast_trans_pvt *pvt)
  339. {
  340. struct codec_dahdi_pvt *dahdip = pvt->pvt;
  341. switch (ast_format_id_from_old_bitfield(dahdip->fmts.dstfmt)) {
  342. case AST_FORMAT_G729A:
  343. case AST_FORMAT_G723_1:
  344. ast_atomic_fetchadd_int(&channels.encoders, -1);
  345. break;
  346. default:
  347. ast_atomic_fetchadd_int(&channels.decoders, -1);
  348. break;
  349. }
  350. close(dahdip->fd);
  351. }
  352. static int dahdi_translate(struct ast_trans_pvt *pvt, struct ast_format *dst_format, struct ast_format *src_format)
  353. {
  354. /* Request translation through zap if possible */
  355. int fd;
  356. struct codec_dahdi_pvt *dahdip = pvt->pvt;
  357. int flags;
  358. int tried_once = 0;
  359. const char *dev_filename = "/dev/dahdi/transcode";
  360. if ((fd = open(dev_filename, O_RDWR)) < 0) {
  361. ast_log(LOG_ERROR, "Failed to open %s: %s\n", dev_filename, strerror(errno));
  362. return -1;
  363. }
  364. dahdip->fmts.srcfmt = ast_format_to_old_bitfield(src_format);
  365. dahdip->fmts.dstfmt = ast_format_to_old_bitfield(dst_format);
  366. ast_debug(1, "Opening transcoder channel from %s to %s.\n", ast_getformatname(src_format), ast_getformatname(dst_format));
  367. retry:
  368. if (ioctl(fd, DAHDI_TC_ALLOCATE, &dahdip->fmts)) {
  369. if ((ENODEV == errno) && !tried_once) {
  370. /* We requested to translate to/from an unsupported
  371. * format. Most likely this is because signed linear
  372. * was not supported by any hardware devices even
  373. * though this module always registers signed linear
  374. * support. In this case we'll retry, requesting
  375. * support for ULAW instead of signed linear and then
  376. * we'll just convert from ulaw to signed linear in
  377. * software. */
  378. if (AST_FORMAT_SLINEAR == ast_format_id_from_old_bitfield(dahdip->fmts.srcfmt)) {
  379. ast_debug(1, "Using soft_slin support on source\n");
  380. dahdip->softslin = 1;
  381. dahdip->fmts.srcfmt = ast_format_id_to_old_bitfield(AST_FORMAT_ULAW);
  382. } else if (AST_FORMAT_SLINEAR == ast_format_id_from_old_bitfield(dahdip->fmts.dstfmt)) {
  383. ast_debug(1, "Using soft_slin support on destination\n");
  384. dahdip->softslin = 1;
  385. dahdip->fmts.dstfmt = ast_format_id_to_old_bitfield(AST_FORMAT_ULAW);
  386. }
  387. tried_once = 1;
  388. goto retry;
  389. }
  390. ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
  391. close(fd);
  392. return -1;
  393. }
  394. flags = fcntl(fd, F_GETFL);
  395. if (flags > - 1) {
  396. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
  397. ast_log(LOG_WARNING, "Could not set non-block mode!\n");
  398. }
  399. dahdip->fd = fd;
  400. dahdip->required_samples = ((dahdip->fmts.dstfmt|dahdip->fmts.srcfmt) & (ast_format_id_to_old_bitfield(AST_FORMAT_G723_1))) ? G723_SAMPLES : G729_SAMPLES;
  401. switch (ast_format_id_from_old_bitfield(dahdip->fmts.dstfmt)) {
  402. case AST_FORMAT_G729A:
  403. ast_atomic_fetchadd_int(&channels.encoders, +1);
  404. break;
  405. case AST_FORMAT_G723_1:
  406. ast_atomic_fetchadd_int(&channels.encoders, +1);
  407. break;
  408. default:
  409. ast_atomic_fetchadd_int(&channels.decoders, +1);
  410. break;
  411. }
  412. return 0;
  413. }
  414. static int dahdi_new(struct ast_trans_pvt *pvt)
  415. {
  416. return dahdi_translate(pvt,
  417. &pvt->t->dst_format,
  418. &pvt->t->src_format);
  419. }
  420. static struct ast_frame *fakesrc_sample(void)
  421. {
  422. /* Don't bother really trying to test hardware ones. */
  423. static struct ast_frame f = {
  424. .frametype = AST_FRAME_VOICE,
  425. .samples = 160,
  426. .src = __PRETTY_FUNCTION__
  427. };
  428. return &f;
  429. }
  430. static int is_encoder(struct translator *zt)
  431. {
  432. if ((zt->t.src_format.id == AST_FORMAT_ULAW) ||
  433. (zt->t.src_format.id == AST_FORMAT_ALAW) ||
  434. (zt->t.src_format.id == AST_FORMAT_SLINEAR)) {
  435. return 1;
  436. } else {
  437. return 0;
  438. }
  439. }
  440. static int register_translator(int dst, int src)
  441. {
  442. struct translator *zt;
  443. int res;
  444. struct ast_format dst_format;
  445. struct ast_format src_format;
  446. ast_format_from_old_bitfield(&dst_format, (1 << dst));
  447. ast_format_from_old_bitfield(&src_format, (1 << src));
  448. if (!(zt = ast_calloc(1, sizeof(*zt)))) {
  449. return -1;
  450. }
  451. snprintf((char *) (zt->t.name), sizeof(zt->t.name), "zap%sto%s",
  452. ast_getformatname(&src_format), ast_getformatname(&dst_format));
  453. ast_format_copy(&zt->t.src_format, &src_format);
  454. ast_format_copy(&zt->t.dst_format, &dst_format);
  455. zt->t.buf_size = BUFFER_SIZE;
  456. if (is_encoder(zt)) {
  457. zt->t.framein = dahdi_encoder_framein;
  458. zt->t.frameout = dahdi_encoder_frameout;
  459. } else {
  460. zt->t.framein = dahdi_decoder_framein;
  461. zt->t.frameout = dahdi_decoder_frameout;
  462. }
  463. zt->t.destroy = dahdi_destroy;
  464. zt->t.buffer_samples = 0;
  465. zt->t.newpvt = dahdi_new;
  466. zt->t.sample = fakesrc_sample;
  467. zt->t.native_plc = 0;
  468. zt->t.desc_size = sizeof(struct codec_dahdi_pvt);
  469. if ((res = ast_register_translator(&zt->t))) {
  470. ast_free(zt);
  471. return -1;
  472. }
  473. AST_LIST_LOCK(&translators);
  474. AST_LIST_INSERT_HEAD(&translators, zt, entry);
  475. AST_LIST_UNLOCK(&translators);
  476. global_format_map.map[dst][src] = 1;
  477. return res;
  478. }
  479. static void drop_translator(int dst, int src)
  480. {
  481. struct translator *cur;
  482. AST_LIST_LOCK(&translators);
  483. AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
  484. if (cur->t.src_format.id != ast_format_id_from_old_bitfield((1 << src)))
  485. continue;
  486. if (cur->t.dst_format.id != ast_format_id_from_old_bitfield((1 << dst)))
  487. continue;
  488. AST_LIST_REMOVE_CURRENT(entry);
  489. ast_unregister_translator(&cur->t);
  490. ast_free(cur);
  491. global_format_map.map[dst][src] = 0;
  492. break;
  493. }
  494. AST_LIST_TRAVERSE_SAFE_END;
  495. AST_LIST_UNLOCK(&translators);
  496. }
  497. static void unregister_translators(void)
  498. {
  499. struct translator *cur;
  500. AST_LIST_LOCK(&translators);
  501. while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
  502. ast_unregister_translator(&cur->t);
  503. ast_free(cur);
  504. }
  505. AST_LIST_UNLOCK(&translators);
  506. }
  507. static void build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
  508. {
  509. unsigned int src, dst;
  510. for (src = 0; src < 32; src++) {
  511. for (dst = 0; dst < 32; dst++) {
  512. if (!(srcfmts & (1 << src)))
  513. continue;
  514. if (!(dstfmts & (1 << dst)))
  515. continue;
  516. if (global_format_map.map[dst][src])
  517. continue;
  518. if (!register_translator(dst, src))
  519. map->map[dst][src] = 1;
  520. }
  521. }
  522. }
  523. static int find_transcoders(void)
  524. {
  525. struct dahdi_transcoder_info info = { 0, };
  526. struct format_map map = { { { 0 } } };
  527. int fd, res;
  528. unsigned int x, y;
  529. if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
  530. ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
  531. return 0;
  532. }
  533. for (info.tcnum = 0; !(res = ioctl(fd, DAHDI_TC_GETINFO, &info)); info.tcnum++) {
  534. if (option_verbose > 1)
  535. ast_verbose(VERBOSE_PREFIX_2 "Found transcoder '%s'.\n", info.name);
  536. /* Complex codecs need to support signed linear. If the
  537. * hardware transcoder does not natively support signed linear
  538. * format, we will emulate it in software directly in this
  539. * module. Also, do not allow direct ulaw/alaw to complex
  540. * codec translation, since that will prevent the generic PLC
  541. * functions from working. */
  542. if (info.dstfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
  543. info.dstfmts |= DAHDI_FORMAT_SLINEAR;
  544. info.dstfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
  545. }
  546. if (info.srcfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
  547. info.srcfmts |= DAHDI_FORMAT_SLINEAR;
  548. info.srcfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
  549. }
  550. build_translators(&map, info.dstfmts, info.srcfmts);
  551. ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
  552. }
  553. close(fd);
  554. if (!info.tcnum && (option_verbose > 1))
  555. ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
  556. for (x = 0; x < 32; x++) {
  557. for (y = 0; y < 32; y++) {
  558. if (!map.map[x][y] && global_format_map.map[x][y])
  559. drop_translator(x, y);
  560. }
  561. }
  562. return 0;
  563. }
  564. static int reload(void)
  565. {
  566. return AST_MODULE_LOAD_SUCCESS;
  567. }
  568. static int unload_module(void)
  569. {
  570. ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
  571. unregister_translators();
  572. return 0;
  573. }
  574. static int load_module(void)
  575. {
  576. ast_ulaw_init();
  577. find_transcoders();
  578. ast_cli_register_multiple(cli, ARRAY_LEN(cli));
  579. return AST_MODULE_LOAD_SUCCESS;
  580. }
  581. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic DAHDI Transcoder Codec Translator",
  582. .load = load_module,
  583. .unload = unload_module,
  584. .reload = reload,
  585. );