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 | if ( cont_pvio_open(&(self->vio), self) == -1 ) { |
---|
55 | free(self); |
---|
56 | return -1; |
---|
57 | } |
---|
58 | |
---|
59 | *inst = self; |
---|
60 | return 0; |
---|
61 | } |
---|
62 | |
---|
63 | int cont_fw_delete (struct cont_fw_parent_inst * inst) { |
---|
64 | int i; |
---|
65 | |
---|
66 | ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst); |
---|
67 | |
---|
68 | if ( inst == NULL ) |
---|
69 | return -1; |
---|
70 | |
---|
71 | ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst); |
---|
72 | |
---|
73 | // check if there are streams to close... |
---|
74 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
75 | if ( inst->child[i] != NULL ) { |
---|
76 | ROAR_DBG("cont_fw_delete(inst=%p) = -1 // there are still open childs", inst); |
---|
77 | return -1; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst); |
---|
82 | |
---|
83 | if ( inst->pcb.close != NULL ) |
---|
84 | inst->pcb.close(inst); |
---|
85 | |
---|
86 | free(inst); |
---|
87 | |
---|
88 | ROAR_DBG("cont_fw_delete(inst=%p) = 0", inst); |
---|
89 | |
---|
90 | return 0; |
---|
91 | } |
---|
92 | |
---|
93 | int cont_fw_set_uinst(struct cont_fw_parent_inst * inst, void * u_inst) { |
---|
94 | if ( inst == NULL ) |
---|
95 | return -1; |
---|
96 | |
---|
97 | inst->u_inst = u_inst; |
---|
98 | |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|
102 | int cont_fw_get_uinst(struct cont_fw_parent_inst * inst, void ** u_inst) { |
---|
103 | if ( inst == NULL || u_inst == NULL ) |
---|
104 | return -1; |
---|
105 | |
---|
106 | *u_inst = inst->u_inst; |
---|
107 | |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|
111 | // VIO Client: |
---|
112 | int cont_fw_new_child(struct cont_fw_parent_inst * inst, int id) { |
---|
113 | struct cont_fw_child_vio_inst * self; |
---|
114 | struct roar_stream_server * ss; |
---|
115 | int i; |
---|
116 | int cid = -1; |
---|
117 | |
---|
118 | ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = ?", inst, id); |
---|
119 | |
---|
120 | if ( inst == NULL || id == -1 ) |
---|
121 | return -1; |
---|
122 | |
---|
123 | if ( streams_get(id, &ss) == -1 ) |
---|
124 | return -1; |
---|
125 | |
---|
126 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
127 | if ( inst->child[i] == NULL ) { |
---|
128 | cid = i; |
---|
129 | break; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | if ( cid == -1 ) |
---|
134 | return -1; |
---|
135 | |
---|
136 | if ( (self = malloc(sizeof(struct cont_fw_child_vio_inst))) == NULL ) |
---|
137 | return -1; |
---|
138 | |
---|
139 | memset(self, 0, sizeof(struct cont_fw_child_vio_inst)); |
---|
140 | |
---|
141 | self->parent = inst; |
---|
142 | self->child = id; |
---|
143 | self->u_inst = NULL; |
---|
144 | |
---|
145 | inst->child[i] = self; |
---|
146 | |
---|
147 | if ( inst->pcb.new_child != NULL ) { |
---|
148 | if ( inst->pcb.new_child(inst, self) == -1 ) { |
---|
149 | inst->child[i] = NULL; |
---|
150 | free(self); |
---|
151 | return -1; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | // no error possible here. |
---|
156 | cont_fw_init_vio(&(ss->vio), self); |
---|
157 | // ss->ready = 1; |
---|
158 | |
---|
159 | streams_set_fh(id, -2); // update some internal structures |
---|
160 | |
---|
161 | ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = 0", inst, id); |
---|
162 | return 0; |
---|
163 | } |
---|
164 | |
---|
165 | int cont_fw_init_vio(struct roar_vio_calls * vio, void * inst) { |
---|
166 | if ( vio == NULL ) |
---|
167 | return -1; |
---|
168 | |
---|
169 | memset(vio, 0, sizeof(struct roar_vio_calls)); |
---|
170 | vio->inst = inst; |
---|
171 | |
---|
172 | vio->read = cont_fw_read; |
---|
173 | vio->write = cont_fw_write; |
---|
174 | vio->sync = cont_fw_sync; |
---|
175 | vio->close = cont_fw_close; |
---|
176 | |
---|
177 | return 0; |
---|
178 | } |
---|
179 | |
---|
180 | ssize_t cont_fw_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
181 | _BASIC(); |
---|
182 | |
---|
183 | if ( inst->parent->ccb.read != NULL ) |
---|
184 | return inst->parent->ccb.read(inst->parent, inst, buf, count); |
---|
185 | |
---|
186 | return -1; |
---|
187 | } |
---|
188 | |
---|
189 | ssize_t cont_fw_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
190 | _BASIC(); |
---|
191 | |
---|
192 | if ( inst->parent->ccb.write != NULL ) |
---|
193 | return inst->parent->ccb.write(inst->parent, inst, buf, count); |
---|
194 | |
---|
195 | return -1; |
---|
196 | } |
---|
197 | |
---|
198 | off_t cont_fw_lseek (struct roar_vio_calls * vio, off_t offset, int whence); |
---|
199 | int cont_fw_nonblock(struct roar_vio_calls * vio, int state); |
---|
200 | |
---|
201 | int cont_fw_sync (struct roar_vio_calls * vio) { |
---|
202 | _BASIC(); |
---|
203 | |
---|
204 | if ( inst->parent->ccb.flush != NULL ) |
---|
205 | return inst->parent->ccb.flush(inst->parent, inst); |
---|
206 | |
---|
207 | return 0; |
---|
208 | } |
---|
209 | |
---|
210 | int cont_fw_close (struct roar_vio_calls * vio) { |
---|
211 | _DECL(); |
---|
212 | int r = 0; |
---|
213 | int i; |
---|
214 | |
---|
215 | _PREP(); |
---|
216 | |
---|
217 | if ( cont_fw_sync(vio) == -1 ) |
---|
218 | r = -1; |
---|
219 | |
---|
220 | if ( inst->parent->ccb.close != NULL ) |
---|
221 | if ( inst->parent->ccb.close(inst->parent, inst) == -1 ) |
---|
222 | r = -1; |
---|
223 | |
---|
224 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
225 | if ( inst->parent->child[i] == inst ) { |
---|
226 | inst->parent->child[i] = NULL; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | free(inst); |
---|
231 | |
---|
232 | return r; |
---|
233 | } |
---|
234 | |
---|
235 | int cont_fw_ctl (struct roar_vio_calls * vio, int cmd, void * data); |
---|
236 | |
---|
237 | // VIO Parent: |
---|
238 | int cont_pvio_open (struct roar_vio_calls * vio, void * inst) { |
---|
239 | memset(vio, 0, sizeof(struct roar_vio_calls)); |
---|
240 | |
---|
241 | vio->inst = inst; |
---|
242 | |
---|
243 | vio->read = cont_pvio_read; |
---|
244 | vio->write = cont_pvio_write; |
---|
245 | vio->lseek = cont_pvio_lseek; |
---|
246 | vio->nonblock = cont_pvio_nonblock; |
---|
247 | vio->sync = cont_pvio_sync; |
---|
248 | vio->ctl = cont_pvio_ctl; |
---|
249 | vio->close = cont_pvio_close; |
---|
250 | |
---|
251 | return 0; |
---|
252 | } |
---|
253 | |
---|
254 | ssize_t cont_pvio_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
255 | return stream_vio_s_read(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count); |
---|
256 | } |
---|
257 | |
---|
258 | ssize_t cont_pvio_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
259 | ROAR_DBG("cont_pvio_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count); |
---|
260 | return stream_vio_s_write(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count); |
---|
261 | } |
---|
262 | |
---|
263 | off_t cont_pvio_lseek (struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
264 | return roar_vio_lseek(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), offset, whence); |
---|
265 | } |
---|
266 | |
---|
267 | int cont_pvio_nonblock(struct roar_vio_calls * vio, int state) { |
---|
268 | return roar_vio_nonblock(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), state); |
---|
269 | } |
---|
270 | |
---|
271 | int cont_pvio_sync (struct roar_vio_calls * vio) { |
---|
272 | return roar_vio_sync(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio)); |
---|
273 | } |
---|
274 | |
---|
275 | int cont_pvio_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
276 | return roar_vio_ctl(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), cmd, data); |
---|
277 | } |
---|
278 | |
---|
279 | int cont_pvio_close (struct roar_vio_calls * vio) { |
---|
280 | return roar_vio_close(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio)); |
---|
281 | } |
---|
282 | |
---|
283 | // CF: |
---|
284 | int cont_fw_cf_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
285 | struct roar_stream_server * info, |
---|
286 | struct roar_codecfilter * filter) { |
---|
287 | struct cont_fw_parent_inst * self; |
---|
288 | CONT_FW_SETUP_TYPE((*setup)); |
---|
289 | |
---|
290 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
291 | |
---|
292 | if ( cont_fw_new(&self) == -1 ) |
---|
293 | return -1; |
---|
294 | |
---|
295 | self->stream.codec = codec; |
---|
296 | self->stream.id = ROAR_STREAM(info)->id; |
---|
297 | self->stream.stream = info; |
---|
298 | self->stream.filter = filter; |
---|
299 | |
---|
300 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
301 | |
---|
302 | if ( (setup = filter->setup) != NULL ) { |
---|
303 | if ( setup(self, codec, filter) == -1 ) { |
---|
304 | cont_fw_delete(self); |
---|
305 | return -1; |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
310 | |
---|
311 | *inst = self; |
---|
312 | |
---|
313 | ROAR_DBG("cont_fw_cf_open(*) = 0"); |
---|
314 | return 0; |
---|
315 | } |
---|
316 | |
---|
317 | int cont_fw_cf_close(CODECFILTER_USERDATA_T inst) { |
---|
318 | ROAR_DBG("cont_fw_cf_close(*) = ?"); |
---|
319 | return cont_fw_delete(inst); |
---|
320 | } |
---|
321 | |
---|
322 | int cont_fw_cf_pause(CODECFILTER_USERDATA_T inst, int newstate); |
---|
323 | |
---|
324 | |
---|
325 | // no direct read or writing... |
---|
326 | int cont_fw_cf_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
327 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
328 | |
---|
329 | ROAR_DBG("cont_fw_cf_write(*) = ?"); |
---|
330 | |
---|
331 | if ( self->state == ROAR_STREAMSTATE_INITING ) { |
---|
332 | if ( self->pcb.open != NULL ) { |
---|
333 | if ( self->pcb.open(self, self->stream.codec, self->stream.stream, self->stream.filter) == -1 ) { |
---|
334 | return -1; |
---|
335 | } |
---|
336 | } |
---|
337 | self->state = ROAR_STREAMSTATE_NEW; |
---|
338 | |
---|
339 | ROAR_DBG("cont_fw_cf_write(*) = 0"); |
---|
340 | return 0; |
---|
341 | } else { |
---|
342 | ROAR_DBG("cont_fw_cf_write(*) = -1"); |
---|
343 | return -1; |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | int cont_fw_cf_read (CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
348 | ROAR_DBG("cont_fw_cf_read(*) = -1"); |
---|
349 | return -1; |
---|
350 | } |
---|
351 | |
---|
352 | int cont_fw_cf_flush(CODECFILTER_USERDATA_T inst) { |
---|
353 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
354 | |
---|
355 | ROAR_DBG("cont_fw_cf_flush(*) = ?"); |
---|
356 | |
---|
357 | if ( self->pcb.flush != NULL ) |
---|
358 | return self->pcb.flush(self); |
---|
359 | |
---|
360 | return 0; |
---|
361 | } |
---|
362 | |
---|
363 | int cont_fw_cf_delay(CODECFILTER_USERDATA_T inst, uint_least32_t * delay); |
---|
364 | |
---|
365 | int cont_fw_cf_ctl (CODECFILTER_USERDATA_T inst, int cmd, void * data) { |
---|
366 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
367 | int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK; |
---|
368 | |
---|
369 | ROAR_DBG("cont_fw_cf_ctl(*) = ?"); |
---|
370 | |
---|
371 | cmd -= type; |
---|
372 | |
---|
373 | ROAR_DBG("cont_fw_cf_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
374 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
375 | |
---|
376 | if ( data == NULL && type != ROAR_STREAM_CTL_TYPE_VOID ) |
---|
377 | return -1; |
---|
378 | |
---|
379 | switch (cmd) { |
---|
380 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE): |
---|
381 | return 0; |
---|
382 | break; |
---|
383 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_NEW): |
---|
384 | if ( type != ROAR_STREAM_CTL_TYPE_INT ) |
---|
385 | return -1; |
---|
386 | |
---|
387 | return cont_fw_new_child(self, *(int*)data); |
---|
388 | break; |
---|
389 | default: |
---|
390 | ROAR_DBG("cont_fw_cf_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
391 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
392 | return -1; |
---|
393 | } |
---|
394 | |
---|
395 | return -1; |
---|
396 | } |
---|
397 | |
---|
398 | //ll |
---|