format_pcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. int 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 (%d) (%s)!\n", res, strerror(errno));
  75. return NULL;
  76. }
  77. s->fr.datalen = res;
  78. if (ast_format_cmp(s->fmt->format, ast_format_g722) == AST_FORMAT_CMP_EQUAL)
  79. *whennext = s->fr.samples = res * 2;
  80. else
  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, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
  124. if (written == -1)
  125. break; /* error */
  126. left -= written;
  127. }
  128. ret = 0; /* successful */
  129. } else {
  130. if (offset > max) {
  131. ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
  132. offset = max;
  133. }
  134. ret = fseeko(fs->f, offset, SEEK_SET);
  135. }
  136. return ret;
  137. }
  138. static int pcm_trunc(struct ast_filestream *fs)
  139. {
  140. int cur, fd;
  141. if ((fd = fileno(fs->f)) < 0) {
  142. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for pcm filestream %p: %s\n", fs, strerror(errno));
  143. return -1;
  144. }
  145. if ((cur = ftello(fs->f)) < 0) {
  146. ast_log(AST_LOG_WARNING, "Unable to determine current position in pcm filestream %p: %s\n", fs, strerror(errno));
  147. return -1;
  148. }
  149. /* Truncate file to current length */
  150. return ftruncate(fd, cur);
  151. }
  152. static off_t pcm_tell(struct ast_filestream *fs)
  153. {
  154. return ftello(fs->f);
  155. }
  156. static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
  157. {
  158. int res;
  159. #ifdef REALTIME_WRITE
  160. if (ast_format_cmp(s->fmt->format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
  161. struct pcm_desc *pd = (struct pcm_desc *)fs->_private;
  162. struct stat stat_buf;
  163. unsigned long cur_time = get_time();
  164. unsigned long fpos = ( cur_time - pd->start_time ) * 8; /* 8 bytes per msec */
  165. /* Check if we have written to this position yet. If we have, then increment pos by one frame
  166. * for some degree of protection against receiving packets in the same clock tick.
  167. */
  168. fstat(fileno(fs->f), &stat_buf );
  169. if (stat_buf.st_size > fpos )
  170. fpos += f->datalen; /* Incrementing with the size of this current frame */
  171. if (stat_buf.st_size < fpos) {
  172. /* fill the gap with 0x55 rather than 0. */
  173. char buf[1024];
  174. unsigned long cur, to_write;
  175. cur = stat_buf.st_size;
  176. if (fseek(fs->f, cur, SEEK_SET) < 0) {
  177. ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
  178. return -1;
  179. }
  180. memset(buf, 0x55, 512);
  181. while (cur < fpos) {
  182. to_write = fpos - cur;
  183. if (to_write > sizeof(buf))
  184. to_write = sizeof(buf);
  185. fwrite(buf, 1, to_write, fs->f);
  186. cur += to_write;
  187. }
  188. }
  189. if (fseek(s->f, fpos, SEEK_SET) < 0) {
  190. ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
  191. return -1;
  192. }
  193. }
  194. #endif /* REALTIME_WRITE */
  195. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  196. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  197. return -1;
  198. }
  199. return 0;
  200. }
  201. /* SUN .au support routines */
  202. #define AU_HEADER_SIZE 24
  203. #define AU_HEADER(var) uint32_t var[6]
  204. #define AU_HDR_MAGIC_OFF 0
  205. #define AU_HDR_HDR_SIZE_OFF 1
  206. #define AU_HDR_DATA_SIZE_OFF 2
  207. #define AU_HDR_ENCODING_OFF 3
  208. #define AU_HDR_SAMPLE_RATE_OFF 4
  209. #define AU_HDR_CHANNELS_OFF 5
  210. #define AU_ENC_8BIT_ULAW 1
  211. #define AU_MAGIC 0x2e736e64
  212. #if __BYTE_ORDER == __BIG_ENDIAN
  213. #define htoll(b) (b)
  214. #define htols(b) (b)
  215. #define ltohl(b) (b)
  216. #define ltohs(b) (b)
  217. #else
  218. #if __BYTE_ORDER == __LITTLE_ENDIAN
  219. #define htoll(b) \
  220. (((((b) ) & 0xFF) << 24) | \
  221. ((((b) >> 8) & 0xFF) << 16) | \
  222. ((((b) >> 16) & 0xFF) << 8) | \
  223. ((((b) >> 24) & 0xFF) ))
  224. #define htols(b) \
  225. (((((b) ) & 0xFF) << 8) | \
  226. ((((b) >> 8) & 0xFF) ))
  227. #define ltohl(b) htoll(b)
  228. #define ltohs(b) htols(b)
  229. #else
  230. #error "Endianess not defined"
  231. #endif
  232. #endif
  233. static int check_header(FILE *f)
  234. {
  235. AU_HEADER(header);
  236. uint32_t magic;
  237. uint32_t hdr_size;
  238. uint32_t data_size;
  239. uint32_t encoding;
  240. uint32_t sample_rate;
  241. uint32_t channels;
  242. if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
  243. ast_log(LOG_WARNING, "Read failed (header)\n");
  244. return -1;
  245. }
  246. magic = ltohl(header[AU_HDR_MAGIC_OFF]);
  247. if (magic != (uint32_t) AU_MAGIC) {
  248. ast_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
  249. }
  250. hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]);
  251. if (hdr_size < AU_HEADER_SIZE) {
  252. hdr_size = AU_HEADER_SIZE;
  253. }
  254. /* data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
  255. encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
  256. if (encoding != AU_ENC_8BIT_ULAW) {
  257. ast_log(LOG_WARNING, "Unexpected format: %u. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
  258. return -1;
  259. }
  260. sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
  261. if (sample_rate != DEFAULT_SAMPLE_RATE) {
  262. ast_log(LOG_WARNING, "Sample rate can only be 8000 not %u\n", sample_rate);
  263. return -1;
  264. }
  265. channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
  266. if (channels != 1) {
  267. ast_log(LOG_WARNING, "Not in mono: channels=%u\n", channels);
  268. return -1;
  269. }
  270. /* Skip to data */
  271. fseek(f, 0, SEEK_END);
  272. data_size = ftell(f) - hdr_size;
  273. if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
  274. ast_log(LOG_WARNING, "Failed to skip to data: %u\n", hdr_size);
  275. return -1;
  276. }
  277. return data_size;
  278. }
  279. static int update_header(FILE *f)
  280. {
  281. off_t cur, end;
  282. uint32_t datalen;
  283. int bytes;
  284. cur = ftell(f);
  285. fseek(f, 0, SEEK_END);
  286. end = ftell(f);
  287. /* data starts 24 bytes in */
  288. bytes = end - AU_HEADER_SIZE;
  289. datalen = htoll(bytes);
  290. if (cur < 0) {
  291. ast_log(LOG_WARNING, "Unable to find our position\n");
  292. return -1;
  293. }
  294. if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) {
  295. ast_log(LOG_WARNING, "Unable to set our position\n");
  296. return -1;
  297. }
  298. if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
  299. ast_log(LOG_WARNING, "Unable to set write file size\n");
  300. return -1;
  301. }
  302. if (fseek(f, cur, SEEK_SET)) {
  303. ast_log(LOG_WARNING, "Unable to return to position\n");
  304. return -1;
  305. }
  306. return 0;
  307. }
  308. static int write_header(FILE *f)
  309. {
  310. AU_HEADER(header);
  311. header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
  312. header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE);
  313. header[AU_HDR_DATA_SIZE_OFF] = 0;
  314. header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
  315. header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
  316. header[AU_HDR_CHANNELS_OFF] = htoll(1);
  317. /* Write an au header, ignoring sizes which will be filled in later */
  318. fseek(f, 0, SEEK_SET);
  319. if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
  320. ast_log(LOG_WARNING, "Unable to write header\n");
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. static int au_open(struct ast_filestream *s)
  326. {
  327. if (check_header(s->f) < 0)
  328. return -1;
  329. return 0;
  330. }
  331. static int au_rewrite(struct ast_filestream *s, const char *comment)
  332. {
  333. if (write_header(s->f))
  334. return -1;
  335. return 0;
  336. }
  337. /* XXX check this, probably incorrect */
  338. static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  339. {
  340. off_t min = AU_HEADER_SIZE, max, cur;
  341. long offset = 0, bytes;
  342. if (ast_format_cmp(fs->fmt->format, ast_format_g722) == AST_FORMAT_CMP_EQUAL)
  343. bytes = sample_offset / 2;
  344. else
  345. bytes = sample_offset;
  346. if ((cur = ftello(fs->f)) < 0) {
  347. ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
  348. return -1;
  349. }
  350. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  351. ast_log(AST_LOG_WARNING, "Unable to seek to end of au filestream %p: %s\n", fs, strerror(errno));
  352. return -1;
  353. }
  354. if ((max = ftello(fs->f)) < 0) {
  355. ast_log(AST_LOG_WARNING, "Unable to determine max position in au filestream %p: %s\n", fs, strerror(errno));
  356. return -1;
  357. }
  358. if (whence == SEEK_SET)
  359. offset = bytes + min;
  360. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  361. offset = bytes + cur;
  362. else if (whence == SEEK_END)
  363. offset = max - bytes;
  364. if (whence != SEEK_FORCECUR) {
  365. offset = (offset > max) ? max : offset;
  366. }
  367. /* always protect the header space. */
  368. offset = (offset < min) ? min : offset;
  369. return fseeko(fs->f, offset, SEEK_SET);
  370. }
  371. static int au_trunc(struct ast_filestream *fs)
  372. {
  373. int fd;
  374. off_t cur;
  375. if ((fd = fileno(fs->f)) < 0) {
  376. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for au filestream %p: %s\n", fs, strerror(errno));
  377. return -1;
  378. }
  379. if ((cur = ftello(fs->f)) < 0) {
  380. ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
  381. return -1;
  382. }
  383. /* Truncate file to current length */
  384. if (ftruncate(fd, cur)) {
  385. return -1;
  386. }
  387. return update_header(fs->f);
  388. }
  389. static off_t au_tell(struct ast_filestream *fs)
  390. {
  391. off_t offset = ftello(fs->f);
  392. return offset - AU_HEADER_SIZE;
  393. }
  394. static struct ast_format_def alaw_f = {
  395. .name = "alaw",
  396. .exts = "alaw|al|alw",
  397. .write = pcm_write,
  398. .seek = pcm_seek,
  399. .trunc = pcm_trunc,
  400. .tell = pcm_tell,
  401. .read = pcm_read,
  402. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  403. #ifdef REALTIME_WRITE
  404. .open = pcma_open,
  405. .rewrite = pcma_rewrite,
  406. .desc_size = sizeof(struct pcm_desc),
  407. #endif
  408. };
  409. static struct ast_format_def pcm_f = {
  410. .name = "pcm",
  411. .exts = "pcm|ulaw|ul|mu|ulw",
  412. .write = pcm_write,
  413. .seek = pcm_seek,
  414. .trunc = pcm_trunc,
  415. .tell = pcm_tell,
  416. .read = pcm_read,
  417. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  418. };
  419. static struct ast_format_def g722_f = {
  420. .name = "g722",
  421. .exts = "g722",
  422. .write = pcm_write,
  423. .seek = pcm_seek,
  424. .trunc = pcm_trunc,
  425. .tell = pcm_tell,
  426. .read = pcm_read,
  427. .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
  428. };
  429. static struct ast_format_def au_f = {
  430. .name = "au",
  431. .exts = "au",
  432. .open = au_open,
  433. .rewrite = au_rewrite,
  434. .write = pcm_write,
  435. .seek = au_seek,
  436. .trunc = au_trunc,
  437. .tell = au_tell,
  438. .read = pcm_read,
  439. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, /* this many shorts */
  440. };
  441. static int load_module(void)
  442. {
  443. int i;
  444. /* XXX better init ? */
  445. for (i = 0; i < ARRAY_LEN(ulaw_silence); i++)
  446. ulaw_silence[i] = AST_LIN2MU(0);
  447. for (i = 0; i < ARRAY_LEN(alaw_silence); i++)
  448. alaw_silence[i] = AST_LIN2A(0);
  449. pcm_f.format = ast_format_ulaw;
  450. alaw_f.format = ast_format_alaw;
  451. au_f.format = ast_format_ulaw;
  452. g722_f.format = ast_format_g722;
  453. if ( ast_format_def_register(&pcm_f)
  454. || ast_format_def_register(&alaw_f)
  455. || ast_format_def_register(&au_f)
  456. || ast_format_def_register(&g722_f) )
  457. return AST_MODULE_LOAD_FAILURE;
  458. return AST_MODULE_LOAD_SUCCESS;
  459. }
  460. static int unload_module(void)
  461. {
  462. return ast_format_def_unregister(pcm_f.name)
  463. || ast_format_def_unregister(alaw_f.name)
  464. || ast_format_def_unregister(au_f.name)
  465. || ast_format_def_unregister(g722_f.name);
  466. }
  467. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz",
  468. .support_level = AST_MODULE_SUPPORT_CORE,
  469. .load = load_module,
  470. .unload = unload_module,
  471. .load_pri = AST_MODPRI_APP_DEPEND
  472. );