Terry found this problem with running the expr2 parser on OSX. Make the #defines...
[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 }
40
41 void ast_slinfactory_destroy(struct ast_slinfactory *sf) 
42 {
43         struct ast_frame *f;
44
45         if (sf->trans) {
46                 ast_translator_free_path(sf->trans);
47                 sf->trans = NULL;
48         }
49
50         while ((f = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
51                 ast_frfree(f);
52 }
53
54 int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
55 {
56         struct ast_frame *begin_frame = f, *duped_frame = NULL, *frame_ptr;
57         unsigned int x;
58
59         if (f->subclass != AST_FORMAT_SLINEAR) {
60                 if (sf->trans && f->subclass != sf->format) {
61                         ast_translator_free_path(sf->trans);
62                         sf->trans = NULL;
63                 }
64                 if (!sf->trans) {
65                         if (!(sf->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, f->subclass))) {
66                                 ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f->subclass));
67                                 return 0;
68                         }
69                         sf->format = f->subclass;
70                 }
71                 if (!(begin_frame = ast_translate(sf->trans, f, 0)) || !(duped_frame = ast_frdup(begin_frame)))
72                         return 0;
73         } else {
74                 if (sf->trans) {
75                         ast_translator_free_path(sf->trans);
76                         sf->trans = NULL;
77                 }
78                 if (!(duped_frame = ast_frdup(f)))
79                         return 0;
80         }
81
82         x = 0;
83         AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list)
84                 x++;
85
86         AST_LIST_INSERT_TAIL(&sf->queue, duped_frame, frame_list);
87
88         sf->size += duped_frame->samples;
89
90         return x;
91 }
92
93 int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples) 
94 {
95         struct ast_frame *frame_ptr;
96         unsigned int sofar = 0, ineed, remain;
97         short *frame_data, *offset = buf;
98
99         while (sofar < samples) {
100                 ineed = samples - sofar;
101
102                 if (sf->holdlen) {
103                         if ((sofar + sf->holdlen) <= ineed) {
104                                 memcpy(offset, sf->hold, sf->holdlen * sizeof(*offset));
105                                 sofar += sf->holdlen;
106                                 offset += sf->holdlen;
107                                 sf->holdlen = 0;
108                                 sf->offset = sf->hold;
109                         } else {
110                                 remain = sf->holdlen - ineed;
111                                 memcpy(offset, sf->offset, ineed * sizeof(*offset));
112                                 sofar += ineed;
113                                 sf->offset += ineed;
114                                 sf->holdlen = remain;
115                         }
116                         continue;
117                 }
118                 
119                 if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
120                         frame_data = frame_ptr->data;
121                         
122                         if ((sofar + frame_ptr->samples) <= ineed) {
123                                 memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
124                                 sofar += frame_ptr->samples;
125                                 offset += frame_ptr->samples;
126                         } else {
127                                 remain = frame_ptr->samples - ineed;
128                                 memcpy(offset, frame_data, ineed * sizeof(*offset));
129                                 sofar += ineed;
130                                 frame_data += ineed;
131                                 memcpy(sf->hold, frame_data, remain * sizeof(*offset));
132                                 sf->holdlen = remain;
133                         }
134                         ast_frfree(frame_ptr);
135                 } else {
136                         break;
137                 }
138         }
139
140         sf->size -= sofar;
141         return sofar;
142 }
143
144 unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
145 {
146         return sf->size;
147 }
148
149 void ast_slinfactory_flush(struct ast_slinfactory *sf)
150 {
151         struct ast_frame *fr = NULL;
152
153         if (sf->trans) {
154                 ast_translator_free_path(sf->trans);
155                 sf->trans = NULL;
156         }
157
158         while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
159                 ast_frfree(fr);
160
161         sf->size = sf->holdlen = 0;
162         sf->offset = sf->hold;
163
164         return;
165 }