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