format_gsm.c 4.6 KB

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