format_g723.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. int 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)) != size) {
  63. ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
  64. return NULL;
  65. }
  66. *whennext = s->fr.samples = 240;
  67. return &s->fr;
  68. }
  69. static int g723_write(struct ast_filestream *s, struct ast_frame *f)
  70. {
  71. uint32_t delay;
  72. uint16_t size;
  73. int res;
  74. /* XXX there used to be a check s->fr means a read stream */
  75. delay = 0;
  76. if (f->datalen <= 0) {
  77. ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
  78. return 0;
  79. }
  80. if ((res = fwrite(&delay, 1, 4, s->f)) != 4) {
  81. ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
  82. return -1;
  83. }
  84. size = htons(f->datalen);
  85. if ((res = fwrite(&size, 1, 2, s->f)) != 2) {
  86. ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
  87. return -1;
  88. }
  89. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  90. ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  96. {
  97. return -1;
  98. }
  99. static int g723_trunc(struct ast_filestream *fs)
  100. {
  101. int fd;
  102. off_t cur;
  103. if ((fd = fileno(fs->f)) < 0) {
  104. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g723 filestream %p: %s\n", fs, strerror(errno));
  105. return -1;
  106. }
  107. if ((cur = ftello(fs->f)) < 0) {
  108. ast_log(AST_LOG_WARNING, "Unable to determine current position in g723 filestream %p: %s\n", fs, strerror(errno));
  109. return -1;
  110. }
  111. /* Truncate file to current length */
  112. return ftruncate(fd, cur);
  113. }
  114. static off_t g723_tell(struct ast_filestream *fs)
  115. {
  116. return -1;
  117. }
  118. static struct ast_format_def g723_1_f = {
  119. .name = "g723sf",
  120. .exts = "g723|g723sf",
  121. .write = g723_write,
  122. .seek = g723_seek,
  123. .trunc = g723_trunc,
  124. .tell = g723_tell,
  125. .read = g723_read,
  126. .buf_size = G723_MAX_SIZE + AST_FRIENDLY_OFFSET,
  127. };
  128. static int load_module(void)
  129. {
  130. g723_1_f.format = ast_format_g723;
  131. if (ast_format_def_register(&g723_1_f))
  132. return AST_MODULE_LOAD_FAILURE;
  133. return AST_MODULE_LOAD_SUCCESS;
  134. }
  135. static int unload_module(void)
  136. {
  137. return ast_format_def_unregister(g723_1_f.name);
  138. }
  139. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "G.723.1 Simple Timestamp File Format",
  140. .support_level = AST_MODULE_SUPPORT_CORE,
  141. .load = load_module,
  142. .unload = unload_module,
  143. .load_pri = AST_MODPRI_APP_DEPEND
  144. );