codec_dahdi.c 21 KB

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