format_gsm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Save to raw, headerless GSM data.
  21. * \arg File name extension: gsm
  22. * \ingroup formats
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/mod_format.h"
  30. #include "asterisk/module.h"
  31. #include "asterisk/endian.h"
  32. #include "asterisk/format_cache.h"
  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. #define GSM_FRAME_SIZE 33
  37. #define GSM_SAMPLES 160
  38. /* silent gsm frame */
  39. /* begin binary data: */
  40. static const char gsm_silence[] = /* 33 */
  41. {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
  42. ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
  43. ,0x92,0x49,0x24};
  44. /* end binary data. size = 33 bytes */
  45. static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
  46. {
  47. int res;
  48. AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
  49. if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
  50. if (res)
  51. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  52. return NULL;
  53. }
  54. *whennext = s->fr.samples = GSM_SAMPLES;
  55. return &s->fr;
  56. }
  57. static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
  58. {
  59. int res;
  60. unsigned char gsm[2*GSM_FRAME_SIZE];
  61. if (!(f->datalen % 65)) {
  62. /* This is in MSGSM format, need to be converted */
  63. int len=0;
  64. while(len < f->datalen) {
  65. conv65(f->data.ptr + len, gsm);
  66. if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
  67. ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
  68. return -1;
  69. }
  70. len += 65;
  71. }
  72. } else {
  73. if (f->datalen % GSM_FRAME_SIZE) {
  74. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
  75. return -1;
  76. }
  77. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  78. ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
  79. return -1;
  80. }
  81. }
  82. return 0;
  83. }
  84. static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  85. {
  86. off_t offset = 0, min = 0, cur, max, distance;
  87. if ((cur = ftello(fs->f)) < 0) {
  88. ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
  89. return -1;
  90. }
  91. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  92. ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
  93. return -1;
  94. }
  95. if ((max = ftello(fs->f)) < 0) {
  96. ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
  97. return -1;
  98. }
  99. /* have to fudge to frame here, so not fully to sample */
  100. distance = (sample_offset / GSM_SAMPLES) * GSM_FRAME_SIZE;
  101. if (whence == SEEK_SET) {
  102. offset = distance;
  103. } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
  104. offset = distance + cur;
  105. } else if (whence == SEEK_END) {
  106. offset = max - distance;
  107. }
  108. /* Always protect against seeking past the begining. */
  109. offset = (offset < min)?min:offset;
  110. if (whence != SEEK_FORCECUR) {
  111. offset = (offset > max)?max:offset;
  112. } else if (offset > max) {
  113. int i;
  114. fseeko(fs->f, 0, SEEK_END);
  115. for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
  116. if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
  117. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  118. }
  119. }
  120. }
  121. return fseeko(fs->f, offset, SEEK_SET);
  122. }
  123. static int gsm_trunc(struct ast_filestream *fs)
  124. {
  125. int fd;
  126. off_t cur;
  127. if ((fd = fileno(fs->f)) < 0) {
  128. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm filestream %p: %s\n", fs, strerror(errno));
  129. return -1;
  130. }
  131. if ((cur = ftello(fs->f)) < 0) {
  132. ast_log(AST_LOG_WARNING, "Unable to determine current position in gsm filestream %p: %s\n", fs, strerror(errno));
  133. return -1;
  134. }
  135. /* Truncate file to current length */
  136. return ftruncate(fd, cur);
  137. }
  138. static off_t gsm_tell(struct ast_filestream *fs)
  139. {
  140. off_t offset = ftello(fs->f);
  141. if (offset < 0) {
  142. ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno));
  143. return 0;
  144. }
  145. return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES;
  146. }
  147. static struct ast_format_def gsm_f = {
  148. .name = "gsm",
  149. .exts = "gsm",
  150. .write = gsm_write,
  151. .seek = gsm_seek,
  152. .trunc = gsm_trunc,
  153. .tell = gsm_tell,
  154. .read = gsm_read,
  155. .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */
  156. };
  157. static int load_module(void)
  158. {
  159. gsm_f.format = ast_format_gsm;
  160. if (ast_format_def_register(&gsm_f))
  161. return AST_MODULE_LOAD_FAILURE;
  162. return AST_MODULE_LOAD_SUCCESS;
  163. }
  164. static int unload_module(void)
  165. {
  166. return ast_format_def_unregister(gsm_f.name);
  167. }
  168. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw GSM data",
  169. .support_level = AST_MODULE_SUPPORT_CORE,
  170. .load = load_module,
  171. .unload = unload_module,
  172. .load_pri = AST_MODPRI_APP_DEPEND
  173. );