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