format_pcm.c 14 KB

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