source: roaraudio/roard/container_framework.c @ 2704:43b2391e7001

Last change on this file since 2704:43b2391e7001 was 2704:43b2391e7001, checked in by phi, 15 years ago

got first child stream to work

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