format_siren7.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 (Siren7, licensed from Polycom) format, 32kbps bitrate only
  21. * \arg File name extensions: siren7
  22. * \ingroup formats
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include "asterisk/mod_format.h"
  29. #include "asterisk/module.h"
  30. #include "asterisk/endian.h"
  31. #include "asterisk/format_cache.h"
  32. #define BUF_SIZE 80 /* 20 milliseconds == 80 bytes, 320 samples */
  33. #define SAMPLES_TO_BYTES(x) x / (320 / 80)
  34. #define BYTES_TO_SAMPLES(x) x * (320 / 80)
  35. static struct ast_frame *siren7read(struct ast_filestream *s, int *whennext)
  36. {
  37. size_t res;
  38. /* Send a frame from the file to the appropriate channel */
  39. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  40. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  41. if (res) {
  42. ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
  43. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  44. strerror(errno));
  45. }
  46. return NULL;
  47. }
  48. *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
  49. return &s->fr;
  50. }
  51. static int siren7write(struct ast_filestream *fs, struct ast_frame *f)
  52. {
  53. int res;
  54. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  55. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  56. return -1;
  57. }
  58. return 0;
  59. }
  60. static int siren7seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  61. {
  62. off_t offset = 0, min = 0, cur, max;
  63. sample_offset = SAMPLES_TO_BYTES(sample_offset);
  64. if ((cur = ftello(fs->f)) < 0) {
  65. ast_log(AST_LOG_WARNING, "Unable to determine current position in siren7 filestream %p: %s\n", fs, strerror(errno));
  66. return -1;
  67. }
  68. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  69. ast_log(AST_LOG_WARNING, "Unable to seek to end of siren7 filestream %p: %s\n", fs, strerror(errno));
  70. return -1;
  71. }
  72. if ((max = ftello(fs->f)) < 0) {
  73. ast_log(AST_LOG_WARNING, "Unable to determine max position in siren7 filestream %p: %s\n", fs, strerror(errno));
  74. return -1;
  75. }
  76. if (whence == SEEK_SET)
  77. offset = sample_offset;
  78. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  79. offset = sample_offset + cur;
  80. else if (whence == SEEK_END)
  81. offset = max - sample_offset;
  82. if (whence != SEEK_FORCECUR)
  83. offset = (offset > max) ? max : offset;
  84. /* always protect against seeking past begining. */
  85. offset = (offset < min) ? min : offset;
  86. return fseeko(fs->f, offset, SEEK_SET);
  87. }
  88. static int siren7trunc(struct ast_filestream *fs)
  89. {
  90. int fd;
  91. off_t cur;
  92. if ((fd = fileno(fs->f)) < 0) {
  93. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for siren7 filestream %p: %s\n", fs, strerror(errno));
  94. return -1;
  95. }
  96. if ((cur = ftello(fs->f)) < 0) {
  97. ast_log(AST_LOG_WARNING, "Unable to determine current position in siren7 filestream %p: %s\n", fs, strerror(errno));
  98. return -1;
  99. }
  100. /* Truncate file to current length */
  101. return ftruncate(fd, cur);
  102. }
  103. static off_t siren7tell(struct ast_filestream *fs)
  104. {
  105. return BYTES_TO_SAMPLES(ftello(fs->f));
  106. }
  107. static struct ast_format_def siren7_f = {
  108. .name = "siren7",
  109. .exts = "siren7",
  110. .write = siren7write,
  111. .seek = siren7seek,
  112. .trunc = siren7trunc,
  113. .tell = siren7tell,
  114. .read = siren7read,
  115. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  116. };
  117. static int load_module(void)
  118. {
  119. siren7_f.format = ast_format_siren7;
  120. if (ast_format_def_register(&siren7_f))
  121. return AST_MODULE_LOAD_DECLINE;
  122. return AST_MODULE_LOAD_SUCCESS;
  123. }
  124. static int unload_module(void)
  125. {
  126. return ast_format_def_unregister(siren7_f.name);
  127. }
  128. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ITU G.722.1 (Siren7, licensed from Polycom)",
  129. .support_level = AST_MODULE_SUPPORT_CORE,
  130. .load = load_module,
  131. .unload = unload_module,
  132. .load_pri = AST_MODPRI_APP_DEPEND
  133. );