[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 |
---|
[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] | 39 | int 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 | |
---|
| 45 | if ( (self = 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 | |
---|
[2696] | 52 | self->stream.id = -1; |
---|
| 53 | self->state = ROAR_STREAMSTATE_INITING; |
---|
| 54 | |
---|
[2829] | 55 | if ( cont_pvio_open(&(self->vio), self) == -1 ) { |
---|
| 56 | free(self); |
---|
| 57 | return -1; |
---|
| 58 | } |
---|
| 59 | |
---|
[2664] | 60 | *inst = self; |
---|
| 61 | return 0; |
---|
[2663] | 62 | } |
---|
| 63 | |
---|
| 64 | int 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 | |
---|
[2664] | 87 | free(inst); |
---|
| 88 | |
---|
[2713] | 89 | ROAR_DBG("cont_fw_delete(inst=%p) = 0", inst); |
---|
| 90 | |
---|
[2664] | 91 | return 0; |
---|
[2663] | 92 | } |
---|
| 93 | |
---|
[2665] | 94 | int 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 | |
---|
| 103 | int 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] | 113 | int 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 | |
---|
[2672] | 137 | if ( (self = 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 | free(self); |
---|
| 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] | 166 | int 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] | 181 | ssize_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 | |
---|
| 190 | ssize_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 | |
---|
| 199 | off_t cont_fw_lseek (struct roar_vio_calls * vio, off_t offset, int whence); |
---|
| 200 | int cont_fw_nonblock(struct roar_vio_calls * vio, int state); |
---|
| 201 | |
---|
| 202 | int cont_fw_sync (struct roar_vio_calls * vio) { |
---|
| 203 | _BASIC(); |
---|
| 204 | |
---|
[2667] | 205 | if ( inst->parent->ccb.flush != NULL ) |
---|
[2669] | 206 | return inst->parent->ccb.flush(inst->parent, inst); |
---|
[2661] | 207 | |
---|
| 208 | return 0; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | int cont_fw_close (struct roar_vio_calls * vio) { |
---|
| 212 | _DECL(); |
---|
| 213 | int r = 0; |
---|
[2671] | 214 | int i; |
---|
[2661] | 215 | |
---|
| 216 | _PREP(); |
---|
| 217 | |
---|
| 218 | if ( cont_fw_sync(vio) == -1 ) |
---|
| 219 | r = -1; |
---|
| 220 | |
---|
[2667] | 221 | if ( inst->parent->ccb.close != NULL ) |
---|
[2713] | 222 | if ( inst->parent->ccb.close(inst->parent, inst) == -1 ) |
---|
| 223 | r = -1; |
---|
[2667] | 224 | |
---|
[2671] | 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 | free(inst); |
---|
| 232 | |
---|
[2661] | 233 | return r; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | int cont_fw_ctl (struct roar_vio_calls * vio, int cmd, void * data); |
---|
| 237 | |
---|
[2829] | 238 | // VIO Parent: |
---|
| 239 | int cont_pvio_open (struct roar_vio_calls * vio, void * inst) { |
---|
| 240 | memset(vio, 0, sizeof(struct roar_vio_calls)); |
---|
| 241 | |
---|
| 242 | vio->inst = inst; |
---|
| 243 | |
---|
| 244 | vio->read = cont_pvio_read; |
---|
| 245 | vio->write = cont_pvio_write; |
---|
| 246 | vio->lseek = cont_pvio_lseek; |
---|
| 247 | vio->nonblock = cont_pvio_nonblock; |
---|
| 248 | vio->sync = cont_pvio_sync; |
---|
| 249 | vio->ctl = cont_pvio_ctl; |
---|
| 250 | vio->close = cont_pvio_close; |
---|
| 251 | |
---|
| 252 | return 0; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | ssize_t cont_pvio_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 256 | return stream_vio_s_read(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | ssize_t cont_pvio_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
[2830] | 260 | ROAR_DBG("cont_pvio_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count); |
---|
[2829] | 261 | return stream_vio_s_write(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream, buf, count); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | off_t cont_pvio_lseek (struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 265 | return roar_vio_lseek(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), offset, whence); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | int cont_pvio_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 269 | return roar_vio_nonblock(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), state); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | int cont_pvio_sync (struct roar_vio_calls * vio) { |
---|
| 273 | return roar_vio_sync(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio)); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | int cont_pvio_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 277 | return roar_vio_ctl(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio), cmd, data); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | int cont_pvio_close (struct roar_vio_calls * vio) { |
---|
| 281 | return roar_vio_close(&(((struct cont_fw_parent_inst*)(vio->inst))->stream.stream->vio)); |
---|
| 282 | } |
---|
| 283 | |
---|
[2675] | 284 | // CF: |
---|
| 285 | int cont_fw_cf_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 286 | struct roar_stream_server * info, |
---|
[2677] | 287 | struct roar_codecfilter * filter) { |
---|
[2678] | 288 | struct cont_fw_parent_inst * self; |
---|
[2683] | 289 | CONT_FW_SETUP_TYPE((*setup)); |
---|
[2678] | 290 | |
---|
[2691] | 291 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
| 292 | |
---|
[2678] | 293 | if ( cont_fw_new(&self) == -1 ) |
---|
| 294 | return -1; |
---|
| 295 | |
---|
[2696] | 296 | self->stream.codec = codec; |
---|
| 297 | self->stream.id = ROAR_STREAM(info)->id; |
---|
| 298 | self->stream.stream = info; |
---|
| 299 | self->stream.filter = filter; |
---|
| 300 | |
---|
[2691] | 301 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
| 302 | |
---|
[2683] | 303 | if ( (setup = filter->setup) != NULL ) { |
---|
| 304 | if ( setup(self, codec, filter) == -1 ) { |
---|
| 305 | cont_fw_delete(self); |
---|
| 306 | return -1; |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
[2691] | 310 | ROAR_DBG("cont_fw_cf_open(*) = ?"); |
---|
| 311 | |
---|
[2678] | 312 | *inst = self; |
---|
[2691] | 313 | |
---|
| 314 | ROAR_DBG("cont_fw_cf_open(*) = 0"); |
---|
[2678] | 315 | return 0; |
---|
[2677] | 316 | } |
---|
[2675] | 317 | |
---|
| 318 | int cont_fw_cf_close(CODECFILTER_USERDATA_T inst) { |
---|
[2695] | 319 | ROAR_DBG("cont_fw_cf_close(*) = ?"); |
---|
[2675] | 320 | return cont_fw_delete(inst); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | int cont_fw_cf_pause(CODECFILTER_USERDATA_T inst, int newstate); |
---|
| 324 | |
---|
| 325 | |
---|
| 326 | // no direct read or writing... |
---|
| 327 | int cont_fw_cf_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
[2696] | 328 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
| 329 | |
---|
| 330 | ROAR_DBG("cont_fw_cf_write(*) = ?"); |
---|
| 331 | |
---|
| 332 | if ( self->state == ROAR_STREAMSTATE_INITING ) { |
---|
| 333 | if ( self->pcb.open != NULL ) { |
---|
| 334 | if ( self->pcb.open(self, self->stream.codec, self->stream.stream, self->stream.filter) == -1 ) { |
---|
| 335 | return -1; |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | self->state = ROAR_STREAMSTATE_NEW; |
---|
| 339 | |
---|
| 340 | ROAR_DBG("cont_fw_cf_write(*) = 0"); |
---|
| 341 | return 0; |
---|
| 342 | } else { |
---|
| 343 | ROAR_DBG("cont_fw_cf_write(*) = -1"); |
---|
| 344 | return -1; |
---|
| 345 | } |
---|
[2675] | 346 | } |
---|
| 347 | |
---|
| 348 | int cont_fw_cf_read (CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
[2695] | 349 | ROAR_DBG("cont_fw_cf_read(*) = -1"); |
---|
[2675] | 350 | return -1; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | int cont_fw_cf_flush(CODECFILTER_USERDATA_T inst) { |
---|
| 354 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
| 355 | |
---|
[2695] | 356 | ROAR_DBG("cont_fw_cf_flush(*) = ?"); |
---|
| 357 | |
---|
[2675] | 358 | if ( self->pcb.flush != NULL ) |
---|
| 359 | return self->pcb.flush(self); |
---|
| 360 | |
---|
| 361 | return 0; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | int cont_fw_cf_delay(CODECFILTER_USERDATA_T inst, uint_least32_t * delay); |
---|
| 365 | |
---|
| 366 | int cont_fw_cf_ctl (CODECFILTER_USERDATA_T inst, int cmd, void * data) { |
---|
[2676] | 367 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
| 368 | int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK; |
---|
| 369 | |
---|
[2695] | 370 | ROAR_DBG("cont_fw_cf_ctl(*) = ?"); |
---|
| 371 | |
---|
[2676] | 372 | cmd -= type; |
---|
| 373 | |
---|
| 374 | ROAR_DBG("cont_fw_cf_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
| 375 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
| 376 | |
---|
| 377 | if ( data == NULL && type != ROAR_STREAM_CTL_TYPE_VOID ) |
---|
| 378 | return -1; |
---|
| 379 | |
---|
| 380 | switch (cmd) { |
---|
| 381 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE): |
---|
| 382 | return 0; |
---|
| 383 | break; |
---|
| 384 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_NEW): |
---|
| 385 | if ( type != ROAR_STREAM_CTL_TYPE_INT ) |
---|
| 386 | return -1; |
---|
| 387 | |
---|
| 388 | return cont_fw_new_child(self, *(int*)data); |
---|
| 389 | break; |
---|
| 390 | default: |
---|
| 391 | ROAR_DBG("cont_fw_cf_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
| 392 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
| 393 | return -1; |
---|
| 394 | } |
---|
| 395 | |
---|
[2675] | 396 | return -1; |
---|
| 397 | } |
---|
| 398 | |
---|
[2661] | 399 | //ll |
---|