format_pcm_alaw.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Flat, binary, alaw 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 <sys/times.h>
  24. #include <sys/types.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <pthread.h>
  30. #ifdef __linux__
  31. #include <endian.h>
  32. #else
  33. #include <machine/endian.h>
  34. #endif
  35. #define BUF_SIZE 160 /* 160 samples */
  36. // #define REALTIME_WRITE
  37. struct ast_filestream {
  38. void *reserved[AST_RESERVED_POINTERS];
  39. /* Believe it or not, we must decode/recode to account for the
  40. weird MS format */
  41. /* This is what a filestream means to us */
  42. int fd; /* Descriptor */
  43. struct ast_frame fr; /* Frame information */
  44. char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
  45. char empty; /* Empty character */
  46. unsigned char buf[BUF_SIZE]; /* Output Buffer */
  47. #ifdef REALTIME_WRITE
  48. unsigned long start_time;
  49. #endif
  50. };
  51. static ast_mutex_t pcm_lock = AST_MUTEX_INITIALIZER;
  52. static int glistcnt = 0;
  53. static char *name = "alaw";
  54. static char *desc = "Raw aLaw 8khz PCM Audio support";
  55. static char *exts = "alaw|al";
  56. #if 0
  57. /* Returns time in msec since system boot. */
  58. static unsigned long get_time(void)
  59. {
  60. struct tms buf;
  61. clock_t cur;
  62. cur = times( &buf );
  63. if( cur < 0 )
  64. {
  65. ast_log( LOG_WARNING, "Cannot get current time\n" );
  66. return 0;
  67. }
  68. return cur * 1000 / sysconf( _SC_CLK_TCK );
  69. }
  70. #endif
  71. static struct ast_filestream *pcm_open(int fd)
  72. {
  73. /* We don't have any header to read or anything really, but
  74. if we did, it would go here. We also might want to check
  75. and be sure it's a valid file. */
  76. struct ast_filestream *tmp;
  77. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  78. memset(tmp, 0, sizeof(struct ast_filestream));
  79. if (ast_mutex_lock(&pcm_lock)) {
  80. ast_log(LOG_WARNING, "Unable to lock pcm list\n");
  81. free(tmp);
  82. return NULL;
  83. }
  84. tmp->fd = fd;
  85. tmp->fr.data = tmp->buf;
  86. tmp->fr.frametype = AST_FRAME_VOICE;
  87. tmp->fr.subclass = AST_FORMAT_ALAW;
  88. /* datalen will vary for each frame */
  89. tmp->fr.src = name;
  90. tmp->fr.mallocd = 0;
  91. #ifdef REALTIME_WRITE
  92. tmp->start_time = get_time();
  93. #endif
  94. glistcnt++;
  95. ast_mutex_unlock(&pcm_lock);
  96. ast_update_use_count();
  97. }
  98. return tmp;
  99. }
  100. static struct ast_filestream *pcm_rewrite(int fd, char *comment)
  101. {
  102. /* We don't have any header to read or anything really, but
  103. if we did, it would go here. We also might want to check
  104. and be sure it's a valid file. */
  105. struct ast_filestream *tmp;
  106. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  107. memset(tmp, 0, sizeof(struct ast_filestream));
  108. if (ast_mutex_lock(&pcm_lock)) {
  109. ast_log(LOG_WARNING, "Unable to lock pcm list\n");
  110. free(tmp);
  111. return NULL;
  112. }
  113. tmp->fd = fd;
  114. #ifdef REALTIME_WRITE
  115. tmp->start_time = get_time();
  116. #endif
  117. glistcnt++;
  118. ast_mutex_unlock(&pcm_lock);
  119. ast_update_use_count();
  120. } else
  121. ast_log(LOG_WARNING, "Out of memory\n");
  122. return tmp;
  123. }
  124. static void pcm_close(struct ast_filestream *s)
  125. {
  126. if (ast_mutex_lock(&pcm_lock)) {
  127. ast_log(LOG_WARNING, "Unable to lock pcm list\n");
  128. return;
  129. }
  130. glistcnt--;
  131. ast_mutex_unlock(&pcm_lock);
  132. ast_update_use_count();
  133. close(s->fd);
  134. free(s);
  135. s = NULL;
  136. }
  137. static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
  138. {
  139. int res;
  140. /* Send a frame from the file to the appropriate channel */
  141. s->fr.frametype = AST_FRAME_VOICE;
  142. s->fr.subclass = AST_FORMAT_ALAW;
  143. s->fr.offset = AST_FRIENDLY_OFFSET;
  144. s->fr.mallocd = 0;
  145. s->fr.data = s->buf;
  146. if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) {
  147. if (res)
  148. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  149. return NULL;
  150. }
  151. s->fr.samples = res;
  152. s->fr.datalen = res;
  153. *whennext = s->fr.samples;
  154. return &s->fr;
  155. }
  156. static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
  157. {
  158. int res;
  159. #ifdef REALTIME_WRITE
  160. unsigned long cur_time;
  161. unsigned long fpos;
  162. struct stat stat_buf;
  163. #endif
  164. if (f->frametype != AST_FRAME_VOICE) {
  165. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  166. return -1;
  167. }
  168. if (f->subclass != AST_FORMAT_ALAW) {
  169. ast_log(LOG_WARNING, "Asked to write non-alaw frame (%d)!\n", f->subclass);
  170. return -1;
  171. }
  172. #ifdef REALTIME_WRITE
  173. cur_time = get_time();
  174. fpos = ( cur_time - fs->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. fstat( fs->fd, &stat_buf );
  178. if( stat_buf.st_size > fpos )
  179. {
  180. fpos += f->datalen; // Incrementing with the size of this current frame
  181. }
  182. if( stat_buf.st_size < fpos )
  183. {
  184. // fill the gap with 0x55 rather than 0.
  185. char buf[ 512 ];
  186. unsigned long cur, to_write;
  187. cur = stat_buf.st_size;
  188. if( lseek( fs->fd, cur, SEEK_SET ) < 0 )
  189. {
  190. ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
  191. return -1;
  192. }
  193. memset( buf, 0x55, 512 );
  194. while( cur < fpos )
  195. {
  196. to_write = fpos - cur;
  197. if( to_write > 512 )
  198. {
  199. to_write = 512;
  200. }
  201. write( fs->fd, buf, to_write );
  202. cur += to_write;
  203. }
  204. }
  205. if( lseek( fs->fd, fpos, SEEK_SET ) < 0 )
  206. {
  207. ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
  208. return -1;
  209. }
  210. #endif // REALTIME_WRITE
  211. if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
  212. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
  218. {
  219. off_t offset=0,min,cur,max;
  220. min = 0;
  221. cur = lseek(fs->fd, 0, SEEK_CUR);
  222. max = lseek(fs->fd, 0, SEEK_END);
  223. if (whence == SEEK_SET)
  224. offset = sample_offset;
  225. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  226. offset = sample_offset + cur;
  227. else if (whence == SEEK_END)
  228. offset = max - sample_offset;
  229. if (whence != SEEK_FORCECUR) {
  230. offset = (offset > max)?max:offset;
  231. offset = (offset < min)?min:offset;
  232. }
  233. return lseek(fs->fd, offset, SEEK_SET);
  234. }
  235. static int pcm_trunc(struct ast_filestream *fs)
  236. {
  237. return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
  238. }
  239. static long pcm_tell(struct ast_filestream *fs)
  240. {
  241. off_t offset;
  242. offset = lseek(fs->fd, 0, SEEK_CUR);
  243. return offset;
  244. }
  245. static char *pcm_getcomment(struct ast_filestream *s)
  246. {
  247. return NULL;
  248. }
  249. int load_module()
  250. {
  251. return ast_format_register(name, exts, AST_FORMAT_ALAW,
  252. pcm_open,
  253. pcm_rewrite,
  254. pcm_write,
  255. pcm_seek,
  256. pcm_trunc,
  257. pcm_tell,
  258. pcm_read,
  259. pcm_close,
  260. pcm_getcomment);
  261. }
  262. int unload_module()
  263. {
  264. return ast_format_unregister(name);
  265. }
  266. int usecount()
  267. {
  268. int res;
  269. if (ast_mutex_lock(&pcm_lock)) {
  270. ast_log(LOG_WARNING, "Unable to lock pcm list\n");
  271. return -1;
  272. }
  273. res = glistcnt;
  274. ast_mutex_unlock(&pcm_lock);
  275. return res;
  276. }
  277. char *description()
  278. {
  279. return desc;
  280. }
  281. char *key()
  282. {
  283. return ASTERISK_GPL_KEY;
  284. }