format_ilbc.c 3.8 KB

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