format_ilbc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Save to raw, headerless iLBC data.
  5. *
  6. * Brian K. West <brian@bkw.org>
  7. *
  8. * Copyright (C) 1999, Mark Spencer
  9. *
  10. * Mark Spencer <markster@linux-support.net>
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License
  14. */
  15. #include <asterisk/lock.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/file.h>
  18. #include <asterisk/logger.h>
  19. #include <asterisk/sched.h>
  20. #include <asterisk/module.h>
  21. #include <netinet/in.h>
  22. #include <arpa/inet.h>
  23. #include <stdlib.h>
  24. #include <sys/time.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include "asterisk/endian.h"
  30. /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
  31. /* Portions of the conversion code are by guido@sienanet.it */
  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 ilbc[50]; /* One Real iLBC Frame */
  42. };
  43. AST_MUTEX_DEFINE_STATIC(ilbc_lock);
  44. static int glistcnt = 0;
  45. static char *name = "iLBC";
  46. static char *desc = "Raw iLBC data";
  47. static char *exts = "ilbc";
  48. static struct ast_filestream *ilbc_open(int fd)
  49. {
  50. /* We don't have any header to read or anything really, but
  51. if we did, it would go here. We also might want to check
  52. and be sure it's a valid file. */
  53. struct ast_filestream *tmp;
  54. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  55. memset(tmp, 0, sizeof(struct ast_filestream));
  56. if (ast_mutex_lock(&ilbc_lock)) {
  57. ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
  58. free(tmp);
  59. return NULL;
  60. }
  61. tmp->fd = fd;
  62. tmp->fr.data = tmp->ilbc;
  63. tmp->fr.frametype = AST_FRAME_VOICE;
  64. tmp->fr.subclass = AST_FORMAT_ILBC;
  65. /* datalen will vary for each frame */
  66. tmp->fr.src = name;
  67. tmp->fr.mallocd = 0;
  68. glistcnt++;
  69. ast_mutex_unlock(&ilbc_lock);
  70. ast_update_use_count();
  71. }
  72. return tmp;
  73. }
  74. static struct ast_filestream *ilbc_rewrite(int fd, char *comment)
  75. {
  76. /* We don't have any header to read or anything really, but
  77. if we did, it would go here. We also might want to check
  78. and be sure it's a valid file. */
  79. struct ast_filestream *tmp;
  80. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  81. memset(tmp, 0, sizeof(struct ast_filestream));
  82. if (ast_mutex_lock(&ilbc_lock)) {
  83. ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
  84. free(tmp);
  85. return NULL;
  86. }
  87. tmp->fd = fd;
  88. glistcnt++;
  89. ast_mutex_unlock(&ilbc_lock);
  90. ast_update_use_count();
  91. } else
  92. ast_log(LOG_WARNING, "Out of memory\n");
  93. return tmp;
  94. }
  95. static void ilbc_close(struct ast_filestream *s)
  96. {
  97. if (ast_mutex_lock(&ilbc_lock)) {
  98. ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
  99. return;
  100. }
  101. glistcnt--;
  102. ast_mutex_unlock(&ilbc_lock);
  103. ast_update_use_count();
  104. close(s->fd);
  105. free(s);
  106. s = NULL;
  107. }
  108. static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
  109. {
  110. int res;
  111. /* Send a frame from the file to the appropriate channel */
  112. s->fr.frametype = AST_FRAME_VOICE;
  113. s->fr.subclass = AST_FORMAT_ILBC;
  114. s->fr.offset = AST_FRIENDLY_OFFSET;
  115. s->fr.samples = 240;
  116. s->fr.datalen = 50;
  117. s->fr.mallocd = 0;
  118. s->fr.data = s->ilbc;
  119. if ((res = read(s->fd, s->ilbc, 50)) != 50) {
  120. if (res)
  121. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  122. return NULL;
  123. }
  124. *whennext = s->fr.samples;
  125. return &s->fr;
  126. }
  127. static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
  128. {
  129. int res;
  130. if (f->frametype != AST_FRAME_VOICE) {
  131. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  132. return -1;
  133. }
  134. if (f->subclass != AST_FORMAT_ILBC) {
  135. ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%d)!\n", f->subclass);
  136. return -1;
  137. }
  138. if (f->datalen % 50) {
  139. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
  140. return -1;
  141. }
  142. if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
  143. ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno));
  144. return -1;
  145. }
  146. return 0;
  147. }
  148. static char *ilbc_getcomment(struct ast_filestream *s)
  149. {
  150. return NULL;
  151. }
  152. static int ilbc_seek(struct ast_filestream *fs, long sample_offset, int whence)
  153. {
  154. long bytes;
  155. off_t min,cur,max,offset=0;
  156. min = 0;
  157. cur = lseek(fs->fd, 0, SEEK_CUR);
  158. max = lseek(fs->fd, 0, SEEK_END);
  159. bytes = 50 * (sample_offset / 240);
  160. if (whence == SEEK_SET)
  161. offset = bytes;
  162. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  163. offset = cur + bytes;
  164. else if (whence == SEEK_END)
  165. offset = max - bytes;
  166. if (whence != SEEK_FORCECUR) {
  167. offset = (offset > max)?max:offset;
  168. }
  169. // protect against seeking beyond begining.
  170. offset = (offset < min)?min:offset;
  171. if (lseek(fs->fd, offset, SEEK_SET) < 0)
  172. return -1;
  173. return 0;
  174. }
  175. static int ilbc_trunc(struct ast_filestream *fs)
  176. {
  177. /* Truncate file to current length */
  178. if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
  179. return -1;
  180. return 0;
  181. }
  182. static long ilbc_tell(struct ast_filestream *fs)
  183. {
  184. off_t offset;
  185. offset = lseek(fs->fd, 0, SEEK_CUR);
  186. return (offset/50)*240;
  187. }
  188. int load_module()
  189. {
  190. return ast_format_register(name, exts, AST_FORMAT_ILBC,
  191. ilbc_open,
  192. ilbc_rewrite,
  193. ilbc_write,
  194. ilbc_seek,
  195. ilbc_trunc,
  196. ilbc_tell,
  197. ilbc_read,
  198. ilbc_close,
  199. ilbc_getcomment);
  200. }
  201. int unload_module()
  202. {
  203. return ast_format_unregister(name);
  204. }
  205. int usecount()
  206. {
  207. int res;
  208. if (ast_mutex_lock(&ilbc_lock)) {
  209. ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
  210. return -1;
  211. }
  212. res = glistcnt;
  213. ast_mutex_unlock(&ilbc_lock);
  214. return res;
  215. }
  216. char *description()
  217. {
  218. return desc;
  219. }
  220. char *key()
  221. {
  222. return ASTERISK_GPL_KEY;
  223. }