capitalize dialplan function
[asterisk/asterisk.git] / 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 /*
20  *
21  * A machine to gather up arbitrary frames and convert them
22  * to raw slinear on demand.
23  * 
24  */
25
26 #include "asterisk/slinfactory.h"
27 #include "asterisk/file.h"
28 #include "asterisk/logger.h"
29 #include "asterisk/channel.h"
30 #include "asterisk/pbx.h"
31 #include "asterisk/module.h"
32 #include "asterisk/lock.h"
33 #include "asterisk/cli.h"
34 #include "asterisk/options.h"
35 #include "asterisk/app.h"
36 #include "asterisk/translate.h"
37
38
39 void ast_slinfactory_init(struct ast_slinfactory *sf) 
40 {
41         memset(sf, 0, sizeof(struct ast_slinfactory));
42         sf->offset = sf->hold;
43         sf->queue = NULL;
44 }
45
46 void ast_slinfactory_destroy(struct ast_slinfactory *sf) 
47 {
48         struct ast_frame *f;
49
50         if (sf->trans) {
51                 ast_translator_free_path(sf->trans);
52                 sf->trans = NULL;
53         }
54
55         while((f = sf->queue)) {
56                 sf->queue = f->next;
57                 ast_frfree(f);
58         }
59 }
60
61 int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
62 {
63         struct ast_frame *frame, *frame_ptr;
64
65         if (!f) {
66                 return 0;
67         }
68
69         if (f->subclass != AST_FORMAT_SLINEAR) {
70                 if (sf->trans && f->subclass != sf->format) {
71                         ast_translator_free_path(sf->trans);
72                         sf->trans = NULL;
73                 }
74                 if (!sf->trans) {
75                         if ((sf->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, f->subclass)) == NULL) {
76                                 ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f->subclass));
77                                 return 0;
78                         } else {
79                                 sf->format = f->subclass;
80                         }
81                 }
82         }
83
84         if (sf->trans) {
85                 frame = ast_translate(sf->trans, f, 0);
86         } else {
87                 frame = ast_frdup(f);
88         }
89
90         if (frame) {
91                 int x = 0;
92                 for (frame_ptr = sf->queue; frame_ptr && frame_ptr->next; frame_ptr=frame_ptr->next) {
93                         x++;
94                 }
95                 if (frame_ptr) {
96                         frame_ptr->next = frame;
97                 } else {
98                         sf->queue = frame;
99                 }
100                 frame->next = NULL;
101                 sf->size += frame->datalen;     
102                 return x;
103         }
104
105         return 0;
106         
107 }
108
109 int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t bytes) 
110 {
111         struct ast_frame *frame_ptr;
112         int sofar = 0, ineed, remain;
113         short *frame_data, *offset = buf;
114
115         while (sofar < bytes) {
116                 ineed = bytes - sofar;
117
118                 if (sf->holdlen) {
119                         if ((sofar + sf->holdlen) <= ineed) {
120                                 memcpy(offset, sf->hold, sf->holdlen);
121                                 sofar += sf->holdlen;
122                                 offset += (sf->holdlen / sizeof(short));
123                                 sf->holdlen = 0;
124                                 sf->offset = sf->hold;
125                         } else {
126                                 remain = sf->holdlen - ineed;
127                                 memcpy(offset, sf->offset, ineed);
128                                 sofar += ineed;
129                                 sf->offset += (ineed / sizeof(short));
130                                 sf->holdlen = remain;
131                         }
132                         continue;
133                 }
134                 
135                 if ((frame_ptr = sf->queue)) {
136                         sf->queue = frame_ptr->next;
137                         frame_data = frame_ptr->data;
138                         
139                         if ((sofar + frame_ptr->datalen) <= ineed) {
140                                 memcpy(offset, frame_data, frame_ptr->datalen);
141                                 sofar += frame_ptr->datalen;
142                                 offset += (frame_ptr->datalen / sizeof(short));
143                         } else {
144                                 remain = frame_ptr->datalen - ineed;
145                                 memcpy(offset, frame_data, ineed);
146                                 sofar += ineed;
147                                 frame_data += (ineed / sizeof(short));
148                                 memcpy(sf->hold, frame_data, remain);
149                                 sf->holdlen = remain;
150                         }
151                         ast_frfree(frame_ptr);
152                 } else {
153                         break;
154                 }
155         }
156
157         sf->size -= sofar;
158         return sofar;
159 }
160
161
162
163