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