PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/asterisk-1.4/formats/format_pcm.c

https://github.com/claymodel/voip-foip
C | 510 lines | 409 code | 62 blank | 39 comment | 79 complexity | 1011e086ace339cf7048419d08ee93fd MD5 | raw file
  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: pcm, ulaw, ul, mu
  22. *
  23. * \ingroup formats
  24. */
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision: 233782 $")
  27. #include <unistd.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <stdlib.h>
  31. #include <sys/time.h>
  32. #include <stdio.h>
  33. #include <errno.h>
  34. #include <string.h>
  35. #include "asterisk/lock.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/file.h"
  38. #include "asterisk/logger.h"
  39. #include "asterisk/sched.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/endian.h"
  42. #include "asterisk/ulaw.h"
  43. #include "asterisk/alaw.h"
  44. #define BUF_SIZE 160 /* 160 bytes, and same number of samples */
  45. static char ulaw_silence[BUF_SIZE];
  46. static char alaw_silence[BUF_SIZE];
  47. /* #define REALTIME_WRITE */ /* XXX does it work at all ? */
  48. #ifdef REALTIME_WRITE
  49. struct pcm_desc {
  50. unsigned long start_time;
  51. };
  52. /* Returns time in msec since system boot. */
  53. static unsigned long get_time(void)
  54. {
  55. struct tms buf;
  56. clock_t cur;
  57. cur = times( &buf );
  58. if( cur < 0 ) {
  59. ast_log( LOG_WARNING, "Cannot get current time\n" );
  60. return 0;
  61. }
  62. return cur * 1000 / sysconf( _SC_CLK_TCK );
  63. }
  64. static int pcma_open(struct ast_filestream *s)
  65. {
  66. if (s->fmt->format == AST_FORMAT_ALAW)
  67. pd->starttime = get_time();
  68. return 0;
  69. }
  70. static int pcma_rewrite(struct ast_filestream *s, const char *comment)
  71. {
  72. return pcma_open(s);
  73. }
  74. #endif
  75. static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
  76. {
  77. int res;
  78. /* Send a frame from the file to the appropriate channel */
  79. s->fr.frametype = AST_FRAME_VOICE;
  80. s->fr.subclass = s->fmt->format;
  81. s->fr.mallocd = 0;
  82. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  83. if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) {
  84. if (res)
  85. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  86. return NULL;
  87. }
  88. s->fr.datalen = res;
  89. if (s->fmt->format == AST_FORMAT_G722)
  90. *whennext = s->fr.samples = res * 2;
  91. else
  92. *whennext = s->fr.samples = res;
  93. return &s->fr;
  94. }
  95. static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  96. {
  97. off_t cur, max, offset = 0;
  98. int ret = -1; /* assume error */
  99. cur = ftello(fs->f);
  100. fseeko(fs->f, 0, SEEK_END);
  101. max = ftello(fs->f);
  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 == 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. return ftruncate(fileno(fs->f), ftello(fs->f));
  143. }
  144. static off_t pcm_tell(struct ast_filestream *fs)
  145. {
  146. return ftello(fs->f);
  147. }
  148. static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
  149. {
  150. int res;
  151. if (f->frametype != AST_FRAME_VOICE) {
  152. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  153. return -1;
  154. }
  155. if (f->subclass != fs->fmt->format) {
  156. ast_log(LOG_WARNING, "Asked to write incompatible format frame (%d)!\n", f->subclass);
  157. return -1;
  158. }
  159. #ifdef REALTIME_WRITE
  160. if (s->fmt->format == AST_FORMAT_ALAW) {
  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, 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: %d. 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 %d\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=%d\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: %d\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, max, cur;
  341. long offset = 0, bytes;
  342. if (fs->fmt->format == AST_FORMAT_G722)
  343. bytes = sample_offset / 2;
  344. else
  345. bytes = sample_offset;
  346. min = AU_HEADER_SIZE;
  347. cur = ftello(fs->f);
  348. fseek(fs->f, 0, SEEK_END);
  349. max = ftello(fs->f);
  350. if (whence == SEEK_SET)
  351. offset = bytes + min;
  352. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  353. offset = bytes + cur;
  354. else if (whence == SEEK_END)
  355. offset = max - bytes;
  356. if (whence != SEEK_FORCECUR) {
  357. offset = (offset > max) ? max : offset;
  358. }
  359. /* always protect the header space. */
  360. offset = (offset < min) ? min : offset;
  361. return fseeko(fs->f, offset, SEEK_SET);
  362. }
  363. static int au_trunc(struct ast_filestream *fs)
  364. {
  365. if (ftruncate(fileno(fs->f), ftell(fs->f)))
  366. return -1;
  367. return update_header(fs->f);
  368. }
  369. static off_t au_tell(struct ast_filestream *fs)
  370. {
  371. off_t offset = ftello(fs->f);
  372. return offset - AU_HEADER_SIZE;
  373. }
  374. static const struct ast_format alaw_f = {
  375. .name = "alaw",
  376. .exts = "alaw|al",
  377. .format = AST_FORMAT_ALAW,
  378. .write = pcm_write,
  379. .seek = pcm_seek,
  380. .trunc = pcm_trunc,
  381. .tell = pcm_tell,
  382. .read = pcm_read,
  383. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  384. #ifdef REALTIME_WRITE
  385. .open = pcma_open,
  386. .rewrite = pcma_rewrite,
  387. .desc_size = sizeof(struct pcm_desc),
  388. #endif
  389. };
  390. static const struct ast_format pcm_f = {
  391. .name = "pcm",
  392. .exts = "pcm|ulaw|ul|mu",
  393. .format = AST_FORMAT_ULAW,
  394. .write = pcm_write,
  395. .seek = pcm_seek,
  396. .trunc = pcm_trunc,
  397. .tell = pcm_tell,
  398. .read = pcm_read,
  399. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  400. };
  401. static const struct ast_format g722_f = {
  402. .name = "g722",
  403. .exts = "g722",
  404. .format = AST_FORMAT_G722,
  405. .write = pcm_write,
  406. .seek = pcm_seek,
  407. .trunc = pcm_trunc,
  408. .tell = pcm_tell,
  409. .read = pcm_read,
  410. .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
  411. };
  412. static const struct ast_format au_f = {
  413. .name = "au",
  414. .exts = "au",
  415. .format = AST_FORMAT_ULAW,
  416. .open = au_open,
  417. .rewrite = au_rewrite,
  418. .write = pcm_write,
  419. .seek = au_seek,
  420. .trunc = au_trunc,
  421. .tell = au_tell,
  422. .read = pcm_read,
  423. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, /* this many shorts */
  424. };
  425. static int load_module(void)
  426. {
  427. int index;
  428. /* XXX better init ? */
  429. for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++)
  430. ulaw_silence[index] = AST_LIN2MU(0);
  431. for (index = 0; index < (sizeof(alaw_silence) / sizeof(alaw_silence[0])); index++)
  432. alaw_silence[index] = AST_LIN2A(0);
  433. return ast_format_register(&pcm_f)
  434. || ast_format_register(&alaw_f)
  435. || ast_format_register(&au_f)
  436. || ast_format_register(&g722_f);
  437. }
  438. static int unload_module(void)
  439. {
  440. return ast_format_unregister(pcm_f.name)
  441. || ast_format_unregister(alaw_f.name)
  442. || ast_format_unregister(au_f.name)
  443. || ast_format_unregister(g722_f.name);
  444. }
  445. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_FIRST, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz",
  446. .load = load_module,
  447. .unload = unload_module,
  448. );