[2661] | 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 | |
---|
[2665] | 37 | // Parent: |
---|
[2663] | 38 | int cont_fw_new (struct cont_fw_parent_inst ** inst) { |
---|
[2664] | 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 | |
---|
[2696] | 51 | self->stream.id = -1; |
---|
| 52 | self->state = ROAR_STREAMSTATE_INITING; |
---|
| 53 | |
---|
[2829] | 54 | if ( cont_pvio_open(&(self->vio), self) == -1 ) { |
---|
| 55 | free(self); |
---|
| 56 | return -1; |
---|
| 57 | } |
---|
| 58 | |
---|
[2664] | 59 | *inst = self; |
---|
| 60 | return 0; |
---|
[2663] | 61 | } |
---|
| 62 | |
---|
| 63 | int cont_fw_delete (struct cont_fw_parent_inst * inst) { |
---|
[2664] | 64 | int i; |
---|
| 65 | |
---|
[2713] | 66 | ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst); |
---|
| 67 | |
---|
[2664] | 68 | if ( inst == NULL ) |
---|
| 69 | return -1; |
---|
| 70 | |
---|
[2713] | 71 | ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst); |
---|
| 72 | |
---|
[2664] | 73 | // check if there are streams to close... |
---|
| 74 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
| 75 | if ( inst->child[i] != NULL ) { |
---|
[2713] | 76 | ROAR_DBG("cont_fw_delete(inst=%p) = -1 // there are still open childs", inst); |
---|
[2664] | 77 | return -1; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
[2713] | 81 | ROAR_DBG("cont_fw_delete(inst=%p) = ?", inst); |
---|
| 82 | |
---|
[2670] | 83 | if ( inst->pcb.close != NULL ) |
---|
| 84 | inst->pcb.close(inst); |
---|
| 85 | |
---|
[2664] | 86 | free(inst); |
---|
| 87 | |
---|
[2713] | 88 | ROAR_DBG("cont_fw_delete(inst=%p) = 0", inst); |
---|
| 89 | |
---|
[2664] | 90 | return 0; |
---|
[2663] | 91 | } |
---|
| 92 | |
---|
[2665] | 93 | int 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 | |
---|
| 102 | int 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 | |
---|
[2829] | 111 | // VIO Client: |
---|
[2668] | 112 | int cont_fw_new_child(struct cont_fw_parent_inst * inst, int id) { |
---|
[2672] | 113 | struct cont_fw_child_vio_inst * self; |
---|
[2673] | 114 | struct roar_stream_server * ss; |
---|
[2672] | 115 | int i; |
---|
| 116 | int cid = -1; |
---|
| 117 | |
---|
[2702] | 118 | ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = ?", inst, id); |
---|
| 119 | |
---|
[2672] | 120 | if ( inst == NULL || id == -1 ) |
---|
| 121 | return -1; |
---|
| 122 | |
---|
[2673] | 123 | if ( streams_get(id, &ss) == -1 ) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
[2672] | 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 ) |
---|
[2668] | 134 | return -1; |
---|
| 135 | |
---|
[2672] | 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 | |
---|
[2673] | 155 | // no error possible here. |
---|
| 156 | cont_fw_init_vio(&(ss->vio), self); |
---|
[2716] | 157 | // ss->ready = 1; |
---|
[2673] | 158 | |
---|
[2716] | 159 | streams_set_fh(id, -2); // update some internal structures |
---|
[2673] | 160 | |
---|
[2704] | 161 | ROAR_DBG("cont_fw_new_child(inst=%p, id=%i) = 0", inst, id); |
---|
[2672] | 162 | return 0; |
---|
[2668] | 163 | } |
---|
[2665] | 164 | |
---|
[2662] | 165 | int 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 | |
---|
[2661] | 180 | ssize_t cont_fw_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 181 | _BASIC(); |
---|
| 182 | |
---|
[2667] | 183 | if ( inst->parent->ccb.read != NULL ) |
---|
[2669] | 184 | return inst->parent->ccb.read(inst->parent, inst, buf, count); |
---|
[2661] | 185 | |
---|
| 186 | return -1; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | ssize_t cont_fw_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 190 | _BASIC(); |
---|
| 191 | |
---|
[2667] | 192 | if ( inst->parent->ccb.write != NULL ) |
---|
[2669] | 193 | return inst->parent->ccb.write(inst->parent, inst, buf, count); |
---|
[2661] | 194 | |
---|
| 195 | return -1; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | off_t cont_fw_lseek (struct roar_vio_calls * vio, off_t offset, int whence); |
---|
| 199 | int cont_fw_nonblock(struct roar_vio_calls * vio, int state); |
---|
| 200 | |
---|
| 201 | int 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 | |
---|
| 210 | int 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 | |
---|
| 230 | free(inst); |
---|
| 231 | |
---|
[2661] | 232 | return r; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | int cont_fw_ctl (struct roar_vio_calls * vio, int cmd, void * data); |
---|
| 236 | |
---|
[2829] | 237 | // VIO Parent: |
---|
| 238 | int 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 | |
---|
| 254 | ssize_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 | |
---|
| 258 | ssize_t cont_pvio_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
[2830] | 259 | ROAR_DBG("cont_pvio_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count); |
---|
[2829] | 260 | return stream_vio_s_write(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | off_t cont_pvio_lseek (struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 264 | return roar_vio_lseek(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), offset, whence); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | int cont_pvio_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 268 | return roar_vio_nonblock(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), state); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | int cont_pvio_sync (struct roar_vio_calls * vio) { |
---|
| 272 | return roar_vio_sync(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio)); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | int cont_pvio_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 276 | return roar_vio_ctl(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), cmd, data); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | int cont_pvio_close (struct roar_vio_calls * vio) { |
---|
| 280 | return roar_vio_close(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio)); |
---|
| 281 | } |
---|
| 282 | |
---|
[2675] | 283 | // CF: |
---|
| 284 | int cont_fw_cf_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 285 | struct roar_stream_server * info, |
---|
[2677] | 286 | struct roar_codecfilter * filter) { |
---|
[2678] | 287 | struct cont_fw_parent_inst * self; |
---|
[2683] | 288 | CONT_FW_SETUP_TYPE((*setup)); |
---|
[2678] | 289 | |
---|
[2691] | 290 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
| 291 | |
---|
[2678] | 292 | if ( cont_fw_new(&self) == -1 ) |
---|
| 293 | return -1; |
---|
| 294 | |
---|
[2696] | 295 | self->stream.codec = codec; |
---|
| 296 | self->stream.id = ROAR_STREAM(info)->id; |
---|
| 297 | self->stream.stream = info; |
---|
| 298 | self->stream.filter = filter; |
---|
| 299 | |
---|
[2691] | 300 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
| 301 | |
---|
[2683] | 302 | if ( (setup = filter->setup) != NULL ) { |
---|
| 303 | if ( setup(self, codec, filter) == -1 ) { |
---|
| 304 | cont_fw_delete(self); |
---|
| 305 | return -1; |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | |
---|
[2691] | 309 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
| 310 | |
---|
[2678] | 311 | *inst = self; |
---|
[2691] | 312 | |
---|
| 313 | ROAR_DBG("cont_fw_cf_open(*) = 0"); |
---|
[2678] | 314 | return 0; |
---|
[2677] | 315 | } |
---|
[2675] | 316 | |
---|
| 317 | int cont_fw_cf_close(CODECFILTER_USERDATA_T inst) { |
---|
[2695] | 318 | ROAR_DBG("cont_fw_cf_close(*) = ?"); |
---|
[2675] | 319 | return cont_fw_delete(inst); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | int cont_fw_cf_pause(CODECFILTER_USERDATA_T inst, int newstate); |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | // no direct read or writing... |
---|
| 326 | int cont_fw_cf_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
[2696] | 327 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
| 328 | |
---|
| 329 | ROAR_DBG("cont_fw_cf_write(*) = ?"); |
---|
| 330 | |
---|
| 331 | if ( self->state == ROAR_STREAMSTATE_INITING ) { |
---|
| 332 | if ( self->pcb.open != NULL ) { |
---|
| 333 | if ( self->pcb.open(self, self->stream.codec, self->stream.stream, self->stream.filter) == -1 ) { |
---|
| 334 | return -1; |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | self->state = ROAR_STREAMSTATE_NEW; |
---|
| 338 | |
---|
| 339 | ROAR_DBG("cont_fw_cf_write(*) = 0"); |
---|
| 340 | return 0; |
---|
| 341 | } else { |
---|
| 342 | ROAR_DBG("cont_fw_cf_write(*) = -1"); |
---|
| 343 | return -1; |
---|
| 344 | } |
---|
[2675] | 345 | } |
---|
| 346 | |
---|
| 347 | int cont_fw_cf_read (CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
[2695] | 348 | ROAR_DBG("cont_fw_cf_read(*) = -1"); |
---|
[2675] | 349 | return -1; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | int cont_fw_cf_flush(CODECFILTER_USERDATA_T inst) { |
---|
| 353 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
| 354 | |
---|
[2695] | 355 | ROAR_DBG("cont_fw_cf_flush(*) = ?"); |
---|
| 356 | |
---|
[2675] | 357 | if ( self->pcb.flush != NULL ) |
---|
| 358 | return self->pcb.flush(self); |
---|
| 359 | |
---|
| 360 | return 0; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | int cont_fw_cf_delay(CODECFILTER_USERDATA_T inst, uint_least32_t * delay); |
---|
| 364 | |
---|
| 365 | int cont_fw_cf_ctl (CODECFILTER_USERDATA_T inst, int cmd, void * data) { |
---|
[2676] | 366 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
| 367 | int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK; |
---|
| 368 | |
---|
[2695] | 369 | ROAR_DBG("cont_fw_cf_ctl(*) = ?"); |
---|
| 370 | |
---|
[2676] | 371 | cmd -= type; |
---|
| 372 | |
---|
| 373 | ROAR_DBG("cont_fw_cf_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
| 374 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
| 375 | |
---|
| 376 | if ( data == NULL && type != ROAR_STREAM_CTL_TYPE_VOID ) |
---|
| 377 | return -1; |
---|
| 378 | |
---|
| 379 | switch (cmd) { |
---|
| 380 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE): |
---|
| 381 | return 0; |
---|
| 382 | break; |
---|
| 383 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_NEW): |
---|
| 384 | if ( type != ROAR_STREAM_CTL_TYPE_INT ) |
---|
| 385 | return -1; |
---|
| 386 | |
---|
| 387 | return cont_fw_new_child(self, *(int*)data); |
---|
| 388 | break; |
---|
| 389 | default: |
---|
| 390 | ROAR_DBG("cont_fw_cf_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
| 391 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
| 392 | return -1; |
---|
| 393 | } |
---|
| 394 | |
---|
[2675] | 395 | return -1; |
---|
| 396 | } |
---|
| 397 | |
---|
[2661] | 398 | //ll |
---|