source: roaraudio/roard/container_framework.c @ 5381:430b1d26e12d

Last change on this file since 5381:430b1d26e12d was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

File size: 9.6 KB
Line 
1//container_framework.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#define _DECL()  struct cont_fw_child_vio_inst * inst
29#define _CHECK() if ( vio == NULL ) \
30                 return -1
31#define _CINST() if ( (inst = vio->inst) == NULL ) \
32                  return -1
33#define _PREP()  _CHECK(); \
34                 _CINST()
35#define _BASIC() _DECL();  \
36                 _PREP()
37
38// Parent:
39int     cont_fw_new     (struct cont_fw_parent_inst ** inst) {
40 struct cont_fw_parent_inst * self;
41
42 if ( inst == NULL )
43  return -1;
44
45 if ( (self = roar_mm_malloc(sizeof(struct cont_fw_parent_inst))) == NULL ) {
46  *inst = NULL;
47  return -1;
48 }
49
50 memset(self, 0, sizeof(struct cont_fw_parent_inst));
51
52 self->stream.id = -1;
53 self->state     = ROAR_STREAMSTATE_INITING;
54
55 if ( cont_pvio_open(&(self->vio), self) == -1 ) {
56  roar_mm_free(self);
57  return -1;
58 }
59
60 *inst = self;
61 return 0;
62}
63
64int     cont_fw_delete  (struct cont_fw_parent_inst  * inst) {
65 int i;
66
67 ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst);
68
69 if ( inst == NULL )
70  return -1;
71
72 ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst);
73
74 // check if there are streams to close...
75 for (i = 0; i < CONT_FW_MAX_CHILDS; i++) {
76  if ( inst->child[i] != NULL ) {
77   ROAR_DBG("cont_fw_delete(inst=%p) = -1 // there are still open childs", inst);
78   return -1;
79  }
80 }
81
82 ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst);
83
84 if ( inst->pcb.close != NULL )
85  inst->pcb.close(inst);
86
87 roar_mm_free(inst);
88
89 ROAR_DBG("cont_fw_delete(inst=%p) = 0", inst);
90
91 return 0;
92}
93
94int     cont_fw_set_uinst(struct cont_fw_parent_inst  * inst, void  * u_inst) {
95 if ( inst == NULL )
96  return -1;
97
98 inst->u_inst = u_inst;
99
100 return 0;
101}
102
103int     cont_fw_get_uinst(struct cont_fw_parent_inst  * inst, void ** u_inst) {
104 if ( inst == NULL || u_inst == NULL )
105  return -1;
106
107 *u_inst = inst->u_inst;
108
109 return 0;
110}
111
112// VIO Client:
113int     cont_fw_new_child(struct cont_fw_parent_inst  * inst, int id) {
114 struct cont_fw_child_vio_inst * self;
115 struct roar_stream_server     * ss;
116 int i;
117 int cid = -1;
118
119 ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = ?", inst, id);
120
121 if ( inst == NULL || id == -1 )
122  return -1;
123
124 if ( streams_get(id, &ss) == -1 )
125  return -1;
126
127 for (i = 0; i < CONT_FW_MAX_CHILDS; i++) {
128  if ( inst->child[i] == NULL ) {
129   cid = i;
130   break;
131  }
132 }
133
134 if ( cid == -1 )
135  return -1;
136
137 if ( (self = roar_mm_malloc(sizeof(struct cont_fw_child_vio_inst))) == NULL )
138  return -1;
139
140 memset(self, 0, sizeof(struct cont_fw_child_vio_inst));
141
142 self->parent = inst;
143 self->child  = id;
144 self->u_inst = NULL;
145
146 inst->child[i] = self;
147
148 if ( inst->pcb.new_child != NULL ) {
149  if ( inst->pcb.new_child(inst, self) == -1 ) {
150   inst->child[i] = NULL;
151   roar_mm_free(self);
152   return -1;
153  }
154 }
155
156 // no error possible here.
157 cont_fw_init_vio(&(ss->vio), self);
158// ss->ready = 1;
159
160 streams_set_fh(id, -2); // update some internal structures
161
162 ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = 0", inst, id);
163 return 0;
164}
165
166int     cont_fw_init_vio(struct roar_vio_calls * vio, void * inst) {
167 if ( vio == NULL )
168  return -1;
169
170 memset(vio, 0, sizeof(struct roar_vio_calls));
171 vio->inst = inst;
172
173 vio->read  = cont_fw_read;
174 vio->write = cont_fw_write;
175 vio->sync  = cont_fw_sync;
176 vio->close = cont_fw_close;
177
178 return 0;
179}
180
181ssize_t cont_fw_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
182 _BASIC();
183
184 if ( inst->parent->ccb.read != NULL )
185  return inst->parent->ccb.read(inst->parent, inst, buf, count);
186
187 return -1;
188}
189
190ssize_t cont_fw_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
191 _BASIC();
192
193 if ( inst->parent->ccb.write != NULL )
194  return inst->parent->ccb.write(inst->parent, inst, buf, count);
195
196 return -1;
197}
198
199roar_off_t   cont_fw_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence);
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 roar_mm_free(inst);
231
232 return  r;
233}
234
235int     cont_fw_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t 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->sync     = cont_pvio_sync;
247 vio->ctl      = cont_pvio_ctl;
248 vio->close    = cont_pvio_close;
249
250 return 0;
251}
252
253ssize_t cont_pvio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
254 return stream_vio_s_read(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count);
255}
256
257ssize_t cont_pvio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
258 ROAR_DBG("cont_pvio_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
259 return stream_vio_s_write(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count);
260}
261
262roar_off_t   cont_pvio_lseek   (struct roar_vio_calls * vio, roar_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_sync    (struct roar_vio_calls * vio) {
267 return roar_vio_sync(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio));
268}
269
270int     cont_pvio_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
271 return roar_vio_ctl(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), cmd, data);
272}
273
274int     cont_pvio_close   (struct roar_vio_calls * vio) {
275 return roar_vio_close(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio));
276}
277
278// CF:
279int cont_fw_cf_open(CODECFILTER_USERDATA_T * inst, int codec,
280                                             struct roar_stream_server * info,
281                                             struct roar_codecfilter   * filter) {
282 struct cont_fw_parent_inst * self;
283 CONT_FW_SETUP_TYPE((*setup));
284
285 ROAR_DBG("cont_fw_cf_open(*) = ?");
286
287 if ( cont_fw_new(&self) == -1 )
288  return -1;
289
290 self->stream.codec  = codec;
291 self->stream.id     = ROAR_STREAM(info)->id;
292 self->stream.stream = info;
293 self->stream.filter = filter;
294
295 ROAR_DBG("cont_fw_cf_open(*) = ?");
296
297 if ( (setup = filter->setup) != NULL ) {
298  if ( setup(self, codec, filter) == -1 ) {
299   cont_fw_delete(self);
300   return -1;
301  }
302 }
303
304 ROAR_DBG("cont_fw_cf_open(*) = ?");
305
306 *inst = self;
307
308 ROAR_DBG("cont_fw_cf_open(*) = 0");
309 return 0;
310}
311
312int cont_fw_cf_close(CODECFILTER_USERDATA_T   inst) {
313 ROAR_DBG("cont_fw_cf_close(*) = ?");
314 return cont_fw_delete(inst);
315}
316
317int cont_fw_cf_pause(CODECFILTER_USERDATA_T   inst, int newstate);
318
319
320// no direct read or writing...
321int cont_fw_cf_write(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
322 struct cont_fw_parent_inst * self = (void*)inst;
323
324 ROAR_DBG("cont_fw_cf_write(*) = ?");
325
326 if ( self->state == ROAR_STREAMSTATE_INITING ) {
327  if ( self->pcb.open != NULL ) {
328   if ( self->pcb.open(self, self->stream.codec, self->stream.stream, self->stream.filter) == -1 ) {
329    return -1;
330   }
331  }
332  self->state = ROAR_STREAMSTATE_NEW;
333
334  ROAR_DBG("cont_fw_cf_write(*) = 0");
335  return 0;
336 } else {
337  ROAR_DBG("cont_fw_cf_write(*) = -1");
338  return -1;
339 }
340}
341
342int cont_fw_cf_read (CODECFILTER_USERDATA_T   inst, char * buf, int len) {
343 ROAR_DBG("cont_fw_cf_read(*) = -1");
344 return -1;
345}
346
347int cont_fw_cf_flush(CODECFILTER_USERDATA_T   inst) {
348 struct cont_fw_parent_inst * self = (void*)inst;
349
350 ROAR_DBG("cont_fw_cf_flush(*) = ?");
351
352 if ( self->pcb.flush != NULL )
353  return self->pcb.flush(self);
354
355 return 0;
356}
357
358int cont_fw_cf_delay(CODECFILTER_USERDATA_T   inst, uint_least32_t * delay);
359
360int cont_fw_cf_ctl  (CODECFILTER_USERDATA_T   inst, int cmd, void * data) {
361 struct cont_fw_parent_inst * self = (void*)inst;
362 int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK;
363
364 ROAR_DBG("cont_fw_cf_ctl(*) = ?");
365
366 cmd -= type;
367
368 ROAR_DBG("cont_fw_cf_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x",
369                    cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd));
370
371 if ( data == NULL && type != ROAR_STREAM_CTL_TYPE_VOID )
372  return -1;
373
374 switch (cmd) {
375  case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE):
376    return 0;
377   break;
378  case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_NEW):
379    if ( type != ROAR_STREAM_CTL_TYPE_INT )
380     return -1;
381
382    return cont_fw_new_child(self, *(int*)data);
383   break;
384  default:
385    ROAR_DBG("cont_fw_cf_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x",
386                    cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd));
387    return -1;
388 }
389
390 return -1;
391}
392
393//ll
Note: See TracBrowser for help on using the repository browser.