slinfactory.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. */
  24. #include <string.h>
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/slinfactory.h"
  28. #include "asterisk/logger.h"
  29. #include "asterisk/translate.h"
  30. void ast_slinfactory_init(struct ast_slinfactory *sf)
  31. {
  32. memset(sf, 0, sizeof(struct ast_slinfactory));
  33. sf->offset = sf->hold;
  34. sf->queue = NULL;
  35. }
  36. void ast_slinfactory_destroy(struct ast_slinfactory *sf)
  37. {
  38. struct ast_frame *f;
  39. if (sf->trans) {
  40. ast_translator_free_path(sf->trans);
  41. sf->trans = NULL;
  42. }
  43. while((f = sf->queue)) {
  44. sf->queue = f->next;
  45. ast_frfree(f);
  46. }
  47. }
  48. int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
  49. {
  50. struct ast_frame *frame, *frame_ptr;
  51. if (!f) {
  52. return 0;
  53. }
  54. if (f->subclass != AST_FORMAT_SLINEAR) {
  55. if (sf->trans && f->subclass != sf->format) {
  56. ast_translator_free_path(sf->trans);
  57. sf->trans = NULL;
  58. }
  59. if (!sf->trans) {
  60. if ((sf->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, f->subclass)) == NULL) {
  61. ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f->subclass));
  62. return 0;
  63. } else {
  64. sf->format = f->subclass;
  65. }
  66. }
  67. }
  68. if (sf->trans) {
  69. frame = ast_translate(sf->trans, f, 0);
  70. } else {
  71. frame = ast_frdup(f);
  72. }
  73. if (frame) {
  74. int x = 0;
  75. for (frame_ptr = sf->queue; frame_ptr && frame_ptr->next; frame_ptr=frame_ptr->next) {
  76. x++;
  77. }
  78. if (frame_ptr) {
  79. frame_ptr->next = frame;
  80. } else {
  81. sf->queue = frame;
  82. }
  83. frame->next = NULL;
  84. sf->size += frame->datalen;
  85. return x;
  86. }
  87. return 0;
  88. }
  89. int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t bytes)
  90. {
  91. struct ast_frame *frame_ptr;
  92. int sofar = 0, ineed, remain;
  93. short *frame_data, *offset = buf;
  94. while (sofar < bytes) {
  95. ineed = bytes - sofar;
  96. if (sf->holdlen) {
  97. if ((sofar + sf->holdlen) <= ineed) {
  98. memcpy(offset, sf->hold, sf->holdlen);
  99. sofar += sf->holdlen;
  100. offset += (sf->holdlen / sizeof(short));
  101. sf->holdlen = 0;
  102. sf->offset = sf->hold;
  103. } else {
  104. remain = sf->holdlen - ineed;
  105. memcpy(offset, sf->offset, ineed);
  106. sofar += ineed;
  107. sf->offset += (ineed / sizeof(short));
  108. sf->holdlen = remain;
  109. }
  110. continue;
  111. }
  112. if ((frame_ptr = sf->queue)) {
  113. sf->queue = frame_ptr->next;
  114. frame_data = frame_ptr->data;
  115. if ((sofar + frame_ptr->datalen) <= ineed) {
  116. memcpy(offset, frame_data, frame_ptr->datalen);
  117. sofar += frame_ptr->datalen;
  118. offset += (frame_ptr->datalen / sizeof(short));
  119. } else {
  120. remain = frame_ptr->datalen - ineed;
  121. memcpy(offset, frame_data, ineed);
  122. sofar += ineed;
  123. frame_data += (ineed / sizeof(short));
  124. memcpy(sf->hold, frame_data, remain);
  125. sf->holdlen = remain;
  126. }
  127. ast_frfree(frame_ptr);
  128. } else {
  129. break;
  130. }
  131. }
  132. sf->size -= sofar;
  133. return sofar;
  134. }