format_gsm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Save to raw, headerless GSM data.
  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. #include "msgsm.h"
  34. /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
  35. /* Portions of the conversion code are by guido@sienanet.it */
  36. /* silent gsm frame */
  37. /* begin binary data: */
  38. char gsm_silence[] = /* 33 */
  39. {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
  40. ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
  41. ,0x92,0x49,0x24};
  42. /* end binary data. size = 33 bytes */
  43. struct ast_filestream {
  44. void *reserved[AST_RESERVED_POINTERS];
  45. /* Believe it or not, we must decode/recode to account for the
  46. weird MS format */
  47. /* This is what a filestream means to us */
  48. int fd; /* Descriptor */
  49. struct ast_frame fr; /* Frame information */
  50. char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
  51. char empty; /* Empty character */
  52. unsigned char gsm[66]; /* Two Real GSM Frames */
  53. };
  54. static ast_mutex_t gsm_lock = AST_MUTEX_INITIALIZER;
  55. static int glistcnt = 0;
  56. static char *name = "gsm";
  57. static char *desc = "Raw GSM data";
  58. static char *exts = "gsm";
  59. static struct ast_filestream *gsm_open(int fd)
  60. {
  61. /* We don't have any header to read or anything really, but
  62. if we did, it would go here. We also might want to check
  63. and be sure it's a valid file. */
  64. struct ast_filestream *tmp;
  65. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  66. memset(tmp, 0, sizeof(struct ast_filestream));
  67. if (ast_mutex_lock(&gsm_lock)) {
  68. ast_log(LOG_WARNING, "Unable to lock gsm list\n");
  69. free(tmp);
  70. return NULL;
  71. }
  72. tmp->fd = fd;
  73. tmp->fr.data = tmp->gsm;
  74. tmp->fr.frametype = AST_FRAME_VOICE;
  75. tmp->fr.subclass = AST_FORMAT_GSM;
  76. /* datalen will vary for each frame */
  77. tmp->fr.src = name;
  78. tmp->fr.mallocd = 0;
  79. glistcnt++;
  80. ast_mutex_unlock(&gsm_lock);
  81. ast_update_use_count();
  82. }
  83. return tmp;
  84. }
  85. static struct ast_filestream *gsm_rewrite(int fd, char *comment)
  86. {
  87. /* We don't have any header to read or anything really, but
  88. if we did, it would go here. We also might want to check
  89. and be sure it's a valid file. */
  90. struct ast_filestream *tmp;
  91. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  92. memset(tmp, 0, sizeof(struct ast_filestream));
  93. if (ast_mutex_lock(&gsm_lock)) {
  94. ast_log(LOG_WARNING, "Unable to lock gsm list\n");
  95. free(tmp);
  96. return NULL;
  97. }
  98. tmp->fd = fd;
  99. glistcnt++;
  100. ast_mutex_unlock(&gsm_lock);
  101. ast_update_use_count();
  102. } else
  103. ast_log(LOG_WARNING, "Out of memory\n");
  104. return tmp;
  105. }
  106. static void gsm_close(struct ast_filestream *s)
  107. {
  108. if (ast_mutex_lock(&gsm_lock)) {
  109. ast_log(LOG_WARNING, "Unable to lock gsm list\n");
  110. return;
  111. }
  112. glistcnt--;
  113. ast_mutex_unlock(&gsm_lock);
  114. ast_update_use_count();
  115. close(s->fd);
  116. free(s);
  117. }
  118. static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
  119. {
  120. int res;
  121. s->fr.frametype = AST_FRAME_VOICE;
  122. s->fr.subclass = AST_FORMAT_GSM;
  123. s->fr.offset = AST_FRIENDLY_OFFSET;
  124. s->fr.samples = 160;
  125. s->fr.datalen = 33;
  126. s->fr.mallocd = 0;
  127. s->fr.data = s->gsm;
  128. if ((res = read(s->fd, s->gsm, 33)) != 33) {
  129. if (res)
  130. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  131. return NULL;
  132. }
  133. *whennext = 160;
  134. return &s->fr;
  135. }
  136. static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
  137. {
  138. int res;
  139. unsigned char gsm[66];
  140. if (f->frametype != AST_FRAME_VOICE) {
  141. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  142. return -1;
  143. }
  144. if (f->subclass != AST_FORMAT_GSM) {
  145. ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
  146. return -1;
  147. }
  148. if (!(f->datalen % 65)) {
  149. /* This is in MSGSM format, need to be converted */
  150. int len=0;
  151. while(len < f->datalen) {
  152. conv65(f->data + len, gsm);
  153. if ((res = write(fs->fd, gsm, 66)) != 66) {
  154. ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
  155. return -1;
  156. }
  157. len += 65;
  158. }
  159. } else {
  160. if (f->datalen % 33) {
  161. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
  162. return -1;
  163. }
  164. if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
  165. ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
  166. return -1;
  167. }
  168. }
  169. return 0;
  170. }
  171. static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
  172. {
  173. off_t offset=0,min,cur,max,distance;
  174. min = 0;
  175. cur = lseek(fs->fd, 0, SEEK_CUR);
  176. max = lseek(fs->fd, 0, SEEK_END);
  177. /* have to fudge to frame here, so not fully to sample */
  178. distance = (sample_offset/160) * 33;
  179. if(whence == SEEK_SET)
  180. offset = distance;
  181. else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
  182. offset = distance + cur;
  183. else if(whence == SEEK_END)
  184. offset = max - distance;
  185. if (whence != SEEK_FORCECUR) {
  186. offset = (offset > max)?max:offset;
  187. offset = (offset < min)?min:offset;
  188. } else if (offset > max) {
  189. int i;
  190. lseek(fs->fd, 0, SEEK_END);
  191. for (i=0; i< (offset - max) / 33; i++) {
  192. write(fs->fd, gsm_silence, 33);
  193. }
  194. }
  195. return lseek(fs->fd, offset, SEEK_SET);
  196. }
  197. static int gsm_trunc(struct ast_filestream *fs)
  198. {
  199. return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
  200. }
  201. static long gsm_tell(struct ast_filestream *fs)
  202. {
  203. off_t offset;
  204. offset = lseek(fs->fd, 0, SEEK_CUR);
  205. return (offset/33)*160;
  206. }
  207. static char *gsm_getcomment(struct ast_filestream *s)
  208. {
  209. return NULL;
  210. }
  211. int load_module()
  212. {
  213. return ast_format_register(name, exts, AST_FORMAT_GSM,
  214. gsm_open,
  215. gsm_rewrite,
  216. gsm_write,
  217. gsm_seek,
  218. gsm_trunc,
  219. gsm_tell,
  220. gsm_read,
  221. gsm_close,
  222. gsm_getcomment);
  223. }
  224. int unload_module()
  225. {
  226. return ast_format_unregister(name);
  227. }
  228. int usecount()
  229. {
  230. int res;
  231. if (ast_mutex_lock(&gsm_lock)) {
  232. ast_log(LOG_WARNING, "Unable to lock gsm list\n");
  233. return -1;
  234. }
  235. res = glistcnt;
  236. ast_mutex_unlock(&gsm_lock);
  237. return res;
  238. }
  239. char *description()
  240. {
  241. return desc;
  242. }
  243. char *key()
  244. {
  245. return ASTERISK_GPL_KEY;
  246. }