format_gsm.c 6.4 KB

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