format_gsm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "msgsm.h"
  33. /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
  34. /* Portions of the conversion code are by guido@sienanet.it */
  35. #define GSM_FRAME_SIZE 33
  36. #define GSM_SAMPLES 160
  37. /* silent gsm frame */
  38. /* begin binary data: */
  39. static const char gsm_silence[] = /* 33 */
  40. {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
  41. ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
  42. ,0x92,0x49,0x24};
  43. /* end binary data. size = 33 bytes */
  44. static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
  45. {
  46. int res;
  47. s->fr.frametype = AST_FRAME_VOICE;
  48. ast_format_set(&s->fr.subclass.format, AST_FORMAT_GSM, 0);
  49. AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
  50. s->fr.mallocd = 0;
  51. if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
  52. if (res)
  53. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  54. return NULL;
  55. }
  56. *whennext = s->fr.samples = GSM_SAMPLES;
  57. return &s->fr;
  58. }
  59. static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
  60. {
  61. int res;
  62. unsigned char gsm[2*GSM_FRAME_SIZE];
  63. if (f->frametype != AST_FRAME_VOICE) {
  64. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  65. return -1;
  66. }
  67. if (f->subclass.format.id != AST_FORMAT_GSM) {
  68. ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(&f->subclass.format));
  69. return -1;
  70. }
  71. if (!(f->datalen % 65)) {
  72. /* This is in MSGSM format, need to be converted */
  73. int len=0;
  74. while(len < f->datalen) {
  75. conv65(f->data.ptr + len, gsm);
  76. if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
  77. ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
  78. return -1;
  79. }
  80. len += 65;
  81. }
  82. } else {
  83. if (f->datalen % GSM_FRAME_SIZE) {
  84. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
  85. return -1;
  86. }
  87. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  88. ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
  89. return -1;
  90. }
  91. }
  92. return 0;
  93. }
  94. static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  95. {
  96. off_t offset = 0, min = 0, cur, max, distance;
  97. if ((cur = ftello(fs->f)) < 0) {
  98. ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
  99. return -1;
  100. }
  101. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  102. ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
  103. return -1;
  104. }
  105. if ((max = ftello(fs->f)) < 0) {
  106. ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
  107. return -1;
  108. }
  109. /* have to fudge to frame here, so not fully to sample */
  110. distance = (sample_offset / GSM_SAMPLES) * GSM_FRAME_SIZE;
  111. if (whence == SEEK_SET) {
  112. offset = distance;
  113. } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
  114. offset = distance + cur;
  115. } else if (whence == SEEK_END) {
  116. offset = max - distance;
  117. }
  118. /* Always protect against seeking past the begining. */
  119. offset = (offset < min)?min:offset;
  120. if (whence != SEEK_FORCECUR) {
  121. offset = (offset > max)?max:offset;
  122. } else if (offset > max) {
  123. int i;
  124. fseeko(fs->f, 0, SEEK_END);
  125. for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
  126. if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
  127. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  128. }
  129. }
  130. }
  131. return fseeko(fs->f, offset, SEEK_SET);
  132. }
  133. static int gsm_trunc(struct ast_filestream *fs)
  134. {
  135. int fd;
  136. off_t cur;
  137. if ((fd = fileno(fs->f)) < 0) {
  138. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm filestream %p: %s\n", fs, strerror(errno));
  139. return -1;
  140. }
  141. if ((cur = ftello(fs->f)) < 0) {
  142. ast_log(AST_LOG_WARNING, "Unable to determine current position in gsm filestream %p: %s\n", fs, strerror(errno));
  143. return -1;
  144. }
  145. /* Truncate file to current length */
  146. return ftruncate(fd, cur);
  147. }
  148. static off_t gsm_tell(struct ast_filestream *fs)
  149. {
  150. off_t offset = ftello(fs->f);
  151. if (offset < 0) {
  152. ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno));
  153. return 0;
  154. }
  155. return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES;
  156. }
  157. static struct ast_format_def gsm_f = {
  158. .name = "gsm",
  159. .exts = "gsm",
  160. .write = gsm_write,
  161. .seek = gsm_seek,
  162. .trunc = gsm_trunc,
  163. .tell = gsm_tell,
  164. .read = gsm_read,
  165. .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */
  166. };
  167. static int load_module(void)
  168. {
  169. ast_format_set(&gsm_f.format, AST_FORMAT_GSM, 0);
  170. if (ast_format_def_register(&gsm_f))
  171. return AST_MODULE_LOAD_FAILURE;
  172. return AST_MODULE_LOAD_SUCCESS;
  173. }
  174. static int unload_module(void)
  175. {
  176. return ast_format_def_unregister(gsm_f.name);
  177. }
  178. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw GSM data",
  179. .load = load_module,
  180. .unload = unload_module,
  181. .load_pri = AST_MODPRI_APP_DEPEND
  182. );