format_g723.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #define G723_MAX_SIZE 1024
  34. static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
  35. {
  36. unsigned short size;
  37. int res;
  38. int delay;
  39. /* Read the delay for the next packet, and schedule again if necessary */
  40. /* XXX is this ignored ? */
  41. if (fread(&delay, 1, 4, s->f) == 4)
  42. delay = ntohl(delay);
  43. else
  44. delay = -1;
  45. if (fread(&size, 1, 2, s->f) != 2) {
  46. /* Out of data, or the file is no longer valid. In any case
  47. go ahead and stop the stream */
  48. return NULL;
  49. }
  50. /* Looks like we have a frame to read from here */
  51. size = ntohs(size);
  52. if (size > G723_MAX_SIZE) {
  53. ast_log(LOG_WARNING, "Size %d is invalid\n", size);
  54. /* The file is apparently no longer any good, as we
  55. shouldn't ever get frames even close to this
  56. size. */
  57. return NULL;
  58. }
  59. /* Read the data into the buffer */
  60. s->fr.frametype = AST_FRAME_VOICE;
  61. ast_format_set(&s->fr.subclass.format, AST_FORMAT_G723_1, 0);
  62. s->fr.mallocd = 0;
  63. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size);
  64. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != size) {
  65. ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
  66. return NULL;
  67. }
  68. *whennext = s->fr.samples = 240;
  69. return &s->fr;
  70. }
  71. static int g723_write(struct ast_filestream *s, struct ast_frame *f)
  72. {
  73. uint32_t delay;
  74. uint16_t size;
  75. int res;
  76. /* XXX there used to be a check s->fr means a read stream */
  77. if (f->frametype != AST_FRAME_VOICE) {
  78. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  79. return -1;
  80. }
  81. if (f->subclass.format.id != AST_FORMAT_G723_1) {
  82. ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
  83. return -1;
  84. }
  85. delay = 0;
  86. if (f->datalen <= 0) {
  87. ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
  88. return 0;
  89. }
  90. if ((res = fwrite(&delay, 1, 4, s->f)) != 4) {
  91. ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
  92. return -1;
  93. }
  94. size = htons(f->datalen);
  95. if ((res = fwrite(&size, 1, 2, s->f)) != 2) {
  96. ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
  97. return -1;
  98. }
  99. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  100. ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  106. {
  107. return -1;
  108. }
  109. static int g723_trunc(struct ast_filestream *fs)
  110. {
  111. int fd;
  112. off_t cur;
  113. if ((fd = fileno(fs->f)) < 0) {
  114. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g723 filestream %p: %s\n", fs, strerror(errno));
  115. return -1;
  116. }
  117. if ((cur = ftello(fs->f)) < 0) {
  118. ast_log(AST_LOG_WARNING, "Unable to determine current position in g723 filestream %p: %s\n", fs, strerror(errno));
  119. return -1;
  120. }
  121. /* Truncate file to current length */
  122. return ftruncate(fd, cur);
  123. }
  124. static off_t g723_tell(struct ast_filestream *fs)
  125. {
  126. return -1;
  127. }
  128. static struct ast_format_def g723_1_f = {
  129. .name = "g723sf",
  130. .exts = "g723|g723sf",
  131. .write = g723_write,
  132. .seek = g723_seek,
  133. .trunc = g723_trunc,
  134. .tell = g723_tell,
  135. .read = g723_read,
  136. .buf_size = G723_MAX_SIZE + AST_FRIENDLY_OFFSET,
  137. };
  138. static int load_module(void)
  139. {
  140. ast_format_set(&g723_1_f.format, AST_FORMAT_G723_1, 0);
  141. if (ast_format_def_register(&g723_1_f))
  142. return AST_MODULE_LOAD_FAILURE;
  143. return AST_MODULE_LOAD_SUCCESS;
  144. }
  145. static int unload_module(void)
  146. {
  147. return ast_format_def_unregister(g723_1_f.name);
  148. }
  149. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "G.723.1 Simple Timestamp File Format",
  150. .load = load_module,
  151. .unload = unload_module,
  152. .load_pri = AST_MODPRI_APP_DEPEND
  153. );