format_g729.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 for 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. #include "asterisk/format_cache.h"
  36. /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
  37. /* Portions of the conversion code are by guido@sienanet.it */
  38. #define BUF_SIZE 20 /* two G729 frames */
  39. #define G729A_SAMPLES 160
  40. static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
  41. {
  42. size_t res;
  43. /* Send a frame from the file to the appropriate channel */
  44. s->fr.samples = G729A_SAMPLES;
  45. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  46. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  47. if (res && res != 10) /* XXX what for ? */ {
  48. ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
  49. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  50. strerror(errno));
  51. }
  52. return NULL;
  53. }
  54. *whennext = s->fr.samples;
  55. return &s->fr;
  56. }
  57. static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
  58. {
  59. int res;
  60. if (f->datalen % 10) {
  61. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
  62. return -1;
  63. }
  64. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  65. ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
  66. return -1;
  67. }
  68. return 0;
  69. }
  70. static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  71. {
  72. long bytes;
  73. off_t min,cur,max,offset=0;
  74. min = 0;
  75. cur = ftello(fs->f);
  76. fseeko(fs->f, 0, SEEK_END);
  77. max = ftello(fs->f);
  78. bytes = BUF_SIZE * (sample_offset / G729A_SAMPLES);
  79. if (whence == SEEK_SET)
  80. offset = bytes;
  81. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  82. offset = cur + bytes;
  83. else if (whence == SEEK_END)
  84. offset = max - bytes;
  85. if (whence != SEEK_FORCECUR) {
  86. offset = (offset > max)?max:offset;
  87. }
  88. /* protect against seeking beyond begining. */
  89. offset = (offset < min)?min:offset;
  90. if (fseeko(fs->f, offset, SEEK_SET) < 0)
  91. return -1;
  92. return 0;
  93. }
  94. static int g729_trunc(struct ast_filestream *fs)
  95. {
  96. int fd;
  97. off_t cur;
  98. if ((fd = fileno(fs->f)) < 0) {
  99. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g729 filestream %p: %s\n", fs, strerror(errno));
  100. return -1;
  101. }
  102. if ((cur = ftello(fs->f)) < 0) {
  103. ast_log(AST_LOG_WARNING, "Unable to determine current position in g729 filestream %p: %s\n", fs, strerror(errno));
  104. return -1;
  105. }
  106. /* Truncate file to current length */
  107. return ftruncate(fd, cur);
  108. }
  109. static off_t g729_tell(struct ast_filestream *fs)
  110. {
  111. off_t offset = ftello(fs->f);
  112. return (offset/BUF_SIZE)*G729A_SAMPLES;
  113. }
  114. static struct ast_format_def g729_f = {
  115. .name = "g729",
  116. .exts = "g729",
  117. .write = g729_write,
  118. .seek = g729_seek,
  119. .trunc = g729_trunc,
  120. .tell = g729_tell,
  121. .read = g729_read,
  122. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  123. };
  124. static int load_module(void)
  125. {
  126. g729_f.format = ast_format_g729;
  127. if (ast_format_def_register(&g729_f))
  128. return AST_MODULE_LOAD_DECLINE;
  129. return AST_MODULE_LOAD_SUCCESS;
  130. }
  131. static int unload_module(void)
  132. {
  133. return ast_format_def_unregister(g729_f.name);
  134. }
  135. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.729 data",
  136. .support_level = AST_MODULE_SUPPORT_CORE,
  137. .load = load_module,
  138. .unload = unload_module,
  139. .load_pri = AST_MODPRI_APP_DEPEND
  140. );