source: roaraudio/roard/container_framework.c @ 2829:d22cf56280bd

Last change on this file since 2829:d22cf56280bd was 2829:d22cf56280bd, checked in by phi, 15 years ago

added vio thingy for parent stream

File size: 9.6 KB
Line 
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:
38int     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
63int     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
93int     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
102int     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:
112int     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
165int     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
180ssize_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
189ssize_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
198off_t   cont_fw_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
199int     cont_fw_nonblock(struct roar_vio_calls * vio, int state);
200
201int     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
210int     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
235int     cont_fw_ctl     (struct roar_vio_calls * vio, int cmd, void * data);
236
237// VIO Parent:
238int     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
254ssize_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
258ssize_t cont_pvio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
259 return stream_vio_s_write(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count);
260}
261
262off_t   cont_pvio_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
263 return roar_vio_lseek(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), offset, whence);
264}
265
266int     cont_pvio_nonblock(struct roar_vio_calls * vio, int state) {
267 return roar_vio_nonblock(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), state);
268}
269
270int     cont_pvio_sync    (struct roar_vio_calls * vio) {
271 return roar_vio_sync(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio));
272}
273
274int     cont_pvio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
275 return roar_vio_ctl(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), cmd, data);
276}
277
278int     cont_pvio_close   (struct roar_vio_calls * vio) {
279 return roar_vio_close(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio));
280}
281
282// CF:
283int cont_fw_cf_open(CODECFILTER_USERDATA_T * inst, int codec,
284                                             struct roar_stream_server * info,
285                                             struct roar_codecfilter   * filter) {
286 struct cont_fw_parent_inst * self;
287 CONT_FW_SETUP_TYPE((*setup));
288
289 ROAR_DBG("cont_fw_cf_open(*) = ?");
290
291 if ( cont_fw_new(&self) == -1 )
292  return -1;
293
294 self->stream.codec  = codec;
295 self->stream.id     = ROAR_STREAM(info)->id;
296 self->stream.stream = info;
297 self->stream.filter = filter;
298
299 ROAR_DBG("cont_fw_cf_open(*) = ?");
300
301 if ( (setup = filter->setup) != NULL ) {
302  if ( setup(self, codec, filter) == -1 ) {
303   cont_fw_delete(self);
304   return -1;
305  }
306 }
307
308 ROAR_DBG("cont_fw_cf_open(*) = ?");
309
310 *inst = self;
311
312 ROAR_DBG("cont_fw_cf_open(*) = 0");
313 return 0;
314}
315
316int cont_fw_cf_close(CODECFILTER_USERDATA_T   inst) {
317 ROAR_DBG("cont_fw_cf_close(*) = ?");
318 return cont_fw_delete(inst);
319}
320
321int cont_fw_cf_pause(CODECFILTER_USERDATA_T   inst, int newstate);
322
323
324// no direct read or writing...
325int cont_fw_cf_write(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
326 struct cont_fw_parent_inst * self = (void*)inst;
327
328 ROAR_DBG("cont_fw_cf_write(*) = ?");
329
330 if ( self->state == ROAR_STREAMSTATE_INITING ) {
331  if ( self->pcb.open != NULL ) {
332   if ( self->pcb.open(self, self->stream.codec, self->stream.stream, self->stream.filter) == -1 ) {
333    return -1;
334   }
335  }
336  self->state = ROAR_STREAMSTATE_NEW;
337
338  ROAR_DBG("cont_fw_cf_write(*) = 0");
339  return 0;
340 } else {
341  ROAR_DBG("cont_fw_cf_write(*) = -1");
342  return -1;
343 }
344}
345
346int cont_fw_cf_read (CODECFILTER_USERDATA_T   inst, char * buf, int len) {
347 ROAR_DBG("cont_fw_cf_read(*) = -1");
348 return -1;
349}
350
351int cont_fw_cf_flush(CODECFILTER_USERDATA_T   inst) {
352 struct cont_fw_parent_inst * self = (void*)inst;
353
354 ROAR_DBG("cont_fw_cf_flush(*) = ?");
355
356 if ( self->pcb.flush != NULL )
357  return self->pcb.flush(self);
358
359 return 0;
360}
361
362int cont_fw_cf_delay(CODECFILTER_USERDATA_T   inst, uint_least32_t * delay);
363
364int cont_fw_cf_ctl  (CODECFILTER_USERDATA_T   inst, int cmd, void * data) {
365 struct cont_fw_parent_inst * self = (void*)inst;
366 int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK;
367
368 ROAR_DBG("cont_fw_cf_ctl(*) = ?");
369
370 cmd -= type;
371
372 ROAR_DBG("cont_fw_cf_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x",
373                    cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd));
374
375 if ( data == NULL && type != ROAR_STREAM_CTL_TYPE_VOID )
376  return -1;
377
378 switch (cmd) {
379  case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE):
380    return 0;
381   break;
382  case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_NEW):
383    if ( type != ROAR_STREAM_CTL_TYPE_INT )
384     return -1;
385
386    return cont_fw_new_child(self, *(int*)data);
387   break;
388  default:
389    ROAR_DBG("cont_fw_cf_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x",
390                    cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd));
391    return -1;
392 }
393
394 return -1;
395}
396
397//ll
Note: See TracBrowser for help on using the repository browser.