This adds DNS SRV record support to DNS manager. If there is a SRV record for a...
[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         if (f->subclass != AST_FORMAT_SLINEAR && f->subclass != AST_FORMAT_SLINEAR16) {
86                 if (sf->trans && f->subclass != sf->format) {
87                         ast_translator_free_path(sf->trans);
88                         sf->trans = NULL;
89                 }
90
91                 if (!sf->trans) {
92                         if (!(sf->trans = ast_translator_build_path((f->subclass == AST_FORMAT_G722 ? AST_FORMAT_SLINEAR16 : AST_FORMAT_SLINEAR), f->subclass))) {
93                                 ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f->subclass));
94                                 return 0;
95                         }
96                         sf->format = f->subclass;
97                 }
98
99                 if (!(begin_frame = ast_translate(sf->trans, f, 0))) 
100                         return 0;
101                 
102                 duped_frame = ast_frdup(begin_frame);
103
104                 ast_frfree(begin_frame);
105
106                 if (!duped_frame)
107                         return 0;
108         } else {
109                 if (sf->trans) {
110                         ast_translator_free_path(sf->trans);
111                         sf->trans = NULL;
112                 }
113                 if (!(duped_frame = ast_frdup(f)))
114                         return 0;
115         }
116
117         x = 0;
118         AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list)
119                 x++;
120
121         AST_LIST_INSERT_TAIL(&sf->queue, duped_frame, frame_list);
122
123         sf->size += duped_frame->samples;
124
125         return x;
126 }
127
128 /*!
129  * \brief Read samples from an slinfactory
130  *
131  * \arg sf The slinfactory to read from
132  * \arg buf Buffer to put samples into
133  * \arg samples Number of samples wanted
134  *
135  * \return Number of samples read
136  */
137 int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples) 
138 {
139         struct ast_frame *frame_ptr;
140         unsigned int sofar = 0, ineed, remain;
141         short *frame_data, *offset = buf;
142
143         while (sofar < samples) {
144                 ineed = samples - sofar;
145
146                 if (sf->holdlen) {
147                         if (sf->holdlen <= ineed) {
148                                 memcpy(offset, sf->hold, sf->holdlen * sizeof(*offset));
149                                 sofar += sf->holdlen;
150                                 offset += sf->holdlen;
151                                 sf->holdlen = 0;
152                                 sf->offset = sf->hold;
153                         } else {
154                                 remain = sf->holdlen - ineed;
155                                 memcpy(offset, sf->offset, ineed * sizeof(*offset));
156                                 sofar += ineed;
157                                 sf->offset += ineed;
158                                 sf->holdlen = remain;
159                         }
160                         continue;
161                 }
162                 
163                 if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
164                         frame_data = frame_ptr->data;
165                         
166                         if (frame_ptr->samples <= ineed) {
167                                 memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
168                                 sofar += frame_ptr->samples;
169                                 offset += frame_ptr->samples;
170                         } else {
171                                 remain = frame_ptr->samples - ineed;
172                                 memcpy(offset, frame_data, ineed * sizeof(*offset));
173                                 sofar += ineed;
174                                 frame_data += ineed;
175                                 memcpy(sf->hold, frame_data, remain * sizeof(*offset));
176                                 sf->holdlen = remain;
177                         }
178                         ast_frfree(frame_ptr);
179                 } else {
180                         break;
181                 }
182         }
183
184         sf->size -= sofar;
185         return sofar;
186 }
187
188 /*!
189  * \brief Retrieve number of samples currently in an slinfactory
190  *
191  * \arg sf The slinfactory to peek into
192  *
193  * \return Number of samples in slinfactory
194  */
195 unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
196 {
197         return sf->size;
198 }
199
200 /*!
201  * \brief Flush the contents of an slinfactory
202  *
203  * \arg sf The slinfactory to flush
204  *
205  * \return Nothing
206  */
207 void ast_slinfactory_flush(struct ast_slinfactory *sf)
208 {
209         struct ast_frame *fr = NULL;
210
211         if (sf->trans) {
212                 ast_translator_free_path(sf->trans);
213                 sf->trans = NULL;
214         }
215
216         while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
217                 ast_frfree(fr);
218
219         sf->size = sf->holdlen = 0;
220         sf->offset = sf->hold;
221
222         return;
223 }