format_g719.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010, 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.719 , 64kbps bitrate only
  21. * \arg File name extensions: g719
  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 160 /* 20 milliseconds == 160 bytes, 960 samples */
  34. #define SAMPLES_TO_BYTES(x) ((typeof(x)) x / ((float) 960 / 160))
  35. #define BYTES_TO_SAMPLES(x) ((typeof(x)) x * ((float) 960 / 160))
  36. static struct ast_frame *g719read(struct ast_filestream *s, int *whennext)
  37. {
  38. int 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 (%d) (%s)!\n", res, strerror(errno));
  44. return NULL;
  45. }
  46. *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
  47. return &s->fr;
  48. }
  49. static int g719write(struct ast_filestream *fs, struct ast_frame *f)
  50. {
  51. int res;
  52. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
  53. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  54. return -1;
  55. }
  56. return 0;
  57. }
  58. static int g719seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  59. {
  60. off_t offset = 0, min = 0, cur, max;
  61. sample_offset = SAMPLES_TO_BYTES(sample_offset);
  62. if ((cur = ftello(fs->f)) < 0) {
  63. ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
  64. return -1;
  65. }
  66. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  67. ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
  68. return -1;
  69. }
  70. if ((max = ftello(fs->f)) < 0) {
  71. ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
  72. return -1;
  73. }
  74. if (whence == SEEK_SET)
  75. offset = sample_offset;
  76. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  77. offset = sample_offset + cur;
  78. else if (whence == SEEK_END)
  79. offset = max - sample_offset;
  80. if (whence != SEEK_FORCECUR)
  81. offset = (offset > max) ? max : offset;
  82. /* always protect against seeking past begining. */
  83. offset = (offset < min) ? min : offset;
  84. return fseeko(fs->f, offset, SEEK_SET);
  85. }
  86. static int g719trunc(struct ast_filestream *fs)
  87. {
  88. int fd;
  89. off_t cur;
  90. if ((fd = fileno(fs->f)) < 0) {
  91. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g719 filestream %p: %s\n", fs, strerror(errno));
  92. return -1;
  93. }
  94. if ((cur = ftello(fs->f)) < 0) {
  95. ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
  96. return -1;
  97. }
  98. return ftruncate(fd, cur);
  99. }
  100. static off_t g719tell(struct ast_filestream *fs)
  101. {
  102. return BYTES_TO_SAMPLES(ftello(fs->f));
  103. }
  104. static struct ast_format_def g719_f = {
  105. .name = "g719",
  106. .exts = "g719",
  107. .write = g719write,
  108. .seek = g719seek,
  109. .trunc = g719trunc,
  110. .tell = g719tell,
  111. .read = g719read,
  112. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  113. };
  114. static int load_module(void)
  115. {
  116. g719_f.format = ast_format_g719;
  117. if (ast_format_def_register(&g719_f))
  118. return AST_MODULE_LOAD_DECLINE;
  119. return AST_MODULE_LOAD_SUCCESS;
  120. }
  121. static int unload_module(void)
  122. {
  123. return ast_format_def_unregister(g719_f.name);
  124. }
  125. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ITU G.719",
  126. .support_level = AST_MODULE_SUPPORT_CORE,
  127. .load = load_module,
  128. .unload = unload_module,
  129. .load_pri = AST_MODPRI_APP_DEPEND
  130. );