format_g723.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@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. /*!
  19. * \file
  20. *
  21. * \brief Old-style G.723.1 frame/timestamp format.
  22. *
  23. * \arg Extensions: g723, g723sf
  24. * \ingroup formats
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/mod_format.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/format_cache.h"
  34. #define G723_MAX_SIZE 1024
  35. static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
  36. {
  37. unsigned short size;
  38. size_t res;
  39. int delay;
  40. /* Read the delay for the next packet, and schedule again if necessary */
  41. /* XXX is this ignored ? */
  42. if (fread(&delay, 1, 4, s->f) == 4)
  43. delay = ntohl(delay);
  44. else
  45. delay = -1;
  46. if (fread(&size, 1, 2, s->f) != 2) {
  47. /* Out of data, or the file is no longer valid. In any case
  48. go ahead and stop the stream */
  49. return NULL;
  50. }
  51. /* Looks like we have a frame to read from here */
  52. size = ntohs(size);
  53. if (size > G723_MAX_SIZE) {
  54. ast_log(LOG_WARNING, "Size %d is invalid\n", size);
  55. /* The file is apparently no longer any good, as we
  56. shouldn't ever get frames even close to this
  57. size. */
  58. return NULL;
  59. }
  60. /* Read the data into the buffer */
  61. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size);
  62. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
  63. if (res) {
  64. ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
  65. ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
  66. strerror(errno));
  67. }
  68. return NULL;
  69. }
  70. *whennext = s->fr.samples = 240;
  71. return &s->fr;
  72. }
  73. static int g723_write(struct ast_filestream *s, struct ast_frame *f)
  74. {
  75. uint32_t delay;
  76. uint16_t size;
  77. int res;
  78. /* XXX there used to be a check s->fr means a read stream */
  79. delay = 0;
  80. if (f->datalen <= 0) {
  81. ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
  82. return 0;
  83. }
  84. if ((res = fwrite(&delay, 1, 4, s->f)) != 4) {
  85. ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
  86. return -1;
  87. }
  88. size = htons(f->datalen);
  89. if ((res = fwrite(&size, 1, 2, s->f)) != 2) {
  90. ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
  91. return -1;
  92. }
  93. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  94. ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  100. {
  101. return -1;
  102. }
  103. static int g723_trunc(struct ast_filestream *fs)
  104. {
  105. int fd;
  106. off_t cur;
  107. if ((fd = fileno(fs->f)) < 0) {
  108. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g723 filestream %p: %s\n", fs, strerror(errno));
  109. return -1;
  110. }
  111. if ((cur = ftello(fs->f)) < 0) {
  112. ast_log(AST_LOG_WARNING, "Unable to determine current position in g723 filestream %p: %s\n", fs, strerror(errno));
  113. return -1;
  114. }
  115. /* Truncate file to current length */
  116. return ftruncate(fd, cur);
  117. }
  118. static off_t g723_tell(struct ast_filestream *fs)
  119. {
  120. return -1;
  121. }
  122. static struct ast_format_def g723_1_f = {
  123. .name = "g723sf",
  124. .exts = "g723|g723sf",
  125. .write = g723_write,
  126. .seek = g723_seek,
  127. .trunc = g723_trunc,
  128. .tell = g723_tell,
  129. .read = g723_read,
  130. .buf_size = G723_MAX_SIZE + AST_FRIENDLY_OFFSET,
  131. };
  132. static int load_module(void)
  133. {
  134. g723_1_f.format = ast_format_g723;
  135. if (ast_format_def_register(&g723_1_f))
  136. return AST_MODULE_LOAD_DECLINE;
  137. return AST_MODULE_LOAD_SUCCESS;
  138. }
  139. static int unload_module(void)
  140. {
  141. return ast_format_def_unregister(g723_1_f.name);
  142. }
  143. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "G.723.1 Simple Timestamp File Format",
  144. .support_level = AST_MODULE_SUPPORT_CORE,
  145. .load = load_module,
  146. .unload = unload_module,
  147. .load_pri = AST_MODPRI_APP_DEPEND
  148. );