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