format_g729.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #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. int 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 (%d) (%s)!\n", res, strerror(errno));
  49. return NULL;
  50. }
  51. *whennext = s->fr.samples;
  52. return &s->fr;
  53. }
  54. static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
  55. {
  56. int res;
  57. if (f->datalen % 10) {
  58. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
  59. return -1;
  60. }
  61. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  62. ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
  63. return -1;
  64. }
  65. return 0;
  66. }
  67. static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  68. {
  69. long bytes;
  70. off_t min,cur,max,offset=0;
  71. min = 0;
  72. cur = ftello(fs->f);
  73. fseeko(fs->f, 0, SEEK_END);
  74. max = ftello(fs->f);
  75. bytes = BUF_SIZE * (sample_offset / G729A_SAMPLES);
  76. if (whence == SEEK_SET)
  77. offset = bytes;
  78. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  79. offset = cur + bytes;
  80. else if (whence == SEEK_END)
  81. offset = max - bytes;
  82. if (whence != SEEK_FORCECUR) {
  83. offset = (offset > max)?max:offset;
  84. }
  85. /* protect against seeking beyond begining. */
  86. offset = (offset < min)?min:offset;
  87. if (fseeko(fs->f, offset, SEEK_SET) < 0)
  88. return -1;
  89. return 0;
  90. }
  91. static int g729_trunc(struct ast_filestream *fs)
  92. {
  93. int fd;
  94. off_t cur;
  95. if ((fd = fileno(fs->f)) < 0) {
  96. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g729 filestream %p: %s\n", fs, strerror(errno));
  97. return -1;
  98. }
  99. if ((cur = ftello(fs->f)) < 0) {
  100. ast_log(AST_LOG_WARNING, "Unable to determine current position in g729 filestream %p: %s\n", fs, strerror(errno));
  101. return -1;
  102. }
  103. /* Truncate file to current length */
  104. return ftruncate(fd, cur);
  105. }
  106. static off_t g729_tell(struct ast_filestream *fs)
  107. {
  108. off_t offset = ftello(fs->f);
  109. return (offset/BUF_SIZE)*G729A_SAMPLES;
  110. }
  111. static struct ast_format_def g729_f = {
  112. .name = "g729",
  113. .exts = "g729",
  114. .write = g729_write,
  115. .seek = g729_seek,
  116. .trunc = g729_trunc,
  117. .tell = g729_tell,
  118. .read = g729_read,
  119. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  120. };
  121. static int load_module(void)
  122. {
  123. g729_f.format = ast_format_g729;
  124. if (ast_format_def_register(&g729_f))
  125. return AST_MODULE_LOAD_FAILURE;
  126. return AST_MODULE_LOAD_SUCCESS;
  127. }
  128. static int unload_module(void)
  129. {
  130. return ast_format_def_unregister(g729_f.name);
  131. }
  132. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.729 data",
  133. .support_level = AST_MODULE_SUPPORT_CORE,
  134. .load = load_module,
  135. .unload = unload_module,
  136. .load_pri = AST_MODPRI_APP_DEPEND
  137. );