source: roaraudio/roard/container_framework.c @ 5823:f9f70dbaa376

Last change on this file since 5823:f9f70dbaa376 was 5823:f9f70dbaa376, checked in by phi, 11 years ago

updated copyright

File size: 9.7 KB
Line 
1//container_framework.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2013
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->flags = ROAR_VIO_FLAGS_NONE;
172 vio->refc  = 1;
173 vio->inst  = inst;
174 vio->read  = cont_fw_read;
175 vio->write = cont_fw_write;
176 vio->sync  = cont_fw_sync;
177 vio->close = cont_fw_close;
178
179 return 0;
180}
181
182ssize_t cont_fw_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
183 _BASIC();
184
185 if ( inst->parent->ccb.read != NULL )
186  return inst->parent->ccb.read(inst->parent, inst, buf, count);
187
188 return -1;
189}
190
191ssize_t cont_fw_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
192 _BASIC();
193
194 if ( inst->parent->ccb.write != NULL )
195  return inst->parent->ccb.write(inst->parent, inst, buf, count);
196
197 return -1;
198}
199
200roar_off_t   cont_fw_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence);
201
202int     cont_fw_sync    (struct roar_vio_calls * vio) {
203 _BASIC();
204
205 if ( inst->parent->ccb.flush != NULL )
206  return inst->parent->ccb.flush(inst->parent, inst);
207
208 return 0;
209}
210
211int     cont_fw_close   (struct roar_vio_calls * vio) {
212 _DECL();
213 int r = 0;
214 int i;
215
216 _PREP();
217
218 if ( cont_fw_sync(vio) == -1 )
219  r = -1;
220
221 if ( inst->parent->ccb.close != NULL )
222  if ( inst->parent->ccb.close(inst->parent, inst) == -1 )
223   r = -1;
224
225 for (i = 0; i < CONT_FW_MAX_CHILDS; i++) {
226  if ( inst->parent->child[i] == inst ) {
227   inst->parent->child[i] = NULL;
228  }
229 }
230
231 roar_mm_free(inst);
232
233 return  r;
234}
235
236int     cont_fw_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data);
237
238// VIO Parent:
239int     cont_pvio_open    (struct roar_vio_calls * vio, void * inst) {
240 memset(vio, 0, sizeof(struct roar_vio_calls));
241 vio->flags = ROAR_VIO_FLAGS_NONE;
242 vio->refc  = 1;
243
244 vio->inst  = inst;
245
246 vio->read     = cont_pvio_read;
247 vio->write    = cont_pvio_write;
248 vio->lseek    = cont_pvio_lseek;
249 vio->sync     = cont_pvio_sync;
250 vio->ctl      = cont_pvio_ctl;
251 vio->close    = cont_pvio_close;
252
253 return 0;
254}
255
256ssize_t cont_pvio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
257 return stream_vio_s_read(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count);
258}
259
260ssize_t cont_pvio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
261 ROAR_DBG("cont_pvio_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
262 return stream_vio_s_write(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count);
263}
264
265roar_off_t   cont_pvio_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence) {
266 return roar_vio_lseek(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), offset, whence);
267}
268
269int     cont_pvio_sync    (struct roar_vio_calls * vio) {
270 return roar_vio_sync(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio));
271}
272
273int     cont_pvio_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
274 return roar_vio_ctl(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), cmd, data);
275}
276
277int     cont_pvio_close   (struct roar_vio_calls * vio) {
278 return roar_vio_close(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio));
279}
280
281// CF:
282int cont_fw_cf_open(CODECFILTER_USERDATA_T * inst, int codec,
283                                             struct roar_stream_server * info,
284                                             struct roar_codecfilter   * filter) {
285 struct cont_fw_parent_inst * self;
286 CONT_FW_SETUP_TYPE((*setup));
287
288 ROAR_DBG("cont_fw_cf_open(*) = ?");
289
290 if ( cont_fw_new(&self) == -1 )
291  return -1;
292
293 self->stream.codec  = codec;
294 self->stream.id     = ROAR_STREAM(info)->id;
295 self->stream.stream = info;
296 self->stream.filter = filter;
297
298 ROAR_DBG("cont_fw_cf_open(*) = ?");
299
300 if ( (setup = filter->setup) != NULL ) {
301  if ( setup(self, codec, filter) == -1 ) {
302   cont_fw_delete(self);
303   return -1;
304  }
305 }
306
307 ROAR_DBG("cont_fw_cf_open(*) = ?");
308
309 *inst = self;
310
311 ROAR_DBG("cont_fw_cf_open(*) = 0");
312 return 0;
313}
314
315int cont_fw_cf_close(CODECFILTER_USERDATA_T   inst) {
316 ROAR_DBG("cont_fw_cf_close(*) = ?");
317 return cont_fw_delete(inst);
318}
319
320int cont_fw_cf_pause(CODECFILTER_USERDATA_T   inst, int newstate);
321
322
323// no direct read or writing...
324int cont_fw_cf_write(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
325 struct cont_fw_parent_inst * self = (void*)inst;
326
327 ROAR_DBG("cont_fw_cf_write(*) = ?");
328
329 if ( self->state == ROAR_STREAMSTATE_INITING ) {
330  if ( self->pcb.open != NULL ) {
331   if ( self->pcb.open(self, self->stream.codec, self->stream.stream, self->stream.filter) == -1 ) {
332    return -1;
333   }
334  }
335  self->state = ROAR_STREAMSTATE_NEW;
336
337  ROAR_DBG("cont_fw_cf_write(*) = 0");
338  return 0;
339 } else {
340  ROAR_DBG("cont_fw_cf_write(*) = -1");
341  return -1;
342 }
343}
344
345int cont_fw_cf_read (CODECFILTER_USERDATA_T   inst, char * buf, int len) {
346 ROAR_DBG("cont_fw_cf_read(*) = -1");
347 return -1;
348}
349
350int cont_fw_cf_flush(CODECFILTER_USERDATA_T   inst) {
351 struct cont_fw_parent_inst * self = (void*)inst;
352
353 ROAR_DBG("cont_fw_cf_flush(*) = ?");
354
355 if ( self->pcb.flush != NULL )
356  return self->pcb.flush(self);
357
358 return 0;
359}
360
361int cont_fw_cf_delay(CODECFILTER_USERDATA_T   inst, uint_least32_t * delay);
362
363int cont_fw_cf_ctl  (CODECFILTER_USERDATA_T   inst, int cmd, void * data) {
364 struct cont_fw_parent_inst * self = (void*)inst;
365 int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK;
366
367 ROAR_DBG("cont_fw_cf_ctl(*) = ?");
368
369 cmd -= type;
370
371 ROAR_DBG("cont_fw_cf_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x",
372                    cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd));
373
374 if ( data == NULL && type != ROAR_STREAM_CTL_TYPE_VOID )
375  return -1;
376
377 switch (cmd) {
378  case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE):
379    return 0;
380   break;
381  case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_NEW):
382    if ( type != ROAR_STREAM_CTL_TYPE_INT )
383     return -1;
384
385    return cont_fw_new_child(self, *(int*)data);
386   break;
387  default:
388    ROAR_DBG("cont_fw_cf_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x",
389                    cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd));
390    return -1;
391 }
392
393 return -1;
394}
395
396//ll
Note: See TracBrowser for help on using the repository browser.