format_pcm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Flat, binary, ulaw PCM file format.
  21. * \arg File name extension: alaw, al, alw, pcm, ulaw, ul, mu, ulw, g722, au
  22. *
  23. * \ingroup formats
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/mod_format.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/endian.h"
  33. #include "asterisk/ulaw.h"
  34. #include "asterisk/alaw.h"
  35. #include "asterisk/format_cache.h"
  36. #define BUF_SIZE 160 /* 160 bytes, and same number of samples */
  37. static char ulaw_silence[BUF_SIZE];
  38. static char alaw_silence[BUF_SIZE];
  39. /* #define REALTIME_WRITE */ /* XXX does it work at all ? */
  40. #ifdef REALTIME_WRITE
  41. struct pcm_desc {
  42. unsigned long start_time;
  43. };
  44. /* Returns time in msec since system boot. */
  45. static unsigned long get_time(void)
  46. {
  47. struct tms buf;
  48. clock_t cur;
  49. cur = times( &buf );
  50. if( cur < 0 ) {
  51. ast_log( LOG_WARNING, "Cannot get current time\n" );
  52. return 0;
  53. }
  54. return cur * 1000 / sysconf( _SC_CLK_TCK );
  55. }
  56. static int pcma_open(struct ast_filestream *s)
  57. {
  58. if (ast_format_cmp(s->fmt->format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL)
  59. pd->starttime = get_time();
  60. return 0;
  61. }
  62. static int pcma_rewrite(struct ast_filestream *s, const char *comment)
  63. {
  64. return pcma_open(s);
  65. }
  66. #endif
  67. static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
  68. {
  69. size_t res;
  70. /* Send a frame from the file to the appropriate channel */
  71. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  72. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
  73. if (res) {
  74. ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
  75. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  76. strerror(errno));
  77. }
  78. return NULL;
  79. }
  80. s->fr.datalen = res;
  81. *whennext = s->fr.samples = res;
  82. return &s->fr;
  83. }
  84. static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  85. {
  86. off_t cur, max, offset = 0;
  87. int ret = -1; /* assume error */
  88. if ((cur = ftello(fs->f)) < 0) {
  89. ast_log(AST_LOG_WARNING, "Unable to determine current position in pcm filestream %p: %s\n", fs, strerror(errno));
  90. return -1;
  91. }
  92. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  93. ast_log(AST_LOG_WARNING, "Unable to seek to end of pcm filestream %p: %s\n", fs, strerror(errno));
  94. return -1;
  95. }
  96. if ((max = ftello(fs->f)) < 0) {
  97. ast_log(AST_LOG_WARNING, "Unable to determine max position in pcm filestream %p: %s\n", fs, strerror(errno));
  98. return -1;
  99. }
  100. switch (whence) {
  101. case SEEK_SET:
  102. offset = sample_offset;
  103. break;
  104. case SEEK_END:
  105. offset = max - sample_offset;
  106. break;
  107. case SEEK_CUR:
  108. case SEEK_FORCECUR:
  109. offset = cur + sample_offset;
  110. break;
  111. default:
  112. ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
  113. offset = sample_offset;
  114. }
  115. if (offset < 0) {
  116. ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset);
  117. offset = 0;
  118. }
  119. if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */
  120. size_t left = offset - max;
  121. const char *src = (ast_format_cmp(fs->fmt->format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) ? alaw_silence : ulaw_silence;
  122. while (left) {
  123. size_t written = fwrite(src, 1, MIN(left, BUF_SIZE), fs->f);
  124. if (written < MIN(left, BUF_SIZE)) {
  125. break; /* error */
  126. }
  127. left -= written;
  128. }
  129. ret = 0; /* successful */
  130. } else {
  131. if (offset > max) {
  132. ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
  133. offset = max;
  134. }
  135. ret = fseeko(fs->f, offset, SEEK_SET);
  136. }
  137. return ret;
  138. }
  139. static int pcm_trunc(struct ast_filestream *fs)
  140. {
  141. int cur, fd;
  142. if ((fd = fileno(fs->f)) < 0) {
  143. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for pcm filestream %p: %s\n", fs, strerror(errno));
  144. return -1;
  145. }
  146. if ((cur = ftello(fs->f)) < 0) {
  147. ast_log(AST_LOG_WARNING, "Unable to determine current position in pcm filestream %p: %s\n", fs, strerror(errno));
  148. return -1;
  149. }
  150. /* Truncate file to current length */
  151. return ftruncate(fd, cur);
  152. }
  153. static off_t pcm_tell(struct ast_filestream *fs)
  154. {
  155. return ftello(fs->f);
  156. }
  157. static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
  158. {
  159. int res;
  160. #ifdef REALTIME_WRITE
  161. if (ast_format_cmp(s->fmt->format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
  162. struct pcm_desc *pd = (struct pcm_desc *)fs->_private;
  163. struct stat stat_buf;
  164. unsigned long cur_time = get_time();
  165. unsigned long fpos = ( cur_time - pd->start_time ) * 8; /* 8 bytes per msec */
  166. /* Check if we have written to this position yet. If we have, then increment pos by one frame
  167. * for some degree of protection against receiving packets in the same clock tick.
  168. */
  169. fstat(fileno(fs->f), &stat_buf );
  170. if (stat_buf.st_size > fpos )
  171. fpos += f->datalen; /* Incrementing with the size of this current frame */
  172. if (stat_buf.st_size < fpos) {
  173. /* fill the gap with 0x55 rather than 0. */
  174. char buf[1024];
  175. unsigned long cur, to_write;
  176. cur = stat_buf.st_size;
  177. if (fseek(fs->f, cur, SEEK_SET) < 0) {
  178. ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
  179. return -1;
  180. }
  181. memset(buf, 0x55, 512);
  182. while (cur < fpos) {
  183. to_write = fpos - cur;
  184. if (to_write > sizeof(buf))
  185. to_write = sizeof(buf);
  186. if (fwrite(buf, 1, to_write, fs->f) != to_write) {
  187. ast_log(LOG_ERROR, "Failed to write to file: %s\n", strerror(errno));
  188. return -1;
  189. }
  190. cur += to_write;
  191. }
  192. }
  193. if (fseek(s->f, fpos, SEEK_SET) < 0) {
  194. ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
  195. return -1;
  196. }
  197. }
  198. #endif /* REALTIME_WRITE */
  199. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  200. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. /* SUN .au support routines */
  206. #define MIN_AU_HEADER_SIZE 24
  207. #define AU_HEADER(var) uint32_t var[6]
  208. #define AU_HDR_MAGIC_OFF 0
  209. #define AU_HDR_HDR_SIZE_OFF 1
  210. #define AU_HDR_DATA_SIZE_OFF 2
  211. #define AU_HDR_ENCODING_OFF 3
  212. #define AU_HDR_SAMPLE_RATE_OFF 4
  213. #define AU_HDR_CHANNELS_OFF 5
  214. #define AU_ENC_8BIT_ULAW 1
  215. #define AU_MAGIC 0x2e736e64
  216. #if __BYTE_ORDER == __BIG_ENDIAN
  217. #define htoll(b) (b)
  218. #define htols(b) (b)
  219. #define ltohl(b) (b)
  220. #define ltohs(b) (b)
  221. #else
  222. #if __BYTE_ORDER == __LITTLE_ENDIAN
  223. #define htoll(b) \
  224. (((((b) ) & 0xFF) << 24) | \
  225. ((((b) >> 8) & 0xFF) << 16) | \
  226. ((((b) >> 16) & 0xFF) << 8) | \
  227. ((((b) >> 24) & 0xFF) ))
  228. #define htols(b) \
  229. (((((b) ) & 0xFF) << 8) | \
  230. ((((b) >> 8) & 0xFF) ))
  231. #define ltohl(b) htoll(b)
  232. #define ltohs(b) htols(b)
  233. #else
  234. #error "Endianess not defined"
  235. #endif
  236. #endif
  237. struct au_desc {
  238. uint32_t hdr_size;
  239. };
  240. static int check_header(struct ast_filestream *fs)
  241. {
  242. AU_HEADER(header);
  243. uint32_t magic;
  244. uint32_t hdr_size;
  245. uint32_t data_size;
  246. uint32_t encoding;
  247. uint32_t sample_rate;
  248. uint32_t channels;
  249. struct au_desc *desc = fs->_private;
  250. FILE *f = fs->f;
  251. if (fread(header, 1, MIN_AU_HEADER_SIZE, f) != MIN_AU_HEADER_SIZE) {
  252. ast_log(LOG_WARNING, "Read failed (header)\n");
  253. return -1;
  254. }
  255. magic = ltohl(header[AU_HDR_MAGIC_OFF]);
  256. if (magic != (uint32_t) AU_MAGIC) {
  257. ast_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
  258. }
  259. hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]);
  260. if (hdr_size < MIN_AU_HEADER_SIZE) {
  261. hdr_size = MIN_AU_HEADER_SIZE;
  262. }
  263. /* data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
  264. encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
  265. if (encoding != AU_ENC_8BIT_ULAW) {
  266. ast_log(LOG_WARNING, "Unexpected format: %u. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
  267. return -1;
  268. }
  269. sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
  270. if (sample_rate != DEFAULT_SAMPLE_RATE) {
  271. ast_log(LOG_WARNING, "Sample rate can only be 8000 not %u\n", sample_rate);
  272. return -1;
  273. }
  274. channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
  275. if (channels != 1) {
  276. ast_log(LOG_WARNING, "Not in mono: channels=%u\n", channels);
  277. return -1;
  278. }
  279. /* Skip to data */
  280. fseek(f, 0, SEEK_END);
  281. data_size = ftell(f) - hdr_size;
  282. if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
  283. ast_log(LOG_WARNING, "Failed to skip to data: %u\n", hdr_size);
  284. return -1;
  285. }
  286. /* We'll need this later */
  287. desc->hdr_size = hdr_size;
  288. return data_size;
  289. }
  290. static int update_header(struct ast_filestream *fs)
  291. {
  292. off_t cur, end;
  293. uint32_t datalen;
  294. int bytes;
  295. struct au_desc *desc = fs->_private;
  296. FILE *f = fs->f;
  297. cur = ftell(f);
  298. fseek(f, 0, SEEK_END);
  299. end = ftell(f);
  300. /* data starts 24 bytes in */
  301. bytes = end - desc->hdr_size;
  302. datalen = htoll(bytes);
  303. if (cur < 0) {
  304. ast_log(LOG_WARNING, "Unable to find our position\n");
  305. return -1;
  306. }
  307. if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) {
  308. ast_log(LOG_WARNING, "Unable to set our position\n");
  309. return -1;
  310. }
  311. if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
  312. ast_log(LOG_WARNING, "Unable to set write file size\n");
  313. return -1;
  314. }
  315. if (fseek(f, cur, SEEK_SET)) {
  316. ast_log(LOG_WARNING, "Unable to return to position\n");
  317. return -1;
  318. }
  319. return 0;
  320. }
  321. static int write_header(struct ast_filestream *fs)
  322. {
  323. struct au_desc *desc = fs->_private;
  324. FILE *f = fs->f;
  325. AU_HEADER(header);
  326. header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
  327. header[AU_HDR_HDR_SIZE_OFF] = htoll(desc->hdr_size);
  328. header[AU_HDR_DATA_SIZE_OFF] = 0;
  329. header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
  330. header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
  331. header[AU_HDR_CHANNELS_OFF] = htoll(1);
  332. /* Write an au header, ignoring sizes which will be filled in later */
  333. fseek(f, 0, SEEK_SET);
  334. if (fwrite(header, 1, MIN_AU_HEADER_SIZE, f) != MIN_AU_HEADER_SIZE) {
  335. ast_log(LOG_WARNING, "Unable to write header\n");
  336. return -1;
  337. }
  338. return 0;
  339. }
  340. static int au_open(struct ast_filestream *s)
  341. {
  342. if (check_header(s) < 0)
  343. return -1;
  344. return 0;
  345. }
  346. static int au_rewrite(struct ast_filestream *s, const char *comment)
  347. {
  348. struct au_desc *desc = s->_private;
  349. desc->hdr_size = MIN_AU_HEADER_SIZE;
  350. if (write_header(s))
  351. return -1;
  352. return 0;
  353. }
  354. /* XXX check this, probably incorrect */
  355. static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  356. {
  357. off_t min, max, cur;
  358. long offset = 0;
  359. struct au_desc *desc = fs->_private;
  360. min = desc->hdr_size;
  361. if ((cur = ftello(fs->f)) < 0) {
  362. ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
  363. return -1;
  364. }
  365. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  366. ast_log(AST_LOG_WARNING, "Unable to seek to end of au filestream %p: %s\n", fs, strerror(errno));
  367. return -1;
  368. }
  369. if ((max = ftello(fs->f)) < 0) {
  370. ast_log(AST_LOG_WARNING, "Unable to determine max position in au filestream %p: %s\n", fs, strerror(errno));
  371. return -1;
  372. }
  373. if (whence == SEEK_SET)
  374. offset = sample_offset + min;
  375. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  376. offset = sample_offset + cur;
  377. else if (whence == SEEK_END)
  378. offset = max - sample_offset;
  379. if (whence != SEEK_FORCECUR) {
  380. offset = (offset > max) ? max : offset;
  381. }
  382. /* always protect the header space. */
  383. offset = (offset < min) ? min : offset;
  384. return fseeko(fs->f, offset, SEEK_SET);
  385. }
  386. static int au_trunc(struct ast_filestream *fs)
  387. {
  388. int fd;
  389. off_t cur;
  390. if ((fd = fileno(fs->f)) < 0) {
  391. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for au filestream %p: %s\n", fs, strerror(errno));
  392. return -1;
  393. }
  394. if ((cur = ftello(fs->f)) < 0) {
  395. ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
  396. return -1;
  397. }
  398. /* Truncate file to current length */
  399. if (ftruncate(fd, cur)) {
  400. return -1;
  401. }
  402. return update_header(fs);
  403. }
  404. static off_t au_tell(struct ast_filestream *fs)
  405. {
  406. struct au_desc *desc = fs->_private;
  407. off_t offset = ftello(fs->f);
  408. return offset - desc->hdr_size;
  409. }
  410. static struct ast_frame *g722_read(struct ast_filestream *s, int *whennext)
  411. {
  412. struct ast_frame *f = pcm_read(s, whennext);
  413. *whennext = s->fr.samples = (*whennext * 2);
  414. return f;
  415. }
  416. static int g722_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  417. {
  418. return pcm_seek(fs, sample_offset / 2, whence);
  419. }
  420. static off_t g722_tell(struct ast_filestream *fs)
  421. {
  422. return pcm_tell(fs) * 2;
  423. }
  424. static struct ast_format_def alaw_f = {
  425. .name = "alaw",
  426. .exts = "alaw|al|alw",
  427. .write = pcm_write,
  428. .seek = pcm_seek,
  429. .trunc = pcm_trunc,
  430. .tell = pcm_tell,
  431. .read = pcm_read,
  432. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  433. #ifdef REALTIME_WRITE
  434. .open = pcma_open,
  435. .rewrite = pcma_rewrite,
  436. .desc_size = sizeof(struct pcm_desc),
  437. #endif
  438. };
  439. static struct ast_format_def pcm_f = {
  440. .name = "pcm",
  441. .exts = "pcm|ulaw|ul|mu|ulw",
  442. .write = pcm_write,
  443. .seek = pcm_seek,
  444. .trunc = pcm_trunc,
  445. .tell = pcm_tell,
  446. .read = pcm_read,
  447. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  448. };
  449. static struct ast_format_def g722_f = {
  450. .name = "g722",
  451. .exts = "g722",
  452. .write = pcm_write,
  453. .seek = g722_seek,
  454. .trunc = pcm_trunc,
  455. .tell = g722_tell,
  456. .read = g722_read,
  457. .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
  458. };
  459. static struct ast_format_def au_f = {
  460. .name = "au",
  461. .exts = "au",
  462. .open = au_open,
  463. .rewrite = au_rewrite,
  464. .write = pcm_write,
  465. .seek = au_seek,
  466. .trunc = au_trunc,
  467. .tell = au_tell,
  468. .read = pcm_read,
  469. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, /* this many shorts */
  470. .desc_size = sizeof(struct au_desc),
  471. };
  472. static int unload_module(void)
  473. {
  474. return ast_format_def_unregister(pcm_f.name)
  475. || ast_format_def_unregister(alaw_f.name)
  476. || ast_format_def_unregister(au_f.name)
  477. || ast_format_def_unregister(g722_f.name);
  478. }
  479. static int load_module(void)
  480. {
  481. int i;
  482. /* XXX better init ? */
  483. for (i = 0; i < ARRAY_LEN(ulaw_silence); i++)
  484. ulaw_silence[i] = AST_LIN2MU(0);
  485. for (i = 0; i < ARRAY_LEN(alaw_silence); i++)
  486. alaw_silence[i] = AST_LIN2A(0);
  487. pcm_f.format = ast_format_ulaw;
  488. alaw_f.format = ast_format_alaw;
  489. au_f.format = ast_format_ulaw;
  490. g722_f.format = ast_format_g722;
  491. if ( ast_format_def_register(&pcm_f)
  492. || ast_format_def_register(&alaw_f)
  493. || ast_format_def_register(&au_f)
  494. || ast_format_def_register(&g722_f) ) {
  495. unload_module();
  496. return AST_MODULE_LOAD_DECLINE;
  497. }
  498. return AST_MODULE_LOAD_SUCCESS;
  499. }
  500. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz",
  501. .support_level = AST_MODULE_SUPPORT_CORE,
  502. .load = load_module,
  503. .unload = unload_module,
  504. .load_pri = AST_MODPRI_APP_DEPEND
  505. );