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