format_ilbc.c 3.9 KB

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