62a863d87dd21f567cd60372a1741910680ec10e
[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 <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 void ast_slinfactory_init(struct ast_slinfactory *sf) 
39 {
40         memset(sf, 0, sizeof(*sf));
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         unsigned int x;
64
65         if (f->subclass != AST_FORMAT_SLINEAR) {
66                 if (sf->trans && f->subclass != sf->format) {
67                         ast_translator_free_path(sf->trans);
68                         sf->trans = NULL;
69                 }
70                 if (!sf->trans) {
71                         if ((sf->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, f->subclass)) == NULL) {
72                                 ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f->subclass));
73                                 return 0;
74                         } else {
75                                 sf->format = f->subclass;
76                         }
77                 }
78         }
79
80         if (sf->trans)
81                 frame = ast_translate(sf->trans, f, 0);
82         else
83                 frame = ast_frdup(f);
84
85         if (!frame)
86                 return 0;
87
88         for (x = 0, frame_ptr = sf->queue; frame_ptr && frame_ptr->next; frame_ptr = frame_ptr->next)
89                 x++;
90
91         if (frame_ptr)
92                 frame_ptr->next = frame;
93         else
94                 sf->queue = frame;
95
96         frame->next = NULL;
97         sf->size += frame->samples;
98
99         return x;
100 }
101
102 int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples) 
103 {
104         struct ast_frame *frame_ptr;
105         unsigned int sofar = 0, ineed, remain;
106         short *frame_data, *offset = buf;
107
108         while (sofar < samples) {
109                 ineed = samples - sofar;
110
111                 if (sf->holdlen) {
112                         if ((sofar + sf->holdlen) <= ineed) {
113                                 memcpy(offset, sf->hold, sf->holdlen * sizeof(*offset));
114                                 sofar += sf->holdlen;
115                                 offset += sf->holdlen;
116                                 sf->holdlen = 0;
117                                 sf->offset = sf->hold;
118                         } else {
119                                 remain = sf->holdlen - ineed;
120                                 memcpy(offset, sf->offset, ineed * sizeof(*offset));
121                                 sofar += ineed;
122                                 sf->offset += ineed;
123                                 sf->holdlen = remain;
124                         }
125                         continue;
126                 }
127                 
128                 if ((frame_ptr = sf->queue)) {
129                         sf->queue = frame_ptr->next;
130                         frame_data = frame_ptr->data;
131                         
132                         if ((sofar + frame_ptr->samples) <= ineed) {
133                                 memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
134                                 sofar += frame_ptr->samples;
135                                 offset += frame_ptr->samples;
136                         } else {
137                                 remain = frame_ptr->samples - ineed;
138                                 memcpy(offset, frame_data, ineed * sizeof(*offset));
139                                 sofar += ineed;
140                                 frame_data += ineed;
141                                 memcpy(sf->hold, frame_data, remain * sizeof(*offset));
142                                 sf->holdlen = remain;
143                         }
144                         ast_frfree(frame_ptr);
145                 } else {
146                         break;
147                 }
148         }
149
150         sf->size -= sofar;
151         return sofar;
152 }
153
154 unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
155 {
156         return sf->size;
157 }