format_g729.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 G729 data.
  21. * \note This is not an encoder/decoder. The codec fo g729 is only
  22. * available with a commercial license from Digium, due to patent
  23. * restrictions. Check http://www.digium.com for information.
  24. * \arg Extensions: g729
  25. * \ingroup formats
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/mod_format.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/endian.h"
  35. /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
  36. /* Portions of the conversion code are by guido@sienanet.it */
  37. #define BUF_SIZE 20 /* two G729 frames */
  38. #define G729A_SAMPLES 160
  39. static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
  40. {
  41. int res;
  42. /* Send a frame from the file to the appropriate channel */
  43. s->fr.frametype = AST_FRAME_VOICE;
  44. ast_format_set(&s->fr.subclass.format, AST_FORMAT_G729A, 0);
  45. s->fr.mallocd = 0;
  46. s->fr.samples = G729A_SAMPLES;
  47. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  48. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  49. if (res && (res != 10)) /* XXX what for ? */
  50. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  51. return NULL;
  52. }
  53. *whennext = s->fr.samples;
  54. return &s->fr;
  55. }
  56. static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
  57. {
  58. int res;
  59. if (f->frametype != AST_FRAME_VOICE) {
  60. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  61. return -1;
  62. }
  63. if (f->subclass.format.id != AST_FORMAT_G729A) {
  64. ast_log(LOG_WARNING, "Asked to write non-G729 frame (%s)!\n", ast_getformatname(&f->subclass.format));
  65. return -1;
  66. }
  67. if (f->datalen % 10) {
  68. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
  69. return -1;
  70. }
  71. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  72. ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  78. {
  79. long bytes;
  80. off_t min,cur,max,offset=0;
  81. min = 0;
  82. cur = ftello(fs->f);
  83. fseeko(fs->f, 0, SEEK_END);
  84. max = ftello(fs->f);
  85. bytes = BUF_SIZE * (sample_offset / G729A_SAMPLES);
  86. if (whence == SEEK_SET)
  87. offset = bytes;
  88. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  89. offset = cur + bytes;
  90. else if (whence == SEEK_END)
  91. offset = max - bytes;
  92. if (whence != SEEK_FORCECUR) {
  93. offset = (offset > max)?max:offset;
  94. }
  95. /* protect against seeking beyond begining. */
  96. offset = (offset < min)?min:offset;
  97. if (fseeko(fs->f, offset, SEEK_SET) < 0)
  98. return -1;
  99. return 0;
  100. }
  101. static int g729_trunc(struct ast_filestream *fs)
  102. {
  103. int fd;
  104. off_t cur;
  105. if ((fd = fileno(fs->f)) < 0) {
  106. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g729 filestream %p: %s\n", fs, strerror(errno));
  107. return -1;
  108. }
  109. if ((cur = ftello(fs->f)) < 0) {
  110. ast_log(AST_LOG_WARNING, "Unable to determine current position in g729 filestream %p: %s\n", fs, strerror(errno));
  111. return -1;
  112. }
  113. /* Truncate file to current length */
  114. return ftruncate(fd, cur);
  115. }
  116. static off_t g729_tell(struct ast_filestream *fs)
  117. {
  118. off_t offset = ftello(fs->f);
  119. return (offset/BUF_SIZE)*G729A_SAMPLES;
  120. }
  121. static struct ast_format_def g729_f = {
  122. .name = "g729",
  123. .exts = "g729",
  124. .write = g729_write,
  125. .seek = g729_seek,
  126. .trunc = g729_trunc,
  127. .tell = g729_tell,
  128. .read = g729_read,
  129. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  130. };
  131. static int load_module(void)
  132. {
  133. ast_format_set(&g729_f.format, AST_FORMAT_G729A, 0);
  134. if (ast_format_def_register(&g729_f))
  135. return AST_MODULE_LOAD_FAILURE;
  136. return AST_MODULE_LOAD_SUCCESS;
  137. }
  138. static int unload_module(void)
  139. {
  140. return ast_format_def_unregister(g729_f.name);
  141. }
  142. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.729 data",
  143. .load = load_module,
  144. .unload = unload_module,
  145. .load_pri = AST_MODPRI_APP_DEPEND
  146. );