slinfactory.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. ast_format_set(&sf->output_format, AST_FORMAT_SLINEAR, 0);
  38. }
  39. int ast_slinfactory_init_with_format(struct ast_slinfactory *sf, const struct ast_format *slin_out)
  40. {
  41. memset(sf, 0, sizeof(*sf));
  42. sf->offset = sf->hold;
  43. if (!ast_format_is_slinear(slin_out)) {
  44. return -1;
  45. }
  46. ast_format_copy(&sf->output_format, slin_out);
  47. return 0;
  48. }
  49. void ast_slinfactory_destroy(struct ast_slinfactory *sf)
  50. {
  51. struct ast_frame *f;
  52. if (sf->trans) {
  53. ast_translator_free_path(sf->trans);
  54. sf->trans = NULL;
  55. }
  56. while ((f = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
  57. ast_frfree(f);
  58. }
  59. int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
  60. {
  61. struct ast_frame *begin_frame = f, *duped_frame = NULL, *frame_ptr;
  62. unsigned int x = 0;
  63. /* In some cases, we can be passed a frame which has no data in it, but
  64. * which has a positive number of samples defined. Once such situation is
  65. * when a jitter buffer is in use and the jitter buffer interpolates a frame.
  66. * The frame it produces has data set to NULL, datalen set to 0, and samples
  67. * set to either 160 or 240.
  68. */
  69. if (!f->data.ptr) {
  70. return 0;
  71. }
  72. if (ast_format_cmp(&f->subclass.format, &sf->output_format) == AST_FORMAT_CMP_NOT_EQUAL) {
  73. if (sf->trans && (ast_format_cmp(&f->subclass.format, &sf->format) == AST_FORMAT_CMP_NOT_EQUAL)) {
  74. ast_translator_free_path(sf->trans);
  75. sf->trans = NULL;
  76. }
  77. if (!sf->trans) {
  78. if (!(sf->trans = ast_translator_build_path(&sf->output_format, &f->subclass.format))) {
  79. ast_log(LOG_WARNING, "Cannot build a path from %s (%u)to %s (%u)\n",
  80. ast_getformatname(&f->subclass.format),
  81. f->subclass.format.id,
  82. ast_getformatname(&sf->output_format),
  83. sf->output_format.id);
  84. return 0;
  85. }
  86. ast_format_copy(&sf->format, &f->subclass.format);
  87. }
  88. if (!(begin_frame = ast_translate(sf->trans, f, 0))) {
  89. return 0;
  90. }
  91. if (!(duped_frame = ast_frisolate(begin_frame))) {
  92. return 0;
  93. }
  94. if (duped_frame != begin_frame) {
  95. ast_frfree(begin_frame);
  96. }
  97. } else {
  98. if (sf->trans) {
  99. ast_translator_free_path(sf->trans);
  100. sf->trans = NULL;
  101. }
  102. if (!(duped_frame = ast_frdup(f)))
  103. return 0;
  104. }
  105. AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list) {
  106. x++;
  107. }
  108. /* if the frame was translated, the translator may have returned multiple
  109. frames, so process each of them
  110. */
  111. for (begin_frame = duped_frame; begin_frame; begin_frame = AST_LIST_NEXT(begin_frame, frame_list)) {
  112. AST_LIST_INSERT_TAIL(&sf->queue, begin_frame, frame_list);
  113. sf->size += begin_frame->samples;
  114. }
  115. return x;
  116. }
  117. int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples)
  118. {
  119. struct ast_frame *frame_ptr;
  120. unsigned int sofar = 0, ineed, remain;
  121. short *frame_data, *offset = buf;
  122. while (sofar < samples) {
  123. ineed = samples - sofar;
  124. if (sf->holdlen) {
  125. if (sf->holdlen <= ineed) {
  126. memcpy(offset, sf->offset, sf->holdlen * sizeof(*offset));
  127. sofar += sf->holdlen;
  128. offset += sf->holdlen;
  129. sf->holdlen = 0;
  130. sf->offset = sf->hold;
  131. } else {
  132. remain = sf->holdlen - ineed;
  133. memcpy(offset, sf->offset, ineed * sizeof(*offset));
  134. sofar += ineed;
  135. sf->offset += ineed;
  136. sf->holdlen = remain;
  137. }
  138. continue;
  139. }
  140. if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
  141. frame_data = frame_ptr->data.ptr;
  142. if (frame_ptr->samples <= ineed) {
  143. memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
  144. sofar += frame_ptr->samples;
  145. offset += frame_ptr->samples;
  146. } else {
  147. remain = frame_ptr->samples - ineed;
  148. memcpy(offset, frame_data, ineed * sizeof(*offset));
  149. sofar += ineed;
  150. frame_data += ineed;
  151. if (remain > (AST_SLINFACTORY_MAX_HOLD - sf->holdlen)) {
  152. remain = AST_SLINFACTORY_MAX_HOLD - sf->holdlen;
  153. }
  154. memcpy(sf->hold, frame_data, remain * sizeof(*offset));
  155. sf->holdlen = remain;
  156. }
  157. ast_frfree(frame_ptr);
  158. } else {
  159. break;
  160. }
  161. }
  162. sf->size -= sofar;
  163. return sofar;
  164. }
  165. unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
  166. {
  167. return sf->size;
  168. }
  169. void ast_slinfactory_flush(struct ast_slinfactory *sf)
  170. {
  171. struct ast_frame *fr = NULL;
  172. if (sf->trans) {
  173. ast_translator_free_path(sf->trans);
  174. sf->trans = NULL;
  175. }
  176. while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
  177. ast_frfree(fr);
  178. sf->size = sf->holdlen = 0;
  179. sf->offset = sf->hold;
  180. return;
  181. }