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
RevLine 
[2661]1//container_framework.c:
2
3/*
[5381]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012
[2661]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[2661]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
[2665]38// Parent:
[2663]39int     cont_fw_new     (struct cont_fw_parent_inst ** inst) {
[2664]40 struct cont_fw_parent_inst * self;
41
42 if ( inst == NULL )
43  return -1;
44
[4957]45 if ( (self = roar_mm_malloc(sizeof(struct cont_fw_parent_inst))) == NULL ) {
[2664]46  *inst = NULL;
47  return -1;
48 }
49
50 memset(self, 0, sizeof(struct cont_fw_parent_inst));
51
[2696]52 self->stream.id = -1;
53 self->state     = ROAR_STREAMSTATE_INITING;
54
[2829]55 if ( cont_pvio_open(&(self->vio), self) == -1 ) {
[4957]56  roar_mm_free(self);
[2829]57  return -1;
58 }
59
[2664]60 *inst = self;
61 return 0;
[2663]62}
63
64int     cont_fw_delete  (struct cont_fw_parent_inst  * inst) {
[2664]65 int i;
66
[2713]67 ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst);
68
[2664]69 if ( inst == NULL )
70  return -1;
71
[2713]72 ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst);
73
[2664]74 // check if there are streams to close...
75 for (i = 0; i < CONT_FW_MAX_CHILDS; i++) {
76  if ( inst->child[i] != NULL ) {
[2713]77   ROAR_DBG("cont_fw_delete(inst=%p) = -1 // there are still open childs", inst);
[2664]78   return -1;
79  }
80 }
81
[2713]82 ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst);
83
[2670]84 if ( inst->pcb.close != NULL )
85  inst->pcb.close(inst);
86
[4957]87 roar_mm_free(inst);
[2664]88
[2713]89 ROAR_DBG("cont_fw_delete(inst=%p) = 0", inst);
90
[2664]91 return 0;
[2663]92}
93
[2665]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
[2829]112// VIO Client:
[2668]113int     cont_fw_new_child(struct cont_fw_parent_inst  * inst, int id) {
[2672]114 struct cont_fw_child_vio_inst * self;
[2673]115 struct roar_stream_server     * ss;
[2672]116 int i;
117 int cid = -1;
118
[2702]119 ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = ?", inst, id);
120
[2672]121 if ( inst == NULL || id == -1 )
122  return -1;
123
[2673]124 if ( streams_get(id, &ss) == -1 )
125  return -1;
126
[2672]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 )
[2668]135  return -1;
136
[4957]137 if ( (self = roar_mm_malloc(sizeof(struct cont_fw_child_vio_inst))) == NULL )
[2672]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;
[4957]151   roar_mm_free(self);
[2672]152   return -1;
153  }
154 }
155
[2673]156 // no error possible here.
157 cont_fw_init_vio(&(ss->vio), self);
[2716]158// ss->ready = 1;
[2673]159
[2716]160 streams_set_fh(id, -2); // update some internal structures
[2673]161
[2704]162 ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = 0", inst, id);
[2672]163 return 0;
[2668]164}
[2665]165
[2662]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
[2661]181ssize_t cont_fw_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
182 _BASIC();
183
[2667]184 if ( inst->parent->ccb.read != NULL )
[2669]185  return inst->parent->ccb.read(inst->parent, inst, buf, count);
[2661]186
187 return -1;
188}
189
190ssize_t cont_fw_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
191 _BASIC();
192
[2667]193 if ( inst->parent->ccb.write != NULL )
[2669]194  return inst->parent->ccb.write(inst->parent, inst, buf, count);
[2661]195
196 return -1;
197}
198
[5278]199roar_off_t   cont_fw_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence);
[2661]200
201int     cont_fw_sync    (struct roar_vio_calls * vio) {
202 _BASIC();
203
[2667]204 if ( inst->parent->ccb.flush != NULL )
[2669]205  return inst->parent->ccb.flush(inst->parent, inst);
[2661]206
207 return 0;
208}
209
210int     cont_fw_close   (struct roar_vio_calls * vio) {
211 _DECL();
212 int r = 0;
[2671]213 int i;
[2661]214
215 _PREP();
216
217 if ( cont_fw_sync(vio) == -1 )
218  r = -1;
219
[2667]220 if ( inst->parent->ccb.close != NULL )
[2713]221  if ( inst->parent->ccb.close(inst->parent, inst) == -1 )
222   r = -1;
[2667]223
[2671]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
[4957]230 roar_mm_free(inst);
[2671]231
[2661]232 return  r;
233}
234
[5278]235int     cont_fw_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data);
[2661]236
[2829]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) {
[2830]258 ROAR_DBG("cont_pvio_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
[2829]259 return stream_vio_s_write(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count);
260}
261
[5278]262roar_off_t   cont_pvio_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence) {
[2829]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
[5278]270int     cont_pvio_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
[2829]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
[2675]278// CF:
279int cont_fw_cf_open(CODECFILTER_USERDATA_T * inst, int codec,
280                                             struct roar_stream_server * info,
[2677]281                                             struct roar_codecfilter   * filter) {
[2678]282 struct cont_fw_parent_inst * self;
[2683]283 CONT_FW_SETUP_TYPE((*setup));
[2678]284
[2691]285 ROAR_DBG("cont_fw_cf_open(*) = ?");
286
[2678]287 if ( cont_fw_new(&self) == -1 )
288  return -1;
289
[2696]290 self->stream.codec  = codec;
291 self->stream.id     = ROAR_STREAM(info)->id;
292 self->stream.stream = info;
293 self->stream.filter = filter;
294
[2691]295 ROAR_DBG("cont_fw_cf_open(*) = ?");
296
[2683]297 if ( (setup = filter->setup) != NULL ) {
298  if ( setup(self, codec, filter) == -1 ) {
299   cont_fw_delete(self);
300   return -1;
301  }
302 }
303
[2691]304 ROAR_DBG("cont_fw_cf_open(*) = ?");
305
[2678]306 *inst = self;
[2691]307
308 ROAR_DBG("cont_fw_cf_open(*) = 0");
[2678]309 return 0;
[2677]310}
[2675]311
312int cont_fw_cf_close(CODECFILTER_USERDATA_T   inst) {
[2695]313 ROAR_DBG("cont_fw_cf_close(*) = ?");
[2675]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) {
[2696]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 }
[2675]340}
341
342int cont_fw_cf_read (CODECFILTER_USERDATA_T   inst, char * buf, int len) {
[2695]343 ROAR_DBG("cont_fw_cf_read(*) = -1");
[2675]344 return -1;
345}
346
347int cont_fw_cf_flush(CODECFILTER_USERDATA_T   inst) {
348 struct cont_fw_parent_inst * self = (void*)inst;
349
[2695]350 ROAR_DBG("cont_fw_cf_flush(*) = ?");
351
[2675]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) {
[2676]361 struct cont_fw_parent_inst * self = (void*)inst;
362 int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK;
363
[2695]364 ROAR_DBG("cont_fw_cf_ctl(*) = ?");
365
[2676]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
[2675]390 return -1;
391}
392
[2661]393//ll
Note: See TracBrowser for help on using the repository browser.