2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Anthony Minessale II.
6 * Anthony Minessale <anthmct@yahoo.com>
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.
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.
21 * \brief A machine to gather up arbitrary frames and convert them
22 * to raw slinear on demand.
24 * \author Anthony Minessale <anthmct@yahoo.com>
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include "asterisk/frame.h"
32 #include "asterisk/slinfactory.h"
33 #include "asterisk/translate.h"
36 * \brief Initialize an slinfactory
38 * \arg sf The slinfactory to initialize
42 void ast_slinfactory_init(struct ast_slinfactory *sf)
44 memset(sf, 0, sizeof(*sf));
45 sf->offset = sf->hold;
49 * \brief Destroy the contents of a slinfactory
51 * \arg sf The slinfactory that is no longer needed
53 * This function will free any memory allocated for the contents of the
54 * slinfactory. It does not free the slinfactory itself. If the sf is
55 * malloc'd, then it must be explicitly free'd after calling this function.
59 void ast_slinfactory_destroy(struct ast_slinfactory *sf)
64 ast_translator_free_path(sf->trans);
68 while ((f = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
73 * \brief Feed audio into an slinfactory
75 * \arg sf The slinfactory to feed into
76 * \arg f Frame containing audio to feed in
78 * \return Number of frames currently in factory
80 int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
82 struct ast_frame *begin_frame = f, *duped_frame = NULL, *frame_ptr;
85 /* In some cases, we can be passed a frame which has no data in it, but
86 * which has a positive number of samples defined. Once such situation is
87 * when a jitter buffer is in use and the jitter buffer interpolates a frame.
88 * The frame it produces has data set to NULL, datalen set to 0, and samples
89 * set to either 160 or 240.
95 if (f->subclass != AST_FORMAT_SLINEAR && f->subclass != AST_FORMAT_SLINEAR16) {
96 if (sf->trans && f->subclass != sf->format) {
97 ast_translator_free_path(sf->trans);
102 if (!(sf->trans = ast_translator_build_path((f->subclass == AST_FORMAT_G722 ? AST_FORMAT_SLINEAR16 : AST_FORMAT_SLINEAR), f->subclass))) {
103 ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f->subclass));
106 sf->format = f->subclass;
109 if (!(begin_frame = ast_translate(sf->trans, f, 0)))
112 duped_frame = ast_frdup(begin_frame);
114 ast_frfree(begin_frame);
120 ast_translator_free_path(sf->trans);
123 if (!(duped_frame = ast_frdup(f)))
128 AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list)
131 AST_LIST_INSERT_TAIL(&sf->queue, duped_frame, frame_list);
133 sf->size += duped_frame->samples;
139 * \brief Read samples from an slinfactory
141 * \arg sf The slinfactory to read from
142 * \arg buf Buffer to put samples into
143 * \arg samples Number of samples wanted
145 * \return Number of samples read
147 int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples)
149 struct ast_frame *frame_ptr;
150 unsigned int sofar = 0, ineed, remain;
151 short *frame_data, *offset = buf;
153 while (sofar < samples) {
154 ineed = samples - sofar;
157 if (sf->holdlen <= ineed) {
158 memcpy(offset, sf->hold, sf->holdlen * sizeof(*offset));
159 sofar += sf->holdlen;
160 offset += sf->holdlen;
162 sf->offset = sf->hold;
164 remain = sf->holdlen - ineed;
165 memcpy(offset, sf->offset, ineed * sizeof(*offset));
168 sf->holdlen = remain;
173 if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
174 frame_data = frame_ptr->data.ptr;
176 if (frame_ptr->samples <= ineed) {
177 memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
178 sofar += frame_ptr->samples;
179 offset += frame_ptr->samples;
181 remain = frame_ptr->samples - ineed;
182 memcpy(offset, frame_data, ineed * sizeof(*offset));
185 if (remain > (AST_SLINFACTORY_MAX_HOLD - sf->holdlen)) {
186 remain = AST_SLINFACTORY_MAX_HOLD - sf->holdlen;
188 memcpy(sf->hold, frame_data, remain * sizeof(*offset));
189 sf->holdlen = remain;
191 ast_frfree(frame_ptr);
202 * \brief Retrieve number of samples currently in an slinfactory
204 * \arg sf The slinfactory to peek into
206 * \return Number of samples in slinfactory
208 unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
214 * \brief Flush the contents of an slinfactory
216 * \arg sf The slinfactory to flush
220 void ast_slinfactory_flush(struct ast_slinfactory *sf)
222 struct ast_frame *fr = NULL;
225 ast_translator_free_path(sf->trans);
229 while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
232 sf->size = sf->holdlen = 0;
233 sf->offset = sf->hold;