format_siren14.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #define BUF_SIZE 120 /* 20 milliseconds == 120 bytes, 640 samples */
  33. #define SAMPLES_TO_BYTES(x) ((typeof(x)) x / ((float) 640 / 120))
  34. #define BYTES_TO_SAMPLES(x) ((typeof(x)) x * ((float) 640 / 120))
  35. static struct ast_frame *siren14read(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. ast_format_set(&s->fr.subclass.format, AST_FORMAT_SIREN14, 0);
  41. s->fr.mallocd = 0;
  42. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, 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 = BYTES_TO_SAMPLES(res);
  49. return &s->fr;
  50. }
  51. static int siren14write(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.format.id != AST_FORMAT_SIREN14) {
  59. ast_log(LOG_WARNING, "Asked to write non-Siren14 frame (%s)!\n", ast_getformatname(&f->subclass.format));
  60. return -1;
  61. }
  62. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  63. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  64. return -1;
  65. }
  66. return 0;
  67. }
  68. static int siren14seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  69. {
  70. off_t offset = 0, min = 0, cur, max;
  71. sample_offset = SAMPLES_TO_BYTES(sample_offset);
  72. cur = ftello(fs->f);
  73. fseeko(fs->f, 0, SEEK_END);
  74. max = ftello(fs->f);
  75. if (whence == SEEK_SET)
  76. offset = sample_offset;
  77. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  78. offset = sample_offset + cur;
  79. else if (whence == SEEK_END)
  80. offset = max - sample_offset;
  81. if (whence != SEEK_FORCECUR)
  82. offset = (offset > max) ? max : offset;
  83. /* always protect against seeking past begining. */
  84. offset = (offset < min) ? min : offset;
  85. return fseeko(fs->f, offset, SEEK_SET);
  86. }
  87. static int siren14trunc(struct ast_filestream *fs)
  88. {
  89. return ftruncate(fileno(fs->f), ftello(fs->f));
  90. }
  91. static off_t siren14tell(struct ast_filestream *fs)
  92. {
  93. return BYTES_TO_SAMPLES(ftello(fs->f));
  94. }
  95. static struct ast_format_def siren14_f = {
  96. .name = "siren14",
  97. .exts = "siren14",
  98. .write = siren14write,
  99. .seek = siren14seek,
  100. .trunc = siren14trunc,
  101. .tell = siren14tell,
  102. .read = siren14read,
  103. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  104. };
  105. static int load_module(void)
  106. {
  107. ast_format_set(&siren14_f.format, AST_FORMAT_SIREN14, 0);
  108. if (ast_format_def_register(&siren14_f))
  109. return AST_MODULE_LOAD_DECLINE;
  110. return AST_MODULE_LOAD_SUCCESS;
  111. }
  112. static int unload_module(void)
  113. {
  114. return ast_format_def_unregister(siren14_f.name);
  115. }
  116. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ITU G.722.1 Annex C (Siren14, licensed from Polycom)",
  117. .load = load_module,
  118. .unload = unload_module,
  119. .load_pri = AST_MODPRI_APP_DEPEND
  120. );