1 | //container_framework.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | #define _DECL() struct cont_fw_child_vio_inst * inst |
---|
28 | #define _CHECK() if ( vio == NULL ) \ |
---|
29 | return -1 |
---|
30 | #define _CINST() if ( (inst = vio->inst) == NULL ) \ |
---|
31 | return -1 |
---|
32 | #define _PREP() _CHECK(); \ |
---|
33 | _CINST() |
---|
34 | #define _BASIC() _DECL(); \ |
---|
35 | _PREP() |
---|
36 | |
---|
37 | // Parent: |
---|
38 | int cont_fw_new (struct cont_fw_parent_inst ** inst) { |
---|
39 | struct cont_fw_parent_inst * self; |
---|
40 | |
---|
41 | if ( inst == NULL ) |
---|
42 | return -1; |
---|
43 | |
---|
44 | if ( (self = malloc(sizeof(struct cont_fw_parent_inst))) == NULL ) { |
---|
45 | *inst = NULL; |
---|
46 | return -1; |
---|
47 | } |
---|
48 | |
---|
49 | memset(self, 0, sizeof(struct cont_fw_parent_inst)); |
---|
50 | |
---|
51 | self->stream.id = -1; |
---|
52 | self->state = ROAR_STREAMSTATE_INITING; |
---|
53 | |
---|
54 | *inst = self; |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | int cont_fw_delete (struct cont_fw_parent_inst * inst) { |
---|
59 | int i; |
---|
60 | |
---|
61 | ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst); |
---|
62 | |
---|
63 | if ( inst == NULL ) |
---|
64 | return -1; |
---|
65 | |
---|
66 | ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst); |
---|
67 | |
---|
68 | // check if there are streams to close... |
---|
69 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
70 | if ( inst->child[i] != NULL ) { |
---|
71 | ROAR_DBG("cont_fw_delete(inst=%p) = -1 // there are still open childs", inst); |
---|
72 | return -1; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst); |
---|
77 | |
---|
78 | if ( inst->pcb.close != NULL ) |
---|
79 | inst->pcb.close(inst); |
---|
80 | |
---|
81 | free(inst); |
---|
82 | |
---|
83 | ROAR_DBG("cont_fw_delete(inst=%p) = 0", inst); |
---|
84 | |
---|
85 | return 0; |
---|
86 | } |
---|
87 | |
---|
88 | int cont_fw_set_uinst(struct cont_fw_parent_inst * inst, void * u_inst) { |
---|
89 | if ( inst == NULL ) |
---|
90 | return -1; |
---|
91 | |
---|
92 | inst->u_inst = u_inst; |
---|
93 | |
---|
94 | return 0; |
---|
95 | } |
---|
96 | |
---|
97 | int cont_fw_get_uinst(struct cont_fw_parent_inst * inst, void ** u_inst) { |
---|
98 | if ( inst == NULL || u_inst == NULL ) |
---|
99 | return -1; |
---|
100 | |
---|
101 | *u_inst = inst->u_inst; |
---|
102 | |
---|
103 | return 0; |
---|
104 | } |
---|
105 | |
---|
106 | // VIOs: |
---|
107 | int cont_fw_new_child(struct cont_fw_parent_inst * inst, int id) { |
---|
108 | struct cont_fw_child_vio_inst * self; |
---|
109 | struct roar_stream_server * ss; |
---|
110 | int i; |
---|
111 | int cid = -1; |
---|
112 | |
---|
113 | ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = ?", inst, id); |
---|
114 | |
---|
115 | if ( inst == NULL || id == -1 ) |
---|
116 | return -1; |
---|
117 | |
---|
118 | if ( streams_get(id, &ss) == -1 ) |
---|
119 | return -1; |
---|
120 | |
---|
121 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
122 | if ( inst->child[i] == NULL ) { |
---|
123 | cid = i; |
---|
124 | break; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | if ( cid == -1 ) |
---|
129 | return -1; |
---|
130 | |
---|
131 | if ( (self = malloc(sizeof(struct cont_fw_child_vio_inst))) == NULL ) |
---|
132 | return -1; |
---|
133 | |
---|
134 | memset(self, 0, sizeof(struct cont_fw_child_vio_inst)); |
---|
135 | |
---|
136 | self->parent = inst; |
---|
137 | self->child = id; |
---|
138 | self->u_inst = NULL; |
---|
139 | |
---|
140 | inst->child[i] = self; |
---|
141 | |
---|
142 | if ( inst->pcb.new_child != NULL ) { |
---|
143 | if ( inst->pcb.new_child(inst, self) == -1 ) { |
---|
144 | inst->child[i] = NULL; |
---|
145 | free(self); |
---|
146 | return -1; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | // no error possible here. |
---|
151 | cont_fw_init_vio(&(ss->vio), self); |
---|
152 | // ss->ready = 1; |
---|
153 | |
---|
154 | streams_set_fh(id, -2); // update some internal structures |
---|
155 | |
---|
156 | ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = 0", inst, id); |
---|
157 | return 0; |
---|
158 | } |
---|
159 | |
---|
160 | int cont_fw_init_vio(struct roar_vio_calls * vio, void * inst) { |
---|
161 | if ( vio == NULL ) |
---|
162 | return -1; |
---|
163 | |
---|
164 | memset(vio, 0, sizeof(struct roar_vio_calls)); |
---|
165 | vio->inst = inst; |
---|
166 | |
---|
167 | vio->read = cont_fw_read; |
---|
168 | vio->write = cont_fw_write; |
---|
169 | vio->sync = cont_fw_sync; |
---|
170 | vio->close = cont_fw_close; |
---|
171 | |
---|
172 | return 0; |
---|
173 | } |
---|
174 | |
---|
175 | ssize_t cont_fw_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
176 | _BASIC(); |
---|
177 | |
---|
178 | if ( inst->parent->ccb.read != NULL ) |
---|
179 | return inst->parent->ccb.read(inst->parent, inst, buf, count); |
---|
180 | |
---|
181 | return -1; |
---|
182 | } |
---|
183 | |
---|
184 | ssize_t cont_fw_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
185 | _BASIC(); |
---|
186 | |
---|
187 | if ( inst->parent->ccb.write != NULL ) |
---|
188 | return inst->parent->ccb.write(inst->parent, inst, buf, count); |
---|
189 | |
---|
190 | return -1; |
---|
191 | } |
---|
192 | |
---|
193 | off_t cont_fw_lseek (struct roar_vio_calls * vio, off_t offset, int whence); |
---|
194 | int cont_fw_nonblock(struct roar_vio_calls * vio, int state); |
---|
195 | |
---|
196 | int cont_fw_sync (struct roar_vio_calls * vio) { |
---|
197 | _BASIC(); |
---|
198 | |
---|
199 | if ( inst->parent->ccb.flush != NULL ) |
---|
200 | return inst->parent->ccb.flush(inst->parent, inst); |
---|
201 | |
---|
202 | return 0; |
---|
203 | } |
---|
204 | |
---|
205 | int cont_fw_close (struct roar_vio_calls * vio) { |
---|
206 | _DECL(); |
---|
207 | int r = 0; |
---|
208 | int i; |
---|
209 | |
---|
210 | _PREP(); |
---|
211 | |
---|
212 | if ( cont_fw_sync(vio) == -1 ) |
---|
213 | r = -1; |
---|
214 | |
---|
215 | if ( inst->parent->ccb.close != NULL ) |
---|
216 | if ( inst->parent->ccb.close(inst->parent, inst) == -1 ) |
---|
217 | r = -1; |
---|
218 | |
---|
219 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
220 | if ( inst->parent->child[i] == inst ) { |
---|
221 | inst->parent->child[i] = NULL; |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | free(inst); |
---|
226 | |
---|
227 | return r; |
---|
228 | } |
---|
229 | |
---|
230 | int cont_fw_ctl (struct roar_vio_calls * vio, int cmd, void * data); |
---|
231 | |
---|
232 | // CF: |
---|
233 | int cont_fw_cf_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
234 | struct roar_stream_server * info, |
---|
235 | struct roar_codecfilter * filter) { |
---|
236 | struct cont_fw_parent_inst * self; |
---|
237 | CONT_FW_SETUP_TYPE((*setup)); |
---|
238 | |
---|
239 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
240 | |
---|
241 | if ( cont_fw_new(&self) == -1 ) |
---|
242 | return -1; |
---|
243 | |
---|
244 | self->stream.codec = codec; |
---|
245 | self->stream.id = ROAR_STREAM(info)->id; |
---|
246 | self->stream.stream = info; |
---|
247 | self->stream.filter = filter; |
---|
248 | |
---|
249 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
250 | |
---|
251 | if ( (setup = filter->setup) != NULL ) { |
---|
252 | if ( setup(self, codec, filter) == -1 ) { |
---|
253 | cont_fw_delete(self); |
---|
254 | return -1; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
259 | |
---|
260 | *inst = self; |
---|
261 | |
---|
262 | ROAR_DBG("cont_fw_cf_open(*) = 0"); |
---|
263 | return 0; |
---|
264 | } |
---|
265 | |
---|
266 | int cont_fw_cf_close(CODECFILTER_USERDATA_T inst) { |
---|
267 | ROAR_DBG("cont_fw_cf_close(*) = ?"); |
---|
268 | return cont_fw_delete(inst); |
---|
269 | } |
---|
270 | |
---|
271 | int cont_fw_cf_pause(CODECFILTER_USERDATA_T inst, int newstate); |
---|
272 | |
---|
273 | |
---|
274 | // no direct read or writing... |
---|
275 | int cont_fw_cf_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
276 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
277 | |
---|
278 | ROAR_DBG("cont_fw_cf_write(*) = ?"); |
---|
279 | |
---|
280 | if ( self->state == ROAR_STREAMSTATE_INITING ) { |
---|
281 | if ( self->pcb.open != NULL ) { |
---|
282 | if ( self->pcb.open(self, self->stream.codec, self->stream.stream, self->stream.filter) == -1 ) { |
---|
283 | return -1; |
---|
284 | } |
---|
285 | } |
---|
286 | self->state = ROAR_STREAMSTATE_NEW; |
---|
287 | |
---|
288 | ROAR_DBG("cont_fw_cf_write(*) = 0"); |
---|
289 | return 0; |
---|
290 | } else { |
---|
291 | ROAR_DBG("cont_fw_cf_write(*) = -1"); |
---|
292 | return -1; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | int cont_fw_cf_read (CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
297 | ROAR_DBG("cont_fw_cf_read(*) = -1"); |
---|
298 | return -1; |
---|
299 | } |
---|
300 | |
---|
301 | int cont_fw_cf_flush(CODECFILTER_USERDATA_T inst) { |
---|
302 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
303 | |
---|
304 | ROAR_DBG("cont_fw_cf_flush(*) = ?"); |
---|
305 | |
---|
306 | if ( self->pcb.flush != NULL ) |
---|
307 | return self->pcb.flush(self); |
---|
308 | |
---|
309 | return 0; |
---|
310 | } |
---|
311 | |
---|
312 | int cont_fw_cf_delay(CODECFILTER_USERDATA_T inst, uint_least32_t * delay); |
---|
313 | |
---|
314 | int cont_fw_cf_ctl (CODECFILTER_USERDATA_T inst, int cmd, void * data) { |
---|
315 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
316 | int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK; |
---|
317 | |
---|
318 | ROAR_DBG("cont_fw_cf_ctl(*) = ?"); |
---|
319 | |
---|
320 | cmd -= type; |
---|
321 | |
---|
322 | ROAR_DBG("cont_fw_cf_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
323 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
324 | |
---|
325 | if ( data == NULL && type != ROAR_STREAM_CTL_TYPE_VOID ) |
---|
326 | return -1; |
---|
327 | |
---|
328 | switch (cmd) { |
---|
329 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE): |
---|
330 | return 0; |
---|
331 | break; |
---|
332 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_NEW): |
---|
333 | if ( type != ROAR_STREAM_CTL_TYPE_INT ) |
---|
334 | return -1; |
---|
335 | |
---|
336 | return cont_fw_new_child(self, *(int*)data); |
---|
337 | break; |
---|
338 | default: |
---|
339 | ROAR_DBG("cont_fw_cf_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
340 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
341 | return -1; |
---|
342 | } |
---|
343 | |
---|
344 | return -1; |
---|
345 | } |
---|
346 | |
---|
347 | //ll |
---|