format_sln.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Anthony Minessale
  5. * Anthony Minessale (anthmct@yahoo.com)
  6. *
  7. * See http://www.asterisk.org for more information about
  8. * the Asterisk project. Please do not directly contact
  9. * any of the maintainers of this project for assistance;
  10. * the project provides a web site, mailing lists and IRC
  11. * channels for your use.
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License Version 2. See the LICENSE file
  15. * at the top of the source tree.
  16. */
  17. /*! \file
  18. *
  19. * \brief RAW SLINEAR Format
  20. * \arg File name extensions: sln, raw
  21. * \ingroup formats
  22. */
  23. #include "asterisk.h"
  24. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  25. #include "asterisk/mod_format.h"
  26. #include "asterisk/module.h"
  27. #include "asterisk/endian.h"
  28. #define BUF_SIZE 320 /* 320 bytes, 160 samples */
  29. #define SLIN_SAMPLES 160
  30. static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext)
  31. {
  32. int res;
  33. /* Send a frame from the file to the appropriate channel */
  34. s->fr.frametype = AST_FRAME_VOICE;
  35. s->fr.subclass = AST_FORMAT_SLINEAR;
  36. s->fr.mallocd = 0;
  37. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  38. if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) {
  39. if (res)
  40. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  41. return NULL;
  42. }
  43. *whennext = s->fr.samples = res/2;
  44. s->fr.datalen = res;
  45. return &s->fr;
  46. }
  47. static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
  48. {
  49. int res;
  50. if (f->frametype != AST_FRAME_VOICE) {
  51. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  52. return -1;
  53. }
  54. if (f->subclass != AST_FORMAT_SLINEAR) {
  55. ast_log(LOG_WARNING, "Asked to write non-slinear frame (%d)!\n", f->subclass);
  56. return -1;
  57. }
  58. if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
  59. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  60. return -1;
  61. }
  62. return 0;
  63. }
  64. static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  65. {
  66. off_t offset=0,min,cur,max;
  67. min = 0;
  68. sample_offset <<= 1;
  69. cur = ftello(fs->f);
  70. fseeko(fs->f, 0, SEEK_END);
  71. max = ftello(fs->f);
  72. if (whence == SEEK_SET)
  73. offset = sample_offset;
  74. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  75. offset = sample_offset + cur;
  76. else if (whence == SEEK_END)
  77. offset = max - sample_offset;
  78. if (whence != SEEK_FORCECUR) {
  79. offset = (offset > max)?max:offset;
  80. }
  81. /* always protect against seeking past begining. */
  82. offset = (offset < min)?min:offset;
  83. return fseeko(fs->f, offset, SEEK_SET);
  84. }
  85. static int slinear_trunc(struct ast_filestream *fs)
  86. {
  87. return ftruncate(fileno(fs->f), ftello(fs->f));
  88. }
  89. static off_t slinear_tell(struct ast_filestream *fs)
  90. {
  91. return ftello(fs->f) / 2;
  92. }
  93. static const struct ast_format slin_f = {
  94. .name = "sln",
  95. .exts = "sln|raw",
  96. .format = AST_FORMAT_SLINEAR,
  97. .write = slinear_write,
  98. .seek = slinear_seek,
  99. .trunc = slinear_trunc,
  100. .tell = slinear_tell,
  101. .read = slinear_read,
  102. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  103. };
  104. static int load_module(void)
  105. {
  106. if (ast_format_register(&slin_f))
  107. return AST_MODULE_LOAD_FAILURE;
  108. return AST_MODULE_LOAD_SUCCESS;
  109. }
  110. static int unload_module(void)
  111. {
  112. return ast_format_unregister(slin_f.name);
  113. }
  114. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_FIRST, "Raw Signed Linear Audio support (SLN)",
  115. .load = load_module,
  116. .unload = unload_module,
  117. );