format_g723.c 4.0 KB

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