2e6e4719357240cc336330902b07e44eaf37df2e
[asterisk/asterisk.git] / main / slinfactory.c
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
19 /*! \file
20  *
21  * \brief A machine to gather up arbitrary frames and convert them
22  * to raw slinear on demand.
23  *
24  * \author Anthony Minessale <anthmct@yahoo.com>
25  */
26
27 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 #include "asterisk/frame.h"
32 #include "asterisk/slinfactory.h"
33 #include "asterisk/translate.h"
34
35 /*!
36  * \brief Initialize an slinfactory
37  *
38  * \arg sf The slinfactory to initialize
39  *
40  * \return Nothing
41  */
42 void ast_slinfactory_init(struct ast_slinfactory *sf) 
43 {
44         memset(sf, 0, sizeof(*sf));
45         sf->offset = sf->hold;
46 }
47
48 /*!
49  * \brief Destroy the contents of a slinfactory
50  *
51  * \arg sf The slinfactory that is no longer needed
52  *
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.
56  *
57  * \return Nothing
58  */
59 void ast_slinfactory_destroy(struct ast_slinfactory *sf) 
60 {
61         struct ast_frame *f;
62
63         if (sf->trans) {
64                 ast_translator_free_path(sf->trans);
65                 sf->trans = NULL;
66         }
67
68         while ((f = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
69                 ast_frfree(f);
70 }
71
72 /*!
73  * \brief Feed audio into an slinfactory
74  *
75  * \arg sf The slinfactory to feed into
76  * \arg f Frame containing audio to feed in
77  *
78  * \return Number of frames currently in factory
79  */
80 int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
81 {
82         struct ast_frame *begin_frame = f, *duped_frame = NULL, *frame_ptr;
83         unsigned int x;
84
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.
90          */
91         if (!f->data.ptr) {
92                 return 0;
93         }
94
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);
98                         sf->trans = NULL;
99                 }
100
101                 if (!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));
104                                 return 0;
105                         }
106                         sf->format = f->subclass;
107                 }
108
109                 if (!(begin_frame = ast_translate(sf->trans, f, 0))) 
110                         return 0;
111                 
112                 duped_frame = ast_frdup(begin_frame);
113
114                 ast_frfree(begin_frame);
115
116                 if (!duped_frame)
117                         return 0;
118         } else {
119                 if (sf->trans) {
120                         ast_translator_free_path(sf->trans);
121                         sf->trans = NULL;
122                 }
123                 if (!(duped_frame = ast_frdup(f)))
124                         return 0;
125         }
126
127         x = 0;
128         AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list)
129                 x++;
130
131         AST_LIST_INSERT_TAIL(&sf->queue, duped_frame, frame_list);
132
133         sf->size += duped_frame->samples;
134
135         return x;
136 }
137
138 /*!
139  * \brief Read samples from an slinfactory
140  *
141  * \arg sf The slinfactory to read from
142  * \arg buf Buffer to put samples into
143  * \arg samples Number of samples wanted
144  *
145  * \return Number of samples read
146  */
147 int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples) 
148 {
149         struct ast_frame *frame_ptr;
150         unsigned int sofar = 0, ineed, remain;
151         short *frame_data, *offset = buf;
152
153         while (sofar < samples) {
154                 ineed = samples - sofar;
155
156                 if (sf->holdlen) {
157                         if (sf->holdlen <= ineed) {
158                                 memcpy(offset, sf->hold, sf->holdlen * sizeof(*offset));
159                                 sofar += sf->holdlen;
160                                 offset += sf->holdlen;
161                                 sf->holdlen = 0;
162                                 sf->offset = sf->hold;
163                         } else {
164                                 remain = sf->holdlen - ineed;
165                                 memcpy(offset, sf->offset, ineed * sizeof(*offset));
166                                 sofar += ineed;
167                                 sf->offset += ineed;
168                                 sf->holdlen = remain;
169                         }
170                         continue;
171                 }
172                 
173                 if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
174                         frame_data = frame_ptr->data.ptr;
175                         
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;
180                         } else {
181                                 remain = frame_ptr->samples - ineed;
182                                 memcpy(offset, frame_data, ineed * sizeof(*offset));
183                                 sofar += ineed;
184                                 frame_data += ineed;
185                                 if (remain > (AST_SLINFACTORY_MAX_HOLD - sf->holdlen)) {
186                                         remain = AST_SLINFACTORY_MAX_HOLD - sf->holdlen;
187                                 }
188                                 memcpy(sf->hold, frame_data, remain * sizeof(*offset));
189                                 sf->holdlen = remain;
190                         }
191                         ast_frfree(frame_ptr);
192                 } else {
193                         break;
194                 }
195         }
196
197         sf->size -= sofar;
198         return sofar;
199 }
200
201 /*!
202  * \brief Retrieve number of samples currently in an slinfactory
203  *
204  * \arg sf The slinfactory to peek into
205  *
206  * \return Number of samples in slinfactory
207  */
208 unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
209 {
210         return sf->size;
211 }
212
213 /*!
214  * \brief Flush the contents of an slinfactory
215  *
216  * \arg sf The slinfactory to flush
217  *
218  * \return Nothing
219  */
220 void ast_slinfactory_flush(struct ast_slinfactory *sf)
221 {
222         struct ast_frame *fr = NULL;
223
224         if (sf->trans) {
225                 ast_translator_free_path(sf->trans);
226                 sf->trans = NULL;
227         }
228
229         while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
230                 ast_frfree(fr);
231
232         sf->size = sf->holdlen = 0;
233         sf->offset = sf->hold;
234
235         return;
236 }