slinfactory.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005, Anthony Minessale II.
  5. *
  6. * Anthony Minessale <anthmct@yahoo.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 A machine to gather up arbitrary frames and convert them
  21. * to raw slinear on demand.
  22. *
  23. * \author Anthony Minessale <anthmct@yahoo.com>
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/frame.h"
  31. #include "asterisk/slinfactory.h"
  32. #include "asterisk/translate.h"
  33. void ast_slinfactory_init(struct ast_slinfactory *sf)
  34. {
  35. memset(sf, 0, sizeof(*sf));
  36. sf->offset = sf->hold;
  37. sf->output_format = AST_FORMAT_SLINEAR;
  38. }
  39. int ast_slinfactory_init_rate(struct ast_slinfactory *sf, unsigned int sample_rate)
  40. {
  41. memset(sf, 0, sizeof(*sf));
  42. sf->offset = sf->hold;
  43. switch (sample_rate) {
  44. case 8000:
  45. sf->output_format = AST_FORMAT_SLINEAR;
  46. break;
  47. case 16000:
  48. sf->output_format = AST_FORMAT_SLINEAR16;
  49. break;
  50. default:
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. void ast_slinfactory_destroy(struct ast_slinfactory *sf)
  56. {
  57. struct ast_frame *f;
  58. if (sf->trans) {
  59. ast_translator_free_path(sf->trans);
  60. sf->trans = NULL;
  61. }
  62. while ((f = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
  63. ast_frfree(f);
  64. }
  65. int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
  66. {
  67. struct ast_frame *begin_frame = f, *duped_frame = NULL, *frame_ptr;
  68. unsigned int x = 0;
  69. /* In some cases, we can be passed a frame which has no data in it, but
  70. * which has a positive number of samples defined. Once such situation is
  71. * when a jitter buffer is in use and the jitter buffer interpolates a frame.
  72. * The frame it produces has data set to NULL, datalen set to 0, and samples
  73. * set to either 160 or 240.
  74. */
  75. if (!f->data.ptr) {
  76. return 0;
  77. }
  78. if (f->subclass.codec != sf->output_format) {
  79. if (sf->trans && f->subclass.codec != sf->format) {
  80. ast_translator_free_path(sf->trans);
  81. sf->trans = NULL;
  82. }
  83. if (!sf->trans) {
  84. if (!(sf->trans = ast_translator_build_path(sf->output_format, f->subclass.codec))) {
  85. ast_log(LOG_WARNING, "Cannot build a path from %s to %s\n", ast_getformatname(f->subclass.codec),
  86. ast_getformatname(sf->output_format));
  87. return 0;
  88. }
  89. sf->format = f->subclass.codec;
  90. }
  91. if (!(begin_frame = ast_translate(sf->trans, f, 0))) {
  92. return 0;
  93. }
  94. if (!(duped_frame = ast_frisolate(begin_frame))) {
  95. return 0;
  96. }
  97. if (duped_frame != begin_frame) {
  98. ast_frfree(begin_frame);
  99. }
  100. } else {
  101. if (sf->trans) {
  102. ast_translator_free_path(sf->trans);
  103. sf->trans = NULL;
  104. }
  105. if (!(duped_frame = ast_frdup(f)))
  106. return 0;
  107. }
  108. AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list) {
  109. x++;
  110. }
  111. /* if the frame was translated, the translator may have returned multiple
  112. frames, so process each of them
  113. */
  114. for (begin_frame = duped_frame; begin_frame; begin_frame = AST_LIST_NEXT(begin_frame, frame_list)) {
  115. AST_LIST_INSERT_TAIL(&sf->queue, begin_frame, frame_list);
  116. sf->size += begin_frame->samples;
  117. }
  118. return x;
  119. }
  120. int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples)
  121. {
  122. struct ast_frame *frame_ptr;
  123. unsigned int sofar = 0, ineed, remain;
  124. short *frame_data, *offset = buf;
  125. while (sofar < samples) {
  126. ineed = samples - sofar;
  127. if (sf->holdlen) {
  128. if (sf->holdlen <= ineed) {
  129. memcpy(offset, sf->offset, sf->holdlen * sizeof(*offset));
  130. sofar += sf->holdlen;
  131. offset += sf->holdlen;
  132. sf->holdlen = 0;
  133. sf->offset = sf->hold;
  134. } else {
  135. remain = sf->holdlen - ineed;
  136. memcpy(offset, sf->offset, ineed * sizeof(*offset));
  137. sofar += ineed;
  138. sf->offset += ineed;
  139. sf->holdlen = remain;
  140. }
  141. continue;
  142. }
  143. if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
  144. frame_data = frame_ptr->data.ptr;
  145. if (frame_ptr->samples <= ineed) {
  146. memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
  147. sofar += frame_ptr->samples;
  148. offset += frame_ptr->samples;
  149. } else {
  150. remain = frame_ptr->samples - ineed;
  151. memcpy(offset, frame_data, ineed * sizeof(*offset));
  152. sofar += ineed;
  153. frame_data += ineed;
  154. if (remain > (AST_SLINFACTORY_MAX_HOLD - sf->holdlen)) {
  155. remain = AST_SLINFACTORY_MAX_HOLD - sf->holdlen;
  156. }
  157. memcpy(sf->hold, frame_data, remain * sizeof(*offset));
  158. sf->holdlen = remain;
  159. }
  160. ast_frfree(frame_ptr);
  161. } else {
  162. break;
  163. }
  164. }
  165. sf->size -= sofar;
  166. return sofar;
  167. }
  168. unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
  169. {
  170. return sf->size;
  171. }
  172. void ast_slinfactory_flush(struct ast_slinfactory *sf)
  173. {
  174. struct ast_frame *fr = NULL;
  175. if (sf->trans) {
  176. ast_translator_free_path(sf->trans);
  177. sf->trans = NULL;
  178. }
  179. while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
  180. ast_frfree(fr);
  181. sf->size = sf->holdlen = 0;
  182. sf->offset = sf->hold;
  183. return;
  184. }