format_ilbc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Brian K. West <brian@bkw.org>
  5. *
  6. * Copyright (C) 1999 - 2005, Digium, Inc.
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief Save to raw, headerless iLBC data.
  23. * \arg File name extension: ilbc
  24. * \ingroup formats
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/mod_format.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/endian.h"
  34. #include "asterisk/format_cache.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 ILBC_BUF_SIZE 50 /* One Real iLBC Frame */
  38. #define ILBC_SAMPLES 240
  39. static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
  40. {
  41. size_t res;
  42. /* Send a frame from the file to the appropriate channel */
  43. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE);
  44. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  45. if (res) {
  46. ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
  47. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  48. strerror(errno));
  49. }
  50. return NULL;
  51. }
  52. *whennext = s->fr.samples = ILBC_SAMPLES;
  53. return &s->fr;
  54. }
  55. static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
  56. {
  57. int res;
  58. if (f->datalen % 50) {
  59. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
  60. return -1;
  61. }
  62. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  63. ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno));
  64. return -1;
  65. }
  66. return 0;
  67. }
  68. static int ilbc_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  69. {
  70. long bytes;
  71. off_t min,cur,max,offset=0;
  72. min = 0;
  73. cur = ftello(fs->f);
  74. fseeko(fs->f, 0, SEEK_END);
  75. max = ftello(fs->f);
  76. bytes = ILBC_BUF_SIZE * (sample_offset / ILBC_SAMPLES);
  77. if (whence == SEEK_SET)
  78. offset = bytes;
  79. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  80. offset = cur + bytes;
  81. else if (whence == SEEK_END)
  82. offset = max - bytes;
  83. if (whence != SEEK_FORCECUR) {
  84. offset = (offset > max)?max:offset;
  85. }
  86. /* protect against seeking beyond begining. */
  87. offset = (offset < min)?min:offset;
  88. if (fseeko(fs->f, offset, SEEK_SET) < 0)
  89. return -1;
  90. return 0;
  91. }
  92. static int ilbc_trunc(struct ast_filestream *fs)
  93. {
  94. int fd;
  95. off_t cur;
  96. if ((fd = fileno(fs->f)) < 0) {
  97. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for iLBC filestream %p: %s\n", fs, strerror(errno));
  98. return -1;
  99. }
  100. if ((cur = ftello(fs->f)) < 0) {
  101. ast_log(AST_LOG_WARNING, "Unable to determine current position in iLBC filestream %p: %s\n", fs, strerror(errno));
  102. return -1;
  103. }
  104. /* Truncate file to current length */
  105. return ftruncate(fd, cur);
  106. }
  107. static off_t ilbc_tell(struct ast_filestream *fs)
  108. {
  109. off_t offset = ftello(fs->f);
  110. return (offset/ILBC_BUF_SIZE)*ILBC_SAMPLES;
  111. }
  112. static struct ast_format_def ilbc_f = {
  113. .name = "iLBC",
  114. .exts = "ilbc",
  115. .write = ilbc_write,
  116. .seek = ilbc_seek,
  117. .trunc = ilbc_trunc,
  118. .tell = ilbc_tell,
  119. .read = ilbc_read,
  120. .buf_size = ILBC_BUF_SIZE + AST_FRIENDLY_OFFSET,
  121. };
  122. static int load_module(void)
  123. {
  124. ilbc_f.format = ast_format_ilbc;
  125. if (ast_format_def_register(&ilbc_f))
  126. return AST_MODULE_LOAD_DECLINE;
  127. return AST_MODULE_LOAD_SUCCESS;
  128. }
  129. static int unload_module(void)
  130. {
  131. return ast_format_def_unregister(ilbc_f.name);
  132. }
  133. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw iLBC data",
  134. .support_level = AST_MODULE_SUPPORT_CORE,
  135. .load = load_module,
  136. .unload = unload_module,
  137. .load_pri = AST_MODPRI_APP_DEPEND
  138. );