format_gsm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. s->fr.subclass.codec = AST_FORMAT_GSM;
  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.codec != AST_FORMAT_GSM) {
  68. ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(f->subclass.codec));
  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,cur,max,distance;
  97. min = 0;
  98. cur = ftello(fs->f);
  99. fseeko(fs->f, 0, SEEK_END);
  100. max = ftello(fs->f);
  101. /* have to fudge to frame here, so not fully to sample */
  102. distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE;
  103. if(whence == SEEK_SET)
  104. offset = distance;
  105. else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
  106. offset = distance + cur;
  107. else if(whence == SEEK_END)
  108. offset = max - distance;
  109. /* Always protect against seeking past the begining. */
  110. offset = (offset < min)?min:offset;
  111. if (whence != SEEK_FORCECUR) {
  112. offset = (offset > max)?max:offset;
  113. } else if (offset > max) {
  114. int i;
  115. fseeko(fs->f, 0, SEEK_END);
  116. for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
  117. if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
  118. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  119. }
  120. }
  121. }
  122. return fseeko(fs->f, offset, SEEK_SET);
  123. }
  124. static int gsm_trunc(struct ast_filestream *fs)
  125. {
  126. return ftruncate(fileno(fs->f), ftello(fs->f));
  127. }
  128. static off_t gsm_tell(struct ast_filestream *fs)
  129. {
  130. off_t offset = ftello(fs->f);
  131. return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES;
  132. }
  133. static const struct ast_format gsm_f = {
  134. .name = "gsm",
  135. .exts = "gsm",
  136. .format = AST_FORMAT_GSM,
  137. .write = gsm_write,
  138. .seek = gsm_seek,
  139. .trunc = gsm_trunc,
  140. .tell = gsm_tell,
  141. .read = gsm_read,
  142. .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */
  143. };
  144. static int load_module(void)
  145. {
  146. if (ast_format_register(&gsm_f))
  147. return AST_MODULE_LOAD_FAILURE;
  148. return AST_MODULE_LOAD_SUCCESS;
  149. }
  150. static int unload_module(void)
  151. {
  152. return ast_format_unregister(gsm_f.name);
  153. }
  154. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw GSM data",
  155. .load = load_module,
  156. .unload = unload_module,
  157. .load_pri = AST_MODPRI_APP_DEPEND
  158. );