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