format_siren14.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Anthony Minessale and Digium, Inc.
  5. * Anthony Minessale (anthmct@yahoo.com)
  6. * Kevin P. Fleming <kpfleming@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 ITU G.722.1 Annex C (Siren14, licensed from Polycom) format, 48kbps bitrate only
  21. * \arg File name extensions: siren14
  22. * \ingroup formats
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/mod_format.h"
  30. #include "asterisk/module.h"
  31. #include "asterisk/endian.h"
  32. #include "asterisk/format_cache.h"
  33. #define BUF_SIZE 120 /* 20 milliseconds == 120 bytes, 640 samples */
  34. #define SAMPLES_TO_BYTES(x) ((typeof(x)) x / ((float) 640 / 120))
  35. #define BYTES_TO_SAMPLES(x) ((typeof(x)) x * ((float) 640 / 120))
  36. static struct ast_frame *siren14read(struct ast_filestream *s, int *whennext)
  37. {
  38. size_t res;
  39. /* Send a frame from the file to the appropriate channel */
  40. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  41. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  42. if (res) {
  43. ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
  44. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  45. strerror(errno));
  46. }
  47. return NULL;
  48. }
  49. *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
  50. return &s->fr;
  51. }
  52. static int siren14write(struct ast_filestream *fs, struct ast_frame *f)
  53. {
  54. int res;
  55. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  56. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. static int siren14seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  62. {
  63. off_t offset = 0, min = 0, cur, max;
  64. sample_offset = SAMPLES_TO_BYTES(sample_offset);
  65. if ((cur = ftello(fs->f)) < 0) {
  66. ast_log(AST_LOG_WARNING, "Unable to determine current position in siren14 filestream %p: %s\n", fs, strerror(errno));
  67. return -1;
  68. }
  69. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  70. ast_log(AST_LOG_WARNING, "Unable to seek to end of siren14 filestream %p: %s\n", fs, strerror(errno));
  71. return -1;
  72. }
  73. if ((max = ftello(fs->f)) < 0) {
  74. ast_log(AST_LOG_WARNING, "Unable to determine max position in siren14 filestream %p: %s\n", fs, strerror(errno));
  75. return -1;
  76. }
  77. if (whence == SEEK_SET)
  78. offset = sample_offset;
  79. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  80. offset = sample_offset + cur;
  81. else if (whence == SEEK_END)
  82. offset = max - sample_offset;
  83. if (whence != SEEK_FORCECUR)
  84. offset = (offset > max) ? max : offset;
  85. /* always protect against seeking past begining. */
  86. offset = (offset < min) ? min : offset;
  87. return fseeko(fs->f, offset, SEEK_SET);
  88. }
  89. static int siren14trunc(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 siren14 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 siren14 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 siren14tell(struct ast_filestream *fs)
  105. {
  106. return BYTES_TO_SAMPLES(ftello(fs->f));
  107. }
  108. static struct ast_format_def siren14_f = {
  109. .name = "siren14",
  110. .exts = "siren14",
  111. .write = siren14write,
  112. .seek = siren14seek,
  113. .trunc = siren14trunc,
  114. .tell = siren14tell,
  115. .read = siren14read,
  116. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  117. };
  118. static int load_module(void)
  119. {
  120. siren14_f.format = ast_format_siren14;
  121. if (ast_format_def_register(&siren14_f))
  122. return AST_MODULE_LOAD_DECLINE;
  123. return AST_MODULE_LOAD_SUCCESS;
  124. }
  125. static int unload_module(void)
  126. {
  127. return ast_format_def_unregister(siren14_f.name);
  128. }
  129. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ITU G.722.1 Annex C (Siren14, licensed from Polycom)",
  130. .support_level = AST_MODULE_SUPPORT_CORE,
  131. .load = load_module,
  132. .unload = unload_module,
  133. .load_pri = AST_MODPRI_APP_DEPEND
  134. );