format_gsm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. size_t 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 of %s data (expected %d bytes, read %zu): %s\n",
  52. ast_format_get_name(s->fr.subclass.format), GSM_FRAME_SIZE, res,
  53. strerror(errno));
  54. }
  55. return NULL;
  56. }
  57. *whennext = s->fr.samples = GSM_SAMPLES;
  58. return &s->fr;
  59. }
  60. static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
  61. {
  62. int res;
  63. unsigned char gsm[2*GSM_FRAME_SIZE];
  64. if (!(f->datalen % 65)) {
  65. /* This is in MSGSM format, need to be converted */
  66. int len=0;
  67. while(len < f->datalen) {
  68. conv65(f->data.ptr + len, gsm);
  69. if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
  70. ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
  71. return -1;
  72. }
  73. len += 65;
  74. }
  75. } else {
  76. if (f->datalen % GSM_FRAME_SIZE) {
  77. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
  78. return -1;
  79. }
  80. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  81. ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
  82. return -1;
  83. }
  84. }
  85. return 0;
  86. }
  87. static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  88. {
  89. off_t offset = 0, min = 0, cur, max, distance;
  90. if ((cur = ftello(fs->f)) < 0) {
  91. ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
  92. return -1;
  93. }
  94. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  95. ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
  96. return -1;
  97. }
  98. if ((max = ftello(fs->f)) < 0) {
  99. ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
  100. return -1;
  101. }
  102. /* have to fudge to frame here, so not fully to sample */
  103. distance = (sample_offset / GSM_SAMPLES) * GSM_FRAME_SIZE;
  104. if (whence == SEEK_SET) {
  105. offset = distance;
  106. } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
  107. offset = distance + cur;
  108. } else if (whence == SEEK_END) {
  109. offset = max - distance;
  110. }
  111. /* Always protect against seeking past the begining. */
  112. offset = (offset < min)?min:offset;
  113. if (whence != SEEK_FORCECUR) {
  114. offset = (offset > max)?max:offset;
  115. } else if (offset > max) {
  116. int i;
  117. fseeko(fs->f, 0, SEEK_END);
  118. for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
  119. if (fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f) != GSM_FRAME_SIZE) {
  120. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  121. }
  122. }
  123. }
  124. return fseeko(fs->f, offset, SEEK_SET);
  125. }
  126. static int gsm_trunc(struct ast_filestream *fs)
  127. {
  128. int fd;
  129. off_t cur;
  130. if ((fd = fileno(fs->f)) < 0) {
  131. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm filestream %p: %s\n", fs, strerror(errno));
  132. return -1;
  133. }
  134. if ((cur = ftello(fs->f)) < 0) {
  135. ast_log(AST_LOG_WARNING, "Unable to determine current position in gsm filestream %p: %s\n", fs, strerror(errno));
  136. return -1;
  137. }
  138. /* Truncate file to current length */
  139. return ftruncate(fd, cur);
  140. }
  141. static off_t gsm_tell(struct ast_filestream *fs)
  142. {
  143. off_t offset = ftello(fs->f);
  144. if (offset < 0) {
  145. ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno));
  146. return 0;
  147. }
  148. return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES;
  149. }
  150. static struct ast_format_def gsm_f = {
  151. .name = "gsm",
  152. .exts = "gsm",
  153. .write = gsm_write,
  154. .seek = gsm_seek,
  155. .trunc = gsm_trunc,
  156. .tell = gsm_tell,
  157. .read = gsm_read,
  158. .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */
  159. };
  160. static int load_module(void)
  161. {
  162. gsm_f.format = ast_format_gsm;
  163. if (ast_format_def_register(&gsm_f))
  164. return AST_MODULE_LOAD_DECLINE;
  165. return AST_MODULE_LOAD_SUCCESS;
  166. }
  167. static int unload_module(void)
  168. {
  169. return ast_format_def_unregister(gsm_f.name);
  170. }
  171. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw GSM data",
  172. .support_level = AST_MODULE_SUPPORT_CORE,
  173. .load = load_module,
  174. .unload = unload_module,
  175. .load_pri = AST_MODPRI_APP_DEPEND
  176. );