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. /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
  35. /* Portions of the conversion code are by guido@sienanet.it */
  36. #define ILBC_BUF_SIZE 50 /* One Real iLBC Frame */
  37. #define ILBC_SAMPLES 240
  38. static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
  39. {
  40. int res;
  41. /* Send a frame from the file to the appropriate channel */
  42. s->fr.frametype = AST_FRAME_VOICE;
  43. s->fr.subclass.codec = AST_FORMAT_ILBC;
  44. s->fr.mallocd = 0;
  45. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE);
  46. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  47. if (res)
  48. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  49. return NULL;
  50. }
  51. *whennext = s->fr.samples = ILBC_SAMPLES;
  52. return &s->fr;
  53. }
  54. static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
  55. {
  56. int res;
  57. if (f->frametype != AST_FRAME_VOICE) {
  58. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  59. return -1;
  60. }
  61. if (f->subclass.codec != AST_FORMAT_ILBC) {
  62. ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%s)!\n", ast_getformatname(f->subclass.codec));
  63. return -1;
  64. }
  65. if (f->datalen % 50) {
  66. ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
  67. return -1;
  68. }
  69. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  70. ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno));
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. static int ilbc_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  76. {
  77. long bytes;
  78. off_t min,cur,max,offset=0;
  79. min = 0;
  80. cur = ftello(fs->f);
  81. fseeko(fs->f, 0, SEEK_END);
  82. max = ftello(fs->f);
  83. bytes = ILBC_BUF_SIZE * (sample_offset / ILBC_SAMPLES);
  84. if (whence == SEEK_SET)
  85. offset = bytes;
  86. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  87. offset = cur + bytes;
  88. else if (whence == SEEK_END)
  89. offset = max - bytes;
  90. if (whence != SEEK_FORCECUR) {
  91. offset = (offset > max)?max:offset;
  92. }
  93. /* protect against seeking beyond begining. */
  94. offset = (offset < min)?min:offset;
  95. if (fseeko(fs->f, offset, SEEK_SET) < 0)
  96. return -1;
  97. return 0;
  98. }
  99. static int ilbc_trunc(struct ast_filestream *fs)
  100. {
  101. /* Truncate file to current length */
  102. if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0)
  103. return -1;
  104. return 0;
  105. }
  106. static off_t ilbc_tell(struct ast_filestream *fs)
  107. {
  108. off_t offset = ftello(fs->f);
  109. return (offset/ILBC_BUF_SIZE)*ILBC_SAMPLES;
  110. }
  111. static const struct ast_format ilbc_f = {
  112. .name = "iLBC",
  113. .exts = "ilbc",
  114. .format = AST_FORMAT_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. if (ast_format_register(&ilbc_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_unregister(ilbc_f.name);
  131. }
  132. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw iLBC data",
  133. .load = load_module,
  134. .unload = unload_module,
  135. .load_pri = AST_MODPRI_APP_DEPEND
  136. );