format_pcm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Flat, binary, ulaw PCM file format.
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/channel.h>
  15. #include <asterisk/file.h>
  16. #include <asterisk/logger.h>
  17. #include <asterisk/sched.h>
  18. #include <asterisk/module.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <stdlib.h>
  22. #include <sys/time.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <pthread.h>
  28. #ifdef __linux__
  29. #include <endian.h>
  30. #else
  31. #include <machine/endian.h>
  32. #endif
  33. #define BUF_SIZE 160 /* 160 samples */
  34. struct ast_filestream {
  35. void *reserved[AST_RESERVED_POINTERS];
  36. /* This is what a filestream means to us */
  37. int fd; /* Descriptor */
  38. struct ast_channel *owner;
  39. struct ast_frame fr; /* Frame information */
  40. char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
  41. char empty; /* Empty character */
  42. unsigned char buf[BUF_SIZE]; /* Output Buffer */
  43. struct timeval last;
  44. };
  45. static ast_mutex_t pcm_lock = AST_MUTEX_INITIALIZER;
  46. static int glistcnt = 0;
  47. static char *name = "pcm";
  48. static char *desc = "Raw uLaw 8khz Audio support (PCM)";
  49. static char *exts = "pcm|ulaw|ul|mu";
  50. static struct ast_filestream *pcm_open(int fd)
  51. {
  52. /* We don't have any header to read or anything really, but
  53. if we did, it would go here. We also might want to check
  54. and be sure it's a valid file. */
  55. struct ast_filestream *tmp;
  56. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  57. memset(tmp, 0, sizeof(struct ast_filestream));
  58. if (ast_mutex_lock(&pcm_lock)) {
  59. ast_log(LOG_WARNING, "Unable to lock pcm list\n");
  60. free(tmp);
  61. return NULL;
  62. }
  63. tmp->fd = fd;
  64. tmp->fr.data = tmp->buf;
  65. tmp->fr.frametype = AST_FRAME_VOICE;
  66. tmp->fr.subclass = AST_FORMAT_ULAW;
  67. /* datalen will vary for each frame */
  68. tmp->fr.src = name;
  69. tmp->fr.mallocd = 0;
  70. glistcnt++;
  71. ast_mutex_unlock(&pcm_lock);
  72. ast_update_use_count();
  73. }
  74. return tmp;
  75. }
  76. static struct ast_filestream *pcm_rewrite(int fd, char *comment)
  77. {
  78. /* We don't have any header to read or anything really, but
  79. if we did, it would go here. We also might want to check
  80. and be sure it's a valid file. */
  81. struct ast_filestream *tmp;
  82. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  83. memset(tmp, 0, sizeof(struct ast_filestream));
  84. if (ast_mutex_lock(&pcm_lock)) {
  85. ast_log(LOG_WARNING, "Unable to lock pcm list\n");
  86. free(tmp);
  87. return NULL;
  88. }
  89. tmp->fd = fd;
  90. glistcnt++;
  91. ast_mutex_unlock(&pcm_lock);
  92. ast_update_use_count();
  93. } else
  94. ast_log(LOG_WARNING, "Out of memory\n");
  95. return tmp;
  96. }
  97. static void pcm_close(struct ast_filestream *s)
  98. {
  99. if (ast_mutex_lock(&pcm_lock)) {
  100. ast_log(LOG_WARNING, "Unable to lock pcm list\n");
  101. return;
  102. }
  103. ast_mutex_unlock(&pcm_lock);
  104. ast_update_use_count();
  105. close(s->fd);
  106. free(s);
  107. s = NULL;
  108. }
  109. static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
  110. {
  111. int res;
  112. int delay;
  113. /* Send a frame from the file to the appropriate channel */
  114. s->fr.frametype = AST_FRAME_VOICE;
  115. s->fr.subclass = AST_FORMAT_ULAW;
  116. s->fr.offset = AST_FRIENDLY_OFFSET;
  117. s->fr.mallocd = 0;
  118. s->fr.data = s->buf;
  119. if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) {
  120. if (res)
  121. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  122. return NULL;
  123. }
  124. s->fr.samples = res;
  125. s->fr.datalen = res;
  126. delay = s->fr.samples;
  127. *whennext = delay;
  128. return &s->fr;
  129. }
  130. static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
  131. {
  132. int res;
  133. if (f->frametype != AST_FRAME_VOICE) {
  134. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  135. return -1;
  136. }
  137. if (f->subclass != AST_FORMAT_ULAW) {
  138. ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
  139. return -1;
  140. }
  141. if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
  142. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  143. return -1;
  144. }
  145. return 0;
  146. }
  147. static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
  148. {
  149. off_t offset=0,min,cur,max;
  150. min = 0;
  151. cur = lseek(fs->fd, 0, SEEK_CUR);
  152. max = lseek(fs->fd, 0, SEEK_END);
  153. if (whence == SEEK_SET)
  154. offset = sample_offset;
  155. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  156. offset = sample_offset + cur;
  157. else if (whence == SEEK_END)
  158. offset = max - sample_offset;
  159. if (whence != SEEK_FORCECUR) {
  160. offset = (offset > max)?max:offset;
  161. offset = (offset < min)?min:offset;
  162. }
  163. return lseek(fs->fd, offset, SEEK_SET);
  164. }
  165. static int pcm_trunc(struct ast_filestream *fs)
  166. {
  167. return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
  168. }
  169. static long pcm_tell(struct ast_filestream *fs)
  170. {
  171. off_t offset;
  172. offset = lseek(fs->fd, 0, SEEK_CUR);
  173. return offset;
  174. }
  175. static char *pcm_getcomment(struct ast_filestream *s)
  176. {
  177. return NULL;
  178. }
  179. int load_module()
  180. {
  181. return ast_format_register(name, exts, AST_FORMAT_ULAW,
  182. pcm_open,
  183. pcm_rewrite,
  184. pcm_write,
  185. pcm_seek,
  186. pcm_trunc,
  187. pcm_tell,
  188. pcm_read,
  189. pcm_close,
  190. pcm_getcomment);
  191. }
  192. int unload_module()
  193. {
  194. return ast_format_unregister(name);
  195. }
  196. int usecount()
  197. {
  198. int res;
  199. if (ast_mutex_lock(&pcm_lock)) {
  200. ast_log(LOG_WARNING, "Unable to lock pcm list\n");
  201. return -1;
  202. }
  203. res = glistcnt;
  204. ast_mutex_unlock(&pcm_lock);
  205. return res;
  206. }
  207. char *description()
  208. {
  209. return desc;
  210. }
  211. char *key()
  212. {
  213. return ASTERISK_GPL_KEY;
  214. }