format_pcm_alaw.c 7.1 KB

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