Debian Codegraph @

main/slinfactory.c

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