codec_gsm.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Translate between signed linear and Global System for Mobile Communications (GSM)
  5. *
  6. * The GSM code is from TOAST. Copyright information for that package is available
  7. * in the GSM directory.
  8. *
  9. * Copyright (C) 1999, Mark Spencer
  10. *
  11. * Mark Spencer <markster@linux-support.net>
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License
  15. */
  16. #define TYPE_SILENCE 0x2
  17. #define TYPE_HIGH 0x0
  18. #define TYPE_LOW 0x1
  19. #define TYPE_MASK 0x3
  20. #include <asterisk/lock.h>
  21. #include <asterisk/translate.h>
  22. #include <asterisk/module.h>
  23. #include <asterisk/logger.h>
  24. #include <asterisk/channel.h>
  25. #include <fcntl.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <netinet/in.h>
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include "gsm/inc/gsm.h"
  32. #include "../formats/msgsm.h"
  33. /* Sample frame data */
  34. #include "slin_gsm_ex.h"
  35. #include "gsm_slin_ex.h"
  36. AST_MUTEX_DEFINE_STATIC(localuser_lock);
  37. static int localusecnt=0;
  38. static char *tdesc = "GSM/PCM16 (signed linear) Codec Translator";
  39. struct ast_translator_pvt {
  40. gsm gsm;
  41. struct ast_frame f;
  42. /* Space to build offset */
  43. char offset[AST_FRIENDLY_OFFSET];
  44. /* Buffer for our outgoing frame */
  45. short outbuf[8000];
  46. /* Enough to store a full second */
  47. short buf[8000];
  48. int tail;
  49. };
  50. #define gsm_coder_pvt ast_translator_pvt
  51. static struct ast_translator_pvt *gsm_new(void)
  52. {
  53. struct gsm_coder_pvt *tmp;
  54. tmp = malloc(sizeof(struct gsm_coder_pvt));
  55. if (tmp) {
  56. if (!(tmp->gsm = gsm_create())) {
  57. free(tmp);
  58. tmp = NULL;
  59. }
  60. tmp->tail = 0;
  61. localusecnt++;
  62. }
  63. return tmp;
  64. }
  65. static struct ast_frame *lintogsm_sample(void)
  66. {
  67. static struct ast_frame f;
  68. f.frametype = AST_FRAME_VOICE;
  69. f.subclass = AST_FORMAT_SLINEAR;
  70. f.datalen = sizeof(slin_gsm_ex);
  71. /* Assume 8000 Hz */
  72. f.samples = sizeof(slin_gsm_ex)/2;
  73. f.mallocd = 0;
  74. f.offset = 0;
  75. f.src = __PRETTY_FUNCTION__;
  76. f.data = slin_gsm_ex;
  77. return &f;
  78. }
  79. static struct ast_frame *gsmtolin_sample(void)
  80. {
  81. static struct ast_frame f;
  82. f.frametype = AST_FRAME_VOICE;
  83. f.subclass = AST_FORMAT_GSM;
  84. f.datalen = sizeof(gsm_slin_ex);
  85. /* All frames are 20 ms long */
  86. f.samples = 160;
  87. f.mallocd = 0;
  88. f.offset = 0;
  89. f.src = __PRETTY_FUNCTION__;
  90. f.data = gsm_slin_ex;
  91. return &f;
  92. }
  93. static struct ast_frame *gsmtolin_frameout(struct ast_translator_pvt *tmp)
  94. {
  95. if (!tmp->tail)
  96. return NULL;
  97. /* Signed linear is no particular frame size, so just send whatever
  98. we have in the buffer in one lump sum */
  99. tmp->f.frametype = AST_FRAME_VOICE;
  100. tmp->f.subclass = AST_FORMAT_SLINEAR;
  101. tmp->f.datalen = tmp->tail * 2;
  102. /* Assume 8000 Hz */
  103. tmp->f.samples = tmp->tail;
  104. tmp->f.mallocd = 0;
  105. tmp->f.offset = AST_FRIENDLY_OFFSET;
  106. tmp->f.src = __PRETTY_FUNCTION__;
  107. tmp->f.data = tmp->buf;
  108. /* Reset tail pointer */
  109. tmp->tail = 0;
  110. return &tmp->f;
  111. }
  112. static int gsmtolin_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
  113. {
  114. /* Assuming there's space left, decode into the current buffer at
  115. the tail location. Read in as many frames as there are */
  116. int x;
  117. unsigned char data[66];
  118. int msgsm=0;
  119. if ((f->datalen % 33) && (f->datalen % 65)) {
  120. ast_log(LOG_WARNING, "Huh? A GSM frame that isn't a multiple of 33 or 65 bytes long from %s (%d)?\n", f->src, f->datalen);
  121. return -1;
  122. }
  123. if (f->datalen % 65 == 0)
  124. msgsm = 1;
  125. for (x=0;x<f->datalen;x+=(msgsm ? 65 : 33)) {
  126. if (msgsm) {
  127. /* Translate MSGSM format to Real GSM format before feeding in */
  128. conv65(f->data + x, data);
  129. if (tmp->tail + 320 < sizeof(tmp->buf)/2) {
  130. if (gsm_decode(tmp->gsm, data, tmp->buf + tmp->tail)) {
  131. ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
  132. return -1;
  133. }
  134. tmp->tail+=160;
  135. if (gsm_decode(tmp->gsm, data + 33, tmp->buf + tmp->tail)) {
  136. ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
  137. return -1;
  138. }
  139. tmp->tail+=160;
  140. } else {
  141. ast_log(LOG_WARNING, "Out of (MS) buffer space\n");
  142. return -1;
  143. }
  144. } else {
  145. if (tmp->tail + 160 < sizeof(tmp->buf)/2) {
  146. if (gsm_decode(tmp->gsm, f->data + x, tmp->buf + tmp->tail)) {
  147. ast_log(LOG_WARNING, "Invalid GSM data\n");
  148. return -1;
  149. }
  150. tmp->tail+=160;
  151. } else {
  152. ast_log(LOG_WARNING, "Out of buffer space\n");
  153. return -1;
  154. }
  155. }
  156. }
  157. return 0;
  158. }
  159. static int lintogsm_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
  160. {
  161. /* Just add the frames to our stream */
  162. /* XXX We should look at how old the rest of our stream is, and if it
  163. is too old, then we should overwrite it entirely, otherwise we can
  164. get artifacts of earlier talk that do not belong */
  165. if (tmp->tail + f->datalen/2 < sizeof(tmp->buf) / 2) {
  166. memcpy((tmp->buf + tmp->tail), f->data, f->datalen);
  167. tmp->tail += f->datalen/2;
  168. } else {
  169. ast_log(LOG_WARNING, "Out of buffer space\n");
  170. return -1;
  171. }
  172. return 0;
  173. }
  174. static struct ast_frame *lintogsm_frameout(struct ast_translator_pvt *tmp)
  175. {
  176. int x=0;
  177. /* We can't work on anything less than a frame in size */
  178. if (tmp->tail < 160)
  179. return NULL;
  180. tmp->f.frametype = AST_FRAME_VOICE;
  181. tmp->f.subclass = AST_FORMAT_GSM;
  182. tmp->f.mallocd = 0;
  183. tmp->f.offset = AST_FRIENDLY_OFFSET;
  184. tmp->f.src = __PRETTY_FUNCTION__;
  185. tmp->f.data = tmp->outbuf;
  186. while(tmp->tail >= 160) {
  187. if ((x+1) * 33 >= sizeof(tmp->outbuf)) {
  188. ast_log(LOG_WARNING, "Out of buffer space\n");
  189. break;
  190. }
  191. /* Encode a frame of data */
  192. gsm_encode(tmp->gsm, tmp->buf, ((gsm_byte *) tmp->outbuf) + (x * 33));
  193. /* Assume 8000 Hz -- 20 ms */
  194. tmp->tail -= 160;
  195. /* Move the data at the end of the buffer to the front */
  196. if (tmp->tail)
  197. memmove(tmp->buf, tmp->buf + 160, tmp->tail * 2);
  198. x++;
  199. }
  200. tmp->f.datalen = x * 33;
  201. tmp->f.samples = x * 160;
  202. return &tmp->f;
  203. }
  204. static void gsm_destroy_stuff(struct ast_translator_pvt *pvt)
  205. {
  206. if (pvt->gsm)
  207. gsm_destroy(pvt->gsm);
  208. free(pvt);
  209. localusecnt--;
  210. }
  211. static struct ast_translator gsmtolin =
  212. { "gsmtolin",
  213. AST_FORMAT_GSM, AST_FORMAT_SLINEAR,
  214. gsm_new,
  215. gsmtolin_framein,
  216. gsmtolin_frameout,
  217. gsm_destroy_stuff,
  218. gsmtolin_sample
  219. };
  220. static struct ast_translator lintogsm =
  221. { "lintogsm",
  222. AST_FORMAT_SLINEAR, AST_FORMAT_GSM,
  223. gsm_new,
  224. lintogsm_framein,
  225. lintogsm_frameout,
  226. gsm_destroy_stuff,
  227. lintogsm_sample
  228. };
  229. int unload_module(void)
  230. {
  231. int res;
  232. ast_mutex_lock(&localuser_lock);
  233. res = ast_unregister_translator(&lintogsm);
  234. if (!res)
  235. res = ast_unregister_translator(&gsmtolin);
  236. if (localusecnt)
  237. res = -1;
  238. ast_mutex_unlock(&localuser_lock);
  239. return res;
  240. }
  241. int load_module(void)
  242. {
  243. int res;
  244. res=ast_register_translator(&gsmtolin);
  245. if (!res)
  246. res=ast_register_translator(&lintogsm);
  247. else
  248. ast_unregister_translator(&gsmtolin);
  249. return res;
  250. }
  251. char *description(void)
  252. {
  253. return tdesc;
  254. }
  255. int usecount(void)
  256. {
  257. int res;
  258. STANDARD_USECOUNT(res);
  259. return res;
  260. }
  261. char *key()
  262. {
  263. return ASTERISK_GPL_KEY;
  264. }